*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35
36 (eval-when-compile (require 'cl))
37
38 ;; Site dependent variables.  These variables should be defined in
39 ;; paths.el.
40
41 (defvar gnus-default-nntp-server nil
42   "Specify a default NNTP server.
43 This variable should be defined in paths.el, and should never be set
44 by the user.
45 If you want to change servers, you should use `gnus-select-method'.
46 See the documentation to that variable.")
47
48 (defvar gnus-backup-default-subscribed-newsgroups
49   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
50   "Default default new newsgroups the first time Gnus is run.
51 Should be set in paths.el, and shouldn't be touched by the user.")
52
53 (defvar gnus-local-domain nil
54   "Local domain name without a host name.
55 The DOMAINNAME environment variable is used instead if it is defined.
56 If the `system-name' function returns the full Internet name, there is
57 no need to set this variable.")
58
59 (defvar gnus-local-organization nil
60   "String with a description of what organization (if any) the user belongs to.
61 The ORGANIZATION environment variable is used instead if it is defined.
62 If this variable contains a function, this function will be called
63 with the current newsgroup name as the argument.  The function should
64 return a string.
65
66 In any case, if the string (either in the variable, in the environment
67 variable, or returned by the function) is a file name, the contents of
68 this file will be used as the organization.")
69
70 ;; Customization variables
71
72 ;; Don't touch this variable.
73 (defvar gnus-nntp-service "nntp"
74   "*NNTP service name (\"nntp\" or 119).
75 This is an obsolete variable, which is scarcely used.  If you use an
76 nntp server for your newsgroup and want to change the port number
77 used to 899, you would say something along these lines:
78
79  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
80
81 (defvar gnus-nntpserver-file "/etc/nntpserver"
82   "*A file with only the name of the nntp server in it.")
83
84 ;; This function is used to check both the environment variable
85 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
86 ;; an nntp server name default.
87 (defun gnus-getenv-nntpserver ()
88   (or (getenv "NNTPSERVER")
89       (and (file-readable-p gnus-nntpserver-file)
90            (save-excursion
91              (set-buffer (get-buffer-create " *gnus nntp*"))
92              (buffer-disable-undo (current-buffer))
93              (insert-file-contents gnus-nntpserver-file)
94              (let ((name (buffer-string)))
95                (prog1
96                    (if (string-match "^[ \t\n]*$" name)
97                        nil
98                      name)
99                  (kill-buffer (current-buffer))))))))
100
101 (defvar gnus-select-method
102   (nconc
103    (list 'nntp (or (condition-case ()
104                        (gnus-getenv-nntpserver)
105                      (error nil))
106                    (if (and gnus-default-nntp-server
107                             (not (string= gnus-default-nntp-server "")))
108                        gnus-default-nntp-server)
109                    (system-name)))
110    (if (or (null gnus-nntp-service)
111            (equal gnus-nntp-service "nntp"))
112        nil
113      (list gnus-nntp-service)))
114   "*Default method for selecting a newsgroup.
115 This variable should be a list, where the first element is how the
116 news is to be fetched, the second is the address.
117
118 For instance, if you want to get your news via NNTP from
119 \"flab.flab.edu\", you could say:
120
121 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
122
123 If you want to use your local spool, say:
124
125 (setq gnus-select-method (list 'nnspool (system-name)))
126
127 If you use this variable, you must set `gnus-nntp-server' to nil.
128
129 There is a lot more to know about select methods and virtual servers -
130 see the manual for details.")
131
132 (defvar gnus-message-archive-method 
133   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
134              (nnfolder-active-file "~/Mail/archive/active")
135              (nnfolder-get-new-mail nil)
136              (nnfolder-inhibit-expiry t))
137   "*Method used for archiving messages you've sent.
138 This should be a mail method.")
139
140 (defvar gnus-refer-article-method nil
141   "*Preferred method for fetching an article by Message-ID.
142 If you are reading news from the local spool (with nnspool), fetching
143 articles by Message-ID is painfully slow.  By setting this method to an
144 nntp method, you might get acceptable results.
145
146 The value of this variable must be a valid select method as discussed
147 in the documentation of `gnus-select-method'.")
148
149 (defvar gnus-secondary-select-methods nil
150   "*A list of secondary methods that will be used for reading news.
151 This is a list where each element is a complete select method (see
152 `gnus-select-method').
153
154 If, for instance, you want to read your mail with the nnml backend,
155 you could set this variable:
156
157 (setq gnus-secondary-select-methods '((nnml \"\")))")
158
159 (defvar gnus-secondary-servers nil
160   "*List of NNTP servers that the user can choose between interactively.
161 To make Gnus query you for a server, you have to give `gnus' a
162 non-numeric prefix - `C-u M-x gnus', in short.")
163
164 (defvar gnus-nntp-server nil
165   "*The name of the host running the NNTP server.
166 This variable is semi-obsolete.  Use the `gnus-select-method'
167 variable instead.")
168
169 (defvar gnus-startup-file "~/.newsrc"
170   "*Your `.newsrc' file.
171 `.newsrc-SERVER' will be used instead if that exists.")
172
173 (defvar gnus-init-file "~/.gnus"
174   "*Your Gnus elisp startup file.
175 If a file with the .el or .elc suffixes exist, it will be read
176 instead.")
177
178 (defvar gnus-group-faq-directory
179   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
180     "/ftp@sunsite.auc.dk:/pub/usenet/"
181     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
182     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
183     "/ftp@rtfm.mit.edu:/pub/usenet/"
184     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
185     "/ftp@ftp.sunet.se:/pub/usenet/"
186     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
187     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
188     "/ftp@ftp.hk.super.net:/mirror/faqs/")
189   "*Directory where the group FAQs are stored.
190 This will most commonly be on a remote machine, and the file will be
191 fetched by ange-ftp.
192
193 This variable can also be a list of directories.  In that case, the
194 first element in the list will be used by default, and the others will
195 be used as backup sites.
196
197 Note that Gnus uses an aol machine as the default directory.  If this
198 feels fundamentally unclean, just think of it as a way to finally get
199 something of value back from them.
200
201 If the default site is too slow, try one of these:
202
203    North America: mirrors.aol.com                /pub/rtfm/usenet
204                   ftp.seas.gwu.edu               /pub/rtfm
205                   rtfm.mit.edu                   /pub/usenet
206    Europe:        ftp.uni-paderborn.de           /pub/FAQ
207                   src.doc.ic.ac.uk               /usenet/news-FAQS
208                   ftp.sunet.se                   /pub/usenet
209                   sunsite.auc.dk                 /pub/usenet
210    Asia:          nctuccca.edu.tw                /USENET/FAQ
211                   hwarang.postech.ac.kr          /pub/usenet
212                   ftp.hk.super.net               /mirror/faqs")
213
214 (defvar gnus-group-archive-directory
215   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
216   "*The address of the (ding) archives.")
217
218 (defvar gnus-group-recent-archive-directory
219   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
220   "*The address of the most recent (ding) articles.")
221
222 (defvar gnus-default-subscribed-newsgroups nil
223   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
224 It should be a list of strings.
225 If it is `t', Gnus will not do anything special the first time it is
226 started; it'll just use the normal newsgroups subscription methods.")
227
228 (defvar gnus-use-cross-reference t
229   "*Non-nil means that cross referenced articles will be marked as read.
230 If nil, ignore cross references.  If t, mark articles as read in
231 subscribed newsgroups.  If neither t nor nil, mark as read in all
232 newsgroups.")
233
234 (defvar gnus-single-article-buffer t
235   "*If non-nil, display all articles in the same buffer.
236 If nil, each group will get its own article buffer.")
237
238 (defvar gnus-use-dribble-file t
239   "*Non-nil means that Gnus will use a dribble file to store user updates.
240 If Emacs should crash without saving the .newsrc files, complete
241 information can be restored from the dribble file.")
242
243 (defvar gnus-dribble-directory nil
244   "*The directory where dribble files will be saved.
245 If this variable is nil, the directory where the .newsrc files are
246 saved will be used.")
247
248 (defvar gnus-asynchronous nil
249   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
250
251 (defvar gnus-kill-summary-on-exit t
252   "*If non-nil, kill the summary buffer when you exit from it.
253 If nil, the summary will become a \"*Dead Summary*\" buffer, and
254 it will be killed sometime later.")
255
256 (defvar gnus-large-newsgroup 200
257   "*The number of articles which indicates a large newsgroup.
258 If the number of articles in a newsgroup is greater than this value,
259 confirmation is required for selecting the newsgroup.")
260
261 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
262 (defvar gnus-no-groups-message "No news is horrible news"
263   "*Message displayed by Gnus when no groups are available.")
264
265 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
266   "*Non-nil means that the default name of a file to save articles in is the group name.
267 If it's nil, the directory form of the group name is used instead.
268
269 If this variable is a list, and the list contains the element
270 `not-score', long file names will not be used for score files; if it
271 contains the element `not-save', long file names will not be used for
272 saving; and if it contains the element `not-kill', long file names
273 will not be used for kill files.")
274
275 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
276   "*Name of the directory articles will be saved in (default \"~/News\").
277 Initialized from the SAVEDIR environment variable.")
278
279 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
280   "*Name of the directory where kill files will be stored (default \"~/News\").
281 Initialized from the SAVEDIR environment variable.")
282
283 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
284   "*A function to save articles in your favorite format.
285 The function must be interactively callable (in other words, it must
286 be an Emacs command).
287
288 Gnus provides the following functions:
289
290 * gnus-summary-save-in-rmail (Rmail format)
291 * gnus-summary-save-in-mail (Unix mail format)
292 * gnus-summary-save-in-folder (MH folder)
293 * gnus-summary-save-in-file (article format).
294 * gnus-summary-save-in-vm (use VM's folder format).")
295
296 (defvar gnus-prompt-before-saving 'always
297   "*This variable says how much prompting is to be done when saving articles.
298 If it is nil, no prompting will be done, and the articles will be
299 saved to the default files.  If this variable is `always', each and
300 every article that is saved will be preceded by a prompt, even when
301 saving large batches of articles.  If this variable is neither nil not
302 `always', there the user will be prompted once for a file name for
303 each invocation of the saving commands.")
304
305 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
306   "*A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
308
309 (defvar gnus-mail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Unix mail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-folder-save-name (function gnus-folder-save-name)
314   "*A function generating a file name to save articles in MH folder.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
316
317 (defvar gnus-file-save-name (function gnus-numeric-save-name)
318   "*A function generating a file name to save articles in article format.
319 The function is called with NEWSGROUP, HEADERS, and optional
320 LAST-FILE.")
321
322 (defvar gnus-split-methods
323   '((gnus-article-archive-name))
324   "*Variable used to suggest where articles are to be saved.
325 For instance, if you would like to save articles related to Gnus in
326 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
327 you could set this variable to something like:
328
329  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
330    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
331
332 This variable is an alist where the where the key is the match and the
333 value is a list of possible files to save in if the match is non-nil.
334
335 If the match is a string, it is used as a regexp match on the
336 article.  If the match is a symbol, that symbol will be funcalled
337 from the buffer of the article to be saved with the newsgroup as the
338 parameter.  If it is a list, it will be evaled in the same buffer.
339
340 If this form or function returns a string, this string will be used as
341 a possible file name; and if it returns a non-nil list, that list will
342 be used as possible file names.")
343
344 (defvar gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable.")
347
348 (defvar gnus-save-score nil
349   "*If non-nil, save group scoring info.")
350
351 (defvar gnus-use-adaptive-scoring nil
352   "*If non-nil, use some adaptive scoring scheme.")
353
354 (defvar gnus-use-cache nil
355   "*If nil, Gnus will ignore the article cache.
356 If `passive', it will allow entering (and reading) articles
357 explicitly entered into the cache.  If anything else, use the
358 cache to the full extent of the law.")
359
360 (defvar gnus-use-trees nil
361   "*If non-nil, display a thread tree buffer.")
362
363 (defvar gnus-use-grouplens nil
364   "*If non-nil, use GroupLens ratings.")
365
366 (defvar gnus-keep-backlog nil
367   "*If non-nil, Gnus will keep read articles for later re-retrieval.
368 If it is a number N, then Gnus will only keep the last N articles
369 read.  If it is neither nil nor a number, Gnus will keep all read
370 articles.  This is not a good idea.")
371
372 (defvar gnus-use-nocem nil
373   "*If non-nil, Gnus will read NoCeM cancel messages.")
374
375 (defvar gnus-use-demon nil
376   "If non-nil, Gnus might use some demons.")
377
378 (defvar gnus-use-scoring t
379   "*If non-nil, enable scoring.")
380
381 (defvar gnus-use-picons nil
382   "*If non-nil, display picons.")
383
384 (defvar gnus-fetch-old-headers nil
385   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
386 If an unread article in the group refers to an older, already read (or
387 just marked as read) article, the old article will not normally be
388 displayed in the Summary buffer.  If this variable is non-nil, Gnus
389 will attempt to grab the headers to the old articles, and thereby
390 build complete threads.  If it has the value `some', only enough
391 headers to connect otherwise loose threads will be displayed.
392 This variable can also be a number.  In that case, no more than that
393 number of old headers will be fetched.
394
395 The server has to support NOV for any of this to work.")
396
397 ;see gnus-cus.el
398 ;(defvar gnus-visual t
399 ;  "*If non-nil, will do various highlighting.
400 ;If nil, no mouse highlights (or any other highlights) will be
401 ;performed.  This might speed up Gnus some when generating large group
402 ;and summary buffers.")
403
404 (defvar gnus-novice-user t
405   "*Non-nil means that you are a usenet novice.
406 If non-nil, verbose messages may be displayed and confirmations may be
407 required.")
408
409 (defvar gnus-expert-user nil
410   "*Non-nil means that you will never be asked for confirmation about anything.
411 And that means *anything*.")
412
413 (defvar gnus-verbose 7
414   "*Integer that says how verbose Gnus should be.
415 The higher the number, the more messages Gnus will flash to say what
416 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
417 display most important messages; and at ten, Gnus will keep on
418 jabbering all the time.")
419
420 (defvar gnus-keep-same-level nil
421   "*Non-nil means that the next newsgroup after the current will be on the same level.
422 When you type, for instance, `n' after reading the last article in the
423 current newsgroup, you will go to the next newsgroup.  If this variable
424 is nil, the next newsgroup will be the next from the group
425 buffer.
426 If this variable is non-nil, Gnus will either put you in the
427 next newsgroup with the same level, or, if no such newsgroup is
428 available, the next newsgroup with the lowest possible level higher
429 than the current level.
430 If this variable is `best', Gnus will make the next newsgroup the one
431 with the best level.")
432
433 (defvar gnus-summary-make-false-root 'adopt
434   "*nil means that Gnus won't gather loose threads.
435 If the root of a thread has expired or been read in a previous
436 session, the information necessary to build a complete thread has been
437 lost.  Instead of having many small sub-threads from this original thread
438 scattered all over the summary buffer, Gnus can gather them.
439
440 If non-nil, Gnus will try to gather all loose sub-threads from an
441 original thread into one large thread.
442
443 If this variable is non-nil, it should be one of `none', `adopt',
444 `dummy' or `empty'.
445
446 If this variable is `none', Gnus will not make a false root, but just
447 present the sub-threads after another.
448 If this variable is `dummy', Gnus will create a dummy root that will
449 have all the sub-threads as children.
450 If this variable is `adopt', Gnus will make one of the \"children\"
451 the parent and mark all the step-children as such.
452 If this variable is `empty', the \"children\" are printed with empty
453 subject fields.  (Or rather, they will be printed with a string
454 given by the `gnus-summary-same-subject' variable.)")
455
456 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
457   "*A regexp to match subjects to be excluded from loose thread gathering.
458 As loose thread gathering is done on subjects only, that means that
459 there can be many false gatherings performed.  By rooting out certain
460 common subjects, gathering might become saner.")
461
462 (defvar gnus-summary-gather-subject-limit nil
463   "*Maximum length of subject comparisons when gathering loose threads.
464 Use nil to compare full subjects.  Setting this variable to a low
465 number will help gather threads that have been corrupted by
466 newsreaders chopping off subject lines, but it might also mean that
467 unrelated articles that have subject that happen to begin with the
468 same few characters will be incorrectly gathered.
469
470 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
471 comparing subjects.")
472
473 (defvar gnus-simplify-ignored-prefixes nil
474   "*Regexp, matches for which are removed from subject lines when simplifying.")
475
476 (defvar gnus-build-sparse-threads nil
477   "*If non-nil, fill in the gaps in threads.
478 If `some', only fill in the gaps that are needed to tie loose threads
479 together.  If `more', fill in all leaf nodes that Gnus can find.  If
480 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
481
482 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
483   "Function used for gathering loose threads.
484 There are two pre-defined functions: `gnus-gather-threads-by-subject',
485 which only takes Subjects into consideration; and
486 `gnus-gather-threads-by-references', which compared the References
487 headers of the articles to find matches.")
488
489 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
490 (defvar gnus-summary-same-subject ""
491   "*String indicating that the current article has the same subject as the previous.
492 This variable will only be used if the value of
493 `gnus-summary-make-false-root' is `empty'.")
494
495 (defvar gnus-summary-goto-unread t
496   "*If non-nil, marking commands will go to the next unread article.
497 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
498 whether it is read or not.")
499
500 (defvar gnus-group-goto-unread t
501   "*If non-nil, movement commands will go to the next unread and subscribed group.")
502
503 (defvar gnus-goto-next-group-when-activating t
504   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
505
506 (defvar gnus-check-new-newsgroups t
507   "*Non-nil means that Gnus will add new newsgroups at startup.
508 If this variable is `ask-server', Gnus will ask the server for new
509 groups since the last time it checked.  This means that the killed list
510 is no longer necessary, so you could set `gnus-save-killed-list' to
511 nil.
512
513 A variant is to have this variable be a list of select methods.  Gnus
514 will then use the `ask-server' method on all these select methods to
515 query for new groups from all those servers.
516
517 Eg.
518   (setq gnus-check-new-newsgroups
519         '((nntp \"some.server\") (nntp \"other.server\")))
520
521 If this variable is nil, then you have to tell Gnus explicitly to
522 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
523
524 (defvar gnus-check-bogus-newsgroups nil
525   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
528
529 (defvar gnus-read-active-file t
530   "*Non-nil means that Gnus will read the entire active file at startup.
531 If this variable is nil, Gnus will only know about the groups in your
532 `.newsrc' file.
533
534 If this variable is `some', Gnus will try to only read the relevant
535 parts of the active file from the server.  Not all servers support
536 this, and it might be quite slow with other servers, but this should
537 generally be faster than both the t and nil value.
538
539 If you set this variable to nil or `some', you probably still want to
540 be told about new newsgroups that arrive.  To do that, set
541 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
542 properly with all servers.")
543
544 (defvar gnus-level-subscribed 5
545   "*Groups with levels less than or equal to this variable are subscribed.")
546
547 (defvar gnus-level-unsubscribed 7
548   "*Groups with levels less than or equal to this variable are unsubscribed.
549 Groups with levels less than `gnus-level-subscribed', which should be
550 less than this variable, are subscribed.")
551
552 (defvar gnus-level-zombie 8
553   "*Groups with this level are zombie groups.")
554
555 (defvar gnus-level-killed 9
556   "*Groups with this level are killed.")
557
558 (defvar gnus-level-default-subscribed 3
559   "*New subscribed groups will be subscribed at this level.")
560
561 (defvar gnus-level-default-unsubscribed 6
562   "*New unsubscribed groups will be unsubscribed at this level.")
563
564 (defvar gnus-activate-level (1+ gnus-level-subscribed)
565   "*Groups higher than this level won't be activated on startup.
566 Setting this variable to something log might save lots of time when
567 you have many groups that you aren't interested in.")
568
569 (defvar gnus-activate-foreign-newsgroups 4
570   "*If nil, Gnus will not check foreign newsgroups at startup.
571 If it is non-nil, it should be a number between one and nine.  Foreign
572 newsgroups that have a level lower or equal to this number will be
573 activated on startup.  For instance, if you want to active all
574 subscribed newsgroups, but not the rest, you'd set this variable to
575 `gnus-level-subscribed'.
576
577 If you subscribe to lots of newsgroups from different servers, startup
578 might take a while.  By setting this variable to nil, you'll save time,
579 but you won't be told how many unread articles there are in the
580 groups.")
581
582 (defvar gnus-save-newsrc-file t
583   "*Non-nil means that Gnus will save the `.newsrc' file.
584 Gnus always saves its own startup file, which is called
585 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
586 be readily understood by other newsreaders.  If you don't plan on
587 using other newsreaders, set this variable to nil to save some time on
588 exit.")
589
590 (defvar gnus-save-killed-list t
591   "*If non-nil, save the list of killed groups to the startup file.
592 If you set this variable to nil, you'll save both time (when starting
593 and quitting) and space (both memory and disk), but it will also mean
594 that Gnus has no record of which groups are new and which are old, so
595 the automatic new newsgroups subscription methods become meaningless.
596
597 You should always set `gnus-check-new-newsgroups' to `ask-server' or
598 nil if you set this variable to nil.")
599
600 (defvar gnus-interactive-catchup t
601   "*If non-nil, require your confirmation when catching up a group.")
602
603 (defvar gnus-interactive-post t
604   "*If non-nil, group name will be asked for when posting.")
605
606 (defvar gnus-interactive-exit t
607   "*If non-nil, require your confirmation when exiting Gnus.")
608
609 (defvar gnus-kill-killed t
610   "*If non-nil, Gnus will apply kill files to already killed articles.
611 If it is nil, Gnus will never apply kill files to articles that have
612 already been through the scoring process, which might very well save lots
613 of time.")
614
615 (defvar gnus-extract-address-components 'gnus-extract-address-components
616   "*Function for extracting address components from a From header.
617 Two pre-defined function exist: `gnus-extract-address-components',
618 which is the default, quite fast, and too simplistic solution, and
619 `mail-extract-address-components', which works much better, but is
620 slower.")
621
622 (defvar gnus-summary-default-score 0
623   "*Default article score level.
624 If this variable is nil, scoring will be disabled.")
625
626 (defvar gnus-summary-zcore-fuzz 0
627   "*Fuzziness factor for the zcore in the summary buffer.
628 Articles with scores closer than this to `gnus-summary-default-score'
629 will not be marked.")
630
631 (defvar gnus-simplify-subject-fuzzy-regexp nil
632   "*Strings to be removed when doing fuzzy matches.
633 This can either be a regular expression or list of regular expressions
634 that will be removed from subject strings if fuzzy subject
635 simplification is selected.")
636
637 (defvar gnus-permanently-visible-groups nil
638   "*Regexp to match groups that should always be listed in the group buffer.
639 This means that they will still be listed when there are no unread
640 articles in the groups.")
641
642 (defvar gnus-list-groups-with-ticked-articles t
643   "*If non-nil, list groups that have only ticked articles.
644 If nil, only list groups that have unread articles.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level.
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-group-list-inactive-groups t
654   "*If non-nil, inactive groups will be listed.")
655
656 (defvar gnus-show-mime nil
657   "*If non-nil, do mime processing of articles.
658 The articles will simply be fed to the function given by
659 `gnus-show-mime-method'.")
660
661 (defvar gnus-strict-mime t
662   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
663
664 (defvar gnus-show-mime-method 'metamail-buffer
665   "*Function to process a MIME message.
666 The function is called from the article buffer.")
667
668 (defvar gnus-decode-encoded-word-method (lambda ())
669   "*Function to decode a MIME encoded-words.
670 The function is called from the article buffer.")
671
672 (defvar gnus-show-threads t
673   "*If non-nil, display threads in summary mode.")
674
675 (defvar gnus-thread-hide-subtree nil
676   "*If non-nil, hide all threads initially.
677 If threads are hidden, you have to run the command
678 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
679 to expose hidden threads.")
680
681 (defvar gnus-thread-hide-killed t
682   "*If non-nil, hide killed threads automatically.")
683
684 (defvar gnus-thread-ignore-subject nil
685   "*If non-nil, ignore subjects and do all threading based on the Reference header.
686 If nil, which is the default, articles that have different subjects
687 from their parents will start separate threads.")
688
689 (defvar gnus-thread-operation-ignore-subject t
690   "*If non-nil, subjects will be ignored when doing thread commands.
691 This affects commands like `gnus-summary-kill-thread' and
692 `gnus-summary-lower-thread'.
693
694 If this variable is nil, articles in the same thread with different
695 subjects will not be included in the operation in question.  If this
696 variable is `fuzzy', only articles that have subjects that are fuzzily
697 equal will be included.")
698
699 (defvar gnus-thread-indent-level 4
700   "*Number that says how much each sub-thread should be indented.")
701
702 (defvar gnus-ignored-newsgroups
703   (purecopy (mapconcat 'identity
704                        '("^to\\."       ; not "real" groups
705                          "^[0-9. \t]+ " ; all digits in name
706                          "[][\"#'()]"   ; bogus characters
707                          )
708                        "\\|"))
709   "*A regexp to match uninteresting newsgroups in the active file.
710 Any lines in the active file matching this regular expression are
711 removed from the newsgroup list before anything else is done to it,
712 thus making them effectively non-existent.")
713
714 (defvar gnus-ignored-headers
715   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
716   "*All headers that match this regexp will be hidden.
717 This variable can also be a list of regexps of headers to be ignored.
718 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
719
720 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
721   "*All headers that do not match this regexp will be hidden.
722 This variable can also be a list of regexp of headers to remain visible.
723 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
724
725 (defvar gnus-sorted-header-list
726   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
727     "^Cc:" "^Date:" "^Organization:")
728   "*This variable is a list of regular expressions.
729 If it is non-nil, headers that match the regular expressions will
730 be placed first in the article buffer in the sequence specified by
731 this list.")
732
733 (defvar gnus-boring-article-headers
734   '(empty followup-to reply-to)
735   "*Headers that are only to be displayed if they have interesting data.
736 Possible values in this list are `empty', `newsgroups', `followup-to',
737 `reply-to', and `date'.")
738
739 (defvar gnus-show-all-headers nil
740   "*If non-nil, don't hide any headers.")
741
742 (defvar gnus-save-all-headers t
743   "*If non-nil, don't remove any headers before saving.")
744
745 (defvar gnus-saved-headers gnus-visible-headers
746   "*Headers to keep if `gnus-save-all-headers' is nil.
747 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
748 If that variable is nil, however, all headers that match this regexp
749 will be kept while the rest will be deleted before saving.")
750
751 (defvar gnus-inhibit-startup-message nil
752   "*If non-nil, the startup message will not be displayed.")
753
754 (defvar gnus-signature-separator "^-- *$"
755   "Regexp matching signature separator.")
756
757 (defvar gnus-signature-limit nil
758   "Provide a limit to what is considered a signature.
759 If it is a number, no signature may not be longer (in characters) than
760 that number.  If it is a function, the function will be called without
761 any parameters, and if it returns nil, there is no signature in the
762 buffer.  If it is a string, it will be used as a regexp.  If it
763 matches, the text in question is not a signature.")
764
765 (defvar gnus-auto-extend-newsgroup t
766   "*If non-nil, extend newsgroup forward and backward when requested.")
767
768 (defvar gnus-auto-select-first t
769   "*If nil, don't select the first unread article when entering a group.
770 If this variable is `best', select the highest-scored unread article
771 in the group.  If neither nil nor `best', select the first unread
772 article.
773
774 If you want to prevent automatic selection of the first unread article
775 in some newsgroups, set the variable to nil in
776 `gnus-select-group-hook'.")
777
778 (defvar gnus-auto-select-next t
779   "*If non-nil, offer to go to the next group from the end of the previous.
780 If the value is t and the next newsgroup is empty, Gnus will exit
781 summary mode and go back to group mode.  If the value is neither nil
782 nor t, Gnus will select the following unread newsgroup.  In
783 particular, if the value is the symbol `quietly', the next unread
784 newsgroup will be selected without any confirmation, and if it is
785 `almost-quietly', the next group will be selected without any
786 confirmation if you are located on the last article in the group.
787 Finally, if this variable is `slightly-quietly', the `Z n' command
788 will go to the next group without confirmation.")
789
790 (defvar gnus-auto-select-same nil
791   "*If non-nil, select the next article with the same subject.")
792
793 (defvar gnus-summary-check-current nil
794   "*If non-nil, consider the current article when moving.
795 The \"unread\" movement commands will stay on the same line if the
796 current article is unread.")
797
798 (defvar gnus-auto-center-summary t
799   "*If non-nil, always center the current summary buffer.
800 In particular, if `vertical' do only vertical recentering.  If non-nil
801 and non-`vertical', do both horizontal and vertical recentering.")
802
803 (defvar gnus-break-pages t
804   "*If non-nil, do page breaking on articles.
805 The page delimiter is specified by the `gnus-page-delimiter'
806 variable.")
807
808 (defvar gnus-page-delimiter "^\^L"
809   "*Regexp describing what to use as article page delimiters.
810 The default value is \"^\^L\", which is a form linefeed at the
811 beginning of a line.")
812
813 (defvar gnus-use-full-window t
814   "*If non-nil, use the entire Emacs screen.")
815
816 (defvar gnus-window-configuration nil
817   "Obsolete variable.  See `gnus-buffer-configuration'.")
818
819 (defvar gnus-window-min-width 2
820   "*Minimum width of Gnus buffers.")
821
822 (defvar gnus-window-min-height 1
823   "*Minimum height of Gnus buffers.")
824
825 (defvar gnus-buffer-configuration
826   '((group
827      (vertical 1.0
828                (group 1.0 point)
829                (if gnus-carpal '(group-carpal 4))))
830     (summary
831      (vertical 1.0
832                (summary 1.0 point)
833                (if gnus-carpal '(summary-carpal 4))))
834     (article
835      (cond 
836       (gnus-use-picons
837        '(frame 1.0
838                (vertical 1.0
839                          (summary 0.25 point)
840                          (if gnus-carpal '(summary-carpal 4))
841                          (article 1.0))
842                (vertical ((height . 5) (width . 15)
843                           (user-position . t)
844                           (left . -1) (top . 1))
845                          (picons 1.0))))
846       (gnus-use-trees
847        '(vertical 1.0
848                   (summary 0.25 point)
849                   (tree 0.25)
850                   (article 1.0)))
851       (t
852        '(vertical 1.0
853                  (summary 0.25 point)
854                  (if gnus-carpal '(summary-carpal 4))
855                  (if gnus-use-trees '(tree 0.25))
856                  (article 1.0)))))
857     (server
858      (vertical 1.0
859                (server 1.0 point)
860                (if gnus-carpal '(server-carpal 2))))
861     (browse
862      (vertical 1.0
863                (browse 1.0 point)
864                (if gnus-carpal '(browse-carpal 2))))
865     (message
866      (vertical 1.0
867                (message 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article-copy 0.5)
896                (message 1.0 point)))
897     (forward
898      (vertical 1.0
899                (message 1.0 point)))
900     (reply-yank
901      (vertical 1.0
902                (message 1.0 point)))
903     (mail-bounce
904      (vertical 1.0
905                (article 0.5)
906                (message 1.0 point)))
907     (draft
908      (vertical 1.0
909                (draft 1.0 point)))
910     (pipe
911      (vertical 1.0
912                (summary 0.25 point)
913                (if gnus-carpal '(summary-carpal 4))
914                ("*Shell Command Output*" 1.0)))
915     (bug
916      (vertical 1.0
917                ("*Gnus Help Bug*" 0.5)
918                ("*Gnus Bug*" 1.0 point)))
919     (compose-bounce
920      (vertical 1.0
921                (article 0.5)
922                (message 1.0 point))))
923   "Window configuration for all possible Gnus buffers.
924 This variable is a list of lists.  Each of these lists has a NAME and
925 a RULE.  The NAMEs are commonsense names like `group', which names a
926 rule used when displaying the group buffer; `summary', which names a
927 rule for what happens when you enter a group and do not display an
928 article buffer; and so on.  See the value of this variable for a
929 complete list of NAMEs.
930
931 Each RULE is a list of vectors.  The first element in this vector is
932 the name of the buffer to be displayed; the second element is the
933 percentage of the screen this buffer is to occupy (a number in the
934 0.0-0.99 range); the optional third element is `point', which should
935 be present to denote which buffer point is to go to after making this
936 buffer configuration.")
937
938 (defvar gnus-window-to-buffer
939   '((group . gnus-group-buffer)
940     (summary . gnus-summary-buffer)
941     (article . gnus-article-buffer)
942     (server . gnus-server-buffer)
943     (browse . "*Gnus Browse Server*")
944     (edit-group . gnus-group-edit-buffer)
945     (edit-server . gnus-server-edit-buffer)
946     (group-carpal . gnus-carpal-group-buffer)
947     (summary-carpal . gnus-carpal-summary-buffer)
948     (server-carpal . gnus-carpal-server-buffer)
949     (browse-carpal . gnus-carpal-browse-buffer)
950     (edit-score . gnus-score-edit-buffer)
951     (message . gnus-message-buffer)
952     (mail . gnus-message-buffer)
953     (post-news . gnus-message-buffer)
954     (faq . gnus-faq-buffer)
955     (picons . "*Picons*")
956     (tree . gnus-tree-buffer)
957     (info . gnus-info-buffer)
958     (article-copy . gnus-article-copy)
959     (draft . gnus-draft-buffer))
960   "Mapping from short symbols to buffer names or buffer variables.")
961
962 (defvar gnus-carpal nil
963   "*If non-nil, display clickable icons.")
964
965 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
966   "*Function called with a group name when new group is detected.
967 A few pre-made functions are supplied: `gnus-subscribe-randomly'
968 inserts new groups at the beginning of the list of groups;
969 `gnus-subscribe-alphabetically' inserts new groups in strict
970 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
971 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
972 for your decision; `gnus-subscribe-killed' kills all new groups.")
973
974 ;; Suggested by a bug report by Hallvard B Furuseth.
975 ;; <h.b.furuseth@usit.uio.no>.
976 (defvar gnus-subscribe-options-newsgroup-method
977   (function gnus-subscribe-alphabetically)
978   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
979 If, for instance, you want to subscribe to all newsgroups in the
980 \"no\" and \"alt\" hierarchies, you'd put the following in your
981 .newsrc file:
982
983 options -n no.all alt.all
984
985 Gnus will the subscribe all new newsgroups in these hierarchies with
986 the subscription method in this variable.")
987
988 (defvar gnus-subscribe-hierarchical-interactive nil
989   "*If non-nil, Gnus will offer to subscribe hierarchically.
990 When a new hierarchy appears, Gnus will ask the user:
991
992 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
993
994 If the user pressed `d', Gnus will descend the hierarchy, `y' will
995 subscribe to all newsgroups in the hierarchy and `s' will skip this
996 hierarchy in its entirety.")
997
998 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
999   "*Function used for sorting the group buffer.
1000 This function will be called with group info entries as the arguments
1001 for the groups to be sorted.  Pre-made functions include
1002 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1003 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1004 `gnus-group-sort-by-rank'.
1005
1006 This variable can also be a list of sorting functions.  In that case,
1007 the most significant sort function should be the last function in the
1008 list.")
1009
1010 ;; Mark variables suggested by Thomas Michanek
1011 ;; <Thomas.Michanek@telelogic.se>.
1012 (defvar gnus-unread-mark ? 
1013   "*Mark used for unread articles.")
1014 (defvar gnus-ticked-mark ?!
1015   "*Mark used for ticked articles.")
1016 (defvar gnus-dormant-mark ??
1017   "*Mark used for dormant articles.")
1018 (defvar gnus-del-mark ?r
1019   "*Mark used for del'd articles.")
1020 (defvar gnus-read-mark ?R
1021   "*Mark used for read articles.")
1022 (defvar gnus-expirable-mark ?E
1023   "*Mark used for expirable articles.")
1024 (defvar gnus-killed-mark ?K
1025   "*Mark used for killed articles.")
1026 (defvar gnus-souped-mark ?F
1027   "*Mark used for killed articles.")
1028 (defvar gnus-kill-file-mark ?X
1029   "*Mark used for articles killed by kill files.")
1030 (defvar gnus-low-score-mark ?Y
1031   "*Mark used for articles with a low score.")
1032 (defvar gnus-catchup-mark ?C
1033   "*Mark used for articles that are caught up.")
1034 (defvar gnus-replied-mark ?A
1035   "*Mark used for articles that have been replied to.")
1036 (defvar gnus-cached-mark ?*
1037   "*Mark used for articles that are in the cache.")
1038 (defvar gnus-saved-mark ?S
1039   "*Mark used for articles that have been saved to.")
1040 (defvar gnus-process-mark ?#
1041   "*Process mark.")
1042 (defvar gnus-ancient-mark ?O
1043   "*Mark used for ancient articles.")
1044 (defvar gnus-sparse-mark ?Q
1045   "*Mark used for sparsely reffed articles.")
1046 (defvar gnus-canceled-mark ?G
1047   "*Mark used for canceled articles.")
1048 (defvar gnus-score-over-mark ?+
1049   "*Score mark used for articles with high scores.")
1050 (defvar gnus-score-below-mark ?-
1051   "*Score mark used for articles with low scores.")
1052 (defvar gnus-empty-thread-mark ? 
1053   "*There is no thread under the article.")
1054 (defvar gnus-not-empty-thread-mark ?=
1055   "*There is a thread under the article.")
1056
1057 (defvar gnus-view-pseudo-asynchronously nil
1058   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1059
1060 (defvar gnus-view-pseudos nil
1061   "*If `automatic', pseudo-articles will be viewed automatically.
1062 If `not-confirm', pseudos will be viewed automatically, and the user
1063 will not be asked to confirm the command.")
1064
1065 (defvar gnus-view-pseudos-separately t
1066   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1067 If nil, all files that use the same viewing command will be given as a
1068 list of parameters to that command.")
1069
1070 (defvar gnus-insert-pseudo-articles t
1071   "*If non-nil, insert pseudo-articles when decoding articles.")
1072
1073 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1074   "*Format of group lines.
1075 It works along the same lines as a normal formatting string,
1076 with some simple extensions.
1077
1078 %M    Only marked articles (character, \"*\" or \" \")
1079 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1080 %L    Level of subscribedness (integer)
1081 %N    Number of unread articles (integer)
1082 %I    Number of dormant articles (integer)
1083 %i    Number of ticked and dormant (integer)
1084 %T    Number of ticked articles (integer)
1085 %R    Number of read articles (integer)
1086 %t    Total number of articles (integer)
1087 %y    Number of unread, unticked articles (integer)
1088 %G    Group name (string)
1089 %g    Qualified group name (string)
1090 %D    Group description (string)
1091 %s    Select method (string)
1092 %o    Moderated group (char, \"m\")
1093 %p    Process mark (char)
1094 %O    Moderated group (string, \"(m)\" or \"\")
1095 %P    Topic indentation (string)
1096 %l    Whether there are GroupLens predictions for this group (string)
1097 %n    Select from where (string)
1098 %z    A string that look like `<%s:%n>' if a foreign select method is used
1099 %u    User defined specifier.  The next character in the format string should
1100       be a letter.  Gnus will call the function gnus-user-format-function-X,
1101       where X is the letter following %u.  The function will be passed the
1102       current header as argument.  The function should return a string, which
1103       will be inserted into the buffer just like information from any other
1104       group specifier.
1105
1106 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1107 the mouse point move inside the area.  There can only be one such area.
1108
1109 Note that this format specification is not always respected.  For
1110 reasons of efficiency, when listing killed groups, this specification
1111 is ignored altogether.  If the spec is changed considerably, your
1112 output may end up looking strange when listing both alive and killed
1113 groups.
1114
1115 If you use %o or %O, reading the active file will be slower and quite
1116 a bit of extra memory will be used. %D will also worsen performance.
1117 Also note that if you change the format specification to include any
1118 of these specs, you must probably re-start Gnus to see them go into
1119 effect.")
1120
1121 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1122   "*The format specification of the lines in the summary buffer.
1123
1124 It works along the same lines as a normal formatting string,
1125 with some simple extensions.
1126
1127 %N   Article number, left padded with spaces (string)
1128 %S   Subject (string)
1129 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1130 %n   Name of the poster (string)
1131 %a   Extracted name of the poster (string)
1132 %A   Extracted address of the poster (string)
1133 %F   Contents of the From: header (string)
1134 %x   Contents of the Xref: header (string)
1135 %D   Date of the article (string)
1136 %d   Date of the article (string) in DD-MMM format
1137 %M   Message-id of the article (string)
1138 %r   References of the article (string)
1139 %c   Number of characters in the article (integer)
1140 %L   Number of lines in the article (integer)
1141 %I   Indentation based on thread level (a string of spaces)
1142 %T   A string with two possible values: 80 spaces if the article
1143      is on thread level two or larger and 0 spaces on level one
1144 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1145 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1146 %[   Opening bracket (character, \"[\" or \"<\")
1147 %]   Closing bracket (character, \"]\" or \">\")
1148 %>   Spaces of length thread-level (string)
1149 %<   Spaces of length (- 20 thread-level) (string)
1150 %i   Article score (number)
1151 %z   Article zcore (character)
1152 %t   Number of articles under the current thread (number).
1153 %e   Whether the thread is empty or not (character).
1154 %l   GroupLens score (string).
1155 %u   User defined specifier.  The next character in the format string should
1156      be a letter.  Gnus will call the function gnus-user-format-function-X,
1157      where X is the letter following %u.  The function will be passed the
1158      current header as argument.  The function should return a string, which
1159      will be inserted into the summary just like information from any other
1160      summary specifier.
1161
1162 Text between %( and %) will be highlighted with `gnus-mouse-face'
1163 when the mouse point is placed inside the area.  There can only be one
1164 such area.
1165
1166 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1167 with care.  For reasons of efficiency, Gnus will compute what column
1168 these characters will end up in, and \"hard-code\" that.  This means that
1169 it is illegal to have these specs after a variable-length spec.  Well,
1170 you might not be arrested, but your summary buffer will look strange,
1171 which is bad enough.
1172
1173 The smart choice is to have these specs as for to the left as
1174 possible.
1175
1176 This restriction may disappear in later versions of Gnus.")
1177
1178 (defvar gnus-summary-dummy-line-format
1179   "*  %(:                          :%) %S\n"
1180   "*The format specification for the dummy roots in the summary buffer.
1181 It works along the same lines as a normal formatting string,
1182 with some simple extensions.
1183
1184 %S  The subject")
1185
1186 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1187   "*The format specification for the summary mode line.
1188 It works along the same lines as a normal formatting string,
1189 with some simple extensions:
1190
1191 %G  Group name
1192 %p  Unprefixed group name
1193 %A  Current article number
1194 %V  Gnus version
1195 %U  Number of unread articles in the group
1196 %e  Number of unselected articles in the group
1197 %Z  A string with unread/unselected article counts
1198 %g  Shortish group name
1199 %S  Subject of the current article
1200 %u  User-defined spec
1201 %s  Current score file name
1202 %d  Number of dormant articles
1203 %r  Number of articles that have been marked as read in this session
1204 %E  Number of articles expunged by the score files")
1205
1206 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1207   "*The format specification for the article mode line.
1208 See `gnus-summary-mode-line-format' for a closer description.")
1209
1210 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1211   "*The format specification for the group mode line.
1212 It works along the same lines as a normal formatting string,
1213 with some simple extensions:
1214
1215 %S   The native news server.
1216 %M   The native select method.")
1217
1218 (defvar gnus-valid-select-methods
1219   '(("nntp" post address prompt-address)
1220     ("nnspool" post address)
1221     ("nnvirtual" post-mail virtual prompt-address)
1222     ("nnmbox" mail respool address)
1223     ("nnml" mail respool address)
1224     ("nnmh" mail respool address)
1225     ("nndir" post-mail prompt-address address)
1226     ("nneething" none address prompt-address)
1227     ("nndoc" none address prompt-address)
1228     ("nnbabyl" mail address respool)
1229     ("nnkiboze" post address virtual)
1230     ("nnsoup" post-mail address)
1231     ("nndraft" post-mail)
1232     ("nnfolder" mail respool address))
1233   "An alist of valid select methods.
1234 The first element of each list lists should be a string with the name
1235 of the select method.  The other elements may be be the category of
1236 this method (ie. `post', `mail', `none' or whatever) or other
1237 properties that this method has (like being respoolable).
1238 If you implement a new select method, all you should have to change is
1239 this variable.  I think.")
1240
1241 (defvar gnus-updated-mode-lines '(group article summary tree)
1242   "*List of buffers that should update their mode lines.
1243 The list may contain the symbols `group', `article' and `summary'.  If
1244 the corresponding symbol is present, Gnus will keep that mode line
1245 updated with information that may be pertinent.
1246 If this variable is nil, screen refresh may be quicker.")
1247
1248 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1249 (defvar gnus-mode-non-string-length nil
1250   "*Max length of mode-line non-string contents.
1251 If this is nil, Gnus will take space as is needed, leaving the rest
1252 of the modeline intact.")
1253
1254 ;see gnus-cus.el
1255 ;(defvar gnus-mouse-face 'highlight
1256 ;  "*Face used for mouse highlighting in Gnus.
1257 ;No mouse highlights will be done if `gnus-visual' is nil.")
1258
1259 (defvar gnus-summary-mark-below nil
1260   "*Mark all articles with a score below this variable as read.
1261 This variable is local to each summary buffer and usually set by the
1262 score file.")
1263
1264 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1265   "*List of functions used for sorting articles in the summary buffer.
1266 This variable is only used when not using a threaded display.")
1267
1268 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1269   "*List of functions used for sorting threads in the summary buffer.
1270 By default, threads are sorted by article number.
1271
1272 Each function takes two threads and return non-nil if the first thread
1273 should be sorted before the other.  If you use more than one function,
1274 the primary sort function should be the last.  You should probably
1275 always include `gnus-thread-sort-by-number' in the list of sorting
1276 functions -- preferably first.
1277
1278 Ready-mady functions include `gnus-thread-sort-by-number',
1279 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1280 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1281 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1282
1283 (defvar gnus-thread-score-function '+
1284   "*Function used for calculating the total score of a thread.
1285
1286 The function is called with the scores of the article and each
1287 subthread and should then return the score of the thread.
1288
1289 Some functions you can use are `+', `max', or `min'.")
1290
1291 (defvar gnus-summary-expunge-below nil
1292   "All articles that have a score less than this variable will be expunged.")
1293
1294 (defvar gnus-thread-expunge-below nil
1295   "All threads that have a total score less than this variable will be expunged.
1296 See `gnus-thread-score-function' for en explanation of what a
1297 \"thread score\" is.")
1298
1299 (defvar gnus-auto-subscribed-groups
1300   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1301   "*All new groups that match this regexp will be subscribed automatically.
1302 Note that this variable only deals with new groups.  It has no effect
1303 whatsoever on old groups.")
1304
1305 (defvar gnus-options-subscribe nil
1306   "*All new groups matching this regexp will be subscribed unconditionally.
1307 Note that this variable deals only with new newsgroups.  This variable
1308 does not affect old newsgroups.")
1309
1310 (defvar gnus-options-not-subscribe nil
1311   "*All new groups matching this regexp will be ignored.
1312 Note that this variable deals only with new newsgroups.  This variable
1313 does not affect old (already subscribed) newsgroups.")
1314
1315 (defvar gnus-auto-expirable-newsgroups nil
1316   "*Groups in which to automatically mark read articles as expirable.
1317 If non-nil, this should be a regexp that should match all groups in
1318 which to perform auto-expiry.  This only makes sense for mail groups.")
1319
1320 (defvar gnus-total-expirable-newsgroups nil
1321   "*Groups in which to perform expiry of all read articles.
1322 Use with extreme caution.  All groups that match this regexp will be
1323 expiring - which means that all read articles will be deleted after
1324 (say) one week.  (This only goes for mail groups and the like, of
1325 course.)")
1326
1327 (defvar gnus-group-uncollapsed-levels 1
1328   "Number of group name elements to leave alone when making a short group name.")
1329
1330 (defvar gnus-hidden-properties '(invisible t intangible t)
1331   "Property list to use for hiding text.")
1332
1333 (defvar gnus-modtime-botch nil
1334   "*Non-nil means .newsrc should be deleted prior to save.  
1335 Its use is due to the bogus appearance that .newsrc was modified on
1336 disc.")
1337
1338 ;; Hooks.
1339
1340 (defvar gnus-group-mode-hook nil
1341   "*A hook for Gnus group mode.")
1342
1343 (defvar gnus-summary-mode-hook nil
1344   "*A hook for Gnus summary mode.
1345 This hook is run before any variables are set in the summary buffer.")
1346
1347 (defvar gnus-article-mode-hook nil
1348   "*A hook for Gnus article mode.")
1349
1350 (defvar gnus-summary-prepare-exit-hook nil
1351   "*A hook called when preparing to exit from the summary buffer.
1352 It calls `gnus-summary-expire-articles' by default.")
1353 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1354
1355 (defvar gnus-summary-exit-hook nil
1356   "*A hook called on exit from the summary buffer.")
1357
1358 (defvar gnus-group-catchup-group-hook nil
1359   "*A hook run when catching up a group from the group buffer.")
1360
1361 (defvar gnus-open-server-hook nil
1362   "*A hook called just before opening connection to the news server.")
1363
1364 (defvar gnus-load-hook nil
1365   "*A hook run while Gnus is loaded.")
1366
1367 (defvar gnus-startup-hook nil
1368   "*A hook called at startup.
1369 This hook is called after Gnus is connected to the NNTP server.")
1370
1371 (defvar gnus-get-new-news-hook nil
1372   "*A hook run just before Gnus checks for new news.")
1373
1374 (defvar gnus-after-getting-new-news-hook nil
1375   "*A hook run after Gnus checks for new news.")
1376
1377 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1378   "*A function that is called to generate the group buffer.
1379 The function is called with three arguments: The first is a number;
1380 all group with a level less or equal to that number should be listed,
1381 if the second is non-nil, empty groups should also be displayed.  If
1382 the third is non-nil, it is a number.  No groups with a level lower
1383 than this number should be displayed.
1384
1385 The only current function implemented is `gnus-group-prepare-flat'.")
1386
1387 (defvar gnus-group-prepare-hook nil
1388   "*A hook called after the group buffer has been generated.
1389 If you want to modify the group buffer, you can use this hook.")
1390
1391 (defvar gnus-summary-prepare-hook nil
1392   "*A hook called after the summary buffer has been generated.
1393 If you want to modify the summary buffer, you can use this hook.")
1394
1395 (defvar gnus-summary-generate-hook nil
1396   "*A hook run just before generating the summary buffer.
1397 This hook is commonly used to customize threading variables and the
1398 like.")
1399
1400 (defvar gnus-article-prepare-hook nil
1401   "*A hook called after an article has been prepared in the article buffer.
1402 If you want to run a special decoding program like nkf, use this hook.")
1403
1404 ;(defvar gnus-article-display-hook nil
1405 ;  "*A hook called after the article is displayed in the article buffer.
1406 ;The hook is designed to change the contents of the article
1407 ;buffer.  Typical functions that this hook may contain are
1408 ;`gnus-article-hide-headers' (hide selected headers),
1409 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1410 ;`gnus-article-hide-signature' (hide signature) and
1411 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1413 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1414 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1415
1416 (defvar gnus-article-x-face-command
1417   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1418   "String or function to be executed to display an X-Face header.
1419 If it is a string, the command will be executed in a sub-shell
1420 asynchronously.  The compressed face will be piped to this command.")
1421
1422 (defvar gnus-article-x-face-too-ugly nil
1423   "Regexp matching posters whose face shouldn't be shown automatically.")
1424
1425 (defvar gnus-select-group-hook nil
1426   "*A hook called when a newsgroup is selected.
1427
1428 If you'd like to simplify subjects like the
1429 `gnus-summary-next-same-subject' command does, you can use the
1430 following hook:
1431
1432  (setq gnus-select-group-hook
1433       (list
1434         (lambda ()
1435           (mapcar (lambda (header)
1436                      (mail-header-set-subject
1437                       header
1438                       (gnus-simplify-subject
1439                        (mail-header-subject header) 're-only)))
1440                   gnus-newsgroup-headers))))")
1441
1442 (defvar gnus-select-article-hook nil
1443   "*A hook called when an article is selected.")
1444
1445 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1446   "*A hook called to apply kill files to a group.
1447 This hook is intended to apply a kill file to the selected newsgroup.
1448 The function `gnus-apply-kill-file' is called by default.
1449
1450 Since a general kill file is too heavy to use only for a few
1451 newsgroups, I recommend you to use a lighter hook function.  For
1452 example, if you'd like to apply a kill file to articles which contains
1453 a string `rmgroup' in subject in newsgroup `control', you can use the
1454 following hook:
1455
1456  (setq gnus-apply-kill-hook
1457       (list
1458         (lambda ()
1459           (cond ((string-match \"control\" gnus-newsgroup-name)
1460                  (gnus-kill \"Subject\" \"rmgroup\")
1461                  (gnus-expunge \"X\"))))))")
1462
1463 (defvar gnus-visual-mark-article-hook
1464   (list 'gnus-highlight-selected-summary)
1465   "*Hook run after selecting an article in the summary buffer.
1466 It is meant to be used for highlighting the article in some way.  It
1467 is not run if `gnus-visual' is nil.")
1468
1469 (defvar gnus-parse-headers-hook nil
1470   "*A hook called before parsing the headers.")
1471 (add-hook 'gnus-parse-headers-hook 'gnus-headers-decode-quoted-printable)
1472
1473 (defvar gnus-exit-group-hook nil
1474   "*A hook called when exiting (not quitting) summary mode.")
1475
1476 (defvar gnus-suspend-gnus-hook nil
1477   "*A hook called when suspending (not exiting) Gnus.")
1478
1479 (defvar gnus-exit-gnus-hook nil
1480   "*A hook called when exiting Gnus.")
1481
1482 (defvar gnus-after-exiting-gnus-hook nil
1483   "*A hook called after exiting Gnus.")
1484
1485 (defvar gnus-save-newsrc-hook nil
1486   "*A hook called before saving any of the newsrc files.")
1487
1488 (defvar gnus-save-quick-newsrc-hook nil
1489   "*A hook called just before saving the quick newsrc file.
1490 Can be used to turn version control on or off.")
1491
1492 (defvar gnus-save-standard-newsrc-hook nil
1493   "*A hook called just before saving the standard newsrc file.
1494 Can be used to turn version control on or off.")
1495
1496 (defvar gnus-summary-update-hook
1497   (list 'gnus-summary-highlight-line)
1498   "*A hook called when a summary line is changed.
1499 The hook will not be called if `gnus-visual' is nil.
1500
1501 The default function `gnus-summary-highlight-line' will
1502 highlight the line according to the `gnus-summary-highlight'
1503 variable.")
1504
1505 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1506   "*A hook called when a group line is changed.
1507 The hook will not be called if `gnus-visual' is nil.
1508
1509 The default function `gnus-group-highlight-line' will
1510 highlight the line according to the `gnus-group-highlight'
1511 variable.")
1512
1513 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1514   "*A hook called when an article is selected for the first time.
1515 The hook is intended to mark an article as read (or unread)
1516 automatically when it is selected.")
1517
1518 (defvar gnus-group-change-level-function nil
1519   "Function run when a group level is changed.
1520 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1521
1522 ;; Remove any hilit infestation.
1523 (add-hook 'gnus-startup-hook
1524           (lambda ()
1525             (remove-hook 'gnus-summary-prepare-hook
1526                          'hilit-rehighlight-buffer-quietly)
1527             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1528             (setq gnus-mark-article-hook
1529                   '(gnus-summary-mark-read-and-unread-as-read))
1530             (remove-hook 'gnus-article-prepare-hook
1531                          'hilit-rehighlight-buffer-quietly)))
1532
1533 \f
1534 ;; Internal variables
1535
1536 ;; Dummy variable.
1537 (defvar gnus-use-generic-from nil)
1538
1539 (defvar gnus-thread-indent-array nil)
1540 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1541
1542 (defvar gnus-newsrc-file-version nil)
1543
1544 (defvar gnus-method-history nil)
1545 ;; Variable holding the user answers to all method prompts.
1546
1547 (defvar gnus-group-history nil)
1548 ;; Variable holding the user answers to all group prompts.
1549
1550 (defvar gnus-server-alist nil
1551   "List of available servers.")
1552
1553 (defvar gnus-group-indentation-function nil)
1554
1555 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1556
1557 (defvar gnus-goto-missing-group-function nil)
1558
1559 (defvar gnus-override-subscribe-method nil)
1560
1561 (defvar gnus-group-goto-next-group-function nil
1562   "Function to override finding the next group after listing groups.")
1563
1564 (defconst gnus-article-mark-lists
1565   '((marked . tick) (replied . reply)
1566     (expirable . expire) (killed . killed)
1567     (bookmarks . bookmark) (dormant . dormant)
1568     (scored . score) (saved . save)
1569     (cached . cache)
1570     ))
1571
1572 ;; Avoid highlighting in kill files.
1573 (defvar gnus-summary-inhibit-highlight nil)
1574 (defvar gnus-newsgroup-selected-overlay nil)
1575
1576 (defvar gnus-inhibit-hiding nil)
1577 (defvar gnus-group-indentation "")
1578 (defvar gnus-inhibit-limiting nil)
1579 (defvar gnus-created-frames nil)
1580
1581 (defvar gnus-article-mode-map nil)
1582 (defvar gnus-dribble-buffer nil)
1583 (defvar gnus-headers-retrieved-by nil)
1584 (defvar gnus-article-reply nil)
1585 (defvar gnus-override-method nil)
1586 (defvar gnus-article-check-size nil)
1587
1588 (defvar gnus-current-score-file nil)
1589 (defvar gnus-newsgroup-adaptive-score-file nil)
1590 (defvar gnus-scores-exclude-files nil)
1591
1592 (defvar gnus-opened-servers nil)
1593
1594 (defvar gnus-current-move-group nil)
1595
1596 (defvar gnus-newsgroup-dependencies nil)
1597 (defvar gnus-newsgroup-async nil)
1598 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1599
1600 (defvar gnus-newsgroup-adaptive nil)
1601
1602 (defvar gnus-summary-display-table nil)
1603 (defvar gnus-summary-display-article-function nil)
1604
1605 (defvar gnus-summary-highlight-line-function nil
1606   "Function called after highlighting a summary line.")
1607
1608 (defvar gnus-group-line-format-alist
1609   `((?M gnus-tmp-marked-mark ?c)
1610     (?S gnus-tmp-subscribed ?c)
1611     (?L gnus-tmp-level ?d)
1612     (?N (cond ((eq number t) "*" )
1613               ((numberp number) 
1614                (int-to-string
1615                 (+ number
1616                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1617                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1618               (t number)) ?s)
1619     (?R gnus-tmp-number-of-read ?s)
1620     (?t gnus-tmp-number-total ?d)
1621     (?y gnus-tmp-number-of-unread ?s)
1622     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1623     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1624     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1625            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1626     (?g gnus-tmp-group ?s)
1627     (?G gnus-tmp-qualified-group ?s)
1628     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1629     (?D gnus-tmp-newsgroup-description ?s)
1630     (?o gnus-tmp-moderated ?c)
1631     (?O gnus-tmp-moderated-string ?s)
1632     (?p gnus-tmp-process-marked ?c)
1633     (?s gnus-tmp-news-server ?s)
1634     (?n gnus-tmp-news-method ?s)
1635     (?P gnus-group-indentation ?s)
1636     (?l gnus-tmp-grouplens ?s)
1637     (?z gnus-tmp-news-method-string ?s)
1638     (?u gnus-tmp-user-defined ?s)))
1639
1640 (defvar gnus-summary-line-format-alist
1641   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1642     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1643     (?s gnus-tmp-subject-or-nil ?s)
1644     (?n gnus-tmp-name ?s)
1645     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1646         ?s)
1647     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1648             gnus-tmp-from) ?s)
1649     (?F gnus-tmp-from ?s)
1650     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1651     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1652     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1653     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1654     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1655     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1656     (?L gnus-tmp-lines ?d)
1657     (?I gnus-tmp-indentation ?s)
1658     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1659     (?R gnus-tmp-replied ?c)
1660     (?\[ gnus-tmp-opening-bracket ?c)
1661     (?\] gnus-tmp-closing-bracket ?c)
1662     (?\> (make-string gnus-tmp-level ? ) ?s)
1663     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1664     (?i gnus-tmp-score ?d)
1665     (?z gnus-tmp-score-char ?c)
1666     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1667     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1668     (?U gnus-tmp-unread ?c)
1669     (?t (gnus-summary-number-of-articles-in-thread
1670          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1671         ?d)
1672     (?e (gnus-summary-number-of-articles-in-thread
1673          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1674         ?c)
1675     (?u gnus-tmp-user-defined ?s))
1676   "An alist of format specifications that can appear in summary lines,
1677 and what variables they correspond with, along with the type of the
1678 variable (string, integer, character, etc).")
1679
1680 (defvar gnus-summary-dummy-line-format-alist
1681   `((?S gnus-tmp-subject ?s)
1682     (?N gnus-tmp-number ?d)
1683     (?u gnus-tmp-user-defined ?s)))
1684
1685 (defvar gnus-summary-mode-line-format-alist
1686   `((?G gnus-tmp-group-name ?s)
1687     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1688     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1689     (?A gnus-tmp-article-number ?d)
1690     (?Z gnus-tmp-unread-and-unselected ?s)
1691     (?V gnus-version ?s)
1692     (?U gnus-tmp-unread ?d)
1693     (?S gnus-tmp-subject ?s)
1694     (?e gnus-tmp-unselected ?d)
1695     (?u gnus-tmp-user-defined ?s)
1696     (?d (length gnus-newsgroup-dormant) ?d)
1697     (?t (length gnus-newsgroup-marked) ?d)
1698     (?r (length gnus-newsgroup-reads) ?d)
1699     (?E gnus-newsgroup-expunged-tally ?d)
1700     (?s (gnus-current-score-file-nondirectory) ?s)))
1701
1702 (defvar gnus-article-mode-line-format-alist
1703   gnus-summary-mode-line-format-alist)
1704
1705 (defvar gnus-group-mode-line-format-alist
1706   `((?S gnus-tmp-news-server ?s)
1707     (?M gnus-tmp-news-method ?s)
1708     (?u gnus-tmp-user-defined ?s)))
1709
1710 (defvar gnus-have-read-active-file nil)
1711
1712 (defconst gnus-maintainer
1713   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1714   "The mail address of the Gnus maintainers.")
1715
1716 (defconst gnus-version "September Gnus v0.80"
1717   "Version number for this version of Gnus.")
1718
1719 (defvar gnus-info-nodes
1720   '((gnus-group-mode "(gnus)The Group Buffer")
1721     (gnus-summary-mode "(gnus)The Summary Buffer")
1722     (gnus-article-mode "(gnus)The Article Buffer"))
1723   "Assoc list of major modes and related Info nodes.")
1724
1725 (defvar gnus-group-buffer "*Group*")
1726 (defvar gnus-summary-buffer "*Summary*")
1727 (defvar gnus-article-buffer "*Article*")
1728 (defvar gnus-server-buffer "*Server*")
1729
1730 (defvar gnus-work-buffer " *gnus work*")
1731
1732 (defvar gnus-original-article-buffer " *Original Article*")
1733 (defvar gnus-original-article nil)
1734
1735 (defvar gnus-buffer-list nil
1736   "Gnus buffers that should be killed on exit.")
1737
1738 (defvar gnus-slave nil
1739   "Whether this Gnus is a slave or not.")
1740
1741 (defvar gnus-variable-list
1742   '(gnus-newsrc-options gnus-newsrc-options-n
1743     gnus-newsrc-last-checked-date
1744     gnus-newsrc-alist gnus-server-alist
1745     gnus-killed-list gnus-zombie-list
1746     gnus-topic-topology gnus-topic-alist
1747     gnus-format-specs)
1748   "Gnus variables saved in the quick startup file.")
1749
1750 (defvar gnus-newsrc-options nil
1751   "Options line in the .newsrc file.")
1752
1753 (defvar gnus-newsrc-options-n nil
1754   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1755
1756 (defvar gnus-newsrc-last-checked-date nil
1757   "Date Gnus last asked server for new newsgroups.")
1758
1759 (defvar gnus-topic-topology nil
1760   "The complete topic hierarchy.")
1761
1762 (defvar gnus-topic-alist nil
1763   "The complete topic-group alist.")
1764
1765 (defvar gnus-newsrc-alist nil
1766   "Assoc list of read articles.
1767 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1768
1769 (defvar gnus-newsrc-hashtb nil
1770   "Hashtable of gnus-newsrc-alist.")
1771
1772 (defvar gnus-killed-list nil
1773   "List of killed newsgroups.")
1774
1775 (defvar gnus-killed-hashtb nil
1776   "Hash table equivalent of gnus-killed-list.")
1777
1778 (defvar gnus-zombie-list nil
1779   "List of almost dead newsgroups.")
1780
1781 (defvar gnus-description-hashtb nil
1782   "Descriptions of newsgroups.")
1783
1784 (defvar gnus-list-of-killed-groups nil
1785   "List of newsgroups that have recently been killed by the user.")
1786
1787 (defvar gnus-active-hashtb nil
1788   "Hashtable of active articles.")
1789
1790 (defvar gnus-moderated-list nil
1791   "List of moderated newsgroups.")
1792
1793 (defvar gnus-group-marked nil)
1794
1795 (defvar gnus-current-startup-file nil
1796   "Startup file for the current host.")
1797
1798 (defvar gnus-last-search-regexp nil
1799   "Default regexp for article search command.")
1800
1801 (defvar gnus-last-shell-command nil
1802   "Default shell command on article.")
1803
1804 (defvar gnus-current-select-method nil
1805   "The current method for selecting a newsgroup.")
1806
1807 (defvar gnus-group-list-mode nil)
1808
1809 (defvar gnus-article-internal-prepare-hook nil)
1810
1811 (defvar gnus-newsgroup-name nil)
1812 (defvar gnus-newsgroup-begin nil)
1813 (defvar gnus-newsgroup-end nil)
1814 (defvar gnus-newsgroup-last-rmail nil)
1815 (defvar gnus-newsgroup-last-mail nil)
1816 (defvar gnus-newsgroup-last-folder nil)
1817 (defvar gnus-newsgroup-last-file nil)
1818 (defvar gnus-newsgroup-auto-expire nil)
1819 (defvar gnus-newsgroup-active nil)
1820
1821 (defvar gnus-newsgroup-data nil)
1822 (defvar gnus-newsgroup-data-reverse nil)
1823 (defvar gnus-newsgroup-limit nil)
1824 (defvar gnus-newsgroup-limits nil)
1825
1826 (defvar gnus-newsgroup-unreads nil
1827   "List of unread articles in the current newsgroup.")
1828
1829 (defvar gnus-newsgroup-unselected nil
1830   "List of unselected unread articles in the current newsgroup.")
1831
1832 (defvar gnus-newsgroup-reads nil
1833   "Alist of read articles and article marks in the current newsgroup.")
1834
1835 (defvar gnus-newsgroup-expunged-tally nil)
1836
1837 (defvar gnus-newsgroup-marked nil
1838   "List of ticked articles in the current newsgroup (a subset of unread art).")
1839
1840 (defvar gnus-newsgroup-killed nil
1841   "List of ranges of articles that have been through the scoring process.")
1842
1843 (defvar gnus-newsgroup-cached nil
1844   "List of articles that come from the article cache.")
1845
1846 (defvar gnus-newsgroup-saved nil
1847   "List of articles that have been saved.")
1848
1849 (defvar gnus-newsgroup-kill-headers nil)
1850
1851 (defvar gnus-newsgroup-replied nil
1852   "List of articles that have been replied to in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-expirable nil
1855   "List of articles in the current newsgroup that can be expired.")
1856
1857 (defvar gnus-newsgroup-processable nil
1858   "List of articles in the current newsgroup that can be processed.")
1859
1860 (defvar gnus-newsgroup-bookmarks nil
1861   "List of articles in the current newsgroup that have bookmarks.")
1862
1863 (defvar gnus-newsgroup-dormant nil
1864   "List of dormant articles in the current newsgroup.")
1865
1866 (defvar gnus-newsgroup-scored nil
1867   "List of scored articles in the current newsgroup.")
1868
1869 (defvar gnus-newsgroup-headers nil
1870   "List of article headers in the current newsgroup.")
1871
1872 (defvar gnus-newsgroup-threads nil)
1873
1874 (defvar gnus-newsgroup-prepared nil
1875   "Whether the current group has been prepared properly.")
1876
1877 (defvar gnus-newsgroup-ancient nil
1878   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1879
1880 (defvar gnus-newsgroup-sparse nil)
1881
1882 (defvar gnus-current-article nil)
1883 (defvar gnus-article-current nil)
1884 (defvar gnus-current-headers nil)
1885 (defvar gnus-have-all-headers nil)
1886 (defvar gnus-last-article nil)
1887 (defvar gnus-newsgroup-history nil)
1888 (defvar gnus-current-kill-article nil)
1889
1890 ;; Save window configuration.
1891 (defvar gnus-prev-winconf nil)
1892
1893 (defvar gnus-summary-mark-positions nil)
1894 (defvar gnus-group-mark-positions nil)
1895
1896 (defvar gnus-reffed-article-number nil)
1897
1898 ;;; Let the byte-compiler know that we know about this variable.
1899 (defvar rmail-default-rmail-file)
1900
1901 (defvar gnus-cache-removable-articles nil)
1902
1903 (defvar gnus-dead-summary nil)
1904
1905 (defconst gnus-summary-local-variables
1906   '(gnus-newsgroup-name
1907     gnus-newsgroup-begin gnus-newsgroup-end
1908     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1909     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1910     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1911     gnus-newsgroup-unselected gnus-newsgroup-marked
1912     gnus-newsgroup-reads gnus-newsgroup-saved
1913     gnus-newsgroup-replied gnus-newsgroup-expirable
1914     gnus-newsgroup-processable gnus-newsgroup-killed
1915     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1916     gnus-newsgroup-headers gnus-newsgroup-threads
1917     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1918     gnus-current-article gnus-current-headers gnus-have-all-headers
1919     gnus-last-article gnus-article-internal-prepare-hook
1920     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1921     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1922     gnus-newsgroup-async gnus-thread-expunge-below
1923     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1924     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1925     gnus-newsgroup-history gnus-newsgroup-ancient
1926     gnus-newsgroup-sparse
1927     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1928     gnus-newsgroup-adaptive-score-file
1929     (gnus-newsgroup-expunged-tally . 0)
1930     gnus-cache-removable-articles gnus-newsgroup-cached
1931     gnus-newsgroup-data gnus-newsgroup-data-reverse
1932     gnus-newsgroup-limit gnus-newsgroup-limits)
1933   "Variables that are buffer-local to the summary buffers.")
1934
1935 (defconst gnus-bug-message
1936   "Sending a bug report to the Gnus Towers.
1937 ========================================
1938
1939 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1940 be sent to the Gnus Bug Exterminators.
1941
1942 At the bottom of the buffer you'll see lots of variable settings.
1943 Please do not delete those.  They will tell the Bug People what your
1944 environment is, so that it will be easier to locate the bugs.
1945
1946 If you have found a bug that makes Emacs go \"beep\", set
1947 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1948 and include the backtrace in your bug report.
1949
1950 Please describe the bug in annoying, painstaking detail.
1951
1952 Thank you for your help in stamping out bugs.
1953 ")
1954
1955 ;;; End of variables.
1956
1957 ;; Define some autoload functions Gnus might use.
1958 (eval-and-compile
1959
1960   ;; This little mapcar goes through the list below and marks the
1961   ;; symbols in question as autoloaded functions.
1962   (mapcar
1963    (lambda (package)
1964      (let ((interactive (nth 1 (memq ':interactive package))))
1965        (mapcar
1966         (lambda (function)
1967           (let (keymap)
1968             (when (consp function)
1969               (setq keymap (car (memq 'keymap function)))
1970               (setq function (car function)))
1971             (autoload function (car package) nil interactive keymap)))
1972         (if (eq (nth 1 package) ':interactive)
1973             (cdddr package)
1974           (cdr package)))))
1975    '(("metamail" metamail-buffer)
1976      ("info" Info-goto-node)
1977      ("hexl" hexl-hex-string-to-integer)
1978      ("pp" pp pp-to-string pp-eval-expression)
1979      ("mail-extr" mail-extract-address-components)
1980      ("nnmail" nnmail-split-fancy nnmail-article-group)
1981      ("nnvirtual" nnvirtual-catchup-group)
1982      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1983       timezone-make-sortable-date timezone-make-time-string)
1984      ("sendmail" mail-position-on-field mail-setup)
1985      ("rmailout" rmail-output)
1986      ("rnewspost" news-mail-other-window news-reply-yank-original
1987       news-caesar-buffer-body)
1988      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1989       rmail-show-message)
1990      ("gnus-soup" :interactive t
1991       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1992       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1993      ("nnsoup" nnsoup-pack-replies)
1994      ("gnus-scomo" :interactive t gnus-score-mode)
1995      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1996       gnus-Folder-save-name gnus-folder-save-name)
1997      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1998      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1999       gnus-server-make-menu-bar gnus-article-make-menu-bar
2000       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2001       gnus-summary-highlight-line gnus-carpal-setup-buffer
2002       gnus-group-highlight-line
2003       gnus-article-add-button gnus-insert-next-page-button
2004       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2005      ("gnus-vis" :interactive t
2006       gnus-article-push-button gnus-article-press-button
2007       gnus-article-highlight gnus-article-highlight-some
2008       gnus-article-highlight-headers gnus-article-highlight-signature
2009       gnus-article-add-buttons gnus-article-add-buttons-to-head
2010       gnus-article-next-button gnus-article-prev-button)
2011      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2012       gnus-demon-add-disconnection gnus-demon-add-handler
2013       gnus-demon-remove-handler)
2014      ("gnus-demon" :interactive t
2015       gnus-demon-init gnus-demon-cancel)
2016      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2017       gnus-tree-open gnus-tree-close)
2018      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2019       gnus-nocem-unwanted-article-p)
2020      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2021      ("gnus-srvr" gnus-browse-foreign-server)
2022      ("gnus-cite" :interactive t
2023       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2024       gnus-article-hide-citation gnus-article-fill-cited-article
2025       gnus-article-hide-citation-in-followups)
2026      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2027       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2028       gnus-execute gnus-expunge)
2029      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2030       gnus-cache-possibly-remove-articles gnus-cache-request-article
2031       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2032       gnus-cache-enter-remove-article gnus-cached-article-p
2033       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2034      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2035       gnus-cache-remove-article)
2036      ("gnus-score" :interactive t
2037       gnus-summary-increase-score gnus-summary-lower-score
2038       gnus-score-flush-cache gnus-score-close
2039       gnus-score-raise-same-subject-and-select
2040       gnus-score-raise-same-subject gnus-score-default
2041       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2042       gnus-score-lower-same-subject gnus-score-lower-thread
2043       gnus-possibly-score-headers)
2044      ("gnus-score"
2045       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2046       gnus-current-score-file-nondirectory gnus-score-adaptive
2047       gnus-score-find-trace gnus-score-file-name)
2048      ("gnus-edit" :interactive t gnus-score-customize)
2049      ("gnus-topic" :interactive t gnus-topic-mode)
2050      ("gnus-topic" gnus-topic-remove-group)
2051      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2052      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2053      ("gnus-uu" :interactive t
2054       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2055       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2056       gnus-uu-mark-by-regexp gnus-uu-mark-all
2057       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2058       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2059       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2060       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2061       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2062       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2063       gnus-uu-decode-binhex-view)
2064      ("gnus-msg" (gnus-summary-send-map keymap)
2065       gnus-mail-yank-original gnus-mail-send-and-exit
2066       gnus-sendmail-setup-mail gnus-article-mail
2067       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2068      ("gnus-msg" :interactive t
2069       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2070       gnus-summary-followup gnus-summary-followup-with-original
2071       gnus-summary-followup-and-reply
2072       gnus-summary-followup-and-reply-with-original
2073       gnus-summary-cancel-article gnus-summary-supersede-article
2074       gnus-post-news gnus-inews-news gnus-cancel-news
2075       gnus-summary-reply gnus-summary-reply-with-original
2076       gnus-summary-mail-forward gnus-summary-mail-other-window
2077       gnus-bug)
2078      ("gnus-picon" :interactive t gnus-article-display-picons
2079       gnus-group-display-picons gnus-picons-article-display-x-face)
2080      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2081       gnus-grouplens-mode)
2082      ("gnus-vm" gnus-vm-mail-setup)
2083      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2084       gnus-summary-save-article-vm gnus-yank-article))))
2085
2086 \f
2087
2088 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2089 ;; If you want the cursor to go somewhere else, set these two
2090 ;; functions in some startup hook to whatever you want.
2091 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2092 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2093
2094 ;;; Various macros and substs.
2095
2096 (defun gnus-header-from (header)
2097   (mail-header-from header))
2098
2099 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2100   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2101   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2102     `(let ((,tempvar (selected-window)))
2103        (unwind-protect
2104            (progn
2105              (pop-to-buffer ,buffer)
2106              ,@forms)
2107          (select-window ,tempvar)))))
2108
2109 (defmacro gnus-gethash (string hashtable)
2110   "Get hash value of STRING in HASHTABLE."
2111   `(symbol-value (intern-soft ,string ,hashtable)))
2112
2113 (defmacro gnus-sethash (string value hashtable)
2114   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2115   `(set (intern ,string ,hashtable) ,value))
2116
2117 (defmacro gnus-intern-safe (string hashtable)
2118   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2119   `(let ((symbol (intern ,string ,hashtable)))
2120      (or (boundp symbol)
2121          (set symbol nil))
2122      symbol))
2123
2124 (defmacro gnus-group-unread (group)
2125   "Get the currently computed number of unread articles in GROUP."
2126   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2127
2128 (defmacro gnus-group-entry (group)
2129   "Get the newsrc entry for GROUP."
2130   `(gnus-gethash ,group gnus-newsrc-hashtb))
2131
2132 (defmacro gnus-active (group)
2133   "Get active info on GROUP."
2134   `(gnus-gethash ,group gnus-active-hashtb))
2135
2136 (defmacro gnus-set-active (group active)
2137   "Set GROUP's active info."
2138   `(gnus-sethash ,group ,active gnus-active-hashtb))
2139
2140 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2141 ;;   function `substring' might cut on a middle of multi-octet
2142 ;;   character.
2143 (defun gnus-truncate-string (str width)
2144   (substring str 0 width))
2145
2146 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2147 ;; to limit the length of a string.  This function is necessary since
2148 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2149 (defsubst gnus-limit-string (str width)
2150   (if (> (length str) width)
2151       (substring str 0 width)
2152     str))
2153
2154 (defsubst gnus-simplify-subject-re (subject)
2155   "Remove \"Re:\" from subject lines."
2156   (if (string-match "^[Rr][Ee]: *" subject)
2157       (substring subject (match-end 0))
2158     subject))
2159
2160 (defsubst gnus-functionp (form)
2161   "Return non-nil if FORM is funcallable."
2162   (or (and (symbolp form) (fboundp form))
2163       (and (listp form) (eq (car form) 'lambda))))
2164
2165 (defsubst gnus-goto-char (point)
2166   (and point (goto-char point)))
2167
2168 (defmacro gnus-buffer-exists-p (buffer)
2169   `(let ((buffer ,buffer))
2170      (and buffer
2171           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2172                    buffer))))
2173
2174 (defmacro gnus-kill-buffer (buffer)
2175   `(let ((buf ,buffer))
2176      (if (gnus-buffer-exists-p buf)
2177          (kill-buffer buf))))
2178
2179 (defsubst gnus-point-at-bol ()
2180   "Return point at the beginning of the line."
2181   (let ((p (point)))
2182     (beginning-of-line)
2183     (prog1
2184         (point)
2185       (goto-char p))))
2186
2187 (defsubst gnus-point-at-eol ()
2188   "Return point at the end of the line."
2189   (let ((p (point)))
2190     (end-of-line)
2191     (prog1
2192         (point)
2193       (goto-char p))))
2194
2195 (defun gnus-alive-p ()
2196   "Say whether Gnus is running or not."
2197   (and gnus-group-buffer
2198        (get-buffer gnus-group-buffer)))
2199
2200 ;; Delete the current line (and the next N lines.);
2201 (defmacro gnus-delete-line (&optional n)
2202   `(delete-region (progn (beginning-of-line) (point))
2203                   (progn (forward-line ,(or n 1)) (point))))
2204
2205 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2206 (defvar gnus-init-inhibit nil)
2207 (defun gnus-read-init-file (&optional inhibit-next)
2208   (if gnus-init-inhibit
2209       (setq gnus-init-inhibit nil)
2210     (setq gnus-init-inhibit inhibit-next)
2211     (and gnus-init-file
2212          (or (and (file-exists-p gnus-init-file)
2213                   ;; Don't try to load a directory.
2214                   (not (file-directory-p gnus-init-file)))
2215              (file-exists-p (concat gnus-init-file ".el"))
2216              (file-exists-p (concat gnus-init-file ".elc")))
2217          (condition-case var
2218              (load gnus-init-file nil t)
2219            (error
2220             (error "Error in %s: %s" gnus-init-file var))))))
2221
2222 ;; Info access macros.
2223
2224 (defmacro gnus-info-group (info)
2225   `(nth 0 ,info))
2226 (defmacro gnus-info-rank (info)
2227   `(nth 1 ,info))
2228 (defmacro gnus-info-read (info)
2229   `(nth 2 ,info))
2230 (defmacro gnus-info-marks (info)
2231   `(nth 3 ,info))
2232 (defmacro gnus-info-method (info)
2233   `(nth 4 ,info))
2234 (defmacro gnus-info-params (info)
2235   `(nth 5 ,info))
2236
2237 (defmacro gnus-info-level (info)
2238   `(let ((rank (gnus-info-rank ,info)))
2239      (if (consp rank)
2240          (car rank)
2241        rank)))
2242 (defmacro gnus-info-score (info)
2243   `(let ((rank (gnus-info-rank ,info)))
2244      (or (and (consp rank) (cdr rank)) 0)))
2245
2246 (defmacro gnus-info-set-group (info group)
2247   `(setcar ,info ,group))
2248 (defmacro gnus-info-set-rank (info rank)
2249   `(setcar (nthcdr 1 ,info) ,rank))
2250 (defmacro gnus-info-set-read (info read)
2251   `(setcar (nthcdr 2 ,info) ,read))
2252 (defmacro gnus-info-set-marks (info marks)
2253   `(setcar (nthcdr 3 ,info) ,marks))
2254 (defmacro gnus-info-set-method (info method)
2255   `(setcar (nthcdr 4 ,info) ,method))
2256 (defmacro gnus-info-set-params (info params)
2257   `(setcar (nthcdr 5 ,info) ,params))
2258
2259 (defmacro gnus-info-set-level (info level)
2260   `(let ((rank (cdr ,info)))
2261      (if (consp (car rank))
2262          (setcar (car rank) ,level)
2263        (setcar rank ,level))))
2264 (defmacro gnus-info-set-score (info score)
2265   `(let ((rank (cdr ,info)))
2266      (if (consp (car rank))
2267          (setcdr (car rank) ,score)
2268        (setcar rank (cons (car rank) ,score)))))
2269
2270 (defmacro gnus-get-info (group)
2271   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2272
2273 (defun gnus-byte-code (func)
2274   "Return a form that can be `eval'ed based on FUNC."
2275   (let ((fval (symbol-function func)))
2276     (if (byte-code-function-p fval)
2277         (let ((flist (append fval nil)))
2278           (setcar flist 'byte-code)
2279           flist)
2280       (cons 'progn (cddr fval)))))
2281
2282 ;;; Load the compatability functions.
2283
2284 (require 'gnus-cus)
2285 (require 'gnus-ems)
2286
2287 \f
2288 ;;;
2289 ;;; Shutdown
2290 ;;;
2291
2292 (defvar gnus-shutdown-alist nil)
2293
2294 (defun gnus-add-shutdown (function &rest symbols)
2295   "Run FUNCTION whenever one of SYMBOLS is shut down."
2296   (push (cons function symbols) gnus-shutdown-alist))
2297
2298 (defun gnus-shutdown (symbol)
2299   "Shut down everything that waits for SYMBOL."
2300   (let ((alist gnus-shutdown-alist)
2301         entry)
2302     (while (setq entry (pop alist))
2303       (when (memq symbol (cdr entry))
2304         (funcall (car entry))))))
2305
2306 \f
2307
2308 ;; Format specs.  The chunks below are the machine-generated forms
2309 ;; that are to be evaled as the result of the default format strings.
2310 ;; We write them in here to get them byte-compiled.  That way the
2311 ;; default actions will be quite fast, while still retaining the full
2312 ;; flexibility of the user-defined format specs.
2313
2314 ;; First we have lots of dummy defvars to let the compiler know these
2315 ;; are really dynamic variables.
2316
2317 (defvar gnus-tmp-unread)
2318 (defvar gnus-tmp-replied)
2319 (defvar gnus-tmp-score-char)
2320 (defvar gnus-tmp-indentation)
2321 (defvar gnus-tmp-opening-bracket)
2322 (defvar gnus-tmp-lines)
2323 (defvar gnus-tmp-name)
2324 (defvar gnus-tmp-closing-bracket)
2325 (defvar gnus-tmp-subject-or-nil)
2326 (defvar gnus-tmp-subject)
2327 (defvar gnus-tmp-marked)
2328 (defvar gnus-tmp-marked-mark)
2329 (defvar gnus-tmp-subscribed)
2330 (defvar gnus-tmp-process-marked)
2331 (defvar gnus-tmp-number-of-unread)
2332 (defvar gnus-tmp-group-name)
2333 (defvar gnus-tmp-group)
2334 (defvar gnus-tmp-article-number)
2335 (defvar gnus-tmp-unread-and-unselected)
2336 (defvar gnus-tmp-news-method)
2337 (defvar gnus-tmp-news-server)
2338 (defvar gnus-tmp-article-number)
2339 (defvar gnus-mouse-face)
2340 (defvar gnus-mouse-face-prop)
2341
2342 (defun gnus-summary-line-format-spec ()
2343   (insert gnus-tmp-unread gnus-tmp-replied
2344           gnus-tmp-score-char gnus-tmp-indentation)
2345   (put-text-property
2346    (point)
2347    (progn
2348      (insert
2349       gnus-tmp-opening-bracket
2350       (format "%4d: %-20s"
2351               gnus-tmp-lines
2352               (if (> (length gnus-tmp-name) 20)
2353                   (substring gnus-tmp-name 0 20)
2354                 gnus-tmp-name))
2355       gnus-tmp-closing-bracket)
2356      (point))
2357    gnus-mouse-face-prop gnus-mouse-face)
2358   (insert " " gnus-tmp-subject-or-nil "\n"))
2359
2360 (defvar gnus-summary-line-format-spec
2361   (gnus-byte-code 'gnus-summary-line-format-spec))
2362
2363 (defun gnus-summary-dummy-line-format-spec ()
2364   (insert "*  ")
2365   (put-text-property
2366    (point)
2367    (progn
2368      (insert ":                          :")
2369      (point))
2370    gnus-mouse-face-prop gnus-mouse-face)
2371   (insert " " gnus-tmp-subject "\n"))
2372
2373 (defvar gnus-summary-dummy-line-format-spec
2374   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2375
2376 (defun gnus-group-line-format-spec ()
2377   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2378           gnus-tmp-process-marked
2379           gnus-group-indentation
2380           (format "%5s: " gnus-tmp-number-of-unread))
2381   (put-text-property
2382    (point)
2383    (progn
2384      (insert gnus-tmp-group "\n")
2385      (1- (point)))
2386    gnus-mouse-face-prop gnus-mouse-face))
2387 (defvar gnus-group-line-format-spec
2388   (gnus-byte-code 'gnus-group-line-format-spec))
2389
2390 (defvar gnus-format-specs
2391   `((version . ,emacs-version)
2392     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2393     (summary-dummy ,gnus-summary-dummy-line-format
2394                    ,gnus-summary-dummy-line-format-spec)
2395     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2396
2397 (defvar gnus-article-mode-line-format-spec nil)
2398 (defvar gnus-summary-mode-line-format-spec nil)
2399 (defvar gnus-group-mode-line-format-spec nil)
2400
2401 ;;; Phew.  All that gruft is over, fortunately.
2402
2403 \f
2404 ;;;
2405 ;;; Gnus Utility Functions
2406 ;;;
2407
2408 (defun gnus-extract-address-components (from)
2409   (let (name address)
2410     ;; First find the address - the thing with the @ in it.  This may
2411     ;; not be accurate in mail addresses, but does the trick most of
2412     ;; the time in news messages.
2413     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2414         (setq address (substring from (match-beginning 0) (match-end 0))))
2415     ;; Then we check whether the "name <address>" format is used.
2416     (and address
2417          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2418          ;; Linear white space is not required.
2419          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2420          (and (setq name (substring from 0 (match-beginning 0)))
2421               ;; Strip any quotes from the name.
2422               (string-match "\".*\"" name)
2423               (setq name (substring name 1 (1- (match-end 0))))))
2424     ;; If not, then "address (name)" is used.
2425     (or name
2426         (and (string-match "(.+)" from)
2427              (setq name (substring from (1+ (match-beginning 0))
2428                                    (1- (match-end 0)))))
2429         (and (string-match "()" from)
2430              (setq name address))
2431         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2432         ;; XOVER might not support folded From headers.
2433         (and (string-match "(.*" from)
2434              (setq name (substring from (1+ (match-beginning 0))
2435                                    (match-end 0)))))
2436     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2437     (list (or name from) (or address from))))
2438
2439 (defun gnus-fetch-field (field)
2440   "Return the value of the header FIELD of current article."
2441   (save-excursion
2442     (save-restriction
2443       (let ((case-fold-search t)
2444             (inhibit-point-motion-hooks t))
2445         (nnheader-narrow-to-headers)
2446         (mail-fetch-field field)))))
2447
2448 (defun gnus-goto-colon ()
2449   (beginning-of-line)
2450   (search-forward ":" (gnus-point-at-eol) t))
2451
2452 ;;;###autoload
2453 (defun gnus-update-format (var)
2454   "Update the format specification near point."
2455   (interactive
2456    (list
2457     (save-excursion
2458       (eval-defun nil)
2459       ;; Find the end of the current word.
2460       (re-search-forward "[ \t\n]" nil t)
2461       ;; Search backward.
2462       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2463         (match-string 1)))))
2464   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2465                               (match-string 1 var))))
2466          (entry (assq type gnus-format-specs))
2467          value spec)
2468     (when entry
2469       (setq gnus-format-specs (delq entry gnus-format-specs)))
2470     (set
2471      (intern (format "%s-spec" var))
2472      (gnus-parse-format (setq value (symbol-value (intern var)))
2473                         (symbol-value (intern (format "%s-alist" var)))
2474                         (not (string-match "mode" var))))
2475     (setq spec (symbol-value (intern (format "%s-spec" var))))
2476     (push (list type value spec) gnus-format-specs)
2477
2478     (pop-to-buffer "*Gnus Format*")
2479     (erase-buffer)
2480     (lisp-interaction-mode)
2481     (insert (pp-to-string spec))))
2482
2483 (defun gnus-update-format-specifications (&optional force)
2484   "Update all (necessary) format specifications."
2485   ;; Make the indentation array.
2486   (gnus-make-thread-indent-array)
2487
2488   ;; See whether all the stored info needs to be flushed.
2489   (when (or force
2490             (not (equal emacs-version
2491                         (cdr (assq 'version gnus-format-specs)))))
2492     (setq gnus-format-specs nil))
2493
2494   ;; Go through all the formats and see whether they need updating.
2495   (let ((types '(summary summary-dummy group
2496                          summary-mode group-mode article-mode))
2497         new-format entry type val)
2498     (while (setq type (pop types))
2499       ;; Jump to the proper buffer to find out the value of
2500       ;; the variable, if possible.  (It may be buffer-local.)
2501       (save-excursion
2502         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2503               val)
2504           (when (and (boundp buffer)
2505                      (setq val (symbol-value buffer))
2506                      (get-buffer val)
2507                      (buffer-name (get-buffer val)))
2508             (set-buffer (get-buffer val)))
2509           (setq new-format (symbol-value
2510                             (intern (format "gnus-%s-line-format" type))))))
2511       (setq entry (cdr (assq type gnus-format-specs)))
2512       (if (and entry
2513                (equal (car entry) new-format))
2514           ;; Use the old format.
2515           (set (intern (format "gnus-%s-line-format-spec" type))
2516                (cadr entry))
2517         ;; This is a new format.
2518         (setq val
2519               (if (not (stringp new-format))
2520                   ;; This is a function call or something.
2521                   new-format
2522                 ;; This is a "real" format.
2523                 (gnus-parse-format
2524                  new-format
2525                  (symbol-value
2526                   (intern (format "gnus-%s-line-format-alist"
2527                                   (if (eq type 'article-mode)
2528                                       'summary-mode type))))
2529                  (not (string-match "mode$" (symbol-name type))))))
2530         ;; Enter the new format spec into the list.
2531         (if entry
2532             (progn
2533               (setcar (cdr entry) val)
2534               (setcar entry new-format))
2535           (push (list type new-format val) gnus-format-specs))
2536         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2537
2538   (unless (assq 'version gnus-format-specs)
2539     (push (cons 'version emacs-version) gnus-format-specs))
2540
2541   (gnus-update-group-mark-positions)
2542   (gnus-update-summary-mark-positions))
2543
2544 (defun gnus-update-summary-mark-positions ()
2545   "Compute where the summary marks are to go."
2546   (save-excursion
2547     (when (and gnus-summary-buffer
2548                (get-buffer gnus-summary-buffer)
2549                (buffer-name (get-buffer gnus-summary-buffer)))
2550       (set-buffer gnus-summary-buffer))
2551     (let ((gnus-replied-mark 129)
2552           (gnus-score-below-mark 130)
2553           (gnus-score-over-mark 130)
2554           (thread nil)
2555           (gnus-visual nil)
2556           (spec gnus-summary-line-format-spec)
2557           pos)
2558       (save-excursion
2559         (gnus-set-work-buffer)
2560         (let ((gnus-summary-line-format-spec spec))
2561           (gnus-summary-insert-line
2562            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2563           (goto-char (point-min))
2564           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2565                                              (- (point) 2)))))
2566           (goto-char (point-min))
2567           (push (cons 'replied (and (search-forward "\201" nil t) 
2568                                     (- (point) 2)))
2569                 pos)
2570           (goto-char (point-min))
2571           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2572                 pos)))
2573       (setq gnus-summary-mark-positions pos))))
2574
2575 (defun gnus-update-group-mark-positions ()
2576   (save-excursion
2577     (let ((gnus-process-mark 128)
2578           (gnus-group-marked '("dummy.group")))
2579       (gnus-set-active "dummy.group" '(0 . 0))
2580       (gnus-set-work-buffer)
2581       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2582       (goto-char (point-min))
2583       (setq gnus-group-mark-positions
2584             (list (cons 'process (and (search-forward "\200" nil t)
2585                                       (- (point) 2))))))))
2586
2587 (defvar gnus-mouse-face-0 'highlight)
2588 (defvar gnus-mouse-face-1 'highlight)
2589 (defvar gnus-mouse-face-2 'highlight)
2590 (defvar gnus-mouse-face-3 'highlight)
2591 (defvar gnus-mouse-face-4 'highlight)
2592
2593 (defun gnus-mouse-face-function (form type)
2594   `(put-text-property
2595     (point) (progn ,@form (point))
2596     gnus-mouse-face-prop
2597     ,(if (equal type 0)
2598          'gnus-mouse-face
2599        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2600
2601 (defvar gnus-face-0 'bold)
2602 (defvar gnus-face-1 'italic)
2603 (defvar gnus-face-2 'bold-italic)
2604 (defvar gnus-face-3 'bold)
2605 (defvar gnus-face-4 'bold)
2606
2607 (defun gnus-face-face-function (form type)
2608   `(put-text-property
2609     (point) (progn ,@form (point))
2610     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2611
2612 (defun gnus-max-width-function (el max-width)
2613   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2614   (if (symbolp el)
2615       `(if (> (length ,el) ,max-width)
2616            (substring ,el 0 ,max-width)
2617          ,el)
2618     `(let ((val (eval ,el)))
2619        (if (numberp val)
2620            (setq val (int-to-string val)))
2621        (if (> (length val) ,max-width)
2622            (substring val 0 ,max-width)
2623          val))))
2624
2625 (defun gnus-parse-format (format spec-alist &optional insert)
2626   ;; This function parses the FORMAT string with the help of the
2627   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2628   ;; string.  If the FORMAT string contains the specifiers %( and %)
2629   ;; the text between them will have the mouse-face text property.
2630   (if (string-match
2631        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2632        format)
2633       (gnus-parse-complex-format format spec-alist)
2634     ;; This is a simple format.
2635     (gnus-parse-simple-format format spec-alist insert)))
2636
2637 (defun gnus-parse-complex-format (format spec-alist)
2638   (save-excursion
2639     (gnus-set-work-buffer)
2640     (insert format)
2641     (goto-char (point-min))
2642     (while (re-search-forward "\"" nil t)
2643       (replace-match "\\\"" nil t))
2644     (goto-char (point-min))
2645     (insert "(\"")
2646     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2647       (let ((number (if (match-beginning 1)
2648                         (match-string 1) "0"))
2649             (delim (aref (match-string 2) 0)))
2650         (if (or (= delim ?\() (= delim ?\{))
2651             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2652                                    " " number " \""))
2653           (replace-match "\")\""))))
2654     (goto-char (point-max))
2655     (insert "\")")
2656     (goto-char (point-min))
2657     (let ((form (read (current-buffer))))
2658       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2659
2660 (defun gnus-complex-form-to-spec (form spec-alist)
2661   (delq nil
2662         (mapcar
2663          (lambda (sform)
2664            (if (stringp sform)
2665                (gnus-parse-simple-format sform spec-alist t)
2666              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2667                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2668                       (nth 1 sform))))
2669          form)))
2670
2671 (defun gnus-parse-simple-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 a
2674   ;; string.
2675   (let ((max-width 0)
2676         spec flist fstring newspec elem beg result dontinsert)
2677     (save-excursion
2678       (gnus-set-work-buffer)
2679       (insert format)
2680       (goto-char (point-min))
2681       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2682                                 nil t)
2683         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2684               (setq newspec "%"
2685                     beg (1+ (match-beginning 0)))
2686           ;; First check if there are any specs that look anything like
2687           ;; "%12,12A", ie. with a "max width specification".  These have
2688           ;; to be treated specially.
2689           (if (setq beg (match-beginning 1))
2690               (setq max-width
2691                     (string-to-int
2692                      (buffer-substring
2693                       (1+ (match-beginning 1)) (match-end 1))))
2694             (setq max-width 0)
2695             (setq beg (match-beginning 2)))
2696           ;; Find the specification from `spec-alist'.
2697           (unless (setq elem (cdr (assq spec spec-alist)))
2698             (setq elem '("*" ?s)))
2699           ;; Treat user defined format specifiers specially.
2700           (when (eq (car elem) 'gnus-tmp-user-defined)
2701             (setq elem
2702                   (list
2703                    (list (intern (concat "gnus-user-format-function-"
2704                                          (match-string 3)))
2705                          'gnus-tmp-header) ?s))
2706             (delete-region (match-beginning 3) (match-end 3)))
2707           (if (not (zerop max-width))
2708               (let ((el (car elem)))
2709                 (cond ((= (cadr elem) ?c)
2710                        (setq el (list 'char-to-string el)))
2711                       ((= (cadr elem) ?d)
2712                        (setq el (list 'int-to-string el))))
2713                 (setq flist (cons (gnus-max-width-function el max-width)
2714                                   flist))
2715                 (setq newspec ?s))
2716             (progn
2717               (setq flist (cons (car elem) flist))
2718               (setq newspec (cadr elem)))))
2719         ;; Remove the old specification (and possibly a ",12" string).
2720         (delete-region beg (match-end 2))
2721         ;; Insert the new specification.
2722         (goto-char beg)
2723         (insert newspec))
2724       (setq fstring (buffer-substring 1 (point-max))))
2725     ;; Do some postprocessing to increase efficiency.
2726     (setq
2727      result
2728      (cond
2729       ;; Emptyness.
2730       ((string= fstring "")
2731        nil)
2732       ;; Not a format string.
2733       ((not (string-match "%" fstring))
2734        (list fstring))
2735       ;; A format string with just a single string spec.
2736       ((string= fstring "%s")
2737        (list (car flist)))
2738       ;; A single character.
2739       ((string= fstring "%c")
2740        (list (car flist)))
2741       ;; A single number.
2742       ((string= fstring "%d")
2743        (setq dontinsert)
2744        (if insert
2745            (list `(princ ,(car flist)))
2746          (list `(int-to-string ,(car flist)))))
2747       ;; Just lots of chars and strings.
2748       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2749        (nreverse flist))
2750       ;; A single string spec at the beginning of the spec.
2751       ((string-match "\\`%[sc][^%]+\\'" fstring)
2752        (list (car flist) (substring fstring 2)))
2753       ;; A single string spec in the middle of the spec.
2754       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2755        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2756       ;; A single string spec in the end of the spec.
2757       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2758        (list (match-string 1 fstring) (car flist)))
2759       ;; A more complex spec.
2760       (t
2761        (list (cons 'format (cons fstring (nreverse flist)))))))
2762
2763     (if insert
2764         (when result
2765           (if dontinsert
2766               result
2767             (cons 'insert result)))
2768       (cond ((stringp result)
2769              result)
2770             ((consp result)
2771              (cons 'concat result))
2772             (t "")))))
2773
2774 (defun gnus-eval-format (format &optional alist props)
2775   "Eval the format variable FORMAT, using ALIST.
2776 If PROPS, insert the result."
2777   (let ((form (gnus-parse-format format alist props)))
2778     (if props
2779         (add-text-properties (point) (progn (eval form) (point)) props)
2780       (eval form))))
2781
2782 (defun gnus-remove-text-with-property (prop)
2783   "Delete all text in the current buffer with text property PROP."
2784   (save-excursion
2785     (goto-char (point-min))
2786     (while (not (eobp))
2787       (while (get-text-property (point) prop)
2788         (delete-char 1))
2789       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2790
2791 (defun gnus-set-work-buffer ()
2792   (if (get-buffer gnus-work-buffer)
2793       (progn
2794         (set-buffer gnus-work-buffer)
2795         (erase-buffer))
2796     (set-buffer (get-buffer-create gnus-work-buffer))
2797     (kill-all-local-variables)
2798     (buffer-disable-undo (current-buffer))
2799     (gnus-add-current-to-buffer-list)))
2800
2801 ;; Article file names when saving.
2802
2803 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2804   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2805 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2806 Otherwise, it is like ~/News/news/group/num."
2807   (let ((default
2808           (expand-file-name
2809            (concat (if (gnus-use-long-file-name 'not-save)
2810                        (gnus-capitalize-newsgroup newsgroup)
2811                      (gnus-newsgroup-directory-form newsgroup))
2812                    "/" (int-to-string (mail-header-number headers)))
2813            (or gnus-article-save-directory "~/News"))))
2814     (if (and last-file
2815              (string-equal (file-name-directory default)
2816                            (file-name-directory last-file))
2817              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2818         default
2819       (or last-file default))))
2820
2821 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2822   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2823 If variable `gnus-use-long-file-name' is non-nil, it is
2824 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2825   (let ((default
2826           (expand-file-name
2827            (concat (if (gnus-use-long-file-name 'not-save)
2828                        newsgroup
2829                      (gnus-newsgroup-directory-form newsgroup))
2830                    "/" (int-to-string (mail-header-number headers)))
2831            (or gnus-article-save-directory "~/News"))))
2832     (if (and last-file
2833              (string-equal (file-name-directory default)
2834                            (file-name-directory last-file))
2835              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2836         default
2837       (or last-file default))))
2838
2839 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2840   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2841 If variable `gnus-use-long-file-name' is non-nil, it is
2842 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2843   (or last-file
2844       (expand-file-name
2845        (if (gnus-use-long-file-name 'not-save)
2846            (gnus-capitalize-newsgroup newsgroup)
2847          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2848        (or gnus-article-save-directory "~/News"))))
2849
2850 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2851   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2852 If variable `gnus-use-long-file-name' is non-nil, it is
2853 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2854   (or last-file
2855       (expand-file-name
2856        (if (gnus-use-long-file-name 'not-save)
2857            newsgroup
2858          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2859        (or gnus-article-save-directory "~/News"))))
2860
2861 ;; For subscribing new newsgroup
2862
2863 (defun gnus-subscribe-hierarchical-interactive (groups)
2864   (let ((groups (sort groups 'string<))
2865         prefixes prefix start ans group starts)
2866     (while groups
2867       (setq prefixes (list "^"))
2868       (while (and groups prefixes)
2869         (while (not (string-match (car prefixes) (car groups)))
2870           (setq prefixes (cdr prefixes)))
2871         (setq prefix (car prefixes))
2872         (setq start (1- (length prefix)))
2873         (if (and (string-match "[^\\.]\\." (car groups) start)
2874                  (cdr groups)
2875                  (setq prefix
2876                        (concat "^" (substring (car groups) 0 (match-end 0))))
2877                  (string-match prefix (cadr groups)))
2878             (progn
2879               (setq prefixes (cons prefix prefixes))
2880               (message "Descend hierarchy %s? ([y]nsq): "
2881                        (substring prefix 1 (1- (length prefix))))
2882               (setq ans (read-char))
2883               (cond ((= ans ?n)
2884                      (while (and groups
2885                                  (string-match prefix
2886                                                (setq group (car groups))))
2887                        (setq gnus-killed-list
2888                              (cons group gnus-killed-list))
2889                        (gnus-sethash group group gnus-killed-hashtb)
2890                        (setq groups (cdr groups)))
2891                      (setq starts (cdr starts)))
2892                     ((= ans ?s)
2893                      (while (and groups
2894                                  (string-match prefix
2895                                                (setq group (car groups))))
2896                        (gnus-sethash group group gnus-killed-hashtb)
2897                        (gnus-subscribe-alphabetically (car groups))
2898                        (setq groups (cdr groups)))
2899                      (setq starts (cdr starts)))
2900                     ((= ans ?q)
2901                      (while groups
2902                        (setq group (car groups))
2903                        (setq gnus-killed-list (cons group gnus-killed-list))
2904                        (gnus-sethash group group gnus-killed-hashtb)
2905                        (setq groups (cdr groups))))
2906                     (t nil)))
2907           (message "Subscribe %s? ([n]yq)" (car groups))
2908           (setq ans (read-char))
2909           (setq group (car groups))
2910           (cond ((= ans ?y)
2911                  (gnus-subscribe-alphabetically (car groups))
2912                  (gnus-sethash group group gnus-killed-hashtb))
2913                 ((= ans ?q)
2914                  (while groups
2915                    (setq group (car groups))
2916                    (setq gnus-killed-list (cons group gnus-killed-list))
2917                    (gnus-sethash group group gnus-killed-hashtb)
2918                    (setq groups (cdr groups))))
2919                 (t
2920                  (setq gnus-killed-list (cons group gnus-killed-list))
2921                  (gnus-sethash group group gnus-killed-hashtb)))
2922           (setq groups (cdr groups)))))))
2923
2924 (defun gnus-subscribe-randomly (newsgroup)
2925   "Subscribe new NEWSGROUP by making it the first newsgroup."
2926   (gnus-subscribe-newsgroup newsgroup))
2927
2928 (defun gnus-subscribe-alphabetically (newgroup)
2929   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2930   (let ((groups (cdr gnus-newsrc-alist))
2931         before)
2932     (while (and (not before) groups)
2933       (if (string< newgroup (caar groups))
2934           (setq before (caar groups))
2935         (setq groups (cdr groups))))
2936     (gnus-subscribe-newsgroup newgroup before)))
2937
2938 (defun gnus-subscribe-hierarchically (newgroup)
2939   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2940   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2941   (save-excursion
2942     (set-buffer (find-file-noselect gnus-current-startup-file))
2943     (let ((groupkey newgroup)
2944           before)
2945       (while (and (not before) groupkey)
2946         (goto-char (point-min))
2947         (let ((groupkey-re
2948                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2949           (while (and (re-search-forward groupkey-re nil t)
2950                       (progn
2951                         (setq before (match-string 1))
2952                         (string< before newgroup)))))
2953         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2954         (setq groupkey
2955               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2956                   (substring groupkey (match-beginning 1) (match-end 1)))))
2957       (gnus-subscribe-newsgroup newgroup before))))
2958
2959 (defun gnus-subscribe-interactively (group)
2960   "Subscribe the new GROUP interactively.
2961 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2962 it is killed."
2963   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2964       (gnus-subscribe-hierarchically group)
2965     (push group gnus-killed-list)))
2966
2967 (defun gnus-subscribe-zombies (group)
2968   "Make the new GROUP into a zombie group."
2969   (push group gnus-zombie-list))
2970
2971 (defun gnus-subscribe-killed (group)
2972   "Make the new GROUP a killed group."
2973   (push group gnus-killed-list))
2974
2975 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2976   "Subscribe new NEWSGROUP.
2977 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2978 the first newsgroup."
2979   ;; We subscribe the group by changing its level to `subscribed'.
2980   (gnus-group-change-level
2981    newsgroup gnus-level-default-subscribed
2982    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2983   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2984
2985 ;; For directories
2986
2987 (defun gnus-newsgroup-directory-form (newsgroup)
2988   "Make hierarchical directory name from NEWSGROUP name."
2989   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2990         (len (length newsgroup))
2991         idx)
2992     ;; If this is a foreign group, we don't want to translate the
2993     ;; entire name.
2994     (if (setq idx (string-match ":" newsgroup))
2995         (aset newsgroup idx ?/)
2996       (setq idx 0))
2997     ;; Replace all occurrences of `.' with `/'.
2998     (while (< idx len)
2999       (if (= (aref newsgroup idx) ?.)
3000           (aset newsgroup idx ?/))
3001       (setq idx (1+ idx)))
3002     newsgroup))
3003
3004 (defun gnus-newsgroup-savable-name (group)
3005   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3006   ;; with dots.
3007   (nnheader-replace-chars-in-string group ?/ ?.))
3008
3009 (defun gnus-make-directory (dir)
3010   "Make DIRECTORY recursively."
3011   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3012   ;; of the many mysteries of the universe.
3013   (let* ((dir (expand-file-name dir default-directory))
3014          dirs err)
3015     (if (string-match "/$" dir)
3016         (setq dir (substring dir 0 (match-beginning 0))))
3017     ;; First go down the path until we find a directory that exists.
3018     (while (not (file-exists-p dir))
3019       (setq dirs (cons dir dirs))
3020       (string-match "/[^/]+$" dir)
3021       (setq dir (substring dir 0 (match-beginning 0))))
3022     ;; Then create all the subdirs.
3023     (while (and dirs (not err))
3024       (condition-case ()
3025           (make-directory (car dirs))
3026         (error (setq err t)))
3027       (setq dirs (cdr dirs)))
3028     ;; We return whether we were successful or not.
3029     (not dirs)))
3030
3031 (defun gnus-capitalize-newsgroup (newsgroup)
3032   "Capitalize NEWSGROUP name."
3033   (and (not (zerop (length newsgroup)))
3034        (concat (char-to-string (upcase (aref newsgroup 0)))
3035                (substring newsgroup 1))))
3036
3037 ;; Various... things.
3038
3039 (defun gnus-simplify-subject (subject &optional re-only)
3040   "Remove `Re:' and words in parentheses.
3041 If RE-ONLY is non-nil, strip leading `Re:'s only."
3042   (let ((case-fold-search t))           ;Ignore case.
3043     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3044     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3045       (setq subject (substring subject (match-end 0))))
3046     ;; Remove uninteresting prefixes.
3047     (if (and (not re-only)
3048              gnus-simplify-ignored-prefixes
3049              (string-match gnus-simplify-ignored-prefixes subject))
3050         (setq subject (substring subject (match-end 0))))
3051     ;; Remove words in parentheses from end.
3052     (unless re-only
3053       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3054         (setq subject (substring subject 0 (match-beginning 0)))))
3055     ;; Return subject string.
3056     subject))
3057
3058 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3059 ;; all whitespace.
3060 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3061 (defun gnus-simplify-buffer-fuzzy ()
3062   (goto-char (point-min))
3063   (while (search-forward "\t" nil t)
3064     (replace-match " " t t))
3065   (goto-char (point-min))
3066   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3067   (goto-char (match-beginning 0))
3068   (while (or
3069           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3070           (looking-at "^[[].*: .*[]]$"))
3071     (goto-char (point-min))
3072     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3073                               nil t)
3074       (replace-match "" t t))
3075     (goto-char (point-min))
3076     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3077       (goto-char (match-end 0))
3078       (delete-char -1)
3079       (delete-region
3080        (progn (goto-char (match-beginning 0)))
3081        (re-search-forward ":"))))
3082   (goto-char (point-min))
3083   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3084     (replace-match "" t t))
3085   (goto-char (point-min))
3086   (while (re-search-forward "  +" nil t)
3087     (replace-match " " t t))
3088   (goto-char (point-min))
3089   (while (re-search-forward " $" nil t)
3090     (replace-match "" t t))
3091   (goto-char (point-min))
3092   (while (re-search-forward "^ +" nil t)
3093     (replace-match "" t t))
3094   (goto-char (point-min))
3095   (when gnus-simplify-subject-fuzzy-regexp
3096     (if (listp gnus-simplify-subject-fuzzy-regexp)
3097         (let ((list gnus-simplify-subject-fuzzy-regexp))
3098           (while list
3099             (goto-char (point-min))
3100             (while (re-search-forward (car list) nil t)
3101               (replace-match "" t t))
3102             (setq list (cdr list))))
3103       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3104         (replace-match "" t t)))))
3105
3106 (defun gnus-simplify-subject-fuzzy (subject)
3107   "Siplify a subject string fuzzily."
3108   (save-excursion
3109     (gnus-set-work-buffer)
3110     (let ((case-fold-search t))
3111       (insert subject)
3112       (inline (gnus-simplify-buffer-fuzzy))
3113       (buffer-string))))
3114
3115 ;; Add the current buffer to the list of buffers to be killed on exit.
3116 (defun gnus-add-current-to-buffer-list ()
3117   (or (memq (current-buffer) gnus-buffer-list)
3118       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3119
3120 (defun gnus-string> (s1 s2)
3121   (not (or (string< s1 s2)
3122            (string= s1 s2))))
3123
3124 ;;; General various misc type functions.
3125
3126 (defun gnus-clear-system ()
3127   "Clear all variables and buffers."
3128   ;; Clear Gnus variables.
3129   (let ((variables gnus-variable-list))
3130     (while variables
3131       (set (car variables) nil)
3132       (setq variables (cdr variables))))
3133   ;; Clear other internal variables.
3134   (setq gnus-list-of-killed-groups nil
3135         gnus-have-read-active-file nil
3136         gnus-newsrc-alist nil
3137         gnus-newsrc-hashtb nil
3138         gnus-killed-list nil
3139         gnus-zombie-list nil
3140         gnus-killed-hashtb nil
3141         gnus-active-hashtb nil
3142         gnus-moderated-list nil
3143         gnus-description-hashtb nil
3144         gnus-current-headers nil
3145         gnus-thread-indent-array nil
3146         gnus-newsgroup-headers nil
3147         gnus-newsgroup-name nil
3148         gnus-server-alist nil
3149         gnus-group-list-mode nil
3150         gnus-opened-servers nil
3151         gnus-current-select-method nil)
3152   (gnus-shutdown 'gnus)
3153   ;; Kill the startup file.
3154   (and gnus-current-startup-file
3155        (get-file-buffer gnus-current-startup-file)
3156        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3157   ;; Clear the dribble buffer.
3158   (gnus-dribble-clear)
3159   ;; Kill global KILL file buffer.
3160   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3161     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3162   (gnus-kill-buffer nntp-server-buffer)
3163   ;; Kill Gnus buffers.
3164   (while gnus-buffer-list
3165     (gnus-kill-buffer (pop gnus-buffer-list)))
3166   ;; Remove Gnus frames.
3167   (while gnus-created-frames
3168     (when (frame-live-p (car gnus-created-frames))
3169       ;; We slap a condition-case around this `delete-frame' to ensure 
3170       ;; agains errors if we try do delete the single frame that's left.
3171       (condition-case ()
3172           (delete-frame (car gnus-created-frames))
3173         (error nil)))
3174     (pop gnus-created-frames)))
3175
3176 (defun gnus-windows-old-to-new (setting)
3177   ;; First we take care of the really, really old Gnus 3 actions.
3178   (when (symbolp setting)
3179     (setq setting
3180           ;; Take care of ooold GNUS 3.x values.
3181           (cond ((eq setting 'SelectArticle) 'article)
3182                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3183                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3184                 (t setting))))
3185   (if (or (listp setting)
3186           (not (and gnus-window-configuration
3187                     (memq setting '(group summary article)))))
3188       setting
3189     (let* ((setting (if (eq setting 'group)
3190                         (if (assq 'newsgroup gnus-window-configuration)
3191                             'newsgroup
3192                           'newsgroups) setting))
3193            (elem (cadr (assq setting gnus-window-configuration)))
3194            (total (apply '+ elem))
3195            (types '(group summary article))
3196            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3197            (i 0)
3198            perc
3199            out)
3200       (while (< i 3)
3201         (or (not (numberp (nth i elem)))
3202             (zerop (nth i elem))
3203             (progn
3204               (setq perc  (/ (float (nth 0 elem)) total))
3205               (setq out (cons (if (eq pbuf (nth i types))
3206                                   (vector (nth i types) perc 'point)
3207                                 (vector (nth i types) perc))
3208                               out))))
3209         (setq i (1+ i)))
3210       (list (nreverse out)))))
3211
3212 ;;;###autoload
3213 (defun gnus-add-configuration (conf)
3214   "Add the window configuration CONF to `gnus-buffer-configuration'."
3215   (setq gnus-buffer-configuration
3216         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3217                          gnus-buffer-configuration))))
3218
3219 (defvar gnus-frame-list nil)
3220
3221 (defun gnus-configure-frame (split &optional window)
3222   "Split WINDOW according to SPLIT."
3223   (unless window
3224     (setq window (get-buffer-window (current-buffer))))
3225   (select-window window)
3226   ;; This might be an old-stylee buffer config.
3227   (when (vectorp split)
3228     (setq split (append split nil)))
3229   (when (or (consp (car split))
3230             (vectorp (car split)))
3231     (push 1.0 split)
3232     (push 'vertical split))
3233   ;; The SPLIT might be something that is to be evaled to
3234   ;; return a new SPLIT.
3235   (while (and (not (assq (car split) gnus-window-to-buffer))
3236               (gnus-functionp (car split)))
3237     (setq split (eval split)))
3238   (let* ((type (car split))
3239          (subs (cddr split))
3240          (len (if (eq type 'horizontal) (window-width) (window-height)))
3241          (total 0)
3242          (window-min-width (or gnus-window-min-width window-min-width))
3243          (window-min-height (or gnus-window-min-height window-min-height))
3244          s result new-win rest comp-subs size sub)
3245     (cond
3246      ;; Nothing to do here.
3247      ((null split))
3248      ;; Don't switch buffers.
3249      ((null type)
3250       (and (memq 'point split) window))
3251      ;; This is a buffer to be selected.
3252      ((not (memq type '(frame horizontal vertical)))
3253       (let ((buffer (cond ((stringp type) type)
3254                           (t (cdr (assq type gnus-window-to-buffer)))))
3255             buf)
3256         (unless buffer
3257           (error "Illegal buffer type: %s" type))
3258         (unless (setq buf (get-buffer (if (symbolp buffer)
3259                                           (symbol-value buffer) buffer)))
3260           (setq buf (get-buffer-create (if (symbolp buffer)
3261                                            (symbol-value buffer) buffer))))
3262         (switch-to-buffer buf)
3263         ;; We return the window if it has the `point' spec.
3264         (and (memq 'point split) window)))
3265      ;; This is a frame split.
3266      ((eq type 'frame)
3267       (unless gnus-frame-list
3268         (setq gnus-frame-list (list (window-frame
3269                                      (get-buffer-window (current-buffer))))))
3270       (let ((i 0)
3271             params frame fresult)
3272         (while (< i (length subs))
3273           ;; Frame parameter is gotten from the sub-split.
3274           (setq params (cadr (elt subs i)))
3275           ;; It should be a list.
3276           (unless (listp params)
3277             (setq params nil))
3278           ;; Create a new frame?
3279           (unless (setq frame (elt gnus-frame-list i))
3280             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3281             (push frame gnus-created-frames))
3282           ;; Is the old frame still alive?
3283           (unless (frame-live-p frame)
3284             (setcar (nthcdr i gnus-frame-list)
3285                     (setq frame (make-frame params))))
3286           ;; Select the frame in question and do more splits there.
3287           (select-frame frame)
3288           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3289           (incf i))
3290         ;; Select the frame that has the selected buffer.
3291         (when fresult
3292           (select-frame (window-frame fresult)))))
3293      ;; This is a normal split.
3294      (t
3295       (when (> (length subs) 0)
3296         ;; First we have to compute the sizes of all new windows.
3297         (while subs
3298           (setq sub (append (pop subs) nil))
3299           (while (and (not (assq (car sub) gnus-window-to-buffer))
3300                       (gnus-functionp (car sub)))
3301             (setq sub (eval sub)))
3302           (when sub
3303             (push sub comp-subs)
3304             (setq size (cadar comp-subs))
3305             (cond ((equal size 1.0)
3306                    (setq rest (car comp-subs))
3307                    (setq s 0))
3308                   ((floatp size)
3309                    (setq s (floor (* size len))))
3310                   ((integerp size)
3311                    (setq s size))
3312                   (t
3313                    (error "Illegal size: %s" size)))
3314             ;; Try to make sure that we are inside the safe limits.
3315             (cond ((zerop s))
3316                   ((eq type 'horizontal)
3317                    (setq s (max s window-min-width)))
3318                   ((eq type 'vertical)
3319                    (setq s (max s window-min-height))))
3320             (setcar (cdar comp-subs) s)
3321             (incf total s)))
3322         ;; Take care of the "1.0" spec.
3323         (if rest
3324             (setcar (cdr rest) (- len total))
3325           (error "No 1.0 specs in %s" split))
3326         ;; The we do the actual splitting in a nice recursive
3327         ;; fashion.
3328         (setq comp-subs (nreverse comp-subs))
3329         (while comp-subs
3330           (if (null (cdr comp-subs))
3331               (setq new-win window)
3332             (setq new-win
3333                   (split-window window (cadar comp-subs)
3334                                 (eq type 'horizontal))))
3335           (setq result (or (gnus-configure-frame
3336                             (car comp-subs) window) result))
3337           (select-window new-win)
3338           (setq window new-win)
3339           (setq comp-subs (cdr comp-subs))))
3340       ;; Return the proper window, if any.
3341       (when result
3342         (select-window result))))))
3343
3344 (defvar gnus-frame-split-p nil)
3345
3346 (defun gnus-configure-windows (setting &optional force)
3347   (setq setting (gnus-windows-old-to-new setting))
3348   (let ((split (if (symbolp setting)
3349                    (cadr (assq setting gnus-buffer-configuration))
3350                  setting))
3351         all-visible)
3352
3353     (setq gnus-frame-split-p nil)
3354
3355     (unless split
3356       (error "No such setting: %s" setting))
3357
3358     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3359              (not force))
3360         ;; All the windows mentioned are already visible, so we just
3361         ;; put point in the assigned buffer, and do not touch the
3362         ;; winconf.
3363         (select-window all-visible)
3364
3365       ;; Either remove all windows or just remove all Gnus windows.
3366       (let ((frame (selected-frame)))
3367         (unwind-protect
3368             (if gnus-use-full-window
3369                 ;; We want to remove all other windows.
3370                 (if (not gnus-frame-split-p)
3371                     ;; This is not a `frame' split, so we ignore the
3372                     ;; other frames.  
3373                     (delete-other-windows)
3374                   ;; This is a `frame' split, so we delete all windows
3375                   ;; on all frames.
3376                   (mapcar 
3377                    (lambda (frame)
3378                      (unless (eq (cdr (assq 'minibuffer
3379                                             (frame-parameters frame)))
3380                                  'only)
3381                        (select-frame frame)
3382                        (delete-other-windows)))
3383                    (frame-list)))
3384               ;; Just remove some windows.
3385               (gnus-remove-some-windows)
3386               (switch-to-buffer nntp-server-buffer))
3387           (select-frame frame)))
3388
3389       (switch-to-buffer nntp-server-buffer)
3390       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3391
3392 (defun gnus-all-windows-visible-p (split)
3393   "Say whether all buffers in SPLIT are currently visible.
3394 In particular, the value returned will be the window that
3395 should have point."
3396   (let ((stack (list split))
3397         (all-visible t)
3398         type buffer win buf)
3399     (while (and (setq split (pop stack))
3400                 all-visible)
3401       ;; Be backwards compatible.
3402       (when (vectorp split)
3403         (setq split (append split nil)))
3404       (when (or (consp (car split))
3405                 (vectorp (car split)))
3406         (push 1.0 split)
3407         (push 'vertical split))
3408       ;; The SPLIT might be something that is to be evaled to
3409       ;; return a new SPLIT.
3410       (while (and (not (assq (car split) gnus-window-to-buffer))
3411                   (gnus-functionp (car split)))
3412         (setq split (eval split)))
3413
3414       (setq type (elt split 0))
3415       (cond
3416        ;; Nothing here.
3417        ((null split) t)
3418        ;; A buffer.
3419        ((not (memq type '(horizontal vertical frame)))
3420         (setq buffer (cond ((stringp type) type)
3421                            (t (cdr (assq type gnus-window-to-buffer)))))
3422         (unless buffer
3423           (error "Illegal buffer type: %s" type))
3424         (when (setq buf (get-buffer (if (symbolp buffer)
3425                                         (symbol-value buffer)
3426                                       buffer)))
3427           (setq win (get-buffer-window buf t)))
3428         (if win
3429             (when (memq 'point split)
3430                 (setq all-visible win))
3431           (setq all-visible nil)))
3432        (t
3433         (when (eq type 'frame)
3434           (setq gnus-frame-split-p t))
3435         (setq stack (append (cddr split) stack)))))
3436     (unless (eq all-visible t)
3437       all-visible)))
3438
3439 (defun gnus-window-top-edge (&optional window)
3440   (nth 1 (window-edges window)))
3441
3442 (defun gnus-remove-some-windows ()
3443   (let ((buffers gnus-window-to-buffer)
3444         buf bufs lowest-buf lowest)
3445     (save-excursion
3446       ;; Remove windows on all known Gnus buffers.
3447       (while buffers
3448         (setq buf (cdar buffers))
3449         (if (symbolp buf)
3450             (setq buf (and (boundp buf) (symbol-value buf))))
3451         (and buf
3452              (get-buffer-window buf)
3453              (progn
3454                (setq bufs (cons buf bufs))
3455                (pop-to-buffer buf)
3456                (if (or (not lowest)
3457                        (< (gnus-window-top-edge) lowest))
3458                    (progn
3459                      (setq lowest (gnus-window-top-edge))
3460                      (setq lowest-buf buf)))))
3461         (setq buffers (cdr buffers)))
3462       ;; Remove windows on *all* summary buffers.
3463       (walk-windows
3464        (lambda (win)
3465          (let ((buf (window-buffer win)))
3466            (if (string-match    "^\\*Summary" (buffer-name buf))
3467                (progn
3468                  (setq bufs (cons buf bufs))
3469                  (pop-to-buffer buf)
3470                  (if (or (not lowest)
3471                          (< (gnus-window-top-edge) lowest))
3472                      (progn
3473                        (setq lowest-buf buf)
3474                        (setq lowest (gnus-window-top-edge)))))))))
3475       (and lowest-buf
3476            (progn
3477              (pop-to-buffer lowest-buf)
3478              (switch-to-buffer nntp-server-buffer)))
3479       (while bufs
3480         (and (not (eq (car bufs) lowest-buf))
3481              (delete-windows-on (car bufs)))
3482         (setq bufs (cdr bufs))))))
3483
3484 (defun gnus-version ()
3485   "Version numbers of this version of Gnus."
3486   (interactive)
3487   (let ((methods gnus-valid-select-methods)
3488         (mess gnus-version)
3489         meth)
3490     ;; Go through all the legal select methods and add their version
3491     ;; numbers to the total version string.  Only the backends that are
3492     ;; currently in use will have their message numbers taken into
3493     ;; consideration.
3494     (while methods
3495       (setq meth (intern (concat (caar methods) "-version")))
3496       (and (boundp meth)
3497            (stringp (symbol-value meth))
3498            (setq mess (concat mess "; " (symbol-value meth))))
3499       (setq methods (cdr methods)))
3500     (gnus-message 2 mess)))
3501
3502 (defun gnus-info-find-node ()
3503   "Find Info documentation of Gnus."
3504   (interactive)
3505   ;; Enlarge info window if needed.
3506   (let ((mode major-mode)
3507         gnus-info-buffer)
3508     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3509     (setq gnus-info-buffer (current-buffer))
3510     (gnus-configure-windows 'info)))
3511
3512 (defun gnus-days-between (date1 date2)
3513   ;; Return the number of days between date1 and date2.
3514   (- (gnus-day-number date1) (gnus-day-number date2)))
3515
3516 (defun gnus-day-number (date)
3517   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3518                      (timezone-parse-date date))))
3519     (timezone-absolute-from-gregorian
3520      (nth 1 dat) (nth 2 dat) (car dat))))
3521
3522 (defun gnus-encode-date (date)
3523   "Convert DATE to internal time."
3524   (let* ((parse (timezone-parse-date date))
3525          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3526          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3527     (encode-time (caddr time) (cadr time) (car time)
3528                  (caddr date) (cadr date) (car date) (nth 4 date))))
3529
3530 (defun gnus-time-minus (t1 t2)
3531   "Subtract two internal times."
3532   (let ((borrow (< (cadr t1) (cadr t2))))
3533     (list (- (car t1) (car t2) (if borrow 1 0))
3534           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3535
3536 (defun gnus-file-newer-than (file date)
3537   (let ((fdate (nth 5 (file-attributes file))))
3538     (or (> (car fdate) (car date))
3539         (and (= (car fdate) (car date))
3540              (> (nth 1 fdate) (nth 1 date))))))
3541
3542 (defmacro gnus-local-set-keys (&rest plist)
3543   "Set the keys in PLIST in the current keymap."
3544   `(gnus-define-keys-1 (current-local-map) ',plist))
3545
3546 (defmacro gnus-define-keys (keymap &rest plist)
3547   "Define all keys in PLIST in KEYMAP."
3548   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3549
3550 (put 'gnus-define-keys 'lisp-indent-function 1)
3551 (put 'gnus-define-keys 'lisp-indent-hook 1)
3552 (put 'gnus-define-keymap 'lisp-indent-function 1)
3553 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3554
3555 (defmacro gnus-define-keymap (keymap &rest plist)
3556   "Define all keys in PLIST in KEYMAP."
3557   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3558
3559 (defun gnus-define-keys-1 (keymap plist)
3560   (when (null keymap)
3561     (error "Can't set keys in a null keymap"))
3562   (cond ((symbolp keymap)
3563          (setq keymap (symbol-value keymap)))
3564         ((keymapp keymap))
3565         ((listp keymap)
3566          (set (car keymap) nil)
3567          (define-prefix-command (car keymap))
3568          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3569          (setq keymap (symbol-value (car keymap)))))
3570   (let (key)
3571     (while plist
3572       (when (symbolp (setq key (pop plist)))
3573         (setq key (symbol-value key)))
3574       (define-key keymap key (pop plist)))))
3575
3576 (defun gnus-group-read-only-p (&optional group)
3577   "Check whether GROUP supports editing or not.
3578 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3579 that that variable is buffer-local to the summary buffers."
3580   (let ((group (or group gnus-newsgroup-name)))
3581     (not (gnus-check-backend-function 'request-replace-article group))))
3582
3583 (defun gnus-group-total-expirable-p (group)
3584   "Check whether GROUP is total-expirable or not."
3585   (let ((params (gnus-info-params (gnus-get-info group))))
3586     (or (memq 'total-expire params)
3587         (cdr (assq 'total-expire params)) ; (total-expire . t)
3588         (and gnus-total-expirable-newsgroups ; Check var.
3589              (string-match gnus-total-expirable-newsgroups group)))))
3590
3591 (defun gnus-group-auto-expirable-p (group)
3592   "Check whether GROUP is total-expirable or not."
3593   (let ((params (gnus-info-params (gnus-get-info group))))
3594     (or (memq 'auto-expire params)
3595         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3596         (and gnus-auto-expirable-newsgroups ; Check var.
3597              (string-match gnus-auto-expirable-newsgroups group)))))
3598
3599 (defun gnus-virtual-group-p (group)
3600   "Say whether GROUP is virtual or not."
3601   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3602                         gnus-valid-select-methods)))
3603
3604 (defun gnus-news-group-p (group &optional article)
3605   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3606   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3607       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3608            (eq (gnus-request-type group article) 'news))))
3609
3610 (defsubst gnus-simplify-subject-fully (subject)
3611   "Simplify a subject string according to the user's wishes."
3612   (cond
3613    ((null gnus-summary-gather-subject-limit)
3614     (gnus-simplify-subject-re subject))
3615    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3616     (gnus-simplify-subject-fuzzy subject))
3617    ((numberp gnus-summary-gather-subject-limit)
3618     (gnus-limit-string (gnus-simplify-subject-re subject)
3619                        gnus-summary-gather-subject-limit))
3620    (t
3621     subject)))
3622
3623 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3624   "Check whether two subjects are equal.  If optional argument
3625 simple-first is t, first argument is already simplified."
3626   (cond
3627    ((null simple-first)
3628     (equal (gnus-simplify-subject-fully s1)
3629            (gnus-simplify-subject-fully s2)))
3630    (t
3631     (equal s1
3632            (gnus-simplify-subject-fully s2)))))
3633
3634 ;; Returns a list of writable groups.
3635 (defun gnus-writable-groups ()
3636   (let ((alist gnus-newsrc-alist)
3637         groups group)
3638     (while (setq group (car (pop alist)))
3639       (unless (gnus-group-read-only-p group)
3640         (push group groups)))
3641     (nreverse groups)))
3642
3643 (defun gnus-completing-read (default prompt &rest args)
3644   ;; Like `completing-read', except that DEFAULT is the default argument.
3645   (let* ((prompt (if default 
3646                      (concat prompt " (default " default ") ")
3647                    (concat prompt " ")))
3648          (answer (apply 'completing-read prompt args)))
3649     (if (or (null answer) (zerop (length answer)))
3650         default
3651       answer)))
3652
3653 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3654 ;; the echo area.
3655 (defun gnus-y-or-n-p (prompt)
3656   (prog1
3657       (y-or-n-p prompt)
3658     (message "")))
3659
3660 (defun gnus-yes-or-no-p (prompt)
3661   (prog1
3662       (yes-or-no-p prompt)
3663     (message "")))
3664
3665 ;; Check whether to use long file names.
3666 (defun gnus-use-long-file-name (symbol)
3667   ;; The variable has to be set...
3668   (and gnus-use-long-file-name
3669        ;; If it isn't a list, then we return t.
3670        (or (not (listp gnus-use-long-file-name))
3671            ;; If it is a list, and the list contains `symbol', we
3672            ;; return nil.
3673            (not (memq symbol gnus-use-long-file-name)))))
3674
3675 ;; I suspect there's a better way, but I haven't taken the time to do
3676 ;; it yet. -erik selberg@cs.washington.edu
3677 (defun gnus-dd-mmm (messy-date)
3678   "Return a string like DD-MMM from a big messy string"
3679   (let ((datevec (timezone-parse-date messy-date)))
3680     (format "%2s-%s"
3681             (condition-case ()
3682                 ;; Make sure leading zeroes are stripped.
3683                 (number-to-string (string-to-number (aref datevec 2)))
3684               (error "??"))
3685             (capitalize
3686              (or (car
3687                   (nth (1- (string-to-number (aref datevec 1)))
3688                        timezone-months-assoc))
3689                  "???")))))
3690
3691 ;; Make a hash table (default and minimum size is 255).
3692 ;; Optional argument HASHSIZE specifies the table size.
3693 (defun gnus-make-hashtable (&optional hashsize)
3694   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3695
3696 ;; Make a number that is suitable for hashing; bigger than MIN and one
3697 ;; less than 2^x.
3698 (defun gnus-create-hash-size (min)
3699   (let ((i 1))
3700     (while (< i min)
3701       (setq i (* 2 i)))
3702     (1- i)))
3703
3704 ;; Show message if message has a lower level than `gnus-verbose'.
3705 ;; Guideline for numbers:
3706 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3707 ;; for things that take a long time, 7 - not very important messages
3708 ;; on stuff, 9 - messages inside loops.
3709 (defun gnus-message (level &rest args)
3710   (if (<= level gnus-verbose)
3711       (apply 'message args)
3712     ;; We have to do this format thingy here even if the result isn't
3713     ;; shown - the return value has to be the same as the return value
3714     ;; from `message'.
3715     (apply 'format args)))
3716
3717 (defun gnus-error (level &rest args)
3718   "Beep an error if `gnus-verbose' is on LEVEL or less."
3719   (when (<= (floor level) gnus-verbose)
3720     (apply 'message args)
3721     (ding)
3722     (let (duration)
3723       (when (and (floatp level)
3724                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3725         (sit-for duration))))
3726   nil)
3727
3728 ;; Generate a unique new group name.
3729 (defun gnus-generate-new-group-name (leaf)
3730   (let ((name leaf)
3731         (num 0))
3732     (while (gnus-gethash name gnus-newsrc-hashtb)
3733       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3734     name))
3735
3736 (defsubst gnus-hide-text (b e props)
3737   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3738   (add-text-properties b e props)
3739   (when (memq 'intangible props)
3740     (put-text-property (max (1- b) (point-min))
3741                        b 'intangible (cddr (memq 'intangible props)))))
3742
3743 (defsubst gnus-unhide-text (b e)
3744   "Remove hidden text properties from region between B and E."
3745   (remove-text-properties b e gnus-hidden-properties)
3746   (when (memq 'intangible gnus-hidden-properties)
3747     (put-text-property (max (1- b) (point-min))
3748                        b 'intangible nil)))
3749
3750 (defun gnus-hide-text-type (b e type)
3751   "Hide text of TYPE between B and E."
3752   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3753
3754 ;; Find out whether the gnus-visual TYPE is wanted.
3755 (defun gnus-visual-p (&optional type class)
3756   (and gnus-visual                      ; Has to be non-nil, at least.
3757        (if (not type)                   ; We don't care about type.
3758            gnus-visual
3759          (if (listp gnus-visual)        ; It's a list, so we check it.
3760              (or (memq type gnus-visual)
3761                  (memq class gnus-visual))
3762            t))))
3763
3764 (defun gnus-parent-id (references)
3765   "Return the last Message-ID in REFERENCES."
3766   (when (and references
3767              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3768     (substring references (match-beginning 1) (match-end 1))))
3769
3770 (defun gnus-split-references (references)
3771   "Return a list of Message-IDs in REFERENCES."
3772   (let ((beg 0)
3773         ids)
3774     (while (string-match "<[^>]+>" references beg)
3775       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3776             ids))
3777     (nreverse ids)))
3778
3779 (defun gnus-buffer-live-p (buffer)
3780   "Say whether BUFFER is alive or not."
3781   (and buffer
3782        (get-buffer buffer)
3783        (buffer-name (get-buffer buffer))))
3784
3785 (defun gnus-ephemeral-group-p (group)
3786   "Say whether GROUP is ephemeral or not."
3787   (gnus-group-get-parameter group 'quit-config))
3788
3789 (defun gnus-group-quit-config (group)
3790   "Return the quit-config of GROUP."
3791   (gnus-group-get-parameter group 'quit-config))
3792
3793 (defun gnus-simplify-mode-line ()
3794   "Make mode lines a bit simpler."
3795   (setq mode-line-modified "-- ")
3796   (when (listp mode-line-format)
3797     (make-local-variable 'mode-line-format)
3798     (setq mode-line-format (copy-sequence mode-line-format))
3799     (when (equal (nth 3 mode-line-format) "   ")
3800       (setcar (nthcdr 3 mode-line-format) " "))))
3801
3802 ;;; List and range functions
3803
3804 (defun gnus-last-element (list)
3805   "Return last element of LIST."
3806   (while (cdr list)
3807     (setq list (cdr list)))
3808   (car list))
3809
3810 (defun gnus-copy-sequence (list)
3811   "Do a complete, total copy of a list."
3812   (if (and (consp list) (not (consp (cdr list))))
3813       (cons (car list) (cdr list))
3814     (mapcar (lambda (elem) (if (consp elem)
3815                                (if (consp (cdr elem))
3816                                    (gnus-copy-sequence elem)
3817                                  (cons (car elem) (cdr elem)))
3818                              elem))
3819             list)))
3820
3821 (defun gnus-set-difference (list1 list2)
3822   "Return a list of elements of LIST1 that do not appear in LIST2."
3823   (let ((list1 (copy-sequence list1)))
3824     (while list2
3825       (setq list1 (delq (car list2) list1))
3826       (setq list2 (cdr list2)))
3827     list1))
3828
3829 (defun gnus-sorted-complement (list1 list2)
3830   "Return a list of elements of LIST1 that do not appear in LIST2.
3831 Both lists have to be sorted over <."
3832   (let (out)
3833     (if (or (null list1) (null list2))
3834         (or list1 list2)
3835       (while (and list1 list2)
3836         (cond ((= (car list1) (car list2))
3837                (setq list1 (cdr list1)
3838                      list2 (cdr list2)))
3839               ((< (car list1) (car list2))
3840                (setq out (cons (car list1) out))
3841                (setq list1 (cdr list1)))
3842               (t
3843                (setq out (cons (car list2) out))
3844                (setq list2 (cdr list2)))))
3845       (nconc (nreverse out) (or list1 list2)))))
3846
3847 (defun gnus-intersection (list1 list2)
3848   (let ((result nil))
3849     (while list2
3850       (if (memq (car list2) list1)
3851           (setq result (cons (car list2) result)))
3852       (setq list2 (cdr list2)))
3853     result))
3854
3855 (defun gnus-sorted-intersection (list1 list2)
3856   ;; LIST1 and LIST2 have to be sorted over <.
3857   (let (out)
3858     (while (and list1 list2)
3859       (cond ((= (car list1) (car list2))
3860              (setq out (cons (car list1) out)
3861                    list1 (cdr list1)
3862                    list2 (cdr list2)))
3863             ((< (car list1) (car list2))
3864              (setq list1 (cdr list1)))
3865             (t
3866              (setq list2 (cdr list2)))))
3867     (nreverse out)))
3868
3869 (defun gnus-set-sorted-intersection (list1 list2)
3870   ;; LIST1 and LIST2 have to be sorted over <.
3871   ;; This function modifies LIST1.
3872   (let* ((top (cons nil list1))
3873          (prev top))
3874     (while (and list1 list2)
3875       (cond ((= (car list1) (car list2))
3876              (setq prev list1
3877                    list1 (cdr list1)
3878                    list2 (cdr list2)))
3879             ((< (car list1) (car list2))
3880              (setcdr prev (cdr list1))
3881              (setq list1 (cdr list1)))
3882             (t
3883              (setq list2 (cdr list2)))))
3884     (setcdr prev nil)
3885     (cdr top)))
3886
3887 (defun gnus-compress-sequence (numbers &optional always-list)
3888   "Convert list of numbers to a list of ranges or a single range.
3889 If ALWAYS-LIST is non-nil, this function will always release a list of
3890 ranges."
3891   (let* ((first (car numbers))
3892          (last (car numbers))
3893          result)
3894     (if (null numbers)
3895         nil
3896       (if (not (listp (cdr numbers)))
3897           numbers
3898         (while numbers
3899           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3900                 ((= (1+ last) (car numbers)) ;Still in sequence
3901                  (setq last (car numbers)))
3902                 (t                      ;End of one sequence
3903                  (setq result
3904                        (cons (if (= first last) first
3905                                (cons first last)) result))
3906                  (setq first (car numbers))
3907                  (setq last  (car numbers))))
3908           (setq numbers (cdr numbers)))
3909         (if (and (not always-list) (null result))
3910             (if (= first last) (list first) (cons first last))
3911           (nreverse (cons (if (= first last) first (cons first last))
3912                           result)))))))
3913
3914 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3915 (defun gnus-uncompress-range (ranges)
3916   "Expand a list of ranges into a list of numbers.
3917 RANGES is either a single range on the form `(num . num)' or a list of
3918 these ranges."
3919   (let (first last result)
3920     (cond
3921      ((null ranges)
3922       nil)
3923      ((not (listp (cdr ranges)))
3924       (setq first (car ranges))
3925       (setq last (cdr ranges))
3926       (while (<= first last)
3927         (setq result (cons first result))
3928         (setq first (1+ first)))
3929       (nreverse result))
3930      (t
3931       (while ranges
3932         (if (atom (car ranges))
3933             (if (numberp (car ranges))
3934                 (setq result (cons (car ranges) result)))
3935           (setq first (caar ranges))
3936           (setq last  (cdar ranges))
3937           (while (<= first last)
3938             (setq result (cons first result))
3939             (setq first (1+ first))))
3940         (setq ranges (cdr ranges)))
3941       (nreverse result)))))
3942
3943 (defun gnus-add-to-range (ranges list)
3944   "Return a list of ranges that has all articles from both RANGES and LIST.
3945 Note: LIST has to be sorted over `<'."
3946   (if (not ranges)
3947       (gnus-compress-sequence list t)
3948     (setq list (copy-sequence list))
3949     (or (listp (cdr ranges))
3950         (setq ranges (list ranges)))
3951     (let ((out ranges)
3952           ilist lowest highest temp)
3953       (while (and ranges list)
3954         (setq ilist list)
3955         (setq lowest (or (and (atom (car ranges)) (car ranges))
3956                          (caar ranges)))
3957         (while (and list (cdr list) (< (cadr list) lowest))
3958           (setq list (cdr list)))
3959         (if (< (car ilist) lowest)
3960             (progn
3961               (setq temp list)
3962               (setq list (cdr list))
3963               (setcdr temp nil)
3964               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3965         (setq highest (or (and (atom (car ranges)) (car ranges))
3966                           (cdar ranges)))
3967         (while (and list (<= (car list) highest))
3968           (setq list (cdr list)))
3969         (setq ranges (cdr ranges)))
3970       (if list
3971           (setq out (nconc (gnus-compress-sequence list t) out)))
3972       (setq out (sort out (lambda (r1 r2)
3973                             (< (or (and (atom r1) r1) (car r1))
3974                                (or (and (atom r2) r2) (car r2))))))
3975       (setq ranges out)
3976       (while ranges
3977         (if (atom (car ranges))
3978             (if (cdr ranges)
3979                 (if (atom (cadr ranges))
3980                     (if (= (1+ (car ranges)) (cadr ranges))
3981                         (progn
3982                           (setcar ranges (cons (car ranges)
3983                                                (cadr ranges)))
3984                           (setcdr ranges (cddr ranges))))
3985                   (if (= (1+ (car ranges)) (caadr ranges))
3986                       (progn
3987                         (setcar (cadr ranges) (car ranges))
3988                         (setcar ranges (cadr ranges))
3989                         (setcdr ranges (cddr ranges))))))
3990           (if (cdr ranges)
3991               (if (atom (cadr ranges))
3992                   (if (= (1+ (cdar ranges)) (cadr ranges))
3993                       (progn
3994                         (setcdr (car ranges) (cadr ranges))
3995                         (setcdr ranges (cddr ranges))))
3996                 (if (= (1+ (cdar ranges)) (caadr ranges))
3997                     (progn
3998                       (setcdr (car ranges) (cdadr ranges))
3999                       (setcdr ranges (cddr ranges)))))))
4000         (setq ranges (cdr ranges)))
4001       out)))
4002
4003 (defun gnus-remove-from-range (ranges list)
4004   "Return a list of ranges that has all articles from LIST removed from RANGES.
4005 Note: LIST has to be sorted over `<'."
4006   ;; !!! This function shouldn't look like this, but I've got a headache.
4007   (gnus-compress-sequence
4008    (gnus-sorted-complement
4009     (gnus-uncompress-range ranges) list)))
4010
4011 (defun gnus-member-of-range (number ranges)
4012   (if (not (listp (cdr ranges)))
4013       (and (>= number (car ranges))
4014            (<= number (cdr ranges)))
4015     (let ((not-stop t))
4016       (while (and ranges
4017                   (if (numberp (car ranges))
4018                       (>= number (car ranges))
4019                     (>= number (caar ranges)))
4020                   not-stop)
4021         (if (if (numberp (car ranges))
4022                 (= number (car ranges))
4023               (and (>= number (caar ranges))
4024                    (<= number (cdar ranges))))
4025             (setq not-stop nil))
4026         (setq ranges (cdr ranges)))
4027       (not not-stop))))
4028
4029 (defun gnus-range-length (range)
4030   "Return the length RANGE would have if uncompressed."
4031   (length (gnus-uncompress-range range)))
4032
4033 (defun gnus-sublist-p (list sublist)
4034   "Test whether all elements in SUBLIST are members of LIST."
4035   (let ((sublistp t))
4036     (while sublist
4037       (unless (memq (pop sublist) list)
4038         (setq sublistp nil
4039               sublist nil)))
4040     sublistp))
4041
4042 \f
4043 ;;;
4044 ;;; Gnus group mode
4045 ;;;
4046
4047 (defvar gnus-group-mode-map nil)
4048 (put 'gnus-group-mode 'mode-class 'special)
4049
4050 (unless gnus-group-mode-map
4051   (setq gnus-group-mode-map (make-keymap))
4052   (suppress-keymap gnus-group-mode-map)
4053
4054   (gnus-define-keys gnus-group-mode-map
4055     " " gnus-group-read-group
4056     "=" gnus-group-select-group
4057     "\r" gnus-group-select-group
4058     "\M-\r" gnus-group-quick-select-group
4059     "j" gnus-group-jump-to-group
4060     "n" gnus-group-next-unread-group
4061     "p" gnus-group-prev-unread-group
4062     "\177" gnus-group-prev-unread-group
4063     [delete] gnus-group-prev-unread-group
4064     "N" gnus-group-next-group
4065     "P" gnus-group-prev-group
4066     "\M-n" gnus-group-next-unread-group-same-level
4067     "\M-p" gnus-group-prev-unread-group-same-level
4068     "," gnus-group-best-unread-group
4069     "." gnus-group-first-unread-group
4070     "u" gnus-group-unsubscribe-current-group
4071     "U" gnus-group-unsubscribe-group
4072     "c" gnus-group-catchup-current
4073     "C" gnus-group-catchup-current-all
4074     "l" gnus-group-list-groups
4075     "L" gnus-group-list-all-groups
4076     "m" gnus-group-mail
4077     "g" gnus-group-get-new-news
4078     "\M-g" gnus-group-get-new-news-this-group
4079     "R" gnus-group-restart
4080     "r" gnus-group-read-init-file
4081     "B" gnus-group-browse-foreign-server
4082     "b" gnus-group-check-bogus-groups
4083     "F" gnus-find-new-newsgroups
4084     "\C-c\C-d" gnus-group-describe-group
4085     "\M-d" gnus-group-describe-all-groups
4086     "\C-c\C-a" gnus-group-apropos
4087     "\C-c\M-\C-a" gnus-group-description-apropos
4088     "a" gnus-group-post-news
4089     "\ek" gnus-group-edit-local-kill
4090     "\eK" gnus-group-edit-global-kill
4091     "\C-k" gnus-group-kill-group
4092     "\C-y" gnus-group-yank-group
4093     "\C-w" gnus-group-kill-region
4094     "\C-x\C-t" gnus-group-transpose-groups
4095     "\C-c\C-l" gnus-group-list-killed
4096     "\C-c\C-x" gnus-group-expire-articles
4097     "\C-c\M-\C-x" gnus-group-expire-all-groups
4098     "V" gnus-version
4099     "s" gnus-group-save-newsrc
4100     "z" gnus-group-suspend
4101     "Z" gnus-group-clear-dribble
4102     "q" gnus-group-exit
4103     "Q" gnus-group-quit
4104     "?" gnus-group-describe-briefly
4105     "\C-c\C-i" gnus-info-find-node
4106     "\M-e" gnus-group-edit-group-method
4107     "^" gnus-group-enter-server-mode
4108     gnus-mouse-2 gnus-mouse-pick-group
4109     "<" beginning-of-buffer
4110     ">" end-of-buffer
4111     "\C-c\C-b" gnus-bug
4112     "\C-c\C-s" gnus-group-sort-groups
4113     "t" gnus-topic-mode
4114     "\C-c\M-g" gnus-activate-all-groups
4115     "\M-&" gnus-group-universal-argument
4116     "#" gnus-group-mark-group
4117     "\M-#" gnus-group-unmark-group)
4118
4119   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4120     "m" gnus-group-mark-group
4121     "u" gnus-group-unmark-group
4122     "w" gnus-group-mark-region
4123     "m" gnus-group-mark-buffer
4124     "r" gnus-group-mark-regexp
4125     "U" gnus-group-unmark-all-groups)
4126
4127   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4128     "d" gnus-group-make-directory-group
4129     "h" gnus-group-make-help-group
4130     "a" gnus-group-make-archive-group
4131     "k" gnus-group-make-kiboze-group
4132     "m" gnus-group-make-group
4133     "E" gnus-group-edit-group
4134     "e" gnus-group-edit-group-method
4135     "p" gnus-group-edit-group-parameters
4136     "v" gnus-group-add-to-virtual
4137     "V" gnus-group-make-empty-virtual
4138     "D" gnus-group-enter-directory
4139     "f" gnus-group-make-doc-group
4140     "r" gnus-group-rename-group
4141     "\177" gnus-group-delete-group
4142     [delete] gnus-group-delete-group)
4143
4144    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4145      "b" gnus-group-brew-soup
4146      "w" gnus-soup-save-areas
4147      "s" gnus-soup-send-replies
4148      "p" gnus-soup-pack-packet
4149      "r" nnsoup-pack-replies)
4150
4151    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4152      "s" gnus-group-sort-groups
4153      "a" gnus-group-sort-groups-by-alphabet
4154      "u" gnus-group-sort-groups-by-unread
4155      "l" gnus-group-sort-groups-by-level
4156      "v" gnus-group-sort-groups-by-score
4157      "r" gnus-group-sort-groups-by-rank
4158      "m" gnus-group-sort-groups-by-method)
4159
4160    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4161      "k" gnus-group-list-killed
4162      "z" gnus-group-list-zombies
4163      "s" gnus-group-list-groups
4164      "u" gnus-group-list-all-groups
4165      "A" gnus-group-list-active
4166      "a" gnus-group-apropos
4167      "d" gnus-group-description-apropos
4168      "m" gnus-group-list-matching
4169      "M" gnus-group-list-all-matching
4170      "l" gnus-group-list-level)
4171
4172    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4173      "f" gnus-score-flush-cache)
4174
4175    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4176      "f" gnus-group-fetch-faq)
4177
4178    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4179      "l" gnus-group-set-current-level
4180      "t" gnus-group-unsubscribe-current-group
4181      "s" gnus-group-unsubscribe-group
4182      "k" gnus-group-kill-group
4183      "y" gnus-group-yank-group
4184      "w" gnus-group-kill-region
4185      "\C-k" gnus-group-kill-level
4186      "z" gnus-group-kill-all-zombies))
4187
4188 (defun gnus-group-mode ()
4189   "Major mode for reading news.
4190
4191 All normal editing commands are switched off.
4192 \\<gnus-group-mode-map>
4193 The group buffer lists (some of) the groups available.  For instance,
4194 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4195 lists all zombie groups.
4196
4197 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4198 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4199
4200 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4201
4202 The following commands are available:
4203
4204 \\{gnus-group-mode-map}"
4205   (interactive)
4206   (when (and menu-bar-mode
4207              (gnus-visual-p 'group-menu 'menu))
4208     (gnus-group-make-menu-bar))
4209   (kill-all-local-variables)
4210   (gnus-simplify-mode-line)
4211   (setq major-mode 'gnus-group-mode)
4212   (setq mode-name "Group")
4213   (gnus-group-set-mode-line)
4214   (setq mode-line-process nil)
4215   (use-local-map gnus-group-mode-map)
4216   (buffer-disable-undo (current-buffer))
4217   (setq truncate-lines t)
4218   (setq buffer-read-only t)
4219   (run-hooks 'gnus-group-mode-hook))
4220
4221 (defun gnus-mouse-pick-group (e)
4222   "Enter the group under the mouse pointer."
4223   (interactive "e")
4224   (mouse-set-point e)
4225   (gnus-group-read-group nil))
4226
4227 ;; Look at LEVEL and find out what the level is really supposed to be.
4228 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4229 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4230 (defun gnus-group-default-level (&optional level number-or-nil)
4231   (cond
4232    (gnus-group-use-permanent-levels
4233 ;    (setq gnus-group-default-list-level
4234 ;         (or level gnus-group-default-list-level))
4235     (or level gnus-group-default-list-level gnus-level-subscribed))
4236    (number-or-nil
4237     level)
4238    (t
4239     (or level gnus-group-default-list-level gnus-level-subscribed))))
4240
4241 ;;;###autoload
4242 (defun gnus-slave-no-server (&optional arg)
4243   "Read network news as a slave, without connecting to local server"
4244   (interactive "P")
4245   (gnus-no-server arg t))
4246
4247 ;;;###autoload
4248 (defun gnus-no-server (&optional arg slave)
4249   "Read network news.
4250 If ARG is a positive number, Gnus will use that as the
4251 startup level.  If ARG is nil, Gnus will be started at level 2.
4252 If ARG is non-nil and not a positive number, Gnus will
4253 prompt the user for the name of an NNTP server to use.
4254 As opposed to `gnus', this command will not connect to the local server."
4255   (interactive "P")
4256   (let ((gnus-group-use-permanent-levels t))
4257     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4258   (make-local-variable 'gnus-group-use-permanent-levels)
4259   (setq gnus-group-use-permanent-levels t))
4260
4261 ;;;###autoload
4262 (defun gnus-slave (&optional arg)
4263   "Read news as a slave."
4264   (interactive "P")
4265   (gnus arg nil 'slave))
4266
4267 ;;;###autoload
4268 (defun gnus-other-frame (&optional arg)
4269   "Pop up a frame to read news."
4270   (interactive "P")
4271   (if (get-buffer gnus-group-buffer)
4272       (let ((pop-up-frames t))
4273         (gnus arg))
4274     (select-frame (make-frame))
4275     (gnus arg)))
4276
4277 ;;;###autoload
4278 (defun gnus (&optional arg dont-connect slave)
4279   "Read network news.
4280 If ARG is non-nil and a positive number, Gnus will use that as the
4281 startup level.  If ARG is non-nil and not a positive number, Gnus will
4282 prompt the user for the name of an NNTP server to use."
4283   (interactive "P")
4284
4285   (if (get-buffer gnus-group-buffer)
4286       (progn
4287         (switch-to-buffer gnus-group-buffer)
4288         (gnus-group-get-new-news))
4289
4290     (gnus-clear-system)
4291     (nnheader-init-server-buffer)
4292     (gnus-read-init-file)
4293     (setq gnus-slave slave)
4294
4295     (gnus-group-setup-buffer)
4296     (let ((buffer-read-only nil))
4297       (erase-buffer)
4298       (if (not gnus-inhibit-startup-message)
4299           (progn
4300             (gnus-group-startup-message)
4301             (sit-for 0))))
4302
4303     (let ((level (and (numberp arg) (> arg 0) arg))
4304           did-connect)
4305       (unwind-protect
4306           (progn
4307             (or dont-connect
4308                 (setq did-connect
4309                       (gnus-start-news-server (and arg (not level))))))
4310         (if (and (not dont-connect)
4311                  (not did-connect))
4312             (gnus-group-quit)
4313           (run-hooks 'gnus-startup-hook)
4314           ;; NNTP server is successfully open.
4315
4316           ;; Find the current startup file name.
4317           (setq gnus-current-startup-file
4318                 (gnus-make-newsrc-file gnus-startup-file))
4319
4320           ;; Read the dribble file.
4321           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4322
4323           ;; Allow using GroupLens predictions.
4324           (when gnus-use-grouplens
4325             (bbb-login)
4326             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4327
4328           (gnus-summary-make-display-table)
4329           ;; Do the actual startup.
4330           (gnus-setup-news nil level dont-connect)
4331           ;; Generate the group buffer.
4332           (gnus-group-list-groups level)
4333           (gnus-group-first-unread-group)
4334           (gnus-configure-windows 'group)
4335           (gnus-group-set-mode-line))))))
4336
4337 (defun gnus-unload ()
4338   "Unload all Gnus features."
4339   (interactive)
4340   (or (boundp 'load-history)
4341       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4342   (let ((history load-history)
4343         feature)
4344     (while history
4345       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4346            (setq feature (cdr (assq 'provide (car history))))
4347            (unload-feature feature 'force))
4348       (setq history (cdr history)))))
4349
4350 (defun gnus-compile ()
4351   "Byte-compile the user-defined format specs."
4352   (interactive)
4353   (let ((entries gnus-format-specs)
4354         entry gnus-tmp-func)
4355     (save-excursion
4356       (gnus-message 7 "Compiling format specs...")
4357
4358       (while entries
4359         (setq entry (pop entries))
4360         (if (eq (car entry) 'version)
4361             (setq gnus-format-specs (delq entry gnus-format-specs))
4362           (when (and (listp (caddr entry))
4363                      (not (eq 'byte-code (caaddr entry))))
4364             (fset 'gnus-tmp-func
4365                   `(lambda () ,(caddr entry)))
4366             (byte-compile 'gnus-tmp-func)
4367             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4368
4369       (push (cons 'version emacs-version) gnus-format-specs)
4370
4371       (gnus-message 7 "Compiling user specs...done"))))
4372
4373 (defun gnus-indent-rigidly (start end arg)
4374   "Indent rigidly using only spaces and no tabs."
4375   (save-excursion
4376     (save-restriction
4377       (narrow-to-region start end)
4378       (indent-rigidly start end arg)
4379       (goto-char (point-min))
4380       (while (search-forward "\t" nil t)
4381         (replace-match "        " t t)))))
4382
4383 (defun gnus-group-startup-message (&optional x y)
4384   "Insert startup message in current buffer."
4385   ;; Insert the message.
4386   (erase-buffer)
4387   (insert
4388    (format "              %s
4389           _    ___ _             _
4390           _ ___ __ ___  __    _ ___
4391           __   _     ___    __  ___
4392               _           ___     _
4393              _  _ __             _
4394              ___   __            _
4395                    __           _
4396                     _      _   _
4397                    _      _    _
4398                       _  _    _
4399                   __  ___
4400                  _   _ _     _
4401                 _   _
4402               _    _
4403              _    _
4404             _
4405           __
4406
4407 "
4408            ""))
4409   ;; And then hack it.
4410   (gnus-indent-rigidly (point-min) (point-max)
4411                        (/ (max (- (window-width) (or x 46)) 0) 2))
4412   (goto-char (point-min))
4413   (forward-line 1)
4414   (let* ((pheight (count-lines (point-min) (point-max)))
4415          (wheight (window-height))
4416          (rest (- wheight pheight)))
4417     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4418   ;; Fontify some.
4419   (goto-char (point-min))
4420   (and (search-forward "Praxis" nil t)
4421        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4422   (goto-char (point-min))
4423   (let* ((mode-string (gnus-group-set-mode-line)))
4424     (setq mode-line-buffer-identification
4425           (list (concat gnus-version (substring (car mode-string) 4))))
4426     (set-buffer-modified-p t)))
4427
4428 (defun gnus-group-setup-buffer ()
4429   (or (get-buffer gnus-group-buffer)
4430       (progn
4431         (switch-to-buffer gnus-group-buffer)
4432         (gnus-add-current-to-buffer-list)
4433         (gnus-group-mode)
4434         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4435
4436 (defun gnus-group-list-groups (&optional level unread lowest)
4437   "List newsgroups with level LEVEL or lower that have unread articles.
4438 Default is all subscribed groups.
4439 If argument UNREAD is non-nil, groups with no unread articles are also
4440 listed."
4441   (interactive (list (if current-prefix-arg
4442                          (prefix-numeric-value current-prefix-arg)
4443                        (or
4444                         (gnus-group-default-level nil t)
4445                         gnus-group-default-list-level
4446                         gnus-level-subscribed))))
4447   (or level
4448       (setq level (car gnus-group-list-mode)
4449             unread (cdr gnus-group-list-mode)))
4450   (setq level (gnus-group-default-level level))
4451   (gnus-group-setup-buffer)             ;May call from out of group buffer
4452   (gnus-update-format-specifications)
4453   (let ((case-fold-search nil)
4454         (props (text-properties-at (gnus-point-at-bol)))
4455         (group (gnus-group-group-name)))
4456     (set-buffer gnus-group-buffer)
4457     (funcall gnus-group-prepare-function level unread lowest)
4458     (if (zerop (buffer-size))
4459         (gnus-message 5 gnus-no-groups-message)
4460       (goto-char (point-max))
4461       (when (or (not gnus-group-goto-next-group-function)
4462                 (not (funcall gnus-group-goto-next-group-function 
4463                               group props)))
4464         (if (not group)
4465             ;; Go to the first group with unread articles.
4466             (gnus-group-search-forward t)
4467           ;; Find the right group to put point on.  If the current group
4468           ;; has disappeared in the new listing, try to find the next
4469           ;; one.        If no next one can be found, just leave point at the
4470           ;; first newsgroup in the buffer.
4471           (if (not (gnus-goto-char
4472                     (text-property-any
4473                      (point-min) (point-max)
4474                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4475               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4476                 (while (and newsrc
4477                             (not (gnus-goto-char
4478                                   (text-property-any
4479                                    (point-min) (point-max) 'gnus-group
4480                                    (gnus-intern-safe
4481                                     (caar newsrc) gnus-active-hashtb)))))
4482                   (setq newsrc (cdr newsrc)))
4483                 (or newsrc (progn (goto-char (point-max))
4484                                   (forward-line -1)))))))
4485       ;; Adjust cursor point.
4486       (gnus-group-position-point))))
4487
4488 (defun gnus-group-list-level (level &optional all)
4489   "List groups on LEVEL.
4490 If ALL (the prefix), also list groups that have no unread articles."
4491   (interactive "nList groups on level: \nP")
4492   (gnus-group-list-groups level all level))
4493
4494 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4495   "List all newsgroups with unread articles of level LEVEL or lower.
4496 If ALL is non-nil, list groups that have no unread articles.
4497 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4498 If REGEXP, only list groups matching REGEXP."
4499   (set-buffer gnus-group-buffer)
4500   (let ((buffer-read-only nil)
4501         (newsrc (cdr gnus-newsrc-alist))
4502         (lowest (or lowest 1))
4503         info clevel unread group params)
4504     (erase-buffer)
4505     (if (< lowest gnus-level-zombie)
4506         ;; List living groups.
4507         (while newsrc
4508           (setq info (car newsrc)
4509                 group (gnus-info-group info)
4510                 params (gnus-info-params info)
4511                 newsrc (cdr newsrc)
4512                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4513           (and unread                   ; This group might be bogus
4514                (or (not regexp)
4515                    (string-match regexp group))
4516                (<= (setq clevel (gnus-info-level info)) level)
4517                (>= clevel lowest)
4518                (or all                  ; We list all groups?
4519                    (if (eq unread t)    ; Unactivated?
4520                        gnus-group-list-inactive-groups ; We list unactivated 
4521                      (> unread 0))      ; We list groups with unread articles
4522                    (and gnus-list-groups-with-ticked-articles
4523                         (cdr (assq 'tick (gnus-info-marks info))))
4524                                         ; And groups with tickeds
4525                    ;; Check for permanent visibility.
4526                    (and gnus-permanently-visible-groups
4527                         (string-match gnus-permanently-visible-groups
4528                                       group))
4529                    (memq 'visible params)
4530                    (cdr (assq 'visible params)))
4531                (gnus-group-insert-group-line
4532                 group (gnus-info-level info)
4533                 (gnus-info-marks info) unread (gnus-info-method info)))))
4534
4535     ;; List dead groups.
4536     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4537          (gnus-group-prepare-flat-list-dead
4538           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4539           gnus-level-zombie ?Z
4540           regexp))
4541     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4542          (gnus-group-prepare-flat-list-dead
4543           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4544           gnus-level-killed ?K regexp))
4545
4546     (gnus-group-set-mode-line)
4547     (setq gnus-group-list-mode (cons level all))
4548     (run-hooks 'gnus-group-prepare-hook)))
4549
4550 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4551   ;; List zombies and killed lists somewhat faster, which was
4552   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4553   ;; this by ignoring the group format specification altogether.
4554   (let (group)
4555     (if regexp
4556         ;; This loop is used when listing groups that match some
4557         ;; regexp.
4558         (while groups
4559           (setq group (pop groups))
4560           (when (string-match regexp group)
4561             (add-text-properties
4562              (point) (prog1 (1+ (point))
4563                        (insert " " mark "     *: " group "\n"))
4564              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4565                    'gnus-unread t
4566                    'gnus-level level))))
4567       ;; This loop is used when listing all groups.
4568       (while groups
4569         (add-text-properties
4570          (point) (prog1 (1+ (point))
4571                    (insert " " mark "     *: "
4572                            (setq group (pop groups)) "\n"))
4573          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4574                'gnus-unread t
4575                'gnus-level level))))))
4576
4577 (defmacro gnus-group-real-name (group)
4578   "Find the real name of a foreign newsgroup."
4579   `(let ((gname ,group))
4580      (if (string-match ":[^:]+$" gname)
4581          (substring gname (1+ (match-beginning 0)))
4582        gname)))
4583
4584 (defsubst gnus-server-add-address (method)
4585   (let ((method-name (symbol-name (car method))))
4586     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4587              (not (assq (intern (concat method-name "-address")) method)))
4588         (append method (list (list (intern (concat method-name "-address"))
4589                                    (nth 1 method))))
4590       method)))
4591
4592 (defsubst gnus-server-get-method (group method)
4593   ;; Input either a server name, and extended server name, or a
4594   ;; select method, and return a select method.
4595   (cond ((stringp method)
4596          (gnus-server-to-method method))
4597         ((equal method gnus-select-method)
4598          gnus-select-method)
4599         ((and (stringp (car method)) group)
4600          (gnus-server-extend-method group method))
4601         ((and method (not group)
4602               (equal (cadr method) ""))
4603          method)
4604         (t
4605          (gnus-server-add-address method))))
4606
4607 (defun gnus-server-to-method (server)
4608   "Map virtual server names to select methods."
4609   (or 
4610    ;; Perhaps this is the native server?
4611    (and (equal server "native") gnus-select-method)
4612    ;; It should be in the server alist.
4613    (cdr (assoc server gnus-server-alist))
4614    ;; If not, we look through all the opened server
4615    ;; to see whether we can find it there.
4616    (let ((opened gnus-opened-servers))
4617      (while (and opened
4618                  (not (equal server (format "%s:%s" (caaar opened)
4619                                             (cadaar opened)))))
4620        (pop opened))
4621      (caar opened))))
4622
4623 (defmacro gnus-method-equal (ss1 ss2)
4624   "Say whether two servers are equal."
4625   `(let ((s1 ,ss1)
4626          (s2 ,ss2))
4627      (or (equal s1 s2)
4628          (and (= (length s1) (length s2))
4629               (progn
4630                 (while (and s1 (member (car s1) s2))
4631                   (setq s1 (cdr s1)))
4632                 (null s1))))))
4633
4634 (defun gnus-server-equal (m1 m2)
4635   "Say whether two methods are equal."
4636   (let ((m1 (cond ((null m1) gnus-select-method)
4637                   ((stringp m1) (gnus-server-to-method m1))
4638                   (t m1)))
4639         (m2 (cond ((null m2) gnus-select-method)
4640                   ((stringp m2) (gnus-server-to-method m2))
4641                   (t m2))))
4642     (gnus-method-equal m1 m2)))
4643
4644 (defun gnus-servers-using-backend (backend)
4645   "Return a list of known servers using BACKEND."
4646   (let ((opened gnus-opened-servers)
4647         out)
4648     (while opened
4649       (when (eq backend (caaar opened))
4650         (push (caar opened) out))
4651       (pop opened))
4652     out))
4653
4654 (defun gnus-group-prefixed-name (group method)
4655   "Return the whole name from GROUP and METHOD."
4656   (and (stringp method) (setq method (gnus-server-to-method method)))
4657   (concat (format "%s" (car method))
4658           (if (and
4659                (or (assoc (format "%s" (car method)) 
4660                           (gnus-methods-using 'address))
4661                    (gnus-server-equal method gnus-message-archive-method))
4662                (nth 1 method)
4663                (not (string= (nth 1 method) "")))
4664               (concat "+" (nth 1 method)))
4665           ":" group))
4666
4667 (defun gnus-group-real-prefix (group)
4668   "Return the prefix of the current group name."
4669   (if (string-match "^[^:]+:" group)
4670       (substring group 0 (match-end 0))
4671     ""))
4672
4673 (defun gnus-group-method (group)
4674   "Return the server or method used for selecting GROUP."
4675   (let ((prefix (gnus-group-real-prefix group)))
4676     (if (equal prefix "")
4677         gnus-select-method
4678       (let ((servers gnus-opened-servers)
4679             (server "")
4680             backend possible found)
4681         (if (string-match "^[^\\+]+\\+" prefix)
4682             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4683                   server (substring prefix (match-end 0) (1- (length prefix))))
4684           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4685         (while servers
4686           (when (eq (caaar servers) backend)
4687             (setq possible (caar servers))
4688             (when (equal (cadaar servers) server)
4689               (setq found (caar servers))))
4690           (pop servers))
4691         (or (car (rassoc found gnus-server-alist))
4692             found
4693             (car (rassoc possible gnus-server-alist))
4694             possible
4695             (list backend server))))))
4696
4697 (defsubst gnus-secondary-method-p (method)
4698   "Return whether METHOD is a secondary select method."
4699   (let ((methods gnus-secondary-select-methods)
4700         (gmethod (gnus-server-get-method nil method)))
4701     (while (and methods
4702                 (not (equal (gnus-server-get-method nil (car methods))
4703                             gmethod)))
4704       (setq methods (cdr methods)))
4705     methods))
4706
4707 (defun gnus-group-foreign-p (group)
4708   "Say whether a group is foreign or not."
4709   (and (not (gnus-group-native-p group))
4710        (not (gnus-group-secondary-p group))))
4711
4712 (defun gnus-group-native-p (group)
4713   "Say whether the group is native or not."
4714   (not (string-match ":" group)))
4715
4716 (defun gnus-group-secondary-p (group)
4717   "Say whether the group is secondary or not."
4718   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4719
4720 (defun gnus-group-get-parameter (group &optional symbol)
4721   "Returns the group parameters for GROUP.
4722 If SYMBOL, return the value of that symbol in the group parameters."
4723   (let ((params (gnus-info-params (gnus-get-info group))))
4724     (if symbol
4725         (gnus-group-parameter-value params symbol)
4726       params)))
4727
4728 (defun gnus-group-parameter-value (params symbol)
4729   "Return the value of SYMBOL in group PARAMS."
4730   (or (car (memq symbol params))        ; It's either a simple symbol
4731       (cdr (assq symbol params))))      ; or a cons.
4732
4733 (defun gnus-group-add-parameter (group param)
4734   "Add parameter PARAM to GROUP."
4735   (let ((info (gnus-get-info group)))
4736     (if (not info)
4737         () ; This is a dead group.  We just ignore it.
4738       ;; Cons the new param to the old one and update.
4739       (gnus-group-set-info (cons param (gnus-info-params info))
4740                            group 'params))))
4741
4742 (defun gnus-group-set-parameter (group name value)
4743   "Set parameter NAME to VALUE in GROUP."
4744   (let ((info (gnus-get-info group)))
4745     (if (not info)
4746         () ; This is a dead group.  We just ignore it.
4747       (let ((old-params (gnus-info-params info))
4748             (new-params (list (cons name value))))
4749         (while old-params
4750           (if (or (not (listp (car old-params)))
4751                   (not (eq (caar old-params) name)))
4752               (setq new-params (append new-params (list (car old-params)))))
4753           (setq old-params (cdr old-params)))
4754         (gnus-group-set-info new-params group 'params)))))
4755
4756 (defun gnus-group-add-score (group &optional score)
4757   "Add SCORE to the GROUP score.
4758 If SCORE is nil, add 1 to the score of GROUP."
4759   (let ((info (gnus-get-info group)))
4760     (when info
4761       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4762
4763 (defun gnus-summary-bubble-group ()
4764   "Increase the score of the current group.
4765 This is a handy function to add to `gnus-summary-exit-hook' to
4766 increase the score of each group you read."
4767   (gnus-group-add-score gnus-newsgroup-name))
4768
4769 (defun gnus-group-set-info (info &optional method-only-group part)
4770   (let* ((entry (gnus-gethash
4771                  (or method-only-group (gnus-info-group info))
4772                  gnus-newsrc-hashtb))
4773          (part-info info)
4774          (info (if method-only-group (nth 2 entry) info))
4775          method)
4776     (when method-only-group
4777       (unless entry
4778         (error "Trying to change non-existent group %s" method-only-group))
4779       ;; We have received parts of the actual group info - either the
4780       ;; select method or the group parameters.  We first check
4781       ;; whether we have to extend the info, and if so, do that.
4782       (let ((len (length info))
4783             (total (if (eq part 'method) 5 6)))
4784         (when (< len total)
4785           (setcdr (nthcdr (1- len) info)
4786                   (make-list (- total len) nil)))
4787         ;; Then we enter the new info.
4788         (setcar (nthcdr (1- total) info) part-info)))
4789     (unless entry
4790       ;; This is a new group, so we just create it.
4791       (save-excursion
4792         (set-buffer gnus-group-buffer)
4793         (setq method (gnus-info-method info))
4794         (when (gnus-server-equal method "native")
4795           (setq method nil))
4796         (save-excursion
4797           (set-buffer gnus-group-buffer)
4798           (if method
4799               ;; It's a foreign group...
4800               (gnus-group-make-group
4801                (gnus-group-real-name (gnus-info-group info))
4802                (if (stringp method) method
4803                  (prin1-to-string (car method)))
4804                (and (consp method)
4805                     (nth 1 (gnus-info-method info))))
4806             ;; It's a native group.
4807             (gnus-group-make-group (gnus-info-group info))))
4808         (gnus-message 6 "Note: New group created")
4809         (setq entry
4810               (gnus-gethash (gnus-group-prefixed-name
4811                              (gnus-group-real-name (gnus-info-group info))
4812                              (or (gnus-info-method info) gnus-select-method))
4813                             gnus-newsrc-hashtb))))
4814     ;; Whether it was a new group or not, we now have the entry, so we
4815     ;; can do the update.
4816     (if entry
4817         (progn
4818           (setcar (nthcdr 2 entry) info)
4819           (when (and (not (eq (car entry) t))
4820                      (gnus-active (gnus-info-group info)))
4821             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4822       (error "No such group: %s" (gnus-info-group info)))))
4823
4824 (defun gnus-group-set-method-info (group select-method)
4825   (gnus-group-set-info select-method group 'method))
4826
4827 (defun gnus-group-set-params-info (group params)
4828   (gnus-group-set-info params group 'params))
4829
4830 (defun gnus-group-update-group-line ()
4831   "Update the current line in the group buffer."
4832   (let* ((buffer-read-only nil)
4833          (group (gnus-group-group-name))
4834          (gnus-group-indentation (gnus-group-group-indentation))
4835          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4836     (and entry
4837          (not (gnus-ephemeral-group-p group))
4838          (gnus-dribble-enter
4839           (concat "(gnus-group-set-info '"
4840                   (prin1-to-string (nth 2 entry)) ")")))
4841     (gnus-delete-line)
4842     (gnus-group-insert-group-line-info group)
4843     (forward-line -1)
4844     (gnus-group-position-point)))
4845
4846 (defun gnus-group-insert-group-line-info (group)
4847   "Insert GROUP on the current line."
4848   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4849         active info)
4850     (setq gnus-group-indentation (gnus-group-group-indentation))
4851     (if entry
4852         (progn
4853           ;; (Un)subscribed group.
4854           (setq info (nth 2 entry))
4855           (gnus-group-insert-group-line
4856            group (gnus-info-level info) (gnus-info-marks info)
4857            (or (car entry) t) (gnus-info-method info)))
4858       ;; This group is dead.
4859       (gnus-group-insert-group-line
4860        group
4861        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4862        nil
4863        (if (setq active (gnus-active group))
4864            (- (1+ (cdr active)) (car active)) 0)
4865        nil))))
4866
4867 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4868                                                     gnus-tmp-marked number
4869                                                     gnus-tmp-method)
4870   "Insert a group line in the group buffer."
4871   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4872          (gnus-tmp-number-total
4873           (if gnus-tmp-active
4874               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4875             0))
4876          (gnus-tmp-number-of-unread
4877           (if (numberp number) (int-to-string (max 0 number))
4878             "*"))
4879          (gnus-tmp-number-of-read
4880           (if (numberp number)
4881               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4882             "*"))
4883          (gnus-tmp-subscribed
4884           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4885                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4886                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4887                 (t ?K)))
4888          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4889          (gnus-tmp-newsgroup-description
4890           (if gnus-description-hashtb
4891               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4892             ""))
4893          (gnus-tmp-moderated
4894           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4895          (gnus-tmp-moderated-string
4896           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4897          (gnus-tmp-method
4898           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4899          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4900          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4901          (gnus-tmp-news-method-string
4902           (if gnus-tmp-method
4903               (format "(%s:%s)" (car gnus-tmp-method)
4904                       (cadr gnus-tmp-method)) ""))
4905          (gnus-tmp-marked-mark
4906           (if (and (numberp number)
4907                    (zerop number)
4908                    (cdr (assq 'tick gnus-tmp-marked)))
4909               ?* ? ))
4910          (gnus-tmp-process-marked
4911           (if (member gnus-tmp-group gnus-group-marked)
4912               gnus-process-mark ? ))
4913          (gnus-tmp-grouplens
4914           (or (and gnus-use-grouplens
4915                    (bbb-grouplens-group-p gnus-tmp-group))
4916               ""))
4917          (buffer-read-only nil)
4918          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4919     (beginning-of-line)
4920     (add-text-properties
4921      (point)
4922      (prog1 (1+ (point))
4923        ;; Insert the text.
4924        (eval gnus-group-line-format-spec))
4925      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4926        gnus-unread ,(if (numberp number)
4927                         (string-to-int gnus-tmp-number-of-unread)
4928                       t)
4929        gnus-marked ,gnus-tmp-marked-mark
4930        gnus-indentation ,gnus-group-indentation
4931        gnus-level ,gnus-tmp-level))
4932     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4933       (forward-line -1)
4934       (run-hooks 'gnus-group-update-hook)
4935       (forward-line))
4936     ;; Allow XEmacs to remove front-sticky text properties.
4937     (gnus-group-remove-excess-properties)))
4938
4939 (defun gnus-group-update-group (group &optional visible-only)
4940   "Update all lines where GROUP appear.
4941 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4942 already."
4943   (save-excursion
4944     (set-buffer gnus-group-buffer)
4945     ;; The buffer may be narrowed.
4946     (save-restriction
4947       (widen)
4948       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4949             (loc (point-min))
4950             found buffer-read-only)
4951         ;; Enter the current status into the dribble buffer.
4952         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4953           (if (and entry (not (gnus-ephemeral-group-p group)))
4954               (gnus-dribble-enter
4955                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4956                        ")"))))
4957         ;; Find all group instances.  If topics are in use, each group
4958         ;; may be listed in more than once.
4959         (while (setq loc (text-property-any
4960                           loc (point-max) 'gnus-group ident))
4961           (setq found t)
4962           (goto-char loc)
4963           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4964             (gnus-delete-line)
4965             (gnus-group-insert-group-line-info group))
4966           (setq loc (1+ loc)))
4967         (unless (or found visible-only)
4968           ;; No such line in the buffer, find out where it's supposed to
4969           ;; go, and insert it there (or at the end of the buffer).
4970           (if gnus-goto-missing-group-function
4971               (funcall gnus-goto-missing-group-function group)
4972             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4973               (while (and entry (car entry)
4974                           (not
4975                            (gnus-goto-char
4976                             (text-property-any
4977                              (point-min) (point-max)
4978                              'gnus-group (gnus-intern-safe
4979                                           (caar entry) gnus-active-hashtb)))))
4980                 (setq entry (cdr entry)))
4981               (or entry (goto-char (point-max)))))
4982           ;; Finally insert the line.
4983           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4984             (gnus-group-insert-group-line-info group)))
4985         (gnus-group-set-mode-line)))))
4986
4987 (defun gnus-group-set-mode-line ()
4988   (when (memq 'group gnus-updated-mode-lines)
4989     (let* ((gformat (or gnus-group-mode-line-format-spec
4990                         (setq gnus-group-mode-line-format-spec
4991                               (gnus-parse-format
4992                                gnus-group-mode-line-format
4993                                gnus-group-mode-line-format-alist))))
4994            (gnus-tmp-news-server (cadr gnus-select-method))
4995            (gnus-tmp-news-method (car gnus-select-method))
4996            (max-len 60)
4997            gnus-tmp-header                      ;Dummy binding for user-defined formats
4998            ;; Get the resulting string.
4999            (mode-string (eval gformat)))
5000       ;; If the line is too long, we chop it off.
5001       (when (> (length mode-string) max-len)
5002         (setq mode-string (substring mode-string 0 (- max-len 4))))
5003       (prog1
5004           (setq mode-line-buffer-identification (list mode-string))
5005         (set-buffer-modified-p t)))))
5006
5007 (defun gnus-group-group-name ()
5008   "Get the name of the newsgroup on the current line."
5009   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5010     (and group (symbol-name group))))
5011
5012 (defun gnus-group-group-level ()
5013   "Get the level of the newsgroup on the current line."
5014   (get-text-property (gnus-point-at-bol) 'gnus-level))
5015
5016 (defun gnus-group-group-indentation ()
5017   "Get the indentation of the newsgroup on the current line."
5018   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5019       (and gnus-group-indentation-function
5020            (funcall gnus-group-indentation-function))
5021       ""))
5022
5023 (defun gnus-group-group-unread ()
5024   "Get the number of unread articles of the newsgroup on the current line."
5025   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5026
5027 (defun gnus-group-search-forward (&optional backward all level first-too)
5028   "Find the next newsgroup with unread articles.
5029 If BACKWARD is non-nil, find the previous newsgroup instead.
5030 If ALL is non-nil, just find any newsgroup.
5031 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5032 group exists.
5033 If FIRST-TOO, the current line is also eligible as a target."
5034   (let ((way (if backward -1 1))
5035         (low gnus-level-killed)
5036         (beg (point))
5037         pos found lev)
5038     (if (and backward (progn (beginning-of-line)) (bobp))
5039         nil
5040       (or first-too (forward-line way))
5041       (while (and
5042               (not (eobp))
5043               (not (setq
5044                     found
5045                     (and (or all
5046                              (and
5047                               (let ((unread
5048                                      (get-text-property (point) 'gnus-unread)))
5049                                 (and (numberp unread) (> unread 0)))
5050                               (setq lev (get-text-property (point)
5051                                                            'gnus-level))
5052                               (<= lev gnus-level-subscribed)))
5053                          (or (not level)
5054                              (and (setq lev (get-text-property (point)
5055                                                                'gnus-level))
5056                                   (or (= lev level)
5057                                       (and (< lev low)
5058                                            (< level lev)
5059                                            (progn
5060                                              (setq low lev)
5061                                              (setq pos (point))
5062                                              nil))))))))
5063               (zerop (forward-line way)))))
5064     (if found
5065         (progn (gnus-group-position-point) t)
5066       (goto-char (or pos beg))
5067       (and pos t))))
5068
5069 ;;; Gnus group mode commands
5070
5071 ;; Group marking.
5072
5073 (defun gnus-group-mark-group (n &optional unmark no-advance)
5074   "Mark the current group."
5075   (interactive "p")
5076   (let ((buffer-read-only nil)
5077         group)
5078     (while
5079         (and (> n 0)
5080              (setq group (gnus-group-group-name))
5081              (progn
5082                (beginning-of-line)
5083                (forward-char
5084                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5085                (delete-char 1)
5086                (if unmark
5087                    (progn
5088                      (insert " ")
5089                      (setq gnus-group-marked (delete group gnus-group-marked)))
5090                  (insert "#")
5091                  (setq gnus-group-marked
5092                        (cons group (delete group gnus-group-marked))))
5093                t)
5094              (or no-advance (zerop (gnus-group-next-group 1))))
5095       (setq n (1- n)))
5096     (gnus-summary-position-point)
5097     n))
5098
5099 (defun gnus-group-unmark-group (n)
5100   "Remove the mark from the current group."
5101   (interactive "p")
5102   (gnus-group-mark-group n 'unmark)
5103   (gnus-group-position-point))
5104
5105 (defun gnus-group-unmark-all-groups ()
5106   "Unmark all groups."
5107   (interactive)
5108   (let ((groups gnus-group-marked))
5109     (save-excursion
5110       (while groups
5111         (gnus-group-remove-mark (pop groups)))))
5112   (gnus-group-position-point))
5113
5114 (defun gnus-group-mark-region (unmark beg end)
5115   "Mark all groups between point and mark.
5116 If UNMARK, remove the mark instead."
5117   (interactive "P\nr")
5118   (let ((num (count-lines beg end)))
5119     (save-excursion
5120       (goto-char beg)
5121       (- num (gnus-group-mark-group num unmark)))))
5122
5123 (defun gnus-group-mark-buffer (&optional unmark)
5124   "Mark all groups in the buffer.
5125 If UNMARK, remove the mark instead."
5126   (interactive "P")
5127   (gnus-group-mark-region unmark (point-min) (point-max)))
5128
5129 (defun gnus-group-mark-regexp (regexp)
5130   "Mark all groups that match some regexp."
5131   (interactive "sMark (regexp): ")
5132   (let ((alist (cdr gnus-newsrc-alist))
5133         group)
5134     (while alist
5135       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5136         (gnus-group-set-mark group))))
5137   (gnus-group-position-point))
5138
5139 (defun gnus-group-remove-mark (group)
5140   "Remove the process mark from GROUP and move point there.
5141 Return nil if the group isn't displayed."
5142   (if (gnus-group-goto-group group)
5143       (save-excursion
5144         (gnus-group-mark-group 1 'unmark t)
5145         t)
5146     (setq gnus-group-marked
5147           (delete group gnus-group-marked))
5148     nil))
5149
5150 (defun gnus-group-set-mark (group)
5151   "Set the process mark on GROUP."
5152   (if (gnus-group-goto-group group) 
5153       (save-excursion
5154         (gnus-group-mark-group 1 nil t))
5155     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5156
5157 (defun gnus-group-universal-argument (arg &optional groups func)
5158   "Perform any command on all groups accoring to the process/prefix convention."
5159   (interactive "P")
5160   (let ((groups (or groups (gnus-group-process-prefix arg)))
5161         group func)
5162     (if (eq (setq func (or func
5163                            (key-binding
5164                             (read-key-sequence
5165                              (substitute-command-keys
5166                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5167             'undefined)
5168         (gnus-error 1 "Undefined key")
5169       (while groups
5170         (gnus-group-remove-mark (setq group (pop groups)))
5171         (command-execute func))))
5172   (gnus-group-position-point))
5173
5174 (defun gnus-group-process-prefix (n)
5175   "Return a list of groups to work on.
5176 Take into consideration N (the prefix) and the list of marked groups."
5177   (cond
5178    (n
5179     (setq n (prefix-numeric-value n))
5180     ;; There is a prefix, so we return a list of the N next
5181     ;; groups.
5182     (let ((way (if (< n 0) -1 1))
5183           (n (abs n))
5184           group groups)
5185       (save-excursion
5186         (while (and (> n 0)
5187                     (setq group (gnus-group-group-name)))
5188           (setq groups (cons group groups))
5189           (setq n (1- n))
5190           (gnus-group-next-group way)))
5191       (nreverse groups)))
5192    ((and (boundp 'transient-mark-mode)
5193          transient-mark-mode
5194          (boundp 'mark-active)
5195          mark-active)
5196     ;; Work on the region between point and mark.
5197     (let ((max (max (point) (mark)))
5198           groups)
5199       (save-excursion
5200         (goto-char (min (point) (mark)))
5201         (while
5202             (and
5203              (push (gnus-group-group-name) groups)
5204              (zerop (gnus-group-next-group 1))
5205              (< (point) max)))
5206         (nreverse groups))))
5207    (gnus-group-marked
5208     ;; No prefix, but a list of marked articles.
5209     (reverse gnus-group-marked))
5210    (t
5211     ;; Neither marked articles or a prefix, so we return the
5212     ;; current group.
5213     (let ((group (gnus-group-group-name)))
5214       (and group (list group))))))
5215
5216 ;; Selecting groups.
5217
5218 (defun gnus-group-read-group (&optional all no-article group)
5219   "Read news in this newsgroup.
5220 If the prefix argument ALL is non-nil, already read articles become
5221 readable.  IF ALL is a number, fetch this number of articles.  If the
5222 optional argument NO-ARTICLE is non-nil, no article will be
5223 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5224 group."
5225   (interactive "P")
5226   (let ((group (or group (gnus-group-group-name)))
5227         number active marked entry)
5228     (or group (error "No group on current line"))
5229     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5230                                             group gnus-newsrc-hashtb)))))
5231     ;; This group might be a dead group.  In that case we have to get
5232     ;; the number of unread articles from `gnus-active-hashtb'.
5233     (setq number
5234           (cond ((numberp all) all)
5235                 (entry (car entry))
5236                 ((setq active (gnus-active group))
5237                  (- (1+ (cdr active)) (car active)))))
5238     (gnus-summary-read-group
5239      group (or all (and (numberp number)
5240                         (zerop (+ number (length (cdr (assq 'tick marked)))
5241                                   (length (cdr (assq 'dormant marked)))))))
5242      no-article)))
5243
5244 (defun gnus-group-select-group (&optional all)
5245   "Select this newsgroup.
5246 No article is selected automatically.
5247 If ALL is non-nil, already read articles become readable.
5248 If ALL is a number, fetch this number of articles."
5249   (interactive "P")
5250   (gnus-group-read-group all t))
5251
5252 (defun gnus-group-quick-select-group (&optional all)
5253   "Select the current group \"quickly\".
5254 This means that no highlighting or scoring will be performed."
5255   (interactive "P")
5256   (let (gnus-visual
5257         gnus-score-find-score-files-function
5258         gnus-apply-kill-hook
5259         gnus-summary-expunge-below)
5260     (gnus-group-read-group all t)))
5261
5262 (defun gnus-group-visible-select-group (&optional all)
5263   "Select the current group without hiding any articles."
5264   (interactive "P")
5265   (let ((gnus-inhibit-limiting t))
5266     (gnus-group-read-group all t)))
5267
5268 ;;;###autoload
5269 (defun gnus-fetch-group (group)
5270   "Start Gnus if necessary and enter GROUP.
5271 Returns whether the fetching was successful or not."
5272   (interactive "sGroup name: ")
5273   (or (get-buffer gnus-group-buffer)
5274       (gnus))
5275   (gnus-group-read-group nil nil group))
5276
5277 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5278 ;; if selection was successful.
5279 (defun gnus-group-read-ephemeral-group
5280   (group method &optional activate quit-config)
5281   (let ((group (if (gnus-group-foreign-p group) group
5282                  (gnus-group-prefixed-name group method))))
5283     (gnus-sethash
5284      group
5285      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5286                      ((quit-config . ,(if quit-config quit-config
5287                                         (cons (current-buffer) 'summary))))))
5288      gnus-newsrc-hashtb)
5289     (set-buffer gnus-group-buffer)
5290     (or (gnus-check-server method)
5291         (error "Unable to contact server: %s" (gnus-status-message method)))
5292     (if activate (or (gnus-request-group group)
5293                      (error "Couldn't request group")))
5294     (condition-case ()
5295         (gnus-group-read-group t t group)
5296       (error nil)
5297       (quit nil))))
5298
5299 (defun gnus-group-jump-to-group (group)
5300   "Jump to newsgroup GROUP."
5301   (interactive
5302    (list (completing-read
5303           "Group: " gnus-active-hashtb nil
5304           (memq gnus-select-method gnus-have-read-active-file)
5305           nil
5306           'gnus-group-history)))
5307
5308   (when (equal group "")
5309     (error "Empty group name"))
5310
5311   (when (string-match "[\000-\032]" group)
5312     (error "Control characters in group: %s" group))
5313
5314   (let ((b (text-property-any
5315             (point-min) (point-max)
5316             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5317     (unless (gnus-ephemeral-group-p group)
5318       (if b
5319           ;; Either go to the line in the group buffer...
5320           (goto-char b)
5321         ;; ... or insert the line.
5322         (or
5323          (gnus-active group)
5324          (gnus-activate-group group)
5325          (error "%s error: %s" group (gnus-status-message group)))
5326
5327         (gnus-group-update-group group)
5328         (goto-char (text-property-any
5329                     (point-min) (point-max)
5330                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5331     ;; Adjust cursor point.
5332     (gnus-group-position-point)))
5333
5334 (defun gnus-group-goto-group (group)
5335   "Goto to newsgroup GROUP."
5336   (when group
5337     (let ((b (text-property-any (point-min) (point-max)
5338                                 'gnus-group (gnus-intern-safe
5339                                              group gnus-active-hashtb))))
5340       (and b (goto-char b)))))
5341
5342 (defun gnus-group-next-group (n)
5343   "Go to next N'th newsgroup.
5344 If N is negative, search backward instead.
5345 Returns the difference between N and the number of skips actually
5346 done."
5347   (interactive "p")
5348   (gnus-group-next-unread-group n t))
5349
5350 (defun gnus-group-next-unread-group (n &optional all level)
5351   "Go to next N'th unread newsgroup.
5352 If N is negative, search backward instead.
5353 If ALL is non-nil, choose any newsgroup, unread or not.
5354 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5355 such group can be found, the next group with a level higher than
5356 LEVEL.
5357 Returns the difference between N and the number of skips actually
5358 made."
5359   (interactive "p")
5360   (let ((backward (< n 0))
5361         (n (abs n)))
5362     (while (and (> n 0)
5363                 (gnus-group-search-forward
5364                  backward (or (not gnus-group-goto-unread) all) level))
5365       (setq n (1- n)))
5366     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5367                                (if level " on this level or higher" "")))
5368     n))
5369
5370 (defun gnus-group-prev-group (n)
5371   "Go to previous N'th newsgroup.
5372 Returns the difference between N and the number of skips actually
5373 done."
5374   (interactive "p")
5375   (gnus-group-next-unread-group (- n) t))
5376
5377 (defun gnus-group-prev-unread-group (n)
5378   "Go to previous N'th unread newsgroup.
5379 Returns the difference between N and the number of skips actually
5380 done."
5381   (interactive "p")
5382   (gnus-group-next-unread-group (- n)))
5383
5384 (defun gnus-group-next-unread-group-same-level (n)
5385   "Go to next N'th unread newsgroup on the same level.
5386 If N is negative, search backward instead.
5387 Returns the difference between N and the number of skips actually
5388 done."
5389   (interactive "p")
5390   (gnus-group-next-unread-group n t (gnus-group-group-level))
5391   (gnus-group-position-point))
5392
5393 (defun gnus-group-prev-unread-group-same-level (n)
5394   "Go to next N'th unread newsgroup on the same level.
5395 Returns the difference between N and the number of skips actually
5396 done."
5397   (interactive "p")
5398   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5399   (gnus-group-position-point))
5400
5401 (defun gnus-group-best-unread-group (&optional exclude-group)
5402   "Go to the group with the highest level.
5403 If EXCLUDE-GROUP, do not go to that group."
5404   (interactive)
5405   (goto-char (point-min))
5406   (let ((best 100000)
5407         unread best-point)
5408     (while (not (eobp))
5409       (setq unread (get-text-property (point) 'gnus-unread))
5410       (if (and (numberp unread) (> unread 0))
5411           (progn
5412             (if (and (get-text-property (point) 'gnus-level)
5413                      (< (get-text-property (point) 'gnus-level) best)
5414                      (or (not exclude-group)
5415                          (not (equal exclude-group (gnus-group-group-name)))))
5416                 (progn
5417                   (setq best (get-text-property (point) 'gnus-level))
5418                   (setq best-point (point))))))
5419       (forward-line 1))
5420     (if best-point (goto-char best-point))
5421     (gnus-summary-position-point)
5422     (and best-point (gnus-group-group-name))))
5423
5424 (defun gnus-group-first-unread-group ()
5425   "Go to the first group with unread articles."
5426   (interactive)
5427   (prog1
5428       (let ((opoint (point))
5429             unread)
5430         (goto-char (point-min))
5431         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5432                 (and (numberp unread)   ; Not a topic.
5433                      (not (zerop unread))) ; Has unread articles.
5434                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5435             (point)                     ; Success.
5436           (goto-char opoint)
5437           nil))                         ; Not success.
5438     (gnus-group-position-point)))
5439
5440 (defun gnus-group-enter-server-mode ()
5441   "Jump to the server buffer."
5442   (interactive)
5443   (gnus-enter-server-buffer))
5444
5445 (defun gnus-group-make-group (name &optional method address)
5446   "Add a new newsgroup.
5447 The user will be prompted for a NAME, for a select METHOD, and an
5448 ADDRESS."
5449   (interactive
5450    (cons
5451     (read-string "Group name: ")
5452     (let ((method
5453            (completing-read
5454             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5455             nil t nil 'gnus-method-history)))
5456       (cond ((assoc method gnus-valid-select-methods)
5457              (list method
5458                    (if (memq 'prompt-address
5459                              (assoc method gnus-valid-select-methods))
5460                        (read-string "Address: ")
5461                      "")))
5462             ((assoc method gnus-server-alist)
5463              (list method))
5464             (t
5465              (list method ""))))))
5466
5467   (let* ((meth (and method (if address (list (intern method) address)
5468                              method)))
5469          (nname (if method (gnus-group-prefixed-name name meth) name))
5470          backend info)
5471     (when (gnus-gethash nname gnus-newsrc-hashtb)
5472       (error "Group %s already exists" nname))
5473     ;; Subscribe to the new group.
5474     (gnus-group-change-level
5475      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5476      gnus-level-default-subscribed gnus-level-killed
5477      (and (gnus-group-group-name)
5478           (gnus-gethash (gnus-group-group-name)
5479                         gnus-newsrc-hashtb))
5480      t)
5481     ;; Make it active.
5482     (gnus-set-active nname (cons 1 0))
5483     (or (gnus-ephemeral-group-p name)
5484         (gnus-dribble-enter
5485          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5486     ;; Insert the line.
5487     (gnus-group-insert-group-line-info nname)
5488     (forward-line -1)
5489     (gnus-group-position-point)
5490
5491     ;; Load the backend and try to make the backend create
5492     ;; the group as well.
5493     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5494                                                   nil meth))))
5495                  gnus-valid-select-methods)
5496       (require backend))
5497     (gnus-check-server meth)
5498     (and (gnus-check-backend-function 'request-create-group nname)
5499          (gnus-request-create-group nname))
5500     t))
5501
5502 (defun gnus-group-delete-group (group &optional force)
5503   "Delete the current group.
5504 If FORCE (the prefix) is non-nil, all the articles in the group will
5505 be deleted.  This is \"deleted\" as in \"removed forever from the face
5506 of the Earth\".  There is no undo."
5507   (interactive
5508    (list (gnus-group-group-name)
5509          current-prefix-arg))
5510   (or group (error "No group to rename"))
5511   (or (gnus-check-backend-function 'request-delete-group group)
5512       (error "This backend does not support group deletion"))
5513   (prog1
5514       (if (not (gnus-yes-or-no-p
5515                 (format
5516                  "Do you really want to delete %s%s? "
5517                  group (if force " and all its contents" ""))))
5518           () ; Whew!
5519         (gnus-message 6 "Deleting group %s..." group)
5520         (if (not (gnus-request-delete-group group force))
5521             (gnus-error 3 "Couldn't delete group %s" group)
5522           (gnus-message 6 "Deleting group %s...done" group)
5523           (gnus-group-goto-group group)
5524           (gnus-group-kill-group 1 t)
5525           (gnus-sethash group nil gnus-active-hashtb)
5526           t))
5527     (gnus-group-position-point)))
5528
5529 (defun gnus-group-rename-group (group new-name)
5530   (interactive
5531    (list
5532     (gnus-group-group-name)
5533     (progn
5534       (or (gnus-check-backend-function
5535            'request-rename-group (gnus-group-group-name))
5536           (error "This backend does not support renaming groups"))
5537       (read-string "New group name: "))))
5538
5539   (or (gnus-check-backend-function 'request-rename-group group)
5540       (error "This backend does not support renaming groups"))
5541
5542   (or group (error "No group to rename"))
5543   (and (string-match "^[ \t]*$" new-name)
5544        (error "Not a valid group name"))
5545
5546   ;; We find the proper prefixed name.
5547   (setq new-name
5548         (gnus-group-prefixed-name
5549          (gnus-group-real-name new-name)
5550          (gnus-info-method (gnus-get-info group))))
5551
5552   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5553   (prog1
5554       (if (not (gnus-request-rename-group group new-name))
5555           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5556         ;; We rename the group internally by killing it...
5557         (gnus-group-goto-group group)
5558         (gnus-group-kill-group)
5559         ;; ... changing its name ...
5560         (setcar (cdar gnus-list-of-killed-groups) new-name)
5561         ;; ... and then yanking it.  Magic!
5562         (gnus-group-yank-group)
5563         (gnus-set-active new-name (gnus-active group))
5564         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5565         new-name)
5566     (gnus-group-position-point)))
5567
5568 (defun gnus-group-edit-group (group &optional part)
5569   "Edit the group on the current line."
5570   (interactive (list (gnus-group-group-name)))
5571   (let* ((part (or part 'info))
5572          (done-func `(lambda ()
5573                        "Exit editing mode and update the information."
5574                        (interactive)
5575                        (gnus-group-edit-group-done ',part ,group)))
5576          (winconf (current-window-configuration))
5577          info)
5578     (or group (error "No group on current line"))
5579     (or (setq info (gnus-get-info group))
5580         (error "Killed group; can't be edited"))
5581     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5582     (gnus-configure-windows 'edit-group)
5583     (gnus-add-current-to-buffer-list)
5584     (emacs-lisp-mode)
5585     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5586     (use-local-map (copy-keymap emacs-lisp-mode-map))
5587     (local-set-key "\C-c\C-c" done-func)
5588     (make-local-variable 'gnus-prev-winconf)
5589     (setq gnus-prev-winconf winconf)
5590     (erase-buffer)
5591     (insert
5592      (cond
5593       ((eq part 'method)
5594        ";; Type `C-c C-c' after editing the select method.\n\n")
5595       ((eq part 'params)
5596        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5597       ((eq part 'info)
5598        ";; Type `C-c C-c' after editing the group info.\n\n")))
5599     (insert
5600      (pp-to-string
5601       (cond ((eq part 'method)
5602              (or (gnus-info-method info) "native"))
5603             ((eq part 'params)
5604              (gnus-info-params info))
5605             (t info)))
5606      "\n")))
5607
5608 (defun gnus-group-edit-group-method (group)
5609   "Edit the select method of GROUP."
5610   (interactive (list (gnus-group-group-name)))
5611   (gnus-group-edit-group group 'method))
5612
5613 (defun gnus-group-edit-group-parameters (group)
5614   "Edit the group parameters of GROUP."
5615   (interactive (list (gnus-group-group-name)))
5616   (gnus-group-edit-group group 'params))
5617
5618 (defun gnus-group-edit-group-done (part group)
5619   "Get info from buffer, update variables and jump to the group buffer."
5620   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5621   (goto-char (point-min))
5622   (let* ((form (read (current-buffer)))
5623          (winconf gnus-prev-winconf)
5624          (method (cond ((eq part 'info) (nth 4 form))
5625                        ((eq part 'method) form)
5626                        (t nil)))
5627          (info (cond ((eq part 'info) form)
5628                      ((eq part 'method) (gnus-get-info group))
5629                      (t nil)))
5630          (new-group (if info
5631                       (if (or (not method)
5632                               (gnus-server-equal
5633                                gnus-select-method method))
5634                           (gnus-group-real-name (car info))
5635                         (gnus-group-prefixed-name
5636                          (gnus-group-real-name (car info)) method))
5637                       nil)))
5638     (when (and new-group
5639                (not (equal new-group group)))
5640       (when (gnus-group-goto-group group)
5641         (gnus-group-kill-group 1))
5642       (gnus-activate-group new-group))
5643     ;; Set the info.
5644     (if (and info new-group)
5645         (progn
5646           (setq info (gnus-copy-sequence info))
5647           (setcar info new-group)
5648           (unless (gnus-server-equal method "native")
5649             (unless (nthcdr 3 info)
5650               (nconc info (list nil nil)))
5651             (unless (nthcdr 4 info)
5652               (nconc info (list nil)))
5653             (gnus-info-set-method info method))
5654           (gnus-group-set-info info))
5655       (gnus-group-set-info form (or new-group group) part))
5656     (kill-buffer (current-buffer))
5657     (and winconf (set-window-configuration winconf))
5658     (set-buffer gnus-group-buffer)
5659     (gnus-group-update-group (or new-group group))
5660     (gnus-group-position-point)))
5661
5662 (defun gnus-group-make-help-group ()
5663   "Create the Gnus documentation group."
5664   (interactive)
5665   (let ((path load-path)
5666         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5667         file dir)
5668     (and (gnus-gethash name gnus-newsrc-hashtb)
5669          (error "Documentation group already exists"))
5670     (while path
5671       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5672             file nil)
5673       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5674                 (file-exists-p
5675                  (setq file (concat (file-name-directory
5676                                      (directory-file-name dir))
5677                                     "etc/gnus-tut.txt"))))
5678         (setq path nil)))
5679     (if (not file)
5680         (gnus-message 1 "Couldn't find doc group")
5681       (gnus-group-make-group
5682        (gnus-group-real-name name)
5683        (list 'nndoc "gnus-help"
5684              (list 'nndoc-address file)
5685              (list 'nndoc-article-type 'mbox)))))
5686   (gnus-group-position-point))
5687
5688 (defun gnus-group-make-doc-group (file type)
5689   "Create a group that uses a single file as the source."
5690   (interactive
5691    (list (read-file-name "File name: ")
5692          (and current-prefix-arg 'ask)))
5693   (when (eq type 'ask)
5694     (let ((err "")
5695           char found)
5696       (while (not found)
5697         (message
5698          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5699          err)
5700         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5701                           ((= char ?b) 'babyl)
5702                           ((= char ?d) 'digest)
5703                           ((= char ?f) 'forward)
5704                           ((= char ?a) 'mmfd)
5705                           (t (setq err (format "%c unknown. " char))
5706                              nil))))
5707       (setq type found)))
5708   (let* ((file (expand-file-name file))
5709          (name (gnus-generate-new-group-name
5710                 (gnus-group-prefixed-name
5711                  (file-name-nondirectory file) '(nndoc "")))))
5712     (gnus-group-make-group
5713      (gnus-group-real-name name)
5714      (list 'nndoc (file-name-nondirectory file)
5715            (list 'nndoc-address file)
5716            (list 'nndoc-article-type (or type 'guess))))))
5717
5718 (defun gnus-group-make-archive-group (&optional all)
5719   "Create the (ding) Gnus archive group of the most recent articles.
5720 Given a prefix, create a full group."
5721   (interactive "P")
5722   (let ((group (gnus-group-prefixed-name
5723                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5724     (and (gnus-gethash group gnus-newsrc-hashtb)
5725          (error "Archive group already exists"))
5726     (gnus-group-make-group
5727      (gnus-group-real-name group)
5728      (list 'nndir (if all "hpc" "edu")
5729            (list 'nndir-directory
5730                  (if all gnus-group-archive-directory
5731                    gnus-group-recent-archive-directory))))))
5732
5733 (defun gnus-group-make-directory-group (dir)
5734   "Create an nndir group.
5735 The user will be prompted for a directory.  The contents of this
5736 directory will be used as a newsgroup.  The directory should contain
5737 mail messages or news articles in files that have numeric names."
5738   (interactive
5739    (list (read-file-name "Create group from directory: ")))
5740   (or (file-exists-p dir) (error "No such directory"))
5741   (or (file-directory-p dir) (error "Not a directory"))
5742   (let ((ext "")
5743         (i 0)
5744         group)
5745     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5746       (setq group
5747             (gnus-group-prefixed-name
5748              (concat (file-name-as-directory (directory-file-name dir))
5749                      ext)
5750              '(nndir "")))
5751       (setq ext (format "<%d>" (setq i (1+ i)))))
5752     (gnus-group-make-group
5753      (gnus-group-real-name group)
5754      (list 'nndir group (list 'nndir-directory dir)))))
5755
5756 (defun gnus-group-make-kiboze-group (group address scores)
5757   "Create an nnkiboze group.
5758 The user will be prompted for a name, a regexp to match groups, and
5759 score file entries for articles to include in the group."
5760   (interactive
5761    (list
5762     (read-string "nnkiboze group name: ")
5763     (read-string "Source groups (regexp): ")
5764     (let ((headers (mapcar (lambda (group) (list group))
5765                            '("subject" "from" "number" "date" "message-id"
5766                              "references" "chars" "lines" "xref"
5767                              "followup" "all" "body" "head")))
5768           scores header regexp regexps)
5769       (while (not (equal "" (setq header (completing-read
5770                                           "Match on header: " headers nil t))))
5771         (setq regexps nil)
5772         (while (not (equal "" (setq regexp (read-string
5773                                             (format "Match on %s (string): "
5774                                                     header)))))
5775           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5776         (setq scores (cons (cons header regexps) scores)))
5777       scores)))
5778   (gnus-group-make-group group "nnkiboze" address)
5779   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5780     (let (emacs-lisp-mode-hook)
5781       (pp scores (current-buffer)))))
5782
5783 (defun gnus-group-add-to-virtual (n vgroup)
5784   "Add the current group to a virtual group."
5785   (interactive
5786    (list current-prefix-arg
5787          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5788                           "nnvirtual:")))
5789   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5790       (error "%s is not an nnvirtual group" vgroup))
5791   (let* ((groups (gnus-group-process-prefix n))
5792          (method (gnus-info-method (gnus-get-info vgroup))))
5793     (setcar (cdr method)
5794             (concat
5795              (nth 1 method) "\\|"
5796              (mapconcat
5797               (lambda (s)
5798                 (gnus-group-remove-mark s)
5799                 (concat "\\(^" (regexp-quote s) "$\\)"))
5800               groups "\\|"))))
5801   (gnus-group-position-point))
5802
5803 (defun gnus-group-make-empty-virtual (group)
5804   "Create a new, fresh, empty virtual group."
5805   (interactive "sCreate new, empty virtual group: ")
5806   (let* ((method (list 'nnvirtual "^$"))
5807          (pgroup (gnus-group-prefixed-name group method)))
5808     ;; Check whether it exists already.
5809     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5810          (error "Group %s already exists." pgroup))
5811     ;; Subscribe the new group after the group on the current line.
5812     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5813     (gnus-group-update-group pgroup)
5814     (forward-line -1)
5815     (gnus-group-position-point)))
5816
5817 (defun gnus-group-enter-directory (dir)
5818   "Enter an ephemeral nneething group."
5819   (interactive "DDirectory to read: ")
5820   (let* ((method (list 'nneething dir))
5821          (leaf (gnus-group-prefixed-name
5822                 (file-name-nondirectory (directory-file-name dir))
5823                 method))
5824          (name (gnus-generate-new-group-name leaf)))
5825     (let ((nneething-read-only t))
5826       (or (gnus-group-read-ephemeral-group
5827            name method t
5828            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5829                                       'summary 'group)))
5830           (error "Couldn't enter %s" dir)))))
5831
5832 ;; Group sorting commands
5833 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5834
5835 (defun gnus-group-sort-groups (func &optional reverse)
5836   "Sort the group buffer according to FUNC.
5837 If REVERSE, reverse the sorting order."
5838   (interactive (list gnus-group-sort-function
5839                      current-prefix-arg))
5840   (let ((func (cond 
5841                ((not (listp func)) func)
5842                ((null func) func)
5843                ((= 1 (length func)) (car func))
5844                (t `(lambda (t1 t2)
5845                      ,(gnus-make-sort-function 
5846                        (reverse func)))))))
5847     ;; We peel off the dummy group from the alist.
5848     (when func
5849       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5850         (pop gnus-newsrc-alist))
5851       ;; Do the sorting.
5852       (setq gnus-newsrc-alist
5853             (sort gnus-newsrc-alist func))
5854       (when reverse
5855         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5856       ;; Regenerate the hash table.
5857       (gnus-make-hashtable-from-newsrc-alist)
5858       (gnus-group-list-groups))))
5859
5860 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5861   "Sort the group buffer alphabetically by group name.
5862 If REVERSE, sort in reverse order."
5863   (interactive "P")
5864   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5865
5866 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5867   "Sort the group buffer by number of unread articles.
5868 If REVERSE, sort in reverse order."
5869   (interactive "P")
5870   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5871
5872 (defun gnus-group-sort-groups-by-level (&optional reverse)
5873   "Sort the group buffer by group level.
5874 If REVERSE, sort in reverse order."
5875   (interactive "P")
5876   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5877
5878 (defun gnus-group-sort-groups-by-score (&optional reverse)
5879   "Sort the group buffer by group score.
5880 If REVERSE, sort in reverse order."
5881   (interactive "P")
5882   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5883
5884 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5885   "Sort the group buffer by group rank.
5886 If REVERSE, sort in reverse order."
5887   (interactive "P")
5888   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5889
5890 (defun gnus-group-sort-groups-by-method (&optional reverse)
5891   "Sort the group buffer alphabetically by backend name.
5892 If REVERSE, sort in reverse order."
5893   (interactive "P")
5894   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5895
5896 (defun gnus-group-sort-by-alphabet (info1 info2)
5897   "Sort alphabetically."
5898   (string< (gnus-info-group info1) (gnus-info-group info2)))
5899
5900 (defun gnus-group-sort-by-unread (info1 info2)
5901   "Sort by number of unread articles."
5902   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5903         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5904     (< (or (and (numberp n1) n1) 0)
5905        (or (and (numberp n2) n2) 0))))
5906
5907 (defun gnus-group-sort-by-level (info1 info2)
5908   "Sort by level."
5909   (< (gnus-info-level info1) (gnus-info-level info2)))
5910
5911 (defun gnus-group-sort-by-method (info1 info2)
5912   "Sort alphabetically by backend name."
5913   (string< (symbol-name (car (gnus-find-method-for-group
5914                               (gnus-info-group info1) info1)))
5915            (symbol-name (car (gnus-find-method-for-group
5916                               (gnus-info-group info2) info2)))))
5917
5918 (defun gnus-group-sort-by-score (info1 info2)
5919   "Sort by group score."
5920   (< (gnus-info-score info1) (gnus-info-score info2)))
5921
5922 (defun gnus-group-sort-by-rank (info1 info2)
5923   "Sort by level and score."
5924   (let ((level1 (gnus-info-level info1))
5925         (level2 (gnus-info-level info2)))
5926     (or (< level1 level2)
5927         (and (= level1 level2)
5928              (> (gnus-info-score info1) (gnus-info-score info2))))))
5929
5930 ;; Group catching up.
5931
5932 (defun gnus-group-clear-data (n)
5933   "Clear all marks and read ranges from the current group."
5934   (interactive "P")
5935   (let ((groups (gnus-group-process-prefix n))
5936         group info)
5937     (while (setq group (pop groups))
5938       (setq info (gnus-get-info group))
5939       (gnus-info-set-read info nil)
5940       (when (gnus-info-marks info)
5941         (gnus-info-set-marks info nil))
5942       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5943       (when (gnus-group-goto-group group)
5944         (gnus-group-remove-mark group)
5945         (gnus-group-update-group-line)))))
5946
5947 (defun gnus-group-catchup-current (&optional n all)
5948   "Mark all articles not marked as unread in current newsgroup as read.
5949 If prefix argument N is numeric, the ARG next newsgroups will be
5950 caught up.  If ALL is non-nil, marked articles will also be marked as
5951 read.  Cross references (Xref: header) of articles are ignored.
5952 The difference between N and actual number of newsgroups that were
5953 caught up is returned."
5954   (interactive "P")
5955   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5956                gnus-expert-user
5957                (gnus-y-or-n-p
5958                 (if all
5959                     "Do you really want to mark all articles as read? "
5960                   "Mark all unread articles as read? "))))
5961       n
5962     (let ((groups (gnus-group-process-prefix n))
5963           (ret 0))
5964       (while groups
5965         ;; Virtual groups have to be given special treatment.
5966         (let ((method (gnus-find-method-for-group (car groups))))
5967           (if (eq 'nnvirtual (car method))
5968               (nnvirtual-catchup-group
5969                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5970         (gnus-group-remove-mark (car groups))
5971         (if (>= (gnus-group-group-level) gnus-level-zombie)
5972             (gnus-message 2 "Dead groups can't be caught up")
5973           (if (prog1
5974                   (gnus-group-goto-group (car groups))
5975                 (gnus-group-catchup (car groups) all))
5976               (gnus-group-update-group-line)
5977             (setq ret (1+ ret))))
5978         (setq groups (cdr groups)))
5979       (gnus-group-next-unread-group 1)
5980       ret)))
5981
5982 (defun gnus-group-catchup-current-all (&optional n)
5983   "Mark all articles in current newsgroup as read.
5984 Cross references (Xref: header) of articles are ignored."
5985   (interactive "P")
5986   (gnus-group-catchup-current n 'all))
5987
5988 (defun gnus-group-catchup (group &optional all)
5989   "Mark all articles in GROUP as read.
5990 If ALL is non-nil, all articles are marked as read.
5991 The return value is the number of articles that were marked as read,
5992 or nil if no action could be taken."
5993   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5994          (num (car entry)))
5995     ;; Do the updating only if the newsgroup isn't killed.
5996     (if (not (numberp (car entry)))
5997         (gnus-message 1 "Can't catch up; non-active group")
5998       ;; Do auto-expirable marks if that's required.
5999       (when (gnus-group-auto-expirable-p group)
6000         (gnus-add-marked-articles
6001          group 'expire (gnus-list-of-unread-articles group))
6002         (when all
6003           (let ((marks (nth 3 (nth 2 entry))))
6004             (gnus-add-marked-articles
6005              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6006             (gnus-add-marked-articles
6007              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6008       (when entry
6009         (gnus-update-read-articles group nil)
6010         ;; Also nix out the lists of marks and dormants.
6011         (when all
6012           (gnus-add-marked-articles group 'tick nil nil 'force)
6013           (gnus-add-marked-articles group 'dormant nil nil 'force))
6014         (run-hooks 'gnus-group-catchup-group-hook)
6015         num))))
6016
6017 (defun gnus-group-expire-articles (&optional n)
6018   "Expire all expirable articles in the current newsgroup."
6019   (interactive "P")
6020   (let ((groups (gnus-group-process-prefix n))
6021         group)
6022     (unless groups
6023       (error "No groups to expire"))
6024     (while (setq group (pop groups))
6025       (gnus-group-remove-mark group)
6026       (when (gnus-check-backend-function 'request-expire-articles group)
6027         (gnus-message 6 "Expiring articles in %s..." group)
6028         (let* ((info (gnus-get-info group))
6029                (expirable (if (gnus-group-total-expirable-p group)
6030                               (cons nil (gnus-list-of-read-articles group))
6031                             (assq 'expire (gnus-info-marks info))))
6032                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6033           (when expirable
6034             (setcdr
6035              expirable
6036              (gnus-compress-sequence
6037               (if expiry-wait
6038                   ;; We set the expiry variables to the groupp
6039                   ;; parameter. 
6040                   (let ((nnmail-expiry-wait-function nil)
6041                         (nnmail-expiry-wait expiry-wait))
6042                     (gnus-request-expire-articles
6043                      (gnus-uncompress-sequence (cdr expirable)) group))
6044                 ;; Just expire using the normal expiry values.
6045                 (gnus-request-expire-articles
6046                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6047           (gnus-message 6 "Expiring articles in %s...done" group)))
6048       (gnus-group-position-point))))
6049
6050 (defun gnus-group-expire-all-groups ()
6051   "Expire all expirable articles in all newsgroups."
6052   (interactive)
6053   (save-excursion
6054     (gnus-message 5 "Expiring...")
6055     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6056                                      (cdr gnus-newsrc-alist))))
6057       (gnus-group-expire-articles nil)))
6058   (gnus-group-position-point)
6059   (gnus-message 5 "Expiring...done"))
6060
6061 (defun gnus-group-set-current-level (n level)
6062   "Set the level of the next N groups to LEVEL."
6063   (interactive
6064    (list
6065     current-prefix-arg
6066     (string-to-int
6067      (let ((s (read-string
6068                (format "Level (default %s): "
6069                        (or (gnus-group-group-level) 
6070                            gnus-level-default-subscribed)))))
6071        (if (string-match "^\\s-*$" s)
6072            (int-to-string (or (gnus-group-group-level) 
6073                               gnus-level-default-subscribed))
6074          s)))))
6075   (or (and (>= level 1) (<= level gnus-level-killed))
6076       (error "Illegal level: %d" level))
6077   (let ((groups (gnus-group-process-prefix n))
6078         group)
6079     (while (setq group (pop groups))
6080       (gnus-group-remove-mark group)
6081       (gnus-message 6 "Changed level of %s from %d to %d"
6082                     group (or (gnus-group-group-level) gnus-level-killed)
6083                     level)
6084       (gnus-group-change-level
6085        group level (or (gnus-group-group-level) gnus-level-killed))
6086       (gnus-group-update-group-line)))
6087   (gnus-group-position-point))
6088
6089 (defun gnus-group-unsubscribe-current-group (&optional n)
6090   "Toggle subscription of the current group.
6091 If given numerical prefix, toggle the N next groups."
6092   (interactive "P")
6093   (let ((groups (gnus-group-process-prefix n))
6094         group)
6095     (while groups
6096       (setq group (car groups)
6097             groups (cdr groups))
6098       (gnus-group-remove-mark group)
6099       (gnus-group-unsubscribe-group
6100        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6101                  gnus-level-default-unsubscribed
6102                gnus-level-default-subscribed) t)
6103       (gnus-group-update-group-line))
6104     (gnus-group-next-group 1)))
6105
6106 (defun gnus-group-unsubscribe-group (group &optional level silent)
6107   "Toggle subscription to GROUP.
6108 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6109 group line."
6110   (interactive
6111    (list (completing-read
6112           "Group: " gnus-active-hashtb nil
6113           (memq gnus-select-method gnus-have-read-active-file)
6114           nil 
6115           'gnus-group-history)))
6116   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6117     (cond
6118      ((string-match "^[ \t]$" group)
6119       (error "Empty group name"))
6120      (newsrc
6121       ;; Toggle subscription flag.
6122       (gnus-group-change-level
6123        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6124                                       gnus-level-subscribed)
6125                                   (1+ gnus-level-subscribed)
6126                                 gnus-level-default-subscribed)))
6127       (unless silent
6128         (gnus-group-update-group group)))
6129      ((and (stringp group)
6130            (or (not (memq gnus-select-method gnus-have-read-active-file))
6131                (gnus-active group)))
6132       ;; Add new newsgroup.
6133       (gnus-group-change-level
6134        group
6135        (if level level gnus-level-default-subscribed)
6136        (or (and (member group gnus-zombie-list)
6137                 gnus-level-zombie)
6138            gnus-level-killed)
6139        (and (gnus-group-group-name)
6140             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6141       (unless silent
6142         (gnus-group-update-group group)))
6143      (t (error "No such newsgroup: %s" group)))
6144     (gnus-group-position-point)))
6145
6146 (defun gnus-group-transpose-groups (n)
6147   "Move the current newsgroup up N places.
6148 If given a negative prefix, move down instead.  The difference between
6149 N and the number of steps taken is returned."
6150   (interactive "p")
6151   (or (gnus-group-group-name)
6152       (error "No group on current line"))
6153   (gnus-group-kill-group 1)
6154   (prog1
6155       (forward-line (- n))
6156     (gnus-group-yank-group)
6157     (gnus-group-position-point)))
6158
6159 (defun gnus-group-kill-all-zombies ()
6160   "Kill all zombie newsgroups."
6161   (interactive)
6162   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6163   (setq gnus-zombie-list nil)
6164   (gnus-group-list-groups))
6165
6166 (defun gnus-group-kill-region (begin end)
6167   "Kill newsgroups in current region (excluding current point).
6168 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6169   (interactive "r")
6170   (let ((lines
6171          ;; Count lines.
6172          (save-excursion
6173            (count-lines
6174             (progn
6175               (goto-char begin)
6176               (beginning-of-line)
6177               (point))
6178             (progn
6179               (goto-char end)
6180               (beginning-of-line)
6181               (point))))))
6182     (goto-char begin)
6183     (beginning-of-line)                 ;Important when LINES < 1
6184     (gnus-group-kill-group lines)))
6185
6186 (defun gnus-group-kill-group (&optional n discard)
6187   "Kill the next N groups.
6188 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6189 However, only groups that were alive can be yanked; already killed
6190 groups or zombie groups can't be yanked.
6191 The return value is the name of the group that was killed, or a list
6192 of groups killed."
6193   (interactive "P")
6194   (let ((buffer-read-only nil)
6195         (groups (gnus-group-process-prefix n))
6196         group entry level out)
6197     (if (< (length groups) 10)
6198         ;; This is faster when there are few groups.
6199         (while groups
6200           (push (setq group (pop groups)) out)
6201           (gnus-group-remove-mark group)
6202           (setq level (gnus-group-group-level))
6203           (gnus-delete-line)
6204           (when (and (not discard)
6205                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6206             (push (cons (car entry) (nth 2 entry))
6207                   gnus-list-of-killed-groups))
6208           (gnus-group-change-level
6209            (if entry entry group) gnus-level-killed (if entry nil level)))
6210       ;; If there are lots and lots of groups to be killed, we use
6211       ;; this thing instead.
6212       (let (entry)
6213         (setq groups (nreverse groups))
6214         (while groups
6215           (gnus-group-remove-mark (setq group (pop groups)))
6216           (gnus-delete-line)
6217           (cond
6218            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6219             (push (cons (car entry) (nth 2 entry))
6220                   gnus-list-of-killed-groups)
6221             (setcdr (cdr entry) (cdddr entry)))
6222            ((member group gnus-zombie-list)
6223             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6224         (gnus-make-hashtable-from-newsrc-alist)))
6225
6226     (gnus-group-position-point)
6227     (if (< (length out) 2) (car out) (nreverse out))))
6228
6229 (defun gnus-group-yank-group (&optional arg)
6230   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6231 inserting it before the current newsgroup.  The numeric ARG specifies
6232 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6233 is returned, or (if several groups are yanked) a list of yanked groups
6234 is returned."
6235   (interactive "p")
6236   (setq arg (or arg 1))
6237   (let (info group prev out)
6238     (while (>= (decf arg) 0)
6239       (if (not (setq info (pop gnus-list-of-killed-groups)))
6240           (error "No more newsgroups to yank"))
6241       (push (setq group (nth 1 info)) out)
6242       ;; Find which newsgroup to insert this one before - search
6243       ;; backward until something suitable is found.  If there are no
6244       ;; other newsgroups in this buffer, just make this newsgroup the
6245       ;; first newsgroup.
6246       (setq prev (gnus-group-group-name))
6247       (gnus-group-change-level
6248        info (gnus-info-level (cdr info)) gnus-level-killed
6249        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6250        t)
6251       (gnus-group-insert-group-line-info group))
6252     (forward-line -1)
6253     (gnus-group-position-point)
6254     (if (< (length out) 2) (car out) (nreverse out))))
6255
6256 (defun gnus-group-kill-level (level)
6257   "Kill all groups that is on a certain LEVEL."
6258   (interactive "nKill all groups on level: ")
6259   (cond
6260    ((= level gnus-level-zombie)
6261     (setq gnus-killed-list
6262           (nconc gnus-zombie-list gnus-killed-list))
6263     (setq gnus-zombie-list nil))
6264    ((and (< level gnus-level-zombie)
6265          (> level 0)
6266          (or gnus-expert-user
6267              (gnus-yes-or-no-p
6268               (format
6269                "Do you really want to kill all groups on level %d? "
6270                level))))
6271     (let* ((prev gnus-newsrc-alist)
6272            (alist (cdr prev)))
6273       (while alist
6274         (if (= (gnus-info-level level) level)
6275             (setcdr prev (cdr alist))
6276           (setq prev alist))
6277         (setq alist (cdr alist)))
6278       (gnus-make-hashtable-from-newsrc-alist)
6279       (gnus-group-list-groups)))
6280    (t
6281     (error "Can't kill; illegal level: %d" level))))
6282
6283 (defun gnus-group-list-all-groups (&optional arg)
6284   "List all newsgroups with level ARG or lower.
6285 Default is gnus-level-unsubscribed, which lists all subscribed and most
6286 unsubscribed groups."
6287   (interactive "P")
6288   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6289
6290 ;; Redefine this to list ALL killed groups if prefix arg used.
6291 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6292 (defun gnus-group-list-killed (&optional arg)
6293   "List all killed newsgroups in the group buffer.
6294 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6295 entail asking the server for the groups."
6296   (interactive "P")
6297   ;; Find all possible killed newsgroups if arg.
6298   (when arg
6299     ;; First make sure active file has been read.
6300     (unless gnus-have-read-active-file
6301       (let ((gnus-read-active-file t))
6302         (gnus-read-active-file)))
6303     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6304     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6305     (mapatoms
6306      (lambda (sym)
6307        (let ((groups 0)
6308              (group (symbol-name sym)))
6309          (if (or (null group)
6310                  (gnus-gethash group gnus-killed-hashtb)
6311                  (gnus-gethash group gnus-newsrc-hashtb))
6312              ()
6313            (let ((do-sub (gnus-matches-options-n group)))
6314              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6315                  ()
6316                (setq groups (1+ groups))
6317                (setq gnus-killed-list
6318                      (cons group gnus-killed-list))
6319                (gnus-sethash group group gnus-killed-hashtb))))))
6320      gnus-active-hashtb))
6321   (if (not gnus-killed-list)
6322       (gnus-message 6 "No killed groups")
6323     (let (gnus-group-list-mode)
6324       (funcall gnus-group-prepare-function
6325                gnus-level-killed t gnus-level-killed))
6326     (goto-char (point-min)))
6327   (gnus-group-position-point))
6328
6329 (defun gnus-group-list-zombies ()
6330   "List all zombie newsgroups in the group buffer."
6331   (interactive)
6332   (if (not gnus-zombie-list)
6333       (gnus-message 6 "No zombie groups")
6334     (let (gnus-group-list-mode)
6335       (funcall gnus-group-prepare-function
6336                gnus-level-zombie t gnus-level-zombie))
6337     (goto-char (point-min)))
6338   (gnus-group-position-point))
6339
6340 (defun gnus-group-list-active ()
6341   "List all groups that are available from the server(s)."
6342   (interactive)
6343   ;; First we make sure that we have really read the active file.
6344   (unless gnus-have-read-active-file
6345     (let ((gnus-read-active-file t))
6346       (gnus-read-active-file)))
6347   ;; Find all groups and sort them.
6348   (let ((groups
6349          (sort
6350           (let (list)
6351             (mapatoms
6352              (lambda (sym)
6353                (and (symbol-value sym)
6354                     (setq list (cons (symbol-name sym) list))))
6355              gnus-active-hashtb)
6356             list)
6357           'string<))
6358         (buffer-read-only nil))
6359     (erase-buffer)
6360     (while groups
6361       (gnus-group-insert-group-line-info (pop groups)))
6362     (goto-char (point-min))))
6363
6364 (defun gnus-activate-all-groups (level)
6365   "Activate absolutely all groups."
6366   (interactive (list 7))
6367   (let ((gnus-activate-level level)
6368         (gnus-activate-foreign-newsgroups level))
6369     (gnus-group-get-new-news)))
6370
6371 (defun gnus-group-get-new-news (&optional arg)
6372   "Get newly arrived articles.
6373 If ARG is a number, it specifies which levels you are interested in
6374 re-scanning.  If ARG is non-nil and not a number, this will force
6375 \"hard\" re-reading of the active files from all servers."
6376   (interactive "P")
6377   (run-hooks 'gnus-get-new-news-hook)
6378   ;; We might read in new NoCeM messages here.
6379   (when (and gnus-use-nocem 
6380              (null arg))
6381     (gnus-nocem-scan-groups))
6382   ;; If ARG is not a number, then we read the active file.
6383   (when (and arg (not (numberp arg)))
6384     (let ((gnus-read-active-file t))
6385       (gnus-read-active-file))
6386     (setq arg nil))
6387
6388   (setq arg (gnus-group-default-level arg t))
6389   (if (and gnus-read-active-file (not arg))
6390       (progn
6391         (gnus-read-active-file)
6392         (gnus-get-unread-articles arg))
6393     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6394       (gnus-get-unread-articles arg)))
6395   (run-hooks 'gnus-after-getting-new-news-hook)
6396   (gnus-group-list-groups))
6397
6398 (defun gnus-group-get-new-news-this-group (&optional n)
6399   "Check for newly arrived news in the current group (and the N-1 next groups).
6400 The difference between N and the number of newsgroup checked is returned.
6401 If N is negative, this group and the N-1 previous groups will be checked."
6402   (interactive "P")
6403   (let* ((groups (gnus-group-process-prefix n))
6404          (ret (if (numberp n) (- n (length groups)) 0))
6405          (beg (unless n (point)))
6406          group)
6407     (while (setq group (pop groups))
6408       (gnus-group-remove-mark group)
6409       (if (gnus-activate-group group 'scan)
6410           (progn
6411             (gnus-get-unread-articles-in-group
6412              (gnus-get-info group) (gnus-active group) t)
6413             (unless (gnus-virtual-group-p group)
6414               (gnus-close-group group))
6415             (gnus-group-update-group group))
6416         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6417     (when beg (goto-char beg))
6418     (when gnus-goto-next-group-when-activating
6419       (gnus-group-next-unread-group 1 t))
6420     (gnus-summary-position-point)
6421     ret))
6422
6423 (defun gnus-group-fetch-faq (group &optional faq-dir)
6424   "Fetch the FAQ for the current group."
6425   (interactive
6426    (list
6427     (gnus-group-real-name (gnus-group-group-name))
6428     (cond (current-prefix-arg
6429            (completing-read
6430             "Faq dir: " (and (listp gnus-group-faq-directory)
6431                              gnus-group-faq-directory))))))
6432   (or faq-dir
6433       (setq faq-dir (if (listp gnus-group-faq-directory)
6434                         (car gnus-group-faq-directory)
6435                       gnus-group-faq-directory)))
6436   (or group (error "No group name given"))
6437   (let ((file (concat (file-name-as-directory faq-dir)
6438                       (gnus-group-real-name group))))
6439     (if (not (file-exists-p file))
6440         (error "No such file: %s" file)
6441       (find-file file))))
6442
6443 (defun gnus-group-describe-group (force &optional group)
6444   "Display a description of the current newsgroup."
6445   (interactive (list current-prefix-arg (gnus-group-group-name)))
6446   (and force (setq gnus-description-hashtb nil))
6447   (let ((method (gnus-find-method-for-group group))
6448         desc)
6449     (or group (error "No group name given"))
6450     (and (or (and gnus-description-hashtb
6451                   ;; We check whether this group's method has been
6452                   ;; queried for a description file.
6453                   (gnus-gethash
6454                    (gnus-group-prefixed-name "" method)
6455                    gnus-description-hashtb))
6456              (setq desc (gnus-group-get-description group))
6457              (gnus-read-descriptions-file method))
6458          (gnus-message 1
6459           (or desc (gnus-gethash group gnus-description-hashtb)
6460               "No description available")))))
6461
6462 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6463 (defun gnus-group-describe-all-groups (&optional force)
6464   "Pop up a buffer with descriptions of all newsgroups."
6465   (interactive "P")
6466   (and force (setq gnus-description-hashtb nil))
6467   (if (not (or gnus-description-hashtb
6468                (gnus-read-all-descriptions-files)))
6469       (error "Couldn't request descriptions file"))
6470   (let ((buffer-read-only nil)
6471         b)
6472     (erase-buffer)
6473     (mapatoms
6474      (lambda (group)
6475        (setq b (point))
6476        (insert (format "      *: %-20s %s\n" (symbol-name group)
6477                        (symbol-value group)))
6478        (add-text-properties
6479         b (1+ b) (list 'gnus-group group
6480                        'gnus-unread t 'gnus-marked nil
6481                        'gnus-level (1+ gnus-level-subscribed))))
6482      gnus-description-hashtb)
6483     (goto-char (point-min))
6484     (gnus-group-position-point)))
6485
6486 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6487 (defun gnus-group-apropos (regexp &optional search-description)
6488   "List all newsgroups that have names that match a regexp."
6489   (interactive "sGnus apropos (regexp): ")
6490   (let ((prev "")
6491         (obuf (current-buffer))
6492         groups des)
6493     ;; Go through all newsgroups that are known to Gnus.
6494     (mapatoms
6495      (lambda (group)
6496        (and (symbol-name group)
6497             (string-match regexp (symbol-name group))
6498             (setq groups (cons (symbol-name group) groups))))
6499      gnus-active-hashtb)
6500     ;; Also go through all descriptions that are known to Gnus.
6501     (when search-description
6502       (mapatoms
6503        (lambda (group)
6504          (and (string-match regexp (symbol-value group))
6505               (gnus-active (symbol-name group))
6506               (setq groups (cons (symbol-name group) groups))))
6507        gnus-description-hashtb))
6508     (if (not groups)
6509         (gnus-message 3 "No groups matched \"%s\"." regexp)
6510       ;; Print out all the groups.
6511       (save-excursion
6512         (pop-to-buffer "*Gnus Help*")
6513         (buffer-disable-undo (current-buffer))
6514         (erase-buffer)
6515         (setq groups (sort groups 'string<))
6516         (while groups
6517           ;; Groups may be entered twice into the list of groups.
6518           (if (not (string= (car groups) prev))
6519               (progn
6520                 (insert (setq prev (car groups)) "\n")
6521                 (if (and gnus-description-hashtb
6522                          (setq des (gnus-gethash (car groups)
6523                                                  gnus-description-hashtb)))
6524                     (insert "  " des "\n"))))
6525           (setq groups (cdr groups)))
6526         (goto-char (point-min))))
6527     (pop-to-buffer obuf)))
6528
6529 (defun gnus-group-description-apropos (regexp)
6530   "List all newsgroups that have names or descriptions that match a regexp."
6531   (interactive "sGnus description apropos (regexp): ")
6532   (if (not (or gnus-description-hashtb
6533                (gnus-read-all-descriptions-files)))
6534       (error "Couldn't request descriptions file"))
6535   (gnus-group-apropos regexp t))
6536
6537 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6538 (defun gnus-group-list-matching (level regexp &optional all lowest)
6539   "List all groups with unread articles that match REGEXP.
6540 If the prefix LEVEL is non-nil, it should be a number that says which
6541 level to cut off listing groups.
6542 If ALL, also list groups with no unread articles.
6543 If LOWEST, don't list groups with level lower than LOWEST."
6544   (interactive "P\nsList newsgroups matching: ")
6545   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6546                            all (or lowest 1) regexp)
6547   (goto-char (point-min))
6548   (gnus-group-position-point))
6549
6550 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6551   "List all groups that match REGEXP.
6552 If the prefix LEVEL is non-nil, it should be a number that says which
6553 level to cut off listing groups.
6554 If LOWEST, don't list groups with level lower than LOWEST."
6555   (interactive "P\nsList newsgroups matching: ")
6556   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6557
6558 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6559 (defun gnus-group-save-newsrc (&optional force)
6560   "Save the Gnus startup files.
6561 If FORCE, force saving whether it is necessary or not."
6562   (interactive "P")
6563   (gnus-save-newsrc-file force))
6564
6565 (defun gnus-group-restart (&optional arg)
6566   "Force Gnus to read the .newsrc file."
6567   (interactive "P")
6568   (when (gnus-yes-or-no-p
6569          (format "Are you sure you want to read %s? "
6570                  gnus-current-startup-file))
6571     (gnus-save-newsrc-file)
6572     (gnus-setup-news 'force)
6573     (gnus-group-list-groups arg)))
6574
6575 (defun gnus-group-read-init-file ()
6576   "Read the Gnus elisp init file."
6577   (interactive)
6578   (gnus-read-init-file))
6579
6580 (defun gnus-group-check-bogus-groups (&optional silent)
6581   "Check bogus newsgroups.
6582 If given a prefix, don't ask for confirmation before removing a bogus
6583 group."
6584   (interactive "P")
6585   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6586   (gnus-group-list-groups))
6587
6588 (defun gnus-group-edit-global-kill (&optional article group)
6589   "Edit the global kill file.
6590 If GROUP, edit that local kill file instead."
6591   (interactive "P")
6592   (setq gnus-current-kill-article article)
6593   (gnus-kill-file-edit-file group)
6594   (gnus-message
6595    6
6596    (substitute-command-keys
6597     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6598             (if group "local" "global")))))
6599
6600 (defun gnus-group-edit-local-kill (article group)
6601   "Edit a local kill file."
6602   (interactive (list nil (gnus-group-group-name)))
6603   (gnus-group-edit-global-kill article group))
6604
6605 (defun gnus-group-force-update ()
6606   "Update `.newsrc' file."
6607   (interactive)
6608   (gnus-save-newsrc-file))
6609
6610 (defun gnus-group-suspend ()
6611   "Suspend the current Gnus session.
6612 In fact, cleanup buffers except for group mode buffer.
6613 The hook gnus-suspend-gnus-hook is called before actually suspending."
6614   (interactive)
6615   (run-hooks 'gnus-suspend-gnus-hook)
6616   ;; Kill Gnus buffers except for group mode buffer.
6617   (let ((group-buf (get-buffer gnus-group-buffer)))
6618     ;; Do this on a separate list in case the user does a ^G before we finish
6619     (let ((gnus-buffer-list
6620            (delq group-buf (delq gnus-dribble-buffer
6621                                  (append gnus-buffer-list nil)))))
6622       (while gnus-buffer-list
6623         (gnus-kill-buffer (car gnus-buffer-list))
6624         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6625     (if group-buf
6626         (progn
6627           (setq gnus-buffer-list (list group-buf))
6628           (bury-buffer group-buf)
6629           (delete-windows-on group-buf t)))))
6630
6631 (defun gnus-group-clear-dribble ()
6632   "Clear all information from the dribble buffer."
6633   (interactive)
6634   (gnus-dribble-clear)
6635   (gnus-message 7 "Cleared dribble buffer"))
6636
6637 (defun gnus-group-exit ()
6638   "Quit reading news after updating .newsrc.eld and .newsrc.
6639 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6640   (interactive)
6641   (when 
6642       (or noninteractive                ;For gnus-batch-kill
6643           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6644           (not gnus-interactive-exit)   ;Without confirmation
6645           gnus-expert-user
6646           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6647     (run-hooks 'gnus-exit-gnus-hook)
6648     ;; Offer to save data from non-quitted summary buffers.
6649     (gnus-offer-save-summaries)
6650     ;; Save the newsrc file(s).
6651     (gnus-save-newsrc-file)
6652     ;; Kill-em-all.
6653     (gnus-close-backends)
6654     ;; Reset everything.
6655     (gnus-clear-system)
6656     ;; Allow the user to do things after cleaning up.
6657     (run-hooks 'gnus-after-exiting-gnus-hook)))
6658
6659 (defun gnus-close-backends ()
6660   ;; Send a close request to all backends that support such a request.
6661   (let ((methods gnus-valid-select-methods)
6662         func)
6663     (while methods
6664       (if (fboundp (setq func (intern (concat (caar methods)
6665                                               "-request-close"))))
6666           (funcall func))
6667       (setq methods (cdr methods)))))
6668
6669 (defun gnus-group-quit ()
6670   "Quit reading news without updating .newsrc.eld or .newsrc.
6671 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6672   (interactive)
6673   (when (or noninteractive              ;For gnus-batch-kill
6674             (zerop (buffer-size))
6675             (not (gnus-server-opened gnus-select-method))
6676             gnus-expert-user
6677             (not gnus-current-startup-file)
6678             (gnus-yes-or-no-p
6679              (format "Quit reading news without saving %s? "
6680                      (file-name-nondirectory gnus-current-startup-file))))
6681     (run-hooks 'gnus-exit-gnus-hook)
6682     (if gnus-use-full-window
6683         (delete-other-windows)
6684       (gnus-remove-some-windows))
6685     (gnus-dribble-save)
6686     (gnus-close-backends)
6687     (gnus-clear-system)
6688     ;; Allow the user to do things after cleaning up.
6689     (run-hooks 'gnus-after-exiting-gnus-hook)))
6690
6691 (defun gnus-offer-save-summaries ()
6692   "Offer to save all active summary buffers."
6693   (save-excursion
6694     (let ((buflist (buffer-list))
6695           buffers bufname)
6696       ;; Go through all buffers and find all summaries.
6697       (while buflist
6698         (and (setq bufname (buffer-name (car buflist)))
6699              (string-match "Summary" bufname)
6700              (save-excursion
6701                (set-buffer bufname)
6702                ;; We check that this is, indeed, a summary buffer.
6703                (and (eq major-mode 'gnus-summary-mode)
6704                     ;; Also make sure this isn't bogus.
6705                     gnus-newsgroup-prepared))
6706              (push bufname buffers))
6707         (setq buflist (cdr buflist)))
6708       ;; Go through all these summary buffers and offer to save them.
6709       (when buffers
6710         (map-y-or-n-p
6711          "Update summary buffer %s? "
6712          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6713          buffers)))))
6714
6715 (defun gnus-group-describe-briefly ()
6716   "Give a one line description of the group mode commands."
6717   (interactive)
6718   (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")))
6719
6720 (defun gnus-group-browse-foreign-server (method)
6721   "Browse a foreign news server.
6722 If called interactively, this function will ask for a select method
6723  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6724 If not, METHOD should be a list where the first element is the method
6725 and the second element is the address."
6726   (interactive
6727    (list (let ((how (completing-read
6728                      "Which backend: "
6729                      (append gnus-valid-select-methods gnus-server-alist)
6730                      nil t (cons "nntp" 0) 'gnus-method-history)))
6731            ;; We either got a backend name or a virtual server name.
6732            ;; If the first, we also need an address.
6733            (if (assoc how gnus-valid-select-methods)
6734                (list (intern how)
6735                      ;; Suggested by mapjph@bath.ac.uk.
6736                      (completing-read
6737                       "Address: "
6738                       (mapcar (lambda (server) (list server))
6739                               gnus-secondary-servers)))
6740              ;; We got a server name, so we find the method.
6741              (gnus-server-to-method how)))))
6742   (gnus-browse-foreign-server method))
6743
6744 \f
6745 ;;;
6746 ;;; Gnus summary mode
6747 ;;;
6748
6749 (defvar gnus-summary-mode-map nil)
6750
6751 (put 'gnus-summary-mode 'mode-class 'special)
6752
6753 (unless gnus-summary-mode-map
6754   (setq gnus-summary-mode-map (make-keymap))
6755   (suppress-keymap gnus-summary-mode-map)
6756
6757   ;; Non-orthogonal keys
6758
6759   (gnus-define-keys gnus-summary-mode-map
6760     " " gnus-summary-next-page
6761     "\177" gnus-summary-prev-page
6762     [delete] gnus-summary-prev-page
6763     "\r" gnus-summary-scroll-up
6764     "n" gnus-summary-next-unread-article
6765     "p" gnus-summary-prev-unread-article
6766     "N" gnus-summary-next-article
6767     "P" gnus-summary-prev-article
6768     "\M-\C-n" gnus-summary-next-same-subject
6769     "\M-\C-p" gnus-summary-prev-same-subject
6770     "\M-n" gnus-summary-next-unread-subject
6771     "\M-p" gnus-summary-prev-unread-subject
6772     "." gnus-summary-first-unread-article
6773     "," gnus-summary-best-unread-article
6774     "\M-s" gnus-summary-search-article-forward
6775     "\M-r" gnus-summary-search-article-backward
6776     "<" gnus-summary-beginning-of-article
6777     ">" gnus-summary-end-of-article
6778     "j" gnus-summary-goto-article
6779     "^" gnus-summary-refer-parent-article
6780     "\M-^" gnus-summary-refer-article
6781     "u" gnus-summary-tick-article-forward
6782     "!" gnus-summary-tick-article-forward
6783     "U" gnus-summary-tick-article-backward
6784     "d" gnus-summary-mark-as-read-forward
6785     "D" gnus-summary-mark-as-read-backward
6786     "E" gnus-summary-mark-as-expirable
6787     "\M-u" gnus-summary-clear-mark-forward
6788     "\M-U" gnus-summary-clear-mark-backward
6789     "k" gnus-summary-kill-same-subject-and-select
6790     "\C-k" gnus-summary-kill-same-subject
6791     "\M-\C-k" gnus-summary-kill-thread
6792     "\M-\C-l" gnus-summary-lower-thread
6793     "e" gnus-summary-edit-article
6794     "#" gnus-summary-mark-as-processable
6795     "\M-#" gnus-summary-unmark-as-processable
6796     "\M-\C-t" gnus-summary-toggle-threads
6797     "\M-\C-s" gnus-summary-show-thread
6798     "\M-\C-h" gnus-summary-hide-thread
6799     "\M-\C-f" gnus-summary-next-thread
6800     "\M-\C-b" gnus-summary-prev-thread
6801     "\M-\C-u" gnus-summary-up-thread
6802     "\M-\C-d" gnus-summary-down-thread
6803     "&" gnus-summary-execute-command
6804     "c" gnus-summary-catchup-and-exit
6805     "\C-w" gnus-summary-mark-region-as-read
6806     "\C-t" gnus-summary-toggle-truncation
6807     "?" gnus-summary-mark-as-dormant
6808     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6809     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6810     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6811     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6812     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6813     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6814     "=" gnus-summary-expand-window
6815     "\C-x\C-s" gnus-summary-reselect-current-group
6816     "\M-g" gnus-summary-rescan-group
6817     "w" gnus-summary-stop-page-breaking
6818     "\C-c\C-r" gnus-summary-caesar-message
6819     "\M-t" gnus-summary-toggle-mime
6820     "f" gnus-summary-followup
6821     "F" gnus-summary-followup-with-original
6822     "C" gnus-summary-cancel-article
6823     "r" gnus-summary-reply
6824     "R" gnus-summary-reply-with-original
6825     "\C-c\C-f" gnus-summary-mail-forward
6826     "o" gnus-summary-save-article
6827     "\C-o" gnus-summary-save-article-mail
6828     "|" gnus-summary-pipe-output
6829     "\M-k" gnus-summary-edit-local-kill
6830     "\M-K" gnus-summary-edit-global-kill
6831     "V" gnus-version
6832     "\C-c\C-d" gnus-summary-describe-group
6833     "q" gnus-summary-exit
6834     "Q" gnus-summary-exit-no-update
6835     "\C-c\C-i" gnus-info-find-node
6836     gnus-mouse-2 gnus-mouse-pick-article
6837     "m" gnus-summary-mail-other-window
6838     "a" gnus-summary-post-news
6839     "x" gnus-summary-limit-to-unread
6840     "s" gnus-summary-isearch-article
6841     "t" gnus-article-hide-headers
6842     "g" gnus-summary-show-article
6843     "l" gnus-summary-goto-last-article
6844     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6845     "\C-d" gnus-summary-enter-digest-group
6846     "\C-c\C-b" gnus-bug
6847     "*" gnus-cache-enter-article
6848     "\M-*" gnus-cache-remove-article
6849     "\M-&" gnus-summary-universal-argument
6850     "\C-l" gnus-recenter
6851     "I" gnus-summary-increase-score
6852     "L" gnus-summary-lower-score
6853
6854     "V" gnus-summary-score-map
6855     "X" gnus-uu-extract-map
6856     "S" gnus-summary-send-map)
6857
6858   ;; Sort of orthogonal keymap
6859   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6860     "t" gnus-summary-tick-article-forward
6861     "!" gnus-summary-tick-article-forward
6862     "d" gnus-summary-mark-as-read-forward
6863     "r" gnus-summary-mark-as-read-forward
6864     "c" gnus-summary-clear-mark-forward
6865     " " gnus-summary-clear-mark-forward
6866     "e" gnus-summary-mark-as-expirable
6867     "x" gnus-summary-mark-as-expirable
6868     "?" gnus-summary-mark-as-dormant
6869     "b" gnus-summary-set-bookmark
6870     "B" gnus-summary-remove-bookmark
6871     "#" gnus-summary-mark-as-processable
6872     "\M-#" gnus-summary-unmark-as-processable
6873     "S" gnus-summary-limit-include-expunged
6874     "C" gnus-summary-catchup
6875     "H" gnus-summary-catchup-to-here
6876     "\C-c" gnus-summary-catchup-all
6877     "k" gnus-summary-kill-same-subject-and-select
6878     "K" gnus-summary-kill-same-subject
6879     "P" gnus-uu-mark-map)
6880
6881   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6882     "c" gnus-summary-clear-above
6883     "u" gnus-summary-tick-above
6884     "m" gnus-summary-mark-above
6885     "k" gnus-summary-kill-below)
6886
6887   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6888     "/" gnus-summary-limit-to-subject
6889     "n" gnus-summary-limit-to-articles
6890     "w" gnus-summary-pop-limit
6891     "s" gnus-summary-limit-to-subject
6892     "a" gnus-summary-limit-to-author
6893     "u" gnus-summary-limit-to-unread
6894     "m" gnus-summary-limit-to-marks
6895     "v" gnus-summary-limit-to-score
6896     "D" gnus-summary-limit-include-dormant
6897     "d" gnus-summary-limit-exclude-dormant
6898     ;;  "t" gnus-summary-limit-exclude-thread
6899     "E" gnus-summary-limit-include-expunged
6900     "c" gnus-summary-limit-exclude-childless-dormant
6901     "C" gnus-summary-limit-mark-excluded-as-read)
6902
6903   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6904     "n" gnus-summary-next-unread-article
6905     "p" gnus-summary-prev-unread-article
6906     "N" gnus-summary-next-article
6907     "P" gnus-summary-prev-article
6908     "\C-n" gnus-summary-next-same-subject
6909     "\C-p" gnus-summary-prev-same-subject
6910     "\M-n" gnus-summary-next-unread-subject
6911     "\M-p" gnus-summary-prev-unread-subject
6912     "f" gnus-summary-first-unread-article
6913     "b" gnus-summary-best-unread-article
6914     "j" gnus-summary-goto-article
6915     "g" gnus-summary-goto-subject
6916     "l" gnus-summary-goto-last-article
6917     "p" gnus-summary-pop-article)
6918
6919   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6920     "k" gnus-summary-kill-thread
6921     "l" gnus-summary-lower-thread
6922     "i" gnus-summary-raise-thread
6923     "T" gnus-summary-toggle-threads
6924     "t" gnus-summary-rethread-current
6925     "^" gnus-summary-reparent-thread
6926     "s" gnus-summary-show-thread
6927     "S" gnus-summary-show-all-threads
6928     "h" gnus-summary-hide-thread
6929     "H" gnus-summary-hide-all-threads
6930     "n" gnus-summary-next-thread
6931     "p" gnus-summary-prev-thread
6932     "u" gnus-summary-up-thread
6933     "o" gnus-summary-top-thread
6934     "d" gnus-summary-down-thread
6935     "#" gnus-uu-mark-thread
6936     "\M-#" gnus-uu-unmark-thread)
6937
6938   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6939     "c" gnus-summary-catchup-and-exit
6940     "C" gnus-summary-catchup-all-and-exit
6941     "E" gnus-summary-exit-no-update
6942     "Q" gnus-summary-exit
6943     "Z" gnus-summary-exit
6944     "n" gnus-summary-catchup-and-goto-next-group
6945     "R" gnus-summary-reselect-current-group
6946     "G" gnus-summary-rescan-group
6947     "N" gnus-summary-next-group
6948     "P" gnus-summary-prev-group)
6949
6950   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6951     " " gnus-summary-next-page
6952     "n" gnus-summary-next-page
6953     "\177" gnus-summary-prev-page
6954     [delete] gnus-summary-prev-page
6955     "p" gnus-summary-prev-page
6956     "\r" gnus-summary-scroll-up
6957     "<" gnus-summary-beginning-of-article
6958     ">" gnus-summary-end-of-article
6959     "b" gnus-summary-beginning-of-article
6960     "e" gnus-summary-end-of-article
6961     "^" gnus-summary-refer-parent-article
6962     "r" gnus-summary-refer-parent-article
6963     "R" gnus-summary-refer-references
6964     "g" gnus-summary-show-article
6965     "s" gnus-summary-isearch-article)
6966
6967   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6968     "b" gnus-article-add-buttons
6969     "B" gnus-article-add-buttons-to-head
6970     "o" gnus-article-treat-overstrike
6971     ;;  "w" gnus-article-word-wrap
6972     "w" gnus-article-fill-cited-article
6973     "c" gnus-article-remove-cr
6974     "L" gnus-article-remove-trailing-blank-lines
6975     "q" gnus-article-de-quoted-unreadable
6976     "f" gnus-article-display-x-face
6977     "l" gnus-summary-stop-page-breaking
6978     "r" gnus-summary-caesar-message
6979     "t" gnus-article-hide-headers
6980     "v" gnus-summary-verbose-headers
6981     "m" gnus-summary-toggle-mime)
6982
6983   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6984     "a" gnus-article-hide
6985     "h" gnus-article-hide-headers
6986     "b" gnus-article-hide-boring-headers
6987     "s" gnus-article-hide-signature
6988     "c" gnus-article-hide-citation
6989     "p" gnus-article-hide-pgp
6990     "\C-c" gnus-article-hide-citation-maybe)
6991
6992   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6993     "a" gnus-article-highlight
6994     "h" gnus-article-highlight-headers
6995     "c" gnus-article-highlight-citation
6996     "s" gnus-article-highlight-signature)
6997
6998   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6999     "z" gnus-article-date-ut
7000     "u" gnus-article-date-ut
7001     "l" gnus-article-date-local
7002     "e" gnus-article-date-lapsed
7003     "o" gnus-article-date-original)
7004
7005   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7006     "v" gnus-version
7007     "f" gnus-summary-fetch-faq
7008     "d" gnus-summary-describe-group
7009     "h" gnus-summary-describe-briefly
7010     "i" gnus-info-find-node)
7011
7012   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7013     "e" gnus-summary-expire-articles
7014     "\M-\C-e" gnus-summary-expire-articles-now
7015     "\177" gnus-summary-delete-article
7016     [delete] gnus-summary-delete-article
7017     "m" gnus-summary-move-article
7018     "r" gnus-summary-respool-article
7019     "w" gnus-summary-edit-article
7020     "c" gnus-summary-copy-article
7021     "B" gnus-summary-crosspost-article
7022     "q" gnus-summary-respool-query
7023     "i" gnus-summary-import-article)
7024
7025   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7026     "o" gnus-summary-save-article
7027     "m" gnus-summary-save-article-mail
7028     "r" gnus-summary-save-article-rmail
7029     "f" gnus-summary-save-article-file
7030     "b" gnus-summary-save-article-body-file
7031     "h" gnus-summary-save-article-folder
7032     "v" gnus-summary-save-article-vm
7033     "p" gnus-summary-pipe-output
7034     "s" gnus-soup-add-article)
7035   )
7036
7037 \f
7038
7039 (defun gnus-summary-mode (&optional group)
7040   "Major mode for reading articles.
7041
7042 All normal editing commands are switched off.
7043 \\<gnus-summary-mode-map>
7044 Each line in this buffer represents one article.  To read an
7045 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7046 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7047 respectively.
7048
7049 You can also post articles and send mail from this buffer.  To
7050 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7051 of an article, type `\\[gnus-summary-reply]'.
7052
7053 There are approx. one gazillion commands you can execute in this
7054 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7055
7056 The following commands are available:
7057
7058 \\{gnus-summary-mode-map}"
7059   (interactive)
7060   (when (and menu-bar-mode
7061              (gnus-visual-p 'summary-menu 'menu))
7062     (gnus-summary-make-menu-bar))
7063   (kill-all-local-variables)
7064   (let ((locals gnus-summary-local-variables))
7065     (while locals
7066       (if (consp (car locals))
7067           (progn
7068             (make-local-variable (caar locals))
7069             (set (caar locals) (eval (cdar locals))))
7070         (make-local-variable (car locals))
7071         (set (car locals) nil))
7072       (setq locals (cdr locals))))
7073   (gnus-make-thread-indent-array)
7074   (gnus-simplify-mode-line)
7075   (setq major-mode 'gnus-summary-mode)
7076   (setq mode-name "Summary")
7077   (make-local-variable 'minor-mode-alist)
7078   (use-local-map gnus-summary-mode-map)
7079   (buffer-disable-undo (current-buffer))
7080   (setq buffer-read-only t)             ;Disable modification
7081   (setq truncate-lines t)
7082   (setq selective-display t)
7083   (setq selective-display-ellipses t)   ;Display `...'
7084   (setq buffer-display-table gnus-summary-display-table)
7085   (setq gnus-newsgroup-name group)
7086   (make-local-variable 'gnus-summary-line-format)
7087   (make-local-variable 'gnus-summary-line-format-spec)
7088   (make-local-variable 'gnus-summary-mark-positions)
7089   (run-hooks 'gnus-summary-mode-hook))
7090
7091 (defun gnus-summary-make-display-table ()
7092   ;; Change the display table.  Odd characters have a tendency to mess
7093   ;; up nicely formatted displays - we make all possible glyphs
7094   ;; display only a single character.
7095
7096   ;; We start from the standard display table, if any.
7097   (setq gnus-summary-display-table
7098         (or (copy-sequence standard-display-table)
7099             (make-display-table)))
7100   ;; Nix out all the control chars...
7101   (let ((i 32))
7102     (while (>= (setq i (1- i)) 0)
7103       (aset gnus-summary-display-table i [??])))
7104   ;; ... but not newline and cr, of course. (cr is necessary for the
7105   ;; selective display).
7106   (aset gnus-summary-display-table ?\n nil)
7107   (aset gnus-summary-display-table ?\r nil)
7108   ;; We nix out any glyphs over 126 that are not set already.
7109   (let ((i 256))
7110     (while (>= (setq i (1- i)) 127)
7111       ;; Only modify if the entry is nil.
7112       (or (aref gnus-summary-display-table i)
7113           (aset gnus-summary-display-table i [??])))))
7114
7115 (defun gnus-summary-clear-local-variables ()
7116   (let ((locals gnus-summary-local-variables))
7117     (while locals
7118       (if (consp (car locals))
7119           (and (vectorp (caar locals))
7120                (set (caar locals) nil))
7121         (and (vectorp (car locals))
7122              (set (car locals) nil)))
7123       (setq locals (cdr locals)))))
7124
7125 ;; Summary data functions.
7126
7127 (defmacro gnus-data-number (data)
7128   `(car ,data))
7129
7130 (defmacro gnus-data-set-number (data number)
7131   `(setcar ,data ,number))
7132
7133 (defmacro gnus-data-mark (data)
7134   `(nth 1 ,data))
7135
7136 (defmacro gnus-data-set-mark (data mark)
7137   `(setcar (nthcdr 1 ,data) ,mark))
7138
7139 (defmacro gnus-data-pos (data)
7140   `(nth 2 ,data))
7141
7142 (defmacro gnus-data-set-pos (data pos)
7143   `(setcar (nthcdr 2 ,data) ,pos))
7144
7145 (defmacro gnus-data-header (data)
7146   `(nth 3 ,data))
7147
7148 (defmacro gnus-data-level (data)
7149   `(nth 4 ,data))
7150
7151 (defmacro gnus-data-unread-p (data)
7152   `(= (nth 1 ,data) gnus-unread-mark))
7153
7154 (defmacro gnus-data-pseudo-p (data)
7155   `(consp (nth 3 ,data)))
7156
7157 (defmacro gnus-data-find (number)
7158   `(assq ,number gnus-newsgroup-data))
7159
7160 (defmacro gnus-data-find-list (number &optional data)
7161   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7162      (memq (assq ,number bdata)
7163            bdata)))
7164
7165 (defmacro gnus-data-make (number mark pos header level)
7166   `(list ,number ,mark ,pos ,header ,level))
7167
7168 (defun gnus-data-enter (after-article number mark pos header level offset)
7169   (let ((data (gnus-data-find-list after-article)))
7170     (or data (error "No such article: %d" after-article))
7171     (setcdr data (cons (gnus-data-make number mark pos header level)
7172                        (cdr data)))
7173     (setq gnus-newsgroup-data-reverse nil)
7174     (gnus-data-update-list (cddr data) offset)))
7175
7176 (defun gnus-data-enter-list (after-article list &optional offset)
7177   (when list
7178     (let ((data (and after-article (gnus-data-find-list after-article)))
7179           (ilist list))
7180       (or data (not after-article) (error "No such article: %d" after-article))
7181       ;; Find the last element in the list to be spliced into the main
7182       ;; list.
7183       (while (cdr list)
7184         (setq list (cdr list)))
7185       (if (not data)
7186           (progn
7187             (setcdr list gnus-newsgroup-data)
7188             (setq gnus-newsgroup-data ilist)
7189             (and offset (gnus-data-update-list (cdr list) offset)))
7190         (setcdr list (cdr data))
7191         (setcdr data ilist)
7192         (and offset (gnus-data-update-list (cdr data) offset)))
7193       (setq gnus-newsgroup-data-reverse nil))))
7194
7195 (defun gnus-data-remove (article &optional offset)
7196   (let ((data gnus-newsgroup-data))
7197     (if (= (gnus-data-number (car data)) article)
7198         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7199               gnus-newsgroup-data-reverse nil)
7200       (while (cdr data)
7201         (and (= (gnus-data-number (cadr data)) article)
7202              (progn
7203                (setcdr data (cddr data))
7204                (and offset (gnus-data-update-list (cdr data) offset))
7205                (setq data nil
7206                      gnus-newsgroup-data-reverse nil)))
7207         (setq data (cdr data))))))
7208
7209 (defmacro gnus-data-list (backward)
7210   `(if ,backward
7211        (or gnus-newsgroup-data-reverse
7212            (setq gnus-newsgroup-data-reverse
7213                  (reverse gnus-newsgroup-data)))
7214      gnus-newsgroup-data))
7215
7216 (defun gnus-data-update-list (data offset)
7217   "Add OFFSET to the POS of all data entries in DATA."
7218   (while data
7219     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7220     (setq data (cdr data))))
7221
7222 (defun gnus-data-compute-positions ()
7223   "Compute the positions of all articles."
7224   (let ((data gnus-newsgroup-data)
7225         pos)
7226     (while data
7227       (when (setq pos (text-property-any
7228                        (point-min) (point-max)
7229                        'gnus-number (gnus-data-number (car data))))
7230         (gnus-data-set-pos (car data) (+ pos 3)))
7231       (setq data (cdr data)))))
7232
7233 (defun gnus-summary-article-pseudo-p (article)
7234   "Say whether this article is a pseudo article or not."
7235   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7236
7237 (defun gnus-article-parent-p (number)
7238   "Say whether this article is a parent or not."
7239   (let ((data (gnus-data-find-list number)))
7240     (and (cdr data)                     ; There has to be an article after...
7241          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7242             (gnus-data-level (nth 1 data))))))
7243
7244 (defun gnus-article-children (number)
7245   "Return a list of all children to NUMBER."
7246   (let* ((data (gnus-data-find-list number))
7247          (level (gnus-data-level (car data)))
7248          children)
7249     (setq data (cdr data))
7250     (while (and data            
7251                 (= (gnus-data-level (car data)) (1+ level)))
7252       (push (gnus-data-number (car data)) children)
7253       (setq data (cdr data)))
7254     children))
7255
7256 (defmacro gnus-summary-skip-intangible ()
7257   "If the current article is intangible, then jump to a different article."
7258   '(let ((to (get-text-property (point) 'gnus-intangible)))
7259     (and to (gnus-summary-goto-subject to))))
7260
7261 (defmacro gnus-summary-article-intangible-p ()
7262   "Say whether this article is intangible or not."
7263   '(get-text-property (point) 'gnus-intangible))
7264
7265 ;; Some summary mode macros.
7266
7267 (defmacro gnus-summary-article-number ()
7268   "The article number of the article on the current line.
7269 If there isn's an article number here, then we return the current
7270 article number."
7271   '(progn
7272      (gnus-summary-skip-intangible)
7273      (or (get-text-property (point) 'gnus-number)
7274          (gnus-summary-last-subject))))
7275
7276 (defmacro gnus-summary-article-header (&optional number)
7277   `(gnus-data-header (gnus-data-find
7278                       ,(or number '(gnus-summary-article-number)))))
7279
7280 (defmacro gnus-summary-thread-level (&optional number)
7281   `(if (and (eq gnus-summary-make-false-root 'dummy)
7282             (get-text-property (point) 'gnus-intangible))
7283        0
7284      (gnus-data-level (gnus-data-find
7285                        ,(or number '(gnus-summary-article-number))))))
7286
7287 (defmacro gnus-summary-article-mark (&optional number)
7288   `(gnus-data-mark (gnus-data-find
7289                     ,(or number '(gnus-summary-article-number)))))
7290
7291 (defmacro gnus-summary-article-pos (&optional number)
7292   `(gnus-data-pos (gnus-data-find
7293                    ,(or number '(gnus-summary-article-number)))))
7294
7295 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7296 (defmacro gnus-summary-article-subject (&optional number)
7297   "Return current subject string or nil if nothing."
7298   `(let ((headers
7299           ,(if number
7300                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7301              '(gnus-data-header (assq (gnus-summary-article-number)
7302                                       gnus-newsgroup-data)))))
7303      (and headers
7304           (vectorp headers)
7305           (mail-header-subject headers))))
7306
7307 (defmacro gnus-summary-article-score (&optional number)
7308   "Return current article score."
7309   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7310                   gnus-newsgroup-scored))
7311        gnus-summary-default-score 0))
7312
7313 (defun gnus-summary-article-children (&optional number)
7314   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7315          (level (gnus-data-level (car data)))
7316          l children)
7317     (while (and (setq data (cdr data))
7318                 (> (setq l (gnus-data-level (car data))) level))
7319       (and (= (1+ level) l)
7320            (setq children (cons (gnus-data-number (car data))
7321                                 children))))
7322     (nreverse children)))
7323
7324 (defun gnus-summary-article-parent (&optional number)
7325   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7326                                     (gnus-data-list t)))
7327          (level (gnus-data-level (car data))))
7328     (if (zerop level)
7329         () ; This is a root.
7330       ;; We search until we find an article with a level less than
7331       ;; this one.  That function has to be the parent.
7332       (while (and (setq data (cdr data))
7333                   (not (< (gnus-data-level (car data)) level))))
7334       (and data (gnus-data-number (car data))))))
7335
7336 (defun gnus-unread-mark-p (mark)
7337   "Say whether MARK is the unread mark."
7338   (= mark gnus-unread-mark))
7339
7340 (defun gnus-read-mark-p (mark)
7341   "Say whether MARK is one of the marks that mark as read.
7342 This is all marks except unread, ticked, dormant, and expirable."
7343   (not (or (= mark gnus-unread-mark)
7344            (= mark gnus-ticked-mark)
7345            (= mark gnus-dormant-mark)
7346            (= mark gnus-expirable-mark))))
7347
7348 ;; Various summary mode internalish functions.
7349
7350 (defun gnus-mouse-pick-article (e)
7351   (interactive "e")
7352   (mouse-set-point e)
7353   (gnus-summary-next-page nil t))
7354
7355 (defun gnus-summary-setup-buffer (group)
7356   "Initialize summary buffer."
7357   (let ((buffer (concat "*Summary " group "*")))
7358     (if (get-buffer buffer)
7359         (progn
7360           (set-buffer buffer)
7361           (setq gnus-summary-buffer (current-buffer))
7362           (not gnus-newsgroup-prepared))
7363       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7364       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7365       (gnus-add-current-to-buffer-list)
7366       (gnus-summary-mode group)
7367       (when gnus-carpal
7368         (gnus-carpal-setup-buffer 'summary))
7369       (unless gnus-single-article-buffer
7370         (make-local-variable 'gnus-article-buffer)
7371         (make-local-variable 'gnus-article-current)
7372         (make-local-variable 'gnus-original-article-buffer))
7373       (setq gnus-newsgroup-name group)
7374       t)))
7375
7376 (defun gnus-set-global-variables ()
7377   ;; Set the global equivalents of the summary buffer-local variables
7378   ;; to the latest values they had.  These reflect the summary buffer
7379   ;; that was in action when the last article was fetched.
7380   (when (eq major-mode 'gnus-summary-mode)
7381     (setq gnus-summary-buffer (current-buffer))
7382     (let ((name gnus-newsgroup-name)
7383           (marked gnus-newsgroup-marked)
7384           (unread gnus-newsgroup-unreads)
7385           (headers gnus-current-headers)
7386           (data gnus-newsgroup-data)
7387           (summary gnus-summary-buffer)
7388           (article-buffer gnus-article-buffer)
7389           (original gnus-original-article-buffer)
7390           (gac gnus-article-current)
7391           (score-file gnus-current-score-file))
7392       (save-excursion
7393         (set-buffer gnus-group-buffer)
7394         (setq gnus-newsgroup-name name)
7395         (setq gnus-newsgroup-marked marked)
7396         (setq gnus-newsgroup-unreads unread)
7397         (setq gnus-current-headers headers)
7398         (setq gnus-newsgroup-data data)
7399         (setq gnus-article-current gac)
7400         (setq gnus-summary-buffer summary)
7401         (setq gnus-article-buffer article-buffer)
7402         (setq gnus-original-article-buffer original)
7403         (setq gnus-current-score-file score-file)))))
7404
7405 (defun gnus-summary-last-article-p (&optional article)
7406   "Return whether ARTICLE is the last article in the buffer."
7407   (if (not (setq article (or article (gnus-summary-article-number))))
7408       t ; All non-existant numbers are the last article. :-)
7409     (not (cdr (gnus-data-find-list article)))))
7410
7411 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7412   "Insert a dummy root in the summary buffer."
7413   (beginning-of-line)
7414   (add-text-properties
7415    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7416    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7417
7418 (defun gnus-make-thread-indent-array ()
7419   (let ((n 200))
7420     (unless (and gnus-thread-indent-array
7421                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7422       (setq gnus-thread-indent-array (make-vector 201 "")
7423             gnus-thread-indent-array-level gnus-thread-indent-level)
7424       (while (>= n 0)
7425         (aset gnus-thread-indent-array n
7426               (make-string (* n gnus-thread-indent-level) ? ))
7427         (setq n (1- n))))))
7428
7429 (defun gnus-summary-insert-line
7430   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7431                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7432                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7433   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7434          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7435          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7436          (gnus-tmp-score-char
7437           (if (or (null gnus-summary-default-score)
7438                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7439                       gnus-summary-zcore-fuzz)) ? 
7440             (if (< gnus-tmp-score gnus-summary-default-score)
7441                 gnus-score-below-mark gnus-score-over-mark)))
7442          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7443                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7444                                   gnus-cached-mark)
7445                                  (gnus-tmp-replied gnus-replied-mark)
7446                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7447                                   gnus-saved-mark)
7448                                  (t gnus-unread-mark)))
7449          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7450          (gnus-tmp-name
7451           (cond
7452            ((string-match "(.+)" gnus-tmp-from)
7453             (substring gnus-tmp-from
7454                        (1+ (match-beginning 0)) (1- (match-end 0))))
7455            ((string-match "<[^>]+> *$" gnus-tmp-from)
7456             (let ((beg (match-beginning 0)))
7457               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7458                        (substring gnus-tmp-from (1+ (match-beginning 0))
7459                                   (1- (match-end 0))))
7460                   (substring gnus-tmp-from 0 beg))))
7461            (t gnus-tmp-from)))
7462          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7463          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7464          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7465          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7466          (buffer-read-only nil))
7467     (when (string= gnus-tmp-name "")
7468       (setq gnus-tmp-name gnus-tmp-from))
7469     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7470     (put-text-property
7471      (point)
7472      (progn (eval gnus-summary-line-format-spec) (point))
7473      'gnus-number gnus-tmp-number)
7474     (when (gnus-visual-p 'summary-highlight 'highlight)
7475       (forward-line -1)
7476       (run-hooks 'gnus-summary-update-hook)
7477       (forward-line 1))))
7478
7479 (defun gnus-summary-update-line (&optional dont-update)
7480   ;; Update summary line after change.
7481   (when (and gnus-summary-default-score
7482              (not gnus-summary-inhibit-highlight))
7483     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7484            (article (gnus-summary-article-number))
7485            (score (gnus-summary-article-score article)))
7486       (unless dont-update
7487         (if (and gnus-summary-mark-below
7488                  (< (gnus-summary-article-score)
7489                     gnus-summary-mark-below))
7490             ;; This article has a low score, so we mark it as read.
7491             (when (memq article gnus-newsgroup-unreads)
7492               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7493           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7494             ;; This article was previously marked as read on account
7495             ;; of a low score, but now it has risen, so we mark it as
7496             ;; unread.
7497             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7498         (gnus-summary-update-mark
7499          (if (or (null gnus-summary-default-score)
7500                  (<= (abs (- score gnus-summary-default-score))
7501                      gnus-summary-zcore-fuzz)) ? 
7502            (if (< score gnus-summary-default-score)
7503                gnus-score-below-mark gnus-score-over-mark)) 'score))
7504       ;; Do visual highlighting.
7505       (when (gnus-visual-p 'summary-highlight 'highlight)
7506         (run-hooks 'gnus-summary-update-hook)))))
7507
7508 (defvar gnus-tmp-new-adopts nil)
7509
7510 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7511   ;; Sum up all elements (and sub-elements) in a list.
7512   (let* ((number
7513           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7514           (cond
7515            ((and (consp thread) (cdr thread))
7516             (apply
7517              '+ 1 (mapcar
7518                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7519            ((null thread)
7520             1)
7521            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7522             1)
7523            (t 1))))
7524     (when (and level (zerop level) gnus-tmp-new-adopts)
7525       (incf number
7526             (apply '+ (mapcar
7527                        'gnus-summary-number-of-articles-in-thread
7528                        gnus-tmp-new-adopts))))
7529     (if char
7530         (if (> number 1) gnus-not-empty-thread-mark
7531           gnus-empty-thread-mark)
7532       number)))
7533
7534 (defun gnus-summary-set-local-parameters (group)
7535  "Go through the local params of GROUP and set all variable specs in that list."
7536   (let ((params (gnus-info-params (gnus-get-info group)))
7537         elem)
7538     (while params
7539       (setq elem (car params)
7540             params (cdr params))
7541       (and (consp elem)                 ; Has to be a cons.
7542            (consp (cdr elem))           ; The cdr has to be a list.
7543            (symbolp (car elem))         ; Has to be a symbol in there.
7544            (not (memq (car elem) 
7545                       '(quit-config to-address to-list to-group)))
7546            (progn                       ; So we set it.
7547              (make-local-variable (car elem))
7548              (set (car elem) (eval (nth 1 elem))))))))
7549
7550 (defun gnus-summary-read-group (group &optional show-all no-article
7551                                       kill-buffer no-display)
7552   "Start reading news in newsgroup GROUP.
7553 If SHOW-ALL is non-nil, already read articles are also listed.
7554 If NO-ARTICLE is non-nil, no article is selected initially.
7555 If NO-DISPLAY, don't generate a summary buffer."
7556   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7557   (let* ((new-group (gnus-summary-setup-buffer group))
7558          (quit-config (gnus-group-quit-config group))
7559          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7560     (cond
7561      ;; This summary buffer exists already, so we just select it.
7562      ((not new-group)
7563       (gnus-set-global-variables)
7564       (when kill-buffer
7565         (gnus-kill-or-deaden-summary kill-buffer))
7566       (gnus-configure-windows 'summary 'force)
7567       (gnus-set-mode-line 'summary)
7568       (gnus-summary-position-point)
7569       (message "")
7570       t)
7571      ;; We couldn't select this group.
7572      ((null did-select)
7573       (when (and (eq major-mode 'gnus-summary-mode)
7574                  (not (equal (current-buffer) kill-buffer)))
7575         (kill-buffer (current-buffer))
7576         (if (not quit-config)
7577             (progn
7578               (set-buffer gnus-group-buffer)
7579               (gnus-group-jump-to-group group)
7580               (gnus-group-next-unread-group 1))
7581           (if (not (buffer-name (car quit-config)))
7582               (gnus-configure-windows 'group 'force)
7583             (set-buffer (car quit-config))
7584             (and (eq major-mode 'gnus-summary-mode)
7585                  (gnus-set-global-variables))
7586             (gnus-configure-windows (cdr quit-config)))))
7587       (gnus-message 3 "Can't select group")
7588       nil)
7589      ;; The user did a `C-g' while prompting for number of articles,
7590      ;; so we exit this group.
7591      ((eq did-select 'quit)
7592       (and (eq major-mode 'gnus-summary-mode)
7593            (not (equal (current-buffer) kill-buffer))
7594            (kill-buffer (current-buffer)))
7595       (when kill-buffer
7596         (gnus-kill-or-deaden-summary kill-buffer))
7597       (if (not quit-config)
7598           (progn
7599             (set-buffer gnus-group-buffer)
7600             (gnus-group-jump-to-group group)
7601             (gnus-group-next-unread-group 1)
7602             (gnus-configure-windows 'group 'force))
7603         (if (not (buffer-name (car quit-config)))
7604             (gnus-configure-windows 'group 'force)
7605           (set-buffer (car quit-config))
7606           (and (eq major-mode 'gnus-summary-mode)
7607                (gnus-set-global-variables))
7608           (gnus-configure-windows (cdr quit-config))))
7609       ;; Finally signal the quit.
7610       (signal 'quit nil))
7611      ;; The group was successfully selected.
7612      (t
7613       (gnus-set-global-variables)
7614       ;; Save the active value in effect when the group was entered.
7615       (setq gnus-newsgroup-active
7616             (gnus-copy-sequence
7617              (gnus-active gnus-newsgroup-name)))
7618       ;; You can change the summary buffer in some way with this hook.
7619       (run-hooks 'gnus-select-group-hook)
7620       ;; Set any local variables in the group parameters.
7621       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7622       (gnus-update-format-specifications)
7623       ;; Do score processing.
7624       (when gnus-use-scoring
7625         (gnus-possibly-score-headers))
7626       ;; Check whether to fill in the gaps in the threads.
7627       (when gnus-build-sparse-threads
7628         (gnus-build-sparse-threads))
7629       ;; Find the initial limit.
7630       (if gnus-show-threads
7631           (if show-all
7632               (let ((gnus-newsgroup-dormant nil))
7633                 (gnus-summary-initial-limit show-all))
7634             (gnus-summary-initial-limit show-all))
7635         (setq gnus-newsgroup-limit 
7636               (mapcar 
7637                (lambda (header) (mail-header-number header))
7638                gnus-newsgroup-headers)))
7639       ;; Generate the summary buffer.
7640       (unless no-display
7641         (gnus-summary-prepare))
7642       (when gnus-use-trees
7643         (gnus-tree-open group)
7644         (setq gnus-summary-highlight-line-function
7645               'gnus-tree-highlight-article))
7646       ;; If the summary buffer is empty, but there are some low-scored
7647       ;; articles or some excluded dormants, we include these in the
7648       ;; buffer.
7649       (when (and (zerop (buffer-size))
7650                  (not no-display))
7651         (cond (gnus-newsgroup-dormant
7652                (gnus-summary-limit-include-dormant))
7653               ((and gnus-newsgroup-scored show-all)
7654                (gnus-summary-limit-include-expunged))))
7655       ;; Function `gnus-apply-kill-file' must be called in this hook.
7656       (run-hooks 'gnus-apply-kill-hook)
7657       (if (and (zerop (buffer-size))
7658                (not no-display))
7659           (progn
7660             ;; This newsgroup is empty.
7661             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7662             (gnus-message 6 "No unread news")
7663             (when kill-buffer
7664               (gnus-kill-or-deaden-summary kill-buffer))
7665             ;; Return nil from this function.
7666             nil)
7667         ;; Hide conversation thread subtrees.  We cannot do this in
7668         ;; gnus-summary-prepare-hook since kill processing may not
7669         ;; work with hidden articles.
7670         (and gnus-show-threads
7671              gnus-thread-hide-subtree
7672              (gnus-summary-hide-all-threads))
7673         ;; Show first unread article if requested.
7674         (if (and (not no-article)
7675                  (not no-display)
7676                  gnus-newsgroup-unreads
7677                  gnus-auto-select-first)
7678             (unless (if (eq gnus-auto-select-first 'best)
7679                         (gnus-summary-best-unread-article)
7680                       (gnus-summary-first-unread-article))
7681               (gnus-configure-windows 'summary))
7682           ;; Don't select any articles, just move point to the first
7683           ;; article in the group.
7684           (goto-char (point-min))
7685           (gnus-summary-position-point)
7686           (gnus-set-mode-line 'summary)
7687           (gnus-configure-windows 'summary 'force))
7688         ;; If we are in async mode, we send some info to the backend.
7689         (when gnus-newsgroup-async
7690           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7691         (when kill-buffer
7692           (gnus-kill-or-deaden-summary kill-buffer))
7693         (when (get-buffer-window gnus-group-buffer t)
7694           ;; Gotta use windows, because recenter does wierd stuff if
7695           ;; the current buffer ain't the displayed window.
7696           (let ((owin (selected-window)))
7697             (select-window (get-buffer-window gnus-group-buffer t))
7698             (when (gnus-group-goto-group group)
7699               (recenter))
7700             (select-window owin))))
7701       ;; Mark this buffer as "prepared".
7702       (setq gnus-newsgroup-prepared t)
7703       t))))
7704
7705 (defun gnus-summary-prepare ()
7706   "Generate the summary buffer."
7707   (let ((buffer-read-only nil))
7708     (erase-buffer)
7709     (setq gnus-newsgroup-data nil
7710           gnus-newsgroup-data-reverse nil)
7711     (run-hooks 'gnus-summary-generate-hook)
7712     ;; Generate the buffer, either with threads or without.
7713     (when gnus-newsgroup-headers
7714       (gnus-summary-prepare-threads
7715        (if gnus-show-threads
7716            (gnus-sort-gathered-threads
7717             (funcall gnus-summary-thread-gathering-function
7718                      (gnus-sort-threads
7719                       (gnus-cut-threads (gnus-make-threads)))))
7720          ;; Unthreaded display.
7721          (gnus-sort-articles gnus-newsgroup-headers))))
7722     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7723     ;; Call hooks for modifying summary buffer.
7724     (goto-char (point-min))
7725     (run-hooks 'gnus-summary-prepare-hook)))
7726
7727 (defun gnus-gather-threads-by-subject (threads)
7728   "Gather threads by looking at Subject headers."
7729   (if (not gnus-summary-make-false-root)
7730       threads
7731     (let ((hashtb (gnus-make-hashtable 1023))
7732           (prev threads)
7733           (result threads)
7734           subject hthread whole-subject)
7735       (while threads
7736         (setq whole-subject (mail-header-subject (caar threads)))
7737         (setq subject
7738               (cond
7739                ;; Truncate the subject.
7740                ((numberp gnus-summary-gather-subject-limit)
7741                 (setq subject (gnus-simplify-subject-re whole-subject))
7742                 (if (> (length subject) gnus-summary-gather-subject-limit)
7743                     (substring subject 0 gnus-summary-gather-subject-limit)
7744                   subject))
7745                ;; Fuzzily simplify it.
7746                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7747                 (gnus-simplify-subject-fuzzy whole-subject))
7748                ;; Just remove the leading "Re:".
7749                (t
7750                 (gnus-simplify-subject-re whole-subject))))
7751
7752         (if (and gnus-summary-gather-exclude-subject
7753                  (string-match gnus-summary-gather-exclude-subject
7754                                subject))
7755             ()          ; We don't want to do anything with this article.
7756           ;; We simplify the subject before looking it up in the
7757           ;; hash table.
7758
7759           (if (setq hthread (gnus-gethash subject hashtb))
7760               (progn
7761                 ;; We enter a dummy root into the thread, if we
7762                 ;; haven't done that already.
7763                 (unless (stringp (caar hthread))
7764                   (setcar hthread (list whole-subject (car hthread))))
7765                 ;; We add this new gathered thread to this gathered
7766                 ;; thread.
7767                 (setcdr (car hthread)
7768                         (nconc (cdar hthread) (list (car threads))))
7769                 ;; Remove it from the list of threads.
7770                 (setcdr prev (cdr threads))
7771                 (setq threads prev))
7772             ;; Enter this thread into the hash table.
7773             (gnus-sethash subject threads hashtb)))
7774         (setq prev threads)
7775         (setq threads (cdr threads)))
7776       result)))
7777
7778 (defun gnus-gather-threads-by-references (threads)
7779   "Gather threads by looking at References headers."
7780   (let ((idhashtb (gnus-make-hashtable 1023))
7781         (thhashtb (gnus-make-hashtable 1023))
7782         (prev threads)
7783         (result threads)
7784         ids references id gthread gid entered)
7785     (while threads
7786       (when (setq references (mail-header-references (caar threads)))
7787         (setq id (mail-header-id (caar threads)))
7788         (setq ids (gnus-split-references references))
7789         (setq entered nil)
7790         (while ids
7791           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7792               (progn
7793                 (gnus-sethash (car ids) id idhashtb)
7794                 (gnus-sethash id threads thhashtb))
7795             (setq gthread (gnus-gethash gid thhashtb))
7796             (unless entered
7797               ;; We enter a dummy root into the thread, if we
7798               ;; haven't done that already.
7799               (unless (stringp (caar gthread))
7800                 (setcar gthread (list (mail-header-subject (caar gthread))
7801                                       (car gthread))))
7802               ;; We add this new gathered thread to this gathered
7803               ;; thread.
7804               (setcdr (car gthread)
7805                       (nconc (cdar gthread) (list (car threads)))))
7806             ;; Add it into the thread hash table.
7807             (gnus-sethash id gthread thhashtb)
7808             (setq entered t)
7809             ;; Remove it from the list of threads.
7810             (setcdr prev (cdr threads))
7811             (setq threads prev))
7812           (setq ids (cdr ids))))
7813       (setq prev threads)
7814       (setq threads (cdr threads)))
7815     result))
7816
7817 (defun gnus-sort-gathered-threads (threads)
7818   "Sort subtreads inside each gathered thread by article number."
7819   (let ((result threads))
7820     (while threads
7821       (when (stringp (caar threads))
7822         (setcdr (car threads)
7823                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7824       (setq threads (cdr threads)))
7825     result))
7826
7827 (defun gnus-make-threads ()
7828   "Go through the dependency hashtb and find the roots.  Return all threads."
7829   (let (threads)
7830     (mapatoms
7831      (lambda (refs)
7832        (unless (car (symbol-value refs))
7833          ;; These threads do not refer back to any other articles,
7834          ;; so they're roots.
7835          (setq threads (append (cdr (symbol-value refs)) threads))))
7836      gnus-newsgroup-dependencies)
7837     threads))
7838
7839 (defun gnus-build-sparse-threads ()
7840   (let ((headers gnus-newsgroup-headers)
7841         (deps gnus-newsgroup-dependencies)
7842         header references generation relations 
7843         cthread subject child end pthread relation)
7844     ;; First we create an alist of generations/relations, where 
7845     ;; generations is how much we trust the ralation, and the relation
7846     ;; is parent/child.
7847     (gnus-message 7 "Making sparse threads...")
7848     (save-excursion
7849       (nnheader-set-temp-buffer " *gnus sparse threads*")
7850       (while (setq header (pop headers))
7851         (when (and (setq references (mail-header-references header))
7852                    (not (string= references "")))
7853           (insert references)
7854           (setq child (mail-header-id header)
7855                 subject (mail-header-subject header))
7856           (setq generation 0)
7857           (while (search-backward ">" nil t)
7858             (setq end (1+ (point)))
7859             (when (search-backward "<" nil t)
7860               (push (list (incf generation) 
7861                           child (setq child (buffer-substring (point) end))
7862                           subject)
7863                     relations)))
7864           (push (list (1+ generation) child nil subject) relations)
7865           (erase-buffer)))
7866       (kill-buffer (current-buffer)))
7867     ;; Sort over trustworthiness.
7868     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7869     (while (setq relation (pop relations))
7870       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7871                 (unless (car (symbol-value cthread))
7872                   ;; Make this article the parent of these threads.
7873                   (setcar (symbol-value cthread)
7874                           (vector gnus-reffed-article-number 
7875                                   (cadddr relation) 
7876                                   "" ""
7877                                   (cadr relation) 
7878                                   (or (caddr relation) "") 0 0 "")))
7879               (set cthread (list (vector gnus-reffed-article-number
7880                                          (cadddr relation) 
7881                                          "" "" (cadr relation) 
7882                                          (or (caddr relation) "") 0 0 ""))))
7883         (push gnus-reffed-article-number gnus-newsgroup-limit)
7884         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7885         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7886               gnus-newsgroup-reads)
7887         (decf gnus-reffed-article-number)
7888         ;; Make this new thread the child of its parent.
7889         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7890             (setcdr (symbol-value pthread)
7891                     (nconc (cdr (symbol-value pthread))
7892                            (list (symbol-value cthread))))
7893           (set pthread (list nil (symbol-value cthread))))))
7894     (gnus-message 7 "Making sparse threads...done")))
7895
7896 (defun gnus-build-old-threads ()
7897   ;; Look at all the articles that refer back to old articles, and
7898   ;; fetch the headers for the articles that aren't there.  This will
7899   ;; build complete threads - if the roots haven't been expired by the
7900   ;; server, that is.
7901   (let (id heads)
7902     (mapatoms
7903      (lambda (refs)
7904        (when (not (car (symbol-value refs)))
7905          (setq heads (cdr (symbol-value refs)))
7906          (while heads
7907            (if (memq (mail-header-number (caar heads))
7908                      gnus-newsgroup-dormant)
7909                (setq heads (cdr heads))
7910              (setq id (symbol-name refs))
7911              (while (and (setq id (gnus-build-get-header id))
7912                          (not (car (gnus-gethash
7913                                     id gnus-newsgroup-dependencies)))))
7914              (setq heads nil)))))
7915      gnus-newsgroup-dependencies)))
7916
7917 (defun gnus-build-get-header (id)
7918   ;; Look through the buffer of NOV lines and find the header to
7919   ;; ID.  Enter this line into the dependencies hash table, and return
7920   ;; the id of the parent article (if any).
7921   (let ((deps gnus-newsgroup-dependencies)
7922         found header)
7923     (prog1
7924         (save-excursion
7925           (set-buffer nntp-server-buffer)
7926           (goto-char (point-min))
7927           (while (and (not found) (search-forward id nil t))
7928             (beginning-of-line)
7929             (setq found (looking-at
7930                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7931                                  (regexp-quote id))))
7932             (or found (beginning-of-line 2)))
7933           (when found
7934             (beginning-of-line)
7935             (and
7936              (setq header (gnus-nov-parse-line
7937                            (read (current-buffer)) deps))
7938              (gnus-parent-id (mail-header-references header)))))
7939       (when header
7940         (let ((number (mail-header-number header)))
7941           (push number gnus-newsgroup-limit)
7942           (push header gnus-newsgroup-headers)
7943           (if (memq number gnus-newsgroup-unselected)
7944               (progn
7945                 (push number gnus-newsgroup-unreads)
7946                 (setq gnus-newsgroup-unselected
7947                       (delq number gnus-newsgroup-unselected)))
7948             (push number gnus-newsgroup-ancient)))))))
7949
7950 (defun gnus-summary-update-article (article &optional header)
7951   "Update ARTICLE in the summary buffer."
7952   (set-buffer gnus-summary-buffer)
7953   (let* ((header (or header (gnus-summary-article-header article)))
7954          (id (mail-header-id header))
7955          (data (gnus-data-find article))
7956          (thread (gnus-id-to-thread id))
7957          (parent
7958           (gnus-id-to-thread (or (gnus-parent-id 
7959                                   (mail-header-references header))
7960                                  "tull")))
7961          (buffer-read-only nil)
7962          (old (car thread))
7963          (number (mail-header-number header))
7964          pos)
7965     (when thread
7966       (setcar thread nil)
7967       (when parent
7968         (delq thread parent))
7969       (if (gnus-summary-insert-subject id header)
7970           ;; Set the (possibly) new article number in the data structure.
7971           (gnus-data-set-number data (gnus-id-to-article id))
7972         (setcar thread old)
7973         nil))))
7974
7975 (defun gnus-rebuild-thread (id)
7976   "Rebuild the thread containing ID."
7977   (let ((buffer-read-only nil)
7978         current thread data)
7979     (if (not gnus-show-threads)
7980         (setq thread (list (car (gnus-id-to-thread id))))
7981       ;; Get the thread this article is part of.
7982       (setq thread (gnus-remove-thread id)))
7983     (setq current (save-excursion
7984                     (and (zerop (forward-line -1))
7985                          (gnus-summary-article-number))))
7986     ;; If this is a gathered thread, we have to go some re-gathering.
7987     (when (stringp (car thread))
7988       (let ((subject (car thread))
7989             roots thr)
7990         (setq thread (cdr thread))
7991         (while thread
7992           (unless (memq (setq thr (gnus-id-to-thread
7993                                       (gnus-root-id
7994                                        (mail-header-id (caar thread)))))
7995                         roots)
7996             (push thr roots))
7997           (setq thread (cdr thread)))
7998         ;; We now have all (unique) roots.
7999         (if (= (length roots) 1)
8000             ;; All the loose roots are now one solid root.
8001             (setq thread (car roots))
8002           (setq thread (cons subject (gnus-sort-threads roots))))))
8003     (let (threads)
8004       ;; We then insert this thread into the summary buffer.
8005       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8006         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8007         (setq data (nreverse gnus-newsgroup-data))
8008         (setq threads gnus-newsgroup-threads))
8009       ;; We splice the new data into the data structure.
8010       (gnus-data-enter-list current data)
8011       (gnus-data-compute-positions)
8012       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8013
8014 (defun gnus-id-to-thread (id)
8015   "Return the (sub-)thread where ID appears."
8016   (gnus-gethash id gnus-newsgroup-dependencies))
8017
8018 (defun gnus-id-to-article (id)
8019   "Return the article number of ID."
8020   (let ((thread (gnus-id-to-thread id)))
8021     (when thread
8022       (mail-header-number (car thread)))))
8023
8024 (defun gnus-id-to-header (id)
8025   "Return the article headers of ID."
8026   (car (gnus-id-to-thread id)))
8027
8028 (defun gnus-article-displayed-root-p (article)
8029   "Say whether ARTICLE is a root(ish) article."
8030   (let ((level (gnus-summary-thread-level article))
8031         particle)
8032     (cond 
8033      ((null level) nil)
8034      ((zerop level) t)
8035      ((and (= 1 level)
8036            (null (setq particle (gnus-id-to-article
8037                                  (gnus-parent-id 
8038                                   (mail-header-references 
8039                                    (gnus-summary-article-header article))))))
8040            (null (gnus-summary-thread-level particle)))))))
8041
8042 (defun gnus-root-id (id)
8043   "Return the id of the root of the thread where ID appears."
8044   (let (last-id prev)
8045     (while (and id (setq prev (car (gnus-gethash 
8046                                     id gnus-newsgroup-dependencies))))
8047       (setq last-id id
8048             id (gnus-parent-id (mail-header-references prev))))
8049     last-id))
8050
8051 (defun gnus-remove-thread (id &optional dont-remove)
8052   "Remove the thread that has ID in it."
8053   (let ((dep gnus-newsgroup-dependencies)
8054         headers thread last-id)
8055     ;; First go up in this thread until we find the root.
8056     (setq last-id (gnus-root-id id))
8057     (setq headers (list (car (gnus-id-to-thread last-id))
8058                         (caadr (gnus-id-to-thread last-id))))
8059     ;; We have now found the real root of this thread.  It might have
8060     ;; been gathered into some loose thread, so we have to search
8061     ;; through the threads to find the thread we wanted.
8062     (let ((threads gnus-newsgroup-threads)
8063           sub)
8064       (while threads
8065         (setq sub (car threads))
8066         (if (stringp (car sub))
8067             ;; This is a gathered threads, so we look at the roots
8068             ;; below it to find whether this article in in this
8069             ;; gathered root.
8070             (progn
8071               (setq sub (cdr sub))
8072               (while sub
8073                 (when (member (caar sub) headers)
8074                   (setq thread (car threads)
8075                         threads nil
8076                         sub nil))
8077                 (setq sub (cdr sub))))
8078           ;; It's an ordinary thread, so we check it.
8079           (when (eq (car sub) (car headers))
8080             (setq thread sub
8081                   threads nil)))
8082         (setq threads (cdr threads)))
8083       ;; If this article is in no thread, then it's a root.
8084       (if thread
8085           (unless dont-remove
8086             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8087         (setq thread (gnus-gethash last-id dep)))
8088       (when thread
8089         (prog1
8090             thread ; We return this thread.
8091           (unless dont-remove
8092             (if (stringp (car thread))
8093                 (progn
8094                   ;; If we use dummy roots, then we have to remove the
8095                   ;; dummy root as well.
8096                   (when (eq gnus-summary-make-false-root 'dummy)
8097                     ;; Uhm.
8098                     )
8099                   (setq thread (cdr thread))
8100                   (while thread
8101                     (gnus-remove-thread-1 (car thread))
8102                     (setq thread (cdr thread))))
8103               (gnus-remove-thread-1 thread))))))))
8104
8105 (defun gnus-remove-thread-1 (thread)
8106   "Remove the thread THREAD recursively."
8107   (let ((number (mail-header-number (car thread)))
8108         pos)
8109     (when (setq pos (text-property-any
8110                      (point-min) (point-max) 'gnus-number number))
8111       (goto-char pos)
8112       (gnus-delete-line)
8113       (gnus-data-remove number))
8114     (setq thread (cdr thread))
8115     (while thread
8116       (gnus-remove-thread-1 (pop thread)))))
8117
8118 (defun gnus-sort-threads (threads)
8119   "Sort THREADS."
8120   (if (not gnus-thread-sort-functions)
8121       threads
8122     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8123                     (car gnus-thread-sort-functions)
8124                   `(lambda (t1 t2)
8125                      ,(gnus-make-sort-function 
8126                        (reverse gnus-thread-sort-functions))))))
8127       (gnus-message 7 "Sorting threads...")
8128       (prog1
8129           (sort threads func)
8130         (gnus-message 7 "Sorting threads...done")))))
8131
8132 (defun gnus-sort-articles (articles)
8133   "Sort ARTICLES."
8134   (when gnus-article-sort-functions
8135     (let ((func (if (= 1 (length gnus-article-sort-functions))
8136                     (car gnus-article-sort-functions)
8137                   `(lambda (t1 t2)
8138                      ,(gnus-make-sort-function 
8139                        (reverse gnus-article-sort-functions))))))
8140       (gnus-message 7 "Sorting articles...")
8141       (prog1
8142           (setq gnus-newsgroup-headers (sort articles func))
8143         (gnus-message 7 "Sorting articles...done")))))
8144
8145 (defun gnus-make-sort-function (funs)
8146   "Return a composite sort condition based on the functions in FUNC."
8147   (if (cdr funs)
8148       `(or (,(car funs) t1 t2)
8149            (and (not (,(car funs) t2 t1))
8150                 ,(gnus-make-sort-function (cdr funs))))
8151     `(,(car funs) t1 t2)))
8152                  
8153 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8154 (defmacro gnus-thread-header (thread)
8155   ;; Return header of first article in THREAD.
8156   ;; Note that THREAD must never, ever be anything else than a variable -
8157   ;; using some other form will lead to serious barfage.
8158   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8159   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8160   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8161         (vector thread) 2))
8162
8163 (defsubst gnus-article-sort-by-number (h1 h2)
8164   "Sort articles by article number."
8165   (< (mail-header-number h1)
8166      (mail-header-number h2)))
8167
8168 (defun gnus-thread-sort-by-number (h1 h2)
8169   "Sort threads by root article number."
8170   (gnus-article-sort-by-number
8171    (gnus-thread-header h1) (gnus-thread-header h2)))
8172
8173 (defsubst gnus-article-sort-by-author (h1 h2)
8174   "Sort articles by root author."
8175   (string-lessp
8176    (let ((extract (funcall
8177                    gnus-extract-address-components
8178                    (mail-header-from h1))))
8179      (or (car extract) (cdr extract)))
8180    (let ((extract (funcall
8181                    gnus-extract-address-components
8182                    (mail-header-from h2))))
8183      (or (car extract) (cdr extract)))))
8184
8185 (defun gnus-thread-sort-by-author (h1 h2)
8186   "Sort threads by root author."
8187   (gnus-article-sort-by-author
8188    (gnus-thread-header h1)  (gnus-thread-header h2)))
8189
8190 (defsubst gnus-article-sort-by-subject (h1 h2)
8191   "Sort articles by root subject."
8192   (string-lessp
8193    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8194    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8195
8196 (defun gnus-thread-sort-by-subject (h1 h2)
8197   "Sort threads by root subject."
8198   (gnus-article-sort-by-subject
8199    (gnus-thread-header h1) (gnus-thread-header h2)))
8200
8201 (defsubst gnus-article-sort-by-date (h1 h2)
8202   "Sort articles by root article date."
8203   (string-lessp
8204    (gnus-sortable-date (mail-header-date h1))
8205    (gnus-sortable-date (mail-header-date h2))))
8206
8207 (defun gnus-thread-sort-by-date (h1 h2)
8208   "Sort threads by root article date."
8209   (gnus-article-sort-by-date
8210    (gnus-thread-header h1) (gnus-thread-header h2)))
8211
8212 (defsubst gnus-article-sort-by-score (h1 h2)
8213   "Sort articles by root article score.
8214 Unscored articles will be counted as having a score of zero."
8215   (> (or (cdr (assq (mail-header-number h1)
8216                     gnus-newsgroup-scored))
8217          gnus-summary-default-score 0)
8218      (or (cdr (assq (mail-header-number h2)
8219                     gnus-newsgroup-scored))
8220          gnus-summary-default-score 0)))
8221
8222 (defun gnus-thread-sort-by-score (h1 h2)
8223   "Sort threads by root article score."
8224   (gnus-article-sort-by-score
8225    (gnus-thread-header h1) (gnus-thread-header h2)))
8226
8227 (defun gnus-thread-sort-by-total-score (h1 h2)
8228   "Sort threads by the sum of all scores in the thread.
8229 Unscored articles will be counted as having a score of zero."
8230   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8231
8232 (defun gnus-thread-total-score (thread)
8233   ;;  This function find the total score of THREAD.
8234   (if (consp thread)
8235       (if (stringp (car thread))
8236           (apply gnus-thread-score-function 0
8237                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8238         (gnus-thread-total-score-1 thread))
8239     (gnus-thread-total-score-1 (list thread))))
8240
8241 (defun gnus-thread-total-score-1 (root)
8242   ;; This function find the total score of the thread below ROOT.
8243   (setq root (car root))
8244   (apply gnus-thread-score-function
8245          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8246              gnus-summary-default-score 0)
8247          (mapcar 'gnus-thread-total-score
8248                  (cdr (gnus-gethash (mail-header-id root)
8249                                     gnus-newsgroup-dependencies)))))
8250
8251 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8252 (defvar gnus-tmp-prev-subject nil)
8253 (defvar gnus-tmp-false-parent nil)
8254 (defvar gnus-tmp-root-expunged nil)
8255 (defvar gnus-tmp-dummy-line nil)
8256
8257 (defun gnus-summary-prepare-threads (threads)
8258   "Prepare summary buffer from THREADS and indentation LEVEL.
8259 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8260 or a straight list of headers."
8261   (gnus-message 7 "Generating summary...")
8262
8263   (setq gnus-newsgroup-threads threads)
8264   (beginning-of-line)
8265
8266   (let ((gnus-tmp-level 0)
8267         (default-score (or gnus-summary-default-score 0))
8268         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8269         thread number subject stack state gnus-tmp-gathered beg-match
8270         new-roots gnus-tmp-new-adopts thread-end
8271         gnus-tmp-header gnus-tmp-unread
8272         gnus-tmp-replied gnus-tmp-subject-or-nil
8273         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8274         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8275         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8276
8277     (setq gnus-tmp-prev-subject nil)
8278
8279     (if (vectorp (car threads))
8280         ;; If this is a straight (sic) list of headers, then a
8281         ;; threaded summary display isn't required, so we just create
8282         ;; an unthreaded one.
8283         (gnus-summary-prepare-unthreaded threads)
8284
8285       ;; Do the threaded display.
8286
8287       (while (or threads stack gnus-tmp-new-adopts new-roots)
8288
8289         (if (and (= gnus-tmp-level 0)
8290                  (not (setq gnus-tmp-dummy-line nil))
8291                  (or (not stack)
8292                      (= (caar stack) 0))
8293                  (not gnus-tmp-false-parent)
8294                  (or gnus-tmp-new-adopts new-roots))
8295             (if gnus-tmp-new-adopts
8296                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8297                       thread (list (car gnus-tmp-new-adopts))
8298                       gnus-tmp-header (caar thread)
8299                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8300               (if new-roots
8301                   (setq thread (list (car new-roots))
8302                         gnus-tmp-header (caar thread)
8303                         new-roots (cdr new-roots))))
8304
8305           (if threads
8306               ;; If there are some threads, we do them before the
8307               ;; threads on the stack.
8308               (setq thread threads
8309                     gnus-tmp-header (caar thread))
8310             ;; There were no current threads, so we pop something off
8311             ;; the stack.
8312             (setq state (car stack)
8313                   gnus-tmp-level (car state)
8314                   thread (cdr state)
8315                   stack (cdr stack)
8316                   gnus-tmp-header (caar thread))))
8317
8318         (setq gnus-tmp-false-parent nil)
8319         (setq gnus-tmp-root-expunged nil)
8320         (setq thread-end nil)
8321
8322         (if (stringp gnus-tmp-header)
8323             ;; The header is a dummy root.
8324             (cond
8325              ((eq gnus-summary-make-false-root 'adopt)
8326               ;; We let the first article adopt the rest.
8327               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8328                                                (cddar thread)))
8329               (setq gnus-tmp-gathered
8330                     (nconc (mapcar
8331                             (lambda (h) (mail-header-number (car h)))
8332                             (cddar thread))
8333                            gnus-tmp-gathered))
8334               (setq thread (cons (list (caar thread)
8335                                        (cadar thread))
8336                                  (cdr thread)))
8337               (setq gnus-tmp-level -1
8338                     gnus-tmp-false-parent t))
8339              ((eq gnus-summary-make-false-root 'empty)
8340               ;; We print adopted articles with empty subject fields.
8341               (setq gnus-tmp-gathered
8342                     (nconc (mapcar
8343                             (lambda (h) (mail-header-number (car h)))
8344                             (cddar thread))
8345                            gnus-tmp-gathered))
8346               (setq gnus-tmp-level -1))
8347              ((eq gnus-summary-make-false-root 'dummy)
8348               ;; We remember that we probably want to output a dummy
8349               ;; root.
8350               (setq gnus-tmp-dummy-line gnus-tmp-header)
8351               (setq gnus-tmp-prev-subject gnus-tmp-header))
8352              (t
8353               ;; We do not make a root for the gathered
8354               ;; sub-threads at all.
8355               (setq gnus-tmp-level -1)))
8356
8357           (setq number (mail-header-number gnus-tmp-header)
8358                 subject (mail-header-subject gnus-tmp-header))
8359
8360           (cond
8361            ;; If the thread has changed subject, we might want to make
8362            ;; this subthread into a root.
8363            ((and (null gnus-thread-ignore-subject)
8364                  (not (zerop gnus-tmp-level))
8365                  gnus-tmp-prev-subject
8366                  (not (inline
8367                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8368             (setq new-roots (nconc new-roots (list (car thread)))
8369                   thread-end t
8370                   gnus-tmp-header nil))
8371            ;; If the article lies outside the current limit,
8372            ;; then we do not display it.
8373            ((and (not (memq number gnus-newsgroup-limit))
8374                  (not gnus-tmp-dummy-line))
8375             (setq gnus-tmp-gathered
8376                   (nconc (mapcar
8377                           (lambda (h) (mail-header-number (car h)))
8378                           (cdar thread))
8379                          gnus-tmp-gathered))
8380             (setq gnus-tmp-new-adopts (if (cdar thread)
8381                                           (append gnus-tmp-new-adopts
8382                                                   (cdar thread))
8383                                         gnus-tmp-new-adopts)
8384                   thread-end t
8385                   gnus-tmp-header nil)
8386             (when (zerop gnus-tmp-level)
8387               (setq gnus-tmp-root-expunged t)))
8388            ;; Perhaps this article is to be marked as read?
8389            ((and gnus-summary-mark-below
8390                  (< (or (cdr (assq number gnus-newsgroup-scored))
8391                         default-score)
8392                     gnus-summary-mark-below)
8393                  ;; Don't touch sparse articles.
8394                  (not (memq number gnus-newsgroup-sparse)))
8395             (setq gnus-newsgroup-unreads
8396                   (delq number gnus-newsgroup-unreads))
8397             (if gnus-newsgroup-auto-expire
8398                 (push number gnus-newsgroup-expirable)
8399               (push (cons number gnus-low-score-mark)
8400                     gnus-newsgroup-reads))))
8401
8402           (when gnus-tmp-header
8403             ;; We may have an old dummy line to output before this
8404             ;; article.
8405             (when gnus-tmp-dummy-line
8406               (gnus-summary-insert-dummy-line
8407                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8408               (setq gnus-tmp-dummy-line nil))
8409
8410             ;; Compute the mark.
8411             (setq
8412              gnus-tmp-unread
8413              (cond
8414               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8415               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8416               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8417               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8418               (t (or (cdr (assq number gnus-newsgroup-reads))
8419                      gnus-ancient-mark))))
8420
8421             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8422                                   gnus-tmp-header gnus-tmp-level)
8423                   gnus-newsgroup-data)
8424
8425             ;; Actually insert the line.
8426             (setq
8427              gnus-tmp-subject-or-nil
8428              (cond
8429               ((and gnus-thread-ignore-subject
8430                     gnus-tmp-prev-subject
8431                     (not (inline (gnus-subject-equal
8432                                   gnus-tmp-prev-subject subject))))
8433                subject)
8434               ((zerop gnus-tmp-level)
8435                (if (and (eq gnus-summary-make-false-root 'empty)
8436                         (memq number gnus-tmp-gathered)
8437                         gnus-tmp-prev-subject
8438                         (inline (gnus-subject-equal
8439                                  gnus-tmp-prev-subject subject)))
8440                    gnus-summary-same-subject
8441                  subject))
8442               (t gnus-summary-same-subject)))
8443             (if (and (eq gnus-summary-make-false-root 'adopt)
8444                      (= gnus-tmp-level 1)
8445                      (memq number gnus-tmp-gathered))
8446                 (setq gnus-tmp-opening-bracket ?\<
8447                       gnus-tmp-closing-bracket ?\>)
8448               (setq gnus-tmp-opening-bracket ?\[
8449                     gnus-tmp-closing-bracket ?\]))
8450             (setq
8451              gnus-tmp-indentation
8452              (aref gnus-thread-indent-array gnus-tmp-level)
8453              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8454              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8455                                 gnus-summary-default-score 0)
8456              gnus-tmp-score-char
8457              (if (or (null gnus-summary-default-score)
8458                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8459                          gnus-summary-zcore-fuzz)) ? 
8460                (if (< gnus-tmp-score gnus-summary-default-score)
8461                    gnus-score-below-mark gnus-score-over-mark))
8462              gnus-tmp-replied
8463              (cond ((memq number gnus-newsgroup-processable)
8464                     gnus-process-mark)
8465                    ((memq number gnus-newsgroup-cached)
8466                     gnus-cached-mark)
8467                    ((memq number gnus-newsgroup-replied)
8468                     gnus-replied-mark)
8469                    (t gnus-unread-mark))
8470              gnus-tmp-from (mail-header-from gnus-tmp-header)
8471              gnus-tmp-name
8472              (cond
8473               ((string-match "(.+)" gnus-tmp-from)
8474                (substring gnus-tmp-from
8475                           (1+ (match-beginning 0)) (1- (match-end 0))))
8476               ((string-match "<[^>]+> *$" gnus-tmp-from)
8477                (setq beg-match (match-beginning 0))
8478                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8479                         (substring gnus-tmp-from (1+ (match-beginning 0))
8480                                    (1- (match-end 0))))
8481                    (substring gnus-tmp-from 0 beg-match)))
8482               (t gnus-tmp-from)))
8483             (when (string= gnus-tmp-name "")
8484               (setq gnus-tmp-name gnus-tmp-from))
8485             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8486             (put-text-property
8487              (point)
8488              (progn (eval gnus-summary-line-format-spec) (point))
8489              'gnus-number number)
8490             (when gnus-visual-p
8491               (forward-line -1)
8492               (run-hooks 'gnus-summary-update-hook)
8493               (forward-line 1))
8494
8495             (setq gnus-tmp-prev-subject subject)))
8496
8497         (when (nth 1 thread)
8498           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8499         (incf gnus-tmp-level)
8500         (setq threads (if thread-end nil (cdar thread)))
8501         (unless threads
8502           (setq gnus-tmp-level 0)))))
8503   (gnus-message 7 "Generating summary...done"))
8504
8505 (defun gnus-summary-prepare-unthreaded (headers)
8506   "Generate an unthreaded summary buffer based on HEADERS."
8507   (let (header number mark)
8508
8509     (while headers
8510       ;; We may have to root out some bad articles...
8511       (when (memq (setq number (mail-header-number
8512                                 (setq header (pop headers))))
8513                   gnus-newsgroup-limit)
8514         ;; Mark article as read when it has a low score.
8515         (when (and gnus-summary-mark-below
8516                    (< (or (cdr (assq number gnus-newsgroup-scored))
8517                           gnus-summary-default-score 0)
8518                       gnus-summary-mark-below))
8519           (setq gnus-newsgroup-unreads
8520                 (delq number gnus-newsgroup-unreads))
8521           (if gnus-newsgroup-auto-expire
8522               (push number gnus-newsgroup-expirable)
8523             (push (cons number gnus-low-score-mark)
8524                   gnus-newsgroup-reads)))
8525
8526         (setq mark
8527               (cond
8528                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8529                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8530                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8531                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8532                (t (or (cdr (assq number gnus-newsgroup-reads))
8533                       gnus-ancient-mark))))
8534         (setq gnus-newsgroup-data
8535               (cons (gnus-data-make number mark (1+ (point)) header 0)
8536                     gnus-newsgroup-data))
8537         (gnus-summary-insert-line
8538          header 0 nil mark (memq number gnus-newsgroup-replied)
8539          (memq number gnus-newsgroup-expirable)
8540          (mail-header-subject header) nil
8541          (cdr (assq number gnus-newsgroup-scored))
8542          (memq number gnus-newsgroup-processable))))))
8543
8544 (defun gnus-select-newsgroup (group &optional read-all)
8545   "Select newsgroup GROUP.
8546 If READ-ALL is non-nil, all articles in the group are selected."
8547   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8548          (info (nth 2 entry))
8549          articles fetched-articles cached)
8550
8551     (or (gnus-check-server
8552          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8553         (error "Couldn't open server"))
8554
8555     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8556         (gnus-activate-group group) ; Or we can activate it...
8557         (progn ; Or we bug out.
8558           (when (equal major-mode 'gnus-summary-mode)
8559             (kill-buffer (current-buffer)))
8560           (error "Couldn't request group %s: %s"
8561                  group (gnus-status-message group))))
8562
8563     (setq gnus-newsgroup-name group)
8564     (setq gnus-newsgroup-unselected nil)
8565     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8566
8567     (and gnus-asynchronous
8568          (gnus-check-backend-function
8569           'request-asynchronous gnus-newsgroup-name)
8570          (setq gnus-newsgroup-async
8571                (gnus-request-asynchronous gnus-newsgroup-name)))
8572
8573     ;; Adjust and set lists of article marks.
8574     (when info
8575       (gnus-adjust-marked-articles info))
8576
8577     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8578     (when (gnus-virtual-group-p group)
8579       (setq cached gnus-newsgroup-cached))
8580
8581     (setq gnus-newsgroup-unreads
8582           (gnus-set-difference
8583            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8584            gnus-newsgroup-dormant))
8585
8586     (setq gnus-newsgroup-processable nil)
8587
8588     (setq articles (gnus-articles-to-read group read-all))
8589
8590     (cond
8591      ((null articles)
8592       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8593       'quit)
8594      ((eq articles 0) nil)
8595      (t
8596       ;; Init the dependencies hash table.
8597       (setq gnus-newsgroup-dependencies
8598             (gnus-make-hashtable (length articles)))
8599       ;; Retrieve the headers and read them in.
8600       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8601       (setq gnus-newsgroup-headers
8602             (if (eq 'nov
8603                     (setq gnus-headers-retrieved-by
8604                           (gnus-retrieve-headers
8605                            articles gnus-newsgroup-name
8606                            ;; We might want to fetch old headers, but
8607                            ;; not if there is only 1 article.
8608                            (and gnus-fetch-old-headers
8609                                 (or (and
8610                                      (not (eq gnus-fetch-old-headers 'some))
8611                                      (not (numberp gnus-fetch-old-headers)))
8612                                     (> (length articles) 1))))))
8613                 (gnus-get-newsgroup-headers-xover articles)
8614               (gnus-get-newsgroup-headers)))
8615       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8616
8617       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8618       (when cached
8619         (setq gnus-newsgroup-cached cached))
8620
8621       ;; Set the initial limit.
8622       (setq gnus-newsgroup-limit (copy-sequence articles))
8623       ;; Remove canceled articles from the list of unread articles.
8624       (setq gnus-newsgroup-unreads
8625             (gnus-set-sorted-intersection
8626              gnus-newsgroup-unreads
8627              (setq fetched-articles
8628                    (mapcar (lambda (headers) (mail-header-number headers))
8629                            gnus-newsgroup-headers))))
8630       ;; Removed marked articles that do not exist.
8631       (gnus-update-missing-marks
8632        (gnus-sorted-complement fetched-articles articles))
8633       ;; We might want to build some more threads first.
8634       (and gnus-fetch-old-headers
8635            (eq gnus-headers-retrieved-by 'nov)
8636            (gnus-build-old-threads))
8637       ;; Check whether auto-expire is to be done in this group.
8638       (setq gnus-newsgroup-auto-expire
8639             (gnus-group-auto-expirable-p group))
8640       ;; Set up the article buffer now, if necessary.
8641       (unless gnus-single-article-buffer
8642         (gnus-article-setup-buffer))
8643       ;; First and last article in this newsgroup.
8644       (and gnus-newsgroup-headers
8645            (setq gnus-newsgroup-begin
8646                  (mail-header-number (car gnus-newsgroup-headers)))
8647            (setq gnus-newsgroup-end
8648                  (mail-header-number
8649                   (gnus-last-element gnus-newsgroup-headers))))
8650       (setq gnus-reffed-article-number -1)
8651       ;; GROUP is successfully selected.
8652       (or gnus-newsgroup-headers t)))))
8653
8654 (defun gnus-articles-to-read (group read-all)
8655   ;; Find out what articles the user wants to read.
8656   (let* ((articles
8657           ;; Select all articles if `read-all' is non-nil, or if there
8658           ;; are no unread articles.
8659           (if (or read-all
8660                   (and (zerop (length gnus-newsgroup-marked))
8661                        (zerop (length gnus-newsgroup-unreads))))
8662               (gnus-uncompress-range (gnus-active group))
8663             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8664                           (copy-sequence gnus-newsgroup-unreads))
8665                   '<)))
8666          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8667          (scored (length scored-list))
8668          (number (length articles))
8669          (marked (+ (length gnus-newsgroup-marked)
8670                     (length gnus-newsgroup-dormant)))
8671          (select
8672           (cond
8673            ((numberp read-all)
8674             read-all)
8675            (t
8676             (condition-case ()
8677                 (cond
8678                  ((and (or (<= scored marked) (= scored number))
8679                        (numberp gnus-large-newsgroup)
8680                        (> number gnus-large-newsgroup))
8681                   (let ((input
8682                          (read-string
8683                           (format
8684                            "How many articles from %s (default %d): "
8685                            gnus-newsgroup-name number))))
8686                     (if (string-match "^[ \t]*$" input) number input)))
8687                  ((and (> scored marked) (< scored number))
8688                   (let ((input
8689                          (read-string
8690                           (format "%s %s (%d scored, %d total): "
8691                                   "How many articles from"
8692                                   group scored number))))
8693                     (if (string-match "^[ \t]*$" input)
8694                         number input)))
8695                  (t number))
8696               (quit nil))))))
8697     (setq select (if (stringp select) (string-to-number select) select))
8698     (if (or (null select) (zerop select))
8699         select
8700       (if (and (not (zerop scored)) (<= (abs select) scored))
8701           (progn
8702             (setq articles (sort scored-list '<))
8703             (setq number (length articles)))
8704         (setq articles (copy-sequence articles)))
8705
8706       (if (< (abs select) number)
8707           (if (< select 0)
8708               ;; Select the N oldest articles.
8709               (setcdr (nthcdr (1- (abs select)) articles) nil)
8710             ;; Select the N most recent articles.
8711             (setq articles (nthcdr (- number select) articles))))
8712       (setq gnus-newsgroup-unselected
8713             (gnus-sorted-intersection
8714              gnus-newsgroup-unreads
8715              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8716       articles)))
8717
8718 (defun gnus-killed-articles (killed articles)
8719   (let (out)
8720     (while articles
8721       (if (inline (gnus-member-of-range (car articles) killed))
8722           (setq out (cons (car articles) out)))
8723       (setq articles (cdr articles)))
8724     out))
8725
8726 (defun gnus-uncompress-marks (marks)
8727   "Uncompress the mark ranges in MARKS."
8728   (let ((uncompressed '(score bookmark))
8729         out)
8730     (while marks
8731       (if (memq (caar marks) uncompressed)
8732           (push (car marks) out)
8733         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8734       (setq marks (cdr marks)))
8735     out))
8736
8737 (defun gnus-adjust-marked-articles (info)
8738   "Set all article lists and remove all marks that are no longer legal."
8739   (let* ((marked-lists (gnus-info-marks info))
8740          (active (gnus-active (gnus-info-group info)))
8741          (min (car active))
8742          (max (cdr active))
8743          (types gnus-article-mark-lists)
8744          (uncompressed '(score bookmark))
8745          marks var articles article mark)
8746
8747     (while marked-lists
8748       (setq marks (pop marked-lists))
8749       (set (setq var (intern (format "gnus-newsgroup-%s"
8750                                      (car (rassq (setq mark (car marks))
8751                                                  types)))))
8752            (if (memq (car marks) uncompressed) (cdr marks)
8753              (gnus-uncompress-range (cdr marks))))
8754
8755       (setq articles (symbol-value var))
8756
8757       ;; All articles have to be subsets of the active articles.
8758       (cond
8759        ;; Adjust "simple" lists.
8760        ((memq mark '(tick dormant expirable reply killed save))
8761         (while articles
8762           (when (or (< (setq article (pop articles)) min) (> article max))
8763             (set var (delq article (symbol-value var))))))
8764        ;; Adjust assocs.
8765        ((memq mark '(score bookmark))
8766         (while articles
8767           (when (or (< (car (setq article (pop articles))) min)
8768                     (> (car article) max))
8769             (set var (delq article (symbol-value var))))))))))
8770
8771 (defun gnus-update-missing-marks (missing)
8772   "Go through the list of MISSING articles and remove them mark lists."
8773   (when missing
8774     (let ((types gnus-article-mark-lists)
8775           var m)
8776       ;; Go through all types.
8777       (while types
8778         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8779         (when (symbol-value var)
8780           ;; This list has articles.  So we delete all missing articles
8781           ;; from it.
8782           (setq m missing)
8783           (while m
8784             (set var (delq (pop m) (symbol-value var)))))))))
8785
8786 (defun gnus-update-marks ()
8787   "Enter the various lists of marked articles into the newsgroup info list."
8788   (let ((types gnus-article-mark-lists)
8789         (info (gnus-get-info gnus-newsgroup-name))
8790         (uncompressed '(score bookmark killed))
8791         type list newmarked symbol)
8792     (when info
8793       ;; Add all marks lists that are non-nil to the list of marks lists.
8794       (while types
8795         (setq type (pop types))
8796         (when (setq list (symbol-value
8797                           (setq symbol
8798                                 (intern (format "gnus-newsgroup-%s"
8799                                                 (car type))))))
8800           (push (cons (cdr type)
8801                       (if (memq (cdr type) uncompressed) list
8802                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8803                 newmarked)))
8804
8805       ;; Enter these new marks into the info of the group.
8806       (if (nthcdr 3 info)
8807           (setcar (nthcdr 3 info) newmarked)
8808         ;; Add the marks lists to the end of the info.
8809         (when newmarked
8810           (setcdr (nthcdr 2 info) (list newmarked))))
8811
8812       ;; Cut off the end of the info if there's nothing else there.
8813       (let ((i 5))
8814         (while (and (> i 2)
8815                     (not (nth i info)))
8816           (when (nthcdr (decf i) info)
8817             (setcdr (nthcdr i info) nil)))))))
8818
8819 (defun gnus-add-marked-articles (group type articles &optional info force)
8820   ;; Add ARTICLES of TYPE to the info of GROUP.
8821   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8822   ;; add, but replace marked articles of TYPE with ARTICLES.
8823   (let ((info (or info (gnus-get-info group)))
8824         (uncompressed '(score bookmark killed))
8825         marked m)
8826     (or (not info)
8827         (and (not (setq marked (nthcdr 3 info)))
8828              (or (null articles)
8829                  (setcdr (nthcdr 2 info)
8830                          (list (list (cons type (gnus-compress-sequence
8831                                                  articles t)))))))
8832         (and (not (setq m (assq type (car marked))))
8833              (or (null articles)
8834                  (setcar marked
8835                          (cons (cons type (gnus-compress-sequence articles t) )
8836                                (car marked)))))
8837         (if force
8838             (if (null articles)
8839                 (setcar (nthcdr 3 info)
8840                         (delq (assq type (car marked)) (car marked)))
8841               (setcdr m (gnus-compress-sequence articles t)))
8842           (setcdr m (gnus-compress-sequence
8843                      (sort (nconc (gnus-uncompress-range (cdr m))
8844                                   (copy-sequence articles)) '<) t))))))
8845
8846 (defun gnus-set-mode-line (where)
8847   "This function sets the mode line of the article or summary buffers.
8848 If WHERE is `summary', the summary mode line format will be used."
8849   ;; Is this mode line one we keep updated?
8850   (when (memq where gnus-updated-mode-lines)
8851     (let (mode-string)
8852       (save-excursion
8853         ;; We evaluate this in the summary buffer since these
8854         ;; variables are buffer-local to that buffer.
8855         (set-buffer gnus-summary-buffer)
8856         ;; We bind all these variables that are used in the `eval' form
8857         ;; below.
8858         (let* ((mformat (symbol-value
8859                          (intern
8860                           (format "gnus-%s-mode-line-format-spec" where))))
8861                (gnus-tmp-group-name gnus-newsgroup-name)
8862                (gnus-tmp-article-number (or gnus-current-article 0))
8863                (gnus-tmp-unread gnus-newsgroup-unreads)
8864                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8865                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8866                (gnus-tmp-unread-and-unselected
8867                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8868                             (zerop gnus-tmp-unselected)) "")
8869                       ((zerop gnus-tmp-unselected)
8870                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8871                       (t (format "{%d(+%d) more}"
8872                                  gnus-tmp-unread-and-unticked
8873                                  gnus-tmp-unselected))))
8874                (gnus-tmp-subject
8875                 (if (and gnus-current-headers
8876                          (vectorp gnus-current-headers))
8877                     (mail-header-subject gnus-current-headers) ""))
8878                max-len
8879                gnus-tmp-header);; passed as argument to any user-format-funcs
8880           (setq mode-string (eval mformat))
8881           (setq max-len (max 4 (if gnus-mode-non-string-length
8882                                    (- (frame-width)
8883                                       gnus-mode-non-string-length)
8884                                  (length mode-string))))
8885           ;; We might have to chop a bit of the string off...
8886           (when (> (length mode-string) max-len)
8887             (setq mode-string
8888                   (concat (gnus-truncate-string mode-string (- max-len 3))
8889                           "...")))
8890           ;; Pad the mode string a bit.
8891           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8892       ;; Update the mode line.
8893       (setq mode-line-buffer-identification (list mode-string))
8894       (set-buffer-modified-p t))))
8895
8896 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8897   "Go through the HEADERS list and add all Xrefs to a hash table.
8898 The resulting hash table is returned, or nil if no Xrefs were found."
8899   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8900          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8901          (xref-hashtb (make-vector 63 0))
8902          start group entry number xrefs header)
8903     (while headers
8904       (setq header (pop headers))
8905       (when (and (setq xrefs (mail-header-xref header))
8906                  (not (memq (setq number (mail-header-number header))
8907                             unreads)))
8908         (setq start 0)
8909         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8910           (setq start (match-end 0))
8911           (setq group (if prefix
8912                           (concat prefix (substring xrefs (match-beginning 1)
8913                                                     (match-end 1)))
8914                         (substring xrefs (match-beginning 1) (match-end 1))))
8915           (setq number
8916                 (string-to-int (substring xrefs (match-beginning 2)
8917                                           (match-end 2))))
8918           (if (setq entry (gnus-gethash group xref-hashtb))
8919               (setcdr entry (cons number (cdr entry)))
8920             (gnus-sethash group (cons number nil) xref-hashtb)))))
8921     (and start xref-hashtb)))
8922
8923 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8924   "Look through all the headers and mark the Xrefs as read."
8925   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8926         name entry info xref-hashtb idlist method nth4)
8927     (save-excursion
8928       (set-buffer gnus-group-buffer)
8929       (when (setq xref-hashtb
8930                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8931         (mapatoms
8932          (lambda (group)
8933            (unless (string= from-newsgroup (setq name (symbol-name group)))
8934              (setq idlist (symbol-value group))
8935              ;; Dead groups are not updated.
8936              (and (prog1
8937                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8938                             info (nth 2 entry))
8939                     (if (stringp (setq nth4 (gnus-info-method info)))
8940                         (setq nth4 (gnus-server-to-method nth4))))
8941                   ;; Only do the xrefs if the group has the same
8942                   ;; select method as the group we have just read.
8943                   (or (gnus-methods-equal-p
8944                        nth4 (gnus-find-method-for-group from-newsgroup))
8945                       virtual
8946                       (equal nth4 (setq method (gnus-find-method-for-group
8947                                                 from-newsgroup)))
8948                       (and (equal (car nth4) (car method))
8949                            (equal (nth 1 nth4) (nth 1 method))))
8950                   gnus-use-cross-reference
8951                   (or (not (eq gnus-use-cross-reference t))
8952                       virtual
8953                       ;; Only do cross-references on subscribed
8954                       ;; groups, if that is what is wanted.
8955                       (<= (gnus-info-level info) gnus-level-subscribed))
8956                   (gnus-group-make-articles-read name idlist))))
8957          xref-hashtb)))))
8958
8959 (defun gnus-group-make-articles-read (group articles)
8960   (let* ((num 0)
8961          (entry (gnus-gethash group gnus-newsrc-hashtb))
8962          (info (nth 2 entry))
8963          (active (gnus-active group))
8964          range)
8965     ;; First peel off all illegal article numbers.
8966     (if active
8967         (let ((ids articles)
8968               id first)
8969           (while ids
8970             (setq id (car ids))
8971             (if (and first (> id (cdr active)))
8972                 (progn
8973                   ;; We'll end up in this situation in one particular
8974                   ;; obscure situation.  If you re-scan a group and get
8975                   ;; a new article that is cross-posted to a different
8976                   ;; group that has not been re-scanned, you might get
8977                   ;; crossposted article that has a higher number than
8978                   ;; Gnus believes possible.  So we re-activate this
8979                   ;; group as well.  This might mean doing the
8980                   ;; crossposting thingy will *increase* the number
8981                   ;; of articles in some groups.  Tsk, tsk.
8982                   (setq active (or (gnus-activate-group group) active))))
8983             (if (or (> id (cdr active))
8984                     (< id (car active)))
8985                 (setq articles (delq id articles)))
8986             (setq ids (cdr ids)))))
8987     ;; If the read list is nil, we init it.
8988     (and active
8989          (null (gnus-info-read info))
8990          (> (car active) 1)
8991          (gnus-info-set-read info (cons 1 (1- (car active)))))
8992     ;; Then we add the read articles to the range.
8993     (gnus-info-set-read
8994      info
8995      (setq range
8996            (gnus-add-to-range
8997             (gnus-info-read info) (setq articles (sort articles '<)))))
8998     ;; Then we have to re-compute how many unread
8999     ;; articles there are in this group.
9000     (if active
9001         (progn
9002           (cond
9003            ((not range)
9004             (setq num (- (1+ (cdr active)) (car active))))
9005            ((not (listp (cdr range)))
9006             (setq num (- (cdr active) (- (1+ (cdr range))
9007                                          (car range)))))
9008            (t
9009             (while range
9010               (if (numberp (car range))
9011                   (setq num (1+ num))
9012                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9013               (setq range (cdr range)))
9014             (setq num (- (cdr active) num))))
9015           ;; Update the number of unread articles.
9016           (setcar entry num)
9017           ;; Update the group buffer.
9018           (gnus-group-update-group group t)))))
9019
9020 (defun gnus-methods-equal-p (m1 m2)
9021   (let ((m1 (or m1 gnus-select-method))
9022         (m2 (or m2 gnus-select-method)))
9023     (or (equal m1 m2)
9024         (and (eq (car m1) (car m2))
9025              (or (not (memq 'address (assoc (symbol-name (car m1))
9026                                             gnus-valid-select-methods)))
9027                  (equal (nth 1 m1) (nth 1 m2)))))))
9028
9029 (defsubst gnus-header-value ()
9030   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9031
9032 (defvar gnus-newsgroup-none-id 0)
9033
9034 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9035   (let ((cur nntp-server-buffer)
9036         (dependencies
9037          (or dependencies
9038              (save-excursion (set-buffer gnus-summary-buffer)
9039                              gnus-newsgroup-dependencies)))
9040         headers id id-dep ref-dep end ref)
9041     (save-excursion
9042       (set-buffer nntp-server-buffer)
9043       (run-hooks 'gnus-parse-headers-hook)
9044       (let ((case-fold-search t)
9045             in-reply-to header p lines)
9046         (goto-char (point-min))
9047         ;; Search to the beginning of the next header.  Error messages
9048         ;; do not begin with 2 or 3.
9049         (while (re-search-forward "^[23][0-9]+ " nil t)
9050           (setq id nil
9051                 ref nil)
9052           ;; This implementation of this function, with nine
9053           ;; search-forwards instead of the one re-search-forward and
9054           ;; a case (which basically was the old function) is actually
9055           ;; about twice as fast, even though it looks messier.  You
9056           ;; can't have everything, I guess.  Speed and elegance
9057           ;; doesn't always go hand in hand.
9058           (setq
9059            header
9060            (vector
9061             ;; Number.
9062             (prog1
9063                 (read cur)
9064               (end-of-line)
9065               (setq p (point))
9066               (narrow-to-region (point)
9067                                 (or (and (search-forward "\n.\n" nil t)
9068                                          (- (point) 2))
9069                                     (point))))
9070             ;; Subject.
9071             (progn
9072               (goto-char p)
9073               (if (search-forward "\nsubject: " nil t)
9074                   (gnus-header-value) "(none)"))
9075             ;; From.
9076             (progn
9077               (goto-char p)
9078               (if (search-forward "\nfrom: " nil t)
9079                   (gnus-header-value) "(nobody)"))
9080             ;; Date.
9081             (progn
9082               (goto-char p)
9083               (if (search-forward "\ndate: " nil t)
9084                   (gnus-header-value) ""))
9085             ;; Message-ID.
9086             (progn
9087               (goto-char p)
9088               (if (search-forward "\nmessage-id: " nil t)
9089                   (setq id (gnus-header-value))
9090                 ;; If there was no message-id, we just fake one to make
9091                 ;; subsequent routines simpler.
9092                 (setq id (concat "none+"
9093                                  (int-to-string
9094                                   (setq gnus-newsgroup-none-id
9095                                         (1+ gnus-newsgroup-none-id)))))))
9096             ;; References.
9097             (progn
9098               (goto-char p)
9099               (if (search-forward "\nreferences: " nil t)
9100                   (prog1
9101                       (gnus-header-value)
9102                     (setq end (match-end 0))
9103                     (save-excursion
9104                       (setq ref
9105                             (buffer-substring
9106                              (progn
9107                                (end-of-line)
9108                                (search-backward ">" end t)
9109                                (1+ (point)))
9110                              (progn
9111                                (search-backward "<" end t)
9112                                (point))))))
9113                 ;; Get the references from the in-reply-to header if there
9114                 ;; were no references and the in-reply-to header looks
9115                 ;; promising.
9116                 (if (and (search-forward "\nin-reply-to: " nil t)
9117                          (setq in-reply-to (gnus-header-value))
9118                          (string-match "<[^>]+>" in-reply-to))
9119                     (setq ref (substring in-reply-to (match-beginning 0)
9120                                          (match-end 0)))
9121                   (setq ref ""))))
9122             ;; Chars.
9123             0
9124             ;; Lines.
9125             (progn
9126               (goto-char p)
9127               (if (search-forward "\nlines: " nil t)
9128                   (if (numberp (setq lines (read cur)))
9129                       lines 0)
9130                 0))
9131             ;; Xref.
9132             (progn
9133               (goto-char p)
9134               (and (search-forward "\nxref: " nil t)
9135                    (gnus-header-value)))))
9136           ;; We do the threading while we read the headers.  The
9137           ;; message-id and the last reference are both entered into
9138           ;; the same hash table.  Some tippy-toeing around has to be
9139           ;; done in case an article has arrived before the article
9140           ;; which it refers to.
9141           (if (boundp (setq id-dep (intern id dependencies)))
9142               (if (and (car (symbol-value id-dep))
9143                        (not force-new))
9144                   ;; An article with this Message-ID has already
9145                   ;; been seen, so we ignore this one, except we add
9146                   ;; any additional Xrefs (in case the two articles
9147                   ;; came from different servers).
9148                   (progn
9149                     (mail-header-set-xref
9150                      (car (symbol-value id-dep))
9151                      (concat (or (mail-header-xref
9152                                   (car (symbol-value id-dep))) "")
9153                              (or (mail-header-xref header) "")))
9154                     (setq header nil))
9155                 (setcar (symbol-value id-dep) header))
9156             (set id-dep (list header)))
9157           (when header
9158             (if (boundp (setq ref-dep (intern ref dependencies)))
9159                 (setcdr (symbol-value ref-dep)
9160                         (nconc (cdr (symbol-value ref-dep))
9161                                (list (symbol-value id-dep))))
9162               (set ref-dep (list nil (symbol-value id-dep))))
9163             (setq headers (cons header headers)))
9164           (goto-char (point-max))
9165           (widen))
9166         (nreverse headers)))))
9167
9168 ;; The following macros and functions were written by Felix Lee
9169 ;; <flee@cse.psu.edu>.
9170
9171 (defmacro gnus-nov-read-integer ()
9172   '(prog1
9173        (if (= (following-char) ?\t)
9174            0
9175          (let ((num (condition-case nil (read buffer) (error nil))))
9176            (if (numberp num) num 0)))
9177      (or (eobp) (forward-char 1))))
9178
9179 (defmacro gnus-nov-skip-field ()
9180   '(search-forward "\t" eol 'move))
9181
9182 (defmacro gnus-nov-field ()
9183   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9184
9185 ;; Goes through the xover lines and returns a list of vectors
9186 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9187                                                   force-new dependencies)
9188   "Parse the news overview data in the server buffer, and return a
9189 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9190   ;; Get the Xref when the users reads the articles since most/some
9191   ;; NNTP servers do not include Xrefs when using XOVER.
9192   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9193   (let ((cur nntp-server-buffer)
9194         (dependencies (or dependencies gnus-newsgroup-dependencies))
9195         number headers header)
9196     (save-excursion
9197       (set-buffer nntp-server-buffer)
9198       ;; Allow the user to mangle the headers before parsing them.
9199       (run-hooks 'gnus-parse-headers-hook)
9200       (goto-char (point-min))
9201       (while (and sequence (not (eobp)))
9202         (setq number (read cur))
9203         (while (and sequence (< (car sequence) number))
9204           (setq sequence (cdr sequence)))
9205         (and sequence
9206              (eq number (car sequence))
9207              (progn
9208                (setq sequence (cdr sequence))
9209                (if (setq header
9210                          (inline (gnus-nov-parse-line
9211                                   number dependencies force-new)))
9212                    (setq headers (cons header headers)))))
9213         (forward-line 1))
9214       (setq headers (nreverse headers)))
9215     headers))
9216
9217 ;; This function has to be called with point after the article number
9218 ;; on the beginning of the line.
9219 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9220   (let ((none 0)
9221         (eol (gnus-point-at-eol))
9222         (buffer (current-buffer))
9223         header ref id id-dep ref-dep)
9224
9225     ;; overview: [num subject from date id refs chars lines misc]
9226     (narrow-to-region (point) eol)
9227     (or (eobp) (forward-char))
9228
9229     (condition-case nil
9230         (setq header
9231               (vector
9232                number                   ; number
9233                (gnus-nov-field)         ; subject
9234                (gnus-nov-field)         ; from
9235                (gnus-nov-field)         ; date
9236                (setq id (or (gnus-nov-field)
9237                             (concat "none+"
9238                                     (int-to-string
9239                                      (setq none (1+ none)))))) ; id
9240                (progn
9241                  (save-excursion
9242                    (let ((beg (point)))
9243                      (search-forward "\t" eol)
9244                      (if (search-backward ">" beg t)
9245                          (setq ref
9246                                (buffer-substring
9247                                 (1+ (point))
9248                                 (search-backward "<" beg t)))
9249                        (setq ref nil))))
9250                  (gnus-nov-field))      ; refs
9251                (gnus-nov-read-integer)  ; chars
9252                (gnus-nov-read-integer)  ; lines
9253                (if (= (following-char) ?\n)
9254                    nil
9255                  (gnus-nov-field))      ; misc
9256                ))
9257       (error (progn
9258                (gnus-error 4 "Strange nov line")
9259                (setq header nil)
9260                (goto-char eol))))
9261
9262     (widen)
9263
9264     ;; We build the thread tree.
9265     (when header
9266       (if (boundp (setq id-dep (intern id dependencies)))
9267           (if (and (car (symbol-value id-dep))
9268                    (not force-new))
9269               ;; An article with this Message-ID has already been seen,
9270               ;; so we ignore this one, except we add any additional
9271               ;; Xrefs (in case the two articles came from different
9272               ;; servers.
9273               (progn
9274                 (mail-header-set-xref
9275                  (car (symbol-value id-dep))
9276                  (concat (or (mail-header-xref
9277                               (car (symbol-value id-dep))) "")
9278                          (or (mail-header-xref header) "")))
9279                 (setq header nil))
9280             (setcar (symbol-value id-dep) header))
9281         (set id-dep (list header))))
9282     (if header
9283         (progn
9284           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9285               (setcdr (symbol-value ref-dep)
9286                       (nconc (cdr (symbol-value ref-dep))
9287                              (list (symbol-value id-dep))))
9288             (set ref-dep (list nil (symbol-value id-dep))))))
9289     header))
9290
9291 (defun gnus-article-get-xrefs ()
9292   "Fill in the Xref value in `gnus-current-headers', if necessary.
9293 This is meant to be called in `gnus-article-internal-prepare-hook'."
9294   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9295                                  gnus-current-headers)))
9296     (or (not gnus-use-cross-reference)
9297         (not headers)
9298         (and (mail-header-xref headers)
9299              (not (string= (mail-header-xref headers) "")))
9300         (let ((case-fold-search t)
9301               xref)
9302           (save-restriction
9303             (nnheader-narrow-to-headers)
9304             (goto-char (point-min))
9305             (if (or (and (eq (downcase (following-char)) ?x)
9306                          (looking-at "Xref:"))
9307                     (search-forward "\nXref:" nil t))
9308                 (progn
9309                   (goto-char (1+ (match-end 0)))
9310                   (setq xref (buffer-substring (point)
9311                                                (progn (end-of-line) (point))))
9312                   (mail-header-set-xref headers xref))))))))
9313
9314 (defun gnus-summary-insert-subject (id &optional old-header)
9315   "Find article ID and insert the summary line for that article."
9316   (let ((header (gnus-read-header id))
9317         (number (and (numberp id) id))
9318         pos)
9319     (when header
9320       ;; Rebuild the thread that this article is part of and go to the
9321       ;; article we have fetched.
9322       (when old-header
9323         (when (setq pos (text-property-any
9324                          (point-min) (point-max) 'gnus-number 
9325                          (mail-header-number old-header)))
9326           (goto-char pos)
9327           (gnus-delete-line)
9328           (gnus-data-remove (mail-header-number old-header))))
9329       (gnus-rebuild-thread (mail-header-id header))
9330       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9331     (when (and (numberp number)
9332                (> number 0))
9333       ;; We have to update the boundaries even if we can't fetch the
9334       ;; article if ID is a number -- so that the next `P' or `N'
9335       ;; command will fetch the previous (or next) article even
9336       ;; if the one we tried to fetch this time has been canceled.
9337       (and (> number gnus-newsgroup-end)
9338            (setq gnus-newsgroup-end number))
9339       (and (< number gnus-newsgroup-begin)
9340            (setq gnus-newsgroup-begin number))
9341       (setq gnus-newsgroup-unselected
9342             (delq number gnus-newsgroup-unselected)))
9343     ;; Report back a success?
9344     (and header (mail-header-number header))))
9345
9346 (defun gnus-summary-work-articles (n)
9347   "Return a list of articles to be worked upon.  The prefix argument,
9348 the list of process marked articles, and the current article will be
9349 taken into consideration."
9350   (cond
9351    ((and n (numberp n))
9352     ;; A numerical prefix has been given.
9353     (let ((backward (< n 0))
9354           (n (abs n))
9355           articles article)
9356       (save-excursion
9357         (while
9358             (and (> n 0)
9359                  (push (setq article (gnus-summary-article-number))
9360                        articles)
9361                  (if backward
9362                      (gnus-summary-find-prev nil article)
9363                    (gnus-summary-find-next nil article)))
9364           (decf n)))
9365       (nreverse articles)))
9366    ((and (boundp 'transient-mark-mode)
9367          transient-mark-mode
9368          mark-active)
9369     ;; Work on the region between point and mark.
9370     (let ((max (max (point) (mark)))
9371           articles article)
9372       (save-excursion
9373         (goto-char (min (point) (mark)))
9374         (while
9375             (and
9376              (push (setq article (gnus-summary-article-number)) articles)
9377              (gnus-summary-find-next nil article)
9378              (< (point) max)))
9379         (nreverse articles))))
9380    (gnus-newsgroup-processable
9381     ;; There are process-marked articles present.
9382     (reverse gnus-newsgroup-processable))
9383    (t
9384     ;; Just return the current article.
9385     (list (gnus-summary-article-number)))))
9386
9387 (defun gnus-summary-search-group (&optional backward use-level)
9388   "Search for next unread newsgroup.
9389 If optional argument BACKWARD is non-nil, search backward instead."
9390   (save-excursion
9391     (set-buffer gnus-group-buffer)
9392     (if (gnus-group-search-forward
9393          backward nil (if use-level (gnus-group-group-level) nil))
9394         (gnus-group-group-name))))
9395
9396 (defun gnus-summary-best-group (&optional exclude-group)
9397   "Find the name of the best unread group.
9398 If EXCLUDE-GROUP, do not go to this group."
9399   (save-excursion
9400     (set-buffer gnus-group-buffer)
9401     (save-excursion
9402       (gnus-group-best-unread-group exclude-group))))
9403
9404 (defun gnus-summary-find-next (&optional unread article backward)
9405   (if backward (gnus-summary-find-prev)
9406     (let* ((dummy (gnus-summary-article-intangible-p))
9407            (article (or article (gnus-summary-article-number)))
9408            (arts (gnus-data-find-list article))
9409            result)
9410       (when (and (not dummy)
9411                  (or (not gnus-summary-check-current)
9412                      (not unread)
9413                      (not (gnus-data-unread-p (car arts)))))
9414         (setq arts (cdr arts)))
9415       (when (setq result
9416                   (if unread
9417                       (progn
9418                         (while arts
9419                           (when (gnus-data-unread-p (car arts))
9420                             (setq result (car arts)
9421                                   arts nil))
9422                           (setq arts (cdr arts)))
9423                         result)
9424                     (car arts)))
9425         (goto-char (gnus-data-pos result))
9426         (gnus-data-number result)))))
9427
9428 (defun gnus-summary-find-prev (&optional unread article)
9429   (let* ((article (or article (gnus-summary-article-number)))
9430          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9431          result)
9432     (when (or (not gnus-summary-check-current)
9433               (not unread)
9434               (not (gnus-data-unread-p (car arts))))
9435       (setq arts (cdr arts)))
9436     (if (setq result
9437               (if unread
9438                   (progn
9439                     (while arts
9440                       (and (gnus-data-unread-p (car arts))
9441                            (setq result (car arts)
9442                                  arts nil))
9443                       (setq arts (cdr arts)))
9444                     result)
9445                 (car arts)))
9446         (progn
9447           (goto-char (gnus-data-pos result))
9448           (gnus-data-number result)))))
9449
9450 (defun gnus-summary-find-subject (subject &optional unread backward article)
9451   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9452          (article (or article (gnus-summary-article-number)))
9453          (articles (gnus-data-list backward))
9454          (arts (gnus-data-find-list article articles))
9455          result)
9456     (when (or (not gnus-summary-check-current)
9457               (not unread)
9458               (not (gnus-data-unread-p (car arts))))
9459       (setq arts (cdr arts)))
9460     (while arts
9461       (and (or (not unread)
9462                (gnus-data-unread-p (car arts)))
9463            (vectorp (gnus-data-header (car arts)))
9464            (gnus-subject-equal
9465             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9466            (setq result (car arts)
9467                  arts nil))
9468       (setq arts (cdr arts)))
9469     (and result
9470          (goto-char (gnus-data-pos result))
9471          (gnus-data-number result))))
9472
9473 (defun gnus-summary-search-forward (&optional unread subject backward)
9474   "Search forward for an article.
9475 If UNREAD, look for unread articles.  If SUBJECT, look for
9476 articles with that subject.  If BACKWARD, search backward instead."
9477   (cond (subject (gnus-summary-find-subject subject unread backward))
9478         (backward (gnus-summary-find-prev unread))
9479         (t (gnus-summary-find-next unread))))
9480
9481 (defun gnus-recenter (&optional n)
9482   "Center point in window and redisplay frame.
9483 Also do horizontal recentering."
9484   (interactive "P")
9485   (when (and gnus-auto-center-summary
9486              (not (eq gnus-auto-center-summary 'vertical)))
9487     (gnus-horizontal-recenter))
9488   (recenter n))
9489
9490 (defun gnus-summary-recenter ()
9491   "Center point in the summary window.
9492 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9493 displayed, no centering will be performed."
9494   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9495   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9496   (let* ((top (cond ((< (window-height) 4) 0)
9497                     ((< (window-height) 7) 1)
9498                     (t 2)))
9499          (height (1- (window-height)))
9500          (bottom (save-excursion (goto-char (point-max))
9501                                  (forward-line (- height))
9502                                  (point)))
9503          (window (get-buffer-window (current-buffer))))
9504     ;; The user has to want it.
9505     (when gnus-auto-center-summary
9506       (when (get-buffer-window gnus-article-buffer)
9507        ;; Only do recentering when the article buffer is displayed,
9508        ;; Set the window start to either `bottom', which is the biggest
9509        ;; possible valid number, or the second line from the top,
9510        ;; whichever is the least.
9511        (set-window-start
9512         window (min bottom (save-excursion 
9513                              (forward-line (- top)) (point)))))
9514       ;; Do horizontal recentering while we're at it.
9515       (when (and (get-buffer-window (current-buffer) t)
9516                  (not (eq gnus-auto-center-summary 'vertical)))
9517         (let ((selected (selected-window)))
9518           (select-window (get-buffer-window (current-buffer) t))
9519           (gnus-summary-position-point)
9520           (gnus-horizontal-recenter)
9521           (select-window selected))))))
9522
9523 (defun gnus-horizontal-recenter ()
9524   "Recenter the current buffer horizontally."
9525   (if (< (current-column) (/ (window-width) 2))
9526       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9527     (let* ((orig (point))
9528            (end (window-end (get-buffer-window (current-buffer) t)))
9529            (max 0))
9530       ;; Find the longest line currently displayed in the window.
9531       (goto-char (window-start))
9532       (while (and (not (eobp)) 
9533                   (< (point) end))
9534         (end-of-line)
9535         (setq max (max max (current-column)))
9536         (forward-line 1))
9537       (goto-char orig)
9538       ;; Scroll horizontally to center (sort of) the point.
9539       (if (> max (window-width))
9540           (set-window-hscroll 
9541            (get-buffer-window (current-buffer) t)
9542            (min (- (current-column) (/ (window-width) 3))
9543                 (+ 2 (- max (window-width)))))
9544         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9545       max)))
9546
9547 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9548 (defun gnus-short-group-name (group &optional levels)
9549   "Collapse GROUP name LEVELS."
9550   (let* ((name "") 
9551          (foreign "")
9552          (depth 0) 
9553          (skip 1)
9554          (levels (or levels
9555                      (progn
9556                        (while (string-match "\\." group skip)
9557                          (setq skip (match-end 0)
9558                                depth (+ depth 1)))
9559                        depth))))
9560     (if (string-match ":" group)
9561         (setq foreign (substring group 0 (match-end 0))
9562               group (substring group (match-end 0))))
9563     (while group
9564       (if (and (string-match "\\." group)
9565                (> levels (- gnus-group-uncollapsed-levels 1)))
9566           (setq name (concat name (substring group 0 1))
9567                 group (substring group (match-end 0))
9568                 levels (- levels 1)
9569                 name (concat name "."))
9570         (setq name (concat foreign name group)
9571               group nil)))
9572     name))
9573
9574 (defun gnus-summary-jump-to-group (newsgroup)
9575   "Move point to NEWSGROUP in group mode buffer."
9576   ;; Keep update point of group mode buffer if visible.
9577   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9578       (save-window-excursion
9579         ;; Take care of tree window mode.
9580         (if (get-buffer-window gnus-group-buffer)
9581             (pop-to-buffer gnus-group-buffer))
9582         (gnus-group-jump-to-group newsgroup))
9583     (save-excursion
9584       ;; Take care of tree window mode.
9585       (if (get-buffer-window gnus-group-buffer)
9586           (pop-to-buffer gnus-group-buffer)
9587         (set-buffer gnus-group-buffer))
9588       (gnus-group-jump-to-group newsgroup))))
9589
9590 ;; This function returns a list of article numbers based on the
9591 ;; difference between the ranges of read articles in this group and
9592 ;; the range of active articles.
9593 (defun gnus-list-of-unread-articles (group)
9594   (let* ((read (gnus-info-read (gnus-get-info group)))
9595          (active (gnus-active group))
9596          (last (cdr active))
9597          first nlast unread)
9598     ;; If none are read, then all are unread.
9599     (if (not read)
9600         (setq first (car active))
9601       ;; If the range of read articles is a single range, then the
9602       ;; first unread article is the article after the last read
9603       ;; article.  Sounds logical, doesn't it?
9604       (if (not (listp (cdr read)))
9605           (setq first (1+ (cdr read)))
9606         ;; `read' is a list of ranges.
9607         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9608                                 (caar read))) 1)
9609             (setq first 1))
9610         (while read
9611           (if first
9612               (while (< first nlast)
9613                 (setq unread (cons first unread))
9614                 (setq first (1+ first))))
9615           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9616           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9617           (setq read (cdr read)))))
9618     ;; And add the last unread articles.
9619     (while (<= first last)
9620       (setq unread (cons first unread))
9621       (setq first (1+ first)))
9622     ;; Return the list of unread articles.
9623     (nreverse unread)))
9624
9625 (defun gnus-list-of-read-articles (group)
9626   "Return a list of unread, unticked and non-dormant articles."
9627   (let* ((info (gnus-get-info group))
9628          (marked (gnus-info-marks info))
9629          (active (gnus-active group)))
9630     (and info active
9631          (gnus-set-difference
9632           (gnus-sorted-complement
9633            (gnus-uncompress-range active)
9634            (gnus-list-of-unread-articles group))
9635           (append
9636            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9637            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9638
9639 ;; Various summary commands
9640
9641 (defun gnus-summary-universal-argument (arg)
9642   "Perform any operation on all articles that are process/prefixed."
9643   (interactive "P")
9644   (gnus-set-global-variables)
9645   (let ((articles (gnus-summary-work-articles arg))
9646         func article)
9647     (if (eq
9648          (setq
9649           func
9650           (key-binding
9651            (read-key-sequence
9652             (substitute-command-keys
9653              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9654              ))))
9655          'undefined)
9656         (gnus-error 1 "Undefined key")
9657       (save-excursion
9658         (while articles
9659           (gnus-summary-goto-subject (setq article (pop articles)))
9660           (command-execute func)
9661           (gnus-summary-remove-process-mark article)))))
9662   (gnus-summary-position-point))
9663
9664 (defun gnus-summary-toggle-truncation (&optional arg)
9665   "Toggle truncation of summary lines.
9666 With arg, turn line truncation on iff arg is positive."
9667   (interactive "P")
9668   (setq truncate-lines
9669         (if (null arg) (not truncate-lines)
9670           (> (prefix-numeric-value arg) 0)))
9671   (redraw-display))
9672
9673 (defun gnus-summary-reselect-current-group (&optional all rescan)
9674   "Exit and then reselect the current newsgroup.
9675 The prefix argument ALL means to select all articles."
9676   (interactive "P")
9677   (gnus-set-global-variables)
9678   (let ((current-subject (gnus-summary-article-number))
9679         (group gnus-newsgroup-name))
9680     (setq gnus-newsgroup-begin nil)
9681     (gnus-summary-exit)
9682     ;; We have to adjust the point of group mode buffer because the
9683     ;; current point was moved to the next unread newsgroup by
9684     ;; exiting.
9685     (gnus-summary-jump-to-group group)
9686     (when rescan
9687       (save-excursion
9688         (gnus-group-get-new-news-this-group 1)))
9689     (gnus-group-read-group all t)
9690     (gnus-summary-goto-subject current-subject)))
9691
9692 (defun gnus-summary-rescan-group (&optional all)
9693   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9694   (interactive "P")
9695   (gnus-summary-reselect-current-group all t))
9696
9697 (defun gnus-summary-update-info ()
9698   (let* ((group gnus-newsgroup-name))
9699     (when gnus-newsgroup-kill-headers
9700       (setq gnus-newsgroup-killed
9701             (gnus-compress-sequence
9702              (nconc
9703               (gnus-set-sorted-intersection
9704                (gnus-uncompress-range gnus-newsgroup-killed)
9705                (setq gnus-newsgroup-unselected
9706                      (sort gnus-newsgroup-unselected '<)))
9707               (setq gnus-newsgroup-unreads
9708                     (sort gnus-newsgroup-unreads '<))) t)))
9709     (unless (listp (cdr gnus-newsgroup-killed))
9710       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9711     (let ((headers gnus-newsgroup-headers))
9712       (run-hooks 'gnus-exit-group-hook)
9713       (unless gnus-save-score
9714         (setq gnus-newsgroup-scored nil))
9715       ;; Set the new ranges of read articles.
9716       (gnus-update-read-articles
9717        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9718       ;; Set the current article marks.
9719       (gnus-update-marks)
9720       ;; Do the cross-ref thing.
9721       (when gnus-use-cross-reference
9722         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9723       ;; Do adaptive scoring, and possibly save score files.
9724       (when gnus-newsgroup-adaptive
9725         (gnus-score-adaptive))
9726       (when gnus-use-scoring
9727         (gnus-score-save))
9728       ;; Do not switch windows but change the buffer to work.
9729       (set-buffer gnus-group-buffer)
9730       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9731           (gnus-group-update-group group)))))
9732
9733 (defun gnus-summary-exit (&optional temporary)
9734   "Exit reading current newsgroup, and then return to group selection mode.
9735 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9736   (interactive)
9737   (gnus-set-global-variables)
9738   (gnus-kill-save-kill-buffer)
9739   (let* ((group gnus-newsgroup-name)
9740          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9741          (mode major-mode)
9742          (buf (current-buffer)))
9743     (run-hooks 'gnus-summary-prepare-exit-hook)
9744     ;; If we have several article buffers, we kill them at exit.
9745     (unless gnus-single-article-buffer
9746       (gnus-kill-buffer gnus-article-buffer)
9747       (gnus-kill-buffer gnus-original-article-buffer)
9748       (setq gnus-article-current nil))
9749     (when gnus-use-cache
9750       (gnus-cache-possibly-remove-articles)
9751       (gnus-cache-save-buffers))
9752     (when gnus-use-trees
9753       (gnus-tree-close group))
9754     ;; Make all changes in this group permanent.
9755     (unless quit-config
9756       (gnus-summary-update-info))
9757     (gnus-close-group group)
9758     ;; Make sure where I was, and go to next newsgroup.
9759     (set-buffer gnus-group-buffer)
9760     (unless quit-config
9761       (gnus-group-jump-to-group group)
9762       (gnus-group-next-unread-group 1))
9763     (run-hooks 'gnus-summary-exit-hook)
9764     (unless gnus-single-article-buffer
9765       (setq gnus-article-current nil))
9766     (if temporary
9767         nil                             ;Nothing to do.
9768       ;; If we have several article buffers, we kill them at exit.
9769       (unless gnus-single-article-buffer
9770         (gnus-kill-buffer gnus-article-buffer)
9771         (gnus-kill-buffer gnus-original-article-buffer)
9772         (setq gnus-article-current nil))
9773       (set-buffer buf)
9774       (if (not gnus-kill-summary-on-exit)
9775           (gnus-deaden-summary)
9776         ;; We set all buffer-local variables to nil.  It is unclear why
9777         ;; this is needed, but if we don't, buffer-local variables are
9778         ;; not garbage-collected, it seems.  This would the lead to en
9779         ;; ever-growing Emacs.
9780         (gnus-summary-clear-local-variables)
9781         (when (get-buffer gnus-article-buffer)
9782           (bury-buffer gnus-article-buffer))
9783         ;; We clear the global counterparts of the buffer-local
9784         ;; variables as well, just to be on the safe side.
9785         (gnus-configure-windows 'group 'force)
9786         (gnus-summary-clear-local-variables)
9787         ;; Return to group mode buffer.
9788         (if (eq mode 'gnus-summary-mode)
9789             (gnus-kill-buffer buf)))
9790       (setq gnus-current-select-method gnus-select-method)
9791       (pop-to-buffer gnus-group-buffer)
9792       ;; Clear the current group name.
9793       (if (not quit-config)
9794           (progn
9795             (gnus-group-jump-to-group group)
9796             (gnus-group-next-unread-group 1)
9797             (gnus-configure-windows 'group 'force))
9798         (if (not (buffer-name (car quit-config)))
9799             (gnus-configure-windows 'group 'force)
9800           (set-buffer (car quit-config))
9801           (and (eq major-mode 'gnus-summary-mode)
9802                (gnus-set-global-variables))
9803           (gnus-configure-windows (cdr quit-config))))
9804       (unless quit-config
9805         (setq gnus-newsgroup-name nil)))))
9806
9807 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9808 (defun gnus-summary-exit-no-update (&optional no-questions)
9809   "Quit reading current newsgroup without updating read article info."
9810   (interactive)
9811   (gnus-set-global-variables)
9812   (let* ((group gnus-newsgroup-name)
9813          (quit-config (gnus-group-quit-config group)))
9814     (when (or no-questions
9815               gnus-expert-user
9816               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9817       ;; If we have several article buffers, we kill them at exit.
9818       (unless gnus-single-article-buffer
9819         (gnus-kill-buffer gnus-article-buffer)
9820         (gnus-kill-buffer gnus-original-article-buffer)
9821         (setq gnus-article-current nil))
9822       (if (not gnus-kill-summary-on-exit)
9823           (gnus-deaden-summary)
9824         (gnus-close-group group)
9825         (gnus-summary-clear-local-variables)
9826         (set-buffer gnus-group-buffer)
9827         (gnus-summary-clear-local-variables)
9828         (when (get-buffer gnus-summary-buffer)
9829           (kill-buffer gnus-summary-buffer)))
9830       (unless gnus-single-article-buffer
9831         (setq gnus-article-current nil))
9832       (when gnus-use-trees
9833         (gnus-tree-close group))
9834       (when (get-buffer gnus-article-buffer)
9835         (bury-buffer gnus-article-buffer))
9836       ;; Return to the group buffer.
9837       (gnus-configure-windows 'group 'force)
9838       ;; Clear the current group name.
9839       (setq gnus-newsgroup-name nil)
9840       (when (equal (gnus-group-group-name) group)
9841         (gnus-group-next-unread-group 1))
9842       (when quit-config
9843         (if (not (buffer-name (car quit-config)))
9844             (gnus-configure-windows 'group 'force)
9845           (set-buffer (car quit-config))
9846           (when (eq major-mode 'gnus-summary-mode)
9847             (gnus-set-global-variables))
9848           (gnus-configure-windows (cdr quit-config)))))))
9849
9850 ;;; Dead summaries.
9851
9852 (defvar gnus-dead-summary-mode-map nil)
9853
9854 (if gnus-dead-summary-mode-map
9855     nil
9856   (setq gnus-dead-summary-mode-map (make-keymap))
9857   (suppress-keymap gnus-dead-summary-mode-map)
9858   (substitute-key-definition
9859    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9860   (let ((keys '("\C-d" "\r" "\177")))
9861     (while keys
9862       (define-key gnus-dead-summary-mode-map
9863         (pop keys) 'gnus-summary-wake-up-the-dead))))
9864
9865 (defvar gnus-dead-summary-mode nil
9866   "Minor mode for Gnus summary buffers.")
9867
9868 (defun gnus-dead-summary-mode (&optional arg)
9869   "Minor mode for Gnus summary buffers."
9870   (interactive "P")
9871   (when (eq major-mode 'gnus-summary-mode)
9872     (make-local-variable 'gnus-dead-summary-mode)
9873     (setq gnus-dead-summary-mode
9874           (if (null arg) (not gnus-dead-summary-mode)
9875             (> (prefix-numeric-value arg) 0)))
9876     (when gnus-dead-summary-mode
9877       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9878         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9879       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9880         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9881               minor-mode-map-alist)))))
9882
9883 (defun gnus-deaden-summary ()
9884   "Make the current summary buffer into a dead summary buffer."
9885   ;; Kill any previous dead summary buffer.
9886   (when (and gnus-dead-summary
9887              (buffer-name gnus-dead-summary))
9888     (save-excursion
9889       (set-buffer gnus-dead-summary)
9890       (when gnus-dead-summary-mode
9891         (kill-buffer (current-buffer)))))
9892   ;; Make this the current dead summary.
9893   (setq gnus-dead-summary (current-buffer))
9894   (gnus-dead-summary-mode 1)
9895   (let ((name (buffer-name)))
9896     (when (string-match "Summary" name)
9897       (rename-buffer
9898        (concat (substring name 0 (match-beginning 0)) "Dead "
9899                (substring name (match-beginning 0))) t))))
9900
9901 (defun gnus-kill-or-deaden-summary (buffer)
9902   "Kill or deaden the summary BUFFER."
9903   (cond (gnus-kill-summary-on-exit
9904          (when (and gnus-use-trees
9905                     (and (get-buffer buffer)
9906                          (buffer-name (get-buffer buffer))))
9907            (save-excursion
9908              (set-buffer (get-buffer buffer))
9909              (gnus-tree-close gnus-newsgroup-name)))
9910          (gnus-kill-buffer buffer))
9911         ((and (get-buffer buffer)
9912               (buffer-name (get-buffer buffer)))
9913          (save-excursion
9914            (set-buffer buffer)
9915            (gnus-deaden-summary)))))
9916
9917 (defun gnus-summary-wake-up-the-dead (&rest args)
9918   "Wake up the dead summary buffer."
9919   (interactive)
9920   (gnus-dead-summary-mode -1)
9921   (let ((name (buffer-name)))
9922     (when (string-match "Dead " name)
9923       (rename-buffer
9924        (concat (substring name 0 (match-beginning 0))
9925                (substring name (match-end 0))) t)))
9926   (gnus-message 3 "This dead summary is now alive again"))
9927
9928 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9929 (defun gnus-summary-fetch-faq (&optional faq-dir)
9930   "Fetch the FAQ for the current group.
9931 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9932 in."
9933   (interactive
9934    (list
9935     (if current-prefix-arg
9936         (completing-read
9937          "Faq dir: " (and (listp gnus-group-faq-directory)
9938                           gnus-group-faq-directory)))))
9939   (let (gnus-faq-buffer)
9940     (and (setq gnus-faq-buffer
9941                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9942          (gnus-configure-windows 'summary-faq))))
9943
9944 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9945 (defun gnus-summary-describe-group (&optional force)
9946   "Describe the current newsgroup."
9947   (interactive "P")
9948   (gnus-group-describe-group force gnus-newsgroup-name))
9949
9950 (defun gnus-summary-describe-briefly ()
9951   "Describe summary mode commands briefly."
9952   (interactive)
9953   (gnus-message 6
9954                 (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")))
9955
9956 ;; Walking around group mode buffer from summary mode.
9957
9958 (defun gnus-summary-next-group (&optional no-article target-group backward)
9959   "Exit current newsgroup and then select next unread newsgroup.
9960 If prefix argument NO-ARTICLE is non-nil, no article is selected
9961 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9962 previous group instead."
9963   (interactive "P")
9964   (gnus-set-global-variables)
9965   (let ((current-group gnus-newsgroup-name)
9966         (current-buffer (current-buffer))
9967         entered)
9968     ;; First we semi-exit this group to update Xrefs and all variables.
9969     ;; We can't do a real exit, because the window conf must remain
9970     ;; the same in case the user is prompted for info, and we don't
9971     ;; want the window conf to change before that...
9972     (gnus-summary-exit t)
9973     (while (not entered)
9974       ;; Then we find what group we are supposed to enter.
9975       (set-buffer gnus-group-buffer)
9976       (gnus-group-jump-to-group current-group)
9977       (setq target-group
9978             (or target-group
9979                 (if (eq gnus-keep-same-level 'best)
9980                     (gnus-summary-best-group gnus-newsgroup-name)
9981                   (gnus-summary-search-group backward gnus-keep-same-level))))
9982       (if (not target-group)
9983           ;; There are no further groups, so we return to the group
9984           ;; buffer.
9985           (progn
9986             (gnus-message 5 "Returning to the group buffer")
9987             (setq entered t)
9988             (set-buffer current-buffer)
9989             (gnus-summary-exit))
9990         ;; We try to enter the target group.
9991         (gnus-group-jump-to-group target-group)
9992         (let ((unreads (gnus-group-group-unread)))
9993           (if (and (or (eq t unreads)
9994                        (and unreads (not (zerop unreads))))
9995                    (gnus-summary-read-group
9996                     target-group nil no-article current-buffer))
9997               (setq entered t)
9998             (setq current-group target-group
9999                   target-group nil)))))))
10000
10001 (defun gnus-summary-prev-group (&optional no-article)
10002   "Exit current newsgroup and then select previous unread newsgroup.
10003 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10004   (interactive "P")
10005   (gnus-summary-next-group no-article nil t))
10006
10007 ;; Walking around summary lines.
10008
10009 (defun gnus-summary-first-subject (&optional unread)
10010   "Go to the first unread subject.
10011 If UNREAD is non-nil, go to the first unread article.
10012 Returns the article selected or nil if there are no unread articles."
10013   (interactive "P")
10014   (prog1
10015       (cond
10016        ;; Empty summary.
10017        ((null gnus-newsgroup-data)
10018         (gnus-message 3 "No articles in the group")
10019         nil)
10020        ;; Pick the first article.
10021        ((not unread)
10022         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10023         (gnus-data-number (car gnus-newsgroup-data)))
10024        ;; No unread articles.
10025        ((null gnus-newsgroup-unreads)
10026         (gnus-message 3 "No more unread articles")
10027         nil)
10028        ;; Find the first unread article.
10029        (t
10030         (let ((data gnus-newsgroup-data))
10031           (while (and data
10032                       (not (gnus-data-unread-p (car data))))
10033             (setq data (cdr data)))
10034           (if data
10035               (progn
10036                 (goto-char (gnus-data-pos (car data)))
10037                 (gnus-data-number (car data)))))))
10038     (gnus-summary-position-point)))
10039
10040 (defun gnus-summary-next-subject (n &optional unread dont-display)
10041   "Go to next N'th summary line.
10042 If N is negative, go to the previous N'th subject line.
10043 If UNREAD is non-nil, only unread articles are selected.
10044 The difference between N and the actual number of steps taken is
10045 returned."
10046   (interactive "p")
10047   (let ((backward (< n 0))
10048         (n (abs n)))
10049     (while (and (> n 0)
10050                 (if backward
10051                     (gnus-summary-find-prev unread)
10052                   (gnus-summary-find-next unread)))
10053       (setq n (1- n)))
10054     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10055                                (if unread " unread" "")))
10056     (unless dont-display
10057       (gnus-summary-recenter)
10058       (gnus-summary-position-point))
10059     n))
10060
10061 (defun gnus-summary-next-unread-subject (n)
10062   "Go to next N'th unread summary line."
10063   (interactive "p")
10064   (gnus-summary-next-subject n t))
10065
10066 (defun gnus-summary-prev-subject (n &optional unread)
10067   "Go to previous N'th summary line.
10068 If optional argument UNREAD is non-nil, only unread article is selected."
10069   (interactive "p")
10070   (gnus-summary-next-subject (- n) unread))
10071
10072 (defun gnus-summary-prev-unread-subject (n)
10073   "Go to previous N'th unread summary line."
10074   (interactive "p")
10075   (gnus-summary-next-subject (- n) t))
10076
10077 (defun gnus-summary-goto-subject (article &optional force silent)
10078   "Go the subject line of ARTICLE.
10079 If FORCE, also allow jumping to articles not currently shown."
10080   (let ((b (point))
10081         (data (gnus-data-find article)))
10082     ;; We read in the article if we have to.
10083     (and (not data)
10084          force
10085          (gnus-summary-insert-subject article)
10086          (setq data (gnus-data-find article)))
10087     (goto-char b)
10088     (if (not data)
10089         (progn
10090           (unless silent
10091             (gnus-message 3 "Can't find article %d" article))
10092           nil)
10093       (goto-char (gnus-data-pos data))
10094       article)))
10095
10096 ;; Walking around summary lines with displaying articles.
10097
10098 (defun gnus-summary-expand-window (&optional arg)
10099   "Make the summary buffer take up the entire Emacs frame.
10100 Given a prefix, will force an `article' buffer configuration."
10101   (interactive "P")
10102   (gnus-set-global-variables)
10103   (if arg
10104       (gnus-configure-windows 'article 'force)
10105     (gnus-configure-windows 'summary 'force)))
10106
10107 (defun gnus-summary-display-article (article &optional all-header)
10108   "Display ARTICLE in article buffer."
10109   (gnus-set-global-variables)
10110   (if (null article)
10111       nil
10112     (prog1
10113         (if gnus-summary-display-article-function
10114             (funcall gnus-summary-display-article-function article all-header)
10115           (gnus-article-prepare article all-header))
10116       (run-hooks 'gnus-select-article-hook)
10117       (gnus-summary-recenter)
10118       (gnus-summary-goto-subject article)
10119       (when gnus-use-trees
10120         (gnus-possibly-generate-tree article)
10121         (gnus-highlight-selected-tree article))
10122       ;; Successfully display article.
10123       (gnus-article-set-window-start
10124        (cdr (assq article gnus-newsgroup-bookmarks)))
10125       t)))
10126
10127 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10128   "Select the current article.
10129 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10130 non-nil, the article will be re-fetched even if it already present in
10131 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10132 be displayed."
10133   (let ((article (or article (gnus-summary-article-number)))
10134         (all-headers (not (not all-headers))) ;Must be T or NIL.
10135         gnus-summary-display-article-function
10136         did)
10137     (and (not pseudo)
10138          (gnus-summary-article-pseudo-p article)
10139          (error "This is a pseudo-article."))
10140     (prog1
10141         (save-excursion
10142           (set-buffer gnus-summary-buffer)
10143           (if (or (and gnus-single-article-buffer
10144                        (or (null gnus-current-article)
10145                            (null gnus-article-current)
10146                            (null (get-buffer gnus-article-buffer))
10147                            (not (eq article (cdr gnus-article-current)))
10148                            (not (equal (car gnus-article-current)
10149                                        gnus-newsgroup-name))))
10150                   (and (not gnus-single-article-buffer)
10151                        (or (null gnus-current-article)
10152                            (not (eq gnus-current-article article))))
10153                   force)
10154               ;; The requested article is different from the current article.
10155               (prog1
10156                   (gnus-summary-display-article article all-headers)
10157                 (setq did article))
10158             (if (or all-headers gnus-show-all-headers)
10159                 (gnus-article-show-all-headers))
10160             'old))
10161       (if did
10162           (gnus-article-set-window-start
10163            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10164
10165 (defun gnus-summary-set-current-mark (&optional current-mark)
10166   "Obsolete function."
10167   nil)
10168
10169 (defun gnus-summary-next-article (&optional unread subject backward push)
10170   "Select the next article.
10171 If UNREAD, only unread articles are selected.
10172 If SUBJECT, only articles with SUBJECT are selected.
10173 If BACKWARD, the previous article is selected instead of the next."
10174   (interactive "P")
10175   (gnus-set-global-variables)
10176   (cond
10177    ;; Is there such an article?
10178    ((and (gnus-summary-search-forward unread subject backward)
10179          (or (gnus-summary-display-article (gnus-summary-article-number))
10180              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10181     (gnus-summary-position-point))
10182    ;; If not, we try the first unread, if that is wanted.
10183    ((and subject
10184          gnus-auto-select-same
10185          (or (gnus-summary-first-unread-article)
10186              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10187     (gnus-summary-position-point)
10188     (gnus-message 6 "Wrapped"))
10189    ;; Try to get next/previous article not displayed in this group.
10190    ((and gnus-auto-extend-newsgroup
10191          (not unread) (not subject))
10192     (gnus-summary-goto-article
10193      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10194      nil t))
10195    ;; Go to next/previous group.
10196    (t
10197     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10198         (gnus-summary-jump-to-group gnus-newsgroup-name))
10199     (let ((cmd last-command-char)
10200           (group
10201            (if (eq gnus-keep-same-level 'best)
10202                (gnus-summary-best-group gnus-newsgroup-name)
10203              (gnus-summary-search-group backward gnus-keep-same-level))))
10204       ;; For some reason, the group window gets selected.  We change
10205       ;; it back.
10206       (select-window (get-buffer-window (current-buffer)))
10207       ;; Select next unread newsgroup automagically.
10208       (cond
10209        ((not gnus-auto-select-next)
10210         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10211        ((or (eq gnus-auto-select-next 'quietly)
10212             (and (eq gnus-auto-select-next 'slightly-quietly)
10213                  push)
10214             (and (eq gnus-auto-select-next 'almost-quietly)
10215                  (gnus-summary-last-article-p)))
10216         ;; Select quietly.
10217         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10218             (gnus-summary-exit)
10219           (gnus-message 7 "No more%s articles (%s)..."
10220                         (if unread " unread" "")
10221                         (if group (concat "selecting " group)
10222                           "exiting"))
10223           (gnus-summary-next-group nil group backward)))
10224        (t
10225         (gnus-summary-walk-group-buffer
10226          gnus-newsgroup-name cmd unread backward)))))))
10227
10228 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10229   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10230                       (?\C-p (gnus-group-prev-unread-group 1))))
10231         keve key group ended)
10232     (save-excursion
10233       (set-buffer gnus-group-buffer)
10234       (gnus-summary-jump-to-group from-group)
10235       (setq group
10236             (if (eq gnus-keep-same-level 'best)
10237                 (gnus-summary-best-group gnus-newsgroup-name)
10238               (gnus-summary-search-group backward gnus-keep-same-level))))
10239     (while (not ended)
10240       (gnus-message
10241        5 "No more%s articles%s" (if unread " unread" "")
10242        (if (and group
10243                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10244            (format " (Type %s for %s [%s])"
10245                    (single-key-description cmd) group
10246                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10247          (format " (Type %s to exit %s)"
10248                  (single-key-description cmd)
10249                  gnus-newsgroup-name)))
10250       ;; Confirm auto selection.
10251       (setq key (car (setq keve (gnus-read-event-char))))
10252       (setq ended t)
10253       (cond
10254        ((assq key keystrokes)
10255         (let ((obuf (current-buffer)))
10256           (switch-to-buffer gnus-group-buffer)
10257           (and group
10258                (gnus-group-jump-to-group group))
10259           (eval (cadr (assq key keystrokes)))
10260           (setq group (gnus-group-group-name))
10261           (switch-to-buffer obuf))
10262         (setq ended nil))
10263        ((equal key cmd)
10264         (if (or (not group)
10265                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10266             (gnus-summary-exit)
10267           (gnus-summary-next-group nil group backward)))
10268        (t
10269         (push (cdr keve) unread-command-events))))))
10270
10271 (defun gnus-read-event-char ()
10272   "Get the next event."
10273   (let ((event (read-event)))
10274     (cons (and (numberp event) event) event)))
10275
10276 (defun gnus-summary-next-unread-article ()
10277   "Select unread article after current one."
10278   (interactive)
10279   (gnus-summary-next-article t (and gnus-auto-select-same
10280                                     (gnus-summary-article-subject))))
10281
10282 (defun gnus-summary-prev-article (&optional unread subject)
10283   "Select the article after the current one.
10284 If UNREAD is non-nil, only unread articles are selected."
10285   (interactive "P")
10286   (gnus-summary-next-article unread subject t))
10287
10288 (defun gnus-summary-prev-unread-article ()
10289   "Select unred article before current one."
10290   (interactive)
10291   (gnus-summary-prev-article t (and gnus-auto-select-same
10292                                     (gnus-summary-article-subject))))
10293
10294 (defun gnus-summary-next-page (&optional lines circular)
10295   "Show next page of the selected article.
10296 If at the end of the current article, select the next article.
10297 LINES says how many lines should be scrolled up.
10298
10299 If CIRCULAR is non-nil, go to the start of the article instead of
10300 selecting the next article when reaching the end of the current
10301 article."
10302   (interactive "P")
10303   (setq gnus-summary-buffer (current-buffer))
10304   (gnus-set-global-variables)
10305   (let ((article (gnus-summary-article-number))
10306         (endp nil))
10307     (gnus-configure-windows 'article)
10308     (if (or (null gnus-current-article)
10309             (null gnus-article-current)
10310             (/= article (cdr gnus-article-current))
10311             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10312         ;; Selected subject is different from current article's.
10313         (gnus-summary-display-article article)
10314       (gnus-eval-in-buffer-window
10315        gnus-article-buffer
10316        (setq endp (gnus-article-next-page lines)))
10317       (if endp
10318           (cond (circular
10319                  (gnus-summary-beginning-of-article))
10320                 (lines
10321                  (gnus-message 3 "End of message"))
10322                 ((null lines)
10323                  (if (and (eq gnus-summary-goto-unread 'never)
10324                           (not (gnus-summary-last-article-p article)))
10325                      (gnus-summary-next-article)
10326                    (gnus-summary-next-unread-article))))))
10327     (gnus-summary-recenter)
10328     (gnus-summary-position-point)))
10329
10330 (defun gnus-summary-prev-page (&optional lines)
10331   "Show previous page of selected article.
10332 Argument LINES specifies lines to be scrolled down."
10333   (interactive "P")
10334   (gnus-set-global-variables)
10335   (let ((article (gnus-summary-article-number)))
10336     (gnus-configure-windows 'article)
10337     (if (or (null gnus-current-article)
10338             (null gnus-article-current)
10339             (/= article (cdr gnus-article-current))
10340             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10341         ;; Selected subject is different from current article's.
10342         (gnus-summary-display-article article)
10343       (gnus-summary-recenter)
10344       (gnus-eval-in-buffer-window gnus-article-buffer
10345                                   (gnus-article-prev-page lines))))
10346   (gnus-summary-position-point))
10347
10348 (defun gnus-summary-scroll-up (lines)
10349   "Scroll up (or down) one line current article.
10350 Argument LINES specifies lines to be scrolled up (or down if negative)."
10351   (interactive "p")
10352   (gnus-set-global-variables)
10353   (gnus-configure-windows 'article)
10354   (gnus-summary-show-thread)
10355   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10356     (gnus-eval-in-buffer-window
10357      gnus-article-buffer
10358      (cond ((> lines 0)
10359             (if (gnus-article-next-page lines)
10360                 (gnus-message 3 "End of message")))
10361            ((< lines 0)
10362             (gnus-article-prev-page (- lines))))))
10363   (gnus-summary-recenter)
10364   (gnus-summary-position-point))
10365
10366 (defun gnus-summary-next-same-subject ()
10367   "Select next article which has the same subject as current one."
10368   (interactive)
10369   (gnus-set-global-variables)
10370   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10371
10372 (defun gnus-summary-prev-same-subject ()
10373   "Select previous article which has the same subject as current one."
10374   (interactive)
10375   (gnus-set-global-variables)
10376   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10377
10378 (defun gnus-summary-next-unread-same-subject ()
10379   "Select next unread article which has the same subject as current one."
10380   (interactive)
10381   (gnus-set-global-variables)
10382   (gnus-summary-next-article t (gnus-summary-article-subject)))
10383
10384 (defun gnus-summary-prev-unread-same-subject ()
10385   "Select previous unread article which has the same subject as current one."
10386   (interactive)
10387   (gnus-set-global-variables)
10388   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10389
10390 (defun gnus-summary-first-unread-article ()
10391   "Select the first unread article.
10392 Return nil if there are no unread articles."
10393   (interactive)
10394   (gnus-set-global-variables)
10395   (prog1
10396       (if (gnus-summary-first-subject t)
10397           (progn
10398             (gnus-summary-show-thread)
10399             (gnus-summary-first-subject t)
10400             (gnus-summary-display-article (gnus-summary-article-number))))
10401     (gnus-summary-position-point)))
10402
10403 (defun gnus-summary-best-unread-article ()
10404   "Select the unread article with the highest score."
10405   (interactive)
10406   (gnus-set-global-variables)
10407   (let ((best -1000000)
10408         (data gnus-newsgroup-data)
10409         article score)
10410     (while data
10411       (and (gnus-data-unread-p (car data))
10412            (> (setq score
10413                     (gnus-summary-article-score (gnus-data-number (car data))))
10414               best)
10415            (setq best score
10416                  article (gnus-data-number (car data))))
10417       (setq data (cdr data)))
10418     (prog1
10419         (if article
10420             (gnus-summary-goto-article article)
10421           (error "No unread articles"))
10422       (gnus-summary-position-point))))
10423
10424 (defun gnus-summary-last-subject ()
10425   "Go to the last displayed subject line in the group."
10426   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10427     (when article
10428       (gnus-summary-goto-subject article))))
10429
10430 (defun gnus-summary-goto-article (article &optional all-headers force)
10431   "Fetch ARTICLE and display it if it exists.
10432 If ALL-HEADERS is non-nil, no header lines are hidden."
10433   (interactive
10434    (list
10435     (string-to-int
10436      (completing-read
10437       "Article number: "
10438       (mapcar (lambda (number) (list (int-to-string number)))
10439               gnus-newsgroup-limit)))
10440     current-prefix-arg
10441     t))
10442   (prog1
10443       (if (gnus-summary-goto-subject article force)
10444           (gnus-summary-display-article article all-headers)
10445         (gnus-message 4 "Couldn't go to article %s" article) nil)
10446     (gnus-summary-position-point)))
10447
10448 (defun gnus-summary-goto-last-article ()
10449   "Go to the previously read article."
10450   (interactive)
10451   (prog1
10452       (and gnus-last-article
10453            (gnus-summary-goto-article gnus-last-article))
10454     (gnus-summary-position-point)))
10455
10456 (defun gnus-summary-pop-article (number)
10457   "Pop one article off the history and go to the previous.
10458 NUMBER articles will be popped off."
10459   (interactive "p")
10460   (let (to)
10461     (setq gnus-newsgroup-history
10462           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10463     (if to
10464         (gnus-summary-goto-article (car to))
10465       (error "Article history empty")))
10466   (gnus-summary-position-point))
10467
10468 ;; Summary commands and functions for limiting the summary buffer.
10469
10470 (defun gnus-summary-limit-to-articles (n)
10471   "Limit the summary buffer to the next N articles.
10472 If not given a prefix, use the process marked articles instead."
10473   (interactive "P")
10474   (gnus-set-global-variables)
10475   (prog1
10476       (let ((articles (gnus-summary-work-articles n)))
10477         (setq gnus-newsgroup-processable nil)
10478         (gnus-summary-limit articles))
10479     (gnus-summary-position-point)))
10480
10481 (defun gnus-summary-pop-limit (&optional total)
10482   "Restore the previous limit.
10483 If given a prefix, remove all limits."
10484   (interactive "P")
10485   (gnus-set-global-variables)
10486   (when total 
10487     (setq gnus-newsgroup-limits
10488           (list (mapcar (lambda (h) (mail-header-number h))
10489                         gnus-newsgroup-headers))))
10490   (unless gnus-newsgroup-limits
10491     (error "No limit to pop"))
10492   (prog1
10493       (gnus-summary-limit nil 'pop)
10494     (gnus-summary-position-point)))
10495
10496 (defun gnus-summary-limit-to-subject (subject &optional header)
10497   "Limit the summary buffer to articles that have subjects that match a regexp."
10498   (interactive "sRegexp: ")
10499   (unless header
10500     (setq header "subject"))
10501   (when (not (equal "" subject))
10502     (prog1
10503         (let ((articles (gnus-summary-find-matching
10504                          (or header "subject") subject 'all)))
10505           (or articles (error "Found no matches for \"%s\"" subject))
10506           (gnus-summary-limit articles))
10507       (gnus-summary-position-point))))
10508
10509 (defun gnus-summary-limit-to-author (from)
10510   "Limit the summary buffer to articles that have authors that match a regexp."
10511   (interactive "sRegexp: ")
10512   (gnus-summary-limit-to-subject from "from"))
10513
10514 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10515 (make-obsolete
10516  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10517
10518 (defun gnus-summary-limit-to-unread (&optional all)
10519   "Limit the summary buffer to articles that are not marked as read.
10520 If ALL is non-nil, limit strictly to unread articles."
10521   (interactive "P")
10522   (if all
10523       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10524     (gnus-summary-limit-to-marks
10525      ;; Concat all the marks that say that an article is read and have
10526      ;; those removed.
10527      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10528            gnus-killed-mark gnus-kill-file-mark
10529            gnus-low-score-mark gnus-expirable-mark
10530            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10531      'reverse)))
10532
10533 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10534 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10535
10536 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10537   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10538 If REVERSE, limit the summary buffer to articles that are not marked
10539 with MARKS.  MARKS can either be a string of marks or a list of marks.
10540 Returns how many articles were removed."
10541   (interactive "sMarks: ")
10542   (gnus-set-global-variables)
10543   (prog1
10544       (let ((data gnus-newsgroup-data)
10545             (marks (if (listp marks) marks
10546                      (append marks nil))) ; Transform to list.
10547             articles)
10548         (while data
10549           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10550                  (memq (gnus-data-mark (car data)) marks))
10551                (setq articles (cons (gnus-data-number (car data)) articles)))
10552           (setq data (cdr data)))
10553         (gnus-summary-limit articles))
10554     (gnus-summary-position-point)))
10555
10556 (defun gnus-summary-limit-to-score (&optional score)
10557   "Limit to articles with score at or above SCORE."
10558   (interactive "P")
10559   (gnus-set-global-variables)
10560   (setq score (if score
10561                   (prefix-numeric-value score)
10562                 (or gnus-summary-default-score 0)))
10563   (let ((data gnus-newsgroup-data)
10564         articles)
10565     (while data
10566       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10567                 score)
10568         (push (gnus-data-number (car data)) articles))
10569       (setq data (cdr data)))
10570     (prog1
10571         (gnus-summary-limit articles)
10572       (gnus-summary-position-point))))
10573
10574 (defun gnus-summary-limit-include-dormant ()
10575   "Display all the hidden articles that are marked as dormant."
10576   (interactive)
10577   (gnus-set-global-variables)
10578   (or gnus-newsgroup-dormant
10579       (error "There are no dormant articles in this group"))
10580   (prog1
10581       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10582     (gnus-summary-position-point)))
10583
10584 (defun gnus-summary-limit-exclude-dormant ()
10585   "Hide all dormant articles."
10586   (interactive)
10587   (gnus-set-global-variables)
10588   (prog1
10589       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10590     (gnus-summary-position-point)))
10591
10592 (defun gnus-summary-limit-exclude-childless-dormant ()
10593   "Hide all dormant articles that have no children."
10594   (interactive)
10595   (gnus-set-global-variables)
10596   (let ((data (gnus-data-list t))
10597         articles d children)
10598     ;; Find all articles that are either not dormant or have
10599     ;; children.
10600     (while (setq d (pop data))
10601       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10602                 (and (setq children 
10603                            (gnus-article-children (gnus-data-number d)))
10604                      (let (found)
10605                        (while children
10606                          (when (memq (car children) articles)
10607                            (setq children nil
10608                                  found t))
10609                          (pop children))
10610                        found)))
10611         (push (gnus-data-number d) articles)))
10612     ;; Do the limiting.
10613     (prog1
10614         (gnus-summary-limit articles)
10615       (gnus-summary-position-point))))
10616
10617 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10618   "Mark all unread excluded articles as read.
10619 If ALL, mark even excluded ticked and dormants as read."
10620   (interactive "P")
10621   (let ((articles (gnus-sorted-complement
10622                    (sort
10623                     (mapcar (lambda (h) (mail-header-number h))
10624                             gnus-newsgroup-headers)
10625                     '<)
10626                    (sort gnus-newsgroup-limit '<)))
10627         article)
10628     (setq gnus-newsgroup-unreads nil)
10629     (if all
10630         (setq gnus-newsgroup-dormant nil
10631               gnus-newsgroup-marked nil
10632               gnus-newsgroup-reads
10633               (nconc
10634                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10635                gnus-newsgroup-reads))
10636       (while (setq article (pop articles))
10637         (unless (or (memq article gnus-newsgroup-dormant)
10638                     (memq article gnus-newsgroup-marked))
10639           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10640
10641 (defun gnus-summary-limit (articles &optional pop)
10642   (if pop
10643       ;; We pop the previous limit off the stack and use that.
10644       (setq articles (car gnus-newsgroup-limits)
10645             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10646     ;; We use the new limit, so we push the old limit on the stack.
10647     (setq gnus-newsgroup-limits
10648           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10649   ;; Set the limit.
10650   (setq gnus-newsgroup-limit articles)
10651   (let ((total (length gnus-newsgroup-data))
10652         (data (gnus-data-find-list (gnus-summary-article-number)))
10653         found)
10654     ;; This will do all the work of generating the new summary buffer
10655     ;; according to the new limit.
10656     (gnus-summary-prepare)
10657     ;; Hide any threads, possibly.
10658     (and gnus-show-threads
10659          gnus-thread-hide-subtree
10660          (gnus-summary-hide-all-threads))
10661     ;; Try to return to the article you were at, or one in the
10662     ;; neighborhood.
10663     (if data
10664         ;; We try to find some article after the current one.
10665         (while data
10666           (and (gnus-summary-goto-subject
10667                 (gnus-data-number (car data)) nil t)
10668                (setq data nil
10669                      found t))
10670           (setq data (cdr data))))
10671     (or found
10672         ;; If there is no data, that means that we were after the last
10673         ;; article.  The same goes when we can't find any articles
10674         ;; after the current one.
10675         (progn
10676           (goto-char (point-max))
10677           (gnus-summary-find-prev)))
10678     ;; We return how many articles were removed from the summary
10679     ;; buffer as a result of the new limit.
10680     (- total (length gnus-newsgroup-data))))
10681
10682 (defsubst gnus-cut-thread (thread)
10683   "Go forwards in the thread until we find an article that we want to display."
10684   (when (eq gnus-fetch-old-headers 'some)
10685     ;; Deal with old-fetched headers.
10686     (while (and thread
10687                 (memq (mail-header-number (car thread)) 
10688                       gnus-newsgroup-ancient)
10689                 (<= (length (cdr thread)) 1))
10690       (setq thread (cadr thread))))
10691   ;; Deal with sparse threads.
10692   (when (or (eq gnus-build-sparse-threads 'some)
10693             (eq gnus-build-sparse-threads 'more))
10694     (while (and thread
10695                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10696                 (= (length (cdr thread)) 1))
10697       (setq thread (cadr thread))))
10698   thread)
10699
10700 (defun gnus-cut-threads (threads)
10701   "Cut off all uninteresting articles from the beginning of threads."
10702   (when (or (eq gnus-fetch-old-headers 'some)
10703             (eq gnus-build-sparse-threads 'some)
10704             (eq gnus-build-sparse-threads 'more))
10705     (let ((th threads))
10706       (while th
10707         (setcar th (gnus-cut-thread (car th)))
10708         (setq th (cdr th)))))
10709   ;; Remove nixed out threads.
10710   (delq nil threads))
10711
10712 (defun gnus-summary-initial-limit (&optional show-if-empty)
10713   "Figure out what the initial limit is supposed to be on group entry.
10714 This entails weeding out unwanted dormants, low-scored articles,
10715 fetch-old-headers verbiage, and so on."
10716   ;; Most groups have nothing to remove.
10717   (if (or gnus-inhibit-limiting
10718           (and (null gnus-newsgroup-dormant)
10719                (not (eq gnus-fetch-old-headers 'some))
10720                (null gnus-summary-expunge-below)
10721                (not (eq gnus-build-sparse-threads 'some))
10722                (not (eq gnus-build-sparse-threads 'more))
10723                (null gnus-thread-expunge-below)
10724                (not gnus-use-nocem)))
10725       () ; Do nothing.
10726     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10727     (setq gnus-newsgroup-limit nil)
10728     (mapatoms
10729      (lambda (node)
10730        (unless (car (symbol-value node))
10731          ;; These threads have no parents -- they are roots.
10732          (let ((nodes (cdr (symbol-value node)))
10733                thread)
10734            (while nodes
10735              (if (and gnus-thread-expunge-below
10736                       (< (gnus-thread-total-score (car nodes))
10737                          gnus-thread-expunge-below))
10738                  (gnus-expunge-thread (pop nodes))
10739                (setq thread (pop nodes))
10740                (gnus-summary-limit-children thread))))))
10741      gnus-newsgroup-dependencies)
10742     ;; If this limitation resulted in an empty group, we might
10743     ;; pop the previous limit and use it instead.
10744     (when (and (not gnus-newsgroup-limit)
10745                show-if-empty)
10746       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10747     gnus-newsgroup-limit))
10748
10749 (defun gnus-summary-limit-children (thread)
10750   "Return 1 if this subthread is visible and 0 if it is not."
10751   ;; First we get the number of visible children to this thread.  This
10752   ;; is done by recursing down the thread using this function, so this
10753   ;; will really go down to a leaf article first, before slowly
10754   ;; working its way up towards the root.
10755   (when thread
10756     (let ((children
10757            (if (cdr thread)
10758                (apply '+ (mapcar 'gnus-summary-limit-children
10759                                  (cdr thread)))
10760              0))
10761           (number (mail-header-number (car thread)))
10762           score)
10763       (if (or
10764            ;; If this article is dormant and has absolutely no visible
10765            ;; children, then this article isn't visible.
10766            (and (memq number gnus-newsgroup-dormant)
10767                 (= children 0))
10768            ;; If this is a "fetch-old-headered" and there is only one
10769            ;; visible child (or less), then we don't want this article.
10770            (and (eq gnus-fetch-old-headers 'some)
10771                 (memq number gnus-newsgroup-ancient)
10772                 (zerop children))
10773            ;; If this is a sparsely inserted article with no children,
10774            ;; we don't want it.
10775            (and (eq gnus-build-sparse-threads 'some)
10776                 (memq number gnus-newsgroup-sparse)
10777                 (zerop children))
10778            ;; If we use expunging, and this article is really
10779            ;; low-scored, then we don't want this article.
10780            (when (and gnus-summary-expunge-below
10781                       (< (setq score
10782                                (or (cdr (assq number gnus-newsgroup-scored))
10783                                    gnus-summary-default-score))
10784                          gnus-summary-expunge-below))
10785              ;; We increase the expunge-tally here, but that has
10786              ;; nothing to do with the limits, really.
10787              (incf gnus-newsgroup-expunged-tally)
10788              ;; We also mark as read here, if that's wanted.
10789              (when (and gnus-summary-mark-below
10790                         (< score gnus-summary-mark-below))
10791                (setq gnus-newsgroup-unreads
10792                      (delq number gnus-newsgroup-unreads))
10793                (if gnus-newsgroup-auto-expire
10794                    (push number gnus-newsgroup-expirable)
10795                  (push (cons number gnus-low-score-mark)
10796                        gnus-newsgroup-reads)))
10797              t)
10798            (and gnus-use-nocem
10799                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10800           ;; Nope, invisible article.
10801           0
10802         ;; Ok, this article is to be visible, so we add it to the limit
10803         ;; and return 1.
10804         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10805         1))))
10806
10807 (defun gnus-expunge-thread (thread)
10808   "Mark all articles in THREAD as read."
10809   (let* ((number (mail-header-number (car thread))))
10810     (incf gnus-newsgroup-expunged-tally)
10811     ;; We also mark as read here, if that's wanted.
10812     (setq gnus-newsgroup-unreads
10813           (delq number gnus-newsgroup-unreads))
10814     (if gnus-newsgroup-auto-expire
10815         (push number gnus-newsgroup-expirable)
10816       (push (cons number gnus-low-score-mark)
10817             gnus-newsgroup-reads)))
10818   ;; Go recursively through all subthreads.
10819   (mapcar 'gnus-expunge-thread (cdr thread)))
10820
10821 ;; Summary article oriented commands
10822
10823 (defun gnus-summary-refer-parent-article (n)
10824   "Refer parent article N times.
10825 The difference between N and the number of articles fetched is returned."
10826   (interactive "p")
10827   (gnus-set-global-variables)
10828   (while
10829       (and
10830        (> n 0)
10831        (let* ((header (gnus-summary-article-header))
10832               (ref
10833                ;; If we try to find the parent of the currently
10834                ;; displayed article, then we take a look at the actual
10835                ;; References header, since this is slightly more
10836                ;; reliable than the References field we got from the
10837                ;; server.
10838                (if (and (eq (mail-header-number header)
10839                             (cdr gnus-article-current))
10840                         (equal gnus-newsgroup-name
10841                                (car gnus-article-current)))
10842                    (save-excursion
10843                      (set-buffer gnus-original-article-buffer)
10844                      (nnheader-narrow-to-headers)
10845                      (prog1
10846                          (mail-fetch-field "references")
10847                        (widen)))
10848                  ;; It's not the current article, so we take a bet on
10849                  ;; the value we got from the server.
10850                  (mail-header-references header))))
10851          (if (setq ref (or ref (mail-header-references header)))
10852              (or (gnus-summary-refer-article (gnus-parent-id ref))
10853                  (gnus-message 1 "Couldn't find parent"))
10854            (gnus-message 1 "No references in article %d"
10855                          (gnus-summary-article-number))
10856            nil)))
10857     (setq n (1- n)))
10858   (gnus-summary-position-point)
10859   n)
10860
10861 (defun gnus-summary-refer-references ()
10862   "Fetch all articles mentioned in the References header.
10863 Return how many articles were fetched."
10864   (interactive)
10865   (gnus-set-global-variables)
10866   (let ((ref (mail-header-references (gnus-summary-article-header)))
10867         (current (gnus-summary-article-number))
10868         (n 0))
10869     ;; For each Message-ID in the References header...
10870     (while (string-match "<[^>]*>" ref)
10871       (incf n)
10872       ;; ... fetch that article.
10873       (gnus-summary-refer-article
10874        (prog1 (match-string 0 ref)
10875          (setq ref (substring ref (match-end 0))))))
10876     (gnus-summary-goto-subject current)
10877     (gnus-summary-position-point)
10878     n))
10879
10880 (defun gnus-summary-refer-article (message-id)
10881   "Fetch an article specified by MESSAGE-ID."
10882   (interactive "sMessage-ID: ")
10883   (when (and (stringp message-id)
10884              (not (zerop (length message-id))))
10885     ;; Construct the correct Message-ID if necessary.
10886     ;; Suggested by tale@pawl.rpi.edu.
10887     (unless (string-match "^<" message-id)
10888       (setq message-id (concat "<" message-id)))
10889     (unless (string-match ">$" message-id)
10890       (setq message-id (concat message-id ">")))
10891     (let ((header (car (gnus-gethash message-id
10892                                      gnus-newsgroup-dependencies))))
10893       (if header
10894           ;; The article is present in the buffer, to we just go to it.
10895           (gnus-summary-goto-article (mail-header-number header) nil t)
10896         ;; We fetch the article
10897         (let ((gnus-override-method 
10898                (and (gnus-news-group-p gnus-newsgroup-name)
10899                     gnus-refer-article-method))
10900               number)
10901           ;; Start the special refer-article method, if necessary.
10902           (when gnus-refer-article-method
10903             (gnus-check-server gnus-refer-article-method))
10904           ;; Fetch the header, and display the article.
10905           (if (setq number (gnus-summary-insert-subject message-id))
10906               (gnus-summary-select-article nil nil nil number)
10907             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10908
10909 (defun gnus-summary-enter-digest-group (&optional force)
10910   "Enter a digest group based on the current article."
10911   (interactive "P")
10912   (gnus-set-global-variables)
10913   (gnus-summary-select-article)
10914   (let ((name (format "%s-%d"
10915                       (gnus-group-prefixed-name
10916                        gnus-newsgroup-name (list 'nndoc ""))
10917                       gnus-current-article))
10918         (ogroup gnus-newsgroup-name)
10919         (case-fold-search t)
10920         (buf (current-buffer))
10921         dig)
10922     (save-excursion
10923       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10924       (insert-buffer-substring gnus-original-article-buffer)
10925       (narrow-to-region
10926        (goto-char (point-min))
10927        (or (search-forward "\n\n" nil t) (point)))
10928       (goto-char (point-min))
10929       (delete-matching-lines "^\\(Path\\):\\|^From ")
10930       (widen))
10931     (unwind-protect
10932         (if (gnus-group-read-ephemeral-group
10933              name `(nndoc ,name (nndoc-address
10934                                  ,(get-buffer dig))
10935                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10936             ;; Make all postings to this group go to the parent group.
10937             (nconc (gnus-info-params (gnus-get-info name))
10938                    (list (cons 'to-group ogroup)))
10939           ;; Couldn't select this doc group.
10940           (switch-to-buffer buf)
10941           (gnus-set-global-variables)
10942           (gnus-configure-windows 'summary)
10943           (gnus-message 3 "Article couldn't be entered?"))
10944       (kill-buffer dig))))
10945
10946 (defun gnus-summary-isearch-article (&optional regexp-p)
10947   "Do incremental search forward on the current article.
10948 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10949   (interactive "P")
10950   (gnus-set-global-variables)
10951   (gnus-summary-select-article)
10952   (gnus-configure-windows 'article)
10953   (gnus-eval-in-buffer-window
10954    gnus-article-buffer
10955    (goto-char (point-min))
10956    (isearch-forward regexp-p)))
10957
10958 (defun gnus-summary-search-article-forward (regexp &optional backward)
10959   "Search for an article containing REGEXP forward.
10960 If BACKWARD, search backward instead."
10961   (interactive
10962    (list (read-string
10963           (format "Search article %s (regexp%s): "
10964                   (if current-prefix-arg "backward" "forward")
10965                   (if gnus-last-search-regexp
10966                       (concat ", default " gnus-last-search-regexp)
10967                     "")))
10968          current-prefix-arg))
10969   (gnus-set-global-variables)
10970   (if (string-equal regexp "")
10971       (setq regexp (or gnus-last-search-regexp ""))
10972     (setq gnus-last-search-regexp regexp))
10973   (if (gnus-summary-search-article regexp backward)
10974       (gnus-article-set-window-start
10975        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10976     (error "Search failed: \"%s\"" regexp)))
10977
10978 (defun gnus-summary-search-article-backward (regexp)
10979   "Search for an article containing REGEXP backward."
10980   (interactive
10981    (list (read-string
10982           (format "Search article backward (regexp%s): "
10983                   (if gnus-last-search-regexp
10984                       (concat ", default " gnus-last-search-regexp)
10985                     "")))))
10986   (gnus-summary-search-article-forward regexp 'backward))
10987
10988 (defun gnus-summary-search-article (regexp &optional backward)
10989   "Search for an article containing REGEXP.
10990 Optional argument BACKWARD means do search for backward.
10991 gnus-select-article-hook is not called during the search."
10992   (let ((gnus-select-article-hook nil)  ;Disable hook.
10993         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10994         (re-search
10995          (if backward
10996              (function re-search-backward) (function re-search-forward)))
10997         (found nil)
10998         (last nil))
10999     ;; Hidden thread subtrees must be searched for ,too.
11000     (gnus-summary-show-all-threads)
11001     ;; First of all, search current article.
11002     ;; We don't want to read article again from NNTP server nor reset
11003     ;; current point.
11004     (gnus-summary-select-article)
11005     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11006     (setq last gnus-current-article)
11007     (gnus-eval-in-buffer-window
11008      gnus-article-buffer
11009      (save-restriction
11010        (widen)
11011        ;; Begin search from current point.
11012        (setq found (funcall re-search regexp nil t))))
11013     ;; Then search next articles.
11014     (while (and (not found)
11015                 (gnus-summary-display-article
11016                  (if backward (gnus-summary-find-prev)
11017                    (gnus-summary-find-next))))
11018       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11019       (gnus-eval-in-buffer-window
11020        gnus-article-buffer
11021        (save-restriction
11022          (widen)
11023          (goto-char (if backward (point-max) (point-min)))
11024          (setq found (funcall re-search regexp nil t)))))
11025     (message "")
11026     ;; Adjust article pointer.
11027     (or (eq last gnus-current-article)
11028         (setq gnus-last-article last))
11029     ;; Return T if found such article.
11030     found))
11031
11032 (defun gnus-summary-find-matching (header regexp &optional backward unread
11033                                           not-case-fold)
11034   "Return a list of all articles that match REGEXP on HEADER.
11035 The search stars on the current article and goes forwards unless
11036 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11037 If UNREAD is non-nil, only unread articles will
11038 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11039 in the comparisons."
11040   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11041                 (gnus-data-find-list
11042                  (gnus-summary-article-number) (gnus-data-list backward))))
11043         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11044         (case-fold-search (not not-case-fold))
11045         articles d)
11046     (or (fboundp (intern (concat "mail-header-" header)))
11047         (error "%s is not a valid header" header))
11048     (while data
11049       (setq d (car data))
11050       (and (or (not unread)             ; We want all articles...
11051                (gnus-data-unread-p d))  ; Or just unreads.
11052            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11053            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11054            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11055       (setq data (cdr data)))
11056     (nreverse articles)))
11057
11058 (defun gnus-summary-execute-command (header regexp command &optional backward)
11059   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11060 If HEADER is an empty string (or nil), the match is done on the entire
11061 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11062   (interactive
11063    (list (let ((completion-ignore-case t))
11064            (completing-read
11065             "Header name: "
11066             (mapcar (lambda (string) (list string))
11067                     '("Number" "Subject" "From" "Lines" "Date"
11068                       "Message-ID" "Xref" "References" "Body"))
11069             nil 'require-match))
11070          (read-string "Regexp: ")
11071          (read-key-sequence "Command: ")
11072          current-prefix-arg))
11073   (when (equal header "Body")
11074     (setq header ""))
11075   (gnus-set-global-variables)
11076   ;; Hidden thread subtrees must be searched as well.
11077   (gnus-summary-show-all-threads)
11078   ;; We don't want to change current point nor window configuration.
11079   (save-excursion
11080     (save-window-excursion
11081       (gnus-message 6 "Executing %s..." (key-description command))
11082       ;; We'd like to execute COMMAND interactively so as to give arguments.
11083       (gnus-execute header regexp
11084                     `(lambda () (call-interactively ',(key-binding command)))
11085                     backward)
11086       (gnus-message 6 "Executing %s...done" (key-description command)))))
11087
11088 (defun gnus-summary-beginning-of-article ()
11089   "Scroll the article back to the beginning."
11090   (interactive)
11091   (gnus-set-global-variables)
11092   (gnus-summary-select-article)
11093   (gnus-configure-windows 'article)
11094   (gnus-eval-in-buffer-window
11095    gnus-article-buffer
11096    (widen)
11097    (goto-char (point-min))
11098    (and gnus-break-pages (gnus-narrow-to-page))))
11099
11100 (defun gnus-summary-end-of-article ()
11101   "Scroll to the end of the article."
11102   (interactive)
11103   (gnus-set-global-variables)
11104   (gnus-summary-select-article)
11105   (gnus-configure-windows 'article)
11106   (gnus-eval-in-buffer-window
11107    gnus-article-buffer
11108    (widen)
11109    (goto-char (point-max))
11110    (recenter -3)
11111    (and gnus-break-pages (gnus-narrow-to-page))))
11112
11113 (defun gnus-summary-show-article (&optional arg)
11114   "Force re-fetching of the current article.
11115 If ARG (the prefix) is non-nil, show the raw article without any
11116 article massaging functions being run."
11117   (interactive "P")
11118   (gnus-set-global-variables)
11119   (if (not arg)
11120       ;; Select the article the normal way.
11121       (gnus-summary-select-article nil 'force)
11122     ;; Bind the article treatment functions to nil.
11123     (let ((gnus-have-all-headers t)
11124           gnus-article-display-hook
11125           gnus-article-prepare-hook
11126           gnus-break-pages
11127           gnus-visual)
11128       (gnus-summary-select-article nil 'force)))
11129 ;  (gnus-configure-windows 'article)
11130   (gnus-summary-position-point))
11131
11132 (defun gnus-summary-verbose-headers (&optional arg)
11133   "Toggle permanent full header display.
11134 If ARG is a positive number, turn header display on.
11135 If ARG is a negative number, turn header display off."
11136   (interactive "P")
11137   (gnus-set-global-variables)
11138   (gnus-summary-toggle-header arg)
11139   (setq gnus-show-all-headers
11140         (cond ((or (not (numberp arg))
11141                    (zerop arg))
11142                (not gnus-show-all-headers))
11143               ((natnump arg)
11144                t))))
11145
11146 (defun gnus-summary-toggle-header (&optional arg)
11147   "Show the headers if they are hidden, or hide them if they are shown.
11148 If ARG is a positive number, show the entire header.
11149 If ARG is a negative number, hide the unwanted header lines."
11150   (interactive "P")
11151   (gnus-set-global-variables)
11152   (save-excursion
11153     (set-buffer gnus-article-buffer)
11154     (let* ((buffer-read-only nil)
11155            (inhibit-point-motion-hooks t)
11156            (hidden (text-property-any
11157                     (goto-char (point-min)) (search-forward "\n\n")
11158                     'invisible t))
11159            e)
11160       (goto-char (point-min))
11161       (when (search-forward "\n\n" nil t)
11162         (delete-region (point-min) (1- (point))))
11163       (goto-char (point-min))
11164       (save-excursion
11165         (set-buffer gnus-original-article-buffer)
11166         (goto-char (point-min))
11167         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11168       (insert-buffer-substring gnus-original-article-buffer 1 e)
11169       (let ((gnus-inhibit-hiding t))
11170         (run-hooks 'gnus-article-display-hook))
11171       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11172           (gnus-article-hide-headers)))))
11173
11174 (defun gnus-summary-show-all-headers ()
11175   "Make all header lines visible."
11176   (interactive)
11177   (gnus-set-global-variables)
11178   (gnus-article-show-all-headers))
11179
11180 (defun gnus-summary-toggle-mime (&optional arg)
11181   "Toggle MIME processing.
11182 If ARG is a positive number, turn MIME processing on."
11183   (interactive "P")
11184   (gnus-set-global-variables)
11185   (setq gnus-show-mime
11186         (if (null arg) (not gnus-show-mime)
11187           (> (prefix-numeric-value arg) 0)))
11188   (gnus-summary-select-article t 'force))
11189
11190 (defun gnus-summary-caesar-message (&optional arg)
11191   "Caesar rotate the current article by 13.
11192 The numerical prefix specifies how manu places to rotate each letter
11193 forward."
11194   (interactive "P")
11195   (gnus-set-global-variables)
11196   (gnus-summary-select-article)
11197   (let ((mail-header-separator ""))
11198     (gnus-eval-in-buffer-window
11199      gnus-article-buffer
11200      (save-restriction
11201        (widen)
11202        (let ((start (window-start)))
11203          (news-caesar-buffer-body arg)
11204          (set-window-start (get-buffer-window (current-buffer)) start))))))
11205
11206 (defun gnus-summary-stop-page-breaking ()
11207   "Stop page breaking in the current article."
11208   (interactive)
11209   (gnus-set-global-variables)
11210   (gnus-summary-select-article)
11211   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11212
11213 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11214   "Move the current article to a different newsgroup.
11215 If N is a positive number, move the N next articles.
11216 If N is a negative number, move the N previous articles.
11217 If N is nil and any articles have been marked with the process mark,
11218 move those articles instead.
11219 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11220 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11221 re-spool using this method.
11222
11223 For this function to work, both the current newsgroup and the
11224 newsgroup that you want to move to have to support the `request-move'
11225 and `request-accept' functions."
11226   (interactive "P")
11227   (unless action (setq action 'move))
11228   (gnus-set-global-variables)
11229   ;; Check whether the source group supports the required functions.
11230   (cond ((and (eq action 'move)
11231               (not (gnus-check-backend-function
11232                     'request-move-article gnus-newsgroup-name)))
11233          (error "The current group does not support article moving"))
11234         ((and (eq action 'crosspost)
11235               (not (gnus-check-backend-function
11236                     'request-replace-article gnus-newsgroup-name)))
11237          (error "The current group does not support article editing")))
11238   (let ((articles (gnus-summary-work-articles n))
11239         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11240         (names '((move "Move" "Moving")
11241                  (copy "Copy" "Copying")
11242                  (crosspost "Crosspost" "Crossposting")))
11243         (copy-buf (save-excursion
11244                     (nnheader-set-temp-buffer " *copy article*")))
11245         art-group to-method new-xref article to-groups)
11246     (unless (assq action names)
11247       (error "Unknown action %s" action))
11248     ;; Read the newsgroup name.
11249     (when (and (not to-newsgroup)
11250                (not select-method))
11251       (setq to-newsgroup
11252             (gnus-read-move-group-name
11253              (cadr (assq action names))
11254              (symbol-value (intern (format "gnus-current-%s-group" action)))
11255              articles prefix))
11256       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11257     (setq to-method (or select-method 
11258                         (gnus-find-method-for-group to-newsgroup)))
11259     ;; Check the method we are to move this article to...
11260     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11261         (error "%s does not support article copying" (car to-method)))
11262     (or (gnus-check-server to-method)
11263         (error "Can't open server %s" (car to-method)))
11264     (gnus-message 6 "%s to %s: %s..."
11265                   (caddr (assq action names))
11266                   (or (car select-method) to-newsgroup) articles)
11267     (while articles
11268       (setq article (pop articles))
11269       (setq
11270        art-group
11271        (cond
11272         ;; Move the article.
11273         ((eq action 'move)
11274          (gnus-request-move-article
11275           article                       ; Article to move
11276           gnus-newsgroup-name           ; From newsgrouo
11277           (nth 1 (gnus-find-method-for-group
11278                   gnus-newsgroup-name)) ; Server
11279           (list 'gnus-request-accept-article
11280                 to-newsgroup (list 'quote select-method)
11281                 (not articles))         ; Accept form
11282           (not articles)))              ; Only save nov last time
11283         ;; Copy the article.
11284         ((eq action 'copy)
11285          (save-excursion
11286            (set-buffer copy-buf)
11287            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11288            (gnus-request-accept-article
11289             to-newsgroup select-method (not articles))))
11290         ;; Crosspost the article.
11291         ((eq action 'crosspost)
11292          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11293            (setq new-xref (concat gnus-newsgroup-name ":" article))
11294            (if (and xref (not (string= xref "")))
11295                (progn
11296                  (when (string-match "^Xref: " xref)
11297                    (setq xref (substring xref (match-end 0))))
11298                  (setq new-xref (concat xref " " new-xref)))
11299              (setq new-xref (concat (system-name) " " new-xref)))
11300            (save-excursion
11301              (set-buffer copy-buf)
11302              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11303              (nnheader-replace-header "xref" new-xref)
11304              (gnus-request-accept-article
11305               to-newsgroup select-method (not articles)))))))
11306       (if (not art-group)
11307           (gnus-message 1 "Couldn't %s article %s"
11308                         (cadr (assq action names)) article)
11309         (let* ((entry
11310                 (or
11311                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11312                  (gnus-gethash
11313                   (gnus-group-prefixed-name
11314                    (car art-group)
11315                    (or select-method 
11316                        (gnus-find-method-for-group to-newsgroup)))
11317                   gnus-newsrc-hashtb)))
11318                (info (nth 2 entry))
11319                (to-group (gnus-info-group info)))
11320           ;; Update the group that has been moved to.
11321           (when (and info
11322                      (memq action '(move copy)))
11323             (unless (member to-group to-groups)
11324               (push to-group to-groups))
11325
11326             (unless (memq article gnus-newsgroup-unreads)
11327               (gnus-info-set-read
11328                info (gnus-add-to-range (gnus-info-read info)
11329                                        (list (cdr art-group)))))
11330
11331             ;; Copy any marks over to the new group.
11332             (let ((marks gnus-article-mark-lists)
11333                   (to-article (cdr art-group)))
11334
11335               ;; See whether the article is to be put in the cache.
11336               (when gnus-use-cache
11337                 (gnus-cache-possibly-enter-article
11338                  to-group to-article
11339                  (let ((header (copy-sequence
11340                                 (gnus-summary-article-header article))))
11341                    (mail-header-set-number header to-article)
11342                    header)
11343                  (memq article gnus-newsgroup-marked)
11344                  (memq article gnus-newsgroup-dormant)
11345                  (memq article gnus-newsgroup-unreads)))
11346
11347               (while marks
11348                 (when (memq article (symbol-value
11349                                      (intern (format "gnus-newsgroup-%s"
11350                                                      (caar marks)))))
11351                   ;; If the other group is the same as this group,
11352                   ;; then we have to add the mark to the list.
11353                   (when (equal to-group gnus-newsgroup-name)
11354                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11355                          (cons to-article
11356                                (symbol-value
11357                                 (intern (format "gnus-newsgroup-%s"
11358                                                 (caar marks)))))))
11359                   ;; Copy mark to other group.
11360                   (gnus-add-marked-articles
11361                    to-group (cdar marks) (list to-article) info))
11362                 (setq marks (cdr marks)))))
11363
11364           ;; Update the Xref header in this article to point to
11365           ;; the new crossposted article we have just created.
11366           (when (eq action 'crosspost)
11367             (save-excursion
11368               (set-buffer copy-buf)
11369               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11370               (nnheader-replace-header
11371                "xref" (concat new-xref " " (gnus-group-prefixed-name
11372                                             (car art-group) to-method)
11373                               ":" (cdr art-group)))
11374               (gnus-request-replace-article
11375                article gnus-newsgroup-name (current-buffer)))))
11376
11377         (gnus-summary-goto-subject article)
11378         (when (eq action 'move)
11379           (gnus-summary-mark-article article gnus-canceled-mark)))
11380       (gnus-summary-remove-process-mark article))
11381     ;; Re-activate all groups that have been moved to.
11382     (while to-groups
11383       (gnus-activate-group (pop to-groups)))
11384     
11385     (gnus-kill-buffer copy-buf)
11386     (gnus-summary-position-point)
11387     (gnus-set-mode-line 'summary)))
11388
11389 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11390   "Move the current article to a different newsgroup.
11391 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11392 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11393 re-spool using this method."
11394   (interactive "P")
11395   (gnus-summary-move-article n nil select-method 'copy))
11396
11397 (defun gnus-summary-crosspost-article (&optional n)
11398   "Crosspost the current article to some other group."
11399   (interactive "P")
11400   (gnus-summary-move-article n nil nil 'crosspost))
11401
11402 (defvar gnus-summary-respool-default-method nil
11403   "Default method for respooling an article.  
11404 If nil, use to the current newsgroup method.")
11405
11406 (defun gnus-summary-respool-article (&optional n method)
11407   "Respool the current article.
11408 The article will be squeezed through the mail spooling process again,
11409 which means that it will be put in some mail newsgroup or other
11410 depending on `nnmail-split-methods'.
11411 If N is a positive number, respool the N next articles.
11412 If N is a negative number, respool the N previous articles.
11413 If N is nil and any articles have been marked with the process mark,
11414 respool those articles instead.
11415
11416 Respooling can be done both from mail groups and \"real\" newsgroups.
11417 In the former case, the articles in question will be moved from the
11418 current group into whatever groups they are destined to.  In the
11419 latter case, they will be copied into the relevant groups."
11420   (interactive 
11421    (list current-prefix-arg
11422          (let* ((methods (gnus-methods-using 'respool))
11423                 (methname
11424                  (symbol-name (or gnus-summary-respool-default-method
11425                                   (car (gnus-find-method-for-group
11426                                         gnus-newsgroup-name)))))
11427                 (method
11428                  (gnus-completing-read 
11429                   methname "What backend do you want to use when? "
11430                   methods nil t nil 'gnus-method-history))
11431                 ms)
11432            (cond
11433             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11434              (list (intern method) ""))
11435             ((= 1 (length ms))
11436              (car ms))
11437             (t
11438              (cdr (completing-read 
11439                    "Server name: "
11440                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11441   (gnus-set-global-variables)
11442   (unless method
11443     (error "No method given for respooling"))
11444   (if (assoc (symbol-name
11445               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11446              (gnus-methods-using 'respool))
11447       (gnus-summary-move-article n nil method)
11448     (gnus-summary-copy-article n nil method)))
11449
11450 (defun gnus-summary-import-article (file)
11451   "Import a random file into a mail newsgroup."
11452   (interactive "fImport file: ")
11453   (gnus-set-global-variables)
11454   (let ((group gnus-newsgroup-name)
11455         (now (current-time))
11456         atts lines)
11457     (or (gnus-check-backend-function 'request-accept-article group)
11458         (error "%s does not support article importing" group))
11459     (or (file-readable-p file)
11460         (not (file-regular-p file))
11461         (error "Can't read %s" file))
11462     (save-excursion
11463       (set-buffer (get-buffer-create " *import file*"))
11464       (buffer-disable-undo (current-buffer))
11465       (erase-buffer)
11466       (insert-file-contents file)
11467       (goto-char (point-min))
11468       (unless (nnheader-article-p)
11469         ;; This doesn't look like an article, so we fudge some headers.
11470         (setq atts (file-attributes file)
11471               lines (count-lines (point-min) (point-max)))
11472         (insert "From: " (read-string "From: ") "\n"
11473                 "Subject: " (read-string "Subject: ") "\n"
11474                 "Date: " (timezone-make-date-arpa-standard
11475                           (current-time-string (nth 5 atts))
11476                           (current-time-zone now)
11477                           (current-time-zone now)) "\n"
11478                 "Message-ID: " (gnus-inews-message-id) "\n"
11479                 "Lines: " (int-to-string lines) "\n"
11480                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11481       (gnus-request-accept-article group nil t)
11482       (kill-buffer (current-buffer)))))
11483
11484 (defun gnus-summary-expire-articles ()
11485   "Expire all articles that are marked as expirable in the current group."
11486   (interactive)
11487   (gnus-set-global-variables)
11488   (when (gnus-check-backend-function
11489          'request-expire-articles gnus-newsgroup-name)
11490     ;; This backend supports expiry.
11491     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11492            (expirable (if total
11493                           (gnus-list-of-read-articles gnus-newsgroup-name)
11494                         (setq gnus-newsgroup-expirable
11495                               (sort gnus-newsgroup-expirable '<))))
11496            (expiry-wait (gnus-group-get-parameter
11497                          gnus-newsgroup-name 'expiry-wait))
11498            es)
11499       (when expirable
11500         ;; There are expirable articles in this group, so we run them
11501         ;; through the expiry process.
11502         (gnus-message 6 "Expiring articles...")
11503         ;; The list of articles that weren't expired is returned.
11504         (if expiry-wait
11505             (let ((nnmail-expiry-wait-function nil)
11506                   (nnmail-expiry-wait expiry-wait))
11507               (setq es (gnus-request-expire-articles
11508                         expirable gnus-newsgroup-name)))
11509           (setq es (gnus-request-expire-articles
11510                     expirable gnus-newsgroup-name)))
11511         (or total (setq gnus-newsgroup-expirable es))
11512         ;; We go through the old list of expirable, and mark all
11513         ;; really expired articles as nonexistent.
11514         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11515           (let ((gnus-use-cache nil))
11516             (while expirable
11517               (unless (memq (car expirable) es)
11518                 (when (gnus-data-find (car expirable))
11519                   (gnus-summary-mark-article
11520                    (car expirable) gnus-canceled-mark)))
11521               (setq expirable (cdr expirable)))))
11522         (gnus-message 6 "Expiring articles...done")))))
11523
11524 (defun gnus-summary-expire-articles-now ()
11525   "Expunge all expirable articles in the current group.
11526 This means that *all* articles that are marked as expirable will be
11527 deleted forever, right now."
11528   (interactive)
11529   (gnus-set-global-variables)
11530   (or gnus-expert-user
11531       (gnus-y-or-n-p
11532        "Are you really, really, really sure you want to expunge? ")
11533       (error "Phew!"))
11534   (let ((nnmail-expiry-wait 'immediate)
11535         (nnmail-expiry-wait-function nil))
11536     (gnus-summary-expire-articles)))
11537
11538 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11539 (defun gnus-summary-delete-article (&optional n)
11540   "Delete the N next (mail) articles.
11541 This command actually deletes articles.  This is not a marking
11542 command.  The article will disappear forever from your life, never to
11543 return.
11544 If N is negative, delete backwards.
11545 If N is nil and articles have been marked with the process mark,
11546 delete these instead."
11547   (interactive "P")
11548   (gnus-set-global-variables)
11549   (or (gnus-check-backend-function 'request-expire-articles
11550                                    gnus-newsgroup-name)
11551       (error "The current newsgroup does not support article deletion."))
11552   ;; Compute the list of articles to delete.
11553   (let ((articles (gnus-summary-work-articles n))
11554         not-deleted)
11555     (if (and gnus-novice-user
11556              (not (gnus-y-or-n-p
11557                    (format "Do you really want to delete %s forever? "
11558                            (if (> (length articles) 1) "these articles"
11559                              "this article")))))
11560         ()
11561       ;; Delete the articles.
11562       (setq not-deleted (gnus-request-expire-articles
11563                          articles gnus-newsgroup-name 'force))
11564       (while articles
11565         (gnus-summary-remove-process-mark (car articles))
11566         ;; The backend might not have been able to delete the article
11567         ;; after all.
11568         (or (memq (car articles) not-deleted)
11569             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11570         (setq articles (cdr articles))))
11571     (gnus-summary-position-point)
11572     (gnus-set-mode-line 'summary)
11573     not-deleted))
11574
11575 (defun gnus-summary-edit-article (&optional force)
11576   "Enter into a buffer and edit the current article.
11577 This will have permanent effect only in mail groups.
11578 If FORCE is non-nil, allow editing of articles even in read-only
11579 groups."
11580   (interactive "P")
11581   (save-excursion
11582     (set-buffer gnus-summary-buffer)
11583     (gnus-set-global-variables)
11584     (when (and (not force)
11585                (gnus-group-read-only-p))
11586       (error "The current newsgroup does not support article editing."))
11587     (gnus-summary-select-article t nil t)
11588     (gnus-configure-windows 'article)
11589     (select-window (get-buffer-window gnus-article-buffer))
11590     (gnus-message 6 "C-c C-c to end edits")
11591     (setq buffer-read-only nil)
11592     (text-mode)
11593     (use-local-map (copy-keymap (current-local-map)))
11594     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11595     (buffer-enable-undo)
11596     (widen)
11597     (goto-char (point-min))
11598     (search-forward "\n\n" nil t)))
11599
11600 (defun gnus-summary-edit-article-done ()
11601   "Make edits to the current article permanent."
11602   (interactive)
11603   (if (gnus-group-read-only-p)
11604       (progn
11605         (gnus-summary-edit-article-postpone)
11606         (gnus-error
11607          1 "The current newsgroup does not support article editing."))
11608     (let ((buf (format "%s" (buffer-string))))
11609       (erase-buffer)
11610       (insert buf)
11611       (if (not (gnus-request-replace-article
11612                 (cdr gnus-article-current) (car gnus-article-current)
11613                 (current-buffer)))
11614           (error "Couldn't replace article.")
11615         (gnus-article-mode)
11616         (use-local-map gnus-article-mode-map)
11617         (setq buffer-read-only t)
11618         (buffer-disable-undo (current-buffer))
11619         (gnus-configure-windows 'summary)
11620         (gnus-summary-update-article (cdr gnus-article-current))
11621         (when gnus-use-cache
11622           (gnus-cache-update-article 
11623            (cdr gnus-article-current) (car gnus-article-current))))
11624       (save-excursion
11625         (when (get-buffer gnus-original-article-buffer)
11626           (set-buffer gnus-original-article-buffer)
11627           (setq gnus-original-article nil)))
11628       (setq gnus-article-current nil
11629             gnus-current-article nil)
11630       (run-hooks 'gnus-article-display-hook)
11631       (and (gnus-visual-p 'summary-highlight 'highlight)
11632            (run-hooks 'gnus-visual-mark-article-hook)))))
11633
11634 (defun gnus-summary-edit-article-postpone ()
11635   "Postpone changes to the current article."
11636   (interactive)
11637   (gnus-article-mode)
11638   (use-local-map gnus-article-mode-map)
11639   (setq buffer-read-only t)
11640   (buffer-disable-undo (current-buffer))
11641   (gnus-configure-windows 'summary)
11642   (and (gnus-visual-p 'summary-highlight 'highlight)
11643        (run-hooks 'gnus-visual-mark-article-hook)))
11644
11645 (defun gnus-summary-respool-query ()
11646   "Query where the respool algorithm would put this article."
11647   (interactive)
11648   (gnus-set-global-variables)
11649   (gnus-summary-select-article)
11650   (save-excursion
11651     (set-buffer gnus-article-buffer)
11652     (save-restriction
11653       (goto-char (point-min))
11654       (search-forward "\n\n")
11655       (narrow-to-region (point-min) (point))
11656       (pp-eval-expression
11657        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11658
11659 ;; Summary score commands.
11660
11661 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11662
11663 (defun gnus-summary-raise-score (n)
11664   "Raise the score of the current article by N."
11665   (interactive "p")
11666   (gnus-set-global-variables)
11667   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11668
11669 (defun gnus-summary-set-score (n)
11670   "Set the score of the current article to N."
11671   (interactive "p")
11672   (gnus-set-global-variables)
11673   (save-excursion
11674     (gnus-summary-show-thread)
11675     (let ((buffer-read-only nil))
11676       ;; Set score.
11677       (gnus-summary-update-mark
11678        (if (= n (or gnus-summary-default-score 0)) ? 
11679          (if (< n (or gnus-summary-default-score 0))
11680              gnus-score-below-mark gnus-score-over-mark)) 'score))
11681     (let* ((article (gnus-summary-article-number))
11682            (score (assq article gnus-newsgroup-scored)))
11683       (if score (setcdr score n)
11684         (setq gnus-newsgroup-scored
11685               (cons (cons article n) gnus-newsgroup-scored))))
11686     (gnus-summary-update-line)))
11687
11688 (defun gnus-summary-current-score ()
11689   "Return the score of the current article."
11690   (interactive)
11691   (gnus-set-global-variables)
11692   (gnus-message 1 "%s" (gnus-summary-article-score)))
11693
11694 ;; Summary marking commands.
11695
11696 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11697   "Mark articles which has the same subject as read, and then select the next.
11698 If UNMARK is positive, remove any kind of mark.
11699 If UNMARK is negative, tick articles."
11700   (interactive "P")
11701   (gnus-set-global-variables)
11702   (if unmark
11703       (setq unmark (prefix-numeric-value unmark)))
11704   (let ((count
11705          (gnus-summary-mark-same-subject
11706           (gnus-summary-article-subject) unmark)))
11707     ;; Select next unread article.  If auto-select-same mode, should
11708     ;; select the first unread article.
11709     (gnus-summary-next-article t (and gnus-auto-select-same
11710                                       (gnus-summary-article-subject)))
11711     (gnus-message 7 "%d article%s marked as %s"
11712                   count (if (= count 1) " is" "s are")
11713                   (if unmark "unread" "read"))))
11714
11715 (defun gnus-summary-kill-same-subject (&optional unmark)
11716   "Mark articles which has the same subject as read.
11717 If UNMARK is positive, remove any kind of mark.
11718 If UNMARK is negative, tick articles."
11719   (interactive "P")
11720   (gnus-set-global-variables)
11721   (if unmark
11722       (setq unmark (prefix-numeric-value unmark)))
11723   (let ((count
11724          (gnus-summary-mark-same-subject
11725           (gnus-summary-article-subject) unmark)))
11726     ;; If marked as read, go to next unread subject.
11727     (if (null unmark)
11728         ;; Go to next unread subject.
11729         (gnus-summary-next-subject 1 t))
11730     (gnus-message 7 "%d articles are marked as %s"
11731                   count (if unmark "unread" "read"))))
11732
11733 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11734   "Mark articles with same SUBJECT as read, and return marked number.
11735 If optional argument UNMARK is positive, remove any kinds of marks.
11736 If optional argument UNMARK is negative, mark articles as unread instead."
11737   (let ((count 1))
11738     (save-excursion
11739       (cond
11740        ((null unmark)                   ; Mark as read.
11741         (while (and
11742                 (progn
11743                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11744                   (gnus-summary-show-thread) t)
11745                 (gnus-summary-find-subject subject))
11746           (setq count (1+ count))))
11747        ((> unmark 0)                    ; Tick.
11748         (while (and
11749                 (progn
11750                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11751                   (gnus-summary-show-thread) t)
11752                 (gnus-summary-find-subject subject))
11753           (setq count (1+ count))))
11754        (t                               ; Mark as unread.
11755         (while (and
11756                 (progn
11757                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11758                   (gnus-summary-show-thread) t)
11759                 (gnus-summary-find-subject subject))
11760           (setq count (1+ count)))))
11761       (gnus-set-mode-line 'summary)
11762       ;; Return the number of marked articles.
11763       count)))
11764
11765 (defun gnus-summary-mark-as-processable (n &optional unmark)
11766   "Set the process mark on the next N articles.
11767 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11768 the process mark instead.  The difference between N and the actual
11769 number of articles marked is returned."
11770   (interactive "p")
11771   (gnus-set-global-variables)
11772   (let ((backward (< n 0))
11773         (n (abs n)))
11774     (while (and
11775             (> n 0)
11776             (if unmark
11777                 (gnus-summary-remove-process-mark
11778                  (gnus-summary-article-number))
11779               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11780             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11781       (setq n (1- n)))
11782     (if (/= 0 n) (gnus-message 7 "No more articles"))
11783     (gnus-summary-recenter)
11784     (gnus-summary-position-point)
11785     n))
11786
11787 (defun gnus-summary-unmark-as-processable (n)
11788   "Remove the process mark from the next N articles.
11789 If N is negative, mark backward instead.  The difference between N and
11790 the actual number of articles marked is returned."
11791   (interactive "p")
11792   (gnus-set-global-variables)
11793   (gnus-summary-mark-as-processable n t))
11794
11795 (defun gnus-summary-unmark-all-processable ()
11796   "Remove the process mark from all articles."
11797   (interactive)
11798   (gnus-set-global-variables)
11799   (save-excursion
11800     (while gnus-newsgroup-processable
11801       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11802   (gnus-summary-position-point))
11803
11804 (defun gnus-summary-mark-as-expirable (n)
11805   "Mark N articles forward as expirable.
11806 If N is negative, mark backward instead.  The difference between N and
11807 the actual number of articles marked is returned."
11808   (interactive "p")
11809   (gnus-set-global-variables)
11810   (gnus-summary-mark-forward n gnus-expirable-mark))
11811
11812 (defun gnus-summary-mark-article-as-replied (article)
11813   "Mark ARTICLE replied and update the summary line."
11814   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11815   (let ((buffer-read-only nil))
11816     (when (gnus-summary-goto-subject article)
11817       (gnus-summary-update-secondary-mark article))))
11818
11819 (defun gnus-summary-set-bookmark (article)
11820   "Set a bookmark in current article."
11821   (interactive (list (gnus-summary-article-number)))
11822   (gnus-set-global-variables)
11823   (if (or (not (get-buffer gnus-article-buffer))
11824           (not gnus-current-article)
11825           (not gnus-article-current)
11826           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11827       (error "No current article selected"))
11828   ;; Remove old bookmark, if one exists.
11829   (let ((old (assq article gnus-newsgroup-bookmarks)))
11830     (if old (setq gnus-newsgroup-bookmarks
11831                   (delq old gnus-newsgroup-bookmarks))))
11832   ;; Set the new bookmark, which is on the form
11833   ;; (article-number . line-number-in-body).
11834   (setq gnus-newsgroup-bookmarks
11835         (cons
11836          (cons article
11837                (save-excursion
11838                  (set-buffer gnus-article-buffer)
11839                  (count-lines
11840                   (min (point)
11841                        (save-excursion
11842                          (goto-char (point-min))
11843                          (search-forward "\n\n" nil t)
11844                          (point)))
11845                   (point))))
11846          gnus-newsgroup-bookmarks))
11847   (gnus-message 6 "A bookmark has been added to the current article."))
11848
11849 (defun gnus-summary-remove-bookmark (article)
11850   "Remove the bookmark from the current article."
11851   (interactive (list (gnus-summary-article-number)))
11852   (gnus-set-global-variables)
11853   ;; Remove old bookmark, if one exists.
11854   (let ((old (assq article gnus-newsgroup-bookmarks)))
11855     (if old
11856         (progn
11857           (setq gnus-newsgroup-bookmarks
11858                 (delq old gnus-newsgroup-bookmarks))
11859           (gnus-message 6 "Removed bookmark."))
11860       (gnus-message 6 "No bookmark in current article."))))
11861
11862 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11863 (defun gnus-summary-mark-as-dormant (n)
11864   "Mark N articles forward as dormant.
11865 If N is negative, mark backward instead.  The difference between N and
11866 the actual number of articles marked is returned."
11867   (interactive "p")
11868   (gnus-set-global-variables)
11869   (gnus-summary-mark-forward n gnus-dormant-mark))
11870
11871 (defun gnus-summary-set-process-mark (article)
11872   "Set the process mark on ARTICLE and update the summary line."
11873   (setq gnus-newsgroup-processable
11874         (cons article
11875               (delq article gnus-newsgroup-processable)))
11876   (when (gnus-summary-goto-subject article)
11877     (gnus-summary-show-thread)
11878     (gnus-summary-update-secondary-mark article)))
11879
11880 (defun gnus-summary-remove-process-mark (article)
11881   "Remove the process mark from ARTICLE and update the summary line."
11882   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11883   (when (gnus-summary-goto-subject article)
11884     (gnus-summary-show-thread)
11885     (gnus-summary-update-secondary-mark article)))
11886
11887 (defun gnus-summary-set-saved-mark (article)
11888   "Set the process mark on ARTICLE and update the summary line."
11889   (push article gnus-newsgroup-saved)
11890   (when (gnus-summary-goto-subject article)
11891     (gnus-summary-update-secondary-mark article)))
11892
11893 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11894   "Mark N articles as read forwards.
11895 If N is negative, mark backwards instead.
11896 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11897 marked as unread.
11898 The difference between N and the actual number of articles marked is
11899 returned."
11900   (interactive "p")
11901   (gnus-set-global-variables)
11902   (let ((backward (< n 0))
11903         (gnus-summary-goto-unread
11904          (and gnus-summary-goto-unread
11905               (not (eq gnus-summary-goto-unread 'never))
11906               (not (memq mark (list gnus-unread-mark
11907                                     gnus-ticked-mark gnus-dormant-mark)))))
11908         (n (abs n))
11909         (mark (or mark gnus-del-mark)))
11910     (while (and (> n 0)
11911                 (gnus-summary-mark-article nil mark no-expire)
11912                 (zerop (gnus-summary-next-subject
11913                         (if backward -1 1)
11914                         (and gnus-summary-goto-unread
11915                              (not (eq gnus-summary-goto-unread 'never)))
11916                         t)))
11917       (setq n (1- n)))
11918     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11919     (gnus-summary-recenter)
11920     (gnus-summary-position-point)
11921     (gnus-set-mode-line 'summary)
11922     n))
11923
11924 (defun gnus-summary-mark-article-as-read (mark)
11925   "Mark the current article quickly as read with MARK."
11926   (let ((article (gnus-summary-article-number)))
11927     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11928     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11929     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11930     (setq gnus-newsgroup-reads
11931           (cons (cons article mark) gnus-newsgroup-reads))
11932     ;; Possibly remove from cache, if that is used.
11933     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11934     ;; Allow the backend to change the mark.
11935     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11936     ;; Check for auto-expiry.
11937     (when (and gnus-newsgroup-auto-expire
11938                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11939                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11940                    (= mark gnus-ancient-mark)
11941                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11942       (setq mark gnus-expirable-mark)
11943       (push article gnus-newsgroup-expirable))
11944     ;; Set the mark in the buffer.
11945     (gnus-summary-update-mark mark 'unread)
11946     t))
11947
11948 (defun gnus-summary-mark-article-as-unread (mark)
11949   "Mark the current article quickly as unread with MARK."
11950   (let ((article (gnus-summary-article-number)))
11951     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11952     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11953     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11954     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11955     (cond ((= mark gnus-ticked-mark)
11956            (push article gnus-newsgroup-marked))
11957           ((= mark gnus-dormant-mark)
11958            (push article gnus-newsgroup-dormant))
11959           (t
11960            (push article gnus-newsgroup-unreads)))
11961     (setq gnus-newsgroup-reads
11962           (delq (assq article gnus-newsgroup-reads)
11963                 gnus-newsgroup-reads))
11964
11965     ;; See whether the article is to be put in the cache.
11966     (and gnus-use-cache
11967          (vectorp (gnus-summary-article-header article))
11968          (save-excursion
11969            (gnus-cache-possibly-enter-article
11970             gnus-newsgroup-name article
11971             (gnus-summary-article-header article)
11972             (= mark gnus-ticked-mark)
11973             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11974
11975     ;; Fix the mark.
11976     (gnus-summary-update-mark mark 'unread)
11977     t))
11978
11979 (defun gnus-summary-mark-article (&optional article mark no-expire)
11980   "Mark ARTICLE with MARK.  MARK can be any character.
11981 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11982 `??' (dormant) and `?E' (expirable).
11983 If MARK is nil, then the default character `?D' is used.
11984 If ARTICLE is nil, then the article on the current line will be
11985 marked."
11986   ;; The mark might be a string.
11987   (and (stringp mark)
11988        (setq mark (aref mark 0)))
11989   ;; If no mark is given, then we check auto-expiring.
11990   (and (not no-expire)
11991        gnus-newsgroup-auto-expire
11992        (or (not mark)
11993            (and (numberp mark)
11994                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11995                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11996                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11997        (setq mark gnus-expirable-mark))
11998   (let* ((mark (or mark gnus-del-mark))
11999          (article (or article (gnus-summary-article-number))))
12000     (or article (error "No article on current line"))
12001     (if (or (= mark gnus-unread-mark)
12002             (= mark gnus-ticked-mark)
12003             (= mark gnus-dormant-mark))
12004         (gnus-mark-article-as-unread article mark)
12005       (gnus-mark-article-as-read article mark))
12006
12007     ;; See whether the article is to be put in the cache.
12008     (and gnus-use-cache
12009          (not (= mark gnus-canceled-mark))
12010          (vectorp (gnus-summary-article-header article))
12011          (save-excursion
12012            (gnus-cache-possibly-enter-article
12013             gnus-newsgroup-name article
12014             (gnus-summary-article-header article)
12015             (= mark gnus-ticked-mark)
12016             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12017
12018     (if (gnus-summary-goto-subject article nil t)
12019         (let ((buffer-read-only nil))
12020           (gnus-summary-show-thread)
12021           ;; Fix the mark.
12022           (gnus-summary-update-mark mark 'unread)
12023           t))))
12024
12025 (defun gnus-summary-update-secondary-mark (article)
12026   "Update the secondary (read, process, cache) mark."
12027   (gnus-summary-update-mark
12028    (cond ((memq article gnus-newsgroup-processable)
12029           gnus-process-mark)
12030          ((memq article gnus-newsgroup-cached)
12031           gnus-cached-mark)
12032          ((memq article gnus-newsgroup-replied)
12033           gnus-replied-mark)
12034          ((memq article gnus-newsgroup-saved)
12035           gnus-saved-mark)
12036          (t gnus-unread-mark))
12037    'replied)
12038   (when (gnus-visual-p 'summary-highlight 'highlight)
12039     (run-hooks 'gnus-summary-update-hook))
12040   t)
12041
12042 (defun gnus-summary-update-mark (mark type)
12043   (beginning-of-line)
12044   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12045         (buffer-read-only nil))
12046     (when (and forward
12047                (<= (+ forward (point)) (point-max)))
12048       ;; Go to the right position on the line.
12049       (goto-char (+ forward (point)))
12050       ;; Replace the old mark with the new mark.
12051       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12052       ;; Optionally update the marks by some user rule.
12053       (when (eq type 'unread)
12054         (gnus-data-set-mark
12055          (gnus-data-find (gnus-summary-article-number)) mark)
12056         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12057
12058 (defun gnus-mark-article-as-read (article &optional mark)
12059   "Enter ARTICLE in the pertinent lists and remove it from others."
12060   ;; Make the article expirable.
12061   (let ((mark (or mark gnus-del-mark)))
12062     (if (= mark gnus-expirable-mark)
12063         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12064       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12065     ;; Remove from unread and marked lists.
12066     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12067     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12068     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12069     (push (cons article mark) gnus-newsgroup-reads)
12070     ;; Possibly remove from cache, if that is used.
12071     (when gnus-use-cache
12072       (gnus-cache-enter-remove-article article))))
12073
12074 (defun gnus-mark-article-as-unread (article &optional mark)
12075   "Enter ARTICLE in the pertinent lists and remove it from others."
12076   (let ((mark (or mark gnus-ticked-mark)))
12077     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12078     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12079     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12080     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12081     (cond ((= mark gnus-ticked-mark)
12082            (push article gnus-newsgroup-marked))
12083           ((= mark gnus-dormant-mark)
12084            (push article gnus-newsgroup-dormant))
12085           (t
12086            (push article gnus-newsgroup-unreads)))
12087     (setq gnus-newsgroup-reads
12088           (delq (assq article gnus-newsgroup-reads)
12089                 gnus-newsgroup-reads))))
12090
12091 (defalias 'gnus-summary-mark-as-unread-forward
12092   'gnus-summary-tick-article-forward)
12093 (make-obsolete 'gnus-summary-mark-as-unread-forward
12094                'gnus-summary-tick-article-forward)
12095 (defun gnus-summary-tick-article-forward (n)
12096   "Tick N articles forwards.
12097 If N is negative, tick backwards instead.
12098 The difference between N and the number of articles ticked is returned."
12099   (interactive "p")
12100   (gnus-summary-mark-forward n gnus-ticked-mark))
12101
12102 (defalias 'gnus-summary-mark-as-unread-backward
12103   'gnus-summary-tick-article-backward)
12104 (make-obsolete 'gnus-summary-mark-as-unread-backward
12105                'gnus-summary-tick-article-backward)
12106 (defun gnus-summary-tick-article-backward (n)
12107   "Tick N articles backwards.
12108 The difference between N and the number of articles ticked is returned."
12109   (interactive "p")
12110   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12111
12112 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12113 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12114 (defun gnus-summary-tick-article (&optional article clear-mark)
12115   "Mark current article as unread.
12116 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12117 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12118   (interactive)
12119   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12120                                        gnus-ticked-mark)))
12121
12122 (defun gnus-summary-mark-as-read-forward (n)
12123   "Mark N articles as read forwards.
12124 If N is negative, mark backwards instead.
12125 The difference between N and the actual number of articles marked is
12126 returned."
12127   (interactive "p")
12128   (gnus-summary-mark-forward n gnus-del-mark t))
12129
12130 (defun gnus-summary-mark-as-read-backward (n)
12131   "Mark the N articles as read backwards.
12132 The difference between N and the actual number of articles marked is
12133 returned."
12134   (interactive "p")
12135   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12136
12137 (defun gnus-summary-mark-as-read (&optional article mark)
12138   "Mark current article as read.
12139 ARTICLE specifies the article to be marked as read.
12140 MARK specifies a string to be inserted at the beginning of the line."
12141   (gnus-summary-mark-article article mark))
12142
12143 (defun gnus-summary-clear-mark-forward (n)
12144   "Clear marks from N articles forward.
12145 If N is negative, clear backward instead.
12146 The difference between N and the number of marks cleared is returned."
12147   (interactive "p")
12148   (gnus-summary-mark-forward n gnus-unread-mark))
12149
12150 (defun gnus-summary-clear-mark-backward (n)
12151   "Clear marks from N articles backward.
12152 The difference between N and the number of marks cleared is returned."
12153   (interactive "p")
12154   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12155
12156 (defun gnus-summary-mark-unread-as-read ()
12157   "Intended to be used by `gnus-summary-mark-article-hook'."
12158   (when (memq gnus-current-article gnus-newsgroup-unreads)
12159     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12160
12161 (defun gnus-summary-mark-read-and-unread-as-read ()
12162   "Intended to be used by `gnus-summary-mark-article-hook'."
12163   (let ((mark (gnus-summary-article-mark)))
12164     (when (or (gnus-unread-mark-p mark)
12165               (gnus-read-mark-p mark))
12166       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12167
12168 (defun gnus-summary-mark-region-as-read (point mark all)
12169   "Mark all unread articles between point and mark as read.
12170 If given a prefix, mark all articles between point and mark as read,
12171 even ticked and dormant ones."
12172   (interactive "r\nP")
12173   (save-excursion
12174     (let (article)
12175       (goto-char point)
12176       (beginning-of-line)
12177       (while (and
12178               (< (point) mark)
12179               (progn
12180                 (when (or all
12181                           (memq (setq article (gnus-summary-article-number))
12182                                 gnus-newsgroup-unreads))
12183                   (gnus-summary-mark-article article gnus-del-mark))
12184                 t)
12185               (gnus-summary-find-next))))))
12186
12187 (defun gnus-summary-mark-below (score mark)
12188   "Mark articles with score less than SCORE with MARK."
12189   (interactive "P\ncMark: ")
12190   (gnus-set-global-variables)
12191   (setq score (if score
12192                   (prefix-numeric-value score)
12193                 (or gnus-summary-default-score 0)))
12194   (save-excursion
12195     (set-buffer gnus-summary-buffer)
12196     (goto-char (point-min))
12197     (while 
12198         (progn
12199           (and (< (gnus-summary-article-score) score)
12200                (gnus-summary-mark-article nil mark))
12201           (gnus-summary-find-next)))))
12202
12203 (defun gnus-summary-kill-below (&optional score)
12204   "Mark articles with score below SCORE as read."
12205   (interactive "P")
12206   (gnus-set-global-variables)
12207   (gnus-summary-mark-below score gnus-killed-mark))
12208
12209 (defun gnus-summary-clear-above (&optional score)
12210   "Clear all marks from articles with score above SCORE."
12211   (interactive "P")
12212   (gnus-set-global-variables)
12213   (gnus-summary-mark-above score gnus-unread-mark))
12214
12215 (defun gnus-summary-tick-above (&optional score)
12216   "Tick all articles with score above SCORE."
12217   (interactive "P")
12218   (gnus-set-global-variables)
12219   (gnus-summary-mark-above score gnus-ticked-mark))
12220
12221 (defun gnus-summary-mark-above (score mark)
12222   "Mark articles with score over SCORE with MARK."
12223   (interactive "P\ncMark: ")
12224   (gnus-set-global-variables)
12225   (setq score (if score
12226                   (prefix-numeric-value score)
12227                 (or gnus-summary-default-score 0)))
12228   (save-excursion
12229     (set-buffer gnus-summary-buffer)
12230     (goto-char (point-min))
12231     (while (and (progn
12232                   (if (> (gnus-summary-article-score) score)
12233                       (gnus-summary-mark-article nil mark))
12234                   t)
12235                 (gnus-summary-find-next)))))
12236
12237 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12238 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12239 (defun gnus-summary-limit-include-expunged ()
12240   "Display all the hidden articles that were expunged for low scores."
12241   (interactive)
12242   (gnus-set-global-variables)
12243   (let ((buffer-read-only nil))
12244     (let ((scored gnus-newsgroup-scored)
12245           headers h)
12246       (while scored
12247         (or (gnus-summary-goto-subject (caar scored))
12248             (and (setq h (gnus-summary-article-header (caar scored)))
12249                  (< (cdar scored) gnus-summary-expunge-below)
12250                  (setq headers (cons h headers))))
12251         (setq scored (cdr scored)))
12252       (or headers (error "No expunged articles hidden."))
12253       (goto-char (point-min))
12254       (gnus-summary-prepare-unthreaded (nreverse headers)))
12255     (goto-char (point-min))
12256     (gnus-summary-position-point)))
12257
12258 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12259   "Mark all articles not marked as unread in this newsgroup as read.
12260 If prefix argument ALL is non-nil, all articles are marked as read.
12261 If QUIETLY is non-nil, no questions will be asked.
12262 If TO-HERE is non-nil, it should be a point in the buffer.  All
12263 articles before this point will be marked as read.
12264 The number of articles marked as read is returned."
12265   (interactive "P")
12266   (gnus-set-global-variables)
12267   (prog1
12268       (if (or quietly
12269               (not gnus-interactive-catchup) ;Without confirmation?
12270               gnus-expert-user
12271               (gnus-y-or-n-p
12272                (if all
12273                    "Mark absolutely all articles as read? "
12274                  "Mark all unread articles as read? ")))
12275           (if (and not-mark
12276                    (not gnus-newsgroup-adaptive)
12277                    (not gnus-newsgroup-auto-expire))
12278               (progn
12279                 (when all
12280                   (setq gnus-newsgroup-marked nil
12281                         gnus-newsgroup-dormant nil))
12282                 (setq gnus-newsgroup-unreads nil))
12283             ;; We actually mark all articles as canceled, which we
12284             ;; have to do when using auto-expiry or adaptive scoring.
12285             (gnus-summary-show-all-threads)
12286             (if (gnus-summary-first-subject (not all))
12287                 (while (and
12288                         (if to-here (< (point) to-here) t)
12289                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12290                         (gnus-summary-find-next (not all)))))
12291             (unless to-here
12292               (setq gnus-newsgroup-unreads nil))
12293             (gnus-set-mode-line 'summary)))
12294     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12295       (if (and (not to-here) (eq 'nnvirtual (car method)))
12296           (nnvirtual-catchup-group
12297            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12298     (gnus-summary-position-point)))
12299
12300 (defun gnus-summary-catchup-to-here (&optional all)
12301   "Mark all unticked articles before the current one as read.
12302 If ALL is non-nil, also mark ticked and dormant articles as read."
12303   (interactive "P")
12304   (gnus-set-global-variables)
12305   (save-excursion
12306     (let ((beg (point)))
12307       ;; We check that there are unread articles.
12308       (when (or all (gnus-summary-find-prev))
12309         (gnus-summary-catchup all t beg))))
12310   (gnus-summary-position-point))
12311
12312 (defun gnus-summary-catchup-all (&optional quietly)
12313   "Mark all articles in this newsgroup as read."
12314   (interactive "P")
12315   (gnus-set-global-variables)
12316   (gnus-summary-catchup t quietly))
12317
12318 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12319   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12320 If prefix argument ALL is non-nil, all articles are marked as read."
12321   (interactive "P")
12322   (gnus-set-global-variables)
12323   (gnus-summary-catchup all quietly nil 'fast)
12324   ;; Select next newsgroup or exit.
12325   (if (eq gnus-auto-select-next 'quietly)
12326       (gnus-summary-next-group nil)
12327     (gnus-summary-exit)))
12328
12329 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12330   "Mark all articles in this newsgroup as read, and then exit."
12331   (interactive "P")
12332   (gnus-set-global-variables)
12333   (gnus-summary-catchup-and-exit t quietly))
12334
12335 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12336 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12337   "Mark all articles in this group as read and select the next group.
12338 If given a prefix, mark all articles, unread as well as ticked, as
12339 read."
12340   (interactive "P")
12341   (gnus-set-global-variables)
12342   (save-excursion
12343     (gnus-summary-catchup all))
12344   (gnus-summary-next-article t nil nil t))
12345
12346 ;; Thread-based commands.
12347
12348 (defun gnus-summary-articles-in-thread (&optional article)
12349   "Return a list of all articles in the current thread.
12350 If ARTICLE is non-nil, return all articles in the thread that starts
12351 with that article."
12352   (let* ((article (or article (gnus-summary-article-number)))
12353          (data (gnus-data-find-list article))
12354          (top-level (gnus-data-level (car data)))
12355          (top-subject
12356           (cond ((null gnus-thread-operation-ignore-subject)
12357                  (gnus-simplify-subject-re
12358                   (mail-header-subject (gnus-data-header (car data)))))
12359                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12360                  (gnus-simplify-subject-fuzzy
12361                   (mail-header-subject (gnus-data-header (car data)))))
12362                 (t nil)))
12363          (end-point (save-excursion
12364                       (gnus-summary-go-to-next-thread) (point)))
12365          articles)
12366     (while (and data
12367                 (< (gnus-data-pos (car data)) end-point))
12368       (and (or (not top-subject)
12369                (string= top-subject
12370                         (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12371                             (gnus-simplify-subject-fuzzy
12372                              (mail-header-subject
12373                               (gnus-data-header (car data))))
12374                           (gnus-simplify-subject-re
12375                            (mail-header-subject
12376                             (gnus-data-header (car data)))))))
12377            (setq articles (cons (gnus-data-number (car data)) articles)))
12378       (unless (and (setq data (cdr data))
12379                    (> (gnus-data-level (car data)) top-level))
12380         (setq data nil)))
12381     ;; Return the list of articles.
12382     (nreverse articles)))
12383
12384 (defun gnus-summary-rethread-current ()
12385   "Rethread the thread the current article is part of."
12386   (interactive)
12387   (gnus-set-global-variables)
12388   (let* ((gnus-show-threads t)
12389          (article (gnus-summary-article-number))
12390          (id (mail-header-id (gnus-summary-article-header)))
12391          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12392     (unless id
12393       (error "No article on the current line"))
12394     (gnus-rebuild-thread id)
12395     (gnus-summary-goto-subject article)))
12396
12397 (defun gnus-summary-reparent-thread ()
12398   "Make current article child of the marked (or previous) article.
12399
12400 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12401 is non-nil or the Subject: of both articles are the same."
12402   (interactive)
12403   (or (not (gnus-group-read-only-p))
12404       (error "The current newsgroup does not support article editing."))
12405   (or (<= (length gnus-newsgroup-processable) 1)
12406       (error "No more than one article may be marked."))
12407   (save-window-excursion
12408     (let ((gnus-article-buffer " *reparent*")
12409           (current-article (gnus-summary-article-number))
12410           ; first grab the marked article, otherwise one line up.
12411           (parent-article (if (not (null gnus-newsgroup-processable))
12412                               (car gnus-newsgroup-processable)
12413                             (save-excursion
12414                               (if (eq (forward-line -1) 0)
12415                                   (gnus-summary-article-number)
12416                                 (error "Beginning of summary buffer."))))))
12417       (or (not (eq current-article parent-article))
12418           (error "An article may not be self-referential."))
12419       (let ((message-id (mail-header-id 
12420                          (gnus-summary-article-header parent-article))))
12421         (or (and message-id (not (equal message-id "")))
12422             (error "No message-id in desired parent."))
12423         (gnus-summary-select-article t t nil current-article)
12424         (set-buffer gnus-article-buffer)
12425         (setq buffer-read-only nil)
12426         (let ((buf (format "%s" (buffer-string))))
12427           (erase-buffer)
12428           (insert buf))
12429         (goto-char (point-min))
12430         (if (search-forward-regexp "^References: " nil t)
12431             (insert message-id " " )
12432           (insert "References: " message-id "\n"))
12433         (or (gnus-request-replace-article current-article
12434                                           (car gnus-article-current)
12435                                           gnus-article-buffer)
12436             (error "Couldn't replace article."))
12437         (set-buffer gnus-summary-buffer)
12438         (gnus-summary-unmark-all-processable)
12439         (gnus-summary-rethread-current)
12440         (gnus-message 3 "Article %d is now the child of article %d."
12441                       current-article parent-article)))))
12442
12443 (defun gnus-summary-toggle-threads (&optional arg)
12444   "Toggle showing conversation threads.
12445 If ARG is positive number, turn showing conversation threads on."
12446   (interactive "P")
12447   (gnus-set-global-variables)
12448   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12449     (setq gnus-show-threads
12450           (if (null arg) (not gnus-show-threads)
12451             (> (prefix-numeric-value arg) 0)))
12452     (gnus-summary-prepare)
12453     (gnus-summary-goto-subject current)
12454     (gnus-summary-position-point)))
12455
12456 (defun gnus-summary-show-all-threads ()
12457   "Show all threads."
12458   (interactive)
12459   (gnus-set-global-variables)
12460   (save-excursion
12461     (let ((buffer-read-only nil))
12462       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12463   (gnus-summary-position-point))
12464
12465 (defun gnus-summary-show-thread ()
12466   "Show thread subtrees.
12467 Returns nil if no thread was there to be shown."
12468   (interactive)
12469   (gnus-set-global-variables)
12470   (let ((buffer-read-only nil)
12471         (orig (point))
12472         ;; first goto end then to beg, to have point at beg after let
12473         (end (progn (end-of-line) (point)))
12474         (beg (progn (beginning-of-line) (point))))
12475     (prog1
12476         ;; Any hidden lines here?
12477         (search-forward "\r" end t)
12478       (subst-char-in-region beg end ?\^M ?\n t)
12479       (goto-char orig)
12480       (gnus-summary-position-point))))
12481
12482 (defun gnus-summary-hide-all-threads ()
12483   "Hide all thread subtrees."
12484   (interactive)
12485   (gnus-set-global-variables)
12486   (save-excursion
12487     (goto-char (point-min))
12488     (gnus-summary-hide-thread)
12489     (while (zerop (gnus-summary-next-thread 1 t))
12490       (gnus-summary-hide-thread)))
12491   (gnus-summary-position-point))
12492
12493 (defun gnus-summary-hide-thread ()
12494   "Hide thread subtrees.
12495 Returns nil if no threads were there to be hidden."
12496   (interactive)
12497   (gnus-set-global-variables)
12498   (let ((buffer-read-only nil)
12499         (start (point))
12500         (article (gnus-summary-article-number)))
12501     (goto-char start)
12502     ;; Go forward until either the buffer ends or the subthread
12503     ;; ends.
12504     (when (and (not (eobp))
12505                (or (zerop (gnus-summary-next-thread 1 t))
12506                    (goto-char (point-max))))
12507       (prog1
12508           (if (and (> (point) start)
12509                    (search-backward "\n" start t))
12510               (progn
12511                 (subst-char-in-region start (point) ?\n ?\^M)
12512                 (gnus-summary-goto-subject article))
12513             (goto-char start)
12514             nil)
12515         ;;(gnus-summary-position-point)
12516         ))))
12517
12518 (defun gnus-summary-go-to-next-thread (&optional previous)
12519   "Go to the same level (or less) next thread.
12520 If PREVIOUS is non-nil, go to previous thread instead.
12521 Return the article number moved to, or nil if moving was impossible."
12522   (let ((level (gnus-summary-thread-level))
12523         (way (if previous -1 1))
12524         (beg (point)))
12525     (forward-line way)
12526     (while (and (not (eobp))
12527                 (< level (gnus-summary-thread-level)))
12528       (forward-line way))
12529     (if (eobp)
12530         (progn
12531           (goto-char beg)
12532           nil)
12533       (setq beg (point))
12534       (prog1
12535           (gnus-summary-article-number)
12536         (goto-char beg)))))
12537
12538 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12539   "Go to the same level (or less) next thread.
12540 If PREVIOUS is non-nil, go to previous thread instead.
12541 Return the article number moved to, or nil if moving was impossible."
12542   (if (and (eq gnus-summary-make-false-root 'dummy)
12543            (gnus-summary-article-intangible-p))
12544       (let ((beg (point)))
12545         (while (and (zerop (forward-line 1))
12546                     (not (gnus-summary-article-intangible-p))
12547                     (not (zerop (save-excursion 
12548                                   (gnus-summary-thread-level))))))
12549         (if (eobp)
12550             (progn
12551               (goto-char beg)
12552               nil)
12553           (point)))
12554     (let* ((level (gnus-summary-thread-level))
12555            (article (gnus-summary-article-number))
12556            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12557            oart)
12558       (while data
12559         (if (<= (gnus-data-level (car data)) level)
12560             (setq oart (gnus-data-number (car data))
12561                   data nil)
12562           (setq data (cdr data))))
12563       (and oart
12564            (gnus-summary-goto-subject oart)))))
12565
12566 (defun gnus-summary-next-thread (n &optional silent)
12567   "Go to the same level next N'th thread.
12568 If N is negative, search backward instead.
12569 Returns the difference between N and the number of skips actually
12570 done.
12571
12572 If SILENT, don't output messages."
12573   (interactive "p")
12574   (gnus-set-global-variables)
12575   (let ((backward (< n 0))
12576         (n (abs n))
12577         old dum int)
12578     (while (and (> n 0)
12579                 (gnus-summary-go-to-next-thread backward))
12580       (decf n))
12581     (unless silent 
12582       (gnus-summary-position-point))
12583     (when (and (not silent) (/= 0 n))
12584       (gnus-message 7 "No more threads"))
12585     n))
12586
12587 (defun gnus-summary-prev-thread (n)
12588   "Go to the same level previous N'th thread.
12589 Returns the difference between N and the number of skips actually
12590 done."
12591   (interactive "p")
12592   (gnus-set-global-variables)
12593   (gnus-summary-next-thread (- n)))
12594
12595 (defun gnus-summary-go-down-thread ()
12596   "Go down one level in the current thread."
12597   (let ((children (gnus-summary-article-children)))
12598     (and children
12599          (gnus-summary-goto-subject (car children)))))
12600
12601 (defun gnus-summary-go-up-thread ()
12602   "Go up one level in the current thread."
12603   (let ((parent (gnus-summary-article-parent)))
12604     (and parent
12605          (gnus-summary-goto-subject parent))))
12606
12607 (defun gnus-summary-down-thread (n)
12608   "Go down thread N steps.
12609 If N is negative, go up instead.
12610 Returns the difference between N and how many steps down that were
12611 taken."
12612   (interactive "p")
12613   (gnus-set-global-variables)
12614   (let ((up (< n 0))
12615         (n (abs n)))
12616     (while (and (> n 0)
12617                 (if up (gnus-summary-go-up-thread)
12618                   (gnus-summary-go-down-thread)))
12619       (setq n (1- n)))
12620     (gnus-summary-position-point)
12621     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12622     n))
12623
12624 (defun gnus-summary-up-thread (n)
12625   "Go up thread N steps.
12626 If N is negative, go up instead.
12627 Returns the difference between N and how many steps down that were
12628 taken."
12629   (interactive "p")
12630   (gnus-set-global-variables)
12631   (gnus-summary-down-thread (- n)))
12632
12633 (defun gnus-summary-top-thread ()
12634   "Go to the top of the thread."
12635   (interactive)
12636   (gnus-set-global-variables)
12637   (while (gnus-summary-go-up-thread))
12638   (gnus-summary-article-number))
12639
12640 (defun gnus-summary-kill-thread (&optional unmark)
12641   "Mark articles under current thread as read.
12642 If the prefix argument is positive, remove any kinds of marks.
12643 If the prefix argument is negative, tick articles instead."
12644   (interactive "P")
12645   (gnus-set-global-variables)
12646   (if unmark
12647       (setq unmark (prefix-numeric-value unmark)))
12648   (let ((articles (gnus-summary-articles-in-thread)))
12649     (save-excursion
12650       ;; Expand the thread.
12651       (gnus-summary-show-thread)
12652       ;; Mark all the articles.
12653       (while articles
12654         (gnus-summary-goto-subject (car articles))
12655         (cond ((null unmark)
12656                (gnus-summary-mark-article-as-read gnus-killed-mark))
12657               ((> unmark 0)
12658                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12659               (t
12660                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12661         (setq articles (cdr articles))))
12662     ;; Hide killed subtrees.
12663     (and (null unmark)
12664          gnus-thread-hide-killed
12665          (gnus-summary-hide-thread))
12666     ;; If marked as read, go to next unread subject.
12667     (if (null unmark)
12668         ;; Go to next unread subject.
12669         (gnus-summary-next-subject 1 t)))
12670   (gnus-set-mode-line 'summary))
12671
12672 ;; Summary sorting commands
12673
12674 (defun gnus-summary-sort-by-number (&optional reverse)
12675   "Sort summary buffer by article number.
12676 Argument REVERSE means reverse order."
12677   (interactive "P")
12678   (gnus-summary-sort 'number reverse))
12679
12680 (defun gnus-summary-sort-by-author (&optional reverse)
12681   "Sort summary buffer by author name alphabetically.
12682 If case-fold-search is non-nil, case of letters is ignored.
12683 Argument REVERSE means reverse order."
12684   (interactive "P")
12685   (gnus-summary-sort 'author reverse))
12686
12687 (defun gnus-summary-sort-by-subject (&optional reverse)
12688   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12689 If case-fold-search is non-nil, case of letters is ignored.
12690 Argument REVERSE means reverse order."
12691   (interactive "P")
12692   (gnus-summary-sort 'subject reverse))
12693
12694 (defun gnus-summary-sort-by-date (&optional reverse)
12695   "Sort summary buffer by date.
12696 Argument REVERSE means reverse order."
12697   (interactive "P")
12698   (gnus-summary-sort 'date reverse))
12699
12700 (defun gnus-summary-sort-by-score (&optional reverse)
12701   "Sort summary buffer by score.
12702 Argument REVERSE means reverse order."
12703   (interactive "P")
12704   (gnus-summary-sort 'score reverse))
12705
12706 (defun gnus-summary-sort (predicate reverse)
12707   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12708   (gnus-set-global-variables)
12709   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12710          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12711          (gnus-thread-sort-functions
12712           (list
12713            (if (not reverse)
12714                thread
12715              `(lambda (t1 t2)
12716                 (,thread t2 t1)))))
12717          (gnus-article-sort-functions
12718           (list
12719            (if (not reverse)
12720                article
12721              `(lambda (t1 t2)
12722                 (,article t2 t1)))))
12723          (buffer-read-only)
12724          (gnus-summary-prepare-hook nil))
12725     ;; We do the sorting by regenerating the threads.
12726     (gnus-summary-prepare)
12727     ;; Hide subthreads if needed.
12728     (when (and gnus-show-threads gnus-thread-hide-subtree)
12729       (gnus-summary-hide-all-threads)))
12730   ;; If in async mode, we send some info to the backend.
12731   (when gnus-newsgroup-async
12732     (gnus-request-asynchronous
12733      gnus-newsgroup-name gnus-newsgroup-data)))
12734
12735 (defun gnus-sortable-date (date)
12736   "Make sortable string by string-lessp from DATE.
12737 Timezone package is used."
12738   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12739          (year (aref date 0))
12740          (month (aref date 1))
12741          (day (aref date 2)))
12742     (timezone-make-sortable-date
12743      year month day
12744      (timezone-make-time-string
12745       (aref date 3) (aref date 4) (aref date 5)))))
12746
12747 ;; Summary saving commands.
12748
12749 (defun gnus-summary-save-article (&optional n not-saved)
12750   "Save the current article using the default saver function.
12751 If N is a positive number, save the N next articles.
12752 If N is a negative number, save the N previous articles.
12753 If N is nil and any articles have been marked with the process mark,
12754 save those articles instead.
12755 The variable `gnus-default-article-saver' specifies the saver function."
12756   (interactive "P")
12757   (gnus-set-global-variables)
12758   (let ((articles (gnus-summary-work-articles n))
12759         file header article)
12760     (while articles
12761       (setq header (gnus-summary-article-header
12762                     (setq article (pop articles))))
12763       (if (not (vectorp header))
12764           ;; This is a pseudo-article.
12765           (if (assq 'name header)
12766               (gnus-copy-file (cdr (assq 'name header)))
12767             (gnus-message 1 "Article %d is unsaveable" article))
12768         ;; This is a real article.
12769         (save-window-excursion
12770           (gnus-summary-select-article t nil nil article))
12771         (unless gnus-save-all-headers
12772           ;; Remove headers accoring to `gnus-saved-headers'.
12773           (let ((gnus-visible-headers
12774                  (or gnus-saved-headers gnus-visible-headers)))
12775             (gnus-article-hide-headers nil t)))
12776         ;; Remove any X-Gnus lines.
12777         (save-excursion
12778           (set-buffer gnus-article-buffer)
12779           (save-restriction
12780             (let ((buffer-read-only nil))
12781               (nnheader-narrow-to-headers)
12782               (while (re-search-forward "^X-Gnus" nil t)
12783                 (gnus-delete-line)))))
12784         (save-window-excursion
12785           (if (not gnus-default-article-saver)
12786               (error "No default saver is defined.")
12787             (setq file (funcall
12788                         gnus-default-article-saver
12789                         (cond
12790                          ((not gnus-prompt-before-saving)
12791                           'default)
12792                          ((eq gnus-prompt-before-saving 'always)
12793                           nil)
12794                          (t file))))))
12795         (gnus-summary-remove-process-mark article)
12796         (unless not-saved
12797           (gnus-summary-set-saved-mark article))))
12798     (gnus-summary-position-point)
12799     n))
12800
12801 (defun gnus-summary-pipe-output (&optional arg)
12802   "Pipe the current article to a subprocess.
12803 If N is a positive number, pipe the N next articles.
12804 If N is a negative number, pipe the N previous articles.
12805 If N is nil and any articles have been marked with the process mark,
12806 pipe those articles instead."
12807   (interactive "P")
12808   (gnus-set-global-variables)
12809   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12810     (gnus-summary-save-article arg t))
12811   (gnus-configure-windows 'pipe))
12812
12813 (defun gnus-summary-save-article-mail (&optional arg)
12814   "Append the current article to an mail file.
12815 If N is a positive number, save the N next articles.
12816 If N is a negative number, save the N previous articles.
12817 If N is nil and any articles have been marked with the process mark,
12818 save those articles instead."
12819   (interactive "P")
12820   (gnus-set-global-variables)
12821   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12822     (gnus-summary-save-article arg)))
12823
12824 (defun gnus-summary-save-article-rmail (&optional arg)
12825   "Append the current article to an rmail file.
12826 If N is a positive number, save the N next articles.
12827 If N is a negative number, save the N previous articles.
12828 If N is nil and any articles have been marked with the process mark,
12829 save those articles instead."
12830   (interactive "P")
12831   (gnus-set-global-variables)
12832   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12833     (gnus-summary-save-article arg)))
12834
12835 (defun gnus-summary-save-article-file (&optional arg)
12836   "Append the current article to a file.
12837 If N is a positive number, save the N next articles.
12838 If N is a negative number, save the N previous articles.
12839 If N is nil and any articles have been marked with the process mark,
12840 save those articles instead."
12841   (interactive "P")
12842   (gnus-set-global-variables)
12843   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12844     (gnus-summary-save-article arg)))
12845
12846 (defun gnus-summary-save-article-body-file (&optional arg)
12847   "Append the current article body to a file.
12848 If N is a positive number, save the N next articles.
12849 If N is a negative number, save the N previous articles.
12850 If N is nil and any articles have been marked with the process mark,
12851 save those articles instead."
12852   (interactive "P")
12853   (gnus-set-global-variables)
12854   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12855     (gnus-summary-save-article arg)))
12856
12857 (defun gnus-get-split-value (methods)
12858   "Return a value based on the split METHODS."
12859   (let (split-name method result match)
12860     (when methods
12861       (save-excursion
12862         (set-buffer gnus-original-article-buffer)
12863         (save-restriction
12864           (nnheader-narrow-to-headers)
12865           (while methods
12866             (goto-char (point-min))
12867             (setq method (pop methods))
12868             (setq match (car method))
12869             (when (cond
12870                    ((stringp match)
12871                     ;; Regular expression.
12872                     (condition-case ()
12873                         (re-search-forward match nil t)
12874                       (error nil)))
12875                    ((gnus-functionp match)
12876                     ;; Function.
12877                     (save-restriction
12878                       (widen)
12879                       (setq result (funcall match gnus-newsgroup-name))))
12880                    ((consp match)
12881                     ;; Form.
12882                     (save-restriction
12883                       (widen)
12884                       (setq result (eval match)))))
12885               (setq split-name (append (cdr method) split-name))
12886               (cond ((stringp result)
12887                      (push result split-name))
12888                     ((consp result)
12889                      (setq split-name (append result split-name)))))))))
12890     split-name))
12891
12892 (defun gnus-read-move-group-name (prompt default articles prefix)
12893   "Read a group name."
12894   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12895          group-map
12896          (dum (mapatoms
12897                (lambda (g) 
12898                  (and (boundp g)
12899                       (symbol-name g)
12900                       (memq 'respool
12901                             (assoc (symbol-name
12902                                     (car (gnus-find-method-for-group
12903                                           (symbol-name g))))
12904                                    gnus-valid-select-methods))
12905                       (push (list (symbol-name g)) group-map)))
12906                gnus-active-hashtb))
12907          (prom
12908           (format "%s %s to:"
12909                   prompt
12910                   (if (> (length articles) 1)
12911                       (format "these %d articles" (length articles))
12912                     "this article")))
12913          (to-newsgroup
12914           (cond
12915            ((null split-name)
12916             (gnus-completing-read default prom
12917                                   group-map nil nil prefix
12918                                   'gnus-group-history))
12919            ((= 1 (length split-name))
12920             (gnus-completing-read (car split-name) prom group-map
12921                                   nil nil nil
12922                                   'gnus-group-history))
12923            (t
12924             (gnus-completing-read nil prom 
12925                                   (mapcar (lambda (el) (list el))
12926                                           (nreverse split-name))
12927                                   nil nil nil
12928                                   'gnus-group-history)))))
12929     (when to-newsgroup
12930       (if (or (string= to-newsgroup "")
12931               (string= to-newsgroup prefix))
12932           (setq to-newsgroup (or default "")))
12933       (or (gnus-active to-newsgroup)
12934           (gnus-activate-group to-newsgroup)
12935           (error "No such group: %s" to-newsgroup)))
12936     to-newsgroup))
12937
12938 (defun gnus-read-save-file-name (prompt default-name)
12939   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12940          (file
12941           ;; Let the split methods have their say.
12942           (cond
12943            ;; No split name was found.
12944            ((null split-name)
12945             (read-file-name
12946              (concat prompt " (default "
12947                      (file-name-nondirectory default-name) ") ")
12948              (file-name-directory default-name)
12949              default-name))
12950            ;; A single split name was found
12951            ((= 1 (length split-name))
12952             (read-file-name
12953              (concat prompt " (default " (car split-name) ") ")
12954              gnus-article-save-directory
12955              (concat gnus-article-save-directory (car split-name))))
12956            ;; A list of splits was found.
12957            (t
12958             (setq split-name (nreverse split-name))
12959             (let (result)
12960               (let ((file-name-history (nconc split-name file-name-history)))
12961                 (setq result
12962                       (read-file-name
12963                        (concat prompt " (`M-p' for defaults) ")
12964                        gnus-article-save-directory
12965                        (car split-name))))
12966               (car (push result file-name-history)))))))
12967     ;; If we have read a directory, we append the default file name.
12968     (when (file-directory-p file)
12969       (setq file (concat (file-name-as-directory file)
12970                          (file-name-nondirectory default-name))))
12971     ;; Possibly translate some charaters.
12972     (nnheader-translate-file-chars file)))
12973
12974 (defun gnus-article-archive-name (group)
12975   "Return the first instance of an \"Archive-name\" in the current buffer."
12976   (let ((case-fold-search t))
12977     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12978       (match-string 1))))
12979
12980 (defun gnus-summary-save-in-rmail (&optional filename)
12981   "Append this article to Rmail file.
12982 Optional argument FILENAME specifies file name.
12983 Directory to save to is default to `gnus-article-save-directory' which
12984 is initialized from the SAVEDIR environment variable."
12985   (interactive)
12986   (gnus-set-global-variables)
12987   (let ((default-name
12988           (funcall gnus-rmail-save-name gnus-newsgroup-name
12989                    gnus-current-headers gnus-newsgroup-last-rmail)))
12990     (setq filename
12991           (cond ((eq filename 'default)
12992                  default-name)
12993                 (filename filename)
12994                 (t (gnus-read-save-file-name
12995                     "Save in rmail file:" default-name))))
12996     (gnus-make-directory (file-name-directory filename))
12997     (gnus-eval-in-buffer-window
12998      gnus-original-article-buffer
12999      (save-excursion
13000        (save-restriction
13001          (widen)
13002          (gnus-output-to-rmail filename))))
13003     ;; Remember the directory name to save articles
13004     (setq gnus-newsgroup-last-rmail filename)))
13005
13006 (defun gnus-summary-save-in-mail (&optional filename)
13007   "Append this article to Unix mail file.
13008 Optional argument FILENAME specifies file name.
13009 Directory to save to is default to `gnus-article-save-directory' which
13010 is initialized from the SAVEDIR environment variable."
13011   (interactive)
13012   (gnus-set-global-variables)
13013   (let ((default-name
13014           (funcall gnus-mail-save-name gnus-newsgroup-name
13015                    gnus-current-headers gnus-newsgroup-last-mail)))
13016     (setq filename
13017           (cond ((eq filename 'default)
13018                  default-name)
13019                 (filename filename)
13020                 (t (gnus-read-save-file-name
13021                     "Save in Unix mail file:" default-name))))
13022     (setq filename
13023           (expand-file-name filename
13024                             (and default-name
13025                                  (file-name-directory default-name))))
13026     (gnus-make-directory (file-name-directory filename))
13027     (gnus-eval-in-buffer-window
13028      gnus-original-article-buffer
13029      (save-excursion
13030        (save-restriction
13031          (widen)
13032          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13033              (gnus-output-to-rmail filename)
13034            (let ((mail-use-rfc822 t))
13035              (rmail-output filename 1 t t))))))
13036     ;; Remember the directory name to save articles.
13037     (setq gnus-newsgroup-last-mail filename)))
13038
13039 (defun gnus-summary-save-in-file (&optional filename)
13040   "Append this article to file.
13041 Optional argument FILENAME specifies file name.
13042 Directory to save to is default to `gnus-article-save-directory' which
13043 is initialized from the SAVEDIR environment variable."
13044   (interactive)
13045   (gnus-set-global-variables)
13046   (let ((default-name
13047           (funcall gnus-file-save-name gnus-newsgroup-name
13048                    gnus-current-headers gnus-newsgroup-last-file)))
13049     (setq filename
13050           (cond ((eq filename 'default)
13051                  default-name)
13052                 (filename filename)
13053                 (t (gnus-read-save-file-name
13054                     "Save in file:" default-name))))
13055     (gnus-make-directory (file-name-directory filename))
13056     (gnus-eval-in-buffer-window
13057      gnus-original-article-buffer
13058      (save-excursion
13059        (save-restriction
13060          (widen)
13061          (gnus-output-to-file filename))))
13062     ;; Remember the directory name to save articles.
13063     (setq gnus-newsgroup-last-file filename)))
13064
13065 (defun gnus-summary-save-body-in-file (&optional filename)
13066   "Append this article body to a file.
13067 Optional argument FILENAME specifies file name.
13068 The directory to save in defaults to `gnus-article-save-directory' which
13069 is initialized from the SAVEDIR environment variable."
13070   (interactive)
13071   (gnus-set-global-variables)
13072   (let ((default-name
13073           (funcall gnus-file-save-name gnus-newsgroup-name
13074                    gnus-current-headers gnus-newsgroup-last-file)))
13075     (setq filename
13076           (cond ((eq filename 'default)
13077                  default-name)
13078                 (filename filename)
13079                 (t (gnus-read-save-file-name
13080                     "Save body in file:" default-name))))
13081     (gnus-make-directory (file-name-directory filename))
13082     (gnus-eval-in-buffer-window
13083      gnus-article-buffer
13084      (save-excursion
13085        (save-restriction
13086          (widen)
13087          (goto-char (point-min))
13088          (and (search-forward "\n\n" nil t)
13089               (narrow-to-region (point) (point-max)))
13090          (gnus-output-to-file filename))))
13091     ;; Remember the directory name to save articles.
13092     (setq gnus-newsgroup-last-file filename)))
13093
13094 (defun gnus-summary-save-in-pipe (&optional command)
13095   "Pipe this article to subprocess."
13096   (interactive)
13097   (gnus-set-global-variables)
13098   (setq command
13099         (cond ((eq command 'default)
13100                gnus-last-shell-command)
13101               (command command)
13102               (t (read-string "Shell command on article: "
13103                               gnus-last-shell-command))))
13104   (if (string-equal command "")
13105       (setq command gnus-last-shell-command))
13106   (gnus-eval-in-buffer-window
13107    gnus-article-buffer
13108    (save-restriction
13109      (widen)
13110      (shell-command-on-region (point-min) (point-max) command nil)))
13111   (setq gnus-last-shell-command command))
13112
13113 ;; Summary extract commands
13114
13115 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13116   (let ((buffer-read-only nil)
13117         (article (gnus-summary-article-number))
13118         after-article b e)
13119     (or (gnus-summary-goto-subject article)
13120         (error (format "No such article: %d" article)))
13121     (gnus-summary-position-point)
13122     ;; If all commands are to be bunched up on one line, we collect
13123     ;; them here.
13124     (if gnus-view-pseudos-separately
13125         ()
13126       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13127             files action)
13128         (while ps
13129           (setq action (cdr (assq 'action (car ps))))
13130           (setq files (list (cdr (assq 'name (car ps)))))
13131           (while (and ps (cdr ps)
13132                       (string= (or action "1")
13133                                (or (cdr (assq 'action (cadr ps))) "2")))
13134             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13135             (setcdr ps (cddr ps)))
13136           (if (not files)
13137               ()
13138             (if (not (string-match "%s" action))
13139                 (setq files (cons " " files)))
13140             (setq files (cons " " files))
13141             (and (assq 'execute (car ps))
13142                  (setcdr (assq 'execute (car ps))
13143                          (funcall (if (string-match "%s" action)
13144                                       'format 'concat)
13145                                   action
13146                                   (mapconcat (lambda (f) f) files " ")))))
13147           (setq ps (cdr ps)))))
13148     (if (and gnus-view-pseudos (not not-view))
13149         (while pslist
13150           (and (assq 'execute (car pslist))
13151                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13152                                      (eq gnus-view-pseudos 'not-confirm)))
13153           (setq pslist (cdr pslist)))
13154       (save-excursion
13155         (while pslist
13156           (setq after-article (or (cdr (assq 'article (car pslist)))
13157                                   (gnus-summary-article-number)))
13158           (gnus-summary-goto-subject after-article)
13159           (forward-line 1)
13160           (setq b (point))
13161           (insert "    " (file-name-nondirectory
13162                                 (cdr (assq 'name (car pslist))))
13163                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13164           (setq e (point))
13165           (forward-line -1)             ; back to `b'
13166           (add-text-properties
13167            b e (list 'gnus-number gnus-reffed-article-number
13168                      gnus-mouse-face-prop gnus-mouse-face))
13169           (gnus-data-enter
13170            after-article gnus-reffed-article-number
13171            gnus-unread-mark b (car pslist) 0 (- e b))
13172           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13173           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13174           (setq pslist (cdr pslist)))))))
13175
13176 (defun gnus-pseudos< (p1 p2)
13177   (let ((c1 (cdr (assq 'action p1)))
13178         (c2 (cdr (assq 'action p2))))
13179     (and c1 c2 (string< c1 c2))))
13180
13181 (defun gnus-request-pseudo-article (props)
13182   (cond ((assq 'execute props)
13183          (gnus-execute-command (cdr (assq 'execute props)))))
13184   (let ((gnus-current-article (gnus-summary-article-number)))
13185     (run-hooks 'gnus-mark-article-hook)))
13186
13187 (defun gnus-execute-command (command &optional automatic)
13188   (save-excursion
13189     (gnus-article-setup-buffer)
13190     (set-buffer gnus-article-buffer)
13191     (let ((command (if automatic command (read-string "Command: " command)))
13192           (buffer-read-only nil))
13193       (erase-buffer)
13194       (insert "$ " command "\n\n")
13195       (if gnus-view-pseudo-asynchronously
13196           (start-process "gnus-execute" nil "sh" "-c" command)
13197         (call-process "sh" nil t nil "-c" command)))))
13198
13199 (defun gnus-copy-file (file &optional to)
13200   "Copy FILE to TO."
13201   (interactive
13202    (list (read-file-name "Copy file: " default-directory)
13203          (read-file-name "Copy file to: " default-directory)))
13204   (gnus-set-global-variables)
13205   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13206   (and (file-directory-p to)
13207        (setq to (concat (file-name-as-directory to)
13208                         (file-name-nondirectory file))))
13209   (copy-file file to))
13210
13211 ;; Summary kill commands.
13212
13213 (defun gnus-summary-edit-global-kill (article)
13214   "Edit the \"global\" kill file."
13215   (interactive (list (gnus-summary-article-number)))
13216   (gnus-set-global-variables)
13217   (gnus-group-edit-global-kill article))
13218
13219 (defun gnus-summary-edit-local-kill ()
13220   "Edit a local kill file applied to the current newsgroup."
13221   (interactive)
13222   (gnus-set-global-variables)
13223   (setq gnus-current-headers (gnus-summary-article-header))
13224   (gnus-set-global-variables)
13225   (gnus-group-edit-local-kill
13226    (gnus-summary-article-number) gnus-newsgroup-name))
13227
13228 \f
13229 ;;;
13230 ;;; Gnus article mode
13231 ;;;
13232
13233 (put 'gnus-article-mode 'mode-class 'special)
13234
13235 (if gnus-article-mode-map
13236     nil
13237   (setq gnus-article-mode-map (make-keymap))
13238   (suppress-keymap gnus-article-mode-map)
13239
13240   (gnus-define-keys gnus-article-mode-map
13241     " " gnus-article-goto-next-page
13242     "\177" gnus-article-goto-prev-page
13243     [delete] gnus-article-goto-prev-page
13244     "\C-c^" gnus-article-refer-article
13245     "h" gnus-article-show-summary
13246     "s" gnus-article-show-summary
13247     "\C-c\C-m" gnus-article-mail
13248     "?" gnus-article-describe-briefly
13249     gnus-mouse-2 gnus-article-push-button
13250     "\r" gnus-article-press-button
13251     "\t" gnus-article-next-button
13252     "\M-\t" gnus-article-prev-button
13253     "\C-c\C-b" gnus-bug)
13254
13255   (substitute-key-definition
13256    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13257
13258 (defun gnus-article-mode ()
13259   "Major mode for displaying an article.
13260
13261 All normal editing commands are switched off.
13262
13263 The following commands are available:
13264
13265 \\<gnus-article-mode-map>
13266 \\[gnus-article-next-page]\t Scroll the article one page forwards
13267 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13268 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13269 \\[gnus-article-show-summary]\t Display the summary buffer
13270 \\[gnus-article-mail]\t Send a reply to the address near point
13271 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13272 \\[gnus-info-find-node]\t Go to the Gnus info node"
13273   (interactive)
13274   (when (and menu-bar-mode
13275              (gnus-visual-p 'article-menu 'menu))
13276     (gnus-article-make-menu-bar))
13277   (kill-all-local-variables)
13278   (gnus-simplify-mode-line)
13279   (setq mode-name "Article")
13280   (setq major-mode 'gnus-article-mode)
13281   (make-local-variable 'minor-mode-alist)
13282   (or (assq 'gnus-show-mime minor-mode-alist)
13283       (setq minor-mode-alist
13284             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13285   (use-local-map gnus-article-mode-map)
13286   (make-local-variable 'page-delimiter)
13287   (setq page-delimiter gnus-page-delimiter)
13288   (buffer-disable-undo (current-buffer))
13289   (setq buffer-read-only t)             ;Disable modification
13290   (run-hooks 'gnus-article-mode-hook))
13291
13292 (defun gnus-article-setup-buffer ()
13293   "Initialize the article buffer."
13294   (let* ((name (if gnus-single-article-buffer "*Article*"
13295                  (concat "*Article " gnus-newsgroup-name "*")))
13296          (original
13297           (progn (string-match "\\*Article" name)
13298                  (concat " *Original Article"
13299                          (substring name (match-end 0))))))
13300     (setq gnus-article-buffer name)
13301     (setq gnus-original-article-buffer original)
13302     ;; This might be a variable local to the summary buffer.
13303     (unless gnus-single-article-buffer
13304       (save-excursion
13305         (set-buffer gnus-summary-buffer)
13306         (setq gnus-article-buffer name)
13307         (setq gnus-original-article-buffer original)
13308         (gnus-set-global-variables))
13309       (make-local-variable 'gnus-summary-buffer))
13310     ;; Init original article buffer.
13311     (save-excursion
13312       (set-buffer (get-buffer-create gnus-original-article-buffer))
13313       (buffer-disable-undo (current-buffer))
13314       (setq major-mode 'gnus-original-article-mode)
13315       (make-local-variable 'gnus-original-article))
13316     (if (get-buffer name)
13317         (save-excursion
13318           (set-buffer name)
13319           (buffer-disable-undo (current-buffer))
13320           (setq buffer-read-only t)
13321           (gnus-add-current-to-buffer-list)
13322           (or (eq major-mode 'gnus-article-mode)
13323               (gnus-article-mode))
13324           (current-buffer))
13325       (save-excursion
13326         (set-buffer (get-buffer-create name))
13327         (gnus-add-current-to-buffer-list)
13328         (gnus-article-mode)
13329         (current-buffer)))))
13330
13331 ;; Set article window start at LINE, where LINE is the number of lines
13332 ;; from the head of the article.
13333 (defun gnus-article-set-window-start (&optional line)
13334   (set-window-start
13335    (get-buffer-window gnus-article-buffer)
13336    (save-excursion
13337      (set-buffer gnus-article-buffer)
13338      (goto-char (point-min))
13339      (if (not line)
13340          (point-min)
13341        (gnus-message 6 "Moved to bookmark")
13342        (search-forward "\n\n" nil t)
13343        (forward-line line)
13344        (point)))))
13345
13346 (defun gnus-kill-all-overlays ()
13347   "Delete all overlays in the current buffer."
13348   (when (fboundp 'overlay-lists)
13349     (let* ((overlayss (overlay-lists))
13350            (buffer-read-only nil)
13351            (overlays (nconc (car overlayss) (cdr overlayss))))
13352       (while overlays
13353         (delete-overlay (pop overlays))))))
13354
13355 (defun gnus-request-article-this-buffer (article group)
13356   "Get an article and insert it into this buffer."
13357   (let (do-update-line)
13358     (prog1
13359         (save-excursion
13360           (erase-buffer)
13361           (gnus-kill-all-overlays)
13362           (setq group (or group gnus-newsgroup-name))
13363
13364           ;; Open server if it has closed.
13365           (gnus-check-server (gnus-find-method-for-group group))
13366
13367           ;; Using `gnus-request-article' directly will insert the article into
13368           ;; `nntp-server-buffer' - so we'll save some time by not having to
13369           ;; copy it from the server buffer into the article buffer.
13370
13371           ;; We only request an article by message-id when we do not have the
13372           ;; headers for it, so we'll have to get those.
13373           (when (stringp article)
13374             (let ((gnus-override-method gnus-refer-article-method))
13375               (gnus-read-header article)))
13376
13377           ;; If the article number is negative, that means that this article
13378           ;; doesn't belong in this newsgroup (possibly), so we find its
13379           ;; message-id and request it by id instead of number.
13380           (when (and (numberp article)
13381                      gnus-summary-buffer
13382                      (get-buffer gnus-summary-buffer)
13383                      (buffer-name (get-buffer gnus-summary-buffer)))
13384             (save-excursion
13385               (set-buffer gnus-summary-buffer)
13386               (let ((header (gnus-summary-article-header article)))
13387                 (if (< article 0)
13388                     (cond 
13389                      ((memq article gnus-newsgroup-sparse)
13390                       ;; This is a sparse gap article.
13391                       (setq do-update-line article)
13392                       (setq article (mail-header-id header))
13393                       (let ((gnus-override-method gnus-refer-article-method))
13394                         (gnus-read-header article)))
13395                      ((vectorp header)
13396                       ;; It's a real article.
13397                       (setq article (mail-header-id header)))
13398                      (t
13399                       ;; It is an extracted pseudo-article.
13400                       (setq article 'pseudo)
13401                       (gnus-request-pseudo-article header))))
13402                 
13403                 (let ((method (gnus-find-method-for-group 
13404                                gnus-newsgroup-name)))
13405                   (if (not (eq (car method) 'nneething))
13406                       ()
13407                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13408                                        (mail-header-subject header))))
13409                       (if (file-directory-p dir)
13410                           (progn
13411                             (setq article 'nneething)
13412                             (gnus-group-enter-directory dir)))))))))
13413
13414           (cond
13415            ;; We first check `gnus-original-article-buffer'.
13416            ((and (get-buffer gnus-original-article-buffer)
13417                  (save-excursion
13418                    (set-buffer gnus-original-article-buffer)
13419                    (and (equal (car gnus-original-article) group)
13420                         (eq (cdr gnus-original-article) article))))
13421             (insert-buffer-substring gnus-original-article-buffer)
13422             'article)
13423            ;; Check the backlog.
13424            ((and gnus-keep-backlog
13425                  (gnus-backlog-request-article group article (current-buffer)))
13426             'article)
13427            ;; Check the cache.
13428            ((and gnus-use-cache
13429                  (numberp article)
13430                  (gnus-cache-request-article article group))
13431             'article)
13432            ;; Get the article and put into the article buffer.
13433            ((or (stringp article) (numberp article))
13434             (let ((gnus-override-method
13435                    (and (stringp article) gnus-refer-article-method))
13436                   (buffer-read-only nil))
13437               (erase-buffer)
13438               (gnus-kill-all-overlays)
13439               (if (gnus-request-article article group (current-buffer))
13440                   (progn
13441                     (and gnus-keep-backlog
13442                          (numberp article)
13443                          (gnus-backlog-enter-article
13444                           group article (current-buffer)))
13445                     'article))))
13446            ;; It was a pseudo.
13447            (t article)))
13448
13449       ;; Take the article from the original article buffer
13450       ;; and place it in the buffer it's supposed to be in.
13451       (when (and (get-buffer gnus-article-buffer)
13452                  (equal (buffer-name (current-buffer))
13453                         (buffer-name (get-buffer gnus-article-buffer))))
13454         (save-excursion
13455           (if (get-buffer gnus-original-article-buffer)
13456               (set-buffer (get-buffer gnus-original-article-buffer))
13457             (set-buffer (get-buffer-create gnus-original-article-buffer))
13458             (buffer-disable-undo (current-buffer))
13459             (setq major-mode 'gnus-original-article-mode)
13460             (setq buffer-read-only t)
13461             (gnus-add-current-to-buffer-list))
13462           (let (buffer-read-only)
13463             (erase-buffer)
13464             (insert-buffer-substring gnus-article-buffer))
13465           (setq gnus-original-article (cons group article))))
13466     
13467       ;; Update sparse articles.
13468       (when do-update-line
13469         (save-excursion
13470           (set-buffer gnus-summary-buffer)
13471           (gnus-summary-update-article do-update-line)
13472           (gnus-summary-goto-subject do-update-line)
13473           (set-window-point (get-buffer-window (current-buffer) t)
13474                             (point)))))))
13475
13476 (defun gnus-read-header (id &optional header)
13477   "Read the headers of article ID and enter them into the Gnus system."
13478   (let ((group gnus-newsgroup-name)
13479         (gnus-override-method 
13480          (and (gnus-news-group-p gnus-newsgroup-name)
13481               gnus-refer-article-method))       
13482         where)
13483     ;; First we check to see whether the header in question is already
13484     ;; fetched.
13485     (if (stringp id)
13486         ;; This is a Message-ID.
13487         (setq header (or header (gnus-id-to-header id)))
13488       ;; This is an article number.
13489       (setq header (or header (gnus-summary-article-header id))))
13490     (if (and header
13491              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13492         ;; We have found the header.
13493         header
13494       ;; We have to really fetch the header to this article.
13495       (when (setq where
13496                   (if (gnus-check-backend-function 'request-head group)
13497                       (gnus-request-head id group)
13498                     (gnus-request-article id group)))
13499         (save-excursion
13500           (set-buffer nntp-server-buffer)
13501           (and (search-forward "\n\n" nil t)
13502                (delete-region (1- (point)) (point-max)))
13503           (goto-char (point-max))
13504           (insert ".\n")
13505           (goto-char (point-min))
13506           (insert "211 ")
13507           (princ (cond
13508                   ((numberp id) id)
13509                   ((cdr where) (cdr where))
13510                   (header (mail-header-number header))
13511                   (t gnus-reffed-article-number))
13512                  (current-buffer))
13513           (insert " Article retrieved.\n"))
13514         ;(when (and header
13515         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13516         ;  (setcar (gnus-id-to-thread id) nil))
13517         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13518             ()                          ; Malformed head.
13519           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13520             (if (and (stringp id)
13521                      (not (string= (gnus-group-real-name group)
13522                                    (car where))))
13523                 ;; If we fetched by Message-ID and the article came
13524                 ;; from a different group, we fudge some bogus article
13525                 ;; numbers for this article.
13526                 (mail-header-set-number header gnus-reffed-article-number))
13527             (decf gnus-reffed-article-number)
13528             (push header gnus-newsgroup-headers)
13529             (setq gnus-current-headers header)
13530             (push (mail-header-number header) gnus-newsgroup-limit))
13531           header)))))
13532
13533 (defun gnus-article-prepare (article &optional all-headers header)
13534   "Prepare ARTICLE in article mode buffer.
13535 ARTICLE should either be an article number or a Message-ID.
13536 If ARTICLE is an id, HEADER should be the article headers.
13537 If ALL-HEADERS is non-nil, no headers are hidden."
13538   (save-excursion
13539     ;; Make sure we start in a summary buffer.
13540     (unless (eq major-mode 'gnus-summary-mode)
13541       (set-buffer gnus-summary-buffer))
13542     (setq gnus-summary-buffer (current-buffer))
13543     ;; Make sure the connection to the server is alive.
13544     (unless (gnus-server-opened
13545              (gnus-find-method-for-group gnus-newsgroup-name))
13546       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13547       (gnus-request-group gnus-newsgroup-name t))
13548     (let* ((article (if header (mail-header-number header) article))
13549            (summary-buffer (current-buffer))
13550            (internal-hook gnus-article-internal-prepare-hook)
13551            (group gnus-newsgroup-name)
13552            result)
13553       (save-excursion
13554         (gnus-article-setup-buffer)
13555         (set-buffer gnus-article-buffer)
13556         ;; Deactivate active regions.
13557         (when (and (boundp 'transient-mark-mode)
13558                    transient-mark-mode)
13559           (setq mark-active nil))
13560         (if (not (setq result (let ((buffer-read-only nil))
13561                                 (gnus-request-article-this-buffer
13562                                  article group))))
13563             ;; There is no such article.
13564             (save-excursion
13565               (when (and (numberp article)
13566                          (not (memq article gnus-newsgroup-sparse)))
13567                 (setq gnus-article-current
13568                       (cons gnus-newsgroup-name article))
13569                 (set-buffer gnus-summary-buffer)
13570                 (setq gnus-current-article article)
13571                 (gnus-summary-mark-article article gnus-canceled-mark))
13572               (unless (memq article gnus-newsgroup-sparse)
13573                 (gnus-error
13574                  1 "No such article (may have expired or been canceled)")))
13575           (if (or (eq result 'pseudo) (eq result 'nneething))
13576               (progn
13577                 (save-excursion
13578                   (set-buffer summary-buffer)
13579                   (setq gnus-last-article gnus-current-article
13580                         gnus-newsgroup-history (cons gnus-current-article
13581                                                      gnus-newsgroup-history)
13582                         gnus-current-article 0
13583                         gnus-current-headers nil
13584                         gnus-article-current nil)
13585                   (if (eq result 'nneething)
13586                       (gnus-configure-windows 'summary)
13587                     (gnus-configure-windows 'article))
13588                   (gnus-set-global-variables))
13589                 (gnus-set-mode-line 'article))
13590             ;; The result from the `request' was an actual article -
13591             ;; or at least some text that is now displayed in the
13592             ;; article buffer.
13593             (if (and (numberp article)
13594                      (not (eq article gnus-current-article)))
13595                 ;; Seems like a new article has been selected.
13596                 ;; `gnus-current-article' must be an article number.
13597                 (save-excursion
13598                   (set-buffer summary-buffer)
13599                   (setq gnus-last-article gnus-current-article
13600                         gnus-newsgroup-history (cons gnus-current-article
13601                                                      gnus-newsgroup-history)
13602                         gnus-current-article article
13603                         gnus-current-headers
13604                         (gnus-summary-article-header gnus-current-article)
13605                         gnus-article-current
13606                         (cons gnus-newsgroup-name gnus-current-article))
13607                   (unless (vectorp gnus-current-headers)
13608                     (setq gnus-current-headers nil))
13609                   (gnus-summary-show-thread)
13610                   (run-hooks 'gnus-mark-article-hook)
13611                   (gnus-set-mode-line 'summary)
13612                   (and (gnus-visual-p 'article-highlight 'highlight)
13613                        (run-hooks 'gnus-visual-mark-article-hook))
13614                   ;; Set the global newsgroup variables here.
13615                   ;; Suggested by Jim Sisolak
13616                   ;; <sisolak@trans4.neep.wisc.edu>.
13617                   (gnus-set-global-variables)
13618                   (setq gnus-have-all-headers
13619                         (or all-headers gnus-show-all-headers))
13620                   (and gnus-use-cache
13621                        (vectorp (gnus-summary-article-header article))
13622                        (gnus-cache-possibly-enter-article
13623                         group article
13624                         (gnus-summary-article-header article)
13625                         (memq article gnus-newsgroup-marked)
13626                         (memq article gnus-newsgroup-dormant)
13627                         (memq article gnus-newsgroup-unreads)))))
13628             ;; Hooks for getting information from the article.
13629             ;; This hook must be called before being narrowed.
13630             (let (buffer-read-only)
13631               (run-hooks 'internal-hook)
13632               (run-hooks 'gnus-article-prepare-hook)
13633               ;; Decode MIME message.
13634               (if gnus-show-mime
13635                   (if (or (not gnus-strict-mime)
13636                           (gnus-fetch-field "Mime-Version"))
13637                       (funcall gnus-show-mime-method)
13638                     (funcall gnus-decode-encoded-word-method)))
13639               ;; Perform the article display hooks.
13640               (run-hooks 'gnus-article-display-hook))
13641             ;; Do page break.
13642             (goto-char (point-min))
13643             (and gnus-break-pages (gnus-narrow-to-page))
13644             (gnus-set-mode-line 'article)
13645             (gnus-configure-windows 'article)
13646             (goto-char (point-min))
13647             t))))))
13648
13649 (defun gnus-article-show-all-headers ()
13650   "Show all article headers in article mode buffer."
13651   (save-excursion
13652     (gnus-article-setup-buffer)
13653     (set-buffer gnus-article-buffer)
13654     (let ((buffer-read-only nil))
13655       (gnus-unhide-text (point-min) (point-max)))))
13656
13657 (defun gnus-article-hide-headers-if-wanted ()
13658   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13659 Provided for backwards compatibility."
13660   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13661       gnus-inhibit-hiding
13662       (gnus-article-hide-headers)))
13663
13664 (defsubst gnus-article-header-rank ()
13665   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13666   (let ((list gnus-sorted-header-list)
13667         (i 0))
13668     (while list
13669       (when (looking-at (car list))
13670         (setq list nil))
13671       (setq list (cdr list))
13672       (incf i))
13673     i))
13674
13675 (defun gnus-article-hide-headers (&optional arg delete)
13676   "Toggle whether to hide unwanted headers and possibly sort them as well.
13677 If given a negative prefix, always show; if given a positive prefix,
13678 always hide."
13679   (interactive "P")
13680   (unless (gnus-article-check-hidden-text 'headers arg)
13681     ;; This function might be inhibited.
13682     (unless gnus-inhibit-hiding
13683       (save-excursion
13684         (set-buffer gnus-article-buffer)
13685         (save-restriction
13686           (let ((buffer-read-only nil)
13687                 (props (nconc (list 'gnus-type 'headers)
13688                               gnus-hidden-properties))
13689                 (max (1+ (length gnus-sorted-header-list)))
13690                 (ignored (when (not (stringp gnus-visible-headers))
13691                            (cond ((stringp gnus-ignored-headers)
13692                                   gnus-ignored-headers)
13693                                  ((listp gnus-ignored-headers)
13694                                   (mapconcat 'identity gnus-ignored-headers
13695                                              "\\|")))))
13696                 (visible
13697                  (cond ((stringp gnus-visible-headers)
13698                         gnus-visible-headers)
13699                        ((and gnus-visible-headers
13700                              (listp gnus-visible-headers))
13701                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13702                 want-list beg)
13703             ;; First we narrow to just the headers.
13704             (widen)
13705             (goto-char (point-min))
13706             ;; Hide any "From " lines at the beginning of (mail) articles.
13707             (while (looking-at "From ")
13708               (forward-line 1))
13709             (unless (bobp)
13710               (gnus-hide-text (point-min) (point) props))
13711             ;; Then treat the rest of the header lines.
13712             (narrow-to-region
13713              (point)
13714              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13715             ;; Then we use the two regular expressions
13716             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13717             ;; select which header lines is to remain visible in the
13718             ;; article buffer.
13719             (goto-char (point-min))
13720             (while (re-search-forward "^[^ \t]*:" nil t)
13721               (beginning-of-line)
13722               ;; We add the headers we want to keep to a list and delete
13723               ;; them from the buffer.
13724               (put-text-property 
13725                (point) (1+ (point)) 'message-rank
13726                (if (or (and visible (looking-at visible))
13727                        (and ignored
13728                             (not (looking-at ignored))))
13729                    (gnus-article-header-rank) 
13730                  (+ 2 max)))
13731               (forward-line 1))
13732             (message-sort-headers-1)
13733             (when (setq beg (text-property-any 
13734                              (point-min) (point-max) 'message-rank (+ 2 max)))
13735               ;; We make the unwanted headers invisible.
13736               (if delete
13737                   (delete-region beg (point-max))
13738                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13739                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13740
13741 (defun gnus-article-hide-boring-headers (&optional arg)
13742   "Toggle hiding of headers that aren't very interesting.
13743 If given a negative prefix, always show; if given a positive prefix,
13744 always hide."
13745   (interactive "P")
13746   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13747     (save-excursion
13748       (set-buffer gnus-article-buffer)
13749       (save-restriction
13750         (let ((buffer-read-only nil)
13751               (list gnus-boring-article-headers)
13752               (inhibit-point-motion-hooks t)
13753               elem)
13754           (nnheader-narrow-to-headers)
13755           (while list
13756             (setq elem (pop list))
13757             (goto-char (point-min))
13758             (cond
13759              ;; Hide empty headers.
13760              ((eq elem 'empty)
13761               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13762                 (forward-line -1)
13763                 (gnus-hide-text-type
13764                  (progn (beginning-of-line) (point))
13765                  (progn 
13766                    (end-of-line)
13767                    (if (re-search-forward "^[^ \t]" nil t)
13768                        (match-beginning 0)
13769                      (point-max)))
13770                  'boring-headers)))
13771              ;; Hide boring Newsgroups header.
13772              ((eq elem 'newsgroups)
13773               (when (equal (mail-fetch-field "newsgroups")
13774                            (gnus-group-real-name gnus-newsgroup-name))
13775                 (gnus-article-hide-header "newsgroups")))
13776              ((eq elem 'followup-to)
13777               (when (equal (mail-fetch-field "followup-to")
13778                            (mail-fetch-field "newsgroups"))
13779                 (gnus-article-hide-header "followup-to")))
13780              ((eq elem 'reply-to)
13781               (let ((from (mail-fetch-field "from"))
13782                     (reply-to (mail-fetch-field "reply-to")))
13783                 (when (and
13784                        from reply-to
13785                        (equal 
13786                         (nth 1 (funcall gnus-extract-address-components from))
13787                         (nth 1 (funcall gnus-extract-address-components
13788                                         reply-to))))
13789                   (gnus-article-hide-header "reply-to"))))
13790              ((eq elem 'date)
13791               (let ((date (mail-fetch-field "date")))
13792                 (when (and date
13793                            (< (gnus-days-between date (current-time-string))
13794                               4))
13795                   (gnus-article-hide-header "date")))))))))))
13796
13797 (defun gnus-article-hide-header (header)
13798   (save-excursion
13799     (goto-char (point-min))
13800     (when (re-search-forward (concat "^" header ":") nil t)
13801       (gnus-hide-text-type
13802        (progn (beginning-of-line) (point))
13803        (progn 
13804          (end-of-line)
13805          (if (re-search-forward "^[^ \t]" nil t)
13806              (match-beginning 0)
13807            (point-max)))
13808        'boring-headers))))
13809
13810 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13811 (defun gnus-article-treat-overstrike ()
13812   "Translate overstrikes into bold text."
13813   (interactive)
13814   (save-excursion
13815     (set-buffer gnus-article-buffer)
13816     (let ((buffer-read-only nil))
13817       (while (search-forward "\b" nil t)
13818         (let ((next (following-char))
13819               (previous (char-after (- (point) 2))))
13820           (cond ((eq next previous)
13821                  (put-text-property (- (point) 2) (point) 'invisible t)
13822                  (put-text-property (point) (1+ (point)) 'face 'bold))
13823                 ((eq next ?_)
13824                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13825                  (put-text-property
13826                   (- (point) 2) (1- (point)) 'face 'underline))
13827                 ((eq previous ?_)
13828                  (put-text-property (- (point) 2) (point) 'invisible t)
13829                  (put-text-property
13830                   (point) (1+ (point))  'face 'underline))))))))
13831
13832 (defun gnus-article-word-wrap ()
13833   "Format too long lines."
13834   (interactive)
13835   (save-excursion
13836     (set-buffer gnus-article-buffer)
13837     (let ((buffer-read-only nil))
13838       (widen)
13839       (goto-char (point-min))
13840       (search-forward "\n\n" nil t)
13841       (end-of-line 1)
13842       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13843             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13844             (adaptive-fill-mode t))
13845         (while (not (eobp))
13846           (and (>= (current-column) (min fill-column (window-width)))
13847                (/= (preceding-char) ?:)
13848                (fill-paragraph nil))
13849           (end-of-line 2))))))
13850
13851 (defun gnus-article-remove-cr ()
13852   "Remove carriage returns from an article."
13853   (interactive)
13854   (save-excursion
13855     (set-buffer gnus-article-buffer)
13856     (let ((buffer-read-only nil))
13857       (goto-char (point-min))
13858       (while (search-forward "\r" nil t)
13859         (replace-match "" t t)))))
13860
13861 (defun gnus-article-remove-trailing-blank-lines ()
13862   "Remove all trailing blank lines from the article."
13863   (interactive)
13864   (save-excursion
13865     (set-buffer gnus-article-buffer)
13866     (let ((buffer-read-only nil))
13867       (goto-char (point-max))
13868       (delete-region
13869        (point)
13870        (progn
13871          (while (looking-at "^[ \t]*$")
13872            (forward-line -1))
13873          (forward-line 1)
13874          (point))))))
13875
13876 (defun gnus-article-display-x-face (&optional force)
13877   "Look for an X-Face header and display it if present."
13878   (interactive (list 'force))
13879   (save-excursion
13880     (set-buffer gnus-article-buffer)
13881     ;; Delete the old process, if any.
13882     (when (process-status "gnus-x-face")
13883       (delete-process "gnus-x-face"))
13884     (let ((inhibit-point-motion-hooks t)
13885           (case-fold-search nil)
13886           from)
13887       (save-restriction
13888         (nnheader-narrow-to-headers)
13889         (setq from (mail-fetch-field "from"))
13890         (goto-char (point-min))
13891         (when (and gnus-article-x-face-command
13892                    (or force
13893                        ;; Check whether this face is censored.
13894                        (not gnus-article-x-face-too-ugly)
13895                        (and gnus-article-x-face-too-ugly from
13896                             (not (string-match gnus-article-x-face-too-ugly
13897                                                from))))
13898                    ;; Has to be present.
13899                    (re-search-forward "^X-Face: " nil t))
13900           ;; We now have the area of the buffer where the X-Face is stored.
13901           (let ((beg (point))
13902                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13903             ;; We display the face.
13904             (if (symbolp gnus-article-x-face-command)
13905                 ;; The command is a lisp function, so we call it.
13906                 (if (gnus-functionp gnus-article-x-face-command)
13907                     (funcall gnus-article-x-face-command beg end)
13908                   (error "%s is not a function" gnus-article-x-face-command))
13909               ;; The command is a string, so we interpret the command
13910               ;; as a, well, command, and fork it off.
13911               (let ((process-connection-type nil))
13912                 (process-kill-without-query
13913                  (start-process
13914                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13915                 (process-send-region "gnus-x-face" beg end)
13916                 (process-send-eof "gnus-x-face")))))))))
13917
13918 (defun gnus-headers-decode-quoted-printable ()
13919   "Hack to remove QP encoding from headers."
13920   (let ((case-fold-search t)
13921         (inhibit-point-motion-hooks t)
13922         string)
13923     (goto-char (point-min))
13924     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13925       (setq string (match-string 1))
13926       (narrow-to-region (match-beginning 0) (match-end 0))
13927       (delete-region (point-min) (point-max))
13928       (insert string)
13929       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13930       (subst-char-in-region (point-min) (point-max) ?_ ? )
13931       (widen)
13932       (goto-char (point-min)))))
13933
13934 (defun gnus-article-de-quoted-unreadable (&optional force)
13935   "Do a naive translation of a quoted-printable-encoded article.
13936 This is in no way, shape or form meant as a replacement for real MIME
13937 processing, but is simply a stop-gap measure until MIME support is
13938 written.
13939 If FORCE, decode the article whether it is marked as quoted-printable
13940 or not."
13941   (interactive (list 'force))
13942   (save-excursion
13943     (set-buffer gnus-article-buffer)
13944     (let ((case-fold-search t)
13945           (buffer-read-only nil)
13946           (type (gnus-fetch-field "content-transfer-encoding")))
13947       (when (or force
13948                 (and type (string-match "quoted-printable" (downcase type))))
13949         (gnus-headers-decode-quoted-printable)
13950         (goto-char (point-min))
13951         (search-forward "\n\n" nil 'move)
13952         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13953
13954 (defun gnus-mime-decode-quoted-printable (from to)
13955   "Decode Quoted-Printable in the region between FROM and TO."
13956   (interactive "r")
13957   (goto-char from)
13958   (while (search-forward "=" to t)
13959     (cond ((eq (following-char) ?\n)
13960            (delete-char -1)
13961            (delete-char 1))
13962           ((looking-at "[0-9A-F][0-9A-F]")
13963            (delete-char -1)
13964            (insert (hexl-hex-string-to-integer
13965                     (buffer-substring (point) (+ 2 (point)))))
13966            (delete-char 2))
13967           ((looking-at "=")
13968            (delete-char 1))
13969           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13970
13971 (defun gnus-article-hide-pgp (&optional arg)
13972   "Toggle hiding of any PGP headers and signatures in the current article.
13973 If given a negative prefix, always show; if given a positive prefix,
13974 always hide."
13975   (interactive "P")
13976   (unless (gnus-article-check-hidden-text 'pgp arg)
13977     (save-excursion
13978       (set-buffer gnus-article-buffer)
13979       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13980             buffer-read-only beg end)
13981         (widen)
13982         (goto-char (point-min))
13983         ;; Hide the "header".
13984         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13985              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13986         (setq beg (point))
13987         ;; Hide the actual signature.
13988         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13989              (setq end (1+ (match-beginning 0)))
13990              (gnus-hide-text
13991               end
13992               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13993                   (match-end 0)
13994                 ;; Perhaps we shouldn't hide to the end of the buffer
13995                 ;; if there is no end to the signature?
13996                 (point-max))
13997               props))
13998         ;; Hide "- " PGP quotation markers.
13999         (when (and beg end)
14000           (narrow-to-region beg end)
14001           (goto-char (point-min))
14002           (while (re-search-forward "^- " nil t)
14003             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14004           (widen))))))
14005
14006 (defun gnus-article-hide-signature (&optional arg)
14007   "Hide the signature in the current article.
14008 If given a negative prefix, always show; if given a positive prefix,
14009 always hide."
14010   (interactive "P")
14011   (unless (gnus-article-check-hidden-text 'signature arg)
14012     (save-excursion
14013       (set-buffer gnus-article-buffer)
14014       (save-restriction
14015         (let ((buffer-read-only nil))
14016           (when (gnus-narrow-to-signature)
14017             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14018
14019 (defun gnus-article-strip-leading-blank-lines ()
14020   "Remove all blank lines from the beginning of the article."
14021   (interactive)
14022   (save-excursion
14023     (set-buffer gnus-article-buffer)
14024     (let (buffer-read-only)
14025       (goto-char (point-min))
14026       (when (search-forward "\n\n" nil t)
14027         (while (looking-at "[ \t]$")
14028           (gnus-delete-line))))))
14029
14030 (defun gnus-narrow-to-signature ()
14031   "Narrow to the signature."
14032   (widen)
14033   (goto-char (point-max))
14034   (when (re-search-backward gnus-signature-separator nil t)
14035     (forward-line 1)
14036     (when (or (null gnus-signature-limit)
14037               (and (numberp gnus-signature-limit)
14038                    (< (- (point-max) (point)) gnus-signature-limit))
14039               (and (gnus-functionp gnus-signature-limit)
14040                    (funcall gnus-signature-limit))
14041               (and (stringp gnus-signature-limit)
14042                    (not (re-search-forward gnus-signature-limit nil t))))
14043       (narrow-to-region (point) (point-max))
14044       t)))
14045
14046 (defun gnus-article-check-hidden-text (type arg)
14047   "Return nil if hiding is necessary."
14048   (save-excursion
14049     (set-buffer gnus-article-buffer)
14050     (let ((hide (gnus-article-hidden-text-p type)))
14051       (cond ((or (and (null arg) (eq hide 'hidden))
14052                  (and arg (< 0 (prefix-numeric-value arg))))
14053              (gnus-article-show-hidden-text type))
14054             ((eq hide 'shown)
14055              (gnus-article-show-hidden-text type t))
14056             (t nil)))))
14057
14058 (defun gnus-article-hidden-text-p (type)
14059   "Say whether the current buffer contains hidden text of type TYPE."
14060   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14061     (when pos
14062       (if (get-text-property pos 'invisible)
14063           'hidden
14064         'shown))))
14065
14066 (defun gnus-article-hide (&optional arg force)
14067   "Hide all the gruft in the current article.
14068 This means that PGP stuff, signatures, cited text and (some)
14069 headers will be hidden.
14070 If given a prefix, show the hidden text instead."
14071   (interactive (list current-prefix-arg 'force))
14072   (gnus-article-hide-headers arg)
14073   (gnus-article-hide-pgp arg)
14074   (gnus-article-hide-citation-maybe arg force)
14075   (gnus-article-hide-signature arg))
14076
14077 (defun gnus-article-show-hidden-text (type &optional hide)
14078   "Show all hidden text of type TYPE.
14079 If HIDE, hide the text instead."
14080   (save-excursion
14081     (set-buffer gnus-article-buffer)
14082     (let ((buffer-read-only nil)
14083           (inhibit-point-motion-hooks t)
14084           (beg (point-min)))
14085       (while (gnus-goto-char (text-property-any
14086                               beg (point-max) 'gnus-type type))
14087         (setq beg (point))
14088         (forward-char)
14089         (if hide
14090             (gnus-hide-text beg (point) gnus-hidden-properties)
14091           (gnus-unhide-text beg (point)))
14092         (setq beg (point)))
14093       t)))
14094
14095 (defvar gnus-article-time-units
14096   `((year . ,(* 365.25 24 60 60))
14097     (week . ,(* 7 24 60 60))
14098     (day . ,(* 24 60 60))
14099     (hour . ,(* 60 60))
14100     (minute . 60)
14101     (second . 1))
14102   "Mapping from time units to seconds.")
14103
14104 (defun gnus-article-date-ut (&optional type highlight)
14105   "Convert DATE date to universal time in the current article.
14106 If TYPE is `local', convert to local time; if it is `lapsed', output
14107 how much time has lapsed since DATE."
14108   (interactive (list 'ut t))
14109   (let* ((header (or gnus-current-headers
14110                      (gnus-summary-article-header) ""))
14111          (date (and (vectorp header) (mail-header-date header)))
14112          (date-regexp "^Date: \\|^X-Sent: ")
14113          (now (current-time))
14114          (inhibit-point-motion-hooks t)
14115          bface eface)
14116     (when (and date (not (string= date "")))
14117       (save-excursion
14118         (set-buffer gnus-article-buffer)
14119         (save-restriction
14120           (nnheader-narrow-to-headers)
14121           (let ((buffer-read-only nil))
14122             ;; Delete any old Date headers.
14123             (if (re-search-forward date-regexp nil t)
14124                 (progn
14125                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14126                         eface (get-text-property (gnus-point-at-eol) 'face))
14127                   (message-remove-header date-regexp t)
14128                   (beginning-of-line))
14129               (goto-char (point-max)))
14130             (insert
14131              (cond
14132               ;; Convert to the local timezone.  We have to slap a
14133               ;; `condition-case' round the calls to the timezone
14134               ;; functions since they aren't particularly resistant to
14135               ;; buggy dates.
14136               ((eq type 'local)
14137                (concat "Date: " (condition-case ()
14138                                     (timezone-make-date-arpa-standard date)
14139                                   (error date))
14140                        "\n"))
14141               ;; Convert to Universal Time.
14142               ((eq type 'ut)
14143                (concat "Date: "
14144                        (condition-case ()
14145                            (timezone-make-date-arpa-standard date nil "UT")
14146                          (error date))
14147                        "\n"))
14148               ;; Get the original date from the article.
14149               ((eq type 'original)
14150                (concat "Date: " date "\n"))
14151               ;; Do an X-Sent lapsed format.
14152               ((eq type 'lapsed)
14153                ;; If the date is seriously mangled, the timezone
14154                ;; functions are liable to bug out, so we condition-case
14155                ;; the entire thing.
14156                (let* ((real-time
14157                        (condition-case ()
14158                            (gnus-time-minus
14159                             (gnus-encode-date
14160                              (timezone-make-date-arpa-standard
14161                               (current-time-string now)
14162                               (current-time-zone now) "UT"))
14163                             (gnus-encode-date
14164                              (timezone-make-date-arpa-standard
14165                               date nil "UT")))
14166                          (error '(0 0))))
14167                       (real-sec (+ (* (float (car real-time)) 65536)
14168                                    (cadr real-time)))
14169                       (sec (abs real-sec))
14170                       num prev)
14171                  (if (zerop sec)
14172                      "X-Sent: Now\n"
14173                    (concat
14174                     "X-Sent: "
14175                     ;; This is a bit convoluted, but basically we go
14176                     ;; through the time units for years, weeks, etc,
14177                     ;; and divide things to see whether that results
14178                     ;; in positive answers.
14179                     (mapconcat
14180                      (lambda (unit)
14181                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14182                            ;; The (remaining) seconds are too few to
14183                            ;; be divided into this time unit.
14184                            ""
14185                          ;; It's big enough, so we output it.
14186                          (setq sec (- sec (* num (cdr unit))))
14187                          (prog1
14188                              (concat (if prev ", " "") (int-to-string
14189                                                         (floor num))
14190                                      " " (symbol-name (car unit))
14191                                      (if (> num 1) "s" ""))
14192                            (setq prev t))))
14193                      gnus-article-time-units "")
14194                     ;; If dates are odd, then it might appear like the
14195                     ;; article was sent in the future.
14196                     (if (> real-sec 0)
14197                         " ago\n"
14198                       " in the future\n")))))
14199               (t
14200                (error "Unknown conversion type: %s" type)))))
14201           ;; Do highlighting.
14202           (beginning-of-line)
14203           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14204                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14205             (put-text-property (match-beginning 1) (match-end 1)
14206                                'face bface)
14207             (put-text-property (match-beginning 2) (match-end 2)
14208                                'face eface)))))))
14209
14210 (defun gnus-article-date-local (&optional highlight)
14211   "Convert the current article date to the local timezone."
14212   (interactive (list t))
14213   (gnus-article-date-ut 'local highlight))
14214
14215 (defun gnus-article-date-original (&optional highlight)
14216   "Convert the current article date to what it was originally.
14217 This is only useful if you have used some other date conversion
14218 function and want to see what the date was before converting."
14219   (interactive (list t))
14220   (gnus-article-date-ut 'original highlight))
14221
14222 (defun gnus-article-date-lapsed (&optional highlight)
14223   "Convert the current article date to time lapsed since it was sent."
14224   (interactive (list t))
14225   (gnus-article-date-ut 'lapsed highlight))
14226
14227 (defun gnus-article-maybe-highlight ()
14228   "Do some article highlighting if `gnus-visual' is non-nil."
14229   (if (gnus-visual-p 'article-highlight 'highlight)
14230       (gnus-article-highlight-some)))
14231
14232 ;; Article savers.
14233
14234 (defun gnus-output-to-rmail (file-name)
14235   "Append the current article to an Rmail file named FILE-NAME."
14236   (require 'rmail)
14237   ;; Most of these codes are borrowed from rmailout.el.
14238   (setq file-name (expand-file-name file-name))
14239   (setq rmail-default-rmail-file file-name)
14240   (let ((artbuf (current-buffer))
14241         (tmpbuf (get-buffer-create " *Gnus-output*")))
14242     (save-excursion
14243       (or (get-file-buffer file-name)
14244           (file-exists-p file-name)
14245           (if (gnus-yes-or-no-p
14246                (concat "\"" file-name "\" does not exist, create it? "))
14247               (let ((file-buffer (create-file-buffer file-name)))
14248                 (save-excursion
14249                   (set-buffer file-buffer)
14250                   (rmail-insert-rmail-file-header)
14251                   (let ((require-final-newline nil))
14252                     (write-region (point-min) (point-max) file-name t 1)))
14253                 (kill-buffer file-buffer))
14254             (error "Output file does not exist")))
14255       (set-buffer tmpbuf)
14256       (buffer-disable-undo (current-buffer))
14257       (erase-buffer)
14258       (insert-buffer-substring artbuf)
14259       (gnus-convert-article-to-rmail)
14260       ;; Decide whether to append to a file or to an Emacs buffer.
14261       (let ((outbuf (get-file-buffer file-name)))
14262         (if (not outbuf)
14263             (append-to-file (point-min) (point-max) file-name)
14264           ;; File has been visited, in buffer OUTBUF.
14265           (set-buffer outbuf)
14266           (let ((buffer-read-only nil)
14267                 (msg (and (boundp 'rmail-current-message)
14268                           (symbol-value 'rmail-current-message))))
14269             ;; If MSG is non-nil, buffer is in RMAIL mode.
14270             (if msg
14271                 (progn (widen)
14272                        (narrow-to-region (point-max) (point-max))))
14273             (insert-buffer-substring tmpbuf)
14274             (if msg
14275                 (progn
14276                   (goto-char (point-min))
14277                   (widen)
14278                   (search-backward "\^_")
14279                   (narrow-to-region (point) (point-max))
14280                   (goto-char (1+ (point-min)))
14281                   (rmail-count-new-messages t)
14282                   (rmail-show-message msg)))))))
14283     (kill-buffer tmpbuf)))
14284
14285 (defun gnus-output-to-file (file-name)
14286   "Append the current article to a file named FILE-NAME."
14287   (setq file-name (expand-file-name file-name))
14288   (let ((artbuf (current-buffer))
14289         (tmpbuf (get-buffer-create " *Gnus-output*")))
14290     (save-excursion
14291       (set-buffer tmpbuf)
14292       (buffer-disable-undo (current-buffer))
14293       (erase-buffer)
14294       (insert-buffer-substring artbuf)
14295       ;; Append newline at end of the buffer as separator, and then
14296       ;; save it to file.
14297       (goto-char (point-max))
14298       (insert "\n")
14299       (append-to-file (point-min) (point-max) file-name))
14300     (kill-buffer tmpbuf)))
14301
14302 (defun gnus-convert-article-to-rmail ()
14303   "Convert article in current buffer to Rmail message format."
14304   (let ((buffer-read-only nil))
14305     ;; Convert article directly into Babyl format.
14306     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14307     (goto-char (point-min))
14308     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14309     (while (search-forward "\n\^_" nil t) ;single char
14310       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14311     (goto-char (point-max))
14312     (insert "\^_")))
14313
14314 (defun gnus-narrow-to-page (&optional arg)
14315   "Narrow the article buffer to a page.
14316 If given a numerical ARG, move forward ARG pages."
14317   (interactive "P")
14318   (setq arg (if arg (prefix-numeric-value arg) 0))
14319   (save-excursion
14320     (set-buffer gnus-article-buffer)
14321     (goto-char (point-min))
14322     (widen)
14323     (when (gnus-visual-p 'page-marker)
14324       (let ((buffer-read-only nil))
14325         (gnus-remove-text-with-property 'gnus-prev)
14326         (gnus-remove-text-with-property 'gnus-next)))
14327     (when
14328         (cond ((< arg 0)
14329                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14330               ((> arg 0)
14331                (re-search-forward page-delimiter nil 'move arg)))
14332       (goto-char (match-end 0)))
14333     (narrow-to-region
14334      (point)
14335      (if (re-search-forward page-delimiter nil 'move)
14336          (match-beginning 0)
14337        (point)))
14338     (when (and (gnus-visual-p 'page-marker)
14339                (not (= (point-min) 1)))
14340       (save-excursion
14341         (goto-char (point-min))
14342         (gnus-insert-prev-page-button)))
14343     (when (and (gnus-visual-p 'page-marker)
14344                (not (= (1- (point-max)) (buffer-size))))
14345       (save-excursion
14346         (goto-char (point-max))
14347         (gnus-insert-next-page-button)))))
14348
14349 ;; Article mode commands
14350
14351 (defun gnus-article-goto-next-page ()
14352   "Show the next page of the article."
14353   (interactive)
14354   (when (gnus-article-next-page)
14355     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14356
14357 (defun gnus-article-goto-prev-page ()
14358   "Show the next page of the article."
14359   (interactive)
14360   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14361     (gnus-article-prev-page nil)))
14362
14363 (defun gnus-article-next-page (&optional lines)
14364   "Show the next page of the current article.
14365 If end of article, return non-nil.  Otherwise return nil.
14366 Argument LINES specifies lines to be scrolled up."
14367   (interactive "p")
14368   (move-to-window-line -1)
14369   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14370   (if (save-excursion
14371         (end-of-line)
14372         (and (pos-visible-in-window-p)  ;Not continuation line.
14373              (eobp)))
14374       ;; Nothing in this page.
14375       (if (or (not gnus-break-pages)
14376               (save-excursion
14377                 (save-restriction
14378                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14379           t                             ;Nothing more.
14380         (gnus-narrow-to-page 1)         ;Go to next page.
14381         nil)
14382     ;; More in this page.
14383     (condition-case ()
14384         (scroll-up lines)
14385       (end-of-buffer
14386        ;; Long lines may cause an end-of-buffer error.
14387        (goto-char (point-max))))
14388     (move-to-window-line 0)
14389     nil))
14390
14391 (defun gnus-article-prev-page (&optional lines)
14392   "Show previous page of current article.
14393 Argument LINES specifies lines to be scrolled down."
14394   (interactive "p")
14395   (move-to-window-line 0)
14396   (if (and gnus-break-pages
14397            (bobp)
14398            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14399       (progn
14400         (gnus-narrow-to-page -1)        ;Go to previous page.
14401         (goto-char (point-max))
14402         (recenter -1))
14403     (prog1
14404         (condition-case ()
14405             (scroll-down lines)
14406           (error nil))
14407       (move-to-window-line 0))))
14408
14409 (defun gnus-article-refer-article ()
14410   "Read article specified by message-id around point."
14411   (interactive)
14412   (let ((point (point)))
14413     (search-forward ">" nil t)          ;Move point to end of "<....>".
14414     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14415         (let ((message-id (match-string 1)))
14416           (goto-char point)
14417           (set-buffer gnus-summary-buffer)
14418           (gnus-summary-refer-article message-id))
14419       (goto-char (point))
14420       (error "No references around point"))))
14421
14422 (defun gnus-article-show-summary ()
14423   "Reconfigure windows to show summary buffer."
14424   (interactive)
14425   (gnus-configure-windows 'article)
14426   (gnus-summary-goto-subject gnus-current-article))
14427
14428 (defun gnus-article-describe-briefly ()
14429   "Describe article mode commands briefly."
14430   (interactive)
14431   (gnus-message 6
14432                 (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")))
14433
14434 (defun gnus-article-summary-command ()
14435   "Execute the last keystroke in the summary buffer."
14436   (interactive)
14437   (let ((obuf (current-buffer))
14438         (owin (current-window-configuration))
14439         func)
14440     (switch-to-buffer gnus-summary-buffer 'norecord)
14441     (setq func (lookup-key (current-local-map) (this-command-keys)))
14442     (call-interactively func)
14443     (set-buffer obuf)
14444     (set-window-configuration owin)
14445     (set-window-point (get-buffer-window (current-buffer)) (point))))
14446
14447 (defun gnus-article-summary-command-nosave ()
14448   "Execute the last keystroke in the summary buffer."
14449   (interactive)
14450   (let (func)
14451     (pop-to-buffer gnus-summary-buffer 'norecord)
14452     (setq func (lookup-key (current-local-map) (this-command-keys)))
14453     (call-interactively func)))
14454
14455 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14456   "Read a summary buffer key sequence and execute it from the article buffer."
14457   (interactive "P")
14458   (let ((nosaves
14459          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14460            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14461            "=" "^" "\M-^"))
14462         keys)
14463     (save-excursion
14464       (set-buffer gnus-summary-buffer)
14465       (push (or key last-command-event) unread-command-events)
14466       (setq keys (read-key-sequence nil)))
14467     (message "")
14468
14469     (if (member keys nosaves)
14470         (let (func)
14471           (pop-to-buffer gnus-summary-buffer 'norecord)
14472           (if (setq func (lookup-key (current-local-map) keys))
14473               (call-interactively func)
14474             (ding)))
14475       (let ((obuf (current-buffer))
14476             (owin (current-window-configuration))
14477             (opoint (point))
14478             func in-buffer)
14479         (if not-restore-window
14480             (pop-to-buffer gnus-summary-buffer 'norecord)
14481           (switch-to-buffer gnus-summary-buffer 'norecord))
14482         (setq in-buffer (current-buffer))
14483         (if (setq func (lookup-key (current-local-map) keys))
14484             (call-interactively func)
14485           (ding))
14486         (when (eq in-buffer (current-buffer))
14487           (set-buffer obuf)
14488           (unless not-restore-window
14489             (set-window-configuration owin))
14490           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14491
14492 \f
14493 ;;;
14494 ;;; Kill file handling.
14495 ;;;
14496
14497 ;;;###autoload
14498 (defalias 'gnus-batch-kill 'gnus-batch-score)
14499 ;;;###autoload
14500 (defun gnus-batch-score ()
14501   "Run batched scoring.
14502 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14503 Newsgroups is a list of strings in Bnews format.  If you want to score
14504 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14505 score the alt hierarchy, you'd say \"!alt.all\"."
14506   (interactive)
14507   (let* ((yes-and-no
14508           (gnus-newsrc-parse-options
14509            (apply (function concat)
14510                   (mapcar (lambda (g) (concat g " "))
14511                           command-line-args-left))))
14512          (gnus-expert-user t)
14513          (nnmail-spool-file nil)
14514          (gnus-use-dribble-file nil)
14515          (yes (car yes-and-no))
14516          (no (cdr yes-and-no))
14517          group newsrc entry
14518          ;; Disable verbose message.
14519          gnus-novice-user gnus-large-newsgroup)
14520     ;; Eat all arguments.
14521     (setq command-line-args-left nil)
14522     ;; Start Gnus.
14523     (gnus)
14524     ;; Apply kills to specified newsgroups in command line arguments.
14525     (setq newsrc (cdr gnus-newsrc-alist))
14526     (while newsrc
14527       (setq group (caar newsrc))
14528       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14529       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14530                (and (car entry)
14531                     (or (eq (car entry) t)
14532                         (not (zerop (car entry)))))
14533                (if yes (string-match yes group) t)
14534                (or (null no) (not (string-match no group))))
14535           (progn
14536             (gnus-summary-read-group group nil t nil t)
14537             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14538                  (gnus-summary-exit))))
14539       (setq newsrc (cdr newsrc)))
14540     ;; Exit Emacs.
14541     (switch-to-buffer gnus-group-buffer)
14542     (gnus-group-save-newsrc)))
14543
14544 (defun gnus-apply-kill-file ()
14545   "Apply a kill file to the current newsgroup.
14546 Returns the number of articles marked as read."
14547   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14548           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14549       (gnus-apply-kill-file-internal)
14550     0))
14551
14552 (defun gnus-kill-save-kill-buffer ()
14553   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14554     (when (get-file-buffer file)
14555       (save-excursion
14556         (set-buffer (get-file-buffer file))
14557         (and (buffer-modified-p) (save-buffer))
14558         (kill-buffer (current-buffer))))))
14559
14560 (defvar gnus-kill-file-name "KILL"
14561   "Suffix of the kill files.")
14562
14563 (defun gnus-newsgroup-kill-file (newsgroup)
14564   "Return the name of a kill file name for NEWSGROUP.
14565 If NEWSGROUP is nil, return the global kill file name instead."
14566   (cond 
14567    ;; The global KILL file is placed at top of the directory.
14568    ((or (null newsgroup)
14569         (string-equal newsgroup ""))
14570     (expand-file-name gnus-kill-file-name
14571                       (or gnus-kill-files-directory "~/News")))
14572    ;; Append ".KILL" to newsgroup name.
14573    ((gnus-use-long-file-name 'not-kill)
14574     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14575                               "." gnus-kill-file-name)
14576                       (or gnus-kill-files-directory "~/News")))
14577    ;; Place "KILL" under the hierarchical directory.
14578    (t
14579     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14580                               "/" gnus-kill-file-name)
14581                       (or gnus-kill-files-directory "~/News")))))
14582
14583 \f
14584 ;;;
14585 ;;; Dribble file
14586 ;;;
14587
14588 (defvar gnus-dribble-ignore nil)
14589 (defvar gnus-dribble-eval-file nil)
14590
14591 (defun gnus-dribble-file-name ()
14592   "Return the dribble file for the current .newsrc."
14593   (concat
14594    (if gnus-dribble-directory
14595        (concat (file-name-as-directory gnus-dribble-directory)
14596                (file-name-nondirectory gnus-current-startup-file))
14597      gnus-current-startup-file)
14598    "-dribble"))
14599
14600 (defun gnus-dribble-enter (string)
14601   "Enter STRING into the dribble buffer."
14602   (if (and (not gnus-dribble-ignore)
14603            gnus-dribble-buffer
14604            (buffer-name gnus-dribble-buffer))
14605       (let ((obuf (current-buffer)))
14606         (set-buffer gnus-dribble-buffer)
14607         (insert string "\n")
14608         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14609         (set-buffer obuf))))
14610
14611 (defun gnus-dribble-read-file ()
14612   "Read the dribble file from disk."
14613   (let ((dribble-file (gnus-dribble-file-name)))
14614     (save-excursion
14615       (set-buffer (setq gnus-dribble-buffer
14616                         (get-buffer-create
14617                          (file-name-nondirectory dribble-file))))
14618       (gnus-add-current-to-buffer-list)
14619       (erase-buffer)
14620       (setq buffer-file-name dribble-file)
14621       (auto-save-mode t)
14622       (buffer-disable-undo (current-buffer))
14623       (bury-buffer (current-buffer))
14624       (set-buffer-modified-p nil)
14625       (let ((auto (make-auto-save-file-name))
14626             (gnus-dribble-ignore t))
14627         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14628           ;; Load whichever file is newest -- the auto save file
14629           ;; or the "real" file.
14630           (if (file-newer-than-file-p auto dribble-file)
14631               (insert-file-contents auto)
14632             (insert-file-contents dribble-file))
14633           (unless (zerop (buffer-size))
14634             (set-buffer-modified-p t))
14635           ;; Set the file modes to reflect the .newsrc file modes.
14636           (save-buffer)
14637           (when (file-exists-p gnus-current-startup-file)
14638             (set-file-modes dribble-file
14639                             (file-modes gnus-current-startup-file)))
14640           ;; Possibly eval the file later.
14641           (when (gnus-y-or-n-p
14642                  "Auto-save file exists.  Do you want to read it? ")
14643             (setq gnus-dribble-eval-file t)))))))
14644
14645 (defun gnus-dribble-eval-file ()
14646   (if (not gnus-dribble-eval-file)
14647       ()
14648     (setq gnus-dribble-eval-file nil)
14649     (save-excursion
14650       (let ((gnus-dribble-ignore t))
14651         (set-buffer gnus-dribble-buffer)
14652         (eval-buffer (current-buffer))))))
14653
14654 (defun gnus-dribble-delete-file ()
14655   (if (file-exists-p (gnus-dribble-file-name))
14656       (delete-file (gnus-dribble-file-name)))
14657   (if gnus-dribble-buffer
14658       (save-excursion
14659         (set-buffer gnus-dribble-buffer)
14660         (let ((auto (make-auto-save-file-name)))
14661           (if (file-exists-p auto)
14662               (delete-file auto))
14663           (erase-buffer)
14664           (set-buffer-modified-p nil)))))
14665
14666 (defun gnus-dribble-save ()
14667   (if (and gnus-dribble-buffer
14668            (buffer-name gnus-dribble-buffer))
14669       (save-excursion
14670         (set-buffer gnus-dribble-buffer)
14671         (save-buffer))))
14672
14673 (defun gnus-dribble-clear ()
14674   (save-excursion
14675     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14676         (progn
14677           (set-buffer gnus-dribble-buffer)
14678           (erase-buffer)
14679           (set-buffer-modified-p nil)
14680           (setq buffer-saved-size (buffer-size))))))
14681
14682 \f
14683 ;;;
14684 ;;; Server Communication
14685 ;;;
14686
14687 (defun gnus-start-news-server (&optional confirm)
14688   "Open a method for getting news.
14689 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14690   (let (how)
14691     (if gnus-current-select-method
14692         ;; Stream is already opened.
14693         nil
14694       ;; Open NNTP server.
14695       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14696       (if confirm
14697           (progn
14698             ;; Read server name with completion.
14699             (setq gnus-nntp-server
14700                   (completing-read "NNTP server: "
14701                                    (mapcar (lambda (server) (list server))
14702                                            (cons (list gnus-nntp-server)
14703                                                  gnus-secondary-servers))
14704                                    nil nil gnus-nntp-server))))
14705
14706       (if (and gnus-nntp-server
14707                (stringp gnus-nntp-server)
14708                (not (string= gnus-nntp-server "")))
14709           (setq gnus-select-method
14710                 (cond ((or (string= gnus-nntp-server "")
14711                            (string= gnus-nntp-server "::"))
14712                        (list 'nnspool (system-name)))
14713                       ((string-match "^:" gnus-nntp-server)
14714                        (list 'nnmh gnus-nntp-server
14715                              (list 'nnmh-directory
14716                                    (file-name-as-directory
14717                                     (expand-file-name
14718                                      (concat "~/" (substring
14719                                                    gnus-nntp-server 1)))))
14720                              (list 'nnmh-get-new-mail nil)))
14721                       (t
14722                        (list 'nntp gnus-nntp-server)))))
14723
14724       (setq how (car gnus-select-method))
14725       (cond ((eq how 'nnspool)
14726              (require 'nnspool)
14727              (gnus-message 5 "Looking up local news spool..."))
14728             ((eq how 'nnmh)
14729              (require 'nnmh)
14730              (gnus-message 5 "Looking up mh spool..."))
14731             (t
14732              (require 'nntp)))
14733       (setq gnus-current-select-method gnus-select-method)
14734       (run-hooks 'gnus-open-server-hook)
14735       (or
14736        ;; gnus-open-server-hook might have opened it
14737        (gnus-server-opened gnus-select-method)
14738        (gnus-open-server gnus-select-method)
14739        (gnus-y-or-n-p
14740         (format
14741          "%s (%s) open error: '%s'.     Continue? "
14742          (car gnus-select-method) (cadr gnus-select-method)
14743          (gnus-status-message gnus-select-method)))
14744        (gnus-error 1 "Couldn't open server on %s"
14745                    (nth 1 gnus-select-method))))))
14746
14747 (defun gnus-check-group (group)
14748   "Try to make sure that the server where GROUP exists is alive."
14749   (let ((method (gnus-find-method-for-group group)))
14750     (or (gnus-server-opened method)
14751         (gnus-open-server method))))
14752
14753 (defun gnus-check-server (&optional method silent)
14754   "Check whether the connection to METHOD is down.
14755 If METHOD is nil, use `gnus-select-method'.
14756 If it is down, start it up (again)."
14757   (let ((method (or method gnus-select-method)))
14758     ;; Transform virtual server names into select methods.
14759     (when (stringp method)
14760       (setq method (gnus-server-to-method method)))
14761     (if (gnus-server-opened method)
14762         ;; The stream is already opened.
14763         t
14764       ;; Open the server.
14765       (unless silent
14766         (gnus-message 5 "Opening %s server%s..." (car method)
14767                       (if (equal (nth 1 method) "") ""
14768                         (format " on %s" (nth 1 method)))))
14769       (run-hooks 'gnus-open-server-hook)
14770       (prog1
14771           (gnus-open-server method)
14772         (unless silent
14773           (message ""))))))
14774
14775 (defun gnus-get-function (method function)
14776   "Return a function symbol based on METHOD and FUNCTION."
14777   ;; Translate server names into methods.
14778   (unless method
14779     (error "Attempted use of a nil select method"))
14780   (when (stringp method)
14781     (setq method (gnus-server-to-method method)))
14782   (let ((func (intern (format "%s-%s" (car method) function))))
14783     ;; If the functions isn't bound, we require the backend in
14784     ;; question.
14785     (unless (fboundp func)
14786       (require (car method))
14787       (unless (fboundp func)
14788         ;; This backend doesn't implement this function.
14789         (error "No such function: %s" func)))
14790     func))
14791
14792 \f
14793 ;;;
14794 ;;; Interface functions to the backends.
14795 ;;;
14796
14797 (defun gnus-open-server (method)
14798   "Open a connection to METHOD."
14799   (when (stringp method)
14800     (setq method (gnus-server-to-method method)))
14801   (let ((elem (assoc method gnus-opened-servers)))
14802     ;; If this method was previously denied, we just return nil.
14803     (if (eq (nth 1 elem) 'denied)
14804         (progn
14805           (gnus-message 1 "Denied server")
14806           nil)
14807       ;; Open the server.
14808       (let ((result
14809              (funcall (gnus-get-function method 'open-server)
14810                       (nth 1 method) (nthcdr 2 method))))
14811         ;; If this hasn't been opened before, we add it to the list.
14812         (unless elem
14813           (setq elem (list method nil)
14814                 gnus-opened-servers (cons elem gnus-opened-servers)))
14815         ;; Set the status of this server.
14816         (setcar (cdr elem) (if result 'ok 'denied))
14817         ;; Return the result from the "open" call.
14818         result))))
14819
14820 (defun gnus-close-server (method)
14821   "Close the connection to METHOD."
14822   (when (stringp method)
14823     (setq method (gnus-server-to-method method)))
14824   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14825
14826 (defun gnus-request-list (method)
14827   "Request the active file from METHOD."
14828   (when (stringp method)
14829     (setq method (gnus-server-to-method method)))
14830   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14831
14832 (defun gnus-request-list-newsgroups (method)
14833   "Request the newsgroups file from METHOD."
14834   (when (stringp method)
14835     (setq method (gnus-server-to-method method)))
14836   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14837
14838 (defun gnus-request-newgroups (date method)
14839   "Request all new groups since DATE from METHOD."
14840   (when (stringp method)
14841     (setq method (gnus-server-to-method method)))
14842   (funcall (gnus-get-function method 'request-newgroups)
14843            date (nth 1 method)))
14844
14845 (defun gnus-server-opened (method)
14846   "Check whether a connection to METHOD has been opened."
14847   (when (stringp method)
14848     (setq method (gnus-server-to-method method)))
14849   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14850
14851 (defun gnus-status-message (method)
14852   "Return the status message from METHOD.
14853 If METHOD is a string, it is interpreted as a group name.   The method
14854 this group uses will be queried."
14855   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14856                   method)))
14857     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14858
14859 (defun gnus-request-group (group &optional dont-check method)
14860   "Request GROUP.  If DONT-CHECK, no information is required."
14861   (let ((method (or method (gnus-find-method-for-group group))))
14862     (when (stringp method)
14863       (setq method (gnus-server-to-method method)))
14864     (funcall (gnus-get-function method 'request-group)
14865              (gnus-group-real-name group) (nth 1 method) dont-check)))
14866
14867 (defun gnus-request-asynchronous (group &optional articles)
14868   "Request that GROUP behave asynchronously.
14869 ARTICLES is the `data' of the group."
14870   (let ((method (gnus-find-method-for-group group)))
14871     (funcall (gnus-get-function method 'request-asynchronous)
14872              (gnus-group-real-name group) (nth 1 method) articles)))
14873
14874 (defun gnus-list-active-group (group)
14875   "Request active information on GROUP."
14876   (let ((method (gnus-find-method-for-group group))
14877         (func 'list-active-group))
14878     (when (gnus-check-backend-function func group)
14879       (funcall (gnus-get-function method func)
14880                (gnus-group-real-name group) (nth 1 method)))))
14881
14882 (defun gnus-request-group-description (group)
14883   "Request a description of GROUP."
14884   (let ((method (gnus-find-method-for-group group))
14885         (func 'request-group-description))
14886     (when (gnus-check-backend-function func group)
14887       (funcall (gnus-get-function method func)
14888                (gnus-group-real-name group) (nth 1 method)))))
14889
14890 (defun gnus-close-group (group)
14891   "Request the GROUP be closed."
14892   (let ((method (gnus-find-method-for-group group)))
14893     (funcall (gnus-get-function method 'close-group)
14894              (gnus-group-real-name group) (nth 1 method))))
14895
14896 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14897   "Request headers for ARTICLES in GROUP.
14898 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14899   (let ((method (gnus-find-method-for-group group)))
14900     (if (and gnus-use-cache (numberp (car articles)))
14901         (gnus-cache-retrieve-headers articles group fetch-old)
14902       (funcall (gnus-get-function method 'retrieve-headers)
14903                articles (gnus-group-real-name group) (nth 1 method)
14904                fetch-old))))
14905
14906 (defun gnus-retrieve-groups (groups method)
14907   "Request active information on GROUPS from METHOD."
14908   (when (stringp method)
14909     (setq method (gnus-server-to-method method)))
14910   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14911
14912 (defun gnus-request-type (group &optional article)
14913   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14914   (let ((method (gnus-find-method-for-group group)))
14915     (if (not (gnus-check-backend-function 'request-type (car method)))
14916         'unknown
14917       (funcall (gnus-get-function method 'request-type)
14918                (gnus-group-real-name group) article))))
14919
14920 (defun gnus-request-update-mark (group article mark)
14921   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14922   (let ((method (gnus-find-method-for-group group)))
14923     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14924         mark
14925       (funcall (gnus-get-function method 'request-update-mark)
14926                (gnus-group-real-name group) article mark))))
14927
14928 (defun gnus-request-article (article group &optional buffer)
14929   "Request the ARTICLE in GROUP.
14930 ARTICLE can either be an article number or an article Message-ID.
14931 If BUFFER, insert the article in that group."
14932   (let ((method (gnus-find-method-for-group group)))
14933     (funcall (gnus-get-function method 'request-article)
14934              article (gnus-group-real-name group) (nth 1 method) buffer)))
14935
14936 (defun gnus-request-head (article group)
14937   "Request the head of ARTICLE in GROUP."
14938   (let ((method (gnus-find-method-for-group group)))
14939     (funcall (gnus-get-function method 'request-head)
14940              article (gnus-group-real-name group) (nth 1 method))))
14941
14942 (defun gnus-request-body (article group)
14943   "Request the body of ARTICLE in GROUP."
14944   (let ((method (gnus-find-method-for-group group)))
14945     (funcall (gnus-get-function method 'request-body)
14946              article (gnus-group-real-name group) (nth 1 method))))
14947
14948 (defun gnus-request-post (method)
14949   "Post the current buffer using METHOD."
14950   (when (stringp method)
14951     (setq method (gnus-server-to-method method)))
14952   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14953
14954 (defun gnus-request-scan (group method)
14955   "Request a SCAN being performed in GROUP from METHOD.
14956 If GROUP is nil, all groups on METHOD are scanned."
14957   (let ((method (if group (gnus-find-method-for-group group) method)))
14958     (funcall (gnus-get-function method 'request-scan)
14959              (and group (gnus-group-real-name group)) (nth 1 method))))
14960
14961 (defsubst gnus-request-update-info (info method)
14962   "Request that METHOD update INFO."
14963   (when (stringp method)
14964     (setq method (gnus-server-to-method method)))
14965   (when (gnus-check-backend-function 'request-update-info (car method))
14966     (funcall (gnus-get-function method 'request-update-info)
14967              (gnus-group-real-name (gnus-info-group info))
14968              info (nth 1 method))))
14969
14970 (defun gnus-request-expire-articles (articles group &optional force)
14971   (let ((method (gnus-find-method-for-group group)))
14972     (funcall (gnus-get-function method 'request-expire-articles)
14973              articles (gnus-group-real-name group) (nth 1 method)
14974              force)))
14975
14976 (defun gnus-request-move-article
14977   (article group server accept-function &optional last)
14978   (let ((method (gnus-find-method-for-group group)))
14979     (funcall (gnus-get-function method 'request-move-article)
14980              article (gnus-group-real-name group)
14981              (nth 1 method) accept-function last)))
14982
14983 (defun gnus-request-accept-article (group method &optional last)
14984   ;; Make sure there's a newline at the end of the article.
14985   (when (stringp method)
14986     (setq method (gnus-server-to-method method)))
14987   (when (and (not method)
14988              (stringp group))
14989     (setq method (gnus-find-method-for-group group)))
14990   (goto-char (point-max))
14991   (unless (bolp)
14992     (insert "\n"))
14993   (let ((func (car (or method (gnus-find-method-for-group group)))))
14994     (funcall (intern (format "%s-request-accept-article" func))
14995              (if (stringp group) (gnus-group-real-name group) group)
14996              (cadr method)
14997              last)))
14998
14999 (defun gnus-request-replace-article (article group buffer)
15000   (let ((func (car (gnus-find-method-for-group group))))
15001     (funcall (intern (format "%s-request-replace-article" func))
15002              article (gnus-group-real-name group) buffer)))
15003
15004 (defun gnus-request-associate-buffer (group)
15005   (let ((method (gnus-find-method-for-group group)))
15006     (funcall (gnus-get-function method 'request-associate-buffer)
15007              (gnus-group-real-name group))))
15008
15009 (defun gnus-request-restore-buffer (article group)
15010   "Request a new buffer restored to the state of ARTICLE."
15011   (let ((method (gnus-find-method-for-group group)))
15012     (funcall (gnus-get-function method 'request-restore-buffer)
15013              article (gnus-group-real-name group) (nth 1 method))))
15014
15015 (defun gnus-request-create-group (group &optional method)
15016   (when (stringp method)
15017     (setq method (gnus-server-to-method method)))
15018   (let ((method (or method (gnus-find-method-for-group group))))
15019     (funcall (gnus-get-function method 'request-create-group)
15020              (gnus-group-real-name group) (nth 1 method))))
15021
15022 (defun gnus-request-delete-group (group &optional force)
15023   (let ((method (gnus-find-method-for-group group)))
15024     (funcall (gnus-get-function method 'request-delete-group)
15025              (gnus-group-real-name group) force (nth 1 method))))
15026
15027 (defun gnus-request-rename-group (group new-name)
15028   (let ((method (gnus-find-method-for-group group)))
15029     (funcall (gnus-get-function method 'request-rename-group)
15030              (gnus-group-real-name group)
15031              (gnus-group-real-name new-name) (nth 1 method))))
15032
15033 (defun gnus-member-of-valid (symbol group)
15034   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15035   (memq symbol (assoc
15036                 (symbol-name (car (gnus-find-method-for-group group)))
15037                 gnus-valid-select-methods)))
15038
15039 (defun gnus-method-option-p (method option)
15040   "Return non-nil if select METHOD has OPTION as a parameter."
15041   (when (stringp method)
15042     (setq method (gnus-server-to-method method)))
15043   (memq option (assoc (format "%s" (car method))
15044                       gnus-valid-select-methods)))
15045
15046 (defun gnus-server-extend-method (group method)
15047   ;; This function "extends" a virtual server.  If the server is
15048   ;; "hello", and the select method is ("hello" (my-var "something"))
15049   ;; in the group "alt.alt", this will result in a new virtual server
15050   ;; called "hello+alt.alt".
15051   (let ((entry
15052          (gnus-copy-sequence
15053           (if (equal (car method) "native") gnus-select-method
15054             (cdr (assoc (car method) gnus-server-alist))))))
15055     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15056     (nconc entry (cdr method))))
15057
15058 (defun gnus-find-method-for-group (group &optional info)
15059   "Find the select method that GROUP uses."
15060   (or gnus-override-method
15061       (and (not group)
15062            gnus-select-method)
15063       (let ((info (or info (gnus-get-info group)))
15064             method)
15065         (if (or (not info)
15066                 (not (setq method (gnus-info-method info)))
15067                 (equal method "native"))
15068             gnus-select-method
15069           (setq method
15070                 (cond ((stringp method)
15071                        (gnus-server-to-method method))
15072                       ((stringp (car method))
15073                        (gnus-server-extend-method group method))
15074                       (t
15075                        method)))
15076           (cond ((equal (cadr method) "")
15077                  method)
15078                 ((null (cadr method))
15079                  (list (car method) ""))
15080                 (t
15081                  (gnus-server-add-address method)))))))
15082
15083 (defun gnus-check-backend-function (func group)
15084   "Check whether GROUP supports function FUNC."
15085   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15086                   group)))
15087     (fboundp (intern (format "%s-%s" method func)))))
15088
15089 (defun gnus-methods-using (feature)
15090   "Find all methods that have FEATURE."
15091   (let ((valids gnus-valid-select-methods)
15092         outs)
15093     (while valids
15094       (if (memq feature (car valids))
15095           (setq outs (cons (car valids) outs)))
15096       (setq valids (cdr valids)))
15097     outs))
15098
15099 \f
15100 ;;;
15101 ;;; Active & Newsrc File Handling
15102 ;;;
15103
15104 (defun gnus-setup-news (&optional rawfile level dont-connect)
15105   "Setup news information.
15106 If RAWFILE is non-nil, the .newsrc file will also be read.
15107 If LEVEL is non-nil, the news will be set up at level LEVEL."
15108   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15109
15110     (when init 
15111       ;; Clear some variables to re-initialize news information.
15112       (setq gnus-newsrc-alist nil
15113             gnus-active-hashtb nil)
15114       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15115       (gnus-read-newsrc-file rawfile))
15116
15117     (when (and (not (assoc "archive" gnus-server-alist))
15118                gnus-message-archive-method)
15119       (push (cons "archive" gnus-message-archive-method)
15120             gnus-server-alist))
15121
15122     ;; If we don't read the complete active file, we fill in the
15123     ;; hashtb here.
15124     (if (or (null gnus-read-active-file)
15125             (eq gnus-read-active-file 'some))
15126         (gnus-update-active-hashtb-from-killed))
15127
15128     ;; Read the active file and create `gnus-active-hashtb'.
15129     ;; If `gnus-read-active-file' is nil, then we just create an empty
15130     ;; hash table.  The partial filling out of the hash table will be
15131     ;; done in `gnus-get-unread-articles'.
15132     (and gnus-read-active-file
15133          (not level)
15134          (gnus-read-active-file))
15135
15136     (or gnus-active-hashtb
15137         (setq gnus-active-hashtb (make-vector 4095 0)))
15138
15139     ;; Initialize the cache.
15140     (when gnus-use-cache
15141       (gnus-cache-open))
15142
15143     ;; Possibly eval the dribble file.
15144     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15145
15146     (gnus-update-format-specifications)
15147
15148     ;; See whether we need to read the description file.
15149     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15150              (not gnus-description-hashtb)
15151              (not dont-connect)
15152              gnus-read-active-file)
15153         (gnus-read-all-descriptions-files))
15154
15155     ;; Find new newsgroups and treat them.
15156     (if (and init gnus-check-new-newsgroups (not level)
15157              (gnus-check-server gnus-select-method))
15158         (gnus-find-new-newsgroups))
15159
15160     ;; We might read in new NoCeM messages here.
15161     (when (and gnus-use-nocem 
15162                (not level)
15163                (not dont-connect))
15164       (gnus-nocem-scan-groups))
15165
15166     ;; Find the number of unread articles in each non-dead group.
15167     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15168       (gnus-get-unread-articles level))
15169
15170     (if (and init gnus-check-bogus-newsgroups
15171              gnus-read-active-file (not level)
15172              (gnus-server-opened gnus-select-method))
15173         (gnus-check-bogus-newsgroups))))
15174
15175 (defun gnus-find-new-newsgroups (&optional arg)
15176   "Search for new newsgroups and add them.
15177 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15178 The `-n' option line from .newsrc is respected.
15179 If ARG (the prefix), use the `ask-server' method to query
15180 the server for new groups."
15181   (interactive "P")
15182   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15183                        (null gnus-read-active-file)
15184                        (eq gnus-read-active-file 'some))
15185                    'ask-server gnus-check-new-newsgroups)))
15186     (unless (gnus-check-first-time-used)
15187       (if (or (consp check)
15188               (eq check 'ask-server))
15189           ;; Ask the server for new groups.
15190           (gnus-ask-server-for-new-groups)
15191         ;; Go through the active hashtb and look for new groups.
15192         (let ((groups 0)
15193               group new-newsgroups)
15194           (gnus-message 5 "Looking for new newsgroups...")
15195           (unless gnus-have-read-active-file
15196             (gnus-read-active-file))
15197           (setq gnus-newsrc-last-checked-date (current-time-string))
15198           (unless gnus-killed-hashtb
15199             (gnus-make-hashtable-from-killed))
15200           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15201           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15202           (mapatoms
15203            (lambda (sym)
15204              (if (or (null (setq group (symbol-name sym)))
15205                      (not (boundp sym))
15206                      (null (symbol-value sym))
15207                      (gnus-gethash group gnus-killed-hashtb)
15208                      (gnus-gethash group gnus-newsrc-hashtb))
15209                  ()
15210                (let ((do-sub (gnus-matches-options-n group)))
15211                  (cond
15212                   ((eq do-sub 'subscribe)
15213                    (setq groups (1+ groups))
15214                    (gnus-sethash group group gnus-killed-hashtb)
15215                    (funcall gnus-subscribe-options-newsgroup-method group))
15216                   ((eq do-sub 'ignore)
15217                    nil)
15218                   (t
15219                    (setq groups (1+ groups))
15220                    (gnus-sethash group group gnus-killed-hashtb)
15221                    (if gnus-subscribe-hierarchical-interactive
15222                        (setq new-newsgroups (cons group new-newsgroups))
15223                      (funcall gnus-subscribe-newsgroup-method group)))))))
15224            gnus-active-hashtb)
15225           (when new-newsgroups
15226             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15227           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15228           (if (> groups 0)
15229               (gnus-message 6 "%d new newsgroup%s arrived."
15230                             groups (if (> groups 1) "s have" " has"))
15231             (gnus-message 6 "No new newsgroups.")))))))
15232
15233 (defun gnus-matches-options-n (group)
15234   ;; Returns `subscribe' if the group is to be unconditionally
15235   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15236   ;; no match for the group.
15237
15238   ;; First we check the two user variables.
15239   (cond
15240    ((and gnus-options-subscribe
15241          (string-match gnus-options-subscribe group))
15242     'subscribe)
15243    ((and gnus-auto-subscribed-groups
15244          (string-match gnus-auto-subscribed-groups group))
15245     'subscribe)
15246    ((and gnus-options-not-subscribe
15247          (string-match gnus-options-not-subscribe group))
15248     'ignore)
15249    ;; Then we go through the list that was retrieved from the .newsrc
15250    ;; file.  This list has elements on the form
15251    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15252    ;; is in the reverse order of the options line) is returned.
15253    (t
15254     (let ((regs gnus-newsrc-options-n))
15255       (while (and regs
15256                   (not (string-match (caar regs) group)))
15257         (setq regs (cdr regs)))
15258       (and regs (cdar regs))))))
15259
15260 (defun gnus-ask-server-for-new-groups ()
15261   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15262          (methods (cons gnus-select-method
15263                         (nconc
15264                          (when gnus-message-archive-method
15265                            (list "archive"))
15266                          (append
15267                           (and (consp gnus-check-new-newsgroups)
15268                                gnus-check-new-newsgroups)
15269                           gnus-secondary-select-methods))))
15270          (groups 0)
15271          (new-date (current-time-string))
15272          group new-newsgroups got-new method hashtb
15273          gnus-override-subscribe-method)
15274     ;; Go through both primary and secondary select methods and
15275     ;; request new newsgroups.
15276     (while (setq method (gnus-server-get-method nil (pop methods)))
15277       (setq new-newsgroups nil)
15278       (setq gnus-override-subscribe-method method)
15279       (when (and (gnus-check-server method)
15280                  (gnus-request-newgroups date method))
15281         (save-excursion
15282           (setq got-new t)
15283           (setq hashtb (gnus-make-hashtable 100))
15284           (set-buffer nntp-server-buffer)
15285           ;; Enter all the new groups into a hashtable.
15286           (gnus-active-to-gnus-format method hashtb 'ignore))
15287         ;; Now all new groups from `method' are in `hashtb'.
15288         (mapatoms
15289          (lambda (group-sym)
15290            (if (or (null (setq group (symbol-name group-sym)))
15291                    (not (boundp group-sym))
15292                    (null (symbol-value group-sym))
15293                    (gnus-gethash group gnus-newsrc-hashtb)
15294                    (member group gnus-zombie-list)
15295                    (member group gnus-killed-list))
15296                ;; The group is already known.
15297                ()
15298              ;; Make this group active.
15299              (when (symbol-value group-sym)
15300                (gnus-set-active group (symbol-value group-sym)))
15301              ;; Check whether we want it or not.
15302              (let ((do-sub (gnus-matches-options-n group)))
15303                (cond
15304                 ((eq do-sub 'subscribe)
15305                  (incf groups)
15306                  (gnus-sethash group group gnus-killed-hashtb)
15307                  (funcall gnus-subscribe-options-newsgroup-method group))
15308                 ((eq do-sub 'ignore)
15309                  nil)
15310                 (t
15311                  (incf groups)
15312                  (gnus-sethash group group gnus-killed-hashtb)
15313                  (if gnus-subscribe-hierarchical-interactive
15314                      (push group new-newsgroups)
15315                    (funcall gnus-subscribe-newsgroup-method group)))))))
15316          hashtb))
15317       (when new-newsgroups
15318         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15319     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15320     (when (> groups 0)
15321       (gnus-message 6 "%d new newsgroup%s arrived."
15322                     groups (if (> groups 1) "s have" " has")))
15323     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15324     got-new))
15325
15326 (defun gnus-check-first-time-used ()
15327   (if (or (> (length gnus-newsrc-alist) 1)
15328           (file-exists-p gnus-startup-file)
15329           (file-exists-p (concat gnus-startup-file ".el"))
15330           (file-exists-p (concat gnus-startup-file ".eld")))
15331       nil
15332     (gnus-message 6 "First time user; subscribing you to default groups")
15333     (unless gnus-have-read-active-file
15334       (gnus-read-active-file))
15335     (setq gnus-newsrc-last-checked-date (current-time-string))
15336     (let ((groups gnus-default-subscribed-newsgroups)
15337           group)
15338       (if (eq groups t)
15339           nil
15340         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15341         (mapatoms
15342          (lambda (sym)
15343            (if (null (setq group (symbol-name sym)))
15344                ()
15345              (let ((do-sub (gnus-matches-options-n group)))
15346                (cond
15347                 ((eq do-sub 'subscribe)
15348                  (gnus-sethash group group gnus-killed-hashtb)
15349                  (funcall gnus-subscribe-options-newsgroup-method group))
15350                 ((eq do-sub 'ignore)
15351                  nil)
15352                 (t
15353                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15354          gnus-active-hashtb)
15355         (while groups
15356           (if (gnus-active (car groups))
15357               (gnus-group-change-level
15358                (car groups) gnus-level-default-subscribed gnus-level-killed))
15359           (setq groups (cdr groups)))
15360         (gnus-group-make-help-group)
15361         (and gnus-novice-user
15362              (gnus-message 7 "`A k' to list killed groups"))))))
15363
15364 (defun gnus-subscribe-group (group previous &optional method)
15365   (gnus-group-change-level
15366    (if method
15367        (list t group gnus-level-default-subscribed nil nil method)
15368      group)
15369    gnus-level-default-subscribed gnus-level-killed previous t))
15370
15371 ;; `gnus-group-change-level' is the fundamental function for changing
15372 ;; subscription levels of newsgroups.  This might mean just changing
15373 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15374 ;; again, which subscribes/unsubscribes a group, which is equally
15375 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15376 ;; from 8-9 to 1-7 means that you remove the group from the list of
15377 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15378 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15379 ;; which is trivial.
15380 ;; ENTRY can either be a string (newsgroup name) or a list (if
15381 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15382 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15383 ;; entries.
15384 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15385 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15386 ;; after.
15387 (defun gnus-group-change-level (entry level &optional oldlevel
15388                                       previous fromkilled)
15389   (let (group info active num)
15390     ;; Glean what info we can from the arguments
15391     (if (consp entry)
15392         (if fromkilled (setq group (nth 1 entry))
15393           (setq group (car (nth 2 entry))))
15394       (setq group entry))
15395     (if (and (stringp entry)
15396              oldlevel
15397              (< oldlevel gnus-level-zombie))
15398         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15399     (if (and (not oldlevel)
15400              (consp entry))
15401         (setq oldlevel (gnus-info-level (nth 2 entry)))
15402       (setq oldlevel (or oldlevel 9)))
15403     (if (stringp previous)
15404         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15405
15406     (if (and (>= oldlevel gnus-level-zombie)
15407              (gnus-gethash group gnus-newsrc-hashtb))
15408         ;; We are trying to subscribe a group that is already
15409         ;; subscribed.
15410         ()                              ; Do nothing.
15411
15412       (or (gnus-ephemeral-group-p group)
15413           (gnus-dribble-enter
15414            (format "(gnus-group-change-level %S %S %S %S %S)"
15415                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15416
15417       ;; Then we remove the newgroup from any old structures, if needed.
15418       ;; If the group was killed, we remove it from the killed or zombie
15419       ;; list.  If not, and it is in fact going to be killed, we remove
15420       ;; it from the newsrc hash table and assoc.
15421       (cond
15422        ((>= oldlevel gnus-level-zombie)
15423         (if (= oldlevel gnus-level-zombie)
15424             (setq gnus-zombie-list (delete group gnus-zombie-list))
15425           (setq gnus-killed-list (delete group gnus-killed-list))))
15426        (t
15427         (if (and (>= level gnus-level-zombie)
15428                  entry)
15429             (progn
15430               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15431               (if (nth 3 entry)
15432                   (setcdr (gnus-gethash (car (nth 3 entry))
15433                                         gnus-newsrc-hashtb)
15434                           (cdr entry)))
15435               (setcdr (cdr entry) (cdddr entry))))))
15436
15437       ;; Finally we enter (if needed) the list where it is supposed to
15438       ;; go, and change the subscription level.  If it is to be killed,
15439       ;; we enter it into the killed or zombie list.
15440       (cond 
15441        ((>= level gnus-level-zombie)
15442         ;; Remove from the hash table.
15443         (gnus-sethash group nil gnus-newsrc-hashtb)
15444         ;; We do not enter foreign groups into the list of dead
15445         ;; groups.
15446         (unless (gnus-group-foreign-p group)
15447           (if (= level gnus-level-zombie)
15448               (setq gnus-zombie-list (cons group gnus-zombie-list))
15449             (setq gnus-killed-list (cons group gnus-killed-list)))))
15450        (t
15451         ;; If the list is to be entered into the newsrc assoc, and
15452         ;; it was killed, we have to create an entry in the newsrc
15453         ;; hashtb format and fix the pointers in the newsrc assoc.
15454         (if (< oldlevel gnus-level-zombie)
15455             ;; It was alive, and it is going to stay alive, so we
15456             ;; just change the level and don't change any pointers or
15457             ;; hash table entries.
15458             (setcar (cdaddr entry) level)
15459           (if (listp entry)
15460               (setq info (cdr entry)
15461                     num (car entry))
15462             (setq active (gnus-active group))
15463             (setq num
15464                   (if active (- (1+ (cdr active)) (car active)) t))
15465             ;; Check whether the group is foreign.  If so, the
15466             ;; foreign select method has to be entered into the
15467             ;; info.
15468             (let ((method (or gnus-override-subscribe-method
15469                               (gnus-group-method group))))
15470               (if (eq method gnus-select-method)
15471                   (setq info (list group level nil))
15472                 (setq info (list group level nil nil method)))))
15473           (unless previous
15474             (setq previous
15475                   (let ((p gnus-newsrc-alist))
15476                     (while (cddr p)
15477                       (setq p (cdr p)))
15478                     p)))
15479           (setq entry (cons info (cddr previous)))
15480           (if (cdr previous)
15481               (progn
15482                 (setcdr (cdr previous) entry)
15483                 (gnus-sethash group (cons num (cdr previous))
15484                               gnus-newsrc-hashtb))
15485             (setcdr previous entry)
15486             (gnus-sethash group (cons num previous)
15487                           gnus-newsrc-hashtb))
15488           (when (cdr entry)
15489             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15490       (when gnus-group-change-level-function
15491         (funcall gnus-group-change-level-function group level oldlevel)))))
15492
15493 (defun gnus-kill-newsgroup (newsgroup)
15494   "Obsolete function.  Kills a newsgroup."
15495   (gnus-group-change-level
15496    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15497
15498 (defun gnus-check-bogus-newsgroups (&optional confirm)
15499   "Remove bogus newsgroups.
15500 If CONFIRM is non-nil, the user has to confirm the deletion of every
15501 newsgroup."
15502   (let ((newsrc (cdr gnus-newsrc-alist))
15503         bogus group entry info)
15504     (gnus-message 5 "Checking bogus newsgroups...")
15505     (unless gnus-have-read-active-file
15506       (gnus-read-active-file))
15507     (when (member gnus-select-method gnus-have-read-active-file)
15508       ;; Find all bogus newsgroup that are subscribed.
15509       (while newsrc
15510         (setq info (pop newsrc)
15511               group (gnus-info-group info))
15512         (unless (or (gnus-active group) ; Active
15513                     (gnus-info-method info) ; Foreign
15514                     (and confirm
15515                          (not (gnus-y-or-n-p
15516                                (format "Remove bogus newsgroup: %s " group)))))
15517           ;; Found a bogus newsgroup.
15518           (push group bogus)))
15519       ;; Remove all bogus subscribed groups by first killing them, and
15520       ;; then removing them from the list of killed groups.
15521       (while bogus
15522         (when (setq entry (gnus-gethash (setq group (pop bogus))
15523                                         gnus-newsrc-hashtb))
15524           (gnus-group-change-level entry gnus-level-killed)
15525           (setq gnus-killed-list (delete group gnus-killed-list))))
15526       ;; Then we remove all bogus groups from the list of killed and
15527       ;; zombie groups.  They are are removed without confirmation.
15528       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15529             killed)
15530         (while dead-lists
15531           (setq killed (symbol-value (car dead-lists)))
15532           (while killed
15533             (unless (gnus-active (setq group (pop killed)))
15534               ;; The group is bogus.
15535               ;; !!!Slow as hell.
15536               (set (car dead-lists)
15537                    (delete group (symbol-value (car dead-lists))))))
15538           (setq dead-lists (cdr dead-lists))))
15539       (gnus-message 5 "Checking bogus newsgroups...done"))))
15540
15541 (defun gnus-check-duplicate-killed-groups ()
15542   "Remove duplicates from the list of killed groups."
15543   (interactive)
15544   (let ((killed gnus-killed-list))
15545     (while killed
15546       (gnus-message 9 "%d" (length killed))
15547       (setcdr killed (delete (car killed) (cdr killed)))
15548       (setq killed (cdr killed)))))
15549
15550 ;; We want to inline a function from gnus-cache, so we cheat here:
15551 (eval-when-compile
15552   (provide 'gnus)
15553   (require 'gnus-cache))
15554
15555 (defun gnus-get-unread-articles-in-group (info active &optional update)
15556   (when active
15557     ;; Allow the backend to update the info in the group.
15558     (when (and update 
15559                (gnus-request-update-info
15560                 info (gnus-find-method-for-group (gnus-info-group info))))
15561       (gnus-activate-group (gnus-info-group info) nil t))
15562     (let* ((range (gnus-info-read info))
15563            (num 0))
15564       ;; If a cache is present, we may have to alter the active info.
15565       (when (and gnus-use-cache info)
15566         (inline (gnus-cache-possibly-alter-active 
15567                  (gnus-info-group info) active)))
15568       ;; Modify the list of read articles according to what articles
15569       ;; are available; then tally the unread articles and add the
15570       ;; number to the group hash table entry.
15571       (cond
15572        ((zerop (cdr active))
15573         (setq num 0))
15574        ((not range)
15575         (setq num (- (1+ (cdr active)) (car active))))
15576        ((not (listp (cdr range)))
15577         ;; Fix a single (num . num) range according to the
15578         ;; active hash table.
15579         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15580         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15581         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15582         ;; Compute number of unread articles.
15583         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15584        (t
15585         ;; The read list is a list of ranges.  Fix them according to
15586         ;; the active hash table.
15587         ;; First peel off any elements that are below the lower
15588         ;; active limit.
15589         (while (and (cdr range)
15590                     (>= (car active)
15591                         (or (and (atom (cadr range)) (cadr range))
15592                             (caadr range))))
15593           (if (numberp (car range))
15594               (setcar range
15595                       (cons (car range)
15596                             (or (and (numberp (cadr range))
15597                                      (cadr range))
15598                                 (cdadr range))))
15599             (setcdr (car range)
15600                     (or (and (numberp (nth 1 range)) (nth 1 range))
15601                         (cdadr range))))
15602           (setcdr range (cddr range)))
15603         ;; Adjust the first element to be the same as the lower limit.
15604         (if (and (not (atom (car range)))
15605                  (< (cdar range) (car active)))
15606             (setcdr (car range) (1- (car active))))
15607         ;; Then we want to peel off any elements that are higher
15608         ;; than the upper active limit.
15609         (let ((srange range))
15610           ;; Go past all legal elements.
15611           (while (and (cdr srange)
15612                       (<= (or (and (atom (cadr srange))
15613                                    (cadr srange))
15614                               (caadr srange)) (cdr active)))
15615             (setq srange (cdr srange)))
15616           (if (cdr srange)
15617               ;; Nuke all remaining illegal elements.
15618               (setcdr srange nil))
15619
15620           ;; Adjust the final element.
15621           (if (and (not (atom (car srange)))
15622                    (> (cdar srange) (cdr active)))
15623               (setcdr (car srange) (cdr active))))
15624         ;; Compute the number of unread articles.
15625         (while range
15626           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15627                                       (cdar range)))
15628                               (or (and (atom (car range)) (car range))
15629                                   (caar range)))))
15630           (setq range (cdr range)))
15631         (setq num (max 0 (- (cdr active) num)))))
15632       ;; Set the number of unread articles.
15633       (when info
15634         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15635       num)))
15636
15637 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15638 ;; and compute how many unread articles there are in each group.
15639 (defun gnus-get-unread-articles (&optional level)
15640   (let* ((newsrc (cdr gnus-newsrc-alist))
15641          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15642          (foreign-level
15643           (min
15644            (cond ((and gnus-activate-foreign-newsgroups
15645                        (not (numberp gnus-activate-foreign-newsgroups)))
15646                   (1+ gnus-level-subscribed))
15647                  ((numberp gnus-activate-foreign-newsgroups)
15648                   gnus-activate-foreign-newsgroups)
15649                  (t 0))
15650            level))
15651          info group active method)
15652     (gnus-message 5 "Checking new news...")
15653
15654     (while newsrc
15655       (setq active (gnus-active (setq group (gnus-info-group
15656                                              (setq info (pop newsrc))))))
15657
15658       ;; Check newsgroups.  If the user doesn't want to check them, or
15659       ;; they can't be checked (for instance, if the news server can't
15660       ;; be reached) we just set the number of unread articles in this
15661       ;; newsgroup to t.  This means that Gnus thinks that there are
15662       ;; unread articles, but it has no idea how many.
15663       (if (and (setq method (gnus-info-method info))
15664                (not (gnus-server-equal
15665                      gnus-select-method
15666                      (setq method (gnus-server-get-method nil method))))
15667                (not (gnus-secondary-method-p method)))
15668           ;; These groups are foreign.  Check the level.
15669           (when (<= (gnus-info-level info) foreign-level)
15670             (setq active (gnus-activate-group group 'scan))
15671             (unless (inline (gnus-virtual-group-p group))
15672               (inline (gnus-close-group group))))
15673
15674         ;; These groups are native or secondary.
15675         (when (and (<= (gnus-info-level info) level)
15676                    (not gnus-read-active-file))
15677           (setq active (gnus-activate-group group 'scan))
15678           (inline (gnus-close-group group))))
15679
15680       (if active
15681           (inline (gnus-get-unread-articles-in-group 
15682                    info active
15683                    (and method
15684                         (fboundp (intern (concat (symbol-name (car method))
15685                                                  "-request-update-info"))))))
15686         ;; The group couldn't be reached, so we nix out the number of
15687         ;; unread articles and stuff.
15688         (gnus-set-active group nil)
15689         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15690
15691     (gnus-message 5 "Checking new news...done")))
15692
15693 ;; Create a hash table out of the newsrc alist.  The `car's of the
15694 ;; alist elements are used as keys.
15695 (defun gnus-make-hashtable-from-newsrc-alist ()
15696   (let ((alist gnus-newsrc-alist)
15697         (ohashtb gnus-newsrc-hashtb)
15698         prev)
15699     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15700     (setq alist
15701           (setq prev (setq gnus-newsrc-alist
15702                            (if (equal (caar gnus-newsrc-alist)
15703                                       "dummy.group")
15704                                gnus-newsrc-alist
15705                              (cons (list "dummy.group" 0 nil) alist)))))
15706     (while alist
15707       (gnus-sethash
15708        (caar alist)
15709        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15710              prev)
15711        gnus-newsrc-hashtb)
15712       (setq prev alist
15713             alist (cdr alist)))))
15714
15715 (defun gnus-make-hashtable-from-killed ()
15716   "Create a hash table from the killed and zombie lists."
15717   (let ((lists '(gnus-killed-list gnus-zombie-list))
15718         list)
15719     (setq gnus-killed-hashtb
15720           (gnus-make-hashtable
15721            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15722     (while (setq list (pop lists))
15723       (setq list (symbol-value list))
15724       (while list
15725         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15726
15727 (defun gnus-activate-group (group &optional scan dont-check)
15728   ;; Check whether a group has been activated or not.
15729   ;; If SCAN, request a scan of that group as well.
15730   (let ((method (gnus-find-method-for-group group))
15731         active)
15732     (and (gnus-check-server method)
15733          ;; We escape all bugs and quit here to make it possible to
15734          ;; continue if a group is so out-there that it reports bugs
15735          ;; and stuff.
15736          (progn
15737            (and scan
15738                 (gnus-check-backend-function 'request-scan (car method))
15739                 (gnus-request-scan group method))
15740            t)
15741          (condition-case ()
15742              (gnus-request-group group dont-check)
15743         ;   (error nil)
15744            (quit nil))
15745          (save-excursion
15746            (set-buffer nntp-server-buffer)
15747            (goto-char (point-min))
15748            ;; Parse the result we got from `gnus-request-group'.
15749            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15750                 (progn
15751                   (goto-char (match-beginning 1))
15752                   (gnus-set-active
15753                    group (setq active (cons (read (current-buffer))
15754                                             (read (current-buffer)))))
15755                   ;; Return the new active info.
15756                   active))))))
15757
15758 (defun gnus-update-read-articles (group unread)
15759   "Update the list of read and ticked articles in GROUP using the
15760 UNREAD and TICKED lists.
15761 Note: UNSELECTED has to be sorted over `<'.
15762 Returns whether the updating was successful."
15763   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15764          (entry (gnus-gethash group gnus-newsrc-hashtb))
15765          (info (nth 2 entry))
15766          (prev 1)
15767          (unread (sort (copy-sequence unread) '<))
15768          read)
15769     (if (or (not info) (not active))
15770         ;; There is no info on this group if it was, in fact,
15771         ;; killed.  Gnus stores no information on killed groups, so
15772         ;; there's nothing to be done.
15773         ;; One could store the information somewhere temporarily,
15774         ;; perhaps...  Hmmm...
15775         ()
15776       ;; Remove any negative articles numbers.
15777       (while (and unread (< (car unread) 0))
15778         (setq unread (cdr unread)))
15779       ;; Remove any expired article numbers
15780       (while (and unread (< (car unread) (car active)))
15781         (setq unread (cdr unread)))
15782       ;; Compute the ranges of read articles by looking at the list of
15783       ;; unread articles.
15784       (while unread
15785         (if (/= (car unread) prev)
15786             (setq read (cons (if (= prev (1- (car unread))) prev
15787                                (cons prev (1- (car unread)))) read)))
15788         (setq prev (1+ (car unread)))
15789         (setq unread (cdr unread)))
15790       (when (<= prev (cdr active))
15791         (setq read (cons (cons prev (cdr active)) read)))
15792       ;; Enter this list into the group info.
15793       (gnus-info-set-read
15794        info (if (> (length read) 1) (nreverse read) read))
15795       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15796       (gnus-get-unread-articles-in-group info (gnus-active group))
15797       t)))
15798
15799 (defun gnus-make-articles-unread (group articles)
15800   "Mark ARTICLES in GROUP as unread."
15801   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15802                           (gnus-gethash (gnus-group-real-name group)
15803                                         gnus-newsrc-hashtb))))
15804          (ranges (gnus-info-read info))
15805          news article)
15806     (while articles
15807       (when (gnus-member-of-range
15808              (setq article (pop articles)) ranges)
15809         (setq news (cons article news))))
15810     (when news
15811       (gnus-info-set-read
15812        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15813       (gnus-group-update-group group t))))
15814
15815 ;; Enter all dead groups into the hashtb.
15816 (defun gnus-update-active-hashtb-from-killed ()
15817   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15818         (lists (list gnus-killed-list gnus-zombie-list))
15819         killed)
15820     (while lists
15821       (setq killed (car lists))
15822       (while killed
15823         (gnus-sethash (car killed) nil hashtb)
15824         (setq killed (cdr killed)))
15825       (setq lists (cdr lists)))))
15826
15827 ;; Get the active file(s) from the backend(s).
15828 (defun gnus-read-active-file ()
15829   (gnus-group-set-mode-line)
15830   (let ((methods 
15831          (append
15832           (if (gnus-check-server gnus-select-method)
15833               ;; The native server is available.
15834               (cons gnus-select-method gnus-secondary-select-methods)
15835             ;; The native server is down, so we just do the
15836             ;; secondary ones.
15837             gnus-secondary-select-methods)
15838           ;; Also read from the archive server.
15839           (when gnus-message-archive-method
15840             (list "archive"))))
15841         list-type)
15842     (setq gnus-have-read-active-file nil)
15843     (save-excursion
15844       (set-buffer nntp-server-buffer)
15845       (while methods
15846         (let* ((method (if (stringp (car methods))
15847                            (gnus-server-get-method nil (car methods))
15848                          (car methods)))
15849                (where (nth 1 method))
15850                (mesg (format "Reading active file%s via %s..."
15851                              (if (and where (not (zerop (length where))))
15852                                  (concat " from " where) "")
15853                              (car method))))
15854           (gnus-message 5 mesg)
15855           (when (gnus-check-server method)
15856             ;; Request that the backend scan its incoming messages.
15857             (and (gnus-check-backend-function 'request-scan (car method))
15858                  (gnus-request-scan nil method))
15859             (cond
15860              ((and (eq gnus-read-active-file 'some)
15861                    (gnus-check-backend-function 'retrieve-groups (car method)))
15862               (let ((newsrc (cdr gnus-newsrc-alist))
15863                     (gmethod (gnus-server-get-method nil method))
15864                     groups info)
15865                 (while (setq info (pop newsrc))
15866                   (when (gnus-server-equal
15867                          (gnus-find-method-for-group 
15868                           (gnus-info-group info) info)
15869                          gmethod)
15870                     (push (gnus-group-real-name (gnus-info-group info)) 
15871                           groups)))
15872                 (when groups
15873                   (gnus-check-server method)
15874                   (setq list-type (gnus-retrieve-groups groups method))
15875                   (cond
15876                    ((not list-type)
15877                     (gnus-error
15878                      1.2 "Cannot read partial active file from %s server."
15879                      (car method)))
15880                    ((eq list-type 'active)
15881                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15882                    (t
15883                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15884              (t
15885               (if (not (gnus-request-list method))
15886                   (unless (equal method gnus-message-archive-method)
15887                     (gnus-error 1 "Cannot read active file from %s server."
15888                                 (car method)))
15889                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15890                 ;; We mark this active file as read.
15891                 (push method gnus-have-read-active-file)
15892                 (gnus-message 5 "%sdone" mesg))))))
15893         (setq methods (cdr methods))))))
15894
15895 ;; Read an active file and place the results in `gnus-active-hashtb'.
15896 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15897   (unless method
15898     (setq method gnus-select-method))
15899   (let ((cur (current-buffer))
15900         (hashtb (or hashtb
15901                     (if (and gnus-active-hashtb
15902                              (not (equal method gnus-select-method)))
15903                         gnus-active-hashtb
15904                       (setq gnus-active-hashtb
15905                             (if (equal method gnus-select-method)
15906                                 (gnus-make-hashtable
15907                                  (count-lines (point-min) (point-max)))
15908                               (gnus-make-hashtable 4096)))))))
15909     ;; Delete unnecessary lines.
15910     (goto-char (point-min))
15911     (while (search-forward "\nto." nil t)
15912       (delete-region (1+ (match-beginning 0))
15913                      (progn (forward-line 1) (point))))
15914     (or (string= gnus-ignored-newsgroups "")
15915         (progn
15916           (goto-char (point-min))
15917           (delete-matching-lines gnus-ignored-newsgroups)))
15918     ;; Make the group names readable as a lisp expression even if they
15919     ;; contain special characters.
15920     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15921     (goto-char (point-max))
15922     (while (re-search-backward "[][';?()#]" nil t)
15923       (insert ?\\))
15924     ;; If these are groups from a foreign select method, we insert the
15925     ;; group prefix in front of the group names.
15926     (and method (not (gnus-server-equal
15927                       (gnus-server-get-method nil method)
15928                       (gnus-server-get-method nil gnus-select-method)))
15929          (let ((prefix (gnus-group-prefixed-name "" method)))
15930            (goto-char (point-min))
15931            (while (and (not (eobp))
15932                        (progn (insert prefix)
15933                               (zerop (forward-line 1)))))))
15934     ;; Store the active file in a hash table.
15935     (goto-char (point-min))
15936     (if (string-match "%[oO]" gnus-group-line-format)
15937         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15938         ;; If we want information on moderated groups, we use this
15939         ;; loop...
15940         (let* ((mod-hashtb (make-vector 7 0))
15941                (m (intern "m" mod-hashtb))
15942                group max min)
15943           (while (not (eobp))
15944             (condition-case nil
15945                 (progn
15946                   (narrow-to-region (point) (gnus-point-at-eol))
15947                   (setq group (let ((obarray hashtb)) (read cur)))
15948                   (if (and (numberp (setq max (read cur)))
15949                            (numberp (setq min (read cur)))
15950                            (progn
15951                              (skip-chars-forward " \t")
15952                              (not
15953                               (or (= (following-char) ?=)
15954                                   (= (following-char) ?x)
15955                                   (= (following-char) ?j)))))
15956                       (set group (cons min max))
15957                     (set group nil))
15958                   ;; Enter moderated groups into a list.
15959                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15960                       (setq gnus-moderated-list
15961                             (cons (symbol-name group) gnus-moderated-list))))
15962               (error
15963                (and group
15964                     (symbolp group)
15965                     (set group nil))))
15966             (widen)
15967             (forward-line 1)))
15968       ;; And if we do not care about moderation, we use this loop,
15969       ;; which is faster.
15970       (let (group max min)
15971         (while (not (eobp))
15972           (condition-case ()
15973               (progn
15974                 (narrow-to-region (point) (gnus-point-at-eol))
15975                 ;; group gets set to a symbol interned in the hash table
15976                 ;; (what a hack!!) - jwz
15977                 (setq group (let ((obarray hashtb)) (read cur)))
15978                 (if (and (numberp (setq max (read cur)))
15979                          (numberp (setq min (read cur)))
15980                          (progn
15981                            (skip-chars-forward " \t")
15982                            (not
15983                             (or (= (following-char) ?=)
15984                                 (= (following-char) ?x)
15985                                 (= (following-char) ?j)))))
15986                     (set group (cons min max))
15987                   (set group nil)))
15988             (error
15989              (progn
15990                (and group
15991                     (symbolp group)
15992                     (set group nil))
15993                (or ignore-errors
15994                    (gnus-message 3 "Warning - illegal active: %s"
15995                                  (buffer-substring
15996                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15997           (widen)
15998           (forward-line 1))))))
15999
16000 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16001   ;; Parse a "groups" active file.
16002   (let ((cur (current-buffer))
16003         (hashtb (or hashtb
16004                     (if (and method gnus-active-hashtb)
16005                         gnus-active-hashtb
16006                       (setq gnus-active-hashtb
16007                             (gnus-make-hashtable
16008                              (count-lines (point-min) (point-max)))))))
16009         (prefix (and method
16010                      (not (gnus-server-equal
16011                            (gnus-server-get-method nil method)
16012                            (gnus-server-get-method nil gnus-select-method)))
16013                      (gnus-group-prefixed-name "" method))))
16014
16015     (goto-char (point-min))
16016     ;; We split this into to separate loops, one with the prefix
16017     ;; and one without to speed the reading up somewhat.
16018     (if prefix
16019         (let (min max opoint group)
16020           (while (not (eobp))
16021             (condition-case ()
16022                 (progn
16023                   (read cur) (read cur)
16024                   (setq min (read cur)
16025                         max (read cur)
16026                         opoint (point))
16027                   (skip-chars-forward " \t")
16028                   (insert prefix)
16029                   (goto-char opoint)
16030                   (set (let ((obarray hashtb)) (read cur))
16031                        (cons min max)))
16032               (error (and group (symbolp group) (set group nil))))
16033             (forward-line 1)))
16034       (let (min max group)
16035         (while (not (eobp))
16036           (condition-case ()
16037               (if (= (following-char) ?2)
16038                   (progn
16039                     (read cur) (read cur)
16040                     (setq min (read cur)
16041                           max (read cur))
16042                     (set (setq group (let ((obarray hashtb)) (read cur)))
16043                          (cons min max))))
16044             (error (and group (symbolp group) (set group nil))))
16045           (forward-line 1))))))
16046
16047 (defun gnus-read-newsrc-file (&optional force)
16048   "Read startup file.
16049 If FORCE is non-nil, the .newsrc file is read."
16050   ;; Reset variables that might be defined in the .newsrc.eld file.
16051   (let ((variables gnus-variable-list))
16052     (while variables
16053       (set (car variables) nil)
16054       (setq variables (cdr variables))))
16055   (let* ((newsrc-file gnus-current-startup-file)
16056          (quick-file (concat newsrc-file ".el")))
16057     (save-excursion
16058       ;; We always load the .newsrc.eld file.  If always contains
16059       ;; much information that can not be gotten from the .newsrc
16060       ;; file (ticked articles, killed groups, foreign methods, etc.)
16061       (gnus-read-newsrc-el-file quick-file)
16062
16063       (if (and (file-exists-p gnus-current-startup-file)
16064                (or force
16065                    (and (file-newer-than-file-p newsrc-file quick-file)
16066                         (file-newer-than-file-p newsrc-file
16067                                                 (concat quick-file "d")))
16068                    (not gnus-newsrc-alist)))
16069           ;; We read the .newsrc file.  Note that if there if a
16070           ;; .newsrc.eld file exists, it has already been read, and
16071           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16072           ;; the .newsrc file, Gnus will only use the information it
16073           ;; can find there for changing the data already read -
16074           ;; ie. reading the .newsrc file will not trash the data
16075           ;; already read (except for read articles).
16076           (save-excursion
16077             (gnus-message 5 "Reading %s..." newsrc-file)
16078             (set-buffer (find-file-noselect newsrc-file))
16079             (buffer-disable-undo (current-buffer))
16080             (gnus-newsrc-to-gnus-format)
16081             (kill-buffer (current-buffer))
16082             (gnus-message 5 "Reading %s...done" newsrc-file)))
16083
16084       ;; Read any slave files.
16085       (unless gnus-slave
16086         (gnus-master-read-slave-newsrc))
16087       
16088       ;; Convert old to new.
16089       (gnus-convert-old-newsrc))))
16090
16091 (defun gnus-continuum-version (version)
16092   "Return VERSION as a floating point number."
16093   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16094     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16095            (number (match-string 2 version))
16096            major minor least)
16097       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16098       (setq major (string-to-number (match-string 1 number)))
16099       (setq minor (string-to-number (match-string 2 number)))
16100       (setq least (if (match-beginning 3)
16101                       (string-to-number (match-string 3 number))
16102                     0))
16103       (string-to-number
16104        (if (zerop major)
16105            (format "%s00%02d%02d"
16106                    (cond 
16107                     ((string= alpha "(ding)") "4.99")
16108                     ((string= alpha "September") "5.01")
16109                     ((string= alpha "Red") "5.03"))
16110                    minor least)
16111          (format "%d.%02d%20d" major minor least))))))
16112
16113 (defun gnus-convert-old-newsrc ()
16114   "Convert old newsrc into the new format, if needed."
16115   (let ((fcv (gnus-continuum-version gnus-newsrc-file-version)))
16116     (cond
16117      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16118       (gnus-convert-old-ticks)))))
16119
16120 (defun gnus-convert-old-ticks ()
16121   (let ((newsrc (cdr gnus-newsrc-alist))
16122         marks info dormant ticked)
16123     (while (setq info (pop newsrc))
16124       (when (setq marks (gnus-info-marks info))
16125         (setq dormant (cdr (assq 'dormant marks))
16126               ticked (cdr (assq 'tick marks)))
16127         (when (or dormant ticked)
16128           (gnus-info-set-read
16129            info
16130            (gnus-add-to-range
16131             (gnus-info-read info)
16132             (nconc (gnus-uncompress-range dormant)
16133                    (gnus-uncompress-range ticked)))))))))
16134
16135 (defun gnus-read-newsrc-el-file (file)
16136   (let ((ding-file (concat file "d")))
16137     ;; We always, always read the .eld file.
16138     (gnus-message 5 "Reading %s..." ding-file)
16139     (let (gnus-newsrc-assoc)
16140       (condition-case nil
16141           (load ding-file t t t)
16142         (error
16143          (gnus-error 1 "Error in %s" ding-file)))
16144       (when gnus-newsrc-assoc
16145         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16146     (gnus-make-hashtable-from-newsrc-alist)
16147     (when (file-newer-than-file-p file ding-file)
16148       ;; Old format quick file
16149       (gnus-message 5 "Reading %s..." file)
16150       ;; The .el file is newer than the .eld file, so we read that one
16151       ;; as well.
16152       (gnus-read-old-newsrc-el-file file))))
16153
16154 ;; Parse the old-style quick startup file
16155 (defun gnus-read-old-newsrc-el-file (file)
16156   (let (newsrc killed marked group m info)
16157     (prog1
16158         (let ((gnus-killed-assoc nil)
16159               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16160           (prog1
16161               (condition-case nil
16162                   (load file t t t)
16163                 (error nil))
16164             (setq newsrc gnus-newsrc-assoc
16165                   killed gnus-killed-assoc
16166                   marked gnus-marked-assoc)))
16167       (setq gnus-newsrc-alist nil)
16168       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16169         (if info
16170             (progn
16171               (gnus-info-set-read info (cddr group))
16172               (gnus-info-set-level
16173                info (if (nth 1 group) gnus-level-default-subscribed
16174                       gnus-level-default-unsubscribed))
16175               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16176           (push (setq info
16177                       (list (car group)
16178                             (if (nth 1 group) gnus-level-default-subscribed
16179                               gnus-level-default-unsubscribed)
16180                             (cddr group)))
16181                 gnus-newsrc-alist))
16182         ;; Copy marks into info.
16183         (when (setq m (assoc (car group) marked))
16184           (unless (nthcdr 3 info)
16185             (nconc info (list nil)))
16186           (gnus-info-set-marks
16187            info (list (cons 'tick (gnus-compress-sequence 
16188                                    (sort (cdr m) '<) t))))))
16189       (setq newsrc killed)
16190       (while newsrc
16191         (setcar newsrc (caar newsrc))
16192         (setq newsrc (cdr newsrc)))
16193       (setq gnus-killed-list killed))
16194     ;; The .el file version of this variable does not begin with
16195     ;; "options", while the .eld version does, so we just add it if it
16196     ;; isn't there.
16197     (and
16198      gnus-newsrc-options
16199      (progn
16200        (and (not (string-match "^ *options" gnus-newsrc-options))
16201             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16202        (and (not (string-match "\n$" gnus-newsrc-options))
16203             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16204        ;; Finally, if we read some options lines, we parse them.
16205        (or (string= gnus-newsrc-options "")
16206            (gnus-newsrc-parse-options gnus-newsrc-options))))
16207
16208     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16209     (gnus-make-hashtable-from-newsrc-alist)))
16210
16211 (defun gnus-make-newsrc-file (file)
16212   "Make server dependent file name by catenating FILE and server host name."
16213   (let* ((file (expand-file-name file nil))
16214          (real-file (concat file "-" (nth 1 gnus-select-method))))
16215     (if (or (file-exists-p real-file)
16216             (file-exists-p (concat real-file ".el"))
16217             (file-exists-p (concat real-file ".eld")))
16218         real-file file)))
16219
16220 (defun gnus-newsrc-to-gnus-format ()
16221   (setq gnus-newsrc-options "")
16222   (setq gnus-newsrc-options-n nil)
16223
16224   (or gnus-active-hashtb
16225       (setq gnus-active-hashtb (make-vector 4095 0)))
16226   (let ((buf (current-buffer))
16227         (already-read (> (length gnus-newsrc-alist) 1))
16228         group subscribed options-symbol newsrc Options-symbol
16229         symbol reads num1)
16230     (goto-char (point-min))
16231     ;; We intern the symbol `options' in the active hashtb so that we
16232     ;; can `eq' against it later.
16233     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16234     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16235
16236     (while (not (eobp))
16237       ;; We first read the first word on the line by narrowing and
16238       ;; then reading into `gnus-active-hashtb'.  Most groups will
16239       ;; already exist in that hashtb, so this will save some string
16240       ;; space.
16241       (narrow-to-region
16242        (point)
16243        (progn (skip-chars-forward "^ \t!:\n") (point)))
16244       (goto-char (point-min))
16245       (setq symbol
16246             (and (/= (point-min) (point-max))
16247                  (let ((obarray gnus-active-hashtb)) (read buf))))
16248       (widen)
16249       ;; Now, the symbol we have read is either `options' or a group
16250       ;; name.  If it is an options line, we just add it to a string.
16251       (cond
16252        ((or (eq symbol options-symbol)
16253             (eq symbol Options-symbol))
16254         (setq gnus-newsrc-options
16255               ;; This concating is quite inefficient, but since our
16256               ;; thorough studies show that approx 99.37% of all
16257               ;; .newsrc files only contain a single options line, we
16258               ;; don't give a damn, frankly, my dear.
16259               (concat gnus-newsrc-options
16260                       (buffer-substring
16261                        (gnus-point-at-bol)
16262                        ;; Options may continue on the next line.
16263                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16264                                 (progn (beginning-of-line) (point)))
16265                            (point)))))
16266         (forward-line -1))
16267        (symbol
16268         ;; Group names can be just numbers.  
16269         (when (numberp symbol) 
16270           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16271         (or (boundp symbol) (set symbol nil))
16272         ;; It was a group name.
16273         (setq subscribed (= (following-char) ?:)
16274               group (symbol-name symbol)
16275               reads nil)
16276         (if (eolp)
16277             ;; If the line ends here, this is clearly a buggy line, so
16278             ;; we put point a the beginning of line and let the cond
16279             ;; below do the error handling.
16280             (beginning-of-line)
16281           ;; We skip to the beginning of the ranges.
16282           (skip-chars-forward "!: \t"))
16283         ;; We are now at the beginning of the list of read articles.
16284         ;; We read them range by range.
16285         (while
16286             (cond
16287              ((looking-at "[0-9]+")
16288               ;; We narrow and read a number instead of buffer-substring/
16289               ;; string-to-int because it's faster.  narrow/widen is
16290               ;; faster than save-restriction/narrow, and save-restriction
16291               ;; produces a garbage object.
16292               (setq num1 (progn
16293                            (narrow-to-region (match-beginning 0) (match-end 0))
16294                            (read buf)))
16295               (widen)
16296               ;; If the next character is a dash, then this is a range.
16297               (if (= (following-char) ?-)
16298                   (progn
16299                     ;; We read the upper bound of the range.
16300                     (forward-char 1)
16301                     (if (not (looking-at "[0-9]+"))
16302                         ;; This is a buggy line, by we pretend that
16303                         ;; it's kinda OK.  Perhaps the user should be
16304                         ;; dinged?
16305                         (setq reads (cons num1 reads))
16306                       (setq reads
16307                             (cons
16308                              (cons num1
16309                                    (progn
16310                                      (narrow-to-region (match-beginning 0)
16311                                                        (match-end 0))
16312                                      (read buf)))
16313                              reads))
16314                       (widen)))
16315                 ;; It was just a simple number, so we add it to the
16316                 ;; list of ranges.
16317                 (setq reads (cons num1 reads)))
16318               ;; If the next char in ?\n, then we have reached the end
16319               ;; of the line and return nil.
16320               (/= (following-char) ?\n))
16321              ((= (following-char) ?\n)
16322               ;; End of line, so we end.
16323               nil)
16324              (t
16325               ;; Not numbers and not eol, so this might be a buggy
16326               ;; line...
16327               (or (eobp)
16328                   ;; If it was eob instead of ?\n, we allow it.
16329                   (progn
16330                     ;; The line was buggy.
16331                     (setq group nil)
16332                     (gnus-error 3.1 "Mangled line: %s"
16333                                 (buffer-substring (gnus-point-at-bol)
16334                                                   (gnus-point-at-eol)))))
16335               nil))
16336           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16337           ;; we allow them, because it's a common mistake to put a
16338           ;; space after the comma.
16339           (skip-chars-forward ", "))
16340
16341         ;; We have already read .newsrc.eld, so we gently update the
16342         ;; data in the hash table with the information we have just
16343         ;; read.
16344         (when group
16345           (let ((info (gnus-get-info group))
16346                 level)
16347             (if info
16348                 ;; There is an entry for this file in the alist.
16349                 (progn
16350                   (gnus-info-set-read info (nreverse reads))
16351                   ;; We update the level very gently.  In fact, we
16352                   ;; only change it if there's been a status change
16353                   ;; from subscribed to unsubscribed, or vice versa.
16354                   (setq level (gnus-info-level info))
16355                   (cond ((and (<= level gnus-level-subscribed)
16356                               (not subscribed))
16357                          (setq level (if reads
16358                                          gnus-level-default-unsubscribed
16359                                        (1+ gnus-level-default-unsubscribed))))
16360                         ((and (> level gnus-level-subscribed) subscribed)
16361                          (setq level gnus-level-default-subscribed)))
16362                   (gnus-info-set-level info level))
16363               ;; This is a new group.
16364               (setq info (list group
16365                                (if subscribed
16366                                    gnus-level-default-subscribed
16367                                  (if reads
16368                                      (1+ gnus-level-subscribed)
16369                                    gnus-level-default-unsubscribed))
16370                                (nreverse reads))))
16371             (setq newsrc (cons info newsrc))))))
16372       (forward-line 1))
16373
16374     (setq newsrc (nreverse newsrc))
16375
16376     (if (not already-read)
16377         ()
16378       ;; We now have two newsrc lists - `newsrc', which is what we
16379       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16380       ;; what we've read from .newsrc.eld.  We have to merge these
16381       ;; lists.  We do this by "attaching" any (foreign) groups in the
16382       ;; gnus-newsrc-alist to the (native) group that precedes them.
16383       (let ((rc (cdr gnus-newsrc-alist))
16384             (prev gnus-newsrc-alist)
16385             entry mentry)
16386         (while rc
16387           (or (null (nth 4 (car rc)))   ; It's a native group.
16388               (assoc (caar rc) newsrc) ; It's already in the alist.
16389               (if (setq entry (assoc (caar prev) newsrc))
16390                   (setcdr (setq mentry (memq entry newsrc))
16391                           (cons (car rc) (cdr mentry)))
16392                 (setq newsrc (cons (car rc) newsrc))))
16393           (setq prev rc
16394                 rc (cdr rc)))))
16395
16396     (setq gnus-newsrc-alist newsrc)
16397     ;; We make the newsrc hashtb.
16398     (gnus-make-hashtable-from-newsrc-alist)
16399
16400     ;; Finally, if we read some options lines, we parse them.
16401     (or (string= gnus-newsrc-options "")
16402         (gnus-newsrc-parse-options gnus-newsrc-options))))
16403
16404 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16405 ;; The return value will be a list on the form
16406 ;; ((regexp1 . ignore)
16407 ;;  (regexp2 . subscribe)...)
16408 ;; When handling new newsgroups, groups that match a `ignore' regexp
16409 ;; will be ignored, and groups that match a `subscribe' regexp will be
16410 ;; subscribed.  A line like
16411 ;; options -n !all rec.all
16412 ;; will lead to a list that looks like
16413 ;; (("^rec\\..+" . subscribe)
16414 ;;  ("^.+" . ignore))
16415 ;; So all "rec.*" groups will be subscribed, while all the other
16416 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16417 ;; different from "options -n rec.all !all".
16418 (defun gnus-newsrc-parse-options (options)
16419   (let (out eol)
16420     (save-excursion
16421       (gnus-set-work-buffer)
16422       (insert (regexp-quote options))
16423       ;; First we treat all continuation lines.
16424       (goto-char (point-min))
16425       (while (re-search-forward "\n[ \t]+" nil t)
16426         (replace-match " " t t))
16427       ;; Then we transform all "all"s into ".+"s.
16428       (goto-char (point-min))
16429       (while (re-search-forward "\\ball\\b" nil t)
16430         (replace-match ".+" t t))
16431       (goto-char (point-min))
16432       ;; We remove all other options than the "-n" ones.
16433       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16434         (replace-match " ")
16435         (forward-char -1))
16436       (goto-char (point-min))
16437
16438       ;; We are only interested in "options -n" lines - we
16439       ;; ignore the other option lines.
16440       (while (re-search-forward "[ \t]-n" nil t)
16441         (setq eol
16442               (or (save-excursion
16443                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16444                          (- (point) 2)))
16445                   (gnus-point-at-eol)))
16446         ;; Search for all "words"...
16447         (while (re-search-forward "[^ \t,\n]+" eol t)
16448           (if (= (char-after (match-beginning 0)) ?!)
16449               ;; If the word begins with a bang (!), this is a "not"
16450               ;; spec.  We put this spec (minus the bang) and the
16451               ;; symbol `ignore' into the list.
16452               (setq out (cons (cons (concat
16453                                      "^" (buffer-substring
16454                                           (1+ (match-beginning 0))
16455                                           (match-end 0)))
16456                                     'ignore) out))
16457             ;; There was no bang, so this is a "yes" spec.
16458             (setq out (cons (cons (concat "^" (match-string 0))
16459                                   'subscribe) out)))))
16460
16461       (setq gnus-newsrc-options-n out))))
16462
16463 (defun gnus-save-newsrc-file (&optional force)
16464   "Save .newsrc file."
16465   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16466   ;; from the variable gnus-newsrc-alist.
16467   (when (and (or gnus-newsrc-alist gnus-killed-list)
16468              gnus-current-startup-file)
16469     (save-excursion
16470       (if (and (or gnus-use-dribble-file gnus-slave)
16471                (not force)
16472                (or (not gnus-dribble-buffer)
16473                    (not (buffer-name gnus-dribble-buffer))
16474                    (zerop (save-excursion
16475                             (set-buffer gnus-dribble-buffer)
16476                             (buffer-size)))))
16477           (gnus-message 4 "(No changes need to be saved)")
16478         (run-hooks 'gnus-save-newsrc-hook)
16479         (if gnus-slave
16480             (gnus-slave-save-newsrc)
16481           ;; Save .newsrc.
16482           (when gnus-save-newsrc-file
16483             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16484             (gnus-gnus-to-newsrc-format)
16485             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16486           ;; Save .newsrc.eld.
16487           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16488           (make-local-variable 'version-control)
16489           (setq version-control 'never)
16490           (setq buffer-file-name
16491                 (concat gnus-current-startup-file ".eld"))
16492           (gnus-add-current-to-buffer-list)
16493           (buffer-disable-undo (current-buffer))
16494           (erase-buffer)
16495           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16496           (gnus-gnus-to-quick-newsrc-format)
16497           (run-hooks 'gnus-save-quick-newsrc-hook)
16498           (save-buffer)
16499           (kill-buffer (current-buffer))
16500           (gnus-message
16501            5 "Saving %s.eld...done" gnus-current-startup-file))
16502         (gnus-dribble-delete-file)))))
16503
16504 (defun gnus-gnus-to-quick-newsrc-format ()
16505   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16506   (insert ";; Gnus startup file.\n")
16507   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16508   (insert ";; to read .newsrc.\n")
16509   (insert "(setq gnus-newsrc-file-version "
16510           (prin1-to-string gnus-version) ")\n")
16511   (let ((variables
16512          (if gnus-save-killed-list gnus-variable-list
16513            ;; Remove the `gnus-killed-list' from the list of variables
16514            ;; to be saved, if required.
16515            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16516         ;; Peel off the "dummy" group.
16517         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16518         variable)
16519     ;; Insert the variables into the file.
16520     (while variables
16521       (when (and (boundp (setq variable (pop variables)))
16522                  (symbol-value variable))
16523         (insert "(setq " (symbol-name variable) " '")
16524         (prin1 (symbol-value variable) (current-buffer))
16525         (insert ")\n")))))
16526
16527 (defun gnus-gnus-to-newsrc-format ()
16528   ;; Generate and save the .newsrc file.
16529   (save-excursion
16530     (set-buffer (create-file-buffer gnus-current-startup-file))
16531     (let ((newsrc (cdr gnus-newsrc-alist))
16532           (standard-output (current-buffer))
16533           info ranges range method)
16534       (setq buffer-file-name gnus-current-startup-file)
16535       (buffer-disable-undo (current-buffer))
16536       (erase-buffer)
16537       ;; Write options.
16538       (if gnus-newsrc-options (insert gnus-newsrc-options))
16539       ;; Write subscribed and unsubscribed.
16540       (while (setq info (pop newsrc))
16541         ;; Don't write foreign groups to .newsrc.
16542         (when (or (null (setq method (gnus-info-method info)))
16543                   (equal method "native")
16544                   (gnus-server-equal method gnus-select-method))
16545           (insert (gnus-info-group info)
16546                   (if (> (gnus-info-level info) gnus-level-subscribed)
16547                       "!" ":"))
16548           (when (setq ranges (gnus-info-read info))
16549             (insert " ")
16550             (if (not (listp (cdr ranges)))
16551                 (if (= (car ranges) (cdr ranges))
16552                     (princ (car ranges))
16553                   (princ (car ranges))
16554                   (insert "-")
16555                   (princ (cdr ranges)))
16556               (while (setq range (pop ranges))
16557                 (if (or (atom range) (= (car range) (cdr range)))
16558                     (princ (or (and (atom range) range) (car range)))
16559                   (princ (car range))
16560                   (insert "-")
16561                   (princ (cdr range)))
16562                 (if ranges (insert ",")))))
16563           (insert "\n")))
16564       (make-local-variable 'version-control)
16565       (setq version-control 'never)
16566       ;; It has been reported that sometime the modtime on the .newsrc
16567       ;; file seems to be off.  We really do want to overwrite it, so
16568       ;; we clear the modtime here before saving.  It's a bit odd,
16569       ;; though...
16570       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16571       ;; delete the silly thing entirely first.  but this fails to provide
16572       ;; such niceties as .newsrc~ creation.
16573       (if gnus-modtime-botch
16574           (delete-file gnus-startup-file)
16575         (clear-visited-file-modtime))
16576       (run-hooks 'gnus-save-standard-newsrc-hook)
16577       (save-buffer)
16578       (kill-buffer (current-buffer)))))
16579
16580 \f
16581 ;;;
16582 ;;; Slave functions.
16583 ;;;
16584
16585 (defun gnus-slave-save-newsrc ()
16586   (save-excursion
16587     (set-buffer gnus-dribble-buffer)
16588     (let ((slave-name
16589            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16590       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16591
16592 (defun gnus-master-read-slave-newsrc ()
16593   (let ((slave-files
16594          (directory-files
16595           (file-name-directory gnus-current-startup-file)
16596           t (concat
16597              "^" (regexp-quote
16598                   (concat
16599                    (file-name-nondirectory gnus-current-startup-file)
16600                    "-slave-")))
16601           t))
16602         file)
16603     (if (not slave-files)
16604         ()                              ; There are no slave files to read.
16605       (gnus-message 7 "Reading slave newsrcs...")
16606       (save-excursion
16607         (set-buffer (get-buffer-create " *gnus slave*"))
16608         (buffer-disable-undo (current-buffer))
16609         (setq slave-files
16610               (sort (mapcar (lambda (file)
16611                               (list (nth 5 (file-attributes file)) file))
16612                             slave-files)
16613                     (lambda (f1 f2)
16614                       (or (< (caar f1) (caar f2))
16615                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16616         (while slave-files
16617           (erase-buffer)
16618           (setq file (nth 1 (car slave-files)))
16619           (insert-file-contents file)
16620           (if (condition-case ()
16621                   (progn
16622                     (eval-buffer (current-buffer))
16623                     t)
16624                 (error
16625                  (gnus-error 3.2 "Possible error in %s" file)
16626                  nil))
16627               (or gnus-slave ; Slaves shouldn't delete these files.
16628                   (condition-case ()
16629                       (delete-file file)
16630                     (error nil))))
16631           (setq slave-files (cdr slave-files))))
16632       (gnus-message 7 "Reading slave newsrcs...done"))))
16633
16634 \f
16635 ;;;
16636 ;;; Group description.
16637 ;;;
16638
16639 (defun gnus-read-all-descriptions-files ()
16640   (let ((methods (cons gnus-select-method 
16641                        (nconc
16642                         (when gnus-message-archive-method
16643                           (list "archive"))
16644                         gnus-secondary-select-methods))))
16645     (while methods
16646       (gnus-read-descriptions-file (car methods))
16647       (setq methods (cdr methods)))
16648     t))
16649
16650 (defun gnus-read-descriptions-file (&optional method)
16651   (let ((method (or method gnus-select-method)))
16652     (when (stringp method)
16653       (setq method (gnus-server-to-method method)))
16654     ;; We create the hashtable whether we manage to read the desc file
16655     ;; to avoid trying to re-read after a failed read.
16656     (or gnus-description-hashtb
16657         (setq gnus-description-hashtb
16658               (gnus-make-hashtable (length gnus-active-hashtb))))
16659     ;; Mark this method's desc file as read.
16660     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16661                   gnus-description-hashtb)
16662
16663     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16664     (cond
16665      ((not (gnus-check-server method))
16666       (gnus-message 1 "Couldn't open server")
16667       nil)
16668      ((not (gnus-request-list-newsgroups method))
16669       (gnus-message 1 "Couldn't read newsgroups descriptions")
16670       nil)
16671      (t
16672       (let (group)
16673         (save-excursion
16674           (save-restriction
16675             (set-buffer nntp-server-buffer)
16676             (goto-char (point-min))
16677             (if (or (search-forward "\n.\n" nil t)
16678                     (goto-char (point-max)))
16679                 (progn
16680                   (beginning-of-line)
16681                   (narrow-to-region (point-min) (point))))
16682             (goto-char (point-min))
16683             (while (not (eobp))
16684               ;; If we get an error, we set group to 0, which is not a
16685               ;; symbol...
16686               (setq group
16687                     (condition-case ()
16688                         (let ((obarray gnus-description-hashtb))
16689                           ;; Group is set to a symbol interned in this
16690                           ;; hash table.
16691                           (read nntp-server-buffer))
16692                       (error 0)))
16693               (skip-chars-forward " \t")
16694               ;; ...  which leads to this line being effectively ignored.
16695               (and (symbolp group)
16696                    (set group (buffer-substring
16697                                (point) (progn (end-of-line) (point)))))
16698               (forward-line 1))))
16699         (gnus-message 5 "Reading descriptions file...done")
16700         t)))))
16701
16702 (defun gnus-group-get-description (group)
16703   "Get the description of a group by sending XGTITLE to the server."
16704   (when (gnus-request-group-description group)
16705     (save-excursion
16706       (set-buffer nntp-server-buffer)
16707       (goto-char (point-min))
16708       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16709         (match-string 1)))))
16710
16711 \f
16712 ;;;
16713 ;;; Buffering of read articles.
16714 ;;;
16715
16716 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16717 (defvar gnus-backlog-articles nil)
16718 (defvar gnus-backlog-hashtb nil)
16719
16720 (defun gnus-backlog-buffer ()
16721   "Return the backlog buffer."
16722   (or (get-buffer gnus-backlog-buffer)
16723       (save-excursion
16724         (set-buffer (get-buffer-create gnus-backlog-buffer))
16725         (buffer-disable-undo (current-buffer))
16726         (setq buffer-read-only t)
16727         (gnus-add-current-to-buffer-list)
16728         (get-buffer gnus-backlog-buffer))))
16729
16730 (defun gnus-backlog-setup ()
16731   "Initialize backlog variables."
16732   (unless gnus-backlog-hashtb
16733     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16734
16735 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16736
16737 (defun gnus-backlog-shutdown ()
16738   "Clear all backlog variables and buffers."
16739   (when (get-buffer gnus-backlog-buffer)
16740     (kill-buffer gnus-backlog-buffer))
16741   (setq gnus-backlog-hashtb nil
16742         gnus-backlog-articles nil))
16743
16744 (defun gnus-backlog-enter-article (group number buffer)
16745   (gnus-backlog-setup)
16746   (let ((ident (intern (concat group ":" (int-to-string number))
16747                        gnus-backlog-hashtb))
16748         b)
16749     (if (memq ident gnus-backlog-articles)
16750         () ; It's already kept.
16751       ;; Remove the oldest article, if necessary.
16752       (and (numberp gnus-keep-backlog)
16753            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16754            (gnus-backlog-remove-oldest-article))
16755       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16756       ;; Insert the new article.
16757       (save-excursion
16758         (set-buffer (gnus-backlog-buffer))
16759         (let (buffer-read-only)
16760           (goto-char (point-max))
16761           (or (bolp) (insert "\n"))
16762           (setq b (point))
16763           (insert-buffer-substring buffer)
16764           ;; Tag the beginning of the article with the ident.
16765           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16766
16767 (defun gnus-backlog-remove-oldest-article ()
16768   (save-excursion
16769     (set-buffer (gnus-backlog-buffer))
16770     (goto-char (point-min))
16771     (if (zerop (buffer-size))
16772         () ; The buffer is empty.
16773       (let ((ident (get-text-property (point) 'gnus-backlog))
16774             buffer-read-only)
16775         ;; Remove the ident from the list of articles.
16776         (when ident
16777           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16778         ;; Delete the article itself.
16779         (delete-region
16780          (point) (next-single-property-change
16781                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16782
16783 (defun gnus-backlog-request-article (group number buffer)
16784   (when (numberp number)
16785     (gnus-backlog-setup)
16786     (let ((ident (intern (concat group ":" (int-to-string number))
16787                          gnus-backlog-hashtb))
16788           beg end)
16789       (when (memq ident gnus-backlog-articles)
16790         ;; It was in the backlog.
16791         (save-excursion
16792           (set-buffer (gnus-backlog-buffer))
16793           (if (not (setq beg (text-property-any
16794                               (point-min) (point-max) 'gnus-backlog
16795                               ident)))
16796               ;; It wasn't in the backlog after all.
16797               (ignore
16798                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16799             ;; Find the end (i. e., the beginning of the next article).
16800             (setq end
16801                   (next-single-property-change
16802                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16803         (let ((buffer-read-only nil))
16804           (erase-buffer)
16805           (insert-buffer-substring gnus-backlog-buffer beg end)
16806           t)))))
16807
16808 ;; Allow redefinition of Gnus functions.
16809
16810 (gnus-ems-redefine)
16811
16812 (provide 'gnus)
16813
16814 ;;; gnus.el ends here