*** 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 It's probably not a very effective to change this variable once you've
150 run Gnus once.  After doing that, you must edit this server from the
151 server buffer.")
152
153 (defvar gnus-refer-article-method nil
154   "*Preferred method for fetching an article by Message-ID.
155 If you are reading news from the local spool (with nnspool), fetching
156 articles by Message-ID is painfully slow.  By setting this method to an
157 nntp method, you might get acceptable results.
158
159 The value of this variable must be a valid select method as discussed
160 in the documentation of `gnus-select-method'.")
161
162 (defvar gnus-secondary-select-methods nil
163   "*A list of secondary methods that will be used for reading news.
164 This is a list where each element is a complete select method (see
165 `gnus-select-method').
166
167 If, for instance, you want to read your mail with the nnml backend,
168 you could set this variable:
169
170 (setq gnus-secondary-select-methods '((nnml \"\")))")
171
172 (defvar gnus-secondary-servers nil
173   "*List of NNTP servers that the user can choose between interactively.
174 To make Gnus query you for a server, you have to give `gnus' a
175 non-numeric prefix - `C-u M-x gnus', in short.")
176
177 (defvar gnus-nntp-server nil
178   "*The name of the host running the NNTP server.
179 This variable is semi-obsolete.  Use the `gnus-select-method'
180 variable instead.")
181
182 (defvar gnus-startup-file "~/.newsrc"
183   "*Your `.newsrc' file.
184 `.newsrc-SERVER' will be used instead if that exists.")
185
186 (defvar gnus-init-file "~/.gnus"
187   "*Your Gnus elisp startup file.
188 If a file with the .el or .elc suffixes exist, it will be read
189 instead.")
190
191 (defvar gnus-group-faq-directory
192   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
193     "/ftp@sunsite.auc.dk:/pub/usenet/"
194     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
195     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
196     "/ftp@rtfm.mit.edu:/pub/usenet/"
197     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
198     "/ftp@ftp.sunet.se:/pub/usenet/"
199     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
200     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
201     "/ftp@ftp.hk.super.net:/mirror/faqs/")
202   "*Directory where the group FAQs are stored.
203 This will most commonly be on a remote machine, and the file will be
204 fetched by ange-ftp.
205
206 This variable can also be a list of directories.  In that case, the
207 first element in the list will be used by default, and the others will
208 be used as backup sites.
209
210 Note that Gnus uses an aol machine as the default directory.  If this
211 feels fundamentally unclean, just think of it as a way to finally get
212 something of value back from them.
213
214 If the default site is too slow, try one of these:
215
216    North America: mirrors.aol.com                /pub/rtfm/usenet
217                   ftp.seas.gwu.edu               /pub/rtfm
218                   rtfm.mit.edu                   /pub/usenet
219    Europe:        ftp.uni-paderborn.de           /pub/FAQ
220                   src.doc.ic.ac.uk               /usenet/news-FAQS
221                   ftp.sunet.se                   /pub/usenet
222                   sunsite.auc.dk                 /pub/usenet
223    Asia:          nctuccca.edu.tw                /USENET/FAQ
224                   hwarang.postech.ac.kr          /pub/usenet
225                   ftp.hk.super.net               /mirror/faqs")
226
227 (defvar gnus-group-archive-directory
228   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
229   "*The address of the (ding) archives.")
230
231 (defvar gnus-group-recent-archive-directory
232   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
233   "*The address of the most recent (ding) articles.")
234
235 (defvar gnus-default-subscribed-newsgroups nil
236   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
237 It should be a list of strings.
238 If it is `t', Gnus will not do anything special the first time it is
239 started; it'll just use the normal newsgroups subscription methods.")
240
241 (defvar gnus-use-cross-reference t
242   "*Non-nil means that cross referenced articles will be marked as read.
243 If nil, ignore cross references.  If t, mark articles as read in
244 subscribed newsgroups.  If neither t nor nil, mark as read in all
245 newsgroups.")
246
247 (defvar gnus-single-article-buffer t
248   "*If non-nil, display all articles in the same buffer.
249 If nil, each group will get its own article buffer.")
250
251 (defvar gnus-use-dribble-file t
252   "*Non-nil means that Gnus will use a dribble file to store user updates.
253 If Emacs should crash without saving the .newsrc files, complete
254 information can be restored from the dribble file.")
255
256 (defvar gnus-dribble-directory nil
257   "*The directory where dribble files will be saved.
258 If this variable is nil, the directory where the .newsrc files are
259 saved will be used.")
260
261 (defvar gnus-asynchronous nil
262   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
263
264 (defvar gnus-kill-summary-on-exit t
265   "*If non-nil, kill the summary buffer when you exit from it.
266 If nil, the summary will become a \"*Dead Summary*\" buffer, and
267 it will be killed sometime later.")
268
269 (defvar gnus-large-newsgroup 200
270   "*The number of articles which indicates a large newsgroup.
271 If the number of articles in a newsgroup is greater than this value,
272 confirmation is required for selecting the newsgroup.")
273
274 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
275 (defvar gnus-no-groups-message "No news is horrible news"
276   "*Message displayed by Gnus when no groups are available.")
277
278 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
279   "*Non-nil means that the default name of a file to save articles in is the group name.
280 If it's nil, the directory form of the group name is used instead.
281
282 If this variable is a list, and the list contains the element
283 `not-score', long file names will not be used for score files; if it
284 contains the element `not-save', long file names will not be used for
285 saving; and if it contains the element `not-kill', long file names
286 will not be used for kill files.")
287
288 (defvar gnus-article-save-directory gnus-directory
289   "*Name of the directory articles will be saved in (default \"~/News\").")
290
291 (defvar gnus-kill-files-directory gnus-directory
292   "*Name of the directory where kill files will be stored (default \"~/News\").")
293
294 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
295   "*A function to save articles in your favorite format.
296 The function must be interactively callable (in other words, it must
297 be an Emacs command).
298
299 Gnus provides the following functions:
300
301 * gnus-summary-save-in-rmail (Rmail format)
302 * gnus-summary-save-in-mail (Unix mail format)
303 * gnus-summary-save-in-folder (MH folder)
304 * gnus-summary-save-in-file (article format).
305 * gnus-summary-save-in-vm (use VM's folder format).")
306
307 (defvar gnus-prompt-before-saving 'always
308   "*This variable says how much prompting is to be done when saving articles.
309 If it is nil, no prompting will be done, and the articles will be
310 saved to the default files.  If this variable is `always', each and
311 every article that is saved will be preceded by a prompt, even when
312 saving large batches of articles.  If this variable is neither nil not
313 `always', there the user will be prompted once for a file name for
314 each invocation of the saving commands.")
315
316 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
317   "*A function generating a file name to save articles in Rmail format.
318 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
319
320 (defvar gnus-mail-save-name (function gnus-plain-save-name)
321   "*A function generating a file name to save articles in Unix mail format.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
323
324 (defvar gnus-folder-save-name (function gnus-folder-save-name)
325   "*A function generating a file name to save articles in MH folder.
326 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
327
328 (defvar gnus-file-save-name (function gnus-numeric-save-name)
329   "*A function generating a file name to save articles in article format.
330 The function is called with NEWSGROUP, HEADERS, and optional
331 LAST-FILE.")
332
333 (defvar gnus-split-methods
334   '((gnus-article-archive-name))
335   "*Variable used to suggest where articles are to be saved.
336 For instance, if you would like to save articles related to Gnus in
337 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
338 you could set this variable to something like:
339
340  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
341    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
342
343 This variable is an alist where the where the key is the match and the
344 value is a list of possible files to save in if the match is non-nil.
345
346 If the match is a string, it is used as a regexp match on the
347 article.  If the match is a symbol, that symbol will be funcalled
348 from the buffer of the article to be saved with the newsgroup as the
349 parameter.  If it is a list, it will be evaled in the same buffer.
350
351 If this form or function returns a string, this string will be used as
352 a possible file name; and if it returns a non-nil list, that list will
353 be used as possible file names.")
354
355 (defvar gnus-move-split-methods nil
356   "*Variable used to suggest where articles are to be moved to.
357 It uses the same syntax as the `gnus-split-methods' variable.")
358
359 (defvar gnus-save-score nil
360   "*If non-nil, save group scoring info.")
361
362 (defvar gnus-use-adaptive-scoring nil
363   "*If non-nil, use some adaptive scoring scheme.")
364
365 (defvar gnus-use-cache 'passive
366   "*If nil, Gnus will ignore the article cache.
367 If `passive', it will allow entering (and reading) articles
368 explicitly entered into the cache.  If anything else, use the
369 cache to the full extent of the law.")
370
371 (defvar gnus-use-trees nil
372   "*If non-nil, display a thread tree buffer.")
373
374 (defvar gnus-use-grouplens nil
375   "*If non-nil, use GroupLens ratings.")
376
377 (defvar gnus-keep-backlog nil
378   "*If non-nil, Gnus will keep read articles for later re-retrieval.
379 If it is a number N, then Gnus will only keep the last N articles
380 read.  If it is neither nil nor a number, Gnus will keep all read
381 articles.  This is not a good idea.")
382
383 (defvar gnus-use-nocem nil
384   "*If non-nil, Gnus will read NoCeM cancel messages.")
385
386 (defvar gnus-use-demon nil
387   "If non-nil, Gnus might use some demons.")
388
389 (defvar gnus-use-scoring t
390   "*If non-nil, enable scoring.")
391
392 (defvar gnus-use-picons nil
393   "*If non-nil, display picons.")
394
395 (defvar gnus-fetch-old-headers nil
396   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
397 If an unread article in the group refers to an older, already read (or
398 just marked as read) article, the old article will not normally be
399 displayed in the Summary buffer.  If this variable is non-nil, Gnus
400 will attempt to grab the headers to the old articles, and thereby
401 build complete threads.  If it has the value `some', only enough
402 headers to connect otherwise loose threads will be displayed.
403 This variable can also be a number.  In that case, no more than that
404 number of old headers will be fetched.
405
406 The server has to support NOV for any of this to work.")
407
408 ;see gnus-cus.el
409 ;(defvar gnus-visual t
410 ;  "*If non-nil, will do various highlighting.
411 ;If nil, no mouse highlights (or any other highlights) will be
412 ;performed.  This might speed up Gnus some when generating large group
413 ;and summary buffers.")
414
415 (defvar gnus-novice-user t
416   "*Non-nil means that you are a usenet novice.
417 If non-nil, verbose messages may be displayed and confirmations may be
418 required.")
419
420 (defvar gnus-expert-user nil
421   "*Non-nil means that you will never be asked for confirmation about anything.
422 And that means *anything*.")
423
424 (defvar gnus-verbose 7
425   "*Integer that says how verbose Gnus should be.
426 The higher the number, the more messages Gnus will flash to say what
427 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
428 display most important messages; and at ten, Gnus will keep on
429 jabbering all the time.")
430
431 (defvar gnus-keep-same-level nil
432   "*Non-nil means that the next newsgroup after the current will be on the same level.
433 When you type, for instance, `n' after reading the last article in the
434 current newsgroup, you will go to the next newsgroup.  If this variable
435 is nil, the next newsgroup will be the next from the group
436 buffer.
437 If this variable is non-nil, Gnus will either put you in the
438 next newsgroup with the same level, or, if no such newsgroup is
439 available, the next newsgroup with the lowest possible level higher
440 than the current level.
441 If this variable is `best', Gnus will make the next newsgroup the one
442 with the best level.")
443
444 (defvar gnus-summary-make-false-root 'adopt
445   "*nil means that Gnus won't gather loose threads.
446 If the root of a thread has expired or been read in a previous
447 session, the information necessary to build a complete thread has been
448 lost.  Instead of having many small sub-threads from this original thread
449 scattered all over the summary buffer, Gnus can gather them.
450
451 If non-nil, Gnus will try to gather all loose sub-threads from an
452 original thread into one large thread.
453
454 If this variable is non-nil, it should be one of `none', `adopt',
455 `dummy' or `empty'.
456
457 If this variable is `none', Gnus will not make a false root, but just
458 present the sub-threads after another.
459 If this variable is `dummy', Gnus will create a dummy root that will
460 have all the sub-threads as children.
461 If this variable is `adopt', Gnus will make one of the \"children\"
462 the parent and mark all the step-children as such.
463 If this variable is `empty', the \"children\" are printed with empty
464 subject fields.  (Or rather, they will be printed with a string
465 given by the `gnus-summary-same-subject' variable.)")
466
467 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
468   "*A regexp to match subjects to be excluded from loose thread gathering.
469 As loose thread gathering is done on subjects only, that means that
470 there can be many false gatherings performed.  By rooting out certain
471 common subjects, gathering might become saner.")
472
473 (defvar gnus-summary-gather-subject-limit nil
474   "*Maximum length of subject comparisons when gathering loose threads.
475 Use nil to compare full subjects.  Setting this variable to a low
476 number will help gather threads that have been corrupted by
477 newsreaders chopping off subject lines, but it might also mean that
478 unrelated articles that have subject that happen to begin with the
479 same few characters will be incorrectly gathered.
480
481 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
482 comparing subjects.")
483
484 (defvar gnus-simplify-ignored-prefixes nil
485   "*Regexp, matches for which are removed from subject lines when simplifying.")
486
487 (defvar gnus-build-sparse-threads nil
488   "*If non-nil, fill in the gaps in threads.
489 If `some', only fill in the gaps that are needed to tie loose threads
490 together.  If `more', fill in all leaf nodes that Gnus can find.  If
491 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
492
493 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
494   "Function used for gathering loose threads.
495 There are two pre-defined functions: `gnus-gather-threads-by-subject',
496 which only takes Subjects into consideration; and
497 `gnus-gather-threads-by-references', which compared the References
498 headers of the articles to find matches.")
499
500 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
501 (defvar gnus-summary-same-subject ""
502   "*String indicating that the current article has the same subject as the previous.
503 This variable will only be used if the value of
504 `gnus-summary-make-false-root' is `empty'.")
505
506 (defvar gnus-summary-goto-unread t
507   "*If non-nil, marking commands will go to the next unread article.
508 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
509 whether it is read or not.")
510
511 (defvar gnus-group-goto-unread t
512   "*If non-nil, movement commands will go to the next unread and subscribed group.")
513
514 (defvar gnus-goto-next-group-when-activating t
515   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
516
517 (defvar gnus-check-new-newsgroups t
518   "*Non-nil means that Gnus will add new newsgroups at startup.
519 If this variable is `ask-server', Gnus will ask the server for new
520 groups since the last time it checked.  This means that the killed list
521 is no longer necessary, so you could set `gnus-save-killed-list' to
522 nil.
523
524 A variant is to have this variable be a list of select methods.  Gnus
525 will then use the `ask-server' method on all these select methods to
526 query for new groups from all those servers.
527
528 Eg.
529   (setq gnus-check-new-newsgroups
530         '((nntp \"some.server\") (nntp \"other.server\")))
531
532 If this variable is nil, then you have to tell Gnus explicitly to
533 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
534
535 (defvar gnus-check-bogus-newsgroups nil
536   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
537 If this variable is nil, then you have to tell Gnus explicitly to
538 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
539
540 (defvar gnus-read-active-file t
541   "*Non-nil means that Gnus will read the entire active file at startup.
542 If this variable is nil, Gnus will only know about the groups in your
543 `.newsrc' file.
544
545 If this variable is `some', Gnus will try to only read the relevant
546 parts of the active file from the server.  Not all servers support
547 this, and it might be quite slow with other servers, but this should
548 generally be faster than both the t and nil value.
549
550 If you set this variable to nil or `some', you probably still want to
551 be told about new newsgroups that arrive.  To do that, set
552 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
553 properly with all servers.")
554
555 (defvar gnus-level-subscribed 5
556   "*Groups with levels less than or equal to this variable are subscribed.")
557
558 (defvar gnus-level-unsubscribed 7
559   "*Groups with levels less than or equal to this variable are unsubscribed.
560 Groups with levels less than `gnus-level-subscribed', which should be
561 less than this variable, are subscribed.")
562
563 (defvar gnus-level-zombie 8
564   "*Groups with this level are zombie groups.")
565
566 (defvar gnus-level-killed 9
567   "*Groups with this level are killed.")
568
569 (defvar gnus-level-default-subscribed 3
570   "*New subscribed groups will be subscribed at this level.")
571
572 (defvar gnus-level-default-unsubscribed 6
573   "*New unsubscribed groups will be unsubscribed at this level.")
574
575 (defvar gnus-activate-level (1+ gnus-level-subscribed)
576   "*Groups higher than this level won't be activated on startup.
577 Setting this variable to something log might save lots of time when
578 you have many groups that you aren't interested in.")
579
580 (defvar gnus-activate-foreign-newsgroups 4
581   "*If nil, Gnus will not check foreign newsgroups at startup.
582 If it is non-nil, it should be a number between one and nine.  Foreign
583 newsgroups that have a level lower or equal to this number will be
584 activated on startup.  For instance, if you want to active all
585 subscribed newsgroups, but not the rest, you'd set this variable to
586 `gnus-level-subscribed'.
587
588 If you subscribe to lots of newsgroups from different servers, startup
589 might take a while.  By setting this variable to nil, you'll save time,
590 but you won't be told how many unread articles there are in the
591 groups.")
592
593 (defvar gnus-save-newsrc-file t
594   "*Non-nil means that Gnus will save the `.newsrc' file.
595 Gnus always saves its own startup file, which is called
596 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
597 be readily understood by other newsreaders.  If you don't plan on
598 using other newsreaders, set this variable to nil to save some time on
599 exit.")
600
601 (defvar gnus-save-killed-list t
602   "*If non-nil, save the list of killed groups to the startup file.
603 If you set this variable to nil, you'll save both time (when starting
604 and quitting) and space (both memory and disk), but it will also mean
605 that Gnus has no record of which groups are new and which are old, so
606 the automatic new newsgroups subscription methods become meaningless.
607
608 You should always set `gnus-check-new-newsgroups' to `ask-server' or
609 nil if you set this variable to nil.")
610
611 (defvar gnus-interactive-catchup t
612   "*If non-nil, require your confirmation when catching up a group.")
613
614 (defvar gnus-interactive-exit t
615   "*If non-nil, require your confirmation when exiting Gnus.")
616
617 (defvar gnus-kill-killed t
618   "*If non-nil, Gnus will apply kill files to already killed articles.
619 If it is nil, Gnus will never apply kill files to articles that have
620 already been through the scoring process, which might very well save lots
621 of time.")
622
623 (defvar gnus-extract-address-components 'gnus-extract-address-components
624   "*Function for extracting address components from a From header.
625 Two pre-defined function exist: `gnus-extract-address-components',
626 which is the default, quite fast, and too simplistic solution, and
627 `mail-extract-address-components', which works much better, but is
628 slower.")
629
630 (defvar gnus-summary-default-score 0
631   "*Default article score level.
632 If this variable is nil, scoring will be disabled.")
633
634 (defvar gnus-summary-zcore-fuzz 0
635   "*Fuzziness factor for the zcore in the summary buffer.
636 Articles with scores closer than this to `gnus-summary-default-score'
637 will not be marked.")
638
639 (defvar gnus-simplify-subject-fuzzy-regexp nil
640   "*Strings to be removed when doing fuzzy matches.
641 This can either be a regular expression or list of regular expressions
642 that will be removed from subject strings if fuzzy subject
643 simplification is selected.")
644
645 (defvar gnus-permanently-visible-groups nil
646   "*Regexp to match groups that should always be listed in the group buffer.
647 This means that they will still be listed when there are no unread
648 articles in the groups.")
649
650 (defvar gnus-list-groups-with-ticked-articles t
651   "*If non-nil, list groups that have only ticked articles.
652 If nil, only list groups that have unread articles.")
653
654 (defvar gnus-group-default-list-level gnus-level-subscribed
655   "*Default listing level.
656 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
657
658 (defvar gnus-group-use-permanent-levels nil
659   "*If non-nil, once you set a level, Gnus will use this level.")
660
661 (defvar gnus-group-list-inactive-groups t
662   "*If non-nil, inactive groups will be listed.")
663
664 (defvar gnus-show-mime nil
665   "*If non-nil, do mime processing of articles.
666 The articles will simply be fed to the function given by
667 `gnus-show-mime-method'.")
668
669 (defvar gnus-strict-mime t
670   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
671
672 (defvar gnus-show-mime-method 'metamail-buffer
673   "*Function to process a MIME message.
674 The function is called from the article buffer.")
675
676 (defvar gnus-decode-encoded-word-method (lambda ())
677   "*Function to decode a MIME encoded-words.
678 The function is called from the article buffer.")
679
680 (defvar gnus-show-threads t
681   "*If non-nil, display threads in summary mode.")
682
683 (defvar gnus-thread-hide-subtree nil
684   "*If non-nil, hide all threads initially.
685 If threads are hidden, you have to run the command
686 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
687 to expose hidden threads.")
688
689 (defvar gnus-thread-hide-killed t
690   "*If non-nil, hide killed threads automatically.")
691
692 (defvar gnus-thread-ignore-subject nil
693   "*If non-nil, ignore subjects and do all threading based on the Reference header.
694 If nil, which is the default, articles that have different subjects
695 from their parents will start separate threads.")
696
697 (defvar gnus-thread-operation-ignore-subject t
698   "*If non-nil, subjects will be ignored when doing thread commands.
699 This affects commands like `gnus-summary-kill-thread' and
700 `gnus-summary-lower-thread'.
701
702 If this variable is nil, articles in the same thread with different
703 subjects will not be included in the operation in question.  If this
704 variable is `fuzzy', only articles that have subjects that are fuzzily
705 equal will be included.")
706
707 (defvar gnus-thread-indent-level 4
708   "*Number that says how much each sub-thread should be indented.")
709
710 (defvar gnus-ignored-newsgroups
711   (purecopy (mapconcat 'identity
712                        '("^to\\."       ; not "real" groups
713                          "^[0-9. \t]+ " ; all digits in name
714                          "[][\"#'()]"   ; bogus characters
715                          )
716                        "\\|"))
717   "*A regexp to match uninteresting newsgroups in the active file.
718 Any lines in the active file matching this regular expression are
719 removed from the newsgroup list before anything else is done to it,
720 thus making them effectively non-existent.")
721
722 (defvar gnus-ignored-headers
723   "^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:"
724   "*All headers that match this regexp will be hidden.
725 This variable can also be a list of regexps of headers to be ignored.
726 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
727
728 (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-"
729   "*All headers that do not match this regexp will be hidden.
730 This variable can also be a list of regexp of headers to remain visible.
731 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
732
733 (defvar gnus-sorted-header-list
734   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
735     "^Cc:" "^Date:" "^Organization:")
736   "*This variable is a list of regular expressions.
737 If it is non-nil, headers that match the regular expressions will
738 be placed first in the article buffer in the sequence specified by
739 this list.")
740
741 (defvar gnus-boring-article-headers
742   '(empty followup-to reply-to)
743   "*Headers that are only to be displayed if they have interesting data.
744 Possible values in this list are `empty', `newsgroups', `followup-to',
745 `reply-to', and `date'.")
746
747 (defvar gnus-show-all-headers nil
748   "*If non-nil, don't hide any headers.")
749
750 (defvar gnus-save-all-headers t
751   "*If non-nil, don't remove any headers before saving.")
752
753 (defvar gnus-saved-headers gnus-visible-headers
754   "*Headers to keep if `gnus-save-all-headers' is nil.
755 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
756 If that variable is nil, however, all headers that match this regexp
757 will be kept while the rest will be deleted before saving.")
758
759 (defvar gnus-inhibit-startup-message nil
760   "*If non-nil, the startup message will not be displayed.")
761
762 (defvar gnus-signature-separator "^-- *$"
763   "Regexp matching signature separator.")
764
765 (defvar gnus-signature-limit nil
766   "Provide a limit to what is considered a signature.
767 If it is a number, no signature may not be longer (in characters) than
768 that number.  If it is a function, the function will be called without
769 any parameters, and if it returns nil, there is no signature in the
770 buffer.  If it is a string, it will be used as a regexp.  If it
771 matches, the text in question is not a signature.")
772
773 (defvar gnus-auto-extend-newsgroup t
774   "*If non-nil, extend newsgroup forward and backward when requested.")
775
776 (defvar gnus-auto-select-first t
777   "*If nil, don't select the first unread article when entering a group.
778 If this variable is `best', select the highest-scored unread article
779 in the group.  If neither nil nor `best', select the first unread
780 article.
781
782 If you want to prevent automatic selection of the first unread article
783 in some newsgroups, set the variable to nil in
784 `gnus-select-group-hook'.")
785
786 (defvar gnus-auto-select-next t
787   "*If non-nil, offer to go to the next group from the end of the previous.
788 If the value is t and the next newsgroup is empty, Gnus will exit
789 summary mode and go back to group mode.  If the value is neither nil
790 nor t, Gnus will select the following unread newsgroup.  In
791 particular, if the value is the symbol `quietly', the next unread
792 newsgroup will be selected without any confirmation, and if it is
793 `almost-quietly', the next group will be selected without any
794 confirmation if you are located on the last article in the group.
795 Finally, if this variable is `slightly-quietly', the `Z n' command
796 will go to the next group without confirmation.")
797
798 (defvar gnus-auto-select-same nil
799   "*If non-nil, select the next article with the same subject.")
800
801 (defvar gnus-summary-check-current nil
802   "*If non-nil, consider the current article when moving.
803 The \"unread\" movement commands will stay on the same line if the
804 current article is unread.")
805
806 (defvar gnus-auto-center-summary t
807   "*If non-nil, always center the current summary buffer.
808 In particular, if `vertical' do only vertical recentering.  If non-nil
809 and non-`vertical', do both horizontal and vertical recentering.")
810
811 (defvar gnus-break-pages t
812   "*If non-nil, do page breaking on articles.
813 The page delimiter is specified by the `gnus-page-delimiter'
814 variable.")
815
816 (defvar gnus-page-delimiter "^\^L"
817   "*Regexp describing what to use as article page delimiters.
818 The default value is \"^\^L\", which is a form linefeed at the
819 beginning of a line.")
820
821 (defvar gnus-use-full-window t
822   "*If non-nil, use the entire Emacs screen.")
823
824 (defvar gnus-window-configuration nil
825   "Obsolete variable.  See `gnus-buffer-configuration'.")
826
827 (defvar gnus-window-min-width 2
828   "*Minimum width of Gnus buffers.")
829
830 (defvar gnus-window-min-height 1
831   "*Minimum height of Gnus buffers.")
832
833 (defvar gnus-buffer-configuration
834   '((group
835      (vertical 1.0
836                (group 1.0 point)
837                (if gnus-carpal '(group-carpal 4))))
838     (summary
839      (vertical 1.0
840                (summary 1.0 point)
841                (if gnus-carpal '(summary-carpal 4))))
842     (article
843      (cond 
844       (gnus-use-picons
845        '(frame 1.0
846                (vertical 1.0
847                          (summary 0.25 point)
848                          (if gnus-carpal '(summary-carpal 4))
849                          (article 1.0))
850                (vertical ((height . 5) (width . 15)
851                           (user-position . t)
852                           (left . -1) (top . 1))
853                          (picons 1.0))))
854       (gnus-use-trees
855        '(vertical 1.0
856                   (summary 0.25 point)
857                   (tree 0.25)
858                   (article 1.0)))
859       (t
860        '(vertical 1.0
861                  (summary 0.25 point)
862                  (if gnus-carpal '(summary-carpal 4))
863                  (if gnus-use-trees '(tree 0.25))
864                  (article 1.0)))))
865     (server
866      (vertical 1.0
867                (server 1.0 point)
868                (if gnus-carpal '(server-carpal 2))))
869     (browse
870      (vertical 1.0
871                (browse 1.0 point)
872                (if gnus-carpal '(browse-carpal 2))))
873     (message
874      (vertical 1.0
875                (message 1.0 point)))
876     (pick
877      (vertical 1.0
878                (article 1.0 point)))
879     (info
880      (vertical 1.0
881                (info 1.0 point)))
882     (summary-faq
883      (vertical 1.0
884                (summary 0.25)
885                (faq 1.0 point)))
886     (edit-group
887      (vertical 1.0
888                (group 0.5)
889                (edit-group 1.0 point)))
890     (edit-server
891      (vertical 1.0
892                (server 0.5)
893                (edit-server 1.0 point)))
894     (edit-score
895      (vertical 1.0
896                (summary 0.25)
897                (edit-score 1.0 point)))
898     (post
899      (vertical 1.0
900                (post 1.0 point)))
901     (reply
902      (vertical 1.0
903                (article-copy 0.5)
904                (message 1.0 point)))
905     (forward
906      (vertical 1.0
907                (message 1.0 point)))
908     (reply-yank
909      (vertical 1.0
910                (message 1.0 point)))
911     (mail-bounce
912      (vertical 1.0
913                (article 0.5)
914                (message 1.0 point)))
915     (draft
916      (vertical 1.0
917                (draft 1.0 point)))
918     (pipe
919      (vertical 1.0
920                (summary 0.25 point)
921                (if gnus-carpal '(summary-carpal 4))
922                ("*Shell Command Output*" 1.0)))
923     (bug
924      (vertical 1.0
925                ("*Gnus Help Bug*" 0.5)
926                ("*Gnus Bug*" 1.0 point)))
927     (compose-bounce
928      (vertical 1.0
929                (article 0.5)
930                (message 1.0 point))))
931   "Window configuration for all possible Gnus buffers.
932 This variable is a list of lists.  Each of these lists has a NAME and
933 a RULE.  The NAMEs are commonsense names like `group', which names a
934 rule used when displaying the group buffer; `summary', which names a
935 rule for what happens when you enter a group and do not display an
936 article buffer; and so on.  See the value of this variable for a
937 complete list of NAMEs.
938
939 Each RULE is a list of vectors.  The first element in this vector is
940 the name of the buffer to be displayed; the second element is the
941 percentage of the screen this buffer is to occupy (a number in the
942 0.0-0.99 range); the optional third element is `point', which should
943 be present to denote which buffer point is to go to after making this
944 buffer configuration.")
945
946 (defvar gnus-window-to-buffer
947   '((group . gnus-group-buffer)
948     (summary . gnus-summary-buffer)
949     (article . gnus-article-buffer)
950     (server . gnus-server-buffer)
951     (browse . "*Gnus Browse Server*")
952     (edit-group . gnus-group-edit-buffer)
953     (edit-server . gnus-server-edit-buffer)
954     (group-carpal . gnus-carpal-group-buffer)
955     (summary-carpal . gnus-carpal-summary-buffer)
956     (server-carpal . gnus-carpal-server-buffer)
957     (browse-carpal . gnus-carpal-browse-buffer)
958     (edit-score . gnus-score-edit-buffer)
959     (message . gnus-message-buffer)
960     (mail . gnus-message-buffer)
961     (post-news . gnus-message-buffer)
962     (faq . gnus-faq-buffer)
963     (picons . "*Picons*")
964     (tree . gnus-tree-buffer)
965     (info . gnus-info-buffer)
966     (article-copy . gnus-article-copy)
967     (draft . gnus-draft-buffer))
968   "Mapping from short symbols to buffer names or buffer variables.")
969
970 (defvar gnus-carpal nil
971   "*If non-nil, display clickable icons.")
972
973 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
974   "*Function called with a group name when new group is detected.
975 A few pre-made functions are supplied: `gnus-subscribe-randomly'
976 inserts new groups at the beginning of the list of groups;
977 `gnus-subscribe-alphabetically' inserts new groups in strict
978 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
979 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
980 for your decision; `gnus-subscribe-killed' kills all new groups;
981 `gnus-subscribe-zombies' will make all new groups into zombies.")
982
983 ;; Suggested by a bug report by Hallvard B Furuseth.
984 ;; <h.b.furuseth@usit.uio.no>.
985 (defvar gnus-subscribe-options-newsgroup-method
986   (function gnus-subscribe-alphabetically)
987   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
988 If, for instance, you want to subscribe to all newsgroups in the
989 \"no\" and \"alt\" hierarchies, you'd put the following in your
990 .newsrc file:
991
992 options -n no.all alt.all
993
994 Gnus will the subscribe all new newsgroups in these hierarchies with
995 the subscription method in this variable.")
996
997 (defvar gnus-subscribe-hierarchical-interactive nil
998   "*If non-nil, Gnus will offer to subscribe hierarchically.
999 When a new hierarchy appears, Gnus will ask the user:
1000
1001 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1002
1003 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1004 subscribe to all newsgroups in the hierarchy and `s' will skip this
1005 hierarchy in its entirety.")
1006
1007 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1008   "*Function used for sorting the group buffer.
1009 This function will be called with group info entries as the arguments
1010 for the groups to be sorted.  Pre-made functions include
1011 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1012 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1013 `gnus-group-sort-by-rank'.
1014
1015 This variable can also be a list of sorting functions.  In that case,
1016 the most significant sort function should be the last function in the
1017 list.")
1018
1019 ;; Mark variables suggested by Thomas Michanek
1020 ;; <Thomas.Michanek@telelogic.se>.
1021 (defvar gnus-unread-mark ? 
1022   "*Mark used for unread articles.")
1023 (defvar gnus-ticked-mark ?!
1024   "*Mark used for ticked articles.")
1025 (defvar gnus-dormant-mark ??
1026   "*Mark used for dormant articles.")
1027 (defvar gnus-del-mark ?r
1028   "*Mark used for del'd articles.")
1029 (defvar gnus-read-mark ?R
1030   "*Mark used for read articles.")
1031 (defvar gnus-expirable-mark ?E
1032   "*Mark used for expirable articles.")
1033 (defvar gnus-killed-mark ?K
1034   "*Mark used for killed articles.")
1035 (defvar gnus-souped-mark ?F
1036   "*Mark used for killed articles.")
1037 (defvar gnus-kill-file-mark ?X
1038   "*Mark used for articles killed by kill files.")
1039 (defvar gnus-low-score-mark ?Y
1040   "*Mark used for articles with a low score.")
1041 (defvar gnus-catchup-mark ?C
1042   "*Mark used for articles that are caught up.")
1043 (defvar gnus-replied-mark ?A
1044   "*Mark used for articles that have been replied to.")
1045 (defvar gnus-cached-mark ?*
1046   "*Mark used for articles that are in the cache.")
1047 (defvar gnus-saved-mark ?S
1048   "*Mark used for articles that have been saved to.")
1049 (defvar gnus-process-mark ?#
1050   "*Process mark.")
1051 (defvar gnus-ancient-mark ?O
1052   "*Mark used for ancient articles.")
1053 (defvar gnus-sparse-mark ?Q
1054   "*Mark used for sparsely reffed articles.")
1055 (defvar gnus-canceled-mark ?G
1056   "*Mark used for canceled articles.")
1057 (defvar gnus-score-over-mark ?+
1058   "*Score mark used for articles with high scores.")
1059 (defvar gnus-score-below-mark ?-
1060   "*Score mark used for articles with low scores.")
1061 (defvar gnus-empty-thread-mark ? 
1062   "*There is no thread under the article.")
1063 (defvar gnus-not-empty-thread-mark ?=
1064   "*There is a thread under the article.")
1065
1066 (defvar gnus-view-pseudo-asynchronously nil
1067   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1068
1069 (defvar gnus-view-pseudos nil
1070   "*If `automatic', pseudo-articles will be viewed automatically.
1071 If `not-confirm', pseudos will be viewed automatically, and the user
1072 will not be asked to confirm the command.")
1073
1074 (defvar gnus-view-pseudos-separately t
1075   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1076 If nil, all files that use the same viewing command will be given as a
1077 list of parameters to that command.")
1078
1079 (defvar gnus-insert-pseudo-articles t
1080   "*If non-nil, insert pseudo-articles when decoding articles.")
1081
1082 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1083   "*Format of group lines.
1084 It works along the same lines as a normal formatting string,
1085 with some simple extensions.
1086
1087 %M    Only marked articles (character, \"*\" or \" \")
1088 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1089 %L    Level of subscribedness (integer)
1090 %N    Number of unread articles (integer)
1091 %I    Number of dormant articles (integer)
1092 %i    Number of ticked and dormant (integer)
1093 %T    Number of ticked articles (integer)
1094 %R    Number of read articles (integer)
1095 %t    Total number of articles (integer)
1096 %y    Number of unread, unticked articles (integer)
1097 %G    Group name (string)
1098 %g    Qualified group name (string)
1099 %D    Group description (string)
1100 %s    Select method (string)
1101 %o    Moderated group (char, \"m\")
1102 %p    Process mark (char)
1103 %O    Moderated group (string, \"(m)\" or \"\")
1104 %P    Topic indentation (string)
1105 %l    Whether there are GroupLens predictions for this group (string)
1106 %n    Select from where (string)
1107 %z    A string that look like `<%s:%n>' if a foreign select method is used
1108 %u    User defined specifier.  The next character in the format string should
1109       be a letter.  Gnus will call the function gnus-user-format-function-X,
1110       where X is the letter following %u.  The function will be passed the
1111       current header as argument.  The function should return a string, which
1112       will be inserted into the buffer just like information from any other
1113       group specifier.
1114
1115 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1116 the mouse point move inside the area.  There can only be one such area.
1117
1118 Note that this format specification is not always respected.  For
1119 reasons of efficiency, when listing killed groups, this specification
1120 is ignored altogether.  If the spec is changed considerably, your
1121 output may end up looking strange when listing both alive and killed
1122 groups.
1123
1124 If you use %o or %O, reading the active file will be slower and quite
1125 a bit of extra memory will be used. %D will also worsen performance.
1126 Also note that if you change the format specification to include any
1127 of these specs, you must probably re-start Gnus to see them go into
1128 effect.")
1129
1130 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1131   "*The format specification of the lines in the summary buffer.
1132
1133 It works along the same lines as a normal formatting string,
1134 with some simple extensions.
1135
1136 %N   Article number, left padded with spaces (string)
1137 %S   Subject (string)
1138 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1139 %n   Name of the poster (string)
1140 %a   Extracted name of the poster (string)
1141 %A   Extracted address of the poster (string)
1142 %F   Contents of the From: header (string)
1143 %x   Contents of the Xref: header (string)
1144 %D   Date of the article (string)
1145 %d   Date of the article (string) in DD-MMM format
1146 %M   Message-id of the article (string)
1147 %r   References of the article (string)
1148 %c   Number of characters in the article (integer)
1149 %L   Number of lines in the article (integer)
1150 %I   Indentation based on thread level (a string of spaces)
1151 %T   A string with two possible values: 80 spaces if the article
1152      is on thread level two or larger and 0 spaces on level one
1153 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1154 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1155 %[   Opening bracket (character, \"[\" or \"<\")
1156 %]   Closing bracket (character, \"]\" or \">\")
1157 %>   Spaces of length thread-level (string)
1158 %<   Spaces of length (- 20 thread-level) (string)
1159 %i   Article score (number)
1160 %z   Article zcore (character)
1161 %t   Number of articles under the current thread (number).
1162 %e   Whether the thread is empty or not (character).
1163 %l   GroupLens score (string).
1164 %u   User defined specifier.  The next character in the format string should
1165      be a letter.  Gnus will call the function gnus-user-format-function-X,
1166      where X is the letter following %u.  The function will be passed the
1167      current header as argument.  The function should return a string, which
1168      will be inserted into the summary just like information from any other
1169      summary specifier.
1170
1171 Text between %( and %) will be highlighted with `gnus-mouse-face'
1172 when the mouse point is placed inside the area.  There can only be one
1173 such area.
1174
1175 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1176 with care.  For reasons of efficiency, Gnus will compute what column
1177 these characters will end up in, and \"hard-code\" that.  This means that
1178 it is illegal to have these specs after a variable-length spec.  Well,
1179 you might not be arrested, but your summary buffer will look strange,
1180 which is bad enough.
1181
1182 The smart choice is to have these specs as for to the left as
1183 possible.
1184
1185 This restriction may disappear in later versions of Gnus.")
1186
1187 (defvar gnus-summary-dummy-line-format
1188   "*  %(:                          :%) %S\n"
1189   "*The format specification for the dummy roots in the summary buffer.
1190 It works along the same lines as a normal formatting string,
1191 with some simple extensions.
1192
1193 %S  The subject")
1194
1195 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1196   "*The format specification for the summary mode line.
1197 It works along the same lines as a normal formatting string,
1198 with some simple extensions:
1199
1200 %G  Group name
1201 %p  Unprefixed group name
1202 %A  Current article number
1203 %V  Gnus version
1204 %U  Number of unread articles in the group
1205 %e  Number of unselected articles in the group
1206 %Z  A string with unread/unselected article counts
1207 %g  Shortish group name
1208 %S  Subject of the current article
1209 %u  User-defined spec
1210 %s  Current score file name
1211 %d  Number of dormant articles
1212 %r  Number of articles that have been marked as read in this session
1213 %E  Number of articles expunged by the score files")
1214
1215 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1216   "*The format specification for the article mode line.
1217 See `gnus-summary-mode-line-format' for a closer description.")
1218
1219 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1220   "*The format specification for the group mode line.
1221 It works along the same lines as a normal formatting string,
1222 with some simple extensions:
1223
1224 %S   The native news server.
1225 %M   The native select method.
1226 %:   \":\" if %S isn't \"\".")
1227
1228 (defvar gnus-valid-select-methods
1229   '(("nntp" post address prompt-address)
1230     ("nnspool" post address)
1231     ("nnvirtual" post-mail virtual prompt-address)
1232     ("nnmbox" mail respool address)
1233     ("nnml" mail respool address)
1234     ("nnmh" mail respool address)
1235     ("nndir" post-mail prompt-address address)
1236     ("nneething" none address prompt-address)
1237     ("nndoc" none address prompt-address)
1238     ("nnbabyl" mail address respool)
1239     ("nnkiboze" post address virtual)
1240     ("nnsoup" post-mail address)
1241     ("nndraft" post-mail)
1242     ("nnfolder" mail respool address))
1243   "An alist of valid select methods.
1244 The first element of each list lists should be a string with the name
1245 of the select method.  The other elements may be be the category of
1246 this method (ie. `post', `mail', `none' or whatever) or other
1247 properties that this method has (like being respoolable).
1248 If you implement a new select method, all you should have to change is
1249 this variable.  I think.")
1250
1251 (defvar gnus-updated-mode-lines '(group article summary tree)
1252   "*List of buffers that should update their mode lines.
1253 The list may contain the symbols `group', `article' and `summary'.  If
1254 the corresponding symbol is present, Gnus will keep that mode line
1255 updated with information that may be pertinent.
1256 If this variable is nil, screen refresh may be quicker.")
1257
1258 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1259 (defvar gnus-mode-non-string-length nil
1260   "*Max length of mode-line non-string contents.
1261 If this is nil, Gnus will take space as is needed, leaving the rest
1262 of the modeline intact.")
1263
1264 ;see gnus-cus.el
1265 ;(defvar gnus-mouse-face 'highlight
1266 ;  "*Face used for mouse highlighting in Gnus.
1267 ;No mouse highlights will be done if `gnus-visual' is nil.")
1268
1269 (defvar gnus-summary-mark-below 0
1270   "*Mark all articles with a score below this variable as read.
1271 This variable is local to each summary buffer and usually set by the
1272 score file.")
1273
1274 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1275   "*List of functions used for sorting articles in the summary buffer.
1276 This variable is only used when not using a threaded display.")
1277
1278 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1279   "*List of functions used for sorting threads in the summary buffer.
1280 By default, threads are sorted by article number.
1281
1282 Each function takes two threads and return non-nil if the first thread
1283 should be sorted before the other.  If you use more than one function,
1284 the primary sort function should be the last.  You should probably
1285 always include `gnus-thread-sort-by-number' in the list of sorting
1286 functions -- preferably first.
1287
1288 Ready-mady functions include `gnus-thread-sort-by-number',
1289 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1290 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1291 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1292
1293 (defvar gnus-thread-score-function '+
1294   "*Function used for calculating the total score of a thread.
1295
1296 The function is called with the scores of the article and each
1297 subthread and should then return the score of the thread.
1298
1299 Some functions you can use are `+', `max', or `min'.")
1300
1301 (defvar gnus-summary-expunge-below nil
1302   "All articles that have a score less than this variable will be expunged.")
1303
1304 (defvar gnus-thread-expunge-below nil
1305   "All threads that have a total score less than this variable will be expunged.
1306 See `gnus-thread-score-function' for en explanation of what a
1307 \"thread score\" is.")
1308
1309 (defvar gnus-auto-subscribed-groups
1310   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1311   "*All new groups that match this regexp will be subscribed automatically.
1312 Note that this variable only deals with new groups.  It has no effect
1313 whatsoever on old groups.")
1314
1315 (defvar gnus-options-subscribe nil
1316   "*All new groups matching this regexp will be subscribed unconditionally.
1317 Note that this variable deals only with new newsgroups.  This variable
1318 does not affect old newsgroups.")
1319
1320 (defvar gnus-options-not-subscribe nil
1321   "*All new groups matching this regexp will be ignored.
1322 Note that this variable deals only with new newsgroups.  This variable
1323 does not affect old (already subscribed) newsgroups.")
1324
1325 (defvar gnus-auto-expirable-newsgroups nil
1326   "*Groups in which to automatically mark read articles as expirable.
1327 If non-nil, this should be a regexp that should match all groups in
1328 which to perform auto-expiry.  This only makes sense for mail groups.")
1329
1330 (defvar gnus-total-expirable-newsgroups nil
1331   "*Groups in which to perform expiry of all read articles.
1332 Use with extreme caution.  All groups that match this regexp will be
1333 expiring - which means that all read articles will be deleted after
1334 (say) one week.  (This only goes for mail groups and the like, of
1335 course.)")
1336
1337 (defvar gnus-group-uncollapsed-levels 1
1338   "Number of group name elements to leave alone when making a short group name.")
1339
1340 (defvar gnus-hidden-properties '(invisible t intangible t)
1341   "Property list to use for hiding text.")
1342
1343 (defvar gnus-modtime-botch nil
1344   "*Non-nil means .newsrc should be deleted prior to save.  
1345 Its use is due to the bogus appearance that .newsrc was modified on
1346 disc.")
1347
1348 ;; Hooks.
1349
1350 (defvar gnus-group-mode-hook nil
1351   "*A hook for Gnus group mode.")
1352
1353 (defvar gnus-summary-mode-hook nil
1354   "*A hook for Gnus summary mode.
1355 This hook is run before any variables are set in the summary buffer.")
1356
1357 (defvar gnus-article-mode-hook nil
1358   "*A hook for Gnus article mode.")
1359
1360 (defvar gnus-summary-prepare-exit-hook nil
1361   "*A hook called when preparing to exit from the summary buffer.
1362 It calls `gnus-summary-expire-articles' by default.")
1363 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1364
1365 (defvar gnus-summary-exit-hook nil
1366   "*A hook called on exit from the summary buffer.")
1367
1368 (defvar gnus-group-catchup-group-hook nil
1369   "*A hook run when catching up a group from the group buffer.")
1370
1371 (defvar gnus-group-update-group-hook nil
1372   "*A hook called when updating group lines.")
1373
1374 (defvar gnus-open-server-hook nil
1375   "*A hook called just before opening connection to the news server.")
1376
1377 (defvar gnus-load-hook nil
1378   "*A hook run while Gnus is loaded.")
1379
1380 (defvar gnus-startup-hook nil
1381   "*A hook called at startup.
1382 This hook is called after Gnus is connected to the NNTP server.")
1383
1384 (defvar gnus-get-new-news-hook nil
1385   "*A hook run just before Gnus checks for new news.")
1386
1387 (defvar gnus-after-getting-new-news-hook nil
1388   "*A hook run after Gnus checks for new news.")
1389
1390 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1391   "*A function that is called to generate the group buffer.
1392 The function is called with three arguments: The first is a number;
1393 all group with a level less or equal to that number should be listed,
1394 if the second is non-nil, empty groups should also be displayed.  If
1395 the third is non-nil, it is a number.  No groups with a level lower
1396 than this number should be displayed.
1397
1398 The only current function implemented is `gnus-group-prepare-flat'.")
1399
1400 (defvar gnus-group-prepare-hook nil
1401   "*A hook called after the group buffer has been generated.
1402 If you want to modify the group buffer, you can use this hook.")
1403
1404 (defvar gnus-summary-prepare-hook nil
1405   "*A hook called after the summary buffer has been generated.
1406 If you want to modify the summary buffer, you can use this hook.")
1407
1408 (defvar gnus-summary-generate-hook nil
1409   "*A hook run just before generating the summary buffer.
1410 This hook is commonly used to customize threading variables and the
1411 like.")
1412
1413 (defvar gnus-article-prepare-hook nil
1414   "*A hook called after an article has been prepared in the article buffer.
1415 If you want to run a special decoding program like nkf, use this hook.")
1416
1417 ;(defvar gnus-article-display-hook nil
1418 ;  "*A hook called after the article is displayed in the article buffer.
1419 ;The hook is designed to change the contents of the article
1420 ;buffer.  Typical functions that this hook may contain are
1421 ;`gnus-article-hide-headers' (hide selected headers),
1422 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1423 ;`gnus-article-hide-signature' (hide signature) and
1424 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1425 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1426 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1427 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1428
1429 (defvar gnus-article-x-face-too-ugly nil
1430   "Regexp matching posters whose face shouldn't be shown automatically.")
1431
1432 (defvar gnus-select-group-hook nil
1433   "*A hook called when a newsgroup is selected.
1434
1435 If you'd like to simplify subjects like the
1436 `gnus-summary-next-same-subject' command does, you can use the
1437 following hook:
1438
1439  (setq gnus-select-group-hook
1440       (list
1441         (lambda ()
1442           (mapcar (lambda (header)
1443                      (mail-header-set-subject
1444                       header
1445                       (gnus-simplify-subject
1446                        (mail-header-subject header) 're-only)))
1447                   gnus-newsgroup-headers))))")
1448
1449 (defvar gnus-select-article-hook nil
1450   "*A hook called when an article is selected.")
1451
1452 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1453   "*A hook called to apply kill files to a group.
1454 This hook is intended to apply a kill file to the selected newsgroup.
1455 The function `gnus-apply-kill-file' is called by default.
1456
1457 Since a general kill file is too heavy to use only for a few
1458 newsgroups, I recommend you to use a lighter hook function.  For
1459 example, if you'd like to apply a kill file to articles which contains
1460 a string `rmgroup' in subject in newsgroup `control', you can use the
1461 following hook:
1462
1463  (setq gnus-apply-kill-hook
1464       (list
1465         (lambda ()
1466           (cond ((string-match \"control\" gnus-newsgroup-name)
1467                  (gnus-kill \"Subject\" \"rmgroup\")
1468                  (gnus-expunge \"X\"))))))")
1469
1470 (defvar gnus-visual-mark-article-hook
1471   (list 'gnus-highlight-selected-summary)
1472   "*Hook run after selecting an article in the summary buffer.
1473 It is meant to be used for highlighting the article in some way.  It
1474 is not run if `gnus-visual' is nil.")
1475
1476 (defvar gnus-parse-headers-hook nil
1477   "*A hook called before parsing the headers.")
1478 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1479
1480 (defvar gnus-exit-group-hook nil
1481   "*A hook called when exiting (not quitting) summary mode.")
1482
1483 (defvar gnus-suspend-gnus-hook nil
1484   "*A hook called when suspending (not exiting) Gnus.")
1485
1486 (defvar gnus-exit-gnus-hook nil
1487   "*A hook called when exiting Gnus.")
1488
1489 (defvar gnus-after-exiting-gnus-hook nil
1490   "*A hook called after exiting Gnus.")
1491
1492 (defvar gnus-save-newsrc-hook nil
1493   "*A hook called before saving any of the newsrc files.")
1494
1495 (defvar gnus-save-quick-newsrc-hook nil
1496   "*A hook called just before saving the quick newsrc file.
1497 Can be used to turn version control on or off.")
1498
1499 (defvar gnus-save-standard-newsrc-hook nil
1500   "*A hook called just before saving the standard newsrc file.
1501 Can be used to turn version control on or off.")
1502
1503 (defvar gnus-summary-update-hook
1504   (list 'gnus-summary-highlight-line)
1505   "*A hook called when a summary line is changed.
1506 The hook will not be called if `gnus-visual' is nil.
1507
1508 The default function `gnus-summary-highlight-line' will
1509 highlight the line according to the `gnus-summary-highlight'
1510 variable.")
1511
1512 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1513   "*A hook called when a group line is changed.
1514 The hook will not be called if `gnus-visual' is nil.
1515
1516 The default function `gnus-group-highlight-line' will
1517 highlight the line according to the `gnus-group-highlight'
1518 variable.")
1519
1520 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1521   "*A hook called when an article is selected for the first time.
1522 The hook is intended to mark an article as read (or unread)
1523 automatically when it is selected.")
1524
1525 (defvar gnus-group-change-level-function nil
1526   "Function run when a group level is changed.
1527 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1528
1529 ;; Remove any hilit infestation.
1530 (add-hook 'gnus-startup-hook
1531           (lambda ()
1532             (remove-hook 'gnus-summary-prepare-hook
1533                          'hilit-rehighlight-buffer-quietly)
1534             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1535             (setq gnus-mark-article-hook
1536                   '(gnus-summary-mark-read-and-unread-as-read))
1537             (remove-hook 'gnus-article-prepare-hook
1538                          'hilit-rehighlight-buffer-quietly)))
1539
1540 \f
1541 ;; Internal variables
1542
1543 (defvar gnus-tree-buffer "*Tree*"
1544   "Buffer where Gnus thread trees are displayed.")
1545
1546 ;; Dummy variable.
1547 (defvar gnus-use-generic-from nil)
1548
1549 (defvar gnus-thread-indent-array nil)
1550 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1551
1552 (defvar gnus-newsrc-file-version nil)
1553
1554 (defvar gnus-method-history nil)
1555 ;; Variable holding the user answers to all method prompts.
1556
1557 (defvar gnus-group-history nil)
1558 ;; Variable holding the user answers to all group prompts.
1559
1560 (defvar gnus-server-alist nil
1561   "List of available servers.")
1562
1563 (defvar gnus-group-indentation-function nil)
1564
1565 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1566
1567 (defvar gnus-goto-missing-group-function nil)
1568
1569 (defvar gnus-override-subscribe-method nil)
1570
1571 (defvar gnus-group-goto-next-group-function nil
1572   "Function to override finding the next group after listing groups.")
1573
1574 (defconst gnus-article-mark-lists
1575   '((marked . tick) (replied . reply)
1576     (expirable . expire) (killed . killed)
1577     (bookmarks . bookmark) (dormant . dormant)
1578     (scored . score) (saved . save)
1579     (cached . cache)
1580     ))
1581
1582 ;; Avoid highlighting in kill files.
1583 (defvar gnus-summary-inhibit-highlight nil)
1584 (defvar gnus-newsgroup-selected-overlay nil)
1585
1586 (defvar gnus-inhibit-hiding nil)
1587 (defvar gnus-group-indentation "")
1588 (defvar gnus-inhibit-limiting nil)
1589 (defvar gnus-created-frames nil)
1590
1591 (defvar gnus-article-mode-map nil)
1592 (defvar gnus-dribble-buffer nil)
1593 (defvar gnus-headers-retrieved-by nil)
1594 (defvar gnus-article-reply nil)
1595 (defvar gnus-override-method nil)
1596 (defvar gnus-article-check-size nil)
1597
1598 (defvar gnus-current-score-file nil)
1599 (defvar gnus-newsgroup-adaptive-score-file nil)
1600 (defvar gnus-scores-exclude-files nil)
1601
1602 (defvar gnus-opened-servers nil)
1603
1604 (defvar gnus-current-move-group nil)
1605 (defvar gnus-current-copy-group nil)
1606 (defvar gnus-current-crosspost-group nil)
1607
1608 (defvar gnus-newsgroup-dependencies nil)
1609 (defvar gnus-newsgroup-async nil)
1610 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1611
1612 (defvar gnus-newsgroup-adaptive nil)
1613
1614 (defvar gnus-summary-display-table nil)
1615 (defvar gnus-summary-display-article-function nil)
1616
1617 (defvar gnus-summary-highlight-line-function nil
1618   "Function called after highlighting a summary line.")
1619
1620 (defvar gnus-group-line-format-alist
1621   `((?M gnus-tmp-marked-mark ?c)
1622     (?S gnus-tmp-subscribed ?c)
1623     (?L gnus-tmp-level ?d)
1624     (?N (cond ((eq number t) "*" )
1625               ((numberp number) 
1626                (int-to-string
1627                 (+ number
1628                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1629                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1630               (t number)) ?s)
1631     (?R gnus-tmp-number-of-read ?s)
1632     (?t gnus-tmp-number-total ?d)
1633     (?y gnus-tmp-number-of-unread ?s)
1634     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1635     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1636     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1637            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1638     (?g gnus-tmp-group ?s)
1639     (?G gnus-tmp-qualified-group ?s)
1640     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1641     (?D gnus-tmp-newsgroup-description ?s)
1642     (?o gnus-tmp-moderated ?c)
1643     (?O gnus-tmp-moderated-string ?s)
1644     (?p gnus-tmp-process-marked ?c)
1645     (?s gnus-tmp-news-server ?s)
1646     (?n gnus-tmp-news-method ?s)
1647     (?P gnus-group-indentation ?s)
1648     (?l gnus-tmp-grouplens ?s)
1649     (?z gnus-tmp-news-method-string ?s)
1650     (?u gnus-tmp-user-defined ?s)))
1651
1652 (defvar gnus-summary-line-format-alist
1653   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1654     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1655     (?s gnus-tmp-subject-or-nil ?s)
1656     (?n gnus-tmp-name ?s)
1657     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1658         ?s)
1659     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1660             gnus-tmp-from) ?s)
1661     (?F gnus-tmp-from ?s)
1662     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1663     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1664     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1665     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1666     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1667     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1668     (?L gnus-tmp-lines ?d)
1669     (?I gnus-tmp-indentation ?s)
1670     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1671     (?R gnus-tmp-replied ?c)
1672     (?\[ gnus-tmp-opening-bracket ?c)
1673     (?\] gnus-tmp-closing-bracket ?c)
1674     (?\> (make-string gnus-tmp-level ? ) ?s)
1675     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1676     (?i gnus-tmp-score ?d)
1677     (?z gnus-tmp-score-char ?c)
1678     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1679     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1680     (?U gnus-tmp-unread ?c)
1681     (?t (gnus-summary-number-of-articles-in-thread
1682          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1683         ?d)
1684     (?e (gnus-summary-number-of-articles-in-thread
1685          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1686         ?c)
1687     (?u gnus-tmp-user-defined ?s))
1688   "An alist of format specifications that can appear in summary lines,
1689 and what variables they correspond with, along with the type of the
1690 variable (string, integer, character, etc).")
1691
1692 (defvar gnus-summary-dummy-line-format-alist
1693   `((?S gnus-tmp-subject ?s)
1694     (?N gnus-tmp-number ?d)
1695     (?u gnus-tmp-user-defined ?s)))
1696
1697 (defvar gnus-summary-mode-line-format-alist
1698   `((?G gnus-tmp-group-name ?s)
1699     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1700     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1701     (?A gnus-tmp-article-number ?d)
1702     (?Z gnus-tmp-unread-and-unselected ?s)
1703     (?V gnus-version ?s)
1704     (?U gnus-tmp-unread ?d)
1705     (?S gnus-tmp-subject ?s)
1706     (?e gnus-tmp-unselected ?d)
1707     (?u gnus-tmp-user-defined ?s)
1708     (?d (length gnus-newsgroup-dormant) ?d)
1709     (?t (length gnus-newsgroup-marked) ?d)
1710     (?r (length gnus-newsgroup-reads) ?d)
1711     (?E gnus-newsgroup-expunged-tally ?d)
1712     (?s (gnus-current-score-file-nondirectory) ?s)))
1713
1714 (defvar gnus-article-mode-line-format-alist
1715   gnus-summary-mode-line-format-alist)
1716
1717 (defvar gnus-group-mode-line-format-alist
1718   `((?S gnus-tmp-news-server ?s)
1719     (?M gnus-tmp-news-method ?s)
1720     (?u gnus-tmp-user-defined ?s)
1721     (?: gnus-tmp-colon ?s)))
1722
1723 (defvar gnus-have-read-active-file nil)
1724
1725 (defconst gnus-maintainer
1726   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1727   "The mail address of the Gnus maintainers.")
1728
1729 (defconst gnus-version-number "5.2.15"
1730   "Version number for this version of Gnus.")
1731
1732 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1733   "Version string for this version of Gnus.")
1734
1735 (defvar gnus-info-nodes
1736   '((gnus-group-mode "(gnus)The Group Buffer")
1737     (gnus-summary-mode "(gnus)The Summary Buffer")
1738     (gnus-article-mode "(gnus)The Article Buffer"))
1739   "Alist of major modes and related Info nodes.")
1740
1741 (defvar gnus-group-buffer "*Group*")
1742 (defvar gnus-summary-buffer "*Summary*")
1743 (defvar gnus-article-buffer "*Article*")
1744 (defvar gnus-server-buffer "*Server*")
1745
1746 (defvar gnus-work-buffer " *gnus work*")
1747
1748 (defvar gnus-original-article-buffer " *Original Article*")
1749 (defvar gnus-original-article nil)
1750
1751 (defvar gnus-buffer-list nil
1752   "Gnus buffers that should be killed on exit.")
1753
1754 (defvar gnus-slave nil
1755   "Whether this Gnus is a slave or not.")
1756
1757 (defvar gnus-variable-list
1758   '(gnus-newsrc-options gnus-newsrc-options-n
1759     gnus-newsrc-last-checked-date
1760     gnus-newsrc-alist gnus-server-alist
1761     gnus-killed-list gnus-zombie-list
1762     gnus-topic-topology gnus-topic-alist
1763     gnus-format-specs)
1764   "Gnus variables saved in the quick startup file.")
1765
1766 (defvar gnus-newsrc-options nil
1767   "Options line in the .newsrc file.")
1768
1769 (defvar gnus-newsrc-options-n nil
1770   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1771
1772 (defvar gnus-newsrc-last-checked-date nil
1773   "Date Gnus last asked server for new newsgroups.")
1774
1775 (defvar gnus-topic-topology nil
1776   "The complete topic hierarchy.")
1777
1778 (defvar gnus-topic-alist nil
1779   "The complete topic-group alist.")
1780
1781 (defvar gnus-newsrc-alist nil
1782   "Assoc list of read articles.
1783 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1784
1785 (defvar gnus-newsrc-hashtb nil
1786   "Hashtable of gnus-newsrc-alist.")
1787
1788 (defvar gnus-killed-list nil
1789   "List of killed newsgroups.")
1790
1791 (defvar gnus-killed-hashtb nil
1792   "Hash table equivalent of gnus-killed-list.")
1793
1794 (defvar gnus-zombie-list nil
1795   "List of almost dead newsgroups.")
1796
1797 (defvar gnus-description-hashtb nil
1798   "Descriptions of newsgroups.")
1799
1800 (defvar gnus-list-of-killed-groups nil
1801   "List of newsgroups that have recently been killed by the user.")
1802
1803 (defvar gnus-active-hashtb nil
1804   "Hashtable of active articles.")
1805
1806 (defvar gnus-moderated-list nil
1807   "List of moderated newsgroups.")
1808
1809 (defvar gnus-group-marked nil)
1810
1811 (defvar gnus-current-startup-file nil
1812   "Startup file for the current host.")
1813
1814 (defvar gnus-last-search-regexp nil
1815   "Default regexp for article search command.")
1816
1817 (defvar gnus-last-shell-command nil
1818   "Default shell command on article.")
1819
1820 (defvar gnus-current-select-method nil
1821   "The current method for selecting a newsgroup.")
1822
1823 (defvar gnus-group-list-mode nil)
1824
1825 (defvar gnus-article-internal-prepare-hook nil)
1826
1827 (defvar gnus-newsgroup-name nil)
1828 (defvar gnus-newsgroup-begin nil)
1829 (defvar gnus-newsgroup-end nil)
1830 (defvar gnus-newsgroup-last-rmail nil)
1831 (defvar gnus-newsgroup-last-mail nil)
1832 (defvar gnus-newsgroup-last-folder nil)
1833 (defvar gnus-newsgroup-last-file nil)
1834 (defvar gnus-newsgroup-auto-expire nil)
1835 (defvar gnus-newsgroup-active nil)
1836
1837 (defvar gnus-newsgroup-data nil)
1838 (defvar gnus-newsgroup-data-reverse nil)
1839 (defvar gnus-newsgroup-limit nil)
1840 (defvar gnus-newsgroup-limits nil)
1841
1842 (defvar gnus-newsgroup-unreads nil
1843   "List of unread articles in the current newsgroup.")
1844
1845 (defvar gnus-newsgroup-unselected nil
1846   "List of unselected unread articles in the current newsgroup.")
1847
1848 (defvar gnus-newsgroup-reads nil
1849   "Alist of read articles and article marks in the current newsgroup.")
1850
1851 (defvar gnus-newsgroup-expunged-tally nil)
1852
1853 (defvar gnus-newsgroup-marked nil
1854   "List of ticked articles in the current newsgroup (a subset of unread art).")
1855
1856 (defvar gnus-newsgroup-killed nil
1857   "List of ranges of articles that have been through the scoring process.")
1858
1859 (defvar gnus-newsgroup-cached nil
1860   "List of articles that come from the article cache.")
1861
1862 (defvar gnus-newsgroup-saved nil
1863   "List of articles that have been saved.")
1864
1865 (defvar gnus-newsgroup-kill-headers nil)
1866
1867 (defvar gnus-newsgroup-replied nil
1868   "List of articles that have been replied to in the current newsgroup.")
1869
1870 (defvar gnus-newsgroup-expirable nil
1871   "List of articles in the current newsgroup that can be expired.")
1872
1873 (defvar gnus-newsgroup-processable nil
1874   "List of articles in the current newsgroup that can be processed.")
1875
1876 (defvar gnus-newsgroup-bookmarks nil
1877   "List of articles in the current newsgroup that have bookmarks.")
1878
1879 (defvar gnus-newsgroup-dormant nil
1880   "List of dormant articles in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-scored nil
1883   "List of scored articles in the current newsgroup.")
1884
1885 (defvar gnus-newsgroup-headers nil
1886   "List of article headers in the current newsgroup.")
1887
1888 (defvar gnus-newsgroup-threads nil)
1889
1890 (defvar gnus-newsgroup-prepared nil
1891   "Whether the current group has been prepared properly.")
1892
1893 (defvar gnus-newsgroup-ancient nil
1894   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1895
1896 (defvar gnus-newsgroup-sparse nil)
1897
1898 (defvar gnus-current-article nil)
1899 (defvar gnus-article-current nil)
1900 (defvar gnus-current-headers nil)
1901 (defvar gnus-have-all-headers nil)
1902 (defvar gnus-last-article nil)
1903 (defvar gnus-newsgroup-history nil)
1904 (defvar gnus-current-kill-article nil)
1905
1906 ;; Save window configuration.
1907 (defvar gnus-prev-winconf nil)
1908
1909 (defvar gnus-summary-mark-positions nil)
1910 (defvar gnus-group-mark-positions nil)
1911
1912 (defvar gnus-reffed-article-number nil)
1913
1914 ;;; Let the byte-compiler know that we know about this variable.
1915 (defvar rmail-default-rmail-file)
1916
1917 (defvar gnus-cache-removable-articles nil)
1918
1919 (defvar gnus-dead-summary nil)
1920
1921 (defconst gnus-summary-local-variables
1922   '(gnus-newsgroup-name
1923     gnus-newsgroup-begin gnus-newsgroup-end
1924     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1925     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1926     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1927     gnus-newsgroup-unselected gnus-newsgroup-marked
1928     gnus-newsgroup-reads gnus-newsgroup-saved
1929     gnus-newsgroup-replied gnus-newsgroup-expirable
1930     gnus-newsgroup-processable gnus-newsgroup-killed
1931     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1932     gnus-newsgroup-headers gnus-newsgroup-threads
1933     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1934     gnus-current-article gnus-current-headers gnus-have-all-headers
1935     gnus-last-article gnus-article-internal-prepare-hook
1936     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1937     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1938     gnus-newsgroup-async gnus-thread-expunge-below
1939     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1940     (gnus-summary-mark-below . global)
1941     gnus-newsgroup-active gnus-scores-exclude-files
1942     gnus-newsgroup-history gnus-newsgroup-ancient
1943     gnus-newsgroup-sparse
1944     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1945     gnus-newsgroup-adaptive-score-file
1946     (gnus-newsgroup-expunged-tally . 0)
1947     gnus-cache-removable-articles gnus-newsgroup-cached
1948     gnus-newsgroup-data gnus-newsgroup-data-reverse
1949     gnus-newsgroup-limit gnus-newsgroup-limits)
1950   "Variables that are buffer-local to the summary buffers.")
1951
1952 (defconst gnus-bug-message
1953   "Sending a bug report to the Gnus Towers.
1954 ========================================
1955
1956 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1957 be sent to the Gnus Bug Exterminators.
1958
1959 At the bottom of the buffer you'll see lots of variable settings.
1960 Please do not delete those.  They will tell the Bug People what your
1961 environment is, so that it will be easier to locate the bugs.
1962
1963 If you have found a bug that makes Emacs go \"beep\", set
1964 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1965 and include the backtrace in your bug report.
1966
1967 Please describe the bug in annoying, painstaking detail.
1968
1969 Thank you for your help in stamping out bugs.
1970 ")
1971
1972 ;;; End of variables.
1973
1974 ;; Define some autoload functions Gnus might use.
1975 (eval-and-compile
1976
1977   ;; This little mapcar goes through the list below and marks the
1978   ;; symbols in question as autoloaded functions.
1979   (mapcar
1980    (lambda (package)
1981      (let ((interactive (nth 1 (memq ':interactive package))))
1982        (mapcar
1983         (lambda (function)
1984           (let (keymap)
1985             (when (consp function)
1986               (setq keymap (car (memq 'keymap function)))
1987               (setq function (car function)))
1988             (autoload function (car package) nil interactive keymap)))
1989         (if (eq (nth 1 package) ':interactive)
1990             (cdddr package)
1991           (cdr package)))))
1992    '(("metamail" metamail-buffer)
1993      ("info" Info-goto-node)
1994      ("hexl" hexl-hex-string-to-integer)
1995      ("pp" pp pp-to-string pp-eval-expression)
1996      ("mail-extr" mail-extract-address-components)
1997      ("nnmail" nnmail-split-fancy nnmail-article-group)
1998      ("nnvirtual" nnvirtual-catchup-group)
1999      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2000       timezone-make-sortable-date timezone-make-time-string)
2001      ("rmailout" rmail-output)
2002      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2003       rmail-show-message)
2004      ("gnus-soup" :interactive t
2005       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2006       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2007      ("nnsoup" nnsoup-pack-replies)
2008      ("gnus-scomo" :interactive t gnus-score-mode)
2009      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2010       gnus-Folder-save-name gnus-folder-save-name)
2011      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2012      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2013       gnus-server-make-menu-bar gnus-article-make-menu-bar
2014       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2015       gnus-summary-highlight-line gnus-carpal-setup-buffer
2016       gnus-group-highlight-line
2017       gnus-article-add-button gnus-insert-next-page-button
2018       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2019      ("gnus-vis" :interactive t
2020       gnus-article-push-button gnus-article-press-button
2021       gnus-article-highlight gnus-article-highlight-some
2022       gnus-article-highlight-headers gnus-article-highlight-signature
2023       gnus-article-add-buttons gnus-article-add-buttons-to-head
2024       gnus-article-next-button gnus-article-prev-button)
2025      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2026       gnus-demon-add-disconnection gnus-demon-add-handler
2027       gnus-demon-remove-handler)
2028      ("gnus-demon" :interactive t
2029       gnus-demon-init gnus-demon-cancel)
2030      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2031       gnus-tree-open gnus-tree-close)
2032      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2033       gnus-nocem-unwanted-article-p)
2034      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2035      ("gnus-srvr" gnus-browse-foreign-server)
2036      ("gnus-cite" :interactive t
2037       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2038       gnus-article-hide-citation gnus-article-fill-cited-article
2039       gnus-article-hide-citation-in-followups)
2040      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2041       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2042       gnus-execute gnus-expunge)
2043      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2044       gnus-cache-possibly-remove-articles gnus-cache-request-article
2045       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2046       gnus-cache-enter-remove-article gnus-cached-article-p
2047       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2048      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2049       gnus-cache-remove-article)
2050      ("gnus-score" :interactive t
2051       gnus-summary-increase-score gnus-summary-lower-score
2052       gnus-score-flush-cache gnus-score-close
2053       gnus-score-raise-same-subject-and-select
2054       gnus-score-raise-same-subject gnus-score-default
2055       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2056       gnus-score-lower-same-subject gnus-score-lower-thread
2057       gnus-possibly-score-headers gnus-summary-raise-score 
2058       gnus-summary-set-score gnus-summary-current-score)
2059      ("gnus-score"
2060       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2061       gnus-current-score-file-nondirectory gnus-score-adaptive
2062       gnus-score-find-trace gnus-score-file-name)
2063      ("gnus-edit" :interactive t gnus-score-customize)
2064      ("gnus-topic" :interactive t gnus-topic-mode)
2065      ("gnus-topic" gnus-topic-remove-group)
2066      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2067      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2068      ("gnus-uu" :interactive t
2069       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2070       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2071       gnus-uu-mark-by-regexp gnus-uu-mark-all
2072       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2073       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2074       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2075       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2076       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2077       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2078       gnus-uu-decode-binhex-view)
2079      ("gnus-msg" (gnus-summary-send-map keymap)
2080       gnus-mail-yank-original gnus-mail-send-and-exit
2081       gnus-sendmail-setup-mail gnus-article-mail
2082       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2083      ("gnus-msg" :interactive t
2084       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2085       gnus-summary-followup gnus-summary-followup-with-original
2086       gnus-summary-cancel-article gnus-summary-supersede-article
2087       gnus-post-news gnus-inews-news gnus-cancel-news
2088       gnus-summary-reply gnus-summary-reply-with-original
2089       gnus-summary-mail-forward gnus-summary-mail-other-window
2090       gnus-bug)
2091      ("gnus-picon" :interactive t gnus-article-display-picons
2092       gnus-group-display-picons gnus-picons-article-display-x-face)
2093      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2094       gnus-grouplens-mode)
2095      ("gnus-vm" gnus-vm-mail-setup)
2096      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2097       gnus-summary-save-article-vm))))
2098
2099 \f
2100
2101 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2102 ;; If you want the cursor to go somewhere else, set these two
2103 ;; functions in some startup hook to whatever you want.
2104 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2105 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2106
2107 ;;; Various macros and substs.
2108
2109 (defun gnus-header-from (header)
2110   (mail-header-from header))
2111
2112 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2113   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2114   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2115         (w (make-symbol "w"))
2116         (buf (make-symbol "buf")))
2117     `(let* ((,tempvar (selected-window))
2118             (,buf ,buffer)
2119             (,w (get-buffer-window ,buf 'visible)))
2120        (unwind-protect
2121            (progn
2122              (if ,w
2123                  (select-window ,w)
2124                (pop-to-buffer ,buf))
2125              ,@forms)
2126          (select-window ,tempvar)))))
2127
2128 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2129 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2130 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2131
2132 (defmacro gnus-gethash (string hashtable)
2133   "Get hash value of STRING in HASHTABLE."
2134   `(symbol-value (intern-soft ,string ,hashtable)))
2135
2136 (defmacro gnus-sethash (string value hashtable)
2137   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2138   `(set (intern ,string ,hashtable) ,value))
2139
2140 (defmacro gnus-intern-safe (string hashtable)
2141   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2142   `(let ((symbol (intern ,string ,hashtable)))
2143      (or (boundp symbol)
2144          (set symbol nil))
2145      symbol))
2146
2147 (defmacro gnus-group-unread (group)
2148   "Get the currently computed number of unread articles in GROUP."
2149   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2150
2151 (defmacro gnus-group-entry (group)
2152   "Get the newsrc entry for GROUP."
2153   `(gnus-gethash ,group gnus-newsrc-hashtb))
2154
2155 (defmacro gnus-active (group)
2156   "Get active info on GROUP."
2157   `(gnus-gethash ,group gnus-active-hashtb))
2158
2159 (defmacro gnus-set-active (group active)
2160   "Set GROUP's active info."
2161   `(gnus-sethash ,group ,active gnus-active-hashtb))
2162
2163 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2164 ;;   function `substring' might cut on a middle of multi-octet
2165 ;;   character.
2166 (defun gnus-truncate-string (str width)
2167   (substring str 0 width))
2168
2169 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2170 ;; to limit the length of a string.  This function is necessary since
2171 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2172 (defsubst gnus-limit-string (str width)
2173   (if (> (length str) width)
2174       (substring str 0 width)
2175     str))
2176
2177 (defsubst gnus-simplify-subject-re (subject)
2178   "Remove \"Re:\" from subject lines."
2179   (if (string-match "^[Rr][Ee]: *" subject)
2180       (substring subject (match-end 0))
2181     subject))
2182
2183 (defsubst gnus-functionp (form)
2184   "Return non-nil if FORM is funcallable."
2185   (or (and (symbolp form) (fboundp form))
2186       (and (listp form) (eq (car form) 'lambda))))
2187
2188 (defsubst gnus-goto-char (point)
2189   (and point (goto-char point)))
2190
2191 (defmacro gnus-buffer-exists-p (buffer)
2192   `(let ((buffer ,buffer))
2193      (and buffer
2194           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2195                    buffer))))
2196
2197 (defmacro gnus-kill-buffer (buffer)
2198   `(let ((buf ,buffer))
2199      (if (gnus-buffer-exists-p buf)
2200          (kill-buffer buf))))
2201
2202 (defsubst gnus-point-at-bol ()
2203   "Return point at the beginning of the line."
2204   (let ((p (point)))
2205     (beginning-of-line)
2206     (prog1
2207         (point)
2208       (goto-char p))))
2209
2210 (defsubst gnus-point-at-eol ()
2211   "Return point at the end of the line."
2212   (let ((p (point)))
2213     (end-of-line)
2214     (prog1
2215         (point)
2216       (goto-char p))))
2217
2218 (defun gnus-alive-p ()
2219   "Say whether Gnus is running or not."
2220   (and gnus-group-buffer
2221        (get-buffer gnus-group-buffer)))
2222
2223 (defun gnus-delete-first (elt list)
2224   "Delete by side effect the first occurrence of ELT as a member of LIST."
2225   (if (equal (car list) elt)
2226       (cdr list)
2227     (let ((total list))
2228       (while (and (cdr list)
2229                   (not (equal (cadr list) elt)))
2230         (setq list (cdr list)))
2231       (when (cdr list)
2232         (setcdr list (cddr list)))
2233       total)))
2234
2235 ;; Delete the current line (and the next N lines.);
2236 (defmacro gnus-delete-line (&optional n)
2237   `(delete-region (progn (beginning-of-line) (point))
2238                   (progn (forward-line ,(or n 1)) (point))))
2239
2240 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2241 (defvar gnus-init-inhibit nil)
2242 (defun gnus-read-init-file (&optional inhibit-next)
2243   (if gnus-init-inhibit
2244       (setq gnus-init-inhibit nil)
2245     (setq gnus-init-inhibit inhibit-next)
2246     (and gnus-init-file
2247          (or (and (file-exists-p gnus-init-file)
2248                   ;; Don't try to load a directory.
2249                   (not (file-directory-p gnus-init-file)))
2250              (file-exists-p (concat gnus-init-file ".el"))
2251              (file-exists-p (concat gnus-init-file ".elc")))
2252          (condition-case var
2253              (load gnus-init-file nil t)
2254            (error
2255             (error "Error in %s: %s" gnus-init-file var))))))
2256
2257 ;; Info access macros.
2258
2259 (defmacro gnus-info-group (info)
2260   `(nth 0 ,info))
2261 (defmacro gnus-info-rank (info)
2262   `(nth 1 ,info))
2263 (defmacro gnus-info-read (info)
2264   `(nth 2 ,info))
2265 (defmacro gnus-info-marks (info)
2266   `(nth 3 ,info))
2267 (defmacro gnus-info-method (info)
2268   `(nth 4 ,info))
2269 (defmacro gnus-info-params (info)
2270   `(nth 5 ,info))
2271
2272 (defmacro gnus-info-level (info)
2273   `(let ((rank (gnus-info-rank ,info)))
2274      (if (consp rank)
2275          (car rank)
2276        rank)))
2277 (defmacro gnus-info-score (info)
2278   `(let ((rank (gnus-info-rank ,info)))
2279      (or (and (consp rank) (cdr rank)) 0)))
2280
2281 (defmacro gnus-info-set-group (info group)
2282   `(setcar ,info ,group))
2283 (defmacro gnus-info-set-rank (info rank)
2284   `(setcar (nthcdr 1 ,info) ,rank))
2285 (defmacro gnus-info-set-read (info read)
2286   `(setcar (nthcdr 2 ,info) ,read))
2287 (defmacro gnus-info-set-marks (info marks)
2288   `(setcar (nthcdr 3 ,info) ,marks))
2289 (defmacro gnus-info-set-method (info method)
2290   `(setcar (nthcdr 4 ,info) ,method))
2291 (defmacro gnus-info-set-params (info params)
2292   `(setcar (nthcdr 5 ,info) ,params))
2293
2294 (defmacro gnus-info-set-level (info level)
2295   `(let ((rank (cdr ,info)))
2296      (if (consp (car rank))
2297          (setcar (car rank) ,level)
2298        (setcar rank ,level))))
2299 (defmacro gnus-info-set-score (info score)
2300   `(let ((rank (cdr ,info)))
2301      (if (consp (car rank))
2302          (setcdr (car rank) ,score)
2303        (setcar rank (cons (car rank) ,score)))))
2304
2305 (defmacro gnus-get-info (group)
2306   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2307
2308 (defun gnus-byte-code (func)
2309   "Return a form that can be `eval'ed based on FUNC."
2310   (let ((fval (symbol-function func)))
2311     (if (byte-code-function-p fval)
2312         (let ((flist (append fval nil)))
2313           (setcar flist 'byte-code)
2314           flist)
2315       (cons 'progn (cddr fval)))))
2316
2317 ;; Find out whether the gnus-visual TYPE is wanted.
2318 (defun gnus-visual-p (&optional type class)
2319   (and gnus-visual                      ; Has to be non-nil, at least.
2320        (if (not type)                   ; We don't care about type.
2321            gnus-visual
2322          (if (listp gnus-visual)        ; It's a list, so we check it.
2323              (or (memq type gnus-visual)
2324                  (memq class gnus-visual))
2325            t))))
2326
2327 ;;; Load the compatability functions.
2328
2329 (require 'gnus-cus)
2330 (require 'gnus-ems)
2331
2332 \f
2333 ;;;
2334 ;;; Shutdown
2335 ;;;
2336
2337 (defvar gnus-shutdown-alist nil)
2338
2339 (defun gnus-add-shutdown (function &rest symbols)
2340   "Run FUNCTION whenever one of SYMBOLS is shut down."
2341   (push (cons function symbols) gnus-shutdown-alist))
2342
2343 (defun gnus-shutdown (symbol)
2344   "Shut down everything that waits for SYMBOL."
2345   (let ((alist gnus-shutdown-alist)
2346         entry)
2347     (while (setq entry (pop alist))
2348       (when (memq symbol (cdr entry))
2349         (funcall (car entry))))))
2350
2351 \f
2352
2353 ;; Format specs.  The chunks below are the machine-generated forms
2354 ;; that are to be evaled as the result of the default format strings.
2355 ;; We write them in here to get them byte-compiled.  That way the
2356 ;; default actions will be quite fast, while still retaining the full
2357 ;; flexibility of the user-defined format specs.
2358
2359 ;; First we have lots of dummy defvars to let the compiler know these
2360 ;; are really dynamic variables.
2361
2362 (defvar gnus-tmp-unread)
2363 (defvar gnus-tmp-replied)
2364 (defvar gnus-tmp-score-char)
2365 (defvar gnus-tmp-indentation)
2366 (defvar gnus-tmp-opening-bracket)
2367 (defvar gnus-tmp-lines)
2368 (defvar gnus-tmp-name)
2369 (defvar gnus-tmp-closing-bracket)
2370 (defvar gnus-tmp-subject-or-nil)
2371 (defvar gnus-tmp-subject)
2372 (defvar gnus-tmp-marked)
2373 (defvar gnus-tmp-marked-mark)
2374 (defvar gnus-tmp-subscribed)
2375 (defvar gnus-tmp-process-marked)
2376 (defvar gnus-tmp-number-of-unread)
2377 (defvar gnus-tmp-group-name)
2378 (defvar gnus-tmp-group)
2379 (defvar gnus-tmp-article-number)
2380 (defvar gnus-tmp-unread-and-unselected)
2381 (defvar gnus-tmp-news-method)
2382 (defvar gnus-tmp-news-server)
2383 (defvar gnus-tmp-article-number)
2384 (defvar gnus-mouse-face)
2385 (defvar gnus-mouse-face-prop)
2386
2387 (defun gnus-summary-line-format-spec ()
2388   (insert gnus-tmp-unread gnus-tmp-replied
2389           gnus-tmp-score-char gnus-tmp-indentation)
2390   (gnus-put-text-property
2391    (point)
2392    (progn
2393      (insert
2394       gnus-tmp-opening-bracket
2395       (format "%4d: %-20s"
2396               gnus-tmp-lines
2397               (if (> (length gnus-tmp-name) 20)
2398                   (substring gnus-tmp-name 0 20)
2399                 gnus-tmp-name))
2400       gnus-tmp-closing-bracket)
2401      (point))
2402    gnus-mouse-face-prop gnus-mouse-face)
2403   (insert " " gnus-tmp-subject-or-nil "\n"))
2404
2405 (defvar gnus-summary-line-format-spec
2406   (gnus-byte-code 'gnus-summary-line-format-spec))
2407
2408 (defun gnus-summary-dummy-line-format-spec ()
2409   (insert "*  ")
2410   (gnus-put-text-property
2411    (point)
2412    (progn
2413      (insert ":                          :")
2414      (point))
2415    gnus-mouse-face-prop gnus-mouse-face)
2416   (insert " " gnus-tmp-subject "\n"))
2417
2418 (defvar gnus-summary-dummy-line-format-spec
2419   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2420
2421 (defun gnus-group-line-format-spec ()
2422   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2423           gnus-tmp-process-marked
2424           gnus-group-indentation
2425           (format "%5s: " gnus-tmp-number-of-unread))
2426   (gnus-put-text-property
2427    (point)
2428    (progn
2429      (insert gnus-tmp-group "\n")
2430      (1- (point)))
2431    gnus-mouse-face-prop gnus-mouse-face))
2432 (defvar gnus-group-line-format-spec
2433   (gnus-byte-code 'gnus-group-line-format-spec))
2434
2435 (defvar gnus-format-specs
2436   `((version . ,emacs-version)
2437     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2438     (summary-dummy ,gnus-summary-dummy-line-format
2439                    ,gnus-summary-dummy-line-format-spec)
2440     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2441
2442 (defvar gnus-article-mode-line-format-spec nil)
2443 (defvar gnus-summary-mode-line-format-spec nil)
2444 (defvar gnus-group-mode-line-format-spec nil)
2445
2446 ;;; Phew.  All that gruft is over, fortunately.
2447
2448 \f
2449 ;;;
2450 ;;; Gnus Utility Functions
2451 ;;;
2452
2453 (defun gnus-extract-address-components (from)
2454   (let (name address)
2455     ;; First find the address - the thing with the @ in it.  This may
2456     ;; not be accurate in mail addresses, but does the trick most of
2457     ;; the time in news messages.
2458     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2459         (setq address (substring from (match-beginning 0) (match-end 0))))
2460     ;; Then we check whether the "name <address>" format is used.
2461     (and address
2462          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2463          ;; Linear white space is not required.
2464          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2465          (and (setq name (substring from 0 (match-beginning 0)))
2466               ;; Strip any quotes from the name.
2467               (string-match "\".*\"" name)
2468               (setq name (substring name 1 (1- (match-end 0))))))
2469     ;; If not, then "address (name)" is used.
2470     (or name
2471         (and (string-match "(.+)" from)
2472              (setq name (substring from (1+ (match-beginning 0))
2473                                    (1- (match-end 0)))))
2474         (and (string-match "()" from)
2475              (setq name address))
2476         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2477         ;; XOVER might not support folded From headers.
2478         (and (string-match "(.*" from)
2479              (setq name (substring from (1+ (match-beginning 0))
2480                                    (match-end 0)))))
2481     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2482     (list (or name from) (or address from))))
2483
2484 (defun gnus-fetch-field (field)
2485   "Return the value of the header FIELD of current article."
2486   (save-excursion
2487     (save-restriction
2488       (let ((case-fold-search t)
2489             (inhibit-point-motion-hooks t))
2490         (nnheader-narrow-to-headers)
2491         (message-fetch-field field)))))
2492
2493 (defun gnus-goto-colon ()
2494   (beginning-of-line)
2495   (search-forward ":" (gnus-point-at-eol) t))
2496
2497 ;;;###autoload
2498 (defun gnus-update-format (var)
2499   "Update the format specification near point."
2500   (interactive
2501    (list
2502     (save-excursion
2503       (eval-defun nil)
2504       ;; Find the end of the current word.
2505       (re-search-forward "[ \t\n]" nil t)
2506       ;; Search backward.
2507       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2508         (match-string 1)))))
2509   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2510                               (match-string 1 var))))
2511          (entry (assq type gnus-format-specs))
2512          value spec)
2513     (when entry
2514       (setq gnus-format-specs (delq entry gnus-format-specs)))
2515     (set
2516      (intern (format "%s-spec" var))
2517      (gnus-parse-format (setq value (symbol-value (intern var)))
2518                         (symbol-value (intern (format "%s-alist" var)))
2519                         (not (string-match "mode" var))))
2520     (setq spec (symbol-value (intern (format "%s-spec" var))))
2521     (push (list type value spec) gnus-format-specs)
2522
2523     (pop-to-buffer "*Gnus Format*")
2524     (erase-buffer)
2525     (lisp-interaction-mode)
2526     (insert (pp-to-string spec))))
2527
2528 (defun gnus-update-format-specifications (&optional force)
2529   "Update all (necessary) format specifications."
2530   ;; Make the indentation array.
2531   (gnus-make-thread-indent-array)
2532
2533   ;; See whether all the stored info needs to be flushed.
2534   (when (or force
2535             (not (equal emacs-version
2536                         (cdr (assq 'version gnus-format-specs)))))
2537     (setq gnus-format-specs nil))
2538
2539   ;; Go through all the formats and see whether they need updating.
2540   (let ((types '(summary summary-dummy group
2541                          summary-mode group-mode article-mode))
2542         new-format entry type val)
2543     (while (setq type (pop types))
2544       ;; Jump to the proper buffer to find out the value of
2545       ;; the variable, if possible.  (It may be buffer-local.)
2546       (save-excursion
2547         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2548               val)
2549           (when (and (boundp buffer)
2550                      (setq val (symbol-value buffer))
2551                      (get-buffer val)
2552                      (buffer-name (get-buffer val)))
2553             (set-buffer (get-buffer val)))
2554           (setq new-format (symbol-value
2555                             (intern (format "gnus-%s-line-format" type))))))
2556       (setq entry (cdr (assq type gnus-format-specs)))
2557       (if (and entry
2558                (equal (car entry) new-format))
2559           ;; Use the old format.
2560           (set (intern (format "gnus-%s-line-format-spec" type))
2561                (cadr entry))
2562         ;; This is a new format.
2563         (setq val
2564               (if (not (stringp new-format))
2565                   ;; This is a function call or something.
2566                   new-format
2567                 ;; This is a "real" format.
2568                 (gnus-parse-format
2569                  new-format
2570                  (symbol-value
2571                   (intern (format "gnus-%s-line-format-alist"
2572                                   (if (eq type 'article-mode)
2573                                       'summary-mode type))))
2574                  (not (string-match "mode$" (symbol-name type))))))
2575         ;; Enter the new format spec into the list.
2576         (if entry
2577             (progn
2578               (setcar (cdr entry) val)
2579               (setcar entry new-format))
2580           (push (list type new-format val) gnus-format-specs))
2581         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2582
2583   (unless (assq 'version gnus-format-specs)
2584     (push (cons 'version emacs-version) gnus-format-specs))
2585
2586   (gnus-update-group-mark-positions)
2587   (gnus-update-summary-mark-positions))
2588
2589 (defun gnus-update-summary-mark-positions ()
2590   "Compute where the summary marks are to go."
2591   (save-excursion
2592     (when (and gnus-summary-buffer
2593                (get-buffer gnus-summary-buffer)
2594                (buffer-name (get-buffer gnus-summary-buffer)))
2595       (set-buffer gnus-summary-buffer))
2596     (let ((gnus-replied-mark 129)
2597           (gnus-score-below-mark 130)
2598           (gnus-score-over-mark 130)
2599           (thread nil)
2600           (gnus-visual nil)
2601           (spec gnus-summary-line-format-spec)
2602           pos)
2603       (save-excursion
2604         (gnus-set-work-buffer)
2605         (let ((gnus-summary-line-format-spec spec))
2606           (gnus-summary-insert-line
2607            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2608           (goto-char (point-min))
2609           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2610                                              (- (point) 2)))))
2611           (goto-char (point-min))
2612           (push (cons 'replied (and (search-forward "\201" nil t) 
2613                                     (- (point) 2)))
2614                 pos)
2615           (goto-char (point-min))
2616           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2617                 pos)))
2618       (setq gnus-summary-mark-positions pos))))
2619
2620 (defun gnus-update-group-mark-positions ()
2621   (save-excursion
2622     (let ((gnus-process-mark 128)
2623           (gnus-group-marked '("dummy.group"))
2624           (gnus-active-hashtb (make-vector 10 0)))
2625       (gnus-set-active "dummy.group" '(0 . 0))
2626       (gnus-set-work-buffer)
2627       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2628       (goto-char (point-min))
2629       (setq gnus-group-mark-positions
2630             (list (cons 'process (and (search-forward "\200" nil t)
2631                                       (- (point) 2))))))))
2632
2633 (defvar gnus-mouse-face-0 'highlight)
2634 (defvar gnus-mouse-face-1 'highlight)
2635 (defvar gnus-mouse-face-2 'highlight)
2636 (defvar gnus-mouse-face-3 'highlight)
2637 (defvar gnus-mouse-face-4 'highlight)
2638
2639 (defun gnus-mouse-face-function (form type)
2640   `(gnus-put-text-property
2641     (point) (progn ,@form (point))
2642     gnus-mouse-face-prop
2643     ,(if (equal type 0)
2644          'gnus-mouse-face
2645        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2646
2647 (defvar gnus-face-0 'bold)
2648 (defvar gnus-face-1 'italic)
2649 (defvar gnus-face-2 'bold-italic)
2650 (defvar gnus-face-3 'bold)
2651 (defvar gnus-face-4 'bold)
2652
2653 (defun gnus-face-face-function (form type)
2654   `(gnus-put-text-property
2655     (point) (progn ,@form (point))
2656     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2657
2658 (defun gnus-max-width-function (el max-width)
2659   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2660   (if (symbolp el)
2661       `(if (> (length ,el) ,max-width)
2662            (substring ,el 0 ,max-width)
2663          ,el)
2664     `(let ((val (eval ,el)))
2665        (if (numberp val)
2666            (setq val (int-to-string val)))
2667        (if (> (length val) ,max-width)
2668            (substring val 0 ,max-width)
2669          val))))
2670
2671 (defun gnus-parse-format (format spec-alist &optional insert)
2672   ;; This function parses the FORMAT string with the help of the
2673   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2674   ;; string.  If the FORMAT string contains the specifiers %( and %)
2675   ;; the text between them will have the mouse-face text property.
2676   (if (string-match
2677        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2678        format)
2679       (gnus-parse-complex-format format spec-alist)
2680     ;; This is a simple format.
2681     (gnus-parse-simple-format format spec-alist insert)))
2682
2683 (defun gnus-parse-complex-format (format spec-alist)
2684   (save-excursion
2685     (gnus-set-work-buffer)
2686     (insert format)
2687     (goto-char (point-min))
2688     (while (re-search-forward "\"" nil t)
2689       (replace-match "\\\"" nil t))
2690     (goto-char (point-min))
2691     (insert "(\"")
2692     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2693       (let ((number (if (match-beginning 1)
2694                         (match-string 1) "0"))
2695             (delim (aref (match-string 2) 0)))
2696         (if (or (= delim ?\() (= delim ?\{))
2697             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2698                                    " " number " \""))
2699           (replace-match "\")\""))))
2700     (goto-char (point-max))
2701     (insert "\")")
2702     (goto-char (point-min))
2703     (let ((form (read (current-buffer))))
2704       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2705
2706 (defun gnus-complex-form-to-spec (form spec-alist)
2707   (delq nil
2708         (mapcar
2709          (lambda (sform)
2710            (if (stringp sform)
2711                (gnus-parse-simple-format sform spec-alist t)
2712              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2713                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2714                       (nth 1 sform))))
2715          form)))
2716
2717 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2718   ;; This function parses the FORMAT string with the help of the
2719   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2720   ;; string.
2721   (let ((max-width 0)
2722         spec flist fstring newspec elem beg result dontinsert)
2723     (save-excursion
2724       (gnus-set-work-buffer)
2725       (insert format)
2726       (goto-char (point-min))
2727       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2728                                 nil t)
2729         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2730               (setq newspec "%"
2731                     beg (1+ (match-beginning 0)))
2732           ;; First check if there are any specs that look anything like
2733           ;; "%12,12A", ie. with a "max width specification".  These have
2734           ;; to be treated specially.
2735           (if (setq beg (match-beginning 1))
2736               (setq max-width
2737                     (string-to-int
2738                      (buffer-substring
2739                       (1+ (match-beginning 1)) (match-end 1))))
2740             (setq max-width 0)
2741             (setq beg (match-beginning 2)))
2742           ;; Find the specification from `spec-alist'.
2743           (unless (setq elem (cdr (assq spec spec-alist)))
2744             (setq elem '("*" ?s)))
2745           ;; Treat user defined format specifiers specially.
2746           (when (eq (car elem) 'gnus-tmp-user-defined)
2747             (setq elem
2748                   (list
2749                    (list (intern (concat "gnus-user-format-function-"
2750                                          (match-string 3)))
2751                          'gnus-tmp-header) ?s))
2752             (delete-region (match-beginning 3) (match-end 3)))
2753           (if (not (zerop max-width))
2754               (let ((el (car elem)))
2755                 (cond ((= (cadr elem) ?c)
2756                        (setq el (list 'char-to-string el)))
2757                       ((= (cadr elem) ?d)
2758                        (setq el (list 'int-to-string el))))
2759                 (setq flist (cons (gnus-max-width-function el max-width)
2760                                   flist))
2761                 (setq newspec ?s))
2762             (progn
2763               (setq flist (cons (car elem) flist))
2764               (setq newspec (cadr elem)))))
2765         ;; Remove the old specification (and possibly a ",12" string).
2766         (delete-region beg (match-end 2))
2767         ;; Insert the new specification.
2768         (goto-char beg)
2769         (insert newspec))
2770       (setq fstring (buffer-substring 1 (point-max))))
2771     ;; Do some postprocessing to increase efficiency.
2772     (setq
2773      result
2774      (cond
2775       ;; Emptyness.
2776       ((string= fstring "")
2777        nil)
2778       ;; Not a format string.
2779       ((not (string-match "%" fstring))
2780        (list fstring))
2781       ;; A format string with just a single string spec.
2782       ((string= fstring "%s")
2783        (list (car flist)))
2784       ;; A single character.
2785       ((string= fstring "%c")
2786        (list (car flist)))
2787       ;; A single number.
2788       ((string= fstring "%d")
2789        (setq dontinsert)
2790        (if insert
2791            (list `(princ ,(car flist)))
2792          (list `(int-to-string ,(car flist)))))
2793       ;; Just lots of chars and strings.
2794       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2795        (nreverse flist))
2796       ;; A single string spec at the beginning of the spec.
2797       ((string-match "\\`%[sc][^%]+\\'" fstring)
2798        (list (car flist) (substring fstring 2)))
2799       ;; A single string spec in the middle of the spec.
2800       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2801        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2802       ;; A single string spec in the end of the spec.
2803       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2804        (list (match-string 1 fstring) (car flist)))
2805       ;; A more complex spec.
2806       (t
2807        (list (cons 'format (cons fstring (nreverse flist)))))))
2808
2809     (if insert
2810         (when result
2811           (if dontinsert
2812               result
2813             (cons 'insert result)))
2814       (cond ((stringp result)
2815              result)
2816             ((consp result)
2817              (cons 'concat result))
2818             (t "")))))
2819
2820 (defun gnus-eval-format (format &optional alist props)
2821   "Eval the format variable FORMAT, using ALIST.
2822 If PROPS, insert the result."
2823   (let ((form (gnus-parse-format format alist props)))
2824     (if props
2825         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2826       (eval form))))
2827
2828 (defun gnus-remove-text-with-property (prop)
2829   "Delete all text in the current buffer with text property PROP."
2830   (save-excursion
2831     (goto-char (point-min))
2832     (while (not (eobp))
2833       (while (get-text-property (point) prop)
2834         (delete-char 1))
2835       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2836
2837 (defun gnus-set-work-buffer ()
2838   (if (get-buffer gnus-work-buffer)
2839       (progn
2840         (set-buffer gnus-work-buffer)
2841         (erase-buffer))
2842     (set-buffer (get-buffer-create gnus-work-buffer))
2843     (kill-all-local-variables)
2844     (buffer-disable-undo (current-buffer))
2845     (gnus-add-current-to-buffer-list)))
2846
2847 ;; Article file names when saving.
2848
2849 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2850   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2851 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2852 Otherwise, it is like ~/News/news/group/num."
2853   (let ((default
2854           (expand-file-name
2855            (concat (if (gnus-use-long-file-name 'not-save)
2856                        (gnus-capitalize-newsgroup newsgroup)
2857                      (gnus-newsgroup-directory-form newsgroup))
2858                    "/" (int-to-string (mail-header-number headers)))
2859            gnus-article-save-directory)))
2860     (if (and last-file
2861              (string-equal (file-name-directory default)
2862                            (file-name-directory last-file))
2863              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2864         default
2865       (or last-file default))))
2866
2867 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2868   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2869 If variable `gnus-use-long-file-name' is non-nil, it is
2870 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2871   (let ((default
2872           (expand-file-name
2873            (concat (if (gnus-use-long-file-name 'not-save)
2874                        newsgroup
2875                      (gnus-newsgroup-directory-form newsgroup))
2876                    "/" (int-to-string (mail-header-number headers)))
2877            gnus-article-save-directory)))
2878     (if (and last-file
2879              (string-equal (file-name-directory default)
2880                            (file-name-directory last-file))
2881              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2882         default
2883       (or last-file default))))
2884
2885 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2886   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2887 If variable `gnus-use-long-file-name' is non-nil, it is
2888 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2889   (or last-file
2890       (expand-file-name
2891        (if (gnus-use-long-file-name 'not-save)
2892            (gnus-capitalize-newsgroup newsgroup)
2893          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2894        gnus-article-save-directory)))
2895
2896 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2897   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2898 If variable `gnus-use-long-file-name' is non-nil, it is
2899 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2900   (or last-file
2901       (expand-file-name
2902        (if (gnus-use-long-file-name 'not-save)
2903            newsgroup
2904          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2905        gnus-article-save-directory)))
2906
2907 ;; For subscribing new newsgroup
2908
2909 (defun gnus-subscribe-hierarchical-interactive (groups)
2910   (let ((groups (sort groups 'string<))
2911         prefixes prefix start ans group starts)
2912     (while groups
2913       (setq prefixes (list "^"))
2914       (while (and groups prefixes)
2915         (while (not (string-match (car prefixes) (car groups)))
2916           (setq prefixes (cdr prefixes)))
2917         (setq prefix (car prefixes))
2918         (setq start (1- (length prefix)))
2919         (if (and (string-match "[^\\.]\\." (car groups) start)
2920                  (cdr groups)
2921                  (setq prefix
2922                        (concat "^" (substring (car groups) 0 (match-end 0))))
2923                  (string-match prefix (cadr groups)))
2924             (progn
2925               (setq prefixes (cons prefix prefixes))
2926               (message "Descend hierarchy %s? ([y]nsq): "
2927                        (substring prefix 1 (1- (length prefix))))
2928               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2929                 (ding)
2930                 (message "Descend hierarchy %s? ([y]nsq): "
2931                          (substring prefix 1 (1- (length prefix)))))
2932               (cond ((= ans ?n)
2933                      (while (and groups
2934                                  (string-match prefix
2935                                                (setq group (car groups))))
2936                        (setq gnus-killed-list
2937                              (cons group gnus-killed-list))
2938                        (gnus-sethash group group gnus-killed-hashtb)
2939                        (setq groups (cdr groups)))
2940                      (setq starts (cdr starts)))
2941                     ((= ans ?s)
2942                      (while (and groups
2943                                  (string-match prefix
2944                                                (setq group (car groups))))
2945                        (gnus-sethash group group gnus-killed-hashtb)
2946                        (gnus-subscribe-alphabetically (car groups))
2947                        (setq groups (cdr groups)))
2948                      (setq starts (cdr starts)))
2949                     ((= ans ?q)
2950                      (while groups
2951                        (setq group (car groups))
2952                        (setq gnus-killed-list (cons group gnus-killed-list))
2953                        (gnus-sethash group group gnus-killed-hashtb)
2954                        (setq groups (cdr groups))))
2955                     (t nil)))
2956           (message "Subscribe %s? ([n]yq)" (car groups))
2957           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2958             (ding)
2959             (message "Subscribe %s? ([n]yq)" (car groups)))
2960           (setq group (car groups))
2961           (cond ((= ans ?y)
2962                  (gnus-subscribe-alphabetically (car groups))
2963                  (gnus-sethash group group gnus-killed-hashtb))
2964                 ((= ans ?q)
2965                  (while groups
2966                    (setq group (car groups))
2967                    (setq gnus-killed-list (cons group gnus-killed-list))
2968                    (gnus-sethash group group gnus-killed-hashtb)
2969                    (setq groups (cdr groups))))
2970                 (t
2971                  (setq gnus-killed-list (cons group gnus-killed-list))
2972                  (gnus-sethash group group gnus-killed-hashtb)))
2973           (setq groups (cdr groups)))))))
2974
2975 (defun gnus-subscribe-randomly (newsgroup)
2976   "Subscribe new NEWSGROUP by making it the first newsgroup."
2977   (gnus-subscribe-newsgroup newsgroup))
2978
2979 (defun gnus-subscribe-alphabetically (newgroup)
2980   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2981   (let ((groups (cdr gnus-newsrc-alist))
2982         before)
2983     (while (and (not before) groups)
2984       (if (string< newgroup (caar groups))
2985           (setq before (caar groups))
2986         (setq groups (cdr groups))))
2987     (gnus-subscribe-newsgroup newgroup before)))
2988
2989 (defun gnus-subscribe-hierarchically (newgroup)
2990   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2991   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2992   (save-excursion
2993     (set-buffer (find-file-noselect gnus-current-startup-file))
2994     (let ((groupkey newgroup)
2995           before)
2996       (while (and (not before) groupkey)
2997         (goto-char (point-min))
2998         (let ((groupkey-re
2999                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3000           (while (and (re-search-forward groupkey-re nil t)
3001                       (progn
3002                         (setq before (match-string 1))
3003                         (string< before newgroup)))))
3004         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3005         (setq groupkey
3006               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3007                   (substring groupkey (match-beginning 1) (match-end 1)))))
3008       (gnus-subscribe-newsgroup newgroup before))))
3009
3010 (defun gnus-subscribe-interactively (group)
3011   "Subscribe the new GROUP interactively.
3012 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3013 it is killed."
3014   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3015       (gnus-subscribe-hierarchically group)
3016     (push group gnus-killed-list)))
3017
3018 (defun gnus-subscribe-zombies (group)
3019   "Make the new GROUP into a zombie group."
3020   (push group gnus-zombie-list))
3021
3022 (defun gnus-subscribe-killed (group)
3023   "Make the new GROUP a killed group."
3024   (push group gnus-killed-list))
3025
3026 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3027   "Subscribe new NEWSGROUP.
3028 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3029 the first newsgroup."
3030   ;; We subscribe the group by changing its level to `subscribed'.
3031   (gnus-group-change-level
3032    newsgroup gnus-level-default-subscribed
3033    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3034   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3035
3036 ;; For directories
3037
3038 (defun gnus-newsgroup-directory-form (newsgroup)
3039   "Make hierarchical directory name from NEWSGROUP name."
3040   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3041         (len (length newsgroup))
3042         idx)
3043     ;; If this is a foreign group, we don't want to translate the
3044     ;; entire name.
3045     (if (setq idx (string-match ":" newsgroup))
3046         (aset newsgroup idx ?/)
3047       (setq idx 0))
3048     ;; Replace all occurrences of `.' with `/'.
3049     (while (< idx len)
3050       (if (= (aref newsgroup idx) ?.)
3051           (aset newsgroup idx ?/))
3052       (setq idx (1+ idx)))
3053     newsgroup))
3054
3055 (defun gnus-newsgroup-savable-name (group)
3056   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3057   ;; with dots.
3058   (nnheader-replace-chars-in-string group ?/ ?.))
3059
3060 (defun gnus-make-directory (dir)
3061   "Make DIRECTORY recursively."
3062   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3063   ;; of the many mysteries of the universe.
3064   (let* ((dir (expand-file-name dir default-directory))
3065          dirs err)
3066     (if (string-match "/$" dir)
3067         (setq dir (substring dir 0 (match-beginning 0))))
3068     ;; First go down the path until we find a directory that exists.
3069     (while (not (file-exists-p dir))
3070       (setq dirs (cons dir dirs))
3071       (string-match "/[^/]+$" dir)
3072       (setq dir (substring dir 0 (match-beginning 0))))
3073     ;; Then create all the subdirs.
3074     (while (and dirs (not err))
3075       (condition-case ()
3076           (make-directory (car dirs))
3077         (error (setq err t)))
3078       (setq dirs (cdr dirs)))
3079     ;; We return whether we were successful or not.
3080     (not dirs)))
3081
3082 (defun gnus-capitalize-newsgroup (newsgroup)
3083   "Capitalize NEWSGROUP name."
3084   (and (not (zerop (length newsgroup)))
3085        (concat (char-to-string (upcase (aref newsgroup 0)))
3086                (substring newsgroup 1))))
3087
3088 ;; Various... things.
3089
3090 (defun gnus-simplify-subject (subject &optional re-only)
3091   "Remove `Re:' and words in parentheses.
3092 If RE-ONLY is non-nil, strip leading `Re:'s only."
3093   (let ((case-fold-search t))           ;Ignore case.
3094     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3095     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3096       (setq subject (substring subject (match-end 0))))
3097     ;; Remove uninteresting prefixes.
3098     (if (and (not re-only)
3099              gnus-simplify-ignored-prefixes
3100              (string-match gnus-simplify-ignored-prefixes subject))
3101         (setq subject (substring subject (match-end 0))))
3102     ;; Remove words in parentheses from end.
3103     (unless re-only
3104       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3105         (setq subject (substring subject 0 (match-beginning 0)))))
3106     ;; Return subject string.
3107     subject))
3108
3109 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3110 ;; all whitespace.
3111 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3112 (defun gnus-simplify-buffer-fuzzy ()
3113   (goto-char (point-min))
3114   (while (search-forward "\t" nil t)
3115     (replace-match " " t t))
3116   (goto-char (point-min))
3117   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3118   (goto-char (match-beginning 0))
3119   (while (or
3120           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3121           (looking-at "^[[].*: .*[]]$"))
3122     (goto-char (point-min))
3123     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3124                               nil t)
3125       (replace-match "" t t))
3126     (goto-char (point-min))
3127     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3128       (goto-char (match-end 0))
3129       (delete-char -1)
3130       (delete-region
3131        (progn (goto-char (match-beginning 0)))
3132        (re-search-forward ":"))))
3133   (goto-char (point-min))
3134   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3135     (replace-match "" t t))
3136   (goto-char (point-min))
3137   (while (re-search-forward "  +" nil t)
3138     (replace-match " " t t))
3139   (goto-char (point-min))
3140   (while (re-search-forward " $" nil t)
3141     (replace-match "" t t))
3142   (goto-char (point-min))
3143   (while (re-search-forward "^ +" nil t)
3144     (replace-match "" t t))
3145   (goto-char (point-min))
3146   (when gnus-simplify-subject-fuzzy-regexp
3147     (if (listp gnus-simplify-subject-fuzzy-regexp)
3148         (let ((list gnus-simplify-subject-fuzzy-regexp))
3149           (while list
3150             (goto-char (point-min))
3151             (while (re-search-forward (car list) nil t)
3152               (replace-match "" t t))
3153             (setq list (cdr list))))
3154       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3155         (replace-match "" t t)))))
3156
3157 (defun gnus-simplify-subject-fuzzy (subject)
3158   "Siplify a subject string fuzzily."
3159   (save-excursion
3160     (gnus-set-work-buffer)
3161     (let ((case-fold-search t))
3162       (insert subject)
3163       (inline (gnus-simplify-buffer-fuzzy))
3164       (buffer-string))))
3165
3166 ;; Add the current buffer to the list of buffers to be killed on exit.
3167 (defun gnus-add-current-to-buffer-list ()
3168   (or (memq (current-buffer) gnus-buffer-list)
3169       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3170
3171 (defun gnus-string> (s1 s2)
3172   (not (or (string< s1 s2)
3173            (string= s1 s2))))
3174
3175 (defun gnus-read-active-file-p ()
3176   "Say whether the active file has been read from `gnus-select-method'."
3177   (memq gnus-select-method gnus-have-read-active-file))
3178
3179 ;;; General various misc type functions.
3180
3181 (defun gnus-clear-system ()
3182   "Clear all variables and buffers."
3183   ;; Clear Gnus variables.
3184   (let ((variables gnus-variable-list))
3185     (while variables
3186       (set (car variables) nil)
3187       (setq variables (cdr variables))))
3188   ;; Clear other internal variables.
3189   (setq gnus-list-of-killed-groups nil
3190         gnus-have-read-active-file nil
3191         gnus-newsrc-alist nil
3192         gnus-newsrc-hashtb nil
3193         gnus-killed-list nil
3194         gnus-zombie-list nil
3195         gnus-killed-hashtb nil
3196         gnus-active-hashtb nil
3197         gnus-moderated-list nil
3198         gnus-description-hashtb nil
3199         gnus-current-headers nil
3200         gnus-thread-indent-array nil
3201         gnus-newsgroup-headers nil
3202         gnus-newsgroup-name nil
3203         gnus-server-alist nil
3204         gnus-group-list-mode nil
3205         gnus-opened-servers nil
3206         gnus-current-select-method nil)
3207   (gnus-shutdown 'gnus)
3208   ;; Kill the startup file.
3209   (and gnus-current-startup-file
3210        (get-file-buffer gnus-current-startup-file)
3211        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3212   ;; Clear the dribble buffer.
3213   (gnus-dribble-clear)
3214   ;; Kill global KILL file buffer.
3215   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3216     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3217   (gnus-kill-buffer nntp-server-buffer)
3218   ;; Kill Gnus buffers.
3219   (while gnus-buffer-list
3220     (gnus-kill-buffer (pop gnus-buffer-list)))
3221   ;; Remove Gnus frames.
3222   (while gnus-created-frames
3223     (when (frame-live-p (car gnus-created-frames))
3224       ;; We slap a condition-case around this `delete-frame' to ensure 
3225       ;; agains errors if we try do delete the single frame that's left.
3226       (condition-case ()
3227           (delete-frame (car gnus-created-frames))
3228         (error nil)))
3229     (pop gnus-created-frames)))
3230
3231 (defun gnus-windows-old-to-new (setting)
3232   ;; First we take care of the really, really old Gnus 3 actions.
3233   (when (symbolp setting)
3234     (setq setting
3235           ;; Take care of ooold GNUS 3.x values.
3236           (cond ((eq setting 'SelectArticle) 'article)
3237                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3238                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3239                 (t setting))))
3240   (if (or (listp setting)
3241           (not (and gnus-window-configuration
3242                     (memq setting '(group summary article)))))
3243       setting
3244     (let* ((setting (if (eq setting 'group)
3245                         (if (assq 'newsgroup gnus-window-configuration)
3246                             'newsgroup
3247                           'newsgroups) setting))
3248            (elem (cadr (assq setting gnus-window-configuration)))
3249            (total (apply '+ elem))
3250            (types '(group summary article))
3251            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3252            (i 0)
3253            perc
3254            out)
3255       (while (< i 3)
3256         (or (not (numberp (nth i elem)))
3257             (zerop (nth i elem))
3258             (progn
3259               (setq perc  (/ (float (nth 0 elem)) total))
3260               (setq out (cons (if (eq pbuf (nth i types))
3261                                   (vector (nth i types) perc 'point)
3262                                 (vector (nth i types) perc))
3263                               out))))
3264         (setq i (1+ i)))
3265       (list (nreverse out)))))
3266
3267 ;;;###autoload
3268 (defun gnus-add-configuration (conf)
3269   "Add the window configuration CONF to `gnus-buffer-configuration'."
3270   (setq gnus-buffer-configuration
3271         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3272                          gnus-buffer-configuration))))
3273
3274 (defvar gnus-frame-list nil)
3275
3276 (defun gnus-configure-frame (split &optional window)
3277   "Split WINDOW according to SPLIT."
3278   (unless window
3279     (setq window (get-buffer-window (current-buffer))))
3280   (select-window window)
3281   ;; This might be an old-stylee buffer config.
3282   (when (vectorp split)
3283     (setq split (append split nil)))
3284   (when (or (consp (car split))
3285             (vectorp (car split)))
3286     (push 1.0 split)
3287     (push 'vertical split))
3288   ;; The SPLIT might be something that is to be evaled to
3289   ;; return a new SPLIT.
3290   (while (and (not (assq (car split) gnus-window-to-buffer))
3291               (gnus-functionp (car split)))
3292     (setq split (eval split)))
3293   (let* ((type (car split))
3294          (subs (cddr split))
3295          (len (if (eq type 'horizontal) (window-width) (window-height)))
3296          (total 0)
3297          (window-min-width (or gnus-window-min-width window-min-width))
3298          (window-min-height (or gnus-window-min-height window-min-height))
3299          s result new-win rest comp-subs size sub)
3300     (cond
3301      ;; Nothing to do here.
3302      ((null split))
3303      ;; Don't switch buffers.
3304      ((null type)
3305       (and (memq 'point split) window))
3306      ;; This is a buffer to be selected.
3307      ((not (memq type '(frame horizontal vertical)))
3308       (let ((buffer (cond ((stringp type) type)
3309                           (t (cdr (assq type gnus-window-to-buffer)))))
3310             buf)
3311         (unless buffer
3312           (error "Illegal buffer type: %s" type))
3313         (unless (setq buf (get-buffer (if (symbolp buffer)
3314                                           (symbol-value buffer) buffer)))
3315           (setq buf (get-buffer-create (if (symbolp buffer)
3316                                            (symbol-value buffer) buffer))))
3317         (switch-to-buffer buf)
3318         ;; We return the window if it has the `point' spec.
3319         (and (memq 'point split) window)))
3320      ;; This is a frame split.
3321      ((eq type 'frame)
3322       (unless gnus-frame-list
3323         (setq gnus-frame-list (list (window-frame
3324                                      (get-buffer-window (current-buffer))))))
3325       (let ((i 0)
3326             params frame fresult)
3327         (while (< i (length subs))
3328           ;; Frame parameter is gotten from the sub-split.
3329           (setq params (cadr (elt subs i)))
3330           ;; It should be a list.
3331           (unless (listp params)
3332             (setq params nil))
3333           ;; Create a new frame?
3334           (unless (setq frame (elt gnus-frame-list i))
3335             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3336             (push frame gnus-created-frames))
3337           ;; Is the old frame still alive?
3338           (unless (frame-live-p frame)
3339             (setcar (nthcdr i gnus-frame-list)
3340                     (setq frame (make-frame params))))
3341           ;; Select the frame in question and do more splits there.
3342           (select-frame frame)
3343           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3344           (incf i))
3345         ;; Select the frame that has the selected buffer.
3346         (when fresult
3347           (select-frame (window-frame fresult)))))
3348      ;; This is a normal split.
3349      (t
3350       (when (> (length subs) 0)
3351         ;; First we have to compute the sizes of all new windows.
3352         (while subs
3353           (setq sub (append (pop subs) nil))
3354           (while (and (not (assq (car sub) gnus-window-to-buffer))
3355                       (gnus-functionp (car sub)))
3356             (setq sub (eval sub)))
3357           (when sub
3358             (push sub comp-subs)
3359             (setq size (cadar comp-subs))
3360             (cond ((equal size 1.0)
3361                    (setq rest (car comp-subs))
3362                    (setq s 0))
3363                   ((floatp size)
3364                    (setq s (floor (* size len))))
3365                   ((integerp size)
3366                    (setq s size))
3367                   (t
3368                    (error "Illegal size: %s" size)))
3369             ;; Try to make sure that we are inside the safe limits.
3370             (cond ((zerop s))
3371                   ((eq type 'horizontal)
3372                    (setq s (max s window-min-width)))
3373                   ((eq type 'vertical)
3374                    (setq s (max s window-min-height))))
3375             (setcar (cdar comp-subs) s)
3376             (incf total s)))
3377         ;; Take care of the "1.0" spec.
3378         (if rest
3379             (setcar (cdr rest) (- len total))
3380           (error "No 1.0 specs in %s" split))
3381         ;; The we do the actual splitting in a nice recursive
3382         ;; fashion.
3383         (setq comp-subs (nreverse comp-subs))
3384         (while comp-subs
3385           (if (null (cdr comp-subs))
3386               (setq new-win window)
3387             (setq new-win
3388                   (split-window window (cadar comp-subs)
3389                                 (eq type 'horizontal))))
3390           (setq result (or (gnus-configure-frame
3391                             (car comp-subs) window) result))
3392           (select-window new-win)
3393           (setq window new-win)
3394           (setq comp-subs (cdr comp-subs))))
3395       ;; Return the proper window, if any.
3396       (when result
3397         (select-window result))))))
3398
3399 (defvar gnus-frame-split-p nil)
3400
3401 (defun gnus-configure-windows (setting &optional force)
3402   (setq setting (gnus-windows-old-to-new setting))
3403   (let ((split (if (symbolp setting)
3404                    (cadr (assq setting gnus-buffer-configuration))
3405                  setting))
3406         all-visible)
3407
3408     (setq gnus-frame-split-p nil)
3409
3410     (unless split
3411       (error "No such setting: %s" setting))
3412
3413     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3414              (not force))
3415         ;; All the windows mentioned are already visible, so we just
3416         ;; put point in the assigned buffer, and do not touch the
3417         ;; winconf.
3418         (select-window all-visible)
3419
3420       ;; Either remove all windows or just remove all Gnus windows.
3421       (let ((frame (selected-frame)))
3422         (unwind-protect
3423             (if gnus-use-full-window
3424                 ;; We want to remove all other windows.
3425                 (if (not gnus-frame-split-p)
3426                     ;; This is not a `frame' split, so we ignore the
3427                     ;; other frames.  
3428                     (delete-other-windows)
3429                   ;; This is a `frame' split, so we delete all windows
3430                   ;; on all frames.
3431                   (mapcar 
3432                    (lambda (frame)
3433                      (unless (eq (cdr (assq 'minibuffer
3434                                             (frame-parameters frame)))
3435                                  'only)
3436                        (select-frame frame)
3437                        (delete-other-windows)))
3438                    (frame-list)))
3439               ;; Just remove some windows.
3440               (gnus-remove-some-windows)
3441               (switch-to-buffer nntp-server-buffer))
3442           (select-frame frame)))
3443
3444       (switch-to-buffer nntp-server-buffer)
3445       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3446
3447 (defun gnus-all-windows-visible-p (split)
3448   "Say whether all buffers in SPLIT are currently visible.
3449 In particular, the value returned will be the window that
3450 should have point."
3451   (let ((stack (list split))
3452         (all-visible t)
3453         type buffer win buf)
3454     (while (and (setq split (pop stack))
3455                 all-visible)
3456       ;; Be backwards compatible.
3457       (when (vectorp split)
3458         (setq split (append split nil)))
3459       (when (or (consp (car split))
3460                 (vectorp (car split)))
3461         (push 1.0 split)
3462         (push 'vertical split))
3463       ;; The SPLIT might be something that is to be evaled to
3464       ;; return a new SPLIT.
3465       (while (and (not (assq (car split) gnus-window-to-buffer))
3466                   (gnus-functionp (car split)))
3467         (setq split (eval split)))
3468
3469       (setq type (elt split 0))
3470       (cond
3471        ;; Nothing here.
3472        ((null split) t)
3473        ;; A buffer.
3474        ((not (memq type '(horizontal vertical frame)))
3475         (setq buffer (cond ((stringp type) type)
3476                            (t (cdr (assq type gnus-window-to-buffer)))))
3477         (unless buffer
3478           (error "Illegal buffer type: %s" type))
3479         (when (setq buf (get-buffer (if (symbolp buffer)
3480                                         (symbol-value buffer)
3481                                       buffer)))
3482           (setq win (get-buffer-window buf t)))
3483         (if win
3484             (when (memq 'point split)
3485                 (setq all-visible win))
3486           (setq all-visible nil)))
3487        (t
3488         (when (eq type 'frame)
3489           (setq gnus-frame-split-p t))
3490         (setq stack (append (cddr split) stack)))))
3491     (unless (eq all-visible t)
3492       all-visible)))
3493
3494 (defun gnus-window-top-edge (&optional window)
3495   (nth 1 (window-edges window)))
3496
3497 (defun gnus-remove-some-windows ()
3498   (let ((buffers gnus-window-to-buffer)
3499         buf bufs lowest-buf lowest)
3500     (save-excursion
3501       ;; Remove windows on all known Gnus buffers.
3502       (while buffers
3503         (setq buf (cdar buffers))
3504         (if (symbolp buf)
3505             (setq buf (and (boundp buf) (symbol-value buf))))
3506         (and buf
3507              (get-buffer-window buf)
3508              (progn
3509                (setq bufs (cons buf bufs))
3510                (pop-to-buffer buf)
3511                (if (or (not lowest)
3512                        (< (gnus-window-top-edge) lowest))
3513                    (progn
3514                      (setq lowest (gnus-window-top-edge))
3515                      (setq lowest-buf buf)))))
3516         (setq buffers (cdr buffers)))
3517       ;; Remove windows on *all* summary buffers.
3518       (walk-windows
3519        (lambda (win)
3520          (let ((buf (window-buffer win)))
3521            (if (string-match    "^\\*Summary" (buffer-name buf))
3522                (progn
3523                  (setq bufs (cons buf bufs))
3524                  (pop-to-buffer buf)
3525                  (if (or (not lowest)
3526                          (< (gnus-window-top-edge) lowest))
3527                      (progn
3528                        (setq lowest-buf buf)
3529                        (setq lowest (gnus-window-top-edge)))))))))
3530       (and lowest-buf
3531            (progn
3532              (pop-to-buffer lowest-buf)
3533              (switch-to-buffer nntp-server-buffer)))
3534       (while bufs
3535         (and (not (eq (car bufs) lowest-buf))
3536              (delete-windows-on (car bufs)))
3537         (setq bufs (cdr bufs))))))
3538
3539 (defun gnus-version (&optional arg)
3540   "Version number of this version of Gnus.
3541 If ARG, insert string at point."
3542   (interactive "P")
3543   (let ((methods gnus-valid-select-methods)
3544         (mess gnus-version)
3545         meth)
3546     ;; Go through all the legal select methods and add their version
3547     ;; numbers to the total version string.  Only the backends that are
3548     ;; currently in use will have their message numbers taken into
3549     ;; consideration.
3550     (while methods
3551       (setq meth (intern (concat (caar methods) "-version")))
3552       (and (boundp meth)
3553            (stringp (symbol-value meth))
3554            (setq mess (concat mess "; " (symbol-value meth))))
3555       (setq methods (cdr methods)))
3556     (if arg
3557         (insert (message mess))
3558       (message mess))))
3559
3560 (defun gnus-info-find-node ()
3561   "Find Info documentation of Gnus."
3562   (interactive)
3563   ;; Enlarge info window if needed.
3564   (let ((mode major-mode)
3565         gnus-info-buffer)
3566     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3567     (setq gnus-info-buffer (current-buffer))
3568     (gnus-configure-windows 'info)))
3569
3570 (defun gnus-days-between (date1 date2)
3571   ;; Return the number of days between date1 and date2.
3572   (- (gnus-day-number date1) (gnus-day-number date2)))
3573
3574 (defun gnus-day-number (date)
3575   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3576                      (timezone-parse-date date))))
3577     (timezone-absolute-from-gregorian
3578      (nth 1 dat) (nth 2 dat) (car dat))))
3579
3580 (defun gnus-encode-date (date)
3581   "Convert DATE to internal time."
3582   (let* ((parse (timezone-parse-date date))
3583          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3584          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3585     (encode-time (caddr time) (cadr time) (car time)
3586                  (caddr date) (cadr date) (car date) (nth 4 date))))
3587
3588 (defun gnus-time-minus (t1 t2)
3589   "Subtract two internal times."
3590   (let ((borrow (< (cadr t1) (cadr t2))))
3591     (list (- (car t1) (car t2) (if borrow 1 0))
3592           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3593
3594 (defun gnus-file-newer-than (file date)
3595   (let ((fdate (nth 5 (file-attributes file))))
3596     (or (> (car fdate) (car date))
3597         (and (= (car fdate) (car date))
3598              (> (nth 1 fdate) (nth 1 date))))))
3599
3600 (defmacro gnus-local-set-keys (&rest plist)
3601   "Set the keys in PLIST in the current keymap."
3602   `(gnus-define-keys-1 (current-local-map) ',plist))
3603
3604 (defmacro gnus-define-keys (keymap &rest plist)
3605   "Define all keys in PLIST in KEYMAP."
3606   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3607
3608 (put 'gnus-define-keys 'lisp-indent-function 1)
3609 (put 'gnus-define-keys 'lisp-indent-hook 1)
3610 (put 'gnus-define-keymap 'lisp-indent-function 1)
3611 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3612
3613 (defmacro gnus-define-keymap (keymap &rest plist)
3614   "Define all keys in PLIST in KEYMAP."
3615   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3616
3617 (defun gnus-define-keys-1 (keymap plist)
3618   (when (null keymap)
3619     (error "Can't set keys in a null keymap"))
3620   (cond ((symbolp keymap)
3621          (setq keymap (symbol-value keymap)))
3622         ((keymapp keymap))
3623         ((listp keymap)
3624          (set (car keymap) nil)
3625          (define-prefix-command (car keymap))
3626          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3627          (setq keymap (symbol-value (car keymap)))))
3628   (let (key)
3629     (while plist
3630       (when (symbolp (setq key (pop plist)))
3631         (setq key (symbol-value key)))
3632       (define-key keymap key (pop plist)))))
3633
3634 (defun gnus-group-read-only-p (&optional group)
3635   "Check whether GROUP supports editing or not.
3636 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3637 that that variable is buffer-local to the summary buffers."
3638   (let ((group (or group gnus-newsgroup-name)))
3639     (not (gnus-check-backend-function 'request-replace-article group))))
3640
3641 (defun gnus-group-total-expirable-p (group)
3642   "Check whether GROUP is total-expirable or not."
3643   (let ((params (gnus-info-params (gnus-get-info group))))
3644     (or (memq 'total-expire params)
3645         (cdr (assq 'total-expire params)) ; (total-expire . t)
3646         (and gnus-total-expirable-newsgroups ; Check var.
3647              (string-match gnus-total-expirable-newsgroups group)))))
3648
3649 (defun gnus-group-auto-expirable-p (group)
3650   "Check whether GROUP is total-expirable or not."
3651   (let ((params (gnus-info-params (gnus-get-info group))))
3652     (or (memq 'auto-expire params)
3653         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3654         (and gnus-auto-expirable-newsgroups ; Check var.
3655              (string-match gnus-auto-expirable-newsgroups group)))))
3656
3657 (defun gnus-virtual-group-p (group)
3658   "Say whether GROUP is virtual or not."
3659   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3660                         gnus-valid-select-methods)))
3661
3662 (defun gnus-news-group-p (group &optional article)
3663   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3664   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3665       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3666            (eq (gnus-request-type group article) 'news))))
3667
3668 (defsubst gnus-simplify-subject-fully (subject)
3669   "Simplify a subject string according to the user's wishes."
3670   (cond
3671    ((null gnus-summary-gather-subject-limit)
3672     (gnus-simplify-subject-re subject))
3673    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3674     (gnus-simplify-subject-fuzzy subject))
3675    ((numberp gnus-summary-gather-subject-limit)
3676     (gnus-limit-string (gnus-simplify-subject-re subject)
3677                        gnus-summary-gather-subject-limit))
3678    (t
3679     subject)))
3680
3681 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3682   "Check whether two subjects are equal.  If optional argument
3683 simple-first is t, first argument is already simplified."
3684   (cond
3685    ((null simple-first)
3686     (equal (gnus-simplify-subject-fully s1)
3687            (gnus-simplify-subject-fully s2)))
3688    (t
3689     (equal s1
3690            (gnus-simplify-subject-fully s2)))))
3691
3692 ;; Returns a list of writable groups.
3693 (defun gnus-writable-groups ()
3694   (let ((alist gnus-newsrc-alist)
3695         groups group)
3696     (while (setq group (car (pop alist)))
3697       (unless (gnus-group-read-only-p group)
3698         (push group groups)))
3699     (nreverse groups)))
3700
3701 (defun gnus-completing-read (default prompt &rest args)
3702   ;; Like `completing-read', except that DEFAULT is the default argument.
3703   (let* ((prompt (if default 
3704                      (concat prompt " (default " default ") ")
3705                    (concat prompt " ")))
3706          (answer (apply 'completing-read prompt args)))
3707     (if (or (null answer) (zerop (length answer)))
3708         default
3709       answer)))
3710
3711 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3712 ;; the echo area.
3713 (defun gnus-y-or-n-p (prompt)
3714   (prog1
3715       (y-or-n-p prompt)
3716     (message "")))
3717
3718 (defun gnus-yes-or-no-p (prompt)
3719   (prog1
3720       (yes-or-no-p prompt)
3721     (message "")))
3722
3723 ;; Check whether to use long file names.
3724 (defun gnus-use-long-file-name (symbol)
3725   ;; The variable has to be set...
3726   (and gnus-use-long-file-name
3727        ;; If it isn't a list, then we return t.
3728        (or (not (listp gnus-use-long-file-name))
3729            ;; If it is a list, and the list contains `symbol', we
3730            ;; return nil.
3731            (not (memq symbol gnus-use-long-file-name)))))
3732
3733 ;; I suspect there's a better way, but I haven't taken the time to do
3734 ;; it yet. -erik selberg@cs.washington.edu
3735 (defun gnus-dd-mmm (messy-date)
3736   "Return a string like DD-MMM from a big messy string"
3737   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3738                    (error nil))))
3739     (if (not datevec)
3740         "??-???"
3741       (format "%2s-%s"
3742               (condition-case ()
3743                   ;; Make sure leading zeroes are stripped.
3744                   (number-to-string (string-to-number (aref datevec 2)))
3745                 (error "??"))
3746               (capitalize
3747                (or (car
3748                     (nth (1- (string-to-number (aref datevec 1)))
3749                          timezone-months-assoc))
3750                    "???"))))))
3751
3752 (defun gnus-mode-string-quote (string)
3753   "Quote all \"%\" in STRING."
3754   (save-excursion
3755     (gnus-set-work-buffer)
3756     (insert string)
3757     (goto-char (point-min))
3758     (while (search-forward "%" nil t)
3759       (insert "%"))
3760     (buffer-string)))
3761
3762 ;; Make a hash table (default and minimum size is 255).
3763 ;; Optional argument HASHSIZE specifies the table size.
3764 (defun gnus-make-hashtable (&optional hashsize)
3765   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3766
3767 ;; Make a number that is suitable for hashing; bigger than MIN and one
3768 ;; less than 2^x.
3769 (defun gnus-create-hash-size (min)
3770   (let ((i 1))
3771     (while (< i min)
3772       (setq i (* 2 i)))
3773     (1- i)))
3774
3775 ;; Show message if message has a lower level than `gnus-verbose'.
3776 ;; Guideline for numbers:
3777 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3778 ;; for things that take a long time, 7 - not very important messages
3779 ;; on stuff, 9 - messages inside loops.
3780 (defun gnus-message (level &rest args)
3781   (if (<= level gnus-verbose)
3782       (apply 'message args)
3783     ;; We have to do this format thingy here even if the result isn't
3784     ;; shown - the return value has to be the same as the return value
3785     ;; from `message'.
3786     (apply 'format args)))
3787
3788 (defun gnus-error (level &rest args)
3789   "Beep an error if `gnus-verbose' is on LEVEL or less."
3790   (when (<= (floor level) gnus-verbose)
3791     (apply 'message args)
3792     (ding)
3793     (let (duration)
3794       (when (and (floatp level)
3795                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3796         (sit-for duration))))
3797   nil)
3798
3799 ;; Generate a unique new group name.
3800 (defun gnus-generate-new-group-name (leaf)
3801   (let ((name leaf)
3802         (num 0))
3803     (while (gnus-gethash name gnus-newsrc-hashtb)
3804       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3805     name))
3806
3807 (defsubst gnus-hide-text (b e props)
3808   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3809   (gnus-add-text-properties b e props)
3810   (when (memq 'intangible props)
3811     (gnus-put-text-property (max (1- b) (point-min))
3812                        b 'intangible (cddr (memq 'intangible props)))))
3813
3814 (defsubst gnus-unhide-text (b e)
3815   "Remove hidden text properties from region between B and E."
3816   (remove-text-properties b e gnus-hidden-properties)
3817   (when (memq 'intangible gnus-hidden-properties)
3818     (gnus-put-text-property (max (1- b) (point-min))
3819                        b 'intangible nil)))
3820
3821 (defun gnus-hide-text-type (b e type)
3822   "Hide text of TYPE between B and E."
3823   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3824
3825 (defun gnus-parent-headers (headers &optional generation)
3826   "Return the headers of the GENERATIONeth parent of HEADERS."
3827   (unless generation 
3828     (setq generation 1))
3829   (let (references parent)
3830     (while (and headers (not (zerop generation)))
3831       (setq references (mail-header-references headers))
3832       (when (and references
3833                  (setq parent (gnus-parent-id references))
3834                  (setq headers (car (gnus-id-to-thread parent))))
3835         (decf generation)))
3836     headers))
3837
3838 (defun gnus-parent-id (references)
3839   "Return the last Message-ID in REFERENCES."
3840   (when (and references
3841              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3842     (substring references (match-beginning 1) (match-end 1))))
3843
3844 (defun gnus-split-references (references)
3845   "Return a list of Message-IDs in REFERENCES."
3846   (let ((beg 0)
3847         ids)
3848     (while (string-match "<[^>]+>" references beg)
3849       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3850             ids))
3851     (nreverse ids)))
3852
3853 (defun gnus-buffer-live-p (buffer)
3854   "Say whether BUFFER is alive or not."
3855   (and buffer
3856        (get-buffer buffer)
3857        (buffer-name (get-buffer buffer))))
3858
3859 (defun gnus-ephemeral-group-p (group)
3860   "Say whether GROUP is ephemeral or not."
3861   (gnus-group-get-parameter group 'quit-config))
3862
3863 (defun gnus-group-quit-config (group)
3864   "Return the quit-config of GROUP."
3865   (gnus-group-get-parameter group 'quit-config))
3866
3867 (defun gnus-simplify-mode-line ()
3868   "Make mode lines a bit simpler."
3869   (setq mode-line-modified "-- ")
3870   (when (listp mode-line-format)
3871     (make-local-variable 'mode-line-format)
3872     (setq mode-line-format (copy-sequence mode-line-format))
3873     (when (equal (nth 3 mode-line-format) "   ")
3874       (setcar (nthcdr 3 mode-line-format) " "))))
3875
3876 ;;; List and range functions
3877
3878 (defun gnus-last-element (list)
3879   "Return last element of LIST."
3880   (while (cdr list)
3881     (setq list (cdr list)))
3882   (car list))
3883
3884 (defun gnus-copy-sequence (list)
3885   "Do a complete, total copy of a list."
3886   (if (and (consp list) (not (consp (cdr list))))
3887       (cons (car list) (cdr list))
3888     (mapcar (lambda (elem) (if (consp elem)
3889                                (if (consp (cdr elem))
3890                                    (gnus-copy-sequence elem)
3891                                  (cons (car elem) (cdr elem)))
3892                              elem))
3893             list)))
3894
3895 (defun gnus-set-difference (list1 list2)
3896   "Return a list of elements of LIST1 that do not appear in LIST2."
3897   (let ((list1 (copy-sequence list1)))
3898     (while list2
3899       (setq list1 (delq (car list2) list1))
3900       (setq list2 (cdr list2)))
3901     list1))
3902
3903 (defun gnus-sorted-complement (list1 list2)
3904   "Return a list of elements of LIST1 that do not appear in LIST2.
3905 Both lists have to be sorted over <."
3906   (let (out)
3907     (if (or (null list1) (null list2))
3908         (or list1 list2)
3909       (while (and list1 list2)
3910         (cond ((= (car list1) (car list2))
3911                (setq list1 (cdr list1)
3912                      list2 (cdr list2)))
3913               ((< (car list1) (car list2))
3914                (setq out (cons (car list1) out))
3915                (setq list1 (cdr list1)))
3916               (t
3917                (setq out (cons (car list2) out))
3918                (setq list2 (cdr list2)))))
3919       (nconc (nreverse out) (or list1 list2)))))
3920
3921 (defun gnus-intersection (list1 list2)
3922   (let ((result nil))
3923     (while list2
3924       (if (memq (car list2) list1)
3925           (setq result (cons (car list2) result)))
3926       (setq list2 (cdr list2)))
3927     result))
3928
3929 (defun gnus-sorted-intersection (list1 list2)
3930   ;; LIST1 and LIST2 have to be sorted over <.
3931   (let (out)
3932     (while (and list1 list2)
3933       (cond ((= (car list1) (car list2))
3934              (setq out (cons (car list1) out)
3935                    list1 (cdr list1)
3936                    list2 (cdr list2)))
3937             ((< (car list1) (car list2))
3938              (setq list1 (cdr list1)))
3939             (t
3940              (setq list2 (cdr list2)))))
3941     (nreverse out)))
3942
3943 (defun gnus-set-sorted-intersection (list1 list2)
3944   ;; LIST1 and LIST2 have to be sorted over <.
3945   ;; This function modifies LIST1.
3946   (let* ((top (cons nil list1))
3947          (prev top))
3948     (while (and list1 list2)
3949       (cond ((= (car list1) (car list2))
3950              (setq prev list1
3951                    list1 (cdr list1)
3952                    list2 (cdr list2)))
3953             ((< (car list1) (car list2))
3954              (setcdr prev (cdr list1))
3955              (setq list1 (cdr list1)))
3956             (t
3957              (setq list2 (cdr list2)))))
3958     (setcdr prev nil)
3959     (cdr top)))
3960
3961 (defun gnus-compress-sequence (numbers &optional always-list)
3962   "Convert list of numbers to a list of ranges or a single range.
3963 If ALWAYS-LIST is non-nil, this function will always release a list of
3964 ranges."
3965   (let* ((first (car numbers))
3966          (last (car numbers))
3967          result)
3968     (if (null numbers)
3969         nil
3970       (if (not (listp (cdr numbers)))
3971           numbers
3972         (while numbers
3973           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3974                 ((= (1+ last) (car numbers)) ;Still in sequence
3975                  (setq last (car numbers)))
3976                 (t                      ;End of one sequence
3977                  (setq result
3978                        (cons (if (= first last) first
3979                                (cons first last)) result))
3980                  (setq first (car numbers))
3981                  (setq last  (car numbers))))
3982           (setq numbers (cdr numbers)))
3983         (if (and (not always-list) (null result))
3984             (if (= first last) (list first) (cons first last))
3985           (nreverse (cons (if (= first last) first (cons first last))
3986                           result)))))))
3987
3988 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3989 (defun gnus-uncompress-range (ranges)
3990   "Expand a list of ranges into a list of numbers.
3991 RANGES is either a single range on the form `(num . num)' or a list of
3992 these ranges."
3993   (let (first last result)
3994     (cond
3995      ((null ranges)
3996       nil)
3997      ((not (listp (cdr ranges)))
3998       (setq first (car ranges))
3999       (setq last (cdr ranges))
4000       (while (<= first last)
4001         (setq result (cons first result))
4002         (setq first (1+ first)))
4003       (nreverse result))
4004      (t
4005       (while ranges
4006         (if (atom (car ranges))
4007             (if (numberp (car ranges))
4008                 (setq result (cons (car ranges) result)))
4009           (setq first (caar ranges))
4010           (setq last  (cdar ranges))
4011           (while (<= first last)
4012             (setq result (cons first result))
4013             (setq first (1+ first))))
4014         (setq ranges (cdr ranges)))
4015       (nreverse result)))))
4016
4017 (defun gnus-add-to-range (ranges list)
4018   "Return a list of ranges that has all articles from both RANGES and LIST.
4019 Note: LIST has to be sorted over `<'."
4020   (if (not ranges)
4021       (gnus-compress-sequence list t)
4022     (setq list (copy-sequence list))
4023     (or (listp (cdr ranges))
4024         (setq ranges (list ranges)))
4025     (let ((out ranges)
4026           ilist lowest highest temp)
4027       (while (and ranges list)
4028         (setq ilist list)
4029         (setq lowest (or (and (atom (car ranges)) (car ranges))
4030                          (caar ranges)))
4031         (while (and list (cdr list) (< (cadr list) lowest))
4032           (setq list (cdr list)))
4033         (if (< (car ilist) lowest)
4034             (progn
4035               (setq temp list)
4036               (setq list (cdr list))
4037               (setcdr temp nil)
4038               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4039         (setq highest (or (and (atom (car ranges)) (car ranges))
4040                           (cdar ranges)))
4041         (while (and list (<= (car list) highest))
4042           (setq list (cdr list)))
4043         (setq ranges (cdr ranges)))
4044       (if list
4045           (setq out (nconc (gnus-compress-sequence list t) out)))
4046       (setq out (sort out (lambda (r1 r2)
4047                             (< (or (and (atom r1) r1) (car r1))
4048                                (or (and (atom r2) r2) (car r2))))))
4049       (setq ranges out)
4050       (while ranges
4051         (if (atom (car ranges))
4052             (if (cdr ranges)
4053                 (if (atom (cadr ranges))
4054                     (if (= (1+ (car ranges)) (cadr ranges))
4055                         (progn
4056                           (setcar ranges (cons (car ranges)
4057                                                (cadr ranges)))
4058                           (setcdr ranges (cddr ranges))))
4059                   (if (= (1+ (car ranges)) (caadr ranges))
4060                       (progn
4061                         (setcar (cadr ranges) (car ranges))
4062                         (setcar ranges (cadr ranges))
4063                         (setcdr ranges (cddr ranges))))))
4064           (if (cdr ranges)
4065               (if (atom (cadr ranges))
4066                   (if (= (1+ (cdar ranges)) (cadr ranges))
4067                       (progn
4068                         (setcdr (car ranges) (cadr ranges))
4069                         (setcdr ranges (cddr ranges))))
4070                 (if (= (1+ (cdar ranges)) (caadr ranges))
4071                     (progn
4072                       (setcdr (car ranges) (cdadr ranges))
4073                       (setcdr ranges (cddr ranges)))))))
4074         (setq ranges (cdr ranges)))
4075       out)))
4076
4077 (defun gnus-remove-from-range (ranges list)
4078   "Return a list of ranges that has all articles from LIST removed from RANGES.
4079 Note: LIST has to be sorted over `<'."
4080   ;; !!! This function shouldn't look like this, but I've got a headache.
4081   (gnus-compress-sequence
4082    (gnus-sorted-complement
4083     (gnus-uncompress-range ranges) list)))
4084
4085 (defun gnus-member-of-range (number ranges)
4086   (if (not (listp (cdr ranges)))
4087       (and (>= number (car ranges))
4088            (<= number (cdr ranges)))
4089     (let ((not-stop t))
4090       (while (and ranges
4091                   (if (numberp (car ranges))
4092                       (>= number (car ranges))
4093                     (>= number (caar ranges)))
4094                   not-stop)
4095         (if (if (numberp (car ranges))
4096                 (= number (car ranges))
4097               (and (>= number (caar ranges))
4098                    (<= number (cdar ranges))))
4099             (setq not-stop nil))
4100         (setq ranges (cdr ranges)))
4101       (not not-stop))))
4102
4103 (defun gnus-range-length (range)
4104   "Return the length RANGE would have if uncompressed."
4105   (length (gnus-uncompress-range range)))
4106
4107 (defun gnus-sublist-p (list sublist)
4108   "Test whether all elements in SUBLIST are members of LIST."
4109   (let ((sublistp t))
4110     (while sublist
4111       (unless (memq (pop sublist) list)
4112         (setq sublistp nil
4113               sublist nil)))
4114     sublistp))
4115
4116 \f
4117 ;;;
4118 ;;; Gnus group mode
4119 ;;;
4120
4121 (defvar gnus-group-mode-map nil)
4122 (put 'gnus-group-mode 'mode-class 'special)
4123
4124 (unless gnus-group-mode-map
4125   (setq gnus-group-mode-map (make-keymap))
4126   (suppress-keymap gnus-group-mode-map)
4127
4128   (gnus-define-keys gnus-group-mode-map
4129     " " gnus-group-read-group
4130     "=" gnus-group-select-group
4131     "\r" gnus-group-select-group
4132     "\M-\r" gnus-group-quick-select-group
4133     "j" gnus-group-jump-to-group
4134     "n" gnus-group-next-unread-group
4135     "p" gnus-group-prev-unread-group
4136     "\177" gnus-group-prev-unread-group
4137     [delete] gnus-group-prev-unread-group
4138     "N" gnus-group-next-group
4139     "P" gnus-group-prev-group
4140     "\M-n" gnus-group-next-unread-group-same-level
4141     "\M-p" gnus-group-prev-unread-group-same-level
4142     "," gnus-group-best-unread-group
4143     "." gnus-group-first-unread-group
4144     "u" gnus-group-unsubscribe-current-group
4145     "U" gnus-group-unsubscribe-group
4146     "c" gnus-group-catchup-current
4147     "C" gnus-group-catchup-current-all
4148     "l" gnus-group-list-groups
4149     "L" gnus-group-list-all-groups
4150     "m" gnus-group-mail
4151     "g" gnus-group-get-new-news
4152     "\M-g" gnus-group-get-new-news-this-group
4153     "R" gnus-group-restart
4154     "r" gnus-group-read-init-file
4155     "B" gnus-group-browse-foreign-server
4156     "b" gnus-group-check-bogus-groups
4157     "F" gnus-find-new-newsgroups
4158     "\C-c\C-d" gnus-group-describe-group
4159     "\M-d" gnus-group-describe-all-groups
4160     "\C-c\C-a" gnus-group-apropos
4161     "\C-c\M-\C-a" gnus-group-description-apropos
4162     "a" gnus-group-post-news
4163     "\ek" gnus-group-edit-local-kill
4164     "\eK" gnus-group-edit-global-kill
4165     "\C-k" gnus-group-kill-group
4166     "\C-y" gnus-group-yank-group
4167     "\C-w" gnus-group-kill-region
4168     "\C-x\C-t" gnus-group-transpose-groups
4169     "\C-c\C-l" gnus-group-list-killed
4170     "\C-c\C-x" gnus-group-expire-articles
4171     "\C-c\M-\C-x" gnus-group-expire-all-groups
4172     "V" gnus-version
4173     "s" gnus-group-save-newsrc
4174     "z" gnus-group-suspend
4175     "Z" gnus-group-clear-dribble
4176     "q" gnus-group-exit
4177     "Q" gnus-group-quit
4178     "?" gnus-group-describe-briefly
4179     "\C-c\C-i" gnus-info-find-node
4180     "\M-e" gnus-group-edit-group-method
4181     "^" gnus-group-enter-server-mode
4182     gnus-mouse-2 gnus-mouse-pick-group
4183     "<" beginning-of-buffer
4184     ">" end-of-buffer
4185     "\C-c\C-b" gnus-bug
4186     "\C-c\C-s" gnus-group-sort-groups
4187     "t" gnus-topic-mode
4188     "\C-c\M-g" gnus-activate-all-groups
4189     "\M-&" gnus-group-universal-argument
4190     "#" gnus-group-mark-group
4191     "\M-#" gnus-group-unmark-group)
4192
4193   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4194     "m" gnus-group-mark-group
4195     "u" gnus-group-unmark-group
4196     "w" gnus-group-mark-region
4197     "m" gnus-group-mark-buffer
4198     "r" gnus-group-mark-regexp
4199     "U" gnus-group-unmark-all-groups)
4200
4201   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4202     "d" gnus-group-make-directory-group
4203     "h" gnus-group-make-help-group
4204     "a" gnus-group-make-archive-group
4205     "k" gnus-group-make-kiboze-group
4206     "m" gnus-group-make-group
4207     "E" gnus-group-edit-group
4208     "e" gnus-group-edit-group-method
4209     "p" gnus-group-edit-group-parameters
4210     "v" gnus-group-add-to-virtual
4211     "V" gnus-group-make-empty-virtual
4212     "D" gnus-group-enter-directory
4213     "f" gnus-group-make-doc-group
4214     "r" gnus-group-rename-group
4215     "\177" gnus-group-delete-group
4216     [delete] gnus-group-delete-group)
4217
4218    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4219      "b" gnus-group-brew-soup
4220      "w" gnus-soup-save-areas
4221      "s" gnus-soup-send-replies
4222      "p" gnus-soup-pack-packet
4223      "r" nnsoup-pack-replies)
4224
4225    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4226      "s" gnus-group-sort-groups
4227      "a" gnus-group-sort-groups-by-alphabet
4228      "u" gnus-group-sort-groups-by-unread
4229      "l" gnus-group-sort-groups-by-level
4230      "v" gnus-group-sort-groups-by-score
4231      "r" gnus-group-sort-groups-by-rank
4232      "m" gnus-group-sort-groups-by-method)
4233
4234    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4235      "k" gnus-group-list-killed
4236      "z" gnus-group-list-zombies
4237      "s" gnus-group-list-groups
4238      "u" gnus-group-list-all-groups
4239      "A" gnus-group-list-active
4240      "a" gnus-group-apropos
4241      "d" gnus-group-description-apropos
4242      "m" gnus-group-list-matching
4243      "M" gnus-group-list-all-matching
4244      "l" gnus-group-list-level)
4245
4246    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4247      "f" gnus-score-flush-cache)
4248
4249    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4250      "f" gnus-group-fetch-faq)
4251
4252    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4253      "l" gnus-group-set-current-level
4254      "t" gnus-group-unsubscribe-current-group
4255      "s" gnus-group-unsubscribe-group
4256      "k" gnus-group-kill-group
4257      "y" gnus-group-yank-group
4258      "w" gnus-group-kill-region
4259      "\C-k" gnus-group-kill-level
4260      "z" gnus-group-kill-all-zombies))
4261
4262 (defun gnus-group-mode ()
4263   "Major mode for reading news.
4264
4265 All normal editing commands are switched off.
4266 \\<gnus-group-mode-map>
4267 The group buffer lists (some of) the groups available.  For instance,
4268 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4269 lists all zombie groups.
4270
4271 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4272 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4273
4274 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4275
4276 The following commands are available:
4277
4278 \\{gnus-group-mode-map}"
4279   (interactive)
4280   (when (and menu-bar-mode
4281              (gnus-visual-p 'group-menu 'menu))
4282     (gnus-group-make-menu-bar))
4283   (kill-all-local-variables)
4284   (gnus-simplify-mode-line)
4285   (setq major-mode 'gnus-group-mode)
4286   (setq mode-name "Group")
4287   (gnus-group-set-mode-line)
4288   (setq mode-line-process nil)
4289   (use-local-map gnus-group-mode-map)
4290   (buffer-disable-undo (current-buffer))
4291   (setq truncate-lines t)
4292   (setq buffer-read-only t)
4293   (gnus-make-local-hook 'post-command-hook)
4294   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4295   (run-hooks 'gnus-group-mode-hook))
4296
4297 (defun gnus-clear-inboxes-moved ()
4298   (setq nnmail-moved-inboxes nil))
4299
4300 (defun gnus-mouse-pick-group (e)
4301   "Enter the group under the mouse pointer."
4302   (interactive "e")
4303   (mouse-set-point e)
4304   (gnus-group-read-group nil))
4305
4306 ;; Look at LEVEL and find out what the level is really supposed to be.
4307 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4308 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4309 (defun gnus-group-default-level (&optional level number-or-nil)
4310   (cond
4311    (gnus-group-use-permanent-levels
4312     (or (setq gnus-group-use-permanent-levels
4313               (or level (if (numberp gnus-group-use-permanent-levels)
4314                             gnus-group-use-permanent-levels
4315                           (or gnus-group-default-list-level
4316                               gnus-level-subscribed))))
4317         gnus-group-default-list-level gnus-level-subscribed))
4318    (number-or-nil
4319     level)
4320    (t
4321     (or level gnus-group-default-list-level gnus-level-subscribed))))
4322
4323 ;;;###autoload
4324 (defun gnus-slave-no-server (&optional arg)
4325   "Read network news as a slave, without connecting to local server"
4326   (interactive "P")
4327   (gnus-no-server arg t))
4328
4329 ;;;###autoload
4330 (defun gnus-no-server (&optional arg slave)
4331   "Read network news.
4332 If ARG is a positive number, Gnus will use that as the
4333 startup level.  If ARG is nil, Gnus will be started at level 2.
4334 If ARG is non-nil and not a positive number, Gnus will
4335 prompt the user for the name of an NNTP server to use.
4336 As opposed to `gnus', this command will not connect to the local server."
4337   (interactive "P")
4338   (let ((val (or arg (1- gnus-level-default-subscribed))))
4339     (gnus val t slave)
4340     (make-local-variable 'gnus-group-use-permanent-levels)
4341     (setq gnus-group-use-permanent-levels val)))
4342
4343 ;;;###autoload
4344 (defun gnus-slave (&optional arg)
4345   "Read news as a slave."
4346   (interactive "P")
4347   (gnus arg nil 'slave))
4348
4349 ;;;###autoload
4350 (defun gnus-other-frame (&optional arg)
4351   "Pop up a frame to read news."
4352   (interactive "P")
4353   (if (get-buffer gnus-group-buffer)
4354       (let ((pop-up-frames t))
4355         (gnus arg))
4356     (select-frame (make-frame))
4357     (gnus arg)))
4358
4359 ;;;###autoload
4360 (defun gnus (&optional arg dont-connect slave)
4361   "Read network news.
4362 If ARG is non-nil and a positive number, Gnus will use that as the
4363 startup level.  If ARG is non-nil and not a positive number, Gnus will
4364 prompt the user for the name of an NNTP server to use."
4365   (interactive "P")
4366
4367   (if (get-buffer gnus-group-buffer)
4368       (progn
4369         (switch-to-buffer gnus-group-buffer)
4370         (gnus-group-get-new-news))
4371
4372     (gnus-clear-system)
4373     (nnheader-init-server-buffer)
4374     (gnus-read-init-file)
4375     (setq gnus-slave slave)
4376
4377     (gnus-group-setup-buffer)
4378     (let ((buffer-read-only nil))
4379       (erase-buffer)
4380       (if (not gnus-inhibit-startup-message)
4381           (progn
4382             (gnus-group-startup-message)
4383             (sit-for 0))))
4384
4385     (let ((level (and (numberp arg) (> arg 0) arg))
4386           did-connect)
4387       (unwind-protect
4388           (progn
4389             (or dont-connect
4390                 (setq did-connect
4391                       (gnus-start-news-server (and arg (not level))))))
4392         (if (and (not dont-connect)
4393                  (not did-connect))
4394             (gnus-group-quit)
4395           (run-hooks 'gnus-startup-hook)
4396           ;; NNTP server is successfully open.
4397
4398           ;; Find the current startup file name.
4399           (setq gnus-current-startup-file
4400                 (gnus-make-newsrc-file gnus-startup-file))
4401
4402           ;; Read the dribble file.
4403           (when (or gnus-slave gnus-use-dribble-file)
4404             (gnus-dribble-read-file))
4405
4406           ;; Allow using GroupLens predictions.
4407           (when gnus-use-grouplens
4408             (bbb-login)
4409             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4410
4411           (gnus-summary-make-display-table)
4412           ;; Do the actual startup.
4413           (gnus-setup-news nil level dont-connect)
4414           ;; Generate the group buffer.
4415           (gnus-group-list-groups level)
4416           (gnus-group-first-unread-group)
4417           (gnus-configure-windows 'group)
4418           (gnus-group-set-mode-line))))))
4419
4420 (defun gnus-unload ()
4421   "Unload all Gnus features."
4422   (interactive)
4423   (or (boundp 'load-history)
4424       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4425   (let ((history load-history)
4426         feature)
4427     (while history
4428       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4429            (setq feature (cdr (assq 'provide (car history))))
4430            (unload-feature feature 'force))
4431       (setq history (cdr history)))))
4432
4433 (defun gnus-compile ()
4434   "Byte-compile the user-defined format specs."
4435   (interactive)
4436   (let ((entries gnus-format-specs)
4437         entry gnus-tmp-func)
4438     (save-excursion
4439       (gnus-message 7 "Compiling format specs...")
4440
4441       (while entries
4442         (setq entry (pop entries))
4443         (if (eq (car entry) 'version)
4444             (setq gnus-format-specs (delq entry gnus-format-specs))
4445           (when (and (listp (caddr entry))
4446                      (not (eq 'byte-code (caaddr entry))))
4447             (fset 'gnus-tmp-func
4448                   `(lambda () ,(caddr entry)))
4449             (byte-compile 'gnus-tmp-func)
4450             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4451
4452       (push (cons 'version emacs-version) gnus-format-specs)
4453
4454       (gnus-message 7 "Compiling user specs...done"))))
4455
4456 (defun gnus-indent-rigidly (start end arg)
4457   "Indent rigidly using only spaces and no tabs."
4458   (save-excursion
4459     (save-restriction
4460       (narrow-to-region start end)
4461       (indent-rigidly start end arg)
4462       (goto-char (point-min))
4463       (while (search-forward "\t" nil t)
4464         (replace-match "        " t t)))))
4465
4466 (defun gnus-group-startup-message (&optional x y)
4467   "Insert startup message in current buffer."
4468   ;; Insert the message.
4469   (erase-buffer)
4470   (insert
4471    (format "              %s
4472           _    ___ _             _
4473           _ ___ __ ___  __    _ ___
4474           __   _     ___    __  ___
4475               _           ___     _
4476              _  _ __             _
4477              ___   __            _
4478                    __           _
4479                     _      _   _
4480                    _      _    _
4481                       _  _    _
4482                   __  ___
4483                  _   _ _     _
4484                 _   _
4485               _    _
4486              _    _
4487             _
4488           __
4489
4490 "
4491            ""))
4492   ;; And then hack it.
4493   (gnus-indent-rigidly (point-min) (point-max)
4494                        (/ (max (- (window-width) (or x 46)) 0) 2))
4495   (goto-char (point-min))
4496   (forward-line 1)
4497   (let* ((pheight (count-lines (point-min) (point-max)))
4498          (wheight (window-height))
4499          (rest (- wheight pheight)))
4500     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4501   ;; Fontify some.
4502   (goto-char (point-min))
4503   (and (search-forward "Praxis" nil t)
4504        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4505   (goto-char (point-min))
4506   (let* ((mode-string (gnus-group-set-mode-line)))
4507     (setq mode-line-buffer-identification
4508           (list (concat gnus-version (substring (car mode-string) 4))))
4509     (set-buffer-modified-p t)))
4510
4511 (defun gnus-group-setup-buffer ()
4512   (or (get-buffer gnus-group-buffer)
4513       (progn
4514         (switch-to-buffer gnus-group-buffer)
4515         (gnus-add-current-to-buffer-list)
4516         (gnus-group-mode)
4517         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4518
4519 (defun gnus-group-list-groups (&optional level unread lowest)
4520   "List newsgroups with level LEVEL or lower that have unread articles.
4521 Default is all subscribed groups.
4522 If argument UNREAD is non-nil, groups with no unread articles are also
4523 listed."
4524   (interactive (list (if current-prefix-arg
4525                          (prefix-numeric-value current-prefix-arg)
4526                        (or
4527                         (gnus-group-default-level nil t)
4528                         gnus-group-default-list-level
4529                         gnus-level-subscribed))))
4530   (or level
4531       (setq level (car gnus-group-list-mode)
4532             unread (cdr gnus-group-list-mode)))
4533   (setq level (gnus-group-default-level level))
4534   (gnus-group-setup-buffer)             ;May call from out of group buffer
4535   (gnus-update-format-specifications)
4536   (let ((case-fold-search nil)
4537         (props (text-properties-at (gnus-point-at-bol)))
4538         (group (gnus-group-group-name)))
4539     (set-buffer gnus-group-buffer)
4540     (funcall gnus-group-prepare-function level unread lowest)
4541     (if (zerop (buffer-size))
4542         (gnus-message 5 gnus-no-groups-message)
4543       (goto-char (point-max))
4544       (when (or (not gnus-group-goto-next-group-function)
4545                 (not (funcall gnus-group-goto-next-group-function 
4546                               group props)))
4547         (if (not group)
4548             ;; Go to the first group with unread articles.
4549             (gnus-group-search-forward t)
4550           ;; Find the right group to put point on.  If the current group
4551           ;; has disappeared in the new listing, try to find the next
4552           ;; one.        If no next one can be found, just leave point at the
4553           ;; first newsgroup in the buffer.
4554           (if (not (gnus-goto-char
4555                     (text-property-any
4556                      (point-min) (point-max)
4557                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4558               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4559                 (while (and newsrc
4560                             (not (gnus-goto-char
4561                                   (text-property-any
4562                                    (point-min) (point-max) 'gnus-group
4563                                    (gnus-intern-safe
4564                                     (caar newsrc) gnus-active-hashtb)))))
4565                   (setq newsrc (cdr newsrc)))
4566                 (or newsrc (progn (goto-char (point-max))
4567                                   (forward-line -1)))))))
4568       ;; Adjust cursor point.
4569       (gnus-group-position-point))))
4570
4571 (defun gnus-group-list-level (level &optional all)
4572   "List groups on LEVEL.
4573 If ALL (the prefix), also list groups that have no unread articles."
4574   (interactive "nList groups on level: \nP")
4575   (gnus-group-list-groups level all level))
4576
4577 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4578   "List all newsgroups with unread articles of level LEVEL or lower.
4579 If ALL is non-nil, list groups that have no unread articles.
4580 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4581 If REGEXP, only list groups matching REGEXP."
4582   (set-buffer gnus-group-buffer)
4583   (let ((buffer-read-only nil)
4584         (newsrc (cdr gnus-newsrc-alist))
4585         (lowest (or lowest 1))
4586         info clevel unread group params)
4587     (erase-buffer)
4588     (if (< lowest gnus-level-zombie)
4589         ;; List living groups.
4590         (while newsrc
4591           (setq info (car newsrc)
4592                 group (gnus-info-group info)
4593                 params (gnus-info-params info)
4594                 newsrc (cdr newsrc)
4595                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4596           (and unread                   ; This group might be bogus
4597                (or (not regexp)
4598                    (string-match regexp group))
4599                (<= (setq clevel (gnus-info-level info)) level)
4600                (>= clevel lowest)
4601                (or all                  ; We list all groups?
4602                    (if (eq unread t)    ; Unactivated?
4603                        gnus-group-list-inactive-groups ; We list unactivated 
4604                      (> unread 0))      ; We list groups with unread articles
4605                    (and gnus-list-groups-with-ticked-articles
4606                         (cdr (assq 'tick (gnus-info-marks info))))
4607                                         ; And groups with tickeds
4608                    ;; Check for permanent visibility.
4609                    (and gnus-permanently-visible-groups
4610                         (string-match gnus-permanently-visible-groups
4611                                       group))
4612                    (memq 'visible params)
4613                    (cdr (assq 'visible params)))
4614                (gnus-group-insert-group-line
4615                 group (gnus-info-level info)
4616                 (gnus-info-marks info) unread (gnus-info-method info)))))
4617
4618     ;; List dead groups.
4619     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4620          (gnus-group-prepare-flat-list-dead
4621           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4622           gnus-level-zombie ?Z
4623           regexp))
4624     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4625          (gnus-group-prepare-flat-list-dead
4626           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4627           gnus-level-killed ?K regexp))
4628
4629     (gnus-group-set-mode-line)
4630     (setq gnus-group-list-mode (cons level all))
4631     (run-hooks 'gnus-group-prepare-hook)))
4632
4633 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4634   ;; List zombies and killed lists somewhat faster, which was
4635   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4636   ;; this by ignoring the group format specification altogether.
4637   (let (group)
4638     (if regexp
4639         ;; This loop is used when listing groups that match some
4640         ;; regexp.
4641         (while groups
4642           (setq group (pop groups))
4643           (when (string-match regexp group)
4644             (gnus-add-text-properties
4645              (point) (prog1 (1+ (point))
4646                        (insert " " mark "     *: " group "\n"))
4647              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4648                    'gnus-unread t
4649                    'gnus-level level))))
4650       ;; This loop is used when listing all groups.
4651       (while groups
4652         (gnus-add-text-properties
4653          (point) (prog1 (1+ (point))
4654                    (insert " " mark "     *: "
4655                            (setq group (pop groups)) "\n"))
4656          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4657                'gnus-unread t
4658                'gnus-level level))))))
4659
4660 (defmacro gnus-group-real-name (group)
4661   "Find the real name of a foreign newsgroup."
4662   `(let ((gname ,group))
4663      (if (string-match ":[^:]+$" gname)
4664          (substring gname (1+ (match-beginning 0)))
4665        gname)))
4666
4667 (defsubst gnus-server-add-address (method)
4668   (let ((method-name (symbol-name (car method))))
4669     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4670              (not (assq (intern (concat method-name "-address")) method)))
4671         (append method (list (list (intern (concat method-name "-address"))
4672                                    (nth 1 method))))
4673       method)))
4674
4675 (defsubst gnus-server-get-method (group method)
4676   ;; Input either a server name, and extended server name, or a
4677   ;; select method, and return a select method.
4678   (cond ((stringp method)
4679          (gnus-server-to-method method))
4680         ((equal method gnus-select-method)
4681          gnus-select-method)
4682         ((and (stringp (car method)) group)
4683          (gnus-server-extend-method group method))
4684         ((and method (not group)
4685               (equal (cadr method) ""))
4686          method)
4687         (t
4688          (gnus-server-add-address method))))
4689
4690 (defun gnus-server-to-method (server)
4691   "Map virtual server names to select methods."
4692   (or 
4693    ;; Is this a method, perhaps?
4694    (and server (listp server) server)
4695    ;; Perhaps this is the native server?
4696    (and (equal server "native") gnus-select-method)
4697    ;; It should be in the server alist.
4698    (cdr (assoc server gnus-server-alist))
4699    ;; If not, we look through all the opened server
4700    ;; to see whether we can find it there.
4701    (let ((opened gnus-opened-servers))
4702      (while (and opened
4703                  (not (equal server (format "%s:%s" (caaar opened)
4704                                             (cadaar opened)))))
4705        (pop opened))
4706      (caar opened))))
4707
4708 (defmacro gnus-method-equal (ss1 ss2)
4709   "Say whether two servers are equal."
4710   `(let ((s1 ,ss1)
4711          (s2 ,ss2))
4712      (or (equal s1 s2)
4713          (and (= (length s1) (length s2))
4714               (progn
4715                 (while (and s1 (member (car s1) s2))
4716                   (setq s1 (cdr s1)))
4717                 (null s1))))))
4718
4719 (defun gnus-server-equal (m1 m2)
4720   "Say whether two methods are equal."
4721   (let ((m1 (cond ((null m1) gnus-select-method)
4722                   ((stringp m1) (gnus-server-to-method m1))
4723                   (t m1)))
4724         (m2 (cond ((null m2) gnus-select-method)
4725                   ((stringp m2) (gnus-server-to-method m2))
4726                   (t m2))))
4727     (gnus-method-equal m1 m2)))
4728
4729 (defun gnus-servers-using-backend (backend)
4730   "Return a list of known servers using BACKEND."
4731   (let ((opened gnus-opened-servers)
4732         out)
4733     (while opened
4734       (when (eq backend (caaar opened))
4735         (push (caar opened) out))
4736       (pop opened))
4737     out))
4738
4739 (defun gnus-group-prefixed-name (group method)
4740   "Return the whole name from GROUP and METHOD."
4741   (and (stringp method) (setq method (gnus-server-to-method method)))
4742   (concat (format "%s" (car method))
4743           (if (and
4744                (or (assoc (format "%s" (car method)) 
4745                           (gnus-methods-using 'address))
4746                    (gnus-server-equal method gnus-message-archive-method))
4747                (nth 1 method)
4748                (not (string= (nth 1 method) "")))
4749               (concat "+" (nth 1 method)))
4750           ":" group))
4751
4752 (defun gnus-group-real-prefix (group)
4753   "Return the prefix of the current group name."
4754   (if (string-match "^[^:]+:" group)
4755       (substring group 0 (match-end 0))
4756     ""))
4757
4758 (defun gnus-group-method (group)
4759   "Return the server or method used for selecting GROUP."
4760   (let ((prefix (gnus-group-real-prefix group)))
4761     (if (equal prefix "")
4762         gnus-select-method
4763       (let ((servers gnus-opened-servers)
4764             (server "")
4765             backend possible found)
4766         (if (string-match "^[^\\+]+\\+" prefix)
4767             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4768                   server (substring prefix (match-end 0) (1- (length prefix))))
4769           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4770         (while servers
4771           (when (eq (caaar servers) backend)
4772             (setq possible (caar servers))
4773             (when (equal (cadaar servers) server)
4774               (setq found (caar servers))))
4775           (pop servers))
4776         (or (car (rassoc found gnus-server-alist))
4777             found
4778             (car (rassoc possible gnus-server-alist))
4779             possible
4780             (list backend server))))))
4781
4782 (defsubst gnus-secondary-method-p (method)
4783   "Return whether METHOD is a secondary select method."
4784   (let ((methods gnus-secondary-select-methods)
4785         (gmethod (gnus-server-get-method nil method)))
4786     (while (and methods
4787                 (not (equal (gnus-server-get-method nil (car methods))
4788                             gmethod)))
4789       (setq methods (cdr methods)))
4790     methods))
4791
4792 (defun gnus-group-foreign-p (group)
4793   "Say whether a group is foreign or not."
4794   (and (not (gnus-group-native-p group))
4795        (not (gnus-group-secondary-p group))))
4796
4797 (defun gnus-group-native-p (group)
4798   "Say whether the group is native or not."
4799   (not (string-match ":" group)))
4800
4801 (defun gnus-group-secondary-p (group)
4802   "Say whether the group is secondary or not."
4803   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4804
4805 (defun gnus-group-get-parameter (group &optional symbol)
4806   "Returns the group parameters for GROUP.
4807 If SYMBOL, return the value of that symbol in the group parameters."
4808   (let ((params (gnus-info-params (gnus-get-info group))))
4809     (if symbol
4810         (gnus-group-parameter-value params symbol)
4811       params)))
4812
4813 (defun gnus-group-parameter-value (params symbol)
4814   "Return the value of SYMBOL in group PARAMS."
4815   (or (car (memq symbol params))        ; It's either a simple symbol
4816       (cdr (assq symbol params))))      ; or a cons.
4817
4818 (defun gnus-group-add-parameter (group param)
4819   "Add parameter PARAM to GROUP."
4820   (let ((info (gnus-get-info group)))
4821     (if (not info)
4822         () ; This is a dead group.  We just ignore it.
4823       ;; Cons the new param to the old one and update.
4824       (gnus-group-set-info (cons param (gnus-info-params info))
4825                            group 'params))))
4826
4827 (defun gnus-group-set-parameter (group name value)
4828   "Set parameter NAME to VALUE in GROUP."
4829   (let ((info (gnus-get-info group)))
4830     (if (not info)
4831         () ; This is a dead group.  We just ignore it.
4832       (let ((old-params (gnus-info-params info))
4833             (new-params (list (cons name value))))
4834         (while old-params
4835           (if (or (not (listp (car old-params)))
4836                   (not (eq (caar old-params) name)))
4837               (setq new-params (append new-params (list (car old-params)))))
4838           (setq old-params (cdr old-params)))
4839         (gnus-group-set-info new-params group 'params)))))
4840
4841 (defun gnus-group-add-score (group &optional score)
4842   "Add SCORE to the GROUP score.
4843 If SCORE is nil, add 1 to the score of GROUP."
4844   (let ((info (gnus-get-info group)))
4845     (when info
4846       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4847
4848 (defun gnus-summary-bubble-group ()
4849   "Increase the score of the current group.
4850 This is a handy function to add to `gnus-summary-exit-hook' to
4851 increase the score of each group you read."
4852   (gnus-group-add-score gnus-newsgroup-name))
4853
4854 (defun gnus-group-set-info (info &optional method-only-group part)
4855   (let* ((entry (gnus-gethash
4856                  (or method-only-group (gnus-info-group info))
4857                  gnus-newsrc-hashtb))
4858          (part-info info)
4859          (info (if method-only-group (nth 2 entry) info))
4860          method)
4861     (when method-only-group
4862       (unless entry
4863         (error "Trying to change non-existent group %s" method-only-group))
4864       ;; We have received parts of the actual group info - either the
4865       ;; select method or the group parameters.  We first check
4866       ;; whether we have to extend the info, and if so, do that.
4867       (let ((len (length info))
4868             (total (if (eq part 'method) 5 6)))
4869         (when (< len total)
4870           (setcdr (nthcdr (1- len) info)
4871                   (make-list (- total len) nil)))
4872         ;; Then we enter the new info.
4873         (setcar (nthcdr (1- total) info) part-info)))
4874     (unless entry
4875       ;; This is a new group, so we just create it.
4876       (save-excursion
4877         (set-buffer gnus-group-buffer)
4878         (setq method (gnus-info-method info))
4879         (when (gnus-server-equal method "native")
4880           (setq method nil))
4881         (save-excursion
4882           (set-buffer gnus-group-buffer)
4883           (if method
4884               ;; It's a foreign group...
4885               (gnus-group-make-group
4886                (gnus-group-real-name (gnus-info-group info))
4887                (if (stringp method) method
4888                  (prin1-to-string (car method)))
4889                (and (consp method)
4890                     (nth 1 (gnus-info-method info))))
4891             ;; It's a native group.
4892             (gnus-group-make-group (gnus-info-group info))))
4893         (gnus-message 6 "Note: New group created")
4894         (setq entry
4895               (gnus-gethash (gnus-group-prefixed-name
4896                              (gnus-group-real-name (gnus-info-group info))
4897                              (or (gnus-info-method info) gnus-select-method))
4898                             gnus-newsrc-hashtb))))
4899     ;; Whether it was a new group or not, we now have the entry, so we
4900     ;; can do the update.
4901     (if entry
4902         (progn
4903           (setcar (nthcdr 2 entry) info)
4904           (when (and (not (eq (car entry) t))
4905                      (gnus-active (gnus-info-group info)))
4906             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4907       (error "No such group: %s" (gnus-info-group info)))))
4908
4909 (defun gnus-group-set-method-info (group select-method)
4910   (gnus-group-set-info select-method group 'method))
4911
4912 (defun gnus-group-set-params-info (group params)
4913   (gnus-group-set-info params group 'params))
4914
4915 (defun gnus-group-update-group-line ()
4916   "Update the current line in the group buffer."
4917   (let* ((buffer-read-only nil)
4918          (group (gnus-group-group-name))
4919          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4920          gnus-group-indentation)
4921     (and entry
4922          (not (gnus-ephemeral-group-p group))
4923          (gnus-dribble-enter
4924           (concat "(gnus-group-set-info '"
4925                   (prin1-to-string (nth 2 entry)) ")")))
4926     (setq gnus-group-indentation (gnus-group-group-indentation))
4927     (gnus-delete-line)
4928     (gnus-group-insert-group-line-info group)
4929     (forward-line -1)
4930     (gnus-group-position-point)))
4931
4932 (defun gnus-group-insert-group-line-info (group)
4933   "Insert GROUP on the current line."
4934   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4935         active info)
4936     (if entry
4937         (progn
4938           ;; (Un)subscribed group.
4939           (setq info (nth 2 entry))
4940           (gnus-group-insert-group-line
4941            group (gnus-info-level info) (gnus-info-marks info)
4942            (or (car entry) t) (gnus-info-method info)))
4943       ;; This group is dead.
4944       (gnus-group-insert-group-line
4945        group
4946        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4947        nil
4948        (if (setq active (gnus-active group))
4949            (- (1+ (cdr active)) (car active)) 0)
4950        nil))))
4951
4952 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4953                                                     gnus-tmp-marked number
4954                                                     gnus-tmp-method)
4955   "Insert a group line in the group buffer."
4956   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4957          (gnus-tmp-number-total
4958           (if gnus-tmp-active
4959               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4960             0))
4961          (gnus-tmp-number-of-unread
4962           (if (numberp number) (int-to-string (max 0 number))
4963             "*"))
4964          (gnus-tmp-number-of-read
4965           (if (numberp number)
4966               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4967             "*"))
4968          (gnus-tmp-subscribed
4969           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4970                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4971                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4972                 (t ?K)))
4973          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4974          (gnus-tmp-newsgroup-description
4975           (if gnus-description-hashtb
4976               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4977             ""))
4978          (gnus-tmp-moderated
4979           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4980          (gnus-tmp-moderated-string
4981           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4982          (gnus-tmp-method
4983           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4984          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4985          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4986          (gnus-tmp-news-method-string
4987           (if gnus-tmp-method
4988               (format "(%s:%s)" (car gnus-tmp-method)
4989                       (cadr gnus-tmp-method)) ""))
4990          (gnus-tmp-marked-mark
4991           (if (and (numberp number)
4992                    (zerop number)
4993                    (cdr (assq 'tick gnus-tmp-marked)))
4994               ?* ? ))
4995          (gnus-tmp-process-marked
4996           (if (member gnus-tmp-group gnus-group-marked)
4997               gnus-process-mark ? ))
4998          (gnus-tmp-grouplens
4999           (or (and gnus-use-grouplens
5000                    (bbb-grouplens-group-p gnus-tmp-group))
5001               ""))
5002          (buffer-read-only nil)
5003          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5004     (beginning-of-line)
5005     (gnus-add-text-properties
5006      (point)
5007      (prog1 (1+ (point))
5008        ;; Insert the text.
5009        (eval gnus-group-line-format-spec))
5010      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5011        gnus-unread ,(if (numberp number)
5012                         (string-to-int gnus-tmp-number-of-unread)
5013                       t)
5014        gnus-marked ,gnus-tmp-marked-mark
5015        gnus-indentation ,gnus-group-indentation
5016        gnus-level ,gnus-tmp-level))
5017     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5018       (forward-line -1)
5019       (run-hooks 'gnus-group-update-hook)
5020       (forward-line))
5021     ;; Allow XEmacs to remove front-sticky text properties.
5022     (gnus-group-remove-excess-properties)))
5023
5024 (defun gnus-group-update-group (group &optional visible-only)
5025   "Update all lines where GROUP appear.
5026 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5027 already."
5028   (save-excursion
5029     (set-buffer gnus-group-buffer)
5030     ;; The buffer may be narrowed.
5031     (save-restriction
5032       (widen)
5033       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5034             (loc (point-min))
5035             found buffer-read-only)
5036         ;; Enter the current status into the dribble buffer.
5037         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5038           (if (and entry (not (gnus-ephemeral-group-p group)))
5039               (gnus-dribble-enter
5040                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5041                        ")"))))
5042         ;; Find all group instances.  If topics are in use, each group
5043         ;; may be listed in more than once.
5044         (while (setq loc (text-property-any
5045                           loc (point-max) 'gnus-group ident))
5046           (setq found t)
5047           (goto-char loc)
5048           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5049             (gnus-delete-line)
5050             (gnus-group-insert-group-line-info group)
5051             (save-excursion
5052               (forward-line -1)
5053               (run-hooks 'gnus-group-update-group-hook)))
5054           (setq loc (1+ loc)))
5055         (unless (or found visible-only)
5056           ;; No such line in the buffer, find out where it's supposed to
5057           ;; go, and insert it there (or at the end of the buffer).
5058           (if gnus-goto-missing-group-function
5059               (funcall gnus-goto-missing-group-function group)
5060             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5061               (while (and entry (car entry)
5062                           (not
5063                            (gnus-goto-char
5064                             (text-property-any
5065                              (point-min) (point-max)
5066                              'gnus-group (gnus-intern-safe
5067                                           (caar entry) gnus-active-hashtb)))))
5068                 (setq entry (cdr entry)))
5069               (or entry (goto-char (point-max)))))
5070           ;; Finally insert the line.
5071           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5072             (gnus-group-insert-group-line-info group)
5073             (save-excursion
5074               (forward-line -1)
5075               (run-hooks 'gnus-group-update-group-hook))))
5076         (gnus-group-set-mode-line)))))
5077
5078 (defun gnus-group-set-mode-line ()
5079   "Update the mode line in the group buffer."
5080   (when (memq 'group gnus-updated-mode-lines)
5081     ;; Yes, we want to keep this mode line updated.
5082     (save-excursion
5083       (set-buffer gnus-group-buffer)
5084       (let* ((gformat (or gnus-group-mode-line-format-spec
5085                           (setq gnus-group-mode-line-format-spec
5086                                 (gnus-parse-format
5087                                  gnus-group-mode-line-format
5088                                  gnus-group-mode-line-format-alist))))
5089              (gnus-tmp-news-server (cadr gnus-select-method))
5090              (gnus-tmp-news-method (car gnus-select-method))
5091              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5092              (max-len 60)
5093              gnus-tmp-header            ;Dummy binding for user-defined formats
5094              ;; Get the resulting string.
5095              (mode-string (eval gformat)))
5096         ;; Say whether the dribble buffer has been modified.
5097         (setq mode-line-modified
5098               (if (and gnus-dribble-buffer
5099                        (buffer-name gnus-dribble-buffer)
5100                        (buffer-modified-p gnus-dribble-buffer)
5101                        (save-excursion
5102                          (set-buffer gnus-dribble-buffer)
5103                          (not (zerop (buffer-size)))))
5104                   "---*- " "----- "))
5105         ;; If the line is too long, we chop it off.
5106         (when (> (length mode-string) max-len)
5107           (setq mode-string (substring mode-string 0 (- max-len 4))))
5108         (prog1
5109             (setq mode-line-buffer-identification 
5110                   (list mode-string))
5111           (set-buffer-modified-p t))))))
5112
5113 (defun gnus-group-group-name ()
5114   "Get the name of the newsgroup on the current line."
5115   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5116     (and group (symbol-name group))))
5117
5118 (defun gnus-group-group-level ()
5119   "Get the level of the newsgroup on the current line."
5120   (get-text-property (gnus-point-at-bol) 'gnus-level))
5121
5122 (defun gnus-group-group-indentation ()
5123   "Get the indentation of the newsgroup on the current line."
5124   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5125       (and gnus-group-indentation-function
5126            (funcall gnus-group-indentation-function))
5127       ""))
5128
5129 (defun gnus-group-group-unread ()
5130   "Get the number of unread articles of the newsgroup on the current line."
5131   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5132
5133 (defun gnus-group-search-forward (&optional backward all level first-too)
5134   "Find the next newsgroup with unread articles.
5135 If BACKWARD is non-nil, find the previous newsgroup instead.
5136 If ALL is non-nil, just find any newsgroup.
5137 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5138 group exists.
5139 If FIRST-TOO, the current line is also eligible as a target."
5140   (let ((way (if backward -1 1))
5141         (low gnus-level-killed)
5142         (beg (point))
5143         pos found lev)
5144     (if (and backward (progn (beginning-of-line)) (bobp))
5145         nil
5146       (or first-too (forward-line way))
5147       (while (and
5148               (not (eobp))
5149               (not (setq
5150                     found
5151                     (and (or all
5152                              (and
5153                               (let ((unread
5154                                      (get-text-property (point) 'gnus-unread)))
5155                                 (and (numberp unread) (> unread 0)))
5156                               (setq lev (get-text-property (point)
5157                                                            'gnus-level))
5158                               (<= lev gnus-level-subscribed)))
5159                          (or (not level)
5160                              (and (setq lev (get-text-property (point)
5161                                                                'gnus-level))
5162                                   (or (= lev level)
5163                                       (and (< lev low)
5164                                            (< level lev)
5165                                            (progn
5166                                              (setq low lev)
5167                                              (setq pos (point))
5168                                              nil))))))))
5169               (zerop (forward-line way)))))
5170     (if found
5171         (progn (gnus-group-position-point) t)
5172       (goto-char (or pos beg))
5173       (and pos t))))
5174
5175 ;;; Gnus group mode commands
5176
5177 ;; Group marking.
5178
5179 (defun gnus-group-mark-group (n &optional unmark no-advance)
5180   "Mark the current group."
5181   (interactive "p")
5182   (let ((buffer-read-only nil)
5183         group)
5184     (while (and (> n 0)
5185                 (not (eobp)))
5186       (when (setq group (gnus-group-group-name))
5187         ;; Update the mark.
5188         (beginning-of-line)
5189         (forward-char
5190          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5191         (delete-char 1)
5192         (if unmark
5193             (progn
5194               (insert " ")
5195               (setq gnus-group-marked (delete group gnus-group-marked)))
5196           (insert "#")
5197           (setq gnus-group-marked
5198                 (cons group (delete group gnus-group-marked)))))
5199       (or no-advance (gnus-group-next-group 1))
5200       (decf n))
5201     (gnus-summary-position-point)
5202     n))
5203
5204 (defun gnus-group-unmark-group (n)
5205   "Remove the mark from the current group."
5206   (interactive "p")
5207   (gnus-group-mark-group n 'unmark)
5208   (gnus-group-position-point))
5209
5210 (defun gnus-group-unmark-all-groups ()
5211   "Unmark all groups."
5212   (interactive)
5213   (let ((groups gnus-group-marked))
5214     (save-excursion
5215       (while groups
5216         (gnus-group-remove-mark (pop groups)))))
5217   (gnus-group-position-point))
5218
5219 (defun gnus-group-mark-region (unmark beg end)
5220   "Mark all groups between point and mark.
5221 If UNMARK, remove the mark instead."
5222   (interactive "P\nr")
5223   (let ((num (count-lines beg end)))
5224     (save-excursion
5225       (goto-char beg)
5226       (- num (gnus-group-mark-group num unmark)))))
5227
5228 (defun gnus-group-mark-buffer (&optional unmark)
5229   "Mark all groups in the buffer.
5230 If UNMARK, remove the mark instead."
5231   (interactive "P")
5232   (gnus-group-mark-region unmark (point-min) (point-max)))
5233
5234 (defun gnus-group-mark-regexp (regexp)
5235   "Mark all groups that match some regexp."
5236   (interactive "sMark (regexp): ")
5237   (let ((alist (cdr gnus-newsrc-alist))
5238         group)
5239     (while alist
5240       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5241         (gnus-group-set-mark group))))
5242   (gnus-group-position-point))
5243
5244 (defun gnus-group-remove-mark (group)
5245   "Remove the process mark from GROUP and move point there.
5246 Return nil if the group isn't displayed."
5247   (if (gnus-group-goto-group group)
5248       (save-excursion
5249         (gnus-group-mark-group 1 'unmark t)
5250         t)
5251     (setq gnus-group-marked
5252           (delete group gnus-group-marked))
5253     nil))
5254
5255 (defun gnus-group-set-mark (group)
5256   "Set the process mark on GROUP."
5257   (if (gnus-group-goto-group group) 
5258       (save-excursion
5259         (gnus-group-mark-group 1 nil t))
5260     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5261
5262 (defun gnus-group-universal-argument (arg &optional groups func)
5263   "Perform any command on all groups accoring to the process/prefix convention."
5264   (interactive "P")
5265   (let ((groups (or groups (gnus-group-process-prefix arg)))
5266         group func)
5267     (if (eq (setq func (or func
5268                            (key-binding
5269                             (read-key-sequence
5270                              (substitute-command-keys
5271                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5272             'undefined)
5273         (gnus-error 1 "Undefined key")
5274       (while groups
5275         (gnus-group-remove-mark (setq group (pop groups)))
5276         (command-execute func))))
5277   (gnus-group-position-point))
5278
5279 (defun gnus-group-process-prefix (n)
5280   "Return a list of groups to work on.
5281 Take into consideration N (the prefix) and the list of marked groups."
5282   (cond
5283    (n
5284     (setq n (prefix-numeric-value n))
5285     ;; There is a prefix, so we return a list of the N next
5286     ;; groups.
5287     (let ((way (if (< n 0) -1 1))
5288           (n (abs n))
5289           group groups)
5290       (save-excursion
5291         (while (and (> n 0)
5292                     (setq group (gnus-group-group-name)))
5293           (setq groups (cons group groups))
5294           (setq n (1- n))
5295           (gnus-group-next-group way)))
5296       (nreverse groups)))
5297    ((and (boundp 'transient-mark-mode)
5298          transient-mark-mode
5299          (boundp 'mark-active)
5300          mark-active)
5301     ;; Work on the region between point and mark.
5302     (let ((max (max (point) (mark)))
5303           groups)
5304       (save-excursion
5305         (goto-char (min (point) (mark)))
5306         (while
5307             (and
5308              (push (gnus-group-group-name) groups)
5309              (zerop (gnus-group-next-group 1))
5310              (< (point) max)))
5311         (nreverse groups))))
5312    (gnus-group-marked
5313     ;; No prefix, but a list of marked articles.
5314     (reverse gnus-group-marked))
5315    (t
5316     ;; Neither marked articles or a prefix, so we return the
5317     ;; current group.
5318     (let ((group (gnus-group-group-name)))
5319       (and group (list group))))))
5320
5321 ;; Selecting groups.
5322
5323 (defun gnus-group-read-group (&optional all no-article group)
5324   "Read news in this newsgroup.
5325 If the prefix argument ALL is non-nil, already read articles become
5326 readable.  IF ALL is a number, fetch this number of articles.  If the
5327 optional argument NO-ARTICLE is non-nil, no article will be
5328 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5329 group."
5330   (interactive "P")
5331   (let ((group (or group (gnus-group-group-name)))
5332         number active marked entry)
5333     (or group (error "No group on current line"))
5334     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5335                                             group gnus-newsrc-hashtb)))))
5336     ;; This group might be a dead group.  In that case we have to get
5337     ;; the number of unread articles from `gnus-active-hashtb'.
5338     (setq number
5339           (cond ((numberp all) all)
5340                 (entry (car entry))
5341                 ((setq active (gnus-active group))
5342                  (- (1+ (cdr active)) (car active)))))
5343     (gnus-summary-read-group
5344      group (or all (and (numberp number)
5345                         (zerop (+ number (length (cdr (assq 'tick marked)))
5346                                   (length (cdr (assq 'dormant marked)))))))
5347      no-article)))
5348
5349 (defun gnus-group-select-group (&optional all)
5350   "Select this newsgroup.
5351 No article is selected automatically.
5352 If ALL is non-nil, already read articles become readable.
5353 If ALL is a number, fetch this number of articles."
5354   (interactive "P")
5355   (gnus-group-read-group all t))
5356
5357 (defun gnus-group-quick-select-group (&optional all)
5358   "Select the current group \"quickly\".
5359 This means that no highlighting or scoring will be performed."
5360   (interactive "P")
5361   (let (gnus-visual
5362         gnus-score-find-score-files-function
5363         gnus-apply-kill-hook
5364         gnus-summary-expunge-below)
5365     (gnus-group-read-group all t)))
5366
5367 (defun gnus-group-visible-select-group (&optional all)
5368   "Select the current group without hiding any articles."
5369   (interactive "P")
5370   (let ((gnus-inhibit-limiting t))
5371     (gnus-group-read-group all t)))
5372
5373 ;;;###autoload
5374 (defun gnus-fetch-group (group)
5375   "Start Gnus if necessary and enter GROUP.
5376 Returns whether the fetching was successful or not."
5377   (interactive "sGroup name: ")
5378   (or (get-buffer gnus-group-buffer)
5379       (gnus))
5380   (gnus-group-read-group nil nil group))
5381
5382 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5383 ;; if selection was successful.
5384 (defun gnus-group-read-ephemeral-group
5385   (group method &optional activate quit-config)
5386   (let ((group (if (gnus-group-foreign-p group) group
5387                  (gnus-group-prefixed-name group method))))
5388     (gnus-sethash
5389      group
5390      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5391                      ((quit-config . ,(if quit-config quit-config
5392                                         (cons (current-buffer) 'summary))))))
5393      gnus-newsrc-hashtb)
5394     (set-buffer gnus-group-buffer)
5395     (or (gnus-check-server method)
5396         (error "Unable to contact server: %s" (gnus-status-message method)))
5397     (if activate (or (gnus-request-group group)
5398                      (error "Couldn't request group")))
5399     (condition-case ()
5400         (gnus-group-read-group t t group)
5401       (error nil)
5402       (quit nil))))
5403
5404 (defun gnus-group-jump-to-group (group)
5405   "Jump to newsgroup GROUP."
5406   (interactive
5407    (list (completing-read
5408           "Group: " gnus-active-hashtb nil
5409           (gnus-read-active-file-p)
5410           nil
5411           'gnus-group-history)))
5412
5413   (when (equal group "")
5414     (error "Empty group name"))
5415
5416   (when (string-match "[\000-\032]" group)
5417     (error "Control characters in group: %s" group))
5418
5419   (let ((b (text-property-any
5420             (point-min) (point-max)
5421             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5422     (unless (gnus-ephemeral-group-p group)
5423       (if b
5424           ;; Either go to the line in the group buffer...
5425           (goto-char b)
5426         ;; ... or insert the line.
5427         (or
5428          (gnus-active group)
5429          (gnus-activate-group group)
5430          (error "%s error: %s" group (gnus-status-message group)))
5431
5432         (gnus-group-update-group group)
5433         (goto-char (text-property-any
5434                     (point-min) (point-max)
5435                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5436     ;; Adjust cursor point.
5437     (gnus-group-position-point)))
5438
5439 (defun gnus-group-goto-group (group)
5440   "Goto to newsgroup GROUP."
5441   (when group
5442     (let ((b (text-property-any (point-min) (point-max)
5443                                 'gnus-group (gnus-intern-safe
5444                                              group gnus-active-hashtb))))
5445       (and b (goto-char b)))))
5446
5447 (defun gnus-group-next-group (n)
5448   "Go to next N'th newsgroup.
5449 If N is negative, search backward instead.
5450 Returns the difference between N and the number of skips actually
5451 done."
5452   (interactive "p")
5453   (gnus-group-next-unread-group n t))
5454
5455 (defun gnus-group-next-unread-group (n &optional all level)
5456   "Go to next N'th unread newsgroup.
5457 If N is negative, search backward instead.
5458 If ALL is non-nil, choose any newsgroup, unread or not.
5459 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5460 such group can be found, the next group with a level higher than
5461 LEVEL.
5462 Returns the difference between N and the number of skips actually
5463 made."
5464   (interactive "p")
5465   (let ((backward (< n 0))
5466         (n (abs n)))
5467     (while (and (> n 0)
5468                 (gnus-group-search-forward
5469                  backward (or (not gnus-group-goto-unread) all) level))
5470       (setq n (1- n)))
5471     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5472                                (if level " on this level or higher" "")))
5473     n))
5474
5475 (defun gnus-group-prev-group (n)
5476   "Go to previous N'th newsgroup.
5477 Returns the difference between N and the number of skips actually
5478 done."
5479   (interactive "p")
5480   (gnus-group-next-unread-group (- n) t))
5481
5482 (defun gnus-group-prev-unread-group (n)
5483   "Go to previous N'th unread newsgroup.
5484 Returns the difference between N and the number of skips actually
5485 done."
5486   (interactive "p")
5487   (gnus-group-next-unread-group (- n)))
5488
5489 (defun gnus-group-next-unread-group-same-level (n)
5490   "Go to next N'th unread newsgroup on the same level.
5491 If N is negative, search backward instead.
5492 Returns the difference between N and the number of skips actually
5493 done."
5494   (interactive "p")
5495   (gnus-group-next-unread-group n t (gnus-group-group-level))
5496   (gnus-group-position-point))
5497
5498 (defun gnus-group-prev-unread-group-same-level (n)
5499   "Go to next N'th unread newsgroup on the same level.
5500 Returns the difference between N and the number of skips actually
5501 done."
5502   (interactive "p")
5503   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5504   (gnus-group-position-point))
5505
5506 (defun gnus-group-best-unread-group (&optional exclude-group)
5507   "Go to the group with the highest level.
5508 If EXCLUDE-GROUP, do not go to that group."
5509   (interactive)
5510   (goto-char (point-min))
5511   (let ((best 100000)
5512         unread best-point)
5513     (while (not (eobp))
5514       (setq unread (get-text-property (point) 'gnus-unread))
5515       (if (and (numberp unread) (> unread 0))
5516           (progn
5517             (if (and (get-text-property (point) 'gnus-level)
5518                      (< (get-text-property (point) 'gnus-level) best)
5519                      (or (not exclude-group)
5520                          (not (equal exclude-group (gnus-group-group-name)))))
5521                 (progn
5522                   (setq best (get-text-property (point) 'gnus-level))
5523                   (setq best-point (point))))))
5524       (forward-line 1))
5525     (if best-point (goto-char best-point))
5526     (gnus-summary-position-point)
5527     (and best-point (gnus-group-group-name))))
5528
5529 (defun gnus-group-first-unread-group ()
5530   "Go to the first group with unread articles."
5531   (interactive)
5532   (prog1
5533       (let ((opoint (point))
5534             unread)
5535         (goto-char (point-min))
5536         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5537                 (and (numberp unread)   ; Not a topic.
5538                      (not (zerop unread))) ; Has unread articles.
5539                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5540             (point)                     ; Success.
5541           (goto-char opoint)
5542           nil))                         ; Not success.
5543     (gnus-group-position-point)))
5544
5545 (defun gnus-group-enter-server-mode ()
5546   "Jump to the server buffer."
5547   (interactive)
5548   (gnus-enter-server-buffer))
5549
5550 (defun gnus-group-make-group (name &optional method address)
5551   "Add a new newsgroup.
5552 The user will be prompted for a NAME, for a select METHOD, and an
5553 ADDRESS."
5554   (interactive
5555    (cons
5556     (read-string "Group name: ")
5557     (let ((method
5558            (completing-read
5559             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5560             nil t nil 'gnus-method-history)))
5561       (cond ((assoc method gnus-valid-select-methods)
5562              (list method
5563                    (if (memq 'prompt-address
5564                              (assoc method gnus-valid-select-methods))
5565                        (read-string "Address: ")
5566                      "")))
5567             ((assoc method gnus-server-alist)
5568              (list method))
5569             (t
5570              (list method ""))))))
5571
5572   (let* ((meth (and method (if address (list (intern method) address)
5573                              method)))
5574          (nname (if method (gnus-group-prefixed-name name meth) name))
5575          backend info)
5576     (when (gnus-gethash nname gnus-newsrc-hashtb)
5577       (error "Group %s already exists" nname))
5578     ;; Subscribe to the new group.
5579     (gnus-group-change-level
5580      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5581      gnus-level-default-subscribed gnus-level-killed
5582      (and (gnus-group-group-name)
5583           (gnus-gethash (gnus-group-group-name)
5584                         gnus-newsrc-hashtb))
5585      t)
5586     ;; Make it active.
5587     (gnus-set-active nname (cons 1 0))
5588     (or (gnus-ephemeral-group-p name)
5589         (gnus-dribble-enter
5590          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5591     ;; Insert the line.
5592     (gnus-group-insert-group-line-info nname)
5593     (forward-line -1)
5594     (gnus-group-position-point)
5595
5596     ;; Load the backend and try to make the backend create
5597     ;; the group as well.
5598     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5599                                                   nil meth))))
5600                  gnus-valid-select-methods)
5601       (require backend))
5602     (gnus-check-server meth)
5603     (and (gnus-check-backend-function 'request-create-group nname)
5604          (gnus-request-create-group nname))
5605     t))
5606
5607 (defun gnus-group-delete-group (group &optional force)
5608   "Delete the current group.
5609 If FORCE (the prefix) is non-nil, all the articles in the group will
5610 be deleted.  This is \"deleted\" as in \"removed forever from the face
5611 of the Earth\".  There is no undo."
5612   (interactive
5613    (list (gnus-group-group-name)
5614          current-prefix-arg))
5615   (or group (error "No group to rename"))
5616   (or (gnus-check-backend-function 'request-delete-group group)
5617       (error "This backend does not support group deletion"))
5618   (prog1
5619       (if (not (gnus-yes-or-no-p
5620                 (format
5621                  "Do you really want to delete %s%s? "
5622                  group (if force " and all its contents" ""))))
5623           () ; Whew!
5624         (gnus-message 6 "Deleting group %s..." group)
5625         (if (not (gnus-request-delete-group group force))
5626             (gnus-error 3 "Couldn't delete group %s" group)
5627           (gnus-message 6 "Deleting group %s...done" group)
5628           (gnus-group-goto-group group)
5629           (gnus-group-kill-group 1 t)
5630           (gnus-sethash group nil gnus-active-hashtb)
5631           t))
5632     (gnus-group-position-point)))
5633
5634 (defun gnus-group-rename-group (group new-name)
5635   (interactive
5636    (list
5637     (gnus-group-group-name)
5638     (progn
5639       (or (gnus-check-backend-function
5640            'request-rename-group (gnus-group-group-name))
5641           (error "This backend does not support renaming groups"))
5642       (read-string "New group name: "))))
5643
5644   (or (gnus-check-backend-function 'request-rename-group group)
5645       (error "This backend does not support renaming groups"))
5646
5647   (or group (error "No group to rename"))
5648   (and (string-match "^[ \t]*$" new-name)
5649        (error "Not a valid group name"))
5650
5651   ;; We find the proper prefixed name.
5652   (setq new-name
5653         (gnus-group-prefixed-name
5654          (gnus-group-real-name new-name)
5655          (gnus-info-method (gnus-get-info group))))
5656
5657   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5658   (prog1
5659       (if (not (gnus-request-rename-group group new-name))
5660           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5661         ;; We rename the group internally by killing it...
5662         (gnus-group-goto-group group)
5663         (gnus-group-kill-group)
5664         ;; ... changing its name ...
5665         (setcar (cdar gnus-list-of-killed-groups) new-name)
5666         ;; ... and then yanking it.  Magic!
5667         (gnus-group-yank-group)
5668         (gnus-set-active new-name (gnus-active group))
5669         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5670         new-name)
5671     (gnus-group-position-point)))
5672
5673 (defun gnus-group-edit-group (group &optional part)
5674   "Edit the group on the current line."
5675   (interactive (list (gnus-group-group-name)))
5676   (let* ((part (or part 'info))
5677          (done-func `(lambda ()
5678                        "Exit editing mode and update the information."
5679                        (interactive)
5680                        (gnus-group-edit-group-done ',part ,group)))
5681          (winconf (current-window-configuration))
5682          info)
5683     (or group (error "No group on current line"))
5684     (or (setq info (gnus-get-info group))
5685         (error "Killed group; can't be edited"))
5686     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5687     (gnus-configure-windows 'edit-group)
5688     (gnus-add-current-to-buffer-list)
5689     (emacs-lisp-mode)
5690     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5691     (use-local-map (copy-keymap emacs-lisp-mode-map))
5692     (local-set-key "\C-c\C-c" done-func)
5693     (make-local-variable 'gnus-prev-winconf)
5694     (setq gnus-prev-winconf winconf)
5695     (erase-buffer)
5696     (insert
5697      (cond
5698       ((eq part 'method)
5699        ";; Type `C-c C-c' after editing the select method.\n\n")
5700       ((eq part 'params)
5701        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5702       ((eq part 'info)
5703        ";; Type `C-c C-c' after editing the group info.\n\n")))
5704     (insert
5705      (pp-to-string
5706       (cond ((eq part 'method)
5707              (or (gnus-info-method info) "native"))
5708             ((eq part 'params)
5709              (gnus-info-params info))
5710             (t info)))
5711      "\n")))
5712
5713 (defun gnus-group-edit-group-method (group)
5714   "Edit the select method of GROUP."
5715   (interactive (list (gnus-group-group-name)))
5716   (gnus-group-edit-group group 'method))
5717
5718 (defun gnus-group-edit-group-parameters (group)
5719   "Edit the group parameters of GROUP."
5720   (interactive (list (gnus-group-group-name)))
5721   (gnus-group-edit-group group 'params))
5722
5723 (defun gnus-group-edit-group-done (part group)
5724   "Get info from buffer, update variables and jump to the group buffer."
5725   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5726   (goto-char (point-min))
5727   (let* ((form (read (current-buffer)))
5728          (winconf gnus-prev-winconf)
5729          (method (cond ((eq part 'info) (nth 4 form))
5730                        ((eq part 'method) form)
5731                        (t nil)))
5732          (info (cond ((eq part 'info) form)
5733                      ((eq part 'method) (gnus-get-info group))
5734                      (t nil)))
5735          (new-group (if info
5736                       (if (or (not method)
5737                               (gnus-server-equal
5738                                gnus-select-method method))
5739                           (gnus-group-real-name (car info))
5740                         (gnus-group-prefixed-name
5741                          (gnus-group-real-name (car info)) method))
5742                       nil)))
5743     (when (and new-group
5744                (not (equal new-group group)))
5745       (when (gnus-group-goto-group group)
5746         (gnus-group-kill-group 1))
5747       (gnus-activate-group new-group))
5748     ;; Set the info.
5749     (if (and info new-group)
5750         (progn
5751           (setq info (gnus-copy-sequence info))
5752           (setcar info new-group)
5753           (unless (gnus-server-equal method "native")
5754             (unless (nthcdr 3 info)
5755               (nconc info (list nil nil)))
5756             (unless (nthcdr 4 info)
5757               (nconc info (list nil)))
5758             (gnus-info-set-method info method))
5759           (gnus-group-set-info info))
5760       (gnus-group-set-info form (or new-group group) part))
5761     (kill-buffer (current-buffer))
5762     (and winconf (set-window-configuration winconf))
5763     (set-buffer gnus-group-buffer)
5764     (gnus-group-update-group (or new-group group))
5765     (gnus-group-position-point)))
5766
5767 (defun gnus-group-make-help-group ()
5768   "Create the Gnus documentation group."
5769   (interactive)
5770   (let ((path load-path)
5771         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5772         file dir)
5773     (and (gnus-gethash name gnus-newsrc-hashtb)
5774          (error "Documentation group already exists"))
5775     (while path
5776       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5777             file nil)
5778       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5779                 (file-exists-p
5780                  (setq file (concat (file-name-directory
5781                                      (directory-file-name dir))
5782                                     "etc/gnus-tut.txt"))))
5783         (setq path nil)))
5784     (if (not file)
5785         (gnus-message 1 "Couldn't find doc group")
5786       (gnus-group-make-group
5787        (gnus-group-real-name name)
5788        (list 'nndoc "gnus-help"
5789              (list 'nndoc-address file)
5790              (list 'nndoc-article-type 'mbox)))))
5791   (gnus-group-position-point))
5792
5793 (defun gnus-group-make-doc-group (file type)
5794   "Create a group that uses a single file as the source."
5795   (interactive
5796    (list (read-file-name "File name: ")
5797          (and current-prefix-arg 'ask)))
5798   (when (eq type 'ask)
5799     (let ((err "")
5800           char found)
5801       (while (not found)
5802         (message
5803          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5804          err)
5805         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5806                           ((= char ?b) 'babyl)
5807                           ((= char ?d) 'digest)
5808                           ((= char ?f) 'forward)
5809                           ((= char ?a) 'mmfd)
5810                           (t (setq err (format "%c unknown. " char))
5811                              nil))))
5812       (setq type found)))
5813   (let* ((file (expand-file-name file))
5814          (name (gnus-generate-new-group-name
5815                 (gnus-group-prefixed-name
5816                  (file-name-nondirectory file) '(nndoc "")))))
5817     (gnus-group-make-group
5818      (gnus-group-real-name name)
5819      (list 'nndoc (file-name-nondirectory file)
5820            (list 'nndoc-address file)
5821            (list 'nndoc-article-type (or type 'guess))))))
5822
5823 (defun gnus-group-make-archive-group (&optional all)
5824   "Create the (ding) Gnus archive group of the most recent articles.
5825 Given a prefix, create a full group."
5826   (interactive "P")
5827   (let ((group (gnus-group-prefixed-name
5828                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5829     (and (gnus-gethash group gnus-newsrc-hashtb)
5830          (error "Archive group already exists"))
5831     (gnus-group-make-group
5832      (gnus-group-real-name group)
5833      (list 'nndir (if all "hpc" "edu")
5834            (list 'nndir-directory
5835                  (if all gnus-group-archive-directory
5836                    gnus-group-recent-archive-directory))))))
5837
5838 (defun gnus-group-make-directory-group (dir)
5839   "Create an nndir group.
5840 The user will be prompted for a directory.  The contents of this
5841 directory will be used as a newsgroup.  The directory should contain
5842 mail messages or news articles in files that have numeric names."
5843   (interactive
5844    (list (read-file-name "Create group from directory: ")))
5845   (or (file-exists-p dir) (error "No such directory"))
5846   (or (file-directory-p dir) (error "Not a directory"))
5847   (let ((ext "")
5848         (i 0)
5849         group)
5850     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5851       (setq group
5852             (gnus-group-prefixed-name
5853              (concat (file-name-as-directory (directory-file-name dir))
5854                      ext)
5855              '(nndir "")))
5856       (setq ext (format "<%d>" (setq i (1+ i)))))
5857     (gnus-group-make-group
5858      (gnus-group-real-name group)
5859      (list 'nndir group (list 'nndir-directory dir)))))
5860
5861 (defun gnus-group-make-kiboze-group (group address scores)
5862   "Create an nnkiboze group.
5863 The user will be prompted for a name, a regexp to match groups, and
5864 score file entries for articles to include in the group."
5865   (interactive
5866    (list
5867     (read-string "nnkiboze group name: ")
5868     (read-string "Source groups (regexp): ")
5869     (let ((headers (mapcar (lambda (group) (list group))
5870                            '("subject" "from" "number" "date" "message-id"
5871                              "references" "chars" "lines" "xref"
5872                              "followup" "all" "body" "head")))
5873           scores header regexp regexps)
5874       (while (not (equal "" (setq header (completing-read
5875                                           "Match on header: " headers nil t))))
5876         (setq regexps nil)
5877         (while (not (equal "" (setq regexp (read-string
5878                                             (format "Match on %s (string): "
5879                                                     header)))))
5880           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5881         (setq scores (cons (cons header regexps) scores)))
5882       scores)))
5883   (gnus-group-make-group group "nnkiboze" address)
5884   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5885     (let (emacs-lisp-mode-hook)
5886       (pp scores (current-buffer)))))
5887
5888 (defun gnus-group-add-to-virtual (n vgroup)
5889   "Add the current group to a virtual group."
5890   (interactive
5891    (list current-prefix-arg
5892          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5893                           "nnvirtual:")))
5894   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5895       (error "%s is not an nnvirtual group" vgroup))
5896   (let* ((groups (gnus-group-process-prefix n))
5897          (method (gnus-info-method (gnus-get-info vgroup))))
5898     (setcar (cdr method)
5899             (concat
5900              (nth 1 method) "\\|"
5901              (mapconcat
5902               (lambda (s)
5903                 (gnus-group-remove-mark s)
5904                 (concat "\\(^" (regexp-quote s) "$\\)"))
5905               groups "\\|"))))
5906   (gnus-group-position-point))
5907
5908 (defun gnus-group-make-empty-virtual (group)
5909   "Create a new, fresh, empty virtual group."
5910   (interactive "sCreate new, empty virtual group: ")
5911   (let* ((method (list 'nnvirtual "^$"))
5912          (pgroup (gnus-group-prefixed-name group method)))
5913     ;; Check whether it exists already.
5914     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5915          (error "Group %s already exists." pgroup))
5916     ;; Subscribe the new group after the group on the current line.
5917     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5918     (gnus-group-update-group pgroup)
5919     (forward-line -1)
5920     (gnus-group-position-point)))
5921
5922 (defun gnus-group-enter-directory (dir)
5923   "Enter an ephemeral nneething group."
5924   (interactive "DDirectory to read: ")
5925   (let* ((method (list 'nneething dir))
5926          (leaf (gnus-group-prefixed-name
5927                 (file-name-nondirectory (directory-file-name dir))
5928                 method))
5929          (name (gnus-generate-new-group-name leaf)))
5930     (let ((nneething-read-only t))
5931       (or (gnus-group-read-ephemeral-group
5932            name method t
5933            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5934                                       'summary 'group)))
5935           (error "Couldn't enter %s" dir)))))
5936
5937 ;; Group sorting commands
5938 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5939
5940 (defun gnus-group-sort-groups (func &optional reverse)
5941   "Sort the group buffer according to FUNC.
5942 If REVERSE, reverse the sorting order."
5943   (interactive (list gnus-group-sort-function
5944                      current-prefix-arg))
5945   (let ((func (cond 
5946                ((not (listp func)) func)
5947                ((null func) func)
5948                ((= 1 (length func)) (car func))
5949                (t `(lambda (t1 t2)
5950                      ,(gnus-make-sort-function 
5951                        (reverse func)))))))
5952     ;; We peel off the dummy group from the alist.
5953     (when func
5954       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5955         (pop gnus-newsrc-alist))
5956       ;; Do the sorting.
5957       (setq gnus-newsrc-alist
5958             (sort gnus-newsrc-alist func))
5959       (when reverse
5960         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5961       ;; Regenerate the hash table.
5962       (gnus-make-hashtable-from-newsrc-alist)
5963       (gnus-group-list-groups))))
5964
5965 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5966   "Sort the group buffer alphabetically by group name.
5967 If REVERSE, sort in reverse order."
5968   (interactive "P")
5969   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5970
5971 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5972   "Sort the group buffer by number of unread articles.
5973 If REVERSE, sort in reverse order."
5974   (interactive "P")
5975   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5976
5977 (defun gnus-group-sort-groups-by-level (&optional reverse)
5978   "Sort the group buffer by group level.
5979 If REVERSE, sort in reverse order."
5980   (interactive "P")
5981   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5982
5983 (defun gnus-group-sort-groups-by-score (&optional reverse)
5984   "Sort the group buffer by group score.
5985 If REVERSE, sort in reverse order."
5986   (interactive "P")
5987   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5988
5989 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5990   "Sort the group buffer by group rank.
5991 If REVERSE, sort in reverse order."
5992   (interactive "P")
5993   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5994
5995 (defun gnus-group-sort-groups-by-method (&optional reverse)
5996   "Sort the group buffer alphabetically by backend name.
5997 If REVERSE, sort in reverse order."
5998   (interactive "P")
5999   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6000
6001 (defun gnus-group-sort-by-alphabet (info1 info2)
6002   "Sort alphabetically."
6003   (string< (gnus-info-group info1) (gnus-info-group info2)))
6004
6005 (defun gnus-group-sort-by-unread (info1 info2)
6006   "Sort by number of unread articles."
6007   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6008         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6009     (< (or (and (numberp n1) n1) 0)
6010        (or (and (numberp n2) n2) 0))))
6011
6012 (defun gnus-group-sort-by-level (info1 info2)
6013   "Sort by level."
6014   (< (gnus-info-level info1) (gnus-info-level info2)))
6015
6016 (defun gnus-group-sort-by-method (info1 info2)
6017   "Sort alphabetically by backend name."
6018   (string< (symbol-name (car (gnus-find-method-for-group
6019                               (gnus-info-group info1) info1)))
6020            (symbol-name (car (gnus-find-method-for-group
6021                               (gnus-info-group info2) info2)))))
6022
6023 (defun gnus-group-sort-by-score (info1 info2)
6024   "Sort by group score."
6025   (< (gnus-info-score info1) (gnus-info-score info2)))
6026
6027 (defun gnus-group-sort-by-rank (info1 info2)
6028   "Sort by level and score."
6029   (let ((level1 (gnus-info-level info1))
6030         (level2 (gnus-info-level info2)))
6031     (or (< level1 level2)
6032         (and (= level1 level2)
6033              (> (gnus-info-score info1) (gnus-info-score info2))))))
6034
6035 ;; Group catching up.
6036
6037 (defun gnus-group-clear-data (n)
6038   "Clear all marks and read ranges from the current group."
6039   (interactive "P")
6040   (let ((groups (gnus-group-process-prefix n))
6041         group info)
6042     (while (setq group (pop groups))
6043       (setq info (gnus-get-info group))
6044       (gnus-info-set-read info nil)
6045       (when (gnus-info-marks info)
6046         (gnus-info-set-marks info nil))
6047       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6048       (when (gnus-group-goto-group group)
6049         (gnus-group-remove-mark group)
6050         (gnus-group-update-group-line)))))
6051
6052 (defun gnus-group-catchup-current (&optional n all)
6053   "Mark all articles not marked as unread in current newsgroup as read.
6054 If prefix argument N is numeric, the ARG next newsgroups will be
6055 caught up.  If ALL is non-nil, marked articles will also be marked as
6056 read.  Cross references (Xref: header) of articles are ignored.
6057 The difference between N and actual number of newsgroups that were
6058 caught up is returned."
6059   (interactive "P")
6060   (unless (gnus-group-group-name)
6061     (error "No group on the current line"))
6062   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6063                gnus-expert-user
6064                (gnus-y-or-n-p
6065                 (if all
6066                     "Do you really want to mark all articles as read? "
6067                   "Mark all unread articles as read? "))))
6068       n
6069     (let ((groups (gnus-group-process-prefix n))
6070           (ret 0))
6071       (while groups
6072         ;; Virtual groups have to be given special treatment.
6073         (let ((method (gnus-find-method-for-group (car groups))))
6074           (if (eq 'nnvirtual (car method))
6075               (nnvirtual-catchup-group
6076                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6077         (gnus-group-remove-mark (car groups))
6078         (if (>= (gnus-group-group-level) gnus-level-zombie)
6079             (gnus-message 2 "Dead groups can't be caught up")
6080           (if (prog1
6081                   (gnus-group-goto-group (car groups))
6082                 (gnus-group-catchup (car groups) all))
6083               (gnus-group-update-group-line)
6084             (setq ret (1+ ret))))
6085         (setq groups (cdr groups)))
6086       (gnus-group-next-unread-group 1)
6087       ret)))
6088
6089 (defun gnus-group-catchup-current-all (&optional n)
6090   "Mark all articles in current newsgroup as read.
6091 Cross references (Xref: header) of articles are ignored."
6092   (interactive "P")
6093   (gnus-group-catchup-current n 'all))
6094
6095 (defun gnus-group-catchup (group &optional all)
6096   "Mark all articles in GROUP as read.
6097 If ALL is non-nil, all articles are marked as read.
6098 The return value is the number of articles that were marked as read,
6099 or nil if no action could be taken."
6100   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6101          (num (car entry)))
6102     ;; Do the updating only if the newsgroup isn't killed.
6103     (if (not (numberp (car entry)))
6104         (gnus-message 1 "Can't catch up; non-active group")
6105       ;; Do auto-expirable marks if that's required.
6106       (when (gnus-group-auto-expirable-p group)
6107         (gnus-add-marked-articles
6108          group 'expire (gnus-list-of-unread-articles group))
6109         (when all
6110           (let ((marks (nth 3 (nth 2 entry))))
6111             (gnus-add-marked-articles
6112              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6113             (gnus-add-marked-articles
6114              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6115       (when entry
6116         (gnus-update-read-articles group nil)
6117         ;; Also nix out the lists of marks and dormants.
6118         (when all
6119           (gnus-add-marked-articles group 'tick nil nil 'force)
6120           (gnus-add-marked-articles group 'dormant nil nil 'force))
6121         (run-hooks 'gnus-group-catchup-group-hook)
6122         num))))
6123
6124 (defun gnus-group-expire-articles (&optional n)
6125   "Expire all expirable articles in the current newsgroup."
6126   (interactive "P")
6127   (let ((groups (gnus-group-process-prefix n))
6128         group)
6129     (unless groups
6130       (error "No groups to expire"))
6131     (while (setq group (pop groups))
6132       (gnus-group-remove-mark group)
6133       (when (gnus-check-backend-function 'request-expire-articles group)
6134         (gnus-message 6 "Expiring articles in %s..." group)
6135         (let* ((info (gnus-get-info group))
6136                (expirable (if (gnus-group-total-expirable-p group)
6137                               (cons nil (gnus-list-of-read-articles group))
6138                             (assq 'expire (gnus-info-marks info))))
6139                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6140           (when expirable
6141             (setcdr
6142              expirable
6143              (gnus-compress-sequence
6144               (if expiry-wait
6145                   ;; We set the expiry variables to the groupp
6146                   ;; parameter. 
6147                   (let ((nnmail-expiry-wait-function nil)
6148                         (nnmail-expiry-wait expiry-wait))
6149                     (gnus-request-expire-articles
6150                      (gnus-uncompress-sequence (cdr expirable)) group))
6151                 ;; Just expire using the normal expiry values.
6152                 (gnus-request-expire-articles
6153                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6154           (gnus-message 6 "Expiring articles in %s...done" group)))
6155       (gnus-group-position-point))))
6156
6157 (defun gnus-group-expire-all-groups ()
6158   "Expire all expirable articles in all newsgroups."
6159   (interactive)
6160   (save-excursion
6161     (gnus-message 5 "Expiring...")
6162     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6163                                      (cdr gnus-newsrc-alist))))
6164       (gnus-group-expire-articles nil)))
6165   (gnus-group-position-point)
6166   (gnus-message 5 "Expiring...done"))
6167
6168 (defun gnus-group-set-current-level (n level)
6169   "Set the level of the next N groups to LEVEL."
6170   (interactive
6171    (list
6172     current-prefix-arg
6173     (string-to-int
6174      (let ((s (read-string
6175                (format "Level (default %s): "
6176                        (or (gnus-group-group-level) 
6177                            gnus-level-default-subscribed)))))
6178        (if (string-match "^\\s-*$" s)
6179            (int-to-string (or (gnus-group-group-level) 
6180                               gnus-level-default-subscribed))
6181          s)))))
6182   (or (and (>= level 1) (<= level gnus-level-killed))
6183       (error "Illegal level: %d" level))
6184   (let ((groups (gnus-group-process-prefix n))
6185         group)
6186     (while (setq group (pop groups))
6187       (gnus-group-remove-mark group)
6188       (gnus-message 6 "Changed level of %s from %d to %d"
6189                     group (or (gnus-group-group-level) gnus-level-killed)
6190                     level)
6191       (gnus-group-change-level
6192        group level (or (gnus-group-group-level) gnus-level-killed))
6193       (gnus-group-update-group-line)))
6194   (gnus-group-position-point))
6195
6196 (defun gnus-group-unsubscribe-current-group (&optional n)
6197   "Toggle subscription of the current group.
6198 If given numerical prefix, toggle the N next groups."
6199   (interactive "P")
6200   (let ((groups (gnus-group-process-prefix n))
6201         group)
6202     (while groups
6203       (setq group (car groups)
6204             groups (cdr groups))
6205       (gnus-group-remove-mark group)
6206       (gnus-group-unsubscribe-group
6207        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6208                  gnus-level-default-unsubscribed
6209                gnus-level-default-subscribed) t)
6210       (gnus-group-update-group-line))
6211     (gnus-group-next-group 1)))
6212
6213 (defun gnus-group-unsubscribe-group (group &optional level silent)
6214   "Toggle subscription to GROUP.
6215 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6216 group line."
6217   (interactive
6218    (list (completing-read
6219           "Group: " gnus-active-hashtb nil
6220           (gnus-read-active-file-p)
6221           nil 
6222           'gnus-group-history)))
6223   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6224     (cond
6225      ((string-match "^[ \t]$" group)
6226       (error "Empty group name"))
6227      (newsrc
6228       ;; Toggle subscription flag.
6229       (gnus-group-change-level
6230        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6231                                       gnus-level-subscribed)
6232                                   (1+ gnus-level-subscribed)
6233                                 gnus-level-default-subscribed)))
6234       (unless silent
6235         (gnus-group-update-group group)))
6236      ((and (stringp group)
6237            (or (not (gnus-read-active-file-p))
6238                (gnus-active group)))
6239       ;; Add new newsgroup.
6240       (gnus-group-change-level
6241        group
6242        (if level level gnus-level-default-subscribed)
6243        (or (and (member group gnus-zombie-list)
6244                 gnus-level-zombie)
6245            gnus-level-killed)
6246        (and (gnus-group-group-name)
6247             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6248       (unless silent
6249         (gnus-group-update-group group)))
6250      (t (error "No such newsgroup: %s" group)))
6251     (gnus-group-position-point)))
6252
6253 (defun gnus-group-transpose-groups (n)
6254   "Move the current newsgroup up N places.
6255 If given a negative prefix, move down instead.  The difference between
6256 N and the number of steps taken is returned."
6257   (interactive "p")
6258   (or (gnus-group-group-name)
6259       (error "No group on current line"))
6260   (gnus-group-kill-group 1)
6261   (prog1
6262       (forward-line (- n))
6263     (gnus-group-yank-group)
6264     (gnus-group-position-point)))
6265
6266 (defun gnus-group-kill-all-zombies ()
6267   "Kill all zombie newsgroups."
6268   (interactive)
6269   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6270   (setq gnus-zombie-list nil)
6271   (gnus-group-list-groups))
6272
6273 (defun gnus-group-kill-region (begin end)
6274   "Kill newsgroups in current region (excluding current point).
6275 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6276   (interactive "r")
6277   (let ((lines
6278          ;; Count lines.
6279          (save-excursion
6280            (count-lines
6281             (progn
6282               (goto-char begin)
6283               (beginning-of-line)
6284               (point))
6285             (progn
6286               (goto-char end)
6287               (beginning-of-line)
6288               (point))))))
6289     (goto-char begin)
6290     (beginning-of-line)                 ;Important when LINES < 1
6291     (gnus-group-kill-group lines)))
6292
6293 (defun gnus-group-kill-group (&optional n discard)
6294   "Kill the next N groups.
6295 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6296 However, only groups that were alive can be yanked; already killed
6297 groups or zombie groups can't be yanked.
6298 The return value is the name of the group that was killed, or a list
6299 of groups killed."
6300   (interactive "P")
6301   (let ((buffer-read-only nil)
6302         (groups (gnus-group-process-prefix n))
6303         group entry level out)
6304     (if (< (length groups) 10)
6305         ;; This is faster when there are few groups.
6306         (while groups
6307           (push (setq group (pop groups)) out)
6308           (gnus-group-remove-mark group)
6309           (setq level (gnus-group-group-level))
6310           (gnus-delete-line)
6311           (when (and (not discard)
6312                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6313             (push (cons (car entry) (nth 2 entry))
6314                   gnus-list-of-killed-groups))
6315           (gnus-group-change-level
6316            (if entry entry group) gnus-level-killed (if entry nil level)))
6317       ;; If there are lots and lots of groups to be killed, we use
6318       ;; this thing instead.
6319       (let (entry)
6320         (setq groups (nreverse groups))
6321         (while groups
6322           (gnus-group-remove-mark (setq group (pop groups)))
6323           (gnus-delete-line)
6324           (cond
6325            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6326             (push (cons (car entry) (nth 2 entry))
6327                   gnus-list-of-killed-groups)
6328             (setcdr (cdr entry) (cdddr entry)))
6329            ((member group gnus-zombie-list)
6330             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6331         (gnus-make-hashtable-from-newsrc-alist)))
6332
6333     (gnus-group-position-point)
6334     (if (< (length out) 2) (car out) (nreverse out))))
6335
6336 (defun gnus-group-yank-group (&optional arg)
6337   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6338 inserting it before the current newsgroup.  The numeric ARG specifies
6339 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6340 is returned, or (if several groups are yanked) a list of yanked groups
6341 is returned."
6342   (interactive "p")
6343   (setq arg (or arg 1))
6344   (let (info group prev out)
6345     (while (>= (decf arg) 0)
6346       (if (not (setq info (pop gnus-list-of-killed-groups)))
6347           (error "No more newsgroups to yank"))
6348       (push (setq group (nth 1 info)) out)
6349       ;; Find which newsgroup to insert this one before - search
6350       ;; backward until something suitable is found.  If there are no
6351       ;; other newsgroups in this buffer, just make this newsgroup the
6352       ;; first newsgroup.
6353       (setq prev (gnus-group-group-name))
6354       (gnus-group-change-level
6355        info (gnus-info-level (cdr info)) gnus-level-killed
6356        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6357        t)
6358       (gnus-group-insert-group-line-info group))
6359     (forward-line -1)
6360     (gnus-group-position-point)
6361     (if (< (length out) 2) (car out) (nreverse out))))
6362
6363 (defun gnus-group-kill-level (level)
6364   "Kill all groups that is on a certain LEVEL."
6365   (interactive "nKill all groups on level: ")
6366   (cond
6367    ((= level gnus-level-zombie)
6368     (setq gnus-killed-list
6369           (nconc gnus-zombie-list gnus-killed-list))
6370     (setq gnus-zombie-list nil))
6371    ((and (< level gnus-level-zombie)
6372          (> level 0)
6373          (or gnus-expert-user
6374              (gnus-yes-or-no-p
6375               (format
6376                "Do you really want to kill all groups on level %d? "
6377                level))))
6378     (let* ((prev gnus-newsrc-alist)
6379            (alist (cdr prev)))
6380       (while alist
6381         (if (= (gnus-info-level level) level)
6382             (setcdr prev (cdr alist))
6383           (setq prev alist))
6384         (setq alist (cdr alist)))
6385       (gnus-make-hashtable-from-newsrc-alist)
6386       (gnus-group-list-groups)))
6387    (t
6388     (error "Can't kill; illegal level: %d" level))))
6389
6390 (defun gnus-group-list-all-groups (&optional arg)
6391   "List all newsgroups with level ARG or lower.
6392 Default is gnus-level-unsubscribed, which lists all subscribed and most
6393 unsubscribed groups."
6394   (interactive "P")
6395   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6396
6397 ;; Redefine this to list ALL killed groups if prefix arg used.
6398 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6399 (defun gnus-group-list-killed (&optional arg)
6400   "List all killed newsgroups in the group buffer.
6401 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6402 entail asking the server for the groups."
6403   (interactive "P")
6404   ;; Find all possible killed newsgroups if arg.
6405   (when arg
6406     (gnus-get-killed-groups))
6407   (if (not gnus-killed-list)
6408       (gnus-message 6 "No killed groups")
6409     (let (gnus-group-list-mode)
6410       (funcall gnus-group-prepare-function
6411                gnus-level-killed t gnus-level-killed))
6412     (goto-char (point-min)))
6413   (gnus-group-position-point))
6414
6415 (defun gnus-group-list-zombies ()
6416   "List all zombie newsgroups in the group buffer."
6417   (interactive)
6418   (if (not gnus-zombie-list)
6419       (gnus-message 6 "No zombie groups")
6420     (let (gnus-group-list-mode)
6421       (funcall gnus-group-prepare-function
6422                gnus-level-zombie t gnus-level-zombie))
6423     (goto-char (point-min)))
6424   (gnus-group-position-point))
6425
6426 (defun gnus-group-list-active ()
6427   "List all groups that are available from the server(s)."
6428   (interactive)
6429   ;; First we make sure that we have really read the active file.
6430   (unless (gnus-read-active-file-p)
6431     (let ((gnus-read-active-file t))
6432       (gnus-read-active-file)))
6433   ;; Find all groups and sort them.
6434   (let ((groups
6435          (sort
6436           (let (list)
6437             (mapatoms
6438              (lambda (sym)
6439                (and (boundp sym)
6440                     (symbol-value sym)
6441                     (setq list (cons (symbol-name sym) list))))
6442              gnus-active-hashtb)
6443             list)
6444           'string<))
6445         (buffer-read-only nil))
6446     (erase-buffer)
6447     (while groups
6448       (gnus-group-insert-group-line-info (pop groups)))
6449     (goto-char (point-min))))
6450
6451 (defun gnus-activate-all-groups (level)
6452   "Activate absolutely all groups."
6453   (interactive (list 7))
6454   (let ((gnus-activate-level level)
6455         (gnus-activate-foreign-newsgroups level))
6456     (gnus-group-get-new-news)))
6457
6458 (defun gnus-group-get-new-news (&optional arg)
6459   "Get newly arrived articles.
6460 If ARG is a number, it specifies which levels you are interested in
6461 re-scanning.  If ARG is non-nil and not a number, this will force
6462 \"hard\" re-reading of the active files from all servers."
6463   (interactive "P")
6464   (run-hooks 'gnus-get-new-news-hook)
6465   ;; We might read in new NoCeM messages here.
6466   (when (and gnus-use-nocem 
6467              (null arg))
6468     (gnus-nocem-scan-groups))
6469   ;; If ARG is not a number, then we read the active file.
6470   (when (and arg (not (numberp arg)))
6471     (let ((gnus-read-active-file t))
6472       (gnus-read-active-file))
6473     (setq arg nil))
6474
6475   (setq arg (gnus-group-default-level arg t))
6476   (if (and gnus-read-active-file (not arg))
6477       (progn
6478         (gnus-read-active-file)
6479         (gnus-get-unread-articles arg))
6480     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6481       (gnus-get-unread-articles arg)))
6482   (run-hooks 'gnus-after-getting-new-news-hook)
6483   (gnus-group-list-groups))
6484
6485 (defun gnus-group-get-new-news-this-group (&optional n)
6486   "Check for newly arrived news in the current group (and the N-1 next groups).
6487 The difference between N and the number of newsgroup checked is returned.
6488 If N is negative, this group and the N-1 previous groups will be checked."
6489   (interactive "P")
6490   (let* ((groups (gnus-group-process-prefix n))
6491          (ret (if (numberp n) (- n (length groups)) 0))
6492          (beg (unless n (point)))
6493          group)
6494     (while (setq group (pop groups))
6495       (gnus-group-remove-mark group)
6496       (if (gnus-activate-group group 'scan)
6497           (progn
6498             (gnus-get-unread-articles-in-group
6499              (gnus-get-info group) (gnus-active group) t)
6500             (unless (gnus-virtual-group-p group)
6501               (gnus-close-group group))
6502             (gnus-group-update-group group))
6503         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6504     (when beg (goto-char beg))
6505     (when gnus-goto-next-group-when-activating
6506       (gnus-group-next-unread-group 1 t))
6507     (gnus-summary-position-point)
6508     ret))
6509
6510 (defun gnus-group-fetch-faq (group &optional faq-dir)
6511   "Fetch the FAQ for the current group."
6512   (interactive
6513    (list
6514     (gnus-group-real-name (gnus-group-group-name))
6515     (cond (current-prefix-arg
6516            (completing-read
6517             "Faq dir: " (and (listp gnus-group-faq-directory)
6518                              (mapcar (lambda (file) (list file))
6519                                      gnus-group-faq-directory)))))))
6520   (or faq-dir
6521       (setq faq-dir (if (listp gnus-group-faq-directory)
6522                         (car gnus-group-faq-directory)
6523                       gnus-group-faq-directory)))
6524   (or group (error "No group name given"))
6525   (let ((file (concat (file-name-as-directory faq-dir)
6526                       (gnus-group-real-name group))))
6527     (if (not (file-exists-p file))
6528         (error "No such file: %s" file)
6529       (find-file file))))
6530
6531 (defun gnus-group-describe-group (force &optional group)
6532   "Display a description of the current newsgroup."
6533   (interactive (list current-prefix-arg (gnus-group-group-name)))
6534   (and force (setq gnus-description-hashtb nil))
6535   (let ((method (gnus-find-method-for-group group))
6536         desc)
6537     (or group (error "No group name given"))
6538     (and (or (and gnus-description-hashtb
6539                   ;; We check whether this group's method has been
6540                   ;; queried for a description file.
6541                   (gnus-gethash
6542                    (gnus-group-prefixed-name "" method)
6543                    gnus-description-hashtb))
6544              (setq desc (gnus-group-get-description group))
6545              (gnus-read-descriptions-file method))
6546          (gnus-message 1
6547           (or desc (gnus-gethash group gnus-description-hashtb)
6548               "No description available")))))
6549
6550 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6551 (defun gnus-group-describe-all-groups (&optional force)
6552   "Pop up a buffer with descriptions of all newsgroups."
6553   (interactive "P")
6554   (and force (setq gnus-description-hashtb nil))
6555   (if (not (or gnus-description-hashtb
6556                (gnus-read-all-descriptions-files)))
6557       (error "Couldn't request descriptions file"))
6558   (let ((buffer-read-only nil)
6559         b)
6560     (erase-buffer)
6561     (mapatoms
6562      (lambda (group)
6563        (setq b (point))
6564        (insert (format "      *: %-20s %s\n" (symbol-name group)
6565                        (symbol-value group)))
6566        (gnus-add-text-properties
6567         b (1+ b) (list 'gnus-group group
6568                        'gnus-unread t 'gnus-marked nil
6569                        'gnus-level (1+ gnus-level-subscribed))))
6570      gnus-description-hashtb)
6571     (goto-char (point-min))
6572     (gnus-group-position-point)))
6573
6574 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6575 (defun gnus-group-apropos (regexp &optional search-description)
6576   "List all newsgroups that have names that match a regexp."
6577   (interactive "sGnus apropos (regexp): ")
6578   (let ((prev "")
6579         (obuf (current-buffer))
6580         groups des)
6581     ;; Go through all newsgroups that are known to Gnus.
6582     (mapatoms
6583      (lambda (group)
6584        (and (symbol-name group)
6585             (string-match regexp (symbol-name group))
6586             (setq groups (cons (symbol-name group) groups))))
6587      gnus-active-hashtb)
6588     ;; Also go through all descriptions that are known to Gnus.
6589     (when search-description
6590       (mapatoms
6591        (lambda (group)
6592          (and (string-match regexp (symbol-value group))
6593               (gnus-active (symbol-name group))
6594               (setq groups (cons (symbol-name group) groups))))
6595        gnus-description-hashtb))
6596     (if (not groups)
6597         (gnus-message 3 "No groups matched \"%s\"." regexp)
6598       ;; Print out all the groups.
6599       (save-excursion
6600         (pop-to-buffer "*Gnus Help*")
6601         (buffer-disable-undo (current-buffer))
6602         (erase-buffer)
6603         (setq groups (sort groups 'string<))
6604         (while groups
6605           ;; Groups may be entered twice into the list of groups.
6606           (if (not (string= (car groups) prev))
6607               (progn
6608                 (insert (setq prev (car groups)) "\n")
6609                 (if (and gnus-description-hashtb
6610                          (setq des (gnus-gethash (car groups)
6611                                                  gnus-description-hashtb)))
6612                     (insert "  " des "\n"))))
6613           (setq groups (cdr groups)))
6614         (goto-char (point-min))))
6615     (pop-to-buffer obuf)))
6616
6617 (defun gnus-group-description-apropos (regexp)
6618   "List all newsgroups that have names or descriptions that match a regexp."
6619   (interactive "sGnus description apropos (regexp): ")
6620   (if (not (or gnus-description-hashtb
6621                (gnus-read-all-descriptions-files)))
6622       (error "Couldn't request descriptions file"))
6623   (gnus-group-apropos regexp t))
6624
6625 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6626 (defun gnus-group-list-matching (level regexp &optional all lowest)
6627   "List all groups with unread articles that match REGEXP.
6628 If the prefix LEVEL is non-nil, it should be a number that says which
6629 level to cut off listing groups.
6630 If ALL, also list groups with no unread articles.
6631 If LOWEST, don't list groups with level lower than LOWEST.
6632
6633 This command may read the active file."
6634   (interactive "P\nsList newsgroups matching: ")
6635   ;; First make sure active file has been read.
6636   (when (and level
6637              (> (prefix-numeric-value level) gnus-level-killed))
6638     (gnus-get-killed-groups))
6639   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6640                            all (or lowest 1) regexp)
6641   (goto-char (point-min))
6642   (gnus-group-position-point))
6643
6644 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6645   "List all groups that match REGEXP.
6646 If the prefix LEVEL is non-nil, it should be a number that says which
6647 level to cut off listing groups.
6648 If LOWEST, don't list groups with level lower than LOWEST."
6649   (interactive "P\nsList newsgroups matching: ")
6650   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6651
6652 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6653 (defun gnus-group-save-newsrc (&optional force)
6654   "Save the Gnus startup files.
6655 If FORCE, force saving whether it is necessary or not."
6656   (interactive "P")
6657   (gnus-save-newsrc-file force))
6658
6659 (defun gnus-group-restart (&optional arg)
6660   "Force Gnus to read the .newsrc file."
6661   (interactive "P")
6662   (when (gnus-yes-or-no-p
6663          (format "Are you sure you want to read %s? "
6664                  gnus-current-startup-file))
6665     (gnus-save-newsrc-file)
6666     (gnus-setup-news 'force)
6667     (gnus-group-list-groups arg)))
6668
6669 (defun gnus-group-read-init-file ()
6670   "Read the Gnus elisp init file."
6671   (interactive)
6672   (gnus-read-init-file))
6673
6674 (defun gnus-group-check-bogus-groups (&optional silent)
6675   "Check bogus newsgroups.
6676 If given a prefix, don't ask for confirmation before removing a bogus
6677 group."
6678   (interactive "P")
6679   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6680   (gnus-group-list-groups))
6681
6682 (defun gnus-group-edit-global-kill (&optional article group)
6683   "Edit the global kill file.
6684 If GROUP, edit that local kill file instead."
6685   (interactive "P")
6686   (setq gnus-current-kill-article article)
6687   (gnus-kill-file-edit-file group)
6688   (gnus-message
6689    6
6690    (substitute-command-keys
6691     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6692             (if group "local" "global")))))
6693
6694 (defun gnus-group-edit-local-kill (article group)
6695   "Edit a local kill file."
6696   (interactive (list nil (gnus-group-group-name)))
6697   (gnus-group-edit-global-kill article group))
6698
6699 (defun gnus-group-force-update ()
6700   "Update `.newsrc' file."
6701   (interactive)
6702   (gnus-save-newsrc-file))
6703
6704 (defun gnus-group-suspend ()
6705   "Suspend the current Gnus session.
6706 In fact, cleanup buffers except for group mode buffer.
6707 The hook gnus-suspend-gnus-hook is called before actually suspending."
6708   (interactive)
6709   (run-hooks 'gnus-suspend-gnus-hook)
6710   ;; Kill Gnus buffers except for group mode buffer.
6711   (let ((group-buf (get-buffer gnus-group-buffer)))
6712     ;; Do this on a separate list in case the user does a ^G before we finish
6713     (let ((gnus-buffer-list
6714            (delq group-buf (delq gnus-dribble-buffer
6715                                  (append gnus-buffer-list nil)))))
6716       (while gnus-buffer-list
6717         (gnus-kill-buffer (car gnus-buffer-list))
6718         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6719     (if group-buf
6720         (progn
6721           (setq gnus-buffer-list (list group-buf))
6722           (bury-buffer group-buf)
6723           (delete-windows-on group-buf t)))))
6724
6725 (defun gnus-group-clear-dribble ()
6726   "Clear all information from the dribble buffer."
6727   (interactive)
6728   (gnus-dribble-clear)
6729   (gnus-message 7 "Cleared dribble buffer"))
6730
6731 (defun gnus-group-exit ()
6732   "Quit reading news after updating .newsrc.eld and .newsrc.
6733 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6734   (interactive)
6735   (when 
6736       (or noninteractive                ;For gnus-batch-kill
6737           (not gnus-interactive-exit)   ;Without confirmation
6738           gnus-expert-user
6739           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6740     (run-hooks 'gnus-exit-gnus-hook)
6741     ;; Offer to save data from non-quitted summary buffers.
6742     (gnus-offer-save-summaries)
6743     ;; Save the newsrc file(s).
6744     (gnus-save-newsrc-file)
6745     ;; Kill-em-all.
6746     (gnus-close-backends)
6747     ;; Reset everything.
6748     (gnus-clear-system)
6749     ;; Allow the user to do things after cleaning up.
6750     (run-hooks 'gnus-after-exiting-gnus-hook)))
6751
6752 (defun gnus-close-backends ()
6753   ;; Send a close request to all backends that support such a request.
6754   (let ((methods gnus-valid-select-methods)
6755         func)
6756     (while methods
6757       (if (fboundp (setq func (intern (concat (caar methods)
6758                                               "-request-close"))))
6759           (funcall func))
6760       (setq methods (cdr methods)))))
6761
6762 (defun gnus-group-quit ()
6763   "Quit reading news without updating .newsrc.eld or .newsrc.
6764 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6765   (interactive)
6766   (when (or noninteractive              ;For gnus-batch-kill
6767             (zerop (buffer-size))
6768             (not (gnus-server-opened gnus-select-method))
6769             gnus-expert-user
6770             (not gnus-current-startup-file)
6771             (gnus-yes-or-no-p
6772              (format "Quit reading news without saving %s? "
6773                      (file-name-nondirectory gnus-current-startup-file))))
6774     (run-hooks 'gnus-exit-gnus-hook)
6775     (if gnus-use-full-window
6776         (delete-other-windows)
6777       (gnus-remove-some-windows))
6778     (gnus-dribble-save)
6779     (gnus-close-backends)
6780     (gnus-clear-system)
6781     ;; Allow the user to do things after cleaning up.
6782     (run-hooks 'gnus-after-exiting-gnus-hook)))
6783
6784 (defun gnus-offer-save-summaries ()
6785   "Offer to save all active summary buffers."
6786   (save-excursion
6787     (let ((buflist (buffer-list))
6788           buffers bufname)
6789       ;; Go through all buffers and find all summaries.
6790       (while buflist
6791         (and (setq bufname (buffer-name (car buflist)))
6792              (string-match "Summary" bufname)
6793              (save-excursion
6794                (set-buffer bufname)
6795                ;; We check that this is, indeed, a summary buffer.
6796                (and (eq major-mode 'gnus-summary-mode)
6797                     ;; Also make sure this isn't bogus.
6798                     gnus-newsgroup-prepared))
6799              (push bufname buffers))
6800         (setq buflist (cdr buflist)))
6801       ;; Go through all these summary buffers and offer to save them.
6802       (when buffers
6803         (map-y-or-n-p
6804          "Update summary buffer %s? "
6805          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6806          buffers)))))
6807
6808 (defun gnus-group-describe-briefly ()
6809   "Give a one line description of the group mode commands."
6810   (interactive)
6811   (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")))
6812
6813 (defun gnus-group-browse-foreign-server (method)
6814   "Browse a foreign news server.
6815 If called interactively, this function will ask for a select method
6816  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6817 If not, METHOD should be a list where the first element is the method
6818 and the second element is the address."
6819   (interactive
6820    (list (let ((how (completing-read
6821                      "Which backend: "
6822                      (append gnus-valid-select-methods gnus-server-alist)
6823                      nil t (cons "nntp" 0) 'gnus-method-history)))
6824            ;; We either got a backend name or a virtual server name.
6825            ;; If the first, we also need an address.
6826            (if (assoc how gnus-valid-select-methods)
6827                (list (intern how)
6828                      ;; Suggested by mapjph@bath.ac.uk.
6829                      (completing-read
6830                       "Address: "
6831                       (mapcar (lambda (server) (list server))
6832                               gnus-secondary-servers)))
6833              ;; We got a server name, so we find the method.
6834              (gnus-server-to-method how)))))
6835   (gnus-browse-foreign-server method))
6836
6837 \f
6838 ;;;
6839 ;;; Gnus summary mode
6840 ;;;
6841
6842 (defvar gnus-summary-mode-map nil)
6843
6844 (put 'gnus-summary-mode 'mode-class 'special)
6845
6846 (unless gnus-summary-mode-map
6847   (setq gnus-summary-mode-map (make-keymap))
6848   (suppress-keymap gnus-summary-mode-map)
6849
6850   ;; Non-orthogonal keys
6851
6852   (gnus-define-keys gnus-summary-mode-map
6853     " " gnus-summary-next-page
6854     "\177" gnus-summary-prev-page
6855     [delete] gnus-summary-prev-page
6856     "\r" gnus-summary-scroll-up
6857     "n" gnus-summary-next-unread-article
6858     "p" gnus-summary-prev-unread-article
6859     "N" gnus-summary-next-article
6860     "P" gnus-summary-prev-article
6861     "\M-\C-n" gnus-summary-next-same-subject
6862     "\M-\C-p" gnus-summary-prev-same-subject
6863     "\M-n" gnus-summary-next-unread-subject
6864     "\M-p" gnus-summary-prev-unread-subject
6865     "." gnus-summary-first-unread-article
6866     "," gnus-summary-best-unread-article
6867     "\M-s" gnus-summary-search-article-forward
6868     "\M-r" gnus-summary-search-article-backward
6869     "<" gnus-summary-beginning-of-article
6870     ">" gnus-summary-end-of-article
6871     "j" gnus-summary-goto-article
6872     "^" gnus-summary-refer-parent-article
6873     "\M-^" gnus-summary-refer-article
6874     "u" gnus-summary-tick-article-forward
6875     "!" gnus-summary-tick-article-forward
6876     "U" gnus-summary-tick-article-backward
6877     "d" gnus-summary-mark-as-read-forward
6878     "D" gnus-summary-mark-as-read-backward
6879     "E" gnus-summary-mark-as-expirable
6880     "\M-u" gnus-summary-clear-mark-forward
6881     "\M-U" gnus-summary-clear-mark-backward
6882     "k" gnus-summary-kill-same-subject-and-select
6883     "\C-k" gnus-summary-kill-same-subject
6884     "\M-\C-k" gnus-summary-kill-thread
6885     "\M-\C-l" gnus-summary-lower-thread
6886     "e" gnus-summary-edit-article
6887     "#" gnus-summary-mark-as-processable
6888     "\M-#" gnus-summary-unmark-as-processable
6889     "\M-\C-t" gnus-summary-toggle-threads
6890     "\M-\C-s" gnus-summary-show-thread
6891     "\M-\C-h" gnus-summary-hide-thread
6892     "\M-\C-f" gnus-summary-next-thread
6893     "\M-\C-b" gnus-summary-prev-thread
6894     "\M-\C-u" gnus-summary-up-thread
6895     "\M-\C-d" gnus-summary-down-thread
6896     "&" gnus-summary-execute-command
6897     "c" gnus-summary-catchup-and-exit
6898     "\C-w" gnus-summary-mark-region-as-read
6899     "\C-t" gnus-summary-toggle-truncation
6900     "?" gnus-summary-mark-as-dormant
6901     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6902     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6903     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6904     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6905     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6906     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6907     "=" gnus-summary-expand-window
6908     "\C-x\C-s" gnus-summary-reselect-current-group
6909     "\M-g" gnus-summary-rescan-group
6910     "w" gnus-summary-stop-page-breaking
6911     "\C-c\C-r" gnus-summary-caesar-message
6912     "\M-t" gnus-summary-toggle-mime
6913     "f" gnus-summary-followup
6914     "F" gnus-summary-followup-with-original
6915     "C" gnus-summary-cancel-article
6916     "r" gnus-summary-reply
6917     "R" gnus-summary-reply-with-original
6918     "\C-c\C-f" gnus-summary-mail-forward
6919     "o" gnus-summary-save-article
6920     "\C-o" gnus-summary-save-article-mail
6921     "|" gnus-summary-pipe-output
6922     "\M-k" gnus-summary-edit-local-kill
6923     "\M-K" gnus-summary-edit-global-kill
6924     "V" gnus-version
6925     "\C-c\C-d" gnus-summary-describe-group
6926     "q" gnus-summary-exit
6927     "Q" gnus-summary-exit-no-update
6928     "\C-c\C-i" gnus-info-find-node
6929     gnus-mouse-2 gnus-mouse-pick-article
6930     "m" gnus-summary-mail-other-window
6931     "a" gnus-summary-post-news
6932     "x" gnus-summary-limit-to-unread
6933     "s" gnus-summary-isearch-article
6934     "t" gnus-article-hide-headers
6935     "g" gnus-summary-show-article
6936     "l" gnus-summary-goto-last-article
6937     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6938     "\C-d" gnus-summary-enter-digest-group
6939     "\C-c\C-b" gnus-bug
6940     "*" gnus-cache-enter-article
6941     "\M-*" gnus-cache-remove-article
6942     "\M-&" gnus-summary-universal-argument
6943     "\C-l" gnus-recenter
6944     "I" gnus-summary-increase-score
6945     "L" gnus-summary-lower-score
6946
6947     "V" gnus-summary-score-map
6948     "X" gnus-uu-extract-map
6949     "S" gnus-summary-send-map)
6950
6951   ;; Sort of orthogonal keymap
6952   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6953     "t" gnus-summary-tick-article-forward
6954     "!" gnus-summary-tick-article-forward
6955     "d" gnus-summary-mark-as-read-forward
6956     "r" gnus-summary-mark-as-read-forward
6957     "c" gnus-summary-clear-mark-forward
6958     " " gnus-summary-clear-mark-forward
6959     "e" gnus-summary-mark-as-expirable
6960     "x" gnus-summary-mark-as-expirable
6961     "?" gnus-summary-mark-as-dormant
6962     "b" gnus-summary-set-bookmark
6963     "B" gnus-summary-remove-bookmark
6964     "#" gnus-summary-mark-as-processable
6965     "\M-#" gnus-summary-unmark-as-processable
6966     "S" gnus-summary-limit-include-expunged
6967     "C" gnus-summary-catchup
6968     "H" gnus-summary-catchup-to-here
6969     "\C-c" gnus-summary-catchup-all
6970     "k" gnus-summary-kill-same-subject-and-select
6971     "K" gnus-summary-kill-same-subject
6972     "P" gnus-uu-mark-map)
6973
6974   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6975     "c" gnus-summary-clear-above
6976     "u" gnus-summary-tick-above
6977     "m" gnus-summary-mark-above
6978     "k" gnus-summary-kill-below)
6979
6980   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6981     "/" gnus-summary-limit-to-subject
6982     "n" gnus-summary-limit-to-articles
6983     "w" gnus-summary-pop-limit
6984     "s" gnus-summary-limit-to-subject
6985     "a" gnus-summary-limit-to-author
6986     "u" gnus-summary-limit-to-unread
6987     "m" gnus-summary-limit-to-marks
6988     "v" gnus-summary-limit-to-score
6989     "D" gnus-summary-limit-include-dormant
6990     "d" gnus-summary-limit-exclude-dormant
6991     ;;  "t" gnus-summary-limit-exclude-thread
6992     "E" gnus-summary-limit-include-expunged
6993     "c" gnus-summary-limit-exclude-childless-dormant
6994     "C" gnus-summary-limit-mark-excluded-as-read)
6995
6996   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6997     "n" gnus-summary-next-unread-article
6998     "p" gnus-summary-prev-unread-article
6999     "N" gnus-summary-next-article
7000     "P" gnus-summary-prev-article
7001     "\C-n" gnus-summary-next-same-subject
7002     "\C-p" gnus-summary-prev-same-subject
7003     "\M-n" gnus-summary-next-unread-subject
7004     "\M-p" gnus-summary-prev-unread-subject
7005     "f" gnus-summary-first-unread-article
7006     "b" gnus-summary-best-unread-article
7007     "j" gnus-summary-goto-article
7008     "g" gnus-summary-goto-subject
7009     "l" gnus-summary-goto-last-article
7010     "p" gnus-summary-pop-article)
7011
7012   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7013     "k" gnus-summary-kill-thread
7014     "l" gnus-summary-lower-thread
7015     "i" gnus-summary-raise-thread
7016     "T" gnus-summary-toggle-threads
7017     "t" gnus-summary-rethread-current
7018     "^" gnus-summary-reparent-thread
7019     "s" gnus-summary-show-thread
7020     "S" gnus-summary-show-all-threads
7021     "h" gnus-summary-hide-thread
7022     "H" gnus-summary-hide-all-threads
7023     "n" gnus-summary-next-thread
7024     "p" gnus-summary-prev-thread
7025     "u" gnus-summary-up-thread
7026     "o" gnus-summary-top-thread
7027     "d" gnus-summary-down-thread
7028     "#" gnus-uu-mark-thread
7029     "\M-#" gnus-uu-unmark-thread)
7030
7031   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7032     "c" gnus-summary-catchup-and-exit
7033     "C" gnus-summary-catchup-all-and-exit
7034     "E" gnus-summary-exit-no-update
7035     "Q" gnus-summary-exit
7036     "Z" gnus-summary-exit
7037     "n" gnus-summary-catchup-and-goto-next-group
7038     "R" gnus-summary-reselect-current-group
7039     "G" gnus-summary-rescan-group
7040     "N" gnus-summary-next-group
7041     "P" gnus-summary-prev-group)
7042
7043   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7044     " " gnus-summary-next-page
7045     "n" gnus-summary-next-page
7046     "\177" gnus-summary-prev-page
7047     [delete] gnus-summary-prev-page
7048     "p" gnus-summary-prev-page
7049     "\r" gnus-summary-scroll-up
7050     "<" gnus-summary-beginning-of-article
7051     ">" gnus-summary-end-of-article
7052     "b" gnus-summary-beginning-of-article
7053     "e" gnus-summary-end-of-article
7054     "^" gnus-summary-refer-parent-article
7055     "r" gnus-summary-refer-parent-article
7056     "R" gnus-summary-refer-references
7057     "g" gnus-summary-show-article
7058     "s" gnus-summary-isearch-article)
7059
7060   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7061     "b" gnus-article-add-buttons
7062     "B" gnus-article-add-buttons-to-head
7063     "o" gnus-article-treat-overstrike
7064     ;;  "w" gnus-article-word-wrap
7065     "w" gnus-article-fill-cited-article
7066     "c" gnus-article-remove-cr
7067     "L" gnus-article-remove-trailing-blank-lines
7068     "q" gnus-article-de-quoted-unreadable
7069     "f" gnus-article-display-x-face
7070     "l" gnus-summary-stop-page-breaking
7071     "r" gnus-summary-caesar-message
7072     "t" gnus-article-hide-headers
7073     "v" gnus-summary-verbose-headers
7074     "m" gnus-summary-toggle-mime)
7075
7076   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7077     "a" gnus-article-hide
7078     "h" gnus-article-hide-headers
7079     "b" gnus-article-hide-boring-headers
7080     "s" gnus-article-hide-signature
7081     "c" gnus-article-hide-citation
7082     "p" gnus-article-hide-pgp
7083     "\C-c" gnus-article-hide-citation-maybe)
7084
7085   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7086     "a" gnus-article-highlight
7087     "h" gnus-article-highlight-headers
7088     "c" gnus-article-highlight-citation
7089     "s" gnus-article-highlight-signature)
7090
7091   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7092     "z" gnus-article-date-ut
7093     "u" gnus-article-date-ut
7094     "l" gnus-article-date-local
7095     "e" gnus-article-date-lapsed
7096     "o" gnus-article-date-original)
7097
7098   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7099     "v" gnus-version
7100     "f" gnus-summary-fetch-faq
7101     "d" gnus-summary-describe-group
7102     "h" gnus-summary-describe-briefly
7103     "i" gnus-info-find-node)
7104
7105   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7106     "e" gnus-summary-expire-articles
7107     "\M-\C-e" gnus-summary-expire-articles-now
7108     "\177" gnus-summary-delete-article
7109     [delete] gnus-summary-delete-article
7110     "m" gnus-summary-move-article
7111     "r" gnus-summary-respool-article
7112     "w" gnus-summary-edit-article
7113     "c" gnus-summary-copy-article
7114     "B" gnus-summary-crosspost-article
7115     "q" gnus-summary-respool-query
7116     "i" gnus-summary-import-article)
7117
7118   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7119     "o" gnus-summary-save-article
7120     "m" gnus-summary-save-article-mail
7121     "r" gnus-summary-save-article-rmail
7122     "f" gnus-summary-save-article-file
7123     "b" gnus-summary-save-article-body-file
7124     "h" gnus-summary-save-article-folder
7125     "v" gnus-summary-save-article-vm
7126     "p" gnus-summary-pipe-output
7127     "s" gnus-soup-add-article)
7128   )
7129
7130 \f
7131
7132 (defun gnus-summary-mode (&optional group)
7133   "Major mode for reading articles.
7134
7135 All normal editing commands are switched off.
7136 \\<gnus-summary-mode-map>
7137 Each line in this buffer represents one article.  To read an
7138 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7139 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7140 respectively.
7141
7142 You can also post articles and send mail from this buffer.  To
7143 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7144 of an article, type `\\[gnus-summary-reply]'.
7145
7146 There are approx. one gazillion commands you can execute in this
7147 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7148
7149 The following commands are available:
7150
7151 \\{gnus-summary-mode-map}"
7152   (interactive)
7153   (when (and menu-bar-mode
7154              (gnus-visual-p 'summary-menu 'menu))
7155     (gnus-summary-make-menu-bar))
7156   (kill-all-local-variables)
7157   (gnus-summary-make-local-variables)
7158   (gnus-make-thread-indent-array)
7159   (gnus-simplify-mode-line)
7160   (setq major-mode 'gnus-summary-mode)
7161   (setq mode-name "Summary")
7162   (make-local-variable 'minor-mode-alist)
7163   (use-local-map gnus-summary-mode-map)
7164   (buffer-disable-undo (current-buffer))
7165   (setq buffer-read-only t)             ;Disable modification
7166   (setq truncate-lines t)
7167   (setq selective-display t)
7168   (setq selective-display-ellipses t)   ;Display `...'
7169   (setq buffer-display-table gnus-summary-display-table)
7170   (setq gnus-newsgroup-name group)
7171   (make-local-variable 'gnus-summary-line-format)
7172   (make-local-variable 'gnus-summary-line-format-spec)
7173   (make-local-variable 'gnus-summary-mark-positions)
7174   (run-hooks 'gnus-summary-mode-hook))
7175
7176 (defun gnus-summary-make-local-variables ()
7177   "Make all the local summary buffer variables."
7178   (let ((locals gnus-summary-local-variables)
7179         global local)
7180     (while (setq local (pop locals))
7181       (if (consp local)
7182           (progn
7183             (if (eq (cdr local) 'global)
7184                 ;; Copy the global value of the variable.
7185                 (setq global (symbol-value (car local)))
7186               ;; Use the value from the list.
7187               (setq global (eval (cdr local))))
7188             (make-local-variable (car local))
7189             (set (car local) global))
7190         ;; Simple nil-valued local variable.
7191         (make-local-variable local)
7192         (set local nil)))))
7193
7194 (defun gnus-summary-make-display-table ()
7195   ;; Change the display table.  Odd characters have a tendency to mess
7196   ;; up nicely formatted displays - we make all possible glyphs
7197   ;; display only a single character.
7198
7199   ;; We start from the standard display table, if any.
7200   (setq gnus-summary-display-table
7201         (or (copy-sequence standard-display-table)
7202             (make-display-table)))
7203   ;; Nix out all the control chars...
7204   (let ((i 32))
7205     (while (>= (setq i (1- i)) 0)
7206       (aset gnus-summary-display-table i [??])))
7207   ;; ... but not newline and cr, of course. (cr is necessary for the
7208   ;; selective display).
7209   (aset gnus-summary-display-table ?\n nil)
7210   (aset gnus-summary-display-table ?\r nil)
7211   ;; We nix out any glyphs over 126 that are not set already.
7212   (let ((i 256))
7213     (while (>= (setq i (1- i)) 127)
7214       ;; Only modify if the entry is nil.
7215       (or (aref gnus-summary-display-table i)
7216           (aset gnus-summary-display-table i [??])))))
7217
7218 (defun gnus-summary-clear-local-variables ()
7219   (let ((locals gnus-summary-local-variables))
7220     (while locals
7221       (if (consp (car locals))
7222           (and (vectorp (caar locals))
7223                (set (caar locals) nil))
7224         (and (vectorp (car locals))
7225              (set (car locals) nil)))
7226       (setq locals (cdr locals)))))
7227
7228 ;; Summary data functions.
7229
7230 (defmacro gnus-data-number (data)
7231   `(car ,data))
7232
7233 (defmacro gnus-data-set-number (data number)
7234   `(setcar ,data ,number))
7235
7236 (defmacro gnus-data-mark (data)
7237   `(nth 1 ,data))
7238
7239 (defmacro gnus-data-set-mark (data mark)
7240   `(setcar (nthcdr 1 ,data) ,mark))
7241
7242 (defmacro gnus-data-pos (data)
7243   `(nth 2 ,data))
7244
7245 (defmacro gnus-data-set-pos (data pos)
7246   `(setcar (nthcdr 2 ,data) ,pos))
7247
7248 (defmacro gnus-data-header (data)
7249   `(nth 3 ,data))
7250
7251 (defmacro gnus-data-level (data)
7252   `(nth 4 ,data))
7253
7254 (defmacro gnus-data-unread-p (data)
7255   `(= (nth 1 ,data) gnus-unread-mark))
7256
7257 (defmacro gnus-data-pseudo-p (data)
7258   `(consp (nth 3 ,data)))
7259
7260 (defmacro gnus-data-find (number)
7261   `(assq ,number gnus-newsgroup-data))
7262
7263 (defmacro gnus-data-find-list (number &optional data)
7264   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7265      (memq (assq ,number bdata)
7266            bdata)))
7267
7268 (defmacro gnus-data-make (number mark pos header level)
7269   `(list ,number ,mark ,pos ,header ,level))
7270
7271 (defun gnus-data-enter (after-article number mark pos header level offset)
7272   (let ((data (gnus-data-find-list after-article)))
7273     (or data (error "No such article: %d" after-article))
7274     (setcdr data (cons (gnus-data-make number mark pos header level)
7275                        (cdr data)))
7276     (setq gnus-newsgroup-data-reverse nil)
7277     (gnus-data-update-list (cddr data) offset)))
7278
7279 (defun gnus-data-enter-list (after-article list &optional offset)
7280   (when list
7281     (let ((data (and after-article (gnus-data-find-list after-article)))
7282           (ilist list))
7283       (or data (not after-article) (error "No such article: %d" after-article))
7284       ;; Find the last element in the list to be spliced into the main
7285       ;; list.
7286       (while (cdr list)
7287         (setq list (cdr list)))
7288       (if (not data)
7289           (progn
7290             (setcdr list gnus-newsgroup-data)
7291             (setq gnus-newsgroup-data ilist)
7292             (and offset (gnus-data-update-list (cdr list) offset)))
7293         (setcdr list (cdr data))
7294         (setcdr data ilist)
7295         (and offset (gnus-data-update-list (cdr data) offset)))
7296       (setq gnus-newsgroup-data-reverse nil))))
7297
7298 (defun gnus-data-remove (article &optional offset)
7299   (let ((data gnus-newsgroup-data))
7300     (if (= (gnus-data-number (car data)) article)
7301         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7302               gnus-newsgroup-data-reverse nil)
7303       (while (cdr data)
7304         (and (= (gnus-data-number (cadr data)) article)
7305              (progn
7306                (setcdr data (cddr data))
7307                (and offset (gnus-data-update-list (cdr data) offset))
7308                (setq data nil
7309                      gnus-newsgroup-data-reverse nil)))
7310         (setq data (cdr data))))))
7311
7312 (defmacro gnus-data-list (backward)
7313   `(if ,backward
7314        (or gnus-newsgroup-data-reverse
7315            (setq gnus-newsgroup-data-reverse
7316                  (reverse gnus-newsgroup-data)))
7317      gnus-newsgroup-data))
7318
7319 (defun gnus-data-update-list (data offset)
7320   "Add OFFSET to the POS of all data entries in DATA."
7321   (while data
7322     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7323     (setq data (cdr data))))
7324
7325 (defun gnus-data-compute-positions ()
7326   "Compute the positions of all articles."
7327   (let ((data gnus-newsgroup-data)
7328         pos)
7329     (while data
7330       (when (setq pos (text-property-any
7331                        (point-min) (point-max)
7332                        'gnus-number (gnus-data-number (car data))))
7333         (gnus-data-set-pos (car data) (+ pos 3)))
7334       (setq data (cdr data)))))
7335
7336 (defun gnus-summary-article-pseudo-p (article)
7337   "Say whether this article is a pseudo article or not."
7338   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7339
7340 (defun gnus-article-parent-p (number)
7341   "Say whether this article is a parent or not."
7342   (let ((data (gnus-data-find-list number)))
7343     (and (cdr data)                     ; There has to be an article after...
7344          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7345             (gnus-data-level (nth 1 data))))))
7346
7347 (defun gnus-article-children (number)
7348   "Return a list of all children to NUMBER."
7349   (let* ((data (gnus-data-find-list number))
7350          (level (gnus-data-level (car data)))
7351          children)
7352     (setq data (cdr data))
7353     (while (and data            
7354                 (= (gnus-data-level (car data)) (1+ level)))
7355       (push (gnus-data-number (car data)) children)
7356       (setq data (cdr data)))
7357     children))
7358
7359 (defmacro gnus-summary-skip-intangible ()
7360   "If the current article is intangible, then jump to a different article."
7361   '(let ((to (get-text-property (point) 'gnus-intangible)))
7362     (and to (gnus-summary-goto-subject to))))
7363
7364 (defmacro gnus-summary-article-intangible-p ()
7365   "Say whether this article is intangible or not."
7366   '(get-text-property (point) 'gnus-intangible))
7367
7368 ;; Some summary mode macros.
7369
7370 (defmacro gnus-summary-article-number ()
7371   "The article number of the article on the current line.
7372 If there isn's an article number here, then we return the current
7373 article number."
7374   '(progn
7375      (gnus-summary-skip-intangible)
7376      (or (get-text-property (point) 'gnus-number)
7377          (gnus-summary-last-subject))))
7378
7379 (defmacro gnus-summary-article-header (&optional number)
7380   `(gnus-data-header (gnus-data-find
7381                       ,(or number '(gnus-summary-article-number)))))
7382
7383 (defmacro gnus-summary-thread-level (&optional number)
7384   `(if (and (eq gnus-summary-make-false-root 'dummy)
7385             (get-text-property (point) 'gnus-intangible))
7386        0
7387      (gnus-data-level (gnus-data-find
7388                        ,(or number '(gnus-summary-article-number))))))
7389
7390 (defmacro gnus-summary-article-mark (&optional number)
7391   `(gnus-data-mark (gnus-data-find
7392                     ,(or number '(gnus-summary-article-number)))))
7393
7394 (defmacro gnus-summary-article-pos (&optional number)
7395   `(gnus-data-pos (gnus-data-find
7396                    ,(or number '(gnus-summary-article-number)))))
7397
7398 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7399 (defmacro gnus-summary-article-subject (&optional number)
7400   "Return current subject string or nil if nothing."
7401   `(let ((headers
7402           ,(if number
7403                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7404              '(gnus-data-header (assq (gnus-summary-article-number)
7405                                       gnus-newsgroup-data)))))
7406      (and headers
7407           (vectorp headers)
7408           (mail-header-subject headers))))
7409
7410 (defmacro gnus-summary-article-score (&optional number)
7411   "Return current article score."
7412   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7413                   gnus-newsgroup-scored))
7414        gnus-summary-default-score 0))
7415
7416 (defun gnus-summary-article-children (&optional number)
7417   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7418          (level (gnus-data-level (car data)))
7419          l children)
7420     (while (and (setq data (cdr data))
7421                 (> (setq l (gnus-data-level (car data))) level))
7422       (and (= (1+ level) l)
7423            (setq children (cons (gnus-data-number (car data))
7424                                 children))))
7425     (nreverse children)))
7426
7427 (defun gnus-summary-article-parent (&optional number)
7428   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7429                                     (gnus-data-list t)))
7430          (level (gnus-data-level (car data))))
7431     (if (zerop level)
7432         () ; This is a root.
7433       ;; We search until we find an article with a level less than
7434       ;; this one.  That function has to be the parent.
7435       (while (and (setq data (cdr data))
7436                   (not (< (gnus-data-level (car data)) level))))
7437       (and data (gnus-data-number (car data))))))
7438
7439 (defun gnus-unread-mark-p (mark)
7440   "Say whether MARK is the unread mark."
7441   (= mark gnus-unread-mark))
7442
7443 (defun gnus-read-mark-p (mark)
7444   "Say whether MARK is one of the marks that mark as read.
7445 This is all marks except unread, ticked, dormant, and expirable."
7446   (not (or (= mark gnus-unread-mark)
7447            (= mark gnus-ticked-mark)
7448            (= mark gnus-dormant-mark)
7449            (= mark gnus-expirable-mark))))
7450
7451 ;; Saving hidden threads.
7452
7453 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7454 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7455 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7456
7457 (defmacro gnus-save-hidden-threads (&rest forms)
7458   "Save hidden threads, eval FORMS, and restore the hidden threads."
7459   (let ((config (make-symbol "config")))
7460     `(let ((,config (gnus-hidden-threads-configuration)))
7461        (unwind-protect
7462            (progn
7463              ,@forms)
7464          (gnus-restore-hidden-threads-configuration ,config)))))
7465
7466 (defun gnus-hidden-threads-configuration ()
7467   "Return the current hidden threads configuration."
7468   (save-excursion
7469     (let (config)
7470       (goto-char (point-min))
7471       (while (search-forward "\r" nil t)
7472         (push (1- (point)) config))
7473       config)))
7474
7475 (defun gnus-restore-hidden-threads-configuration (config)
7476   "Restore hidden threads configuration from CONFIG."
7477   (let (point buffer-read-only)
7478     (while (setq point (pop config))
7479       (when (and (< point (point-max))
7480                  (goto-char point)
7481                  (= (following-char) ?\n))
7482         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7483
7484 ;; Various summary mode internalish functions.
7485
7486 (defun gnus-mouse-pick-article (e)
7487   (interactive "e")
7488   (mouse-set-point e)
7489   (gnus-summary-next-page nil t))
7490
7491 (defun gnus-summary-setup-buffer (group)
7492   "Initialize summary buffer."
7493   (let ((buffer (concat "*Summary " group "*")))
7494     (if (get-buffer buffer)
7495         (progn
7496           (set-buffer buffer)
7497           (setq gnus-summary-buffer (current-buffer))
7498           (not gnus-newsgroup-prepared))
7499       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7500       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7501       (gnus-add-current-to-buffer-list)
7502       (gnus-summary-mode group)
7503       (when gnus-carpal
7504         (gnus-carpal-setup-buffer 'summary))
7505       (unless gnus-single-article-buffer
7506         (make-local-variable 'gnus-article-buffer)
7507         (make-local-variable 'gnus-article-current)
7508         (make-local-variable 'gnus-original-article-buffer))
7509       (setq gnus-newsgroup-name group)
7510       t)))
7511
7512 (defun gnus-set-global-variables ()
7513   ;; Set the global equivalents of the summary buffer-local variables
7514   ;; to the latest values they had.  These reflect the summary buffer
7515   ;; that was in action when the last article was fetched.
7516   (when (eq major-mode 'gnus-summary-mode)
7517     (setq gnus-summary-buffer (current-buffer))
7518     (let ((name gnus-newsgroup-name)
7519           (marked gnus-newsgroup-marked)
7520           (unread gnus-newsgroup-unreads)
7521           (headers gnus-current-headers)
7522           (data gnus-newsgroup-data)
7523           (summary gnus-summary-buffer)
7524           (article-buffer gnus-article-buffer)
7525           (original gnus-original-article-buffer)
7526           (gac gnus-article-current)
7527           (score-file gnus-current-score-file))
7528       (save-excursion
7529         (set-buffer gnus-group-buffer)
7530         (setq gnus-newsgroup-name name)
7531         (setq gnus-newsgroup-marked marked)
7532         (setq gnus-newsgroup-unreads unread)
7533         (setq gnus-current-headers headers)
7534         (setq gnus-newsgroup-data data)
7535         (setq gnus-article-current gac)
7536         (setq gnus-summary-buffer summary)
7537         (setq gnus-article-buffer article-buffer)
7538         (setq gnus-original-article-buffer original)
7539         (setq gnus-current-score-file score-file)))))
7540
7541 (defun gnus-summary-last-article-p (&optional article)
7542   "Return whether ARTICLE is the last article in the buffer."
7543   (if (not (setq article (or article (gnus-summary-article-number))))
7544       t ; All non-existant numbers are the last article. :-)
7545     (not (cdr (gnus-data-find-list article)))))
7546
7547 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7548   "Insert a dummy root in the summary buffer."
7549   (beginning-of-line)
7550   (gnus-add-text-properties
7551    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7552    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7553
7554 (defun gnus-make-thread-indent-array ()
7555   (let ((n 200))
7556     (unless (and gnus-thread-indent-array
7557                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7558       (setq gnus-thread-indent-array (make-vector 201 "")
7559             gnus-thread-indent-array-level gnus-thread-indent-level)
7560       (while (>= n 0)
7561         (aset gnus-thread-indent-array n
7562               (make-string (* n gnus-thread-indent-level) ? ))
7563         (setq n (1- n))))))
7564
7565 (defun gnus-summary-insert-line
7566   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7567                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7568                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7569   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7570          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7571          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7572          (gnus-tmp-score-char
7573           (if (or (null gnus-summary-default-score)
7574                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7575                       gnus-summary-zcore-fuzz)) ? 
7576             (if (< gnus-tmp-score gnus-summary-default-score)
7577                 gnus-score-below-mark gnus-score-over-mark)))
7578          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7579                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7580                                   gnus-cached-mark)
7581                                  (gnus-tmp-replied gnus-replied-mark)
7582                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7583                                   gnus-saved-mark)
7584                                  (t gnus-unread-mark)))
7585          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7586          (gnus-tmp-name
7587           (cond
7588            ((string-match "(.+)" gnus-tmp-from)
7589             (substring gnus-tmp-from
7590                        (1+ (match-beginning 0)) (1- (match-end 0))))
7591            ((string-match "<[^>]+> *$" gnus-tmp-from)
7592             (let ((beg (match-beginning 0)))
7593               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7594                        (substring gnus-tmp-from (1+ (match-beginning 0))
7595                                   (1- (match-end 0))))
7596                   (substring gnus-tmp-from 0 beg))))
7597            (t gnus-tmp-from)))
7598          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7599          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7600          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7601          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7602          (buffer-read-only nil))
7603     (when (string= gnus-tmp-name "")
7604       (setq gnus-tmp-name gnus-tmp-from))
7605     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7606     (gnus-put-text-property
7607      (point)
7608      (progn (eval gnus-summary-line-format-spec) (point))
7609      'gnus-number gnus-tmp-number)
7610     (when (gnus-visual-p 'summary-highlight 'highlight)
7611       (forward-line -1)
7612       (run-hooks 'gnus-summary-update-hook)
7613       (forward-line 1))))
7614
7615 (defun gnus-summary-update-line (&optional dont-update)
7616   ;; Update summary line after change.
7617   (when (and gnus-summary-default-score
7618              (not gnus-summary-inhibit-highlight))
7619     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7620            (article (gnus-summary-article-number))
7621            (score (gnus-summary-article-score article)))
7622       (unless dont-update
7623         (if (and gnus-summary-mark-below
7624                  (< (gnus-summary-article-score)
7625                     gnus-summary-mark-below))
7626             ;; This article has a low score, so we mark it as read.
7627             (when (memq article gnus-newsgroup-unreads)
7628               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7629           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7630             ;; This article was previously marked as read on account
7631             ;; of a low score, but now it has risen, so we mark it as
7632             ;; unread.
7633             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7634         (gnus-summary-update-mark
7635          (if (or (null gnus-summary-default-score)
7636                  (<= (abs (- score gnus-summary-default-score))
7637                      gnus-summary-zcore-fuzz)) ? 
7638            (if (< score gnus-summary-default-score)
7639                gnus-score-below-mark gnus-score-over-mark)) 'score))
7640       ;; Do visual highlighting.
7641       (when (gnus-visual-p 'summary-highlight 'highlight)
7642         (run-hooks 'gnus-summary-update-hook)))))
7643
7644 (defvar gnus-tmp-new-adopts nil)
7645
7646 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7647   ;; Sum up all elements (and sub-elements) in a list.
7648   (let* ((number
7649           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7650           (cond
7651            ((and (consp thread) (cdr thread))
7652             (apply
7653              '+ 1 (mapcar
7654                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7655            ((null thread)
7656             1)
7657            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7658             1)
7659            (t 0))))
7660     (when (and level (zerop level) gnus-tmp-new-adopts)
7661       (incf number
7662             (apply '+ (mapcar
7663                        'gnus-summary-number-of-articles-in-thread
7664                        gnus-tmp-new-adopts))))
7665     (if char
7666         (if (> number 1) gnus-not-empty-thread-mark
7667           gnus-empty-thread-mark)
7668       number)))
7669
7670 (defun gnus-summary-set-local-parameters (group)
7671  "Go through the local params of GROUP and set all variable specs in that list."
7672   (let ((params (gnus-info-params (gnus-get-info group)))
7673         elem)
7674     (while params
7675       (setq elem (car params)
7676             params (cdr params))
7677       (and (consp elem)                 ; Has to be a cons.
7678            (consp (cdr elem))           ; The cdr has to be a list.
7679            (symbolp (car elem))         ; Has to be a symbol in there.
7680            (not (memq (car elem) 
7681                       '(quit-config to-address to-list to-group)))
7682            (progn                       ; So we set it.
7683              (make-local-variable (car elem))
7684              (set (car elem) (eval (nth 1 elem))))))))
7685
7686 (defun gnus-summary-read-group (group &optional show-all no-article
7687                                       kill-buffer no-display)
7688   "Start reading news in newsgroup GROUP.
7689 If SHOW-ALL is non-nil, already read articles are also listed.
7690 If NO-ARTICLE is non-nil, no article is selected initially.
7691 If NO-DISPLAY, don't generate a summary buffer."
7692   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7693   (let* ((new-group (gnus-summary-setup-buffer group))
7694          (quit-config (gnus-group-quit-config group))
7695          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7696     (cond
7697      ;; This summary buffer exists already, so we just select it.
7698      ((not new-group)
7699       (gnus-set-global-variables)
7700       (when kill-buffer
7701         (gnus-kill-or-deaden-summary kill-buffer))
7702       (gnus-configure-windows 'summary 'force)
7703       (gnus-set-mode-line 'summary)
7704       (gnus-summary-position-point)
7705       (message "")
7706       t)
7707      ;; We couldn't select this group.
7708      ((null did-select)
7709       (when (and (eq major-mode 'gnus-summary-mode)
7710                  (not (equal (current-buffer) kill-buffer)))
7711         (kill-buffer (current-buffer))
7712         (if (not quit-config)
7713             (progn
7714               (set-buffer gnus-group-buffer)
7715               (gnus-group-jump-to-group group)
7716               (gnus-group-next-unread-group 1))
7717           (if (not (buffer-name (car quit-config)))
7718               (gnus-configure-windows 'group 'force)
7719             (set-buffer (car quit-config))
7720             (and (eq major-mode 'gnus-summary-mode)
7721                  (gnus-set-global-variables))
7722             (gnus-configure-windows (cdr quit-config)))))
7723       (gnus-message 3 "Can't select group")
7724       nil)
7725      ;; The user did a `C-g' while prompting for number of articles,
7726      ;; so we exit this group.
7727      ((eq did-select 'quit)
7728       (and (eq major-mode 'gnus-summary-mode)
7729            (not (equal (current-buffer) kill-buffer))
7730            (kill-buffer (current-buffer)))
7731       (when kill-buffer
7732         (gnus-kill-or-deaden-summary kill-buffer))
7733       (if (not quit-config)
7734           (progn
7735             (set-buffer gnus-group-buffer)
7736             (gnus-group-jump-to-group group)
7737             (gnus-group-next-unread-group 1)
7738             (gnus-configure-windows 'group 'force))
7739         (if (not (buffer-name (car quit-config)))
7740             (gnus-configure-windows 'group 'force)
7741           (set-buffer (car quit-config))
7742           (and (eq major-mode 'gnus-summary-mode)
7743                (gnus-set-global-variables))
7744           (gnus-configure-windows (cdr quit-config))))
7745       ;; Finally signal the quit.
7746       (signal 'quit nil))
7747      ;; The group was successfully selected.
7748      (t
7749       (gnus-set-global-variables)
7750       ;; Save the active value in effect when the group was entered.
7751       (setq gnus-newsgroup-active
7752             (gnus-copy-sequence
7753              (gnus-active gnus-newsgroup-name)))
7754       ;; You can change the summary buffer in some way with this hook.
7755       (run-hooks 'gnus-select-group-hook)
7756       ;; Set any local variables in the group parameters.
7757       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7758       (gnus-update-format-specifications)
7759       ;; Do score processing.
7760       (when gnus-use-scoring
7761         (gnus-possibly-score-headers))
7762       ;; Check whether to fill in the gaps in the threads.
7763       (when gnus-build-sparse-threads
7764         (gnus-build-sparse-threads))
7765       ;; Find the initial limit.
7766       (if gnus-show-threads
7767           (if show-all
7768               (let ((gnus-newsgroup-dormant nil))
7769                 (gnus-summary-initial-limit show-all))
7770             (gnus-summary-initial-limit show-all))
7771         (setq gnus-newsgroup-limit 
7772               (mapcar 
7773                (lambda (header) (mail-header-number header))
7774                gnus-newsgroup-headers)))
7775       ;; Generate the summary buffer.
7776       (unless no-display
7777         (gnus-summary-prepare))
7778       (when gnus-use-trees
7779         (gnus-tree-open group)
7780         (setq gnus-summary-highlight-line-function
7781               'gnus-tree-highlight-article))
7782       ;; If the summary buffer is empty, but there are some low-scored
7783       ;; articles or some excluded dormants, we include these in the
7784       ;; buffer.
7785       (when (and (zerop (buffer-size))
7786                  (not no-display))
7787         (cond (gnus-newsgroup-dormant
7788                (gnus-summary-limit-include-dormant))
7789               ((and gnus-newsgroup-scored show-all)
7790                (gnus-summary-limit-include-expunged))))
7791       ;; Function `gnus-apply-kill-file' must be called in this hook.
7792       (run-hooks 'gnus-apply-kill-hook)
7793       (if (and (zerop (buffer-size))
7794                (not no-display))
7795           (progn
7796             ;; This newsgroup is empty.
7797             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7798             (gnus-message 6 "No unread news")
7799             (when kill-buffer
7800               (gnus-kill-or-deaden-summary kill-buffer))
7801             ;; Return nil from this function.
7802             nil)
7803         ;; Hide conversation thread subtrees.  We cannot do this in
7804         ;; gnus-summary-prepare-hook since kill processing may not
7805         ;; work with hidden articles.
7806         (and gnus-show-threads
7807              gnus-thread-hide-subtree
7808              (gnus-summary-hide-all-threads))
7809         ;; Show first unread article if requested.
7810         (if (and (not no-article)
7811                  (not no-display)
7812                  gnus-newsgroup-unreads
7813                  gnus-auto-select-first)
7814             (unless (if (eq gnus-auto-select-first 'best)
7815                         (gnus-summary-best-unread-article)
7816                       (gnus-summary-first-unread-article))
7817               (gnus-configure-windows 'summary))
7818           ;; Don't select any articles, just move point to the first
7819           ;; article in the group.
7820           (goto-char (point-min))
7821           (gnus-summary-position-point)
7822           (gnus-set-mode-line 'summary)
7823           (gnus-configure-windows 'summary 'force))
7824         ;; If we are in async mode, we send some info to the backend.
7825         (when gnus-newsgroup-async
7826           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7827         (when kill-buffer
7828           (gnus-kill-or-deaden-summary kill-buffer))
7829         (when (get-buffer-window gnus-group-buffer t)
7830           ;; Gotta use windows, because recenter does wierd stuff if
7831           ;; the current buffer ain't the displayed window.
7832           (let ((owin (selected-window)))
7833             (select-window (get-buffer-window gnus-group-buffer t))
7834             (when (gnus-group-goto-group group)
7835               (recenter))
7836             (select-window owin))))
7837       ;; Mark this buffer as "prepared".
7838       (setq gnus-newsgroup-prepared t)
7839       t))))
7840
7841 (defun gnus-summary-prepare ()
7842   "Generate the summary buffer."
7843   (let ((buffer-read-only nil))
7844     (erase-buffer)
7845     (setq gnus-newsgroup-data nil
7846           gnus-newsgroup-data-reverse nil)
7847     (run-hooks 'gnus-summary-generate-hook)
7848     ;; Generate the buffer, either with threads or without.
7849     (when gnus-newsgroup-headers
7850       (gnus-summary-prepare-threads
7851        (if gnus-show-threads
7852            (gnus-sort-gathered-threads
7853             (funcall gnus-summary-thread-gathering-function
7854                      (gnus-sort-threads
7855                       (gnus-cut-threads (gnus-make-threads)))))
7856          ;; Unthreaded display.
7857          (gnus-sort-articles gnus-newsgroup-headers))))
7858     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7859     ;; Call hooks for modifying summary buffer.
7860     (goto-char (point-min))
7861     (run-hooks 'gnus-summary-prepare-hook)))
7862
7863 (defun gnus-gather-threads-by-subject (threads)
7864   "Gather threads by looking at Subject headers."
7865   (if (not gnus-summary-make-false-root)
7866       threads
7867     (let ((hashtb (gnus-make-hashtable 1023))
7868           (prev threads)
7869           (result threads)
7870           subject hthread whole-subject)
7871       (while threads
7872         (setq whole-subject (mail-header-subject (caar threads)))
7873         (setq subject
7874               (cond
7875                ;; Truncate the subject.
7876                ((numberp gnus-summary-gather-subject-limit)
7877                 (setq subject (gnus-simplify-subject-re whole-subject))
7878                 (if (> (length subject) gnus-summary-gather-subject-limit)
7879                     (substring subject 0 gnus-summary-gather-subject-limit)
7880                   subject))
7881                ;; Fuzzily simplify it.
7882                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7883                 (gnus-simplify-subject-fuzzy whole-subject))
7884                ;; Just remove the leading "Re:".
7885                (t
7886                 (gnus-simplify-subject-re whole-subject))))
7887
7888         (if (and gnus-summary-gather-exclude-subject
7889                  (string-match gnus-summary-gather-exclude-subject
7890                                subject))
7891             ()          ; We don't want to do anything with this article.
7892           ;; We simplify the subject before looking it up in the
7893           ;; hash table.
7894
7895           (if (setq hthread (gnus-gethash subject hashtb))
7896               (progn
7897                 ;; We enter a dummy root into the thread, if we
7898                 ;; haven't done that already.
7899                 (unless (stringp (caar hthread))
7900                   (setcar hthread (list whole-subject (car hthread))))
7901                 ;; We add this new gathered thread to this gathered
7902                 ;; thread.
7903                 (setcdr (car hthread)
7904                         (nconc (cdar hthread) (list (car threads))))
7905                 ;; Remove it from the list of threads.
7906                 (setcdr prev (cdr threads))
7907                 (setq threads prev))
7908             ;; Enter this thread into the hash table.
7909             (gnus-sethash subject threads hashtb)))
7910         (setq prev threads)
7911         (setq threads (cdr threads)))
7912       result)))
7913
7914 (defun gnus-gather-threads-by-references (threads)
7915   "Gather threads by looking at References headers."
7916   (let ((idhashtb (gnus-make-hashtable 1023))
7917         (thhashtb (gnus-make-hashtable 1023))
7918         (prev threads)
7919         (result threads)
7920         ids references id gthread gid entered)
7921     (while threads
7922       (when (setq references (mail-header-references (caar threads)))
7923         (setq id (mail-header-id (caar threads)))
7924         (setq ids (gnus-split-references references))
7925         (setq entered nil)
7926         (while ids
7927           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7928               (progn
7929                 (gnus-sethash (car ids) id idhashtb)
7930                 (gnus-sethash id threads thhashtb))
7931             (setq gthread (gnus-gethash gid thhashtb))
7932             (unless entered
7933               ;; We enter a dummy root into the thread, if we
7934               ;; haven't done that already.
7935               (unless (stringp (caar gthread))
7936                 (setcar gthread (list (mail-header-subject (caar gthread))
7937                                       (car gthread))))
7938               ;; We add this new gathered thread to this gathered
7939               ;; thread.
7940               (setcdr (car gthread)
7941                       (nconc (cdar gthread) (list (car threads)))))
7942             ;; Add it into the thread hash table.
7943             (gnus-sethash id gthread thhashtb)
7944             (setq entered t)
7945             ;; Remove it from the list of threads.
7946             (setcdr prev (cdr threads))
7947             (setq threads prev))
7948           (setq ids (cdr ids))))
7949       (setq prev threads)
7950       (setq threads (cdr threads)))
7951     result))
7952
7953 (defun gnus-sort-gathered-threads (threads)
7954   "Sort subtreads inside each gathered thread by article number."
7955   (let ((result threads))
7956     (while threads
7957       (when (stringp (caar threads))
7958         (setcdr (car threads)
7959                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7960       (setq threads (cdr threads)))
7961     result))
7962
7963 (defun gnus-make-threads ()
7964   "Go through the dependency hashtb and find the roots.  Return all threads."
7965   (let (threads)
7966     (mapatoms
7967      (lambda (refs)
7968        (unless (car (symbol-value refs))
7969          ;; These threads do not refer back to any other articles,
7970          ;; so they're roots.
7971          (setq threads (append (cdr (symbol-value refs)) threads))))
7972      gnus-newsgroup-dependencies)
7973     threads))
7974
7975 (defun gnus-build-sparse-threads ()
7976   (let ((headers gnus-newsgroup-headers)
7977         (deps gnus-newsgroup-dependencies)
7978         header references generation relations 
7979         cthread subject child end pthread relation)
7980     ;; First we create an alist of generations/relations, where 
7981     ;; generations is how much we trust the ralation, and the relation
7982     ;; is parent/child.
7983     (gnus-message 7 "Making sparse threads...")
7984     (save-excursion
7985       (nnheader-set-temp-buffer " *gnus sparse threads*")
7986       (while (setq header (pop headers))
7987         (when (and (setq references (mail-header-references header))
7988                    (not (string= references "")))
7989           (insert references)
7990           (setq child (mail-header-id header)
7991                 subject (mail-header-subject header))
7992           (setq generation 0)
7993           (while (search-backward ">" nil t)
7994             (setq end (1+ (point)))
7995             (when (search-backward "<" nil t)
7996               (push (list (incf generation) 
7997                           child (setq child (buffer-substring (point) end))
7998                           subject)
7999                     relations)))
8000           (push (list (1+ generation) child nil subject) relations)
8001           (erase-buffer)))
8002       (kill-buffer (current-buffer)))
8003     ;; Sort over trustworthiness.
8004     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8005     (while (setq relation (pop relations))
8006       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8007                 (unless (car (symbol-value cthread))
8008                   ;; Make this article the parent of these threads.
8009                   (setcar (symbol-value cthread)
8010                           (vector gnus-reffed-article-number 
8011                                   (cadddr relation) 
8012                                   "" ""
8013                                   (cadr relation) 
8014                                   (or (caddr relation) "") 0 0 "")))
8015               (set cthread (list (vector gnus-reffed-article-number
8016                                          (cadddr relation) 
8017                                          "" "" (cadr relation) 
8018                                          (or (caddr relation) "") 0 0 ""))))
8019         (push gnus-reffed-article-number gnus-newsgroup-limit)
8020         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8021         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8022               gnus-newsgroup-reads)
8023         (decf gnus-reffed-article-number)
8024         ;; Make this new thread the child of its parent.
8025         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8026             (setcdr (symbol-value pthread)
8027                     (nconc (cdr (symbol-value pthread))
8028                            (list (symbol-value cthread))))
8029           (set pthread (list nil (symbol-value cthread))))))
8030     (gnus-message 7 "Making sparse threads...done")))
8031
8032 (defun gnus-build-old-threads ()
8033   ;; Look at all the articles that refer back to old articles, and
8034   ;; fetch the headers for the articles that aren't there.  This will
8035   ;; build complete threads - if the roots haven't been expired by the
8036   ;; server, that is.
8037   (let (id heads)
8038     (mapatoms
8039      (lambda (refs)
8040        (when (not (car (symbol-value refs)))
8041          (setq heads (cdr (symbol-value refs)))
8042          (while heads
8043            (if (memq (mail-header-number (caar heads))
8044                      gnus-newsgroup-dormant)
8045                (setq heads (cdr heads))
8046              (setq id (symbol-name refs))
8047              (while (and (setq id (gnus-build-get-header id))
8048                          (not (car (gnus-gethash
8049                                     id gnus-newsgroup-dependencies)))))
8050              (setq heads nil)))))
8051      gnus-newsgroup-dependencies)))
8052
8053 (defun gnus-build-get-header (id)
8054   ;; Look through the buffer of NOV lines and find the header to
8055   ;; ID.  Enter this line into the dependencies hash table, and return
8056   ;; the id of the parent article (if any).
8057   (let ((deps gnus-newsgroup-dependencies)
8058         found header)
8059     (prog1
8060         (save-excursion
8061           (set-buffer nntp-server-buffer)
8062           (goto-char (point-min))
8063           (while (and (not found) (search-forward id nil t))
8064             (beginning-of-line)
8065             (setq found (looking-at
8066                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8067                                  (regexp-quote id))))
8068             (or found (beginning-of-line 2)))
8069           (when found
8070             (beginning-of-line)
8071             (and
8072              (setq header (gnus-nov-parse-line
8073                            (read (current-buffer)) deps))
8074              (gnus-parent-id (mail-header-references header)))))
8075       (when header
8076         (let ((number (mail-header-number header)))
8077           (push number gnus-newsgroup-limit)
8078           (push header gnus-newsgroup-headers)
8079           (if (memq number gnus-newsgroup-unselected)
8080               (progn
8081                 (push number gnus-newsgroup-unreads)
8082                 (setq gnus-newsgroup-unselected
8083                       (delq number gnus-newsgroup-unselected)))
8084             (push number gnus-newsgroup-ancient)))))))
8085
8086 (defun gnus-summary-update-article (article &optional iheader)
8087   "Update ARTICLE in the summary buffer."
8088   (set-buffer gnus-summary-buffer)
8089   (let* ((header (or iheader (gnus-summary-article-header article)))
8090          (id (mail-header-id header))
8091          (data (gnus-data-find article))
8092          (thread (gnus-id-to-thread id))
8093          (references (mail-header-references header))
8094          (parent
8095           (gnus-id-to-thread
8096            (or (gnus-parent-id 
8097                 (if (and references
8098                          (not (equal "" references)))
8099                     references))
8100                "none")))
8101          (buffer-read-only nil)
8102          (old (car thread))
8103          (number (mail-header-number header))
8104          pos)
8105     (when thread
8106       ;; !!! Should this be in or not?
8107       (unless iheader
8108         (setcar thread nil))
8109       (when parent
8110         (delq thread parent))
8111       (if (gnus-summary-insert-subject id header iheader)
8112           ;; Set the (possibly) new article number in the data structure.
8113           (gnus-data-set-number data (gnus-id-to-article id))
8114         (setcar thread old)
8115         nil))))
8116
8117 (defun gnus-rebuild-thread (id)
8118   "Rebuild the thread containing ID."
8119   (let ((buffer-read-only nil)
8120         current thread data)
8121     (if (not gnus-show-threads)
8122         (setq thread (list (car (gnus-id-to-thread id))))
8123       ;; Get the thread this article is part of.
8124       (setq thread (gnus-remove-thread id)))
8125     (setq current (save-excursion
8126                     (and (zerop (forward-line -1))
8127                          (gnus-summary-article-number))))
8128     ;; If this is a gathered thread, we have to go some re-gathering.
8129     (when (stringp (car thread))
8130       (let ((subject (car thread))
8131             roots thr)
8132         (setq thread (cdr thread))
8133         (while thread
8134           (unless (memq (setq thr (gnus-id-to-thread
8135                                       (gnus-root-id
8136                                        (mail-header-id (caar thread)))))
8137                         roots)
8138             (push thr roots))
8139           (setq thread (cdr thread)))
8140         ;; We now have all (unique) roots.
8141         (if (= (length roots) 1)
8142             ;; All the loose roots are now one solid root.
8143             (setq thread (car roots))
8144           (setq thread (cons subject (gnus-sort-threads roots))))))
8145     (let (threads)
8146       ;; We then insert this thread into the summary buffer.
8147       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8148         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8149         (setq data (nreverse gnus-newsgroup-data))
8150         (setq threads gnus-newsgroup-threads))
8151       ;; We splice the new data into the data structure.
8152       (gnus-data-enter-list current data)
8153       (gnus-data-compute-positions)
8154       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8155
8156 (defun gnus-number-to-header (number)
8157   "Return the header for article NUMBER."
8158   (let ((headers gnus-newsgroup-headers))
8159     (while (and headers
8160                 (not (= number (mail-header-number (car headers)))))
8161       (pop headers))
8162     (when headers
8163       (car headers))))
8164
8165 (defun gnus-id-to-thread (id)
8166   "Return the (sub-)thread where ID appears."
8167   (gnus-gethash id gnus-newsgroup-dependencies))
8168
8169 (defun gnus-id-to-article (id)
8170   "Return the article number of ID."
8171   (let ((thread (gnus-id-to-thread id)))
8172     (when (and thread
8173                (car thread))
8174       (mail-header-number (car thread)))))
8175
8176 (defun gnus-id-to-header (id)
8177   "Return the article headers of ID."
8178   (car (gnus-id-to-thread id)))
8179
8180 (defun gnus-article-displayed-root-p (article)
8181   "Say whether ARTICLE is a root(ish) article."
8182   (let ((level (gnus-summary-thread-level article))
8183         (refs (mail-header-references  (gnus-summary-article-header article)))
8184         particle)
8185     (cond 
8186      ((null level) nil)
8187      ((zerop level) t)
8188      ((null refs) t)
8189      ((null (gnus-parent-id refs)) t)
8190      ((and (= 1 level)
8191            (null (setq particle (gnus-id-to-article
8192                                  (gnus-parent-id refs))))
8193            (null (gnus-summary-thread-level particle)))))))
8194
8195 (defun gnus-root-id (id)
8196   "Return the id of the root of the thread where ID appears."
8197   (let (last-id prev)
8198     (while (and id (setq prev (car (gnus-gethash 
8199                                     id gnus-newsgroup-dependencies))))
8200       (setq last-id id
8201             id (gnus-parent-id (mail-header-references prev))))
8202     last-id))
8203
8204 (defun gnus-remove-thread (id &optional dont-remove)
8205   "Remove the thread that has ID in it."
8206   (let ((dep gnus-newsgroup-dependencies)
8207         headers thread last-id)
8208     ;; First go up in this thread until we find the root.
8209     (setq last-id (gnus-root-id id))
8210     (setq headers (list (car (gnus-id-to-thread last-id))
8211                         (caadr (gnus-id-to-thread last-id))))
8212     ;; We have now found the real root of this thread.  It might have
8213     ;; been gathered into some loose thread, so we have to search
8214     ;; through the threads to find the thread we wanted.
8215     (let ((threads gnus-newsgroup-threads)
8216           sub)
8217       (while threads
8218         (setq sub (car threads))
8219         (if (stringp (car sub))
8220             ;; This is a gathered threads, so we look at the roots
8221             ;; below it to find whether this article in in this
8222             ;; gathered root.
8223             (progn
8224               (setq sub (cdr sub))
8225               (while sub
8226                 (when (member (caar sub) headers)
8227                   (setq thread (car threads)
8228                         threads nil
8229                         sub nil))
8230                 (setq sub (cdr sub))))
8231           ;; It's an ordinary thread, so we check it.
8232           (when (eq (car sub) (car headers))
8233             (setq thread sub
8234                   threads nil)))
8235         (setq threads (cdr threads)))
8236       ;; If this article is in no thread, then it's a root.
8237       (if thread
8238           (unless dont-remove
8239             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8240         (setq thread (gnus-gethash last-id dep)))
8241       (when thread
8242         (prog1
8243             thread ; We return this thread.
8244           (unless dont-remove
8245             (if (stringp (car thread))
8246                 (progn
8247                   ;; If we use dummy roots, then we have to remove the
8248                   ;; dummy root as well.
8249                   (when (eq gnus-summary-make-false-root 'dummy)
8250                     ;; Uhm.
8251                     )
8252                   (setq thread (cdr thread))
8253                   (while thread
8254                     (gnus-remove-thread-1 (car thread))
8255                     (setq thread (cdr thread))))
8256               (gnus-remove-thread-1 thread))))))))
8257
8258 (defun gnus-remove-thread-1 (thread)
8259   "Remove the thread THREAD recursively."
8260   (let ((number (mail-header-number (car thread)))
8261         pos)
8262     (when (setq pos (text-property-any
8263                      (point-min) (point-max) 'gnus-number number))
8264       (goto-char pos)
8265       (gnus-delete-line)
8266       (gnus-data-remove number))
8267     (setq thread (cdr thread))
8268     (while thread
8269       (gnus-remove-thread-1 (pop thread)))))
8270
8271 (defun gnus-sort-threads (threads)
8272   "Sort THREADS."
8273   (if (not gnus-thread-sort-functions)
8274       threads
8275     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8276                     (car gnus-thread-sort-functions)
8277                   `(lambda (t1 t2)
8278                      ,(gnus-make-sort-function 
8279                        (reverse gnus-thread-sort-functions))))))
8280       (gnus-message 7 "Sorting threads...")
8281       (prog1
8282           (sort threads func)
8283         (gnus-message 7 "Sorting threads...done")))))
8284
8285 (defun gnus-sort-articles (articles)
8286   "Sort ARTICLES."
8287   (when gnus-article-sort-functions
8288     (let ((func (if (= 1 (length gnus-article-sort-functions))
8289                     (car gnus-article-sort-functions)
8290                   `(lambda (t1 t2)
8291                      ,(gnus-make-sort-function 
8292                        (reverse gnus-article-sort-functions))))))
8293       (gnus-message 7 "Sorting articles...")
8294       (prog1
8295           (setq gnus-newsgroup-headers (sort articles func))
8296         (gnus-message 7 "Sorting articles...done")))))
8297
8298 (defun gnus-make-sort-function (funs)
8299   "Return a composite sort condition based on the functions in FUNC."
8300   (if (cdr funs)
8301       `(or (,(car funs) t1 t2)
8302            (and (not (,(car funs) t2 t1))
8303                 ,(gnus-make-sort-function (cdr funs))))
8304     `(,(car funs) t1 t2)))
8305                  
8306 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8307 (defmacro gnus-thread-header (thread)
8308   ;; Return header of first article in THREAD.
8309   ;; Note that THREAD must never, ever be anything else than a variable -
8310   ;; using some other form will lead to serious barfage.
8311   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8312   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8313   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8314         (vector thread) 2))
8315
8316 (defsubst gnus-article-sort-by-number (h1 h2)
8317   "Sort articles by article number."
8318   (< (mail-header-number h1)
8319      (mail-header-number h2)))
8320
8321 (defun gnus-thread-sort-by-number (h1 h2)
8322   "Sort threads by root article number."
8323   (gnus-article-sort-by-number
8324    (gnus-thread-header h1) (gnus-thread-header h2)))
8325
8326 (defsubst gnus-article-sort-by-author (h1 h2)
8327   "Sort articles by root author."
8328   (string-lessp
8329    (let ((extract (funcall
8330                    gnus-extract-address-components
8331                    (mail-header-from h1))))
8332      (or (car extract) (cdr extract)))
8333    (let ((extract (funcall
8334                    gnus-extract-address-components
8335                    (mail-header-from h2))))
8336      (or (car extract) (cdr extract)))))
8337
8338 (defun gnus-thread-sort-by-author (h1 h2)
8339   "Sort threads by root author."
8340   (gnus-article-sort-by-author
8341    (gnus-thread-header h1)  (gnus-thread-header h2)))
8342
8343 (defsubst gnus-article-sort-by-subject (h1 h2)
8344   "Sort articles by root subject."
8345   (string-lessp
8346    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8347    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8348
8349 (defun gnus-thread-sort-by-subject (h1 h2)
8350   "Sort threads by root subject."
8351   (gnus-article-sort-by-subject
8352    (gnus-thread-header h1) (gnus-thread-header h2)))
8353
8354 (defsubst gnus-article-sort-by-date (h1 h2)
8355   "Sort articles by root article date."
8356   (string-lessp
8357    (inline (gnus-sortable-date (mail-header-date h1)))
8358    (inline (gnus-sortable-date (mail-header-date h2)))))
8359
8360 (defun gnus-thread-sort-by-date (h1 h2)
8361   "Sort threads by root article date."
8362   (gnus-article-sort-by-date
8363    (gnus-thread-header h1) (gnus-thread-header h2)))
8364
8365 (defsubst gnus-article-sort-by-score (h1 h2)
8366   "Sort articles by root article score.
8367 Unscored articles will be counted as having a score of zero."
8368   (> (or (cdr (assq (mail-header-number h1)
8369                     gnus-newsgroup-scored))
8370          gnus-summary-default-score 0)
8371      (or (cdr (assq (mail-header-number h2)
8372                     gnus-newsgroup-scored))
8373          gnus-summary-default-score 0)))
8374
8375 (defun gnus-thread-sort-by-score (h1 h2)
8376   "Sort threads by root article score."
8377   (gnus-article-sort-by-score
8378    (gnus-thread-header h1) (gnus-thread-header h2)))
8379
8380 (defun gnus-thread-sort-by-total-score (h1 h2)
8381   "Sort threads by the sum of all scores in the thread.
8382 Unscored articles will be counted as having a score of zero."
8383   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8384
8385 (defun gnus-thread-total-score (thread)
8386   ;;  This function find the total score of THREAD.
8387   (cond ((null thread)
8388          0)
8389         ((consp thread)
8390          (if (stringp (car thread))
8391              (apply gnus-thread-score-function 0
8392                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8393            (gnus-thread-total-score-1 thread)))
8394         (t
8395          (gnus-thread-total-score-1 (list thread)))))
8396
8397 (defun gnus-thread-total-score-1 (root)
8398   ;; This function find the total score of the thread below ROOT.
8399   (setq root (car root))
8400   (apply gnus-thread-score-function
8401          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8402              gnus-summary-default-score 0)
8403          (mapcar 'gnus-thread-total-score
8404                  (cdr (gnus-gethash (mail-header-id root)
8405                                     gnus-newsgroup-dependencies)))))
8406
8407 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8408 (defvar gnus-tmp-prev-subject nil)
8409 (defvar gnus-tmp-false-parent nil)
8410 (defvar gnus-tmp-root-expunged nil)
8411 (defvar gnus-tmp-dummy-line nil)
8412
8413 (defun gnus-summary-prepare-threads (threads)
8414   "Prepare summary buffer from THREADS and indentation LEVEL.
8415 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8416 or a straight list of headers."
8417   (gnus-message 7 "Generating summary...")
8418
8419   (setq gnus-newsgroup-threads threads)
8420   (beginning-of-line)
8421
8422   (let ((gnus-tmp-level 0)
8423         (default-score (or gnus-summary-default-score 0))
8424         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8425         thread number subject stack state gnus-tmp-gathered beg-match
8426         new-roots gnus-tmp-new-adopts thread-end
8427         gnus-tmp-header gnus-tmp-unread
8428         gnus-tmp-replied gnus-tmp-subject-or-nil
8429         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8430         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8431         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8432
8433     (setq gnus-tmp-prev-subject nil)
8434
8435     (if (vectorp (car threads))
8436         ;; If this is a straight (sic) list of headers, then a
8437         ;; threaded summary display isn't required, so we just create
8438         ;; an unthreaded one.
8439         (gnus-summary-prepare-unthreaded threads)
8440
8441       ;; Do the threaded display.
8442
8443       (while (or threads stack gnus-tmp-new-adopts new-roots)
8444
8445         (if (and (= gnus-tmp-level 0)
8446                  (not (setq gnus-tmp-dummy-line nil))
8447                  (or (not stack)
8448                      (= (caar stack) 0))
8449                  (not gnus-tmp-false-parent)
8450                  (or gnus-tmp-new-adopts new-roots))
8451             (if gnus-tmp-new-adopts
8452                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8453                       thread (list (car gnus-tmp-new-adopts))
8454                       gnus-tmp-header (caar thread)
8455                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8456               (if new-roots
8457                   (setq thread (list (car new-roots))
8458                         gnus-tmp-header (caar thread)
8459                         new-roots (cdr new-roots))))
8460
8461           (if threads
8462               ;; If there are some threads, we do them before the
8463               ;; threads on the stack.
8464               (setq thread threads
8465                     gnus-tmp-header (caar thread))
8466             ;; There were no current threads, so we pop something off
8467             ;; the stack.
8468             (setq state (car stack)
8469                   gnus-tmp-level (car state)
8470                   thread (cdr state)
8471                   stack (cdr stack)
8472                   gnus-tmp-header (caar thread))))
8473
8474         (setq gnus-tmp-false-parent nil)
8475         (setq gnus-tmp-root-expunged nil)
8476         (setq thread-end nil)
8477
8478         (if (stringp gnus-tmp-header)
8479             ;; The header is a dummy root.
8480             (cond
8481              ((eq gnus-summary-make-false-root 'adopt)
8482               ;; We let the first article adopt the rest.
8483               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8484                                                (cddar thread)))
8485               (setq gnus-tmp-gathered
8486                     (nconc (mapcar
8487                             (lambda (h) (mail-header-number (car h)))
8488                             (cddar thread))
8489                            gnus-tmp-gathered))
8490               (setq thread (cons (list (caar thread)
8491                                        (cadar thread))
8492                                  (cdr thread)))
8493               (setq gnus-tmp-level -1
8494                     gnus-tmp-false-parent t))
8495              ((eq gnus-summary-make-false-root 'empty)
8496               ;; We print adopted articles with empty subject fields.
8497               (setq gnus-tmp-gathered
8498                     (nconc (mapcar
8499                             (lambda (h) (mail-header-number (car h)))
8500                             (cddar thread))
8501                            gnus-tmp-gathered))
8502               (setq gnus-tmp-level -1))
8503              ((eq gnus-summary-make-false-root 'dummy)
8504               ;; We remember that we probably want to output a dummy
8505               ;; root.
8506               (setq gnus-tmp-dummy-line gnus-tmp-header)
8507               (setq gnus-tmp-prev-subject gnus-tmp-header))
8508              (t
8509               ;; We do not make a root for the gathered
8510               ;; sub-threads at all.
8511               (setq gnus-tmp-level -1)))
8512
8513           (setq number (mail-header-number gnus-tmp-header)
8514                 subject (mail-header-subject gnus-tmp-header))
8515
8516           (cond
8517            ;; If the thread has changed subject, we might want to make
8518            ;; this subthread into a root.
8519            ((and (null gnus-thread-ignore-subject)
8520                  (not (zerop gnus-tmp-level))
8521                  gnus-tmp-prev-subject
8522                  (not (inline
8523                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8524             (setq new-roots (nconc new-roots (list (car thread)))
8525                   thread-end t
8526                   gnus-tmp-header nil))
8527            ;; If the article lies outside the current limit,
8528            ;; then we do not display it.
8529            ((and (not (memq number gnus-newsgroup-limit))
8530                  (not gnus-tmp-dummy-line))
8531             (setq gnus-tmp-gathered
8532                   (nconc (mapcar
8533                           (lambda (h) (mail-header-number (car h)))
8534                           (cdar thread))
8535                          gnus-tmp-gathered))
8536             (setq gnus-tmp-new-adopts (if (cdar thread)
8537                                           (append gnus-tmp-new-adopts
8538                                                   (cdar thread))
8539                                         gnus-tmp-new-adopts)
8540                   thread-end t
8541                   gnus-tmp-header nil)
8542             (when (zerop gnus-tmp-level)
8543               (setq gnus-tmp-root-expunged t)))
8544            ;; Perhaps this article is to be marked as read?
8545            ((and gnus-summary-mark-below
8546                  (< (or (cdr (assq number gnus-newsgroup-scored))
8547                         default-score)
8548                     gnus-summary-mark-below)
8549                  ;; Don't touch sparse articles.
8550                  (not (memq number gnus-newsgroup-sparse))
8551                  (not (memq number gnus-newsgroup-ancient)))
8552             (setq gnus-newsgroup-unreads
8553                   (delq number gnus-newsgroup-unreads))
8554             (if gnus-newsgroup-auto-expire
8555                 (push number gnus-newsgroup-expirable)
8556               (push (cons number gnus-low-score-mark)
8557                     gnus-newsgroup-reads))))
8558
8559           (when gnus-tmp-header
8560             ;; We may have an old dummy line to output before this
8561             ;; article.
8562             (when gnus-tmp-dummy-line
8563               (gnus-summary-insert-dummy-line
8564                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8565               (setq gnus-tmp-dummy-line nil))
8566
8567             ;; Compute the mark.
8568             (setq
8569              gnus-tmp-unread
8570              (cond
8571               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8572               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8573               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8574               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8575               (t (or (cdr (assq number gnus-newsgroup-reads))
8576                      gnus-ancient-mark))))
8577
8578             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8579                                   gnus-tmp-header gnus-tmp-level)
8580                   gnus-newsgroup-data)
8581
8582             ;; Actually insert the line.
8583             (setq
8584              gnus-tmp-subject-or-nil
8585              (cond
8586               ((and gnus-thread-ignore-subject
8587                     gnus-tmp-prev-subject
8588                     (not (inline (gnus-subject-equal
8589                                   gnus-tmp-prev-subject subject))))
8590                subject)
8591               ((zerop gnus-tmp-level)
8592                (if (and (eq gnus-summary-make-false-root 'empty)
8593                         (memq number gnus-tmp-gathered)
8594                         gnus-tmp-prev-subject
8595                         (inline (gnus-subject-equal
8596                                  gnus-tmp-prev-subject subject)))
8597                    gnus-summary-same-subject
8598                  subject))
8599               (t gnus-summary-same-subject)))
8600             (if (and (eq gnus-summary-make-false-root 'adopt)
8601                      (= gnus-tmp-level 1)
8602                      (memq number gnus-tmp-gathered))
8603                 (setq gnus-tmp-opening-bracket ?\<
8604                       gnus-tmp-closing-bracket ?\>)
8605               (setq gnus-tmp-opening-bracket ?\[
8606                     gnus-tmp-closing-bracket ?\]))
8607             (setq
8608              gnus-tmp-indentation
8609              (aref gnus-thread-indent-array gnus-tmp-level)
8610              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8611              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8612                                 gnus-summary-default-score 0)
8613              gnus-tmp-score-char
8614              (if (or (null gnus-summary-default-score)
8615                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8616                          gnus-summary-zcore-fuzz)) ? 
8617                (if (< gnus-tmp-score gnus-summary-default-score)
8618                    gnus-score-below-mark gnus-score-over-mark))
8619              gnus-tmp-replied
8620              (cond ((memq number gnus-newsgroup-processable)
8621                     gnus-process-mark)
8622                    ((memq number gnus-newsgroup-cached)
8623                     gnus-cached-mark)
8624                    ((memq number gnus-newsgroup-replied)
8625                     gnus-replied-mark)
8626                    (t gnus-unread-mark))
8627              gnus-tmp-from (mail-header-from gnus-tmp-header)
8628              gnus-tmp-name
8629              (cond
8630               ((string-match "(.+)" gnus-tmp-from)
8631                (substring gnus-tmp-from
8632                           (1+ (match-beginning 0)) (1- (match-end 0))))
8633               ((string-match "<[^>]+> *$" gnus-tmp-from)
8634                (setq beg-match (match-beginning 0))
8635                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8636                         (substring gnus-tmp-from (1+ (match-beginning 0))
8637                                    (1- (match-end 0))))
8638                    (substring gnus-tmp-from 0 beg-match)))
8639               (t gnus-tmp-from)))
8640             (when (string= gnus-tmp-name "")
8641               (setq gnus-tmp-name gnus-tmp-from))
8642             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8643             (gnus-put-text-property
8644              (point)
8645              (progn (eval gnus-summary-line-format-spec) (point))
8646              'gnus-number number)
8647             (when gnus-visual-p
8648               (forward-line -1)
8649               (run-hooks 'gnus-summary-update-hook)
8650               (forward-line 1))
8651
8652             (setq gnus-tmp-prev-subject subject)))
8653
8654         (when (nth 1 thread)
8655           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8656         (incf gnus-tmp-level)
8657         (setq threads (if thread-end nil (cdar thread)))
8658         (unless threads
8659           (setq gnus-tmp-level 0)))))
8660   (gnus-message 7 "Generating summary...done"))
8661
8662 (defun gnus-summary-prepare-unthreaded (headers)
8663   "Generate an unthreaded summary buffer based on HEADERS."
8664   (let (header number mark)
8665
8666     (while headers
8667       ;; We may have to root out some bad articles...
8668       (when (memq (setq number (mail-header-number
8669                                 (setq header (pop headers))))
8670                   gnus-newsgroup-limit)
8671         ;; Mark article as read when it has a low score.
8672         (when (and gnus-summary-mark-below
8673                    (< (or (cdr (assq number gnus-newsgroup-scored))
8674                           gnus-summary-default-score 0)
8675                       gnus-summary-mark-below)
8676                    (not (memq number gnus-newsgroup-ancient)))
8677           (setq gnus-newsgroup-unreads
8678                 (delq number gnus-newsgroup-unreads))
8679           (if gnus-newsgroup-auto-expire
8680               (push number gnus-newsgroup-expirable)
8681             (push (cons number gnus-low-score-mark)
8682                   gnus-newsgroup-reads)))
8683
8684         (setq mark
8685               (cond
8686                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8687                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8688                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8689                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8690                (t (or (cdr (assq number gnus-newsgroup-reads))
8691                       gnus-ancient-mark))))
8692         (setq gnus-newsgroup-data
8693               (cons (gnus-data-make number mark (1+ (point)) header 0)
8694                     gnus-newsgroup-data))
8695         (gnus-summary-insert-line
8696          header 0 nil mark (memq number gnus-newsgroup-replied)
8697          (memq number gnus-newsgroup-expirable)
8698          (mail-header-subject header) nil
8699          (cdr (assq number gnus-newsgroup-scored))
8700          (memq number gnus-newsgroup-processable))))))
8701
8702 (defun gnus-select-newsgroup (group &optional read-all)
8703   "Select newsgroup GROUP.
8704 If READ-ALL is non-nil, all articles in the group are selected."
8705   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8706          (info (nth 2 entry))
8707          articles fetched-articles cached)
8708
8709     (or (gnus-check-server
8710          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8711         (error "Couldn't open server"))
8712
8713     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8714         (gnus-activate-group group)     ; Or we can activate it...
8715         (progn                          ; Or we bug out.
8716           (when (equal major-mode 'gnus-summary-mode)
8717             (kill-buffer (current-buffer)))
8718           (error "Couldn't request group %s: %s"
8719                  group (gnus-status-message group))))
8720
8721     (unless (gnus-request-group group t)
8722       (when (equal major-mode 'gnus-summary-mode)
8723         (kill-buffer (current-buffer)))
8724       (error "Couldn't request group %s: %s"
8725              group (gnus-status-message group)))      
8726
8727     (setq gnus-newsgroup-name group)
8728     (setq gnus-newsgroup-unselected nil)
8729     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8730
8731     (and gnus-asynchronous
8732          (gnus-check-backend-function
8733           'request-asynchronous gnus-newsgroup-name)
8734          (setq gnus-newsgroup-async
8735                (gnus-request-asynchronous gnus-newsgroup-name)))
8736
8737     ;; Adjust and set lists of article marks.
8738     (when info
8739       (gnus-adjust-marked-articles info))
8740
8741     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8742     (when (gnus-virtual-group-p group)
8743       (setq cached gnus-newsgroup-cached))
8744
8745     (setq gnus-newsgroup-unreads
8746           (gnus-set-difference
8747            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8748            gnus-newsgroup-dormant))
8749
8750     (setq gnus-newsgroup-processable nil)
8751
8752     (setq articles (gnus-articles-to-read group read-all))
8753
8754     (cond
8755      ((null articles)
8756       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8757       'quit)
8758      ((eq articles 0) nil)
8759      (t
8760       ;; Init the dependencies hash table.
8761       (setq gnus-newsgroup-dependencies
8762             (gnus-make-hashtable (length articles)))
8763       ;; Retrieve the headers and read them in.
8764       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8765       (setq gnus-newsgroup-headers
8766             (if (eq 'nov
8767                     (setq gnus-headers-retrieved-by
8768                           (gnus-retrieve-headers
8769                            articles gnus-newsgroup-name
8770                            ;; We might want to fetch old headers, but
8771                            ;; not if there is only 1 article.
8772                            (and gnus-fetch-old-headers
8773                                 (or (and
8774                                      (not (eq gnus-fetch-old-headers 'some))
8775                                      (not (numberp gnus-fetch-old-headers)))
8776                                     (> (length articles) 1))))))
8777                 (gnus-get-newsgroup-headers-xover articles)
8778               (gnus-get-newsgroup-headers)))
8779       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8780
8781       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8782       (when cached
8783         (setq gnus-newsgroup-cached cached))
8784
8785       ;; Set the initial limit.
8786       (setq gnus-newsgroup-limit (copy-sequence articles))
8787       ;; Remove canceled articles from the list of unread articles.
8788       (setq gnus-newsgroup-unreads
8789             (gnus-set-sorted-intersection
8790              gnus-newsgroup-unreads
8791              (setq fetched-articles
8792                    (mapcar (lambda (headers) (mail-header-number headers))
8793                            gnus-newsgroup-headers))))
8794       ;; Removed marked articles that do not exist.
8795       (gnus-update-missing-marks
8796        (gnus-sorted-complement fetched-articles articles))
8797       ;; We might want to build some more threads first.
8798       (and gnus-fetch-old-headers
8799            (eq gnus-headers-retrieved-by 'nov)
8800            (gnus-build-old-threads))
8801       ;; Check whether auto-expire is to be done in this group.
8802       (setq gnus-newsgroup-auto-expire
8803             (gnus-group-auto-expirable-p group))
8804       ;; Set up the article buffer now, if necessary.
8805       (unless gnus-single-article-buffer
8806         (gnus-article-setup-buffer))
8807       ;; First and last article in this newsgroup.
8808       (when gnus-newsgroup-headers
8809         (setq gnus-newsgroup-begin
8810               (mail-header-number (car gnus-newsgroup-headers))
8811               gnus-newsgroup-end
8812               (mail-header-number
8813                (gnus-last-element gnus-newsgroup-headers))))
8814       (setq gnus-reffed-article-number -1)
8815       ;; GROUP is successfully selected.
8816       (or gnus-newsgroup-headers t)))))
8817
8818 (defun gnus-articles-to-read (group read-all)
8819   ;; Find out what articles the user wants to read.
8820   (let* ((articles
8821           ;; Select all articles if `read-all' is non-nil, or if there
8822           ;; are no unread articles.
8823           (if (or read-all
8824                   (and (zerop (length gnus-newsgroup-marked))
8825                        (zerop (length gnus-newsgroup-unreads))))
8826               (gnus-uncompress-range (gnus-active group))
8827             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8828                           (copy-sequence gnus-newsgroup-unreads))
8829                   '<)))
8830          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8831          (scored (length scored-list))
8832          (number (length articles))
8833          (marked (+ (length gnus-newsgroup-marked)
8834                     (length gnus-newsgroup-dormant)))
8835          (select
8836           (cond
8837            ((numberp read-all)
8838             read-all)
8839            (t
8840             (condition-case ()
8841                 (cond
8842                  ((and (or (<= scored marked) (= scored number))
8843                        (numberp gnus-large-newsgroup)
8844                        (> number gnus-large-newsgroup))
8845                   (let ((input
8846                          (read-string
8847                           (format
8848                            "How many articles from %s (default %d): "
8849                            gnus-newsgroup-name number))))
8850                     (if (string-match "^[ \t]*$" input) number input)))
8851                  ((and (> scored marked) (< scored number)
8852                        (> (- scored number) 20))
8853                   (let ((input
8854                          (read-string
8855                           (format "%s %s (%d scored, %d total): "
8856                                   "How many articles from"
8857                                   group scored number))))
8858                     (if (string-match "^[ \t]*$" input)
8859                         number input)))
8860                  (t number))
8861               (quit nil))))))
8862     (setq select (if (stringp select) (string-to-number select) select))
8863     (if (or (null select) (zerop select))
8864         select
8865       (if (and (not (zerop scored)) (<= (abs select) scored))
8866           (progn
8867             (setq articles (sort scored-list '<))
8868             (setq number (length articles)))
8869         (setq articles (copy-sequence articles)))
8870
8871       (if (< (abs select) number)
8872           (if (< select 0)
8873               ;; Select the N oldest articles.
8874               (setcdr (nthcdr (1- (abs select)) articles) nil)
8875             ;; Select the N most recent articles.
8876             (setq articles (nthcdr (- number select) articles))))
8877       (setq gnus-newsgroup-unselected
8878             (gnus-sorted-intersection
8879              gnus-newsgroup-unreads
8880              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8881       articles)))
8882
8883 (defun gnus-killed-articles (killed articles)
8884   (let (out)
8885     (while articles
8886       (if (inline (gnus-member-of-range (car articles) killed))
8887           (setq out (cons (car articles) out)))
8888       (setq articles (cdr articles)))
8889     out))
8890
8891 (defun gnus-uncompress-marks (marks)
8892   "Uncompress the mark ranges in MARKS."
8893   (let ((uncompressed '(score bookmark))
8894         out)
8895     (while marks
8896       (if (memq (caar marks) uncompressed)
8897           (push (car marks) out)
8898         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8899       (setq marks (cdr marks)))
8900     out))
8901
8902 (defun gnus-adjust-marked-articles (info)
8903   "Set all article lists and remove all marks that are no longer legal."
8904   (let* ((marked-lists (gnus-info-marks info))
8905          (active (gnus-active (gnus-info-group info)))
8906          (min (car active))
8907          (max (cdr active))
8908          (types gnus-article-mark-lists)
8909          (uncompressed '(score bookmark))
8910          marks var articles article mark)
8911
8912     (while marked-lists
8913       (setq marks (pop marked-lists))
8914       (set (setq var (intern (format "gnus-newsgroup-%s"
8915                                      (car (rassq (setq mark (car marks))
8916                                                  types)))))
8917            (if (memq (car marks) uncompressed) (cdr marks)
8918              (gnus-uncompress-range (cdr marks))))
8919
8920       (setq articles (symbol-value var))
8921
8922       ;; All articles have to be subsets of the active articles.
8923       (cond
8924        ;; Adjust "simple" lists.
8925        ((memq mark '(tick dormant expirable reply killed save))
8926         (while articles
8927           (when (or (< (setq article (pop articles)) min) (> article max))
8928             (set var (delq article (symbol-value var))))))
8929        ;; Adjust assocs.
8930        ((memq mark '(score bookmark))
8931         (while articles
8932           (when (or (< (car (setq article (pop articles))) min)
8933                     (> (car article) max))
8934             (set var (delq article (symbol-value var))))))))))
8935
8936 (defun gnus-update-missing-marks (missing)
8937   "Go through the list of MISSING articles and remove them mark lists."
8938   (when missing
8939     (let ((types gnus-article-mark-lists)
8940           var m)
8941       ;; Go through all types.
8942       (while types
8943         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8944         (when (symbol-value var)
8945           ;; This list has articles.  So we delete all missing articles
8946           ;; from it.
8947           (setq m missing)
8948           (while m
8949             (set var (delq (pop m) (symbol-value var)))))))))
8950
8951 (defun gnus-update-marks ()
8952   "Enter the various lists of marked articles into the newsgroup info list."
8953   (let ((types gnus-article-mark-lists)
8954         (info (gnus-get-info gnus-newsgroup-name))
8955         (uncompressed '(score bookmark killed))
8956         type list newmarked symbol)
8957     (when info
8958       ;; Add all marks lists that are non-nil to the list of marks lists.
8959       (while types
8960         (setq type (pop types))
8961         (when (setq list (symbol-value
8962                           (setq symbol
8963                                 (intern (format "gnus-newsgroup-%s"
8964                                                 (car type))))))
8965           (push (cons (cdr type)
8966                       (if (memq (cdr type) uncompressed) list
8967                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8968                 newmarked)))
8969
8970       ;; Enter these new marks into the info of the group.
8971       (if (nthcdr 3 info)
8972           (setcar (nthcdr 3 info) newmarked)
8973         ;; Add the marks lists to the end of the info.
8974         (when newmarked
8975           (setcdr (nthcdr 2 info) (list newmarked))))
8976
8977       ;; Cut off the end of the info if there's nothing else there.
8978       (let ((i 5))
8979         (while (and (> i 2)
8980                     (not (nth i info)))
8981           (when (nthcdr (decf i) info)
8982             (setcdr (nthcdr i info) nil)))))))
8983
8984 (defun gnus-add-marked-articles (group type articles &optional info force)
8985   ;; Add ARTICLES of TYPE to the info of GROUP.
8986   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8987   ;; add, but replace marked articles of TYPE with ARTICLES.
8988   (let ((info (or info (gnus-get-info group)))
8989         (uncompressed '(score bookmark killed))
8990         marked m)
8991     (or (not info)
8992         (and (not (setq marked (nthcdr 3 info)))
8993              (or (null articles)
8994                  (setcdr (nthcdr 2 info)
8995                          (list (list (cons type (gnus-compress-sequence
8996                                                  articles t)))))))
8997         (and (not (setq m (assq type (car marked))))
8998              (or (null articles)
8999                  (setcar marked
9000                          (cons (cons type (gnus-compress-sequence articles t) )
9001                                (car marked)))))
9002         (if force
9003             (if (null articles)
9004                 (setcar (nthcdr 3 info)
9005                         (delq (assq type (car marked)) (car marked)))
9006               (setcdr m (gnus-compress-sequence articles t)))
9007           (setcdr m (gnus-compress-sequence
9008                      (sort (nconc (gnus-uncompress-range (cdr m))
9009                                   (copy-sequence articles)) '<) t))))))
9010
9011 (defun gnus-set-mode-line (where)
9012   "This function sets the mode line of the article or summary buffers.
9013 If WHERE is `summary', the summary mode line format will be used."
9014   ;; Is this mode line one we keep updated?
9015   (when (memq where gnus-updated-mode-lines)
9016     (let (mode-string)
9017       (save-excursion
9018         ;; We evaluate this in the summary buffer since these
9019         ;; variables are buffer-local to that buffer.
9020         (set-buffer gnus-summary-buffer)
9021         ;; We bind all these variables that are used in the `eval' form
9022         ;; below.
9023         (let* ((mformat (symbol-value
9024                          (intern
9025                           (format "gnus-%s-mode-line-format-spec" where))))
9026                (gnus-tmp-group-name gnus-newsgroup-name)
9027                (gnus-tmp-article-number (or gnus-current-article 0))
9028                (gnus-tmp-unread gnus-newsgroup-unreads)
9029                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9030                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9031                (gnus-tmp-unread-and-unselected
9032                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9033                             (zerop gnus-tmp-unselected)) "")
9034                       ((zerop gnus-tmp-unselected)
9035                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9036                       (t (format "{%d(+%d) more}"
9037                                  gnus-tmp-unread-and-unticked
9038                                  gnus-tmp-unselected))))
9039                (gnus-tmp-subject
9040                 (if (and gnus-current-headers
9041                          (vectorp gnus-current-headers))
9042                     (gnus-mode-string-quote
9043                      (mail-header-subject gnus-current-headers)) ""))
9044                max-len
9045                gnus-tmp-header);; passed as argument to any user-format-funcs
9046           (setq mode-string (eval mformat))
9047           (setq max-len (max 4 (if gnus-mode-non-string-length
9048                                    (- (window-width)
9049                                       gnus-mode-non-string-length)
9050                                  (length mode-string))))
9051           ;; We might have to chop a bit of the string off...
9052           (when (> (length mode-string) max-len)
9053             (setq mode-string
9054                   (concat (gnus-truncate-string mode-string (- max-len 3))
9055                           "...")))
9056           ;; Pad the mode string a bit.
9057           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9058       ;; Update the mode line.
9059       (setq mode-line-buffer-identification (list mode-string))
9060       (set-buffer-modified-p t))))
9061
9062 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9063   "Go through the HEADERS list and add all Xrefs to a hash table.
9064 The resulting hash table is returned, or nil if no Xrefs were found."
9065   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9066          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9067          (xref-hashtb (make-vector 63 0))
9068          start group entry number xrefs header)
9069     (while headers
9070       (setq header (pop headers))
9071       (when (and (setq xrefs (mail-header-xref header))
9072                  (not (memq (setq number (mail-header-number header))
9073                             unreads)))
9074         (setq start 0)
9075         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9076           (setq start (match-end 0))
9077           (setq group (if prefix
9078                           (concat prefix (substring xrefs (match-beginning 1)
9079                                                     (match-end 1)))
9080                         (substring xrefs (match-beginning 1) (match-end 1))))
9081           (setq number
9082                 (string-to-int (substring xrefs (match-beginning 2)
9083                                           (match-end 2))))
9084           (if (setq entry (gnus-gethash group xref-hashtb))
9085               (setcdr entry (cons number (cdr entry)))
9086             (gnus-sethash group (cons number nil) xref-hashtb)))))
9087     (and start xref-hashtb)))
9088
9089 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9090   "Look through all the headers and mark the Xrefs as read."
9091   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9092         name entry info xref-hashtb idlist method nth4)
9093     (save-excursion
9094       (set-buffer gnus-group-buffer)
9095       (when (setq xref-hashtb
9096                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9097         (mapatoms
9098          (lambda (group)
9099            (unless (string= from-newsgroup (setq name (symbol-name group)))
9100              (setq idlist (symbol-value group))
9101              ;; Dead groups are not updated.
9102              (and (prog1
9103                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9104                             info (nth 2 entry))
9105                     (if (stringp (setq nth4 (gnus-info-method info)))
9106                         (setq nth4 (gnus-server-to-method nth4))))
9107                   ;; Only do the xrefs if the group has the same
9108                   ;; select method as the group we have just read.
9109                   (or (gnus-methods-equal-p
9110                        nth4 (gnus-find-method-for-group from-newsgroup))
9111                       virtual
9112                       (equal nth4 (setq method (gnus-find-method-for-group
9113                                                 from-newsgroup)))
9114                       (and (equal (car nth4) (car method))
9115                            (equal (nth 1 nth4) (nth 1 method))))
9116                   gnus-use-cross-reference
9117                   (or (not (eq gnus-use-cross-reference t))
9118                       virtual
9119                       ;; Only do cross-references on subscribed
9120                       ;; groups, if that is what is wanted.
9121                       (<= (gnus-info-level info) gnus-level-subscribed))
9122                   (gnus-group-make-articles-read name idlist))))
9123          xref-hashtb)))))
9124
9125 (defun gnus-group-make-articles-read (group articles)
9126   (let* ((num 0)
9127          (entry (gnus-gethash group gnus-newsrc-hashtb))
9128          (info (nth 2 entry))
9129          (active (gnus-active group))
9130          range)
9131     ;; First peel off all illegal article numbers.
9132     (if active
9133         (let ((ids articles)
9134               id first)
9135           (while ids
9136             (setq id (car ids))
9137             (if (and first (> id (cdr active)))
9138                 (progn
9139                   ;; We'll end up in this situation in one particular
9140                   ;; obscure situation.  If you re-scan a group and get
9141                   ;; a new article that is cross-posted to a different
9142                   ;; group that has not been re-scanned, you might get
9143                   ;; crossposted article that has a higher number than
9144                   ;; Gnus believes possible.  So we re-activate this
9145                   ;; group as well.  This might mean doing the
9146                   ;; crossposting thingy will *increase* the number
9147                   ;; of articles in some groups.  Tsk, tsk.
9148                   (setq active (or (gnus-activate-group group) active))))
9149             (if (or (> id (cdr active))
9150                     (< id (car active)))
9151                 (setq articles (delq id articles)))
9152             (setq ids (cdr ids)))))
9153     ;; If the read list is nil, we init it.
9154     (and active
9155          (null (gnus-info-read info))
9156          (> (car active) 1)
9157          (gnus-info-set-read info (cons 1 (1- (car active)))))
9158     ;; Then we add the read articles to the range.
9159     (gnus-info-set-read
9160      info
9161      (setq range
9162            (gnus-add-to-range
9163             (gnus-info-read info) (setq articles (sort articles '<)))))
9164     ;; Then we have to re-compute how many unread
9165     ;; articles there are in this group.
9166     (if active
9167         (progn
9168           (cond
9169            ((not range)
9170             (setq num (- (1+ (cdr active)) (car active))))
9171            ((not (listp (cdr range)))
9172             (setq num (- (cdr active) (- (1+ (cdr range))
9173                                          (car range)))))
9174            (t
9175             (while range
9176               (if (numberp (car range))
9177                   (setq num (1+ num))
9178                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9179               (setq range (cdr range)))
9180             (setq num (- (cdr active) num))))
9181           ;; Update the number of unread articles.
9182           (setcar entry num)
9183           ;; Update the group buffer.
9184           (gnus-group-update-group group t)))))
9185
9186 (defun gnus-methods-equal-p (m1 m2)
9187   (let ((m1 (or m1 gnus-select-method))
9188         (m2 (or m2 gnus-select-method)))
9189     (or (equal m1 m2)
9190         (and (eq (car m1) (car m2))
9191              (or (not (memq 'address (assoc (symbol-name (car m1))
9192                                             gnus-valid-select-methods)))
9193                  (equal (nth 1 m1) (nth 1 m2)))))))
9194
9195 (defsubst gnus-header-value ()
9196   (buffer-substring 
9197    (match-end 0) 
9198    (if (re-search-forward "^[^ \t]" nil t)
9199        (progn
9200          (backward-char 2)
9201          (point))
9202      (gnus-point-at-eol))))
9203
9204 (defvar gnus-newsgroup-none-id 0)
9205
9206 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9207   (let ((cur nntp-server-buffer)
9208         (dependencies
9209          (or dependencies
9210              (save-excursion (set-buffer gnus-summary-buffer)
9211                              gnus-newsgroup-dependencies)))
9212         headers id id-dep ref-dep end ref)
9213     (save-excursion
9214       (set-buffer nntp-server-buffer)
9215       (run-hooks 'gnus-parse-headers-hook)
9216       (let ((case-fold-search t)
9217             in-reply-to header p lines)
9218         (goto-char (point-min))
9219         ;; Search to the beginning of the next header.  Error messages
9220         ;; do not begin with 2 or 3.
9221         (while (re-search-forward "^[23][0-9]+ " nil t)
9222           (setq id nil
9223                 ref nil)
9224           ;; This implementation of this function, with nine
9225           ;; search-forwards instead of the one re-search-forward and
9226           ;; a case (which basically was the old function) is actually
9227           ;; about twice as fast, even though it looks messier.  You
9228           ;; can't have everything, I guess.  Speed and elegance
9229           ;; doesn't always go hand in hand.
9230           (setq
9231            header
9232            (vector
9233             ;; Number.
9234             (prog1
9235                 (read cur)
9236               (end-of-line)
9237               (setq p (point))
9238               (narrow-to-region (point)
9239                                 (or (and (search-forward "\n.\n" nil t)
9240                                          (- (point) 2))
9241                                     (point))))
9242             ;; Subject.
9243             (progn
9244               (goto-char p)
9245               (if (search-forward "\nsubject: " nil t)
9246                   (gnus-header-value) "(none)"))
9247             ;; From.
9248             (progn
9249               (goto-char p)
9250               (if (search-forward "\nfrom: " nil t)
9251                   (gnus-header-value) "(nobody)"))
9252             ;; Date.
9253             (progn
9254               (goto-char p)
9255               (if (search-forward "\ndate: " nil t)
9256                   (gnus-header-value) ""))
9257             ;; Message-ID.
9258             (progn
9259               (goto-char p)
9260               (if (search-forward "\nmessage-id: " nil t)
9261                   (setq id (gnus-header-value))
9262                 ;; If there was no message-id, we just fake one to make
9263                 ;; subsequent routines simpler.
9264                 (setq id (concat "none+"
9265                                  (int-to-string
9266                                   (setq gnus-newsgroup-none-id
9267                                         (1+ gnus-newsgroup-none-id)))))))
9268             ;; References.
9269             (progn
9270               (goto-char p)
9271               (if (search-forward "\nreferences: " nil t)
9272                   (progn
9273                     (setq end (point))
9274                     (prog1
9275                         (gnus-header-value)
9276                       (setq ref
9277                             (buffer-substring
9278                              (progn
9279                                (end-of-line)
9280                                (search-backward ">" end t)
9281                                (1+ (point)))
9282                              (progn
9283                                (search-backward "<" end t)
9284                                (point))))))
9285                 ;; Get the references from the in-reply-to header if there
9286                 ;; were no references and the in-reply-to header looks
9287                 ;; promising.
9288                 (if (and (search-forward "\nin-reply-to: " nil t)
9289                          (setq in-reply-to (gnus-header-value))
9290                          (string-match "<[^>]+>" in-reply-to))
9291                     (setq ref (substring in-reply-to (match-beginning 0)
9292                                          (match-end 0)))
9293                   (setq ref ""))))
9294             ;; Chars.
9295             0
9296             ;; Lines.
9297             (progn
9298               (goto-char p)
9299               (if (search-forward "\nlines: " nil t)
9300                   (if (numberp (setq lines (read cur)))
9301                       lines 0)
9302                 0))
9303             ;; Xref.
9304             (progn
9305               (goto-char p)
9306               (and (search-forward "\nxref: " nil t)
9307                    (gnus-header-value)))))
9308           ;; We do the threading while we read the headers.  The
9309           ;; message-id and the last reference are both entered into
9310           ;; the same hash table.  Some tippy-toeing around has to be
9311           ;; done in case an article has arrived before the article
9312           ;; which it refers to.
9313           (if (boundp (setq id-dep (intern id dependencies)))
9314               (if (and (car (symbol-value id-dep))
9315                        (not force-new))
9316                   ;; An article with this Message-ID has already
9317                   ;; been seen, so we ignore this one, except we add
9318                   ;; any additional Xrefs (in case the two articles
9319                   ;; came from different servers).
9320                   (progn
9321                     (mail-header-set-xref
9322                      (car (symbol-value id-dep))
9323                      (concat (or (mail-header-xref
9324                                   (car (symbol-value id-dep))) "")
9325                              (or (mail-header-xref header) "")))
9326                     (setq header nil))
9327                 (setcar (symbol-value id-dep) header))
9328             (set id-dep (list header)))
9329           (when header
9330             (if (boundp (setq ref-dep (intern ref dependencies)))
9331                 (setcdr (symbol-value ref-dep)
9332                         (nconc (cdr (symbol-value ref-dep))
9333                                (list (symbol-value id-dep))))
9334               (set ref-dep (list nil (symbol-value id-dep))))
9335             (setq headers (cons header headers)))
9336           (goto-char (point-max))
9337           (widen))
9338         (nreverse headers)))))
9339
9340 ;; The following macros and functions were written by Felix Lee
9341 ;; <flee@cse.psu.edu>.
9342
9343 (defmacro gnus-nov-read-integer ()
9344   '(prog1
9345        (if (= (following-char) ?\t)
9346            0
9347          (let ((num (condition-case nil (read buffer) (error nil))))
9348            (if (numberp num) num 0)))
9349      (or (eobp) (forward-char 1))))
9350
9351 (defmacro gnus-nov-skip-field ()
9352   '(search-forward "\t" eol 'move))
9353
9354 (defmacro gnus-nov-field ()
9355   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9356
9357 ;; Goes through the xover lines and returns a list of vectors
9358 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9359                                                   force-new dependencies)
9360   "Parse the news overview data in the server buffer, and return a
9361 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9362   ;; Get the Xref when the users reads the articles since most/some
9363   ;; NNTP servers do not include Xrefs when using XOVER.
9364   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9365   (let ((cur nntp-server-buffer)
9366         (dependencies (or dependencies gnus-newsgroup-dependencies))
9367         number headers header)
9368     (save-excursion
9369       (set-buffer nntp-server-buffer)
9370       ;; Allow the user to mangle the headers before parsing them.
9371       (run-hooks 'gnus-parse-headers-hook)
9372       (goto-char (point-min))
9373       (while (and sequence (not (eobp)))
9374         (setq number (read cur))
9375         (while (and sequence (< (car sequence) number))
9376           (setq sequence (cdr sequence)))
9377         (and sequence
9378              (eq number (car sequence))
9379              (progn
9380                (setq sequence (cdr sequence))
9381                (if (setq header
9382                          (inline (gnus-nov-parse-line
9383                                   number dependencies force-new)))
9384                    (setq headers (cons header headers)))))
9385         (forward-line 1))
9386       (setq headers (nreverse headers)))
9387     headers))
9388
9389 ;; This function has to be called with point after the article number
9390 ;; on the beginning of the line.
9391 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9392   (let ((none 0)
9393         (eol (gnus-point-at-eol))
9394         (buffer (current-buffer))
9395         header ref id id-dep ref-dep)
9396
9397     ;; overview: [num subject from date id refs chars lines misc]
9398     (narrow-to-region (point) eol)
9399     (or (eobp) (forward-char))
9400
9401     (condition-case nil
9402         (setq header
9403               (vector
9404                number                   ; number
9405                (gnus-nov-field)         ; subject
9406                (gnus-nov-field)         ; from
9407                (gnus-nov-field)         ; date
9408                (setq id (or (gnus-nov-field)
9409                             (concat "none+"
9410                                     (int-to-string
9411                                      (setq none (1+ none)))))) ; id
9412                (progn
9413                  (save-excursion
9414                    (let ((beg (point)))
9415                      (search-forward "\t" eol)
9416                      (if (search-backward ">" beg t)
9417                          (setq ref
9418                                (buffer-substring
9419                                 (1+ (point))
9420                                 (search-backward "<" beg t)))
9421                        (setq ref nil))))
9422                  (gnus-nov-field))      ; refs
9423                (gnus-nov-read-integer)  ; chars
9424                (gnus-nov-read-integer)  ; lines
9425                (if (= (following-char) ?\n)
9426                    nil
9427                  (gnus-nov-field))      ; misc
9428                ))
9429       (error (progn
9430                (gnus-error 4 "Strange nov line")
9431                (setq header nil)
9432                (goto-char eol))))
9433
9434     (widen)
9435
9436     ;; We build the thread tree.
9437     (when header
9438       (if (boundp (setq id-dep (intern id dependencies)))
9439           (if (and (car (symbol-value id-dep))
9440                    (not force-new))
9441               ;; An article with this Message-ID has already been seen,
9442               ;; so we ignore this one, except we add any additional
9443               ;; Xrefs (in case the two articles came from different
9444               ;; servers.
9445               (progn
9446                 (mail-header-set-xref
9447                  (car (symbol-value id-dep))
9448                  (concat (or (mail-header-xref
9449                               (car (symbol-value id-dep))) "")
9450                          (or (mail-header-xref header) "")))
9451                 (setq header nil))
9452             (setcar (symbol-value id-dep) header))
9453         (set id-dep (list header))))
9454     (when header
9455       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9456           (setcdr (symbol-value ref-dep)
9457                   (nconc (cdr (symbol-value ref-dep))
9458                          (list (symbol-value id-dep))))
9459         (set ref-dep (list nil (symbol-value id-dep)))))
9460     header))
9461
9462 (defun gnus-article-get-xrefs ()
9463   "Fill in the Xref value in `gnus-current-headers', if necessary.
9464 This is meant to be called in `gnus-article-internal-prepare-hook'."
9465   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9466                                  gnus-current-headers)))
9467     (or (not gnus-use-cross-reference)
9468         (not headers)
9469         (and (mail-header-xref headers)
9470              (not (string= (mail-header-xref headers) "")))
9471         (let ((case-fold-search t)
9472               xref)
9473           (save-restriction
9474             (nnheader-narrow-to-headers)
9475             (goto-char (point-min))
9476             (if (or (and (eq (downcase (following-char)) ?x)
9477                          (looking-at "Xref:"))
9478                     (search-forward "\nXref:" nil t))
9479                 (progn
9480                   (goto-char (1+ (match-end 0)))
9481                   (setq xref (buffer-substring (point)
9482                                                (progn (end-of-line) (point))))
9483                   (mail-header-set-xref headers xref))))))))
9484
9485 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9486   "Find article ID and insert the summary line for that article."
9487   (let ((header (if (and old-header use-old-header)
9488                     old-header (gnus-read-header id)))
9489         (number (and (numberp id) id))
9490         pos)
9491     (when header
9492       ;; Rebuild the thread that this article is part of and go to the
9493       ;; article we have fetched.
9494       (when (and (not gnus-show-threads)
9495                  old-header)
9496         (when (setq pos (text-property-any
9497                          (point-min) (point-max) 'gnus-number 
9498                          (mail-header-number old-header)))
9499           (goto-char pos)
9500           (gnus-delete-line)
9501           (gnus-data-remove (mail-header-number old-header))))
9502       (when old-header
9503         (mail-header-set-number header (mail-header-number old-header)))
9504       (setq gnus-newsgroup-sparse
9505             (delq (setq number (mail-header-number header)) 
9506                   gnus-newsgroup-sparse))
9507       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9508       (gnus-rebuild-thread (mail-header-id header))
9509       (gnus-summary-goto-subject number nil t))
9510     (when (and (numberp number)
9511                (> number 0))
9512       ;; We have to update the boundaries even if we can't fetch the
9513       ;; article if ID is a number -- so that the next `P' or `N'
9514       ;; command will fetch the previous (or next) article even
9515       ;; if the one we tried to fetch this time has been canceled.
9516       (and (> number gnus-newsgroup-end)
9517            (setq gnus-newsgroup-end number))
9518       (and (< number gnus-newsgroup-begin)
9519            (setq gnus-newsgroup-begin number))
9520       (setq gnus-newsgroup-unselected
9521             (delq number gnus-newsgroup-unselected)))
9522     ;; Report back a success?
9523     (and header (mail-header-number header))))
9524
9525 (defun gnus-summary-work-articles (n)
9526   "Return a list of articles to be worked upon.  The prefix argument,
9527 the list of process marked articles, and the current article will be
9528 taken into consideration."
9529   (cond
9530    ((and n (numberp n))
9531     ;; A numerical prefix has been given.
9532     (let ((backward (< n 0))
9533           (n (abs n))
9534           articles article)
9535       (save-excursion
9536         (while
9537             (and (> n 0)
9538                  (push (setq article (gnus-summary-article-number))
9539                        articles)
9540                  (if backward
9541                      (gnus-summary-find-prev nil article)
9542                    (gnus-summary-find-next nil article)))
9543           (decf n)))
9544       (nreverse articles)))
9545    ((and (boundp 'transient-mark-mode)
9546          transient-mark-mode
9547          mark-active)
9548     ;; Work on the region between point and mark.
9549     (let ((max (max (point) (mark)))
9550           articles article)
9551       (save-excursion
9552         (goto-char (min (point) (mark)))
9553         (while
9554             (and
9555              (push (setq article (gnus-summary-article-number)) articles)
9556              (gnus-summary-find-next nil article)
9557              (< (point) max)))
9558         (nreverse articles))))
9559    (gnus-newsgroup-processable
9560     ;; There are process-marked articles present.
9561     (reverse gnus-newsgroup-processable))
9562    (t
9563     ;; Just return the current article.
9564     (list (gnus-summary-article-number)))))
9565
9566 (defun gnus-summary-search-group (&optional backward use-level)
9567   "Search for next unread newsgroup.
9568 If optional argument BACKWARD is non-nil, search backward instead."
9569   (save-excursion
9570     (set-buffer gnus-group-buffer)
9571     (if (gnus-group-search-forward
9572          backward nil (if use-level (gnus-group-group-level) nil))
9573         (gnus-group-group-name))))
9574
9575 (defun gnus-summary-best-group (&optional exclude-group)
9576   "Find the name of the best unread group.
9577 If EXCLUDE-GROUP, do not go to this group."
9578   (save-excursion
9579     (set-buffer gnus-group-buffer)
9580     (save-excursion
9581       (gnus-group-best-unread-group exclude-group))))
9582
9583 (defun gnus-summary-find-next (&optional unread article backward)
9584   (if backward (gnus-summary-find-prev)
9585     (let* ((dummy (gnus-summary-article-intangible-p))
9586            (article (or article (gnus-summary-article-number)))
9587            (arts (gnus-data-find-list article))
9588            result)
9589       (when (and (not dummy)
9590                  (or (not gnus-summary-check-current)
9591                      (not unread)
9592                      (not (gnus-data-unread-p (car arts)))))
9593         (setq arts (cdr arts)))
9594       (when (setq result
9595                   (if unread
9596                       (progn
9597                         (while arts
9598                           (when (gnus-data-unread-p (car arts))
9599                             (setq result (car arts)
9600                                   arts nil))
9601                           (setq arts (cdr arts)))
9602                         result)
9603                     (car arts)))
9604         (goto-char (gnus-data-pos result))
9605         (gnus-data-number result)))))
9606
9607 (defun gnus-summary-find-prev (&optional unread article)
9608   (let* ((eobp (eobp))
9609          (article (or article (gnus-summary-article-number)))
9610          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9611          result)
9612     (when (and (not eobp)
9613                (or (not gnus-summary-check-current)
9614                    (not unread)
9615                    (not (gnus-data-unread-p (car arts)))))
9616       (setq arts (cdr arts)))
9617     (if (setq result
9618               (if unread
9619                   (progn
9620                     (while arts
9621                       (and (gnus-data-unread-p (car arts))
9622                            (setq result (car arts)
9623                                  arts nil))
9624                       (setq arts (cdr arts)))
9625                     result)
9626                 (car arts)))
9627         (progn
9628           (goto-char (gnus-data-pos result))
9629           (gnus-data-number result)))))
9630
9631 (defun gnus-summary-find-subject (subject &optional unread backward article)
9632   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9633          (article (or article (gnus-summary-article-number)))
9634          (articles (gnus-data-list backward))
9635          (arts (gnus-data-find-list article articles))
9636          result)
9637     (when (or (not gnus-summary-check-current)
9638               (not unread)
9639               (not (gnus-data-unread-p (car arts))))
9640       (setq arts (cdr arts)))
9641     (while arts
9642       (and (or (not unread)
9643                (gnus-data-unread-p (car arts)))
9644            (vectorp (gnus-data-header (car arts)))
9645            (gnus-subject-equal
9646             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9647            (setq result (car arts)
9648                  arts nil))
9649       (setq arts (cdr arts)))
9650     (and result
9651          (goto-char (gnus-data-pos result))
9652          (gnus-data-number result))))
9653
9654 (defun gnus-summary-search-forward (&optional unread subject backward)
9655   "Search forward for an article.
9656 If UNREAD, look for unread articles.  If SUBJECT, look for
9657 articles with that subject.  If BACKWARD, search backward instead."
9658   (cond (subject (gnus-summary-find-subject subject unread backward))
9659         (backward (gnus-summary-find-prev unread))
9660         (t (gnus-summary-find-next unread))))
9661
9662 (defun gnus-recenter (&optional n)
9663   "Center point in window and redisplay frame.
9664 Also do horizontal recentering."
9665   (interactive "P")
9666   (when (and gnus-auto-center-summary
9667              (not (eq gnus-auto-center-summary 'vertical)))
9668     (gnus-horizontal-recenter))
9669   (recenter n))
9670
9671 (defun gnus-summary-recenter ()
9672   "Center point in the summary window.
9673 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9674 displayed, no centering will be performed."
9675   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9676   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9677   (let* ((top (cond ((< (window-height) 4) 0)
9678                     ((< (window-height) 7) 1)
9679                     (t 2)))
9680          (height (1- (window-height)))
9681          (bottom (save-excursion (goto-char (point-max))
9682                                  (forward-line (- height))
9683                                  (point)))
9684          (window (get-buffer-window (current-buffer))))
9685     ;; The user has to want it.
9686     (when gnus-auto-center-summary
9687       (when (get-buffer-window gnus-article-buffer)
9688        ;; Only do recentering when the article buffer is displayed,
9689        ;; Set the window start to either `bottom', which is the biggest
9690        ;; possible valid number, or the second line from the top,
9691        ;; whichever is the least.
9692        (set-window-start
9693         window (min bottom (save-excursion 
9694                              (forward-line (- top)) (point)))))
9695       ;; Do horizontal recentering while we're at it.
9696       (when (and (get-buffer-window (current-buffer) t)
9697                  (not (eq gnus-auto-center-summary 'vertical)))
9698         (let ((selected (selected-window)))
9699           (select-window (get-buffer-window (current-buffer) t))
9700           (gnus-summary-position-point)
9701           (gnus-horizontal-recenter)
9702           (select-window selected))))))
9703
9704 (defun gnus-horizontal-recenter ()
9705   "Recenter the current buffer horizontally."
9706   (if (< (current-column) (/ (window-width) 2))
9707       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9708     (let* ((orig (point))
9709            (end (window-end (get-buffer-window (current-buffer) t)))
9710            (max 0))
9711       ;; Find the longest line currently displayed in the window.
9712       (goto-char (window-start))
9713       (while (and (not (eobp)) 
9714                   (< (point) end))
9715         (end-of-line)
9716         (setq max (max max (current-column)))
9717         (forward-line 1))
9718       (goto-char orig)
9719       ;; Scroll horizontally to center (sort of) the point.
9720       (if (> max (window-width))
9721           (set-window-hscroll 
9722            (get-buffer-window (current-buffer) t)
9723            (min (- (current-column) (/ (window-width) 3))
9724                 (+ 2 (- max (window-width)))))
9725         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9726       max)))
9727
9728 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9729 (defun gnus-short-group-name (group &optional levels)
9730   "Collapse GROUP name LEVELS."
9731   (let* ((name "") 
9732          (foreign "")
9733          (depth 0) 
9734          (skip 1)
9735          (levels (or levels
9736                      (progn
9737                        (while (string-match "\\." group skip)
9738                          (setq skip (match-end 0)
9739                                depth (+ depth 1)))
9740                        depth))))
9741     (if (string-match ":" group)
9742         (setq foreign (substring group 0 (match-end 0))
9743               group (substring group (match-end 0))))
9744     (while group
9745       (if (and (string-match "\\." group)
9746                (> levels (- gnus-group-uncollapsed-levels 1)))
9747           (setq name (concat name (substring group 0 1))
9748                 group (substring group (match-end 0))
9749                 levels (- levels 1)
9750                 name (concat name "."))
9751         (setq name (concat foreign name group)
9752               group nil)))
9753     name))
9754
9755 (defun gnus-summary-jump-to-group (newsgroup)
9756   "Move point to NEWSGROUP in group mode buffer."
9757   ;; Keep update point of group mode buffer if visible.
9758   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9759       (save-window-excursion
9760         ;; Take care of tree window mode.
9761         (if (get-buffer-window gnus-group-buffer)
9762             (pop-to-buffer gnus-group-buffer))
9763         (gnus-group-jump-to-group newsgroup))
9764     (save-excursion
9765       ;; Take care of tree window mode.
9766       (if (get-buffer-window gnus-group-buffer)
9767           (pop-to-buffer gnus-group-buffer)
9768         (set-buffer gnus-group-buffer))
9769       (gnus-group-jump-to-group newsgroup))))
9770
9771 ;; This function returns a list of article numbers based on the
9772 ;; difference between the ranges of read articles in this group and
9773 ;; the range of active articles.
9774 (defun gnus-list-of-unread-articles (group)
9775   (let* ((read (gnus-info-read (gnus-get-info group)))
9776          (active (gnus-active group))
9777          (last (cdr active))
9778          first nlast unread)
9779     ;; If none are read, then all are unread.
9780     (if (not read)
9781         (setq first (car active))
9782       ;; If the range of read articles is a single range, then the
9783       ;; first unread article is the article after the last read
9784       ;; article.  Sounds logical, doesn't it?
9785       (if (not (listp (cdr read)))
9786           (setq first (1+ (cdr read)))
9787         ;; `read' is a list of ranges.
9788         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9789                                 (caar read))) 1)
9790             (setq first 1))
9791         (while read
9792           (if first
9793               (while (< first nlast)
9794                 (setq unread (cons first unread))
9795                 (setq first (1+ first))))
9796           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9797           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9798           (setq read (cdr read)))))
9799     ;; And add the last unread articles.
9800     (while (<= first last)
9801       (setq unread (cons first unread))
9802       (setq first (1+ first)))
9803     ;; Return the list of unread articles.
9804     (nreverse unread)))
9805
9806 (defun gnus-list-of-read-articles (group)
9807   "Return a list of unread, unticked and non-dormant articles."
9808   (let* ((info (gnus-get-info group))
9809          (marked (gnus-info-marks info))
9810          (active (gnus-active group)))
9811     (and info active
9812          (gnus-set-difference
9813           (gnus-sorted-complement
9814            (gnus-uncompress-range active)
9815            (gnus-list-of-unread-articles group))
9816           (append
9817            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9818            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9819
9820 ;; Various summary commands
9821
9822 (defun gnus-summary-universal-argument (arg)
9823   "Perform any operation on all articles that are process/prefixed."
9824   (interactive "P")
9825   (gnus-set-global-variables)
9826   (let ((articles (gnus-summary-work-articles arg))
9827         func article)
9828     (if (eq
9829          (setq
9830           func
9831           (key-binding
9832            (read-key-sequence
9833             (substitute-command-keys
9834              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9835              ))))
9836          'undefined)
9837         (gnus-error 1 "Undefined key")
9838       (save-excursion
9839         (while articles
9840           (gnus-summary-goto-subject (setq article (pop articles)))
9841           (command-execute func)
9842           (gnus-summary-remove-process-mark article)))))
9843   (gnus-summary-position-point))
9844
9845 (defun gnus-summary-toggle-truncation (&optional arg)
9846   "Toggle truncation of summary lines.
9847 With arg, turn line truncation on iff arg is positive."
9848   (interactive "P")
9849   (setq truncate-lines
9850         (if (null arg) (not truncate-lines)
9851           (> (prefix-numeric-value arg) 0)))
9852   (redraw-display))
9853
9854 (defun gnus-summary-reselect-current-group (&optional all rescan)
9855   "Exit and then reselect the current newsgroup.
9856 The prefix argument ALL means to select all articles."
9857   (interactive "P")
9858   (gnus-set-global-variables)
9859   (let ((current-subject (gnus-summary-article-number))
9860         (group gnus-newsgroup-name))
9861     (setq gnus-newsgroup-begin nil)
9862     (gnus-summary-exit)
9863     ;; We have to adjust the point of group mode buffer because the
9864     ;; current point was moved to the next unread newsgroup by
9865     ;; exiting.
9866     (gnus-summary-jump-to-group group)
9867     (when rescan
9868       (save-excursion
9869         (gnus-group-get-new-news-this-group 1)))
9870     (gnus-group-read-group all t)
9871     (gnus-summary-goto-subject current-subject)))
9872
9873 (defun gnus-summary-rescan-group (&optional all)
9874   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9875   (interactive "P")
9876   (gnus-summary-reselect-current-group all t))
9877
9878 (defun gnus-summary-update-info ()
9879   (let* ((group gnus-newsgroup-name))
9880     (when gnus-newsgroup-kill-headers
9881       (setq gnus-newsgroup-killed
9882             (gnus-compress-sequence
9883              (nconc
9884               (gnus-set-sorted-intersection
9885                (gnus-uncompress-range gnus-newsgroup-killed)
9886                (setq gnus-newsgroup-unselected
9887                      (sort gnus-newsgroup-unselected '<)))
9888               (setq gnus-newsgroup-unreads
9889                     (sort gnus-newsgroup-unreads '<))) t)))
9890     (unless (listp (cdr gnus-newsgroup-killed))
9891       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9892     (let ((headers gnus-newsgroup-headers))
9893       (run-hooks 'gnus-exit-group-hook)
9894       (unless gnus-save-score
9895         (setq gnus-newsgroup-scored nil))
9896       ;; Set the new ranges of read articles.
9897       (gnus-update-read-articles
9898        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9899       ;; Set the current article marks.
9900       (gnus-update-marks)
9901       ;; Do the cross-ref thing.
9902       (when gnus-use-cross-reference
9903         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9904       ;; Do adaptive scoring, and possibly save score files.
9905       (when gnus-newsgroup-adaptive
9906         (gnus-score-adaptive))
9907       (when gnus-use-scoring
9908         (gnus-score-save))
9909       ;; Do not switch windows but change the buffer to work.
9910       (set-buffer gnus-group-buffer)
9911       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9912           (gnus-group-update-group group)))))
9913
9914 (defun gnus-summary-exit (&optional temporary)
9915   "Exit reading current newsgroup, and then return to group selection mode.
9916 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9917   (interactive)
9918   (gnus-set-global-variables)
9919   (gnus-kill-save-kill-buffer)
9920   (let* ((group gnus-newsgroup-name)
9921          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9922          (mode major-mode)
9923          (buf (current-buffer)))
9924     (run-hooks 'gnus-summary-prepare-exit-hook)
9925     ;; If we have several article buffers, we kill them at exit.
9926     (unless gnus-single-article-buffer
9927       (gnus-kill-buffer gnus-original-article-buffer)
9928       (setq gnus-article-current nil))
9929     (when gnus-use-cache
9930       (gnus-cache-possibly-remove-articles)
9931       (gnus-cache-save-buffers))
9932     (when gnus-use-trees
9933       (gnus-tree-close group))
9934     ;; Make all changes in this group permanent.
9935     (unless quit-config
9936       (gnus-summary-update-info))
9937     (gnus-close-group group)
9938     ;; Make sure where I was, and go to next newsgroup.
9939     (set-buffer gnus-group-buffer)
9940     (unless quit-config
9941       (gnus-group-jump-to-group group))
9942     (run-hooks 'gnus-summary-exit-hook)
9943     (unless quit-config
9944       (gnus-group-next-unread-group 1))
9945     (if temporary
9946         nil                             ;Nothing to do.
9947       ;; If we have several article buffers, we kill them at exit.
9948       (unless gnus-single-article-buffer
9949         (gnus-kill-buffer gnus-article-buffer)
9950         (gnus-kill-buffer gnus-original-article-buffer)
9951         (setq gnus-article-current nil))
9952       (set-buffer buf)
9953       (if (not gnus-kill-summary-on-exit)
9954           (gnus-deaden-summary)
9955         ;; We set all buffer-local variables to nil.  It is unclear why
9956         ;; this is needed, but if we don't, buffer-local variables are
9957         ;; not garbage-collected, it seems.  This would the lead to en
9958         ;; ever-growing Emacs.
9959         (gnus-summary-clear-local-variables)
9960         (when (get-buffer gnus-article-buffer)
9961           (bury-buffer gnus-article-buffer))
9962         ;; We clear the global counterparts of the buffer-local
9963         ;; variables as well, just to be on the safe side.
9964         (gnus-configure-windows 'group 'force)
9965         (gnus-summary-clear-local-variables)
9966         ;; Return to group mode buffer.
9967         (if (eq mode 'gnus-summary-mode)
9968             (gnus-kill-buffer buf)))
9969       (setq gnus-current-select-method gnus-select-method)
9970       (pop-to-buffer gnus-group-buffer)
9971       ;; Clear the current group name.
9972       (if (not quit-config)
9973           (progn
9974             (gnus-group-jump-to-group group)
9975             (gnus-group-next-unread-group 1)
9976             (gnus-configure-windows 'group 'force))
9977         (if (not (buffer-name (car quit-config)))
9978             (gnus-configure-windows 'group 'force)
9979           (set-buffer (car quit-config))
9980           (and (eq major-mode 'gnus-summary-mode)
9981                (gnus-set-global-variables))
9982           (gnus-configure-windows (cdr quit-config))))
9983       (unless quit-config
9984         (setq gnus-newsgroup-name nil)))))
9985
9986 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9987 (defun gnus-summary-exit-no-update (&optional no-questions)
9988   "Quit reading current newsgroup without updating read article info."
9989   (interactive)
9990   (gnus-set-global-variables)
9991   (let* ((group gnus-newsgroup-name)
9992          (quit-config (gnus-group-quit-config group)))
9993     (when (or no-questions
9994               gnus-expert-user
9995               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9996       ;; If we have several article buffers, we kill them at exit.
9997       (unless gnus-single-article-buffer
9998         (gnus-kill-buffer gnus-article-buffer)
9999         (gnus-kill-buffer gnus-original-article-buffer)
10000         (setq gnus-article-current nil))
10001       (if (not gnus-kill-summary-on-exit)
10002           (gnus-deaden-summary)
10003         (gnus-close-group group)
10004         (gnus-summary-clear-local-variables)
10005         (set-buffer gnus-group-buffer)
10006         (gnus-summary-clear-local-variables)
10007         (when (get-buffer gnus-summary-buffer)
10008           (kill-buffer gnus-summary-buffer)))
10009       (unless gnus-single-article-buffer
10010         (setq gnus-article-current nil))
10011       (when gnus-use-trees
10012         (gnus-tree-close group))
10013       (when (get-buffer gnus-article-buffer)
10014         (bury-buffer gnus-article-buffer))
10015       ;; Return to the group buffer.
10016       (gnus-configure-windows 'group 'force)
10017       ;; Clear the current group name.
10018       (setq gnus-newsgroup-name nil)
10019       (when (equal (gnus-group-group-name) group)
10020         (gnus-group-next-unread-group 1))
10021       (when quit-config
10022         (if (not (buffer-name (car quit-config)))
10023             (gnus-configure-windows 'group 'force)
10024           (set-buffer (car quit-config))
10025           (when (eq major-mode 'gnus-summary-mode)
10026             (gnus-set-global-variables))
10027           (gnus-configure-windows (cdr quit-config)))))))
10028
10029 ;;; Dead summaries.
10030
10031 (defvar gnus-dead-summary-mode-map nil)
10032
10033 (if gnus-dead-summary-mode-map
10034     nil
10035   (setq gnus-dead-summary-mode-map (make-keymap))
10036   (suppress-keymap gnus-dead-summary-mode-map)
10037   (substitute-key-definition
10038    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10039   (let ((keys '("\C-d" "\r" "\177")))
10040     (while keys
10041       (define-key gnus-dead-summary-mode-map
10042         (pop keys) 'gnus-summary-wake-up-the-dead))))
10043
10044 (defvar gnus-dead-summary-mode nil
10045   "Minor mode for Gnus summary buffers.")
10046
10047 (defun gnus-dead-summary-mode (&optional arg)
10048   "Minor mode for Gnus summary buffers."
10049   (interactive "P")
10050   (when (eq major-mode 'gnus-summary-mode)
10051     (make-local-variable 'gnus-dead-summary-mode)
10052     (setq gnus-dead-summary-mode
10053           (if (null arg) (not gnus-dead-summary-mode)
10054             (> (prefix-numeric-value arg) 0)))
10055     (when gnus-dead-summary-mode
10056       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10057         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10058       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10059         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10060               minor-mode-map-alist)))))
10061
10062 (defun gnus-deaden-summary ()
10063   "Make the current summary buffer into a dead summary buffer."
10064   ;; Kill any previous dead summary buffer.
10065   (when (and gnus-dead-summary
10066              (buffer-name gnus-dead-summary))
10067     (save-excursion
10068       (set-buffer gnus-dead-summary)
10069       (when gnus-dead-summary-mode
10070         (kill-buffer (current-buffer)))))
10071   ;; Make this the current dead summary.
10072   (setq gnus-dead-summary (current-buffer))
10073   (gnus-dead-summary-mode 1)
10074   (let ((name (buffer-name)))
10075     (when (string-match "Summary" name)
10076       (rename-buffer
10077        (concat (substring name 0 (match-beginning 0)) "Dead "
10078                (substring name (match-beginning 0))) t))))
10079
10080 (defun gnus-kill-or-deaden-summary (buffer)
10081   "Kill or deaden the summary BUFFER."
10082   (when (and (buffer-name buffer)
10083              (not gnus-single-article-buffer))
10084     (save-excursion
10085       (set-buffer buffer)
10086       (gnus-kill-buffer gnus-article-buffer)
10087       (gnus-kill-buffer gnus-original-article-buffer)))
10088   (cond (gnus-kill-summary-on-exit
10089          (when (and gnus-use-trees
10090                     (and (get-buffer buffer)
10091                          (buffer-name (get-buffer buffer))))
10092            (save-excursion
10093              (set-buffer (get-buffer buffer))
10094              (gnus-tree-close gnus-newsgroup-name)))
10095          (gnus-kill-buffer buffer))
10096         ((and (get-buffer buffer)
10097               (buffer-name (get-buffer buffer)))
10098          (save-excursion
10099            (set-buffer buffer)
10100            (gnus-deaden-summary)))))
10101
10102 (defun gnus-summary-wake-up-the-dead (&rest args)
10103   "Wake up the dead summary buffer."
10104   (interactive)
10105   (gnus-dead-summary-mode -1)
10106   (let ((name (buffer-name)))
10107     (when (string-match "Dead " name)
10108       (rename-buffer
10109        (concat (substring name 0 (match-beginning 0))
10110                (substring name (match-end 0))) t)))
10111   (gnus-message 3 "This dead summary is now alive again"))
10112
10113 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10114 (defun gnus-summary-fetch-faq (&optional faq-dir)
10115   "Fetch the FAQ for the current group.
10116 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10117 in."
10118   (interactive
10119    (list
10120     (if current-prefix-arg
10121         (completing-read
10122          "Faq dir: " (and (listp gnus-group-faq-directory)
10123                           gnus-group-faq-directory)))))
10124   (let (gnus-faq-buffer)
10125     (and (setq gnus-faq-buffer
10126                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10127          (gnus-configure-windows 'summary-faq))))
10128
10129 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10130 (defun gnus-summary-describe-group (&optional force)
10131   "Describe the current newsgroup."
10132   (interactive "P")
10133   (gnus-group-describe-group force gnus-newsgroup-name))
10134
10135 (defun gnus-summary-describe-briefly ()
10136   "Describe summary mode commands briefly."
10137   (interactive)
10138   (gnus-message 6
10139                 (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")))
10140
10141 ;; Walking around group mode buffer from summary mode.
10142
10143 (defun gnus-summary-next-group (&optional no-article target-group backward)
10144   "Exit current newsgroup and then select next unread newsgroup.
10145 If prefix argument NO-ARTICLE is non-nil, no article is selected
10146 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10147 previous group instead."
10148   (interactive "P")
10149   (gnus-set-global-variables)
10150   (let ((current-group gnus-newsgroup-name)
10151         (current-buffer (current-buffer))
10152         entered)
10153     ;; First we semi-exit this group to update Xrefs and all variables.
10154     ;; We can't do a real exit, because the window conf must remain
10155     ;; the same in case the user is prompted for info, and we don't
10156     ;; want the window conf to change before that...
10157     (gnus-summary-exit t)
10158     (while (not entered)
10159       ;; Then we find what group we are supposed to enter.
10160       (set-buffer gnus-group-buffer)
10161       (gnus-group-jump-to-group current-group)
10162       (setq target-group
10163             (or target-group
10164                 (if (eq gnus-keep-same-level 'best)
10165                     (gnus-summary-best-group gnus-newsgroup-name)
10166                   (gnus-summary-search-group backward gnus-keep-same-level))))
10167       (if (not target-group)
10168           ;; There are no further groups, so we return to the group
10169           ;; buffer.
10170           (progn
10171             (gnus-message 5 "Returning to the group buffer")
10172             (setq entered t)
10173             (set-buffer current-buffer)
10174             (gnus-summary-exit))
10175         ;; We try to enter the target group.
10176         (gnus-group-jump-to-group target-group)
10177         (let ((unreads (gnus-group-group-unread)))
10178           (if (and (or (eq t unreads)
10179                        (and unreads (not (zerop unreads))))
10180                    (gnus-summary-read-group
10181                     target-group nil no-article current-buffer))
10182               (setq entered t)
10183             (setq current-group target-group
10184                   target-group nil)))))))
10185
10186 (defun gnus-summary-prev-group (&optional no-article)
10187   "Exit current newsgroup and then select previous unread newsgroup.
10188 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10189   (interactive "P")
10190   (gnus-summary-next-group no-article nil t))
10191
10192 ;; Walking around summary lines.
10193
10194 (defun gnus-summary-first-subject (&optional unread)
10195   "Go to the first unread subject.
10196 If UNREAD is non-nil, go to the first unread article.
10197 Returns the article selected or nil if there are no unread articles."
10198   (interactive "P")
10199   (prog1
10200       (cond
10201        ;; Empty summary.
10202        ((null gnus-newsgroup-data)
10203         (gnus-message 3 "No articles in the group")
10204         nil)
10205        ;; Pick the first article.
10206        ((not unread)
10207         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10208         (gnus-data-number (car gnus-newsgroup-data)))
10209        ;; No unread articles.
10210        ((null gnus-newsgroup-unreads)
10211         (gnus-message 3 "No more unread articles")
10212         nil)
10213        ;; Find the first unread article.
10214        (t
10215         (let ((data gnus-newsgroup-data))
10216           (while (and data
10217                       (not (gnus-data-unread-p (car data))))
10218             (setq data (cdr data)))
10219           (if data
10220               (progn
10221                 (goto-char (gnus-data-pos (car data)))
10222                 (gnus-data-number (car data)))))))
10223     (gnus-summary-position-point)))
10224
10225 (defun gnus-summary-next-subject (n &optional unread dont-display)
10226   "Go to next N'th summary line.
10227 If N is negative, go to the previous N'th subject line.
10228 If UNREAD is non-nil, only unread articles are selected.
10229 The difference between N and the actual number of steps taken is
10230 returned."
10231   (interactive "p")
10232   (let ((backward (< n 0))
10233         (n (abs n)))
10234     (while (and (> n 0)
10235                 (if backward
10236                     (gnus-summary-find-prev unread)
10237                   (gnus-summary-find-next unread)))
10238       (setq n (1- n)))
10239     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10240                                (if unread " unread" "")))
10241     (unless dont-display
10242       (gnus-summary-recenter)
10243       (gnus-summary-position-point))
10244     n))
10245
10246 (defun gnus-summary-next-unread-subject (n)
10247   "Go to next N'th unread summary line."
10248   (interactive "p")
10249   (gnus-summary-next-subject n t))
10250
10251 (defun gnus-summary-prev-subject (n &optional unread)
10252   "Go to previous N'th summary line.
10253 If optional argument UNREAD is non-nil, only unread article is selected."
10254   (interactive "p")
10255   (gnus-summary-next-subject (- n) unread))
10256
10257 (defun gnus-summary-prev-unread-subject (n)
10258   "Go to previous N'th unread summary line."
10259   (interactive "p")
10260   (gnus-summary-next-subject (- n) t))
10261
10262 (defun gnus-summary-goto-subject (article &optional force silent)
10263   "Go the subject line of ARTICLE.
10264 If FORCE, also allow jumping to articles not currently shown."
10265   (let ((b (point))
10266         (data (gnus-data-find article)))
10267     ;; We read in the article if we have to.
10268     (and (not data)
10269          force
10270          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10271          (setq data (gnus-data-find article)))
10272     (goto-char b)
10273     (if (not data)
10274         (progn
10275           (unless silent
10276             (gnus-message 3 "Can't find article %d" article))
10277           nil)
10278       (goto-char (gnus-data-pos data))
10279       article)))
10280
10281 ;; Walking around summary lines with displaying articles.
10282
10283 (defun gnus-summary-expand-window (&optional arg)
10284   "Make the summary buffer take up the entire Emacs frame.
10285 Given a prefix, will force an `article' buffer configuration."
10286   (interactive "P")
10287   (gnus-set-global-variables)
10288   (if arg
10289       (gnus-configure-windows 'article 'force)
10290     (gnus-configure-windows 'summary 'force)))
10291
10292 (defun gnus-summary-display-article (article &optional all-header)
10293   "Display ARTICLE in article buffer."
10294   (gnus-set-global-variables)
10295   (if (null article)
10296       nil
10297     (prog1
10298         (if gnus-summary-display-article-function
10299             (funcall gnus-summary-display-article-function article all-header)
10300           (gnus-article-prepare article all-header))
10301       (run-hooks 'gnus-select-article-hook)
10302       (unless (zerop gnus-current-article)
10303         (gnus-summary-goto-subject gnus-current-article))
10304       (gnus-summary-recenter)
10305       (when gnus-use-trees
10306         (gnus-possibly-generate-tree article)
10307         (gnus-highlight-selected-tree article))
10308       ;; Successfully display article.
10309       (gnus-article-set-window-start
10310        (cdr (assq article gnus-newsgroup-bookmarks))))))
10311
10312 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10313   "Select the current article.
10314 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10315 non-nil, the article will be re-fetched even if it already present in
10316 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10317 be displayed."
10318   ;; Make sure we are in the summary buffer to work around bbdb bug.
10319   (unless (eq major-mode 'gnus-summary-mode)
10320     (set-buffer gnus-summary-buffer))
10321   (let ((article (or article (gnus-summary-article-number)))
10322         (all-headers (not (not all-headers))) ;Must be T or NIL.
10323         gnus-summary-display-article-function
10324         did)
10325     (and (not pseudo)
10326          (gnus-summary-article-pseudo-p article)
10327          (error "This is a pseudo-article."))
10328     (prog1
10329         (save-excursion
10330           (set-buffer gnus-summary-buffer)
10331           (if (or (and gnus-single-article-buffer
10332                        (or (null gnus-current-article)
10333                            (null gnus-article-current)
10334                            (null (get-buffer gnus-article-buffer))
10335                            (not (eq article (cdr gnus-article-current)))
10336                            (not (equal (car gnus-article-current)
10337                                        gnus-newsgroup-name))))
10338                   (and (not gnus-single-article-buffer)
10339                        (or (null gnus-current-article)
10340                            (not (eq gnus-current-article article))))
10341                   force)
10342               ;; The requested article is different from the current article.
10343               (prog1
10344                   (gnus-summary-display-article article all-headers)
10345                 (setq did article))
10346             (if (or all-headers gnus-show-all-headers)
10347                 (gnus-article-show-all-headers))
10348             'old))
10349       (if did
10350           (gnus-article-set-window-start
10351            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10352
10353 (defun gnus-summary-set-current-mark (&optional current-mark)
10354   "Obsolete function."
10355   nil)
10356
10357 (defun gnus-summary-next-article (&optional unread subject backward push)
10358   "Select the next article.
10359 If UNREAD, only unread articles are selected.
10360 If SUBJECT, only articles with SUBJECT are selected.
10361 If BACKWARD, the previous article is selected instead of the next."
10362   (interactive "P")
10363   (gnus-set-global-variables)
10364   (cond
10365    ;; Is there such an article?
10366    ((and (gnus-summary-search-forward unread subject backward)
10367          (or (gnus-summary-display-article (gnus-summary-article-number))
10368              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10369     (gnus-summary-position-point))
10370    ;; If not, we try the first unread, if that is wanted.
10371    ((and subject
10372          gnus-auto-select-same
10373          (or (gnus-summary-first-unread-article)
10374              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10375     (gnus-summary-position-point)
10376     (gnus-message 6 "Wrapped"))
10377    ;; Try to get next/previous article not displayed in this group.
10378    ((and gnus-auto-extend-newsgroup
10379          (not unread) (not subject))
10380     (gnus-summary-goto-article
10381      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10382      nil t))
10383    ;; Go to next/previous group.
10384    (t
10385     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10386         (gnus-summary-jump-to-group gnus-newsgroup-name))
10387     (let ((cmd last-command-char)
10388           (group
10389            (if (eq gnus-keep-same-level 'best)
10390                (gnus-summary-best-group gnus-newsgroup-name)
10391              (gnus-summary-search-group backward gnus-keep-same-level))))
10392       ;; For some reason, the group window gets selected.  We change
10393       ;; it back.
10394       (select-window (get-buffer-window (current-buffer)))
10395       ;; Select next unread newsgroup automagically.
10396       (cond
10397        ((not gnus-auto-select-next)
10398         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10399        ((or (eq gnus-auto-select-next 'quietly)
10400             (and (eq gnus-auto-select-next 'slightly-quietly)
10401                  push)
10402             (and (eq gnus-auto-select-next 'almost-quietly)
10403                  (gnus-summary-last-article-p)))
10404         ;; Select quietly.
10405         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10406             (gnus-summary-exit)
10407           (gnus-message 7 "No more%s articles (%s)..."
10408                         (if unread " unread" "")
10409                         (if group (concat "selecting " group)
10410                           "exiting"))
10411           (gnus-summary-next-group nil group backward)))
10412        (t
10413         (gnus-summary-walk-group-buffer
10414          gnus-newsgroup-name cmd unread backward)))))))
10415
10416 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10417   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10418                       (?\C-p (gnus-group-prev-unread-group 1))))
10419         keve key group ended)
10420     (save-excursion
10421       (set-buffer gnus-group-buffer)
10422       (gnus-summary-jump-to-group from-group)
10423       (setq group
10424             (if (eq gnus-keep-same-level 'best)
10425                 (gnus-summary-best-group gnus-newsgroup-name)
10426               (gnus-summary-search-group backward gnus-keep-same-level))))
10427     (while (not ended)
10428       (gnus-message
10429        5 "No more%s articles%s" (if unread " unread" "")
10430        (if (and group
10431                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10432            (format " (Type %s for %s [%s])"
10433                    (single-key-description cmd) group
10434                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10435          (format " (Type %s to exit %s)"
10436                  (single-key-description cmd)
10437                  gnus-newsgroup-name)))
10438       ;; Confirm auto selection.
10439       (setq key (car (setq keve (gnus-read-event-char))))
10440       (setq ended t)
10441       (cond
10442        ((assq key keystrokes)
10443         (let ((obuf (current-buffer)))
10444           (switch-to-buffer gnus-group-buffer)
10445           (and group
10446                (gnus-group-jump-to-group group))
10447           (eval (cadr (assq key keystrokes)))
10448           (setq group (gnus-group-group-name))
10449           (switch-to-buffer obuf))
10450         (setq ended nil))
10451        ((equal key cmd)
10452         (if (or (not group)
10453                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10454             (gnus-summary-exit)
10455           (gnus-summary-next-group nil group backward)))
10456        (t
10457         (push (cdr keve) unread-command-events))))))
10458
10459 (defun gnus-read-event-char ()
10460   "Get the next event."
10461   (let ((event (read-event)))
10462     (cons (and (numberp event) event) event)))
10463
10464 (defun gnus-summary-next-unread-article ()
10465   "Select unread article after current one."
10466   (interactive)
10467   (gnus-summary-next-article t (and gnus-auto-select-same
10468                                     (gnus-summary-article-subject))))
10469
10470 (defun gnus-summary-prev-article (&optional unread subject)
10471   "Select the article after the current one.
10472 If UNREAD is non-nil, only unread articles are selected."
10473   (interactive "P")
10474   (gnus-summary-next-article unread subject t))
10475
10476 (defun gnus-summary-prev-unread-article ()
10477   "Select unred article before current one."
10478   (interactive)
10479   (gnus-summary-prev-article t (and gnus-auto-select-same
10480                                     (gnus-summary-article-subject))))
10481
10482 (defun gnus-summary-next-page (&optional lines circular)
10483   "Show next page of the selected article.
10484 If at the end of the current article, select the next article.
10485 LINES says how many lines should be scrolled up.
10486
10487 If CIRCULAR is non-nil, go to the start of the article instead of
10488 selecting the next article when reaching the end of the current
10489 article."
10490   (interactive "P")
10491   (setq gnus-summary-buffer (current-buffer))
10492   (gnus-set-global-variables)
10493   (let ((article (gnus-summary-article-number))
10494         (endp nil))
10495     (gnus-configure-windows 'article)
10496     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10497         (if (and (eq gnus-summary-goto-unread 'never)
10498                  (not (gnus-summary-last-article-p article)))
10499             (gnus-summary-next-article)
10500           (gnus-summary-next-unread-article))
10501       (if (or (null gnus-current-article)
10502               (null gnus-article-current)
10503               (/= article (cdr gnus-article-current))
10504               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10505           ;; Selected subject is different from current article's.
10506           (gnus-summary-display-article article)
10507         (gnus-eval-in-buffer-window gnus-article-buffer
10508           (setq endp (gnus-article-next-page lines)))
10509         (if endp
10510             (cond (circular
10511                    (gnus-summary-beginning-of-article))
10512                   (lines
10513                    (gnus-message 3 "End of message"))
10514                   ((null lines)
10515                    (if (and (eq gnus-summary-goto-unread 'never)
10516                             (not (gnus-summary-last-article-p article)))
10517                        (gnus-summary-next-article)
10518                      (gnus-summary-next-unread-article)))))))
10519     (gnus-summary-recenter)
10520     (gnus-summary-position-point)))
10521
10522 (defun gnus-summary-prev-page (&optional lines)
10523   "Show previous page of selected article.
10524 Argument LINES specifies lines to be scrolled down."
10525   (interactive "P")
10526   (gnus-set-global-variables)
10527   (let ((article (gnus-summary-article-number)))
10528     (gnus-configure-windows 'article)
10529     (if (or (null gnus-current-article)
10530             (null gnus-article-current)
10531             (/= article (cdr gnus-article-current))
10532             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10533         ;; Selected subject is different from current article's.
10534         (gnus-summary-display-article article)
10535       (gnus-summary-recenter)
10536       (gnus-eval-in-buffer-window gnus-article-buffer
10537         (gnus-article-prev-page lines))))
10538   (gnus-summary-position-point))
10539
10540 (defun gnus-summary-scroll-up (lines)
10541   "Scroll up (or down) one line current article.
10542 Argument LINES specifies lines to be scrolled up (or down if negative)."
10543   (interactive "p")
10544   (gnus-set-global-variables)
10545   (gnus-configure-windows 'article)
10546   (gnus-summary-show-thread)
10547   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10548     (gnus-eval-in-buffer-window gnus-article-buffer
10549       (cond ((> lines 0)
10550              (if (gnus-article-next-page lines)
10551                  (gnus-message 3 "End of message")))
10552             ((< lines 0)
10553              (gnus-article-prev-page (- lines))))))
10554   (gnus-summary-recenter)
10555   (gnus-summary-position-point))
10556
10557 (defun gnus-summary-next-same-subject ()
10558   "Select next article which has the same subject as current one."
10559   (interactive)
10560   (gnus-set-global-variables)
10561   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10562
10563 (defun gnus-summary-prev-same-subject ()
10564   "Select previous article which has the same subject as current one."
10565   (interactive)
10566   (gnus-set-global-variables)
10567   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10568
10569 (defun gnus-summary-next-unread-same-subject ()
10570   "Select next unread article which has the same subject as current one."
10571   (interactive)
10572   (gnus-set-global-variables)
10573   (gnus-summary-next-article t (gnus-summary-article-subject)))
10574
10575 (defun gnus-summary-prev-unread-same-subject ()
10576   "Select previous unread article which has the same subject as current one."
10577   (interactive)
10578   (gnus-set-global-variables)
10579   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10580
10581 (defun gnus-summary-first-unread-article ()
10582   "Select the first unread article.
10583 Return nil if there are no unread articles."
10584   (interactive)
10585   (gnus-set-global-variables)
10586   (prog1
10587       (if (gnus-summary-first-subject t)
10588           (progn
10589             (gnus-summary-show-thread)
10590             (gnus-summary-first-subject t)
10591             (gnus-summary-display-article (gnus-summary-article-number))))
10592     (gnus-summary-position-point)))
10593
10594 (defun gnus-summary-best-unread-article ()
10595   "Select the unread article with the highest score."
10596   (interactive)
10597   (gnus-set-global-variables)
10598   (let ((best -1000000)
10599         (data gnus-newsgroup-data)
10600         article score)
10601     (while data
10602       (and (gnus-data-unread-p (car data))
10603            (> (setq score
10604                     (gnus-summary-article-score (gnus-data-number (car data))))
10605               best)
10606            (setq best score
10607                  article (gnus-data-number (car data))))
10608       (setq data (cdr data)))
10609     (prog1
10610         (if article
10611             (gnus-summary-goto-article article)
10612           (error "No unread articles"))
10613       (gnus-summary-position-point))))
10614
10615 (defun gnus-summary-last-subject ()
10616   "Go to the last displayed subject line in the group."
10617   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10618     (when article
10619       (gnus-summary-goto-subject article))))
10620
10621 (defun gnus-summary-goto-article (article &optional all-headers force)
10622   "Fetch ARTICLE and display it if it exists.
10623 If ALL-HEADERS is non-nil, no header lines are hidden."
10624   (interactive
10625    (list
10626     (string-to-int
10627      (completing-read
10628       "Article number: "
10629       (mapcar (lambda (number) (list (int-to-string number)))
10630               gnus-newsgroup-limit)))
10631     current-prefix-arg
10632     t))
10633   (prog1
10634       (if (gnus-summary-goto-subject article force)
10635           (gnus-summary-display-article article all-headers)
10636         (gnus-message 4 "Couldn't go to article %s" article) nil)
10637     (gnus-summary-position-point)))
10638
10639 (defun gnus-summary-goto-last-article ()
10640   "Go to the previously read article."
10641   (interactive)
10642   (prog1
10643       (and gnus-last-article
10644            (gnus-summary-goto-article gnus-last-article))
10645     (gnus-summary-position-point)))
10646
10647 (defun gnus-summary-pop-article (number)
10648   "Pop one article off the history and go to the previous.
10649 NUMBER articles will be popped off."
10650   (interactive "p")
10651   (let (to)
10652     (setq gnus-newsgroup-history
10653           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10654     (if to
10655         (gnus-summary-goto-article (car to))
10656       (error "Article history empty")))
10657   (gnus-summary-position-point))
10658
10659 ;; Summary commands and functions for limiting the summary buffer.
10660
10661 (defun gnus-summary-limit-to-articles (n)
10662   "Limit the summary buffer to the next N articles.
10663 If not given a prefix, use the process marked articles instead."
10664   (interactive "P")
10665   (gnus-set-global-variables)
10666   (prog1
10667       (let ((articles (gnus-summary-work-articles n)))
10668         (setq gnus-newsgroup-processable nil)
10669         (gnus-summary-limit articles))
10670     (gnus-summary-position-point)))
10671
10672 (defun gnus-summary-pop-limit (&optional total)
10673   "Restore the previous limit.
10674 If given a prefix, remove all limits."
10675   (interactive "P")
10676   (gnus-set-global-variables)
10677   (when total 
10678     (setq gnus-newsgroup-limits
10679           (list (mapcar (lambda (h) (mail-header-number h))
10680                         gnus-newsgroup-headers))))
10681   (unless gnus-newsgroup-limits
10682     (error "No limit to pop"))
10683   (prog1
10684       (gnus-summary-limit nil 'pop)
10685     (gnus-summary-position-point)))
10686
10687 (defun gnus-summary-limit-to-subject (subject &optional header)
10688   "Limit the summary buffer to articles that have subjects that match a regexp."
10689   (interactive "sRegexp: ")
10690   (unless header
10691     (setq header "subject"))
10692   (when (not (equal "" subject))
10693     (prog1
10694         (let ((articles (gnus-summary-find-matching
10695                          (or header "subject") subject 'all)))
10696           (or articles (error "Found no matches for \"%s\"" subject))
10697           (gnus-summary-limit articles))
10698       (gnus-summary-position-point))))
10699
10700 (defun gnus-summary-limit-to-author (from)
10701   "Limit the summary buffer to articles that have authors that match a regexp."
10702   (interactive "sRegexp: ")
10703   (gnus-summary-limit-to-subject from "from"))
10704
10705 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10706 (make-obsolete
10707  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10708
10709 (defun gnus-summary-limit-to-unread (&optional all)
10710   "Limit the summary buffer to articles that are not marked as read.
10711 If ALL is non-nil, limit strictly to unread articles."
10712   (interactive "P")
10713   (if all
10714       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10715     (gnus-summary-limit-to-marks
10716      ;; Concat all the marks that say that an article is read and have
10717      ;; those removed.
10718      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10719            gnus-killed-mark gnus-kill-file-mark
10720            gnus-low-score-mark gnus-expirable-mark
10721            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10722      'reverse)))
10723
10724 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10725 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10726
10727 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10728   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10729 If REVERSE, limit the summary buffer to articles that are not marked
10730 with MARKS.  MARKS can either be a string of marks or a list of marks.
10731 Returns how many articles were removed."
10732   (interactive "sMarks: ")
10733   (gnus-set-global-variables)
10734   (prog1
10735       (let ((data gnus-newsgroup-data)
10736             (marks (if (listp marks) marks
10737                      (append marks nil))) ; Transform to list.
10738             articles)
10739         (while data
10740           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10741                  (memq (gnus-data-mark (car data)) marks))
10742                (setq articles (cons (gnus-data-number (car data)) articles)))
10743           (setq data (cdr data)))
10744         (gnus-summary-limit articles))
10745     (gnus-summary-position-point)))
10746
10747 (defun gnus-summary-limit-to-score (&optional score)
10748   "Limit to articles with score at or above SCORE."
10749   (interactive "P")
10750   (gnus-set-global-variables)
10751   (setq score (if score
10752                   (prefix-numeric-value score)
10753                 (or gnus-summary-default-score 0)))
10754   (let ((data gnus-newsgroup-data)
10755         articles)
10756     (while data
10757       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10758                 score)
10759         (push (gnus-data-number (car data)) articles))
10760       (setq data (cdr data)))
10761     (prog1
10762         (gnus-summary-limit articles)
10763       (gnus-summary-position-point))))
10764
10765 (defun gnus-summary-limit-include-dormant ()
10766   "Display all the hidden articles that are marked as dormant."
10767   (interactive)
10768   (gnus-set-global-variables)
10769   (or gnus-newsgroup-dormant
10770       (error "There are no dormant articles in this group"))
10771   (prog1
10772       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10773     (gnus-summary-position-point)))
10774
10775 (defun gnus-summary-limit-exclude-dormant ()
10776   "Hide all dormant articles."
10777   (interactive)
10778   (gnus-set-global-variables)
10779   (prog1
10780       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10781     (gnus-summary-position-point)))
10782
10783 (defun gnus-summary-limit-exclude-childless-dormant ()
10784   "Hide all dormant articles that have no children."
10785   (interactive)
10786   (gnus-set-global-variables)
10787   (let ((data (gnus-data-list t))
10788         articles d children)
10789     ;; Find all articles that are either not dormant or have
10790     ;; children.
10791     (while (setq d (pop data))
10792       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10793                 (and (setq children 
10794                            (gnus-article-children (gnus-data-number d)))
10795                      (let (found)
10796                        (while children
10797                          (when (memq (car children) articles)
10798                            (setq children nil
10799                                  found t))
10800                          (pop children))
10801                        found)))
10802         (push (gnus-data-number d) articles)))
10803     ;; Do the limiting.
10804     (prog1
10805         (gnus-summary-limit articles)
10806       (gnus-summary-position-point))))
10807
10808 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10809   "Mark all unread excluded articles as read.
10810 If ALL, mark even excluded ticked and dormants as read."
10811   (interactive "P")
10812   (let ((articles (gnus-sorted-complement
10813                    (sort
10814                     (mapcar (lambda (h) (mail-header-number h))
10815                             gnus-newsgroup-headers)
10816                     '<)
10817                    (sort gnus-newsgroup-limit '<)))
10818         article)
10819     (setq gnus-newsgroup-unreads nil)
10820     (if all
10821         (setq gnus-newsgroup-dormant nil
10822               gnus-newsgroup-marked nil
10823               gnus-newsgroup-reads
10824               (nconc
10825                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10826                gnus-newsgroup-reads))
10827       (while (setq article (pop articles))
10828         (unless (or (memq article gnus-newsgroup-dormant)
10829                     (memq article gnus-newsgroup-marked))
10830           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10831
10832 (defun gnus-summary-limit (articles &optional pop)
10833   (if pop
10834       ;; We pop the previous limit off the stack and use that.
10835       (setq articles (car gnus-newsgroup-limits)
10836             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10837     ;; We use the new limit, so we push the old limit on the stack.
10838     (setq gnus-newsgroup-limits
10839           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10840   ;; Set the limit.
10841   (setq gnus-newsgroup-limit articles)
10842   (let ((total (length gnus-newsgroup-data))
10843         (data (gnus-data-find-list (gnus-summary-article-number)))
10844         found)
10845     ;; This will do all the work of generating the new summary buffer
10846     ;; according to the new limit.
10847     (gnus-summary-prepare)
10848     ;; Hide any threads, possibly.
10849     (and gnus-show-threads
10850          gnus-thread-hide-subtree
10851          (gnus-summary-hide-all-threads))
10852     ;; Try to return to the article you were at, or one in the
10853     ;; neighborhood.
10854     (if data
10855         ;; We try to find some article after the current one.
10856         (while data
10857           (and (gnus-summary-goto-subject
10858                 (gnus-data-number (car data)) nil t)
10859                (setq data nil
10860                      found t))
10861           (setq data (cdr data))))
10862     (or found
10863         ;; If there is no data, that means that we were after the last
10864         ;; article.  The same goes when we can't find any articles
10865         ;; after the current one.
10866         (progn
10867           (goto-char (point-max))
10868           (gnus-summary-find-prev)))
10869     ;; We return how many articles were removed from the summary
10870     ;; buffer as a result of the new limit.
10871     (- total (length gnus-newsgroup-data))))
10872
10873 (defsubst gnus-invisible-cut-children (threads)
10874   (let ((num 0))
10875     (while threads
10876       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10877         (incf num))
10878       (pop threads))
10879     (< num 2)))
10880
10881 (defsubst gnus-cut-thread (thread)
10882   "Go forwards in the thread until we find an article that we want to display."
10883   (when (or (eq gnus-fetch-old-headers 'some)
10884             (eq gnus-build-sparse-threads 'some)
10885             (eq gnus-build-sparse-threads 'more))
10886     ;; Deal with old-fetched headers and sparse threads.
10887     (while (and
10888             thread
10889             (or
10890              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10891              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10892             (or (<= (length (cdr thread)) 1)
10893                 (gnus-invisible-cut-children (cdr thread))))
10894       (setq thread (cadr thread))))
10895   thread)
10896
10897 (defun gnus-cut-threads (threads)
10898   "Cut off all uninteresting articles from the beginning of threads."
10899   (when (or (eq gnus-fetch-old-headers 'some)
10900             (eq gnus-build-sparse-threads 'some)
10901             (eq gnus-build-sparse-threads 'more))
10902     (let ((th threads))
10903       (while th
10904         (setcar th (gnus-cut-thread (car th)))
10905         (setq th (cdr th)))))
10906   ;; Remove nixed out threads.
10907   (delq nil threads))
10908
10909 (defun gnus-summary-initial-limit (&optional show-if-empty)
10910   "Figure out what the initial limit is supposed to be on group entry.
10911 This entails weeding out unwanted dormants, low-scored articles,
10912 fetch-old-headers verbiage, and so on."
10913   ;; Most groups have nothing to remove.
10914   (if (or gnus-inhibit-limiting
10915           (and (null gnus-newsgroup-dormant)
10916                (not (eq gnus-fetch-old-headers 'some))
10917                (null gnus-summary-expunge-below)
10918                (not (eq gnus-build-sparse-threads 'some))
10919                (not (eq gnus-build-sparse-threads 'more))
10920                (null gnus-thread-expunge-below)
10921                (not gnus-use-nocem)))
10922       () ; Do nothing.
10923     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10924     (setq gnus-newsgroup-limit nil)
10925     (mapatoms
10926      (lambda (node)
10927        (unless (car (symbol-value node))
10928          ;; These threads have no parents -- they are roots.
10929          (let ((nodes (cdr (symbol-value node)))
10930                thread)
10931            (while nodes
10932              (if (and gnus-thread-expunge-below
10933                       (< (gnus-thread-total-score (car nodes))
10934                          gnus-thread-expunge-below))
10935                  (gnus-expunge-thread (pop nodes))
10936                (setq thread (pop nodes))
10937                (gnus-summary-limit-children thread))))))
10938      gnus-newsgroup-dependencies)
10939     ;; If this limitation resulted in an empty group, we might
10940     ;; pop the previous limit and use it instead.
10941     (when (and (not gnus-newsgroup-limit)
10942                show-if-empty)
10943       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10944     gnus-newsgroup-limit))
10945
10946 (defun gnus-summary-limit-children (thread)
10947   "Return 1 if this subthread is visible and 0 if it is not."
10948   ;; First we get the number of visible children to this thread.  This
10949   ;; is done by recursing down the thread using this function, so this
10950   ;; will really go down to a leaf article first, before slowly
10951   ;; working its way up towards the root.
10952   (when thread
10953     (let ((children
10954            (if (cdr thread)
10955                (apply '+ (mapcar 'gnus-summary-limit-children
10956                                  (cdr thread)))
10957              0))
10958           (number (mail-header-number (car thread)))
10959           score)
10960       (if (or
10961            ;; If this article is dormant and has absolutely no visible
10962            ;; children, then this article isn't visible.
10963            (and (memq number gnus-newsgroup-dormant)
10964                 (= children 0))
10965            ;; If this is "fetch-old-headered" and there is only one
10966            ;; visible child (or less), then we don't want this article.
10967            (and (eq gnus-fetch-old-headers 'some)
10968                 (memq number gnus-newsgroup-ancient)
10969                 (zerop children))
10970            ;; If this is a sparsely inserted article with no children,
10971            ;; we don't want it.
10972            (and (eq gnus-build-sparse-threads 'some)
10973                 (memq number gnus-newsgroup-sparse)
10974                 (zerop children))
10975            ;; If we use expunging, and this article is really
10976            ;; low-scored, then we don't want this article.
10977            (when (and gnus-summary-expunge-below
10978                       (< (setq score
10979                                (or (cdr (assq number gnus-newsgroup-scored))
10980                                    gnus-summary-default-score))
10981                          gnus-summary-expunge-below))
10982              ;; We increase the expunge-tally here, but that has
10983              ;; nothing to do with the limits, really.
10984              (incf gnus-newsgroup-expunged-tally)
10985              ;; We also mark as read here, if that's wanted.
10986              (when (and gnus-summary-mark-below
10987                         (< score gnus-summary-mark-below))
10988                (setq gnus-newsgroup-unreads
10989                      (delq number gnus-newsgroup-unreads))
10990                (if gnus-newsgroup-auto-expire
10991                    (push number gnus-newsgroup-expirable)
10992                  (push (cons number gnus-low-score-mark)
10993                        gnus-newsgroup-reads)))
10994              t)
10995            (and gnus-use-nocem
10996                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10997           ;; Nope, invisible article.
10998           0
10999         ;; Ok, this article is to be visible, so we add it to the limit
11000         ;; and return 1.
11001         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11002         1))))
11003
11004 (defun gnus-expunge-thread (thread)
11005   "Mark all articles in THREAD as read."
11006   (let* ((number (mail-header-number (car thread))))
11007     (incf gnus-newsgroup-expunged-tally)
11008     ;; We also mark as read here, if that's wanted.
11009     (setq gnus-newsgroup-unreads
11010           (delq number gnus-newsgroup-unreads))
11011     (if gnus-newsgroup-auto-expire
11012         (push number gnus-newsgroup-expirable)
11013       (push (cons number gnus-low-score-mark)
11014             gnus-newsgroup-reads)))
11015   ;; Go recursively through all subthreads.
11016   (mapcar 'gnus-expunge-thread (cdr thread)))
11017
11018 ;; Summary article oriented commands
11019
11020 (defun gnus-summary-refer-parent-article (n)
11021   "Refer parent article N times.
11022 The difference between N and the number of articles fetched is returned."
11023   (interactive "p")
11024   (gnus-set-global-variables)
11025   (while
11026       (and
11027        (> n 0)
11028        (let* ((header (gnus-summary-article-header))
11029               (ref
11030                ;; If we try to find the parent of the currently
11031                ;; displayed article, then we take a look at the actual
11032                ;; References header, since this is slightly more
11033                ;; reliable than the References field we got from the
11034                ;; server.
11035                (if (and (eq (mail-header-number header)
11036                             (cdr gnus-article-current))
11037                         (equal gnus-newsgroup-name
11038                                (car gnus-article-current)))
11039                    (save-excursion
11040                      (set-buffer gnus-original-article-buffer)
11041                      (nnheader-narrow-to-headers)
11042                      (prog1
11043                          (message-fetch-field "references")
11044                        (widen)))
11045                  ;; It's not the current article, so we take a bet on
11046                  ;; the value we got from the server.
11047                  (mail-header-references header))))
11048          (if (setq ref (or ref (mail-header-references header)))
11049              (or (gnus-summary-refer-article (gnus-parent-id ref))
11050                  (gnus-message 1 "Couldn't find parent"))
11051            (gnus-message 1 "No references in article %d"
11052                          (gnus-summary-article-number))
11053            nil)))
11054     (setq n (1- n)))
11055   (gnus-summary-position-point)
11056   n)
11057
11058 (defun gnus-summary-refer-references ()
11059   "Fetch all articles mentioned in the References header.
11060 Return how many articles were fetched."
11061   (interactive)
11062   (gnus-set-global-variables)
11063   (let ((ref (mail-header-references (gnus-summary-article-header)))
11064         (current (gnus-summary-article-number))
11065         (n 0))
11066     ;; For each Message-ID in the References header...
11067     (while (string-match "<[^>]*>" ref)
11068       (incf n)
11069       ;; ... fetch that article.
11070       (gnus-summary-refer-article
11071        (prog1 (match-string 0 ref)
11072          (setq ref (substring ref (match-end 0))))))
11073     (gnus-summary-goto-subject current)
11074     (gnus-summary-position-point)
11075     n))
11076
11077 (defun gnus-summary-refer-article (message-id)
11078   "Fetch an article specified by MESSAGE-ID."
11079   (interactive "sMessage-ID: ")
11080   (when (and (stringp message-id)
11081              (not (zerop (length message-id))))
11082     ;; Construct the correct Message-ID if necessary.
11083     ;; Suggested by tale@pawl.rpi.edu.
11084     (unless (string-match "^<" message-id)
11085       (setq message-id (concat "<" message-id)))
11086     (unless (string-match ">$" message-id)
11087       (setq message-id (concat message-id ">")))
11088     (let* ((header (gnus-id-to-header message-id))
11089            (sparse (and header
11090                         (memq (mail-header-number header)
11091                               gnus-newsgroup-sparse))))
11092       (if header
11093           (prog1
11094               ;; The article is present in the buffer, to we just go to it.
11095               (gnus-summary-goto-article 
11096                (mail-header-number header) nil header)
11097             (when sparse
11098               (gnus-summary-update-article (mail-header-number header))))
11099         ;; We fetch the article
11100         (let ((gnus-override-method 
11101                (and (gnus-news-group-p gnus-newsgroup-name)
11102                     gnus-refer-article-method))
11103               number)
11104           ;; Start the special refer-article method, if necessary.
11105           (when (and gnus-refer-article-method
11106                      (gnus-news-group-p gnus-newsgroup-name))
11107             (gnus-check-server gnus-refer-article-method))
11108           ;; Fetch the header, and display the article.
11109           (if (setq number (gnus-summary-insert-subject message-id))
11110               (gnus-summary-select-article nil nil nil number)
11111             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11112
11113 (defun gnus-summary-enter-digest-group (&optional force)
11114   "Enter a digest group based on the current article."
11115   (interactive "P")
11116   (gnus-set-global-variables)
11117   (gnus-summary-select-article)
11118   (let ((name (format "%s-%d"
11119                       (gnus-group-prefixed-name
11120                        gnus-newsgroup-name (list 'nndoc ""))
11121                       gnus-current-article))
11122         (ogroup gnus-newsgroup-name)
11123         (case-fold-search t)
11124         (buf (current-buffer))
11125         dig)
11126     (save-excursion
11127       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11128       (insert-buffer-substring gnus-original-article-buffer)
11129       (narrow-to-region
11130        (goto-char (point-min))
11131        (or (search-forward "\n\n" nil t) (point)))
11132       (goto-char (point-min))
11133       (delete-matching-lines "^\\(Path\\):\\|^From ")
11134       (widen))
11135     (unwind-protect
11136         (if (gnus-group-read-ephemeral-group
11137              name `(nndoc ,name (nndoc-address
11138                                  ,(get-buffer dig))
11139                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11140             ;; Make all postings to this group go to the parent group.
11141             (nconc (gnus-info-params (gnus-get-info name))
11142                    (list (cons 'to-group ogroup)))
11143           ;; Couldn't select this doc group.
11144           (switch-to-buffer buf)
11145           (gnus-set-global-variables)
11146           (gnus-configure-windows 'summary)
11147           (gnus-message 3 "Article couldn't be entered?"))
11148       (kill-buffer dig))))
11149
11150 (defun gnus-summary-isearch-article (&optional regexp-p)
11151   "Do incremental search forward on the current article.
11152 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11153   (interactive "P")
11154   (gnus-set-global-variables)
11155   (gnus-summary-select-article)
11156   (gnus-configure-windows 'article)
11157   (gnus-eval-in-buffer-window gnus-article-buffer
11158     (goto-char (point-min))
11159     (isearch-forward regexp-p)))
11160
11161 (defun gnus-summary-search-article-forward (regexp &optional backward)
11162   "Search for an article containing REGEXP forward.
11163 If BACKWARD, search backward instead."
11164   (interactive
11165    (list (read-string
11166           (format "Search article %s (regexp%s): "
11167                   (if current-prefix-arg "backward" "forward")
11168                   (if gnus-last-search-regexp
11169                       (concat ", default " gnus-last-search-regexp)
11170                     "")))
11171          current-prefix-arg))
11172   (gnus-set-global-variables)
11173   (if (string-equal regexp "")
11174       (setq regexp (or gnus-last-search-regexp ""))
11175     (setq gnus-last-search-regexp regexp))
11176   (unless (gnus-summary-search-article regexp backward)
11177     (error "Search failed: \"%s\"" regexp)))
11178
11179 (defun gnus-summary-search-article-backward (regexp)
11180   "Search for an article containing REGEXP backward."
11181   (interactive
11182    (list (read-string
11183           (format "Search article backward (regexp%s): "
11184                   (if gnus-last-search-regexp
11185                       (concat ", default " gnus-last-search-regexp)
11186                     "")))))
11187   (gnus-summary-search-article-forward regexp 'backward))
11188
11189 (defun gnus-summary-search-article (regexp &optional backward)
11190   "Search for an article containing REGEXP.
11191 Optional argument BACKWARD means do search for backward.
11192 `gnus-select-article-hook' is not called during the search."
11193   (let ((gnus-select-article-hook nil)  ;Disable hook.
11194         (gnus-article-display-hook nil)
11195         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11196         (re-search
11197          (if backward
11198              're-search-backward 're-search-forward))
11199         (sum (current-buffer))
11200         (found nil))
11201     (gnus-save-hidden-threads
11202       (gnus-summary-select-article)
11203       (set-buffer gnus-article-buffer)
11204       (while (not found)
11205         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11206         (if (if backward
11207                 (re-search-backward regexp nil t)
11208               (re-search-forward regexp nil t))
11209             ;; We found the regexp.
11210             (progn
11211               (setq found 'found)
11212               (beginning-of-line)
11213               (set-window-start
11214                (get-buffer-window (current-buffer))
11215                (point))
11216               (forward-line 1)
11217               (set-buffer sum))
11218           ;; We didn't find it, so we go to the next article.
11219           (set-buffer sum)
11220           (if (not (if backward (gnus-summary-find-prev)
11221                      (gnus-summary-find-next)))
11222               ;; No more articles.
11223               (setq found t)
11224             ;; Select the next article and adjust point.
11225             (gnus-summary-select-article)
11226             (set-buffer gnus-article-buffer)
11227             (widen)
11228             (goto-char (if backward (point-max) (point-min))))))
11229       (gnus-message 7 ""))
11230     ;; Return whether we found the regexp.
11231     (when (eq found 'found)
11232       (gnus-summary-show-thread)
11233       (gnus-summary-goto-subject gnus-current-article)
11234       (gnus-summary-position-point)
11235       t)))
11236
11237 (defun gnus-summary-find-matching (header regexp &optional backward unread
11238                                           not-case-fold)
11239   "Return a list of all articles that match REGEXP on HEADER.
11240 The search stars on the current article and goes forwards unless
11241 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11242 If UNREAD is non-nil, only unread articles will
11243 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11244 in the comparisons."
11245   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11246                 (gnus-data-find-list
11247                  (gnus-summary-article-number) (gnus-data-list backward))))
11248         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11249         (case-fold-search (not not-case-fold))
11250         articles d)
11251     (or (fboundp (intern (concat "mail-header-" header)))
11252         (error "%s is not a valid header" header))
11253     (while data
11254       (setq d (car data))
11255       (and (or (not unread)             ; We want all articles...
11256                (gnus-data-unread-p d))  ; Or just unreads.
11257            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11258            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11259            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11260       (setq data (cdr data)))
11261     (nreverse articles)))
11262
11263 (defun gnus-summary-execute-command (header regexp command &optional backward)
11264   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11265 If HEADER is an empty string (or nil), the match is done on the entire
11266 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11267   (interactive
11268    (list (let ((completion-ignore-case t))
11269            (completing-read
11270             "Header name: "
11271             (mapcar (lambda (string) (list string))
11272                     '("Number" "Subject" "From" "Lines" "Date"
11273                       "Message-ID" "Xref" "References" "Body"))
11274             nil 'require-match))
11275          (read-string "Regexp: ")
11276          (read-key-sequence "Command: ")
11277          current-prefix-arg))
11278   (when (equal header "Body")
11279     (setq header ""))
11280   (gnus-set-global-variables)
11281   ;; Hidden thread subtrees must be searched as well.
11282   (gnus-summary-show-all-threads)
11283   ;; We don't want to change current point nor window configuration.
11284   (save-excursion
11285     (save-window-excursion
11286       (gnus-message 6 "Executing %s..." (key-description command))
11287       ;; We'd like to execute COMMAND interactively so as to give arguments.
11288       (gnus-execute header regexp
11289                     `(lambda () (call-interactively ',(key-binding command)))
11290                     backward)
11291       (gnus-message 6 "Executing %s...done" (key-description command)))))
11292
11293 (defun gnus-summary-beginning-of-article ()
11294   "Scroll the article back to the beginning."
11295   (interactive)
11296   (gnus-set-global-variables)
11297   (gnus-summary-select-article)
11298   (gnus-configure-windows 'article)
11299   (gnus-eval-in-buffer-window 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 gnus-article-buffer
11311     (widen)
11312     (goto-char (point-max))
11313     (recenter -3)
11314     (and gnus-break-pages (gnus-narrow-to-page))))
11315
11316 (defun gnus-summary-show-article (&optional arg)
11317   "Force re-fetching of the current article.
11318 If ARG (the prefix) is non-nil, show the raw article without any
11319 article massaging functions being run."
11320   (interactive "P")
11321   (gnus-set-global-variables)
11322   (if (not arg)
11323       ;; Select the article the normal way.
11324       (gnus-summary-select-article nil 'force)
11325     ;; Bind the article treatment functions to nil.
11326     (let ((gnus-have-all-headers t)
11327           gnus-article-display-hook
11328           gnus-article-prepare-hook
11329           gnus-break-pages
11330           gnus-visual)
11331       (gnus-summary-select-article nil 'force)))
11332   (gnus-summary-goto-subject gnus-current-article)
11333 ;  (gnus-configure-windows 'article)
11334   (gnus-summary-position-point))
11335
11336 (defun gnus-summary-verbose-headers (&optional arg)
11337   "Toggle permanent full header display.
11338 If ARG is a positive number, turn header display on.
11339 If ARG is a negative number, turn header display off."
11340   (interactive "P")
11341   (gnus-set-global-variables)
11342   (gnus-summary-toggle-header arg)
11343   (setq gnus-show-all-headers
11344         (cond ((or (not (numberp arg))
11345                    (zerop arg))
11346                (not gnus-show-all-headers))
11347               ((natnump arg)
11348                t))))
11349
11350 (defun gnus-summary-toggle-header (&optional arg)
11351   "Show the headers if they are hidden, or hide them if they are shown.
11352 If ARG is a positive number, show the entire header.
11353 If ARG is a negative number, hide the unwanted header lines."
11354   (interactive "P")
11355   (gnus-set-global-variables)
11356   (save-excursion
11357     (set-buffer gnus-article-buffer)
11358     (let* ((buffer-read-only nil)
11359            (inhibit-point-motion-hooks t)
11360            (hidden (text-property-any
11361                     (goto-char (point-min)) (search-forward "\n\n")
11362                     'invisible t))
11363            e)
11364       (goto-char (point-min))
11365       (when (search-forward "\n\n" nil t)
11366         (delete-region (point-min) (1- (point))))
11367       (goto-char (point-min))
11368       (save-excursion
11369         (set-buffer gnus-original-article-buffer)
11370         (goto-char (point-min))
11371         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11372       (insert-buffer-substring gnus-original-article-buffer 1 e)
11373       (let ((gnus-inhibit-hiding t))
11374         (run-hooks 'gnus-article-display-hook))
11375       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11376           (gnus-article-hide-headers)))))
11377
11378 (defun gnus-summary-show-all-headers ()
11379   "Make all header lines visible."
11380   (interactive)
11381   (gnus-set-global-variables)
11382   (gnus-article-show-all-headers))
11383
11384 (defun gnus-summary-toggle-mime (&optional arg)
11385   "Toggle MIME processing.
11386 If ARG is a positive number, turn MIME processing on."
11387   (interactive "P")
11388   (gnus-set-global-variables)
11389   (setq gnus-show-mime
11390         (if (null arg) (not gnus-show-mime)
11391           (> (prefix-numeric-value arg) 0)))
11392   (gnus-summary-select-article t 'force))
11393
11394 (defun gnus-summary-caesar-message (&optional arg)
11395   "Caesar rotate the current article by 13.
11396 The numerical prefix specifies how manu places to rotate each letter
11397 forward."
11398   (interactive "P")
11399   (gnus-set-global-variables)
11400   (gnus-summary-select-article)
11401   (let ((mail-header-separator ""))
11402     (gnus-eval-in-buffer-window gnus-article-buffer
11403       (save-restriction
11404         (widen)
11405         (let ((start (window-start))
11406               buffer-read-only)
11407           (message-caesar-buffer-body arg)
11408           (set-window-start (get-buffer-window (current-buffer)) start))))))
11409
11410 (defun gnus-summary-stop-page-breaking ()
11411   "Stop page breaking in the current article."
11412   (interactive)
11413   (gnus-set-global-variables)
11414   (gnus-summary-select-article)
11415   (gnus-eval-in-buffer-window gnus-article-buffer
11416     (widen)))
11417
11418 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11419   "Move the current article to a different newsgroup.
11420 If N is a positive number, move the N next articles.
11421 If N is a negative number, move the N previous articles.
11422 If N is nil and any articles have been marked with the process mark,
11423 move those articles instead.
11424 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11425 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11426 re-spool using this method.
11427
11428 For this function to work, both the current newsgroup and the
11429 newsgroup that you want to move to have to support the `request-move'
11430 and `request-accept' functions."
11431   (interactive "P")
11432   (unless action (setq action 'move))
11433   (gnus-set-global-variables)
11434   ;; Check whether the source group supports the required functions.
11435   (cond ((and (eq action 'move)
11436               (not (gnus-check-backend-function
11437                     'request-move-article gnus-newsgroup-name)))
11438          (error "The current group does not support article moving"))
11439         ((and (eq action 'crosspost)
11440               (not (gnus-check-backend-function
11441                     'request-replace-article gnus-newsgroup-name)))
11442          (error "The current group does not support article editing")))
11443   (let ((articles (gnus-summary-work-articles n))
11444         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11445         (names '((move "Move" "Moving")
11446                  (copy "Copy" "Copying")
11447                  (crosspost "Crosspost" "Crossposting")))
11448         (copy-buf (save-excursion
11449                     (nnheader-set-temp-buffer " *copy article*")))
11450         art-group to-method new-xref article to-groups)
11451     (unless (assq action names)
11452       (error "Unknown action %s" action))
11453     ;; Read the newsgroup name.
11454     (when (and (not to-newsgroup)
11455                (not select-method))
11456       (setq to-newsgroup
11457             (gnus-read-move-group-name
11458              (cadr (assq action names))
11459              (symbol-value (intern (format "gnus-current-%s-group" action)))
11460              articles prefix))
11461       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11462     (setq to-method (or select-method 
11463                         (gnus-find-method-for-group to-newsgroup)))
11464     ;; Check the method we are to move this article to...
11465     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11466         (error "%s does not support article copying" (car to-method)))
11467     (or (gnus-check-server to-method)
11468         (error "Can't open server %s" (car to-method)))
11469     (gnus-message 6 "%s to %s: %s..."
11470                   (caddr (assq action names))
11471                   (or (car select-method) to-newsgroup) articles)
11472     (while articles
11473       (setq article (pop articles))
11474       (setq
11475        art-group
11476        (cond
11477         ;; Move the article.
11478         ((eq action 'move)
11479          (gnus-request-move-article
11480           article                       ; Article to move
11481           gnus-newsgroup-name           ; From newsgrouo
11482           (nth 1 (gnus-find-method-for-group
11483                   gnus-newsgroup-name)) ; Server
11484           (list 'gnus-request-accept-article
11485                 to-newsgroup (list 'quote select-method)
11486                 (not articles))         ; Accept form
11487           (not articles)))              ; Only save nov last time
11488         ;; Copy the article.
11489         ((eq action 'copy)
11490          (save-excursion
11491            (set-buffer copy-buf)
11492            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11493            (gnus-request-accept-article
11494             to-newsgroup select-method (not articles))))
11495         ;; Crosspost the article.
11496         ((eq action 'crosspost)
11497          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11498            (setq new-xref (concat gnus-newsgroup-name ":" article))
11499            (if (and xref (not (string= xref "")))
11500                (progn
11501                  (when (string-match "^Xref: " xref)
11502                    (setq xref (substring xref (match-end 0))))
11503                  (setq new-xref (concat xref " " new-xref)))
11504              (setq new-xref (concat (system-name) " " new-xref)))
11505            (save-excursion
11506              (set-buffer copy-buf)
11507              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11508              (nnheader-replace-header "xref" new-xref)
11509              (gnus-request-accept-article
11510               to-newsgroup select-method (not articles)))))))
11511       (if (not art-group)
11512           (gnus-message 1 "Couldn't %s article %s"
11513                         (cadr (assq action names)) article)
11514         (let* ((entry
11515                 (or
11516                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11517                  (gnus-gethash
11518                   (gnus-group-prefixed-name
11519                    (car art-group)
11520                    (or select-method 
11521                        (gnus-find-method-for-group to-newsgroup)))
11522                   gnus-newsrc-hashtb)))
11523                (info (nth 2 entry))
11524                (to-group (gnus-info-group info)))
11525           ;; Update the group that has been moved to.
11526           (when (and info
11527                      (memq action '(move copy)))
11528             (unless (member to-group to-groups)
11529               (push to-group to-groups))
11530
11531             (unless (memq article gnus-newsgroup-unreads)
11532               (gnus-info-set-read
11533                info (gnus-add-to-range (gnus-info-read info)
11534                                        (list (cdr art-group)))))
11535
11536             ;; Copy any marks over to the new group.
11537             (let ((marks gnus-article-mark-lists)
11538                   (to-article (cdr art-group)))
11539
11540               ;; See whether the article is to be put in the cache.
11541               (when gnus-use-cache
11542                 (gnus-cache-possibly-enter-article
11543                  to-group to-article
11544                  (let ((header (copy-sequence
11545                                 (gnus-summary-article-header article))))
11546                    (mail-header-set-number header to-article)
11547                    header)
11548                  (memq article gnus-newsgroup-marked)
11549                  (memq article gnus-newsgroup-dormant)
11550                  (memq article gnus-newsgroup-unreads)))
11551
11552               (while marks
11553                 (when (memq article (symbol-value
11554                                      (intern (format "gnus-newsgroup-%s"
11555                                                      (caar marks)))))
11556                   ;; If the other group is the same as this group,
11557                   ;; then we have to add the mark to the list.
11558                   (when (equal to-group gnus-newsgroup-name)
11559                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11560                          (cons to-article
11561                                (symbol-value
11562                                 (intern (format "gnus-newsgroup-%s"
11563                                                 (caar marks)))))))
11564                   ;; Copy mark to other group.
11565                   (gnus-add-marked-articles
11566                    to-group (cdar marks) (list to-article) info))
11567                 (setq marks (cdr marks)))))
11568
11569           ;; Update the Xref header in this article to point to
11570           ;; the new crossposted article we have just created.
11571           (when (eq action 'crosspost)
11572             (save-excursion
11573               (set-buffer copy-buf)
11574               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11575               (nnheader-replace-header
11576                "xref" (concat new-xref " " (gnus-group-prefixed-name
11577                                             (car art-group) to-method)
11578                               ":" (cdr art-group)))
11579               (gnus-request-replace-article
11580                article gnus-newsgroup-name (current-buffer)))))
11581
11582         (gnus-summary-goto-subject article)
11583         (when (eq action 'move)
11584           (gnus-summary-mark-article article gnus-canceled-mark)))
11585       (gnus-summary-remove-process-mark article))
11586     ;; Re-activate all groups that have been moved to.
11587     (while to-groups
11588       (gnus-activate-group (pop to-groups)))
11589     
11590     (gnus-kill-buffer copy-buf)
11591     (gnus-summary-position-point)
11592     (gnus-set-mode-line 'summary)))
11593
11594 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11595   "Move the current article to a different newsgroup.
11596 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11597 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11598 re-spool using this method."
11599   (interactive "P")
11600   (gnus-summary-move-article n nil select-method 'copy))
11601
11602 (defun gnus-summary-crosspost-article (&optional n)
11603   "Crosspost the current article to some other group."
11604   (interactive "P")
11605   (gnus-summary-move-article n nil nil 'crosspost))
11606
11607 (defvar gnus-summary-respool-default-method nil
11608   "Default method for respooling an article.  
11609 If nil, use to the current newsgroup method.")
11610
11611 (defun gnus-summary-respool-article (&optional n method)
11612   "Respool the current article.
11613 The article will be squeezed through the mail spooling process again,
11614 which means that it will be put in some mail newsgroup or other
11615 depending on `nnmail-split-methods'.
11616 If N is a positive number, respool the N next articles.
11617 If N is a negative number, respool the N previous articles.
11618 If N is nil and any articles have been marked with the process mark,
11619 respool those articles instead.
11620
11621 Respooling can be done both from mail groups and \"real\" newsgroups.
11622 In the former case, the articles in question will be moved from the
11623 current group into whatever groups they are destined to.  In the
11624 latter case, they will be copied into the relevant groups."
11625   (interactive 
11626    (list current-prefix-arg
11627          (let* ((methods (gnus-methods-using 'respool))
11628                 (methname
11629                  (symbol-name (or gnus-summary-respool-default-method
11630                                   (car (gnus-find-method-for-group
11631                                         gnus-newsgroup-name)))))
11632                 (method
11633                  (gnus-completing-read 
11634                   methname "What backend do you want to use when? "
11635                   methods nil t nil 'gnus-method-history))
11636                 ms)
11637            (cond
11638             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11639              (list (intern method) ""))
11640             ((= 1 (length ms))
11641              (car ms))
11642             (t
11643              (cdr (completing-read 
11644                    "Server name: "
11645                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11646   (gnus-set-global-variables)
11647   (unless method
11648     (error "No method given for respooling"))
11649   (if (assoc (symbol-name
11650               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11651              (gnus-methods-using 'respool))
11652       (gnus-summary-move-article n nil method)
11653     (gnus-summary-copy-article n nil method)))
11654
11655 (defun gnus-summary-import-article (file)
11656   "Import a random file into a mail newsgroup."
11657   (interactive "fImport file: ")
11658   (gnus-set-global-variables)
11659   (let ((group gnus-newsgroup-name)
11660         (now (current-time))
11661         atts lines)
11662     (or (gnus-check-backend-function 'request-accept-article group)
11663         (error "%s does not support article importing" group))
11664     (or (file-readable-p file)
11665         (not (file-regular-p file))
11666         (error "Can't read %s" file))
11667     (save-excursion
11668       (set-buffer (get-buffer-create " *import file*"))
11669       (buffer-disable-undo (current-buffer))
11670       (erase-buffer)
11671       (insert-file-contents file)
11672       (goto-char (point-min))
11673       (unless (nnheader-article-p)
11674         ;; This doesn't look like an article, so we fudge some headers.
11675         (setq atts (file-attributes file)
11676               lines (count-lines (point-min) (point-max)))
11677         (insert "From: " (read-string "From: ") "\n"
11678                 "Subject: " (read-string "Subject: ") "\n"
11679                 "Date: " (timezone-make-date-arpa-standard
11680                           (current-time-string (nth 5 atts))
11681                           (current-time-zone now)
11682                           (current-time-zone now)) "\n"
11683                 "Message-ID: " (gnus-inews-message-id) "\n"
11684                 "Lines: " (int-to-string lines) "\n"
11685                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11686       (gnus-request-accept-article group nil t)
11687       (kill-buffer (current-buffer)))))
11688
11689 (defun gnus-summary-expire-articles (&optional now)
11690   "Expire all articles that are marked as expirable in the current group."
11691   (interactive)
11692   (gnus-set-global-variables)
11693   (when (gnus-check-backend-function
11694          'request-expire-articles gnus-newsgroup-name)
11695     ;; This backend supports expiry.
11696     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11697            (expirable (if total
11698                           (gnus-list-of-read-articles gnus-newsgroup-name)
11699                         (setq gnus-newsgroup-expirable
11700                               (sort gnus-newsgroup-expirable '<))))
11701            (expiry-wait (if now 'immediate
11702                           (gnus-group-get-parameter
11703                            gnus-newsgroup-name 'expiry-wait)))
11704            es)
11705       (when expirable
11706         ;; There are expirable articles in this group, so we run them
11707         ;; through the expiry process.
11708         (gnus-message 6 "Expiring articles...")
11709         ;; The list of articles that weren't expired is returned.
11710         (if expiry-wait
11711             (let ((nnmail-expiry-wait-function nil)
11712                   (nnmail-expiry-wait expiry-wait))
11713               (setq es (gnus-request-expire-articles
11714                         expirable gnus-newsgroup-name)))
11715           (setq es (gnus-request-expire-articles
11716                     expirable gnus-newsgroup-name)))
11717         (or total (setq gnus-newsgroup-expirable es))
11718         ;; We go through the old list of expirable, and mark all
11719         ;; really expired articles as nonexistent.
11720         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11721           (let ((gnus-use-cache nil))
11722             (while expirable
11723               (unless (memq (car expirable) es)
11724                 (when (gnus-data-find (car expirable))
11725                   (gnus-summary-mark-article
11726                    (car expirable) gnus-canceled-mark)))
11727               (setq expirable (cdr expirable)))))
11728         (gnus-message 6 "Expiring articles...done")))))
11729
11730 (defun gnus-summary-expire-articles-now ()
11731   "Expunge all expirable articles in the current group.
11732 This means that *all* articles that are marked as expirable will be
11733 deleted forever, right now."
11734   (interactive)
11735   (gnus-set-global-variables)
11736   (or gnus-expert-user
11737       (gnus-y-or-n-p
11738        "Are you really, really, really sure you want to delete all these messages? ")
11739       (error "Phew!"))
11740   (gnus-summary-expire-articles t))
11741
11742 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11743 (defun gnus-summary-delete-article (&optional n)
11744   "Delete the N next (mail) articles.
11745 This command actually deletes articles.  This is not a marking
11746 command.  The article will disappear forever from your life, never to
11747 return.
11748 If N is negative, delete backwards.
11749 If N is nil and articles have been marked with the process mark,
11750 delete these instead."
11751   (interactive "P")
11752   (gnus-set-global-variables)
11753   (or (gnus-check-backend-function 'request-expire-articles
11754                                    gnus-newsgroup-name)
11755       (error "The current newsgroup does not support article deletion."))
11756   ;; Compute the list of articles to delete.
11757   (let ((articles (gnus-summary-work-articles n))
11758         not-deleted)
11759     (if (and gnus-novice-user
11760              (not (gnus-y-or-n-p
11761                    (format "Do you really want to delete %s forever? "
11762                            (if (> (length articles) 1) 
11763                                (format "these %s articles" (length articles))
11764                              "this article")))))
11765         ()
11766       ;; Delete the articles.
11767       (setq not-deleted (gnus-request-expire-articles
11768                          articles gnus-newsgroup-name 'force))
11769       (while articles
11770         (gnus-summary-remove-process-mark (car articles))
11771         ;; The backend might not have been able to delete the article
11772         ;; after all.
11773         (or (memq (car articles) not-deleted)
11774             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11775         (setq articles (cdr articles))))
11776     (gnus-summary-position-point)
11777     (gnus-set-mode-line 'summary)
11778     not-deleted))
11779
11780 (defun gnus-summary-edit-article (&optional force)
11781   "Enter into a buffer and edit the current article.
11782 This will have permanent effect only in mail groups.
11783 If FORCE is non-nil, allow editing of articles even in read-only
11784 groups."
11785   (interactive "P")
11786   (save-excursion
11787     (set-buffer gnus-summary-buffer)
11788     (gnus-set-global-variables)
11789     (when (and (not force)
11790                (gnus-group-read-only-p))
11791       (error "The current newsgroup does not support article editing."))
11792     (gnus-summary-select-article t nil t)
11793     (gnus-configure-windows 'article)
11794     (select-window (get-buffer-window gnus-article-buffer))
11795     (gnus-message 6 "C-c C-c to end edits")
11796     (setq buffer-read-only nil)
11797     (text-mode)
11798     (use-local-map (copy-keymap (current-local-map)))
11799     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11800     (buffer-enable-undo)
11801     (widen)
11802     (goto-char (point-min))
11803     (search-forward "\n\n" nil t)))
11804
11805 (defun gnus-summary-edit-article-done ()
11806   "Make edits to the current article permanent."
11807   (interactive)
11808   (if (gnus-group-read-only-p)
11809       (progn
11810         (gnus-summary-edit-article-postpone)
11811         (gnus-error
11812          1 "The current newsgroup does not support article editing."))
11813     (let ((buf (format "%s" (buffer-string))))
11814       (erase-buffer)
11815       (insert buf)
11816       (if (not (gnus-request-replace-article
11817                 (cdr gnus-article-current) (car gnus-article-current)
11818                 (current-buffer)))
11819           (error "Couldn't replace article.")
11820         (gnus-article-mode)
11821         (use-local-map gnus-article-mode-map)
11822         (setq buffer-read-only t)
11823         (buffer-disable-undo (current-buffer))
11824         (gnus-configure-windows 'summary)
11825         (gnus-summary-update-article (cdr gnus-article-current))
11826         (when gnus-use-cache
11827           (gnus-cache-update-article    
11828            (car gnus-article-current) (cdr gnus-article-current)))
11829         (when gnus-keep-backlog
11830           (gnus-backlog-remove-article 
11831            (car gnus-article-current) (cdr gnus-article-current))))
11832       (save-excursion
11833         (when (get-buffer gnus-original-article-buffer)
11834           (set-buffer gnus-original-article-buffer)
11835           (setq gnus-original-article nil)))
11836       (setq gnus-article-current nil
11837             gnus-current-article nil)
11838       (run-hooks 'gnus-article-display-hook)
11839       (and (gnus-visual-p 'summary-highlight 'highlight)
11840            (run-hooks 'gnus-visual-mark-article-hook)))))
11841
11842 (defun gnus-summary-edit-article-postpone ()
11843   "Postpone changes to the current article."
11844   (interactive)
11845   (gnus-article-mode)
11846   (use-local-map gnus-article-mode-map)
11847   (setq buffer-read-only t)
11848   (buffer-disable-undo (current-buffer))
11849   (gnus-configure-windows 'summary)
11850   (and (gnus-visual-p 'summary-highlight 'highlight)
11851        (run-hooks 'gnus-visual-mark-article-hook)))
11852
11853 (defun gnus-summary-respool-query ()
11854   "Query where the respool algorithm would put this article."
11855   (interactive)
11856   (gnus-set-global-variables)
11857   (gnus-summary-select-article)
11858   (save-excursion
11859     (set-buffer gnus-article-buffer)
11860     (save-restriction
11861       (goto-char (point-min))
11862       (search-forward "\n\n")
11863       (narrow-to-region (point-min) (point))
11864       (pp-eval-expression
11865        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11866
11867 ;; Summary marking commands.
11868
11869 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11870   "Mark articles which has the same subject as read, and then select the next.
11871 If UNMARK is positive, remove any kind of mark.
11872 If UNMARK is negative, tick articles."
11873   (interactive "P")
11874   (gnus-set-global-variables)
11875   (if unmark
11876       (setq unmark (prefix-numeric-value unmark)))
11877   (let ((count
11878          (gnus-summary-mark-same-subject
11879           (gnus-summary-article-subject) unmark)))
11880     ;; Select next unread article.  If auto-select-same mode, should
11881     ;; select the first unread article.
11882     (gnus-summary-next-article t (and gnus-auto-select-same
11883                                       (gnus-summary-article-subject)))
11884     (gnus-message 7 "%d article%s marked as %s"
11885                   count (if (= count 1) " is" "s are")
11886                   (if unmark "unread" "read"))))
11887
11888 (defun gnus-summary-kill-same-subject (&optional unmark)
11889   "Mark articles which has the same subject as read.
11890 If UNMARK is positive, remove any kind of mark.
11891 If UNMARK is negative, tick articles."
11892   (interactive "P")
11893   (gnus-set-global-variables)
11894   (if unmark
11895       (setq unmark (prefix-numeric-value unmark)))
11896   (let ((count
11897          (gnus-summary-mark-same-subject
11898           (gnus-summary-article-subject) unmark)))
11899     ;; If marked as read, go to next unread subject.
11900     (if (null unmark)
11901         ;; Go to next unread subject.
11902         (gnus-summary-next-subject 1 t))
11903     (gnus-message 7 "%d articles are marked as %s"
11904                   count (if unmark "unread" "read"))))
11905
11906 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11907   "Mark articles with same SUBJECT as read, and return marked number.
11908 If optional argument UNMARK is positive, remove any kinds of marks.
11909 If optional argument UNMARK is negative, mark articles as unread instead."
11910   (let ((count 1))
11911     (save-excursion
11912       (cond
11913        ((null unmark)                   ; Mark as read.
11914         (while (and
11915                 (progn
11916                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11917                   (gnus-summary-show-thread) t)
11918                 (gnus-summary-find-subject subject))
11919           (setq count (1+ count))))
11920        ((> unmark 0)                    ; Tick.
11921         (while (and
11922                 (progn
11923                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11924                   (gnus-summary-show-thread) t)
11925                 (gnus-summary-find-subject subject))
11926           (setq count (1+ count))))
11927        (t                               ; Mark as unread.
11928         (while (and
11929                 (progn
11930                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11931                   (gnus-summary-show-thread) t)
11932                 (gnus-summary-find-subject subject))
11933           (setq count (1+ count)))))
11934       (gnus-set-mode-line 'summary)
11935       ;; Return the number of marked articles.
11936       count)))
11937
11938 (defun gnus-summary-mark-as-processable (n &optional unmark)
11939   "Set the process mark on the next N articles.
11940 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11941 the process mark instead.  The difference between N and the actual
11942 number of articles marked is returned."
11943   (interactive "p")
11944   (gnus-set-global-variables)
11945   (let ((backward (< n 0))
11946         (n (abs n)))
11947     (while (and
11948             (> n 0)
11949             (if unmark
11950                 (gnus-summary-remove-process-mark
11951                  (gnus-summary-article-number))
11952               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11953             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11954       (setq n (1- n)))
11955     (if (/= 0 n) (gnus-message 7 "No more articles"))
11956     (gnus-summary-recenter)
11957     (gnus-summary-position-point)
11958     n))
11959
11960 (defun gnus-summary-unmark-as-processable (n)
11961   "Remove the process mark from the next N articles.
11962 If N is negative, mark backward instead.  The difference between N and
11963 the actual number of articles marked is returned."
11964   (interactive "p")
11965   (gnus-set-global-variables)
11966   (gnus-summary-mark-as-processable n t))
11967
11968 (defun gnus-summary-unmark-all-processable ()
11969   "Remove the process mark from all articles."
11970   (interactive)
11971   (gnus-set-global-variables)
11972   (save-excursion
11973     (while gnus-newsgroup-processable
11974       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11975   (gnus-summary-position-point))
11976
11977 (defun gnus-summary-mark-as-expirable (n)
11978   "Mark N articles forward as expirable.
11979 If N is negative, mark backward instead.  The difference between N and
11980 the actual number of articles marked is returned."
11981   (interactive "p")
11982   (gnus-set-global-variables)
11983   (gnus-summary-mark-forward n gnus-expirable-mark))
11984
11985 (defun gnus-summary-mark-article-as-replied (article)
11986   "Mark ARTICLE replied and update the summary line."
11987   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11988   (let ((buffer-read-only nil))
11989     (when (gnus-summary-goto-subject article)
11990       (gnus-summary-update-secondary-mark article))))
11991
11992 (defun gnus-summary-set-bookmark (article)
11993   "Set a bookmark in current article."
11994   (interactive (list (gnus-summary-article-number)))
11995   (gnus-set-global-variables)
11996   (if (or (not (get-buffer gnus-article-buffer))
11997           (not gnus-current-article)
11998           (not gnus-article-current)
11999           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12000       (error "No current article selected"))
12001   ;; Remove old bookmark, if one exists.
12002   (let ((old (assq article gnus-newsgroup-bookmarks)))
12003     (if old (setq gnus-newsgroup-bookmarks
12004                   (delq old gnus-newsgroup-bookmarks))))
12005   ;; Set the new bookmark, which is on the form
12006   ;; (article-number . line-number-in-body).
12007   (setq gnus-newsgroup-bookmarks
12008         (cons
12009          (cons article
12010                (save-excursion
12011                  (set-buffer gnus-article-buffer)
12012                  (count-lines
12013                   (min (point)
12014                        (save-excursion
12015                          (goto-char (point-min))
12016                          (search-forward "\n\n" nil t)
12017                          (point)))
12018                   (point))))
12019          gnus-newsgroup-bookmarks))
12020   (gnus-message 6 "A bookmark has been added to the current article."))
12021
12022 (defun gnus-summary-remove-bookmark (article)
12023   "Remove the bookmark from the current article."
12024   (interactive (list (gnus-summary-article-number)))
12025   (gnus-set-global-variables)
12026   ;; Remove old bookmark, if one exists.
12027   (let ((old (assq article gnus-newsgroup-bookmarks)))
12028     (if old
12029         (progn
12030           (setq gnus-newsgroup-bookmarks
12031                 (delq old gnus-newsgroup-bookmarks))
12032           (gnus-message 6 "Removed bookmark."))
12033       (gnus-message 6 "No bookmark in current article."))))
12034
12035 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12036 (defun gnus-summary-mark-as-dormant (n)
12037   "Mark N articles forward as dormant.
12038 If N is negative, mark backward instead.  The difference between N and
12039 the actual number of articles marked is returned."
12040   (interactive "p")
12041   (gnus-set-global-variables)
12042   (gnus-summary-mark-forward n gnus-dormant-mark))
12043
12044 (defun gnus-summary-set-process-mark (article)
12045   "Set the process mark on ARTICLE and update the summary line."
12046   (setq gnus-newsgroup-processable
12047         (cons article
12048               (delq article gnus-newsgroup-processable)))
12049   (when (gnus-summary-goto-subject article)
12050     (gnus-summary-show-thread)
12051     (gnus-summary-update-secondary-mark article)))
12052
12053 (defun gnus-summary-remove-process-mark (article)
12054   "Remove the process mark from ARTICLE and update the summary line."
12055   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12056   (when (gnus-summary-goto-subject article)
12057     (gnus-summary-show-thread)
12058     (gnus-summary-update-secondary-mark article)))
12059
12060 (defun gnus-summary-set-saved-mark (article)
12061   "Set the process mark on ARTICLE and update the summary line."
12062   (push article gnus-newsgroup-saved)
12063   (when (gnus-summary-goto-subject article)
12064     (gnus-summary-update-secondary-mark article)))
12065
12066 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12067   "Mark N articles as read forwards.
12068 If N is negative, mark backwards instead.
12069 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
12070 marked as unread.
12071 The difference between N and the actual number of articles marked is
12072 returned."
12073   (interactive "p")
12074   (gnus-set-global-variables)
12075   (let ((backward (< n 0))
12076         (gnus-summary-goto-unread
12077          (and gnus-summary-goto-unread
12078               (not (eq gnus-summary-goto-unread 'never))
12079               (not (memq mark (list gnus-unread-mark
12080                                     gnus-ticked-mark gnus-dormant-mark)))))
12081         (n (abs n))
12082         (mark (or mark gnus-del-mark)))
12083     (while (and (> n 0)
12084                 (gnus-summary-mark-article nil mark no-expire)
12085                 (zerop (gnus-summary-next-subject
12086                         (if backward -1 1)
12087                         (and gnus-summary-goto-unread
12088                              (not (eq gnus-summary-goto-unread 'never)))
12089                         t)))
12090       (setq n (1- n)))
12091     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12092     (gnus-summary-recenter)
12093     (gnus-summary-position-point)
12094     (gnus-set-mode-line 'summary)
12095     n))
12096
12097 (defun gnus-summary-mark-article-as-read (mark)
12098   "Mark the current article quickly as read with MARK."
12099   (let ((article (gnus-summary-article-number)))
12100     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12101     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12102     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12103     (setq gnus-newsgroup-reads
12104           (cons (cons article mark) gnus-newsgroup-reads))
12105     ;; Possibly remove from cache, if that is used.
12106     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12107     ;; Allow the backend to change the mark.
12108     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12109     ;; Check for auto-expiry.
12110     (when (and gnus-newsgroup-auto-expire
12111                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12112                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12113                    (= mark gnus-ancient-mark)
12114                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12115       (setq mark gnus-expirable-mark)
12116       (push article gnus-newsgroup-expirable))
12117     ;; Set the mark in the buffer.
12118     (gnus-summary-update-mark mark 'unread)
12119     t))
12120
12121 (defun gnus-summary-mark-article-as-unread (mark)
12122   "Mark the current article quickly as unread with MARK."
12123   (let ((article (gnus-summary-article-number)))
12124     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12125     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12126     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12127     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12128     (cond ((= mark gnus-ticked-mark)
12129            (push article gnus-newsgroup-marked))
12130           ((= mark gnus-dormant-mark)
12131            (push article gnus-newsgroup-dormant))
12132           (t
12133            (push article gnus-newsgroup-unreads)))
12134     (setq gnus-newsgroup-reads
12135           (delq (assq article gnus-newsgroup-reads)
12136                 gnus-newsgroup-reads))
12137
12138     ;; See whether the article is to be put in the cache.
12139     (and gnus-use-cache
12140          (vectorp (gnus-summary-article-header article))
12141          (save-excursion
12142            (gnus-cache-possibly-enter-article
12143             gnus-newsgroup-name article
12144             (gnus-summary-article-header article)
12145             (= mark gnus-ticked-mark)
12146             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12147
12148     ;; Fix the mark.
12149     (gnus-summary-update-mark mark 'unread)
12150     t))
12151
12152 (defun gnus-summary-mark-article (&optional article mark no-expire)
12153   "Mark ARTICLE with MARK.  MARK can be any character.
12154 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12155 `??' (dormant) and `?E' (expirable).
12156 If MARK is nil, then the default character `?D' is used.
12157 If ARTICLE is nil, then the article on the current line will be
12158 marked."
12159   ;; The mark might be a string.
12160   (and (stringp mark)
12161        (setq mark (aref mark 0)))
12162   ;; If no mark is given, then we check auto-expiring.
12163   (and (not no-expire)
12164        gnus-newsgroup-auto-expire
12165        (or (not mark)
12166            (and (numberp mark)
12167                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12168                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12169                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12170        (setq mark gnus-expirable-mark))
12171   (let* ((mark (or mark gnus-del-mark))
12172          (article (or article (gnus-summary-article-number))))
12173     (or article (error "No article on current line"))
12174     (if (or (= mark gnus-unread-mark)
12175             (= mark gnus-ticked-mark)
12176             (= mark gnus-dormant-mark))
12177         (gnus-mark-article-as-unread article mark)
12178       (gnus-mark-article-as-read article mark))
12179
12180     ;; See whether the article is to be put in the cache.
12181     (and gnus-use-cache
12182          (not (= mark gnus-canceled-mark))
12183          (vectorp (gnus-summary-article-header article))
12184          (save-excursion
12185            (gnus-cache-possibly-enter-article
12186             gnus-newsgroup-name article
12187             (gnus-summary-article-header article)
12188             (= mark gnus-ticked-mark)
12189             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12190
12191     (if (gnus-summary-goto-subject article nil t)
12192         (let ((buffer-read-only nil))
12193           (gnus-summary-show-thread)
12194           ;; Fix the mark.
12195           (gnus-summary-update-mark mark 'unread)
12196           t))))
12197
12198 (defun gnus-summary-update-secondary-mark (article)
12199   "Update the secondary (read, process, cache) mark."
12200   (gnus-summary-update-mark
12201    (cond ((memq article gnus-newsgroup-processable)
12202           gnus-process-mark)
12203          ((memq article gnus-newsgroup-cached)
12204           gnus-cached-mark)
12205          ((memq article gnus-newsgroup-replied)
12206           gnus-replied-mark)
12207          ((memq article gnus-newsgroup-saved)
12208           gnus-saved-mark)
12209          (t gnus-unread-mark))
12210    'replied)
12211   (when (gnus-visual-p 'summary-highlight 'highlight)
12212     (run-hooks 'gnus-summary-update-hook))
12213   t)
12214
12215 (defun gnus-summary-update-mark (mark type)
12216   (beginning-of-line)
12217   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12218         (buffer-read-only nil))
12219     (when (and forward
12220                (<= (+ forward (point)) (point-max)))
12221       ;; Go to the right position on the line.
12222       (goto-char (+ forward (point)))
12223       ;; Replace the old mark with the new mark.
12224       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12225       ;; Optionally update the marks by some user rule.
12226       (when (eq type 'unread)
12227         (gnus-data-set-mark
12228          (gnus-data-find (gnus-summary-article-number)) mark)
12229         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12230
12231 (defun gnus-mark-article-as-read (article &optional mark)
12232   "Enter ARTICLE in the pertinent lists and remove it from others."
12233   ;; Make the article expirable.
12234   (let ((mark (or mark gnus-del-mark)))
12235     (if (= mark gnus-expirable-mark)
12236         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12237       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12238     ;; Remove from unread and marked lists.
12239     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12240     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12241     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12242     (push (cons article mark) gnus-newsgroup-reads)
12243     ;; Possibly remove from cache, if that is used.
12244     (when gnus-use-cache
12245       (gnus-cache-enter-remove-article article))))
12246
12247 (defun gnus-mark-article-as-unread (article &optional mark)
12248   "Enter ARTICLE in the pertinent lists and remove it from others."
12249   (let ((mark (or mark gnus-ticked-mark)))
12250     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12251     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12252     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12253     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12254     (cond ((= mark gnus-ticked-mark)
12255            (push article gnus-newsgroup-marked))
12256           ((= mark gnus-dormant-mark)
12257            (push article gnus-newsgroup-dormant))
12258           (t
12259            (push article gnus-newsgroup-unreads)))
12260     (setq gnus-newsgroup-reads
12261           (delq (assq article gnus-newsgroup-reads)
12262                 gnus-newsgroup-reads))))
12263
12264 (defalias 'gnus-summary-mark-as-unread-forward
12265   'gnus-summary-tick-article-forward)
12266 (make-obsolete 'gnus-summary-mark-as-unread-forward
12267                'gnus-summary-tick-article-forward)
12268 (defun gnus-summary-tick-article-forward (n)
12269   "Tick N articles forwards.
12270 If N is negative, tick backwards instead.
12271 The difference between N and the number of articles ticked is returned."
12272   (interactive "p")
12273   (gnus-summary-mark-forward n gnus-ticked-mark))
12274
12275 (defalias 'gnus-summary-mark-as-unread-backward
12276   'gnus-summary-tick-article-backward)
12277 (make-obsolete 'gnus-summary-mark-as-unread-backward
12278                'gnus-summary-tick-article-backward)
12279 (defun gnus-summary-tick-article-backward (n)
12280   "Tick N articles backwards.
12281 The difference between N and the number of articles ticked is returned."
12282   (interactive "p")
12283   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12284
12285 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12286 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12287 (defun gnus-summary-tick-article (&optional article clear-mark)
12288   "Mark current article as unread.
12289 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12290 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12291   (interactive)
12292   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12293                                        gnus-ticked-mark)))
12294
12295 (defun gnus-summary-mark-as-read-forward (n)
12296   "Mark N articles as read forwards.
12297 If N is negative, mark backwards instead.
12298 The difference between N and the actual number of articles marked is
12299 returned."
12300   (interactive "p")
12301   (gnus-summary-mark-forward n gnus-del-mark t))
12302
12303 (defun gnus-summary-mark-as-read-backward (n)
12304   "Mark the N articles as read backwards.
12305 The difference between N and the actual number of articles marked is
12306 returned."
12307   (interactive "p")
12308   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12309
12310 (defun gnus-summary-mark-as-read (&optional article mark)
12311   "Mark current article as read.
12312 ARTICLE specifies the article to be marked as read.
12313 MARK specifies a string to be inserted at the beginning of the line."
12314   (gnus-summary-mark-article article mark))
12315
12316 (defun gnus-summary-clear-mark-forward (n)
12317   "Clear marks from N articles forward.
12318 If N is negative, clear backward instead.
12319 The difference between N and the number of marks cleared is returned."
12320   (interactive "p")
12321   (gnus-summary-mark-forward n gnus-unread-mark))
12322
12323 (defun gnus-summary-clear-mark-backward (n)
12324   "Clear marks from N articles backward.
12325 The difference between N and the number of marks cleared is returned."
12326   (interactive "p")
12327   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12328
12329 (defun gnus-summary-mark-unread-as-read ()
12330   "Intended to be used by `gnus-summary-mark-article-hook'."
12331   (when (memq gnus-current-article gnus-newsgroup-unreads)
12332     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12333
12334 (defun gnus-summary-mark-read-and-unread-as-read ()
12335   "Intended to be used by `gnus-summary-mark-article-hook'."
12336   (let ((mark (gnus-summary-article-mark)))
12337     (when (or (gnus-unread-mark-p mark)
12338               (gnus-read-mark-p mark))
12339       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12340
12341 (defun gnus-summary-mark-region-as-read (point mark all)
12342   "Mark all unread articles between point and mark as read.
12343 If given a prefix, mark all articles between point and mark as read,
12344 even ticked and dormant ones."
12345   (interactive "r\nP")
12346   (save-excursion
12347     (let (article)
12348       (goto-char point)
12349       (beginning-of-line)
12350       (while (and
12351               (< (point) mark)
12352               (progn
12353                 (when (or all
12354                           (memq (setq article (gnus-summary-article-number))
12355                                 gnus-newsgroup-unreads))
12356                   (gnus-summary-mark-article article gnus-del-mark))
12357                 t)
12358               (gnus-summary-find-next))))))
12359
12360 (defun gnus-summary-mark-below (score mark)
12361   "Mark articles with score less than SCORE with MARK."
12362   (interactive "P\ncMark: ")
12363   (gnus-set-global-variables)
12364   (setq score (if score
12365                   (prefix-numeric-value score)
12366                 (or gnus-summary-default-score 0)))
12367   (save-excursion
12368     (set-buffer gnus-summary-buffer)
12369     (goto-char (point-min))
12370     (while 
12371         (progn
12372           (and (< (gnus-summary-article-score) score)
12373                (gnus-summary-mark-article nil mark))
12374           (gnus-summary-find-next)))))
12375
12376 (defun gnus-summary-kill-below (&optional score)
12377   "Mark articles with score below SCORE as read."
12378   (interactive "P")
12379   (gnus-set-global-variables)
12380   (gnus-summary-mark-below score gnus-killed-mark))
12381
12382 (defun gnus-summary-clear-above (&optional score)
12383   "Clear all marks from articles with score above SCORE."
12384   (interactive "P")
12385   (gnus-set-global-variables)
12386   (gnus-summary-mark-above score gnus-unread-mark))
12387
12388 (defun gnus-summary-tick-above (&optional score)
12389   "Tick all articles with score above SCORE."
12390   (interactive "P")
12391   (gnus-set-global-variables)
12392   (gnus-summary-mark-above score gnus-ticked-mark))
12393
12394 (defun gnus-summary-mark-above (score mark)
12395   "Mark articles with score over SCORE with MARK."
12396   (interactive "P\ncMark: ")
12397   (gnus-set-global-variables)
12398   (setq score (if score
12399                   (prefix-numeric-value score)
12400                 (or gnus-summary-default-score 0)))
12401   (save-excursion
12402     (set-buffer gnus-summary-buffer)
12403     (goto-char (point-min))
12404     (while (and (progn
12405                   (if (> (gnus-summary-article-score) score)
12406                       (gnus-summary-mark-article nil mark))
12407                   t)
12408                 (gnus-summary-find-next)))))
12409
12410 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12411 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12412 (defun gnus-summary-limit-include-expunged ()
12413   "Display all the hidden articles that were expunged for low scores."
12414   (interactive)
12415   (gnus-set-global-variables)
12416   (let ((buffer-read-only nil))
12417     (let ((scored gnus-newsgroup-scored)
12418           headers h)
12419       (while scored
12420         (or (gnus-summary-goto-subject (caar scored))
12421             (and (setq h (gnus-summary-article-header (caar scored)))
12422                  (< (cdar scored) gnus-summary-expunge-below)
12423                  (setq headers (cons h headers))))
12424         (setq scored (cdr scored)))
12425       (or headers (error "No expunged articles hidden."))
12426       (goto-char (point-min))
12427       (gnus-summary-prepare-unthreaded (nreverse headers)))
12428     (goto-char (point-min))
12429     (gnus-summary-position-point)))
12430
12431 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12432   "Mark all articles not marked as unread in this newsgroup as read.
12433 If prefix argument ALL is non-nil, all articles are marked as read.
12434 If QUIETLY is non-nil, no questions will be asked.
12435 If TO-HERE is non-nil, it should be a point in the buffer.  All
12436 articles before this point will be marked as read.
12437 The number of articles marked as read is returned."
12438   (interactive "P")
12439   (gnus-set-global-variables)
12440   (prog1
12441       (if (or quietly
12442               (not gnus-interactive-catchup) ;Without confirmation?
12443               gnus-expert-user
12444               (gnus-y-or-n-p
12445                (if all
12446                    "Mark absolutely all articles as read? "
12447                  "Mark all unread articles as read? ")))
12448           (if (and not-mark
12449                    (not gnus-newsgroup-adaptive)
12450                    (not gnus-newsgroup-auto-expire))
12451               (progn
12452                 (when all
12453                   (setq gnus-newsgroup-marked nil
12454                         gnus-newsgroup-dormant nil))
12455                 (setq gnus-newsgroup-unreads nil))
12456             ;; We actually mark all articles as canceled, which we
12457             ;; have to do when using auto-expiry or adaptive scoring.
12458             (gnus-summary-show-all-threads)
12459             (if (gnus-summary-first-subject (not all))
12460                 (while (and
12461                         (if to-here (< (point) to-here) t)
12462                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12463                         (gnus-summary-find-next (not all)))))
12464             (unless to-here
12465               (setq gnus-newsgroup-unreads nil))
12466             (gnus-set-mode-line 'summary)))
12467     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12468       (if (and (not to-here) (eq 'nnvirtual (car method)))
12469           (nnvirtual-catchup-group
12470            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12471     (gnus-summary-position-point)))
12472
12473 (defun gnus-summary-catchup-to-here (&optional all)
12474   "Mark all unticked articles before the current one as read.
12475 If ALL is non-nil, also mark ticked and dormant articles as read."
12476   (interactive "P")
12477   (gnus-set-global-variables)
12478   (save-excursion
12479     (gnus-save-hidden-threads
12480       (let ((beg (point)))
12481         ;; We check that there are unread articles.
12482         (when (or all (gnus-summary-find-prev))
12483           (gnus-summary-catchup all t beg)))))
12484   (gnus-summary-position-point))
12485
12486 (defun gnus-summary-catchup-all (&optional quietly)
12487   "Mark all articles in this newsgroup as read."
12488   (interactive "P")
12489   (gnus-set-global-variables)
12490   (gnus-summary-catchup t quietly))
12491
12492 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12493   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12494 If prefix argument ALL is non-nil, all articles are marked as read."
12495   (interactive "P")
12496   (gnus-set-global-variables)
12497   (gnus-summary-catchup all quietly nil 'fast)
12498   ;; Select next newsgroup or exit.
12499   (if (eq gnus-auto-select-next 'quietly)
12500       (gnus-summary-next-group nil)
12501     (gnus-summary-exit)))
12502
12503 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12504   "Mark all articles in this newsgroup as read, and then exit."
12505   (interactive "P")
12506   (gnus-set-global-variables)
12507   (gnus-summary-catchup-and-exit t quietly))
12508
12509 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12510 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12511   "Mark all articles in this group as read and select the next group.
12512 If given a prefix, mark all articles, unread as well as ticked, as
12513 read."
12514   (interactive "P")
12515   (gnus-set-global-variables)
12516   (save-excursion
12517     (gnus-summary-catchup all))
12518   (gnus-summary-next-article t nil nil t))
12519
12520 ;; Thread-based commands.
12521
12522 (defun gnus-summary-articles-in-thread (&optional article)
12523   "Return a list of all articles in the current thread.
12524 If ARTICLE is non-nil, return all articles in the thread that starts
12525 with that article."
12526   (let* ((article (or article (gnus-summary-article-number)))
12527          (data (gnus-data-find-list article))
12528          (top-level (gnus-data-level (car data)))
12529          (top-subject
12530           (cond ((null gnus-thread-operation-ignore-subject)
12531                  (gnus-simplify-subject-re
12532                   (mail-header-subject (gnus-data-header (car data)))))
12533                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12534                  (gnus-simplify-subject-fuzzy
12535                   (mail-header-subject (gnus-data-header (car data)))))
12536                 (t nil)))
12537          (end-point (save-excursion
12538                       (if (gnus-summary-go-to-next-thread) 
12539                           (point) (point-max))))
12540          articles)
12541     (while (and data
12542                 (< (gnus-data-pos (car data)) end-point))
12543       (when (or (not top-subject)
12544                 (string= top-subject
12545                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12546                              (gnus-simplify-subject-fuzzy
12547                               (mail-header-subject
12548                                (gnus-data-header (car data))))
12549                            (gnus-simplify-subject-re
12550                             (mail-header-subject
12551                              (gnus-data-header (car data)))))))
12552         (push (gnus-data-number (car data)) articles))
12553       (unless (and (setq data (cdr data))
12554                    (> (gnus-data-level (car data)) top-level))
12555         (setq data nil)))
12556     ;; Return the list of articles.
12557     (nreverse articles)))
12558
12559 (defun gnus-summary-rethread-current ()
12560   "Rethread the thread the current article is part of."
12561   (interactive)
12562   (gnus-set-global-variables)
12563   (let* ((gnus-show-threads t)
12564          (article (gnus-summary-article-number))
12565          (id (mail-header-id (gnus-summary-article-header)))
12566          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12567     (unless id
12568       (error "No article on the current line"))
12569     (gnus-rebuild-thread id)
12570     (gnus-summary-goto-subject article)))
12571
12572 (defun gnus-summary-reparent-thread ()
12573   "Make current article child of the marked (or previous) article.
12574
12575 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12576 is non-nil or the Subject: of both articles are the same."
12577   (interactive)
12578   (or (not (gnus-group-read-only-p))
12579       (error "The current newsgroup does not support article editing."))
12580   (or (<= (length gnus-newsgroup-processable) 1)
12581       (error "No more than one article may be marked."))
12582   (save-window-excursion
12583     (let ((gnus-article-buffer " *reparent*")
12584           (current-article (gnus-summary-article-number))
12585           ; first grab the marked article, otherwise one line up.
12586           (parent-article (if (not (null gnus-newsgroup-processable))
12587                               (car gnus-newsgroup-processable)
12588                             (save-excursion
12589                               (if (eq (forward-line -1) 0)
12590                                   (gnus-summary-article-number)
12591                                 (error "Beginning of summary buffer."))))))
12592       (or (not (eq current-article parent-article))
12593           (error "An article may not be self-referential."))
12594       (let ((message-id (mail-header-id 
12595                          (gnus-summary-article-header parent-article))))
12596         (or (and message-id (not (equal message-id "")))
12597             (error "No message-id in desired parent."))
12598         (gnus-summary-select-article t t nil current-article)
12599         (set-buffer gnus-article-buffer)
12600         (setq buffer-read-only nil)
12601         (let ((buf (format "%s" (buffer-string))))
12602           (erase-buffer)
12603           (insert buf))
12604         (goto-char (point-min))
12605         (if (search-forward-regexp "^References: " nil t)
12606             (insert message-id " " )
12607           (insert "References: " message-id "\n"))
12608         (or (gnus-request-replace-article current-article
12609                                           (car gnus-article-current)
12610                                           gnus-article-buffer)
12611             (error "Couldn't replace article."))
12612         (set-buffer gnus-summary-buffer)
12613         (gnus-summary-unmark-all-processable)
12614         (gnus-summary-rethread-current)
12615         (gnus-message 3 "Article %d is now the child of article %d."
12616                       current-article parent-article)))))
12617
12618 (defun gnus-summary-toggle-threads (&optional arg)
12619   "Toggle showing conversation threads.
12620 If ARG is positive number, turn showing conversation threads on."
12621   (interactive "P")
12622   (gnus-set-global-variables)
12623   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12624     (setq gnus-show-threads
12625           (if (null arg) (not gnus-show-threads)
12626             (> (prefix-numeric-value arg) 0)))
12627     (gnus-summary-prepare)
12628     (gnus-summary-goto-subject current)
12629     (gnus-summary-position-point)))
12630
12631 (defun gnus-summary-show-all-threads ()
12632   "Show all threads."
12633   (interactive)
12634   (gnus-set-global-variables)
12635   (save-excursion
12636     (let ((buffer-read-only nil))
12637       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12638   (gnus-summary-position-point))
12639
12640 (defun gnus-summary-show-thread ()
12641   "Show thread subtrees.
12642 Returns nil if no thread was there to be shown."
12643   (interactive)
12644   (gnus-set-global-variables)
12645   (let ((buffer-read-only nil)
12646         (orig (point))
12647         ;; first goto end then to beg, to have point at beg after let
12648         (end (progn (end-of-line) (point)))
12649         (beg (progn (beginning-of-line) (point))))
12650     (prog1
12651         ;; Any hidden lines here?
12652         (search-forward "\r" end t)
12653       (subst-char-in-region beg end ?\^M ?\n t)
12654       (goto-char orig)
12655       (gnus-summary-position-point))))
12656
12657 (defun gnus-summary-hide-all-threads ()
12658   "Hide all thread subtrees."
12659   (interactive)
12660   (gnus-set-global-variables)
12661   (save-excursion
12662     (goto-char (point-min))
12663     (gnus-summary-hide-thread)
12664     (while (zerop (gnus-summary-next-thread 1 t))
12665       (gnus-summary-hide-thread)))
12666   (gnus-summary-position-point))
12667
12668 (defun gnus-summary-hide-thread ()
12669   "Hide thread subtrees.
12670 Returns nil if no threads were there to be hidden."
12671   (interactive)
12672   (gnus-set-global-variables)
12673   (let ((buffer-read-only nil)
12674         (start (point))
12675         (article (gnus-summary-article-number)))
12676     (goto-char start)
12677     ;; Go forward until either the buffer ends or the subthread
12678     ;; ends.
12679     (when (and (not (eobp))
12680                (or (zerop (gnus-summary-next-thread 1 t))
12681                    (goto-char (point-max))))
12682       (prog1
12683           (if (and (> (point) start)
12684                    (search-backward "\n" start t))
12685               (progn
12686                 (subst-char-in-region start (point) ?\n ?\^M)
12687                 (gnus-summary-goto-subject article))
12688             (goto-char start)
12689             nil)
12690         ;;(gnus-summary-position-point)
12691         ))))
12692
12693 (defun gnus-summary-go-to-next-thread (&optional previous)
12694   "Go to the same level (or less) next thread.
12695 If PREVIOUS is non-nil, go to previous thread instead.
12696 Return the article number moved to, or nil if moving was impossible."
12697   (let ((level (gnus-summary-thread-level))
12698         (way (if previous -1 1))
12699         (beg (point)))
12700     (forward-line way)
12701     (while (and (not (eobp))
12702                 (< level (gnus-summary-thread-level)))
12703       (forward-line way))
12704     (if (eobp)
12705         (progn
12706           (goto-char beg)
12707           nil)
12708       (setq beg (point))
12709       (prog1
12710           (gnus-summary-article-number)
12711         (goto-char beg)))))
12712
12713 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12714   "Go to the same level (or less) next thread.
12715 If PREVIOUS is non-nil, go to previous thread instead.
12716 Return the article number moved to, or nil if moving was impossible."
12717   (if (and (eq gnus-summary-make-false-root 'dummy)
12718            (gnus-summary-article-intangible-p))
12719       (let ((beg (point)))
12720         (while (and (zerop (forward-line 1))
12721                     (not (gnus-summary-article-intangible-p))
12722                     (not (zerop (save-excursion 
12723                                   (gnus-summary-thread-level))))))
12724         (if (eobp)
12725             (progn
12726               (goto-char beg)
12727               nil)
12728           (point)))
12729     (let* ((level (gnus-summary-thread-level))
12730            (article (gnus-summary-article-number))
12731            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12732            oart)
12733       (while data
12734         (if (<= (gnus-data-level (car data)) level)
12735             (setq oart (gnus-data-number (car data))
12736                   data nil)
12737           (setq data (cdr data))))
12738       (and oart
12739            (gnus-summary-goto-subject oart)))))
12740
12741 (defun gnus-summary-next-thread (n &optional silent)
12742   "Go to the same level next N'th thread.
12743 If N is negative, search backward instead.
12744 Returns the difference between N and the number of skips actually
12745 done.
12746
12747 If SILENT, don't output messages."
12748   (interactive "p")
12749   (gnus-set-global-variables)
12750   (let ((backward (< n 0))
12751         (n (abs n))
12752         old dum int)
12753     (while (and (> n 0)
12754                 (gnus-summary-go-to-next-thread backward))
12755       (decf n))
12756     (unless silent 
12757       (gnus-summary-position-point))
12758     (when (and (not silent) (/= 0 n))
12759       (gnus-message 7 "No more threads"))
12760     n))
12761
12762 (defun gnus-summary-prev-thread (n)
12763   "Go to the same level previous N'th thread.
12764 Returns the difference between N and the number of skips actually
12765 done."
12766   (interactive "p")
12767   (gnus-set-global-variables)
12768   (gnus-summary-next-thread (- n)))
12769
12770 (defun gnus-summary-go-down-thread ()
12771   "Go down one level in the current thread."
12772   (let ((children (gnus-summary-article-children)))
12773     (and children
12774          (gnus-summary-goto-subject (car children)))))
12775
12776 (defun gnus-summary-go-up-thread ()
12777   "Go up one level in the current thread."
12778   (let ((parent (gnus-summary-article-parent)))
12779     (and parent
12780          (gnus-summary-goto-subject parent))))
12781
12782 (defun gnus-summary-down-thread (n)
12783   "Go down thread N steps.
12784 If N is negative, go up instead.
12785 Returns the difference between N and how many steps down that were
12786 taken."
12787   (interactive "p")
12788   (gnus-set-global-variables)
12789   (let ((up (< n 0))
12790         (n (abs n)))
12791     (while (and (> n 0)
12792                 (if up (gnus-summary-go-up-thread)
12793                   (gnus-summary-go-down-thread)))
12794       (setq n (1- n)))
12795     (gnus-summary-position-point)
12796     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12797     n))
12798
12799 (defun gnus-summary-up-thread (n)
12800   "Go up thread N steps.
12801 If N is negative, go up instead.
12802 Returns the difference between N and how many steps down that were
12803 taken."
12804   (interactive "p")
12805   (gnus-set-global-variables)
12806   (gnus-summary-down-thread (- n)))
12807
12808 (defun gnus-summary-top-thread ()
12809   "Go to the top of the thread."
12810   (interactive)
12811   (gnus-set-global-variables)
12812   (while (gnus-summary-go-up-thread))
12813   (gnus-summary-article-number))
12814
12815 (defun gnus-summary-kill-thread (&optional unmark)
12816   "Mark articles under current thread as read.
12817 If the prefix argument is positive, remove any kinds of marks.
12818 If the prefix argument is negative, tick articles instead."
12819   (interactive "P")
12820   (gnus-set-global-variables)
12821   (if unmark
12822       (setq unmark (prefix-numeric-value unmark)))
12823   (let ((articles (gnus-summary-articles-in-thread)))
12824     (save-excursion
12825       ;; Expand the thread.
12826       (gnus-summary-show-thread)
12827       ;; Mark all the articles.
12828       (while articles
12829         (gnus-summary-goto-subject (car articles))
12830         (cond ((null unmark)
12831                (gnus-summary-mark-article-as-read gnus-killed-mark))
12832               ((> unmark 0)
12833                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12834               (t
12835                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12836         (setq articles (cdr articles))))
12837     ;; Hide killed subtrees.
12838     (and (null unmark)
12839          gnus-thread-hide-killed
12840          (gnus-summary-hide-thread))
12841     ;; If marked as read, go to next unread subject.
12842     (if (null unmark)
12843         ;; Go to next unread subject.
12844         (gnus-summary-next-subject 1 t)))
12845   (gnus-set-mode-line 'summary))
12846
12847 ;; Summary sorting commands
12848
12849 (defun gnus-summary-sort-by-number (&optional reverse)
12850   "Sort summary buffer by article number.
12851 Argument REVERSE means reverse order."
12852   (interactive "P")
12853   (gnus-summary-sort 'number reverse))
12854
12855 (defun gnus-summary-sort-by-author (&optional reverse)
12856   "Sort summary buffer by author name alphabetically.
12857 If case-fold-search is non-nil, case of letters is ignored.
12858 Argument REVERSE means reverse order."
12859   (interactive "P")
12860   (gnus-summary-sort 'author reverse))
12861
12862 (defun gnus-summary-sort-by-subject (&optional reverse)
12863   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12864 If case-fold-search is non-nil, case of letters is ignored.
12865 Argument REVERSE means reverse order."
12866   (interactive "P")
12867   (gnus-summary-sort 'subject reverse))
12868
12869 (defun gnus-summary-sort-by-date (&optional reverse)
12870   "Sort summary buffer by date.
12871 Argument REVERSE means reverse order."
12872   (interactive "P")
12873   (gnus-summary-sort 'date reverse))
12874
12875 (defun gnus-summary-sort-by-score (&optional reverse)
12876   "Sort summary buffer by score.
12877 Argument REVERSE means reverse order."
12878   (interactive "P")
12879   (gnus-summary-sort 'score reverse))
12880
12881 (defun gnus-summary-sort (predicate reverse)
12882   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12883   (gnus-set-global-variables)
12884   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12885          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12886          (gnus-thread-sort-functions
12887           (list
12888            (if (not reverse)
12889                thread
12890              `(lambda (t1 t2)
12891                 (,thread t2 t1)))))
12892          (gnus-article-sort-functions
12893           (list
12894            (if (not reverse)
12895                article
12896              `(lambda (t1 t2)
12897                 (,article t2 t1)))))
12898          (buffer-read-only)
12899          (gnus-summary-prepare-hook nil))
12900     ;; We do the sorting by regenerating the threads.
12901     (gnus-summary-prepare)
12902     ;; Hide subthreads if needed.
12903     (when (and gnus-show-threads gnus-thread-hide-subtree)
12904       (gnus-summary-hide-all-threads)))
12905   ;; If in async mode, we send some info to the backend.
12906   (when gnus-newsgroup-async
12907     (gnus-request-asynchronous
12908      gnus-newsgroup-name gnus-newsgroup-data)))
12909
12910 (defun gnus-sortable-date (date)
12911   "Make sortable string by string-lessp from DATE.
12912 Timezone package is used."
12913   (condition-case ()
12914       (progn
12915         (setq date (inline (timezone-fix-time 
12916                             date nil 
12917                             (aref (inline (timezone-parse-date date)) 4))))
12918         (inline
12919           (timezone-make-sortable-date
12920            (aref date 0) (aref date 1) (aref date 2)
12921            (inline
12922              (timezone-make-time-string
12923               (aref date 3) (aref date 4) (aref date 5))))))
12924     (error "")))
12925   
12926 ;; Summary saving commands.
12927
12928 (defun gnus-summary-save-article (&optional n not-saved)
12929   "Save the current article using the default saver function.
12930 If N is a positive number, save the N next articles.
12931 If N is a negative number, save the N previous articles.
12932 If N is nil and any articles have been marked with the process mark,
12933 save those articles instead.
12934 The variable `gnus-default-article-saver' specifies the saver function."
12935   (interactive "P")
12936   (gnus-set-global-variables)
12937   (let ((articles (gnus-summary-work-articles n))
12938         (save-buffer (save-excursion 
12939                        (nnheader-set-temp-buffer " *Gnus Save*")))
12940         file header article)
12941     (while articles
12942       (setq header (gnus-summary-article-header
12943                     (setq article (pop articles))))
12944       (if (not (vectorp header))
12945           ;; This is a pseudo-article.
12946           (if (assq 'name header)
12947               (gnus-copy-file (cdr (assq 'name header)))
12948             (gnus-message 1 "Article %d is unsaveable" article))
12949         ;; This is a real article.
12950         (save-window-excursion
12951           (gnus-summary-select-article t nil nil article))
12952         (save-excursion
12953           (set-buffer save-buffer)
12954           (erase-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 our save-buffer.
12968             (set-buffer gnus-article-buffer)
12969             (let ((gnus-original-article-buffer save-buffer))
12970               (set-buffer gnus-summary-buffer)
12971               (setq file (funcall
12972                           gnus-default-article-saver
12973                           (cond
12974                            ((not gnus-prompt-before-saving)
12975                             'default)
12976                            ((eq gnus-prompt-before-saving 'always)
12977                             nil)
12978                            (t file)))))))
12979         (gnus-summary-remove-process-mark article)
12980         (unless not-saved
12981           (gnus-summary-set-saved-mark article))))
12982     (gnus-kill-buffer save-buffer)
12983     (gnus-summary-position-point)
12984     n))
12985
12986 (defun gnus-summary-pipe-output (&optional arg)
12987   "Pipe the current article to a subprocess.
12988 If N is a positive number, pipe the N next articles.
12989 If N is a negative number, pipe the N previous articles.
12990 If N is nil and any articles have been marked with the process mark,
12991 pipe those articles instead."
12992   (interactive "P")
12993   (gnus-set-global-variables)
12994   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12995     (gnus-summary-save-article arg t))
12996   (gnus-configure-windows 'pipe))
12997
12998 (defun gnus-summary-save-article-mail (&optional arg)
12999   "Append the current article to an mail file.
13000 If N is a positive number, save the N next articles.
13001 If N is a negative number, save the N previous articles.
13002 If N is nil and any articles have been marked with the process mark,
13003 save those articles instead."
13004   (interactive "P")
13005   (gnus-set-global-variables)
13006   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13007     (gnus-summary-save-article arg)))
13008
13009 (defun gnus-summary-save-article-rmail (&optional arg)
13010   "Append the current article to an rmail file.
13011 If N is a positive number, save the N next articles.
13012 If N is a negative number, save the N previous articles.
13013 If N is nil and any articles have been marked with the process mark,
13014 save those articles instead."
13015   (interactive "P")
13016   (gnus-set-global-variables)
13017   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13018     (gnus-summary-save-article arg)))
13019
13020 (defun gnus-summary-save-article-file (&optional arg)
13021   "Append the current article to a file.
13022 If N is a positive number, save the N next articles.
13023 If N is a negative number, save the N previous articles.
13024 If N is nil and any articles have been marked with the process mark,
13025 save those articles instead."
13026   (interactive "P")
13027   (gnus-set-global-variables)
13028   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13029     (gnus-summary-save-article arg)))
13030
13031 (defun gnus-summary-save-article-body-file (&optional arg)
13032   "Append the current article body to a file.
13033 If N is a positive number, save the N next articles.
13034 If N is a negative number, save the N previous articles.
13035 If N is nil and any articles have been marked with the process mark,
13036 save those articles instead."
13037   (interactive "P")
13038   (gnus-set-global-variables)
13039   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13040     (gnus-summary-save-article arg)))
13041
13042 (defun gnus-get-split-value (methods)
13043   "Return a value based on the split METHODS."
13044   (let (split-name method result match)
13045     (when methods
13046       (save-excursion
13047         (set-buffer gnus-original-article-buffer)
13048         (save-restriction
13049           (nnheader-narrow-to-headers)
13050           (while methods
13051             (goto-char (point-min))
13052             (setq method (pop methods))
13053             (setq match (car method))
13054             (when (cond
13055                    ((stringp match)
13056                     ;; Regular expression.
13057                     (condition-case ()
13058                         (re-search-forward match nil t)
13059                       (error nil)))
13060                    ((gnus-functionp match)
13061                     ;; Function.
13062                     (save-restriction
13063                       (widen)
13064                       (setq result (funcall match gnus-newsgroup-name))))
13065                    ((consp match)
13066                     ;; Form.
13067                     (save-restriction
13068                       (widen)
13069                       (setq result (eval match)))))
13070               (setq split-name (append (cdr method) split-name))
13071               (cond ((stringp result)
13072                      (push result split-name))
13073                     ((consp result)
13074                      (setq split-name (append result split-name)))))))))
13075     split-name))
13076
13077 (defun gnus-read-move-group-name (prompt default articles prefix)
13078   "Read a group name."
13079   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13080          (minibuffer-confirm-incomplete nil) ; XEmacs
13081          group-map
13082          (dum (mapatoms
13083                (lambda (g) 
13084                  (and (boundp g)
13085                       (symbol-name g)
13086                       (memq 'respool
13087                             (assoc (symbol-name
13088                                     (car (gnus-find-method-for-group
13089                                           (symbol-name g))))
13090                                    gnus-valid-select-methods))
13091                       (push (list (symbol-name g)) group-map)))
13092                gnus-active-hashtb))
13093          (prom
13094           (format "%s %s to:"
13095                   prompt
13096                   (if (> (length articles) 1)
13097                       (format "these %d articles" (length articles))
13098                     "this article")))
13099          (to-newsgroup
13100           (cond
13101            ((null split-name)
13102             (gnus-completing-read default prom
13103                                   group-map nil nil prefix
13104                                   'gnus-group-history))
13105            ((= 1 (length split-name))
13106             (gnus-completing-read (car split-name) prom group-map
13107                                   nil nil nil
13108                                   'gnus-group-history))
13109            (t
13110             (gnus-completing-read nil prom 
13111                                   (mapcar (lambda (el) (list el))
13112                                           (nreverse split-name))
13113                                   nil nil nil
13114                                   'gnus-group-history)))))
13115     (when to-newsgroup
13116       (if (or (string= to-newsgroup "")
13117               (string= to-newsgroup prefix))
13118           (setq to-newsgroup (or default "")))
13119       (or (gnus-active to-newsgroup)
13120           (gnus-activate-group to-newsgroup)
13121           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13122                                      to-newsgroup))
13123               (or (gnus-request-create-group to-newsgroup)
13124                   (error "Couldn't create group %s" to-newsgroup)))
13125           (error "No such group: %s" to-newsgroup)))
13126     to-newsgroup))
13127
13128 (defun gnus-read-save-file-name (prompt default-name)
13129   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13130          (file
13131           ;; Let the split methods have their say.
13132           (cond
13133            ;; No split name was found.
13134            ((null split-name)
13135             (read-file-name
13136              (concat prompt " (default "
13137                      (file-name-nondirectory default-name) ") ")
13138              (file-name-directory default-name)
13139              default-name))
13140            ;; A single split name was found
13141            ((= 1 (length split-name))
13142             (read-file-name
13143              (concat prompt " (default " (car split-name) ") ")
13144              gnus-article-save-directory
13145              (concat gnus-article-save-directory (car split-name))))
13146            ;; A list of splits was found.
13147            (t
13148             (setq split-name (nreverse split-name))
13149             (let (result)
13150               (let ((file-name-history (nconc split-name file-name-history)))
13151                 (setq result
13152                       (read-file-name
13153                        (concat prompt " (`M-p' for defaults) ")
13154                        gnus-article-save-directory
13155                        (car split-name))))
13156               (car (push result file-name-history)))))))
13157     ;; If we have read a directory, we append the default file name.
13158     (when (file-directory-p file)
13159       (setq file (concat (file-name-as-directory file)
13160                          (file-name-nondirectory default-name))))
13161     ;; Possibly translate some charaters.
13162     (nnheader-translate-file-chars file)))
13163
13164 (defun gnus-article-archive-name (group)
13165   "Return the first instance of an \"Archive-name\" in the current buffer."
13166   (let ((case-fold-search t))
13167     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13168       (match-string 1))))
13169
13170 (defun gnus-summary-save-in-rmail (&optional filename)
13171   "Append this article to Rmail file.
13172 Optional argument FILENAME specifies file name.
13173 Directory to save to is default to `gnus-article-save-directory'."
13174   (interactive)
13175   (gnus-set-global-variables)
13176   (let ((default-name
13177           (funcall gnus-rmail-save-name gnus-newsgroup-name
13178                    gnus-current-headers gnus-newsgroup-last-rmail)))
13179     (setq filename
13180           (cond ((eq filename 'default)
13181                  default-name)
13182                 (filename filename)
13183                 (t (gnus-read-save-file-name
13184                     "Save in rmail file:" default-name))))
13185     (gnus-make-directory (file-name-directory filename))
13186     (gnus-eval-in-buffer-window gnus-original-article-buffer
13187       (save-excursion
13188         (save-restriction
13189           (widen)
13190           (gnus-output-to-rmail filename))))
13191     ;; Remember the directory name to save articles
13192     (setq gnus-newsgroup-last-rmail filename)))
13193
13194 (defun gnus-summary-save-in-mail (&optional filename)
13195   "Append this article to Unix mail file.
13196 Optional argument FILENAME specifies file name.
13197 Directory to save to is default to `gnus-article-save-directory'."
13198   (interactive)
13199   (gnus-set-global-variables)
13200   (let ((default-name
13201           (funcall gnus-mail-save-name gnus-newsgroup-name
13202                    gnus-current-headers gnus-newsgroup-last-mail)))
13203     (setq filename
13204           (cond ((eq filename 'default)
13205                  default-name)
13206                 (filename filename)
13207                 (t (gnus-read-save-file-name
13208                     "Save in Unix mail file:" default-name))))
13209     (setq filename
13210           (expand-file-name filename
13211                             (and default-name
13212                                  (file-name-directory default-name))))
13213     (gnus-make-directory (file-name-directory filename))
13214     (gnus-eval-in-buffer-window gnus-original-article-buffer
13215       (save-excursion
13216         (save-restriction
13217           (widen)
13218           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13219               (gnus-output-to-rmail filename)
13220             (let ((mail-use-rfc822 t))
13221               (rmail-output filename 1 t t))))))
13222     ;; Remember the directory name to save articles.
13223     (setq gnus-newsgroup-last-mail filename)))
13224
13225 (defun gnus-summary-save-in-file (&optional filename)
13226   "Append this article to file.
13227 Optional argument FILENAME specifies file name.
13228 Directory to save to is default to `gnus-article-save-directory'."
13229   (interactive)
13230   (gnus-set-global-variables)
13231   (let ((default-name
13232           (funcall gnus-file-save-name gnus-newsgroup-name
13233                    gnus-current-headers gnus-newsgroup-last-file)))
13234     (setq filename
13235           (cond ((eq filename 'default)
13236                  default-name)
13237                 (filename filename)
13238                 (t (gnus-read-save-file-name
13239                     "Save in file:" default-name))))
13240     (gnus-make-directory (file-name-directory filename))
13241     (gnus-eval-in-buffer-window gnus-original-article-buffer
13242       (save-excursion
13243         (save-restriction
13244           (widen)
13245           (gnus-output-to-file filename))))
13246     ;; Remember the directory name to save articles.
13247     (setq gnus-newsgroup-last-file filename)))
13248
13249 (defun gnus-summary-save-body-in-file (&optional filename)
13250   "Append this article body to a file.
13251 Optional argument FILENAME specifies file name.
13252 The directory to save in defaults to `gnus-article-save-directory'."
13253   (interactive)
13254   (gnus-set-global-variables)
13255   (let ((default-name
13256           (funcall gnus-file-save-name gnus-newsgroup-name
13257                    gnus-current-headers gnus-newsgroup-last-file)))
13258     (setq filename
13259           (cond ((eq filename 'default)
13260                  default-name)
13261                 (filename filename)
13262                 (t (gnus-read-save-file-name
13263                     "Save body in file:" default-name))))
13264     (gnus-make-directory (file-name-directory filename))
13265     (gnus-eval-in-buffer-window gnus-article-buffer
13266       (save-excursion
13267         (save-restriction
13268           (widen)
13269           (goto-char (point-min))
13270           (and (search-forward "\n\n" nil t)
13271                (narrow-to-region (point) (point-max)))
13272           (gnus-output-to-file filename))))
13273     ;; Remember the directory name to save articles.
13274     (setq gnus-newsgroup-last-file filename)))
13275
13276 (defun gnus-summary-save-in-pipe (&optional command)
13277   "Pipe this article to subprocess."
13278   (interactive)
13279   (gnus-set-global-variables)
13280   (setq command
13281         (cond ((eq command 'default)
13282                gnus-last-shell-command)
13283               (command command)
13284               (t (read-string "Shell command on article: "
13285                               gnus-last-shell-command))))
13286   (if (string-equal command "")
13287       (setq command gnus-last-shell-command))
13288   (gnus-eval-in-buffer-window 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 (gnus-hidden-arg))
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                 (inhibit-point-motion-hooks t)
13923                 want-list beg)
13924             ;; First we narrow to just the headers.
13925             (widen)
13926             (goto-char (point-min))
13927             ;; Hide any "From " lines at the beginning of (mail) articles.
13928             (while (looking-at "From ")
13929               (forward-line 1))
13930             (unless (bobp)
13931               (if delete
13932                   (delete-region (point-min) (point))
13933                 (gnus-hide-text (point-min) (point) props)))
13934             ;; Then treat the rest of the header lines.
13935             (narrow-to-region
13936              (point)
13937              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13938             ;; Then we use the two regular expressions
13939             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13940             ;; select which header lines is to remain visible in the
13941             ;; article buffer.
13942             (goto-char (point-min))
13943             (while (re-search-forward "^[^ \t]*:" nil t)
13944               (beginning-of-line)
13945               ;; We add the headers we want to keep to a list and delete
13946               ;; them from the buffer.
13947               (gnus-put-text-property 
13948                (point) (1+ (point)) 'message-rank
13949                (if (or (and visible (looking-at visible))
13950                        (and ignored
13951                             (not (looking-at ignored))))
13952                    (gnus-article-header-rank) 
13953                  (+ 2 max)))
13954               (forward-line 1))
13955             (message-sort-headers-1)
13956             (when (setq beg (text-property-any 
13957                              (point-min) (point-max) 'message-rank (+ 2 max)))
13958               ;; We make the unwanted headers invisible.
13959               (if delete
13960                   (delete-region beg (point-max))
13961                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13962                 (gnus-hide-text-type beg (point-max) 'headers))
13963               ;; Work around XEmacs lossage.
13964               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13965
13966 (defun gnus-article-hide-boring-headers (&optional arg)
13967   "Toggle hiding of headers that aren't very interesting.
13968 If given a negative prefix, always show; if given a positive prefix,
13969 always hide."
13970   (interactive (gnus-hidden-arg))
13971   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13972     (save-excursion
13973       (set-buffer gnus-article-buffer)
13974       (save-restriction
13975         (let ((buffer-read-only nil)
13976               (list gnus-boring-article-headers)
13977               (inhibit-point-motion-hooks t)
13978               elem)
13979           (nnheader-narrow-to-headers)
13980           (while list
13981             (setq elem (pop list))
13982             (goto-char (point-min))
13983             (cond
13984              ;; Hide empty headers.
13985              ((eq elem 'empty)
13986               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13987                 (forward-line -1)
13988                 (gnus-hide-text-type
13989                  (progn (beginning-of-line) (point))
13990                  (progn 
13991                    (end-of-line)
13992                    (if (re-search-forward "^[^ \t]" nil t)
13993                        (match-beginning 0)
13994                      (point-max)))
13995                  'boring-headers)))
13996              ;; Hide boring Newsgroups header.
13997              ((eq elem 'newsgroups)
13998               (when (equal (message-fetch-field "newsgroups")
13999                            (gnus-group-real-name gnus-newsgroup-name))
14000                 (gnus-article-hide-header "newsgroups")))
14001              ((eq elem 'followup-to)
14002               (when (equal (message-fetch-field "followup-to")
14003                            (message-fetch-field "newsgroups"))
14004                 (gnus-article-hide-header "followup-to")))
14005              ((eq elem 'reply-to)
14006               (let ((from (message-fetch-field "from"))
14007                     (reply-to (message-fetch-field "reply-to")))
14008                 (when (and
14009                        from reply-to
14010                        (equal 
14011                         (nth 1 (funcall gnus-extract-address-components from))
14012                         (nth 1 (funcall gnus-extract-address-components
14013                                         reply-to))))
14014                   (gnus-article-hide-header "reply-to"))))
14015              ((eq elem 'date)
14016               (let ((date (message-fetch-field "date")))
14017                 (when (and date
14018                            (< (gnus-days-between date (current-time-string))
14019                               4))
14020                   (gnus-article-hide-header "date")))))))))))
14021
14022 (defun gnus-article-hide-header (header)
14023   (save-excursion
14024     (goto-char (point-min))
14025     (when (re-search-forward (concat "^" header ":") nil t)
14026       (gnus-hide-text-type
14027        (progn (beginning-of-line) (point))
14028        (progn 
14029          (end-of-line)
14030          (if (re-search-forward "^[^ \t]" nil t)
14031              (match-beginning 0)
14032            (point-max)))
14033        'boring-headers))))
14034
14035 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14036 (defun gnus-article-treat-overstrike ()
14037   "Translate overstrikes into bold text."
14038   (interactive)
14039   (save-excursion
14040     (set-buffer gnus-article-buffer)
14041     (let ((buffer-read-only nil))
14042       (while (search-forward "\b" nil t)
14043         (let ((next (following-char))
14044               (previous (char-after (- (point) 2))))
14045           (cond 
14046            ((eq next previous)
14047             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14048             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14049            ((eq next ?_)
14050             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14051             (gnus-put-text-property
14052              (- (point) 2) (1- (point)) 'face 'underline))
14053            ((eq previous ?_)
14054             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14055             (gnus-put-text-property
14056              (point) (1+ (point))       'face 'underline))))))))
14057
14058 (defun gnus-article-word-wrap ()
14059   "Format too long lines."
14060   (interactive)
14061   (save-excursion
14062     (set-buffer gnus-article-buffer)
14063     (let ((buffer-read-only nil))
14064       (widen)
14065       (goto-char (point-min))
14066       (search-forward "\n\n" nil t)
14067       (end-of-line 1)
14068       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14069             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14070             (adaptive-fill-mode t))
14071         (while (not (eobp))
14072           (and (>= (current-column) (min fill-column (window-width)))
14073                (/= (preceding-char) ?:)
14074                (fill-paragraph nil))
14075           (end-of-line 2))))))
14076
14077 (defun gnus-article-remove-cr ()
14078   "Remove carriage returns from an article."
14079   (interactive)
14080   (save-excursion
14081     (set-buffer gnus-article-buffer)
14082     (let ((buffer-read-only nil))
14083       (goto-char (point-min))
14084       (while (search-forward "\r" nil t)
14085         (replace-match "" t t)))))
14086
14087 (defun gnus-article-remove-trailing-blank-lines ()
14088   "Remove all trailing blank lines from the article."
14089   (interactive)
14090   (save-excursion
14091     (set-buffer gnus-article-buffer)
14092     (let ((buffer-read-only nil))
14093       (goto-char (point-max))
14094       (delete-region
14095        (point)
14096        (progn
14097          (while (looking-at "^[ \t]*$")
14098            (forward-line -1))
14099          (forward-line 1)
14100          (point))))))
14101
14102 (defun gnus-article-display-x-face (&optional force)
14103   "Look for an X-Face header and display it if present."
14104   (interactive (list 'force))
14105   (save-excursion
14106     (set-buffer gnus-article-buffer)
14107     ;; Delete the old process, if any.
14108     (when (process-status "gnus-x-face")
14109       (delete-process "gnus-x-face"))
14110     (let ((inhibit-point-motion-hooks t)
14111           (case-fold-search nil)
14112           from)
14113       (save-restriction
14114         (nnheader-narrow-to-headers)
14115         (setq from (message-fetch-field "from"))
14116         (goto-char (point-min))
14117         (when (and gnus-article-x-face-command
14118                    (or force
14119                        ;; Check whether this face is censored.
14120                        (not gnus-article-x-face-too-ugly)
14121                        (and gnus-article-x-face-too-ugly from
14122                             (not (string-match gnus-article-x-face-too-ugly
14123                                                from))))
14124                    ;; Has to be present.
14125                    (re-search-forward "^X-Face: " nil t))
14126           ;; We now have the area of the buffer where the X-Face is stored.
14127           (let ((beg (point))
14128                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14129             ;; We display the face.
14130             (if (symbolp gnus-article-x-face-command)
14131                 ;; The command is a lisp function, so we call it.
14132                 (if (gnus-functionp gnus-article-x-face-command)
14133                     (funcall gnus-article-x-face-command beg end)
14134                   (error "%s is not a function" gnus-article-x-face-command))
14135               ;; The command is a string, so we interpret the command
14136               ;; as a, well, command, and fork it off.
14137               (let ((process-connection-type nil))
14138                 (process-kill-without-query
14139                  (start-process
14140                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14141                 (process-send-region "gnus-x-face" beg end)
14142                 (process-send-eof "gnus-x-face")))))))))
14143
14144 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14145 (defun gnus-decode-rfc1522 ()
14146   "Hack to remove QP encoding from headers."
14147   (let ((case-fold-search t)
14148         (inhibit-point-motion-hooks t)
14149         (buffer-read-only nil)
14150         string)
14151     (save-restriction
14152       (narrow-to-region
14153        (goto-char (point-min))
14154        (or (search-forward "\n\n" nil t) (point-max)))
14155
14156       (while (re-search-forward 
14157               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14158         (setq string (match-string 1))
14159         (narrow-to-region (match-beginning 0) (match-end 0))
14160         (delete-region (point-min) (point-max))
14161         (insert string)
14162         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14163         (subst-char-in-region (point-min) (point-max) ?_ ? )
14164         (widen)
14165         (goto-char (point-min))))))
14166
14167 (defun gnus-article-de-quoted-unreadable (&optional force)
14168   "Do a naive translation of a quoted-printable-encoded article.
14169 This is in no way, shape or form meant as a replacement for real MIME
14170 processing, but is simply a stop-gap measure until MIME support is
14171 written.
14172 If FORCE, decode the article whether it is marked as quoted-printable
14173 or not."
14174   (interactive (list 'force))
14175   (save-excursion
14176     (set-buffer gnus-article-buffer)
14177     (let ((case-fold-search t)
14178           (buffer-read-only nil)
14179           (type (gnus-fetch-field "content-transfer-encoding")))
14180       (gnus-decode-rfc1522)
14181       (when (or force
14182                 (and type (string-match "quoted-printable" (downcase type))))
14183         (goto-char (point-min))
14184         (search-forward "\n\n" nil 'move)
14185         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14186
14187 (defun gnus-mime-decode-quoted-printable (from to)
14188   "Decode Quoted-Printable in the region between FROM and TO."
14189   (interactive "r")
14190   (goto-char from)
14191   (while (search-forward "=" to t)
14192     (cond ((eq (following-char) ?\n)
14193            (delete-char -1)
14194            (delete-char 1))
14195           ((looking-at "[0-9A-F][0-9A-F]")
14196            (subst-char-in-region
14197             (1- (point)) (point) ?=
14198             (hexl-hex-string-to-integer
14199              (buffer-substring (point) (+ 2 (point)))))
14200            (delete-char 2))
14201           ((looking-at "=")
14202            (delete-char 1))
14203           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14204
14205 (defun gnus-article-hide-pgp (&optional arg)
14206   "Toggle hiding of any PGP headers and signatures in the current article.
14207 If given a negative prefix, always show; if given a positive prefix,
14208 always hide."
14209   (interactive (gnus-hidden-arg))
14210   (unless (gnus-article-check-hidden-text 'pgp arg)
14211     (save-excursion
14212       (set-buffer gnus-article-buffer)
14213       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14214             buffer-read-only beg end)
14215         (widen)
14216         (goto-char (point-min))
14217         ;; Hide the "header".
14218         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14219              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14220         (setq beg (point))
14221         ;; Hide the actual signature.
14222         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14223              (setq end (1+ (match-beginning 0)))
14224              (gnus-hide-text
14225               end
14226               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14227                   (match-end 0)
14228                 ;; Perhaps we shouldn't hide to the end of the buffer
14229                 ;; if there is no end to the signature?
14230                 (point-max))
14231               props))
14232         ;; Hide "- " PGP quotation markers.
14233         (when (and beg end)
14234           (narrow-to-region beg end)
14235           (goto-char (point-min))
14236           (while (re-search-forward "^- " nil t)
14237             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14238           (widen))))))
14239
14240 (defun gnus-article-hide-signature (&optional arg)
14241   "Hide the signature in the current article.
14242 If given a negative prefix, always show; if given a positive prefix,
14243 always hide."
14244   (interactive (gnus-hidden-arg))
14245   (unless (gnus-article-check-hidden-text 'signature arg)
14246     (save-excursion
14247       (set-buffer gnus-article-buffer)
14248       (save-restriction
14249         (let ((buffer-read-only nil))
14250           (when (gnus-narrow-to-signature)
14251             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14252
14253 (defun gnus-article-strip-leading-blank-lines ()
14254   "Remove all blank lines from the beginning of the article."
14255   (interactive)
14256   (save-excursion
14257     (set-buffer gnus-article-buffer)
14258     (let (buffer-read-only)
14259       (goto-char (point-min))
14260       (when (search-forward "\n\n" nil t)
14261         (while (looking-at "[ \t]$")
14262           (gnus-delete-line))))))
14263
14264 (defvar mime::preview/content-list)
14265 (defvar mime::preview-content-info/point-min)
14266 (defun gnus-narrow-to-signature ()
14267   "Narrow to the signature."
14268   (widen)
14269   (if (and (boundp 'mime::preview/content-list)
14270            mime::preview/content-list)
14271       (let ((pcinfo (car (last mime::preview/content-list))))
14272         (narrow-to-region
14273          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14274          (point-max))
14275         t))
14276   (goto-char (point-max))
14277   (when (re-search-backward gnus-signature-separator nil t)
14278     (forward-line 1)
14279     (when (or (null gnus-signature-limit)
14280               (and (numberp gnus-signature-limit)
14281                    (< (- (point-max) (point)) gnus-signature-limit))
14282               (and (gnus-functionp gnus-signature-limit)
14283                    (funcall gnus-signature-limit))
14284               (and (stringp gnus-signature-limit)
14285                    (not (re-search-forward gnus-signature-limit nil t))))
14286       (narrow-to-region (point) (point-max))
14287       t)))
14288
14289 (defun gnus-hidden-arg ()
14290   "Return the current prefix arg as a number, or 0 if no prefix."
14291   (list (if current-prefix-arg
14292             (prefix-numeric-value current-prefix-arg)
14293           0)))
14294
14295 (defun gnus-article-check-hidden-text (type arg)
14296   "Return nil if hiding is necessary.
14297 Arg can be nil or a number.  Nil and positive means hide, negative
14298 means show, 0 means toggle."
14299   (save-excursion
14300     (set-buffer gnus-article-buffer)
14301     (let ((hide (gnus-article-hidden-text-p type)))
14302       (cond
14303        ((or (null arg)
14304             (> arg 0))
14305         nil)
14306        ((< arg 0)
14307         (gnus-article-show-hidden-text type))
14308        (t
14309         (if (eq hide 'hidden)
14310             (gnus-article-show-hidden-text type)
14311           nil))))))
14312
14313 (defun gnus-article-hidden-text-p (type)
14314   "Say whether the current buffer contains hidden text of type TYPE."
14315   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14316     (when pos
14317       (if (get-text-property pos 'invisible)
14318           'hidden
14319         'shown))))
14320
14321 (defun gnus-article-hide (&optional arg force)
14322   "Hide all the gruft in the current article.
14323 This means that PGP stuff, signatures, cited text and (some)
14324 headers will be hidden.
14325 If given a prefix, show the hidden text instead."
14326   (interactive (list current-prefix-arg 'force))
14327   (gnus-article-hide-headers arg)
14328   (gnus-article-hide-pgp arg)
14329   (gnus-article-hide-citation-maybe arg force)
14330   (gnus-article-hide-signature arg))
14331
14332 (defun gnus-article-show-hidden-text (type &optional hide)
14333   "Show all hidden text of type TYPE.
14334 If HIDE, hide the text instead."
14335   (save-excursion
14336     (set-buffer gnus-article-buffer)
14337     (let ((buffer-read-only nil)
14338           (inhibit-point-motion-hooks t)
14339           (beg (point-min)))
14340       (while (gnus-goto-char (text-property-any
14341                               beg (point-max) 'gnus-type type))
14342         (setq beg (point))
14343         (forward-char)
14344         (if hide
14345             (gnus-hide-text beg (point) gnus-hidden-properties)
14346           (gnus-unhide-text beg (point)))
14347         (setq beg (point)))
14348       t)))
14349
14350 (defvar gnus-article-time-units
14351   `((year . ,(* 365.25 24 60 60))
14352     (week . ,(* 7 24 60 60))
14353     (day . ,(* 24 60 60))
14354     (hour . ,(* 60 60))
14355     (minute . 60)
14356     (second . 1))
14357   "Mapping from time units to seconds.")
14358
14359 (defun gnus-article-date-ut (&optional type highlight)
14360   "Convert DATE date to universal time in the current article.
14361 If TYPE is `local', convert to local time; if it is `lapsed', output
14362 how much time has lapsed since DATE."
14363   (interactive (list 'ut t))
14364   (let* ((header (or gnus-current-headers
14365                      (gnus-summary-article-header) ""))
14366          (date (and (vectorp header) (mail-header-date header)))
14367          (date-regexp "^Date: \\|^X-Sent: ")
14368          (now (current-time))
14369          (inhibit-point-motion-hooks t)
14370          bface eface)
14371     (when (and date (not (string= date "")))
14372       (save-excursion
14373         (set-buffer gnus-article-buffer)
14374         (save-restriction
14375           (nnheader-narrow-to-headers)
14376           (let ((buffer-read-only nil))
14377             ;; Delete any old Date headers.
14378             (if (re-search-forward date-regexp nil t)
14379                 (progn
14380                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14381                         eface (get-text-property (1- (gnus-point-at-eol))
14382                                                  'face))
14383                   (message-remove-header date-regexp t)
14384                   (beginning-of-line))
14385               (goto-char (point-max)))
14386             (insert (gnus-make-date-line date type))
14387             ;; Do highlighting.
14388             (forward-line -1)
14389             (when (and (gnus-visual-p 'article-highlight 'highlight)
14390                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14391               (gnus-put-text-property (match-beginning 1) (match-end 1)
14392                                  'face bface)
14393               (gnus-put-text-property (match-beginning 2) (match-end 2)
14394                                  'face eface))))))))
14395
14396 (defun gnus-make-date-line (date type)
14397   "Return a DATE line of TYPE."
14398   (cond
14399    ;; Convert to the local timezone.  We have to slap a
14400    ;; `condition-case' round the calls to the timezone
14401    ;; functions since they aren't particularly resistant to
14402    ;; buggy dates.
14403    ((eq type 'local)
14404     (concat "Date: " (condition-case ()
14405                          (timezone-make-date-arpa-standard date)
14406                        (error date))
14407             "\n"))
14408    ;; Convert to Universal Time.
14409    ((eq type 'ut)
14410     (concat "Date: "
14411             (condition-case ()
14412                 (timezone-make-date-arpa-standard date nil "UT")
14413               (error date))
14414             "\n"))
14415    ;; Get the original date from the article.
14416    ((eq type 'original)
14417     (concat "Date: " date "\n"))
14418    ;; Do an X-Sent lapsed format.
14419    ((eq type 'lapsed)
14420     ;; If the date is seriously mangled, the timezone
14421     ;; functions are liable to bug out, so we condition-case
14422     ;; the entire thing.
14423     (let* ((now (current-time))
14424            (real-time
14425             (condition-case ()
14426                 (gnus-time-minus
14427                  (gnus-encode-date
14428                   (timezone-make-date-arpa-standard
14429                    (current-time-string now)
14430                    (current-time-zone now) "UT"))
14431                  (gnus-encode-date
14432                   (timezone-make-date-arpa-standard
14433                    date nil "UT")))
14434               (error '(0 0))))
14435            (real-sec (+ (* (float (car real-time)) 65536)
14436                         (cadr real-time)))
14437            (sec (abs real-sec))
14438            num prev)
14439       (cond
14440        ((equal real-time '(0 0))
14441         "X-Sent: Unknown\n")
14442        ((zerop sec)
14443         "X-Sent: Now\n")
14444        (t
14445         (concat
14446          "X-Sent: "
14447          ;; This is a bit convoluted, but basically we go
14448          ;; through the time units for years, weeks, etc,
14449          ;; and divide things to see whether that results
14450          ;; in positive answers.
14451          (mapconcat
14452           (lambda (unit)
14453             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14454                 ;; The (remaining) seconds are too few to
14455                 ;; be divided into this time unit.
14456                 ""
14457               ;; It's big enough, so we output it.
14458               (setq sec (- sec (* num (cdr unit))))
14459               (prog1
14460                   (concat (if prev ", " "") (int-to-string
14461                                              (floor num))
14462                           " " (symbol-name (car unit)) 
14463                           (if (> num 1) "s" ""))
14464                 (setq prev t))))
14465           gnus-article-time-units "")
14466          ;; If dates are odd, then it might appear like the
14467          ;; article was sent in the future.
14468          (if (> real-sec 0)
14469              " ago\n"
14470            " in the future\n"))))))
14471    (t
14472     (error "Unknown conversion type: %s" type))))
14473
14474 (defun gnus-article-date-local (&optional highlight)
14475   "Convert the current article date to the local timezone."
14476   (interactive (list t))
14477   (gnus-article-date-ut 'local highlight))
14478
14479 (defun gnus-article-date-original (&optional highlight)
14480   "Convert the current article date to what it was originally.
14481 This is only useful if you have used some other date conversion
14482 function and want to see what the date was before converting."
14483   (interactive (list t))
14484   (gnus-article-date-ut 'original highlight))
14485
14486 (defun gnus-article-date-lapsed (&optional highlight)
14487   "Convert the current article date to time lapsed since it was sent."
14488   (interactive (list t))
14489   (gnus-article-date-ut 'lapsed highlight))
14490
14491 (defun gnus-article-maybe-highlight ()
14492   "Do some article highlighting if `gnus-visual' is non-nil."
14493   (if (gnus-visual-p 'article-highlight 'highlight)
14494       (gnus-article-highlight-some)))
14495
14496 ;;; Article savers.
14497
14498 (defun gnus-output-to-rmail (file-name)
14499   "Append the current article to an Rmail file named FILE-NAME."
14500   (require 'rmail)
14501   ;; Most of these codes are borrowed from rmailout.el.
14502   (setq file-name (expand-file-name file-name))
14503   (setq rmail-default-rmail-file file-name)
14504   (let ((artbuf (current-buffer))
14505         (tmpbuf (get-buffer-create " *Gnus-output*")))
14506     (save-excursion
14507       (or (get-file-buffer file-name)
14508           (file-exists-p file-name)
14509           (if (gnus-yes-or-no-p
14510                (concat "\"" file-name "\" does not exist, create it? "))
14511               (let ((file-buffer (create-file-buffer file-name)))
14512                 (save-excursion
14513                   (set-buffer file-buffer)
14514                   (rmail-insert-rmail-file-header)
14515                   (let ((require-final-newline nil))
14516                     (write-region (point-min) (point-max) file-name t 1)))
14517                 (kill-buffer file-buffer))
14518             (error "Output file does not exist")))
14519       (set-buffer tmpbuf)
14520       (buffer-disable-undo (current-buffer))
14521       (erase-buffer)
14522       (insert-buffer-substring artbuf)
14523       (gnus-convert-article-to-rmail)
14524       ;; Decide whether to append to a file or to an Emacs buffer.
14525       (let ((outbuf (get-file-buffer file-name)))
14526         (if (not outbuf)
14527             (append-to-file (point-min) (point-max) file-name)
14528           ;; File has been visited, in buffer OUTBUF.
14529           (set-buffer outbuf)
14530           (let ((buffer-read-only nil)
14531                 (msg (and (boundp 'rmail-current-message)
14532                           (symbol-value 'rmail-current-message))))
14533             ;; If MSG is non-nil, buffer is in RMAIL mode.
14534             (if msg
14535                 (progn (widen)
14536                        (narrow-to-region (point-max) (point-max))))
14537             (insert-buffer-substring tmpbuf)
14538             (if msg
14539                 (progn
14540                   (goto-char (point-min))
14541                   (widen)
14542                   (search-backward "\^_")
14543                   (narrow-to-region (point) (point-max))
14544                   (goto-char (1+ (point-min)))
14545                   (rmail-count-new-messages t)
14546                   (rmail-show-message msg)))))))
14547     (kill-buffer tmpbuf)))
14548
14549 (defun gnus-output-to-file (file-name)
14550   "Append the current article to a file named FILE-NAME."
14551   (let ((artbuf (current-buffer)))
14552     (nnheader-temp-write nil
14553       (insert-buffer-substring artbuf)
14554       ;; Append newline at end of the buffer as separator, and then
14555       ;; save it to file.
14556       (goto-char (point-max))
14557       (insert "\n")
14558       (append-to-file (point-min) (point-max) file-name))))
14559
14560 (defun gnus-convert-article-to-rmail ()
14561   "Convert article in current buffer to Rmail message format."
14562   (let ((buffer-read-only nil))
14563     ;; Convert article directly into Babyl format.
14564     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14565     (goto-char (point-min))
14566     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14567     (while (search-forward "\n\^_" nil t) ;single char
14568       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14569     (goto-char (point-max))
14570     (insert "\^_")))
14571
14572 (defun gnus-narrow-to-page (&optional arg)
14573   "Narrow the article buffer to a page.
14574 If given a numerical ARG, move forward ARG pages."
14575   (interactive "P")
14576   (setq arg (if arg (prefix-numeric-value arg) 0))
14577   (save-excursion
14578     (set-buffer gnus-article-buffer)
14579     (goto-char (point-min))
14580     (widen)
14581     (when (gnus-visual-p 'page-marker)
14582       (let ((buffer-read-only nil))
14583         (gnus-remove-text-with-property 'gnus-prev)
14584         (gnus-remove-text-with-property 'gnus-next)))
14585     (when
14586         (cond ((< arg 0)
14587                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14588               ((> arg 0)
14589                (re-search-forward page-delimiter nil 'move arg)))
14590       (goto-char (match-end 0)))
14591     (narrow-to-region
14592      (point)
14593      (if (re-search-forward page-delimiter nil 'move)
14594          (match-beginning 0)
14595        (point)))
14596     (when (and (gnus-visual-p 'page-marker)
14597                (not (= (point-min) 1)))
14598       (save-excursion
14599         (goto-char (point-min))
14600         (gnus-insert-prev-page-button)))
14601     (when (and (gnus-visual-p 'page-marker)
14602                (not (= (1- (point-max)) (buffer-size))))
14603       (save-excursion
14604         (goto-char (point-max))
14605         (gnus-insert-next-page-button)))))
14606
14607 ;; Article mode commands
14608
14609 (defun gnus-article-goto-next-page ()
14610   "Show the next page of the article."
14611   (interactive)
14612   (when (gnus-article-next-page)
14613     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14614
14615 (defun gnus-article-goto-prev-page ()
14616   "Show the next page of the article."
14617   (interactive)
14618   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14619     (gnus-article-prev-page nil)))
14620
14621 (defun gnus-article-next-page (&optional lines)
14622   "Show the next page of the current article.
14623 If end of article, return non-nil.  Otherwise return nil.
14624 Argument LINES specifies lines to be scrolled up."
14625   (interactive "p")
14626   (move-to-window-line -1)
14627   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14628   (if (save-excursion
14629         (end-of-line)
14630         (and (pos-visible-in-window-p)  ;Not continuation line.
14631              (eobp)))
14632       ;; Nothing in this page.
14633       (if (or (not gnus-break-pages)
14634               (save-excursion
14635                 (save-restriction
14636                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14637           t                             ;Nothing more.
14638         (gnus-narrow-to-page 1)         ;Go to next page.
14639         nil)
14640     ;; More in this page.
14641     (condition-case ()
14642         (scroll-up lines)
14643       (end-of-buffer
14644        ;; Long lines may cause an end-of-buffer error.
14645        (goto-char (point-max))))
14646     (move-to-window-line 0)
14647     nil))
14648
14649 (defun gnus-article-prev-page (&optional lines)
14650   "Show previous page of current article.
14651 Argument LINES specifies lines to be scrolled down."
14652   (interactive "p")
14653   (move-to-window-line 0)
14654   (if (and gnus-break-pages
14655            (bobp)
14656            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14657       (progn
14658         (gnus-narrow-to-page -1)        ;Go to previous page.
14659         (goto-char (point-max))
14660         (recenter -1))
14661     (prog1
14662         (condition-case ()
14663             (scroll-down lines)
14664           (error nil))
14665       (move-to-window-line 0))))
14666
14667 (defun gnus-article-refer-article ()
14668   "Read article specified by message-id around point."
14669   (interactive)
14670   (let ((point (point)))
14671     (search-forward ">" nil t)          ;Move point to end of "<....>".
14672     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14673         (let ((message-id (match-string 1)))
14674           (goto-char point)
14675           (set-buffer gnus-summary-buffer)
14676           (gnus-summary-refer-article message-id))
14677       (goto-char (point))
14678       (error "No references around point"))))
14679
14680 (defun gnus-article-show-summary ()
14681   "Reconfigure windows to show summary buffer."
14682   (interactive)
14683   (gnus-configure-windows 'article)
14684   (gnus-summary-goto-subject gnus-current-article))
14685
14686 (defun gnus-article-describe-briefly ()
14687   "Describe article mode commands briefly."
14688   (interactive)
14689   (gnus-message 6
14690                 (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")))
14691
14692 (defun gnus-article-summary-command ()
14693   "Execute the last keystroke in the summary buffer."
14694   (interactive)
14695   (let ((obuf (current-buffer))
14696         (owin (current-window-configuration))
14697         func)
14698     (switch-to-buffer gnus-summary-buffer 'norecord)
14699     (setq func (lookup-key (current-local-map) (this-command-keys)))
14700     (call-interactively func)
14701     (set-buffer obuf)
14702     (set-window-configuration owin)
14703     (set-window-point (get-buffer-window (current-buffer)) (point))))
14704
14705 (defun gnus-article-summary-command-nosave ()
14706   "Execute the last keystroke in the summary buffer."
14707   (interactive)
14708   (let (func)
14709     (pop-to-buffer gnus-summary-buffer 'norecord)
14710     (setq func (lookup-key (current-local-map) (this-command-keys)))
14711     (call-interactively func)))
14712
14713 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14714   "Read a summary buffer key sequence and execute it from the article buffer."
14715   (interactive "P")
14716   (let ((nosaves
14717          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14718            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14719            "=" "^" "\M-^" "|"))
14720         keys)
14721     (save-excursion
14722       (set-buffer gnus-summary-buffer)
14723       (push (or key last-command-event) unread-command-events)
14724       (setq keys (read-key-sequence nil)))
14725     (message "")
14726
14727     (if (member keys nosaves)
14728         (let (func)
14729           (pop-to-buffer gnus-summary-buffer 'norecord)
14730           (if (setq func (lookup-key (current-local-map) keys))
14731               (call-interactively func)
14732             (ding)))
14733       (let ((obuf (current-buffer))
14734             (owin (current-window-configuration))
14735             (opoint (point))
14736             func in-buffer)
14737         (if not-restore-window
14738             (pop-to-buffer gnus-summary-buffer 'norecord)
14739           (switch-to-buffer gnus-summary-buffer 'norecord))
14740         (setq in-buffer (current-buffer))
14741         (if (setq func (lookup-key (current-local-map) keys))
14742             (call-interactively func)
14743           (ding))
14744         (when (eq in-buffer (current-buffer))
14745           (set-buffer obuf)
14746           (unless not-restore-window
14747             (set-window-configuration owin))
14748           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14749
14750 \f
14751 ;;;
14752 ;;; Kill file handling.
14753 ;;;
14754
14755 ;;;###autoload
14756 (defalias 'gnus-batch-kill 'gnus-batch-score)
14757 ;;;###autoload
14758 (defun gnus-batch-score ()
14759   "Run batched scoring.
14760 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14761 Newsgroups is a list of strings in Bnews format.  If you want to score
14762 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14763 score the alt hierarchy, you'd say \"!alt.all\"."
14764   (interactive)
14765   (let* ((yes-and-no
14766           (gnus-newsrc-parse-options
14767            (apply (function concat)
14768                   (mapcar (lambda (g) (concat g " "))
14769                           command-line-args-left))))
14770          (gnus-expert-user t)
14771          (nnmail-spool-file nil)
14772          (gnus-use-dribble-file nil)
14773          (yes (car yes-and-no))
14774          (no (cdr yes-and-no))
14775          group newsrc entry
14776          ;; Disable verbose message.
14777          gnus-novice-user gnus-large-newsgroup)
14778     ;; Eat all arguments.
14779     (setq command-line-args-left nil)
14780     ;; Start Gnus.
14781     (gnus)
14782     ;; Apply kills to specified newsgroups in command line arguments.
14783     (setq newsrc (cdr gnus-newsrc-alist))
14784     (while newsrc
14785       (setq group (caar newsrc))
14786       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14787       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14788                (and (car entry)
14789                     (or (eq (car entry) t)
14790                         (not (zerop (car entry)))))
14791                (if yes (string-match yes group) t)
14792                (or (null no) (not (string-match no group))))
14793           (progn
14794             (gnus-summary-read-group group nil t nil t)
14795             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14796                  (gnus-summary-exit))))
14797       (setq newsrc (cdr newsrc)))
14798     ;; Exit Emacs.
14799     (switch-to-buffer gnus-group-buffer)
14800     (gnus-group-save-newsrc)))
14801
14802 (defun gnus-apply-kill-file ()
14803   "Apply a kill file to the current newsgroup.
14804 Returns the number of articles marked as read."
14805   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14806           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14807       (gnus-apply-kill-file-internal)
14808     0))
14809
14810 (defun gnus-kill-save-kill-buffer ()
14811   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14812     (when (get-file-buffer file)
14813       (save-excursion
14814         (set-buffer (get-file-buffer file))
14815         (and (buffer-modified-p) (save-buffer))
14816         (kill-buffer (current-buffer))))))
14817
14818 (defvar gnus-kill-file-name "KILL"
14819   "Suffix of the kill files.")
14820
14821 (defun gnus-newsgroup-kill-file (newsgroup)
14822   "Return the name of a kill file name for NEWSGROUP.
14823 If NEWSGROUP is nil, return the global kill file name instead."
14824   (cond 
14825    ;; The global KILL file is placed at top of the directory.
14826    ((or (null newsgroup)
14827         (string-equal newsgroup ""))
14828     (expand-file-name gnus-kill-file-name
14829                       gnus-kill-files-directory))
14830    ;; Append ".KILL" to newsgroup name.
14831    ((gnus-use-long-file-name 'not-kill)
14832     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14833                               "." gnus-kill-file-name)
14834                       gnus-kill-files-directory))
14835    ;; Place "KILL" under the hierarchical directory.
14836    (t
14837     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14838                               "/" gnus-kill-file-name)
14839                       gnus-kill-files-directory))))
14840
14841 \f
14842 ;;;
14843 ;;; Dribble file
14844 ;;;
14845
14846 (defvar gnus-dribble-ignore nil)
14847 (defvar gnus-dribble-eval-file nil)
14848
14849 (defun gnus-dribble-file-name ()
14850   "Return the dribble file for the current .newsrc."
14851   (concat
14852    (if gnus-dribble-directory
14853        (concat (file-name-as-directory gnus-dribble-directory)
14854                (file-name-nondirectory gnus-current-startup-file))
14855      gnus-current-startup-file)
14856    "-dribble"))
14857
14858 (defun gnus-dribble-enter (string)
14859   "Enter STRING into the dribble buffer."
14860   (if (and (not gnus-dribble-ignore)
14861            gnus-dribble-buffer
14862            (buffer-name gnus-dribble-buffer))
14863       (let ((obuf (current-buffer)))
14864         (set-buffer gnus-dribble-buffer)
14865         (insert string "\n")
14866         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14867         (set-buffer obuf))))
14868
14869 (defun gnus-dribble-read-file ()
14870   "Read the dribble file from disk."
14871   (let ((dribble-file (gnus-dribble-file-name)))
14872     (save-excursion
14873       (set-buffer (setq gnus-dribble-buffer
14874                         (get-buffer-create
14875                          (file-name-nondirectory dribble-file))))
14876       (gnus-add-current-to-buffer-list)
14877       (erase-buffer)
14878       (setq buffer-file-name dribble-file)
14879       (auto-save-mode t)
14880       (buffer-disable-undo (current-buffer))
14881       (bury-buffer (current-buffer))
14882       (set-buffer-modified-p nil)
14883       (let ((auto (make-auto-save-file-name))
14884             (gnus-dribble-ignore t)
14885             modes)
14886         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14887           ;; Load whichever file is newest -- the auto save file
14888           ;; or the "real" file.
14889           (if (file-newer-than-file-p auto dribble-file)
14890               (insert-file-contents auto)
14891             (insert-file-contents dribble-file))
14892           (unless (zerop (buffer-size))
14893             (set-buffer-modified-p t))
14894           ;; Set the file modes to reflect the .newsrc file modes.
14895           (save-buffer)
14896           (when (and (file-exists-p gnus-current-startup-file)
14897                      (setq modes (file-modes gnus-current-startup-file)))
14898             (set-file-modes dribble-file modes))
14899           ;; Possibly eval the file later.
14900           (when (gnus-y-or-n-p
14901                  "Auto-save file exists.  Do you want to read it? ")
14902             (setq gnus-dribble-eval-file t)))))))
14903
14904 (defun gnus-dribble-eval-file ()
14905   (when gnus-dribble-eval-file
14906     (setq gnus-dribble-eval-file nil)
14907     (save-excursion
14908       (let ((gnus-dribble-ignore t))
14909         (set-buffer gnus-dribble-buffer)
14910         (eval-buffer (current-buffer))))))
14911
14912 (defun gnus-dribble-delete-file ()
14913   (when (file-exists-p (gnus-dribble-file-name))
14914     (delete-file (gnus-dribble-file-name)))
14915   (when gnus-dribble-buffer
14916     (save-excursion
14917       (set-buffer gnus-dribble-buffer)
14918       (let ((auto (make-auto-save-file-name)))
14919         (if (file-exists-p auto)
14920             (delete-file auto))
14921         (erase-buffer)
14922         (set-buffer-modified-p nil)))))
14923
14924 (defun gnus-dribble-save ()
14925   (when (and gnus-dribble-buffer
14926              (buffer-name gnus-dribble-buffer))
14927     (save-excursion
14928       (set-buffer gnus-dribble-buffer)
14929       (save-buffer))))
14930
14931 (defun gnus-dribble-clear ()
14932   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14933     (save-excursion
14934       (set-buffer gnus-dribble-buffer)
14935       (erase-buffer)
14936       (set-buffer-modified-p nil)
14937       (setq buffer-saved-size (buffer-size)))))
14938
14939 \f
14940 ;;;
14941 ;;; Server Communication
14942 ;;;
14943
14944 (defun gnus-start-news-server (&optional confirm)
14945   "Open a method for getting news.
14946 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14947   (let (how)
14948     (if gnus-current-select-method
14949         ;; Stream is already opened.
14950         nil
14951       ;; Open NNTP server.
14952       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14953       (if confirm
14954           (progn
14955             ;; Read server name with completion.
14956             (setq gnus-nntp-server
14957                   (completing-read "NNTP server: "
14958                                    (mapcar (lambda (server) (list server))
14959                                            (cons (list gnus-nntp-server)
14960                                                  gnus-secondary-servers))
14961                                    nil nil gnus-nntp-server))))
14962
14963       (if (and gnus-nntp-server
14964                (stringp gnus-nntp-server)
14965                (not (string= gnus-nntp-server "")))
14966           (setq gnus-select-method
14967                 (cond ((or (string= gnus-nntp-server "")
14968                            (string= gnus-nntp-server "::"))
14969                        (list 'nnspool (system-name)))
14970                       ((string-match "^:" gnus-nntp-server)
14971                        (list 'nnmh gnus-nntp-server
14972                              (list 'nnmh-directory
14973                                    (file-name-as-directory
14974                                     (expand-file-name
14975                                      (concat "~/" (substring
14976                                                    gnus-nntp-server 1)))))
14977                              (list 'nnmh-get-new-mail nil)))
14978                       (t
14979                        (list 'nntp gnus-nntp-server)))))
14980
14981       (setq how (car gnus-select-method))
14982       (cond ((eq how 'nnspool)
14983              (require 'nnspool)
14984              (gnus-message 5 "Looking up local news spool..."))
14985             ((eq how 'nnmh)
14986              (require 'nnmh)
14987              (gnus-message 5 "Looking up mh spool..."))
14988             (t
14989              (require 'nntp)))
14990       (setq gnus-current-select-method gnus-select-method)
14991       (run-hooks 'gnus-open-server-hook)
14992       (or
14993        ;; gnus-open-server-hook might have opened it
14994        (gnus-server-opened gnus-select-method)
14995        (gnus-open-server gnus-select-method)
14996        (gnus-y-or-n-p
14997         (format
14998          "%s (%s) open error: '%s'.     Continue? "
14999          (car gnus-select-method) (cadr gnus-select-method)
15000          (gnus-status-message gnus-select-method)))
15001        (gnus-error 1 "Couldn't open server on %s"
15002                    (nth 1 gnus-select-method))))))
15003
15004 (defun gnus-check-group (group)
15005   "Try to make sure that the server where GROUP exists is alive."
15006   (let ((method (gnus-find-method-for-group group)))
15007     (or (gnus-server-opened method)
15008         (gnus-open-server method))))
15009
15010 (defun gnus-check-server (&optional method silent)
15011   "Check whether the connection to METHOD is down.
15012 If METHOD is nil, use `gnus-select-method'.
15013 If it is down, start it up (again)."
15014   (let ((method (or method gnus-select-method)))
15015     ;; Transform virtual server names into select methods.
15016     (when (stringp method)
15017       (setq method (gnus-server-to-method method)))
15018     (if (gnus-server-opened method)
15019         ;; The stream is already opened.
15020         t
15021       ;; Open the server.
15022       (unless silent
15023         (gnus-message 5 "Opening %s server%s..." (car method)
15024                       (if (equal (nth 1 method) "") ""
15025                         (format " on %s" (nth 1 method)))))
15026       (run-hooks 'gnus-open-server-hook)
15027       (prog1
15028           (gnus-open-server method)
15029         (unless silent
15030           (message ""))))))
15031
15032 (defun gnus-get-function (method function)
15033   "Return a function symbol based on METHOD and FUNCTION."
15034   ;; Translate server names into methods.
15035   (unless method
15036     (error "Attempted use of a nil select method"))
15037   (when (stringp method)
15038     (setq method (gnus-server-to-method method)))
15039   (let ((func (intern (format "%s-%s" (car method) function))))
15040     ;; If the functions isn't bound, we require the backend in
15041     ;; question.
15042     (unless (fboundp func)
15043       (require (car method))
15044       (unless (fboundp func)
15045         ;; This backend doesn't implement this function.
15046         (error "No such function: %s" func)))
15047     func))
15048
15049 \f
15050 ;;;
15051 ;;; Interface functions to the backends.
15052 ;;;
15053
15054 (defun gnus-open-server (method)
15055   "Open a connection to METHOD."
15056   (when (stringp method)
15057     (setq method (gnus-server-to-method method)))
15058   (let ((elem (assoc method gnus-opened-servers)))
15059     ;; If this method was previously denied, we just return nil.
15060     (if (eq (nth 1 elem) 'denied)
15061         (progn
15062           (gnus-message 1 "Denied server")
15063           nil)
15064       ;; Open the server.
15065       (let ((result
15066              (funcall (gnus-get-function method 'open-server)
15067                       (nth 1 method) (nthcdr 2 method))))
15068         ;; If this hasn't been opened before, we add it to the list.
15069         (unless elem
15070           (setq elem (list method nil)
15071                 gnus-opened-servers (cons elem gnus-opened-servers)))
15072         ;; Set the status of this server.
15073         (setcar (cdr elem) (if result 'ok 'denied))
15074         ;; Return the result from the "open" call.
15075         result))))
15076
15077 (defun gnus-close-server (method)
15078   "Close the connection to METHOD."
15079   (when (stringp method)
15080     (setq method (gnus-server-to-method method)))
15081   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15082
15083 (defun gnus-request-list (method)
15084   "Request the active file from METHOD."
15085   (when (stringp method)
15086     (setq method (gnus-server-to-method method)))
15087   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15088
15089 (defun gnus-request-list-newsgroups (method)
15090   "Request the newsgroups file from METHOD."
15091   (when (stringp method)
15092     (setq method (gnus-server-to-method method)))
15093   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15094
15095 (defun gnus-request-newgroups (date method)
15096   "Request all new groups since DATE from METHOD."
15097   (when (stringp method)
15098     (setq method (gnus-server-to-method method)))
15099   (funcall (gnus-get-function method 'request-newgroups)
15100            date (nth 1 method)))
15101
15102 (defun gnus-server-opened (method)
15103   "Check whether a connection to METHOD has been opened."
15104   (when (stringp method)
15105     (setq method (gnus-server-to-method method)))
15106   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15107
15108 (defun gnus-status-message (method)
15109   "Return the status message from METHOD.
15110 If METHOD is a string, it is interpreted as a group name.   The method
15111 this group uses will be queried."
15112   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15113                   method)))
15114     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15115
15116 (defun gnus-request-group (group &optional dont-check method)
15117   "Request GROUP.  If DONT-CHECK, no information is required."
15118   (let ((method (or method (gnus-find-method-for-group group))))
15119     (when (stringp method)
15120       (setq method (gnus-server-to-method method)))
15121     (funcall (gnus-get-function method 'request-group)
15122              (gnus-group-real-name group) (nth 1 method) dont-check)))
15123
15124 (defun gnus-request-asynchronous (group &optional articles)
15125   "Request that GROUP behave asynchronously.
15126 ARTICLES is the `data' of the group."
15127   (let ((method (gnus-find-method-for-group group)))
15128     (funcall (gnus-get-function method 'request-asynchronous)
15129              (gnus-group-real-name group) (nth 1 method) articles)))
15130
15131 (defun gnus-list-active-group (group)
15132   "Request active information on GROUP."
15133   (let ((method (gnus-find-method-for-group group))
15134         (func 'list-active-group))
15135     (when (gnus-check-backend-function func group)
15136       (funcall (gnus-get-function method func)
15137                (gnus-group-real-name group) (nth 1 method)))))
15138
15139 (defun gnus-request-group-description (group)
15140   "Request a description of GROUP."
15141   (let ((method (gnus-find-method-for-group group))
15142         (func 'request-group-description))
15143     (when (gnus-check-backend-function func group)
15144       (funcall (gnus-get-function method func)
15145                (gnus-group-real-name group) (nth 1 method)))))
15146
15147 (defun gnus-close-group (group)
15148   "Request the GROUP be closed."
15149   (let ((method (gnus-find-method-for-group group)))
15150     (funcall (gnus-get-function method 'close-group)
15151              (gnus-group-real-name group) (nth 1 method))))
15152
15153 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15154   "Request headers for ARTICLES in GROUP.
15155 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15156   (let ((method (gnus-find-method-for-group group)))
15157     (if (and gnus-use-cache (numberp (car articles)))
15158         (gnus-cache-retrieve-headers articles group fetch-old)
15159       (funcall (gnus-get-function method 'retrieve-headers)
15160                articles (gnus-group-real-name group) (nth 1 method)
15161                fetch-old))))
15162
15163 (defun gnus-retrieve-groups (groups method)
15164   "Request active information on GROUPS from METHOD."
15165   (when (stringp method)
15166     (setq method (gnus-server-to-method method)))
15167   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15168
15169 (defun gnus-request-type (group &optional article)
15170   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15171   (let ((method (gnus-find-method-for-group group)))
15172     (if (not (gnus-check-backend-function 'request-type (car method)))
15173         'unknown
15174       (funcall (gnus-get-function method 'request-type)
15175                (gnus-group-real-name group) article))))
15176
15177 (defun gnus-request-update-mark (group article mark)
15178   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15179   (let ((method (gnus-find-method-for-group group)))
15180     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15181         mark
15182       (funcall (gnus-get-function method 'request-update-mark)
15183                (gnus-group-real-name group) article mark))))
15184
15185 (defun gnus-request-article (article group &optional buffer)
15186   "Request the ARTICLE in GROUP.
15187 ARTICLE can either be an article number or an article Message-ID.
15188 If BUFFER, insert the article in that group."
15189   (let ((method (gnus-find-method-for-group group)))
15190     (funcall (gnus-get-function method 'request-article)
15191              article (gnus-group-real-name group) (nth 1 method) buffer)))
15192
15193 (defun gnus-request-head (article group)
15194   "Request the head of ARTICLE in GROUP."
15195   (let ((method (gnus-find-method-for-group group)))
15196     (funcall (gnus-get-function method 'request-head)
15197              article (gnus-group-real-name group) (nth 1 method))))
15198
15199 (defun gnus-request-body (article group)
15200   "Request the body of ARTICLE in GROUP."
15201   (let ((method (gnus-find-method-for-group group)))
15202     (funcall (gnus-get-function method 'request-body)
15203              article (gnus-group-real-name group) (nth 1 method))))
15204
15205 (defun gnus-request-post (method)
15206   "Post the current buffer using METHOD."
15207   (when (stringp method)
15208     (setq method (gnus-server-to-method method)))
15209   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15210
15211 (defun gnus-request-scan (group method)
15212   "Request a SCAN being performed in GROUP from METHOD.
15213 If GROUP is nil, all groups on METHOD are scanned."
15214   (let ((method (if group (gnus-find-method-for-group group) method)))
15215     (funcall (gnus-get-function method 'request-scan)
15216              (and group (gnus-group-real-name group)) (nth 1 method))))
15217
15218 (defsubst gnus-request-update-info (info method)
15219   "Request that METHOD update INFO."
15220   (when (stringp method)
15221     (setq method (gnus-server-to-method method)))
15222   (when (gnus-check-backend-function 'request-update-info (car method))
15223     (funcall (gnus-get-function method 'request-update-info)
15224              (gnus-group-real-name (gnus-info-group info))
15225              info (nth 1 method))))
15226
15227 (defun gnus-request-expire-articles (articles group &optional force)
15228   (let ((method (gnus-find-method-for-group group)))
15229     (funcall (gnus-get-function method 'request-expire-articles)
15230              articles (gnus-group-real-name group) (nth 1 method)
15231              force)))
15232
15233 (defun gnus-request-move-article
15234   (article group server accept-function &optional last)
15235   (let ((method (gnus-find-method-for-group group)))
15236     (funcall (gnus-get-function method 'request-move-article)
15237              article (gnus-group-real-name group)
15238              (nth 1 method) accept-function last)))
15239
15240 (defun gnus-request-accept-article (group method &optional last)
15241   ;; Make sure there's a newline at the end of the article.
15242   (when (stringp method)
15243     (setq method (gnus-server-to-method method)))
15244   (when (and (not method)
15245              (stringp group))
15246     (setq method (gnus-find-method-for-group group)))
15247   (goto-char (point-max))
15248   (unless (bolp)
15249     (insert "\n"))
15250   (let ((func (car (or method (gnus-find-method-for-group group)))))
15251     (funcall (intern (format "%s-request-accept-article" func))
15252              (if (stringp group) (gnus-group-real-name group) group)
15253              (cadr method)
15254              last)))
15255
15256 (defun gnus-request-replace-article (article group buffer)
15257   (let ((func (car (gnus-find-method-for-group group))))
15258     (funcall (intern (format "%s-request-replace-article" func))
15259              article (gnus-group-real-name group) buffer)))
15260
15261 (defun gnus-request-associate-buffer (group)
15262   (let ((method (gnus-find-method-for-group group)))
15263     (funcall (gnus-get-function method 'request-associate-buffer)
15264              (gnus-group-real-name group))))
15265
15266 (defun gnus-request-restore-buffer (article group)
15267   "Request a new buffer restored to the state of ARTICLE."
15268   (let ((method (gnus-find-method-for-group group)))
15269     (funcall (gnus-get-function method 'request-restore-buffer)
15270              article (gnus-group-real-name group) (nth 1 method))))
15271
15272 (defun gnus-request-create-group (group &optional method)
15273   (when (stringp method)
15274     (setq method (gnus-server-to-method method)))
15275   (let ((method (or method (gnus-find-method-for-group group))))
15276     (funcall (gnus-get-function method 'request-create-group)
15277              (gnus-group-real-name group) (nth 1 method))))
15278
15279 (defun gnus-request-delete-group (group &optional force)
15280   (let ((method (gnus-find-method-for-group group)))
15281     (funcall (gnus-get-function method 'request-delete-group)
15282              (gnus-group-real-name group) force (nth 1 method))))
15283
15284 (defun gnus-request-rename-group (group new-name)
15285   (let ((method (gnus-find-method-for-group group)))
15286     (funcall (gnus-get-function method 'request-rename-group)
15287              (gnus-group-real-name group)
15288              (gnus-group-real-name new-name) (nth 1 method))))
15289
15290 (defun gnus-member-of-valid (symbol group)
15291   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15292   (memq symbol (assoc
15293                 (symbol-name (car (gnus-find-method-for-group group)))
15294                 gnus-valid-select-methods)))
15295
15296 (defun gnus-method-option-p (method option)
15297   "Return non-nil if select METHOD has OPTION as a parameter."
15298   (when (stringp method)
15299     (setq method (gnus-server-to-method method)))
15300   (memq option (assoc (format "%s" (car method))
15301                       gnus-valid-select-methods)))
15302
15303 (defun gnus-server-extend-method (group method)
15304   ;; This function "extends" a virtual server.  If the server is
15305   ;; "hello", and the select method is ("hello" (my-var "something"))
15306   ;; in the group "alt.alt", this will result in a new virtual server
15307   ;; called "hello+alt.alt".
15308   (let ((entry
15309          (gnus-copy-sequence
15310           (if (equal (car method) "native") gnus-select-method
15311             (cdr (assoc (car method) gnus-server-alist))))))
15312     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15313     (nconc entry (cdr method))))
15314
15315 (defun gnus-find-method-for-group (group &optional info)
15316   "Find the select method that GROUP uses."
15317   (or gnus-override-method
15318       (and (not group)
15319            gnus-select-method)
15320       (let ((info (or info (gnus-get-info group)))
15321             method)
15322         (if (or (not info)
15323                 (not (setq method (gnus-info-method info)))
15324                 (equal method "native"))
15325             gnus-select-method
15326           (setq method
15327                 (cond ((stringp method)
15328                        (gnus-server-to-method method))
15329                       ((stringp (car method))
15330                        (gnus-server-extend-method group method))
15331                       (t
15332                        method)))
15333           (cond ((equal (cadr method) "")
15334                  method)
15335                 ((null (cadr method))
15336                  (list (car method) ""))
15337                 (t
15338                  (gnus-server-add-address method)))))))
15339
15340 (defun gnus-check-backend-function (func group)
15341   "Check whether GROUP supports function FUNC."
15342   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15343                   group)))
15344     (fboundp (intern (format "%s-%s" method func)))))
15345
15346 (defun gnus-methods-using (feature)
15347   "Find all methods that have FEATURE."
15348   (let ((valids gnus-valid-select-methods)
15349         outs)
15350     (while valids
15351       (if (memq feature (car valids))
15352           (setq outs (cons (car valids) outs)))
15353       (setq valids (cdr valids)))
15354     outs))
15355
15356 \f
15357 ;;;
15358 ;;; Active & Newsrc File Handling
15359 ;;;
15360
15361 (defun gnus-setup-news (&optional rawfile level dont-connect)
15362   "Setup news information.
15363 If RAWFILE is non-nil, the .newsrc file will also be read.
15364 If LEVEL is non-nil, the news will be set up at level LEVEL."
15365   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15366
15367     (when init 
15368       ;; Clear some variables to re-initialize news information.
15369       (setq gnus-newsrc-alist nil
15370             gnus-active-hashtb nil)
15371       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15372       (gnus-read-newsrc-file rawfile))
15373
15374     (when (and (not (assoc "archive" gnus-server-alist))
15375                gnus-message-archive-method)
15376       (push (cons "archive" gnus-message-archive-method)
15377             gnus-server-alist))
15378
15379     ;; If we don't read the complete active file, we fill in the
15380     ;; hashtb here.
15381     (if (or (null gnus-read-active-file)
15382             (eq gnus-read-active-file 'some))
15383         (gnus-update-active-hashtb-from-killed))
15384
15385     ;; Read the active file and create `gnus-active-hashtb'.
15386     ;; If `gnus-read-active-file' is nil, then we just create an empty
15387     ;; hash table.  The partial filling out of the hash table will be
15388     ;; done in `gnus-get-unread-articles'.
15389     (and gnus-read-active-file
15390          (not level)
15391          (gnus-read-active-file))
15392
15393     (or gnus-active-hashtb
15394         (setq gnus-active-hashtb (make-vector 4095 0)))
15395
15396     ;; Initialize the cache.
15397     (when gnus-use-cache
15398       (gnus-cache-open))
15399
15400     ;; Possibly eval the dribble file.
15401     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15402
15403     ;; Slave Gnusii should then clear the dribble buffer.
15404     (when (and init gnus-slave)
15405       (gnus-dribble-clear))
15406
15407     (gnus-update-format-specifications)
15408
15409     ;; See whether we need to read the description file.
15410     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15411              (not gnus-description-hashtb)
15412              (not dont-connect)
15413              gnus-read-active-file)
15414         (gnus-read-all-descriptions-files))
15415
15416     ;; Find new newsgroups and treat them.
15417     (if (and init gnus-check-new-newsgroups (not level)
15418              (gnus-check-server gnus-select-method))
15419         (gnus-find-new-newsgroups))
15420
15421     ;; We might read in new NoCeM messages here.
15422     (when (and gnus-use-nocem 
15423                (not level)
15424                (not dont-connect))
15425       (gnus-nocem-scan-groups))
15426
15427     ;; Find the number of unread articles in each non-dead group.
15428     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15429       (gnus-get-unread-articles level))
15430
15431     (if (and init gnus-check-bogus-newsgroups
15432              gnus-read-active-file (not level)
15433              (gnus-server-opened gnus-select-method))
15434         (gnus-check-bogus-newsgroups))))
15435
15436 (defun gnus-find-new-newsgroups (&optional arg)
15437   "Search for new newsgroups and add them.
15438 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15439 The `-n' option line from .newsrc is respected.
15440 If ARG (the prefix), use the `ask-server' method to query
15441 the server for new groups."
15442   (interactive "P")
15443   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15444                        (null gnus-read-active-file)
15445                        (eq gnus-read-active-file 'some))
15446                    'ask-server gnus-check-new-newsgroups)))
15447     (unless (gnus-check-first-time-used)
15448       (if (or (consp check)
15449               (eq check 'ask-server))
15450           ;; Ask the server for new groups.
15451           (gnus-ask-server-for-new-groups)
15452         ;; Go through the active hashtb and look for new groups.
15453         (let ((groups 0)
15454               group new-newsgroups)
15455           (gnus-message 5 "Looking for new newsgroups...")
15456           (unless gnus-have-read-active-file
15457             (gnus-read-active-file))
15458           (setq gnus-newsrc-last-checked-date (current-time-string))
15459           (unless gnus-killed-hashtb
15460             (gnus-make-hashtable-from-killed))
15461           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15462           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15463           (mapatoms
15464            (lambda (sym)
15465              (if (or (null (setq group (symbol-name sym)))
15466                      (not (boundp sym))
15467                      (null (symbol-value sym))
15468                      (gnus-gethash group gnus-killed-hashtb)
15469                      (gnus-gethash group gnus-newsrc-hashtb))
15470                  ()
15471                (let ((do-sub (gnus-matches-options-n group)))
15472                  (cond
15473                   ((eq do-sub 'subscribe)
15474                    (setq groups (1+ groups))
15475                    (gnus-sethash group group gnus-killed-hashtb)
15476                    (funcall gnus-subscribe-options-newsgroup-method group))
15477                   ((eq do-sub 'ignore)
15478                    nil)
15479                   (t
15480                    (setq groups (1+ groups))
15481                    (gnus-sethash group group gnus-killed-hashtb)
15482                    (if gnus-subscribe-hierarchical-interactive
15483                        (setq new-newsgroups (cons group new-newsgroups))
15484                      (funcall gnus-subscribe-newsgroup-method group)))))))
15485            gnus-active-hashtb)
15486           (when new-newsgroups
15487             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15488           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15489           (if (> groups 0)
15490               (gnus-message 6 "%d new newsgroup%s arrived."
15491                             groups (if (> groups 1) "s have" " has"))
15492             (gnus-message 6 "No new newsgroups.")))))))
15493
15494 (defun gnus-matches-options-n (group)
15495   ;; Returns `subscribe' if the group is to be unconditionally
15496   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15497   ;; no match for the group.
15498
15499   ;; First we check the two user variables.
15500   (cond
15501    ((and gnus-options-subscribe
15502          (string-match gnus-options-subscribe group))
15503     'subscribe)
15504    ((and gnus-auto-subscribed-groups
15505          (string-match gnus-auto-subscribed-groups group))
15506     'subscribe)
15507    ((and gnus-options-not-subscribe
15508          (string-match gnus-options-not-subscribe group))
15509     'ignore)
15510    ;; Then we go through the list that was retrieved from the .newsrc
15511    ;; file.  This list has elements on the form
15512    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15513    ;; is in the reverse order of the options line) is returned.
15514    (t
15515     (let ((regs gnus-newsrc-options-n))
15516       (while (and regs
15517                   (not (string-match (caar regs) group)))
15518         (setq regs (cdr regs)))
15519       (and regs (cdar regs))))))
15520
15521 (defun gnus-ask-server-for-new-groups ()
15522   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15523          (methods (cons gnus-select-method
15524                         (nconc
15525                          (when gnus-message-archive-method
15526                            (list "archive"))
15527                          (append
15528                           (and (consp gnus-check-new-newsgroups)
15529                                gnus-check-new-newsgroups)
15530                           gnus-secondary-select-methods))))
15531          (groups 0)
15532          (new-date (current-time-string))
15533          group new-newsgroups got-new method hashtb
15534          gnus-override-subscribe-method)
15535     ;; Go through both primary and secondary select methods and
15536     ;; request new newsgroups.
15537     (while (setq method (gnus-server-get-method nil (pop methods)))
15538       (setq new-newsgroups nil)
15539       (setq gnus-override-subscribe-method method)
15540       (when (and (gnus-check-server method)
15541                  (gnus-request-newgroups date method))
15542         (save-excursion
15543           (setq got-new t)
15544           (setq hashtb (gnus-make-hashtable 100))
15545           (set-buffer nntp-server-buffer)
15546           ;; Enter all the new groups into a hashtable.
15547           (gnus-active-to-gnus-format method hashtb 'ignore))
15548         ;; Now all new groups from `method' are in `hashtb'.
15549         (mapatoms
15550          (lambda (group-sym)
15551            (if (or (null (setq group (symbol-name group-sym)))
15552                    (not (boundp group-sym))
15553                    (null (symbol-value group-sym))
15554                    (gnus-gethash group gnus-newsrc-hashtb)
15555                    (member group gnus-zombie-list)
15556                    (member group gnus-killed-list))
15557                ;; The group is already known.
15558                ()
15559              ;; Make this group active.
15560              (when (symbol-value group-sym)
15561                (gnus-set-active group (symbol-value group-sym)))
15562              ;; Check whether we want it or not.
15563              (let ((do-sub (gnus-matches-options-n group)))
15564                (cond
15565                 ((eq do-sub 'subscribe)
15566                  (incf groups)
15567                  (gnus-sethash group group gnus-killed-hashtb)
15568                  (funcall gnus-subscribe-options-newsgroup-method group))
15569                 ((eq do-sub 'ignore)
15570                  nil)
15571                 (t
15572                  (incf groups)
15573                  (gnus-sethash group group gnus-killed-hashtb)
15574                  (if gnus-subscribe-hierarchical-interactive
15575                      (push group new-newsgroups)
15576                    (funcall gnus-subscribe-newsgroup-method group)))))))
15577          hashtb))
15578       (when new-newsgroups
15579         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15580     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15581     (when (> groups 0)
15582       (gnus-message 6 "%d new newsgroup%s arrived."
15583                     groups (if (> groups 1) "s have" " has")))
15584     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15585     got-new))
15586
15587 (defun gnus-check-first-time-used ()
15588   (if (or (> (length gnus-newsrc-alist) 1)
15589           (file-exists-p gnus-startup-file)
15590           (file-exists-p (concat gnus-startup-file ".el"))
15591           (file-exists-p (concat gnus-startup-file ".eld")))
15592       nil
15593     (gnus-message 6 "First time user; subscribing you to default groups")
15594     (unless (gnus-read-active-file-p)
15595       (gnus-read-active-file))
15596     (setq gnus-newsrc-last-checked-date (current-time-string))
15597     (let ((groups gnus-default-subscribed-newsgroups)
15598           group)
15599       (if (eq groups t)
15600           nil
15601         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15602         (mapatoms
15603          (lambda (sym)
15604            (if (null (setq group (symbol-name sym)))
15605                ()
15606              (let ((do-sub (gnus-matches-options-n group)))
15607                (cond
15608                 ((eq do-sub 'subscribe)
15609                  (gnus-sethash group group gnus-killed-hashtb)
15610                  (funcall gnus-subscribe-options-newsgroup-method group))
15611                 ((eq do-sub 'ignore)
15612                  nil)
15613                 (t
15614                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15615          gnus-active-hashtb)
15616         (while groups
15617           (if (gnus-active (car groups))
15618               (gnus-group-change-level
15619                (car groups) gnus-level-default-subscribed gnus-level-killed))
15620           (setq groups (cdr groups)))
15621         (gnus-group-make-help-group)
15622         (and gnus-novice-user
15623              (gnus-message 7 "`A k' to list killed groups"))))))
15624
15625 (defun gnus-subscribe-group (group previous &optional method)
15626   (gnus-group-change-level
15627    (if method
15628        (list t group gnus-level-default-subscribed nil nil method)
15629      group)
15630    gnus-level-default-subscribed gnus-level-killed previous t))
15631
15632 ;; `gnus-group-change-level' is the fundamental function for changing
15633 ;; subscription levels of newsgroups.  This might mean just changing
15634 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15635 ;; again, which subscribes/unsubscribes a group, which is equally
15636 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15637 ;; from 8-9 to 1-7 means that you remove the group from the list of
15638 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15639 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15640 ;; which is trivial.
15641 ;; ENTRY can either be a string (newsgroup name) or a list (if
15642 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15643 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15644 ;; entries.
15645 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15646 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15647 ;; after.
15648 (defun gnus-group-change-level (entry level &optional oldlevel
15649                                       previous fromkilled)
15650   (let (group info active num)
15651     ;; Glean what info we can from the arguments
15652     (if (consp entry)
15653         (if fromkilled (setq group (nth 1 entry))
15654           (setq group (car (nth 2 entry))))
15655       (setq group entry))
15656     (if (and (stringp entry)
15657              oldlevel
15658              (< oldlevel gnus-level-zombie))
15659         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15660     (if (and (not oldlevel)
15661              (consp entry))
15662         (setq oldlevel (gnus-info-level (nth 2 entry)))
15663       (setq oldlevel (or oldlevel 9)))
15664     (if (stringp previous)
15665         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15666
15667     (if (and (>= oldlevel gnus-level-zombie)
15668              (gnus-gethash group gnus-newsrc-hashtb))
15669         ;; We are trying to subscribe a group that is already
15670         ;; subscribed.
15671         ()                              ; Do nothing.
15672
15673       (or (gnus-ephemeral-group-p group)
15674           (gnus-dribble-enter
15675            (format "(gnus-group-change-level %S %S %S %S %S)"
15676                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15677
15678       ;; Then we remove the newgroup from any old structures, if needed.
15679       ;; If the group was killed, we remove it from the killed or zombie
15680       ;; list.  If not, and it is in fact going to be killed, we remove
15681       ;; it from the newsrc hash table and assoc.
15682       (cond
15683        ((>= oldlevel gnus-level-zombie)
15684         (if (= oldlevel gnus-level-zombie)
15685             (setq gnus-zombie-list (delete group gnus-zombie-list))
15686           (setq gnus-killed-list (delete group gnus-killed-list))))
15687        (t
15688         (if (and (>= level gnus-level-zombie)
15689                  entry)
15690             (progn
15691               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15692               (if (nth 3 entry)
15693                   (setcdr (gnus-gethash (car (nth 3 entry))
15694                                         gnus-newsrc-hashtb)
15695                           (cdr entry)))
15696               (setcdr (cdr entry) (cdddr entry))))))
15697
15698       ;; Finally we enter (if needed) the list where it is supposed to
15699       ;; go, and change the subscription level.  If it is to be killed,
15700       ;; we enter it into the killed or zombie list.
15701       (cond 
15702        ((>= level gnus-level-zombie)
15703         ;; Remove from the hash table.
15704         (gnus-sethash group nil gnus-newsrc-hashtb)
15705         ;; We do not enter foreign groups into the list of dead
15706         ;; groups.
15707         (unless (gnus-group-foreign-p group)
15708           (if (= level gnus-level-zombie)
15709               (setq gnus-zombie-list (cons group gnus-zombie-list))
15710             (setq gnus-killed-list (cons group gnus-killed-list)))))
15711        (t
15712         ;; If the list is to be entered into the newsrc assoc, and
15713         ;; it was killed, we have to create an entry in the newsrc
15714         ;; hashtb format and fix the pointers in the newsrc assoc.
15715         (if (< oldlevel gnus-level-zombie)
15716             ;; It was alive, and it is going to stay alive, so we
15717             ;; just change the level and don't change any pointers or
15718             ;; hash table entries.
15719             (setcar (cdaddr entry) level)
15720           (if (listp entry)
15721               (setq info (cdr entry)
15722                     num (car entry))
15723             (setq active (gnus-active group))
15724             (setq num
15725                   (if active (- (1+ (cdr active)) (car active)) t))
15726             ;; Check whether the group is foreign.  If so, the
15727             ;; foreign select method has to be entered into the
15728             ;; info.
15729             (let ((method (or gnus-override-subscribe-method
15730                               (gnus-group-method group))))
15731               (if (eq method gnus-select-method)
15732                   (setq info (list group level nil))
15733                 (setq info (list group level nil nil method)))))
15734           (unless previous
15735             (setq previous
15736                   (let ((p gnus-newsrc-alist))
15737                     (while (cddr p)
15738                       (setq p (cdr p)))
15739                     p)))
15740           (setq entry (cons info (cddr previous)))
15741           (if (cdr previous)
15742               (progn
15743                 (setcdr (cdr previous) entry)
15744                 (gnus-sethash group (cons num (cdr previous))
15745                               gnus-newsrc-hashtb))
15746             (setcdr previous entry)
15747             (gnus-sethash group (cons num previous)
15748                           gnus-newsrc-hashtb))
15749           (when (cdr entry)
15750             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15751       (when gnus-group-change-level-function
15752         (funcall gnus-group-change-level-function group level oldlevel)))))
15753
15754 (defun gnus-kill-newsgroup (newsgroup)
15755   "Obsolete function.  Kills a newsgroup."
15756   (gnus-group-change-level
15757    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15758
15759 (defun gnus-check-bogus-newsgroups (&optional confirm)
15760   "Remove bogus newsgroups.
15761 If CONFIRM is non-nil, the user has to confirm the deletion of every
15762 newsgroup."
15763   (let ((newsrc (cdr gnus-newsrc-alist))
15764         bogus group entry info)
15765     (gnus-message 5 "Checking bogus newsgroups...")
15766     (unless (gnus-read-active-file-p)
15767       (gnus-read-active-file))
15768     (when (gnus-read-active-file-p)
15769       ;; Find all bogus newsgroup that are subscribed.
15770       (while newsrc
15771         (setq info (pop newsrc)
15772               group (gnus-info-group info))
15773         (unless (or (gnus-active group) ; Active
15774                     (gnus-info-method info) ; Foreign
15775                     (and confirm
15776                          (not (gnus-y-or-n-p
15777                                (format "Remove bogus newsgroup: %s " group)))))
15778           ;; Found a bogus newsgroup.
15779           (push group bogus)))
15780       ;; Remove all bogus subscribed groups by first killing them, and
15781       ;; then removing them from the list of killed groups.
15782       (while bogus
15783         (when (setq entry (gnus-gethash (setq group (pop bogus))
15784                                         gnus-newsrc-hashtb))
15785           (gnus-group-change-level entry gnus-level-killed)
15786           (setq gnus-killed-list (delete group gnus-killed-list))))
15787       ;; Then we remove all bogus groups from the list of killed and
15788       ;; zombie groups.  They are are removed without confirmation.
15789       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15790             killed)
15791         (while dead-lists
15792           (setq killed (symbol-value (car dead-lists)))
15793           (while killed
15794             (unless (gnus-active (setq group (pop killed)))
15795               ;; The group is bogus.
15796               ;; !!!Slow as hell.
15797               (set (car dead-lists)
15798                    (delete group (symbol-value (car dead-lists))))))
15799           (setq dead-lists (cdr dead-lists))))
15800       (gnus-message 5 "Checking bogus newsgroups...done"))))
15801
15802 (defun gnus-check-duplicate-killed-groups ()
15803   "Remove duplicates from the list of killed groups."
15804   (interactive)
15805   (let ((killed gnus-killed-list))
15806     (while killed
15807       (gnus-message 9 "%d" (length killed))
15808       (setcdr killed (delete (car killed) (cdr killed)))
15809       (setq killed (cdr killed)))))
15810
15811 ;; We want to inline a function from gnus-cache, so we cheat here:
15812 (eval-when-compile
15813   (provide 'gnus)
15814   (require 'gnus-cache))
15815
15816 (defun gnus-get-unread-articles-in-group (info active &optional update)
15817   (when active
15818     ;; Allow the backend to update the info in the group.
15819     (when (and update 
15820                (gnus-request-update-info
15821                 info (gnus-find-method-for-group (gnus-info-group info))))
15822       (gnus-activate-group (gnus-info-group info) nil t))
15823     (let* ((range (gnus-info-read info))
15824            (num 0))
15825       ;; If a cache is present, we may have to alter the active info.
15826       (when (and gnus-use-cache info)
15827         (inline (gnus-cache-possibly-alter-active 
15828                  (gnus-info-group info) active)))
15829       ;; Modify the list of read articles according to what articles
15830       ;; are available; then tally the unread articles and add the
15831       ;; number to the group hash table entry.
15832       (cond
15833        ((zerop (cdr active))
15834         (setq num 0))
15835        ((not range)
15836         (setq num (- (1+ (cdr active)) (car active))))
15837        ((not (listp (cdr range)))
15838         ;; Fix a single (num . num) range according to the
15839         ;; active hash table.
15840         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15841         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15842         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15843         ;; Compute number of unread articles.
15844         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15845        (t
15846         ;; The read list is a list of ranges.  Fix them according to
15847         ;; the active hash table.
15848         ;; First peel off any elements that are below the lower
15849         ;; active limit.
15850         (while (and (cdr range)
15851                     (>= (car active)
15852                         (or (and (atom (cadr range)) (cadr range))
15853                             (caadr range))))
15854           (if (numberp (car range))
15855               (setcar range
15856                       (cons (car range)
15857                             (or (and (numberp (cadr range))
15858                                      (cadr range))
15859                                 (cdadr range))))
15860             (setcdr (car range)
15861                     (or (and (numberp (nth 1 range)) (nth 1 range))
15862                         (cdadr range))))
15863           (setcdr range (cddr range)))
15864         ;; Adjust the first element to be the same as the lower limit.
15865         (if (and (not (atom (car range)))
15866                  (< (cdar range) (car active)))
15867             (setcdr (car range) (1- (car active))))
15868         ;; Then we want to peel off any elements that are higher
15869         ;; than the upper active limit.
15870         (let ((srange range))
15871           ;; Go past all legal elements.
15872           (while (and (cdr srange)
15873                       (<= (or (and (atom (cadr srange))
15874                                    (cadr srange))
15875                               (caadr srange)) (cdr active)))
15876             (setq srange (cdr srange)))
15877           (if (cdr srange)
15878               ;; Nuke all remaining illegal elements.
15879               (setcdr srange nil))
15880
15881           ;; Adjust the final element.
15882           (if (and (not (atom (car srange)))
15883                    (> (cdar srange) (cdr active)))
15884               (setcdr (car srange) (cdr active))))
15885         ;; Compute the number of unread articles.
15886         (while range
15887           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15888                                       (cdar range)))
15889                               (or (and (atom (car range)) (car range))
15890                                   (caar range)))))
15891           (setq range (cdr range)))
15892         (setq num (max 0 (- (cdr active) num)))))
15893       ;; Set the number of unread articles.
15894       (when info
15895         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15896       num)))
15897
15898 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15899 ;; and compute how many unread articles there are in each group.
15900 (defun gnus-get-unread-articles (&optional level)
15901   (let* ((newsrc (cdr gnus-newsrc-alist))
15902          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15903          (foreign-level
15904           (min
15905            (cond ((and gnus-activate-foreign-newsgroups
15906                        (not (numberp gnus-activate-foreign-newsgroups)))
15907                   (1+ gnus-level-subscribed))
15908                  ((numberp gnus-activate-foreign-newsgroups)
15909                   gnus-activate-foreign-newsgroups)
15910                  (t 0))
15911            level))
15912          info group active method)
15913     (gnus-message 5 "Checking new news...")
15914
15915     (while newsrc
15916       (setq active (gnus-active (setq group (gnus-info-group
15917                                              (setq info (pop newsrc))))))
15918
15919       ;; Check newsgroups.  If the user doesn't want to check them, or
15920       ;; they can't be checked (for instance, if the news server can't
15921       ;; be reached) we just set the number of unread articles in this
15922       ;; newsgroup to t.  This means that Gnus thinks that there are
15923       ;; unread articles, but it has no idea how many.
15924       (if (and (setq method (gnus-info-method info))
15925                (not (gnus-server-equal
15926                      gnus-select-method
15927                      (setq method (gnus-server-get-method nil method))))
15928                (not (gnus-secondary-method-p method)))
15929           ;; These groups are foreign.  Check the level.
15930           (when (<= (gnus-info-level info) foreign-level)
15931             (setq active (gnus-activate-group group 'scan))
15932             (unless (inline (gnus-virtual-group-p group))
15933               (inline (gnus-close-group group)))
15934             (when (fboundp (intern (concat (symbol-name (car method))
15935                                            "-request-update-info")))
15936               (inline (gnus-request-update-info info method))))
15937         ;; These groups are native or secondary.
15938         (when (and (<= (gnus-info-level info) level)
15939                    (not gnus-read-active-file))
15940           (setq active (gnus-activate-group group 'scan))
15941           (inline (gnus-close-group group))))
15942
15943       ;; Get the number of unread articles in the group.
15944       (if active
15945           (inline (gnus-get-unread-articles-in-group info active))
15946         ;; The group couldn't be reached, so we nix out the number of
15947         ;; unread articles and stuff.
15948         (gnus-set-active group nil)
15949         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15950
15951     (gnus-message 5 "Checking new news...done")))
15952
15953 ;; Create a hash table out of the newsrc alist.  The `car's of the
15954 ;; alist elements are used as keys.
15955 (defun gnus-make-hashtable-from-newsrc-alist ()
15956   (let ((alist gnus-newsrc-alist)
15957         (ohashtb gnus-newsrc-hashtb)
15958         prev)
15959     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15960     (setq alist
15961           (setq prev (setq gnus-newsrc-alist
15962                            (if (equal (caar gnus-newsrc-alist)
15963                                       "dummy.group")
15964                                gnus-newsrc-alist
15965                              (cons (list "dummy.group" 0 nil) alist)))))
15966     (while alist
15967       (gnus-sethash
15968        (caar alist)
15969        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15970              prev)
15971        gnus-newsrc-hashtb)
15972       (setq prev alist
15973             alist (cdr alist)))))
15974
15975 (defun gnus-make-hashtable-from-killed ()
15976   "Create a hash table from the killed and zombie lists."
15977   (let ((lists '(gnus-killed-list gnus-zombie-list))
15978         list)
15979     (setq gnus-killed-hashtb
15980           (gnus-make-hashtable
15981            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15982     (while (setq list (pop lists))
15983       (setq list (symbol-value list))
15984       (while list
15985         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15986
15987 (defun gnus-activate-group (group &optional scan dont-check)
15988   ;; Check whether a group has been activated or not.
15989   ;; If SCAN, request a scan of that group as well.
15990   (let ((method (gnus-find-method-for-group group))
15991         active)
15992     (and (gnus-check-server method)
15993          ;; We escape all bugs and quit here to make it possible to
15994          ;; continue if a group is so out-there that it reports bugs
15995          ;; and stuff.
15996          (progn
15997            (and scan
15998                 (gnus-check-backend-function 'request-scan (car method))
15999                 (gnus-request-scan group method))
16000            t)
16001          (condition-case ()
16002              (gnus-request-group group dont-check)
16003         ;   (error nil)
16004            (quit nil))
16005          (save-excursion
16006            (set-buffer nntp-server-buffer)
16007            (goto-char (point-min))
16008            ;; Parse the result we got from `gnus-request-group'.
16009            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16010                 (progn
16011                   (goto-char (match-beginning 1))
16012                   (gnus-set-active
16013                    group (setq active (cons (read (current-buffer))
16014                                             (read (current-buffer)))))
16015                   ;; Return the new active info.
16016                   active))))))
16017
16018 (defun gnus-update-read-articles (group unread)
16019   "Update the list of read and ticked articles in GROUP using the
16020 UNREAD and TICKED lists.
16021 Note: UNSELECTED has to be sorted over `<'.
16022 Returns whether the updating was successful."
16023   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16024          (entry (gnus-gethash group gnus-newsrc-hashtb))
16025          (info (nth 2 entry))
16026          (prev 1)
16027          (unread (sort (copy-sequence unread) '<))
16028          read)
16029     (if (or (not info) (not active))
16030         ;; There is no info on this group if it was, in fact,
16031         ;; killed.  Gnus stores no information on killed groups, so
16032         ;; there's nothing to be done.
16033         ;; One could store the information somewhere temporarily,
16034         ;; perhaps...  Hmmm...
16035         ()
16036       ;; Remove any negative articles numbers.
16037       (while (and unread (< (car unread) 0))
16038         (setq unread (cdr unread)))
16039       ;; Remove any expired article numbers
16040       (while (and unread (< (car unread) (car active)))
16041         (setq unread (cdr unread)))
16042       ;; Compute the ranges of read articles by looking at the list of
16043       ;; unread articles.
16044       (while unread
16045         (if (/= (car unread) prev)
16046             (setq read (cons (if (= prev (1- (car unread))) prev
16047                                (cons prev (1- (car unread)))) read)))
16048         (setq prev (1+ (car unread)))
16049         (setq unread (cdr unread)))
16050       (when (<= prev (cdr active))
16051         (setq read (cons (cons prev (cdr active)) read)))
16052       ;; Enter this list into the group info.
16053       (gnus-info-set-read
16054        info (if (> (length read) 1) (nreverse read) read))
16055       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16056       (gnus-get-unread-articles-in-group info (gnus-active group))
16057       t)))
16058
16059 (defun gnus-make-articles-unread (group articles)
16060   "Mark ARTICLES in GROUP as unread."
16061   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16062                           (gnus-gethash (gnus-group-real-name group)
16063                                         gnus-newsrc-hashtb))))
16064          (ranges (gnus-info-read info))
16065          news article)
16066     (while articles
16067       (when (gnus-member-of-range
16068              (setq article (pop articles)) ranges)
16069         (setq news (cons article news))))
16070     (when news
16071       (gnus-info-set-read
16072        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16073       (gnus-group-update-group group t))))
16074
16075 ;; Enter all dead groups into the hashtb.
16076 (defun gnus-update-active-hashtb-from-killed ()
16077   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16078         (lists (list gnus-killed-list gnus-zombie-list))
16079         killed)
16080     (while lists
16081       (setq killed (car lists))
16082       (while killed
16083         (gnus-sethash (car killed) nil hashtb)
16084         (setq killed (cdr killed)))
16085       (setq lists (cdr lists)))))
16086
16087 (defun gnus-get-killed-groups ()
16088   "Go through the active hashtb and all all unknown groups as killed."
16089   ;; First make sure active file has been read.
16090   (unless (gnus-read-active-file-p)
16091     (let ((gnus-read-active-file t))
16092       (gnus-read-active-file)))
16093   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16094   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16095   (mapatoms
16096    (lambda (sym)
16097      (let ((groups 0)
16098            (group (symbol-name sym)))
16099        (if (or (null group)
16100                (gnus-gethash group gnus-killed-hashtb)
16101                (gnus-gethash group gnus-newsrc-hashtb))
16102            ()
16103          (let ((do-sub (gnus-matches-options-n group)))
16104            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16105                ()
16106              (setq groups (1+ groups))
16107              (setq gnus-killed-list
16108                    (cons group gnus-killed-list))
16109              (gnus-sethash group group gnus-killed-hashtb))))))
16110    gnus-active-hashtb))
16111
16112 ;; Get the active file(s) from the backend(s).
16113 (defun gnus-read-active-file ()
16114   (gnus-group-set-mode-line)
16115   (let ((methods 
16116          (append
16117           (if (gnus-check-server gnus-select-method)
16118               ;; The native server is available.
16119               (cons gnus-select-method gnus-secondary-select-methods)
16120             ;; The native server is down, so we just do the
16121             ;; secondary ones.
16122             gnus-secondary-select-methods)
16123           ;; Also read from the archive server.
16124           (when gnus-message-archive-method
16125             (list "archive"))))
16126         list-type)
16127     (setq gnus-have-read-active-file nil)
16128     (save-excursion
16129       (set-buffer nntp-server-buffer)
16130       (while methods
16131         (let* ((method (if (stringp (car methods))
16132                            (gnus-server-get-method nil (car methods))
16133                          (car methods)))
16134                (where (nth 1 method))
16135                (mesg (format "Reading active file%s via %s..."
16136                              (if (and where (not (zerop (length where))))
16137                                  (concat " from " where) "")
16138                              (car method))))
16139           (gnus-message 5 mesg)
16140           (when (gnus-check-server method)
16141             ;; Request that the backend scan its incoming messages.
16142             (and (gnus-check-backend-function 'request-scan (car method))
16143                  (gnus-request-scan nil method))
16144             (cond
16145              ((and (eq gnus-read-active-file 'some)
16146                    (gnus-check-backend-function 'retrieve-groups (car method)))
16147               (let ((newsrc (cdr gnus-newsrc-alist))
16148                     (gmethod (gnus-server-get-method nil method))
16149                     groups info)
16150                 (while (setq info (pop newsrc))
16151                   (when (gnus-server-equal
16152                          (gnus-find-method-for-group 
16153                           (gnus-info-group info) info)
16154                          gmethod)
16155                     (push (gnus-group-real-name (gnus-info-group info)) 
16156                           groups)))
16157                 (when groups
16158                   (gnus-check-server method)
16159                   (setq list-type (gnus-retrieve-groups groups method))
16160                   (cond
16161                    ((not list-type)
16162                     (gnus-error
16163                      1.2 "Cannot read partial active file from %s server."
16164                      (car method)))
16165                    ((eq list-type 'active)
16166                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16167                    (t
16168                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16169              (t
16170               (if (not (gnus-request-list method))
16171                   (unless (equal method gnus-message-archive-method)
16172                     (gnus-error 1 "Cannot read active file from %s server."
16173                                 (car method)))
16174                 (gnus-message 5 mesg)
16175                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16176                 ;; We mark this active file as read.
16177                 (push method gnus-have-read-active-file)
16178                 (gnus-message 5 "%sdone" mesg))))))
16179         (setq methods (cdr methods))))))
16180
16181 ;; Read an active file and place the results in `gnus-active-hashtb'.
16182 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16183   (unless method
16184     (setq method gnus-select-method))
16185   (let ((cur (current-buffer))
16186         (hashtb (or hashtb
16187                     (if (and gnus-active-hashtb
16188                              (not (equal method gnus-select-method)))
16189                         gnus-active-hashtb
16190                       (setq gnus-active-hashtb
16191                             (if (equal method gnus-select-method)
16192                                 (gnus-make-hashtable
16193                                  (count-lines (point-min) (point-max)))
16194                               (gnus-make-hashtable 4096)))))))
16195     ;; Delete unnecessary lines.
16196     (goto-char (point-min))
16197     (while (search-forward "\nto." nil t)
16198       (delete-region (1+ (match-beginning 0))
16199                      (progn (forward-line 1) (point))))
16200     (or (string= gnus-ignored-newsgroups "")
16201         (progn
16202           (goto-char (point-min))
16203           (delete-matching-lines gnus-ignored-newsgroups)))
16204     ;; Make the group names readable as a lisp expression even if they
16205     ;; contain special characters.
16206     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16207     (goto-char (point-max))
16208     (while (re-search-backward "[][';?()#]" nil t)
16209       (insert ?\\))
16210     ;; If these are groups from a foreign select method, we insert the
16211     ;; group prefix in front of the group names.
16212     (and method (not (gnus-server-equal
16213                       (gnus-server-get-method nil method)
16214                       (gnus-server-get-method nil gnus-select-method)))
16215          (let ((prefix (gnus-group-prefixed-name "" method)))
16216            (goto-char (point-min))
16217            (while (and (not (eobp))
16218                        (progn (insert prefix)
16219                               (zerop (forward-line 1)))))))
16220     ;; Store the active file in a hash table.
16221     (goto-char (point-min))
16222     (if (string-match "%[oO]" gnus-group-line-format)
16223         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16224         ;; If we want information on moderated groups, we use this
16225         ;; loop...
16226         (let* ((mod-hashtb (make-vector 7 0))
16227                (m (intern "m" mod-hashtb))
16228                group max min)
16229           (while (not (eobp))
16230             (condition-case nil
16231                 (progn
16232                   (narrow-to-region (point) (gnus-point-at-eol))
16233                   (setq group (let ((obarray hashtb)) (read cur)))
16234                   (if (and (numberp (setq max (read cur)))
16235                            (numberp (setq min (read cur)))
16236                            (progn
16237                              (skip-chars-forward " \t")
16238                              (not
16239                               (or (= (following-char) ?=)
16240                                   (= (following-char) ?x)
16241                                   (= (following-char) ?j)))))
16242                       (set group (cons min max))
16243                     (set group nil))
16244                   ;; Enter moderated groups into a list.
16245                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16246                       (setq gnus-moderated-list
16247                             (cons (symbol-name group) gnus-moderated-list))))
16248               (error
16249                (and group
16250                     (symbolp group)
16251                     (set group nil))))
16252             (widen)
16253             (forward-line 1)))
16254       ;; And if we do not care about moderation, we use this loop,
16255       ;; which is faster.
16256       (let (group max min)
16257         (while (not (eobp))
16258           (condition-case ()
16259               (progn
16260                 (narrow-to-region (point) (gnus-point-at-eol))
16261                 ;; group gets set to a symbol interned in the hash table
16262                 ;; (what a hack!!) - jwz
16263                 (setq group (let ((obarray hashtb)) (read cur)))
16264                 (if (and (numberp (setq max (read cur)))
16265                          (numberp (setq min (read cur)))
16266                          (progn
16267                            (skip-chars-forward " \t")
16268                            (not
16269                             (or (= (following-char) ?=)
16270                                 (= (following-char) ?x)
16271                                 (= (following-char) ?j)))))
16272                     (set group (cons min max))
16273                   (set group nil)))
16274             (error
16275              (progn
16276                (and group
16277                     (symbolp group)
16278                     (set group nil))
16279                (or ignore-errors
16280                    (gnus-message 3 "Warning - illegal active: %s"
16281                                  (buffer-substring
16282                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16283           (widen)
16284           (forward-line 1))))))
16285
16286 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16287   ;; Parse a "groups" active file.
16288   (let ((cur (current-buffer))
16289         (hashtb (or hashtb
16290                     (if (and method gnus-active-hashtb)
16291                         gnus-active-hashtb
16292                       (setq gnus-active-hashtb
16293                             (gnus-make-hashtable
16294                              (count-lines (point-min) (point-max)))))))
16295         (prefix (and method
16296                      (not (gnus-server-equal
16297                            (gnus-server-get-method nil method)
16298                            (gnus-server-get-method nil gnus-select-method)))
16299                      (gnus-group-prefixed-name "" method))))
16300
16301     (goto-char (point-min))
16302     ;; We split this into to separate loops, one with the prefix
16303     ;; and one without to speed the reading up somewhat.
16304     (if prefix
16305         (let (min max opoint group)
16306           (while (not (eobp))
16307             (condition-case ()
16308                 (progn
16309                   (read cur) (read cur)
16310                   (setq min (read cur)
16311                         max (read cur)
16312                         opoint (point))
16313                   (skip-chars-forward " \t")
16314                   (insert prefix)
16315                   (goto-char opoint)
16316                   (set (let ((obarray hashtb)) (read cur))
16317                        (cons min max)))
16318               (error (and group (symbolp group) (set group nil))))
16319             (forward-line 1)))
16320       (let (min max group)
16321         (while (not (eobp))
16322           (condition-case ()
16323               (if (= (following-char) ?2)
16324                   (progn
16325                     (read cur) (read cur)
16326                     (setq min (read cur)
16327                           max (read cur))
16328                     (set (setq group (let ((obarray hashtb)) (read cur)))
16329                          (cons min max))))
16330             (error (and group (symbolp group) (set group nil))))
16331           (forward-line 1))))))
16332
16333 (defun gnus-read-newsrc-file (&optional force)
16334   "Read startup file.
16335 If FORCE is non-nil, the .newsrc file is read."
16336   ;; Reset variables that might be defined in the .newsrc.eld file.
16337   (let ((variables gnus-variable-list))
16338     (while variables
16339       (set (car variables) nil)
16340       (setq variables (cdr variables))))
16341   (let* ((newsrc-file gnus-current-startup-file)
16342          (quick-file (concat newsrc-file ".el")))
16343     (save-excursion
16344       ;; We always load the .newsrc.eld file.  If always contains
16345       ;; much information that can not be gotten from the .newsrc
16346       ;; file (ticked articles, killed groups, foreign methods, etc.)
16347       (gnus-read-newsrc-el-file quick-file)
16348
16349       (if (and (file-exists-p gnus-current-startup-file)
16350                (or force
16351                    (and (file-newer-than-file-p newsrc-file quick-file)
16352                         (file-newer-than-file-p newsrc-file
16353                                                 (concat quick-file "d")))
16354                    (not gnus-newsrc-alist)))
16355           ;; We read the .newsrc file.  Note that if there if a
16356           ;; .newsrc.eld file exists, it has already been read, and
16357           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16358           ;; the .newsrc file, Gnus will only use the information it
16359           ;; can find there for changing the data already read -
16360           ;; ie. reading the .newsrc file will not trash the data
16361           ;; already read (except for read articles).
16362           (save-excursion
16363             (gnus-message 5 "Reading %s..." newsrc-file)
16364             (set-buffer (find-file-noselect newsrc-file))
16365             (buffer-disable-undo (current-buffer))
16366             (gnus-newsrc-to-gnus-format)
16367             (kill-buffer (current-buffer))
16368             (gnus-message 5 "Reading %s...done" newsrc-file)))
16369
16370       ;; Read any slave files.
16371       (unless gnus-slave
16372         (gnus-master-read-slave-newsrc))
16373       
16374       ;; Convert old to new.
16375       (gnus-convert-old-newsrc))))
16376
16377 (defun gnus-continuum-version (version)
16378   "Return VERSION as a floating point number."
16379   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16380             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16381     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16382            (number (match-string 2 version))
16383            major minor least)
16384       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16385       (setq major (string-to-number (match-string 1 number)))
16386       (setq minor (string-to-number (match-string 2 number)))
16387       (setq least (if (match-beginning 3)
16388                       (string-to-number (match-string 3 number))
16389                     0))
16390       (string-to-number
16391        (if (zerop major)
16392            (format "%s00%02d%02d"
16393                    (cond 
16394                     ((member alpha '("(ding)" "d")) "4.99")
16395                     ((member alpha '("September" "s")) "5.01")
16396                     ((member alpha '("Red" "r")) "5.03"))
16397                    minor least)
16398          (format "%d.%02d%02d" major minor least))))))
16399
16400 (defun gnus-convert-old-newsrc ()
16401   "Convert old newsrc into the new format, if needed."
16402   (let ((fcv (and gnus-newsrc-file-version
16403                   (gnus-continuum-version gnus-newsrc-file-version))))
16404     (cond
16405      ;; No .newsrc.eld file was loaded.
16406      ((null fcv) nil)
16407      ;; Gnus 5 .newsrc.eld was loaded.
16408      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16409       (gnus-convert-old-ticks)))))
16410
16411 (defun gnus-convert-old-ticks ()
16412   (let ((newsrc (cdr gnus-newsrc-alist))
16413         marks info dormant ticked)
16414     (while (setq info (pop newsrc))
16415       (when (setq marks (gnus-info-marks info))
16416         (setq dormant (cdr (assq 'dormant marks))
16417               ticked (cdr (assq 'tick marks)))
16418         (when (or dormant ticked)
16419           (gnus-info-set-read
16420            info
16421            (gnus-add-to-range
16422             (gnus-info-read info)
16423             (nconc (gnus-uncompress-range dormant)
16424                    (gnus-uncompress-range ticked)))))))))
16425
16426 (defun gnus-read-newsrc-el-file (file)
16427   (let ((ding-file (concat file "d")))
16428     ;; We always, always read the .eld file.
16429     (gnus-message 5 "Reading %s..." ding-file)
16430     (let (gnus-newsrc-assoc)
16431       (condition-case nil
16432           (load ding-file t t t)
16433         (error
16434          (gnus-error 1 "Error in %s" ding-file)))
16435       (when gnus-newsrc-assoc
16436         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16437     (gnus-make-hashtable-from-newsrc-alist)
16438     (when (file-newer-than-file-p file ding-file)
16439       ;; Old format quick file
16440       (gnus-message 5 "Reading %s..." file)
16441       ;; The .el file is newer than the .eld file, so we read that one
16442       ;; as well.
16443       (gnus-read-old-newsrc-el-file file))))
16444
16445 ;; Parse the old-style quick startup file
16446 (defun gnus-read-old-newsrc-el-file (file)
16447   (let (newsrc killed marked group m info)
16448     (prog1
16449         (let ((gnus-killed-assoc nil)
16450               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16451           (prog1
16452               (condition-case nil
16453                   (load file t t t)
16454                 (error nil))
16455             (setq newsrc gnus-newsrc-assoc
16456                   killed gnus-killed-assoc
16457                   marked gnus-marked-assoc)))
16458       (setq gnus-newsrc-alist nil)
16459       (while (setq group (pop newsrc))
16460         (if (setq info (gnus-get-info (car group)))
16461             (progn
16462               (gnus-info-set-read info (cddr group))
16463               (gnus-info-set-level
16464                info (if (nth 1 group) gnus-level-default-subscribed
16465                       gnus-level-default-unsubscribed))
16466               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16467           (push (setq info
16468                       (list (car group)
16469                             (if (nth 1 group) gnus-level-default-subscribed
16470                               gnus-level-default-unsubscribed)
16471                             (cddr group)))
16472                 gnus-newsrc-alist))
16473         ;; Copy marks into info.
16474         (when (setq m (assoc (car group) marked))
16475           (unless (nthcdr 3 info)
16476             (nconc info (list nil)))
16477           (gnus-info-set-marks
16478            info (list (cons 'tick (gnus-compress-sequence 
16479                                    (sort (cdr m) '<) t))))))
16480       (setq newsrc killed)
16481       (while newsrc
16482         (setcar newsrc (caar newsrc))
16483         (setq newsrc (cdr newsrc)))
16484       (setq gnus-killed-list killed))
16485     ;; The .el file version of this variable does not begin with
16486     ;; "options", while the .eld version does, so we just add it if it
16487     ;; isn't there.
16488     (and
16489      gnus-newsrc-options
16490      (progn
16491        (and (not (string-match "^ *options" gnus-newsrc-options))
16492             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16493        (and (not (string-match "\n$" gnus-newsrc-options))
16494             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16495        ;; Finally, if we read some options lines, we parse them.
16496        (or (string= gnus-newsrc-options "")
16497            (gnus-newsrc-parse-options gnus-newsrc-options))))
16498
16499     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16500     (gnus-make-hashtable-from-newsrc-alist)))
16501
16502 (defun gnus-make-newsrc-file (file)
16503   "Make server dependent file name by catenating FILE and server host name."
16504   (let* ((file (expand-file-name file nil))
16505          (real-file (concat file "-" (nth 1 gnus-select-method))))
16506     (if (or (file-exists-p real-file)
16507             (file-exists-p (concat real-file ".el"))
16508             (file-exists-p (concat real-file ".eld")))
16509         real-file file)))
16510
16511 (defun gnus-newsrc-to-gnus-format ()
16512   (setq gnus-newsrc-options "")
16513   (setq gnus-newsrc-options-n nil)
16514
16515   (or gnus-active-hashtb
16516       (setq gnus-active-hashtb (make-vector 4095 0)))
16517   (let ((buf (current-buffer))
16518         (already-read (> (length gnus-newsrc-alist) 1))
16519         group subscribed options-symbol newsrc Options-symbol
16520         symbol reads num1)
16521     (goto-char (point-min))
16522     ;; We intern the symbol `options' in the active hashtb so that we
16523     ;; can `eq' against it later.
16524     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16525     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16526
16527     (while (not (eobp))
16528       ;; We first read the first word on the line by narrowing and
16529       ;; then reading into `gnus-active-hashtb'.  Most groups will
16530       ;; already exist in that hashtb, so this will save some string
16531       ;; space.
16532       (narrow-to-region
16533        (point)
16534        (progn (skip-chars-forward "^ \t!:\n") (point)))
16535       (goto-char (point-min))
16536       (setq symbol
16537             (and (/= (point-min) (point-max))
16538                  (let ((obarray gnus-active-hashtb)) (read buf))))
16539       (widen)
16540       ;; Now, the symbol we have read is either `options' or a group
16541       ;; name.  If it is an options line, we just add it to a string.
16542       (cond
16543        ((or (eq symbol options-symbol)
16544             (eq symbol Options-symbol))
16545         (setq gnus-newsrc-options
16546               ;; This concating is quite inefficient, but since our
16547               ;; thorough studies show that approx 99.37% of all
16548               ;; .newsrc files only contain a single options line, we
16549               ;; don't give a damn, frankly, my dear.
16550               (concat gnus-newsrc-options
16551                       (buffer-substring
16552                        (gnus-point-at-bol)
16553                        ;; Options may continue on the next line.
16554                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16555                                 (progn (beginning-of-line) (point)))
16556                            (point)))))
16557         (forward-line -1))
16558        (symbol
16559         ;; Group names can be just numbers.  
16560         (when (numberp symbol) 
16561           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16562         (or (boundp symbol) (set symbol nil))
16563         ;; It was a group name.
16564         (setq subscribed (= (following-char) ?:)
16565               group (symbol-name symbol)
16566               reads nil)
16567         (if (eolp)
16568             ;; If the line ends here, this is clearly a buggy line, so
16569             ;; we put point a the beginning of line and let the cond
16570             ;; below do the error handling.
16571             (beginning-of-line)
16572           ;; We skip to the beginning of the ranges.
16573           (skip-chars-forward "!: \t"))
16574         ;; We are now at the beginning of the list of read articles.
16575         ;; We read them range by range.
16576         (while
16577             (cond
16578              ((looking-at "[0-9]+")
16579               ;; We narrow and read a number instead of buffer-substring/
16580               ;; string-to-int because it's faster.  narrow/widen is
16581               ;; faster than save-restriction/narrow, and save-restriction
16582               ;; produces a garbage object.
16583               (setq num1 (progn
16584                            (narrow-to-region (match-beginning 0) (match-end 0))
16585                            (read buf)))
16586               (widen)
16587               ;; If the next character is a dash, then this is a range.
16588               (if (= (following-char) ?-)
16589                   (progn
16590                     ;; We read the upper bound of the range.
16591                     (forward-char 1)
16592                     (if (not (looking-at "[0-9]+"))
16593                         ;; This is a buggy line, by we pretend that
16594                         ;; it's kinda OK.  Perhaps the user should be
16595                         ;; dinged?
16596                         (setq reads (cons num1 reads))
16597                       (setq reads
16598                             (cons
16599                              (cons num1
16600                                    (progn
16601                                      (narrow-to-region (match-beginning 0)
16602                                                        (match-end 0))
16603                                      (read buf)))
16604                              reads))
16605                       (widen)))
16606                 ;; It was just a simple number, so we add it to the
16607                 ;; list of ranges.
16608                 (setq reads (cons num1 reads)))
16609               ;; If the next char in ?\n, then we have reached the end
16610               ;; of the line and return nil.
16611               (/= (following-char) ?\n))
16612              ((= (following-char) ?\n)
16613               ;; End of line, so we end.
16614               nil)
16615              (t
16616               ;; Not numbers and not eol, so this might be a buggy
16617               ;; line...
16618               (or (eobp)
16619                   ;; If it was eob instead of ?\n, we allow it.
16620                   (progn
16621                     ;; The line was buggy.
16622                     (setq group nil)
16623                     (gnus-error 3.1 "Mangled line: %s"
16624                                 (buffer-substring (gnus-point-at-bol)
16625                                                   (gnus-point-at-eol)))))
16626               nil))
16627           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16628           ;; we allow them, because it's a common mistake to put a
16629           ;; space after the comma.
16630           (skip-chars-forward ", "))
16631
16632         ;; We have already read .newsrc.eld, so we gently update the
16633         ;; data in the hash table with the information we have just
16634         ;; read.
16635         (when group
16636           (let ((info (gnus-get-info group))
16637                 level)
16638             (if info
16639                 ;; There is an entry for this file in the alist.
16640                 (progn
16641                   (gnus-info-set-read info (nreverse reads))
16642                   ;; We update the level very gently.  In fact, we
16643                   ;; only change it if there's been a status change
16644                   ;; from subscribed to unsubscribed, or vice versa.
16645                   (setq level (gnus-info-level info))
16646                   (cond ((and (<= level gnus-level-subscribed)
16647                               (not subscribed))
16648                          (setq level (if reads
16649                                          gnus-level-default-unsubscribed
16650                                        (1+ gnus-level-default-unsubscribed))))
16651                         ((and (> level gnus-level-subscribed) subscribed)
16652                          (setq level gnus-level-default-subscribed)))
16653                   (gnus-info-set-level info level))
16654               ;; This is a new group.
16655               (setq info (list group
16656                                (if subscribed
16657                                    gnus-level-default-subscribed
16658                                  (if reads
16659                                      (1+ gnus-level-subscribed)
16660                                    gnus-level-default-unsubscribed))
16661                                (nreverse reads))))
16662             (setq newsrc (cons info newsrc))))))
16663       (forward-line 1))
16664
16665     (setq newsrc (nreverse newsrc))
16666
16667     (if (not already-read)
16668         ()
16669       ;; We now have two newsrc lists - `newsrc', which is what we
16670       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16671       ;; what we've read from .newsrc.eld.  We have to merge these
16672       ;; lists.  We do this by "attaching" any (foreign) groups in the
16673       ;; gnus-newsrc-alist to the (native) group that precedes them.
16674       (let ((rc (cdr gnus-newsrc-alist))
16675             (prev gnus-newsrc-alist)
16676             entry mentry)
16677         (while rc
16678           (or (null (nth 4 (car rc)))   ; It's a native group.
16679               (assoc (caar rc) newsrc) ; It's already in the alist.
16680               (if (setq entry (assoc (caar prev) newsrc))
16681                   (setcdr (setq mentry (memq entry newsrc))
16682                           (cons (car rc) (cdr mentry)))
16683                 (setq newsrc (cons (car rc) newsrc))))
16684           (setq prev rc
16685                 rc (cdr rc)))))
16686
16687     (setq gnus-newsrc-alist newsrc)
16688     ;; We make the newsrc hashtb.
16689     (gnus-make-hashtable-from-newsrc-alist)
16690
16691     ;; Finally, if we read some options lines, we parse them.
16692     (or (string= gnus-newsrc-options "")
16693         (gnus-newsrc-parse-options gnus-newsrc-options))))
16694
16695 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16696 ;; The return value will be a list on the form
16697 ;; ((regexp1 . ignore)
16698 ;;  (regexp2 . subscribe)...)
16699 ;; When handling new newsgroups, groups that match a `ignore' regexp
16700 ;; will be ignored, and groups that match a `subscribe' regexp will be
16701 ;; subscribed.  A line like
16702 ;; options -n !all rec.all
16703 ;; will lead to a list that looks like
16704 ;; (("^rec\\..+" . subscribe)
16705 ;;  ("^.+" . ignore))
16706 ;; So all "rec.*" groups will be subscribed, while all the other
16707 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16708 ;; different from "options -n rec.all !all".
16709 (defun gnus-newsrc-parse-options (options)
16710   (let (out eol)
16711     (save-excursion
16712       (gnus-set-work-buffer)
16713       (insert (regexp-quote options))
16714       ;; First we treat all continuation lines.
16715       (goto-char (point-min))
16716       (while (re-search-forward "\n[ \t]+" nil t)
16717         (replace-match " " t t))
16718       ;; Then we transform all "all"s into ".+"s.
16719       (goto-char (point-min))
16720       (while (re-search-forward "\\ball\\b" nil t)
16721         (replace-match ".+" t t))
16722       (goto-char (point-min))
16723       ;; We remove all other options than the "-n" ones.
16724       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16725         (replace-match " ")
16726         (forward-char -1))
16727       (goto-char (point-min))
16728
16729       ;; We are only interested in "options -n" lines - we
16730       ;; ignore the other option lines.
16731       (while (re-search-forward "[ \t]-n" nil t)
16732         (setq eol
16733               (or (save-excursion
16734                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16735                          (- (point) 2)))
16736                   (gnus-point-at-eol)))
16737         ;; Search for all "words"...
16738         (while (re-search-forward "[^ \t,\n]+" eol t)
16739           (if (= (char-after (match-beginning 0)) ?!)
16740               ;; If the word begins with a bang (!), this is a "not"
16741               ;; spec.  We put this spec (minus the bang) and the
16742               ;; symbol `ignore' into the list.
16743               (setq out (cons (cons (concat
16744                                      "^" (buffer-substring
16745                                           (1+ (match-beginning 0))
16746                                           (match-end 0)))
16747                                     'ignore) out))
16748             ;; There was no bang, so this is a "yes" spec.
16749             (setq out (cons (cons (concat "^" (match-string 0))
16750                                   'subscribe) out)))))
16751
16752       (setq gnus-newsrc-options-n out))))
16753
16754 (defun gnus-save-newsrc-file (&optional force)
16755   "Save .newsrc file."
16756   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16757   ;; from the variable gnus-newsrc-alist.
16758   (when (and (or gnus-newsrc-alist gnus-killed-list)
16759              gnus-current-startup-file)
16760     (save-excursion
16761       (if (and (or gnus-use-dribble-file gnus-slave)
16762                (not force)
16763                (or (not gnus-dribble-buffer)
16764                    (not (buffer-name gnus-dribble-buffer))
16765                    (zerop (save-excursion
16766                             (set-buffer gnus-dribble-buffer)
16767                             (buffer-size)))))
16768           (gnus-message 4 "(No changes need to be saved)")
16769         (run-hooks 'gnus-save-newsrc-hook)
16770         (if gnus-slave
16771             (gnus-slave-save-newsrc)
16772           ;; Save .newsrc.
16773           (when gnus-save-newsrc-file
16774             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16775             (gnus-gnus-to-newsrc-format)
16776             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16777           ;; Save .newsrc.eld.
16778           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16779           (make-local-variable 'version-control)
16780           (setq version-control 'never)
16781           (setq buffer-file-name
16782                 (concat gnus-current-startup-file ".eld"))
16783           (setq default-directory (file-name-directory buffer-file-name))
16784           (gnus-add-current-to-buffer-list)
16785           (buffer-disable-undo (current-buffer))
16786           (erase-buffer)
16787           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16788           (gnus-gnus-to-quick-newsrc-format)
16789           (run-hooks 'gnus-save-quick-newsrc-hook)
16790           (save-buffer)
16791           (kill-buffer (current-buffer))
16792           (gnus-message
16793            5 "Saving %s.eld...done" gnus-current-startup-file))
16794         (gnus-dribble-delete-file)
16795         (gnus-group-set-mode-line)))))
16796
16797 (defun gnus-gnus-to-quick-newsrc-format ()
16798   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16799   (insert ";; Gnus startup file.\n")
16800   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16801   (insert ";; to read .newsrc.\n")
16802   (insert "(setq gnus-newsrc-file-version "
16803           (prin1-to-string gnus-version) ")\n")
16804   (let ((variables
16805          (if gnus-save-killed-list gnus-variable-list
16806            ;; Remove the `gnus-killed-list' from the list of variables
16807            ;; to be saved, if required.
16808            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16809         ;; Peel off the "dummy" group.
16810         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16811         variable)
16812     ;; Insert the variables into the file.
16813     (while variables
16814       (when (and (boundp (setq variable (pop variables)))
16815                  (symbol-value variable))
16816         (insert "(setq " (symbol-name variable) " '")
16817         (prin1 (symbol-value variable) (current-buffer))
16818         (insert ")\n")))))
16819
16820 (defun gnus-gnus-to-newsrc-format ()
16821   ;; Generate and save the .newsrc file.
16822   (save-excursion
16823     (set-buffer (create-file-buffer gnus-current-startup-file))
16824     (let ((newsrc (cdr gnus-newsrc-alist))
16825           (standard-output (current-buffer))
16826           info ranges range method)
16827       (setq buffer-file-name gnus-current-startup-file)
16828       (setq default-directory (file-name-directory buffer-file-name))
16829       (buffer-disable-undo (current-buffer))
16830       (erase-buffer)
16831       ;; Write options.
16832       (if gnus-newsrc-options (insert gnus-newsrc-options))
16833       ;; Write subscribed and unsubscribed.
16834       (while (setq info (pop newsrc))
16835         ;; Don't write foreign groups to .newsrc.
16836         (when (or (null (setq method (gnus-info-method info)))
16837                   (equal method "native")
16838                   (gnus-server-equal method gnus-select-method))
16839           (insert (gnus-info-group info)
16840                   (if (> (gnus-info-level info) gnus-level-subscribed)
16841                       "!" ":"))
16842           (when (setq ranges (gnus-info-read info))
16843             (insert " ")
16844             (if (not (listp (cdr ranges)))
16845                 (if (= (car ranges) (cdr ranges))
16846                     (princ (car ranges))
16847                   (princ (car ranges))
16848                   (insert "-")
16849                   (princ (cdr ranges)))
16850               (while (setq range (pop ranges))
16851                 (if (or (atom range) (= (car range) (cdr range)))
16852                     (princ (or (and (atom range) range) (car range)))
16853                   (princ (car range))
16854                   (insert "-")
16855                   (princ (cdr range)))
16856                 (if ranges (insert ",")))))
16857           (insert "\n")))
16858       (make-local-variable 'version-control)
16859       (setq version-control 'never)
16860       ;; It has been reported that sometime the modtime on the .newsrc
16861       ;; file seems to be off.  We really do want to overwrite it, so
16862       ;; we clear the modtime here before saving.  It's a bit odd,
16863       ;; though...
16864       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16865       ;; delete the silly thing entirely first.  but this fails to provide
16866       ;; such niceties as .newsrc~ creation.
16867       (if gnus-modtime-botch
16868           (delete-file gnus-startup-file)
16869         (clear-visited-file-modtime))
16870       (run-hooks 'gnus-save-standard-newsrc-hook)
16871       (save-buffer)
16872       (kill-buffer (current-buffer)))))
16873
16874 \f
16875 ;;;
16876 ;;; Slave functions.
16877 ;;;
16878
16879 (defun gnus-slave-save-newsrc ()
16880   (save-excursion
16881     (set-buffer gnus-dribble-buffer)
16882     (let ((slave-name
16883            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16884       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16885
16886 (defun gnus-master-read-slave-newsrc ()
16887   (let ((slave-files
16888          (directory-files
16889           (file-name-directory gnus-current-startup-file)
16890           t (concat
16891              "^" (regexp-quote
16892                   (concat
16893                    (file-name-nondirectory gnus-current-startup-file)
16894                    "-slave-")))
16895           t))
16896         file)
16897     (if (not slave-files)
16898         ()                              ; There are no slave files to read.
16899       (gnus-message 7 "Reading slave newsrcs...")
16900       (save-excursion
16901         (set-buffer (get-buffer-create " *gnus slave*"))
16902         (buffer-disable-undo (current-buffer))
16903         (setq slave-files
16904               (sort (mapcar (lambda (file)
16905                               (list (nth 5 (file-attributes file)) file))
16906                             slave-files)
16907                     (lambda (f1 f2)
16908                       (or (< (caar f1) (caar f2))
16909                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16910         (while slave-files
16911           (erase-buffer)
16912           (setq file (nth 1 (car slave-files)))
16913           (insert-file-contents file)
16914           (if (condition-case ()
16915                   (progn
16916                     (eval-buffer (current-buffer))
16917                     t)
16918                 (error
16919                  (gnus-error 3.2 "Possible error in %s" file)
16920                  nil))
16921               (or gnus-slave ; Slaves shouldn't delete these files.
16922                   (condition-case ()
16923                       (delete-file file)
16924                     (error nil))))
16925           (setq slave-files (cdr slave-files))))
16926       (gnus-message 7 "Reading slave newsrcs...done"))))
16927
16928 \f
16929 ;;;
16930 ;;; Group description.
16931 ;;;
16932
16933 (defun gnus-read-all-descriptions-files ()
16934   (let ((methods (cons gnus-select-method 
16935                        (nconc
16936                         (when gnus-message-archive-method
16937                           (list "archive"))
16938                         gnus-secondary-select-methods))))
16939     (while methods
16940       (gnus-read-descriptions-file (car methods))
16941       (setq methods (cdr methods)))
16942     t))
16943
16944 (defun gnus-read-descriptions-file (&optional method)
16945   (let ((method (or method gnus-select-method)))
16946     (when (stringp method)
16947       (setq method (gnus-server-to-method method)))
16948     ;; We create the hashtable whether we manage to read the desc file
16949     ;; to avoid trying to re-read after a failed read.
16950     (or gnus-description-hashtb
16951         (setq gnus-description-hashtb
16952               (gnus-make-hashtable (length gnus-active-hashtb))))
16953     ;; Mark this method's desc file as read.
16954     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16955                   gnus-description-hashtb)
16956
16957     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16958     (cond
16959      ((not (gnus-check-server method))
16960       (gnus-message 1 "Couldn't open server")
16961       nil)
16962      ((not (gnus-request-list-newsgroups method))
16963       (gnus-message 1 "Couldn't read newsgroups descriptions")
16964       nil)
16965      (t
16966       (let (group)
16967         (save-excursion
16968           (save-restriction
16969             (set-buffer nntp-server-buffer)
16970             (goto-char (point-min))
16971             (if (or (search-forward "\n.\n" nil t)
16972                     (goto-char (point-max)))
16973                 (progn
16974                   (beginning-of-line)
16975                   (narrow-to-region (point-min) (point))))
16976             (goto-char (point-min))
16977             (while (not (eobp))
16978               ;; If we get an error, we set group to 0, which is not a
16979               ;; symbol...
16980               (setq group
16981                     (condition-case ()
16982                         (let ((obarray gnus-description-hashtb))
16983                           ;; Group is set to a symbol interned in this
16984                           ;; hash table.
16985                           (read nntp-server-buffer))
16986                       (error 0)))
16987               (skip-chars-forward " \t")
16988               ;; ...  which leads to this line being effectively ignored.
16989               (and (symbolp group)
16990                    (set group (buffer-substring
16991                                (point) (progn (end-of-line) (point)))))
16992               (forward-line 1))))
16993         (gnus-message 5 "Reading descriptions file...done")
16994         t)))))
16995
16996 (defun gnus-group-get-description (group)
16997   "Get the description of a group by sending XGTITLE to the server."
16998   (when (gnus-request-group-description group)
16999     (save-excursion
17000       (set-buffer nntp-server-buffer)
17001       (goto-char (point-min))
17002       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17003         (match-string 1)))))
17004
17005 \f
17006 ;;;
17007 ;;; Buffering of read articles.
17008 ;;;
17009
17010 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17011 (defvar gnus-backlog-articles nil)
17012 (defvar gnus-backlog-hashtb nil)
17013
17014 (defun gnus-backlog-buffer ()
17015   "Return the backlog buffer."
17016   (or (get-buffer gnus-backlog-buffer)
17017       (save-excursion
17018         (set-buffer (get-buffer-create gnus-backlog-buffer))
17019         (buffer-disable-undo (current-buffer))
17020         (setq buffer-read-only t)
17021         (gnus-add-current-to-buffer-list)
17022         (get-buffer gnus-backlog-buffer))))
17023
17024 (defun gnus-backlog-setup ()
17025   "Initialize backlog variables."
17026   (unless gnus-backlog-hashtb
17027     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17028
17029 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17030
17031 (defun gnus-backlog-shutdown ()
17032   "Clear all backlog variables and buffers."
17033   (when (get-buffer gnus-backlog-buffer)
17034     (kill-buffer gnus-backlog-buffer))
17035   (setq gnus-backlog-hashtb nil
17036         gnus-backlog-articles nil))
17037
17038 (defun gnus-backlog-enter-article (group number buffer)
17039   (gnus-backlog-setup)
17040   (let ((ident (intern (concat group ":" (int-to-string number))
17041                        gnus-backlog-hashtb))
17042         b)
17043     (if (memq ident gnus-backlog-articles)
17044         () ; It's already kept.
17045       ;; Remove the oldest article, if necessary.
17046       (and (numberp gnus-keep-backlog)
17047            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17048            (gnus-backlog-remove-oldest-article))
17049       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17050       ;; Insert the new article.
17051       (save-excursion
17052         (set-buffer (gnus-backlog-buffer))
17053         (let (buffer-read-only)
17054           (goto-char (point-max))
17055           (or (bolp) (insert "\n"))
17056           (setq b (point))
17057           (insert-buffer-substring buffer)
17058           ;; Tag the beginning of the article with the ident.
17059           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17060
17061 (defun gnus-backlog-remove-oldest-article ()
17062   (save-excursion
17063     (set-buffer (gnus-backlog-buffer))
17064     (goto-char (point-min))
17065     (if (zerop (buffer-size))
17066         () ; The buffer is empty.
17067       (let ((ident (get-text-property (point) 'gnus-backlog))
17068             buffer-read-only)
17069         ;; Remove the ident from the list of articles.
17070         (when ident
17071           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17072         ;; Delete the article itself.
17073         (delete-region
17074          (point) (next-single-property-change
17075                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17076
17077 (defun gnus-backlog-remove-article (group number)
17078   "Remove article NUMBER in GROUP from the backlog."
17079   (when (numberp number)
17080     (gnus-backlog-setup)
17081     (let ((ident (intern (concat group ":" (int-to-string number))
17082                          gnus-backlog-hashtb))
17083           beg end)
17084       (when (memq ident gnus-backlog-articles)
17085         ;; It was in the backlog.
17086         (save-excursion
17087           (set-buffer (gnus-backlog-buffer))
17088           (let (buffer-read-only)
17089             (when (setq beg (text-property-any
17090                              (point-min) (point-max) 'gnus-backlog
17091                              ident))
17092               ;; Find the end (i. e., the beginning of the next article).
17093               (setq end
17094                     (next-single-property-change
17095                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17096               (delete-region beg end)
17097               ;; Return success.
17098               t)))))))
17099
17100 (defun gnus-backlog-request-article (group number buffer)
17101   (when (numberp number)
17102     (gnus-backlog-setup)
17103     (let ((ident (intern (concat group ":" (int-to-string number))
17104                          gnus-backlog-hashtb))
17105           beg end)
17106       (when (memq ident gnus-backlog-articles)
17107         ;; It was in the backlog.
17108         (save-excursion
17109           (set-buffer (gnus-backlog-buffer))
17110           (if (not (setq beg (text-property-any
17111                               (point-min) (point-max) 'gnus-backlog
17112                               ident)))
17113               ;; It wasn't in the backlog after all.
17114               (ignore
17115                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17116             ;; Find the end (i. e., the beginning of the next article).
17117             (setq end
17118                   (next-single-property-change
17119                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17120         (let ((buffer-read-only nil))
17121           (erase-buffer)
17122           (insert-buffer-substring gnus-backlog-buffer beg end)
17123           t)))))
17124
17125 ;; Allow redefinition of Gnus functions.
17126
17127 (gnus-ems-redefine)
17128
17129 (provide 'gnus)
17130
17131 ;;; gnus.el ends here