*** 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     (faq . gnus-faq-buffer)
953     (picons . "*Picons*")
954     (tree . gnus-tree-buffer)
955     (info . gnus-info-buffer)
956     (article-copy . gnus-article-copy)
957     (draft . gnus-draft-buffer))
958   "Mapping from short symbols to buffer names or buffer variables.")
959
960 (defvar gnus-carpal nil
961   "*If non-nil, display clickable icons.")
962
963 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
964   "*Function called with a group name when new group is detected.
965 A few pre-made functions are supplied: `gnus-subscribe-randomly'
966 inserts new groups at the beginning of the list of groups;
967 `gnus-subscribe-alphabetically' inserts new groups in strict
968 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
969 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
970 for your decision; `gnus-subscribe-killed' kills all new groups.")
971
972 ;; Suggested by a bug report by Hallvard B Furuseth.
973 ;; <h.b.furuseth@usit.uio.no>.
974 (defvar gnus-subscribe-options-newsgroup-method
975   (function gnus-subscribe-alphabetically)
976   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
977 If, for instance, you want to subscribe to all newsgroups in the
978 \"no\" and \"alt\" hierarchies, you'd put the following in your
979 .newsrc file:
980
981 options -n no.all alt.all
982
983 Gnus will the subscribe all new newsgroups in these hierarchies with
984 the subscription method in this variable.")
985
986 (defvar gnus-subscribe-hierarchical-interactive nil
987   "*If non-nil, Gnus will offer to subscribe hierarchically.
988 When a new hierarchy appears, Gnus will ask the user:
989
990 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
991
992 If the user pressed `d', Gnus will descend the hierarchy, `y' will
993 subscribe to all newsgroups in the hierarchy and `s' will skip this
994 hierarchy in its entirety.")
995
996 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
997   "*Function used for sorting the group buffer.
998 This function will be called with group info entries as the arguments
999 for the groups to be sorted.  Pre-made functions include
1000 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1001 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1002 `gnus-group-sort-by-rank'.
1003
1004 This variable can also be a list of sorting functions.  In that case,
1005 the most significant sort function should be the last function in the
1006 list.")
1007
1008 ;; Mark variables suggested by Thomas Michanek
1009 ;; <Thomas.Michanek@telelogic.se>.
1010 (defvar gnus-unread-mark ? 
1011   "*Mark used for unread articles.")
1012 (defvar gnus-ticked-mark ?!
1013   "*Mark used for ticked articles.")
1014 (defvar gnus-dormant-mark ??
1015   "*Mark used for dormant articles.")
1016 (defvar gnus-del-mark ?r
1017   "*Mark used for del'd articles.")
1018 (defvar gnus-read-mark ?R
1019   "*Mark used for read articles.")
1020 (defvar gnus-expirable-mark ?E
1021   "*Mark used for expirable articles.")
1022 (defvar gnus-killed-mark ?K
1023   "*Mark used for killed articles.")
1024 (defvar gnus-souped-mark ?F
1025   "*Mark used for killed articles.")
1026 (defvar gnus-kill-file-mark ?X
1027   "*Mark used for articles killed by kill files.")
1028 (defvar gnus-low-score-mark ?Y
1029   "*Mark used for articles with a low score.")
1030 (defvar gnus-catchup-mark ?C
1031   "*Mark used for articles that are caught up.")
1032 (defvar gnus-replied-mark ?A
1033   "*Mark used for articles that have been replied to.")
1034 (defvar gnus-cached-mark ?*
1035   "*Mark used for articles that are in the cache.")
1036 (defvar gnus-saved-mark ?S
1037   "*Mark used for articles that have been saved to.")
1038 (defvar gnus-process-mark ?#
1039   "*Process mark.")
1040 (defvar gnus-ancient-mark ?O
1041   "*Mark used for ancient articles.")
1042 (defvar gnus-sparse-mark ?Q
1043   "*Mark used for sparsely reffed articles.")
1044 (defvar gnus-canceled-mark ?G
1045   "*Mark used for canceled articles.")
1046 (defvar gnus-score-over-mark ?+
1047   "*Score mark used for articles with high scores.")
1048 (defvar gnus-score-below-mark ?-
1049   "*Score mark used for articles with low scores.")
1050 (defvar gnus-empty-thread-mark ? 
1051   "*There is no thread under the article.")
1052 (defvar gnus-not-empty-thread-mark ?=
1053   "*There is a thread under the article.")
1054
1055 (defvar gnus-view-pseudo-asynchronously nil
1056   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1057
1058 (defvar gnus-view-pseudos nil
1059   "*If `automatic', pseudo-articles will be viewed automatically.
1060 If `not-confirm', pseudos will be viewed automatically, and the user
1061 will not be asked to confirm the command.")
1062
1063 (defvar gnus-view-pseudos-separately t
1064   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1065 If nil, all files that use the same viewing command will be given as a
1066 list of parameters to that command.")
1067
1068 (defvar gnus-insert-pseudo-articles t
1069   "*If non-nil, insert pseudo-articles when decoding articles.")
1070
1071 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1072   "*Format of group lines.
1073 It works along the same lines as a normal formatting string,
1074 with some simple extensions.
1075
1076 %M    Only marked articles (character, \"*\" or \" \")
1077 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1078 %L    Level of subscribedness (integer)
1079 %N    Number of unread articles (integer)
1080 %I    Number of dormant articles (integer)
1081 %i    Number of ticked and dormant (integer)
1082 %T    Number of ticked articles (integer)
1083 %R    Number of read articles (integer)
1084 %t    Total number of articles (integer)
1085 %y    Number of unread, unticked articles (integer)
1086 %G    Group name (string)
1087 %g    Qualified group name (string)
1088 %D    Group description (string)
1089 %s    Select method (string)
1090 %o    Moderated group (char, \"m\")
1091 %p    Process mark (char)
1092 %O    Moderated group (string, \"(m)\" or \"\")
1093 %P    Topic indentation (string)
1094 %l    Whether there are GroupLens predictions for this group (string)
1095 %n    Select from where (string)
1096 %z    A string that look like `<%s:%n>' if a foreign select method is used
1097 %u    User defined specifier.  The next character in the format string should
1098       be a letter.  Gnus will call the function gnus-user-format-function-X,
1099       where X is the letter following %u.  The function will be passed the
1100       current header as argument.  The function should return a string, which
1101       will be inserted into the buffer just like information from any other
1102       group specifier.
1103
1104 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1105 the mouse point move inside the area.  There can only be one such area.
1106
1107 Note that this format specification is not always respected.  For
1108 reasons of efficiency, when listing killed groups, this specification
1109 is ignored altogether.  If the spec is changed considerably, your
1110 output may end up looking strange when listing both alive and killed
1111 groups.
1112
1113 If you use %o or %O, reading the active file will be slower and quite
1114 a bit of extra memory will be used. %D will also worsen performance.
1115 Also note that if you change the format specification to include any
1116 of these specs, you must probably re-start Gnus to see them go into
1117 effect.")
1118
1119 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1120   "*The format specification of the lines in the summary buffer.
1121
1122 It works along the same lines as a normal formatting string,
1123 with some simple extensions.
1124
1125 %N   Article number, left padded with spaces (string)
1126 %S   Subject (string)
1127 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1128 %n   Name of the poster (string)
1129 %a   Extracted name of the poster (string)
1130 %A   Extracted address of the poster (string)
1131 %F   Contents of the From: header (string)
1132 %x   Contents of the Xref: header (string)
1133 %D   Date of the article (string)
1134 %d   Date of the article (string) in DD-MMM format
1135 %M   Message-id of the article (string)
1136 %r   References of the article (string)
1137 %c   Number of characters in the article (integer)
1138 %L   Number of lines in the article (integer)
1139 %I   Indentation based on thread level (a string of spaces)
1140 %T   A string with two possible values: 80 spaces if the article
1141      is on thread level two or larger and 0 spaces on level one
1142 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1143 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1144 %[   Opening bracket (character, \"[\" or \"<\")
1145 %]   Closing bracket (character, \"]\" or \">\")
1146 %>   Spaces of length thread-level (string)
1147 %<   Spaces of length (- 20 thread-level) (string)
1148 %i   Article score (number)
1149 %z   Article zcore (character)
1150 %t   Number of articles under the current thread (number).
1151 %e   Whether the thread is empty or not (character).
1152 %l   GroupLens score (string).
1153 %u   User defined specifier.  The next character in the format string should
1154      be a letter.  Gnus will call the function gnus-user-format-function-X,
1155      where X is the letter following %u.  The function will be passed the
1156      current header as argument.  The function should return a string, which
1157      will be inserted into the summary just like information from any other
1158      summary specifier.
1159
1160 Text between %( and %) will be highlighted with `gnus-mouse-face'
1161 when the mouse point is placed inside the area.  There can only be one
1162 such area.
1163
1164 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1165 with care.  For reasons of efficiency, Gnus will compute what column
1166 these characters will end up in, and \"hard-code\" that.  This means that
1167 it is illegal to have these specs after a variable-length spec.  Well,
1168 you might not be arrested, but your summary buffer will look strange,
1169 which is bad enough.
1170
1171 The smart choice is to have these specs as for to the left as
1172 possible.
1173
1174 This restriction may disappear in later versions of Gnus.")
1175
1176 (defvar gnus-summary-dummy-line-format
1177   "*  %(:                          :%) %S\n"
1178   "*The format specification for the dummy roots in the summary buffer.
1179 It works along the same lines as a normal formatting string,
1180 with some simple extensions.
1181
1182 %S  The subject")
1183
1184 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1185   "*The format specification for the summary mode line.
1186 It works along the same lines as a normal formatting string,
1187 with some simple extensions:
1188
1189 %G  Group name
1190 %p  Unprefixed group name
1191 %A  Current article number
1192 %V  Gnus version
1193 %U  Number of unread articles in the group
1194 %e  Number of unselected articles in the group
1195 %Z  A string with unread/unselected article counts
1196 %g  Shortish group name
1197 %S  Subject of the current article
1198 %u  User-defined spec
1199 %s  Current score file name
1200 %d  Number of dormant articles
1201 %r  Number of articles that have been marked as read in this session
1202 %E  Number of articles expunged by the score files")
1203
1204 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1205   "*The format specification for the article mode line.
1206 See `gnus-summary-mode-line-format' for a closer description.")
1207
1208 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1209   "*The format specification for the group mode line.
1210 It works along the same lines as a normal formatting string,
1211 with some simple extensions:
1212
1213 %S   The native news server.
1214 %M   The native select method.")
1215
1216 (defvar gnus-valid-select-methods
1217   '(("nntp" post address prompt-address)
1218     ("nnspool" post address)
1219     ("nnvirtual" post-mail virtual prompt-address)
1220     ("nnmbox" mail respool address)
1221     ("nnml" mail respool address)
1222     ("nnmh" mail respool address)
1223     ("nndir" post-mail prompt-address address)
1224     ("nneething" none address prompt-address)
1225     ("nndoc" none address prompt-address)
1226     ("nnbabyl" mail address respool)
1227     ("nnkiboze" post address virtual)
1228     ("nnsoup" post-mail address)
1229     ("nndraft" post-mail)
1230     ("nnfolder" mail respool address))
1231   "An alist of valid select methods.
1232 The first element of each list lists should be a string with the name
1233 of the select method.  The other elements may be be the category of
1234 this method (ie. `post', `mail', `none' or whatever) or other
1235 properties that this method has (like being respoolable).
1236 If you implement a new select method, all you should have to change is
1237 this variable.  I think.")
1238
1239 (defvar gnus-updated-mode-lines '(group article summary tree)
1240   "*List of buffers that should update their mode lines.
1241 The list may contain the symbols `group', `article' and `summary'.  If
1242 the corresponding symbol is present, Gnus will keep that mode line
1243 updated with information that may be pertinent.
1244 If this variable is nil, screen refresh may be quicker.")
1245
1246 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1247 (defvar gnus-mode-non-string-length nil
1248   "*Max length of mode-line non-string contents.
1249 If this is nil, Gnus will take space as is needed, leaving the rest
1250 of the modeline intact.")
1251
1252 ;see gnus-cus.el
1253 ;(defvar gnus-mouse-face 'highlight
1254 ;  "*Face used for mouse highlighting in Gnus.
1255 ;No mouse highlights will be done if `gnus-visual' is nil.")
1256
1257 (defvar gnus-summary-mark-below nil
1258   "*Mark all articles with a score below this variable as read.
1259 This variable is local to each summary buffer and usually set by the
1260 score file.")
1261
1262 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1263   "*List of functions used for sorting articles in the summary buffer.
1264 This variable is only used when not using a threaded display.")
1265
1266 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1267   "*List of functions used for sorting threads in the summary buffer.
1268 By default, threads are sorted by article number.
1269
1270 Each function takes two threads and return non-nil if the first thread
1271 should be sorted before the other.  If you use more than one function,
1272 the primary sort function should be the last.  You should probably
1273 always include `gnus-thread-sort-by-number' in the list of sorting
1274 functions -- preferably first.
1275
1276 Ready-mady functions include `gnus-thread-sort-by-number',
1277 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1278 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1279 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1280
1281 (defvar gnus-thread-score-function '+
1282   "*Function used for calculating the total score of a thread.
1283
1284 The function is called with the scores of the article and each
1285 subthread and should then return the score of the thread.
1286
1287 Some functions you can use are `+', `max', or `min'.")
1288
1289 (defvar gnus-summary-expunge-below nil
1290   "All articles that have a score less than this variable will be expunged.")
1291
1292 (defvar gnus-thread-expunge-below nil
1293   "All threads that have a total score less than this variable will be expunged.
1294 See `gnus-thread-score-function' for en explanation of what a
1295 \"thread score\" is.")
1296
1297 (defvar gnus-auto-subscribed-groups
1298   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1299   "*All new groups that match this regexp will be subscribed automatically.
1300 Note that this variable only deals with new groups.  It has no effect
1301 whatsoever on old groups.")
1302
1303 (defvar gnus-options-subscribe nil
1304   "*All new groups matching this regexp will be subscribed unconditionally.
1305 Note that this variable deals only with new newsgroups.  This variable
1306 does not affect old newsgroups.")
1307
1308 (defvar gnus-options-not-subscribe nil
1309   "*All new groups matching this regexp will be ignored.
1310 Note that this variable deals only with new newsgroups.  This variable
1311 does not affect old (already subscribed) newsgroups.")
1312
1313 (defvar gnus-auto-expirable-newsgroups nil
1314   "*Groups in which to automatically mark read articles as expirable.
1315 If non-nil, this should be a regexp that should match all groups in
1316 which to perform auto-expiry.  This only makes sense for mail groups.")
1317
1318 (defvar gnus-total-expirable-newsgroups nil
1319   "*Groups in which to perform expiry of all read articles.
1320 Use with extreme caution.  All groups that match this regexp will be
1321 expiring - which means that all read articles will be deleted after
1322 (say) one week.  (This only goes for mail groups and the like, of
1323 course.)")
1324
1325 (defvar gnus-group-uncollapsed-levels 1
1326   "Number of group name elements to leave alone when making a short group name.")
1327
1328 (defvar gnus-hidden-properties '(invisible t intangible t)
1329   "Property list to use for hiding text.")
1330
1331 (defvar gnus-modtime-botch nil
1332   "*Non-nil means .newsrc should be deleted prior to save.  
1333 Its use is due to the bogus appearance that .newsrc was modified on
1334 disc.")
1335
1336 ;; Hooks.
1337
1338 (defvar gnus-group-mode-hook nil
1339   "*A hook for Gnus group mode.")
1340
1341 (defvar gnus-summary-mode-hook nil
1342   "*A hook for Gnus summary mode.
1343 This hook is run before any variables are set in the summary buffer.")
1344
1345 (defvar gnus-article-mode-hook nil
1346   "*A hook for Gnus article mode.")
1347
1348 (defvar gnus-summary-prepare-exit-hook nil
1349   "*A hook called when preparing to exit from the summary buffer.
1350 It calls `gnus-summary-expire-articles' by default.")
1351 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1352
1353 (defvar gnus-summary-exit-hook nil
1354   "*A hook called on exit from the summary buffer.")
1355
1356 (defvar gnus-group-catchup-group-hook nil
1357   "*A hook run when catching up a group from the group buffer.")
1358
1359 (defvar gnus-open-server-hook nil
1360   "*A hook called just before opening connection to the news server.")
1361
1362 (defvar gnus-load-hook nil
1363   "*A hook run while Gnus is loaded.")
1364
1365 (defvar gnus-startup-hook nil
1366   "*A hook called at startup.
1367 This hook is called after Gnus is connected to the NNTP server.")
1368
1369 (defvar gnus-get-new-news-hook nil
1370   "*A hook run just before Gnus checks for new news.")
1371
1372 (defvar gnus-after-getting-new-news-hook nil
1373   "*A hook run after Gnus checks for new news.")
1374
1375 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1376   "*A function that is called to generate the group buffer.
1377 The function is called with three arguments: The first is a number;
1378 all group with a level less or equal to that number should be listed,
1379 if the second is non-nil, empty groups should also be displayed.  If
1380 the third is non-nil, it is a number.  No groups with a level lower
1381 than this number should be displayed.
1382
1383 The only current function implemented is `gnus-group-prepare-flat'.")
1384
1385 (defvar gnus-group-prepare-hook nil
1386   "*A hook called after the group buffer has been generated.
1387 If you want to modify the group buffer, you can use this hook.")
1388
1389 (defvar gnus-summary-prepare-hook nil
1390   "*A hook called after the summary buffer has been generated.
1391 If you want to modify the summary buffer, you can use this hook.")
1392
1393 (defvar gnus-summary-generate-hook nil
1394   "*A hook run just before generating the summary buffer.
1395 This hook is commonly used to customize threading variables and the
1396 like.")
1397
1398 (defvar gnus-article-prepare-hook nil
1399   "*A hook called after an article has been prepared in the article buffer.
1400 If you want to run a special decoding program like nkf, use this hook.")
1401
1402 ;(defvar gnus-article-display-hook nil
1403 ;  "*A hook called after the article is displayed in the article buffer.
1404 ;The hook is designed to change the contents of the article
1405 ;buffer.  Typical functions that this hook may contain are
1406 ;`gnus-article-hide-headers' (hide selected headers),
1407 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1408 ;`gnus-article-hide-signature' (hide signature) and
1409 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1410 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1411 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1413
1414 (defvar gnus-article-x-face-command
1415   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1416   "String or function to be executed to display an X-Face header.
1417 If it is a string, the command will be executed in a sub-shell
1418 asynchronously.  The compressed face will be piped to this command.")
1419
1420 (defvar gnus-article-x-face-too-ugly nil
1421   "Regexp matching posters whose face shouldn't be shown automatically.")
1422
1423 (defvar gnus-select-group-hook nil
1424   "*A hook called when a newsgroup is selected.
1425
1426 If you'd like to simplify subjects like the
1427 `gnus-summary-next-same-subject' command does, you can use the
1428 following hook:
1429
1430  (setq gnus-select-group-hook
1431       (list
1432         (lambda ()
1433           (mapcar (lambda (header)
1434                      (mail-header-set-subject
1435                       header
1436                       (gnus-simplify-subject
1437                        (mail-header-subject header) 're-only)))
1438                   gnus-newsgroup-headers))))")
1439
1440 (defvar gnus-select-article-hook nil
1441   "*A hook called when an article is selected.")
1442
1443 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1444   "*A hook called to apply kill files to a group.
1445 This hook is intended to apply a kill file to the selected newsgroup.
1446 The function `gnus-apply-kill-file' is called by default.
1447
1448 Since a general kill file is too heavy to use only for a few
1449 newsgroups, I recommend you to use a lighter hook function.  For
1450 example, if you'd like to apply a kill file to articles which contains
1451 a string `rmgroup' in subject in newsgroup `control', you can use the
1452 following hook:
1453
1454  (setq gnus-apply-kill-hook
1455       (list
1456         (lambda ()
1457           (cond ((string-match \"control\" gnus-newsgroup-name)
1458                  (gnus-kill \"Subject\" \"rmgroup\")
1459                  (gnus-expunge \"X\"))))))")
1460
1461 (defvar gnus-visual-mark-article-hook
1462   (list 'gnus-highlight-selected-summary)
1463   "*Hook run after selecting an article in the summary buffer.
1464 It is meant to be used for highlighting the article in some way.  It
1465 is not run if `gnus-visual' is nil.")
1466
1467 (defvar gnus-parse-headers-hook nil
1468   "*A hook called before parsing the headers.")
1469
1470 (defvar gnus-exit-group-hook nil
1471   "*A hook called when exiting (not quitting) summary mode.")
1472
1473 (defvar gnus-suspend-gnus-hook nil
1474   "*A hook called when suspending (not exiting) Gnus.")
1475
1476 (defvar gnus-exit-gnus-hook nil
1477   "*A hook called when exiting Gnus.")
1478
1479 (defvar gnus-after-exiting-gnus-hook nil
1480   "*A hook called after exiting Gnus.")
1481
1482 (defvar gnus-save-newsrc-hook nil
1483   "*A hook called before saving any of the newsrc files.")
1484
1485 (defvar gnus-save-quick-newsrc-hook nil
1486   "*A hook called just before saving the quick newsrc file.
1487 Can be used to turn version control on or off.")
1488
1489 (defvar gnus-save-standard-newsrc-hook nil
1490   "*A hook called just before saving the standard newsrc file.
1491 Can be used to turn version control on or off.")
1492
1493 (defvar gnus-summary-update-hook
1494   (list 'gnus-summary-highlight-line)
1495   "*A hook called when a summary line is changed.
1496 The hook will not be called if `gnus-visual' is nil.
1497
1498 The default function `gnus-summary-highlight-line' will
1499 highlight the line according to the `gnus-summary-highlight'
1500 variable.")
1501
1502 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1503   "*A hook called when a group line is changed.
1504 The hook will not be called if `gnus-visual' is nil.
1505
1506 The default function `gnus-group-highlight-line' will
1507 highlight the line according to the `gnus-group-highlight'
1508 variable.")
1509
1510 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1511   "*A hook called when an article is selected for the first time.
1512 The hook is intended to mark an article as read (or unread)
1513 automatically when it is selected.")
1514
1515 (defvar gnus-group-change-level-function nil
1516   "Function run when a group level is changed.
1517 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1518
1519 ;; Remove any hilit infestation.
1520 (add-hook 'gnus-startup-hook
1521           (lambda ()
1522             (remove-hook 'gnus-summary-prepare-hook
1523                          'hilit-rehighlight-buffer-quietly)
1524             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1525             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1526             (remove-hook 'gnus-article-prepare-hook
1527                          'hilit-rehighlight-buffer-quietly)))
1528
1529 \f
1530 ;; Internal variables
1531
1532 (defvar gnus-server-alist nil
1533   "List of available servers.")
1534
1535 (defvar gnus-group-indentation-function nil)
1536
1537 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1538
1539 (defvar gnus-goto-missing-group-function nil)
1540
1541 (defvar gnus-override-subscribe-method nil)
1542
1543 (defvar gnus-group-goto-next-group-function nil
1544   "Function to override finding the next group after listing groups.")
1545
1546 (defconst gnus-article-mark-lists
1547   '((marked . tick) (replied . reply)
1548     (expirable . expire) (killed . killed)
1549     (bookmarks . bookmark) (dormant . dormant)
1550     (scored . score) (saved . save)
1551     (cached . cache)
1552     ))
1553
1554 ;; Avoid highlighting in kill files.
1555 (defvar gnus-summary-inhibit-highlight nil)
1556 (defvar gnus-newsgroup-selected-overlay nil)
1557
1558 (defvar gnus-inhibit-hiding nil)
1559 (defvar gnus-group-indentation "")
1560 (defvar gnus-inhibit-limiting nil)
1561 (defvar gnus-created-frames nil)
1562
1563 (defvar gnus-article-mode-map nil)
1564 (defvar gnus-dribble-buffer nil)
1565 (defvar gnus-headers-retrieved-by nil)
1566 (defvar gnus-article-reply nil)
1567 (defvar gnus-override-method nil)
1568 (defvar gnus-article-check-size nil)
1569
1570 (defvar gnus-current-score-file nil)
1571 (defvar gnus-newsgroup-adaptive-score-file nil)
1572 (defvar gnus-scores-exclude-files nil)
1573
1574 (defvar gnus-opened-servers nil)
1575
1576 (defvar gnus-current-move-group nil)
1577
1578 (defvar gnus-newsgroup-dependencies nil)
1579 (defvar gnus-newsgroup-async nil)
1580 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1581
1582 (defvar gnus-newsgroup-adaptive nil)
1583
1584 (defvar gnus-summary-display-table nil)
1585 (defvar gnus-summary-display-article-function nil)
1586
1587 (defvar gnus-summary-highlight-line-function nil
1588   "Function called after highlighting a summary line.")
1589
1590 (defvar gnus-group-line-format-alist
1591   `((?M gnus-tmp-marked-mark ?c)
1592     (?S gnus-tmp-subscribed ?c)
1593     (?L gnus-tmp-level ?d)
1594     (?N (cond ((eq number t) "*" )
1595               ((numberp number) 
1596                (int-to-string
1597                 (+ number
1598                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1599                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1600               (t number)) ?s)
1601     (?R gnus-tmp-number-of-read ?s)
1602     (?t gnus-tmp-number-total ?d)
1603     (?y gnus-tmp-number-of-unread ?s)
1604     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1605     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1606     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1607            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1608     (?g gnus-tmp-group ?s)
1609     (?G gnus-tmp-qualified-group ?s)
1610     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1611     (?D gnus-tmp-newsgroup-description ?s)
1612     (?o gnus-tmp-moderated ?c)
1613     (?O gnus-tmp-moderated-string ?s)
1614     (?p gnus-tmp-process-marked ?c)
1615     (?s gnus-tmp-news-server ?s)
1616     (?n gnus-tmp-news-method ?s)
1617     (?P gnus-group-indentation ?s)
1618     (?l gnus-tmp-grouplens ?s)
1619     (?z gnus-tmp-news-method-string ?s)
1620     (?u gnus-tmp-user-defined ?s)))
1621
1622 (defvar gnus-summary-line-format-alist
1623   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1624     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1625     (?s gnus-tmp-subject-or-nil ?s)
1626     (?n gnus-tmp-name ?s)
1627     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1628         ?s)
1629     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1630             gnus-tmp-from) ?s)
1631     (?F gnus-tmp-from ?s)
1632     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1633     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1634     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1635     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1636     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1637     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1638     (?L gnus-tmp-lines ?d)
1639     (?I gnus-tmp-indentation ?s)
1640     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1641     (?R gnus-tmp-replied ?c)
1642     (?\[ gnus-tmp-opening-bracket ?c)
1643     (?\] gnus-tmp-closing-bracket ?c)
1644     (?\> (make-string gnus-tmp-level ? ) ?s)
1645     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1646     (?i gnus-tmp-score ?d)
1647     (?z gnus-tmp-score-char ?c)
1648     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1649     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1650     (?U gnus-tmp-unread ?c)
1651     (?t (gnus-summary-number-of-articles-in-thread
1652          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1653         ?d)
1654     (?e (gnus-summary-number-of-articles-in-thread
1655          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1656         ?c)
1657     (?u gnus-tmp-user-defined ?s))
1658   "An alist of format specifications that can appear in summary lines,
1659 and what variables they correspond with, along with the type of the
1660 variable (string, integer, character, etc).")
1661
1662 (defvar gnus-summary-dummy-line-format-alist
1663   `((?S gnus-tmp-subject ?s)
1664     (?N gnus-tmp-number ?d)
1665     (?u gnus-tmp-user-defined ?s)))
1666
1667 (defvar gnus-summary-mode-line-format-alist
1668   `((?G gnus-tmp-group-name ?s)
1669     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1670     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1671     (?A gnus-tmp-article-number ?d)
1672     (?Z gnus-tmp-unread-and-unselected ?s)
1673     (?V gnus-version ?s)
1674     (?U gnus-tmp-unread ?d)
1675     (?S gnus-tmp-subject ?s)
1676     (?e gnus-tmp-unselected ?d)
1677     (?u gnus-tmp-user-defined ?s)
1678     (?d (length gnus-newsgroup-dormant) ?d)
1679     (?t (length gnus-newsgroup-marked) ?d)
1680     (?r (length gnus-newsgroup-reads) ?d)
1681     (?E gnus-newsgroup-expunged-tally ?d)
1682     (?s (gnus-current-score-file-nondirectory) ?s)))
1683
1684 (defvar gnus-article-mode-line-format-alist
1685   gnus-summary-mode-line-format-alist)
1686
1687 (defvar gnus-group-mode-line-format-alist
1688   `((?S gnus-tmp-news-server ?s)
1689     (?M gnus-tmp-news-method ?s)
1690     (?u gnus-tmp-user-defined ?s)))
1691
1692 (defvar gnus-have-read-active-file nil)
1693
1694 (defconst gnus-maintainer
1695   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1696   "The mail address of the Gnus maintainers.")
1697
1698 (defconst gnus-version "September Gnus v0.73"
1699   "Version number for this version of Gnus.")
1700
1701 (defvar gnus-info-nodes
1702   '((gnus-group-mode            "(gnus)The Group Buffer")
1703     (gnus-summary-mode          "(gnus)The Summary Buffer")
1704     (gnus-article-mode          "(gnus)The Article Buffer"))
1705   "Assoc list of major modes and related Info nodes.")
1706
1707 (defvar gnus-group-buffer "*Group*")
1708 (defvar gnus-summary-buffer "*Summary*")
1709 (defvar gnus-article-buffer "*Article*")
1710 (defvar gnus-server-buffer "*Server*")
1711
1712 (defvar gnus-work-buffer " *gnus work*")
1713
1714 (defvar gnus-original-article-buffer " *Original Article*")
1715 (defvar gnus-original-article nil)
1716
1717 (defvar gnus-buffer-list nil
1718   "Gnus buffers that should be killed on exit.")
1719
1720 (defvar gnus-slave nil
1721   "Whether this Gnus is a slave or not.")
1722
1723 (defvar gnus-variable-list
1724   '(gnus-newsrc-options gnus-newsrc-options-n
1725     gnus-newsrc-last-checked-date
1726     gnus-newsrc-alist gnus-server-alist
1727     gnus-killed-list gnus-zombie-list
1728     gnus-topic-topology gnus-topic-alist
1729     gnus-format-specs)
1730   "Gnus variables saved in the quick startup file.")
1731
1732 (defvar gnus-newsrc-options nil
1733   "Options line in the .newsrc file.")
1734
1735 (defvar gnus-newsrc-options-n nil
1736   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1737
1738 (defvar gnus-newsrc-last-checked-date nil
1739   "Date Gnus last asked server for new newsgroups.")
1740
1741 (defvar gnus-topic-topology nil
1742   "The complete topic hierarchy.")
1743
1744 (defvar gnus-topic-alist nil
1745   "The complete topic-group alist.")
1746
1747 (defvar gnus-newsrc-alist nil
1748   "Assoc list of read articles.
1749 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1750
1751 (defvar gnus-newsrc-hashtb nil
1752   "Hashtable of gnus-newsrc-alist.")
1753
1754 (defvar gnus-killed-list nil
1755   "List of killed newsgroups.")
1756
1757 (defvar gnus-killed-hashtb nil
1758   "Hash table equivalent of gnus-killed-list.")
1759
1760 (defvar gnus-zombie-list nil
1761   "List of almost dead newsgroups.")
1762
1763 (defvar gnus-description-hashtb nil
1764   "Descriptions of newsgroups.")
1765
1766 (defvar gnus-list-of-killed-groups nil
1767   "List of newsgroups that have recently been killed by the user.")
1768
1769 (defvar gnus-active-hashtb nil
1770   "Hashtable of active articles.")
1771
1772 (defvar gnus-moderated-list nil
1773   "List of moderated newsgroups.")
1774
1775 (defvar gnus-group-marked nil)
1776
1777 (defvar gnus-current-startup-file nil
1778   "Startup file for the current host.")
1779
1780 (defvar gnus-last-search-regexp nil
1781   "Default regexp for article search command.")
1782
1783 (defvar gnus-last-shell-command nil
1784   "Default shell command on article.")
1785
1786 (defvar gnus-current-select-method nil
1787   "The current method for selecting a newsgroup.")
1788
1789 (defvar gnus-group-list-mode nil)
1790
1791 (defvar gnus-article-internal-prepare-hook nil)
1792
1793 (defvar gnus-newsgroup-name nil)
1794 (defvar gnus-newsgroup-begin nil)
1795 (defvar gnus-newsgroup-end nil)
1796 (defvar gnus-newsgroup-last-rmail nil)
1797 (defvar gnus-newsgroup-last-mail nil)
1798 (defvar gnus-newsgroup-last-folder nil)
1799 (defvar gnus-newsgroup-last-file nil)
1800 (defvar gnus-newsgroup-auto-expire nil)
1801 (defvar gnus-newsgroup-active nil)
1802
1803 (defvar gnus-newsgroup-data nil)
1804 (defvar gnus-newsgroup-data-reverse nil)
1805 (defvar gnus-newsgroup-limit nil)
1806 (defvar gnus-newsgroup-limits nil)
1807
1808 (defvar gnus-newsgroup-unreads nil
1809   "List of unread articles in the current newsgroup.")
1810
1811 (defvar gnus-newsgroup-unselected nil
1812   "List of unselected unread articles in the current newsgroup.")
1813
1814 (defvar gnus-newsgroup-reads nil
1815   "Alist of read articles and article marks in the current newsgroup.")
1816
1817 (defvar gnus-newsgroup-expunged-tally nil)
1818
1819 (defvar gnus-newsgroup-marked nil
1820   "List of ticked articles in the current newsgroup (a subset of unread art).")
1821
1822 (defvar gnus-newsgroup-killed nil
1823   "List of ranges of articles that have been through the scoring process.")
1824
1825 (defvar gnus-newsgroup-cached nil
1826   "List of articles that come from the article cache.")
1827
1828 (defvar gnus-newsgroup-saved nil
1829   "List of articles that have been saved.")
1830
1831 (defvar gnus-newsgroup-kill-headers nil)
1832
1833 (defvar gnus-newsgroup-replied nil
1834   "List of articles that have been replied to in the current newsgroup.")
1835
1836 (defvar gnus-newsgroup-expirable nil
1837   "List of articles in the current newsgroup that can be expired.")
1838
1839 (defvar gnus-newsgroup-processable nil
1840   "List of articles in the current newsgroup that can be processed.")
1841
1842 (defvar gnus-newsgroup-bookmarks nil
1843   "List of articles in the current newsgroup that have bookmarks.")
1844
1845 (defvar gnus-newsgroup-dormant nil
1846   "List of dormant articles in the current newsgroup.")
1847
1848 (defvar gnus-newsgroup-scored nil
1849   "List of scored articles in the current newsgroup.")
1850
1851 (defvar gnus-newsgroup-headers nil
1852   "List of article headers in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-threads nil)
1855
1856 (defvar gnus-newsgroup-prepared nil
1857   "Whether the current group has been prepared properly.")
1858
1859 (defvar gnus-newsgroup-ancient nil
1860   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1861
1862 (defvar gnus-newsgroup-sparse nil)
1863
1864 (defvar gnus-current-article nil)
1865 (defvar gnus-article-current nil)
1866 (defvar gnus-current-headers nil)
1867 (defvar gnus-have-all-headers nil)
1868 (defvar gnus-last-article nil)
1869 (defvar gnus-newsgroup-history nil)
1870 (defvar gnus-current-kill-article nil)
1871
1872 ;; Save window configuration.
1873 (defvar gnus-prev-winconf nil)
1874
1875 (defvar gnus-summary-mark-positions nil)
1876 (defvar gnus-group-mark-positions nil)
1877
1878 (defvar gnus-reffed-article-number nil)
1879
1880 ;;; Let the byte-compiler know that we know about this variable.
1881 (defvar rmail-default-rmail-file)
1882
1883 (defvar gnus-cache-removable-articles nil)
1884
1885 (defvar gnus-dead-summary nil)
1886
1887 (defconst gnus-summary-local-variables
1888   '(gnus-newsgroup-name
1889     gnus-newsgroup-begin gnus-newsgroup-end
1890     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1891     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1892     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1893     gnus-newsgroup-unselected gnus-newsgroup-marked
1894     gnus-newsgroup-reads gnus-newsgroup-saved
1895     gnus-newsgroup-replied gnus-newsgroup-expirable
1896     gnus-newsgroup-processable gnus-newsgroup-killed
1897     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1898     gnus-newsgroup-headers gnus-newsgroup-threads
1899     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1900     gnus-current-article gnus-current-headers gnus-have-all-headers
1901     gnus-last-article gnus-article-internal-prepare-hook
1902     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1903     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1904     gnus-newsgroup-async gnus-thread-expunge-below
1905     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1906     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1907     gnus-newsgroup-history gnus-newsgroup-ancient
1908     gnus-newsgroup-sparse
1909     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1910     gnus-newsgroup-adaptive-score-file
1911     (gnus-newsgroup-expunged-tally . 0)
1912     gnus-cache-removable-articles gnus-newsgroup-cached
1913     gnus-newsgroup-data gnus-newsgroup-data-reverse
1914     gnus-newsgroup-limit gnus-newsgroup-limits)
1915   "Variables that are buffer-local to the summary buffers.")
1916
1917 (defconst gnus-bug-message
1918   "Sending a bug report to the Gnus Towers.
1919 ========================================
1920
1921 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1922 be sent to the Gnus Bug Exterminators.
1923
1924 At the bottom of the buffer you'll see lots of variable settings.
1925 Please do not delete those.  They will tell the Bug People what your
1926 environment is, so that it will be easier to locate the bugs.
1927
1928 If you have found a bug that makes Emacs go \"beep\", set
1929 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1930 and include the backtrace in your bug report.
1931
1932 Please describe the bug in annoying, painstaking detail.
1933
1934 Thank you for your help in stamping out bugs.
1935 ")
1936
1937 ;;; End of variables.
1938
1939 ;; Define some autoload functions Gnus might use.
1940 (eval-and-compile
1941
1942   ;; This little mapcar goes through the list below and marks the
1943   ;; symbols in question as autoloaded functions.
1944   (mapcar
1945    (lambda (package)
1946      (let ((interactive (nth 1 (memq ':interactive package))))
1947        (mapcar
1948         (lambda (function)
1949           (let (keymap)
1950             (when (consp function)
1951               (setq keymap (car (memq 'keymap function)))
1952               (setq function (car function)))
1953             (autoload function (car package) nil interactive keymap)))
1954         (if (eq (nth 1 package) ':interactive)
1955             (cdddr package)
1956           (cdr package)))))
1957    '(("metamail" metamail-buffer)
1958      ("info" Info-goto-node)
1959      ("hexl" hexl-hex-string-to-integer)
1960      ("pp" pp pp-to-string pp-eval-expression)
1961      ("mail-extr" mail-extract-address-components)
1962      ("nnmail" nnmail-split-fancy nnmail-article-group)
1963      ("nnvirtual" nnvirtual-catchup-group)
1964      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1965       timezone-make-sortable-date timezone-make-time-string)
1966      ("sendmail" mail-position-on-field mail-setup)
1967      ("rmailout" rmail-output)
1968      ("rnewspost" news-mail-other-window news-reply-yank-original
1969       news-caesar-buffer-body)
1970      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1971       rmail-show-message)
1972      ("gnus-soup" :interactive t
1973       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1974       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1975      ("nnsoup" nnsoup-pack-replies)
1976      ("gnus-scomo" :interactive t gnus-score-mode)
1977      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1978       gnus-Folder-save-name gnus-folder-save-name)
1979      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1980      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1981       gnus-server-make-menu-bar gnus-article-make-menu-bar
1982       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1983       gnus-summary-highlight-line gnus-carpal-setup-buffer
1984       gnus-group-highlight-line
1985       gnus-article-add-button gnus-insert-next-page-button
1986       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1987      ("gnus-vis" :interactive t
1988       gnus-article-push-button gnus-article-press-button
1989       gnus-article-highlight gnus-article-highlight-some
1990       gnus-article-highlight-headers gnus-article-highlight-signature
1991       gnus-article-add-buttons gnus-article-add-buttons-to-head
1992       gnus-article-next-button gnus-article-prev-button)
1993      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1994       gnus-demon-add-disconnection gnus-demon-add-handler
1995       gnus-demon-remove-handler)
1996      ("gnus-demon" :interactive t
1997       gnus-demon-init gnus-demon-cancel)
1998      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1999       gnus-tree-open gnus-tree-close)
2000      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2001       gnus-nocem-unwanted-article-p)
2002      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2003      ("gnus-srvr" gnus-browse-foreign-server)
2004      ("gnus-cite" :interactive t
2005       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2006       gnus-article-hide-citation gnus-article-fill-cited-article)
2007      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2008       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2009       gnus-execute gnus-expunge)
2010      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2011       gnus-cache-possibly-remove-articles gnus-cache-request-article
2012       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2013       gnus-cache-enter-remove-article gnus-cached-article-p
2014       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2015      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2016       gnus-cache-remove-article)
2017      ("gnus-score" :interactive t
2018       gnus-summary-increase-score gnus-summary-lower-score
2019       gnus-score-flush-cache gnus-score-close
2020       gnus-score-raise-same-subject-and-select
2021       gnus-score-raise-same-subject gnus-score-default
2022       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2023       gnus-score-lower-same-subject gnus-score-lower-thread
2024       gnus-possibly-score-headers)
2025      ("gnus-score"
2026       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2027       gnus-current-score-file-nondirectory gnus-score-adaptive
2028       gnus-score-find-trace gnus-score-file-name)
2029      ("gnus-edit" :interactive t gnus-score-customize)
2030      ("gnus-topic" :interactive t gnus-topic-mode)
2031      ("gnus-topic" gnus-topic-remove-group)
2032      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2033      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2034      ("gnus-uu" :interactive t
2035       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2036       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2037       gnus-uu-mark-by-regexp gnus-uu-mark-all
2038       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2039       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2040       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2041       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2042       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2043       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2044       gnus-uu-decode-binhex-view)
2045      ("gnus-msg" (gnus-summary-send-map keymap)
2046       gnus-mail-yank-original gnus-mail-send-and-exit
2047       gnus-sendmail-setup-mail gnus-article-mail
2048       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2049      ("gnus-msg" :interactive t
2050       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2051       gnus-summary-followup gnus-summary-followup-with-original
2052       gnus-summary-followup-and-reply
2053       gnus-summary-followup-and-reply-with-original
2054       gnus-summary-cancel-article gnus-summary-supersede-article
2055       gnus-post-news gnus-inews-news gnus-cancel-news
2056       gnus-summary-reply gnus-summary-reply-with-original
2057       gnus-summary-mail-forward gnus-summary-mail-other-window
2058       gnus-bug)
2059      ("gnus-picon" :interactive t gnus-article-display-picons
2060       gnus-group-display-picons gnus-picons-article-display-x-face)
2061      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2062       gnus-grouplens-mode)
2063      ("gnus-vm" gnus-vm-mail-setup)
2064      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2065       gnus-summary-save-article-vm gnus-yank-article))))
2066
2067 \f
2068
2069 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2070 ;; If you want the cursor to go somewhere else, set these two
2071 ;; functions in some startup hook to whatever you want.
2072 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2073 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2074
2075 ;;; Various macros and substs.
2076
2077 (defun gnus-header-from (header)
2078   (mail-header-from header))
2079
2080 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2081   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2082   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2083     `(let ((,tempvar (selected-window)))
2084        (unwind-protect
2085            (progn
2086              (pop-to-buffer ,buffer)
2087              ,@forms)
2088          (select-window ,tempvar)))))
2089
2090 (defmacro gnus-gethash (string hashtable)
2091   "Get hash value of STRING in HASHTABLE."
2092   `(symbol-value (intern-soft ,string ,hashtable)))
2093
2094 (defmacro gnus-sethash (string value hashtable)
2095   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2096   `(set (intern ,string ,hashtable) ,value))
2097
2098 (defmacro gnus-intern-safe (string hashtable)
2099   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2100   `(let ((symbol (intern ,string ,hashtable)))
2101      (or (boundp symbol)
2102          (set symbol nil))
2103      symbol))
2104
2105 (defmacro gnus-group-unread (group)
2106   "Get the currently computed number of unread articles in GROUP."
2107   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2108
2109 (defmacro gnus-group-entry (group)
2110   "Get the newsrc entry for GROUP."
2111   `(gnus-gethash ,group gnus-newsrc-hashtb))
2112
2113 (defmacro gnus-active (group)
2114   "Get active info on GROUP."
2115   `(gnus-gethash ,group gnus-active-hashtb))
2116
2117 (defmacro gnus-set-active (group active)
2118   "Set GROUP's active info."
2119   `(gnus-sethash ,group ,active gnus-active-hashtb))
2120
2121 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2122 ;;   function `substring' might cut on a middle of multi-octet
2123 ;;   character.
2124 (defun gnus-truncate-string (str width)
2125   (substring str 0 width))
2126
2127 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2128 ;; to limit the length of a string.  This function is necessary since
2129 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2130 (defsubst gnus-limit-string (str width)
2131   (if (> (length str) width)
2132       (substring str 0 width)
2133     str))
2134
2135 (defsubst gnus-simplify-subject-re (subject)
2136   "Remove \"Re:\" from subject lines."
2137   (if (string-match "^[Rr][Ee]: *" subject)
2138       (substring subject (match-end 0))
2139     subject))
2140
2141 (defsubst gnus-functionp (form)
2142   "Return non-nil if FORM is funcallable."
2143   (or (and (symbolp form) (fboundp form))
2144       (and (listp form) (eq (car form) 'lambda))))
2145
2146 (defsubst gnus-goto-char (point)
2147   (and point (goto-char point)))
2148
2149 (defmacro gnus-buffer-exists-p (buffer)
2150   `(let ((buffer ,buffer))
2151      (and buffer
2152           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2153                    buffer))))
2154
2155 (defmacro gnus-kill-buffer (buffer)
2156   `(let ((buf ,buffer))
2157      (if (gnus-buffer-exists-p buf)
2158          (kill-buffer buf))))
2159
2160 (defsubst gnus-point-at-bol ()
2161   "Return point at the beginning of the line."
2162   (let ((p (point)))
2163     (beginning-of-line)
2164     (prog1
2165         (point)
2166       (goto-char p))))
2167
2168 (defsubst gnus-point-at-eol ()
2169   "Return point at the end of the line."
2170   (let ((p (point)))
2171     (end-of-line)
2172     (prog1
2173         (point)
2174       (goto-char p))))
2175
2176 (defun gnus-alive-p ()
2177   "Say whether Gnus is running or not."
2178   (and gnus-group-buffer
2179        (get-buffer gnus-group-buffer)))
2180
2181 ;; Delete the current line (and the next N lines.);
2182 (defmacro gnus-delete-line (&optional n)
2183   `(delete-region (progn (beginning-of-line) (point))
2184                   (progn (forward-line ,(or n 1)) (point))))
2185
2186 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2187 (defvar gnus-init-inhibit nil)
2188 (defun gnus-read-init-file (&optional inhibit-next)
2189   (if gnus-init-inhibit
2190       (setq gnus-init-inhibit nil)
2191     (setq gnus-init-inhibit inhibit-next)
2192     (and gnus-init-file
2193          (or (and (file-exists-p gnus-init-file)
2194                   ;; Don't try to load a directory.
2195                   (not (file-directory-p gnus-init-file)))
2196              (file-exists-p (concat gnus-init-file ".el"))
2197              (file-exists-p (concat gnus-init-file ".elc")))
2198          (condition-case var
2199              (load gnus-init-file nil t)
2200            (error
2201             (error "Error in %s: %s" gnus-init-file var))))))
2202
2203 ;; Info access macros.
2204
2205 (defmacro gnus-info-group (info)
2206   `(nth 0 ,info))
2207 (defmacro gnus-info-rank (info)
2208   `(nth 1 ,info))
2209 (defmacro gnus-info-read (info)
2210   `(nth 2 ,info))
2211 (defmacro gnus-info-marks (info)
2212   `(nth 3 ,info))
2213 (defmacro gnus-info-method (info)
2214   `(nth 4 ,info))
2215 (defmacro gnus-info-params (info)
2216   `(nth 5 ,info))
2217
2218 (defmacro gnus-info-level (info)
2219   `(let ((rank (gnus-info-rank ,info)))
2220      (if (consp rank)
2221          (car rank)
2222        rank)))
2223 (defmacro gnus-info-score (info)
2224   `(let ((rank (gnus-info-rank ,info)))
2225      (or (and (consp rank) (cdr rank)) 0)))
2226
2227 (defmacro gnus-info-set-group (info group)
2228   `(setcar ,info ,group))
2229 (defmacro gnus-info-set-rank (info rank)
2230   `(setcar (nthcdr 1 ,info) ,rank))
2231 (defmacro gnus-info-set-read (info read)
2232   `(setcar (nthcdr 2 ,info) ,read))
2233 (defmacro gnus-info-set-marks (info marks)
2234   `(setcar (nthcdr 3 ,info) ,marks))
2235 (defmacro gnus-info-set-method (info method)
2236   `(setcar (nthcdr 4 ,info) ,method))
2237 (defmacro gnus-info-set-params (info params)
2238   `(setcar (nthcdr 5 ,info) ,params))
2239
2240 (defmacro gnus-info-set-level (info level)
2241   `(let ((rank (cdr ,info)))
2242      (if (consp (car rank))
2243          (setcar (car rank) ,level)
2244        (setcar rank ,level))))
2245 (defmacro gnus-info-set-score (info score)
2246   `(let ((rank (cdr ,info)))
2247      (if (consp (car rank))
2248          (setcdr (car rank) ,score)
2249        (setcar rank (cons (car rank) ,score)))))
2250
2251 (defmacro gnus-get-info (group)
2252   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2253
2254 (defun gnus-byte-code (func)
2255   "Return a form that can be `eval'ed based on FUNC."
2256   (let ((fval (symbol-function func)))
2257     (if (byte-code-function-p fval)
2258         (let ((flist (append fval nil)))
2259           (setcar flist 'byte-code)
2260           flist)
2261       (cons 'progn (cddr fval)))))
2262
2263 ;;; Load the compatability functions.
2264
2265 (require 'gnus-cus)
2266 (require 'gnus-ems)
2267
2268 \f
2269 ;;;
2270 ;;; Shutdown
2271 ;;;
2272
2273 (defvar gnus-shutdown-alist nil)
2274
2275 (defun gnus-add-shutdown (function &rest symbols)
2276   "Run FUNCTION whenever one of SYMBOLS is shut down."
2277   (push (cons function symbols) gnus-shutdown-alist))
2278
2279 (defun gnus-shutdown (symbol)
2280   "Shut down everything that waits for SYMBOL."
2281   (let ((alist gnus-shutdown-alist)
2282         entry)
2283     (while (setq entry (pop alist))
2284       (when (memq symbol (cdr entry))
2285         (funcall (car entry))))))
2286
2287 \f
2288
2289 ;; Format specs.  The chunks below are the machine-generated forms
2290 ;; that are to be evaled as the result of the default format strings.
2291 ;; We write them in here to get them byte-compiled.  That way the
2292 ;; default actions will be quite fast, while still retaining the full
2293 ;; flexibility of the user-defined format specs.
2294
2295 ;; First we have lots of dummy defvars to let the compiler know these
2296 ;; are really dynamic variables.
2297
2298 (defvar gnus-tmp-unread)
2299 (defvar gnus-tmp-replied)
2300 (defvar gnus-tmp-score-char)
2301 (defvar gnus-tmp-indentation)
2302 (defvar gnus-tmp-opening-bracket)
2303 (defvar gnus-tmp-lines)
2304 (defvar gnus-tmp-name)
2305 (defvar gnus-tmp-closing-bracket)
2306 (defvar gnus-tmp-subject-or-nil)
2307 (defvar gnus-tmp-subject)
2308 (defvar gnus-tmp-marked)
2309 (defvar gnus-tmp-marked-mark)
2310 (defvar gnus-tmp-subscribed)
2311 (defvar gnus-tmp-process-marked)
2312 (defvar gnus-tmp-number-of-unread)
2313 (defvar gnus-tmp-group-name)
2314 (defvar gnus-tmp-group)
2315 (defvar gnus-tmp-article-number)
2316 (defvar gnus-tmp-unread-and-unselected)
2317 (defvar gnus-tmp-news-method)
2318 (defvar gnus-tmp-news-server)
2319 (defvar gnus-tmp-article-number)
2320 (defvar gnus-mouse-face)
2321 (defvar gnus-mouse-face-prop)
2322
2323 (defun gnus-summary-line-format-spec ()
2324   (insert gnus-tmp-unread gnus-tmp-replied
2325           gnus-tmp-score-char gnus-tmp-indentation)
2326   (put-text-property
2327    (point)
2328    (progn
2329      (insert
2330       gnus-tmp-opening-bracket
2331       (format "%4d: %-20s"
2332               gnus-tmp-lines
2333               (if (> (length gnus-tmp-name) 20)
2334                   (substring gnus-tmp-name 0 20)
2335                 gnus-tmp-name))
2336       gnus-tmp-closing-bracket)
2337      (point))
2338    gnus-mouse-face-prop gnus-mouse-face)
2339   (insert " " gnus-tmp-subject-or-nil "\n"))
2340
2341 (defvar gnus-summary-line-format-spec
2342   (gnus-byte-code 'gnus-summary-line-format-spec))
2343
2344 (defun gnus-summary-dummy-line-format-spec ()
2345   (insert "*  ")
2346   (put-text-property
2347    (point)
2348    (progn
2349      (insert ":                          :")
2350      (point))
2351    gnus-mouse-face-prop gnus-mouse-face)
2352   (insert " " gnus-tmp-subject "\n"))
2353
2354 (defvar gnus-summary-dummy-line-format-spec
2355   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2356
2357 (defun gnus-group-line-format-spec ()
2358   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2359           gnus-tmp-process-marked
2360           gnus-group-indentation
2361           (format "%5s: " gnus-tmp-number-of-unread))
2362   (put-text-property
2363    (point)
2364    (progn
2365      (insert gnus-tmp-group "\n")
2366      (1- (point)))
2367    gnus-mouse-face-prop gnus-mouse-face))
2368 (defvar gnus-group-line-format-spec
2369   (gnus-byte-code 'gnus-group-line-format-spec))
2370
2371 (defvar gnus-format-specs
2372   `((version . ,emacs-version)
2373     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2374     (summary-dummy ,gnus-summary-dummy-line-format
2375                    ,gnus-summary-dummy-line-format-spec)
2376     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2377
2378 (defvar gnus-article-mode-line-format-spec nil)
2379 (defvar gnus-summary-mode-line-format-spec nil)
2380 (defvar gnus-group-mode-line-format-spec nil)
2381
2382 ;;; Phew.  All that gruft is over, fortunately.
2383
2384 \f
2385 ;;;
2386 ;;; Gnus Utility Functions
2387 ;;;
2388
2389 (defun gnus-extract-address-components (from)
2390   (let (name address)
2391     ;; First find the address - the thing with the @ in it.  This may
2392     ;; not be accurate in mail addresses, but does the trick most of
2393     ;; the time in news messages.
2394     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2395         (setq address (substring from (match-beginning 0) (match-end 0))))
2396     ;; Then we check whether the "name <address>" format is used.
2397     (and address
2398          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2399          ;; Linear white space is not required.
2400          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2401          (and (setq name (substring from 0 (match-beginning 0)))
2402               ;; Strip any quotes from the name.
2403               (string-match "\".*\"" name)
2404               (setq name (substring name 1 (1- (match-end 0))))))
2405     ;; If not, then "address (name)" is used.
2406     (or name
2407         (and (string-match "(.+)" from)
2408              (setq name (substring from (1+ (match-beginning 0))
2409                                    (1- (match-end 0)))))
2410         (and (string-match "()" from)
2411              (setq name address))
2412         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2413         ;; XOVER might not support folded From headers.
2414         (and (string-match "(.*" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (match-end 0)))))
2417     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2418     (list (or name from) (or address from))))
2419
2420 (defun gnus-fetch-field (field)
2421   "Return the value of the header FIELD of current article."
2422   (save-excursion
2423     (save-restriction
2424       (let ((case-fold-search t)
2425             (inhibit-point-motion-hooks t))
2426         (nnheader-narrow-to-headers)
2427         (mail-fetch-field field)))))
2428
2429 (defun gnus-goto-colon ()
2430   (beginning-of-line)
2431   (search-forward ":" (gnus-point-at-eol) t))
2432
2433 ;;;###autoload
2434 (defun gnus-update-format (var)
2435   "Update the format specification near point."
2436   (interactive
2437    (list
2438     (save-excursion
2439       (eval-defun nil)
2440       ;; Find the end of the current word.
2441       (re-search-forward "[ \t\n]" nil t)
2442       ;; Search backward.
2443       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2444         (match-string 1)))))
2445   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2446                               (match-string 1 var))))
2447          (entry (assq type gnus-format-specs))
2448          value spec)
2449     (when entry
2450       (setq gnus-format-specs (delq entry gnus-format-specs)))
2451     (set
2452      (intern (format "%s-spec" var))
2453      (gnus-parse-format (setq value (symbol-value (intern var)))
2454                         (symbol-value (intern (format "%s-alist" var)))
2455                         (not (string-match "mode" var))))
2456     (setq spec (symbol-value (intern (format "%s-spec" var))))
2457     (push (list type value spec) gnus-format-specs)
2458
2459     (pop-to-buffer "*Gnus Format*")
2460     (erase-buffer)
2461     (lisp-interaction-mode)
2462     (insert (pp-to-string spec))))
2463
2464 (defun gnus-update-format-specifications (&optional force)
2465   "Update all (necessary) format specifications."
2466   ;; Make the indentation array.
2467   (gnus-make-thread-indent-array)
2468
2469   ;; See whether all the stored info needs to be flushed.
2470   (when (or force
2471             (not (equal emacs-version
2472                         (cdr (assq 'version gnus-format-specs)))))
2473     (setq gnus-format-specs nil))
2474
2475   ;; Go through all the formats and see whether they need updating.
2476   (let ((types '(summary summary-dummy group
2477                          summary-mode group-mode article-mode))
2478         new-format entry type val)
2479     (while (setq type (pop types))
2480       ;; Jump to the proper buffer to find out the value of
2481       ;; the variable, if possible.  (It may be buffer-local.)
2482       (save-excursion
2483         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2484               val)
2485           (when (and (boundp buffer)
2486                      (setq val (symbol-value buffer))
2487                      (get-buffer val)
2488                      (buffer-name (get-buffer val)))
2489             (set-buffer (get-buffer val)))
2490           (setq new-format (symbol-value
2491                             (intern (format "gnus-%s-line-format" type))))))
2492       (setq entry (cdr (assq type gnus-format-specs)))
2493       (if (and entry
2494                (equal (car entry) new-format))
2495           ;; Use the old format.
2496           (set (intern (format "gnus-%s-line-format-spec" type))
2497                (cadr entry))
2498         ;; This is a new format.
2499         (setq val
2500               (if (not (stringp new-format))
2501                   ;; This is a function call or something.
2502                   new-format
2503                 ;; This is a "real" format.
2504                 (gnus-parse-format
2505                  new-format
2506                  (symbol-value
2507                   (intern (format "gnus-%s-line-format-alist"
2508                                   (if (eq type 'article-mode)
2509                                       'summary-mode type))))
2510                  (not (string-match "mode$" (symbol-name type))))))
2511         ;; Enter the new format spec into the list.
2512         (if entry
2513             (progn
2514               (setcar (cdr entry) val)
2515               (setcar entry new-format))
2516           (push (list type new-format val) gnus-format-specs))
2517         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2518
2519   (unless (assq 'version gnus-format-specs)
2520     (push (cons 'version emacs-version) gnus-format-specs))
2521
2522   (gnus-update-group-mark-positions)
2523   (gnus-update-summary-mark-positions))
2524
2525 (defun gnus-update-summary-mark-positions ()
2526   "Compute where the summary marks are to go."
2527   (save-excursion
2528     (let ((gnus-replied-mark 129)
2529           (gnus-score-below-mark 130)
2530           (gnus-score-over-mark 130)
2531           (thread nil)
2532           (gnus-visual nil)
2533           pos)
2534       (gnus-set-work-buffer)
2535       (gnus-summary-insert-line
2536        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2537       (goto-char (point-min))
2538       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2539                                          (- (point) 2)))))
2540       (goto-char (point-min))
2541       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2542             pos)
2543       (goto-char (point-min))
2544       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2545             pos)
2546       (setq gnus-summary-mark-positions pos))))
2547
2548 (defun gnus-update-group-mark-positions ()
2549   (save-excursion
2550     (let ((gnus-process-mark 128)
2551           (gnus-group-marked '("dummy.group")))
2552       (gnus-set-active "dummy.group" '(0 . 0))
2553       (gnus-set-work-buffer)
2554       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2555       (goto-char (point-min))
2556       (setq gnus-group-mark-positions
2557             (list (cons 'process (and (search-forward "\200" nil t)
2558                                       (- (point) 2))))))))
2559
2560 (defvar gnus-mouse-face-0 'highlight)
2561 (defvar gnus-mouse-face-1 'highlight)
2562 (defvar gnus-mouse-face-2 'highlight)
2563 (defvar gnus-mouse-face-3 'highlight)
2564 (defvar gnus-mouse-face-4 'highlight)
2565
2566 (defun gnus-mouse-face-function (form type)
2567   `(put-text-property
2568     (point) (progn ,@form (point))
2569     gnus-mouse-face-prop
2570     ,(if (equal type 0)
2571          'gnus-mouse-face
2572        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2573
2574 (defvar gnus-face-0 'bold)
2575 (defvar gnus-face-1 'italic)
2576 (defvar gnus-face-2 'bold-italic)
2577 (defvar gnus-face-3 'bold)
2578 (defvar gnus-face-4 'bold)
2579
2580 (defun gnus-face-face-function (form type)
2581   `(put-text-property
2582     (point) (progn ,@form (point))
2583     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2584
2585 (defun gnus-max-width-function (el max-width)
2586   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2587   (if (symbolp el)
2588       `(if (> (length ,el) ,max-width)
2589            (substring ,el 0 ,max-width)
2590          ,el)
2591     `(let ((val (eval ,el)))
2592        (if (numberp val)
2593            (setq val (int-to-string val)))
2594        (if (> (length val) ,max-width)
2595            (substring val 0 ,max-width)
2596          val))))
2597
2598 (defun gnus-parse-format (format spec-alist &optional insert)
2599   ;; This function parses the FORMAT string with the help of the
2600   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2601   ;; string.  If the FORMAT string contains the specifiers %( and %)
2602   ;; the text between them will have the mouse-face text property.
2603   (if (string-match
2604        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2605        format)
2606       (gnus-parse-complex-format format spec-alist)
2607     ;; This is a simple format.
2608     (gnus-parse-simple-format format spec-alist insert)))
2609
2610 (defun gnus-parse-complex-format (format spec-alist)
2611   (save-excursion
2612     (gnus-set-work-buffer)
2613     (insert format)
2614     (goto-char (point-min))
2615     (while (re-search-forward "\"" nil t)
2616       (replace-match "\\\"" nil t))
2617     (goto-char (point-min))
2618     (insert "(\"")
2619     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2620       (let ((number (if (match-beginning 1)
2621                         (match-string 1) "0"))
2622             (delim (aref (match-string 2) 0)))
2623         (if (or (= delim ?\() (= delim ?\{))
2624             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2625                                    " " number " \""))
2626           (replace-match "\")\""))))
2627     (goto-char (point-max))
2628     (insert "\")")
2629     (goto-char (point-min))
2630     (let ((form (read (current-buffer))))
2631       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2632
2633 (defun gnus-complex-form-to-spec (form spec-alist)
2634   (delq nil
2635         (mapcar
2636          (lambda (sform)
2637            (if (stringp sform)
2638                (gnus-parse-simple-format sform spec-alist t)
2639              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2640                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2641                       (nth 1 sform))))
2642          form)))
2643
2644 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2645   ;; This function parses the FORMAT string with the help of the
2646   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2647   ;; string.
2648   (let ((max-width 0)
2649         spec flist fstring newspec elem beg result dontinsert)
2650     (save-excursion
2651       (gnus-set-work-buffer)
2652       (insert format)
2653       (goto-char (point-min))
2654       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2655                                 nil t)
2656         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2657               (setq newspec "%"
2658                     beg (1+ (match-beginning 0)))
2659           ;; First check if there are any specs that look anything like
2660           ;; "%12,12A", ie. with a "max width specification".  These have
2661           ;; to be treated specially.
2662           (if (setq beg (match-beginning 1))
2663               (setq max-width
2664                     (string-to-int
2665                      (buffer-substring
2666                       (1+ (match-beginning 1)) (match-end 1))))
2667             (setq max-width 0)
2668             (setq beg (match-beginning 2)))
2669           ;; Find the specification from `spec-alist'.
2670           (unless (setq elem (cdr (assq spec spec-alist)))
2671             (setq elem '("*" ?s)))
2672           ;; Treat user defined format specifiers specially.
2673           (when (eq (car elem) 'gnus-tmp-user-defined)
2674             (setq elem
2675                   (list
2676                    (list (intern (concat "gnus-user-format-function-"
2677                                          (match-string 3)))
2678                          'gnus-tmp-header) ?s))
2679             (delete-region (match-beginning 3) (match-end 3)))
2680           (if (not (zerop max-width))
2681               (let ((el (car elem)))
2682                 (cond ((= (cadr elem) ?c)
2683                        (setq el (list 'char-to-string el)))
2684                       ((= (cadr elem) ?d)
2685                        (setq el (list 'int-to-string el))))
2686                 (setq flist (cons (gnus-max-width-function el max-width)
2687                                   flist))
2688                 (setq newspec ?s))
2689             (progn
2690               (setq flist (cons (car elem) flist))
2691               (setq newspec (cadr elem)))))
2692         ;; Remove the old specification (and possibly a ",12" string).
2693         (delete-region beg (match-end 2))
2694         ;; Insert the new specification.
2695         (goto-char beg)
2696         (insert newspec))
2697       (setq fstring (buffer-substring 1 (point-max))))
2698     ;; Do some postprocessing to increase efficiency.
2699     (setq
2700      result
2701      (cond
2702       ;; Emptyness.
2703       ((string= fstring "")
2704        nil)
2705       ;; Not a format string.
2706       ((not (string-match "%" fstring))
2707        (list fstring))
2708       ;; A format string with just a single string spec.
2709       ((string= fstring "%s")
2710        (list (car flist)))
2711       ;; A single character.
2712       ((string= fstring "%c")
2713        (list (car flist)))
2714       ;; A single number.
2715       ((string= fstring "%d")
2716        (setq dontinsert)
2717        (if insert
2718            (list `(princ ,(car flist)))
2719          (list `(int-to-string ,(car flist)))))
2720       ;; Just lots of chars and strings.
2721       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2722        (nreverse flist))
2723       ;; A single string spec at the beginning of the spec.
2724       ((string-match "\\`%[sc][^%]+\\'" fstring)
2725        (list (car flist) (substring fstring 2)))
2726       ;; A single string spec in the middle of the spec.
2727       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2728        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2729       ;; A single string spec in the end of the spec.
2730       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2731        (list (match-string 1 fstring) (car flist)))
2732       ;; A more complex spec.
2733       (t
2734        (list (cons 'format (cons fstring (nreverse flist)))))))
2735
2736     (if insert
2737         (when result
2738           (if dontinsert
2739               result
2740             (cons 'insert result)))
2741       (cond ((stringp result)
2742              result)
2743             ((consp result)
2744              (cons 'concat result))
2745             (t "")))))
2746
2747 (defun gnus-eval-format (format &optional alist props)
2748   "Eval the format variable FORMAT, using ALIST.
2749 If PROPS, insert the result."
2750   (let ((form (gnus-parse-format format alist props)))
2751     (if props
2752         (add-text-properties (point) (progn (eval form) (point)) props)
2753       (eval form))))
2754
2755 (defun gnus-remove-text-with-property (prop)
2756   "Delete all text in the current buffer with text property PROP."
2757   (save-excursion
2758     (goto-char (point-min))
2759     (while (not (eobp))
2760       (while (get-text-property (point) prop)
2761         (delete-char 1))
2762       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2763
2764 (defun gnus-set-work-buffer ()
2765   (if (get-buffer gnus-work-buffer)
2766       (progn
2767         (set-buffer gnus-work-buffer)
2768         (erase-buffer))
2769     (set-buffer (get-buffer-create gnus-work-buffer))
2770     (kill-all-local-variables)
2771     (buffer-disable-undo (current-buffer))
2772     (gnus-add-current-to-buffer-list)))
2773
2774 ;; Article file names when saving.
2775
2776 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2777   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2778 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2779 Otherwise, it is like ~/News/news/group/num."
2780   (let ((default
2781           (expand-file-name
2782            (concat (if (gnus-use-long-file-name 'not-save)
2783                        (gnus-capitalize-newsgroup newsgroup)
2784                      (gnus-newsgroup-directory-form newsgroup))
2785                    "/" (int-to-string (mail-header-number headers)))
2786            (or gnus-article-save-directory "~/News"))))
2787     (if (and last-file
2788              (string-equal (file-name-directory default)
2789                            (file-name-directory last-file))
2790              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2791         default
2792       (or last-file default))))
2793
2794 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2795   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2796 If variable `gnus-use-long-file-name' is non-nil, it is
2797 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2798   (let ((default
2799           (expand-file-name
2800            (concat (if (gnus-use-long-file-name 'not-save)
2801                        newsgroup
2802                      (gnus-newsgroup-directory-form newsgroup))
2803                    "/" (int-to-string (mail-header-number headers)))
2804            (or gnus-article-save-directory "~/News"))))
2805     (if (and last-file
2806              (string-equal (file-name-directory default)
2807                            (file-name-directory last-file))
2808              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2809         default
2810       (or last-file default))))
2811
2812 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2813   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2814 If variable `gnus-use-long-file-name' is non-nil, it is
2815 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2816   (or last-file
2817       (expand-file-name
2818        (if (gnus-use-long-file-name 'not-save)
2819            (gnus-capitalize-newsgroup newsgroup)
2820          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2821        (or gnus-article-save-directory "~/News"))))
2822
2823 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2824   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2825 If variable `gnus-use-long-file-name' is non-nil, it is
2826 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2827   (or last-file
2828       (expand-file-name
2829        (if (gnus-use-long-file-name 'not-save)
2830            newsgroup
2831          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2832        (or gnus-article-save-directory "~/News"))))
2833
2834 ;; For subscribing new newsgroup
2835
2836 (defun gnus-subscribe-hierarchical-interactive (groups)
2837   (let ((groups (sort groups 'string<))
2838         prefixes prefix start ans group starts)
2839     (while groups
2840       (setq prefixes (list "^"))
2841       (while (and groups prefixes)
2842         (while (not (string-match (car prefixes) (car groups)))
2843           (setq prefixes (cdr prefixes)))
2844         (setq prefix (car prefixes))
2845         (setq start (1- (length prefix)))
2846         (if (and (string-match "[^\\.]\\." (car groups) start)
2847                  (cdr groups)
2848                  (setq prefix
2849                        (concat "^" (substring (car groups) 0 (match-end 0))))
2850                  (string-match prefix (cadr groups)))
2851             (progn
2852               (setq prefixes (cons prefix prefixes))
2853               (message "Descend hierarchy %s? ([y]nsq): "
2854                        (substring prefix 1 (1- (length prefix))))
2855               (setq ans (read-char))
2856               (cond ((= ans ?n)
2857                      (while (and groups
2858                                  (string-match prefix
2859                                                (setq group (car groups))))
2860                        (setq gnus-killed-list
2861                              (cons group gnus-killed-list))
2862                        (gnus-sethash group group gnus-killed-hashtb)
2863                        (setq groups (cdr groups)))
2864                      (setq starts (cdr starts)))
2865                     ((= ans ?s)
2866                      (while (and groups
2867                                  (string-match prefix
2868                                                (setq group (car groups))))
2869                        (gnus-sethash group group gnus-killed-hashtb)
2870                        (gnus-subscribe-alphabetically (car groups))
2871                        (setq groups (cdr groups)))
2872                      (setq starts (cdr starts)))
2873                     ((= ans ?q)
2874                      (while groups
2875                        (setq group (car groups))
2876                        (setq gnus-killed-list (cons group gnus-killed-list))
2877                        (gnus-sethash group group gnus-killed-hashtb)
2878                        (setq groups (cdr groups))))
2879                     (t nil)))
2880           (message "Subscribe %s? ([n]yq)" (car groups))
2881           (setq ans (read-char))
2882           (setq group (car groups))
2883           (cond ((= ans ?y)
2884                  (gnus-subscribe-alphabetically (car groups))
2885                  (gnus-sethash group group gnus-killed-hashtb))
2886                 ((= ans ?q)
2887                  (while groups
2888                    (setq group (car groups))
2889                    (setq gnus-killed-list (cons group gnus-killed-list))
2890                    (gnus-sethash group group gnus-killed-hashtb)
2891                    (setq groups (cdr groups))))
2892                 (t
2893                  (setq gnus-killed-list (cons group gnus-killed-list))
2894                  (gnus-sethash group group gnus-killed-hashtb)))
2895           (setq groups (cdr groups)))))))
2896
2897 (defun gnus-subscribe-randomly (newsgroup)
2898   "Subscribe new NEWSGROUP by making it the first newsgroup."
2899   (gnus-subscribe-newsgroup newsgroup))
2900
2901 (defun gnus-subscribe-alphabetically (newgroup)
2902   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2903   (let ((groups (cdr gnus-newsrc-alist))
2904         before)
2905     (while (and (not before) groups)
2906       (if (string< newgroup (caar groups))
2907           (setq before (caar groups))
2908         (setq groups (cdr groups))))
2909     (gnus-subscribe-newsgroup newgroup before)))
2910
2911 (defun gnus-subscribe-hierarchically (newgroup)
2912   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2913   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2914   (save-excursion
2915     (set-buffer (find-file-noselect gnus-current-startup-file))
2916     (let ((groupkey newgroup)
2917           before)
2918       (while (and (not before) groupkey)
2919         (goto-char (point-min))
2920         (let ((groupkey-re
2921                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2922           (while (and (re-search-forward groupkey-re nil t)
2923                       (progn
2924                         (setq before (match-string 1))
2925                         (string< before newgroup)))))
2926         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2927         (setq groupkey
2928               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2929                   (substring groupkey (match-beginning 1) (match-end 1)))))
2930       (gnus-subscribe-newsgroup newgroup before))))
2931
2932 (defun gnus-subscribe-interactively (group)
2933   "Subscribe the new GROUP interactively.
2934 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2935 it is killed."
2936   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2937       (gnus-subscribe-hierarchically group)
2938     (push group gnus-killed-list)))
2939
2940 (defun gnus-subscribe-zombies (group)
2941   "Make the new GROUP into a zombie group."
2942   (push group gnus-zombie-list))
2943
2944 (defun gnus-subscribe-killed (group)
2945   "Make the new GROUP a killed group."
2946   (push group gnus-killed-list))
2947
2948 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2949   "Subscribe new NEWSGROUP.
2950 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2951 the first newsgroup."
2952   ;; We subscribe the group by changing its level to `subscribed'.
2953   (gnus-group-change-level
2954    newsgroup gnus-level-default-subscribed
2955    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2956   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2957
2958 ;; For directories
2959
2960 (defun gnus-newsgroup-directory-form (newsgroup)
2961   "Make hierarchical directory name from NEWSGROUP name."
2962   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2963         (len (length newsgroup))
2964         idx)
2965     ;; If this is a foreign group, we don't want to translate the
2966     ;; entire name.
2967     (if (setq idx (string-match ":" newsgroup))
2968         (aset newsgroup idx ?/)
2969       (setq idx 0))
2970     ;; Replace all occurrences of `.' with `/'.
2971     (while (< idx len)
2972       (if (= (aref newsgroup idx) ?.)
2973           (aset newsgroup idx ?/))
2974       (setq idx (1+ idx)))
2975     newsgroup))
2976
2977 (defun gnus-newsgroup-savable-name (group)
2978   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2979   ;; with dots.
2980   (nnheader-replace-chars-in-string group ?/ ?.))
2981
2982 (defun gnus-make-directory (dir)
2983   "Make DIRECTORY recursively."
2984   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2985   ;; of the many mysteries of the universe.
2986   (let* ((dir (expand-file-name dir default-directory))
2987          dirs err)
2988     (if (string-match "/$" dir)
2989         (setq dir (substring dir 0 (match-beginning 0))))
2990     ;; First go down the path until we find a directory that exists.
2991     (while (not (file-exists-p dir))
2992       (setq dirs (cons dir dirs))
2993       (string-match "/[^/]+$" dir)
2994       (setq dir (substring dir 0 (match-beginning 0))))
2995     ;; Then create all the subdirs.
2996     (while (and dirs (not err))
2997       (condition-case ()
2998           (make-directory (car dirs))
2999         (error (setq err t)))
3000       (setq dirs (cdr dirs)))
3001     ;; We return whether we were successful or not.
3002     (not dirs)))
3003
3004 (defun gnus-capitalize-newsgroup (newsgroup)
3005   "Capitalize NEWSGROUP name."
3006   (and (not (zerop (length newsgroup)))
3007        (concat (char-to-string (upcase (aref newsgroup 0)))
3008                (substring newsgroup 1))))
3009
3010 ;; Various... things.
3011
3012 (defun gnus-simplify-subject (subject &optional re-only)
3013   "Remove `Re:' and words in parentheses.
3014 If RE-ONLY is non-nil, strip leading `Re:'s only."
3015   (let ((case-fold-search t))           ;Ignore case.
3016     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3017     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3018       (setq subject (substring subject (match-end 0))))
3019     ;; Remove uninteresting prefixes.
3020     (if (and (not re-only)
3021              gnus-simplify-ignored-prefixes
3022              (string-match gnus-simplify-ignored-prefixes subject))
3023         (setq subject (substring subject (match-end 0))))
3024     ;; Remove words in parentheses from end.
3025     (unless re-only
3026       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3027         (setq subject (substring subject 0 (match-beginning 0)))))
3028     ;; Return subject string.
3029     subject))
3030
3031 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3032 ;; all whitespace.
3033 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3034 (defun gnus-simplify-buffer-fuzzy ()
3035   (goto-char (point-min))
3036   (while (search-forward "\t" nil t)
3037     (replace-match " " t t))
3038   (goto-char (point-min))
3039   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3040   (goto-char (match-beginning 0))
3041   (while (or
3042           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3043           (looking-at "^[[].*: .*[]]$"))
3044     (goto-char (point-min))
3045     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3046                               nil t)
3047       (replace-match "" t t))
3048     (goto-char (point-min))
3049     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3050       (goto-char (match-end 0))
3051       (delete-char -1)
3052       (delete-region
3053        (progn (goto-char (match-beginning 0)))
3054        (re-search-forward ":"))))
3055   (goto-char (point-min))
3056   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3057     (replace-match "" t t))
3058   (goto-char (point-min))
3059   (while (re-search-forward "  +" nil t)
3060     (replace-match " " t t))
3061   (goto-char (point-min))
3062   (while (re-search-forward " $" nil t)
3063     (replace-match "" t t))
3064   (goto-char (point-min))
3065   (while (re-search-forward "^ +" nil t)
3066     (replace-match "" t t))
3067   (goto-char (point-min))
3068   (when gnus-simplify-subject-fuzzy-regexp
3069     (if (listp gnus-simplify-subject-fuzzy-regexp)
3070         (let ((list gnus-simplify-subject-fuzzy-regexp))
3071           (while list
3072             (goto-char (point-min))
3073             (while (re-search-forward (car list) nil t)
3074               (replace-match "" t t))
3075             (setq list (cdr list))))
3076       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3077         (replace-match "" t t)))))
3078
3079 (defun gnus-simplify-subject-fuzzy (subject)
3080   "Siplify a subject string fuzzily."
3081   (save-excursion
3082     (gnus-set-work-buffer)
3083     (let ((case-fold-search t))
3084       (insert subject)
3085       (inline (gnus-simplify-buffer-fuzzy))
3086       (buffer-string))))
3087
3088 ;; Add the current buffer to the list of buffers to be killed on exit.
3089 (defun gnus-add-current-to-buffer-list ()
3090   (or (memq (current-buffer) gnus-buffer-list)
3091       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3092
3093 (defun gnus-string> (s1 s2)
3094   (not (or (string< s1 s2)
3095            (string= s1 s2))))
3096
3097 ;;; General various misc type functions.
3098
3099 (defun gnus-clear-system ()
3100   "Clear all variables and buffers."
3101   ;; Clear Gnus variables.
3102   (let ((variables gnus-variable-list))
3103     (while variables
3104       (set (car variables) nil)
3105       (setq variables (cdr variables))))
3106   ;; Clear other internal variables.
3107   (setq gnus-list-of-killed-groups nil
3108         gnus-have-read-active-file nil
3109         gnus-newsrc-alist nil
3110         gnus-newsrc-hashtb nil
3111         gnus-killed-list nil
3112         gnus-zombie-list nil
3113         gnus-killed-hashtb nil
3114         gnus-active-hashtb nil
3115         gnus-moderated-list nil
3116         gnus-description-hashtb nil
3117         gnus-current-headers nil
3118         gnus-thread-indent-array nil
3119         gnus-newsgroup-headers nil
3120         gnus-newsgroup-name nil
3121         gnus-server-alist nil
3122         gnus-group-list-mode nil
3123         gnus-opened-servers nil
3124         gnus-current-select-method nil)
3125   (gnus-shutdown 'gnus)
3126   ;; Kill the startup file.
3127   (and gnus-current-startup-file
3128        (get-file-buffer gnus-current-startup-file)
3129        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3130   ;; Clear the dribble buffer.
3131   (gnus-dribble-clear)
3132   ;; Kill global KILL file buffer.
3133   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3134     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3135   (gnus-kill-buffer nntp-server-buffer)
3136   ;; Kill Gnus buffers.
3137   (while gnus-buffer-list
3138     (gnus-kill-buffer (pop gnus-buffer-list)))
3139   ;; Remove Gnus frames.
3140   (while gnus-created-frames
3141     (when (frame-live-p (car gnus-created-frames))
3142       ;; We slap a condition-case around this `delete-frame' to ensure 
3143       ;; agains errors if we try do delete the single frame that's left.
3144       (condition-case ()
3145           (delete-frame (car gnus-created-frames))
3146         (error nil)))
3147     (pop gnus-created-frames)))
3148
3149 (defun gnus-windows-old-to-new (setting)
3150   ;; First we take care of the really, really old Gnus 3 actions.
3151   (when (symbolp setting)
3152     (setq setting
3153           ;; Take care of ooold GNUS 3.x values.
3154           (cond ((eq setting 'SelectArticle) 'article)
3155                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3156                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3157                 (t setting))))
3158   (if (or (listp setting)
3159           (not (and gnus-window-configuration
3160                     (memq setting '(group summary article)))))
3161       setting
3162     (let* ((setting (if (eq setting 'group)
3163                         (if (assq 'newsgroup gnus-window-configuration)
3164                             'newsgroup
3165                           'newsgroups) setting))
3166            (elem (cadr (assq setting gnus-window-configuration)))
3167            (total (apply '+ elem))
3168            (types '(group summary article))
3169            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3170            (i 0)
3171            perc
3172            out)
3173       (while (< i 3)
3174         (or (not (numberp (nth i elem)))
3175             (zerop (nth i elem))
3176             (progn
3177               (setq perc  (/ (float (nth 0 elem)) total))
3178               (setq out (cons (if (eq pbuf (nth i types))
3179                                   (vector (nth i types) perc 'point)
3180                                 (vector (nth i types) perc))
3181                               out))))
3182         (setq i (1+ i)))
3183       (list (nreverse out)))))
3184
3185 ;;;###autoload
3186 (defun gnus-add-configuration (conf)
3187   "Add the window configuration CONF to `gnus-buffer-configuration'."
3188   (setq gnus-buffer-configuration
3189         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3190                          gnus-buffer-configuration))))
3191
3192 (defvar gnus-frame-list nil)
3193
3194 (defun gnus-configure-frame (split &optional window)
3195   "Split WINDOW according to SPLIT."
3196   (unless window
3197     (setq window (get-buffer-window (current-buffer))))
3198   (select-window window)
3199   ;; This might be an old-stylee buffer config.
3200   (when (vectorp split)
3201     (setq split (append split nil)))
3202   (when (or (consp (car split))
3203             (vectorp (car split)))
3204     (push 1.0 split)
3205     (push 'vertical split))
3206   ;; The SPLIT might be something that is to be evaled to
3207   ;; return a new SPLIT.
3208   (while (and (not (assq (car split) gnus-window-to-buffer))
3209               (gnus-functionp (car split)))
3210     (setq split (eval split)))
3211   (let* ((type (car split))
3212          (subs (cddr split))
3213          (len (if (eq type 'horizontal) (window-width) (window-height)))
3214          (total 0)
3215          (window-min-width (or gnus-window-min-width window-min-width))
3216          (window-min-height (or gnus-window-min-height window-min-height))
3217          s result new-win rest comp-subs size sub)
3218     (cond
3219      ;; Nothing to do here.
3220      ((null split))
3221      ;; Don't switch buffers.
3222      ((null type)
3223       (and (memq 'point split) window))
3224      ;; This is a buffer to be selected.
3225      ((not (memq type '(frame horizontal vertical)))
3226       (let ((buffer (cond ((stringp type) type)
3227                           (t (cdr (assq type gnus-window-to-buffer)))))
3228             buf)
3229         (unless buffer
3230           (error "Illegal buffer type: %s" type))
3231         (unless (setq buf (get-buffer (if (symbolp buffer)
3232                                           (symbol-value buffer) buffer)))
3233           (setq buf (get-buffer-create (if (symbolp buffer)
3234                                            (symbol-value buffer) buffer))))
3235         (switch-to-buffer buf)
3236         ;; We return the window if it has the `point' spec.
3237         (and (memq 'point split) window)))
3238      ;; This is a frame split.
3239      ((eq type 'frame)
3240       (unless gnus-frame-list
3241         (setq gnus-frame-list (list (window-frame
3242                                      (get-buffer-window (current-buffer))))))
3243       (let ((i 0)
3244             params frame fresult)
3245         (while (< i (length subs))
3246           ;; Frame parameter is gotten from the sub-split.
3247           (setq params (cadr (elt subs i)))
3248           ;; It should be a list.
3249           (unless (listp params)
3250             (setq params nil))
3251           ;; Create a new frame?
3252           (unless (setq frame (elt gnus-frame-list i))
3253             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3254             (push frame gnus-created-frames))
3255           ;; Is the old frame still alive?
3256           (unless (frame-live-p frame)
3257             (setcar (nthcdr i gnus-frame-list)
3258                     (setq frame (make-frame params))))
3259           ;; Select the frame in question and do more splits there.
3260           (select-frame frame)
3261           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3262           (incf i))
3263         ;; Select the frame that has the selected buffer.
3264         (when fresult
3265           (select-frame (window-frame fresult)))))
3266      ;; This is a normal split.
3267      (t
3268       (when (> (length subs) 0)
3269         ;; First we have to compute the sizes of all new windows.
3270         (while subs
3271           (setq sub (append (pop subs) nil))
3272           (while (and (not (assq (car sub) gnus-window-to-buffer))
3273                       (gnus-functionp (car sub)))
3274             (setq sub (eval sub)))
3275           (when sub
3276             (push sub comp-subs)
3277             (setq size (cadar comp-subs))
3278             (cond ((equal size 1.0)
3279                    (setq rest (car comp-subs))
3280                    (setq s 0))
3281                   ((floatp size)
3282                    (setq s (floor (* size len))))
3283                   ((integerp size)
3284                    (setq s size))
3285                   (t
3286                    (error "Illegal size: %s" size)))
3287             ;; Try to make sure that we are inside the safe limits.
3288             (cond ((zerop s))
3289                   ((eq type 'horizontal)
3290                    (setq s (max s window-min-width)))
3291                   ((eq type 'vertical)
3292                    (setq s (max s window-min-height))))
3293             (setcar (cdar comp-subs) s)
3294             (incf total s)))
3295         ;; Take care of the "1.0" spec.
3296         (if rest
3297             (setcar (cdr rest) (- len total))
3298           (error "No 1.0 specs in %s" split))
3299         ;; The we do the actual splitting in a nice recursive
3300         ;; fashion.
3301         (setq comp-subs (nreverse comp-subs))
3302         (while comp-subs
3303           (if (null (cdr comp-subs))
3304               (setq new-win window)
3305             (setq new-win
3306                   (split-window window (cadar comp-subs)
3307                                 (eq type 'horizontal))))
3308           (setq result (or (gnus-configure-frame
3309                             (car comp-subs) window) result))
3310           (select-window new-win)
3311           (setq window new-win)
3312           (setq comp-subs (cdr comp-subs))))
3313       ;; Return the proper window, if any.
3314       (when result
3315         (select-window result))))))
3316
3317 (defvar gnus-frame-split-p nil)
3318
3319 (defun gnus-configure-windows (setting &optional force)
3320   (setq setting (gnus-windows-old-to-new setting))
3321   (let ((split (if (symbolp setting)
3322                    (cadr (assq setting gnus-buffer-configuration))
3323                  setting))
3324         all-visible)
3325
3326     (setq gnus-frame-split-p nil)
3327
3328     (unless split
3329       (error "No such setting: %s" setting))
3330
3331     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3332              (not force))
3333         ;; All the windows mentioned are already visible, so we just
3334         ;; put point in the assigned buffer, and do not touch the
3335         ;; winconf.
3336         (select-window all-visible)
3337
3338       ;; Either remove all windows or just remove all Gnus windows.
3339       (let ((frame (selected-frame)))
3340         (unwind-protect
3341             (if gnus-use-full-window
3342                 ;; We want to remove all other windows.
3343                 (if (not gnus-frame-split-p)
3344                     ;; This is not a `frame' split, so we ignore the
3345                     ;; other frames.  
3346                     (delete-other-windows)
3347                   ;; This is a `frame' split, so we delete all windows
3348                   ;; on all frames.
3349                   (mapcar 
3350                    (lambda (frame)
3351                      (unless (eq (cdr (assq 'minibuffer
3352                                             (frame-parameters frame)))
3353                                  'only)
3354                        (select-frame frame)
3355                        (delete-other-windows)))
3356                    (frame-list)))
3357               ;; Just remove some windows.
3358               (gnus-remove-some-windows)
3359               (switch-to-buffer nntp-server-buffer))
3360           (select-frame frame)))
3361
3362       (switch-to-buffer nntp-server-buffer)
3363       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3364
3365 (defun gnus-all-windows-visible-p (split)
3366   "Say whether all buffers in SPLIT are currently visible.
3367 In particular, the value returned will be the window that
3368 should have point."
3369   (let ((stack (list split))
3370         (all-visible t)
3371         type buffer win buf)
3372     (while (and (setq split (pop stack))
3373                 all-visible)
3374       ;; Be backwards compatible.
3375       (when (vectorp split)
3376         (setq split (append split nil)))
3377       (when (or (consp (car split))
3378                 (vectorp (car split)))
3379         (push 1.0 split)
3380         (push 'vertical split))
3381       ;; The SPLIT might be something that is to be evaled to
3382       ;; return a new SPLIT.
3383       (while (and (not (assq (car split) gnus-window-to-buffer))
3384                   (gnus-functionp (car split)))
3385         (setq split (eval split)))
3386
3387       (setq type (elt split 0))
3388       (cond
3389        ;; Nothing here.
3390        ((null split) t)
3391        ;; A buffer.
3392        ((not (memq type '(horizontal vertical frame)))
3393         (setq buffer (cond ((stringp type) type)
3394                            (t (cdr (assq type gnus-window-to-buffer)))))
3395         (unless buffer
3396           (error "Illegal buffer type: %s" type))
3397         (when (setq buf (get-buffer (if (symbolp buffer)
3398                                         (symbol-value buffer)
3399                                       buffer)))
3400           (setq win (get-buffer-window buf t)))
3401         (if win
3402             (when (memq 'point split)
3403                 (setq all-visible win))
3404           (setq all-visible nil)))
3405        (t
3406         (when (eq type 'frame)
3407           (setq gnus-frame-split-p t))
3408         (setq stack (append (cddr split) stack)))))
3409     (unless (eq all-visible t)
3410       all-visible)))
3411
3412 (defun gnus-window-top-edge (&optional window)
3413   (nth 1 (window-edges window)))
3414
3415 (defun gnus-remove-some-windows ()
3416   (let ((buffers gnus-window-to-buffer)
3417         buf bufs lowest-buf lowest)
3418     (save-excursion
3419       ;; Remove windows on all known Gnus buffers.
3420       (while buffers
3421         (setq buf (cdar buffers))
3422         (if (symbolp buf)
3423             (setq buf (and (boundp buf) (symbol-value buf))))
3424         (and buf
3425              (get-buffer-window buf)
3426              (progn
3427                (setq bufs (cons buf bufs))
3428                (pop-to-buffer buf)
3429                (if (or (not lowest)
3430                        (< (gnus-window-top-edge) lowest))
3431                    (progn
3432                      (setq lowest (gnus-window-top-edge))
3433                      (setq lowest-buf buf)))))
3434         (setq buffers (cdr buffers)))
3435       ;; Remove windows on *all* summary buffers.
3436       (walk-windows
3437        (lambda (win)
3438          (let ((buf (window-buffer win)))
3439            (if (string-match    "^\\*Summary" (buffer-name buf))
3440                (progn
3441                  (setq bufs (cons buf bufs))
3442                  (pop-to-buffer buf)
3443                  (if (or (not lowest)
3444                          (< (gnus-window-top-edge) lowest))
3445                      (progn
3446                        (setq lowest-buf buf)
3447                        (setq lowest (gnus-window-top-edge)))))))))
3448       (and lowest-buf
3449            (progn
3450              (pop-to-buffer lowest-buf)
3451              (switch-to-buffer nntp-server-buffer)))
3452       (while bufs
3453         (and (not (eq (car bufs) lowest-buf))
3454              (delete-windows-on (car bufs)))
3455         (setq bufs (cdr bufs))))))
3456
3457 (defun gnus-version ()
3458   "Version numbers of this version of Gnus."
3459   (interactive)
3460   (let ((methods gnus-valid-select-methods)
3461         (mess gnus-version)
3462         meth)
3463     ;; Go through all the legal select methods and add their version
3464     ;; numbers to the total version string.  Only the backends that are
3465     ;; currently in use will have their message numbers taken into
3466     ;; consideration.
3467     (while methods
3468       (setq meth (intern (concat (caar methods) "-version")))
3469       (and (boundp meth)
3470            (stringp (symbol-value meth))
3471            (setq mess (concat mess "; " (symbol-value meth))))
3472       (setq methods (cdr methods)))
3473     (gnus-message 2 mess)))
3474
3475 (defun gnus-info-find-node ()
3476   "Find Info documentation of Gnus."
3477   (interactive)
3478   ;; Enlarge info window if needed.
3479   (let ((mode major-mode)
3480         gnus-info-buffer)
3481     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3482     (setq gnus-info-buffer (current-buffer))
3483     (gnus-configure-windows 'info)))
3484
3485 (defun gnus-days-between (date1 date2)
3486   ;; Return the number of days between date1 and date2.
3487   (- (gnus-day-number date1) (gnus-day-number date2)))
3488
3489 (defun gnus-day-number (date)
3490   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3491                      (timezone-parse-date date))))
3492     (timezone-absolute-from-gregorian
3493      (nth 1 dat) (nth 2 dat) (car dat))))
3494
3495 (defun gnus-encode-date (date)
3496   "Convert DATE to internal time."
3497   (let* ((parse (timezone-parse-date date))
3498          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3499          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3500     (encode-time (caddr time) (cadr time) (car time)
3501                  (caddr date) (cadr date) (car date) (nth 4 date))))
3502
3503 (defun gnus-time-minus (t1 t2)
3504   "Subtract two internal times."
3505   (let ((borrow (< (cadr t1) (cadr t2))))
3506     (list (- (car t1) (car t2) (if borrow 1 0))
3507           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3508
3509 (defun gnus-file-newer-than (file date)
3510   (let ((fdate (nth 5 (file-attributes file))))
3511     (or (> (car fdate) (car date))
3512         (and (= (car fdate) (car date))
3513              (> (nth 1 fdate) (nth 1 date))))))
3514
3515 (defmacro gnus-local-set-keys (&rest plist)
3516   "Set the keys in PLIST in the current keymap."
3517   `(gnus-define-keys-1 (current-local-map) ',plist))
3518
3519 (defmacro gnus-define-keys (keymap &rest plist)
3520   "Define all keys in PLIST in KEYMAP."
3521   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3522
3523 (put 'gnus-define-keys 'lisp-indent-function 1)
3524 (put 'gnus-define-keys 'lisp-indent-hook 1)
3525 (put 'gnus-define-keymap 'lisp-indent-function 1)
3526 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3527
3528 (defmacro gnus-define-keymap (keymap &rest plist)
3529   "Define all keys in PLIST in KEYMAP."
3530   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3531
3532 (defun gnus-define-keys-1 (keymap plist)
3533   (when (null keymap)
3534     (error "Can't set keys in a null keymap"))
3535   (cond ((symbolp keymap)
3536          (setq keymap (symbol-value keymap)))
3537         ((keymapp keymap))
3538         ((listp keymap)
3539          (set (car keymap) nil)
3540          (define-prefix-command (car keymap))
3541          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3542          (setq keymap (symbol-value (car keymap)))))
3543   (let (key)
3544     (while plist
3545       (when (symbolp (setq key (pop plist)))
3546         (setq key (symbol-value key)))
3547       (define-key keymap key (pop plist)))))
3548
3549 (defun gnus-group-read-only-p (&optional group)
3550   "Check whether GROUP supports editing or not.
3551 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3552 that that variable is buffer-local to the summary buffers."
3553   (let ((group (or group gnus-newsgroup-name)))
3554     (not (gnus-check-backend-function 'request-replace-article group))))
3555
3556 (defun gnus-group-total-expirable-p (group)
3557   "Check whether GROUP is total-expirable or not."
3558   (let ((params (gnus-info-params (gnus-get-info group))))
3559     (or (memq 'total-expire params)
3560         (cdr (assq 'total-expire params)) ; (total-expire . t)
3561         (and gnus-total-expirable-newsgroups ; Check var.
3562              (string-match gnus-total-expirable-newsgroups group)))))
3563
3564 (defun gnus-group-auto-expirable-p (group)
3565   "Check whether GROUP is total-expirable or not."
3566   (let ((params (gnus-info-params (gnus-get-info group))))
3567     (or (memq 'auto-expire params)
3568         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3569         (and gnus-auto-expirable-newsgroups ; Check var.
3570              (string-match gnus-auto-expirable-newsgroups group)))))
3571
3572 (defun gnus-virtual-group-p (group)
3573   "Say whether GROUP is virtual or not."
3574   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3575                         gnus-valid-select-methods)))
3576
3577 (defun gnus-news-group-p (group &optional article)
3578   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3579   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3580       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3581            (eq (gnus-request-type group article) 'news))))
3582
3583 (defsubst gnus-simplify-subject-fully (subject)
3584   "Simplify a subject string according to the user's wishes."
3585   (cond
3586    ((null gnus-summary-gather-subject-limit)
3587     (gnus-simplify-subject-re subject))
3588    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3589     (gnus-simplify-subject-fuzzy subject))
3590    ((numberp gnus-summary-gather-subject-limit)
3591     (gnus-limit-string (gnus-simplify-subject-re subject)
3592                        gnus-summary-gather-subject-limit))
3593    (t
3594     subject)))
3595
3596 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3597   "Check whether two subjects are equal.  If optional argument
3598 simple-first is t, first argument is already simplified."
3599   (cond
3600    ((null simple-first)
3601     (equal (gnus-simplify-subject-fully s1)
3602            (gnus-simplify-subject-fully s2)))
3603    (t
3604     (equal s1
3605            (gnus-simplify-subject-fully s2)))))
3606
3607 ;; Returns a list of writable groups.
3608 (defun gnus-writable-groups ()
3609   (let ((alist gnus-newsrc-alist)
3610         groups group)
3611     (while (setq group (car (pop alist)))
3612       (unless (gnus-group-read-only-p group)
3613         (push group groups)))
3614     (nreverse groups)))
3615
3616 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3617 ;; the echo area.
3618 (defun gnus-y-or-n-p (prompt)
3619   (prog1
3620       (y-or-n-p prompt)
3621     (message "")))
3622
3623 (defun gnus-yes-or-no-p (prompt)
3624   (prog1
3625       (yes-or-no-p prompt)
3626     (message "")))
3627
3628 ;; Check whether to use long file names.
3629 (defun gnus-use-long-file-name (symbol)
3630   ;; The variable has to be set...
3631   (and gnus-use-long-file-name
3632        ;; If it isn't a list, then we return t.
3633        (or (not (listp gnus-use-long-file-name))
3634            ;; If it is a list, and the list contains `symbol', we
3635            ;; return nil.
3636            (not (memq symbol gnus-use-long-file-name)))))
3637
3638 ;; I suspect there's a better way, but I haven't taken the time to do
3639 ;; it yet. -erik selberg@cs.washington.edu
3640 (defun gnus-dd-mmm (messy-date)
3641   "Return a string like DD-MMM from a big messy string"
3642   (let ((datevec (timezone-parse-date messy-date)))
3643     (format "%2s-%s"
3644             (condition-case ()
3645                 ;; Make sure leading zeroes are stripped.
3646                 (number-to-string (string-to-number (aref datevec 2)))
3647               (error "??"))
3648             (capitalize
3649              (or (car
3650                   (nth (1- (string-to-number (aref datevec 1)))
3651                        timezone-months-assoc))
3652                  "???")))))
3653
3654 ;; Make a hash table (default and minimum size is 255).
3655 ;; Optional argument HASHSIZE specifies the table size.
3656 (defun gnus-make-hashtable (&optional hashsize)
3657   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3658
3659 ;; Make a number that is suitable for hashing; bigger than MIN and one
3660 ;; less than 2^x.
3661 (defun gnus-create-hash-size (min)
3662   (let ((i 1))
3663     (while (< i min)
3664       (setq i (* 2 i)))
3665     (1- i)))
3666
3667 ;; Show message if message has a lower level than `gnus-verbose'.
3668 ;; Guideline for numbers:
3669 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3670 ;; for things that take a long time, 7 - not very important messages
3671 ;; on stuff, 9 - messages inside loops.
3672 (defun gnus-message (level &rest args)
3673   (if (<= level gnus-verbose)
3674       (apply 'message args)
3675     ;; We have to do this format thingy here even if the result isn't
3676     ;; shown - the return value has to be the same as the return value
3677     ;; from `message'.
3678     (apply 'format args)))
3679
3680 ;; Generate a unique new group name.
3681 (defun gnus-generate-new-group-name (leaf)
3682   (let ((name leaf)
3683         (num 0))
3684     (while (gnus-gethash name gnus-newsrc-hashtb)
3685       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3686     name))
3687
3688 (defsubst gnus-hide-text (b e props)
3689   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3690   (add-text-properties b e props)
3691   (when (memq 'intangible props)
3692     (put-text-property (max (1- b) (point-min))
3693                        b 'intangible (cddr (memq 'intangible props)))))
3694
3695 (defsubst gnus-unhide-text (b e)
3696   "Remove hidden text properties from region between B and E."
3697   (remove-text-properties b e gnus-hidden-properties)
3698   (when (memq 'intangible gnus-hidden-properties)
3699     (put-text-property (max (1- b) (point-min))
3700                        b 'intangible nil)))
3701
3702 (defun gnus-hide-text-type (b e type)
3703   "Hide text of TYPE between B and E."
3704   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3705
3706 ;; Find out whether the gnus-visual TYPE is wanted.
3707 (defun gnus-visual-p (&optional type class)
3708   (and gnus-visual                      ; Has to be non-nil, at least.
3709        (if (not type)                   ; We don't care about type.
3710            gnus-visual
3711          (if (listp gnus-visual)        ; It's a list, so we check it.
3712              (or (memq type gnus-visual)
3713                  (memq class gnus-visual))
3714            t))))
3715
3716 (defun gnus-parent-id (references)
3717   "Return the last Message-ID in REFERENCES."
3718   (when (and references
3719              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3720     (substring references (match-beginning 1) (match-end 1))))
3721
3722 (defun gnus-split-references (references)
3723   "Return a list of Message-IDs in REFERENCES."
3724   (let ((beg 0)
3725         ids)
3726     (while (string-match "<[^>]+>" references beg)
3727       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3728             ids))
3729     (nreverse ids)))
3730
3731 (defun gnus-buffer-live-p (buffer)
3732   "Say whether BUFFER is alive or not."
3733   (and buffer
3734        (get-buffer buffer)
3735        (buffer-name (get-buffer buffer))))
3736
3737 (defun gnus-ephemeral-group-p (group)
3738   "Say whether GROUP is ephemeral or not."
3739   (gnus-group-get-parameter group 'quit-config))
3740
3741 (defun gnus-group-quit-config (group)
3742   "Return the quit-config of GROUP."
3743   (gnus-group-get-parameter group 'quit-config))
3744
3745 (defun gnus-simplify-mode-line ()
3746   "Make mode lines a bit simpler."
3747   (setq mode-line-modified "-- ")
3748   (when (listp mode-line-format)
3749     (make-local-variable 'mode-line-format)
3750     (setq mode-line-format (copy-sequence mode-line-format))
3751     (when (equal (nth 3 mode-line-format) "   ")
3752       (setcar (nthcdr 3 mode-line-format) " "))))
3753
3754 ;;; List and range functions
3755
3756 (defun gnus-last-element (list)
3757   "Return last element of LIST."
3758   (while (cdr list)
3759     (setq list (cdr list)))
3760   (car list))
3761
3762 (defun gnus-copy-sequence (list)
3763   "Do a complete, total copy of a list."
3764   (if (and (consp list) (not (consp (cdr list))))
3765       (cons (car list) (cdr list))
3766     (mapcar (lambda (elem) (if (consp elem)
3767                                (if (consp (cdr elem))
3768                                    (gnus-copy-sequence elem)
3769                                  (cons (car elem) (cdr elem)))
3770                              elem))
3771             list)))
3772
3773 (defun gnus-set-difference (list1 list2)
3774   "Return a list of elements of LIST1 that do not appear in LIST2."
3775   (let ((list1 (copy-sequence list1)))
3776     (while list2
3777       (setq list1 (delq (car list2) list1))
3778       (setq list2 (cdr list2)))
3779     list1))
3780
3781 (defun gnus-sorted-complement (list1 list2)
3782   "Return a list of elements of LIST1 that do not appear in LIST2.
3783 Both lists have to be sorted over <."
3784   (let (out)
3785     (if (or (null list1) (null list2))
3786         (or list1 list2)
3787       (while (and list1 list2)
3788         (cond ((= (car list1) (car list2))
3789                (setq list1 (cdr list1)
3790                      list2 (cdr list2)))
3791               ((< (car list1) (car list2))
3792                (setq out (cons (car list1) out))
3793                (setq list1 (cdr list1)))
3794               (t
3795                (setq out (cons (car list2) out))
3796                (setq list2 (cdr list2)))))
3797       (nconc (nreverse out) (or list1 list2)))))
3798
3799 (defun gnus-intersection (list1 list2)
3800   (let ((result nil))
3801     (while list2
3802       (if (memq (car list2) list1)
3803           (setq result (cons (car list2) result)))
3804       (setq list2 (cdr list2)))
3805     result))
3806
3807 (defun gnus-sorted-intersection (list1 list2)
3808   ;; LIST1 and LIST2 have to be sorted over <.
3809   (let (out)
3810     (while (and list1 list2)
3811       (cond ((= (car list1) (car list2))
3812              (setq out (cons (car list1) out)
3813                    list1 (cdr list1)
3814                    list2 (cdr list2)))
3815             ((< (car list1) (car list2))
3816              (setq list1 (cdr list1)))
3817             (t
3818              (setq list2 (cdr list2)))))
3819     (nreverse out)))
3820
3821 (defun gnus-set-sorted-intersection (list1 list2)
3822   ;; LIST1 and LIST2 have to be sorted over <.
3823   ;; This function modifies LIST1.
3824   (let* ((top (cons nil list1))
3825          (prev top))
3826     (while (and list1 list2)
3827       (cond ((= (car list1) (car list2))
3828              (setq prev list1
3829                    list1 (cdr list1)
3830                    list2 (cdr list2)))
3831             ((< (car list1) (car list2))
3832              (setcdr prev (cdr list1))
3833              (setq list1 (cdr list1)))
3834             (t
3835              (setq list2 (cdr list2)))))
3836     (setcdr prev nil)
3837     (cdr top)))
3838
3839 (defun gnus-compress-sequence (numbers &optional always-list)
3840   "Convert list of numbers to a list of ranges or a single range.
3841 If ALWAYS-LIST is non-nil, this function will always release a list of
3842 ranges."
3843   (let* ((first (car numbers))
3844          (last (car numbers))
3845          result)
3846     (if (null numbers)
3847         nil
3848       (if (not (listp (cdr numbers)))
3849           numbers
3850         (while numbers
3851           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3852                 ((= (1+ last) (car numbers)) ;Still in sequence
3853                  (setq last (car numbers)))
3854                 (t                      ;End of one sequence
3855                  (setq result
3856                        (cons (if (= first last) first
3857                                (cons first last)) result))
3858                  (setq first (car numbers))
3859                  (setq last  (car numbers))))
3860           (setq numbers (cdr numbers)))
3861         (if (and (not always-list) (null result))
3862             (if (= first last) (list first) (cons first last))
3863           (nreverse (cons (if (= first last) first (cons first last))
3864                           result)))))))
3865
3866 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3867 (defun gnus-uncompress-range (ranges)
3868   "Expand a list of ranges into a list of numbers.
3869 RANGES is either a single range on the form `(num . num)' or a list of
3870 these ranges."
3871   (let (first last result)
3872     (cond
3873      ((null ranges)
3874       nil)
3875      ((not (listp (cdr ranges)))
3876       (setq first (car ranges))
3877       (setq last (cdr ranges))
3878       (while (<= first last)
3879         (setq result (cons first result))
3880         (setq first (1+ first)))
3881       (nreverse result))
3882      (t
3883       (while ranges
3884         (if (atom (car ranges))
3885             (if (numberp (car ranges))
3886                 (setq result (cons (car ranges) result)))
3887           (setq first (caar ranges))
3888           (setq last  (cdar ranges))
3889           (while (<= first last)
3890             (setq result (cons first result))
3891             (setq first (1+ first))))
3892         (setq ranges (cdr ranges)))
3893       (nreverse result)))))
3894
3895 (defun gnus-add-to-range (ranges list)
3896   "Return a list of ranges that has all articles from both RANGES and LIST.
3897 Note: LIST has to be sorted over `<'."
3898   (if (not ranges)
3899       (gnus-compress-sequence list t)
3900     (setq list (copy-sequence list))
3901     (or (listp (cdr ranges))
3902         (setq ranges (list ranges)))
3903     (let ((out ranges)
3904           ilist lowest highest temp)
3905       (while (and ranges list)
3906         (setq ilist list)
3907         (setq lowest (or (and (atom (car ranges)) (car ranges))
3908                          (caar ranges)))
3909         (while (and list (cdr list) (< (cadr list) lowest))
3910           (setq list (cdr list)))
3911         (if (< (car ilist) lowest)
3912             (progn
3913               (setq temp list)
3914               (setq list (cdr list))
3915               (setcdr temp nil)
3916               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3917         (setq highest (or (and (atom (car ranges)) (car ranges))
3918                           (cdar ranges)))
3919         (while (and list (<= (car list) highest))
3920           (setq list (cdr list)))
3921         (setq ranges (cdr ranges)))
3922       (if list
3923           (setq out (nconc (gnus-compress-sequence list t) out)))
3924       (setq out (sort out (lambda (r1 r2)
3925                             (< (or (and (atom r1) r1) (car r1))
3926                                (or (and (atom r2) r2) (car r2))))))
3927       (setq ranges out)
3928       (while ranges
3929         (if (atom (car ranges))
3930             (if (cdr ranges)
3931                 (if (atom (cadr ranges))
3932                     (if (= (1+ (car ranges)) (cadr ranges))
3933                         (progn
3934                           (setcar ranges (cons (car ranges)
3935                                                (cadr ranges)))
3936                           (setcdr ranges (cddr ranges))))
3937                   (if (= (1+ (car ranges)) (caadr ranges))
3938                       (progn
3939                         (setcar (cadr ranges) (car ranges))
3940                         (setcar ranges (cadr ranges))
3941                         (setcdr ranges (cddr ranges))))))
3942           (if (cdr ranges)
3943               (if (atom (cadr ranges))
3944                   (if (= (1+ (cdar ranges)) (cadr ranges))
3945                       (progn
3946                         (setcdr (car ranges) (cadr ranges))
3947                         (setcdr ranges (cddr ranges))))
3948                 (if (= (1+ (cdar ranges)) (caadr ranges))
3949                     (progn
3950                       (setcdr (car ranges) (cdadr ranges))
3951                       (setcdr ranges (cddr ranges)))))))
3952         (setq ranges (cdr ranges)))
3953       out)))
3954
3955 (defun gnus-remove-from-range (ranges list)
3956   "Return a list of ranges that has all articles from LIST removed from RANGES.
3957 Note: LIST has to be sorted over `<'."
3958   ;; !!! This function shouldn't look like this, but I've got a headache.
3959   (gnus-compress-sequence
3960    (gnus-sorted-complement
3961     (gnus-uncompress-range ranges) list)))
3962
3963 (defun gnus-member-of-range (number ranges)
3964   (if (not (listp (cdr ranges)))
3965       (and (>= number (car ranges))
3966            (<= number (cdr ranges)))
3967     (let ((not-stop t))
3968       (while (and ranges
3969                   (if (numberp (car ranges))
3970                       (>= number (car ranges))
3971                     (>= number (caar ranges)))
3972                   not-stop)
3973         (if (if (numberp (car ranges))
3974                 (= number (car ranges))
3975               (and (>= number (caar ranges))
3976                    (<= number (cdar ranges))))
3977             (setq not-stop nil))
3978         (setq ranges (cdr ranges)))
3979       (not not-stop))))
3980
3981 (defun gnus-range-length (range)
3982   "Return the length RANGE would have if uncompressed."
3983   (length (gnus-uncompress-range range)))
3984
3985 (defun gnus-sublist-p (list sublist)
3986   "Test whether all elements in SUBLIST are members of LIST."
3987   (let ((sublistp t))
3988     (while sublist
3989       (unless (memq (pop sublist) list)
3990         (setq sublistp nil
3991               sublist nil)))
3992     sublistp))
3993
3994 \f
3995 ;;;
3996 ;;; Gnus group mode
3997 ;;;
3998
3999 (defvar gnus-group-mode-map nil)
4000 (put 'gnus-group-mode 'mode-class 'special)
4001
4002 (unless gnus-group-mode-map
4003   (setq gnus-group-mode-map (make-keymap))
4004   (suppress-keymap gnus-group-mode-map)
4005
4006   (gnus-define-keys gnus-group-mode-map
4007     " " gnus-group-read-group
4008     "=" gnus-group-select-group
4009     "\r" gnus-group-select-group
4010     "\M-\r" gnus-group-quick-select-group
4011     "j" gnus-group-jump-to-group
4012     "n" gnus-group-next-unread-group
4013     "p" gnus-group-prev-unread-group
4014     "\177" gnus-group-prev-unread-group
4015     [delete] gnus-group-prev-unread-group
4016     "N" gnus-group-next-group
4017     "P" gnus-group-prev-group
4018     "\M-n" gnus-group-next-unread-group-same-level
4019     "\M-p" gnus-group-prev-unread-group-same-level
4020     "," gnus-group-best-unread-group
4021     "." gnus-group-first-unread-group
4022     "u" gnus-group-unsubscribe-current-group
4023     "U" gnus-group-unsubscribe-group
4024     "c" gnus-group-catchup-current
4025     "C" gnus-group-catchup-current-all
4026     "l" gnus-group-list-groups
4027     "L" gnus-group-list-all-groups
4028     "m" gnus-group-mail
4029     "g" gnus-group-get-new-news
4030     "\M-g" gnus-group-get-new-news-this-group
4031     "R" gnus-group-restart
4032     "r" gnus-group-read-init-file
4033     "B" gnus-group-browse-foreign-server
4034     "b" gnus-group-check-bogus-groups
4035     "F" gnus-find-new-newsgroups
4036     "\C-c\C-d" gnus-group-describe-group
4037     "\M-d" gnus-group-describe-all-groups
4038     "\C-c\C-a" gnus-group-apropos
4039     "\C-c\M-\C-a" gnus-group-description-apropos
4040     "a" gnus-group-post-news
4041     "\ek" gnus-group-edit-local-kill
4042     "\eK" gnus-group-edit-global-kill
4043     "\C-k" gnus-group-kill-group
4044     "\C-y" gnus-group-yank-group
4045     "\C-w" gnus-group-kill-region
4046     "\C-x\C-t" gnus-group-transpose-groups
4047     "\C-c\C-l" gnus-group-list-killed
4048     "\C-c\C-x" gnus-group-expire-articles
4049     "\C-c\M-\C-x" gnus-group-expire-all-groups
4050     "V" gnus-version
4051     "s" gnus-group-save-newsrc
4052     "z" gnus-group-suspend
4053     "Z" gnus-group-clear-dribble
4054     "q" gnus-group-exit
4055     "Q" gnus-group-quit
4056     "?" gnus-group-describe-briefly
4057     "\C-c\C-i" gnus-info-find-node
4058     "\M-e" gnus-group-edit-group-method
4059     "^" gnus-group-enter-server-mode
4060     gnus-mouse-2 gnus-mouse-pick-group
4061     "<" beginning-of-buffer
4062     ">" end-of-buffer
4063     "\C-c\C-b" gnus-bug
4064     "\C-c\C-s" gnus-group-sort-groups
4065     "t" gnus-topic-mode
4066     "\C-c\M-g" gnus-activate-all-groups
4067     "\M-&" gnus-group-universal-argument
4068     "#" gnus-group-mark-group
4069     "\M-#" gnus-group-unmark-group)
4070
4071   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4072     "m" gnus-group-mark-group
4073     "u" gnus-group-unmark-group
4074     "w" gnus-group-mark-region
4075     "m" gnus-group-mark-buffer
4076     "r" gnus-group-mark-regexp
4077     "U" gnus-group-unmark-all-groups)
4078
4079   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4080     "d" gnus-group-make-directory-group
4081     "h" gnus-group-make-help-group
4082     "a" gnus-group-make-archive-group
4083     "k" gnus-group-make-kiboze-group
4084     "m" gnus-group-make-group
4085     "E" gnus-group-edit-group
4086     "e" gnus-group-edit-group-method
4087     "p" gnus-group-edit-group-parameters
4088     "v" gnus-group-add-to-virtual
4089     "V" gnus-group-make-empty-virtual
4090     "D" gnus-group-enter-directory
4091     "f" gnus-group-make-doc-group
4092     "r" gnus-group-rename-group
4093     "\177" gnus-group-delete-group
4094     [delete] gnus-group-delete-group)
4095
4096    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4097      "b" gnus-group-brew-soup
4098      "w" gnus-soup-save-areas
4099      "s" gnus-soup-send-replies
4100      "p" gnus-soup-pack-packet
4101      "r" nnsoup-pack-replies)
4102
4103    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4104      "s" gnus-group-sort-groups
4105      "a" gnus-group-sort-groups-by-alphabet
4106      "u" gnus-group-sort-groups-by-unread
4107      "l" gnus-group-sort-groups-by-level
4108      "v" gnus-group-sort-groups-by-score
4109      "r" gnus-group-sort-groups-by-rank
4110      "m" gnus-group-sort-groups-by-method)
4111
4112    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4113      "k" gnus-group-list-killed
4114      "z" gnus-group-list-zombies
4115      "s" gnus-group-list-groups
4116      "u" gnus-group-list-all-groups
4117      "A" gnus-group-list-active
4118      "a" gnus-group-apropos
4119      "d" gnus-group-description-apropos
4120      "m" gnus-group-list-matching
4121      "M" gnus-group-list-all-matching
4122      "l" gnus-group-list-level)
4123
4124    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4125      "f" gnus-score-flush-cache)
4126
4127    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4128      "f" gnus-group-fetch-faq)
4129
4130    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4131      "l" gnus-group-set-current-level
4132      "t" gnus-group-unsubscribe-current-group
4133      "s" gnus-group-unsubscribe-group
4134      "k" gnus-group-kill-group
4135      "y" gnus-group-yank-group
4136      "w" gnus-group-kill-region
4137      "\C-k" gnus-group-kill-level
4138      "z" gnus-group-kill-all-zombies))
4139
4140 (defun gnus-group-mode ()
4141   "Major mode for reading news.
4142
4143 All normal editing commands are switched off.
4144 \\<gnus-group-mode-map>
4145 The group buffer lists (some of) the groups available.  For instance,
4146 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4147 lists all zombie groups.
4148
4149 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4150 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4151
4152 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4153
4154 The following commands are available:
4155
4156 \\{gnus-group-mode-map}"
4157   (interactive)
4158   (when (and menu-bar-mode
4159              (gnus-visual-p 'group-menu 'menu))
4160     (gnus-group-make-menu-bar))
4161   (kill-all-local-variables)
4162   (gnus-simplify-mode-line)
4163   (setq major-mode 'gnus-group-mode)
4164   (setq mode-name "Group")
4165   (gnus-group-set-mode-line)
4166   (setq mode-line-process nil)
4167   (use-local-map gnus-group-mode-map)
4168   (buffer-disable-undo (current-buffer))
4169   (setq truncate-lines t)
4170   (setq buffer-read-only t)
4171   (run-hooks 'gnus-group-mode-hook))
4172
4173 (defun gnus-mouse-pick-group (e)
4174   "Enter the group under the mouse pointer."
4175   (interactive "e")
4176   (mouse-set-point e)
4177   (gnus-group-read-group nil))
4178
4179 ;; Look at LEVEL and find out what the level is really supposed to be.
4180 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4181 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4182 (defun gnus-group-default-level (&optional level number-or-nil)
4183   (cond
4184    (gnus-group-use-permanent-levels
4185 ;    (setq gnus-group-default-list-level
4186 ;         (or level gnus-group-default-list-level))
4187     (or level gnus-group-default-list-level gnus-level-subscribed))
4188    (number-or-nil
4189     level)
4190    (t
4191     (or level gnus-group-default-list-level gnus-level-subscribed))))
4192
4193 ;;;###autoload
4194 (defun gnus-slave-no-server (&optional arg)
4195   "Read network news as a slave, without connecting to local server"
4196   (interactive "P")
4197   (gnus-no-server arg t))
4198
4199 ;;;###autoload
4200 (defun gnus-no-server (&optional arg slave)
4201   "Read network news.
4202 If ARG is a positive number, Gnus will use that as the
4203 startup level.  If ARG is nil, Gnus will be started at level 2.
4204 If ARG is non-nil and not a positive number, Gnus will
4205 prompt the user for the name of an NNTP server to use.
4206 As opposed to `gnus', this command will not connect to the local server."
4207   (interactive "P")
4208   (let ((gnus-group-use-permanent-levels t))
4209     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4210   (make-local-variable 'gnus-group-use-permanent-levels)
4211   (setq gnus-group-use-permanent-levels t))
4212
4213 ;;;###autoload
4214 (defun gnus-slave (&optional arg)
4215   "Read news as a slave."
4216   (interactive "P")
4217   (gnus arg nil 'slave))
4218
4219 ;;;###autoload
4220 (defun gnus-other-frame (&optional arg)
4221   "Pop up a frame to read news."
4222   (interactive "P")
4223   (if (get-buffer gnus-group-buffer)
4224       (let ((pop-up-frames t))
4225         (gnus arg))
4226     (select-frame (make-frame))
4227     (gnus arg)))
4228
4229 ;;;###autoload
4230 (defun gnus (&optional arg dont-connect slave)
4231   "Read network news.
4232 If ARG is non-nil and a positive number, Gnus will use that as the
4233 startup level.  If ARG is non-nil and not a positive number, Gnus will
4234 prompt the user for the name of an NNTP server to use."
4235   (interactive "P")
4236
4237   (if (get-buffer gnus-group-buffer)
4238       (progn
4239         (switch-to-buffer gnus-group-buffer)
4240         (gnus-group-get-new-news))
4241
4242     (gnus-clear-system)
4243     (nnheader-init-server-buffer)
4244     (gnus-read-init-file)
4245     (setq gnus-slave slave)
4246
4247     (gnus-group-setup-buffer)
4248     (let ((buffer-read-only nil))
4249       (erase-buffer)
4250       (if (not gnus-inhibit-startup-message)
4251           (progn
4252             (gnus-group-startup-message)
4253             (sit-for 0))))
4254
4255     (let ((level (and (numberp arg) (> arg 0) arg))
4256           did-connect)
4257       (unwind-protect
4258           (progn
4259             (or dont-connect
4260                 (setq did-connect
4261                       (gnus-start-news-server (and arg (not level))))))
4262         (if (and (not dont-connect)
4263                  (not did-connect))
4264             (gnus-group-quit)
4265           (run-hooks 'gnus-startup-hook)
4266           ;; NNTP server is successfully open.
4267
4268           ;; Find the current startup file name.
4269           (setq gnus-current-startup-file
4270                 (gnus-make-newsrc-file gnus-startup-file))
4271
4272           ;; Read the dribble file.
4273           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4274
4275           ;; Allow using GroupLens predictions.
4276           (when gnus-use-grouplens
4277             (bbb-login)
4278             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4279
4280           (gnus-summary-make-display-table)
4281           ;; Do the actual startup.
4282           (gnus-setup-news nil level dont-connect)
4283           ;; Generate the group buffer.
4284           (gnus-group-list-groups level)
4285           (gnus-group-first-unread-group)
4286           (gnus-configure-windows 'group)
4287           (gnus-group-set-mode-line))))))
4288
4289 (defun gnus-unload ()
4290   "Unload all Gnus features."
4291   (interactive)
4292   (or (boundp 'load-history)
4293       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4294   (let ((history load-history)
4295         feature)
4296     (while history
4297       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4298            (setq feature (cdr (assq 'provide (car history))))
4299            (unload-feature feature 'force))
4300       (setq history (cdr history)))))
4301
4302 (defun gnus-compile ()
4303   "Byte-compile the user-defined format specs."
4304   (interactive)
4305   (let ((entries gnus-format-specs)
4306         entry gnus-tmp-func)
4307     (save-excursion
4308       (gnus-message 7 "Compiling format specs...")
4309
4310       (while entries
4311         (setq entry (pop entries))
4312         (if (eq (car entry) 'version)
4313             (setq gnus-format-specs (delq entry gnus-format-specs))
4314           (when (and (listp (caddr entry))
4315                      (not (eq 'byte-code (caaddr entry))))
4316             (fset 'gnus-tmp-func
4317                   `(lambda () ,(caddr entry)))
4318             (byte-compile 'gnus-tmp-func)
4319             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4320
4321       (push (cons 'version emacs-version) gnus-format-specs)
4322
4323       (gnus-message 7 "Compiling user specs...done"))))
4324
4325 (defun gnus-indent-rigidly (start end arg)
4326   "Indent rigidly using only spaces and no tabs."
4327   (save-excursion
4328     (save-restriction
4329       (narrow-to-region start end)
4330       (indent-rigidly start end arg)
4331       (goto-char (point-min))
4332       (while (search-forward "\t" nil t)
4333         (replace-match "        " t t)))))
4334
4335 (defun gnus-group-startup-message (&optional x y)
4336   "Insert startup message in current buffer."
4337   ;; Insert the message.
4338   (erase-buffer)
4339   (insert
4340    (format "              %s
4341           _    ___ _             _
4342           _ ___ __ ___  __    _ ___
4343           __   _     ___    __  ___
4344               _           ___     _
4345              _  _ __             _
4346              ___   __            _
4347                    __           _
4348                     _      _   _
4349                    _      _    _
4350                       _  _    _
4351                   __  ___
4352                  _   _ _     _
4353                 _   _
4354               _    _
4355              _    _
4356             _
4357           __
4358
4359 "
4360            ""))
4361   ;; And then hack it.
4362   (gnus-indent-rigidly (point-min) (point-max)
4363                        (/ (max (- (window-width) (or x 46)) 0) 2))
4364   (goto-char (point-min))
4365   (forward-line 1)
4366   (let* ((pheight (count-lines (point-min) (point-max)))
4367          (wheight (window-height))
4368          (rest (- wheight pheight)))
4369     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4370   ;; Fontify some.
4371   (goto-char (point-min))
4372   (and (search-forward "Praxis" nil t)
4373        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4374   (goto-char (point-min))
4375   (let* ((mode-string (gnus-group-set-mode-line)))
4376     (setq mode-line-buffer-identification
4377           (list (concat gnus-version (substring (car mode-string) 4))))
4378     (set-buffer-modified-p t)))
4379
4380 (defun gnus-group-setup-buffer ()
4381   (or (get-buffer gnus-group-buffer)
4382       (progn
4383         (switch-to-buffer gnus-group-buffer)
4384         (gnus-add-current-to-buffer-list)
4385         (gnus-group-mode)
4386         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4387
4388 (defun gnus-group-list-groups (&optional level unread lowest)
4389   "List newsgroups with level LEVEL or lower that have unread articles.
4390 Default is all subscribed groups.
4391 If argument UNREAD is non-nil, groups with no unread articles are also
4392 listed."
4393   (interactive (list (if current-prefix-arg
4394                          (prefix-numeric-value current-prefix-arg)
4395                        (or
4396                         (gnus-group-default-level nil t)
4397                         gnus-group-default-list-level
4398                         gnus-level-subscribed))))
4399   (or level
4400       (setq level (car gnus-group-list-mode)
4401             unread (cdr gnus-group-list-mode)))
4402   (setq level (gnus-group-default-level level))
4403   (gnus-group-setup-buffer)             ;May call from out of group buffer
4404   (gnus-update-format-specifications)
4405   (let ((case-fold-search nil)
4406         (props (text-properties-at (gnus-point-at-bol)))
4407         (group (gnus-group-group-name)))
4408     (set-buffer gnus-group-buffer)
4409     (funcall gnus-group-prepare-function level unread lowest)
4410     (if (zerop (buffer-size))
4411         (gnus-message 5 gnus-no-groups-message)
4412       (goto-char (point-max))
4413       (when (or (not gnus-group-goto-next-group-function)
4414                 (not (funcall gnus-group-goto-next-group-function 
4415                               group props)))
4416         (if (not group)
4417             ;; Go to the first group with unread articles.
4418             (gnus-group-search-forward t)
4419           ;; Find the right group to put point on.  If the current group
4420           ;; has disappeared in the new listing, try to find the next
4421           ;; one.        If no next one can be found, just leave point at the
4422           ;; first newsgroup in the buffer.
4423           (if (not (gnus-goto-char
4424                     (text-property-any
4425                      (point-min) (point-max)
4426                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4427               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4428                 (while (and newsrc
4429                             (not (gnus-goto-char
4430                                   (text-property-any
4431                                    (point-min) (point-max) 'gnus-group
4432                                    (gnus-intern-safe
4433                                     (caar newsrc) gnus-active-hashtb)))))
4434                   (setq newsrc (cdr newsrc)))
4435                 (or newsrc (progn (goto-char (point-max))
4436                                   (forward-line -1)))))))
4437       ;; Adjust cursor point.
4438       (gnus-group-position-point))))
4439
4440 (defun gnus-group-list-level (level &optional all)
4441   "List groups on LEVEL.
4442 If ALL (the prefix), also list groups that have no unread articles."
4443   (interactive "nList groups on level: \nP")
4444   (gnus-group-list-groups level all level))
4445
4446 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4447   "List all newsgroups with unread articles of level LEVEL or lower.
4448 If ALL is non-nil, list groups that have no unread articles.
4449 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4450 If REGEXP, only list groups matching REGEXP."
4451   (set-buffer gnus-group-buffer)
4452   (let ((buffer-read-only nil)
4453         (newsrc (cdr gnus-newsrc-alist))
4454         (lowest (or lowest 1))
4455         info clevel unread group params)
4456     (erase-buffer)
4457     (if (< lowest gnus-level-zombie)
4458         ;; List living groups.
4459         (while newsrc
4460           (setq info (car newsrc)
4461                 group (gnus-info-group info)
4462                 params (gnus-info-params info)
4463                 newsrc (cdr newsrc)
4464                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4465           (and unread                   ; This group might be bogus
4466                (or (not regexp)
4467                    (string-match regexp group))
4468                (<= (setq clevel (gnus-info-level info)) level)
4469                (>= clevel lowest)
4470                (or all                  ; We list all groups?
4471                    (if (eq unread t)    ; Unactivated?
4472                        gnus-group-list-inactive-groups ; We list unactivated 
4473                      (> unread 0))      ; We list groups with unread articles
4474                    (and gnus-list-groups-with-ticked-articles
4475                         (cdr (assq 'tick (gnus-info-marks info))))
4476                                         ; And groups with tickeds
4477                    ;; Check for permanent visibility.
4478                    (and gnus-permanently-visible-groups
4479                         (string-match gnus-permanently-visible-groups
4480                                       group))
4481                    (memq 'visible params)
4482                    (cdr (assq 'visible params)))
4483                (gnus-group-insert-group-line
4484                 group (gnus-info-level info)
4485                 (gnus-info-marks info) unread (gnus-info-method info)))))
4486
4487     ;; List dead groups.
4488     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4489          (gnus-group-prepare-flat-list-dead
4490           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4491           gnus-level-zombie ?Z
4492           regexp))
4493     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4494          (gnus-group-prepare-flat-list-dead
4495           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4496           gnus-level-killed ?K regexp))
4497
4498     (gnus-group-set-mode-line)
4499     (setq gnus-group-list-mode (cons level all))
4500     (run-hooks 'gnus-group-prepare-hook)))
4501
4502 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4503   ;; List zombies and killed lists somewhat faster, which was
4504   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4505   ;; this by ignoring the group format specification altogether.
4506   (let (group)
4507     (if regexp
4508         ;; This loop is used when listing groups that match some
4509         ;; regexp.
4510         (while groups
4511           (setq group (pop groups))
4512           (when (string-match regexp group)
4513             (add-text-properties
4514              (point) (prog1 (1+ (point))
4515                        (insert " " mark "     *: " group "\n"))
4516              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4517                    'gnus-unread t
4518                    'gnus-level level))))
4519       ;; This loop is used when listing all groups.
4520       (while groups
4521         (add-text-properties
4522          (point) (prog1 (1+ (point))
4523                    (insert " " mark "     *: "
4524                            (setq group (pop groups)) "\n"))
4525          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4526                'gnus-unread t
4527                'gnus-level level))))))
4528
4529 (defmacro gnus-group-real-name (group)
4530   "Find the real name of a foreign newsgroup."
4531   `(let ((gname ,group))
4532      (if (string-match ":[^:]+$" gname)
4533          (substring gname (1+ (match-beginning 0)))
4534        gname)))
4535
4536 (defsubst gnus-server-add-address (method)
4537   (let ((method-name (symbol-name (car method))))
4538     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4539              (not (assq (intern (concat method-name "-address")) method)))
4540         (append method (list (list (intern (concat method-name "-address"))
4541                                    (nth 1 method))))
4542       method)))
4543
4544 (defsubst gnus-server-get-method (group method)
4545   ;; Input either a server name, and extended server name, or a
4546   ;; select method, and return a select method.
4547   (cond ((stringp method)
4548          (gnus-server-to-method method))
4549         ((equal method gnus-select-method)
4550          gnus-select-method)
4551         ((and (stringp (car method)) group)
4552          (gnus-server-extend-method group method))
4553         ((and method (not group)
4554               (equal (cadr method) ""))
4555          method)
4556         (t
4557          (gnus-server-add-address method))))
4558
4559 (defun gnus-server-to-method (server)
4560   "Map virtual server names to select methods."
4561   (or 
4562    ;; Perhaps this is the native server?
4563    (and (equal server "native") gnus-select-method)
4564    ;; It should be in the server alist.
4565    (cdr (assoc server gnus-server-alist))
4566    ;; If not, we look through all the opened server
4567    ;; to see whether we can find it there.
4568    (let ((opened gnus-opened-servers))
4569      (while (and opened
4570                  (not (equal server (format "%s:%s" (caaar opened)
4571                                             (cadaar opened)))))
4572        (pop opened))
4573      (caar opened))))
4574
4575 (defmacro gnus-method-equal (ss1 ss2)
4576   "Say whether two servers are equal."
4577   `(let ((s1 ,ss1)
4578          (s2 ,ss2))
4579      (or (equal s1 s2)
4580          (and (= (length s1) (length s2))
4581               (progn
4582                 (while (and s1 (member (car s1) s2))
4583                   (setq s1 (cdr s1)))
4584                 (null s1))))))
4585
4586 (defun gnus-server-equal (m1 m2)
4587   "Say whether two methods are equal."
4588   (let ((m1 (cond ((null m1) gnus-select-method)
4589                   ((stringp m1) (gnus-server-to-method m1))
4590                   (t m1)))
4591         (m2 (cond ((null m2) gnus-select-method)
4592                   ((stringp m2) (gnus-server-to-method m2))
4593                   (t m2))))
4594     (gnus-method-equal m1 m2)))
4595
4596 (defun gnus-servers-using-backend (backend)
4597   "Return a list of known servers using BACKEND."
4598   (let ((opened gnus-opened-servers)
4599         out)
4600     (while opened
4601       (when (eq backend (caaar opened))
4602         (push (caar opened) out))
4603       (pop opened))
4604     out))
4605
4606 (defun gnus-group-prefixed-name (group method)
4607   "Return the whole name from GROUP and METHOD."
4608   (and (stringp method) (setq method (gnus-server-to-method method)))
4609   (concat (format "%s" (car method))
4610           (if (and
4611                (or (assoc (format "%s" (car method)) 
4612                           (gnus-methods-using 'address))
4613                    (gnus-server-equal method gnus-message-archive-method))
4614                (nth 1 method)
4615                (not (string= (nth 1 method) "")))
4616               (concat "+" (nth 1 method)))
4617           ":" group))
4618
4619 (defun gnus-group-real-prefix (group)
4620   "Return the prefix of the current group name."
4621   (if (string-match "^[^:]+:" group)
4622       (substring group 0 (match-end 0))
4623     ""))
4624
4625 (defun gnus-group-method (group)
4626   "Return the server or method used for selecting GROUP."
4627   (let ((prefix (gnus-group-real-prefix group)))
4628     (if (equal prefix "")
4629         gnus-select-method
4630       (let ((servers gnus-opened-servers)
4631             (server "")
4632             backend possible found)
4633         (if (string-match "^[^\\+]+\\+" prefix)
4634             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4635                   server (substring prefix (match-end 0) (1- (length prefix))))
4636           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4637         (while servers
4638           (when (eq (caaar servers) backend)
4639             (setq possible (caar servers))
4640             (when (equal (cadaar servers) server)
4641               (setq found (caar servers))))
4642           (pop servers))
4643         (or (car (rassoc found gnus-server-alist))
4644             found
4645             (car (rassoc possible gnus-server-alist))
4646             possible
4647             (list backend server))))))
4648
4649 (defsubst gnus-secondary-method-p (method)
4650   "Return whether METHOD is a secondary select method."
4651   (let ((methods gnus-secondary-select-methods)
4652         (gmethod (gnus-server-get-method nil method)))
4653     (while (and methods
4654                 (not (equal (gnus-server-get-method nil (car methods))
4655                             gmethod)))
4656       (setq methods (cdr methods)))
4657     methods))
4658
4659 (defun gnus-group-foreign-p (group)
4660   "Say whether a group is foreign or not."
4661   (and (not (gnus-group-native-p group))
4662        (not (gnus-group-secondary-p group))))
4663
4664 (defun gnus-group-native-p (group)
4665   "Say whether the group is native or not."
4666   (not (string-match ":" group)))
4667
4668 (defun gnus-group-secondary-p (group)
4669   "Say whether the group is secondary or not."
4670   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4671
4672 (defun gnus-group-get-parameter (group &optional symbol)
4673   "Returns the group parameters for GROUP.
4674 If SYMBOL, return the value of that symbol in the group parameters."
4675   (let ((params (gnus-info-params (gnus-get-info group))))
4676     (if symbol
4677         (gnus-group-parameter-value params symbol)
4678       params)))
4679
4680 (defun gnus-group-parameter-value (params symbol)
4681   "Return the value of SYMBOL in group PARAMS."
4682   (or (car (memq symbol params))        ; It's either a simple symbol
4683       (cdr (assq symbol params))))      ; or a cons.
4684
4685 (defun gnus-group-add-parameter (group param)
4686   "Add parameter PARAM to GROUP."
4687   (let ((info (gnus-get-info group)))
4688     (if (not info)
4689         () ; This is a dead group.  We just ignore it.
4690       ;; Cons the new param to the old one and update.
4691       (gnus-group-set-info (cons param (gnus-info-params info))
4692                            group 'params))))
4693
4694 (defun gnus-group-set-parameter (group name value)
4695   "Set parameter NAME to VALUE in GROUP."
4696   (let ((info (gnus-get-info group)))
4697     (if (not info)
4698         () ; This is a dead group.  We just ignore it.
4699       (let ((old-params (gnus-info-params info))
4700             (new-params (list (cons name value))))
4701         (while old-params
4702           (if (or (not (listp (car old-params)))
4703                   (not (eq (caar old-params) name)))
4704               (setq new-params (append new-params (list (car old-params)))))
4705           (setq old-params (cdr old-params)))
4706         (gnus-group-set-info new-params group 'params)))))
4707
4708 (defun gnus-group-add-score (group &optional score)
4709   "Add SCORE to the GROUP score.
4710 If SCORE is nil, add 1 to the score of GROUP."
4711   (let ((info (gnus-get-info group)))
4712     (when info
4713       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4714
4715 (defun gnus-summary-bubble-group ()
4716   "Increase the score of the current group.
4717 This is a handy function to add to `gnus-summary-exit-hook' to
4718 increase the score of each group you read."
4719   (gnus-group-add-score gnus-newsgroup-name))
4720
4721 (defun gnus-group-set-info (info &optional method-only-group part)
4722   (let* ((entry (gnus-gethash
4723                  (or method-only-group (gnus-info-group info))
4724                  gnus-newsrc-hashtb))
4725          (part-info info)
4726          (info (if method-only-group (nth 2 entry) info))
4727          method)
4728     (when method-only-group
4729       (unless entry
4730         (error "Trying to change non-existent group %s" method-only-group))
4731       ;; We have received parts of the actual group info - either the
4732       ;; select method or the group parameters.  We first check
4733       ;; whether we have to extend the info, and if so, do that.
4734       (let ((len (length info))
4735             (total (if (eq part 'method) 5 6)))
4736         (when (< len total)
4737           (setcdr (nthcdr (1- len) info)
4738                   (make-list (- total len) nil)))
4739         ;; Then we enter the new info.
4740         (setcar (nthcdr (1- total) info) part-info)))
4741     (unless entry
4742       ;; This is a new group, so we just create it.
4743       (save-excursion
4744         (set-buffer gnus-group-buffer)
4745         (setq method (gnus-info-method info))
4746         (when (gnus-server-equal method "native")
4747           (setq method nil))
4748         (if method
4749             ;; It's a foreign group...
4750             (gnus-group-make-group
4751              (gnus-group-real-name (gnus-info-group info))
4752              (if (stringp method) method
4753                (prin1-to-string (car method)))
4754              (and (consp method)
4755                   (nth 1 (gnus-info-method info))))
4756           ;; It's a native group.
4757           (gnus-group-make-group (gnus-info-group info)))
4758         (gnus-message 6 "Note: New group created")
4759         (setq entry
4760               (gnus-gethash (gnus-group-prefixed-name
4761                              (gnus-group-real-name (gnus-info-group info))
4762                              (or (gnus-info-method info) gnus-select-method))
4763                             gnus-newsrc-hashtb))))
4764     ;; Whether it was a new group or not, we now have the entry, so we
4765     ;; can do the update.
4766     (if entry
4767         (progn
4768           (setcar (nthcdr 2 entry) info)
4769           (when (and (not (eq (car entry) t))
4770                      (gnus-active (gnus-info-group info)))
4771             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4772       (error "No such group: %s" (gnus-info-group info)))))
4773
4774 (defun gnus-group-set-method-info (group select-method)
4775   (gnus-group-set-info select-method group 'method))
4776
4777 (defun gnus-group-set-params-info (group params)
4778   (gnus-group-set-info params group 'params))
4779
4780 (defun gnus-group-update-group-line ()
4781   "Update the current line in the group buffer."
4782   (let* ((buffer-read-only nil)
4783          (group (gnus-group-group-name))
4784          (gnus-group-indentation (gnus-group-group-indentation))
4785          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4786     (and entry
4787          (not (gnus-ephemeral-group-p group))
4788          (gnus-dribble-enter
4789           (concat "(gnus-group-set-info '"
4790                   (prin1-to-string (nth 2 entry)) ")")))
4791     (gnus-delete-line)
4792     (gnus-group-insert-group-line-info group)
4793     (forward-line -1)
4794     (gnus-group-position-point)))
4795
4796 (defun gnus-group-insert-group-line-info (group)
4797   "Insert GROUP on the current line."
4798   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4799         active info)
4800     (if entry
4801         (progn
4802           ;; (Un)subscribed group.
4803           (setq info (nth 2 entry))
4804           (gnus-group-insert-group-line
4805            group (gnus-info-level info) (gnus-info-marks info)
4806            (or (car entry) t) (gnus-info-method info)))
4807       ;; This group is dead.
4808       (gnus-group-insert-group-line
4809        group
4810        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4811        nil
4812        (if (setq active (gnus-active group))
4813            (- (1+ (cdr active)) (car active)) 0)
4814        nil))))
4815
4816 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4817                                                     gnus-tmp-marked number
4818                                                     gnus-tmp-method)
4819   "Insert a group line in the group buffer."
4820   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4821          (gnus-tmp-number-total
4822           (if gnus-tmp-active
4823               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4824             0))
4825          (gnus-tmp-number-of-unread
4826           (if (numberp number) (int-to-string (max 0 number))
4827             "*"))
4828          (gnus-tmp-number-of-read
4829           (if (numberp number)
4830               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4831             "*"))
4832          (gnus-tmp-subscribed
4833           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4834                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4835                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4836                 (t ?K)))
4837          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4838          (gnus-tmp-newsgroup-description
4839           (if gnus-description-hashtb
4840               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4841             ""))
4842          (gnus-tmp-moderated
4843           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4844          (gnus-tmp-moderated-string
4845           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4846          (gnus-tmp-method
4847           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4848          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4849          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4850          (gnus-tmp-news-method-string
4851           (if gnus-tmp-method
4852               (format "(%s:%s)" (car gnus-tmp-method)
4853                       (cadr gnus-tmp-method)) ""))
4854          (gnus-tmp-marked-mark
4855           (if (and (numberp number)
4856                    (zerop number)
4857                    (cdr (assq 'tick gnus-tmp-marked)))
4858               ?* ? ))
4859          (gnus-tmp-process-marked
4860           (if (member gnus-tmp-group gnus-group-marked)
4861               gnus-process-mark ? ))
4862          (gnus-tmp-grouplens
4863           (or (and gnus-use-grouplens
4864                    (bbb-grouplens-group-p gnus-tmp-group))
4865               ""))
4866          (buffer-read-only nil)
4867          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4868     (beginning-of-line)
4869     (add-text-properties
4870      (point)
4871      (prog1 (1+ (point))
4872        ;; Insert the text.
4873        (eval gnus-group-line-format-spec))
4874      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4875        gnus-unread ,(if (numberp number)
4876                         (string-to-int gnus-tmp-number-of-unread)
4877                       t)
4878        gnus-marked ,gnus-tmp-marked-mark
4879        gnus-indentation ,gnus-group-indentation
4880        gnus-level ,gnus-tmp-level))
4881     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4882       (forward-line -1)
4883       (run-hooks 'gnus-group-update-hook)
4884       (forward-line))
4885     ;; Allow XEmacs to remove front-sticky text properties.
4886     (gnus-group-remove-excess-properties)))
4887
4888 (defun gnus-group-update-group (group &optional visible-only)
4889   "Update all lines where GROUP appear.
4890 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4891 already."
4892   (save-excursion
4893     (set-buffer gnus-group-buffer)
4894     ;; The buffer may be narrowed.
4895     (save-restriction
4896       (widen)
4897       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4898             (loc (point-min))
4899             found buffer-read-only)
4900         ;; Enter the current status into the dribble buffer.
4901         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4902           (if (and entry (not (gnus-ephemeral-group-p group)))
4903               (gnus-dribble-enter
4904                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4905                        ")"))))
4906         ;; Find all group instances.  If topics are in use, each group
4907         ;; may be listed in more than once.
4908         (while (setq loc (text-property-any
4909                           loc (point-max) 'gnus-group ident))
4910           (setq found t)
4911           (goto-char loc)
4912           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4913             (gnus-delete-line)
4914             (gnus-group-insert-group-line-info group))
4915           (setq loc (1+ loc)))
4916         (unless (or found visible-only)
4917           ;; No such line in the buffer, find out where it's supposed to
4918           ;; go, and insert it there (or at the end of the buffer).
4919           (if gnus-goto-missing-group-function
4920               (funcall gnus-goto-missing-group-function group)
4921             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4922               (while (and entry (car entry)
4923                           (not
4924                            (gnus-goto-char
4925                             (text-property-any
4926                              (point-min) (point-max)
4927                              'gnus-group (gnus-intern-safe
4928                                           (caar entry) gnus-active-hashtb)))))
4929                 (setq entry (cdr entry)))
4930               (or entry (goto-char (point-max)))))
4931           ;; Finally insert the line.
4932           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4933             (gnus-group-insert-group-line-info group)))
4934         (gnus-group-set-mode-line)))))
4935
4936 (defun gnus-group-set-mode-line ()
4937   (when (memq 'group gnus-updated-mode-lines)
4938     (let* ((gformat (or gnus-group-mode-line-format-spec
4939                         (setq gnus-group-mode-line-format-spec
4940                               (gnus-parse-format
4941                                gnus-group-mode-line-format
4942                                gnus-group-mode-line-format-alist))))
4943            (gnus-tmp-news-server (cadr gnus-select-method))
4944            (gnus-tmp-news-method (car gnus-select-method))
4945            (max-len 60)
4946            gnus-tmp-header                      ;Dummy binding for user-defined formats
4947            ;; Get the resulting string.
4948            (mode-string (eval gformat)))
4949       ;; If the line is too long, we chop it off.
4950       (when (> (length mode-string) max-len)
4951         (setq mode-string (substring mode-string 0 (- max-len 4))))
4952       (prog1
4953           (setq mode-line-buffer-identification (list mode-string))
4954         (set-buffer-modified-p t)))))
4955
4956 (defun gnus-group-group-name ()
4957   "Get the name of the newsgroup on the current line."
4958   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4959     (and group (symbol-name group))))
4960
4961 (defun gnus-group-group-level ()
4962   "Get the level of the newsgroup on the current line."
4963   (get-text-property (gnus-point-at-bol) 'gnus-level))
4964
4965 (defun gnus-group-group-indentation ()
4966   "Get the indentation of the newsgroup on the current line."
4967   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4968       (and gnus-group-indentation-function
4969            (funcall gnus-group-indentation-function))
4970       ""))
4971
4972 (defun gnus-group-group-unread ()
4973   "Get the number of unread articles of the newsgroup on the current line."
4974   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4975
4976 (defun gnus-group-search-forward (&optional backward all level first-too)
4977   "Find the next newsgroup with unread articles.
4978 If BACKWARD is non-nil, find the previous newsgroup instead.
4979 If ALL is non-nil, just find any newsgroup.
4980 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4981 group exists.
4982 If FIRST-TOO, the current line is also eligible as a target."
4983   (let ((way (if backward -1 1))
4984         (low gnus-level-killed)
4985         (beg (point))
4986         pos found lev)
4987     (if (and backward (progn (beginning-of-line)) (bobp))
4988         nil
4989       (or first-too (forward-line way))
4990       (while (and
4991               (not (eobp))
4992               (not (setq
4993                     found
4994                     (and (or all
4995                              (and
4996                               (let ((unread
4997                                      (get-text-property (point) 'gnus-unread)))
4998                                 (and (numberp unread) (> unread 0)))
4999                               (setq lev (get-text-property (point)
5000                                                            'gnus-level))
5001                               (<= lev gnus-level-subscribed)))
5002                          (or (not level)
5003                              (and (setq lev (get-text-property (point)
5004                                                                'gnus-level))
5005                                   (or (= lev level)
5006                                       (and (< lev low)
5007                                            (< level lev)
5008                                            (progn
5009                                              (setq low lev)
5010                                              (setq pos (point))
5011                                              nil))))))))
5012               (zerop (forward-line way)))))
5013     (if found
5014         (progn (gnus-group-position-point) t)
5015       (goto-char (or pos beg))
5016       (and pos t))))
5017
5018 ;;; Gnus group mode commands
5019
5020 ;; Group marking.
5021
5022 (defun gnus-group-mark-group (n &optional unmark no-advance)
5023   "Mark the current group."
5024   (interactive "p")
5025   (let ((buffer-read-only nil)
5026         group)
5027     (while
5028         (and (> n 0)
5029              (setq group (gnus-group-group-name))
5030              (progn
5031                (beginning-of-line)
5032                (forward-char
5033                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5034                (delete-char 1)
5035                (if unmark
5036                    (progn
5037                      (insert " ")
5038                      (setq gnus-group-marked (delete group gnus-group-marked)))
5039                  (insert "#")
5040                  (setq gnus-group-marked
5041                        (cons group (delete group gnus-group-marked))))
5042                t)
5043              (or no-advance (zerop (gnus-group-next-group 1))))
5044       (setq n (1- n)))
5045     (gnus-summary-position-point)
5046     n))
5047
5048 (defun gnus-group-unmark-group (n)
5049   "Remove the mark from the current group."
5050   (interactive "p")
5051   (gnus-group-mark-group n 'unmark)
5052   (gnus-group-position-point))
5053
5054 (defun gnus-group-unmark-all-groups ()
5055   "Unmark all groups."
5056   (interactive)
5057   (let ((groups gnus-group-marked))
5058     (save-excursion
5059       (while groups
5060         (gnus-group-remove-mark (pop groups)))))
5061   (gnus-group-position-point))
5062
5063 (defun gnus-group-mark-region (unmark beg end)
5064   "Mark all groups between point and mark.
5065 If UNMARK, remove the mark instead."
5066   (interactive "P\nr")
5067   (let ((num (count-lines beg end)))
5068     (save-excursion
5069       (goto-char beg)
5070       (- num (gnus-group-mark-group num unmark)))))
5071
5072 (defun gnus-group-mark-buffer (&optional unmark)
5073   "Mark all groups in the buffer.
5074 If UNMARK, remove the mark instead."
5075   (interactive "P")
5076   (gnus-group-mark-region unmark (point-min) (point-max)))
5077
5078 (defun gnus-group-mark-regexp (regexp)
5079   "Mark all groups that match some regexp."
5080   (interactive "sMark (regexp): ")
5081   (let ((alist (cdr gnus-newsrc-alist))
5082         group)
5083     (while alist
5084       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5085         (gnus-group-set-mark group))))
5086   (gnus-group-position-point))
5087
5088 (defun gnus-group-remove-mark (group)
5089   "Remove the process mark from GROUP and move point there.
5090 Return nil if the group isn't displayed."
5091   (if (gnus-group-goto-group group)
5092       (save-excursion
5093         (gnus-group-mark-group 1 'unmark t)
5094         t)
5095     (setq gnus-group-marked
5096           (delete group gnus-group-marked))
5097     nil))
5098
5099 (defun gnus-group-set-mark (group)
5100   "Set the process mark on GROUP."
5101   (if (gnus-group-goto-group group) 
5102       (save-excursion
5103         (gnus-group-mark-group 1 nil t))
5104     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5105
5106 (defun gnus-group-universal-argument (arg &optional groups func)
5107   "Perform any command on all groups accoring to the process/prefix convention."
5108   (interactive "P")
5109   (let ((groups (or groups (gnus-group-process-prefix arg)))
5110         group func)
5111     (if (eq (setq func (or func
5112                            (key-binding
5113                             (read-key-sequence
5114                              (substitute-command-keys
5115                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5116             'undefined)
5117         (progn
5118           (message "Undefined key")
5119           (ding))
5120       (while groups
5121         (gnus-group-remove-mark (setq group (pop groups)))
5122         (command-execute func))))
5123   (gnus-group-position-point))
5124
5125 (defun gnus-group-process-prefix (n)
5126   "Return a list of groups to work on.
5127 Take into consideration N (the prefix) and the list of marked groups."
5128   (cond
5129    (n
5130     (setq n (prefix-numeric-value n))
5131     ;; There is a prefix, so we return a list of the N next
5132     ;; groups.
5133     (let ((way (if (< n 0) -1 1))
5134           (n (abs n))
5135           group groups)
5136       (save-excursion
5137         (while (and (> n 0)
5138                     (setq group (gnus-group-group-name)))
5139           (setq groups (cons group groups))
5140           (setq n (1- n))
5141           (gnus-group-next-group way)))
5142       (nreverse groups)))
5143    ((and (boundp 'transient-mark-mode)
5144          transient-mark-mode
5145          mark-active)
5146     ;; Work on the region between point and mark.
5147     (let ((max (max (point) (mark)))
5148           groups)
5149       (save-excursion
5150         (goto-char (min (point) (mark)))
5151         (while
5152             (and
5153              (push (gnus-group-group-name) groups)
5154              (zerop (gnus-group-next-group 1))
5155              (< (point) max)))
5156         (nreverse groups))))
5157    (gnus-group-marked
5158     ;; No prefix, but a list of marked articles.
5159     (reverse gnus-group-marked))
5160    (t
5161     ;; Neither marked articles or a prefix, so we return the
5162     ;; current group.
5163     (let ((group (gnus-group-group-name)))
5164       (and group (list group))))))
5165
5166 ;; Selecting groups.
5167
5168 (defun gnus-group-read-group (&optional all no-article group)
5169   "Read news in this newsgroup.
5170 If the prefix argument ALL is non-nil, already read articles become
5171 readable.  IF ALL is a number, fetch this number of articles.  If the
5172 optional argument NO-ARTICLE is non-nil, no article will be
5173 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5174 group."
5175   (interactive "P")
5176   (let ((group (or group (gnus-group-group-name)))
5177         number active marked entry)
5178     (or group (error "No group on current line"))
5179     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5180                                             group gnus-newsrc-hashtb)))))
5181     ;; This group might be a dead group.  In that case we have to get
5182     ;; the number of unread articles from `gnus-active-hashtb'.
5183     (setq number
5184           (cond ((numberp all) all)
5185                 (entry (car entry))
5186                 ((setq active (gnus-active group))
5187                  (- (1+ (cdr active)) (car active)))))
5188     (gnus-summary-read-group
5189      group (or all (and (numberp number)
5190                         (zerop (+ number (length (cdr (assq 'tick marked)))
5191                                   (length (cdr (assq 'dormant marked)))))))
5192      no-article)))
5193
5194 (defun gnus-group-select-group (&optional all)
5195   "Select this newsgroup.
5196 No article is selected automatically.
5197 If ALL is non-nil, already read articles become readable.
5198 If ALL is a number, fetch this number of articles."
5199   (interactive "P")
5200   (gnus-group-read-group all t))
5201
5202 (defun gnus-group-quick-select-group (&optional all)
5203   "Select the current group \"quickly\".
5204 This means that no highlighting or scoring will be performed."
5205   (interactive "P")
5206   (let (gnus-visual
5207         gnus-score-find-score-files-function
5208         gnus-apply-kill-hook
5209         gnus-summary-expunge-below)
5210     (gnus-group-read-group all t)))
5211
5212 (defun gnus-group-visible-select-group (&optional all)
5213   "Select the current group without hiding any articles."
5214   (interactive "P")
5215   (let ((gnus-inhibit-limiting t))
5216     (gnus-group-read-group all t)))
5217
5218 ;;;###autoload
5219 (defun gnus-fetch-group (group)
5220   "Start Gnus if necessary and enter GROUP.
5221 Returns whether the fetching was successful or not."
5222   (interactive "sGroup name: ")
5223   (or (get-buffer gnus-group-buffer)
5224       (gnus))
5225   (gnus-group-read-group nil nil group))
5226
5227 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5228 ;; if selection was successful.
5229 (defun gnus-group-read-ephemeral-group
5230   (group method &optional activate quit-config)
5231   (let ((group (if (gnus-group-foreign-p group) group
5232                  (gnus-group-prefixed-name group method))))
5233     (gnus-sethash
5234      group
5235      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5236                      ((quit-config . ,(if quit-config quit-config
5237                                         (cons (current-buffer) 'summary))))))
5238      gnus-newsrc-hashtb)
5239     (set-buffer gnus-group-buffer)
5240     (or (gnus-check-server method)
5241         (error "Unable to contact server: %s" (gnus-status-message method)))
5242     (if activate (or (gnus-request-group group)
5243                      (error "Couldn't request group")))
5244     (condition-case ()
5245         (gnus-group-read-group t t group)
5246       (error nil)
5247       (quit nil))))
5248
5249 (defun gnus-group-jump-to-group (group)
5250   "Jump to newsgroup GROUP."
5251   (interactive
5252    (list (completing-read
5253           "Group: " gnus-active-hashtb nil
5254           (memq gnus-select-method gnus-have-read-active-file))))
5255
5256   (if (equal group "")
5257       (error "Empty group name"))
5258
5259   (let ((b (text-property-any
5260             (point-min) (point-max)
5261             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5262     (unless (gnus-ephemeral-group-p group)
5263       (if b
5264           ;; Either go to the line in the group buffer...
5265           (goto-char b)
5266         ;; ... or insert the line.
5267         (or
5268          (gnus-active group)
5269          (gnus-activate-group group)
5270          (error "%s error: %s" group (gnus-status-message group)))
5271
5272         (gnus-group-update-group group)
5273         (goto-char (text-property-any
5274                     (point-min) (point-max)
5275                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5276     ;; Adjust cursor point.
5277     (gnus-group-position-point)))
5278
5279 (defun gnus-group-goto-group (group)
5280   "Goto to newsgroup GROUP."
5281   (when group
5282     (let ((b (text-property-any (point-min) (point-max)
5283                                 'gnus-group (gnus-intern-safe
5284                                              group gnus-active-hashtb))))
5285       (and b (goto-char b)))))
5286
5287 (defun gnus-group-next-group (n)
5288   "Go to next N'th newsgroup.
5289 If N is negative, search backward instead.
5290 Returns the difference between N and the number of skips actually
5291 done."
5292   (interactive "p")
5293   (gnus-group-next-unread-group n t))
5294
5295 (defun gnus-group-next-unread-group (n &optional all level)
5296   "Go to next N'th unread newsgroup.
5297 If N is negative, search backward instead.
5298 If ALL is non-nil, choose any newsgroup, unread or not.
5299 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5300 such group can be found, the next group with a level higher than
5301 LEVEL.
5302 Returns the difference between N and the number of skips actually
5303 made."
5304   (interactive "p")
5305   (let ((backward (< n 0))
5306         (n (abs n)))
5307     (while (and (> n 0)
5308                 (gnus-group-search-forward
5309                  backward (or (not gnus-group-goto-unread) all) level))
5310       (setq n (1- n)))
5311     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5312                                (if level " on this level or higher" "")))
5313     n))
5314
5315 (defun gnus-group-prev-group (n)
5316   "Go to previous N'th newsgroup.
5317 Returns the difference between N and the number of skips actually
5318 done."
5319   (interactive "p")
5320   (gnus-group-next-unread-group (- n) t))
5321
5322 (defun gnus-group-prev-unread-group (n)
5323   "Go to previous N'th unread newsgroup.
5324 Returns the difference between N and the number of skips actually
5325 done."
5326   (interactive "p")
5327   (gnus-group-next-unread-group (- n)))
5328
5329 (defun gnus-group-next-unread-group-same-level (n)
5330   "Go to next N'th unread newsgroup on the same level.
5331 If N is negative, search backward instead.
5332 Returns the difference between N and the number of skips actually
5333 done."
5334   (interactive "p")
5335   (gnus-group-next-unread-group n t (gnus-group-group-level))
5336   (gnus-group-position-point))
5337
5338 (defun gnus-group-prev-unread-group-same-level (n)
5339   "Go to next N'th unread newsgroup on the same level.
5340 Returns the difference between N and the number of skips actually
5341 done."
5342   (interactive "p")
5343   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5344   (gnus-group-position-point))
5345
5346 (defun gnus-group-best-unread-group (&optional exclude-group)
5347   "Go to the group with the highest level.
5348 If EXCLUDE-GROUP, do not go to that group."
5349   (interactive)
5350   (goto-char (point-min))
5351   (let ((best 100000)
5352         unread best-point)
5353     (while (not (eobp))
5354       (setq unread (get-text-property (point) 'gnus-unread))
5355       (if (and (numberp unread) (> unread 0))
5356           (progn
5357             (if (and (get-text-property (point) 'gnus-level)
5358                      (< (get-text-property (point) 'gnus-level) best)
5359                      (or (not exclude-group)
5360                          (not (equal exclude-group (gnus-group-group-name)))))
5361                 (progn
5362                   (setq best (get-text-property (point) 'gnus-level))
5363                   (setq best-point (point))))))
5364       (forward-line 1))
5365     (if best-point (goto-char best-point))
5366     (gnus-summary-position-point)
5367     (and best-point (gnus-group-group-name))))
5368
5369 (defun gnus-group-first-unread-group ()
5370   "Go to the first group with unread articles."
5371   (interactive)
5372   (prog1
5373       (let ((opoint (point))
5374             unread)
5375         (goto-char (point-min))
5376         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5377                 (and (numberp unread)   ; Not a topic.
5378                      (not (zerop unread))) ; Has unread articles.
5379                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5380             (point)                     ; Success.
5381           (goto-char opoint)
5382           nil))                         ; Not success.
5383     (gnus-group-position-point)))
5384
5385 (defun gnus-group-enter-server-mode ()
5386   "Jump to the server buffer."
5387   (interactive)
5388   (gnus-enter-server-buffer))
5389
5390 (defun gnus-group-make-group (name &optional method address)
5391   "Add a new newsgroup.
5392 The user will be prompted for a NAME, for a select METHOD, and an
5393 ADDRESS."
5394   (interactive
5395    (cons
5396     (read-string "Group name: ")
5397     (let ((method
5398            (completing-read
5399             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5400             nil t)))
5401       (cond ((assoc method gnus-valid-select-methods)
5402              (list method
5403                    (if (memq 'prompt-address
5404                              (assoc method gnus-valid-select-methods))
5405                        (read-string "Address: ")
5406                      "")))
5407             ((assoc method gnus-server-alist)
5408              (list method))
5409             (t
5410              (list method ""))))))
5411
5412   (save-excursion
5413     (set-buffer gnus-group-buffer)
5414     (let* ((meth (and method (if address (list (intern method) address)
5415                                method)))
5416            (nname (if method (gnus-group-prefixed-name name meth) name))
5417            backend info)
5418       (and (gnus-gethash nname gnus-newsrc-hashtb)
5419            (error "Group %s already exists" nname))
5420       (gnus-group-change-level
5421        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5422        gnus-level-default-subscribed gnus-level-killed
5423        (and (gnus-group-group-name)
5424             (gnus-gethash (gnus-group-group-name)
5425                           gnus-newsrc-hashtb))
5426        t)
5427       (gnus-set-active nname (cons 1 0))
5428       (or (gnus-ephemeral-group-p name)
5429           (gnus-dribble-enter
5430            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5431       (gnus-group-insert-group-line-info nname)
5432
5433       (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5434                                                     nil meth))))
5435                    gnus-valid-select-methods)
5436         (require backend))
5437       (gnus-check-server meth)
5438       (and (gnus-check-backend-function 'request-create-group nname)
5439            (gnus-request-create-group nname))
5440       t)))
5441
5442 (defun gnus-group-delete-group (group &optional force)
5443   "Delete the current group.
5444 If FORCE (the prefix) is non-nil, all the articles in the group will
5445 be deleted.  This is \"deleted\" as in \"removed forever from the face
5446 of the Earth\".  There is no undo."
5447   (interactive
5448    (list (gnus-group-group-name)
5449          current-prefix-arg))
5450   (or group (error "No group to rename"))
5451   (or (gnus-check-backend-function 'request-delete-group group)
5452       (error "This backend does not support group deletion"))
5453   (prog1
5454       (if (not (gnus-yes-or-no-p
5455                 (format
5456                  "Do you really want to delete %s%s? "
5457                  group (if force " and all its contents" ""))))
5458           () ; Whew!
5459         (gnus-message 6 "Deleting group %s..." group)
5460         (if (not (gnus-request-delete-group group force))
5461             (progn
5462               (gnus-message 3 "Couldn't delete group %s" group)
5463               (ding))
5464           (gnus-message 6 "Deleting group %s...done" group)
5465           (gnus-group-goto-group group)
5466           (gnus-group-kill-group 1 t)
5467           (gnus-sethash group nil gnus-active-hashtb)
5468           t))
5469     (gnus-group-position-point)))
5470
5471 (defun gnus-group-rename-group (group new-name)
5472   (interactive
5473    (list
5474     (gnus-group-group-name)
5475     (progn
5476       (or (gnus-check-backend-function
5477            'request-rename-group (gnus-group-group-name))
5478           (error "This backend does not support renaming groups"))
5479       (read-string "New group name: "))))
5480
5481   (or (gnus-check-backend-function 'request-rename-group group)
5482       (error "This backend does not support renaming groups"))
5483
5484   (or group (error "No group to rename"))
5485   (and (string-match "^[ \t]*$" new-name)
5486        (error "Not a valid group name"))
5487
5488   ;; We find the proper prefixed name.
5489   (setq new-name
5490         (gnus-group-prefixed-name
5491          (gnus-group-real-name new-name)
5492          (gnus-info-method (gnus-get-info group))))
5493
5494   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5495   (prog1
5496       (if (not (gnus-request-rename-group group new-name))
5497           (progn
5498             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5499             (ding))
5500         ;; We rename the group internally by killing it...
5501         (gnus-group-goto-group group)
5502         (gnus-group-kill-group)
5503         ;; ... changing its name ...
5504         (setcar (cdar gnus-list-of-killed-groups) new-name)
5505         ;; ... and then yanking it.  Magic!
5506         (gnus-group-yank-group)
5507         (gnus-set-active new-name (gnus-active group))
5508         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5509         new-name)
5510     (gnus-group-position-point)))
5511
5512 (defun gnus-group-edit-group (group &optional part)
5513   "Edit the group on the current line."
5514   (interactive (list (gnus-group-group-name)))
5515   (let* ((part (or part 'info))
5516          (done-func `(lambda ()
5517                        "Exit editing mode and update the information."
5518                        (interactive)
5519                        (gnus-group-edit-group-done ',part ,group)))
5520          (winconf (current-window-configuration))
5521          info)
5522     (or group (error "No group on current line"))
5523     (or (setq info (gnus-get-info group))
5524         (error "Killed group; can't be edited"))
5525     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5526     (gnus-configure-windows 'edit-group)
5527     (gnus-add-current-to-buffer-list)
5528     (emacs-lisp-mode)
5529     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5530     (use-local-map (copy-keymap emacs-lisp-mode-map))
5531     (local-set-key "\C-c\C-c" done-func)
5532     (make-local-variable 'gnus-prev-winconf)
5533     (setq gnus-prev-winconf winconf)
5534     (erase-buffer)
5535     (insert
5536      (cond
5537       ((eq part 'method)
5538        ";; Type `C-c C-c' after editing the select method.\n\n")
5539       ((eq part 'params)
5540        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5541       ((eq part 'info)
5542        ";; Type `C-c C-c' after editing the group info.\n\n")))
5543     (insert
5544      (pp-to-string
5545       (cond ((eq part 'method)
5546              (or (gnus-info-method info) "native"))
5547             ((eq part 'params)
5548              (gnus-info-params info))
5549             (t info)))
5550      "\n")))
5551
5552 (defun gnus-group-edit-group-method (group)
5553   "Edit the select method of GROUP."
5554   (interactive (list (gnus-group-group-name)))
5555   (gnus-group-edit-group group 'method))
5556
5557 (defun gnus-group-edit-group-parameters (group)
5558   "Edit the group parameters of GROUP."
5559   (interactive (list (gnus-group-group-name)))
5560   (gnus-group-edit-group group 'params))
5561
5562 (defun gnus-group-edit-group-done (part group)
5563   "Get info from buffer, update variables and jump to the group buffer."
5564   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5565   (goto-char (point-min))
5566   (let* ((form (read (current-buffer)))
5567          (winconf gnus-prev-winconf)
5568          (method (cond ((eq part 'info) (nth 4 form))
5569                        ((eq part 'method) form)
5570                        (t nil)))
5571          (info (cond ((eq part 'info) form)
5572                      ((eq part 'method) (gnus-get-info group))
5573                      (t nil)))
5574          (new-group (if info
5575                       (if (or (not method)
5576                               (gnus-server-equal
5577                                gnus-select-method method))
5578                           (gnus-group-real-name (car info))
5579                         (gnus-group-prefixed-name
5580                          (gnus-group-real-name (car info)) method))
5581                       nil)))
5582     (when (and new-group
5583                (not (equal new-group group)))
5584       (when (gnus-group-goto-group group)
5585         (gnus-group-kill-group 1))
5586       (gnus-activate-group new-group))
5587     ;; Set the info.
5588     (if (and info new-group)
5589         (progn
5590           (setq info (gnus-copy-sequence info))
5591           (setcar info new-group)
5592           (unless (gnus-server-equal method "native")
5593             (unless (nthcdr 3 info)
5594               (nconc info (list nil nil)))
5595             (unless (nthcdr 4 info)
5596               (nconc info (list nil)))
5597             (gnus-info-set-method info method))
5598           (gnus-group-set-info info))
5599       (gnus-group-set-info form (or new-group group) part))
5600     (kill-buffer (current-buffer))
5601     (and winconf (set-window-configuration winconf))
5602     (set-buffer gnus-group-buffer)
5603     (gnus-group-update-group (or new-group group))
5604     (gnus-group-position-point)))
5605
5606 (defun gnus-group-make-help-group ()
5607   "Create the Gnus documentation group."
5608   (interactive)
5609   (let ((path load-path)
5610         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5611         file dir)
5612     (and (gnus-gethash name gnus-newsrc-hashtb)
5613          (error "Documentation group already exists"))
5614     (while path
5615       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5616             file nil)
5617       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5618                 (file-exists-p
5619                  (setq file (concat (file-name-directory
5620                                      (directory-file-name dir))
5621                                     "etc/gnus-tut.txt"))))
5622         (setq path nil)))
5623     (if (not file)
5624         (message "Couldn't find doc group")
5625       (gnus-group-make-group
5626        (gnus-group-real-name name)
5627        (list 'nndoc "gnus-help"
5628              (list 'nndoc-address file)
5629              (list 'nndoc-article-type 'mbox)))))
5630   (gnus-group-position-point))
5631
5632 (defun gnus-group-make-doc-group (file type)
5633   "Create a group that uses a single file as the source."
5634   (interactive
5635    (list (read-file-name "File name: ")
5636          (and current-prefix-arg 'ask)))
5637   (when (eq type 'ask)
5638     (let ((err "")
5639           char found)
5640       (while (not found)
5641         (message
5642          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5643          err)
5644         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5645                           ((= char ?b) 'babyl)
5646                           ((= char ?d) 'digest)
5647                           ((= char ?f) 'forward)
5648                           ((= char ?a) 'mmfd)
5649                           (t (setq err (format "%c unknown. " char))
5650                              nil))))
5651       (setq type found)))
5652   (let* ((file (expand-file-name file))
5653          (name (gnus-generate-new-group-name
5654                 (gnus-group-prefixed-name
5655                  (file-name-nondirectory file) '(nndoc "")))))
5656     (gnus-group-make-group
5657      (gnus-group-real-name name)
5658      (list 'nndoc (file-name-nondirectory file)
5659            (list 'nndoc-address file)
5660            (list 'nndoc-article-type (or type 'guess))))
5661     (forward-line -1)
5662     (gnus-group-position-point)))
5663
5664 (defun gnus-group-make-archive-group (&optional all)
5665   "Create the (ding) Gnus archive group of the most recent articles.
5666 Given a prefix, create a full group."
5667   (interactive "P")
5668   (let ((group (gnus-group-prefixed-name
5669                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5670     (and (gnus-gethash group gnus-newsrc-hashtb)
5671          (error "Archive group already exists"))
5672     (gnus-group-make-group
5673      (gnus-group-real-name group)
5674      (list 'nndir (if all "hpc" "edu")
5675            (list 'nndir-directory
5676                  (if all gnus-group-archive-directory
5677                    gnus-group-recent-archive-directory)))))
5678   (forward-line -1)
5679   (gnus-group-position-point))
5680
5681 (defun gnus-group-make-directory-group (dir)
5682   "Create an nndir group.
5683 The user will be prompted for a directory.  The contents of this
5684 directory will be used as a newsgroup.  The directory should contain
5685 mail messages or news articles in files that have numeric names."
5686   (interactive
5687    (list (read-file-name "Create group from directory: ")))
5688   (or (file-exists-p dir) (error "No such directory"))
5689   (or (file-directory-p dir) (error "Not a directory"))
5690   (let ((ext "")
5691         (i 0)
5692         group)
5693     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5694       (setq group
5695             (gnus-group-prefixed-name
5696              (concat (file-name-as-directory (directory-file-name dir))
5697                      ext)
5698              '(nndir "")))
5699       (setq ext (format "<%d>" (setq i (1+ i)))))
5700     (gnus-group-make-group
5701      (gnus-group-real-name group)
5702      (list 'nndir group (list 'nndir-directory dir))))
5703   (forward-line -1)
5704   (gnus-group-position-point))
5705
5706 (defun gnus-group-make-kiboze-group (group address scores)
5707   "Create an nnkiboze group.
5708 The user will be prompted for a name, a regexp to match groups, and
5709 score file entries for articles to include in the group."
5710   (interactive
5711    (list
5712     (read-string "nnkiboze group name: ")
5713     (read-string "Source groups (regexp): ")
5714     (let ((headers (mapcar (lambda (group) (list group))
5715                            '("subject" "from" "number" "date" "message-id"
5716                              "references" "chars" "lines" "xref"
5717                              "followup" "all" "body" "head")))
5718           scores header regexp regexps)
5719       (while (not (equal "" (setq header (completing-read
5720                                           "Match on header: " headers nil t))))
5721         (setq regexps nil)
5722         (while (not (equal "" (setq regexp (read-string
5723                                             (format "Match on %s (string): "
5724                                                     header)))))
5725           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5726         (setq scores (cons (cons header regexps) scores)))
5727       scores)))
5728   (gnus-group-make-group group "nnkiboze" address)
5729   (save-excursion
5730     (gnus-set-work-buffer)
5731     (let (emacs-lisp-mode-hook)
5732       (pp scores (current-buffer)))
5733     (write-region (point-min) (point-max)
5734                   (gnus-score-file-name (concat "nnkiboze:" group))))
5735   (forward-line -1)
5736   (gnus-group-position-point))
5737
5738 (defun gnus-group-add-to-virtual (n vgroup)
5739   "Add the current group to a virtual group."
5740   (interactive
5741    (list current-prefix-arg
5742          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5743                           "nnvirtual:")))
5744   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5745       (error "%s is not an nnvirtual group" vgroup))
5746   (let* ((groups (gnus-group-process-prefix n))
5747          (method (gnus-info-method (gnus-get-info vgroup))))
5748     (setcar (cdr method)
5749             (concat
5750              (nth 1 method) "\\|"
5751              (mapconcat
5752               (lambda (s)
5753                 (gnus-group-remove-mark s)
5754                 (concat "\\(^" (regexp-quote s) "$\\)"))
5755               groups "\\|"))))
5756   (gnus-group-position-point))
5757
5758 (defun gnus-group-make-empty-virtual (group)
5759   "Create a new, fresh, empty virtual group."
5760   (interactive "sCreate new, empty virtual group: ")
5761   (let* ((method (list 'nnvirtual "^$"))
5762          (pgroup (gnus-group-prefixed-name group method)))
5763     ;; Check whether it exists already.
5764     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5765          (error "Group %s already exists." pgroup))
5766     ;; Subscribe the new group after the group on the current line.
5767     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5768     (gnus-group-update-group pgroup)
5769     (forward-line -1)
5770     (gnus-group-position-point)))
5771
5772 (defun gnus-group-enter-directory (dir)
5773   "Enter an ephemeral nneething group."
5774   (interactive "DDirectory to read: ")
5775   (let* ((method (list 'nneething dir))
5776          (leaf (gnus-group-prefixed-name
5777                 (file-name-nondirectory (directory-file-name dir))
5778                 method))
5779          (name (gnus-generate-new-group-name leaf)))
5780     (let ((nneething-read-only t))
5781       (or (gnus-group-read-ephemeral-group
5782            name method t
5783            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5784                                       'summary 'group)))
5785           (error "Couldn't enter %s" dir)))))
5786
5787 ;; Group sorting commands
5788 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5789
5790 (defun gnus-group-sort-groups (func &optional reverse)
5791   "Sort the group buffer according to FUNC.
5792 If REVERSE, reverse the sorting order."
5793   (interactive (list gnus-group-sort-function
5794                      current-prefix-arg))
5795   (let ((func (cond 
5796                ((not (listp func)) func)
5797                ((null func) func)
5798                ((= 1 (length func)) (car func))
5799                (t `(lambda (t1 t2)
5800                      ,(gnus-make-sort-function 
5801                        (reverse func)))))))
5802     ;; We peel off the dummy group from the alist.
5803     (when func
5804       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5805         (pop gnus-newsrc-alist))
5806       ;; Do the sorting.
5807       (setq gnus-newsrc-alist
5808             (sort gnus-newsrc-alist func))
5809       (when reverse
5810         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5811       ;; Regenerate the hash table.
5812       (gnus-make-hashtable-from-newsrc-alist)
5813       (gnus-group-list-groups))))
5814
5815 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5816   "Sort the group buffer alphabetically by group name.
5817 If REVERSE, sort in reverse order."
5818   (interactive "P")
5819   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5820
5821 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5822   "Sort the group buffer by number of unread articles.
5823 If REVERSE, sort in reverse order."
5824   (interactive "P")
5825   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5826
5827 (defun gnus-group-sort-groups-by-level (&optional reverse)
5828   "Sort the group buffer by group level.
5829 If REVERSE, sort in reverse order."
5830   (interactive "P")
5831   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5832
5833 (defun gnus-group-sort-groups-by-score (&optional reverse)
5834   "Sort the group buffer by group score.
5835 If REVERSE, sort in reverse order."
5836   (interactive "P")
5837   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5838
5839 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5840   "Sort the group buffer by group rank.
5841 If REVERSE, sort in reverse order."
5842   (interactive "P")
5843   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5844
5845 (defun gnus-group-sort-groups-by-method (&optional reverse)
5846   "Sort the group buffer alphabetically by backend name.
5847 If REVERSE, sort in reverse order."
5848   (interactive "P")
5849   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5850
5851 (defun gnus-group-sort-by-alphabet (info1 info2)
5852   "Sort alphabetically."
5853   (string< (gnus-info-group info1) (gnus-info-group info2)))
5854
5855 (defun gnus-group-sort-by-unread (info1 info2)
5856   "Sort by number of unread articles."
5857   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5858         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5859     (< (or (and (numberp n1) n1) 0)
5860        (or (and (numberp n2) n2) 0))))
5861
5862 (defun gnus-group-sort-by-level (info1 info2)
5863   "Sort by level."
5864   (< (gnus-info-level info1) (gnus-info-level info2)))
5865
5866 (defun gnus-group-sort-by-method (info1 info2)
5867   "Sort alphabetically by backend name."
5868   (string< (symbol-name (car (gnus-find-method-for-group
5869                               (gnus-info-group info1) info1)))
5870            (symbol-name (car (gnus-find-method-for-group
5871                               (gnus-info-group info2) info2)))))
5872
5873 (defun gnus-group-sort-by-score (info1 info2)
5874   "Sort by group score."
5875   (< (gnus-info-score info1) (gnus-info-score info2)))
5876
5877 (defun gnus-group-sort-by-rank (info1 info2)
5878   "Sort by level and score."
5879   (let ((level1 (gnus-info-level info1))
5880         (level2 (gnus-info-level info2)))
5881     (or (< level1 level2)
5882         (and (= level1 level2)
5883              (< (gnus-info-score info1) (gnus-info-score info2))))))
5884
5885 ;; Group catching up.
5886
5887 (defun gnus-group-clear-data (n)
5888   "Clear all marks and read ranges from the current group."
5889   (interactive "P")
5890   (let ((groups (gnus-group-process-prefix n))
5891         group info)
5892     (while (setq group (pop groups))
5893       (setq info (gnus-get-info group))
5894       (gnus-info-set-read info nil)
5895       (when (gnus-info-marks info)
5896         (gnus-info-set-marks info nil))
5897       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5898       (when (gnus-group-goto-group group)
5899         (gnus-group-remove-mark group)
5900         (gnus-group-update-group-line)))))
5901
5902 (defun gnus-group-catchup-current (&optional n all)
5903   "Mark all articles not marked as unread in current newsgroup as read.
5904 If prefix argument N is numeric, the ARG next newsgroups will be
5905 caught up.  If ALL is non-nil, marked articles will also be marked as
5906 read.  Cross references (Xref: header) of articles are ignored.
5907 The difference between N and actual number of newsgroups that were
5908 caught up is returned."
5909   (interactive "P")
5910   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5911                gnus-expert-user
5912                (gnus-y-or-n-p
5913                 (if all
5914                     "Do you really want to mark all articles as read? "
5915                   "Mark all unread articles as read? "))))
5916       n
5917     (let ((groups (gnus-group-process-prefix n))
5918           (ret 0))
5919       (while groups
5920         ;; Virtual groups have to be given special treatment.
5921         (let ((method (gnus-find-method-for-group (car groups))))
5922           (if (eq 'nnvirtual (car method))
5923               (nnvirtual-catchup-group
5924                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5925         (gnus-group-remove-mark (car groups))
5926         (if (prog1
5927                 (gnus-group-goto-group (car groups))
5928               (gnus-group-catchup (car groups) all))
5929             (gnus-group-update-group-line)
5930           (setq ret (1+ ret)))
5931         (setq groups (cdr groups)))
5932       (gnus-group-next-unread-group 1)
5933       ret)))
5934
5935 (defun gnus-group-catchup-current-all (&optional n)
5936   "Mark all articles in current newsgroup as read.
5937 Cross references (Xref: header) of articles are ignored."
5938   (interactive "P")
5939   (gnus-group-catchup-current n 'all))
5940
5941 (defun gnus-group-catchup (group &optional all)
5942   "Mark all articles in GROUP as read.
5943 If ALL is non-nil, all articles are marked as read.
5944 The return value is the number of articles that were marked as read,
5945 or nil if no action could be taken."
5946   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5947          (num (car entry)))
5948     ;; Do the updating only if the newsgroup isn't killed.
5949     (if (not (numberp (car entry)))
5950         (gnus-message 1 "Can't catch up; non-active group")
5951       ;; Do auto-expirable marks if that's required.
5952       (when (gnus-group-auto-expirable-p group)
5953         (gnus-add-marked-articles
5954          group 'expire (gnus-list-of-unread-articles group))
5955         (when all
5956           (let ((marks (nth 3 (nth 2 entry))))
5957             (gnus-add-marked-articles
5958              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5959             (gnus-add-marked-articles
5960              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5961       (when entry
5962         (gnus-update-read-articles group nil)
5963         ;; Also nix out the lists of marks and dormants.
5964         (when all
5965           (gnus-add-marked-articles group 'tick nil nil 'force)
5966           (gnus-add-marked-articles group 'dormant nil nil 'force))
5967         (run-hooks 'gnus-group-catchup-group-hook)
5968         num))))
5969
5970 (defun gnus-group-expire-articles (&optional n)
5971   "Expire all expirable articles in the current newsgroup."
5972   (interactive "P")
5973   (let ((groups (gnus-group-process-prefix n))
5974         group)
5975     (unless groups
5976       (error "No groups to expire"))
5977     (while (setq group (pop groups))
5978       (gnus-group-remove-mark group)
5979       (when (gnus-check-backend-function 'request-expire-articles group)
5980         (gnus-message 6 "Expiring articles in %s..." group)
5981         (let* ((info (gnus-get-info group))
5982                (expirable (if (gnus-group-total-expirable-p group)
5983                               (cons nil (gnus-list-of-read-articles group))
5984                             (assq 'expire (gnus-info-marks info))))
5985                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5986           (when expirable
5987             (setcdr
5988              expirable
5989              (gnus-compress-sequence
5990               (if expiry-wait
5991                   ;; We set the expiry variables to the groupp
5992                   ;; parameter. 
5993                   (let ((nnmail-expiry-wait-function nil)
5994                         (nnmail-expiry-wait expiry-wait))
5995                     (gnus-request-expire-articles
5996                      (gnus-uncompress-sequence (cdr expirable)) group))
5997                 ;; Just expire using the normal expiry values.
5998                 (gnus-request-expire-articles
5999                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6000           (gnus-message 6 "Expiring articles in %s...done" group)))
6001       (gnus-group-position-point))))
6002
6003 (defun gnus-group-expire-all-groups ()
6004   "Expire all expirable articles in all newsgroups."
6005   (interactive)
6006   (save-excursion
6007     (gnus-message 5 "Expiring...")
6008     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6009                                      (cdr gnus-newsrc-alist))))
6010       (gnus-group-expire-articles nil)))
6011   (gnus-group-position-point)
6012   (gnus-message 5 "Expiring...done"))
6013
6014 (defun gnus-group-set-current-level (n level)
6015   "Set the level of the next N groups to LEVEL."
6016   (interactive
6017    (list
6018     current-prefix-arg
6019     (string-to-int
6020      (let ((s (read-string
6021                (format "Level (default %s): "
6022                        (or (gnus-group-group-level) 
6023                            gnus-level-default-subscribed)))))
6024        (if (string-match "^\\s-*$" s)
6025            (int-to-string (or (gnus-group-group-level) 
6026                               gnus-level-default-subscribed))
6027          s)))))
6028   (or (and (>= level 1) (<= level gnus-level-killed))
6029       (error "Illegal level: %d" level))
6030   (let ((groups (gnus-group-process-prefix n))
6031         group)
6032     (while (setq group (pop groups))
6033       (gnus-group-remove-mark group)
6034       (gnus-message 6 "Changed level of %s from %d to %d"
6035                     group (or (gnus-group-group-level) gnus-level-killed)
6036                     level)
6037       (gnus-group-change-level
6038        group level (or (gnus-group-group-level) gnus-level-killed))
6039       (gnus-group-update-group-line)))
6040   (gnus-group-position-point))
6041
6042 (defun gnus-group-unsubscribe-current-group (&optional n)
6043   "Toggle subscription of the current group.
6044 If given numerical prefix, toggle the N next groups."
6045   (interactive "P")
6046   (let ((groups (gnus-group-process-prefix n))
6047         group)
6048     (while groups
6049       (setq group (car groups)
6050             groups (cdr groups))
6051       (gnus-group-remove-mark group)
6052       (gnus-group-unsubscribe-group
6053        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6054                  gnus-level-default-unsubscribed
6055                gnus-level-default-subscribed) t)
6056       (gnus-group-update-group-line))
6057     (gnus-group-next-group 1)))
6058
6059 (defun gnus-group-unsubscribe-group (group &optional level silent)
6060   "Toggle subscription to GROUP.
6061 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6062 group line."
6063   (interactive
6064    (list (completing-read
6065           "Group: " gnus-active-hashtb nil
6066           (memq gnus-select-method gnus-have-read-active-file))))
6067   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6068     (cond
6069      ((string-match "^[ \t]$" group)
6070       (error "Empty group name"))
6071      (newsrc
6072       ;; Toggle subscription flag.
6073       (gnus-group-change-level
6074        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6075                                       gnus-level-subscribed)
6076                                   (1+ gnus-level-subscribed)
6077                                 gnus-level-default-subscribed)))
6078       (unless silent
6079         (gnus-group-update-group group)))
6080      ((and (stringp group)
6081            (or (not (memq gnus-select-method gnus-have-read-active-file))
6082                (gnus-active group)))
6083       ;; Add new newsgroup.
6084       (gnus-group-change-level
6085        group
6086        (if level level gnus-level-default-subscribed)
6087        (or (and (member group gnus-zombie-list)
6088                 gnus-level-zombie)
6089            gnus-level-killed)
6090        (and (gnus-group-group-name)
6091             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6092       (unless silent
6093         (gnus-group-update-group group)))
6094      (t (error "No such newsgroup: %s" group)))
6095     (gnus-group-position-point)))
6096
6097 (defun gnus-group-transpose-groups (n)
6098   "Move the current newsgroup up N places.
6099 If given a negative prefix, move down instead.  The difference between
6100 N and the number of steps taken is returned."
6101   (interactive "p")
6102   (or (gnus-group-group-name)
6103       (error "No group on current line"))
6104   (gnus-group-kill-group 1)
6105   (prog1
6106       (forward-line (- n))
6107     (gnus-group-yank-group)
6108     (gnus-group-position-point)))
6109
6110 (defun gnus-group-kill-all-zombies ()
6111   "Kill all zombie newsgroups."
6112   (interactive)
6113   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6114   (setq gnus-zombie-list nil)
6115   (gnus-group-list-groups))
6116
6117 (defun gnus-group-kill-region (begin end)
6118   "Kill newsgroups in current region (excluding current point).
6119 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6120   (interactive "r")
6121   (let ((lines
6122          ;; Count lines.
6123          (save-excursion
6124            (count-lines
6125             (progn
6126               (goto-char begin)
6127               (beginning-of-line)
6128               (point))
6129             (progn
6130               (goto-char end)
6131               (beginning-of-line)
6132               (point))))))
6133     (goto-char begin)
6134     (beginning-of-line)                 ;Important when LINES < 1
6135     (gnus-group-kill-group lines)))
6136
6137 (defun gnus-group-kill-group (&optional n discard)
6138   "Kill the next N groups.
6139 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6140 However, only groups that were alive can be yanked; already killed
6141 groups or zombie groups can't be yanked.
6142 The return value is the name of the group that was killed, or a list
6143 of groups killed."
6144   (interactive "P")
6145   (let ((buffer-read-only nil)
6146         (groups (gnus-group-process-prefix n))
6147         group entry level out)
6148     (if (< (length groups) 10)
6149         ;; This is faster when there are few groups.
6150         (while groups
6151           (push (setq group (pop groups)) out)
6152           (gnus-group-remove-mark group)
6153           (setq level (gnus-group-group-level))
6154           (gnus-delete-line)
6155           (when (and (not discard)
6156                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6157             (push (cons (car entry) (nth 2 entry))
6158                   gnus-list-of-killed-groups))
6159           (gnus-group-change-level
6160            (if entry entry group) gnus-level-killed (if entry nil level)))
6161       ;; If there are lots and lots of groups to be killed, we use
6162       ;; this thing instead.
6163       (let (entry)
6164         (setq groups (nreverse groups))
6165         (while groups
6166           (gnus-group-remove-mark (setq group (pop groups)))
6167           (gnus-delete-line)
6168           (cond
6169            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6170             (push (cons (car entry) (nth 2 entry))
6171                   gnus-list-of-killed-groups)
6172             (setcdr (cdr entry) (cdddr entry)))
6173            ((member group gnus-zombie-list)
6174             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6175         (gnus-make-hashtable-from-newsrc-alist)))
6176
6177     (gnus-group-position-point)
6178     (if (< (length out) 2) (car out) (nreverse out))))
6179
6180 (defun gnus-group-yank-group (&optional arg)
6181   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6182 inserting it before the current newsgroup.  The numeric ARG specifies
6183 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6184 is returned, or (if several groups are yanked) a list of yanked groups
6185 is returned."
6186   (interactive "p")
6187   (setq arg (or arg 1))
6188   (let (info group prev out)
6189     (while (>= (decf arg) 0)
6190       (if (not (setq info (pop gnus-list-of-killed-groups)))
6191           (error "No more newsgroups to yank"))
6192       (push (setq group (nth 1 info)) out)
6193       ;; Find which newsgroup to insert this one before - search
6194       ;; backward until something suitable is found.  If there are no
6195       ;; other newsgroups in this buffer, just make this newsgroup the
6196       ;; first newsgroup.
6197       (setq prev (gnus-group-group-name))
6198       (gnus-group-change-level
6199        info (gnus-info-level (cdr info)) gnus-level-killed
6200        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6201        t)
6202       (gnus-group-insert-group-line-info group))
6203     (forward-line -1)
6204     (gnus-group-position-point)
6205     (if (< (length out) 2) (car out) (nreverse out))))
6206
6207 (defun gnus-group-kill-level (level)
6208   "Kill all groups that is on a certain LEVEL."
6209   (interactive "nKill all groups on level: ")
6210   (cond
6211    ((= level gnus-level-zombie)
6212     (setq gnus-killed-list
6213           (nconc gnus-zombie-list gnus-killed-list))
6214     (setq gnus-zombie-list nil))
6215    ((and (< level gnus-level-zombie)
6216          (> level 0)
6217          (or gnus-expert-user
6218              (gnus-yes-or-no-p
6219               (format
6220                "Do you really want to kill all groups on level %d? "
6221                level))))
6222     (let* ((prev gnus-newsrc-alist)
6223            (alist (cdr prev)))
6224       (while alist
6225         (if (= (gnus-info-level level) level)
6226             (setcdr prev (cdr alist))
6227           (setq prev alist))
6228         (setq alist (cdr alist)))
6229       (gnus-make-hashtable-from-newsrc-alist)
6230       (gnus-group-list-groups)))
6231    (t
6232     (error "Can't kill; illegal level: %d" level))))
6233
6234 (defun gnus-group-list-all-groups (&optional arg)
6235   "List all newsgroups with level ARG or lower.
6236 Default is gnus-level-unsubscribed, which lists all subscribed and most
6237 unsubscribed groups."
6238   (interactive "P")
6239   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6240
6241 ;; Redefine this to list ALL killed groups if prefix arg used.
6242 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6243 (defun gnus-group-list-killed (&optional arg)
6244   "List all killed newsgroups in the group buffer.
6245 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6246 entail asking the server for the groups."
6247   (interactive "P")
6248   ;; Find all possible killed newsgroups if arg.
6249   (when arg
6250     ;; First make sure active file has been read.
6251     (unless gnus-have-read-active-file
6252       (let ((gnus-read-active-file t))
6253         (gnus-read-active-file)))
6254     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6255     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6256     (mapatoms
6257      (lambda (sym)
6258        (let ((groups 0)
6259              (group (symbol-name sym)))
6260          (if (or (null group)
6261                  (gnus-gethash group gnus-killed-hashtb)
6262                  (gnus-gethash group gnus-newsrc-hashtb))
6263              ()
6264            (let ((do-sub (gnus-matches-options-n group)))
6265              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6266                  ()
6267                (setq groups (1+ groups))
6268                (setq gnus-killed-list
6269                      (cons group gnus-killed-list))
6270                (gnus-sethash group group gnus-killed-hashtb))))))
6271      gnus-active-hashtb))
6272   (if (not gnus-killed-list)
6273       (gnus-message 6 "No killed groups")
6274     (let (gnus-group-list-mode)
6275       (funcall gnus-group-prepare-function
6276                gnus-level-killed t gnus-level-killed))
6277     (goto-char (point-min)))
6278   (gnus-group-position-point))
6279
6280 (defun gnus-group-list-zombies ()
6281   "List all zombie newsgroups in the group buffer."
6282   (interactive)
6283   (if (not gnus-zombie-list)
6284       (gnus-message 6 "No zombie groups")
6285     (let (gnus-group-list-mode)
6286       (funcall gnus-group-prepare-function
6287                gnus-level-zombie t gnus-level-zombie))
6288     (goto-char (point-min)))
6289   (gnus-group-position-point))
6290
6291 (defun gnus-group-list-active ()
6292   "List all groups that are available from the server(s)."
6293   (interactive)
6294   ;; First we make sure that we have really read the active file.
6295   (unless gnus-have-read-active-file
6296     (let ((gnus-read-active-file t))
6297       (gnus-read-active-file)))
6298   ;; Find all groups and sort them.
6299   (let ((groups
6300          (sort
6301           (let (list)
6302             (mapatoms
6303              (lambda (sym)
6304                (and (symbol-value sym)
6305                     (setq list (cons (symbol-name sym) list))))
6306              gnus-active-hashtb)
6307             list)
6308           'string<))
6309         (buffer-read-only nil))
6310     (erase-buffer)
6311     (while groups
6312       (gnus-group-insert-group-line-info (pop groups)))
6313     (goto-char (point-min))))
6314
6315 (defun gnus-activate-all-groups (level)
6316   "Activate absolutely all groups."
6317   (interactive (list 7))
6318   (let ((gnus-activate-level level)
6319         (gnus-activate-foreign-newsgroups level))
6320     (gnus-group-get-new-news)))
6321
6322 (defun gnus-group-get-new-news (&optional arg)
6323   "Get newly arrived articles.
6324 If ARG is a number, it specifies which levels you are interested in
6325 re-scanning.  If ARG is non-nil and not a number, this will force
6326 \"hard\" re-reading of the active files from all servers."
6327   (interactive "P")
6328   (run-hooks 'gnus-get-new-news-hook)
6329   ;; We might read in new NoCeM messages here.
6330   (when (and gnus-use-nocem 
6331              (null arg))
6332     (gnus-nocem-scan-groups))
6333   ;; If ARG is not a number, then we read the active file.
6334   (when (and arg (not (numberp arg)))
6335     (let ((gnus-read-active-file t))
6336       (gnus-read-active-file))
6337     (setq arg nil))
6338
6339   (setq arg (gnus-group-default-level arg t))
6340   (if (and gnus-read-active-file (not arg))
6341       (progn
6342         (gnus-read-active-file)
6343         (gnus-get-unread-articles arg))
6344     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6345       (gnus-get-unread-articles arg)))
6346   (run-hooks 'gnus-after-getting-new-news-hook)
6347   (gnus-group-list-groups))
6348
6349 (defun gnus-group-get-new-news-this-group (&optional n)
6350   "Check for newly arrived news in the current group (and the N-1 next groups).
6351 The difference between N and the number of newsgroup checked is returned.
6352 If N is negative, this group and the N-1 previous groups will be checked."
6353   (interactive "P")
6354   (let* ((groups (gnus-group-process-prefix n))
6355          (ret (if (numberp n) (- n (length groups)) 0))
6356          (beg (unless n (point)))
6357          group)
6358     (while (setq group (pop groups))
6359       (gnus-group-remove-mark group)
6360       (if (gnus-activate-group group 'scan)
6361           (progn
6362             (gnus-get-unread-articles-in-group
6363              (gnus-get-info group) (gnus-active group) t)
6364             (unless (gnus-virtual-group-p group)
6365               (gnus-close-group group))
6366             (gnus-group-update-group group))
6367         (ding)
6368         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6369     (when beg (goto-char beg))
6370     (when gnus-goto-next-group-when-activating
6371       (gnus-group-next-unread-group 1 t))
6372     (gnus-summary-position-point)
6373     ret))
6374
6375 (defun gnus-group-fetch-faq (group &optional faq-dir)
6376   "Fetch the FAQ for the current group."
6377   (interactive
6378    (list
6379     (gnus-group-real-name (gnus-group-group-name))
6380     (cond (current-prefix-arg
6381            (completing-read
6382             "Faq dir: " (and (listp gnus-group-faq-directory)
6383                              gnus-group-faq-directory))))))
6384   (or faq-dir
6385       (setq faq-dir (if (listp gnus-group-faq-directory)
6386                         (car gnus-group-faq-directory)
6387                       gnus-group-faq-directory)))
6388   (or group (error "No group name given"))
6389   (let ((file (concat (file-name-as-directory faq-dir)
6390                       (gnus-group-real-name group))))
6391     (if (not (file-exists-p file))
6392         (error "No such file: %s" file)
6393       (find-file file))))
6394
6395 (defun gnus-group-describe-group (force &optional group)
6396   "Display a description of the current newsgroup."
6397   (interactive (list current-prefix-arg (gnus-group-group-name)))
6398   (and force (setq gnus-description-hashtb nil))
6399   (let ((method (gnus-find-method-for-group group))
6400         desc)
6401     (or group (error "No group name given"))
6402     (and (or (and gnus-description-hashtb
6403                   ;; We check whether this group's method has been
6404                   ;; queried for a description file.
6405                   (gnus-gethash
6406                    (gnus-group-prefixed-name "" method)
6407                    gnus-description-hashtb))
6408              (setq desc (gnus-group-get-description group))
6409              (gnus-read-descriptions-file method))
6410          (message
6411           (or desc (gnus-gethash group gnus-description-hashtb)
6412               "No description available")))))
6413
6414 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6415 (defun gnus-group-describe-all-groups (&optional force)
6416   "Pop up a buffer with descriptions of all newsgroups."
6417   (interactive "P")
6418   (and force (setq gnus-description-hashtb nil))
6419   (if (not (or gnus-description-hashtb
6420                (gnus-read-all-descriptions-files)))
6421       (error "Couldn't request descriptions file"))
6422   (let ((buffer-read-only nil)
6423         b)
6424     (erase-buffer)
6425     (mapatoms
6426      (lambda (group)
6427        (setq b (point))
6428        (insert (format "      *: %-20s %s\n" (symbol-name group)
6429                        (symbol-value group)))
6430        (add-text-properties
6431         b (1+ b) (list 'gnus-group group
6432                        'gnus-unread t 'gnus-marked nil
6433                        'gnus-level (1+ gnus-level-subscribed))))
6434      gnus-description-hashtb)
6435     (goto-char (point-min))
6436     (gnus-group-position-point)))
6437
6438 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6439 (defun gnus-group-apropos (regexp &optional search-description)
6440   "List all newsgroups that have names that match a regexp."
6441   (interactive "sGnus apropos (regexp): ")
6442   (let ((prev "")
6443         (obuf (current-buffer))
6444         groups des)
6445     ;; Go through all newsgroups that are known to Gnus.
6446     (mapatoms
6447      (lambda (group)
6448        (and (symbol-name group)
6449             (string-match regexp (symbol-name group))
6450             (setq groups (cons (symbol-name group) groups))))
6451      gnus-active-hashtb)
6452     ;; Also go through all descriptions that are known to Gnus.
6453     (when search-description
6454       (mapatoms
6455        (lambda (group)
6456          (and (string-match regexp (symbol-value group))
6457               (gnus-active (symbol-name group))
6458               (setq groups (cons (symbol-name group) groups))))
6459        gnus-description-hashtb))
6460     (if (not groups)
6461         (gnus-message 3 "No groups matched \"%s\"." regexp)
6462       ;; Print out all the groups.
6463       (save-excursion
6464         (pop-to-buffer "*Gnus Help*")
6465         (buffer-disable-undo (current-buffer))
6466         (erase-buffer)
6467         (setq groups (sort groups 'string<))
6468         (while groups
6469           ;; Groups may be entered twice into the list of groups.
6470           (if (not (string= (car groups) prev))
6471               (progn
6472                 (insert (setq prev (car groups)) "\n")
6473                 (if (and gnus-description-hashtb
6474                          (setq des (gnus-gethash (car groups)
6475                                                  gnus-description-hashtb)))
6476                     (insert "  " des "\n"))))
6477           (setq groups (cdr groups)))
6478         (goto-char (point-min))))
6479     (pop-to-buffer obuf)))
6480
6481 (defun gnus-group-description-apropos (regexp)
6482   "List all newsgroups that have names or descriptions that match a regexp."
6483   (interactive "sGnus description apropos (regexp): ")
6484   (if (not (or gnus-description-hashtb
6485                (gnus-read-all-descriptions-files)))
6486       (error "Couldn't request descriptions file"))
6487   (gnus-group-apropos regexp t))
6488
6489 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6490 (defun gnus-group-list-matching (level regexp &optional all lowest)
6491   "List all groups with unread articles that match REGEXP.
6492 If the prefix LEVEL is non-nil, it should be a number that says which
6493 level to cut off listing groups.
6494 If ALL, also list groups with no unread articles.
6495 If LOWEST, don't list groups with level lower than LOWEST."
6496   (interactive "P\nsList newsgroups matching: ")
6497   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6498                            all (or lowest 1) regexp)
6499   (goto-char (point-min))
6500   (gnus-group-position-point))
6501
6502 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6503   "List all groups that match REGEXP.
6504 If the prefix LEVEL is non-nil, it should be a number that says which
6505 level to cut off listing groups.
6506 If LOWEST, don't list groups with level lower than LOWEST."
6507   (interactive "P\nsList newsgroups matching: ")
6508   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6509
6510 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6511 (defun gnus-group-save-newsrc (&optional force)
6512   "Save the Gnus startup files.
6513 If FORCE, force saving whether it is necessary or not."
6514   (interactive "P")
6515   (gnus-save-newsrc-file force))
6516
6517 (defun gnus-group-restart (&optional arg)
6518   "Force Gnus to read the .newsrc file."
6519   (interactive "P")
6520   (when (gnus-yes-or-no-p
6521          (format "Are you sure you want to read %s? "
6522                  gnus-current-startup-file))
6523     (gnus-save-newsrc-file)
6524     (gnus-setup-news 'force)
6525     (gnus-group-list-groups arg)))
6526
6527 (defun gnus-group-read-init-file ()
6528   "Read the Gnus elisp init file."
6529   (interactive)
6530   (gnus-read-init-file))
6531
6532 (defun gnus-group-check-bogus-groups (&optional silent)
6533   "Check bogus newsgroups.
6534 If given a prefix, don't ask for confirmation before removing a bogus
6535 group."
6536   (interactive "P")
6537   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6538   (gnus-group-list-groups))
6539
6540 (defun gnus-group-edit-global-kill (&optional article group)
6541   "Edit the global kill file.
6542 If GROUP, edit that local kill file instead."
6543   (interactive "P")
6544   (setq gnus-current-kill-article article)
6545   (gnus-kill-file-edit-file group)
6546   (gnus-message
6547    6
6548    (substitute-command-keys
6549     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6550             (if group "local" "global")))))
6551
6552 (defun gnus-group-edit-local-kill (article group)
6553   "Edit a local kill file."
6554   (interactive (list nil (gnus-group-group-name)))
6555   (gnus-group-edit-global-kill article group))
6556
6557 (defun gnus-group-force-update ()
6558   "Update `.newsrc' file."
6559   (interactive)
6560   (gnus-save-newsrc-file))
6561
6562 (defun gnus-group-suspend ()
6563   "Suspend the current Gnus session.
6564 In fact, cleanup buffers except for group mode buffer.
6565 The hook gnus-suspend-gnus-hook is called before actually suspending."
6566   (interactive)
6567   (run-hooks 'gnus-suspend-gnus-hook)
6568   ;; Kill Gnus buffers except for group mode buffer.
6569   (let ((group-buf (get-buffer gnus-group-buffer)))
6570     ;; Do this on a separate list in case the user does a ^G before we finish
6571     (let ((gnus-buffer-list
6572            (delq group-buf (delq gnus-dribble-buffer
6573                                  (append gnus-buffer-list nil)))))
6574       (while gnus-buffer-list
6575         (gnus-kill-buffer (car gnus-buffer-list))
6576         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6577     (if group-buf
6578         (progn
6579           (setq gnus-buffer-list (list group-buf))
6580           (bury-buffer group-buf)
6581           (delete-windows-on group-buf t)))))
6582
6583 (defun gnus-group-clear-dribble ()
6584   "Clear all information from the dribble buffer."
6585   (interactive)
6586   (gnus-dribble-clear)
6587   (gnus-message 7 "Cleared dribble buffer"))
6588
6589 (defun gnus-group-exit ()
6590   "Quit reading news after updating .newsrc.eld and .newsrc.
6591 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6592   (interactive)
6593   (when 
6594       (or noninteractive                ;For gnus-batch-kill
6595           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6596           (not gnus-interactive-exit)   ;Without confirmation
6597           gnus-expert-user
6598           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6599     (run-hooks 'gnus-exit-gnus-hook)
6600     ;; Offer to save data from non-quitted summary buffers.
6601     (gnus-offer-save-summaries)
6602     ;; Save the newsrc file(s).
6603     (gnus-save-newsrc-file)
6604     ;; Kill-em-all.
6605     (gnus-close-backends)
6606     ;; Reset everything.
6607     (gnus-clear-system)
6608     ;; Allow the user to do things after cleaning up.
6609     (run-hooks 'gnus-after-exiting-gnus-hook)))
6610
6611 (defun gnus-close-backends ()
6612   ;; Send a close request to all backends that support such a request.
6613   (let ((methods gnus-valid-select-methods)
6614         func)
6615     (while methods
6616       (if (fboundp (setq func (intern (concat (caar methods)
6617                                               "-request-close"))))
6618           (funcall func))
6619       (setq methods (cdr methods)))))
6620
6621 (defun gnus-group-quit ()
6622   "Quit reading news without updating .newsrc.eld or .newsrc.
6623 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6624   (interactive)
6625   (when (or noninteractive              ;For gnus-batch-kill
6626             (zerop (buffer-size))
6627             (not (gnus-server-opened gnus-select-method))
6628             gnus-expert-user
6629             (not gnus-current-startup-file)
6630             (gnus-yes-or-no-p
6631              (format "Quit reading news without saving %s? "
6632                      (file-name-nondirectory gnus-current-startup-file))))
6633     (run-hooks 'gnus-exit-gnus-hook)
6634     (if gnus-use-full-window
6635         (delete-other-windows)
6636       (gnus-remove-some-windows))
6637     (gnus-dribble-save)
6638     (gnus-close-backends)
6639     (gnus-clear-system)
6640     ;; Allow the user to do things after cleaning up.
6641     (run-hooks 'gnus-after-exiting-gnus-hook)))
6642
6643 (defun gnus-offer-save-summaries ()
6644   "Offer to save all active summary buffers."
6645   (save-excursion
6646     (let ((buflist (buffer-list))
6647           buffers bufname)
6648       ;; Go through all buffers and find all summaries.
6649       (while buflist
6650         (and (setq bufname (buffer-name (car buflist)))
6651              (string-match "Summary" bufname)
6652              (save-excursion
6653                (set-buffer bufname)
6654                ;; We check that this is, indeed, a summary buffer.
6655                (and (eq major-mode 'gnus-summary-mode)
6656                     ;; Also make sure this isn't bogus.
6657                     gnus-newsgroup-prepared))
6658              (push bufname buffers))
6659         (setq buflist (cdr buflist)))
6660       ;; Go through all these summary buffers and offer to save them.
6661       (when buffers
6662         (map-y-or-n-p
6663          "Update summary buffer %s? "
6664          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6665          buffers)))))
6666
6667 (defun gnus-group-describe-briefly ()
6668   "Give a one line description of the group mode commands."
6669   (interactive)
6670   (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")))
6671
6672 (defun gnus-group-browse-foreign-server (method)
6673   "Browse a foreign news server.
6674 If called interactively, this function will ask for a select method
6675  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6676 If not, METHOD should be a list where the first element is the method
6677 and the second element is the address."
6678   (interactive
6679    (list (let ((how (completing-read
6680                      "Which backend: "
6681                      (append gnus-valid-select-methods gnus-server-alist)
6682                      nil t (cons "nntp" 0))))
6683            ;; We either got a backend name or a virtual server name.
6684            ;; If the first, we also need an address.
6685            (if (assoc how gnus-valid-select-methods)
6686                (list (intern how)
6687                      ;; Suggested by mapjph@bath.ac.uk.
6688                      (completing-read
6689                       "Address: "
6690                       (mapcar (lambda (server) (list server))
6691                               gnus-secondary-servers)))
6692              ;; We got a server name, so we find the method.
6693              (gnus-server-to-method how)))))
6694   (gnus-browse-foreign-server method))
6695
6696 \f
6697 ;;;
6698 ;;; Gnus summary mode
6699 ;;;
6700
6701 (defvar gnus-summary-mode-map nil)
6702
6703 (put 'gnus-summary-mode 'mode-class 'special)
6704
6705 (unless gnus-summary-mode-map
6706   (setq gnus-summary-mode-map (make-keymap))
6707   (suppress-keymap gnus-summary-mode-map)
6708
6709   ;; Non-orthogonal keys
6710
6711   (gnus-define-keys gnus-summary-mode-map
6712     " " gnus-summary-next-page
6713     "\177" gnus-summary-prev-page
6714     [delete] gnus-summary-prev-page
6715     "\r" gnus-summary-scroll-up
6716     "n" gnus-summary-next-unread-article
6717     "p" gnus-summary-prev-unread-article
6718     "N" gnus-summary-next-article
6719     "P" gnus-summary-prev-article
6720     "\M-\C-n" gnus-summary-next-same-subject
6721     "\M-\C-p" gnus-summary-prev-same-subject
6722     "\M-n" gnus-summary-next-unread-subject
6723     "\M-p" gnus-summary-prev-unread-subject
6724     "." gnus-summary-first-unread-article
6725     "," gnus-summary-best-unread-article
6726     "\M-s" gnus-summary-search-article-forward
6727     "\M-r" gnus-summary-search-article-backward
6728     "<" gnus-summary-beginning-of-article
6729     ">" gnus-summary-end-of-article
6730     "j" gnus-summary-goto-article
6731     "^" gnus-summary-refer-parent-article
6732     "\M-^" gnus-summary-refer-article
6733     "u" gnus-summary-tick-article-forward
6734     "!" gnus-summary-tick-article-forward
6735     "U" gnus-summary-tick-article-backward
6736     "d" gnus-summary-mark-as-read-forward
6737     "D" gnus-summary-mark-as-read-backward
6738     "E" gnus-summary-mark-as-expirable
6739     "\M-u" gnus-summary-clear-mark-forward
6740     "\M-U" gnus-summary-clear-mark-backward
6741     "k" gnus-summary-kill-same-subject-and-select
6742     "\C-k" gnus-summary-kill-same-subject
6743     "\M-\C-k" gnus-summary-kill-thread
6744     "\M-\C-l" gnus-summary-lower-thread
6745     "e" gnus-summary-edit-article
6746     "#" gnus-summary-mark-as-processable
6747     "\M-#" gnus-summary-unmark-as-processable
6748     "\M-\C-t" gnus-summary-toggle-threads
6749     "\M-\C-s" gnus-summary-show-thread
6750     "\M-\C-h" gnus-summary-hide-thread
6751     "\M-\C-f" gnus-summary-next-thread
6752     "\M-\C-b" gnus-summary-prev-thread
6753     "\M-\C-u" gnus-summary-up-thread
6754     "\M-\C-d" gnus-summary-down-thread
6755     "&" gnus-summary-execute-command
6756     "c" gnus-summary-catchup-and-exit
6757     "\C-w" gnus-summary-mark-region-as-read
6758     "\C-t" gnus-summary-toggle-truncation
6759     "?" gnus-summary-mark-as-dormant
6760     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6761     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6762     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6763     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6764     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6765     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6766     "=" gnus-summary-expand-window
6767     "\C-x\C-s" gnus-summary-reselect-current-group
6768     "\M-g" gnus-summary-rescan-group
6769     "w" gnus-summary-stop-page-breaking
6770     "\C-c\C-r" gnus-summary-caesar-message
6771     "\M-t" gnus-summary-toggle-mime
6772     "f" gnus-summary-followup
6773     "F" gnus-summary-followup-with-original
6774     "C" gnus-summary-cancel-article
6775     "r" gnus-summary-reply
6776     "R" gnus-summary-reply-with-original
6777     "\C-c\C-f" gnus-summary-mail-forward
6778     "o" gnus-summary-save-article
6779     "\C-o" gnus-summary-save-article-mail
6780     "|" gnus-summary-pipe-output
6781     "\M-k" gnus-summary-edit-local-kill
6782     "\M-K" gnus-summary-edit-global-kill
6783     "V" gnus-version
6784     "\C-c\C-d" gnus-summary-describe-group
6785     "q" gnus-summary-exit
6786     "Q" gnus-summary-exit-no-update
6787     "\C-c\C-i" gnus-info-find-node
6788     gnus-mouse-2 gnus-mouse-pick-article
6789     "m" gnus-summary-mail-other-window
6790     "a" gnus-summary-post-news
6791     "x" gnus-summary-limit-to-unread
6792     "s" gnus-summary-isearch-article
6793     "t" gnus-article-hide-headers
6794     "g" gnus-summary-show-article
6795     "l" gnus-summary-goto-last-article
6796     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6797     "\C-d" gnus-summary-enter-digest-group
6798     "\C-c\C-b" gnus-bug
6799     "*" gnus-cache-enter-article
6800     "\M-*" gnus-cache-remove-article
6801     "\M-&" gnus-summary-universal-argument
6802     "\C-l" gnus-recenter
6803     "I" gnus-summary-increase-score
6804     "L" gnus-summary-lower-score
6805
6806     "V" gnus-summary-score-map
6807     "X" gnus-uu-extract-map
6808     "S" gnus-summary-send-map)
6809
6810   ;; Sort of orthogonal keymap
6811   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6812     "t" gnus-summary-tick-article-forward
6813     "!" gnus-summary-tick-article-forward
6814     "d" gnus-summary-mark-as-read-forward
6815     "r" gnus-summary-mark-as-read-forward
6816     "c" gnus-summary-clear-mark-forward
6817     " " gnus-summary-clear-mark-forward
6818     "e" gnus-summary-mark-as-expirable
6819     "x" gnus-summary-mark-as-expirable
6820     "?" gnus-summary-mark-as-dormant
6821     "b" gnus-summary-set-bookmark
6822     "B" gnus-summary-remove-bookmark
6823     "#" gnus-summary-mark-as-processable
6824     "\M-#" gnus-summary-unmark-as-processable
6825     "S" gnus-summary-limit-include-expunged
6826     "C" gnus-summary-catchup
6827     "H" gnus-summary-catchup-to-here
6828     "\C-c" gnus-summary-catchup-all
6829     "k" gnus-summary-kill-same-subject-and-select
6830     "K" gnus-summary-kill-same-subject
6831     "P" gnus-uu-mark-map)
6832
6833   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6834     "c" gnus-summary-clear-above
6835     "u" gnus-summary-tick-above
6836     "m" gnus-summary-mark-above
6837     "k" gnus-summary-kill-below)
6838
6839   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6840     "/" gnus-summary-limit-to-subject
6841     "n" gnus-summary-limit-to-articles
6842     "w" gnus-summary-pop-limit
6843     "s" gnus-summary-limit-to-subject
6844     "a" gnus-summary-limit-to-author
6845     "u" gnus-summary-limit-to-unread
6846     "m" gnus-summary-limit-to-marks
6847     "v" gnus-summary-limit-to-score
6848     "D" gnus-summary-limit-include-dormant
6849     "d" gnus-summary-limit-exclude-dormant
6850     ;;  "t" gnus-summary-limit-exclude-thread
6851     "E" gnus-summary-limit-include-expunged
6852     "c" gnus-summary-limit-exclude-childless-dormant
6853     "C" gnus-summary-limit-mark-excluded-as-read)
6854
6855   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6856     "n" gnus-summary-next-unread-article
6857     "p" gnus-summary-prev-unread-article
6858     "N" gnus-summary-next-article
6859     "P" gnus-summary-prev-article
6860     "\C-n" gnus-summary-next-same-subject
6861     "\C-p" gnus-summary-prev-same-subject
6862     "\M-n" gnus-summary-next-unread-subject
6863     "\M-p" gnus-summary-prev-unread-subject
6864     "f" gnus-summary-first-unread-article
6865     "b" gnus-summary-best-unread-article
6866     "j" gnus-summary-goto-article
6867     "g" gnus-summary-goto-subject
6868     "l" gnus-summary-goto-last-article
6869     "p" gnus-summary-pop-article)
6870
6871   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6872     "k" gnus-summary-kill-thread
6873     "l" gnus-summary-lower-thread
6874     "i" gnus-summary-raise-thread
6875     "T" gnus-summary-toggle-threads
6876     "t" gnus-summary-rethread-current
6877     "^" gnus-summary-reparent-thread
6878     "s" gnus-summary-show-thread
6879     "S" gnus-summary-show-all-threads
6880     "h" gnus-summary-hide-thread
6881     "H" gnus-summary-hide-all-threads
6882     "n" gnus-summary-next-thread
6883     "p" gnus-summary-prev-thread
6884     "u" gnus-summary-up-thread
6885     "o" gnus-summary-top-thread
6886     "d" gnus-summary-down-thread
6887     "#" gnus-uu-mark-thread
6888     "\M-#" gnus-uu-unmark-thread)
6889
6890   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6891     "c" gnus-summary-catchup-and-exit
6892     "C" gnus-summary-catchup-all-and-exit
6893     "E" gnus-summary-exit-no-update
6894     "Q" gnus-summary-exit
6895     "Z" gnus-summary-exit
6896     "n" gnus-summary-catchup-and-goto-next-group
6897     "R" gnus-summary-reselect-current-group
6898     "G" gnus-summary-rescan-group
6899     "N" gnus-summary-next-group
6900     "P" gnus-summary-prev-group)
6901
6902   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6903     " " gnus-summary-next-page
6904     "n" gnus-summary-next-page
6905     "\177" gnus-summary-prev-page
6906     [delete] gnus-summary-prev-page
6907     "p" gnus-summary-prev-page
6908     "\r" gnus-summary-scroll-up
6909     "<" gnus-summary-beginning-of-article
6910     ">" gnus-summary-end-of-article
6911     "b" gnus-summary-beginning-of-article
6912     "e" gnus-summary-end-of-article
6913     "^" gnus-summary-refer-parent-article
6914     "r" gnus-summary-refer-parent-article
6915     "R" gnus-summary-refer-references
6916     "g" gnus-summary-show-article
6917     "s" gnus-summary-isearch-article)
6918
6919   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6920     "b" gnus-article-add-buttons
6921     "B" gnus-article-add-buttons-to-head
6922     "o" gnus-article-treat-overstrike
6923     ;;  "w" gnus-article-word-wrap
6924     "w" gnus-article-fill-cited-article
6925     "c" gnus-article-remove-cr
6926     "L" gnus-article-remove-trailing-blank-lines
6927     "q" gnus-article-de-quoted-unreadable
6928     "f" gnus-article-display-x-face
6929     "l" gnus-summary-stop-page-breaking
6930     "r" gnus-summary-caesar-message
6931     "t" gnus-article-hide-headers
6932     "v" gnus-summary-verbose-headers
6933     "m" gnus-summary-toggle-mime)
6934
6935   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6936     "a" gnus-article-hide
6937     "h" gnus-article-hide-headers
6938     "b" gnus-article-hide-boring-headers
6939     "s" gnus-article-hide-signature
6940     "c" gnus-article-hide-citation
6941     "p" gnus-article-hide-pgp
6942     "\C-c" gnus-article-hide-citation-maybe)
6943
6944   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6945     "a" gnus-article-highlight
6946     "h" gnus-article-highlight-headers
6947     "c" gnus-article-highlight-citation
6948     "s" gnus-article-highlight-signature)
6949
6950   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6951     "z" gnus-article-date-ut
6952     "u" gnus-article-date-ut
6953     "l" gnus-article-date-local
6954     "e" gnus-article-date-lapsed
6955     "o" gnus-article-date-original)
6956
6957   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6958     "v" gnus-version
6959     "f" gnus-summary-fetch-faq
6960     "d" gnus-summary-describe-group
6961     "h" gnus-summary-describe-briefly
6962     "i" gnus-info-find-node)
6963
6964   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6965     "e" gnus-summary-expire-articles
6966     "\M-\C-e" gnus-summary-expire-articles-now
6967     "\177" gnus-summary-delete-article
6968     [delete] gnus-summary-delete-article
6969     "m" gnus-summary-move-article
6970     "r" gnus-summary-respool-article
6971     "w" gnus-summary-edit-article
6972     "c" gnus-summary-copy-article
6973     "B" gnus-summary-crosspost-article
6974     "q" gnus-summary-respool-query
6975     "i" gnus-summary-import-article)
6976
6977   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6978     "o" gnus-summary-save-article
6979     "m" gnus-summary-save-article-mail
6980     "r" gnus-summary-save-article-rmail
6981     "f" gnus-summary-save-article-file
6982     "b" gnus-summary-save-article-body-file
6983     "h" gnus-summary-save-article-folder
6984     "v" gnus-summary-save-article-vm
6985     "p" gnus-summary-pipe-output
6986     "s" gnus-soup-add-article)
6987   )
6988
6989 \f
6990
6991 (defun gnus-summary-mode (&optional group)
6992   "Major mode for reading articles.
6993
6994 All normal editing commands are switched off.
6995 \\<gnus-summary-mode-map>
6996 Each line in this buffer represents one article.  To read an
6997 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6998 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6999 respectively.
7000
7001 You can also post articles and send mail from this buffer.  To
7002 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7003 of an article, type `\\[gnus-summary-reply]'.
7004
7005 There are approx. one gazillion commands you can execute in this
7006 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7007
7008 The following commands are available:
7009
7010 \\{gnus-summary-mode-map}"
7011   (interactive)
7012   (when (and menu-bar-mode
7013              (gnus-visual-p 'summary-menu 'menu))
7014     (gnus-summary-make-menu-bar))
7015   (kill-all-local-variables)
7016   (let ((locals gnus-summary-local-variables))
7017     (while locals
7018       (if (consp (car locals))
7019           (progn
7020             (make-local-variable (caar locals))
7021             (set (caar locals) (eval (cdar locals))))
7022         (make-local-variable (car locals))
7023         (set (car locals) nil))
7024       (setq locals (cdr locals))))
7025   (gnus-make-thread-indent-array)
7026   (gnus-simplify-mode-line)
7027   (setq major-mode 'gnus-summary-mode)
7028   (setq mode-name "Summary")
7029   (make-local-variable 'minor-mode-alist)
7030   (use-local-map gnus-summary-mode-map)
7031   (buffer-disable-undo (current-buffer))
7032   (setq buffer-read-only t)             ;Disable modification
7033   (setq truncate-lines t)
7034   (setq selective-display t)
7035   (setq selective-display-ellipses t)   ;Display `...'
7036   (setq buffer-display-table gnus-summary-display-table)
7037   (setq gnus-newsgroup-name group)
7038   (run-hooks 'gnus-summary-mode-hook))
7039
7040 (defun gnus-summary-make-display-table ()
7041   ;; Change the display table.  Odd characters have a tendency to mess
7042   ;; up nicely formatted displays - we make all possible glyphs
7043   ;; display only a single character.
7044
7045   ;; We start from the standard display table, if any.
7046   (setq gnus-summary-display-table
7047         (or (copy-sequence standard-display-table)
7048             (make-display-table)))
7049   ;; Nix out all the control chars...
7050   (let ((i 32))
7051     (while (>= (setq i (1- i)) 0)
7052       (aset gnus-summary-display-table i [??])))
7053   ;; ... but not newline and cr, of course. (cr is necessary for the
7054   ;; selective display).
7055   (aset gnus-summary-display-table ?\n nil)
7056   (aset gnus-summary-display-table ?\r nil)
7057   ;; We nix out any glyphs over 126 that are not set already.
7058   (let ((i 256))
7059     (while (>= (setq i (1- i)) 127)
7060       ;; Only modify if the entry is nil.
7061       (or (aref gnus-summary-display-table i)
7062           (aset gnus-summary-display-table i [??])))))
7063
7064 (defun gnus-summary-clear-local-variables ()
7065   (let ((locals gnus-summary-local-variables))
7066     (while locals
7067       (if (consp (car locals))
7068           (and (vectorp (caar locals))
7069                (set (caar locals) nil))
7070         (and (vectorp (car locals))
7071              (set (car locals) nil)))
7072       (setq locals (cdr locals)))))
7073
7074 ;; Summary data functions.
7075
7076 (defmacro gnus-data-number (data)
7077   `(car ,data))
7078
7079 (defmacro gnus-data-set-number (data number)
7080   `(setcar ,data ,number))
7081
7082 (defmacro gnus-data-mark (data)
7083   `(nth 1 ,data))
7084
7085 (defmacro gnus-data-set-mark (data mark)
7086   `(setcar (nthcdr 1 ,data) ,mark))
7087
7088 (defmacro gnus-data-pos (data)
7089   `(nth 2 ,data))
7090
7091 (defmacro gnus-data-set-pos (data pos)
7092   `(setcar (nthcdr 2 ,data) ,pos))
7093
7094 (defmacro gnus-data-header (data)
7095   `(nth 3 ,data))
7096
7097 (defmacro gnus-data-level (data)
7098   `(nth 4 ,data))
7099
7100 (defmacro gnus-data-unread-p (data)
7101   `(= (nth 1 ,data) gnus-unread-mark))
7102
7103 (defmacro gnus-data-pseudo-p (data)
7104   `(consp (nth 3 ,data)))
7105
7106 (defmacro gnus-data-find (number)
7107   `(assq ,number gnus-newsgroup-data))
7108
7109 (defmacro gnus-data-find-list (number &optional data)
7110   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7111      (memq (assq ,number bdata)
7112            bdata)))
7113
7114 (defmacro gnus-data-make (number mark pos header level)
7115   `(list ,number ,mark ,pos ,header ,level))
7116
7117 (defun gnus-data-enter (after-article number mark pos header level offset)
7118   (let ((data (gnus-data-find-list after-article)))
7119     (or data (error "No such article: %d" after-article))
7120     (setcdr data (cons (gnus-data-make number mark pos header level)
7121                        (cdr data)))
7122     (setq gnus-newsgroup-data-reverse nil)
7123     (gnus-data-update-list (cddr data) offset)))
7124
7125 (defun gnus-data-enter-list (after-article list &optional offset)
7126   (when list
7127     (let ((data (and after-article (gnus-data-find-list after-article)))
7128           (ilist list))
7129       (or data (not after-article) (error "No such article: %d" after-article))
7130       ;; Find the last element in the list to be spliced into the main
7131       ;; list.
7132       (while (cdr list)
7133         (setq list (cdr list)))
7134       (if (not data)
7135           (progn
7136             (setcdr list gnus-newsgroup-data)
7137             (setq gnus-newsgroup-data ilist)
7138             (and offset (gnus-data-update-list (cdr list) offset)))
7139         (setcdr list (cdr data))
7140         (setcdr data ilist)
7141         (and offset (gnus-data-update-list (cdr data) offset)))
7142       (setq gnus-newsgroup-data-reverse nil))))
7143
7144 (defun gnus-data-remove (article &optional offset)
7145   (let ((data gnus-newsgroup-data))
7146     (if (= (gnus-data-number (car data)) article)
7147         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7148               gnus-newsgroup-data-reverse nil)
7149       (while (cdr data)
7150         (and (= (gnus-data-number (cadr data)) article)
7151              (progn
7152                (setcdr data (cddr data))
7153                (and offset (gnus-data-update-list (cdr data) offset))
7154                (setq data nil
7155                      gnus-newsgroup-data-reverse nil)))
7156         (setq data (cdr data))))))
7157
7158 (defmacro gnus-data-list (backward)
7159   `(if ,backward
7160        (or gnus-newsgroup-data-reverse
7161            (setq gnus-newsgroup-data-reverse
7162                  (reverse gnus-newsgroup-data)))
7163      gnus-newsgroup-data))
7164
7165 (defun gnus-data-update-list (data offset)
7166   "Add OFFSET to the POS of all data entries in DATA."
7167   (while data
7168     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7169     (setq data (cdr data))))
7170
7171 (defun gnus-data-compute-positions ()
7172   "Compute the positions of all articles."
7173   (let ((data gnus-newsgroup-data)
7174         pos)
7175     (while data
7176       (when (setq pos (text-property-any
7177                        (point-min) (point-max)
7178                        'gnus-number (gnus-data-number (car data))))
7179         (gnus-data-set-pos (car data) (+ pos 3)))
7180       (setq data (cdr data)))))
7181
7182 (defun gnus-summary-article-pseudo-p (article)
7183   "Say whether this article is a pseudo article or not."
7184   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7185
7186 (defun gnus-article-parent-p (number)
7187   "Say whether this article is a parent or not."
7188   (let ((data (gnus-data-find-list number)))
7189     (and (cdr data)                     ; There has to be an article after...
7190          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7191             (gnus-data-level (nth 1 data))))))
7192
7193 (defun gnus-article-children (number)
7194   "Return a list of all children to NUMBER."
7195   (let* ((data (gnus-data-find-list number))
7196          (level (gnus-data-level (car data)))
7197          children)
7198     (setq data (cdr data))
7199     (while (and data            
7200                 (= (gnus-data-level (car data)) (1+ level)))
7201       (push (gnus-data-number (car data)) children)
7202       (setq data (cdr data)))
7203     children))
7204
7205 (defmacro gnus-summary-skip-intangible ()
7206   "If the current article is intangible, then jump to a different article."
7207   '(let ((to (get-text-property (point) 'gnus-intangible)))
7208     (and to (gnus-summary-goto-subject to))))
7209
7210 (defmacro gnus-summary-article-intangible-p ()
7211   "Say whether this article is intangible or not."
7212   '(get-text-property (point) 'gnus-intangible))
7213
7214 ;; Some summary mode macros.
7215
7216 (defmacro gnus-summary-article-number ()
7217   "The article number of the article on the current line.
7218 If there isn's an article number here, then we return the current
7219 article number."
7220   '(progn
7221      (gnus-summary-skip-intangible)
7222      (or (get-text-property (point) 'gnus-number)
7223          (gnus-summary-last-subject))))
7224
7225 (defmacro gnus-summary-article-header (&optional number)
7226   `(gnus-data-header (gnus-data-find
7227                       ,(or number '(gnus-summary-article-number)))))
7228
7229 (defmacro gnus-summary-thread-level (&optional number)
7230   `(if (and (eq gnus-summary-make-false-root 'dummy)
7231             (get-text-property (point) 'gnus-intangible))
7232        0
7233      (gnus-data-level (gnus-data-find
7234                        ,(or number '(gnus-summary-article-number))))))
7235
7236 (defmacro gnus-summary-article-mark (&optional number)
7237   `(gnus-data-mark (gnus-data-find
7238                     ,(or number '(gnus-summary-article-number)))))
7239
7240 (defmacro gnus-summary-article-pos (&optional number)
7241   `(gnus-data-pos (gnus-data-find
7242                    ,(or number '(gnus-summary-article-number)))))
7243
7244 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7245 (defmacro gnus-summary-article-subject (&optional number)
7246   "Return current subject string or nil if nothing."
7247   `(let ((headers
7248           ,(if number
7249                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7250              '(gnus-data-header (assq (gnus-summary-article-number)
7251                                       gnus-newsgroup-data)))))
7252      (and headers
7253           (vectorp headers)
7254           (mail-header-subject headers))))
7255
7256 (defmacro gnus-summary-article-score (&optional number)
7257   "Return current article score."
7258   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7259                   gnus-newsgroup-scored))
7260        gnus-summary-default-score 0))
7261
7262 (defun gnus-summary-article-children (&optional number)
7263   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7264          (level (gnus-data-level (car data)))
7265          l children)
7266     (while (and (setq data (cdr data))
7267                 (> (setq l (gnus-data-level (car data))) level))
7268       (and (= (1+ level) l)
7269            (setq children (cons (gnus-data-number (car data))
7270                                 children))))
7271     (nreverse children)))
7272
7273 (defun gnus-summary-article-parent (&optional number)
7274   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7275                                     (gnus-data-list t)))
7276          (level (gnus-data-level (car data))))
7277     (if (zerop level)
7278         () ; This is a root.
7279       ;; We search until we find an article with a level less than
7280       ;; this one.  That function has to be the parent.
7281       (while (and (setq data (cdr data))
7282                   (not (< (gnus-data-level (car data)) level))))
7283       (and data (gnus-data-number (car data))))))
7284
7285 (defun gnus-unread-mark-p (mark)
7286   "Say whether MARK is the unread mark."
7287   (= mark gnus-unread-mark))
7288
7289 (defun gnus-read-mark-p (mark)
7290   "Say whether MARK is one of the marks that mark as read.
7291 This is all marks except unread, ticked, dormant, and expirable."
7292   (not (or (= mark gnus-unread-mark)
7293            (= mark gnus-ticked-mark)
7294            (= mark gnus-dormant-mark)
7295            (= mark gnus-expirable-mark))))
7296
7297 ;; Various summary mode internalish functions.
7298
7299 (defun gnus-mouse-pick-article (e)
7300   (interactive "e")
7301   (mouse-set-point e)
7302   (gnus-summary-next-page nil t))
7303
7304 (defun gnus-summary-setup-buffer (group)
7305   "Initialize summary buffer."
7306   (let ((buffer (concat "*Summary " group "*")))
7307     (if (get-buffer buffer)
7308         (progn
7309           (set-buffer buffer)
7310           (setq gnus-summary-buffer (current-buffer))
7311           (not gnus-newsgroup-prepared))
7312       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7313       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7314       (gnus-add-current-to-buffer-list)
7315       (gnus-summary-mode group)
7316       (when gnus-carpal
7317         (gnus-carpal-setup-buffer 'summary))
7318       (unless gnus-single-article-buffer
7319         (make-local-variable 'gnus-article-buffer)
7320         (make-local-variable 'gnus-article-current)
7321         (make-local-variable 'gnus-original-article-buffer))
7322       (setq gnus-newsgroup-name group)
7323       t)))
7324
7325 (defun gnus-set-global-variables ()
7326   ;; Set the global equivalents of the summary buffer-local variables
7327   ;; to the latest values they had.  These reflect the summary buffer
7328   ;; that was in action when the last article was fetched.
7329   (when (eq major-mode 'gnus-summary-mode)
7330     (setq gnus-summary-buffer (current-buffer))
7331     (let ((name gnus-newsgroup-name)
7332           (marked gnus-newsgroup-marked)
7333           (unread gnus-newsgroup-unreads)
7334           (headers gnus-current-headers)
7335           (data gnus-newsgroup-data)
7336           (summary gnus-summary-buffer)
7337           (article-buffer gnus-article-buffer)
7338           (original gnus-original-article-buffer)
7339           (gac gnus-article-current)
7340           (score-file gnus-current-score-file))
7341       (save-excursion
7342         (set-buffer gnus-group-buffer)
7343         (setq gnus-newsgroup-name name)
7344         (setq gnus-newsgroup-marked marked)
7345         (setq gnus-newsgroup-unreads unread)
7346         (setq gnus-current-headers headers)
7347         (setq gnus-newsgroup-data data)
7348         (setq gnus-article-current gac)
7349         (setq gnus-summary-buffer summary)
7350         (setq gnus-article-buffer article-buffer)
7351         (setq gnus-original-article-buffer original)
7352         (setq gnus-current-score-file score-file)))))
7353
7354 (defun gnus-summary-last-article-p (&optional article)
7355   "Return whether ARTICLE is the last article in the buffer."
7356   (if (not (setq article (or article (gnus-summary-article-number))))
7357       t ; All non-existant numbers are the last article. :-)
7358     (not (cdr (gnus-data-find-list article)))))
7359
7360 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7361   "Insert a dummy root in the summary buffer."
7362   (beginning-of-line)
7363   (add-text-properties
7364    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7365    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7366
7367 (defvar gnus-thread-indent-array nil)
7368 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7369 (defun gnus-make-thread-indent-array ()
7370   (let ((n 200))
7371     (unless (and gnus-thread-indent-array
7372                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7373       (setq gnus-thread-indent-array (make-vector 201 "")
7374             gnus-thread-indent-array-level gnus-thread-indent-level)
7375       (while (>= n 0)
7376         (aset gnus-thread-indent-array n
7377               (make-string (* n gnus-thread-indent-level) ? ))
7378         (setq n (1- n))))))
7379
7380 (defun gnus-summary-insert-line
7381   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7382                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7383                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7384   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7385          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7386          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7387          (gnus-tmp-score-char
7388           (if (or (null gnus-summary-default-score)
7389                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7390                       gnus-summary-zcore-fuzz)) ? 
7391             (if (< gnus-tmp-score gnus-summary-default-score)
7392                 gnus-score-below-mark gnus-score-over-mark)))
7393          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7394                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7395                                   gnus-cached-mark)
7396                                  (gnus-tmp-replied gnus-replied-mark)
7397                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7398                                   gnus-saved-mark)
7399                                  (t gnus-unread-mark)))
7400          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7401          (gnus-tmp-name
7402           (cond
7403            ((string-match "(.+)" gnus-tmp-from)
7404             (substring gnus-tmp-from
7405                        (1+ (match-beginning 0)) (1- (match-end 0))))
7406            ((string-match "<[^>]+> *$" gnus-tmp-from)
7407             (let ((beg (match-beginning 0)))
7408               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7409                        (substring gnus-tmp-from (1+ (match-beginning 0))
7410                                   (1- (match-end 0))))
7411                   (substring gnus-tmp-from 0 beg))))
7412            (t gnus-tmp-from)))
7413          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7414          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7415          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7416          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7417          (buffer-read-only nil))
7418     (when (string= gnus-tmp-name "")
7419       (setq gnus-tmp-name gnus-tmp-from))
7420     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7421     (put-text-property
7422      (point)
7423      (progn (eval gnus-summary-line-format-spec) (point))
7424      'gnus-number gnus-tmp-number)
7425     (when (gnus-visual-p 'summary-highlight 'highlight)
7426       (forward-line -1)
7427       (run-hooks 'gnus-summary-update-hook)
7428       (forward-line 1))))
7429
7430 (defun gnus-summary-update-line (&optional dont-update)
7431   ;; Update summary line after change.
7432   (when (and gnus-summary-default-score
7433              (not gnus-summary-inhibit-highlight))
7434     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7435            (article (gnus-summary-article-number))
7436            (score (gnus-summary-article-score article)))
7437       (unless dont-update
7438         (if (and gnus-summary-mark-below
7439                  (< (gnus-summary-article-score)
7440                     gnus-summary-mark-below))
7441             ;; This article has a low score, so we mark it as read.
7442             (when (memq article gnus-newsgroup-unreads)
7443               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7444           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7445             ;; This article was previously marked as read on account
7446             ;; of a low score, but now it has risen, so we mark it as
7447             ;; unread.
7448             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7449         (gnus-summary-update-mark
7450          (if (or (null gnus-summary-default-score)
7451                  (<= (abs (- score gnus-summary-default-score))
7452                      gnus-summary-zcore-fuzz)) ? 
7453            (if (< score gnus-summary-default-score)
7454                gnus-score-below-mark gnus-score-over-mark)) 'score))
7455       ;; Do visual highlighting.
7456       (when (gnus-visual-p 'summary-highlight 'highlight)
7457         (run-hooks 'gnus-summary-update-hook)))))
7458
7459 (defvar gnus-tmp-new-adopts nil)
7460
7461 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7462   ;; Sum up all elements (and sub-elements) in a list.
7463   (let* ((number
7464           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7465           (cond
7466            ((and (consp thread) (cdr thread))
7467             (apply
7468              '+ 1 (mapcar
7469                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7470            ((null thread)
7471             1)
7472            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7473             1)
7474            (t 1))))
7475     (when (and level (zerop level) gnus-tmp-new-adopts)
7476       (incf number
7477             (apply '+ (mapcar
7478                        'gnus-summary-number-of-articles-in-thread
7479                        gnus-tmp-new-adopts))))
7480     (if char
7481         (if (> number 1) gnus-not-empty-thread-mark
7482           gnus-empty-thread-mark)
7483       number)))
7484
7485 (defun gnus-summary-set-local-parameters (group)
7486  "Go through the local params of GROUP and set all variable specs in that list."
7487   (let ((params (gnus-info-params (gnus-get-info group)))
7488         elem)
7489     (while params
7490       (setq elem (car params)
7491             params (cdr params))
7492       (and (consp elem)                 ; Has to be a cons.
7493            (consp (cdr elem))           ; The cdr has to be a list.
7494            (symbolp (car elem))         ; Has to be a symbol in there.
7495            (not (memq (car elem) 
7496                       '(quit-config to-address to-list to-group)))
7497            (progn                       ; So we set it.
7498              (make-local-variable (car elem))
7499              (set (car elem) (eval (nth 1 elem))))))))
7500
7501 (defun gnus-summary-read-group (group &optional show-all no-article
7502                                       kill-buffer no-display)
7503   "Start reading news in newsgroup GROUP.
7504 If SHOW-ALL is non-nil, already read articles are also listed.
7505 If NO-ARTICLE is non-nil, no article is selected initially.
7506 If NO-DISPLAY, don't generate a summary buffer."
7507   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7508   (let* ((new-group (gnus-summary-setup-buffer group))
7509          (quit-config (gnus-group-quit-config group))
7510          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7511     (cond
7512      ;; This summary buffer exists already, so we just select it.
7513      ((not new-group)
7514       (gnus-set-global-variables)
7515       (when kill-buffer
7516         (gnus-kill-or-deaden-summary kill-buffer))
7517       (gnus-configure-windows 'summary 'force)
7518       (gnus-set-mode-line 'summary)
7519       (gnus-summary-position-point)
7520       (message "")
7521       t)
7522      ;; We couldn't select this group.
7523      ((null did-select)
7524       (when (and (eq major-mode 'gnus-summary-mode)
7525                  (not (equal (current-buffer) kill-buffer)))
7526         (kill-buffer (current-buffer))
7527         (if (not quit-config)
7528             (progn
7529               (set-buffer gnus-group-buffer)
7530               (gnus-group-jump-to-group group)
7531               (gnus-group-next-unread-group 1))
7532           (if (not (buffer-name (car quit-config)))
7533               (gnus-configure-windows 'group 'force)
7534             (set-buffer (car quit-config))
7535             (and (eq major-mode 'gnus-summary-mode)
7536                  (gnus-set-global-variables))
7537             (gnus-configure-windows (cdr quit-config)))))
7538       (gnus-message 3 "Can't select group")
7539       nil)
7540      ;; The user did a `C-g' while prompting for number of articles,
7541      ;; so we exit this group.
7542      ((eq did-select 'quit)
7543       (and (eq major-mode 'gnus-summary-mode)
7544            (not (equal (current-buffer) kill-buffer))
7545            (kill-buffer (current-buffer)))
7546       (when kill-buffer
7547         (gnus-kill-or-deaden-summary kill-buffer))
7548       (if (not quit-config)
7549           (progn
7550             (set-buffer gnus-group-buffer)
7551             (gnus-group-jump-to-group group)
7552             (gnus-group-next-unread-group 1)
7553             (gnus-configure-windows 'group 'force))
7554         (if (not (buffer-name (car quit-config)))
7555             (gnus-configure-windows 'group 'force)
7556           (set-buffer (car quit-config))
7557           (and (eq major-mode 'gnus-summary-mode)
7558                (gnus-set-global-variables))
7559           (gnus-configure-windows (cdr quit-config))))
7560       ;; Finally signal the quit.
7561       (signal 'quit nil))
7562      ;; The group was successfully selected.
7563      (t
7564       (gnus-set-global-variables)
7565       ;; Save the active value in effect when the group was entered.
7566       (setq gnus-newsgroup-active
7567             (gnus-copy-sequence
7568              (gnus-active gnus-newsgroup-name)))
7569       ;; You can change the summary buffer in some way with this hook.
7570       (run-hooks 'gnus-select-group-hook)
7571       ;; Set any local variables in the group parameters.
7572       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7573       (gnus-update-format-specifications)
7574       ;; Do score processing.
7575       (when gnus-use-scoring
7576         (gnus-possibly-score-headers))
7577       ;; Check whether to fill in the gaps in the threads.
7578       (when gnus-build-sparse-threads
7579         (gnus-build-sparse-threads))
7580       ;; Find the initial limit.
7581       (if show-all
7582           (let ((gnus-newsgroup-dormant nil))
7583             (gnus-summary-initial-limit show-all))
7584         (gnus-summary-initial-limit show-all))
7585       ;; Generate the summary buffer.
7586       (unless no-display
7587         (gnus-summary-prepare))
7588       (when gnus-use-trees
7589         (gnus-tree-open group)
7590         (setq gnus-summary-highlight-line-function
7591               'gnus-tree-highlight-article))
7592       ;; If the summary buffer is empty, but there are some low-scored
7593       ;; articles or some excluded dormants, we include these in the
7594       ;; buffer.
7595       (when (and (zerop (buffer-size))
7596                  (not no-display))
7597         (cond (gnus-newsgroup-dormant
7598                (gnus-summary-limit-include-dormant))
7599               ((and gnus-newsgroup-scored show-all)
7600                (gnus-summary-limit-include-expunged))))
7601       ;; Function `gnus-apply-kill-file' must be called in this hook.
7602       (run-hooks 'gnus-apply-kill-hook)
7603       (if (and (zerop (buffer-size))
7604                (not no-display))
7605           (progn
7606             ;; This newsgroup is empty.
7607             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7608             (gnus-message 6 "No unread news")
7609             (when kill-buffer
7610               (gnus-kill-or-deaden-summary kill-buffer))
7611             ;; Return nil from this function.
7612             nil)
7613         ;; Hide conversation thread subtrees.  We cannot do this in
7614         ;; gnus-summary-prepare-hook since kill processing may not
7615         ;; work with hidden articles.
7616         (and gnus-show-threads
7617              gnus-thread-hide-subtree
7618              (gnus-summary-hide-all-threads))
7619         ;; Show first unread article if requested.
7620         (if (and (not no-article)
7621                  (not no-display)
7622                  gnus-newsgroup-unreads
7623                  gnus-auto-select-first)
7624             (unless (if (eq gnus-auto-select-first 'best)
7625                         (gnus-summary-best-unread-article)
7626                       (gnus-summary-first-unread-article))
7627               (gnus-configure-windows 'summary))
7628           ;; Don't select any articles, just move point to the first
7629           ;; article in the group.
7630           (goto-char (point-min))
7631           (gnus-summary-position-point)
7632           (gnus-set-mode-line 'summary)
7633           (gnus-configure-windows 'summary 'force))
7634         ;; If we are in async mode, we send some info to the backend.
7635         (when gnus-newsgroup-async
7636           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7637         (when kill-buffer
7638           (gnus-kill-or-deaden-summary kill-buffer))
7639         (when (get-buffer-window gnus-group-buffer t)
7640           ;; Gotta use windows, because recenter does wierd stuff if
7641           ;; the current buffer ain't the displayed window.
7642           (let ((owin (selected-window)))
7643             (select-window (get-buffer-window gnus-group-buffer t))
7644             (when (gnus-group-goto-group group)
7645               (recenter))
7646             (select-window owin))))
7647       ;; Mark this buffer as "prepared".
7648       (setq gnus-newsgroup-prepared t)
7649       t))))
7650
7651 (defun gnus-summary-prepare ()
7652   "Generate the summary buffer."
7653   (let ((buffer-read-only nil))
7654     (erase-buffer)
7655     (setq gnus-newsgroup-data nil
7656           gnus-newsgroup-data-reverse nil)
7657     (run-hooks 'gnus-summary-generate-hook)
7658     ;; Generate the buffer, either with threads or without.
7659     (when gnus-newsgroup-headers
7660       (gnus-summary-prepare-threads
7661        (if gnus-show-threads
7662            (gnus-sort-gathered-threads
7663             (funcall gnus-summary-thread-gathering-function
7664                      (gnus-sort-threads
7665                       (gnus-cut-threads (gnus-make-threads)))))
7666          ;; Unthreaded display.
7667          (gnus-sort-articles gnus-newsgroup-headers))))
7668     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7669     ;; Call hooks for modifying summary buffer.
7670     (goto-char (point-min))
7671     (run-hooks 'gnus-summary-prepare-hook)))
7672
7673 (defun gnus-gather-threads-by-subject (threads)
7674   "Gather threads by looking at Subject headers."
7675   (if (not gnus-summary-make-false-root)
7676       threads
7677     (let ((hashtb (gnus-make-hashtable 1023))
7678           (prev threads)
7679           (result threads)
7680           subject hthread whole-subject)
7681       (while threads
7682         (setq whole-subject (mail-header-subject (caar threads)))
7683         (setq subject
7684               (cond
7685                ;; Truncate the subject.
7686                ((numberp gnus-summary-gather-subject-limit)
7687                 (setq subject (gnus-simplify-subject-re whole-subject))
7688                 (if (> (length subject) gnus-summary-gather-subject-limit)
7689                     (substring subject 0 gnus-summary-gather-subject-limit)
7690                   subject))
7691                ;; Fuzzily simplify it.
7692                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7693                 (gnus-simplify-subject-fuzzy whole-subject))
7694                ;; Just remove the leading "Re:".
7695                (t
7696                 (gnus-simplify-subject-re whole-subject))))
7697
7698         (if (and gnus-summary-gather-exclude-subject
7699                  (string-match gnus-summary-gather-exclude-subject
7700                                subject))
7701             ()          ; We don't want to do anything with this article.
7702           ;; We simplify the subject before looking it up in the
7703           ;; hash table.
7704
7705           (if (setq hthread (gnus-gethash subject hashtb))
7706               (progn
7707                 ;; We enter a dummy root into the thread, if we
7708                 ;; haven't done that already.
7709                 (unless (stringp (caar hthread))
7710                   (setcar hthread (list whole-subject (car hthread))))
7711                 ;; We add this new gathered thread to this gathered
7712                 ;; thread.
7713                 (setcdr (car hthread)
7714                         (nconc (cdar hthread) (list (car threads))))
7715                 ;; Remove it from the list of threads.
7716                 (setcdr prev (cdr threads))
7717                 (setq threads prev))
7718             ;; Enter this thread into the hash table.
7719             (gnus-sethash subject threads hashtb)))
7720         (setq prev threads)
7721         (setq threads (cdr threads)))
7722       result)))
7723
7724 (defun gnus-gather-threads-by-references (threads)
7725   "Gather threads by looking at References headers."
7726   (let ((idhashtb (gnus-make-hashtable 1023))
7727         (thhashtb (gnus-make-hashtable 1023))
7728         (prev threads)
7729         (result threads)
7730         ids references id gthread gid entered)
7731     (while threads
7732       (when (setq references (mail-header-references (caar threads)))
7733         (setq id (mail-header-id (caar threads)))
7734         (setq ids (gnus-split-references references))
7735         (setq entered nil)
7736         (while ids
7737           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7738               (progn
7739                 (gnus-sethash (car ids) id idhashtb)
7740                 (gnus-sethash id threads thhashtb))
7741             (setq gthread (gnus-gethash gid thhashtb))
7742             (unless entered
7743               ;; We enter a dummy root into the thread, if we
7744               ;; haven't done that already.
7745               (unless (stringp (caar gthread))
7746                 (setcar gthread (list (mail-header-subject (caar gthread))
7747                                       (car gthread))))
7748               ;; We add this new gathered thread to this gathered
7749               ;; thread.
7750               (setcdr (car gthread)
7751                       (nconc (cdar gthread) (list (car threads)))))
7752             ;; Add it into the thread hash table.
7753             (gnus-sethash id gthread thhashtb)
7754             (setq entered t)
7755             ;; Remove it from the list of threads.
7756             (setcdr prev (cdr threads))
7757             (setq threads prev))
7758           (setq ids (cdr ids))))
7759       (setq prev threads)
7760       (setq threads (cdr threads)))
7761     result))
7762
7763 (defun gnus-sort-gathered-threads (threads)
7764   "Sort subtreads inside each gathered thread by article number."
7765   (let ((result threads))
7766     (while threads
7767       (when (stringp (caar threads))
7768         (setcdr (car threads)
7769                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7770       (setq threads (cdr threads)))
7771     result))
7772
7773 (defun gnus-make-threads ()
7774   "Go through the dependency hashtb and find the roots.  Return all threads."
7775   (let (threads)
7776     (mapatoms
7777      (lambda (refs)
7778        (unless (car (symbol-value refs))
7779          ;; These threads do not refer back to any other articles,
7780          ;; so they're roots.
7781          (setq threads (append (cdr (symbol-value refs)) threads))))
7782      gnus-newsgroup-dependencies)
7783     threads))
7784
7785 (defun gnus-build-sparse-threads ()
7786   (let ((headers gnus-newsgroup-headers)
7787         (deps gnus-newsgroup-dependencies)
7788         header references generation relations 
7789         cthread subject child end pthread relation)
7790     ;; First we create an alist of generations/relations, where 
7791     ;; generations is how much we trust the ralation, and the relation
7792     ;; is parent/child.
7793     (gnus-message 7 "Making sparse threads...")
7794     (save-excursion
7795       (nnheader-set-temp-buffer " *gnus sparse threads*")
7796       (while (setq header (pop headers))
7797         (when (and (setq references (mail-header-references header))
7798                    (not (string= references "")))
7799           (insert references)
7800           (setq child (mail-header-id header)
7801                 subject (mail-header-subject header))
7802           (setq generation 0)
7803           (while (search-backward ">" nil t)
7804             (setq end (1+ (point)))
7805             (when (search-backward "<" nil t)
7806               (push (list (incf generation) 
7807                           child (setq child (buffer-substring (point) end))
7808                           subject)
7809                     relations)))
7810           (push (list (1+ generation) child nil subject) relations)
7811           (erase-buffer)))
7812       (kill-buffer (current-buffer)))
7813     ;; Sort over trustworthiness.
7814     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7815     (while (setq relation (pop relations))
7816       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7817                 (unless (car (symbol-value cthread))
7818                   ;; Make this article the parent of these threads.
7819                   (setcar (symbol-value cthread)
7820                           (vector gnus-reffed-article-number 
7821                                   (cadddr relation) 
7822                                   "" ""
7823                                   (cadr relation) 
7824                                   (or (caddr relation) "") 0 0 "")))
7825               (set cthread (list (vector gnus-reffed-article-number
7826                                          (cadddr relation) 
7827                                          "" "" (cadr relation) 
7828                                          (or (caddr relation) "") 0 0 ""))))
7829         (push gnus-reffed-article-number gnus-newsgroup-limit)
7830         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7831         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7832               gnus-newsgroup-reads)
7833         (decf gnus-reffed-article-number)
7834         ;; Make this new thread the child of its parent.
7835         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7836             (setcdr (symbol-value pthread)
7837                     (nconc (cdr (symbol-value pthread))
7838                            (list (symbol-value cthread))))
7839           (set pthread (list nil (symbol-value cthread))))))
7840     (gnus-message 7 "Making sparse threads...done")))
7841
7842 (defun gnus-build-old-threads ()
7843   ;; Look at all the articles that refer back to old articles, and
7844   ;; fetch the headers for the articles that aren't there.  This will
7845   ;; build complete threads - if the roots haven't been expired by the
7846   ;; server, that is.
7847   (let (id heads)
7848     (mapatoms
7849      (lambda (refs)
7850        (when (not (car (symbol-value refs)))
7851          (setq heads (cdr (symbol-value refs)))
7852          (while heads
7853            (if (memq (mail-header-number (caar heads))
7854                      gnus-newsgroup-dormant)
7855                (setq heads (cdr heads))
7856              (setq id (symbol-name refs))
7857              (while (and (setq id (gnus-build-get-header id))
7858                          (not (car (gnus-gethash
7859                                     id gnus-newsgroup-dependencies)))))
7860              (setq heads nil)))))
7861      gnus-newsgroup-dependencies)))
7862
7863 (defun gnus-build-get-header (id)
7864   ;; Look through the buffer of NOV lines and find the header to
7865   ;; ID.  Enter this line into the dependencies hash table, and return
7866   ;; the id of the parent article (if any).
7867   (let ((deps gnus-newsgroup-dependencies)
7868         found header)
7869     (prog1
7870         (save-excursion
7871           (set-buffer nntp-server-buffer)
7872           (goto-char (point-min))
7873           (while (and (not found) (search-forward id nil t))
7874             (beginning-of-line)
7875             (setq found (looking-at
7876                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7877                                  (regexp-quote id))))
7878             (or found (beginning-of-line 2)))
7879           (when found
7880             (beginning-of-line)
7881             (and
7882              (setq header (gnus-nov-parse-line
7883                            (read (current-buffer)) deps))
7884              (gnus-parent-id (mail-header-references header)))))
7885       (when header
7886         (let ((number (mail-header-number header)))
7887           (push number gnus-newsgroup-limit)
7888           (push header gnus-newsgroup-headers)
7889           (if (memq number gnus-newsgroup-unselected)
7890               (progn
7891                 (push number gnus-newsgroup-unreads)
7892                 (setq gnus-newsgroup-unselected
7893                       (delq number gnus-newsgroup-unselected)))
7894             (push number gnus-newsgroup-ancient)))))))
7895
7896 (defun gnus-summary-update-article (article &optional header)
7897   "Update ARTICLE in the summary buffer."
7898   (set-buffer gnus-summary-buffer)
7899   (let* ((header (or header (gnus-summary-article-header article)))
7900          (id (mail-header-id header))
7901          (data (gnus-data-find article))
7902          (thread (gnus-id-to-thread id))
7903          (parent
7904           (gnus-id-to-thread (or (gnus-parent-id 
7905                                   (mail-header-references header))
7906                                  "tull")))
7907          (buffer-read-only nil)
7908          (old (car thread))
7909          (number (mail-header-number header))
7910          pos)
7911     (when thread
7912       (setcar thread nil)
7913       (when parent
7914         (delq thread parent))
7915       (if (gnus-summary-insert-subject id header)
7916           ;; Set the (possibly) new article number in the data structure.
7917           (gnus-data-set-number data (gnus-id-to-article id))
7918         (setcar thread old)
7919         nil))))
7920
7921 (defun gnus-rebuild-thread (id)
7922   "Rebuild the thread containing ID."
7923   (let ((buffer-read-only nil)
7924         current thread data)
7925     (if (not gnus-show-threads)
7926         (setq thread (list (car (gnus-id-to-thread id))))
7927       ;; Get the thread this article is part of.
7928       (setq thread (gnus-remove-thread id)))
7929     (setq current (save-excursion
7930                     (and (zerop (forward-line -1))
7931                          (gnus-summary-article-number))))
7932     ;; If this is a gathered thread, we have to go some re-gathering.
7933     (when (stringp (car thread))
7934       (let ((subject (car thread))
7935             roots thr)
7936         (setq thread (cdr thread))
7937         (while thread
7938           (unless (memq (setq thr (gnus-id-to-thread
7939                                       (gnus-root-id
7940                                        (mail-header-id (caar thread)))))
7941                         roots)
7942             (push thr roots))
7943           (setq thread (cdr thread)))
7944         ;; We now have all (unique) roots.
7945         (if (= (length roots) 1)
7946             ;; All the loose roots are now one solid root.
7947             (setq thread (car roots))
7948           (setq thread (cons subject (gnus-sort-threads roots))))))
7949     (let (threads)
7950       ;; We then insert this thread into the summary buffer.
7951       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7952         (gnus-summary-prepare-threads (list thread))
7953         (setq data (nreverse gnus-newsgroup-data))
7954         (setq threads gnus-newsgroup-threads))
7955       ;; We splice the new data into the data structure.
7956       (gnus-data-enter-list current data)
7957       (gnus-data-compute-positions)
7958       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7959
7960 (defun gnus-id-to-thread (id)
7961   "Return the (sub-)thread where ID appears."
7962   (gnus-gethash id gnus-newsgroup-dependencies))
7963
7964 (defun gnus-id-to-article (id)
7965   "Return the article number of ID."
7966   (let ((thread (gnus-id-to-thread id)))
7967     (when thread
7968       (mail-header-number (car thread)))))
7969
7970 (defun gnus-id-to-header (id)
7971   "Return the article headers of ID."
7972   (car (gnus-id-to-thread id)))
7973
7974 (defun gnus-article-displayed-root-p (article)
7975   "Say whether ARTICLE is a root(ish) article."
7976   (let ((level (gnus-summary-thread-level article))
7977         particle)
7978     (cond 
7979      ((null level) nil)
7980      ((zerop level) t)
7981      ((and (= 1 level)
7982            (null (setq particle (gnus-id-to-article
7983                                  (gnus-parent-id 
7984                                   (mail-header-references 
7985                                    (gnus-summary-article-header article))))))
7986            (null (gnus-summary-thread-level particle)))))))
7987
7988 (defun gnus-root-id (id)
7989   "Return the id of the root of the thread where ID appears."
7990   (let (last-id prev)
7991     (while (and id (setq prev (car (gnus-gethash 
7992                                     id gnus-newsgroup-dependencies))))
7993       (setq last-id id
7994             id (gnus-parent-id (mail-header-references prev))))
7995     last-id))
7996
7997 (defun gnus-remove-thread (id &optional dont-remove)
7998   "Remove the thread that has ID in it."
7999   (let ((dep gnus-newsgroup-dependencies)
8000         headers thread last-id)
8001     ;; First go up in this thread until we find the root.
8002     (setq last-id (gnus-root-id id))
8003     (setq headers (list (car (gnus-id-to-thread last-id))
8004                         (caadr (gnus-id-to-thread last-id))))
8005     ;; We have now found the real root of this thread.  It might have
8006     ;; been gathered into some loose thread, so we have to search
8007     ;; through the threads to find the thread we wanted.
8008     (let ((threads gnus-newsgroup-threads)
8009           sub)
8010       (while threads
8011         (setq sub (car threads))
8012         (if (stringp (car sub))
8013             ;; This is a gathered threads, so we look at the roots
8014             ;; below it to find whether this article in in this
8015             ;; gathered root.
8016             (progn
8017               (setq sub (cdr sub))
8018               (while sub
8019                 (when (member (caar sub) headers)
8020                   (setq thread (car threads)
8021                         threads nil
8022                         sub nil))
8023                 (setq sub (cdr sub))))
8024           ;; It's an ordinary thread, so we check it.
8025           (when (eq (car sub) (car headers))
8026             (setq thread sub
8027                   threads nil)))
8028         (setq threads (cdr threads)))
8029       ;; If this article is in no thread, then it's a root.
8030       (if thread
8031           (unless dont-remove
8032             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8033         (setq thread (gnus-gethash last-id dep)))
8034       (when thread
8035         (prog1
8036             thread ; We return this thread.
8037           (unless dont-remove
8038             (if (stringp (car thread))
8039                 (progn
8040                   ;; If we use dummy roots, then we have to remove the
8041                   ;; dummy root as well.
8042                   (when (eq gnus-summary-make-false-root 'dummy)
8043                     ;; Uhm.
8044                     )
8045                   (setq thread (cdr thread))
8046                   (while thread
8047                     (gnus-remove-thread-1 (car thread))
8048                     (setq thread (cdr thread))))
8049               (gnus-remove-thread-1 thread))))))))
8050
8051 (defun gnus-remove-thread-1 (thread)
8052   "Remove the thread THREAD recursively."
8053   (let ((number (mail-header-number (car thread)))
8054         pos)
8055     (when (setq pos (text-property-any
8056                      (point-min) (point-max) 'gnus-number number))
8057       (goto-char pos)
8058       (gnus-delete-line)
8059       (gnus-data-remove number))
8060     (setq thread (cdr thread))
8061     (while thread
8062       (gnus-remove-thread-1 (pop thread)))))
8063
8064 (defun gnus-sort-threads (threads)
8065   "Sort THREADS."
8066   (if (not gnus-thread-sort-functions)
8067       threads
8068     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8069                     (car gnus-thread-sort-functions)
8070                   `(lambda (t1 t2)
8071                      ,(gnus-make-sort-function 
8072                        (reverse gnus-thread-sort-functions))))))
8073       (gnus-message 7 "Sorting threads...")
8074       (prog1
8075           (sort threads func)
8076         (gnus-message 7 "Sorting threads...done")))))
8077
8078 (defun gnus-sort-articles (articles)
8079   "Sort ARTICLES."
8080   (when gnus-article-sort-functions
8081     (let ((func (if (= 1 (length gnus-article-sort-functions))
8082                     (car gnus-article-sort-functions)
8083                   `(lambda (t1 t2)
8084                      ,(gnus-make-sort-function 
8085                        (reverse gnus-article-sort-functions))))))
8086       (gnus-message 7 "Sorting articles...")
8087       (prog1
8088           (setq gnus-newsgroup-headers (sort articles func))
8089         (gnus-message 7 "Sorting articles...done")))))
8090
8091 (defun gnus-make-sort-function (funs)
8092   "Return a composite sort condition based on the functions in FUNC."
8093   (if (cdr funs)
8094       `(or (,(car funs) t1 t2)
8095            (and (not (,(car funs) t2 t1))
8096                 ,(gnus-make-sort-function (cdr funs))))
8097     `(,(car funs) t1 t2)))
8098                  
8099 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8100 (defmacro gnus-thread-header (thread)
8101   ;; Return header of first article in THREAD.
8102   ;; Note that THREAD must never, ever be anything else than a variable -
8103   ;; using some other form will lead to serious barfage.
8104   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8105   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8106   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8107         (vector thread) 2))
8108
8109 (defsubst gnus-article-sort-by-number (h1 h2)
8110   "Sort articles by article number."
8111   (< (mail-header-number h1)
8112      (mail-header-number h2)))
8113
8114 (defun gnus-thread-sort-by-number (h1 h2)
8115   "Sort threads by root article number."
8116   (gnus-article-sort-by-number
8117    (gnus-thread-header h1) (gnus-thread-header h2)))
8118
8119 (defsubst gnus-article-sort-by-author (h1 h2)
8120   "Sort articles by root author."
8121   (string-lessp
8122    (let ((extract (funcall
8123                    gnus-extract-address-components
8124                    (mail-header-from h1))))
8125      (or (car extract) (cdr extract)))
8126    (let ((extract (funcall
8127                    gnus-extract-address-components
8128                    (mail-header-from h2))))
8129      (or (car extract) (cdr extract)))))
8130
8131 (defun gnus-thread-sort-by-author (h1 h2)
8132   "Sort threads by root author."
8133   (gnus-article-sort-by-author
8134    (gnus-thread-header h1)  (gnus-thread-header h2)))
8135
8136 (defsubst gnus-article-sort-by-subject (h1 h2)
8137   "Sort articles by root subject."
8138   (string-lessp
8139    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8140    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8141
8142 (defun gnus-thread-sort-by-subject (h1 h2)
8143   "Sort threads by root subject."
8144   (gnus-article-sort-by-subject
8145    (gnus-thread-header h1) (gnus-thread-header h2)))
8146
8147 (defsubst gnus-article-sort-by-date (h1 h2)
8148   "Sort articles by root article date."
8149   (string-lessp
8150    (gnus-sortable-date (mail-header-date h1))
8151    (gnus-sortable-date (mail-header-date h2))))
8152
8153 (defun gnus-thread-sort-by-date (h1 h2)
8154   "Sort threads by root article date."
8155   (gnus-article-sort-by-date
8156    (gnus-thread-header h1) (gnus-thread-header h2)))
8157
8158 (defsubst gnus-article-sort-by-score (h1 h2)
8159   "Sort articles by root article score.
8160 Unscored articles will be counted as having a score of zero."
8161   (> (or (cdr (assq (mail-header-number h1)
8162                     gnus-newsgroup-scored))
8163          gnus-summary-default-score 0)
8164      (or (cdr (assq (mail-header-number h2)
8165                     gnus-newsgroup-scored))
8166          gnus-summary-default-score 0)))
8167
8168 (defun gnus-thread-sort-by-score (h1 h2)
8169   "Sort threads by root article score."
8170   (gnus-article-sort-by-score
8171    (gnus-thread-header h1) (gnus-thread-header h2)))
8172
8173 (defun gnus-thread-sort-by-total-score (h1 h2)
8174   "Sort threads by the sum of all scores in the thread.
8175 Unscored articles will be counted as having a score of zero."
8176   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8177
8178 (defun gnus-thread-total-score (thread)
8179   ;;  This function find the total score of THREAD.
8180   (if (consp thread)
8181       (if (stringp (car thread))
8182           (apply gnus-thread-score-function 0
8183                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8184         (gnus-thread-total-score-1 thread))
8185     (gnus-thread-total-score-1 (list thread))))
8186
8187 (defun gnus-thread-total-score-1 (root)
8188   ;; This function find the total score of the thread below ROOT.
8189   (setq root (car root))
8190   (apply gnus-thread-score-function
8191          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8192              gnus-summary-default-score 0)
8193          (mapcar 'gnus-thread-total-score
8194                  (cdr (gnus-gethash (mail-header-id root)
8195                                     gnus-newsgroup-dependencies)))))
8196
8197 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8198 (defvar gnus-tmp-prev-subject nil)
8199 (defvar gnus-tmp-false-parent nil)
8200 (defvar gnus-tmp-root-expunged nil)
8201 (defvar gnus-tmp-dummy-line nil)
8202
8203 (defun gnus-summary-prepare-threads (threads)
8204   "Prepare summary buffer from THREADS and indentation LEVEL.
8205 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8206 or a straight list of headers."
8207   (gnus-message 7 "Generating summary...")
8208
8209   (setq gnus-newsgroup-threads threads)
8210   (beginning-of-line)
8211
8212   (let ((gnus-tmp-level 0)
8213         (default-score (or gnus-summary-default-score 0))
8214         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8215         thread number subject stack state gnus-tmp-gathered beg-match
8216         new-roots gnus-tmp-new-adopts thread-end
8217         gnus-tmp-header gnus-tmp-unread
8218         gnus-tmp-replied gnus-tmp-subject-or-nil
8219         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8220         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8221         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8222
8223     (setq gnus-tmp-prev-subject nil)
8224
8225     (if (vectorp (car threads))
8226         ;; If this is a straight (sic) list of headers, then a
8227         ;; threaded summary display isn't required, so we just create
8228         ;; an unthreaded one.
8229         (gnus-summary-prepare-unthreaded threads)
8230
8231       ;; Do the threaded display.
8232
8233       (while (or threads stack gnus-tmp-new-adopts new-roots)
8234
8235         (if (and (= gnus-tmp-level 0)
8236                  (not (setq gnus-tmp-dummy-line nil))
8237                  (or (not stack)
8238                      (= (caar stack) 0))
8239                  (not gnus-tmp-false-parent)
8240                  (or gnus-tmp-new-adopts new-roots))
8241             (if gnus-tmp-new-adopts
8242                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8243                       thread (list (car gnus-tmp-new-adopts))
8244                       gnus-tmp-header (caar thread)
8245                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8246               (if new-roots
8247                   (setq thread (list (car new-roots))
8248                         gnus-tmp-header (caar thread)
8249                         new-roots (cdr new-roots))))
8250
8251           (if threads
8252               ;; If there are some threads, we do them before the
8253               ;; threads on the stack.
8254               (setq thread threads
8255                     gnus-tmp-header (caar thread))
8256             ;; There were no current threads, so we pop something off
8257             ;; the stack.
8258             (setq state (car stack)
8259                   gnus-tmp-level (car state)
8260                   thread (cdr state)
8261                   stack (cdr stack)
8262                   gnus-tmp-header (caar thread))))
8263
8264         (setq gnus-tmp-false-parent nil)
8265         (setq gnus-tmp-root-expunged nil)
8266         (setq thread-end nil)
8267
8268         (if (stringp gnus-tmp-header)
8269             ;; The header is a dummy root.
8270             (cond
8271              ((eq gnus-summary-make-false-root 'adopt)
8272               ;; We let the first article adopt the rest.
8273               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8274                                                (cddar thread)))
8275               (setq gnus-tmp-gathered
8276                     (nconc (mapcar
8277                             (lambda (h) (mail-header-number (car h)))
8278                             (cddar thread))
8279                            gnus-tmp-gathered))
8280               (setq thread (cons (list (caar thread)
8281                                        (cadar thread))
8282                                  (cdr thread)))
8283               (setq gnus-tmp-level -1
8284                     gnus-tmp-false-parent t))
8285              ((eq gnus-summary-make-false-root 'empty)
8286               ;; We print adopted articles with empty subject fields.
8287               (setq gnus-tmp-gathered
8288                     (nconc (mapcar
8289                             (lambda (h) (mail-header-number (car h)))
8290                             (cddar thread))
8291                            gnus-tmp-gathered))
8292               (setq gnus-tmp-level -1))
8293              ((eq gnus-summary-make-false-root 'dummy)
8294               ;; We remember that we probably want to output a dummy
8295               ;; root.
8296               (setq gnus-tmp-dummy-line gnus-tmp-header)
8297               (setq gnus-tmp-prev-subject gnus-tmp-header))
8298              (t
8299               ;; We do not make a root for the gathered
8300               ;; sub-threads at all.
8301               (setq gnus-tmp-level -1)))
8302
8303           (setq number (mail-header-number gnus-tmp-header)
8304                 subject (mail-header-subject gnus-tmp-header))
8305
8306           (cond
8307            ;; If the thread has changed subject, we might want to make
8308            ;; this subthread into a root.
8309            ((and (null gnus-thread-ignore-subject)
8310                  (not (zerop gnus-tmp-level))
8311                  gnus-tmp-prev-subject
8312                  (not (inline
8313                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8314             (setq new-roots (nconc new-roots (list (car thread)))
8315                   thread-end t
8316                   gnus-tmp-header nil))
8317            ;; If the article lies outside the current limit,
8318            ;; then we do not display it.
8319            ((and (not (memq number gnus-newsgroup-limit))
8320                  (not gnus-tmp-dummy-line))
8321             (setq gnus-tmp-gathered
8322                   (nconc (mapcar
8323                           (lambda (h) (mail-header-number (car h)))
8324                           (cdar thread))
8325                          gnus-tmp-gathered))
8326             (setq gnus-tmp-new-adopts (if (cdar thread)
8327                                           (append gnus-tmp-new-adopts
8328                                                   (cdar thread))
8329                                         gnus-tmp-new-adopts)
8330                   thread-end t
8331                   gnus-tmp-header nil)
8332             (when (zerop gnus-tmp-level)
8333               (setq gnus-tmp-root-expunged t)))
8334            ;; Perhaps this article is to be marked as read?
8335            ((and gnus-summary-mark-below
8336                  (< (or (cdr (assq number gnus-newsgroup-scored))
8337                         default-score)
8338                     gnus-summary-mark-below)
8339                  ;; Don't touch sparse articles.
8340                  (not (memq number gnus-newsgroup-sparse)))
8341             (setq gnus-newsgroup-unreads
8342                   (delq number gnus-newsgroup-unreads))
8343             (if gnus-newsgroup-auto-expire
8344                 (push number gnus-newsgroup-expirable)
8345               (push (cons number gnus-low-score-mark)
8346                     gnus-newsgroup-reads))))
8347
8348           (when gnus-tmp-header
8349             ;; We may have an old dummy line to output before this
8350             ;; article.
8351             (when gnus-tmp-dummy-line
8352               (gnus-summary-insert-dummy-line
8353                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8354               (setq gnus-tmp-dummy-line nil))
8355
8356             ;; Compute the mark.
8357             (setq
8358              gnus-tmp-unread
8359              (cond
8360               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8361               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8362               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8363               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8364               (t (or (cdr (assq number gnus-newsgroup-reads))
8365                      gnus-ancient-mark))))
8366
8367             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8368                                   gnus-tmp-header gnus-tmp-level)
8369                   gnus-newsgroup-data)
8370
8371             ;; Actually insert the line.
8372             (setq
8373              gnus-tmp-subject-or-nil
8374              (cond
8375               ((and gnus-thread-ignore-subject
8376                     gnus-tmp-prev-subject
8377                     (not (inline (gnus-subject-equal
8378                                   gnus-tmp-prev-subject subject))))
8379                subject)
8380               ((zerop gnus-tmp-level)
8381                (if (and (eq gnus-summary-make-false-root 'empty)
8382                         (memq number gnus-tmp-gathered)
8383                         gnus-tmp-prev-subject
8384                         (inline (gnus-subject-equal
8385                                  gnus-tmp-prev-subject subject)))
8386                    gnus-summary-same-subject
8387                  subject))
8388               (t gnus-summary-same-subject)))
8389             (if (and (eq gnus-summary-make-false-root 'adopt)
8390                      (= gnus-tmp-level 1)
8391                      (memq number gnus-tmp-gathered))
8392                 (setq gnus-tmp-opening-bracket ?\<
8393                       gnus-tmp-closing-bracket ?\>)
8394               (setq gnus-tmp-opening-bracket ?\[
8395                     gnus-tmp-closing-bracket ?\]))
8396             (setq
8397              gnus-tmp-indentation
8398              (aref gnus-thread-indent-array gnus-tmp-level)
8399              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8400              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8401                                 gnus-summary-default-score 0)
8402              gnus-tmp-score-char
8403              (if (or (null gnus-summary-default-score)
8404                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8405                          gnus-summary-zcore-fuzz)) ? 
8406                (if (< gnus-tmp-score gnus-summary-default-score)
8407                    gnus-score-below-mark gnus-score-over-mark))
8408              gnus-tmp-replied
8409              (cond ((memq number gnus-newsgroup-processable)
8410                     gnus-process-mark)
8411                    ((memq number gnus-newsgroup-cached)
8412                     gnus-cached-mark)
8413                    ((memq number gnus-newsgroup-replied)
8414                     gnus-replied-mark)
8415                    (t gnus-unread-mark))
8416              gnus-tmp-from (mail-header-from gnus-tmp-header)
8417              gnus-tmp-name
8418              (cond
8419               ((string-match "(.+)" gnus-tmp-from)
8420                (substring gnus-tmp-from
8421                           (1+ (match-beginning 0)) (1- (match-end 0))))
8422               ((string-match "<[^>]+> *$" gnus-tmp-from)
8423                (setq beg-match (match-beginning 0))
8424                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8425                         (substring gnus-tmp-from (1+ (match-beginning 0))
8426                                    (1- (match-end 0))))
8427                    (substring gnus-tmp-from 0 beg-match)))
8428               (t gnus-tmp-from)))
8429             (when (string= gnus-tmp-name "")
8430               (setq gnus-tmp-name gnus-tmp-from))
8431             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8432             (put-text-property
8433              (point)
8434              (progn (eval gnus-summary-line-format-spec) (point))
8435              'gnus-number number)
8436             (when gnus-visual-p
8437               (forward-line -1)
8438               (run-hooks 'gnus-summary-update-hook)
8439               (forward-line 1))
8440
8441             (setq gnus-tmp-prev-subject subject)))
8442
8443         (when (nth 1 thread)
8444           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8445         (incf gnus-tmp-level)
8446         (setq threads (if thread-end nil (cdar thread)))
8447         (unless threads
8448           (setq gnus-tmp-level 0)))))
8449   (gnus-message 7 "Generating summary...done"))
8450
8451 (defun gnus-summary-prepare-unthreaded (headers)
8452   "Generate an unthreaded summary buffer based on HEADERS."
8453   (let (header number mark)
8454
8455     (while headers
8456       (setq header (car headers)
8457             headers (cdr headers)
8458             number (mail-header-number header))
8459
8460       ;; We may have to root out some bad articles...
8461       (when (memq number gnus-newsgroup-limit)
8462         (when (and gnus-summary-mark-below
8463                    (< (or (cdr (assq number gnus-newsgroup-scored))
8464                           gnus-summary-default-score 0)
8465                       gnus-summary-mark-below))
8466           (setq gnus-newsgroup-unreads
8467                 (delq number gnus-newsgroup-unreads))
8468           (if gnus-newsgroup-auto-expire
8469               (push number gnus-newsgroup-expirable)
8470             (push (cons number gnus-low-score-mark)
8471                   gnus-newsgroup-reads)))
8472
8473         (setq mark
8474               (cond
8475                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8476                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8477                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8478                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8479                (t (or (cdr (assq number gnus-newsgroup-reads))
8480                       gnus-ancient-mark))))
8481         (setq gnus-newsgroup-data
8482               (cons (gnus-data-make number mark (1+ (point)) header 0)
8483                     gnus-newsgroup-data))
8484         (gnus-summary-insert-line
8485          header 0 nil mark (memq number gnus-newsgroup-replied)
8486          (memq number gnus-newsgroup-expirable)
8487          (mail-header-subject header) nil
8488          (cdr (assq number gnus-newsgroup-scored))
8489          (memq number gnus-newsgroup-processable))))))
8490
8491 (defun gnus-select-newsgroup (group &optional read-all)
8492   "Select newsgroup GROUP.
8493 If READ-ALL is non-nil, all articles in the group are selected."
8494   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8495          (info (nth 2 entry))
8496          articles fetched-articles cached)
8497
8498     (or (gnus-check-server
8499          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8500         (error "Couldn't open server"))
8501
8502     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8503         (gnus-activate-group group) ; Or we can activate it...
8504         (progn ; Or we bug out.
8505           (when (equal major-mode 'gnus-summary-mode)
8506             (kill-buffer (current-buffer)))
8507           (error "Couldn't request group %s: %s"
8508                  group (gnus-status-message group))))
8509
8510     (setq gnus-newsgroup-name group)
8511     (setq gnus-newsgroup-unselected nil)
8512     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8513
8514     (and gnus-asynchronous
8515          (gnus-check-backend-function
8516           'request-asynchronous gnus-newsgroup-name)
8517          (setq gnus-newsgroup-async
8518                (gnus-request-asynchronous gnus-newsgroup-name)))
8519
8520     ;; Adjust and set lists of article marks.
8521     (when info
8522       (gnus-adjust-marked-articles info))
8523
8524     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8525     (when (gnus-virtual-group-p group)
8526       (setq cached gnus-newsgroup-cached))
8527
8528     (setq gnus-newsgroup-unreads
8529           (gnus-set-difference
8530            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8531            gnus-newsgroup-dormant))
8532
8533     (setq gnus-newsgroup-processable nil)
8534
8535     (setq articles (gnus-articles-to-read group read-all))
8536
8537     (cond
8538      ((null articles)
8539       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8540       'quit)
8541      ((eq articles 0) nil)
8542      (t
8543       ;; Init the dependencies hash table.
8544       (setq gnus-newsgroup-dependencies
8545             (gnus-make-hashtable (length articles)))
8546       ;; Retrieve the headers and read them in.
8547       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8548       (setq gnus-newsgroup-headers
8549             (if (eq 'nov
8550                     (setq gnus-headers-retrieved-by
8551                           (gnus-retrieve-headers
8552                            articles gnus-newsgroup-name
8553                            ;; We might want to fetch old headers, but
8554                            ;; not if there is only 1 article.
8555                            (and gnus-fetch-old-headers
8556                                 (or (and
8557                                      (not (eq gnus-fetch-old-headers 'some))
8558                                      (not (numberp gnus-fetch-old-headers)))
8559                                     (> (length articles) 1))))))
8560                 (gnus-get-newsgroup-headers-xover articles)
8561               (gnus-get-newsgroup-headers)))
8562       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8563
8564       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8565       (when cached
8566         (setq gnus-newsgroup-cached cached))
8567
8568       ;; Set the initial limit.
8569       (setq gnus-newsgroup-limit (copy-sequence articles))
8570       ;; Remove canceled articles from the list of unread articles.
8571       (setq gnus-newsgroup-unreads
8572             (gnus-set-sorted-intersection
8573              gnus-newsgroup-unreads
8574              (setq fetched-articles
8575                    (mapcar (lambda (headers) (mail-header-number headers))
8576                            gnus-newsgroup-headers))))
8577       ;; Removed marked articles that do not exist.
8578       (gnus-update-missing-marks
8579        (gnus-sorted-complement fetched-articles articles))
8580       ;; We might want to build some more threads first.
8581       (and gnus-fetch-old-headers
8582            (eq gnus-headers-retrieved-by 'nov)
8583            (gnus-build-old-threads))
8584       ;; Check whether auto-expire is to be done in this group.
8585       (setq gnus-newsgroup-auto-expire
8586             (gnus-group-auto-expirable-p group))
8587       ;; Set up the article buffer now, if necessary.
8588       (unless gnus-single-article-buffer
8589         (gnus-article-setup-buffer))
8590       ;; First and last article in this newsgroup.
8591       (and gnus-newsgroup-headers
8592            (setq gnus-newsgroup-begin
8593                  (mail-header-number (car gnus-newsgroup-headers)))
8594            (setq gnus-newsgroup-end
8595                  (mail-header-number
8596                   (gnus-last-element gnus-newsgroup-headers))))
8597       (setq gnus-reffed-article-number -1)
8598       ;; GROUP is successfully selected.
8599       (or gnus-newsgroup-headers t)))))
8600
8601 (defun gnus-articles-to-read (group read-all)
8602   ;; Find out what articles the user wants to read.
8603   (let* ((articles
8604           ;; Select all articles if `read-all' is non-nil, or if there
8605           ;; are no unread articles.
8606           (if (or read-all
8607                   (and (zerop (length gnus-newsgroup-marked))
8608                        (zerop (length gnus-newsgroup-unreads))))
8609               (gnus-uncompress-range (gnus-active group))
8610             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8611                           (copy-sequence gnus-newsgroup-unreads))
8612                   '<)))
8613          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8614          (scored (length scored-list))
8615          (number (length articles))
8616          (marked (+ (length gnus-newsgroup-marked)
8617                     (length gnus-newsgroup-dormant)))
8618          (select
8619           (cond
8620            ((numberp read-all)
8621             read-all)
8622            (t
8623             (condition-case ()
8624                 (cond
8625                  ((and (or (<= scored marked) (= scored number))
8626                        (numberp gnus-large-newsgroup)
8627                        (> number gnus-large-newsgroup))
8628                   (let ((input
8629                          (read-string
8630                           (format
8631                            "How many articles from %s (default %d): "
8632                            gnus-newsgroup-name number))))
8633                     (if (string-match "^[ \t]*$" input) number input)))
8634                  ((and (> scored marked) (< scored number))
8635                   (let ((input
8636                          (read-string
8637                           (format "%s %s (%d scored, %d total): "
8638                                   "How many articles from"
8639                                   group scored number))))
8640                     (if (string-match "^[ \t]*$" input)
8641                         number input)))
8642                  (t number))
8643               (quit nil))))))
8644     (setq select (if (stringp select) (string-to-number select) select))
8645     (if (or (null select) (zerop select))
8646         select
8647       (if (and (not (zerop scored)) (<= (abs select) scored))
8648           (progn
8649             (setq articles (sort scored-list '<))
8650             (setq number (length articles)))
8651         (setq articles (copy-sequence articles)))
8652
8653       (if (< (abs select) number)
8654           (if (< select 0)
8655               ;; Select the N oldest articles.
8656               (setcdr (nthcdr (1- (abs select)) articles) nil)
8657             ;; Select the N most recent articles.
8658             (setq articles (nthcdr (- number select) articles))))
8659       (setq gnus-newsgroup-unselected
8660             (gnus-sorted-intersection
8661              gnus-newsgroup-unreads
8662              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8663       articles)))
8664
8665 (defun gnus-killed-articles (killed articles)
8666   (let (out)
8667     (while articles
8668       (if (inline (gnus-member-of-range (car articles) killed))
8669           (setq out (cons (car articles) out)))
8670       (setq articles (cdr articles)))
8671     out))
8672
8673 (defun gnus-uncompress-marks (marks)
8674   "Uncompress the mark ranges in MARKS."
8675   (let ((uncompressed '(score bookmark))
8676         out)
8677     (while marks
8678       (if (memq (caar marks) uncompressed)
8679           (push (car marks) out)
8680         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8681       (setq marks (cdr marks)))
8682     out))
8683
8684 (defun gnus-adjust-marked-articles (info)
8685   "Set all article lists and remove all marks that are no longer legal."
8686   (let* ((marked-lists (gnus-info-marks info))
8687          (active (gnus-active (gnus-info-group info)))
8688          (min (car active))
8689          (max (cdr active))
8690          (types gnus-article-mark-lists)
8691          (uncompressed '(score bookmark))
8692          marks var articles article mark)
8693
8694     (while marked-lists
8695       (setq marks (pop marked-lists))
8696       (set (setq var (intern (format "gnus-newsgroup-%s"
8697                                      (car (rassq (setq mark (car marks))
8698                                                  types)))))
8699            (if (memq (car marks) uncompressed) (cdr marks)
8700              (gnus-uncompress-range (cdr marks))))
8701
8702       (setq articles (symbol-value var))
8703
8704       ;; All articles have to be subsets of the active articles.
8705       (cond
8706        ;; Adjust "simple" lists.
8707        ((memq mark '(tick dormant expirable reply killed save))
8708         (while articles
8709           (when (or (< (setq article (pop articles)) min) (> article max))
8710             (set var (delq article (symbol-value var))))))
8711        ;; Adjust assocs.
8712        ((memq mark '(score bookmark))
8713         (while articles
8714           (when (or (< (car (setq article (pop articles))) min)
8715                     (> (car article) max))
8716             (set var (delq article (symbol-value var))))))))))
8717
8718 (defun gnus-update-missing-marks (missing)
8719   "Go through the list of MISSING articles and remove them mark lists."
8720   (when missing
8721     (let ((types gnus-article-mark-lists)
8722           var m)
8723       ;; Go through all types.
8724       (while types
8725         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8726         (when (symbol-value var)
8727           ;; This list has articles.  So we delete all missing articles
8728           ;; from it.
8729           (setq m missing)
8730           (while m
8731             (set var (delq (pop m) (symbol-value var)))))))))
8732
8733 (defun gnus-update-marks ()
8734   "Enter the various lists of marked articles into the newsgroup info list."
8735   (let ((types gnus-article-mark-lists)
8736         (info (gnus-get-info gnus-newsgroup-name))
8737         (uncompressed '(score bookmark killed))
8738         type list newmarked symbol)
8739     (when info
8740       ;; Add all marks lists that are non-nil to the list of marks lists.
8741       (while types
8742         (setq type (pop types))
8743         (when (setq list (symbol-value
8744                           (setq symbol
8745                                 (intern (format "gnus-newsgroup-%s"
8746                                                 (car type))))))
8747           (push (cons (cdr type)
8748                       (if (memq (cdr type) uncompressed) list
8749                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8750                 newmarked)))
8751
8752       ;; Enter these new marks into the info of the group.
8753       (if (nthcdr 3 info)
8754           (setcar (nthcdr 3 info) newmarked)
8755         ;; Add the marks lists to the end of the info.
8756         (when newmarked
8757           (setcdr (nthcdr 2 info) (list newmarked))))
8758
8759       ;; Cut off the end of the info if there's nothing else there.
8760       (let ((i 5))
8761         (while (and (> i 2)
8762                     (not (nth i info)))
8763           (when (nthcdr (decf i) info)
8764             (setcdr (nthcdr i info) nil)))))))
8765
8766 (defun gnus-add-marked-articles (group type articles &optional info force)
8767   ;; Add ARTICLES of TYPE to the info of GROUP.
8768   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8769   ;; add, but replace marked articles of TYPE with ARTICLES.
8770   (let ((info (or info (gnus-get-info group)))
8771         (uncompressed '(score bookmark killed))
8772         marked m)
8773     (or (not info)
8774         (and (not (setq marked (nthcdr 3 info)))
8775              (or (null articles)
8776                  (setcdr (nthcdr 2 info)
8777                          (list (list (cons type (gnus-compress-sequence
8778                                                  articles t)))))))
8779         (and (not (setq m (assq type (car marked))))
8780              (or (null articles)
8781                  (setcar marked
8782                          (cons (cons type (gnus-compress-sequence articles t) )
8783                                (car marked)))))
8784         (if force
8785             (if (null articles)
8786                 (setcar (nthcdr 3 info)
8787                         (delq (assq type (car marked)) (car marked)))
8788               (setcdr m (gnus-compress-sequence articles t)))
8789           (setcdr m (gnus-compress-sequence
8790                      (sort (nconc (gnus-uncompress-range (cdr m))
8791                                   (copy-sequence articles)) '<) t))))))
8792
8793 (defun gnus-set-mode-line (where)
8794   "This function sets the mode line of the article or summary buffers.
8795 If WHERE is `summary', the summary mode line format will be used."
8796   ;; Is this mode line one we keep updated?
8797   (when (memq where gnus-updated-mode-lines)
8798     (let (mode-string)
8799       (save-excursion
8800         ;; We evaluate this in the summary buffer since these
8801         ;; variables are buffer-local to that buffer.
8802         (set-buffer gnus-summary-buffer)
8803         ;; We bind all these variables that are used in the `eval' form
8804         ;; below.
8805         (let* ((mformat (symbol-value
8806                          (intern
8807                           (format "gnus-%s-mode-line-format-spec" where))))
8808                (gnus-tmp-group-name gnus-newsgroup-name)
8809                (gnus-tmp-article-number (or gnus-current-article 0))
8810                (gnus-tmp-unread gnus-newsgroup-unreads)
8811                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8812                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8813                (gnus-tmp-unread-and-unselected
8814                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8815                             (zerop gnus-tmp-unselected)) "")
8816                       ((zerop gnus-tmp-unselected)
8817                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8818                       (t (format "{%d(+%d) more}"
8819                                  gnus-tmp-unread-and-unticked
8820                                  gnus-tmp-unselected))))
8821                (gnus-tmp-subject
8822                 (if (and gnus-current-headers
8823                          (vectorp gnus-current-headers))
8824                     (mail-header-subject gnus-current-headers) ""))
8825                max-len
8826                gnus-tmp-header);; passed as argument to any user-format-funcs
8827           (setq mode-string (eval mformat))
8828           (setq max-len (max 4 (if gnus-mode-non-string-length
8829                                    (- (frame-width)
8830                                       gnus-mode-non-string-length)
8831                                  (length mode-string))))
8832           ;; We might have to chop a bit of the string off...
8833           (when (> (length mode-string) max-len)
8834             (setq mode-string
8835                   (concat (gnus-truncate-string mode-string (- max-len 3))
8836                           "...")))
8837           ;; Pad the mode string a bit.
8838           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8839       ;; Update the mode line.
8840       (setq mode-line-buffer-identification (list mode-string))
8841       (set-buffer-modified-p t))))
8842
8843 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8844   "Go through the HEADERS list and add all Xrefs to a hash table.
8845 The resulting hash table is returned, or nil if no Xrefs were found."
8846   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8847          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8848          (xref-hashtb (make-vector 63 0))
8849          start group entry number xrefs header)
8850     (while headers
8851       (setq header (pop headers))
8852       (when (and (setq xrefs (mail-header-xref header))
8853                  (not (memq (setq number (mail-header-number header))
8854                             unreads)))
8855         (setq start 0)
8856         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8857           (setq start (match-end 0))
8858           (setq group (if prefix
8859                           (concat prefix (substring xrefs (match-beginning 1)
8860                                                     (match-end 1)))
8861                         (substring xrefs (match-beginning 1) (match-end 1))))
8862           (setq number
8863                 (string-to-int (substring xrefs (match-beginning 2)
8864                                           (match-end 2))))
8865           (if (setq entry (gnus-gethash group xref-hashtb))
8866               (setcdr entry (cons number (cdr entry)))
8867             (gnus-sethash group (cons number nil) xref-hashtb)))))
8868     (and start xref-hashtb)))
8869
8870 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8871   "Look through all the headers and mark the Xrefs as read."
8872   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8873         name entry info xref-hashtb idlist method nth4)
8874     (save-excursion
8875       (set-buffer gnus-group-buffer)
8876       (when (setq xref-hashtb
8877                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8878         (mapatoms
8879          (lambda (group)
8880            (unless (string= from-newsgroup (setq name (symbol-name group)))
8881              (setq idlist (symbol-value group))
8882              ;; Dead groups are not updated.
8883              (and (prog1
8884                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8885                             info (nth 2 entry))
8886                     (if (stringp (setq nth4 (gnus-info-method info)))
8887                         (setq nth4 (gnus-server-to-method nth4))))
8888                   ;; Only do the xrefs if the group has the same
8889                   ;; select method as the group we have just read.
8890                   (or (gnus-methods-equal-p
8891                        nth4 (gnus-find-method-for-group from-newsgroup))
8892                       virtual
8893                       (equal nth4 (setq method (gnus-find-method-for-group
8894                                                 from-newsgroup)))
8895                       (and (equal (car nth4) (car method))
8896                            (equal (nth 1 nth4) (nth 1 method))))
8897                   gnus-use-cross-reference
8898                   (or (not (eq gnus-use-cross-reference t))
8899                       virtual
8900                       ;; Only do cross-references on subscribed
8901                       ;; groups, if that is what is wanted.
8902                       (<= (gnus-info-level info) gnus-level-subscribed))
8903                   (gnus-group-make-articles-read name idlist))))
8904          xref-hashtb)))))
8905
8906 (defun gnus-group-make-articles-read (group articles)
8907   (let* ((num 0)
8908          (entry (gnus-gethash group gnus-newsrc-hashtb))
8909          (info (nth 2 entry))
8910          (active (gnus-active group))
8911          range)
8912     ;; First peel off all illegal article numbers.
8913     (if active
8914         (let ((ids articles)
8915               id first)
8916           (while ids
8917             (setq id (car ids))
8918             (if (and first (> id (cdr active)))
8919                 (progn
8920                   ;; We'll end up in this situation in one particular
8921                   ;; obscure situation.  If you re-scan a group and get
8922                   ;; a new article that is cross-posted to a different
8923                   ;; group that has not been re-scanned, you might get
8924                   ;; crossposted article that has a higher number than
8925                   ;; Gnus believes possible.  So we re-activate this
8926                   ;; group as well.  This might mean doing the
8927                   ;; crossposting thingy will *increase* the number
8928                   ;; of articles in some groups.  Tsk, tsk.
8929                   (setq active (or (gnus-activate-group group) active))))
8930             (if (or (> id (cdr active))
8931                     (< id (car active)))
8932                 (setq articles (delq id articles)))
8933             (setq ids (cdr ids)))))
8934     ;; If the read list is nil, we init it.
8935     (and active
8936          (null (gnus-info-read info))
8937          (> (car active) 1)
8938          (gnus-info-set-read info (cons 1 (1- (car active)))))
8939     ;; Then we add the read articles to the range.
8940     (gnus-info-set-read
8941      info
8942      (setq range
8943            (gnus-add-to-range
8944             (gnus-info-read info) (setq articles (sort articles '<)))))
8945     ;; Then we have to re-compute how many unread
8946     ;; articles there are in this group.
8947     (if active
8948         (progn
8949           (cond
8950            ((not range)
8951             (setq num (- (1+ (cdr active)) (car active))))
8952            ((not (listp (cdr range)))
8953             (setq num (- (cdr active) (- (1+ (cdr range))
8954                                          (car range)))))
8955            (t
8956             (while range
8957               (if (numberp (car range))
8958                   (setq num (1+ num))
8959                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8960               (setq range (cdr range)))
8961             (setq num (- (cdr active) num))))
8962           ;; Update the number of unread articles.
8963           (setcar entry num)
8964           ;; Update the group buffer.
8965           (gnus-group-update-group group t)))))
8966
8967 (defun gnus-methods-equal-p (m1 m2)
8968   (let ((m1 (or m1 gnus-select-method))
8969         (m2 (or m2 gnus-select-method)))
8970     (or (equal m1 m2)
8971         (and (eq (car m1) (car m2))
8972              (or (not (memq 'address (assoc (symbol-name (car m1))
8973                                             gnus-valid-select-methods)))
8974                  (equal (nth 1 m1) (nth 1 m2)))))))
8975
8976 (defsubst gnus-header-value ()
8977   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8978
8979 (defvar gnus-newsgroup-none-id 0)
8980
8981 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8982   (let ((cur nntp-server-buffer)
8983         (dependencies
8984          (or dependencies
8985              (save-excursion (set-buffer gnus-summary-buffer)
8986                              gnus-newsgroup-dependencies)))
8987         headers id id-dep ref-dep end ref)
8988     (save-excursion
8989       (set-buffer nntp-server-buffer)
8990       (let ((case-fold-search t)
8991             in-reply-to header p lines)
8992         (goto-char (point-min))
8993         ;; Search to the beginning of the next header.  Error messages
8994         ;; do not begin with 2 or 3.
8995         (while (re-search-forward "^[23][0-9]+ " nil t)
8996           (setq id nil
8997                 ref nil)
8998           ;; This implementation of this function, with nine
8999           ;; search-forwards instead of the one re-search-forward and
9000           ;; a case (which basically was the old function) is actually
9001           ;; about twice as fast, even though it looks messier.  You
9002           ;; can't have everything, I guess.  Speed and elegance
9003           ;; doesn't always go hand in hand.
9004           (setq
9005            header
9006            (vector
9007             ;; Number.
9008             (prog1
9009                 (read cur)
9010               (end-of-line)
9011               (setq p (point))
9012               (narrow-to-region (point)
9013                                 (or (and (search-forward "\n.\n" nil t)
9014                                          (- (point) 2))
9015                                     (point))))
9016             ;; Subject.
9017             (progn
9018               (goto-char p)
9019               (if (search-forward "\nsubject: " nil t)
9020                   (gnus-header-value) "(none)"))
9021             ;; From.
9022             (progn
9023               (goto-char p)
9024               (if (search-forward "\nfrom: " nil t)
9025                   (gnus-header-value) "(nobody)"))
9026             ;; Date.
9027             (progn
9028               (goto-char p)
9029               (if (search-forward "\ndate: " nil t)
9030                   (gnus-header-value) ""))
9031             ;; Message-ID.
9032             (progn
9033               (goto-char p)
9034               (if (search-forward "\nmessage-id: " nil t)
9035                   (setq id (gnus-header-value))
9036                 ;; If there was no message-id, we just fake one to make
9037                 ;; subsequent routines simpler.
9038                 (setq id (concat "none+"
9039                                  (int-to-string
9040                                   (setq gnus-newsgroup-none-id
9041                                         (1+ gnus-newsgroup-none-id)))))))
9042             ;; References.
9043             (progn
9044               (goto-char p)
9045               (if (search-forward "\nreferences: " nil t)
9046                   (prog1
9047                       (gnus-header-value)
9048                     (setq end (match-end 0))
9049                     (save-excursion
9050                       (setq ref
9051                             (buffer-substring
9052                              (progn
9053                                (end-of-line)
9054                                (search-backward ">" end t)
9055                                (1+ (point)))
9056                              (progn
9057                                (search-backward "<" end t)
9058                                (point))))))
9059                 ;; Get the references from the in-reply-to header if there
9060                 ;; were no references and the in-reply-to header looks
9061                 ;; promising.
9062                 (if (and (search-forward "\nin-reply-to: " nil t)
9063                          (setq in-reply-to (gnus-header-value))
9064                          (string-match "<[^>]+>" in-reply-to))
9065                     (setq ref (substring in-reply-to (match-beginning 0)
9066                                          (match-end 0)))
9067                   (setq ref ""))))
9068             ;; Chars.
9069             0
9070             ;; Lines.
9071             (progn
9072               (goto-char p)
9073               (if (search-forward "\nlines: " nil t)
9074                   (if (numberp (setq lines (read cur)))
9075                       lines 0)
9076                 0))
9077             ;; Xref.
9078             (progn
9079               (goto-char p)
9080               (and (search-forward "\nxref: " nil t)
9081                    (gnus-header-value)))))
9082           ;; We do the threading while we read the headers.  The
9083           ;; message-id and the last reference are both entered into
9084           ;; the same hash table.  Some tippy-toeing around has to be
9085           ;; done in case an article has arrived before the article
9086           ;; which it refers to.
9087           (if (boundp (setq id-dep (intern id dependencies)))
9088               (if (and (car (symbol-value id-dep))
9089                        (not force-new))
9090                   ;; An article with this Message-ID has already
9091                   ;; been seen, so we ignore this one, except we add
9092                   ;; any additional Xrefs (in case the two articles
9093                   ;; came from different servers).
9094                   (progn
9095                     (mail-header-set-xref
9096                      (car (symbol-value id-dep))
9097                      (concat (or (mail-header-xref
9098                                   (car (symbol-value id-dep))) "")
9099                              (or (mail-header-xref header) "")))
9100                     (setq header nil))
9101                 (setcar (symbol-value id-dep) header))
9102             (set id-dep (list header)))
9103           (when header
9104             (if (boundp (setq ref-dep (intern ref dependencies)))
9105                 (setcdr (symbol-value ref-dep)
9106                         (nconc (cdr (symbol-value ref-dep))
9107                                (list (symbol-value id-dep))))
9108               (set ref-dep (list nil (symbol-value id-dep))))
9109             (setq headers (cons header headers)))
9110           (goto-char (point-max))
9111           (widen))
9112         (nreverse headers)))))
9113
9114 ;; The following macros and functions were written by Felix Lee
9115 ;; <flee@cse.psu.edu>.
9116
9117 (defmacro gnus-nov-read-integer ()
9118   '(prog1
9119        (if (= (following-char) ?\t)
9120            0
9121          (let ((num (condition-case nil (read buffer) (error nil))))
9122            (if (numberp num) num 0)))
9123      (or (eobp) (forward-char 1))))
9124
9125 (defmacro gnus-nov-skip-field ()
9126   '(search-forward "\t" eol 'move))
9127
9128 (defmacro gnus-nov-field ()
9129   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9130
9131 ;; Goes through the xover lines and returns a list of vectors
9132 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9133                                                   force-new dependencies)
9134   "Parse the news overview data in the server buffer, and return a
9135 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9136   ;; Get the Xref when the users reads the articles since most/some
9137   ;; NNTP servers do not include Xrefs when using XOVER.
9138   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9139   (let ((cur nntp-server-buffer)
9140         (dependencies (or dependencies gnus-newsgroup-dependencies))
9141         number headers header)
9142     (save-excursion
9143       (set-buffer nntp-server-buffer)
9144       ;; Allow the user to mangle the headers before parsing them.
9145       (run-hooks 'gnus-parse-headers-hook)
9146       ;; Allow the user to mangle the headers before parsing them.
9147       (run-hooks 'gnus-parse-headers-hook)
9148       (goto-char (point-min))
9149       (while (and sequence (not (eobp)))
9150         (setq number (read cur))
9151         (while (and sequence (< (car sequence) number))
9152           (setq sequence (cdr sequence)))
9153         (and sequence
9154              (eq number (car sequence))
9155              (progn
9156                (setq sequence (cdr sequence))
9157                (if (setq header
9158                          (inline (gnus-nov-parse-line
9159                                   number dependencies force-new)))
9160                    (setq headers (cons header headers)))))
9161         (forward-line 1))
9162       (setq headers (nreverse headers)))
9163     headers))
9164
9165 ;; This function has to be called with point after the article number
9166 ;; on the beginning of the line.
9167 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9168   (let ((none 0)
9169         (eol (gnus-point-at-eol))
9170         (buffer (current-buffer))
9171         header ref id id-dep ref-dep)
9172
9173     ;; overview: [num subject from date id refs chars lines misc]
9174     (narrow-to-region (point) eol)
9175     (or (eobp) (forward-char))
9176
9177     (condition-case nil
9178         (setq header
9179               (vector
9180                number                   ; number
9181                (gnus-nov-field)         ; subject
9182                (gnus-nov-field)         ; from
9183                (gnus-nov-field)         ; date
9184                (setq id (or (gnus-nov-field)
9185                             (concat "none+"
9186                                     (int-to-string
9187                                      (setq none (1+ none)))))) ; id
9188                (progn
9189                  (save-excursion
9190                    (let ((beg (point)))
9191                      (search-forward "\t" eol)
9192                      (if (search-backward ">" beg t)
9193                          (setq ref
9194                                (buffer-substring
9195                                 (1+ (point))
9196                                 (search-backward "<" beg t)))
9197                        (setq ref nil))))
9198                  (gnus-nov-field))      ; refs
9199                (gnus-nov-read-integer)  ; chars
9200                (gnus-nov-read-integer)  ; lines
9201                (if (= (following-char) ?\n)
9202                    nil
9203                  (gnus-nov-field))      ; misc
9204                ))
9205       (error (progn
9206                (ding)
9207                (gnus-message 4 "Strange nov line")
9208                (setq header nil)
9209                (goto-char eol))))
9210
9211     (widen)
9212
9213     ;; We build the thread tree.
9214     (when header
9215       (if (boundp (setq id-dep (intern id dependencies)))
9216           (if (and (car (symbol-value id-dep))
9217                    (not force-new))
9218               ;; An article with this Message-ID has already been seen,
9219               ;; so we ignore this one, except we add any additional
9220               ;; Xrefs (in case the two articles came from different
9221               ;; servers.
9222               (progn
9223                 (mail-header-set-xref
9224                  (car (symbol-value id-dep))
9225                  (concat (or (mail-header-xref
9226                               (car (symbol-value id-dep))) "")
9227                          (or (mail-header-xref header) "")))
9228                 (setq header nil))
9229             (setcar (symbol-value id-dep) header))
9230         (set id-dep (list header))))
9231     (if header
9232         (progn
9233           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9234               (setcdr (symbol-value ref-dep)
9235                       (nconc (cdr (symbol-value ref-dep))
9236                              (list (symbol-value id-dep))))
9237             (set ref-dep (list nil (symbol-value id-dep))))))
9238     header))
9239
9240 (defun gnus-article-get-xrefs ()
9241   "Fill in the Xref value in `gnus-current-headers', if necessary.
9242 This is meant to be called in `gnus-article-internal-prepare-hook'."
9243   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9244                                  gnus-current-headers)))
9245     (or (not gnus-use-cross-reference)
9246         (not headers)
9247         (and (mail-header-xref headers)
9248              (not (string= (mail-header-xref headers) "")))
9249         (let ((case-fold-search t)
9250               xref)
9251           (save-restriction
9252             (nnheader-narrow-to-headers)
9253             (goto-char (point-min))
9254             (if (or (and (eq (downcase (following-char)) ?x)
9255                          (looking-at "Xref:"))
9256                     (search-forward "\nXref:" nil t))
9257                 (progn
9258                   (goto-char (1+ (match-end 0)))
9259                   (setq xref (buffer-substring (point)
9260                                                (progn (end-of-line) (point))))
9261                   (mail-header-set-xref headers xref))))))))
9262
9263 (defun gnus-summary-insert-subject (id &optional old-header)
9264   "Find article ID and insert the summary line for that article."
9265   (let ((header (gnus-read-header id))
9266         (number (and (numberp id) id))
9267         pos)
9268     (when header
9269       ;; Rebuild the thread that this article is part of and go to the
9270       ;; article we have fetched.
9271       (when old-header
9272         (when (setq pos (text-property-any
9273                          (point-min) (point-max) 'gnus-number 
9274                          (mail-header-number old-header)))
9275           (goto-char pos)
9276           (gnus-delete-line)
9277           (gnus-data-remove (mail-header-number old-header))))
9278       (gnus-rebuild-thread (mail-header-id header))
9279       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9280     (when (and (numberp number)
9281                (> number 0))
9282       ;; We have to update the boundaries even if we can't fetch the
9283       ;; article if ID is a number -- so that the next `P' or `N'
9284       ;; command will fetch the previous (or next) article even
9285       ;; if the one we tried to fetch this time has been canceled.
9286       (and (> number gnus-newsgroup-end)
9287            (setq gnus-newsgroup-end number))
9288       (and (< number gnus-newsgroup-begin)
9289            (setq gnus-newsgroup-begin number))
9290       (setq gnus-newsgroup-unselected
9291             (delq number gnus-newsgroup-unselected)))
9292     ;; Report back a success?
9293     (and header (mail-header-number header))))
9294
9295 (defun gnus-summary-work-articles (n)
9296   "Return a list of articles to be worked upon.  The prefix argument,
9297 the list of process marked articles, and the current article will be
9298 taken into consideration."
9299   (cond
9300    ((and n (numberp n))
9301     ;; A numerical prefix has been given.
9302     (let ((backward (< n 0))
9303           (n (abs n))
9304           articles article)
9305       (save-excursion
9306         (while
9307             (and (> n 0)
9308                  (push (setq article (gnus-summary-article-number))
9309                        articles)
9310                  (if backward
9311                      (gnus-summary-find-prev nil article)
9312                    (gnus-summary-find-next nil article)))
9313           (decf n)))
9314       (nreverse articles)))
9315    ((and (boundp 'transient-mark-mode)
9316          transient-mark-mode
9317          mark-active)
9318     ;; Work on the region between point and mark.
9319     (let ((max (max (point) (mark)))
9320           articles article)
9321       (save-excursion
9322         (goto-char (min (point) (mark)))
9323         (while
9324             (and
9325              (push (setq article (gnus-summary-article-number)) articles)
9326              (gnus-summary-find-next nil article)
9327              (< (point) max)))
9328         (nreverse articles))))
9329    (gnus-newsgroup-processable
9330     ;; There are process-marked articles present.
9331     (reverse gnus-newsgroup-processable))
9332    (t
9333     ;; Just return the current article.
9334     (list (gnus-summary-article-number)))))
9335
9336 (defun gnus-summary-search-group (&optional backward use-level)
9337   "Search for next unread newsgroup.
9338 If optional argument BACKWARD is non-nil, search backward instead."
9339   (save-excursion
9340     (set-buffer gnus-group-buffer)
9341     (if (gnus-group-search-forward
9342          backward nil (if use-level (gnus-group-group-level) nil))
9343         (gnus-group-group-name))))
9344
9345 (defun gnus-summary-best-group (&optional exclude-group)
9346   "Find the name of the best unread group.
9347 If EXCLUDE-GROUP, do not go to this group."
9348   (save-excursion
9349     (set-buffer gnus-group-buffer)
9350     (save-excursion
9351       (gnus-group-best-unread-group exclude-group))))
9352
9353 (defun gnus-summary-find-next (&optional unread article backward)
9354   (if backward (gnus-summary-find-prev)
9355     (let* ((article (or article (gnus-summary-article-number)))
9356            (arts (gnus-data-find-list article))
9357            result)
9358       (when (or (not gnus-summary-check-current)
9359                 (not unread)
9360                 (not (gnus-data-unread-p (car arts))))
9361         (setq arts (cdr arts)))
9362       (when (setq result
9363                   (if unread
9364                       (progn
9365                         (while arts
9366                           (when (gnus-data-unread-p (car arts))
9367                             (setq result (car arts)
9368                                   arts nil))
9369                           (setq arts (cdr arts)))
9370                         result)
9371                     (car arts)))
9372         (goto-char (gnus-data-pos result))
9373         (gnus-data-number result)))))
9374
9375 (defun gnus-summary-find-prev (&optional unread article)
9376   (let* ((article (or article (gnus-summary-article-number)))
9377          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9378          result)
9379     (when (or (not gnus-summary-check-current)
9380               (not unread)
9381               (not (gnus-data-unread-p (car arts))))
9382       (setq arts (cdr arts)))
9383     (if (setq result
9384               (if unread
9385                   (progn
9386                     (while arts
9387                       (and (gnus-data-unread-p (car arts))
9388                            (setq result (car arts)
9389                                  arts nil))
9390                       (setq arts (cdr arts)))
9391                     result)
9392                 (car arts)))
9393         (progn
9394           (goto-char (gnus-data-pos result))
9395           (gnus-data-number result)))))
9396
9397 (defun gnus-summary-find-subject (subject &optional unread backward article)
9398   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9399          (article (or article (gnus-summary-article-number)))
9400          (articles (gnus-data-list backward))
9401          (arts (gnus-data-find-list article articles))
9402          result)
9403     (when (or (not gnus-summary-check-current)
9404               (not unread)
9405               (not (gnus-data-unread-p (car arts))))
9406       (setq arts (cdr arts)))
9407     (while arts
9408       (and (or (not unread)
9409                (gnus-data-unread-p (car arts)))
9410            (vectorp (gnus-data-header (car arts)))
9411            (gnus-subject-equal
9412             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9413            (setq result (car arts)
9414                  arts nil))
9415       (setq arts (cdr arts)))
9416     (and result
9417          (goto-char (gnus-data-pos result))
9418          (gnus-data-number result))))
9419
9420 (defun gnus-summary-search-forward (&optional unread subject backward)
9421   "Search forward for an article.
9422 If UNREAD, look for unread articles.  If SUBJECT, look for
9423 articles with that subject.  If BACKWARD, search backward instead."
9424   (cond (subject (gnus-summary-find-subject subject unread backward))
9425         (backward (gnus-summary-find-prev unread))
9426         (t (gnus-summary-find-next unread))))
9427
9428 (defun gnus-recenter (&optional n)
9429   "Center point in window and redisplay frame.
9430 Also do horizontal recentering."
9431   (interactive "P")
9432   (when (and gnus-auto-center-summary
9433              (not (eq gnus-auto-center-summary 'vertical)))
9434     (gnus-horizontal-recenter))
9435   (recenter n))
9436
9437 (defun gnus-summary-recenter ()
9438   "Center point in the summary window.
9439 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9440 displayed, no centering will be performed."
9441   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9442   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9443   (let* ((top (cond ((< (window-height) 4) 0)
9444                     ((< (window-height) 7) 1)
9445                     (t 2)))
9446          (height (1- (window-height)))
9447          (bottom (save-excursion (goto-char (point-max))
9448                                  (forward-line (- height))
9449                                  (point)))
9450          (window (get-buffer-window (current-buffer))))
9451     ;; The user has to want it.
9452     (when gnus-auto-center-summary
9453       (when (get-buffer-window gnus-article-buffer)
9454        ;; Only do recentering when the article buffer is displayed,
9455        ;; Set the window start to either `bottom', which is the biggest
9456        ;; possible valid number, or the second line from the top,
9457        ;; whichever is the least.
9458        (set-window-start
9459         window (min bottom (save-excursion 
9460                              (forward-line (- top)) (point)))))
9461       ;; Do horizontal recentering while we're at it.
9462       (when (and (get-buffer-window (current-buffer) t)
9463                  (not (eq gnus-auto-center-summary 'vertical)))
9464         (let ((selected (selected-window)))
9465           (select-window (get-buffer-window (current-buffer) t))
9466           (gnus-summary-position-point)
9467           (gnus-horizontal-recenter)
9468           (select-window selected))))))
9469
9470 (defun gnus-horizontal-recenter ()
9471   "Recenter the current buffer horizontally."
9472   (if (< (current-column) (/ (window-width) 2))
9473       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9474     (let* ((orig (point))
9475            (end (window-end (get-buffer-window (current-buffer) t)))
9476            (max 0))
9477       ;; Find the longest line currently displayed in the window.
9478       (goto-char (window-start))
9479       (while (and (not (eobp)) 
9480                   (< (point) end))
9481         (end-of-line)
9482         (setq max (max max (current-column)))
9483         (forward-line 1))
9484       (goto-char orig)
9485       ;; Scroll horizontally to center (sort of) the point.
9486       (if (> max (window-width))
9487           (set-window-hscroll 
9488            (get-buffer-window (current-buffer) t)
9489            (min (- (current-column) (/ (window-width) 3))
9490                 (+ 2 (- max (window-width)))))
9491         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9492       max)))
9493
9494 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9495 (defun gnus-short-group-name (group &optional levels)
9496   "Collapse GROUP name LEVELS."
9497   (let* ((name "") 
9498          (foreign "")
9499          (depth 0) 
9500          (skip 1)
9501          (levels (or levels
9502                      (progn
9503                        (while (string-match "\\." group skip)
9504                          (setq skip (match-end 0)
9505                                depth (+ depth 1)))
9506                        depth))))
9507     (if (string-match ":" group)
9508         (setq foreign (substring group 0 (match-end 0))
9509               group (substring group (match-end 0))))
9510     (while group
9511       (if (and (string-match "\\." group)
9512                (> levels (- gnus-group-uncollapsed-levels 1)))
9513           (setq name (concat name (substring group 0 1))
9514                 group (substring group (match-end 0))
9515                 levels (- levels 1)
9516                 name (concat name "."))
9517         (setq name (concat foreign name group)
9518               group nil)))
9519     name))
9520
9521 (defun gnus-summary-jump-to-group (newsgroup)
9522   "Move point to NEWSGROUP in group mode buffer."
9523   ;; Keep update point of group mode buffer if visible.
9524   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9525       (save-window-excursion
9526         ;; Take care of tree window mode.
9527         (if (get-buffer-window gnus-group-buffer)
9528             (pop-to-buffer gnus-group-buffer))
9529         (gnus-group-jump-to-group newsgroup))
9530     (save-excursion
9531       ;; Take care of tree window mode.
9532       (if (get-buffer-window gnus-group-buffer)
9533           (pop-to-buffer gnus-group-buffer)
9534         (set-buffer gnus-group-buffer))
9535       (gnus-group-jump-to-group newsgroup))))
9536
9537 ;; This function returns a list of article numbers based on the
9538 ;; difference between the ranges of read articles in this group and
9539 ;; the range of active articles.
9540 (defun gnus-list-of-unread-articles (group)
9541   (let* ((read (gnus-info-read (gnus-get-info group)))
9542          (active (gnus-active group))
9543          (last (cdr active))
9544          first nlast unread)
9545     ;; If none are read, then all are unread.
9546     (if (not read)
9547         (setq first (car active))
9548       ;; If the range of read articles is a single range, then the
9549       ;; first unread article is the article after the last read
9550       ;; article.  Sounds logical, doesn't it?
9551       (if (not (listp (cdr read)))
9552           (setq first (1+ (cdr read)))
9553         ;; `read' is a list of ranges.
9554         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9555                                 (caar read))) 1)
9556             (setq first 1))
9557         (while read
9558           (if first
9559               (while (< first nlast)
9560                 (setq unread (cons first unread))
9561                 (setq first (1+ first))))
9562           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9563           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9564           (setq read (cdr read)))))
9565     ;; And add the last unread articles.
9566     (while (<= first last)
9567       (setq unread (cons first unread))
9568       (setq first (1+ first)))
9569     ;; Return the list of unread articles.
9570     (nreverse unread)))
9571
9572 (defun gnus-list-of-read-articles (group)
9573   "Return a list of unread, unticked and non-dormant articles."
9574   (let* ((info (gnus-get-info group))
9575          (marked (gnus-info-marks info))
9576          (active (gnus-active group)))
9577     (and info active
9578          (gnus-set-difference
9579           (gnus-sorted-complement
9580            (gnus-uncompress-range active)
9581            (gnus-list-of-unread-articles group))
9582           (append
9583            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9584            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9585
9586 ;; Various summary commands
9587
9588 (defun gnus-summary-universal-argument (arg)
9589   "Perform any operation on all articles that are process/prefixed."
9590   (interactive "P")
9591   (gnus-set-global-variables)
9592   (let ((articles (gnus-summary-work-articles arg))
9593         func article)
9594     (if (eq
9595          (setq
9596           func
9597           (key-binding
9598            (read-key-sequence
9599             (substitute-command-keys
9600              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9601              ))))
9602          'undefined)
9603         (progn
9604           (message "Undefined key")
9605           (ding))
9606       (save-excursion
9607         (while articles
9608           (gnus-summary-goto-subject (setq article (pop articles)))
9609           (command-execute func)
9610           (gnus-summary-remove-process-mark article)))))
9611   (gnus-summary-position-point))
9612
9613 (defun gnus-summary-toggle-truncation (&optional arg)
9614   "Toggle truncation of summary lines.
9615 With arg, turn line truncation on iff arg is positive."
9616   (interactive "P")
9617   (setq truncate-lines
9618         (if (null arg) (not truncate-lines)
9619           (> (prefix-numeric-value arg) 0)))
9620   (redraw-display))
9621
9622 (defun gnus-summary-reselect-current-group (&optional all rescan)
9623   "Exit and then reselect the current newsgroup.
9624 The prefix argument ALL means to select all articles."
9625   (interactive "P")
9626   (gnus-set-global-variables)
9627   (let ((current-subject (gnus-summary-article-number))
9628         (group gnus-newsgroup-name))
9629     (setq gnus-newsgroup-begin nil)
9630     (gnus-summary-exit)
9631     ;; We have to adjust the point of group mode buffer because the
9632     ;; current point was moved to the next unread newsgroup by
9633     ;; exiting.
9634     (gnus-summary-jump-to-group group)
9635     (when rescan
9636       (save-excursion
9637         (gnus-group-get-new-news-this-group 1)))
9638     (gnus-group-read-group all t)
9639     (gnus-summary-goto-subject current-subject)))
9640
9641 (defun gnus-summary-rescan-group (&optional all)
9642   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9643   (interactive "P")
9644   (gnus-summary-reselect-current-group all t))
9645
9646 (defun gnus-summary-update-info ()
9647   (let* ((group gnus-newsgroup-name))
9648     (when gnus-newsgroup-kill-headers
9649       (setq gnus-newsgroup-killed
9650             (gnus-compress-sequence
9651              (nconc
9652               (gnus-set-sorted-intersection
9653                (gnus-uncompress-range gnus-newsgroup-killed)
9654                (setq gnus-newsgroup-unselected
9655                      (sort gnus-newsgroup-unselected '<)))
9656               (setq gnus-newsgroup-unreads
9657                     (sort gnus-newsgroup-unreads '<))) t)))
9658     (unless (listp (cdr gnus-newsgroup-killed))
9659       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9660     (let ((headers gnus-newsgroup-headers))
9661       (run-hooks 'gnus-exit-group-hook)
9662       (unless gnus-save-score
9663         (setq gnus-newsgroup-scored nil))
9664       ;; Set the new ranges of read articles.
9665       (gnus-update-read-articles
9666        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9667       ;; Set the current article marks.
9668       (gnus-update-marks)
9669       ;; Do the cross-ref thing.
9670       (when gnus-use-cross-reference
9671         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9672       ;; Do adaptive scoring, and possibly save score files.
9673       (when gnus-newsgroup-adaptive
9674         (gnus-score-adaptive))
9675       (when gnus-use-scoring
9676         (gnus-score-save))
9677       ;; Do not switch windows but change the buffer to work.
9678       (set-buffer gnus-group-buffer)
9679       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9680           (gnus-group-update-group group)))))
9681
9682 (defun gnus-summary-exit (&optional temporary)
9683   "Exit reading current newsgroup, and then return to group selection mode.
9684 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9685   (interactive)
9686   (gnus-set-global-variables)
9687   (gnus-kill-save-kill-buffer)
9688   (let* ((group gnus-newsgroup-name)
9689          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9690          (mode major-mode)
9691          (buf (current-buffer)))
9692     (unless temporary
9693       (run-hooks 'gnus-summary-prepare-exit-hook))
9694     ;; If we have several article buffers, we kill them at exit.
9695     (unless gnus-single-article-buffer
9696       (gnus-kill-buffer gnus-article-buffer)
9697       (gnus-kill-buffer gnus-original-article-buffer)
9698       (setq gnus-article-current nil))
9699     (when gnus-use-cache
9700       (gnus-cache-possibly-remove-articles)
9701       (gnus-cache-save-buffers))
9702     (when gnus-use-trees
9703       (gnus-tree-close group))
9704     ;; Make all changes in this group permanent.
9705     (unless quit-config
9706       (gnus-summary-update-info))
9707     (gnus-close-group group)
9708     ;; Make sure where I was, and go to next newsgroup.
9709     (set-buffer gnus-group-buffer)
9710     (unless quit-config
9711       (gnus-group-jump-to-group group)
9712       (gnus-group-next-unread-group 1))
9713     (run-hooks 'gnus-summary-exit-hook)
9714     (unless gnus-single-article-buffer
9715       (setq gnus-article-current nil))
9716     (if temporary
9717         nil                             ;Nothing to do.
9718       ;; If we have several article buffers, we kill them at exit.
9719       (unless gnus-single-article-buffer
9720         (gnus-kill-buffer gnus-article-buffer)
9721         (gnus-kill-buffer gnus-original-article-buffer)
9722         (setq gnus-article-current nil))
9723       (set-buffer buf)
9724       (if (not gnus-kill-summary-on-exit)
9725           (gnus-deaden-summary)
9726         ;; We set all buffer-local variables to nil.  It is unclear why
9727         ;; this is needed, but if we don't, buffer-local variables are
9728         ;; not garbage-collected, it seems.  This would the lead to en
9729         ;; ever-growing Emacs.
9730         (gnus-summary-clear-local-variables)
9731         (when (get-buffer gnus-article-buffer)
9732           (bury-buffer gnus-article-buffer))
9733         ;; We clear the global counterparts of the buffer-local
9734         ;; variables as well, just to be on the safe side.
9735         (gnus-configure-windows 'group 'force)
9736         (gnus-summary-clear-local-variables)
9737         ;; Return to group mode buffer.
9738         (if (eq mode 'gnus-summary-mode)
9739             (gnus-kill-buffer buf)))
9740       (setq gnus-current-select-method gnus-select-method)
9741       (pop-to-buffer gnus-group-buffer)
9742       ;; Clear the current group name.
9743       (if (not quit-config)
9744           (progn
9745             (gnus-group-jump-to-group group)
9746             (gnus-group-next-unread-group 1)
9747             (gnus-configure-windows 'group 'force))
9748         (if (not (buffer-name (car quit-config)))
9749             (gnus-configure-windows 'group 'force)
9750           (set-buffer (car quit-config))
9751           (and (eq major-mode 'gnus-summary-mode)
9752                (gnus-set-global-variables))
9753           (gnus-configure-windows (cdr quit-config))))
9754       (unless quit-config
9755         (setq gnus-newsgroup-name nil)))))
9756
9757 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9758 (defun gnus-summary-exit-no-update (&optional no-questions)
9759   "Quit reading current newsgroup without updating read article info."
9760   (interactive)
9761   (gnus-set-global-variables)
9762   (let* ((group gnus-newsgroup-name)
9763          (quit-config (gnus-group-quit-config group)))
9764     (when (or no-questions
9765               gnus-expert-user
9766               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9767       ;; If we have several article buffers, we kill them at exit.
9768       (unless gnus-single-article-buffer
9769         (gnus-kill-buffer gnus-article-buffer)
9770         (gnus-kill-buffer gnus-original-article-buffer)
9771         (setq gnus-article-current nil))
9772       (if (not gnus-kill-summary-on-exit)
9773           (gnus-deaden-summary)
9774         (gnus-close-group group)
9775         (gnus-summary-clear-local-variables)
9776         (set-buffer gnus-group-buffer)
9777         (gnus-summary-clear-local-variables)
9778         (when (get-buffer gnus-summary-buffer)
9779           (kill-buffer gnus-summary-buffer)))
9780       (unless gnus-single-article-buffer
9781         (setq gnus-article-current nil))
9782       (when gnus-use-trees
9783         (gnus-tree-close group))
9784       (when (get-buffer gnus-article-buffer)
9785         (bury-buffer gnus-article-buffer))
9786       ;; Return to the group buffer.
9787       (gnus-configure-windows 'group 'force)
9788       ;; Clear the current group name.
9789       (setq gnus-newsgroup-name nil)
9790       (when (equal (gnus-group-group-name) group)
9791         (gnus-group-next-unread-group 1))
9792       (when quit-config
9793         (if (not (buffer-name (car quit-config)))
9794             (gnus-configure-windows 'group 'force)
9795           (set-buffer (car quit-config))
9796           (when (eq major-mode 'gnus-summary-mode)
9797             (gnus-set-global-variables))
9798           (gnus-configure-windows (cdr quit-config)))))))
9799
9800 ;;; Dead summaries.
9801
9802 (defvar gnus-dead-summary-mode-map nil)
9803
9804 (if gnus-dead-summary-mode-map
9805     nil
9806   (setq gnus-dead-summary-mode-map (make-keymap))
9807   (suppress-keymap gnus-dead-summary-mode-map)
9808   (substitute-key-definition
9809    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9810   (let ((keys '("\C-d" "\r" "\177")))
9811     (while keys
9812       (define-key gnus-dead-summary-mode-map
9813         (pop keys) 'gnus-summary-wake-up-the-dead))))
9814
9815 (defvar gnus-dead-summary-mode nil
9816   "Minor mode for Gnus summary buffers.")
9817
9818 (defun gnus-dead-summary-mode (&optional arg)
9819   "Minor mode for Gnus summary buffers."
9820   (interactive "P")
9821   (when (eq major-mode 'gnus-summary-mode)
9822     (make-local-variable 'gnus-dead-summary-mode)
9823     (setq gnus-dead-summary-mode
9824           (if (null arg) (not gnus-dead-summary-mode)
9825             (> (prefix-numeric-value arg) 0)))
9826     (when gnus-dead-summary-mode
9827       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9828         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9829       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9830         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9831               minor-mode-map-alist)))))
9832
9833 (defun gnus-deaden-summary ()
9834   "Make the current summary buffer into a dead summary buffer."
9835   ;; Kill any previous dead summary buffer.
9836   (when (and gnus-dead-summary
9837              (buffer-name gnus-dead-summary))
9838     (save-excursion
9839       (set-buffer gnus-dead-summary)
9840       (when gnus-dead-summary-mode
9841         (kill-buffer (current-buffer)))))
9842   ;; Make this the current dead summary.
9843   (setq gnus-dead-summary (current-buffer))
9844   (gnus-dead-summary-mode 1)
9845   (let ((name (buffer-name)))
9846     (when (string-match "Summary" name)
9847       (rename-buffer
9848        (concat (substring name 0 (match-beginning 0)) "Dead "
9849                (substring name (match-beginning 0))) t))))
9850
9851 (defun gnus-kill-or-deaden-summary (buffer)
9852   "Kill or deaden the summary BUFFER."
9853   (cond (gnus-kill-summary-on-exit
9854          (when (and gnus-use-trees
9855                     (and (get-buffer buffer)
9856                          (buffer-name (get-buffer buffer))))
9857            (save-excursion
9858              (set-buffer (get-buffer buffer))
9859              (gnus-tree-close gnus-newsgroup-name)))
9860          (gnus-kill-buffer buffer))
9861         ((and (get-buffer buffer)
9862               (buffer-name (get-buffer buffer)))
9863          (save-excursion
9864            (set-buffer buffer)
9865            (gnus-deaden-summary)))))
9866
9867 (defun gnus-summary-wake-up-the-dead (&rest args)
9868   "Wake up the dead summary buffer."
9869   (interactive)
9870   (gnus-dead-summary-mode -1)
9871   (let ((name (buffer-name)))
9872     (when (string-match "Dead " name)
9873       (rename-buffer
9874        (concat (substring name 0 (match-beginning 0))
9875                (substring name (match-end 0))) t)))
9876   (gnus-message 3 "This dead summary is now alive again"))
9877
9878 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9879 (defun gnus-summary-fetch-faq (&optional faq-dir)
9880   "Fetch the FAQ for the current group.
9881 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9882 in."
9883   (interactive
9884    (list
9885     (if current-prefix-arg
9886         (completing-read
9887          "Faq dir: " (and (listp gnus-group-faq-directory)
9888                           gnus-group-faq-directory)))))
9889   (let (gnus-faq-buffer)
9890     (and (setq gnus-faq-buffer
9891                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9892          (gnus-configure-windows 'summary-faq))))
9893
9894 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9895 (defun gnus-summary-describe-group (&optional force)
9896   "Describe the current newsgroup."
9897   (interactive "P")
9898   (gnus-group-describe-group force gnus-newsgroup-name))
9899
9900 (defun gnus-summary-describe-briefly ()
9901   "Describe summary mode commands briefly."
9902   (interactive)
9903   (gnus-message 6
9904                 (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")))
9905
9906 ;; Walking around group mode buffer from summary mode.
9907
9908 (defun gnus-summary-next-group (&optional no-article target-group backward)
9909   "Exit current newsgroup and then select next unread newsgroup.
9910 If prefix argument NO-ARTICLE is non-nil, no article is selected
9911 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9912 previous group instead."
9913   (interactive "P")
9914   (gnus-set-global-variables)
9915   (let ((current-group gnus-newsgroup-name)
9916         (current-buffer (current-buffer))
9917         entered)
9918     ;; First we semi-exit this group to update Xrefs and all variables.
9919     ;; We can't do a real exit, because the window conf must remain
9920     ;; the same in case the user is prompted for info, and we don't
9921     ;; want the window conf to change before that...
9922     (gnus-summary-exit t)
9923     (while (not entered)
9924       ;; Then we find what group we are supposed to enter.
9925       (set-buffer gnus-group-buffer)
9926       (gnus-group-jump-to-group current-group)
9927       (setq target-group
9928             (or target-group
9929                 (if (eq gnus-keep-same-level 'best)
9930                     (gnus-summary-best-group gnus-newsgroup-name)
9931                   (gnus-summary-search-group backward gnus-keep-same-level))))
9932       (if (not target-group)
9933           ;; There are no further groups, so we return to the group
9934           ;; buffer.
9935           (progn
9936             (gnus-message 5 "Returning to the group buffer")
9937             (setq entered t)
9938             (set-buffer current-buffer)
9939             (gnus-summary-exit))
9940         ;; We try to enter the target group.
9941         (gnus-group-jump-to-group target-group)
9942         (let ((unreads (gnus-group-group-unread)))
9943           (if (and (or (eq t unreads)
9944                        (and unreads (not (zerop unreads))))
9945                    (gnus-summary-read-group
9946                     target-group nil no-article current-buffer))
9947               (setq entered t)
9948             (setq current-group target-group
9949                   target-group nil)))))))
9950
9951 (defun gnus-summary-prev-group (&optional no-article)
9952   "Exit current newsgroup and then select previous unread newsgroup.
9953 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9954   (interactive "P")
9955   (gnus-summary-next-group no-article nil t))
9956
9957 ;; Walking around summary lines.
9958
9959 (defun gnus-summary-first-subject (&optional unread)
9960   "Go to the first unread subject.
9961 If UNREAD is non-nil, go to the first unread article.
9962 Returns the article selected or nil if there are no unread articles."
9963   (interactive "P")
9964   (prog1
9965       (cond
9966        ;; Empty summary.
9967        ((null gnus-newsgroup-data)
9968         (gnus-message 3 "No articles in the group")
9969         nil)
9970        ;; Pick the first article.
9971        ((not unread)
9972         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9973         (gnus-data-number (car gnus-newsgroup-data)))
9974        ;; No unread articles.
9975        ((null gnus-newsgroup-unreads)
9976         (gnus-message 3 "No more unread articles")
9977         nil)
9978        ;; Find the first unread article.
9979        (t
9980         (let ((data gnus-newsgroup-data))
9981           (while (and data
9982                       (not (gnus-data-unread-p (car data))))
9983             (setq data (cdr data)))
9984           (if data
9985               (progn
9986                 (goto-char (gnus-data-pos (car data)))
9987                 (gnus-data-number (car data)))))))
9988     (gnus-summary-position-point)))
9989
9990 (defun gnus-summary-next-subject (n &optional unread dont-display)
9991   "Go to next N'th summary line.
9992 If N is negative, go to the previous N'th subject line.
9993 If UNREAD is non-nil, only unread articles are selected.
9994 The difference between N and the actual number of steps taken is
9995 returned."
9996   (interactive "p")
9997   (let ((backward (< n 0))
9998         (n (abs n)))
9999     (while (and (> n 0)
10000                 (if backward
10001                     (gnus-summary-find-prev unread)
10002                   (gnus-summary-find-next unread)))
10003       (setq n (1- n)))
10004     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10005                                (if unread " unread" "")))
10006     (unless dont-display
10007       (gnus-summary-recenter)
10008       (gnus-summary-position-point))
10009     n))
10010
10011 (defun gnus-summary-next-unread-subject (n)
10012   "Go to next N'th unread summary line."
10013   (interactive "p")
10014   (gnus-summary-next-subject n t))
10015
10016 (defun gnus-summary-prev-subject (n &optional unread)
10017   "Go to previous N'th summary line.
10018 If optional argument UNREAD is non-nil, only unread article is selected."
10019   (interactive "p")
10020   (gnus-summary-next-subject (- n) unread))
10021
10022 (defun gnus-summary-prev-unread-subject (n)
10023   "Go to previous N'th unread summary line."
10024   (interactive "p")
10025   (gnus-summary-next-subject (- n) t))
10026
10027 (defun gnus-summary-goto-subject (article &optional force silent)
10028   "Go the subject line of ARTICLE.
10029 If FORCE, also allow jumping to articles not currently shown."
10030   (let ((b (point))
10031         (data (gnus-data-find article)))
10032     ;; We read in the article if we have to.
10033     (and (not data)
10034          force
10035          (gnus-summary-insert-subject article)
10036          (setq data (gnus-data-find article)))
10037     (goto-char b)
10038     (if (not data)
10039         (progn
10040           (unless silent
10041             (gnus-message 3 "Can't find article %d" article))
10042           nil)
10043       (goto-char (gnus-data-pos data))
10044       article)))
10045
10046 ;; Walking around summary lines with displaying articles.
10047
10048 (defun gnus-summary-expand-window (&optional arg)
10049   "Make the summary buffer take up the entire Emacs frame.
10050 Given a prefix, will force an `article' buffer configuration."
10051   (interactive "P")
10052   (gnus-set-global-variables)
10053   (if arg
10054       (gnus-configure-windows 'article 'force)
10055     (gnus-configure-windows 'summary 'force)))
10056
10057 (defun gnus-summary-display-article (article &optional all-header)
10058   "Display ARTICLE in article buffer."
10059   (gnus-set-global-variables)
10060   (if (null article)
10061       nil
10062     (prog1
10063         (if gnus-summary-display-article-function
10064             (funcall gnus-summary-display-article-function article all-header)
10065           (gnus-article-prepare article all-header))
10066       (run-hooks 'gnus-select-article-hook)
10067       (gnus-summary-recenter)
10068       (gnus-summary-goto-subject article)
10069       (when gnus-use-trees
10070         (gnus-possibly-generate-tree article)
10071         (gnus-highlight-selected-tree article))
10072       ;; Successfully display article.
10073       (gnus-article-set-window-start
10074        (cdr (assq article gnus-newsgroup-bookmarks)))
10075       t)))
10076
10077 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10078   "Select the current article.
10079 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10080 non-nil, the article will be re-fetched even if it already present in
10081 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10082 be displayed."
10083   (let ((article (or article (gnus-summary-article-number)))
10084         (all-headers (not (not all-headers))) ;Must be T or NIL.
10085         gnus-summary-display-article-function
10086         did)
10087     (and (not pseudo)
10088          (gnus-summary-article-pseudo-p article)
10089          (error "This is a pseudo-article."))
10090     (prog1
10091         (save-excursion
10092           (set-buffer gnus-summary-buffer)
10093           (if (or (and gnus-single-article-buffer
10094                        (or (null gnus-current-article)
10095                            (null gnus-article-current)
10096                            (null (get-buffer gnus-article-buffer))
10097                            (not (eq article (cdr gnus-article-current)))
10098                            (not (equal (car gnus-article-current)
10099                                        gnus-newsgroup-name))))
10100                   (and (not gnus-single-article-buffer)
10101                        (or (null gnus-current-article)
10102                            (not (eq gnus-current-article article))))
10103                   force)
10104               ;; The requested article is different from the current article.
10105               (prog1
10106                   (gnus-summary-display-article article all-headers)
10107                 (setq did article))
10108             (if (or all-headers gnus-show-all-headers)
10109                 (gnus-article-show-all-headers))
10110             'old))
10111       (if did
10112           (gnus-article-set-window-start
10113            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10114
10115 (defun gnus-summary-set-current-mark (&optional current-mark)
10116   "Obsolete function."
10117   nil)
10118
10119 (defun gnus-summary-next-article (&optional unread subject backward push)
10120   "Select the next article.
10121 If UNREAD, only unread articles are selected.
10122 If SUBJECT, only articles with SUBJECT are selected.
10123 If BACKWARD, the previous article is selected instead of the next."
10124   (interactive "P")
10125   (gnus-set-global-variables)
10126   (cond
10127    ;; Is there such an article?
10128    ((and (gnus-summary-search-forward unread subject backward)
10129          (or (gnus-summary-display-article (gnus-summary-article-number))
10130              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10131     (gnus-summary-position-point))
10132    ;; If not, we try the first unread, if that is wanted.
10133    ((and subject
10134          gnus-auto-select-same
10135          (or (gnus-summary-first-unread-article)
10136              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10137     (gnus-summary-position-point)
10138     (gnus-message 6 "Wrapped"))
10139    ;; Try to get next/previous article not displayed in this group.
10140    ((and gnus-auto-extend-newsgroup
10141          (not unread) (not subject))
10142     (gnus-summary-goto-article
10143      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10144      nil t))
10145    ;; Go to next/previous group.
10146    (t
10147     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10148         (gnus-summary-jump-to-group gnus-newsgroup-name))
10149     (let ((cmd last-command-char)
10150           (group
10151            (if (eq gnus-keep-same-level 'best)
10152                (gnus-summary-best-group gnus-newsgroup-name)
10153              (gnus-summary-search-group backward gnus-keep-same-level))))
10154       ;; For some reason, the group window gets selected.  We change
10155       ;; it back.
10156       (select-window (get-buffer-window (current-buffer)))
10157       ;; Select next unread newsgroup automagically.
10158       (cond
10159        ((not gnus-auto-select-next)
10160         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10161        ((or (eq gnus-auto-select-next 'quietly)
10162             (and (eq gnus-auto-select-next 'slightly-quietly)
10163                  push)
10164             (and (eq gnus-auto-select-next 'almost-quietly)
10165                  (gnus-summary-last-article-p)))
10166         ;; Select quietly.
10167         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10168             (gnus-summary-exit)
10169           (gnus-message 7 "No more%s articles (%s)..."
10170                         (if unread " unread" "")
10171                         (if group (concat "selecting " group)
10172                           "exiting"))
10173           (gnus-summary-next-group nil group backward)))
10174        (t
10175         (gnus-summary-walk-group-buffer
10176          gnus-newsgroup-name cmd unread backward)))))))
10177
10178 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10179   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10180                       (?\C-p (gnus-group-prev-unread-group 1))))
10181         keve key group ended)
10182     (save-excursion
10183       (set-buffer gnus-group-buffer)
10184       (gnus-summary-jump-to-group from-group)
10185       (setq group
10186             (if (eq gnus-keep-same-level 'best)
10187                 (gnus-summary-best-group gnus-newsgroup-name)
10188               (gnus-summary-search-group backward gnus-keep-same-level))))
10189     (while (not ended)
10190       (gnus-message
10191        5 "No more%s articles%s" (if unread " unread" "")
10192        (if (and group
10193                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10194            (format " (Type %s for %s [%s])"
10195                    (single-key-description cmd) group
10196                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10197          (format " (Type %s to exit %s)"
10198                  (single-key-description cmd)
10199                  gnus-newsgroup-name)))
10200       ;; Confirm auto selection.
10201       (setq key (car (setq keve (gnus-read-event-char))))
10202       (setq ended t)
10203       (cond
10204        ((assq key keystrokes)
10205         (let ((obuf (current-buffer)))
10206           (switch-to-buffer gnus-group-buffer)
10207           (and group
10208                (gnus-group-jump-to-group group))
10209           (eval (cadr (assq key keystrokes)))
10210           (setq group (gnus-group-group-name))
10211           (switch-to-buffer obuf))
10212         (setq ended nil))
10213        ((equal key cmd)
10214         (if (or (not group)
10215                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10216             (gnus-summary-exit)
10217           (gnus-summary-next-group nil group backward)))
10218        (t
10219         (push (cdr keve) unread-command-events))))))
10220
10221 (defun gnus-read-event-char ()
10222   "Get the next event."
10223   (let ((event (read-event)))
10224     (cons (and (numberp event) event) event)))
10225
10226 (defun gnus-summary-next-unread-article ()
10227   "Select unread article after current one."
10228   (interactive)
10229   (gnus-summary-next-article t (and gnus-auto-select-same
10230                                     (gnus-summary-article-subject))))
10231
10232 (defun gnus-summary-prev-article (&optional unread subject)
10233   "Select the article after the current one.
10234 If UNREAD is non-nil, only unread articles are selected."
10235   (interactive "P")
10236   (gnus-summary-next-article unread subject t))
10237
10238 (defun gnus-summary-prev-unread-article ()
10239   "Select unred article before current one."
10240   (interactive)
10241   (gnus-summary-prev-article t (and gnus-auto-select-same
10242                                     (gnus-summary-article-subject))))
10243
10244 (defun gnus-summary-next-page (&optional lines circular)
10245   "Show next page of the selected article.
10246 If at the end of the current article, select the next article.
10247 LINES says how many lines should be scrolled up.
10248
10249 If CIRCULAR is non-nil, go to the start of the article instead of
10250 selecting the next article when reaching the end of the current
10251 article."
10252   (interactive "P")
10253   (setq gnus-summary-buffer (current-buffer))
10254   (gnus-set-global-variables)
10255   (let ((article (gnus-summary-article-number))
10256         (endp nil))
10257     (gnus-configure-windows 'article)
10258     (if (or (null gnus-current-article)
10259             (null gnus-article-current)
10260             (/= article (cdr gnus-article-current))
10261             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10262         ;; Selected subject is different from current article's.
10263         (gnus-summary-display-article article)
10264       (gnus-eval-in-buffer-window
10265        gnus-article-buffer
10266        (setq endp (gnus-article-next-page lines)))
10267       (if endp
10268           (cond (circular
10269                  (gnus-summary-beginning-of-article))
10270                 (lines
10271                  (gnus-message 3 "End of message"))
10272                 ((null lines)
10273                  (if (and (eq gnus-summary-goto-unread 'never)
10274                           (not (gnus-summary-last-article-p article)))
10275                      (gnus-summary-next-article)
10276                    (gnus-summary-next-unread-article))))))
10277     (gnus-summary-recenter)
10278     (gnus-summary-position-point)))
10279
10280 (defun gnus-summary-prev-page (&optional lines)
10281   "Show previous page of selected article.
10282 Argument LINES specifies lines to be scrolled down."
10283   (interactive "P")
10284   (gnus-set-global-variables)
10285   (let ((article (gnus-summary-article-number)))
10286     (gnus-configure-windows 'article)
10287     (if (or (null gnus-current-article)
10288             (null gnus-article-current)
10289             (/= article (cdr gnus-article-current))
10290             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10291         ;; Selected subject is different from current article's.
10292         (gnus-summary-display-article article)
10293       (gnus-summary-recenter)
10294       (gnus-eval-in-buffer-window gnus-article-buffer
10295                                   (gnus-article-prev-page lines))))
10296   (gnus-summary-position-point))
10297
10298 (defun gnus-summary-scroll-up (lines)
10299   "Scroll up (or down) one line current article.
10300 Argument LINES specifies lines to be scrolled up (or down if negative)."
10301   (interactive "p")
10302   (gnus-set-global-variables)
10303   (gnus-configure-windows 'article)
10304   (gnus-summary-show-thread)
10305   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10306     (gnus-eval-in-buffer-window
10307      gnus-article-buffer
10308      (cond ((> lines 0)
10309             (if (gnus-article-next-page lines)
10310                 (gnus-message 3 "End of message")))
10311            ((< lines 0)
10312             (gnus-article-prev-page (- lines))))))
10313   (gnus-summary-recenter)
10314   (gnus-summary-position-point))
10315
10316 (defun gnus-summary-next-same-subject ()
10317   "Select next article which has the same subject as current one."
10318   (interactive)
10319   (gnus-set-global-variables)
10320   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10321
10322 (defun gnus-summary-prev-same-subject ()
10323   "Select previous article which has the same subject as current one."
10324   (interactive)
10325   (gnus-set-global-variables)
10326   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10327
10328 (defun gnus-summary-next-unread-same-subject ()
10329   "Select next unread article which has the same subject as current one."
10330   (interactive)
10331   (gnus-set-global-variables)
10332   (gnus-summary-next-article t (gnus-summary-article-subject)))
10333
10334 (defun gnus-summary-prev-unread-same-subject ()
10335   "Select previous unread article which has the same subject as current one."
10336   (interactive)
10337   (gnus-set-global-variables)
10338   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10339
10340 (defun gnus-summary-first-unread-article ()
10341   "Select the first unread article.
10342 Return nil if there are no unread articles."
10343   (interactive)
10344   (gnus-set-global-variables)
10345   (prog1
10346       (if (gnus-summary-first-subject t)
10347           (progn
10348             (gnus-summary-show-thread)
10349             (gnus-summary-first-subject t)
10350             (gnus-summary-display-article (gnus-summary-article-number))))
10351     (gnus-summary-position-point)))
10352
10353 (defun gnus-summary-best-unread-article ()
10354   "Select the unread article with the highest score."
10355   (interactive)
10356   (gnus-set-global-variables)
10357   (let ((best -1000000)
10358         (data gnus-newsgroup-data)
10359         article score)
10360     (while data
10361       (and (gnus-data-unread-p (car data))
10362            (> (setq score
10363                     (gnus-summary-article-score (gnus-data-number (car data))))
10364               best)
10365            (setq best score
10366                  article (gnus-data-number (car data))))
10367       (setq data (cdr data)))
10368     (prog1
10369         (if article
10370             (gnus-summary-goto-article article)
10371           (error "No unread articles"))
10372       (gnus-summary-position-point))))
10373
10374 (defun gnus-summary-last-subject ()
10375   "Go to the last displayed subject line in the group."
10376   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10377     (when article
10378       (gnus-summary-goto-subject article))))
10379
10380 (defun gnus-summary-goto-article (article &optional all-headers force)
10381   "Fetch ARTICLE and display it if it exists.
10382 If ALL-HEADERS is non-nil, no header lines are hidden."
10383   (interactive
10384    (list
10385     (string-to-int
10386      (completing-read
10387       "Article number: "
10388       (mapcar (lambda (number) (list (int-to-string number)))
10389               gnus-newsgroup-limit)))
10390     current-prefix-arg
10391     t))
10392   (prog1
10393       (if (gnus-summary-goto-subject article force)
10394           (gnus-summary-display-article article all-headers)
10395         (gnus-message 4 "Couldn't go to article %s" article) nil)
10396     (gnus-summary-position-point)))
10397
10398 (defun gnus-summary-goto-last-article ()
10399   "Go to the previously read article."
10400   (interactive)
10401   (prog1
10402       (and gnus-last-article
10403            (gnus-summary-goto-article gnus-last-article))
10404     (gnus-summary-position-point)))
10405
10406 (defun gnus-summary-pop-article (number)
10407   "Pop one article off the history and go to the previous.
10408 NUMBER articles will be popped off."
10409   (interactive "p")
10410   (let (to)
10411     (setq gnus-newsgroup-history
10412           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10413     (if to
10414         (gnus-summary-goto-article (car to))
10415       (error "Article history empty")))
10416   (gnus-summary-position-point))
10417
10418 ;; Summary commands and functions for limiting the summary buffer.
10419
10420 (defun gnus-summary-limit-to-articles (n)
10421   "Limit the summary buffer to the next N articles.
10422 If not given a prefix, use the process marked articles instead."
10423   (interactive "P")
10424   (gnus-set-global-variables)
10425   (prog1
10426       (let ((articles (gnus-summary-work-articles n)))
10427         (setq gnus-newsgroup-processable nil)
10428         (gnus-summary-limit articles))
10429     (gnus-summary-position-point)))
10430
10431 (defun gnus-summary-pop-limit (&optional total)
10432   "Restore the previous limit.
10433 If given a prefix, remove all limits."
10434   (interactive "P")
10435   (gnus-set-global-variables)
10436   (when total 
10437     (setq gnus-newsgroup-limits
10438           (list (mapcar (lambda (h) (mail-header-number h))
10439                         gnus-newsgroup-headers))))
10440   (unless gnus-newsgroup-limits
10441     (error "No limit to pop"))
10442   (prog1
10443       (gnus-summary-limit nil 'pop)
10444     (gnus-summary-position-point)))
10445
10446 (defun gnus-summary-limit-to-subject (subject &optional header)
10447   "Limit the summary buffer to articles that have subjects that match a regexp."
10448   (interactive "sRegexp: ")
10449   (unless header
10450     (setq header "subject"))
10451   (when (not (equal "" subject))
10452     (prog1
10453         (let ((articles (gnus-summary-find-matching
10454                          (or header "subject") subject 'all)))
10455           (or articles (error "Found no matches for \"%s\"" subject))
10456           (gnus-summary-limit articles))
10457       (gnus-summary-position-point))))
10458
10459 (defun gnus-summary-limit-to-author (from)
10460   "Limit the summary buffer to articles that have authors that match a regexp."
10461   (interactive "sRegexp: ")
10462   (gnus-summary-limit-to-subject from "from"))
10463
10464 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10465 (make-obsolete
10466  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10467
10468 (defun gnus-summary-limit-to-unread (&optional all)
10469   "Limit the summary buffer to articles that are not marked as read.
10470 If ALL is non-nil, limit strictly to unread articles."
10471   (interactive "P")
10472   (if all
10473       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10474     (gnus-summary-limit-to-marks
10475      ;; Concat all the marks that say that an article is read and have
10476      ;; those removed.
10477      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10478            gnus-killed-mark gnus-kill-file-mark
10479            gnus-low-score-mark gnus-expirable-mark
10480            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10481      'reverse)))
10482
10483 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10484 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10485
10486 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10487   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10488 If REVERSE, limit the summary buffer to articles that are not marked
10489 with MARKS.  MARKS can either be a string of marks or a list of marks.
10490 Returns how many articles were removed."
10491   (interactive "sMarks: ")
10492   (gnus-set-global-variables)
10493   (prog1
10494       (let ((data gnus-newsgroup-data)
10495             (marks (if (listp marks) marks
10496                      (append marks nil))) ; Transform to list.
10497             articles)
10498         (while data
10499           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10500                  (memq (gnus-data-mark (car data)) marks))
10501                (setq articles (cons (gnus-data-number (car data)) articles)))
10502           (setq data (cdr data)))
10503         (gnus-summary-limit articles))
10504     (gnus-summary-position-point)))
10505
10506 (defun gnus-summary-limit-to-score (&optional score)
10507   "Limit to articles with score at or above SCORE."
10508   (interactive "P")
10509   (gnus-set-global-variables)
10510   (setq score (if score
10511                   (prefix-numeric-value score)
10512                 (or gnus-summary-default-score 0)))
10513   (let ((data gnus-newsgroup-data)
10514         articles)
10515     (while data
10516       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10517                 score)
10518         (push (gnus-data-number (car data)) articles))
10519       (setq data (cdr data)))
10520     (prog1
10521         (gnus-summary-limit articles)
10522       (gnus-summary-position-point))))
10523
10524 (defun gnus-summary-limit-include-dormant ()
10525   "Display all the hidden articles that are marked as dormant."
10526   (interactive)
10527   (gnus-set-global-variables)
10528   (or gnus-newsgroup-dormant
10529       (error "There are no dormant articles in this group"))
10530   (prog1
10531       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10532     (gnus-summary-position-point)))
10533
10534 (defun gnus-summary-limit-exclude-dormant ()
10535   "Hide all dormant articles."
10536   (interactive)
10537   (gnus-set-global-variables)
10538   (prog1
10539       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10540     (gnus-summary-position-point)))
10541
10542 (defun gnus-summary-limit-exclude-childless-dormant ()
10543   "Hide all dormant articles that have no children."
10544   (interactive)
10545   (gnus-set-global-variables)
10546   (let ((data (gnus-data-list t))
10547         articles d children)
10548     ;; Find all articles that are either not dormant or have
10549     ;; children.
10550     (while (setq d (pop data))
10551       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10552                 (and (setq children 
10553                            (gnus-article-children (gnus-data-number d)))
10554                      (let (found)
10555                        (while children
10556                          (when (memq (car children) articles)
10557                            (setq children nil
10558                                  found t))
10559                          (pop children))
10560                        found)))
10561         (push (gnus-data-number d) articles)))
10562     ;; Do the limiting.
10563     (prog1
10564         (gnus-summary-limit articles)
10565       (gnus-summary-position-point))))
10566
10567 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10568   "Mark all unread excluded articles as read.
10569 If ALL, mark even excluded ticked and dormants as read."
10570   (interactive "P")
10571   (let ((articles (gnus-sorted-complement
10572                    (sort
10573                     (mapcar (lambda (h) (mail-header-number h))
10574                             gnus-newsgroup-headers)
10575                     '<)
10576                    (sort gnus-newsgroup-limit '<)))
10577         article)
10578     (setq gnus-newsgroup-unreads nil)
10579     (if all
10580         (setq gnus-newsgroup-dormant nil
10581               gnus-newsgroup-marked nil
10582               gnus-newsgroup-reads
10583               (nconc
10584                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10585                gnus-newsgroup-reads))
10586       (while (setq article (pop articles))
10587         (unless (or (memq article gnus-newsgroup-dormant)
10588                     (memq article gnus-newsgroup-marked))
10589           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10590
10591 (defun gnus-summary-limit (articles &optional pop)
10592   (if pop
10593       ;; We pop the previous limit off the stack and use that.
10594       (setq articles (car gnus-newsgroup-limits)
10595             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10596     ;; We use the new limit, so we push the old limit on the stack.
10597     (setq gnus-newsgroup-limits
10598           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10599   ;; Set the limit.
10600   (setq gnus-newsgroup-limit articles)
10601   (let ((total (length gnus-newsgroup-data))
10602         (data (gnus-data-find-list (gnus-summary-article-number)))
10603         found)
10604     ;; This will do all the work of generating the new summary buffer
10605     ;; according to the new limit.
10606     (gnus-summary-prepare)
10607     ;; Hide any threads, possibly.
10608     (and gnus-show-threads
10609          gnus-thread-hide-subtree
10610          (gnus-summary-hide-all-threads))
10611     ;; Try to return to the article you were at, or one in the
10612     ;; neighborhood.
10613     (if data
10614         ;; We try to find some article after the current one.
10615         (while data
10616           (and (gnus-summary-goto-subject
10617                 (gnus-data-number (car data)) nil t)
10618                (setq data nil
10619                      found t))
10620           (setq data (cdr data))))
10621     (or found
10622         ;; If there is no data, that means that we were after the last
10623         ;; article.  The same goes when we can't find any articles
10624         ;; after the current one.
10625         (progn
10626           (goto-char (point-max))
10627           (gnus-summary-find-prev)))
10628     ;; We return how many articles were removed from the summary
10629     ;; buffer as a result of the new limit.
10630     (- total (length gnus-newsgroup-data))))
10631
10632 (defsubst gnus-cut-thread (thread)
10633   "Go forwards in the thread until we find an article that we want to display."
10634   (when (eq gnus-fetch-old-headers 'some)
10635     ;; Deal with old-fetched headers.
10636     (while (and thread
10637                 (memq (mail-header-number (car thread)) 
10638                       gnus-newsgroup-ancient)
10639                 (<= (length (cdr thread)) 1))
10640       (setq thread (cadr thread))))
10641   ;; Deal with sparse threads.
10642   (when (or (eq gnus-build-sparse-threads 'some)
10643             (eq gnus-build-sparse-threads 'more))
10644     (while (and thread
10645                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10646                 (= (length (cdr thread)) 1))
10647       (setq thread (cadr thread))))
10648   thread)
10649
10650 (defun gnus-cut-threads (threads)
10651   "Cut off all uninteresting articles from the beginning of threads."
10652   (when (or (eq gnus-fetch-old-headers 'some)
10653             (eq gnus-build-sparse-threads 'some)
10654             (eq gnus-build-sparse-threads 'more))
10655     (let ((th threads))
10656       (while th
10657         (setcar th (gnus-cut-thread (car th)))
10658         (setq th (cdr th)))))
10659   ;; Remove nixed out threads.
10660   (delq nil threads))
10661
10662 (defun gnus-summary-initial-limit (&optional show-if-empty)
10663   "Figure out what the initial limit is supposed to be on group entry.
10664 This entails weeding out unwanted dormants, low-scored articles,
10665 fetch-old-headers verbiage, and so on."
10666   ;; Most groups have nothing to remove.
10667   (if (or gnus-inhibit-limiting
10668           (and (null gnus-newsgroup-dormant)
10669                (not (eq gnus-fetch-old-headers 'some))
10670                (null gnus-summary-expunge-below)
10671                (not (eq gnus-build-sparse-threads 'some))
10672                (not (eq gnus-build-sparse-threads 'more))
10673                (null gnus-thread-expunge-below)
10674                (not gnus-use-nocem)))
10675       () ; Do nothing.
10676     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10677     (setq gnus-newsgroup-limit nil)
10678     (mapatoms
10679      (lambda (node)
10680        (unless (car (symbol-value node))
10681          ;; These threads have no parents -- they are roots.
10682          (let ((nodes (cdr (symbol-value node)))
10683                thread)
10684            (while nodes
10685              (if (and gnus-thread-expunge-below
10686                       (< (gnus-thread-total-score (car nodes))
10687                          gnus-thread-expunge-below))
10688                  (gnus-expunge-thread (pop nodes))
10689                (setq thread (pop nodes))
10690                (gnus-summary-limit-children thread))))))
10691      gnus-newsgroup-dependencies)
10692     ;; If this limitation resulted in an empty group, we might
10693     ;; pop the previous limit and use it instead.
10694     (when (and (not gnus-newsgroup-limit)
10695                show-if-empty)
10696       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10697     gnus-newsgroup-limit))
10698
10699 (defun gnus-summary-limit-children (thread)
10700   "Return 1 if this subthread is visible and 0 if it is not."
10701   ;; First we get the number of visible children to this thread.  This
10702   ;; is done by recursing down the thread using this function, so this
10703   ;; will really go down to a leaf article first, before slowly
10704   ;; working its way up towards the root.
10705   (when thread
10706     (let ((children
10707            (if (cdr thread)
10708                (apply '+ (mapcar 'gnus-summary-limit-children
10709                                  (cdr thread)))
10710              0))
10711           (number (mail-header-number (car thread)))
10712           score)
10713       (if (or
10714            ;; If this article is dormant and has absolutely no visible
10715            ;; children, then this article isn't visible.
10716            (and (memq number gnus-newsgroup-dormant)
10717                 (= children 0))
10718            ;; If this is a "fetch-old-headered" and there is only one
10719            ;; visible child (or less), then we don't want this article.
10720            (and (eq gnus-fetch-old-headers 'some)
10721                 (memq number gnus-newsgroup-ancient)
10722                 (zerop children))
10723            ;; If this is a sparsely inserted article with no children,
10724            ;; we don't want it.
10725            (and (eq gnus-build-sparse-threads 'some)
10726                 (memq number gnus-newsgroup-sparse)
10727                 (zerop children))
10728            ;; If we use expunging, and this article is really
10729            ;; low-scored, then we don't want this article.
10730            (when (and gnus-summary-expunge-below
10731                       (< (setq score
10732                                (or (cdr (assq number gnus-newsgroup-scored))
10733                                    gnus-summary-default-score))
10734                          gnus-summary-expunge-below))
10735              ;; We increase the expunge-tally here, but that has
10736              ;; nothing to do with the limits, really.
10737              (incf gnus-newsgroup-expunged-tally)
10738              ;; We also mark as read here, if that's wanted.
10739              (when (and gnus-summary-mark-below
10740                         (< score gnus-summary-mark-below))
10741                (setq gnus-newsgroup-unreads
10742                      (delq number gnus-newsgroup-unreads))
10743                (if gnus-newsgroup-auto-expire
10744                    (push number gnus-newsgroup-expirable)
10745                  (push (cons number gnus-low-score-mark)
10746                        gnus-newsgroup-reads)))
10747              t)
10748            (and gnus-use-nocem
10749                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10750           ;; Nope, invisible article.
10751           0
10752         ;; Ok, this article is to be visible, so we add it to the limit
10753         ;; and return 1.
10754         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10755         1))))
10756
10757 (defun gnus-expunge-thread (thread)
10758   "Mark all articles in THREAD as read."
10759   (let* ((number (mail-header-number (car thread))))
10760     (incf gnus-newsgroup-expunged-tally)
10761     ;; We also mark as read here, if that's wanted.
10762     (setq gnus-newsgroup-unreads
10763           (delq number gnus-newsgroup-unreads))
10764     (if gnus-newsgroup-auto-expire
10765         (push number gnus-newsgroup-expirable)
10766       (push (cons number gnus-low-score-mark)
10767             gnus-newsgroup-reads)))
10768   ;; Go recursively through all subthreads.
10769   (mapcar 'gnus-expunge-thread (cdr thread)))
10770
10771 ;; Summary article oriented commands
10772
10773 (defun gnus-summary-refer-parent-article (n)
10774   "Refer parent article N times.
10775 The difference between N and the number of articles fetched is returned."
10776   (interactive "p")
10777   (gnus-set-global-variables)
10778   (while
10779       (and
10780        (> n 0)
10781        (let* ((header (gnus-summary-article-header))
10782               (ref
10783                ;; If we try to find the parent of the currently
10784                ;; displayed article, then we take a look at the actual
10785                ;; References header, since this is slightly more
10786                ;; reliable than the References field we got from the
10787                ;; server.
10788                (if (and (eq (mail-header-number header)
10789                             (cdr gnus-article-current))
10790                         (equal gnus-newsgroup-name
10791                                (car gnus-article-current)))
10792                    (save-excursion
10793                      (set-buffer gnus-original-article-buffer)
10794                      (nnheader-narrow-to-headers)
10795                      (prog1
10796                          (mail-fetch-field "references")
10797                        (widen)))
10798                  ;; It's not the current article, so we take a bet on
10799                  ;; the value we got from the server.
10800                  (mail-header-references header))))
10801          (if (setq ref (or ref (mail-header-references header)))
10802              (or (gnus-summary-refer-article (gnus-parent-id ref))
10803                  (gnus-message 1 "Couldn't find parent"))
10804            (gnus-message 1 "No references in article %d"
10805                          (gnus-summary-article-number))
10806            nil)))
10807     (setq n (1- n)))
10808   (gnus-summary-position-point)
10809   n)
10810
10811 (defun gnus-summary-refer-references ()
10812   "Fetch all articles mentioned in the References header.
10813 Return how many articles were fetched."
10814   (interactive)
10815   (gnus-set-global-variables)
10816   (let ((ref (mail-header-references (gnus-summary-article-header)))
10817         (current (gnus-summary-article-number))
10818         (n 0))
10819     ;; For each Message-ID in the References header...
10820     (while (string-match "<[^>]*>" ref)
10821       (incf n)
10822       ;; ... fetch that article.
10823       (gnus-summary-refer-article
10824        (prog1 (match-string 0 ref)
10825          (setq ref (substring ref (match-end 0))))))
10826     (gnus-summary-goto-subject current)
10827     (gnus-summary-position-point)
10828     n))
10829
10830 (defun gnus-summary-refer-article (message-id)
10831   "Fetch an article specified by MESSAGE-ID."
10832   (interactive "sMessage-ID: ")
10833   (when (and (stringp message-id)
10834              (not (zerop (length message-id))))
10835     ;; Construct the correct Message-ID if necessary.
10836     ;; Suggested by tale@pawl.rpi.edu.
10837     (unless (string-match "^<" message-id)
10838       (setq message-id (concat "<" message-id)))
10839     (unless (string-match ">$" message-id)
10840       (setq message-id (concat message-id ">")))
10841     (let ((header (car (gnus-gethash message-id
10842                                      gnus-newsgroup-dependencies))))
10843       (if header
10844           ;; The article is present in the buffer, to we just go to it.
10845           (gnus-summary-goto-article (mail-header-number header) nil t)
10846         ;; We fetch the article
10847         (let ((gnus-override-method 
10848                (and (gnus-news-group-p gnus-newsgroup-name)
10849                     gnus-refer-article-method))
10850               number)
10851           ;; Start the special refer-article method, if necessary.
10852           (when gnus-refer-article-method
10853             (gnus-check-server gnus-refer-article-method))
10854           ;; Fetch the header, and display the article.
10855           (if (setq number (gnus-summary-insert-subject message-id))
10856               (gnus-summary-select-article nil nil nil number)
10857             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10858
10859 (defun gnus-summary-enter-digest-group (&optional force)
10860   "Enter a digest group based on the current article."
10861   (interactive "P")
10862   (gnus-set-global-variables)
10863   (gnus-summary-select-article)
10864   (let ((name (format "%s-%d"
10865                       (gnus-group-prefixed-name
10866                        gnus-newsgroup-name (list 'nndoc ""))
10867                       gnus-current-article))
10868         (ogroup gnus-newsgroup-name)
10869         (case-fold-search t)
10870         (buf (current-buffer))
10871         dig)
10872     (save-excursion
10873       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10874       (insert-buffer-substring gnus-original-article-buffer)
10875       (narrow-to-region
10876        (goto-char (point-min))
10877        (or (search-forward "\n\n" nil t) (point)))
10878       (goto-char (point-min))
10879       (delete-matching-lines "^\\(Path\\):\\|^From ")
10880       (widen))
10881     (unwind-protect
10882         (if (gnus-group-read-ephemeral-group
10883              name `(nndoc ,name (nndoc-address
10884                                  ,(get-buffer dig))
10885                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10886             ;; Make all postings to this group go to the parent group.
10887             (nconc (gnus-info-params (gnus-get-info name))
10888                    (list (cons 'to-group ogroup)))
10889           ;; Couldn't select this doc group.
10890           (switch-to-buffer buf)
10891           (gnus-set-global-variables)
10892           (gnus-configure-windows 'summary)
10893           (gnus-message 3 "Article couldn't be entered?"))
10894       (kill-buffer dig))))
10895
10896 (defun gnus-summary-isearch-article (&optional regexp-p)
10897   "Do incremental search forward on the current article.
10898 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10899   (interactive "P")
10900   (gnus-set-global-variables)
10901   (gnus-summary-select-article)
10902   (gnus-configure-windows 'article)
10903   (gnus-eval-in-buffer-window
10904    gnus-article-buffer
10905    (goto-char (point-min))
10906    (isearch-forward regexp-p)))
10907
10908 (defun gnus-summary-search-article-forward (regexp &optional backward)
10909   "Search for an article containing REGEXP forward.
10910 If BACKWARD, search backward instead."
10911   (interactive
10912    (list (read-string
10913           (format "Search article %s (regexp%s): "
10914                   (if current-prefix-arg "backward" "forward")
10915                   (if gnus-last-search-regexp
10916                       (concat ", default " gnus-last-search-regexp)
10917                     "")))
10918          current-prefix-arg))
10919   (gnus-set-global-variables)
10920   (if (string-equal regexp "")
10921       (setq regexp (or gnus-last-search-regexp ""))
10922     (setq gnus-last-search-regexp regexp))
10923   (if (gnus-summary-search-article regexp backward)
10924       (gnus-article-set-window-start
10925        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10926     (error "Search failed: \"%s\"" regexp)))
10927
10928 (defun gnus-summary-search-article-backward (regexp)
10929   "Search for an article containing REGEXP backward."
10930   (interactive
10931    (list (read-string
10932           (format "Search article backward (regexp%s): "
10933                   (if gnus-last-search-regexp
10934                       (concat ", default " gnus-last-search-regexp)
10935                     "")))))
10936   (gnus-summary-search-article-forward regexp 'backward))
10937
10938 (defun gnus-summary-search-article (regexp &optional backward)
10939   "Search for an article containing REGEXP.
10940 Optional argument BACKWARD means do search for backward.
10941 gnus-select-article-hook is not called during the search."
10942   (let ((gnus-select-article-hook nil)  ;Disable hook.
10943         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10944         (re-search
10945          (if backward
10946              (function re-search-backward) (function re-search-forward)))
10947         (found nil)
10948         (last nil))
10949     ;; Hidden thread subtrees must be searched for ,too.
10950     (gnus-summary-show-all-threads)
10951     ;; First of all, search current article.
10952     ;; We don't want to read article again from NNTP server nor reset
10953     ;; current point.
10954     (gnus-summary-select-article)
10955     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10956     (setq last gnus-current-article)
10957     (gnus-eval-in-buffer-window
10958      gnus-article-buffer
10959      (save-restriction
10960        (widen)
10961        ;; Begin search from current point.
10962        (setq found (funcall re-search regexp nil t))))
10963     ;; Then search next articles.
10964     (while (and (not found)
10965                 (gnus-summary-display-article
10966                  (if backward (gnus-summary-find-prev)
10967                    (gnus-summary-find-next))))
10968       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10969       (gnus-eval-in-buffer-window
10970        gnus-article-buffer
10971        (save-restriction
10972          (widen)
10973          (goto-char (if backward (point-max) (point-min)))
10974          (setq found (funcall re-search regexp nil t)))))
10975     (message "")
10976     ;; Adjust article pointer.
10977     (or (eq last gnus-current-article)
10978         (setq gnus-last-article last))
10979     ;; Return T if found such article.
10980     found))
10981
10982 (defun gnus-summary-find-matching (header regexp &optional backward unread
10983                                           not-case-fold)
10984   "Return a list of all articles that match REGEXP on HEADER.
10985 The search stars on the current article and goes forwards unless
10986 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10987 If UNREAD is non-nil, only unread articles will
10988 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10989 in the comparisons."
10990   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10991                 (gnus-data-find-list
10992                  (gnus-summary-article-number) (gnus-data-list backward))))
10993         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10994         (case-fold-search (not not-case-fold))
10995         articles d)
10996     (or (fboundp (intern (concat "mail-header-" header)))
10997         (error "%s is not a valid header" header))
10998     (while data
10999       (setq d (car data))
11000       (and (or (not unread)             ; We want all articles...
11001                (gnus-data-unread-p d))  ; Or just unreads.
11002            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11003            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11004            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11005       (setq data (cdr data)))
11006     (nreverse articles)))
11007
11008 (defun gnus-summary-execute-command (header regexp command &optional backward)
11009   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11010 If HEADER is an empty string (or nil), the match is done on the entire
11011 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11012   (interactive
11013    (list (let ((completion-ignore-case t))
11014            (completing-read
11015             "Header name: "
11016             (mapcar (lambda (string) (list string))
11017                     '("Number" "Subject" "From" "Lines" "Date"
11018                       "Message-ID" "Xref" "References" "Body"))
11019             nil 'require-match))
11020          (read-string "Regexp: ")
11021          (read-key-sequence "Command: ")
11022          current-prefix-arg))
11023   (when (equal header "Body")
11024     (setq header ""))
11025   (gnus-set-global-variables)
11026   ;; Hidden thread subtrees must be searched as well.
11027   (gnus-summary-show-all-threads)
11028   ;; We don't want to change current point nor window configuration.
11029   (save-excursion
11030     (save-window-excursion
11031       (gnus-message 6 "Executing %s..." (key-description command))
11032       ;; We'd like to execute COMMAND interactively so as to give arguments.
11033       (gnus-execute header regexp
11034                     `(lambda () (call-interactively ',(key-binding command)))
11035                     backward)
11036       (gnus-message 6 "Executing %s...done" (key-description command)))))
11037
11038 (defun gnus-summary-beginning-of-article ()
11039   "Scroll the article back to the beginning."
11040   (interactive)
11041   (gnus-set-global-variables)
11042   (gnus-summary-select-article)
11043   (gnus-configure-windows 'article)
11044   (gnus-eval-in-buffer-window
11045    gnus-article-buffer
11046    (widen)
11047    (goto-char (point-min))
11048    (and gnus-break-pages (gnus-narrow-to-page))))
11049
11050 (defun gnus-summary-end-of-article ()
11051   "Scroll to the end of the article."
11052   (interactive)
11053   (gnus-set-global-variables)
11054   (gnus-summary-select-article)
11055   (gnus-configure-windows 'article)
11056   (gnus-eval-in-buffer-window
11057    gnus-article-buffer
11058    (widen)
11059    (goto-char (point-max))
11060    (recenter -3)
11061    (and gnus-break-pages (gnus-narrow-to-page))))
11062
11063 (defun gnus-summary-show-article (&optional arg)
11064   "Force re-fetching of the current article.
11065 If ARG (the prefix) is non-nil, show the raw article without any
11066 article massaging functions being run."
11067   (interactive "P")
11068   (gnus-set-global-variables)
11069   (if (not arg)
11070       ;; Select the article the normal way.
11071       (gnus-summary-select-article nil 'force)
11072     ;; Bind the article treatment functions to nil.
11073     (let ((gnus-have-all-headers t)
11074           gnus-article-display-hook
11075           gnus-article-prepare-hook
11076           gnus-visual)
11077       (gnus-summary-select-article nil 'force)))
11078 ;  (gnus-configure-windows 'article)
11079   (gnus-summary-position-point))
11080
11081 (defun gnus-summary-verbose-headers (&optional arg)
11082   "Toggle permanent full header display.
11083 If ARG is a positive number, turn header display on.
11084 If ARG is a negative number, turn header display off."
11085   (interactive "P")
11086   (gnus-set-global-variables)
11087   (gnus-summary-toggle-header arg)
11088   (setq gnus-show-all-headers
11089         (cond ((or (not (numberp arg))
11090                    (zerop arg))
11091                (not gnus-show-all-headers))
11092               ((natnump arg)
11093                t))))
11094
11095 (defun gnus-summary-toggle-header (&optional arg)
11096   "Show the headers if they are hidden, or hide them if they are shown.
11097 If ARG is a positive number, show the entire header.
11098 If ARG is a negative number, hide the unwanted header lines."
11099   (interactive "P")
11100   (gnus-set-global-variables)
11101   (save-excursion
11102     (set-buffer gnus-article-buffer)
11103     (let* ((buffer-read-only nil)
11104            (inhibit-point-motion-hooks t)
11105            (hidden (text-property-any
11106                     (goto-char (point-min)) (search-forward "\n\n")
11107                     'invisible t))
11108            e)
11109       (goto-char (point-min))
11110       (when (search-forward "\n\n" nil t)
11111         (delete-region (point-min) (1- (point))))
11112       (goto-char (point-min))
11113       (save-excursion
11114         (set-buffer gnus-original-article-buffer)
11115         (goto-char (point-min))
11116         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11117       (insert-buffer-substring gnus-original-article-buffer 1 e)
11118       (let ((gnus-inhibit-hiding t))
11119         (run-hooks 'gnus-article-display-hook))
11120       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11121           (gnus-article-hide-headers)))))
11122
11123 (defun gnus-summary-show-all-headers ()
11124   "Make all header lines visible."
11125   (interactive)
11126   (gnus-set-global-variables)
11127   (gnus-article-show-all-headers))
11128
11129 (defun gnus-summary-toggle-mime (&optional arg)
11130   "Toggle MIME processing.
11131 If ARG is a positive number, turn MIME processing on."
11132   (interactive "P")
11133   (gnus-set-global-variables)
11134   (setq gnus-show-mime
11135         (if (null arg) (not gnus-show-mime)
11136           (> (prefix-numeric-value arg) 0)))
11137   (gnus-summary-select-article t 'force))
11138
11139 (defun gnus-summary-caesar-message (&optional arg)
11140   "Caesar rotate the current article by 13.
11141 The numerical prefix specifies how manu places to rotate each letter
11142 forward."
11143   (interactive "P")
11144   (gnus-set-global-variables)
11145   (gnus-summary-select-article)
11146   (let ((mail-header-separator ""))
11147     (gnus-eval-in-buffer-window
11148      gnus-article-buffer
11149      (save-restriction
11150        (widen)
11151        (let ((start (window-start)))
11152          (news-caesar-buffer-body arg)
11153          (set-window-start (get-buffer-window (current-buffer)) start))))))
11154
11155 (defun gnus-summary-stop-page-breaking ()
11156   "Stop page breaking in the current article."
11157   (interactive)
11158   (gnus-set-global-variables)
11159   (gnus-summary-select-article)
11160   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11161
11162 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11163   "Move the current article to a different newsgroup.
11164 If N is a positive number, move the N next articles.
11165 If N is a negative number, move the N previous articles.
11166 If N is nil and any articles have been marked with the process mark,
11167 move those articles instead.
11168 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11169 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11170 re-spool using this method.
11171
11172 For this function to work, both the current newsgroup and the
11173 newsgroup that you want to move to have to support the `request-move'
11174 and `request-accept' functions."
11175   (interactive "P")
11176   (unless action (setq action 'move))
11177   (gnus-set-global-variables)
11178   ;; Check whether the source group supports the required functions.
11179   (cond ((and (eq action 'move)
11180               (not (gnus-check-backend-function
11181                     'request-move-article gnus-newsgroup-name)))
11182          (error "The current group does not support article moving"))
11183         ((and (eq action 'crosspost)
11184               (not (gnus-check-backend-function
11185                     'request-replace-article gnus-newsgroup-name)))
11186          (error "The current group does not support article editing")))
11187   (let ((articles (gnus-summary-work-articles n))
11188         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11189         (names '((move "move" "Moving")
11190                  (copy "copy" "Copying")
11191                  (crosspost "crosspost" "Crossposting")))
11192         (copy-buf (save-excursion
11193                     (nnheader-set-temp-buffer " *copy article*")))
11194         art-group to-method new-xref article to-groups)
11195     (unless (assq action names)
11196       (error "Unknown action %s" action))
11197     ;; Read the newsgroup name.
11198     (when (and (not to-newsgroup)
11199                (not select-method))
11200       (setq to-newsgroup
11201             (gnus-read-move-group-name
11202              (cadr (assq action names))
11203              gnus-current-move-group articles prefix))
11204       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11205     (setq to-method (or select-method 
11206                         (gnus-find-method-for-group to-newsgroup)))
11207     ;; Check the method we are to move this article to...
11208     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11209         (error "%s does not support article copying" (car to-method)))
11210     (or (gnus-check-server to-method)
11211         (error "Can't open server %s" (car to-method)))
11212     (gnus-message 6 "%s to %s: %s..."
11213                   (caddr (assq action names))
11214                   (or (car select-method) to-newsgroup) articles)
11215     (while articles
11216       (setq article (pop articles))
11217       (setq
11218        art-group
11219        (cond
11220         ;; Move the article.
11221         ((eq action 'move)
11222          (gnus-request-move-article
11223           article                       ; Article to move
11224           gnus-newsgroup-name           ; From newsgrouo
11225           (nth 1 (gnus-find-method-for-group
11226                   gnus-newsgroup-name)) ; Server
11227           (list 'gnus-request-accept-article
11228                 to-newsgroup (list 'quote select-method)
11229                 (not articles))         ; Accept form
11230           (not articles)))              ; Only save nov last time
11231         ;; Copy the article.
11232         ((eq action 'copy)
11233          (save-excursion
11234            (set-buffer copy-buf)
11235            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11236            (gnus-request-accept-article
11237             to-newsgroup select-method (not articles))))
11238         ;; Crosspost the article.
11239         ((eq action 'crosspost)
11240          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11241            (setq new-xref (concat gnus-newsgroup-name ":" article))
11242            (if (and xref (not (string= xref "")))
11243                (progn
11244                  (when (string-match "^Xref: " xref)
11245                    (setq xref (substring xref (match-end 0))))
11246                  (setq new-xref (concat xref " " new-xref)))
11247              (setq new-xref (concat (system-name) " " new-xref)))
11248            (save-excursion
11249              (set-buffer copy-buf)
11250              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11251              (nnheader-replace-header "xref" new-xref)
11252              (gnus-request-accept-article
11253               to-newsgroup select-method (not articles)))))))
11254       (if (not art-group)
11255           (gnus-message 1 "Couldn't %s article %s"
11256                         (cadr (assq action names)) article)
11257         (let* ((entry
11258                 (or
11259                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11260                  (gnus-gethash
11261                   (gnus-group-prefixed-name
11262                    (car art-group)
11263                    (or select-method 
11264                        (gnus-find-method-for-group to-newsgroup)))
11265                   gnus-newsrc-hashtb)))
11266                (info (nth 2 entry))
11267                (to-group (gnus-info-group info)))
11268           ;; Update the group that has been moved to.
11269           (when (and info
11270                      (memq action '(move copy)))
11271             (unless (member to-group to-groups)
11272               (push to-group to-groups))
11273
11274             (unless (memq article gnus-newsgroup-unreads)
11275               (gnus-info-set-read
11276                info (gnus-add-to-range (gnus-info-read info)
11277                                        (list (cdr art-group)))))
11278
11279             ;; Copy any marks over to the new group.
11280             (let ((marks gnus-article-mark-lists)
11281                   (to-article (cdr art-group)))
11282
11283               ;; See whether the article is to be put in the cache.
11284               (when gnus-use-cache
11285                 (gnus-cache-possibly-enter-article
11286                  to-group to-article
11287                  (let ((header (copy-sequence
11288                                 (gnus-summary-article-header article))))
11289                    (mail-header-set-number header to-article)
11290                    header)
11291                  (memq article gnus-newsgroup-marked)
11292                  (memq article gnus-newsgroup-dormant)
11293                  (memq article gnus-newsgroup-unreads)))
11294
11295               (while marks
11296                 (when (memq article (symbol-value
11297                                      (intern (format "gnus-newsgroup-%s"
11298                                                      (caar marks)))))
11299                   ;; If the other group is the same as this group,
11300                   ;; then we have to add the mark to the list.
11301                   (when (equal to-group gnus-newsgroup-name)
11302                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11303                          (cons to-article
11304                                (symbol-value
11305                                 (intern (format "gnus-newsgroup-%s"
11306                                                 (caar marks)))))))
11307                   ;; Copy mark to other group.
11308                   (gnus-add-marked-articles
11309                    to-group (cdar marks) (list to-article) info))
11310                 (setq marks (cdr marks)))))
11311
11312           ;; Update the Xref header in this article to point to
11313           ;; the new crossposted article we have just created.
11314           (when (eq action 'crosspost)
11315             (save-excursion
11316               (set-buffer copy-buf)
11317               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11318               (nnheader-replace-header
11319                "xref" (concat new-xref " " (gnus-group-prefixed-name
11320                                             (car art-group) to-method)
11321                               ":" (cdr art-group)))
11322               (gnus-request-replace-article
11323                article gnus-newsgroup-name (current-buffer)))))
11324
11325         (gnus-summary-goto-subject article)
11326         (when (eq action 'move)
11327           (gnus-summary-mark-article article gnus-canceled-mark)))
11328       (gnus-summary-remove-process-mark article))
11329     ;; Re-activate all groups that have been moved to.
11330     (while to-groups
11331       (gnus-activate-group (pop to-groups)))
11332     
11333     (gnus-kill-buffer copy-buf)
11334     (gnus-summary-position-point)
11335     (gnus-set-mode-line 'summary)))
11336
11337 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11338   "Move the current article to a different newsgroup.
11339 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11340 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11341 re-spool using this method."
11342   (interactive "P")
11343   (gnus-summary-move-article n nil select-method 'copy))
11344
11345 (defun gnus-summary-crosspost-article (&optional n)
11346   "Crosspost the current article to some other group."
11347   (interactive "P")
11348   (gnus-summary-move-article n nil nil 'crosspost))
11349
11350 (defun gnus-summary-respool-article (&optional n method)
11351   "Respool the current article.
11352 The article will be squeezed through the mail spooling process again,
11353 which means that it will be put in some mail newsgroup or other
11354 depending on `nnmail-split-methods'.
11355 If N is a positive number, respool the N next articles.
11356 If N is a negative number, respool the N previous articles.
11357 If N is nil and any articles have been marked with the process mark,
11358 respool those articles instead.
11359
11360 Respooling can be done both from mail groups and \"real\" newsgroups.
11361 In the former case, the articles in question will be moved from the
11362 current group into whatever groups they are destined to.  In the
11363 latter case, they will be copied into the relevant groups."
11364   (interactive 
11365    (list current-prefix-arg
11366          (let* ((methods (gnus-methods-using 'respool))
11367                 (methname
11368                  (symbol-name (car (gnus-find-method-for-group
11369                                     gnus-newsgroup-name))))
11370                 (method
11371                  (completing-read
11372                   "What backend do you want to use when respooling? "
11373                   methods nil t (cons methname 0)))
11374                 ms)
11375            (cond
11376             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11377              (list (intern method) ""))
11378             ((= 1 (length ms))
11379              (car ms))
11380             (t
11381              (cdr (completing-read 
11382                    "Server name: "
11383                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11384   (gnus-set-global-variables)
11385   (unless method
11386     (error "No method given for respooling"))
11387   (if (assoc (symbol-name
11388               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11389              (gnus-methods-using 'respool))
11390       (gnus-summary-move-article n nil method)
11391     (gnus-summary-copy-article n nil method)))
11392
11393 (defun gnus-summary-import-article (file)
11394   "Import a random file into a mail newsgroup."
11395   (interactive "fImport file: ")
11396   (gnus-set-global-variables)
11397   (let ((group gnus-newsgroup-name)
11398         (now (current-time))
11399         atts lines)
11400     (or (gnus-check-backend-function 'request-accept-article group)
11401         (error "%s does not support article importing" group))
11402     (or (file-readable-p file)
11403         (not (file-regular-p file))
11404         (error "Can't read %s" file))
11405     (save-excursion
11406       (set-buffer (get-buffer-create " *import file*"))
11407       (buffer-disable-undo (current-buffer))
11408       (erase-buffer)
11409       (insert-file-contents file)
11410       (goto-char (point-min))
11411       (unless (nnheader-article-p)
11412         ;; This doesn't look like an article, so we fudge some headers.
11413         (setq atts (file-attributes file)
11414               lines (count-lines (point-min) (point-max)))
11415         (insert "From: " (read-string "From: ") "\n"
11416                 "Subject: " (read-string "Subject: ") "\n"
11417                 "Date: " (timezone-make-date-arpa-standard
11418                           (current-time-string (nth 5 atts))
11419                           (current-time-zone now)
11420                           (current-time-zone now)) "\n"
11421                 "Message-ID: " (gnus-inews-message-id) "\n"
11422                 "Lines: " (int-to-string lines) "\n"
11423                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11424       (gnus-request-accept-article group nil t)
11425       (kill-buffer (current-buffer)))))
11426
11427 (defun gnus-summary-expire-articles ()
11428   "Expire all articles that are marked as expirable in the current group."
11429   (interactive)
11430   (gnus-set-global-variables)
11431   (when (gnus-check-backend-function
11432          'request-expire-articles gnus-newsgroup-name)
11433     ;; This backend supports expiry.
11434     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11435            (expirable (if total
11436                           (gnus-list-of-read-articles gnus-newsgroup-name)
11437                         (setq gnus-newsgroup-expirable
11438                               (sort gnus-newsgroup-expirable '<))))
11439            (expiry-wait (gnus-group-get-parameter
11440                          gnus-newsgroup-name 'expiry-wait))
11441            es)
11442       (when expirable
11443         ;; There are expirable articles in this group, so we run them
11444         ;; through the expiry process.
11445         (gnus-message 6 "Expiring articles...")
11446         ;; The list of articles that weren't expired is returned.
11447         (if expiry-wait
11448             (let ((nnmail-expiry-wait-function nil)
11449                   (nnmail-expiry-wait expiry-wait))
11450               (setq es (gnus-request-expire-articles
11451                         expirable gnus-newsgroup-name)))
11452           (setq es (gnus-request-expire-articles
11453                     expirable gnus-newsgroup-name)))
11454         (or total (setq gnus-newsgroup-expirable es))
11455         ;; We go through the old list of expirable, and mark all
11456         ;; really expired articles as nonexistent.
11457         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11458           (let ((gnus-use-cache nil))
11459             (while expirable
11460               (unless (memq (car expirable) es)
11461                 (when (gnus-data-find (car expirable))
11462                   (gnus-summary-mark-article
11463                    (car expirable) gnus-canceled-mark)))
11464               (setq expirable (cdr expirable)))))
11465         (gnus-message 6 "Expiring articles...done")))))
11466
11467 (defun gnus-summary-expire-articles-now ()
11468   "Expunge all expirable articles in the current group.
11469 This means that *all* articles that are marked as expirable will be
11470 deleted forever, right now."
11471   (interactive)
11472   (gnus-set-global-variables)
11473   (or gnus-expert-user
11474       (gnus-y-or-n-p
11475        "Are you really, really, really sure you want to expunge? ")
11476       (error "Phew!"))
11477   (let ((nnmail-expiry-wait 'immediate)
11478         (nnmail-expiry-wait-function nil))
11479     (gnus-summary-expire-articles)))
11480
11481 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11482 (defun gnus-summary-delete-article (&optional n)
11483   "Delete the N next (mail) articles.
11484 This command actually deletes articles.  This is not a marking
11485 command.  The article will disappear forever from your life, never to
11486 return.
11487 If N is negative, delete backwards.
11488 If N is nil and articles have been marked with the process mark,
11489 delete these instead."
11490   (interactive "P")
11491   (gnus-set-global-variables)
11492   (or (gnus-check-backend-function 'request-expire-articles
11493                                    gnus-newsgroup-name)
11494       (error "The current newsgroup does not support article deletion."))
11495   ;; Compute the list of articles to delete.
11496   (let ((articles (gnus-summary-work-articles n))
11497         not-deleted)
11498     (if (and gnus-novice-user
11499              (not (gnus-y-or-n-p
11500                    (format "Do you really want to delete %s forever? "
11501                            (if (> (length articles) 1) "these articles"
11502                              "this article")))))
11503         ()
11504       ;; Delete the articles.
11505       (setq not-deleted (gnus-request-expire-articles
11506                          articles gnus-newsgroup-name 'force))
11507       (while articles
11508         (gnus-summary-remove-process-mark (car articles))
11509         ;; The backend might not have been able to delete the article
11510         ;; after all.
11511         (or (memq (car articles) not-deleted)
11512             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11513         (setq articles (cdr articles))))
11514     (gnus-summary-position-point)
11515     (gnus-set-mode-line 'summary)
11516     not-deleted))
11517
11518 (defun gnus-summary-edit-article (&optional force)
11519   "Enter into a buffer and edit the current article.
11520 This will have permanent effect only in mail groups.
11521 If FORCE is non-nil, allow editing of articles even in read-only
11522 groups."
11523   (interactive "P")
11524   (save-excursion
11525     (set-buffer gnus-summary-buffer)
11526     (gnus-set-global-variables)
11527     (when (and (not force)
11528                (gnus-group-read-only-p))
11529       (error "The current newsgroup does not support article editing."))
11530     (gnus-summary-select-article t nil t)
11531     (gnus-configure-windows 'article)
11532     (select-window (get-buffer-window gnus-article-buffer))
11533     (gnus-message 6 "C-c C-c to end edits")
11534     (setq buffer-read-only nil)
11535     (text-mode)
11536     (use-local-map (copy-keymap (current-local-map)))
11537     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11538     (buffer-enable-undo)
11539     (widen)
11540     (goto-char (point-min))
11541     (search-forward "\n\n" nil t)))
11542
11543 (defun gnus-summary-edit-article-done ()
11544   "Make edits to the current article permanent."
11545   (interactive)
11546   (if (gnus-group-read-only-p)
11547       (progn
11548         (gnus-summary-edit-article-postpone)
11549         (gnus-message
11550          1 "The current newsgroup does not support article editing.")
11551         (ding))
11552     (let ((buf (format "%s" (buffer-string))))
11553       (erase-buffer)
11554       (insert buf)
11555       (if (not (gnus-request-replace-article
11556                 (cdr gnus-article-current) (car gnus-article-current)
11557                 (current-buffer)))
11558           (error "Couldn't replace article.")
11559         (gnus-article-mode)
11560         (use-local-map gnus-article-mode-map)
11561         (setq buffer-read-only t)
11562         (buffer-disable-undo (current-buffer))
11563         (gnus-configure-windows 'summary)
11564         (gnus-summary-update-article (cdr gnus-article-current))
11565         (when gnus-use-cache
11566           (gnus-cache-update-article 
11567            (cdr gnus-article-current) (car gnus-article-current))))
11568       (run-hooks 'gnus-article-display-hook)
11569       (and (gnus-visual-p 'summary-highlight 'highlight)
11570            (run-hooks 'gnus-visual-mark-article-hook)))))
11571
11572 (defun gnus-summary-edit-article-postpone ()
11573   "Postpone changes to the current article."
11574   (interactive)
11575   (gnus-article-mode)
11576   (use-local-map gnus-article-mode-map)
11577   (setq buffer-read-only t)
11578   (buffer-disable-undo (current-buffer))
11579   (gnus-configure-windows 'summary)
11580   (and (gnus-visual-p 'summary-highlight 'highlight)
11581        (run-hooks 'gnus-visual-mark-article-hook)))
11582
11583 (defun gnus-summary-respool-query ()
11584   "Query where the respool algorithm would put this article."
11585   (interactive)
11586   (gnus-set-global-variables)
11587   (gnus-summary-select-article)
11588   (save-excursion
11589     (set-buffer gnus-article-buffer)
11590     (save-restriction
11591       (goto-char (point-min))
11592       (search-forward "\n\n")
11593       (narrow-to-region (point-min) (point))
11594       (pp-eval-expression
11595        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11596
11597 ;; Summary score commands.
11598
11599 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11600
11601 (defun gnus-summary-raise-score (n)
11602   "Raise the score of the current article by N."
11603   (interactive "p")
11604   (gnus-set-global-variables)
11605   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11606
11607 (defun gnus-summary-set-score (n)
11608   "Set the score of the current article to N."
11609   (interactive "p")
11610   (gnus-set-global-variables)
11611   (save-excursion
11612     (gnus-summary-show-thread)
11613     (let ((buffer-read-only nil))
11614       ;; Set score.
11615       (gnus-summary-update-mark
11616        (if (= n (or gnus-summary-default-score 0)) ? 
11617          (if (< n (or gnus-summary-default-score 0))
11618              gnus-score-below-mark gnus-score-over-mark)) 'score))
11619     (let* ((article (gnus-summary-article-number))
11620            (score (assq article gnus-newsgroup-scored)))
11621       (if score (setcdr score n)
11622         (setq gnus-newsgroup-scored
11623               (cons (cons article n) gnus-newsgroup-scored))))
11624     (gnus-summary-update-line)))
11625
11626 (defun gnus-summary-current-score ()
11627   "Return the score of the current article."
11628   (interactive)
11629   (gnus-set-global-variables)
11630   (message "%s" (gnus-summary-article-score)))
11631
11632 ;; Summary marking commands.
11633
11634 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11635   "Mark articles which has the same subject as read, and then select the next.
11636 If UNMARK is positive, remove any kind of mark.
11637 If UNMARK is negative, tick articles."
11638   (interactive "P")
11639   (gnus-set-global-variables)
11640   (if unmark
11641       (setq unmark (prefix-numeric-value unmark)))
11642   (let ((count
11643          (gnus-summary-mark-same-subject
11644           (gnus-summary-article-subject) unmark)))
11645     ;; Select next unread article.  If auto-select-same mode, should
11646     ;; select the first unread article.
11647     (gnus-summary-next-article t (and gnus-auto-select-same
11648                                       (gnus-summary-article-subject)))
11649     (gnus-message 7 "%d article%s marked as %s"
11650                   count (if (= count 1) " is" "s are")
11651                   (if unmark "unread" "read"))))
11652
11653 (defun gnus-summary-kill-same-subject (&optional unmark)
11654   "Mark articles which has the same subject as read.
11655 If UNMARK is positive, remove any kind of mark.
11656 If UNMARK is negative, tick articles."
11657   (interactive "P")
11658   (gnus-set-global-variables)
11659   (if unmark
11660       (setq unmark (prefix-numeric-value unmark)))
11661   (let ((count
11662          (gnus-summary-mark-same-subject
11663           (gnus-summary-article-subject) unmark)))
11664     ;; If marked as read, go to next unread subject.
11665     (if (null unmark)
11666         ;; Go to next unread subject.
11667         (gnus-summary-next-subject 1 t))
11668     (gnus-message 7 "%d articles are marked as %s"
11669                   count (if unmark "unread" "read"))))
11670
11671 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11672   "Mark articles with same SUBJECT as read, and return marked number.
11673 If optional argument UNMARK is positive, remove any kinds of marks.
11674 If optional argument UNMARK is negative, mark articles as unread instead."
11675   (let ((count 1))
11676     (save-excursion
11677       (cond
11678        ((null unmark)                   ; Mark as read.
11679         (while (and
11680                 (progn
11681                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11682                   (gnus-summary-show-thread) t)
11683                 (gnus-summary-find-subject subject))
11684           (setq count (1+ count))))
11685        ((> unmark 0)                    ; Tick.
11686         (while (and
11687                 (progn
11688                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11689                   (gnus-summary-show-thread) t)
11690                 (gnus-summary-find-subject subject))
11691           (setq count (1+ count))))
11692        (t                               ; Mark as unread.
11693         (while (and
11694                 (progn
11695                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11696                   (gnus-summary-show-thread) t)
11697                 (gnus-summary-find-subject subject))
11698           (setq count (1+ count)))))
11699       (gnus-set-mode-line 'summary)
11700       ;; Return the number of marked articles.
11701       count)))
11702
11703 (defun gnus-summary-mark-as-processable (n &optional unmark)
11704   "Set the process mark on the next N articles.
11705 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11706 the process mark instead.  The difference between N and the actual
11707 number of articles marked is returned."
11708   (interactive "p")
11709   (gnus-set-global-variables)
11710   (let ((backward (< n 0))
11711         (n (abs n)))
11712     (while (and
11713             (> n 0)
11714             (if unmark
11715                 (gnus-summary-remove-process-mark
11716                  (gnus-summary-article-number))
11717               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11718             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11719       (setq n (1- n)))
11720     (if (/= 0 n) (gnus-message 7 "No more articles"))
11721     (gnus-summary-recenter)
11722     (gnus-summary-position-point)
11723     n))
11724
11725 (defun gnus-summary-unmark-as-processable (n)
11726   "Remove the process mark from the next N articles.
11727 If N is negative, mark backward instead.  The difference between N and
11728 the actual number of articles marked is returned."
11729   (interactive "p")
11730   (gnus-set-global-variables)
11731   (gnus-summary-mark-as-processable n t))
11732
11733 (defun gnus-summary-unmark-all-processable ()
11734   "Remove the process mark from all articles."
11735   (interactive)
11736   (gnus-set-global-variables)
11737   (save-excursion
11738     (while gnus-newsgroup-processable
11739       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11740   (gnus-summary-position-point))
11741
11742 (defun gnus-summary-mark-as-expirable (n)
11743   "Mark N articles forward as expirable.
11744 If N is negative, mark backward instead.  The difference between N and
11745 the actual number of articles marked is returned."
11746   (interactive "p")
11747   (gnus-set-global-variables)
11748   (gnus-summary-mark-forward n gnus-expirable-mark))
11749
11750 (defun gnus-summary-mark-article-as-replied (article)
11751   "Mark ARTICLE replied and update the summary line."
11752   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11753   (let ((buffer-read-only nil))
11754     (when (gnus-summary-goto-subject article)
11755       (gnus-summary-update-secondary-mark article))))
11756
11757 (defun gnus-summary-set-bookmark (article)
11758   "Set a bookmark in current article."
11759   (interactive (list (gnus-summary-article-number)))
11760   (gnus-set-global-variables)
11761   (if (or (not (get-buffer gnus-article-buffer))
11762           (not gnus-current-article)
11763           (not gnus-article-current)
11764           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11765       (error "No current article selected"))
11766   ;; Remove old bookmark, if one exists.
11767   (let ((old (assq article gnus-newsgroup-bookmarks)))
11768     (if old (setq gnus-newsgroup-bookmarks
11769                   (delq old gnus-newsgroup-bookmarks))))
11770   ;; Set the new bookmark, which is on the form
11771   ;; (article-number . line-number-in-body).
11772   (setq gnus-newsgroup-bookmarks
11773         (cons
11774          (cons article
11775                (save-excursion
11776                  (set-buffer gnus-article-buffer)
11777                  (count-lines
11778                   (min (point)
11779                        (save-excursion
11780                          (goto-char (point-min))
11781                          (search-forward "\n\n" nil t)
11782                          (point)))
11783                   (point))))
11784          gnus-newsgroup-bookmarks))
11785   (gnus-message 6 "A bookmark has been added to the current article."))
11786
11787 (defun gnus-summary-remove-bookmark (article)
11788   "Remove the bookmark from the current article."
11789   (interactive (list (gnus-summary-article-number)))
11790   (gnus-set-global-variables)
11791   ;; Remove old bookmark, if one exists.
11792   (let ((old (assq article gnus-newsgroup-bookmarks)))
11793     (if old
11794         (progn
11795           (setq gnus-newsgroup-bookmarks
11796                 (delq old gnus-newsgroup-bookmarks))
11797           (gnus-message 6 "Removed bookmark."))
11798       (gnus-message 6 "No bookmark in current article."))))
11799
11800 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11801 (defun gnus-summary-mark-as-dormant (n)
11802   "Mark N articles forward as dormant.
11803 If N is negative, mark backward instead.  The difference between N and
11804 the actual number of articles marked is returned."
11805   (interactive "p")
11806   (gnus-set-global-variables)
11807   (gnus-summary-mark-forward n gnus-dormant-mark))
11808
11809 (defun gnus-summary-set-process-mark (article)
11810   "Set the process mark on ARTICLE and update the summary line."
11811   (setq gnus-newsgroup-processable
11812         (cons article
11813               (delq article gnus-newsgroup-processable)))
11814   (when (gnus-summary-goto-subject article)
11815     (gnus-summary-show-thread)
11816     (gnus-summary-update-secondary-mark article)))
11817
11818 (defun gnus-summary-remove-process-mark (article)
11819   "Remove the process mark from ARTICLE and update the summary line."
11820   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11821   (when (gnus-summary-goto-subject article)
11822     (gnus-summary-show-thread)
11823     (gnus-summary-update-secondary-mark article)))
11824
11825 (defun gnus-summary-set-saved-mark (article)
11826   "Set the process mark on ARTICLE and update the summary line."
11827   (push article gnus-newsgroup-saved)
11828   (when (gnus-summary-goto-subject article)
11829     (gnus-summary-update-secondary-mark article)))
11830
11831 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11832   "Mark N articles as read forwards.
11833 If N is negative, mark backwards instead.
11834 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11835 marked as unread.
11836 The difference between N and the actual number of articles marked is
11837 returned."
11838   (interactive "p")
11839   (gnus-set-global-variables)
11840   (let ((backward (< n 0))
11841         (gnus-summary-goto-unread
11842          (and gnus-summary-goto-unread
11843               (not (eq gnus-summary-goto-unread 'never))
11844               (not (memq mark (list gnus-unread-mark
11845                                     gnus-ticked-mark gnus-dormant-mark)))))
11846         (n (abs n))
11847         (mark (or mark gnus-del-mark)))
11848     (while (and (> n 0)
11849                 (gnus-summary-mark-article nil mark no-expire)
11850                 (zerop (gnus-summary-next-subject
11851                         (if backward -1 1)
11852                         (and gnus-summary-goto-unread
11853                              (not (eq gnus-summary-goto-unread 'never)))
11854                         t)))
11855       (setq n (1- n)))
11856     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11857     (gnus-summary-recenter)
11858     (gnus-summary-position-point)
11859     (gnus-set-mode-line 'summary)
11860     n))
11861
11862 (defun gnus-summary-mark-article-as-read (mark)
11863   "Mark the current article quickly as read with MARK."
11864   (let ((article (gnus-summary-article-number)))
11865     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11866     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11867     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11868     (setq gnus-newsgroup-reads
11869           (cons (cons article mark) gnus-newsgroup-reads))
11870     ;; Possibly remove from cache, if that is used.
11871     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11872     ;; Allow the backend to change the mark.
11873     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11874     ;; Check for auto-expiry.
11875     (when (and gnus-newsgroup-auto-expire
11876                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11877                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11878                    (= mark gnus-ancient-mark)
11879                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11880       (setq mark gnus-expirable-mark)
11881       (push article gnus-newsgroup-expirable))
11882     ;; Set the mark in the buffer.
11883     (gnus-summary-update-mark mark 'unread)
11884     t))
11885
11886 (defun gnus-summary-mark-article-as-unread (mark)
11887   "Mark the current article quickly as unread with MARK."
11888   (let ((article (gnus-summary-article-number)))
11889     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11890     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11891     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11892     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11893     (cond ((= mark gnus-ticked-mark)
11894            (push article gnus-newsgroup-marked))
11895           ((= mark gnus-dormant-mark)
11896            (push article gnus-newsgroup-dormant))
11897           (t
11898            (push article gnus-newsgroup-unreads)))
11899     (setq gnus-newsgroup-reads
11900           (delq (assq article gnus-newsgroup-reads)
11901                 gnus-newsgroup-reads))
11902
11903     ;; See whether the article is to be put in the cache.
11904     (and gnus-use-cache
11905          (vectorp (gnus-summary-article-header article))
11906          (save-excursion
11907            (gnus-cache-possibly-enter-article
11908             gnus-newsgroup-name article
11909             (gnus-summary-article-header article)
11910             (= mark gnus-ticked-mark)
11911             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11912
11913     ;; Fix the mark.
11914     (gnus-summary-update-mark mark 'unread)
11915     t))
11916
11917 (defun gnus-summary-mark-article (&optional article mark no-expire)
11918   "Mark ARTICLE with MARK.  MARK can be any character.
11919 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11920 `??' (dormant) and `?E' (expirable).
11921 If MARK is nil, then the default character `?D' is used.
11922 If ARTICLE is nil, then the article on the current line will be
11923 marked."
11924   ;; The mark might be a string.
11925   (and (stringp mark)
11926        (setq mark (aref mark 0)))
11927   ;; If no mark is given, then we check auto-expiring.
11928   (and (not no-expire)
11929        gnus-newsgroup-auto-expire
11930        (or (not mark)
11931            (and (numberp mark)
11932                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11933                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11934                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11935        (setq mark gnus-expirable-mark))
11936   (let* ((mark (or mark gnus-del-mark))
11937          (article (or article (gnus-summary-article-number))))
11938     (or article (error "No article on current line"))
11939     (if (or (= mark gnus-unread-mark)
11940             (= mark gnus-ticked-mark)
11941             (= mark gnus-dormant-mark))
11942         (gnus-mark-article-as-unread article mark)
11943       (gnus-mark-article-as-read article mark))
11944
11945     ;; See whether the article is to be put in the cache.
11946     (and gnus-use-cache
11947          (not (= mark gnus-canceled-mark))
11948          (vectorp (gnus-summary-article-header article))
11949          (save-excursion
11950            (gnus-cache-possibly-enter-article
11951             gnus-newsgroup-name article
11952             (gnus-summary-article-header article)
11953             (= mark gnus-ticked-mark)
11954             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11955
11956     (if (gnus-summary-goto-subject article nil t)
11957         (let ((buffer-read-only nil))
11958           (gnus-summary-show-thread)
11959           ;; Fix the mark.
11960           (gnus-summary-update-mark mark 'unread)
11961           t))))
11962
11963 (defun gnus-summary-update-secondary-mark (article)
11964   "Update the secondary (read, process, cache) mark."
11965   (gnus-summary-update-mark
11966    (cond ((memq article gnus-newsgroup-processable)
11967           gnus-process-mark)
11968          ((memq article gnus-newsgroup-cached)
11969           gnus-cached-mark)
11970          ((memq article gnus-newsgroup-replied)
11971           gnus-replied-mark)
11972          ((memq article gnus-newsgroup-saved)
11973           gnus-saved-mark)
11974          (t gnus-unread-mark))
11975    'replied)
11976   (when (gnus-visual-p 'summary-highlight 'highlight)
11977     (run-hooks 'gnus-summary-update-hook))
11978   t)
11979
11980 (defun gnus-summary-update-mark (mark type)
11981   (beginning-of-line)
11982   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11983         (buffer-read-only nil))
11984     (when forward
11985       ;; Go to the right position on the line.
11986       (forward-char forward)
11987       ;; Replace the old mark with the new mark.
11988       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11989       ;; Optionally update the marks by some user rule.
11990       (when (eq type 'unread)
11991         (gnus-data-set-mark
11992          (gnus-data-find (gnus-summary-article-number)) mark)
11993         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11994
11995 (defun gnus-mark-article-as-read (article &optional mark)
11996   "Enter ARTICLE in the pertinent lists and remove it from others."
11997   ;; Make the article expirable.
11998   (let ((mark (or mark gnus-del-mark)))
11999     (if (= mark gnus-expirable-mark)
12000         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12001       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12002     ;; Remove from unread and marked lists.
12003     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12004     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12005     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12006     (push (cons article mark) gnus-newsgroup-reads)
12007     ;; Possibly remove from cache, if that is used.
12008     (when gnus-use-cache
12009       (gnus-cache-enter-remove-article article))))
12010
12011 (defun gnus-mark-article-as-unread (article &optional mark)
12012   "Enter ARTICLE in the pertinent lists and remove it from others."
12013   (let ((mark (or mark gnus-ticked-mark)))
12014     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12015     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12016     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12017     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12018     (cond ((= mark gnus-ticked-mark)
12019            (push article gnus-newsgroup-marked))
12020           ((= mark gnus-dormant-mark)
12021            (push article gnus-newsgroup-dormant))
12022           (t
12023            (push article gnus-newsgroup-unreads)))
12024     (setq gnus-newsgroup-reads
12025           (delq (assq article gnus-newsgroup-reads)
12026                 gnus-newsgroup-reads))))
12027
12028 (defalias 'gnus-summary-mark-as-unread-forward
12029   'gnus-summary-tick-article-forward)
12030 (make-obsolete 'gnus-summary-mark-as-unread-forward
12031                'gnus-summary-tick-article-forward)
12032 (defun gnus-summary-tick-article-forward (n)
12033   "Tick N articles forwards.
12034 If N is negative, tick backwards instead.
12035 The difference between N and the number of articles ticked is returned."
12036   (interactive "p")
12037   (gnus-summary-mark-forward n gnus-ticked-mark))
12038
12039 (defalias 'gnus-summary-mark-as-unread-backward
12040   'gnus-summary-tick-article-backward)
12041 (make-obsolete 'gnus-summary-mark-as-unread-backward
12042                'gnus-summary-tick-article-backward)
12043 (defun gnus-summary-tick-article-backward (n)
12044   "Tick N articles backwards.
12045 The difference between N and the number of articles ticked is returned."
12046   (interactive "p")
12047   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12048
12049 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12050 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12051 (defun gnus-summary-tick-article (&optional article clear-mark)
12052   "Mark current article as unread.
12053 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12054 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12055   (interactive)
12056   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12057                                        gnus-ticked-mark)))
12058
12059 (defun gnus-summary-mark-as-read-forward (n)
12060   "Mark N articles as read forwards.
12061 If N is negative, mark backwards instead.
12062 The difference between N and the actual number of articles marked is
12063 returned."
12064   (interactive "p")
12065   (gnus-summary-mark-forward n gnus-del-mark t))
12066
12067 (defun gnus-summary-mark-as-read-backward (n)
12068   "Mark the N articles as read backwards.
12069 The difference between N and the actual number of articles marked is
12070 returned."
12071   (interactive "p")
12072   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12073
12074 (defun gnus-summary-mark-as-read (&optional article mark)
12075   "Mark current article as read.
12076 ARTICLE specifies the article to be marked as read.
12077 MARK specifies a string to be inserted at the beginning of the line."
12078   (gnus-summary-mark-article article mark))
12079
12080 (defun gnus-summary-clear-mark-forward (n)
12081   "Clear marks from N articles forward.
12082 If N is negative, clear backward instead.
12083 The difference between N and the number of marks cleared is returned."
12084   (interactive "p")
12085   (gnus-summary-mark-forward n gnus-unread-mark))
12086
12087 (defun gnus-summary-clear-mark-backward (n)
12088   "Clear marks from N articles backward.
12089 The difference between N and the number of marks cleared is returned."
12090   (interactive "p")
12091   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12092
12093 (defun gnus-summary-mark-unread-as-read ()
12094   "Intended to be used by `gnus-summary-mark-article-hook'."
12095   (when (memq gnus-current-article gnus-newsgroup-unreads)
12096     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12097
12098 (defun gnus-summary-mark-read-and-unread-as-read ()
12099   "Intended to be used by `gnus-summary-mark-article-hook'."
12100   (let ((mark (gnus-summary-article-mark)))
12101     (when (or (gnus-unread-mark-p mark)
12102               (gnus-read-mark-p mark))
12103       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12104
12105 (defun gnus-summary-mark-region-as-read (point mark all)
12106   "Mark all unread articles between point and mark as read.
12107 If given a prefix, mark all articles between point and mark as read,
12108 even ticked and dormant ones."
12109   (interactive "r\nP")
12110   (save-excursion
12111     (let (article)
12112       (goto-char point)
12113       (beginning-of-line)
12114       (while (and
12115               (< (point) mark)
12116               (progn
12117                 (when (or all
12118                           (memq (setq article (gnus-summary-article-number))
12119                                 gnus-newsgroup-unreads))
12120                   (gnus-summary-mark-article article gnus-del-mark))
12121                 t)
12122               (gnus-summary-find-next))))))
12123
12124 (defun gnus-summary-mark-below (score mark)
12125   "Mark articles with score less than SCORE with MARK."
12126   (interactive "P\ncMark: ")
12127   (gnus-set-global-variables)
12128   (setq score (if score
12129                   (prefix-numeric-value score)
12130                 (or gnus-summary-default-score 0)))
12131   (save-excursion
12132     (set-buffer gnus-summary-buffer)
12133     (goto-char (point-min))
12134     (while 
12135         (progn
12136           (and (< (gnus-summary-article-score) score)
12137                (gnus-summary-mark-article nil mark))
12138           (gnus-summary-find-next)))))
12139
12140 (defun gnus-summary-kill-below (&optional score)
12141   "Mark articles with score below SCORE as read."
12142   (interactive "P")
12143   (gnus-set-global-variables)
12144   (gnus-summary-mark-below score gnus-killed-mark))
12145
12146 (defun gnus-summary-clear-above (&optional score)
12147   "Clear all marks from articles with score above SCORE."
12148   (interactive "P")
12149   (gnus-set-global-variables)
12150   (gnus-summary-mark-above score gnus-unread-mark))
12151
12152 (defun gnus-summary-tick-above (&optional score)
12153   "Tick all articles with score above SCORE."
12154   (interactive "P")
12155   (gnus-set-global-variables)
12156   (gnus-summary-mark-above score gnus-ticked-mark))
12157
12158 (defun gnus-summary-mark-above (score mark)
12159   "Mark articles with score over SCORE with MARK."
12160   (interactive "P\ncMark: ")
12161   (gnus-set-global-variables)
12162   (setq score (if score
12163                   (prefix-numeric-value score)
12164                 (or gnus-summary-default-score 0)))
12165   (save-excursion
12166     (set-buffer gnus-summary-buffer)
12167     (goto-char (point-min))
12168     (while (and (progn
12169                   (if (> (gnus-summary-article-score) score)
12170                       (gnus-summary-mark-article nil mark))
12171                   t)
12172                 (gnus-summary-find-next)))))
12173
12174 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12175 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12176 (defun gnus-summary-limit-include-expunged ()
12177   "Display all the hidden articles that were expunged for low scores."
12178   (interactive)
12179   (gnus-set-global-variables)
12180   (let ((buffer-read-only nil))
12181     (let ((scored gnus-newsgroup-scored)
12182           headers h)
12183       (while scored
12184         (or (gnus-summary-goto-subject (caar scored))
12185             (and (setq h (gnus-summary-article-header (caar scored)))
12186                  (< (cdar scored) gnus-summary-expunge-below)
12187                  (setq headers (cons h headers))))
12188         (setq scored (cdr scored)))
12189       (or headers (error "No expunged articles hidden."))
12190       (goto-char (point-min))
12191       (gnus-summary-prepare-unthreaded (nreverse headers)))
12192     (goto-char (point-min))
12193     (gnus-summary-position-point)))
12194
12195 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12196   "Mark all articles not marked as unread in this newsgroup as read.
12197 If prefix argument ALL is non-nil, all articles are marked as read.
12198 If QUIETLY is non-nil, no questions will be asked.
12199 If TO-HERE is non-nil, it should be a point in the buffer.  All
12200 articles before this point will be marked as read.
12201 The number of articles marked as read is returned."
12202   (interactive "P")
12203   (gnus-set-global-variables)
12204   (prog1
12205       (if (or quietly
12206               (not gnus-interactive-catchup) ;Without confirmation?
12207               gnus-expert-user
12208               (gnus-y-or-n-p
12209                (if all
12210                    "Mark absolutely all articles as read? "
12211                  "Mark all unread articles as read? ")))
12212           (if (and not-mark
12213                    (not gnus-newsgroup-adaptive)
12214                    (not gnus-newsgroup-auto-expire))
12215               (progn
12216                 (when all
12217                   (setq gnus-newsgroup-marked nil
12218                         gnus-newsgroup-dormant nil))
12219                 (setq gnus-newsgroup-unreads nil))
12220             ;; We actually mark all articles as canceled, which we
12221             ;; have to do when using auto-expiry or adaptive scoring.
12222             (gnus-summary-show-all-threads)
12223             (if (gnus-summary-first-subject (not all))
12224                 (while (and
12225                         (if to-here (< (point) to-here) t)
12226                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12227                         (gnus-summary-find-next (not all)))))
12228             (unless to-here
12229               (setq gnus-newsgroup-unreads nil))
12230             (gnus-set-mode-line 'summary)))
12231     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12232       (if (and (not to-here) (eq 'nnvirtual (car method)))
12233           (nnvirtual-catchup-group
12234            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12235     (gnus-summary-position-point)))
12236
12237 (defun gnus-summary-catchup-to-here (&optional all)
12238   "Mark all unticked articles before the current one as read.
12239 If ALL is non-nil, also mark ticked and dormant articles as read."
12240   (interactive "P")
12241   (gnus-set-global-variables)
12242   (save-excursion
12243     (let ((beg (point)))
12244       ;; We check that there are unread articles.
12245       (when (or all (gnus-summary-find-prev))
12246         (gnus-summary-catchup all t beg))))
12247   (gnus-summary-position-point))
12248
12249 (defun gnus-summary-catchup-all (&optional quietly)
12250   "Mark all articles in this newsgroup as read."
12251   (interactive "P")
12252   (gnus-set-global-variables)
12253   (gnus-summary-catchup t quietly))
12254
12255 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12256   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12257 If prefix argument ALL is non-nil, all articles are marked as read."
12258   (interactive "P")
12259   (gnus-set-global-variables)
12260   (gnus-summary-catchup all quietly nil 'fast)
12261   ;; Select next newsgroup or exit.
12262   (if (eq gnus-auto-select-next 'quietly)
12263       (gnus-summary-next-group nil)
12264     (gnus-summary-exit)))
12265
12266 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12267   "Mark all articles in this newsgroup as read, and then exit."
12268   (interactive "P")
12269   (gnus-set-global-variables)
12270   (gnus-summary-catchup-and-exit t quietly))
12271
12272 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12273 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12274   "Mark all articles in this group as read and select the next group.
12275 If given a prefix, mark all articles, unread as well as ticked, as
12276 read."
12277   (interactive "P")
12278   (gnus-set-global-variables)
12279   (save-excursion
12280     (gnus-summary-catchup all))
12281   (gnus-summary-next-article t nil nil t))
12282
12283 ;; Thread-based commands.
12284
12285 (defun gnus-summary-articles-in-thread (&optional article)
12286   "Return a list of all articles in the current thread.
12287 If ARTICLE is non-nil, return all articles in the thread that starts
12288 with that article."
12289   (let* ((article (or article (gnus-summary-article-number)))
12290          (data (gnus-data-find-list article))
12291          (top-level (gnus-data-level (car data)))
12292          (top-subject
12293           (cond ((null gnus-thread-operation-ignore-subject)
12294                  (gnus-simplify-subject-re
12295                   (mail-header-subject (gnus-data-header (car data)))))
12296                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12297                  (gnus-simplify-subject-fuzzy
12298                   (mail-header-subject (gnus-data-header (car data)))))
12299                 (t nil)))
12300          articles)
12301     (if (not data)
12302         ()                              ; This article doesn't exist.
12303       (while data
12304         (and (or (not top-subject)
12305                  (string= top-subject
12306                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12307                               (gnus-simplify-subject-fuzzy
12308                                (mail-header-subject
12309                                 (gnus-data-header (car data))))
12310                             (gnus-simplify-subject-re
12311                              (mail-header-subject
12312                               (gnus-data-header (car data)))))))
12313              (setq articles (cons (gnus-data-number (car data)) articles)))
12314         (if (and (setq data (cdr data))
12315                  (> (gnus-data-level (car data)) top-level))
12316             ()
12317           (setq data nil)))
12318       ;; Return the list of articles.
12319       (nreverse articles))))
12320
12321 (defun gnus-summary-rethread-current ()
12322   "Rethread the thread the current article is part of."
12323   (interactive)
12324   (gnus-set-global-variables)
12325   (let* ((gnus-show-threads t)
12326          (article (gnus-summary-article-number))
12327          (id (mail-header-id (gnus-summary-article-header)))
12328          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12329     (unless id
12330       (error "No article on the current line"))
12331     (gnus-rebuild-thread id)
12332     (gnus-summary-goto-subject article)))
12333
12334 (defun gnus-summary-reparent-thread ()
12335   "Make current article child of the marked (or previous) article.
12336
12337 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12338 is non-nil or the Subject: of both articles are the same."
12339   (interactive)
12340   (or (not (gnus-group-read-only-p))
12341       (error "The current newsgroup does not support article editing."))
12342   (or (<= (length gnus-newsgroup-processable) 1)
12343       (error "No more than one article may be marked."))
12344   (save-window-excursion
12345     (let ((gnus-article-buffer " *reparent*")
12346           (current-article (gnus-summary-article-number))
12347           ; first grab the marked article, otherwise one line up.
12348           (parent-article (if (not (null gnus-newsgroup-processable))
12349                               (car gnus-newsgroup-processable)
12350                             (save-excursion
12351                               (if (eq (forward-line -1) 0)
12352                                   (gnus-summary-article-number)
12353                                 (error "Beginning of summary buffer."))))))
12354       (or (not (eq current-article parent-article))
12355           (error "An article may not be self-referential."))
12356       (let ((message-id (mail-header-id 
12357                          (gnus-summary-article-header parent-article))))
12358         (or (and message-id (not (equal message-id "")))
12359             (error "No message-id in desired parent."))
12360         (gnus-summary-select-article t t nil current-article)
12361         (set-buffer gnus-article-buffer)
12362         (setq buffer-read-only nil)
12363         (let ((buf (format "%s" (buffer-string))))
12364           (erase-buffer)
12365           (insert buf))
12366         (goto-char (point-min))
12367         (if (search-forward-regexp "^References: " nil t)
12368             (insert message-id " " )
12369           (insert "References: " message-id "\n"))
12370         (or (gnus-request-replace-article current-article
12371                                           (car gnus-article-current)
12372                                           gnus-article-buffer)
12373             (error "Couldn't replace article."))
12374         (set-buffer gnus-summary-buffer)
12375         (gnus-summary-unmark-all-processable)
12376         (gnus-summary-rethread-current)
12377         (message "Article %d is now the child of article %d."
12378                  current-article parent-article)))))
12379
12380 (defun gnus-summary-toggle-threads (&optional arg)
12381   "Toggle showing conversation threads.
12382 If ARG is positive number, turn showing conversation threads on."
12383   (interactive "P")
12384   (gnus-set-global-variables)
12385   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12386     (setq gnus-show-threads
12387           (if (null arg) (not gnus-show-threads)
12388             (> (prefix-numeric-value arg) 0)))
12389     (gnus-summary-prepare)
12390     (gnus-summary-goto-subject current)
12391     (gnus-summary-position-point)))
12392
12393 (defun gnus-summary-show-all-threads ()
12394   "Show all threads."
12395   (interactive)
12396   (gnus-set-global-variables)
12397   (save-excursion
12398     (let ((buffer-read-only nil))
12399       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12400   (gnus-summary-position-point))
12401
12402 (defun gnus-summary-show-thread ()
12403   "Show thread subtrees.
12404 Returns nil if no thread was there to be shown."
12405   (interactive)
12406   (gnus-set-global-variables)
12407   (let ((buffer-read-only nil)
12408         (orig (point))
12409         ;; first goto end then to beg, to have point at beg after let
12410         (end (progn (end-of-line) (point)))
12411         (beg (progn (beginning-of-line) (point))))
12412     (prog1
12413         ;; Any hidden lines here?
12414         (search-forward "\r" end t)
12415       (subst-char-in-region beg end ?\^M ?\n t)
12416       (goto-char orig)
12417       (gnus-summary-position-point))))
12418
12419 (defun gnus-summary-hide-all-threads ()
12420   "Hide all thread subtrees."
12421   (interactive)
12422   (gnus-set-global-variables)
12423   (save-excursion
12424     (goto-char (point-min))
12425     (gnus-summary-hide-thread)
12426     (while (zerop (gnus-summary-next-thread 1 t))
12427       (gnus-summary-hide-thread)))
12428   (gnus-summary-position-point))
12429
12430 (defun gnus-summary-hide-thread ()
12431   "Hide thread subtrees.
12432 Returns nil if no threads were there to be hidden."
12433   (interactive)
12434   (gnus-set-global-variables)
12435   (let ((buffer-read-only nil)
12436         (start (point))
12437         (article (gnus-summary-article-number)))
12438     (goto-char start)
12439     ;; Go forward until either the buffer ends or the subthread
12440     ;; ends.
12441     (when (and (not (eobp))
12442                (or (zerop (gnus-summary-next-thread 1 t))
12443                    (goto-char (point-max))))
12444       (prog1
12445           (if (and (> (point) start)
12446                    (search-backward "\n" start t))
12447               (progn
12448                 (subst-char-in-region start (point) ?\n ?\^M)
12449                 (gnus-summary-goto-subject article))
12450             (goto-char start)
12451             nil)
12452         ;;(gnus-summary-position-point)
12453         ))))
12454
12455 (defun gnus-summary-go-to-next-thread (&optional previous)
12456   "Go to the same level (or less) next thread.
12457 If PREVIOUS is non-nil, go to previous thread instead.
12458 Return the article number moved to, or nil if moving was impossible."
12459   (let ((level (gnus-summary-thread-level))
12460         (way (if previous -1 1))
12461         (beg (point)))
12462     (forward-line way)
12463     (while (and (not (eobp))
12464                 (< level (gnus-summary-thread-level)))
12465       (forward-line way))
12466     (if (eobp)
12467         (progn
12468           (goto-char beg)
12469           nil)
12470       (setq beg (point))
12471       (prog1
12472           (gnus-summary-article-number)
12473         (goto-char beg)))))
12474
12475 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12476   "Go to the same level (or less) next thread.
12477 If PREVIOUS is non-nil, go to previous thread instead.
12478 Return the article number moved to, or nil if moving was impossible."
12479   (if (and (eq gnus-summary-make-false-root 'dummy)
12480            (gnus-summary-article-intangible-p))
12481       (let ((beg (point)))
12482         (while (and (zerop (forward-line 1))
12483                     (not (gnus-summary-article-intangible-p))
12484                     (not (zerop (save-excursion 
12485                                   (gnus-summary-thread-level))))))
12486         (if (eobp)
12487             (progn
12488               (goto-char beg)
12489               nil)
12490           (point)))
12491     (let* ((level (gnus-summary-thread-level))
12492            (article (gnus-summary-article-number))
12493            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12494            oart)
12495       (while data
12496         (if (<= (gnus-data-level (car data)) level)
12497             (setq oart (gnus-data-number (car data))
12498                   data nil)
12499           (setq data (cdr data))))
12500       (and oart
12501            (gnus-summary-goto-subject oart)))))
12502
12503 (defun gnus-summary-next-thread (n &optional silent)
12504   "Go to the same level next N'th thread.
12505 If N is negative, search backward instead.
12506 Returns the difference between N and the number of skips actually
12507 done.
12508
12509 If SILENT, don't output messages."
12510   (interactive "p")
12511   (gnus-set-global-variables)
12512   (let ((backward (< n 0))
12513         (n (abs n))
12514         old dum int)
12515     (while (and (> n 0)
12516                 (gnus-summary-go-to-next-thread backward))
12517       (decf n))
12518     (unless silent 
12519       (gnus-summary-position-point))
12520     (when (and (not silent) (/= 0 n))
12521       (gnus-message 7 "No more threads"))
12522     n))
12523
12524 (defun gnus-summary-prev-thread (n)
12525   "Go to the same level previous N'th thread.
12526 Returns the difference between N and the number of skips actually
12527 done."
12528   (interactive "p")
12529   (gnus-set-global-variables)
12530   (gnus-summary-next-thread (- n)))
12531
12532 (defun gnus-summary-go-down-thread ()
12533   "Go down one level in the current thread."
12534   (let ((children (gnus-summary-article-children)))
12535     (and children
12536          (gnus-summary-goto-subject (car children)))))
12537
12538 (defun gnus-summary-go-up-thread ()
12539   "Go up one level in the current thread."
12540   (let ((parent (gnus-summary-article-parent)))
12541     (and parent
12542          (gnus-summary-goto-subject parent))))
12543
12544 (defun gnus-summary-down-thread (n)
12545   "Go down thread N steps.
12546 If N is negative, go up instead.
12547 Returns the difference between N and how many steps down that were
12548 taken."
12549   (interactive "p")
12550   (gnus-set-global-variables)
12551   (let ((up (< n 0))
12552         (n (abs n)))
12553     (while (and (> n 0)
12554                 (if up (gnus-summary-go-up-thread)
12555                   (gnus-summary-go-down-thread)))
12556       (setq n (1- n)))
12557     (gnus-summary-position-point)
12558     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12559     n))
12560
12561 (defun gnus-summary-up-thread (n)
12562   "Go up thread N steps.
12563 If N is negative, go up instead.
12564 Returns the difference between N and how many steps down that were
12565 taken."
12566   (interactive "p")
12567   (gnus-set-global-variables)
12568   (gnus-summary-down-thread (- n)))
12569
12570 (defun gnus-summary-top-thread ()
12571   "Go to the top of the thread."
12572   (interactive)
12573   (gnus-set-global-variables)
12574   (while (gnus-summary-go-up-thread))
12575   (gnus-summary-article-number))
12576
12577 (defun gnus-summary-kill-thread (&optional unmark)
12578   "Mark articles under current thread as read.
12579 If the prefix argument is positive, remove any kinds of marks.
12580 If the prefix argument is negative, tick articles instead."
12581   (interactive "P")
12582   (gnus-set-global-variables)
12583   (if unmark
12584       (setq unmark (prefix-numeric-value unmark)))
12585   (let ((articles (gnus-summary-articles-in-thread)))
12586     (save-excursion
12587       ;; Expand the thread.
12588       (gnus-summary-show-thread)
12589       ;; Mark all the articles.
12590       (while articles
12591         (gnus-summary-goto-subject (car articles))
12592         (cond ((null unmark)
12593                (gnus-summary-mark-article-as-read gnus-killed-mark))
12594               ((> unmark 0)
12595                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12596               (t
12597                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12598         (setq articles (cdr articles))))
12599     ;; Hide killed subtrees.
12600     (and (null unmark)
12601          gnus-thread-hide-killed
12602          (gnus-summary-hide-thread))
12603     ;; If marked as read, go to next unread subject.
12604     (if (null unmark)
12605         ;; Go to next unread subject.
12606         (gnus-summary-next-subject 1 t)))
12607   (gnus-set-mode-line 'summary))
12608
12609 ;; Summary sorting commands
12610
12611 (defun gnus-summary-sort-by-number (&optional reverse)
12612   "Sort summary buffer by article number.
12613 Argument REVERSE means reverse order."
12614   (interactive "P")
12615   (gnus-summary-sort 'number reverse))
12616
12617 (defun gnus-summary-sort-by-author (&optional reverse)
12618   "Sort summary buffer by author name alphabetically.
12619 If case-fold-search is non-nil, case of letters is ignored.
12620 Argument REVERSE means reverse order."
12621   (interactive "P")
12622   (gnus-summary-sort 'author reverse))
12623
12624 (defun gnus-summary-sort-by-subject (&optional reverse)
12625   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12626 If case-fold-search is non-nil, case of letters is ignored.
12627 Argument REVERSE means reverse order."
12628   (interactive "P")
12629   (gnus-summary-sort 'subject reverse))
12630
12631 (defun gnus-summary-sort-by-date (&optional reverse)
12632   "Sort summary buffer by date.
12633 Argument REVERSE means reverse order."
12634   (interactive "P")
12635   (gnus-summary-sort 'date reverse))
12636
12637 (defun gnus-summary-sort-by-score (&optional reverse)
12638   "Sort summary buffer by score.
12639 Argument REVERSE means reverse order."
12640   (interactive "P")
12641   (gnus-summary-sort 'score reverse))
12642
12643 (defun gnus-summary-sort (predicate reverse)
12644   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12645   (gnus-set-global-variables)
12646   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12647          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12648          (gnus-thread-sort-functions
12649           (list
12650            (if (not reverse)
12651                thread
12652              `(lambda (t1 t2)
12653                 (,thread t2 t1)))))
12654          (gnus-article-sort-functions
12655           (list
12656            (if (not reverse)
12657                article
12658              `(lambda (t1 t2)
12659                 (,article t2 t1)))))
12660          (buffer-read-only)
12661          (gnus-summary-prepare-hook nil))
12662     ;; We do the sorting by regenerating the threads.
12663     (gnus-summary-prepare)
12664     ;; Hide subthreads if needed.
12665     (when (and gnus-show-threads gnus-thread-hide-subtree)
12666       (gnus-summary-hide-all-threads)))
12667   ;; If in async mode, we send some info to the backend.
12668   (when gnus-newsgroup-async
12669     (gnus-request-asynchronous
12670      gnus-newsgroup-name gnus-newsgroup-data)))
12671
12672 (defun gnus-sortable-date (date)
12673   "Make sortable string by string-lessp from DATE.
12674 Timezone package is used."
12675   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12676          (year (aref date 0))
12677          (month (aref date 1))
12678          (day (aref date 2)))
12679     (timezone-make-sortable-date
12680      year month day
12681      (timezone-make-time-string
12682       (aref date 3) (aref date 4) (aref date 5)))))
12683
12684 ;; Summary saving commands.
12685
12686 (defun gnus-summary-save-article (&optional n not-saved)
12687   "Save the current article using the default saver function.
12688 If N is a positive number, save the N next articles.
12689 If N is a negative number, save the N previous articles.
12690 If N is nil and any articles have been marked with the process mark,
12691 save those articles instead.
12692 The variable `gnus-default-article-saver' specifies the saver function."
12693   (interactive "P")
12694   (gnus-set-global-variables)
12695   (let ((articles (gnus-summary-work-articles n))
12696         file header article)
12697     (while articles
12698       (setq header (gnus-summary-article-header
12699                     (setq article (pop articles))))
12700       (if (not (vectorp header))
12701           ;; This is a pseudo-article.
12702           (if (assq 'name header)
12703               (gnus-copy-file (cdr (assq 'name header)))
12704             (gnus-message 1 "Article %d is unsaveable" article))
12705         ;; This is a real article.
12706         (save-window-excursion
12707           (gnus-summary-select-article t nil nil article))
12708         (unless gnus-save-all-headers
12709           ;; Remove headers accoring to `gnus-saved-headers'.
12710           (let ((gnus-visible-headers
12711                  (or gnus-saved-headers gnus-visible-headers)))
12712             (gnus-article-hide-headers nil t)))
12713         ;; Remove any X-Gnus lines.
12714         (save-excursion
12715           (set-buffer gnus-article-buffer)
12716           (save-restriction
12717             (let ((buffer-read-only nil))
12718               (nnheader-narrow-to-headers)
12719               (while (re-search-forward "^X-Gnus" nil t)
12720                 (gnus-delete-line)))))
12721         (save-window-excursion
12722           (if (not gnus-default-article-saver)
12723               (error "No default saver is defined.")
12724             (setq file (funcall
12725                         gnus-default-article-saver
12726                         (cond
12727                          ((not gnus-prompt-before-saving)
12728                           'default)
12729                          ((eq gnus-prompt-before-saving 'always)
12730                           nil)
12731                          (t file))))))
12732         (gnus-summary-remove-process-mark article)
12733         (unless not-saved
12734           (gnus-summary-set-saved-mark article))))
12735     (gnus-summary-position-point)
12736     n))
12737
12738 (defun gnus-summary-pipe-output (&optional arg)
12739   "Pipe the current article to a subprocess.
12740 If N is a positive number, pipe the N next articles.
12741 If N is a negative number, pipe the N previous articles.
12742 If N is nil and any articles have been marked with the process mark,
12743 pipe those articles instead."
12744   (interactive "P")
12745   (gnus-set-global-variables)
12746   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12747     (gnus-summary-save-article arg t))
12748   (gnus-configure-windows 'pipe))
12749
12750 (defun gnus-summary-save-article-mail (&optional arg)
12751   "Append the current article to an mail file.
12752 If N is a positive number, save the N next articles.
12753 If N is a negative number, save the N previous articles.
12754 If N is nil and any articles have been marked with the process mark,
12755 save those articles instead."
12756   (interactive "P")
12757   (gnus-set-global-variables)
12758   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12759     (gnus-summary-save-article arg)))
12760
12761 (defun gnus-summary-save-article-rmail (&optional arg)
12762   "Append the current article to an rmail file.
12763 If N is a positive number, save the N next articles.
12764 If N is a negative number, save the N previous articles.
12765 If N is nil and any articles have been marked with the process mark,
12766 save those articles instead."
12767   (interactive "P")
12768   (gnus-set-global-variables)
12769   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12770     (gnus-summary-save-article arg)))
12771
12772 (defun gnus-summary-save-article-file (&optional arg)
12773   "Append the current article to a file.
12774 If N is a positive number, save the N next articles.
12775 If N is a negative number, save the N previous articles.
12776 If N is nil and any articles have been marked with the process mark,
12777 save those articles instead."
12778   (interactive "P")
12779   (gnus-set-global-variables)
12780   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12781     (gnus-summary-save-article arg)))
12782
12783 (defun gnus-summary-save-article-body-file (&optional arg)
12784   "Append the current article body to a file.
12785 If N is a positive number, save the N next articles.
12786 If N is a negative number, save the N previous articles.
12787 If N is nil and any articles have been marked with the process mark,
12788 save those articles instead."
12789   (interactive "P")
12790   (gnus-set-global-variables)
12791   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12792     (gnus-summary-save-article arg)))
12793
12794 (defun gnus-get-split-value (methods)
12795   "Return a value based on the split METHODS."
12796   (let (split-name method result match)
12797     (when methods
12798       (save-excursion
12799         (set-buffer gnus-original-article-buffer)
12800         (save-restriction
12801           (nnheader-narrow-to-headers)
12802           (while methods
12803             (goto-char (point-min))
12804             (setq method (pop methods))
12805             (setq match (car method))
12806             (when (cond
12807                    ((stringp match)
12808                     ;; Regular expression.
12809                     (condition-case ()
12810                         (re-search-forward match nil t)
12811                       (error nil)))
12812                    ((gnus-functionp match)
12813                     ;; Function.
12814                     (save-restriction
12815                       (widen)
12816                       (setq result (funcall match gnus-newsgroup-name))))
12817                    ((consp match)
12818                     ;; Form.
12819                     (save-restriction
12820                       (widen)
12821                       (setq result (eval match)))))
12822               (setq split-name (append (cdr method) split-name))
12823               (cond ((stringp result)
12824                      (push result split-name))
12825                     ((consp result)
12826                      (setq split-name (append result split-name)))))))))
12827     split-name))
12828
12829 (defun gnus-read-move-group-name (prompt default articles prefix)
12830   "Read a group name."
12831   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12832          (prom
12833           (format "Where do you want to %s %s? "
12834                   prompt
12835                   (if (> (length articles) 1)
12836                       (format "these %d articles" (length articles))
12837                     "this article")))
12838          (to-newsgroup
12839           (cond
12840            ((null split-name)
12841             (completing-read
12842              (concat prom
12843                      (if default
12844                          (format "(default %s) " default)
12845                        ""))
12846              gnus-active-hashtb nil nil prefix))
12847            ((= 1 (length split-name))
12848             (completing-read prom gnus-active-hashtb
12849                              nil nil (cons (car split-name) 0)))
12850            (t
12851             (completing-read
12852              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12853
12854     (when to-newsgroup
12855       (if (or (string= to-newsgroup "")
12856               (string= to-newsgroup prefix))
12857           (setq to-newsgroup (or default "")))
12858       (or (gnus-active to-newsgroup)
12859           (gnus-activate-group to-newsgroup)
12860           (error "No such group: %s" to-newsgroup)))
12861     to-newsgroup))
12862
12863 (defun gnus-read-save-file-name (prompt default-name)
12864   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12865          (file
12866           ;; Let the split methods have their say.
12867           (cond
12868            ;; No split name was found.
12869            ((null split-name)
12870             (read-file-name
12871              (concat prompt " (default "
12872                      (file-name-nondirectory default-name) ") ")
12873              (file-name-directory default-name)
12874              default-name))
12875            ;; A single split name was found
12876            ((= 1 (length split-name))
12877             (read-file-name
12878              (concat prompt " (default " (car split-name) ") ")
12879              gnus-article-save-directory
12880              (concat gnus-article-save-directory (car split-name))))
12881            ;; A list of splits was found.
12882            (t
12883             (setq split-name (mapcar (lambda (el) (list el))
12884                                      (nreverse split-name)))
12885             (let ((result (completing-read
12886                            (concat prompt " ") split-name nil nil)))
12887               (concat gnus-article-save-directory
12888                       (if (string= result "")
12889                           (caar split-name)
12890                         result)))))))
12891     ;; If we have read a directory, we append the default file name.
12892     (when (file-directory-p file)
12893       (setq file (concat (file-name-as-directory file)
12894                          (file-name-nondirectory default-name))))
12895     ;; Possibly translate some charaters.
12896     (nnheader-translate-file-chars file)))
12897
12898 (defun gnus-article-archive-name (group)
12899   "Return the first instance of an \"Archive-name\" in the current buffer."
12900   (let ((case-fold-search t))
12901     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12902       (match-string 1))))
12903
12904 (defun gnus-summary-save-in-rmail (&optional filename)
12905   "Append this article to Rmail file.
12906 Optional argument FILENAME specifies file name.
12907 Directory to save to is default to `gnus-article-save-directory' which
12908 is initialized from the SAVEDIR environment variable."
12909   (interactive)
12910   (gnus-set-global-variables)
12911   (let ((default-name
12912           (funcall gnus-rmail-save-name gnus-newsgroup-name
12913                    gnus-current-headers gnus-newsgroup-last-rmail)))
12914     (setq filename
12915           (cond ((eq filename 'default)
12916                  default-name)
12917                 (filename filename)
12918                 (t (gnus-read-save-file-name
12919                     "Save in rmail file:" default-name))))
12920     (gnus-make-directory (file-name-directory filename))
12921     (gnus-eval-in-buffer-window
12922      gnus-original-article-buffer
12923      (save-excursion
12924        (save-restriction
12925          (widen)
12926          (gnus-output-to-rmail filename))))
12927     ;; Remember the directory name to save articles
12928     (setq gnus-newsgroup-last-rmail filename)))
12929
12930 (defun gnus-summary-save-in-mail (&optional filename)
12931   "Append this article to Unix mail file.
12932 Optional argument FILENAME specifies file name.
12933 Directory to save to is default to `gnus-article-save-directory' which
12934 is initialized from the SAVEDIR environment variable."
12935   (interactive)
12936   (gnus-set-global-variables)
12937   (let ((default-name
12938           (funcall gnus-mail-save-name gnus-newsgroup-name
12939                    gnus-current-headers gnus-newsgroup-last-mail)))
12940     (setq filename
12941           (cond ((eq filename 'default)
12942                  default-name)
12943                 (filename filename)
12944                 (t (gnus-read-save-file-name
12945                     "Save in Unix mail file:" default-name))))
12946     (setq filename
12947           (expand-file-name filename
12948                             (and default-name
12949                                  (file-name-directory default-name))))
12950     (gnus-make-directory (file-name-directory filename))
12951     (gnus-eval-in-buffer-window
12952      gnus-original-article-buffer
12953      (save-excursion
12954        (save-restriction
12955          (widen)
12956          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12957              (gnus-output-to-rmail filename)
12958            (let ((mail-use-rfc822 t))
12959              (rmail-output filename 1 t t))))))
12960     ;; Remember the directory name to save articles.
12961     (setq gnus-newsgroup-last-mail filename)))
12962
12963 (defun gnus-summary-save-in-file (&optional filename)
12964   "Append this article to file.
12965 Optional argument FILENAME specifies file name.
12966 Directory to save to is default to `gnus-article-save-directory' which
12967 is initialized from the SAVEDIR environment variable."
12968   (interactive)
12969   (gnus-set-global-variables)
12970   (let ((default-name
12971           (funcall gnus-file-save-name gnus-newsgroup-name
12972                    gnus-current-headers gnus-newsgroup-last-file)))
12973     (setq filename
12974           (cond ((eq filename 'default)
12975                  default-name)
12976                 (filename filename)
12977                 (t (gnus-read-save-file-name
12978                     "Save in file:" default-name))))
12979     (gnus-make-directory (file-name-directory filename))
12980     (gnus-eval-in-buffer-window
12981      gnus-original-article-buffer
12982      (save-excursion
12983        (save-restriction
12984          (widen)
12985          (gnus-output-to-file filename))))
12986     ;; Remember the directory name to save articles.
12987     (setq gnus-newsgroup-last-file filename)))
12988
12989 (defun gnus-summary-save-body-in-file (&optional filename)
12990   "Append this article body to a file.
12991 Optional argument FILENAME specifies file name.
12992 The directory to save in defaults to `gnus-article-save-directory' which
12993 is initialized from the SAVEDIR environment variable."
12994   (interactive)
12995   (gnus-set-global-variables)
12996   (let ((default-name
12997           (funcall gnus-file-save-name gnus-newsgroup-name
12998                    gnus-current-headers gnus-newsgroup-last-file)))
12999     (setq filename
13000           (cond ((eq filename 'default)
13001                  default-name)
13002                 (filename filename)
13003                 (t (gnus-read-save-file-name
13004                     "Save body in file:" default-name))))
13005     (gnus-make-directory (file-name-directory filename))
13006     (gnus-eval-in-buffer-window
13007      gnus-article-buffer
13008      (save-excursion
13009        (save-restriction
13010          (widen)
13011          (goto-char (point-min))
13012          (and (search-forward "\n\n" nil t)
13013               (narrow-to-region (point) (point-max)))
13014          (gnus-output-to-file filename))))
13015     ;; Remember the directory name to save articles.
13016     (setq gnus-newsgroup-last-file filename)))
13017
13018 (defun gnus-summary-save-in-pipe (&optional command)
13019   "Pipe this article to subprocess."
13020   (interactive)
13021   (gnus-set-global-variables)
13022   (setq command
13023         (cond ((eq command 'default)
13024                gnus-last-shell-command)
13025               (command command)
13026               (t (read-string "Shell command on article: "
13027                               gnus-last-shell-command))))
13028   (if (string-equal command "")
13029       (setq command gnus-last-shell-command))
13030   (gnus-eval-in-buffer-window
13031    gnus-article-buffer
13032    (save-restriction
13033      (widen)
13034      (shell-command-on-region (point-min) (point-max) command nil)))
13035   (setq gnus-last-shell-command command))
13036
13037 ;; Summary extract commands
13038
13039 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13040   (let ((buffer-read-only nil)
13041         (article (gnus-summary-article-number))
13042         after-article b e)
13043     (or (gnus-summary-goto-subject article)
13044         (error (format "No such article: %d" article)))
13045     (gnus-summary-position-point)
13046     ;; If all commands are to be bunched up on one line, we collect
13047     ;; them here.
13048     (if gnus-view-pseudos-separately
13049         ()
13050       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13051             files action)
13052         (while ps
13053           (setq action (cdr (assq 'action (car ps))))
13054           (setq files (list (cdr (assq 'name (car ps)))))
13055           (while (and ps (cdr ps)
13056                       (string= (or action "1")
13057                                (or (cdr (assq 'action (cadr ps))) "2")))
13058             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13059             (setcdr ps (cddr ps)))
13060           (if (not files)
13061               ()
13062             (if (not (string-match "%s" action))
13063                 (setq files (cons " " files)))
13064             (setq files (cons " " files))
13065             (and (assq 'execute (car ps))
13066                  (setcdr (assq 'execute (car ps))
13067                          (funcall (if (string-match "%s" action)
13068                                       'format 'concat)
13069                                   action
13070                                   (mapconcat (lambda (f) f) files " ")))))
13071           (setq ps (cdr ps)))))
13072     (if (and gnus-view-pseudos (not not-view))
13073         (while pslist
13074           (and (assq 'execute (car pslist))
13075                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13076                                      (eq gnus-view-pseudos 'not-confirm)))
13077           (setq pslist (cdr pslist)))
13078       (save-excursion
13079         (while pslist
13080           (setq after-article (or (cdr (assq 'article (car pslist)))
13081                                   (gnus-summary-article-number)))
13082           (gnus-summary-goto-subject after-article)
13083           (forward-line 1)
13084           (setq b (point))
13085           (insert "    " (file-name-nondirectory
13086                                 (cdr (assq 'name (car pslist))))
13087                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13088           (setq e (point))
13089           (forward-line -1)             ; back to `b'
13090           (add-text-properties
13091            b e (list 'gnus-number gnus-reffed-article-number
13092                      gnus-mouse-face-prop gnus-mouse-face))
13093           (gnus-data-enter
13094            after-article gnus-reffed-article-number
13095            gnus-unread-mark b (car pslist) 0 (- e b))
13096           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13097           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13098           (setq pslist (cdr pslist)))))))
13099
13100 (defun gnus-pseudos< (p1 p2)
13101   (let ((c1 (cdr (assq 'action p1)))
13102         (c2 (cdr (assq 'action p2))))
13103     (and c1 c2 (string< c1 c2))))
13104
13105 (defun gnus-request-pseudo-article (props)
13106   (cond ((assq 'execute props)
13107          (gnus-execute-command (cdr (assq 'execute props)))))
13108   (let ((gnus-current-article (gnus-summary-article-number)))
13109     (run-hooks 'gnus-mark-article-hook)))
13110
13111 (defun gnus-execute-command (command &optional automatic)
13112   (save-excursion
13113     (gnus-article-setup-buffer)
13114     (set-buffer gnus-article-buffer)
13115     (let ((command (if automatic command (read-string "Command: " command)))
13116           (buffer-read-only nil))
13117       (erase-buffer)
13118       (insert "$ " command "\n\n")
13119       (if gnus-view-pseudo-asynchronously
13120           (start-process "gnus-execute" nil "sh" "-c" command)
13121         (call-process "sh" nil t nil "-c" command)))))
13122
13123 (defun gnus-copy-file (file &optional to)
13124   "Copy FILE to TO."
13125   (interactive
13126    (list (read-file-name "Copy file: " default-directory)
13127          (read-file-name "Copy file to: " default-directory)))
13128   (gnus-set-global-variables)
13129   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13130   (and (file-directory-p to)
13131        (setq to (concat (file-name-as-directory to)
13132                         (file-name-nondirectory file))))
13133   (copy-file file to))
13134
13135 ;; Summary kill commands.
13136
13137 (defun gnus-summary-edit-global-kill (article)
13138   "Edit the \"global\" kill file."
13139   (interactive (list (gnus-summary-article-number)))
13140   (gnus-set-global-variables)
13141   (gnus-group-edit-global-kill article))
13142
13143 (defun gnus-summary-edit-local-kill ()
13144   "Edit a local kill file applied to the current newsgroup."
13145   (interactive)
13146   (gnus-set-global-variables)
13147   (setq gnus-current-headers (gnus-summary-article-header))
13148   (gnus-set-global-variables)
13149   (gnus-group-edit-local-kill
13150    (gnus-summary-article-number) gnus-newsgroup-name))
13151
13152 \f
13153 ;;;
13154 ;;; Gnus article mode
13155 ;;;
13156
13157 (put 'gnus-article-mode 'mode-class 'special)
13158
13159 (if gnus-article-mode-map
13160     nil
13161   (setq gnus-article-mode-map (make-keymap))
13162   (suppress-keymap gnus-article-mode-map)
13163
13164   (gnus-define-keys gnus-article-mode-map
13165     " " gnus-article-goto-next-page
13166     "\177" gnus-article-goto-prev-page
13167     [delete] gnus-article-goto-prev-page
13168     "\C-c^" gnus-article-refer-article
13169     "h" gnus-article-show-summary
13170     "s" gnus-article-show-summary
13171     "\C-c\C-m" gnus-article-mail
13172     "?" gnus-article-describe-briefly
13173     gnus-mouse-2 gnus-article-push-button
13174     "\r" gnus-article-press-button
13175     "\t" gnus-article-next-button
13176     "\M-\t" gnus-article-prev-button
13177     "\C-c\C-b" gnus-bug)
13178
13179   (substitute-key-definition
13180    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13181
13182 (defun gnus-article-mode ()
13183   "Major mode for displaying an article.
13184
13185 All normal editing commands are switched off.
13186
13187 The following commands are available:
13188
13189 \\<gnus-article-mode-map>
13190 \\[gnus-article-next-page]\t Scroll the article one page forwards
13191 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13192 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13193 \\[gnus-article-show-summary]\t Display the summary buffer
13194 \\[gnus-article-mail]\t Send a reply to the address near point
13195 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13196 \\[gnus-info-find-node]\t Go to the Gnus info node"
13197   (interactive)
13198   (when (and menu-bar-mode
13199              (gnus-visual-p 'article-menu 'menu))
13200     (gnus-article-make-menu-bar))
13201   (kill-all-local-variables)
13202   (gnus-simplify-mode-line)
13203   (setq mode-name "Article")
13204   (setq major-mode 'gnus-article-mode)
13205   (make-local-variable 'minor-mode-alist)
13206   (or (assq 'gnus-show-mime minor-mode-alist)
13207       (setq minor-mode-alist
13208             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13209   (use-local-map gnus-article-mode-map)
13210   (make-local-variable 'page-delimiter)
13211   (setq page-delimiter gnus-page-delimiter)
13212   (buffer-disable-undo (current-buffer))
13213   (setq buffer-read-only t)             ;Disable modification
13214   (run-hooks 'gnus-article-mode-hook))
13215
13216 (defun gnus-article-setup-buffer ()
13217   "Initialize the article buffer."
13218   (let* ((name (if gnus-single-article-buffer "*Article*"
13219                  (concat "*Article " gnus-newsgroup-name "*")))
13220          (original
13221           (progn (string-match "\\*Article" name)
13222                  (concat " *Original Article"
13223                          (substring name (match-end 0))))))
13224     (setq gnus-article-buffer name)
13225     (setq gnus-original-article-buffer original)
13226     ;; This might be a variable local to the summary buffer.
13227     (unless gnus-single-article-buffer
13228       (save-excursion
13229         (set-buffer gnus-summary-buffer)
13230         (setq gnus-article-buffer name)
13231         (setq gnus-original-article-buffer original)
13232         (gnus-set-global-variables))
13233       (make-local-variable 'gnus-summary-buffer))
13234     ;; Init original article buffer.
13235     (save-excursion
13236       (set-buffer (get-buffer-create gnus-original-article-buffer))
13237       (buffer-disable-undo (current-buffer))
13238       (setq major-mode 'gnus-original-article-mode)
13239       (make-local-variable 'gnus-original-article))
13240     (if (get-buffer name)
13241         (save-excursion
13242           (set-buffer name)
13243           (buffer-disable-undo (current-buffer))
13244           (setq buffer-read-only t)
13245           (gnus-add-current-to-buffer-list)
13246           (or (eq major-mode 'gnus-article-mode)
13247               (gnus-article-mode))
13248           (current-buffer))
13249       (save-excursion
13250         (set-buffer (get-buffer-create name))
13251         (gnus-add-current-to-buffer-list)
13252         (gnus-article-mode)
13253         (current-buffer)))))
13254
13255 ;; Set article window start at LINE, where LINE is the number of lines
13256 ;; from the head of the article.
13257 (defun gnus-article-set-window-start (&optional line)
13258   (set-window-start
13259    (get-buffer-window gnus-article-buffer)
13260    (save-excursion
13261      (set-buffer gnus-article-buffer)
13262      (goto-char (point-min))
13263      (if (not line)
13264          (point-min)
13265        (gnus-message 6 "Moved to bookmark")
13266        (search-forward "\n\n" nil t)
13267        (forward-line line)
13268        (point)))))
13269
13270 (defun gnus-kill-all-overlays ()
13271   "Delete all overlays in the current buffer."
13272   (when (fboundp 'overlay-lists)
13273     (let* ((overlayss (overlay-lists))
13274            (buffer-read-only nil)
13275            (overlays (nconc (car overlayss) (cdr overlayss))))
13276       (while overlays
13277         (delete-overlay (pop overlays))))))
13278
13279 (defun gnus-request-article-this-buffer (article group)
13280   "Get an article and insert it into this buffer."
13281   (let (do-update-line)
13282     (prog1
13283         (save-excursion
13284           (erase-buffer)
13285           (gnus-kill-all-overlays)
13286           (setq group (or group gnus-newsgroup-name))
13287
13288           ;; Open server if it has closed.
13289           (gnus-check-server (gnus-find-method-for-group group))
13290
13291           ;; Using `gnus-request-article' directly will insert the article into
13292           ;; `nntp-server-buffer' - so we'll save some time by not having to
13293           ;; copy it from the server buffer into the article buffer.
13294
13295           ;; We only request an article by message-id when we do not have the
13296           ;; headers for it, so we'll have to get those.
13297           (when (stringp article)
13298             (let ((gnus-override-method gnus-refer-article-method))
13299               (gnus-read-header article)))
13300
13301           ;; If the article number is negative, that means that this article
13302           ;; doesn't belong in this newsgroup (possibly), so we find its
13303           ;; message-id and request it by id instead of number.
13304           (when (and (numberp article)
13305                      gnus-summary-buffer
13306                      (get-buffer gnus-summary-buffer)
13307                      (buffer-name (get-buffer gnus-summary-buffer)))
13308             (save-excursion
13309               (set-buffer gnus-summary-buffer)
13310               (let ((header (gnus-summary-article-header article)))
13311                 (if (< article 0)
13312                     (cond 
13313                      ((memq article gnus-newsgroup-sparse)
13314                       ;; This is a sparse gap article.
13315                       (setq do-update-line article)
13316                       (setq article (mail-header-id header))
13317                       (let ((gnus-override-method gnus-refer-article-method))
13318                         (gnus-read-header article)))
13319                      ((vectorp header)
13320                       ;; It's a real article.
13321                       (setq article (mail-header-id header)))
13322                      (t
13323                       ;; It is an extracted pseudo-article.
13324                       (setq article 'pseudo)
13325                       (gnus-request-pseudo-article header))))
13326                 
13327                 (let ((method (gnus-find-method-for-group 
13328                                gnus-newsgroup-name)))
13329                   (if (not (eq (car method) 'nneething))
13330                       ()
13331                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13332                                        (mail-header-subject header))))
13333                       (if (file-directory-p dir)
13334                           (progn
13335                             (setq article 'nneething)
13336                             (gnus-group-enter-directory dir)))))))))
13337
13338           (cond
13339            ;; We first check `gnus-original-article-buffer'.
13340            ((and (get-buffer gnus-original-article-buffer)
13341                  (save-excursion
13342                    (set-buffer gnus-original-article-buffer)
13343                    (and (equal (car gnus-original-article) group)
13344                         (eq (cdr gnus-original-article) article))))
13345             (insert-buffer-substring gnus-original-article-buffer)
13346             'article)
13347            ;; Check the backlog.
13348            ((and gnus-keep-backlog
13349                  (gnus-backlog-request-article group article (current-buffer)))
13350             'article)
13351            ;; Check the cache.
13352            ((and gnus-use-cache
13353                  (numberp article)
13354                  (gnus-cache-request-article article group))
13355             'article)
13356            ;; Get the article and put into the article buffer.
13357            ((or (stringp article) (numberp article))
13358             (let ((gnus-override-method
13359                    (and (stringp article) gnus-refer-article-method))
13360                   (buffer-read-only nil))
13361               (erase-buffer)
13362               (gnus-kill-all-overlays)
13363               (if (gnus-request-article article group (current-buffer))
13364                   (progn
13365                     (and gnus-keep-backlog
13366                          (numberp article)
13367                          (gnus-backlog-enter-article
13368                           group article (current-buffer)))
13369                     'article))))
13370            ;; It was a pseudo.
13371            (t article)))
13372
13373       ;; Take the article from the original article buffer
13374       ;; and place it in the buffer it's supposed to be in.
13375       (when (and (get-buffer gnus-article-buffer)
13376                  (equal (buffer-name (current-buffer))
13377                         (buffer-name (get-buffer gnus-article-buffer))))
13378         (save-excursion
13379           (if (get-buffer gnus-original-article-buffer)
13380               (set-buffer (get-buffer gnus-original-article-buffer))
13381             (set-buffer (get-buffer-create gnus-original-article-buffer))
13382             (buffer-disable-undo (current-buffer))
13383             (setq major-mode 'gnus-original-article-mode)
13384             (setq buffer-read-only t)
13385             (gnus-add-current-to-buffer-list))
13386           (let (buffer-read-only)
13387             (erase-buffer)
13388             (insert-buffer-substring gnus-article-buffer))
13389           (setq gnus-original-article (cons group article))))
13390     
13391       ;; Update sparse articles.
13392       (when do-update-line
13393         (save-excursion
13394           (set-buffer gnus-summary-buffer)
13395           (gnus-summary-update-article do-update-line)
13396           (gnus-summary-goto-subject do-update-line)
13397           (set-window-point (get-buffer-window (current-buffer) t)
13398                             (point)))))))
13399
13400 (defun gnus-read-header (id &optional header)
13401   "Read the headers of article ID and enter them into the Gnus system."
13402   (let ((group gnus-newsgroup-name)
13403         where)
13404     ;; First we check to see whether the header in question is already
13405     ;; fetched.
13406     (if (stringp id)
13407         ;; This is a Message-ID.
13408         (setq header (or header (gnus-id-to-header id)))
13409       ;; This is an article number.
13410       (setq header (or header (gnus-summary-article-header id))))
13411     (if (and header
13412              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13413         ;; We have found the header.
13414         header
13415       ;; We have to really fetch the header to this article.
13416       (when (setq where
13417                   (if (gnus-check-backend-function 'request-head group)
13418                       (gnus-request-head id group)
13419                     (gnus-request-article id group)))
13420         (save-excursion
13421           (set-buffer nntp-server-buffer)
13422           (and (search-forward "\n\n" nil t)
13423                (delete-region (1- (point)) (point-max)))
13424           (goto-char (point-max))
13425           (insert ".\n")
13426           (goto-char (point-min))
13427           (insert "211 ")
13428           (princ (cond
13429                   ((numberp id) id)
13430                   ((cdr where) (cdr where))
13431                   (header (mail-header-number header))
13432                   (t gnus-reffed-article-number))
13433                  (current-buffer))
13434           (insert " Article retrieved.\n"))
13435         ;(when (and header
13436         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13437         ;  (setcar (gnus-id-to-thread id) nil))
13438         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13439             ()                          ; Malformed head.
13440           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13441             (if (and (stringp id)
13442                      (not (string= (gnus-group-real-name group)
13443                                    (car where))))
13444                 ;; If we fetched by Message-ID and the article came
13445                 ;; from a different group, we fudge some bogus article
13446                 ;; numbers for this article.
13447                 (mail-header-set-number header gnus-reffed-article-number))
13448             (decf gnus-reffed-article-number)
13449             (push header gnus-newsgroup-headers)
13450             (setq gnus-current-headers header)
13451             (push (mail-header-number header) gnus-newsgroup-limit))
13452           header)))))
13453
13454 (defun gnus-article-prepare (article &optional all-headers header)
13455   "Prepare ARTICLE in article mode buffer.
13456 ARTICLE should either be an article number or a Message-ID.
13457 If ARTICLE is an id, HEADER should be the article headers.
13458 If ALL-HEADERS is non-nil, no headers are hidden."
13459   (save-excursion
13460     ;; Make sure we start in a summary buffer.
13461     (unless (eq major-mode 'gnus-summary-mode)
13462       (set-buffer gnus-summary-buffer))
13463     (setq gnus-summary-buffer (current-buffer))
13464     ;; Make sure the connection to the server is alive.
13465     (unless (gnus-server-opened
13466              (gnus-find-method-for-group gnus-newsgroup-name))
13467       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13468       (gnus-request-group gnus-newsgroup-name t))
13469     (let* ((article (if header (mail-header-number header) article))
13470            (summary-buffer (current-buffer))
13471            (internal-hook gnus-article-internal-prepare-hook)
13472            (group gnus-newsgroup-name)
13473            result)
13474       (save-excursion
13475         (gnus-article-setup-buffer)
13476         (set-buffer gnus-article-buffer)
13477         ;; Deactivate active regions.
13478         (when (and (boundp 'transient-mark-mode)
13479                    transient-mark-mode)
13480           (setq mark-active nil))
13481         (if (not (setq result (let ((buffer-read-only nil))
13482                                 (gnus-request-article-this-buffer
13483                                  article group))))
13484             ;; There is no such article.
13485             (save-excursion
13486               (when (and (numberp article)
13487                          (not (memq article gnus-newsgroup-sparse)))
13488                 (setq gnus-article-current
13489                       (cons gnus-newsgroup-name article))
13490                 (set-buffer gnus-summary-buffer)
13491                 (setq gnus-current-article article)
13492                 (gnus-summary-mark-article article gnus-canceled-mark))
13493               (unless (memq article gnus-newsgroup-sparse)
13494                 (gnus-message
13495                  1 "No such article (may have expired or been canceled)")
13496                 (ding)
13497                 nil))
13498           (if (or (eq result 'pseudo) (eq result 'nneething))
13499               (progn
13500                 (save-excursion
13501                   (set-buffer summary-buffer)
13502                   (setq gnus-last-article gnus-current-article
13503                         gnus-newsgroup-history (cons gnus-current-article
13504                                                      gnus-newsgroup-history)
13505                         gnus-current-article 0
13506                         gnus-current-headers nil
13507                         gnus-article-current nil)
13508                   (if (eq result 'nneething)
13509                       (gnus-configure-windows 'summary)
13510                     (gnus-configure-windows 'article))
13511                   (gnus-set-global-variables))
13512                 (gnus-set-mode-line 'article))
13513             ;; The result from the `request' was an actual article -
13514             ;; or at least some text that is now displayed in the
13515             ;; article buffer.
13516             (if (and (numberp article)
13517                      (not (eq article gnus-current-article)))
13518                 ;; Seems like a new article has been selected.
13519                 ;; `gnus-current-article' must be an article number.
13520                 (save-excursion
13521                   (set-buffer summary-buffer)
13522                   (setq gnus-last-article gnus-current-article
13523                         gnus-newsgroup-history (cons gnus-current-article
13524                                                      gnus-newsgroup-history)
13525                         gnus-current-article article
13526                         gnus-current-headers
13527                         (gnus-summary-article-header gnus-current-article)
13528                         gnus-article-current
13529                         (cons gnus-newsgroup-name gnus-current-article))
13530                   (unless (vectorp gnus-current-headers)
13531                     (setq gnus-current-headers nil))
13532                   (gnus-summary-show-thread)
13533                   (run-hooks 'gnus-mark-article-hook)
13534                   (gnus-set-mode-line 'summary)
13535                   (and (gnus-visual-p 'article-highlight 'highlight)
13536                        (run-hooks 'gnus-visual-mark-article-hook))
13537                   ;; Set the global newsgroup variables here.
13538                   ;; Suggested by Jim Sisolak
13539                   ;; <sisolak@trans4.neep.wisc.edu>.
13540                   (gnus-set-global-variables)
13541                   (setq gnus-have-all-headers
13542                         (or all-headers gnus-show-all-headers))
13543                   (and gnus-use-cache
13544                        (vectorp (gnus-summary-article-header article))
13545                        (gnus-cache-possibly-enter-article
13546                         group article
13547                         (gnus-summary-article-header article)
13548                         (memq article gnus-newsgroup-marked)
13549                         (memq article gnus-newsgroup-dormant)
13550                         (memq article gnus-newsgroup-unreads)))))
13551             ;; Hooks for getting information from the article.
13552             ;; This hook must be called before being narrowed.
13553             (let (buffer-read-only)
13554               (run-hooks 'internal-hook)
13555               (run-hooks 'gnus-article-prepare-hook)
13556               ;; Decode MIME message.
13557               (if gnus-show-mime
13558                   (if (or (not gnus-strict-mime)
13559                           (gnus-fetch-field "Mime-Version"))
13560                       (funcall gnus-show-mime-method)
13561                     (funcall gnus-decode-encoded-word-method)))
13562               ;; Perform the article display hooks.
13563               (run-hooks 'gnus-article-display-hook))
13564             ;; Do page break.
13565             (goto-char (point-min))
13566             (and gnus-break-pages (gnus-narrow-to-page))
13567             (gnus-set-mode-line 'article)
13568             (gnus-configure-windows 'article)
13569             (goto-char (point-min))
13570             t))))))
13571
13572 (defun gnus-article-show-all-headers ()
13573   "Show all article headers in article mode buffer."
13574   (save-excursion
13575     (gnus-article-setup-buffer)
13576     (set-buffer gnus-article-buffer)
13577     (let ((buffer-read-only nil))
13578       (gnus-unhide-text (point-min) (point-max)))))
13579
13580 (defun gnus-article-hide-headers-if-wanted ()
13581   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13582 Provided for backwards compatibility."
13583   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13584       gnus-inhibit-hiding
13585       (gnus-article-hide-headers)))
13586
13587 (defun gnus-article-hide-headers (&optional arg delete)
13588   "Toggle whether to hide unwanted headers and possibly sort them as well.
13589 If given a negative prefix, always show; if given a positive prefix,
13590 always hide."
13591   (interactive "P")
13592   (unless (gnus-article-check-hidden-text 'headers arg)
13593     ;; This function might be inhibited.
13594     (unless gnus-inhibit-hiding
13595       (save-excursion
13596         (set-buffer gnus-article-buffer)
13597         (save-restriction
13598           (let ((buffer-read-only nil)
13599                 (props (nconc (list 'gnus-type 'headers)
13600                               gnus-hidden-properties))
13601                 (ignored (when (not (stringp gnus-visible-headers))
13602                            (cond ((stringp gnus-ignored-headers)
13603                                   gnus-ignored-headers)
13604                                  ((listp gnus-ignored-headers)
13605                                   (mapconcat 'identity gnus-ignored-headers
13606                                              "\\|")))))
13607                 (visible
13608                  (cond ((stringp gnus-visible-headers)
13609                         gnus-visible-headers)
13610                        ((and gnus-visible-headers
13611                              (listp gnus-visible-headers))
13612                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13613                 want-list beg)
13614             ;; First we narrow to just the headers.
13615             (widen)
13616             (goto-char (point-min))
13617             ;; Hide any "From " lines at the beginning of (mail) articles.
13618             (while (looking-at "From ")
13619               (forward-line 1))
13620             (unless (bobp)
13621               (gnus-hide-text (point-min) (point) props))
13622             ;; Then treat the rest of the header lines.
13623             (narrow-to-region
13624              (point)
13625              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13626             ;; Then we use the two regular expressions
13627             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13628             ;; select which header lines is to remain visible in the
13629             ;; article buffer.
13630             (goto-char (point-min))
13631             (while (re-search-forward "^[^ \t]*:" nil t)
13632               (beginning-of-line)
13633               ;; We add the headers we want to keep to a list and delete
13634               ;; them from the buffer.
13635               (if (or (and visible (looking-at visible))
13636                       (and ignored (not (looking-at ignored))))
13637                   (progn
13638                     (push (buffer-substring
13639                            (setq beg (point))
13640                            (progn
13641                              (forward-line 1)
13642                              ;; Be sure to get multi-line headers...
13643                              (re-search-forward "^[^ \t]*:" nil t)
13644                              (beginning-of-line)
13645                              (point)))
13646                           want-list)
13647                     (delete-region beg (point)))
13648                 (forward-line 1)))
13649             ;; Sort the headers that we want to display.
13650             (setq want-list (sort want-list 'gnus-article-header-less))
13651             (goto-char (point-min))
13652             (while want-list
13653               (insert (pop want-list)))
13654             ;; We make the unwanted headers invisible.
13655             (if delete
13656                 (delete-region (point-min) (point-max))
13657               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13658               (gnus-hide-text-type (point) (point-max) 'headers))))))))
13659
13660 (defsubst gnus-article-header-rank (header)
13661   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13662   (let ((list gnus-sorted-header-list)
13663         (i 0))
13664     (while list
13665       (when (string-match (car list) header)
13666         (setq list nil))
13667       (setq list (cdr list))
13668       (incf i))
13669     i))
13670
13671 (defun gnus-article-header-less (h1 h2)
13672   "Say whether string H1 is \"less\" than string H2."
13673   (< (gnus-article-header-rank h1)
13674      (gnus-article-header-rank h2)))
13675
13676 (defun gnus-article-hide-boring-headers (&optional arg)
13677   "Toggle hiding of headers that aren't very interesting.
13678 If given a negative prefix, always show; if given a positive prefix,
13679 always hide."
13680   (interactive "P")
13681   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13682     (save-excursion
13683       (set-buffer gnus-article-buffer)
13684       (save-restriction
13685         (let ((buffer-read-only nil)
13686               (list gnus-boring-article-headers)
13687               (inhibit-point-motion-hooks t)
13688               elem)
13689           (nnheader-narrow-to-headers)
13690           (while list
13691             (setq elem (pop list))
13692             (goto-char (point-min))
13693             (cond
13694              ;; Hide empty headers.
13695              ((eq elem 'empty)
13696               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13697                 (forward-line -1)
13698                 (gnus-hide-text-type
13699                  (progn (beginning-of-line) (point))
13700                  (progn 
13701                    (end-of-line)
13702                    (if (re-search-forward "^[^ \t]" nil t)
13703                        (match-beginning 0)
13704                      (point-max)))
13705                  'boring-headers)))
13706              ;; Hide boring Newsgroups header.
13707              ((eq elem 'newsgroups)
13708               (when (equal (mail-fetch-field "newsgroups")
13709                            (gnus-group-real-name gnus-newsgroup-name))
13710                 (gnus-article-hide-header "newsgroups")))
13711              ((eq elem 'followup-to)
13712               (when (equal (mail-fetch-field "followup-to")
13713                            (mail-fetch-field "newsgroups"))
13714                 (gnus-article-hide-header "followup-to")))
13715              ((eq elem 'reply-to)
13716               (let ((from (mail-fetch-field "from"))
13717                     (reply-to (mail-fetch-field "reply-to")))
13718                 (when (and
13719                        from reply-to
13720                        (equal 
13721                         (nth 1 (funcall gnus-extract-address-components from))
13722                         (nth 1 (funcall gnus-extract-address-components
13723                                         reply-to))))
13724                   (gnus-article-hide-header "reply-to"))))
13725              ((eq elem 'date)
13726               (let ((date (mail-fetch-field "date")))
13727                 (when (and date
13728                            (< (gnus-days-between date (current-time-string))
13729                               4))
13730                   (gnus-article-hide-header "date")))))))))))
13731
13732 (defun gnus-article-hide-header (header)
13733   (save-excursion
13734     (goto-char (point-min))
13735     (when (re-search-forward (concat "^" header ":") nil t)
13736       (gnus-hide-text-type
13737        (progn (beginning-of-line) (point))
13738        (progn 
13739          (end-of-line)
13740          (if (re-search-forward "^[^ \t]" nil t)
13741              (match-beginning 0)
13742            (point-max)))
13743        'boring-headers))))
13744
13745 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13746 (defun gnus-article-treat-overstrike ()
13747   "Translate overstrikes into bold text."
13748   (interactive)
13749   (save-excursion
13750     (set-buffer gnus-article-buffer)
13751     (let ((buffer-read-only nil))
13752       (while (search-forward "\b" nil t)
13753         (let ((next (following-char))
13754               (previous (char-after (- (point) 2))))
13755           (cond ((eq next previous)
13756                  (put-text-property (- (point) 2) (point) 'invisible t)
13757                  (put-text-property (point) (1+ (point)) 'face 'bold))
13758                 ((eq next ?_)
13759                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13760                  (put-text-property
13761                   (- (point) 2) (1- (point)) 'face 'underline))
13762                 ((eq previous ?_)
13763                  (put-text-property (- (point) 2) (point) 'invisible t)
13764                  (put-text-property
13765                   (point) (1+ (point))  'face 'underline))))))))
13766
13767 (defun gnus-article-word-wrap ()
13768   "Format too long lines."
13769   (interactive)
13770   (save-excursion
13771     (set-buffer gnus-article-buffer)
13772     (let ((buffer-read-only nil))
13773       (widen)
13774       (goto-char (point-min))
13775       (search-forward "\n\n" nil t)
13776       (end-of-line 1)
13777       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13778             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13779             (adaptive-fill-mode t))
13780         (while (not (eobp))
13781           (and (>= (current-column) (min fill-column (window-width)))
13782                (/= (preceding-char) ?:)
13783                (fill-paragraph nil))
13784           (end-of-line 2))))))
13785
13786 (defun gnus-article-remove-cr ()
13787   "Remove carriage returns from an article."
13788   (interactive)
13789   (save-excursion
13790     (set-buffer gnus-article-buffer)
13791     (let ((buffer-read-only nil))
13792       (goto-char (point-min))
13793       (while (search-forward "\r" nil t)
13794         (replace-match "" t t)))))
13795
13796 (defun gnus-article-remove-trailing-blank-lines ()
13797   "Remove all trailing blank lines from the article."
13798   (interactive)
13799   (save-excursion
13800     (set-buffer gnus-article-buffer)
13801     (let ((buffer-read-only nil))
13802       (goto-char (point-max))
13803       (delete-region
13804        (point)
13805        (progn
13806          (while (looking-at "^[ \t]*$")
13807            (forward-line -1))
13808          (forward-line 1)
13809          (point))))))
13810
13811 (defun gnus-article-display-x-face (&optional force)
13812   "Look for an X-Face header and display it if present."
13813   (interactive (list 'force))
13814   (save-excursion
13815     (set-buffer gnus-article-buffer)
13816     ;; Delete the old process, if any.
13817     (when (process-status "gnus-x-face")
13818       (delete-process "gnus-x-face"))
13819     (let ((inhibit-point-motion-hooks t)
13820           (case-fold-search nil)
13821           from)
13822       (save-restriction
13823         (nnheader-narrow-to-headers)
13824         (setq from (mail-fetch-field "from"))
13825         (goto-char (point-min))
13826         (when (and gnus-article-x-face-command
13827                    (or force
13828                        ;; Check whether this face is censored.
13829                        (not gnus-article-x-face-too-ugly)
13830                        (and gnus-article-x-face-too-ugly from
13831                             (not (string-match gnus-article-x-face-too-ugly
13832                                                from))))
13833                    ;; Has to be present.
13834                    (re-search-forward "^X-Face: " nil t))
13835           ;; We now have the area of the buffer where the X-Face is stored.
13836           (let ((beg (point))
13837                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13838             ;; We display the face.
13839             (if (symbolp gnus-article-x-face-command)
13840                 ;; The command is a lisp function, so we call it.
13841                 (if (gnus-functionp gnus-article-x-face-command)
13842                     (funcall gnus-article-x-face-command beg end)
13843                   (error "%s is not a function" gnus-article-x-face-command))
13844               ;; The command is a string, so we interpret the command
13845               ;; as a, well, command, and fork it off.
13846               (let ((process-connection-type nil))
13847                 (process-kill-without-query
13848                  (start-process
13849                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13850                 (process-send-region "gnus-x-face" beg end)
13851                 (process-send-eof "gnus-x-face")))))))))
13852
13853 (defun gnus-headers-decode-quoted-printable ()
13854   "Hack to remove QP encoding from headers."
13855   (let ((case-fold-search t)
13856         (inhibit-point-motion-hooks t)
13857         string)
13858     (goto-char (point-min))
13859     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13860       (setq string (match-string 1))
13861       (narrow-to-region (match-beginning 0) (match-end 0))
13862       (delete-region (point-min) (point-max))
13863       (insert string)
13864       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13865       (subst-char-in-region (point-min) (point-max) ?_ ? )
13866       (widen)
13867       (goto-char (point-min)))))
13868
13869 (defun gnus-article-de-quoted-unreadable (&optional force)
13870   "Do a naive translation of a quoted-printable-encoded article.
13871 This is in no way, shape or form meant as a replacement for real MIME
13872 processing, but is simply a stop-gap measure until MIME support is
13873 written.
13874 If FORCE, decode the article whether it is marked as quoted-printable
13875 or not."
13876   (interactive (list 'force))
13877   (save-excursion
13878     (set-buffer gnus-article-buffer)
13879     (let ((case-fold-search t)
13880           (buffer-read-only nil)
13881           (type (gnus-fetch-field "content-transfer-encoding")))
13882       (when (or force
13883                 (and type (string-match "quoted-printable" (downcase type))))
13884         (gnus-headers-decode-quoted-printable)
13885         (goto-char (point-min))
13886         (search-forward "\n\n" nil 'move)
13887         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13888
13889 (defun gnus-mime-decode-quoted-printable (from to)
13890   "Decode Quoted-Printable in the region between FROM and TO."
13891   (goto-char from)
13892   (while (search-forward "=" to t)
13893     (cond ((eq (following-char) ?\n)
13894            (delete-char -1)
13895            (delete-char 1))
13896           ((looking-at "[0-9A-F][0-9A-F]")
13897            (delete-char -1)
13898            (insert (hexl-hex-string-to-integer
13899                     (buffer-substring (point) (+ 2 (point)))))
13900            (delete-char 2))
13901           ((looking-at "=")
13902            (delete-char 1))
13903           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13904
13905 (defun gnus-article-hide-pgp (&optional arg)
13906   "Toggle hiding of any PGP headers and signatures in the current article.
13907 If given a negative prefix, always show; if given a positive prefix,
13908 always hide."
13909   (interactive "P")
13910   (unless (gnus-article-check-hidden-text 'pgp arg)
13911     (save-excursion
13912       (set-buffer gnus-article-buffer)
13913       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13914             buffer-read-only beg end)
13915         (widen)
13916         (goto-char (point-min))
13917         ;; Hide the "header".
13918         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13919              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13920         (setq beg (point))
13921         ;; Hide the actual signature.
13922         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13923              (setq end (1+ (match-beginning 0)))
13924              (gnus-hide-text
13925               end
13926               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13927                   (match-end 0)
13928                 ;; Perhaps we shouldn't hide to the end of the buffer
13929                 ;; if there is no end to the signature?
13930                 (point-max))
13931               props))
13932         ;; Hide "- " PGP quotation markers.
13933         (when (and beg end)
13934           (narrow-to-region beg end)
13935           (goto-char (point-min))
13936           (while (re-search-forward "^- " nil t)
13937             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13938           (widen))))))
13939
13940 (defun gnus-article-hide-signature (&optional arg)
13941   "Hide the signature in the current article.
13942 If given a negative prefix, always show; if given a positive prefix,
13943 always hide."
13944   (interactive "P")
13945   (unless (gnus-article-check-hidden-text 'signature arg)
13946     (save-excursion
13947       (set-buffer gnus-article-buffer)
13948       (save-restriction
13949         (let ((buffer-read-only nil))
13950           (when (gnus-narrow-to-signature)
13951             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13952
13953 (defun gnus-article-strip-leading-blank-lines ()
13954   "Remove all blank lines from the beginning of the article."
13955   (interactive)
13956   (save-excursion
13957     (set-buffer gnus-article-buffer)
13958     (let (buffer-read-only)
13959       (goto-char (point-min))
13960       (when (search-forward "\n\n" nil t)
13961         (while (looking-at "[ \t]$")
13962           (gnus-delete-line))))))
13963
13964 (defun gnus-narrow-to-signature ()
13965   "Narrow to the signature."
13966   (widen)
13967   (goto-char (point-max))
13968   (when (re-search-backward gnus-signature-separator nil t)
13969     (forward-line 1)
13970     (when (or (null gnus-signature-limit)
13971               (and (numberp gnus-signature-limit)
13972                    (< (- (point-max) (point)) gnus-signature-limit))
13973               (and (gnus-functionp gnus-signature-limit)
13974                    (funcall gnus-signature-limit))
13975               (and (stringp gnus-signature-limit)
13976                    (not (re-search-forward gnus-signature-limit nil t))))
13977       (narrow-to-region (point) (point-max))
13978       t)))
13979
13980 (defun gnus-article-check-hidden-text (type arg)
13981   "Return nil if hiding is necessary."
13982   (save-excursion
13983     (set-buffer gnus-article-buffer)
13984     (let ((hide (gnus-article-hidden-text-p type)))
13985       (cond ((or (and (null arg) (eq hide 'hidden))
13986                  (and arg (< 0 (prefix-numeric-value arg))))
13987              (gnus-article-show-hidden-text type))
13988             ((eq hide 'shown)
13989              (gnus-article-show-hidden-text type t))
13990             (t nil)))))
13991
13992 (defun gnus-article-hidden-text-p (type)
13993   "Say whether the current buffer contains hidden text of type TYPE."
13994   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13995     (when pos
13996       (if (get-text-property pos 'invisible)
13997           'hidden
13998         'shown))))
13999
14000 (defun gnus-article-hide (&optional arg force)
14001   "Hide all the gruft in the current article.
14002 This means that PGP stuff, signatures, cited text and (some)
14003 headers will be hidden.
14004 If given a prefix, show the hidden text instead."
14005   (interactive (list current-prefix-arg 'force))
14006   (gnus-article-hide-headers arg)
14007   (gnus-article-hide-pgp arg)
14008   (gnus-article-hide-citation-maybe arg force)
14009   (gnus-article-hide-signature arg))
14010
14011 (defun gnus-article-show-hidden-text (type &optional hide)
14012   "Show all hidden text of type TYPE.
14013 If HIDE, hide the text instead."
14014   (save-excursion
14015     (set-buffer gnus-article-buffer)
14016     (let ((buffer-read-only nil)
14017           (inhibit-point-motion-hooks t)
14018           (beg (point-min)))
14019       (while (gnus-goto-char (text-property-any
14020                               beg (point-max) 'gnus-type type))
14021         (setq beg (point))
14022         (forward-char)
14023         (if hide
14024             (gnus-hide-text beg (point) gnus-hidden-properties)
14025           (gnus-unhide-text beg (point)))
14026         (setq beg (point)))
14027       t)))
14028
14029 (defvar gnus-article-time-units
14030   `((year . ,(* 365.25 24 60 60))
14031     (week . ,(* 7 24 60 60))
14032     (day . ,(* 24 60 60))
14033     (hour . ,(* 60 60))
14034     (minute . 60)
14035     (second . 1))
14036   "Mapping from time units to seconds.")
14037
14038 (defun gnus-article-date-ut (&optional type highlight)
14039   "Convert DATE date to universal time in the current article.
14040 If TYPE is `local', convert to local time; if it is `lapsed', output
14041 how much time has lapsed since DATE."
14042   (interactive (list 'ut t))
14043   (let* ((header (or gnus-current-headers
14044                      (gnus-summary-article-header) ""))
14045          (date (and (vectorp header) (mail-header-date header)))
14046          (date-regexp "^Date: \\|^X-Sent: ")
14047          (now (current-time))
14048          (inhibit-point-motion-hooks t)
14049          bface eface)
14050     (when (and date (not (string= date "")))
14051       (save-excursion
14052         (set-buffer gnus-article-buffer)
14053         (save-restriction
14054           (nnheader-narrow-to-headers)
14055           (let ((buffer-read-only nil))
14056             ;; Delete any old Date headers.
14057             (if (re-search-forward date-regexp nil t)
14058                 (progn
14059                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14060                         eface (get-text-property (gnus-point-at-eol) 'face))
14061                   (message-remove-header date-regexp t)
14062                   (beginning-of-line))
14063               (goto-char (point-max)))
14064             (insert
14065              (cond
14066               ;; Convert to the local timezone.  We have to slap a
14067               ;; `condition-case' round the calls to the timezone
14068               ;; functions since they aren't particularly resistant to
14069               ;; buggy dates.
14070               ((eq type 'local)
14071                (concat "Date: " (condition-case ()
14072                                     (timezone-make-date-arpa-standard date)
14073                                   (error date))
14074                        "\n"))
14075               ;; Convert to Universal Time.
14076               ((eq type 'ut)
14077                (concat "Date: "
14078                        (condition-case ()
14079                            (timezone-make-date-arpa-standard date nil "UT")
14080                          (error date))
14081                        "\n"))
14082               ;; Get the original date from the article.
14083               ((eq type 'original)
14084                (concat "Date: " date "\n"))
14085               ;; Do an X-Sent lapsed format.
14086               ((eq type 'lapsed)
14087                ;; If the date is seriously mangled, the timezone
14088                ;; functions are liable to bug out, so we condition-case
14089                ;; the entire thing.
14090                (let* ((real-time
14091                        (condition-case ()
14092                            (gnus-time-minus
14093                             (gnus-encode-date
14094                              (timezone-make-date-arpa-standard
14095                               (current-time-string now)
14096                               (current-time-zone now) "UT"))
14097                             (gnus-encode-date
14098                              (timezone-make-date-arpa-standard
14099                               date nil "UT")))
14100                          (error '(0 0))))
14101                       (real-sec (+ (* (float (car real-time)) 65536)
14102                                    (cadr real-time)))
14103                       (sec (abs real-sec))
14104                       num prev)
14105                  (if (zerop sec)
14106                      "X-Sent: Now\n"
14107                    (concat
14108                     "X-Sent: "
14109                     ;; This is a bit convoluted, but basically we go
14110                     ;; through the time units for years, weeks, etc,
14111                     ;; and divide things to see whether that results
14112                     ;; in positive answers.
14113                     (mapconcat
14114                      (lambda (unit)
14115                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14116                            ;; The (remaining) seconds are too few to
14117                            ;; be divided into this time unit.
14118                            ""
14119                          ;; It's big enough, so we output it.
14120                          (setq sec (- sec (* num (cdr unit))))
14121                          (prog1
14122                              (concat (if prev ", " "") (int-to-string
14123                                                         (floor num))
14124                                      " " (symbol-name (car unit))
14125                                      (if (> num 1) "s" ""))
14126                            (setq prev t))))
14127                      gnus-article-time-units "")
14128                     ;; If dates are odd, then it might appear like the
14129                     ;; article was sent in the future.
14130                     (if (> real-sec 0)
14131                         " ago\n"
14132                       " in the future\n")))))
14133               (t
14134                (error "Unknown conversion type: %s" type)))))
14135           ;; Do highlighting.
14136           (beginning-of-line)
14137           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14138                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14139             (put-text-property (match-beginning 1) (match-end 1)
14140                                'face bface)
14141             (put-text-property (match-beginning 2) (match-end 2)
14142                                'face eface)))))))
14143
14144 (defun gnus-article-date-local (&optional highlight)
14145   "Convert the current article date to the local timezone."
14146   (interactive (list t))
14147   (gnus-article-date-ut 'local highlight))
14148
14149 (defun gnus-article-date-original (&optional highlight)
14150   "Convert the current article date to what it was originally.
14151 This is only useful if you have used some other date conversion
14152 function and want to see what the date was before converting."
14153   (interactive (list t))
14154   (gnus-article-date-ut 'original highlight))
14155
14156 (defun gnus-article-date-lapsed (&optional highlight)
14157   "Convert the current article date to time lapsed since it was sent."
14158   (interactive (list t))
14159   (gnus-article-date-ut 'lapsed highlight))
14160
14161 (defun gnus-article-maybe-highlight ()
14162   "Do some article highlighting if `gnus-visual' is non-nil."
14163   (if (gnus-visual-p 'article-highlight 'highlight)
14164       (gnus-article-highlight-some)))
14165
14166 ;; Article savers.
14167
14168 (defun gnus-output-to-rmail (file-name)
14169   "Append the current article to an Rmail file named FILE-NAME."
14170   (require 'rmail)
14171   ;; Most of these codes are borrowed from rmailout.el.
14172   (setq file-name (expand-file-name file-name))
14173   (setq rmail-default-rmail-file file-name)
14174   (let ((artbuf (current-buffer))
14175         (tmpbuf (get-buffer-create " *Gnus-output*")))
14176     (save-excursion
14177       (or (get-file-buffer file-name)
14178           (file-exists-p file-name)
14179           (if (gnus-yes-or-no-p
14180                (concat "\"" file-name "\" does not exist, create it? "))
14181               (let ((file-buffer (create-file-buffer file-name)))
14182                 (save-excursion
14183                   (set-buffer file-buffer)
14184                   (rmail-insert-rmail-file-header)
14185                   (let ((require-final-newline nil))
14186                     (write-region (point-min) (point-max) file-name t 1)))
14187                 (kill-buffer file-buffer))
14188             (error "Output file does not exist")))
14189       (set-buffer tmpbuf)
14190       (buffer-disable-undo (current-buffer))
14191       (erase-buffer)
14192       (insert-buffer-substring artbuf)
14193       (gnus-convert-article-to-rmail)
14194       ;; Decide whether to append to a file or to an Emacs buffer.
14195       (let ((outbuf (get-file-buffer file-name)))
14196         (if (not outbuf)
14197             (append-to-file (point-min) (point-max) file-name)
14198           ;; File has been visited, in buffer OUTBUF.
14199           (set-buffer outbuf)
14200           (let ((buffer-read-only nil)
14201                 (msg (and (boundp 'rmail-current-message)
14202                           (symbol-value 'rmail-current-message))))
14203             ;; If MSG is non-nil, buffer is in RMAIL mode.
14204             (if msg
14205                 (progn (widen)
14206                        (narrow-to-region (point-max) (point-max))))
14207             (insert-buffer-substring tmpbuf)
14208             (if msg
14209                 (progn
14210                   (goto-char (point-min))
14211                   (widen)
14212                   (search-backward "\^_")
14213                   (narrow-to-region (point) (point-max))
14214                   (goto-char (1+ (point-min)))
14215                   (rmail-count-new-messages t)
14216                   (rmail-show-message msg)))))))
14217     (kill-buffer tmpbuf)))
14218
14219 (defun gnus-output-to-file (file-name)
14220   "Append the current article to a file named FILE-NAME."
14221   (setq file-name (expand-file-name file-name))
14222   (let ((artbuf (current-buffer))
14223         (tmpbuf (get-buffer-create " *Gnus-output*")))
14224     (save-excursion
14225       (set-buffer tmpbuf)
14226       (buffer-disable-undo (current-buffer))
14227       (erase-buffer)
14228       (insert-buffer-substring artbuf)
14229       ;; Append newline at end of the buffer as separator, and then
14230       ;; save it to file.
14231       (goto-char (point-max))
14232       (insert "\n")
14233       (append-to-file (point-min) (point-max) file-name))
14234     (kill-buffer tmpbuf)))
14235
14236 (defun gnus-convert-article-to-rmail ()
14237   "Convert article in current buffer to Rmail message format."
14238   (let ((buffer-read-only nil))
14239     ;; Convert article directly into Babyl format.
14240     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14241     (goto-char (point-min))
14242     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14243     (while (search-forward "\n\^_" nil t) ;single char
14244       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14245     (goto-char (point-max))
14246     (insert "\^_")))
14247
14248 (defun gnus-narrow-to-page (&optional arg)
14249   "Narrow the article buffer to a page.
14250 If given a numerical ARG, move forward ARG pages."
14251   (interactive "P")
14252   (setq arg (if arg (prefix-numeric-value arg) 0))
14253   (save-excursion
14254     (set-buffer gnus-article-buffer)
14255     (goto-char (point-min))
14256     (widen)
14257     (when (gnus-visual-p 'page-marker)
14258       (let ((buffer-read-only nil))
14259         (gnus-remove-text-with-property 'gnus-prev)
14260         (gnus-remove-text-with-property 'gnus-next)))
14261     (when
14262         (cond ((< arg 0)
14263                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14264               ((> arg 0)
14265                (re-search-forward page-delimiter nil 'move arg)))
14266       (goto-char (match-end 0)))
14267     (narrow-to-region
14268      (point)
14269      (if (re-search-forward page-delimiter nil 'move)
14270          (match-beginning 0)
14271        (point)))
14272     (when (and (gnus-visual-p 'page-marker)
14273                (not (= (point-min) 1)))
14274       (save-excursion
14275         (goto-char (point-min))
14276         (gnus-insert-prev-page-button)))
14277     (when (and (gnus-visual-p 'page-marker)
14278                (not (= (1- (point-max)) (buffer-size))))
14279       (save-excursion
14280         (goto-char (point-max))
14281         (gnus-insert-next-page-button)))))
14282
14283 ;; Article mode commands
14284
14285 (defun gnus-article-goto-next-page ()
14286   "Show the next page of the article."
14287   (interactive)
14288   (when (gnus-article-next-page)
14289     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14290
14291 (defun gnus-article-goto-prev-page ()
14292   "Show the next page of the article."
14293   (interactive)
14294   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14295     (gnus-article-prev-page nil)))
14296
14297 (defun gnus-article-next-page (&optional lines)
14298   "Show the next page of the current article.
14299 If end of article, return non-nil.  Otherwise return nil.
14300 Argument LINES specifies lines to be scrolled up."
14301   (interactive "p")
14302   (move-to-window-line -1)
14303   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14304   (if (save-excursion
14305         (end-of-line)
14306         (and (pos-visible-in-window-p)  ;Not continuation line.
14307              (eobp)))
14308       ;; Nothing in this page.
14309       (if (or (not gnus-break-pages)
14310               (save-excursion
14311                 (save-restriction
14312                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14313           t                             ;Nothing more.
14314         (gnus-narrow-to-page 1)         ;Go to next page.
14315         nil)
14316     ;; More in this page.
14317     (condition-case ()
14318         (scroll-up lines)
14319       (end-of-buffer
14320        ;; Long lines may cause an end-of-buffer error.
14321        (goto-char (point-max))))
14322     (move-to-window-line 0)
14323     nil))
14324
14325 (defun gnus-article-prev-page (&optional lines)
14326   "Show previous page of current article.
14327 Argument LINES specifies lines to be scrolled down."
14328   (interactive "p")
14329   (move-to-window-line 0)
14330   (if (and gnus-break-pages
14331            (bobp)
14332            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14333       (progn
14334         (gnus-narrow-to-page -1)        ;Go to previous page.
14335         (goto-char (point-max))
14336         (recenter -1))
14337     (prog1
14338         (condition-case ()
14339             (scroll-down lines)
14340           (error nil))
14341       (move-to-window-line 0))))
14342
14343 (defun gnus-article-refer-article ()
14344   "Read article specified by message-id around point."
14345   (interactive)
14346   (let ((point (point)))
14347     (search-forward ">" nil t)          ;Move point to end of "<....>".
14348     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14349         (let ((message-id (match-string 1)))
14350           (goto-char point)
14351           (set-buffer gnus-summary-buffer)
14352           (gnus-summary-refer-article message-id))
14353       (goto-char (point))
14354       (error "No references around point"))))
14355
14356 (defun gnus-article-show-summary ()
14357   "Reconfigure windows to show summary buffer."
14358   (interactive)
14359   (gnus-configure-windows 'article)
14360   (gnus-summary-goto-subject gnus-current-article))
14361
14362 (defun gnus-article-describe-briefly ()
14363   "Describe article mode commands briefly."
14364   (interactive)
14365   (gnus-message 6
14366                 (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")))
14367
14368 (defun gnus-article-summary-command ()
14369   "Execute the last keystroke in the summary buffer."
14370   (interactive)
14371   (let ((obuf (current-buffer))
14372         (owin (current-window-configuration))
14373         func)
14374     (switch-to-buffer gnus-summary-buffer 'norecord)
14375     (setq func (lookup-key (current-local-map) (this-command-keys)))
14376     (call-interactively func)
14377     (set-buffer obuf)
14378     (set-window-configuration owin)
14379     (set-window-point (get-buffer-window (current-buffer)) (point))))
14380
14381 (defun gnus-article-summary-command-nosave ()
14382   "Execute the last keystroke in the summary buffer."
14383   (interactive)
14384   (let (func)
14385     (pop-to-buffer gnus-summary-buffer 'norecord)
14386     (setq func (lookup-key (current-local-map) (this-command-keys)))
14387     (call-interactively func)))
14388
14389 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14390   "Read a summary buffer key sequence and execute it from the article buffer."
14391   (interactive "P")
14392   (let ((nosaves
14393          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14394            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14395            "=" "^" "\M-^"))
14396         keys)
14397     (save-excursion
14398       (set-buffer gnus-summary-buffer)
14399       (push (or key last-command-event) unread-command-events)
14400       (setq keys (read-key-sequence nil)))
14401     (message "")
14402
14403     (if (member keys nosaves)
14404         (let (func)
14405           (pop-to-buffer gnus-summary-buffer 'norecord)
14406           (if (setq func (lookup-key (current-local-map) keys))
14407               (call-interactively func)
14408             (ding)))
14409       (let ((obuf (current-buffer))
14410             (owin (current-window-configuration))
14411             (opoint (point))
14412             func in-buffer)
14413         (if not-restore-window
14414             (pop-to-buffer gnus-summary-buffer 'norecord)
14415           (switch-to-buffer gnus-summary-buffer 'norecord))
14416         (setq in-buffer (current-buffer))
14417         (if (setq func (lookup-key (current-local-map) keys))
14418             (call-interactively func)
14419           (ding))
14420         (when (eq in-buffer (current-buffer))
14421           (set-buffer obuf)
14422           (unless not-restore-window
14423             (set-window-configuration owin))
14424           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14425
14426 \f
14427 ;;;
14428 ;;; Kill file handling.
14429 ;;;
14430
14431 ;;;###autoload
14432 (defalias 'gnus-batch-kill 'gnus-batch-score)
14433 ;;;###autoload
14434 (defun gnus-batch-score ()
14435   "Run batched scoring.
14436 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14437 Newsgroups is a list of strings in Bnews format.  If you want to score
14438 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14439 score the alt hierarchy, you'd say \"!alt.all\"."
14440   (interactive)
14441   (let* ((yes-and-no
14442           (gnus-newsrc-parse-options
14443            (apply (function concat)
14444                   (mapcar (lambda (g) (concat g " "))
14445                           command-line-args-left))))
14446          (gnus-expert-user t)
14447          (nnmail-spool-file nil)
14448          (gnus-use-dribble-file nil)
14449          (yes (car yes-and-no))
14450          (no (cdr yes-and-no))
14451          group newsrc entry
14452          ;; Disable verbose message.
14453          gnus-novice-user gnus-large-newsgroup)
14454     ;; Eat all arguments.
14455     (setq command-line-args-left nil)
14456     ;; Start Gnus.
14457     (gnus)
14458     ;; Apply kills to specified newsgroups in command line arguments.
14459     (setq newsrc (cdr gnus-newsrc-alist))
14460     (while newsrc
14461       (setq group (caar newsrc))
14462       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14463       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14464                (and (car entry)
14465                     (or (eq (car entry) t)
14466                         (not (zerop (car entry)))))
14467                (if yes (string-match yes group) t)
14468                (or (null no) (not (string-match no group))))
14469           (progn
14470             (gnus-summary-read-group group nil t nil t)
14471             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14472                  (gnus-summary-exit))))
14473       (setq newsrc (cdr newsrc)))
14474     ;; Exit Emacs.
14475     (switch-to-buffer gnus-group-buffer)
14476     (gnus-group-save-newsrc)))
14477
14478 (defun gnus-apply-kill-file ()
14479   "Apply a kill file to the current newsgroup.
14480 Returns the number of articles marked as read."
14481   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14482           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14483       (gnus-apply-kill-file-internal)
14484     0))
14485
14486 (defun gnus-kill-save-kill-buffer ()
14487   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14488     (when (get-file-buffer file)
14489       (save-excursion
14490         (set-buffer (get-file-buffer file))
14491         (and (buffer-modified-p) (save-buffer))
14492         (kill-buffer (current-buffer))))))
14493
14494 (defvar gnus-kill-file-name "KILL"
14495   "Suffix of the kill files.")
14496
14497 (defun gnus-newsgroup-kill-file (newsgroup)
14498   "Return the name of a kill file name for NEWSGROUP.
14499 If NEWSGROUP is nil, return the global kill file name instead."
14500   (cond 
14501    ;; The global KILL file is placed at top of the directory.
14502    ((or (null newsgroup)
14503         (string-equal newsgroup ""))
14504     (expand-file-name gnus-kill-file-name
14505                       (or gnus-kill-files-directory "~/News")))
14506    ;; Append ".KILL" to newsgroup name.
14507    ((gnus-use-long-file-name 'not-kill)
14508     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14509                               "." gnus-kill-file-name)
14510                       (or gnus-kill-files-directory "~/News")))
14511    ;; Place "KILL" under the hierarchical directory.
14512    (t
14513     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14514                               "/" gnus-kill-file-name)
14515                       (or gnus-kill-files-directory "~/News")))))
14516
14517 \f
14518 ;;;
14519 ;;; Dribble file
14520 ;;;
14521
14522 (defvar gnus-dribble-ignore nil)
14523 (defvar gnus-dribble-eval-file nil)
14524
14525 (defun gnus-dribble-file-name ()
14526   "Return the dribble file for the current .newsrc."
14527   (concat
14528    (if gnus-dribble-directory
14529        (concat (file-name-as-directory gnus-dribble-directory)
14530                (file-name-nondirectory gnus-current-startup-file))
14531      gnus-current-startup-file)
14532    "-dribble"))
14533
14534 (defun gnus-dribble-enter (string)
14535   "Enter STRING into the dribble buffer."
14536   (if (and (not gnus-dribble-ignore)
14537            gnus-dribble-buffer
14538            (buffer-name gnus-dribble-buffer))
14539       (let ((obuf (current-buffer)))
14540         (set-buffer gnus-dribble-buffer)
14541         (insert string "\n")
14542         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14543         (set-buffer obuf))))
14544
14545 (defun gnus-dribble-read-file ()
14546   "Read the dribble file from disk."
14547   (let ((dribble-file (gnus-dribble-file-name)))
14548     (save-excursion
14549       (set-buffer (setq gnus-dribble-buffer
14550                         (get-buffer-create
14551                          (file-name-nondirectory dribble-file))))
14552       (gnus-add-current-to-buffer-list)
14553       (erase-buffer)
14554       (setq buffer-file-name dribble-file)
14555       (auto-save-mode t)
14556       (buffer-disable-undo (current-buffer))
14557       (bury-buffer (current-buffer))
14558       (set-buffer-modified-p nil)
14559       (let ((auto (make-auto-save-file-name))
14560             (gnus-dribble-ignore t))
14561         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14562           ;; Load whichever file is newest -- the auto save file
14563           ;; or the "real" file.
14564           (if (file-newer-than-file-p auto dribble-file)
14565               (insert-file-contents auto)
14566             (insert-file-contents dribble-file))
14567           (unless (zerop (buffer-size))
14568             (set-buffer-modified-p t))
14569           ;; Set the file modes to reflect the .newsrc file modes.
14570           (save-buffer)
14571           (when (file-exists-p gnus-current-startup-file)
14572             (set-file-modes dribble-file
14573                             (file-modes gnus-current-startup-file)))
14574           ;; Possibly eval the file later.
14575           (when (gnus-y-or-n-p
14576                  "Auto-save file exists.  Do you want to read it? ")
14577             (setq gnus-dribble-eval-file t)))))))
14578
14579 (defun gnus-dribble-eval-file ()
14580   (if (not gnus-dribble-eval-file)
14581       ()
14582     (setq gnus-dribble-eval-file nil)
14583     (save-excursion
14584       (let ((gnus-dribble-ignore t))
14585         (set-buffer gnus-dribble-buffer)
14586         (eval-buffer (current-buffer))))))
14587
14588 (defun gnus-dribble-delete-file ()
14589   (if (file-exists-p (gnus-dribble-file-name))
14590       (delete-file (gnus-dribble-file-name)))
14591   (if gnus-dribble-buffer
14592       (save-excursion
14593         (set-buffer gnus-dribble-buffer)
14594         (let ((auto (make-auto-save-file-name)))
14595           (if (file-exists-p auto)
14596               (delete-file auto))
14597           (erase-buffer)
14598           (set-buffer-modified-p nil)))))
14599
14600 (defun gnus-dribble-save ()
14601   (if (and gnus-dribble-buffer
14602            (buffer-name gnus-dribble-buffer))
14603       (save-excursion
14604         (set-buffer gnus-dribble-buffer)
14605         (save-buffer))))
14606
14607 (defun gnus-dribble-clear ()
14608   (save-excursion
14609     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14610         (progn
14611           (set-buffer gnus-dribble-buffer)
14612           (erase-buffer)
14613           (set-buffer-modified-p nil)
14614           (setq buffer-saved-size (buffer-size))))))
14615
14616 \f
14617 ;;;
14618 ;;; Server Communication
14619 ;;;
14620
14621 (defun gnus-start-news-server (&optional confirm)
14622   "Open a method for getting news.
14623 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14624   (let (how)
14625     (if gnus-current-select-method
14626         ;; Stream is already opened.
14627         nil
14628       ;; Open NNTP server.
14629       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14630       (if confirm
14631           (progn
14632             ;; Read server name with completion.
14633             (setq gnus-nntp-server
14634                   (completing-read "NNTP server: "
14635                                    (mapcar (lambda (server) (list server))
14636                                            (cons (list gnus-nntp-server)
14637                                                  gnus-secondary-servers))
14638                                    nil nil gnus-nntp-server))))
14639
14640       (if (and gnus-nntp-server
14641                (stringp gnus-nntp-server)
14642                (not (string= gnus-nntp-server "")))
14643           (setq gnus-select-method
14644                 (cond ((or (string= gnus-nntp-server "")
14645                            (string= gnus-nntp-server "::"))
14646                        (list 'nnspool (system-name)))
14647                       ((string-match "^:" gnus-nntp-server)
14648                        (list 'nnmh gnus-nntp-server
14649                              (list 'nnmh-directory
14650                                    (file-name-as-directory
14651                                     (expand-file-name
14652                                      (concat "~/" (substring
14653                                                    gnus-nntp-server 1)))))
14654                              (list 'nnmh-get-new-mail nil)))
14655                       (t
14656                        (list 'nntp gnus-nntp-server)))))
14657
14658       (setq how (car gnus-select-method))
14659       (cond ((eq how 'nnspool)
14660              (require 'nnspool)
14661              (gnus-message 5 "Looking up local news spool..."))
14662             ((eq how 'nnmh)
14663              (require 'nnmh)
14664              (gnus-message 5 "Looking up mh spool..."))
14665             (t
14666              (require 'nntp)))
14667       (setq gnus-current-select-method gnus-select-method)
14668       (run-hooks 'gnus-open-server-hook)
14669       (or
14670        ;; gnus-open-server-hook might have opened it
14671        (gnus-server-opened gnus-select-method)
14672        (gnus-open-server gnus-select-method)
14673        (gnus-y-or-n-p
14674         (format
14675          "%s (%s) open error: '%s'.     Continue? "
14676          (car gnus-select-method) (cadr gnus-select-method)
14677          (gnus-status-message gnus-select-method)))
14678        (progn
14679          (gnus-message 1 "Couldn't open server on %s"
14680                        (nth 1 gnus-select-method))
14681          (ding)
14682          nil)))))
14683
14684 (defun gnus-check-group (group)
14685   "Try to make sure that the server where GROUP exists is alive."
14686   (let ((method (gnus-find-method-for-group group)))
14687     (or (gnus-server-opened method)
14688         (gnus-open-server method))))
14689
14690 (defun gnus-check-server (&optional method silent)
14691   "Check whether the connection to METHOD is down.
14692 If METHOD is nil, use `gnus-select-method'.
14693 If it is down, start it up (again)."
14694   (let ((method (or method gnus-select-method)))
14695     ;; Transform virtual server names into select methods.
14696     (when (stringp method)
14697       (setq method (gnus-server-to-method method)))
14698     (if (gnus-server-opened method)
14699         ;; The stream is already opened.
14700         t
14701       ;; Open the server.
14702       (unless silent
14703         (gnus-message 5 "Opening %s server%s..." (car method)
14704                       (if (equal (nth 1 method) "") ""
14705                         (format " on %s" (nth 1 method)))))
14706       (run-hooks 'gnus-open-server-hook)
14707       (prog1
14708           (gnus-open-server method)
14709         (unless silent
14710           (message ""))))))
14711
14712 (defun gnus-get-function (method function)
14713   "Return a function symbol based on METHOD and FUNCTION."
14714   ;; Translate server names into methods.
14715   (unless method
14716     (error "Attempted use of a nil select method"))
14717   (when (stringp method)
14718     (setq method (gnus-server-to-method method)))
14719   (let ((func (intern (format "%s-%s" (car method) function))))
14720     ;; If the functions isn't bound, we require the backend in
14721     ;; question.
14722     (unless (fboundp func)
14723       (require (car method))
14724       (unless (fboundp func)
14725         ;; This backend doesn't implement this function.
14726         (error "No such function: %s" func)))
14727     func))
14728
14729 \f
14730 ;;;
14731 ;;; Interface functions to the backends.
14732 ;;;
14733
14734 (defun gnus-open-server (method)
14735   "Open a connection to METHOD."
14736   (when (stringp method)
14737     (setq method (gnus-server-to-method method)))
14738   (let ((elem (assoc method gnus-opened-servers)))
14739     ;; If this method was previously denied, we just return nil.
14740     (if (eq (nth 1 elem) 'denied)
14741         (progn
14742           (gnus-message 1 "Denied server")
14743           nil)
14744       ;; Open the server.
14745       (let ((result
14746              (funcall (gnus-get-function method 'open-server)
14747                       (nth 1 method) (nthcdr 2 method))))
14748         ;; If this hasn't been opened before, we add it to the list.
14749         (unless elem
14750           (setq elem (list method nil)
14751                 gnus-opened-servers (cons elem gnus-opened-servers)))
14752         ;; Set the status of this server.
14753         (setcar (cdr elem) (if result 'ok 'denied))
14754         ;; Return the result from the "open" call.
14755         result))))
14756
14757 (defun gnus-close-server (method)
14758   "Close the connection to METHOD."
14759   (when (stringp method)
14760     (setq method (gnus-server-to-method method)))
14761   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14762
14763 (defun gnus-request-list (method)
14764   "Request the active file from METHOD."
14765   (when (stringp method)
14766     (setq method (gnus-server-to-method method)))
14767   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14768
14769 (defun gnus-request-list-newsgroups (method)
14770   "Request the newsgroups file from METHOD."
14771   (when (stringp method)
14772     (setq method (gnus-server-to-method method)))
14773   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14774
14775 (defun gnus-request-newgroups (date method)
14776   "Request all new groups since DATE from METHOD."
14777   (when (stringp method)
14778     (setq method (gnus-server-to-method method)))
14779   (funcall (gnus-get-function method 'request-newgroups)
14780            date (nth 1 method)))
14781
14782 (defun gnus-server-opened (method)
14783   "Check whether a connection to METHOD has been opened."
14784   (when (stringp method)
14785     (setq method (gnus-server-to-method method)))
14786   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14787
14788 (defun gnus-status-message (method)
14789   "Return the status message from METHOD.
14790 If METHOD is a string, it is interpreted as a group name.   The method
14791 this group uses will be queried."
14792   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14793                   method)))
14794     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14795
14796 (defun gnus-request-group (group &optional dont-check method)
14797   "Request GROUP.  If DONT-CHECK, no information is required."
14798   (let ((method (or method (gnus-find-method-for-group group))))
14799     (when (stringp method)
14800       (setq method (gnus-server-to-method method)))
14801     (funcall (gnus-get-function method 'request-group)
14802              (gnus-group-real-name group) (nth 1 method) dont-check)))
14803
14804 (defun gnus-request-asynchronous (group &optional articles)
14805   "Request that GROUP behave asynchronously.
14806 ARTICLES is the `data' of the group."
14807   (let ((method (gnus-find-method-for-group group)))
14808     (funcall (gnus-get-function method 'request-asynchronous)
14809              (gnus-group-real-name group) (nth 1 method) articles)))
14810
14811 (defun gnus-list-active-group (group)
14812   "Request active information on GROUP."
14813   (let ((method (gnus-find-method-for-group group))
14814         (func 'list-active-group))
14815     (when (gnus-check-backend-function func group)
14816       (funcall (gnus-get-function method func)
14817                (gnus-group-real-name group) (nth 1 method)))))
14818
14819 (defun gnus-request-group-description (group)
14820   "Request a description of GROUP."
14821   (let ((method (gnus-find-method-for-group group))
14822         (func 'request-group-description))
14823     (when (gnus-check-backend-function func group)
14824       (funcall (gnus-get-function method func)
14825                (gnus-group-real-name group) (nth 1 method)))))
14826
14827 (defun gnus-close-group (group)
14828   "Request the GROUP be closed."
14829   (let ((method (gnus-find-method-for-group group)))
14830     (funcall (gnus-get-function method 'close-group)
14831              (gnus-group-real-name group) (nth 1 method))))
14832
14833 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14834   "Request headers for ARTICLES in GROUP.
14835 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14836   (let ((method (gnus-find-method-for-group group)))
14837     (if (and gnus-use-cache (numberp (car articles)))
14838         (gnus-cache-retrieve-headers articles group fetch-old)
14839       (funcall (gnus-get-function method 'retrieve-headers)
14840                articles (gnus-group-real-name group) (nth 1 method)
14841                fetch-old))))
14842
14843 (defun gnus-retrieve-groups (groups method)
14844   "Request active information on GROUPS from METHOD."
14845   (when (stringp method)
14846     (setq method (gnus-server-to-method method)))
14847   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14848
14849 (defun gnus-request-type (group &optional article)
14850   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14851   (let ((method (gnus-find-method-for-group group)))
14852     (if (not (gnus-check-backend-function 'request-type (car method)))
14853         'unknown
14854       (funcall (gnus-get-function method 'request-type)
14855                (gnus-group-real-name group) article))))
14856
14857 (defun gnus-request-update-mark (group article mark)
14858   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14859   (let ((method (gnus-find-method-for-group group)))
14860     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14861         mark
14862       (funcall (gnus-get-function method 'request-update-mark)
14863                (gnus-group-real-name group) article mark))))
14864
14865 (defun gnus-request-article (article group &optional buffer)
14866   "Request the ARTICLE in GROUP.
14867 ARTICLE can either be an article number or an article Message-ID.
14868 If BUFFER, insert the article in that group."
14869   (let ((method (gnus-find-method-for-group group)))
14870     (funcall (gnus-get-function method 'request-article)
14871              article (gnus-group-real-name group) (nth 1 method) buffer)))
14872
14873 (defun gnus-request-head (article group)
14874   "Request the head of ARTICLE in GROUP."
14875   (let ((method (gnus-find-method-for-group group)))
14876     (funcall (gnus-get-function method 'request-head)
14877              article (gnus-group-real-name group) (nth 1 method))))
14878
14879 (defun gnus-request-body (article group)
14880   "Request the body of ARTICLE in GROUP."
14881   (let ((method (gnus-find-method-for-group group)))
14882     (funcall (gnus-get-function method 'request-body)
14883              article (gnus-group-real-name group) (nth 1 method))))
14884
14885 (defun gnus-request-post (method)
14886   "Post the current buffer using METHOD."
14887   (when (stringp method)
14888     (setq method (gnus-server-to-method method)))
14889   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14890
14891 (defun gnus-request-scan (group method)
14892   "Request a SCAN being performed in GROUP from METHOD.
14893 If GROUP is nil, all groups on METHOD are scanned."
14894   (let ((method (if group (gnus-find-method-for-group group) method)))
14895     (funcall (gnus-get-function method 'request-scan)
14896              (and group (gnus-group-real-name group)) (nth 1 method))))
14897
14898 (defsubst gnus-request-update-info (info method)
14899   "Request that METHOD update INFO."
14900   (when (stringp method)
14901     (setq method (gnus-server-to-method method)))
14902   (when (gnus-check-backend-function 'request-update-info (car method))
14903     (funcall (gnus-get-function method 'request-update-info)
14904              (gnus-group-real-name (gnus-info-group info))
14905              info (nth 1 method))))
14906
14907 (defun gnus-request-expire-articles (articles group &optional force)
14908   (let ((method (gnus-find-method-for-group group)))
14909     (funcall (gnus-get-function method 'request-expire-articles)
14910              articles (gnus-group-real-name group) (nth 1 method)
14911              force)))
14912
14913 (defun gnus-request-move-article
14914   (article group server accept-function &optional last)
14915   (let ((method (gnus-find-method-for-group group)))
14916     (funcall (gnus-get-function method 'request-move-article)
14917              article (gnus-group-real-name group)
14918              (nth 1 method) accept-function last)))
14919
14920 (defun gnus-request-accept-article (group method &optional last)
14921   ;; Make sure there's a newline at the end of the article.
14922   (when (stringp method)
14923     (setq method (gnus-server-to-method method)))
14924   (when (and (not method)
14925              (stringp group))
14926     (setq method (gnus-find-method-for-group group)))
14927   (goto-char (point-max))
14928   (unless (bolp)
14929     (insert "\n"))
14930   (let ((func (car (or method (gnus-find-method-for-group group)))))
14931     (funcall (intern (format "%s-request-accept-article" func))
14932              (if (stringp group) (gnus-group-real-name group) group)
14933              (cadr method)
14934              last)))
14935
14936 (defun gnus-request-replace-article (article group buffer)
14937   (let ((func (car (gnus-find-method-for-group group))))
14938     (funcall (intern (format "%s-request-replace-article" func))
14939              article (gnus-group-real-name group) buffer)))
14940
14941 (defun gnus-request-associate-buffer (group)
14942   (let ((method (gnus-find-method-for-group group)))
14943     (funcall (gnus-get-function method 'request-associate-buffer)
14944              (gnus-group-real-name group))))
14945
14946 (defun gnus-request-restore-buffer (article group)
14947   "Request a new buffer restored to the state of ARTICLE."
14948   (let ((method (gnus-find-method-for-group group)))
14949     (funcall (gnus-get-function method 'request-restore-buffer)
14950              article (gnus-group-real-name group) (nth 1 method))))
14951
14952 (defun gnus-request-create-group (group &optional method)
14953   (when (stringp method)
14954     (setq method (gnus-server-to-method method)))
14955   (let ((method (or method (gnus-find-method-for-group group))))
14956     (funcall (gnus-get-function method 'request-create-group)
14957              (gnus-group-real-name group) (nth 1 method))))
14958
14959 (defun gnus-request-delete-group (group &optional force)
14960   (let ((method (gnus-find-method-for-group group)))
14961     (funcall (gnus-get-function method 'request-delete-group)
14962              (gnus-group-real-name group) force (nth 1 method))))
14963
14964 (defun gnus-request-rename-group (group new-name)
14965   (let ((method (gnus-find-method-for-group group)))
14966     (funcall (gnus-get-function method 'request-rename-group)
14967              (gnus-group-real-name group)
14968              (gnus-group-real-name new-name) (nth 1 method))))
14969
14970 (defun gnus-member-of-valid (symbol group)
14971   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14972   (memq symbol (assoc
14973                 (symbol-name (car (gnus-find-method-for-group group)))
14974                 gnus-valid-select-methods)))
14975
14976 (defun gnus-method-option-p (method option)
14977   "Return non-nil if select METHOD has OPTION as a parameter."
14978   (when (stringp method)
14979     (setq method (gnus-server-to-method method)))
14980   (memq option (assoc (format "%s" (car method))
14981                       gnus-valid-select-methods)))
14982
14983 (defun gnus-server-extend-method (group method)
14984   ;; This function "extends" a virtual server.  If the server is
14985   ;; "hello", and the select method is ("hello" (my-var "something"))
14986   ;; in the group "alt.alt", this will result in a new virtual server
14987   ;; called "hello+alt.alt".
14988   (let ((entry
14989          (gnus-copy-sequence
14990           (if (equal (car method) "native") gnus-select-method
14991             (cdr (assoc (car method) gnus-server-alist))))))
14992     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14993     (nconc entry (cdr method))))
14994
14995 (defun gnus-find-method-for-group (group &optional info)
14996   "Find the select method that GROUP uses."
14997   (or gnus-override-method
14998       (and (not group)
14999            gnus-select-method)
15000       (let ((info (or info (gnus-get-info group)))
15001             method)
15002         (if (or (not info)
15003                 (not (setq method (gnus-info-method info)))
15004                 (equal method "native"))
15005             gnus-select-method
15006           (setq method
15007                 (cond ((stringp method)
15008                        (gnus-server-to-method method))
15009                       ((stringp (car method))
15010                        (gnus-server-extend-method group method))
15011                       (t
15012                        method)))
15013           (if (equal (cadr method) "")
15014               method
15015             (gnus-server-add-address method))))))
15016
15017 (defun gnus-check-backend-function (func group)
15018   "Check whether GROUP supports function FUNC."
15019   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15020                   group)))
15021     (fboundp (intern (format "%s-%s" method func)))))
15022
15023 (defun gnus-methods-using (feature)
15024   "Find all methods that have FEATURE."
15025   (let ((valids gnus-valid-select-methods)
15026         outs)
15027     (while valids
15028       (if (memq feature (car valids))
15029           (setq outs (cons (car valids) outs)))
15030       (setq valids (cdr valids)))
15031     outs))
15032
15033 \f
15034 ;;;
15035 ;;; Active & Newsrc File Handling
15036 ;;;
15037
15038 (defun gnus-setup-news (&optional rawfile level dont-connect)
15039   "Setup news information.
15040 If RAWFILE is non-nil, the .newsrc file will also be read.
15041 If LEVEL is non-nil, the news will be set up at level LEVEL."
15042   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15043
15044     (when init 
15045       ;; Clear some variables to re-initialize news information.
15046       (setq gnus-newsrc-alist nil
15047             gnus-active-hashtb nil)
15048       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15049       (gnus-read-newsrc-file rawfile))
15050
15051     (when (and (not (assoc "archive" gnus-server-alist))
15052                gnus-message-archive-method)
15053       (push (cons "archive" gnus-message-archive-method)
15054             gnus-server-alist))
15055
15056     ;; If we don't read the complete active file, we fill in the
15057     ;; hashtb here.
15058     (if (or (null gnus-read-active-file)
15059             (eq gnus-read-active-file 'some))
15060         (gnus-update-active-hashtb-from-killed))
15061
15062     ;; Read the active file and create `gnus-active-hashtb'.
15063     ;; If `gnus-read-active-file' is nil, then we just create an empty
15064     ;; hash table.  The partial filling out of the hash table will be
15065     ;; done in `gnus-get-unread-articles'.
15066     (and gnus-read-active-file
15067          (not level)
15068          (gnus-read-active-file))
15069
15070     (or gnus-active-hashtb
15071         (setq gnus-active-hashtb (make-vector 4095 0)))
15072
15073     ;; Initialize the cache.
15074     (when gnus-use-cache
15075       (gnus-cache-open))
15076
15077     ;; Possibly eval the dribble file.
15078     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15079
15080     (gnus-update-format-specifications)
15081
15082     ;; See whether we need to read the description file.
15083     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15084              (not gnus-description-hashtb)
15085              (not dont-connect)
15086              gnus-read-active-file)
15087         (gnus-read-all-descriptions-files))
15088
15089     ;; Find new newsgroups and treat them.
15090     (if (and init gnus-check-new-newsgroups (not level)
15091              (gnus-check-server gnus-select-method))
15092         (gnus-find-new-newsgroups))
15093
15094     ;; We might read in new NoCeM messages here.
15095     (when gnus-use-nocem 
15096       (gnus-nocem-scan-groups))
15097
15098     ;; Find the number of unread articles in each non-dead group.
15099     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15100       (gnus-get-unread-articles level))
15101
15102     (if (and init gnus-check-bogus-newsgroups
15103              gnus-read-active-file (not level)
15104              (gnus-server-opened gnus-select-method))
15105         (gnus-check-bogus-newsgroups))))
15106
15107 (defun gnus-find-new-newsgroups (&optional arg)
15108   "Search for new newsgroups and add them.
15109 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15110 The `-n' option line from .newsrc is respected.
15111 If ARG (the prefix), use the `ask-server' method to query
15112 the server for new groups."
15113   (interactive "P")
15114   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15115                        (null gnus-read-active-file)
15116                        (eq gnus-read-active-file 'some))
15117                    'ask-server gnus-check-new-newsgroups)))
15118     (unless (gnus-check-first-time-used)
15119       (if (or (consp check)
15120               (eq check 'ask-server))
15121           ;; Ask the server for new groups.
15122           (gnus-ask-server-for-new-groups)
15123         ;; Go through the active hashtb and look for new groups.
15124         (let ((groups 0)
15125               group new-newsgroups)
15126           (gnus-message 5 "Looking for new newsgroups...")
15127           (unless gnus-have-read-active-file
15128             (gnus-read-active-file))
15129           (setq gnus-newsrc-last-checked-date (current-time-string))
15130           (unless gnus-killed-hashtb
15131             (gnus-make-hashtable-from-killed))
15132           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15133           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15134           (mapatoms
15135            (lambda (sym)
15136              (if (or (null (setq group (symbol-name sym)))
15137                      (not (boundp sym))
15138                      (null (symbol-value sym))
15139                      (gnus-gethash group gnus-killed-hashtb)
15140                      (gnus-gethash group gnus-newsrc-hashtb))
15141                  ()
15142                (let ((do-sub (gnus-matches-options-n group)))
15143                  (cond
15144                   ((eq do-sub 'subscribe)
15145                    (setq groups (1+ groups))
15146                    (gnus-sethash group group gnus-killed-hashtb)
15147                    (funcall gnus-subscribe-options-newsgroup-method group))
15148                   ((eq do-sub 'ignore)
15149                    nil)
15150                   (t
15151                    (setq groups (1+ groups))
15152                    (gnus-sethash group group gnus-killed-hashtb)
15153                    (if gnus-subscribe-hierarchical-interactive
15154                        (setq new-newsgroups (cons group new-newsgroups))
15155                      (funcall gnus-subscribe-newsgroup-method group)))))))
15156            gnus-active-hashtb)
15157           (when new-newsgroups
15158             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15159           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15160           (if (> groups 0)
15161               (gnus-message 6 "%d new newsgroup%s arrived."
15162                             groups (if (> groups 1) "s have" " has"))
15163             (gnus-message 6 "No new newsgroups.")))))))
15164
15165 (defun gnus-matches-options-n (group)
15166   ;; Returns `subscribe' if the group is to be unconditionally
15167   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15168   ;; no match for the group.
15169
15170   ;; First we check the two user variables.
15171   (cond
15172    ((and gnus-options-subscribe
15173          (string-match gnus-options-subscribe group))
15174     'subscribe)
15175    ((and gnus-auto-subscribed-groups
15176          (string-match gnus-auto-subscribed-groups group))
15177     'subscribe)
15178    ((and gnus-options-not-subscribe
15179          (string-match gnus-options-not-subscribe group))
15180     'ignore)
15181    ;; Then we go through the list that was retrieved from the .newsrc
15182    ;; file.  This list has elements on the form
15183    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15184    ;; is in the reverse order of the options line) is returned.
15185    (t
15186     (let ((regs gnus-newsrc-options-n))
15187       (while (and regs
15188                   (not (string-match (caar regs) group)))
15189         (setq regs (cdr regs)))
15190       (and regs (cdar regs))))))
15191
15192 (defun gnus-ask-server-for-new-groups ()
15193   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15194          (methods (cons gnus-select-method
15195                         (nconc
15196                          (when gnus-message-archive-method
15197                            (list "archive"))
15198                          (append
15199                           (and (consp gnus-check-new-newsgroups)
15200                                gnus-check-new-newsgroups)
15201                           gnus-secondary-select-methods))))
15202          (groups 0)
15203          (new-date (current-time-string))
15204          group new-newsgroups got-new method hashtb
15205          gnus-override-subscribe-method)
15206     ;; Go through both primary and secondary select methods and
15207     ;; request new newsgroups.
15208     (while (setq method (gnus-server-get-method nil (pop methods)))
15209       (setq new-newsgroups nil)
15210       (setq gnus-override-subscribe-method method)
15211       (when (and (gnus-check-server method)
15212                  (gnus-request-newgroups date method))
15213         (save-excursion
15214           (setq got-new t)
15215           (setq hashtb (gnus-make-hashtable 100))
15216           (set-buffer nntp-server-buffer)
15217           ;; Enter all the new groups into a hashtable.
15218           (gnus-active-to-gnus-format method hashtb 'ignore)))
15219       ;; Now all new groups from `method' are in `hashtb'.
15220       (mapatoms
15221        (lambda (group-sym)
15222          (if (or (null (setq group (symbol-name group-sym)))
15223                  (null (symbol-value group-sym))
15224                  (gnus-gethash group gnus-newsrc-hashtb)
15225                  (member group gnus-zombie-list)
15226                  (member group gnus-killed-list))
15227              ;; The group is already known.
15228              ()
15229            ;; Make this group active.
15230            (when (symbol-value group-sym)
15231              (gnus-set-active group (symbol-value group-sym)))
15232            ;; Check whether we want it or not.
15233            (let ((do-sub (gnus-matches-options-n group)))
15234              (cond
15235               ((eq do-sub 'subscribe)
15236                (incf groups)
15237                (gnus-sethash group group gnus-killed-hashtb)
15238                (funcall gnus-subscribe-options-newsgroup-method group))
15239               ((eq do-sub 'ignore)
15240                nil)
15241               (t
15242                (incf groups)
15243                (gnus-sethash group group gnus-killed-hashtb)
15244                (if gnus-subscribe-hierarchical-interactive
15245                    (push group new-newsgroups)
15246                  (funcall gnus-subscribe-newsgroup-method group)))))))
15247        hashtb)
15248       (when new-newsgroups
15249         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15250     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15251     (when (> groups 0)
15252       (gnus-message 6 "%d new newsgroup%s arrived."
15253                     groups (if (> groups 1) "s have" " has")))
15254     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15255     got-new))
15256
15257 (defun gnus-check-first-time-used ()
15258   (if (or (> (length gnus-newsrc-alist) 1)
15259           (file-exists-p gnus-startup-file)
15260           (file-exists-p (concat gnus-startup-file ".el"))
15261           (file-exists-p (concat gnus-startup-file ".eld")))
15262       nil
15263     (gnus-message 6 "First time user; subscribing you to default groups")
15264     (unless gnus-have-read-active-file
15265       (gnus-read-active-file))
15266     (setq gnus-newsrc-last-checked-date (current-time-string))
15267     (let ((groups gnus-default-subscribed-newsgroups)
15268           group)
15269       (if (eq groups t)
15270           nil
15271         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15272         (mapatoms
15273          (lambda (sym)
15274            (if (null (setq group (symbol-name sym)))
15275                ()
15276              (let ((do-sub (gnus-matches-options-n group)))
15277                (cond
15278                 ((eq do-sub 'subscribe)
15279                  (gnus-sethash group group gnus-killed-hashtb)
15280                  (funcall gnus-subscribe-options-newsgroup-method group))
15281                 ((eq do-sub 'ignore)
15282                  nil)
15283                 (t
15284                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15285          gnus-active-hashtb)
15286         (while groups
15287           (if (gnus-active (car groups))
15288               (gnus-group-change-level
15289                (car groups) gnus-level-default-subscribed gnus-level-killed))
15290           (setq groups (cdr groups)))
15291         (gnus-group-make-help-group)
15292         (and gnus-novice-user
15293              (gnus-message 7 "`A k' to list killed groups"))))))
15294
15295 (defun gnus-subscribe-group (group previous &optional method)
15296   (gnus-group-change-level
15297    (if method
15298        (list t group gnus-level-default-subscribed nil nil method)
15299      group)
15300    gnus-level-default-subscribed gnus-level-killed previous t))
15301
15302 ;; `gnus-group-change-level' is the fundamental function for changing
15303 ;; subscription levels of newsgroups.  This might mean just changing
15304 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15305 ;; again, which subscribes/unsubscribes a group, which is equally
15306 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15307 ;; from 8-9 to 1-7 means that you remove the group from the list of
15308 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15309 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15310 ;; which is trivial.
15311 ;; ENTRY can either be a string (newsgroup name) or a list (if
15312 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15313 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15314 ;; entries.
15315 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15316 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15317 ;; after.
15318 (defun gnus-group-change-level (entry level &optional oldlevel
15319                                       previous fromkilled)
15320   (let (group info active num)
15321     ;; Glean what info we can from the arguments
15322     (if (consp entry)
15323         (if fromkilled (setq group (nth 1 entry))
15324           (setq group (car (nth 2 entry))))
15325       (setq group entry))
15326     (if (and (stringp entry)
15327              oldlevel
15328              (< oldlevel gnus-level-zombie))
15329         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15330     (if (and (not oldlevel)
15331              (consp entry))
15332         (setq oldlevel (gnus-info-level (nth 2 entry)))
15333       (setq oldlevel (or oldlevel 9)))
15334     (if (stringp previous)
15335         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15336
15337     (if (and (>= oldlevel gnus-level-zombie)
15338              (gnus-gethash group gnus-newsrc-hashtb))
15339         ;; We are trying to subscribe a group that is already
15340         ;; subscribed.
15341         ()                              ; Do nothing.
15342
15343       (or (gnus-ephemeral-group-p group)
15344           (gnus-dribble-enter
15345            (format "(gnus-group-change-level %S %S %S %S %S)"
15346                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15347
15348       ;; Then we remove the newgroup from any old structures, if needed.
15349       ;; If the group was killed, we remove it from the killed or zombie
15350       ;; list.  If not, and it is in fact going to be killed, we remove
15351       ;; it from the newsrc hash table and assoc.
15352       (cond
15353        ((>= oldlevel gnus-level-zombie)
15354         (if (= oldlevel gnus-level-zombie)
15355             (setq gnus-zombie-list (delete group gnus-zombie-list))
15356           (setq gnus-killed-list (delete group gnus-killed-list))))
15357        (t
15358         (if (and (>= level gnus-level-zombie)
15359                  entry)
15360             (progn
15361               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15362               (if (nth 3 entry)
15363                   (setcdr (gnus-gethash (car (nth 3 entry))
15364                                         gnus-newsrc-hashtb)
15365                           (cdr entry)))
15366               (setcdr (cdr entry) (cdddr entry))))))
15367
15368       ;; Finally we enter (if needed) the list where it is supposed to
15369       ;; go, and change the subscription level.  If it is to be killed,
15370       ;; we enter it into the killed or zombie list.
15371       (cond 
15372        ((>= level gnus-level-zombie)
15373         ;; Remove from the hash table.
15374         (gnus-sethash group nil gnus-newsrc-hashtb)
15375         ;; We do not enter foreign groups into the list of dead
15376         ;; groups.
15377         (unless (gnus-group-foreign-p group)
15378           (if (= level gnus-level-zombie)
15379               (setq gnus-zombie-list (cons group gnus-zombie-list))
15380             (setq gnus-killed-list (cons group gnus-killed-list)))))
15381        (t
15382         ;; If the list is to be entered into the newsrc assoc, and
15383         ;; it was killed, we have to create an entry in the newsrc
15384         ;; hashtb format and fix the pointers in the newsrc assoc.
15385         (if (< oldlevel gnus-level-zombie)
15386             ;; It was alive, and it is going to stay alive, so we
15387             ;; just change the level and don't change any pointers or
15388             ;; hash table entries.
15389             (setcar (cdaddr entry) level)
15390           (if (listp entry)
15391               (setq info (cdr entry)
15392                     num (car entry))
15393             (setq active (gnus-active group))
15394             (setq num
15395                   (if active (- (1+ (cdr active)) (car active)) t))
15396             ;; Check whether the group is foreign.  If so, the
15397             ;; foreign select method has to be entered into the
15398             ;; info.
15399             (let ((method (or gnus-override-subscribe-method
15400                               (gnus-group-method group))))
15401               (if (eq method gnus-select-method)
15402                   (setq info (list group level nil))
15403                 (setq info (list group level nil nil method)))))
15404           (unless previous
15405             (setq previous
15406                   (let ((p gnus-newsrc-alist))
15407                     (while (cddr p)
15408                       (setq p (cdr p)))
15409                     p)))
15410           (setq entry (cons info (cddr previous)))
15411           (if (cdr previous)
15412               (progn
15413                 (setcdr (cdr previous) entry)
15414                 (gnus-sethash group (cons num (cdr previous))
15415                               gnus-newsrc-hashtb))
15416             (setcdr previous entry)
15417             (gnus-sethash group (cons num previous)
15418                           gnus-newsrc-hashtb))
15419           (when (cdr entry)
15420             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15421       (when gnus-group-change-level-function
15422         (funcall gnus-group-change-level-function group level oldlevel)))))
15423
15424 (defun gnus-kill-newsgroup (newsgroup)
15425   "Obsolete function.  Kills a newsgroup."
15426   (gnus-group-change-level
15427    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15428
15429 (defun gnus-check-bogus-newsgroups (&optional confirm)
15430   "Remove bogus newsgroups.
15431 If CONFIRM is non-nil, the user has to confirm the deletion of every
15432 newsgroup."
15433   (let ((newsrc (cdr gnus-newsrc-alist))
15434         bogus group entry info)
15435     (gnus-message 5 "Checking bogus newsgroups...")
15436     (unless gnus-have-read-active-file
15437       (gnus-read-active-file))
15438     (when (member gnus-select-method gnus-have-read-active-file)
15439       ;; Find all bogus newsgroup that are subscribed.
15440       (while newsrc
15441         (setq info (pop newsrc)
15442               group (gnus-info-group info))
15443         (unless (or (gnus-active group) ; Active
15444                     (gnus-info-method info) ; Foreign
15445                     (and confirm
15446                          (not (gnus-y-or-n-p
15447                                (format "Remove bogus newsgroup: %s " group)))))
15448           ;; Found a bogus newsgroup.
15449           (push group bogus)))
15450       ;; Remove all bogus subscribed groups by first killing them, and
15451       ;; then removing them from the list of killed groups.
15452       (while bogus
15453         (when (setq entry (gnus-gethash (setq group (pop bogus))
15454                                         gnus-newsrc-hashtb))
15455           (gnus-group-change-level entry gnus-level-killed)
15456           (setq gnus-killed-list (delete group gnus-killed-list))))
15457       ;; Then we remove all bogus groups from the list of killed and
15458       ;; zombie groups.  They are are removed without confirmation.
15459       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15460             killed)
15461         (while dead-lists
15462           (setq killed (symbol-value (car dead-lists)))
15463           (while killed
15464             (unless (gnus-active (setq group (pop killed)))
15465               ;; The group is bogus.
15466               ;; !!!Slow as hell.
15467               (set (car dead-lists)
15468                    (delete group (symbol-value (car dead-lists))))))
15469           (setq dead-lists (cdr dead-lists))))
15470       (gnus-message 5 "Checking bogus newsgroups...done"))))
15471
15472 (defun gnus-check-duplicate-killed-groups ()
15473   "Remove duplicates from the list of killed groups."
15474   (interactive)
15475   (let ((killed gnus-killed-list))
15476     (while killed
15477       (gnus-message 9 "%d" (length killed))
15478       (setcdr killed (delete (car killed) (cdr killed)))
15479       (setq killed (cdr killed)))))
15480
15481 ;; We want to inline a function from gnus-cache, so we cheat here:
15482 (eval-when-compile
15483   (provide 'gnus)
15484   (require 'gnus-cache))
15485
15486 (defun gnus-get-unread-articles-in-group (info active &optional update)
15487   (when active
15488     ;; Allow the backend to update the info in the group.
15489     (when (and update 
15490                (gnus-request-update-info
15491                 info (gnus-find-method-for-group (gnus-info-group info))))
15492       (gnus-activate-group (gnus-info-group info) nil t))
15493     (let* ((range (gnus-info-read info))
15494            (num 0))
15495       ;; If a cache is present, we may have to alter the active info.
15496       (when (and gnus-use-cache info)
15497         (inline (gnus-cache-possibly-alter-active 
15498                  (gnus-info-group info) active)))
15499       ;; Modify the list of read articles according to what articles
15500       ;; are available; then tally the unread articles and add the
15501       ;; number to the group hash table entry.
15502       (cond
15503        ((zerop (cdr active))
15504         (setq num 0))
15505        ((not range)
15506         (setq num (- (1+ (cdr active)) (car active))))
15507        ((not (listp (cdr range)))
15508         ;; Fix a single (num . num) range according to the
15509         ;; active hash table.
15510         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15511         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15512         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15513         ;; Compute number of unread articles.
15514         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15515        (t
15516         ;; The read list is a list of ranges.  Fix them according to
15517         ;; the active hash table.
15518         ;; First peel off any elements that are below the lower
15519         ;; active limit.
15520         (while (and (cdr range)
15521                     (>= (car active)
15522                         (or (and (atom (cadr range)) (cadr range))
15523                             (caadr range))))
15524           (if (numberp (car range))
15525               (setcar range
15526                       (cons (car range)
15527                             (or (and (numberp (cadr range))
15528                                      (cadr range))
15529                                 (cdadr range))))
15530             (setcdr (car range)
15531                     (or (and (numberp (nth 1 range)) (nth 1 range))
15532                         (cdadr range))))
15533           (setcdr range (cddr range)))
15534         ;; Adjust the first element to be the same as the lower limit.
15535         (if (and (not (atom (car range)))
15536                  (< (cdar range) (car active)))
15537             (setcdr (car range) (1- (car active))))
15538         ;; Then we want to peel off any elements that are higher
15539         ;; than the upper active limit.
15540         (let ((srange range))
15541           ;; Go past all legal elements.
15542           (while (and (cdr srange)
15543                       (<= (or (and (atom (cadr srange))
15544                                    (cadr srange))
15545                               (caadr srange)) (cdr active)))
15546             (setq srange (cdr srange)))
15547           (if (cdr srange)
15548               ;; Nuke all remaining illegal elements.
15549               (setcdr srange nil))
15550
15551           ;; Adjust the final element.
15552           (if (and (not (atom (car srange)))
15553                    (> (cdar srange) (cdr active)))
15554               (setcdr (car srange) (cdr active))))
15555         ;; Compute the number of unread articles.
15556         (while range
15557           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15558                                       (cdar range)))
15559                               (or (and (atom (car range)) (car range))
15560                                   (caar range)))))
15561           (setq range (cdr range)))
15562         (setq num (max 0 (- (cdr active) num)))))
15563       ;; Set the number of unread articles.
15564       (when info
15565         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15566       num)))
15567
15568 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15569 ;; and compute how many unread articles there are in each group.
15570 (defun gnus-get-unread-articles (&optional level)
15571   (let* ((newsrc (cdr gnus-newsrc-alist))
15572          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15573          (foreign-level
15574           (min
15575            (cond ((and gnus-activate-foreign-newsgroups
15576                        (not (numberp gnus-activate-foreign-newsgroups)))
15577                   (1+ gnus-level-subscribed))
15578                  ((numberp gnus-activate-foreign-newsgroups)
15579                   gnus-activate-foreign-newsgroups)
15580                  (t 0))
15581            level))
15582          info group active method)
15583     (gnus-message 5 "Checking new news...")
15584
15585     (while newsrc
15586       (setq active (gnus-active (setq group (gnus-info-group
15587                                              (setq info (pop newsrc))))))
15588
15589       ;; Check newsgroups.  If the user doesn't want to check them, or
15590       ;; they can't be checked (for instance, if the news server can't
15591       ;; be reached) we just set the number of unread articles in this
15592       ;; newsgroup to t.  This means that Gnus thinks that there are
15593       ;; unread articles, but it has no idea how many.
15594       (if (and (setq method (gnus-info-method info))
15595                (not (gnus-server-equal
15596                      gnus-select-method
15597                      (setq method (gnus-server-get-method nil method))))
15598                (not (gnus-secondary-method-p method)))
15599           ;; These groups are foreign.  Check the level.
15600           (when (<= (gnus-info-level info) foreign-level)
15601             (setq active (gnus-activate-group group 'scan))
15602             (unless (inline (gnus-virtual-group-p group))
15603               (inline (gnus-close-group group))))
15604
15605         ;; These groups are native or secondary.
15606         (when (and (<= (gnus-info-level info) level)
15607                    (not gnus-read-active-file))
15608           (setq active (gnus-activate-group group 'scan))
15609           (inline (gnus-close-group group))))
15610
15611       (if active
15612           (inline (gnus-get-unread-articles-in-group 
15613                    info active
15614                    (and method
15615                         (fboundp (intern (concat (symbol-name (car method))
15616                                                  "-request-update-info"))))))
15617         ;; The group couldn't be reached, so we nix out the number of
15618         ;; unread articles and stuff.
15619         (gnus-set-active group nil)
15620         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15621
15622     (gnus-message 5 "Checking new news...done")))
15623
15624 ;; Create a hash table out of the newsrc alist.  The `car's of the
15625 ;; alist elements are used as keys.
15626 (defun gnus-make-hashtable-from-newsrc-alist ()
15627   (let ((alist gnus-newsrc-alist)
15628         (ohashtb gnus-newsrc-hashtb)
15629         prev)
15630     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15631     (setq alist
15632           (setq prev (setq gnus-newsrc-alist
15633                            (if (equal (caar gnus-newsrc-alist)
15634                                       "dummy.group")
15635                                gnus-newsrc-alist
15636                              (cons (list "dummy.group" 0 nil) alist)))))
15637     (while alist
15638       (gnus-sethash
15639        (caar alist)
15640        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15641              prev)
15642        gnus-newsrc-hashtb)
15643       (setq prev alist
15644             alist (cdr alist)))))
15645
15646 (defun gnus-make-hashtable-from-killed ()
15647   "Create a hash table from the killed and zombie lists."
15648   (let ((lists '(gnus-killed-list gnus-zombie-list))
15649         list)
15650     (setq gnus-killed-hashtb
15651           (gnus-make-hashtable
15652            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15653     (while (setq list (pop lists))
15654       (setq list (symbol-value list))
15655       (while list
15656         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15657
15658 (defun gnus-activate-group (group &optional scan dont-check)
15659   ;; Check whether a group has been activated or not.
15660   ;; If SCAN, request a scan of that group as well.
15661   (let ((method (gnus-find-method-for-group group))
15662         active)
15663     (and (gnus-check-server method)
15664          ;; We escape all bugs and quit here to make it possible to
15665          ;; continue if a group is so out-there that it reports bugs
15666          ;; and stuff.
15667          (progn
15668            (and scan
15669                 (gnus-check-backend-function 'request-scan (car method))
15670                 (gnus-request-scan group method))
15671            t)
15672          (condition-case ()
15673              (gnus-request-group group dont-check)
15674         ;   (error nil)
15675            (quit nil))
15676          (save-excursion
15677            (set-buffer nntp-server-buffer)
15678            (goto-char (point-min))
15679            ;; Parse the result we got from `gnus-request-group'.
15680            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15681                 (progn
15682                   (goto-char (match-beginning 1))
15683                   (gnus-set-active
15684                    group (setq active (cons (read (current-buffer))
15685                                             (read (current-buffer)))))
15686                   ;; Return the new active info.
15687                   active))))))
15688
15689 (defun gnus-update-read-articles (group unread)
15690   "Update the list of read and ticked articles in GROUP using the
15691 UNREAD and TICKED lists.
15692 Note: UNSELECTED has to be sorted over `<'.
15693 Returns whether the updating was successful."
15694   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15695          (entry (gnus-gethash group gnus-newsrc-hashtb))
15696          (info (nth 2 entry))
15697          (prev 1)
15698          (unread (sort (copy-sequence unread) '<))
15699          read)
15700     (if (or (not info) (not active))
15701         ;; There is no info on this group if it was, in fact,
15702         ;; killed.  Gnus stores no information on killed groups, so
15703         ;; there's nothing to be done.
15704         ;; One could store the information somewhere temporarily,
15705         ;; perhaps...  Hmmm...
15706         ()
15707       ;; Remove any negative articles numbers.
15708       (while (and unread (< (car unread) 0))
15709         (setq unread (cdr unread)))
15710       ;; Remove any expired article numbers
15711       (while (and unread (< (car unread) (car active)))
15712         (setq unread (cdr unread)))
15713       ;; Compute the ranges of read articles by looking at the list of
15714       ;; unread articles.
15715       (while unread
15716         (if (/= (car unread) prev)
15717             (setq read (cons (if (= prev (1- (car unread))) prev
15718                                (cons prev (1- (car unread)))) read)))
15719         (setq prev (1+ (car unread)))
15720         (setq unread (cdr unread)))
15721       (when (<= prev (cdr active))
15722         (setq read (cons (cons prev (cdr active)) read)))
15723       ;; Enter this list into the group info.
15724       (gnus-info-set-read
15725        info (if (> (length read) 1) (nreverse read) read))
15726       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15727       (gnus-get-unread-articles-in-group info (gnus-active group))
15728       t)))
15729
15730 (defun gnus-make-articles-unread (group articles)
15731   "Mark ARTICLES in GROUP as unread."
15732   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15733                           (gnus-gethash (gnus-group-real-name group)
15734                                         gnus-newsrc-hashtb))))
15735          (ranges (gnus-info-read info))
15736          news article)
15737     (while articles
15738       (when (gnus-member-of-range
15739              (setq article (pop articles)) ranges)
15740         (setq news (cons article news))))
15741     (when news
15742       (gnus-info-set-read
15743        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15744       (gnus-group-update-group group t))))
15745
15746 ;; Enter all dead groups into the hashtb.
15747 (defun gnus-update-active-hashtb-from-killed ()
15748   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15749         (lists (list gnus-killed-list gnus-zombie-list))
15750         killed)
15751     (while lists
15752       (setq killed (car lists))
15753       (while killed
15754         (gnus-sethash (car killed) nil hashtb)
15755         (setq killed (cdr killed)))
15756       (setq lists (cdr lists)))))
15757
15758 ;; Get the active file(s) from the backend(s).
15759 (defun gnus-read-active-file ()
15760   (gnus-group-set-mode-line)
15761   (let ((methods 
15762          (append
15763           (if (gnus-check-server gnus-select-method)
15764               ;; The native server is available.
15765               (cons gnus-select-method gnus-secondary-select-methods)
15766             ;; The native server is down, so we just do the
15767             ;; secondary ones.
15768             gnus-secondary-select-methods)
15769           ;; Also read from the archive server.
15770           (when gnus-message-archive-method
15771             (list "archive"))))
15772         list-type)
15773     (setq gnus-have-read-active-file nil)
15774     (save-excursion
15775       (set-buffer nntp-server-buffer)
15776       (while methods
15777         (let* ((method (if (stringp (car methods))
15778                            (gnus-server-get-method nil (car methods))
15779                          (car methods)))
15780                (where (nth 1 method))
15781                (mesg (format "Reading active file%s via %s..."
15782                              (if (and where (not (zerop (length where))))
15783                                  (concat " from " where) "")
15784                              (car method))))
15785           (gnus-message 5 mesg)
15786           (when (gnus-check-server method)
15787             ;; Request that the backend scan its incoming messages.
15788             (and (gnus-check-backend-function 'request-scan (car method))
15789                  (gnus-request-scan nil method))
15790             (cond
15791              ((and (eq gnus-read-active-file 'some)
15792                    (gnus-check-backend-function 'retrieve-groups (car method)))
15793               (let ((newsrc (cdr gnus-newsrc-alist))
15794                     (gmethod (gnus-server-get-method nil method))
15795                     groups info)
15796                 (while (setq info (pop newsrc))
15797                   (when (gnus-server-equal
15798                          (gnus-find-method-for-group 
15799                           (gnus-info-group info) info)
15800                          gmethod)
15801                     (push (gnus-group-real-name (gnus-info-group info)) 
15802                           groups)))
15803                 (when groups
15804                   (gnus-check-server method)
15805                   (setq list-type (gnus-retrieve-groups groups method))
15806                   (cond
15807                    ((not list-type)
15808                     (gnus-message
15809                      1 "Cannot read partial active file from %s server."
15810                      (car method))
15811                     (ding)
15812                     (sit-for 2))
15813                    ((eq list-type 'active)
15814                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15815                    (t
15816                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15817              (t
15818               (if (not (gnus-request-list method))
15819                   (progn
15820                     (unless (equal method gnus-message-archive-method)
15821                       (gnus-message 1 "Cannot read active file from %s server."
15822                                     (car method))
15823                       (ding)))
15824                 (gnus-active-to-gnus-format method)
15825                 ;; We mark this active file as read.
15826                 (push method gnus-have-read-active-file)
15827                 (gnus-message 5 "%sdone" mesg))))))
15828         (setq methods (cdr methods))))))
15829
15830 ;; Read an active file and place the results in `gnus-active-hashtb'.
15831 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15832   (unless method
15833     (setq method gnus-select-method))
15834   (let ((cur (current-buffer))
15835         (hashtb (or hashtb
15836                     (if (and gnus-active-hashtb
15837                              (not (equal method gnus-select-method)))
15838                         gnus-active-hashtb
15839                       (setq gnus-active-hashtb
15840                             (if (equal method gnus-select-method)
15841                                 (gnus-make-hashtable
15842                                  (count-lines (point-min) (point-max)))
15843                               (gnus-make-hashtable 4096)))))))
15844     ;; Delete unnecessary lines.
15845     (goto-char (point-min))
15846     (while (search-forward "\nto." nil t)
15847       (delete-region (1+ (match-beginning 0))
15848                      (progn (forward-line 1) (point))))
15849     (or (string= gnus-ignored-newsgroups "")
15850         (progn
15851           (goto-char (point-min))
15852           (delete-matching-lines gnus-ignored-newsgroups)))
15853     ;; Make the group names readable as a lisp expression even if they
15854     ;; contain special characters.
15855     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15856     (goto-char (point-max))
15857     (while (re-search-backward "[][';?()#]" nil t)
15858       (insert ?\\))
15859     ;; If these are groups from a foreign select method, we insert the
15860     ;; group prefix in front of the group names.
15861     (and method (not (gnus-server-equal
15862                       (gnus-server-get-method nil method)
15863                       (gnus-server-get-method nil gnus-select-method)))
15864          (let ((prefix (gnus-group-prefixed-name "" method)))
15865            (goto-char (point-min))
15866            (while (and (not (eobp))
15867                        (progn (insert prefix)
15868                               (zerop (forward-line 1)))))))
15869     ;; Store the active file in a hash table.
15870     (goto-char (point-min))
15871     (if (string-match "%[oO]" gnus-group-line-format)
15872         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15873         ;; If we want information on moderated groups, we use this
15874         ;; loop...
15875         (let* ((mod-hashtb (make-vector 7 0))
15876                (m (intern "m" mod-hashtb))
15877                group max min)
15878           (while (not (eobp))
15879             (condition-case nil
15880                 (progn
15881                   (narrow-to-region (point) (gnus-point-at-eol))
15882                   (setq group (let ((obarray hashtb)) (read cur)))
15883                   (if (and (numberp (setq max (read cur)))
15884                            (numberp (setq min (read cur)))
15885                            (progn
15886                              (skip-chars-forward " \t")
15887                              (not
15888                               (or (= (following-char) ?=)
15889                                   (= (following-char) ?x)
15890                                   (= (following-char) ?j)))))
15891                       (set group (cons min max))
15892                     (set group nil))
15893                   ;; Enter moderated groups into a list.
15894                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15895                       (setq gnus-moderated-list
15896                             (cons (symbol-name group) gnus-moderated-list))))
15897               (error
15898                (and group
15899                     (symbolp group)
15900                     (set group nil))))
15901             (widen)
15902             (forward-line 1)))
15903       ;; And if we do not care about moderation, we use this loop,
15904       ;; which is faster.
15905       (let (group max min)
15906         (while (not (eobp))
15907           (condition-case ()
15908               (progn
15909                 (narrow-to-region (point) (gnus-point-at-eol))
15910                 ;; group gets set to a symbol interned in the hash table
15911                 ;; (what a hack!!) - jwz
15912                 (setq group (let ((obarray hashtb)) (read cur)))
15913                 (if (and (numberp (setq max (read cur)))
15914                          (numberp (setq min (read cur)))
15915                          (progn
15916                            (skip-chars-forward " \t")
15917                            (not
15918                             (or (= (following-char) ?=)
15919                                 (= (following-char) ?x)
15920                                 (= (following-char) ?j)))))
15921                     (set group (cons min max))
15922                   (set group nil)))
15923             (error
15924              (progn
15925                (and group
15926                     (symbolp group)
15927                     (set group nil))
15928                (or ignore-errors
15929                    (gnus-message 3 "Warning - illegal active: %s"
15930                                  (buffer-substring
15931                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15932           (widen)
15933           (forward-line 1))))))
15934
15935 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15936   ;; Parse a "groups" active file.
15937   (let ((cur (current-buffer))
15938         (hashtb (or hashtb
15939                     (if (and method gnus-active-hashtb)
15940                         gnus-active-hashtb
15941                       (setq gnus-active-hashtb
15942                             (gnus-make-hashtable
15943                              (count-lines (point-min) (point-max)))))))
15944         (prefix (and method
15945                      (not (gnus-server-equal
15946                            (gnus-server-get-method nil method)
15947                            (gnus-server-get-method nil gnus-select-method)))
15948                      (gnus-group-prefixed-name "" method))))
15949
15950     (goto-char (point-min))
15951     ;; We split this into to separate loops, one with the prefix
15952     ;; and one without to speed the reading up somewhat.
15953     (if prefix
15954         (let (min max opoint group)
15955           (while (not (eobp))
15956             (condition-case ()
15957                 (progn
15958                   (read cur) (read cur)
15959                   (setq min (read cur)
15960                         max (read cur)
15961                         opoint (point))
15962                   (skip-chars-forward " \t")
15963                   (insert prefix)
15964                   (goto-char opoint)
15965                   (set (let ((obarray hashtb)) (read cur))
15966                        (cons min max)))
15967               (error (and group (symbolp group) (set group nil))))
15968             (forward-line 1)))
15969       (let (min max group)
15970         (while (not (eobp))
15971           (condition-case ()
15972               (if (= (following-char) ?2)
15973                   (progn
15974                     (read cur) (read cur)
15975                     (setq min (read cur)
15976                           max (read cur))
15977                     (set (setq group (let ((obarray hashtb)) (read cur)))
15978                          (cons min max))))
15979             (error (and group (symbolp group) (set group nil))))
15980           (forward-line 1))))))
15981
15982 (defun gnus-read-newsrc-file (&optional force)
15983   "Read startup file.
15984 If FORCE is non-nil, the .newsrc file is read."
15985   ;; Reset variables that might be defined in the .newsrc.eld file.
15986   (let ((variables gnus-variable-list))
15987     (while variables
15988       (set (car variables) nil)
15989       (setq variables (cdr variables))))
15990   (let* ((newsrc-file gnus-current-startup-file)
15991          (quick-file (concat newsrc-file ".el")))
15992     (save-excursion
15993       ;; We always load the .newsrc.eld file.  If always contains
15994       ;; much information that can not be gotten from the .newsrc
15995       ;; file (ticked articles, killed groups, foreign methods, etc.)
15996       (gnus-read-newsrc-el-file quick-file)
15997
15998       (if (and (file-exists-p gnus-current-startup-file)
15999                (or force
16000                    (and (file-newer-than-file-p newsrc-file quick-file)
16001                         (file-newer-than-file-p newsrc-file
16002                                                 (concat quick-file "d")))
16003                    (not gnus-newsrc-alist)))
16004           ;; We read the .newsrc file.  Note that if there if a
16005           ;; .newsrc.eld file exists, it has already been read, and
16006           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16007           ;; the .newsrc file, Gnus will only use the information it
16008           ;; can find there for changing the data already read -
16009           ;; ie. reading the .newsrc file will not trash the data
16010           ;; already read (except for read articles).
16011           (save-excursion
16012             (gnus-message 5 "Reading %s..." newsrc-file)
16013             (set-buffer (find-file-noselect newsrc-file))
16014             (buffer-disable-undo (current-buffer))
16015             (gnus-newsrc-to-gnus-format)
16016             (kill-buffer (current-buffer))
16017             (gnus-message 5 "Reading %s...done" newsrc-file)))
16018
16019       ;; Read any slave files.
16020       (or gnus-slave
16021           (gnus-master-read-slave-newsrc)))))
16022
16023 (defun gnus-read-newsrc-el-file (file)
16024   (let ((ding-file (concat file "d")))
16025     ;; We always, always read the .eld file.
16026     (gnus-message 5 "Reading %s..." ding-file)
16027     (let (gnus-newsrc-assoc)
16028       (condition-case nil
16029           (load ding-file t t t)
16030         (error
16031          (gnus-message 1 "Error in %s" ding-file)
16032          (ding)))
16033       (when gnus-newsrc-assoc
16034         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16035     (gnus-make-hashtable-from-newsrc-alist)
16036     (when (file-newer-than-file-p file ding-file)
16037       ;; Old format quick file
16038       (gnus-message 5 "Reading %s..." file)
16039       ;; The .el file is newer than the .eld file, so we read that one
16040       ;; as well.
16041       (gnus-read-old-newsrc-el-file file))))
16042
16043 ;; Parse the old-style quick startup file
16044 (defun gnus-read-old-newsrc-el-file (file)
16045   (let (newsrc killed marked group m info)
16046     (prog1
16047         (let ((gnus-killed-assoc nil)
16048               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16049           (prog1
16050               (condition-case nil
16051                   (load file t t t)
16052                 (error nil))
16053             (setq newsrc gnus-newsrc-assoc
16054                   killed gnus-killed-assoc
16055                   marked gnus-marked-assoc)))
16056       (setq gnus-newsrc-alist nil)
16057       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16058         (if info
16059             (progn
16060               (gnus-info-set-read info (cddr group))
16061               (gnus-info-set-level
16062                info (if (nth 1 group) gnus-level-default-subscribed
16063                       gnus-level-default-unsubscribed))
16064               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16065           (push (setq info
16066                       (list (car group)
16067                             (if (nth 1 group) gnus-level-default-subscribed
16068                               gnus-level-default-unsubscribed)
16069                             (cddr group)))
16070                 gnus-newsrc-alist))
16071         ;; Copy marks into info.
16072         (when (setq m (assoc (car group) marked))
16073           (unless (nthcdr 3 info)
16074             (nconc info (list nil)))
16075           (gnus-info-set-marks
16076            info (list (cons 'tick (gnus-compress-sequence 
16077                                    (sort (cdr m) '<) t))))))
16078       (setq newsrc killed)
16079       (while newsrc
16080         (setcar newsrc (caar newsrc))
16081         (setq newsrc (cdr newsrc)))
16082       (setq gnus-killed-list killed))
16083     ;; The .el file version of this variable does not begin with
16084     ;; "options", while the .eld version does, so we just add it if it
16085     ;; isn't there.
16086     (and
16087      gnus-newsrc-options
16088      (progn
16089        (and (not (string-match "^ *options" gnus-newsrc-options))
16090             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16091        (and (not (string-match "\n$" gnus-newsrc-options))
16092             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16093        ;; Finally, if we read some options lines, we parse them.
16094        (or (string= gnus-newsrc-options "")
16095            (gnus-newsrc-parse-options gnus-newsrc-options))))
16096
16097     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16098     (gnus-make-hashtable-from-newsrc-alist)))
16099
16100 (defun gnus-make-newsrc-file (file)
16101   "Make server dependent file name by catenating FILE and server host name."
16102   (let* ((file (expand-file-name file nil))
16103          (real-file (concat file "-" (nth 1 gnus-select-method))))
16104     (if (or (file-exists-p real-file)
16105             (file-exists-p (concat real-file ".el"))
16106             (file-exists-p (concat real-file ".eld")))
16107         real-file file)))
16108
16109 (defun gnus-newsrc-to-gnus-format ()
16110   (setq gnus-newsrc-options "")
16111   (setq gnus-newsrc-options-n nil)
16112
16113   (or gnus-active-hashtb
16114       (setq gnus-active-hashtb (make-vector 4095 0)))
16115   (let ((buf (current-buffer))
16116         (already-read (> (length gnus-newsrc-alist) 1))
16117         group subscribed options-symbol newsrc Options-symbol
16118         symbol reads num1)
16119     (goto-char (point-min))
16120     ;; We intern the symbol `options' in the active hashtb so that we
16121     ;; can `eq' against it later.
16122     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16123     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16124
16125     (while (not (eobp))
16126       ;; We first read the first word on the line by narrowing and
16127       ;; then reading into `gnus-active-hashtb'.  Most groups will
16128       ;; already exist in that hashtb, so this will save some string
16129       ;; space.
16130       (narrow-to-region
16131        (point)
16132        (progn (skip-chars-forward "^ \t!:\n") (point)))
16133       (goto-char (point-min))
16134       (setq symbol
16135             (and (/= (point-min) (point-max))
16136                  (let ((obarray gnus-active-hashtb)) (read buf))))
16137       (widen)
16138       ;; Now, the symbol we have read is either `options' or a group
16139       ;; name.  If it is an options line, we just add it to a string.
16140       (cond
16141        ((or (eq symbol options-symbol)
16142             (eq symbol Options-symbol))
16143         (setq gnus-newsrc-options
16144               ;; This concating is quite inefficient, but since our
16145               ;; thorough studies show that approx 99.37% of all
16146               ;; .newsrc files only contain a single options line, we
16147               ;; don't give a damn, frankly, my dear.
16148               (concat gnus-newsrc-options
16149                       (buffer-substring
16150                        (gnus-point-at-bol)
16151                        ;; Options may continue on the next line.
16152                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16153                                 (progn (beginning-of-line) (point)))
16154                            (point)))))
16155         (forward-line -1))
16156        (symbol
16157         ;; Group names can be just numbers.  
16158         (when (numberp symbol) 
16159           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16160         (or (boundp symbol) (set symbol nil))
16161         ;; It was a group name.
16162         (setq subscribed (= (following-char) ?:)
16163               group (symbol-name symbol)
16164               reads nil)
16165         (if (eolp)
16166             ;; If the line ends here, this is clearly a buggy line, so
16167             ;; we put point a the beginning of line and let the cond
16168             ;; below do the error handling.
16169             (beginning-of-line)
16170           ;; We skip to the beginning of the ranges.
16171           (skip-chars-forward "!: \t"))
16172         ;; We are now at the beginning of the list of read articles.
16173         ;; We read them range by range.
16174         (while
16175             (cond
16176              ((looking-at "[0-9]+")
16177               ;; We narrow and read a number instead of buffer-substring/
16178               ;; string-to-int because it's faster.  narrow/widen is
16179               ;; faster than save-restriction/narrow, and save-restriction
16180               ;; produces a garbage object.
16181               (setq num1 (progn
16182                            (narrow-to-region (match-beginning 0) (match-end 0))
16183                            (read buf)))
16184               (widen)
16185               ;; If the next character is a dash, then this is a range.
16186               (if (= (following-char) ?-)
16187                   (progn
16188                     ;; We read the upper bound of the range.
16189                     (forward-char 1)
16190                     (if (not (looking-at "[0-9]+"))
16191                         ;; This is a buggy line, by we pretend that
16192                         ;; it's kinda OK.  Perhaps the user should be
16193                         ;; dinged?
16194                         (setq reads (cons num1 reads))
16195                       (setq reads
16196                             (cons
16197                              (cons num1
16198                                    (progn
16199                                      (narrow-to-region (match-beginning 0)
16200                                                        (match-end 0))
16201                                      (read buf)))
16202                              reads))
16203                       (widen)))
16204                 ;; It was just a simple number, so we add it to the
16205                 ;; list of ranges.
16206                 (setq reads (cons num1 reads)))
16207               ;; If the next char in ?\n, then we have reached the end
16208               ;; of the line and return nil.
16209               (/= (following-char) ?\n))
16210              ((= (following-char) ?\n)
16211               ;; End of line, so we end.
16212               nil)
16213              (t
16214               ;; Not numbers and not eol, so this might be a buggy
16215               ;; line...
16216               (or (eobp)
16217                   ;; If it was eob instead of ?\n, we allow it.
16218                   (progn
16219                     ;; The line was buggy.
16220                     (setq group nil)
16221                     (gnus-message 3 "Mangled line: %s"
16222                                   (buffer-substring (gnus-point-at-bol)
16223                                                     (gnus-point-at-eol)))
16224                     (ding)
16225                     (sit-for 1)))
16226               nil))
16227           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16228           ;; we allow them, because it's a common mistake to put a
16229           ;; space after the comma.
16230           (skip-chars-forward ", "))
16231
16232         ;; We have already read .newsrc.eld, so we gently update the
16233         ;; data in the hash table with the information we have just
16234         ;; read.
16235         (when group
16236           (let ((info (gnus-get-info group))
16237                 level)
16238             (if info
16239                 ;; There is an entry for this file in the alist.
16240                 (progn
16241                   (gnus-info-set-read info (nreverse reads))
16242                   ;; We update the level very gently.  In fact, we
16243                   ;; only change it if there's been a status change
16244                   ;; from subscribed to unsubscribed, or vice versa.
16245                   (setq level (gnus-info-level info))
16246                   (cond ((and (<= level gnus-level-subscribed)
16247                               (not subscribed))
16248                          (setq level (if reads
16249                                          gnus-level-default-unsubscribed
16250                                        (1+ gnus-level-default-unsubscribed))))
16251                         ((and (> level gnus-level-subscribed) subscribed)
16252                          (setq level gnus-level-default-subscribed)))
16253                   (gnus-info-set-level info level))
16254               ;; This is a new group.
16255               (setq info (list group
16256                                (if subscribed
16257                                    gnus-level-default-subscribed
16258                                  (if reads
16259                                      (1+ gnus-level-subscribed)
16260                                    gnus-level-default-unsubscribed))
16261                                (nreverse reads))))
16262             (setq newsrc (cons info newsrc))))))
16263       (forward-line 1))
16264
16265     (setq newsrc (nreverse newsrc))
16266
16267     (if (not already-read)
16268         ()
16269       ;; We now have two newsrc lists - `newsrc', which is what we
16270       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16271       ;; what we've read from .newsrc.eld.  We have to merge these
16272       ;; lists.  We do this by "attaching" any (foreign) groups in the
16273       ;; gnus-newsrc-alist to the (native) group that precedes them.
16274       (let ((rc (cdr gnus-newsrc-alist))
16275             (prev gnus-newsrc-alist)
16276             entry mentry)
16277         (while rc
16278           (or (null (nth 4 (car rc)))   ; It's a native group.
16279               (assoc (caar rc) newsrc) ; It's already in the alist.
16280               (if (setq entry (assoc (caar prev) newsrc))
16281                   (setcdr (setq mentry (memq entry newsrc))
16282                           (cons (car rc) (cdr mentry)))
16283                 (setq newsrc (cons (car rc) newsrc))))
16284           (setq prev rc
16285                 rc (cdr rc)))))
16286
16287     (setq gnus-newsrc-alist newsrc)
16288     ;; We make the newsrc hashtb.
16289     (gnus-make-hashtable-from-newsrc-alist)
16290
16291     ;; Finally, if we read some options lines, we parse them.
16292     (or (string= gnus-newsrc-options "")
16293         (gnus-newsrc-parse-options gnus-newsrc-options))))
16294
16295 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16296 ;; The return value will be a list on the form
16297 ;; ((regexp1 . ignore)
16298 ;;  (regexp2 . subscribe)...)
16299 ;; When handling new newsgroups, groups that match a `ignore' regexp
16300 ;; will be ignored, and groups that match a `subscribe' regexp will be
16301 ;; subscribed.  A line like
16302 ;; options -n !all rec.all
16303 ;; will lead to a list that looks like
16304 ;; (("^rec\\..+" . subscribe)
16305 ;;  ("^.+" . ignore))
16306 ;; So all "rec.*" groups will be subscribed, while all the other
16307 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16308 ;; different from "options -n rec.all !all".
16309 (defun gnus-newsrc-parse-options (options)
16310   (let (out eol)
16311     (save-excursion
16312       (gnus-set-work-buffer)
16313       (insert (regexp-quote options))
16314       ;; First we treat all continuation lines.
16315       (goto-char (point-min))
16316       (while (re-search-forward "\n[ \t]+" nil t)
16317         (replace-match " " t t))
16318       ;; Then we transform all "all"s into ".+"s.
16319       (goto-char (point-min))
16320       (while (re-search-forward "\\ball\\b" nil t)
16321         (replace-match ".+" t t))
16322       (goto-char (point-min))
16323       ;; We remove all other options than the "-n" ones.
16324       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16325         (replace-match " ")
16326         (forward-char -1))
16327       (goto-char (point-min))
16328
16329       ;; We are only interested in "options -n" lines - we
16330       ;; ignore the other option lines.
16331       (while (re-search-forward "[ \t]-n" nil t)
16332         (setq eol
16333               (or (save-excursion
16334                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16335                          (- (point) 2)))
16336                   (gnus-point-at-eol)))
16337         ;; Search for all "words"...
16338         (while (re-search-forward "[^ \t,\n]+" eol t)
16339           (if (= (char-after (match-beginning 0)) ?!)
16340               ;; If the word begins with a bang (!), this is a "not"
16341               ;; spec.  We put this spec (minus the bang) and the
16342               ;; symbol `ignore' into the list.
16343               (setq out (cons (cons (concat
16344                                      "^" (buffer-substring
16345                                           (1+ (match-beginning 0))
16346                                           (match-end 0)))
16347                                     'ignore) out))
16348             ;; There was no bang, so this is a "yes" spec.
16349             (setq out (cons (cons (concat "^" (match-string 0))
16350                                   'subscribe) out)))))
16351
16352       (setq gnus-newsrc-options-n out))))
16353
16354 (defun gnus-save-newsrc-file (&optional force)
16355   "Save .newsrc file."
16356   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16357   ;; from the variable gnus-newsrc-alist.
16358   (when (and (or gnus-newsrc-alist gnus-killed-list)
16359              gnus-current-startup-file)
16360     (save-excursion
16361       (if (and (or gnus-use-dribble-file gnus-slave)
16362                (not force)
16363                (or (not gnus-dribble-buffer)
16364                    (not (buffer-name gnus-dribble-buffer))
16365                    (zerop (save-excursion
16366                             (set-buffer gnus-dribble-buffer)
16367                             (buffer-size)))))
16368           (gnus-message 4 "(No changes need to be saved)")
16369         (run-hooks 'gnus-save-newsrc-hook)
16370         (if gnus-slave
16371             (gnus-slave-save-newsrc)
16372           ;; Save .newsrc.
16373           (when gnus-save-newsrc-file
16374             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16375             (gnus-gnus-to-newsrc-format)
16376             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16377           ;; Save .newsrc.eld.
16378           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16379           (make-local-variable 'version-control)
16380           (setq version-control 'never)
16381           (setq buffer-file-name
16382                 (concat gnus-current-startup-file ".eld"))
16383           (gnus-add-current-to-buffer-list)
16384           (buffer-disable-undo (current-buffer))
16385           (erase-buffer)
16386           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16387           (gnus-gnus-to-quick-newsrc-format)
16388           (run-hooks 'gnus-save-quick-newsrc-hook)
16389           (save-buffer)
16390           (kill-buffer (current-buffer))
16391           (gnus-message
16392            5 "Saving %s.eld...done" gnus-current-startup-file))
16393         (gnus-dribble-delete-file)))))
16394
16395 (defun gnus-gnus-to-quick-newsrc-format ()
16396   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16397   (insert ";; Gnus startup file.\n")
16398   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16399   (insert ";; to read .newsrc.\n")
16400   (insert "(setq gnus-newsrc-file-version "
16401           (prin1-to-string gnus-version) ")\n")
16402   (let ((variables
16403          (if gnus-save-killed-list gnus-variable-list
16404            ;; Remove the `gnus-killed-list' from the list of variables
16405            ;; to be saved, if required.
16406            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16407         ;; Peel off the "dummy" group.
16408         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16409         variable)
16410     ;; Insert the variables into the file.
16411     (while variables
16412       (when (and (boundp (setq variable (pop variables)))
16413                  (symbol-value variable))
16414         (insert "(setq " (symbol-name variable) " '")
16415         (prin1 (symbol-value variable) (current-buffer))
16416         (insert ")\n")))))
16417
16418 (defun gnus-gnus-to-newsrc-format ()
16419   ;; Generate and save the .newsrc file.
16420   (save-excursion
16421     (set-buffer (create-file-buffer gnus-current-startup-file))
16422     (let ((newsrc (cdr gnus-newsrc-alist))
16423           (standard-output (current-buffer))
16424           info ranges range method)
16425       (setq buffer-file-name gnus-current-startup-file)
16426       (buffer-disable-undo (current-buffer))
16427       (erase-buffer)
16428       ;; Write options.
16429       (if gnus-newsrc-options (insert gnus-newsrc-options))
16430       ;; Write subscribed and unsubscribed.
16431       (while (setq info (pop newsrc))
16432         ;; Don't write foreign groups to .newsrc.
16433         (when (or (null (setq method (gnus-info-method info)))
16434                   (equal method "native")
16435                   (gnus-server-equal method gnus-select-method))
16436           (insert (gnus-info-group info)
16437                   (if (> (gnus-info-level info) gnus-level-subscribed)
16438                       "!" ":"))
16439           (when (setq ranges (gnus-info-read info))
16440             (insert " ")
16441             (if (not (listp (cdr ranges)))
16442                 (if (= (car ranges) (cdr ranges))
16443                     (princ (car ranges))
16444                   (princ (car ranges))
16445                   (insert "-")
16446                   (princ (cdr ranges)))
16447               (while (setq range (pop ranges))
16448                 (if (or (atom range) (= (car range) (cdr range)))
16449                     (princ (or (and (atom range) range) (car range)))
16450                   (princ (car range))
16451                   (insert "-")
16452                   (princ (cdr range)))
16453                 (if ranges (insert ",")))))
16454           (insert "\n")))
16455       (make-local-variable 'version-control)
16456       (setq version-control 'never)
16457       ;; It has been reported that sometime the modtime on the .newsrc
16458       ;; file seems to be off.  We really do want to overwrite it, so
16459       ;; we clear the modtime here before saving.  It's a bit odd,
16460       ;; though...
16461       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16462       ;; delete the silly thing entirely first.  but this fails to provide
16463       ;; such niceties as .newsrc~ creation.
16464       (if gnus-modtime-botch
16465           (delete-file gnus-startup-file)
16466         (clear-visited-file-modtime))
16467       (run-hooks 'gnus-save-standard-newsrc-hook)
16468       (save-buffer)
16469       (kill-buffer (current-buffer)))))
16470
16471 \f
16472 ;;;
16473 ;;; Slave functions.
16474 ;;;
16475
16476 (defun gnus-slave-save-newsrc ()
16477   (save-excursion
16478     (set-buffer gnus-dribble-buffer)
16479     (let ((slave-name
16480            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16481       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16482
16483 (defun gnus-master-read-slave-newsrc ()
16484   (let ((slave-files
16485          (directory-files
16486           (file-name-directory gnus-current-startup-file)
16487           t (concat
16488              "^" (regexp-quote
16489                   (concat
16490                    (file-name-nondirectory gnus-current-startup-file)
16491                    "-slave-")))
16492           t))
16493         file)
16494     (if (not slave-files)
16495         ()                              ; There are no slave files to read.
16496       (gnus-message 7 "Reading slave newsrcs...")
16497       (save-excursion
16498         (set-buffer (get-buffer-create " *gnus slave*"))
16499         (buffer-disable-undo (current-buffer))
16500         (setq slave-files
16501               (sort (mapcar (lambda (file)
16502                               (list (nth 5 (file-attributes file)) file))
16503                             slave-files)
16504                     (lambda (f1 f2)
16505                       (or (< (caar f1) (caar f2))
16506                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16507         (while slave-files
16508           (erase-buffer)
16509           (setq file (nth 1 (car slave-files)))
16510           (insert-file-contents file)
16511           (if (condition-case ()
16512                   (progn
16513                     (eval-buffer (current-buffer))
16514                     t)
16515                 (error
16516                  (gnus-message 3 "Possible error in %s" file)
16517                  (ding)
16518                  (sit-for 2)
16519                  nil))
16520               (or gnus-slave ; Slaves shouldn't delete these files.
16521                   (condition-case ()
16522                       (delete-file file)
16523                     (error nil))))
16524           (setq slave-files (cdr slave-files))))
16525       (gnus-message 7 "Reading slave newsrcs...done"))))
16526
16527 \f
16528 ;;;
16529 ;;; Group description.
16530 ;;;
16531
16532 (defun gnus-read-all-descriptions-files ()
16533   (let ((methods (cons gnus-select-method 
16534                        (nconc
16535                         (when gnus-message-archive-method
16536                           (list "archive"))
16537                         gnus-secondary-select-methods))))
16538     (while methods
16539       (gnus-read-descriptions-file (car methods))
16540       (setq methods (cdr methods)))
16541     t))
16542
16543 (defun gnus-read-descriptions-file (&optional method)
16544   (let ((method (or method gnus-select-method)))
16545     (when (stringp method)
16546       (setq method (gnus-server-to-method method)))
16547     ;; We create the hashtable whether we manage to read the desc file
16548     ;; to avoid trying to re-read after a failed read.
16549     (or gnus-description-hashtb
16550         (setq gnus-description-hashtb
16551               (gnus-make-hashtable (length gnus-active-hashtb))))
16552     ;; Mark this method's desc file as read.
16553     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16554                   gnus-description-hashtb)
16555
16556     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16557     (cond
16558      ((not (gnus-check-server method))
16559       (gnus-message 1 "Couldn't open server")
16560       nil)
16561      ((not (gnus-request-list-newsgroups method))
16562       (gnus-message 1 "Couldn't read newsgroups descriptions")
16563       nil)
16564      (t
16565       (let (group)
16566         (save-excursion
16567           (save-restriction
16568             (set-buffer nntp-server-buffer)
16569             (goto-char (point-min))
16570             (if (or (search-forward "\n.\n" nil t)
16571                     (goto-char (point-max)))
16572                 (progn
16573                   (beginning-of-line)
16574                   (narrow-to-region (point-min) (point))))
16575             (goto-char (point-min))
16576             (while (not (eobp))
16577               ;; If we get an error, we set group to 0, which is not a
16578               ;; symbol...
16579               (setq group
16580                     (condition-case ()
16581                         (let ((obarray gnus-description-hashtb))
16582                           ;; Group is set to a symbol interned in this
16583                           ;; hash table.
16584                           (read nntp-server-buffer))
16585                       (error 0)))
16586               (skip-chars-forward " \t")
16587               ;; ...  which leads to this line being effectively ignored.
16588               (and (symbolp group)
16589                    (set group (buffer-substring
16590                                (point) (progn (end-of-line) (point)))))
16591               (forward-line 1))))
16592         (gnus-message 5 "Reading descriptions file...done")
16593         t)))))
16594
16595 (defun gnus-group-get-description (group)
16596   "Get the description of a group by sending XGTITLE to the server."
16597   (when (gnus-request-group-description group)
16598     (save-excursion
16599       (set-buffer nntp-server-buffer)
16600       (goto-char (point-min))
16601       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16602         (match-string 1)))))
16603
16604 \f
16605 ;;;
16606 ;;; Buffering of read articles.
16607 ;;;
16608
16609 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16610 (defvar gnus-backlog-articles nil)
16611 (defvar gnus-backlog-hashtb nil)
16612
16613 (defun gnus-backlog-buffer ()
16614   "Return the backlog buffer."
16615   (or (get-buffer gnus-backlog-buffer)
16616       (save-excursion
16617         (set-buffer (get-buffer-create gnus-backlog-buffer))
16618         (buffer-disable-undo (current-buffer))
16619         (setq buffer-read-only t)
16620         (gnus-add-current-to-buffer-list)
16621         (get-buffer gnus-backlog-buffer))))
16622
16623 (defun gnus-backlog-setup ()
16624   "Initialize backlog variables."
16625   (unless gnus-backlog-hashtb
16626     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16627
16628 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16629
16630 (defun gnus-backlog-shutdown ()
16631   "Clear all backlog variables and buffers."
16632   (when (get-buffer gnus-backlog-buffer)
16633     (kill-buffer gnus-backlog-buffer))
16634   (setq gnus-backlog-hashtb nil
16635         gnus-backlog-articles nil))
16636
16637 (defun gnus-backlog-enter-article (group number buffer)
16638   (gnus-backlog-setup)
16639   (let ((ident (intern (concat group ":" (int-to-string number))
16640                        gnus-backlog-hashtb))
16641         b)
16642     (if (memq ident gnus-backlog-articles)
16643         () ; It's already kept.
16644       ;; Remove the oldest article, if necessary.
16645       (and (numberp gnus-keep-backlog)
16646            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16647            (gnus-backlog-remove-oldest-article))
16648       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16649       ;; Insert the new article.
16650       (save-excursion
16651         (set-buffer (gnus-backlog-buffer))
16652         (let (buffer-read-only)
16653           (goto-char (point-max))
16654           (or (bolp) (insert "\n"))
16655           (setq b (point))
16656           (insert-buffer-substring buffer)
16657           ;; Tag the beginning of the article with the ident.
16658           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16659
16660 (defun gnus-backlog-remove-oldest-article ()
16661   (save-excursion
16662     (set-buffer (gnus-backlog-buffer))
16663     (goto-char (point-min))
16664     (if (zerop (buffer-size))
16665         () ; The buffer is empty.
16666       (let ((ident (get-text-property (point) 'gnus-backlog))
16667             buffer-read-only)
16668         ;; Remove the ident from the list of articles.
16669         (when ident
16670           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16671         ;; Delete the article itself.
16672         (delete-region
16673          (point) (next-single-property-change
16674                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16675
16676 (defun gnus-backlog-request-article (group number buffer)
16677   (when (numberp number)
16678     (gnus-backlog-setup)
16679     (let ((ident (intern (concat group ":" (int-to-string number))
16680                          gnus-backlog-hashtb))
16681           beg end)
16682       (when (memq ident gnus-backlog-articles)
16683         ;; It was in the backlog.
16684         (save-excursion
16685           (set-buffer (gnus-backlog-buffer))
16686           (if (not (setq beg (text-property-any
16687                               (point-min) (point-max) 'gnus-backlog
16688                               ident)))
16689               ;; It wasn't in the backlog after all.
16690               (ignore
16691                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16692             ;; Find the end (i. e., the beginning of the next article).
16693             (setq end
16694                   (next-single-property-change
16695                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16696         (let ((buffer-read-only nil))
16697           (erase-buffer)
16698           (insert-buffer-substring gnus-backlog-buffer beg end)
16699           t)))))
16700
16701 ;; Allow redefinition of Gnus functions.
16702
16703 (gnus-ems-redefine)
16704
16705 (provide 'gnus)
16706
16707 ;;; gnus.el ends here