*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35 (require 'nnmail)
36 (require 'backquote)
37
38 (eval-when-compile (require 'cl))
39
40 ;;;###autoload
41 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
42   "*Directory variable from which all other Gnus file variables are derived.")
43
44 ;; Site dependent variables.  These variables should be defined in
45 ;; paths.el.
46
47 (defvar gnus-default-nntp-server nil
48   "Specify a default NNTP server.
49 This variable should be defined in paths.el, and should never be set
50 by the user.
51 If you want to change servers, you should use `gnus-select-method'.
52 See the documentation to that variable.")
53
54 (defvar gnus-backup-default-subscribed-newsgroups
55   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
56   "Default default new newsgroups the first time Gnus is run.
57 Should be set in paths.el, and shouldn't be touched by the user.")
58
59 (defvar gnus-local-domain nil
60   "Local domain name without a host name.
61 The DOMAINNAME environment variable is used instead if it is defined.
62 If the `system-name' function returns the full Internet name, there is
63 no need to set this variable.")
64
65 (defvar gnus-local-organization nil
66   "String with a description of what organization (if any) the user belongs to.
67 The ORGANIZATION environment variable is used instead if it is defined.
68 If this variable contains a function, this function will be called
69 with the current newsgroup name as the argument.  The function should
70 return a string.
71
72 In any case, if the string (either in the variable, in the environment
73 variable, or returned by the function) is a file name, the contents of
74 this file will be used as the organization.")
75
76 ;; Customization variables
77
78 ;; Don't touch this variable.
79 (defvar gnus-nntp-service "nntp"
80   "*NNTP service name (\"nntp\" or 119).
81 This is an obsolete variable, which is scarcely used.  If you use an
82 nntp server for your newsgroup and want to change the port number
83 used to 899, you would say something along these lines:
84
85  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
86
87 (defvar gnus-nntpserver-file "/etc/nntpserver"
88   "*A file with only the name of the nntp server in it.")
89
90 ;; This function is used to check both the environment variable
91 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
92 ;; an nntp server name default.
93 (defun gnus-getenv-nntpserver ()
94   (or (getenv "NNTPSERVER")
95       (and (file-readable-p gnus-nntpserver-file)
96            (save-excursion
97              (set-buffer (get-buffer-create " *gnus nntp*"))
98              (buffer-disable-undo (current-buffer))
99              (insert-file-contents gnus-nntpserver-file)
100              (let ((name (buffer-string)))
101                (prog1
102                    (if (string-match "^[ \t\n]*$" name)
103                        nil
104                      name)
105                  (kill-buffer (current-buffer))))))))
106
107 (defvar gnus-select-method
108   (nconc
109    (list 'nntp (or (condition-case ()
110                        (gnus-getenv-nntpserver)
111                      (error nil))
112                    (if (and gnus-default-nntp-server
113                             (not (string= gnus-default-nntp-server "")))
114                        gnus-default-nntp-server)
115                    (system-name)))
116    (if (or (null gnus-nntp-service)
117            (equal gnus-nntp-service "nntp"))
118        nil
119      (list gnus-nntp-service)))
120   "*Default method for selecting a newsgroup.
121 This variable should be a list, where the first element is how the
122 news is to be fetched, the second is the address.
123
124 For instance, if you want to get your news via NNTP from
125 \"flab.flab.edu\", you could say:
126
127 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
128
129 If you want to use your local spool, say:
130
131 (setq gnus-select-method (list 'nnspool (system-name)))
132
133 If you use this variable, you must set `gnus-nntp-server' to nil.
134
135 There is a lot more to know about select methods and virtual servers -
136 see the manual for details.")
137
138 (defvar gnus-message-archive-method 
139   `(nnfolder
140     "archive"
141     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
142     (nnfolder-active-file 
143      ,(nnheader-concat message-directory "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@sunsite.auc.dk:/pub/usenet/"
190     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
191     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
192     "/ftp@rtfm.mit.edu:/pub/usenet/"
193     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
194     "/ftp@ftp.sunet.se:/pub/usenet/"
195     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
196     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
197     "/ftp@ftp.hk.super.net:/mirror/faqs/")
198   "*Directory where the group FAQs are stored.
199 This will most commonly be on a remote machine, and the file will be
200 fetched by ange-ftp.
201
202 This variable can also be a list of directories.  In that case, the
203 first element in the list will be used by default, and the others will
204 be used as backup sites.
205
206 Note that Gnus uses an aol machine as the default directory.  If this
207 feels fundamentally unclean, just think of it as a way to finally get
208 something of value back from them.
209
210 If the default site is too slow, try one of these:
211
212    North America: mirrors.aol.com                /pub/rtfm/usenet
213                   ftp.seas.gwu.edu               /pub/rtfm
214                   rtfm.mit.edu                   /pub/usenet
215    Europe:        ftp.uni-paderborn.de           /pub/FAQ
216                   src.doc.ic.ac.uk               /usenet/news-FAQS
217                   ftp.sunet.se                   /pub/usenet
218                   sunsite.auc.dk                 /pub/usenet
219    Asia:          nctuccca.edu.tw                /USENET/FAQ
220                   hwarang.postech.ac.kr          /pub/usenet
221                   ftp.hk.super.net               /mirror/faqs")
222
223 (defvar gnus-group-archive-directory
224   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
225   "*The address of the (ding) archives.")
226
227 (defvar gnus-group-recent-archive-directory
228   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
229   "*The address of the most recent (ding) articles.")
230
231 (defvar gnus-default-subscribed-newsgroups nil
232   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
233 It should be a list of strings.
234 If it is `t', Gnus will not do anything special the first time it is
235 started; it'll just use the normal newsgroups subscription methods.")
236
237 (defvar gnus-use-cross-reference t
238   "*Non-nil means that cross referenced articles will be marked as read.
239 If nil, ignore cross references.  If t, mark articles as read in
240 subscribed newsgroups.  If neither t nor nil, mark as read in all
241 newsgroups.")
242
243 (defvar gnus-single-article-buffer t
244   "*If non-nil, display all articles in the same buffer.
245 If nil, each group will get its own article buffer.")
246
247 (defvar gnus-use-dribble-file t
248   "*Non-nil means that Gnus will use a dribble file to store user updates.
249 If Emacs should crash without saving the .newsrc files, complete
250 information can be restored from the dribble file.")
251
252 (defvar gnus-dribble-directory nil
253   "*The directory where dribble files will be saved.
254 If this variable is nil, the directory where the .newsrc files are
255 saved will be used.")
256
257 (defvar gnus-asynchronous nil
258   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
259
260 (defvar gnus-kill-summary-on-exit t
261   "*If non-nil, kill the summary buffer when you exit from it.
262 If nil, the summary will become a \"*Dead Summary*\" buffer, and
263 it will be killed sometime later.")
264
265 (defvar gnus-large-newsgroup 200
266   "*The number of articles which indicates a large newsgroup.
267 If the number of articles in a newsgroup is greater than this value,
268 confirmation is required for selecting the newsgroup.")
269
270 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
271 (defvar gnus-no-groups-message "No news is horrible news"
272   "*Message displayed by Gnus when no groups are available.")
273
274 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
275   "*Non-nil means that the default name of a file to save articles in is the group name.
276 If it's nil, the directory form of the group name is used instead.
277
278 If this variable is a list, and the list contains the element
279 `not-score', long file names will not be used for score files; if it
280 contains the element `not-save', long file names will not be used for
281 saving; and if it contains the element `not-kill', long file names
282 will not be used for kill files.")
283
284 (defvar gnus-article-save-directory gnus-directory
285   "*Name of the directory articles will be saved in (default \"~/News\").")
286
287 (defvar gnus-kill-files-directory gnus-directory
288   "*Name of the directory where kill files will be stored (default \"~/News\").")
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 'passive
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-exit t
611   "*If non-nil, require your confirmation when exiting Gnus.")
612
613 (defvar gnus-kill-killed t
614   "*If non-nil, Gnus will apply kill files to already killed articles.
615 If it is nil, Gnus will never apply kill files to articles that have
616 already been through the scoring process, which might very well save lots
617 of time.")
618
619 (defvar gnus-extract-address-components 'gnus-extract-address-components
620   "*Function for extracting address components from a From header.
621 Two pre-defined function exist: `gnus-extract-address-components',
622 which is the default, quite fast, and too simplistic solution, and
623 `mail-extract-address-components', which works much better, but is
624 slower.")
625
626 (defvar gnus-summary-default-score 0
627   "*Default article score level.
628 If this variable is nil, scoring will be disabled.")
629
630 (defvar gnus-summary-zcore-fuzz 0
631   "*Fuzziness factor for the zcore in the summary buffer.
632 Articles with scores closer than this to `gnus-summary-default-score'
633 will not be marked.")
634
635 (defvar gnus-simplify-subject-fuzzy-regexp nil
636   "*Strings to be removed when doing fuzzy matches.
637 This can either be a regular expression or list of regular expressions
638 that will be removed from subject strings if fuzzy subject
639 simplification is selected.")
640
641 (defvar gnus-permanently-visible-groups nil
642   "*Regexp to match groups that should always be listed in the group buffer.
643 This means that they will still be listed when there are no unread
644 articles in the groups.")
645
646 (defvar gnus-list-groups-with-ticked-articles t
647   "*If non-nil, list groups that have only ticked articles.
648 If nil, only list groups that have unread articles.")
649
650 (defvar gnus-group-default-list-level gnus-level-subscribed
651   "*Default listing level.
652 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
653
654 (defvar gnus-group-use-permanent-levels nil
655   "*If non-nil, once you set a level, Gnus will use this level.")
656
657 (defvar gnus-group-list-inactive-groups t
658   "*If non-nil, inactive groups will be listed.")
659
660 (defvar gnus-show-mime nil
661   "*If non-nil, do mime processing of articles.
662 The articles will simply be fed to the function given by
663 `gnus-show-mime-method'.")
664
665 (defvar gnus-strict-mime t
666   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
667
668 (defvar gnus-show-mime-method 'metamail-buffer
669   "*Function to process a MIME message.
670 The function is called from the article buffer.")
671
672 (defvar gnus-decode-encoded-word-method (lambda ())
673   "*Function to decode a MIME encoded-words.
674 The function is called from the article buffer.")
675
676 (defvar gnus-show-threads t
677   "*If non-nil, display threads in summary mode.")
678
679 (defvar gnus-thread-hide-subtree nil
680   "*If non-nil, hide all threads initially.
681 If threads are hidden, you have to run the command
682 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
683 to expose hidden threads.")
684
685 (defvar gnus-thread-hide-killed t
686   "*If non-nil, hide killed threads automatically.")
687
688 (defvar gnus-thread-ignore-subject nil
689   "*If non-nil, ignore subjects and do all threading based on the Reference header.
690 If nil, which is the default, articles that have different subjects
691 from their parents will start separate threads.")
692
693 (defvar gnus-thread-operation-ignore-subject t
694   "*If non-nil, subjects will be ignored when doing thread commands.
695 This affects commands like `gnus-summary-kill-thread' and
696 `gnus-summary-lower-thread'.
697
698 If this variable is nil, articles in the same thread with different
699 subjects will not be included in the operation in question.  If this
700 variable is `fuzzy', only articles that have subjects that are fuzzily
701 equal will be included.")
702
703 (defvar gnus-thread-indent-level 4
704   "*Number that says how much each sub-thread should be indented.")
705
706 (defvar gnus-ignored-newsgroups
707   (purecopy (mapconcat 'identity
708                        '("^to\\."       ; not "real" groups
709                          "^[0-9. \t]+ " ; all digits in name
710                          "[][\"#'()]"   ; bogus characters
711                          )
712                        "\\|"))
713   "*A regexp to match uninteresting newsgroups in the active file.
714 Any lines in the active file matching this regular expression are
715 removed from the newsgroup list before anything else is done to it,
716 thus making them effectively non-existent.")
717
718 (defvar gnus-ignored-headers
719   "^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:"
720   "*All headers that match this regexp will be hidden.
721 This variable can also be a list of regexps of headers to be ignored.
722 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
723
724 (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-"
725   "*All headers that do not match this regexp will be hidden.
726 This variable can also be a list of regexp of headers to remain visible.
727 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
728
729 (defvar gnus-sorted-header-list
730   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
731     "^Cc:" "^Date:" "^Organization:")
732   "*This variable is a list of regular expressions.
733 If it is non-nil, headers that match the regular expressions will
734 be placed first in the article buffer in the sequence specified by
735 this list.")
736
737 (defvar gnus-boring-article-headers
738   '(empty followup-to reply-to)
739   "*Headers that are only to be displayed if they have interesting data.
740 Possible values in this list are `empty', `newsgroups', `followup-to',
741 `reply-to', and `date'.")
742
743 (defvar gnus-show-all-headers nil
744   "*If non-nil, don't hide any headers.")
745
746 (defvar gnus-save-all-headers t
747   "*If non-nil, don't remove any headers before saving.")
748
749 (defvar gnus-saved-headers gnus-visible-headers
750   "*Headers to keep if `gnus-save-all-headers' is nil.
751 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
752 If that variable is nil, however, all headers that match this regexp
753 will be kept while the rest will be deleted before saving.")
754
755 (defvar gnus-inhibit-startup-message nil
756   "*If non-nil, the startup message will not be displayed.")
757
758 (defvar gnus-signature-separator "^-- *$"
759   "Regexp matching signature separator.")
760
761 (defvar gnus-signature-limit nil
762   "Provide a limit to what is considered a signature.
763 If it is a number, no signature may not be longer (in characters) than
764 that number.  If it is a function, the function will be called without
765 any parameters, and if it returns nil, there is no signature in the
766 buffer.  If it is a string, it will be used as a regexp.  If it
767 matches, the text in question is not a signature.")
768
769 (defvar gnus-auto-extend-newsgroup t
770   "*If non-nil, extend newsgroup forward and backward when requested.")
771
772 (defvar gnus-auto-select-first t
773   "*If nil, don't select the first unread article when entering a group.
774 If this variable is `best', select the highest-scored unread article
775 in the group.  If neither nil nor `best', select the first unread
776 article.
777
778 If you want to prevent automatic selection of the first unread article
779 in some newsgroups, set the variable to nil in
780 `gnus-select-group-hook'.")
781
782 (defvar gnus-auto-select-next t
783   "*If non-nil, offer to go to the next group from the end of the previous.
784 If the value is t and the next newsgroup is empty, Gnus will exit
785 summary mode and go back to group mode.  If the value is neither nil
786 nor t, Gnus will select the following unread newsgroup.  In
787 particular, if the value is the symbol `quietly', the next unread
788 newsgroup will be selected without any confirmation, and if it is
789 `almost-quietly', the next group will be selected without any
790 confirmation if you are located on the last article in the group.
791 Finally, if this variable is `slightly-quietly', the `Z n' command
792 will go to the next group without confirmation.")
793
794 (defvar gnus-auto-select-same nil
795   "*If non-nil, select the next article with the same subject.")
796
797 (defvar gnus-summary-check-current nil
798   "*If non-nil, consider the current article when moving.
799 The \"unread\" movement commands will stay on the same line if the
800 current article is unread.")
801
802 (defvar gnus-auto-center-summary t
803   "*If non-nil, always center the current summary buffer.
804 In particular, if `vertical' do only vertical recentering.  If non-nil
805 and non-`vertical', do both horizontal and vertical recentering.")
806
807 (defvar gnus-break-pages t
808   "*If non-nil, do page breaking on articles.
809 The page delimiter is specified by the `gnus-page-delimiter'
810 variable.")
811
812 (defvar gnus-page-delimiter "^\^L"
813   "*Regexp describing what to use as article page delimiters.
814 The default value is \"^\^L\", which is a form linefeed at the
815 beginning of a line.")
816
817 (defvar gnus-use-full-window t
818   "*If non-nil, use the entire Emacs screen.")
819
820 (defvar gnus-window-configuration nil
821   "Obsolete variable.  See `gnus-buffer-configuration'.")
822
823 (defvar gnus-window-min-width 2
824   "*Minimum width of Gnus buffers.")
825
826 (defvar gnus-window-min-height 1
827   "*Minimum height of Gnus buffers.")
828
829 (defvar gnus-buffer-configuration
830   '((group
831      (vertical 1.0
832                (group 1.0 point)
833                (if gnus-carpal '(group-carpal 4))))
834     (summary
835      (vertical 1.0
836                (summary 1.0 point)
837                (if gnus-carpal '(summary-carpal 4))))
838     (article
839      (cond 
840       (gnus-use-picons
841        '(frame 1.0
842                (vertical 1.0
843                          (summary 0.25 point)
844                          (if gnus-carpal '(summary-carpal 4))
845                          (article 1.0))
846                (vertical ((height . 5) (width . 15)
847                           (user-position . t)
848                           (left . -1) (top . 1))
849                          (picons 1.0))))
850       (gnus-use-trees
851        '(vertical 1.0
852                   (summary 0.25 point)
853                   (tree 0.25)
854                   (article 1.0)))
855       (t
856        '(vertical 1.0
857                  (summary 0.25 point)
858                  (if gnus-carpal '(summary-carpal 4))
859                  (if gnus-use-trees '(tree 0.25))
860                  (article 1.0)))))
861     (server
862      (vertical 1.0
863                (server 1.0 point)
864                (if gnus-carpal '(server-carpal 2))))
865     (browse
866      (vertical 1.0
867                (browse 1.0 point)
868                (if gnus-carpal '(browse-carpal 2))))
869     (message
870      (vertical 1.0
871                (message 1.0 point)))
872     (pick
873      (vertical 1.0
874                (article 1.0 point)))
875     (info
876      (vertical 1.0
877                (info 1.0 point)))
878     (summary-faq
879      (vertical 1.0
880                (summary 0.25)
881                (faq 1.0 point)))
882     (edit-group
883      (vertical 1.0
884                (group 0.5)
885                (edit-group 1.0 point)))
886     (edit-server
887      (vertical 1.0
888                (server 0.5)
889                (edit-server 1.0 point)))
890     (edit-score
891      (vertical 1.0
892                (summary 0.25)
893                (edit-score 1.0 point)))
894     (post
895      (vertical 1.0
896                (post 1.0 point)))
897     (reply
898      (vertical 1.0
899                (article-copy 0.5)
900                (message 1.0 point)))
901     (forward
902      (vertical 1.0
903                (message 1.0 point)))
904     (reply-yank
905      (vertical 1.0
906                (message 1.0 point)))
907     (mail-bounce
908      (vertical 1.0
909                (article 0.5)
910                (message 1.0 point)))
911     (draft
912      (vertical 1.0
913                (draft 1.0 point)))
914     (pipe
915      (vertical 1.0
916                (summary 0.25 point)
917                (if gnus-carpal '(summary-carpal 4))
918                ("*Shell Command Output*" 1.0)))
919     (bug
920      (vertical 1.0
921                ("*Gnus Help Bug*" 0.5)
922                ("*Gnus Bug*" 1.0 point)))
923     (compose-bounce
924      (vertical 1.0
925                (article 0.5)
926                (message 1.0 point))))
927   "Window configuration for all possible Gnus buffers.
928 This variable is a list of lists.  Each of these lists has a NAME and
929 a RULE.  The NAMEs are commonsense names like `group', which names a
930 rule used when displaying the group buffer; `summary', which names a
931 rule for what happens when you enter a group and do not display an
932 article buffer; and so on.  See the value of this variable for a
933 complete list of NAMEs.
934
935 Each RULE is a list of vectors.  The first element in this vector is
936 the name of the buffer to be displayed; the second element is the
937 percentage of the screen this buffer is to occupy (a number in the
938 0.0-0.99 range); the optional third element is `point', which should
939 be present to denote which buffer point is to go to after making this
940 buffer configuration.")
941
942 (defvar gnus-window-to-buffer
943   '((group . gnus-group-buffer)
944     (summary . gnus-summary-buffer)
945     (article . gnus-article-buffer)
946     (server . gnus-server-buffer)
947     (browse . "*Gnus Browse Server*")
948     (edit-group . gnus-group-edit-buffer)
949     (edit-server . gnus-server-edit-buffer)
950     (group-carpal . gnus-carpal-group-buffer)
951     (summary-carpal . gnus-carpal-summary-buffer)
952     (server-carpal . gnus-carpal-server-buffer)
953     (browse-carpal . gnus-carpal-browse-buffer)
954     (edit-score . gnus-score-edit-buffer)
955     (message . gnus-message-buffer)
956     (mail . gnus-message-buffer)
957     (post-news . gnus-message-buffer)
958     (faq . gnus-faq-buffer)
959     (picons . "*Picons*")
960     (tree . gnus-tree-buffer)
961     (info . gnus-info-buffer)
962     (article-copy . gnus-article-copy)
963     (draft . gnus-draft-buffer))
964   "Mapping from short symbols to buffer names or buffer variables.")
965
966 (defvar gnus-carpal nil
967   "*If non-nil, display clickable icons.")
968
969 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
970   "*Function called with a group name when new group is detected.
971 A few pre-made functions are supplied: `gnus-subscribe-randomly'
972 inserts new groups at the beginning of the list of groups;
973 `gnus-subscribe-alphabetically' inserts new groups in strict
974 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
975 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
976 for your decision; `gnus-subscribe-killed' kills all new groups;
977 `gnus-subscribe-zombies' will make all new groups into zombies.")
978
979 ;; Suggested by a bug report by Hallvard B Furuseth.
980 ;; <h.b.furuseth@usit.uio.no>.
981 (defvar gnus-subscribe-options-newsgroup-method
982   (function gnus-subscribe-alphabetically)
983   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
984 If, for instance, you want to subscribe to all newsgroups in the
985 \"no\" and \"alt\" hierarchies, you'd put the following in your
986 .newsrc file:
987
988 options -n no.all alt.all
989
990 Gnus will the subscribe all new newsgroups in these hierarchies with
991 the subscription method in this variable.")
992
993 (defvar gnus-subscribe-hierarchical-interactive nil
994   "*If non-nil, Gnus will offer to subscribe hierarchically.
995 When a new hierarchy appears, Gnus will ask the user:
996
997 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
998
999 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1000 subscribe to all newsgroups in the hierarchy and `s' will skip this
1001 hierarchy in its entirety.")
1002
1003 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1004   "*Function used for sorting the group buffer.
1005 This function will be called with group info entries as the arguments
1006 for the groups to be sorted.  Pre-made functions include
1007 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1008 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1009 `gnus-group-sort-by-rank'.
1010
1011 This variable can also be a list of sorting functions.  In that case,
1012 the most significant sort function should be the last function in the
1013 list.")
1014
1015 ;; Mark variables suggested by Thomas Michanek
1016 ;; <Thomas.Michanek@telelogic.se>.
1017 (defvar gnus-unread-mark ? 
1018   "*Mark used for unread articles.")
1019 (defvar gnus-ticked-mark ?!
1020   "*Mark used for ticked articles.")
1021 (defvar gnus-dormant-mark ??
1022   "*Mark used for dormant articles.")
1023 (defvar gnus-del-mark ?r
1024   "*Mark used for del'd articles.")
1025 (defvar gnus-read-mark ?R
1026   "*Mark used for read articles.")
1027 (defvar gnus-expirable-mark ?E
1028   "*Mark used for expirable articles.")
1029 (defvar gnus-killed-mark ?K
1030   "*Mark used for killed articles.")
1031 (defvar gnus-souped-mark ?F
1032   "*Mark used for killed articles.")
1033 (defvar gnus-kill-file-mark ?X
1034   "*Mark used for articles killed by kill files.")
1035 (defvar gnus-low-score-mark ?Y
1036   "*Mark used for articles with a low score.")
1037 (defvar gnus-catchup-mark ?C
1038   "*Mark used for articles that are caught up.")
1039 (defvar gnus-replied-mark ?A
1040   "*Mark used for articles that have been replied to.")
1041 (defvar gnus-cached-mark ?*
1042   "*Mark used for articles that are in the cache.")
1043 (defvar gnus-saved-mark ?S
1044   "*Mark used for articles that have been saved to.")
1045 (defvar gnus-process-mark ?#
1046   "*Process mark.")
1047 (defvar gnus-ancient-mark ?O
1048   "*Mark used for ancient articles.")
1049 (defvar gnus-sparse-mark ?Q
1050   "*Mark used for sparsely reffed articles.")
1051 (defvar gnus-canceled-mark ?G
1052   "*Mark used for canceled articles.")
1053 (defvar gnus-score-over-mark ?+
1054   "*Score mark used for articles with high scores.")
1055 (defvar gnus-score-below-mark ?-
1056   "*Score mark used for articles with low scores.")
1057 (defvar gnus-empty-thread-mark ? 
1058   "*There is no thread under the article.")
1059 (defvar gnus-not-empty-thread-mark ?=
1060   "*There is a thread under the article.")
1061
1062 (defvar gnus-view-pseudo-asynchronously nil
1063   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1064
1065 (defvar gnus-view-pseudos nil
1066   "*If `automatic', pseudo-articles will be viewed automatically.
1067 If `not-confirm', pseudos will be viewed automatically, and the user
1068 will not be asked to confirm the command.")
1069
1070 (defvar gnus-view-pseudos-separately t
1071   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1072 If nil, all files that use the same viewing command will be given as a
1073 list of parameters to that command.")
1074
1075 (defvar gnus-insert-pseudo-articles t
1076   "*If non-nil, insert pseudo-articles when decoding articles.")
1077
1078 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1079   "*Format of group lines.
1080 It works along the same lines as a normal formatting string,
1081 with some simple extensions.
1082
1083 %M    Only marked articles (character, \"*\" or \" \")
1084 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1085 %L    Level of subscribedness (integer)
1086 %N    Number of unread articles (integer)
1087 %I    Number of dormant articles (integer)
1088 %i    Number of ticked and dormant (integer)
1089 %T    Number of ticked articles (integer)
1090 %R    Number of read articles (integer)
1091 %t    Total number of articles (integer)
1092 %y    Number of unread, unticked articles (integer)
1093 %G    Group name (string)
1094 %g    Qualified group name (string)
1095 %D    Group description (string)
1096 %s    Select method (string)
1097 %o    Moderated group (char, \"m\")
1098 %p    Process mark (char)
1099 %O    Moderated group (string, \"(m)\" or \"\")
1100 %P    Topic indentation (string)
1101 %l    Whether there are GroupLens predictions for this group (string)
1102 %n    Select from where (string)
1103 %z    A string that look like `<%s:%n>' if a foreign select method is used
1104 %u    User defined specifier.  The next character in the format string should
1105       be a letter.  Gnus will call the function gnus-user-format-function-X,
1106       where X is the letter following %u.  The function will be passed the
1107       current header as argument.  The function should return a string, which
1108       will be inserted into the buffer just like information from any other
1109       group specifier.
1110
1111 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1112 the mouse point move inside the area.  There can only be one such area.
1113
1114 Note that this format specification is not always respected.  For
1115 reasons of efficiency, when listing killed groups, this specification
1116 is ignored altogether.  If the spec is changed considerably, your
1117 output may end up looking strange when listing both alive and killed
1118 groups.
1119
1120 If you use %o or %O, reading the active file will be slower and quite
1121 a bit of extra memory will be used. %D will also worsen performance.
1122 Also note that if you change the format specification to include any
1123 of these specs, you must probably re-start Gnus to see them go into
1124 effect.")
1125
1126 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1127   "*The format specification of the lines in the summary buffer.
1128
1129 It works along the same lines as a normal formatting string,
1130 with some simple extensions.
1131
1132 %N   Article number, left padded with spaces (string)
1133 %S   Subject (string)
1134 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1135 %n   Name of the poster (string)
1136 %a   Extracted name of the poster (string)
1137 %A   Extracted address of the poster (string)
1138 %F   Contents of the From: header (string)
1139 %x   Contents of the Xref: header (string)
1140 %D   Date of the article (string)
1141 %d   Date of the article (string) in DD-MMM format
1142 %M   Message-id of the article (string)
1143 %r   References of the article (string)
1144 %c   Number of characters in the article (integer)
1145 %L   Number of lines in the article (integer)
1146 %I   Indentation based on thread level (a string of spaces)
1147 %T   A string with two possible values: 80 spaces if the article
1148      is on thread level two or larger and 0 spaces on level one
1149 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1150 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1151 %[   Opening bracket (character, \"[\" or \"<\")
1152 %]   Closing bracket (character, \"]\" or \">\")
1153 %>   Spaces of length thread-level (string)
1154 %<   Spaces of length (- 20 thread-level) (string)
1155 %i   Article score (number)
1156 %z   Article zcore (character)
1157 %t   Number of articles under the current thread (number).
1158 %e   Whether the thread is empty or not (character).
1159 %l   GroupLens score (string).
1160 %u   User defined specifier.  The next character in the format string should
1161      be a letter.  Gnus will call the function gnus-user-format-function-X,
1162      where X is the letter following %u.  The function will be passed the
1163      current header as argument.  The function should return a string, which
1164      will be inserted into the summary just like information from any other
1165      summary specifier.
1166
1167 Text between %( and %) will be highlighted with `gnus-mouse-face'
1168 when the mouse point is placed inside the area.  There can only be one
1169 such area.
1170
1171 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1172 with care.  For reasons of efficiency, Gnus will compute what column
1173 these characters will end up in, and \"hard-code\" that.  This means that
1174 it is illegal to have these specs after a variable-length spec.  Well,
1175 you might not be arrested, but your summary buffer will look strange,
1176 which is bad enough.
1177
1178 The smart choice is to have these specs as for to the left as
1179 possible.
1180
1181 This restriction may disappear in later versions of Gnus.")
1182
1183 (defvar gnus-summary-dummy-line-format
1184   "*  %(:                          :%) %S\n"
1185   "*The format specification for the dummy roots in the summary buffer.
1186 It works along the same lines as a normal formatting string,
1187 with some simple extensions.
1188
1189 %S  The subject")
1190
1191 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1192   "*The format specification for the summary mode line.
1193 It works along the same lines as a normal formatting string,
1194 with some simple extensions:
1195
1196 %G  Group name
1197 %p  Unprefixed group name
1198 %A  Current article number
1199 %V  Gnus version
1200 %U  Number of unread articles in the group
1201 %e  Number of unselected articles in the group
1202 %Z  A string with unread/unselected article counts
1203 %g  Shortish group name
1204 %S  Subject of the current article
1205 %u  User-defined spec
1206 %s  Current score file name
1207 %d  Number of dormant articles
1208 %r  Number of articles that have been marked as read in this session
1209 %E  Number of articles expunged by the score files")
1210
1211 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1212   "*The format specification for the article mode line.
1213 See `gnus-summary-mode-line-format' for a closer description.")
1214
1215 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1216   "*The format specification for the group mode line.
1217 It works along the same lines as a normal formatting string,
1218 with some simple extensions:
1219
1220 %S   The native news server.
1221 %M   The native select method.
1222 %:   \":\" if %S isn't \"\".")
1223
1224 (defvar gnus-valid-select-methods
1225   '(("nntp" post address prompt-address)
1226     ("nnspool" post address)
1227     ("nnvirtual" post-mail virtual prompt-address)
1228     ("nnmbox" mail respool address)
1229     ("nnml" mail respool address)
1230     ("nnmh" mail respool address)
1231     ("nndir" post-mail prompt-address address)
1232     ("nneething" none address prompt-address)
1233     ("nndoc" none address prompt-address)
1234     ("nnbabyl" mail address respool)
1235     ("nnkiboze" post address virtual)
1236     ("nnsoup" post-mail address)
1237     ("nndraft" post-mail)
1238     ("nnfolder" mail respool address))
1239   "An alist of valid select methods.
1240 The first element of each list lists should be a string with the name
1241 of the select method.  The other elements may be be the category of
1242 this method (ie. `post', `mail', `none' or whatever) or other
1243 properties that this method has (like being respoolable).
1244 If you implement a new select method, all you should have to change is
1245 this variable.  I think.")
1246
1247 (defvar gnus-updated-mode-lines '(group article summary tree)
1248   "*List of buffers that should update their mode lines.
1249 The list may contain the symbols `group', `article' and `summary'.  If
1250 the corresponding symbol is present, Gnus will keep that mode line
1251 updated with information that may be pertinent.
1252 If this variable is nil, screen refresh may be quicker.")
1253
1254 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1255 (defvar gnus-mode-non-string-length nil
1256   "*Max length of mode-line non-string contents.
1257 If this is nil, Gnus will take space as is needed, leaving the rest
1258 of the modeline intact.")
1259
1260 ;see gnus-cus.el
1261 ;(defvar gnus-mouse-face 'highlight
1262 ;  "*Face used for mouse highlighting in Gnus.
1263 ;No mouse highlights will be done if `gnus-visual' is nil.")
1264
1265 (defvar gnus-summary-mark-below 0
1266   "*Mark all articles with a score below this variable as read.
1267 This variable is local to each summary buffer and usually set by the
1268 score file.")
1269
1270 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1271   "*List of functions used for sorting articles in the summary buffer.
1272 This variable is only used when not using a threaded display.")
1273
1274 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1275   "*List of functions used for sorting threads in the summary buffer.
1276 By default, threads are sorted by article number.
1277
1278 Each function takes two threads and return non-nil if the first thread
1279 should be sorted before the other.  If you use more than one function,
1280 the primary sort function should be the last.  You should probably
1281 always include `gnus-thread-sort-by-number' in the list of sorting
1282 functions -- preferably first.
1283
1284 Ready-mady functions include `gnus-thread-sort-by-number',
1285 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1286 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1287 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1288
1289 (defvar gnus-thread-score-function '+
1290   "*Function used for calculating the total score of a thread.
1291
1292 The function is called with the scores of the article and each
1293 subthread and should then return the score of the thread.
1294
1295 Some functions you can use are `+', `max', or `min'.")
1296
1297 (defvar gnus-summary-expunge-below nil
1298   "All articles that have a score less than this variable will be expunged.")
1299
1300 (defvar gnus-thread-expunge-below nil
1301   "All threads that have a total score less than this variable will be expunged.
1302 See `gnus-thread-score-function' for en explanation of what a
1303 \"thread score\" is.")
1304
1305 (defvar gnus-auto-subscribed-groups
1306   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1307   "*All new groups that match this regexp will be subscribed automatically.
1308 Note that this variable only deals with new groups.  It has no effect
1309 whatsoever on old groups.")
1310
1311 (defvar gnus-options-subscribe nil
1312   "*All new groups matching this regexp will be subscribed unconditionally.
1313 Note that this variable deals only with new newsgroups.  This variable
1314 does not affect old newsgroups.")
1315
1316 (defvar gnus-options-not-subscribe nil
1317   "*All new groups matching this regexp will be ignored.
1318 Note that this variable deals only with new newsgroups.  This variable
1319 does not affect old (already subscribed) newsgroups.")
1320
1321 (defvar gnus-auto-expirable-newsgroups nil
1322   "*Groups in which to automatically mark read articles as expirable.
1323 If non-nil, this should be a regexp that should match all groups in
1324 which to perform auto-expiry.  This only makes sense for mail groups.")
1325
1326 (defvar gnus-total-expirable-newsgroups nil
1327   "*Groups in which to perform expiry of all read articles.
1328 Use with extreme caution.  All groups that match this regexp will be
1329 expiring - which means that all read articles will be deleted after
1330 (say) one week.  (This only goes for mail groups and the like, of
1331 course.)")
1332
1333 (defvar gnus-group-uncollapsed-levels 1
1334   "Number of group name elements to leave alone when making a short group name.")
1335
1336 (defvar gnus-hidden-properties '(invisible t intangible t)
1337   "Property list to use for hiding text.")
1338
1339 (defvar gnus-modtime-botch nil
1340   "*Non-nil means .newsrc should be deleted prior to save.  
1341 Its use is due to the bogus appearance that .newsrc was modified on
1342 disc.")
1343
1344 ;; Hooks.
1345
1346 (defvar gnus-group-mode-hook nil
1347   "*A hook for Gnus group mode.")
1348
1349 (defvar gnus-summary-mode-hook nil
1350   "*A hook for Gnus summary mode.
1351 This hook is run before any variables are set in the summary buffer.")
1352
1353 (defvar gnus-article-mode-hook nil
1354   "*A hook for Gnus article mode.")
1355
1356 (defvar gnus-summary-prepare-exit-hook nil
1357   "*A hook called when preparing to exit from the summary buffer.
1358 It calls `gnus-summary-expire-articles' by default.")
1359 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1360
1361 (defvar gnus-summary-exit-hook nil
1362   "*A hook called on exit from the summary buffer.")
1363
1364 (defvar gnus-group-catchup-group-hook nil
1365   "*A hook run when catching up a group from the group buffer.")
1366
1367 (defvar gnus-group-update-group-hook nil
1368   "*A hook called when updating group lines.")
1369
1370 (defvar gnus-open-server-hook nil
1371   "*A hook called just before opening connection to the news server.")
1372
1373 (defvar gnus-load-hook nil
1374   "*A hook run while Gnus is loaded.")
1375
1376 (defvar gnus-startup-hook nil
1377   "*A hook called at startup.
1378 This hook is called after Gnus is connected to the NNTP server.")
1379
1380 (defvar gnus-get-new-news-hook nil
1381   "*A hook run just before Gnus checks for new news.")
1382
1383 (defvar gnus-after-getting-new-news-hook nil
1384   "*A hook run after Gnus checks for new news.")
1385
1386 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1387   "*A function that is called to generate the group buffer.
1388 The function is called with three arguments: The first is a number;
1389 all group with a level less or equal to that number should be listed,
1390 if the second is non-nil, empty groups should also be displayed.  If
1391 the third is non-nil, it is a number.  No groups with a level lower
1392 than this number should be displayed.
1393
1394 The only current function implemented is `gnus-group-prepare-flat'.")
1395
1396 (defvar gnus-group-prepare-hook nil
1397   "*A hook called after the group buffer has been generated.
1398 If you want to modify the group buffer, you can use this hook.")
1399
1400 (defvar gnus-summary-prepare-hook nil
1401   "*A hook called after the summary buffer has been generated.
1402 If you want to modify the summary buffer, you can use this hook.")
1403
1404 (defvar gnus-summary-generate-hook nil
1405   "*A hook run just before generating the summary buffer.
1406 This hook is commonly used to customize threading variables and the
1407 like.")
1408
1409 (defvar gnus-article-prepare-hook nil
1410   "*A hook called after an article has been prepared in the article buffer.
1411 If you want to run a special decoding program like nkf, use this hook.")
1412
1413 ;(defvar gnus-article-display-hook nil
1414 ;  "*A hook called after the article is displayed in the article buffer.
1415 ;The hook is designed to change the contents of the article
1416 ;buffer.  Typical functions that this hook may contain are
1417 ;`gnus-article-hide-headers' (hide selected headers),
1418 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1419 ;`gnus-article-hide-signature' (hide signature) and
1420 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1421 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1422 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1423 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1424
1425 (defvar gnus-article-x-face-command
1426   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1427   "String or function to be executed to display an X-Face header.
1428 If it is a string, the command will be executed in a sub-shell
1429 asynchronously.  The compressed face will be piped to this command.")
1430
1431 (defvar gnus-article-x-face-too-ugly nil
1432   "Regexp matching posters whose face shouldn't be shown automatically.")
1433
1434 (defvar gnus-select-group-hook nil
1435   "*A hook called when a newsgroup is selected.
1436
1437 If you'd like to simplify subjects like the
1438 `gnus-summary-next-same-subject' command does, you can use the
1439 following hook:
1440
1441  (setq gnus-select-group-hook
1442       (list
1443         (lambda ()
1444           (mapcar (lambda (header)
1445                      (mail-header-set-subject
1446                       header
1447                       (gnus-simplify-subject
1448                        (mail-header-subject header) 're-only)))
1449                   gnus-newsgroup-headers))))")
1450
1451 (defvar gnus-select-article-hook nil
1452   "*A hook called when an article is selected.")
1453
1454 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1455   "*A hook called to apply kill files to a group.
1456 This hook is intended to apply a kill file to the selected newsgroup.
1457 The function `gnus-apply-kill-file' is called by default.
1458
1459 Since a general kill file is too heavy to use only for a few
1460 newsgroups, I recommend you to use a lighter hook function.  For
1461 example, if you'd like to apply a kill file to articles which contains
1462 a string `rmgroup' in subject in newsgroup `control', you can use the
1463 following hook:
1464
1465  (setq gnus-apply-kill-hook
1466       (list
1467         (lambda ()
1468           (cond ((string-match \"control\" gnus-newsgroup-name)
1469                  (gnus-kill \"Subject\" \"rmgroup\")
1470                  (gnus-expunge \"X\"))))))")
1471
1472 (defvar gnus-visual-mark-article-hook
1473   (list 'gnus-highlight-selected-summary)
1474   "*Hook run after selecting an article in the summary buffer.
1475 It is meant to be used for highlighting the article in some way.  It
1476 is not run if `gnus-visual' is nil.")
1477
1478 (defvar gnus-parse-headers-hook nil
1479   "*A hook called before parsing the headers.")
1480 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1481
1482 (defvar gnus-exit-group-hook nil
1483   "*A hook called when exiting (not quitting) summary mode.")
1484
1485 (defvar gnus-suspend-gnus-hook nil
1486   "*A hook called when suspending (not exiting) Gnus.")
1487
1488 (defvar gnus-exit-gnus-hook nil
1489   "*A hook called when exiting Gnus.")
1490
1491 (defvar gnus-after-exiting-gnus-hook nil
1492   "*A hook called after exiting Gnus.")
1493
1494 (defvar gnus-save-newsrc-hook nil
1495   "*A hook called before saving any of the newsrc files.")
1496
1497 (defvar gnus-save-quick-newsrc-hook nil
1498   "*A hook called just before saving the quick newsrc file.
1499 Can be used to turn version control on or off.")
1500
1501 (defvar gnus-save-standard-newsrc-hook nil
1502   "*A hook called just before saving the standard newsrc file.
1503 Can be used to turn version control on or off.")
1504
1505 (defvar gnus-summary-update-hook
1506   (list 'gnus-summary-highlight-line)
1507   "*A hook called when a summary line is changed.
1508 The hook will not be called if `gnus-visual' is nil.
1509
1510 The default function `gnus-summary-highlight-line' will
1511 highlight the line according to the `gnus-summary-highlight'
1512 variable.")
1513
1514 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1515   "*A hook called when a group line is changed.
1516 The hook will not be called if `gnus-visual' is nil.
1517
1518 The default function `gnus-group-highlight-line' will
1519 highlight the line according to the `gnus-group-highlight'
1520 variable.")
1521
1522 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1523   "*A hook called when an article is selected for the first time.
1524 The hook is intended to mark an article as read (or unread)
1525 automatically when it is selected.")
1526
1527 (defvar gnus-group-change-level-function nil
1528   "Function run when a group level is changed.
1529 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1530
1531 ;; Remove any hilit infestation.
1532 (add-hook 'gnus-startup-hook
1533           (lambda ()
1534             (remove-hook 'gnus-summary-prepare-hook
1535                          'hilit-rehighlight-buffer-quietly)
1536             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1537             (setq gnus-mark-article-hook
1538                   '(gnus-summary-mark-read-and-unread-as-read))
1539             (remove-hook 'gnus-article-prepare-hook
1540                          'hilit-rehighlight-buffer-quietly)))
1541
1542 \f
1543 ;; Internal variables
1544
1545 (defvar gnus-tree-buffer "*Tree*"
1546   "Buffer where Gnus thread trees are displayed.")
1547
1548 ;; Dummy variable.
1549 (defvar gnus-use-generic-from nil)
1550
1551 (defvar gnus-thread-indent-array nil)
1552 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1553
1554 (defvar gnus-newsrc-file-version nil)
1555
1556 (defvar gnus-method-history nil)
1557 ;; Variable holding the user answers to all method prompts.
1558
1559 (defvar gnus-group-history nil)
1560 ;; Variable holding the user answers to all group prompts.
1561
1562 (defvar gnus-server-alist nil
1563   "List of available servers.")
1564
1565 (defvar gnus-group-indentation-function nil)
1566
1567 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1568
1569 (defvar gnus-goto-missing-group-function nil)
1570
1571 (defvar gnus-override-subscribe-method nil)
1572
1573 (defvar gnus-group-goto-next-group-function nil
1574   "Function to override finding the next group after listing groups.")
1575
1576 (defconst gnus-article-mark-lists
1577   '((marked . tick) (replied . reply)
1578     (expirable . expire) (killed . killed)
1579     (bookmarks . bookmark) (dormant . dormant)
1580     (scored . score) (saved . save)
1581     (cached . cache)
1582     ))
1583
1584 ;; Avoid highlighting in kill files.
1585 (defvar gnus-summary-inhibit-highlight nil)
1586 (defvar gnus-newsgroup-selected-overlay nil)
1587
1588 (defvar gnus-inhibit-hiding nil)
1589 (defvar gnus-group-indentation "")
1590 (defvar gnus-inhibit-limiting nil)
1591 (defvar gnus-created-frames nil)
1592
1593 (defvar gnus-article-mode-map nil)
1594 (defvar gnus-dribble-buffer nil)
1595 (defvar gnus-headers-retrieved-by nil)
1596 (defvar gnus-article-reply nil)
1597 (defvar gnus-override-method nil)
1598 (defvar gnus-article-check-size nil)
1599
1600 (defvar gnus-current-score-file nil)
1601 (defvar gnus-newsgroup-adaptive-score-file nil)
1602 (defvar gnus-scores-exclude-files nil)
1603
1604 (defvar gnus-opened-servers nil)
1605
1606 (defvar gnus-current-move-group nil)
1607 (defvar gnus-current-copy-group nil)
1608 (defvar gnus-current-crosspost-group nil)
1609
1610 (defvar gnus-newsgroup-dependencies nil)
1611 (defvar gnus-newsgroup-async nil)
1612 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1613
1614 (defvar gnus-newsgroup-adaptive nil)
1615
1616 (defvar gnus-summary-display-table nil)
1617 (defvar gnus-summary-display-article-function nil)
1618
1619 (defvar gnus-summary-highlight-line-function nil
1620   "Function called after highlighting a summary line.")
1621
1622 (defvar gnus-group-line-format-alist
1623   `((?M gnus-tmp-marked-mark ?c)
1624     (?S gnus-tmp-subscribed ?c)
1625     (?L gnus-tmp-level ?d)
1626     (?N (cond ((eq number t) "*" )
1627               ((numberp number) 
1628                (int-to-string
1629                 (+ number
1630                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1631                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1632               (t number)) ?s)
1633     (?R gnus-tmp-number-of-read ?s)
1634     (?t gnus-tmp-number-total ?d)
1635     (?y gnus-tmp-number-of-unread ?s)
1636     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1637     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1638     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1639            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1640     (?g gnus-tmp-group ?s)
1641     (?G gnus-tmp-qualified-group ?s)
1642     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1643     (?D gnus-tmp-newsgroup-description ?s)
1644     (?o gnus-tmp-moderated ?c)
1645     (?O gnus-tmp-moderated-string ?s)
1646     (?p gnus-tmp-process-marked ?c)
1647     (?s gnus-tmp-news-server ?s)
1648     (?n gnus-tmp-news-method ?s)
1649     (?P gnus-group-indentation ?s)
1650     (?l gnus-tmp-grouplens ?s)
1651     (?z gnus-tmp-news-method-string ?s)
1652     (?u gnus-tmp-user-defined ?s)))
1653
1654 (defvar gnus-summary-line-format-alist
1655   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1656     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1657     (?s gnus-tmp-subject-or-nil ?s)
1658     (?n gnus-tmp-name ?s)
1659     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1660         ?s)
1661     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1662             gnus-tmp-from) ?s)
1663     (?F gnus-tmp-from ?s)
1664     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1665     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1666     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1667     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1668     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1669     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1670     (?L gnus-tmp-lines ?d)
1671     (?I gnus-tmp-indentation ?s)
1672     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1673     (?R gnus-tmp-replied ?c)
1674     (?\[ gnus-tmp-opening-bracket ?c)
1675     (?\] gnus-tmp-closing-bracket ?c)
1676     (?\> (make-string gnus-tmp-level ? ) ?s)
1677     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1678     (?i gnus-tmp-score ?d)
1679     (?z gnus-tmp-score-char ?c)
1680     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1681     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1682     (?U gnus-tmp-unread ?c)
1683     (?t (gnus-summary-number-of-articles-in-thread
1684          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1685         ?d)
1686     (?e (gnus-summary-number-of-articles-in-thread
1687          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1688         ?c)
1689     (?u gnus-tmp-user-defined ?s))
1690   "An alist of format specifications that can appear in summary lines,
1691 and what variables they correspond with, along with the type of the
1692 variable (string, integer, character, etc).")
1693
1694 (defvar gnus-summary-dummy-line-format-alist
1695   `((?S gnus-tmp-subject ?s)
1696     (?N gnus-tmp-number ?d)
1697     (?u gnus-tmp-user-defined ?s)))
1698
1699 (defvar gnus-summary-mode-line-format-alist
1700   `((?G gnus-tmp-group-name ?s)
1701     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1702     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1703     (?A gnus-tmp-article-number ?d)
1704     (?Z gnus-tmp-unread-and-unselected ?s)
1705     (?V gnus-version ?s)
1706     (?U gnus-tmp-unread ?d)
1707     (?S gnus-tmp-subject ?s)
1708     (?e gnus-tmp-unselected ?d)
1709     (?u gnus-tmp-user-defined ?s)
1710     (?d (length gnus-newsgroup-dormant) ?d)
1711     (?t (length gnus-newsgroup-marked) ?d)
1712     (?r (length gnus-newsgroup-reads) ?d)
1713     (?E gnus-newsgroup-expunged-tally ?d)
1714     (?s (gnus-current-score-file-nondirectory) ?s)))
1715
1716 (defvar gnus-article-mode-line-format-alist
1717   gnus-summary-mode-line-format-alist)
1718
1719 (defvar gnus-group-mode-line-format-alist
1720   `((?S gnus-tmp-news-server ?s)
1721     (?M gnus-tmp-news-method ?s)
1722     (?u gnus-tmp-user-defined ?s)
1723     (?: gnus-tmp-colon ?s)))
1724
1725 (defvar gnus-have-read-active-file nil)
1726
1727 (defconst gnus-maintainer
1728   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1729   "The mail address of the Gnus maintainers.")
1730
1731 (defconst gnus-version-number "5.2.12"
1732   "Version number for this version of Gnus.")
1733
1734 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1735   "Version string for this version of Gnus.")
1736
1737 (defvar gnus-info-nodes
1738   '((gnus-group-mode "(gnus)The Group Buffer")
1739     (gnus-summary-mode "(gnus)The Summary Buffer")
1740     (gnus-article-mode "(gnus)The Article Buffer"))
1741   "Alist of major modes and related Info nodes.")
1742
1743 (defvar gnus-group-buffer "*Group*")
1744 (defvar gnus-summary-buffer "*Summary*")
1745 (defvar gnus-article-buffer "*Article*")
1746 (defvar gnus-server-buffer "*Server*")
1747
1748 (defvar gnus-work-buffer " *gnus work*")
1749
1750 (defvar gnus-original-article-buffer " *Original Article*")
1751 (defvar gnus-original-article nil)
1752
1753 (defvar gnus-buffer-list nil
1754   "Gnus buffers that should be killed on exit.")
1755
1756 (defvar gnus-slave nil
1757   "Whether this Gnus is a slave or not.")
1758
1759 (defvar gnus-variable-list
1760   '(gnus-newsrc-options gnus-newsrc-options-n
1761     gnus-newsrc-last-checked-date
1762     gnus-newsrc-alist gnus-server-alist
1763     gnus-killed-list gnus-zombie-list
1764     gnus-topic-topology gnus-topic-alist
1765     gnus-format-specs)
1766   "Gnus variables saved in the quick startup file.")
1767
1768 (defvar gnus-newsrc-options nil
1769   "Options line in the .newsrc file.")
1770
1771 (defvar gnus-newsrc-options-n nil
1772   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1773
1774 (defvar gnus-newsrc-last-checked-date nil
1775   "Date Gnus last asked server for new newsgroups.")
1776
1777 (defvar gnus-topic-topology nil
1778   "The complete topic hierarchy.")
1779
1780 (defvar gnus-topic-alist nil
1781   "The complete topic-group alist.")
1782
1783 (defvar gnus-newsrc-alist nil
1784   "Assoc list of read articles.
1785 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1786
1787 (defvar gnus-newsrc-hashtb nil
1788   "Hashtable of gnus-newsrc-alist.")
1789
1790 (defvar gnus-killed-list nil
1791   "List of killed newsgroups.")
1792
1793 (defvar gnus-killed-hashtb nil
1794   "Hash table equivalent of gnus-killed-list.")
1795
1796 (defvar gnus-zombie-list nil
1797   "List of almost dead newsgroups.")
1798
1799 (defvar gnus-description-hashtb nil
1800   "Descriptions of newsgroups.")
1801
1802 (defvar gnus-list-of-killed-groups nil
1803   "List of newsgroups that have recently been killed by the user.")
1804
1805 (defvar gnus-active-hashtb nil
1806   "Hashtable of active articles.")
1807
1808 (defvar gnus-moderated-list nil
1809   "List of moderated newsgroups.")
1810
1811 (defvar gnus-group-marked nil)
1812
1813 (defvar gnus-current-startup-file nil
1814   "Startup file for the current host.")
1815
1816 (defvar gnus-last-search-regexp nil
1817   "Default regexp for article search command.")
1818
1819 (defvar gnus-last-shell-command nil
1820   "Default shell command on article.")
1821
1822 (defvar gnus-current-select-method nil
1823   "The current method for selecting a newsgroup.")
1824
1825 (defvar gnus-group-list-mode nil)
1826
1827 (defvar gnus-article-internal-prepare-hook nil)
1828
1829 (defvar gnus-newsgroup-name nil)
1830 (defvar gnus-newsgroup-begin nil)
1831 (defvar gnus-newsgroup-end nil)
1832 (defvar gnus-newsgroup-last-rmail nil)
1833 (defvar gnus-newsgroup-last-mail nil)
1834 (defvar gnus-newsgroup-last-folder nil)
1835 (defvar gnus-newsgroup-last-file nil)
1836 (defvar gnus-newsgroup-auto-expire nil)
1837 (defvar gnus-newsgroup-active nil)
1838
1839 (defvar gnus-newsgroup-data nil)
1840 (defvar gnus-newsgroup-data-reverse nil)
1841 (defvar gnus-newsgroup-limit nil)
1842 (defvar gnus-newsgroup-limits nil)
1843
1844 (defvar gnus-newsgroup-unreads nil
1845   "List of unread articles in the current newsgroup.")
1846
1847 (defvar gnus-newsgroup-unselected nil
1848   "List of unselected unread articles in the current newsgroup.")
1849
1850 (defvar gnus-newsgroup-reads nil
1851   "Alist of read articles and article marks in the current newsgroup.")
1852
1853 (defvar gnus-newsgroup-expunged-tally nil)
1854
1855 (defvar gnus-newsgroup-marked nil
1856   "List of ticked articles in the current newsgroup (a subset of unread art).")
1857
1858 (defvar gnus-newsgroup-killed nil
1859   "List of ranges of articles that have been through the scoring process.")
1860
1861 (defvar gnus-newsgroup-cached nil
1862   "List of articles that come from the article cache.")
1863
1864 (defvar gnus-newsgroup-saved nil
1865   "List of articles that have been saved.")
1866
1867 (defvar gnus-newsgroup-kill-headers nil)
1868
1869 (defvar gnus-newsgroup-replied nil
1870   "List of articles that have been replied to in the current newsgroup.")
1871
1872 (defvar gnus-newsgroup-expirable nil
1873   "List of articles in the current newsgroup that can be expired.")
1874
1875 (defvar gnus-newsgroup-processable nil
1876   "List of articles in the current newsgroup that can be processed.")
1877
1878 (defvar gnus-newsgroup-bookmarks nil
1879   "List of articles in the current newsgroup that have bookmarks.")
1880
1881 (defvar gnus-newsgroup-dormant nil
1882   "List of dormant articles in the current newsgroup.")
1883
1884 (defvar gnus-newsgroup-scored nil
1885   "List of scored articles in the current newsgroup.")
1886
1887 (defvar gnus-newsgroup-headers nil
1888   "List of article headers in the current newsgroup.")
1889
1890 (defvar gnus-newsgroup-threads nil)
1891
1892 (defvar gnus-newsgroup-prepared nil
1893   "Whether the current group has been prepared properly.")
1894
1895 (defvar gnus-newsgroup-ancient nil
1896   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1897
1898 (defvar gnus-newsgroup-sparse nil)
1899
1900 (defvar gnus-current-article nil)
1901 (defvar gnus-article-current nil)
1902 (defvar gnus-current-headers nil)
1903 (defvar gnus-have-all-headers nil)
1904 (defvar gnus-last-article nil)
1905 (defvar gnus-newsgroup-history nil)
1906 (defvar gnus-current-kill-article nil)
1907
1908 ;; Save window configuration.
1909 (defvar gnus-prev-winconf nil)
1910
1911 (defvar gnus-summary-mark-positions nil)
1912 (defvar gnus-group-mark-positions nil)
1913
1914 (defvar gnus-reffed-article-number nil)
1915
1916 ;;; Let the byte-compiler know that we know about this variable.
1917 (defvar rmail-default-rmail-file)
1918
1919 (defvar gnus-cache-removable-articles nil)
1920
1921 (defvar gnus-dead-summary nil)
1922
1923 (defconst gnus-summary-local-variables
1924   '(gnus-newsgroup-name
1925     gnus-newsgroup-begin gnus-newsgroup-end
1926     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1927     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1928     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1929     gnus-newsgroup-unselected gnus-newsgroup-marked
1930     gnus-newsgroup-reads gnus-newsgroup-saved
1931     gnus-newsgroup-replied gnus-newsgroup-expirable
1932     gnus-newsgroup-processable gnus-newsgroup-killed
1933     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1934     gnus-newsgroup-headers gnus-newsgroup-threads
1935     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1936     gnus-current-article gnus-current-headers gnus-have-all-headers
1937     gnus-last-article gnus-article-internal-prepare-hook
1938     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1939     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1940     gnus-newsgroup-async gnus-thread-expunge-below
1941     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1942     (gnus-summary-mark-below . global)
1943     gnus-newsgroup-active gnus-scores-exclude-files
1944     gnus-newsgroup-history gnus-newsgroup-ancient
1945     gnus-newsgroup-sparse
1946     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1947     gnus-newsgroup-adaptive-score-file
1948     (gnus-newsgroup-expunged-tally . 0)
1949     gnus-cache-removable-articles gnus-newsgroup-cached
1950     gnus-newsgroup-data gnus-newsgroup-data-reverse
1951     gnus-newsgroup-limit gnus-newsgroup-limits)
1952   "Variables that are buffer-local to the summary buffers.")
1953
1954 (defconst gnus-bug-message
1955   "Sending a bug report to the Gnus Towers.
1956 ========================================
1957
1958 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1959 be sent to the Gnus Bug Exterminators.
1960
1961 At the bottom of the buffer you'll see lots of variable settings.
1962 Please do not delete those.  They will tell the Bug People what your
1963 environment is, so that it will be easier to locate the bugs.
1964
1965 If you have found a bug that makes Emacs go \"beep\", set
1966 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1967 and include the backtrace in your bug report.
1968
1969 Please describe the bug in annoying, painstaking detail.
1970
1971 Thank you for your help in stamping out bugs.
1972 ")
1973
1974 ;;; End of variables.
1975
1976 ;; Define some autoload functions Gnus might use.
1977 (eval-and-compile
1978
1979   ;; This little mapcar goes through the list below and marks the
1980   ;; symbols in question as autoloaded functions.
1981   (mapcar
1982    (lambda (package)
1983      (let ((interactive (nth 1 (memq ':interactive package))))
1984        (mapcar
1985         (lambda (function)
1986           (let (keymap)
1987             (when (consp function)
1988               (setq keymap (car (memq 'keymap function)))
1989               (setq function (car function)))
1990             (autoload function (car package) nil interactive keymap)))
1991         (if (eq (nth 1 package) ':interactive)
1992             (cdddr package)
1993           (cdr package)))))
1994    '(("metamail" metamail-buffer)
1995      ("info" Info-goto-node)
1996      ("hexl" hexl-hex-string-to-integer)
1997      ("pp" pp pp-to-string pp-eval-expression)
1998      ("mail-extr" mail-extract-address-components)
1999      ("nnmail" nnmail-split-fancy nnmail-article-group)
2000      ("nnvirtual" nnvirtual-catchup-group)
2001      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2002       timezone-make-sortable-date timezone-make-time-string)
2003      ("rmailout" rmail-output)
2004      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2005       rmail-show-message)
2006      ("gnus-soup" :interactive t
2007       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2008       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2009      ("nnsoup" nnsoup-pack-replies)
2010      ("gnus-scomo" :interactive t gnus-score-mode)
2011      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2012       gnus-Folder-save-name gnus-folder-save-name)
2013      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2014      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2015       gnus-server-make-menu-bar gnus-article-make-menu-bar
2016       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2017       gnus-summary-highlight-line gnus-carpal-setup-buffer
2018       gnus-group-highlight-line
2019       gnus-article-add-button gnus-insert-next-page-button
2020       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2021      ("gnus-vis" :interactive t
2022       gnus-article-push-button gnus-article-press-button
2023       gnus-article-highlight gnus-article-highlight-some
2024       gnus-article-highlight-headers gnus-article-highlight-signature
2025       gnus-article-add-buttons gnus-article-add-buttons-to-head
2026       gnus-article-next-button gnus-article-prev-button)
2027      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2028       gnus-demon-add-disconnection gnus-demon-add-handler
2029       gnus-demon-remove-handler)
2030      ("gnus-demon" :interactive t
2031       gnus-demon-init gnus-demon-cancel)
2032      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2033       gnus-tree-open gnus-tree-close)
2034      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2035       gnus-nocem-unwanted-article-p)
2036      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2037      ("gnus-srvr" gnus-browse-foreign-server)
2038      ("gnus-cite" :interactive t
2039       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2040       gnus-article-hide-citation gnus-article-fill-cited-article
2041       gnus-article-hide-citation-in-followups)
2042      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2043       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2044       gnus-execute gnus-expunge)
2045      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2046       gnus-cache-possibly-remove-articles gnus-cache-request-article
2047       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2048       gnus-cache-enter-remove-article gnus-cached-article-p
2049       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2050      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2051       gnus-cache-remove-article)
2052      ("gnus-score" :interactive t
2053       gnus-summary-increase-score gnus-summary-lower-score
2054       gnus-score-flush-cache gnus-score-close
2055       gnus-score-raise-same-subject-and-select
2056       gnus-score-raise-same-subject gnus-score-default
2057       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2058       gnus-score-lower-same-subject gnus-score-lower-thread
2059       gnus-possibly-score-headers gnus-summary-raise-score 
2060       gnus-summary-set-score gnus-summary-current-score)
2061      ("gnus-score"
2062       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2063       gnus-current-score-file-nondirectory gnus-score-adaptive
2064       gnus-score-find-trace gnus-score-file-name)
2065      ("gnus-edit" :interactive t gnus-score-customize)
2066      ("gnus-topic" :interactive t gnus-topic-mode)
2067      ("gnus-topic" gnus-topic-remove-group)
2068      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2069      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2070      ("gnus-uu" :interactive t
2071       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2072       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2073       gnus-uu-mark-by-regexp gnus-uu-mark-all
2074       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2075       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2076       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2077       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2078       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2079       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2080       gnus-uu-decode-binhex-view)
2081      ("gnus-msg" (gnus-summary-send-map keymap)
2082       gnus-mail-yank-original gnus-mail-send-and-exit
2083       gnus-sendmail-setup-mail gnus-article-mail
2084       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2085      ("gnus-msg" :interactive t
2086       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2087       gnus-summary-followup gnus-summary-followup-with-original
2088       gnus-summary-cancel-article gnus-summary-supersede-article
2089       gnus-post-news gnus-inews-news gnus-cancel-news
2090       gnus-summary-reply gnus-summary-reply-with-original
2091       gnus-summary-mail-forward gnus-summary-mail-other-window
2092       gnus-bug)
2093      ("gnus-picon" :interactive t gnus-article-display-picons
2094       gnus-group-display-picons gnus-picons-article-display-x-face)
2095      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2096       gnus-grouplens-mode)
2097      ("gnus-vm" gnus-vm-mail-setup)
2098      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2099       gnus-summary-save-article-vm))))
2100
2101 \f
2102
2103 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2104 ;; If you want the cursor to go somewhere else, set these two
2105 ;; functions in some startup hook to whatever you want.
2106 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2107 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2108
2109 ;;; Various macros and substs.
2110
2111 (defun gnus-header-from (header)
2112   (mail-header-from header))
2113
2114 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2115   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2116   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2117         (w (make-symbol "w"))
2118         (buf (make-symbol "buf")))
2119     `(let* ((,tempvar (selected-window))
2120             (,buf ,buffer)
2121             (,w (get-buffer-window ,buf 'visible)))
2122        (unwind-protect
2123            (progn
2124              (if ,w
2125                  (select-window ,w)
2126                (pop-to-buffer ,buf))
2127              ,@forms)
2128          (select-window ,tempvar)))))
2129
2130 (defmacro gnus-gethash (string hashtable)
2131   "Get hash value of STRING in HASHTABLE."
2132   `(symbol-value (intern-soft ,string ,hashtable)))
2133
2134 (defmacro gnus-sethash (string value hashtable)
2135   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2136   `(set (intern ,string ,hashtable) ,value))
2137
2138 (defmacro gnus-intern-safe (string hashtable)
2139   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2140   `(let ((symbol (intern ,string ,hashtable)))
2141      (or (boundp symbol)
2142          (set symbol nil))
2143      symbol))
2144
2145 (defmacro gnus-group-unread (group)
2146   "Get the currently computed number of unread articles in GROUP."
2147   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2148
2149 (defmacro gnus-group-entry (group)
2150   "Get the newsrc entry for GROUP."
2151   `(gnus-gethash ,group gnus-newsrc-hashtb))
2152
2153 (defmacro gnus-active (group)
2154   "Get active info on GROUP."
2155   `(gnus-gethash ,group gnus-active-hashtb))
2156
2157 (defmacro gnus-set-active (group active)
2158   "Set GROUP's active info."
2159   `(gnus-sethash ,group ,active gnus-active-hashtb))
2160
2161 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2162 ;;   function `substring' might cut on a middle of multi-octet
2163 ;;   character.
2164 (defun gnus-truncate-string (str width)
2165   (substring str 0 width))
2166
2167 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2168 ;; to limit the length of a string.  This function is necessary since
2169 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2170 (defsubst gnus-limit-string (str width)
2171   (if (> (length str) width)
2172       (substring str 0 width)
2173     str))
2174
2175 (defsubst gnus-simplify-subject-re (subject)
2176   "Remove \"Re:\" from subject lines."
2177   (if (string-match "^[Rr][Ee]: *" subject)
2178       (substring subject (match-end 0))
2179     subject))
2180
2181 (defsubst gnus-functionp (form)
2182   "Return non-nil if FORM is funcallable."
2183   (or (and (symbolp form) (fboundp form))
2184       (and (listp form) (eq (car form) 'lambda))))
2185
2186 (defsubst gnus-goto-char (point)
2187   (and point (goto-char point)))
2188
2189 (defmacro gnus-buffer-exists-p (buffer)
2190   `(let ((buffer ,buffer))
2191      (and buffer
2192           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2193                    buffer))))
2194
2195 (defmacro gnus-kill-buffer (buffer)
2196   `(let ((buf ,buffer))
2197      (if (gnus-buffer-exists-p buf)
2198          (kill-buffer buf))))
2199
2200 (defsubst gnus-point-at-bol ()
2201   "Return point at the beginning of the line."
2202   (let ((p (point)))
2203     (beginning-of-line)
2204     (prog1
2205         (point)
2206       (goto-char p))))
2207
2208 (defsubst gnus-point-at-eol ()
2209   "Return point at the end of the line."
2210   (let ((p (point)))
2211     (end-of-line)
2212     (prog1
2213         (point)
2214       (goto-char p))))
2215
2216 (defun gnus-alive-p ()
2217   "Say whether Gnus is running or not."
2218   (and gnus-group-buffer
2219        (get-buffer gnus-group-buffer)))
2220
2221 (defun gnus-delete-first (elt list)
2222   "Delete by side effect the first occurrence of ELT as a member of LIST."
2223   (if (equal (car list) elt)
2224       (cdr list)
2225     (let ((total list))
2226       (while (and (cdr list)
2227                   (not (equal (cadr list) elt)))
2228         (setq list (cdr list)))
2229       (when (cdr list)
2230         (setcdr list (cddr list)))
2231       total)))
2232
2233 ;; Delete the current line (and the next N lines.);
2234 (defmacro gnus-delete-line (&optional n)
2235   `(delete-region (progn (beginning-of-line) (point))
2236                   (progn (forward-line ,(or n 1)) (point))))
2237
2238 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2239 (defvar gnus-init-inhibit nil)
2240 (defun gnus-read-init-file (&optional inhibit-next)
2241   (if gnus-init-inhibit
2242       (setq gnus-init-inhibit nil)
2243     (setq gnus-init-inhibit inhibit-next)
2244     (and gnus-init-file
2245          (or (and (file-exists-p gnus-init-file)
2246                   ;; Don't try to load a directory.
2247                   (not (file-directory-p gnus-init-file)))
2248              (file-exists-p (concat gnus-init-file ".el"))
2249              (file-exists-p (concat gnus-init-file ".elc")))
2250          (condition-case var
2251              (load gnus-init-file nil t)
2252            (error
2253             (error "Error in %s: %s" gnus-init-file var))))))
2254
2255 ;; Info access macros.
2256
2257 (defmacro gnus-info-group (info)
2258   `(nth 0 ,info))
2259 (defmacro gnus-info-rank (info)
2260   `(nth 1 ,info))
2261 (defmacro gnus-info-read (info)
2262   `(nth 2 ,info))
2263 (defmacro gnus-info-marks (info)
2264   `(nth 3 ,info))
2265 (defmacro gnus-info-method (info)
2266   `(nth 4 ,info))
2267 (defmacro gnus-info-params (info)
2268   `(nth 5 ,info))
2269
2270 (defmacro gnus-info-level (info)
2271   `(let ((rank (gnus-info-rank ,info)))
2272      (if (consp rank)
2273          (car rank)
2274        rank)))
2275 (defmacro gnus-info-score (info)
2276   `(let ((rank (gnus-info-rank ,info)))
2277      (or (and (consp rank) (cdr rank)) 0)))
2278
2279 (defmacro gnus-info-set-group (info group)
2280   `(setcar ,info ,group))
2281 (defmacro gnus-info-set-rank (info rank)
2282   `(setcar (nthcdr 1 ,info) ,rank))
2283 (defmacro gnus-info-set-read (info read)
2284   `(setcar (nthcdr 2 ,info) ,read))
2285 (defmacro gnus-info-set-marks (info marks)
2286   `(setcar (nthcdr 3 ,info) ,marks))
2287 (defmacro gnus-info-set-method (info method)
2288   `(setcar (nthcdr 4 ,info) ,method))
2289 (defmacro gnus-info-set-params (info params)
2290   `(setcar (nthcdr 5 ,info) ,params))
2291
2292 (defmacro gnus-info-set-level (info level)
2293   `(let ((rank (cdr ,info)))
2294      (if (consp (car rank))
2295          (setcar (car rank) ,level)
2296        (setcar rank ,level))))
2297 (defmacro gnus-info-set-score (info score)
2298   `(let ((rank (cdr ,info)))
2299      (if (consp (car rank))
2300          (setcdr (car rank) ,score)
2301        (setcar rank (cons (car rank) ,score)))))
2302
2303 (defmacro gnus-get-info (group)
2304   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2305
2306 (defun gnus-byte-code (func)
2307   "Return a form that can be `eval'ed based on FUNC."
2308   (let ((fval (symbol-function func)))
2309     (if (byte-code-function-p fval)
2310         (let ((flist (append fval nil)))
2311           (setcar flist 'byte-code)
2312           flist)
2313       (cons 'progn (cddr fval)))))
2314
2315 ;; Find out whether the gnus-visual TYPE is wanted.
2316 (defun gnus-visual-p (&optional type class)
2317   (and gnus-visual                      ; Has to be non-nil, at least.
2318        (if (not type)                   ; We don't care about type.
2319            gnus-visual
2320          (if (listp gnus-visual)        ; It's a list, so we check it.
2321              (or (memq type gnus-visual)
2322                  (memq class gnus-visual))
2323            t))))
2324
2325 ;;; Load the compatability functions.
2326
2327 (require 'gnus-cus)
2328 (require 'gnus-ems)
2329
2330 \f
2331 ;;;
2332 ;;; Shutdown
2333 ;;;
2334
2335 (defvar gnus-shutdown-alist nil)
2336
2337 (defun gnus-add-shutdown (function &rest symbols)
2338   "Run FUNCTION whenever one of SYMBOLS is shut down."
2339   (push (cons function symbols) gnus-shutdown-alist))
2340
2341 (defun gnus-shutdown (symbol)
2342   "Shut down everything that waits for SYMBOL."
2343   (let ((alist gnus-shutdown-alist)
2344         entry)
2345     (while (setq entry (pop alist))
2346       (when (memq symbol (cdr entry))
2347         (funcall (car entry))))))
2348
2349 \f
2350
2351 ;; Format specs.  The chunks below are the machine-generated forms
2352 ;; that are to be evaled as the result of the default format strings.
2353 ;; We write them in here to get them byte-compiled.  That way the
2354 ;; default actions will be quite fast, while still retaining the full
2355 ;; flexibility of the user-defined format specs.
2356
2357 ;; First we have lots of dummy defvars to let the compiler know these
2358 ;; are really dynamic variables.
2359
2360 (defvar gnus-tmp-unread)
2361 (defvar gnus-tmp-replied)
2362 (defvar gnus-tmp-score-char)
2363 (defvar gnus-tmp-indentation)
2364 (defvar gnus-tmp-opening-bracket)
2365 (defvar gnus-tmp-lines)
2366 (defvar gnus-tmp-name)
2367 (defvar gnus-tmp-closing-bracket)
2368 (defvar gnus-tmp-subject-or-nil)
2369 (defvar gnus-tmp-subject)
2370 (defvar gnus-tmp-marked)
2371 (defvar gnus-tmp-marked-mark)
2372 (defvar gnus-tmp-subscribed)
2373 (defvar gnus-tmp-process-marked)
2374 (defvar gnus-tmp-number-of-unread)
2375 (defvar gnus-tmp-group-name)
2376 (defvar gnus-tmp-group)
2377 (defvar gnus-tmp-article-number)
2378 (defvar gnus-tmp-unread-and-unselected)
2379 (defvar gnus-tmp-news-method)
2380 (defvar gnus-tmp-news-server)
2381 (defvar gnus-tmp-article-number)
2382 (defvar gnus-mouse-face)
2383 (defvar gnus-mouse-face-prop)
2384
2385 (defun gnus-summary-line-format-spec ()
2386   (insert gnus-tmp-unread gnus-tmp-replied
2387           gnus-tmp-score-char gnus-tmp-indentation)
2388   (gnus-put-text-property
2389    (point)
2390    (progn
2391      (insert
2392       gnus-tmp-opening-bracket
2393       (format "%4d: %-20s"
2394               gnus-tmp-lines
2395               (if (> (length gnus-tmp-name) 20)
2396                   (substring gnus-tmp-name 0 20)
2397                 gnus-tmp-name))
2398       gnus-tmp-closing-bracket)
2399      (point))
2400    gnus-mouse-face-prop gnus-mouse-face)
2401   (insert " " gnus-tmp-subject-or-nil "\n"))
2402
2403 (defvar gnus-summary-line-format-spec
2404   (gnus-byte-code 'gnus-summary-line-format-spec))
2405
2406 (defun gnus-summary-dummy-line-format-spec ()
2407   (insert "*  ")
2408   (gnus-put-text-property
2409    (point)
2410    (progn
2411      (insert ":                          :")
2412      (point))
2413    gnus-mouse-face-prop gnus-mouse-face)
2414   (insert " " gnus-tmp-subject "\n"))
2415
2416 (defvar gnus-summary-dummy-line-format-spec
2417   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2418
2419 (defun gnus-group-line-format-spec ()
2420   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2421           gnus-tmp-process-marked
2422           gnus-group-indentation
2423           (format "%5s: " gnus-tmp-number-of-unread))
2424   (gnus-put-text-property
2425    (point)
2426    (progn
2427      (insert gnus-tmp-group "\n")
2428      (1- (point)))
2429    gnus-mouse-face-prop gnus-mouse-face))
2430 (defvar gnus-group-line-format-spec
2431   (gnus-byte-code 'gnus-group-line-format-spec))
2432
2433 (defvar gnus-format-specs
2434   `((version . ,emacs-version)
2435     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2436     (summary-dummy ,gnus-summary-dummy-line-format
2437                    ,gnus-summary-dummy-line-format-spec)
2438     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2439
2440 (defvar gnus-article-mode-line-format-spec nil)
2441 (defvar gnus-summary-mode-line-format-spec nil)
2442 (defvar gnus-group-mode-line-format-spec nil)
2443
2444 ;;; Phew.  All that gruft is over, fortunately.
2445
2446 \f
2447 ;;;
2448 ;;; Gnus Utility Functions
2449 ;;;
2450
2451 (defun gnus-extract-address-components (from)
2452   (let (name address)
2453     ;; First find the address - the thing with the @ in it.  This may
2454     ;; not be accurate in mail addresses, but does the trick most of
2455     ;; the time in news messages.
2456     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2457         (setq address (substring from (match-beginning 0) (match-end 0))))
2458     ;; Then we check whether the "name <address>" format is used.
2459     (and address
2460          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2461          ;; Linear white space is not required.
2462          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2463          (and (setq name (substring from 0 (match-beginning 0)))
2464               ;; Strip any quotes from the name.
2465               (string-match "\".*\"" name)
2466               (setq name (substring name 1 (1- (match-end 0))))))
2467     ;; If not, then "address (name)" is used.
2468     (or name
2469         (and (string-match "(.+)" from)
2470              (setq name (substring from (1+ (match-beginning 0))
2471                                    (1- (match-end 0)))))
2472         (and (string-match "()" from)
2473              (setq name address))
2474         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2475         ;; XOVER might not support folded From headers.
2476         (and (string-match "(.*" from)
2477              (setq name (substring from (1+ (match-beginning 0))
2478                                    (match-end 0)))))
2479     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2480     (list (or name from) (or address from))))
2481
2482 (defun gnus-fetch-field (field)
2483   "Return the value of the header FIELD of current article."
2484   (save-excursion
2485     (save-restriction
2486       (let ((case-fold-search t)
2487             (inhibit-point-motion-hooks t))
2488         (nnheader-narrow-to-headers)
2489         (message-fetch-field field)))))
2490
2491 (defun gnus-goto-colon ()
2492   (beginning-of-line)
2493   (search-forward ":" (gnus-point-at-eol) t))
2494
2495 ;;;###autoload
2496 (defun gnus-update-format (var)
2497   "Update the format specification near point."
2498   (interactive
2499    (list
2500     (save-excursion
2501       (eval-defun nil)
2502       ;; Find the end of the current word.
2503       (re-search-forward "[ \t\n]" nil t)
2504       ;; Search backward.
2505       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2506         (match-string 1)))))
2507   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2508                               (match-string 1 var))))
2509          (entry (assq type gnus-format-specs))
2510          value spec)
2511     (when entry
2512       (setq gnus-format-specs (delq entry gnus-format-specs)))
2513     (set
2514      (intern (format "%s-spec" var))
2515      (gnus-parse-format (setq value (symbol-value (intern var)))
2516                         (symbol-value (intern (format "%s-alist" var)))
2517                         (not (string-match "mode" var))))
2518     (setq spec (symbol-value (intern (format "%s-spec" var))))
2519     (push (list type value spec) gnus-format-specs)
2520
2521     (pop-to-buffer "*Gnus Format*")
2522     (erase-buffer)
2523     (lisp-interaction-mode)
2524     (insert (pp-to-string spec))))
2525
2526 (defun gnus-update-format-specifications (&optional force)
2527   "Update all (necessary) format specifications."
2528   ;; Make the indentation array.
2529   (gnus-make-thread-indent-array)
2530
2531   ;; See whether all the stored info needs to be flushed.
2532   (when (or force
2533             (not (equal emacs-version
2534                         (cdr (assq 'version gnus-format-specs)))))
2535     (setq gnus-format-specs nil))
2536
2537   ;; Go through all the formats and see whether they need updating.
2538   (let ((types '(summary summary-dummy group
2539                          summary-mode group-mode article-mode))
2540         new-format entry type val)
2541     (while (setq type (pop types))
2542       ;; Jump to the proper buffer to find out the value of
2543       ;; the variable, if possible.  (It may be buffer-local.)
2544       (save-excursion
2545         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2546               val)
2547           (when (and (boundp buffer)
2548                      (setq val (symbol-value buffer))
2549                      (get-buffer val)
2550                      (buffer-name (get-buffer val)))
2551             (set-buffer (get-buffer val)))
2552           (setq new-format (symbol-value
2553                             (intern (format "gnus-%s-line-format" type))))))
2554       (setq entry (cdr (assq type gnus-format-specs)))
2555       (if (and entry
2556                (equal (car entry) new-format))
2557           ;; Use the old format.
2558           (set (intern (format "gnus-%s-line-format-spec" type))
2559                (cadr entry))
2560         ;; This is a new format.
2561         (setq val
2562               (if (not (stringp new-format))
2563                   ;; This is a function call or something.
2564                   new-format
2565                 ;; This is a "real" format.
2566                 (gnus-parse-format
2567                  new-format
2568                  (symbol-value
2569                   (intern (format "gnus-%s-line-format-alist"
2570                                   (if (eq type 'article-mode)
2571                                       'summary-mode type))))
2572                  (not (string-match "mode$" (symbol-name type))))))
2573         ;; Enter the new format spec into the list.
2574         (if entry
2575             (progn
2576               (setcar (cdr entry) val)
2577               (setcar entry new-format))
2578           (push (list type new-format val) gnus-format-specs))
2579         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2580
2581   (unless (assq 'version gnus-format-specs)
2582     (push (cons 'version emacs-version) gnus-format-specs))
2583
2584   (gnus-update-group-mark-positions)
2585   (gnus-update-summary-mark-positions))
2586
2587 (defun gnus-update-summary-mark-positions ()
2588   "Compute where the summary marks are to go."
2589   (save-excursion
2590     (when (and gnus-summary-buffer
2591                (get-buffer gnus-summary-buffer)
2592                (buffer-name (get-buffer gnus-summary-buffer)))
2593       (set-buffer gnus-summary-buffer))
2594     (let ((gnus-replied-mark 129)
2595           (gnus-score-below-mark 130)
2596           (gnus-score-over-mark 130)
2597           (thread nil)
2598           (gnus-visual nil)
2599           (spec gnus-summary-line-format-spec)
2600           pos)
2601       (save-excursion
2602         (gnus-set-work-buffer)
2603         (let ((gnus-summary-line-format-spec spec))
2604           (gnus-summary-insert-line
2605            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2606           (goto-char (point-min))
2607           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2608                                              (- (point) 2)))))
2609           (goto-char (point-min))
2610           (push (cons 'replied (and (search-forward "\201" nil t) 
2611                                     (- (point) 2)))
2612                 pos)
2613           (goto-char (point-min))
2614           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2615                 pos)))
2616       (setq gnus-summary-mark-positions pos))))
2617
2618 (defun gnus-update-group-mark-positions ()
2619   (save-excursion
2620     (let ((gnus-process-mark 128)
2621           (gnus-group-marked '("dummy.group"))
2622           (gnus-active-hashtb (make-vector 10 0)))
2623       (gnus-set-active "dummy.group" '(0 . 0))
2624       (gnus-set-work-buffer)
2625       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2626       (goto-char (point-min))
2627       (setq gnus-group-mark-positions
2628             (list (cons 'process (and (search-forward "\200" nil t)
2629                                       (- (point) 2))))))))
2630
2631 (defvar gnus-mouse-face-0 'highlight)
2632 (defvar gnus-mouse-face-1 'highlight)
2633 (defvar gnus-mouse-face-2 'highlight)
2634 (defvar gnus-mouse-face-3 'highlight)
2635 (defvar gnus-mouse-face-4 'highlight)
2636
2637 (defun gnus-mouse-face-function (form type)
2638   `(gnus-put-text-property
2639     (point) (progn ,@form (point))
2640     gnus-mouse-face-prop
2641     ,(if (equal type 0)
2642          'gnus-mouse-face
2643        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2644
2645 (defvar gnus-face-0 'bold)
2646 (defvar gnus-face-1 'italic)
2647 (defvar gnus-face-2 'bold-italic)
2648 (defvar gnus-face-3 'bold)
2649 (defvar gnus-face-4 'bold)
2650
2651 (defun gnus-face-face-function (form type)
2652   `(gnus-put-text-property
2653     (point) (progn ,@form (point))
2654     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2655
2656 (defun gnus-max-width-function (el max-width)
2657   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2658   (if (symbolp el)
2659       `(if (> (length ,el) ,max-width)
2660            (substring ,el 0 ,max-width)
2661          ,el)
2662     `(let ((val (eval ,el)))
2663        (if (numberp val)
2664            (setq val (int-to-string val)))
2665        (if (> (length val) ,max-width)
2666            (substring val 0 ,max-width)
2667          val))))
2668
2669 (defun gnus-parse-format (format spec-alist &optional insert)
2670   ;; This function parses the FORMAT string with the help of the
2671   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2672   ;; string.  If the FORMAT string contains the specifiers %( and %)
2673   ;; the text between them will have the mouse-face text property.
2674   (if (string-match
2675        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2676        format)
2677       (gnus-parse-complex-format format spec-alist)
2678     ;; This is a simple format.
2679     (gnus-parse-simple-format format spec-alist insert)))
2680
2681 (defun gnus-parse-complex-format (format spec-alist)
2682   (save-excursion
2683     (gnus-set-work-buffer)
2684     (insert format)
2685     (goto-char (point-min))
2686     (while (re-search-forward "\"" nil t)
2687       (replace-match "\\\"" nil t))
2688     (goto-char (point-min))
2689     (insert "(\"")
2690     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2691       (let ((number (if (match-beginning 1)
2692                         (match-string 1) "0"))
2693             (delim (aref (match-string 2) 0)))
2694         (if (or (= delim ?\() (= delim ?\{))
2695             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2696                                    " " number " \""))
2697           (replace-match "\")\""))))
2698     (goto-char (point-max))
2699     (insert "\")")
2700     (goto-char (point-min))
2701     (let ((form (read (current-buffer))))
2702       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2703
2704 (defun gnus-complex-form-to-spec (form spec-alist)
2705   (delq nil
2706         (mapcar
2707          (lambda (sform)
2708            (if (stringp sform)
2709                (gnus-parse-simple-format sform spec-alist t)
2710              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2711                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2712                       (nth 1 sform))))
2713          form)))
2714
2715 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2716   ;; This function parses the FORMAT string with the help of the
2717   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2718   ;; string.
2719   (let ((max-width 0)
2720         spec flist fstring newspec elem beg result dontinsert)
2721     (save-excursion
2722       (gnus-set-work-buffer)
2723       (insert format)
2724       (goto-char (point-min))
2725       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2726                                 nil t)
2727         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2728               (setq newspec "%"
2729                     beg (1+ (match-beginning 0)))
2730           ;; First check if there are any specs that look anything like
2731           ;; "%12,12A", ie. with a "max width specification".  These have
2732           ;; to be treated specially.
2733           (if (setq beg (match-beginning 1))
2734               (setq max-width
2735                     (string-to-int
2736                      (buffer-substring
2737                       (1+ (match-beginning 1)) (match-end 1))))
2738             (setq max-width 0)
2739             (setq beg (match-beginning 2)))
2740           ;; Find the specification from `spec-alist'.
2741           (unless (setq elem (cdr (assq spec spec-alist)))
2742             (setq elem '("*" ?s)))
2743           ;; Treat user defined format specifiers specially.
2744           (when (eq (car elem) 'gnus-tmp-user-defined)
2745             (setq elem
2746                   (list
2747                    (list (intern (concat "gnus-user-format-function-"
2748                                          (match-string 3)))
2749                          'gnus-tmp-header) ?s))
2750             (delete-region (match-beginning 3) (match-end 3)))
2751           (if (not (zerop max-width))
2752               (let ((el (car elem)))
2753                 (cond ((= (cadr elem) ?c)
2754                        (setq el (list 'char-to-string el)))
2755                       ((= (cadr elem) ?d)
2756                        (setq el (list 'int-to-string el))))
2757                 (setq flist (cons (gnus-max-width-function el max-width)
2758                                   flist))
2759                 (setq newspec ?s))
2760             (progn
2761               (setq flist (cons (car elem) flist))
2762               (setq newspec (cadr elem)))))
2763         ;; Remove the old specification (and possibly a ",12" string).
2764         (delete-region beg (match-end 2))
2765         ;; Insert the new specification.
2766         (goto-char beg)
2767         (insert newspec))
2768       (setq fstring (buffer-substring 1 (point-max))))
2769     ;; Do some postprocessing to increase efficiency.
2770     (setq
2771      result
2772      (cond
2773       ;; Emptyness.
2774       ((string= fstring "")
2775        nil)
2776       ;; Not a format string.
2777       ((not (string-match "%" fstring))
2778        (list fstring))
2779       ;; A format string with just a single string spec.
2780       ((string= fstring "%s")
2781        (list (car flist)))
2782       ;; A single character.
2783       ((string= fstring "%c")
2784        (list (car flist)))
2785       ;; A single number.
2786       ((string= fstring "%d")
2787        (setq dontinsert)
2788        (if insert
2789            (list `(princ ,(car flist)))
2790          (list `(int-to-string ,(car flist)))))
2791       ;; Just lots of chars and strings.
2792       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2793        (nreverse flist))
2794       ;; A single string spec at the beginning of the spec.
2795       ((string-match "\\`%[sc][^%]+\\'" fstring)
2796        (list (car flist) (substring fstring 2)))
2797       ;; A single string spec in the middle of the spec.
2798       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2799        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2800       ;; A single string spec in the end of the spec.
2801       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2802        (list (match-string 1 fstring) (car flist)))
2803       ;; A more complex spec.
2804       (t
2805        (list (cons 'format (cons fstring (nreverse flist)))))))
2806
2807     (if insert
2808         (when result
2809           (if dontinsert
2810               result
2811             (cons 'insert result)))
2812       (cond ((stringp result)
2813              result)
2814             ((consp result)
2815              (cons 'concat result))
2816             (t "")))))
2817
2818 (defun gnus-eval-format (format &optional alist props)
2819   "Eval the format variable FORMAT, using ALIST.
2820 If PROPS, insert the result."
2821   (let ((form (gnus-parse-format format alist props)))
2822     (if props
2823         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2824       (eval form))))
2825
2826 (defun gnus-remove-text-with-property (prop)
2827   "Delete all text in the current buffer with text property PROP."
2828   (save-excursion
2829     (goto-char (point-min))
2830     (while (not (eobp))
2831       (while (get-text-property (point) prop)
2832         (delete-char 1))
2833       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2834
2835 (defun gnus-set-work-buffer ()
2836   (if (get-buffer gnus-work-buffer)
2837       (progn
2838         (set-buffer gnus-work-buffer)
2839         (erase-buffer))
2840     (set-buffer (get-buffer-create gnus-work-buffer))
2841     (kill-all-local-variables)
2842     (buffer-disable-undo (current-buffer))
2843     (gnus-add-current-to-buffer-list)))
2844
2845 ;; Article file names when saving.
2846
2847 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2848   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2849 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2850 Otherwise, it is like ~/News/news/group/num."
2851   (let ((default
2852           (expand-file-name
2853            (concat (if (gnus-use-long-file-name 'not-save)
2854                        (gnus-capitalize-newsgroup newsgroup)
2855                      (gnus-newsgroup-directory-form newsgroup))
2856                    "/" (int-to-string (mail-header-number headers)))
2857            gnus-article-save-directory)))
2858     (if (and last-file
2859              (string-equal (file-name-directory default)
2860                            (file-name-directory last-file))
2861              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2862         default
2863       (or last-file default))))
2864
2865 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2866   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2867 If variable `gnus-use-long-file-name' is non-nil, it is
2868 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2869   (let ((default
2870           (expand-file-name
2871            (concat (if (gnus-use-long-file-name 'not-save)
2872                        newsgroup
2873                      (gnus-newsgroup-directory-form newsgroup))
2874                    "/" (int-to-string (mail-header-number headers)))
2875            gnus-article-save-directory)))
2876     (if (and last-file
2877              (string-equal (file-name-directory default)
2878                            (file-name-directory last-file))
2879              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2880         default
2881       (or last-file default))))
2882
2883 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2884   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2885 If variable `gnus-use-long-file-name' is non-nil, it is
2886 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2887   (or last-file
2888       (expand-file-name
2889        (if (gnus-use-long-file-name 'not-save)
2890            (gnus-capitalize-newsgroup newsgroup)
2891          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2892        gnus-article-save-directory)))
2893
2894 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2895   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2896 If variable `gnus-use-long-file-name' is non-nil, it is
2897 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2898   (or last-file
2899       (expand-file-name
2900        (if (gnus-use-long-file-name 'not-save)
2901            newsgroup
2902          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2903        gnus-article-save-directory)))
2904
2905 ;; For subscribing new newsgroup
2906
2907 (defun gnus-subscribe-hierarchical-interactive (groups)
2908   (let ((groups (sort groups 'string<))
2909         prefixes prefix start ans group starts)
2910     (while groups
2911       (setq prefixes (list "^"))
2912       (while (and groups prefixes)
2913         (while (not (string-match (car prefixes) (car groups)))
2914           (setq prefixes (cdr prefixes)))
2915         (setq prefix (car prefixes))
2916         (setq start (1- (length prefix)))
2917         (if (and (string-match "[^\\.]\\." (car groups) start)
2918                  (cdr groups)
2919                  (setq prefix
2920                        (concat "^" (substring (car groups) 0 (match-end 0))))
2921                  (string-match prefix (cadr groups)))
2922             (progn
2923               (setq prefixes (cons prefix prefixes))
2924               (message "Descend hierarchy %s? ([y]nsq): "
2925                        (substring prefix 1 (1- (length prefix))))
2926               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2927                 (ding)
2928                 (message "Descend hierarchy %s? ([y]nsq): "
2929                          (substring prefix 1 (1- (length prefix)))))
2930               (cond ((= ans ?n)
2931                      (while (and groups
2932                                  (string-match prefix
2933                                                (setq group (car groups))))
2934                        (setq gnus-killed-list
2935                              (cons group gnus-killed-list))
2936                        (gnus-sethash group group gnus-killed-hashtb)
2937                        (setq groups (cdr groups)))
2938                      (setq starts (cdr starts)))
2939                     ((= ans ?s)
2940                      (while (and groups
2941                                  (string-match prefix
2942                                                (setq group (car groups))))
2943                        (gnus-sethash group group gnus-killed-hashtb)
2944                        (gnus-subscribe-alphabetically (car groups))
2945                        (setq groups (cdr groups)))
2946                      (setq starts (cdr starts)))
2947                     ((= ans ?q)
2948                      (while groups
2949                        (setq group (car groups))
2950                        (setq gnus-killed-list (cons group gnus-killed-list))
2951                        (gnus-sethash group group gnus-killed-hashtb)
2952                        (setq groups (cdr groups))))
2953                     (t nil)))
2954           (message "Subscribe %s? ([n]yq)" (car groups))
2955           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2956             (ding)
2957             (message "Subscribe %s? ([n]yq)" (car groups)))
2958           (setq group (car groups))
2959           (cond ((= ans ?y)
2960                  (gnus-subscribe-alphabetically (car groups))
2961                  (gnus-sethash group group gnus-killed-hashtb))
2962                 ((= ans ?q)
2963                  (while groups
2964                    (setq group (car groups))
2965                    (setq gnus-killed-list (cons group gnus-killed-list))
2966                    (gnus-sethash group group gnus-killed-hashtb)
2967                    (setq groups (cdr groups))))
2968                 (t
2969                  (setq gnus-killed-list (cons group gnus-killed-list))
2970                  (gnus-sethash group group gnus-killed-hashtb)))
2971           (setq groups (cdr groups)))))))
2972
2973 (defun gnus-subscribe-randomly (newsgroup)
2974   "Subscribe new NEWSGROUP by making it the first newsgroup."
2975   (gnus-subscribe-newsgroup newsgroup))
2976
2977 (defun gnus-subscribe-alphabetically (newgroup)
2978   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2979   (let ((groups (cdr gnus-newsrc-alist))
2980         before)
2981     (while (and (not before) groups)
2982       (if (string< newgroup (caar groups))
2983           (setq before (caar groups))
2984         (setq groups (cdr groups))))
2985     (gnus-subscribe-newsgroup newgroup before)))
2986
2987 (defun gnus-subscribe-hierarchically (newgroup)
2988   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2989   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2990   (save-excursion
2991     (set-buffer (find-file-noselect gnus-current-startup-file))
2992     (let ((groupkey newgroup)
2993           before)
2994       (while (and (not before) groupkey)
2995         (goto-char (point-min))
2996         (let ((groupkey-re
2997                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2998           (while (and (re-search-forward groupkey-re nil t)
2999                       (progn
3000                         (setq before (match-string 1))
3001                         (string< before newgroup)))))
3002         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3003         (setq groupkey
3004               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3005                   (substring groupkey (match-beginning 1) (match-end 1)))))
3006       (gnus-subscribe-newsgroup newgroup before))))
3007
3008 (defun gnus-subscribe-interactively (group)
3009   "Subscribe the new GROUP interactively.
3010 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3011 it is killed."
3012   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3013       (gnus-subscribe-hierarchically group)
3014     (push group gnus-killed-list)))
3015
3016 (defun gnus-subscribe-zombies (group)
3017   "Make the new GROUP into a zombie group."
3018   (push group gnus-zombie-list))
3019
3020 (defun gnus-subscribe-killed (group)
3021   "Make the new GROUP a killed group."
3022   (push group gnus-killed-list))
3023
3024 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3025   "Subscribe new NEWSGROUP.
3026 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3027 the first newsgroup."
3028   ;; We subscribe the group by changing its level to `subscribed'.
3029   (gnus-group-change-level
3030    newsgroup gnus-level-default-subscribed
3031    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3032   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3033
3034 ;; For directories
3035
3036 (defun gnus-newsgroup-directory-form (newsgroup)
3037   "Make hierarchical directory name from NEWSGROUP name."
3038   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3039         (len (length newsgroup))
3040         idx)
3041     ;; If this is a foreign group, we don't want to translate the
3042     ;; entire name.
3043     (if (setq idx (string-match ":" newsgroup))
3044         (aset newsgroup idx ?/)
3045       (setq idx 0))
3046     ;; Replace all occurrences of `.' with `/'.
3047     (while (< idx len)
3048       (if (= (aref newsgroup idx) ?.)
3049           (aset newsgroup idx ?/))
3050       (setq idx (1+ idx)))
3051     newsgroup))
3052
3053 (defun gnus-newsgroup-savable-name (group)
3054   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3055   ;; with dots.
3056   (nnheader-replace-chars-in-string group ?/ ?.))
3057
3058 (defun gnus-make-directory (dir)
3059   "Make DIRECTORY recursively."
3060   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3061   ;; of the many mysteries of the universe.
3062   (let* ((dir (expand-file-name dir default-directory))
3063          dirs err)
3064     (if (string-match "/$" dir)
3065         (setq dir (substring dir 0 (match-beginning 0))))
3066     ;; First go down the path until we find a directory that exists.
3067     (while (not (file-exists-p dir))
3068       (setq dirs (cons dir dirs))
3069       (string-match "/[^/]+$" dir)
3070       (setq dir (substring dir 0 (match-beginning 0))))
3071     ;; Then create all the subdirs.
3072     (while (and dirs (not err))
3073       (condition-case ()
3074           (make-directory (car dirs))
3075         (error (setq err t)))
3076       (setq dirs (cdr dirs)))
3077     ;; We return whether we were successful or not.
3078     (not dirs)))
3079
3080 (defun gnus-capitalize-newsgroup (newsgroup)
3081   "Capitalize NEWSGROUP name."
3082   (and (not (zerop (length newsgroup)))
3083        (concat (char-to-string (upcase (aref newsgroup 0)))
3084                (substring newsgroup 1))))
3085
3086 ;; Various... things.
3087
3088 (defun gnus-simplify-subject (subject &optional re-only)
3089   "Remove `Re:' and words in parentheses.
3090 If RE-ONLY is non-nil, strip leading `Re:'s only."
3091   (let ((case-fold-search t))           ;Ignore case.
3092     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3093     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3094       (setq subject (substring subject (match-end 0))))
3095     ;; Remove uninteresting prefixes.
3096     (if (and (not re-only)
3097              gnus-simplify-ignored-prefixes
3098              (string-match gnus-simplify-ignored-prefixes subject))
3099         (setq subject (substring subject (match-end 0))))
3100     ;; Remove words in parentheses from end.
3101     (unless re-only
3102       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3103         (setq subject (substring subject 0 (match-beginning 0)))))
3104     ;; Return subject string.
3105     subject))
3106
3107 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3108 ;; all whitespace.
3109 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3110 (defun gnus-simplify-buffer-fuzzy ()
3111   (goto-char (point-min))
3112   (while (search-forward "\t" nil t)
3113     (replace-match " " t t))
3114   (goto-char (point-min))
3115   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3116   (goto-char (match-beginning 0))
3117   (while (or
3118           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3119           (looking-at "^[[].*: .*[]]$"))
3120     (goto-char (point-min))
3121     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3122                               nil t)
3123       (replace-match "" t t))
3124     (goto-char (point-min))
3125     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3126       (goto-char (match-end 0))
3127       (delete-char -1)
3128       (delete-region
3129        (progn (goto-char (match-beginning 0)))
3130        (re-search-forward ":"))))
3131   (goto-char (point-min))
3132   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3133     (replace-match "" t t))
3134   (goto-char (point-min))
3135   (while (re-search-forward "  +" nil t)
3136     (replace-match " " t t))
3137   (goto-char (point-min))
3138   (while (re-search-forward " $" nil t)
3139     (replace-match "" t t))
3140   (goto-char (point-min))
3141   (while (re-search-forward "^ +" nil t)
3142     (replace-match "" t t))
3143   (goto-char (point-min))
3144   (when gnus-simplify-subject-fuzzy-regexp
3145     (if (listp gnus-simplify-subject-fuzzy-regexp)
3146         (let ((list gnus-simplify-subject-fuzzy-regexp))
3147           (while list
3148             (goto-char (point-min))
3149             (while (re-search-forward (car list) nil t)
3150               (replace-match "" t t))
3151             (setq list (cdr list))))
3152       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3153         (replace-match "" t t)))))
3154
3155 (defun gnus-simplify-subject-fuzzy (subject)
3156   "Siplify a subject string fuzzily."
3157   (save-excursion
3158     (gnus-set-work-buffer)
3159     (let ((case-fold-search t))
3160       (insert subject)
3161       (inline (gnus-simplify-buffer-fuzzy))
3162       (buffer-string))))
3163
3164 ;; Add the current buffer to the list of buffers to be killed on exit.
3165 (defun gnus-add-current-to-buffer-list ()
3166   (or (memq (current-buffer) gnus-buffer-list)
3167       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3168
3169 (defun gnus-string> (s1 s2)
3170   (not (or (string< s1 s2)
3171            (string= s1 s2))))
3172
3173 (defun gnus-read-active-file-p ()
3174   "Say whether the active file has been read from `gnus-select-method'."
3175   (memq gnus-select-method gnus-have-read-active-file))
3176
3177 ;;; General various misc type functions.
3178
3179 (defun gnus-clear-system ()
3180   "Clear all variables and buffers."
3181   ;; Clear Gnus variables.
3182   (let ((variables gnus-variable-list))
3183     (while variables
3184       (set (car variables) nil)
3185       (setq variables (cdr variables))))
3186   ;; Clear other internal variables.
3187   (setq gnus-list-of-killed-groups nil
3188         gnus-have-read-active-file nil
3189         gnus-newsrc-alist nil
3190         gnus-newsrc-hashtb nil
3191         gnus-killed-list nil
3192         gnus-zombie-list nil
3193         gnus-killed-hashtb nil
3194         gnus-active-hashtb nil
3195         gnus-moderated-list nil
3196         gnus-description-hashtb nil
3197         gnus-current-headers nil
3198         gnus-thread-indent-array nil
3199         gnus-newsgroup-headers nil
3200         gnus-newsgroup-name nil
3201         gnus-server-alist nil
3202         gnus-group-list-mode nil
3203         gnus-opened-servers nil
3204         gnus-current-select-method nil)
3205   (gnus-shutdown 'gnus)
3206   ;; Kill the startup file.
3207   (and gnus-current-startup-file
3208        (get-file-buffer gnus-current-startup-file)
3209        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3210   ;; Clear the dribble buffer.
3211   (gnus-dribble-clear)
3212   ;; Kill global KILL file buffer.
3213   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3214     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3215   (gnus-kill-buffer nntp-server-buffer)
3216   ;; Kill Gnus buffers.
3217   (while gnus-buffer-list
3218     (gnus-kill-buffer (pop gnus-buffer-list)))
3219   ;; Remove Gnus frames.
3220   (while gnus-created-frames
3221     (when (frame-live-p (car gnus-created-frames))
3222       ;; We slap a condition-case around this `delete-frame' to ensure 
3223       ;; agains errors if we try do delete the single frame that's left.
3224       (condition-case ()
3225           (delete-frame (car gnus-created-frames))
3226         (error nil)))
3227     (pop gnus-created-frames)))
3228
3229 (defun gnus-windows-old-to-new (setting)
3230   ;; First we take care of the really, really old Gnus 3 actions.
3231   (when (symbolp setting)
3232     (setq setting
3233           ;; Take care of ooold GNUS 3.x values.
3234           (cond ((eq setting 'SelectArticle) 'article)
3235                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3236                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3237                 (t setting))))
3238   (if (or (listp setting)
3239           (not (and gnus-window-configuration
3240                     (memq setting '(group summary article)))))
3241       setting
3242     (let* ((setting (if (eq setting 'group)
3243                         (if (assq 'newsgroup gnus-window-configuration)
3244                             'newsgroup
3245                           'newsgroups) setting))
3246            (elem (cadr (assq setting gnus-window-configuration)))
3247            (total (apply '+ elem))
3248            (types '(group summary article))
3249            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3250            (i 0)
3251            perc
3252            out)
3253       (while (< i 3)
3254         (or (not (numberp (nth i elem)))
3255             (zerop (nth i elem))
3256             (progn
3257               (setq perc  (/ (float (nth 0 elem)) total))
3258               (setq out (cons (if (eq pbuf (nth i types))
3259                                   (vector (nth i types) perc 'point)
3260                                 (vector (nth i types) perc))
3261                               out))))
3262         (setq i (1+ i)))
3263       (list (nreverse out)))))
3264
3265 ;;;###autoload
3266 (defun gnus-add-configuration (conf)
3267   "Add the window configuration CONF to `gnus-buffer-configuration'."
3268   (setq gnus-buffer-configuration
3269         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3270                          gnus-buffer-configuration))))
3271
3272 (defvar gnus-frame-list nil)
3273
3274 (defun gnus-configure-frame (split &optional window)
3275   "Split WINDOW according to SPLIT."
3276   (unless window
3277     (setq window (get-buffer-window (current-buffer))))
3278   (select-window window)
3279   ;; This might be an old-stylee buffer config.
3280   (when (vectorp split)
3281     (setq split (append split nil)))
3282   (when (or (consp (car split))
3283             (vectorp (car split)))
3284     (push 1.0 split)
3285     (push 'vertical split))
3286   ;; The SPLIT might be something that is to be evaled to
3287   ;; return a new SPLIT.
3288   (while (and (not (assq (car split) gnus-window-to-buffer))
3289               (gnus-functionp (car split)))
3290     (setq split (eval split)))
3291   (let* ((type (car split))
3292          (subs (cddr split))
3293          (len (if (eq type 'horizontal) (window-width) (window-height)))
3294          (total 0)
3295          (window-min-width (or gnus-window-min-width window-min-width))
3296          (window-min-height (or gnus-window-min-height window-min-height))
3297          s result new-win rest comp-subs size sub)
3298     (cond
3299      ;; Nothing to do here.
3300      ((null split))
3301      ;; Don't switch buffers.
3302      ((null type)
3303       (and (memq 'point split) window))
3304      ;; This is a buffer to be selected.
3305      ((not (memq type '(frame horizontal vertical)))
3306       (let ((buffer (cond ((stringp type) type)
3307                           (t (cdr (assq type gnus-window-to-buffer)))))
3308             buf)
3309         (unless buffer
3310           (error "Illegal buffer type: %s" type))
3311         (unless (setq buf (get-buffer (if (symbolp buffer)
3312                                           (symbol-value buffer) buffer)))
3313           (setq buf (get-buffer-create (if (symbolp buffer)
3314                                            (symbol-value buffer) buffer))))
3315         (switch-to-buffer buf)
3316         ;; We return the window if it has the `point' spec.
3317         (and (memq 'point split) window)))
3318      ;; This is a frame split.
3319      ((eq type 'frame)
3320       (unless gnus-frame-list
3321         (setq gnus-frame-list (list (window-frame
3322                                      (get-buffer-window (current-buffer))))))
3323       (let ((i 0)
3324             params frame fresult)
3325         (while (< i (length subs))
3326           ;; Frame parameter is gotten from the sub-split.
3327           (setq params (cadr (elt subs i)))
3328           ;; It should be a list.
3329           (unless (listp params)
3330             (setq params nil))
3331           ;; Create a new frame?
3332           (unless (setq frame (elt gnus-frame-list i))
3333             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3334             (push frame gnus-created-frames))
3335           ;; Is the old frame still alive?
3336           (unless (frame-live-p frame)
3337             (setcar (nthcdr i gnus-frame-list)
3338                     (setq frame (make-frame params))))
3339           ;; Select the frame in question and do more splits there.
3340           (select-frame frame)
3341           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3342           (incf i))
3343         ;; Select the frame that has the selected buffer.
3344         (when fresult
3345           (select-frame (window-frame fresult)))))
3346      ;; This is a normal split.
3347      (t
3348       (when (> (length subs) 0)
3349         ;; First we have to compute the sizes of all new windows.
3350         (while subs
3351           (setq sub (append (pop subs) nil))
3352           (while (and (not (assq (car sub) gnus-window-to-buffer))
3353                       (gnus-functionp (car sub)))
3354             (setq sub (eval sub)))
3355           (when sub
3356             (push sub comp-subs)
3357             (setq size (cadar comp-subs))
3358             (cond ((equal size 1.0)
3359                    (setq rest (car comp-subs))
3360                    (setq s 0))
3361                   ((floatp size)
3362                    (setq s (floor (* size len))))
3363                   ((integerp size)
3364                    (setq s size))
3365                   (t
3366                    (error "Illegal size: %s" size)))
3367             ;; Try to make sure that we are inside the safe limits.
3368             (cond ((zerop s))
3369                   ((eq type 'horizontal)
3370                    (setq s (max s window-min-width)))
3371                   ((eq type 'vertical)
3372                    (setq s (max s window-min-height))))
3373             (setcar (cdar comp-subs) s)
3374             (incf total s)))
3375         ;; Take care of the "1.0" spec.
3376         (if rest
3377             (setcar (cdr rest) (- len total))
3378           (error "No 1.0 specs in %s" split))
3379         ;; The we do the actual splitting in a nice recursive
3380         ;; fashion.
3381         (setq comp-subs (nreverse comp-subs))
3382         (while comp-subs
3383           (if (null (cdr comp-subs))
3384               (setq new-win window)
3385             (setq new-win
3386                   (split-window window (cadar comp-subs)
3387                                 (eq type 'horizontal))))
3388           (setq result (or (gnus-configure-frame
3389                             (car comp-subs) window) result))
3390           (select-window new-win)
3391           (setq window new-win)
3392           (setq comp-subs (cdr comp-subs))))
3393       ;; Return the proper window, if any.
3394       (when result
3395         (select-window result))))))
3396
3397 (defvar gnus-frame-split-p nil)
3398
3399 (defun gnus-configure-windows (setting &optional force)
3400   (setq setting (gnus-windows-old-to-new setting))
3401   (let ((split (if (symbolp setting)
3402                    (cadr (assq setting gnus-buffer-configuration))
3403                  setting))
3404         all-visible)
3405
3406     (setq gnus-frame-split-p nil)
3407
3408     (unless split
3409       (error "No such setting: %s" setting))
3410
3411     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3412              (not force))
3413         ;; All the windows mentioned are already visible, so we just
3414         ;; put point in the assigned buffer, and do not touch the
3415         ;; winconf.
3416         (select-window all-visible)
3417
3418       ;; Either remove all windows or just remove all Gnus windows.
3419       (let ((frame (selected-frame)))
3420         (unwind-protect
3421             (if gnus-use-full-window
3422                 ;; We want to remove all other windows.
3423                 (if (not gnus-frame-split-p)
3424                     ;; This is not a `frame' split, so we ignore the
3425                     ;; other frames.  
3426                     (delete-other-windows)
3427                   ;; This is a `frame' split, so we delete all windows
3428                   ;; on all frames.
3429                   (mapcar 
3430                    (lambda (frame)
3431                      (unless (eq (cdr (assq 'minibuffer
3432                                             (frame-parameters frame)))
3433                                  'only)
3434                        (select-frame frame)
3435                        (delete-other-windows)))
3436                    (frame-list)))
3437               ;; Just remove some windows.
3438               (gnus-remove-some-windows)
3439               (switch-to-buffer nntp-server-buffer))
3440           (select-frame frame)))
3441
3442       (switch-to-buffer nntp-server-buffer)
3443       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3444
3445 (defun gnus-all-windows-visible-p (split)
3446   "Say whether all buffers in SPLIT are currently visible.
3447 In particular, the value returned will be the window that
3448 should have point."
3449   (let ((stack (list split))
3450         (all-visible t)
3451         type buffer win buf)
3452     (while (and (setq split (pop stack))
3453                 all-visible)
3454       ;; Be backwards compatible.
3455       (when (vectorp split)
3456         (setq split (append split nil)))
3457       (when (or (consp (car split))
3458                 (vectorp (car split)))
3459         (push 1.0 split)
3460         (push 'vertical split))
3461       ;; The SPLIT might be something that is to be evaled to
3462       ;; return a new SPLIT.
3463       (while (and (not (assq (car split) gnus-window-to-buffer))
3464                   (gnus-functionp (car split)))
3465         (setq split (eval split)))
3466
3467       (setq type (elt split 0))
3468       (cond
3469        ;; Nothing here.
3470        ((null split) t)
3471        ;; A buffer.
3472        ((not (memq type '(horizontal vertical frame)))
3473         (setq buffer (cond ((stringp type) type)
3474                            (t (cdr (assq type gnus-window-to-buffer)))))
3475         (unless buffer
3476           (error "Illegal buffer type: %s" type))
3477         (when (setq buf (get-buffer (if (symbolp buffer)
3478                                         (symbol-value buffer)
3479                                       buffer)))
3480           (setq win (get-buffer-window buf t)))
3481         (if win
3482             (when (memq 'point split)
3483                 (setq all-visible win))
3484           (setq all-visible nil)))
3485        (t
3486         (when (eq type 'frame)
3487           (setq gnus-frame-split-p t))
3488         (setq stack (append (cddr split) stack)))))
3489     (unless (eq all-visible t)
3490       all-visible)))
3491
3492 (defun gnus-window-top-edge (&optional window)
3493   (nth 1 (window-edges window)))
3494
3495 (defun gnus-remove-some-windows ()
3496   (let ((buffers gnus-window-to-buffer)
3497         buf bufs lowest-buf lowest)
3498     (save-excursion
3499       ;; Remove windows on all known Gnus buffers.
3500       (while buffers
3501         (setq buf (cdar buffers))
3502         (if (symbolp buf)
3503             (setq buf (and (boundp buf) (symbol-value buf))))
3504         (and buf
3505              (get-buffer-window buf)
3506              (progn
3507                (setq bufs (cons buf bufs))
3508                (pop-to-buffer buf)
3509                (if (or (not lowest)
3510                        (< (gnus-window-top-edge) lowest))
3511                    (progn
3512                      (setq lowest (gnus-window-top-edge))
3513                      (setq lowest-buf buf)))))
3514         (setq buffers (cdr buffers)))
3515       ;; Remove windows on *all* summary buffers.
3516       (walk-windows
3517        (lambda (win)
3518          (let ((buf (window-buffer win)))
3519            (if (string-match    "^\\*Summary" (buffer-name buf))
3520                (progn
3521                  (setq bufs (cons buf bufs))
3522                  (pop-to-buffer buf)
3523                  (if (or (not lowest)
3524                          (< (gnus-window-top-edge) lowest))
3525                      (progn
3526                        (setq lowest-buf buf)
3527                        (setq lowest (gnus-window-top-edge)))))))))
3528       (and lowest-buf
3529            (progn
3530              (pop-to-buffer lowest-buf)
3531              (switch-to-buffer nntp-server-buffer)))
3532       (while bufs
3533         (and (not (eq (car bufs) lowest-buf))
3534              (delete-windows-on (car bufs)))
3535         (setq bufs (cdr bufs))))))
3536
3537 (defun gnus-version (&optional arg)
3538   "Version number of this version of Gnus.
3539 If ARG, insert string at point."
3540   (interactive "P")
3541   (let ((methods gnus-valid-select-methods)
3542         (mess gnus-version)
3543         meth)
3544     ;; Go through all the legal select methods and add their version
3545     ;; numbers to the total version string.  Only the backends that are
3546     ;; currently in use will have their message numbers taken into
3547     ;; consideration.
3548     (while methods
3549       (setq meth (intern (concat (caar methods) "-version")))
3550       (and (boundp meth)
3551            (stringp (symbol-value meth))
3552            (setq mess (concat mess "; " (symbol-value meth))))
3553       (setq methods (cdr methods)))
3554     (if arg
3555         (insert (message mess))
3556       (message mess))))
3557
3558 (defun gnus-info-find-node ()
3559   "Find Info documentation of Gnus."
3560   (interactive)
3561   ;; Enlarge info window if needed.
3562   (let ((mode major-mode)
3563         gnus-info-buffer)
3564     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3565     (setq gnus-info-buffer (current-buffer))
3566     (gnus-configure-windows 'info)))
3567
3568 (defun gnus-days-between (date1 date2)
3569   ;; Return the number of days between date1 and date2.
3570   (- (gnus-day-number date1) (gnus-day-number date2)))
3571
3572 (defun gnus-day-number (date)
3573   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3574                      (timezone-parse-date date))))
3575     (timezone-absolute-from-gregorian
3576      (nth 1 dat) (nth 2 dat) (car dat))))
3577
3578 (defun gnus-encode-date (date)
3579   "Convert DATE to internal time."
3580   (let* ((parse (timezone-parse-date date))
3581          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3582          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3583     (encode-time (caddr time) (cadr time) (car time)
3584                  (caddr date) (cadr date) (car date) (nth 4 date))))
3585
3586 (defun gnus-time-minus (t1 t2)
3587   "Subtract two internal times."
3588   (let ((borrow (< (cadr t1) (cadr t2))))
3589     (list (- (car t1) (car t2) (if borrow 1 0))
3590           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3591
3592 (defun gnus-file-newer-than (file date)
3593   (let ((fdate (nth 5 (file-attributes file))))
3594     (or (> (car fdate) (car date))
3595         (and (= (car fdate) (car date))
3596              (> (nth 1 fdate) (nth 1 date))))))
3597
3598 (defmacro gnus-local-set-keys (&rest plist)
3599   "Set the keys in PLIST in the current keymap."
3600   `(gnus-define-keys-1 (current-local-map) ',plist))
3601
3602 (defmacro gnus-define-keys (keymap &rest plist)
3603   "Define all keys in PLIST in KEYMAP."
3604   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3605
3606 (put 'gnus-define-keys 'lisp-indent-function 1)
3607 (put 'gnus-define-keys 'lisp-indent-hook 1)
3608 (put 'gnus-define-keymap 'lisp-indent-function 1)
3609 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3610
3611 (defmacro gnus-define-keymap (keymap &rest plist)
3612   "Define all keys in PLIST in KEYMAP."
3613   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3614
3615 (defun gnus-define-keys-1 (keymap plist)
3616   (when (null keymap)
3617     (error "Can't set keys in a null keymap"))
3618   (cond ((symbolp keymap)
3619          (setq keymap (symbol-value keymap)))
3620         ((keymapp keymap))
3621         ((listp keymap)
3622          (set (car keymap) nil)
3623          (define-prefix-command (car keymap))
3624          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3625          (setq keymap (symbol-value (car keymap)))))
3626   (let (key)
3627     (while plist
3628       (when (symbolp (setq key (pop plist)))
3629         (setq key (symbol-value key)))
3630       (define-key keymap key (pop plist)))))
3631
3632 (defun gnus-group-read-only-p (&optional group)
3633   "Check whether GROUP supports editing or not.
3634 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3635 that that variable is buffer-local to the summary buffers."
3636   (let ((group (or group gnus-newsgroup-name)))
3637     (not (gnus-check-backend-function 'request-replace-article group))))
3638
3639 (defun gnus-group-total-expirable-p (group)
3640   "Check whether GROUP is total-expirable or not."
3641   (let ((params (gnus-info-params (gnus-get-info group))))
3642     (or (memq 'total-expire params)
3643         (cdr (assq 'total-expire params)) ; (total-expire . t)
3644         (and gnus-total-expirable-newsgroups ; Check var.
3645              (string-match gnus-total-expirable-newsgroups group)))))
3646
3647 (defun gnus-group-auto-expirable-p (group)
3648   "Check whether GROUP is total-expirable or not."
3649   (let ((params (gnus-info-params (gnus-get-info group))))
3650     (or (memq 'auto-expire params)
3651         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3652         (and gnus-auto-expirable-newsgroups ; Check var.
3653              (string-match gnus-auto-expirable-newsgroups group)))))
3654
3655 (defun gnus-virtual-group-p (group)
3656   "Say whether GROUP is virtual or not."
3657   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3658                         gnus-valid-select-methods)))
3659
3660 (defun gnus-news-group-p (group &optional article)
3661   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3662   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3663       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3664            (eq (gnus-request-type group article) 'news))))
3665
3666 (defsubst gnus-simplify-subject-fully (subject)
3667   "Simplify a subject string according to the user's wishes."
3668   (cond
3669    ((null gnus-summary-gather-subject-limit)
3670     (gnus-simplify-subject-re subject))
3671    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3672     (gnus-simplify-subject-fuzzy subject))
3673    ((numberp gnus-summary-gather-subject-limit)
3674     (gnus-limit-string (gnus-simplify-subject-re subject)
3675                        gnus-summary-gather-subject-limit))
3676    (t
3677     subject)))
3678
3679 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3680   "Check whether two subjects are equal.  If optional argument
3681 simple-first is t, first argument is already simplified."
3682   (cond
3683    ((null simple-first)
3684     (equal (gnus-simplify-subject-fully s1)
3685            (gnus-simplify-subject-fully s2)))
3686    (t
3687     (equal s1
3688            (gnus-simplify-subject-fully s2)))))
3689
3690 ;; Returns a list of writable groups.
3691 (defun gnus-writable-groups ()
3692   (let ((alist gnus-newsrc-alist)
3693         groups group)
3694     (while (setq group (car (pop alist)))
3695       (unless (gnus-group-read-only-p group)
3696         (push group groups)))
3697     (nreverse groups)))
3698
3699 (defun gnus-completing-read (default prompt &rest args)
3700   ;; Like `completing-read', except that DEFAULT is the default argument.
3701   (let* ((prompt (if default 
3702                      (concat prompt " (default " default ") ")
3703                    (concat prompt " ")))
3704          (answer (apply 'completing-read prompt args)))
3705     (if (or (null answer) (zerop (length answer)))
3706         default
3707       answer)))
3708
3709 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3710 ;; the echo area.
3711 (defun gnus-y-or-n-p (prompt)
3712   (prog1
3713       (y-or-n-p prompt)
3714     (message "")))
3715
3716 (defun gnus-yes-or-no-p (prompt)
3717   (prog1
3718       (yes-or-no-p prompt)
3719     (message "")))
3720
3721 ;; Check whether to use long file names.
3722 (defun gnus-use-long-file-name (symbol)
3723   ;; The variable has to be set...
3724   (and gnus-use-long-file-name
3725        ;; If it isn't a list, then we return t.
3726        (or (not (listp gnus-use-long-file-name))
3727            ;; If it is a list, and the list contains `symbol', we
3728            ;; return nil.
3729            (not (memq symbol gnus-use-long-file-name)))))
3730
3731 ;; I suspect there's a better way, but I haven't taken the time to do
3732 ;; it yet. -erik selberg@cs.washington.edu
3733 (defun gnus-dd-mmm (messy-date)
3734   "Return a string like DD-MMM from a big messy string"
3735   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3736                    (error nil))))
3737     (if (not datevec)
3738         "??-???"
3739       (format "%2s-%s"
3740               (condition-case ()
3741                   ;; Make sure leading zeroes are stripped.
3742                   (number-to-string (string-to-number (aref datevec 2)))
3743                 (error "??"))
3744               (capitalize
3745                (or (car
3746                     (nth (1- (string-to-number (aref datevec 1)))
3747                          timezone-months-assoc))
3748                    "???"))))))
3749
3750 (defun gnus-mode-string-quote (string)
3751   "Quote all \"%\" in STRING."
3752   (save-excursion
3753     (gnus-set-work-buffer)
3754     (insert string)
3755     (goto-char (point-min))
3756     (while (search-forward "%" nil t)
3757       (insert "%"))
3758     (buffer-string)))
3759
3760 ;; Make a hash table (default and minimum size is 255).
3761 ;; Optional argument HASHSIZE specifies the table size.
3762 (defun gnus-make-hashtable (&optional hashsize)
3763   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3764
3765 ;; Make a number that is suitable for hashing; bigger than MIN and one
3766 ;; less than 2^x.
3767 (defun gnus-create-hash-size (min)
3768   (let ((i 1))
3769     (while (< i min)
3770       (setq i (* 2 i)))
3771     (1- i)))
3772
3773 ;; Show message if message has a lower level than `gnus-verbose'.
3774 ;; Guideline for numbers:
3775 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3776 ;; for things that take a long time, 7 - not very important messages
3777 ;; on stuff, 9 - messages inside loops.
3778 (defun gnus-message (level &rest args)
3779   (if (<= level gnus-verbose)
3780       (apply 'message args)
3781     ;; We have to do this format thingy here even if the result isn't
3782     ;; shown - the return value has to be the same as the return value
3783     ;; from `message'.
3784     (apply 'format args)))
3785
3786 (defun gnus-error (level &rest args)
3787   "Beep an error if `gnus-verbose' is on LEVEL or less."
3788   (when (<= (floor level) gnus-verbose)
3789     (apply 'message args)
3790     (ding)
3791     (let (duration)
3792       (when (and (floatp level)
3793                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3794         (sit-for duration))))
3795   nil)
3796
3797 ;; Generate a unique new group name.
3798 (defun gnus-generate-new-group-name (leaf)
3799   (let ((name leaf)
3800         (num 0))
3801     (while (gnus-gethash name gnus-newsrc-hashtb)
3802       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3803     name))
3804
3805 (defsubst gnus-hide-text (b e props)
3806   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3807   (gnus-add-text-properties b e props)
3808   (when (memq 'intangible props)
3809     (gnus-put-text-property (max (1- b) (point-min))
3810                        b 'intangible (cddr (memq 'intangible props)))))
3811
3812 (defsubst gnus-unhide-text (b e)
3813   "Remove hidden text properties from region between B and E."
3814   (remove-text-properties b e gnus-hidden-properties)
3815   (when (memq 'intangible gnus-hidden-properties)
3816     (gnus-put-text-property (max (1- b) (point-min))
3817                        b 'intangible nil)))
3818
3819 (defun gnus-hide-text-type (b e type)
3820   "Hide text of TYPE between B and E."
3821   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3822
3823 (defun gnus-parent-headers (headers &optional generation)
3824   "Return the headers of the GENERATIONeth parent of HEADERS."
3825   (unless generation 
3826     (setq generation 1))
3827   (let (references parent)
3828     (while (and headers (not (zerop generation)))
3829       (setq references (mail-header-references headers))
3830       (when (and references
3831                  (setq parent (gnus-parent-id references))
3832                  (setq headers (car (gnus-id-to-thread parent))))
3833         (decf generation)))
3834     headers))
3835
3836 (defun gnus-parent-id (references)
3837   "Return the last Message-ID in REFERENCES."
3838   (when (and references
3839              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3840     (substring references (match-beginning 1) (match-end 1))))
3841
3842 (defun gnus-split-references (references)
3843   "Return a list of Message-IDs in REFERENCES."
3844   (let ((beg 0)
3845         ids)
3846     (while (string-match "<[^>]+>" references beg)
3847       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3848             ids))
3849     (nreverse ids)))
3850
3851 (defun gnus-buffer-live-p (buffer)
3852   "Say whether BUFFER is alive or not."
3853   (and buffer
3854        (get-buffer buffer)
3855        (buffer-name (get-buffer buffer))))
3856
3857 (defun gnus-ephemeral-group-p (group)
3858   "Say whether GROUP is ephemeral or not."
3859   (gnus-group-get-parameter group 'quit-config))
3860
3861 (defun gnus-group-quit-config (group)
3862   "Return the quit-config of GROUP."
3863   (gnus-group-get-parameter group 'quit-config))
3864
3865 (defun gnus-simplify-mode-line ()
3866   "Make mode lines a bit simpler."
3867   (setq mode-line-modified "-- ")
3868   (when (listp mode-line-format)
3869     (make-local-variable 'mode-line-format)
3870     (setq mode-line-format (copy-sequence mode-line-format))
3871     (when (equal (nth 3 mode-line-format) "   ")
3872       (setcar (nthcdr 3 mode-line-format) " "))))
3873
3874 ;;; List and range functions
3875
3876 (defun gnus-last-element (list)
3877   "Return last element of LIST."
3878   (while (cdr list)
3879     (setq list (cdr list)))
3880   (car list))
3881
3882 (defun gnus-copy-sequence (list)
3883   "Do a complete, total copy of a list."
3884   (if (and (consp list) (not (consp (cdr list))))
3885       (cons (car list) (cdr list))
3886     (mapcar (lambda (elem) (if (consp elem)
3887                                (if (consp (cdr elem))
3888                                    (gnus-copy-sequence elem)
3889                                  (cons (car elem) (cdr elem)))
3890                              elem))
3891             list)))
3892
3893 (defun gnus-set-difference (list1 list2)
3894   "Return a list of elements of LIST1 that do not appear in LIST2."
3895   (let ((list1 (copy-sequence list1)))
3896     (while list2
3897       (setq list1 (delq (car list2) list1))
3898       (setq list2 (cdr list2)))
3899     list1))
3900
3901 (defun gnus-sorted-complement (list1 list2)
3902   "Return a list of elements of LIST1 that do not appear in LIST2.
3903 Both lists have to be sorted over <."
3904   (let (out)
3905     (if (or (null list1) (null list2))
3906         (or list1 list2)
3907       (while (and list1 list2)
3908         (cond ((= (car list1) (car list2))
3909                (setq list1 (cdr list1)
3910                      list2 (cdr list2)))
3911               ((< (car list1) (car list2))
3912                (setq out (cons (car list1) out))
3913                (setq list1 (cdr list1)))
3914               (t
3915                (setq out (cons (car list2) out))
3916                (setq list2 (cdr list2)))))
3917       (nconc (nreverse out) (or list1 list2)))))
3918
3919 (defun gnus-intersection (list1 list2)
3920   (let ((result nil))
3921     (while list2
3922       (if (memq (car list2) list1)
3923           (setq result (cons (car list2) result)))
3924       (setq list2 (cdr list2)))
3925     result))
3926
3927 (defun gnus-sorted-intersection (list1 list2)
3928   ;; LIST1 and LIST2 have to be sorted over <.
3929   (let (out)
3930     (while (and list1 list2)
3931       (cond ((= (car list1) (car list2))
3932              (setq out (cons (car list1) out)
3933                    list1 (cdr list1)
3934                    list2 (cdr list2)))
3935             ((< (car list1) (car list2))
3936              (setq list1 (cdr list1)))
3937             (t
3938              (setq list2 (cdr list2)))))
3939     (nreverse out)))
3940
3941 (defun gnus-set-sorted-intersection (list1 list2)
3942   ;; LIST1 and LIST2 have to be sorted over <.
3943   ;; This function modifies LIST1.
3944   (let* ((top (cons nil list1))
3945          (prev top))
3946     (while (and list1 list2)
3947       (cond ((= (car list1) (car list2))
3948              (setq prev list1
3949                    list1 (cdr list1)
3950                    list2 (cdr list2)))
3951             ((< (car list1) (car list2))
3952              (setcdr prev (cdr list1))
3953              (setq list1 (cdr list1)))
3954             (t
3955              (setq list2 (cdr list2)))))
3956     (setcdr prev nil)
3957     (cdr top)))
3958
3959 (defun gnus-compress-sequence (numbers &optional always-list)
3960   "Convert list of numbers to a list of ranges or a single range.
3961 If ALWAYS-LIST is non-nil, this function will always release a list of
3962 ranges."
3963   (let* ((first (car numbers))
3964          (last (car numbers))
3965          result)
3966     (if (null numbers)
3967         nil
3968       (if (not (listp (cdr numbers)))
3969           numbers
3970         (while numbers
3971           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3972                 ((= (1+ last) (car numbers)) ;Still in sequence
3973                  (setq last (car numbers)))
3974                 (t                      ;End of one sequence
3975                  (setq result
3976                        (cons (if (= first last) first
3977                                (cons first last)) result))
3978                  (setq first (car numbers))
3979                  (setq last  (car numbers))))
3980           (setq numbers (cdr numbers)))
3981         (if (and (not always-list) (null result))
3982             (if (= first last) (list first) (cons first last))
3983           (nreverse (cons (if (= first last) first (cons first last))
3984                           result)))))))
3985
3986 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3987 (defun gnus-uncompress-range (ranges)
3988   "Expand a list of ranges into a list of numbers.
3989 RANGES is either a single range on the form `(num . num)' or a list of
3990 these ranges."
3991   (let (first last result)
3992     (cond
3993      ((null ranges)
3994       nil)
3995      ((not (listp (cdr ranges)))
3996       (setq first (car ranges))
3997       (setq last (cdr ranges))
3998       (while (<= first last)
3999         (setq result (cons first result))
4000         (setq first (1+ first)))
4001       (nreverse result))
4002      (t
4003       (while ranges
4004         (if (atom (car ranges))
4005             (if (numberp (car ranges))
4006                 (setq result (cons (car ranges) result)))
4007           (setq first (caar ranges))
4008           (setq last  (cdar ranges))
4009           (while (<= first last)
4010             (setq result (cons first result))
4011             (setq first (1+ first))))
4012         (setq ranges (cdr ranges)))
4013       (nreverse result)))))
4014
4015 (defun gnus-add-to-range (ranges list)
4016   "Return a list of ranges that has all articles from both RANGES and LIST.
4017 Note: LIST has to be sorted over `<'."
4018   (if (not ranges)
4019       (gnus-compress-sequence list t)
4020     (setq list (copy-sequence list))
4021     (or (listp (cdr ranges))
4022         (setq ranges (list ranges)))
4023     (let ((out ranges)
4024           ilist lowest highest temp)
4025       (while (and ranges list)
4026         (setq ilist list)
4027         (setq lowest (or (and (atom (car ranges)) (car ranges))
4028                          (caar ranges)))
4029         (while (and list (cdr list) (< (cadr list) lowest))
4030           (setq list (cdr list)))
4031         (if (< (car ilist) lowest)
4032             (progn
4033               (setq temp list)
4034               (setq list (cdr list))
4035               (setcdr temp nil)
4036               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4037         (setq highest (or (and (atom (car ranges)) (car ranges))
4038                           (cdar ranges)))
4039         (while (and list (<= (car list) highest))
4040           (setq list (cdr list)))
4041         (setq ranges (cdr ranges)))
4042       (if list
4043           (setq out (nconc (gnus-compress-sequence list t) out)))
4044       (setq out (sort out (lambda (r1 r2)
4045                             (< (or (and (atom r1) r1) (car r1))
4046                                (or (and (atom r2) r2) (car r2))))))
4047       (setq ranges out)
4048       (while ranges
4049         (if (atom (car ranges))
4050             (if (cdr ranges)
4051                 (if (atom (cadr ranges))
4052                     (if (= (1+ (car ranges)) (cadr ranges))
4053                         (progn
4054                           (setcar ranges (cons (car ranges)
4055                                                (cadr ranges)))
4056                           (setcdr ranges (cddr ranges))))
4057                   (if (= (1+ (car ranges)) (caadr ranges))
4058                       (progn
4059                         (setcar (cadr ranges) (car ranges))
4060                         (setcar ranges (cadr ranges))
4061                         (setcdr ranges (cddr ranges))))))
4062           (if (cdr ranges)
4063               (if (atom (cadr ranges))
4064                   (if (= (1+ (cdar ranges)) (cadr ranges))
4065                       (progn
4066                         (setcdr (car ranges) (cadr ranges))
4067                         (setcdr ranges (cddr ranges))))
4068                 (if (= (1+ (cdar ranges)) (caadr ranges))
4069                     (progn
4070                       (setcdr (car ranges) (cdadr ranges))
4071                       (setcdr ranges (cddr ranges)))))))
4072         (setq ranges (cdr ranges)))
4073       out)))
4074
4075 (defun gnus-remove-from-range (ranges list)
4076   "Return a list of ranges that has all articles from LIST removed from RANGES.
4077 Note: LIST has to be sorted over `<'."
4078   ;; !!! This function shouldn't look like this, but I've got a headache.
4079   (gnus-compress-sequence
4080    (gnus-sorted-complement
4081     (gnus-uncompress-range ranges) list)))
4082
4083 (defun gnus-member-of-range (number ranges)
4084   (if (not (listp (cdr ranges)))
4085       (and (>= number (car ranges))
4086            (<= number (cdr ranges)))
4087     (let ((not-stop t))
4088       (while (and ranges
4089                   (if (numberp (car ranges))
4090                       (>= number (car ranges))
4091                     (>= number (caar ranges)))
4092                   not-stop)
4093         (if (if (numberp (car ranges))
4094                 (= number (car ranges))
4095               (and (>= number (caar ranges))
4096                    (<= number (cdar ranges))))
4097             (setq not-stop nil))
4098         (setq ranges (cdr ranges)))
4099       (not not-stop))))
4100
4101 (defun gnus-range-length (range)
4102   "Return the length RANGE would have if uncompressed."
4103   (length (gnus-uncompress-range range)))
4104
4105 (defun gnus-sublist-p (list sublist)
4106   "Test whether all elements in SUBLIST are members of LIST."
4107   (let ((sublistp t))
4108     (while sublist
4109       (unless (memq (pop sublist) list)
4110         (setq sublistp nil
4111               sublist nil)))
4112     sublistp))
4113
4114 \f
4115 ;;;
4116 ;;; Gnus group mode
4117 ;;;
4118
4119 (defvar gnus-group-mode-map nil)
4120 (put 'gnus-group-mode 'mode-class 'special)
4121
4122 (unless gnus-group-mode-map
4123   (setq gnus-group-mode-map (make-keymap))
4124   (suppress-keymap gnus-group-mode-map)
4125
4126   (gnus-define-keys gnus-group-mode-map
4127     " " gnus-group-read-group
4128     "=" gnus-group-select-group
4129     "\r" gnus-group-select-group
4130     "\M-\r" gnus-group-quick-select-group
4131     "j" gnus-group-jump-to-group
4132     "n" gnus-group-next-unread-group
4133     "p" gnus-group-prev-unread-group
4134     "\177" gnus-group-prev-unread-group
4135     [delete] gnus-group-prev-unread-group
4136     "N" gnus-group-next-group
4137     "P" gnus-group-prev-group
4138     "\M-n" gnus-group-next-unread-group-same-level
4139     "\M-p" gnus-group-prev-unread-group-same-level
4140     "," gnus-group-best-unread-group
4141     "." gnus-group-first-unread-group
4142     "u" gnus-group-unsubscribe-current-group
4143     "U" gnus-group-unsubscribe-group
4144     "c" gnus-group-catchup-current
4145     "C" gnus-group-catchup-current-all
4146     "l" gnus-group-list-groups
4147     "L" gnus-group-list-all-groups
4148     "m" gnus-group-mail
4149     "g" gnus-group-get-new-news
4150     "\M-g" gnus-group-get-new-news-this-group
4151     "R" gnus-group-restart
4152     "r" gnus-group-read-init-file
4153     "B" gnus-group-browse-foreign-server
4154     "b" gnus-group-check-bogus-groups
4155     "F" gnus-find-new-newsgroups
4156     "\C-c\C-d" gnus-group-describe-group
4157     "\M-d" gnus-group-describe-all-groups
4158     "\C-c\C-a" gnus-group-apropos
4159     "\C-c\M-\C-a" gnus-group-description-apropos
4160     "a" gnus-group-post-news
4161     "\ek" gnus-group-edit-local-kill
4162     "\eK" gnus-group-edit-global-kill
4163     "\C-k" gnus-group-kill-group
4164     "\C-y" gnus-group-yank-group
4165     "\C-w" gnus-group-kill-region
4166     "\C-x\C-t" gnus-group-transpose-groups
4167     "\C-c\C-l" gnus-group-list-killed
4168     "\C-c\C-x" gnus-group-expire-articles
4169     "\C-c\M-\C-x" gnus-group-expire-all-groups
4170     "V" gnus-version
4171     "s" gnus-group-save-newsrc
4172     "z" gnus-group-suspend
4173     "Z" gnus-group-clear-dribble
4174     "q" gnus-group-exit
4175     "Q" gnus-group-quit
4176     "?" gnus-group-describe-briefly
4177     "\C-c\C-i" gnus-info-find-node
4178     "\M-e" gnus-group-edit-group-method
4179     "^" gnus-group-enter-server-mode
4180     gnus-mouse-2 gnus-mouse-pick-group
4181     "<" beginning-of-buffer
4182     ">" end-of-buffer
4183     "\C-c\C-b" gnus-bug
4184     "\C-c\C-s" gnus-group-sort-groups
4185     "t" gnus-topic-mode
4186     "\C-c\M-g" gnus-activate-all-groups
4187     "\M-&" gnus-group-universal-argument
4188     "#" gnus-group-mark-group
4189     "\M-#" gnus-group-unmark-group)
4190
4191   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4192     "m" gnus-group-mark-group
4193     "u" gnus-group-unmark-group
4194     "w" gnus-group-mark-region
4195     "m" gnus-group-mark-buffer
4196     "r" gnus-group-mark-regexp
4197     "U" gnus-group-unmark-all-groups)
4198
4199   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4200     "d" gnus-group-make-directory-group
4201     "h" gnus-group-make-help-group
4202     "a" gnus-group-make-archive-group
4203     "k" gnus-group-make-kiboze-group
4204     "m" gnus-group-make-group
4205     "E" gnus-group-edit-group
4206     "e" gnus-group-edit-group-method
4207     "p" gnus-group-edit-group-parameters
4208     "v" gnus-group-add-to-virtual
4209     "V" gnus-group-make-empty-virtual
4210     "D" gnus-group-enter-directory
4211     "f" gnus-group-make-doc-group
4212     "r" gnus-group-rename-group
4213     "\177" gnus-group-delete-group
4214     [delete] gnus-group-delete-group)
4215
4216    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4217      "b" gnus-group-brew-soup
4218      "w" gnus-soup-save-areas
4219      "s" gnus-soup-send-replies
4220      "p" gnus-soup-pack-packet
4221      "r" nnsoup-pack-replies)
4222
4223    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4224      "s" gnus-group-sort-groups
4225      "a" gnus-group-sort-groups-by-alphabet
4226      "u" gnus-group-sort-groups-by-unread
4227      "l" gnus-group-sort-groups-by-level
4228      "v" gnus-group-sort-groups-by-score
4229      "r" gnus-group-sort-groups-by-rank
4230      "m" gnus-group-sort-groups-by-method)
4231
4232    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4233      "k" gnus-group-list-killed
4234      "z" gnus-group-list-zombies
4235      "s" gnus-group-list-groups
4236      "u" gnus-group-list-all-groups
4237      "A" gnus-group-list-active
4238      "a" gnus-group-apropos
4239      "d" gnus-group-description-apropos
4240      "m" gnus-group-list-matching
4241      "M" gnus-group-list-all-matching
4242      "l" gnus-group-list-level)
4243
4244    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4245      "f" gnus-score-flush-cache)
4246
4247    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4248      "f" gnus-group-fetch-faq)
4249
4250    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4251      "l" gnus-group-set-current-level
4252      "t" gnus-group-unsubscribe-current-group
4253      "s" gnus-group-unsubscribe-group
4254      "k" gnus-group-kill-group
4255      "y" gnus-group-yank-group
4256      "w" gnus-group-kill-region
4257      "\C-k" gnus-group-kill-level
4258      "z" gnus-group-kill-all-zombies))
4259
4260 (defun gnus-group-mode ()
4261   "Major mode for reading news.
4262
4263 All normal editing commands are switched off.
4264 \\<gnus-group-mode-map>
4265 The group buffer lists (some of) the groups available.  For instance,
4266 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4267 lists all zombie groups.
4268
4269 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4270 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4271
4272 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4273
4274 The following commands are available:
4275
4276 \\{gnus-group-mode-map}"
4277   (interactive)
4278   (when (and menu-bar-mode
4279              (gnus-visual-p 'group-menu 'menu))
4280     (gnus-group-make-menu-bar))
4281   (kill-all-local-variables)
4282   (gnus-simplify-mode-line)
4283   (setq major-mode 'gnus-group-mode)
4284   (setq mode-name "Group")
4285   (gnus-group-set-mode-line)
4286   (setq mode-line-process nil)
4287   (use-local-map gnus-group-mode-map)
4288   (buffer-disable-undo (current-buffer))
4289   (setq truncate-lines t)
4290   (setq buffer-read-only t)
4291   (gnus-make-local-hook 'post-command-hook)
4292   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4293   (run-hooks 'gnus-group-mode-hook))
4294
4295 (defun gnus-clear-inboxes-moved ()
4296   (setq nnmail-moved-inboxes nil))
4297
4298 (defun gnus-mouse-pick-group (e)
4299   "Enter the group under the mouse pointer."
4300   (interactive "e")
4301   (mouse-set-point e)
4302   (gnus-group-read-group nil))
4303
4304 ;; Look at LEVEL and find out what the level is really supposed to be.
4305 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4306 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4307 (defun gnus-group-default-level (&optional level number-or-nil)
4308   (cond
4309    (gnus-group-use-permanent-levels
4310     (or (setq gnus-group-use-permanent-levels
4311               (or level (if (numberp gnus-group-use-permanent-levels)
4312                             gnus-group-use-permanent-levels
4313                           (or gnus-group-default-list-level
4314                               gnus-level-subscribed))))
4315         gnus-group-default-list-level gnus-level-subscribed))
4316    (number-or-nil
4317     level)
4318    (t
4319     (or level gnus-group-default-list-level gnus-level-subscribed))))
4320
4321 ;;;###autoload
4322 (defun gnus-slave-no-server (&optional arg)
4323   "Read network news as a slave, without connecting to local server"
4324   (interactive "P")
4325   (gnus-no-server arg t))
4326
4327 ;;;###autoload
4328 (defun gnus-no-server (&optional arg slave)
4329   "Read network news.
4330 If ARG is a positive number, Gnus will use that as the
4331 startup level.  If ARG is nil, Gnus will be started at level 2.
4332 If ARG is non-nil and not a positive number, Gnus will
4333 prompt the user for the name of an NNTP server to use.
4334 As opposed to `gnus', this command will not connect to the local server."
4335   (interactive "P")
4336   (let ((val (or arg (1- gnus-level-default-subscribed))))
4337     (gnus val t slave)
4338     (make-local-variable 'gnus-group-use-permanent-levels)
4339     (setq gnus-group-use-permanent-levels val)))
4340
4341 ;;;###autoload
4342 (defun gnus-slave (&optional arg)
4343   "Read news as a slave."
4344   (interactive "P")
4345   (gnus arg nil 'slave))
4346
4347 ;;;###autoload
4348 (defun gnus-other-frame (&optional arg)
4349   "Pop up a frame to read news."
4350   (interactive "P")
4351   (if (get-buffer gnus-group-buffer)
4352       (let ((pop-up-frames t))
4353         (gnus arg))
4354     (select-frame (make-frame))
4355     (gnus arg)))
4356
4357 ;;;###autoload
4358 (defun gnus (&optional arg dont-connect slave)
4359   "Read network news.
4360 If ARG is non-nil and a positive number, Gnus will use that as the
4361 startup level.  If ARG is non-nil and not a positive number, Gnus will
4362 prompt the user for the name of an NNTP server to use."
4363   (interactive "P")
4364
4365   (if (get-buffer gnus-group-buffer)
4366       (progn
4367         (switch-to-buffer gnus-group-buffer)
4368         (gnus-group-get-new-news))
4369
4370     (gnus-clear-system)
4371     (nnheader-init-server-buffer)
4372     (gnus-read-init-file)
4373     (setq gnus-slave slave)
4374
4375     (gnus-group-setup-buffer)
4376     (let ((buffer-read-only nil))
4377       (erase-buffer)
4378       (if (not gnus-inhibit-startup-message)
4379           (progn
4380             (gnus-group-startup-message)
4381             (sit-for 0))))
4382
4383     (let ((level (and (numberp arg) (> arg 0) arg))
4384           did-connect)
4385       (unwind-protect
4386           (progn
4387             (or dont-connect
4388                 (setq did-connect
4389                       (gnus-start-news-server (and arg (not level))))))
4390         (if (and (not dont-connect)
4391                  (not did-connect))
4392             (gnus-group-quit)
4393           (run-hooks 'gnus-startup-hook)
4394           ;; NNTP server is successfully open.
4395
4396           ;; Find the current startup file name.
4397           (setq gnus-current-startup-file
4398                 (gnus-make-newsrc-file gnus-startup-file))
4399
4400           ;; Read the dribble file.
4401           (when (or gnus-slave gnus-use-dribble-file)
4402             (gnus-dribble-read-file))
4403
4404           ;; Allow using GroupLens predictions.
4405           (when gnus-use-grouplens
4406             (bbb-login)
4407             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4408
4409           (gnus-summary-make-display-table)
4410           ;; Do the actual startup.
4411           (gnus-setup-news nil level dont-connect)
4412           ;; Generate the group buffer.
4413           (gnus-group-list-groups level)
4414           (gnus-group-first-unread-group)
4415           (gnus-configure-windows 'group)
4416           (gnus-group-set-mode-line))))))
4417
4418 (defun gnus-unload ()
4419   "Unload all Gnus features."
4420   (interactive)
4421   (or (boundp 'load-history)
4422       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4423   (let ((history load-history)
4424         feature)
4425     (while history
4426       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4427            (setq feature (cdr (assq 'provide (car history))))
4428            (unload-feature feature 'force))
4429       (setq history (cdr history)))))
4430
4431 (defun gnus-compile ()
4432   "Byte-compile the user-defined format specs."
4433   (interactive)
4434   (let ((entries gnus-format-specs)
4435         entry gnus-tmp-func)
4436     (save-excursion
4437       (gnus-message 7 "Compiling format specs...")
4438
4439       (while entries
4440         (setq entry (pop entries))
4441         (if (eq (car entry) 'version)
4442             (setq gnus-format-specs (delq entry gnus-format-specs))
4443           (when (and (listp (caddr entry))
4444                      (not (eq 'byte-code (caaddr entry))))
4445             (fset 'gnus-tmp-func
4446                   `(lambda () ,(caddr entry)))
4447             (byte-compile 'gnus-tmp-func)
4448             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4449
4450       (push (cons 'version emacs-version) gnus-format-specs)
4451
4452       (gnus-message 7 "Compiling user specs...done"))))
4453
4454 (defun gnus-indent-rigidly (start end arg)
4455   "Indent rigidly using only spaces and no tabs."
4456   (save-excursion
4457     (save-restriction
4458       (narrow-to-region start end)
4459       (indent-rigidly start end arg)
4460       (goto-char (point-min))
4461       (while (search-forward "\t" nil t)
4462         (replace-match "        " t t)))))
4463
4464 (defun gnus-group-startup-message (&optional x y)
4465   "Insert startup message in current buffer."
4466   ;; Insert the message.
4467   (erase-buffer)
4468   (insert
4469    (format "              %s
4470           _    ___ _             _
4471           _ ___ __ ___  __    _ ___
4472           __   _     ___    __  ___
4473               _           ___     _
4474              _  _ __             _
4475              ___   __            _
4476                    __           _
4477                     _      _   _
4478                    _      _    _
4479                       _  _    _
4480                   __  ___
4481                  _   _ _     _
4482                 _   _
4483               _    _
4484              _    _
4485             _
4486           __
4487
4488 "
4489            ""))
4490   ;; And then hack it.
4491   (gnus-indent-rigidly (point-min) (point-max)
4492                        (/ (max (- (window-width) (or x 46)) 0) 2))
4493   (goto-char (point-min))
4494   (forward-line 1)
4495   (let* ((pheight (count-lines (point-min) (point-max)))
4496          (wheight (window-height))
4497          (rest (- wheight pheight)))
4498     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4499   ;; Fontify some.
4500   (goto-char (point-min))
4501   (and (search-forward "Praxis" nil t)
4502        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4503   (goto-char (point-min))
4504   (let* ((mode-string (gnus-group-set-mode-line)))
4505     (setq mode-line-buffer-identification
4506           (list (concat gnus-version (substring (car mode-string) 4))))
4507     (set-buffer-modified-p t)))
4508
4509 (defun gnus-group-setup-buffer ()
4510   (or (get-buffer gnus-group-buffer)
4511       (progn
4512         (switch-to-buffer gnus-group-buffer)
4513         (gnus-add-current-to-buffer-list)
4514         (gnus-group-mode)
4515         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4516
4517 (defun gnus-group-list-groups (&optional level unread lowest)
4518   "List newsgroups with level LEVEL or lower that have unread articles.
4519 Default is all subscribed groups.
4520 If argument UNREAD is non-nil, groups with no unread articles are also
4521 listed."
4522   (interactive (list (if current-prefix-arg
4523                          (prefix-numeric-value current-prefix-arg)
4524                        (or
4525                         (gnus-group-default-level nil t)
4526                         gnus-group-default-list-level
4527                         gnus-level-subscribed))))
4528   (or level
4529       (setq level (car gnus-group-list-mode)
4530             unread (cdr gnus-group-list-mode)))
4531   (setq level (gnus-group-default-level level))
4532   (gnus-group-setup-buffer)             ;May call from out of group buffer
4533   (gnus-update-format-specifications)
4534   (let ((case-fold-search nil)
4535         (props (text-properties-at (gnus-point-at-bol)))
4536         (group (gnus-group-group-name)))
4537     (set-buffer gnus-group-buffer)
4538     (funcall gnus-group-prepare-function level unread lowest)
4539     (if (zerop (buffer-size))
4540         (gnus-message 5 gnus-no-groups-message)
4541       (goto-char (point-max))
4542       (when (or (not gnus-group-goto-next-group-function)
4543                 (not (funcall gnus-group-goto-next-group-function 
4544                               group props)))
4545         (if (not group)
4546             ;; Go to the first group with unread articles.
4547             (gnus-group-search-forward t)
4548           ;; Find the right group to put point on.  If the current group
4549           ;; has disappeared in the new listing, try to find the next
4550           ;; one.        If no next one can be found, just leave point at the
4551           ;; first newsgroup in the buffer.
4552           (if (not (gnus-goto-char
4553                     (text-property-any
4554                      (point-min) (point-max)
4555                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4556               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4557                 (while (and newsrc
4558                             (not (gnus-goto-char
4559                                   (text-property-any
4560                                    (point-min) (point-max) 'gnus-group
4561                                    (gnus-intern-safe
4562                                     (caar newsrc) gnus-active-hashtb)))))
4563                   (setq newsrc (cdr newsrc)))
4564                 (or newsrc (progn (goto-char (point-max))
4565                                   (forward-line -1)))))))
4566       ;; Adjust cursor point.
4567       (gnus-group-position-point))))
4568
4569 (defun gnus-group-list-level (level &optional all)
4570   "List groups on LEVEL.
4571 If ALL (the prefix), also list groups that have no unread articles."
4572   (interactive "nList groups on level: \nP")
4573   (gnus-group-list-groups level all level))
4574
4575 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4576   "List all newsgroups with unread articles of level LEVEL or lower.
4577 If ALL is non-nil, list groups that have no unread articles.
4578 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4579 If REGEXP, only list groups matching REGEXP."
4580   (set-buffer gnus-group-buffer)
4581   (let ((buffer-read-only nil)
4582         (newsrc (cdr gnus-newsrc-alist))
4583         (lowest (or lowest 1))
4584         info clevel unread group params)
4585     (erase-buffer)
4586     (if (< lowest gnus-level-zombie)
4587         ;; List living groups.
4588         (while newsrc
4589           (setq info (car newsrc)
4590                 group (gnus-info-group info)
4591                 params (gnus-info-params info)
4592                 newsrc (cdr newsrc)
4593                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4594           (and unread                   ; This group might be bogus
4595                (or (not regexp)
4596                    (string-match regexp group))
4597                (<= (setq clevel (gnus-info-level info)) level)
4598                (>= clevel lowest)
4599                (or all                  ; We list all groups?
4600                    (if (eq unread t)    ; Unactivated?
4601                        gnus-group-list-inactive-groups ; We list unactivated 
4602                      (> unread 0))      ; We list groups with unread articles
4603                    (and gnus-list-groups-with-ticked-articles
4604                         (cdr (assq 'tick (gnus-info-marks info))))
4605                                         ; And groups with tickeds
4606                    ;; Check for permanent visibility.
4607                    (and gnus-permanently-visible-groups
4608                         (string-match gnus-permanently-visible-groups
4609                                       group))
4610                    (memq 'visible params)
4611                    (cdr (assq 'visible params)))
4612                (gnus-group-insert-group-line
4613                 group (gnus-info-level info)
4614                 (gnus-info-marks info) unread (gnus-info-method info)))))
4615
4616     ;; List dead groups.
4617     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4618          (gnus-group-prepare-flat-list-dead
4619           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4620           gnus-level-zombie ?Z
4621           regexp))
4622     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4623          (gnus-group-prepare-flat-list-dead
4624           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4625           gnus-level-killed ?K regexp))
4626
4627     (gnus-group-set-mode-line)
4628     (setq gnus-group-list-mode (cons level all))
4629     (run-hooks 'gnus-group-prepare-hook)))
4630
4631 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4632   ;; List zombies and killed lists somewhat faster, which was
4633   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4634   ;; this by ignoring the group format specification altogether.
4635   (let (group)
4636     (if regexp
4637         ;; This loop is used when listing groups that match some
4638         ;; regexp.
4639         (while groups
4640           (setq group (pop groups))
4641           (when (string-match regexp group)
4642             (gnus-add-text-properties
4643              (point) (prog1 (1+ (point))
4644                        (insert " " mark "     *: " group "\n"))
4645              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4646                    'gnus-unread t
4647                    'gnus-level level))))
4648       ;; This loop is used when listing all groups.
4649       (while groups
4650         (gnus-add-text-properties
4651          (point) (prog1 (1+ (point))
4652                    (insert " " mark "     *: "
4653                            (setq group (pop groups)) "\n"))
4654          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4655                'gnus-unread t
4656                'gnus-level level))))))
4657
4658 (defmacro gnus-group-real-name (group)
4659   "Find the real name of a foreign newsgroup."
4660   `(let ((gname ,group))
4661      (if (string-match ":[^:]+$" gname)
4662          (substring gname (1+ (match-beginning 0)))
4663        gname)))
4664
4665 (defsubst gnus-server-add-address (method)
4666   (let ((method-name (symbol-name (car method))))
4667     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4668              (not (assq (intern (concat method-name "-address")) method)))
4669         (append method (list (list (intern (concat method-name "-address"))
4670                                    (nth 1 method))))
4671       method)))
4672
4673 (defsubst gnus-server-get-method (group method)
4674   ;; Input either a server name, and extended server name, or a
4675   ;; select method, and return a select method.
4676   (cond ((stringp method)
4677          (gnus-server-to-method method))
4678         ((equal method gnus-select-method)
4679          gnus-select-method)
4680         ((and (stringp (car method)) group)
4681          (gnus-server-extend-method group method))
4682         ((and method (not group)
4683               (equal (cadr method) ""))
4684          method)
4685         (t
4686          (gnus-server-add-address method))))
4687
4688 (defun gnus-server-to-method (server)
4689   "Map virtual server names to select methods."
4690   (or 
4691    ;; Is this a method, perhaps?
4692    (and server (listp server) server)
4693    ;; Perhaps this is the native server?
4694    (and (equal server "native") gnus-select-method)
4695    ;; It should be in the server alist.
4696    (cdr (assoc server gnus-server-alist))
4697    ;; If not, we look through all the opened server
4698    ;; to see whether we can find it there.
4699    (let ((opened gnus-opened-servers))
4700      (while (and opened
4701                  (not (equal server (format "%s:%s" (caaar opened)
4702                                             (cadaar opened)))))
4703        (pop opened))
4704      (caar opened))))
4705
4706 (defmacro gnus-method-equal (ss1 ss2)
4707   "Say whether two servers are equal."
4708   `(let ((s1 ,ss1)
4709          (s2 ,ss2))
4710      (or (equal s1 s2)
4711          (and (= (length s1) (length s2))
4712               (progn
4713                 (while (and s1 (member (car s1) s2))
4714                   (setq s1 (cdr s1)))
4715                 (null s1))))))
4716
4717 (defun gnus-server-equal (m1 m2)
4718   "Say whether two methods are equal."
4719   (let ((m1 (cond ((null m1) gnus-select-method)
4720                   ((stringp m1) (gnus-server-to-method m1))
4721                   (t m1)))
4722         (m2 (cond ((null m2) gnus-select-method)
4723                   ((stringp m2) (gnus-server-to-method m2))
4724                   (t m2))))
4725     (gnus-method-equal m1 m2)))
4726
4727 (defun gnus-servers-using-backend (backend)
4728   "Return a list of known servers using BACKEND."
4729   (let ((opened gnus-opened-servers)
4730         out)
4731     (while opened
4732       (when (eq backend (caaar opened))
4733         (push (caar opened) out))
4734       (pop opened))
4735     out))
4736
4737 (defun gnus-group-prefixed-name (group method)
4738   "Return the whole name from GROUP and METHOD."
4739   (and (stringp method) (setq method (gnus-server-to-method method)))
4740   (concat (format "%s" (car method))
4741           (if (and
4742                (or (assoc (format "%s" (car method)) 
4743                           (gnus-methods-using 'address))
4744                    (gnus-server-equal method gnus-message-archive-method))
4745                (nth 1 method)
4746                (not (string= (nth 1 method) "")))
4747               (concat "+" (nth 1 method)))
4748           ":" group))
4749
4750 (defun gnus-group-real-prefix (group)
4751   "Return the prefix of the current group name."
4752   (if (string-match "^[^:]+:" group)
4753       (substring group 0 (match-end 0))
4754     ""))
4755
4756 (defun gnus-group-method (group)
4757   "Return the server or method used for selecting GROUP."
4758   (let ((prefix (gnus-group-real-prefix group)))
4759     (if (equal prefix "")
4760         gnus-select-method
4761       (let ((servers gnus-opened-servers)
4762             (server "")
4763             backend possible found)
4764         (if (string-match "^[^\\+]+\\+" prefix)
4765             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4766                   server (substring prefix (match-end 0) (1- (length prefix))))
4767           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4768         (while servers
4769           (when (eq (caaar servers) backend)
4770             (setq possible (caar servers))
4771             (when (equal (cadaar servers) server)
4772               (setq found (caar servers))))
4773           (pop servers))
4774         (or (car (rassoc found gnus-server-alist))
4775             found
4776             (car (rassoc possible gnus-server-alist))
4777             possible
4778             (list backend server))))))
4779
4780 (defsubst gnus-secondary-method-p (method)
4781   "Return whether METHOD is a secondary select method."
4782   (let ((methods gnus-secondary-select-methods)
4783         (gmethod (gnus-server-get-method nil method)))
4784     (while (and methods
4785                 (not (equal (gnus-server-get-method nil (car methods))
4786                             gmethod)))
4787       (setq methods (cdr methods)))
4788     methods))
4789
4790 (defun gnus-group-foreign-p (group)
4791   "Say whether a group is foreign or not."
4792   (and (not (gnus-group-native-p group))
4793        (not (gnus-group-secondary-p group))))
4794
4795 (defun gnus-group-native-p (group)
4796   "Say whether the group is native or not."
4797   (not (string-match ":" group)))
4798
4799 (defun gnus-group-secondary-p (group)
4800   "Say whether the group is secondary or not."
4801   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4802
4803 (defun gnus-group-get-parameter (group &optional symbol)
4804   "Returns the group parameters for GROUP.
4805 If SYMBOL, return the value of that symbol in the group parameters."
4806   (let ((params (gnus-info-params (gnus-get-info group))))
4807     (if symbol
4808         (gnus-group-parameter-value params symbol)
4809       params)))
4810
4811 (defun gnus-group-parameter-value (params symbol)
4812   "Return the value of SYMBOL in group PARAMS."
4813   (or (car (memq symbol params))        ; It's either a simple symbol
4814       (cdr (assq symbol params))))      ; or a cons.
4815
4816 (defun gnus-group-add-parameter (group param)
4817   "Add parameter PARAM to GROUP."
4818   (let ((info (gnus-get-info group)))
4819     (if (not info)
4820         () ; This is a dead group.  We just ignore it.
4821       ;; Cons the new param to the old one and update.
4822       (gnus-group-set-info (cons param (gnus-info-params info))
4823                            group 'params))))
4824
4825 (defun gnus-group-set-parameter (group name value)
4826   "Set parameter NAME to VALUE in GROUP."
4827   (let ((info (gnus-get-info group)))
4828     (if (not info)
4829         () ; This is a dead group.  We just ignore it.
4830       (let ((old-params (gnus-info-params info))
4831             (new-params (list (cons name value))))
4832         (while old-params
4833           (if (or (not (listp (car old-params)))
4834                   (not (eq (caar old-params) name)))
4835               (setq new-params (append new-params (list (car old-params)))))
4836           (setq old-params (cdr old-params)))
4837         (gnus-group-set-info new-params group 'params)))))
4838
4839 (defun gnus-group-add-score (group &optional score)
4840   "Add SCORE to the GROUP score.
4841 If SCORE is nil, add 1 to the score of GROUP."
4842   (let ((info (gnus-get-info group)))
4843     (when info
4844       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4845
4846 (defun gnus-summary-bubble-group ()
4847   "Increase the score of the current group.
4848 This is a handy function to add to `gnus-summary-exit-hook' to
4849 increase the score of each group you read."
4850   (gnus-group-add-score gnus-newsgroup-name))
4851
4852 (defun gnus-group-set-info (info &optional method-only-group part)
4853   (let* ((entry (gnus-gethash
4854                  (or method-only-group (gnus-info-group info))
4855                  gnus-newsrc-hashtb))
4856          (part-info info)
4857          (info (if method-only-group (nth 2 entry) info))
4858          method)
4859     (when method-only-group
4860       (unless entry
4861         (error "Trying to change non-existent group %s" method-only-group))
4862       ;; We have received parts of the actual group info - either the
4863       ;; select method or the group parameters.  We first check
4864       ;; whether we have to extend the info, and if so, do that.
4865       (let ((len (length info))
4866             (total (if (eq part 'method) 5 6)))
4867         (when (< len total)
4868           (setcdr (nthcdr (1- len) info)
4869                   (make-list (- total len) nil)))
4870         ;; Then we enter the new info.
4871         (setcar (nthcdr (1- total) info) part-info)))
4872     (unless entry
4873       ;; This is a new group, so we just create it.
4874       (save-excursion
4875         (set-buffer gnus-group-buffer)
4876         (setq method (gnus-info-method info))
4877         (when (gnus-server-equal method "native")
4878           (setq method nil))
4879         (save-excursion
4880           (set-buffer gnus-group-buffer)
4881           (if method
4882               ;; It's a foreign group...
4883               (gnus-group-make-group
4884                (gnus-group-real-name (gnus-info-group info))
4885                (if (stringp method) method
4886                  (prin1-to-string (car method)))
4887                (and (consp method)
4888                     (nth 1 (gnus-info-method info))))
4889             ;; It's a native group.
4890             (gnus-group-make-group (gnus-info-group info))))
4891         (gnus-message 6 "Note: New group created")
4892         (setq entry
4893               (gnus-gethash (gnus-group-prefixed-name
4894                              (gnus-group-real-name (gnus-info-group info))
4895                              (or (gnus-info-method info) gnus-select-method))
4896                             gnus-newsrc-hashtb))))
4897     ;; Whether it was a new group or not, we now have the entry, so we
4898     ;; can do the update.
4899     (if entry
4900         (progn
4901           (setcar (nthcdr 2 entry) info)
4902           (when (and (not (eq (car entry) t))
4903                      (gnus-active (gnus-info-group info)))
4904             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4905       (error "No such group: %s" (gnus-info-group info)))))
4906
4907 (defun gnus-group-set-method-info (group select-method)
4908   (gnus-group-set-info select-method group 'method))
4909
4910 (defun gnus-group-set-params-info (group params)
4911   (gnus-group-set-info params group 'params))
4912
4913 (defun gnus-group-update-group-line ()
4914   "Update the current line in the group buffer."
4915   (let* ((buffer-read-only nil)
4916          (group (gnus-group-group-name))
4917          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4918          gnus-group-indentation)
4919     (and entry
4920          (not (gnus-ephemeral-group-p group))
4921          (gnus-dribble-enter
4922           (concat "(gnus-group-set-info '"
4923                   (prin1-to-string (nth 2 entry)) ")")))
4924     (setq gnus-group-indentation (gnus-group-group-indentation))
4925     (gnus-delete-line)
4926     (gnus-group-insert-group-line-info group)
4927     (forward-line -1)
4928     (gnus-group-position-point)))
4929
4930 (defun gnus-group-insert-group-line-info (group)
4931   "Insert GROUP on the current line."
4932   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4933         active info)
4934     (if entry
4935         (progn
4936           ;; (Un)subscribed group.
4937           (setq info (nth 2 entry))
4938           (gnus-group-insert-group-line
4939            group (gnus-info-level info) (gnus-info-marks info)
4940            (or (car entry) t) (gnus-info-method info)))
4941       ;; This group is dead.
4942       (gnus-group-insert-group-line
4943        group
4944        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4945        nil
4946        (if (setq active (gnus-active group))
4947            (- (1+ (cdr active)) (car active)) 0)
4948        nil))))
4949
4950 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4951                                                     gnus-tmp-marked number
4952                                                     gnus-tmp-method)
4953   "Insert a group line in the group buffer."
4954   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4955          (gnus-tmp-number-total
4956           (if gnus-tmp-active
4957               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4958             0))
4959          (gnus-tmp-number-of-unread
4960           (if (numberp number) (int-to-string (max 0 number))
4961             "*"))
4962          (gnus-tmp-number-of-read
4963           (if (numberp number)
4964               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4965             "*"))
4966          (gnus-tmp-subscribed
4967           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4968                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4969                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4970                 (t ?K)))
4971          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4972          (gnus-tmp-newsgroup-description
4973           (if gnus-description-hashtb
4974               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4975             ""))
4976          (gnus-tmp-moderated
4977           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4978          (gnus-tmp-moderated-string
4979           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4980          (gnus-tmp-method
4981           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4982          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4983          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4984          (gnus-tmp-news-method-string
4985           (if gnus-tmp-method
4986               (format "(%s:%s)" (car gnus-tmp-method)
4987                       (cadr gnus-tmp-method)) ""))
4988          (gnus-tmp-marked-mark
4989           (if (and (numberp number)
4990                    (zerop number)
4991                    (cdr (assq 'tick gnus-tmp-marked)))
4992               ?* ? ))
4993          (gnus-tmp-process-marked
4994           (if (member gnus-tmp-group gnus-group-marked)
4995               gnus-process-mark ? ))
4996          (gnus-tmp-grouplens
4997           (or (and gnus-use-grouplens
4998                    (bbb-grouplens-group-p gnus-tmp-group))
4999               ""))
5000          (buffer-read-only nil)
5001          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5002     (beginning-of-line)
5003     (gnus-add-text-properties
5004      (point)
5005      (prog1 (1+ (point))
5006        ;; Insert the text.
5007        (eval gnus-group-line-format-spec))
5008      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5009        gnus-unread ,(if (numberp number)
5010                         (string-to-int gnus-tmp-number-of-unread)
5011                       t)
5012        gnus-marked ,gnus-tmp-marked-mark
5013        gnus-indentation ,gnus-group-indentation
5014        gnus-level ,gnus-tmp-level))
5015     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5016       (forward-line -1)
5017       (run-hooks 'gnus-group-update-hook)
5018       (forward-line))
5019     ;; Allow XEmacs to remove front-sticky text properties.
5020     (gnus-group-remove-excess-properties)))
5021
5022 (defun gnus-group-update-group (group &optional visible-only)
5023   "Update all lines where GROUP appear.
5024 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5025 already."
5026   (save-excursion
5027     (set-buffer gnus-group-buffer)
5028     ;; The buffer may be narrowed.
5029     (save-restriction
5030       (widen)
5031       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5032             (loc (point-min))
5033             found buffer-read-only)
5034         ;; Enter the current status into the dribble buffer.
5035         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5036           (if (and entry (not (gnus-ephemeral-group-p group)))
5037               (gnus-dribble-enter
5038                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5039                        ")"))))
5040         ;; Find all group instances.  If topics are in use, each group
5041         ;; may be listed in more than once.
5042         (while (setq loc (text-property-any
5043                           loc (point-max) 'gnus-group ident))
5044           (setq found t)
5045           (goto-char loc)
5046           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5047             (gnus-delete-line)
5048             (gnus-group-insert-group-line-info group)
5049             (save-excursion
5050               (forward-line -1)
5051               (run-hooks 'gnus-group-update-group-hook)))
5052           (setq loc (1+ loc)))
5053         (unless (or found visible-only)
5054           ;; No such line in the buffer, find out where it's supposed to
5055           ;; go, and insert it there (or at the end of the buffer).
5056           (if gnus-goto-missing-group-function
5057               (funcall gnus-goto-missing-group-function group)
5058             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5059               (while (and entry (car entry)
5060                           (not
5061                            (gnus-goto-char
5062                             (text-property-any
5063                              (point-min) (point-max)
5064                              'gnus-group (gnus-intern-safe
5065                                           (caar entry) gnus-active-hashtb)))))
5066                 (setq entry (cdr entry)))
5067               (or entry (goto-char (point-max)))))
5068           ;; Finally insert the line.
5069           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5070             (gnus-group-insert-group-line-info group)
5071             (save-excursion
5072               (forward-line -1)
5073               (run-hooks 'gnus-group-update-group-hook))))
5074         (gnus-group-set-mode-line)))))
5075
5076 (defun gnus-group-set-mode-line ()
5077   "Update the mode line in the group buffer."
5078   (when (memq 'group gnus-updated-mode-lines)
5079     ;; Yes, we want to keep this mode line updated.
5080     (save-excursion
5081       (set-buffer gnus-group-buffer)
5082       (let* ((gformat (or gnus-group-mode-line-format-spec
5083                           (setq gnus-group-mode-line-format-spec
5084                                 (gnus-parse-format
5085                                  gnus-group-mode-line-format
5086                                  gnus-group-mode-line-format-alist))))
5087              (gnus-tmp-news-server (cadr gnus-select-method))
5088              (gnus-tmp-news-method (car gnus-select-method))
5089              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5090              (max-len 60)
5091              gnus-tmp-header            ;Dummy binding for user-defined formats
5092              ;; Get the resulting string.
5093              (mode-string (eval gformat)))
5094         ;; Say whether the dribble buffer has been modified.
5095         (setq mode-line-modified
5096               (if (and gnus-dribble-buffer
5097                        (buffer-name gnus-dribble-buffer)
5098                        (buffer-modified-p gnus-dribble-buffer)
5099                        (save-excursion
5100                          (set-buffer gnus-dribble-buffer)
5101                          (not (zerop (buffer-size)))))
5102                   "---*- " "----- "))
5103         ;; If the line is too long, we chop it off.
5104         (when (> (length mode-string) max-len)
5105           (setq mode-string (substring mode-string 0 (- max-len 4))))
5106         (prog1
5107             (setq mode-line-buffer-identification 
5108                   (list mode-string))
5109           (set-buffer-modified-p t))))))
5110
5111 (defun gnus-group-group-name ()
5112   "Get the name of the newsgroup on the current line."
5113   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5114     (and group (symbol-name group))))
5115
5116 (defun gnus-group-group-level ()
5117   "Get the level of the newsgroup on the current line."
5118   (get-text-property (gnus-point-at-bol) 'gnus-level))
5119
5120 (defun gnus-group-group-indentation ()
5121   "Get the indentation of the newsgroup on the current line."
5122   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5123       (and gnus-group-indentation-function
5124            (funcall gnus-group-indentation-function))
5125       ""))
5126
5127 (defun gnus-group-group-unread ()
5128   "Get the number of unread articles of the newsgroup on the current line."
5129   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5130
5131 (defun gnus-group-search-forward (&optional backward all level first-too)
5132   "Find the next newsgroup with unread articles.
5133 If BACKWARD is non-nil, find the previous newsgroup instead.
5134 If ALL is non-nil, just find any newsgroup.
5135 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5136 group exists.
5137 If FIRST-TOO, the current line is also eligible as a target."
5138   (let ((way (if backward -1 1))
5139         (low gnus-level-killed)
5140         (beg (point))
5141         pos found lev)
5142     (if (and backward (progn (beginning-of-line)) (bobp))
5143         nil
5144       (or first-too (forward-line way))
5145       (while (and
5146               (not (eobp))
5147               (not (setq
5148                     found
5149                     (and (or all
5150                              (and
5151                               (let ((unread
5152                                      (get-text-property (point) 'gnus-unread)))
5153                                 (and (numberp unread) (> unread 0)))
5154                               (setq lev (get-text-property (point)
5155                                                            'gnus-level))
5156                               (<= lev gnus-level-subscribed)))
5157                          (or (not level)
5158                              (and (setq lev (get-text-property (point)
5159                                                                'gnus-level))
5160                                   (or (= lev level)
5161                                       (and (< lev low)
5162                                            (< level lev)
5163                                            (progn
5164                                              (setq low lev)
5165                                              (setq pos (point))
5166                                              nil))))))))
5167               (zerop (forward-line way)))))
5168     (if found
5169         (progn (gnus-group-position-point) t)
5170       (goto-char (or pos beg))
5171       (and pos t))))
5172
5173 ;;; Gnus group mode commands
5174
5175 ;; Group marking.
5176
5177 (defun gnus-group-mark-group (n &optional unmark no-advance)
5178   "Mark the current group."
5179   (interactive "p")
5180   (let ((buffer-read-only nil)
5181         group)
5182     (while (and (> n 0)
5183                 (not (eobp)))
5184       (when (setq group (gnus-group-group-name))
5185         ;; Update the mark.
5186         (beginning-of-line)
5187         (forward-char
5188          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5189         (delete-char 1)
5190         (if unmark
5191             (progn
5192               (insert " ")
5193               (setq gnus-group-marked (delete group gnus-group-marked)))
5194           (insert "#")
5195           (setq gnus-group-marked
5196                 (cons group (delete group gnus-group-marked)))))
5197       (or no-advance (gnus-group-next-group 1))
5198       (decf n))
5199     (gnus-summary-position-point)
5200     n))
5201
5202 (defun gnus-group-unmark-group (n)
5203   "Remove the mark from the current group."
5204   (interactive "p")
5205   (gnus-group-mark-group n 'unmark)
5206   (gnus-group-position-point))
5207
5208 (defun gnus-group-unmark-all-groups ()
5209   "Unmark all groups."
5210   (interactive)
5211   (let ((groups gnus-group-marked))
5212     (save-excursion
5213       (while groups
5214         (gnus-group-remove-mark (pop groups)))))
5215   (gnus-group-position-point))
5216
5217 (defun gnus-group-mark-region (unmark beg end)
5218   "Mark all groups between point and mark.
5219 If UNMARK, remove the mark instead."
5220   (interactive "P\nr")
5221   (let ((num (count-lines beg end)))
5222     (save-excursion
5223       (goto-char beg)
5224       (- num (gnus-group-mark-group num unmark)))))
5225
5226 (defun gnus-group-mark-buffer (&optional unmark)
5227   "Mark all groups in the buffer.
5228 If UNMARK, remove the mark instead."
5229   (interactive "P")
5230   (gnus-group-mark-region unmark (point-min) (point-max)))
5231
5232 (defun gnus-group-mark-regexp (regexp)
5233   "Mark all groups that match some regexp."
5234   (interactive "sMark (regexp): ")
5235   (let ((alist (cdr gnus-newsrc-alist))
5236         group)
5237     (while alist
5238       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5239         (gnus-group-set-mark group))))
5240   (gnus-group-position-point))
5241
5242 (defun gnus-group-remove-mark (group)
5243   "Remove the process mark from GROUP and move point there.
5244 Return nil if the group isn't displayed."
5245   (if (gnus-group-goto-group group)
5246       (save-excursion
5247         (gnus-group-mark-group 1 'unmark t)
5248         t)
5249     (setq gnus-group-marked
5250           (delete group gnus-group-marked))
5251     nil))
5252
5253 (defun gnus-group-set-mark (group)
5254   "Set the process mark on GROUP."
5255   (if (gnus-group-goto-group group) 
5256       (save-excursion
5257         (gnus-group-mark-group 1 nil t))
5258     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5259
5260 (defun gnus-group-universal-argument (arg &optional groups func)
5261   "Perform any command on all groups accoring to the process/prefix convention."
5262   (interactive "P")
5263   (let ((groups (or groups (gnus-group-process-prefix arg)))
5264         group func)
5265     (if (eq (setq func (or func
5266                            (key-binding
5267                             (read-key-sequence
5268                              (substitute-command-keys
5269                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5270             'undefined)
5271         (gnus-error 1 "Undefined key")
5272       (while groups
5273         (gnus-group-remove-mark (setq group (pop groups)))
5274         (command-execute func))))
5275   (gnus-group-position-point))
5276
5277 (defun gnus-group-process-prefix (n)
5278   "Return a list of groups to work on.
5279 Take into consideration N (the prefix) and the list of marked groups."
5280   (cond
5281    (n
5282     (setq n (prefix-numeric-value n))
5283     ;; There is a prefix, so we return a list of the N next
5284     ;; groups.
5285     (let ((way (if (< n 0) -1 1))
5286           (n (abs n))
5287           group groups)
5288       (save-excursion
5289         (while (and (> n 0)
5290                     (setq group (gnus-group-group-name)))
5291           (setq groups (cons group groups))
5292           (setq n (1- n))
5293           (gnus-group-next-group way)))
5294       (nreverse groups)))
5295    ((and (boundp 'transient-mark-mode)
5296          transient-mark-mode
5297          (boundp 'mark-active)
5298          mark-active)
5299     ;; Work on the region between point and mark.
5300     (let ((max (max (point) (mark)))
5301           groups)
5302       (save-excursion
5303         (goto-char (min (point) (mark)))
5304         (while
5305             (and
5306              (push (gnus-group-group-name) groups)
5307              (zerop (gnus-group-next-group 1))
5308              (< (point) max)))
5309         (nreverse groups))))
5310    (gnus-group-marked
5311     ;; No prefix, but a list of marked articles.
5312     (reverse gnus-group-marked))
5313    (t
5314     ;; Neither marked articles or a prefix, so we return the
5315     ;; current group.
5316     (let ((group (gnus-group-group-name)))
5317       (and group (list group))))))
5318
5319 ;; Selecting groups.
5320
5321 (defun gnus-group-read-group (&optional all no-article group)
5322   "Read news in this newsgroup.
5323 If the prefix argument ALL is non-nil, already read articles become
5324 readable.  IF ALL is a number, fetch this number of articles.  If the
5325 optional argument NO-ARTICLE is non-nil, no article will be
5326 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5327 group."
5328   (interactive "P")
5329   (let ((group (or group (gnus-group-group-name)))
5330         number active marked entry)
5331     (or group (error "No group on current line"))
5332     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5333                                             group gnus-newsrc-hashtb)))))
5334     ;; This group might be a dead group.  In that case we have to get
5335     ;; the number of unread articles from `gnus-active-hashtb'.
5336     (setq number
5337           (cond ((numberp all) all)
5338                 (entry (car entry))
5339                 ((setq active (gnus-active group))
5340                  (- (1+ (cdr active)) (car active)))))
5341     (gnus-summary-read-group
5342      group (or all (and (numberp number)
5343                         (zerop (+ number (length (cdr (assq 'tick marked)))
5344                                   (length (cdr (assq 'dormant marked)))))))
5345      no-article)))
5346
5347 (defun gnus-group-select-group (&optional all)
5348   "Select this newsgroup.
5349 No article is selected automatically.
5350 If ALL is non-nil, already read articles become readable.
5351 If ALL is a number, fetch this number of articles."
5352   (interactive "P")
5353   (gnus-group-read-group all t))
5354
5355 (defun gnus-group-quick-select-group (&optional all)
5356   "Select the current group \"quickly\".
5357 This means that no highlighting or scoring will be performed."
5358   (interactive "P")
5359   (let (gnus-visual
5360         gnus-score-find-score-files-function
5361         gnus-apply-kill-hook
5362         gnus-summary-expunge-below)
5363     (gnus-group-read-group all t)))
5364
5365 (defun gnus-group-visible-select-group (&optional all)
5366   "Select the current group without hiding any articles."
5367   (interactive "P")
5368   (let ((gnus-inhibit-limiting t))
5369     (gnus-group-read-group all t)))
5370
5371 ;;;###autoload
5372 (defun gnus-fetch-group (group)
5373   "Start Gnus if necessary and enter GROUP.
5374 Returns whether the fetching was successful or not."
5375   (interactive "sGroup name: ")
5376   (or (get-buffer gnus-group-buffer)
5377       (gnus))
5378   (gnus-group-read-group nil nil group))
5379
5380 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5381 ;; if selection was successful.
5382 (defun gnus-group-read-ephemeral-group
5383   (group method &optional activate quit-config)
5384   (let ((group (if (gnus-group-foreign-p group) group
5385                  (gnus-group-prefixed-name group method))))
5386     (gnus-sethash
5387      group
5388      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5389                      ((quit-config . ,(if quit-config quit-config
5390                                         (cons (current-buffer) 'summary))))))
5391      gnus-newsrc-hashtb)
5392     (set-buffer gnus-group-buffer)
5393     (or (gnus-check-server method)
5394         (error "Unable to contact server: %s" (gnus-status-message method)))
5395     (if activate (or (gnus-request-group group)
5396                      (error "Couldn't request group")))
5397     (condition-case ()
5398         (gnus-group-read-group t t group)
5399       (error nil)
5400       (quit nil))))
5401
5402 (defun gnus-group-jump-to-group (group)
5403   "Jump to newsgroup GROUP."
5404   (interactive
5405    (list (completing-read
5406           "Group: " gnus-active-hashtb nil
5407           (gnus-read-active-file-p)
5408           nil
5409           'gnus-group-history)))
5410
5411   (when (equal group "")
5412     (error "Empty group name"))
5413
5414   (when (string-match "[\000-\032]" group)
5415     (error "Control characters in group: %s" group))
5416
5417   (let ((b (text-property-any
5418             (point-min) (point-max)
5419             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5420     (unless (gnus-ephemeral-group-p group)
5421       (if b
5422           ;; Either go to the line in the group buffer...
5423           (goto-char b)
5424         ;; ... or insert the line.
5425         (or
5426          (gnus-active group)
5427          (gnus-activate-group group)
5428          (error "%s error: %s" group (gnus-status-message group)))
5429
5430         (gnus-group-update-group group)
5431         (goto-char (text-property-any
5432                     (point-min) (point-max)
5433                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5434     ;; Adjust cursor point.
5435     (gnus-group-position-point)))
5436
5437 (defun gnus-group-goto-group (group)
5438   "Goto to newsgroup GROUP."
5439   (when group
5440     (let ((b (text-property-any (point-min) (point-max)
5441                                 'gnus-group (gnus-intern-safe
5442                                              group gnus-active-hashtb))))
5443       (and b (goto-char b)))))
5444
5445 (defun gnus-group-next-group (n)
5446   "Go to next N'th newsgroup.
5447 If N is negative, search backward instead.
5448 Returns the difference between N and the number of skips actually
5449 done."
5450   (interactive "p")
5451   (gnus-group-next-unread-group n t))
5452
5453 (defun gnus-group-next-unread-group (n &optional all level)
5454   "Go to next N'th unread newsgroup.
5455 If N is negative, search backward instead.
5456 If ALL is non-nil, choose any newsgroup, unread or not.
5457 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5458 such group can be found, the next group with a level higher than
5459 LEVEL.
5460 Returns the difference between N and the number of skips actually
5461 made."
5462   (interactive "p")
5463   (let ((backward (< n 0))
5464         (n (abs n)))
5465     (while (and (> n 0)
5466                 (gnus-group-search-forward
5467                  backward (or (not gnus-group-goto-unread) all) level))
5468       (setq n (1- n)))
5469     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5470                                (if level " on this level or higher" "")))
5471     n))
5472
5473 (defun gnus-group-prev-group (n)
5474   "Go to previous N'th newsgroup.
5475 Returns the difference between N and the number of skips actually
5476 done."
5477   (interactive "p")
5478   (gnus-group-next-unread-group (- n) t))
5479
5480 (defun gnus-group-prev-unread-group (n)
5481   "Go to previous N'th unread newsgroup.
5482 Returns the difference between N and the number of skips actually
5483 done."
5484   (interactive "p")
5485   (gnus-group-next-unread-group (- n)))
5486
5487 (defun gnus-group-next-unread-group-same-level (n)
5488   "Go to next N'th unread newsgroup on the same level.
5489 If N is negative, search backward instead.
5490 Returns the difference between N and the number of skips actually
5491 done."
5492   (interactive "p")
5493   (gnus-group-next-unread-group n t (gnus-group-group-level))
5494   (gnus-group-position-point))
5495
5496 (defun gnus-group-prev-unread-group-same-level (n)
5497   "Go to next N'th unread newsgroup on the same level.
5498 Returns the difference between N and the number of skips actually
5499 done."
5500   (interactive "p")
5501   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5502   (gnus-group-position-point))
5503
5504 (defun gnus-group-best-unread-group (&optional exclude-group)
5505   "Go to the group with the highest level.
5506 If EXCLUDE-GROUP, do not go to that group."
5507   (interactive)
5508   (goto-char (point-min))
5509   (let ((best 100000)
5510         unread best-point)
5511     (while (not (eobp))
5512       (setq unread (get-text-property (point) 'gnus-unread))
5513       (if (and (numberp unread) (> unread 0))
5514           (progn
5515             (if (and (get-text-property (point) 'gnus-level)
5516                      (< (get-text-property (point) 'gnus-level) best)
5517                      (or (not exclude-group)
5518                          (not (equal exclude-group (gnus-group-group-name)))))
5519                 (progn
5520                   (setq best (get-text-property (point) 'gnus-level))
5521                   (setq best-point (point))))))
5522       (forward-line 1))
5523     (if best-point (goto-char best-point))
5524     (gnus-summary-position-point)
5525     (and best-point (gnus-group-group-name))))
5526
5527 (defun gnus-group-first-unread-group ()
5528   "Go to the first group with unread articles."
5529   (interactive)
5530   (prog1
5531       (let ((opoint (point))
5532             unread)
5533         (goto-char (point-min))
5534         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5535                 (and (numberp unread)   ; Not a topic.
5536                      (not (zerop unread))) ; Has unread articles.
5537                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5538             (point)                     ; Success.
5539           (goto-char opoint)
5540           nil))                         ; Not success.
5541     (gnus-group-position-point)))
5542
5543 (defun gnus-group-enter-server-mode ()
5544   "Jump to the server buffer."
5545   (interactive)
5546   (gnus-enter-server-buffer))
5547
5548 (defun gnus-group-make-group (name &optional method address)
5549   "Add a new newsgroup.
5550 The user will be prompted for a NAME, for a select METHOD, and an
5551 ADDRESS."
5552   (interactive
5553    (cons
5554     (read-string "Group name: ")
5555     (let ((method
5556            (completing-read
5557             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5558             nil t nil 'gnus-method-history)))
5559       (cond ((assoc method gnus-valid-select-methods)
5560              (list method
5561                    (if (memq 'prompt-address
5562                              (assoc method gnus-valid-select-methods))
5563                        (read-string "Address: ")
5564                      "")))
5565             ((assoc method gnus-server-alist)
5566              (list method))
5567             (t
5568              (list method ""))))))
5569
5570   (let* ((meth (and method (if address (list (intern method) address)
5571                              method)))
5572          (nname (if method (gnus-group-prefixed-name name meth) name))
5573          backend info)
5574     (when (gnus-gethash nname gnus-newsrc-hashtb)
5575       (error "Group %s already exists" nname))
5576     ;; Subscribe to the new group.
5577     (gnus-group-change-level
5578      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5579      gnus-level-default-subscribed gnus-level-killed
5580      (and (gnus-group-group-name)
5581           (gnus-gethash (gnus-group-group-name)
5582                         gnus-newsrc-hashtb))
5583      t)
5584     ;; Make it active.
5585     (gnus-set-active nname (cons 1 0))
5586     (or (gnus-ephemeral-group-p name)
5587         (gnus-dribble-enter
5588          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5589     ;; Insert the line.
5590     (gnus-group-insert-group-line-info nname)
5591     (forward-line -1)
5592     (gnus-group-position-point)
5593
5594     ;; Load the backend and try to make the backend create
5595     ;; the group as well.
5596     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5597                                                   nil meth))))
5598                  gnus-valid-select-methods)
5599       (require backend))
5600     (gnus-check-server meth)
5601     (and (gnus-check-backend-function 'request-create-group nname)
5602          (gnus-request-create-group nname))
5603     t))
5604
5605 (defun gnus-group-delete-group (group &optional force)
5606   "Delete the current group.
5607 If FORCE (the prefix) is non-nil, all the articles in the group will
5608 be deleted.  This is \"deleted\" as in \"removed forever from the face
5609 of the Earth\".  There is no undo."
5610   (interactive
5611    (list (gnus-group-group-name)
5612          current-prefix-arg))
5613   (or group (error "No group to rename"))
5614   (or (gnus-check-backend-function 'request-delete-group group)
5615       (error "This backend does not support group deletion"))
5616   (prog1
5617       (if (not (gnus-yes-or-no-p
5618                 (format
5619                  "Do you really want to delete %s%s? "
5620                  group (if force " and all its contents" ""))))
5621           () ; Whew!
5622         (gnus-message 6 "Deleting group %s..." group)
5623         (if (not (gnus-request-delete-group group force))
5624             (gnus-error 3 "Couldn't delete group %s" group)
5625           (gnus-message 6 "Deleting group %s...done" group)
5626           (gnus-group-goto-group group)
5627           (gnus-group-kill-group 1 t)
5628           (gnus-sethash group nil gnus-active-hashtb)
5629           t))
5630     (gnus-group-position-point)))
5631
5632 (defun gnus-group-rename-group (group new-name)
5633   (interactive
5634    (list
5635     (gnus-group-group-name)
5636     (progn
5637       (or (gnus-check-backend-function
5638            'request-rename-group (gnus-group-group-name))
5639           (error "This backend does not support renaming groups"))
5640       (read-string "New group name: "))))
5641
5642   (or (gnus-check-backend-function 'request-rename-group group)
5643       (error "This backend does not support renaming groups"))
5644
5645   (or group (error "No group to rename"))
5646   (and (string-match "^[ \t]*$" new-name)
5647        (error "Not a valid group name"))
5648
5649   ;; We find the proper prefixed name.
5650   (setq new-name
5651         (gnus-group-prefixed-name
5652          (gnus-group-real-name new-name)
5653          (gnus-info-method (gnus-get-info group))))
5654
5655   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5656   (prog1
5657       (if (not (gnus-request-rename-group group new-name))
5658           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5659         ;; We rename the group internally by killing it...
5660         (gnus-group-goto-group group)
5661         (gnus-group-kill-group)
5662         ;; ... changing its name ...
5663         (setcar (cdar gnus-list-of-killed-groups) new-name)
5664         ;; ... and then yanking it.  Magic!
5665         (gnus-group-yank-group)
5666         (gnus-set-active new-name (gnus-active group))
5667         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5668         new-name)
5669     (gnus-group-position-point)))
5670
5671 (defun gnus-group-edit-group (group &optional part)
5672   "Edit the group on the current line."
5673   (interactive (list (gnus-group-group-name)))
5674   (let* ((part (or part 'info))
5675          (done-func `(lambda ()
5676                        "Exit editing mode and update the information."
5677                        (interactive)
5678                        (gnus-group-edit-group-done ',part ,group)))
5679          (winconf (current-window-configuration))
5680          info)
5681     (or group (error "No group on current line"))
5682     (or (setq info (gnus-get-info group))
5683         (error "Killed group; can't be edited"))
5684     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5685     (gnus-configure-windows 'edit-group)
5686     (gnus-add-current-to-buffer-list)
5687     (emacs-lisp-mode)
5688     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5689     (use-local-map (copy-keymap emacs-lisp-mode-map))
5690     (local-set-key "\C-c\C-c" done-func)
5691     (make-local-variable 'gnus-prev-winconf)
5692     (setq gnus-prev-winconf winconf)
5693     (erase-buffer)
5694     (insert
5695      (cond
5696       ((eq part 'method)
5697        ";; Type `C-c C-c' after editing the select method.\n\n")
5698       ((eq part 'params)
5699        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5700       ((eq part 'info)
5701        ";; Type `C-c C-c' after editing the group info.\n\n")))
5702     (insert
5703      (pp-to-string
5704       (cond ((eq part 'method)
5705              (or (gnus-info-method info) "native"))
5706             ((eq part 'params)
5707              (gnus-info-params info))
5708             (t info)))
5709      "\n")))
5710
5711 (defun gnus-group-edit-group-method (group)
5712   "Edit the select method of GROUP."
5713   (interactive (list (gnus-group-group-name)))
5714   (gnus-group-edit-group group 'method))
5715
5716 (defun gnus-group-edit-group-parameters (group)
5717   "Edit the group parameters of GROUP."
5718   (interactive (list (gnus-group-group-name)))
5719   (gnus-group-edit-group group 'params))
5720
5721 (defun gnus-group-edit-group-done (part group)
5722   "Get info from buffer, update variables and jump to the group buffer."
5723   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5724   (goto-char (point-min))
5725   (let* ((form (read (current-buffer)))
5726          (winconf gnus-prev-winconf)
5727          (method (cond ((eq part 'info) (nth 4 form))
5728                        ((eq part 'method) form)
5729                        (t nil)))
5730          (info (cond ((eq part 'info) form)
5731                      ((eq part 'method) (gnus-get-info group))
5732                      (t nil)))
5733          (new-group (if info
5734                       (if (or (not method)
5735                               (gnus-server-equal
5736                                gnus-select-method method))
5737                           (gnus-group-real-name (car info))
5738                         (gnus-group-prefixed-name
5739                          (gnus-group-real-name (car info)) method))
5740                       nil)))
5741     (when (and new-group
5742                (not (equal new-group group)))
5743       (when (gnus-group-goto-group group)
5744         (gnus-group-kill-group 1))
5745       (gnus-activate-group new-group))
5746     ;; Set the info.
5747     (if (and info new-group)
5748         (progn
5749           (setq info (gnus-copy-sequence info))
5750           (setcar info new-group)
5751           (unless (gnus-server-equal method "native")
5752             (unless (nthcdr 3 info)
5753               (nconc info (list nil nil)))
5754             (unless (nthcdr 4 info)
5755               (nconc info (list nil)))
5756             (gnus-info-set-method info method))
5757           (gnus-group-set-info info))
5758       (gnus-group-set-info form (or new-group group) part))
5759     (kill-buffer (current-buffer))
5760     (and winconf (set-window-configuration winconf))
5761     (set-buffer gnus-group-buffer)
5762     (gnus-group-update-group (or new-group group))
5763     (gnus-group-position-point)))
5764
5765 (defun gnus-group-make-help-group ()
5766   "Create the Gnus documentation group."
5767   (interactive)
5768   (let ((path load-path)
5769         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5770         file dir)
5771     (and (gnus-gethash name gnus-newsrc-hashtb)
5772          (error "Documentation group already exists"))
5773     (while path
5774       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5775             file nil)
5776       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5777                 (file-exists-p
5778                  (setq file (concat (file-name-directory
5779                                      (directory-file-name dir))
5780                                     "etc/gnus-tut.txt"))))
5781         (setq path nil)))
5782     (if (not file)
5783         (gnus-message 1 "Couldn't find doc group")
5784       (gnus-group-make-group
5785        (gnus-group-real-name name)
5786        (list 'nndoc "gnus-help"
5787              (list 'nndoc-address file)
5788              (list 'nndoc-article-type 'mbox)))))
5789   (gnus-group-position-point))
5790
5791 (defun gnus-group-make-doc-group (file type)
5792   "Create a group that uses a single file as the source."
5793   (interactive
5794    (list (read-file-name "File name: ")
5795          (and current-prefix-arg 'ask)))
5796   (when (eq type 'ask)
5797     (let ((err "")
5798           char found)
5799       (while (not found)
5800         (message
5801          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5802          err)
5803         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5804                           ((= char ?b) 'babyl)
5805                           ((= char ?d) 'digest)
5806                           ((= char ?f) 'forward)
5807                           ((= char ?a) 'mmfd)
5808                           (t (setq err (format "%c unknown. " char))
5809                              nil))))
5810       (setq type found)))
5811   (let* ((file (expand-file-name file))
5812          (name (gnus-generate-new-group-name
5813                 (gnus-group-prefixed-name
5814                  (file-name-nondirectory file) '(nndoc "")))))
5815     (gnus-group-make-group
5816      (gnus-group-real-name name)
5817      (list 'nndoc (file-name-nondirectory file)
5818            (list 'nndoc-address file)
5819            (list 'nndoc-article-type (or type 'guess))))))
5820
5821 (defun gnus-group-make-archive-group (&optional all)
5822   "Create the (ding) Gnus archive group of the most recent articles.
5823 Given a prefix, create a full group."
5824   (interactive "P")
5825   (let ((group (gnus-group-prefixed-name
5826                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5827     (and (gnus-gethash group gnus-newsrc-hashtb)
5828          (error "Archive group already exists"))
5829     (gnus-group-make-group
5830      (gnus-group-real-name group)
5831      (list 'nndir (if all "hpc" "edu")
5832            (list 'nndir-directory
5833                  (if all gnus-group-archive-directory
5834                    gnus-group-recent-archive-directory))))))
5835
5836 (defun gnus-group-make-directory-group (dir)
5837   "Create an nndir group.
5838 The user will be prompted for a directory.  The contents of this
5839 directory will be used as a newsgroup.  The directory should contain
5840 mail messages or news articles in files that have numeric names."
5841   (interactive
5842    (list (read-file-name "Create group from directory: ")))
5843   (or (file-exists-p dir) (error "No such directory"))
5844   (or (file-directory-p dir) (error "Not a directory"))
5845   (let ((ext "")
5846         (i 0)
5847         group)
5848     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5849       (setq group
5850             (gnus-group-prefixed-name
5851              (concat (file-name-as-directory (directory-file-name dir))
5852                      ext)
5853              '(nndir "")))
5854       (setq ext (format "<%d>" (setq i (1+ i)))))
5855     (gnus-group-make-group
5856      (gnus-group-real-name group)
5857      (list 'nndir group (list 'nndir-directory dir)))))
5858
5859 (defun gnus-group-make-kiboze-group (group address scores)
5860   "Create an nnkiboze group.
5861 The user will be prompted for a name, a regexp to match groups, and
5862 score file entries for articles to include in the group."
5863   (interactive
5864    (list
5865     (read-string "nnkiboze group name: ")
5866     (read-string "Source groups (regexp): ")
5867     (let ((headers (mapcar (lambda (group) (list group))
5868                            '("subject" "from" "number" "date" "message-id"
5869                              "references" "chars" "lines" "xref"
5870                              "followup" "all" "body" "head")))
5871           scores header regexp regexps)
5872       (while (not (equal "" (setq header (completing-read
5873                                           "Match on header: " headers nil t))))
5874         (setq regexps nil)
5875         (while (not (equal "" (setq regexp (read-string
5876                                             (format "Match on %s (string): "
5877                                                     header)))))
5878           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5879         (setq scores (cons (cons header regexps) scores)))
5880       scores)))
5881   (gnus-group-make-group group "nnkiboze" address)
5882   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5883     (let (emacs-lisp-mode-hook)
5884       (pp scores (current-buffer)))))
5885
5886 (defun gnus-group-add-to-virtual (n vgroup)
5887   "Add the current group to a virtual group."
5888   (interactive
5889    (list current-prefix-arg
5890          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5891                           "nnvirtual:")))
5892   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5893       (error "%s is not an nnvirtual group" vgroup))
5894   (let* ((groups (gnus-group-process-prefix n))
5895          (method (gnus-info-method (gnus-get-info vgroup))))
5896     (setcar (cdr method)
5897             (concat
5898              (nth 1 method) "\\|"
5899              (mapconcat
5900               (lambda (s)
5901                 (gnus-group-remove-mark s)
5902                 (concat "\\(^" (regexp-quote s) "$\\)"))
5903               groups "\\|"))))
5904   (gnus-group-position-point))
5905
5906 (defun gnus-group-make-empty-virtual (group)
5907   "Create a new, fresh, empty virtual group."
5908   (interactive "sCreate new, empty virtual group: ")
5909   (let* ((method (list 'nnvirtual "^$"))
5910          (pgroup (gnus-group-prefixed-name group method)))
5911     ;; Check whether it exists already.
5912     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5913          (error "Group %s already exists." pgroup))
5914     ;; Subscribe the new group after the group on the current line.
5915     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5916     (gnus-group-update-group pgroup)
5917     (forward-line -1)
5918     (gnus-group-position-point)))
5919
5920 (defun gnus-group-enter-directory (dir)
5921   "Enter an ephemeral nneething group."
5922   (interactive "DDirectory to read: ")
5923   (let* ((method (list 'nneething dir))
5924          (leaf (gnus-group-prefixed-name
5925                 (file-name-nondirectory (directory-file-name dir))
5926                 method))
5927          (name (gnus-generate-new-group-name leaf)))
5928     (let ((nneething-read-only t))
5929       (or (gnus-group-read-ephemeral-group
5930            name method t
5931            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5932                                       'summary 'group)))
5933           (error "Couldn't enter %s" dir)))))
5934
5935 ;; Group sorting commands
5936 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5937
5938 (defun gnus-group-sort-groups (func &optional reverse)
5939   "Sort the group buffer according to FUNC.
5940 If REVERSE, reverse the sorting order."
5941   (interactive (list gnus-group-sort-function
5942                      current-prefix-arg))
5943   (let ((func (cond 
5944                ((not (listp func)) func)
5945                ((null func) func)
5946                ((= 1 (length func)) (car func))
5947                (t `(lambda (t1 t2)
5948                      ,(gnus-make-sort-function 
5949                        (reverse func)))))))
5950     ;; We peel off the dummy group from the alist.
5951     (when func
5952       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5953         (pop gnus-newsrc-alist))
5954       ;; Do the sorting.
5955       (setq gnus-newsrc-alist
5956             (sort gnus-newsrc-alist func))
5957       (when reverse
5958         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5959       ;; Regenerate the hash table.
5960       (gnus-make-hashtable-from-newsrc-alist)
5961       (gnus-group-list-groups))))
5962
5963 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5964   "Sort the group buffer alphabetically by group name.
5965 If REVERSE, sort in reverse order."
5966   (interactive "P")
5967   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5968
5969 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5970   "Sort the group buffer by number of unread articles.
5971 If REVERSE, sort in reverse order."
5972   (interactive "P")
5973   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5974
5975 (defun gnus-group-sort-groups-by-level (&optional reverse)
5976   "Sort the group buffer by group level.
5977 If REVERSE, sort in reverse order."
5978   (interactive "P")
5979   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5980
5981 (defun gnus-group-sort-groups-by-score (&optional reverse)
5982   "Sort the group buffer by group score.
5983 If REVERSE, sort in reverse order."
5984   (interactive "P")
5985   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5986
5987 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5988   "Sort the group buffer by group rank.
5989 If REVERSE, sort in reverse order."
5990   (interactive "P")
5991   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5992
5993 (defun gnus-group-sort-groups-by-method (&optional reverse)
5994   "Sort the group buffer alphabetically by backend name.
5995 If REVERSE, sort in reverse order."
5996   (interactive "P")
5997   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5998
5999 (defun gnus-group-sort-by-alphabet (info1 info2)
6000   "Sort alphabetically."
6001   (string< (gnus-info-group info1) (gnus-info-group info2)))
6002
6003 (defun gnus-group-sort-by-unread (info1 info2)
6004   "Sort by number of unread articles."
6005   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6006         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6007     (< (or (and (numberp n1) n1) 0)
6008        (or (and (numberp n2) n2) 0))))
6009
6010 (defun gnus-group-sort-by-level (info1 info2)
6011   "Sort by level."
6012   (< (gnus-info-level info1) (gnus-info-level info2)))
6013
6014 (defun gnus-group-sort-by-method (info1 info2)
6015   "Sort alphabetically by backend name."
6016   (string< (symbol-name (car (gnus-find-method-for-group
6017                               (gnus-info-group info1) info1)))
6018            (symbol-name (car (gnus-find-method-for-group
6019                               (gnus-info-group info2) info2)))))
6020
6021 (defun gnus-group-sort-by-score (info1 info2)
6022   "Sort by group score."
6023   (< (gnus-info-score info1) (gnus-info-score info2)))
6024
6025 (defun gnus-group-sort-by-rank (info1 info2)
6026   "Sort by level and score."
6027   (let ((level1 (gnus-info-level info1))
6028         (level2 (gnus-info-level info2)))
6029     (or (< level1 level2)
6030         (and (= level1 level2)
6031              (> (gnus-info-score info1) (gnus-info-score info2))))))
6032
6033 ;; Group catching up.
6034
6035 (defun gnus-group-clear-data (n)
6036   "Clear all marks and read ranges from the current group."
6037   (interactive "P")
6038   (let ((groups (gnus-group-process-prefix n))
6039         group info)
6040     (while (setq group (pop groups))
6041       (setq info (gnus-get-info group))
6042       (gnus-info-set-read info nil)
6043       (when (gnus-info-marks info)
6044         (gnus-info-set-marks info nil))
6045       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6046       (when (gnus-group-goto-group group)
6047         (gnus-group-remove-mark group)
6048         (gnus-group-update-group-line)))))
6049
6050 (defun gnus-group-catchup-current (&optional n all)
6051   "Mark all articles not marked as unread in current newsgroup as read.
6052 If prefix argument N is numeric, the ARG next newsgroups will be
6053 caught up.  If ALL is non-nil, marked articles will also be marked as
6054 read.  Cross references (Xref: header) of articles are ignored.
6055 The difference between N and actual number of newsgroups that were
6056 caught up is returned."
6057   (interactive "P")
6058   (unless (gnus-group-group-name)
6059     (error "No group on the current line"))
6060   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6061                gnus-expert-user
6062                (gnus-y-or-n-p
6063                 (if all
6064                     "Do you really want to mark all articles as read? "
6065                   "Mark all unread articles as read? "))))
6066       n
6067     (let ((groups (gnus-group-process-prefix n))
6068           (ret 0))
6069       (while groups
6070         ;; Virtual groups have to be given special treatment.
6071         (let ((method (gnus-find-method-for-group (car groups))))
6072           (if (eq 'nnvirtual (car method))
6073               (nnvirtual-catchup-group
6074                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6075         (gnus-group-remove-mark (car groups))
6076         (if (>= (gnus-group-group-level) gnus-level-zombie)
6077             (gnus-message 2 "Dead groups can't be caught up")
6078           (if (prog1
6079                   (gnus-group-goto-group (car groups))
6080                 (gnus-group-catchup (car groups) all))
6081               (gnus-group-update-group-line)
6082             (setq ret (1+ ret))))
6083         (setq groups (cdr groups)))
6084       (gnus-group-next-unread-group 1)
6085       ret)))
6086
6087 (defun gnus-group-catchup-current-all (&optional n)
6088   "Mark all articles in current newsgroup as read.
6089 Cross references (Xref: header) of articles are ignored."
6090   (interactive "P")
6091   (gnus-group-catchup-current n 'all))
6092
6093 (defun gnus-group-catchup (group &optional all)
6094   "Mark all articles in GROUP as read.
6095 If ALL is non-nil, all articles are marked as read.
6096 The return value is the number of articles that were marked as read,
6097 or nil if no action could be taken."
6098   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6099          (num (car entry)))
6100     ;; Do the updating only if the newsgroup isn't killed.
6101     (if (not (numberp (car entry)))
6102         (gnus-message 1 "Can't catch up; non-active group")
6103       ;; Do auto-expirable marks if that's required.
6104       (when (gnus-group-auto-expirable-p group)
6105         (gnus-add-marked-articles
6106          group 'expire (gnus-list-of-unread-articles group))
6107         (when all
6108           (let ((marks (nth 3 (nth 2 entry))))
6109             (gnus-add-marked-articles
6110              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6111             (gnus-add-marked-articles
6112              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6113       (when entry
6114         (gnus-update-read-articles group nil)
6115         ;; Also nix out the lists of marks and dormants.
6116         (when all
6117           (gnus-add-marked-articles group 'tick nil nil 'force)
6118           (gnus-add-marked-articles group 'dormant nil nil 'force))
6119         (run-hooks 'gnus-group-catchup-group-hook)
6120         num))))
6121
6122 (defun gnus-group-expire-articles (&optional n)
6123   "Expire all expirable articles in the current newsgroup."
6124   (interactive "P")
6125   (let ((groups (gnus-group-process-prefix n))
6126         group)
6127     (unless groups
6128       (error "No groups to expire"))
6129     (while (setq group (pop groups))
6130       (gnus-group-remove-mark group)
6131       (when (gnus-check-backend-function 'request-expire-articles group)
6132         (gnus-message 6 "Expiring articles in %s..." group)
6133         (let* ((info (gnus-get-info group))
6134                (expirable (if (gnus-group-total-expirable-p group)
6135                               (cons nil (gnus-list-of-read-articles group))
6136                             (assq 'expire (gnus-info-marks info))))
6137                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6138           (when expirable
6139             (setcdr
6140              expirable
6141              (gnus-compress-sequence
6142               (if expiry-wait
6143                   ;; We set the expiry variables to the groupp
6144                   ;; parameter. 
6145                   (let ((nnmail-expiry-wait-function nil)
6146                         (nnmail-expiry-wait expiry-wait))
6147                     (gnus-request-expire-articles
6148                      (gnus-uncompress-sequence (cdr expirable)) group))
6149                 ;; Just expire using the normal expiry values.
6150                 (gnus-request-expire-articles
6151                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6152           (gnus-message 6 "Expiring articles in %s...done" group)))
6153       (gnus-group-position-point))))
6154
6155 (defun gnus-group-expire-all-groups ()
6156   "Expire all expirable articles in all newsgroups."
6157   (interactive)
6158   (save-excursion
6159     (gnus-message 5 "Expiring...")
6160     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6161                                      (cdr gnus-newsrc-alist))))
6162       (gnus-group-expire-articles nil)))
6163   (gnus-group-position-point)
6164   (gnus-message 5 "Expiring...done"))
6165
6166 (defun gnus-group-set-current-level (n level)
6167   "Set the level of the next N groups to LEVEL."
6168   (interactive
6169    (list
6170     current-prefix-arg
6171     (string-to-int
6172      (let ((s (read-string
6173                (format "Level (default %s): "
6174                        (or (gnus-group-group-level) 
6175                            gnus-level-default-subscribed)))))
6176        (if (string-match "^\\s-*$" s)
6177            (int-to-string (or (gnus-group-group-level) 
6178                               gnus-level-default-subscribed))
6179          s)))))
6180   (or (and (>= level 1) (<= level gnus-level-killed))
6181       (error "Illegal level: %d" level))
6182   (let ((groups (gnus-group-process-prefix n))
6183         group)
6184     (while (setq group (pop groups))
6185       (gnus-group-remove-mark group)
6186       (gnus-message 6 "Changed level of %s from %d to %d"
6187                     group (or (gnus-group-group-level) gnus-level-killed)
6188                     level)
6189       (gnus-group-change-level
6190        group level (or (gnus-group-group-level) gnus-level-killed))
6191       (gnus-group-update-group-line)))
6192   (gnus-group-position-point))
6193
6194 (defun gnus-group-unsubscribe-current-group (&optional n)
6195   "Toggle subscription of the current group.
6196 If given numerical prefix, toggle the N next groups."
6197   (interactive "P")
6198   (let ((groups (gnus-group-process-prefix n))
6199         group)
6200     (while groups
6201       (setq group (car groups)
6202             groups (cdr groups))
6203       (gnus-group-remove-mark group)
6204       (gnus-group-unsubscribe-group
6205        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6206                  gnus-level-default-unsubscribed
6207                gnus-level-default-subscribed) t)
6208       (gnus-group-update-group-line))
6209     (gnus-group-next-group 1)))
6210
6211 (defun gnus-group-unsubscribe-group (group &optional level silent)
6212   "Toggle subscription to GROUP.
6213 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6214 group line."
6215   (interactive
6216    (list (completing-read
6217           "Group: " gnus-active-hashtb nil
6218           (gnus-read-active-file-p)
6219           nil 
6220           'gnus-group-history)))
6221   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6222     (cond
6223      ((string-match "^[ \t]$" group)
6224       (error "Empty group name"))
6225      (newsrc
6226       ;; Toggle subscription flag.
6227       (gnus-group-change-level
6228        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6229                                       gnus-level-subscribed)
6230                                   (1+ gnus-level-subscribed)
6231                                 gnus-level-default-subscribed)))
6232       (unless silent
6233         (gnus-group-update-group group)))
6234      ((and (stringp group)
6235            (or (not (gnus-read-active-file-p))
6236                (gnus-active group)))
6237       ;; Add new newsgroup.
6238       (gnus-group-change-level
6239        group
6240        (if level level gnus-level-default-subscribed)
6241        (or (and (member group gnus-zombie-list)
6242                 gnus-level-zombie)
6243            gnus-level-killed)
6244        (and (gnus-group-group-name)
6245             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6246       (unless silent
6247         (gnus-group-update-group group)))
6248      (t (error "No such newsgroup: %s" group)))
6249     (gnus-group-position-point)))
6250
6251 (defun gnus-group-transpose-groups (n)
6252   "Move the current newsgroup up N places.
6253 If given a negative prefix, move down instead.  The difference between
6254 N and the number of steps taken is returned."
6255   (interactive "p")
6256   (or (gnus-group-group-name)
6257       (error "No group on current line"))
6258   (gnus-group-kill-group 1)
6259   (prog1
6260       (forward-line (- n))
6261     (gnus-group-yank-group)
6262     (gnus-group-position-point)))
6263
6264 (defun gnus-group-kill-all-zombies ()
6265   "Kill all zombie newsgroups."
6266   (interactive)
6267   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6268   (setq gnus-zombie-list nil)
6269   (gnus-group-list-groups))
6270
6271 (defun gnus-group-kill-region (begin end)
6272   "Kill newsgroups in current region (excluding current point).
6273 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6274   (interactive "r")
6275   (let ((lines
6276          ;; Count lines.
6277          (save-excursion
6278            (count-lines
6279             (progn
6280               (goto-char begin)
6281               (beginning-of-line)
6282               (point))
6283             (progn
6284               (goto-char end)
6285               (beginning-of-line)
6286               (point))))))
6287     (goto-char begin)
6288     (beginning-of-line)                 ;Important when LINES < 1
6289     (gnus-group-kill-group lines)))
6290
6291 (defun gnus-group-kill-group (&optional n discard)
6292   "Kill the next N groups.
6293 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6294 However, only groups that were alive can be yanked; already killed
6295 groups or zombie groups can't be yanked.
6296 The return value is the name of the group that was killed, or a list
6297 of groups killed."
6298   (interactive "P")
6299   (let ((buffer-read-only nil)
6300         (groups (gnus-group-process-prefix n))
6301         group entry level out)
6302     (if (< (length groups) 10)
6303         ;; This is faster when there are few groups.
6304         (while groups
6305           (push (setq group (pop groups)) out)
6306           (gnus-group-remove-mark group)
6307           (setq level (gnus-group-group-level))
6308           (gnus-delete-line)
6309           (when (and (not discard)
6310                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6311             (push (cons (car entry) (nth 2 entry))
6312                   gnus-list-of-killed-groups))
6313           (gnus-group-change-level
6314            (if entry entry group) gnus-level-killed (if entry nil level)))
6315       ;; If there are lots and lots of groups to be killed, we use
6316       ;; this thing instead.
6317       (let (entry)
6318         (setq groups (nreverse groups))
6319         (while groups
6320           (gnus-group-remove-mark (setq group (pop groups)))
6321           (gnus-delete-line)
6322           (cond
6323            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6324             (push (cons (car entry) (nth 2 entry))
6325                   gnus-list-of-killed-groups)
6326             (setcdr (cdr entry) (cdddr entry)))
6327            ((member group gnus-zombie-list)
6328             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6329         (gnus-make-hashtable-from-newsrc-alist)))
6330
6331     (gnus-group-position-point)
6332     (if (< (length out) 2) (car out) (nreverse out))))
6333
6334 (defun gnus-group-yank-group (&optional arg)
6335   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6336 inserting it before the current newsgroup.  The numeric ARG specifies
6337 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6338 is returned, or (if several groups are yanked) a list of yanked groups
6339 is returned."
6340   (interactive "p")
6341   (setq arg (or arg 1))
6342   (let (info group prev out)
6343     (while (>= (decf arg) 0)
6344       (if (not (setq info (pop gnus-list-of-killed-groups)))
6345           (error "No more newsgroups to yank"))
6346       (push (setq group (nth 1 info)) out)
6347       ;; Find which newsgroup to insert this one before - search
6348       ;; backward until something suitable is found.  If there are no
6349       ;; other newsgroups in this buffer, just make this newsgroup the
6350       ;; first newsgroup.
6351       (setq prev (gnus-group-group-name))
6352       (gnus-group-change-level
6353        info (gnus-info-level (cdr info)) gnus-level-killed
6354        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6355        t)
6356       (gnus-group-insert-group-line-info group))
6357     (forward-line -1)
6358     (gnus-group-position-point)
6359     (if (< (length out) 2) (car out) (nreverse out))))
6360
6361 (defun gnus-group-kill-level (level)
6362   "Kill all groups that is on a certain LEVEL."
6363   (interactive "nKill all groups on level: ")
6364   (cond
6365    ((= level gnus-level-zombie)
6366     (setq gnus-killed-list
6367           (nconc gnus-zombie-list gnus-killed-list))
6368     (setq gnus-zombie-list nil))
6369    ((and (< level gnus-level-zombie)
6370          (> level 0)
6371          (or gnus-expert-user
6372              (gnus-yes-or-no-p
6373               (format
6374                "Do you really want to kill all groups on level %d? "
6375                level))))
6376     (let* ((prev gnus-newsrc-alist)
6377            (alist (cdr prev)))
6378       (while alist
6379         (if (= (gnus-info-level level) level)
6380             (setcdr prev (cdr alist))
6381           (setq prev alist))
6382         (setq alist (cdr alist)))
6383       (gnus-make-hashtable-from-newsrc-alist)
6384       (gnus-group-list-groups)))
6385    (t
6386     (error "Can't kill; illegal level: %d" level))))
6387
6388 (defun gnus-group-list-all-groups (&optional arg)
6389   "List all newsgroups with level ARG or lower.
6390 Default is gnus-level-unsubscribed, which lists all subscribed and most
6391 unsubscribed groups."
6392   (interactive "P")
6393   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6394
6395 ;; Redefine this to list ALL killed groups if prefix arg used.
6396 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6397 (defun gnus-group-list-killed (&optional arg)
6398   "List all killed newsgroups in the group buffer.
6399 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6400 entail asking the server for the groups."
6401   (interactive "P")
6402   ;; Find all possible killed newsgroups if arg.
6403   (when arg
6404     (gnus-get-killed-groups))
6405   (if (not gnus-killed-list)
6406       (gnus-message 6 "No killed groups")
6407     (let (gnus-group-list-mode)
6408       (funcall gnus-group-prepare-function
6409                gnus-level-killed t gnus-level-killed))
6410     (goto-char (point-min)))
6411   (gnus-group-position-point))
6412
6413 (defun gnus-group-list-zombies ()
6414   "List all zombie newsgroups in the group buffer."
6415   (interactive)
6416   (if (not gnus-zombie-list)
6417       (gnus-message 6 "No zombie groups")
6418     (let (gnus-group-list-mode)
6419       (funcall gnus-group-prepare-function
6420                gnus-level-zombie t gnus-level-zombie))
6421     (goto-char (point-min)))
6422   (gnus-group-position-point))
6423
6424 (defun gnus-group-list-active ()
6425   "List all groups that are available from the server(s)."
6426   (interactive)
6427   ;; First we make sure that we have really read the active file.
6428   (unless (gnus-read-active-file-p)
6429     (let ((gnus-read-active-file t))
6430       (gnus-read-active-file)))
6431   ;; Find all groups and sort them.
6432   (let ((groups
6433          (sort
6434           (let (list)
6435             (mapatoms
6436              (lambda (sym)
6437                (and (boundp sym)
6438                     (symbol-value sym)
6439                     (setq list (cons (symbol-name sym) list))))
6440              gnus-active-hashtb)
6441             list)
6442           'string<))
6443         (buffer-read-only nil))
6444     (erase-buffer)
6445     (while groups
6446       (gnus-group-insert-group-line-info (pop groups)))
6447     (goto-char (point-min))))
6448
6449 (defun gnus-activate-all-groups (level)
6450   "Activate absolutely all groups."
6451   (interactive (list 7))
6452   (let ((gnus-activate-level level)
6453         (gnus-activate-foreign-newsgroups level))
6454     (gnus-group-get-new-news)))
6455
6456 (defun gnus-group-get-new-news (&optional arg)
6457   "Get newly arrived articles.
6458 If ARG is a number, it specifies which levels you are interested in
6459 re-scanning.  If ARG is non-nil and not a number, this will force
6460 \"hard\" re-reading of the active files from all servers."
6461   (interactive "P")
6462   (run-hooks 'gnus-get-new-news-hook)
6463   ;; We might read in new NoCeM messages here.
6464   (when (and gnus-use-nocem 
6465              (null arg))
6466     (gnus-nocem-scan-groups))
6467   ;; If ARG is not a number, then we read the active file.
6468   (when (and arg (not (numberp arg)))
6469     (let ((gnus-read-active-file t))
6470       (gnus-read-active-file))
6471     (setq arg nil))
6472
6473   (setq arg (gnus-group-default-level arg t))
6474   (if (and gnus-read-active-file (not arg))
6475       (progn
6476         (gnus-read-active-file)
6477         (gnus-get-unread-articles arg))
6478     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6479       (gnus-get-unread-articles arg)))
6480   (run-hooks 'gnus-after-getting-new-news-hook)
6481   (gnus-group-list-groups))
6482
6483 (defun gnus-group-get-new-news-this-group (&optional n)
6484   "Check for newly arrived news in the current group (and the N-1 next groups).
6485 The difference between N and the number of newsgroup checked is returned.
6486 If N is negative, this group and the N-1 previous groups will be checked."
6487   (interactive "P")
6488   (let* ((groups (gnus-group-process-prefix n))
6489          (ret (if (numberp n) (- n (length groups)) 0))
6490          (beg (unless n (point)))
6491          group)
6492     (while (setq group (pop groups))
6493       (gnus-group-remove-mark group)
6494       (if (gnus-activate-group group 'scan)
6495           (progn
6496             (gnus-get-unread-articles-in-group
6497              (gnus-get-info group) (gnus-active group) t)
6498             (unless (gnus-virtual-group-p group)
6499               (gnus-close-group group))
6500             (gnus-group-update-group group))
6501         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6502     (when beg (goto-char beg))
6503     (when gnus-goto-next-group-when-activating
6504       (gnus-group-next-unread-group 1 t))
6505     (gnus-summary-position-point)
6506     ret))
6507
6508 (defun gnus-group-fetch-faq (group &optional faq-dir)
6509   "Fetch the FAQ for the current group."
6510   (interactive
6511    (list
6512     (gnus-group-real-name (gnus-group-group-name))
6513     (cond (current-prefix-arg
6514            (completing-read
6515             "Faq dir: " (and (listp gnus-group-faq-directory)
6516                              gnus-group-faq-directory))))))
6517   (or faq-dir
6518       (setq faq-dir (if (listp gnus-group-faq-directory)
6519                         (car gnus-group-faq-directory)
6520                       gnus-group-faq-directory)))
6521   (or group (error "No group name given"))
6522   (let ((file (concat (file-name-as-directory faq-dir)
6523                       (gnus-group-real-name group))))
6524     (if (not (file-exists-p file))
6525         (error "No such file: %s" file)
6526       (find-file file))))
6527
6528 (defun gnus-group-describe-group (force &optional group)
6529   "Display a description of the current newsgroup."
6530   (interactive (list current-prefix-arg (gnus-group-group-name)))
6531   (and force (setq gnus-description-hashtb nil))
6532   (let ((method (gnus-find-method-for-group group))
6533         desc)
6534     (or group (error "No group name given"))
6535     (and (or (and gnus-description-hashtb
6536                   ;; We check whether this group's method has been
6537                   ;; queried for a description file.
6538                   (gnus-gethash
6539                    (gnus-group-prefixed-name "" method)
6540                    gnus-description-hashtb))
6541              (setq desc (gnus-group-get-description group))
6542              (gnus-read-descriptions-file method))
6543          (gnus-message 1
6544           (or desc (gnus-gethash group gnus-description-hashtb)
6545               "No description available")))))
6546
6547 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6548 (defun gnus-group-describe-all-groups (&optional force)
6549   "Pop up a buffer with descriptions of all newsgroups."
6550   (interactive "P")
6551   (and force (setq gnus-description-hashtb nil))
6552   (if (not (or gnus-description-hashtb
6553                (gnus-read-all-descriptions-files)))
6554       (error "Couldn't request descriptions file"))
6555   (let ((buffer-read-only nil)
6556         b)
6557     (erase-buffer)
6558     (mapatoms
6559      (lambda (group)
6560        (setq b (point))
6561        (insert (format "      *: %-20s %s\n" (symbol-name group)
6562                        (symbol-value group)))
6563        (gnus-add-text-properties
6564         b (1+ b) (list 'gnus-group group
6565                        'gnus-unread t 'gnus-marked nil
6566                        'gnus-level (1+ gnus-level-subscribed))))
6567      gnus-description-hashtb)
6568     (goto-char (point-min))
6569     (gnus-group-position-point)))
6570
6571 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6572 (defun gnus-group-apropos (regexp &optional search-description)
6573   "List all newsgroups that have names that match a regexp."
6574   (interactive "sGnus apropos (regexp): ")
6575   (let ((prev "")
6576         (obuf (current-buffer))
6577         groups des)
6578     ;; Go through all newsgroups that are known to Gnus.
6579     (mapatoms
6580      (lambda (group)
6581        (and (symbol-name group)
6582             (string-match regexp (symbol-name group))
6583             (setq groups (cons (symbol-name group) groups))))
6584      gnus-active-hashtb)
6585     ;; Also go through all descriptions that are known to Gnus.
6586     (when search-description
6587       (mapatoms
6588        (lambda (group)
6589          (and (string-match regexp (symbol-value group))
6590               (gnus-active (symbol-name group))
6591               (setq groups (cons (symbol-name group) groups))))
6592        gnus-description-hashtb))
6593     (if (not groups)
6594         (gnus-message 3 "No groups matched \"%s\"." regexp)
6595       ;; Print out all the groups.
6596       (save-excursion
6597         (pop-to-buffer "*Gnus Help*")
6598         (buffer-disable-undo (current-buffer))
6599         (erase-buffer)
6600         (setq groups (sort groups 'string<))
6601         (while groups
6602           ;; Groups may be entered twice into the list of groups.
6603           (if (not (string= (car groups) prev))
6604               (progn
6605                 (insert (setq prev (car groups)) "\n")
6606                 (if (and gnus-description-hashtb
6607                          (setq des (gnus-gethash (car groups)
6608                                                  gnus-description-hashtb)))
6609                     (insert "  " des "\n"))))
6610           (setq groups (cdr groups)))
6611         (goto-char (point-min))))
6612     (pop-to-buffer obuf)))
6613
6614 (defun gnus-group-description-apropos (regexp)
6615   "List all newsgroups that have names or descriptions that match a regexp."
6616   (interactive "sGnus description apropos (regexp): ")
6617   (if (not (or gnus-description-hashtb
6618                (gnus-read-all-descriptions-files)))
6619       (error "Couldn't request descriptions file"))
6620   (gnus-group-apropos regexp t))
6621
6622 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6623 (defun gnus-group-list-matching (level regexp &optional all lowest)
6624   "List all groups with unread articles that match REGEXP.
6625 If the prefix LEVEL is non-nil, it should be a number that says which
6626 level to cut off listing groups.
6627 If ALL, also list groups with no unread articles.
6628 If LOWEST, don't list groups with level lower than LOWEST.
6629
6630 This command may read the active file."
6631   (interactive "P\nsList newsgroups matching: ")
6632   ;; First make sure active file has been read.
6633   (when (and level
6634              (> (prefix-numeric-value level) gnus-level-killed))
6635     (gnus-get-killed-groups))
6636   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6637                            all (or lowest 1) regexp)
6638   (goto-char (point-min))
6639   (gnus-group-position-point))
6640
6641 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6642   "List all groups that match REGEXP.
6643 If the prefix LEVEL is non-nil, it should be a number that says which
6644 level to cut off listing groups.
6645 If LOWEST, don't list groups with level lower than LOWEST."
6646   (interactive "P\nsList newsgroups matching: ")
6647   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6648
6649 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6650 (defun gnus-group-save-newsrc (&optional force)
6651   "Save the Gnus startup files.
6652 If FORCE, force saving whether it is necessary or not."
6653   (interactive "P")
6654   (gnus-save-newsrc-file force))
6655
6656 (defun gnus-group-restart (&optional arg)
6657   "Force Gnus to read the .newsrc file."
6658   (interactive "P")
6659   (when (gnus-yes-or-no-p
6660          (format "Are you sure you want to read %s? "
6661                  gnus-current-startup-file))
6662     (gnus-save-newsrc-file)
6663     (gnus-setup-news 'force)
6664     (gnus-group-list-groups arg)))
6665
6666 (defun gnus-group-read-init-file ()
6667   "Read the Gnus elisp init file."
6668   (interactive)
6669   (gnus-read-init-file))
6670
6671 (defun gnus-group-check-bogus-groups (&optional silent)
6672   "Check bogus newsgroups.
6673 If given a prefix, don't ask for confirmation before removing a bogus
6674 group."
6675   (interactive "P")
6676   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6677   (gnus-group-list-groups))
6678
6679 (defun gnus-group-edit-global-kill (&optional article group)
6680   "Edit the global kill file.
6681 If GROUP, edit that local kill file instead."
6682   (interactive "P")
6683   (setq gnus-current-kill-article article)
6684   (gnus-kill-file-edit-file group)
6685   (gnus-message
6686    6
6687    (substitute-command-keys
6688     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6689             (if group "local" "global")))))
6690
6691 (defun gnus-group-edit-local-kill (article group)
6692   "Edit a local kill file."
6693   (interactive (list nil (gnus-group-group-name)))
6694   (gnus-group-edit-global-kill article group))
6695
6696 (defun gnus-group-force-update ()
6697   "Update `.newsrc' file."
6698   (interactive)
6699   (gnus-save-newsrc-file))
6700
6701 (defun gnus-group-suspend ()
6702   "Suspend the current Gnus session.
6703 In fact, cleanup buffers except for group mode buffer.
6704 The hook gnus-suspend-gnus-hook is called before actually suspending."
6705   (interactive)
6706   (run-hooks 'gnus-suspend-gnus-hook)
6707   ;; Kill Gnus buffers except for group mode buffer.
6708   (let ((group-buf (get-buffer gnus-group-buffer)))
6709     ;; Do this on a separate list in case the user does a ^G before we finish
6710     (let ((gnus-buffer-list
6711            (delq group-buf (delq gnus-dribble-buffer
6712                                  (append gnus-buffer-list nil)))))
6713       (while gnus-buffer-list
6714         (gnus-kill-buffer (car gnus-buffer-list))
6715         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6716     (if group-buf
6717         (progn
6718           (setq gnus-buffer-list (list group-buf))
6719           (bury-buffer group-buf)
6720           (delete-windows-on group-buf t)))))
6721
6722 (defun gnus-group-clear-dribble ()
6723   "Clear all information from the dribble buffer."
6724   (interactive)
6725   (gnus-dribble-clear)
6726   (gnus-message 7 "Cleared dribble buffer"))
6727
6728 (defun gnus-group-exit ()
6729   "Quit reading news after updating .newsrc.eld and .newsrc.
6730 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6731   (interactive)
6732   (when 
6733       (or noninteractive                ;For gnus-batch-kill
6734           (not gnus-interactive-exit)   ;Without confirmation
6735           gnus-expert-user
6736           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6737     (run-hooks 'gnus-exit-gnus-hook)
6738     ;; Offer to save data from non-quitted summary buffers.
6739     (gnus-offer-save-summaries)
6740     ;; Save the newsrc file(s).
6741     (gnus-save-newsrc-file)
6742     ;; Kill-em-all.
6743     (gnus-close-backends)
6744     ;; Reset everything.
6745     (gnus-clear-system)
6746     ;; Allow the user to do things after cleaning up.
6747     (run-hooks 'gnus-after-exiting-gnus-hook)))
6748
6749 (defun gnus-close-backends ()
6750   ;; Send a close request to all backends that support such a request.
6751   (let ((methods gnus-valid-select-methods)
6752         func)
6753     (while methods
6754       (if (fboundp (setq func (intern (concat (caar methods)
6755                                               "-request-close"))))
6756           (funcall func))
6757       (setq methods (cdr methods)))))
6758
6759 (defun gnus-group-quit ()
6760   "Quit reading news without updating .newsrc.eld or .newsrc.
6761 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6762   (interactive)
6763   (when (or noninteractive              ;For gnus-batch-kill
6764             (zerop (buffer-size))
6765             (not (gnus-server-opened gnus-select-method))
6766             gnus-expert-user
6767             (not gnus-current-startup-file)
6768             (gnus-yes-or-no-p
6769              (format "Quit reading news without saving %s? "
6770                      (file-name-nondirectory gnus-current-startup-file))))
6771     (run-hooks 'gnus-exit-gnus-hook)
6772     (if gnus-use-full-window
6773         (delete-other-windows)
6774       (gnus-remove-some-windows))
6775     (gnus-dribble-save)
6776     (gnus-close-backends)
6777     (gnus-clear-system)
6778     ;; Allow the user to do things after cleaning up.
6779     (run-hooks 'gnus-after-exiting-gnus-hook)))
6780
6781 (defun gnus-offer-save-summaries ()
6782   "Offer to save all active summary buffers."
6783   (save-excursion
6784     (let ((buflist (buffer-list))
6785           buffers bufname)
6786       ;; Go through all buffers and find all summaries.
6787       (while buflist
6788         (and (setq bufname (buffer-name (car buflist)))
6789              (string-match "Summary" bufname)
6790              (save-excursion
6791                (set-buffer bufname)
6792                ;; We check that this is, indeed, a summary buffer.
6793                (and (eq major-mode 'gnus-summary-mode)
6794                     ;; Also make sure this isn't bogus.
6795                     gnus-newsgroup-prepared))
6796              (push bufname buffers))
6797         (setq buflist (cdr buflist)))
6798       ;; Go through all these summary buffers and offer to save them.
6799       (when buffers
6800         (map-y-or-n-p
6801          "Update summary buffer %s? "
6802          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6803          buffers)))))
6804
6805 (defun gnus-group-describe-briefly ()
6806   "Give a one line description of the group mode commands."
6807   (interactive)
6808   (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")))
6809
6810 (defun gnus-group-browse-foreign-server (method)
6811   "Browse a foreign news server.
6812 If called interactively, this function will ask for a select method
6813  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6814 If not, METHOD should be a list where the first element is the method
6815 and the second element is the address."
6816   (interactive
6817    (list (let ((how (completing-read
6818                      "Which backend: "
6819                      (append gnus-valid-select-methods gnus-server-alist)
6820                      nil t (cons "nntp" 0) 'gnus-method-history)))
6821            ;; We either got a backend name or a virtual server name.
6822            ;; If the first, we also need an address.
6823            (if (assoc how gnus-valid-select-methods)
6824                (list (intern how)
6825                      ;; Suggested by mapjph@bath.ac.uk.
6826                      (completing-read
6827                       "Address: "
6828                       (mapcar (lambda (server) (list server))
6829                               gnus-secondary-servers)))
6830              ;; We got a server name, so we find the method.
6831              (gnus-server-to-method how)))))
6832   (gnus-browse-foreign-server method))
6833
6834 \f
6835 ;;;
6836 ;;; Gnus summary mode
6837 ;;;
6838
6839 (defvar gnus-summary-mode-map nil)
6840
6841 (put 'gnus-summary-mode 'mode-class 'special)
6842
6843 (unless gnus-summary-mode-map
6844   (setq gnus-summary-mode-map (make-keymap))
6845   (suppress-keymap gnus-summary-mode-map)
6846
6847   ;; Non-orthogonal keys
6848
6849   (gnus-define-keys gnus-summary-mode-map
6850     " " gnus-summary-next-page
6851     "\177" gnus-summary-prev-page
6852     [delete] gnus-summary-prev-page
6853     "\r" gnus-summary-scroll-up
6854     "n" gnus-summary-next-unread-article
6855     "p" gnus-summary-prev-unread-article
6856     "N" gnus-summary-next-article
6857     "P" gnus-summary-prev-article
6858     "\M-\C-n" gnus-summary-next-same-subject
6859     "\M-\C-p" gnus-summary-prev-same-subject
6860     "\M-n" gnus-summary-next-unread-subject
6861     "\M-p" gnus-summary-prev-unread-subject
6862     "." gnus-summary-first-unread-article
6863     "," gnus-summary-best-unread-article
6864     "\M-s" gnus-summary-search-article-forward
6865     "\M-r" gnus-summary-search-article-backward
6866     "<" gnus-summary-beginning-of-article
6867     ">" gnus-summary-end-of-article
6868     "j" gnus-summary-goto-article
6869     "^" gnus-summary-refer-parent-article
6870     "\M-^" gnus-summary-refer-article
6871     "u" gnus-summary-tick-article-forward
6872     "!" gnus-summary-tick-article-forward
6873     "U" gnus-summary-tick-article-backward
6874     "d" gnus-summary-mark-as-read-forward
6875     "D" gnus-summary-mark-as-read-backward
6876     "E" gnus-summary-mark-as-expirable
6877     "\M-u" gnus-summary-clear-mark-forward
6878     "\M-U" gnus-summary-clear-mark-backward
6879     "k" gnus-summary-kill-same-subject-and-select
6880     "\C-k" gnus-summary-kill-same-subject
6881     "\M-\C-k" gnus-summary-kill-thread
6882     "\M-\C-l" gnus-summary-lower-thread
6883     "e" gnus-summary-edit-article
6884     "#" gnus-summary-mark-as-processable
6885     "\M-#" gnus-summary-unmark-as-processable
6886     "\M-\C-t" gnus-summary-toggle-threads
6887     "\M-\C-s" gnus-summary-show-thread
6888     "\M-\C-h" gnus-summary-hide-thread
6889     "\M-\C-f" gnus-summary-next-thread
6890     "\M-\C-b" gnus-summary-prev-thread
6891     "\M-\C-u" gnus-summary-up-thread
6892     "\M-\C-d" gnus-summary-down-thread
6893     "&" gnus-summary-execute-command
6894     "c" gnus-summary-catchup-and-exit
6895     "\C-w" gnus-summary-mark-region-as-read
6896     "\C-t" gnus-summary-toggle-truncation
6897     "?" gnus-summary-mark-as-dormant
6898     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6899     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6900     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6901     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6902     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6903     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6904     "=" gnus-summary-expand-window
6905     "\C-x\C-s" gnus-summary-reselect-current-group
6906     "\M-g" gnus-summary-rescan-group
6907     "w" gnus-summary-stop-page-breaking
6908     "\C-c\C-r" gnus-summary-caesar-message
6909     "\M-t" gnus-summary-toggle-mime
6910     "f" gnus-summary-followup
6911     "F" gnus-summary-followup-with-original
6912     "C" gnus-summary-cancel-article
6913     "r" gnus-summary-reply
6914     "R" gnus-summary-reply-with-original
6915     "\C-c\C-f" gnus-summary-mail-forward
6916     "o" gnus-summary-save-article
6917     "\C-o" gnus-summary-save-article-mail
6918     "|" gnus-summary-pipe-output
6919     "\M-k" gnus-summary-edit-local-kill
6920     "\M-K" gnus-summary-edit-global-kill
6921     "V" gnus-version
6922     "\C-c\C-d" gnus-summary-describe-group
6923     "q" gnus-summary-exit
6924     "Q" gnus-summary-exit-no-update
6925     "\C-c\C-i" gnus-info-find-node
6926     gnus-mouse-2 gnus-mouse-pick-article
6927     "m" gnus-summary-mail-other-window
6928     "a" gnus-summary-post-news
6929     "x" gnus-summary-limit-to-unread
6930     "s" gnus-summary-isearch-article
6931     "t" gnus-article-hide-headers
6932     "g" gnus-summary-show-article
6933     "l" gnus-summary-goto-last-article
6934     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6935     "\C-d" gnus-summary-enter-digest-group
6936     "\C-c\C-b" gnus-bug
6937     "*" gnus-cache-enter-article
6938     "\M-*" gnus-cache-remove-article
6939     "\M-&" gnus-summary-universal-argument
6940     "\C-l" gnus-recenter
6941     "I" gnus-summary-increase-score
6942     "L" gnus-summary-lower-score
6943
6944     "V" gnus-summary-score-map
6945     "X" gnus-uu-extract-map
6946     "S" gnus-summary-send-map)
6947
6948   ;; Sort of orthogonal keymap
6949   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6950     "t" gnus-summary-tick-article-forward
6951     "!" gnus-summary-tick-article-forward
6952     "d" gnus-summary-mark-as-read-forward
6953     "r" gnus-summary-mark-as-read-forward
6954     "c" gnus-summary-clear-mark-forward
6955     " " gnus-summary-clear-mark-forward
6956     "e" gnus-summary-mark-as-expirable
6957     "x" gnus-summary-mark-as-expirable
6958     "?" gnus-summary-mark-as-dormant
6959     "b" gnus-summary-set-bookmark
6960     "B" gnus-summary-remove-bookmark
6961     "#" gnus-summary-mark-as-processable
6962     "\M-#" gnus-summary-unmark-as-processable
6963     "S" gnus-summary-limit-include-expunged
6964     "C" gnus-summary-catchup
6965     "H" gnus-summary-catchup-to-here
6966     "\C-c" gnus-summary-catchup-all
6967     "k" gnus-summary-kill-same-subject-and-select
6968     "K" gnus-summary-kill-same-subject
6969     "P" gnus-uu-mark-map)
6970
6971   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6972     "c" gnus-summary-clear-above
6973     "u" gnus-summary-tick-above
6974     "m" gnus-summary-mark-above
6975     "k" gnus-summary-kill-below)
6976
6977   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6978     "/" gnus-summary-limit-to-subject
6979     "n" gnus-summary-limit-to-articles
6980     "w" gnus-summary-pop-limit
6981     "s" gnus-summary-limit-to-subject
6982     "a" gnus-summary-limit-to-author
6983     "u" gnus-summary-limit-to-unread
6984     "m" gnus-summary-limit-to-marks
6985     "v" gnus-summary-limit-to-score
6986     "D" gnus-summary-limit-include-dormant
6987     "d" gnus-summary-limit-exclude-dormant
6988     ;;  "t" gnus-summary-limit-exclude-thread
6989     "E" gnus-summary-limit-include-expunged
6990     "c" gnus-summary-limit-exclude-childless-dormant
6991     "C" gnus-summary-limit-mark-excluded-as-read)
6992
6993   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6994     "n" gnus-summary-next-unread-article
6995     "p" gnus-summary-prev-unread-article
6996     "N" gnus-summary-next-article
6997     "P" gnus-summary-prev-article
6998     "\C-n" gnus-summary-next-same-subject
6999     "\C-p" gnus-summary-prev-same-subject
7000     "\M-n" gnus-summary-next-unread-subject
7001     "\M-p" gnus-summary-prev-unread-subject
7002     "f" gnus-summary-first-unread-article
7003     "b" gnus-summary-best-unread-article
7004     "j" gnus-summary-goto-article
7005     "g" gnus-summary-goto-subject
7006     "l" gnus-summary-goto-last-article
7007     "p" gnus-summary-pop-article)
7008
7009   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7010     "k" gnus-summary-kill-thread
7011     "l" gnus-summary-lower-thread
7012     "i" gnus-summary-raise-thread
7013     "T" gnus-summary-toggle-threads
7014     "t" gnus-summary-rethread-current
7015     "^" gnus-summary-reparent-thread
7016     "s" gnus-summary-show-thread
7017     "S" gnus-summary-show-all-threads
7018     "h" gnus-summary-hide-thread
7019     "H" gnus-summary-hide-all-threads
7020     "n" gnus-summary-next-thread
7021     "p" gnus-summary-prev-thread
7022     "u" gnus-summary-up-thread
7023     "o" gnus-summary-top-thread
7024     "d" gnus-summary-down-thread
7025     "#" gnus-uu-mark-thread
7026     "\M-#" gnus-uu-unmark-thread)
7027
7028   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7029     "c" gnus-summary-catchup-and-exit
7030     "C" gnus-summary-catchup-all-and-exit
7031     "E" gnus-summary-exit-no-update
7032     "Q" gnus-summary-exit
7033     "Z" gnus-summary-exit
7034     "n" gnus-summary-catchup-and-goto-next-group
7035     "R" gnus-summary-reselect-current-group
7036     "G" gnus-summary-rescan-group
7037     "N" gnus-summary-next-group
7038     "P" gnus-summary-prev-group)
7039
7040   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7041     " " gnus-summary-next-page
7042     "n" gnus-summary-next-page
7043     "\177" gnus-summary-prev-page
7044     [delete] gnus-summary-prev-page
7045     "p" gnus-summary-prev-page
7046     "\r" gnus-summary-scroll-up
7047     "<" gnus-summary-beginning-of-article
7048     ">" gnus-summary-end-of-article
7049     "b" gnus-summary-beginning-of-article
7050     "e" gnus-summary-end-of-article
7051     "^" gnus-summary-refer-parent-article
7052     "r" gnus-summary-refer-parent-article
7053     "R" gnus-summary-refer-references
7054     "g" gnus-summary-show-article
7055     "s" gnus-summary-isearch-article)
7056
7057   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7058     "b" gnus-article-add-buttons
7059     "B" gnus-article-add-buttons-to-head
7060     "o" gnus-article-treat-overstrike
7061     ;;  "w" gnus-article-word-wrap
7062     "w" gnus-article-fill-cited-article
7063     "c" gnus-article-remove-cr
7064     "L" gnus-article-remove-trailing-blank-lines
7065     "q" gnus-article-de-quoted-unreadable
7066     "f" gnus-article-display-x-face
7067     "l" gnus-summary-stop-page-breaking
7068     "r" gnus-summary-caesar-message
7069     "t" gnus-article-hide-headers
7070     "v" gnus-summary-verbose-headers
7071     "m" gnus-summary-toggle-mime)
7072
7073   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7074     "a" gnus-article-hide
7075     "h" gnus-article-hide-headers
7076     "b" gnus-article-hide-boring-headers
7077     "s" gnus-article-hide-signature
7078     "c" gnus-article-hide-citation
7079     "p" gnus-article-hide-pgp
7080     "\C-c" gnus-article-hide-citation-maybe)
7081
7082   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7083     "a" gnus-article-highlight
7084     "h" gnus-article-highlight-headers
7085     "c" gnus-article-highlight-citation
7086     "s" gnus-article-highlight-signature)
7087
7088   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7089     "z" gnus-article-date-ut
7090     "u" gnus-article-date-ut
7091     "l" gnus-article-date-local
7092     "e" gnus-article-date-lapsed
7093     "o" gnus-article-date-original)
7094
7095   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7096     "v" gnus-version
7097     "f" gnus-summary-fetch-faq
7098     "d" gnus-summary-describe-group
7099     "h" gnus-summary-describe-briefly
7100     "i" gnus-info-find-node)
7101
7102   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7103     "e" gnus-summary-expire-articles
7104     "\M-\C-e" gnus-summary-expire-articles-now
7105     "\177" gnus-summary-delete-article
7106     [delete] gnus-summary-delete-article
7107     "m" gnus-summary-move-article
7108     "r" gnus-summary-respool-article
7109     "w" gnus-summary-edit-article
7110     "c" gnus-summary-copy-article
7111     "B" gnus-summary-crosspost-article
7112     "q" gnus-summary-respool-query
7113     "i" gnus-summary-import-article)
7114
7115   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7116     "o" gnus-summary-save-article
7117     "m" gnus-summary-save-article-mail
7118     "r" gnus-summary-save-article-rmail
7119     "f" gnus-summary-save-article-file
7120     "b" gnus-summary-save-article-body-file
7121     "h" gnus-summary-save-article-folder
7122     "v" gnus-summary-save-article-vm
7123     "p" gnus-summary-pipe-output
7124     "s" gnus-soup-add-article)
7125   )
7126
7127 \f
7128
7129 (defun gnus-summary-mode (&optional group)
7130   "Major mode for reading articles.
7131
7132 All normal editing commands are switched off.
7133 \\<gnus-summary-mode-map>
7134 Each line in this buffer represents one article.  To read an
7135 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7136 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7137 respectively.
7138
7139 You can also post articles and send mail from this buffer.  To
7140 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7141 of an article, type `\\[gnus-summary-reply]'.
7142
7143 There are approx. one gazillion commands you can execute in this
7144 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7145
7146 The following commands are available:
7147
7148 \\{gnus-summary-mode-map}"
7149   (interactive)
7150   (when (and menu-bar-mode
7151              (gnus-visual-p 'summary-menu 'menu))
7152     (gnus-summary-make-menu-bar))
7153   (kill-all-local-variables)
7154   (gnus-summary-make-local-variables)
7155   (gnus-make-thread-indent-array)
7156   (gnus-simplify-mode-line)
7157   (setq major-mode 'gnus-summary-mode)
7158   (setq mode-name "Summary")
7159   (make-local-variable 'minor-mode-alist)
7160   (use-local-map gnus-summary-mode-map)
7161   (buffer-disable-undo (current-buffer))
7162   (setq buffer-read-only t)             ;Disable modification
7163   (setq truncate-lines t)
7164   (setq selective-display t)
7165   (setq selective-display-ellipses t)   ;Display `...'
7166   (setq buffer-display-table gnus-summary-display-table)
7167   (setq gnus-newsgroup-name group)
7168   (make-local-variable 'gnus-summary-line-format)
7169   (make-local-variable 'gnus-summary-line-format-spec)
7170   (make-local-variable 'gnus-summary-mark-positions)
7171   (run-hooks 'gnus-summary-mode-hook))
7172
7173 (defun gnus-summary-make-local-variables ()
7174   "Make all the local summary buffer variables."
7175   (let ((locals gnus-summary-local-variables)
7176         global local)
7177     (while (setq local (pop locals))
7178       (if (consp local)
7179           (progn
7180             (if (eq (cdr local) 'global)
7181                 ;; Copy the global value of the variable.
7182                 (setq global (symbol-value (car local)))
7183               ;; Use the value from the list.
7184               (setq global (eval (cdr local))))
7185             (make-local-variable (car local))
7186             (set (car local) global))
7187         ;; Simple nil-valued local variable.
7188         (make-local-variable local)
7189         (set local nil)))))
7190
7191 (defun gnus-summary-make-display-table ()
7192   ;; Change the display table.  Odd characters have a tendency to mess
7193   ;; up nicely formatted displays - we make all possible glyphs
7194   ;; display only a single character.
7195
7196   ;; We start from the standard display table, if any.
7197   (setq gnus-summary-display-table
7198         (or (copy-sequence standard-display-table)
7199             (make-display-table)))
7200   ;; Nix out all the control chars...
7201   (let ((i 32))
7202     (while (>= (setq i (1- i)) 0)
7203       (aset gnus-summary-display-table i [??])))
7204   ;; ... but not newline and cr, of course. (cr is necessary for the
7205   ;; selective display).
7206   (aset gnus-summary-display-table ?\n nil)
7207   (aset gnus-summary-display-table ?\r nil)
7208   ;; We nix out any glyphs over 126 that are not set already.
7209   (let ((i 256))
7210     (while (>= (setq i (1- i)) 127)
7211       ;; Only modify if the entry is nil.
7212       (or (aref gnus-summary-display-table i)
7213           (aset gnus-summary-display-table i [??])))))
7214
7215 (defun gnus-summary-clear-local-variables ()
7216   (let ((locals gnus-summary-local-variables))
7217     (while locals
7218       (if (consp (car locals))
7219           (and (vectorp (caar locals))
7220                (set (caar locals) nil))
7221         (and (vectorp (car locals))
7222              (set (car locals) nil)))
7223       (setq locals (cdr locals)))))
7224
7225 ;; Summary data functions.
7226
7227 (defmacro gnus-data-number (data)
7228   `(car ,data))
7229
7230 (defmacro gnus-data-set-number (data number)
7231   `(setcar ,data ,number))
7232
7233 (defmacro gnus-data-mark (data)
7234   `(nth 1 ,data))
7235
7236 (defmacro gnus-data-set-mark (data mark)
7237   `(setcar (nthcdr 1 ,data) ,mark))
7238
7239 (defmacro gnus-data-pos (data)
7240   `(nth 2 ,data))
7241
7242 (defmacro gnus-data-set-pos (data pos)
7243   `(setcar (nthcdr 2 ,data) ,pos))
7244
7245 (defmacro gnus-data-header (data)
7246   `(nth 3 ,data))
7247
7248 (defmacro gnus-data-level (data)
7249   `(nth 4 ,data))
7250
7251 (defmacro gnus-data-unread-p (data)
7252   `(= (nth 1 ,data) gnus-unread-mark))
7253
7254 (defmacro gnus-data-pseudo-p (data)
7255   `(consp (nth 3 ,data)))
7256
7257 (defmacro gnus-data-find (number)
7258   `(assq ,number gnus-newsgroup-data))
7259
7260 (defmacro gnus-data-find-list (number &optional data)
7261   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7262      (memq (assq ,number bdata)
7263            bdata)))
7264
7265 (defmacro gnus-data-make (number mark pos header level)
7266   `(list ,number ,mark ,pos ,header ,level))
7267
7268 (defun gnus-data-enter (after-article number mark pos header level offset)
7269   (let ((data (gnus-data-find-list after-article)))
7270     (or data (error "No such article: %d" after-article))
7271     (setcdr data (cons (gnus-data-make number mark pos header level)
7272                        (cdr data)))
7273     (setq gnus-newsgroup-data-reverse nil)
7274     (gnus-data-update-list (cddr data) offset)))
7275
7276 (defun gnus-data-enter-list (after-article list &optional offset)
7277   (when list
7278     (let ((data (and after-article (gnus-data-find-list after-article)))
7279           (ilist list))
7280       (or data (not after-article) (error "No such article: %d" after-article))
7281       ;; Find the last element in the list to be spliced into the main
7282       ;; list.
7283       (while (cdr list)
7284         (setq list (cdr list)))
7285       (if (not data)
7286           (progn
7287             (setcdr list gnus-newsgroup-data)
7288             (setq gnus-newsgroup-data ilist)
7289             (and offset (gnus-data-update-list (cdr list) offset)))
7290         (setcdr list (cdr data))
7291         (setcdr data ilist)
7292         (and offset (gnus-data-update-list (cdr data) offset)))
7293       (setq gnus-newsgroup-data-reverse nil))))
7294
7295 (defun gnus-data-remove (article &optional offset)
7296   (let ((data gnus-newsgroup-data))
7297     (if (= (gnus-data-number (car data)) article)
7298         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7299               gnus-newsgroup-data-reverse nil)
7300       (while (cdr data)
7301         (and (= (gnus-data-number (cadr data)) article)
7302              (progn
7303                (setcdr data (cddr data))
7304                (and offset (gnus-data-update-list (cdr data) offset))
7305                (setq data nil
7306                      gnus-newsgroup-data-reverse nil)))
7307         (setq data (cdr data))))))
7308
7309 (defmacro gnus-data-list (backward)
7310   `(if ,backward
7311        (or gnus-newsgroup-data-reverse
7312            (setq gnus-newsgroup-data-reverse
7313                  (reverse gnus-newsgroup-data)))
7314      gnus-newsgroup-data))
7315
7316 (defun gnus-data-update-list (data offset)
7317   "Add OFFSET to the POS of all data entries in DATA."
7318   (while data
7319     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7320     (setq data (cdr data))))
7321
7322 (defun gnus-data-compute-positions ()
7323   "Compute the positions of all articles."
7324   (let ((data gnus-newsgroup-data)
7325         pos)
7326     (while data
7327       (when (setq pos (text-property-any
7328                        (point-min) (point-max)
7329                        'gnus-number (gnus-data-number (car data))))
7330         (gnus-data-set-pos (car data) (+ pos 3)))
7331       (setq data (cdr data)))))
7332
7333 (defun gnus-summary-article-pseudo-p (article)
7334   "Say whether this article is a pseudo article or not."
7335   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7336
7337 (defun gnus-article-parent-p (number)
7338   "Say whether this article is a parent or not."
7339   (let ((data (gnus-data-find-list number)))
7340     (and (cdr data)                     ; There has to be an article after...
7341          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7342             (gnus-data-level (nth 1 data))))))
7343
7344 (defun gnus-article-children (number)
7345   "Return a list of all children to NUMBER."
7346   (let* ((data (gnus-data-find-list number))
7347          (level (gnus-data-level (car data)))
7348          children)
7349     (setq data (cdr data))
7350     (while (and data            
7351                 (= (gnus-data-level (car data)) (1+ level)))
7352       (push (gnus-data-number (car data)) children)
7353       (setq data (cdr data)))
7354     children))
7355
7356 (defmacro gnus-summary-skip-intangible ()
7357   "If the current article is intangible, then jump to a different article."
7358   '(let ((to (get-text-property (point) 'gnus-intangible)))
7359     (and to (gnus-summary-goto-subject to))))
7360
7361 (defmacro gnus-summary-article-intangible-p ()
7362   "Say whether this article is intangible or not."
7363   '(get-text-property (point) 'gnus-intangible))
7364
7365 ;; Some summary mode macros.
7366
7367 (defmacro gnus-summary-article-number ()
7368   "The article number of the article on the current line.
7369 If there isn's an article number here, then we return the current
7370 article number."
7371   '(progn
7372      (gnus-summary-skip-intangible)
7373      (or (get-text-property (point) 'gnus-number)
7374          (gnus-summary-last-subject))))
7375
7376 (defmacro gnus-summary-article-header (&optional number)
7377   `(gnus-data-header (gnus-data-find
7378                       ,(or number '(gnus-summary-article-number)))))
7379
7380 (defmacro gnus-summary-thread-level (&optional number)
7381   `(if (and (eq gnus-summary-make-false-root 'dummy)
7382             (get-text-property (point) 'gnus-intangible))
7383        0
7384      (gnus-data-level (gnus-data-find
7385                        ,(or number '(gnus-summary-article-number))))))
7386
7387 (defmacro gnus-summary-article-mark (&optional number)
7388   `(gnus-data-mark (gnus-data-find
7389                     ,(or number '(gnus-summary-article-number)))))
7390
7391 (defmacro gnus-summary-article-pos (&optional number)
7392   `(gnus-data-pos (gnus-data-find
7393                    ,(or number '(gnus-summary-article-number)))))
7394
7395 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7396 (defmacro gnus-summary-article-subject (&optional number)
7397   "Return current subject string or nil if nothing."
7398   `(let ((headers
7399           ,(if number
7400                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7401              '(gnus-data-header (assq (gnus-summary-article-number)
7402                                       gnus-newsgroup-data)))))
7403      (and headers
7404           (vectorp headers)
7405           (mail-header-subject headers))))
7406
7407 (defmacro gnus-summary-article-score (&optional number)
7408   "Return current article score."
7409   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7410                   gnus-newsgroup-scored))
7411        gnus-summary-default-score 0))
7412
7413 (defun gnus-summary-article-children (&optional number)
7414   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7415          (level (gnus-data-level (car data)))
7416          l children)
7417     (while (and (setq data (cdr data))
7418                 (> (setq l (gnus-data-level (car data))) level))
7419       (and (= (1+ level) l)
7420            (setq children (cons (gnus-data-number (car data))
7421                                 children))))
7422     (nreverse children)))
7423
7424 (defun gnus-summary-article-parent (&optional number)
7425   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7426                                     (gnus-data-list t)))
7427          (level (gnus-data-level (car data))))
7428     (if (zerop level)
7429         () ; This is a root.
7430       ;; We search until we find an article with a level less than
7431       ;; this one.  That function has to be the parent.
7432       (while (and (setq data (cdr data))
7433                   (not (< (gnus-data-level (car data)) level))))
7434       (and data (gnus-data-number (car data))))))
7435
7436 (defun gnus-unread-mark-p (mark)
7437   "Say whether MARK is the unread mark."
7438   (= mark gnus-unread-mark))
7439
7440 (defun gnus-read-mark-p (mark)
7441   "Say whether MARK is one of the marks that mark as read.
7442 This is all marks except unread, ticked, dormant, and expirable."
7443   (not (or (= mark gnus-unread-mark)
7444            (= mark gnus-ticked-mark)
7445            (= mark gnus-dormant-mark)
7446            (= mark gnus-expirable-mark))))
7447
7448 ;; Saving hidden threads.
7449
7450 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7451 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7452 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7453
7454 (defmacro gnus-save-hidden-threads (&rest forms)
7455   "Save hidden threads, eval FORMS, and restore the hidden threads."
7456   (let ((config (make-symbol "config")))
7457     `(let ((,config (gnus-hidden-threads-configuration)))
7458        (unwind-protect
7459            (progn
7460              ,@forms)
7461          (gnus-restore-hidden-threads-configuration ,config)))))
7462
7463 (defun gnus-hidden-threads-configuration ()
7464   "Return the current hidden threads configuration."
7465   (save-excursion
7466     (let (config)
7467       (goto-char (point-min))
7468       (while (search-forward "\r" nil t)
7469         (push (1- (point)) config))
7470       config)))
7471
7472 (defun gnus-restore-hidden-threads-configuration (config)
7473   "Restore hidden threads configuration from CONFIG."
7474   (let (point buffer-read-only)
7475     (while (setq point (pop config))
7476       (when (and (< point (point-max))
7477                  (goto-char point)
7478                  (= (following-char) ?\n))
7479         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7480
7481 ;; Various summary mode internalish functions.
7482
7483 (defun gnus-mouse-pick-article (e)
7484   (interactive "e")
7485   (mouse-set-point e)
7486   (gnus-summary-next-page nil t))
7487
7488 (defun gnus-summary-setup-buffer (group)
7489   "Initialize summary buffer."
7490   (let ((buffer (concat "*Summary " group "*")))
7491     (if (get-buffer buffer)
7492         (progn
7493           (set-buffer buffer)
7494           (setq gnus-summary-buffer (current-buffer))
7495           (not gnus-newsgroup-prepared))
7496       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7497       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7498       (gnus-add-current-to-buffer-list)
7499       (gnus-summary-mode group)
7500       (when gnus-carpal
7501         (gnus-carpal-setup-buffer 'summary))
7502       (unless gnus-single-article-buffer
7503         (make-local-variable 'gnus-article-buffer)
7504         (make-local-variable 'gnus-article-current)
7505         (make-local-variable 'gnus-original-article-buffer))
7506       (setq gnus-newsgroup-name group)
7507       t)))
7508
7509 (defun gnus-set-global-variables ()
7510   ;; Set the global equivalents of the summary buffer-local variables
7511   ;; to the latest values they had.  These reflect the summary buffer
7512   ;; that was in action when the last article was fetched.
7513   (when (eq major-mode 'gnus-summary-mode)
7514     (setq gnus-summary-buffer (current-buffer))
7515     (let ((name gnus-newsgroup-name)
7516           (marked gnus-newsgroup-marked)
7517           (unread gnus-newsgroup-unreads)
7518           (headers gnus-current-headers)
7519           (data gnus-newsgroup-data)
7520           (summary gnus-summary-buffer)
7521           (article-buffer gnus-article-buffer)
7522           (original gnus-original-article-buffer)
7523           (gac gnus-article-current)
7524           (score-file gnus-current-score-file))
7525       (save-excursion
7526         (set-buffer gnus-group-buffer)
7527         (setq gnus-newsgroup-name name)
7528         (setq gnus-newsgroup-marked marked)
7529         (setq gnus-newsgroup-unreads unread)
7530         (setq gnus-current-headers headers)
7531         (setq gnus-newsgroup-data data)
7532         (setq gnus-article-current gac)
7533         (setq gnus-summary-buffer summary)
7534         (setq gnus-article-buffer article-buffer)
7535         (setq gnus-original-article-buffer original)
7536         (setq gnus-current-score-file score-file)))))
7537
7538 (defun gnus-summary-last-article-p (&optional article)
7539   "Return whether ARTICLE is the last article in the buffer."
7540   (if (not (setq article (or article (gnus-summary-article-number))))
7541       t ; All non-existant numbers are the last article. :-)
7542     (not (cdr (gnus-data-find-list article)))))
7543
7544 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7545   "Insert a dummy root in the summary buffer."
7546   (beginning-of-line)
7547   (gnus-add-text-properties
7548    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7549    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7550
7551 (defun gnus-make-thread-indent-array ()
7552   (let ((n 200))
7553     (unless (and gnus-thread-indent-array
7554                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7555       (setq gnus-thread-indent-array (make-vector 201 "")
7556             gnus-thread-indent-array-level gnus-thread-indent-level)
7557       (while (>= n 0)
7558         (aset gnus-thread-indent-array n
7559               (make-string (* n gnus-thread-indent-level) ? ))
7560         (setq n (1- n))))))
7561
7562 (defun gnus-summary-insert-line
7563   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7564                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7565                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7566   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7567          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7568          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7569          (gnus-tmp-score-char
7570           (if (or (null gnus-summary-default-score)
7571                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7572                       gnus-summary-zcore-fuzz)) ? 
7573             (if (< gnus-tmp-score gnus-summary-default-score)
7574                 gnus-score-below-mark gnus-score-over-mark)))
7575          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7576                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7577                                   gnus-cached-mark)
7578                                  (gnus-tmp-replied gnus-replied-mark)
7579                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7580                                   gnus-saved-mark)
7581                                  (t gnus-unread-mark)))
7582          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7583          (gnus-tmp-name
7584           (cond
7585            ((string-match "(.+)" gnus-tmp-from)
7586             (substring gnus-tmp-from
7587                        (1+ (match-beginning 0)) (1- (match-end 0))))
7588            ((string-match "<[^>]+> *$" gnus-tmp-from)
7589             (let ((beg (match-beginning 0)))
7590               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7591                        (substring gnus-tmp-from (1+ (match-beginning 0))
7592                                   (1- (match-end 0))))
7593                   (substring gnus-tmp-from 0 beg))))
7594            (t gnus-tmp-from)))
7595          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7596          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7597          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7598          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7599          (buffer-read-only nil))
7600     (when (string= gnus-tmp-name "")
7601       (setq gnus-tmp-name gnus-tmp-from))
7602     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7603     (gnus-put-text-property
7604      (point)
7605      (progn (eval gnus-summary-line-format-spec) (point))
7606      'gnus-number gnus-tmp-number)
7607     (when (gnus-visual-p 'summary-highlight 'highlight)
7608       (forward-line -1)
7609       (run-hooks 'gnus-summary-update-hook)
7610       (forward-line 1))))
7611
7612 (defun gnus-summary-update-line (&optional dont-update)
7613   ;; Update summary line after change.
7614   (when (and gnus-summary-default-score
7615              (not gnus-summary-inhibit-highlight))
7616     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7617            (article (gnus-summary-article-number))
7618            (score (gnus-summary-article-score article)))
7619       (unless dont-update
7620         (if (and gnus-summary-mark-below
7621                  (< (gnus-summary-article-score)
7622                     gnus-summary-mark-below))
7623             ;; This article has a low score, so we mark it as read.
7624             (when (memq article gnus-newsgroup-unreads)
7625               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7626           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7627             ;; This article was previously marked as read on account
7628             ;; of a low score, but now it has risen, so we mark it as
7629             ;; unread.
7630             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7631         (gnus-summary-update-mark
7632          (if (or (null gnus-summary-default-score)
7633                  (<= (abs (- score gnus-summary-default-score))
7634                      gnus-summary-zcore-fuzz)) ? 
7635            (if (< score gnus-summary-default-score)
7636                gnus-score-below-mark gnus-score-over-mark)) 'score))
7637       ;; Do visual highlighting.
7638       (when (gnus-visual-p 'summary-highlight 'highlight)
7639         (run-hooks 'gnus-summary-update-hook)))))
7640
7641 (defvar gnus-tmp-new-adopts nil)
7642
7643 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7644   ;; Sum up all elements (and sub-elements) in a list.
7645   (let* ((number
7646           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7647           (cond
7648            ((and (consp thread) (cdr thread))
7649             (apply
7650              '+ 1 (mapcar
7651                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7652            ((null thread)
7653             1)
7654            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7655             1)
7656            (t 0))))
7657     (when (and level (zerop level) gnus-tmp-new-adopts)
7658       (incf number
7659             (apply '+ (mapcar
7660                        'gnus-summary-number-of-articles-in-thread
7661                        gnus-tmp-new-adopts))))
7662     (if char
7663         (if (> number 1) gnus-not-empty-thread-mark
7664           gnus-empty-thread-mark)
7665       number)))
7666
7667 (defun gnus-summary-set-local-parameters (group)
7668  "Go through the local params of GROUP and set all variable specs in that list."
7669   (let ((params (gnus-info-params (gnus-get-info group)))
7670         elem)
7671     (while params
7672       (setq elem (car params)
7673             params (cdr params))
7674       (and (consp elem)                 ; Has to be a cons.
7675            (consp (cdr elem))           ; The cdr has to be a list.
7676            (symbolp (car elem))         ; Has to be a symbol in there.
7677            (not (memq (car elem) 
7678                       '(quit-config to-address to-list to-group)))
7679            (progn                       ; So we set it.
7680              (make-local-variable (car elem))
7681              (set (car elem) (eval (nth 1 elem))))))))
7682
7683 (defun gnus-summary-read-group (group &optional show-all no-article
7684                                       kill-buffer no-display)
7685   "Start reading news in newsgroup GROUP.
7686 If SHOW-ALL is non-nil, already read articles are also listed.
7687 If NO-ARTICLE is non-nil, no article is selected initially.
7688 If NO-DISPLAY, don't generate a summary buffer."
7689   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7690   (let* ((new-group (gnus-summary-setup-buffer group))
7691          (quit-config (gnus-group-quit-config group))
7692          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7693     (cond
7694      ;; This summary buffer exists already, so we just select it.
7695      ((not new-group)
7696       (gnus-set-global-variables)
7697       (when kill-buffer
7698         (gnus-kill-or-deaden-summary kill-buffer))
7699       (gnus-configure-windows 'summary 'force)
7700       (gnus-set-mode-line 'summary)
7701       (gnus-summary-position-point)
7702       (message "")
7703       t)
7704      ;; We couldn't select this group.
7705      ((null did-select)
7706       (when (and (eq major-mode 'gnus-summary-mode)
7707                  (not (equal (current-buffer) kill-buffer)))
7708         (kill-buffer (current-buffer))
7709         (if (not quit-config)
7710             (progn
7711               (set-buffer gnus-group-buffer)
7712               (gnus-group-jump-to-group group)
7713               (gnus-group-next-unread-group 1))
7714           (if (not (buffer-name (car quit-config)))
7715               (gnus-configure-windows 'group 'force)
7716             (set-buffer (car quit-config))
7717             (and (eq major-mode 'gnus-summary-mode)
7718                  (gnus-set-global-variables))
7719             (gnus-configure-windows (cdr quit-config)))))
7720       (gnus-message 3 "Can't select group")
7721       nil)
7722      ;; The user did a `C-g' while prompting for number of articles,
7723      ;; so we exit this group.
7724      ((eq did-select 'quit)
7725       (and (eq major-mode 'gnus-summary-mode)
7726            (not (equal (current-buffer) kill-buffer))
7727            (kill-buffer (current-buffer)))
7728       (when kill-buffer
7729         (gnus-kill-or-deaden-summary kill-buffer))
7730       (if (not quit-config)
7731           (progn
7732             (set-buffer gnus-group-buffer)
7733             (gnus-group-jump-to-group group)
7734             (gnus-group-next-unread-group 1)
7735             (gnus-configure-windows 'group 'force))
7736         (if (not (buffer-name (car quit-config)))
7737             (gnus-configure-windows 'group 'force)
7738           (set-buffer (car quit-config))
7739           (and (eq major-mode 'gnus-summary-mode)
7740                (gnus-set-global-variables))
7741           (gnus-configure-windows (cdr quit-config))))
7742       ;; Finally signal the quit.
7743       (signal 'quit nil))
7744      ;; The group was successfully selected.
7745      (t
7746       (gnus-set-global-variables)
7747       ;; Save the active value in effect when the group was entered.
7748       (setq gnus-newsgroup-active
7749             (gnus-copy-sequence
7750              (gnus-active gnus-newsgroup-name)))
7751       ;; You can change the summary buffer in some way with this hook.
7752       (run-hooks 'gnus-select-group-hook)
7753       ;; Set any local variables in the group parameters.
7754       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7755       (gnus-update-format-specifications)
7756       ;; Do score processing.
7757       (when gnus-use-scoring
7758         (gnus-possibly-score-headers))
7759       ;; Check whether to fill in the gaps in the threads.
7760       (when gnus-build-sparse-threads
7761         (gnus-build-sparse-threads))
7762       ;; Find the initial limit.
7763       (if gnus-show-threads
7764           (if show-all
7765               (let ((gnus-newsgroup-dormant nil))
7766                 (gnus-summary-initial-limit show-all))
7767             (gnus-summary-initial-limit show-all))
7768         (setq gnus-newsgroup-limit 
7769               (mapcar 
7770                (lambda (header) (mail-header-number header))
7771                gnus-newsgroup-headers)))
7772       ;; Generate the summary buffer.
7773       (unless no-display
7774         (gnus-summary-prepare))
7775       (when gnus-use-trees
7776         (gnus-tree-open group)
7777         (setq gnus-summary-highlight-line-function
7778               'gnus-tree-highlight-article))
7779       ;; If the summary buffer is empty, but there are some low-scored
7780       ;; articles or some excluded dormants, we include these in the
7781       ;; buffer.
7782       (when (and (zerop (buffer-size))
7783                  (not no-display))
7784         (cond (gnus-newsgroup-dormant
7785                (gnus-summary-limit-include-dormant))
7786               ((and gnus-newsgroup-scored show-all)
7787                (gnus-summary-limit-include-expunged))))
7788       ;; Function `gnus-apply-kill-file' must be called in this hook.
7789       (run-hooks 'gnus-apply-kill-hook)
7790       (if (and (zerop (buffer-size))
7791                (not no-display))
7792           (progn
7793             ;; This newsgroup is empty.
7794             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7795             (gnus-message 6 "No unread news")
7796             (when kill-buffer
7797               (gnus-kill-or-deaden-summary kill-buffer))
7798             ;; Return nil from this function.
7799             nil)
7800         ;; Hide conversation thread subtrees.  We cannot do this in
7801         ;; gnus-summary-prepare-hook since kill processing may not
7802         ;; work with hidden articles.
7803         (and gnus-show-threads
7804              gnus-thread-hide-subtree
7805              (gnus-summary-hide-all-threads))
7806         ;; Show first unread article if requested.
7807         (if (and (not no-article)
7808                  (not no-display)
7809                  gnus-newsgroup-unreads
7810                  gnus-auto-select-first)
7811             (unless (if (eq gnus-auto-select-first 'best)
7812                         (gnus-summary-best-unread-article)
7813                       (gnus-summary-first-unread-article))
7814               (gnus-configure-windows 'summary))
7815           ;; Don't select any articles, just move point to the first
7816           ;; article in the group.
7817           (goto-char (point-min))
7818           (gnus-summary-position-point)
7819           (gnus-set-mode-line 'summary)
7820           (gnus-configure-windows 'summary 'force))
7821         ;; If we are in async mode, we send some info to the backend.
7822         (when gnus-newsgroup-async
7823           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7824         (when kill-buffer
7825           (gnus-kill-or-deaden-summary kill-buffer))
7826         (when (get-buffer-window gnus-group-buffer t)
7827           ;; Gotta use windows, because recenter does wierd stuff if
7828           ;; the current buffer ain't the displayed window.
7829           (let ((owin (selected-window)))
7830             (select-window (get-buffer-window gnus-group-buffer t))
7831             (when (gnus-group-goto-group group)
7832               (recenter))
7833             (select-window owin))))
7834       ;; Mark this buffer as "prepared".
7835       (setq gnus-newsgroup-prepared t)
7836       t))))
7837
7838 (defun gnus-summary-prepare ()
7839   "Generate the summary buffer."
7840   (let ((buffer-read-only nil))
7841     (erase-buffer)
7842     (setq gnus-newsgroup-data nil
7843           gnus-newsgroup-data-reverse nil)
7844     (run-hooks 'gnus-summary-generate-hook)
7845     ;; Generate the buffer, either with threads or without.
7846     (when gnus-newsgroup-headers
7847       (gnus-summary-prepare-threads
7848        (if gnus-show-threads
7849            (gnus-sort-gathered-threads
7850             (funcall gnus-summary-thread-gathering-function
7851                      (gnus-sort-threads
7852                       (gnus-cut-threads (gnus-make-threads)))))
7853          ;; Unthreaded display.
7854          (gnus-sort-articles gnus-newsgroup-headers))))
7855     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7856     ;; Call hooks for modifying summary buffer.
7857     (goto-char (point-min))
7858     (run-hooks 'gnus-summary-prepare-hook)))
7859
7860 (defun gnus-gather-threads-by-subject (threads)
7861   "Gather threads by looking at Subject headers."
7862   (if (not gnus-summary-make-false-root)
7863       threads
7864     (let ((hashtb (gnus-make-hashtable 1023))
7865           (prev threads)
7866           (result threads)
7867           subject hthread whole-subject)
7868       (while threads
7869         (setq whole-subject (mail-header-subject (caar threads)))
7870         (setq subject
7871               (cond
7872                ;; Truncate the subject.
7873                ((numberp gnus-summary-gather-subject-limit)
7874                 (setq subject (gnus-simplify-subject-re whole-subject))
7875                 (if (> (length subject) gnus-summary-gather-subject-limit)
7876                     (substring subject 0 gnus-summary-gather-subject-limit)
7877                   subject))
7878                ;; Fuzzily simplify it.
7879                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7880                 (gnus-simplify-subject-fuzzy whole-subject))
7881                ;; Just remove the leading "Re:".
7882                (t
7883                 (gnus-simplify-subject-re whole-subject))))
7884
7885         (if (and gnus-summary-gather-exclude-subject
7886                  (string-match gnus-summary-gather-exclude-subject
7887                                subject))
7888             ()          ; We don't want to do anything with this article.
7889           ;; We simplify the subject before looking it up in the
7890           ;; hash table.
7891
7892           (if (setq hthread (gnus-gethash subject hashtb))
7893               (progn
7894                 ;; We enter a dummy root into the thread, if we
7895                 ;; haven't done that already.
7896                 (unless (stringp (caar hthread))
7897                   (setcar hthread (list whole-subject (car hthread))))
7898                 ;; We add this new gathered thread to this gathered
7899                 ;; thread.
7900                 (setcdr (car hthread)
7901                         (nconc (cdar hthread) (list (car threads))))
7902                 ;; Remove it from the list of threads.
7903                 (setcdr prev (cdr threads))
7904                 (setq threads prev))
7905             ;; Enter this thread into the hash table.
7906             (gnus-sethash subject threads hashtb)))
7907         (setq prev threads)
7908         (setq threads (cdr threads)))
7909       result)))
7910
7911 (defun gnus-gather-threads-by-references (threads)
7912   "Gather threads by looking at References headers."
7913   (let ((idhashtb (gnus-make-hashtable 1023))
7914         (thhashtb (gnus-make-hashtable 1023))
7915         (prev threads)
7916         (result threads)
7917         ids references id gthread gid entered)
7918     (while threads
7919       (when (setq references (mail-header-references (caar threads)))
7920         (setq id (mail-header-id (caar threads)))
7921         (setq ids (gnus-split-references references))
7922         (setq entered nil)
7923         (while ids
7924           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7925               (progn
7926                 (gnus-sethash (car ids) id idhashtb)
7927                 (gnus-sethash id threads thhashtb))
7928             (setq gthread (gnus-gethash gid thhashtb))
7929             (unless entered
7930               ;; We enter a dummy root into the thread, if we
7931               ;; haven't done that already.
7932               (unless (stringp (caar gthread))
7933                 (setcar gthread (list (mail-header-subject (caar gthread))
7934                                       (car gthread))))
7935               ;; We add this new gathered thread to this gathered
7936               ;; thread.
7937               (setcdr (car gthread)
7938                       (nconc (cdar gthread) (list (car threads)))))
7939             ;; Add it into the thread hash table.
7940             (gnus-sethash id gthread thhashtb)
7941             (setq entered t)
7942             ;; Remove it from the list of threads.
7943             (setcdr prev (cdr threads))
7944             (setq threads prev))
7945           (setq ids (cdr ids))))
7946       (setq prev threads)
7947       (setq threads (cdr threads)))
7948     result))
7949
7950 (defun gnus-sort-gathered-threads (threads)
7951   "Sort subtreads inside each gathered thread by article number."
7952   (let ((result threads))
7953     (while threads
7954       (when (stringp (caar threads))
7955         (setcdr (car threads)
7956                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7957       (setq threads (cdr threads)))
7958     result))
7959
7960 (defun gnus-make-threads ()
7961   "Go through the dependency hashtb and find the roots.  Return all threads."
7962   (let (threads)
7963     (mapatoms
7964      (lambda (refs)
7965        (unless (car (symbol-value refs))
7966          ;; These threads do not refer back to any other articles,
7967          ;; so they're roots.
7968          (setq threads (append (cdr (symbol-value refs)) threads))))
7969      gnus-newsgroup-dependencies)
7970     threads))
7971
7972 (defun gnus-build-sparse-threads ()
7973   (let ((headers gnus-newsgroup-headers)
7974         (deps gnus-newsgroup-dependencies)
7975         header references generation relations 
7976         cthread subject child end pthread relation)
7977     ;; First we create an alist of generations/relations, where 
7978     ;; generations is how much we trust the ralation, and the relation
7979     ;; is parent/child.
7980     (gnus-message 7 "Making sparse threads...")
7981     (save-excursion
7982       (nnheader-set-temp-buffer " *gnus sparse threads*")
7983       (while (setq header (pop headers))
7984         (when (and (setq references (mail-header-references header))
7985                    (not (string= references "")))
7986           (insert references)
7987           (setq child (mail-header-id header)
7988                 subject (mail-header-subject header))
7989           (setq generation 0)
7990           (while (search-backward ">" nil t)
7991             (setq end (1+ (point)))
7992             (when (search-backward "<" nil t)
7993               (push (list (incf generation) 
7994                           child (setq child (buffer-substring (point) end))
7995                           subject)
7996                     relations)))
7997           (push (list (1+ generation) child nil subject) relations)
7998           (erase-buffer)))
7999       (kill-buffer (current-buffer)))
8000     ;; Sort over trustworthiness.
8001     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8002     (while (setq relation (pop relations))
8003       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8004                 (unless (car (symbol-value cthread))
8005                   ;; Make this article the parent of these threads.
8006                   (setcar (symbol-value cthread)
8007                           (vector gnus-reffed-article-number 
8008                                   (cadddr relation) 
8009                                   "" ""
8010                                   (cadr relation) 
8011                                   (or (caddr relation) "") 0 0 "")))
8012               (set cthread (list (vector gnus-reffed-article-number
8013                                          (cadddr relation) 
8014                                          "" "" (cadr relation) 
8015                                          (or (caddr relation) "") 0 0 ""))))
8016         (push gnus-reffed-article-number gnus-newsgroup-limit)
8017         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8018         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8019               gnus-newsgroup-reads)
8020         (decf gnus-reffed-article-number)
8021         ;; Make this new thread the child of its parent.
8022         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8023             (setcdr (symbol-value pthread)
8024                     (nconc (cdr (symbol-value pthread))
8025                            (list (symbol-value cthread))))
8026           (set pthread (list nil (symbol-value cthread))))))
8027     (gnus-message 7 "Making sparse threads...done")))
8028
8029 (defun gnus-build-old-threads ()
8030   ;; Look at all the articles that refer back to old articles, and
8031   ;; fetch the headers for the articles that aren't there.  This will
8032   ;; build complete threads - if the roots haven't been expired by the
8033   ;; server, that is.
8034   (let (id heads)
8035     (mapatoms
8036      (lambda (refs)
8037        (when (not (car (symbol-value refs)))
8038          (setq heads (cdr (symbol-value refs)))
8039          (while heads
8040            (if (memq (mail-header-number (caar heads))
8041                      gnus-newsgroup-dormant)
8042                (setq heads (cdr heads))
8043              (setq id (symbol-name refs))
8044              (while (and (setq id (gnus-build-get-header id))
8045                          (not (car (gnus-gethash
8046                                     id gnus-newsgroup-dependencies)))))
8047              (setq heads nil)))))
8048      gnus-newsgroup-dependencies)))
8049
8050 (defun gnus-build-get-header (id)
8051   ;; Look through the buffer of NOV lines and find the header to
8052   ;; ID.  Enter this line into the dependencies hash table, and return
8053   ;; the id of the parent article (if any).
8054   (let ((deps gnus-newsgroup-dependencies)
8055         found header)
8056     (prog1
8057         (save-excursion
8058           (set-buffer nntp-server-buffer)
8059           (goto-char (point-min))
8060           (while (and (not found) (search-forward id nil t))
8061             (beginning-of-line)
8062             (setq found (looking-at
8063                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8064                                  (regexp-quote id))))
8065             (or found (beginning-of-line 2)))
8066           (when found
8067             (beginning-of-line)
8068             (and
8069              (setq header (gnus-nov-parse-line
8070                            (read (current-buffer)) deps))
8071              (gnus-parent-id (mail-header-references header)))))
8072       (when header
8073         (let ((number (mail-header-number header)))
8074           (push number gnus-newsgroup-limit)
8075           (push header gnus-newsgroup-headers)
8076           (if (memq number gnus-newsgroup-unselected)
8077               (progn
8078                 (push number gnus-newsgroup-unreads)
8079                 (setq gnus-newsgroup-unselected
8080                       (delq number gnus-newsgroup-unselected)))
8081             (push number gnus-newsgroup-ancient)))))))
8082
8083 (defun gnus-summary-update-article (article &optional iheader)
8084   "Update ARTICLE in the summary buffer."
8085   (set-buffer gnus-summary-buffer)
8086   (let* ((header (or iheader (gnus-summary-article-header article)))
8087          (id (mail-header-id header))
8088          (data (gnus-data-find article))
8089          (thread (gnus-id-to-thread id))
8090          (references (mail-header-references header))
8091          (parent
8092           (gnus-id-to-thread
8093            (or (gnus-parent-id 
8094                 (if (and references
8095                          (not (equal "" references)))
8096                     references))
8097                "none")))
8098          (buffer-read-only nil)
8099          (old (car thread))
8100          (number (mail-header-number header))
8101          pos)
8102     (when thread
8103       ;; !!! Should this be in or not?
8104       (unless iheader
8105         (setcar thread nil))
8106       (when parent
8107         (delq thread parent))
8108       (if (gnus-summary-insert-subject id header iheader)
8109           ;; Set the (possibly) new article number in the data structure.
8110           (gnus-data-set-number data (gnus-id-to-article id))
8111         (setcar thread old)
8112         nil))))
8113
8114 (defun gnus-rebuild-thread (id)
8115   "Rebuild the thread containing ID."
8116   (let ((buffer-read-only nil)
8117         current thread data)
8118     (if (not gnus-show-threads)
8119         (setq thread (list (car (gnus-id-to-thread id))))
8120       ;; Get the thread this article is part of.
8121       (setq thread (gnus-remove-thread id)))
8122     (setq current (save-excursion
8123                     (and (zerop (forward-line -1))
8124                          (gnus-summary-article-number))))
8125     ;; If this is a gathered thread, we have to go some re-gathering.
8126     (when (stringp (car thread))
8127       (let ((subject (car thread))
8128             roots thr)
8129         (setq thread (cdr thread))
8130         (while thread
8131           (unless (memq (setq thr (gnus-id-to-thread
8132                                       (gnus-root-id
8133                                        (mail-header-id (caar thread)))))
8134                         roots)
8135             (push thr roots))
8136           (setq thread (cdr thread)))
8137         ;; We now have all (unique) roots.
8138         (if (= (length roots) 1)
8139             ;; All the loose roots are now one solid root.
8140             (setq thread (car roots))
8141           (setq thread (cons subject (gnus-sort-threads roots))))))
8142     (let (threads)
8143       ;; We then insert this thread into the summary buffer.
8144       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8145         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8146         (setq data (nreverse gnus-newsgroup-data))
8147         (setq threads gnus-newsgroup-threads))
8148       ;; We splice the new data into the data structure.
8149       (gnus-data-enter-list current data)
8150       (gnus-data-compute-positions)
8151       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8152
8153 (defun gnus-number-to-header (number)
8154   "Return the header for article NUMBER."
8155   (let ((headers gnus-newsgroup-headers))
8156     (while (and headers
8157                 (not (= number (mail-header-number (car headers)))))
8158       (pop headers))
8159     (when headers
8160       (car headers))))
8161
8162 (defun gnus-id-to-thread (id)
8163   "Return the (sub-)thread where ID appears."
8164   (gnus-gethash id gnus-newsgroup-dependencies))
8165
8166 (defun gnus-id-to-article (id)
8167   "Return the article number of ID."
8168   (let ((thread (gnus-id-to-thread id)))
8169     (when (and thread
8170                (car thread))
8171       (mail-header-number (car thread)))))
8172
8173 (defun gnus-id-to-header (id)
8174   "Return the article headers of ID."
8175   (car (gnus-id-to-thread id)))
8176
8177 (defun gnus-article-displayed-root-p (article)
8178   "Say whether ARTICLE is a root(ish) article."
8179   (let ((level (gnus-summary-thread-level article))
8180         (refs (mail-header-references  (gnus-summary-article-header article)))
8181         particle)
8182     (cond 
8183      ((null level) nil)
8184      ((zerop level) t)
8185      ((null refs) t)
8186      ((null (gnus-parent-id refs)) t)
8187      ((and (= 1 level)
8188            (null (setq particle (gnus-id-to-article
8189                                  (gnus-parent-id refs))))
8190            (null (gnus-summary-thread-level particle)))))))
8191
8192 (defun gnus-root-id (id)
8193   "Return the id of the root of the thread where ID appears."
8194   (let (last-id prev)
8195     (while (and id (setq prev (car (gnus-gethash 
8196                                     id gnus-newsgroup-dependencies))))
8197       (setq last-id id
8198             id (gnus-parent-id (mail-header-references prev))))
8199     last-id))
8200
8201 (defun gnus-remove-thread (id &optional dont-remove)
8202   "Remove the thread that has ID in it."
8203   (let ((dep gnus-newsgroup-dependencies)
8204         headers thread last-id)
8205     ;; First go up in this thread until we find the root.
8206     (setq last-id (gnus-root-id id))
8207     (setq headers (list (car (gnus-id-to-thread last-id))
8208                         (caadr (gnus-id-to-thread last-id))))
8209     ;; We have now found the real root of this thread.  It might have
8210     ;; been gathered into some loose thread, so we have to search
8211     ;; through the threads to find the thread we wanted.
8212     (let ((threads gnus-newsgroup-threads)
8213           sub)
8214       (while threads
8215         (setq sub (car threads))
8216         (if (stringp (car sub))
8217             ;; This is a gathered threads, so we look at the roots
8218             ;; below it to find whether this article in in this
8219             ;; gathered root.
8220             (progn
8221               (setq sub (cdr sub))
8222               (while sub
8223                 (when (member (caar sub) headers)
8224                   (setq thread (car threads)
8225                         threads nil
8226                         sub nil))
8227                 (setq sub (cdr sub))))
8228           ;; It's an ordinary thread, so we check it.
8229           (when (eq (car sub) (car headers))
8230             (setq thread sub
8231                   threads nil)))
8232         (setq threads (cdr threads)))
8233       ;; If this article is in no thread, then it's a root.
8234       (if thread
8235           (unless dont-remove
8236             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8237         (setq thread (gnus-gethash last-id dep)))
8238       (when thread
8239         (prog1
8240             thread ; We return this thread.
8241           (unless dont-remove
8242             (if (stringp (car thread))
8243                 (progn
8244                   ;; If we use dummy roots, then we have to remove the
8245                   ;; dummy root as well.
8246                   (when (eq gnus-summary-make-false-root 'dummy)
8247                     ;; Uhm.
8248                     )
8249                   (setq thread (cdr thread))
8250                   (while thread
8251                     (gnus-remove-thread-1 (car thread))
8252                     (setq thread (cdr thread))))
8253               (gnus-remove-thread-1 thread))))))))
8254
8255 (defun gnus-remove-thread-1 (thread)
8256   "Remove the thread THREAD recursively."
8257   (let ((number (mail-header-number (car thread)))
8258         pos)
8259     (when (setq pos (text-property-any
8260                      (point-min) (point-max) 'gnus-number number))
8261       (goto-char pos)
8262       (gnus-delete-line)
8263       (gnus-data-remove number))
8264     (setq thread (cdr thread))
8265     (while thread
8266       (gnus-remove-thread-1 (pop thread)))))
8267
8268 (defun gnus-sort-threads (threads)
8269   "Sort THREADS."
8270   (if (not gnus-thread-sort-functions)
8271       threads
8272     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8273                     (car gnus-thread-sort-functions)
8274                   `(lambda (t1 t2)
8275                      ,(gnus-make-sort-function 
8276                        (reverse gnus-thread-sort-functions))))))
8277       (gnus-message 7 "Sorting threads...")
8278       (prog1
8279           (sort threads func)
8280         (gnus-message 7 "Sorting threads...done")))))
8281
8282 (defun gnus-sort-articles (articles)
8283   "Sort ARTICLES."
8284   (when gnus-article-sort-functions
8285     (let ((func (if (= 1 (length gnus-article-sort-functions))
8286                     (car gnus-article-sort-functions)
8287                   `(lambda (t1 t2)
8288                      ,(gnus-make-sort-function 
8289                        (reverse gnus-article-sort-functions))))))
8290       (gnus-message 7 "Sorting articles...")
8291       (prog1
8292           (setq gnus-newsgroup-headers (sort articles func))
8293         (gnus-message 7 "Sorting articles...done")))))
8294
8295 (defun gnus-make-sort-function (funs)
8296   "Return a composite sort condition based on the functions in FUNC."
8297   (if (cdr funs)
8298       `(or (,(car funs) t1 t2)
8299            (and (not (,(car funs) t2 t1))
8300                 ,(gnus-make-sort-function (cdr funs))))
8301     `(,(car funs) t1 t2)))
8302                  
8303 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8304 (defmacro gnus-thread-header (thread)
8305   ;; Return header of first article in THREAD.
8306   ;; Note that THREAD must never, ever be anything else than a variable -
8307   ;; using some other form will lead to serious barfage.
8308   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8309   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8310   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8311         (vector thread) 2))
8312
8313 (defsubst gnus-article-sort-by-number (h1 h2)
8314   "Sort articles by article number."
8315   (< (mail-header-number h1)
8316      (mail-header-number h2)))
8317
8318 (defun gnus-thread-sort-by-number (h1 h2)
8319   "Sort threads by root article number."
8320   (gnus-article-sort-by-number
8321    (gnus-thread-header h1) (gnus-thread-header h2)))
8322
8323 (defsubst gnus-article-sort-by-author (h1 h2)
8324   "Sort articles by root author."
8325   (string-lessp
8326    (let ((extract (funcall
8327                    gnus-extract-address-components
8328                    (mail-header-from h1))))
8329      (or (car extract) (cdr extract)))
8330    (let ((extract (funcall
8331                    gnus-extract-address-components
8332                    (mail-header-from h2))))
8333      (or (car extract) (cdr extract)))))
8334
8335 (defun gnus-thread-sort-by-author (h1 h2)
8336   "Sort threads by root author."
8337   (gnus-article-sort-by-author
8338    (gnus-thread-header h1)  (gnus-thread-header h2)))
8339
8340 (defsubst gnus-article-sort-by-subject (h1 h2)
8341   "Sort articles by root subject."
8342   (string-lessp
8343    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8344    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8345
8346 (defun gnus-thread-sort-by-subject (h1 h2)
8347   "Sort threads by root subject."
8348   (gnus-article-sort-by-subject
8349    (gnus-thread-header h1) (gnus-thread-header h2)))
8350
8351 (defsubst gnus-article-sort-by-date (h1 h2)
8352   "Sort articles by root article date."
8353   (string-lessp
8354    (inline (gnus-sortable-date (mail-header-date h1)))
8355    (inline (gnus-sortable-date (mail-header-date h2)))))
8356
8357 (defun gnus-thread-sort-by-date (h1 h2)
8358   "Sort threads by root article date."
8359   (gnus-article-sort-by-date
8360    (gnus-thread-header h1) (gnus-thread-header h2)))
8361
8362 (defsubst gnus-article-sort-by-score (h1 h2)
8363   "Sort articles by root article score.
8364 Unscored articles will be counted as having a score of zero."
8365   (> (or (cdr (assq (mail-header-number h1)
8366                     gnus-newsgroup-scored))
8367          gnus-summary-default-score 0)
8368      (or (cdr (assq (mail-header-number h2)
8369                     gnus-newsgroup-scored))
8370          gnus-summary-default-score 0)))
8371
8372 (defun gnus-thread-sort-by-score (h1 h2)
8373   "Sort threads by root article score."
8374   (gnus-article-sort-by-score
8375    (gnus-thread-header h1) (gnus-thread-header h2)))
8376
8377 (defun gnus-thread-sort-by-total-score (h1 h2)
8378   "Sort threads by the sum of all scores in the thread.
8379 Unscored articles will be counted as having a score of zero."
8380   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8381
8382 (defun gnus-thread-total-score (thread)
8383   ;;  This function find the total score of THREAD.
8384   (cond ((null thread)
8385          0)
8386         ((consp thread)
8387          (if (stringp (car thread))
8388              (apply gnus-thread-score-function 0
8389                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8390            (gnus-thread-total-score-1 thread)))
8391         (t
8392          (gnus-thread-total-score-1 (list thread)))))
8393
8394 (defun gnus-thread-total-score-1 (root)
8395   ;; This function find the total score of the thread below ROOT.
8396   (setq root (car root))
8397   (apply gnus-thread-score-function
8398          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8399              gnus-summary-default-score 0)
8400          (mapcar 'gnus-thread-total-score
8401                  (cdr (gnus-gethash (mail-header-id root)
8402                                     gnus-newsgroup-dependencies)))))
8403
8404 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8405 (defvar gnus-tmp-prev-subject nil)
8406 (defvar gnus-tmp-false-parent nil)
8407 (defvar gnus-tmp-root-expunged nil)
8408 (defvar gnus-tmp-dummy-line nil)
8409
8410 (defun gnus-summary-prepare-threads (threads)
8411   "Prepare summary buffer from THREADS and indentation LEVEL.
8412 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8413 or a straight list of headers."
8414   (gnus-message 7 "Generating summary...")
8415
8416   (setq gnus-newsgroup-threads threads)
8417   (beginning-of-line)
8418
8419   (let ((gnus-tmp-level 0)
8420         (default-score (or gnus-summary-default-score 0))
8421         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8422         thread number subject stack state gnus-tmp-gathered beg-match
8423         new-roots gnus-tmp-new-adopts thread-end
8424         gnus-tmp-header gnus-tmp-unread
8425         gnus-tmp-replied gnus-tmp-subject-or-nil
8426         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8427         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8428         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8429
8430     (setq gnus-tmp-prev-subject nil)
8431
8432     (if (vectorp (car threads))
8433         ;; If this is a straight (sic) list of headers, then a
8434         ;; threaded summary display isn't required, so we just create
8435         ;; an unthreaded one.
8436         (gnus-summary-prepare-unthreaded threads)
8437
8438       ;; Do the threaded display.
8439
8440       (while (or threads stack gnus-tmp-new-adopts new-roots)
8441
8442         (if (and (= gnus-tmp-level 0)
8443                  (not (setq gnus-tmp-dummy-line nil))
8444                  (or (not stack)
8445                      (= (caar stack) 0))
8446                  (not gnus-tmp-false-parent)
8447                  (or gnus-tmp-new-adopts new-roots))
8448             (if gnus-tmp-new-adopts
8449                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8450                       thread (list (car gnus-tmp-new-adopts))
8451                       gnus-tmp-header (caar thread)
8452                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8453               (if new-roots
8454                   (setq thread (list (car new-roots))
8455                         gnus-tmp-header (caar thread)
8456                         new-roots (cdr new-roots))))
8457
8458           (if threads
8459               ;; If there are some threads, we do them before the
8460               ;; threads on the stack.
8461               (setq thread threads
8462                     gnus-tmp-header (caar thread))
8463             ;; There were no current threads, so we pop something off
8464             ;; the stack.
8465             (setq state (car stack)
8466                   gnus-tmp-level (car state)
8467                   thread (cdr state)
8468                   stack (cdr stack)
8469                   gnus-tmp-header (caar thread))))
8470
8471         (setq gnus-tmp-false-parent nil)
8472         (setq gnus-tmp-root-expunged nil)
8473         (setq thread-end nil)
8474
8475         (if (stringp gnus-tmp-header)
8476             ;; The header is a dummy root.
8477             (cond
8478              ((eq gnus-summary-make-false-root 'adopt)
8479               ;; We let the first article adopt the rest.
8480               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8481                                                (cddar thread)))
8482               (setq gnus-tmp-gathered
8483                     (nconc (mapcar
8484                             (lambda (h) (mail-header-number (car h)))
8485                             (cddar thread))
8486                            gnus-tmp-gathered))
8487               (setq thread (cons (list (caar thread)
8488                                        (cadar thread))
8489                                  (cdr thread)))
8490               (setq gnus-tmp-level -1
8491                     gnus-tmp-false-parent t))
8492              ((eq gnus-summary-make-false-root 'empty)
8493               ;; We print adopted articles with empty subject fields.
8494               (setq gnus-tmp-gathered
8495                     (nconc (mapcar
8496                             (lambda (h) (mail-header-number (car h)))
8497                             (cddar thread))
8498                            gnus-tmp-gathered))
8499               (setq gnus-tmp-level -1))
8500              ((eq gnus-summary-make-false-root 'dummy)
8501               ;; We remember that we probably want to output a dummy
8502               ;; root.
8503               (setq gnus-tmp-dummy-line gnus-tmp-header)
8504               (setq gnus-tmp-prev-subject gnus-tmp-header))
8505              (t
8506               ;; We do not make a root for the gathered
8507               ;; sub-threads at all.
8508               (setq gnus-tmp-level -1)))
8509
8510           (setq number (mail-header-number gnus-tmp-header)
8511                 subject (mail-header-subject gnus-tmp-header))
8512
8513           (cond
8514            ;; If the thread has changed subject, we might want to make
8515            ;; this subthread into a root.
8516            ((and (null gnus-thread-ignore-subject)
8517                  (not (zerop gnus-tmp-level))
8518                  gnus-tmp-prev-subject
8519                  (not (inline
8520                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8521             (setq new-roots (nconc new-roots (list (car thread)))
8522                   thread-end t
8523                   gnus-tmp-header nil))
8524            ;; If the article lies outside the current limit,
8525            ;; then we do not display it.
8526            ((and (not (memq number gnus-newsgroup-limit))
8527                  (not gnus-tmp-dummy-line))
8528             (setq gnus-tmp-gathered
8529                   (nconc (mapcar
8530                           (lambda (h) (mail-header-number (car h)))
8531                           (cdar thread))
8532                          gnus-tmp-gathered))
8533             (setq gnus-tmp-new-adopts (if (cdar thread)
8534                                           (append gnus-tmp-new-adopts
8535                                                   (cdar thread))
8536                                         gnus-tmp-new-adopts)
8537                   thread-end t
8538                   gnus-tmp-header nil)
8539             (when (zerop gnus-tmp-level)
8540               (setq gnus-tmp-root-expunged t)))
8541            ;; Perhaps this article is to be marked as read?
8542            ((and gnus-summary-mark-below
8543                  (< (or (cdr (assq number gnus-newsgroup-scored))
8544                         default-score)
8545                     gnus-summary-mark-below)
8546                  ;; Don't touch sparse articles.
8547                  (not (memq number gnus-newsgroup-sparse))
8548                  (not (memq number gnus-newsgroup-ancient)))
8549             (setq gnus-newsgroup-unreads
8550                   (delq number gnus-newsgroup-unreads))
8551             (if gnus-newsgroup-auto-expire
8552                 (push number gnus-newsgroup-expirable)
8553               (push (cons number gnus-low-score-mark)
8554                     gnus-newsgroup-reads))))
8555
8556           (when gnus-tmp-header
8557             ;; We may have an old dummy line to output before this
8558             ;; article.
8559             (when gnus-tmp-dummy-line
8560               (gnus-summary-insert-dummy-line
8561                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8562               (setq gnus-tmp-dummy-line nil))
8563
8564             ;; Compute the mark.
8565             (setq
8566              gnus-tmp-unread
8567              (cond
8568               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8569               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8570               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8571               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8572               (t (or (cdr (assq number gnus-newsgroup-reads))
8573                      gnus-ancient-mark))))
8574
8575             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8576                                   gnus-tmp-header gnus-tmp-level)
8577                   gnus-newsgroup-data)
8578
8579             ;; Actually insert the line.
8580             (setq
8581              gnus-tmp-subject-or-nil
8582              (cond
8583               ((and gnus-thread-ignore-subject
8584                     gnus-tmp-prev-subject
8585                     (not (inline (gnus-subject-equal
8586                                   gnus-tmp-prev-subject subject))))
8587                subject)
8588               ((zerop gnus-tmp-level)
8589                (if (and (eq gnus-summary-make-false-root 'empty)
8590                         (memq number gnus-tmp-gathered)
8591                         gnus-tmp-prev-subject
8592                         (inline (gnus-subject-equal
8593                                  gnus-tmp-prev-subject subject)))
8594                    gnus-summary-same-subject
8595                  subject))
8596               (t gnus-summary-same-subject)))
8597             (if (and (eq gnus-summary-make-false-root 'adopt)
8598                      (= gnus-tmp-level 1)
8599                      (memq number gnus-tmp-gathered))
8600                 (setq gnus-tmp-opening-bracket ?\<
8601                       gnus-tmp-closing-bracket ?\>)
8602               (setq gnus-tmp-opening-bracket ?\[
8603                     gnus-tmp-closing-bracket ?\]))
8604             (setq
8605              gnus-tmp-indentation
8606              (aref gnus-thread-indent-array gnus-tmp-level)
8607              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8608              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8609                                 gnus-summary-default-score 0)
8610              gnus-tmp-score-char
8611              (if (or (null gnus-summary-default-score)
8612                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8613                          gnus-summary-zcore-fuzz)) ? 
8614                (if (< gnus-tmp-score gnus-summary-default-score)
8615                    gnus-score-below-mark gnus-score-over-mark))
8616              gnus-tmp-replied
8617              (cond ((memq number gnus-newsgroup-processable)
8618                     gnus-process-mark)
8619                    ((memq number gnus-newsgroup-cached)
8620                     gnus-cached-mark)
8621                    ((memq number gnus-newsgroup-replied)
8622                     gnus-replied-mark)
8623                    (t gnus-unread-mark))
8624              gnus-tmp-from (mail-header-from gnus-tmp-header)
8625              gnus-tmp-name
8626              (cond
8627               ((string-match "(.+)" gnus-tmp-from)
8628                (substring gnus-tmp-from
8629                           (1+ (match-beginning 0)) (1- (match-end 0))))
8630               ((string-match "<[^>]+> *$" gnus-tmp-from)
8631                (setq beg-match (match-beginning 0))
8632                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8633                         (substring gnus-tmp-from (1+ (match-beginning 0))
8634                                    (1- (match-end 0))))
8635                    (substring gnus-tmp-from 0 beg-match)))
8636               (t gnus-tmp-from)))
8637             (when (string= gnus-tmp-name "")
8638               (setq gnus-tmp-name gnus-tmp-from))
8639             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8640             (gnus-put-text-property
8641              (point)
8642              (progn (eval gnus-summary-line-format-spec) (point))
8643              'gnus-number number)
8644             (when gnus-visual-p
8645               (forward-line -1)
8646               (run-hooks 'gnus-summary-update-hook)
8647               (forward-line 1))
8648
8649             (setq gnus-tmp-prev-subject subject)))
8650
8651         (when (nth 1 thread)
8652           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8653         (incf gnus-tmp-level)
8654         (setq threads (if thread-end nil (cdar thread)))
8655         (unless threads
8656           (setq gnus-tmp-level 0)))))
8657   (gnus-message 7 "Generating summary...done"))
8658
8659 (defun gnus-summary-prepare-unthreaded (headers)
8660   "Generate an unthreaded summary buffer based on HEADERS."
8661   (let (header number mark)
8662
8663     (while headers
8664       ;; We may have to root out some bad articles...
8665       (when (memq (setq number (mail-header-number
8666                                 (setq header (pop headers))))
8667                   gnus-newsgroup-limit)
8668         ;; Mark article as read when it has a low score.
8669         (when (and gnus-summary-mark-below
8670                    (< (or (cdr (assq number gnus-newsgroup-scored))
8671                           gnus-summary-default-score 0)
8672                       gnus-summary-mark-below)
8673                    (not (memq number gnus-newsgroup-ancient)))
8674           (setq gnus-newsgroup-unreads
8675                 (delq number gnus-newsgroup-unreads))
8676           (if gnus-newsgroup-auto-expire
8677               (push number gnus-newsgroup-expirable)
8678             (push (cons number gnus-low-score-mark)
8679                   gnus-newsgroup-reads)))
8680
8681         (setq mark
8682               (cond
8683                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8684                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8685                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8686                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8687                (t (or (cdr (assq number gnus-newsgroup-reads))
8688                       gnus-ancient-mark))))
8689         (setq gnus-newsgroup-data
8690               (cons (gnus-data-make number mark (1+ (point)) header 0)
8691                     gnus-newsgroup-data))
8692         (gnus-summary-insert-line
8693          header 0 nil mark (memq number gnus-newsgroup-replied)
8694          (memq number gnus-newsgroup-expirable)
8695          (mail-header-subject header) nil
8696          (cdr (assq number gnus-newsgroup-scored))
8697          (memq number gnus-newsgroup-processable))))))
8698
8699 (defun gnus-select-newsgroup (group &optional read-all)
8700   "Select newsgroup GROUP.
8701 If READ-ALL is non-nil, all articles in the group are selected."
8702   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8703          (info (nth 2 entry))
8704          articles fetched-articles cached)
8705
8706     (or (gnus-check-server
8707          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8708         (error "Couldn't open server"))
8709
8710     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8711         (gnus-activate-group group)     ; Or we can activate it...
8712         (progn                          ; Or we bug out.
8713           (when (equal major-mode 'gnus-summary-mode)
8714             (kill-buffer (current-buffer)))
8715           (error "Couldn't request group %s: %s"
8716                  group (gnus-status-message group))))
8717
8718     (unless (gnus-request-group group t)
8719       (when (equal major-mode 'gnus-summary-mode)
8720         (kill-buffer (current-buffer)))
8721       (error "Couldn't request group %s: %s"
8722              group (gnus-status-message group)))      
8723
8724     (setq gnus-newsgroup-name group)
8725     (setq gnus-newsgroup-unselected nil)
8726     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8727
8728     (and gnus-asynchronous
8729          (gnus-check-backend-function
8730           'request-asynchronous gnus-newsgroup-name)
8731          (setq gnus-newsgroup-async
8732                (gnus-request-asynchronous gnus-newsgroup-name)))
8733
8734     ;; Adjust and set lists of article marks.
8735     (when info
8736       (gnus-adjust-marked-articles info))
8737
8738     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8739     (when (gnus-virtual-group-p group)
8740       (setq cached gnus-newsgroup-cached))
8741
8742     (setq gnus-newsgroup-unreads
8743           (gnus-set-difference
8744            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8745            gnus-newsgroup-dormant))
8746
8747     (setq gnus-newsgroup-processable nil)
8748
8749     (setq articles (gnus-articles-to-read group read-all))
8750
8751     (cond
8752      ((null articles)
8753       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8754       'quit)
8755      ((eq articles 0) nil)
8756      (t
8757       ;; Init the dependencies hash table.
8758       (setq gnus-newsgroup-dependencies
8759             (gnus-make-hashtable (length articles)))
8760       ;; Retrieve the headers and read them in.
8761       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8762       (setq gnus-newsgroup-headers
8763             (if (eq 'nov
8764                     (setq gnus-headers-retrieved-by
8765                           (gnus-retrieve-headers
8766                            articles gnus-newsgroup-name
8767                            ;; We might want to fetch old headers, but
8768                            ;; not if there is only 1 article.
8769                            (and gnus-fetch-old-headers
8770                                 (or (and
8771                                      (not (eq gnus-fetch-old-headers 'some))
8772                                      (not (numberp gnus-fetch-old-headers)))
8773                                     (> (length articles) 1))))))
8774                 (gnus-get-newsgroup-headers-xover articles)
8775               (gnus-get-newsgroup-headers)))
8776       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8777
8778       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8779       (when cached
8780         (setq gnus-newsgroup-cached cached))
8781
8782       ;; Set the initial limit.
8783       (setq gnus-newsgroup-limit (copy-sequence articles))
8784       ;; Remove canceled articles from the list of unread articles.
8785       (setq gnus-newsgroup-unreads
8786             (gnus-set-sorted-intersection
8787              gnus-newsgroup-unreads
8788              (setq fetched-articles
8789                    (mapcar (lambda (headers) (mail-header-number headers))
8790                            gnus-newsgroup-headers))))
8791       ;; Removed marked articles that do not exist.
8792       (gnus-update-missing-marks
8793        (gnus-sorted-complement fetched-articles articles))
8794       ;; We might want to build some more threads first.
8795       (and gnus-fetch-old-headers
8796            (eq gnus-headers-retrieved-by 'nov)
8797            (gnus-build-old-threads))
8798       ;; Check whether auto-expire is to be done in this group.
8799       (setq gnus-newsgroup-auto-expire
8800             (gnus-group-auto-expirable-p group))
8801       ;; Set up the article buffer now, if necessary.
8802       (unless gnus-single-article-buffer
8803         (gnus-article-setup-buffer))
8804       ;; First and last article in this newsgroup.
8805       (when gnus-newsgroup-headers
8806         (setq gnus-newsgroup-begin
8807               (mail-header-number (car gnus-newsgroup-headers))
8808               gnus-newsgroup-end
8809               (mail-header-number
8810                (gnus-last-element gnus-newsgroup-headers))))
8811       (setq gnus-reffed-article-number -1)
8812       ;; GROUP is successfully selected.
8813       (or gnus-newsgroup-headers t)))))
8814
8815 (defun gnus-articles-to-read (group read-all)
8816   ;; Find out what articles the user wants to read.
8817   (let* ((articles
8818           ;; Select all articles if `read-all' is non-nil, or if there
8819           ;; are no unread articles.
8820           (if (or read-all
8821                   (and (zerop (length gnus-newsgroup-marked))
8822                        (zerop (length gnus-newsgroup-unreads))))
8823               (gnus-uncompress-range (gnus-active group))
8824             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8825                           (copy-sequence gnus-newsgroup-unreads))
8826                   '<)))
8827          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8828          (scored (length scored-list))
8829          (number (length articles))
8830          (marked (+ (length gnus-newsgroup-marked)
8831                     (length gnus-newsgroup-dormant)))
8832          (select
8833           (cond
8834            ((numberp read-all)
8835             read-all)
8836            (t
8837             (condition-case ()
8838                 (cond
8839                  ((and (or (<= scored marked) (= scored number))
8840                        (numberp gnus-large-newsgroup)
8841                        (> number gnus-large-newsgroup))
8842                   (let ((input
8843                          (read-string
8844                           (format
8845                            "How many articles from %s (default %d): "
8846                            gnus-newsgroup-name number))))
8847                     (if (string-match "^[ \t]*$" input) number input)))
8848                  ((and (> scored marked) (< scored number))
8849                   (let ((input
8850                          (read-string
8851                           (format "%s %s (%d scored, %d total): "
8852                                   "How many articles from"
8853                                   group scored number))))
8854                     (if (string-match "^[ \t]*$" input)
8855                         number input)))
8856                  (t number))
8857               (quit nil))))))
8858     (setq select (if (stringp select) (string-to-number select) select))
8859     (if (or (null select) (zerop select))
8860         select
8861       (if (and (not (zerop scored)) (<= (abs select) scored))
8862           (progn
8863             (setq articles (sort scored-list '<))
8864             (setq number (length articles)))
8865         (setq articles (copy-sequence articles)))
8866
8867       (if (< (abs select) number)
8868           (if (< select 0)
8869               ;; Select the N oldest articles.
8870               (setcdr (nthcdr (1- (abs select)) articles) nil)
8871             ;; Select the N most recent articles.
8872             (setq articles (nthcdr (- number select) articles))))
8873       (setq gnus-newsgroup-unselected
8874             (gnus-sorted-intersection
8875              gnus-newsgroup-unreads
8876              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8877       articles)))
8878
8879 (defun gnus-killed-articles (killed articles)
8880   (let (out)
8881     (while articles
8882       (if (inline (gnus-member-of-range (car articles) killed))
8883           (setq out (cons (car articles) out)))
8884       (setq articles (cdr articles)))
8885     out))
8886
8887 (defun gnus-uncompress-marks (marks)
8888   "Uncompress the mark ranges in MARKS."
8889   (let ((uncompressed '(score bookmark))
8890         out)
8891     (while marks
8892       (if (memq (caar marks) uncompressed)
8893           (push (car marks) out)
8894         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8895       (setq marks (cdr marks)))
8896     out))
8897
8898 (defun gnus-adjust-marked-articles (info)
8899   "Set all article lists and remove all marks that are no longer legal."
8900   (let* ((marked-lists (gnus-info-marks info))
8901          (active (gnus-active (gnus-info-group info)))
8902          (min (car active))
8903          (max (cdr active))
8904          (types gnus-article-mark-lists)
8905          (uncompressed '(score bookmark))
8906          marks var articles article mark)
8907
8908     (while marked-lists
8909       (setq marks (pop marked-lists))
8910       (set (setq var (intern (format "gnus-newsgroup-%s"
8911                                      (car (rassq (setq mark (car marks))
8912                                                  types)))))
8913            (if (memq (car marks) uncompressed) (cdr marks)
8914              (gnus-uncompress-range (cdr marks))))
8915
8916       (setq articles (symbol-value var))
8917
8918       ;; All articles have to be subsets of the active articles.
8919       (cond
8920        ;; Adjust "simple" lists.
8921        ((memq mark '(tick dormant expirable reply killed save))
8922         (while articles
8923           (when (or (< (setq article (pop articles)) min) (> article max))
8924             (set var (delq article (symbol-value var))))))
8925        ;; Adjust assocs.
8926        ((memq mark '(score bookmark))
8927         (while articles
8928           (when (or (< (car (setq article (pop articles))) min)
8929                     (> (car article) max))
8930             (set var (delq article (symbol-value var))))))))))
8931
8932 (defun gnus-update-missing-marks (missing)
8933   "Go through the list of MISSING articles and remove them mark lists."
8934   (when missing
8935     (let ((types gnus-article-mark-lists)
8936           var m)
8937       ;; Go through all types.
8938       (while types
8939         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8940         (when (symbol-value var)
8941           ;; This list has articles.  So we delete all missing articles
8942           ;; from it.
8943           (setq m missing)
8944           (while m
8945             (set var (delq (pop m) (symbol-value var)))))))))
8946
8947 (defun gnus-update-marks ()
8948   "Enter the various lists of marked articles into the newsgroup info list."
8949   (let ((types gnus-article-mark-lists)
8950         (info (gnus-get-info gnus-newsgroup-name))
8951         (uncompressed '(score bookmark killed))
8952         type list newmarked symbol)
8953     (when info
8954       ;; Add all marks lists that are non-nil to the list of marks lists.
8955       (while types
8956         (setq type (pop types))
8957         (when (setq list (symbol-value
8958                           (setq symbol
8959                                 (intern (format "gnus-newsgroup-%s"
8960                                                 (car type))))))
8961           (push (cons (cdr type)
8962                       (if (memq (cdr type) uncompressed) list
8963                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8964                 newmarked)))
8965
8966       ;; Enter these new marks into the info of the group.
8967       (if (nthcdr 3 info)
8968           (setcar (nthcdr 3 info) newmarked)
8969         ;; Add the marks lists to the end of the info.
8970         (when newmarked
8971           (setcdr (nthcdr 2 info) (list newmarked))))
8972
8973       ;; Cut off the end of the info if there's nothing else there.
8974       (let ((i 5))
8975         (while (and (> i 2)
8976                     (not (nth i info)))
8977           (when (nthcdr (decf i) info)
8978             (setcdr (nthcdr i info) nil)))))))
8979
8980 (defun gnus-add-marked-articles (group type articles &optional info force)
8981   ;; Add ARTICLES of TYPE to the info of GROUP.
8982   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8983   ;; add, but replace marked articles of TYPE with ARTICLES.
8984   (let ((info (or info (gnus-get-info group)))
8985         (uncompressed '(score bookmark killed))
8986         marked m)
8987     (or (not info)
8988         (and (not (setq marked (nthcdr 3 info)))
8989              (or (null articles)
8990                  (setcdr (nthcdr 2 info)
8991                          (list (list (cons type (gnus-compress-sequence
8992                                                  articles t)))))))
8993         (and (not (setq m (assq type (car marked))))
8994              (or (null articles)
8995                  (setcar marked
8996                          (cons (cons type (gnus-compress-sequence articles t) )
8997                                (car marked)))))
8998         (if force
8999             (if (null articles)
9000                 (setcar (nthcdr 3 info)
9001                         (delq (assq type (car marked)) (car marked)))
9002               (setcdr m (gnus-compress-sequence articles t)))
9003           (setcdr m (gnus-compress-sequence
9004                      (sort (nconc (gnus-uncompress-range (cdr m))
9005                                   (copy-sequence articles)) '<) t))))))
9006
9007 (defun gnus-set-mode-line (where)
9008   "This function sets the mode line of the article or summary buffers.
9009 If WHERE is `summary', the summary mode line format will be used."
9010   ;; Is this mode line one we keep updated?
9011   (when (memq where gnus-updated-mode-lines)
9012     (let (mode-string)
9013       (save-excursion
9014         ;; We evaluate this in the summary buffer since these
9015         ;; variables are buffer-local to that buffer.
9016         (set-buffer gnus-summary-buffer)
9017         ;; We bind all these variables that are used in the `eval' form
9018         ;; below.
9019         (let* ((mformat (symbol-value
9020                          (intern
9021                           (format "gnus-%s-mode-line-format-spec" where))))
9022                (gnus-tmp-group-name gnus-newsgroup-name)
9023                (gnus-tmp-article-number (or gnus-current-article 0))
9024                (gnus-tmp-unread gnus-newsgroup-unreads)
9025                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9026                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9027                (gnus-tmp-unread-and-unselected
9028                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9029                             (zerop gnus-tmp-unselected)) "")
9030                       ((zerop gnus-tmp-unselected)
9031                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9032                       (t (format "{%d(+%d) more}"
9033                                  gnus-tmp-unread-and-unticked
9034                                  gnus-tmp-unselected))))
9035                (gnus-tmp-subject
9036                 (if (and gnus-current-headers
9037                          (vectorp gnus-current-headers))
9038                     (gnus-mode-string-quote
9039                      (mail-header-subject gnus-current-headers)) ""))
9040                max-len
9041                gnus-tmp-header);; passed as argument to any user-format-funcs
9042           (setq mode-string (eval mformat))
9043           (setq max-len (max 4 (if gnus-mode-non-string-length
9044                                    (- (window-width)
9045                                       gnus-mode-non-string-length)
9046                                  (length mode-string))))
9047           ;; We might have to chop a bit of the string off...
9048           (when (> (length mode-string) max-len)
9049             (setq mode-string
9050                   (concat (gnus-truncate-string mode-string (- max-len 3))
9051                           "...")))
9052           ;; Pad the mode string a bit.
9053           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9054       ;; Update the mode line.
9055       (setq mode-line-buffer-identification (list mode-string))
9056       (set-buffer-modified-p t))))
9057
9058 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9059   "Go through the HEADERS list and add all Xrefs to a hash table.
9060 The resulting hash table is returned, or nil if no Xrefs were found."
9061   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9062          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9063          (xref-hashtb (make-vector 63 0))
9064          start group entry number xrefs header)
9065     (while headers
9066       (setq header (pop headers))
9067       (when (and (setq xrefs (mail-header-xref header))
9068                  (not (memq (setq number (mail-header-number header))
9069                             unreads)))
9070         (setq start 0)
9071         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9072           (setq start (match-end 0))
9073           (setq group (if prefix
9074                           (concat prefix (substring xrefs (match-beginning 1)
9075                                                     (match-end 1)))
9076                         (substring xrefs (match-beginning 1) (match-end 1))))
9077           (setq number
9078                 (string-to-int (substring xrefs (match-beginning 2)
9079                                           (match-end 2))))
9080           (if (setq entry (gnus-gethash group xref-hashtb))
9081               (setcdr entry (cons number (cdr entry)))
9082             (gnus-sethash group (cons number nil) xref-hashtb)))))
9083     (and start xref-hashtb)))
9084
9085 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9086   "Look through all the headers and mark the Xrefs as read."
9087   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9088         name entry info xref-hashtb idlist method nth4)
9089     (save-excursion
9090       (set-buffer gnus-group-buffer)
9091       (when (setq xref-hashtb
9092                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9093         (mapatoms
9094          (lambda (group)
9095            (unless (string= from-newsgroup (setq name (symbol-name group)))
9096              (setq idlist (symbol-value group))
9097              ;; Dead groups are not updated.
9098              (and (prog1
9099                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9100                             info (nth 2 entry))
9101                     (if (stringp (setq nth4 (gnus-info-method info)))
9102                         (setq nth4 (gnus-server-to-method nth4))))
9103                   ;; Only do the xrefs if the group has the same
9104                   ;; select method as the group we have just read.
9105                   (or (gnus-methods-equal-p
9106                        nth4 (gnus-find-method-for-group from-newsgroup))
9107                       virtual
9108                       (equal nth4 (setq method (gnus-find-method-for-group
9109                                                 from-newsgroup)))
9110                       (and (equal (car nth4) (car method))
9111                            (equal (nth 1 nth4) (nth 1 method))))
9112                   gnus-use-cross-reference
9113                   (or (not (eq gnus-use-cross-reference t))
9114                       virtual
9115                       ;; Only do cross-references on subscribed
9116                       ;; groups, if that is what is wanted.
9117                       (<= (gnus-info-level info) gnus-level-subscribed))
9118                   (gnus-group-make-articles-read name idlist))))
9119          xref-hashtb)))))
9120
9121 (defun gnus-group-make-articles-read (group articles)
9122   (let* ((num 0)
9123          (entry (gnus-gethash group gnus-newsrc-hashtb))
9124          (info (nth 2 entry))
9125          (active (gnus-active group))
9126          range)
9127     ;; First peel off all illegal article numbers.
9128     (if active
9129         (let ((ids articles)
9130               id first)
9131           (while ids
9132             (setq id (car ids))
9133             (if (and first (> id (cdr active)))
9134                 (progn
9135                   ;; We'll end up in this situation in one particular
9136                   ;; obscure situation.  If you re-scan a group and get
9137                   ;; a new article that is cross-posted to a different
9138                   ;; group that has not been re-scanned, you might get
9139                   ;; crossposted article that has a higher number than
9140                   ;; Gnus believes possible.  So we re-activate this
9141                   ;; group as well.  This might mean doing the
9142                   ;; crossposting thingy will *increase* the number
9143                   ;; of articles in some groups.  Tsk, tsk.
9144                   (setq active (or (gnus-activate-group group) active))))
9145             (if (or (> id (cdr active))
9146                     (< id (car active)))
9147                 (setq articles (delq id articles)))
9148             (setq ids (cdr ids)))))
9149     ;; If the read list is nil, we init it.
9150     (and active
9151          (null (gnus-info-read info))
9152          (> (car active) 1)
9153          (gnus-info-set-read info (cons 1 (1- (car active)))))
9154     ;; Then we add the read articles to the range.
9155     (gnus-info-set-read
9156      info
9157      (setq range
9158            (gnus-add-to-range
9159             (gnus-info-read info) (setq articles (sort articles '<)))))
9160     ;; Then we have to re-compute how many unread
9161     ;; articles there are in this group.
9162     (if active
9163         (progn
9164           (cond
9165            ((not range)
9166             (setq num (- (1+ (cdr active)) (car active))))
9167            ((not (listp (cdr range)))
9168             (setq num (- (cdr active) (- (1+ (cdr range))
9169                                          (car range)))))
9170            (t
9171             (while range
9172               (if (numberp (car range))
9173                   (setq num (1+ num))
9174                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9175               (setq range (cdr range)))
9176             (setq num (- (cdr active) num))))
9177           ;; Update the number of unread articles.
9178           (setcar entry num)
9179           ;; Update the group buffer.
9180           (gnus-group-update-group group t)))))
9181
9182 (defun gnus-methods-equal-p (m1 m2)
9183   (let ((m1 (or m1 gnus-select-method))
9184         (m2 (or m2 gnus-select-method)))
9185     (or (equal m1 m2)
9186         (and (eq (car m1) (car m2))
9187              (or (not (memq 'address (assoc (symbol-name (car m1))
9188                                             gnus-valid-select-methods)))
9189                  (equal (nth 1 m1) (nth 1 m2)))))))
9190
9191 (defsubst gnus-header-value ()
9192   (buffer-substring 
9193    (match-end 0) 
9194    (if (re-search-forward "^[^ \t]" nil t)
9195        (progn
9196          (backward-char 2)
9197          (point))
9198      (gnus-point-at-eol))))
9199
9200 (defvar gnus-newsgroup-none-id 0)
9201
9202 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9203   (let ((cur nntp-server-buffer)
9204         (dependencies
9205          (or dependencies
9206              (save-excursion (set-buffer gnus-summary-buffer)
9207                              gnus-newsgroup-dependencies)))
9208         headers id id-dep ref-dep end ref)
9209     (save-excursion
9210       (set-buffer nntp-server-buffer)
9211       (run-hooks 'gnus-parse-headers-hook)
9212       (let ((case-fold-search t)
9213             in-reply-to header p lines)
9214         (goto-char (point-min))
9215         ;; Search to the beginning of the next header.  Error messages
9216         ;; do not begin with 2 or 3.
9217         (while (re-search-forward "^[23][0-9]+ " nil t)
9218           (setq id nil
9219                 ref nil)
9220           ;; This implementation of this function, with nine
9221           ;; search-forwards instead of the one re-search-forward and
9222           ;; a case (which basically was the old function) is actually
9223           ;; about twice as fast, even though it looks messier.  You
9224           ;; can't have everything, I guess.  Speed and elegance
9225           ;; doesn't always go hand in hand.
9226           (setq
9227            header
9228            (vector
9229             ;; Number.
9230             (prog1
9231                 (read cur)
9232               (end-of-line)
9233               (setq p (point))
9234               (narrow-to-region (point)
9235                                 (or (and (search-forward "\n.\n" nil t)
9236                                          (- (point) 2))
9237                                     (point))))
9238             ;; Subject.
9239             (progn
9240               (goto-char p)
9241               (if (search-forward "\nsubject: " nil t)
9242                   (gnus-header-value) "(none)"))
9243             ;; From.
9244             (progn
9245               (goto-char p)
9246               (if (search-forward "\nfrom: " nil t)
9247                   (gnus-header-value) "(nobody)"))
9248             ;; Date.
9249             (progn
9250               (goto-char p)
9251               (if (search-forward "\ndate: " nil t)
9252                   (gnus-header-value) ""))
9253             ;; Message-ID.
9254             (progn
9255               (goto-char p)
9256               (if (search-forward "\nmessage-id: " nil t)
9257                   (setq id (gnus-header-value))
9258                 ;; If there was no message-id, we just fake one to make
9259                 ;; subsequent routines simpler.
9260                 (setq id (concat "none+"
9261                                  (int-to-string
9262                                   (setq gnus-newsgroup-none-id
9263                                         (1+ gnus-newsgroup-none-id)))))))
9264             ;; References.
9265             (progn
9266               (goto-char p)
9267               (if (search-forward "\nreferences: " nil t)
9268                   (progn
9269                     (setq end (point))
9270                     (prog1
9271                         (gnus-header-value)
9272                       (setq ref
9273                             (buffer-substring
9274                              (progn
9275                                (end-of-line)
9276                                (search-backward ">" end t)
9277                                (1+ (point)))
9278                              (progn
9279                                (search-backward "<" end t)
9280                                (point))))))
9281                 ;; Get the references from the in-reply-to header if there
9282                 ;; were no references and the in-reply-to header looks
9283                 ;; promising.
9284                 (if (and (search-forward "\nin-reply-to: " nil t)
9285                          (setq in-reply-to (gnus-header-value))
9286                          (string-match "<[^>]+>" in-reply-to))
9287                     (setq ref (substring in-reply-to (match-beginning 0)
9288                                          (match-end 0)))
9289                   (setq ref ""))))
9290             ;; Chars.
9291             0
9292             ;; Lines.
9293             (progn
9294               (goto-char p)
9295               (if (search-forward "\nlines: " nil t)
9296                   (if (numberp (setq lines (read cur)))
9297                       lines 0)
9298                 0))
9299             ;; Xref.
9300             (progn
9301               (goto-char p)
9302               (and (search-forward "\nxref: " nil t)
9303                    (gnus-header-value)))))
9304           ;; We do the threading while we read the headers.  The
9305           ;; message-id and the last reference are both entered into
9306           ;; the same hash table.  Some tippy-toeing around has to be
9307           ;; done in case an article has arrived before the article
9308           ;; which it refers to.
9309           (if (boundp (setq id-dep (intern id dependencies)))
9310               (if (and (car (symbol-value id-dep))
9311                        (not force-new))
9312                   ;; An article with this Message-ID has already
9313                   ;; been seen, so we ignore this one, except we add
9314                   ;; any additional Xrefs (in case the two articles
9315                   ;; came from different servers).
9316                   (progn
9317                     (mail-header-set-xref
9318                      (car (symbol-value id-dep))
9319                      (concat (or (mail-header-xref
9320                                   (car (symbol-value id-dep))) "")
9321                              (or (mail-header-xref header) "")))
9322                     (setq header nil))
9323                 (setcar (symbol-value id-dep) header))
9324             (set id-dep (list header)))
9325           (when header
9326             (if (boundp (setq ref-dep (intern ref dependencies)))
9327                 (setcdr (symbol-value ref-dep)
9328                         (nconc (cdr (symbol-value ref-dep))
9329                                (list (symbol-value id-dep))))
9330               (set ref-dep (list nil (symbol-value id-dep))))
9331             (setq headers (cons header headers)))
9332           (goto-char (point-max))
9333           (widen))
9334         (nreverse headers)))))
9335
9336 ;; The following macros and functions were written by Felix Lee
9337 ;; <flee@cse.psu.edu>.
9338
9339 (defmacro gnus-nov-read-integer ()
9340   '(prog1
9341        (if (= (following-char) ?\t)
9342            0
9343          (let ((num (condition-case nil (read buffer) (error nil))))
9344            (if (numberp num) num 0)))
9345      (or (eobp) (forward-char 1))))
9346
9347 (defmacro gnus-nov-skip-field ()
9348   '(search-forward "\t" eol 'move))
9349
9350 (defmacro gnus-nov-field ()
9351   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9352
9353 ;; Goes through the xover lines and returns a list of vectors
9354 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9355                                                   force-new dependencies)
9356   "Parse the news overview data in the server buffer, and return a
9357 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9358   ;; Get the Xref when the users reads the articles since most/some
9359   ;; NNTP servers do not include Xrefs when using XOVER.
9360   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9361   (let ((cur nntp-server-buffer)
9362         (dependencies (or dependencies gnus-newsgroup-dependencies))
9363         number headers header)
9364     (save-excursion
9365       (set-buffer nntp-server-buffer)
9366       ;; Allow the user to mangle the headers before parsing them.
9367       (run-hooks 'gnus-parse-headers-hook)
9368       (goto-char (point-min))
9369       (while (and sequence (not (eobp)))
9370         (setq number (read cur))
9371         (while (and sequence (< (car sequence) number))
9372           (setq sequence (cdr sequence)))
9373         (and sequence
9374              (eq number (car sequence))
9375              (progn
9376                (setq sequence (cdr sequence))
9377                (if (setq header
9378                          (inline (gnus-nov-parse-line
9379                                   number dependencies force-new)))
9380                    (setq headers (cons header headers)))))
9381         (forward-line 1))
9382       (setq headers (nreverse headers)))
9383     headers))
9384
9385 ;; This function has to be called with point after the article number
9386 ;; on the beginning of the line.
9387 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9388   (let ((none 0)
9389         (eol (gnus-point-at-eol))
9390         (buffer (current-buffer))
9391         header ref id id-dep ref-dep)
9392
9393     ;; overview: [num subject from date id refs chars lines misc]
9394     (narrow-to-region (point) eol)
9395     (or (eobp) (forward-char))
9396
9397     (condition-case nil
9398         (setq header
9399               (vector
9400                number                   ; number
9401                (gnus-nov-field)         ; subject
9402                (gnus-nov-field)         ; from
9403                (gnus-nov-field)         ; date
9404                (setq id (or (gnus-nov-field)
9405                             (concat "none+"
9406                                     (int-to-string
9407                                      (setq none (1+ none)))))) ; id
9408                (progn
9409                  (save-excursion
9410                    (let ((beg (point)))
9411                      (search-forward "\t" eol)
9412                      (if (search-backward ">" beg t)
9413                          (setq ref
9414                                (buffer-substring
9415                                 (1+ (point))
9416                                 (search-backward "<" beg t)))
9417                        (setq ref nil))))
9418                  (gnus-nov-field))      ; refs
9419                (gnus-nov-read-integer)  ; chars
9420                (gnus-nov-read-integer)  ; lines
9421                (if (= (following-char) ?\n)
9422                    nil
9423                  (gnus-nov-field))      ; misc
9424                ))
9425       (error (progn
9426                (gnus-error 4 "Strange nov line")
9427                (setq header nil)
9428                (goto-char eol))))
9429
9430     (widen)
9431
9432     ;; We build the thread tree.
9433     (when header
9434       (if (boundp (setq id-dep (intern id dependencies)))
9435           (if (and (car (symbol-value id-dep))
9436                    (not force-new))
9437               ;; An article with this Message-ID has already been seen,
9438               ;; so we ignore this one, except we add any additional
9439               ;; Xrefs (in case the two articles came from different
9440               ;; servers.
9441               (progn
9442                 (mail-header-set-xref
9443                  (car (symbol-value id-dep))
9444                  (concat (or (mail-header-xref
9445                               (car (symbol-value id-dep))) "")
9446                          (or (mail-header-xref header) "")))
9447                 (setq header nil))
9448             (setcar (symbol-value id-dep) header))
9449         (set id-dep (list header))))
9450     (when header
9451       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9452           (setcdr (symbol-value ref-dep)
9453                   (nconc (cdr (symbol-value ref-dep))
9454                          (list (symbol-value id-dep))))
9455         (set ref-dep (list nil (symbol-value id-dep)))))
9456     header))
9457
9458 (defun gnus-article-get-xrefs ()
9459   "Fill in the Xref value in `gnus-current-headers', if necessary.
9460 This is meant to be called in `gnus-article-internal-prepare-hook'."
9461   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9462                                  gnus-current-headers)))
9463     (or (not gnus-use-cross-reference)
9464         (not headers)
9465         (and (mail-header-xref headers)
9466              (not (string= (mail-header-xref headers) "")))
9467         (let ((case-fold-search t)
9468               xref)
9469           (save-restriction
9470             (nnheader-narrow-to-headers)
9471             (goto-char (point-min))
9472             (if (or (and (eq (downcase (following-char)) ?x)
9473                          (looking-at "Xref:"))
9474                     (search-forward "\nXref:" nil t))
9475                 (progn
9476                   (goto-char (1+ (match-end 0)))
9477                   (setq xref (buffer-substring (point)
9478                                                (progn (end-of-line) (point))))
9479                   (mail-header-set-xref headers xref))))))))
9480
9481 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9482   "Find article ID and insert the summary line for that article."
9483   (let ((header (if (and old-header use-old-header)
9484                     old-header (gnus-read-header id)))
9485         (number (and (numberp id) id))
9486         pos)
9487     (when header
9488       ;; Rebuild the thread that this article is part of and go to the
9489       ;; article we have fetched.
9490       (when (and (not gnus-show-threads)
9491                  old-header)
9492         (when (setq pos (text-property-any
9493                          (point-min) (point-max) 'gnus-number 
9494                          (mail-header-number old-header)))
9495           (goto-char pos)
9496           (gnus-delete-line)
9497           (gnus-data-remove (mail-header-number old-header))))
9498       (when old-header
9499         (mail-header-set-number header (mail-header-number old-header)))
9500       (setq gnus-newsgroup-sparse
9501             (delq (setq number (mail-header-number header)) 
9502                   gnus-newsgroup-sparse))
9503       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9504       (gnus-rebuild-thread (mail-header-id header))
9505       (gnus-summary-goto-subject number nil t))
9506     (when (and (numberp number)
9507                (> number 0))
9508       ;; We have to update the boundaries even if we can't fetch the
9509       ;; article if ID is a number -- so that the next `P' or `N'
9510       ;; command will fetch the previous (or next) article even
9511       ;; if the one we tried to fetch this time has been canceled.
9512       (and (> number gnus-newsgroup-end)
9513            (setq gnus-newsgroup-end number))
9514       (and (< number gnus-newsgroup-begin)
9515            (setq gnus-newsgroup-begin number))
9516       (setq gnus-newsgroup-unselected
9517             (delq number gnus-newsgroup-unselected)))
9518     ;; Report back a success?
9519     (and header (mail-header-number header))))
9520
9521 (defun gnus-summary-work-articles (n)
9522   "Return a list of articles to be worked upon.  The prefix argument,
9523 the list of process marked articles, and the current article will be
9524 taken into consideration."
9525   (cond
9526    ((and n (numberp n))
9527     ;; A numerical prefix has been given.
9528     (let ((backward (< n 0))
9529           (n (abs n))
9530           articles article)
9531       (save-excursion
9532         (while
9533             (and (> n 0)
9534                  (push (setq article (gnus-summary-article-number))
9535                        articles)
9536                  (if backward
9537                      (gnus-summary-find-prev nil article)
9538                    (gnus-summary-find-next nil article)))
9539           (decf n)))
9540       (nreverse articles)))
9541    ((and (boundp 'transient-mark-mode)
9542          transient-mark-mode
9543          mark-active)
9544     ;; Work on the region between point and mark.
9545     (let ((max (max (point) (mark)))
9546           articles article)
9547       (save-excursion
9548         (goto-char (min (point) (mark)))
9549         (while
9550             (and
9551              (push (setq article (gnus-summary-article-number)) articles)
9552              (gnus-summary-find-next nil article)
9553              (< (point) max)))
9554         (nreverse articles))))
9555    (gnus-newsgroup-processable
9556     ;; There are process-marked articles present.
9557     (reverse gnus-newsgroup-processable))
9558    (t
9559     ;; Just return the current article.
9560     (list (gnus-summary-article-number)))))
9561
9562 (defun gnus-summary-search-group (&optional backward use-level)
9563   "Search for next unread newsgroup.
9564 If optional argument BACKWARD is non-nil, search backward instead."
9565   (save-excursion
9566     (set-buffer gnus-group-buffer)
9567     (if (gnus-group-search-forward
9568          backward nil (if use-level (gnus-group-group-level) nil))
9569         (gnus-group-group-name))))
9570
9571 (defun gnus-summary-best-group (&optional exclude-group)
9572   "Find the name of the best unread group.
9573 If EXCLUDE-GROUP, do not go to this group."
9574   (save-excursion
9575     (set-buffer gnus-group-buffer)
9576     (save-excursion
9577       (gnus-group-best-unread-group exclude-group))))
9578
9579 (defun gnus-summary-find-next (&optional unread article backward)
9580   (if backward (gnus-summary-find-prev)
9581     (let* ((dummy (gnus-summary-article-intangible-p))
9582            (article (or article (gnus-summary-article-number)))
9583            (arts (gnus-data-find-list article))
9584            result)
9585       (when (and (not dummy)
9586                  (or (not gnus-summary-check-current)
9587                      (not unread)
9588                      (not (gnus-data-unread-p (car arts)))))
9589         (setq arts (cdr arts)))
9590       (when (setq result
9591                   (if unread
9592                       (progn
9593                         (while arts
9594                           (when (gnus-data-unread-p (car arts))
9595                             (setq result (car arts)
9596                                   arts nil))
9597                           (setq arts (cdr arts)))
9598                         result)
9599                     (car arts)))
9600         (goto-char (gnus-data-pos result))
9601         (gnus-data-number result)))))
9602
9603 (defun gnus-summary-find-prev (&optional unread article)
9604   (let* ((eobp (eobp))
9605          (article (or article (gnus-summary-article-number)))
9606          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9607          result)
9608     (when (and (not eobp)
9609                (or (not gnus-summary-check-current)
9610                    (not unread)
9611                    (not (gnus-data-unread-p (car arts)))))
9612       (setq arts (cdr arts)))
9613     (if (setq result
9614               (if unread
9615                   (progn
9616                     (while arts
9617                       (and (gnus-data-unread-p (car arts))
9618                            (setq result (car arts)
9619                                  arts nil))
9620                       (setq arts (cdr arts)))
9621                     result)
9622                 (car arts)))
9623         (progn
9624           (goto-char (gnus-data-pos result))
9625           (gnus-data-number result)))))
9626
9627 (defun gnus-summary-find-subject (subject &optional unread backward article)
9628   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9629          (article (or article (gnus-summary-article-number)))
9630          (articles (gnus-data-list backward))
9631          (arts (gnus-data-find-list article articles))
9632          result)
9633     (when (or (not gnus-summary-check-current)
9634               (not unread)
9635               (not (gnus-data-unread-p (car arts))))
9636       (setq arts (cdr arts)))
9637     (while arts
9638       (and (or (not unread)
9639                (gnus-data-unread-p (car arts)))
9640            (vectorp (gnus-data-header (car arts)))
9641            (gnus-subject-equal
9642             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9643            (setq result (car arts)
9644                  arts nil))
9645       (setq arts (cdr arts)))
9646     (and result
9647          (goto-char (gnus-data-pos result))
9648          (gnus-data-number result))))
9649
9650 (defun gnus-summary-search-forward (&optional unread subject backward)
9651   "Search forward for an article.
9652 If UNREAD, look for unread articles.  If SUBJECT, look for
9653 articles with that subject.  If BACKWARD, search backward instead."
9654   (cond (subject (gnus-summary-find-subject subject unread backward))
9655         (backward (gnus-summary-find-prev unread))
9656         (t (gnus-summary-find-next unread))))
9657
9658 (defun gnus-recenter (&optional n)
9659   "Center point in window and redisplay frame.
9660 Also do horizontal recentering."
9661   (interactive "P")
9662   (when (and gnus-auto-center-summary
9663              (not (eq gnus-auto-center-summary 'vertical)))
9664     (gnus-horizontal-recenter))
9665   (recenter n))
9666
9667 (defun gnus-summary-recenter ()
9668   "Center point in the summary window.
9669 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9670 displayed, no centering will be performed."
9671   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9672   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9673   (let* ((top (cond ((< (window-height) 4) 0)
9674                     ((< (window-height) 7) 1)
9675                     (t 2)))
9676          (height (1- (window-height)))
9677          (bottom (save-excursion (goto-char (point-max))
9678                                  (forward-line (- height))
9679                                  (point)))
9680          (window (get-buffer-window (current-buffer))))
9681     ;; The user has to want it.
9682     (when gnus-auto-center-summary
9683       (when (get-buffer-window gnus-article-buffer)
9684        ;; Only do recentering when the article buffer is displayed,
9685        ;; Set the window start to either `bottom', which is the biggest
9686        ;; possible valid number, or the second line from the top,
9687        ;; whichever is the least.
9688        (set-window-start
9689         window (min bottom (save-excursion 
9690                              (forward-line (- top)) (point)))))
9691       ;; Do horizontal recentering while we're at it.
9692       (when (and (get-buffer-window (current-buffer) t)
9693                  (not (eq gnus-auto-center-summary 'vertical)))
9694         (let ((selected (selected-window)))
9695           (select-window (get-buffer-window (current-buffer) t))
9696           (gnus-summary-position-point)
9697           (gnus-horizontal-recenter)
9698           (select-window selected))))))
9699
9700 (defun gnus-horizontal-recenter ()
9701   "Recenter the current buffer horizontally."
9702   (if (< (current-column) (/ (window-width) 2))
9703       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9704     (let* ((orig (point))
9705            (end (window-end (get-buffer-window (current-buffer) t)))
9706            (max 0))
9707       ;; Find the longest line currently displayed in the window.
9708       (goto-char (window-start))
9709       (while (and (not (eobp)) 
9710                   (< (point) end))
9711         (end-of-line)
9712         (setq max (max max (current-column)))
9713         (forward-line 1))
9714       (goto-char orig)
9715       ;; Scroll horizontally to center (sort of) the point.
9716       (if (> max (window-width))
9717           (set-window-hscroll 
9718            (get-buffer-window (current-buffer) t)
9719            (min (- (current-column) (/ (window-width) 3))
9720                 (+ 2 (- max (window-width)))))
9721         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9722       max)))
9723
9724 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9725 (defun gnus-short-group-name (group &optional levels)
9726   "Collapse GROUP name LEVELS."
9727   (let* ((name "") 
9728          (foreign "")
9729          (depth 0) 
9730          (skip 1)
9731          (levels (or levels
9732                      (progn
9733                        (while (string-match "\\." group skip)
9734                          (setq skip (match-end 0)
9735                                depth (+ depth 1)))
9736                        depth))))
9737     (if (string-match ":" group)
9738         (setq foreign (substring group 0 (match-end 0))
9739               group (substring group (match-end 0))))
9740     (while group
9741       (if (and (string-match "\\." group)
9742                (> levels (- gnus-group-uncollapsed-levels 1)))
9743           (setq name (concat name (substring group 0 1))
9744                 group (substring group (match-end 0))
9745                 levels (- levels 1)
9746                 name (concat name "."))
9747         (setq name (concat foreign name group)
9748               group nil)))
9749     name))
9750
9751 (defun gnus-summary-jump-to-group (newsgroup)
9752   "Move point to NEWSGROUP in group mode buffer."
9753   ;; Keep update point of group mode buffer if visible.
9754   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9755       (save-window-excursion
9756         ;; Take care of tree window mode.
9757         (if (get-buffer-window gnus-group-buffer)
9758             (pop-to-buffer gnus-group-buffer))
9759         (gnus-group-jump-to-group newsgroup))
9760     (save-excursion
9761       ;; Take care of tree window mode.
9762       (if (get-buffer-window gnus-group-buffer)
9763           (pop-to-buffer gnus-group-buffer)
9764         (set-buffer gnus-group-buffer))
9765       (gnus-group-jump-to-group newsgroup))))
9766
9767 ;; This function returns a list of article numbers based on the
9768 ;; difference between the ranges of read articles in this group and
9769 ;; the range of active articles.
9770 (defun gnus-list-of-unread-articles (group)
9771   (let* ((read (gnus-info-read (gnus-get-info group)))
9772          (active (gnus-active group))
9773          (last (cdr active))
9774          first nlast unread)
9775     ;; If none are read, then all are unread.
9776     (if (not read)
9777         (setq first (car active))
9778       ;; If the range of read articles is a single range, then the
9779       ;; first unread article is the article after the last read
9780       ;; article.  Sounds logical, doesn't it?
9781       (if (not (listp (cdr read)))
9782           (setq first (1+ (cdr read)))
9783         ;; `read' is a list of ranges.
9784         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9785                                 (caar read))) 1)
9786             (setq first 1))
9787         (while read
9788           (if first
9789               (while (< first nlast)
9790                 (setq unread (cons first unread))
9791                 (setq first (1+ first))))
9792           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9793           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9794           (setq read (cdr read)))))
9795     ;; And add the last unread articles.
9796     (while (<= first last)
9797       (setq unread (cons first unread))
9798       (setq first (1+ first)))
9799     ;; Return the list of unread articles.
9800     (nreverse unread)))
9801
9802 (defun gnus-list-of-read-articles (group)
9803   "Return a list of unread, unticked and non-dormant articles."
9804   (let* ((info (gnus-get-info group))
9805          (marked (gnus-info-marks info))
9806          (active (gnus-active group)))
9807     (and info active
9808          (gnus-set-difference
9809           (gnus-sorted-complement
9810            (gnus-uncompress-range active)
9811            (gnus-list-of-unread-articles group))
9812           (append
9813            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9814            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9815
9816 ;; Various summary commands
9817
9818 (defun gnus-summary-universal-argument (arg)
9819   "Perform any operation on all articles that are process/prefixed."
9820   (interactive "P")
9821   (gnus-set-global-variables)
9822   (let ((articles (gnus-summary-work-articles arg))
9823         func article)
9824     (if (eq
9825          (setq
9826           func
9827           (key-binding
9828            (read-key-sequence
9829             (substitute-command-keys
9830              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9831              ))))
9832          'undefined)
9833         (gnus-error 1 "Undefined key")
9834       (save-excursion
9835         (while articles
9836           (gnus-summary-goto-subject (setq article (pop articles)))
9837           (command-execute func)
9838           (gnus-summary-remove-process-mark article)))))
9839   (gnus-summary-position-point))
9840
9841 (defun gnus-summary-toggle-truncation (&optional arg)
9842   "Toggle truncation of summary lines.
9843 With arg, turn line truncation on iff arg is positive."
9844   (interactive "P")
9845   (setq truncate-lines
9846         (if (null arg) (not truncate-lines)
9847           (> (prefix-numeric-value arg) 0)))
9848   (redraw-display))
9849
9850 (defun gnus-summary-reselect-current-group (&optional all rescan)
9851   "Exit and then reselect the current newsgroup.
9852 The prefix argument ALL means to select all articles."
9853   (interactive "P")
9854   (gnus-set-global-variables)
9855   (let ((current-subject (gnus-summary-article-number))
9856         (group gnus-newsgroup-name))
9857     (setq gnus-newsgroup-begin nil)
9858     (gnus-summary-exit)
9859     ;; We have to adjust the point of group mode buffer because the
9860     ;; current point was moved to the next unread newsgroup by
9861     ;; exiting.
9862     (gnus-summary-jump-to-group group)
9863     (when rescan
9864       (save-excursion
9865         (gnus-group-get-new-news-this-group 1)))
9866     (gnus-group-read-group all t)
9867     (gnus-summary-goto-subject current-subject)))
9868
9869 (defun gnus-summary-rescan-group (&optional all)
9870   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9871   (interactive "P")
9872   (gnus-summary-reselect-current-group all t))
9873
9874 (defun gnus-summary-update-info ()
9875   (let* ((group gnus-newsgroup-name))
9876     (when gnus-newsgroup-kill-headers
9877       (setq gnus-newsgroup-killed
9878             (gnus-compress-sequence
9879              (nconc
9880               (gnus-set-sorted-intersection
9881                (gnus-uncompress-range gnus-newsgroup-killed)
9882                (setq gnus-newsgroup-unselected
9883                      (sort gnus-newsgroup-unselected '<)))
9884               (setq gnus-newsgroup-unreads
9885                     (sort gnus-newsgroup-unreads '<))) t)))
9886     (unless (listp (cdr gnus-newsgroup-killed))
9887       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9888     (let ((headers gnus-newsgroup-headers))
9889       (run-hooks 'gnus-exit-group-hook)
9890       (unless gnus-save-score
9891         (setq gnus-newsgroup-scored nil))
9892       ;; Set the new ranges of read articles.
9893       (gnus-update-read-articles
9894        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9895       ;; Set the current article marks.
9896       (gnus-update-marks)
9897       ;; Do the cross-ref thing.
9898       (when gnus-use-cross-reference
9899         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9900       ;; Do adaptive scoring, and possibly save score files.
9901       (when gnus-newsgroup-adaptive
9902         (gnus-score-adaptive))
9903       (when gnus-use-scoring
9904         (gnus-score-save))
9905       ;; Do not switch windows but change the buffer to work.
9906       (set-buffer gnus-group-buffer)
9907       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9908           (gnus-group-update-group group)))))
9909
9910 (defun gnus-summary-exit (&optional temporary)
9911   "Exit reading current newsgroup, and then return to group selection mode.
9912 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9913   (interactive)
9914   (gnus-set-global-variables)
9915   (gnus-kill-save-kill-buffer)
9916   (let* ((group gnus-newsgroup-name)
9917          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9918          (mode major-mode)
9919          (buf (current-buffer)))
9920     (run-hooks 'gnus-summary-prepare-exit-hook)
9921     ;; If we have several article buffers, we kill them at exit.
9922     (unless gnus-single-article-buffer
9923       (gnus-kill-buffer gnus-original-article-buffer)
9924       (setq gnus-article-current nil))
9925     (when gnus-use-cache
9926       (gnus-cache-possibly-remove-articles)
9927       (gnus-cache-save-buffers))
9928     (when gnus-use-trees
9929       (gnus-tree-close group))
9930     ;; Make all changes in this group permanent.
9931     (unless quit-config
9932       (gnus-summary-update-info))
9933     (gnus-close-group group)
9934     ;; Make sure where I was, and go to next newsgroup.
9935     (set-buffer gnus-group-buffer)
9936     (unless quit-config
9937       (gnus-group-jump-to-group group))
9938     (run-hooks 'gnus-summary-exit-hook)
9939     (unless quit-config
9940       (gnus-group-next-unread-group 1))
9941     (if temporary
9942         nil                             ;Nothing to do.
9943       ;; If we have several article buffers, we kill them at exit.
9944       (unless gnus-single-article-buffer
9945         (gnus-kill-buffer gnus-article-buffer)
9946         (gnus-kill-buffer gnus-original-article-buffer)
9947         (setq gnus-article-current nil))
9948       (set-buffer buf)
9949       (if (not gnus-kill-summary-on-exit)
9950           (gnus-deaden-summary)
9951         ;; We set all buffer-local variables to nil.  It is unclear why
9952         ;; this is needed, but if we don't, buffer-local variables are
9953         ;; not garbage-collected, it seems.  This would the lead to en
9954         ;; ever-growing Emacs.
9955         (gnus-summary-clear-local-variables)
9956         (when (get-buffer gnus-article-buffer)
9957           (bury-buffer gnus-article-buffer))
9958         ;; We clear the global counterparts of the buffer-local
9959         ;; variables as well, just to be on the safe side.
9960         (gnus-configure-windows 'group 'force)
9961         (gnus-summary-clear-local-variables)
9962         ;; Return to group mode buffer.
9963         (if (eq mode 'gnus-summary-mode)
9964             (gnus-kill-buffer buf)))
9965       (setq gnus-current-select-method gnus-select-method)
9966       (pop-to-buffer gnus-group-buffer)
9967       ;; Clear the current group name.
9968       (if (not quit-config)
9969           (progn
9970             (gnus-group-jump-to-group group)
9971             (gnus-group-next-unread-group 1)
9972             (gnus-configure-windows 'group 'force))
9973         (if (not (buffer-name (car quit-config)))
9974             (gnus-configure-windows 'group 'force)
9975           (set-buffer (car quit-config))
9976           (and (eq major-mode 'gnus-summary-mode)
9977                (gnus-set-global-variables))
9978           (gnus-configure-windows (cdr quit-config))))
9979       (unless quit-config
9980         (setq gnus-newsgroup-name nil)))))
9981
9982 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9983 (defun gnus-summary-exit-no-update (&optional no-questions)
9984   "Quit reading current newsgroup without updating read article info."
9985   (interactive)
9986   (gnus-set-global-variables)
9987   (let* ((group gnus-newsgroup-name)
9988          (quit-config (gnus-group-quit-config group)))
9989     (when (or no-questions
9990               gnus-expert-user
9991               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9992       ;; If we have several article buffers, we kill them at exit.
9993       (unless gnus-single-article-buffer
9994         (gnus-kill-buffer gnus-article-buffer)
9995         (gnus-kill-buffer gnus-original-article-buffer)
9996         (setq gnus-article-current nil))
9997       (if (not gnus-kill-summary-on-exit)
9998           (gnus-deaden-summary)
9999         (gnus-close-group group)
10000         (gnus-summary-clear-local-variables)
10001         (set-buffer gnus-group-buffer)
10002         (gnus-summary-clear-local-variables)
10003         (when (get-buffer gnus-summary-buffer)
10004           (kill-buffer gnus-summary-buffer)))
10005       (unless gnus-single-article-buffer
10006         (setq gnus-article-current nil))
10007       (when gnus-use-trees
10008         (gnus-tree-close group))
10009       (when (get-buffer gnus-article-buffer)
10010         (bury-buffer gnus-article-buffer))
10011       ;; Return to the group buffer.
10012       (gnus-configure-windows 'group 'force)
10013       ;; Clear the current group name.
10014       (setq gnus-newsgroup-name nil)
10015       (when (equal (gnus-group-group-name) group)
10016         (gnus-group-next-unread-group 1))
10017       (when quit-config
10018         (if (not (buffer-name (car quit-config)))
10019             (gnus-configure-windows 'group 'force)
10020           (set-buffer (car quit-config))
10021           (when (eq major-mode 'gnus-summary-mode)
10022             (gnus-set-global-variables))
10023           (gnus-configure-windows (cdr quit-config)))))))
10024
10025 ;;; Dead summaries.
10026
10027 (defvar gnus-dead-summary-mode-map nil)
10028
10029 (if gnus-dead-summary-mode-map
10030     nil
10031   (setq gnus-dead-summary-mode-map (make-keymap))
10032   (suppress-keymap gnus-dead-summary-mode-map)
10033   (substitute-key-definition
10034    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10035   (let ((keys '("\C-d" "\r" "\177")))
10036     (while keys
10037       (define-key gnus-dead-summary-mode-map
10038         (pop keys) 'gnus-summary-wake-up-the-dead))))
10039
10040 (defvar gnus-dead-summary-mode nil
10041   "Minor mode for Gnus summary buffers.")
10042
10043 (defun gnus-dead-summary-mode (&optional arg)
10044   "Minor mode for Gnus summary buffers."
10045   (interactive "P")
10046   (when (eq major-mode 'gnus-summary-mode)
10047     (make-local-variable 'gnus-dead-summary-mode)
10048     (setq gnus-dead-summary-mode
10049           (if (null arg) (not gnus-dead-summary-mode)
10050             (> (prefix-numeric-value arg) 0)))
10051     (when gnus-dead-summary-mode
10052       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10053         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10054       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10055         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10056               minor-mode-map-alist)))))
10057
10058 (defun gnus-deaden-summary ()
10059   "Make the current summary buffer into a dead summary buffer."
10060   ;; Kill any previous dead summary buffer.
10061   (when (and gnus-dead-summary
10062              (buffer-name gnus-dead-summary))
10063     (save-excursion
10064       (set-buffer gnus-dead-summary)
10065       (when gnus-dead-summary-mode
10066         (kill-buffer (current-buffer)))))
10067   ;; Make this the current dead summary.
10068   (setq gnus-dead-summary (current-buffer))
10069   (gnus-dead-summary-mode 1)
10070   (let ((name (buffer-name)))
10071     (when (string-match "Summary" name)
10072       (rename-buffer
10073        (concat (substring name 0 (match-beginning 0)) "Dead "
10074                (substring name (match-beginning 0))) t))))
10075
10076 (defun gnus-kill-or-deaden-summary (buffer)
10077   "Kill or deaden the summary BUFFER."
10078   (when (and (buffer-name buffer)
10079              (not gnus-single-article-buffer))
10080     (save-excursion
10081       (set-buffer buffer)
10082       (gnus-kill-buffer gnus-article-buffer)
10083       (gnus-kill-buffer gnus-original-article-buffer)))
10084   (cond (gnus-kill-summary-on-exit
10085          (when (and gnus-use-trees
10086                     (and (get-buffer buffer)
10087                          (buffer-name (get-buffer buffer))))
10088            (save-excursion
10089              (set-buffer (get-buffer buffer))
10090              (gnus-tree-close gnus-newsgroup-name)))
10091          (gnus-kill-buffer buffer))
10092         ((and (get-buffer buffer)
10093               (buffer-name (get-buffer buffer)))
10094          (save-excursion
10095            (set-buffer buffer)
10096            (gnus-deaden-summary)))))
10097
10098 (defun gnus-summary-wake-up-the-dead (&rest args)
10099   "Wake up the dead summary buffer."
10100   (interactive)
10101   (gnus-dead-summary-mode -1)
10102   (let ((name (buffer-name)))
10103     (when (string-match "Dead " name)
10104       (rename-buffer
10105        (concat (substring name 0 (match-beginning 0))
10106                (substring name (match-end 0))) t)))
10107   (gnus-message 3 "This dead summary is now alive again"))
10108
10109 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10110 (defun gnus-summary-fetch-faq (&optional faq-dir)
10111   "Fetch the FAQ for the current group.
10112 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10113 in."
10114   (interactive
10115    (list
10116     (if current-prefix-arg
10117         (completing-read
10118          "Faq dir: " (and (listp gnus-group-faq-directory)
10119                           gnus-group-faq-directory)))))
10120   (let (gnus-faq-buffer)
10121     (and (setq gnus-faq-buffer
10122                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10123          (gnus-configure-windows 'summary-faq))))
10124
10125 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10126 (defun gnus-summary-describe-group (&optional force)
10127   "Describe the current newsgroup."
10128   (interactive "P")
10129   (gnus-group-describe-group force gnus-newsgroup-name))
10130
10131 (defun gnus-summary-describe-briefly ()
10132   "Describe summary mode commands briefly."
10133   (interactive)
10134   (gnus-message 6
10135                 (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")))
10136
10137 ;; Walking around group mode buffer from summary mode.
10138
10139 (defun gnus-summary-next-group (&optional no-article target-group backward)
10140   "Exit current newsgroup and then select next unread newsgroup.
10141 If prefix argument NO-ARTICLE is non-nil, no article is selected
10142 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10143 previous group instead."
10144   (interactive "P")
10145   (gnus-set-global-variables)
10146   (let ((current-group gnus-newsgroup-name)
10147         (current-buffer (current-buffer))
10148         entered)
10149     ;; First we semi-exit this group to update Xrefs and all variables.
10150     ;; We can't do a real exit, because the window conf must remain
10151     ;; the same in case the user is prompted for info, and we don't
10152     ;; want the window conf to change before that...
10153     (gnus-summary-exit t)
10154     (while (not entered)
10155       ;; Then we find what group we are supposed to enter.
10156       (set-buffer gnus-group-buffer)
10157       (gnus-group-jump-to-group current-group)
10158       (setq target-group
10159             (or target-group
10160                 (if (eq gnus-keep-same-level 'best)
10161                     (gnus-summary-best-group gnus-newsgroup-name)
10162                   (gnus-summary-search-group backward gnus-keep-same-level))))
10163       (if (not target-group)
10164           ;; There are no further groups, so we return to the group
10165           ;; buffer.
10166           (progn
10167             (gnus-message 5 "Returning to the group buffer")
10168             (setq entered t)
10169             (set-buffer current-buffer)
10170             (gnus-summary-exit))
10171         ;; We try to enter the target group.
10172         (gnus-group-jump-to-group target-group)
10173         (let ((unreads (gnus-group-group-unread)))
10174           (if (and (or (eq t unreads)
10175                        (and unreads (not (zerop unreads))))
10176                    (gnus-summary-read-group
10177                     target-group nil no-article current-buffer))
10178               (setq entered t)
10179             (setq current-group target-group
10180                   target-group nil)))))))
10181
10182 (defun gnus-summary-prev-group (&optional no-article)
10183   "Exit current newsgroup and then select previous unread newsgroup.
10184 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10185   (interactive "P")
10186   (gnus-summary-next-group no-article nil t))
10187
10188 ;; Walking around summary lines.
10189
10190 (defun gnus-summary-first-subject (&optional unread)
10191   "Go to the first unread subject.
10192 If UNREAD is non-nil, go to the first unread article.
10193 Returns the article selected or nil if there are no unread articles."
10194   (interactive "P")
10195   (prog1
10196       (cond
10197        ;; Empty summary.
10198        ((null gnus-newsgroup-data)
10199         (gnus-message 3 "No articles in the group")
10200         nil)
10201        ;; Pick the first article.
10202        ((not unread)
10203         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10204         (gnus-data-number (car gnus-newsgroup-data)))
10205        ;; No unread articles.
10206        ((null gnus-newsgroup-unreads)
10207         (gnus-message 3 "No more unread articles")
10208         nil)
10209        ;; Find the first unread article.
10210        (t
10211         (let ((data gnus-newsgroup-data))
10212           (while (and data
10213                       (not (gnus-data-unread-p (car data))))
10214             (setq data (cdr data)))
10215           (if data
10216               (progn
10217                 (goto-char (gnus-data-pos (car data)))
10218                 (gnus-data-number (car data)))))))
10219     (gnus-summary-position-point)))
10220
10221 (defun gnus-summary-next-subject (n &optional unread dont-display)
10222   "Go to next N'th summary line.
10223 If N is negative, go to the previous N'th subject line.
10224 If UNREAD is non-nil, only unread articles are selected.
10225 The difference between N and the actual number of steps taken is
10226 returned."
10227   (interactive "p")
10228   (let ((backward (< n 0))
10229         (n (abs n)))
10230     (while (and (> n 0)
10231                 (if backward
10232                     (gnus-summary-find-prev unread)
10233                   (gnus-summary-find-next unread)))
10234       (setq n (1- n)))
10235     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10236                                (if unread " unread" "")))
10237     (unless dont-display
10238       (gnus-summary-recenter)
10239       (gnus-summary-position-point))
10240     n))
10241
10242 (defun gnus-summary-next-unread-subject (n)
10243   "Go to next N'th unread summary line."
10244   (interactive "p")
10245   (gnus-summary-next-subject n t))
10246
10247 (defun gnus-summary-prev-subject (n &optional unread)
10248   "Go to previous N'th summary line.
10249 If optional argument UNREAD is non-nil, only unread article is selected."
10250   (interactive "p")
10251   (gnus-summary-next-subject (- n) unread))
10252
10253 (defun gnus-summary-prev-unread-subject (n)
10254   "Go to previous N'th unread summary line."
10255   (interactive "p")
10256   (gnus-summary-next-subject (- n) t))
10257
10258 (defun gnus-summary-goto-subject (article &optional force silent)
10259   "Go the subject line of ARTICLE.
10260 If FORCE, also allow jumping to articles not currently shown."
10261   (let ((b (point))
10262         (data (gnus-data-find article)))
10263     ;; We read in the article if we have to.
10264     (and (not data)
10265          force
10266          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10267          (setq data (gnus-data-find article)))
10268     (goto-char b)
10269     (if (not data)
10270         (progn
10271           (unless silent
10272             (gnus-message 3 "Can't find article %d" article))
10273           nil)
10274       (goto-char (gnus-data-pos data))
10275       article)))
10276
10277 ;; Walking around summary lines with displaying articles.
10278
10279 (defun gnus-summary-expand-window (&optional arg)
10280   "Make the summary buffer take up the entire Emacs frame.
10281 Given a prefix, will force an `article' buffer configuration."
10282   (interactive "P")
10283   (gnus-set-global-variables)
10284   (if arg
10285       (gnus-configure-windows 'article 'force)
10286     (gnus-configure-windows 'summary 'force)))
10287
10288 (defun gnus-summary-display-article (article &optional all-header)
10289   "Display ARTICLE in article buffer."
10290   (gnus-set-global-variables)
10291   (if (null article)
10292       nil
10293     (prog1
10294         (if gnus-summary-display-article-function
10295             (funcall gnus-summary-display-article-function article all-header)
10296           (gnus-article-prepare article all-header))
10297       (run-hooks 'gnus-select-article-hook)
10298       (unless (zerop gnus-current-article)
10299         (gnus-summary-goto-subject gnus-current-article))
10300       (gnus-summary-recenter)
10301       (when gnus-use-trees
10302         (gnus-possibly-generate-tree article)
10303         (gnus-highlight-selected-tree article))
10304       ;; Successfully display article.
10305       (gnus-article-set-window-start
10306        (cdr (assq article gnus-newsgroup-bookmarks))))))
10307
10308 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10309   "Select the current article.
10310 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10311 non-nil, the article will be re-fetched even if it already present in
10312 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10313 be displayed."
10314   ;; Make sure we are in the summary buffer to work around bbdb bug.
10315   (unless (eq major-mode 'gnus-summary-mode)
10316     (set-buffer gnus-summary-buffer))
10317   (let ((article (or article (gnus-summary-article-number)))
10318         (all-headers (not (not all-headers))) ;Must be T or NIL.
10319         gnus-summary-display-article-function
10320         did)
10321     (and (not pseudo)
10322          (gnus-summary-article-pseudo-p article)
10323          (error "This is a pseudo-article."))
10324     (prog1
10325         (save-excursion
10326           (set-buffer gnus-summary-buffer)
10327           (if (or (and gnus-single-article-buffer
10328                        (or (null gnus-current-article)
10329                            (null gnus-article-current)
10330                            (null (get-buffer gnus-article-buffer))
10331                            (not (eq article (cdr gnus-article-current)))
10332                            (not (equal (car gnus-article-current)
10333                                        gnus-newsgroup-name))))
10334                   (and (not gnus-single-article-buffer)
10335                        (or (null gnus-current-article)
10336                            (not (eq gnus-current-article article))))
10337                   force)
10338               ;; The requested article is different from the current article.
10339               (prog1
10340                   (gnus-summary-display-article article all-headers)
10341                 (setq did article))
10342             (if (or all-headers gnus-show-all-headers)
10343                 (gnus-article-show-all-headers))
10344             'old))
10345       (if did
10346           (gnus-article-set-window-start
10347            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10348
10349 (defun gnus-summary-set-current-mark (&optional current-mark)
10350   "Obsolete function."
10351   nil)
10352
10353 (defun gnus-summary-next-article (&optional unread subject backward push)
10354   "Select the next article.
10355 If UNREAD, only unread articles are selected.
10356 If SUBJECT, only articles with SUBJECT are selected.
10357 If BACKWARD, the previous article is selected instead of the next."
10358   (interactive "P")
10359   (gnus-set-global-variables)
10360   (cond
10361    ;; Is there such an article?
10362    ((and (gnus-summary-search-forward unread subject backward)
10363          (or (gnus-summary-display-article (gnus-summary-article-number))
10364              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10365     (gnus-summary-position-point))
10366    ;; If not, we try the first unread, if that is wanted.
10367    ((and subject
10368          gnus-auto-select-same
10369          (or (gnus-summary-first-unread-article)
10370              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10371     (gnus-summary-position-point)
10372     (gnus-message 6 "Wrapped"))
10373    ;; Try to get next/previous article not displayed in this group.
10374    ((and gnus-auto-extend-newsgroup
10375          (not unread) (not subject))
10376     (gnus-summary-goto-article
10377      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10378      nil t))
10379    ;; Go to next/previous group.
10380    (t
10381     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10382         (gnus-summary-jump-to-group gnus-newsgroup-name))
10383     (let ((cmd last-command-char)
10384           (group
10385            (if (eq gnus-keep-same-level 'best)
10386                (gnus-summary-best-group gnus-newsgroup-name)
10387              (gnus-summary-search-group backward gnus-keep-same-level))))
10388       ;; For some reason, the group window gets selected.  We change
10389       ;; it back.
10390       (select-window (get-buffer-window (current-buffer)))
10391       ;; Select next unread newsgroup automagically.
10392       (cond
10393        ((not gnus-auto-select-next)
10394         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10395        ((or (eq gnus-auto-select-next 'quietly)
10396             (and (eq gnus-auto-select-next 'slightly-quietly)
10397                  push)
10398             (and (eq gnus-auto-select-next 'almost-quietly)
10399                  (gnus-summary-last-article-p)))
10400         ;; Select quietly.
10401         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10402             (gnus-summary-exit)
10403           (gnus-message 7 "No more%s articles (%s)..."
10404                         (if unread " unread" "")
10405                         (if group (concat "selecting " group)
10406                           "exiting"))
10407           (gnus-summary-next-group nil group backward)))
10408        (t
10409         (gnus-summary-walk-group-buffer
10410          gnus-newsgroup-name cmd unread backward)))))))
10411
10412 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10413   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10414                       (?\C-p (gnus-group-prev-unread-group 1))))
10415         keve key group ended)
10416     (save-excursion
10417       (set-buffer gnus-group-buffer)
10418       (gnus-summary-jump-to-group from-group)
10419       (setq group
10420             (if (eq gnus-keep-same-level 'best)
10421                 (gnus-summary-best-group gnus-newsgroup-name)
10422               (gnus-summary-search-group backward gnus-keep-same-level))))
10423     (while (not ended)
10424       (gnus-message
10425        5 "No more%s articles%s" (if unread " unread" "")
10426        (if (and group
10427                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10428            (format " (Type %s for %s [%s])"
10429                    (single-key-description cmd) group
10430                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10431          (format " (Type %s to exit %s)"
10432                  (single-key-description cmd)
10433                  gnus-newsgroup-name)))
10434       ;; Confirm auto selection.
10435       (setq key (car (setq keve (gnus-read-event-char))))
10436       (setq ended t)
10437       (cond
10438        ((assq key keystrokes)
10439         (let ((obuf (current-buffer)))
10440           (switch-to-buffer gnus-group-buffer)
10441           (and group
10442                (gnus-group-jump-to-group group))
10443           (eval (cadr (assq key keystrokes)))
10444           (setq group (gnus-group-group-name))
10445           (switch-to-buffer obuf))
10446         (setq ended nil))
10447        ((equal key cmd)
10448         (if (or (not group)
10449                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10450             (gnus-summary-exit)
10451           (gnus-summary-next-group nil group backward)))
10452        (t
10453         (push (cdr keve) unread-command-events))))))
10454
10455 (defun gnus-read-event-char ()
10456   "Get the next event."
10457   (let ((event (read-event)))
10458     (cons (and (numberp event) event) event)))
10459
10460 (defun gnus-summary-next-unread-article ()
10461   "Select unread article after current one."
10462   (interactive)
10463   (gnus-summary-next-article t (and gnus-auto-select-same
10464                                     (gnus-summary-article-subject))))
10465
10466 (defun gnus-summary-prev-article (&optional unread subject)
10467   "Select the article after the current one.
10468 If UNREAD is non-nil, only unread articles are selected."
10469   (interactive "P")
10470   (gnus-summary-next-article unread subject t))
10471
10472 (defun gnus-summary-prev-unread-article ()
10473   "Select unred article before current one."
10474   (interactive)
10475   (gnus-summary-prev-article t (and gnus-auto-select-same
10476                                     (gnus-summary-article-subject))))
10477
10478 (defun gnus-summary-next-page (&optional lines circular)
10479   "Show next page of the selected article.
10480 If at the end of the current article, select the next article.
10481 LINES says how many lines should be scrolled up.
10482
10483 If CIRCULAR is non-nil, go to the start of the article instead of
10484 selecting the next article when reaching the end of the current
10485 article."
10486   (interactive "P")
10487   (setq gnus-summary-buffer (current-buffer))
10488   (gnus-set-global-variables)
10489   (let ((article (gnus-summary-article-number))
10490         (endp nil))
10491     (gnus-configure-windows 'article)
10492     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10493         (if (and (eq gnus-summary-goto-unread 'never)
10494                  (not (gnus-summary-last-article-p article)))
10495             (gnus-summary-next-article)
10496           (gnus-summary-next-unread-article))
10497       (if (or (null gnus-current-article)
10498               (null gnus-article-current)
10499               (/= article (cdr gnus-article-current))
10500               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10501           ;; Selected subject is different from current article's.
10502           (gnus-summary-display-article article)
10503         (gnus-eval-in-buffer-window
10504          gnus-article-buffer
10505          (setq endp (gnus-article-next-page lines)))
10506         (if endp
10507             (cond (circular
10508                    (gnus-summary-beginning-of-article))
10509                   (lines
10510                    (gnus-message 3 "End of message"))
10511                   ((null lines)
10512                    (if (and (eq gnus-summary-goto-unread 'never)
10513                             (not (gnus-summary-last-article-p article)))
10514                        (gnus-summary-next-article)
10515                      (gnus-summary-next-unread-article)))))))
10516     (gnus-summary-recenter)
10517     (gnus-summary-position-point)))
10518
10519 (defun gnus-summary-prev-page (&optional lines)
10520   "Show previous page of selected article.
10521 Argument LINES specifies lines to be scrolled down."
10522   (interactive "P")
10523   (gnus-set-global-variables)
10524   (let ((article (gnus-summary-article-number)))
10525     (gnus-configure-windows 'article)
10526     (if (or (null gnus-current-article)
10527             (null gnus-article-current)
10528             (/= article (cdr gnus-article-current))
10529             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10530         ;; Selected subject is different from current article's.
10531         (gnus-summary-display-article article)
10532       (gnus-summary-recenter)
10533       (gnus-eval-in-buffer-window gnus-article-buffer
10534                                   (gnus-article-prev-page lines))))
10535   (gnus-summary-position-point))
10536
10537 (defun gnus-summary-scroll-up (lines)
10538   "Scroll up (or down) one line current article.
10539 Argument LINES specifies lines to be scrolled up (or down if negative)."
10540   (interactive "p")
10541   (gnus-set-global-variables)
10542   (gnus-configure-windows 'article)
10543   (gnus-summary-show-thread)
10544   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10545     (gnus-eval-in-buffer-window
10546      gnus-article-buffer
10547      (cond ((> lines 0)
10548             (if (gnus-article-next-page lines)
10549                 (gnus-message 3 "End of message")))
10550            ((< lines 0)
10551             (gnus-article-prev-page (- lines))))))
10552   (gnus-summary-recenter)
10553   (gnus-summary-position-point))
10554
10555 (defun gnus-summary-next-same-subject ()
10556   "Select next article which has the same subject as current one."
10557   (interactive)
10558   (gnus-set-global-variables)
10559   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10560
10561 (defun gnus-summary-prev-same-subject ()
10562   "Select previous article which has the same subject as current one."
10563   (interactive)
10564   (gnus-set-global-variables)
10565   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10566
10567 (defun gnus-summary-next-unread-same-subject ()
10568   "Select next unread article which has the same subject as current one."
10569   (interactive)
10570   (gnus-set-global-variables)
10571   (gnus-summary-next-article t (gnus-summary-article-subject)))
10572
10573 (defun gnus-summary-prev-unread-same-subject ()
10574   "Select previous unread article which has the same subject as current one."
10575   (interactive)
10576   (gnus-set-global-variables)
10577   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10578
10579 (defun gnus-summary-first-unread-article ()
10580   "Select the first unread article.
10581 Return nil if there are no unread articles."
10582   (interactive)
10583   (gnus-set-global-variables)
10584   (prog1
10585       (if (gnus-summary-first-subject t)
10586           (progn
10587             (gnus-summary-show-thread)
10588             (gnus-summary-first-subject t)
10589             (gnus-summary-display-article (gnus-summary-article-number))))
10590     (gnus-summary-position-point)))
10591
10592 (defun gnus-summary-best-unread-article ()
10593   "Select the unread article with the highest score."
10594   (interactive)
10595   (gnus-set-global-variables)
10596   (let ((best -1000000)
10597         (data gnus-newsgroup-data)
10598         article score)
10599     (while data
10600       (and (gnus-data-unread-p (car data))
10601            (> (setq score
10602                     (gnus-summary-article-score (gnus-data-number (car data))))
10603               best)
10604            (setq best score
10605                  article (gnus-data-number (car data))))
10606       (setq data (cdr data)))
10607     (prog1
10608         (if article
10609             (gnus-summary-goto-article article)
10610           (error "No unread articles"))
10611       (gnus-summary-position-point))))
10612
10613 (defun gnus-summary-last-subject ()
10614   "Go to the last displayed subject line in the group."
10615   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10616     (when article
10617       (gnus-summary-goto-subject article))))
10618
10619 (defun gnus-summary-goto-article (article &optional all-headers force)
10620   "Fetch ARTICLE and display it if it exists.
10621 If ALL-HEADERS is non-nil, no header lines are hidden."
10622   (interactive
10623    (list
10624     (string-to-int
10625      (completing-read
10626       "Article number: "
10627       (mapcar (lambda (number) (list (int-to-string number)))
10628               gnus-newsgroup-limit)))
10629     current-prefix-arg
10630     t))
10631   (prog1
10632       (if (gnus-summary-goto-subject article force)
10633           (gnus-summary-display-article article all-headers)
10634         (gnus-message 4 "Couldn't go to article %s" article) nil)
10635     (gnus-summary-position-point)))
10636
10637 (defun gnus-summary-goto-last-article ()
10638   "Go to the previously read article."
10639   (interactive)
10640   (prog1
10641       (and gnus-last-article
10642            (gnus-summary-goto-article gnus-last-article))
10643     (gnus-summary-position-point)))
10644
10645 (defun gnus-summary-pop-article (number)
10646   "Pop one article off the history and go to the previous.
10647 NUMBER articles will be popped off."
10648   (interactive "p")
10649   (let (to)
10650     (setq gnus-newsgroup-history
10651           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10652     (if to
10653         (gnus-summary-goto-article (car to))
10654       (error "Article history empty")))
10655   (gnus-summary-position-point))
10656
10657 ;; Summary commands and functions for limiting the summary buffer.
10658
10659 (defun gnus-summary-limit-to-articles (n)
10660   "Limit the summary buffer to the next N articles.
10661 If not given a prefix, use the process marked articles instead."
10662   (interactive "P")
10663   (gnus-set-global-variables)
10664   (prog1
10665       (let ((articles (gnus-summary-work-articles n)))
10666         (setq gnus-newsgroup-processable nil)
10667         (gnus-summary-limit articles))
10668     (gnus-summary-position-point)))
10669
10670 (defun gnus-summary-pop-limit (&optional total)
10671   "Restore the previous limit.
10672 If given a prefix, remove all limits."
10673   (interactive "P")
10674   (gnus-set-global-variables)
10675   (when total 
10676     (setq gnus-newsgroup-limits
10677           (list (mapcar (lambda (h) (mail-header-number h))
10678                         gnus-newsgroup-headers))))
10679   (unless gnus-newsgroup-limits
10680     (error "No limit to pop"))
10681   (prog1
10682       (gnus-summary-limit nil 'pop)
10683     (gnus-summary-position-point)))
10684
10685 (defun gnus-summary-limit-to-subject (subject &optional header)
10686   "Limit the summary buffer to articles that have subjects that match a regexp."
10687   (interactive "sRegexp: ")
10688   (unless header
10689     (setq header "subject"))
10690   (when (not (equal "" subject))
10691     (prog1
10692         (let ((articles (gnus-summary-find-matching
10693                          (or header "subject") subject 'all)))
10694           (or articles (error "Found no matches for \"%s\"" subject))
10695           (gnus-summary-limit articles))
10696       (gnus-summary-position-point))))
10697
10698 (defun gnus-summary-limit-to-author (from)
10699   "Limit the summary buffer to articles that have authors that match a regexp."
10700   (interactive "sRegexp: ")
10701   (gnus-summary-limit-to-subject from "from"))
10702
10703 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10704 (make-obsolete
10705  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10706
10707 (defun gnus-summary-limit-to-unread (&optional all)
10708   "Limit the summary buffer to articles that are not marked as read.
10709 If ALL is non-nil, limit strictly to unread articles."
10710   (interactive "P")
10711   (if all
10712       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10713     (gnus-summary-limit-to-marks
10714      ;; Concat all the marks that say that an article is read and have
10715      ;; those removed.
10716      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10717            gnus-killed-mark gnus-kill-file-mark
10718            gnus-low-score-mark gnus-expirable-mark
10719            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10720      'reverse)))
10721
10722 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10723 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10724
10725 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10726   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10727 If REVERSE, limit the summary buffer to articles that are not marked
10728 with MARKS.  MARKS can either be a string of marks or a list of marks.
10729 Returns how many articles were removed."
10730   (interactive "sMarks: ")
10731   (gnus-set-global-variables)
10732   (prog1
10733       (let ((data gnus-newsgroup-data)
10734             (marks (if (listp marks) marks
10735                      (append marks nil))) ; Transform to list.
10736             articles)
10737         (while data
10738           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10739                  (memq (gnus-data-mark (car data)) marks))
10740                (setq articles (cons (gnus-data-number (car data)) articles)))
10741           (setq data (cdr data)))
10742         (gnus-summary-limit articles))
10743     (gnus-summary-position-point)))
10744
10745 (defun gnus-summary-limit-to-score (&optional score)
10746   "Limit to articles with score at or above SCORE."
10747   (interactive "P")
10748   (gnus-set-global-variables)
10749   (setq score (if score
10750                   (prefix-numeric-value score)
10751                 (or gnus-summary-default-score 0)))
10752   (let ((data gnus-newsgroup-data)
10753         articles)
10754     (while data
10755       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10756                 score)
10757         (push (gnus-data-number (car data)) articles))
10758       (setq data (cdr data)))
10759     (prog1
10760         (gnus-summary-limit articles)
10761       (gnus-summary-position-point))))
10762
10763 (defun gnus-summary-limit-include-dormant ()
10764   "Display all the hidden articles that are marked as dormant."
10765   (interactive)
10766   (gnus-set-global-variables)
10767   (or gnus-newsgroup-dormant
10768       (error "There are no dormant articles in this group"))
10769   (prog1
10770       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10771     (gnus-summary-position-point)))
10772
10773 (defun gnus-summary-limit-exclude-dormant ()
10774   "Hide all dormant articles."
10775   (interactive)
10776   (gnus-set-global-variables)
10777   (prog1
10778       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10779     (gnus-summary-position-point)))
10780
10781 (defun gnus-summary-limit-exclude-childless-dormant ()
10782   "Hide all dormant articles that have no children."
10783   (interactive)
10784   (gnus-set-global-variables)
10785   (let ((data (gnus-data-list t))
10786         articles d children)
10787     ;; Find all articles that are either not dormant or have
10788     ;; children.
10789     (while (setq d (pop data))
10790       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10791                 (and (setq children 
10792                            (gnus-article-children (gnus-data-number d)))
10793                      (let (found)
10794                        (while children
10795                          (when (memq (car children) articles)
10796                            (setq children nil
10797                                  found t))
10798                          (pop children))
10799                        found)))
10800         (push (gnus-data-number d) articles)))
10801     ;; Do the limiting.
10802     (prog1
10803         (gnus-summary-limit articles)
10804       (gnus-summary-position-point))))
10805
10806 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10807   "Mark all unread excluded articles as read.
10808 If ALL, mark even excluded ticked and dormants as read."
10809   (interactive "P")
10810   (let ((articles (gnus-sorted-complement
10811                    (sort
10812                     (mapcar (lambda (h) (mail-header-number h))
10813                             gnus-newsgroup-headers)
10814                     '<)
10815                    (sort gnus-newsgroup-limit '<)))
10816         article)
10817     (setq gnus-newsgroup-unreads nil)
10818     (if all
10819         (setq gnus-newsgroup-dormant nil
10820               gnus-newsgroup-marked nil
10821               gnus-newsgroup-reads
10822               (nconc
10823                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10824                gnus-newsgroup-reads))
10825       (while (setq article (pop articles))
10826         (unless (or (memq article gnus-newsgroup-dormant)
10827                     (memq article gnus-newsgroup-marked))
10828           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10829
10830 (defun gnus-summary-limit (articles &optional pop)
10831   (if pop
10832       ;; We pop the previous limit off the stack and use that.
10833       (setq articles (car gnus-newsgroup-limits)
10834             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10835     ;; We use the new limit, so we push the old limit on the stack.
10836     (setq gnus-newsgroup-limits
10837           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10838   ;; Set the limit.
10839   (setq gnus-newsgroup-limit articles)
10840   (let ((total (length gnus-newsgroup-data))
10841         (data (gnus-data-find-list (gnus-summary-article-number)))
10842         found)
10843     ;; This will do all the work of generating the new summary buffer
10844     ;; according to the new limit.
10845     (gnus-summary-prepare)
10846     ;; Hide any threads, possibly.
10847     (and gnus-show-threads
10848          gnus-thread-hide-subtree
10849          (gnus-summary-hide-all-threads))
10850     ;; Try to return to the article you were at, or one in the
10851     ;; neighborhood.
10852     (if data
10853         ;; We try to find some article after the current one.
10854         (while data
10855           (and (gnus-summary-goto-subject
10856                 (gnus-data-number (car data)) nil t)
10857                (setq data nil
10858                      found t))
10859           (setq data (cdr data))))
10860     (or found
10861         ;; If there is no data, that means that we were after the last
10862         ;; article.  The same goes when we can't find any articles
10863         ;; after the current one.
10864         (progn
10865           (goto-char (point-max))
10866           (gnus-summary-find-prev)))
10867     ;; We return how many articles were removed from the summary
10868     ;; buffer as a result of the new limit.
10869     (- total (length gnus-newsgroup-data))))
10870
10871 (defsubst gnus-invisible-cut-children (threads)
10872   (let ((num 0))
10873     (while threads
10874       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10875         (incf num))
10876       (pop threads))
10877     (< num 2)))
10878
10879 (defsubst gnus-cut-thread (thread)
10880   "Go forwards in the thread until we find an article that we want to display."
10881   (when (or (eq gnus-fetch-old-headers 'some)
10882             (eq gnus-build-sparse-threads 'some)
10883             (eq gnus-build-sparse-threads 'more))
10884     ;; Deal with old-fetched headers and sparse threads.
10885     (while (and
10886             thread
10887             (or
10888              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10889              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10890             (or (<= (length (cdr thread)) 1)
10891                 (gnus-invisible-cut-children (cdr thread))))
10892       (setq thread (cadr thread))))
10893   thread)
10894
10895 (defun gnus-cut-threads (threads)
10896   "Cut off all uninteresting articles from the beginning of threads."
10897   (when (or (eq gnus-fetch-old-headers 'some)
10898             (eq gnus-build-sparse-threads 'some)
10899             (eq gnus-build-sparse-threads 'more))
10900     (let ((th threads))
10901       (while th
10902         (setcar th (gnus-cut-thread (car th)))
10903         (setq th (cdr th)))))
10904   ;; Remove nixed out threads.
10905   (delq nil threads))
10906
10907 (defun gnus-summary-initial-limit (&optional show-if-empty)
10908   "Figure out what the initial limit is supposed to be on group entry.
10909 This entails weeding out unwanted dormants, low-scored articles,
10910 fetch-old-headers verbiage, and so on."
10911   ;; Most groups have nothing to remove.
10912   (if (or gnus-inhibit-limiting
10913           (and (null gnus-newsgroup-dormant)
10914                (not (eq gnus-fetch-old-headers 'some))
10915                (null gnus-summary-expunge-below)
10916                (not (eq gnus-build-sparse-threads 'some))
10917                (not (eq gnus-build-sparse-threads 'more))
10918                (null gnus-thread-expunge-below)
10919                (not gnus-use-nocem)))
10920       () ; Do nothing.
10921     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10922     (setq gnus-newsgroup-limit nil)
10923     (mapatoms
10924      (lambda (node)
10925        (unless (car (symbol-value node))
10926          ;; These threads have no parents -- they are roots.
10927          (let ((nodes (cdr (symbol-value node)))
10928                thread)
10929            (while nodes
10930              (if (and gnus-thread-expunge-below
10931                       (< (gnus-thread-total-score (car nodes))
10932                          gnus-thread-expunge-below))
10933                  (gnus-expunge-thread (pop nodes))
10934                (setq thread (pop nodes))
10935                (gnus-summary-limit-children thread))))))
10936      gnus-newsgroup-dependencies)
10937     ;; If this limitation resulted in an empty group, we might
10938     ;; pop the previous limit and use it instead.
10939     (when (and (not gnus-newsgroup-limit)
10940                show-if-empty)
10941       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10942     gnus-newsgroup-limit))
10943
10944 (defun gnus-summary-limit-children (thread)
10945   "Return 1 if this subthread is visible and 0 if it is not."
10946   ;; First we get the number of visible children to this thread.  This
10947   ;; is done by recursing down the thread using this function, so this
10948   ;; will really go down to a leaf article first, before slowly
10949   ;; working its way up towards the root.
10950   (when thread
10951     (let ((children
10952            (if (cdr thread)
10953                (apply '+ (mapcar 'gnus-summary-limit-children
10954                                  (cdr thread)))
10955              0))
10956           (number (mail-header-number (car thread)))
10957           score)
10958       (if (or
10959            ;; If this article is dormant and has absolutely no visible
10960            ;; children, then this article isn't visible.
10961            (and (memq number gnus-newsgroup-dormant)
10962                 (= children 0))
10963            ;; If this is "fetch-old-headered" and there is only one
10964            ;; visible child (or less), then we don't want this article.
10965            (and (eq gnus-fetch-old-headers 'some)
10966                 (memq number gnus-newsgroup-ancient)
10967                 (zerop children))
10968            ;; If this is a sparsely inserted article with no children,
10969            ;; we don't want it.
10970            (and (eq gnus-build-sparse-threads 'some)
10971                 (memq number gnus-newsgroup-sparse)
10972                 (zerop children))
10973            ;; If we use expunging, and this article is really
10974            ;; low-scored, then we don't want this article.
10975            (when (and gnus-summary-expunge-below
10976                       (< (setq score
10977                                (or (cdr (assq number gnus-newsgroup-scored))
10978                                    gnus-summary-default-score))
10979                          gnus-summary-expunge-below))
10980              ;; We increase the expunge-tally here, but that has
10981              ;; nothing to do with the limits, really.
10982              (incf gnus-newsgroup-expunged-tally)
10983              ;; We also mark as read here, if that's wanted.
10984              (when (and gnus-summary-mark-below
10985                         (< score gnus-summary-mark-below))
10986                (setq gnus-newsgroup-unreads
10987                      (delq number gnus-newsgroup-unreads))
10988                (if gnus-newsgroup-auto-expire
10989                    (push number gnus-newsgroup-expirable)
10990                  (push (cons number gnus-low-score-mark)
10991                        gnus-newsgroup-reads)))
10992              t)
10993            (and gnus-use-nocem
10994                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10995           ;; Nope, invisible article.
10996           0
10997         ;; Ok, this article is to be visible, so we add it to the limit
10998         ;; and return 1.
10999         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11000         1))))
11001
11002 (defun gnus-expunge-thread (thread)
11003   "Mark all articles in THREAD as read."
11004   (let* ((number (mail-header-number (car thread))))
11005     (incf gnus-newsgroup-expunged-tally)
11006     ;; We also mark as read here, if that's wanted.
11007     (setq gnus-newsgroup-unreads
11008           (delq number gnus-newsgroup-unreads))
11009     (if gnus-newsgroup-auto-expire
11010         (push number gnus-newsgroup-expirable)
11011       (push (cons number gnus-low-score-mark)
11012             gnus-newsgroup-reads)))
11013   ;; Go recursively through all subthreads.
11014   (mapcar 'gnus-expunge-thread (cdr thread)))
11015
11016 ;; Summary article oriented commands
11017
11018 (defun gnus-summary-refer-parent-article (n)
11019   "Refer parent article N times.
11020 The difference between N and the number of articles fetched is returned."
11021   (interactive "p")
11022   (gnus-set-global-variables)
11023   (while
11024       (and
11025        (> n 0)
11026        (let* ((header (gnus-summary-article-header))
11027               (ref
11028                ;; If we try to find the parent of the currently
11029                ;; displayed article, then we take a look at the actual
11030                ;; References header, since this is slightly more
11031                ;; reliable than the References field we got from the
11032                ;; server.
11033                (if (and (eq (mail-header-number header)
11034                             (cdr gnus-article-current))
11035                         (equal gnus-newsgroup-name
11036                                (car gnus-article-current)))
11037                    (save-excursion
11038                      (set-buffer gnus-original-article-buffer)
11039                      (nnheader-narrow-to-headers)
11040                      (prog1
11041                          (message-fetch-field "references")
11042                        (widen)))
11043                  ;; It's not the current article, so we take a bet on
11044                  ;; the value we got from the server.
11045                  (mail-header-references header))))
11046          (if (setq ref (or ref (mail-header-references header)))
11047              (or (gnus-summary-refer-article (gnus-parent-id ref))
11048                  (gnus-message 1 "Couldn't find parent"))
11049            (gnus-message 1 "No references in article %d"
11050                          (gnus-summary-article-number))
11051            nil)))
11052     (setq n (1- n)))
11053   (gnus-summary-position-point)
11054   n)
11055
11056 (defun gnus-summary-refer-references ()
11057   "Fetch all articles mentioned in the References header.
11058 Return how many articles were fetched."
11059   (interactive)
11060   (gnus-set-global-variables)
11061   (let ((ref (mail-header-references (gnus-summary-article-header)))
11062         (current (gnus-summary-article-number))
11063         (n 0))
11064     ;; For each Message-ID in the References header...
11065     (while (string-match "<[^>]*>" ref)
11066       (incf n)
11067       ;; ... fetch that article.
11068       (gnus-summary-refer-article
11069        (prog1 (match-string 0 ref)
11070          (setq ref (substring ref (match-end 0))))))
11071     (gnus-summary-goto-subject current)
11072     (gnus-summary-position-point)
11073     n))
11074
11075 (defun gnus-summary-refer-article (message-id)
11076   "Fetch an article specified by MESSAGE-ID."
11077   (interactive "sMessage-ID: ")
11078   (when (and (stringp message-id)
11079              (not (zerop (length message-id))))
11080     ;; Construct the correct Message-ID if necessary.
11081     ;; Suggested by tale@pawl.rpi.edu.
11082     (unless (string-match "^<" message-id)
11083       (setq message-id (concat "<" message-id)))
11084     (unless (string-match ">$" message-id)
11085       (setq message-id (concat message-id ">")))
11086     (let* ((header (gnus-id-to-header message-id))
11087            (sparse (and header
11088                         (memq (mail-header-number header)
11089                               gnus-newsgroup-sparse))))
11090       (if header
11091           (prog1
11092               ;; The article is present in the buffer, to we just go to it.
11093               (gnus-summary-goto-article 
11094                (mail-header-number header) nil header)
11095             (when sparse
11096               (gnus-summary-update-article (mail-header-number header))))
11097         ;; We fetch the article
11098         (let ((gnus-override-method 
11099                (and (gnus-news-group-p gnus-newsgroup-name)
11100                     gnus-refer-article-method))
11101               number)
11102           ;; Start the special refer-article method, if necessary.
11103           (when (and gnus-refer-article-method
11104                      (gnus-news-group-p gnus-newsgroup-name))
11105             (gnus-check-server gnus-refer-article-method))
11106           ;; Fetch the header, and display the article.
11107           (if (setq number (gnus-summary-insert-subject message-id))
11108               (gnus-summary-select-article nil nil nil number)
11109             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11110
11111 (defun gnus-summary-enter-digest-group (&optional force)
11112   "Enter a digest group based on the current article."
11113   (interactive "P")
11114   (gnus-set-global-variables)
11115   (gnus-summary-select-article)
11116   (let ((name (format "%s-%d"
11117                       (gnus-group-prefixed-name
11118                        gnus-newsgroup-name (list 'nndoc ""))
11119                       gnus-current-article))
11120         (ogroup gnus-newsgroup-name)
11121         (case-fold-search t)
11122         (buf (current-buffer))
11123         dig)
11124     (save-excursion
11125       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11126       (insert-buffer-substring gnus-original-article-buffer)
11127       (narrow-to-region
11128        (goto-char (point-min))
11129        (or (search-forward "\n\n" nil t) (point)))
11130       (goto-char (point-min))
11131       (delete-matching-lines "^\\(Path\\):\\|^From ")
11132       (widen))
11133     (unwind-protect
11134         (if (gnus-group-read-ephemeral-group
11135              name `(nndoc ,name (nndoc-address
11136                                  ,(get-buffer dig))
11137                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11138             ;; Make all postings to this group go to the parent group.
11139             (nconc (gnus-info-params (gnus-get-info name))
11140                    (list (cons 'to-group ogroup)))
11141           ;; Couldn't select this doc group.
11142           (switch-to-buffer buf)
11143           (gnus-set-global-variables)
11144           (gnus-configure-windows 'summary)
11145           (gnus-message 3 "Article couldn't be entered?"))
11146       (kill-buffer dig))))
11147
11148 (defun gnus-summary-isearch-article (&optional regexp-p)
11149   "Do incremental search forward on the current article.
11150 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11151   (interactive "P")
11152   (gnus-set-global-variables)
11153   (gnus-summary-select-article)
11154   (gnus-configure-windows 'article)
11155   (gnus-eval-in-buffer-window
11156    gnus-article-buffer
11157    (goto-char (point-min))
11158    (isearch-forward regexp-p)))
11159
11160 (defun gnus-summary-search-article-forward (regexp &optional backward)
11161   "Search for an article containing REGEXP forward.
11162 If BACKWARD, search backward instead."
11163   (interactive
11164    (list (read-string
11165           (format "Search article %s (regexp%s): "
11166                   (if current-prefix-arg "backward" "forward")
11167                   (if gnus-last-search-regexp
11168                       (concat ", default " gnus-last-search-regexp)
11169                     "")))
11170          current-prefix-arg))
11171   (gnus-set-global-variables)
11172   (if (string-equal regexp "")
11173       (setq regexp (or gnus-last-search-regexp ""))
11174     (setq gnus-last-search-regexp regexp))
11175   (unless (gnus-summary-search-article regexp backward)
11176     (error "Search failed: \"%s\"" regexp)))
11177
11178 (defun gnus-summary-search-article-backward (regexp)
11179   "Search for an article containing REGEXP backward."
11180   (interactive
11181    (list (read-string
11182           (format "Search article backward (regexp%s): "
11183                   (if gnus-last-search-regexp
11184                       (concat ", default " gnus-last-search-regexp)
11185                     "")))))
11186   (gnus-summary-search-article-forward regexp 'backward))
11187
11188 (defun gnus-summary-search-article (regexp &optional backward)
11189   "Search for an article containing REGEXP.
11190 Optional argument BACKWARD means do search for backward.
11191 `gnus-select-article-hook' is not called during the search."
11192   (let ((gnus-select-article-hook nil)  ;Disable hook.
11193         (gnus-article-display-hook nil)
11194         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11195         (re-search
11196          (if backward
11197              're-search-backward 're-search-forward))
11198         (sum (current-buffer))
11199         (found nil))
11200     (gnus-save-hidden-threads
11201       (gnus-summary-select-article)
11202       (set-buffer gnus-article-buffer)
11203       (while (not found)
11204         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11205         (if (if backward
11206                 (re-search-backward regexp nil t)
11207               (re-search-forward regexp nil t))
11208             ;; We found the regexp.
11209             (progn
11210               (setq found 'found)
11211               (beginning-of-line)
11212               (set-window-start
11213                (get-buffer-window (current-buffer))
11214                (point))
11215               (forward-line 1)
11216               (set-buffer sum))
11217           ;; We didn't find it, so we go to the next article.
11218           (set-buffer sum)
11219           (if (not (if backward (gnus-summary-find-prev)
11220                      (gnus-summary-find-next)))
11221               ;; No more articles.
11222               (setq found t)
11223             ;; Select the next article and adjust point.
11224             (gnus-summary-select-article)
11225             (set-buffer gnus-article-buffer)
11226             (widen)
11227             (goto-char (if backward (point-max) (point-min))))))
11228       (gnus-message 7 ""))
11229     ;; Return whether we found the regexp.
11230     (when (eq found 'found)
11231       (gnus-summary-show-thread)
11232       (gnus-summary-goto-subject gnus-current-article)
11233       (gnus-summary-position-point)
11234       t)))
11235
11236 (defun gnus-summary-find-matching (header regexp &optional backward unread
11237                                           not-case-fold)
11238   "Return a list of all articles that match REGEXP on HEADER.
11239 The search stars on the current article and goes forwards unless
11240 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11241 If UNREAD is non-nil, only unread articles will
11242 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11243 in the comparisons."
11244   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11245                 (gnus-data-find-list
11246                  (gnus-summary-article-number) (gnus-data-list backward))))
11247         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11248         (case-fold-search (not not-case-fold))
11249         articles d)
11250     (or (fboundp (intern (concat "mail-header-" header)))
11251         (error "%s is not a valid header" header))
11252     (while data
11253       (setq d (car data))
11254       (and (or (not unread)             ; We want all articles...
11255                (gnus-data-unread-p d))  ; Or just unreads.
11256            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11257            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11258            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11259       (setq data (cdr data)))
11260     (nreverse articles)))
11261
11262 (defun gnus-summary-execute-command (header regexp command &optional backward)
11263   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11264 If HEADER is an empty string (or nil), the match is done on the entire
11265 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11266   (interactive
11267    (list (let ((completion-ignore-case t))
11268            (completing-read
11269             "Header name: "
11270             (mapcar (lambda (string) (list string))
11271                     '("Number" "Subject" "From" "Lines" "Date"
11272                       "Message-ID" "Xref" "References" "Body"))
11273             nil 'require-match))
11274          (read-string "Regexp: ")
11275          (read-key-sequence "Command: ")
11276          current-prefix-arg))
11277   (when (equal header "Body")
11278     (setq header ""))
11279   (gnus-set-global-variables)
11280   ;; Hidden thread subtrees must be searched as well.
11281   (gnus-summary-show-all-threads)
11282   ;; We don't want to change current point nor window configuration.
11283   (save-excursion
11284     (save-window-excursion
11285       (gnus-message 6 "Executing %s..." (key-description command))
11286       ;; We'd like to execute COMMAND interactively so as to give arguments.
11287       (gnus-execute header regexp
11288                     `(lambda () (call-interactively ',(key-binding command)))
11289                     backward)
11290       (gnus-message 6 "Executing %s...done" (key-description command)))))
11291
11292 (defun gnus-summary-beginning-of-article ()
11293   "Scroll the article back to the beginning."
11294   (interactive)
11295   (gnus-set-global-variables)
11296   (gnus-summary-select-article)
11297   (gnus-configure-windows 'article)
11298   (gnus-eval-in-buffer-window
11299    gnus-article-buffer
11300    (widen)
11301    (goto-char (point-min))
11302    (and gnus-break-pages (gnus-narrow-to-page))))
11303
11304 (defun gnus-summary-end-of-article ()
11305   "Scroll to the end of the article."
11306   (interactive)
11307   (gnus-set-global-variables)
11308   (gnus-summary-select-article)
11309   (gnus-configure-windows 'article)
11310   (gnus-eval-in-buffer-window
11311    gnus-article-buffer
11312    (widen)
11313    (goto-char (point-max))
11314    (recenter -3)
11315    (and gnus-break-pages (gnus-narrow-to-page))))
11316
11317 (defun gnus-summary-show-article (&optional arg)
11318   "Force re-fetching of the current article.
11319 If ARG (the prefix) is non-nil, show the raw article without any
11320 article massaging functions being run."
11321   (interactive "P")
11322   (gnus-set-global-variables)
11323   (if (not arg)
11324       ;; Select the article the normal way.
11325       (gnus-summary-select-article nil 'force)
11326     ;; Bind the article treatment functions to nil.
11327     (let ((gnus-have-all-headers t)
11328           gnus-article-display-hook
11329           gnus-article-prepare-hook
11330           gnus-break-pages
11331           gnus-visual)
11332       (gnus-summary-select-article nil 'force)))
11333   (gnus-summary-goto-subject gnus-current-article)
11334 ;  (gnus-configure-windows 'article)
11335   (gnus-summary-position-point))
11336
11337 (defun gnus-summary-verbose-headers (&optional arg)
11338   "Toggle permanent full header display.
11339 If ARG is a positive number, turn header display on.
11340 If ARG is a negative number, turn header display off."
11341   (interactive "P")
11342   (gnus-set-global-variables)
11343   (gnus-summary-toggle-header arg)
11344   (setq gnus-show-all-headers
11345         (cond ((or (not (numberp arg))
11346                    (zerop arg))
11347                (not gnus-show-all-headers))
11348               ((natnump arg)
11349                t))))
11350
11351 (defun gnus-summary-toggle-header (&optional arg)
11352   "Show the headers if they are hidden, or hide them if they are shown.
11353 If ARG is a positive number, show the entire header.
11354 If ARG is a negative number, hide the unwanted header lines."
11355   (interactive "P")
11356   (gnus-set-global-variables)
11357   (save-excursion
11358     (set-buffer gnus-article-buffer)
11359     (let* ((buffer-read-only nil)
11360            (inhibit-point-motion-hooks t)
11361            (hidden (text-property-any
11362                     (goto-char (point-min)) (search-forward "\n\n")
11363                     'invisible t))
11364            e)
11365       (goto-char (point-min))
11366       (when (search-forward "\n\n" nil t)
11367         (delete-region (point-min) (1- (point))))
11368       (goto-char (point-min))
11369       (save-excursion
11370         (set-buffer gnus-original-article-buffer)
11371         (goto-char (point-min))
11372         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11373       (insert-buffer-substring gnus-original-article-buffer 1 e)
11374       (let ((gnus-inhibit-hiding t))
11375         (run-hooks 'gnus-article-display-hook))
11376       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11377           (gnus-article-hide-headers)))))
11378
11379 (defun gnus-summary-show-all-headers ()
11380   "Make all header lines visible."
11381   (interactive)
11382   (gnus-set-global-variables)
11383   (gnus-article-show-all-headers))
11384
11385 (defun gnus-summary-toggle-mime (&optional arg)
11386   "Toggle MIME processing.
11387 If ARG is a positive number, turn MIME processing on."
11388   (interactive "P")
11389   (gnus-set-global-variables)
11390   (setq gnus-show-mime
11391         (if (null arg) (not gnus-show-mime)
11392           (> (prefix-numeric-value arg) 0)))
11393   (gnus-summary-select-article t 'force))
11394
11395 (defun gnus-summary-caesar-message (&optional arg)
11396   "Caesar rotate the current article by 13.
11397 The numerical prefix specifies how manu places to rotate each letter
11398 forward."
11399   (interactive "P")
11400   (gnus-set-global-variables)
11401   (gnus-summary-select-article)
11402   (let ((mail-header-separator ""))
11403     (gnus-eval-in-buffer-window
11404      gnus-article-buffer
11405      (save-restriction
11406        (widen)
11407        (let ((start (window-start))
11408              buffer-read-only)
11409          (message-caesar-buffer-body arg)
11410          (set-window-start (get-buffer-window (current-buffer)) start))))))
11411
11412 (defun gnus-summary-stop-page-breaking ()
11413   "Stop page breaking in the current article."
11414   (interactive)
11415   (gnus-set-global-variables)
11416   (gnus-summary-select-article)
11417   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11418
11419 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11420   "Move the current article to a different newsgroup.
11421 If N is a positive number, move the N next articles.
11422 If N is a negative number, move the N previous articles.
11423 If N is nil and any articles have been marked with the process mark,
11424 move those articles instead.
11425 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11426 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11427 re-spool using this method.
11428
11429 For this function to work, both the current newsgroup and the
11430 newsgroup that you want to move to have to support the `request-move'
11431 and `request-accept' functions."
11432   (interactive "P")
11433   (unless action (setq action 'move))
11434   (gnus-set-global-variables)
11435   ;; Check whether the source group supports the required functions.
11436   (cond ((and (eq action 'move)
11437               (not (gnus-check-backend-function
11438                     'request-move-article gnus-newsgroup-name)))
11439          (error "The current group does not support article moving"))
11440         ((and (eq action 'crosspost)
11441               (not (gnus-check-backend-function
11442                     'request-replace-article gnus-newsgroup-name)))
11443          (error "The current group does not support article editing")))
11444   (let ((articles (gnus-summary-work-articles n))
11445         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11446         (names '((move "Move" "Moving")
11447                  (copy "Copy" "Copying")
11448                  (crosspost "Crosspost" "Crossposting")))
11449         (copy-buf (save-excursion
11450                     (nnheader-set-temp-buffer " *copy article*")))
11451         art-group to-method new-xref article to-groups)
11452     (unless (assq action names)
11453       (error "Unknown action %s" action))
11454     ;; Read the newsgroup name.
11455     (when (and (not to-newsgroup)
11456                (not select-method))
11457       (setq to-newsgroup
11458             (gnus-read-move-group-name
11459              (cadr (assq action names))
11460              (symbol-value (intern (format "gnus-current-%s-group" action)))
11461              articles prefix))
11462       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11463     (setq to-method (or select-method 
11464                         (gnus-find-method-for-group to-newsgroup)))
11465     ;; Check the method we are to move this article to...
11466     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11467         (error "%s does not support article copying" (car to-method)))
11468     (or (gnus-check-server to-method)
11469         (error "Can't open server %s" (car to-method)))
11470     (gnus-message 6 "%s to %s: %s..."
11471                   (caddr (assq action names))
11472                   (or (car select-method) to-newsgroup) articles)
11473     (while articles
11474       (setq article (pop articles))
11475       (setq
11476        art-group
11477        (cond
11478         ;; Move the article.
11479         ((eq action 'move)
11480          (gnus-request-move-article
11481           article                       ; Article to move
11482           gnus-newsgroup-name           ; From newsgrouo
11483           (nth 1 (gnus-find-method-for-group
11484                   gnus-newsgroup-name)) ; Server
11485           (list 'gnus-request-accept-article
11486                 to-newsgroup (list 'quote select-method)
11487                 (not articles))         ; Accept form
11488           (not articles)))              ; Only save nov last time
11489         ;; Copy the article.
11490         ((eq action 'copy)
11491          (save-excursion
11492            (set-buffer copy-buf)
11493            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11494            (gnus-request-accept-article
11495             to-newsgroup select-method (not articles))))
11496         ;; Crosspost the article.
11497         ((eq action 'crosspost)
11498          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11499            (setq new-xref (concat gnus-newsgroup-name ":" article))
11500            (if (and xref (not (string= xref "")))
11501                (progn
11502                  (when (string-match "^Xref: " xref)
11503                    (setq xref (substring xref (match-end 0))))
11504                  (setq new-xref (concat xref " " new-xref)))
11505              (setq new-xref (concat (system-name) " " new-xref)))
11506            (save-excursion
11507              (set-buffer copy-buf)
11508              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11509              (nnheader-replace-header "xref" new-xref)
11510              (gnus-request-accept-article
11511               to-newsgroup select-method (not articles)))))))
11512       (if (not art-group)
11513           (gnus-message 1 "Couldn't %s article %s"
11514                         (cadr (assq action names)) article)
11515         (let* ((entry
11516                 (or
11517                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11518                  (gnus-gethash
11519                   (gnus-group-prefixed-name
11520                    (car art-group)
11521                    (or select-method 
11522                        (gnus-find-method-for-group to-newsgroup)))
11523                   gnus-newsrc-hashtb)))
11524                (info (nth 2 entry))
11525                (to-group (gnus-info-group info)))
11526           ;; Update the group that has been moved to.
11527           (when (and info
11528                      (memq action '(move copy)))
11529             (unless (member to-group to-groups)
11530               (push to-group to-groups))
11531
11532             (unless (memq article gnus-newsgroup-unreads)
11533               (gnus-info-set-read
11534                info (gnus-add-to-range (gnus-info-read info)
11535                                        (list (cdr art-group)))))
11536
11537             ;; Copy any marks over to the new group.
11538             (let ((marks gnus-article-mark-lists)
11539                   (to-article (cdr art-group)))
11540
11541               ;; See whether the article is to be put in the cache.
11542               (when gnus-use-cache
11543                 (gnus-cache-possibly-enter-article
11544                  to-group to-article
11545                  (let ((header (copy-sequence
11546                                 (gnus-summary-article-header article))))
11547                    (mail-header-set-number header to-article)
11548                    header)
11549                  (memq article gnus-newsgroup-marked)
11550                  (memq article gnus-newsgroup-dormant)
11551                  (memq article gnus-newsgroup-unreads)))
11552
11553               (while marks
11554                 (when (memq article (symbol-value
11555                                      (intern (format "gnus-newsgroup-%s"
11556                                                      (caar marks)))))
11557                   ;; If the other group is the same as this group,
11558                   ;; then we have to add the mark to the list.
11559                   (when (equal to-group gnus-newsgroup-name)
11560                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11561                          (cons to-article
11562                                (symbol-value
11563                                 (intern (format "gnus-newsgroup-%s"
11564                                                 (caar marks)))))))
11565                   ;; Copy mark to other group.
11566                   (gnus-add-marked-articles
11567                    to-group (cdar marks) (list to-article) info))
11568                 (setq marks (cdr marks)))))
11569
11570           ;; Update the Xref header in this article to point to
11571           ;; the new crossposted article we have just created.
11572           (when (eq action 'crosspost)
11573             (save-excursion
11574               (set-buffer copy-buf)
11575               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11576               (nnheader-replace-header
11577                "xref" (concat new-xref " " (gnus-group-prefixed-name
11578                                             (car art-group) to-method)
11579                               ":" (cdr art-group)))
11580               (gnus-request-replace-article
11581                article gnus-newsgroup-name (current-buffer)))))
11582
11583         (gnus-summary-goto-subject article)
11584         (when (eq action 'move)
11585           (gnus-summary-mark-article article gnus-canceled-mark)))
11586       (gnus-summary-remove-process-mark article))
11587     ;; Re-activate all groups that have been moved to.
11588     (while to-groups
11589       (gnus-activate-group (pop to-groups)))
11590     
11591     (gnus-kill-buffer copy-buf)
11592     (gnus-summary-position-point)
11593     (gnus-set-mode-line 'summary)))
11594
11595 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11596   "Move the current article to a different newsgroup.
11597 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11598 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11599 re-spool using this method."
11600   (interactive "P")
11601   (gnus-summary-move-article n nil select-method 'copy))
11602
11603 (defun gnus-summary-crosspost-article (&optional n)
11604   "Crosspost the current article to some other group."
11605   (interactive "P")
11606   (gnus-summary-move-article n nil nil 'crosspost))
11607
11608 (defvar gnus-summary-respool-default-method nil
11609   "Default method for respooling an article.  
11610 If nil, use to the current newsgroup method.")
11611
11612 (defun gnus-summary-respool-article (&optional n method)
11613   "Respool the current article.
11614 The article will be squeezed through the mail spooling process again,
11615 which means that it will be put in some mail newsgroup or other
11616 depending on `nnmail-split-methods'.
11617 If N is a positive number, respool the N next articles.
11618 If N is a negative number, respool the N previous articles.
11619 If N is nil and any articles have been marked with the process mark,
11620 respool those articles instead.
11621
11622 Respooling can be done both from mail groups and \"real\" newsgroups.
11623 In the former case, the articles in question will be moved from the
11624 current group into whatever groups they are destined to.  In the
11625 latter case, they will be copied into the relevant groups."
11626   (interactive 
11627    (list current-prefix-arg
11628          (let* ((methods (gnus-methods-using 'respool))
11629                 (methname
11630                  (symbol-name (or gnus-summary-respool-default-method
11631                                   (car (gnus-find-method-for-group
11632                                         gnus-newsgroup-name)))))
11633                 (method
11634                  (gnus-completing-read 
11635                   methname "What backend do you want to use when? "
11636                   methods nil t nil 'gnus-method-history))
11637                 ms)
11638            (cond
11639             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11640              (list (intern method) ""))
11641             ((= 1 (length ms))
11642              (car ms))
11643             (t
11644              (cdr (completing-read 
11645                    "Server name: "
11646                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11647   (gnus-set-global-variables)
11648   (unless method
11649     (error "No method given for respooling"))
11650   (if (assoc (symbol-name
11651               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11652              (gnus-methods-using 'respool))
11653       (gnus-summary-move-article n nil method)
11654     (gnus-summary-copy-article n nil method)))
11655
11656 (defun gnus-summary-import-article (file)
11657   "Import a random file into a mail newsgroup."
11658   (interactive "fImport file: ")
11659   (gnus-set-global-variables)
11660   (let ((group gnus-newsgroup-name)
11661         (now (current-time))
11662         atts lines)
11663     (or (gnus-check-backend-function 'request-accept-article group)
11664         (error "%s does not support article importing" group))
11665     (or (file-readable-p file)
11666         (not (file-regular-p file))
11667         (error "Can't read %s" file))
11668     (save-excursion
11669       (set-buffer (get-buffer-create " *import file*"))
11670       (buffer-disable-undo (current-buffer))
11671       (erase-buffer)
11672       (insert-file-contents file)
11673       (goto-char (point-min))
11674       (unless (nnheader-article-p)
11675         ;; This doesn't look like an article, so we fudge some headers.
11676         (setq atts (file-attributes file)
11677               lines (count-lines (point-min) (point-max)))
11678         (insert "From: " (read-string "From: ") "\n"
11679                 "Subject: " (read-string "Subject: ") "\n"
11680                 "Date: " (timezone-make-date-arpa-standard
11681                           (current-time-string (nth 5 atts))
11682                           (current-time-zone now)
11683                           (current-time-zone now)) "\n"
11684                 "Message-ID: " (gnus-inews-message-id) "\n"
11685                 "Lines: " (int-to-string lines) "\n"
11686                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11687       (gnus-request-accept-article group nil t)
11688       (kill-buffer (current-buffer)))))
11689
11690 (defun gnus-summary-expire-articles (&optional now)
11691   "Expire all articles that are marked as expirable in the current group."
11692   (interactive)
11693   (gnus-set-global-variables)
11694   (when (gnus-check-backend-function
11695          'request-expire-articles gnus-newsgroup-name)
11696     ;; This backend supports expiry.
11697     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11698            (expirable (if total
11699                           (gnus-list-of-read-articles gnus-newsgroup-name)
11700                         (setq gnus-newsgroup-expirable
11701                               (sort gnus-newsgroup-expirable '<))))
11702            (expiry-wait (if now 'immediate
11703                           (gnus-group-get-parameter
11704                            gnus-newsgroup-name 'expiry-wait)))
11705            es)
11706       (when expirable
11707         ;; There are expirable articles in this group, so we run them
11708         ;; through the expiry process.
11709         (gnus-message 6 "Expiring articles...")
11710         ;; The list of articles that weren't expired is returned.
11711         (if expiry-wait
11712             (let ((nnmail-expiry-wait-function nil)
11713                   (nnmail-expiry-wait expiry-wait))
11714               (setq es (gnus-request-expire-articles
11715                         expirable gnus-newsgroup-name)))
11716           (setq es (gnus-request-expire-articles
11717                     expirable gnus-newsgroup-name)))
11718         (or total (setq gnus-newsgroup-expirable es))
11719         ;; We go through the old list of expirable, and mark all
11720         ;; really expired articles as nonexistent.
11721         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11722           (let ((gnus-use-cache nil))
11723             (while expirable
11724               (unless (memq (car expirable) es)
11725                 (when (gnus-data-find (car expirable))
11726                   (gnus-summary-mark-article
11727                    (car expirable) gnus-canceled-mark)))
11728               (setq expirable (cdr expirable)))))
11729         (gnus-message 6 "Expiring articles...done")))))
11730
11731 (defun gnus-summary-expire-articles-now ()
11732   "Expunge all expirable articles in the current group.
11733 This means that *all* articles that are marked as expirable will be
11734 deleted forever, right now."
11735   (interactive)
11736   (gnus-set-global-variables)
11737   (or gnus-expert-user
11738       (gnus-y-or-n-p
11739        "Are you really, really, really sure you want to delete all these messages? ")
11740       (error "Phew!"))
11741   (gnus-summary-expire-articles t))
11742
11743 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11744 (defun gnus-summary-delete-article (&optional n)
11745   "Delete the N next (mail) articles.
11746 This command actually deletes articles.  This is not a marking
11747 command.  The article will disappear forever from your life, never to
11748 return.
11749 If N is negative, delete backwards.
11750 If N is nil and articles have been marked with the process mark,
11751 delete these instead."
11752   (interactive "P")
11753   (gnus-set-global-variables)
11754   (or (gnus-check-backend-function 'request-expire-articles
11755                                    gnus-newsgroup-name)
11756       (error "The current newsgroup does not support article deletion."))
11757   ;; Compute the list of articles to delete.
11758   (let ((articles (gnus-summary-work-articles n))
11759         not-deleted)
11760     (if (and gnus-novice-user
11761              (not (gnus-y-or-n-p
11762                    (format "Do you really want to delete %s forever? "
11763                            (if (> (length articles) 1) 
11764                                (format "these %s articles" (length articles))
11765                              "this article")))))
11766         ()
11767       ;; Delete the articles.
11768       (setq not-deleted (gnus-request-expire-articles
11769                          articles gnus-newsgroup-name 'force))
11770       (while articles
11771         (gnus-summary-remove-process-mark (car articles))
11772         ;; The backend might not have been able to delete the article
11773         ;; after all.
11774         (or (memq (car articles) not-deleted)
11775             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11776         (setq articles (cdr articles))))
11777     (gnus-summary-position-point)
11778     (gnus-set-mode-line 'summary)
11779     not-deleted))
11780
11781 (defun gnus-summary-edit-article (&optional force)
11782   "Enter into a buffer and edit the current article.
11783 This will have permanent effect only in mail groups.
11784 If FORCE is non-nil, allow editing of articles even in read-only
11785 groups."
11786   (interactive "P")
11787   (save-excursion
11788     (set-buffer gnus-summary-buffer)
11789     (gnus-set-global-variables)
11790     (when (and (not force)
11791                (gnus-group-read-only-p))
11792       (error "The current newsgroup does not support article editing."))
11793     (gnus-summary-select-article t nil t)
11794     (gnus-configure-windows 'article)
11795     (select-window (get-buffer-window gnus-article-buffer))
11796     (gnus-message 6 "C-c C-c to end edits")
11797     (setq buffer-read-only nil)
11798     (text-mode)
11799     (use-local-map (copy-keymap (current-local-map)))
11800     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11801     (buffer-enable-undo)
11802     (widen)
11803     (goto-char (point-min))
11804     (search-forward "\n\n" nil t)))
11805
11806 (defun gnus-summary-edit-article-done ()
11807   "Make edits to the current article permanent."
11808   (interactive)
11809   (if (gnus-group-read-only-p)
11810       (progn
11811         (gnus-summary-edit-article-postpone)
11812         (gnus-error
11813          1 "The current newsgroup does not support article editing."))
11814     (let ((buf (format "%s" (buffer-string))))
11815       (erase-buffer)
11816       (insert buf)
11817       (if (not (gnus-request-replace-article
11818                 (cdr gnus-article-current) (car gnus-article-current)
11819                 (current-buffer)))
11820           (error "Couldn't replace article.")
11821         (gnus-article-mode)
11822         (use-local-map gnus-article-mode-map)
11823         (setq buffer-read-only t)
11824         (buffer-disable-undo (current-buffer))
11825         (gnus-configure-windows 'summary)
11826         (gnus-summary-update-article (cdr gnus-article-current))
11827         (when gnus-use-cache
11828           (gnus-cache-update-article 
11829            (cdr gnus-article-current) (car gnus-article-current)))
11830         (when gnus-keep-backlog
11831           (gnus-backlog-remove-article 
11832            (car gnus-article-current) (cdr gnus-article-current))))
11833       (save-excursion
11834         (when (get-buffer gnus-original-article-buffer)
11835           (set-buffer gnus-original-article-buffer)
11836           (setq gnus-original-article nil)))
11837       (setq gnus-article-current nil
11838             gnus-current-article nil)
11839       (run-hooks 'gnus-article-display-hook)
11840       (and (gnus-visual-p 'summary-highlight 'highlight)
11841            (run-hooks 'gnus-visual-mark-article-hook)))))
11842
11843 (defun gnus-summary-edit-article-postpone ()
11844   "Postpone changes to the current article."
11845   (interactive)
11846   (gnus-article-mode)
11847   (use-local-map gnus-article-mode-map)
11848   (setq buffer-read-only t)
11849   (buffer-disable-undo (current-buffer))
11850   (gnus-configure-windows 'summary)
11851   (and (gnus-visual-p 'summary-highlight 'highlight)
11852        (run-hooks 'gnus-visual-mark-article-hook)))
11853
11854 (defun gnus-summary-respool-query ()
11855   "Query where the respool algorithm would put this article."
11856   (interactive)
11857   (gnus-set-global-variables)
11858   (gnus-summary-select-article)
11859   (save-excursion
11860     (set-buffer gnus-article-buffer)
11861     (save-restriction
11862       (goto-char (point-min))
11863       (search-forward "\n\n")
11864       (narrow-to-region (point-min) (point))
11865       (pp-eval-expression
11866        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11867
11868 ;; Summary marking commands.
11869
11870 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11871   "Mark articles which has the same subject as read, and then select the next.
11872 If UNMARK is positive, remove any kind of mark.
11873 If UNMARK is negative, tick articles."
11874   (interactive "P")
11875   (gnus-set-global-variables)
11876   (if unmark
11877       (setq unmark (prefix-numeric-value unmark)))
11878   (let ((count
11879          (gnus-summary-mark-same-subject
11880           (gnus-summary-article-subject) unmark)))
11881     ;; Select next unread article.  If auto-select-same mode, should
11882     ;; select the first unread article.
11883     (gnus-summary-next-article t (and gnus-auto-select-same
11884                                       (gnus-summary-article-subject)))
11885     (gnus-message 7 "%d article%s marked as %s"
11886                   count (if (= count 1) " is" "s are")
11887                   (if unmark "unread" "read"))))
11888
11889 (defun gnus-summary-kill-same-subject (&optional unmark)
11890   "Mark articles which has the same subject as read.
11891 If UNMARK is positive, remove any kind of mark.
11892 If UNMARK is negative, tick articles."
11893   (interactive "P")
11894   (gnus-set-global-variables)
11895   (if unmark
11896       (setq unmark (prefix-numeric-value unmark)))
11897   (let ((count
11898          (gnus-summary-mark-same-subject
11899           (gnus-summary-article-subject) unmark)))
11900     ;; If marked as read, go to next unread subject.
11901     (if (null unmark)
11902         ;; Go to next unread subject.
11903         (gnus-summary-next-subject 1 t))
11904     (gnus-message 7 "%d articles are marked as %s"
11905                   count (if unmark "unread" "read"))))
11906
11907 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11908   "Mark articles with same SUBJECT as read, and return marked number.
11909 If optional argument UNMARK is positive, remove any kinds of marks.
11910 If optional argument UNMARK is negative, mark articles as unread instead."
11911   (let ((count 1))
11912     (save-excursion
11913       (cond
11914        ((null unmark)                   ; Mark as read.
11915         (while (and
11916                 (progn
11917                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11918                   (gnus-summary-show-thread) t)
11919                 (gnus-summary-find-subject subject))
11920           (setq count (1+ count))))
11921        ((> unmark 0)                    ; Tick.
11922         (while (and
11923                 (progn
11924                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11925                   (gnus-summary-show-thread) t)
11926                 (gnus-summary-find-subject subject))
11927           (setq count (1+ count))))
11928        (t                               ; Mark as unread.
11929         (while (and
11930                 (progn
11931                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11932                   (gnus-summary-show-thread) t)
11933                 (gnus-summary-find-subject subject))
11934           (setq count (1+ count)))))
11935       (gnus-set-mode-line 'summary)
11936       ;; Return the number of marked articles.
11937       count)))
11938
11939 (defun gnus-summary-mark-as-processable (n &optional unmark)
11940   "Set the process mark on the next N articles.
11941 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11942 the process mark instead.  The difference between N and the actual
11943 number of articles marked is returned."
11944   (interactive "p")
11945   (gnus-set-global-variables)
11946   (let ((backward (< n 0))
11947         (n (abs n)))
11948     (while (and
11949             (> n 0)
11950             (if unmark
11951                 (gnus-summary-remove-process-mark
11952                  (gnus-summary-article-number))
11953               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11954             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11955       (setq n (1- n)))
11956     (if (/= 0 n) (gnus-message 7 "No more articles"))
11957     (gnus-summary-recenter)
11958     (gnus-summary-position-point)
11959     n))
11960
11961 (defun gnus-summary-unmark-as-processable (n)
11962   "Remove the process mark from the next N articles.
11963 If N is negative, mark backward instead.  The difference between N and
11964 the actual number of articles marked is returned."
11965   (interactive "p")
11966   (gnus-set-global-variables)
11967   (gnus-summary-mark-as-processable n t))
11968
11969 (defun gnus-summary-unmark-all-processable ()
11970   "Remove the process mark from all articles."
11971   (interactive)
11972   (gnus-set-global-variables)
11973   (save-excursion
11974     (while gnus-newsgroup-processable
11975       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11976   (gnus-summary-position-point))
11977
11978 (defun gnus-summary-mark-as-expirable (n)
11979   "Mark N articles forward as expirable.
11980 If N is negative, mark backward instead.  The difference between N and
11981 the actual number of articles marked is returned."
11982   (interactive "p")
11983   (gnus-set-global-variables)
11984   (gnus-summary-mark-forward n gnus-expirable-mark))
11985
11986 (defun gnus-summary-mark-article-as-replied (article)
11987   "Mark ARTICLE replied and update the summary line."
11988   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11989   (let ((buffer-read-only nil))
11990     (when (gnus-summary-goto-subject article)
11991       (gnus-summary-update-secondary-mark article))))
11992
11993 (defun gnus-summary-set-bookmark (article)
11994   "Set a bookmark in current article."
11995   (interactive (list (gnus-summary-article-number)))
11996   (gnus-set-global-variables)
11997   (if (or (not (get-buffer gnus-article-buffer))
11998           (not gnus-current-article)
11999           (not gnus-article-current)
12000           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12001       (error "No current article selected"))
12002   ;; Remove old bookmark, if one exists.
12003   (let ((old (assq article gnus-newsgroup-bookmarks)))
12004     (if old (setq gnus-newsgroup-bookmarks
12005                   (delq old gnus-newsgroup-bookmarks))))
12006   ;; Set the new bookmark, which is on the form
12007   ;; (article-number . line-number-in-body).
12008   (setq gnus-newsgroup-bookmarks
12009         (cons
12010          (cons article
12011                (save-excursion
12012                  (set-buffer gnus-article-buffer)
12013                  (count-lines
12014                   (min (point)
12015                        (save-excursion
12016                          (goto-char (point-min))
12017                          (search-forward "\n\n" nil t)
12018                          (point)))
12019                   (point))))
12020          gnus-newsgroup-bookmarks))
12021   (gnus-message 6 "A bookmark has been added to the current article."))
12022
12023 (defun gnus-summary-remove-bookmark (article)
12024   "Remove the bookmark from the current article."
12025   (interactive (list (gnus-summary-article-number)))
12026   (gnus-set-global-variables)
12027   ;; Remove old bookmark, if one exists.
12028   (let ((old (assq article gnus-newsgroup-bookmarks)))
12029     (if old
12030         (progn
12031           (setq gnus-newsgroup-bookmarks
12032                 (delq old gnus-newsgroup-bookmarks))
12033           (gnus-message 6 "Removed bookmark."))
12034       (gnus-message 6 "No bookmark in current article."))))
12035
12036 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12037 (defun gnus-summary-mark-as-dormant (n)
12038   "Mark N articles forward as dormant.
12039 If N is negative, mark backward instead.  The difference between N and
12040 the actual number of articles marked is returned."
12041   (interactive "p")
12042   (gnus-set-global-variables)
12043   (gnus-summary-mark-forward n gnus-dormant-mark))
12044
12045 (defun gnus-summary-set-process-mark (article)
12046   "Set the process mark on ARTICLE and update the summary line."
12047   (setq gnus-newsgroup-processable
12048         (cons article
12049               (delq article gnus-newsgroup-processable)))
12050   (when (gnus-summary-goto-subject article)
12051     (gnus-summary-show-thread)
12052     (gnus-summary-update-secondary-mark article)))
12053
12054 (defun gnus-summary-remove-process-mark (article)
12055   "Remove the process mark from ARTICLE and update the summary line."
12056   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12057   (when (gnus-summary-goto-subject article)
12058     (gnus-summary-show-thread)
12059     (gnus-summary-update-secondary-mark article)))
12060
12061 (defun gnus-summary-set-saved-mark (article)
12062   "Set the process mark on ARTICLE and update the summary line."
12063   (push article gnus-newsgroup-saved)
12064   (when (gnus-summary-goto-subject article)
12065     (gnus-summary-update-secondary-mark article)))
12066
12067 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12068   "Mark N articles as read forwards.
12069 If N is negative, mark backwards instead.
12070 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
12071 marked as unread.
12072 The difference between N and the actual number of articles marked is
12073 returned."
12074   (interactive "p")
12075   (gnus-set-global-variables)
12076   (let ((backward (< n 0))
12077         (gnus-summary-goto-unread
12078          (and gnus-summary-goto-unread
12079               (not (eq gnus-summary-goto-unread 'never))
12080               (not (memq mark (list gnus-unread-mark
12081                                     gnus-ticked-mark gnus-dormant-mark)))))
12082         (n (abs n))
12083         (mark (or mark gnus-del-mark)))
12084     (while (and (> n 0)
12085                 (gnus-summary-mark-article nil mark no-expire)
12086                 (zerop (gnus-summary-next-subject
12087                         (if backward -1 1)
12088                         (and gnus-summary-goto-unread
12089                              (not (eq gnus-summary-goto-unread 'never)))
12090                         t)))
12091       (setq n (1- n)))
12092     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12093     (gnus-summary-recenter)
12094     (gnus-summary-position-point)
12095     (gnus-set-mode-line 'summary)
12096     n))
12097
12098 (defun gnus-summary-mark-article-as-read (mark)
12099   "Mark the current article quickly as read with MARK."
12100   (let ((article (gnus-summary-article-number)))
12101     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12102     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12103     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12104     (setq gnus-newsgroup-reads
12105           (cons (cons article mark) gnus-newsgroup-reads))
12106     ;; Possibly remove from cache, if that is used.
12107     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12108     ;; Allow the backend to change the mark.
12109     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12110     ;; Check for auto-expiry.
12111     (when (and gnus-newsgroup-auto-expire
12112                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12113                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12114                    (= mark gnus-ancient-mark)
12115                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12116       (setq mark gnus-expirable-mark)
12117       (push article gnus-newsgroup-expirable))
12118     ;; Set the mark in the buffer.
12119     (gnus-summary-update-mark mark 'unread)
12120     t))
12121
12122 (defun gnus-summary-mark-article-as-unread (mark)
12123   "Mark the current article quickly as unread with MARK."
12124   (let ((article (gnus-summary-article-number)))
12125     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12126     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12127     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12128     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12129     (cond ((= mark gnus-ticked-mark)
12130            (push article gnus-newsgroup-marked))
12131           ((= mark gnus-dormant-mark)
12132            (push article gnus-newsgroup-dormant))
12133           (t
12134            (push article gnus-newsgroup-unreads)))
12135     (setq gnus-newsgroup-reads
12136           (delq (assq article gnus-newsgroup-reads)
12137                 gnus-newsgroup-reads))
12138
12139     ;; See whether the article is to be put in the cache.
12140     (and gnus-use-cache
12141          (vectorp (gnus-summary-article-header article))
12142          (save-excursion
12143            (gnus-cache-possibly-enter-article
12144             gnus-newsgroup-name article
12145             (gnus-summary-article-header article)
12146             (= mark gnus-ticked-mark)
12147             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12148
12149     ;; Fix the mark.
12150     (gnus-summary-update-mark mark 'unread)
12151     t))
12152
12153 (defun gnus-summary-mark-article (&optional article mark no-expire)
12154   "Mark ARTICLE with MARK.  MARK can be any character.
12155 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12156 `??' (dormant) and `?E' (expirable).
12157 If MARK is nil, then the default character `?D' is used.
12158 If ARTICLE is nil, then the article on the current line will be
12159 marked."
12160   ;; The mark might be a string.
12161   (and (stringp mark)
12162        (setq mark (aref mark 0)))
12163   ;; If no mark is given, then we check auto-expiring.
12164   (and (not no-expire)
12165        gnus-newsgroup-auto-expire
12166        (or (not mark)
12167            (and (numberp mark)
12168                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12169                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12170                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12171        (setq mark gnus-expirable-mark))
12172   (let* ((mark (or mark gnus-del-mark))
12173          (article (or article (gnus-summary-article-number))))
12174     (or article (error "No article on current line"))
12175     (if (or (= mark gnus-unread-mark)
12176             (= mark gnus-ticked-mark)
12177             (= mark gnus-dormant-mark))
12178         (gnus-mark-article-as-unread article mark)
12179       (gnus-mark-article-as-read article mark))
12180
12181     ;; See whether the article is to be put in the cache.
12182     (and gnus-use-cache
12183          (not (= mark gnus-canceled-mark))
12184          (vectorp (gnus-summary-article-header article))
12185          (save-excursion
12186            (gnus-cache-possibly-enter-article
12187             gnus-newsgroup-name article
12188             (gnus-summary-article-header article)
12189             (= mark gnus-ticked-mark)
12190             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12191
12192     (if (gnus-summary-goto-subject article nil t)
12193         (let ((buffer-read-only nil))
12194           (gnus-summary-show-thread)
12195           ;; Fix the mark.
12196           (gnus-summary-update-mark mark 'unread)
12197           t))))
12198
12199 (defun gnus-summary-update-secondary-mark (article)
12200   "Update the secondary (read, process, cache) mark."
12201   (gnus-summary-update-mark
12202    (cond ((memq article gnus-newsgroup-processable)
12203           gnus-process-mark)
12204          ((memq article gnus-newsgroup-cached)
12205           gnus-cached-mark)
12206          ((memq article gnus-newsgroup-replied)
12207           gnus-replied-mark)
12208          ((memq article gnus-newsgroup-saved)
12209           gnus-saved-mark)
12210          (t gnus-unread-mark))
12211    'replied)
12212   (when (gnus-visual-p 'summary-highlight 'highlight)
12213     (run-hooks 'gnus-summary-update-hook))
12214   t)
12215
12216 (defun gnus-summary-update-mark (mark type)
12217   (beginning-of-line)
12218   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12219         (buffer-read-only nil))
12220     (when (and forward
12221                (<= (+ forward (point)) (point-max)))
12222       ;; Go to the right position on the line.
12223       (goto-char (+ forward (point)))
12224       ;; Replace the old mark with the new mark.
12225       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12226       ;; Optionally update the marks by some user rule.
12227       (when (eq type 'unread)
12228         (gnus-data-set-mark
12229          (gnus-data-find (gnus-summary-article-number)) mark)
12230         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12231
12232 (defun gnus-mark-article-as-read (article &optional mark)
12233   "Enter ARTICLE in the pertinent lists and remove it from others."
12234   ;; Make the article expirable.
12235   (let ((mark (or mark gnus-del-mark)))
12236     (if (= mark gnus-expirable-mark)
12237         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12238       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12239     ;; Remove from unread and marked lists.
12240     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12241     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12242     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12243     (push (cons article mark) gnus-newsgroup-reads)
12244     ;; Possibly remove from cache, if that is used.
12245     (when gnus-use-cache
12246       (gnus-cache-enter-remove-article article))))
12247
12248 (defun gnus-mark-article-as-unread (article &optional mark)
12249   "Enter ARTICLE in the pertinent lists and remove it from others."
12250   (let ((mark (or mark gnus-ticked-mark)))
12251     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12252     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12253     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12254     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12255     (cond ((= mark gnus-ticked-mark)
12256            (push article gnus-newsgroup-marked))
12257           ((= mark gnus-dormant-mark)
12258            (push article gnus-newsgroup-dormant))
12259           (t
12260            (push article gnus-newsgroup-unreads)))
12261     (setq gnus-newsgroup-reads
12262           (delq (assq article gnus-newsgroup-reads)
12263                 gnus-newsgroup-reads))))
12264
12265 (defalias 'gnus-summary-mark-as-unread-forward
12266   'gnus-summary-tick-article-forward)
12267 (make-obsolete 'gnus-summary-mark-as-unread-forward
12268                'gnus-summary-tick-article-forward)
12269 (defun gnus-summary-tick-article-forward (n)
12270   "Tick N articles forwards.
12271 If N is negative, tick backwards instead.
12272 The difference between N and the number of articles ticked is returned."
12273   (interactive "p")
12274   (gnus-summary-mark-forward n gnus-ticked-mark))
12275
12276 (defalias 'gnus-summary-mark-as-unread-backward
12277   'gnus-summary-tick-article-backward)
12278 (make-obsolete 'gnus-summary-mark-as-unread-backward
12279                'gnus-summary-tick-article-backward)
12280 (defun gnus-summary-tick-article-backward (n)
12281   "Tick N articles backwards.
12282 The difference between N and the number of articles ticked is returned."
12283   (interactive "p")
12284   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12285
12286 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12287 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12288 (defun gnus-summary-tick-article (&optional article clear-mark)
12289   "Mark current article as unread.
12290 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12291 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12292   (interactive)
12293   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12294                                        gnus-ticked-mark)))
12295
12296 (defun gnus-summary-mark-as-read-forward (n)
12297   "Mark N articles as read forwards.
12298 If N is negative, mark backwards instead.
12299 The difference between N and the actual number of articles marked is
12300 returned."
12301   (interactive "p")
12302   (gnus-summary-mark-forward n gnus-del-mark t))
12303
12304 (defun gnus-summary-mark-as-read-backward (n)
12305   "Mark the N articles as read backwards.
12306 The difference between N and the actual number of articles marked is
12307 returned."
12308   (interactive "p")
12309   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12310
12311 (defun gnus-summary-mark-as-read (&optional article mark)
12312   "Mark current article as read.
12313 ARTICLE specifies the article to be marked as read.
12314 MARK specifies a string to be inserted at the beginning of the line."
12315   (gnus-summary-mark-article article mark))
12316
12317 (defun gnus-summary-clear-mark-forward (n)
12318   "Clear marks from N articles forward.
12319 If N is negative, clear backward instead.
12320 The difference between N and the number of marks cleared is returned."
12321   (interactive "p")
12322   (gnus-summary-mark-forward n gnus-unread-mark))
12323
12324 (defun gnus-summary-clear-mark-backward (n)
12325   "Clear marks from N articles backward.
12326 The difference between N and the number of marks cleared is returned."
12327   (interactive "p")
12328   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12329
12330 (defun gnus-summary-mark-unread-as-read ()
12331   "Intended to be used by `gnus-summary-mark-article-hook'."
12332   (when (memq gnus-current-article gnus-newsgroup-unreads)
12333     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12334
12335 (defun gnus-summary-mark-read-and-unread-as-read ()
12336   "Intended to be used by `gnus-summary-mark-article-hook'."
12337   (let ((mark (gnus-summary-article-mark)))
12338     (when (or (gnus-unread-mark-p mark)
12339               (gnus-read-mark-p mark))
12340       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12341
12342 (defun gnus-summary-mark-region-as-read (point mark all)
12343   "Mark all unread articles between point and mark as read.
12344 If given a prefix, mark all articles between point and mark as read,
12345 even ticked and dormant ones."
12346   (interactive "r\nP")
12347   (save-excursion
12348     (let (article)
12349       (goto-char point)
12350       (beginning-of-line)
12351       (while (and
12352               (< (point) mark)
12353               (progn
12354                 (when (or all
12355                           (memq (setq article (gnus-summary-article-number))
12356                                 gnus-newsgroup-unreads))
12357                   (gnus-summary-mark-article article gnus-del-mark))
12358                 t)
12359               (gnus-summary-find-next))))))
12360
12361 (defun gnus-summary-mark-below (score mark)
12362   "Mark articles with score less than SCORE with MARK."
12363   (interactive "P\ncMark: ")
12364   (gnus-set-global-variables)
12365   (setq score (if score
12366                   (prefix-numeric-value score)
12367                 (or gnus-summary-default-score 0)))
12368   (save-excursion
12369     (set-buffer gnus-summary-buffer)
12370     (goto-char (point-min))
12371     (while 
12372         (progn
12373           (and (< (gnus-summary-article-score) score)
12374                (gnus-summary-mark-article nil mark))
12375           (gnus-summary-find-next)))))
12376
12377 (defun gnus-summary-kill-below (&optional score)
12378   "Mark articles with score below SCORE as read."
12379   (interactive "P")
12380   (gnus-set-global-variables)
12381   (gnus-summary-mark-below score gnus-killed-mark))
12382
12383 (defun gnus-summary-clear-above (&optional score)
12384   "Clear all marks from articles with score above SCORE."
12385   (interactive "P")
12386   (gnus-set-global-variables)
12387   (gnus-summary-mark-above score gnus-unread-mark))
12388
12389 (defun gnus-summary-tick-above (&optional score)
12390   "Tick all articles with score above SCORE."
12391   (interactive "P")
12392   (gnus-set-global-variables)
12393   (gnus-summary-mark-above score gnus-ticked-mark))
12394
12395 (defun gnus-summary-mark-above (score mark)
12396   "Mark articles with score over SCORE with MARK."
12397   (interactive "P\ncMark: ")
12398   (gnus-set-global-variables)
12399   (setq score (if score
12400                   (prefix-numeric-value score)
12401                 (or gnus-summary-default-score 0)))
12402   (save-excursion
12403     (set-buffer gnus-summary-buffer)
12404     (goto-char (point-min))
12405     (while (and (progn
12406                   (if (> (gnus-summary-article-score) score)
12407                       (gnus-summary-mark-article nil mark))
12408                   t)
12409                 (gnus-summary-find-next)))))
12410
12411 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12412 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12413 (defun gnus-summary-limit-include-expunged ()
12414   "Display all the hidden articles that were expunged for low scores."
12415   (interactive)
12416   (gnus-set-global-variables)
12417   (let ((buffer-read-only nil))
12418     (let ((scored gnus-newsgroup-scored)
12419           headers h)
12420       (while scored
12421         (or (gnus-summary-goto-subject (caar scored))
12422             (and (setq h (gnus-summary-article-header (caar scored)))
12423                  (< (cdar scored) gnus-summary-expunge-below)
12424                  (setq headers (cons h headers))))
12425         (setq scored (cdr scored)))
12426       (or headers (error "No expunged articles hidden."))
12427       (goto-char (point-min))
12428       (gnus-summary-prepare-unthreaded (nreverse headers)))
12429     (goto-char (point-min))
12430     (gnus-summary-position-point)))
12431
12432 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12433   "Mark all articles not marked as unread in this newsgroup as read.
12434 If prefix argument ALL is non-nil, all articles are marked as read.
12435 If QUIETLY is non-nil, no questions will be asked.
12436 If TO-HERE is non-nil, it should be a point in the buffer.  All
12437 articles before this point will be marked as read.
12438 The number of articles marked as read is returned."
12439   (interactive "P")
12440   (gnus-set-global-variables)
12441   (prog1
12442       (if (or quietly
12443               (not gnus-interactive-catchup) ;Without confirmation?
12444               gnus-expert-user
12445               (gnus-y-or-n-p
12446                (if all
12447                    "Mark absolutely all articles as read? "
12448                  "Mark all unread articles as read? ")))
12449           (if (and not-mark
12450                    (not gnus-newsgroup-adaptive)
12451                    (not gnus-newsgroup-auto-expire))
12452               (progn
12453                 (when all
12454                   (setq gnus-newsgroup-marked nil
12455                         gnus-newsgroup-dormant nil))
12456                 (setq gnus-newsgroup-unreads nil))
12457             ;; We actually mark all articles as canceled, which we
12458             ;; have to do when using auto-expiry or adaptive scoring.
12459             (gnus-summary-show-all-threads)
12460             (if (gnus-summary-first-subject (not all))
12461                 (while (and
12462                         (if to-here (< (point) to-here) t)
12463                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12464                         (gnus-summary-find-next (not all)))))
12465             (unless to-here
12466               (setq gnus-newsgroup-unreads nil))
12467             (gnus-set-mode-line 'summary)))
12468     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12469       (if (and (not to-here) (eq 'nnvirtual (car method)))
12470           (nnvirtual-catchup-group
12471            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12472     (gnus-summary-position-point)))
12473
12474 (defun gnus-summary-catchup-to-here (&optional all)
12475   "Mark all unticked articles before the current one as read.
12476 If ALL is non-nil, also mark ticked and dormant articles as read."
12477   (interactive "P")
12478   (gnus-set-global-variables)
12479   (save-excursion
12480     (gnus-save-hidden-threads
12481       (let ((beg (point)))
12482         ;; We check that there are unread articles.
12483         (when (or all (gnus-summary-find-prev))
12484           (gnus-summary-catchup all t beg)))))
12485   (gnus-summary-position-point))
12486
12487 (defun gnus-summary-catchup-all (&optional quietly)
12488   "Mark all articles in this newsgroup as read."
12489   (interactive "P")
12490   (gnus-set-global-variables)
12491   (gnus-summary-catchup t quietly))
12492
12493 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12494   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12495 If prefix argument ALL is non-nil, all articles are marked as read."
12496   (interactive "P")
12497   (gnus-set-global-variables)
12498   (gnus-summary-catchup all quietly nil 'fast)
12499   ;; Select next newsgroup or exit.
12500   (if (eq gnus-auto-select-next 'quietly)
12501       (gnus-summary-next-group nil)
12502     (gnus-summary-exit)))
12503
12504 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12505   "Mark all articles in this newsgroup as read, and then exit."
12506   (interactive "P")
12507   (gnus-set-global-variables)
12508   (gnus-summary-catchup-and-exit t quietly))
12509
12510 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12511 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12512   "Mark all articles in this group as read and select the next group.
12513 If given a prefix, mark all articles, unread as well as ticked, as
12514 read."
12515   (interactive "P")
12516   (gnus-set-global-variables)
12517   (save-excursion
12518     (gnus-summary-catchup all))
12519   (gnus-summary-next-article t nil nil t))
12520
12521 ;; Thread-based commands.
12522
12523 (defun gnus-summary-articles-in-thread (&optional article)
12524   "Return a list of all articles in the current thread.
12525 If ARTICLE is non-nil, return all articles in the thread that starts
12526 with that article."
12527   (let* ((article (or article (gnus-summary-article-number)))
12528          (data (gnus-data-find-list article))
12529          (top-level (gnus-data-level (car data)))
12530          (top-subject
12531           (cond ((null gnus-thread-operation-ignore-subject)
12532                  (gnus-simplify-subject-re
12533                   (mail-header-subject (gnus-data-header (car data)))))
12534                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12535                  (gnus-simplify-subject-fuzzy
12536                   (mail-header-subject (gnus-data-header (car data)))))
12537                 (t nil)))
12538          (end-point (save-excursion
12539                       (if (gnus-summary-go-to-next-thread) 
12540                           (point) (point-max))))
12541          articles)
12542     (while (and data
12543                 (< (gnus-data-pos (car data)) end-point))
12544       (when (or (not top-subject)
12545                 (string= top-subject
12546                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12547                              (gnus-simplify-subject-fuzzy
12548                               (mail-header-subject
12549                                (gnus-data-header (car data))))
12550                            (gnus-simplify-subject-re
12551                             (mail-header-subject
12552                              (gnus-data-header (car data)))))))
12553         (push (gnus-data-number (car data)) articles))
12554       (unless (and (setq data (cdr data))
12555                    (> (gnus-data-level (car data)) top-level))
12556         (setq data nil)))
12557     ;; Return the list of articles.
12558     (nreverse articles)))
12559
12560 (defun gnus-summary-rethread-current ()
12561   "Rethread the thread the current article is part of."
12562   (interactive)
12563   (gnus-set-global-variables)
12564   (let* ((gnus-show-threads t)
12565          (article (gnus-summary-article-number))
12566          (id (mail-header-id (gnus-summary-article-header)))
12567          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12568     (unless id
12569       (error "No article on the current line"))
12570     (gnus-rebuild-thread id)
12571     (gnus-summary-goto-subject article)))
12572
12573 (defun gnus-summary-reparent-thread ()
12574   "Make current article child of the marked (or previous) article.
12575
12576 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12577 is non-nil or the Subject: of both articles are the same."
12578   (interactive)
12579   (or (not (gnus-group-read-only-p))
12580       (error "The current newsgroup does not support article editing."))
12581   (or (<= (length gnus-newsgroup-processable) 1)
12582       (error "No more than one article may be marked."))
12583   (save-window-excursion
12584     (let ((gnus-article-buffer " *reparent*")
12585           (current-article (gnus-summary-article-number))
12586           ; first grab the marked article, otherwise one line up.
12587           (parent-article (if (not (null gnus-newsgroup-processable))
12588                               (car gnus-newsgroup-processable)
12589                             (save-excursion
12590                               (if (eq (forward-line -1) 0)
12591                                   (gnus-summary-article-number)
12592                                 (error "Beginning of summary buffer."))))))
12593       (or (not (eq current-article parent-article))
12594           (error "An article may not be self-referential."))
12595       (let ((message-id (mail-header-id 
12596                          (gnus-summary-article-header parent-article))))
12597         (or (and message-id (not (equal message-id "")))
12598             (error "No message-id in desired parent."))
12599         (gnus-summary-select-article t t nil current-article)
12600         (set-buffer gnus-article-buffer)
12601         (setq buffer-read-only nil)
12602         (let ((buf (format "%s" (buffer-string))))
12603           (erase-buffer)
12604           (insert buf))
12605         (goto-char (point-min))
12606         (if (search-forward-regexp "^References: " nil t)
12607             (insert message-id " " )
12608           (insert "References: " message-id "\n"))
12609         (or (gnus-request-replace-article current-article
12610                                           (car gnus-article-current)
12611                                           gnus-article-buffer)
12612             (error "Couldn't replace article."))
12613         (set-buffer gnus-summary-buffer)
12614         (gnus-summary-unmark-all-processable)
12615         (gnus-summary-rethread-current)
12616         (gnus-message 3 "Article %d is now the child of article %d."
12617                       current-article parent-article)))))
12618
12619 (defun gnus-summary-toggle-threads (&optional arg)
12620   "Toggle showing conversation threads.
12621 If ARG is positive number, turn showing conversation threads on."
12622   (interactive "P")
12623   (gnus-set-global-variables)
12624   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12625     (setq gnus-show-threads
12626           (if (null arg) (not gnus-show-threads)
12627             (> (prefix-numeric-value arg) 0)))
12628     (gnus-summary-prepare)
12629     (gnus-summary-goto-subject current)
12630     (gnus-summary-position-point)))
12631
12632 (defun gnus-summary-show-all-threads ()
12633   "Show all threads."
12634   (interactive)
12635   (gnus-set-global-variables)
12636   (save-excursion
12637     (let ((buffer-read-only nil))
12638       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12639   (gnus-summary-position-point))
12640
12641 (defun gnus-summary-show-thread ()
12642   "Show thread subtrees.
12643 Returns nil if no thread was there to be shown."
12644   (interactive)
12645   (gnus-set-global-variables)
12646   (let ((buffer-read-only nil)
12647         (orig (point))
12648         ;; first goto end then to beg, to have point at beg after let
12649         (end (progn (end-of-line) (point)))
12650         (beg (progn (beginning-of-line) (point))))
12651     (prog1
12652         ;; Any hidden lines here?
12653         (search-forward "\r" end t)
12654       (subst-char-in-region beg end ?\^M ?\n t)
12655       (goto-char orig)
12656       (gnus-summary-position-point))))
12657
12658 (defun gnus-summary-hide-all-threads ()
12659   "Hide all thread subtrees."
12660   (interactive)
12661   (gnus-set-global-variables)
12662   (save-excursion
12663     (goto-char (point-min))
12664     (gnus-summary-hide-thread)
12665     (while (zerop (gnus-summary-next-thread 1 t))
12666       (gnus-summary-hide-thread)))
12667   (gnus-summary-position-point))
12668
12669 (defun gnus-summary-hide-thread ()
12670   "Hide thread subtrees.
12671 Returns nil if no threads were there to be hidden."
12672   (interactive)
12673   (gnus-set-global-variables)
12674   (let ((buffer-read-only nil)
12675         (start (point))
12676         (article (gnus-summary-article-number)))
12677     (goto-char start)
12678     ;; Go forward until either the buffer ends or the subthread
12679     ;; ends.
12680     (when (and (not (eobp))
12681                (or (zerop (gnus-summary-next-thread 1 t))
12682                    (goto-char (point-max))))
12683       (prog1
12684           (if (and (> (point) start)
12685                    (search-backward "\n" start t))
12686               (progn
12687                 (subst-char-in-region start (point) ?\n ?\^M)
12688                 (gnus-summary-goto-subject article))
12689             (goto-char start)
12690             nil)
12691         ;;(gnus-summary-position-point)
12692         ))))
12693
12694 (defun gnus-summary-go-to-next-thread (&optional previous)
12695   "Go to the same level (or less) next thread.
12696 If PREVIOUS is non-nil, go to previous thread instead.
12697 Return the article number moved to, or nil if moving was impossible."
12698   (let ((level (gnus-summary-thread-level))
12699         (way (if previous -1 1))
12700         (beg (point)))
12701     (forward-line way)
12702     (while (and (not (eobp))
12703                 (< level (gnus-summary-thread-level)))
12704       (forward-line way))
12705     (if (eobp)
12706         (progn
12707           (goto-char beg)
12708           nil)
12709       (setq beg (point))
12710       (prog1
12711           (gnus-summary-article-number)
12712         (goto-char beg)))))
12713
12714 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12715   "Go to the same level (or less) next thread.
12716 If PREVIOUS is non-nil, go to previous thread instead.
12717 Return the article number moved to, or nil if moving was impossible."
12718   (if (and (eq gnus-summary-make-false-root 'dummy)
12719            (gnus-summary-article-intangible-p))
12720       (let ((beg (point)))
12721         (while (and (zerop (forward-line 1))
12722                     (not (gnus-summary-article-intangible-p))
12723                     (not (zerop (save-excursion 
12724                                   (gnus-summary-thread-level))))))
12725         (if (eobp)
12726             (progn
12727               (goto-char beg)
12728               nil)
12729           (point)))
12730     (let* ((level (gnus-summary-thread-level))
12731            (article (gnus-summary-article-number))
12732            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12733            oart)
12734       (while data
12735         (if (<= (gnus-data-level (car data)) level)
12736             (setq oart (gnus-data-number (car data))
12737                   data nil)
12738           (setq data (cdr data))))
12739       (and oart
12740            (gnus-summary-goto-subject oart)))))
12741
12742 (defun gnus-summary-next-thread (n &optional silent)
12743   "Go to the same level next N'th thread.
12744 If N is negative, search backward instead.
12745 Returns the difference between N and the number of skips actually
12746 done.
12747
12748 If SILENT, don't output messages."
12749   (interactive "p")
12750   (gnus-set-global-variables)
12751   (let ((backward (< n 0))
12752         (n (abs n))
12753         old dum int)
12754     (while (and (> n 0)
12755                 (gnus-summary-go-to-next-thread backward))
12756       (decf n))
12757     (unless silent 
12758       (gnus-summary-position-point))
12759     (when (and (not silent) (/= 0 n))
12760       (gnus-message 7 "No more threads"))
12761     n))
12762
12763 (defun gnus-summary-prev-thread (n)
12764   "Go to the same level previous N'th thread.
12765 Returns the difference between N and the number of skips actually
12766 done."
12767   (interactive "p")
12768   (gnus-set-global-variables)
12769   (gnus-summary-next-thread (- n)))
12770
12771 (defun gnus-summary-go-down-thread ()
12772   "Go down one level in the current thread."
12773   (let ((children (gnus-summary-article-children)))
12774     (and children
12775          (gnus-summary-goto-subject (car children)))))
12776
12777 (defun gnus-summary-go-up-thread ()
12778   "Go up one level in the current thread."
12779   (let ((parent (gnus-summary-article-parent)))
12780     (and parent
12781          (gnus-summary-goto-subject parent))))
12782
12783 (defun gnus-summary-down-thread (n)
12784   "Go down thread N steps.
12785 If N is negative, go up instead.
12786 Returns the difference between N and how many steps down that were
12787 taken."
12788   (interactive "p")
12789   (gnus-set-global-variables)
12790   (let ((up (< n 0))
12791         (n (abs n)))
12792     (while (and (> n 0)
12793                 (if up (gnus-summary-go-up-thread)
12794                   (gnus-summary-go-down-thread)))
12795       (setq n (1- n)))
12796     (gnus-summary-position-point)
12797     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12798     n))
12799
12800 (defun gnus-summary-up-thread (n)
12801   "Go up thread N steps.
12802 If N is negative, go up instead.
12803 Returns the difference between N and how many steps down that were
12804 taken."
12805   (interactive "p")
12806   (gnus-set-global-variables)
12807   (gnus-summary-down-thread (- n)))
12808
12809 (defun gnus-summary-top-thread ()
12810   "Go to the top of the thread."
12811   (interactive)
12812   (gnus-set-global-variables)
12813   (while (gnus-summary-go-up-thread))
12814   (gnus-summary-article-number))
12815
12816 (defun gnus-summary-kill-thread (&optional unmark)
12817   "Mark articles under current thread as read.
12818 If the prefix argument is positive, remove any kinds of marks.
12819 If the prefix argument is negative, tick articles instead."
12820   (interactive "P")
12821   (gnus-set-global-variables)
12822   (if unmark
12823       (setq unmark (prefix-numeric-value unmark)))
12824   (let ((articles (gnus-summary-articles-in-thread)))
12825     (save-excursion
12826       ;; Expand the thread.
12827       (gnus-summary-show-thread)
12828       ;; Mark all the articles.
12829       (while articles
12830         (gnus-summary-goto-subject (car articles))
12831         (cond ((null unmark)
12832                (gnus-summary-mark-article-as-read gnus-killed-mark))
12833               ((> unmark 0)
12834                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12835               (t
12836                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12837         (setq articles (cdr articles))))
12838     ;; Hide killed subtrees.
12839     (and (null unmark)
12840          gnus-thread-hide-killed
12841          (gnus-summary-hide-thread))
12842     ;; If marked as read, go to next unread subject.
12843     (if (null unmark)
12844         ;; Go to next unread subject.
12845         (gnus-summary-next-subject 1 t)))
12846   (gnus-set-mode-line 'summary))
12847
12848 ;; Summary sorting commands
12849
12850 (defun gnus-summary-sort-by-number (&optional reverse)
12851   "Sort summary buffer by article number.
12852 Argument REVERSE means reverse order."
12853   (interactive "P")
12854   (gnus-summary-sort 'number reverse))
12855
12856 (defun gnus-summary-sort-by-author (&optional reverse)
12857   "Sort summary buffer by author name alphabetically.
12858 If case-fold-search is non-nil, case of letters is ignored.
12859 Argument REVERSE means reverse order."
12860   (interactive "P")
12861   (gnus-summary-sort 'author reverse))
12862
12863 (defun gnus-summary-sort-by-subject (&optional reverse)
12864   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12865 If case-fold-search is non-nil, case of letters is ignored.
12866 Argument REVERSE means reverse order."
12867   (interactive "P")
12868   (gnus-summary-sort 'subject reverse))
12869
12870 (defun gnus-summary-sort-by-date (&optional reverse)
12871   "Sort summary buffer by date.
12872 Argument REVERSE means reverse order."
12873   (interactive "P")
12874   (gnus-summary-sort 'date reverse))
12875
12876 (defun gnus-summary-sort-by-score (&optional reverse)
12877   "Sort summary buffer by score.
12878 Argument REVERSE means reverse order."
12879   (interactive "P")
12880   (gnus-summary-sort 'score reverse))
12881
12882 (defun gnus-summary-sort (predicate reverse)
12883   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12884   (gnus-set-global-variables)
12885   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12886          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12887          (gnus-thread-sort-functions
12888           (list
12889            (if (not reverse)
12890                thread
12891              `(lambda (t1 t2)
12892                 (,thread t2 t1)))))
12893          (gnus-article-sort-functions
12894           (list
12895            (if (not reverse)
12896                article
12897              `(lambda (t1 t2)
12898                 (,article t2 t1)))))
12899          (buffer-read-only)
12900          (gnus-summary-prepare-hook nil))
12901     ;; We do the sorting by regenerating the threads.
12902     (gnus-summary-prepare)
12903     ;; Hide subthreads if needed.
12904     (when (and gnus-show-threads gnus-thread-hide-subtree)
12905       (gnus-summary-hide-all-threads)))
12906   ;; If in async mode, we send some info to the backend.
12907   (when gnus-newsgroup-async
12908     (gnus-request-asynchronous
12909      gnus-newsgroup-name gnus-newsgroup-data)))
12910
12911 (defun gnus-sortable-date (date)
12912   "Make sortable string by string-lessp from DATE.
12913 Timezone package is used."
12914   (condition-case ()
12915       (progn
12916         (setq date (inline (timezone-fix-time 
12917                             date nil 
12918                             (aref (inline (timezone-parse-date date)) 4))))
12919         (inline
12920           (timezone-make-sortable-date
12921            (aref date 0) (aref date 1) (aref date 2)
12922            (inline
12923              (timezone-make-time-string
12924               (aref date 3) (aref date 4) (aref date 5))))))
12925     (error "")))
12926   
12927 ;; Summary saving commands.
12928
12929 (defun gnus-summary-save-article (&optional n not-saved)
12930   "Save the current article using the default saver function.
12931 If N is a positive number, save the N next articles.
12932 If N is a negative number, save the N previous articles.
12933 If N is nil and any articles have been marked with the process mark,
12934 save those articles instead.
12935 The variable `gnus-default-article-saver' specifies the saver function."
12936   (interactive "P")
12937   (gnus-set-global-variables)
12938   (let ((articles (gnus-summary-work-articles n))
12939         (save-buffer (save-excursion 
12940                        (nnheader-set-temp-buffer " *Gnus Save*")))
12941         file header article)
12942     (while articles
12943       (setq header (gnus-summary-article-header
12944                     (setq article (pop articles))))
12945       (if (not (vectorp header))
12946           ;; This is a pseudo-article.
12947           (if (assq 'name header)
12948               (gnus-copy-file (cdr (assq 'name header)))
12949             (gnus-message 1 "Article %d is unsaveable" article))
12950         ;; This is a real article.
12951         (save-window-excursion
12952           (gnus-summary-select-article t nil nil article))
12953         (save-excursion
12954           (set-buffer save-buffer)
12955           (insert-buffer-substring gnus-original-article-buffer))
12956         (unless gnus-save-all-headers
12957           ;; Remove headers accoring to `gnus-saved-headers'.
12958           (let ((gnus-visible-headers
12959                  (or gnus-saved-headers gnus-visible-headers))
12960                 (gnus-article-buffer save-buffer))
12961             (gnus-article-hide-headers 1 t)))
12962         (save-window-excursion
12963           (if (not gnus-default-article-saver)
12964               (error "No default saver is defined.")
12965             ;; !!! Magic!  The saving functions all save
12966             ;; `gnus-original-article-buffer' (or so they think),
12967             ;; but we bind that variable to out save-buffer.
12968             (set-buffer gnus-article-buffer)
12969             (let ((gnus-original-article-buffer save-buffer))
12970               (setq file (funcall
12971                           gnus-default-article-saver
12972                           (cond
12973                            ((not gnus-prompt-before-saving)
12974                             'default)
12975                            ((eq gnus-prompt-before-saving 'always)
12976                             nil)
12977                            (t file)))))))
12978         (gnus-summary-remove-process-mark article)
12979         (unless not-saved
12980           (gnus-summary-set-saved-mark article))))
12981     (gnus-kill-buffer save-buffer)
12982     (gnus-summary-position-point)
12983     n))
12984
12985 (defun gnus-summary-pipe-output (&optional arg)
12986   "Pipe the current article to a subprocess.
12987 If N is a positive number, pipe the N next articles.
12988 If N is a negative number, pipe the N previous articles.
12989 If N is nil and any articles have been marked with the process mark,
12990 pipe those articles instead."
12991   (interactive "P")
12992   (gnus-set-global-variables)
12993   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12994     (gnus-summary-save-article arg t))
12995   (gnus-configure-windows 'pipe))
12996
12997 (defun gnus-summary-save-article-mail (&optional arg)
12998   "Append the current article to an mail file.
12999 If N is a positive number, save the N next articles.
13000 If N is a negative number, save the N previous articles.
13001 If N is nil and any articles have been marked with the process mark,
13002 save those articles instead."
13003   (interactive "P")
13004   (gnus-set-global-variables)
13005   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13006     (gnus-summary-save-article arg)))
13007
13008 (defun gnus-summary-save-article-rmail (&optional arg)
13009   "Append the current article to an rmail file.
13010 If N is a positive number, save the N next articles.
13011 If N is a negative number, save the N previous articles.
13012 If N is nil and any articles have been marked with the process mark,
13013 save those articles instead."
13014   (interactive "P")
13015   (gnus-set-global-variables)
13016   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13017     (gnus-summary-save-article arg)))
13018
13019 (defun gnus-summary-save-article-file (&optional arg)
13020   "Append the current article to a file.
13021 If N is a positive number, save the N next articles.
13022 If N is a negative number, save the N previous articles.
13023 If N is nil and any articles have been marked with the process mark,
13024 save those articles instead."
13025   (interactive "P")
13026   (gnus-set-global-variables)
13027   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13028     (gnus-summary-save-article arg)))
13029
13030 (defun gnus-summary-save-article-body-file (&optional arg)
13031   "Append the current article body to a file.
13032 If N is a positive number, save the N next articles.
13033 If N is a negative number, save the N previous articles.
13034 If N is nil and any articles have been marked with the process mark,
13035 save those articles instead."
13036   (interactive "P")
13037   (gnus-set-global-variables)
13038   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13039     (gnus-summary-save-article arg)))
13040
13041 (defun gnus-get-split-value (methods)
13042   "Return a value based on the split METHODS."
13043   (let (split-name method result match)
13044     (when methods
13045       (save-excursion
13046         (set-buffer gnus-original-article-buffer)
13047         (save-restriction
13048           (nnheader-narrow-to-headers)
13049           (while methods
13050             (goto-char (point-min))
13051             (setq method (pop methods))
13052             (setq match (car method))
13053             (when (cond
13054                    ((stringp match)
13055                     ;; Regular expression.
13056                     (condition-case ()
13057                         (re-search-forward match nil t)
13058                       (error nil)))
13059                    ((gnus-functionp match)
13060                     ;; Function.
13061                     (save-restriction
13062                       (widen)
13063                       (setq result (funcall match gnus-newsgroup-name))))
13064                    ((consp match)
13065                     ;; Form.
13066                     (save-restriction
13067                       (widen)
13068                       (setq result (eval match)))))
13069               (setq split-name (append (cdr method) split-name))
13070               (cond ((stringp result)
13071                      (push result split-name))
13072                     ((consp result)
13073                      (setq split-name (append result split-name)))))))))
13074     split-name))
13075
13076 (defun gnus-read-move-group-name (prompt default articles prefix)
13077   "Read a group name."
13078   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13079          (minibuffer-confirm-incomplete nil) ; XEmacs
13080          group-map
13081          (dum (mapatoms
13082                (lambda (g) 
13083                  (and (boundp g)
13084                       (symbol-name g)
13085                       (memq 'respool
13086                             (assoc (symbol-name
13087                                     (car (gnus-find-method-for-group
13088                                           (symbol-name g))))
13089                                    gnus-valid-select-methods))
13090                       (push (list (symbol-name g)) group-map)))
13091                gnus-active-hashtb))
13092          (prom
13093           (format "%s %s to:"
13094                   prompt
13095                   (if (> (length articles) 1)
13096                       (format "these %d articles" (length articles))
13097                     "this article")))
13098          (to-newsgroup
13099           (cond
13100            ((null split-name)
13101             (gnus-completing-read default prom
13102                                   group-map nil nil prefix
13103                                   'gnus-group-history))
13104            ((= 1 (length split-name))
13105             (gnus-completing-read (car split-name) prom group-map
13106                                   nil nil nil
13107                                   'gnus-group-history))
13108            (t
13109             (gnus-completing-read nil prom 
13110                                   (mapcar (lambda (el) (list el))
13111                                           (nreverse split-name))
13112                                   nil nil nil
13113                                   'gnus-group-history)))))
13114     (when to-newsgroup
13115       (if (or (string= to-newsgroup "")
13116               (string= to-newsgroup prefix))
13117           (setq to-newsgroup (or default "")))
13118       (or (gnus-active to-newsgroup)
13119           (gnus-activate-group to-newsgroup)
13120           (error "No such group: %s" to-newsgroup)))
13121     to-newsgroup))
13122
13123 (defun gnus-read-save-file-name (prompt default-name)
13124   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13125          (file
13126           ;; Let the split methods have their say.
13127           (cond
13128            ;; No split name was found.
13129            ((null split-name)
13130             (read-file-name
13131              (concat prompt " (default "
13132                      (file-name-nondirectory default-name) ") ")
13133              (file-name-directory default-name)
13134              default-name))
13135            ;; A single split name was found
13136            ((= 1 (length split-name))
13137             (read-file-name
13138              (concat prompt " (default " (car split-name) ") ")
13139              gnus-article-save-directory
13140              (concat gnus-article-save-directory (car split-name))))
13141            ;; A list of splits was found.
13142            (t
13143             (setq split-name (nreverse split-name))
13144             (let (result)
13145               (let ((file-name-history (nconc split-name file-name-history)))
13146                 (setq result
13147                       (read-file-name
13148                        (concat prompt " (`M-p' for defaults) ")
13149                        gnus-article-save-directory
13150                        (car split-name))))
13151               (car (push result file-name-history)))))))
13152     ;; If we have read a directory, we append the default file name.
13153     (when (file-directory-p file)
13154       (setq file (concat (file-name-as-directory file)
13155                          (file-name-nondirectory default-name))))
13156     ;; Possibly translate some charaters.
13157     (nnheader-translate-file-chars file)))
13158
13159 (defun gnus-article-archive-name (group)
13160   "Return the first instance of an \"Archive-name\" in the current buffer."
13161   (let ((case-fold-search t))
13162     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13163       (match-string 1))))
13164
13165 (defun gnus-summary-save-in-rmail (&optional filename)
13166   "Append this article to Rmail file.
13167 Optional argument FILENAME specifies file name.
13168 Directory to save to is default to `gnus-article-save-directory'."
13169   (interactive)
13170   (gnus-set-global-variables)
13171   (let ((default-name
13172           (funcall gnus-rmail-save-name gnus-newsgroup-name
13173                    gnus-current-headers gnus-newsgroup-last-rmail)))
13174     (setq filename
13175           (cond ((eq filename 'default)
13176                  default-name)
13177                 (filename filename)
13178                 (t (gnus-read-save-file-name
13179                     "Save in rmail file:" default-name))))
13180     (gnus-make-directory (file-name-directory filename))
13181     (gnus-eval-in-buffer-window
13182      gnus-original-article-buffer
13183      (save-excursion
13184        (save-restriction
13185          (widen)
13186          (gnus-output-to-rmail filename))))
13187     ;; Remember the directory name to save articles
13188     (setq gnus-newsgroup-last-rmail filename)))
13189
13190 (defun gnus-summary-save-in-mail (&optional filename)
13191   "Append this article to Unix mail file.
13192 Optional argument FILENAME specifies file name.
13193 Directory to save to is default to `gnus-article-save-directory'."
13194   (interactive)
13195   (gnus-set-global-variables)
13196   (let ((default-name
13197           (funcall gnus-mail-save-name gnus-newsgroup-name
13198                    gnus-current-headers gnus-newsgroup-last-mail)))
13199     (setq filename
13200           (cond ((eq filename 'default)
13201                  default-name)
13202                 (filename filename)
13203                 (t (gnus-read-save-file-name
13204                     "Save in Unix mail file:" default-name))))
13205     (setq filename
13206           (expand-file-name filename
13207                             (and default-name
13208                                  (file-name-directory default-name))))
13209     (gnus-make-directory (file-name-directory filename))
13210     (gnus-eval-in-buffer-window
13211      gnus-original-article-buffer
13212      (save-excursion
13213        (save-restriction
13214          (widen)
13215          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13216              (gnus-output-to-rmail filename)
13217            (let ((mail-use-rfc822 t))
13218              (rmail-output filename 1 t t))))))
13219     ;; Remember the directory name to save articles.
13220     (setq gnus-newsgroup-last-mail filename)))
13221
13222 (defun gnus-summary-save-in-file (&optional filename)
13223   "Append this article to file.
13224 Optional argument FILENAME specifies file name.
13225 Directory to save to is default to `gnus-article-save-directory'."
13226   (interactive)
13227   (gnus-set-global-variables)
13228   (let ((default-name
13229           (funcall gnus-file-save-name gnus-newsgroup-name
13230                    gnus-current-headers gnus-newsgroup-last-file)))
13231     (setq filename
13232           (cond ((eq filename 'default)
13233                  default-name)
13234                 (filename filename)
13235                 (t (gnus-read-save-file-name
13236                     "Save in file:" default-name))))
13237     (gnus-make-directory (file-name-directory filename))
13238     (gnus-eval-in-buffer-window
13239      gnus-original-article-buffer
13240      (save-excursion
13241        (save-restriction
13242          (widen)
13243          (gnus-output-to-file filename))))
13244     ;; Remember the directory name to save articles.
13245     (setq gnus-newsgroup-last-file filename)))
13246
13247 (defun gnus-summary-save-body-in-file (&optional filename)
13248   "Append this article body to a file.
13249 Optional argument FILENAME specifies file name.
13250 The directory to save in defaults to `gnus-article-save-directory'."
13251   (interactive)
13252   (gnus-set-global-variables)
13253   (let ((default-name
13254           (funcall gnus-file-save-name gnus-newsgroup-name
13255                    gnus-current-headers gnus-newsgroup-last-file)))
13256     (setq filename
13257           (cond ((eq filename 'default)
13258                  default-name)
13259                 (filename filename)
13260                 (t (gnus-read-save-file-name
13261                     "Save body in file:" default-name))))
13262     (gnus-make-directory (file-name-directory filename))
13263     (gnus-eval-in-buffer-window
13264      gnus-article-buffer
13265      (save-excursion
13266        (save-restriction
13267          (widen)
13268          (goto-char (point-min))
13269          (and (search-forward "\n\n" nil t)
13270               (narrow-to-region (point) (point-max)))
13271          (gnus-output-to-file filename))))
13272     ;; Remember the directory name to save articles.
13273     (setq gnus-newsgroup-last-file filename)))
13274
13275 (defun gnus-summary-save-in-pipe (&optional command)
13276   "Pipe this article to subprocess."
13277   (interactive)
13278   (gnus-set-global-variables)
13279   (setq command
13280         (cond ((eq command 'default)
13281                gnus-last-shell-command)
13282               (command command)
13283               (t (read-string "Shell command on article: "
13284                               gnus-last-shell-command))))
13285   (if (string-equal command "")
13286       (setq command gnus-last-shell-command))
13287   (gnus-eval-in-buffer-window
13288    gnus-article-buffer
13289    (save-restriction
13290      (widen)
13291      (shell-command-on-region (point-min) (point-max) command nil)))
13292   (setq gnus-last-shell-command command))
13293
13294 ;; Summary extract commands
13295
13296 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13297   (let ((buffer-read-only nil)
13298         (article (gnus-summary-article-number))
13299         after-article b e)
13300     (or (gnus-summary-goto-subject article)
13301         (error (format "No such article: %d" article)))
13302     (gnus-summary-position-point)
13303     ;; If all commands are to be bunched up on one line, we collect
13304     ;; them here.
13305     (if gnus-view-pseudos-separately
13306         ()
13307       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13308             files action)
13309         (while ps
13310           (setq action (cdr (assq 'action (car ps))))
13311           (setq files (list (cdr (assq 'name (car ps)))))
13312           (while (and ps (cdr ps)
13313                       (string= (or action "1")
13314                                (or (cdr (assq 'action (cadr ps))) "2")))
13315             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13316             (setcdr ps (cddr ps)))
13317           (if (not files)
13318               ()
13319             (if (not (string-match "%s" action))
13320                 (setq files (cons " " files)))
13321             (setq files (cons " " files))
13322             (and (assq 'execute (car ps))
13323                  (setcdr (assq 'execute (car ps))
13324                          (funcall (if (string-match "%s" action)
13325                                       'format 'concat)
13326                                   action
13327                                   (mapconcat (lambda (f) f) files " ")))))
13328           (setq ps (cdr ps)))))
13329     (if (and gnus-view-pseudos (not not-view))
13330         (while pslist
13331           (and (assq 'execute (car pslist))
13332                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13333                                      (eq gnus-view-pseudos 'not-confirm)))
13334           (setq pslist (cdr pslist)))
13335       (save-excursion
13336         (while pslist
13337           (setq after-article (or (cdr (assq 'article (car pslist)))
13338                                   (gnus-summary-article-number)))
13339           (gnus-summary-goto-subject after-article)
13340           (forward-line 1)
13341           (setq b (point))
13342           (insert "    " (file-name-nondirectory
13343                                 (cdr (assq 'name (car pslist))))
13344                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13345           (setq e (point))
13346           (forward-line -1)             ; back to `b'
13347           (gnus-add-text-properties
13348            b (1- e) (list 'gnus-number gnus-reffed-article-number
13349                           gnus-mouse-face-prop gnus-mouse-face))
13350           (gnus-data-enter
13351            after-article gnus-reffed-article-number
13352            gnus-unread-mark b (car pslist) 0 (- e b))
13353           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13354           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13355           (setq pslist (cdr pslist)))))))
13356
13357 (defun gnus-pseudos< (p1 p2)
13358   (let ((c1 (cdr (assq 'action p1)))
13359         (c2 (cdr (assq 'action p2))))
13360     (and c1 c2 (string< c1 c2))))
13361
13362 (defun gnus-request-pseudo-article (props)
13363   (cond ((assq 'execute props)
13364          (gnus-execute-command (cdr (assq 'execute props)))))
13365   (let ((gnus-current-article (gnus-summary-article-number)))
13366     (run-hooks 'gnus-mark-article-hook)))
13367
13368 (defun gnus-execute-command (command &optional automatic)
13369   (save-excursion
13370     (gnus-article-setup-buffer)
13371     (set-buffer gnus-article-buffer)
13372     (setq buffer-read-only nil)
13373     (let ((command (if automatic command (read-string "Command: " command)))
13374           ;; Just binding this here doesn't help, because there might
13375           ;; be output from the process after exiting the scope of 
13376           ;; this `let'.
13377           ;; (buffer-read-only nil)
13378           )
13379       (erase-buffer)
13380       (insert "$ " command "\n\n")
13381       (if gnus-view-pseudo-asynchronously
13382           (start-process "gnus-execute" nil "sh" "-c" command)
13383         (call-process "sh" nil t nil "-c" command)))))
13384
13385 (defun gnus-copy-file (file &optional to)
13386   "Copy FILE to TO."
13387   (interactive
13388    (list (read-file-name "Copy file: " default-directory)
13389          (read-file-name "Copy file to: " default-directory)))
13390   (gnus-set-global-variables)
13391   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13392   (and (file-directory-p to)
13393        (setq to (concat (file-name-as-directory to)
13394                         (file-name-nondirectory file))))
13395   (copy-file file to))
13396
13397 ;; Summary kill commands.
13398
13399 (defun gnus-summary-edit-global-kill (article)
13400   "Edit the \"global\" kill file."
13401   (interactive (list (gnus-summary-article-number)))
13402   (gnus-set-global-variables)
13403   (gnus-group-edit-global-kill article))
13404
13405 (defun gnus-summary-edit-local-kill ()
13406   "Edit a local kill file applied to the current newsgroup."
13407   (interactive)
13408   (gnus-set-global-variables)
13409   (setq gnus-current-headers (gnus-summary-article-header))
13410   (gnus-set-global-variables)
13411   (gnus-group-edit-local-kill
13412    (gnus-summary-article-number) gnus-newsgroup-name))
13413
13414 \f
13415 ;;;
13416 ;;; Gnus article mode
13417 ;;;
13418
13419 (put 'gnus-article-mode 'mode-class 'special)
13420
13421 (if gnus-article-mode-map
13422     nil
13423   (setq gnus-article-mode-map (make-keymap))
13424   (suppress-keymap gnus-article-mode-map)
13425
13426   (gnus-define-keys gnus-article-mode-map
13427     " " gnus-article-goto-next-page
13428     "\177" gnus-article-goto-prev-page
13429     [delete] gnus-article-goto-prev-page
13430     "\C-c^" gnus-article-refer-article
13431     "h" gnus-article-show-summary
13432     "s" gnus-article-show-summary
13433     "\C-c\C-m" gnus-article-mail
13434     "?" gnus-article-describe-briefly
13435     gnus-mouse-2 gnus-article-push-button
13436     "\r" gnus-article-press-button
13437     "\t" gnus-article-next-button
13438     "\M-\t" gnus-article-prev-button
13439     "\C-c\C-b" gnus-bug)
13440
13441   (substitute-key-definition
13442    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13443
13444 (defun gnus-article-mode ()
13445   "Major mode for displaying an article.
13446
13447 All normal editing commands are switched off.
13448
13449 The following commands are available:
13450
13451 \\<gnus-article-mode-map>
13452 \\[gnus-article-next-page]\t Scroll the article one page forwards
13453 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13454 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13455 \\[gnus-article-show-summary]\t Display the summary buffer
13456 \\[gnus-article-mail]\t Send a reply to the address near point
13457 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13458 \\[gnus-info-find-node]\t Go to the Gnus info node"
13459   (interactive)
13460   (when (and menu-bar-mode
13461              (gnus-visual-p 'article-menu 'menu))
13462     (gnus-article-make-menu-bar))
13463   (kill-all-local-variables)
13464   (gnus-simplify-mode-line)
13465   (setq mode-name "Article")
13466   (setq major-mode 'gnus-article-mode)
13467   (make-local-variable 'minor-mode-alist)
13468   (or (assq 'gnus-show-mime minor-mode-alist)
13469       (setq minor-mode-alist
13470             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13471   (use-local-map gnus-article-mode-map)
13472   (make-local-variable 'page-delimiter)
13473   (setq page-delimiter gnus-page-delimiter)
13474   (buffer-disable-undo (current-buffer))
13475   (setq buffer-read-only t)             ;Disable modification
13476   (run-hooks 'gnus-article-mode-hook))
13477
13478 (defun gnus-article-setup-buffer ()
13479   "Initialize the article buffer."
13480   (let* ((name (if gnus-single-article-buffer "*Article*"
13481                  (concat "*Article " gnus-newsgroup-name "*")))
13482          (original
13483           (progn (string-match "\\*Article" name)
13484                  (concat " *Original Article"
13485                          (substring name (match-end 0))))))
13486     (setq gnus-article-buffer name)
13487     (setq gnus-original-article-buffer original)
13488     ;; This might be a variable local to the summary buffer.
13489     (unless gnus-single-article-buffer
13490       (save-excursion
13491         (set-buffer gnus-summary-buffer)
13492         (setq gnus-article-buffer name)
13493         (setq gnus-original-article-buffer original)
13494         (gnus-set-global-variables))
13495       (make-local-variable 'gnus-summary-buffer))
13496     ;; Init original article buffer.
13497     (save-excursion
13498       (set-buffer (get-buffer-create gnus-original-article-buffer))
13499       (buffer-disable-undo (current-buffer))
13500       (setq major-mode 'gnus-original-article-mode)
13501       (make-local-variable 'gnus-original-article))
13502     (if (get-buffer name)
13503         (save-excursion
13504           (set-buffer name)
13505           (buffer-disable-undo (current-buffer))
13506           (setq buffer-read-only t)
13507           (gnus-add-current-to-buffer-list)
13508           (or (eq major-mode 'gnus-article-mode)
13509               (gnus-article-mode))
13510           (current-buffer))
13511       (save-excursion
13512         (set-buffer (get-buffer-create name))
13513         (gnus-add-current-to-buffer-list)
13514         (gnus-article-mode)
13515         (current-buffer)))))
13516
13517 ;; Set article window start at LINE, where LINE is the number of lines
13518 ;; from the head of the article.
13519 (defun gnus-article-set-window-start (&optional line)
13520   (set-window-start
13521    (get-buffer-window gnus-article-buffer t)
13522    (save-excursion
13523      (set-buffer gnus-article-buffer)
13524      (goto-char (point-min))
13525      (if (not line)
13526          (point-min)
13527        (gnus-message 6 "Moved to bookmark")
13528        (search-forward "\n\n" nil t)
13529        (forward-line line)
13530        (point)))))
13531
13532 (defun gnus-kill-all-overlays ()
13533   "Delete all overlays in the current buffer."
13534   (when (fboundp 'overlay-lists)
13535     (let* ((overlayss (overlay-lists))
13536            (buffer-read-only nil)
13537            (overlays (nconc (car overlayss) (cdr overlayss))))
13538       (while overlays
13539         (delete-overlay (pop overlays))))))
13540
13541 (defun gnus-request-article-this-buffer (article group)
13542   "Get an article and insert it into this buffer."
13543   (let (do-update-line)
13544     (prog1
13545         (save-excursion
13546           (erase-buffer)
13547           (gnus-kill-all-overlays)
13548           (setq group (or group gnus-newsgroup-name))
13549
13550           ;; Open server if it has closed.
13551           (gnus-check-server (gnus-find-method-for-group group))
13552
13553           ;; Using `gnus-request-article' directly will insert the article into
13554           ;; `nntp-server-buffer' - so we'll save some time by not having to
13555           ;; copy it from the server buffer into the article buffer.
13556
13557           ;; We only request an article by message-id when we do not have the
13558           ;; headers for it, so we'll have to get those.
13559           (when (stringp article)
13560             (let ((gnus-override-method gnus-refer-article-method))
13561               (gnus-read-header article)))
13562
13563           ;; If the article number is negative, that means that this article
13564           ;; doesn't belong in this newsgroup (possibly), so we find its
13565           ;; message-id and request it by id instead of number.
13566           (when (and (numberp article)
13567                      gnus-summary-buffer
13568                      (get-buffer gnus-summary-buffer)
13569                      (buffer-name (get-buffer gnus-summary-buffer)))
13570             (save-excursion
13571               (set-buffer gnus-summary-buffer)
13572               (let ((header (gnus-summary-article-header article)))
13573                 (if (< article 0)
13574                     (cond 
13575                      ((memq article gnus-newsgroup-sparse)
13576                       ;; This is a sparse gap article.
13577                       (setq do-update-line article)
13578                       (setq article (mail-header-id header))
13579                       (let ((gnus-override-method gnus-refer-article-method))
13580                         (gnus-read-header article))
13581                       (setq gnus-newsgroup-sparse
13582                             (delq article gnus-newsgroup-sparse)))
13583                      ((vectorp header)
13584                       ;; It's a real article.
13585                       (setq article (mail-header-id header)))
13586                      (t
13587                       ;; It is an extracted pseudo-article.
13588                       (setq article 'pseudo)
13589                       (gnus-request-pseudo-article header))))
13590                 
13591                 (let ((method (gnus-find-method-for-group 
13592                                gnus-newsgroup-name)))
13593                   (if (not (eq (car method) 'nneething))
13594                       ()
13595                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13596                                        (mail-header-subject header))))
13597                       (if (file-directory-p dir)
13598                           (progn
13599                             (setq article 'nneething)
13600                             (gnus-group-enter-directory dir)))))))))
13601
13602           (cond
13603            ;; Refuse to select canceled articles.
13604            ((and (numberp article)
13605                  gnus-summary-buffer
13606                  (get-buffer gnus-summary-buffer)
13607                  (buffer-name (get-buffer gnus-summary-buffer))
13608                  (eq (cdr (save-excursion
13609                             (set-buffer gnus-summary-buffer)
13610                             (assq article gnus-newsgroup-reads)))
13611                      gnus-canceled-mark))
13612             nil)
13613            ;; We first check `gnus-original-article-buffer'.
13614            ((and (get-buffer gnus-original-article-buffer)
13615                  (numberp article)
13616                  (save-excursion
13617                    (set-buffer gnus-original-article-buffer)
13618                    (and (equal (car gnus-original-article) group)
13619                         (eq (cdr gnus-original-article) article))))
13620             (insert-buffer-substring gnus-original-article-buffer)
13621             'article)
13622            ;; Check the backlog.
13623            ((and gnus-keep-backlog
13624                  (gnus-backlog-request-article group article (current-buffer)))
13625             'article)
13626            ;; Check the cache.
13627            ((and gnus-use-cache
13628                  (numberp article)
13629                  (gnus-cache-request-article article group))
13630             'article)
13631            ;; Get the article and put into the article buffer.
13632            ((or (stringp article) (numberp article))
13633             (let ((gnus-override-method
13634                    (and (stringp article) gnus-refer-article-method))
13635                   (buffer-read-only nil))
13636               (erase-buffer)
13637               (gnus-kill-all-overlays)
13638               (if (gnus-request-article article group (current-buffer))
13639                   (progn
13640                     (and gnus-keep-backlog
13641                          (numberp article)
13642                          (gnus-backlog-enter-article
13643                           group article (current-buffer)))
13644                     'article))))
13645            ;; It was a pseudo.
13646            (t article)))
13647
13648       ;; Take the article from the original article buffer
13649       ;; and place it in the buffer it's supposed to be in.
13650       (when (and (get-buffer gnus-article-buffer)
13651                  ;;(numberp article)
13652                  (equal (buffer-name (current-buffer))
13653                         (buffer-name (get-buffer gnus-article-buffer))))
13654         (save-excursion
13655           (if (get-buffer gnus-original-article-buffer)
13656               (set-buffer (get-buffer gnus-original-article-buffer))
13657             (set-buffer (get-buffer-create gnus-original-article-buffer))
13658             (buffer-disable-undo (current-buffer))
13659             (setq major-mode 'gnus-original-article-mode)
13660             (setq buffer-read-only t)
13661             (gnus-add-current-to-buffer-list))
13662           (let (buffer-read-only)
13663             (erase-buffer)
13664             (insert-buffer-substring gnus-article-buffer))
13665           (setq gnus-original-article (cons group article))))
13666     
13667       ;; Update sparse articles.
13668       (when (and do-update-line
13669                  (or (numberp article)
13670                      (stringp article)))
13671         (let ((buf (current-buffer)))
13672           (set-buffer gnus-summary-buffer)
13673           (gnus-summary-update-article do-update-line)
13674           (gnus-summary-goto-subject do-update-line nil t)
13675           (set-window-point (get-buffer-window (current-buffer) t)
13676                             (point))
13677           (set-buffer buf))))))
13678
13679 (defun gnus-read-header (id &optional header)
13680   "Read the headers of article ID and enter them into the Gnus system."
13681   (let ((group gnus-newsgroup-name)
13682         (gnus-override-method 
13683          (and (gnus-news-group-p gnus-newsgroup-name)
13684               gnus-refer-article-method))       
13685         where)
13686     ;; First we check to see whether the header in question is already
13687     ;; fetched.
13688     (if (stringp id)
13689         ;; This is a Message-ID.
13690         (setq header (or header (gnus-id-to-header id)))
13691       ;; This is an article number.
13692       (setq header (or header (gnus-summary-article-header id))))
13693     (if (and header
13694              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13695         ;; We have found the header.
13696         header
13697       ;; We have to really fetch the header to this article.
13698       (when (setq where
13699                   (if (gnus-check-backend-function 'request-head group)
13700                       (gnus-request-head id group)
13701                     (gnus-request-article id group)))
13702         (save-excursion
13703           (set-buffer nntp-server-buffer)
13704           (and (search-forward "\n\n" nil t)
13705                (delete-region (1- (point)) (point-max)))
13706           (goto-char (point-max))
13707           (insert ".\n")
13708           (goto-char (point-min))
13709           (insert "211 ")
13710           (princ (cond
13711                   ((numberp id) id)
13712                   ((cdr where) (cdr where))
13713                   (header (mail-header-number header))
13714                   (t gnus-reffed-article-number))
13715                  (current-buffer))
13716           (insert " Article retrieved.\n"))
13717         ;(when (and header
13718         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13719         ;  (setcar (gnus-id-to-thread id) nil))
13720         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13721             ()                          ; Malformed head.
13722           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13723             (if (and (stringp id)
13724                      (not (string= (gnus-group-real-name group)
13725                                    (car where))))
13726                 ;; If we fetched by Message-ID and the article came
13727                 ;; from a different group, we fudge some bogus article
13728                 ;; numbers for this article.
13729                 (mail-header-set-number header gnus-reffed-article-number))
13730             (decf gnus-reffed-article-number)
13731             (gnus-remove-header (mail-header-number header))
13732             (push header gnus-newsgroup-headers)
13733             (setq gnus-current-headers header)
13734             (push (mail-header-number header) gnus-newsgroup-limit))
13735           header)))))
13736
13737 (defun gnus-remove-header (number)
13738   "Remove header NUMBER from `gnus-newsgroup-headers'."
13739   (if (and gnus-newsgroup-headers
13740            (= number (mail-header-number (car gnus-newsgroup-headers))))
13741       (pop gnus-newsgroup-headers)
13742     (let ((headers gnus-newsgroup-headers))
13743       (while (and (cdr headers)
13744                   (not (= number (mail-header-number (cadr headers)))))
13745         (pop headers))
13746       (when (cdr headers)
13747         (setcdr headers (cddr headers))))))
13748
13749 (defun gnus-article-prepare (article &optional all-headers header)
13750   "Prepare ARTICLE in article mode buffer.
13751 ARTICLE should either be an article number or a Message-ID.
13752 If ARTICLE is an id, HEADER should be the article headers.
13753 If ALL-HEADERS is non-nil, no headers are hidden."
13754   (save-excursion
13755     ;; Make sure we start in a summary buffer.
13756     (unless (eq major-mode 'gnus-summary-mode)
13757       (set-buffer gnus-summary-buffer))
13758     (setq gnus-summary-buffer (current-buffer))
13759     ;; Make sure the connection to the server is alive.
13760     (unless (gnus-server-opened
13761              (gnus-find-method-for-group gnus-newsgroup-name))
13762       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13763       (gnus-request-group gnus-newsgroup-name t))
13764     (let* ((article (if header (mail-header-number header) article))
13765            (summary-buffer (current-buffer))
13766            (internal-hook gnus-article-internal-prepare-hook)
13767            (group gnus-newsgroup-name)
13768            result)
13769       (save-excursion
13770         (gnus-article-setup-buffer)
13771         (set-buffer gnus-article-buffer)
13772         ;; Deactivate active regions.
13773         (when (and (boundp 'transient-mark-mode)
13774                    transient-mark-mode)
13775           (setq mark-active nil))
13776         (if (not (setq result (let ((buffer-read-only nil))
13777                                 (gnus-request-article-this-buffer
13778                                  article group))))
13779             ;; There is no such article.
13780             (save-excursion
13781               (when (and (numberp article)
13782                          (not (memq article gnus-newsgroup-sparse)))
13783                 (setq gnus-article-current
13784                       (cons gnus-newsgroup-name article))
13785                 (set-buffer gnus-summary-buffer)
13786                 (setq gnus-current-article article)
13787                 (gnus-summary-mark-article article gnus-canceled-mark))
13788               (unless (memq article gnus-newsgroup-sparse)
13789                 (gnus-error
13790                  1 "No such article (may have expired or been canceled)")))
13791           (if (or (eq result 'pseudo) (eq result 'nneething))
13792               (progn
13793                 (save-excursion
13794                   (set-buffer summary-buffer)
13795                   (setq gnus-last-article gnus-current-article
13796                         gnus-newsgroup-history (cons gnus-current-article
13797                                                      gnus-newsgroup-history)
13798                         gnus-current-article 0
13799                         gnus-current-headers nil
13800                         gnus-article-current nil)
13801                   (if (eq result 'nneething)
13802                       (gnus-configure-windows 'summary)
13803                     (gnus-configure-windows 'article))
13804                   (gnus-set-global-variables))
13805                 (gnus-set-mode-line 'article))
13806             ;; The result from the `request' was an actual article -
13807             ;; or at least some text that is now displayed in the
13808             ;; article buffer.
13809             (if (and (numberp article)
13810                      (not (eq article gnus-current-article)))
13811                 ;; Seems like a new article has been selected.
13812                 ;; `gnus-current-article' must be an article number.
13813                 (save-excursion
13814                   (set-buffer summary-buffer)
13815                   (setq gnus-last-article gnus-current-article
13816                         gnus-newsgroup-history (cons gnus-current-article
13817                                                      gnus-newsgroup-history)
13818                         gnus-current-article article
13819                         gnus-current-headers
13820                         (gnus-summary-article-header gnus-current-article)
13821                         gnus-article-current
13822                         (cons gnus-newsgroup-name gnus-current-article))
13823                   (unless (vectorp gnus-current-headers)
13824                     (setq gnus-current-headers nil))
13825                   (gnus-summary-show-thread)
13826                   (run-hooks 'gnus-mark-article-hook)
13827                   (gnus-set-mode-line 'summary)
13828                   (and (gnus-visual-p 'article-highlight 'highlight)
13829                        (run-hooks 'gnus-visual-mark-article-hook))
13830                   ;; Set the global newsgroup variables here.
13831                   ;; Suggested by Jim Sisolak
13832                   ;; <sisolak@trans4.neep.wisc.edu>.
13833                   (gnus-set-global-variables)
13834                   (setq gnus-have-all-headers
13835                         (or all-headers gnus-show-all-headers))
13836                   (and gnus-use-cache
13837                        (vectorp (gnus-summary-article-header article))
13838                        (gnus-cache-possibly-enter-article
13839                         group article
13840                         (gnus-summary-article-header article)
13841                         (memq article gnus-newsgroup-marked)
13842                         (memq article gnus-newsgroup-dormant)
13843                         (memq article gnus-newsgroup-unreads)))))
13844             (when (or (numberp article)
13845                       (stringp article))
13846               ;; Hooks for getting information from the article.
13847               ;; This hook must be called before being narrowed.
13848               (let (buffer-read-only)
13849                 (run-hooks 'internal-hook)
13850                 (run-hooks 'gnus-article-prepare-hook)
13851                 ;; Decode MIME message.
13852                 (if gnus-show-mime
13853                     (if (or (not gnus-strict-mime)
13854                             (gnus-fetch-field "Mime-Version"))
13855                         (funcall gnus-show-mime-method)
13856                       (funcall gnus-decode-encoded-word-method)))
13857                 ;; Perform the article display hooks.
13858                 (run-hooks 'gnus-article-display-hook))
13859               ;; Do page break.
13860               (goto-char (point-min))
13861               (and gnus-break-pages (gnus-narrow-to-page)))
13862             (gnus-set-mode-line 'article)
13863             (gnus-configure-windows 'article)
13864             (goto-char (point-min))
13865             t))))))
13866
13867 (defun gnus-article-show-all-headers ()
13868   "Show all article headers in article mode buffer."
13869   (save-excursion
13870     (gnus-article-setup-buffer)
13871     (set-buffer gnus-article-buffer)
13872     (let ((buffer-read-only nil))
13873       (gnus-unhide-text (point-min) (point-max)))))
13874
13875 (defun gnus-article-hide-headers-if-wanted ()
13876   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13877 Provided for backwards compatibility."
13878   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13879       gnus-inhibit-hiding
13880       (gnus-article-hide-headers)))
13881
13882 (defsubst gnus-article-header-rank ()
13883   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13884   (let ((list gnus-sorted-header-list)
13885         (i 0))
13886     (while list
13887       (when (looking-at (car list))
13888         (setq list nil))
13889       (setq list (cdr list))
13890       (incf i))
13891     i))
13892
13893 (defun gnus-article-hide-headers (&optional arg delete)
13894   "Toggle whether to hide unwanted headers and possibly sort them as well.
13895 If given a negative prefix, always show; if given a positive prefix,
13896 always hide."
13897   (interactive "P")
13898   (if (gnus-article-check-hidden-text 'headers arg)
13899       ;; Show boring headers as well.
13900       (gnus-article-show-hidden-text 'boring-headers)
13901     ;; This function might be inhibited.
13902     (unless gnus-inhibit-hiding
13903       (save-excursion
13904         (set-buffer gnus-article-buffer)
13905         (save-restriction
13906           (let ((buffer-read-only nil)
13907                 (props (nconc (list 'gnus-type 'headers)
13908                               gnus-hidden-properties))
13909                 (max (1+ (length gnus-sorted-header-list)))
13910                 (ignored (when (not (stringp gnus-visible-headers))
13911                            (cond ((stringp gnus-ignored-headers)
13912                                   gnus-ignored-headers)
13913                                  ((listp gnus-ignored-headers)
13914                                   (mapconcat 'identity gnus-ignored-headers
13915                                              "\\|")))))
13916                 (visible
13917                  (cond ((stringp gnus-visible-headers)
13918                         gnus-visible-headers)
13919                        ((and gnus-visible-headers
13920                              (listp gnus-visible-headers))
13921                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13922                 want-list beg)
13923             ;; First we narrow to just the headers.
13924             (widen)
13925             (goto-char (point-min))
13926             ;; Hide any "From " lines at the beginning of (mail) articles.
13927             (while (looking-at "From ")
13928               (forward-line 1))
13929             (unless (bobp)
13930               (if delete
13931                   (delete-region (point-min) (point))
13932                 (gnus-hide-text (point-min) (point) props)))
13933             ;; Then treat the rest of the header lines.
13934             (narrow-to-region
13935              (point)
13936              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13937             ;; Then we use the two regular expressions
13938             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13939             ;; select which header lines is to remain visible in the
13940             ;; article buffer.
13941             (goto-char (point-min))
13942             (while (re-search-forward "^[^ \t]*:" nil t)
13943               (beginning-of-line)
13944               ;; We add the headers we want to keep to a list and delete
13945               ;; them from the buffer.
13946               (gnus-put-text-property 
13947                (point) (1+ (point)) 'message-rank
13948                (if (or (and visible (looking-at visible))
13949                        (and ignored
13950                             (not (looking-at ignored))))
13951                    (gnus-article-header-rank) 
13952                  (+ 2 max)))
13953               (forward-line 1))
13954             (message-sort-headers-1)
13955             (when (setq beg (text-property-any 
13956                              (point-min) (point-max) 'message-rank (+ 2 max)))
13957               ;; We make the unwanted headers invisible.
13958               (if delete
13959                   (delete-region beg (point-max))
13960                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13961                 (gnus-hide-text-type beg (point-max) 'headers))
13962               ;; Work around XEmacs lossage.
13963               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13964
13965 (defun gnus-article-hide-boring-headers (&optional arg)
13966   "Toggle hiding of headers that aren't very interesting.
13967 If given a negative prefix, always show; if given a positive prefix,
13968 always hide."
13969   (interactive "P")
13970   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13971     (save-excursion
13972       (set-buffer gnus-article-buffer)
13973       (save-restriction
13974         (let ((buffer-read-only nil)
13975               (list gnus-boring-article-headers)
13976               (inhibit-point-motion-hooks t)
13977               elem)
13978           (nnheader-narrow-to-headers)
13979           (while list
13980             (setq elem (pop list))
13981             (goto-char (point-min))
13982             (cond
13983              ;; Hide empty headers.
13984              ((eq elem 'empty)
13985               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13986                 (forward-line -1)
13987                 (gnus-hide-text-type
13988                  (progn (beginning-of-line) (point))
13989                  (progn 
13990                    (end-of-line)
13991                    (if (re-search-forward "^[^ \t]" nil t)
13992                        (match-beginning 0)
13993                      (point-max)))
13994                  'boring-headers)))
13995              ;; Hide boring Newsgroups header.
13996              ((eq elem 'newsgroups)
13997               (when (equal (message-fetch-field "newsgroups")
13998                            (gnus-group-real-name gnus-newsgroup-name))
13999                 (gnus-article-hide-header "newsgroups")))
14000              ((eq elem 'followup-to)
14001               (when (equal (message-fetch-field "followup-to")
14002                            (message-fetch-field "newsgroups"))
14003                 (gnus-article-hide-header "followup-to")))
14004              ((eq elem 'reply-to)
14005               (let ((from (message-fetch-field "from"))
14006                     (reply-to (message-fetch-field "reply-to")))
14007                 (when (and
14008                        from reply-to
14009                        (equal 
14010                         (nth 1 (funcall gnus-extract-address-components from))
14011                         (nth 1 (funcall gnus-extract-address-components
14012                                         reply-to))))
14013                   (gnus-article-hide-header "reply-to"))))
14014              ((eq elem 'date)
14015               (let ((date (message-fetch-field "date")))
14016                 (when (and date
14017                            (< (gnus-days-between date (current-time-string))
14018                               4))
14019                   (gnus-article-hide-header "date")))))))))))
14020
14021 (defun gnus-article-hide-header (header)
14022   (save-excursion
14023     (goto-char (point-min))
14024     (when (re-search-forward (concat "^" header ":") nil t)
14025       (gnus-hide-text-type
14026        (progn (beginning-of-line) (point))
14027        (progn 
14028          (end-of-line)
14029          (if (re-search-forward "^[^ \t]" nil t)
14030              (match-beginning 0)
14031            (point-max)))
14032        'boring-headers))))
14033
14034 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14035 (defun gnus-article-treat-overstrike ()
14036   "Translate overstrikes into bold text."
14037   (interactive)
14038   (save-excursion
14039     (set-buffer gnus-article-buffer)
14040     (let ((buffer-read-only nil))
14041       (while (search-forward "\b" nil t)
14042         (let ((next (following-char))
14043               (previous (char-after (- (point) 2))))
14044           (cond 
14045            ((eq next previous)
14046             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14047             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14048            ((eq next ?_)
14049             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14050             (gnus-put-text-property
14051              (- (point) 2) (1- (point)) 'face 'underline))
14052            ((eq previous ?_)
14053             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14054             (gnus-put-text-property
14055              (point) (1+ (point))       'face 'underline))))))))
14056
14057 (defun gnus-article-word-wrap ()
14058   "Format too long lines."
14059   (interactive)
14060   (save-excursion
14061     (set-buffer gnus-article-buffer)
14062     (let ((buffer-read-only nil))
14063       (widen)
14064       (goto-char (point-min))
14065       (search-forward "\n\n" nil t)
14066       (end-of-line 1)
14067       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14068             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14069             (adaptive-fill-mode t))
14070         (while (not (eobp))
14071           (and (>= (current-column) (min fill-column (window-width)))
14072                (/= (preceding-char) ?:)
14073                (fill-paragraph nil))
14074           (end-of-line 2))))))
14075
14076 (defun gnus-article-remove-cr ()
14077   "Remove carriage returns from an article."
14078   (interactive)
14079   (save-excursion
14080     (set-buffer gnus-article-buffer)
14081     (let ((buffer-read-only nil))
14082       (goto-char (point-min))
14083       (while (search-forward "\r" nil t)
14084         (replace-match "" t t)))))
14085
14086 (defun gnus-article-remove-trailing-blank-lines ()
14087   "Remove all trailing blank lines from the article."
14088   (interactive)
14089   (save-excursion
14090     (set-buffer gnus-article-buffer)
14091     (let ((buffer-read-only nil))
14092       (goto-char (point-max))
14093       (delete-region
14094        (point)
14095        (progn
14096          (while (looking-at "^[ \t]*$")
14097            (forward-line -1))
14098          (forward-line 1)
14099          (point))))))
14100
14101 (defun gnus-article-display-x-face (&optional force)
14102   "Look for an X-Face header and display it if present."
14103   (interactive (list 'force))
14104   (save-excursion
14105     (set-buffer gnus-article-buffer)
14106     ;; Delete the old process, if any.
14107     (when (process-status "gnus-x-face")
14108       (delete-process "gnus-x-face"))
14109     (let ((inhibit-point-motion-hooks t)
14110           (case-fold-search nil)
14111           from)
14112       (save-restriction
14113         (nnheader-narrow-to-headers)
14114         (setq from (message-fetch-field "from"))
14115         (goto-char (point-min))
14116         (when (and gnus-article-x-face-command
14117                    (or force
14118                        ;; Check whether this face is censored.
14119                        (not gnus-article-x-face-too-ugly)
14120                        (and gnus-article-x-face-too-ugly from
14121                             (not (string-match gnus-article-x-face-too-ugly
14122                                                from))))
14123                    ;; Has to be present.
14124                    (re-search-forward "^X-Face: " nil t))
14125           ;; We now have the area of the buffer where the X-Face is stored.
14126           (let ((beg (point))
14127                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14128             ;; We display the face.
14129             (if (symbolp gnus-article-x-face-command)
14130                 ;; The command is a lisp function, so we call it.
14131                 (if (gnus-functionp gnus-article-x-face-command)
14132                     (funcall gnus-article-x-face-command beg end)
14133                   (error "%s is not a function" gnus-article-x-face-command))
14134               ;; The command is a string, so we interpret the command
14135               ;; as a, well, command, and fork it off.
14136               (let ((process-connection-type nil))
14137                 (process-kill-without-query
14138                  (start-process
14139                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14140                 (process-send-region "gnus-x-face" beg end)
14141                 (process-send-eof "gnus-x-face")))))))))
14142
14143 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14144 (defun gnus-decode-rfc1522 ()
14145   "Hack to remove QP encoding from headers."
14146   (let ((case-fold-search t)
14147         (inhibit-point-motion-hooks t)
14148         (buffer-read-only nil)
14149         string)
14150     (save-restriction
14151       (narrow-to-region
14152        (goto-char (point-min))
14153        (or (search-forward "\n\n" nil t) (point-max)))
14154
14155       (while (re-search-forward 
14156               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14157         (setq string (match-string 1))
14158         (narrow-to-region (match-beginning 0) (match-end 0))
14159         (delete-region (point-min) (point-max))
14160         (insert string)
14161         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14162         (subst-char-in-region (point-min) (point-max) ?_ ? )
14163         (widen)
14164         (goto-char (point-min))))))
14165
14166 (defun gnus-article-de-quoted-unreadable (&optional force)
14167   "Do a naive translation of a quoted-printable-encoded article.
14168 This is in no way, shape or form meant as a replacement for real MIME
14169 processing, but is simply a stop-gap measure until MIME support is
14170 written.
14171 If FORCE, decode the article whether it is marked as quoted-printable
14172 or not."
14173   (interactive (list 'force))
14174   (save-excursion
14175     (set-buffer gnus-article-buffer)
14176     (let ((case-fold-search t)
14177           (buffer-read-only nil)
14178           (type (gnus-fetch-field "content-transfer-encoding")))
14179       (gnus-decode-rfc1522)
14180       (when (or force
14181                 (and type (string-match "quoted-printable" (downcase type))))
14182         (goto-char (point-min))
14183         (search-forward "\n\n" nil 'move)
14184         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14185
14186 (defun gnus-mime-decode-quoted-printable (from to)
14187   "Decode Quoted-Printable in the region between FROM and TO."
14188   (interactive "r")
14189   (goto-char from)
14190   (while (search-forward "=" to t)
14191     (cond ((eq (following-char) ?\n)
14192            (delete-char -1)
14193            (delete-char 1))
14194           ((looking-at "[0-9A-F][0-9A-F]")
14195            (subst-char-in-region
14196             (1- (point)) (point) ?=
14197             (hexl-hex-string-to-integer
14198              (buffer-substring (point) (+ 2 (point)))))
14199            (delete-char 2))
14200           ((looking-at "=")
14201            (delete-char 1))
14202           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14203
14204 (defun gnus-article-hide-pgp (&optional arg)
14205   "Toggle hiding of any PGP headers and signatures in the current article.
14206 If given a negative prefix, always show; if given a positive prefix,
14207 always hide."
14208   (interactive "P")
14209   (unless (gnus-article-check-hidden-text 'pgp arg)
14210     (save-excursion
14211       (set-buffer gnus-article-buffer)
14212       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14213             buffer-read-only beg end)
14214         (widen)
14215         (goto-char (point-min))
14216         ;; Hide the "header".
14217         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14218              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14219         (setq beg (point))
14220         ;; Hide the actual signature.
14221         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14222              (setq end (1+ (match-beginning 0)))
14223              (gnus-hide-text
14224               end
14225               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14226                   (match-end 0)
14227                 ;; Perhaps we shouldn't hide to the end of the buffer
14228                 ;; if there is no end to the signature?
14229                 (point-max))
14230               props))
14231         ;; Hide "- " PGP quotation markers.
14232         (when (and beg end)
14233           (narrow-to-region beg end)
14234           (goto-char (point-min))
14235           (while (re-search-forward "^- " nil t)
14236             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14237           (widen))))))
14238
14239 (defun gnus-article-hide-signature (&optional arg)
14240   "Hide the signature in the current article.
14241 If given a negative prefix, always show; if given a positive prefix,
14242 always hide."
14243   (interactive "P")
14244   (unless (gnus-article-check-hidden-text 'signature arg)
14245     (save-excursion
14246       (set-buffer gnus-article-buffer)
14247       (save-restriction
14248         (let ((buffer-read-only nil))
14249           (when (gnus-narrow-to-signature)
14250             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14251
14252 (defun gnus-article-strip-leading-blank-lines ()
14253   "Remove all blank lines from the beginning of the article."
14254   (interactive)
14255   (save-excursion
14256     (set-buffer gnus-article-buffer)
14257     (let (buffer-read-only)
14258       (goto-char (point-min))
14259       (when (search-forward "\n\n" nil t)
14260         (while (looking-at "[ \t]$")
14261           (gnus-delete-line))))))
14262
14263 (defvar mime::preview/content-list)
14264 (defvar mime::preview-content-info/point-min)
14265 (defun gnus-narrow-to-signature ()
14266   "Narrow to the signature."
14267   (widen)
14268   (if (and (boundp 'mime::preview/content-list)
14269            mime::preview/content-list)
14270       (let ((pcinfo (car (last mime::preview/content-list))))
14271         (narrow-to-region
14272          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14273          (point-max))
14274         t))
14275   (goto-char (point-max))
14276   (when (re-search-backward gnus-signature-separator nil t)
14277     (forward-line 1)
14278     (when (or (null gnus-signature-limit)
14279               (and (numberp gnus-signature-limit)
14280                    (< (- (point-max) (point)) gnus-signature-limit))
14281               (and (gnus-functionp gnus-signature-limit)
14282                    (funcall gnus-signature-limit))
14283               (and (stringp gnus-signature-limit)
14284                    (not (re-search-forward gnus-signature-limit nil t))))
14285       (narrow-to-region (point) (point-max))
14286       t)))
14287
14288 (defun gnus-article-check-hidden-text (type arg)
14289   "Return nil if hiding is necessary."
14290   (save-excursion
14291     (set-buffer gnus-article-buffer)
14292     (let ((hide (gnus-article-hidden-text-p type)))
14293       (cond ((or (and (null arg) (eq hide 'hidden))
14294                  (and arg (< (prefix-numeric-value arg) 1)))
14295              (gnus-article-show-hidden-text type))
14296             ((and (numberp arg) (> (prefix-numeric-value arg) 0))
14297              nil)
14298             ((eq hide 'shown)
14299              (gnus-article-show-hidden-text type t))
14300             (t nil)))))
14301
14302 (defun gnus-article-hidden-text-p (type)
14303   "Say whether the current buffer contains hidden text of type TYPE."
14304   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14305     (when pos
14306       (if (get-text-property pos 'invisible)
14307           'hidden
14308         'shown))))
14309
14310 (defun gnus-article-hide (&optional arg force)
14311   "Hide all the gruft in the current article.
14312 This means that PGP stuff, signatures, cited text and (some)
14313 headers will be hidden.
14314 If given a prefix, show the hidden text instead."
14315   (interactive (list current-prefix-arg 'force))
14316   (gnus-article-hide-headers arg)
14317   (gnus-article-hide-pgp arg)
14318   (gnus-article-hide-citation-maybe arg force)
14319   (gnus-article-hide-signature arg))
14320
14321 (defun gnus-article-show-hidden-text (type &optional hide)
14322   "Show all hidden text of type TYPE.
14323 If HIDE, hide the text instead."
14324   (save-excursion
14325     (set-buffer gnus-article-buffer)
14326     (let ((buffer-read-only nil)
14327           (inhibit-point-motion-hooks t)
14328           (beg (point-min)))
14329       (while (gnus-goto-char (text-property-any
14330                               beg (point-max) 'gnus-type type))
14331         (setq beg (point))
14332         (forward-char)
14333         (if hide
14334             (gnus-hide-text beg (point) gnus-hidden-properties)
14335           (gnus-unhide-text beg (point)))
14336         (setq beg (point)))
14337       t)))
14338
14339 (defvar gnus-article-time-units
14340   `((year . ,(* 365.25 24 60 60))
14341     (week . ,(* 7 24 60 60))
14342     (day . ,(* 24 60 60))
14343     (hour . ,(* 60 60))
14344     (minute . 60)
14345     (second . 1))
14346   "Mapping from time units to seconds.")
14347
14348 (defun gnus-article-date-ut (&optional type highlight)
14349   "Convert DATE date to universal time in the current article.
14350 If TYPE is `local', convert to local time; if it is `lapsed', output
14351 how much time has lapsed since DATE."
14352   (interactive (list 'ut t))
14353   (let* ((header (or gnus-current-headers
14354                      (gnus-summary-article-header) ""))
14355          (date (and (vectorp header) (mail-header-date header)))
14356          (date-regexp "^Date: \\|^X-Sent: ")
14357          (now (current-time))
14358          (inhibit-point-motion-hooks t)
14359          bface eface)
14360     (when (and date (not (string= date "")))
14361       (save-excursion
14362         (set-buffer gnus-article-buffer)
14363         (save-restriction
14364           (nnheader-narrow-to-headers)
14365           (let ((buffer-read-only nil))
14366             ;; Delete any old Date headers.
14367             (if (re-search-forward date-regexp nil t)
14368                 (progn
14369                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14370                         eface (get-text-property (1- (gnus-point-at-eol))
14371                                                  'face))
14372                   (message-remove-header date-regexp t)
14373                   (beginning-of-line))
14374               (goto-char (point-max)))
14375             (insert (gnus-make-date-line date type))
14376             ;; Do highlighting.
14377             (forward-line -1)
14378             (when (and (gnus-visual-p 'article-highlight 'highlight)
14379                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14380               (gnus-put-text-property (match-beginning 1) (match-end 1)
14381                                  'face bface)
14382               (gnus-put-text-property (match-beginning 2) (match-end 2)
14383                                  'face eface))))))))
14384
14385 (defun gnus-make-date-line (date type)
14386   "Return a DATE line of TYPE."
14387   (cond
14388    ;; Convert to the local timezone.  We have to slap a
14389    ;; `condition-case' round the calls to the timezone
14390    ;; functions since they aren't particularly resistant to
14391    ;; buggy dates.
14392    ((eq type 'local)
14393     (concat "Date: " (condition-case ()
14394                          (timezone-make-date-arpa-standard date)
14395                        (error date))
14396             "\n"))
14397    ;; Convert to Universal Time.
14398    ((eq type 'ut)
14399     (concat "Date: "
14400             (condition-case ()
14401                 (timezone-make-date-arpa-standard date nil "UT")
14402               (error date))
14403             "\n"))
14404    ;; Get the original date from the article.
14405    ((eq type 'original)
14406     (concat "Date: " date "\n"))
14407    ;; Do an X-Sent lapsed format.
14408    ((eq type 'lapsed)
14409     ;; If the date is seriously mangled, the timezone
14410     ;; functions are liable to bug out, so we condition-case
14411     ;; the entire thing.
14412     (let* ((now (current-time))
14413            (real-time
14414             (condition-case ()
14415                 (gnus-time-minus
14416                  (gnus-encode-date
14417                   (timezone-make-date-arpa-standard
14418                    (current-time-string now)
14419                    (current-time-zone now) "UT"))
14420                  (gnus-encode-date
14421                   (timezone-make-date-arpa-standard
14422                    date nil "UT")))
14423               (error '(0 0))))
14424            (real-sec (+ (* (float (car real-time)) 65536)
14425                         (cadr real-time)))
14426            (sec (abs real-sec))
14427            num prev)
14428       (cond
14429        ((equal real-time '(0 0))
14430         "X-Sent: Unknown\n")
14431        ((zerop sec)
14432         "X-Sent: Now\n")
14433        (t
14434         (concat
14435          "X-Sent: "
14436          ;; This is a bit convoluted, but basically we go
14437          ;; through the time units for years, weeks, etc,
14438          ;; and divide things to see whether that results
14439          ;; in positive answers.
14440          (mapconcat
14441           (lambda (unit)
14442             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14443                 ;; The (remaining) seconds are too few to
14444                 ;; be divided into this time unit.
14445                 ""
14446               ;; It's big enough, so we output it.
14447               (setq sec (- sec (* num (cdr unit))))
14448               (prog1
14449                   (concat (if prev ", " "") (int-to-string
14450                                              (floor num))
14451                           " " (symbol-name (car unit)) 
14452                           (if (> num 1) "s" ""))
14453                 (setq prev t))))
14454           gnus-article-time-units "")
14455          ;; If dates are odd, then it might appear like the
14456          ;; article was sent in the future.
14457          (if (> real-sec 0)
14458              " ago\n"
14459            " in the future\n"))))))
14460    (t
14461     (error "Unknown conversion type: %s" type))))
14462
14463 (defun gnus-article-date-local (&optional highlight)
14464   "Convert the current article date to the local timezone."
14465   (interactive (list t))
14466   (gnus-article-date-ut 'local highlight))
14467
14468 (defun gnus-article-date-original (&optional highlight)
14469   "Convert the current article date to what it was originally.
14470 This is only useful if you have used some other date conversion
14471 function and want to see what the date was before converting."
14472   (interactive (list t))
14473   (gnus-article-date-ut 'original highlight))
14474
14475 (defun gnus-article-date-lapsed (&optional highlight)
14476   "Convert the current article date to time lapsed since it was sent."
14477   (interactive (list t))
14478   (gnus-article-date-ut 'lapsed highlight))
14479
14480 (defun gnus-article-maybe-highlight ()
14481   "Do some article highlighting if `gnus-visual' is non-nil."
14482   (if (gnus-visual-p 'article-highlight 'highlight)
14483       (gnus-article-highlight-some)))
14484
14485 ;;; Article savers.
14486
14487 (defun gnus-output-to-rmail (file-name)
14488   "Append the current article to an Rmail file named FILE-NAME."
14489   (require 'rmail)
14490   ;; Most of these codes are borrowed from rmailout.el.
14491   (setq file-name (expand-file-name file-name))
14492   (setq rmail-default-rmail-file file-name)
14493   (let ((artbuf (current-buffer))
14494         (tmpbuf (get-buffer-create " *Gnus-output*")))
14495     (save-excursion
14496       (or (get-file-buffer file-name)
14497           (file-exists-p file-name)
14498           (if (gnus-yes-or-no-p
14499                (concat "\"" file-name "\" does not exist, create it? "))
14500               (let ((file-buffer (create-file-buffer file-name)))
14501                 (save-excursion
14502                   (set-buffer file-buffer)
14503                   (rmail-insert-rmail-file-header)
14504                   (let ((require-final-newline nil))
14505                     (write-region (point-min) (point-max) file-name t 1)))
14506                 (kill-buffer file-buffer))
14507             (error "Output file does not exist")))
14508       (set-buffer tmpbuf)
14509       (buffer-disable-undo (current-buffer))
14510       (erase-buffer)
14511       (insert-buffer-substring artbuf)
14512       (gnus-convert-article-to-rmail)
14513       ;; Decide whether to append to a file or to an Emacs buffer.
14514       (let ((outbuf (get-file-buffer file-name)))
14515         (if (not outbuf)
14516             (append-to-file (point-min) (point-max) file-name)
14517           ;; File has been visited, in buffer OUTBUF.
14518           (set-buffer outbuf)
14519           (let ((buffer-read-only nil)
14520                 (msg (and (boundp 'rmail-current-message)
14521                           (symbol-value 'rmail-current-message))))
14522             ;; If MSG is non-nil, buffer is in RMAIL mode.
14523             (if msg
14524                 (progn (widen)
14525                        (narrow-to-region (point-max) (point-max))))
14526             (insert-buffer-substring tmpbuf)
14527             (if msg
14528                 (progn
14529                   (goto-char (point-min))
14530                   (widen)
14531                   (search-backward "\^_")
14532                   (narrow-to-region (point) (point-max))
14533                   (goto-char (1+ (point-min)))
14534                   (rmail-count-new-messages t)
14535                   (rmail-show-message msg)))))))
14536     (kill-buffer tmpbuf)))
14537
14538 (defun gnus-output-to-file (file-name)
14539   "Append the current article to a file named FILE-NAME."
14540   (setq file-name (expand-file-name file-name))
14541   (let ((artbuf (current-buffer))
14542         (tmpbuf (get-buffer-create " *Gnus-output*")))
14543     (save-excursion
14544       (set-buffer tmpbuf)
14545       (buffer-disable-undo (current-buffer))
14546       (erase-buffer)
14547       (insert-buffer-substring artbuf)
14548       ;; Append newline at end of the buffer as separator, and then
14549       ;; save it to file.
14550       (goto-char (point-max))
14551       (insert "\n")
14552       (append-to-file (point-min) (point-max) file-name))
14553     (kill-buffer tmpbuf)))
14554
14555 (defun gnus-convert-article-to-rmail ()
14556   "Convert article in current buffer to Rmail message format."
14557   (let ((buffer-read-only nil))
14558     ;; Convert article directly into Babyl format.
14559     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14560     (goto-char (point-min))
14561     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14562     (while (search-forward "\n\^_" nil t) ;single char
14563       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14564     (goto-char (point-max))
14565     (insert "\^_")))
14566
14567 (defun gnus-narrow-to-page (&optional arg)
14568   "Narrow the article buffer to a page.
14569 If given a numerical ARG, move forward ARG pages."
14570   (interactive "P")
14571   (setq arg (if arg (prefix-numeric-value arg) 0))
14572   (save-excursion
14573     (set-buffer gnus-article-buffer)
14574     (goto-char (point-min))
14575     (widen)
14576     (when (gnus-visual-p 'page-marker)
14577       (let ((buffer-read-only nil))
14578         (gnus-remove-text-with-property 'gnus-prev)
14579         (gnus-remove-text-with-property 'gnus-next)))
14580     (when
14581         (cond ((< arg 0)
14582                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14583               ((> arg 0)
14584                (re-search-forward page-delimiter nil 'move arg)))
14585       (goto-char (match-end 0)))
14586     (narrow-to-region
14587      (point)
14588      (if (re-search-forward page-delimiter nil 'move)
14589          (match-beginning 0)
14590        (point)))
14591     (when (and (gnus-visual-p 'page-marker)
14592                (not (= (point-min) 1)))
14593       (save-excursion
14594         (goto-char (point-min))
14595         (gnus-insert-prev-page-button)))
14596     (when (and (gnus-visual-p 'page-marker)
14597                (not (= (1- (point-max)) (buffer-size))))
14598       (save-excursion
14599         (goto-char (point-max))
14600         (gnus-insert-next-page-button)))))
14601
14602 ;; Article mode commands
14603
14604 (defun gnus-article-goto-next-page ()
14605   "Show the next page of the article."
14606   (interactive)
14607   (when (gnus-article-next-page)
14608     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14609
14610 (defun gnus-article-goto-prev-page ()
14611   "Show the next page of the article."
14612   (interactive)
14613   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14614     (gnus-article-prev-page nil)))
14615
14616 (defun gnus-article-next-page (&optional lines)
14617   "Show the next page of the current article.
14618 If end of article, return non-nil.  Otherwise return nil.
14619 Argument LINES specifies lines to be scrolled up."
14620   (interactive "p")
14621   (move-to-window-line -1)
14622   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14623   (if (save-excursion
14624         (end-of-line)
14625         (and (pos-visible-in-window-p)  ;Not continuation line.
14626              (eobp)))
14627       ;; Nothing in this page.
14628       (if (or (not gnus-break-pages)
14629               (save-excursion
14630                 (save-restriction
14631                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14632           t                             ;Nothing more.
14633         (gnus-narrow-to-page 1)         ;Go to next page.
14634         nil)
14635     ;; More in this page.
14636     (condition-case ()
14637         (scroll-up lines)
14638       (end-of-buffer
14639        ;; Long lines may cause an end-of-buffer error.
14640        (goto-char (point-max))))
14641     (move-to-window-line 0)
14642     nil))
14643
14644 (defun gnus-article-prev-page (&optional lines)
14645   "Show previous page of current article.
14646 Argument LINES specifies lines to be scrolled down."
14647   (interactive "p")
14648   (move-to-window-line 0)
14649   (if (and gnus-break-pages
14650            (bobp)
14651            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14652       (progn
14653         (gnus-narrow-to-page -1)        ;Go to previous page.
14654         (goto-char (point-max))
14655         (recenter -1))
14656     (prog1
14657         (condition-case ()
14658             (scroll-down lines)
14659           (error nil))
14660       (move-to-window-line 0))))
14661
14662 (defun gnus-article-refer-article ()
14663   "Read article specified by message-id around point."
14664   (interactive)
14665   (let ((point (point)))
14666     (search-forward ">" nil t)          ;Move point to end of "<....>".
14667     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14668         (let ((message-id (match-string 1)))
14669           (goto-char point)
14670           (set-buffer gnus-summary-buffer)
14671           (gnus-summary-refer-article message-id))
14672       (goto-char (point))
14673       (error "No references around point"))))
14674
14675 (defun gnus-article-show-summary ()
14676   "Reconfigure windows to show summary buffer."
14677   (interactive)
14678   (gnus-configure-windows 'article)
14679   (gnus-summary-goto-subject gnus-current-article))
14680
14681 (defun gnus-article-describe-briefly ()
14682   "Describe article mode commands briefly."
14683   (interactive)
14684   (gnus-message 6
14685                 (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")))
14686
14687 (defun gnus-article-summary-command ()
14688   "Execute the last keystroke in the summary buffer."
14689   (interactive)
14690   (let ((obuf (current-buffer))
14691         (owin (current-window-configuration))
14692         func)
14693     (switch-to-buffer gnus-summary-buffer 'norecord)
14694     (setq func (lookup-key (current-local-map) (this-command-keys)))
14695     (call-interactively func)
14696     (set-buffer obuf)
14697     (set-window-configuration owin)
14698     (set-window-point (get-buffer-window (current-buffer)) (point))))
14699
14700 (defun gnus-article-summary-command-nosave ()
14701   "Execute the last keystroke in the summary buffer."
14702   (interactive)
14703   (let (func)
14704     (pop-to-buffer gnus-summary-buffer 'norecord)
14705     (setq func (lookup-key (current-local-map) (this-command-keys)))
14706     (call-interactively func)))
14707
14708 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14709   "Read a summary buffer key sequence and execute it from the article buffer."
14710   (interactive "P")
14711   (let ((nosaves
14712          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14713            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14714            "=" "^" "\M-^"))
14715         keys)
14716     (save-excursion
14717       (set-buffer gnus-summary-buffer)
14718       (push (or key last-command-event) unread-command-events)
14719       (setq keys (read-key-sequence nil)))
14720     (message "")
14721
14722     (if (member keys nosaves)
14723         (let (func)
14724           (pop-to-buffer gnus-summary-buffer 'norecord)
14725           (if (setq func (lookup-key (current-local-map) keys))
14726               (call-interactively func)
14727             (ding)))
14728       (let ((obuf (current-buffer))
14729             (owin (current-window-configuration))
14730             (opoint (point))
14731             func in-buffer)
14732         (if not-restore-window
14733             (pop-to-buffer gnus-summary-buffer 'norecord)
14734           (switch-to-buffer gnus-summary-buffer 'norecord))
14735         (setq in-buffer (current-buffer))
14736         (if (setq func (lookup-key (current-local-map) keys))
14737             (call-interactively func)
14738           (ding))
14739         (when (eq in-buffer (current-buffer))
14740           (set-buffer obuf)
14741           (unless not-restore-window
14742             (set-window-configuration owin))
14743           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14744
14745 \f
14746 ;;;
14747 ;;; Kill file handling.
14748 ;;;
14749
14750 ;;;###autoload
14751 (defalias 'gnus-batch-kill 'gnus-batch-score)
14752 ;;;###autoload
14753 (defun gnus-batch-score ()
14754   "Run batched scoring.
14755 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14756 Newsgroups is a list of strings in Bnews format.  If you want to score
14757 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14758 score the alt hierarchy, you'd say \"!alt.all\"."
14759   (interactive)
14760   (let* ((yes-and-no
14761           (gnus-newsrc-parse-options
14762            (apply (function concat)
14763                   (mapcar (lambda (g) (concat g " "))
14764                           command-line-args-left))))
14765          (gnus-expert-user t)
14766          (nnmail-spool-file nil)
14767          (gnus-use-dribble-file nil)
14768          (yes (car yes-and-no))
14769          (no (cdr yes-and-no))
14770          group newsrc entry
14771          ;; Disable verbose message.
14772          gnus-novice-user gnus-large-newsgroup)
14773     ;; Eat all arguments.
14774     (setq command-line-args-left nil)
14775     ;; Start Gnus.
14776     (gnus)
14777     ;; Apply kills to specified newsgroups in command line arguments.
14778     (setq newsrc (cdr gnus-newsrc-alist))
14779     (while newsrc
14780       (setq group (caar newsrc))
14781       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14782       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14783                (and (car entry)
14784                     (or (eq (car entry) t)
14785                         (not (zerop (car entry)))))
14786                (if yes (string-match yes group) t)
14787                (or (null no) (not (string-match no group))))
14788           (progn
14789             (gnus-summary-read-group group nil t nil t)
14790             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14791                  (gnus-summary-exit))))
14792       (setq newsrc (cdr newsrc)))
14793     ;; Exit Emacs.
14794     (switch-to-buffer gnus-group-buffer)
14795     (gnus-group-save-newsrc)))
14796
14797 (defun gnus-apply-kill-file ()
14798   "Apply a kill file to the current newsgroup.
14799 Returns the number of articles marked as read."
14800   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14801           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14802       (gnus-apply-kill-file-internal)
14803     0))
14804
14805 (defun gnus-kill-save-kill-buffer ()
14806   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14807     (when (get-file-buffer file)
14808       (save-excursion
14809         (set-buffer (get-file-buffer file))
14810         (and (buffer-modified-p) (save-buffer))
14811         (kill-buffer (current-buffer))))))
14812
14813 (defvar gnus-kill-file-name "KILL"
14814   "Suffix of the kill files.")
14815
14816 (defun gnus-newsgroup-kill-file (newsgroup)
14817   "Return the name of a kill file name for NEWSGROUP.
14818 If NEWSGROUP is nil, return the global kill file name instead."
14819   (cond 
14820    ;; The global KILL file is placed at top of the directory.
14821    ((or (null newsgroup)
14822         (string-equal newsgroup ""))
14823     (expand-file-name gnus-kill-file-name
14824                       gnus-kill-files-directory))
14825    ;; Append ".KILL" to newsgroup name.
14826    ((gnus-use-long-file-name 'not-kill)
14827     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14828                               "." gnus-kill-file-name)
14829                       gnus-kill-files-directory))
14830    ;; Place "KILL" under the hierarchical directory.
14831    (t
14832     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14833                               "/" gnus-kill-file-name)
14834                       gnus-kill-files-directory))))
14835
14836 \f
14837 ;;;
14838 ;;; Dribble file
14839 ;;;
14840
14841 (defvar gnus-dribble-ignore nil)
14842 (defvar gnus-dribble-eval-file nil)
14843
14844 (defun gnus-dribble-file-name ()
14845   "Return the dribble file for the current .newsrc."
14846   (concat
14847    (if gnus-dribble-directory
14848        (concat (file-name-as-directory gnus-dribble-directory)
14849                (file-name-nondirectory gnus-current-startup-file))
14850      gnus-current-startup-file)
14851    "-dribble"))
14852
14853 (defun gnus-dribble-enter (string)
14854   "Enter STRING into the dribble buffer."
14855   (if (and (not gnus-dribble-ignore)
14856            gnus-dribble-buffer
14857            (buffer-name gnus-dribble-buffer))
14858       (let ((obuf (current-buffer)))
14859         (set-buffer gnus-dribble-buffer)
14860         (insert string "\n")
14861         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14862         (set-buffer obuf))))
14863
14864 (defun gnus-dribble-read-file ()
14865   "Read the dribble file from disk."
14866   (let ((dribble-file (gnus-dribble-file-name)))
14867     (save-excursion
14868       (set-buffer (setq gnus-dribble-buffer
14869                         (get-buffer-create
14870                          (file-name-nondirectory dribble-file))))
14871       (gnus-add-current-to-buffer-list)
14872       (erase-buffer)
14873       (setq buffer-file-name dribble-file)
14874       (auto-save-mode t)
14875       (buffer-disable-undo (current-buffer))
14876       (bury-buffer (current-buffer))
14877       (set-buffer-modified-p nil)
14878       (let ((auto (make-auto-save-file-name))
14879             (gnus-dribble-ignore t)
14880             modes)
14881         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14882           ;; Load whichever file is newest -- the auto save file
14883           ;; or the "real" file.
14884           (if (file-newer-than-file-p auto dribble-file)
14885               (insert-file-contents auto)
14886             (insert-file-contents dribble-file))
14887           (unless (zerop (buffer-size))
14888             (set-buffer-modified-p t))
14889           ;; Set the file modes to reflect the .newsrc file modes.
14890           (save-buffer)
14891           (when (and (file-exists-p gnus-current-startup-file)
14892                      (setq modes (file-modes gnus-current-startup-file)))
14893             (set-file-modes dribble-file modes))
14894           ;; Possibly eval the file later.
14895           (when (gnus-y-or-n-p
14896                  "Auto-save file exists.  Do you want to read it? ")
14897             (setq gnus-dribble-eval-file t)))))))
14898
14899 (defun gnus-dribble-eval-file ()
14900   (when gnus-dribble-eval-file
14901     (setq gnus-dribble-eval-file nil)
14902     (save-excursion
14903       (let ((gnus-dribble-ignore t))
14904         (set-buffer gnus-dribble-buffer)
14905         (eval-buffer (current-buffer))))))
14906
14907 (defun gnus-dribble-delete-file ()
14908   (when (file-exists-p (gnus-dribble-file-name))
14909     (delete-file (gnus-dribble-file-name)))
14910   (when gnus-dribble-buffer
14911     (save-excursion
14912       (set-buffer gnus-dribble-buffer)
14913       (let ((auto (make-auto-save-file-name)))
14914         (if (file-exists-p auto)
14915             (delete-file auto))
14916         (erase-buffer)
14917         (set-buffer-modified-p nil)))))
14918
14919 (defun gnus-dribble-save ()
14920   (when (and gnus-dribble-buffer
14921              (buffer-name gnus-dribble-buffer))
14922     (save-excursion
14923       (set-buffer gnus-dribble-buffer)
14924       (save-buffer))))
14925
14926 (defun gnus-dribble-clear ()
14927   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14928     (save-excursion
14929       (set-buffer gnus-dribble-buffer)
14930       (erase-buffer)
14931       (set-buffer-modified-p nil)
14932       (setq buffer-saved-size (buffer-size)))))
14933
14934 \f
14935 ;;;
14936 ;;; Server Communication
14937 ;;;
14938
14939 (defun gnus-start-news-server (&optional confirm)
14940   "Open a method for getting news.
14941 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14942   (let (how)
14943     (if gnus-current-select-method
14944         ;; Stream is already opened.
14945         nil
14946       ;; Open NNTP server.
14947       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14948       (if confirm
14949           (progn
14950             ;; Read server name with completion.
14951             (setq gnus-nntp-server
14952                   (completing-read "NNTP server: "
14953                                    (mapcar (lambda (server) (list server))
14954                                            (cons (list gnus-nntp-server)
14955                                                  gnus-secondary-servers))
14956                                    nil nil gnus-nntp-server))))
14957
14958       (if (and gnus-nntp-server
14959                (stringp gnus-nntp-server)
14960                (not (string= gnus-nntp-server "")))
14961           (setq gnus-select-method
14962                 (cond ((or (string= gnus-nntp-server "")
14963                            (string= gnus-nntp-server "::"))
14964                        (list 'nnspool (system-name)))
14965                       ((string-match "^:" gnus-nntp-server)
14966                        (list 'nnmh gnus-nntp-server
14967                              (list 'nnmh-directory
14968                                    (file-name-as-directory
14969                                     (expand-file-name
14970                                      (concat "~/" (substring
14971                                                    gnus-nntp-server 1)))))
14972                              (list 'nnmh-get-new-mail nil)))
14973                       (t
14974                        (list 'nntp gnus-nntp-server)))))
14975
14976       (setq how (car gnus-select-method))
14977       (cond ((eq how 'nnspool)
14978              (require 'nnspool)
14979              (gnus-message 5 "Looking up local news spool..."))
14980             ((eq how 'nnmh)
14981              (require 'nnmh)
14982              (gnus-message 5 "Looking up mh spool..."))
14983             (t
14984              (require 'nntp)))
14985       (setq gnus-current-select-method gnus-select-method)
14986       (run-hooks 'gnus-open-server-hook)
14987       (or
14988        ;; gnus-open-server-hook might have opened it
14989        (gnus-server-opened gnus-select-method)
14990        (gnus-open-server gnus-select-method)
14991        (gnus-y-or-n-p
14992         (format
14993          "%s (%s) open error: '%s'.     Continue? "
14994          (car gnus-select-method) (cadr gnus-select-method)
14995          (gnus-status-message gnus-select-method)))
14996        (gnus-error 1 "Couldn't open server on %s"
14997                    (nth 1 gnus-select-method))))))
14998
14999 (defun gnus-check-group (group)
15000   "Try to make sure that the server where GROUP exists is alive."
15001   (let ((method (gnus-find-method-for-group group)))
15002     (or (gnus-server-opened method)
15003         (gnus-open-server method))))
15004
15005 (defun gnus-check-server (&optional method silent)
15006   "Check whether the connection to METHOD is down.
15007 If METHOD is nil, use `gnus-select-method'.
15008 If it is down, start it up (again)."
15009   (let ((method (or method gnus-select-method)))
15010     ;; Transform virtual server names into select methods.
15011     (when (stringp method)
15012       (setq method (gnus-server-to-method method)))
15013     (if (gnus-server-opened method)
15014         ;; The stream is already opened.
15015         t
15016       ;; Open the server.
15017       (unless silent
15018         (gnus-message 5 "Opening %s server%s..." (car method)
15019                       (if (equal (nth 1 method) "") ""
15020                         (format " on %s" (nth 1 method)))))
15021       (run-hooks 'gnus-open-server-hook)
15022       (prog1
15023           (gnus-open-server method)
15024         (unless silent
15025           (message ""))))))
15026
15027 (defun gnus-get-function (method function)
15028   "Return a function symbol based on METHOD and FUNCTION."
15029   ;; Translate server names into methods.
15030   (unless method
15031     (error "Attempted use of a nil select method"))
15032   (when (stringp method)
15033     (setq method (gnus-server-to-method method)))
15034   (let ((func (intern (format "%s-%s" (car method) function))))
15035     ;; If the functions isn't bound, we require the backend in
15036     ;; question.
15037     (unless (fboundp func)
15038       (require (car method))
15039       (unless (fboundp func)
15040         ;; This backend doesn't implement this function.
15041         (error "No such function: %s" func)))
15042     func))
15043
15044 \f
15045 ;;;
15046 ;;; Interface functions to the backends.
15047 ;;;
15048
15049 (defun gnus-open-server (method)
15050   "Open a connection to METHOD."
15051   (when (stringp method)
15052     (setq method (gnus-server-to-method method)))
15053   (let ((elem (assoc method gnus-opened-servers)))
15054     ;; If this method was previously denied, we just return nil.
15055     (if (eq (nth 1 elem) 'denied)
15056         (progn
15057           (gnus-message 1 "Denied server")
15058           nil)
15059       ;; Open the server.
15060       (let ((result
15061              (funcall (gnus-get-function method 'open-server)
15062                       (nth 1 method) (nthcdr 2 method))))
15063         ;; If this hasn't been opened before, we add it to the list.
15064         (unless elem
15065           (setq elem (list method nil)
15066                 gnus-opened-servers (cons elem gnus-opened-servers)))
15067         ;; Set the status of this server.
15068         (setcar (cdr elem) (if result 'ok 'denied))
15069         ;; Return the result from the "open" call.
15070         result))))
15071
15072 (defun gnus-close-server (method)
15073   "Close the connection to METHOD."
15074   (when (stringp method)
15075     (setq method (gnus-server-to-method method)))
15076   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15077
15078 (defun gnus-request-list (method)
15079   "Request the active file from METHOD."
15080   (when (stringp method)
15081     (setq method (gnus-server-to-method method)))
15082   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15083
15084 (defun gnus-request-list-newsgroups (method)
15085   "Request the newsgroups file from METHOD."
15086   (when (stringp method)
15087     (setq method (gnus-server-to-method method)))
15088   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15089
15090 (defun gnus-request-newgroups (date method)
15091   "Request all new groups since DATE from METHOD."
15092   (when (stringp method)
15093     (setq method (gnus-server-to-method method)))
15094   (funcall (gnus-get-function method 'request-newgroups)
15095            date (nth 1 method)))
15096
15097 (defun gnus-server-opened (method)
15098   "Check whether a connection to METHOD has been opened."
15099   (when (stringp method)
15100     (setq method (gnus-server-to-method method)))
15101   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15102
15103 (defun gnus-status-message (method)
15104   "Return the status message from METHOD.
15105 If METHOD is a string, it is interpreted as a group name.   The method
15106 this group uses will be queried."
15107   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15108                   method)))
15109     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15110
15111 (defun gnus-request-group (group &optional dont-check method)
15112   "Request GROUP.  If DONT-CHECK, no information is required."
15113   (let ((method (or method (gnus-find-method-for-group group))))
15114     (when (stringp method)
15115       (setq method (gnus-server-to-method method)))
15116     (funcall (gnus-get-function method 'request-group)
15117              (gnus-group-real-name group) (nth 1 method) dont-check)))
15118
15119 (defun gnus-request-asynchronous (group &optional articles)
15120   "Request that GROUP behave asynchronously.
15121 ARTICLES is the `data' of the group."
15122   (let ((method (gnus-find-method-for-group group)))
15123     (funcall (gnus-get-function method 'request-asynchronous)
15124              (gnus-group-real-name group) (nth 1 method) articles)))
15125
15126 (defun gnus-list-active-group (group)
15127   "Request active information on GROUP."
15128   (let ((method (gnus-find-method-for-group group))
15129         (func 'list-active-group))
15130     (when (gnus-check-backend-function func group)
15131       (funcall (gnus-get-function method func)
15132                (gnus-group-real-name group) (nth 1 method)))))
15133
15134 (defun gnus-request-group-description (group)
15135   "Request a description of GROUP."
15136   (let ((method (gnus-find-method-for-group group))
15137         (func 'request-group-description))
15138     (when (gnus-check-backend-function func group)
15139       (funcall (gnus-get-function method func)
15140                (gnus-group-real-name group) (nth 1 method)))))
15141
15142 (defun gnus-close-group (group)
15143   "Request the GROUP be closed."
15144   (let ((method (gnus-find-method-for-group group)))
15145     (funcall (gnus-get-function method 'close-group)
15146              (gnus-group-real-name group) (nth 1 method))))
15147
15148 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15149   "Request headers for ARTICLES in GROUP.
15150 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15151   (let ((method (gnus-find-method-for-group group)))
15152     (if (and gnus-use-cache (numberp (car articles)))
15153         (gnus-cache-retrieve-headers articles group fetch-old)
15154       (funcall (gnus-get-function method 'retrieve-headers)
15155                articles (gnus-group-real-name group) (nth 1 method)
15156                fetch-old))))
15157
15158 (defun gnus-retrieve-groups (groups method)
15159   "Request active information on GROUPS from METHOD."
15160   (when (stringp method)
15161     (setq method (gnus-server-to-method method)))
15162   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15163
15164 (defun gnus-request-type (group &optional article)
15165   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15166   (let ((method (gnus-find-method-for-group group)))
15167     (if (not (gnus-check-backend-function 'request-type (car method)))
15168         'unknown
15169       (funcall (gnus-get-function method 'request-type)
15170                (gnus-group-real-name group) article))))
15171
15172 (defun gnus-request-update-mark (group article mark)
15173   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15174   (let ((method (gnus-find-method-for-group group)))
15175     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15176         mark
15177       (funcall (gnus-get-function method 'request-update-mark)
15178                (gnus-group-real-name group) article mark))))
15179
15180 (defun gnus-request-article (article group &optional buffer)
15181   "Request the ARTICLE in GROUP.
15182 ARTICLE can either be an article number or an article Message-ID.
15183 If BUFFER, insert the article in that group."
15184   (let ((method (gnus-find-method-for-group group)))
15185     (funcall (gnus-get-function method 'request-article)
15186              article (gnus-group-real-name group) (nth 1 method) buffer)))
15187
15188 (defun gnus-request-head (article group)
15189   "Request the head of ARTICLE in GROUP."
15190   (let ((method (gnus-find-method-for-group group)))
15191     (funcall (gnus-get-function method 'request-head)
15192              article (gnus-group-real-name group) (nth 1 method))))
15193
15194 (defun gnus-request-body (article group)
15195   "Request the body of ARTICLE in GROUP."
15196   (let ((method (gnus-find-method-for-group group)))
15197     (funcall (gnus-get-function method 'request-body)
15198              article (gnus-group-real-name group) (nth 1 method))))
15199
15200 (defun gnus-request-post (method)
15201   "Post the current buffer using METHOD."
15202   (when (stringp method)
15203     (setq method (gnus-server-to-method method)))
15204   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15205
15206 (defun gnus-request-scan (group method)
15207   "Request a SCAN being performed in GROUP from METHOD.
15208 If GROUP is nil, all groups on METHOD are scanned."
15209   (let ((method (if group (gnus-find-method-for-group group) method)))
15210     (funcall (gnus-get-function method 'request-scan)
15211              (and group (gnus-group-real-name group)) (nth 1 method))))
15212
15213 (defsubst gnus-request-update-info (info method)
15214   "Request that METHOD update INFO."
15215   (when (stringp method)
15216     (setq method (gnus-server-to-method method)))
15217   (when (gnus-check-backend-function 'request-update-info (car method))
15218     (funcall (gnus-get-function method 'request-update-info)
15219              (gnus-group-real-name (gnus-info-group info))
15220              info (nth 1 method))))
15221
15222 (defun gnus-request-expire-articles (articles group &optional force)
15223   (let ((method (gnus-find-method-for-group group)))
15224     (funcall (gnus-get-function method 'request-expire-articles)
15225              articles (gnus-group-real-name group) (nth 1 method)
15226              force)))
15227
15228 (defun gnus-request-move-article
15229   (article group server accept-function &optional last)
15230   (let ((method (gnus-find-method-for-group group)))
15231     (funcall (gnus-get-function method 'request-move-article)
15232              article (gnus-group-real-name group)
15233              (nth 1 method) accept-function last)))
15234
15235 (defun gnus-request-accept-article (group method &optional last)
15236   ;; Make sure there's a newline at the end of the article.
15237   (when (stringp method)
15238     (setq method (gnus-server-to-method method)))
15239   (when (and (not method)
15240              (stringp group))
15241     (setq method (gnus-find-method-for-group group)))
15242   (goto-char (point-max))
15243   (unless (bolp)
15244     (insert "\n"))
15245   (let ((func (car (or method (gnus-find-method-for-group group)))))
15246     (funcall (intern (format "%s-request-accept-article" func))
15247              (if (stringp group) (gnus-group-real-name group) group)
15248              (cadr method)
15249              last)))
15250
15251 (defun gnus-request-replace-article (article group buffer)
15252   (let ((func (car (gnus-find-method-for-group group))))
15253     (funcall (intern (format "%s-request-replace-article" func))
15254              article (gnus-group-real-name group) buffer)))
15255
15256 (defun gnus-request-associate-buffer (group)
15257   (let ((method (gnus-find-method-for-group group)))
15258     (funcall (gnus-get-function method 'request-associate-buffer)
15259              (gnus-group-real-name group))))
15260
15261 (defun gnus-request-restore-buffer (article group)
15262   "Request a new buffer restored to the state of ARTICLE."
15263   (let ((method (gnus-find-method-for-group group)))
15264     (funcall (gnus-get-function method 'request-restore-buffer)
15265              article (gnus-group-real-name group) (nth 1 method))))
15266
15267 (defun gnus-request-create-group (group &optional method)
15268   (when (stringp method)
15269     (setq method (gnus-server-to-method method)))
15270   (let ((method (or method (gnus-find-method-for-group group))))
15271     (funcall (gnus-get-function method 'request-create-group)
15272              (gnus-group-real-name group) (nth 1 method))))
15273
15274 (defun gnus-request-delete-group (group &optional force)
15275   (let ((method (gnus-find-method-for-group group)))
15276     (funcall (gnus-get-function method 'request-delete-group)
15277              (gnus-group-real-name group) force (nth 1 method))))
15278
15279 (defun gnus-request-rename-group (group new-name)
15280   (let ((method (gnus-find-method-for-group group)))
15281     (funcall (gnus-get-function method 'request-rename-group)
15282              (gnus-group-real-name group)
15283              (gnus-group-real-name new-name) (nth 1 method))))
15284
15285 (defun gnus-member-of-valid (symbol group)
15286   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15287   (memq symbol (assoc
15288                 (symbol-name (car (gnus-find-method-for-group group)))
15289                 gnus-valid-select-methods)))
15290
15291 (defun gnus-method-option-p (method option)
15292   "Return non-nil if select METHOD has OPTION as a parameter."
15293   (when (stringp method)
15294     (setq method (gnus-server-to-method method)))
15295   (memq option (assoc (format "%s" (car method))
15296                       gnus-valid-select-methods)))
15297
15298 (defun gnus-server-extend-method (group method)
15299   ;; This function "extends" a virtual server.  If the server is
15300   ;; "hello", and the select method is ("hello" (my-var "something"))
15301   ;; in the group "alt.alt", this will result in a new virtual server
15302   ;; called "hello+alt.alt".
15303   (let ((entry
15304          (gnus-copy-sequence
15305           (if (equal (car method) "native") gnus-select-method
15306             (cdr (assoc (car method) gnus-server-alist))))))
15307     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15308     (nconc entry (cdr method))))
15309
15310 (defun gnus-find-method-for-group (group &optional info)
15311   "Find the select method that GROUP uses."
15312   (or gnus-override-method
15313       (and (not group)
15314            gnus-select-method)
15315       (let ((info (or info (gnus-get-info group)))
15316             method)
15317         (if (or (not info)
15318                 (not (setq method (gnus-info-method info)))
15319                 (equal method "native"))
15320             gnus-select-method
15321           (setq method
15322                 (cond ((stringp method)
15323                        (gnus-server-to-method method))
15324                       ((stringp (car method))
15325                        (gnus-server-extend-method group method))
15326                       (t
15327                        method)))
15328           (cond ((equal (cadr method) "")
15329                  method)
15330                 ((null (cadr method))
15331                  (list (car method) ""))
15332                 (t
15333                  (gnus-server-add-address method)))))))
15334
15335 (defun gnus-check-backend-function (func group)
15336   "Check whether GROUP supports function FUNC."
15337   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15338                   group)))
15339     (fboundp (intern (format "%s-%s" method func)))))
15340
15341 (defun gnus-methods-using (feature)
15342   "Find all methods that have FEATURE."
15343   (let ((valids gnus-valid-select-methods)
15344         outs)
15345     (while valids
15346       (if (memq feature (car valids))
15347           (setq outs (cons (car valids) outs)))
15348       (setq valids (cdr valids)))
15349     outs))
15350
15351 \f
15352 ;;;
15353 ;;; Active & Newsrc File Handling
15354 ;;;
15355
15356 (defun gnus-setup-news (&optional rawfile level dont-connect)
15357   "Setup news information.
15358 If RAWFILE is non-nil, the .newsrc file will also be read.
15359 If LEVEL is non-nil, the news will be set up at level LEVEL."
15360   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15361
15362     (when init 
15363       ;; Clear some variables to re-initialize news information.
15364       (setq gnus-newsrc-alist nil
15365             gnus-active-hashtb nil)
15366       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15367       (gnus-read-newsrc-file rawfile))
15368
15369     (when (and (not (assoc "archive" gnus-server-alist))
15370                gnus-message-archive-method)
15371       (push (cons "archive" gnus-message-archive-method)
15372             gnus-server-alist))
15373
15374     ;; If we don't read the complete active file, we fill in the
15375     ;; hashtb here.
15376     (if (or (null gnus-read-active-file)
15377             (eq gnus-read-active-file 'some))
15378         (gnus-update-active-hashtb-from-killed))
15379
15380     ;; Read the active file and create `gnus-active-hashtb'.
15381     ;; If `gnus-read-active-file' is nil, then we just create an empty
15382     ;; hash table.  The partial filling out of the hash table will be
15383     ;; done in `gnus-get-unread-articles'.
15384     (and gnus-read-active-file
15385          (not level)
15386          (gnus-read-active-file))
15387
15388     (or gnus-active-hashtb
15389         (setq gnus-active-hashtb (make-vector 4095 0)))
15390
15391     ;; Initialize the cache.
15392     (when gnus-use-cache
15393       (gnus-cache-open))
15394
15395     ;; Possibly eval the dribble file.
15396     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15397
15398     ;; Slave Gnusii should then clear the dribble buffer.
15399     (when (and init gnus-slave)
15400       (gnus-dribble-clear))
15401
15402     (gnus-update-format-specifications)
15403
15404     ;; See whether we need to read the description file.
15405     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15406              (not gnus-description-hashtb)
15407              (not dont-connect)
15408              gnus-read-active-file)
15409         (gnus-read-all-descriptions-files))
15410
15411     ;; Find new newsgroups and treat them.
15412     (if (and init gnus-check-new-newsgroups (not level)
15413              (gnus-check-server gnus-select-method))
15414         (gnus-find-new-newsgroups))
15415
15416     ;; We might read in new NoCeM messages here.
15417     (when (and gnus-use-nocem 
15418                (not level)
15419                (not dont-connect))
15420       (gnus-nocem-scan-groups))
15421
15422     ;; Find the number of unread articles in each non-dead group.
15423     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15424       (gnus-get-unread-articles level))
15425
15426     (if (and init gnus-check-bogus-newsgroups
15427              gnus-read-active-file (not level)
15428              (gnus-server-opened gnus-select-method))
15429         (gnus-check-bogus-newsgroups))))
15430
15431 (defun gnus-find-new-newsgroups (&optional arg)
15432   "Search for new newsgroups and add them.
15433 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15434 The `-n' option line from .newsrc is respected.
15435 If ARG (the prefix), use the `ask-server' method to query
15436 the server for new groups."
15437   (interactive "P")
15438   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15439                        (null gnus-read-active-file)
15440                        (eq gnus-read-active-file 'some))
15441                    'ask-server gnus-check-new-newsgroups)))
15442     (unless (gnus-check-first-time-used)
15443       (if (or (consp check)
15444               (eq check 'ask-server))
15445           ;; Ask the server for new groups.
15446           (gnus-ask-server-for-new-groups)
15447         ;; Go through the active hashtb and look for new groups.
15448         (let ((groups 0)
15449               group new-newsgroups)
15450           (gnus-message 5 "Looking for new newsgroups...")
15451           (unless gnus-have-read-active-file
15452             (gnus-read-active-file))
15453           (setq gnus-newsrc-last-checked-date (current-time-string))
15454           (unless gnus-killed-hashtb
15455             (gnus-make-hashtable-from-killed))
15456           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15457           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15458           (mapatoms
15459            (lambda (sym)
15460              (if (or (null (setq group (symbol-name sym)))
15461                      (not (boundp sym))
15462                      (null (symbol-value sym))
15463                      (gnus-gethash group gnus-killed-hashtb)
15464                      (gnus-gethash group gnus-newsrc-hashtb))
15465                  ()
15466                (let ((do-sub (gnus-matches-options-n group)))
15467                  (cond
15468                   ((eq do-sub 'subscribe)
15469                    (setq groups (1+ groups))
15470                    (gnus-sethash group group gnus-killed-hashtb)
15471                    (funcall gnus-subscribe-options-newsgroup-method group))
15472                   ((eq do-sub 'ignore)
15473                    nil)
15474                   (t
15475                    (setq groups (1+ groups))
15476                    (gnus-sethash group group gnus-killed-hashtb)
15477                    (if gnus-subscribe-hierarchical-interactive
15478                        (setq new-newsgroups (cons group new-newsgroups))
15479                      (funcall gnus-subscribe-newsgroup-method group)))))))
15480            gnus-active-hashtb)
15481           (when new-newsgroups
15482             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15483           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15484           (if (> groups 0)
15485               (gnus-message 6 "%d new newsgroup%s arrived."
15486                             groups (if (> groups 1) "s have" " has"))
15487             (gnus-message 6 "No new newsgroups.")))))))
15488
15489 (defun gnus-matches-options-n (group)
15490   ;; Returns `subscribe' if the group is to be unconditionally
15491   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15492   ;; no match for the group.
15493
15494   ;; First we check the two user variables.
15495   (cond
15496    ((and gnus-options-subscribe
15497          (string-match gnus-options-subscribe group))
15498     'subscribe)
15499    ((and gnus-auto-subscribed-groups
15500          (string-match gnus-auto-subscribed-groups group))
15501     'subscribe)
15502    ((and gnus-options-not-subscribe
15503          (string-match gnus-options-not-subscribe group))
15504     'ignore)
15505    ;; Then we go through the list that was retrieved from the .newsrc
15506    ;; file.  This list has elements on the form
15507    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15508    ;; is in the reverse order of the options line) is returned.
15509    (t
15510     (let ((regs gnus-newsrc-options-n))
15511       (while (and regs
15512                   (not (string-match (caar regs) group)))
15513         (setq regs (cdr regs)))
15514       (and regs (cdar regs))))))
15515
15516 (defun gnus-ask-server-for-new-groups ()
15517   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15518          (methods (cons gnus-select-method
15519                         (nconc
15520                          (when gnus-message-archive-method
15521                            (list "archive"))
15522                          (append
15523                           (and (consp gnus-check-new-newsgroups)
15524                                gnus-check-new-newsgroups)
15525                           gnus-secondary-select-methods))))
15526          (groups 0)
15527          (new-date (current-time-string))
15528          group new-newsgroups got-new method hashtb
15529          gnus-override-subscribe-method)
15530     ;; Go through both primary and secondary select methods and
15531     ;; request new newsgroups.
15532     (while (setq method (gnus-server-get-method nil (pop methods)))
15533       (setq new-newsgroups nil)
15534       (setq gnus-override-subscribe-method method)
15535       (when (and (gnus-check-server method)
15536                  (gnus-request-newgroups date method))
15537         (save-excursion
15538           (setq got-new t)
15539           (setq hashtb (gnus-make-hashtable 100))
15540           (set-buffer nntp-server-buffer)
15541           ;; Enter all the new groups into a hashtable.
15542           (gnus-active-to-gnus-format method hashtb 'ignore))
15543         ;; Now all new groups from `method' are in `hashtb'.
15544         (mapatoms
15545          (lambda (group-sym)
15546            (if (or (null (setq group (symbol-name group-sym)))
15547                    (not (boundp group-sym))
15548                    (null (symbol-value group-sym))
15549                    (gnus-gethash group gnus-newsrc-hashtb)
15550                    (member group gnus-zombie-list)
15551                    (member group gnus-killed-list))
15552                ;; The group is already known.
15553                ()
15554              ;; Make this group active.
15555              (when (symbol-value group-sym)
15556                (gnus-set-active group (symbol-value group-sym)))
15557              ;; Check whether we want it or not.
15558              (let ((do-sub (gnus-matches-options-n group)))
15559                (cond
15560                 ((eq do-sub 'subscribe)
15561                  (incf groups)
15562                  (gnus-sethash group group gnus-killed-hashtb)
15563                  (funcall gnus-subscribe-options-newsgroup-method group))
15564                 ((eq do-sub 'ignore)
15565                  nil)
15566                 (t
15567                  (incf groups)
15568                  (gnus-sethash group group gnus-killed-hashtb)
15569                  (if gnus-subscribe-hierarchical-interactive
15570                      (push group new-newsgroups)
15571                    (funcall gnus-subscribe-newsgroup-method group)))))))
15572          hashtb))
15573       (when new-newsgroups
15574         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15575     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15576     (when (> groups 0)
15577       (gnus-message 6 "%d new newsgroup%s arrived."
15578                     groups (if (> groups 1) "s have" " has")))
15579     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15580     got-new))
15581
15582 (defun gnus-check-first-time-used ()
15583   (if (or (> (length gnus-newsrc-alist) 1)
15584           (file-exists-p gnus-startup-file)
15585           (file-exists-p (concat gnus-startup-file ".el"))
15586           (file-exists-p (concat gnus-startup-file ".eld")))
15587       nil
15588     (gnus-message 6 "First time user; subscribing you to default groups")
15589     (unless (gnus-read-active-file-p)
15590       (gnus-read-active-file))
15591     (setq gnus-newsrc-last-checked-date (current-time-string))
15592     (let ((groups gnus-default-subscribed-newsgroups)
15593           group)
15594       (if (eq groups t)
15595           nil
15596         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15597         (mapatoms
15598          (lambda (sym)
15599            (if (null (setq group (symbol-name sym)))
15600                ()
15601              (let ((do-sub (gnus-matches-options-n group)))
15602                (cond
15603                 ((eq do-sub 'subscribe)
15604                  (gnus-sethash group group gnus-killed-hashtb)
15605                  (funcall gnus-subscribe-options-newsgroup-method group))
15606                 ((eq do-sub 'ignore)
15607                  nil)
15608                 (t
15609                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15610          gnus-active-hashtb)
15611         (while groups
15612           (if (gnus-active (car groups))
15613               (gnus-group-change-level
15614                (car groups) gnus-level-default-subscribed gnus-level-killed))
15615           (setq groups (cdr groups)))
15616         (gnus-group-make-help-group)
15617         (and gnus-novice-user
15618              (gnus-message 7 "`A k' to list killed groups"))))))
15619
15620 (defun gnus-subscribe-group (group previous &optional method)
15621   (gnus-group-change-level
15622    (if method
15623        (list t group gnus-level-default-subscribed nil nil method)
15624      group)
15625    gnus-level-default-subscribed gnus-level-killed previous t))
15626
15627 ;; `gnus-group-change-level' is the fundamental function for changing
15628 ;; subscription levels of newsgroups.  This might mean just changing
15629 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15630 ;; again, which subscribes/unsubscribes a group, which is equally
15631 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15632 ;; from 8-9 to 1-7 means that you remove the group from the list of
15633 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15634 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15635 ;; which is trivial.
15636 ;; ENTRY can either be a string (newsgroup name) or a list (if
15637 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15638 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15639 ;; entries.
15640 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15641 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15642 ;; after.
15643 (defun gnus-group-change-level (entry level &optional oldlevel
15644                                       previous fromkilled)
15645   (let (group info active num)
15646     ;; Glean what info we can from the arguments
15647     (if (consp entry)
15648         (if fromkilled (setq group (nth 1 entry))
15649           (setq group (car (nth 2 entry))))
15650       (setq group entry))
15651     (if (and (stringp entry)
15652              oldlevel
15653              (< oldlevel gnus-level-zombie))
15654         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15655     (if (and (not oldlevel)
15656              (consp entry))
15657         (setq oldlevel (gnus-info-level (nth 2 entry)))
15658       (setq oldlevel (or oldlevel 9)))
15659     (if (stringp previous)
15660         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15661
15662     (if (and (>= oldlevel gnus-level-zombie)
15663              (gnus-gethash group gnus-newsrc-hashtb))
15664         ;; We are trying to subscribe a group that is already
15665         ;; subscribed.
15666         ()                              ; Do nothing.
15667
15668       (or (gnus-ephemeral-group-p group)
15669           (gnus-dribble-enter
15670            (format "(gnus-group-change-level %S %S %S %S %S)"
15671                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15672
15673       ;; Then we remove the newgroup from any old structures, if needed.
15674       ;; If the group was killed, we remove it from the killed or zombie
15675       ;; list.  If not, and it is in fact going to be killed, we remove
15676       ;; it from the newsrc hash table and assoc.
15677       (cond
15678        ((>= oldlevel gnus-level-zombie)
15679         (if (= oldlevel gnus-level-zombie)
15680             (setq gnus-zombie-list (delete group gnus-zombie-list))
15681           (setq gnus-killed-list (delete group gnus-killed-list))))
15682        (t
15683         (if (and (>= level gnus-level-zombie)
15684                  entry)
15685             (progn
15686               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15687               (if (nth 3 entry)
15688                   (setcdr (gnus-gethash (car (nth 3 entry))
15689                                         gnus-newsrc-hashtb)
15690                           (cdr entry)))
15691               (setcdr (cdr entry) (cdddr entry))))))
15692
15693       ;; Finally we enter (if needed) the list where it is supposed to
15694       ;; go, and change the subscription level.  If it is to be killed,
15695       ;; we enter it into the killed or zombie list.
15696       (cond 
15697        ((>= level gnus-level-zombie)
15698         ;; Remove from the hash table.
15699         (gnus-sethash group nil gnus-newsrc-hashtb)
15700         ;; We do not enter foreign groups into the list of dead
15701         ;; groups.
15702         (unless (gnus-group-foreign-p group)
15703           (if (= level gnus-level-zombie)
15704               (setq gnus-zombie-list (cons group gnus-zombie-list))
15705             (setq gnus-killed-list (cons group gnus-killed-list)))))
15706        (t
15707         ;; If the list is to be entered into the newsrc assoc, and
15708         ;; it was killed, we have to create an entry in the newsrc
15709         ;; hashtb format and fix the pointers in the newsrc assoc.
15710         (if (< oldlevel gnus-level-zombie)
15711             ;; It was alive, and it is going to stay alive, so we
15712             ;; just change the level and don't change any pointers or
15713             ;; hash table entries.
15714             (setcar (cdaddr entry) level)
15715           (if (listp entry)
15716               (setq info (cdr entry)
15717                     num (car entry))
15718             (setq active (gnus-active group))
15719             (setq num
15720                   (if active (- (1+ (cdr active)) (car active)) t))
15721             ;; Check whether the group is foreign.  If so, the
15722             ;; foreign select method has to be entered into the
15723             ;; info.
15724             (let ((method (or gnus-override-subscribe-method
15725                               (gnus-group-method group))))
15726               (if (eq method gnus-select-method)
15727                   (setq info (list group level nil))
15728                 (setq info (list group level nil nil method)))))
15729           (unless previous
15730             (setq previous
15731                   (let ((p gnus-newsrc-alist))
15732                     (while (cddr p)
15733                       (setq p (cdr p)))
15734                     p)))
15735           (setq entry (cons info (cddr previous)))
15736           (if (cdr previous)
15737               (progn
15738                 (setcdr (cdr previous) entry)
15739                 (gnus-sethash group (cons num (cdr previous))
15740                               gnus-newsrc-hashtb))
15741             (setcdr previous entry)
15742             (gnus-sethash group (cons num previous)
15743                           gnus-newsrc-hashtb))
15744           (when (cdr entry)
15745             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15746       (when gnus-group-change-level-function
15747         (funcall gnus-group-change-level-function group level oldlevel)))))
15748
15749 (defun gnus-kill-newsgroup (newsgroup)
15750   "Obsolete function.  Kills a newsgroup."
15751   (gnus-group-change-level
15752    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15753
15754 (defun gnus-check-bogus-newsgroups (&optional confirm)
15755   "Remove bogus newsgroups.
15756 If CONFIRM is non-nil, the user has to confirm the deletion of every
15757 newsgroup."
15758   (let ((newsrc (cdr gnus-newsrc-alist))
15759         bogus group entry info)
15760     (gnus-message 5 "Checking bogus newsgroups...")
15761     (unless (gnus-read-active-file-p)
15762       (gnus-read-active-file))
15763     (when (gnus-read-active-file-p)
15764       ;; Find all bogus newsgroup that are subscribed.
15765       (while newsrc
15766         (setq info (pop newsrc)
15767               group (gnus-info-group info))
15768         (unless (or (gnus-active group) ; Active
15769                     (gnus-info-method info) ; Foreign
15770                     (and confirm
15771                          (not (gnus-y-or-n-p
15772                                (format "Remove bogus newsgroup: %s " group)))))
15773           ;; Found a bogus newsgroup.
15774           (push group bogus)))
15775       ;; Remove all bogus subscribed groups by first killing them, and
15776       ;; then removing them from the list of killed groups.
15777       (while bogus
15778         (when (setq entry (gnus-gethash (setq group (pop bogus))
15779                                         gnus-newsrc-hashtb))
15780           (gnus-group-change-level entry gnus-level-killed)
15781           (setq gnus-killed-list (delete group gnus-killed-list))))
15782       ;; Then we remove all bogus groups from the list of killed and
15783       ;; zombie groups.  They are are removed without confirmation.
15784       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15785             killed)
15786         (while dead-lists
15787           (setq killed (symbol-value (car dead-lists)))
15788           (while killed
15789             (unless (gnus-active (setq group (pop killed)))
15790               ;; The group is bogus.
15791               ;; !!!Slow as hell.
15792               (set (car dead-lists)
15793                    (delete group (symbol-value (car dead-lists))))))
15794           (setq dead-lists (cdr dead-lists))))
15795       (gnus-message 5 "Checking bogus newsgroups...done"))))
15796
15797 (defun gnus-check-duplicate-killed-groups ()
15798   "Remove duplicates from the list of killed groups."
15799   (interactive)
15800   (let ((killed gnus-killed-list))
15801     (while killed
15802       (gnus-message 9 "%d" (length killed))
15803       (setcdr killed (delete (car killed) (cdr killed)))
15804       (setq killed (cdr killed)))))
15805
15806 ;; We want to inline a function from gnus-cache, so we cheat here:
15807 (eval-when-compile
15808   (provide 'gnus)
15809   (require 'gnus-cache))
15810
15811 (defun gnus-get-unread-articles-in-group (info active &optional update)
15812   (when active
15813     ;; Allow the backend to update the info in the group.
15814     (when (and update 
15815                (gnus-request-update-info
15816                 info (gnus-find-method-for-group (gnus-info-group info))))
15817       (gnus-activate-group (gnus-info-group info) nil t))
15818     (let* ((range (gnus-info-read info))
15819            (num 0))
15820       ;; If a cache is present, we may have to alter the active info.
15821       (when (and gnus-use-cache info)
15822         (inline (gnus-cache-possibly-alter-active 
15823                  (gnus-info-group info) active)))
15824       ;; Modify the list of read articles according to what articles
15825       ;; are available; then tally the unread articles and add the
15826       ;; number to the group hash table entry.
15827       (cond
15828        ((zerop (cdr active))
15829         (setq num 0))
15830        ((not range)
15831         (setq num (- (1+ (cdr active)) (car active))))
15832        ((not (listp (cdr range)))
15833         ;; Fix a single (num . num) range according to the
15834         ;; active hash table.
15835         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15836         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15837         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15838         ;; Compute number of unread articles.
15839         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15840        (t
15841         ;; The read list is a list of ranges.  Fix them according to
15842         ;; the active hash table.
15843         ;; First peel off any elements that are below the lower
15844         ;; active limit.
15845         (while (and (cdr range)
15846                     (>= (car active)
15847                         (or (and (atom (cadr range)) (cadr range))
15848                             (caadr range))))
15849           (if (numberp (car range))
15850               (setcar range
15851                       (cons (car range)
15852                             (or (and (numberp (cadr range))
15853                                      (cadr range))
15854                                 (cdadr range))))
15855             (setcdr (car range)
15856                     (or (and (numberp (nth 1 range)) (nth 1 range))
15857                         (cdadr range))))
15858           (setcdr range (cddr range)))
15859         ;; Adjust the first element to be the same as the lower limit.
15860         (if (and (not (atom (car range)))
15861                  (< (cdar range) (car active)))
15862             (setcdr (car range) (1- (car active))))
15863         ;; Then we want to peel off any elements that are higher
15864         ;; than the upper active limit.
15865         (let ((srange range))
15866           ;; Go past all legal elements.
15867           (while (and (cdr srange)
15868                       (<= (or (and (atom (cadr srange))
15869                                    (cadr srange))
15870                               (caadr srange)) (cdr active)))
15871             (setq srange (cdr srange)))
15872           (if (cdr srange)
15873               ;; Nuke all remaining illegal elements.
15874               (setcdr srange nil))
15875
15876           ;; Adjust the final element.
15877           (if (and (not (atom (car srange)))
15878                    (> (cdar srange) (cdr active)))
15879               (setcdr (car srange) (cdr active))))
15880         ;; Compute the number of unread articles.
15881         (while range
15882           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15883                                       (cdar range)))
15884                               (or (and (atom (car range)) (car range))
15885                                   (caar range)))))
15886           (setq range (cdr range)))
15887         (setq num (max 0 (- (cdr active) num)))))
15888       ;; Set the number of unread articles.
15889       (when info
15890         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15891       num)))
15892
15893 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15894 ;; and compute how many unread articles there are in each group.
15895 (defun gnus-get-unread-articles (&optional level)
15896   (let* ((newsrc (cdr gnus-newsrc-alist))
15897          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15898          (foreign-level
15899           (min
15900            (cond ((and gnus-activate-foreign-newsgroups
15901                        (not (numberp gnus-activate-foreign-newsgroups)))
15902                   (1+ gnus-level-subscribed))
15903                  ((numberp gnus-activate-foreign-newsgroups)
15904                   gnus-activate-foreign-newsgroups)
15905                  (t 0))
15906            level))
15907          info group active method)
15908     (gnus-message 5 "Checking new news...")
15909
15910     (while newsrc
15911       (setq active (gnus-active (setq group (gnus-info-group
15912                                              (setq info (pop newsrc))))))
15913
15914       ;; Check newsgroups.  If the user doesn't want to check them, or
15915       ;; they can't be checked (for instance, if the news server can't
15916       ;; be reached) we just set the number of unread articles in this
15917       ;; newsgroup to t.  This means that Gnus thinks that there are
15918       ;; unread articles, but it has no idea how many.
15919       (if (and (setq method (gnus-info-method info))
15920                (not (gnus-server-equal
15921                      gnus-select-method
15922                      (setq method (gnus-server-get-method nil method))))
15923                (not (gnus-secondary-method-p method)))
15924           ;; These groups are foreign.  Check the level.
15925           (when (<= (gnus-info-level info) foreign-level)
15926             (setq active (gnus-activate-group group 'scan))
15927             (unless (inline (gnus-virtual-group-p group))
15928               (inline (gnus-close-group group)))
15929             (when (fboundp (intern (concat (symbol-name (car method))
15930                                            "-request-update-info")))
15931               (inline (gnus-request-update-info info method))))
15932         ;; These groups are native or secondary.
15933         (when (and (<= (gnus-info-level info) level)
15934                    (not gnus-read-active-file))
15935           (setq active (gnus-activate-group group 'scan))
15936           (inline (gnus-close-group group))))
15937
15938       ;; Get the number of unread articles in the group.
15939       (if active
15940           (inline (gnus-get-unread-articles-in-group info active))
15941         ;; The group couldn't be reached, so we nix out the number of
15942         ;; unread articles and stuff.
15943         (gnus-set-active group nil)
15944         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15945
15946     (gnus-message 5 "Checking new news...done")))
15947
15948 ;; Create a hash table out of the newsrc alist.  The `car's of the
15949 ;; alist elements are used as keys.
15950 (defun gnus-make-hashtable-from-newsrc-alist ()
15951   (let ((alist gnus-newsrc-alist)
15952         (ohashtb gnus-newsrc-hashtb)
15953         prev)
15954     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15955     (setq alist
15956           (setq prev (setq gnus-newsrc-alist
15957                            (if (equal (caar gnus-newsrc-alist)
15958                                       "dummy.group")
15959                                gnus-newsrc-alist
15960                              (cons (list "dummy.group" 0 nil) alist)))))
15961     (while alist
15962       (gnus-sethash
15963        (caar alist)
15964        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15965              prev)
15966        gnus-newsrc-hashtb)
15967       (setq prev alist
15968             alist (cdr alist)))))
15969
15970 (defun gnus-make-hashtable-from-killed ()
15971   "Create a hash table from the killed and zombie lists."
15972   (let ((lists '(gnus-killed-list gnus-zombie-list))
15973         list)
15974     (setq gnus-killed-hashtb
15975           (gnus-make-hashtable
15976            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15977     (while (setq list (pop lists))
15978       (setq list (symbol-value list))
15979       (while list
15980         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15981
15982 (defun gnus-activate-group (group &optional scan dont-check)
15983   ;; Check whether a group has been activated or not.
15984   ;; If SCAN, request a scan of that group as well.
15985   (let ((method (gnus-find-method-for-group group))
15986         active)
15987     (and (gnus-check-server method)
15988          ;; We escape all bugs and quit here to make it possible to
15989          ;; continue if a group is so out-there that it reports bugs
15990          ;; and stuff.
15991          (progn
15992            (and scan
15993                 (gnus-check-backend-function 'request-scan (car method))
15994                 (gnus-request-scan group method))
15995            t)
15996          (condition-case ()
15997              (gnus-request-group group dont-check)
15998         ;   (error nil)
15999            (quit nil))
16000          (save-excursion
16001            (set-buffer nntp-server-buffer)
16002            (goto-char (point-min))
16003            ;; Parse the result we got from `gnus-request-group'.
16004            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16005                 (progn
16006                   (goto-char (match-beginning 1))
16007                   (gnus-set-active
16008                    group (setq active (cons (read (current-buffer))
16009                                             (read (current-buffer)))))
16010                   ;; Return the new active info.
16011                   active))))))
16012
16013 (defun gnus-update-read-articles (group unread)
16014   "Update the list of read and ticked articles in GROUP using the
16015 UNREAD and TICKED lists.
16016 Note: UNSELECTED has to be sorted over `<'.
16017 Returns whether the updating was successful."
16018   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16019          (entry (gnus-gethash group gnus-newsrc-hashtb))
16020          (info (nth 2 entry))
16021          (prev 1)
16022          (unread (sort (copy-sequence unread) '<))
16023          read)
16024     (if (or (not info) (not active))
16025         ;; There is no info on this group if it was, in fact,
16026         ;; killed.  Gnus stores no information on killed groups, so
16027         ;; there's nothing to be done.
16028         ;; One could store the information somewhere temporarily,
16029         ;; perhaps...  Hmmm...
16030         ()
16031       ;; Remove any negative articles numbers.
16032       (while (and unread (< (car unread) 0))
16033         (setq unread (cdr unread)))
16034       ;; Remove any expired article numbers
16035       (while (and unread (< (car unread) (car active)))
16036         (setq unread (cdr unread)))
16037       ;; Compute the ranges of read articles by looking at the list of
16038       ;; unread articles.
16039       (while unread
16040         (if (/= (car unread) prev)
16041             (setq read (cons (if (= prev (1- (car unread))) prev
16042                                (cons prev (1- (car unread)))) read)))
16043         (setq prev (1+ (car unread)))
16044         (setq unread (cdr unread)))
16045       (when (<= prev (cdr active))
16046         (setq read (cons (cons prev (cdr active)) read)))
16047       ;; Enter this list into the group info.
16048       (gnus-info-set-read
16049        info (if (> (length read) 1) (nreverse read) read))
16050       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16051       (gnus-get-unread-articles-in-group info (gnus-active group))
16052       t)))
16053
16054 (defun gnus-make-articles-unread (group articles)
16055   "Mark ARTICLES in GROUP as unread."
16056   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16057                           (gnus-gethash (gnus-group-real-name group)
16058                                         gnus-newsrc-hashtb))))
16059          (ranges (gnus-info-read info))
16060          news article)
16061     (while articles
16062       (when (gnus-member-of-range
16063              (setq article (pop articles)) ranges)
16064         (setq news (cons article news))))
16065     (when news
16066       (gnus-info-set-read
16067        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16068       (gnus-group-update-group group t))))
16069
16070 ;; Enter all dead groups into the hashtb.
16071 (defun gnus-update-active-hashtb-from-killed ()
16072   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16073         (lists (list gnus-killed-list gnus-zombie-list))
16074         killed)
16075     (while lists
16076       (setq killed (car lists))
16077       (while killed
16078         (gnus-sethash (car killed) nil hashtb)
16079         (setq killed (cdr killed)))
16080       (setq lists (cdr lists)))))
16081
16082 (defun gnus-get-killed-groups ()
16083   "Go through the active hashtb and all all unknown groups as killed."
16084   ;; First make sure active file has been read.
16085   (unless (gnus-read-active-file-p)
16086     (let ((gnus-read-active-file t))
16087       (gnus-read-active-file)))
16088   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16089   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16090   (mapatoms
16091    (lambda (sym)
16092      (let ((groups 0)
16093            (group (symbol-name sym)))
16094        (if (or (null group)
16095                (gnus-gethash group gnus-killed-hashtb)
16096                (gnus-gethash group gnus-newsrc-hashtb))
16097            ()
16098          (let ((do-sub (gnus-matches-options-n group)))
16099            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16100                ()
16101              (setq groups (1+ groups))
16102              (setq gnus-killed-list
16103                    (cons group gnus-killed-list))
16104              (gnus-sethash group group gnus-killed-hashtb))))))
16105    gnus-active-hashtb))
16106
16107 ;; Get the active file(s) from the backend(s).
16108 (defun gnus-read-active-file ()
16109   (gnus-group-set-mode-line)
16110   (let ((methods 
16111          (append
16112           (if (gnus-check-server gnus-select-method)
16113               ;; The native server is available.
16114               (cons gnus-select-method gnus-secondary-select-methods)
16115             ;; The native server is down, so we just do the
16116             ;; secondary ones.
16117             gnus-secondary-select-methods)
16118           ;; Also read from the archive server.
16119           (when gnus-message-archive-method
16120             (list "archive"))))
16121         list-type)
16122     (setq gnus-have-read-active-file nil)
16123     (save-excursion
16124       (set-buffer nntp-server-buffer)
16125       (while methods
16126         (let* ((method (if (stringp (car methods))
16127                            (gnus-server-get-method nil (car methods))
16128                          (car methods)))
16129                (where (nth 1 method))
16130                (mesg (format "Reading active file%s via %s..."
16131                              (if (and where (not (zerop (length where))))
16132                                  (concat " from " where) "")
16133                              (car method))))
16134           (gnus-message 5 mesg)
16135           (when (gnus-check-server method)
16136             ;; Request that the backend scan its incoming messages.
16137             (and (gnus-check-backend-function 'request-scan (car method))
16138                  (gnus-request-scan nil method))
16139             (cond
16140              ((and (eq gnus-read-active-file 'some)
16141                    (gnus-check-backend-function 'retrieve-groups (car method)))
16142               (let ((newsrc (cdr gnus-newsrc-alist))
16143                     (gmethod (gnus-server-get-method nil method))
16144                     groups info)
16145                 (while (setq info (pop newsrc))
16146                   (when (gnus-server-equal
16147                          (gnus-find-method-for-group 
16148                           (gnus-info-group info) info)
16149                          gmethod)
16150                     (push (gnus-group-real-name (gnus-info-group info)) 
16151                           groups)))
16152                 (when groups
16153                   (gnus-check-server method)
16154                   (setq list-type (gnus-retrieve-groups groups method))
16155                   (cond
16156                    ((not list-type)
16157                     (gnus-error
16158                      1.2 "Cannot read partial active file from %s server."
16159                      (car method)))
16160                    ((eq list-type 'active)
16161                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16162                    (t
16163                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16164              (t
16165               (if (not (gnus-request-list method))
16166                   (unless (equal method gnus-message-archive-method)
16167                     (gnus-error 1 "Cannot read active file from %s server."
16168                                 (car method)))
16169                 (gnus-message 5 mesg)
16170                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16171                 ;; We mark this active file as read.
16172                 (push method gnus-have-read-active-file)
16173                 (gnus-message 5 "%sdone" mesg))))))
16174         (setq methods (cdr methods))))))
16175
16176 ;; Read an active file and place the results in `gnus-active-hashtb'.
16177 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16178   (unless method
16179     (setq method gnus-select-method))
16180   (let ((cur (current-buffer))
16181         (hashtb (or hashtb
16182                     (if (and gnus-active-hashtb
16183                              (not (equal method gnus-select-method)))
16184                         gnus-active-hashtb
16185                       (setq gnus-active-hashtb
16186                             (if (equal method gnus-select-method)
16187                                 (gnus-make-hashtable
16188                                  (count-lines (point-min) (point-max)))
16189                               (gnus-make-hashtable 4096)))))))
16190     ;; Delete unnecessary lines.
16191     (goto-char (point-min))
16192     (while (search-forward "\nto." nil t)
16193       (delete-region (1+ (match-beginning 0))
16194                      (progn (forward-line 1) (point))))
16195     (or (string= gnus-ignored-newsgroups "")
16196         (progn
16197           (goto-char (point-min))
16198           (delete-matching-lines gnus-ignored-newsgroups)))
16199     ;; Make the group names readable as a lisp expression even if they
16200     ;; contain special characters.
16201     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16202     (goto-char (point-max))
16203     (while (re-search-backward "[][';?()#]" nil t)
16204       (insert ?\\))
16205     ;; If these are groups from a foreign select method, we insert the
16206     ;; group prefix in front of the group names.
16207     (and method (not (gnus-server-equal
16208                       (gnus-server-get-method nil method)
16209                       (gnus-server-get-method nil gnus-select-method)))
16210          (let ((prefix (gnus-group-prefixed-name "" method)))
16211            (goto-char (point-min))
16212            (while (and (not (eobp))
16213                        (progn (insert prefix)
16214                               (zerop (forward-line 1)))))))
16215     ;; Store the active file in a hash table.
16216     (goto-char (point-min))
16217     (if (string-match "%[oO]" gnus-group-line-format)
16218         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16219         ;; If we want information on moderated groups, we use this
16220         ;; loop...
16221         (let* ((mod-hashtb (make-vector 7 0))
16222                (m (intern "m" mod-hashtb))
16223                group max min)
16224           (while (not (eobp))
16225             (condition-case nil
16226                 (progn
16227                   (narrow-to-region (point) (gnus-point-at-eol))
16228                   (setq group (let ((obarray hashtb)) (read cur)))
16229                   (if (and (numberp (setq max (read cur)))
16230                            (numberp (setq min (read cur)))
16231                            (progn
16232                              (skip-chars-forward " \t")
16233                              (not
16234                               (or (= (following-char) ?=)
16235                                   (= (following-char) ?x)
16236                                   (= (following-char) ?j)))))
16237                       (set group (cons min max))
16238                     (set group nil))
16239                   ;; Enter moderated groups into a list.
16240                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16241                       (setq gnus-moderated-list
16242                             (cons (symbol-name group) gnus-moderated-list))))
16243               (error
16244                (and group
16245                     (symbolp group)
16246                     (set group nil))))
16247             (widen)
16248             (forward-line 1)))
16249       ;; And if we do not care about moderation, we use this loop,
16250       ;; which is faster.
16251       (let (group max min)
16252         (while (not (eobp))
16253           (condition-case ()
16254               (progn
16255                 (narrow-to-region (point) (gnus-point-at-eol))
16256                 ;; group gets set to a symbol interned in the hash table
16257                 ;; (what a hack!!) - jwz
16258                 (setq group (let ((obarray hashtb)) (read cur)))
16259                 (if (and (numberp (setq max (read cur)))
16260                          (numberp (setq min (read cur)))
16261                          (progn
16262                            (skip-chars-forward " \t")
16263                            (not
16264                             (or (= (following-char) ?=)
16265                                 (= (following-char) ?x)
16266                                 (= (following-char) ?j)))))
16267                     (set group (cons min max))
16268                   (set group nil)))
16269             (error
16270              (progn
16271                (and group
16272                     (symbolp group)
16273                     (set group nil))
16274                (or ignore-errors
16275                    (gnus-message 3 "Warning - illegal active: %s"
16276                                  (buffer-substring
16277                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16278           (widen)
16279           (forward-line 1))))))
16280
16281 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16282   ;; Parse a "groups" active file.
16283   (let ((cur (current-buffer))
16284         (hashtb (or hashtb
16285                     (if (and method gnus-active-hashtb)
16286                         gnus-active-hashtb
16287                       (setq gnus-active-hashtb
16288                             (gnus-make-hashtable
16289                              (count-lines (point-min) (point-max)))))))
16290         (prefix (and method
16291                      (not (gnus-server-equal
16292                            (gnus-server-get-method nil method)
16293                            (gnus-server-get-method nil gnus-select-method)))
16294                      (gnus-group-prefixed-name "" method))))
16295
16296     (goto-char (point-min))
16297     ;; We split this into to separate loops, one with the prefix
16298     ;; and one without to speed the reading up somewhat.
16299     (if prefix
16300         (let (min max opoint group)
16301           (while (not (eobp))
16302             (condition-case ()
16303                 (progn
16304                   (read cur) (read cur)
16305                   (setq min (read cur)
16306                         max (read cur)
16307                         opoint (point))
16308                   (skip-chars-forward " \t")
16309                   (insert prefix)
16310                   (goto-char opoint)
16311                   (set (let ((obarray hashtb)) (read cur))
16312                        (cons min max)))
16313               (error (and group (symbolp group) (set group nil))))
16314             (forward-line 1)))
16315       (let (min max group)
16316         (while (not (eobp))
16317           (condition-case ()
16318               (if (= (following-char) ?2)
16319                   (progn
16320                     (read cur) (read cur)
16321                     (setq min (read cur)
16322                           max (read cur))
16323                     (set (setq group (let ((obarray hashtb)) (read cur)))
16324                          (cons min max))))
16325             (error (and group (symbolp group) (set group nil))))
16326           (forward-line 1))))))
16327
16328 (defun gnus-read-newsrc-file (&optional force)
16329   "Read startup file.
16330 If FORCE is non-nil, the .newsrc file is read."
16331   ;; Reset variables that might be defined in the .newsrc.eld file.
16332   (let ((variables gnus-variable-list))
16333     (while variables
16334       (set (car variables) nil)
16335       (setq variables (cdr variables))))
16336   (let* ((newsrc-file gnus-current-startup-file)
16337          (quick-file (concat newsrc-file ".el")))
16338     (save-excursion
16339       ;; We always load the .newsrc.eld file.  If always contains
16340       ;; much information that can not be gotten from the .newsrc
16341       ;; file (ticked articles, killed groups, foreign methods, etc.)
16342       (gnus-read-newsrc-el-file quick-file)
16343
16344       (if (and (file-exists-p gnus-current-startup-file)
16345                (or force
16346                    (and (file-newer-than-file-p newsrc-file quick-file)
16347                         (file-newer-than-file-p newsrc-file
16348                                                 (concat quick-file "d")))
16349                    (not gnus-newsrc-alist)))
16350           ;; We read the .newsrc file.  Note that if there if a
16351           ;; .newsrc.eld file exists, it has already been read, and
16352           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16353           ;; the .newsrc file, Gnus will only use the information it
16354           ;; can find there for changing the data already read -
16355           ;; ie. reading the .newsrc file will not trash the data
16356           ;; already read (except for read articles).
16357           (save-excursion
16358             (gnus-message 5 "Reading %s..." newsrc-file)
16359             (set-buffer (find-file-noselect newsrc-file))
16360             (buffer-disable-undo (current-buffer))
16361             (gnus-newsrc-to-gnus-format)
16362             (kill-buffer (current-buffer))
16363             (gnus-message 5 "Reading %s...done" newsrc-file)))
16364
16365       ;; Read any slave files.
16366       (unless gnus-slave
16367         (gnus-master-read-slave-newsrc))
16368       
16369       ;; Convert old to new.
16370       (gnus-convert-old-newsrc))))
16371
16372 (defun gnus-continuum-version (version)
16373   "Return VERSION as a floating point number."
16374   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16375             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16376     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16377            (number (match-string 2 version))
16378            major minor least)
16379       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16380       (setq major (string-to-number (match-string 1 number)))
16381       (setq minor (string-to-number (match-string 2 number)))
16382       (setq least (if (match-beginning 3)
16383                       (string-to-number (match-string 3 number))
16384                     0))
16385       (string-to-number
16386        (if (zerop major)
16387            (format "%s00%02d%02d"
16388                    (cond 
16389                     ((member alpha '("(ding)" "d")) "4.99")
16390                     ((member alpha '("September" "s")) "5.01")
16391                     ((member alpha '("Red" "r")) "5.03"))
16392                    minor least)
16393          (format "%d.%02d%02d" major minor least))))))
16394
16395 (defun gnus-convert-old-newsrc ()
16396   "Convert old newsrc into the new format, if needed."
16397   (let ((fcv (and gnus-newsrc-file-version
16398                   (gnus-continuum-version gnus-newsrc-file-version))))
16399     (cond
16400      ;; No .newsrc.eld file was loaded.
16401      ((null fcv) nil)
16402      ;; Gnus 5 .newsrc.eld was loaded.
16403      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16404       (gnus-convert-old-ticks)))))
16405
16406 (defun gnus-convert-old-ticks ()
16407   (let ((newsrc (cdr gnus-newsrc-alist))
16408         marks info dormant ticked)
16409     (while (setq info (pop newsrc))
16410       (when (setq marks (gnus-info-marks info))
16411         (setq dormant (cdr (assq 'dormant marks))
16412               ticked (cdr (assq 'tick marks)))
16413         (when (or dormant ticked)
16414           (gnus-info-set-read
16415            info
16416            (gnus-add-to-range
16417             (gnus-info-read info)
16418             (nconc (gnus-uncompress-range dormant)
16419                    (gnus-uncompress-range ticked)))))))))
16420
16421 (defun gnus-read-newsrc-el-file (file)
16422   (let ((ding-file (concat file "d")))
16423     ;; We always, always read the .eld file.
16424     (gnus-message 5 "Reading %s..." ding-file)
16425     (let (gnus-newsrc-assoc)
16426       (condition-case nil
16427           (load ding-file t t t)
16428         (error
16429          (gnus-error 1 "Error in %s" ding-file)))
16430       (when gnus-newsrc-assoc
16431         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16432     (gnus-make-hashtable-from-newsrc-alist)
16433     (when (file-newer-than-file-p file ding-file)
16434       ;; Old format quick file
16435       (gnus-message 5 "Reading %s..." file)
16436       ;; The .el file is newer than the .eld file, so we read that one
16437       ;; as well.
16438       (gnus-read-old-newsrc-el-file file))))
16439
16440 ;; Parse the old-style quick startup file
16441 (defun gnus-read-old-newsrc-el-file (file)
16442   (let (newsrc killed marked group m info)
16443     (prog1
16444         (let ((gnus-killed-assoc nil)
16445               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16446           (prog1
16447               (condition-case nil
16448                   (load file t t t)
16449                 (error nil))
16450             (setq newsrc gnus-newsrc-assoc
16451                   killed gnus-killed-assoc
16452                   marked gnus-marked-assoc)))
16453       (setq gnus-newsrc-alist nil)
16454       (while (setq group (pop newsrc))
16455         (if (setq info (gnus-get-info (car group)))
16456             (progn
16457               (gnus-info-set-read info (cddr group))
16458               (gnus-info-set-level
16459                info (if (nth 1 group) gnus-level-default-subscribed
16460                       gnus-level-default-unsubscribed))
16461               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16462           (push (setq info
16463                       (list (car group)
16464                             (if (nth 1 group) gnus-level-default-subscribed
16465                               gnus-level-default-unsubscribed)
16466                             (cddr group)))
16467                 gnus-newsrc-alist))
16468         ;; Copy marks into info.
16469         (when (setq m (assoc (car group) marked))
16470           (unless (nthcdr 3 info)
16471             (nconc info (list nil)))
16472           (gnus-info-set-marks
16473            info (list (cons 'tick (gnus-compress-sequence 
16474                                    (sort (cdr m) '<) t))))))
16475       (setq newsrc killed)
16476       (while newsrc
16477         (setcar newsrc (caar newsrc))
16478         (setq newsrc (cdr newsrc)))
16479       (setq gnus-killed-list killed))
16480     ;; The .el file version of this variable does not begin with
16481     ;; "options", while the .eld version does, so we just add it if it
16482     ;; isn't there.
16483     (and
16484      gnus-newsrc-options
16485      (progn
16486        (and (not (string-match "^ *options" gnus-newsrc-options))
16487             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16488        (and (not (string-match "\n$" gnus-newsrc-options))
16489             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16490        ;; Finally, if we read some options lines, we parse them.
16491        (or (string= gnus-newsrc-options "")
16492            (gnus-newsrc-parse-options gnus-newsrc-options))))
16493
16494     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16495     (gnus-make-hashtable-from-newsrc-alist)))
16496
16497 (defun gnus-make-newsrc-file (file)
16498   "Make server dependent file name by catenating FILE and server host name."
16499   (let* ((file (expand-file-name file nil))
16500          (real-file (concat file "-" (nth 1 gnus-select-method))))
16501     (if (or (file-exists-p real-file)
16502             (file-exists-p (concat real-file ".el"))
16503             (file-exists-p (concat real-file ".eld")))
16504         real-file file)))
16505
16506 (defun gnus-newsrc-to-gnus-format ()
16507   (setq gnus-newsrc-options "")
16508   (setq gnus-newsrc-options-n nil)
16509
16510   (or gnus-active-hashtb
16511       (setq gnus-active-hashtb (make-vector 4095 0)))
16512   (let ((buf (current-buffer))
16513         (already-read (> (length gnus-newsrc-alist) 1))
16514         group subscribed options-symbol newsrc Options-symbol
16515         symbol reads num1)
16516     (goto-char (point-min))
16517     ;; We intern the symbol `options' in the active hashtb so that we
16518     ;; can `eq' against it later.
16519     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16520     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16521
16522     (while (not (eobp))
16523       ;; We first read the first word on the line by narrowing and
16524       ;; then reading into `gnus-active-hashtb'.  Most groups will
16525       ;; already exist in that hashtb, so this will save some string
16526       ;; space.
16527       (narrow-to-region
16528        (point)
16529        (progn (skip-chars-forward "^ \t!:\n") (point)))
16530       (goto-char (point-min))
16531       (setq symbol
16532             (and (/= (point-min) (point-max))
16533                  (let ((obarray gnus-active-hashtb)) (read buf))))
16534       (widen)
16535       ;; Now, the symbol we have read is either `options' or a group
16536       ;; name.  If it is an options line, we just add it to a string.
16537       (cond
16538        ((or (eq symbol options-symbol)
16539             (eq symbol Options-symbol))
16540         (setq gnus-newsrc-options
16541               ;; This concating is quite inefficient, but since our
16542               ;; thorough studies show that approx 99.37% of all
16543               ;; .newsrc files only contain a single options line, we
16544               ;; don't give a damn, frankly, my dear.
16545               (concat gnus-newsrc-options
16546                       (buffer-substring
16547                        (gnus-point-at-bol)
16548                        ;; Options may continue on the next line.
16549                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16550                                 (progn (beginning-of-line) (point)))
16551                            (point)))))
16552         (forward-line -1))
16553        (symbol
16554         ;; Group names can be just numbers.  
16555         (when (numberp symbol) 
16556           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16557         (or (boundp symbol) (set symbol nil))
16558         ;; It was a group name.
16559         (setq subscribed (= (following-char) ?:)
16560               group (symbol-name symbol)
16561               reads nil)
16562         (if (eolp)
16563             ;; If the line ends here, this is clearly a buggy line, so
16564             ;; we put point a the beginning of line and let the cond
16565             ;; below do the error handling.
16566             (beginning-of-line)
16567           ;; We skip to the beginning of the ranges.
16568           (skip-chars-forward "!: \t"))
16569         ;; We are now at the beginning of the list of read articles.
16570         ;; We read them range by range.
16571         (while
16572             (cond
16573              ((looking-at "[0-9]+")
16574               ;; We narrow and read a number instead of buffer-substring/
16575               ;; string-to-int because it's faster.  narrow/widen is
16576               ;; faster than save-restriction/narrow, and save-restriction
16577               ;; produces a garbage object.
16578               (setq num1 (progn
16579                            (narrow-to-region (match-beginning 0) (match-end 0))
16580                            (read buf)))
16581               (widen)
16582               ;; If the next character is a dash, then this is a range.
16583               (if (= (following-char) ?-)
16584                   (progn
16585                     ;; We read the upper bound of the range.
16586                     (forward-char 1)
16587                     (if (not (looking-at "[0-9]+"))
16588                         ;; This is a buggy line, by we pretend that
16589                         ;; it's kinda OK.  Perhaps the user should be
16590                         ;; dinged?
16591                         (setq reads (cons num1 reads))
16592                       (setq reads
16593                             (cons
16594                              (cons num1
16595                                    (progn
16596                                      (narrow-to-region (match-beginning 0)
16597                                                        (match-end 0))
16598                                      (read buf)))
16599                              reads))
16600                       (widen)))
16601                 ;; It was just a simple number, so we add it to the
16602                 ;; list of ranges.
16603                 (setq reads (cons num1 reads)))
16604               ;; If the next char in ?\n, then we have reached the end
16605               ;; of the line and return nil.
16606               (/= (following-char) ?\n))
16607              ((= (following-char) ?\n)
16608               ;; End of line, so we end.
16609               nil)
16610              (t
16611               ;; Not numbers and not eol, so this might be a buggy
16612               ;; line...
16613               (or (eobp)
16614                   ;; If it was eob instead of ?\n, we allow it.
16615                   (progn
16616                     ;; The line was buggy.
16617                     (setq group nil)
16618                     (gnus-error 3.1 "Mangled line: %s"
16619                                 (buffer-substring (gnus-point-at-bol)
16620                                                   (gnus-point-at-eol)))))
16621               nil))
16622           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16623           ;; we allow them, because it's a common mistake to put a
16624           ;; space after the comma.
16625           (skip-chars-forward ", "))
16626
16627         ;; We have already read .newsrc.eld, so we gently update the
16628         ;; data in the hash table with the information we have just
16629         ;; read.
16630         (when group
16631           (let ((info (gnus-get-info group))
16632                 level)
16633             (if info
16634                 ;; There is an entry for this file in the alist.
16635                 (progn
16636                   (gnus-info-set-read info (nreverse reads))
16637                   ;; We update the level very gently.  In fact, we
16638                   ;; only change it if there's been a status change
16639                   ;; from subscribed to unsubscribed, or vice versa.
16640                   (setq level (gnus-info-level info))
16641                   (cond ((and (<= level gnus-level-subscribed)
16642                               (not subscribed))
16643                          (setq level (if reads
16644                                          gnus-level-default-unsubscribed
16645                                        (1+ gnus-level-default-unsubscribed))))
16646                         ((and (> level gnus-level-subscribed) subscribed)
16647                          (setq level gnus-level-default-subscribed)))
16648                   (gnus-info-set-level info level))
16649               ;; This is a new group.
16650               (setq info (list group
16651                                (if subscribed
16652                                    gnus-level-default-subscribed
16653                                  (if reads
16654                                      (1+ gnus-level-subscribed)
16655                                    gnus-level-default-unsubscribed))
16656                                (nreverse reads))))
16657             (setq newsrc (cons info newsrc))))))
16658       (forward-line 1))
16659
16660     (setq newsrc (nreverse newsrc))
16661
16662     (if (not already-read)
16663         ()
16664       ;; We now have two newsrc lists - `newsrc', which is what we
16665       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16666       ;; what we've read from .newsrc.eld.  We have to merge these
16667       ;; lists.  We do this by "attaching" any (foreign) groups in the
16668       ;; gnus-newsrc-alist to the (native) group that precedes them.
16669       (let ((rc (cdr gnus-newsrc-alist))
16670             (prev gnus-newsrc-alist)
16671             entry mentry)
16672         (while rc
16673           (or (null (nth 4 (car rc)))   ; It's a native group.
16674               (assoc (caar rc) newsrc) ; It's already in the alist.
16675               (if (setq entry (assoc (caar prev) newsrc))
16676                   (setcdr (setq mentry (memq entry newsrc))
16677                           (cons (car rc) (cdr mentry)))
16678                 (setq newsrc (cons (car rc) newsrc))))
16679           (setq prev rc
16680                 rc (cdr rc)))))
16681
16682     (setq gnus-newsrc-alist newsrc)
16683     ;; We make the newsrc hashtb.
16684     (gnus-make-hashtable-from-newsrc-alist)
16685
16686     ;; Finally, if we read some options lines, we parse them.
16687     (or (string= gnus-newsrc-options "")
16688         (gnus-newsrc-parse-options gnus-newsrc-options))))
16689
16690 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16691 ;; The return value will be a list on the form
16692 ;; ((regexp1 . ignore)
16693 ;;  (regexp2 . subscribe)...)
16694 ;; When handling new newsgroups, groups that match a `ignore' regexp
16695 ;; will be ignored, and groups that match a `subscribe' regexp will be
16696 ;; subscribed.  A line like
16697 ;; options -n !all rec.all
16698 ;; will lead to a list that looks like
16699 ;; (("^rec\\..+" . subscribe)
16700 ;;  ("^.+" . ignore))
16701 ;; So all "rec.*" groups will be subscribed, while all the other
16702 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16703 ;; different from "options -n rec.all !all".
16704 (defun gnus-newsrc-parse-options (options)
16705   (let (out eol)
16706     (save-excursion
16707       (gnus-set-work-buffer)
16708       (insert (regexp-quote options))
16709       ;; First we treat all continuation lines.
16710       (goto-char (point-min))
16711       (while (re-search-forward "\n[ \t]+" nil t)
16712         (replace-match " " t t))
16713       ;; Then we transform all "all"s into ".+"s.
16714       (goto-char (point-min))
16715       (while (re-search-forward "\\ball\\b" nil t)
16716         (replace-match ".+" t t))
16717       (goto-char (point-min))
16718       ;; We remove all other options than the "-n" ones.
16719       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16720         (replace-match " ")
16721         (forward-char -1))
16722       (goto-char (point-min))
16723
16724       ;; We are only interested in "options -n" lines - we
16725       ;; ignore the other option lines.
16726       (while (re-search-forward "[ \t]-n" nil t)
16727         (setq eol
16728               (or (save-excursion
16729                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16730                          (- (point) 2)))
16731                   (gnus-point-at-eol)))
16732         ;; Search for all "words"...
16733         (while (re-search-forward "[^ \t,\n]+" eol t)
16734           (if (= (char-after (match-beginning 0)) ?!)
16735               ;; If the word begins with a bang (!), this is a "not"
16736               ;; spec.  We put this spec (minus the bang) and the
16737               ;; symbol `ignore' into the list.
16738               (setq out (cons (cons (concat
16739                                      "^" (buffer-substring
16740                                           (1+ (match-beginning 0))
16741                                           (match-end 0)))
16742                                     'ignore) out))
16743             ;; There was no bang, so this is a "yes" spec.
16744             (setq out (cons (cons (concat "^" (match-string 0))
16745                                   'subscribe) out)))))
16746
16747       (setq gnus-newsrc-options-n out))))
16748
16749 (defun gnus-save-newsrc-file (&optional force)
16750   "Save .newsrc file."
16751   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16752   ;; from the variable gnus-newsrc-alist.
16753   (when (and (or gnus-newsrc-alist gnus-killed-list)
16754              gnus-current-startup-file)
16755     (save-excursion
16756       (if (and (or gnus-use-dribble-file gnus-slave)
16757                (not force)
16758                (or (not gnus-dribble-buffer)
16759                    (not (buffer-name gnus-dribble-buffer))
16760                    (zerop (save-excursion
16761                             (set-buffer gnus-dribble-buffer)
16762                             (buffer-size)))))
16763           (gnus-message 4 "(No changes need to be saved)")
16764         (run-hooks 'gnus-save-newsrc-hook)
16765         (if gnus-slave
16766             (gnus-slave-save-newsrc)
16767           ;; Save .newsrc.
16768           (when gnus-save-newsrc-file
16769             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16770             (gnus-gnus-to-newsrc-format)
16771             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16772           ;; Save .newsrc.eld.
16773           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16774           (make-local-variable 'version-control)
16775           (setq version-control 'never)
16776           (setq buffer-file-name
16777                 (concat gnus-current-startup-file ".eld"))
16778           (setq default-directory (file-name-directory buffer-file-name))
16779           (gnus-add-current-to-buffer-list)
16780           (buffer-disable-undo (current-buffer))
16781           (erase-buffer)
16782           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16783           (gnus-gnus-to-quick-newsrc-format)
16784           (run-hooks 'gnus-save-quick-newsrc-hook)
16785           (save-buffer)
16786           (kill-buffer (current-buffer))
16787           (gnus-message
16788            5 "Saving %s.eld...done" gnus-current-startup-file))
16789         (gnus-dribble-delete-file)
16790         (gnus-group-set-mode-line)))))
16791
16792 (defun gnus-gnus-to-quick-newsrc-format ()
16793   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16794   (insert ";; Gnus startup file.\n")
16795   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16796   (insert ";; to read .newsrc.\n")
16797   (insert "(setq gnus-newsrc-file-version "
16798           (prin1-to-string gnus-version) ")\n")
16799   (let ((variables
16800          (if gnus-save-killed-list gnus-variable-list
16801            ;; Remove the `gnus-killed-list' from the list of variables
16802            ;; to be saved, if required.
16803            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16804         ;; Peel off the "dummy" group.
16805         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16806         variable)
16807     ;; Insert the variables into the file.
16808     (while variables
16809       (when (and (boundp (setq variable (pop variables)))
16810                  (symbol-value variable))
16811         (insert "(setq " (symbol-name variable) " '")
16812         (prin1 (symbol-value variable) (current-buffer))
16813         (insert ")\n")))))
16814
16815 (defun gnus-gnus-to-newsrc-format ()
16816   ;; Generate and save the .newsrc file.
16817   (save-excursion
16818     (set-buffer (create-file-buffer gnus-current-startup-file))
16819     (let ((newsrc (cdr gnus-newsrc-alist))
16820           (standard-output (current-buffer))
16821           info ranges range method)
16822       (setq buffer-file-name gnus-current-startup-file)
16823       (setq default-directory (file-name-directory buffer-file-name))
16824       (buffer-disable-undo (current-buffer))
16825       (erase-buffer)
16826       ;; Write options.
16827       (if gnus-newsrc-options (insert gnus-newsrc-options))
16828       ;; Write subscribed and unsubscribed.
16829       (while (setq info (pop newsrc))
16830         ;; Don't write foreign groups to .newsrc.
16831         (when (or (null (setq method (gnus-info-method info)))
16832                   (equal method "native")
16833                   (gnus-server-equal method gnus-select-method))
16834           (insert (gnus-info-group info)
16835                   (if (> (gnus-info-level info) gnus-level-subscribed)
16836                       "!" ":"))
16837           (when (setq ranges (gnus-info-read info))
16838             (insert " ")
16839             (if (not (listp (cdr ranges)))
16840                 (if (= (car ranges) (cdr ranges))
16841                     (princ (car ranges))
16842                   (princ (car ranges))
16843                   (insert "-")
16844                   (princ (cdr ranges)))
16845               (while (setq range (pop ranges))
16846                 (if (or (atom range) (= (car range) (cdr range)))
16847                     (princ (or (and (atom range) range) (car range)))
16848                   (princ (car range))
16849                   (insert "-")
16850                   (princ (cdr range)))
16851                 (if ranges (insert ",")))))
16852           (insert "\n")))
16853       (make-local-variable 'version-control)
16854       (setq version-control 'never)
16855       ;; It has been reported that sometime the modtime on the .newsrc
16856       ;; file seems to be off.  We really do want to overwrite it, so
16857       ;; we clear the modtime here before saving.  It's a bit odd,
16858       ;; though...
16859       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16860       ;; delete the silly thing entirely first.  but this fails to provide
16861       ;; such niceties as .newsrc~ creation.
16862       (if gnus-modtime-botch
16863           (delete-file gnus-startup-file)
16864         (clear-visited-file-modtime))
16865       (run-hooks 'gnus-save-standard-newsrc-hook)
16866       (save-buffer)
16867       (kill-buffer (current-buffer)))))
16868
16869 \f
16870 ;;;
16871 ;;; Slave functions.
16872 ;;;
16873
16874 (defun gnus-slave-save-newsrc ()
16875   (save-excursion
16876     (set-buffer gnus-dribble-buffer)
16877     (let ((slave-name
16878            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16879       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16880
16881 (defun gnus-master-read-slave-newsrc ()
16882   (let ((slave-files
16883          (directory-files
16884           (file-name-directory gnus-current-startup-file)
16885           t (concat
16886              "^" (regexp-quote
16887                   (concat
16888                    (file-name-nondirectory gnus-current-startup-file)
16889                    "-slave-")))
16890           t))
16891         file)
16892     (if (not slave-files)
16893         ()                              ; There are no slave files to read.
16894       (gnus-message 7 "Reading slave newsrcs...")
16895       (save-excursion
16896         (set-buffer (get-buffer-create " *gnus slave*"))
16897         (buffer-disable-undo (current-buffer))
16898         (setq slave-files
16899               (sort (mapcar (lambda (file)
16900                               (list (nth 5 (file-attributes file)) file))
16901                             slave-files)
16902                     (lambda (f1 f2)
16903                       (or (< (caar f1) (caar f2))
16904                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16905         (while slave-files
16906           (erase-buffer)
16907           (setq file (nth 1 (car slave-files)))
16908           (insert-file-contents file)
16909           (if (condition-case ()
16910                   (progn
16911                     (eval-buffer (current-buffer))
16912                     t)
16913                 (error
16914                  (gnus-error 3.2 "Possible error in %s" file)
16915                  nil))
16916               (or gnus-slave ; Slaves shouldn't delete these files.
16917                   (condition-case ()
16918                       (delete-file file)
16919                     (error nil))))
16920           (setq slave-files (cdr slave-files))))
16921       (gnus-message 7 "Reading slave newsrcs...done"))))
16922
16923 \f
16924 ;;;
16925 ;;; Group description.
16926 ;;;
16927
16928 (defun gnus-read-all-descriptions-files ()
16929   (let ((methods (cons gnus-select-method 
16930                        (nconc
16931                         (when gnus-message-archive-method
16932                           (list "archive"))
16933                         gnus-secondary-select-methods))))
16934     (while methods
16935       (gnus-read-descriptions-file (car methods))
16936       (setq methods (cdr methods)))
16937     t))
16938
16939 (defun gnus-read-descriptions-file (&optional method)
16940   (let ((method (or method gnus-select-method)))
16941     (when (stringp method)
16942       (setq method (gnus-server-to-method method)))
16943     ;; We create the hashtable whether we manage to read the desc file
16944     ;; to avoid trying to re-read after a failed read.
16945     (or gnus-description-hashtb
16946         (setq gnus-description-hashtb
16947               (gnus-make-hashtable (length gnus-active-hashtb))))
16948     ;; Mark this method's desc file as read.
16949     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16950                   gnus-description-hashtb)
16951
16952     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16953     (cond
16954      ((not (gnus-check-server method))
16955       (gnus-message 1 "Couldn't open server")
16956       nil)
16957      ((not (gnus-request-list-newsgroups method))
16958       (gnus-message 1 "Couldn't read newsgroups descriptions")
16959       nil)
16960      (t
16961       (let (group)
16962         (save-excursion
16963           (save-restriction
16964             (set-buffer nntp-server-buffer)
16965             (goto-char (point-min))
16966             (if (or (search-forward "\n.\n" nil t)
16967                     (goto-char (point-max)))
16968                 (progn
16969                   (beginning-of-line)
16970                   (narrow-to-region (point-min) (point))))
16971             (goto-char (point-min))
16972             (while (not (eobp))
16973               ;; If we get an error, we set group to 0, which is not a
16974               ;; symbol...
16975               (setq group
16976                     (condition-case ()
16977                         (let ((obarray gnus-description-hashtb))
16978                           ;; Group is set to a symbol interned in this
16979                           ;; hash table.
16980                           (read nntp-server-buffer))
16981                       (error 0)))
16982               (skip-chars-forward " \t")
16983               ;; ...  which leads to this line being effectively ignored.
16984               (and (symbolp group)
16985                    (set group (buffer-substring
16986                                (point) (progn (end-of-line) (point)))))
16987               (forward-line 1))))
16988         (gnus-message 5 "Reading descriptions file...done")
16989         t)))))
16990
16991 (defun gnus-group-get-description (group)
16992   "Get the description of a group by sending XGTITLE to the server."
16993   (when (gnus-request-group-description group)
16994     (save-excursion
16995       (set-buffer nntp-server-buffer)
16996       (goto-char (point-min))
16997       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16998         (match-string 1)))))
16999
17000 \f
17001 ;;;
17002 ;;; Buffering of read articles.
17003 ;;;
17004
17005 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17006 (defvar gnus-backlog-articles nil)
17007 (defvar gnus-backlog-hashtb nil)
17008
17009 (defun gnus-backlog-buffer ()
17010   "Return the backlog buffer."
17011   (or (get-buffer gnus-backlog-buffer)
17012       (save-excursion
17013         (set-buffer (get-buffer-create gnus-backlog-buffer))
17014         (buffer-disable-undo (current-buffer))
17015         (setq buffer-read-only t)
17016         (gnus-add-current-to-buffer-list)
17017         (get-buffer gnus-backlog-buffer))))
17018
17019 (defun gnus-backlog-setup ()
17020   "Initialize backlog variables."
17021   (unless gnus-backlog-hashtb
17022     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17023
17024 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17025
17026 (defun gnus-backlog-shutdown ()
17027   "Clear all backlog variables and buffers."
17028   (when (get-buffer gnus-backlog-buffer)
17029     (kill-buffer gnus-backlog-buffer))
17030   (setq gnus-backlog-hashtb nil
17031         gnus-backlog-articles nil))
17032
17033 (defun gnus-backlog-enter-article (group number buffer)
17034   (gnus-backlog-setup)
17035   (let ((ident (intern (concat group ":" (int-to-string number))
17036                        gnus-backlog-hashtb))
17037         b)
17038     (if (memq ident gnus-backlog-articles)
17039         () ; It's already kept.
17040       ;; Remove the oldest article, if necessary.
17041       (and (numberp gnus-keep-backlog)
17042            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17043            (gnus-backlog-remove-oldest-article))
17044       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17045       ;; Insert the new article.
17046       (save-excursion
17047         (set-buffer (gnus-backlog-buffer))
17048         (let (buffer-read-only)
17049           (goto-char (point-max))
17050           (or (bolp) (insert "\n"))
17051           (setq b (point))
17052           (insert-buffer-substring buffer)
17053           ;; Tag the beginning of the article with the ident.
17054           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17055
17056 (defun gnus-backlog-remove-oldest-article ()
17057   (save-excursion
17058     (set-buffer (gnus-backlog-buffer))
17059     (goto-char (point-min))
17060     (if (zerop (buffer-size))
17061         () ; The buffer is empty.
17062       (let ((ident (get-text-property (point) 'gnus-backlog))
17063             buffer-read-only)
17064         ;; Remove the ident from the list of articles.
17065         (when ident
17066           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17067         ;; Delete the article itself.
17068         (delete-region
17069          (point) (next-single-property-change
17070                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17071
17072 (defun gnus-backlog-remove-article (group number)
17073   "Remove article NUMBER in GROUP from the backlog."
17074   (when (numberp number)
17075     (gnus-backlog-setup)
17076     (let ((ident (intern (concat group ":" (int-to-string number))
17077                          gnus-backlog-hashtb))
17078           beg end)
17079       (when (memq ident gnus-backlog-articles)
17080         ;; It was in the backlog.
17081         (save-excursion
17082           (set-buffer (gnus-backlog-buffer))
17083           (let (buffer-read-only)
17084             (when (setq beg (text-property-any
17085                              (point-min) (point-max) 'gnus-backlog
17086                              ident))
17087               ;; Find the end (i. e., the beginning of the next article).
17088               (setq end
17089                     (next-single-property-change
17090                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17091               (delete-region beg end)
17092               ;; Return success.
17093               t)))))))
17094
17095 (defun gnus-backlog-request-article (group number buffer)
17096   (when (numberp number)
17097     (gnus-backlog-setup)
17098     (let ((ident (intern (concat group ":" (int-to-string number))
17099                          gnus-backlog-hashtb))
17100           beg end)
17101       (when (memq ident gnus-backlog-articles)
17102         ;; It was in the backlog.
17103         (save-excursion
17104           (set-buffer (gnus-backlog-buffer))
17105           (if (not (setq beg (text-property-any
17106                               (point-min) (point-max) 'gnus-backlog
17107                               ident)))
17108               ;; It wasn't in the backlog after all.
17109               (ignore
17110                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17111             ;; Find the end (i. e., the beginning of the next article).
17112             (setq end
17113                   (next-single-property-change
17114                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17115         (let ((buffer-read-only nil))
17116           (erase-buffer)
17117           (insert-buffer-substring gnus-backlog-buffer beg end)
17118           t)))))
17119
17120 ;; Allow redefinition of Gnus functions.
17121
17122 (gnus-ems-redefine)
17123
17124 (provide 'gnus)
17125
17126 ;;; gnus.el ends here