*** 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval '(run-hooks 'gnus-load-hook))
29
30 (require 'mail-utils)
31 (require 'timezone)
32 (require 'nnheader)
33
34 (eval-when-compile (require 'cl))
35
36 ;; Site dependent variables.  These variables should be defined in
37 ;; paths.el.
38
39 (defvar gnus-default-nntp-server nil
40   "Specify a default NNTP server.
41 This variable should be defined in paths.el, and should never be set
42 by the user.
43 If you want to change servers, you should use `gnus-select-method'.
44 See the documentation to that variable.")
45
46 (defvar gnus-backup-default-subscribed-newsgroups
47   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
48   "Default default new newsgroups the first time Gnus is run.
49 Should be set in paths.el, and shouldn't be touched by the user.")
50
51 (defvar gnus-local-domain nil
52   "Local domain name without a host name.
53 The DOMAINNAME environment variable is used instead if it is defined.
54 If the `system-name' function returns the full Internet name, there is
55 no need to set this variable.")
56
57 (defvar gnus-local-organization nil
58   "String with a description of what organization (if any) the user belongs to.
59 The ORGANIZATION environment variable is used instead if it is defined.
60 If this variable contains a function, this function will be called
61 with the current newsgroup name as the argument.  The function should
62 return a string.
63
64 In any case, if the string (either in the variable, in the environment
65 variable, or returned by the function) is a file name, the contents of
66 this file will be used as the organization.")
67
68 (defvar gnus-use-generic-from nil
69   "If nil, the full host name will be the system name prepended to the domain name.
70 If this is a string, the full host name will be this string.
71 If this is non-nil, non-string, the domain name will be used as the
72 full host name.")
73
74 (defvar gnus-use-generic-path nil
75   "If nil, use the NNTP server name in the Path header.
76 If stringp, use this; if non-nil, use no host name (user name only).")
77
78 ;; Customization variables
79
80 ;; Don't touch this variable.
81 (defvar gnus-nntp-service "nntp"
82   "*NNTP service name (\"nntp\" or 119).
83 This is an obsolete variable, which is scarcely used.  If you use an
84 nntp server for your newsgroup and want to change the port number
85 used to 899, you would say something along these lines:
86
87  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
88
89 (defvar gnus-nntpserver-file "/etc/nntpserver"
90   "*A file with only the name of the nntp server in it.")
91
92 ;; This function is used to check both the environment variable
93 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
94 ;; an nntp server name default.
95 (defun gnus-getenv-nntpserver ()
96   (or (getenv "NNTPSERVER")
97       (and (file-readable-p gnus-nntpserver-file)
98            (save-excursion
99              (set-buffer (get-buffer-create " *gnus nntp*"))
100              (buffer-disable-undo (current-buffer))
101              (insert-file-contents gnus-nntpserver-file)
102              (let ((name (buffer-string)))
103                (prog1
104                    (if (string-match "^[ \t\n]*$" name)
105                        nil
106                      name)
107                  (kill-buffer (current-buffer))))))))
108
109 (defvar gnus-select-method
110   (nconc
111    (list 'nntp (or (condition-case ()
112                        (gnus-getenv-nntpserver)
113                      (error nil))
114                    (if (and gnus-default-nntp-server
115                             (not (string= gnus-default-nntp-server "")))
116                        gnus-default-nntp-server)
117                    (system-name)))
118    (if (or (null gnus-nntp-service)
119            (equal gnus-nntp-service "nntp"))
120        nil
121      (list gnus-nntp-service)))
122   "*Default method for selecting a newsgroup.
123 This variable should be a list, where the first element is how the
124 news is to be fetched, the second is the address.
125
126 For instance, if you want to get your news via NNTP from
127 \"flab.flab.edu\", you could say:
128
129 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
130
131 If you want to use your local spool, say:
132
133 (setq gnus-select-method (list 'nnspool (system-name)))
134
135 If you use this variable, you must set `gnus-nntp-server' to nil.
136
137 There is a lot more to know about select methods and virtual servers -
138 see the manual for details.")
139
140 (defvar gnus-message-archive-method 
141   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
142              (nnfolder-active-file "~/Mail/archive/active")
143              (nnfolder-get-new-mail nil)
144              (nnfolder-inhibit-expiry t))
145   "*Method used for archiving messages you've sent.
146 This should be a mail method.")
147
148 (defvar gnus-refer-article-method nil
149   "*Preferred method for fetching an article by Message-ID.
150 If you are reading news from the local spool (with nnspool), fetching
151 articles by Message-ID is painfully slow.  By setting this method to an
152 nntp method, you might get acceptable results.
153
154 The value of this variable must be a valid select method as discussed
155 in the documentation of `gnus-select-method'.")
156
157 (defvar gnus-secondary-select-methods nil
158   "*A list of secondary methods that will be used for reading news.
159 This is a list where each element is a complete select method (see
160 `gnus-select-method').
161
162 If, for instance, you want to read your mail with the nnml backend,
163 you could set this variable:
164
165 (setq gnus-secondary-select-methods '((nnml \"\")))")
166
167 (defvar gnus-secondary-servers nil
168   "*List of NNTP servers that the user can choose between interactively.
169 To make Gnus query you for a server, you have to give `gnus' a
170 non-numeric prefix - `C-u M-x gnus', in short.")
171
172 (defvar gnus-nntp-server nil
173   "*The name of the host running the NNTP server.
174 This variable is semi-obsolete.  Use the `gnus-select-method'
175 variable instead.")
176
177 (defvar gnus-startup-file "~/.newsrc"
178   "*Your `.newsrc' file.
179 `.newsrc-SERVER' will be used instead if that exists.")
180
181 (defvar gnus-init-file "~/.gnus"
182   "*Your Gnus elisp startup file.
183 If a file with the .el or .elc suffixes exist, it will be read
184 instead.")
185
186 (defvar gnus-group-faq-directory
187   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
188     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
189     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
190     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
191     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
192     "/ftp@ftp.sunet.se:/pub/usenet/"
193     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
194     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
195     "/ftp@ftp.hk.super.net:/mirror/faqs/")
196   "*Directory where the group FAQs are stored.
197 This will most commonly be on a remote machine, and the file will be
198 fetched by ange-ftp.
199
200 This variable can also be a list of directories.  In that case, the
201 first element in the list will be used by default, and the others will
202 be used as backup sites.
203
204 Note that Gnus uses an aol machine as the default directory.  If this
205 feels fundamentally unclean, just think of it as a way to finally get
206 something of value back from them.
207
208 If the default site is too slow, try one of these:
209
210    North America: mirrors.aol.com                /pub/rtfm/usenet
211                   ftp.seas.gwu.edu               /pub/rtfm
212                   rtfm.mit.edu                   /pub/usenet/news.answers
213    Europe:        ftp.uni-paderborn.de           /pub/FAQ
214                   src.doc.ic.ac.uk               /usenet/news-FAQS
215                   ftp.sunet.se                   /pub/usenet
216    Asia:          nctuccca.edu.tw                /USENET/FAQ
217                   hwarang.postech.ac.kr          /pub/usenet/news.answers
218                   ftp.hk.super.net               /mirror/faqs")
219
220 (defvar gnus-group-archive-directory
221   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
222   "*The address of the (ding) archives.")
223
224 (defvar gnus-group-recent-archive-directory
225   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
226   "*The address of the most recent (ding) articles.")
227
228 (defvar gnus-default-subscribed-newsgroups nil
229   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
230 It should be a list of strings.
231 If it is `t', Gnus will not do anything special the first time it is
232 started; it'll just use the normal newsgroups subscription methods.")
233
234 (defvar gnus-use-cross-reference t
235   "*Non-nil means that cross referenced articles will be marked as read.
236 If nil, ignore cross references.  If t, mark articles as read in
237 subscribed newsgroups.  If neither t nor nil, mark as read in all
238 newsgroups.")
239
240 (defvar gnus-single-article-buffer t
241   "*If non-nil, display all articles in the same buffer.
242 If nil, each group will get its own article buffer.")
243
244 (defvar gnus-use-dribble-file t
245   "*Non-nil means that Gnus will use a dribble file to store user updates.
246 If Emacs should crash without saving the .newsrc files, complete
247 information can be restored from the dribble file.")
248
249 (defvar gnus-dribble-directory nil
250   "*The directory where dribble files will be saved.
251 If this variable is nil, the directory where the .newsrc files are
252 saved will be used.")
253
254 (defvar gnus-asynchronous nil
255   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
256
257 (defvar gnus-kill-summary-on-exit t
258   "*If non-nil, kill the summary buffer when you exit from it.
259 If nil, the summary will become a \"*Dead Summary*\" buffer, and
260 it will be killed sometime later.")
261
262 (defvar gnus-large-newsgroup 200
263   "*The number of articles which indicates a large newsgroup.
264 If the number of articles in a newsgroup is greater than this value,
265 confirmation is required for selecting the newsgroup.")
266
267 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
268 (defvar gnus-no-groups-message "No news is horrible news"
269   "*Message displayed by Gnus when no groups are available.")
270
271 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
272   "*Non-nil means that the default name of a file to save articles in is the group name.
273 If it's nil, the directory form of the group name is used instead.
274
275 If this variable is a list, and the list contains the element
276 `not-score', long file names will not be used for score files; if it
277 contains the element `not-save', long file names will not be used for
278 saving; and if it contains the element `not-kill', long file names
279 will not be used for kill files.")
280
281 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
282   "*Name of the directory articles will be saved in (default \"~/News\").
283 Initialized from the SAVEDIR environment variable.")
284
285 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
286   "*Name of the directory where kill files will be stored (default \"~/News\").
287 Initialized from the SAVEDIR environment variable.")
288
289 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
290   "*A function to save articles in your favorite format.
291 The function must be interactively callable (in other words, it must
292 be an Emacs command).
293
294 Gnus provides the following functions:
295
296 * gnus-summary-save-in-rmail (Rmail format)
297 * gnus-summary-save-in-mail (Unix mail format)
298 * gnus-summary-save-in-folder (MH folder)
299 * gnus-summary-save-in-file (article format).
300 * gnus-summary-save-in-vm (use VM's folder format).")
301
302 (defvar gnus-prompt-before-saving 'always
303   "*This variable says how much prompting is to be done when saving articles.
304 If it is nil, no prompting will be done, and the articles will be
305 saved to the default files.  If this variable is `always', each and
306 every article that is saved will be preceded by a prompt, even when
307 saving large batches of articles.  If this variable is neither nil not
308 `always', there the user will be prompted once for a file name for
309 each invocation of the saving commands.")
310
311 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
312   "*A function generating a file name to save articles in Rmail format.
313 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
314
315 (defvar gnus-mail-save-name (function gnus-plain-save-name)
316   "*A function generating a file name to save articles in Unix mail format.
317 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
318
319 (defvar gnus-folder-save-name (function gnus-folder-save-name)
320   "*A function generating a file name to save articles in MH folder.
321 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
322
323 (defvar gnus-file-save-name (function gnus-numeric-save-name)
324   "*A function generating a file name to save articles in article format.
325 The function is called with NEWSGROUP, HEADERS, and optional
326 LAST-FILE.")
327
328 (defvar gnus-split-methods
329   '((gnus-article-archive-name))
330   "*Variable used to suggest where articles are to be saved.
331 For instance, if you would like to save articles related to Gnus in
332 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
333 you could set this variable to something like:
334
335  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
336    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
337
338 This variable is an alist where the where the key is the match and the
339 value is a list of possible files to save in if the match is non-nil.
340
341 If the match is a string, it is used as a regexp match on the
342 article.  If the match is a symbol, that symbol will be funcalled
343 from the buffer of the article to be saved with the newsgroup as the
344 parameter.  If it is a list, it will be evaled in the same buffer.
345
346 If this form or function returns a string, this string will be used as
347 a possible file name; and if it returns a non-nil list, that list will
348 be used as possible file names.")
349
350 (defvar gnus-move-split-methods nil
351   "*Variable used to suggest where articles are to be moved to.
352 It uses the same syntax as the `gnus-split-methods' variable.")
353
354 (defvar gnus-save-score nil
355   "*If non-nil, save group scoring info.")
356
357 (defvar gnus-use-adaptive-scoring nil
358   "*If non-nil, use some adaptive scoring scheme.")
359
360 (defvar gnus-use-cache nil
361   "*If nil, Gnus will ignore the article cache.
362 If `passive', it will allow entering (and reading) articles
363 explicitly entered into the cache.  If anything else, use the
364 cache to the full extent of the law.")
365
366 (defvar gnus-use-trees nil
367   "*If non-nil, display a thread tree buffer.")
368
369 (defvar gnus-use-grouplens nil
370   "*If non-nil, use GroupLens ratings.")
371
372 (defvar gnus-keep-backlog nil
373   "*If non-nil, Gnus will keep read articles for later re-retrieval.
374 If it is a number N, then Gnus will only keep the last N articles
375 read.  If it is neither nil nor a number, Gnus will keep all read
376 articles.  This is not a good idea.")
377
378 (defvar gnus-use-nocem nil
379   "*If non-nil, Gnus will read NoCeM cancel messages.")
380
381 (defvar gnus-use-demon nil
382   "If non-nil, Gnus might use some demons.")
383
384 (defvar gnus-use-scoring t
385   "*If non-nil, enable scoring.")
386
387 (defvar gnus-use-picons nil
388   "*If non-nil, display picons.")
389
390 (defvar gnus-fetch-old-headers nil
391   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
392 If an unread article in the group refers to an older, already read (or
393 just marked as read) article, the old article will not normally be
394 displayed in the Summary buffer.  If this variable is non-nil, Gnus
395 will attempt to grab the headers to the old articles, and thereby
396 build complete threads.  If it has the value `some', only enough
397 headers to connect otherwise loose threads will be displayed.
398 This variable can also be a number.  In that case, no more than that
399 number of old headers will be fetched.
400
401 The server has to support NOV for any of this to work.")
402
403 ;see gnus-cus.el
404 ;(defvar gnus-visual t
405 ;  "*If non-nil, will do various highlighting.
406 ;If nil, no mouse highlights (or any other highlights) will be
407 ;performed.  This might speed up Gnus some when generating large group
408 ;and summary buffers.")
409
410 (defvar gnus-novice-user t
411   "*Non-nil means that you are a usenet novice.
412 If non-nil, verbose messages may be displayed and confirmations may be
413 required.")
414
415 (defvar gnus-expert-user nil
416   "*Non-nil means that you will never be asked for confirmation about anything.
417 And that means *anything*.")
418
419 (defvar gnus-verbose 7
420   "*Integer that says how verbose Gnus should be.
421 The higher the number, the more messages Gnus will flash to say what
422 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
423 display most important messages; and at ten, Gnus will keep on
424 jabbering all the time.")
425
426 (defvar gnus-keep-same-level nil
427   "*Non-nil means that the next newsgroup after the current will be on the same level.
428 When you type, for instance, `n' after reading the last article in the
429 current newsgroup, you will go to the next newsgroup.  If this variable
430 is nil, the next newsgroup will be the next from the group
431 buffer.
432 If this variable is non-nil, Gnus will either put you in the
433 next newsgroup with the same level, or, if no such newsgroup is
434 available, the next newsgroup with the lowest possible level higher
435 than the current level.
436 If this variable is `best', Gnus will make the next newsgroup the one
437 with the best level.")
438
439 (defvar gnus-summary-make-false-root 'adopt
440   "*nil means that Gnus won't gather loose threads.
441 If the root of a thread has expired or been read in a previous
442 session, the information necessary to build a complete thread has been
443 lost.  Instead of having many small sub-threads from this original thread
444 scattered all over the summary buffer, Gnus can gather them.
445
446 If non-nil, Gnus will try to gather all loose sub-threads from an
447 original thread into one large thread.
448
449 If this variable is non-nil, it should be one of `none', `adopt',
450 `dummy' or `empty'.
451
452 If this variable is `none', Gnus will not make a false root, but just
453 present the sub-threads after another.
454 If this variable is `dummy', Gnus will create a dummy root that will
455 have all the sub-threads as children.
456 If this variable is `adopt', Gnus will make one of the \"children\"
457 the parent and mark all the step-children as such.
458 If this variable is `empty', the \"children\" are printed with empty
459 subject fields.  (Or rather, they will be printed with a string
460 given by the `gnus-summary-same-subject' variable.)")
461
462 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
463   "*A regexp to match subjects to be excluded from loose thread gathering.
464 As loose thread gathering is done on subjects only, that means that
465 there can be many false gatherings performed.  By rooting out certain
466 common subjects, gathering might become saner.")
467
468 (defvar gnus-summary-gather-subject-limit nil
469   "*Maximum length of subject comparisons when gathering loose threads.
470 Use nil to compare full subjects.  Setting this variable to a low
471 number will help gather threads that have been corrupted by
472 newsreaders chopping off subject lines, but it might also mean that
473 unrelated articles that have subject that happen to begin with the
474 same few characters will be incorrectly gathered.
475
476 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
477 comparing subjects.")
478
479 (defvar gnus-simplify-ignored-prefixes nil
480   "*Regexp, matches for which are removed from subject lines when simplifying.")
481
482 (defvar gnus-build-sparse-threads nil
483   "*If non-nil, fill in the gaps in threads.
484 If `some', only fill in the gaps that are needed to tie loose threads
485 together.  If `more', fill in all leaf nodes that Gnus can find.  If
486 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
487
488 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
489   "Function used for gathering loose threads.
490 There are two pre-defined functions: `gnus-gather-threads-by-subject',
491 which only takes Subjects into consideration; and
492 `gnus-gather-threads-by-references', which compared the References
493 headers of the articles to find matches.")
494
495 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
496 (defvar gnus-summary-same-subject ""
497   "*String indicating that the current article has the same subject as the previous.
498 This variable will only be used if the value of
499 `gnus-summary-make-false-root' is `empty'.")
500
501 (defvar gnus-summary-goto-unread t
502   "*If non-nil, marking commands will go to the next unread article.
503 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
504 whether it is read or not.")
505
506 (defvar gnus-group-goto-unread t
507   "*If non-nil, movement commands will go to the next unread and subscribed group.")
508
509 (defvar gnus-goto-next-group-when-activating t
510   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
511
512 (defvar gnus-check-new-newsgroups t
513   "*Non-nil means that Gnus will add new newsgroups at startup.
514 If this variable is `ask-server', Gnus will ask the server for new
515 groups since the last time it checked.  This means that the killed list
516 is no longer necessary, so you could set `gnus-save-killed-list' to
517 nil.
518
519 A variant is to have this variable be a list of select methods.  Gnus
520 will then use the `ask-server' method on all these select methods to
521 query for new groups from all those servers.
522
523 Eg.
524   (setq gnus-check-new-newsgroups
525         '((nntp \"some.server\") (nntp \"other.server\")))
526
527 If this variable is nil, then you have to tell Gnus explicitly to
528 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
529
530 (defvar gnus-check-bogus-newsgroups nil
531   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
532 If this variable is nil, then you have to tell Gnus explicitly to
533 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
534
535 (defvar gnus-read-active-file t
536   "*Non-nil means that Gnus will read the entire active file at startup.
537 If this variable is nil, Gnus will only know about the groups in your
538 `.newsrc' file.
539
540 If this variable is `some', Gnus will try to only read the relevant
541 parts of the active file from the server.  Not all servers support
542 this, and it might be quite slow with other servers, but this should
543 generally be faster than both the t and nil value.
544
545 If you set this variable to nil or `some', you probably still want to
546 be told about new newsgroups that arrive.  To do that, set
547 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
548 properly with all servers.")
549
550 (defvar gnus-level-subscribed 5
551   "*Groups with levels less than or equal to this variable are subscribed.")
552
553 (defvar gnus-level-unsubscribed 7
554   "*Groups with levels less than or equal to this variable are unsubscribed.
555 Groups with levels less than `gnus-level-subscribed', which should be
556 less than this variable, are subscribed.")
557
558 (defvar gnus-level-zombie 8
559   "*Groups with this level are zombie groups.")
560
561 (defvar gnus-level-killed 9
562   "*Groups with this level are killed.")
563
564 (defvar gnus-level-default-subscribed 3
565   "*New subscribed groups will be subscribed at this level.")
566
567 (defvar gnus-level-default-unsubscribed 6
568   "*New unsubscribed groups will be unsubscribed at this level.")
569
570 (defvar gnus-activate-level (1+ gnus-level-subscribed)
571   "*Groups higher than this level won't be activated on startup.
572 Setting this variable to something log might save lots of time when
573 you have many groups that you aren't interested in.")
574
575 (defvar gnus-activate-foreign-newsgroups 4
576   "*If nil, Gnus will not check foreign newsgroups at startup.
577 If it is non-nil, it should be a number between one and nine.  Foreign
578 newsgroups that have a level lower or equal to this number will be
579 activated on startup.  For instance, if you want to active all
580 subscribed newsgroups, but not the rest, you'd set this variable to
581 `gnus-level-subscribed'.
582
583 If you subscribe to lots of newsgroups from different servers, startup
584 might take a while.  By setting this variable to nil, you'll save time,
585 but you won't be told how many unread articles there are in the
586 groups.")
587
588 (defvar gnus-save-newsrc-file t
589   "*Non-nil means that Gnus will save the `.newsrc' file.
590 Gnus always saves its own startup file, which is called
591 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
592 be readily understood by other newsreaders.  If you don't plan on
593 using other newsreaders, set this variable to nil to save some time on
594 exit.")
595
596 (defvar gnus-save-killed-list t
597   "*If non-nil, save the list of killed groups to the startup file.
598 If you set this variable to nil, you'll save both time (when starting
599 and quitting) and space (both memory and disk), but it will also mean
600 that Gnus has no record of which groups are new and which are old, so
601 the automatic new newsgroups subscription methods become meaningless.
602
603 You should always set `gnus-check-new-newsgroups' to `ask-server' or
604 nil if you set this variable to nil.")
605
606 (defvar gnus-interactive-catchup t
607   "*If non-nil, require your confirmation when catching up a group.")
608
609 (defvar gnus-interactive-post t
610   "*If non-nil, group name will be asked for when posting.")
611
612 (defvar gnus-interactive-exit t
613   "*If non-nil, require your confirmation when exiting Gnus.")
614
615 (defvar gnus-kill-killed t
616   "*If non-nil, Gnus will apply kill files to already killed articles.
617 If it is nil, Gnus will never apply kill files to articles that have
618 already been through the scoring process, which might very well save lots
619 of time.")
620
621 (defvar gnus-extract-address-components 'gnus-extract-address-components
622   "*Function for extracting address components from a From header.
623 Two pre-defined function exist: `gnus-extract-address-components',
624 which is the default, quite fast, and too simplistic solution, and
625 `mail-extract-address-components', which works much better, but is
626 slower.")
627
628 (defvar gnus-summary-default-score 0
629   "*Default article score level.
630 If this variable is nil, scoring will be disabled.")
631
632 (defvar gnus-summary-zcore-fuzz 0
633   "*Fuzziness factor for the zcore in the summary buffer.
634 Articles with scores closer than this to `gnus-summary-default-score'
635 will not be marked.")
636
637 (defvar gnus-simplify-subject-fuzzy-regexp nil
638   "*Strings to be removed when doing fuzzy matches.
639 This can either be a regular expression or list of regular expressions
640 that will be removed from subject strings if fuzzy subject
641 simplification is selected.")
642
643 (defvar gnus-permanently-visible-groups nil
644   "*Regexp to match groups that should always be listed in the group buffer.
645 This means that they will still be listed when there are no unread
646 articles in the groups.")
647
648 (defvar gnus-list-groups-with-ticked-articles t
649   "*If non-nil, list groups that have only ticked articles.
650 If nil, only list groups that have unread articles.")
651
652 (defvar gnus-group-default-list-level gnus-level-subscribed
653   "*Default listing level.
654 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
655
656 (defvar gnus-group-use-permanent-levels nil
657   "*If non-nil, once you set a level, Gnus will use this level.")
658
659 (defvar gnus-group-list-inactive-groups t
660   "*If non-nil, inactive groups will be listed.")
661
662 (defvar gnus-show-mime nil
663   "*If non-nil, do mime processing of articles.
664 The articles will simply be fed to the function given by
665 `gnus-show-mime-method'.")
666
667 (defvar gnus-strict-mime t
668   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
669
670 (defvar gnus-show-mime-method 'metamail-buffer
671   "*Function to process a MIME message.
672 The function is called from the article buffer.")
673
674 (defvar gnus-decode-encoded-word-method (lambda ())
675   "*Function to decode a MIME encoded-words.
676 The function is called from the article buffer.")
677
678 (defvar gnus-show-threads t
679   "*If non-nil, display threads in summary mode.")
680
681 (defvar gnus-thread-hide-subtree nil
682   "*If non-nil, hide all threads initially.
683 If threads are hidden, you have to run the command
684 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
685 to expose hidden threads.")
686
687 (defvar gnus-thread-hide-killed t
688   "*If non-nil, hide killed threads automatically.")
689
690 (defvar gnus-thread-ignore-subject nil
691   "*If non-nil, ignore subjects and do all threading based on the Reference header.
692 If nil, which is the default, articles that have different subjects
693 from their parents will start separate threads.")
694
695 (defvar gnus-thread-operation-ignore-subject t
696   "*If non-nil, subjects will be ignored when doing thread commands.
697 This affects commands like `gnus-summary-kill-thread' and
698 `gnus-summary-lower-thread'.
699
700 If this variable is nil, articles in the same thread with different
701 subjects will not be included in the operation in question.  If this
702 variable is `fuzzy', only articles that have subjects that are fuzzily
703 equal will be included.")
704
705 (defvar gnus-thread-indent-level 4
706   "*Number that says how much each sub-thread should be indented.")
707
708 (defvar gnus-ignored-newsgroups
709   (purecopy (mapconcat 'identity
710                        '("^to\\."       ; not "real" groups
711                          "^[0-9. \t]+ " ; all digits in name
712                          "[][\"#'()]"   ; bogus characters
713                          )
714                        "\\|"))
715   "*A regexp to match uninteresting newsgroups in the active file.
716 Any lines in the active file matching this regular expression are
717 removed from the newsgroup list before anything else is done to it,
718 thus making them effectively non-existent.")
719
720 (defvar gnus-ignored-headers
721   "^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:"
722   "*All headers that match this regexp will be hidden.
723 This variable can also be a list of regexps of headers to be ignored.
724 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
725
726 (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-"
727   "*All headers that do not match this regexp will be hidden.
728 This variable can also be a list of regexp of headers to remain visible.
729 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
730
731 (defvar gnus-sorted-header-list
732   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
733     "^Cc:" "^Date:" "^Organization:")
734   "*This variable is a list of regular expressions.
735 If it is non-nil, headers that match the regular expressions will
736 be placed first in the article buffer in the sequence specified by
737 this list.")
738
739 (defvar gnus-boring-article-headers
740   '(empty followup-to reply-to)
741   "*Headers that are only to be displayed if they have interesting data.
742 Possible values in this list are `empty', `newsgroups', `followup-to',
743 `reply-to', and `date'.")
744
745 (defvar gnus-show-all-headers nil
746   "*If non-nil, don't hide any headers.")
747
748 (defvar gnus-save-all-headers t
749   "*If non-nil, don't remove any headers before saving.")
750
751 (defvar gnus-saved-headers gnus-visible-headers
752   "*Headers to keep if `gnus-save-all-headers' is nil.
753 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
754 If that variable is nil, however, all headers that match this regexp
755 will be kept while the rest will be deleted before saving.")
756
757 (defvar gnus-inhibit-startup-message nil
758   "*If non-nil, the startup message will not be displayed.")
759
760 (defvar gnus-signature-separator "^-- *$"
761   "Regexp matching signature separator.")
762
763 (defvar gnus-auto-extend-newsgroup t
764   "*If non-nil, extend newsgroup forward and backward when requested.")
765
766 (defvar gnus-auto-select-first t
767   "*If nil, don't select the first unread article when entering a group.
768 If this variable is `best', select the highest-scored unread article
769 in the group.  If neither nil nor `best', select the first unread
770 article.
771
772 If you want to prevent automatic selection of the first unread article
773 in some newsgroups, set the variable to nil in
774 `gnus-select-group-hook'.")
775
776 (defvar gnus-auto-select-next t
777   "*If non-nil, offer to go to the next group from the end of the previous.
778 If the value is t and the next newsgroup is empty, Gnus will exit
779 summary mode and go back to group mode.  If the value is neither nil
780 nor t, Gnus will select the following unread newsgroup.  In
781 particular, if the value is the symbol `quietly', the next unread
782 newsgroup will be selected without any confirmation, and if it is
783 `almost-quietly', the next group will be selected without any
784 confirmation if you are located on the last article in the group.
785 Finally, if this variable is `slightly-quietly', the `Z n' command
786 will go to the next group without confirmation.")
787
788 (defvar gnus-auto-select-same nil
789   "*If non-nil, select the next article with the same subject.")
790
791 (defvar gnus-summary-check-current nil
792   "*If non-nil, consider the current article when moving.
793 The \"unread\" movement commands will stay on the same line if the
794 current article is unread.")
795
796 (defvar gnus-auto-center-summary t
797   "*If non-nil, always center the current summary buffer.")
798
799 (defvar gnus-break-pages t
800   "*If non-nil, do page breaking on articles.
801 The page delimiter is specified by the `gnus-page-delimiter'
802 variable.")
803
804 (defvar gnus-page-delimiter "^\^L"
805   "*Regexp describing what to use as article page delimiters.
806 The default value is \"^\^L\", which is a form linefeed at the
807 beginning of a line.")
808
809 (defvar gnus-use-full-window t
810   "*If non-nil, use the entire Emacs screen.")
811
812 (defvar gnus-window-configuration nil
813   "Obsolete variable.  See `gnus-buffer-configuration'.")
814
815 (defvar gnus-window-min-width 2
816   "*Minimum width of Gnus buffers.")
817
818 (defvar gnus-window-min-height 1
819   "*Minimum height of Gnus buffers.")
820
821 (defvar gnus-buffer-configuration
822   '((group
823      (vertical 1.0
824                (group 1.0 point)
825                (if gnus-carpal '(group-carpal 4))))
826     (summary
827      (vertical 1.0
828                (summary 1.0 point)
829                (if gnus-carpal '(summary-carpal 4))))
830     (article
831      (cond 
832       (gnus-use-picons
833        '(frame 1.0
834                (vertical 1.0
835                          (summary 0.25 point)
836                          (if gnus-carpal '(summary-carpal 4))
837                          (article 1.0))
838                (vertical ((height . 5) (width . 15)
839                           (user-position . t)
840                           (left . -1) (top . 1))
841                          (picons 1.0))))
842       (gnus-use-trees
843        '(vertical 1.0
844                   (summary 0.25 point)
845                   (tree 0.25)
846                   (article 1.0)))
847       (t
848        '(vertical 1.0
849                  (summary 0.25 point)
850                  (if gnus-carpal '(summary-carpal 4))
851                  (if gnus-use-trees '(tree 0.25))
852                  (article 1.0)))))
853     (server
854      (vertical 1.0
855                (server 1.0 point)
856                (if gnus-carpal '(server-carpal 2))))
857     (browse
858      (vertical 1.0
859                (browse 1.0 point)
860                (if gnus-carpal '(browse-carpal 2))))
861     (group-mail
862      (vertical 1.0
863                (mail 1.0 point)))
864     (summary-mail
865      (vertical 1.0
866                (mail 1.0 point)))
867     (summary-reply
868      (vertical 1.0
869                (article 0.5)
870                (mail 1.0 point)))
871     (pick
872      (vertical 1.0
873                (article 1.0 point)))
874     (info
875      (vertical 1.0
876                (info 1.0 point)))
877     (summary-faq
878      (vertical 1.0
879                (summary 0.25)
880                (faq 1.0 point)))
881     (edit-group
882      (vertical 1.0
883                (group 0.5)
884                (edit-group 1.0 point)))
885     (edit-server
886      (vertical 1.0
887                (server 0.5)
888                (edit-server 1.0 point)))
889     (edit-score
890      (vertical 1.0
891                (summary 0.25)
892                (edit-score 1.0 point)))
893     (post
894      (vertical 1.0
895                (post 1.0 point)))
896     (reply
897      (vertical 1.0
898                (article 0.5)
899                (mail 1.0 point)))
900     (mail-forward
901      (vertical 1.0
902                (mail 1.0 point)))
903     (post-forward
904      (vertical 1.0
905                (post 1.0 point)))
906     (reply-yank
907      (vertical 1.0
908                (mail 1.0 point)))
909     (mail-bounce
910      (vertical 1.0
911                (article 0.5)
912                (mail 1.0 point)))
913     (draft
914      (vertical 1.0
915                (draft 1.0 point)))
916     (pipe
917      (vertical 1.0
918                (summary 0.25 point)
919                (if gnus-carpal '(summary-carpal 4))
920                ("*Shell Command Output*" 1.0)))
921     (followup
922      (vertical 1.0
923                (article 0.5)
924                (post 1.0 point)))
925     (followup-yank
926      (vertical 1.0
927                (post 1.0 point))))
928   "Window configuration for all possible Gnus buffers.
929 This variable is a list of lists.  Each of these lists has a NAME and
930 a RULE.  The NAMEs are commonsense names like `group', which names a
931 rule used when displaying the group buffer; `summary', which names a
932 rule for what happens when you enter a group and do not display an
933 article buffer; and so on.  See the value of this variable for a
934 complete list of NAMEs.
935
936 Each RULE is a list of vectors.  The first element in this vector is
937 the name of the buffer to be displayed; the second element is the
938 percentage of the screen this buffer is to occupy (a number in the
939 0.0-0.99 range); the optional third element is `point', which should
940 be present to denote which buffer point is to go to after making this
941 buffer configuration.")
942
943 (defvar gnus-window-to-buffer
944   '((group . gnus-group-buffer)
945     (summary . gnus-summary-buffer)
946     (article . gnus-article-buffer)
947     (server . gnus-server-buffer)
948     (browse . "*Gnus Browse Server*")
949     (edit-group . gnus-group-edit-buffer)
950     (edit-server . gnus-server-edit-buffer)
951     (group-carpal . gnus-carpal-group-buffer)
952     (summary-carpal . gnus-carpal-summary-buffer)
953     (server-carpal . gnus-carpal-server-buffer)
954     (browse-carpal . gnus-carpal-browse-buffer)
955     (edit-score . gnus-score-edit-buffer)
956     (mail . gnus-mail-buffer)
957     (post . gnus-post-news-buffer)
958     (faq . gnus-faq-buffer)
959     (picons . "*Picons*")
960     (tree . gnus-tree-buffer)
961     (info . gnus-info-buffer)
962     (draft . gnus-draft-buffer))
963   "Mapping from short symbols to buffer names or buffer variables.")
964
965 (defvar gnus-carpal nil
966   "*If non-nil, display clickable icons.")
967
968 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
969   "*Function called with a group name when new group is detected.
970 A few pre-made functions are supplied: `gnus-subscribe-randomly'
971 inserts new groups at the beginning of the list of groups;
972 `gnus-subscribe-alphabetically' inserts new groups in strict
973 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
974 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
975 for your decision; `gnus-subscribe-killed' kills all new groups.")
976
977 ;; Suggested by a bug report by Hallvard B Furuseth.
978 ;; <h.b.furuseth@usit.uio.no>.
979 (defvar gnus-subscribe-options-newsgroup-method
980   (function gnus-subscribe-alphabetically)
981   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
982 If, for instance, you want to subscribe to all newsgroups in the
983 \"no\" and \"alt\" hierarchies, you'd put the following in your
984 .newsrc file:
985
986 options -n no.all alt.all
987
988 Gnus will the subscribe all new newsgroups in these hierarchies with
989 the subscription method in this variable.")
990
991 (defvar gnus-subscribe-hierarchical-interactive nil
992   "*If non-nil, Gnus will offer to subscribe hierarchically.
993 When a new hierarchy appears, Gnus will ask the user:
994
995 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
996
997 If the user pressed `d', Gnus will descend the hierarchy, `y' will
998 subscribe to all newsgroups in the hierarchy and `s' will skip this
999 hierarchy in its entirety.")
1000
1001 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1002   "*Function used for sorting the group buffer.
1003 This function will be called with group info entries as the arguments
1004 for the groups to be sorted.  Pre-made functions include
1005 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1006 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1007 `gnus-group-sort-by-rank'.
1008
1009 This variable can also be a list of sorting functions.  In that case,
1010 the most significant sort function should be the last function in the
1011 list.")
1012
1013 ;; Mark variables suggested by Thomas Michanek
1014 ;; <Thomas.Michanek@telelogic.se>.
1015 (defvar gnus-unread-mark ? 
1016   "*Mark used for unread articles.")
1017 (defvar gnus-ticked-mark ?!
1018   "*Mark used for ticked articles.")
1019 (defvar gnus-dormant-mark ??
1020   "*Mark used for dormant articles.")
1021 (defvar gnus-del-mark ?r
1022   "*Mark used for del'd articles.")
1023 (defvar gnus-read-mark ?R
1024   "*Mark used for read articles.")
1025 (defvar gnus-expirable-mark ?E
1026   "*Mark used for expirable articles.")
1027 (defvar gnus-killed-mark ?K
1028   "*Mark used for killed articles.")
1029 (defvar gnus-souped-mark ?F
1030   "*Mark used for killed articles.")
1031 (defvar gnus-kill-file-mark ?X
1032   "*Mark used for articles killed by kill files.")
1033 (defvar gnus-low-score-mark ?Y
1034   "*Mark used for articles with a low score.")
1035 (defvar gnus-catchup-mark ?C
1036   "*Mark used for articles that are caught up.")
1037 (defvar gnus-replied-mark ?A
1038   "*Mark used for articles that have been replied to.")
1039 (defvar gnus-cached-mark ?*
1040   "*Mark used for articles that are in the cache.")
1041 (defvar gnus-saved-mark ?S
1042   "*Mark used for articles that have been saved to.")
1043 (defvar gnus-process-mark ?#
1044   "*Process mark.")
1045 (defvar gnus-ancient-mark ?O
1046   "*Mark used for ancient articles.")
1047 (defvar gnus-sparse-mark ?Q
1048   "*Mark used for sparsely reffed articles.")
1049 (defvar gnus-canceled-mark ?G
1050   "*Mark used for canceled articles.")
1051 (defvar gnus-score-over-mark ?+
1052   "*Score mark used for articles with high scores.")
1053 (defvar gnus-score-below-mark ?-
1054   "*Score mark used for articles with low scores.")
1055 (defvar gnus-empty-thread-mark ? 
1056   "*There is no thread under the article.")
1057 (defvar gnus-not-empty-thread-mark ?=
1058   "*There is a thread under the article.")
1059
1060 (defvar gnus-view-pseudo-asynchronously nil
1061   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1062
1063 (defvar gnus-view-pseudos nil
1064   "*If `automatic', pseudo-articles will be viewed automatically.
1065 If `not-confirm', pseudos will be viewed automatically, and the user
1066 will not be asked to confirm the command.")
1067
1068 (defvar gnus-view-pseudos-separately t
1069   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1070 If nil, all files that use the same viewing command will be given as a
1071 list of parameters to that command.")
1072
1073 (defvar gnus-insert-pseudo-articles t
1074   "*If non-nil, insert pseudo-articles when decoding articles.")
1075
1076 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1077   "*Format of group lines.
1078 It works along the same lines as a normal formatting string,
1079 with some simple extensions.
1080
1081 %M    Only marked articles (character, \"*\" or \" \")
1082 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1083 %L    Level of subscribedness (integer)
1084 %N    Number of unread articles (integer)
1085 %I    Number of dormant articles (integer)
1086 %i    Number of ticked and dormant (integer)
1087 %T    Number of ticked articles (integer)
1088 %R    Number of read articles (integer)
1089 %t    Total number of articles (integer)
1090 %y    Number of unread, unticked articles (integer)
1091 %G    Group name (string)
1092 %g    Qualified group name (string)
1093 %D    Group description (string)
1094 %s    Select method (string)
1095 %o    Moderated group (char, \"m\")
1096 %p    Process mark (char)
1097 %O    Moderated group (string, \"(m)\" or \"\")
1098 %P    Topic indentation (string)
1099 %l    Whether there are GroupLens predictions for this group (string)
1100 %n    Select from where (string)
1101 %z    A string that look like `<%s:%n>' if a foreign select method is used
1102 %u    User defined specifier.  The next character in the format string should
1103       be a letter.  Gnus will call the function gnus-user-format-function-X,
1104       where X is the letter following %u.  The function will be passed the
1105       current header as argument.  The function should return a string, which
1106       will be inserted into the buffer just like information from any other
1107       group specifier.
1108
1109 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1110 the mouse point move inside the area.  There can only be one such area.
1111
1112 Note that this format specification is not always respected.  For
1113 reasons of efficiency, when listing killed groups, this specification
1114 is ignored altogether.  If the spec is changed considerably, your
1115 output may end up looking strange when listing both alive and killed
1116 groups.
1117
1118 If you use %o or %O, reading the active file will be slower and quite
1119 a bit of extra memory will be used. %D will also worsen performance.
1120 Also note that if you change the format specification to include any
1121 of these specs, you must probably re-start Gnus to see them go into
1122 effect.")
1123
1124 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1125   "*The format specification of the lines in the summary buffer.
1126
1127 It works along the same lines as a normal formatting string,
1128 with some simple extensions.
1129
1130 %N   Article number, left padded with spaces (string)
1131 %S   Subject (string)
1132 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1133 %n   Name of the poster (string)
1134 %a   Extracted name of the poster (string)
1135 %A   Extracted address of the poster (string)
1136 %F   Contents of the From: header (string)
1137 %x   Contents of the Xref: header (string)
1138 %D   Date of the article (string)
1139 %d   Date of the article (string) in DD-MMM format
1140 %M   Message-id of the article (string)
1141 %r   References of the article (string)
1142 %c   Number of characters in the article (integer)
1143 %L   Number of lines in the article (integer)
1144 %I   Indentation based on thread level (a string of spaces)
1145 %T   A string with two possible values: 80 spaces if the article
1146      is on thread level two or larger and 0 spaces on level one
1147 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1148 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1149 %[   Opening bracket (character, \"[\" or \"<\")
1150 %]   Closing bracket (character, \"]\" or \">\")
1151 %>   Spaces of length thread-level (string)
1152 %<   Spaces of length (- 20 thread-level) (string)
1153 %i   Article score (number)
1154 %z   Article zcore (character)
1155 %t   Number of articles under the current thread (number).
1156 %e   Whether the thread is empty or not (character).
1157 %l   GroupLens score (number)
1158 %u   User defined specifier.  The next character in the format string should
1159      be a letter.  Gnus will call the function gnus-user-format-function-X,
1160      where X is the letter following %u.  The function will be passed the
1161      current header as argument.  The function should return a string, which
1162      will be inserted into the summary just like information from any other
1163      summary specifier.
1164
1165 Text between %( and %) will be highlighted with `gnus-mouse-face'
1166 when the mouse point is placed inside the area.  There can only be one
1167 such area.
1168
1169 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1170 with care.  For reasons of efficiency, Gnus will compute what column
1171 these characters will end up in, and \"hard-code\" that.  This means that
1172 it is illegal to have these specs after a variable-length spec.  Well,
1173 you might not be arrested, but your summary buffer will look strange,
1174 which is bad enough.
1175
1176 The smart choice is to have these specs as for to the left as
1177 possible.
1178
1179 This restriction may disappear in later versions of Gnus.")
1180
1181 (defvar gnus-summary-dummy-line-format
1182   "*  %(:                          :%) %S\n"
1183   "*The format specification for the dummy roots in the summary buffer.
1184 It works along the same lines as a normal formatting string,
1185 with some simple extensions.
1186
1187 %S  The subject")
1188
1189 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1190   "*The format specification for the summary mode line.
1191 It works along the same lines as a normal formatting string,
1192 with some simple extensions:
1193
1194 %G  Group name
1195 %p  Unprefixed group name
1196 %A  Current article number
1197 %V  Gnus version
1198 %U  Number of unread articles in the group
1199 %e  Number of unselected articles in the group
1200 %Z  A string with unread/unselected article counts
1201 %g  Shortish group name
1202 %S  Subject of the current article
1203 %u  User-defined spec
1204 %s  Current score file name
1205 %d  Number of dormant articles
1206 %r  Number of articles that have been marked as read in this session
1207 %E  Number of articles expunged by the score files")
1208
1209 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1210   "*The format specification for the article mode line.
1211 See `gnus-summary-mode-line-format' for a closer description.")
1212
1213 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1214   "*The format specification for the group mode line.
1215 It works along the same lines as a normal formatting string,
1216 with some simple extensions:
1217
1218 %S   The native news server.
1219 %M   The native select method.")
1220
1221 (defvar gnus-valid-select-methods
1222   '(("nntp" post address prompt-address)
1223     ("nnspool" post address)
1224     ("nnvirtual" post-mail virtual prompt-address)
1225     ("nnmbox" mail respool address)
1226     ("nnml" mail respool address)
1227     ("nnmh" mail respool address)
1228     ("nndir" post-mail prompt-address address)
1229     ("nneething" none address prompt-address)
1230     ("nndoc" none address prompt-address)
1231     ("nnbabyl" mail address respool)
1232     ("nnkiboze" post address virtual)
1233     ("nnsoup" post-mail address)
1234     ("nndraft" post-mail)
1235     ("nnfolder" mail respool address))
1236   "An alist of valid select methods.
1237 The first element of each list lists should be a string with the name
1238 of the select method.  The other elements may be be the category of
1239 this method (ie. `post', `mail', `none' or whatever) or other
1240 properties that this method has (like being respoolable).
1241 If you implement a new select method, all you should have to change is
1242 this variable.  I think.")
1243
1244 (defvar gnus-updated-mode-lines '(group article summary tree)
1245   "*List of buffers that should update their mode lines.
1246 The list may contain the symbols `group', `article' and `summary'.  If
1247 the corresponding symbol is present, Gnus will keep that mode line
1248 updated with information that may be pertinent.
1249 If this variable is nil, screen refresh may be quicker.")
1250
1251 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1252 (defvar gnus-mode-non-string-length nil
1253   "*Max length of mode-line non-string contents.
1254 If this is nil, Gnus will take space as is needed, leaving the rest
1255 of the modeline intact.")
1256
1257 ;see gnus-cus.el
1258 ;(defvar gnus-mouse-face 'highlight
1259 ;  "*Face used for mouse highlighting in Gnus.
1260 ;No mouse highlights will be done if `gnus-visual' is nil.")
1261
1262 (defvar gnus-summary-mark-below nil
1263   "*Mark all articles with a score below this variable as read.
1264 This variable is local to each summary buffer and usually set by the
1265 score file.")
1266
1267 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1268   "*List of functions used for sorting articles in the summary buffer.
1269 This variable is only used when not using a threaded display.")
1270
1271 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1272   "*List of functions used for sorting threads in the summary buffer.
1273 By default, threads are sorted by article number.
1274
1275 Each function takes two threads and return non-nil if the first thread
1276 should be sorted before the other.  If you use more than one function,
1277 the primary sort function should be the last.  You should probably
1278 always include `gnus-thread-sort-by-number' in the list of sorting
1279 functions -- preferably first.
1280
1281 Ready-mady functions include `gnus-thread-sort-by-number',
1282 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1283 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1284 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1285
1286 (defvar gnus-thread-score-function '+
1287   "*Function used for calculating the total score of a thread.
1288
1289 The function is called with the scores of the article and each
1290 subthread and should then return the score of the thread.
1291
1292 Some functions you can use are `+', `max', or `min'.")
1293
1294 (defvar gnus-summary-expunge-below nil
1295   "All articles that have a score less than this variable will be expunged.")
1296
1297 (defvar gnus-thread-expunge-below nil
1298   "All threads that have a total score less than this variable will be expunged.
1299 See `gnus-thread-score-function' for en explanation of what a
1300 \"thread score\" is.")
1301
1302 (defvar gnus-auto-subscribed-groups
1303   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1304   "*All new groups that match this regexp will be subscribed automatically.
1305 Note that this variable only deals with new groups.  It has no effect
1306 whatsoever on old groups.")
1307
1308 (defvar gnus-options-subscribe nil
1309   "*All new groups matching this regexp will be subscribed unconditionally.
1310 Note that this variable deals only with new newsgroups.  This variable
1311 does not affect old newsgroups.")
1312
1313 (defvar gnus-options-not-subscribe nil
1314   "*All new groups matching this regexp will be ignored.
1315 Note that this variable deals only with new newsgroups.  This variable
1316 does not affect old (already subscribed) newsgroups.")
1317
1318 (defvar gnus-auto-expirable-newsgroups nil
1319   "*Groups in which to automatically mark read articles as expirable.
1320 If non-nil, this should be a regexp that should match all groups in
1321 which to perform auto-expiry.  This only makes sense for mail groups.")
1322
1323 (defvar gnus-total-expirable-newsgroups nil
1324   "*Groups in which to perform expiry of all read articles.
1325 Use with extreme caution.  All groups that match this regexp will be
1326 expiring - which means that all read articles will be deleted after
1327 (say) one week.  (This only goes for mail groups and the like, of
1328 course.)")
1329
1330 (defvar gnus-group-uncollapsed-levels 1
1331   "Number of group name elements to leave alone when making a short group name.")
1332
1333 (defvar gnus-hidden-properties '(invisible t intangible t)
1334   "Property list to use for hiding text.")
1335
1336 (defvar gnus-modtime-botch nil
1337   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1338 due to the bogus appearance that .newsrc was modified on disc.")
1339
1340 ;; Hooks.
1341
1342 (defvar gnus-group-mode-hook nil
1343   "*A hook for Gnus group mode.")
1344
1345 (defvar gnus-summary-mode-hook nil
1346   "*A hook for Gnus summary mode.
1347 This hook is run before any variables are set in the summary buffer.")
1348
1349 (defvar gnus-article-mode-hook nil
1350   "*A hook for Gnus article mode.")
1351
1352 (defvar gnus-summary-prepare-exit-hook nil
1353   "*A hook called when preparing to exit from the summary buffer.
1354 It calls `gnus-summary-expire-articles' by default.")
1355 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1356
1357 (defvar gnus-summary-exit-hook nil
1358   "*A hook called on exit from the summary buffer.")
1359
1360 (defvar gnus-group-catchup-group-hook nil
1361   "*A hook run when catching up a group from the group buffer.")
1362
1363 (defvar gnus-open-server-hook nil
1364   "*A hook called just before opening connection to the news server.")
1365
1366 (defvar gnus-load-hook nil
1367   "*A hook run while Gnus is loaded.")
1368
1369 (defvar gnus-startup-hook nil
1370   "*A hook called at startup.
1371 This hook is called after Gnus is connected to the NNTP server.")
1372
1373 (defvar gnus-get-new-news-hook nil
1374   "*A hook run just before Gnus checks for new news.")
1375
1376 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1377   "*A function that is called to generate the group buffer.
1378 The function is called with three arguments: The first is a number;
1379 all group with a level less or equal to that number should be listed,
1380 if the second is non-nil, empty groups should also be displayed.  If
1381 the third is non-nil, it is a number.  No groups with a level lower
1382 than this number should be displayed.
1383
1384 The only current function implemented is `gnus-group-prepare-flat'.")
1385
1386 (defvar gnus-group-prepare-hook nil
1387   "*A hook called after the group buffer has been generated.
1388 If you want to modify the group buffer, you can use this hook.")
1389
1390 (defvar gnus-summary-prepare-hook nil
1391   "*A hook called after the summary buffer has been generated.
1392 If you want to modify the summary buffer, you can use this hook.")
1393
1394 (defvar gnus-summary-generate-hook nil
1395   "*A hook run just before generating the summary buffer.
1396 This hook is commonly used to customize threading variables and the
1397 like.")
1398
1399 (defvar gnus-article-prepare-hook nil
1400   "*A hook called after an article has been prepared in the article buffer.
1401 If you want to run a special decoding program like nkf, use this hook.")
1402
1403 ;(defvar gnus-article-display-hook nil
1404 ;  "*A hook called after the article is displayed in the article buffer.
1405 ;The hook is designed to change the contents of the article
1406 ;buffer.  Typical functions that this hook may contain are
1407 ;`gnus-article-hide-headers' (hide selected headers),
1408 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1409 ;`gnus-article-hide-signature' (hide signature) and
1410 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1411 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1413 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1414
1415 (defvar gnus-article-x-face-command
1416   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1417   "String or function to be executed to display an X-Face header.
1418 If it is a string, the command will be executed in a sub-shell
1419 asynchronously.  The compressed face will be piped to this command.")
1420
1421 (defvar gnus-article-x-face-too-ugly nil
1422   "Regexp matching posters whose face shouldn't be shown automatically.")
1423
1424 (defvar gnus-select-group-hook nil
1425   "*A hook called when a newsgroup is selected.
1426
1427 If you'd like to simplify subjects like the
1428 `gnus-summary-next-same-subject' command does, you can use the
1429 following hook:
1430
1431  (setq gnus-select-group-hook
1432       (list
1433         (lambda ()
1434           (mapcar (lambda (header)
1435                      (mail-header-set-subject
1436                       header
1437                       (gnus-simplify-subject
1438                        (mail-header-subject header) 're-only)))
1439                   gnus-newsgroup-headers))))")
1440
1441 (defvar gnus-select-article-hook nil
1442   "*A hook called when an article is selected.")
1443
1444 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1445   "*A hook called to apply kill files to a group.
1446 This hook is intended to apply a kill file to the selected newsgroup.
1447 The function `gnus-apply-kill-file' is called by default.
1448
1449 Since a general kill file is too heavy to use only for a few
1450 newsgroups, I recommend you to use a lighter hook function.  For
1451 example, if you'd like to apply a kill file to articles which contains
1452 a string `rmgroup' in subject in newsgroup `control', you can use the
1453 following hook:
1454
1455  (setq gnus-apply-kill-hook
1456       (list
1457         (lambda ()
1458           (cond ((string-match \"control\" gnus-newsgroup-name)
1459                  (gnus-kill \"Subject\" \"rmgroup\")
1460                  (gnus-expunge \"X\"))))))")
1461
1462 (defvar gnus-visual-mark-article-hook
1463   (list 'gnus-highlight-selected-summary)
1464   "*Hook run after selecting an article in the summary buffer.
1465 It is meant to be used for highlighting the article in some way.  It
1466 is not run if `gnus-visual' is nil.")
1467
1468 (defvar gnus-parse-headers-hook nil
1469   "*A hook called before parsing the headers.")
1470
1471 (defvar gnus-exit-group-hook nil
1472   "*A hook called when exiting (not quitting) summary mode.")
1473
1474 (defvar gnus-suspend-gnus-hook nil
1475   "*A hook called when suspending (not exiting) Gnus.")
1476
1477 (defvar gnus-exit-gnus-hook nil
1478   "*A hook called when exiting Gnus.")
1479
1480 (defvar gnus-after-exiting-gnus-hook nil
1481   "*A hook called after exiting Gnus.")
1482
1483 (defvar gnus-save-newsrc-hook nil
1484   "*A hook called before saving any of the newsrc files.")
1485
1486 (defvar gnus-save-quick-newsrc-hook nil
1487   "*A hook called just before saving the quick newsrc file.
1488 Can be used to turn version control on or off.")
1489
1490 (defvar gnus-save-standard-newsrc-hook nil
1491   "*A hook called just before saving the standard newsrc file.
1492 Can be used to turn version control on or off.")
1493
1494 (defvar gnus-summary-update-hook
1495   (list 'gnus-summary-highlight-line)
1496   "*A hook called when a summary line is changed.
1497 The hook will not be called if `gnus-visual' is nil.
1498
1499 The default function `gnus-summary-highlight-line' will
1500 highlight the line according to the `gnus-summary-highlight'
1501 variable.")
1502
1503 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1504   "*A hook called when a group line is changed.
1505 The hook will not be called if `gnus-visual' is nil.
1506
1507 The default function `gnus-group-highlight-line' will
1508 highlight the line according to the `gnus-group-highlight'
1509 variable.")
1510
1511 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1512   "*A hook called when an article is selected for the first time.
1513 The hook is intended to mark an article as read (or unread)
1514 automatically when it is selected.")
1515
1516 (defvar gnus-group-change-level-function nil
1517   "Function run when a group level is changed.
1518 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1519
1520 ;; Remove any hilit infestation.
1521 (add-hook 'gnus-startup-hook
1522           (lambda ()
1523             (remove-hook 'gnus-summary-prepare-hook
1524                          'hilit-rehighlight-buffer-quietly)
1525             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1526             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1527             (remove-hook 'gnus-article-prepare-hook
1528                          'hilit-rehighlight-buffer-quietly)))
1529
1530 \f
1531 ;; Internal variables
1532
1533 (defvar gnus-server-alist nil
1534   "List of available servers.")
1535
1536 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1537
1538 (defvar gnus-goto-missing-group-function nil)
1539
1540 (defvar gnus-override-subscribe-method nil)
1541
1542 (defvar gnus-group-goto-next-group-function nil
1543   "Function to override finding the next group after listing groups.")
1544
1545 (defconst gnus-article-mark-lists
1546   '((marked . tick) (replied . reply)
1547     (expirable . expire) (killed . killed)
1548     (bookmarks . bookmark) (dormant . dormant)
1549     (scored . score) (saved . save)
1550     (cached . cache)
1551     ))
1552
1553 ;; Avoid highlighting in kill files.
1554 (defvar gnus-summary-inhibit-highlight nil)
1555 (defvar gnus-newsgroup-selected-overlay nil)
1556
1557 (defvar gnus-inhibit-hiding nil)
1558 (defvar gnus-group-indentation "")
1559 (defvar gnus-inhibit-limiting nil)
1560 (defvar gnus-created-frames nil)
1561
1562 (defvar gnus-article-mode-map nil)
1563 (defvar gnus-dribble-buffer nil)
1564 (defvar gnus-headers-retrieved-by nil)
1565 (defvar gnus-article-reply nil)
1566 (defvar gnus-override-method nil)
1567 (defvar gnus-article-check-size nil)
1568
1569 (defvar gnus-current-score-file nil)
1570 (defvar gnus-newsgroup-adaptive-score-file nil)
1571 (defvar gnus-scores-exclude-files nil)
1572
1573 (defvar gnus-opened-servers nil)
1574
1575 (defvar gnus-current-move-group nil)
1576
1577 (defvar gnus-newsgroup-dependencies nil)
1578 (defvar gnus-newsgroup-async nil)
1579 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1580
1581 (defvar gnus-newsgroup-adaptive nil)
1582
1583 (defvar gnus-summary-display-table nil)
1584 (defvar gnus-summary-display-article-function nil)
1585
1586 (defvar gnus-summary-highlight-line-function nil
1587   "Function called after highlighting a summary line.")
1588
1589 (defvar gnus-group-line-format-alist
1590   `((?M gnus-tmp-marked-mark ?c)
1591     (?S gnus-tmp-subscribed ?c)
1592     (?L gnus-tmp-level ?d)
1593     (?N (cond ((eq number t) "*" )
1594               ((numberp number) 
1595                (int-to-string
1596                 (+ number
1597                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1598                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1599               (t number)) ?s)
1600     (?R gnus-tmp-number-of-read ?s)
1601     (?t gnus-tmp-number-total ?d)
1602     (?y gnus-tmp-number-of-unread ?s)
1603     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1604     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1605     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1606            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1607     (?g gnus-tmp-group ?s)
1608     (?G gnus-tmp-qualified-group ?s)
1609     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1610     (?D gnus-tmp-newsgroup-description ?s)
1611     (?o gnus-tmp-moderated ?c)
1612     (?O gnus-tmp-moderated-string ?s)
1613     (?p gnus-tmp-process-marked ?c)
1614     (?s gnus-tmp-news-server ?s)
1615     (?n gnus-tmp-news-method ?s)
1616     (?P gnus-group-indentation ?s)
1617     (?l gnus-tmp-grouplens ?s)
1618     (?z gnus-tmp-news-method-string ?s)
1619     (?u gnus-tmp-user-defined ?s)))
1620
1621 (defvar gnus-summary-line-format-alist
1622   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1623     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1624     (?s gnus-tmp-subject-or-nil ?s)
1625     (?n gnus-tmp-name ?s)
1626     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1627         ?s)
1628     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1629             gnus-tmp-from) ?s)
1630     (?F gnus-tmp-from ?s)
1631     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1632     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1633     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1634     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1635     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1636     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1637     (?L gnus-tmp-lines ?d)
1638     (?I gnus-tmp-indentation ?s)
1639     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1640     (?R gnus-tmp-replied ?c)
1641     (?\[ gnus-tmp-opening-bracket ?c)
1642     (?\] gnus-tmp-closing-bracket ?c)
1643     (?\> (make-string gnus-tmp-level ? ) ?s)
1644     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1645     (?i gnus-tmp-score ?d)
1646     (?z gnus-tmp-score-char ?c)
1647     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1648     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1649     (?U gnus-tmp-unread ?c)
1650     (?t (gnus-summary-number-of-articles-in-thread
1651          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1652         ?d)
1653     (?e (gnus-summary-number-of-articles-in-thread
1654          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1655         ?c)
1656     (?u gnus-tmp-user-defined ?s))
1657   "An alist of format specifications that can appear in summary lines,
1658 and what variables they correspond with, along with the type of the
1659 variable (string, integer, character, etc).")
1660
1661 (defvar gnus-summary-dummy-line-format-alist
1662   `((?S gnus-tmp-subject ?s)
1663     (?N gnus-tmp-number ?d)
1664     (?u gnus-tmp-user-defined ?s)))
1665
1666 (defvar gnus-summary-mode-line-format-alist
1667   `((?G gnus-tmp-group-name ?s)
1668     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1669     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1670     (?A gnus-tmp-article-number ?d)
1671     (?Z gnus-tmp-unread-and-unselected ?s)
1672     (?V gnus-version ?s)
1673     (?U gnus-tmp-unread ?d)
1674     (?S gnus-tmp-subject ?s)
1675     (?e gnus-tmp-unselected ?d)
1676     (?u gnus-tmp-user-defined ?s)
1677     (?d (length gnus-newsgroup-dormant) ?d)
1678     (?t (length gnus-newsgroup-marked) ?d)
1679     (?r (length gnus-newsgroup-reads) ?d)
1680     (?E gnus-newsgroup-expunged-tally ?d)
1681     (?s (gnus-current-score-file-nondirectory) ?s)))
1682
1683 (defvar gnus-article-mode-line-format-alist
1684   gnus-summary-mode-line-format-alist)
1685
1686 (defvar gnus-group-mode-line-format-alist
1687   `((?S gnus-tmp-news-server ?s)
1688     (?M gnus-tmp-news-method ?s)
1689     (?u gnus-tmp-user-defined ?s)))
1690
1691 (defvar gnus-have-read-active-file nil)
1692
1693 (defconst gnus-maintainer
1694   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1695   "The mail address of the Gnus maintainers.")
1696
1697 (defconst gnus-version "September Gnus v0.44"
1698   "Version number for this version of Gnus.")
1699
1700 (defvar gnus-info-nodes
1701   '((gnus-group-mode            "(gnus)The Group Buffer")
1702     (gnus-summary-mode          "(gnus)The Summary Buffer")
1703     (gnus-article-mode          "(gnus)The Article Buffer"))
1704   "Assoc list of major modes and related Info nodes.")
1705
1706 (defvar gnus-group-buffer "*Group*")
1707 (defvar gnus-summary-buffer "*Summary*")
1708 (defvar gnus-article-buffer "*Article*")
1709 (defvar gnus-server-buffer "*Server*")
1710
1711 (defvar gnus-work-buffer " *gnus work*")
1712
1713 (defvar gnus-original-article-buffer " *Original Article*")
1714 (defvar gnus-original-article nil)
1715
1716 (defvar gnus-buffer-list nil
1717   "Gnus buffers that should be killed on exit.")
1718
1719 (defvar gnus-slave nil
1720   "Whether this Gnus is a slave or not.")
1721
1722 (defvar gnus-variable-list
1723   '(gnus-newsrc-options gnus-newsrc-options-n
1724     gnus-newsrc-last-checked-date
1725     gnus-newsrc-alist gnus-server-alist
1726     gnus-killed-list gnus-zombie-list
1727     gnus-topic-topology gnus-topic-alist
1728     gnus-format-specs)
1729   "Gnus variables saved in the quick startup file.")
1730
1731 (defvar gnus-newsrc-options nil
1732   "Options line in the .newsrc file.")
1733
1734 (defvar gnus-newsrc-options-n nil
1735   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1736
1737 (defvar gnus-newsrc-last-checked-date nil
1738   "Date Gnus last asked server for new newsgroups.")
1739
1740 (defvar gnus-topic-topology nil
1741   "The complete topic hierarchy.")
1742
1743 (defvar gnus-topic-alist nil
1744   "The complete topic-group alist.")
1745
1746 (defvar gnus-newsrc-alist nil
1747   "Assoc list of read articles.
1748 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1749
1750 (defvar gnus-newsrc-hashtb nil
1751   "Hashtable of gnus-newsrc-alist.")
1752
1753 (defvar gnus-killed-list nil
1754   "List of killed newsgroups.")
1755
1756 (defvar gnus-killed-hashtb nil
1757   "Hash table equivalent of gnus-killed-list.")
1758
1759 (defvar gnus-zombie-list nil
1760   "List of almost dead newsgroups.")
1761
1762 (defvar gnus-description-hashtb nil
1763   "Descriptions of newsgroups.")
1764
1765 (defvar gnus-list-of-killed-groups nil
1766   "List of newsgroups that have recently been killed by the user.")
1767
1768 (defvar gnus-active-hashtb nil
1769   "Hashtable of active articles.")
1770
1771 (defvar gnus-moderated-list nil
1772   "List of moderated newsgroups.")
1773
1774 (defvar gnus-group-marked nil)
1775
1776 (defvar gnus-current-startup-file nil
1777   "Startup file for the current host.")
1778
1779 (defvar gnus-last-search-regexp nil
1780   "Default regexp for article search command.")
1781
1782 (defvar gnus-last-shell-command nil
1783   "Default shell command on article.")
1784
1785 (defvar gnus-current-select-method nil
1786   "The current method for selecting a newsgroup.")
1787
1788 (defvar gnus-group-list-mode nil)
1789
1790 (defvar gnus-article-internal-prepare-hook nil)
1791
1792 (defvar gnus-newsgroup-name nil)
1793 (defvar gnus-newsgroup-begin nil)
1794 (defvar gnus-newsgroup-end nil)
1795 (defvar gnus-newsgroup-last-rmail nil)
1796 (defvar gnus-newsgroup-last-mail nil)
1797 (defvar gnus-newsgroup-last-folder nil)
1798 (defvar gnus-newsgroup-last-file nil)
1799 (defvar gnus-newsgroup-auto-expire nil)
1800 (defvar gnus-newsgroup-active nil)
1801
1802 (defvar gnus-newsgroup-data nil)
1803 (defvar gnus-newsgroup-data-reverse nil)
1804 (defvar gnus-newsgroup-limit nil)
1805 (defvar gnus-newsgroup-limits nil)
1806
1807 (defvar gnus-newsgroup-unreads nil
1808   "List of unread articles in the current newsgroup.")
1809
1810 (defvar gnus-newsgroup-unselected nil
1811   "List of unselected unread articles in the current newsgroup.")
1812
1813 (defvar gnus-newsgroup-reads nil
1814   "Alist of read articles and article marks in the current newsgroup.")
1815
1816 (defvar gnus-newsgroup-expunged-tally nil)
1817
1818 (defvar gnus-newsgroup-marked nil
1819   "List of ticked articles in the current newsgroup (a subset of unread art).")
1820
1821 (defvar gnus-newsgroup-killed nil
1822   "List of ranges of articles that have been through the scoring process.")
1823
1824 (defvar gnus-newsgroup-cached nil
1825   "List of articles that come from the article cache.")
1826
1827 (defvar gnus-newsgroup-saved nil
1828   "List of articles that have been saved.")
1829
1830 (defvar gnus-newsgroup-kill-headers nil)
1831
1832 (defvar gnus-newsgroup-replied nil
1833   "List of articles that have been replied to in the current newsgroup.")
1834
1835 (defvar gnus-newsgroup-expirable nil
1836   "List of articles in the current newsgroup that can be expired.")
1837
1838 (defvar gnus-newsgroup-processable nil
1839   "List of articles in the current newsgroup that can be processed.")
1840
1841 (defvar gnus-newsgroup-bookmarks nil
1842   "List of articles in the current newsgroup that have bookmarks.")
1843
1844 (defvar gnus-newsgroup-dormant nil
1845   "List of dormant articles in the current newsgroup.")
1846
1847 (defvar gnus-newsgroup-scored nil
1848   "List of scored articles in the current newsgroup.")
1849
1850 (defvar gnus-newsgroup-headers nil
1851   "List of article headers in the current newsgroup.")
1852
1853 (defvar gnus-newsgroup-threads nil)
1854
1855 (defvar gnus-newsgroup-prepared nil
1856   "Whether the current group has been prepared properly.")
1857
1858 (defvar gnus-newsgroup-ancient nil
1859   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1860
1861 (defvar gnus-newsgroup-sparse nil)
1862
1863 (defvar gnus-current-article nil)
1864 (defvar gnus-article-current nil)
1865 (defvar gnus-current-headers nil)
1866 (defvar gnus-have-all-headers nil)
1867 (defvar gnus-last-article nil)
1868 (defvar gnus-newsgroup-history nil)
1869 (defvar gnus-current-kill-article nil)
1870
1871 ;; Save window configuration.
1872 (defvar gnus-prev-winconf nil)
1873
1874 (defvar gnus-summary-mark-positions nil)
1875 (defvar gnus-group-mark-positions nil)
1876
1877 (defvar gnus-reffed-article-number nil)
1878
1879 ;;; Let the byte-compiler know that we know about this variable.
1880 (defvar rmail-default-rmail-file)
1881
1882 (defvar gnus-cache-removable-articles nil)
1883
1884 (defvar gnus-dead-summary nil)
1885
1886 (defconst gnus-summary-local-variables
1887   '(gnus-newsgroup-name
1888     gnus-newsgroup-begin gnus-newsgroup-end
1889     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1890     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1891     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1892     gnus-newsgroup-unselected gnus-newsgroup-marked
1893     gnus-newsgroup-reads gnus-newsgroup-saved
1894     gnus-newsgroup-replied gnus-newsgroup-expirable
1895     gnus-newsgroup-processable gnus-newsgroup-killed
1896     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1897     gnus-newsgroup-headers gnus-newsgroup-threads
1898     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1899     gnus-current-article gnus-current-headers gnus-have-all-headers
1900     gnus-last-article gnus-article-internal-prepare-hook
1901     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1902     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1903     gnus-newsgroup-async 
1904     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1905     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1906     gnus-newsgroup-history gnus-newsgroup-ancient
1907     gnus-newsgroup-sparse
1908     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1909     gnus-newsgroup-adaptive-score-file
1910     (gnus-newsgroup-expunged-tally . 0)
1911     gnus-cache-removeable-articles gnus-newsgroup-cached
1912     gnus-newsgroup-data gnus-newsgroup-data-reverse
1913     gnus-newsgroup-limit gnus-newsgroup-limits)
1914   "Variables that are buffer-local to the summary buffers.")
1915
1916 (defconst gnus-bug-message
1917   "Sending a bug report to the Gnus Towers.
1918 ========================================
1919
1920 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1921 be sent to the Gnus Bug Exterminators.
1922
1923 At the bottom of the buffer you'll see lots of variable settings.
1924 Please do not delete those.  They will tell the Bug People what your
1925 environment is, so that it will be easier to locate the bugs.
1926
1927 If you have found a bug that makes Emacs go \"beep\", set
1928 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1929 and include the backtrace in your bug report.
1930
1931 Please describe the bug in annoying, painstaking detail.
1932
1933 Thank you for your help in stamping out bugs.
1934 ")
1935
1936 ;;; End of variables.
1937
1938 ;; Define some autoload functions Gnus might use.
1939 (eval-and-compile
1940
1941   ;; This little mapcar goes through the list below and marks the
1942   ;; symbols in question as autoloaded functions.
1943   (mapcar
1944    (lambda (package)
1945      (let ((interactive (nth 1 (memq ':interactive package))))
1946        (mapcar
1947         (lambda (function)
1948           (let (keymap)
1949             (when (consp function)
1950               (setq keymap (car (memq 'keymap function)))
1951               (setq function (car function)))
1952             (autoload function (car package) nil interactive keymap)))
1953         (if (eq (nth 1 package) ':interactive)
1954             (cdddr package)
1955           (cdr package)))))
1956    '(("metamail" metamail-buffer)
1957      ("info" Info-goto-node)
1958      ("hexl" hexl-hex-string-to-integer)
1959      ("pp" pp pp-to-string pp-eval-expression)
1960      ("mail-extr" mail-extract-address-components)
1961      ("nnmail" nnmail-split-fancy nnmail-article-group)
1962      ("nnvirtual" nnvirtual-catchup-group)
1963      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1964       timezone-make-sortable-date timezone-make-time-string)
1965      ("sendmail" mail-position-on-field mail-setup)
1966      ("rmailout" rmail-output)
1967      ("rnewspost" news-mail-other-window news-reply-yank-original
1968       news-caesar-buffer-body)
1969      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1970       rmail-show-message)
1971      ("gnus-soup" :interactive t
1972       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1973       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1974      ("nnsoup" nnsoup-pack-replies)
1975      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1976       gnus-Folder-save-name gnus-folder-save-name)
1977      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1978      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1979       gnus-server-make-menu-bar gnus-article-make-menu-bar
1980       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1981       gnus-summary-highlight-line gnus-carpal-setup-buffer
1982       gnus-group-highlight-line
1983       gnus-article-add-button gnus-insert-next-page-button
1984       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1985      ("gnus-vis" :interactive t
1986       gnus-article-push-button gnus-article-press-button
1987       gnus-article-highlight gnus-article-highlight-some
1988       gnus-article-highlight-headers gnus-article-highlight-signature
1989       gnus-article-add-buttons gnus-article-add-buttons-to-head
1990       gnus-article-next-button gnus-article-prev-button)
1991      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1992       gnus-demon-add-disconnection gnus-demon-add-handler
1993       gnus-demon-remove-handler)
1994      ("gnus-demon" :interactive t
1995       gnus-demon-init gnus-demon-cancel)
1996      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1997       gnus-tree-open gnus-tree-close)
1998      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
1999       gnus-nocem-unwanted-article-p)
2000      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2001      ("gnus-srvr" gnus-browse-foreign-server)
2002      ("gnus-cite" :interactive t
2003       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2004       gnus-article-hide-citation gnus-article-fill-cited-article)
2005      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2006       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2007       gnus-execute gnus-expunge)
2008      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2009       gnus-cache-possibly-remove-articles gnus-cache-request-article
2010       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2011       gnus-cache-enter-remove-article gnus-cached-article-p
2012       gnus-cache-open gnus-cache-close)
2013      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2014       gnus-cache-remove-article)
2015      ("gnus-score" :interactive t
2016       gnus-summary-increase-score gnus-summary-lower-score
2017       gnus-score-flush-cache gnus-score-close
2018       gnus-score-raise-same-subject-and-select
2019       gnus-score-raise-same-subject gnus-score-default
2020       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2021       gnus-score-lower-same-subject gnus-score-lower-thread
2022       gnus-possibly-score-headers)
2023      ("gnus-score"
2024       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2025       gnus-current-score-file-nondirectory gnus-score-adaptive
2026       gnus-score-find-trace gnus-score-file-name)
2027      ("gnus-edit" :interactive t gnus-score-customize)
2028      ("gnus-topic" :interactive t gnus-topic-mode)
2029      ("gnus-topic" gnus-topic-remove-group)
2030      ("gnus-salt" :interactive t gnus-pick-mode)
2031      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2032      ("gnus-uu" :interactive t
2033       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2034       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2035       gnus-uu-mark-by-regexp gnus-uu-mark-all
2036       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2037       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2038       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2039       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2040       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2041       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2042       gnus-uu-decode-binhex-view)
2043      ("gnus-msg" (gnus-summary-send-map keymap)
2044       gnus-mail-yank-original gnus-mail-send-and-exit
2045       gnus-sendmail-setup-mail gnus-article-mail
2046       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2047      ("gnus-msg" :interactive t
2048       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2049       gnus-summary-followup gnus-summary-followup-with-original
2050       gnus-summary-followup-and-reply
2051       gnus-summary-followup-and-reply-with-original
2052       gnus-summary-cancel-article gnus-summary-supersede-article
2053       gnus-post-news gnus-inews-news gnus-cancel-news
2054       gnus-summary-reply gnus-summary-reply-with-original
2055       gnus-summary-mail-forward gnus-summary-mail-other-window
2056       gnus-bug)
2057      ("gnus-picon" :interactive t gnus-article-display-picons
2058       gnus-group-display-picons gnus-picons-article-display-x-face)
2059      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2060       gnus-grouplens-mode)
2061      ("gnus-vm" gnus-vm-mail-setup)
2062      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2063       gnus-summary-save-article-vm gnus-yank-article))))
2064
2065 \f
2066
2067 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2068 ;; If you want the cursor to go somewhere else, set these two
2069 ;; functions in some startup hook to whatever you want.
2070 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2071 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2072
2073 ;;; Various macros and substs.
2074
2075 (defun gnus-header-from (header)
2076   (mail-header-from header))
2077
2078 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2079   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2080   `(let ((GnusStartBufferWindow (selected-window)))
2081      (unwind-protect
2082          (progn
2083            (pop-to-buffer ,buffer)
2084            ,@forms)
2085        (select-window GnusStartBufferWindow))))
2086
2087 (defmacro gnus-gethash (string hashtable)
2088   "Get hash value of STRING in HASHTABLE."
2089   `(symbol-value (intern-soft ,string ,hashtable)))
2090
2091 (defmacro gnus-sethash (string value hashtable)
2092   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2093   `(set (intern ,string ,hashtable) ,value))
2094
2095 (defmacro gnus-intern-safe (string hashtable)
2096   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2097   `(let ((symbol (intern ,string ,hashtable)))
2098      (or (boundp symbol)
2099          (set symbol nil))
2100      symbol))
2101
2102 (defmacro gnus-group-unread (group)
2103   "Get the currently computed number of unread articles in GROUP."
2104   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2105
2106 (defmacro gnus-group-entry (group)
2107   "Get the newsrc entry for GROUP."
2108   `(gnus-gethash ,group gnus-newsrc-hashtb))
2109
2110 (defmacro gnus-active (group)
2111   "Get active info on GROUP."
2112   `(gnus-gethash ,group gnus-active-hashtb))
2113
2114 (defmacro gnus-set-active (group active)
2115   "Set GROUP's active info."
2116   `(gnus-sethash ,group ,active gnus-active-hashtb))
2117
2118 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2119 ;;   function `substring' might cut on a middle of multi-octet
2120 ;;   character.
2121 (defun gnus-truncate-string (str width)
2122   (substring str 0 width))
2123
2124 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2125 ;; to limit the length of a string.  This function is necessary since
2126 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2127 (defsubst gnus-limit-string (str width)
2128   (if (> (length str) width)
2129       (substring str 0 width)
2130     str))
2131
2132 (defsubst gnus-simplify-subject-re (subject)
2133   "Remove \"Re:\" from subject lines."
2134   (if (string-match "^[Rr][Ee]: *" subject)
2135       (substring subject (match-end 0))
2136     subject))
2137
2138 (defsubst gnus-goto-char (point)
2139   (and point (goto-char point)))
2140
2141 (defmacro gnus-buffer-exists-p (buffer)
2142   `(and ,buffer
2143         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2144                  ,buffer)))
2145
2146 (defmacro gnus-kill-buffer (buffer)
2147   `(let ((buf ,buffer))
2148      (if (gnus-buffer-exists-p buf)
2149          (kill-buffer buf))))
2150
2151 (defsubst gnus-point-at-bol ()
2152   "Return point at the beginning of the line."
2153   (let ((p (point)))
2154     (beginning-of-line)
2155     (prog1
2156         (point)
2157       (goto-char p))))
2158
2159 (defsubst gnus-point-at-eol ()
2160   "Return point at the end of the line."
2161   (let ((p (point)))
2162     (end-of-line)
2163     (prog1
2164         (point)
2165       (goto-char p))))
2166
2167 (defun gnus-alive-p ()
2168   "Say whether Gnus is running or not."
2169   (and gnus-group-buffer
2170        (get-buffer gnus-group-buffer)))
2171
2172 ;; Delete the current line (and the next N lines.);
2173 (defmacro gnus-delete-line (&optional n)
2174   `(delete-region (progn (beginning-of-line) (point))
2175                   (progn (forward-line ,(or n 1)) (point))))
2176
2177 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2178 (defvar gnus-init-inhibit nil)
2179 (defun gnus-read-init-file (&optional inhibit-next)
2180   (if gnus-init-inhibit
2181       (setq gnus-init-inhibit nil)
2182     (setq gnus-init-inhibit inhibit-next)
2183     (and gnus-init-file
2184          (or (and (file-exists-p gnus-init-file)
2185                   ;; Don't try to load a directory.
2186                   (not (file-directory-p gnus-init-file)))
2187              (file-exists-p (concat gnus-init-file ".el"))
2188              (file-exists-p (concat gnus-init-file ".elc")))
2189          (condition-case var
2190              (load gnus-init-file nil t)
2191            (error
2192             (error "Error in %s: %s" gnus-init-file var))))))
2193
2194 ;; Info access macros.
2195
2196 (defmacro gnus-info-group (info)
2197   `(nth 0 ,info))
2198 (defmacro gnus-info-rank (info)
2199   `(nth 1 ,info))
2200 (defmacro gnus-info-read (info)
2201   `(nth 2 ,info))
2202 (defmacro gnus-info-marks (info)
2203   `(nth 3 ,info))
2204 (defmacro gnus-info-method (info)
2205   `(nth 4 ,info))
2206 (defmacro gnus-info-params (info)
2207   `(nth 5 ,info))
2208
2209 (defmacro gnus-info-level (info)
2210   `(let ((rank (gnus-info-rank ,info)))
2211      (if (consp rank)
2212          (car rank)
2213        rank)))
2214 (defmacro gnus-info-score (info)
2215   `(let ((rank (gnus-info-rank ,info)))
2216      (or (and (consp rank) (cdr rank)) 0)))
2217
2218 (defmacro gnus-info-set-group (info group)
2219   `(setcar ,info ,group))
2220 (defmacro gnus-info-set-rank (info rank)
2221   `(setcar (nthcdr 1 ,info) ,rank))
2222 (defmacro gnus-info-set-read (info read)
2223   `(setcar (nthcdr 2 ,info) ,read))
2224 (defmacro gnus-info-set-marks (info marks)
2225   `(setcar (nthcdr 3 ,info) ,marks))
2226 (defmacro gnus-info-set-method (info method)
2227   `(setcar (nthcdr 4 ,info) ,method))
2228 (defmacro gnus-info-set-params (info params)
2229   `(setcar (nthcdr 5 ,info) ,params))
2230
2231 (defmacro gnus-info-set-level (info level)
2232   `(let ((rank (cdr ,info)))
2233      (if (consp (car rank))
2234          (setcar (car rank) ,level)
2235        (setcar rank ,level))))
2236 (defmacro gnus-info-set-score (info score)
2237   `(let ((rank (cdr ,info)))
2238      (if (consp (car rank))
2239          (setcdr (car rank) ,score)
2240        (setcar rank (cons (car rank) ,score)))))
2241
2242 (defmacro gnus-get-info (group)
2243   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2244
2245 (defun gnus-byte-code (func)
2246   "Return a form that can be `eval'ed based on FUNC."
2247   (let ((fval (symbol-function func)))
2248     (if (byte-code-function-p fval)
2249         (let ((flist (append fval nil)))
2250           (setcar flist 'byte-code)
2251           flist)
2252       (cons 'progn (cddr fval)))))
2253
2254 ;;; Load the compatability functions.
2255
2256 (require 'gnus-cus)
2257 (require 'gnus-ems)
2258
2259 \f
2260
2261 ;; Format specs.  The chunks below are the machine-generated forms
2262 ;; that are to be evaled as the result of the default format strings.
2263 ;; We write them in here to get them byte-compiled.  That way the
2264 ;; default actions will be quite fast, while still retaining the full
2265 ;; flexibility of the user-defined format specs.
2266
2267 ;; First we have lots of dummy defvars to let the compiler know these
2268 ;; are really dynamic variables.
2269
2270 (defvar gnus-tmp-unread)
2271 (defvar gnus-tmp-replied)
2272 (defvar gnus-tmp-score-char)
2273 (defvar gnus-tmp-indentation)
2274 (defvar gnus-tmp-opening-bracket)
2275 (defvar gnus-tmp-lines)
2276 (defvar gnus-tmp-name)
2277 (defvar gnus-tmp-closing-bracket)
2278 (defvar gnus-tmp-subject-or-nil)
2279 (defvar gnus-tmp-subject)
2280 (defvar gnus-tmp-marked)
2281 (defvar gnus-tmp-marked-mark)
2282 (defvar gnus-tmp-subscribed)
2283 (defvar gnus-tmp-process-marked)
2284 (defvar gnus-tmp-number-of-unread)
2285 (defvar gnus-tmp-group-name)
2286 (defvar gnus-tmp-group)
2287 (defvar gnus-tmp-article-number)
2288 (defvar gnus-tmp-unread-and-unselected)
2289 (defvar gnus-tmp-news-method)
2290 (defvar gnus-tmp-news-server)
2291 (defvar gnus-tmp-article-number)
2292 (defvar gnus-mouse-face)
2293 (defvar gnus-mouse-face-prop)
2294
2295 (defun gnus-summary-line-format-spec ()
2296   (insert gnus-tmp-unread gnus-tmp-replied
2297           gnus-tmp-score-char gnus-tmp-indentation)
2298   (put-text-property
2299    (point)
2300    (progn
2301      (insert
2302       gnus-tmp-opening-bracket
2303       (format "%4d: %-20s"
2304               gnus-tmp-lines
2305               (if (> (length gnus-tmp-name) 20)
2306                   (substring gnus-tmp-name 0 20)
2307                 gnus-tmp-name))
2308       gnus-tmp-closing-bracket)
2309      (point))
2310    gnus-mouse-face-prop gnus-mouse-face)
2311   (insert " " gnus-tmp-subject-or-nil "\n"))
2312
2313 (defvar gnus-summary-line-format-spec
2314   (gnus-byte-code 'gnus-summary-line-format-spec))
2315
2316 (defun gnus-summary-dummy-line-format-spec ()
2317   (insert "*  ")
2318   (put-text-property
2319    (point)
2320    (progn
2321      (insert ":                          :")
2322      (point))
2323    gnus-mouse-face-prop gnus-mouse-face)
2324   (insert " " gnus-tmp-subject "\n"))
2325
2326 (defvar gnus-summary-dummy-line-format-spec
2327   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2328
2329 (defun gnus-group-line-format-spec ()
2330   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2331           gnus-tmp-process-marked
2332           gnus-group-indentation
2333           (format "%5s: " gnus-tmp-number-of-unread))
2334   (put-text-property
2335    (point)
2336    (progn
2337      (insert gnus-tmp-group "\n")
2338      (1- (point)))
2339    gnus-mouse-face-prop gnus-mouse-face))
2340 (defvar gnus-group-line-format-spec
2341   (gnus-byte-code 'gnus-group-line-format-spec))
2342
2343 (defvar gnus-format-specs
2344   `((version . ,emacs-version)
2345     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2346     (summary-dummy ,gnus-summary-dummy-line-format
2347                    ,gnus-summary-dummy-line-format-spec)
2348     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2349
2350 (defvar gnus-article-mode-line-format-spec nil)
2351 (defvar gnus-summary-mode-line-format-spec nil)
2352 (defvar gnus-group-mode-line-format-spec nil)
2353
2354 ;;; Phew.  All that gruft is over, fortunately.
2355
2356 \f
2357 ;;;
2358 ;;; Gnus Utility Functions
2359 ;;;
2360
2361 (defun gnus-extract-address-components (from)
2362   (let (name address)
2363     ;; First find the address - the thing with the @ in it.  This may
2364     ;; not be accurate in mail addresses, but does the trick most of
2365     ;; the time in news messages.
2366     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2367         (setq address (substring from (match-beginning 0) (match-end 0))))
2368     ;; Then we check whether the "name <address>" format is used.
2369     (and address
2370          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2371          ;; Linear white space is not required.
2372          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2373          (and (setq name (substring from 0 (match-beginning 0)))
2374               ;; Strip any quotes from the name.
2375               (string-match "\".*\"" name)
2376               (setq name (substring name 1 (1- (match-end 0))))))
2377     ;; If not, then "address (name)" is used.
2378     (or name
2379         (and (string-match "(.+)" from)
2380              (setq name (substring from (1+ (match-beginning 0))
2381                                    (1- (match-end 0)))))
2382         (and (string-match "()" from)
2383              (setq name address))
2384         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2385         ;; XOVER might not support folded From headers.
2386         (and (string-match "(.*" from)
2387              (setq name (substring from (1+ (match-beginning 0))
2388                                    (match-end 0)))))
2389     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2390     (list (or name from) (or address from))))
2391
2392 (defun gnus-fetch-field (field)
2393   "Return the value of the header FIELD of current article."
2394   (save-excursion
2395     (save-restriction
2396       (let ((case-fold-search t))
2397         (nnheader-narrow-to-headers)
2398         (mail-fetch-field field)))))
2399
2400 (defun gnus-goto-colon ()
2401   (beginning-of-line)
2402   (search-forward ":" (gnus-point-at-eol) t))
2403
2404 ;;;###autoload
2405 (defun gnus-update-format (var)
2406   "Update the format specification near point."
2407   (interactive
2408    (list
2409     (save-excursion
2410       (eval-defun nil)
2411       ;; Find the end of the current word.
2412       (re-search-forward "[ \t\n]" nil t)
2413       ;; Search backward.
2414       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2415         (match-string 1)))))
2416   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2417                               (match-string 1 var))))
2418          (entry (assq type gnus-format-specs))
2419          value spec)
2420     (when entry
2421       (setq gnus-format-specs (delq entry gnus-format-specs)))
2422     (set
2423      (intern (format "%s-spec" var))
2424      (gnus-parse-format (setq value (symbol-value (intern var)))
2425                         (symbol-value (intern (format "%s-alist" var)))
2426                         (not (string-match "mode" var))))
2427     (setq spec (symbol-value (intern (format "%s-spec" var))))
2428     (push (list type value spec) gnus-format-specs)
2429
2430     (pop-to-buffer "*Gnus Format*")
2431     (erase-buffer)
2432     (lisp-interaction-mode)
2433     (insert (pp-to-string spec))))
2434
2435 (defun gnus-update-format-specifications (&optional force)
2436   "Update all (necessary) format specifications."
2437   ;; Make the indentation array.
2438   (gnus-make-thread-indent-array)
2439
2440   ;; See whether all the stored info needs to be flushed.
2441   (when (or force
2442             (not (equal emacs-version
2443                         (cdr (assq 'version gnus-format-specs)))))
2444     (setq gnus-format-specs nil))
2445
2446   ;; Go through all the formats and see whether they need updating.
2447   (let ((types '(summary summary-dummy group
2448                          summary-mode group-mode article-mode))
2449         new-format entry type val)
2450     (while (setq type (pop types))
2451       ;; Jump to the proper buffer to find out the value of
2452       ;; the variable, if possible.  (It may be buffer-local.)
2453       (save-excursion
2454         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2455               val)
2456           (when (and (boundp buffer)
2457                      (setq val (symbol-value buffer))
2458                      (get-buffer val)
2459                      (buffer-name (get-buffer val)))
2460             (set-buffer (get-buffer val)))
2461           (setq new-format (symbol-value
2462                             (intern (format "gnus-%s-line-format" type))))))
2463       (setq entry (cdr (assq type gnus-format-specs)))
2464       (if (and entry
2465                (equal (car entry) new-format))
2466           ;; Use the old format.
2467           (set (intern (format "gnus-%s-line-format-spec" type))
2468                (cadr entry))
2469         ;; This is a new format.
2470         (setq val
2471               (if (not (stringp new-format))
2472                   ;; This is a function call or something.
2473                   new-format
2474                 ;; This is a "real" format.
2475                 (gnus-parse-format
2476                  new-format
2477                  (symbol-value
2478                   (intern (format "gnus-%s-line-format-alist"
2479                                   (if (eq type 'article-mode)
2480                                       'summary-mode type))))
2481                  (not (string-match "mode$" (symbol-name type))))))
2482         ;; Enter the new format spec into the list.
2483         (if entry
2484             (progn
2485               (setcar (cdr entry) val)
2486               (setcar entry new-format))
2487           (push (list type new-format val) gnus-format-specs))
2488         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2489
2490   (gnus-update-group-mark-positions)
2491   (gnus-update-summary-mark-positions)
2492
2493   ;; See whether we need to read the description file.
2494   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2495            (not gnus-description-hashtb)
2496            gnus-read-active-file)
2497       (gnus-read-all-descriptions-files)))
2498
2499 (defun gnus-update-summary-mark-positions ()
2500   "Compute where the summary marks are to go."
2501   (save-excursion
2502     (let ((gnus-replied-mark 129)
2503           (gnus-score-below-mark 130)
2504           (gnus-score-over-mark 130)
2505           (thread nil)
2506           (gnus-visual nil)
2507           pos)
2508       (gnus-set-work-buffer)
2509       (gnus-summary-insert-line
2510        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2511       (goto-char (point-min))
2512       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2513                                          (- (point) 2)))))
2514       (goto-char (point-min))
2515       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2516             pos)
2517       (goto-char (point-min))
2518       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2519             pos)
2520       (setq gnus-summary-mark-positions pos))))
2521
2522 (defun gnus-update-group-mark-positions ()
2523   (save-excursion
2524     (let ((gnus-process-mark 128)
2525           (gnus-group-marked '("dummy.group")))
2526       (gnus-set-active "dummy.group" '(0 . 0))
2527       (gnus-set-work-buffer)
2528       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2529       (goto-char (point-min))
2530       (setq gnus-group-mark-positions
2531             (list (cons 'process (and (search-forward "\200" nil t)
2532                                       (- (point) 2))))))))
2533
2534 (defvar gnus-mouse-face-0 'highlight)
2535 (defvar gnus-mouse-face-1 'highlight)
2536 (defvar gnus-mouse-face-2 'highlight)
2537 (defvar gnus-mouse-face-3 'highlight)
2538 (defvar gnus-mouse-face-4 'highlight)
2539
2540 (defun gnus-mouse-face-function (form type)
2541   `(put-text-property
2542     (point) (progn ,@form (point))
2543     gnus-mouse-face-prop
2544     ,(if (equal type 0)
2545          'gnus-mouse-face
2546        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2547
2548 (defvar gnus-face-0 'bold)
2549 (defvar gnus-face-1 'italic)
2550 (defvar gnus-face-2 'bold-italic)
2551 (defvar gnus-face-3 'bold)
2552 (defvar gnus-face-4 'bold)
2553
2554 (defun gnus-face-face-function (form type)
2555   `(put-text-property
2556     (point) (progn ,@form (point))
2557     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2558
2559 (defun gnus-max-width-function (el max-width)
2560   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2561   (if (symbolp el)
2562       `(if (> (length ,el) ,max-width)
2563            (substring ,el 0 ,max-width)
2564          ,el)
2565     `(let ((val (eval ,el)))
2566        (if (numberp val)
2567            (setq val (int-to-string val)))
2568        (if (> (length val) ,max-width)
2569            (substring val 0 ,max-width)
2570          val))))
2571
2572 (defun gnus-parse-format (format spec-alist &optional insert)
2573   ;; This function parses the FORMAT string with the help of the
2574   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2575   ;; string.  If the FORMAT string contains the specifiers %( and %)
2576   ;; the text between them will have the mouse-face text property.
2577   (if (string-match
2578        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2579        format)
2580       (gnus-parse-complex-format format spec-alist)
2581     ;; This is a simple format.
2582     (gnus-parse-simple-format format spec-alist insert)))
2583
2584 (defun gnus-parse-complex-format (format spec-alist)
2585   (save-excursion
2586     (gnus-set-work-buffer)
2587     (insert format)
2588     (goto-char (point-min))
2589     (while (re-search-forward "\"" nil t)
2590       (replace-match "\\\"" nil t))
2591     (goto-char (point-min))
2592     (insert "(\"")
2593     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2594       (let ((number (if (match-beginning 1)
2595                         (match-string 1) "0"))
2596             (delim (aref (match-string 2) 0)))
2597         (if (or (= delim ?\() (= delim ?\{))
2598             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2599                                    " " number " \""))
2600           (replace-match "\")\""))))
2601     (goto-char (point-max))
2602     (insert "\")")
2603     (goto-char (point-min))
2604     (let ((form (read (current-buffer))))
2605       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2606
2607 (defun gnus-complex-form-to-spec (form spec-alist)
2608   (delq nil
2609         (mapcar
2610          (lambda (sform)
2611            (if (stringp sform)
2612                (gnus-parse-simple-format sform spec-alist t)
2613              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2614                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2615                       (nth 1 sform))))
2616          form)))
2617
2618 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2619   ;; This function parses the FORMAT string with the help of the
2620   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2621   ;; string.
2622   (let ((max-width 0)
2623         spec flist fstring newspec elem beg result dontinsert)
2624     (save-excursion
2625       (gnus-set-work-buffer)
2626       (insert format)
2627       (goto-char (point-min))
2628       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2629                                 nil t)
2630         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2631               (setq newspec "%"
2632                     beg (1+ (match-beginning 0)))
2633           ;; First check if there are any specs that look anything like
2634           ;; "%12,12A", ie. with a "max width specification".  These have
2635           ;; to be treated specially.
2636           (if (setq beg (match-beginning 1))
2637               (setq max-width
2638                     (string-to-int
2639                      (buffer-substring
2640                       (1+ (match-beginning 1)) (match-end 1))))
2641             (setq max-width 0)
2642             (setq beg (match-beginning 2)))
2643           ;; Find the specification from `spec-alist'.
2644           (unless (setq elem (cdr (assq spec spec-alist)))
2645             (setq elem '("*" ?s)))
2646           ;; Treat user defined format specifiers specially.
2647           (when (eq (car elem) 'gnus-tmp-user-defined)
2648             (setq elem
2649                   (list
2650                    (list (intern (concat "gnus-user-format-function-"
2651                                          (match-string 3)))
2652                          'gnus-tmp-header) ?s))
2653             (delete-region (match-beginning 3) (match-end 3)))
2654           (if (not (zerop max-width))
2655               (let ((el (car elem)))
2656                 (cond ((= (cadr elem) ?c)
2657                        (setq el (list 'char-to-string el)))
2658                       ((= (cadr elem) ?d)
2659                        (setq el (list 'int-to-string el))))
2660                 (setq flist (cons (gnus-max-width-function el max-width)
2661                                   flist))
2662                 (setq newspec ?s))
2663             (progn
2664               (setq flist (cons (car elem) flist))
2665               (setq newspec (cadr elem)))))
2666         ;; Remove the old specification (and possibly a ",12" string).
2667         (delete-region beg (match-end 2))
2668         ;; Insert the new specification.
2669         (goto-char beg)
2670         (insert newspec))
2671       (setq fstring (buffer-substring 1 (point-max))))
2672     ;; Do some postprocessing to increase efficiency.
2673     (setq
2674      result
2675      (cond
2676       ;; Emptyness.
2677       ((string= fstring "")
2678        nil)
2679       ;; Not a format string.
2680       ((not (string-match "%" fstring))
2681        (list fstring))
2682       ;; A format string with just a single string spec.
2683       ((string= fstring "%s")
2684        (list (car flist)))
2685       ;; A single character.
2686       ((string= fstring "%c")
2687        (list (car flist)))
2688       ;; A single number.
2689       ((string= fstring "%d")
2690        (setq dontinsert)
2691        (if insert
2692            (list `(princ ,(car flist)))
2693          (list `(int-to-string ,(car flist)))))
2694       ;; Just lots of chars and strings.
2695       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2696        (nreverse flist))
2697       ;; A single string spec at the beginning of the spec.
2698       ((string-match "\\`%[sc][^%]+\\'" fstring)
2699        (list (car flist) (substring fstring 2)))
2700       ;; A single string spec in the middle of the spec.
2701       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2702        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2703       ;; A single string spec in the end of the spec.
2704       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2705        (list (match-string 1 fstring) (car flist)))
2706       ;; A more complex spec.
2707       (t
2708        (list (cons 'format (cons fstring (nreverse flist)))))))
2709
2710     (if insert
2711         (when result
2712           (if dontinsert
2713               result
2714             (cons 'insert result)))
2715       (cond ((stringp result)
2716              result)
2717             ((consp result)
2718              (cons 'concat result))
2719             (t "")))))
2720
2721 (defun gnus-eval-format (format &optional alist props)
2722   "Eval the format variable FORMAT, using ALIST.
2723 If PROPS, insert the result."
2724   (let ((form (gnus-parse-format format alist props)))
2725     (if props
2726         (add-text-properties (point) (progn (eval form) (point)) props)
2727       (eval form))))
2728
2729 (defun gnus-remove-text-with-property (prop)
2730   "Delete all text in the current buffer with text property PROP."
2731   (save-excursion
2732     (goto-char (point-min))
2733     (while (not (eobp))
2734       (while (get-text-property (point) prop)
2735         (delete-char 1))
2736       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2737
2738 (defun gnus-set-work-buffer ()
2739   (if (get-buffer gnus-work-buffer)
2740       (progn
2741         (set-buffer gnus-work-buffer)
2742         (erase-buffer))
2743     (set-buffer (get-buffer-create gnus-work-buffer))
2744     (kill-all-local-variables)
2745     (buffer-disable-undo (current-buffer))
2746     (gnus-add-current-to-buffer-list)))
2747
2748 ;; Article file names when saving.
2749
2750 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2751   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2752 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2753 Otherwise, it is like ~/News/news/group/num."
2754   (let ((default
2755           (expand-file-name
2756            (concat (if (gnus-use-long-file-name 'not-save)
2757                        (gnus-capitalize-newsgroup newsgroup)
2758                      (gnus-newsgroup-directory-form newsgroup))
2759                    "/" (int-to-string (mail-header-number headers)))
2760            (or gnus-article-save-directory "~/News"))))
2761     (if (and last-file
2762              (string-equal (file-name-directory default)
2763                            (file-name-directory last-file))
2764              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2765         default
2766       (or last-file default))))
2767
2768 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2769   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2770 If variable `gnus-use-long-file-name' is non-nil, it is
2771 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2772   (let ((default
2773           (expand-file-name
2774            (concat (if (gnus-use-long-file-name 'not-save)
2775                        newsgroup
2776                      (gnus-newsgroup-directory-form newsgroup))
2777                    "/" (int-to-string (mail-header-number headers)))
2778            (or gnus-article-save-directory "~/News"))))
2779     (if (and last-file
2780              (string-equal (file-name-directory default)
2781                            (file-name-directory last-file))
2782              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2783         default
2784       (or last-file default))))
2785
2786 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2787   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2788 If variable `gnus-use-long-file-name' is non-nil, it is
2789 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2790   (or last-file
2791       (expand-file-name
2792        (if (gnus-use-long-file-name 'not-save)
2793            (gnus-capitalize-newsgroup newsgroup)
2794          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2795        (or gnus-article-save-directory "~/News"))))
2796
2797 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2798   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2799 If variable `gnus-use-long-file-name' is non-nil, it is
2800 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2801   (or last-file
2802       (expand-file-name
2803        (if (gnus-use-long-file-name 'not-save)
2804            newsgroup
2805          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2806        (or gnus-article-save-directory "~/News"))))
2807
2808 ;; For subscribing new newsgroup
2809
2810 (defun gnus-subscribe-hierarchical-interactive (groups)
2811   (let ((groups (sort groups 'string<))
2812         prefixes prefix start ans group starts)
2813     (while groups
2814       (setq prefixes (list "^"))
2815       (while (and groups prefixes)
2816         (while (not (string-match (car prefixes) (car groups)))
2817           (setq prefixes (cdr prefixes)))
2818         (setq prefix (car prefixes))
2819         (setq start (1- (length prefix)))
2820         (if (and (string-match "[^\\.]\\." (car groups) start)
2821                  (cdr groups)
2822                  (setq prefix
2823                        (concat "^" (substring (car groups) 0 (match-end 0))))
2824                  (string-match prefix (cadr groups)))
2825             (progn
2826               (setq prefixes (cons prefix prefixes))
2827               (message "Descend hierarchy %s? ([y]nsq): "
2828                        (substring prefix 1 (1- (length prefix))))
2829               (setq ans (read-char))
2830               (cond ((= ans ?n)
2831                      (while (and groups
2832                                  (string-match prefix
2833                                                (setq group (car groups))))
2834                        (setq gnus-killed-list
2835                              (cons group gnus-killed-list))
2836                        (gnus-sethash group group gnus-killed-hashtb)
2837                        (setq groups (cdr groups)))
2838                      (setq starts (cdr starts)))
2839                     ((= ans ?s)
2840                      (while (and groups
2841                                  (string-match prefix
2842                                                (setq group (car groups))))
2843                        (gnus-sethash group group gnus-killed-hashtb)
2844                        (gnus-subscribe-alphabetically (car groups))
2845                        (setq groups (cdr groups)))
2846                      (setq starts (cdr starts)))
2847                     ((= ans ?q)
2848                      (while groups
2849                        (setq group (car groups))
2850                        (setq gnus-killed-list (cons group gnus-killed-list))
2851                        (gnus-sethash group group gnus-killed-hashtb)
2852                        (setq groups (cdr groups))))
2853                     (t nil)))
2854           (message "Subscribe %s? ([n]yq)" (car groups))
2855           (setq ans (read-char))
2856           (setq group (car groups))
2857           (cond ((= ans ?y)
2858                  (gnus-subscribe-alphabetically (car groups))
2859                  (gnus-sethash group group gnus-killed-hashtb))
2860                 ((= ans ?q)
2861                  (while groups
2862                    (setq group (car groups))
2863                    (setq gnus-killed-list (cons group gnus-killed-list))
2864                    (gnus-sethash group group gnus-killed-hashtb)
2865                    (setq groups (cdr groups))))
2866                 (t
2867                  (setq gnus-killed-list (cons group gnus-killed-list))
2868                  (gnus-sethash group group gnus-killed-hashtb)))
2869           (setq groups (cdr groups)))))))
2870
2871 (defun gnus-subscribe-randomly (newsgroup)
2872   "Subscribe new NEWSGROUP by making it the first newsgroup."
2873   (gnus-subscribe-newsgroup newsgroup))
2874
2875 (defun gnus-subscribe-alphabetically (newgroup)
2876   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2877   (let ((groups (cdr gnus-newsrc-alist))
2878         before)
2879     (while (and (not before) groups)
2880       (if (string< newgroup (caar groups))
2881           (setq before (caar groups))
2882         (setq groups (cdr groups))))
2883     (gnus-subscribe-newsgroup newgroup before)))
2884
2885 (defun gnus-subscribe-hierarchically (newgroup)
2886   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2887   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2888   (save-excursion
2889     (set-buffer (find-file-noselect gnus-current-startup-file))
2890     (let ((groupkey newgroup)
2891           before)
2892       (while (and (not before) groupkey)
2893         (goto-char (point-min))
2894         (let ((groupkey-re
2895                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2896           (while (and (re-search-forward groupkey-re nil t)
2897                       (progn
2898                         (setq before (match-string 1))
2899                         (string< before newgroup)))))
2900         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2901         (setq groupkey
2902               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2903                   (substring groupkey (match-beginning 1) (match-end 1)))))
2904       (gnus-subscribe-newsgroup newgroup before))))
2905
2906 (defun gnus-subscribe-interactively (group)
2907   "Subscribe the new GROUP interactively.
2908 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2909 it is killed."
2910   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2911       (gnus-subscribe-hierarchically group)
2912     (push group gnus-killed-list)))
2913
2914 (defun gnus-subscribe-zombies (group)
2915   "Make the new GROUP into a zombie group."
2916   (push group gnus-zombie-list))
2917
2918 (defun gnus-subscribe-killed (group)
2919   "Make the new GROUP a killed group."
2920   (push group gnus-killed-list))
2921
2922 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2923   "Subscribe new NEWSGROUP.
2924 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2925 the first newsgroup."
2926   ;; We subscribe the group by changing its level to `subscribed'.
2927   (gnus-group-change-level
2928    newsgroup gnus-level-default-subscribed
2929    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2930   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2931
2932 ;; For directories
2933
2934 (defun gnus-newsgroup-directory-form (newsgroup)
2935   "Make hierarchical directory name from NEWSGROUP name."
2936   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2937         (len (length newsgroup))
2938         idx)
2939     ;; If this is a foreign group, we don't want to translate the
2940     ;; entire name.
2941     (if (setq idx (string-match ":" newsgroup))
2942         (aset newsgroup idx ?/)
2943       (setq idx 0))
2944     ;; Replace all occurrences of `.' with `/'.
2945     (while (< idx len)
2946       (if (= (aref newsgroup idx) ?.)
2947           (aset newsgroup idx ?/))
2948       (setq idx (1+ idx)))
2949     newsgroup))
2950
2951 (defun gnus-newsgroup-savable-name (group)
2952   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2953   ;; with dots.
2954   (nnheader-replace-chars-in-string group ?/ ?.))
2955
2956 (defun gnus-make-directory (dir)
2957   "Make DIRECTORY recursively."
2958   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2959   ;; of the many mysteries of the universe.
2960   (let* ((dir (expand-file-name dir default-directory))
2961          dirs err)
2962     (if (string-match "/$" dir)
2963         (setq dir (substring dir 0 (match-beginning 0))))
2964     ;; First go down the path until we find a directory that exists.
2965     (while (not (file-exists-p dir))
2966       (setq dirs (cons dir dirs))
2967       (string-match "/[^/]+$" dir)
2968       (setq dir (substring dir 0 (match-beginning 0))))
2969     ;; Then create all the subdirs.
2970     (while (and dirs (not err))
2971       (condition-case ()
2972           (make-directory (car dirs))
2973         (error (setq err t)))
2974       (setq dirs (cdr dirs)))
2975     ;; We return whether we were successful or not.
2976     (not dirs)))
2977
2978 (defun gnus-capitalize-newsgroup (newsgroup)
2979   "Capitalize NEWSGROUP name."
2980   (and (not (zerop (length newsgroup)))
2981        (concat (char-to-string (upcase (aref newsgroup 0)))
2982                (substring newsgroup 1))))
2983
2984 ;; Various... things.
2985
2986 (defun gnus-simplify-subject (subject &optional re-only)
2987   "Remove `Re:' and words in parentheses.
2988 If RE-ONLY is non-nil, strip leading `Re:'s only."
2989   (let ((case-fold-search t))           ;Ignore case.
2990     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
2991     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
2992       (setq subject (substring subject (match-end 0))))
2993     ;; Remove uninteresting prefixes.
2994     (if (and (not re-only)
2995              gnus-simplify-ignored-prefixes
2996              (string-match gnus-simplify-ignored-prefixes subject))
2997         (setq subject (substring subject (match-end 0))))
2998     ;; Remove words in parentheses from end.
2999     (unless re-only
3000       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3001         (setq subject (substring subject 0 (match-beginning 0)))))
3002     ;; Return subject string.
3003     subject))
3004
3005 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3006 ;; all whitespace.
3007 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3008 (defun gnus-simplify-buffer-fuzzy ()
3009   (goto-char (point-min))
3010   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
3011   (goto-char (match-beginning 0))
3012   (while (or
3013           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
3014           (looking-at "^[[].*:[ \t].*[]]$"))
3015     (goto-char (point-min))
3016     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
3017                               nil t)
3018       (replace-match "" t t))
3019     (goto-char (point-min))
3020     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
3021       (goto-char (match-end 0))
3022       (delete-char -1)
3023       (delete-region
3024        (progn (goto-char (match-beginning 0)))
3025        (re-search-forward ":"))))
3026   (goto-char (point-min))
3027   (while (re-search-forward "[ \t\n]*[[{(][^()]*[]})][ \t]*$" nil t)
3028     (replace-match "" t t))
3029   (goto-char (point-min))
3030   (while (re-search-forward "[ \t]+" nil t)
3031     (replace-match " " t t))
3032   (goto-char (point-min))
3033   (while (re-search-forward "[ \t]+$" nil t)
3034     (replace-match "" t t))
3035   (goto-char (point-min))
3036   (while (re-search-forward "^[ \t]+" nil t)
3037     (replace-match "" t t))
3038   (goto-char (point-min))
3039   (if gnus-simplify-subject-fuzzy-regexp
3040       (if (listp gnus-simplify-subject-fuzzy-regexp)
3041           (let ((list gnus-simplify-subject-fuzzy-regexp))
3042             (while list
3043               (goto-char (point-min))
3044               (while (re-search-forward (car list) nil t)
3045                 (replace-match "" t t))
3046               (setq list (cdr list))))
3047         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3048           (replace-match "" t t)))))
3049
3050 (defun gnus-simplify-subject-fuzzy (subject)
3051   "Siplify a subject string fuzzily."
3052   (save-excursion
3053     (gnus-set-work-buffer)
3054     (let ((case-fold-search t))
3055       (insert subject)
3056       (inline (gnus-simplify-buffer-fuzzy))
3057       (buffer-string))))
3058
3059 ;; Add the current buffer to the list of buffers to be killed on exit.
3060 (defun gnus-add-current-to-buffer-list ()
3061   (or (memq (current-buffer) gnus-buffer-list)
3062       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3063
3064 (defun gnus-string> (s1 s2)
3065   (not (or (string< s1 s2)
3066            (string= s1 s2))))
3067
3068 ;;; General various misc type functions.
3069
3070 (defun gnus-clear-system ()
3071   "Clear all variables and buffers."
3072   ;; Clear Gnus variables.
3073   (let ((variables gnus-variable-list))
3074     (while variables
3075       (set (car variables) nil)
3076       (setq variables (cdr variables))))
3077   ;; Clear other internal variables.
3078   (setq gnus-list-of-killed-groups nil
3079         gnus-have-read-active-file nil
3080         gnus-newsrc-alist nil
3081         gnus-newsrc-hashtb nil
3082         gnus-killed-list nil
3083         gnus-zombie-list nil
3084         gnus-killed-hashtb nil
3085         gnus-active-hashtb nil
3086         gnus-moderated-list nil
3087         gnus-description-hashtb nil
3088         gnus-newsgroup-headers nil
3089         gnus-newsgroup-name nil
3090         gnus-server-alist nil
3091         gnus-opened-servers nil
3092         gnus-current-select-method nil)
3093   ;; Reset any score variables.
3094   (when gnus-use-scoring 
3095     (gnus-score-close))
3096   ;; Kill the startup file.
3097   (and gnus-current-startup-file
3098        (get-file-buffer gnus-current-startup-file)
3099        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3100   ;; Save any cache buffers.
3101   (when gnus-use-cache 
3102     (gnus-cache-save-buffers))
3103   ;; Clear the dribble buffer.
3104   (gnus-dribble-clear)
3105   ;; Close down NoCeM.
3106   (when gnus-use-nocem 
3107     (gnus-nocem-close))
3108   ;; Shut down the demons.
3109   (when gnus-use-demon
3110     (gnus-demon-cancel))
3111   ;; Kill global KILL file buffer.
3112   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3113     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3114   (gnus-kill-buffer nntp-server-buffer)
3115   ;; Backlog.
3116   (when gnus-keep-backlog
3117     (gnus-backlog-shutdown))
3118   ;; Kill Gnus buffers.
3119   (while gnus-buffer-list
3120     (gnus-kill-buffer (pop gnus-buffer-list)))
3121   ;; Remove Gnus frames.
3122   (while gnus-created-frames
3123     (when (frame-live-p (car gnus-created-frames))
3124       ;; We slap a condition-case around this `delete-frame' to ensure 
3125       ;; agains errors if we try do delete the single frame that's left.
3126       (condition-case ()
3127           (delete-frame (car gnus-created-frames))
3128         (error nil)))
3129     (pop gnus-created-frames)))
3130
3131 (defun gnus-windows-old-to-new (setting)
3132   ;; First we take care of the really, really old Gnus 3 actions.
3133   (when (symbolp setting)
3134     (setq setting
3135           ;; Take care of ooold GNUS 3.x values.
3136           (cond ((eq setting 'SelectArticle) 'article)
3137                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3138                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3139                 (t setting))))
3140   (if (or (listp setting)
3141           (not (and gnus-window-configuration
3142                     (memq setting '(group summary article)))))
3143       setting
3144     (let* ((setting (if (eq setting 'group)
3145                         (if (assq 'newsgroup gnus-window-configuration)
3146                             'newsgroup
3147                           'newsgroups) setting))
3148            (elem (cadr (assq setting gnus-window-configuration)))
3149            (total (apply '+ elem))
3150            (types '(group summary article))
3151            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3152            (i 0)
3153            perc
3154            out)
3155       (while (< i 3)
3156         (or (not (numberp (nth i elem)))
3157             (zerop (nth i elem))
3158             (progn
3159               (setq perc  (/ (float (nth 0 elem)) total))
3160               (setq out (cons (if (eq pbuf (nth i types))
3161                                   (vector (nth i types) perc 'point)
3162                                 (vector (nth i types) perc))
3163                               out))))
3164         (setq i (1+ i)))
3165       (list (nreverse out)))))
3166
3167 ;;;###autoload
3168 (defun gnus-add-configuration (conf)
3169   "Add the window configuration CONF to `gnus-buffer-configuration'."
3170   (setq gnus-buffer-configuration
3171         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3172                          gnus-buffer-configuration))))
3173
3174 (defvar gnus-frame-list nil)
3175
3176 (defun gnus-configure-frame (split &optional window)
3177   "Split WINDOW according to SPLIT."
3178   (unless window
3179     (setq window (get-buffer-window (current-buffer))))
3180   (select-window window)
3181   ;; This might be an old-stylee buffer config.
3182   (when (vectorp split)
3183     (setq split (append split nil)))
3184   (when (or (consp (car split))
3185             (vectorp (car split)))
3186     (push 1.0 split)
3187     (push 'vertical split))
3188   ;; The SPLIT might be something that is to be evaled to
3189   ;; return a new SPLIT.
3190   (while (and (not (assq (car split) gnus-window-to-buffer))
3191               (gnus-functionp (car split)))
3192     (setq split (eval split)))
3193   (let* ((type (car split))
3194          (subs (cddr split))
3195          (len (if (eq type 'horizontal) (window-width) (window-height)))
3196          (total 0)
3197          (window-min-width (or gnus-window-min-width window-min-width))
3198          (window-min-height (or gnus-window-min-height window-min-height))
3199          s result new-win rest comp-subs size sub)
3200     (cond
3201      ;; Nothing to do here.
3202      ((null split))
3203      ;; Don't switch buffers.
3204      ((null type)
3205       (and (memq 'point split) window))
3206      ;; This is a buffer to be selected.
3207      ((not (memq type '(frame horizontal vertical)))
3208       (let ((buffer (cond ((stringp type) type)
3209                           (t (cdr (assq type gnus-window-to-buffer)))))
3210             buf)
3211         (unless buffer
3212           (error "Illegal buffer type: %s" type))
3213         (unless (setq buf (get-buffer (if (symbolp buffer)
3214                                           (symbol-value buffer) buffer)))
3215           (setq buf (get-buffer-create (if (symbolp buffer)
3216                                            (symbol-value buffer) buffer))))
3217         (switch-to-buffer buf)
3218         ;; We return the window if it has the `point' spec.
3219         (and (memq 'point split) window)))
3220      ;; This is a frame split.
3221      ((eq type 'frame)
3222       (unless gnus-frame-list
3223         (setq gnus-frame-list (list (window-frame
3224                                      (get-buffer-window (current-buffer))))))
3225       (let ((i 0)
3226             params frame fresult)
3227         (while (< i (length subs))
3228           ;; Frame parameter is gotten from the sub-split.
3229           (setq params (cadr (elt subs i)))
3230           ;; It should be a list.
3231           (unless (listp params)
3232             (setq params nil))
3233           ;; Create a new frame?
3234           (unless (setq frame (elt gnus-frame-list i))
3235             (nconc gnus-frame-list (list (setq frame (make-frame params)))))
3236           (push frame gnus-created-frames)
3237           ;; Is the old frame still alive?
3238           (unless (frame-live-p frame)
3239             (setcar (nthcdr i gnus-frame-list)
3240                     (setq frame (make-frame params))))
3241           ;; Select the frame in question and do more splits there.
3242           (select-frame frame)
3243           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3244           (incf i))
3245         ;; Select the frame that has the selected buffer.
3246         (when fresult
3247           (select-frame (window-frame fresult)))))
3248      ;; This is a normal split.
3249      (t
3250       (when (> (length subs) 0)
3251         ;; First we have to compute the sizes of all new windows.
3252         (while subs
3253           (setq sub (append (pop subs) nil))
3254           (while (and (not (assq (car sub) gnus-window-to-buffer))
3255                       (gnus-functionp (car sub)))
3256             (setq sub (eval sub)))
3257           (when sub
3258             (push sub comp-subs)
3259             (setq size (cadar comp-subs))
3260             (cond ((equal size 1.0)
3261                    (setq rest (car comp-subs))
3262                    (setq s 0))
3263                   ((floatp size)
3264                    (setq s (floor (* size len))))
3265                   ((integerp size)
3266                    (setq s size))
3267                   (t
3268                    (error "Illegal size: %s" size)))
3269             ;; Try to make sure that we are inside the safe limits.
3270             (cond ((zerop s))
3271                   ((eq type 'horizontal)
3272                    (setq s (max s window-min-width)))
3273                   ((eq type 'vertical)
3274                    (setq s (max s window-min-height))))
3275             (setcar (cdar comp-subs) s)
3276             (incf total s)))
3277         ;; Take care of the "1.0" spec.
3278         (if rest
3279             (setcar (cdr rest) (- len total))
3280           (error "No 1.0 specs in %s" split))
3281         ;; The we do the actual splitting in a nice recursive
3282         ;; fashion.
3283         (setq comp-subs (nreverse comp-subs))
3284         (while comp-subs
3285           (if (null (cdr comp-subs))
3286               (setq new-win window)
3287             (setq new-win
3288                   (split-window window (cadar comp-subs)
3289                                 (eq type 'horizontal))))
3290           (setq result (or (gnus-configure-frame
3291                             (car comp-subs) window) result))
3292           (select-window new-win)
3293           (setq window new-win)
3294           (setq comp-subs (cdr comp-subs))))
3295       ;; Return the proper window, if any.
3296       (when result
3297         (select-window result))))))
3298
3299 (defvar gnus-frame-split-p nil)
3300
3301 (defun gnus-configure-windows (setting &optional force)
3302   (setq setting (gnus-windows-old-to-new setting))
3303   (let ((split (if (symbolp setting)
3304                    (cadr (assq setting gnus-buffer-configuration))
3305                  setting))
3306         (in-buf (current-buffer))
3307         rule val w height hor ohor heights sub jump-buffer
3308         rel total to-buf all-visible)
3309
3310     (setq gnus-frame-split-p nil)
3311
3312     (unless split
3313       (error "No such setting: %s" setting))
3314
3315     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3316              (not force))
3317         ;; All the windows mentioned are already visible, so we just
3318         ;; put point in the assigned buffer, and do not touch the
3319         ;; winconf.
3320         (select-window all-visible)
3321
3322       ;; Either remove all windows or just remove all Gnus windows.
3323       (let ((frame (selected-frame)))
3324         (unwind-protect
3325             (if gnus-use-full-window
3326                 ;; We want to remove all other windows.
3327                 (if (not gnus-frame-split-p)
3328                     ;; This is not a `frame' split, so we ignore the
3329                     ;; other frames.  
3330                     (delete-other-windows)
3331                   ;; This is a `frame' split, so we delete all windows
3332                   ;; on all frames.
3333                   (mapcar 
3334                    (lambda (frame)
3335                      (unless (eq (cdr (assq 'minibuffer
3336                                             (frame-parameters frame)))
3337                                  'only)
3338                        (select-frame frame)
3339                        (delete-other-windows)))
3340                    (frame-list)))
3341               ;; Just remove some windows.
3342               (gnus-remove-some-windows)
3343               (switch-to-buffer nntp-server-buffer))
3344           (select-frame frame)))
3345
3346       (switch-to-buffer nntp-server-buffer)
3347       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3348
3349 (defun gnus-all-windows-visible-p (split)
3350   (when (vectorp split)
3351     (setq split (append split nil)))
3352   (when (or (consp (car split))
3353             (vectorp (car split)))
3354     (push 1.0 split)
3355     (push 'vertical split))
3356   ;; The SPLIT might be something that is to be evaled to
3357   ;; return a new SPLIT.
3358   (while (and (not (assq (car split) gnus-window-to-buffer))
3359               (gnus-functionp (car split)))
3360     (setq split (eval split)))
3361   (let* ((type (elt split 0)))
3362     (cond
3363      ((null split)
3364       t)
3365      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3366       (let ((buffer (cond ((stringp type) type)
3367                           (t (cdr (assq type gnus-window-to-buffer)))))
3368             win buf)
3369         (unless buffer
3370           (error "Illegal buffer type: %s" type))
3371         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3372                                       buffer)))
3373           (setq win (get-buffer-window buf t)))
3374         (when win
3375           (if (memq 'point split)
3376               win
3377             t))))
3378      (t
3379       (when (eq type 'frame)
3380         (setq gnus-frame-split-p t))
3381       (let ((n (mapcar 'gnus-all-windows-visible-p
3382                        (cddr split)))
3383             (win t))
3384         (while n
3385           (cond ((windowp (car n))
3386                  (setq win (car n)))
3387                 ((null (car n))
3388                  (setq win nil)))
3389           (setq n (cdr n)))
3390         win)))))
3391
3392 (defun gnus-window-top-edge (&optional window)
3393   (nth 1 (window-edges window)))
3394
3395 (defun gnus-remove-some-windows ()
3396   (let ((buffers gnus-window-to-buffer)
3397         buf bufs lowest-buf lowest)
3398     (save-excursion
3399       ;; Remove windows on all known Gnus buffers.
3400       (while buffers
3401         (setq buf (cdar buffers))
3402         (if (symbolp buf)
3403             (setq buf (and (boundp buf) (symbol-value buf))))
3404         (and buf
3405              (get-buffer-window buf)
3406              (progn
3407                (setq bufs (cons buf bufs))
3408                (pop-to-buffer buf)
3409                (if (or (not lowest)
3410                        (< (gnus-window-top-edge) lowest))
3411                    (progn
3412                      (setq lowest (gnus-window-top-edge))
3413                      (setq lowest-buf buf)))))
3414         (setq buffers (cdr buffers)))
3415       ;; Remove windows on *all* summary buffers.
3416       (let (wins)
3417         (walk-windows
3418          (lambda (win)
3419            (let ((buf (window-buffer win)))
3420              (if (string-match  "^\\*Summary" (buffer-name buf))
3421                  (progn
3422                    (setq bufs (cons buf bufs))
3423                    (pop-to-buffer buf)
3424                    (if (or (not lowest)
3425                            (< (gnus-window-top-edge) lowest))
3426                        (progn
3427                          (setq lowest-buf buf)
3428                          (setq lowest (gnus-window-top-edge))))))))))
3429       (and lowest-buf
3430            (progn
3431              (pop-to-buffer lowest-buf)
3432              (switch-to-buffer nntp-server-buffer)))
3433       (while bufs
3434         (and (not (eq (car bufs) lowest-buf))
3435              (delete-windows-on (car bufs)))
3436         (setq bufs (cdr bufs))))))
3437
3438 (defun gnus-version ()
3439   "Version numbers of this version of Gnus."
3440   (interactive)
3441   (let ((methods gnus-valid-select-methods)
3442         (mess gnus-version)
3443         meth)
3444     ;; Go through all the legal select methods and add their version
3445     ;; numbers to the total version string.  Only the backends that are
3446     ;; currently in use will have their message numbers taken into
3447     ;; consideration.
3448     (while methods
3449       (setq meth (intern (concat (caar methods) "-version")))
3450       (and (boundp meth)
3451            (stringp (symbol-value meth))
3452            (setq mess (concat mess "; " (symbol-value meth))))
3453       (setq methods (cdr methods)))
3454     (gnus-message 2 mess)))
3455
3456 (defun gnus-info-find-node ()
3457   "Find Info documentation of Gnus."
3458   (interactive)
3459   ;; Enlarge info window if needed.
3460   (let ((mode major-mode)
3461         gnus-info-buffer)
3462     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3463     (setq gnus-info-buffer (current-buffer))
3464     (gnus-configure-windows 'info)))
3465
3466 (defun gnus-days-between (date1 date2)
3467   ;; Return the number of days between date1 and date2.
3468   (- (gnus-day-number date1) (gnus-day-number date2)))
3469
3470 (defun gnus-day-number (date)
3471   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3472                      (timezone-parse-date date))))
3473     (timezone-absolute-from-gregorian
3474      (nth 1 dat) (nth 2 dat) (car dat))))
3475
3476 (defun gnus-encode-date (date)
3477   "Convert DATE to internal time."
3478   (let* ((parse (timezone-parse-date date))
3479          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3480          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3481     (encode-time (caddr time) (cadr time) (car time)
3482                  (caddr date) (cadr date) (car date) (nth 4 date))))
3483
3484 (defun gnus-time-minus (t1 t2)
3485   "Subtract two internal times."
3486   (let ((borrow (< (cadr t1) (cadr t2))))
3487     (list (- (car t1) (car t2) (if borrow 1 0))
3488           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3489
3490 (defun gnus-file-newer-than (file date)
3491   (let ((fdate (nth 5 (file-attributes file))))
3492     (or (> (car fdate) (car date))
3493         (and (= (car fdate) (car date))
3494              (> (nth 1 fdate) (nth 1 date))))))
3495
3496 (defmacro gnus-local-set-keys (&rest plist)
3497   "Set the keys in PLIST in the current keymap."
3498   `(gnus-define-keys-1 (current-local-map) ',plist))
3499
3500 (defmacro gnus-define-keys (keymap &rest plist)
3501   "Define all keys in PLIST in KEYMAP."
3502   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3503
3504 (put 'gnus-define-keys 'lisp-indent-function 1)
3505 (put 'gnus-define-keys 'lisp-indent-hook 1)
3506 (put 'gnus-define-keymap 'lisp-indent-function 1)
3507 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3508
3509 (defmacro gnus-define-keymap (keymap &rest plist)
3510   "Define all keys in PLIST in KEYMAP."
3511   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3512
3513 (defun gnus-define-keys-1 (keymap plist)
3514   (when (null keymap)
3515     (error "Can't set keys in a null keymap"))
3516   (cond ((symbolp keymap)
3517          (setq keymap (symbol-value keymap)))
3518         ((keymapp keymap))
3519         ((listp keymap)
3520          (set (car keymap) nil)
3521          (define-prefix-command (car keymap))
3522          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3523          (setq keymap (symbol-value (car keymap)))))
3524   (let (key)
3525     (while plist
3526       (when (symbolp (setq key (pop plist)))
3527         (setq key (symbol-value key)))
3528       (define-key keymap key (pop plist)))))
3529
3530 (defun gnus-group-read-only-p (&optional group)
3531   "Check whether GROUP supports editing or not.
3532 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3533 that that variable is buffer-local to the summary buffers."
3534   (let ((group (or group gnus-newsgroup-name)))
3535     (not (gnus-check-backend-function 'request-replace-article group))))
3536
3537 (defun gnus-group-total-expirable-p (group)
3538   "Check whether GROUP is total-expirable or not."
3539   (let ((params (gnus-info-params (gnus-get-info group))))
3540     (or (memq 'total-expire params)
3541         (cdr (assq 'total-expire params)) ; (total-expire . t)
3542         (and gnus-total-expirable-newsgroups ; Check var.
3543              (string-match gnus-total-expirable-newsgroups group)))))
3544
3545 (defun gnus-group-auto-expirable-p (group)
3546   "Check whether GROUP is total-expirable or not."
3547   (let ((params (gnus-info-params (gnus-get-info group))))
3548     (or (memq 'auto-expire params)
3549         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3550         (and gnus-auto-expirable-newsgroups ; Check var.
3551              (string-match gnus-auto-expirable-newsgroups group)))))
3552
3553 (defun gnus-virtual-group-p (group)
3554   "Say whether GROUP is virtual or not."
3555   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3556                         gnus-valid-select-methods)))
3557
3558 (defsubst gnus-simplify-subject-fully (subject)
3559   "Simplify a subject string according to the user's wishes."
3560   (cond
3561    ((null gnus-summary-gather-subject-limit)
3562     (gnus-simplify-subject-re subject))
3563    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3564     (gnus-simplify-subject-fuzzy subject))
3565    ((numberp gnus-summary-gather-subject-limit)
3566     (gnus-limit-string (gnus-simplify-subject-re subject)
3567                        gnus-summary-gather-subject-limit))
3568    (t
3569     subject)))
3570
3571 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3572   "Check whether two subjects are equal.  If optional argument
3573 simple-first is t, first argument is already simplified."
3574   (cond
3575    ((null simple-first)
3576     (equal (gnus-simplify-subject-fully s1)
3577            (gnus-simplify-subject-fully s2)))
3578    (t
3579     (equal s1
3580            (gnus-simplify-subject-fully s2)))))
3581
3582 ;; Returns a list of writable groups.
3583 (defun gnus-writable-groups ()
3584   (let ((alist gnus-newsrc-alist)
3585         groups group)
3586     (while (setq group (car (pop alist)))
3587       (unless (gnus-group-read-only-p group)
3588         (push group groups)))
3589     (nreverse groups)))
3590
3591 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3592 ;; the echo area.
3593 (defun gnus-y-or-n-p (prompt)
3594   (prog1
3595       (y-or-n-p prompt)
3596     (message "")))
3597
3598 (defun gnus-yes-or-no-p (prompt)
3599   (prog1
3600       (yes-or-no-p prompt)
3601     (message "")))
3602
3603 ;; Check whether to use long file names.
3604 (defun gnus-use-long-file-name (symbol)
3605   ;; The variable has to be set...
3606   (and gnus-use-long-file-name
3607        ;; If it isn't a list, then we return t.
3608        (or (not (listp gnus-use-long-file-name))
3609            ;; If it is a list, and the list contains `symbol', we
3610            ;; return nil.
3611            (not (memq symbol gnus-use-long-file-name)))))
3612
3613 ;; I suspect there's a better way, but I haven't taken the time to do
3614 ;; it yet. -erik selberg@cs.washington.edu
3615 (defun gnus-dd-mmm (messy-date)
3616   "Return a string like DD-MMM from a big messy string"
3617   (let ((datevec (timezone-parse-date messy-date)))
3618     (format "%2s-%s"
3619             (or (aref datevec 2) "??")
3620             (capitalize
3621              (or (car
3622                   (nth (1- (string-to-number (aref datevec 1)))
3623                        timezone-months-assoc))
3624                  "???")))))
3625
3626 ;; Make a hash table (default and minimum size is 255).
3627 ;; Optional argument HASHSIZE specifies the table size.
3628 (defun gnus-make-hashtable (&optional hashsize)
3629   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3630
3631 ;; Make a number that is suitable for hashing; bigger than MIN and one
3632 ;; less than 2^x.
3633 (defun gnus-create-hash-size (min)
3634   (let ((i 1))
3635     (while (< i min)
3636       (setq i (* 2 i)))
3637     (1- i)))
3638
3639 ;; Show message if message has a lower level than `gnus-verbose'.
3640 ;; Guideline for numbers:
3641 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3642 ;; for things that take a long time, 7 - not very important messages
3643 ;; on stuff, 9 - messages inside loops.
3644 (defun gnus-message (level &rest args)
3645   (if (<= level gnus-verbose)
3646       (apply 'message args)
3647     ;; We have to do this format thingy here even if the result isn't
3648     ;; shown - the return value has to be the same as the return value
3649     ;; from `message'.
3650     (apply 'format args)))
3651
3652 (defun gnus-functionp (form)
3653   "Return non-nil if FORM is funcallable."
3654   (or (and (symbolp form) (fboundp form))
3655       (and (listp form) (eq (car form) 'lambda))))
3656
3657 ;; Generate a unique new group name.
3658 (defun gnus-generate-new-group-name (leaf)
3659   (let ((name leaf)
3660         (num 0))
3661     (while (gnus-gethash name gnus-newsrc-hashtb)
3662       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3663     name))
3664
3665 ;; Find out whether the gnus-visual TYPE is wanted.
3666 (defun gnus-visual-p (&optional type class)
3667   (and gnus-visual                      ; Has to be non-nil, at least.
3668        (if (not type)                   ; We don't care about type.
3669            gnus-visual
3670          (if (listp gnus-visual)        ; It's a list, so we check it.
3671              (or (memq type gnus-visual)
3672                  (memq class gnus-visual))
3673            t))))
3674
3675 (defun gnus-parent-id (references)
3676   "Return the last Message-ID in REFERENCES."
3677   (when (and references
3678              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3679     (substring references (match-beginning 1) (match-end 1))))
3680
3681 (defun gnus-split-references (references)
3682   "Return a list of Message-IDs in REFERENCES."
3683   (let ((beg 0)
3684         ids)
3685     (while (string-match "<[^>]+>" references beg)
3686       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3687             ids))
3688     (nreverse ids)))
3689
3690 (defun gnus-ephemeral-group-p (group)
3691   "Say whether GROUP is ephemeral or not."
3692   (gnus-group-get-parameter group 'quit-config))
3693
3694 (defun gnus-group-quit-config (group)
3695   "Return the quit-config of GROUP."
3696   (gnus-group-get-parameter group 'quit-config))
3697
3698 (defun gnus-simplify-mode-line ()
3699   "Make mode lines a bit simpler."
3700   (setq mode-line-modified "-- ")
3701   (when (listp mode-line-format)
3702     (make-local-variable 'mode-line-format)
3703     (setq mode-line-format (copy-sequence mode-line-format))
3704     (when (equal (nth 3 mode-line-format) "   ")
3705       (setcar (nthcdr 3 mode-line-format) " "))))
3706
3707 ;;; List and range functions
3708
3709 (defun gnus-last-element (list)
3710   "Return last element of LIST."
3711   (while (cdr list)
3712     (setq list (cdr list)))
3713   (car list))
3714
3715 (defun gnus-copy-sequence (list)
3716   "Do a complete, total copy of a list."
3717   (if (and (consp list) (not (consp (cdr list))))
3718       (cons (car list) (cdr list))
3719     (mapcar (lambda (elem) (if (consp elem)
3720                                (if (consp (cdr elem))
3721                                    (gnus-copy-sequence elem)
3722                                  (cons (car elem) (cdr elem)))
3723                              elem))
3724             list)))
3725
3726 (defun gnus-set-difference (list1 list2)
3727   "Return a list of elements of LIST1 that do not appear in LIST2."
3728   (let ((list1 (copy-sequence list1)))
3729     (while list2
3730       (setq list1 (delq (car list2) list1))
3731       (setq list2 (cdr list2)))
3732     list1))
3733
3734 (defun gnus-sorted-complement (list1 list2)
3735   "Return a list of elements of LIST1 that do not appear in LIST2.
3736 Both lists have to be sorted over <."
3737   (let (out)
3738     (if (or (null list1) (null list2))
3739         (or list1 list2)
3740       (while (and list1 list2)
3741         (cond ((= (car list1) (car list2))
3742                (setq list1 (cdr list1)
3743                      list2 (cdr list2)))
3744               ((< (car list1) (car list2))
3745                (setq out (cons (car list1) out))
3746                (setq list1 (cdr list1)))
3747               (t
3748                (setq out (cons (car list2) out))
3749                (setq list2 (cdr list2)))))
3750       (nconc (nreverse out) (or list1 list2)))))
3751
3752 (defun gnus-intersection (list1 list2)
3753   (let ((result nil))
3754     (while list2
3755       (if (memq (car list2) list1)
3756           (setq result (cons (car list2) result)))
3757       (setq list2 (cdr list2)))
3758     result))
3759
3760 (defun gnus-sorted-intersection (list1 list2)
3761   ;; LIST1 and LIST2 have to be sorted over <.
3762   (let (out)
3763     (while (and list1 list2)
3764       (cond ((= (car list1) (car list2))
3765              (setq out (cons (car list1) out)
3766                    list1 (cdr list1)
3767                    list2 (cdr list2)))
3768             ((< (car list1) (car list2))
3769              (setq list1 (cdr list1)))
3770             (t
3771              (setq list2 (cdr list2)))))
3772     (nreverse out)))
3773
3774 (defun gnus-set-sorted-intersection (list1 list2)
3775   ;; LIST1 and LIST2 have to be sorted over <.
3776   ;; This function modifies LIST1.
3777   (let* ((top (cons nil list1))
3778          (prev top))
3779     (while (and list1 list2)
3780       (cond ((= (car list1) (car list2))
3781              (setq prev list1
3782                    list1 (cdr list1)
3783                    list2 (cdr list2)))
3784             ((< (car list1) (car list2))
3785              (setcdr prev (cdr list1))
3786              (setq list1 (cdr list1)))
3787             (t
3788              (setq list2 (cdr list2)))))
3789     (setcdr prev nil)
3790     (cdr top)))
3791
3792 (defun gnus-compress-sequence (numbers &optional always-list)
3793   "Convert list of numbers to a list of ranges or a single range.
3794 If ALWAYS-LIST is non-nil, this function will always release a list of
3795 ranges."
3796   (let* ((first (car numbers))
3797          (last (car numbers))
3798          result)
3799     (if (null numbers)
3800         nil
3801       (if (not (listp (cdr numbers)))
3802           numbers
3803         (while numbers
3804           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3805                 ((= (1+ last) (car numbers)) ;Still in sequence
3806                  (setq last (car numbers)))
3807                 (t                      ;End of one sequence
3808                  (setq result
3809                        (cons (if (= first last) first
3810                                (cons first last)) result))
3811                  (setq first (car numbers))
3812                  (setq last  (car numbers))))
3813           (setq numbers (cdr numbers)))
3814         (if (and (not always-list) (null result))
3815             (if (= first last) (list first) (cons first last))
3816           (nreverse (cons (if (= first last) first (cons first last))
3817                           result)))))))
3818
3819 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3820 (defun gnus-uncompress-range (ranges)
3821   "Expand a list of ranges into a list of numbers.
3822 RANGES is either a single range on the form `(num . num)' or a list of
3823 these ranges."
3824   (let (first last result)
3825     (cond
3826      ((null ranges)
3827       nil)
3828      ((not (listp (cdr ranges)))
3829       (setq first (car ranges))
3830       (setq last (cdr ranges))
3831       (while (<= first last)
3832         (setq result (cons first result))
3833         (setq first (1+ first)))
3834       (nreverse result))
3835      (t
3836       (while ranges
3837         (if (atom (car ranges))
3838             (if (numberp (car ranges))
3839                 (setq result (cons (car ranges) result)))
3840           (setq first (caar ranges))
3841           (setq last  (cdar ranges))
3842           (while (<= first last)
3843             (setq result (cons first result))
3844             (setq first (1+ first))))
3845         (setq ranges (cdr ranges)))
3846       (nreverse result)))))
3847
3848 (defun gnus-add-to-range (ranges list)
3849   "Return a list of ranges that has all articles from both RANGES and LIST.
3850 Note: LIST has to be sorted over `<'."
3851   (if (not ranges)
3852       (gnus-compress-sequence list t)
3853     (setq list (copy-sequence list))
3854     (or (listp (cdr ranges))
3855         (setq ranges (list ranges)))
3856     (let ((out ranges)
3857           ilist lowest highest temp)
3858       (while (and ranges list)
3859         (setq ilist list)
3860         (setq lowest (or (and (atom (car ranges)) (car ranges))
3861                          (caar ranges)))
3862         (while (and list (cdr list) (< (cadr list) lowest))
3863           (setq list (cdr list)))
3864         (if (< (car ilist) lowest)
3865             (progn
3866               (setq temp list)
3867               (setq list (cdr list))
3868               (setcdr temp nil)
3869               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3870         (setq highest (or (and (atom (car ranges)) (car ranges))
3871                           (cdar ranges)))
3872         (while (and list (<= (car list) highest))
3873           (setq list (cdr list)))
3874         (setq ranges (cdr ranges)))
3875       (if list
3876           (setq out (nconc (gnus-compress-sequence list t) out)))
3877       (setq out (sort out (lambda (r1 r2)
3878                             (< (or (and (atom r1) r1) (car r1))
3879                                (or (and (atom r2) r2) (car r2))))))
3880       (setq ranges out)
3881       (while ranges
3882         (if (atom (car ranges))
3883             (if (cdr ranges)
3884                 (if (atom (cadr ranges))
3885                     (if (= (1+ (car ranges)) (cadr ranges))
3886                         (progn
3887                           (setcar ranges (cons (car ranges)
3888                                                (cadr ranges)))
3889                           (setcdr ranges (cddr ranges))))
3890                   (if (= (1+ (car ranges)) (caadr ranges))
3891                       (progn
3892                         (setcar (cadr ranges) (car ranges))
3893                         (setcar ranges (cadr ranges))
3894                         (setcdr ranges (cddr ranges))))))
3895           (if (cdr ranges)
3896               (if (atom (cadr ranges))
3897                   (if (= (1+ (cdar ranges)) (cadr ranges))
3898                       (progn
3899                         (setcdr (car ranges) (cadr ranges))
3900                         (setcdr ranges (cddr ranges))))
3901                 (if (= (1+ (cdar ranges)) (caadr ranges))
3902                     (progn
3903                       (setcdr (car ranges) (cdadr ranges))
3904                       (setcdr ranges (cddr ranges)))))))
3905         (setq ranges (cdr ranges)))
3906       out)))
3907
3908 (defun gnus-remove-from-range (ranges list)
3909   "Return a list of ranges that has all articles from LIST removed from RANGES.
3910 Note: LIST has to be sorted over `<'."
3911   ;; !!! This function shouldn't look like this, but I've got a headache.
3912   (gnus-compress-sequence
3913    (gnus-sorted-complement
3914     (gnus-uncompress-range ranges) list)))
3915
3916 (defun gnus-member-of-range (number ranges)
3917   (if (not (listp (cdr ranges)))
3918       (and (>= number (car ranges))
3919            (<= number (cdr ranges)))
3920     (let ((not-stop t))
3921       (while (and ranges
3922                   (if (numberp (car ranges))
3923                       (>= number (car ranges))
3924                     (>= number (caar ranges)))
3925                   not-stop)
3926         (if (if (numberp (car ranges))
3927                 (= number (car ranges))
3928               (and (>= number (caar ranges))
3929                    (<= number (cdar ranges))))
3930             (setq not-stop nil))
3931         (setq ranges (cdr ranges)))
3932       (not not-stop))))
3933
3934 (defun gnus-range-length (range)
3935   "Return the length RANGE would have if uncompressed."
3936   (length (gnus-uncompress-range range)))
3937
3938 (defun gnus-sublist-p (list sublist)
3939   "Test whether all elements in SUBLIST are members of LIST."
3940   (let ((sublistp t))
3941     (while sublist
3942       (unless (memq (pop sublist) list)
3943         (setq sublistp nil
3944               sublist nil)))
3945     sublistp))
3946
3947 \f
3948 ;;;
3949 ;;; Gnus group mode
3950 ;;;
3951
3952 (defvar gnus-group-mode-map nil)
3953 (put 'gnus-group-mode 'mode-class 'special)
3954
3955 (unless gnus-group-mode-map
3956   (setq gnus-group-mode-map (make-keymap))
3957   (suppress-keymap gnus-group-mode-map)
3958
3959   (gnus-define-keys gnus-group-mode-map
3960     " " gnus-group-read-group
3961     "=" gnus-group-select-group
3962     "\M- " gnus-group-unhidden-select-group
3963     "\r" gnus-group-select-group
3964     "\M-\r" gnus-group-quick-select-group
3965     "j" gnus-group-jump-to-group
3966     "n" gnus-group-next-unread-group
3967     "p" gnus-group-prev-unread-group
3968     "\177" gnus-group-prev-unread-group
3969     [delete] gnus-group-prev-unread-group
3970     "N" gnus-group-next-group
3971     "P" gnus-group-prev-group
3972     "\M-n" gnus-group-next-unread-group-same-level
3973     "\M-p" gnus-group-prev-unread-group-same-level
3974     "," gnus-group-best-unread-group
3975     "." gnus-group-first-unread-group
3976     "u" gnus-group-unsubscribe-current-group
3977     "U" gnus-group-unsubscribe-group
3978     "c" gnus-group-catchup-current
3979     "C" gnus-group-catchup-current-all
3980     "l" gnus-group-list-groups
3981     "L" gnus-group-list-all-groups
3982     "m" gnus-group-mail
3983     "g" gnus-group-get-new-news
3984     "\M-g" gnus-group-get-new-news-this-group
3985     "R" gnus-group-restart
3986     "r" gnus-group-read-init-file
3987     "B" gnus-group-browse-foreign-server
3988     "b" gnus-group-check-bogus-groups
3989     "F" gnus-find-new-newsgroups
3990     "\C-c\C-d" gnus-group-describe-group
3991     "\M-d" gnus-group-describe-all-groups
3992     "\C-c\C-a" gnus-group-apropos
3993     "\C-c\M-\C-a" gnus-group-description-apropos
3994     "a" gnus-group-post-news
3995     "\ek" gnus-group-edit-local-kill
3996     "\eK" gnus-group-edit-global-kill
3997     "\C-k" gnus-group-kill-group
3998     "\C-y" gnus-group-yank-group
3999     "\C-w" gnus-group-kill-region
4000     "\C-x\C-t" gnus-group-transpose-groups
4001     "\C-c\C-l" gnus-group-list-killed
4002     "\C-c\C-x" gnus-group-expire-articles
4003     "\C-c\M-\C-x" gnus-group-expire-all-groups
4004     "V" gnus-version
4005     "s" gnus-group-save-newsrc
4006     "z" gnus-group-suspend
4007     "Z" gnus-group-clear-dribble
4008     "q" gnus-group-exit
4009     "Q" gnus-group-quit
4010     "?" gnus-group-describe-briefly
4011     "\C-c\C-i" gnus-info-find-node
4012     "\M-e" gnus-group-edit-group-method
4013     "^" gnus-group-enter-server-mode
4014     gnus-mouse-2 gnus-mouse-pick-group
4015     "<" beginning-of-buffer
4016     ">" end-of-buffer
4017     "\C-c\C-b" gnus-bug
4018     "\C-c\C-s" gnus-group-sort-groups
4019     "t" gnus-topic-mode
4020     "\C-c\M-g" gnus-activate-all-groups
4021     "\M-&" gnus-group-universal-argument
4022     "#" gnus-group-mark-group
4023     "\M-#" gnus-group-unmark-group)
4024
4025   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4026     "m" gnus-group-mark-group
4027     "u" gnus-group-unmark-group
4028     "w" gnus-group-mark-region
4029     "m" gnus-group-mark-buffer
4030     "r" gnus-group-mark-regexp
4031     "U" gnus-group-unmark-all-groups)
4032
4033   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4034     "d" gnus-group-make-directory-group
4035     "h" gnus-group-make-help-group
4036     "a" gnus-group-make-archive-group
4037     "k" gnus-group-make-kiboze-group
4038     "m" gnus-group-make-group
4039     "E" gnus-group-edit-group
4040     "e" gnus-group-edit-group-method
4041     "p" gnus-group-edit-group-parameters
4042     "v" gnus-group-add-to-virtual
4043     "V" gnus-group-make-empty-virtual
4044     "D" gnus-group-enter-directory
4045     "f" gnus-group-make-doc-group
4046     "r" gnus-group-rename-group
4047     "\177" gnus-group-delete-group
4048     [delete] gnus-group-delete-group)
4049
4050    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4051      "b" gnus-group-brew-soup
4052      "w" gnus-soup-save-areas
4053      "s" gnus-soup-send-replies
4054      "p" gnus-soup-pack-packet
4055      "r" nnsoup-pack-replies)
4056
4057    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4058      "s" gnus-group-sort-groups
4059      "a" gnus-group-sort-groups-by-alphabet
4060      "u" gnus-group-sort-groups-by-unread
4061      "l" gnus-group-sort-groups-by-level
4062      "v" gnus-group-sort-groups-by-score
4063      "r" gnus-group-sort-groups-by-rank
4064      "m" gnus-group-sort-groups-by-method)
4065
4066    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4067      "k" gnus-group-list-killed
4068      "z" gnus-group-list-zombies
4069      "s" gnus-group-list-groups
4070      "u" gnus-group-list-all-groups
4071      "A" gnus-group-list-active
4072      "a" gnus-group-apropos
4073      "d" gnus-group-description-apropos
4074      "m" gnus-group-list-matching
4075      "M" gnus-group-list-all-matching
4076      "l" gnus-group-list-level)
4077
4078    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4079      "f" gnus-score-flush-cache)
4080
4081    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4082      "f" gnus-group-fetch-faq)
4083
4084    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4085      "l" gnus-group-set-current-level
4086      "t" gnus-group-unsubscribe-current-group
4087      "s" gnus-group-unsubscribe-group
4088      "k" gnus-group-kill-group
4089      "y" gnus-group-yank-group
4090      "w" gnus-group-kill-region
4091      "\C-k" gnus-group-kill-level
4092      "z" gnus-group-kill-all-zombies))
4093
4094 (defun gnus-group-mode ()
4095   "Major mode for reading news.
4096
4097 All normal editing commands are switched off.
4098 \\<gnus-group-mode-map>
4099 The group buffer lists (some of) the groups available.  For instance,
4100 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4101 lists all zombie groups.
4102
4103 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4104 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4105
4106 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4107
4108 The following commands are available:
4109
4110 \\{gnus-group-mode-map}"
4111   (interactive)
4112   (when (and menu-bar-mode
4113              (gnus-visual-p 'group-menu 'menu))
4114     (gnus-group-make-menu-bar))
4115   (kill-all-local-variables)
4116   (gnus-simplify-mode-line)
4117   (setq major-mode 'gnus-group-mode)
4118   (setq mode-name "Group")
4119   (gnus-group-set-mode-line)
4120   (setq mode-line-process nil)
4121   (use-local-map gnus-group-mode-map)
4122   (buffer-disable-undo (current-buffer))
4123   (setq truncate-lines t)
4124   (setq buffer-read-only t)
4125   (run-hooks 'gnus-group-mode-hook))
4126
4127 (defun gnus-mouse-pick-group (e)
4128   "Enter the group under the mouse pointer."
4129   (interactive "e")
4130   (mouse-set-point e)
4131   (gnus-group-read-group nil))
4132
4133 ;; Look at LEVEL and find out what the level is really supposed to be.
4134 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4135 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4136 (defun gnus-group-default-level (&optional level number-or-nil)
4137   (cond
4138    (gnus-group-use-permanent-levels
4139     (setq gnus-group-default-list-level
4140           (or level gnus-group-default-list-level))
4141     (or gnus-group-default-list-level gnus-level-subscribed))
4142    (number-or-nil
4143     level)
4144    (t
4145     (or level gnus-group-default-list-level gnus-level-subscribed))))
4146
4147 ;;;###autoload
4148 (defun gnus-slave-no-server (&optional arg)
4149   "Read network news as a slave, without connecting to local server"
4150   (interactive "P")
4151   (gnus-no-server arg t))
4152
4153 ;;;###autoload
4154 (defun gnus-no-server (&optional arg slave)
4155   "Read network news.
4156 If ARG is a positive number, Gnus will use that as the
4157 startup level.  If ARG is nil, Gnus will be started at level 2.
4158 If ARG is non-nil and not a positive number, Gnus will
4159 prompt the user for the name of an NNTP server to use.
4160 As opposed to `gnus', this command will not connect to the local server."
4161   (interactive "P")
4162   (make-local-variable 'gnus-group-use-permanent-levels)
4163   (setq gnus-group-use-permanent-levels t)
4164   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4165
4166 ;;;###autoload
4167 (defun gnus-slave (&optional arg)
4168   "Read news as a slave."
4169   (interactive "P")
4170   (gnus arg nil 'slave))
4171
4172 ;;;###autoload
4173 (defun gnus-other-frame (&optional arg)
4174   "Pop up a frame to read news."
4175   (interactive "P")
4176   (if (get-buffer gnus-group-buffer)
4177       (let ((pop-up-frames t))
4178         (gnus arg))
4179     (select-frame (make-frame))
4180     (gnus arg)))
4181
4182 ;;;###autoload
4183 (defun gnus (&optional arg dont-connect slave)
4184   "Read network news.
4185 If ARG is non-nil and a positive number, Gnus will use that as the
4186 startup level.  If ARG is non-nil and not a positive number, Gnus will
4187 prompt the user for the name of an NNTP server to use."
4188   (interactive "P")
4189
4190   (if (get-buffer gnus-group-buffer)
4191       (progn
4192         (switch-to-buffer gnus-group-buffer)
4193         (gnus-group-get-new-news))
4194
4195     (gnus-clear-system)
4196     (nnheader-init-server-buffer)
4197     (gnus-read-init-file)
4198     (setq gnus-slave slave)
4199
4200     (gnus-group-setup-buffer)
4201     (let ((buffer-read-only nil))
4202       (erase-buffer)
4203       (if (not gnus-inhibit-startup-message)
4204           (progn
4205             (gnus-group-startup-message)
4206             (sit-for 0))))
4207
4208     (let ((level (and (numberp arg) (> arg 0) arg))
4209           did-connect)
4210       (unwind-protect
4211           (progn
4212             (or dont-connect
4213                 (setq did-connect
4214                       (gnus-start-news-server (and arg (not level))))))
4215         (if (and (not dont-connect)
4216                  (not did-connect))
4217             (gnus-group-quit)
4218           (run-hooks 'gnus-startup-hook)
4219           ;; NNTP server is successfully open.
4220
4221           ;; Find the current startup file name.
4222           (setq gnus-current-startup-file
4223                 (gnus-make-newsrc-file gnus-startup-file))
4224
4225           ;; Read the dribble file.
4226           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4227
4228           ;; Allow using GroupLens predictions.
4229           (when gnus-use-grouplens
4230             (bbb-login)
4231             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4232
4233           (gnus-summary-make-display-table)
4234           ;; Do the actual startup.
4235           (gnus-setup-news nil level)
4236           ;; Generate the group buffer.
4237           (gnus-group-list-groups level)
4238           (gnus-group-first-unread-group)
4239           (gnus-configure-windows 'group)
4240           (gnus-group-set-mode-line))))))
4241
4242 (defun gnus-unload ()
4243   "Unload all Gnus features."
4244   (interactive)
4245   (or (boundp 'load-history)
4246       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4247   (let ((history load-history)
4248         feature)
4249     (while history
4250       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4251            (setq feature (cdr (assq 'provide (car history))))
4252            (unload-feature feature 'force))
4253       (setq history (cdr history)))))
4254
4255 (defun gnus-compile ()
4256   "Byte-compile the user-defined format specs."
4257   (interactive)
4258   (let ((entries gnus-format-specs)
4259         entry gnus-tmp-func)
4260     (save-excursion
4261       (gnus-message 7 "Compiling format specs...")
4262
4263       (while entries
4264         (setq entry (pop entries))
4265         (if (eq (car entry) 'version)
4266             (setq gnus-format-specs (delq entry gnus-format-specs))
4267           (when (and (listp (caddr entry))
4268                      (not (eq 'byte-code (caaddr entry))))
4269             (fset 'gnus-tmp-func
4270                   `(lambda () ,(caddr entry)))
4271             (byte-compile 'gnus-tmp-func)
4272             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4273
4274       (push (cons 'version emacs-version) gnus-format-specs)
4275
4276       (gnus-message 7 "Compiling user specs...done"))))
4277
4278 (defun gnus-indent-rigidly (start end arg)
4279   "Indent rigidly using only spaces and no tabs."
4280   (save-excursion
4281     (save-restriction
4282       (narrow-to-region start end)
4283       (indent-rigidly start end arg)
4284       (goto-char (point-min))
4285       (while (search-forward "\t" nil t)
4286         (replace-match "        " t t)))))
4287
4288 (defun gnus-group-startup-message (&optional x y)
4289   "Insert startup message in current buffer."
4290   ;; Insert the message.
4291   (erase-buffer)
4292   (insert
4293    (format "              %s
4294           _    ___ _             _
4295           _ ___ __ ___  __    _ ___
4296           __   _     ___    __  ___
4297               _           ___     _
4298              _  _ __             _
4299              ___   __            _
4300                    __           _
4301                     _      _   _
4302                    _      _    _
4303                       _  _    _
4304                   __  ___
4305                  _   _ _     _
4306                 _   _
4307               _    _
4308              _    _
4309             _
4310           __
4311
4312 "
4313            ""))
4314   ;; And then hack it.
4315   (gnus-indent-rigidly (point-min) (point-max)
4316                        (/ (max (- (window-width) (or x 46)) 0) 2))
4317   (goto-char (point-min))
4318   (forward-line 1)
4319   (let* ((pheight (count-lines (point-min) (point-max)))
4320          (wheight (window-height))
4321          (rest (- wheight pheight)))
4322     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4323   ;; Fontify some.
4324   (goto-char (point-min))
4325   (and (search-forward "Praxis" nil t)
4326        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4327   (goto-char (point-min))
4328   (let* ((mode-string (gnus-group-set-mode-line)))
4329     (setq mode-line-buffer-identification
4330           (list (concat gnus-version (substring (car mode-string) 4))))
4331     (set-buffer-modified-p t)))
4332
4333 (defun gnus-group-setup-buffer ()
4334   (or (get-buffer gnus-group-buffer)
4335       (progn
4336         (switch-to-buffer gnus-group-buffer)
4337         (gnus-add-current-to-buffer-list)
4338         (gnus-group-mode)
4339         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4340
4341 (defun gnus-group-list-groups (&optional level unread lowest)
4342   "List newsgroups with level LEVEL or lower that have unread articles.
4343 Default is all subscribed groups.
4344 If argument UNREAD is non-nil, groups with no unread articles are also
4345 listed."
4346   (interactive (list (if current-prefix-arg
4347                          (prefix-numeric-value current-prefix-arg)
4348                        (or
4349                         (gnus-group-default-level nil t)
4350                         gnus-group-default-list-level
4351                         gnus-level-subscribed))))
4352   (or level
4353       (setq level (car gnus-group-list-mode)
4354             unread (cdr gnus-group-list-mode)))
4355   (setq level (gnus-group-default-level level))
4356   (gnus-group-setup-buffer)             ;May call from out of group buffer
4357   (gnus-update-format-specifications)
4358   (let ((case-fold-search nil)
4359         (props (text-properties-at (gnus-point-at-bol)))
4360         (group (gnus-group-group-name)))
4361     (funcall gnus-group-prepare-function level unread lowest)
4362     (if (zerop (buffer-size))
4363         (gnus-message 5 gnus-no-groups-message)
4364       (goto-char (point-max))
4365       (when (or (not gnus-group-goto-next-group-function)
4366                 (not (funcall gnus-group-goto-next-group-function 
4367                               group props)))
4368         (if (not group)
4369             ;; Go to the first group with unread articles.
4370             (gnus-group-search-forward t)
4371           ;; Find the right group to put point on.  If the current group
4372           ;; has disappeared in the new listing, try to find the next
4373           ;; one.        If no next one can be found, just leave point at the
4374           ;; first newsgroup in the buffer.
4375           (if (not (gnus-goto-char
4376                     (text-property-any
4377                      (point-min) (point-max)
4378                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4379               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4380                 (while (and newsrc
4381                             (not (gnus-goto-char
4382                                   (text-property-any
4383                                    (point-min) (point-max) 'gnus-group
4384                                    (gnus-intern-safe
4385                                     (caar newsrc) gnus-active-hashtb)))))
4386                   (setq newsrc (cdr newsrc)))
4387                 (or newsrc (progn (goto-char (point-max))
4388                                   (forward-line -1)))))))
4389       ;; Adjust cursor point.
4390       (gnus-group-position-point))))
4391
4392 (defun gnus-group-list-level (level &optional all)
4393   "List groups on LEVEL.
4394 If ALL (the prefix), also list groups that have no unread articles."
4395   (interactive "nList groups on level: \nP")
4396   (gnus-group-list-groups level all level))
4397
4398 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4399   "List all newsgroups with unread articles of level LEVEL or lower.
4400 If ALL is non-nil, list groups that have no unread articles.
4401 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4402 If REGEXP, only list groups matching REGEXP."
4403   (set-buffer gnus-group-buffer)
4404   (let ((buffer-read-only nil)
4405         (newsrc (cdr gnus-newsrc-alist))
4406         (lowest (or lowest 1))
4407         info clevel unread group params)
4408     (erase-buffer)
4409     (if (< lowest gnus-level-zombie)
4410         ;; List living groups.
4411         (while newsrc
4412           (setq info (car newsrc)
4413                 group (gnus-info-group info)
4414                 params (gnus-info-params info)
4415                 newsrc (cdr newsrc)
4416                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4417           (and unread                   ; This group might be bogus
4418                (or (not regexp)
4419                    (string-match regexp group))
4420                (<= (setq clevel (gnus-info-level info)) level)
4421                (>= clevel lowest)
4422                (or all                  ; We list all groups?
4423                    (and gnus-group-list-inactive-groups
4424                         (eq unread t))  ; We list unactivated groups
4425                    (> unread 0)         ; We list groups with unread articles
4426                    (and gnus-list-groups-with-ticked-articles
4427                         (cdr (assq 'tick (gnus-info-marks info))))
4428                                         ; And groups with tickeds
4429                    ;; Check for permanent visibility.
4430                    (and gnus-permanently-visible-groups
4431                         (string-match gnus-permanently-visible-groups
4432                                       group))
4433                    (memq 'visible params)
4434                    (cdr (assq 'visible params)))
4435                (gnus-group-insert-group-line
4436                 group (gnus-info-level info)
4437                 (gnus-info-marks info) unread (gnus-info-method info)))))
4438
4439     ;; List dead groups.
4440     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4441          (gnus-group-prepare-flat-list-dead
4442           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4443           gnus-level-zombie ?Z
4444           regexp))
4445     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4446          (gnus-group-prepare-flat-list-dead
4447           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4448           gnus-level-killed ?K regexp))
4449
4450     (gnus-group-set-mode-line)
4451     (setq gnus-group-list-mode (cons level all))
4452     (run-hooks 'gnus-group-prepare-hook)))
4453
4454 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4455   ;; List zombies and killed lists somewhat faster, which was
4456   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4457   ;; this by ignoring the group format specification altogether.
4458   (let (group beg)
4459     (if regexp
4460         ;; This loop is used when listing groups that match some
4461         ;; regexp.
4462         (while groups
4463           (setq group (pop groups))
4464           (when (string-match regexp group)
4465             (add-text-properties
4466              (point) (prog1 (1+ (point))
4467                        (insert " " mark "     *: " group "\n"))
4468              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4469                    'gnus-unread t
4470                    'gnus-level level))))
4471       ;; This loop is used when listing all groups.
4472       (while groups
4473         (add-text-properties
4474          (point) (prog1 (1+ (point))
4475                    (insert " " mark "     *: "
4476                            (setq group (pop groups)) "\n"))
4477          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4478                'gnus-unread t
4479                'gnus-level level))))))
4480
4481 (defmacro gnus-group-real-name (group)
4482   "Find the real name of a foreign newsgroup."
4483   `(let ((gname ,group))
4484      (if (string-match ":[^:]+$" gname)
4485          (substring gname (1+ (match-beginning 0)))
4486        gname)))
4487
4488 (defsubst gnus-server-add-address (method)
4489   (let ((method-name (symbol-name (car method))))
4490     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4491              (not (assq (intern (concat method-name "-address")) method)))
4492         (append method (list (list (intern (concat method-name "-address"))
4493                                    (nth 1 method))))
4494       method)))
4495
4496 (defsubst gnus-server-get-method (group method)
4497   ;; Input either a server name, and extended server name, or a
4498   ;; select method, and return a select method.
4499   (cond ((stringp method)
4500          (gnus-server-to-method method))
4501         ((and (stringp (car method)) group)
4502          (gnus-server-extend-method group method))
4503         (t
4504          (gnus-server-add-address method))))
4505
4506 (defun gnus-server-to-method (server)
4507   "Map virtual server names to select methods."
4508   (or 
4509    ;; Perhaps this is the native server?
4510    (and (equal server "native") gnus-select-method)
4511    ;; It should be in the server alist.
4512    (cdr (assoc server gnus-server-alist))
4513    ;; If not, we look through all the opened server
4514    ;; to see whether we can find it there.
4515    (let ((opened gnus-opened-servers))
4516      (while (and opened
4517                  (not (equal server (format "%s:%s" (caaar opened)
4518                                             (cadaar opened)))))
4519        (pop opened))
4520      (caar opened))))
4521
4522 (defmacro gnus-server-equal (ss1 ss2)
4523   "Say whether two servers are equal."
4524   `(let ((s1 ,ss1)
4525          (s2 ,ss2))
4526      (or (equal s1 s2)
4527          (and (= (length s1) (length s2))
4528               (progn
4529                 (while (and s1 (member (car s1) s2))
4530                   (setq s1 (cdr s1)))
4531                 (null s1))))))
4532
4533 (defun gnus-group-prefixed-name (group method)
4534   "Return the whole name from GROUP and METHOD."
4535   (and (stringp method) (setq method (gnus-server-to-method method)))
4536   (concat (format "%s" (car method))
4537           (if (and
4538                (or (assoc (format "%s" (car method)) 
4539                           (gnus-methods-using 'address))
4540                    (equal method gnus-message-archive-method))
4541                (not (string= (nth 1 method) "")))
4542               (concat "+" (nth 1 method)))
4543           ":" group))
4544
4545 (defun gnus-group-real-prefix (group)
4546   "Return the prefix of the current group name."
4547   (if (string-match "^[^:]+:" group)
4548       (substring group 0 (match-end 0))
4549     ""))
4550
4551 (defun gnus-group-method (group)
4552   "Return the server or method used for selecting GROUP."
4553   (let ((prefix (gnus-group-real-prefix group)))
4554     (if (equal prefix "")
4555         gnus-select-method
4556       (let ((servers gnus-opened-servers)
4557             (server "")
4558             backend possible found)
4559         (if (string-match "^[^\\+]+\\+" prefix)
4560             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4561                   server (substring prefix (match-end 0) (1- (length prefix))))
4562           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4563         (while servers
4564           (when (eq (caaar servers) backend)
4565             (setq possible (caar servers))
4566             (when (equal (cadaar servers) server)
4567               (setq found (caar servers))))
4568           (pop servers))
4569         (or (car (rassoc found gnus-server-alist))
4570             found
4571             (car (rassoc possible gnus-server-alist))
4572             possible
4573             (list backend server))))))
4574
4575 (defsubst gnus-secondary-method-p (method)
4576   "Return whether METHOD is a secondary select method."
4577   (let ((methods gnus-secondary-select-methods)
4578         (gmethod (gnus-server-get-method nil method)))
4579     (while (and methods
4580                 (not (equal (gnus-server-get-method nil (car methods))
4581                             gmethod)))
4582       (setq methods (cdr methods)))
4583     methods))
4584
4585 (defun gnus-group-foreign-p (group)
4586   "Say whether a group is foreign or not."
4587   (and (not (gnus-group-native-p group))
4588        (not (gnus-group-secondary-p group))))
4589
4590 (defun gnus-group-native-p (group)
4591   "Say whether the group is native or not."
4592   (not (string-match ":" group)))
4593
4594 (defun gnus-group-secondary-p (group)
4595   "Say whether the group is secondary or not."
4596   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4597
4598 (defun gnus-group-get-parameter (group &optional symbol)
4599   "Returns the group parameters for GROUP.
4600 If SYMBOL, return the value of that symbol in the group parameters."
4601   (let ((params (gnus-info-params (gnus-get-info group))))
4602     (if symbol
4603         (gnus-group-parameter-value params symbol)
4604       params)))
4605
4606 (defun gnus-group-parameter-value (params symbol)
4607   "Return the value of SYMBOL in group PARAMS."
4608   (or (car (memq symbol params))        ; It's either a simple symbol
4609       (cdr (assq symbol params))))      ; or a cons.
4610
4611 (defun gnus-group-add-parameter (group param)
4612   "Add parameter PARAM to GROUP."
4613   (let ((info (gnus-get-info group)))
4614     (if (not info)
4615         () ; This is a dead group.  We just ignore it.
4616       ;; Cons the new param to the old one and update.
4617       (gnus-group-set-info (cons param (gnus-info-params info))
4618                            group 'params))))
4619
4620 (defun gnus-group-set-parameter (group name value)
4621   "Set parameter NAME to VALUE in GROUP."
4622   (let ((info (gnus-get-info group)))
4623     (if (not info)
4624         () ; This is a dead group.  We just ignore it.
4625       (let ((old-params (gnus-info-params info))
4626             (new-params (list (cons name value))))
4627         (while old-params
4628           (if (or (not (listp (car old-params)))
4629                   (not (eq (caar old-params) name)))
4630               (setq new-params (append new-params (list (car old-params)))))
4631           (setq old-params (cdr old-params)))
4632         (gnus-group-set-info new-params group 'params)))))
4633
4634 (defun gnus-group-add-score (group &optional score)
4635   "Add SCORE to the GROUP score.
4636 If SCORE is nil, add 1 to the score of GROUP."
4637   (let ((info (gnus-get-info group)))
4638     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4639
4640 (defun gnus-summary-bubble-group ()
4641   "Increase the score of the current group.
4642 This is a handy function to add to `gnus-summary-exit-hook' to
4643 increase the score of each group you read."
4644   (gnus-group-add-score gnus-newsgroup-name))
4645
4646 (defun gnus-group-set-info (info &optional method-only-group part)
4647   (let* ((entry (gnus-gethash
4648                  (or method-only-group (gnus-info-group info))
4649                  gnus-newsrc-hashtb))
4650          (part-info info)
4651          (info (if method-only-group (nth 2 entry) info)))
4652     (when method-only-group
4653       (unless entry
4654         (error "Trying to change non-existent group %s" method-only-group))
4655       ;; We have received parts of the actual group info - either the
4656       ;; select method or the group parameters.  We first check
4657       ;; whether we have to extend the info, and if so, do that.
4658       (let ((len (length info))
4659             (total (if (eq part 'method) 5 6)))
4660         (when (< len total)
4661           (setcdr (nthcdr (1- len) info)
4662                   (make-list (- total len) nil)))
4663         ;; Then we enter the new info.
4664         (setcar (nthcdr (1- total) info) part-info)))
4665     (unless entry
4666       ;; This is a new group, so we just create it.
4667       (save-excursion
4668         (set-buffer gnus-group-buffer)
4669         (if (gnus-info-method info)
4670             ;; It's a foreign group...
4671             (gnus-group-make-group
4672              (gnus-group-real-name (gnus-info-group info))
4673              (prin1-to-string (car (gnus-info-method info)))
4674              (nth 1 (gnus-info-method info)))
4675           ;; It's a native group.
4676           (gnus-group-make-group (gnus-info-group info)))
4677         (gnus-message 6 "Note: New group created")
4678         (setq entry
4679               (gnus-gethash (gnus-group-prefixed-name
4680                              (gnus-group-real-name (gnus-info-group info))
4681                              (or (gnus-info-method info) gnus-select-method))
4682                             gnus-newsrc-hashtb))))
4683     ;; Whether it was a new group or not, we now have the entry, so we
4684     ;; can do the update.
4685     (if entry
4686         (progn
4687           (setcar (nthcdr 2 entry) info)
4688           (when (and (not (eq (car entry) t))
4689                      (gnus-active (gnus-info-group info)))
4690             (let ((marked (gnus-info-marks info)))
4691               (setcar entry (length (gnus-list-of-unread-articles
4692                                      (car info)))))))
4693       (error "No such group: %s" (gnus-info-group info)))))
4694
4695 (defun gnus-group-set-method-info (group select-method)
4696   (gnus-group-set-info select-method group 'method))
4697
4698 (defun gnus-group-set-params-info (group params)
4699   (gnus-group-set-info params group 'params))
4700
4701 (defun gnus-group-update-group-line ()
4702   "Update the current line in the group buffer."
4703   (let* ((buffer-read-only nil)
4704          (group (gnus-group-group-name))
4705          (gnus-group-indentation (gnus-group-group-indentation))
4706          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4707     (and entry
4708          (not (gnus-ephemeral-group-p group))
4709          (gnus-dribble-enter
4710           (concat "(gnus-group-set-info '"
4711                   (prin1-to-string (nth 2 entry)) ")")))
4712     (gnus-delete-line)
4713     (gnus-group-insert-group-line-info group)
4714     (forward-line -1)
4715     (gnus-group-position-point)))
4716
4717 (defun gnus-group-insert-group-line-info (group)
4718   "Insert GROUP on the current line."
4719   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4720         active info)
4721     (if entry
4722         (progn
4723           ;; (Un)subscribed group.
4724           (setq info (nth 2 entry))
4725           (gnus-group-insert-group-line
4726            group (gnus-info-level info) (gnus-info-marks info)
4727            (or (car entry) t) (gnus-info-method info)))
4728       ;; This group is dead.
4729       (gnus-group-insert-group-line
4730        group
4731        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4732        nil
4733        (if (setq active (gnus-active group))
4734            (- (1+ (cdr active)) (car active)) 0)
4735        nil))))
4736
4737 (defun gnus-group-insert-group-line
4738   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4739                   gnus-tmp-method)
4740   "Insert a group line in the group buffer."
4741   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4742          (gnus-tmp-number-total
4743           (if gnus-tmp-active
4744               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4745             0))
4746          (gnus-tmp-number-of-unread
4747           (if (numberp number) (int-to-string (max 0 number))
4748             "*"))
4749          (gnus-tmp-number-of-read
4750           (if (numberp number)
4751               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4752             "*"))
4753          (gnus-tmp-subscribed
4754           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4755                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4756                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4757                 (t ?K)))
4758          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4759          (gnus-tmp-newsgroup-description
4760           (if gnus-description-hashtb
4761               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4762             ""))
4763          (gnus-tmp-moderated
4764           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4765          (gnus-tmp-moderated-string
4766           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4767          (gnus-tmp-method
4768           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4769          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4770          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4771          (gnus-tmp-news-method-string
4772           (if gnus-tmp-method
4773               (format "(%s:%s)" (car gnus-tmp-method)
4774                       (cadr gnus-tmp-method)) ""))
4775          (gnus-tmp-marked-mark
4776           (if (and (numberp number)
4777                    (zerop number)
4778                    (cdr (assq 'tick gnus-tmp-marked)))
4779               ?* ? ))
4780          (gnus-tmp-process-marked
4781           (if (member gnus-tmp-group gnus-group-marked)
4782               gnus-process-mark ? ))
4783          (gnus-tmp-grouplens
4784           (or (and gnus-use-grouplens
4785                    (bbb-grouplens-group-p gnus-tmp-group))
4786               ""))
4787          (buffer-read-only nil)
4788          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4789     (beginning-of-line)
4790     (add-text-properties
4791      (point)
4792      (prog1 (1+ (point))
4793        ;; Insert the text.
4794        (eval gnus-group-line-format-spec))
4795      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4796        gnus-unread ,(if (numberp number)
4797                         (string-to-int gnus-tmp-number-of-unread)
4798                       t)
4799        gnus-marked ,gnus-tmp-marked-mark
4800        gnus-indentation ,gnus-group-indentation
4801        gnus-level ,gnus-tmp-level))
4802     (when (gnus-visual-p 'group-highlight 'highlight)
4803       (forward-line -1)
4804       (run-hooks 'gnus-group-update-hook)
4805       (forward-line))
4806     ;; Allow XEmacs to remove front-sticky text properties.
4807     (gnus-group-remove-excess-properties)))
4808
4809 (defun gnus-group-update-group (group &optional visible-only)
4810   "Update all lines where GROUP appear.
4811 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4812 already."
4813   (save-excursion
4814     (set-buffer gnus-group-buffer)
4815     ;; The buffer may be narrowed.
4816     (save-restriction
4817       (widen)
4818       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4819             (loc (point-min))
4820             found buffer-read-only visible)
4821         ;; Enter the current status into the dribble buffer.
4822         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4823           (if (and entry (not (gnus-ephemeral-group-p group)))
4824               (gnus-dribble-enter
4825                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4826                        ")"))))
4827         ;; Find all group instances.  If topics are in use, each group
4828         ;; may be listed in more than once.
4829         (while (setq loc (text-property-any
4830                           loc (point-max) 'gnus-group ident))
4831           (setq found t)
4832           (goto-char loc)
4833           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4834             (gnus-delete-line)
4835             (gnus-group-insert-group-line-info group))
4836           (setq loc (1+ loc)))
4837         (unless (or found visible-only)
4838           ;; No such line in the buffer, find out where it's supposed to
4839           ;; go, and insert it there (or at the end of the buffer).
4840           (if gnus-goto-missing-group-function
4841               (funcall gnus-goto-missing-group-function group)
4842             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4843               (while (and entry (car entry)
4844                           (not
4845                            (gnus-goto-char
4846                             (text-property-any
4847                              (point-min) (point-max)
4848                              'gnus-group (gnus-intern-safe
4849                                           (caar entry) gnus-active-hashtb)))))
4850                 (setq entry (cdr entry)))
4851               (or entry (goto-char (point-max)))))
4852           ;; Finally insert the line.
4853           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4854             (gnus-group-insert-group-line-info group)))
4855         (gnus-group-set-mode-line)))))
4856
4857 (defun gnus-group-set-mode-line ()
4858   (when (memq 'group gnus-updated-mode-lines)
4859     (let* ((gformat (or gnus-group-mode-line-format-spec
4860                         (setq gnus-group-mode-line-format-spec
4861                               (gnus-parse-format
4862                                gnus-group-mode-line-format
4863                                gnus-group-mode-line-format-alist))))
4864            (gnus-tmp-news-server (car (cdr gnus-select-method)))
4865            (gnus-tmp-news-method (car gnus-select-method))
4866            (max-len 60)
4867            gnus-tmp-header                      ;Dummy binding for user-defined formats
4868            ;; Get the resulting string.
4869            (mode-string (eval gformat)))
4870       ;; If the line is too long, we chop it off.
4871       (when (> (length mode-string) max-len)
4872         (setq mode-string (substring mode-string 0 (- max-len 4))))
4873       (prog1
4874           (setq mode-line-buffer-identification (list mode-string))
4875         (set-buffer-modified-p t)))))
4876
4877 (defun gnus-group-group-name ()
4878   "Get the name of the newsgroup on the current line."
4879   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4880     (and group (symbol-name group))))
4881
4882 (defun gnus-group-group-level ()
4883   "Get the level of the newsgroup on the current line."
4884   (get-text-property (gnus-point-at-bol) 'gnus-level))
4885
4886 (defun gnus-group-group-indentation ()
4887   "Get the indentation of the newsgroup on the current line."
4888   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation) ""))
4889
4890 (defun gnus-group-group-unread ()
4891   "Get the number of unread articles of the newsgroup on the current line."
4892   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4893
4894 (defun gnus-group-search-forward (&optional backward all level first-too)
4895   "Find the next newsgroup with unread articles.
4896 If BACKWARD is non-nil, find the previous newsgroup instead.
4897 If ALL is non-nil, just find any newsgroup.
4898 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4899 group exists.
4900 If FIRST-TOO, the current line is also eligible as a target."
4901   (let ((way (if backward -1 1))
4902         (low gnus-level-killed)
4903         (beg (point))
4904         pos found lev)
4905     (if (and backward (progn (beginning-of-line)) (bobp))
4906         nil
4907       (or first-too (forward-line way))
4908       (while (and
4909               (not (eobp))
4910               (not (setq
4911                     found
4912                     (and (or all
4913                              (and
4914                               (let ((unread
4915                                      (get-text-property (point) 'gnus-unread)))
4916                                 (and (numberp unread) (> unread 0)))
4917                               (setq lev (get-text-property (point)
4918                                                            'gnus-level))
4919                               (<= lev gnus-level-subscribed)))
4920                          (or (not level)
4921                              (and (setq lev (get-text-property (point)
4922                                                                'gnus-level))
4923                                   (or (= lev level)
4924                                       (and (< lev low)
4925                                            (< level lev)
4926                                            (progn
4927                                              (setq low lev)
4928                                              (setq pos (point))
4929                                              nil))))))))
4930               (zerop (forward-line way)))))
4931     (if found
4932         (progn (gnus-group-position-point) t)
4933       (goto-char (or pos beg))
4934       (and pos t))))
4935
4936 ;;; Gnus group mode commands
4937
4938 ;; Group marking.
4939
4940 (defun gnus-group-mark-group (n &optional unmark no-advance)
4941   "Mark the current group."
4942   (interactive "p")
4943   (let ((buffer-read-only nil)
4944         group)
4945     (while
4946         (and (> n 0)
4947              (setq group (gnus-group-group-name))
4948              (progn
4949                (beginning-of-line)
4950                (forward-char
4951                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4952                (delete-char 1)
4953                (if unmark
4954                    (progn
4955                      (insert " ")
4956                      (setq gnus-group-marked (delete group gnus-group-marked)))
4957                  (insert "#")
4958                  (setq gnus-group-marked
4959                        (cons group (delete group gnus-group-marked))))
4960                t)
4961              (or no-advance (zerop (gnus-group-next-group 1))))
4962       (setq n (1- n)))
4963     (gnus-summary-position-point)
4964     n))
4965
4966 (defun gnus-group-unmark-group (n)
4967   "Remove the mark from the current group."
4968   (interactive "p")
4969   (gnus-group-mark-group n 'unmark)
4970   (gnus-group-position-point))
4971
4972 (defun gnus-group-unmark-all-groups ()
4973   "Unmark all groups."
4974   (let ((groups gnus-group-marked))
4975     (save-excursion
4976       (while groups
4977         (gnus-group-remove-mark (pop groups)))))
4978   (gnus-group-position-point))
4979
4980 (defun gnus-group-mark-region (unmark beg end)
4981   "Mark all groups between point and mark.
4982 If UNMARK, remove the mark instead."
4983   (interactive "P\nr")
4984   (let ((num (count-lines beg end)))
4985     (save-excursion
4986       (goto-char beg)
4987       (- num (gnus-group-mark-group num unmark)))))
4988
4989 (defun gnus-group-mark-buffer (&optional unmark)
4990   "Mark all groups in the buffer.
4991 If UNMARK, remove the mark instead."
4992   (interactive "P")
4993   (gnus-group-mark-region unmark (point-min) (point-max)))
4994
4995 (defun gnus-group-mark-regexp (regexp)
4996   "Mark all groups that match some regexp."
4997   (interactive "sMark (regexp): ")
4998   (let ((alist (cdr gnus-newsrc-alist))
4999         group)
5000     (while alist
5001       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5002         (gnus-group-set-mark group))))
5003   (gnus-group-position-point))
5004
5005 (defun gnus-group-remove-mark (group)
5006   "Remove the process mark from GROUP and move point there.
5007 Return nil if the group isn't displayed."
5008   (if (gnus-group-goto-group group)
5009       (save-excursion
5010         (gnus-group-mark-group 1 'unmark t)
5011         t)
5012     (setq gnus-group-marked
5013           (delete group gnus-group-marked))
5014     nil))
5015
5016 (defun gnus-group-set-mark (group)
5017   "Set the process mark on GROUP."
5018   (if (gnus-group-goto-group group)
5019       (save-excursion
5020         (gnus-group-mark-group 1 nil t))
5021     (setq gnus-group-marked
5022           (cons group (delete group gnus-group-marked)))))
5023
5024 (defun gnus-group-universal-argument (arg &optional groups func)
5025   "Perform any command on all groups accoring to the process/prefix convention."
5026   (interactive "P")
5027   (let ((groups (or groups (gnus-group-process-prefix arg)))
5028         group func)
5029     (if (eq (setq func (or func
5030                            (key-binding
5031                             (read-key-sequence
5032                              (substitute-command-keys
5033                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5034             'undefined)
5035         (progn
5036           (message "Undefined key")
5037           (ding))
5038       (while groups
5039         (gnus-group-remove-mark (setq group (pop groups)))
5040         (command-execute func))))
5041   (gnus-group-position-point))
5042
5043 (defun gnus-group-process-prefix (n)
5044   "Return a list of groups to work on.
5045 Take into consideration N (the prefix) and the list of marked groups."
5046   (cond
5047    (n
5048     (setq n (prefix-numeric-value n))
5049     ;; There is a prefix, so we return a list of the N next
5050     ;; groups.
5051     (let ((way (if (< n 0) -1 1))
5052           (n (abs n))
5053           group groups)
5054       (save-excursion
5055         (while (and (> n 0)
5056                     (setq group (gnus-group-group-name)))
5057           (setq groups (cons group groups))
5058           (setq n (1- n))
5059           (gnus-group-next-group way)))
5060       (nreverse groups)))
5061    ((and (boundp 'transient-mark-mode)
5062          transient-mark-mode
5063          mark-active)
5064     ;; Work on the region between point and mark.
5065     (let ((max (max (point) (mark)))
5066           groups)
5067       (save-excursion
5068         (goto-char (min (point) (mark)))
5069         (while
5070             (and
5071              (push (gnus-group-group-name) groups)
5072              (zerop (gnus-group-next-group 1))
5073              (< (point) max)))
5074         (nreverse groups))))
5075    (gnus-group-marked
5076     ;; No prefix, but a list of marked articles.
5077     (reverse gnus-group-marked))
5078    (t
5079     ;; Neither marked articles or a prefix, so we return the
5080     ;; current group.
5081     (let ((group (gnus-group-group-name)))
5082       (and group (list group))))))
5083
5084 ;; Selecting groups.
5085
5086 (defun gnus-group-read-group (&optional all no-article group)
5087   "Read news in this newsgroup.
5088 If the prefix argument ALL is non-nil, already read articles become
5089 readable.  IF ALL is a number, fetch this number of articles.  If the
5090 optional argument NO-ARTICLE is non-nil, no article will be
5091 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5092 group."
5093   (interactive "P")
5094   (let ((group (or group (gnus-group-group-name)))
5095         number active marked entry)
5096     (or group (error "No group on current line"))
5097     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5098                                             group gnus-newsrc-hashtb)))))
5099     ;; This group might be a dead group.  In that case we have to get
5100     ;; the number of unread articles from `gnus-active-hashtb'.
5101     (setq number
5102           (cond ((numberp all) all)
5103                 (entry (car entry))
5104                 ((setq active (gnus-active group))
5105                  (- (1+ (cdr active)) (car active)))))
5106     (gnus-summary-read-group
5107      group (or all (and (numberp number)
5108                         (zerop (+ number (length (cdr (assq 'tick marked)))
5109                                   (length (cdr (assq 'dormant marked)))))))
5110      no-article)))
5111
5112 (defun gnus-group-select-group (&optional all)
5113   "Select this newsgroup.
5114 No article is selected automatically.
5115 If ALL is non-nil, already read articles become readable.
5116 If ALL is a number, fetch this number of articles."
5117   (interactive "P")
5118   (gnus-group-read-group all t))
5119
5120 (defun gnus-group-quick-select-group (&optional all)
5121   "Select the current group \"quickly\".
5122 This means that no highlighting or scoring will be performed."
5123   (interactive "P")
5124   (let (gnus-visual
5125         gnus-score-find-score-files-function
5126         gnus-apply-kill-hook
5127         gnus-summary-expunge-below)
5128     (gnus-group-read-group all t)))
5129
5130 (defun gnus-group-visible-select-group (&optional all)
5131   "Select the current group without hiding any articles."
5132   (interactive "P")
5133   (let ((gnus-inhibit-limiting t))
5134     (gnus-group-read-group all t)))
5135
5136 ;;;###autoload
5137 (defun gnus-fetch-group (group)
5138   "Start Gnus if necessary and enter GROUP.
5139 Returns whether the fetching was successful or not."
5140   (interactive "sGroup name: ")
5141   (or (get-buffer gnus-group-buffer)
5142       (gnus))
5143   (gnus-group-select-group))
5144
5145 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5146 ;; if selection was successful.
5147 (defun gnus-group-read-ephemeral-group
5148   (group method &optional activate quit-config)
5149   (let ((group (if (gnus-group-foreign-p group) group
5150                  (gnus-group-prefixed-name group method))))
5151     (gnus-sethash
5152      group
5153      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5154                      ((quit-config . ,(if quit-config quit-config
5155                                         (cons (current-buffer) 'summary))))))
5156      gnus-newsrc-hashtb)
5157     (set-buffer gnus-group-buffer)
5158     (or (gnus-check-server method)
5159         (error "Unable to contact server: %s" (gnus-status-message method)))
5160     (if activate (or (gnus-request-group group)
5161                      (error "Couldn't request group")))
5162     (condition-case ()
5163         (gnus-group-read-group t t group)
5164       (error nil)
5165       (quit nil))))
5166
5167 (defun gnus-group-jump-to-group (group)
5168   "Jump to newsgroup GROUP."
5169   (interactive
5170    (list (completing-read
5171           "Group: " gnus-active-hashtb nil
5172           (memq gnus-select-method gnus-have-read-active-file))))
5173
5174   (if (equal group "")
5175       (error "Empty group name"))
5176
5177   (let ((b (text-property-any
5178             (point-min) (point-max)
5179             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5180     (unless (gnus-ephemeral-group-p group)
5181       (if b
5182           ;; Either go to the line in the group buffer...
5183           (goto-char b)
5184         ;; ... or insert the line.
5185         (or
5186          (gnus-active group)
5187          (gnus-activate-group group)
5188          (error "%s error: %s" group (gnus-status-message group)))
5189
5190         (gnus-group-update-group group)
5191         (goto-char (text-property-any
5192                     (point-min) (point-max)
5193                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5194     ;; Adjust cursor point.
5195     (gnus-group-position-point)))
5196
5197 (defun gnus-group-goto-group (group)
5198   "Goto to newsgroup GROUP."
5199   (when group
5200     (let ((b (text-property-any (point-min) (point-max)
5201                                 'gnus-group (gnus-intern-safe
5202                                              group gnus-active-hashtb))))
5203       (and b (goto-char b)))))
5204
5205 (defun gnus-group-next-group (n)
5206   "Go to next N'th newsgroup.
5207 If N is negative, search backward instead.
5208 Returns the difference between N and the number of skips actually
5209 done."
5210   (interactive "p")
5211   (gnus-group-next-unread-group n t))
5212
5213 (defun gnus-group-next-unread-group (n &optional all level)
5214   "Go to next N'th unread newsgroup.
5215 If N is negative, search backward instead.
5216 If ALL is non-nil, choose any newsgroup, unread or not.
5217 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5218 such group can be found, the next group with a level higher than
5219 LEVEL.
5220 Returns the difference between N and the number of skips actually
5221 made."
5222   (interactive "p")
5223   (let ((backward (< n 0))
5224         (n (abs n)))
5225     (while (and (> n 0)
5226                 (gnus-group-search-forward
5227                  backward (or (not gnus-group-goto-unread) all) level))
5228       (setq n (1- n)))
5229     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5230                                (if level " on this level or higher" "")))
5231     n))
5232
5233 (defun gnus-group-prev-group (n)
5234   "Go to previous N'th newsgroup.
5235 Returns the difference between N and the number of skips actually
5236 done."
5237   (interactive "p")
5238   (gnus-group-next-unread-group (- n) t))
5239
5240 (defun gnus-group-prev-unread-group (n)
5241   "Go to previous N'th unread newsgroup.
5242 Returns the difference between N and the number of skips actually
5243 done."
5244   (interactive "p")
5245   (gnus-group-next-unread-group (- n)))
5246
5247 (defun gnus-group-next-unread-group-same-level (n)
5248   "Go to next N'th unread newsgroup on the same level.
5249 If N is negative, search backward instead.
5250 Returns the difference between N and the number of skips actually
5251 done."
5252   (interactive "p")
5253   (gnus-group-next-unread-group n t (gnus-group-group-level))
5254   (gnus-group-position-point))
5255
5256 (defun gnus-group-prev-unread-group-same-level (n)
5257   "Go to next N'th unread newsgroup on the same level.
5258 Returns the difference between N and the number of skips actually
5259 done."
5260   (interactive "p")
5261   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5262   (gnus-group-position-point))
5263
5264 (defun gnus-group-best-unread-group (&optional exclude-group)
5265   "Go to the group with the highest level.
5266 If EXCLUDE-GROUP, do not go to that group."
5267   (interactive)
5268   (goto-char (point-min))
5269   (let ((best 100000)
5270         unread best-point)
5271     (while (setq unread (get-text-property (point) 'gnus-unread))
5272       (if (and (numberp unread) (> unread 0))
5273           (progn
5274             (if (and (< (get-text-property (point) 'gnus-level) best)
5275                      (or (not exclude-group)
5276                          (not (equal exclude-group (gnus-group-group-name)))))
5277                 (progn
5278                   (setq best (get-text-property (point) 'gnus-level))
5279                   (setq best-point (point))))))
5280       (forward-line 1))
5281     (if best-point (goto-char best-point))
5282     (gnus-summary-position-point)
5283     (and best-point (gnus-group-group-name))))
5284
5285 (defun gnus-group-first-unread-group ()
5286   "Go to the first group with unread articles."
5287   (interactive)
5288   (prog1
5289       (let ((opoint (point))
5290             unread)
5291         (goto-char (point-min))
5292         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5293                 (and (numberp unread)   ; Not a topic.
5294                      (not (zerop unread))) ; Has unread articles.
5295                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5296             (point)                     ; Success.
5297           (goto-char opoint)
5298           nil))                         ; Not success.
5299     (gnus-group-position-point)))
5300
5301 (defun gnus-group-enter-server-mode ()
5302   "Jump to the server buffer."
5303   (interactive)
5304   (gnus-enter-server-buffer))
5305
5306 (defun gnus-group-make-group (name &optional method address)
5307   "Add a new newsgroup.
5308 The user will be prompted for a NAME, for a select METHOD, and an
5309 ADDRESS."
5310   (interactive
5311    (cons
5312     (read-string "Group name: ")
5313     (let ((method
5314            (completing-read
5315             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5316             nil t)))
5317       (if (assoc method gnus-valid-select-methods)
5318           (list method
5319                 (if (memq 'prompt-address
5320                           (assoc method gnus-valid-select-methods))
5321                     (read-string "Address: ")
5322                   ""))
5323         (list method "")))))
5324
5325   (save-excursion
5326     (set-buffer gnus-group-buffer)
5327     (let* ((meth (and method (if address (list (intern method) address)
5328                                method)))
5329            (nname (if method (gnus-group-prefixed-name name meth) name))
5330            info)
5331       (and (gnus-gethash nname gnus-newsrc-hashtb)
5332            (error "Group %s already exists" nname))
5333       (gnus-group-change-level
5334        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5335        gnus-level-default-subscribed gnus-level-killed
5336        (and (gnus-group-group-name)
5337             (gnus-gethash (gnus-group-group-name)
5338                           gnus-newsrc-hashtb))
5339        t)
5340       (gnus-set-active nname (cons 1 0))
5341       (or (gnus-ephemeral-group-p name)
5342           (gnus-dribble-enter
5343            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5344       (gnus-group-insert-group-line-info nname)
5345
5346       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5347         (require (car meth)))
5348       (gnus-check-server meth)
5349       (and (gnus-check-backend-function 'request-create-group nname)
5350            (gnus-request-create-group nname))
5351       t)))
5352
5353 (defun gnus-group-delete-group (group &optional force)
5354   "Delete the current group.
5355 If FORCE (the prefix) is non-nil, all the articles in the group will
5356 be deleted.  This is \"deleted\" as in \"removed forever from the face
5357 of the Earth\".  There is no undo."
5358   (interactive
5359    (list (gnus-group-group-name)
5360          current-prefix-arg))
5361   (or group (error "No group to rename"))
5362   (or (gnus-check-backend-function 'request-delete-group group)
5363       (error "This backend does not support group deletion"))
5364   (prog1
5365       (if (not (gnus-yes-or-no-p
5366                 (format
5367                  "Do you really want to delete %s%s? "
5368                  group (if force " and all its contents" ""))))
5369           () ; Whew!
5370         (gnus-message 6 "Deleting group %s..." group)
5371         (if (not (gnus-request-delete-group group force))
5372             (progn
5373               (gnus-message 3 "Couldn't delete group %s" group)
5374               (ding))
5375           (gnus-message 6 "Deleting group %s...done" group)
5376           (gnus-group-goto-group group)
5377           (gnus-group-kill-group 1 t)
5378           t))
5379     (gnus-group-position-point)))
5380
5381 (defun gnus-group-rename-group (group new-name)
5382   (interactive
5383    (list
5384     (gnus-group-group-name)
5385     (progn
5386       (or (gnus-check-backend-function
5387            'request-rename-group (gnus-group-group-name))
5388           (error "This backend does not support renaming groups"))
5389       (read-string "New group name: "))))
5390
5391   (or (gnus-check-backend-function 'request-rename-group group)
5392       (error "This backend does not support renaming groups"))
5393
5394   (or group (error "No group to rename"))
5395   (and (string-match "^[ \t]*$" new-name)
5396        (error "Not a valid group name"))
5397
5398   ;; We find the proper prefixed name.
5399   (setq new-name
5400         (gnus-group-prefixed-name
5401          (gnus-group-real-name new-name)
5402          (gnus-info-method (gnus-get-info group))))
5403
5404   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5405   (prog1
5406       (if (not (gnus-request-rename-group group new-name))
5407           (progn
5408             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5409             (ding))
5410         ;; We rename the group internally by killing it...
5411         (gnus-group-goto-group group)
5412         (gnus-group-kill-group)
5413         ;; ... changing its name ...
5414         (setcar (cdr (car gnus-list-of-killed-groups))
5415                 new-name)
5416         ;; ... and then yanking it.  Magic!
5417         (gnus-group-yank-group)
5418         (gnus-set-active new-name (gnus-active group))
5419         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5420         new-name)
5421     (gnus-group-position-point)))
5422
5423 (defun gnus-group-edit-group (group &optional part)
5424   "Edit the group on the current line."
5425   (interactive (list (gnus-group-group-name)))
5426   (let* ((part (or part 'info))
5427          (done-func `(lambda ()
5428                        "Exit editing mode and update the information."
5429                        (interactive)
5430                        (gnus-group-edit-group-done ',part ,group)))
5431          (winconf (current-window-configuration))
5432          info)
5433     (or group (error "No group on current line"))
5434     (or (setq info (gnus-get-info group))
5435         (error "Killed group; can't be edited"))
5436     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5437     (gnus-configure-windows 'edit-group)
5438     (gnus-add-current-to-buffer-list)
5439     (emacs-lisp-mode)
5440     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5441     (use-local-map (copy-keymap emacs-lisp-mode-map))
5442     (local-set-key "\C-c\C-c" done-func)
5443     (make-local-variable 'gnus-prev-winconf)
5444     (setq gnus-prev-winconf winconf)
5445     (erase-buffer)
5446     (insert
5447      (cond
5448       ((eq part 'method)
5449        ";; Type `C-c C-c' after editing the select method.\n\n")
5450       ((eq part 'params)
5451        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5452       ((eq part 'info)
5453        ";; Type `C-c C-c' after editing the group info.\n\n")))
5454     (insert
5455      (pp-to-string
5456       (cond ((eq part 'method)
5457              (or (gnus-info-method info) "native"))
5458             ((eq part 'params)
5459              (gnus-info-params info))
5460             (t info)))
5461      "\n")))
5462
5463 (defun gnus-group-edit-group-method (group)
5464   "Edit the select method of GROUP."
5465   (interactive (list (gnus-group-group-name)))
5466   (gnus-group-edit-group group 'method))
5467
5468 (defun gnus-group-edit-group-parameters (group)
5469   "Edit the group parameters of GROUP."
5470   (interactive (list (gnus-group-group-name)))
5471   (gnus-group-edit-group group 'params))
5472
5473 (defun gnus-group-edit-group-done (part group)
5474   "Get info from buffer, update variables and jump to the group buffer."
5475   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5476   (goto-char (point-min))
5477   (let* ((form (read (current-buffer)))
5478          (winconf gnus-prev-winconf)
5479          (method (cond ((eq part 'info) (nth 4 form))
5480                        ((eq part 'method) form)
5481                        (t nil)))
5482          (info (cond ((eq part 'info) form)
5483                      ((eq part 'method) (gnus-get-info group))
5484                      (t nil)))
5485          (new-group (if info
5486                       (if (or (not method)
5487                               (gnus-server-equal
5488                                gnus-select-method method))
5489                           (gnus-group-real-name (car info))
5490                         (gnus-group-prefixed-name
5491                          (gnus-group-real-name (car info)) method))
5492                       nil)))
5493     (when (and new-group
5494                (not (equal new-group group)))
5495       (when (gnus-group-goto-group group)
5496         (gnus-group-kill-group 1))
5497       (gnus-activate-group new-group))
5498     ;; Set the info.
5499     (if (and info new-group)
5500         (progn
5501           (setq info (gnus-copy-sequence info))
5502           (setcar info new-group)
5503           (setcar (cddddr info) method)
5504           (gnus-group-set-info info))
5505       (gnus-group-set-info form (or new-group group) part))
5506     (kill-buffer (current-buffer))
5507     (and winconf (set-window-configuration winconf))
5508     (set-buffer gnus-group-buffer)
5509     (gnus-group-update-group (or new-group group))
5510     (gnus-group-position-point)))
5511
5512 (defun gnus-group-make-help-group ()
5513   "Create the Gnus documentation group."
5514   (interactive)
5515   (let ((path load-path)
5516         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5517         file dir)
5518     (and (gnus-gethash name gnus-newsrc-hashtb)
5519          (error "Documentation group already exists"))
5520     (while path
5521       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5522             file nil)
5523       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5524                 (file-exists-p
5525                  (setq file (concat (file-name-directory
5526                                      (directory-file-name dir))
5527                                     "etc/gnus-tut.txt"))))
5528         (setq path nil)))
5529     (if (not file)
5530         (message "Couldn't find doc group")
5531       (gnus-group-make-group
5532        (gnus-group-real-name name)
5533        (list 'nndoc name
5534              (list 'nndoc-address file)
5535              (list 'nndoc-article-type 'mbox)))))
5536   (gnus-group-position-point))
5537
5538 (defun gnus-group-make-doc-group (file type)
5539   "Create a group that uses a single file as the source."
5540   (interactive
5541    (list (read-file-name "File name: ")
5542          (and current-prefix-arg 'ask)))
5543   (when (eq type 'ask)
5544     (let ((err "")
5545           char found)
5546       (while (not found)
5547         (message
5548          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5549          err)
5550         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5551                           ((= char ?b) 'babyl)
5552                           ((= char ?d) 'digest)
5553                           ((= char ?f) 'forward)
5554                           ((= char ?a) 'mmfd)
5555                           (t (setq err (format "%c unknown. " char))
5556                              nil))))
5557       (setq type found)))
5558   (let* ((file (expand-file-name file))
5559          (name (gnus-generate-new-group-name
5560                 (gnus-group-prefixed-name
5561                  (file-name-nondirectory file) '(nndoc "")))))
5562     (gnus-group-make-group
5563      (gnus-group-real-name name)
5564      (list 'nndoc name
5565            (list 'nndoc-address file)
5566            (list 'nndoc-article-type (or type 'guess))))
5567     (forward-line -1)
5568     (gnus-group-position-point)))
5569
5570 (defun gnus-group-make-archive-group (&optional all)
5571   "Create the (ding) Gnus archive group of the most recent articles.
5572 Given a prefix, create a full group."
5573   (interactive "P")
5574   (let ((group (gnus-group-prefixed-name
5575                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5576     (and (gnus-gethash group gnus-newsrc-hashtb)
5577          (error "Archive group already exists"))
5578     (gnus-group-make-group
5579      (gnus-group-real-name group)
5580      (list 'nndir (if all "hpc" "edu")
5581            (list 'nndir-directory
5582                  (if all gnus-group-archive-directory
5583                    gnus-group-recent-archive-directory)))))
5584   (forward-line -1)
5585   (gnus-group-position-point))
5586
5587 (defun gnus-group-make-directory-group (dir)
5588   "Create an nndir group.
5589 The user will be prompted for a directory.  The contents of this
5590 directory will be used as a newsgroup.  The directory should contain
5591 mail messages or news articles in files that have numeric names."
5592   (interactive
5593    (list (read-file-name "Create group from directory: ")))
5594   (or (file-exists-p dir) (error "No such directory"))
5595   (or (file-directory-p dir) (error "Not a directory"))
5596   (let ((ext "")
5597         (i 0)
5598         group)
5599     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5600       (setq group
5601             (gnus-group-prefixed-name
5602              (concat (file-name-as-directory (directory-file-name dir))
5603                      ext)
5604              '(nndir "")))
5605       (setq ext (format "<%d>" (setq i (1+ i)))))
5606     (gnus-group-make-group
5607      (gnus-group-real-name group)
5608      (list 'nndir group (list 'nndir-directory dir))))
5609   (forward-line -1)
5610   (gnus-group-position-point))
5611
5612 (defun gnus-group-make-kiboze-group (group address scores)
5613   "Create an nnkiboze group.
5614 The user will be prompted for a name, a regexp to match groups, and
5615 score file entries for articles to include in the group."
5616   (interactive
5617    (list
5618     (read-string "nnkiboze group name: ")
5619     (read-string "Source groups (regexp): ")
5620     (let ((headers (mapcar (lambda (group) (list group))
5621                            '("subject" "from" "number" "date" "message-id"
5622                              "references" "chars" "lines" "xref"
5623                              "followup" "all" "body" "head")))
5624           scores header regexp regexps)
5625       (while (not (equal "" (setq header (completing-read
5626                                           "Match on header: " headers nil t))))
5627         (setq regexps nil)
5628         (while (not (equal "" (setq regexp (read-string
5629                                             (format "Match on %s (string): "
5630                                                     header)))))
5631           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5632         (setq scores (cons (cons header regexps) scores)))
5633       scores)))
5634   (gnus-group-make-group group "nnkiboze" address)
5635   (save-excursion
5636     (gnus-set-work-buffer)
5637     (let (emacs-lisp-mode-hook)
5638       (pp scores (current-buffer)))
5639     (write-region (point-min) (point-max)
5640                   (gnus-score-file-name (concat "nnkiboze:" group))))
5641   (forward-line -1)
5642   (gnus-group-position-point))
5643
5644 (defun gnus-group-add-to-virtual (n vgroup)
5645   "Add the current group to a virtual group."
5646   (interactive
5647    (list current-prefix-arg
5648          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5649                           "nnvirtual:")))
5650   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5651       (error "%s is not an nnvirtual group" vgroup))
5652   (let* ((groups (gnus-group-process-prefix n))
5653          (method (gnus-info-method (gnus-get-info vgroup))))
5654     (setcar (cdr method)
5655             (concat
5656              (nth 1 method) "\\|"
5657              (mapconcat
5658               (lambda (s)
5659                 (gnus-group-remove-mark s)
5660                 (concat "\\(^" (regexp-quote s) "$\\)"))
5661               groups "\\|"))))
5662   (gnus-group-position-point))
5663
5664 (defun gnus-group-make-empty-virtual (group)
5665   "Create a new, fresh, empty virtual group."
5666   (interactive "sCreate new, empty virtual group: ")
5667   (let* ((method (list 'nnvirtual "^$"))
5668          (pgroup (gnus-group-prefixed-name group method)))
5669     ;; Check whether it exists already.
5670     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5671          (error "Group %s already exists." pgroup))
5672     ;; Subscribe the new group after the group on the current line.
5673     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5674     (gnus-group-update-group pgroup)
5675     (forward-line -1)
5676     (gnus-group-position-point)))
5677
5678 (defun gnus-group-enter-directory (dir)
5679   "Enter an ephemeral nneething group."
5680   (interactive "DDirectory to read: ")
5681   (let* ((method (list 'nneething dir))
5682          (leaf (gnus-group-prefixed-name
5683                 (file-name-nondirectory (directory-file-name dir))
5684                 method))
5685          (name (gnus-generate-new-group-name leaf)))
5686     (let ((nneething-read-only t))
5687       (or (gnus-group-read-ephemeral-group
5688            name method t
5689            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5690                                       'summary 'group)))
5691           (error "Couldn't enter %s" dir)))))
5692
5693 ;; Group sorting commands
5694 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5695
5696 (defun gnus-group-sort-groups (func &optional reverse)
5697   "Sort the group buffer according to FUNC.
5698 If REVERSE, reverse the sorting order."
5699   (interactive (list gnus-group-sort-function
5700                      current-prefix-arg))
5701   (let ((func (cond 
5702                ((not (listp func)) func)
5703                ((null func) func)
5704                ((= 1 (length func)) (car func))
5705                (t `(lambda (t1 t2)
5706                      ,(gnus-make-sort-function 
5707                        (reverse func)))))))
5708     ;; We peel off the dummy group from the alist.
5709     (when func
5710       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5711         (pop gnus-newsrc-alist))
5712       ;; Do the sorting.
5713       (setq gnus-newsrc-alist
5714             (sort gnus-newsrc-alist func))
5715       (when reverse
5716         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5717       ;; Regenerate the hash table.
5718       (gnus-make-hashtable-from-newsrc-alist)
5719       (gnus-group-list-groups))))
5720
5721 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5722   "Sort the group buffer alphabetically by group name.
5723 If REVERSE, sort in reverse order."
5724   (interactive "P")
5725   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5726
5727 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5728   "Sort the group buffer by number of unread articles.
5729 If REVERSE, sort in reverse order."
5730   (interactive "P")
5731   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5732
5733 (defun gnus-group-sort-groups-by-level (&optional reverse)
5734   "Sort the group buffer by group level.
5735 If REVERSE, sort in reverse order."
5736   (interactive "P")
5737   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5738
5739 (defun gnus-group-sort-groups-by-score (&optional reverse)
5740   "Sort the group buffer by group score.
5741 If REVERSE, sort in reverse order."
5742   (interactive "P")
5743   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5744
5745 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5746   "Sort the group buffer by group rank.
5747 If REVERSE, sort in reverse order."
5748   (interactive "P")
5749   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5750
5751 (defun gnus-group-sort-groups-by-method (&optional reverse)
5752   "Sort the group buffer alphabetically by backend name.
5753 If REVERSE, sort in reverse order."
5754   (interactive "P")
5755   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5756
5757 (defun gnus-group-sort-by-alphabet (info1 info2)
5758   "Sort alphabetically."
5759   (string< (gnus-info-group info1) (gnus-info-group info2)))
5760
5761 (defun gnus-group-sort-by-unread (info1 info2)
5762   "Sort by number of unread articles."
5763   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5764         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5765     (< (or (and (numberp n1) n1) 0)
5766        (or (and (numberp n2) n2) 0))))
5767
5768 (defun gnus-group-sort-by-level (info1 info2)
5769   "Sort by level."
5770   (< (gnus-info-level info1) (gnus-info-level info2)))
5771
5772 (defun gnus-group-sort-by-method (info1 info2)
5773   "Sort alphabetically by backend name."
5774   (string< (symbol-name (car (gnus-find-method-for-group
5775                               (gnus-info-group info1) info1)))
5776            (symbol-name (car (gnus-find-method-for-group
5777                               (gnus-info-group info2) info2)))))
5778
5779 (defun gnus-group-sort-by-score (info1 info2)
5780   "Sort by group score."
5781   (< (gnus-info-score info1) (gnus-info-score info2)))
5782
5783 (defun gnus-group-sort-by-rank (info1 info2)
5784   "Sort by level and score."
5785   (let ((level1 (gnus-info-level info1))
5786         (level2 (gnus-info-level info2)))
5787     (or (< level1 level2)
5788         (and (= level1 level2)
5789              (< (gnus-info-score info1) (gnus-info-score info2))))))
5790
5791 ;; Group catching up.
5792
5793 (defun gnus-group-catchup-current (&optional n all)
5794   "Mark all articles not marked as unread in current newsgroup as read.
5795 If prefix argument N is numeric, the ARG next newsgroups will be
5796 caught up.  If ALL is non-nil, marked articles will also be marked as
5797 read.  Cross references (Xref: header) of articles are ignored.
5798 The difference between N and actual number of newsgroups that were
5799 caught up is returned."
5800   (interactive "P")
5801   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5802                gnus-expert-user
5803                (gnus-y-or-n-p
5804                 (if all
5805                     "Do you really want to mark all articles as read? "
5806                   "Mark all unread articles as read? "))))
5807       n
5808     (let ((groups (gnus-group-process-prefix n))
5809           (ret 0))
5810       (while groups
5811         ;; Virtual groups have to be given special treatment.
5812         (let ((method (gnus-find-method-for-group (car groups))))
5813           (if (eq 'nnvirtual (car method))
5814               (nnvirtual-catchup-group
5815                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5816         (gnus-group-remove-mark (car groups))
5817         (if (prog1
5818                 (gnus-group-goto-group (car groups))
5819               (gnus-group-catchup (car groups) all))
5820             (gnus-group-update-group-line)
5821           (setq ret (1+ ret)))
5822         (setq groups (cdr groups)))
5823       (gnus-group-next-unread-group 1)
5824       ret)))
5825
5826 (defun gnus-group-catchup-current-all (&optional n)
5827   "Mark all articles in current newsgroup as read.
5828 Cross references (Xref: header) of articles are ignored."
5829   (interactive "P")
5830   (gnus-group-catchup-current n 'all))
5831
5832 (defun gnus-group-catchup (group &optional all)
5833   "Mark all articles in GROUP as read.
5834 If ALL is non-nil, all articles are marked as read.
5835 The return value is the number of articles that were marked as read,
5836 or nil if no action could be taken."
5837   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5838          (num (car entry)))
5839     ;; Do the updating only if the newsgroup isn't killed.
5840     (if (not (numberp (car entry)))
5841         (gnus-message 1 "Can't catch up; non-active group")
5842       ;; Do auto-expirable marks if that's required.
5843       (when (gnus-group-auto-expirable-p group)
5844         (gnus-add-marked-articles
5845          group 'expire (gnus-list-of-unread-articles group))
5846         (when all
5847           (let ((marks (nth 3 (nth 2 entry))))
5848             (gnus-add-marked-articles
5849              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5850             (gnus-add-marked-articles
5851              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5852       (when entry
5853         (gnus-update-read-articles group nil)
5854         ;; Also nix out the lists of marks and dormants.
5855         (when all
5856           (gnus-add-marked-articles group 'tick nil nil 'force)
5857           (gnus-add-marked-articles group 'dormant nil nil 'force))
5858         (run-hooks 'gnus-group-catchup-group-hook)
5859         num))))
5860
5861 (defun gnus-group-expire-articles (&optional n)
5862   "Expire all expirable articles in the current newsgroup."
5863   (interactive "P")
5864   (let ((groups (gnus-group-process-prefix n))
5865         group)
5866     (unless groups
5867       (error "No groups to expire"))
5868     (while (setq group (pop groups))
5869       (gnus-group-remove-mark group)
5870       (when (gnus-check-backend-function 'request-expire-articles group)
5871         (gnus-message 6 "Expiring articles in %s..." group)
5872         (let* ((info (gnus-get-info group))
5873                (expirable (if (gnus-group-total-expirable-p group)
5874                               (cons nil (gnus-list-of-read-articles group))
5875                             (assq 'expire (gnus-info-marks info))))
5876                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5877           (when expirable
5878             (setcdr
5879              expirable
5880              (gnus-compress-sequence
5881               (if expiry-wait
5882                   ;; We set the expiry variables to the groupp
5883                   ;; parameter. 
5884                   (let ((nnmail-expiry-wait-function nil)
5885                         (nnmail-expiry-wait expiry-wait))
5886                     (gnus-request-expire-articles
5887                      (gnus-uncompress-sequence (cdr expirable)) group))
5888                 ;; Just expire using the normal expiry values.
5889                 (gnus-request-expire-articles
5890                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5891           (gnus-message 6 "Expiring articles in %s...done" group)))
5892       (gnus-group-position-point))))
5893
5894 (defun gnus-group-expire-all-groups ()
5895   "Expire all expirable articles in all newsgroups."
5896   (interactive)
5897   (save-excursion
5898     (gnus-message 5 "Expiring...")
5899     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5900                                      (cdr gnus-newsrc-alist))))
5901       (gnus-group-expire-articles nil)))
5902   (gnus-group-position-point)
5903   (gnus-message 5 "Expiring...done"))
5904
5905 (defun gnus-group-set-current-level (n level)
5906   "Set the level of the next N groups to LEVEL."
5907   (interactive
5908    (list
5909     current-prefix-arg
5910     (if (not (gnus-group-group-name))
5911         (error "No group on the current line")
5912       (string-to-int
5913        (let ((s (read-string
5914                  (format "Level (default %s): " (gnus-group-group-level)))))
5915          (if (string-match "^\\s-*$" s)
5916              (int-to-string (gnus-group-group-level))
5917            s))))))
5918   (or (and (>= level 1) (<= level gnus-level-killed))
5919       (error "Illegal level: %d" level))
5920   (let ((groups (gnus-group-process-prefix n))
5921         group)
5922     (while (setq group (pop groups))
5923       (gnus-group-remove-mark group)
5924       (gnus-message 6 "Changed level of %s from %d to %d"
5925                     group (or (gnus-group-group-level) gnus-level-killed)
5926                     level)
5927       (gnus-group-change-level
5928        group level (or (gnus-group-group-level) gnus-level-killed))
5929       (gnus-group-update-group-line)))
5930   (gnus-group-position-point))
5931
5932 (defun gnus-group-unsubscribe-current-group (&optional n)
5933   "Toggle subscription of the current group.
5934 If given numerical prefix, toggle the N next groups."
5935   (interactive "P")
5936   (let ((groups (gnus-group-process-prefix n))
5937         group)
5938     (while groups
5939       (setq group (car groups)
5940             groups (cdr groups))
5941       (gnus-group-remove-mark group)
5942       (gnus-group-unsubscribe-group
5943        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5944                  gnus-level-default-unsubscribed
5945                gnus-level-default-subscribed) t)
5946       (gnus-group-update-group-line))
5947     (gnus-group-next-group 1)))
5948
5949 (defun gnus-group-unsubscribe-group (group &optional level silent)
5950   "Toggle subscription to GROUP.
5951 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5952 group line."
5953   (interactive
5954    (list (completing-read
5955           "Group: " gnus-active-hashtb nil
5956           (memq gnus-select-method gnus-have-read-active-file))))
5957   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5958     (cond
5959      ((string-match "^[ \t]$" group)
5960       (error "Empty group name"))
5961      (newsrc
5962       ;; Toggle subscription flag.
5963       (gnus-group-change-level
5964        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
5965                                       gnus-level-subscribed)
5966                                   (1+ gnus-level-subscribed)
5967                                 gnus-level-default-subscribed)))
5968       (unless silent
5969         (gnus-group-update-group group)))
5970      ((and (stringp group)
5971            (or (not (memq gnus-select-method gnus-have-read-active-file))
5972                (gnus-active group)))
5973       ;; Add new newsgroup.
5974       (gnus-group-change-level
5975        group
5976        (if level level gnus-level-default-subscribed)
5977        (or (and (member group gnus-zombie-list)
5978                 gnus-level-zombie)
5979            gnus-level-killed)
5980        (and (gnus-group-group-name)
5981             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5982       (unless silent
5983         (gnus-group-update-group group)))
5984      (t (error "No such newsgroup: %s" group)))
5985     (gnus-group-position-point)))
5986
5987 (defun gnus-group-transpose-groups (n)
5988   "Move the current newsgroup up N places.
5989 If given a negative prefix, move down instead.  The difference between
5990 N and the number of steps taken is returned."
5991   (interactive "p")
5992   (or (gnus-group-group-name)
5993       (error "No group on current line"))
5994   (gnus-group-kill-group 1)
5995   (prog1
5996       (forward-line (- n))
5997     (gnus-group-yank-group)
5998     (gnus-group-position-point)))
5999
6000 (defun gnus-group-kill-all-zombies ()
6001   "Kill all zombie newsgroups."
6002   (interactive)
6003   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6004   (setq gnus-zombie-list nil)
6005   (gnus-group-list-groups))
6006
6007 (defun gnus-group-kill-region (begin end)
6008   "Kill newsgroups in current region (excluding current point).
6009 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6010   (interactive "r")
6011   (let ((lines
6012          ;; Count lines.
6013          (save-excursion
6014            (count-lines
6015             (progn
6016               (goto-char begin)
6017               (beginning-of-line)
6018               (point))
6019             (progn
6020               (goto-char end)
6021               (beginning-of-line)
6022               (point))))))
6023     (goto-char begin)
6024     (beginning-of-line)                 ;Important when LINES < 1
6025     (gnus-group-kill-group lines)))
6026
6027 (defun gnus-group-kill-group (&optional n discard)
6028   "Kill the next N groups.
6029 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6030 However, only groups that were alive can be yanked; already killed
6031 groups or zombie groups can't be yanked.
6032 The return value is the name of the group that was killed, or a list
6033 of groups killed."
6034   (interactive "P")
6035   (let ((buffer-read-only nil)
6036         (groups (gnus-group-process-prefix n))
6037         group entry level out)
6038     (if (< (length groups) 10)
6039         ;; This is faster when there are few groups.
6040         (while groups
6041           (push (setq group (pop groups)) out)
6042           (gnus-group-remove-mark group)
6043           (setq level (gnus-group-group-level))
6044           (gnus-delete-line)
6045           (if (and (not discard)
6046                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6047               (setq gnus-list-of-killed-groups
6048                     (cons (cons (car entry) (nth 2 entry))
6049                           gnus-list-of-killed-groups)))
6050           (gnus-group-change-level
6051            (if entry entry group) gnus-level-killed (if entry nil level)))
6052       ;; If there are lots and lots of groups to be killed, we use
6053       ;; this thing instead.
6054       (let (entry)
6055         (setq groups (nreverse groups))
6056         (while groups
6057           (gnus-group-remove-mark (setq group (pop groups)))
6058           (gnus-delete-line)
6059           (cond
6060            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6061             (push (cons (car entry) (nth 2 entry))
6062                   gnus-list-of-killed-groups)
6063             (setcdr (cdr entry) (cdr (cdr (cdr entry)))))
6064            ((member group gnus-zombie-list)
6065             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6066         (gnus-make-hashtable-from-newsrc-alist)))
6067
6068     (gnus-group-position-point)
6069     (if (< (length out) 2) (car out) (nreverse out))))
6070
6071 (defun gnus-group-yank-group (&optional arg)
6072   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6073 inserting it before the current newsgroup.  The numeric ARG specifies
6074 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6075 is returned, or (if several groups are yanked) a list of yanked groups
6076 is returned."
6077   (interactive "p")
6078   (setq arg (or arg 1))
6079   (let (info group prev out)
6080     (while (>= (decf arg) 0)
6081       (if (not (setq info (pop gnus-list-of-killed-groups)))
6082           (error "No more newsgroups to yank"))
6083       (push (setq group (nth 1 info)) out)
6084       ;; Find which newsgroup to insert this one before - search
6085       ;; backward until something suitable is found.  If there are no
6086       ;; other newsgroups in this buffer, just make this newsgroup the
6087       ;; first newsgroup.
6088       (setq prev (gnus-group-group-name))
6089       (gnus-group-change-level
6090        info (nth 2 info) gnus-level-killed
6091        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6092        t)
6093       (gnus-group-insert-group-line-info group))
6094     (forward-line -1)
6095     (gnus-group-position-point)
6096     (if (< (length out) 2) (car out) (nreverse out))))
6097
6098 (defun gnus-group-kill-level (level)
6099   "Kill all groups that is on a certain LEVEL."
6100   (interactive "nKill all groups on level: ")
6101   (cond
6102    ((= level gnus-level-zombie)
6103     (setq gnus-killed-list
6104           (nconc gnus-zombie-list gnus-killed-list))
6105     (setq gnus-zombie-list nil))
6106    ((and (< level gnus-level-zombie)
6107          (> level 0)
6108          (or gnus-expert-user
6109              (gnus-yes-or-no-p
6110               (format
6111                "Do you really want to kill all groups on level %d? "
6112                level))))
6113     (let* ((prev gnus-newsrc-alist)
6114            (alist (cdr prev)))
6115       (while alist
6116         (if (= (gnus-info-level level) level)
6117             (setcdr prev (cdr alist))
6118           (setq prev alist))
6119         (setq alist (cdr alist)))
6120       (gnus-make-hashtable-from-newsrc-alist)
6121       (gnus-group-list-groups)))
6122    (t
6123     (error "Can't kill; illegal level: %d" level))))
6124
6125 (defun gnus-group-list-all-groups (&optional arg)
6126   "List all newsgroups with level ARG or lower.
6127 Default is gnus-level-unsubscribed, which lists all subscribed and most
6128 unsubscribed groups."
6129   (interactive "P")
6130   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6131
6132 ;; Redefine this to list ALL killed groups if prefix arg used.
6133 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6134 (defun gnus-group-list-killed (&optional arg)
6135   "List all killed newsgroups in the group buffer.
6136 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6137 entail asking the server for the groups."
6138   (interactive "P")
6139   ;; Find all possible killed newsgroups if arg.
6140   (when arg
6141     ;; First make sure active file has been read.
6142     (unless gnus-have-read-active-file
6143       (let ((gnus-read-active-file t))
6144         (gnus-read-active-file)))
6145     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6146     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6147     (mapatoms
6148      (lambda (sym)
6149        (let ((groups 0)
6150              (group (symbol-name sym)))
6151          (if (or (null group)
6152                  (gnus-gethash group gnus-killed-hashtb)
6153                  (gnus-gethash group gnus-newsrc-hashtb))
6154              ()
6155            (let ((do-sub (gnus-matches-options-n group)))
6156              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6157                  ()
6158                (setq groups (1+ groups))
6159                (setq gnus-killed-list
6160                      (cons group gnus-killed-list))
6161                (gnus-sethash group group gnus-killed-hashtb))))))
6162      gnus-active-hashtb))
6163   (if (not gnus-killed-list)
6164       (gnus-message 6 "No killed groups")
6165     (let (gnus-group-list-mode)
6166       (funcall gnus-group-prepare-function
6167                gnus-level-killed t gnus-level-killed))
6168     (goto-char (point-min)))
6169   (gnus-group-position-point))
6170
6171 (defun gnus-group-list-zombies ()
6172   "List all zombie newsgroups in the group buffer."
6173   (interactive)
6174   (if (not gnus-zombie-list)
6175       (gnus-message 6 "No zombie groups")
6176     (let (gnus-group-list-mode)
6177       (funcall gnus-group-prepare-function
6178                gnus-level-zombie t gnus-level-zombie))
6179     (goto-char (point-min)))
6180   (gnus-group-position-point))
6181
6182 (defun gnus-group-list-active ()
6183   "List all groups that are available from the server(s)."
6184   (interactive)
6185   ;; First we make sure that we have really read the active file.
6186   (unless gnus-have-read-active-file
6187     (let ((gnus-read-active-file t))
6188       (gnus-read-active-file)))
6189   ;; Find all groups and sort them.
6190   (let ((groups
6191          (sort
6192           (let (list)
6193             (mapatoms
6194              (lambda (sym)
6195                (and (symbol-value sym)
6196                     (setq list (cons (symbol-name sym) list))))
6197              gnus-active-hashtb)
6198             list)
6199           'string<))
6200         (buffer-read-only nil))
6201     (erase-buffer)
6202     (while groups
6203       (gnus-group-insert-group-line-info (car groups))
6204       (setq groups (cdr groups)))
6205     (goto-char (point-min))))
6206
6207 (defun gnus-activate-all-groups (level)
6208   "Activate absolutely all groups."
6209   (interactive (list 7))
6210   (let ((gnus-activate-level level)
6211         (gnus-activate-foreign-newsgroups level))
6212     (gnus-group-get-new-news)))
6213
6214 (defun gnus-group-get-new-news (&optional arg)
6215   "Get newly arrived articles.
6216 If ARG is a number, it specifies which levels you are interested in
6217 re-scanning.  If ARG is non-nil and not a number, this will force
6218 \"hard\" re-reading of the active files from all servers."
6219   (interactive "P")
6220   (run-hooks 'gnus-get-new-news-hook)
6221   ;; We might read in new NoCeM messages here.
6222   (when gnus-use-nocem 
6223     (gnus-nocem-scan-groups))
6224   ;; If ARG is not a number, then we read the active file.
6225   (when (and arg (not (numberp arg)))
6226     (let ((gnus-read-active-file t))
6227       (gnus-read-active-file))
6228     (setq arg nil))
6229
6230   (setq arg (gnus-group-default-level arg t))
6231   (if (and gnus-read-active-file (not arg))
6232       (progn
6233         (gnus-read-active-file)
6234         (gnus-get-unread-articles arg))
6235     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6236       (gnus-get-unread-articles arg)))
6237   (gnus-group-list-groups))
6238
6239 (defun gnus-group-get-new-news-this-group (&optional n)
6240   "Check for newly arrived news in the current group (and the N-1 next groups).
6241 The difference between N and the number of newsgroup checked is returned.
6242 If N is negative, this group and the N-1 previous groups will be checked."
6243   (interactive "P")
6244   (let* ((groups (gnus-group-process-prefix n))
6245          (ret (if (numberp n) (- n (length groups)) 0))
6246          group)
6247     (while groups
6248       (setq group (car groups)
6249             groups (cdr groups))
6250       (gnus-group-remove-mark group)
6251       (unless (gnus-get-new-news-in-group group)
6252         (ding)
6253         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6254     (when gnus-goto-next-group-when-activating
6255       (gnus-group-next-unread-group 1 t))
6256     (gnus-summary-position-point)
6257     ret))
6258
6259 (defun gnus-get-new-news-in-group (group)
6260   (when (and group (gnus-activate-group group 'scan))
6261     (gnus-get-unread-articles-in-group
6262      (gnus-get-info group) (gnus-active group) t)
6263     (when (gnus-group-goto-group group)
6264       (gnus-group-update-group-line))
6265     t))
6266
6267 (defun gnus-group-fetch-faq (group &optional faq-dir)
6268   "Fetch the FAQ for the current group."
6269   (interactive
6270    (list
6271     (gnus-group-real-name (gnus-group-group-name))
6272     (cond (current-prefix-arg
6273            (completing-read
6274             "Faq dir: " (and (listp gnus-group-faq-directory)
6275                              gnus-group-faq-directory))))))
6276   (or faq-dir
6277       (setq faq-dir (if (listp gnus-group-faq-directory)
6278                         (car gnus-group-faq-directory)
6279                       gnus-group-faq-directory)))
6280   (or group (error "No group name given"))
6281   (let ((file (concat (file-name-as-directory faq-dir)
6282                       (gnus-group-real-name group))))
6283     (if (not (file-exists-p file))
6284         (error "No such file: %s" file)
6285       (find-file file))))
6286
6287 (defun gnus-group-describe-group (force &optional group)
6288   "Display a description of the current newsgroup."
6289   (interactive (list current-prefix-arg (gnus-group-group-name)))
6290   (and force (setq gnus-description-hashtb nil))
6291   (let ((method (gnus-find-method-for-group group))
6292         desc)
6293     (or group (error "No group name given"))
6294     (and (or (and gnus-description-hashtb
6295                   ;; We check whether this group's method has been
6296                   ;; queried for a description file.
6297                   (gnus-gethash
6298                    (gnus-group-prefixed-name "" method)
6299                    gnus-description-hashtb))
6300              (setq desc (gnus-group-get-description group))
6301              (gnus-read-descriptions-file method))
6302          (message
6303           (or desc (gnus-gethash group gnus-description-hashtb)
6304               "No description available")))))
6305
6306 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6307 (defun gnus-group-describe-all-groups (&optional force)
6308   "Pop up a buffer with descriptions of all newsgroups."
6309   (interactive "P")
6310   (and force (setq gnus-description-hashtb nil))
6311   (if (not (or gnus-description-hashtb
6312                (gnus-read-all-descriptions-files)))
6313       (error "Couldn't request descriptions file"))
6314   (let ((buffer-read-only nil)
6315         b)
6316     (erase-buffer)
6317     (mapatoms
6318      (lambda (group)
6319        (setq b (point))
6320        (insert (format "      *: %-20s %s\n" (symbol-name group)
6321                        (symbol-value group)))
6322        (add-text-properties
6323         b (1+ b) (list 'gnus-group group
6324                        'gnus-unread t 'gnus-marked nil
6325                        'gnus-level (1+ gnus-level-subscribed))))
6326      gnus-description-hashtb)
6327     (goto-char (point-min))
6328     (gnus-group-position-point)))
6329
6330 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6331 (defun gnus-group-apropos (regexp &optional search-description)
6332   "List all newsgroups that have names that match a regexp."
6333   (interactive "sGnus apropos (regexp): ")
6334   (let ((prev "")
6335         (obuf (current-buffer))
6336         groups des)
6337     ;; Go through all newsgroups that are known to Gnus.
6338     (mapatoms
6339      (lambda (group)
6340        (and (symbol-name group)
6341             (string-match regexp (symbol-name group))
6342             (setq groups (cons (symbol-name group) groups))))
6343      gnus-active-hashtb)
6344     ;; Go through all descriptions that are known to Gnus.
6345     (if search-description
6346         (mapatoms
6347          (lambda (group)
6348            (and (string-match regexp (symbol-value group))
6349                 (gnus-active (symbol-name group))
6350                 (setq groups (cons (symbol-name group) groups))))
6351          gnus-description-hashtb))
6352     (if (not groups)
6353         (gnus-message 3 "No groups matched \"%s\"." regexp)
6354       ;; Print out all the groups.
6355       (save-excursion
6356         (pop-to-buffer "*Gnus Help*")
6357         (buffer-disable-undo (current-buffer))
6358         (erase-buffer)
6359         (setq groups (sort groups 'string<))
6360         (while groups
6361           ;; Groups may be entered twice into the list of groups.
6362           (if (not (string= (car groups) prev))
6363               (progn
6364                 (insert (setq prev (car groups)) "\n")
6365                 (if (and gnus-description-hashtb
6366                          (setq des (gnus-gethash (car groups)
6367                                                  gnus-description-hashtb)))
6368                     (insert "  " des "\n"))))
6369           (setq groups (cdr groups)))
6370         (goto-char (point-min))))
6371     (pop-to-buffer obuf)))
6372
6373 (defun gnus-group-description-apropos (regexp)
6374   "List all newsgroups that have names or descriptions that match a regexp."
6375   (interactive "sGnus description apropos (regexp): ")
6376   (if (not (or gnus-description-hashtb
6377                (gnus-read-all-descriptions-files)))
6378       (error "Couldn't request descriptions file"))
6379   (gnus-group-apropos regexp t))
6380
6381 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6382 (defun gnus-group-list-matching (level regexp &optional all lowest)
6383   "List all groups with unread articles that match REGEXP.
6384 If the prefix LEVEL is non-nil, it should be a number that says which
6385 level to cut off listing groups.
6386 If ALL, also list groups with no unread articles.
6387 If LOWEST, don't list groups with level lower than LOWEST."
6388   (interactive "P\nsList newsgroups matching: ")
6389   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6390                            all (or lowest 1) regexp)
6391   (goto-char (point-min))
6392   (gnus-group-position-point))
6393
6394 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6395   "List all groups that match REGEXP.
6396 If the prefix LEVEL is non-nil, it should be a number that says which
6397 level to cut off listing groups.
6398 If LOWEST, don't list groups with level lower than LOWEST."
6399   (interactive "P\nsList newsgroups matching: ")
6400   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6401
6402 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6403 (defun gnus-group-save-newsrc (&optional force)
6404   "Save the Gnus startup files.
6405 If FORCE, force saving whether it is necessary or not."
6406   (interactive "P")
6407   (gnus-save-newsrc-file force))
6408
6409 (defun gnus-group-restart (&optional arg)
6410   "Force Gnus to read the .newsrc file."
6411   (interactive "P")
6412   (gnus-save-newsrc-file)
6413   (gnus-setup-news 'force)
6414   (gnus-group-list-groups arg))
6415
6416 (defun gnus-group-read-init-file ()
6417   "Read the Gnus elisp init file."
6418   (interactive)
6419   (gnus-read-init-file))
6420
6421 (defun gnus-group-check-bogus-groups (&optional silent)
6422   "Check bogus newsgroups.
6423 If given a prefix, don't ask for confirmation before removing a bogus
6424 group."
6425   (interactive "P")
6426   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6427   (gnus-group-list-groups))
6428
6429 (defun gnus-group-edit-global-kill (&optional article group)
6430   "Edit the global kill file.
6431 If GROUP, edit that local kill file instead."
6432   (interactive "P")
6433   (setq gnus-current-kill-article article)
6434   (gnus-kill-file-edit-file group)
6435   (gnus-message
6436    6
6437    (substitute-command-keys
6438     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6439             (if group "local" "global")))))
6440
6441 (defun gnus-group-edit-local-kill (article group)
6442   "Edit a local kill file."
6443   (interactive (list nil (gnus-group-group-name)))
6444   (gnus-group-edit-global-kill article group))
6445
6446 (defun gnus-group-force-update ()
6447   "Update `.newsrc' file."
6448   (interactive)
6449   (gnus-save-newsrc-file))
6450
6451 (defun gnus-group-suspend ()
6452   "Suspend the current Gnus session.
6453 In fact, cleanup buffers except for group mode buffer.
6454 The hook gnus-suspend-gnus-hook is called before actually suspending."
6455   (interactive)
6456   (run-hooks 'gnus-suspend-gnus-hook)
6457   ;; Kill Gnus buffers except for group mode buffer.
6458   (let ((group-buf (get-buffer gnus-group-buffer)))
6459     ;; Do this on a separate list in case the user does a ^G before we finish
6460     (let ((gnus-buffer-list
6461            (delq group-buf (delq gnus-dribble-buffer
6462                                  (append gnus-buffer-list nil)))))
6463       (while gnus-buffer-list
6464         (gnus-kill-buffer (car gnus-buffer-list))
6465         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6466     (if group-buf
6467         (progn
6468           (setq gnus-buffer-list (list group-buf))
6469           (bury-buffer group-buf)
6470           (delete-windows-on group-buf t)))))
6471
6472 (defun gnus-group-clear-dribble ()
6473   "Clear all information from the dribble buffer."
6474   (interactive)
6475   (gnus-dribble-clear)
6476   (gnus-message 7 "Cleared dribble buffer"))
6477
6478 (defun gnus-group-exit ()
6479   "Quit reading news after updating .newsrc.eld and .newsrc.
6480 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6481   (interactive)
6482   (when 
6483       (or noninteractive                ;For gnus-batch-kill
6484           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6485           (not gnus-interactive-exit)   ;Without confirmation
6486           gnus-expert-user
6487           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6488     (run-hooks 'gnus-exit-gnus-hook)
6489     ;; Close down GroupLens.
6490     (when gnus-use-grouplens
6491       (bbb-logout))
6492     ;; Offer to save data from non-quitted summary buffers.
6493     (gnus-offer-save-summaries)
6494     ;; Save the newsrc file(s).
6495     (gnus-save-newsrc-file)
6496     ;; Kill-em-all.
6497     (gnus-close-backends)
6498     ;; Shut down the cache.
6499     (when gnus-use-cache
6500       (gnus-cache-close))
6501     ;; Reset everything.
6502     (gnus-clear-system)
6503     ;; Allow the user to do things after cleaning up.
6504     (run-hooks 'gnus-after-exiting-gnus-hook)))
6505
6506 (defun gnus-close-backends ()
6507   ;; Send a close request to all backends that support such a request.
6508   (let ((methods gnus-valid-select-methods)
6509         func)
6510     (while methods
6511       (if (fboundp (setq func (intern (concat (car (car methods))
6512                                               "-request-close"))))
6513           (funcall func))
6514       (setq methods (cdr methods)))))
6515
6516 (defun gnus-group-quit ()
6517   "Quit reading news without updating .newsrc.eld or .newsrc.
6518 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6519   (interactive)
6520   (when (or noninteractive              ;For gnus-batch-kill
6521             (zerop (buffer-size))
6522             (not (gnus-server-opened gnus-select-method))
6523             gnus-expert-user
6524             (not gnus-current-startup-file)
6525             (gnus-yes-or-no-p
6526              (format "Quit reading news without saving %s? "
6527                      (file-name-nondirectory gnus-current-startup-file))))
6528     (run-hooks 'gnus-exit-gnus-hook)
6529     (if gnus-use-full-window
6530         (delete-other-windows)
6531       (gnus-remove-some-windows))
6532     (gnus-dribble-save)
6533     (gnus-close-backends)
6534     ;; Shut down the cache.
6535     (when gnus-use-cache
6536       (gnus-cache-close))
6537     (gnus-clear-system)
6538     ;; Allow the user to do things after cleaning up.
6539     (run-hooks 'gnus-after-exiting-gnus-hook)))
6540
6541 (defun gnus-offer-save-summaries ()
6542   "Offer to save all active summary buffers."
6543   (save-excursion
6544     (let ((buflist (buffer-list))
6545           buffers bufname)
6546       ;; Go through all buffers and find all summaries.
6547       (while buflist
6548         (and (setq bufname (buffer-name (car buflist)))
6549              (string-match "Summary" bufname)
6550              (save-excursion
6551                (set-buffer bufname)
6552                ;; We check that this is, indeed, a summary buffer.
6553                (and (eq major-mode 'gnus-summary-mode)
6554                     ;; Also make sure this isn't bogus.
6555                     gnus-newsgroup-prepared))
6556              (push bufname buffers))
6557         (setq buflist (cdr buflist)))
6558       ;; Go through all these summary buffers and offer to save them.
6559       (when buffers
6560         (map-y-or-n-p
6561          "Update summary buffer %s? "
6562          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6563          buffers)))))
6564
6565 (defun gnus-group-describe-briefly ()
6566   "Give a one line description of the group mode commands."
6567   (interactive)
6568   (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")))
6569
6570 (defun gnus-group-browse-foreign-server (method)
6571   "Browse a foreign news server.
6572 If called interactively, this function will ask for a select method
6573  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6574 If not, METHOD should be a list where the first element is the method
6575 and the second element is the address."
6576   (interactive
6577    (list (let ((how (completing-read
6578                      "Which backend: "
6579                      (append gnus-valid-select-methods gnus-server-alist)
6580                      nil t (cons "nntp" 0))))
6581            ;; We either got a backend name or a virtual server name.
6582            ;; If the first, we also need an address.
6583            (if (assoc how gnus-valid-select-methods)
6584                (list (intern how)
6585                      ;; Suggested by mapjph@bath.ac.uk.
6586                      (completing-read
6587                       "Address: "
6588                       (mapcar (lambda (server) (list server))
6589                               gnus-secondary-servers)))
6590              ;; We got a server name, so we find the method.
6591              (gnus-server-to-method how)))))
6592   (gnus-browse-foreign-server method))
6593
6594 \f
6595 ;;;
6596 ;;; Gnus summary mode
6597 ;;;
6598
6599 (defvar gnus-summary-mode-map nil)
6600
6601 (put 'gnus-summary-mode 'mode-class 'special)
6602
6603 (unless gnus-summary-mode-map
6604   (setq gnus-summary-mode-map (make-keymap))
6605   (suppress-keymap gnus-summary-mode-map)
6606
6607   ;; Non-orthogonal keys
6608
6609   (gnus-define-keys gnus-summary-mode-map
6610     " " gnus-summary-next-page
6611     "\177" gnus-summary-prev-page
6612     [delete] gnus-summary-prev-page
6613     "\r" gnus-summary-scroll-up
6614     "n" gnus-summary-next-unread-article
6615     "p" gnus-summary-prev-unread-article
6616     "N" gnus-summary-next-article
6617     "P" gnus-summary-prev-article
6618     "\M-\C-n" gnus-summary-next-same-subject
6619     "\M-\C-p" gnus-summary-prev-same-subject
6620     "\M-n" gnus-summary-next-unread-subject
6621     "\M-p" gnus-summary-prev-unread-subject
6622     "." gnus-summary-first-unread-article
6623     "," gnus-summary-best-unread-article
6624     "\M-s" gnus-summary-search-article-forward
6625     "\M-r" gnus-summary-search-article-backward
6626     "<" gnus-summary-beginning-of-article
6627     ">" gnus-summary-end-of-article
6628     "j" gnus-summary-goto-article
6629     "^" gnus-summary-refer-parent-article
6630     "\M-^" gnus-summary-refer-article
6631     "u" gnus-summary-tick-article-forward
6632     "!" gnus-summary-tick-article-forward
6633     "U" gnus-summary-tick-article-backward
6634     "d" gnus-summary-mark-as-read-forward
6635     "D" gnus-summary-mark-as-read-backward
6636     "E" gnus-summary-mark-as-expirable
6637     "\M-u" gnus-summary-clear-mark-forward
6638     "\M-U" gnus-summary-clear-mark-backward
6639     "k" gnus-summary-kill-same-subject-and-select
6640     "\C-k" gnus-summary-kill-same-subject
6641     "\M-\C-k" gnus-summary-kill-thread
6642     "\M-\C-l" gnus-summary-lower-thread
6643     "e" gnus-summary-edit-article
6644     "#" gnus-summary-mark-as-processable
6645     "\M-#" gnus-summary-unmark-as-processable
6646     "\M-\C-t" gnus-summary-toggle-threads
6647     "\M-\C-s" gnus-summary-show-thread
6648     "\M-\C-h" gnus-summary-hide-thread
6649     "\M-\C-f" gnus-summary-next-thread
6650     "\M-\C-b" gnus-summary-prev-thread
6651     "\M-\C-u" gnus-summary-up-thread
6652     "\M-\C-d" gnus-summary-down-thread
6653     "&" gnus-summary-execute-command
6654     "c" gnus-summary-catchup-and-exit
6655     "\C-w" gnus-summary-mark-region-as-read
6656     "\C-t" gnus-summary-toggle-truncation
6657     "?" gnus-summary-mark-as-dormant
6658     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6659     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6660     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6661     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6662     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6663     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6664     "=" gnus-summary-expand-window
6665     "\C-x\C-s" gnus-summary-reselect-current-group
6666     "\M-g" gnus-summary-rescan-group
6667     "w" gnus-summary-stop-page-breaking
6668     "\C-c\C-r" gnus-summary-caesar-message
6669     "\M-t" gnus-summary-toggle-mime
6670     "f" gnus-summary-followup
6671     "F" gnus-summary-followup-with-original
6672     "C" gnus-summary-cancel-article
6673     "r" gnus-summary-reply
6674     "R" gnus-summary-reply-with-original
6675     "\C-c\C-f" gnus-summary-mail-forward
6676     "o" gnus-summary-save-article
6677     "\C-o" gnus-summary-save-article-mail
6678     "|" gnus-summary-pipe-output
6679     "\M-k" gnus-summary-edit-local-kill
6680     "\M-K" gnus-summary-edit-global-kill
6681     "V" gnus-version
6682     "\C-c\C-d" gnus-summary-describe-group
6683     "q" gnus-summary-exit
6684     "Q" gnus-summary-exit-no-update
6685     "\C-c\C-i" gnus-info-find-node
6686     gnus-mouse-2 gnus-mouse-pick-article
6687     "m" gnus-summary-mail-other-window
6688     "a" gnus-summary-post-news
6689     "x" gnus-summary-limit-to-unread
6690     "s" gnus-summary-isearch-article
6691     "t" gnus-article-hide-headers
6692     "g" gnus-summary-show-article
6693     "l" gnus-summary-goto-last-article
6694     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6695     "\C-d" gnus-summary-enter-digest-group
6696     "\C-c\C-b" gnus-bug
6697     "*" gnus-cache-enter-article
6698     "\M-*" gnus-cache-remove-article
6699     "\M-&" gnus-summary-universal-argument
6700     "\C-l" gnus-recenter
6701     "I" gnus-summary-increase-score
6702     "L" gnus-summary-lower-score
6703
6704     "V" gnus-summary-score-map
6705     "X" gnus-uu-extract-map
6706     "S" gnus-summary-send-map)
6707
6708   ;; Sort of orthogonal keymap
6709   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6710     "t" gnus-summary-tick-article-forward
6711     "!" gnus-summary-tick-article-forward
6712     "d" gnus-summary-mark-as-read-forward
6713     "r" gnus-summary-mark-as-read-forward
6714     "c" gnus-summary-clear-mark-forward
6715     " " gnus-summary-clear-mark-forward
6716     "e" gnus-summary-mark-as-expirable
6717     "x" gnus-summary-mark-as-expirable
6718     "?" gnus-summary-mark-as-dormant
6719     "b" gnus-summary-set-bookmark
6720     "B" gnus-summary-remove-bookmark
6721     "#" gnus-summary-mark-as-processable
6722     "\M-#" gnus-summary-unmark-as-processable
6723     "S" gnus-summary-limit-include-expunged
6724     "C" gnus-summary-catchup
6725     "H" gnus-summary-catchup-to-here
6726     "\C-c" gnus-summary-catchup-all
6727     "k" gnus-summary-kill-same-subject-and-select
6728     "K" gnus-summary-kill-same-subject
6729     "P" gnus-uu-mark-map)
6730
6731   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6732     "c" gnus-summary-clear-above
6733     "u" gnus-summary-tick-above
6734     "m" gnus-summary-mark-above
6735     "k" gnus-summary-kill-below)
6736
6737   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6738     "/" gnus-summary-limit-to-subject
6739     "n" gnus-summary-limit-to-articles
6740     "w" gnus-summary-pop-limit
6741     "s" gnus-summary-limit-to-subject
6742     "a" gnus-summary-limit-to-author
6743     "u" gnus-summary-limit-to-unread
6744     "m" gnus-summary-limit-to-marks
6745     "v" gnus-summary-limit-to-score
6746     "D" gnus-summary-limit-include-dormant
6747     "d" gnus-summary-limit-exclude-dormant
6748     ;;  "t" gnus-summary-limit-exclude-thread
6749     "E" gnus-summary-limit-include-expunged
6750     "c" gnus-summary-limit-exclude-childless-dormant
6751     "C" gnus-summary-limit-mark-excluded-as-read)
6752
6753   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6754     "n" gnus-summary-next-unread-article
6755     "p" gnus-summary-prev-unread-article
6756     "N" gnus-summary-next-article
6757     "P" gnus-summary-prev-article
6758     "\C-n" gnus-summary-next-same-subject
6759     "\C-p" gnus-summary-prev-same-subject
6760     "\M-n" gnus-summary-next-unread-subject
6761     "\M-p" gnus-summary-prev-unread-subject
6762     "f" gnus-summary-first-unread-article
6763     "b" gnus-summary-best-unread-article
6764     "g" gnus-summary-goto-subject
6765     "l" gnus-summary-goto-last-article
6766     "p" gnus-summary-pop-article)
6767
6768   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6769     "k" gnus-summary-kill-thread
6770     "l" gnus-summary-lower-thread
6771     "i" gnus-summary-raise-thread
6772     "T" gnus-summary-toggle-threads
6773     "t" gnus-summary-rethread-current
6774     "^" gnus-summary-reparent-thread
6775     "s" gnus-summary-show-thread
6776     "S" gnus-summary-show-all-threads
6777     "h" gnus-summary-hide-thread
6778     "H" gnus-summary-hide-all-threads
6779     "n" gnus-summary-next-thread
6780     "p" gnus-summary-prev-thread
6781     "u" gnus-summary-up-thread
6782     "o" gnus-summary-top-thread
6783     "d" gnus-summary-down-thread
6784     "#" gnus-uu-mark-thread
6785     "\M-#" gnus-uu-unmark-thread)
6786
6787   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6788     "c" gnus-summary-catchup-and-exit
6789     "C" gnus-summary-catchup-all-and-exit
6790     "E" gnus-summary-exit-no-update
6791     "Q" gnus-summary-exit
6792     "Z" gnus-summary-exit
6793     "n" gnus-summary-catchup-and-goto-next-group
6794     "R" gnus-summary-reselect-current-group
6795     "G" gnus-summary-rescan-group
6796     "N" gnus-summary-next-group
6797     "P" gnus-summary-prev-group)
6798
6799   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6800     " " gnus-summary-next-page
6801     "n" gnus-summary-next-page
6802     "\177" gnus-summary-prev-page
6803     [delete] gnus-summary-prev-page
6804     "p" gnus-summary-prev-page
6805     "\r" gnus-summary-scroll-up
6806     "<" gnus-summary-beginning-of-article
6807     ">" gnus-summary-end-of-article
6808     "b" gnus-summary-beginning-of-article
6809     "e" gnus-summary-end-of-article
6810     "^" gnus-summary-refer-parent-article
6811     "r" gnus-summary-refer-parent-article
6812     "R" gnus-summary-refer-references
6813     "g" gnus-summary-show-article
6814     "s" gnus-summary-isearch-article)
6815
6816   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6817     "b" gnus-article-add-buttons
6818     "B" gnus-article-add-buttons-to-head
6819     "o" gnus-article-treat-overstrike
6820     ;;  "w" gnus-article-word-wrap
6821     "w" gnus-article-fill-cited-article
6822     "c" gnus-article-remove-cr
6823     "L" gnus-article-remove-trailing-blank-lines
6824     "q" gnus-article-de-quoted-unreadable
6825     "f" gnus-article-display-x-face
6826     "l" gnus-summary-stop-page-breaking
6827     "r" gnus-summary-caesar-message
6828     "t" gnus-article-hide-headers
6829     "v" gnus-summary-verbose-headers
6830     "m" gnus-summary-toggle-mime)
6831
6832   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6833     "a" gnus-article-hide
6834     "h" gnus-article-hide-headers
6835     "b" gnus-article-hide-boring-headers
6836     "s" gnus-article-hide-signature
6837     "c" gnus-article-hide-citation
6838     "p" gnus-article-hide-pgp
6839     "\C-c" gnus-article-hide-citation-maybe)
6840
6841   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6842     "a" gnus-article-highlight
6843     "h" gnus-article-highlight-headers
6844     "c" gnus-article-highlight-citation
6845     "s" gnus-article-highlight-signature)
6846
6847   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6848     "z" gnus-article-date-ut
6849     "u" gnus-article-date-ut
6850     "l" gnus-article-date-local
6851     "e" gnus-article-date-lapsed
6852     "o" gnus-article-date-original)
6853
6854   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6855     "v" gnus-version
6856     "f" gnus-summary-fetch-faq
6857     "d" gnus-summary-describe-group
6858     "h" gnus-summary-describe-briefly
6859     "i" gnus-info-find-node)
6860
6861   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6862     "e" gnus-summary-expire-articles
6863     "\M-\C-e" gnus-summary-expire-articles-now
6864     "\177" gnus-summary-delete-article
6865     [delete] gnus-summary-delete-article
6866     "m" gnus-summary-move-article
6867     "r" gnus-summary-respool-article
6868     "w" gnus-summary-edit-article
6869     "c" gnus-summary-copy-article
6870     "B" gnus-summary-crosspost-article
6871     "q" gnus-summary-respool-query
6872     "i" gnus-summary-import-article)
6873
6874   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6875     "o" gnus-summary-save-article
6876     "m" gnus-summary-save-article-mail
6877     "r" gnus-summary-save-article-rmail
6878     "f" gnus-summary-save-article-file
6879     "b" gnus-summary-save-article-body-file
6880     "h" gnus-summary-save-article-folder
6881     "v" gnus-summary-save-article-vm
6882     "p" gnus-summary-pipe-output
6883     "s" gnus-soup-add-article)
6884   )
6885
6886 \f
6887
6888 (defun gnus-summary-mode (&optional group)
6889   "Major mode for reading articles.
6890
6891 All normal editing commands are switched off.
6892 \\<gnus-summary-mode-map>
6893 Each line in this buffer represents one article.  To read an
6894 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6895 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6896 respectively.
6897
6898 You can also post articles and send mail from this buffer.  To
6899 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6900 of an article, type `\\[gnus-summary-reply]'.
6901
6902 There are approx. one gazillion commands you can execute in this
6903 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6904
6905 The following commands are available:
6906
6907 \\{gnus-summary-mode-map}"
6908   (interactive)
6909   (when (and menu-bar-mode
6910              (gnus-visual-p 'summary-menu 'menu))
6911     (gnus-summary-make-menu-bar))
6912   (kill-all-local-variables)
6913   (let ((locals gnus-summary-local-variables))
6914     (while locals
6915       (if (consp (car locals))
6916           (progn
6917             (make-local-variable (car (car locals)))
6918             (set (car (car locals)) (eval (cdr (car locals)))))
6919         (make-local-variable (car locals))
6920         (set (car locals) nil))
6921       (setq locals (cdr locals))))
6922   (gnus-make-thread-indent-array)
6923   (gnus-simplify-mode-line)
6924   (setq major-mode 'gnus-summary-mode)
6925   (setq mode-name "Summary")
6926   (make-local-variable 'minor-mode-alist)
6927   (use-local-map gnus-summary-mode-map)
6928   (buffer-disable-undo (current-buffer))
6929   (setq buffer-read-only t)             ;Disable modification
6930   (setq truncate-lines t)
6931   (setq selective-display t)
6932   (setq selective-display-ellipses t)   ;Display `...'
6933   (setq buffer-display-table gnus-summary-display-table)
6934   (setq gnus-newsgroup-name group)
6935   (run-hooks 'gnus-summary-mode-hook))
6936
6937 (defun gnus-summary-make-display-table ()
6938   ;; Change the display table.  Odd characters have a tendency to mess
6939   ;; up nicely formatted displays - we make all possible glyphs
6940   ;; display only a single character.
6941
6942   ;; We start from the standard display table, if any.
6943   (setq gnus-summary-display-table
6944         (or (copy-sequence standard-display-table)
6945             (make-display-table)))
6946   ;; Nix out all the control chars...
6947   (let ((i 32))
6948     (while (>= (setq i (1- i)) 0)
6949       (aset gnus-summary-display-table i [??])))
6950   ;; ... but not newline and cr, of course. (cr is necessary for the
6951   ;; selective display).
6952   (aset gnus-summary-display-table ?\n nil)
6953   (aset gnus-summary-display-table ?\r nil)
6954   ;; We nix out any glyphs over 126 that are not set already.
6955   (let ((i 256))
6956     (while (>= (setq i (1- i)) 127)
6957       ;; Only modify if the entry is nil.
6958       (or (aref gnus-summary-display-table i)
6959           (aset gnus-summary-display-table i [??])))))
6960
6961 (defun gnus-summary-clear-local-variables ()
6962   (let ((locals gnus-summary-local-variables))
6963     (while locals
6964       (if (consp (car locals))
6965           (and (vectorp (car (car locals)))
6966                (set (car (car locals)) nil))
6967         (and (vectorp (car locals))
6968              (set (car locals) nil)))
6969       (setq locals (cdr locals)))))
6970
6971 ;; Summary data functions.
6972
6973 (defmacro gnus-data-number (data)
6974   `(car ,data))
6975
6976 (defmacro gnus-data-set-number (data number)
6977   `(setcar ,data ,number))
6978
6979 (defmacro gnus-data-mark (data)
6980   `(nth 1 ,data))
6981
6982 (defmacro gnus-data-set-mark (data mark)
6983   `(setcar (nthcdr 1 ,data) ,mark))
6984
6985 (defmacro gnus-data-pos (data)
6986   `(nth 2 ,data))
6987
6988 (defmacro gnus-data-set-pos (data pos)
6989   `(setcar (nthcdr 2 ,data) ,pos))
6990
6991 (defmacro gnus-data-header (data)
6992   `(nth 3 ,data))
6993
6994 (defmacro gnus-data-level (data)
6995   `(nth 4 ,data))
6996
6997 (defmacro gnus-data-unread-p (data)
6998   `(= (nth 1 ,data) gnus-unread-mark))
6999
7000 (defmacro gnus-data-pseudo-p (data)
7001   `(consp (nth 3 ,data)))
7002
7003 (defmacro gnus-data-find (number)
7004   `(assq ,number gnus-newsgroup-data))
7005
7006 (defmacro gnus-data-find-list (number &optional data)
7007   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7008      (memq (assq ,number bdata)
7009            bdata)))
7010
7011 (defmacro gnus-data-make (number mark pos header level)
7012   `(list ,number ,mark ,pos ,header ,level))
7013
7014 (defun gnus-data-enter (after-article number mark pos header level offset)
7015   (let ((data (gnus-data-find-list after-article)))
7016     (or data (error "No such article: %d" after-article))
7017     (setcdr data (cons (gnus-data-make number mark pos header level)
7018                        (cdr data)))
7019     (setq gnus-newsgroup-data-reverse nil)
7020     (gnus-data-update-list (cdr (cdr data)) offset)))
7021
7022 (defun gnus-data-enter-list (after-article list &optional offset)
7023   (when list
7024     (let ((data (and after-article (gnus-data-find-list after-article)))
7025           (ilist list))
7026       (or data (not after-article) (error "No such article: %d" after-article))
7027       ;; Find the last element in the list to be spliced into the main
7028       ;; list.
7029       (while (cdr list)
7030         (setq list (cdr list)))
7031       (if (not data)
7032           (progn
7033             (setcdr list gnus-newsgroup-data)
7034             (setq gnus-newsgroup-data ilist)
7035             (and offset (gnus-data-update-list (cdr list) offset)))
7036         (setcdr list (cdr data))
7037         (setcdr data ilist)
7038         (and offset (gnus-data-update-list (cdr data) offset)))
7039       (setq gnus-newsgroup-data-reverse nil))))
7040
7041 (defun gnus-data-remove (article &optional offset)
7042   (let ((data gnus-newsgroup-data))
7043     (if (= (gnus-data-number (car data)) article)
7044         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7045               gnus-newsgroup-data-reverse nil)
7046       (while (cdr data)
7047         (and (= (gnus-data-number (car (cdr data))) article)
7048              (progn
7049                (setcdr data (cdr (cdr data)))
7050                (and offset (gnus-data-update-list (cdr data) offset))
7051                (setq data nil
7052                      gnus-newsgroup-data-reverse nil)))
7053         (setq data (cdr data))))))
7054
7055 (defmacro gnus-data-list (backward)
7056   `(if ,backward
7057        (or gnus-newsgroup-data-reverse
7058            (setq gnus-newsgroup-data-reverse
7059                  (reverse gnus-newsgroup-data)))
7060      gnus-newsgroup-data))
7061
7062 (defun gnus-data-update-list (data offset)
7063   "Add OFFSET to the POS of all data entries in DATA."
7064   (while data
7065     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7066     (setq data (cdr data))))
7067
7068 (defun gnus-data-compute-positions ()
7069   "Compute the positions of all articles."
7070   (let ((data gnus-newsgroup-data)
7071         pos)
7072     (while data
7073       (when (setq pos (text-property-any
7074                        (point-min) (point-max)
7075                        'gnus-number (gnus-data-number (car data))))
7076         (gnus-data-set-pos (car data) (+ pos 3)))
7077       (setq data (cdr data)))))
7078
7079 (defun gnus-summary-article-pseudo-p (article)
7080   "Say whether this article is a pseudo article or not."
7081   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7082
7083 (defun gnus-article-parent-p (number)
7084   "Say whether this article is a parent or not."
7085   (let ((data (gnus-data-find-list number)))
7086     (and (cdr data)                     ; There has to be an article after...
7087          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7088             (gnus-data-level (nth 1 data))))))
7089
7090 (defun gnus-article-children (number)
7091   "Return a list of all children to NUMBER."
7092   (let* ((data (gnus-data-find-list number))
7093          (level (gnus-data-level (car data)))
7094          children)
7095     (setq data (cdr data))
7096     (while (and data            
7097                 (= (gnus-data-level (car data)) (1+ level)))
7098       (push (gnus-data-number (car data)) children)
7099       (setq data (cdr data)))
7100     children))
7101
7102 (defmacro gnus-summary-skip-intangible ()
7103   "If the current article is intangible, then jump to a different article."
7104   '(let ((to (get-text-property (point) 'gnus-intangible)))
7105     (and to (gnus-summary-goto-subject to))))
7106
7107 (defmacro gnus-summary-article-intangible-p ()
7108   "Say whether this article is intangible or not."
7109   '(get-text-property (point) 'gnus-intangible))
7110
7111 ;; Some summary mode macros.
7112
7113 (defmacro gnus-summary-article-number ()
7114   "The article number of the article on the current line.
7115 If there isn's an article number here, then we return the current
7116 article number."
7117   '(progn
7118      (gnus-summary-skip-intangible)
7119      (or (get-text-property (point) 'gnus-number)
7120          (gnus-summary-last-subject))))
7121
7122 (defmacro gnus-summary-article-header (&optional number)
7123   `(gnus-data-header (gnus-data-find
7124                       ,(or number '(gnus-summary-article-number)))))
7125
7126 (defmacro gnus-summary-thread-level (&optional number)
7127   `(if (and (eq gnus-summary-make-false-root 'dummy)
7128             (get-text-property (point) 'gnus-intangible))
7129        0
7130      (gnus-data-level (gnus-data-find
7131                        ,(or number '(gnus-summary-article-number))))))
7132
7133 (defmacro gnus-summary-article-mark (&optional number)
7134   `(gnus-data-mark (gnus-data-find
7135                     ,(or number '(gnus-summary-article-number)))))
7136
7137 (defmacro gnus-summary-article-pos (&optional number)
7138   `(gnus-data-pos (gnus-data-find
7139                    ,(or number '(gnus-summary-article-number)))))
7140
7141 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7142 (defmacro gnus-summary-article-subject (&optional number)
7143   "Return current subject string or nil if nothing."
7144   `(let ((headers
7145           ,(if number
7146                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7147              '(gnus-data-header (assq (gnus-summary-article-number)
7148                                       gnus-newsgroup-data)))))
7149      (and headers
7150           (vectorp headers)
7151           (mail-header-subject headers))))
7152
7153 (defmacro gnus-summary-article-score (&optional number)
7154   "Return current article score."
7155   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7156                   gnus-newsgroup-scored))
7157        gnus-summary-default-score 0))
7158
7159 (defun gnus-summary-article-children (&optional number)
7160   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7161          (level (gnus-data-level (car data)))
7162          l children)
7163     (while (and (setq data (cdr data))
7164                 (> (setq l (gnus-data-level (car data))) level))
7165       (and (= (1+ level) l)
7166            (setq children (cons (gnus-data-number (car data))
7167                                 children))))
7168     (nreverse children)))
7169
7170 (defun gnus-summary-article-parent (&optional number)
7171   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7172                                     (gnus-data-list t)))
7173          (level (gnus-data-level (car data)))
7174          l)
7175     (if (zerop level)
7176         () ; This is a root.
7177       ;; We search until we find an article with a level less than
7178       ;; this one.  That function has to be the parent.
7179       (while (and (setq data (cdr data))
7180                   (not (< (gnus-data-level (car data)) level))))
7181       (and data (gnus-data-number (car data))))))
7182
7183 (defun gnus-unread-mark-p (mark)
7184   "Say whether MARK is the unread mark."
7185   (= mark gnus-unread-mark))
7186
7187 (defun gnus-read-mark-p (mark)
7188   "Say whether MARK is one of the marks that mark as read.
7189 This is all marks except unread, ticked, dormant, and expirable."
7190   (not (or (= mark gnus-unread-mark)
7191            (= mark gnus-ticked-mark)
7192            (= mark gnus-dormant-mark)
7193            (= mark gnus-expirable-mark))))
7194
7195 ;; Various summary mode internalish functions.
7196
7197 (defun gnus-mouse-pick-article (e)
7198   (interactive "e")
7199   (mouse-set-point e)
7200   (gnus-summary-next-page nil t))
7201
7202 (defun gnus-summary-setup-buffer (group)
7203   "Initialize summary buffer."
7204   (let ((buffer (concat "*Summary " group "*")))
7205     (if (get-buffer buffer)
7206         (progn
7207           (set-buffer buffer)
7208           (setq gnus-summary-buffer (current-buffer))
7209           (not gnus-newsgroup-prepared))
7210       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7211       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7212       (gnus-add-current-to-buffer-list)
7213       (gnus-summary-mode group)
7214       (when gnus-carpal
7215         (gnus-carpal-setup-buffer 'summary))
7216       (unless gnus-single-article-buffer
7217         (make-local-variable 'gnus-article-buffer)
7218         (make-local-variable 'gnus-original-article-buffer))
7219       (setq gnus-newsgroup-name group)
7220       t)))
7221
7222 (defun gnus-set-global-variables ()
7223   ;; Set the global equivalents of the summary buffer-local variables
7224   ;; to the latest values they had.  These reflect the summary buffer
7225   ;; that was in action when the last article was fetched.
7226   (when (eq major-mode 'gnus-summary-mode)
7227     (setq gnus-summary-buffer (current-buffer))
7228     (let ((name gnus-newsgroup-name)
7229           (marked gnus-newsgroup-marked)
7230           (unread gnus-newsgroup-unreads)
7231           (headers gnus-current-headers)
7232           (data gnus-newsgroup-data)
7233           (article-buffer gnus-article-buffer)
7234           (original gnus-original-article-buffer)
7235           (score-file gnus-current-score-file))
7236       (save-excursion
7237         (set-buffer gnus-group-buffer)
7238         (setq gnus-newsgroup-name name)
7239         (setq gnus-newsgroup-marked marked)
7240         (setq gnus-newsgroup-unreads unread)
7241         (setq gnus-current-headers headers)
7242         (setq gnus-newsgroup-data data)
7243         (setq gnus-article-buffer article-buffer)
7244         (setq gnus-original-article-buffer original)
7245         (setq gnus-current-score-file score-file)))))
7246
7247 (defun gnus-summary-last-article-p (&optional article)
7248   "Return whether ARTICLE is the last article in the buffer."
7249   (if (not (setq article (or article (gnus-summary-article-number))))
7250       t ; All non-existant numbers are the last article. :-)
7251     (cdr (gnus-data-find-list article))))
7252
7253 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7254   "Insert a dummy root in the summary buffer."
7255   (beginning-of-line)
7256   (add-text-properties
7257    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7258    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7259
7260 (defvar gnus-thread-indent-array nil)
7261 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7262 (defun gnus-make-thread-indent-array ()
7263   (let ((n 200))
7264     (if (and gnus-thread-indent-array
7265              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7266         nil
7267       (setq gnus-thread-indent-array (make-vector 201 "")
7268             gnus-thread-indent-array-level gnus-thread-indent-level)
7269       (while (>= n 0)
7270         (aset gnus-thread-indent-array n
7271               (make-string (* n gnus-thread-indent-level) ? ))
7272         (setq n (1- n))))))
7273
7274 (defun gnus-summary-insert-line
7275   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7276                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7277                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7278   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7279          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7280          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7281          (gnus-tmp-score-char
7282           (if (or (null gnus-summary-default-score)
7283                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7284                       gnus-summary-zcore-fuzz)) ? 
7285             (if (< gnus-tmp-score gnus-summary-default-score)
7286                 gnus-score-below-mark gnus-score-over-mark)))
7287          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7288                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7289                                   gnus-cached-mark)
7290                                  (gnus-tmp-replied gnus-replied-mark)
7291                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7292                                   gnus-saved-mark)
7293                                  (t gnus-unread-mark)))
7294          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7295          (gnus-tmp-name
7296           (cond
7297            ((string-match "(.+)" gnus-tmp-from)
7298             (substring gnus-tmp-from
7299                        (1+ (match-beginning 0)) (1- (match-end 0))))
7300            ((string-match "<[^>]+> *$" gnus-tmp-from)
7301             (let ((beg (match-beginning 0)))
7302               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7303                        (substring gnus-tmp-from (1+ (match-beginning 0))
7304                                   (1- (match-end 0))))
7305                   (substring gnus-tmp-from 0 beg))))
7306            (t gnus-tmp-from)))
7307          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7308          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7309          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7310          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7311          (buffer-read-only nil))
7312     (when (string= gnus-tmp-name "")
7313       (setq gnus-tmp-name gnus-tmp-from))
7314     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7315     (put-text-property
7316      (point)
7317      (progn (eval gnus-summary-line-format-spec) (point))
7318      'gnus-number gnus-tmp-number)
7319     (when (gnus-visual-p 'summary-highlight 'highlight)
7320       (forward-line -1)
7321       (run-hooks 'gnus-summary-update-hook)
7322       (forward-line 1))))
7323
7324 (defun gnus-summary-update-line (&optional dont-update)
7325   ;; Update summary line after change.
7326   (when (and gnus-summary-default-score
7327              (not gnus-summary-inhibit-highlight))
7328     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7329            (article (gnus-summary-article-number))
7330            (score (gnus-summary-article-score article)))
7331       (unless dont-update
7332         (if (and gnus-summary-mark-below
7333                  (< (gnus-summary-article-score)
7334                     gnus-summary-mark-below))
7335             ;; This article has a low score, so we mark it as read.
7336             (when (memq article gnus-newsgroup-unreads)
7337               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7338           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7339             ;; This article was previously marked as read on account
7340             ;; of a low score, but now it has risen, so we mark it as
7341             ;; unread.
7342             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7343         (gnus-summary-update-mark
7344          (if (or (null gnus-summary-default-score)
7345                  (<= (abs (- score gnus-summary-default-score))
7346                      gnus-summary-zcore-fuzz)) ? 
7347            (if (< score gnus-summary-default-score)
7348                gnus-score-below-mark gnus-score-over-mark)) 'score))
7349       ;; Do visual highlighting.
7350       (when (gnus-visual-p 'summary-highlight 'highlight)
7351         (run-hooks 'gnus-summary-update-hook)))))
7352
7353 (defvar gnus-tmp-new-adopts nil)
7354
7355 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7356   ;; Sum up all elements (and sub-elements) in a list.
7357   (let* ((number
7358           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7359           (cond
7360            ((and (consp thread) (cdr thread))
7361             (apply
7362              '+ 1 (mapcar
7363                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7364            ((null thread)
7365             1)
7366            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7367             1)
7368            (t 1))))
7369     (when (and level (zerop level) gnus-tmp-new-adopts)
7370       (incf number
7371             (apply '+ (mapcar
7372                        'gnus-summary-number-of-articles-in-thread
7373                        gnus-tmp-new-adopts))))
7374     (if char
7375         (if (> number 1) gnus-not-empty-thread-mark
7376           gnus-empty-thread-mark)
7377       number)))
7378
7379 (defun gnus-summary-set-local-parameters (group)
7380  "Go through the local params of GROUP and set all variable specs in that list."
7381   (let ((params (gnus-info-params (gnus-get-info group)))
7382         elem)
7383     (while params
7384       (setq elem (car params)
7385             params (cdr params))
7386       (and (consp elem)                 ; Has to be a cons.
7387            (consp (cdr elem))           ; The cdr has to be a list.
7388            (symbolp (car elem))         ; Has to be a symbol in there.
7389            (not (memq (car elem) 
7390                       '(quit-config to-address to-list to-group)))
7391            (progn                       ; So we set it.
7392              (make-local-variable (car elem))
7393              (set (car elem) (eval (nth 1 elem))))))))
7394
7395 (defun gnus-summary-read-group
7396   (group &optional show-all no-article kill-buffer no-display)
7397   "Start reading news in newsgroup GROUP.
7398 If SHOW-ALL is non-nil, already read articles are also listed.
7399 If NO-ARTICLE is non-nil, no article is selected initially.
7400 If NO-DISPLAY, don't generate a summary buffer."
7401   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7402   (let* ((new-group (gnus-summary-setup-buffer group))
7403          (quit-config (gnus-group-quit-config group))
7404          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7405     (cond
7406      ;; This summary buffer exists already, so we just select it.
7407      ((not new-group)
7408       (gnus-set-global-variables)
7409       (when kill-buffer
7410         (gnus-kill-or-deaden-summary kill-buffer))
7411       (gnus-configure-windows 'summary 'force)
7412       (gnus-set-mode-line 'summary)
7413       (gnus-summary-position-point)
7414       (message "")
7415       t)
7416      ;; We couldn't select this group.
7417      ((null did-select)
7418       (when (and (eq major-mode 'gnus-summary-mode)
7419                  (not (equal (current-buffer) kill-buffer)))
7420         (kill-buffer (current-buffer))
7421         (if (not quit-config)
7422             (progn
7423               (set-buffer gnus-group-buffer)
7424               (gnus-group-jump-to-group group)
7425               (gnus-group-next-unread-group 1))
7426           (if (not (buffer-name (car quit-config)))
7427               (gnus-configure-windows 'group 'force)
7428             (set-buffer (car quit-config))
7429             (and (eq major-mode 'gnus-summary-mode)
7430                  (gnus-set-global-variables))
7431             (gnus-configure-windows (cdr quit-config)))))
7432       (gnus-message 3 "Can't select group")
7433       nil)
7434      ;; The user did a `C-g' while prompting for number of articles,
7435      ;; so we exit this group.
7436      ((eq did-select 'quit)
7437       (and (eq major-mode 'gnus-summary-mode)
7438            (not (equal (current-buffer) kill-buffer))
7439            (kill-buffer (current-buffer)))
7440       (when kill-buffer
7441         (gnus-kill-or-deaden-summary kill-buffer))
7442       (if (not quit-config)
7443           (progn
7444             (set-buffer gnus-group-buffer)
7445             (gnus-group-jump-to-group group)
7446             (gnus-group-next-unread-group 1)
7447             (gnus-configure-windows 'group 'force))
7448         (if (not (buffer-name (car quit-config)))
7449             (gnus-configure-windows 'group 'force)
7450           (set-buffer (car quit-config))
7451           (and (eq major-mode 'gnus-summary-mode)
7452                (gnus-set-global-variables))
7453           (gnus-configure-windows (cdr quit-config))))
7454       ;; Finally signal the quit.
7455       (signal 'quit nil))
7456      ;; The group was successfully selected.
7457      (t
7458       (gnus-set-global-variables)
7459       ;; Save the active value in effect when the group was entered.
7460       (setq gnus-newsgroup-active
7461             (gnus-copy-sequence
7462              (gnus-active gnus-newsgroup-name)))
7463       ;; You can change the summary buffer in some way with this hook.
7464       (run-hooks 'gnus-select-group-hook)
7465       ;; Set any local variables in the group parameters.
7466       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7467       (gnus-update-format-specifications)
7468       ;; Do score processing.
7469       (when gnus-use-scoring
7470         (gnus-possibly-score-headers))
7471       ;; Check whether to fill in the gaps in the threads.
7472       (when gnus-build-sparse-threads
7473         (gnus-build-sparse-threads))
7474       ;; Find the initial limit.
7475       (if show-all
7476           (let ((gnus-newsgroup-dormant nil))
7477             (gnus-summary-initial-limit show-all))
7478         (gnus-summary-initial-limit show-all))
7479       ;; Generate the summary buffer.
7480       (unless no-display
7481         (gnus-summary-prepare))
7482       (when gnus-use-trees
7483         (gnus-tree-open group)
7484         (setq gnus-summary-highlight-line-function
7485               'gnus-tree-highlight-article))
7486       ;; If the summary buffer is empty, but there are some low-scored
7487       ;; articles or some excluded dormants, we include these in the
7488       ;; buffer.
7489       (when (and (zerop (buffer-size))
7490                  (not no-display))
7491         (cond (gnus-newsgroup-dormant
7492                (gnus-summary-limit-include-dormant))
7493               ((and gnus-newsgroup-scored show-all)
7494                (gnus-summary-limit-include-expunged))))
7495       ;; Function `gnus-apply-kill-file' must be called in this hook.
7496       (run-hooks 'gnus-apply-kill-hook)
7497       (if (and (zerop (buffer-size))
7498                (not no-display))
7499           (progn
7500             ;; This newsgroup is empty.
7501             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7502             (gnus-message 6 "No unread news")
7503             (when kill-buffer
7504               (gnus-kill-or-deaden-summary kill-buffer))
7505             ;; Return nil from this function.
7506             nil)
7507         ;; Hide conversation thread subtrees.  We cannot do this in
7508         ;; gnus-summary-prepare-hook since kill processing may not
7509         ;; work with hidden articles.
7510         (and gnus-show-threads
7511              gnus-thread-hide-subtree
7512              (gnus-summary-hide-all-threads))
7513         ;; Show first unread article if requested.
7514         (if (and (not no-article)
7515                  (not no-display)
7516                  gnus-newsgroup-unreads
7517                  gnus-auto-select-first)
7518             (if (eq gnus-auto-select-first 'best)
7519                 (gnus-summary-best-unread-article)
7520               (gnus-summary-first-unread-article))
7521           ;; Don't select any articles, just move point to the first
7522           ;; article in the group.
7523           (goto-char (point-min))
7524           (gnus-summary-position-point)
7525           (gnus-set-mode-line 'summary)
7526           (gnus-configure-windows 'summary 'force))
7527         ;; If we are in async mode, we send some info to the backend.
7528         (when gnus-newsgroup-async
7529           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7530         (when kill-buffer
7531           (gnus-kill-or-deaden-summary kill-buffer))
7532         (when (get-buffer-window gnus-group-buffer t)
7533           ;; Gotta use windows, because recenter does wierd stuff if
7534           ;; the current buffer ain't the displayed window.
7535           (let ((owin (selected-window)))
7536             (select-window (get-buffer-window gnus-group-buffer t))
7537             (when (gnus-group-goto-group group)
7538               (recenter))
7539             (select-window owin))))
7540       ;; Mark this buffer as "prepared".
7541       (setq gnus-newsgroup-prepared t)
7542       t))))
7543
7544 (defun gnus-summary-prepare ()
7545   "Generate the summary buffer."
7546   (let ((buffer-read-only nil))
7547     (erase-buffer)
7548     (setq gnus-newsgroup-data nil
7549           gnus-newsgroup-data-reverse nil)
7550     (run-hooks 'gnus-summary-generate-hook)
7551     ;; Generate the buffer, either with threads or without.
7552     (when gnus-newsgroup-headers
7553       (gnus-summary-prepare-threads
7554        (if gnus-show-threads
7555            (gnus-sort-gathered-threads
7556             (funcall gnus-summary-thread-gathering-function
7557                      (gnus-sort-threads
7558                       (gnus-cut-threads (gnus-make-threads)))))
7559          ;; Unthreaded display.
7560          (gnus-sort-articles gnus-newsgroup-headers))))
7561     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7562     ;; Call hooks for modifying summary buffer.
7563     (goto-char (point-min))
7564     (run-hooks 'gnus-summary-prepare-hook)))
7565
7566 (defun gnus-gather-threads-by-subject (threads)
7567   "Gather threads by looking at Subject headers."
7568   (if (not gnus-summary-make-false-root)
7569       threads
7570     (let ((hashtb (gnus-make-hashtable 1023))
7571           (prev threads)
7572           (result threads)
7573           subject hthread whole-subject)
7574       (while threads
7575         (setq whole-subject (mail-header-subject (car (car threads))))
7576         (if (and gnus-summary-gather-exclude-subject
7577                  (string-match gnus-summary-gather-exclude-subject
7578                                whole-subject))
7579             () ; We don't want to do anything with this article.
7580           ;; We simplify the subject before looking it up in the
7581           ;; hash table.
7582           (setq subject
7583                 (cond
7584                  ;; Truncate the subject.
7585                  ((numberp gnus-summary-gather-subject-limit)
7586                   (setq subject (gnus-simplify-subject-re whole-subject))
7587                   (if (> (length subject) gnus-summary-gather-subject-limit)
7588                       (substring subject 0 gnus-summary-gather-subject-limit)
7589                     subject))
7590                  ;; Fuzzily simplify it.
7591                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7592                   (gnus-simplify-subject-fuzzy whole-subject))
7593                  ;; Just remove the leading "Re:".
7594                  (t
7595                   (gnus-simplify-subject-re whole-subject))))
7596
7597           (if (setq hthread (gnus-gethash subject hashtb))
7598               (progn
7599                 ;; We enter a dummy root into the thread, if we
7600                 ;; haven't done that already.
7601                 (unless (stringp (car (car hthread)))
7602                   (setcar hthread (list whole-subject (car hthread))))
7603                 ;; We add this new gathered thread to this gathered
7604                 ;; thread.
7605                 (setcdr (car hthread)
7606                         (nconc (cdr (car hthread)) (list (car threads))))
7607                 ;; Remove it from the list of threads.
7608                 (setcdr prev (cdr threads))
7609                 (setq threads prev))
7610             ;; Enter this thread into the hash table.
7611             (gnus-sethash subject threads hashtb)))
7612         (setq prev threads)
7613         (setq threads (cdr threads)))
7614       result)))
7615
7616 (defun gnus-summary-gather-threads-by-references (threads)
7617   "Gather threads by looking at References headers."
7618   (let ((idhashtb (gnus-make-hashtable 1023))
7619         (thhashtb (gnus-make-hashtable 1023))
7620         (prev threads)
7621         (result threads)
7622         ids references id gthread gid entered)
7623     (while threads
7624       (when (setq references (mail-header-references (caar threads)))
7625         (setq id (mail-header-id (caar threads)))
7626         (setq ids (gnus-split-references references))
7627         (setq entered nil)
7628         (while ids
7629           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7630               (progn
7631                 (gnus-sethash (car ids) id idhashtb)
7632                 (gnus-sethash id threads thhashtb))
7633             (setq gthread (gnus-gethash gid thhashtb))
7634             (unless entered
7635               ;; We enter a dummy root into the thread, if we
7636               ;; haven't done that already.
7637               (unless (stringp (caar gthread))
7638                 (setcar gthread (list (mail-header-subject (caar gthread))
7639                                       (car gthread))))
7640               ;; We add this new gathered thread to this gathered
7641               ;; thread.
7642               (setcdr (car gthread)
7643                       (nconc (cdar gthread) (list (car threads)))))
7644             ;; Add it into the thread hash table.
7645             (gnus-sethash id gthread thhashtb)
7646             (setq entered t)
7647             ;; Remove it from the list of threads.
7648             (setcdr prev (cdr threads))
7649             (setq threads prev))
7650           (setq ids (cdr ids))))
7651       (setq prev threads)
7652       (setq threads (cdr threads)))
7653     result))
7654
7655 (defun gnus-sort-gathered-threads (threads)
7656   "Sort subtreads inside each gathered thread by article number."
7657   (let ((result threads))
7658     (while threads
7659       (when (stringp (car (car threads)))
7660         (setcdr (car threads)
7661                 (sort (cdr (car threads)) 'gnus-thread-sort-by-number)))
7662       (setq threads (cdr threads)))
7663     result))
7664
7665 (defun gnus-make-threads ()
7666   "Go through the dependency hashtb and find the roots.  Return all threads."
7667   (let (threads)
7668     (mapatoms
7669      (lambda (refs)
7670        (unless (car (symbol-value refs))
7671          ;; These threads do not refer back to any other articles,
7672          ;; so they're roots.
7673          (setq threads (append (cdr (symbol-value refs)) threads))))
7674      gnus-newsgroup-dependencies)
7675     threads))
7676
7677 (defun gnus-build-sparse-threads ()
7678   (let ((headers gnus-newsgroup-headers)
7679         (deps gnus-newsgroup-dependencies)
7680         header references generation relations 
7681         cthread subject child end pthread relation)
7682     ;; First we create an alist of generations/relations, where 
7683     ;; generations is how much we trust the ralation, and the relation
7684     ;; is parent/child.
7685     (gnus-message 7 "Making sparse threads...")
7686     (save-excursion
7687       (nnheader-set-temp-buffer " *gnus sparse threads*")
7688       (while (setq header (pop headers))
7689         (when (and (setq references (mail-header-references header))
7690                    (not (string= references "")))
7691           (insert references)
7692           (setq child (downcase (mail-header-id header))
7693                 subject (mail-header-subject header))
7694           (setq generation 0)
7695           (while (search-backward ">" nil t)
7696             (setq end (1+ (point)))
7697             (when (search-backward "<" nil t)
7698               (push (list (incf generation) 
7699                           child (setq child (downcase
7700                                              (buffer-substring (point) end)))
7701                           subject)
7702                     relations)))
7703           (push (list (1+ generation) child nil subject) relations)
7704           (erase-buffer)))
7705       (kill-buffer (current-buffer)))
7706     ;; Sort over trustworthiness.
7707     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7708     (while (setq relation (pop relations))
7709       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7710                 (unless (car (symbol-value cthread))
7711                   ;; Make this article the parent of these threads.
7712                   (setcar (symbol-value cthread)
7713                           (vector gnus-reffed-article-number 
7714                                   (cadddr relation) 
7715                                   "" ""
7716                                   (cadr relation) 
7717                                   (or (caddr relation) "") 0 0 "")))
7718               (set cthread (list (vector gnus-reffed-article-number
7719                                          (cadddr relation) 
7720                                          "" "" (cadr relation) 
7721                                          (or (caddr relation) "") 0 0 ""))))
7722         (push gnus-reffed-article-number gnus-newsgroup-limit)
7723         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7724         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7725               gnus-newsgroup-reads)
7726         (decf gnus-reffed-article-number)
7727         ;; Make this new thread the child of its parent.
7728         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7729             (setcdr (symbol-value pthread)
7730                     (nconc (cdr (symbol-value pthread))
7731                            (list (symbol-value cthread))))
7732           (set pthread (list nil (symbol-value cthread))))))
7733     (gnus-message 7 "Making sparse threads...done")))
7734
7735 (defun gnus-build-old-threads ()
7736   ;; Look at all the articles that refer back to old articles, and
7737   ;; fetch the headers for the articles that aren't there.  This will
7738   ;; build complete threads - if the roots haven't been expired by the
7739   ;; server, that is.
7740   (let (id heads)
7741     (mapatoms
7742      (lambda (refs)
7743        (when (not (car (symbol-value refs)))
7744          (setq heads (cdr (symbol-value refs)))
7745          (while heads
7746            (if (memq (mail-header-number (car (car heads)))
7747                      gnus-newsgroup-dormant)
7748                (setq heads (cdr heads))
7749              (setq id (symbol-name refs))
7750              (while (and (setq id (gnus-build-get-header id))
7751                          (not (car (gnus-gethash
7752                                     id gnus-newsgroup-dependencies)))))
7753              (setq heads nil)))))
7754      gnus-newsgroup-dependencies)))
7755
7756 (defun gnus-build-get-header (id)
7757   ;; Look through the buffer of NOV lines and find the header to
7758   ;; ID.  Enter this line into the dependencies hash table, and return
7759   ;; the id of the parent article (if any).
7760   (let ((deps gnus-newsgroup-dependencies)
7761         found header)
7762     (prog1
7763         (save-excursion
7764           (set-buffer nntp-server-buffer)
7765           (goto-char (point-min))
7766           (while (and (not found) (search-forward id nil t))
7767             (beginning-of-line)
7768             (setq found (looking-at
7769                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7770                                  (regexp-quote id))))
7771             (or found (beginning-of-line 2)))
7772           (when found
7773             (let (ref)
7774               (beginning-of-line)
7775               (and
7776                (setq header (gnus-nov-parse-line
7777                              (read (current-buffer)) deps))
7778                (gnus-parent-id (mail-header-references header))))))
7779       (when header
7780         (let ((number (mail-header-number header)))
7781           (push number gnus-newsgroup-limit)
7782           (push header gnus-newsgroup-headers)
7783           (if (memq number gnus-newsgroup-unselected)
7784               (progn
7785                 (push number gnus-newsgroup-unreads)
7786                 (setq gnus-newsgroup-unselected
7787                       (delq number gnus-newsgroup-unselected)))
7788             (push number gnus-newsgroup-ancient)))))))
7789
7790 (defun gnus-summary-update-article (article &optional header)
7791   "Update ARTICLE in the summary buffer."
7792   (let ((id (mail-header-id (gnus-summary-article-header article)))
7793         (data (gnus-data-find article)))
7794     (setcar (gnus-id-to-thread id) nil)
7795     (gnus-summary-insert-subject id)
7796     ;; Set the (possibly) new article number in the data structure.
7797     (gnus-data-set-number data (gnus-id-to-article id))))
7798
7799 (defun gnus-rebuild-thread (id)
7800   "Rebuild the thread containing ID."
7801   (let ((dep gnus-newsgroup-dependencies)
7802         (buffer-read-only nil)
7803         current headers refs thread art data)
7804     (if (not gnus-show-threads)
7805         (setq thread (list (car (gnus-id-to-thread id))))
7806       ;; Get the thread this article is part of.
7807       (setq thread (gnus-remove-thread id)))
7808     (setq current (save-excursion
7809                     (and (zerop (forward-line -1))
7810                          (gnus-summary-article-number))))
7811     ;; If this is a gathered thread, we have to go some re-gathering.
7812     (when (stringp (car thread))
7813       (let ((subject (car thread))
7814             roots thr)
7815         (setq thread (cdr thread))
7816         (while thread
7817           (unless (memq (setq thr (gnus-id-to-thread
7818                                       (gnus-root-id
7819                                        (mail-header-id (car (car thread))))))
7820                         roots)
7821             (push thr roots))
7822           (setq thread (cdr thread)))
7823         ;; We now have all (unique) roots.
7824         (if (= (length roots) 1)
7825             ;; All the loose roots are now one solid root.
7826             (setq thread (car roots))
7827           (setq thread (cons subject (gnus-sort-threads roots))))))
7828     (let ((beg (point))
7829           threads)
7830       ;; We then insert this thread into the summary buffer.
7831       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7832         (gnus-summary-prepare-threads (list thread))
7833         (setq data (nreverse gnus-newsgroup-data))
7834         (setq threads gnus-newsgroup-threads))
7835       ;; We splice the new data into the data structure.
7836       (gnus-data-enter-list current data)
7837       (gnus-data-compute-positions)
7838       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7839
7840 (defun gnus-id-to-thread (id)
7841   "Return the (sub-)thread where ID appears."
7842   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7843
7844 (defun gnus-id-to-article (id)
7845   "Return the article number of ID."
7846   (let ((thread (gnus-id-to-thread id)))
7847     (when thread
7848       (mail-header-number (car thread)))))
7849
7850 (defun gnus-id-to-header (id)
7851   "Return the article headers of ID."
7852   (car (gnus-id-to-thread id)))
7853
7854 (defun gnus-article-displayed-root-p (article)
7855   "Say whether ARTICLE is a root(ish) article."
7856   (let ((level (gnus-summary-thread-level article))
7857         particle)
7858     (cond 
7859      ((null level) nil)
7860      ((zerop level) t)
7861      ((and (= 1 level)
7862            (null (setq particle (gnus-id-to-article
7863                                  (gnus-parent-id 
7864                                   (mail-header-references 
7865                                    (gnus-summary-article-header article))))))
7866            (null (gnus-summary-thread-level particle)))))))
7867
7868 (defun gnus-root-id (id)
7869   "Return the id of the root of the thread where ID appears."
7870   (let (last-id prev)
7871     (while (and id (setq prev (car (gnus-gethash
7872                                     (downcase id)
7873                                     gnus-newsgroup-dependencies))))
7874       (setq last-id id
7875             id (gnus-parent-id (mail-header-references prev))))
7876     last-id))
7877
7878 (defun gnus-remove-thread (id &optional dont-remove)
7879   "Remove the thread that has ID in it."
7880   (let ((dep gnus-newsgroup-dependencies)
7881         headers thread prev last-id)
7882     ;; First go up in this thread until we find the root.
7883     (setq last-id (gnus-root-id id))
7884     (setq headers (list (car (gnus-id-to-thread last-id))
7885                         (car (car (cdr (gnus-id-to-thread last-id))))))
7886     ;; We have now found the real root of this thread.  It might have
7887     ;; been gathered into some loose thread, so we have to search
7888     ;; through the threads to find the thread we wanted.
7889     (let ((threads gnus-newsgroup-threads)
7890           sub)
7891       (while threads
7892         (setq sub (car threads))
7893         (if (stringp (car sub))
7894             ;; This is a gathered threads, so we look at the roots
7895             ;; below it to find whether this article in in this
7896             ;; gathered root.
7897             (progn
7898               (setq sub (cdr sub))
7899               (while sub
7900                 (when (member (car (car sub)) headers)
7901                   (setq thread (car threads)
7902                         threads nil
7903                         sub nil))
7904                 (setq sub (cdr sub))))
7905           ;; It's an ordinary thread, so we check it.
7906           (when (eq (car sub) (car headers))
7907             (setq thread sub
7908                   threads nil)))
7909         (setq threads (cdr threads)))
7910       ;; If this article is in no thread, then it's a root.
7911       (if thread
7912           (unless dont-remove
7913             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7914         (setq thread (gnus-gethash (downcase last-id) dep)))
7915       (when thread
7916         (prog1
7917             thread ; We return this thread.
7918           (unless dont-remove
7919             (if (stringp (car thread))
7920                 (progn
7921                   ;; If we use dummy roots, then we have to remove the
7922                   ;; dummy root as well.
7923                   (when (eq gnus-summary-make-false-root 'dummy)
7924                     ;; Uhm.
7925                     )
7926                   (setq thread (cdr thread))
7927                   (while thread
7928                     (gnus-remove-thread-1 (car thread))
7929                     (setq thread (cdr thread))))
7930               (gnus-remove-thread-1 thread))))))))
7931
7932 (defun gnus-remove-thread-1 (thread)
7933   "Remove the thread THREAD recursively."
7934   (let ((number (mail-header-number (car thread)))
7935         pos)
7936     (when (setq pos (text-property-any
7937                      (point-min) (point-max) 'gnus-number number))
7938       (goto-char pos)
7939       (gnus-delete-line)
7940       (gnus-data-remove number))
7941     (setq thread (cdr thread))
7942     (while thread
7943       (gnus-remove-thread-1 (car thread))
7944       (setq thread (cdr thread)))))
7945
7946 (defun gnus-sort-threads (threads)
7947   "Sort THREADS."
7948   (if (not gnus-thread-sort-functions)
7949       threads
7950     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7951                     (car gnus-thread-sort-functions)
7952                   `(lambda (t1 t2)
7953                      ,(gnus-make-sort-function 
7954                        (reverse gnus-thread-sort-functions))))))
7955       (gnus-message 7 "Sorting threads...")
7956       (prog1
7957           (sort threads func)
7958         (gnus-message 7 "Sorting threads...done")))))
7959
7960 (defun gnus-sort-articles (articles)
7961   "Sort ARTICLES."
7962   (when gnus-article-sort-functions
7963     (let ((func (if (= 1 (length gnus-article-sort-functions))
7964                     (car gnus-article-sort-functions)
7965                   `(lambda (t1 t2)
7966                      ,(gnus-make-sort-function 
7967                        (reverse gnus-article-sort-functions))))))
7968       (gnus-message 7 "Sorting articles...")
7969       (prog1
7970           (setq gnus-newsgroup-headers (sort articles func))
7971         (gnus-message 7 "Sorting articles...done")))))
7972
7973 (defun gnus-make-sort-function (funs)
7974   "Return a composite sort condition based on the functions in FUNC."
7975   (if (cdr funs)
7976       `(or (,(car funs) t1 t2)
7977            (and (not (,(car funs) t2 t1))
7978                 ,(gnus-make-sort-function (cdr funs))))
7979     `(,(car funs) t1 t2)))
7980                  
7981 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7982 (defmacro gnus-thread-header (thread)
7983   ;; Return header of first article in THREAD.
7984   ;; Note that THREAD must never, ever be anything else than a variable -
7985   ;; using some other form will lead to serious barfage.
7986   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7987   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7988   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
7989         (vector thread) 2))
7990
7991 (defsubst gnus-article-sort-by-number (h1 h2)
7992   "Sort articles by article number."
7993   (< (mail-header-number h1)
7994      (mail-header-number h2)))
7995
7996 (defun gnus-thread-sort-by-number (h1 h2)
7997   "Sort threads by root article number."
7998   (gnus-article-sort-by-number
7999    (gnus-thread-header h1) (gnus-thread-header h2)))
8000
8001 (defsubst gnus-article-sort-by-author (h1 h2)
8002   "Sort articles by root author."
8003   (string-lessp
8004    (let ((extract (funcall
8005                    gnus-extract-address-components
8006                    (mail-header-from h1))))
8007      (or (car extract) (cdr extract)))
8008    (let ((extract (funcall
8009                    gnus-extract-address-components
8010                    (mail-header-from h2))))
8011      (or (car extract) (cdr extract)))))
8012
8013 (defun gnus-thread-sort-by-author (h1 h2)
8014   "Sort threads by root author."
8015   (gnus-article-sort-by-author
8016    (gnus-thread-header h1)  (gnus-thread-header h2)))
8017
8018 (defsubst gnus-article-sort-by-subject (h1 h2)
8019   "Sort articles by root subject."
8020   (string-lessp
8021    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8022    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8023
8024 (defun gnus-thread-sort-by-subject (h1 h2)
8025   "Sort threads by root subject."
8026   (gnus-article-sort-by-subject
8027    (gnus-thread-header h1) (gnus-thread-header h2)))
8028
8029 (defsubst gnus-article-sort-by-date (h1 h2)
8030   "Sort articles by root article date."
8031   (string-lessp
8032    (gnus-sortable-date (mail-header-date h1))
8033    (gnus-sortable-date (mail-header-date h2))))
8034
8035 (defun gnus-thread-sort-by-date (h1 h2)
8036   "Sort threads by root article date."
8037   (gnus-article-sort-by-date
8038    (gnus-thread-header h1) (gnus-thread-header h2)))
8039
8040 (defsubst gnus-article-sort-by-score (h1 h2)
8041   "Sort articles by root article score.
8042 Unscored articles will be counted as having a score of zero."
8043   (> (or (cdr (assq (mail-header-number h1)
8044                     gnus-newsgroup-scored))
8045          gnus-summary-default-score 0)
8046      (or (cdr (assq (mail-header-number h2)
8047                     gnus-newsgroup-scored))
8048          gnus-summary-default-score 0)))
8049
8050 (defun gnus-thread-sort-by-score (h1 h2)
8051   "Sort threads by root article score."
8052   (gnus-article-sort-by-score
8053    (gnus-thread-header h1) (gnus-thread-header h2)))
8054
8055 (defun gnus-thread-sort-by-total-score (h1 h2)
8056   "Sort threads by the sum of all scores in the thread.
8057 Unscored articles will be counted as having a score of zero."
8058   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8059
8060 (defun gnus-thread-total-score (thread)
8061   ;;  This function find the total score of THREAD.
8062   (if (consp thread)
8063       (if (stringp (car thread))
8064           (apply gnus-thread-score-function 0
8065                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8066         (gnus-thread-total-score-1 thread))
8067     (gnus-thread-total-score-1 (list thread))))
8068
8069 (defun gnus-thread-total-score-1 (root)
8070   ;; This function find the total score of the thread below ROOT.
8071   (setq root (car root))
8072   (apply gnus-thread-score-function
8073          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8074              gnus-summary-default-score 0)
8075          (mapcar 'gnus-thread-total-score
8076                  (cdr (gnus-gethash (downcase (mail-header-id root))
8077                                     gnus-newsgroup-dependencies)))))
8078
8079 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8080 (defvar gnus-tmp-prev-subject nil)
8081 (defvar gnus-tmp-false-parent nil)
8082 (defvar gnus-tmp-root-expunged nil)
8083 (defvar gnus-tmp-dummy-line nil)
8084
8085 (defun gnus-summary-prepare-threads (threads)
8086   "Prepare summary buffer from THREADS and indentation LEVEL.
8087 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8088 or a straight list of headers."
8089   (gnus-message 7 "Generating summary...")
8090
8091   (setq gnus-newsgroup-threads threads)
8092   (beginning-of-line)
8093
8094   (let ((gnus-tmp-level 0)
8095         (default-score (or gnus-summary-default-score 0))
8096         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8097         thread number subject stack state gnus-tmp-gathered beg-match
8098         new-roots gnus-tmp-new-adopts thread-end
8099         gnus-tmp-header gnus-tmp-unread
8100         gnus-tmp-replied gnus-tmp-subject-or-nil
8101         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8102         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8103         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8104
8105     (setq gnus-tmp-prev-subject nil)
8106
8107     (if (vectorp (car threads))
8108         ;; If this is a straight (sic) list of headers, then a
8109         ;; threaded summary display isn't required, so we just create
8110         ;; an unthreaded one.
8111         (gnus-summary-prepare-unthreaded threads)
8112
8113       ;; Do the threaded display.
8114
8115       (while (or threads stack gnus-tmp-new-adopts new-roots)
8116
8117         (if (and (= gnus-tmp-level 0)
8118                  (not (setq gnus-tmp-dummy-line nil))
8119                  (or (not stack)
8120                      (= (car (car stack)) 0))
8121                  (not gnus-tmp-false-parent)
8122                  (or gnus-tmp-new-adopts new-roots))
8123             (if gnus-tmp-new-adopts
8124                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8125                       thread (list (car gnus-tmp-new-adopts))
8126                       gnus-tmp-header (car (car thread))
8127                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8128               (if new-roots
8129                   (setq thread (list (car new-roots))
8130                         gnus-tmp-header (car (car thread))
8131                         new-roots (cdr new-roots))))
8132
8133           (if threads
8134               ;; If there are some threads, we do them before the
8135               ;; threads on the stack.
8136               (setq thread threads
8137                     gnus-tmp-header (car (car thread)))
8138             ;; There were no current threads, so we pop something off
8139             ;; the stack.
8140             (setq state (car stack)
8141                   gnus-tmp-level (car state)
8142                   thread (cdr state)
8143                   stack (cdr stack)
8144                   gnus-tmp-header (car (car thread)))))
8145
8146         (setq gnus-tmp-false-parent nil)
8147         (setq gnus-tmp-root-expunged nil)
8148         (setq thread-end nil)
8149
8150         (if (stringp gnus-tmp-header)
8151             ;; The header is a dummy root.
8152             (cond
8153              ((eq gnus-summary-make-false-root 'adopt)
8154               ;; We let the first article adopt the rest.
8155               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8156                                                (cdr (cdr (car thread)))))
8157               (setq gnus-tmp-gathered
8158                     (nconc (mapcar
8159                             (lambda (h) (mail-header-number (car h)))
8160                             (cdr (cdr (car thread))))
8161                            gnus-tmp-gathered))
8162               (setq thread (cons (list (car (car thread))
8163                                        (car (cdr (car thread))))
8164                                  (cdr thread)))
8165               (setq gnus-tmp-level -1
8166                     gnus-tmp-false-parent t))
8167              ((eq gnus-summary-make-false-root 'empty)
8168               ;; We print adopted articles with empty subject fields.
8169               (setq gnus-tmp-gathered
8170                     (nconc (mapcar
8171                             (lambda (h) (mail-header-number (car h)))
8172                             (cdr (cdr (car thread))))
8173                            gnus-tmp-gathered))
8174               (setq gnus-tmp-level -1))
8175              ((eq gnus-summary-make-false-root 'dummy)
8176               ;; We remember that we probably want to output a dummy
8177               ;; root.
8178               (setq gnus-tmp-dummy-line gnus-tmp-header)
8179               (setq gnus-tmp-prev-subject gnus-tmp-header))
8180              (t
8181               ;; We do not make a root for the gathered
8182               ;; sub-threads at all.
8183               (setq gnus-tmp-level -1)))
8184
8185           (setq number (mail-header-number gnus-tmp-header)
8186                 subject (mail-header-subject gnus-tmp-header))
8187
8188           (cond
8189            ;; If the thread has changed subject, we might want to make
8190            ;; this subthread into a root.
8191            ((and (null gnus-thread-ignore-subject)
8192                  (not (zerop gnus-tmp-level))
8193                  gnus-tmp-prev-subject
8194                  (not (inline
8195                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8196             (setq new-roots (nconc new-roots (list (car thread)))
8197                   thread-end t
8198                   gnus-tmp-header nil))
8199            ;; If the article lies outside the current limit,
8200            ;; then we do not display it.
8201            ((and (not (memq number gnus-newsgroup-limit))
8202                  (not gnus-tmp-dummy-line))
8203             (setq gnus-tmp-gathered
8204                   (nconc (mapcar
8205                           (lambda (h) (mail-header-number (car h)))
8206                           (cdr (car thread)))
8207                          gnus-tmp-gathered))
8208             (setq gnus-tmp-new-adopts (if (cdr (car thread))
8209                                           (append gnus-tmp-new-adopts
8210                                                   (cdr (car thread)))
8211                                         gnus-tmp-new-adopts)
8212                   thread-end t
8213                   gnus-tmp-header nil)
8214             (when (zerop gnus-tmp-level)
8215               (setq gnus-tmp-root-expunged t)))
8216            ;; Perhaps this article is to be marked as read?
8217            ((and gnus-summary-mark-below
8218                  (< (or (cdr (assq number gnus-newsgroup-scored))
8219                         default-score)
8220                     gnus-summary-mark-below)
8221                  ;; Don't touch sparse articles.
8222                  (not (memq number gnus-newsgroup-sparse)))
8223             (setq gnus-newsgroup-unreads
8224                   (delq number gnus-newsgroup-unreads))
8225             (if gnus-newsgroup-auto-expire
8226                 (push number gnus-newsgroup-expirable)
8227               (push (cons number gnus-low-score-mark)
8228                     gnus-newsgroup-reads))))
8229
8230           (when gnus-tmp-header
8231             ;; We may have an old dummy line to output before this
8232             ;; article.
8233             (when gnus-tmp-dummy-line
8234               (gnus-summary-insert-dummy-line
8235                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8236               (setq gnus-tmp-dummy-line nil))
8237
8238             ;; Compute the mark.
8239             (setq
8240              gnus-tmp-unread
8241              (cond
8242               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8243               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8244               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8245               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8246               (t (or (cdr (assq number gnus-newsgroup-reads))
8247                      gnus-ancient-mark))))
8248
8249             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8250                                   gnus-tmp-header gnus-tmp-level)
8251                   gnus-newsgroup-data)
8252
8253             ;; Actually insert the line.
8254             (setq
8255              gnus-tmp-subject-or-nil
8256              (cond
8257               ((and gnus-thread-ignore-subject
8258                     gnus-tmp-prev-subject
8259                     (not (inline (gnus-subject-equal
8260                                   gnus-tmp-prev-subject subject))))
8261                subject)
8262               ((zerop gnus-tmp-level)
8263                (if (and (eq gnus-summary-make-false-root 'empty)
8264                         (memq number gnus-tmp-gathered)
8265                         gnus-tmp-prev-subject
8266                         (inline (gnus-subject-equal
8267                                  gnus-tmp-prev-subject subject)))
8268                    gnus-summary-same-subject
8269                  subject))
8270               (t gnus-summary-same-subject)))
8271             (if (and (eq gnus-summary-make-false-root 'adopt)
8272                      (= gnus-tmp-level 1)
8273                      (memq number gnus-tmp-gathered))
8274                 (setq gnus-tmp-opening-bracket ?\<
8275                       gnus-tmp-closing-bracket ?\>)
8276               (setq gnus-tmp-opening-bracket ?\[
8277                     gnus-tmp-closing-bracket ?\]))
8278             (setq
8279              gnus-tmp-indentation
8280              (aref gnus-thread-indent-array gnus-tmp-level)
8281              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8282              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8283                                 gnus-summary-default-score 0)
8284              gnus-tmp-score-char
8285              (if (or (null gnus-summary-default-score)
8286                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8287                          gnus-summary-zcore-fuzz)) ? 
8288                (if (< gnus-tmp-score gnus-summary-default-score)
8289                    gnus-score-below-mark gnus-score-over-mark))
8290              gnus-tmp-replied
8291              (cond ((memq number gnus-newsgroup-processable)
8292                     gnus-process-mark)
8293                    ((memq number gnus-newsgroup-cached)
8294                     gnus-cached-mark)
8295                    ((memq number gnus-newsgroup-replied)
8296                     gnus-replied-mark)
8297                    (t gnus-unread-mark))
8298              gnus-tmp-from (mail-header-from gnus-tmp-header)
8299              gnus-tmp-name
8300              (cond
8301               ((string-match "(.+)" gnus-tmp-from)
8302                (substring gnus-tmp-from
8303                           (1+ (match-beginning 0)) (1- (match-end 0))))
8304               ((string-match "<[^>]+> *$" gnus-tmp-from)
8305                (setq beg-match (match-beginning 0))
8306                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8307                         (substring gnus-tmp-from (1+ (match-beginning 0))
8308                                    (1- (match-end 0))))
8309                    (substring gnus-tmp-from 0 beg-match)))
8310               (t gnus-tmp-from)))
8311             (when (string= gnus-tmp-name "")
8312               (setq gnus-tmp-name gnus-tmp-from))
8313             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8314             (put-text-property
8315              (point)
8316              (progn (eval gnus-summary-line-format-spec) (point))
8317              'gnus-number number)
8318             (when gnus-visual-p
8319               (forward-line -1)
8320               (run-hooks 'gnus-summary-update-hook)
8321               (forward-line 1))
8322
8323             (setq gnus-tmp-prev-subject subject)))
8324
8325         (when (nth 1 thread)
8326           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8327         (incf gnus-tmp-level)
8328         (setq threads (if thread-end nil (cdr (car thread))))
8329         (unless threads
8330           (setq gnus-tmp-level 0)))))
8331   (gnus-message 7 "Generating summary...done"))
8332
8333 (defun gnus-summary-prepare-unthreaded (headers)
8334   "Generate an unthreaded summary buffer based on HEADERS."
8335   (let (header number mark)
8336
8337     (while headers
8338       (setq header (car headers)
8339             headers (cdr headers)
8340             number (mail-header-number header))
8341
8342       ;; We may have to root out some bad articles...
8343       (when (memq number gnus-newsgroup-limit)
8344         (when (and gnus-summary-mark-below
8345                    (< (or (cdr (assq number gnus-newsgroup-scored))
8346                           gnus-summary-default-score 0)
8347                       gnus-summary-mark-below))
8348           (setq gnus-newsgroup-unreads
8349                 (delq number gnus-newsgroup-unreads))
8350           (if gnus-newsgroup-auto-expire
8351               (push number gnus-newsgroup-expirable)
8352             (push (cons number gnus-low-score-mark)
8353                   gnus-newsgroup-reads)))
8354
8355         (setq mark
8356               (cond
8357                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8358                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8359                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8360                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8361                (t (or (cdr (assq number gnus-newsgroup-reads))
8362                       gnus-ancient-mark))))
8363         (setq gnus-newsgroup-data
8364               (cons (gnus-data-make number mark (1+ (point)) header 0)
8365                     gnus-newsgroup-data))
8366         (gnus-summary-insert-line
8367          header 0 nil mark (memq number gnus-newsgroup-replied)
8368          (memq number gnus-newsgroup-expirable)
8369          (mail-header-subject header) nil
8370          (cdr (assq number gnus-newsgroup-scored))
8371          (memq number gnus-newsgroup-processable))))))
8372
8373 (defun gnus-select-newsgroup (group &optional read-all)
8374   "Select newsgroup GROUP.
8375 If READ-ALL is non-nil, all articles in the group are selected."
8376   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8377          (info (nth 2 entry))
8378          articles fetched-articles cached)
8379
8380     (or (gnus-check-server
8381          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8382         (error "Couldn't open server"))
8383
8384     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8385         (gnus-activate-group group) ; Or we can activate it...
8386         (progn ; Or we bug out.
8387           (kill-buffer (current-buffer))
8388           (error "Couldn't request group %s: %s"
8389                  group (gnus-status-message group))))
8390
8391     (setq gnus-newsgroup-name group)
8392     (setq gnus-newsgroup-unselected nil)
8393     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8394
8395     (and gnus-asynchronous
8396          (gnus-check-backend-function
8397           'request-asynchronous gnus-newsgroup-name)
8398          (setq gnus-newsgroup-async
8399                (gnus-request-asynchronous gnus-newsgroup-name)))
8400
8401     ;; Adjust and set lists of article marks.
8402     (when info
8403       (gnus-adjust-marked-articles info))
8404
8405     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8406     (when (gnus-virtual-group-p group)
8407       (setq cached gnus-newsgroup-cached))
8408
8409     (setq gnus-newsgroup-unreads
8410           (gnus-set-difference
8411            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8412            gnus-newsgroup-dormant))
8413
8414     (setq gnus-newsgroup-processable nil)
8415
8416     (setq articles (gnus-articles-to-read group read-all))
8417
8418     (cond
8419      ((null articles)
8420       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8421       'quit)
8422      ((eq articles 0) nil)
8423      (t
8424       ;; Init the dependencies hash table.
8425       (setq gnus-newsgroup-dependencies
8426             (gnus-make-hashtable (length articles)))
8427       ;; Retrieve the headers and read them in.
8428       (gnus-message 5 "Fetching headers...")
8429       (setq gnus-newsgroup-headers
8430             (if (eq 'nov
8431                     (setq gnus-headers-retrieved-by
8432                           (gnus-retrieve-headers
8433                            articles gnus-newsgroup-name
8434                            ;; We might want to fetch old headers, but
8435                            ;; not if there is only 1 article.
8436                            (and gnus-fetch-old-headers
8437                                 (or (and
8438                                      (not (eq gnus-fetch-old-headers 'some))
8439                                      (not (numberp gnus-fetch-old-headers)))
8440                                     (> (length articles) 1))))))
8441                 (gnus-get-newsgroup-headers-xover articles)
8442               (gnus-get-newsgroup-headers)))
8443       (gnus-message 5 "Fetching headers...done")
8444
8445       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8446       (when cached
8447         (setq gnus-newsgroup-cached cached))
8448
8449       ;; Set the initial limit.
8450       (setq gnus-newsgroup-limit (copy-sequence articles))
8451       ;; Remove canceled articles from the list of unread articles.
8452       (setq gnus-newsgroup-unreads
8453             (gnus-set-sorted-intersection
8454              gnus-newsgroup-unreads
8455              (setq fetched-articles
8456                    (mapcar (lambda (headers) (mail-header-number headers))
8457                            gnus-newsgroup-headers))))
8458       ;; Removed marked articles that do not exist.
8459       (gnus-update-missing-marks
8460        (gnus-sorted-complement fetched-articles articles))
8461       ;; We might want to build some more threads first.
8462       (and gnus-fetch-old-headers
8463            (eq gnus-headers-retrieved-by 'nov)
8464            (gnus-build-old-threads))
8465       ;; Check whether auto-expire is to be done in this group.
8466       (setq gnus-newsgroup-auto-expire
8467             (gnus-group-auto-expirable-p group))
8468       ;; First and last article in this newsgroup.
8469       (and gnus-newsgroup-headers
8470            (setq gnus-newsgroup-begin
8471                  (mail-header-number (car gnus-newsgroup-headers)))
8472            (setq gnus-newsgroup-end
8473                  (mail-header-number
8474                   (gnus-last-element gnus-newsgroup-headers))))
8475       (setq gnus-reffed-article-number -1)
8476       ;; GROUP is successfully selected.
8477       (or gnus-newsgroup-headers t)))))
8478
8479 (defun gnus-articles-to-read (group read-all)
8480   ;; Find out what articles the user wants to read.
8481   (let* ((articles
8482           ;; Select all articles if `read-all' is non-nil, or if there
8483           ;; are no unread articles.
8484           (if (or read-all
8485                   (and (zerop (length gnus-newsgroup-marked))
8486                        (zerop (length gnus-newsgroup-unreads))))
8487               (gnus-uncompress-range (gnus-active group))
8488             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8489                           (copy-sequence gnus-newsgroup-unreads))
8490                   '<)))
8491          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8492          (scored (length scored-list))
8493          (number (length articles))
8494          (marked (+ (length gnus-newsgroup-marked)
8495                     (length gnus-newsgroup-dormant)))
8496          (select
8497           (cond
8498            ((numberp read-all)
8499             read-all)
8500            (t
8501             (condition-case ()
8502                 (cond
8503                  ((and (or (<= scored marked) (= scored number))
8504                        (numberp gnus-large-newsgroup)
8505                        (> number gnus-large-newsgroup))
8506                   (let ((input
8507                          (read-string
8508                           (format
8509                            "How many articles from %s (default %d): "
8510                            gnus-newsgroup-name number))))
8511                     (if (string-match "^[ \t]*$" input) number input)))
8512                  ((and (> scored marked) (< scored number))
8513                   (let ((input
8514                          (read-string
8515                           (format "%s %s (%d scored, %d total): "
8516                                   "How many articles from"
8517                                   group scored number))))
8518                     (if (string-match "^[ \t]*$" input)
8519                         number input)))
8520                  (t number))
8521               (quit nil))))))
8522     (setq select (if (stringp select) (string-to-number select) select))
8523     (if (or (null select) (zerop select))
8524         select
8525       (if (and (not (zerop scored)) (<= (abs select) scored))
8526           (progn
8527             (setq articles (sort scored-list '<))
8528             (setq number (length articles)))
8529         (setq articles (copy-sequence articles)))
8530
8531       (if (< (abs select) number)
8532           (if (< select 0)
8533               ;; Select the N oldest articles.
8534               (setcdr (nthcdr (1- (abs select)) articles) nil)
8535             ;; Select the N most recent articles.
8536             (setq articles (nthcdr (- number select) articles))))
8537       (setq gnus-newsgroup-unselected
8538             (gnus-sorted-intersection
8539              gnus-newsgroup-unreads
8540              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8541       articles)))
8542
8543 (defun gnus-killed-articles (killed articles)
8544   (let (out)
8545     (while articles
8546       (if (inline (gnus-member-of-range (car articles) killed))
8547           (setq out (cons (car articles) out)))
8548       (setq articles (cdr articles)))
8549     out))
8550
8551 (defun gnus-uncompress-marks (marks)
8552   "Uncompress the mark ranges in MARKS."
8553   (let ((uncompressed '(score bookmark))
8554         out)
8555     (while marks
8556       (if (memq (caar marks) uncompressed)
8557           (push (car marks) out)
8558         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8559       (setq marks (cdr marks)))
8560     out))
8561
8562 (defun gnus-adjust-marked-articles (info)
8563   "Set all article lists and remove all marks that are no longer legal."
8564   (let* ((marked-lists (gnus-info-marks info))
8565          (active (gnus-active (gnus-info-group info)))
8566          (min (car active))
8567          (max (cdr active))
8568          (types gnus-article-mark-lists)
8569          (uncompressed '(score bookmark))
8570          marks var articles article mark)
8571
8572     (while marked-lists
8573       (setq marks (pop marked-lists))
8574       (set (setq var (intern (format "gnus-newsgroup-%s"
8575                                      (car (rassq (setq mark (car marks))
8576                                                  types)))))
8577            (if (memq (car marks) uncompressed) (cdr marks)
8578              (gnus-uncompress-range (cdr marks))))
8579
8580       (setq articles (symbol-value var))
8581
8582       ;; All articles have to be subsets of the active articles.
8583       (cond
8584        ;; Adjust "simple" lists.
8585        ((memq mark '(tick dormant expirable reply killed save))
8586         (while articles
8587           (when (or (< (setq article (pop articles)) min) (> article max))
8588             (set var (delq article (symbol-value var))))))
8589        ;; Adjust assocs.
8590        ((memq mark '(score bookmark))
8591         (while articles
8592           (when (or (< (car (setq article (pop articles))) min)
8593                     (> (car article) max))
8594             (set var (delq article (symbol-value var))))))))))
8595
8596 (defun gnus-update-missing-marks (missing)
8597   "Go through the list of MISSING articles and remove them mark lists."
8598   (when missing
8599     (let ((types gnus-article-mark-lists)
8600           var m)
8601       ;; Go through all types.
8602       (while types
8603         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8604         (when (symbol-value var)
8605           ;; This list has articles.  So we delete all missing articles
8606           ;; from it.
8607           (setq m missing)
8608           (while m
8609             (set var (delq (pop m) (symbol-value var)))))))))
8610
8611 (defun gnus-update-marks ()
8612   "Enter the various lists of marked articles into the newsgroup info list."
8613   (let ((types gnus-article-mark-lists)
8614         (info (gnus-get-info gnus-newsgroup-name))
8615         (uncompressed '(score bookmark killed))
8616         var type list newmarked symbol)
8617     (when info
8618       ;; Add all marks lists that are non-nil to the list of marks lists.
8619       (while types
8620         (setq type (pop types))
8621         (when (setq list (symbol-value
8622                           (setq symbol
8623                                 (intern (format "gnus-newsgroup-%s"
8624                                                 (car type))))))
8625           (push (cons (cdr type)
8626                       (if (memq (cdr type) uncompressed) list
8627                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8628                 newmarked)))
8629
8630       ;; Enter these new marks into the info of the group.
8631       (if (nthcdr 3 info)
8632           (setcar (nthcdr 3 info) newmarked)
8633         ;; Add the marks lists to the end of the info.
8634         (when newmarked
8635           (setcdr (nthcdr 2 info) (list newmarked))))
8636
8637       ;; Cut off the end of the info if there's nothing else there.
8638       (let ((i 5))
8639         (while (and (> i 2)
8640                     (not (nth i info)))
8641           (when (nthcdr (decf i) info)
8642             (setcdr (nthcdr i info) nil)))))))
8643
8644 (defun gnus-add-marked-articles (group type articles &optional info force)
8645   ;; Add ARTICLES of TYPE to the info of GROUP.
8646   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8647   ;; add, but replace marked articles of TYPE with ARTICLES.
8648   (let ((info (or info (gnus-get-info group)))
8649         (uncompressed '(score bookmark killed))
8650         marked m)
8651     (or (not info)
8652         (and (not (setq marked (nthcdr 3 info)))
8653              (setcdr (nthcdr 2 info)
8654                      (list (list (cons type (gnus-compress-sequence
8655                                              articles t))))))
8656         (and (not (setq m (assq type (car marked))))
8657              (setcar marked
8658                      (cons (cons type (gnus-compress-sequence articles t) )
8659                            (car marked))))
8660         (if force
8661             (setcdr m (gnus-compress-sequence articles t))
8662           (setcdr m (gnus-compress-sequence
8663                      (sort (nconc (gnus-uncompress-range m)
8664                                   (copy-sequence articles)) '<) t))))))
8665
8666 (defun gnus-set-mode-line (where)
8667   "This function sets the mode line of the article or summary buffers.
8668 If WHERE is `summary', the summary mode line format will be used."
8669   ;; Is this mode line one we keep updated?
8670   (when (memq where gnus-updated-mode-lines)
8671     (let (mode-string)
8672       (save-excursion
8673         ;; We evaluate this in the summary buffer since these
8674         ;; variables are buffer-local to that buffer.
8675         (set-buffer gnus-summary-buffer)
8676         ;; We bind all these variables that are used in the `eval' form
8677         ;; below.
8678         (let* ((mformat (symbol-value
8679                          (intern
8680                           (format "gnus-%s-mode-line-format-spec" where))))
8681                (gnus-tmp-group-name gnus-newsgroup-name)
8682                (gnus-tmp-article-number (or gnus-current-article 0))
8683                (gnus-tmp-unread gnus-newsgroup-unreads)
8684                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8685                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8686                (gnus-tmp-unread-and-unselected
8687                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8688                             (zerop gnus-tmp-unselected)) "")
8689                       ((zerop gnus-tmp-unselected)
8690                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8691                       (t (format "{%d(+%d) more}"
8692                                  gnus-tmp-unread-and-unticked
8693                                  gnus-tmp-unselected))))
8694                (gnus-tmp-subject
8695                 (if (and gnus-current-headers
8696                          (vectorp gnus-current-headers))
8697                     (mail-header-subject gnus-current-headers) ""))
8698                max-len
8699                gnus-tmp-header);; passed as argument to any user-format-funcs
8700           (setq mode-string (eval mformat))
8701           (setq max-len (max 4 (if gnus-mode-non-string-length
8702                                    (- (frame-width)
8703                                       gnus-mode-non-string-length)
8704                                  (length mode-string))))
8705           ;; We might have to chop a bit of the string off...
8706           (when (> (length mode-string) max-len)
8707             (setq mode-string
8708                   (concat (gnus-truncate-string mode-string (- max-len 3))
8709                           "...")))
8710           ;; Pad the mode string a bit.
8711           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8712       ;; Update the mode line.
8713       (setq mode-line-buffer-identification (list mode-string))
8714       (set-buffer-modified-p t))))
8715
8716 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8717   "Go through the HEADERS list and add all Xrefs to a hash table.
8718 The resulting hash table is returned, or nil if no Xrefs were found."
8719   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8720          (virtual (gnus-virtual-group-p from-newsgroup))
8721          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8722          (xref-hashtb (make-vector 63 0))
8723          start group entry number xrefs header)
8724     (while headers
8725       (setq header (pop headers))
8726       (when (and (setq xrefs (mail-header-xref header))
8727                  (not (memq (setq number (mail-header-number header))
8728                             unreads)))
8729         (setq start 0)
8730         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8731           (setq start (match-end 0))
8732           (setq group (concat prefix (substring xrefs (match-beginning 1)
8733                                                 (match-end 1))))
8734           (setq number
8735                 (string-to-int (substring xrefs (match-beginning 2)
8736                                           (match-end 2))))
8737           (if (setq entry (gnus-gethash group xref-hashtb))
8738               (setcdr entry (cons number (cdr entry)))
8739             (gnus-sethash group (cons number nil) xref-hashtb)))))
8740     (and start xref-hashtb)))
8741
8742 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8743   "Look through all the headers and mark the Xrefs as read."
8744   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8745         name entry info xref-hashtb idlist method nth4)
8746     (save-excursion
8747       (set-buffer gnus-group-buffer)
8748       (when (setq xref-hashtb
8749                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8750         (mapatoms
8751          (lambda (group)
8752            (unless (string= from-newsgroup (setq name (symbol-name group)))
8753              (setq idlist (symbol-value group))
8754              ;; Dead groups are not updated.
8755              (and (prog1
8756                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8757                             info (nth 2 entry))
8758                     (if (stringp (setq nth4 (gnus-info-method info)))
8759                         (setq nth4 (gnus-server-to-method nth4))))
8760                   ;; Only do the xrefs if the group has the same
8761                   ;; select method as the group we have just read.
8762                   (or (gnus-methods-equal-p
8763                        nth4 (gnus-find-method-for-group from-newsgroup))
8764                       virtual
8765                       (equal nth4 (setq method (gnus-find-method-for-group
8766                                                 from-newsgroup)))
8767                       (and (equal (car nth4) (car method))
8768                            (equal (nth 1 nth4) (nth 1 method))))
8769                   gnus-use-cross-reference
8770                   (or (not (eq gnus-use-cross-reference t))
8771                       virtual
8772                       ;; Only do cross-references on subscribed
8773                       ;; groups, if that is what is wanted.
8774                       (<= (gnus-info-level info) gnus-level-subscribed))
8775                   (gnus-group-make-articles-read name idlist))))
8776          xref-hashtb)))))
8777
8778 (defun gnus-group-make-articles-read (group articles)
8779   (let* ((num 0)
8780          (entry (gnus-gethash group gnus-newsrc-hashtb))
8781          (info (nth 2 entry))
8782          (active (gnus-active group))
8783          range)
8784     ;; First peel off all illegal article numbers.
8785     (if active
8786         (let ((ids articles)
8787               id first)
8788           (while ids
8789             (setq id (car ids))
8790             (if (and first (> id (cdr active)))
8791                 (progn
8792                   ;; We'll end up in this situation in one particular
8793                   ;; obscure situation.  If you re-scan a group and get
8794                   ;; a new article that is cross-posted to a different
8795                   ;; group that has not been re-scanned, you might get
8796                   ;; crossposted article that has a higher number than
8797                   ;; Gnus believes possible.  So we re-activate this
8798                   ;; group as well.  This might mean doing the
8799                   ;; crossposting thingy will *increase* the number
8800                   ;; of articles in some groups.  Tsk, tsk.
8801                   (setq active (or (gnus-activate-group group) active))))
8802             (if (or (> id (cdr active))
8803                     (< id (car active)))
8804                 (setq articles (delq id articles)))
8805             (setq ids (cdr ids)))))
8806     ;; If the read list is nil, we init it.
8807     (and active
8808          (null (gnus-info-read info))
8809          (> (car active) 1)
8810          (gnus-info-set-read info (cons 1 (1- (car active)))))
8811     ;; Then we add the read articles to the range.
8812     (gnus-info-set-read
8813      info
8814      (setq range
8815            (gnus-add-to-range
8816             (gnus-info-read info) (setq articles (sort articles '<)))))
8817     ;; Then we have to re-compute how many unread
8818     ;; articles there are in this group.
8819     (if active
8820         (progn
8821           (cond
8822            ((not range)
8823             (setq num (- (1+ (cdr active)) (car active))))
8824            ((not (listp (cdr range)))
8825             (setq num (- (cdr active) (- (1+ (cdr range))
8826                                          (car range)))))
8827            (t
8828             (while range
8829               (if (numberp (car range))
8830                   (setq num (1+ num))
8831                 (setq num (+ num (- (1+ (cdr (car range)))
8832                                     (car (car range))))))
8833               (setq range (cdr range)))
8834             (setq num (- (cdr active) num))))
8835           ;; Update the number of unread articles.
8836           (setcar entry num)
8837           ;; Update the group buffer.
8838           (gnus-group-update-group group t)))))
8839
8840 (defun gnus-methods-equal-p (m1 m2)
8841   (let ((m1 (or m1 gnus-select-method))
8842         (m2 (or m2 gnus-select-method)))
8843     (or (equal m1 m2)
8844         (and (eq (car m1) (car m2))
8845              (or (not (memq 'address (assoc (symbol-name (car m1))
8846                                             gnus-valid-select-methods)))
8847                  (equal (nth 1 m1) (nth 1 m2)))))))
8848
8849 (defsubst gnus-header-value ()
8850   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8851
8852 (defvar gnus-newsgroup-none-id 0)
8853
8854 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8855   (let ((cur nntp-server-buffer)
8856         (dependencies
8857          (or dependencies
8858              (save-excursion (set-buffer gnus-summary-buffer)
8859                              gnus-newsgroup-dependencies)))
8860         headers id id-dep ref-dep end ref)
8861     (save-excursion
8862       (set-buffer nntp-server-buffer)
8863       (let ((case-fold-search t)
8864             in-reply-to header number p lines)
8865         (goto-char (point-min))
8866         ;; Search to the beginning of the next header.  Error messages
8867         ;; do not begin with 2 or 3.
8868         (while (re-search-forward "^[23][0-9]+ " nil t)
8869           (setq id nil
8870                 ref nil)
8871           ;; This implementation of this function, with nine
8872           ;; search-forwards instead of the one re-search-forward and
8873           ;; a case (which basically was the old function) is actually
8874           ;; about twice as fast, even though it looks messier.  You
8875           ;; can't have everything, I guess.  Speed and elegance
8876           ;; doesn't always go hand in hand.
8877           (setq
8878            header
8879            (vector
8880             ;; Number.
8881             (prog1
8882                 (read cur)
8883               (end-of-line)
8884               (setq p (point))
8885               (narrow-to-region (point)
8886                                 (or (and (search-forward "\n.\n" nil t)
8887                                          (- (point) 2))
8888                                     (point))))
8889             ;; Subject.
8890             (progn
8891               (goto-char p)
8892               (if (search-forward "\nsubject: " nil t)
8893                   (gnus-header-value) "(none)"))
8894             ;; From.
8895             (progn
8896               (goto-char p)
8897               (if (search-forward "\nfrom: " nil t)
8898                   (gnus-header-value) "(nobody)"))
8899             ;; Date.
8900             (progn
8901               (goto-char p)
8902               (if (search-forward "\ndate: " nil t)
8903                   (gnus-header-value) ""))
8904             ;; Message-ID.
8905             (progn
8906               (goto-char p)
8907               (if (search-forward "\nmessage-id: " nil t)
8908                   (setq id (gnus-header-value))
8909                 ;; If there was no message-id, we just fake one to make
8910                 ;; subsequent routines simpler.
8911                 (setq id (concat "none+"
8912                                  (int-to-string
8913                                   (setq gnus-newsgroup-none-id
8914                                         (1+ gnus-newsgroup-none-id)))))))
8915             ;; References.
8916             (progn
8917               (goto-char p)
8918               (if (search-forward "\nreferences: " nil t)
8919                   (prog1
8920                       (gnus-header-value)
8921                     (setq end (match-end 0))
8922                     (save-excursion
8923                       (setq ref
8924                             (downcase
8925                              (buffer-substring
8926                               (progn
8927                                 (end-of-line)
8928                                 (search-backward ">" end t)
8929                                 (1+ (point)))
8930                               (progn
8931                                 (search-backward "<" end t)
8932                                 (point)))))))
8933                 ;; Get the references from the in-reply-to header if there
8934                 ;; were no references and the in-reply-to header looks
8935                 ;; promising.
8936                 (if (and (search-forward "\nin-reply-to: " nil t)
8937                          (setq in-reply-to (gnus-header-value))
8938                          (string-match "<[^>]+>" in-reply-to))
8939                     (prog1
8940                         (setq ref (substring in-reply-to (match-beginning 0)
8941                                              (match-end 0)))
8942                       (setq ref (downcase ref))))
8943                 (setq ref "")))
8944             ;; Chars.
8945             0
8946             ;; Lines.
8947             (progn
8948               (goto-char p)
8949               (if (search-forward "\nlines: " nil t)
8950                   (if (numberp (setq lines (read cur)))
8951                       lines 0)
8952                 0))
8953             ;; Xref.
8954             (progn
8955               (goto-char p)
8956               (and (search-forward "\nxref: " nil t)
8957                    (gnus-header-value)))))
8958           ;; We do the threading while we read the headers.  The
8959           ;; message-id and the last reference are both entered into
8960           ;; the same hash table.  Some tippy-toeing around has to be
8961           ;; done in case an article has arrived before the article
8962           ;; which it refers to.
8963           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8964               (if (and (car (symbol-value id-dep))
8965                        (not force-new))
8966                   ;; An article with this Message-ID has already
8967                   ;; been seen, so we ignore this one, except we add
8968                   ;; any additional Xrefs (in case the two articles
8969                   ;; came from different servers).
8970                   (progn
8971                     (mail-header-set-xref
8972                      (car (symbol-value id-dep))
8973                      (concat (or (mail-header-xref
8974                                   (car (symbol-value id-dep))) "")
8975                              (or (mail-header-xref header) "")))
8976                     (setq header nil))
8977                 (setcar (symbol-value id-dep) header))
8978             (set id-dep (list header)))
8979           (when header
8980             (if (boundp (setq ref-dep (intern ref dependencies)))
8981                 (setcdr (symbol-value ref-dep)
8982                         (nconc (cdr (symbol-value ref-dep))
8983                                (list (symbol-value id-dep))))
8984               (set ref-dep (list nil (symbol-value id-dep))))
8985             (setq headers (cons header headers)))
8986           (goto-char (point-max))
8987           (widen))
8988         (nreverse headers)))))
8989
8990 ;; The following macros and functions were written by Felix Lee
8991 ;; <flee@cse.psu.edu>.
8992
8993 (defmacro gnus-nov-read-integer ()
8994   '(prog1
8995        (if (= (following-char) ?\t)
8996            0
8997          (let ((num (condition-case nil (read buffer) (error nil))))
8998            (if (numberp num) num 0)))
8999      (or (eobp) (forward-char 1))))
9000
9001 (defmacro gnus-nov-skip-field ()
9002   '(search-forward "\t" eol 'move))
9003
9004 (defmacro gnus-nov-field ()
9005   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9006
9007 ;; Goes through the xover lines and returns a list of vectors
9008 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9009   "Parse the news overview data in the server buffer, and return a
9010 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9011   ;; Get the Xref when the users reads the articles since most/some
9012   ;; NNTP servers do not include Xrefs when using XOVER.
9013   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9014   (let ((cur nntp-server-buffer)
9015         (dependencies gnus-newsgroup-dependencies)
9016         number headers header)
9017     (save-excursion
9018       (set-buffer nntp-server-buffer)
9019       ;; Allow the user to mangle the headers before parsing them.
9020       (run-hooks 'gnus-parse-headers-hook)
9021       ;; Allow the user to mangle the headers before parsing them.
9022       (run-hooks 'gnus-parse-headers-hook)
9023       (goto-char (point-min))
9024       (while (and sequence (not (eobp)))
9025         (setq number (read cur))
9026         (while (and sequence (< (car sequence) number))
9027           (setq sequence (cdr sequence)))
9028         (and sequence
9029              (eq number (car sequence))
9030              (progn
9031                (setq sequence (cdr sequence))
9032                (if (setq header
9033                          (inline (gnus-nov-parse-line
9034                                   number dependencies force-new)))
9035                    (setq headers (cons header headers)))))
9036         (forward-line 1))
9037       (setq headers (nreverse headers)))
9038     headers))
9039
9040 ;; This function has to be called with point after the article number
9041 ;; on the beginning of the line.
9042 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9043   (let ((none 0)
9044         (eol (gnus-point-at-eol))
9045         (buffer (current-buffer))
9046         header ref id id-dep ref-dep)
9047
9048     ;; overview: [num subject from date id refs chars lines misc]
9049     (narrow-to-region (point) eol)
9050     (or (eobp) (forward-char))
9051
9052     (condition-case nil
9053         (setq header
9054               (vector
9055                number                   ; number
9056                (gnus-nov-field)         ; subject
9057                (gnus-nov-field)         ; from
9058                (gnus-nov-field)         ; date
9059                (setq id (or (gnus-nov-field)
9060                             (concat "none+"
9061                                     (int-to-string
9062                                      (setq none (1+ none)))))) ; id
9063                (progn
9064                  (save-excursion
9065                    (let ((beg (point)))
9066                      (search-forward "\t" eol)
9067                      (if (search-backward ">" beg t)
9068                          (setq ref
9069                                (downcase
9070                                 (buffer-substring
9071                                  (1+ (point))
9072                                  (progn
9073                                    (search-backward "<" beg t)
9074                                    (point)))))
9075                        (setq ref nil))))
9076                  (gnus-nov-field))      ; refs
9077                (gnus-nov-read-integer)  ; chars
9078                (gnus-nov-read-integer)  ; lines
9079                (if (= (following-char) ?\n)
9080                    nil
9081                  (gnus-nov-field))      ; misc
9082                ))
9083       (error (progn
9084                (ding)
9085                (gnus-message 4 "Strange nov line")
9086                (setq header nil)
9087                (goto-char eol))))
9088
9089     (widen)
9090
9091     ;; We build the thread tree.
9092     (when header
9093       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9094           (if (and (car (symbol-value id-dep))
9095                    (not force-new))
9096               ;; An article with this Message-ID has already been seen,
9097               ;; so we ignore this one, except we add any additional
9098               ;; Xrefs (in case the two articles came from different
9099               ;; servers.
9100               (progn
9101                 (mail-header-set-xref
9102                  (car (symbol-value id-dep))
9103                  (concat (or (mail-header-xref
9104                               (car (symbol-value id-dep))) "")
9105                          (or (mail-header-xref header) "")))
9106                 (setq header nil))
9107             (setcar (symbol-value id-dep) header))
9108         (set id-dep (list header))))
9109     (if header
9110         (progn
9111           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9112               (setcdr (symbol-value ref-dep)
9113                       (nconc (cdr (symbol-value ref-dep))
9114                              (list (symbol-value id-dep))))
9115             (set ref-dep (list nil (symbol-value id-dep))))))
9116     header))
9117
9118 (defun gnus-article-get-xrefs ()
9119   "Fill in the Xref value in `gnus-current-headers', if necessary.
9120 This is meant to be called in `gnus-article-internal-prepare-hook'."
9121   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9122                                  gnus-current-headers)))
9123     (or (not gnus-use-cross-reference)
9124         (not headers)
9125         (and (mail-header-xref headers)
9126              (not (string= (mail-header-xref headers) "")))
9127         (let ((case-fold-search t)
9128               xref)
9129           (save-restriction
9130             (nnheader-narrow-to-headers)
9131             (goto-char (point-min))
9132             (if (or (and (eq (downcase (following-char)) ?x)
9133                          (looking-at "Xref:"))
9134                     (search-forward "\nXref:" nil t))
9135                 (progn
9136                   (goto-char (1+ (match-end 0)))
9137                   (setq xref (buffer-substring (point)
9138                                                (progn (end-of-line) (point))))
9139                   (mail-header-set-xref headers xref))))))))
9140
9141 (defun gnus-summary-insert-subject (id)
9142   "Find article ID and insert the summary line for that article."
9143   (let ((header (gnus-read-header id))
9144         (number (and (numberp id) id)))
9145     (when header
9146       ;; Rebuild the thread that this article is part of and go to the
9147       ;; article we have fetched.
9148       (gnus-rebuild-thread (mail-header-id header))
9149       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9150     (when (and (numberp number)
9151                (> number 0))
9152       ;; We have to update the boundaries even if we can't fetch the
9153       ;; article if ID is a number -- so that the next `P' or `N'
9154       ;; command will fetch the previous (or next) article even
9155       ;; if the one we tried to fetch this time has been canceled.
9156       (and (> number gnus-newsgroup-end)
9157            (setq gnus-newsgroup-end number))
9158       (and (< number gnus-newsgroup-begin)
9159            (setq gnus-newsgroup-begin number))
9160       (setq gnus-newsgroup-unselected
9161             (delq number gnus-newsgroup-unselected)))
9162     ;; Report back a success?
9163     (and header number)))
9164
9165 (defun gnus-summary-work-articles (n)
9166   "Return a list of articles to be worked upon.  The prefix argument,
9167 the list of process marked articles, and the current article will be
9168 taken into consideration."
9169   (cond
9170    ((and n (numberp n))
9171     ;; A numerical prefix has been given.
9172     (let ((backward (< n 0))
9173           (n (abs n))
9174           articles article)
9175       (save-excursion
9176         (while
9177             (and (> n 0)
9178                  (push (setq article (gnus-summary-article-number))
9179                        articles)
9180                  (if backward
9181                      (gnus-summary-find-prev nil article)
9182                    (gnus-summary-find-next nil article)))
9183           (decf n)))
9184       (nreverse articles)))
9185    ((and (boundp 'transient-mark-mode)
9186          transient-mark-mode
9187          mark-active)
9188     ;; Work on the region between point and mark.
9189     (let ((max (max (point) (mark)))
9190           articles article)
9191       (save-excursion
9192         (goto-char (min (point) (mark)))
9193         (while
9194             (and
9195              (push (setq article (gnus-summary-article-number)) articles)
9196              (gnus-summary-find-next nil article)
9197              (< (point) max)))
9198         (nreverse articles))))
9199    (gnus-newsgroup-processable
9200     ;; There are process-marked articles present.
9201     (reverse gnus-newsgroup-processable))
9202    (t
9203     ;; Just return the current article.
9204     (list (gnus-summary-article-number)))))
9205
9206 (defun gnus-summary-search-group (&optional backward use-level)
9207   "Search for next unread newsgroup.
9208 If optional argument BACKWARD is non-nil, search backward instead."
9209   (save-excursion
9210     (set-buffer gnus-group-buffer)
9211     (if (gnus-group-search-forward
9212          backward nil (if use-level (gnus-group-group-level) nil))
9213         (gnus-group-group-name))))
9214
9215 (defun gnus-summary-best-group (&optional exclude-group)
9216   "Find the name of the best unread group.
9217 If EXCLUDE-GROUP, do not go to this group."
9218   (save-excursion
9219     (set-buffer gnus-group-buffer)
9220     (save-excursion
9221       (gnus-group-best-unread-group exclude-group))))
9222
9223 (defun gnus-summary-find-next (&optional unread article backward)
9224   (if backward (gnus-summary-find-prev)
9225     (let* ((article (or article (gnus-summary-article-number)))
9226            (arts (gnus-data-find-list article))
9227            result)
9228       (when (or (not gnus-summary-check-current)
9229                 (not unread)
9230                 (not (gnus-data-unread-p (car arts))))
9231         (setq arts (cdr arts)))
9232       (when (setq result
9233                   (if unread
9234                       (progn
9235                         (while arts
9236                           (when (gnus-data-unread-p (car arts))
9237                             (setq result (car arts)
9238                                   arts nil))
9239                           (setq arts (cdr arts)))
9240                         result)
9241                     (car arts)))
9242         (goto-char (gnus-data-pos result))
9243         (gnus-data-number result)))))
9244
9245 (defun gnus-summary-find-prev (&optional unread article)
9246   (let* ((article (or article (gnus-summary-article-number)))
9247          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9248          result)
9249     (when (or (not gnus-summary-check-current)
9250               (not unread)
9251               (not (gnus-data-unread-p (car arts))))
9252       (setq arts (cdr arts)))
9253     (if (setq result
9254               (if unread
9255                   (progn
9256                     (while arts
9257                       (and (gnus-data-unread-p (car arts))
9258                            (setq result (car arts)
9259                                  arts nil))
9260                       (setq arts (cdr arts)))
9261                     result)
9262                 (car arts)))
9263         (progn
9264           (goto-char (gnus-data-pos result))
9265           (gnus-data-number result)))))
9266
9267 (defun gnus-summary-find-subject (subject &optional unread backward article)
9268   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9269          (article (or article (gnus-summary-article-number)))
9270          (articles (gnus-data-list backward))
9271          (arts (gnus-data-find-list article articles))
9272          result)
9273     (when (or (not gnus-summary-check-current)
9274               (not unread)
9275               (not (gnus-data-unread-p (car arts))))
9276       (setq arts (cdr arts)))
9277     (while arts
9278       (and (or (not unread)
9279                (gnus-data-unread-p (car arts)))
9280            (vectorp (gnus-data-header (car arts)))
9281            (gnus-subject-equal
9282             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9283            (setq result (car arts)
9284                  arts nil))
9285       (setq arts (cdr arts)))
9286     (and result
9287          (goto-char (gnus-data-pos result))
9288          (gnus-data-number result))))
9289
9290 (defun gnus-summary-search-forward (&optional unread subject backward)
9291   "Search forward for an article.
9292 If UNREAD, look for unread articles.  If SUBJECT, look for
9293 articles with that subject.  If BACKWARD, search backward instead."
9294   (cond (subject (gnus-summary-find-subject subject unread backward))
9295         (backward (gnus-summary-find-prev unread))
9296         (t (gnus-summary-find-next unread))))
9297
9298 (defun gnus-recenter (&optional n)
9299   "Center point in window and redisplay frame.
9300 Also do horizontal recentering."
9301   (interactive)
9302   (gnus-horizontal-recenter)
9303   (recenter n))
9304
9305 (defun gnus-summary-recenter ()
9306   "Center point in the summary window.
9307 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9308 displayed, no centering will be performed."
9309   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9310   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9311   (let* ((top (cond ((< (window-height) 4) 0)
9312                     ((< (window-height) 7) 1)
9313                     (t 2)))
9314          (height (1- (window-height)))
9315          (bottom (save-excursion (goto-char (point-max))
9316                                  (forward-line (- height))
9317                                  (point)))
9318          (window (get-buffer-window (current-buffer))))
9319     ;; The user has to want it.
9320     (when gnus-auto-center-summary
9321       (when (get-buffer-window gnus-article-buffer)
9322        ;; Only do recentering when the article buffer is displayed,
9323        ;; Set the window start to either `bottom', which is the biggest
9324        ;; possible valid number, or the second line from the top,
9325        ;; whichever is the least.
9326        (set-window-start
9327         window (min bottom (save-excursion 
9328                              (forward-line (- top)) (point)))))
9329       ;; Do horizontal recentering while we're at it.
9330       (when (get-buffer-window (current-buffer) t)
9331         (let ((selected (selected-window)))
9332           (select-window (get-buffer-window (current-buffer) t))
9333           (gnus-summary-position-point)
9334           (gnus-horizontal-recenter)
9335           (select-window selected))))))
9336
9337 (defun gnus-horizontal-recenter ()
9338   "Recenter the current buffer horizontally."
9339   (if (< (current-column) (/ (window-width) 2))
9340       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9341     (let* ((orig (point))
9342            (end (window-end (get-buffer-window (current-buffer) t)))
9343            (max 0))
9344       ;; Find the longest line currently displayed in the window.
9345       (goto-char (window-start))
9346       (while (and (not (eobp)) 
9347                   (< (point) end))
9348         (end-of-line)
9349         (setq max (max max (current-column)))
9350         (forward-line 1))
9351       (goto-char orig)
9352       ;; Scroll horizontally to center (sort of) the point.
9353       (if (> max (window-width))
9354           (set-window-hscroll 
9355            (get-buffer-window (current-buffer) t)
9356            (min (- (current-column) (/ (window-width) 3))
9357                 (+ 2 (- max (window-width)))))
9358         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9359       max)))
9360
9361 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9362 (defun gnus-short-group-name (group &optional levels)
9363   "Collapse GROUP name LEVELS."
9364   (let* ((name "") 
9365          (foreign "")
9366          (depth 0) 
9367          (skip 1)
9368          (levels (or levels
9369                      (progn
9370                        (while (string-match "\\." group skip)
9371                          (setq skip (match-end 0)
9372                                depth (+ depth 1)))
9373                        depth))))
9374     (if (string-match ":" group)
9375         (setq foreign (substring group 0 (match-end 0))
9376               group (substring group (match-end 0))))
9377     (while group
9378       (if (and (string-match "\\." group)
9379                (> levels (- gnus-group-uncollapsed-levels 1)))
9380           (setq name (concat name (substring group 0 1))
9381                 group (substring group (match-end 0))
9382                 levels (- levels 1)
9383                 name (concat name "."))
9384         (setq name (concat foreign name group)
9385               group nil)))
9386     name))
9387
9388 (defun gnus-summary-jump-to-group (newsgroup)
9389   "Move point to NEWSGROUP in group mode buffer."
9390   ;; Keep update point of group mode buffer if visible.
9391   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9392       (save-window-excursion
9393         ;; Take care of tree window mode.
9394         (if (get-buffer-window gnus-group-buffer)
9395             (pop-to-buffer gnus-group-buffer))
9396         (gnus-group-jump-to-group newsgroup))
9397     (save-excursion
9398       ;; Take care of tree window mode.
9399       (if (get-buffer-window gnus-group-buffer)
9400           (pop-to-buffer gnus-group-buffer)
9401         (set-buffer gnus-group-buffer))
9402       (gnus-group-jump-to-group newsgroup))))
9403
9404 ;; This function returns a list of article numbers based on the
9405 ;; difference between the ranges of read articles in this group and
9406 ;; the range of active articles.
9407 (defun gnus-list-of-unread-articles (group)
9408   (let* ((read (gnus-info-read (gnus-get-info group)))
9409          (active (gnus-active group))
9410          (last (cdr active))
9411          first nlast unread)
9412     ;; If none are read, then all are unread.
9413     (if (not read)
9414         (setq first (car active))
9415       ;; If the range of read articles is a single range, then the
9416       ;; first unread article is the article after the last read
9417       ;; article.  Sounds logical, doesn't it?
9418       (if (not (listp (cdr read)))
9419           (setq first (1+ (cdr read)))
9420         ;; `read' is a list of ranges.
9421         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9422                                 (car (car read)))) 1)
9423             (setq first 1))
9424         (while read
9425           (if first
9426               (while (< first nlast)
9427                 (setq unread (cons first unread))
9428                 (setq first (1+ first))))
9429           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9430           (setq nlast (if (atom (car (cdr read)))
9431                           (car (cdr read))
9432                         (car (car (cdr read)))))
9433           (setq read (cdr read)))))
9434     ;; And add the last unread articles.
9435     (while (<= first last)
9436       (setq unread (cons first unread))
9437       (setq first (1+ first)))
9438     ;; Return the list of unread articles.
9439     (nreverse unread)))
9440
9441 (defun gnus-list-of-read-articles (group)
9442   "Return a list of unread, unticked and non-dormant articles."
9443   (let* ((info (gnus-get-info group))
9444          (marked (gnus-info-marks info))
9445          (active (gnus-active group)))
9446     (and info active
9447          (gnus-set-difference
9448           (gnus-sorted-complement
9449            (gnus-uncompress-range active)
9450            (gnus-list-of-unread-articles group))
9451           (append
9452            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9453            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9454
9455 ;; Various summary commands
9456
9457 (defun gnus-summary-universal-argument (arg)
9458   "Perform any operation on all articles that are process/prefixed."
9459   (interactive "P")
9460   (gnus-set-global-variables)
9461   (let ((articles (gnus-summary-work-articles arg))
9462         func article)
9463     (if (eq
9464          (setq
9465           func
9466           (key-binding
9467            (read-key-sequence
9468             (substitute-command-keys
9469              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9470              ))))
9471          'undefined)
9472         (progn
9473           (message "Undefined key")
9474           (ding))
9475       (save-excursion
9476         (while articles
9477           (gnus-summary-goto-subject (setq article (pop articles)))
9478           (command-execute func)
9479           (gnus-summary-remove-process-mark article)))))
9480   (gnus-summary-position-point))
9481
9482 (defun gnus-summary-toggle-truncation (&optional arg)
9483   "Toggle truncation of summary lines.
9484 With arg, turn line truncation on iff arg is positive."
9485   (interactive "P")
9486   (setq truncate-lines
9487         (if (null arg) (not truncate-lines)
9488           (> (prefix-numeric-value arg) 0)))
9489   (redraw-display))
9490
9491 (defun gnus-summary-reselect-current-group (&optional all rescan)
9492   "Exit and then reselect the current newsgroup.
9493 The prefix argument ALL means to select all articles."
9494   (interactive "P")
9495   (gnus-set-global-variables)
9496   (let ((current-subject (gnus-summary-article-number))
9497         (group gnus-newsgroup-name))
9498     (setq gnus-newsgroup-begin nil)
9499     (gnus-summary-exit)
9500     ;; We have to adjust the point of group mode buffer because the
9501     ;; current point was moved to the next unread newsgroup by
9502     ;; exiting.
9503     (gnus-summary-jump-to-group group)
9504     (when rescan
9505       (save-excursion
9506         (gnus-group-get-new-news-this-group 1)))
9507     (gnus-group-read-group all t)
9508     (gnus-summary-goto-subject current-subject)))
9509
9510 (defun gnus-summary-rescan-group (&optional all)
9511   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9512   (interactive "P")
9513   (gnus-summary-reselect-current-group all t))
9514
9515 (defun gnus-summary-update-info ()
9516   (let* ((group gnus-newsgroup-name))
9517     (when gnus-newsgroup-kill-headers
9518       (setq gnus-newsgroup-killed
9519             (gnus-compress-sequence
9520              (nconc
9521               (gnus-set-sorted-intersection
9522                (gnus-uncompress-range gnus-newsgroup-killed)
9523                (setq gnus-newsgroup-unselected
9524                      (sort gnus-newsgroup-unselected '<)))
9525               (setq gnus-newsgroup-unreads
9526                     (sort gnus-newsgroup-unreads '<))) t)))
9527     (unless (listp (cdr gnus-newsgroup-killed))
9528       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9529     (let ((headers gnus-newsgroup-headers))
9530       (gnus-close-group group)
9531       (run-hooks 'gnus-exit-group-hook)
9532       (unless gnus-save-score
9533         (setq gnus-newsgroup-scored nil))
9534       ;; Set the new ranges of read articles.
9535       (gnus-update-read-articles
9536        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9537       ;; Set the current article marks.
9538       (gnus-update-marks)
9539       ;; Do the cross-ref thing.
9540       (when gnus-use-cross-reference
9541         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9542       ;; Do adaptive scoring, and possibly save score files.
9543       (when gnus-newsgroup-adaptive
9544         (gnus-score-adaptive))
9545       (when gnus-use-scoring
9546         (gnus-score-save))
9547       ;; Do not switch windows but change the buffer to work.
9548       (set-buffer gnus-group-buffer)
9549       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9550           (gnus-group-update-group group)))))
9551
9552 (defun gnus-summary-exit (&optional temporary)
9553   "Exit reading current newsgroup, and then return to group selection mode.
9554 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9555   (interactive)
9556   (gnus-set-global-variables)
9557   (gnus-kill-save-kill-buffer)
9558   (let* ((group gnus-newsgroup-name)
9559          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9560          (mode major-mode)
9561          (buf (current-buffer)))
9562     (run-hooks 'gnus-summary-prepare-exit-hook)
9563     ;; If we have several article buffers, we kill them at exit.
9564     (unless gnus-single-article-buffer
9565       (gnus-kill-buffer gnus-article-buffer)
9566       (gnus-kill-buffer gnus-original-article-buffer))
9567     (when gnus-use-cache
9568       (gnus-cache-possibly-remove-articles)
9569       (gnus-cache-save-buffers))
9570     (when gnus-use-trees
9571       (gnus-tree-close group))
9572     ;; Make all changes in this group permanent.
9573     (unless quit-config
9574       (gnus-summary-update-info))
9575     ;; Make sure where I was, and go to next newsgroup.
9576     (set-buffer gnus-group-buffer)
9577     (unless quit-config
9578       (gnus-group-jump-to-group group)
9579       (gnus-group-next-unread-group 1))
9580     (run-hooks 'gnus-summary-exit-hook)
9581     (if temporary
9582         nil                             ;Nothing to do.
9583       ;; If we have several article buffers, we kill them at exit.
9584       (unless gnus-single-article-buffer
9585         (gnus-kill-buffer gnus-article-buffer)
9586         (gnus-kill-buffer gnus-original-article-buffer))
9587       (set-buffer buf)
9588       (if (not gnus-kill-summary-on-exit)
9589           (gnus-deaden-summary)
9590         ;; We set all buffer-local variables to nil.  It is unclear why
9591         ;; this is needed, but if we don't, buffer-local variables are
9592         ;; not garbage-collected, it seems.  This would the lead to en
9593         ;; ever-growing Emacs.
9594         (gnus-summary-clear-local-variables)
9595         (when (get-buffer gnus-article-buffer)
9596           (bury-buffer gnus-article-buffer))
9597         ;; We clear the global counterparts of the buffer-local
9598         ;; variables as well, just to be on the safe side.
9599         (gnus-configure-windows 'group 'force)
9600         (gnus-summary-clear-local-variables)
9601         ;; Return to group mode buffer.
9602         (if (eq mode 'gnus-summary-mode)
9603             (gnus-kill-buffer buf)))
9604       (setq gnus-current-select-method gnus-select-method)
9605       (pop-to-buffer gnus-group-buffer)
9606       ;; Clear the current group name.
9607       (if (not quit-config)
9608           (progn
9609             (gnus-group-jump-to-group group)
9610             (gnus-group-next-unread-group 1)
9611             (gnus-configure-windows 'group 'force))
9612         (if (not (buffer-name (car quit-config)))
9613             (gnus-configure-windows 'group 'force)
9614           (set-buffer (car quit-config))
9615           (and (eq major-mode 'gnus-summary-mode)
9616                (gnus-set-global-variables))
9617           (gnus-configure-windows (cdr quit-config))))
9618       (unless quit-config
9619         (setq gnus-newsgroup-name nil)))))
9620
9621 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9622 (defun gnus-summary-exit-no-update (&optional no-questions)
9623   "Quit reading current newsgroup without updating read article info."
9624   (interactive)
9625   (gnus-set-global-variables)
9626   (let* ((group gnus-newsgroup-name)
9627          (quit-config (gnus-group-quit-config group)))
9628     (when (or no-questions
9629               gnus-expert-user
9630               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9631       ;; If we have several article buffers, we kill them at exit.
9632       (unless gnus-single-article-buffer
9633         (gnus-kill-buffer gnus-article-buffer)
9634         (gnus-kill-buffer gnus-original-article-buffer))
9635       (if (not gnus-kill-summary-on-exit)
9636           (gnus-deaden-summary)
9637         (gnus-close-group group)
9638         (gnus-summary-clear-local-variables)
9639         (set-buffer gnus-group-buffer)
9640         (gnus-summary-clear-local-variables)
9641         (when (get-buffer gnus-summary-buffer)
9642           (kill-buffer gnus-summary-buffer)))
9643       (when gnus-use-trees
9644         (gnus-tree-close group))
9645       (when (get-buffer gnus-article-buffer)
9646         (bury-buffer gnus-article-buffer))
9647       ;; Return to the group buffer.
9648       (gnus-configure-windows 'group 'force)
9649       ;; Clear the current group name.
9650       (setq gnus-newsgroup-name nil)
9651       (when (equal (gnus-group-group-name) group)
9652         (gnus-group-next-unread-group 1))
9653       (when quit-config
9654         (if (not (buffer-name (car quit-config)))
9655             (gnus-configure-windows 'group 'force)
9656           (set-buffer (car quit-config))
9657           (when (eq major-mode 'gnus-summary-mode)
9658             (gnus-set-global-variables))
9659           (gnus-configure-windows (cdr quit-config)))))))
9660
9661 ;;; Dead summaries.
9662
9663 (defvar gnus-dead-summary-mode-map nil)
9664
9665 (if gnus-dead-summary-mode-map
9666     nil
9667   (setq gnus-dead-summary-mode-map (make-keymap))
9668   (suppress-keymap gnus-dead-summary-mode-map)
9669   (substitute-key-definition
9670    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9671   (let ((keys '("\C-d" "\r" "\177")))
9672     (while keys
9673       (define-key gnus-dead-summary-mode-map
9674         (pop keys) 'gnus-summary-wake-up-the-dead))))
9675
9676 (defvar gnus-dead-summary-mode nil
9677   "Minor mode for Gnus summary buffers.")
9678
9679 (defun gnus-dead-summary-mode (&optional arg)
9680   "Minor mode for Gnus summary buffers."
9681   (interactive "P")
9682   (when (eq major-mode 'gnus-summary-mode)
9683     (make-local-variable 'gnus-dead-summary-mode)
9684     (setq gnus-dead-summary-mode
9685           (if (null arg) (not gnus-dead-summary-mode)
9686             (> (prefix-numeric-value arg) 0)))
9687     (when gnus-dead-summary-mode
9688       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9689         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9690       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9691         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9692               minor-mode-map-alist)))))
9693
9694 (defun gnus-deaden-summary ()
9695   "Make the current summary buffer into a dead summary buffer."
9696   ;; Kill any previous dead summary buffer.
9697   (when (and gnus-dead-summary
9698              (buffer-name gnus-dead-summary))
9699     (save-excursion
9700       (set-buffer gnus-dead-summary)
9701       (when gnus-dead-summary-mode
9702         (kill-buffer (current-buffer)))))
9703   ;; Make this the current dead summary.
9704   (setq gnus-dead-summary (current-buffer))
9705   (gnus-dead-summary-mode 1)
9706   (let ((name (buffer-name)))
9707     (when (string-match "Summary" name)
9708       (rename-buffer
9709        (concat (substring name 0 (match-beginning 0)) "Dead "
9710                (substring name (match-beginning 0))) t))))
9711
9712 (defun gnus-kill-or-deaden-summary (buffer)
9713   "Kill or deaden the summary BUFFER."
9714   (cond (gnus-kill-summary-on-exit
9715          (when (and gnus-use-trees
9716                     (and (get-buffer buffer)
9717                          (buffer-name (get-buffer buffer))))
9718            (save-excursion
9719              (set-buffer (get-buffer buffer))
9720              (gnus-tree-close gnus-newsgroup-name)))
9721          (gnus-kill-buffer buffer))
9722         ((and (get-buffer buffer)
9723               (buffer-name (get-buffer buffer)))
9724          (save-excursion
9725            (set-buffer buffer)
9726            (gnus-deaden-summary)))))
9727
9728 (defun gnus-summary-wake-up-the-dead (&rest args)
9729   "Wake up the dead summary buffer."
9730   (interactive)
9731   (gnus-dead-summary-mode -1)
9732   (let ((name (buffer-name)))
9733     (when (string-match "Dead " name)
9734       (rename-buffer
9735        (concat (substring name 0 (match-beginning 0))
9736                (substring name (match-end 0))) t)))
9737   (gnus-message 3 "This dead summary is now alive again"))
9738
9739 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9740 (defun gnus-summary-fetch-faq (&optional faq-dir)
9741   "Fetch the FAQ for the current group.
9742 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9743 in."
9744   (interactive
9745    (list
9746     (if current-prefix-arg
9747         (completing-read
9748          "Faq dir: " (and (listp gnus-group-faq-directory)
9749                           gnus-group-faq-directory)))))
9750   (let (gnus-faq-buffer)
9751     (and (setq gnus-faq-buffer
9752                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9753          (gnus-configure-windows 'summary-faq))))
9754
9755 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9756 (defun gnus-summary-describe-group (&optional force)
9757   "Describe the current newsgroup."
9758   (interactive "P")
9759   (gnus-group-describe-group force gnus-newsgroup-name))
9760
9761 (defun gnus-summary-describe-briefly ()
9762   "Describe summary mode commands briefly."
9763   (interactive)
9764   (gnus-message 6
9765                 (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")))
9766
9767 ;; Walking around group mode buffer from summary mode.
9768
9769 (defun gnus-summary-next-group (&optional no-article target-group backward)
9770   "Exit current newsgroup and then select next unread newsgroup.
9771 If prefix argument NO-ARTICLE is non-nil, no article is selected
9772 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9773 previous group instead."
9774   (interactive "P")
9775   (gnus-set-global-variables)
9776   (let ((current-group gnus-newsgroup-name)
9777         (current-buffer (current-buffer))
9778         entered)
9779     ;; First we semi-exit this group to update Xrefs and all variables.
9780     ;; We can't do a real exit, because the window conf must remain
9781     ;; the same in case the user is prompted for info, and we don't
9782     ;; want the window conf to change before that...
9783     (gnus-summary-exit t)
9784     (while (not entered)
9785       ;; Then we find what group we are supposed to enter.
9786       (set-buffer gnus-group-buffer)
9787       (gnus-group-jump-to-group current-group)
9788       (setq target-group
9789             (or target-group
9790                 (if (eq gnus-keep-same-level 'best)
9791                     (gnus-summary-best-group gnus-newsgroup-name)
9792                   (gnus-summary-search-group backward gnus-keep-same-level))))
9793       (if (not target-group)
9794           ;; There are no further groups, so we return to the group
9795           ;; buffer.
9796           (progn
9797             (gnus-message 5 "Returning to the group buffer")
9798             (setq entered t)
9799             (set-buffer current-buffer)
9800             (gnus-summary-exit))
9801         ;; We try to enter the target group.
9802         (gnus-group-jump-to-group target-group)
9803         (let ((unreads (gnus-group-group-unread)))
9804           (if (and (or (eq t unreads)
9805                        (and unreads (not (zerop unreads))))
9806                    (gnus-summary-read-group
9807                     target-group nil no-article current-buffer))
9808               (setq entered t)
9809             (setq current-group target-group
9810                   target-group nil)))))))
9811
9812 (defun gnus-summary-prev-group (&optional no-article)
9813   "Exit current newsgroup and then select previous unread newsgroup.
9814 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9815   (interactive "P")
9816   (gnus-summary-next-group no-article nil t))
9817
9818 ;; Walking around summary lines.
9819
9820 (defun gnus-summary-first-subject (&optional unread)
9821   "Go to the first unread subject.
9822 If UNREAD is non-nil, go to the first unread article.
9823 Returns the article selected or nil if there are no unread articles."
9824   (interactive "P")
9825   (prog1
9826       (cond
9827        ;; Empty summary.
9828        ((null gnus-newsgroup-data)
9829         (gnus-message 3 "No articles in the group")
9830         nil)
9831        ;; Pick the first article.
9832        ((not unread)
9833         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9834         (gnus-data-number (car gnus-newsgroup-data)))
9835        ;; No unread articles.
9836        ((null gnus-newsgroup-unreads)
9837         (gnus-message 3 "No more unread articles")
9838         nil)
9839        ;; Find the first unread article.
9840        (t
9841         (let ((data gnus-newsgroup-data))
9842           (while (and data
9843                       (not (gnus-data-unread-p (car data))))
9844             (setq data (cdr data)))
9845           (if data
9846               (progn
9847                 (goto-char (gnus-data-pos (car data)))
9848                 (gnus-data-number (car data)))))))
9849     (gnus-summary-position-point)))
9850
9851 (defun gnus-summary-next-subject (n &optional unread dont-display)
9852   "Go to next N'th summary line.
9853 If N is negative, go to the previous N'th subject line.
9854 If UNREAD is non-nil, only unread articles are selected.
9855 The difference between N and the actual number of steps taken is
9856 returned."
9857   (interactive "p")
9858   (let ((backward (< n 0))
9859         (n (abs n)))
9860     (while (and (> n 0)
9861                 (if backward
9862                     (gnus-summary-find-prev unread)
9863                   (gnus-summary-find-next unread)))
9864       (setq n (1- n)))
9865     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9866                                (if unread " unread" "")))
9867     (or dont-display
9868         (progn
9869           (gnus-summary-recenter)
9870           (gnus-summary-position-point)))
9871     n))
9872
9873 (defun gnus-summary-next-unread-subject (n)
9874   "Go to next N'th unread summary line."
9875   (interactive "p")
9876   (gnus-summary-next-subject n t))
9877
9878 (defun gnus-summary-prev-subject (n &optional unread)
9879   "Go to previous N'th summary line.
9880 If optional argument UNREAD is non-nil, only unread article is selected."
9881   (interactive "p")
9882   (gnus-summary-next-subject (- n) unread))
9883
9884 (defun gnus-summary-prev-unread-subject (n)
9885   "Go to previous N'th unread summary line."
9886   (interactive "p")
9887   (gnus-summary-next-subject (- n) t))
9888
9889 (defun gnus-summary-goto-subject (article &optional force silent)
9890   "Go the subject line of ARTICLE.
9891 If FORCE, also allow jumping to articles not currently shown."
9892   (let ((b (point))
9893         (data (gnus-data-find article)))
9894     ;; We read in the article if we have to.
9895     (and (not data)
9896          force
9897          (gnus-summary-insert-subject article)
9898          (setq data (gnus-data-find article)))
9899     (goto-char b)
9900     (if (not data)
9901         (progn
9902           (unless silent
9903             (gnus-message 3 "Can't find article %d" article))
9904           nil)
9905       (goto-char (gnus-data-pos data))
9906       article)))
9907
9908 ;; Walking around summary lines with displaying articles.
9909
9910 (defun gnus-summary-expand-window (&optional arg)
9911   "Make the summary buffer take up the entire Emacs frame.
9912 Given a prefix, will force an `article' buffer configuration."
9913   (interactive "P")
9914   (gnus-set-global-variables)
9915   (if arg
9916       (gnus-configure-windows 'article 'force)
9917     (gnus-configure-windows 'summary 'force)))
9918
9919 (defun gnus-summary-display-article (article &optional all-header)
9920   "Display ARTICLE in article buffer."
9921   (gnus-set-global-variables)
9922   (if (null article)
9923       nil
9924     (prog1
9925         (if gnus-summary-display-article-function
9926             (funcall gnus-summary-display-article-function article all-header)
9927           (gnus-article-prepare article all-header))
9928       (run-hooks 'gnus-select-article-hook)
9929       (gnus-summary-recenter)
9930       (gnus-summary-goto-subject article)
9931       (when gnus-use-trees
9932         (gnus-possibly-generate-tree article)
9933         (gnus-highlight-selected-tree article))
9934       ;; Successfully display article.
9935       (gnus-article-set-window-start
9936        (cdr (assq article gnus-newsgroup-bookmarks)))
9937       t)))
9938
9939 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9940   "Select the current article.
9941 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9942 non-nil, the article will be re-fetched even if it already present in
9943 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9944 be displayed."
9945   (let ((article (or article (gnus-summary-article-number)))
9946         (all-headers (not (not all-headers))) ;Must be T or NIL.
9947         gnus-summary-display-article-function
9948         did)
9949     (and (not pseudo)
9950          (gnus-summary-article-pseudo-p article)
9951          (error "This is a pseudo-article."))
9952     (prog1
9953         (save-excursion
9954           (set-buffer gnus-summary-buffer)
9955           (if (or (and gnus-single-article-buffer
9956                        (or (null gnus-current-article)
9957                            (null gnus-article-current)
9958                            (null (get-buffer gnus-article-buffer))
9959                            (not (eq article (cdr gnus-article-current)))
9960                            (not (equal (car gnus-article-current)
9961                                        gnus-newsgroup-name))))
9962                   (and (not gnus-single-article-buffer)
9963                        (null gnus-current-article))
9964                   force)
9965               ;; The requested article is different from the current article.
9966               (prog1
9967                   (gnus-summary-display-article article all-headers)
9968                 (setq did article))
9969             (if (or all-headers gnus-show-all-headers)
9970                 (gnus-article-show-all-headers))
9971             'old))
9972       (if did
9973           (gnus-article-set-window-start
9974            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9975
9976 (defun gnus-summary-set-current-mark (&optional current-mark)
9977   "Obsolete function."
9978   nil)
9979
9980 (defun gnus-summary-next-article (&optional unread subject backward push)
9981   "Select the next article.
9982 If UNREAD, only unread articles are selected.
9983 If SUBJECT, only articles with SUBJECT are selected.
9984 If BACKWARD, the previous article is selected instead of the next."
9985   (interactive "P")
9986   (gnus-set-global-variables)
9987   (let (header)
9988     (cond
9989      ;; Is there such an article?
9990      ((and (gnus-summary-search-forward unread subject backward)
9991            (or (gnus-summary-display-article (gnus-summary-article-number))
9992                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9993       (gnus-summary-position-point))
9994      ;; If not, we try the first unread, if that is wanted.
9995      ((and subject
9996            gnus-auto-select-same
9997            (or (gnus-summary-first-unread-article)
9998                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9999       (gnus-summary-position-point)
10000       (gnus-message 6 "Wrapped"))
10001      ;; Try to get next/previous article not displayed in this group.
10002      ((and gnus-auto-extend-newsgroup
10003            (not unread) (not subject))
10004       (gnus-summary-goto-article
10005        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10006        nil t))
10007      ;; Go to next/previous group.
10008      (t
10009       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10010           (gnus-summary-jump-to-group gnus-newsgroup-name))
10011       (let ((cmd last-command-char)
10012             (group
10013              (if (eq gnus-keep-same-level 'best)
10014                  (gnus-summary-best-group gnus-newsgroup-name)
10015                (gnus-summary-search-group backward gnus-keep-same-level))))
10016         ;; For some reason, the group window gets selected.  We change
10017         ;; it back.
10018         (select-window (get-buffer-window (current-buffer)))
10019         ;; Select next unread newsgroup automagically.
10020         (cond
10021          ((not gnus-auto-select-next)
10022           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10023          ((or (eq gnus-auto-select-next 'quietly)
10024               (and (eq gnus-auto-select-next 'slightly-quietly)
10025                    push)
10026               (and (eq gnus-auto-select-next 'almost-quietly)
10027                    (gnus-summary-last-article-p)))
10028           ;; Select quietly.
10029           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10030               (gnus-summary-exit)
10031             (gnus-message 7 "No more%s articles (%s)..."
10032                           (if unread " unread" "")
10033                           (if group (concat "selecting " group)
10034                             "exiting"))
10035             (gnus-summary-next-group nil group backward)))
10036          (t
10037           (gnus-summary-walk-group-buffer
10038            gnus-newsgroup-name cmd unread backward))))))))
10039
10040 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10041   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10042                       (?\C-p (gnus-group-prev-unread-group 1))))
10043         keve key group ended)
10044     (save-excursion
10045       (set-buffer gnus-group-buffer)
10046       (gnus-summary-jump-to-group from-group)
10047       (setq group
10048             (if (eq gnus-keep-same-level 'best)
10049                 (gnus-summary-best-group gnus-newsgroup-name)
10050               (gnus-summary-search-group backward gnus-keep-same-level))))
10051     (while (not ended)
10052       (gnus-message
10053        5 "No more%s articles%s" (if unread " unread" "")
10054        (if (and group
10055                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10056            (format " (Type %s for %s [%s])"
10057                    (single-key-description cmd) group
10058                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10059          (format " (Type %s to exit %s)"
10060                  (single-key-description cmd)
10061                  gnus-newsgroup-name)))
10062       ;; Confirm auto selection.
10063       (setq key (car (setq keve (gnus-read-event-char))))
10064       (setq ended t)
10065       (cond
10066        ((assq key keystrokes)
10067         (let ((obuf (current-buffer)))
10068           (switch-to-buffer gnus-group-buffer)
10069           (and group
10070                (gnus-group-jump-to-group group))
10071           (eval (car (cdr (assq key keystrokes))))
10072           (setq group (gnus-group-group-name))
10073           (switch-to-buffer obuf))
10074         (setq ended nil))
10075        ((equal key cmd)
10076         (if (or (not group)
10077                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10078             (gnus-summary-exit)
10079           (gnus-summary-next-group nil group backward)))
10080        (t
10081         (push (cdr keve) unread-command-events))))))
10082
10083 (defun gnus-read-event-char ()
10084   "Get the next event."
10085   (let ((event (read-event)))
10086     (cons (and (numberp event) event) event)))
10087
10088 (defun gnus-summary-next-unread-article ()
10089   "Select unread article after current one."
10090   (interactive)
10091   (gnus-summary-next-article t (and gnus-auto-select-same
10092                                     (gnus-summary-article-subject))))
10093
10094 (defun gnus-summary-prev-article (&optional unread subject)
10095   "Select the article after the current one.
10096 If UNREAD is non-nil, only unread articles are selected."
10097   (interactive "P")
10098   (gnus-summary-next-article unread subject t))
10099
10100 (defun gnus-summary-prev-unread-article ()
10101   "Select unred article before current one."
10102   (interactive)
10103   (gnus-summary-prev-article t (and gnus-auto-select-same
10104                                     (gnus-summary-article-subject))))
10105
10106 (defun gnus-summary-next-page (&optional lines circular)
10107   "Show next page of the selected article.
10108 If at the end of the current article, select the next article.
10109 LINES says how many lines should be scrolled up.
10110
10111 If CIRCULAR is non-nil, go to the start of the article instead of
10112 selecting the next article when reaching the end of the current
10113 article."
10114   (interactive "P")
10115   (setq gnus-summary-buffer (current-buffer))
10116   (gnus-set-global-variables)
10117   (let ((article (gnus-summary-article-number))
10118         (endp nil))
10119     (gnus-configure-windows 'article)
10120     (if (or (null gnus-current-article)
10121             (null gnus-article-current)
10122             (/= article (cdr gnus-article-current))
10123             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10124         ;; Selected subject is different from current article's.
10125         (gnus-summary-display-article article)
10126       (gnus-eval-in-buffer-window
10127        gnus-article-buffer
10128        (setq endp (gnus-article-next-page lines)))
10129       (if endp
10130           (cond (circular
10131                  (gnus-summary-beginning-of-article))
10132                 (lines
10133                  (gnus-message 3 "End of message"))
10134                 ((null lines)
10135                  (if (and (eq gnus-summary-goto-unread 'never)
10136                           (not (eq article gnus-newsgroup-end)))
10137                      (gnus-summary-next-article)
10138                    (gnus-summary-next-unread-article))))))
10139     (gnus-summary-recenter)
10140     (gnus-summary-position-point)))
10141
10142 (defun gnus-summary-prev-page (&optional lines)
10143   "Show previous page of selected article.
10144 Argument LINES specifies lines to be scrolled down."
10145   (interactive "P")
10146   (gnus-set-global-variables)
10147   (let ((article (gnus-summary-article-number)))
10148     (gnus-configure-windows 'article)
10149     (if (or (null gnus-current-article)
10150             (null gnus-article-current)
10151             (/= article (cdr gnus-article-current))
10152             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10153         ;; Selected subject is different from current article's.
10154         (gnus-summary-display-article article)
10155       (gnus-summary-recenter)
10156       (gnus-eval-in-buffer-window gnus-article-buffer
10157                                   (gnus-article-prev-page lines))))
10158   (gnus-summary-position-point))
10159
10160 (defun gnus-summary-scroll-up (lines)
10161   "Scroll up (or down) one line current article.
10162 Argument LINES specifies lines to be scrolled up (or down if negative)."
10163   (interactive "p")
10164   (gnus-set-global-variables)
10165   (gnus-configure-windows 'article)
10166   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10167     (gnus-eval-in-buffer-window
10168      gnus-article-buffer
10169      (cond ((> lines 0)
10170             (if (gnus-article-next-page lines)
10171                 (gnus-message 3 "End of message")))
10172            ((< lines 0)
10173             (gnus-article-prev-page (- lines))))))
10174   (gnus-summary-recenter)
10175   (gnus-summary-position-point))
10176
10177 (defun gnus-summary-next-same-subject ()
10178   "Select next article which has the same subject as current one."
10179   (interactive)
10180   (gnus-set-global-variables)
10181   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10182
10183 (defun gnus-summary-prev-same-subject ()
10184   "Select previous article which has the same subject as current one."
10185   (interactive)
10186   (gnus-set-global-variables)
10187   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10188
10189 (defun gnus-summary-next-unread-same-subject ()
10190   "Select next unread article which has the same subject as current one."
10191   (interactive)
10192   (gnus-set-global-variables)
10193   (gnus-summary-next-article t (gnus-summary-article-subject)))
10194
10195 (defun gnus-summary-prev-unread-same-subject ()
10196   "Select previous unread article which has the same subject as current one."
10197   (interactive)
10198   (gnus-set-global-variables)
10199   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10200
10201 (defun gnus-summary-first-unread-article ()
10202   "Select the first unread article.
10203 Return nil if there are no unread articles."
10204   (interactive)
10205   (gnus-set-global-variables)
10206   (prog1
10207       (if (gnus-summary-first-subject t)
10208           (progn
10209             (gnus-summary-show-thread)
10210             (gnus-summary-first-subject t)
10211             (gnus-summary-display-article (gnus-summary-article-number))))
10212     (gnus-summary-position-point)))
10213
10214 (defun gnus-summary-best-unread-article ()
10215   "Select the unread article with the highest score."
10216   (interactive)
10217   (gnus-set-global-variables)
10218   (let ((best -1000000)
10219         (data gnus-newsgroup-data)
10220         article score)
10221     (while data
10222       (and (gnus-data-unread-p (car data))
10223            (> (setq score
10224                     (gnus-summary-article-score (gnus-data-number (car data))))
10225               best)
10226            (setq best score
10227                  article (gnus-data-number (car data))))
10228       (setq data (cdr data)))
10229     (if article
10230         (gnus-summary-goto-article article)
10231       (error "No unread articles"))
10232     (gnus-summary-position-point)))
10233
10234 (defun gnus-summary-last-subject ()
10235   "Go to the last displayed subject line in the group."
10236   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10237     (when article
10238       (gnus-summary-goto-subject article))))
10239
10240 (defun gnus-summary-goto-article (article &optional all-headers force)
10241   "Fetch ARTICLE and display it if it exists.
10242 If ALL-HEADERS is non-nil, no header lines are hidden."
10243   (interactive
10244    (list
10245     (string-to-int
10246      (completing-read
10247       "Article number: "
10248       (mapcar (lambda (number) (list (int-to-string number)))
10249               gnus-newsgroup-limit)))
10250     current-prefix-arg
10251     t))
10252   (prog1
10253       (if (gnus-summary-goto-subject article force)
10254           (gnus-summary-display-article article all-headers)
10255         (gnus-message 4 "Couldn't go to article %s" article) nil)
10256     (gnus-summary-position-point)))
10257
10258 (defun gnus-summary-goto-last-article ()
10259   "Go to the previously read article."
10260   (interactive)
10261   (prog1
10262       (and gnus-last-article
10263            (gnus-summary-goto-article gnus-last-article))
10264     (gnus-summary-position-point)))
10265
10266 (defun gnus-summary-pop-article (number)
10267   "Pop one article off the history and go to the previous.
10268 NUMBER articles will be popped off."
10269   (interactive "p")
10270   (let (to)
10271     (setq gnus-newsgroup-history
10272           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10273     (if to
10274         (gnus-summary-goto-article (car to))
10275       (error "Article history empty")))
10276   (gnus-summary-position-point))
10277
10278 ;; Summary commands and functions for limiting the summary buffer.
10279
10280 (defun gnus-summary-limit-to-articles (n)
10281   "Limit the summary buffer to the next N articles.
10282 If not given a prefix, use the process marked articles instead."
10283   (interactive "P")
10284   (gnus-set-global-variables)
10285   (prog1
10286       (let ((articles (gnus-summary-work-articles n)))
10287         (setq gnus-newsgroup-processable nil)
10288         (gnus-summary-limit articles))
10289     (gnus-summary-position-point)))
10290
10291 (defun gnus-summary-pop-limit (&optional total)
10292   "Restore the previous limit.
10293 If given a prefix, remove all limits."
10294   (interactive "P")
10295   (gnus-set-global-variables)
10296   (when total 
10297     (setq gnus-newsgroup-limits
10298           (list (mapcar (lambda (h) (mail-header-number h))
10299                         gnus-newsgroup-headers))))
10300   (unless gnus-newsgroup-limits
10301     (error "No limit to pop"))
10302   (prog1
10303       (gnus-summary-limit nil 'pop)
10304     (gnus-summary-position-point)))
10305
10306 (defun gnus-summary-limit-to-subject (subject &optional header)
10307   "Limit the summary buffer to articles that have subjects that match a regexp."
10308   (interactive "sRegexp: ")
10309   (unless header
10310     (setq header "subject"))
10311   (when (not (equal "" subject))
10312     (prog1
10313         (let ((articles (gnus-summary-find-matching
10314                          (or header "subject") subject 'all)))
10315           (or articles (error "Found no matches for \"%s\"" subject))
10316           (gnus-summary-limit articles))
10317       (gnus-summary-position-point))))
10318
10319 (defun gnus-summary-limit-to-author (from)
10320   "Limit the summary buffer to articles that have authors that match a regexp."
10321   (interactive "sRegexp: ")
10322   (gnus-summary-limit-to-subject from "from"))
10323
10324 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10325 (make-obsolete
10326  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10327
10328 (defun gnus-summary-limit-to-unread (&optional all)
10329   "Limit the summary buffer to articles that are not marked as read.
10330 If ALL is non-nil, limit strictly to unread articles."
10331   (interactive "P")
10332   (if all
10333       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10334     (gnus-summary-limit-to-marks
10335      ;; Concat all the marks that say that an article is read and have
10336      ;; those removed.
10337      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10338            gnus-killed-mark gnus-kill-file-mark
10339            gnus-low-score-mark gnus-expirable-mark
10340            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10341      'reverse)))
10342
10343 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10344 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10345
10346 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10347   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10348 If REVERSE, limit the summary buffer to articles that are not marked
10349 with MARKS.  MARKS can either be a string of marks or a list of marks.
10350 Returns how many articles were removed."
10351   (interactive "sMarks: ")
10352   (gnus-set-global-variables)
10353   (prog1
10354       (let ((data gnus-newsgroup-data)
10355             (marks (if (listp marks) marks
10356                      (append marks nil))) ; Transform to list.
10357             articles)
10358         (while data
10359           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10360                  (memq (gnus-data-mark (car data)) marks))
10361                (setq articles (cons (gnus-data-number (car data)) articles)))
10362           (setq data (cdr data)))
10363         (gnus-summary-limit articles))
10364     (gnus-summary-position-point)))
10365
10366 (defun gnus-summary-limit-to-score (&optional score)
10367   "Limit to articles with score at or above SCORE."
10368   (interactive "P")
10369   (gnus-set-global-variables)
10370   (setq score (if score
10371                   (prefix-numeric-value score)
10372                 (or gnus-summary-default-score 0)))
10373   (let ((data gnus-newsgroup-data)
10374         articles)
10375     (while data
10376       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10377                 score)
10378         (push (gnus-data-number (car data)) articles))
10379       (setq data (cdr data)))
10380     (prog1
10381         (gnus-summary-limit articles)
10382       (gnus-summary-position-point))))
10383
10384 (defun gnus-summary-limit-include-dormant ()
10385   "Display all the hidden articles that are marked as dormant."
10386   (interactive)
10387   (gnus-set-global-variables)
10388   (or gnus-newsgroup-dormant
10389       (error "There are no dormant articles in this group"))
10390   (prog1
10391       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10392     (gnus-summary-position-point)))
10393
10394 (defun gnus-summary-limit-exclude-dormant ()
10395   "Hide all dormant articles."
10396   (interactive)
10397   (gnus-set-global-variables)
10398   (prog1
10399       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10400     (gnus-summary-position-point)))
10401
10402 (defun gnus-summary-limit-exclude-childless-dormant ()
10403   "Hide all dormant articles that have no children."
10404   (interactive)
10405   (gnus-set-global-variables)
10406   (let ((data (gnus-data-list t))
10407         articles d children)
10408     ;; Find all articles that are either not dormant or have
10409     ;; children.
10410     (while (setq d (pop data))
10411       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10412                 (and (setq children 
10413                            (gnus-article-children (gnus-data-number d)))
10414                      (let (found)
10415                        (while children
10416                          (when (memq (car children) articles)
10417                            (setq children nil
10418                                  found t))
10419                          (pop children))
10420                        found)))
10421         (push (gnus-data-number d) articles)))
10422     ;; Do the limiting.
10423     (prog1
10424         (gnus-summary-limit articles)
10425       (gnus-summary-position-point))))
10426
10427 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10428   "Mark all unread excluded articles as read.
10429 If ALL, mark even excluded ticked and dormants as read."
10430   (interactive "P")
10431   (let ((articles (gnus-sorted-complement
10432                    (sort
10433                     (mapcar (lambda (h) (mail-header-number h))
10434                             gnus-newsgroup-headers)
10435                     '<)
10436                    (sort gnus-newsgroup-limit '<)))
10437         article)
10438     (setq gnus-newsgroup-unreads nil)
10439     (if all
10440         (setq gnus-newsgroup-dormant nil
10441               gnus-newsgroup-marked nil
10442               gnus-newsgroup-reads
10443               (nconc
10444                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10445                gnus-newsgroup-reads))
10446       (while (setq article (pop articles))
10447         (unless (or (memq article gnus-newsgroup-dormant)
10448                     (memq article gnus-newsgroup-marked))
10449           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10450
10451 (defun gnus-summary-limit (articles &optional pop)
10452   (if pop
10453       ;; We pop the previous limit off the stack and use that.
10454       (setq articles (car gnus-newsgroup-limits)
10455             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10456     ;; We use the new limit, so we push the old limit on the stack.
10457     (setq gnus-newsgroup-limits
10458           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10459   ;; Set the limit.
10460   (setq gnus-newsgroup-limit articles)
10461   (let ((total (length gnus-newsgroup-data))
10462         (data (gnus-data-find-list (gnus-summary-article-number)))
10463         found)
10464     ;; This will do all the work of generating the new summary buffer
10465     ;; according to the new limit.
10466     (gnus-summary-prepare)
10467     ;; Hide any threads, possibly.
10468     (and gnus-show-threads
10469          gnus-thread-hide-subtree
10470          (gnus-summary-hide-all-threads))
10471     ;; Try to return to the article you were at, or one in the
10472     ;; neighborhood.
10473     (if data
10474         ;; We try to find some article after the current one.
10475         (while data
10476           (and (gnus-summary-goto-subject
10477                 (gnus-data-number (car data)) nil t)
10478                (setq data nil
10479                      found t))
10480           (setq data (cdr data))))
10481     (or found
10482         ;; If there is no data, that means that we were after the last
10483         ;; article.  The same goes when we can't find any articles
10484         ;; after the current one.
10485         (progn
10486           (goto-char (point-max))
10487           (gnus-summary-find-prev)))
10488     ;; We return how many articles were removed from the summary
10489     ;; buffer as a result of the new limit.
10490     (- total (length gnus-newsgroup-data))))
10491
10492 (defsubst gnus-cut-thread (thread)
10493   "Go forwards in the thread until we find an article that we want to display."
10494   (if (eq gnus-fetch-old-headers 'some)
10495       (while (and thread
10496                   (memq (mail-header-number (car thread)) 
10497                         gnus-newsgroup-ancient)
10498                   (<= (length (cdr thread)) 1))
10499         (setq thread (cadr thread)))
10500     (while (and thread
10501                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10502                 (= (length (cdr thread)) 1))
10503       (setq thread (cadr thread))))
10504   thread)
10505
10506 (defun gnus-cut-threads (threads)
10507   "Cut off all uninteresting articles from the beginning of threads."
10508   (when (or (eq gnus-fetch-old-headers 'some)
10509             (eq gnus-build-sparse-threads 'some)
10510             (eq gnus-build-sparse-threads 'more))
10511     (let ((th threads))
10512       (while th
10513         (setcar th (gnus-cut-thread (car th)))
10514         (setq th (cdr th)))))
10515   threads)
10516
10517 (defun gnus-summary-initial-limit (&optional show-if-empty)
10518   "Figure out what the initial limit is supposed to be on group entry.
10519 This entails weeding out unwanted dormants, low-scored articles,
10520 fetch-old-headers verbiage, and so on."
10521   ;; Most groups have nothing to remove.
10522   (if (or gnus-inhibit-limiting
10523           (and (null gnus-newsgroup-dormant)
10524                (not (eq gnus-fetch-old-headers 'some))
10525                (null gnus-summary-expunge-below)
10526                (not (eq gnus-build-sparse-threads 'some))
10527                (not (eq gnus-build-sparse-threads 'more))
10528                (null gnus-thread-expunge-below)
10529                (not gnus-use-nocem)))
10530       () ; Do nothing.
10531     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10532     (setq gnus-newsgroup-limit nil)
10533     (mapatoms
10534      (lambda (node)
10535        (unless (car (symbol-value node))
10536          ;; These threads have no parents -- they are roots.
10537          (let ((nodes (cdr (symbol-value node)))
10538                thread)
10539            (while nodes
10540              (if (and gnus-thread-expunge-below
10541                       (< (gnus-thread-total-score (car nodes))
10542                          gnus-thread-expunge-below))
10543                  (gnus-expunge-thread (pop nodes))
10544                (setq thread (pop nodes))
10545                (gnus-summary-limit-children thread))))))
10546      gnus-newsgroup-dependencies)
10547     ;; If this limitation resulted in an empty group, we might
10548     ;; pop the previous limit and use it instead.
10549     (when (and (not gnus-newsgroup-limit)
10550                show-if-empty)
10551       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10552     gnus-newsgroup-limit))
10553
10554 (defun gnus-summary-limit-children (thread)
10555   "Return 1 if this subthread is visible and 0 if it is not."
10556   ;; First we get the number of visible children to this thread.  This
10557   ;; is done by recursing down the thread using this function, so this
10558   ;; will really go down to a leaf article first, before slowly
10559   ;; working its way up towards the root.
10560   (when thread
10561     (let ((children
10562            (if (cdr thread)
10563                (apply '+ (mapcar 'gnus-summary-limit-children
10564                                  (cdr thread)))
10565              0))
10566           (number (mail-header-number (car thread)))
10567           score)
10568       (if (or
10569            ;; If this article is dormant and has absolutely no visible
10570            ;; children, then this article isn't visible.
10571            (and (memq number gnus-newsgroup-dormant)
10572                 (= children 0))
10573            ;; If this is a "fetch-old-headered" and there is only one
10574            ;; visible child (or less), then we don't want this article.
10575            (and (eq gnus-fetch-old-headers 'some)
10576                 (memq number gnus-newsgroup-ancient)
10577                 (zerop children))
10578            ;; If this is a sparsely inserted article with no children,
10579            ;; we don't want it.
10580            (and (eq gnus-build-sparse-threads 'some)
10581                 (memq number gnus-newsgroup-sparse)
10582                 (zerop children))
10583            ;; If we use expunging, and this article is really
10584            ;; low-scored, then we don't want this article.
10585            (when (and gnus-summary-expunge-below
10586                       (< (setq score
10587                                (or (cdr (assq number gnus-newsgroup-scored))
10588                                    gnus-summary-default-score))
10589                          gnus-summary-expunge-below))
10590              ;; We increase the expunge-tally here, but that has
10591              ;; nothing to do with the limits, really.
10592              (incf gnus-newsgroup-expunged-tally)
10593              ;; We also mark as read here, if that's wanted.
10594              (when (and gnus-summary-mark-below
10595                         (< score gnus-summary-mark-below))
10596                (setq gnus-newsgroup-unreads
10597                      (delq number gnus-newsgroup-unreads))
10598                (if gnus-newsgroup-auto-expire
10599                    (push number gnus-newsgroup-expirable)
10600                  (push (cons number gnus-low-score-mark)
10601                        gnus-newsgroup-reads)))
10602              t)
10603            (and gnus-use-nocem
10604                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10605           ;; Nope, invisible article.
10606           0
10607         ;; Ok, this article is to be visible, so we add it to the limit
10608         ;; and return 1.
10609         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10610         1))))
10611
10612 (defun gnus-expunge-thread (thread)
10613   "Mark all articles in THREAD as read."
10614   (let* ((number (mail-header-number (car thread))))
10615     (incf gnus-newsgroup-expunged-tally)
10616     ;; We also mark as read here, if that's wanted.
10617     (setq gnus-newsgroup-unreads
10618           (delq number gnus-newsgroup-unreads))
10619     (if gnus-newsgroup-auto-expire
10620         (push number gnus-newsgroup-expirable)
10621       (push (cons number gnus-low-score-mark)
10622             gnus-newsgroup-reads)))
10623   ;; Go recursively through all subthreads.
10624   (mapcar 'gnus-expunge-thread (cdr thread)))
10625
10626 ;; Summary article oriented commands
10627
10628 (defun gnus-summary-refer-parent-article (n)
10629   "Refer parent article N times.
10630 The difference between N and the number of articles fetched is returned."
10631   (interactive "p")
10632   (gnus-set-global-variables)
10633   (while
10634       (and
10635        (> n 0)
10636        (let* ((header (gnus-summary-article-header))
10637               (ref
10638                ;; If we try to find the parent of the currently
10639                ;; displayed article, then we take a look at the actual
10640                ;; References header, since this is slightly more
10641                ;; reliable than the References field we got from the
10642                ;; server.
10643                (if (and (eq (mail-header-number header)
10644                             (cdr gnus-article-current))
10645                         (equal gnus-newsgroup-name
10646                                (car gnus-article-current)))
10647                    (save-excursion
10648                      (set-buffer gnus-original-article-buffer)
10649                      (nnheader-narrow-to-headers)
10650                      (prog1
10651                          (mail-fetch-field "references")
10652                        (widen)))
10653                  ;; It's not the current article, so we take a bet on
10654                  ;; the value we got from the server.
10655                  (mail-header-references header))))
10656          (if (setq ref (or ref (mail-header-references header)))
10657              (or (gnus-summary-refer-article (gnus-parent-id ref))
10658                  (gnus-message 1 "Couldn't find parent"))
10659            (gnus-message 1 "No references in article %d"
10660                          (gnus-summary-article-number))
10661            nil)))
10662     (setq n (1- n)))
10663   (gnus-summary-position-point)
10664   n)
10665
10666 (defun gnus-summary-refer-references ()
10667   "Fetch all articles mentioned in the References header.
10668 Return how many articles were fetched."
10669   (interactive)
10670   (gnus-set-global-variables)
10671   (let ((ref (mail-header-references (gnus-summary-article-header)))
10672         (current (gnus-summary-article-number))
10673         (n 0))
10674     ;; For each Message-ID in the References header...
10675     (while (string-match "<[^>]*>" ref)
10676       (incf n)
10677       ;; ... fetch that article.
10678       (gnus-summary-refer-article
10679        (prog1 (match-string 0 ref)
10680          (setq ref (substring ref (match-end 0))))))
10681     (gnus-summary-goto-subject current)
10682     (gnus-summary-position-point)
10683     n))
10684
10685 (defun gnus-summary-refer-article (message-id)
10686   "Fetch an article specified by MESSAGE-ID."
10687   (interactive "sMessage-ID: ")
10688   (when (and (stringp message-id)
10689              (not (zerop (length message-id))))
10690     ;; Construct the correct Message-ID if necessary.
10691     ;; Suggested by tale@pawl.rpi.edu.
10692     (unless (string-match "^<" message-id)
10693       (setq message-id (concat "<" message-id)))
10694     (unless (string-match ">$" message-id)
10695       (setq message-id (concat message-id ">")))
10696     (let ((header (car (gnus-gethash (downcase message-id)
10697                                      gnus-newsgroup-dependencies))))
10698       (if header
10699           ;; The article is present in the buffer, to we just go to it.
10700           (gnus-summary-goto-article (mail-header-number header) nil t)
10701         ;; We fetch the article
10702         (let ((gnus-override-method gnus-refer-article-method)
10703               number)
10704           ;; Start the special refer-article method, if necessary.
10705           (when gnus-refer-article-method
10706             (gnus-check-server gnus-refer-article-method))
10707           ;; Fetch the header, and display the article.
10708           (if (setq number (gnus-summary-insert-subject message-id))
10709               (gnus-summary-select-article nil nil nil number)
10710             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10711
10712 (defun gnus-summary-enter-digest-group (&optional force)
10713   "Enter a digest group based on the current article."
10714   (interactive "P")
10715   (gnus-set-global-variables)
10716   (gnus-summary-select-article)
10717   (let ((name (format "%s-%d"
10718                       (gnus-group-prefixed-name
10719                        gnus-newsgroup-name (list 'nndoc ""))
10720                       gnus-current-article))
10721         (ogroup gnus-newsgroup-name)
10722         (case-fold-search t)
10723         (buf (current-buffer))
10724         dig)
10725     (save-excursion
10726       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10727       (insert-buffer-substring gnus-original-article-buffer)
10728       (narrow-to-region
10729        (goto-char (point-min))
10730        (or (search-forward "\n\n" nil t) (point)))
10731       (goto-char (point-min))
10732       (delete-matching-lines "^\\(Path\\):\\|^From ")
10733       (widen))
10734     (unwind-protect
10735         (if (gnus-group-read-ephemeral-group
10736              name `(nndoc ,name (nndoc-address
10737                                  ,(get-buffer dig))
10738                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10739             ;; Make all postings to this group go to the parent group.
10740             (nconc (gnus-info-params (gnus-get-info name))
10741                    (list (cons 'to-group ogroup)))
10742           ;; Couldn't select this doc group.
10743           (switch-to-buffer buf)
10744           (gnus-set-global-variables)
10745           (gnus-configure-windows 'summary)
10746           (gnus-message 3 "Article couldn't be entered?"))
10747       (kill-buffer dig))))
10748
10749 (defun gnus-summary-isearch-article (&optional regexp-p)
10750   "Do incremental search forward on the current article.
10751 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10752   (interactive "P")
10753   (gnus-set-global-variables)
10754   (gnus-summary-select-article)
10755   (gnus-configure-windows 'article)
10756   (gnus-eval-in-buffer-window
10757    gnus-article-buffer
10758    (goto-char (point-min))
10759    (isearch-forward regexp-p)))
10760
10761 (defun gnus-summary-search-article-forward (regexp &optional backward)
10762   "Search for an article containing REGEXP forward.
10763 If BACKWARD, search backward instead."
10764   (interactive
10765    (list (read-string
10766           (format "Search article %s (regexp%s): "
10767                   (if current-prefix-arg "backward" "forward")
10768                   (if gnus-last-search-regexp
10769                       (concat ", default " gnus-last-search-regexp)
10770                     "")))
10771          current-prefix-arg))
10772   (gnus-set-global-variables)
10773   (if (string-equal regexp "")
10774       (setq regexp (or gnus-last-search-regexp ""))
10775     (setq gnus-last-search-regexp regexp))
10776   (if (gnus-summary-search-article regexp backward)
10777       (gnus-article-set-window-start
10778        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10779     (error "Search failed: \"%s\"" regexp)))
10780
10781 (defun gnus-summary-search-article-backward (regexp)
10782   "Search for an article containing REGEXP backward."
10783   (interactive
10784    (list (read-string
10785           (format "Search article backward (regexp%s): "
10786                   (if gnus-last-search-regexp
10787                       (concat ", default " gnus-last-search-regexp)
10788                     "")))))
10789   (gnus-summary-search-article-forward regexp 'backward))
10790
10791 (defun gnus-summary-search-article (regexp &optional backward)
10792   "Search for an article containing REGEXP.
10793 Optional argument BACKWARD means do search for backward.
10794 gnus-select-article-hook is not called during the search."
10795   (let ((gnus-select-article-hook nil)  ;Disable hook.
10796         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10797         (re-search
10798          (if backward
10799              (function re-search-backward) (function re-search-forward)))
10800         (found nil)
10801         (last nil))
10802     ;; Hidden thread subtrees must be searched for ,too.
10803     (gnus-summary-show-all-threads)
10804     ;; First of all, search current article.
10805     ;; We don't want to read article again from NNTP server nor reset
10806     ;; current point.
10807     (gnus-summary-select-article)
10808     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10809     (setq last gnus-current-article)
10810     (gnus-eval-in-buffer-window
10811      gnus-article-buffer
10812      (save-restriction
10813        (widen)
10814        ;; Begin search from current point.
10815        (setq found (funcall re-search regexp nil t))))
10816     ;; Then search next articles.
10817     (while (and (not found)
10818                 (gnus-summary-display-article
10819                  (if backward (gnus-summary-find-prev)
10820                    (gnus-summary-find-next))))
10821       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10822       (gnus-eval-in-buffer-window
10823        gnus-article-buffer
10824        (save-restriction
10825          (widen)
10826          (goto-char (if backward (point-max) (point-min)))
10827          (setq found (funcall re-search regexp nil t)))))
10828     (message "")
10829     ;; Adjust article pointer.
10830     (or (eq last gnus-current-article)
10831         (setq gnus-last-article last))
10832     ;; Return T if found such article.
10833     found))
10834
10835 (defun gnus-summary-find-matching (header regexp &optional backward unread
10836                                           not-case-fold)
10837   "Return a list of all articles that match REGEXP on HEADER.
10838 The search stars on the current article and goes forwards unless
10839 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10840 If UNREAD is non-nil, only unread articles will
10841 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10842 in the comparisons."
10843   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10844                 (gnus-data-find-list
10845                  (gnus-summary-article-number) (gnus-data-list backward))))
10846         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10847         (case-fold-search (not not-case-fold))
10848         articles d)
10849     (or (fboundp (intern (concat "mail-header-" header)))
10850         (error "%s is not a valid header" header))
10851     (while data
10852       (setq d (car data))
10853       (and (or (not unread)             ; We want all articles...
10854                (gnus-data-unread-p d))  ; Or just unreads.
10855            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10856            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10857            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10858       (setq data (cdr data)))
10859     (nreverse articles)))
10860
10861 (defun gnus-summary-execute-command (header regexp command &optional backward)
10862   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10863 If HEADER is an empty string (or nil), the match is done on the entire
10864 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10865   (interactive
10866    (list (let ((completion-ignore-case t))
10867            (completing-read
10868             "Header name: "
10869             (mapcar (lambda (string) (list string))
10870                     '("Number" "Subject" "From" "Lines" "Date"
10871                       "Message-ID" "Xref" "References" "Body"))
10872             nil 'require-match))
10873          (read-string "Regexp: ")
10874          (read-key-sequence "Command: ")
10875          current-prefix-arg))
10876   (when (equal header "Body")
10877     (setq header ""))
10878   (gnus-set-global-variables)
10879   ;; Hidden thread subtrees must be searched as well.
10880   (gnus-summary-show-all-threads)
10881   ;; We don't want to change current point nor window configuration.
10882   (save-excursion
10883     (save-window-excursion
10884       (gnus-message 6 "Executing %s..." (key-description command))
10885       ;; We'd like to execute COMMAND interactively so as to give arguments.
10886       (gnus-execute header regexp
10887                     `(lambda () (call-interactively ',(key-binding command)))
10888                     backward)
10889       (gnus-message 6 "Executing %s...done" (key-description command)))))
10890
10891 (defun gnus-summary-beginning-of-article ()
10892   "Scroll the article back to the beginning."
10893   (interactive)
10894   (gnus-set-global-variables)
10895   (gnus-summary-select-article)
10896   (gnus-configure-windows 'article)
10897   (gnus-eval-in-buffer-window
10898    gnus-article-buffer
10899    (widen)
10900    (goto-char (point-min))
10901    (and gnus-break-pages (gnus-narrow-to-page))))
10902
10903 (defun gnus-summary-end-of-article ()
10904   "Scroll to the end of the article."
10905   (interactive)
10906   (gnus-set-global-variables)
10907   (gnus-summary-select-article)
10908   (gnus-configure-windows 'article)
10909   (gnus-eval-in-buffer-window
10910    gnus-article-buffer
10911    (widen)
10912    (goto-char (point-max))
10913    (recenter -3)
10914    (and gnus-break-pages (gnus-narrow-to-page))))
10915
10916 (defun gnus-summary-show-article (&optional arg)
10917   "Force re-fetching of the current article.
10918 If ARG (the prefix) is non-nil, show the raw article without any
10919 article massaging functions being run."
10920   (interactive "P")
10921   (gnus-set-global-variables)
10922   (if (not arg)
10923       ;; Select the article the normal way.
10924       (gnus-summary-select-article nil 'force)
10925     ;; Bind the article treatment functions to nil.
10926     (let ((gnus-have-all-headers t)
10927           gnus-article-display-hook
10928           gnus-article-prepare-hook
10929           gnus-visual)
10930       (gnus-summary-select-article nil 'force)))
10931 ;  (gnus-configure-windows 'article)
10932   (gnus-summary-position-point))
10933
10934 (defun gnus-summary-verbose-headers (&optional arg)
10935   "Toggle permanent full header display.
10936 If ARG is a positive number, turn header display on.
10937 If ARG is a negative number, turn header display off."
10938   (interactive "P")
10939   (gnus-set-global-variables)
10940   (gnus-summary-toggle-header arg)
10941   (setq gnus-show-all-headers
10942         (cond ((or (not (numberp arg))
10943                    (zerop arg))
10944                (not gnus-show-all-headers))
10945               ((natnump arg)
10946                t))))
10947
10948 (defun gnus-summary-toggle-header (&optional arg)
10949   "Show the headers if they are hidden, or hide them if they are shown.
10950 If ARG is a positive number, show the entire header.
10951 If ARG is a negative number, hide the unwanted header lines."
10952   (interactive "P")
10953   (gnus-set-global-variables)
10954   (save-excursion
10955     (set-buffer gnus-article-buffer)
10956     (let* ((buffer-read-only nil)
10957            (inhibit-point-motion-hooks t)
10958            (hidden (text-property-any
10959                     (goto-char (point-min)) (search-forward "\n\n")
10960                     'invisible t))
10961            e)
10962       (goto-char (point-min))
10963       (when (search-forward "\n\n" nil t)
10964         (delete-region (point-min) (1- (point))))
10965       (goto-char (point-min))
10966       (save-excursion
10967         (set-buffer gnus-original-article-buffer)
10968         (goto-char (point-min))
10969         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10970       (insert-buffer-substring gnus-original-article-buffer 1 e)
10971       (let ((gnus-inhibit-hiding t))
10972         (run-hooks 'gnus-article-display-hook))
10973       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10974           (gnus-article-hide-headers)))))
10975
10976 (defun gnus-summary-show-all-headers ()
10977   "Make all header lines visible."
10978   (interactive)
10979   (gnus-set-global-variables)
10980   (gnus-article-show-all-headers))
10981
10982 (defun gnus-summary-toggle-mime (&optional arg)
10983   "Toggle MIME processing.
10984 If ARG is a positive number, turn MIME processing on."
10985   (interactive "P")
10986   (gnus-set-global-variables)
10987   (setq gnus-show-mime
10988         (if (null arg) (not gnus-show-mime)
10989           (> (prefix-numeric-value arg) 0)))
10990   (gnus-summary-select-article t 'force))
10991
10992 (defun gnus-summary-caesar-message (&optional arg)
10993   "Caesar rotate the current article by 13.
10994 The numerical prefix specifies how manu places to rotate each letter
10995 forward."
10996   (interactive "P")
10997   (gnus-set-global-variables)
10998   (gnus-summary-select-article)
10999   (let ((mail-header-separator ""))
11000     (gnus-eval-in-buffer-window
11001      gnus-article-buffer
11002      (save-restriction
11003        (widen)
11004        (let ((start (window-start)))
11005          (news-caesar-buffer-body arg)
11006          (set-window-start (get-buffer-window (current-buffer)) start))))))
11007
11008 (defun gnus-summary-stop-page-breaking ()
11009   "Stop page breaking in the current article."
11010   (interactive)
11011   (gnus-set-global-variables)
11012   (gnus-summary-select-article)
11013   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11014
11015 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11016   "Move the current article to a different newsgroup.
11017 If N is a positive number, move the N next articles.
11018 If N is a negative number, move the N previous articles.
11019 If N is nil and any articles have been marked with the process mark,
11020 move those articles instead.
11021 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11022 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11023 re-spool using this method.
11024
11025 For this function to work, both the current newsgroup and the
11026 newsgroup that you want to move to have to support the `request-move'
11027 and `request-accept' functions."
11028   (interactive "P")
11029   (unless action (setq action 'move))
11030   (gnus-set-global-variables)
11031   ;; Check whether the source group supports the required functions.
11032   (cond ((and (eq action 'move)
11033               (not (gnus-check-backend-function
11034                     'request-move-article gnus-newsgroup-name)))
11035          (error "The current group does not support article moving"))
11036         ((and (eq action 'crosspost)
11037               (not (gnus-check-backend-function
11038                     'request-replace-article gnus-newsgroup-name)))
11039          (error "The current group does not support article editing")))
11040   (let ((articles (gnus-summary-work-articles n))
11041         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11042         (names '((move "move" "Moving")
11043                  (copy "copy" "Copying")
11044                  (crosspost "crosspost" "Crossposting")))
11045         (copy-buf (save-excursion
11046                     (nnheader-set-temp-buffer " *copy article*")))
11047         art-group to-method new-xref article)
11048     (unless (assq action names)
11049       (error "Unknown action %s" action))
11050     ;; Read the newsgroup name.
11051     (when (and (not to-newsgroup)
11052                (not select-method))
11053       (setq to-newsgroup
11054             (gnus-read-move-group-name
11055              (cadr (assq action names))
11056              gnus-current-move-group articles prefix))
11057       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11058     (setq to-method (if select-method (list select-method "")
11059                       (gnus-find-method-for-group to-newsgroup)))
11060     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11061     ;;(error "Can't %s to the same group you're already in" action))
11062     ;; Check the method we are to move this article to...
11063     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11064         (error "%s does not support article copying" (car to-method)))
11065     (or (gnus-check-server to-method)
11066         (error "Can't open server %s" (car to-method)))
11067     (gnus-message 6 "%s to %s: %s..."
11068                   (caddr (assq action names))
11069                   (or select-method to-newsgroup) articles)
11070     (while articles
11071       (setq article (pop articles))
11072       (setq
11073        art-group
11074        (cond
11075         ;; Move the article.
11076         ((eq action 'move)
11077          (gnus-request-move-article
11078           article                       ; Article to move
11079           gnus-newsgroup-name           ; From newsgrouo
11080           (nth 1 (gnus-find-method-for-group
11081                   gnus-newsgroup-name)) ; Server
11082           (list 'gnus-request-accept-article
11083                 (if select-method
11084                     (list 'quote select-method)
11085                   to-newsgroup)
11086                 (not articles))         ; Accept form
11087           (not articles)))              ; Only save nov last time
11088         ;; Copy the article.
11089         ((eq action 'copy)
11090          (save-excursion
11091            (set-buffer copy-buf)
11092            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11093            (gnus-request-accept-article
11094             (if select-method select-method to-newsgroup)
11095             (not articles))))
11096         ;; Crosspost the article.
11097         ((eq action 'crosspost)
11098          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11099            (setq new-xref (concat gnus-newsgroup-name ":" article))
11100            (if (and xref (not (string= xref "")))
11101                (progn
11102                  (when (string-match "^Xref: " xref)
11103                    (setq xref (substring xref (match-end 0))))
11104                  (setq new-xref (concat xref " " new-xref)))
11105              (setq new-xref (concat (system-name) " " new-xref)))
11106            (save-excursion
11107              (set-buffer copy-buf)
11108              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11109              (nnheader-replace-header "xref" new-xref)
11110              (gnus-request-accept-article
11111               (if select-method select-method to-newsgroup)
11112               (not articles)))))))
11113       (if (not art-group)
11114           (gnus-message 1 "Couldn't %s article %s"
11115                         (cadr (assq action names)) article)
11116         (let* ((entry
11117                 (or
11118                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11119                  (gnus-gethash
11120                   (gnus-group-prefixed-name
11121                    (car art-group)
11122                    (if select-method (list select-method "")
11123                      (gnus-find-method-for-group to-newsgroup)))
11124                   gnus-newsrc-hashtb)))
11125                (info (nth 2 entry)))
11126           ;; Update the group that has been moved to.
11127           (when (and info
11128                      (memq action '(move copy)))
11129             (unless (memq article gnus-newsgroup-unreads)
11130               (gnus-info-set-read
11131                info (gnus-add-to-range (gnus-info-read info)
11132                                        (list (cdr art-group)))))
11133
11134             ;; Copy any marks over to the new group.
11135             (let ((marks gnus-article-mark-lists)
11136                   (to-article (cdr art-group)))
11137
11138               ;; See whether the article is to be put in the cache.
11139               (when gnus-use-cache
11140                 (gnus-cache-possibly-enter-article
11141                  (gnus-info-group info) to-article
11142                  (let ((header (copy-sequence
11143                                 (gnus-summary-article-header article))))
11144                    (mail-header-set-number header to-article)
11145                    header)
11146                  (memq article gnus-newsgroup-marked)
11147                  (memq article gnus-newsgroup-dormant)
11148                  (memq article gnus-newsgroup-unreads)))
11149
11150               (while marks
11151                 (when (memq article (symbol-value
11152                                      (intern (format "gnus-newsgroup-%s"
11153                                                      (caar marks)))))
11154                   (gnus-add-marked-articles
11155                    (gnus-info-group info) (cdar marks)
11156                    (list to-article) info))
11157                 (setq marks (cdr marks)))))
11158
11159           ;; Update the Xref header in this article to point to
11160           ;; the new crossposted article we have just created.
11161           (when (eq action 'crosspost)
11162             (save-excursion
11163               (set-buffer copy-buf)
11164               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11165               (nnheader-replace-header
11166                "xref" (concat new-xref " " (gnus-group-prefixed-name
11167                                             (car art-group) to-method)
11168                               ":" (cdr art-group)))
11169               (gnus-request-replace-article
11170                article gnus-newsgroup-name (current-buffer)))))
11171
11172         (gnus-summary-goto-subject article)
11173         (when (eq action 'move)
11174           (gnus-summary-mark-article article gnus-canceled-mark)))
11175       (gnus-summary-remove-process-mark article))
11176     (gnus-kill-buffer copy-buf)
11177     (gnus-summary-position-point)
11178     (gnus-set-mode-line 'summary)))
11179
11180 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11181   "Move the current article to a different newsgroup.
11182 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11183 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11184 re-spool using this method."
11185   (interactive "P")
11186   (gnus-summary-move-article n nil nil 'copy))
11187
11188 (defun gnus-summary-crosspost-article (&optional n)
11189   "Crosspost the current article to some other group."
11190   (interactive "P")
11191   (gnus-summary-move-article n nil nil 'crosspost))
11192
11193 (defun gnus-summary-respool-article (&optional n respool-method)
11194   "Respool the current article.
11195 The article will be squeezed through the mail spooling process again,
11196 which means that it will be put in some mail newsgroup or other
11197 depending on `nnmail-split-methods'.
11198 If N is a positive number, respool the N next articles.
11199 If N is a negative number, respool the N previous articles.
11200 If N is nil and any articles have been marked with the process mark,
11201 respool those articles instead.
11202
11203 Respooling can be done both from mail groups and \"real\" newsgroups.
11204 In the former case, the articles in question will be moved from the
11205 current group into whatever groups they are destined to.  In the
11206 latter case, they will be copied into the relevant groups."
11207   (interactive "P")
11208   (gnus-set-global-variables)
11209   (let ((respool-methods (gnus-methods-using 'respool))
11210         (methname
11211          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11212     (or respool-method
11213         (setq respool-method
11214               (completing-read
11215                "What method do you want to use when respooling? "
11216                respool-methods nil t methname)))
11217     (or (string= respool-method "")
11218         (if (assoc (symbol-name
11219                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11220                    respool-methods)
11221             (gnus-summary-move-article n nil (intern respool-method))
11222           (gnus-summary-copy-article n nil (intern respool-method))))))
11223
11224 (defun gnus-summary-import-article (file)
11225   "Import a random file into a mail newsgroup."
11226   (interactive "fImport file: ")
11227   (gnus-set-global-variables)
11228   (let ((group gnus-newsgroup-name)
11229         (now (current-time))
11230         atts lines)
11231     (or (gnus-check-backend-function 'request-accept-article group)
11232         (error "%s does not support article importing" group))
11233     (or (file-readable-p file)
11234         (not (file-regular-p file))
11235         (error "Can't read %s" file))
11236     (save-excursion
11237       (set-buffer (get-buffer-create " *import file*"))
11238       (buffer-disable-undo (current-buffer))
11239       (erase-buffer)
11240       (insert-file-contents file)
11241       (goto-char (point-min))
11242       (unless (nnheader-article-p)
11243         ;; This doesn't look like an article, so we fudge some headers.
11244         (setq atts (file-attributes file)
11245               lines (count-lines (point-min) (point-max)))
11246         (insert "From: " (read-string "From: ") "\n"
11247                 "Subject: " (read-string "Subject: ") "\n"
11248                 "Date: " (timezone-make-date-arpa-standard
11249                           (current-time-string (nth 5 atts))
11250                           (current-time-zone now)
11251                           (current-time-zone now)) "\n"
11252                 "Message-ID: " (gnus-inews-message-id) "\n"
11253                 "Lines: " (int-to-string lines) "\n"
11254                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11255       (gnus-request-accept-article group t)
11256       (kill-buffer (current-buffer)))))
11257
11258 (defun gnus-summary-expire-articles ()
11259   "Expire all articles that are marked as expirable in the current group."
11260   (interactive)
11261   (gnus-set-global-variables)
11262   (when (gnus-check-backend-function
11263          'request-expire-articles gnus-newsgroup-name)
11264     ;; This backend supports expiry.
11265     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11266            (expirable (if total
11267                           (gnus-list-of-read-articles gnus-newsgroup-name)
11268                         (setq gnus-newsgroup-expirable
11269                               (sort gnus-newsgroup-expirable '<))))
11270            (expiry-wait (gnus-group-get-parameter
11271                          gnus-newsgroup-name 'expiry-wait))
11272            es)
11273       (when expirable
11274         ;; There are expirable articles in this group, so we run them
11275         ;; through the expiry process.
11276         (gnus-message 6 "Expiring articles...")
11277         ;; The list of articles that weren't expired is returned.
11278         (if expiry-wait
11279             (let ((nnmail-expiry-wait-function nil)
11280                   (nnmail-expiry-wait expiry-wait))
11281               (setq es (gnus-request-expire-articles
11282                         expirable gnus-newsgroup-name)))
11283           (setq es (gnus-request-expire-articles
11284                     expirable gnus-newsgroup-name)))
11285         (or total (setq gnus-newsgroup-expirable es))
11286         ;; We go through the old list of expirable, and mark all
11287         ;; really expired articles as nonexistent.
11288         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11289           (let ((gnus-use-cache nil))
11290             (while expirable
11291               (unless (memq (car expirable) es)
11292                 (when (gnus-data-find (car expirable))
11293                   (gnus-summary-mark-article
11294                    (car expirable) gnus-canceled-mark)))
11295               (setq expirable (cdr expirable)))))
11296         (gnus-message 6 "Expiring articles...done")))))
11297
11298 (defun gnus-summary-expire-articles-now ()
11299   "Expunge all expirable articles in the current group.
11300 This means that *all* articles that are marked as expirable will be
11301 deleted forever, right now."
11302   (interactive)
11303   (gnus-set-global-variables)
11304   (or gnus-expert-user
11305       (gnus-y-or-n-p
11306        "Are you really, really, really sure you want to expunge? ")
11307       (error "Phew!"))
11308   (let ((nnmail-expiry-wait 'immediate)
11309         (nnmail-expiry-wait-function nil))
11310     (gnus-summary-expire-articles)))
11311
11312 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11313 (defun gnus-summary-delete-article (&optional n)
11314   "Delete the N next (mail) articles.
11315 This command actually deletes articles.  This is not a marking
11316 command.  The article will disappear forever from your life, never to
11317 return.
11318 If N is negative, delete backwards.
11319 If N is nil and articles have been marked with the process mark,
11320 delete these instead."
11321   (interactive "P")
11322   (gnus-set-global-variables)
11323   (or (gnus-check-backend-function 'request-expire-articles
11324                                    gnus-newsgroup-name)
11325       (error "The current newsgroup does not support article deletion."))
11326   ;; Compute the list of articles to delete.
11327   (let ((articles (gnus-summary-work-articles n))
11328         not-deleted)
11329     (if (and gnus-novice-user
11330              (not (gnus-y-or-n-p
11331                    (format "Do you really want to delete %s forever? "
11332                            (if (> (length articles) 1) "these articles"
11333                              "this article")))))
11334         ()
11335       ;; Delete the articles.
11336       (setq not-deleted (gnus-request-expire-articles
11337                          articles gnus-newsgroup-name 'force))
11338       (while articles
11339         (gnus-summary-remove-process-mark (car articles))
11340         ;; The backend might not have been able to delete the article
11341         ;; after all.
11342         (or (memq (car articles) not-deleted)
11343             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11344         (setq articles (cdr articles))))
11345     (gnus-summary-position-point)
11346     (gnus-set-mode-line 'summary)
11347     not-deleted))
11348
11349 (defun gnus-summary-edit-article (&optional force)
11350   "Enter into a buffer and edit the current article.
11351 This will have permanent effect only in mail groups.
11352 If FORCE is non-nil, allow editing of articles even in read-only
11353 groups."
11354   (interactive "P")
11355   (save-excursion
11356     (set-buffer gnus-summary-buffer)
11357     (gnus-set-global-variables)
11358     (when (and (not force)
11359                (gnus-group-read-only-p))
11360       (error "The current newsgroup does not support article editing."))
11361     (gnus-summary-select-article t nil t)
11362     (gnus-configure-windows 'article)
11363     (select-window (get-buffer-window gnus-article-buffer))
11364     (gnus-message 6 "C-c C-c to end edits")
11365     (setq buffer-read-only nil)
11366     (text-mode)
11367     (use-local-map (copy-keymap (current-local-map)))
11368     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11369     (buffer-enable-undo)
11370     (widen)
11371     (goto-char (point-min))
11372     (search-forward "\n\n" nil t)))
11373
11374 (defun gnus-summary-edit-article-done ()
11375   "Make edits to the current article permanent."
11376   (interactive)
11377   (if (gnus-group-read-only-p)
11378       (progn
11379         (gnus-summary-edit-article-postpone)
11380         (gnus-message
11381          1 "The current newsgroup does not support article editing.")
11382         (ding))
11383     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11384       (erase-buffer)
11385       (insert buf)
11386       (if (not (gnus-request-replace-article
11387                 (cdr gnus-article-current) (car gnus-article-current)
11388                 (current-buffer)))
11389           (error "Couldn't replace article.")
11390         (gnus-article-mode)
11391         (use-local-map gnus-article-mode-map)
11392         (setq buffer-read-only t)
11393         (buffer-disable-undo (current-buffer))
11394         (gnus-configure-windows 'summary)
11395         (gnus-summary-update-article (cdr gnus-article-current)))
11396       (run-hooks 'gnus-article-display-hook)
11397       (and (gnus-visual-p 'summary-highlight 'highlight)
11398            (run-hooks 'gnus-visual-mark-article-hook)))))
11399
11400 (defun gnus-summary-edit-article-postpone ()
11401   "Postpone changes to the current article."
11402   (interactive)
11403   (gnus-article-mode)
11404   (use-local-map gnus-article-mode-map)
11405   (setq buffer-read-only t)
11406   (buffer-disable-undo (current-buffer))
11407   (gnus-configure-windows 'summary)
11408   (and (gnus-visual-p 'summary-highlight 'highlight)
11409        (run-hooks 'gnus-visual-mark-article-hook)))
11410
11411 (defun gnus-summary-respool-query ()
11412   "Query where the respool algorithm would put this article."
11413   (interactive)
11414   (gnus-set-global-variables)
11415   (gnus-summary-select-article)
11416   (save-excursion
11417     (set-buffer gnus-article-buffer)
11418     (save-restriction
11419       (goto-char (point-min))
11420       (search-forward "\n\n")
11421       (narrow-to-region (point-min) (point))
11422       (pp-eval-expression
11423        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11424
11425 ;; Summary score commands.
11426
11427 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11428
11429 (defun gnus-summary-raise-score (n)
11430   "Raise the score of the current article by N."
11431   (interactive "p")
11432   (gnus-set-global-variables)
11433   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11434
11435 (defun gnus-summary-set-score (n)
11436   "Set the score of the current article to N."
11437   (interactive "p")
11438   (gnus-set-global-variables)
11439   (save-excursion
11440     (gnus-summary-show-thread)
11441     (let ((buffer-read-only nil))
11442       ;; Set score.
11443       (gnus-summary-update-mark
11444        (if (= n (or gnus-summary-default-score 0)) ? 
11445          (if (< n (or gnus-summary-default-score 0))
11446              gnus-score-below-mark gnus-score-over-mark)) 'score))
11447     (let* ((article (gnus-summary-article-number))
11448            (score (assq article gnus-newsgroup-scored)))
11449       (if score (setcdr score n)
11450         (setq gnus-newsgroup-scored
11451               (cons (cons article n) gnus-newsgroup-scored))))
11452     (gnus-summary-update-line)))
11453
11454 (defun gnus-summary-current-score ()
11455   "Return the score of the current article."
11456   (interactive)
11457   (gnus-set-global-variables)
11458   (message "%s" (gnus-summary-article-score)))
11459
11460 ;; Summary marking commands.
11461
11462 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11463   "Mark articles which has the same subject as read, and then select the next.
11464 If UNMARK is positive, remove any kind of mark.
11465 If UNMARK is negative, tick articles."
11466   (interactive "P")
11467   (gnus-set-global-variables)
11468   (if unmark
11469       (setq unmark (prefix-numeric-value unmark)))
11470   (let ((count
11471          (gnus-summary-mark-same-subject
11472           (gnus-summary-article-subject) unmark)))
11473     ;; Select next unread article.  If auto-select-same mode, should
11474     ;; select the first unread article.
11475     (gnus-summary-next-article t (and gnus-auto-select-same
11476                                       (gnus-summary-article-subject)))
11477     (gnus-message 7 "%d article%s marked as %s"
11478                   count (if (= count 1) " is" "s are")
11479                   (if unmark "unread" "read"))))
11480
11481 (defun gnus-summary-kill-same-subject (&optional unmark)
11482   "Mark articles which has the same subject as read.
11483 If UNMARK is positive, remove any kind of mark.
11484 If UNMARK is negative, tick articles."
11485   (interactive "P")
11486   (gnus-set-global-variables)
11487   (if unmark
11488       (setq unmark (prefix-numeric-value unmark)))
11489   (let ((count
11490          (gnus-summary-mark-same-subject
11491           (gnus-summary-article-subject) unmark)))
11492     ;; If marked as read, go to next unread subject.
11493     (if (null unmark)
11494         ;; Go to next unread subject.
11495         (gnus-summary-next-subject 1 t))
11496     (gnus-message 7 "%d articles are marked as %s"
11497                   count (if unmark "unread" "read"))))
11498
11499 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11500   "Mark articles with same SUBJECT as read, and return marked number.
11501 If optional argument UNMARK is positive, remove any kinds of marks.
11502 If optional argument UNMARK is negative, mark articles as unread instead."
11503   (let ((count 1))
11504     (save-excursion
11505       (cond
11506        ((null unmark)                   ; Mark as read.
11507         (while (and
11508                 (progn
11509                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11510                   (gnus-summary-show-thread) t)
11511                 (gnus-summary-find-subject subject))
11512           (setq count (1+ count))))
11513        ((> unmark 0)                    ; Tick.
11514         (while (and
11515                 (progn
11516                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11517                   (gnus-summary-show-thread) t)
11518                 (gnus-summary-find-subject subject))
11519           (setq count (1+ count))))
11520        (t                               ; Mark as unread.
11521         (while (and
11522                 (progn
11523                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11524                   (gnus-summary-show-thread) t)
11525                 (gnus-summary-find-subject subject))
11526           (setq count (1+ count)))))
11527       (gnus-set-mode-line 'summary)
11528       ;; Return the number of marked articles.
11529       count)))
11530
11531 (defun gnus-summary-mark-as-processable (n &optional unmark)
11532   "Set the process mark on the next N articles.
11533 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11534 the process mark instead.  The difference between N and the actual
11535 number of articles marked is returned."
11536   (interactive "p")
11537   (gnus-set-global-variables)
11538   (let ((backward (< n 0))
11539         (n (abs n)))
11540     (while (and
11541             (> n 0)
11542             (if unmark
11543                 (gnus-summary-remove-process-mark
11544                  (gnus-summary-article-number))
11545               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11546             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11547       (setq n (1- n)))
11548     (if (/= 0 n) (gnus-message 7 "No more articles"))
11549     (gnus-summary-recenter)
11550     (gnus-summary-position-point)
11551     n))
11552
11553 (defun gnus-summary-unmark-as-processable (n)
11554   "Remove the process mark from the next N articles.
11555 If N is negative, mark backward instead.  The difference between N and
11556 the actual number of articles marked is returned."
11557   (interactive "p")
11558   (gnus-set-global-variables)
11559   (gnus-summary-mark-as-processable n t))
11560
11561 (defun gnus-summary-unmark-all-processable ()
11562   "Remove the process mark from all articles."
11563   (interactive)
11564   (gnus-set-global-variables)
11565   (save-excursion
11566     (while gnus-newsgroup-processable
11567       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11568   (gnus-summary-position-point))
11569
11570 (defun gnus-summary-mark-as-expirable (n)
11571   "Mark N articles forward as expirable.
11572 If N is negative, mark backward instead.  The difference between N and
11573 the actual number of articles marked is returned."
11574   (interactive "p")
11575   (gnus-set-global-variables)
11576   (gnus-summary-mark-forward n gnus-expirable-mark))
11577
11578 (defun gnus-summary-mark-article-as-replied (article)
11579   "Mark ARTICLE replied and update the summary line."
11580   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11581   (let ((buffer-read-only nil))
11582     (when (gnus-summary-goto-subject article)
11583       (gnus-summary-update-secondary-mark article))))
11584
11585 (defun gnus-summary-set-bookmark (article)
11586   "Set a bookmark in current article."
11587   (interactive (list (gnus-summary-article-number)))
11588   (gnus-set-global-variables)
11589   (if (or (not (get-buffer gnus-article-buffer))
11590           (not gnus-current-article)
11591           (not gnus-article-current)
11592           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11593       (error "No current article selected"))
11594   ;; Remove old bookmark, if one exists.
11595   (let ((old (assq article gnus-newsgroup-bookmarks)))
11596     (if old (setq gnus-newsgroup-bookmarks
11597                   (delq old gnus-newsgroup-bookmarks))))
11598   ;; Set the new bookmark, which is on the form
11599   ;; (article-number . line-number-in-body).
11600   (setq gnus-newsgroup-bookmarks
11601         (cons
11602          (cons article
11603                (save-excursion
11604                  (set-buffer gnus-article-buffer)
11605                  (count-lines
11606                   (min (point)
11607                        (save-excursion
11608                          (goto-char (point-min))
11609                          (search-forward "\n\n" nil t)
11610                          (point)))
11611                   (point))))
11612          gnus-newsgroup-bookmarks))
11613   (gnus-message 6 "A bookmark has been added to the current article."))
11614
11615 (defun gnus-summary-remove-bookmark (article)
11616   "Remove the bookmark from the current article."
11617   (interactive (list (gnus-summary-article-number)))
11618   (gnus-set-global-variables)
11619   ;; Remove old bookmark, if one exists.
11620   (let ((old (assq article gnus-newsgroup-bookmarks)))
11621     (if old
11622         (progn
11623           (setq gnus-newsgroup-bookmarks
11624                 (delq old gnus-newsgroup-bookmarks))
11625           (gnus-message 6 "Removed bookmark."))
11626       (gnus-message 6 "No bookmark in current article."))))
11627
11628 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11629 (defun gnus-summary-mark-as-dormant (n)
11630   "Mark N articles forward as dormant.
11631 If N is negative, mark backward instead.  The difference between N and
11632 the actual number of articles marked is returned."
11633   (interactive "p")
11634   (gnus-set-global-variables)
11635   (gnus-summary-mark-forward n gnus-dormant-mark))
11636
11637 (defun gnus-summary-set-process-mark (article)
11638   "Set the process mark on ARTICLE and update the summary line."
11639   (setq gnus-newsgroup-processable
11640         (cons article
11641               (delq article gnus-newsgroup-processable)))
11642   (when (gnus-summary-goto-subject article)
11643     (gnus-summary-show-thread)
11644     (gnus-summary-update-secondary-mark article)))
11645
11646 (defun gnus-summary-remove-process-mark (article)
11647   "Remove the process mark from ARTICLE and update the summary line."
11648   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11649   (when (gnus-summary-goto-subject article)
11650     (gnus-summary-show-thread)
11651     (gnus-summary-update-secondary-mark article)))
11652
11653 (defun gnus-summary-set-saved-mark (article)
11654   "Set the process mark on ARTICLE and update the summary line."
11655   (push article gnus-newsgroup-saved)
11656   (when (gnus-summary-goto-subject article)
11657     (gnus-summary-update-secondary-mark article)))
11658
11659 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11660   "Mark N articles as read forwards.
11661 If N is negative, mark backwards instead.
11662 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11663 marked as unread.
11664 The difference between N and the actual number of articles marked is
11665 returned."
11666   (interactive "p")
11667   (gnus-set-global-variables)
11668   (let ((backward (< n 0))
11669         (gnus-summary-goto-unread
11670          (and gnus-summary-goto-unread
11671               (not (eq gnus-summary-goto-unread 'never))
11672               (not (memq mark (list gnus-unread-mark
11673                                     gnus-ticked-mark gnus-dormant-mark)))))
11674         (n (abs n))
11675         (mark (or mark gnus-del-mark)))
11676     (while (and (> n 0)
11677                 (gnus-summary-mark-article nil mark no-expire)
11678                 (zerop (gnus-summary-next-subject
11679                         (if backward -1 1)
11680                         (and gnus-summary-goto-unread
11681                              (not (eq gnus-summary-goto-unread 'never)))
11682                         t)))
11683       (setq n (1- n)))
11684     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11685     (gnus-summary-recenter)
11686     (gnus-summary-position-point)
11687     (gnus-set-mode-line 'summary)
11688     n))
11689
11690 (defun gnus-summary-mark-article-as-read (mark)
11691   "Mark the current article quickly as read with MARK."
11692   (let ((article (gnus-summary-article-number)))
11693     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11694     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11695     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11696     (setq gnus-newsgroup-reads
11697           (cons (cons article mark) gnus-newsgroup-reads))
11698     ;; Possibly remove from cache, if that is used.
11699     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11700     ;; Allow the backend to change the mark.
11701     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11702     ;; Check for auto-expiry.
11703     (when (and gnus-newsgroup-auto-expire
11704                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11705                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11706                    (= mark gnus-ancient-mark)
11707                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11708       (setq mark gnus-expirable-mark)
11709       (push article gnus-newsgroup-expirable))
11710     ;; Set the mark in the buffer.
11711     (gnus-summary-update-mark mark 'unread)
11712     t))
11713
11714 (defun gnus-summary-mark-article-as-unread (mark)
11715   "Mark the current article quickly as unread with MARK."
11716   (let ((article (gnus-summary-article-number)))
11717     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11718     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11719     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11720     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11721     (cond ((= mark gnus-ticked-mark)
11722            (push article gnus-newsgroup-marked))
11723           ((= mark gnus-dormant-mark)
11724            (push article gnus-newsgroup-dormant))
11725           (t
11726            (push article gnus-newsgroup-unreads)))
11727     (setq gnus-newsgroup-reads
11728           (delq (assq article gnus-newsgroup-reads)
11729                 gnus-newsgroup-reads))
11730
11731     ;; See whether the article is to be put in the cache.
11732     (and gnus-use-cache
11733          (vectorp (gnus-summary-article-header article))
11734          (save-excursion
11735            (gnus-cache-possibly-enter-article
11736             gnus-newsgroup-name article
11737             (gnus-summary-article-header article)
11738             (= mark gnus-ticked-mark)
11739             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11740
11741     ;; Fix the mark.
11742     (gnus-summary-update-mark mark 'unread)
11743     t))
11744
11745 (defun gnus-summary-mark-article (&optional article mark no-expire)
11746   "Mark ARTICLE with MARK.  MARK can be any character.
11747 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11748 `??' (dormant) and `?E' (expirable).
11749 If MARK is nil, then the default character `?D' is used.
11750 If ARTICLE is nil, then the article on the current line will be
11751 marked."
11752   ;; The mark might be a string.
11753   (and (stringp mark)
11754        (setq mark (aref mark 0)))
11755   ;; If no mark is given, then we check auto-expiring.
11756   (and (not no-expire)
11757        gnus-newsgroup-auto-expire
11758        (or (not mark)
11759            (and (numberp mark)
11760                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11761                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11762                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11763        (setq mark gnus-expirable-mark))
11764   (let* ((mark (or mark gnus-del-mark))
11765          (article (or article (gnus-summary-article-number))))
11766     (or article (error "No article on current line"))
11767     (if (or (= mark gnus-unread-mark)
11768             (= mark gnus-ticked-mark)
11769             (= mark gnus-dormant-mark))
11770         (gnus-mark-article-as-unread article mark)
11771       (gnus-mark-article-as-read article mark))
11772
11773     ;; See whether the article is to be put in the cache.
11774     (and gnus-use-cache
11775          (not (= mark gnus-canceled-mark))
11776          (vectorp (gnus-summary-article-header article))
11777          (save-excursion
11778            (gnus-cache-possibly-enter-article
11779             gnus-newsgroup-name article
11780             (gnus-summary-article-header article)
11781             (= mark gnus-ticked-mark)
11782             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11783
11784     (if (gnus-summary-goto-subject article nil t)
11785         (let ((buffer-read-only nil))
11786           (gnus-summary-show-thread)
11787           ;; Fix the mark.
11788           (gnus-summary-update-mark mark 'unread)
11789           t))))
11790
11791 (defun gnus-summary-update-secondary-mark (article)
11792   "Update the secondary (read, process, cache) mark."
11793   (gnus-summary-update-mark
11794    (cond ((memq article gnus-newsgroup-processable)
11795           gnus-process-mark)
11796          ((memq article gnus-newsgroup-cached)
11797           gnus-cached-mark)
11798          ((memq article gnus-newsgroup-replied)
11799           gnus-replied-mark)
11800          ((memq article gnus-newsgroup-saved)
11801           gnus-saved-mark)
11802          (t gnus-unread-mark))
11803    'replied)
11804   (when (gnus-visual-p 'summary-highlight 'highlight)
11805     (run-hooks 'gnus-summary-update-hook))
11806   t)
11807
11808 (defun gnus-summary-update-mark (mark type)
11809   (beginning-of-line)
11810   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11811         (buffer-read-only nil))
11812     (when forward
11813       ;; Go to the right position on the line.
11814       (forward-char forward)
11815       ;; Replace the old mark with the new mark.
11816       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11817       ;; Optionally update the marks by some user rule.
11818       (when (eq type 'unread)
11819         (gnus-data-set-mark
11820          (gnus-data-find (gnus-summary-article-number)) mark)
11821         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11822
11823 (defun gnus-mark-article-as-read (article &optional mark)
11824   "Enter ARTICLE in the pertinent lists and remove it from others."
11825   ;; Make the article expirable.
11826   (let ((mark (or mark gnus-del-mark)))
11827     (if (= mark gnus-expirable-mark)
11828         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11829       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11830     ;; Remove from unread and marked lists.
11831     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11832     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11833     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11834     (push (cons article mark) gnus-newsgroup-reads)
11835     ;; Possibly remove from cache, if that is used.
11836     (when gnus-use-cache
11837       (gnus-cache-enter-remove-article article))))
11838
11839 (defun gnus-mark-article-as-unread (article &optional mark)
11840   "Enter ARTICLE in the pertinent lists and remove it from others."
11841   (let ((mark (or mark gnus-ticked-mark)))
11842     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11843     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11844     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11845     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11846     (cond ((= mark gnus-ticked-mark)
11847            (push article gnus-newsgroup-marked))
11848           ((= mark gnus-dormant-mark)
11849            (push article gnus-newsgroup-dormant))
11850           (t
11851            (push article gnus-newsgroup-unreads)))
11852     (setq gnus-newsgroup-reads
11853           (delq (assq article gnus-newsgroup-reads)
11854                 gnus-newsgroup-reads))))
11855
11856 (defalias 'gnus-summary-mark-as-unread-forward
11857   'gnus-summary-tick-article-forward)
11858 (make-obsolete 'gnus-summary-mark-as-unread-forward
11859                'gnus-summary-tick-article-forward)
11860 (defun gnus-summary-tick-article-forward (n)
11861   "Tick N articles forwards.
11862 If N is negative, tick backwards instead.
11863 The difference between N and the number of articles ticked is returned."
11864   (interactive "p")
11865   (gnus-summary-mark-forward n gnus-ticked-mark))
11866
11867 (defalias 'gnus-summary-mark-as-unread-backward
11868   'gnus-summary-tick-article-backward)
11869 (make-obsolete 'gnus-summary-mark-as-unread-backward
11870                'gnus-summary-tick-article-backward)
11871 (defun gnus-summary-tick-article-backward (n)
11872   "Tick N articles backwards.
11873 The difference between N and the number of articles ticked is returned."
11874   (interactive "p")
11875   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11876
11877 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11878 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11879 (defun gnus-summary-tick-article (&optional article clear-mark)
11880   "Mark current article as unread.
11881 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11882 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11883   (interactive)
11884   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11885                                        gnus-ticked-mark)))
11886
11887 (defun gnus-summary-mark-as-read-forward (n)
11888   "Mark N articles as read forwards.
11889 If N is negative, mark backwards instead.
11890 The difference between N and the actual number of articles marked is
11891 returned."
11892   (interactive "p")
11893   (gnus-summary-mark-forward n gnus-del-mark t))
11894
11895 (defun gnus-summary-mark-as-read-backward (n)
11896   "Mark the N articles as read backwards.
11897 The difference between N and the actual number of articles marked is
11898 returned."
11899   (interactive "p")
11900   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11901
11902 (defun gnus-summary-mark-as-read (&optional article mark)
11903   "Mark current article as read.
11904 ARTICLE specifies the article to be marked as read.
11905 MARK specifies a string to be inserted at the beginning of the line."
11906   (gnus-summary-mark-article article mark))
11907
11908 (defun gnus-summary-clear-mark-forward (n)
11909   "Clear marks from N articles forward.
11910 If N is negative, clear backward instead.
11911 The difference between N and the number of marks cleared is returned."
11912   (interactive "p")
11913   (gnus-summary-mark-forward n gnus-unread-mark))
11914
11915 (defun gnus-summary-clear-mark-backward (n)
11916   "Clear marks from N articles backward.
11917 The difference between N and the number of marks cleared is returned."
11918   (interactive "p")
11919   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11920
11921 (defun gnus-summary-mark-unread-as-read ()
11922   "Intended to be used by `gnus-summary-mark-article-hook'."
11923   (when (memq gnus-current-article gnus-newsgroup-unreads)
11924     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11925
11926 (defun gnus-summary-mark-read-and-unread-as-read ()
11927   "Intended to be used by `gnus-summary-mark-article-hook'."
11928   (let ((mark (gnus-summary-article-mark)))
11929     (when (or (gnus-unread-mark-p mark)
11930               (gnus-read-mark-p mark))
11931       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11932
11933 (defun gnus-summary-mark-region-as-read (point mark all)
11934   "Mark all unread articles between point and mark as read.
11935 If given a prefix, mark all articles between point and mark as read,
11936 even ticked and dormant ones."
11937   (interactive "r\nP")
11938   (save-excursion
11939     (let (article)
11940       (goto-char point)
11941       (beginning-of-line)
11942       (while (and
11943               (< (point) mark)
11944               (progn
11945                 (when (or all
11946                           (memq (setq article (gnus-summary-article-number))
11947                                 gnus-newsgroup-unreads))
11948                   (gnus-summary-mark-article article gnus-del-mark))
11949                 t)
11950               (gnus-summary-find-next))))))
11951
11952 (defun gnus-summary-mark-below (score mark)
11953   "Mark articles with score less than SCORE with MARK."
11954   (interactive "P\ncMark: ")
11955   (gnus-set-global-variables)
11956   (setq score (if score
11957                   (prefix-numeric-value score)
11958                 (or gnus-summary-default-score 0)))
11959   (save-excursion
11960     (set-buffer gnus-summary-buffer)
11961     (goto-char (point-min))
11962     (while 
11963         (progn
11964           (and (< (gnus-summary-article-score) score)
11965                (gnus-summary-mark-article nil mark))
11966           (gnus-summary-find-next)))))
11967
11968 (defun gnus-summary-kill-below (&optional score)
11969   "Mark articles with score below SCORE as read."
11970   (interactive "P")
11971   (gnus-set-global-variables)
11972   (gnus-summary-mark-below score gnus-killed-mark))
11973
11974 (defun gnus-summary-clear-above (&optional score)
11975   "Clear all marks from articles with score above SCORE."
11976   (interactive "P")
11977   (gnus-set-global-variables)
11978   (gnus-summary-mark-above score gnus-unread-mark))
11979
11980 (defun gnus-summary-tick-above (&optional score)
11981   "Tick all articles with score above SCORE."
11982   (interactive "P")
11983   (gnus-set-global-variables)
11984   (gnus-summary-mark-above score gnus-ticked-mark))
11985
11986 (defun gnus-summary-mark-above (score mark)
11987   "Mark articles with score over SCORE with MARK."
11988   (interactive "P\ncMark: ")
11989   (gnus-set-global-variables)
11990   (setq score (if score
11991                   (prefix-numeric-value score)
11992                 (or gnus-summary-default-score 0)))
11993   (save-excursion
11994     (set-buffer gnus-summary-buffer)
11995     (goto-char (point-min))
11996     (while (and (progn
11997                   (if (> (gnus-summary-article-score) score)
11998                       (gnus-summary-mark-article nil mark))
11999                   t)
12000                 (gnus-summary-find-next)))))
12001
12002 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12003 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12004 (defun gnus-summary-limit-include-expunged ()
12005   "Display all the hidden articles that were expunged for low scores."
12006   (interactive)
12007   (gnus-set-global-variables)
12008   (let ((buffer-read-only nil))
12009     (let ((scored gnus-newsgroup-scored)
12010           headers h)
12011       (while scored
12012         (or (gnus-summary-goto-subject (car (car scored)))
12013             (and (setq h (gnus-summary-article-header (car (car scored))))
12014                  (< (cdr (car scored)) gnus-summary-expunge-below)
12015                  (setq headers (cons h headers))))
12016         (setq scored (cdr scored)))
12017       (or headers (error "No expunged articles hidden."))
12018       (goto-char (point-min))
12019       (gnus-summary-prepare-unthreaded (nreverse headers)))
12020     (goto-char (point-min))
12021     (gnus-summary-position-point)))
12022
12023 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12024   "Mark all articles not marked as unread in this newsgroup as read.
12025 If prefix argument ALL is non-nil, all articles are marked as read.
12026 If QUIETLY is non-nil, no questions will be asked.
12027 If TO-HERE is non-nil, it should be a point in the buffer.  All
12028 articles before this point will be marked as read.
12029 The number of articles marked as read is returned."
12030   (interactive "P")
12031   (gnus-set-global-variables)
12032   (prog1
12033       (if (or quietly
12034               (not gnus-interactive-catchup) ;Without confirmation?
12035               gnus-expert-user
12036               (gnus-y-or-n-p
12037                (if all
12038                    "Mark absolutely all articles as read? "
12039                  "Mark all unread articles as read? ")))
12040           (if (and not-mark
12041                    (not gnus-newsgroup-adaptive)
12042                    (not gnus-newsgroup-auto-expire))
12043               (progn
12044                 (when all
12045                   (setq gnus-newsgroup-marked nil
12046                         gnus-newsgroup-dormant nil))
12047                 (setq gnus-newsgroup-unreads nil))
12048             ;; We actually mark all articles as canceled, which we
12049             ;; have to do when using auto-expiry or adaptive scoring.
12050             (gnus-summary-show-all-threads)
12051             (if (gnus-summary-first-subject (not all))
12052                 (while (and
12053                         (if to-here (< (point) to-here) t)
12054                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12055                         (gnus-summary-find-next (not all)))))
12056             (unless to-here
12057               (setq gnus-newsgroup-unreads nil))
12058             (gnus-set-mode-line 'summary)))
12059     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12060       (if (and (not to-here) (eq 'nnvirtual (car method)))
12061           (nnvirtual-catchup-group
12062            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12063     (gnus-summary-position-point)))
12064
12065 (defun gnus-summary-catchup-to-here (&optional all)
12066   "Mark all unticked articles before the current one as read.
12067 If ALL is non-nil, also mark ticked and dormant articles as read."
12068   (interactive "P")
12069   (gnus-set-global-variables)
12070   (save-excursion
12071     (let ((beg (point)))
12072       ;; We check that there are unread articles.
12073       (when (or all (gnus-summary-find-prev))
12074         (gnus-summary-catchup all t beg))))
12075   (gnus-summary-position-point))
12076
12077 (defun gnus-summary-catchup-all (&optional quietly)
12078   "Mark all articles in this newsgroup as read."
12079   (interactive "P")
12080   (gnus-set-global-variables)
12081   (gnus-summary-catchup t quietly))
12082
12083 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12084   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12085 If prefix argument ALL is non-nil, all articles are marked as read."
12086   (interactive "P")
12087   (gnus-set-global-variables)
12088   (gnus-summary-catchup all quietly nil 'fast)
12089   ;; Select next newsgroup or exit.
12090   (if (eq gnus-auto-select-next 'quietly)
12091       (gnus-summary-next-group nil)
12092     (gnus-summary-exit)))
12093
12094 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12095   "Mark all articles in this newsgroup as read, and then exit."
12096   (interactive "P")
12097   (gnus-set-global-variables)
12098   (gnus-summary-catchup-and-exit t quietly))
12099
12100 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12101 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12102   "Mark all articles in this group as read and select the next group.
12103 If given a prefix, mark all articles, unread as well as ticked, as
12104 read."
12105   (interactive "P")
12106   (gnus-set-global-variables)
12107   (save-excursion
12108     (gnus-summary-catchup all))
12109   (gnus-summary-next-article t nil nil t))
12110
12111 ;; Thread-based commands.
12112
12113 (defun gnus-summary-articles-in-thread (&optional article)
12114   "Return a list of all articles in the current thread.
12115 If ARTICLE is non-nil, return all articles in the thread that starts
12116 with that article."
12117   (let* ((article (or article (gnus-summary-article-number)))
12118          (data (gnus-data-find-list article))
12119          (top-level (gnus-data-level (car data)))
12120          (top-subject
12121           (cond ((null gnus-thread-operation-ignore-subject)
12122                  (gnus-simplify-subject-re
12123                   (mail-header-subject (gnus-data-header (car data)))))
12124                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12125                  (gnus-simplify-subject-fuzzy
12126                   (mail-header-subject (gnus-data-header (car data)))))
12127                 (t nil)))
12128          articles)
12129     (if (not data)
12130         ()                              ; This article doesn't exist.
12131       (while data
12132         (and (or (not top-subject)
12133                  (string= top-subject
12134                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12135                               (gnus-simplify-subject-fuzzy
12136                                (mail-header-subject
12137                                 (gnus-data-header (car data))))
12138                             (gnus-simplify-subject-re
12139                              (mail-header-subject
12140                               (gnus-data-header (car data)))))))
12141              (setq articles (cons (gnus-data-number (car data)) articles)))
12142         (if (and (setq data (cdr data))
12143                  (> (gnus-data-level (car data)) top-level))
12144             ()
12145           (setq data nil)))
12146       ;; Return the list of articles.
12147       (nreverse articles))))
12148
12149 (defun gnus-summary-rethread-current ()
12150   "Rethread the thread the current article is part of."
12151   (interactive)
12152   (gnus-set-global-variables)
12153   (let* ((gnus-show-threads t)
12154          (article (gnus-summary-article-number))
12155          (id (mail-header-id (gnus-summary-article-header)))
12156          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12157     (unless id
12158       (error "No article on the current line"))
12159     (gnus-rebuild-thread id)
12160     (gnus-summary-goto-subject article)))
12161
12162 (defun gnus-summary-reparent-thread ()
12163   "Make current article child of the marked (or previous) article.
12164
12165 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12166 is non-nil or the Subject: of both articles are the same."
12167   (interactive)
12168   (or (not (gnus-group-read-only-p))
12169       (error "The current newsgroup does not support article editing."))
12170   (or (<= (length gnus-newsgroup-processable) 1)
12171       (error "No more than one article may be marked."))
12172   (save-window-excursion
12173     (let ((gnus-article-buffer " *reparent*")
12174           (current-article (gnus-summary-article-number))
12175           ; first grab the marked article, otherwise one line up.
12176           (parent-article (if (not (null gnus-newsgroup-processable))
12177                               (car gnus-newsgroup-processable)
12178                             (save-excursion
12179                               (if (eq (forward-line -1) 0)
12180                                   (gnus-summary-article-number)
12181                                 (error "Beginning of summary buffer."))))))
12182       (or (not (eq current-article parent-article))
12183           (error "An article may not be self-referential."))
12184       (let ((message-id (mail-header-id 
12185                          (gnus-summary-article-header parent-article))))
12186         (or (and message-id (not (equal message-id "")))
12187             (error "No message-id in desired parent."))
12188         (gnus-summary-select-article t t nil current-article)
12189         (set-buffer gnus-article-buffer)
12190         (setq buffer-read-only nil)
12191         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12192           (erase-buffer)
12193           (insert buf))
12194         (goto-char (point-min))
12195         (if (search-forward-regexp "^References: " nil t)
12196             (insert message-id " " )
12197           (insert "References: " message-id "\n"))
12198         (or (gnus-request-replace-article current-article
12199                                           (car gnus-article-current)
12200                                           gnus-article-buffer)
12201             (error "Couldn't replace article."))
12202         (set-buffer gnus-summary-buffer)
12203         (gnus-summary-unmark-all-processable)
12204         (gnus-summary-rethread-current)
12205         (message "Article %d is now the child of article %d."
12206                  current-article parent-article)))))
12207
12208 (defun gnus-summary-toggle-threads (&optional arg)
12209   "Toggle showing conversation threads.
12210 If ARG is positive number, turn showing conversation threads on."
12211   (interactive "P")
12212   (gnus-set-global-variables)
12213   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12214     (setq gnus-show-threads
12215           (if (null arg) (not gnus-show-threads)
12216             (> (prefix-numeric-value arg) 0)))
12217     (gnus-summary-prepare)
12218     (gnus-summary-goto-subject current)
12219     (gnus-summary-position-point)))
12220
12221 (defun gnus-summary-show-all-threads ()
12222   "Show all threads."
12223   (interactive)
12224   (gnus-set-global-variables)
12225   (save-excursion
12226     (let ((buffer-read-only nil))
12227       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12228   (gnus-summary-position-point))
12229
12230 (defun gnus-summary-show-thread ()
12231   "Show thread subtrees.
12232 Returns nil if no thread was there to be shown."
12233   (interactive)
12234   (gnus-set-global-variables)
12235   (let ((buffer-read-only nil)
12236         (orig (point))
12237         ;; first goto end then to beg, to have point at beg after let
12238         (end (progn (end-of-line) (point)))
12239         (beg (progn (beginning-of-line) (point))))
12240     (prog1
12241         ;; Any hidden lines here?
12242         (search-forward "\r" end t)
12243       (subst-char-in-region beg end ?\^M ?\n t)
12244       (goto-char orig)
12245       (gnus-summary-position-point))))
12246
12247 (defun gnus-summary-hide-all-threads ()
12248   "Hide all thread subtrees."
12249   (interactive)
12250   (gnus-set-global-variables)
12251   (save-excursion
12252     (goto-char (point-min))
12253     (gnus-summary-hide-thread)
12254     (while (zerop (gnus-summary-next-thread 1 t))
12255       (gnus-summary-hide-thread)))
12256   (gnus-summary-position-point))
12257
12258 (defun gnus-summary-hide-thread ()
12259   "Hide thread subtrees.
12260 Returns nil if no threads were there to be hidden."
12261   (interactive)
12262   (gnus-set-global-variables)
12263   (let ((buffer-read-only nil)
12264         (start (point))
12265         (article (gnus-summary-article-number))
12266         end)
12267     (goto-char start)
12268     ;; Go forward until either the buffer ends or the subthread
12269     ;; ends.
12270     (when (and (not (eobp))
12271                (or (zerop (gnus-summary-next-thread 1 t))
12272                    (goto-char (point-max))))
12273       (setq end (point))
12274       (prog1
12275           (if (and (> (point) start)
12276                    (search-backward "\n" start t))
12277               (progn
12278                 (subst-char-in-region start (point) ?\n ?\^M)
12279                 (gnus-summary-goto-subject article))
12280             (goto-char start)
12281             nil)
12282         ;;(gnus-summary-position-point)
12283         ))))
12284
12285 (defun gnus-summary-go-to-next-thread (&optional previous)
12286   "Go to the same level (or less) next thread.
12287 If PREVIOUS is non-nil, go to previous thread instead.
12288 Return the article number moved to, or nil if moving was impossible."
12289   (let* ((level (gnus-summary-thread-level))
12290          (article (gnus-summary-article-number))
12291          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12292          oart)
12293     (while data
12294       (if (<= (gnus-data-level (car data)) level)
12295           (setq oart (gnus-data-number (car data))
12296                 data nil)
12297         (setq data (cdr data))))
12298     (and oart
12299          (gnus-summary-goto-subject oart))))
12300
12301 (defun gnus-summary-next-thread (n &optional silent)
12302   "Go to the same level next N'th thread.
12303 If N is negative, search backward instead.
12304 Returns the difference between N and the number of skips actually
12305 done.
12306
12307 If SILENT, don't output messages."
12308   (interactive "p")
12309   (gnus-set-global-variables)
12310   (let ((backward (< n 0))
12311         (n (abs n))
12312         old dum)
12313     (while (and (> n 0)
12314                 (setq old (save-excursion (forward-line 1) (point)))
12315                 (gnus-summary-go-to-next-thread backward))
12316       (when (and (eq gnus-summary-make-false-root 'dummy)
12317                  (setq dum (text-property-not-all
12318                             old (point) 'gnus-intangible nil)))
12319         (goto-char dum))
12320       (decf n))
12321     (unless silent 
12322       (gnus-summary-position-point))
12323     (when (and (not silent) (/= 0 n))
12324       (gnus-message 7 "No more threads"))
12325     n))
12326
12327 (defun gnus-summary-prev-thread (n)
12328   "Go to the same level previous N'th thread.
12329 Returns the difference between N and the number of skips actually
12330 done."
12331   (interactive "p")
12332   (gnus-set-global-variables)
12333   (gnus-summary-next-thread (- n)))
12334
12335 (defun gnus-summary-go-down-thread ()
12336   "Go down one level in the current thread."
12337   (let ((children (gnus-summary-article-children)))
12338     (and children
12339          (gnus-summary-goto-subject (car children)))))
12340
12341 (defun gnus-summary-go-up-thread ()
12342   "Go up one level in the current thread."
12343   (let ((parent (gnus-summary-article-parent)))
12344     (and parent
12345          (gnus-summary-goto-subject parent))))
12346
12347 (defun gnus-summary-down-thread (n)
12348   "Go down thread N steps.
12349 If N is negative, go up instead.
12350 Returns the difference between N and how many steps down that were
12351 taken."
12352   (interactive "p")
12353   (gnus-set-global-variables)
12354   (let ((up (< n 0))
12355         (n (abs n)))
12356     (while (and (> n 0)
12357                 (if up (gnus-summary-go-up-thread)
12358                   (gnus-summary-go-down-thread)))
12359       (setq n (1- n)))
12360     (gnus-summary-position-point)
12361     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12362     n))
12363
12364 (defun gnus-summary-up-thread (n)
12365   "Go up thread N steps.
12366 If N is negative, go up instead.
12367 Returns the difference between N and how many steps down that were
12368 taken."
12369   (interactive "p")
12370   (gnus-set-global-variables)
12371   (gnus-summary-down-thread (- n)))
12372
12373 (defun gnus-summary-top-thread ()
12374   "Go to the top of the thread."
12375   (interactive)
12376   (gnus-set-global-variables)
12377   (while (gnus-summary-go-up-thread))
12378   (gnus-summary-article-number))
12379
12380 (defun gnus-summary-kill-thread (&optional unmark)
12381   "Mark articles under current thread as read.
12382 If the prefix argument is positive, remove any kinds of marks.
12383 If the prefix argument is negative, tick articles instead."
12384   (interactive "P")
12385   (gnus-set-global-variables)
12386   (if unmark
12387       (setq unmark (prefix-numeric-value unmark)))
12388   (let ((articles (gnus-summary-articles-in-thread)))
12389     (save-excursion
12390       ;; Expand the thread.
12391       (gnus-summary-show-thread)
12392       ;; Mark all the articles.
12393       (while articles
12394         (gnus-summary-goto-subject (car articles))
12395         (cond ((null unmark)
12396                (gnus-summary-mark-article-as-read gnus-killed-mark))
12397               ((> unmark 0)
12398                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12399               (t
12400                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12401         (setq articles (cdr articles))))
12402     ;; Hide killed subtrees.
12403     (and (null unmark)
12404          gnus-thread-hide-killed
12405          (gnus-summary-hide-thread))
12406     ;; If marked as read, go to next unread subject.
12407     (if (null unmark)
12408         ;; Go to next unread subject.
12409         (gnus-summary-next-subject 1 t)))
12410   (gnus-set-mode-line 'summary))
12411
12412 ;; Summary sorting commands
12413
12414 (defun gnus-summary-sort-by-number (&optional reverse)
12415   "Sort summary buffer by article number.
12416 Argument REVERSE means reverse order."
12417   (interactive "P")
12418   (gnus-summary-sort 'number reverse))
12419
12420 (defun gnus-summary-sort-by-author (&optional reverse)
12421   "Sort summary buffer by author name alphabetically.
12422 If case-fold-search is non-nil, case of letters is ignored.
12423 Argument REVERSE means reverse order."
12424   (interactive "P")
12425   (gnus-summary-sort 'author reverse))
12426
12427 (defun gnus-summary-sort-by-subject (&optional reverse)
12428   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12429 If case-fold-search is non-nil, case of letters is ignored.
12430 Argument REVERSE means reverse order."
12431   (interactive "P")
12432   (gnus-summary-sort 'subject reverse))
12433
12434 (defun gnus-summary-sort-by-date (&optional reverse)
12435   "Sort summary buffer by date.
12436 Argument REVERSE means reverse order."
12437   (interactive "P")
12438   (gnus-summary-sort 'date reverse))
12439
12440 (defun gnus-summary-sort-by-score (&optional reverse)
12441   "Sort summary buffer by score.
12442 Argument REVERSE means reverse order."
12443   (interactive "P")
12444   (gnus-summary-sort 'score reverse))
12445
12446 (defun gnus-summary-sort (predicate reverse)
12447   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12448   (gnus-set-global-variables)
12449   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12450          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12451          (gnus-thread-sort-functions
12452           (list
12453            (if (not reverse)
12454                thread
12455              `(lambda (t1 t2)
12456                 (,thread t2 t1)))))
12457          (gnus-article-sort-functions
12458           (list
12459            (if (not reverse)
12460                article
12461              `(lambda (t1 t2)
12462                 (,article t2 t1)))))
12463          (buffer-read-only)
12464          (gnus-summary-prepare-hook nil))
12465     ;; We do the sorting by regenerating the threads.
12466     (gnus-summary-prepare)
12467     ;; Hide subthreads if needed.
12468     (when (and gnus-show-threads gnus-thread-hide-subtree)
12469       (gnus-summary-hide-all-threads)))
12470   ;; If in async mode, we send some info to the backend.
12471   (when gnus-newsgroup-async
12472     (gnus-request-asynchronous
12473      gnus-newsgroup-name gnus-newsgroup-data)))
12474
12475 (defun gnus-sortable-date (date)
12476   "Make sortable string by string-lessp from DATE.
12477 Timezone package is used."
12478   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12479          (year (aref date 0))
12480          (month (aref date 1))
12481          (day (aref date 2)))
12482     (timezone-make-sortable-date
12483      year month day
12484      (timezone-make-time-string
12485       (aref date 3) (aref date 4) (aref date 5)))))
12486
12487 ;; Summary saving commands.
12488
12489 (defun gnus-summary-save-article (&optional n not-saved)
12490   "Save the current article using the default saver function.
12491 If N is a positive number, save the N next articles.
12492 If N is a negative number, save the N previous articles.
12493 If N is nil and any articles have been marked with the process mark,
12494 save those articles instead.
12495 The variable `gnus-default-article-saver' specifies the saver function."
12496   (interactive "P")
12497   (gnus-set-global-variables)
12498   (let ((articles (gnus-summary-work-articles n))
12499         file header article)
12500     (while articles
12501       (setq header (gnus-summary-article-header
12502                     (setq article (pop articles))))
12503       (if (not (vectorp header))
12504           ;; This is a pseudo-article.
12505           (if (assq 'name header)
12506               (gnus-copy-file (cdr (assq 'name header)))
12507             (gnus-message 1 "Article %d is unsaveable" article))
12508         ;; This is a real article.
12509         (save-window-excursion
12510           (gnus-summary-select-article t nil nil article))
12511         (unless gnus-save-all-headers
12512           ;; Remove headers accoring to `gnus-saved-headers'.
12513           (let ((gnus-visible-headers
12514                  (or gnus-saved-headers gnus-visible-headers)))
12515             (gnus-article-hide-headers nil t)))
12516         ;; Remove any X-Gnus lines.
12517         (save-excursion
12518           (set-buffer gnus-article-buffer)
12519           (save-restriction
12520             (let ((buffer-read-only nil))
12521               (nnheader-narrow-to-headers)
12522               (while (re-search-forward "^X-Gnus" nil t)
12523                 (gnus-delete-line)))))
12524         (save-window-excursion
12525           (if (not gnus-default-article-saver)
12526               (error "No default saver is defined.")
12527             (setq file (funcall
12528                         gnus-default-article-saver
12529                         (cond
12530                          ((not gnus-prompt-before-saving)
12531                           'default)
12532                          ((eq gnus-prompt-before-saving 'always)
12533                           nil)
12534                          (t file))))))
12535         (gnus-summary-remove-process-mark article)
12536         (unless not-saved
12537           (gnus-summary-set-saved-mark article))))
12538     (gnus-summary-position-point)
12539     n))
12540
12541 (defun gnus-summary-pipe-output (&optional arg)
12542   "Pipe the current article to a subprocess.
12543 If N is a positive number, pipe the N next articles.
12544 If N is a negative number, pipe the N previous articles.
12545 If N is nil and any articles have been marked with the process mark,
12546 pipe those articles instead."
12547   (interactive "P")
12548   (gnus-set-global-variables)
12549   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12550     (gnus-summary-save-article arg t))
12551   (gnus-configure-windows 'pipe))
12552
12553 (defun gnus-summary-save-article-mail (&optional arg)
12554   "Append the current article to an mail file.
12555 If N is a positive number, save the N next articles.
12556 If N is a negative number, save the N previous articles.
12557 If N is nil and any articles have been marked with the process mark,
12558 save those articles instead."
12559   (interactive "P")
12560   (gnus-set-global-variables)
12561   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12562     (gnus-summary-save-article arg)))
12563
12564 (defun gnus-summary-save-article-rmail (&optional arg)
12565   "Append the current article to an rmail file.
12566 If N is a positive number, save the N next articles.
12567 If N is a negative number, save the N previous articles.
12568 If N is nil and any articles have been marked with the process mark,
12569 save those articles instead."
12570   (interactive "P")
12571   (gnus-set-global-variables)
12572   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12573     (gnus-summary-save-article arg)))
12574
12575 (defun gnus-summary-save-article-file (&optional arg)
12576   "Append the current article to a file.
12577 If N is a positive number, save the N next articles.
12578 If N is a negative number, save the N previous articles.
12579 If N is nil and any articles have been marked with the process mark,
12580 save those articles instead."
12581   (interactive "P")
12582   (gnus-set-global-variables)
12583   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12584     (gnus-summary-save-article arg)))
12585
12586 (defun gnus-summary-save-article-body-file (&optional arg)
12587   "Append the current article body to a file.
12588 If N is a positive number, save the N next articles.
12589 If N is a negative number, save the N previous articles.
12590 If N is nil and any articles have been marked with the process mark,
12591 save those articles instead."
12592   (interactive "P")
12593   (gnus-set-global-variables)
12594   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12595     (gnus-summary-save-article arg)))
12596
12597 (defun gnus-get-split-value (methods)
12598   "Return a value based on the split METHODS."
12599   (let (split-name method result match)
12600     (when methods
12601       (save-excursion
12602         (set-buffer gnus-original-article-buffer)
12603         (save-restriction
12604           (nnheader-narrow-to-headers)
12605           (while methods
12606             (goto-char (point-min))
12607             (setq method (pop methods))
12608             (setq match (pop method))
12609             (when (cond
12610                    ((stringp match)
12611                     ;; Regular expression.
12612                     (condition-case ()
12613                         (re-search-forward match nil t)
12614                       (error nil)))
12615                    ((gnus-functionp match)
12616                     ;; Function.
12617                     (save-restriction
12618                       (widen)
12619                       (setq result (funcall match gnus-newsgroup-name))))
12620                    ((consp match)
12621                     ;; Form.
12622                     (save-restriction
12623                       (widen)
12624                       (setq result (eval match)))))
12625               (setq split-name (append (cdr methods) split-name))
12626               (cond ((stringp result)
12627                      (push result split-name))
12628                     ((consp result)
12629                      (setq split-name (append result split-name)))))))))
12630     split-name))
12631
12632 (defun gnus-read-move-group-name (prompt default articles prefix)
12633   "Read a group name."
12634   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12635          (prom
12636           (format "Where do you want to %s %s? "
12637                   prompt
12638                   (if (> (length articles) 1)
12639                       (format "these %d articles" (length articles))
12640                     "this article")))
12641          (to-newsgroup
12642           (cond
12643            ((null split-name)
12644             (completing-read
12645              (concat prom
12646                      (if default
12647                          (format "(default %s) " default)
12648                        ""))
12649              gnus-active-hashtb nil nil prefix))
12650            ((= 1 (length split-name))
12651             (completing-read prom gnus-active-hashtb
12652                              nil nil (cons (car split-name) 0)))
12653            (t
12654             (completing-read
12655              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12656
12657     (when to-newsgroup
12658       (if (or (string= to-newsgroup "")
12659               (string= to-newsgroup prefix))
12660           (setq to-newsgroup (or default "")))
12661       (or (gnus-active to-newsgroup)
12662           (gnus-activate-group to-newsgroup)
12663           (error "No such group: %s" to-newsgroup)))
12664     to-newsgroup))
12665
12666 (defun gnus-read-save-file-name (prompt default-name)
12667   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12668          (file
12669           ;; Let the split methods have their say.
12670           (cond
12671            ;; No split name was found.
12672            ((null split-name)
12673             (read-file-name
12674              (concat prompt " (default "
12675                      (file-name-nondirectory default-name) ") ")
12676              (file-name-directory default-name)
12677              default-name))
12678            ;; A single split name was found
12679            ((= 1 (length split-name))
12680             (read-file-name
12681              (concat prompt " (default " (car split-name) ") ")
12682              gnus-article-save-directory
12683              (concat gnus-article-save-directory (car split-name))))
12684            ;; A list of splits was found.
12685            (t
12686             (setq split-name (mapcar (lambda (el) (list el))
12687                                      (nreverse split-name)))
12688             (let ((result (completing-read
12689                            (concat prompt " ") split-name nil nil)))
12690               (concat gnus-article-save-directory
12691                       (if (string= result "")
12692                           (car (car split-name))
12693                         result)))))))
12694     ;; If we have read a directory, we append the default file name.
12695     (when (file-directory-p file)
12696       (setq file (concat (file-name-as-directory file)
12697                          (file-name-nondirectory default-name))))
12698     ;; Possibly translate some charaters.
12699     (nnheader-translate-file-chars file)))
12700
12701 (defun gnus-article-archive-name (group)
12702   "Return the first instance of an \"Archive-name\" in the current buffer."
12703   (let ((case-fold-search t))
12704     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12705       (match-string 1))))
12706
12707 (defun gnus-summary-save-in-rmail (&optional filename)
12708   "Append this article to Rmail file.
12709 Optional argument FILENAME specifies file name.
12710 Directory to save to is default to `gnus-article-save-directory' which
12711 is initialized from the SAVEDIR environment variable."
12712   (interactive)
12713   (gnus-set-global-variables)
12714   (let ((default-name
12715           (funcall gnus-rmail-save-name gnus-newsgroup-name
12716                    gnus-current-headers gnus-newsgroup-last-rmail)))
12717     (setq filename
12718           (cond ((eq filename 'default)
12719                  default-name)
12720                 (filename filename)
12721                 (t (gnus-read-save-file-name
12722                     "Save in rmail file:" default-name))))
12723     (gnus-make-directory (file-name-directory filename))
12724     (gnus-eval-in-buffer-window
12725      gnus-original-article-buffer
12726      (save-excursion
12727        (save-restriction
12728          (widen)
12729          (gnus-output-to-rmail filename))))
12730     ;; Remember the directory name to save articles
12731     (setq gnus-newsgroup-last-rmail filename)))
12732
12733 (defun gnus-summary-save-in-mail (&optional filename)
12734   "Append this article to Unix mail file.
12735 Optional argument FILENAME specifies file name.
12736 Directory to save to is default to `gnus-article-save-directory' which
12737 is initialized from the SAVEDIR environment variable."
12738   (interactive)
12739   (gnus-set-global-variables)
12740   (let ((default-name
12741           (funcall gnus-mail-save-name gnus-newsgroup-name
12742                    gnus-current-headers gnus-newsgroup-last-mail)))
12743     (setq filename
12744           (cond ((eq filename 'default)
12745                  default-name)
12746                 (filename filename)
12747                 (t (gnus-read-save-file-name
12748                     "Save in Unix mail file:" default-name))))
12749     (setq filename
12750           (expand-file-name filename
12751                             (and default-name
12752                                  (file-name-directory default-name))))
12753     (gnus-make-directory (file-name-directory filename))
12754     (gnus-eval-in-buffer-window
12755      gnus-original-article-buffer
12756      (save-excursion
12757        (save-restriction
12758          (widen)
12759          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12760              (gnus-output-to-rmail filename)
12761            (let ((mail-use-rfc822 t))
12762              (rmail-output filename 1 t t))))))
12763     ;; Remember the directory name to save articles.
12764     (setq gnus-newsgroup-last-mail filename)))
12765
12766 (defun gnus-summary-save-in-file (&optional filename)
12767   "Append this article to file.
12768 Optional argument FILENAME specifies file name.
12769 Directory to save to is default to `gnus-article-save-directory' which
12770 is initialized from the SAVEDIR environment variable."
12771   (interactive)
12772   (gnus-set-global-variables)
12773   (let ((default-name
12774           (funcall gnus-file-save-name gnus-newsgroup-name
12775                    gnus-current-headers gnus-newsgroup-last-file)))
12776     (setq filename
12777           (cond ((eq filename 'default)
12778                  default-name)
12779                 (filename filename)
12780                 (t (gnus-read-save-file-name
12781                     "Save in file:" default-name))))
12782     (gnus-make-directory (file-name-directory filename))
12783     (gnus-eval-in-buffer-window
12784      gnus-original-article-buffer
12785      (save-excursion
12786        (save-restriction
12787          (widen)
12788          (gnus-output-to-file filename))))
12789     ;; Remember the directory name to save articles.
12790     (setq gnus-newsgroup-last-file filename)))
12791
12792 (defun gnus-summary-save-body-in-file (&optional filename)
12793   "Append this article body to a file.
12794 Optional argument FILENAME specifies file name.
12795 The directory to save in defaults to `gnus-article-save-directory' which
12796 is initialized from the SAVEDIR environment variable."
12797   (interactive)
12798   (gnus-set-global-variables)
12799   (let ((default-name
12800           (funcall gnus-file-save-name gnus-newsgroup-name
12801                    gnus-current-headers gnus-newsgroup-last-file)))
12802     (setq filename
12803           (cond ((eq filename 'default)
12804                  default-name)
12805                 (filename filename)
12806                 (t (gnus-read-save-file-name
12807                     "Save body in file:" default-name))))
12808     (gnus-make-directory (file-name-directory filename))
12809     (gnus-eval-in-buffer-window
12810      gnus-article-buffer
12811      (save-excursion
12812        (save-restriction
12813          (widen)
12814          (goto-char (point-min))
12815          (and (search-forward "\n\n" nil t)
12816               (narrow-to-region (point) (point-max)))
12817          (gnus-output-to-file filename))))
12818     ;; Remember the directory name to save articles.
12819     (setq gnus-newsgroup-last-file filename)))
12820
12821 (defun gnus-summary-save-in-pipe (&optional command)
12822   "Pipe this article to subprocess."
12823   (interactive)
12824   (gnus-set-global-variables)
12825   (setq command
12826         (cond ((eq command 'default)
12827                gnus-last-shell-command)
12828               (command command)
12829               (t (read-string "Shell command on article: "
12830                               gnus-last-shell-command))))
12831   (if (string-equal command "")
12832       (setq command gnus-last-shell-command))
12833   (gnus-eval-in-buffer-window
12834    gnus-article-buffer
12835    (save-restriction
12836      (widen)
12837      (shell-command-on-region (point-min) (point-max) command nil)))
12838   (setq gnus-last-shell-command command))
12839
12840 ;; Summary extract commands
12841
12842 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12843   (let ((buffer-read-only nil)
12844         (article (gnus-summary-article-number))
12845         after-article b e)
12846     (or (gnus-summary-goto-subject article)
12847         (error (format "No such article: %d" article)))
12848     (gnus-summary-position-point)
12849     ;; If all commands are to be bunched up on one line, we collect
12850     ;; them here.
12851     (if gnus-view-pseudos-separately
12852         ()
12853       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12854             files action)
12855         (while ps
12856           (setq action (cdr (assq 'action (car ps))))
12857           (setq files (list (cdr (assq 'name (car ps)))))
12858           (while (and ps (cdr ps)
12859                       (string= (or action "1")
12860                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12861             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12862             (setcdr ps (cdr (cdr ps))))
12863           (if (not files)
12864               ()
12865             (if (not (string-match "%s" action))
12866                 (setq files (cons " " files)))
12867             (setq files (cons " " files))
12868             (and (assq 'execute (car ps))
12869                  (setcdr (assq 'execute (car ps))
12870                          (funcall (if (string-match "%s" action)
12871                                       'format 'concat)
12872                                   action
12873                                   (mapconcat (lambda (f) f) files " ")))))
12874           (setq ps (cdr ps)))))
12875     (if (and gnus-view-pseudos (not not-view))
12876         (while pslist
12877           (and (assq 'execute (car pslist))
12878                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12879                                      (eq gnus-view-pseudos 'not-confirm)))
12880           (setq pslist (cdr pslist)))
12881       (save-excursion
12882         (while pslist
12883           (setq after-article (or (cdr (assq 'article (car pslist)))
12884                                   (gnus-summary-article-number)))
12885           (gnus-summary-goto-subject after-article)
12886           (forward-line 1)
12887           (setq b (point))
12888           (insert "          " (file-name-nondirectory
12889                                 (cdr (assq 'name (car pslist))))
12890                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12891           (setq e (point))
12892           (forward-line -1)             ; back to `b'
12893           (add-text-properties
12894            b e (list 'gnus-number gnus-reffed-article-number
12895                      gnus-mouse-face-prop gnus-mouse-face))
12896           (gnus-data-enter
12897            after-article gnus-reffed-article-number
12898            gnus-unread-mark b (car pslist) 0 (- e b))
12899           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12900           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12901           (setq pslist (cdr pslist)))))))
12902
12903 (defun gnus-pseudos< (p1 p2)
12904   (let ((c1 (cdr (assq 'action p1)))
12905         (c2 (cdr (assq 'action p2))))
12906     (and c1 c2 (string< c1 c2))))
12907
12908 (defun gnus-request-pseudo-article (props)
12909   (cond ((assq 'execute props)
12910          (gnus-execute-command (cdr (assq 'execute props)))))
12911   (let ((gnus-current-article (gnus-summary-article-number)))
12912     (run-hooks 'gnus-mark-article-hook)))
12913
12914 (defun gnus-execute-command (command &optional automatic)
12915   (save-excursion
12916     (gnus-article-setup-buffer)
12917     (set-buffer gnus-article-buffer)
12918     (let ((command (if automatic command (read-string "Command: " command)))
12919           (buffer-read-only nil))
12920       (erase-buffer)
12921       (insert "$ " command "\n\n")
12922       (if gnus-view-pseudo-asynchronously
12923           (start-process "gnus-execute" nil "sh" "-c" command)
12924         (call-process "sh" nil t nil "-c" command)))))
12925
12926 (defun gnus-copy-file (file &optional to)
12927   "Copy FILE to TO."
12928   (interactive
12929    (list (read-file-name "Copy file: " default-directory)
12930          (read-file-name "Copy file to: " default-directory)))
12931   (gnus-set-global-variables)
12932   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12933   (and (file-directory-p to)
12934        (setq to (concat (file-name-as-directory to)
12935                         (file-name-nondirectory file))))
12936   (copy-file file to))
12937
12938 ;; Summary kill commands.
12939
12940 (defun gnus-summary-edit-global-kill (article)
12941   "Edit the \"global\" kill file."
12942   (interactive (list (gnus-summary-article-number)))
12943   (gnus-set-global-variables)
12944   (gnus-group-edit-global-kill article))
12945
12946 (defun gnus-summary-edit-local-kill ()
12947   "Edit a local kill file applied to the current newsgroup."
12948   (interactive)
12949   (gnus-set-global-variables)
12950   (setq gnus-current-headers (gnus-summary-article-header))
12951   (gnus-set-global-variables)
12952   (gnus-group-edit-local-kill
12953    (gnus-summary-article-number) gnus-newsgroup-name))
12954
12955 \f
12956 ;;;
12957 ;;; Gnus article mode
12958 ;;;
12959
12960 (put 'gnus-article-mode 'mode-class 'special)
12961
12962 (if gnus-article-mode-map
12963     nil
12964   (setq gnus-article-mode-map (make-keymap))
12965   (suppress-keymap gnus-article-mode-map)
12966
12967   (gnus-define-keys gnus-article-mode-map
12968     " " gnus-article-goto-next-page
12969     "\177" gnus-article-goto-prev-page
12970     [delete] gnus-article-goto-prev-page
12971     "\C-c^" gnus-article-refer-article
12972     "h" gnus-article-show-summary
12973     "s" gnus-article-show-summary
12974     "\C-c\C-m" gnus-article-mail
12975     "?" gnus-article-describe-briefly
12976     gnus-mouse-2 gnus-article-push-button
12977     "\r" gnus-article-press-button
12978     "\t" gnus-article-next-button
12979     "\M-\t" gnus-article-prev-button
12980     "\C-c\C-b" gnus-bug)
12981
12982   (substitute-key-definition
12983    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
12984
12985 (defun gnus-article-mode ()
12986   "Major mode for displaying an article.
12987
12988 All normal editing commands are switched off.
12989
12990 The following commands are available:
12991
12992 \\<gnus-article-mode-map>
12993 \\[gnus-article-next-page]\t Scroll the article one page forwards
12994 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12995 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12996 \\[gnus-article-show-summary]\t Display the summary buffer
12997 \\[gnus-article-mail]\t Send a reply to the address near point
12998 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12999 \\[gnus-info-find-node]\t Go to the Gnus info node"
13000   (interactive)
13001   (when (and menu-bar-mode
13002              (gnus-visual-p 'article-menu 'menu))
13003     (gnus-article-make-menu-bar))
13004   (kill-all-local-variables)
13005   (gnus-simplify-mode-line)
13006   (setq mode-name "Article")
13007   (setq major-mode 'gnus-article-mode)
13008   (make-local-variable 'minor-mode-alist)
13009   (or (assq 'gnus-show-mime minor-mode-alist)
13010       (setq minor-mode-alist
13011             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13012   (use-local-map gnus-article-mode-map)
13013   (make-local-variable 'page-delimiter)
13014   (setq page-delimiter gnus-page-delimiter)
13015   (buffer-disable-undo (current-buffer))
13016   (setq buffer-read-only t)             ;Disable modification
13017   (run-hooks 'gnus-article-mode-hook))
13018
13019 (defun gnus-article-setup-buffer ()
13020   "Initialize the article buffer."
13021   (let* ((name (if gnus-single-article-buffer "*Article*"
13022                  (concat "*Article " gnus-newsgroup-name "*")))
13023          (original
13024           (progn (string-match "\\*Article" name)
13025                  (concat " *Original Article"
13026                          (substring name (match-end 0))))))
13027     (setq gnus-article-buffer name)
13028     (setq gnus-original-article-buffer original)
13029     ;; This might be a variable local to the summary buffer.
13030     (unless gnus-single-article-buffer
13031       (save-excursion
13032         (set-buffer gnus-summary-buffer)
13033         (setq gnus-article-buffer name)
13034         (setq gnus-original-article-buffer original)
13035         (gnus-set-global-variables))
13036       (make-local-variable 'gnus-summary-buffer))
13037     (if (get-buffer name)
13038         (save-excursion
13039           (set-buffer name)
13040           (buffer-disable-undo (current-buffer))
13041           (setq buffer-read-only t)
13042           (gnus-add-current-to-buffer-list)
13043           (or (eq major-mode 'gnus-article-mode)
13044               (gnus-article-mode))
13045           (current-buffer))
13046       (save-excursion
13047         (set-buffer (get-buffer-create name))
13048         (gnus-add-current-to-buffer-list)
13049         (gnus-article-mode)
13050         (current-buffer)))))
13051
13052 ;; Set article window start at LINE, where LINE is the number of lines
13053 ;; from the head of the article.
13054 (defun gnus-article-set-window-start (&optional line)
13055   (set-window-start
13056    (get-buffer-window gnus-article-buffer)
13057    (save-excursion
13058      (set-buffer gnus-article-buffer)
13059      (goto-char (point-min))
13060      (if (not line)
13061          (point-min)
13062        (gnus-message 6 "Moved to bookmark")
13063        (search-forward "\n\n" nil t)
13064        (forward-line line)
13065        (point)))))
13066
13067 (defun gnus-kill-all-overlays ()
13068   "Delete all overlays in the current buffer."
13069   (when (fboundp 'overlay-lists)
13070     (let* ((overlayss (overlay-lists))
13071            (buffer-read-only nil)
13072            (overlays (nconc (car overlayss) (cdr overlayss))))
13073       (while overlays
13074         (delete-overlay (pop overlays))))))
13075
13076 (defun gnus-request-article-this-buffer (article group)
13077   "Get an article and insert it into this buffer."
13078   (prog1
13079       (save-excursion
13080         (erase-buffer)
13081         (gnus-kill-all-overlays)
13082         (setq group (or group gnus-newsgroup-name))
13083
13084         ;; Open server if it has closed.
13085         (gnus-check-server (gnus-find-method-for-group group))
13086
13087         ;; Using `gnus-request-article' directly will insert the article into
13088         ;; `nntp-server-buffer' - so we'll save some time by not having to
13089         ;; copy it from the server buffer into the article buffer.
13090
13091         ;; We only request an article by message-id when we do not have the
13092         ;; headers for it, so we'll have to get those.
13093         (when (stringp article)
13094           (let ((gnus-override-method gnus-refer-article-method))
13095             (gnus-read-header article)))
13096
13097         ;; If the article number is negative, that means that this article
13098         ;; doesn't belong in this newsgroup (possibly), so we find its
13099         ;; message-id and request it by id instead of number.
13100         (when (numberp article)
13101           (save-excursion
13102             (set-buffer gnus-summary-buffer)
13103             (let ((header (gnus-summary-article-header article)))
13104               (if (< article 0)
13105                   (cond 
13106                    ((memq article gnus-newsgroup-sparse)
13107                     ;; This is a sparse gap article.
13108                     (setq article (mail-header-id header)))
13109                    ((vectorp header)
13110                     ;; It's a real article.
13111                     (setq article (mail-header-id header)))
13112                    (t
13113                     ;; It is an extracted pseudo-article.
13114                     (setq article 'pseudo)
13115                     (gnus-request-pseudo-article header))))
13116                 
13117               (let ((method (gnus-find-method-for-group 
13118                              gnus-newsgroup-name)))
13119                 (if (not (eq (car method) 'nneething))
13120                     ()
13121                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13122                                      (mail-header-subject header))))
13123                     (if (file-directory-p dir)
13124                         (progn
13125                           (setq article 'nneething)
13126                           (gnus-group-enter-directory dir)))))))))
13127
13128         (cond
13129          ;; We first check `gnus-original-article-buffer'.
13130          ((and (equal (car gnus-original-article) group)
13131                (eq (cdr gnus-original-article) article)
13132                (get-buffer gnus-original-article-buffer))
13133           (insert-buffer-substring gnus-original-article-buffer)
13134           'article)
13135          ;; Check the backlog.
13136          ((and gnus-keep-backlog
13137                (gnus-backlog-request-article group article (current-buffer)))
13138           'article)
13139          ;; Check the cache.
13140          ((and gnus-use-cache
13141                (numberp article)
13142                (gnus-cache-request-article article group))
13143           'article)
13144          ;; Get the article and put into the article buffer.
13145          ((or (stringp article) (numberp article))
13146           (let ((gnus-override-method
13147                  (and (stringp article) gnus-refer-article-method))
13148                 (buffer-read-only nil))
13149             (erase-buffer)
13150             (gnus-kill-all-overlays)
13151             (if (gnus-request-article article group (current-buffer))
13152                 (progn
13153                   (and gnus-keep-backlog
13154                        (gnus-backlog-enter-article
13155                         group article (current-buffer)))
13156                   'article))))
13157          ;; It was a pseudo.
13158          (t article)))
13159
13160     ;; Take the article from the original article buffer
13161     ;; and place it in the buffer it's supposed to be in.
13162     (setq gnus-original-article (cons group article))
13163     (when (and (get-buffer gnus-article-buffer)
13164                (equal (buffer-name (current-buffer))
13165                       (buffer-name (get-buffer gnus-article-buffer))))
13166       (save-excursion
13167         (if (get-buffer gnus-original-article-buffer)
13168             (set-buffer (get-buffer gnus-original-article-buffer))
13169           (set-buffer (get-buffer-create gnus-original-article-buffer))
13170           (buffer-disable-undo (current-buffer))
13171           (setq major-mode 'gnus-original-article-mode)
13172           (setq buffer-read-only t)
13173           (gnus-add-current-to-buffer-list))
13174         (let (buffer-read-only)
13175           (erase-buffer)
13176           (insert-buffer-substring gnus-article-buffer))))
13177     
13178     ;; Update sparse articles.
13179     (when (memq article gnus-newsgroup-sparse)
13180       (gnus-summary-update-article article))))
13181
13182 (defun gnus-read-header (id)
13183   "Read the headers of article ID and enter them into the Gnus system."
13184   (let ((group gnus-newsgroup-name)
13185         (headers gnus-newsgroup-headers)
13186         header where)
13187     ;; First we check to see whether the header in question is already
13188     ;; fetched.
13189     (if (stringp id)
13190         ;; This is a Message-ID.
13191         (setq header (gnus-id-to-header id))
13192       ;; This is an article number.
13193       (setq header (gnus-summary-article-header id)))
13194     (if header
13195         ;; We have found the header.
13196         header
13197       ;; We have to really fetch the header to this article.
13198       (when (setq where
13199                   (if (gnus-check-backend-function 'request-head group)
13200                       (gnus-request-head id group)
13201                     (gnus-request-article id group)))
13202         (save-excursion
13203           (set-buffer nntp-server-buffer)
13204           (and (search-forward "\n\n" nil t)
13205                (delete-region (1- (point)) (point-max)))
13206           (goto-char (point-max))
13207           (insert ".\n")
13208           (goto-char (point-min))
13209           (insert "211 "
13210                   (int-to-string
13211                    (cond
13212                     ((numberp id)
13213                      id)
13214                     ((cdr where)
13215                      (cdr where))
13216                     (t
13217                      gnus-reffed-article-number)))
13218                   " Article retrieved.\n"))
13219         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13220             () ; Malformed head.
13221           (if (and (stringp id)
13222                    (not (string= (gnus-group-real-name group)
13223                                  (car where))))
13224               ;; If we fetched by Message-ID and the article came
13225               ;; from a different group, we fudge some bogus article
13226               ;; numbers for this article.
13227               (mail-header-set-number header gnus-reffed-article-number))
13228           (decf gnus-reffed-article-number)
13229           (push header gnus-newsgroup-headers)
13230           (setq gnus-current-headers header)
13231           (push (mail-header-number header) gnus-newsgroup-limit)
13232           header)))))
13233
13234 (defun gnus-article-prepare (article &optional all-headers header)
13235   "Prepare ARTICLE in article mode buffer.
13236 ARTICLE should either be an article number or a Message-ID.
13237 If ARTICLE is an id, HEADER should be the article headers.
13238 If ALL-HEADERS is non-nil, no headers are hidden."
13239   (save-excursion
13240     ;; Make sure we start in a summary buffer.
13241     (unless (eq major-mode 'gnus-summary-mode)
13242       (set-buffer gnus-summary-buffer))
13243     (setq gnus-summary-buffer (current-buffer))
13244     ;; Make sure the connection to the server is alive.
13245     (unless (gnus-server-opened
13246              (gnus-find-method-for-group gnus-newsgroup-name))
13247       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13248       (gnus-request-group gnus-newsgroup-name t))
13249     (let* ((article (if header (mail-header-number header) article))
13250            (summary-buffer (current-buffer))
13251            (internal-hook gnus-article-internal-prepare-hook)
13252            (group gnus-newsgroup-name)
13253            result)
13254       (save-excursion
13255         (gnus-article-setup-buffer)
13256         (set-buffer gnus-article-buffer)
13257         ;; Deactivate active regions.
13258         (when (and (boundp 'transient-mark-mode)
13259                    transient-mark-mode)
13260           (setq mark-active nil))
13261         (if (not (setq result (let ((buffer-read-only nil))
13262                                 (gnus-request-article-this-buffer
13263                                  article group))))
13264             ;; There is no such article.
13265             (save-excursion
13266               (when (and (numberp article)
13267                          (not (memq article gnus-newsgroup-sparse)))
13268                 (setq gnus-article-current
13269                       (cons gnus-newsgroup-name article))
13270                 (set-buffer gnus-summary-buffer)
13271                 (setq gnus-current-article article)
13272                 (gnus-summary-mark-article article gnus-canceled-mark))
13273               (unless (memq article gnus-newsgroup-sparse)
13274                 (gnus-message
13275                  1 "No such article (may have expired or been canceled)")
13276                 (ding)
13277                 nil))
13278           (if (or (eq result 'pseudo) (eq result 'nneething))
13279               (progn
13280                 (save-excursion
13281                   (set-buffer summary-buffer)
13282                   (setq gnus-last-article gnus-current-article
13283                         gnus-newsgroup-history (cons gnus-current-article
13284                                                      gnus-newsgroup-history)
13285                         gnus-current-article 0
13286                         gnus-current-headers nil
13287                         gnus-article-current nil)
13288                   (if (eq result 'nneething)
13289                       (gnus-configure-windows 'summary)
13290                     (gnus-configure-windows 'article))
13291                   (gnus-set-global-variables))
13292                 (gnus-set-mode-line 'article))
13293             ;; The result from the `request' was an actual article -
13294             ;; or at least some text that is now displayed in the
13295             ;; article buffer.
13296             (if (and (numberp article)
13297                      (not (eq article gnus-current-article)))
13298                 ;; Seems like a new article has been selected.
13299                 ;; `gnus-current-article' must be an article number.
13300                 (save-excursion
13301                   (set-buffer summary-buffer)
13302                   (setq gnus-last-article gnus-current-article
13303                         gnus-newsgroup-history (cons gnus-current-article
13304                                                      gnus-newsgroup-history)
13305                         gnus-current-article article
13306                         gnus-current-headers
13307                         (gnus-summary-article-header gnus-current-article)
13308                         gnus-article-current
13309                         (cons gnus-newsgroup-name gnus-current-article))
13310                   (unless (vectorp gnus-current-headers)
13311                     (setq gnus-current-headers nil))
13312                   (gnus-summary-show-thread)
13313                   (run-hooks 'gnus-mark-article-hook)
13314                   (gnus-set-mode-line 'summary)
13315                   (and (gnus-visual-p 'article-highlight 'highlight)
13316                        (run-hooks 'gnus-visual-mark-article-hook))
13317                   ;; Set the global newsgroup variables here.
13318                   ;; Suggested by Jim Sisolak
13319                   ;; <sisolak@trans4.neep.wisc.edu>.
13320                   (gnus-set-global-variables)
13321                   (setq gnus-have-all-headers
13322                         (or all-headers gnus-show-all-headers))
13323                   (and gnus-use-cache
13324                        (vectorp (gnus-summary-article-header article))
13325                        (gnus-cache-possibly-enter-article
13326                         group article
13327                         (gnus-summary-article-header article)
13328                         (memq article gnus-newsgroup-marked)
13329                         (memq article gnus-newsgroup-dormant)
13330                         (memq article gnus-newsgroup-unreads)))))
13331             ;; Hooks for getting information from the article.
13332             ;; This hook must be called before being narrowed.
13333             (let (buffer-read-only)
13334               (run-hooks 'internal-hook)
13335               (run-hooks 'gnus-article-prepare-hook)
13336               ;; Decode MIME message.
13337               (if gnus-show-mime
13338                   (if (or (not gnus-strict-mime)
13339                           (gnus-fetch-field "Mime-Version"))
13340                       (funcall gnus-show-mime-method)
13341                     (funcall gnus-decode-encoded-word-method)))
13342               ;; Perform the article display hooks.
13343               (run-hooks 'gnus-article-display-hook))
13344             ;; Do page break.
13345             (goto-char (point-min))
13346             (and gnus-break-pages (gnus-narrow-to-page))
13347             (gnus-set-mode-line 'article)
13348             (gnus-configure-windows 'article)
13349             (goto-char (point-min))
13350             t))))))
13351
13352 (defun gnus-article-show-all-headers ()
13353   "Show all article headers in article mode buffer."
13354   (save-excursion
13355     (gnus-article-setup-buffer)
13356     (set-buffer gnus-article-buffer)
13357     (let ((buffer-read-only nil))
13358       (remove-text-properties (point-min) (point-max)
13359                               gnus-hidden-properties))))
13360
13361 (defun gnus-article-hide-headers-if-wanted ()
13362   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13363 Provided for backwards compatibility."
13364   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13365       gnus-inhibit-hiding
13366       (gnus-article-hide-headers)))
13367
13368 (defun gnus-article-hide-headers (&optional arg delete)
13369   "Toggle whether to hide unwanted headers and possibly sort them as well.
13370 If given a negative prefix, always show; if given a positive prefix,
13371 always hide."
13372   (interactive "P")
13373   (unless (gnus-article-check-hidden-text 'headers arg)
13374     ;; This function might be inhibited.
13375     (unless gnus-inhibit-hiding
13376       (save-excursion
13377         (set-buffer gnus-article-buffer)
13378         (save-restriction
13379           (let ((buffer-read-only nil)
13380                 (ignored (when (not (stringp gnus-visible-headers))
13381                            (cond ((stringp gnus-ignored-headers)
13382                                   gnus-ignored-headers)
13383                                  ((listp gnus-ignored-headers)
13384                                   (mapconcat 'identity gnus-ignored-headers
13385                                              "\\|")))))
13386                 (visible
13387                  (cond ((stringp gnus-visible-headers)
13388                         gnus-visible-headers)
13389                        ((and gnus-visible-headers
13390                              (listp gnus-visible-headers))
13391                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13392                 want-list beg want-l)
13393             ;; First we narrow to just the headers.
13394             (widen)
13395             (goto-char (point-min))
13396             ;; Hide any "From " lines at the beginning of (mail) articles.
13397             (while (looking-at "From ")
13398               (forward-line 1))
13399             (unless (bobp)
13400               (add-text-properties
13401                (point-min) (point)
13402                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13403             ;; Then treat the rest of the header lines.
13404             (narrow-to-region
13405              (point)
13406              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13407             ;; Then we use the two regular expressions
13408             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13409             ;; select which header lines is to remain visible in the
13410             ;; article buffer.
13411             (goto-char (point-min))
13412             (while (re-search-forward "^[^ \t]*:" nil t)
13413               (beginning-of-line)
13414               ;; We add the headers we want to keep to a list and delete
13415               ;; them from the buffer.
13416               (if (or (and visible (looking-at visible))
13417                       (and ignored (not (looking-at ignored))))
13418                   (progn
13419                     (push (buffer-substring
13420                            (setq beg (point))
13421                            (progn
13422                              (forward-line 1)
13423                              ;; Be sure to get multi-line headers...
13424                              (re-search-forward "^[^ \t]*:" nil t)
13425                              (beginning-of-line)
13426                              (point)))
13427                           want-list)
13428                     (delete-region beg (point)))
13429                 (forward-line 1)))
13430             ;; Sort the headers that we want to display.
13431             (setq want-list (sort want-list 'gnus-article-header-less))
13432             (goto-char (point-min))
13433             (while want-list
13434               (insert (pop want-list)))
13435             ;; We make the unwanted headers invisible.
13436             (if delete
13437                 (delete-region (point-min) (point-max))
13438               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13439               (add-text-properties
13440                (point) (point-max)
13441                (nconc (list 'gnus-type 'headers)
13442                       gnus-hidden-properties)))))))))
13443
13444 (defsubst gnus-article-header-rank (header)
13445   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13446   (let ((list gnus-sorted-header-list)
13447         (i 0))
13448     (while list
13449       (when (string-match (car list) header)
13450         (setq list nil))
13451       (setq list (cdr list))
13452       (incf i))
13453     i))
13454
13455 (defun gnus-article-header-less (h1 h2)
13456   "Say whether string H1 is \"less\" than string H2."
13457   (< (gnus-article-header-rank h1)
13458      (gnus-article-header-rank h2)))
13459
13460 (defun gnus-article-hide-boring-headers (&optional arg)
13461   "Toggle hiding of headers that aren't very interesting.
13462 If given a negative prefix, always show; if given a positive prefix,
13463 always hide."
13464   (interactive "P")
13465   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13466     (save-excursion
13467       (set-buffer gnus-article-buffer)
13468       (save-restriction
13469         (let ((buffer-read-only nil)
13470               (list gnus-boring-article-headers)
13471               (inhibit-point-motion-hooks t)
13472               elem)
13473           (nnheader-narrow-to-headers)
13474           (while list
13475             (setq elem (pop list))
13476             (goto-char (point-min))
13477             (cond
13478              ;; Hide empty headers.
13479              ((eq elem 'empty)
13480               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13481                 (forward-line -1)
13482                 (add-text-properties
13483                  (progn (beginning-of-line) (point))
13484                  (progn 
13485                    (end-of-line)
13486                    (if (re-search-forward "^[^ \t]" nil t)
13487                        (match-beginning 0)
13488                      (point-max)))
13489                  (nconc (list 'gnus-type 'boring-headers)
13490                         gnus-hidden-properties))))
13491              ;; Hide boring Newsgroups header.
13492              ((eq elem 'newsgroups)
13493               (when (equal (mail-fetch-field "newsgroups")
13494                            (gnus-group-real-name gnus-newsgroup-name))
13495                 (gnus-article-hide-header "newsgroups")))
13496              ((eq elem 'followup-to)
13497               (when (equal (mail-fetch-field "followup-to")
13498                            (mail-fetch-field "newsgroups"))
13499                 (gnus-article-hide-header "followup-to")))
13500              ((eq elem 'reply-to)
13501               (let ((from (mail-fetch-field "from"))
13502                     (reply-to (mail-fetch-field "reply-to")))
13503                 (when (and
13504                        from reply-to
13505                        (equal 
13506                         (nth 1 (mail-extract-address-components from))
13507                         (nth 1 (mail-extract-address-components reply-to))))
13508                   (gnus-article-hide-header "reply-to"))))
13509              ((eq elem 'date)
13510               (let ((date (mail-fetch-field "date")))
13511                 (when (and date
13512                            (< (gnus-days-between date (current-time-string))
13513                               4))
13514                   (gnus-article-hide-header "date")))))))))))
13515
13516 (defun gnus-article-hide-header (header)
13517   (save-excursion
13518     (goto-char (point-min))
13519     (when (re-search-forward (concat "^" header ":") nil t)
13520       (add-text-properties
13521        (progn (beginning-of-line) (point))
13522        (progn 
13523          (end-of-line)
13524          (if (re-search-forward "^[^ \t]" nil t)
13525              (match-beginning 0)
13526            (point-max)))
13527        (nconc (list 'gnus-type 'boring-headers)
13528               gnus-hidden-properties)))))
13529
13530 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13531 (defun gnus-article-treat-overstrike ()
13532   "Translate overstrikes into bold text."
13533   (interactive)
13534   (save-excursion
13535     (set-buffer gnus-article-buffer)
13536     (let ((buffer-read-only nil))
13537       (while (search-forward "\b" nil t)
13538         (let ((next (following-char))
13539               (previous (char-after (- (point) 2))))
13540           (cond ((eq next previous)
13541                  (put-text-property (- (point) 2) (point) 'invisible t)
13542                  (put-text-property (point) (1+ (point)) 'face 'bold))
13543                 ((eq next ?_)
13544                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13545                  (put-text-property
13546                   (- (point) 2) (1- (point)) 'face 'underline))
13547                 ((eq previous ?_)
13548                  (put-text-property (- (point) 2) (point) 'invisible t)
13549                  (put-text-property
13550                   (point) (1+ (point))  'face 'underline))))))))
13551
13552 (defun gnus-article-word-wrap ()
13553   "Format too long lines."
13554   (interactive)
13555   (save-excursion
13556     (set-buffer gnus-article-buffer)
13557     (let ((buffer-read-only nil)
13558           p)
13559       (widen)
13560       (goto-char (point-min))
13561       (search-forward "\n\n" nil t)
13562       (end-of-line 1)
13563       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13564             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13565             (adaptive-fill-mode t))
13566         (while (not (eobp))
13567           (and (>= (current-column) (min fill-column (window-width)))
13568                (/= (preceding-char) ?:)
13569                (fill-paragraph nil))
13570           (end-of-line 2))))))
13571
13572 (defun gnus-article-remove-cr ()
13573   "Remove carriage returns from an article."
13574   (interactive)
13575   (save-excursion
13576     (set-buffer gnus-article-buffer)
13577     (let ((buffer-read-only nil))
13578       (goto-char (point-min))
13579       (while (search-forward "\r" nil t)
13580         (replace-match "" t t)))))
13581
13582 (defun gnus-article-remove-trailing-blank-lines ()
13583   "Remove all trailing blank lines from the article."
13584   (interactive)
13585   (save-excursion
13586     (set-buffer gnus-article-buffer)
13587     (let ((buffer-read-only nil))
13588       (goto-char (point-max))
13589       (delete-region
13590        (point)
13591        (progn
13592          (while (looking-at "^[ \t]*$")
13593            (forward-line -1))
13594          (forward-line 1)
13595          (point))))))
13596
13597 (defun gnus-article-display-x-face (&optional force)
13598   "Look for an X-Face header and display it if present."
13599   (interactive (list 'force))
13600   (save-excursion
13601     (set-buffer gnus-article-buffer)
13602     ;; Delete the old process, if any.
13603     (when (process-status "gnus-x-face")
13604       (delete-process "gnus-x-face"))
13605     (let ((inhibit-point-motion-hooks t)
13606           (case-fold-search nil)
13607           from)
13608       (save-restriction
13609         (nnheader-narrow-to-headers)
13610         (setq from (mail-fetch-field "from"))
13611         (goto-char (point-min))
13612         (when (and gnus-article-x-face-command
13613                    (or force
13614                        ;; Check whether this face is censored.
13615                        (not gnus-article-x-face-too-ugly)
13616                        (and gnus-article-x-face-too-ugly from
13617                             (not (string-match gnus-article-x-face-too-ugly
13618                                                from))))
13619                    ;; Has to be present.
13620                    (re-search-forward "^X-Face: " nil t))
13621           ;; We now have the area of the buffer where the X-Face is stored.
13622           (let ((beg (point))
13623                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13624             ;; We display the face.
13625             (if (symbolp gnus-article-x-face-command)
13626                 ;; The command is a lisp function, so we call it.
13627                 (if (gnus-functionp gnus-article-x-face-command)
13628                     (funcall gnus-article-x-face-command beg end)
13629                   (error "%s is not a function" gnus-article-x-face-command))
13630               ;; The command is a string, so we interpret the command
13631               ;; as a, well, command, and fork it off.
13632               (let ((process-connection-type nil))
13633                 (process-kill-without-query
13634                  (start-process
13635                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13636                 (process-send-region "gnus-x-face" beg end)
13637                 (process-send-eof "gnus-x-face")))))))))
13638
13639 (defun gnus-headers-decode-quoted-printable ()
13640   "Hack to remove QP encoding from headers."
13641   (let ((case-fold-search t)
13642         (inhibit-point-motion-hooks t)
13643         string)
13644     (goto-char (point-min))
13645     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13646       (setq string (match-string 1))
13647       (narrow-to-region (match-beginning 0) (match-end 0))
13648       (delete-region (point-min) (point-max))
13649       (insert string)
13650       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13651       (subst-char-in-region (point-min) (point-max) ?_ ? )
13652       (widen)
13653       (goto-char (point-min)))))
13654
13655 (defun gnus-article-de-quoted-unreadable (&optional force)
13656   "Do a naive translation of a quoted-printable-encoded article.
13657 This is in no way, shape or form meant as a replacement for real MIME
13658 processing, but is simply a stop-gap measure until MIME support is
13659 written.
13660 If FORCE, decode the article whether it is marked as quoted-printable
13661 or not."
13662   (interactive (list 'force))
13663   (save-excursion
13664     (set-buffer gnus-article-buffer)
13665     (let ((case-fold-search t)
13666           (buffer-read-only nil)
13667           (type (gnus-fetch-field "content-transfer-encoding")))
13668       (when (or force
13669                 (and type (string-match "quoted-printable" type)))
13670         (gnus-headers-decode-quoted-printable)
13671         (goto-char (point-min))
13672         (search-forward "\n\n" nil 'move)
13673         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13674
13675 (defun gnus-mime-decode-quoted-printable (from to)
13676   "Decode Quoted-Printable in the region between FROM and TO."
13677   (goto-char from)
13678   (while (search-forward "=" to t)
13679     (cond ((eq (following-char) ?\n)
13680            (delete-char -1)
13681            (delete-char 1))
13682           ((looking-at "[0-9A-F][0-9A-F]")
13683            (delete-char -1)
13684            (insert (hexl-hex-string-to-integer
13685                     (buffer-substring (point) (+ 2 (point)))))
13686            (delete-char 2))
13687           ((looking-at "=")
13688            (delete-char 1))
13689           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13690
13691 (defun gnus-article-hide-pgp (&optional arg)
13692   "Toggle hiding of any PGP headers and signatures in the current article.
13693 If given a negative prefix, always show; if given a positive prefix,
13694 always hide."
13695   (interactive "P")
13696   (unless (gnus-article-check-hidden-text 'pgp arg)
13697     (save-excursion
13698       (set-buffer gnus-article-buffer)
13699       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13700             buffer-read-only beg end)
13701         (widen)
13702         (goto-char (point-min))
13703         ;; Hide the "header".
13704         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13705              (add-text-properties (match-beginning 0) (match-end 0) props))
13706         (setq beg (point))
13707         ;; Hide the actual signature.
13708         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13709              (setq end (match-beginning 0))
13710              (add-text-properties
13711               (match-beginning 0)
13712               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13713                   (match-end 0)
13714                 ;; Perhaps we shouldn't hide to the end of the buffer
13715                 ;; if there is no end to the signature?
13716                 (point-max))
13717               props))
13718         ;; Hide "- " PGP quotation markers.
13719         (when (and beg end)
13720           (narrow-to-region beg end)
13721           (goto-char (point-min))
13722           (while (re-search-forward "^- " nil t)
13723             (add-text-properties (match-beginning 0) (match-end 0) props))
13724           (widen))))))
13725
13726 (defun gnus-article-hide-signature (&optional arg)
13727   "Hide the signature in the current article.
13728 If given a negative prefix, always show; if given a positive prefix,
13729 always hide."
13730   (interactive "P")
13731   (unless (gnus-article-check-hidden-text 'signature arg)
13732     (save-excursion
13733       (set-buffer gnus-article-buffer)
13734       (save-restriction
13735         (let ((buffer-read-only nil))
13736           (when (gnus-narrow-to-signature)
13737             (add-text-properties
13738              (point-min) (point-max)
13739              (nconc (list 'gnus-type 'signature)
13740                     gnus-hidden-properties))))))))
13741
13742 (defvar gnus-signature-limit nil
13743   "Provide a limit to what is considered a signature.
13744 If it is a number, no signature may not be longer (in characters) than
13745 that number.  If it is a function, the function will be called without
13746 any parameters, and if it returns nil, there is no signature in the
13747 buffer.  If it is a string, it will be used as a regexp.  If it
13748 matches, the text in question is not a signature.")
13749
13750 (defun gnus-narrow-to-signature ()
13751   "Narrow to the signature."
13752   (widen)
13753   (goto-char (point-max))
13754   (when (re-search-backward gnus-signature-separator nil t)
13755     (forward-line 1)
13756     (when (or (null gnus-signature-limit)
13757               (and (numberp gnus-signature-limit)
13758                    (< (- (point-max) (point)) gnus-signature-limit))
13759               (and (gnus-functionp gnus-signature-limit)
13760                    (funcall gnus-signature-limit))
13761               (and (stringp gnus-signature-limit)
13762                    (not (re-search-forward gnus-signature-limit nil t))))
13763       (narrow-to-region (point) (point-max))
13764       t)))
13765
13766 (defun gnus-article-check-hidden-text (type arg)
13767   "Return nil if hiding is necessary."
13768   (save-excursion
13769     (set-buffer gnus-article-buffer)
13770     (let ((hide (gnus-article-hidden-text-p type)))
13771       (cond ((or (and (null arg) (eq hide 'hidden))
13772                  (and arg (< 0 (prefix-numeric-value arg))))
13773              (gnus-article-show-hidden-text type))
13774             ((eq hide 'shown)
13775              (gnus-article-show-hidden-text type t))
13776             (t nil)))))
13777
13778 (defun gnus-article-hidden-text-p (type)
13779   "Say whether the current buffer contains hidden text of type TYPE."
13780   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13781         prop)
13782     (when pos
13783       (if (get-text-property pos 'invisible)
13784           'hidden
13785         'shown))))
13786
13787 (defun gnus-article-hide (&optional arg force)
13788   "Hide all the gruft in the current article.
13789 This means that PGP stuff, signatures, cited text and (some)
13790 headers will be hidden.
13791 If given a prefix, show the hidden text instead."
13792   (interactive (list current-prefix-arg 'force))
13793   (gnus-article-hide-headers arg)
13794   (gnus-article-hide-pgp arg)
13795   (gnus-article-hide-citation-maybe arg force)
13796   (gnus-article-hide-signature arg))
13797
13798 (defun gnus-article-show-hidden-text (type &optional hide)
13799   "Show all hidden text of type TYPE.
13800 If HIDE, hide the text instead."
13801   (save-excursion
13802     (set-buffer gnus-article-buffer)
13803     (let ((buffer-read-only nil)
13804           (inhibit-point-motion-hooks t)
13805           (beg (point-min)))
13806       (while (gnus-goto-char (text-property-any
13807                               beg (point-max) 'gnus-type type))
13808         (setq beg (point))
13809         (forward-char)
13810         (if hide
13811             (add-text-properties beg (point) gnus-hidden-properties)
13812           (remove-text-properties beg (point) gnus-hidden-properties))
13813         (setq beg (point)))
13814       t)))
13815
13816 (defvar gnus-article-time-units
13817   `((year . ,(* 365.25 24 60 60))
13818     (week . ,(* 7 24 60 60))
13819     (day . ,(* 24 60 60))
13820     (hour . ,(* 60 60))
13821     (minute . 60)
13822     (second . 1))
13823   "Mapping from time units to seconds.")
13824
13825 (defun gnus-article-date-ut (&optional type highlight)
13826   "Convert DATE date to universal time in the current article.
13827 If TYPE is `local', convert to local time; if it is `lapsed', output
13828 how much time has lapsed since DATE."
13829   (interactive (list 'ut t))
13830   (let* ((header (or gnus-current-headers
13831                      (gnus-summary-article-header) ""))
13832          (date (and (vectorp header) (mail-header-date header)))
13833          (date-regexp "^Date: \\|^X-Sent: ")
13834          (now (current-time))
13835          (inhibit-point-motion-hooks t))
13836     (when (and date (not (string= date "")))
13837       (save-excursion
13838         (set-buffer gnus-article-buffer)
13839         (save-restriction
13840           (nnheader-narrow-to-headers)
13841           (let ((buffer-read-only nil))
13842             ;; Delete any old Date headers.
13843             (if (zerop (nnheader-remove-header date-regexp t))
13844                 (beginning-of-line)
13845               (goto-char (point-max)))
13846             (insert
13847              (cond
13848               ;; Convert to the local timezone.  We have to slap a
13849               ;; `condition-case' round the calls to the timezone
13850               ;; functions since they aren't particularly resistant to
13851               ;; buggy dates.
13852               ((eq type 'local)
13853                (concat "Date: " (condition-case ()
13854                                     (timezone-make-date-arpa-standard date)
13855                                   (error date))
13856                        "\n"))
13857               ;; Convert to Universal Time.
13858               ((eq type 'ut)
13859                (concat "Date: "
13860                        (condition-case ()
13861                            (timezone-make-date-arpa-standard date nil "UT")
13862                          (error date))
13863                        "\n"))
13864               ;; Get the original date from the article.
13865               ((eq type 'original)
13866                (concat "Date: " date "\n"))
13867               ;; Do an X-Sent lapsed format.
13868               ((eq type 'lapsed)
13869                ;; If the date is seriously mangled, the timezone
13870                ;; functions are liable to bug out, so we condition-case
13871                ;; the entire thing.
13872                (let* ((real-time
13873                        (condition-case ()
13874                            (gnus-time-minus
13875                             (gnus-encode-date
13876                              (timezone-make-date-arpa-standard
13877                               (current-time-string now)
13878                               (current-time-zone now) "UT"))
13879                             (gnus-encode-date
13880                              (timezone-make-date-arpa-standard
13881                               date nil "UT")))
13882                          (error '(0 0))))
13883                       (real-sec (+ (* (float (car real-time)) 65536)
13884                                    (cadr real-time)))
13885                       (sec (abs real-sec))
13886                       num prev)
13887                  (if (zerop sec)
13888                      "X-Sent: Now\n"
13889                    (concat
13890                     "X-Sent: "
13891                     ;; This is a bit convoluted, but basically we go
13892                     ;; through the time units for years, weeks, etc,
13893                     ;; and divide things to see whether that results
13894                     ;; in positive answers.
13895                     (mapconcat
13896                      (lambda (unit)
13897                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13898                            ;; The (remaining) seconds are too few to
13899                            ;; be divided into this time unit.
13900                            ""
13901                          ;; It's big enough, so we output it.
13902                          (setq sec (- sec (* num (cdr unit))))
13903                          (prog1
13904                              (concat (if prev ", " "") (int-to-string
13905                                                         (floor num))
13906                                      " " (symbol-name (car unit))
13907                                      (if (> num 1) "s" ""))
13908                            (setq prev t))))
13909                      gnus-article-time-units "")
13910                     ;; If dates are odd, then it might appear like the
13911                     ;; article was sent in the future.
13912                     (if (> real-sec 0)
13913                         " ago\n"
13914                       " in the future\n")))))
13915               (t
13916                (error "Unknown conversion type: %s" type)))))
13917           ;; Do highlighting.
13918           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13919             (gnus-article-highlight-headers)))))))
13920
13921 (defun gnus-article-date-local (&optional highlight)
13922   "Convert the current article date to the local timezone."
13923   (interactive (list t))
13924   (gnus-article-date-ut 'local highlight))
13925
13926 (defun gnus-article-date-original (&optional highlight)
13927   "Convert the current article date to what it was originally.
13928 This is only useful if you have used some other date conversion
13929 function and want to see what the date was before converting."
13930   (interactive (list t))
13931   (gnus-article-date-ut 'original highlight))
13932
13933 (defun gnus-article-date-lapsed (&optional highlight)
13934   "Convert the current article date to time lapsed since it was sent."
13935   (interactive (list t))
13936   (gnus-article-date-ut 'lapsed highlight))
13937
13938 (defun gnus-article-maybe-highlight ()
13939   "Do some article highlighting if `gnus-visual' is non-nil."
13940   (if (gnus-visual-p 'article-highlight 'highlight)
13941       (gnus-article-highlight-some)))
13942
13943 ;; Article savers.
13944
13945 (defun gnus-output-to-rmail (file-name)
13946   "Append the current article to an Rmail file named FILE-NAME."
13947   (require 'rmail)
13948   ;; Most of these codes are borrowed from rmailout.el.
13949   (setq file-name (expand-file-name file-name))
13950   (setq rmail-default-rmail-file file-name)
13951   (let ((artbuf (current-buffer))
13952         (tmpbuf (get-buffer-create " *Gnus-output*")))
13953     (save-excursion
13954       (or (get-file-buffer file-name)
13955           (file-exists-p file-name)
13956           (if (gnus-yes-or-no-p
13957                (concat "\"" file-name "\" does not exist, create it? "))
13958               (let ((file-buffer (create-file-buffer file-name)))
13959                 (save-excursion
13960                   (set-buffer file-buffer)
13961                   (rmail-insert-rmail-file-header)
13962                   (let ((require-final-newline nil))
13963                     (write-region (point-min) (point-max) file-name t 1)))
13964                 (kill-buffer file-buffer))
13965             (error "Output file does not exist")))
13966       (set-buffer tmpbuf)
13967       (buffer-disable-undo (current-buffer))
13968       (erase-buffer)
13969       (insert-buffer-substring artbuf)
13970       (gnus-convert-article-to-rmail)
13971       ;; Decide whether to append to a file or to an Emacs buffer.
13972       (let ((outbuf (get-file-buffer file-name)))
13973         (if (not outbuf)
13974             (append-to-file (point-min) (point-max) file-name)
13975           ;; File has been visited, in buffer OUTBUF.
13976           (set-buffer outbuf)
13977           (let ((buffer-read-only nil)
13978                 (msg (and (boundp 'rmail-current-message)
13979                           (symbol-value 'rmail-current-message))))
13980             ;; If MSG is non-nil, buffer is in RMAIL mode.
13981             (if msg
13982                 (progn (widen)
13983                        (narrow-to-region (point-max) (point-max))))
13984             (insert-buffer-substring tmpbuf)
13985             (if msg
13986                 (progn
13987                   (goto-char (point-min))
13988                   (widen)
13989                   (search-backward "\^_")
13990                   (narrow-to-region (point) (point-max))
13991                   (goto-char (1+ (point-min)))
13992                   (rmail-count-new-messages t)
13993                   (rmail-show-message msg)))))))
13994     (kill-buffer tmpbuf)))
13995
13996 (defun gnus-output-to-file (file-name)
13997   "Append the current article to a file named FILE-NAME."
13998   (setq file-name (expand-file-name file-name))
13999   (let ((artbuf (current-buffer))
14000         (tmpbuf (get-buffer-create " *Gnus-output*")))
14001     (save-excursion
14002       (set-buffer tmpbuf)
14003       (buffer-disable-undo (current-buffer))
14004       (erase-buffer)
14005       (insert-buffer-substring artbuf)
14006       ;; Append newline at end of the buffer as separator, and then
14007       ;; save it to file.
14008       (goto-char (point-max))
14009       (insert "\n")
14010       (append-to-file (point-min) (point-max) file-name))
14011     (kill-buffer tmpbuf)))
14012
14013 (defun gnus-convert-article-to-rmail ()
14014   "Convert article in current buffer to Rmail message format."
14015   (let ((buffer-read-only nil))
14016     ;; Convert article directly into Babyl format.
14017     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14018     (goto-char (point-min))
14019     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14020     (while (search-forward "\n\^_" nil t) ;single char
14021       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14022     (goto-char (point-max))
14023     (insert "\^_")))
14024
14025 (defun gnus-narrow-to-page (&optional arg)
14026   "Narrow the article buffer to a page.
14027 If given a numerical ARG, move forward ARG pages."
14028   (interactive "P")
14029   (setq arg (if arg (prefix-numeric-value arg) 0))
14030   (save-excursion
14031     (set-buffer gnus-article-buffer)
14032     (goto-char (point-min))
14033     (widen)
14034     (when (gnus-visual-p 'page-marker)
14035       (let ((buffer-read-only nil))
14036         (gnus-remove-text-with-property 'gnus-prev)
14037         (gnus-remove-text-with-property 'gnus-next)))
14038     (when
14039         (cond ((< arg 0)
14040                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14041               ((> arg 0)
14042                (re-search-forward page-delimiter nil 'move arg)))
14043       (goto-char (match-end 0)))
14044     (narrow-to-region
14045      (point)
14046      (if (re-search-forward page-delimiter nil 'move)
14047          (match-beginning 0)
14048        (point)))
14049     (when (and (gnus-visual-p 'page-marker)
14050                (not (= (point-min) 1)))
14051       (save-excursion
14052         (goto-char (point-min))
14053         (gnus-insert-prev-page-button)))
14054     (when (and (gnus-visual-p 'page-marker)
14055                (not (= (1- (point-max)) (buffer-size))))
14056       (save-excursion
14057         (goto-char (point-max))
14058         (gnus-insert-next-page-button)))))
14059
14060 ;; Article mode commands
14061
14062 (defun gnus-article-goto-next-page ()
14063   "Show the next page of the article."
14064   (interactive)
14065   (when (gnus-article-next-page)
14066     (gnus-article-read-summary-keys nil ?n)))
14067
14068 (defun gnus-article-goto-prev-page ()
14069   "Show the next page of the article."
14070   (interactive)
14071   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14072     (gnus-article-prev-page nil)))
14073
14074 (defun gnus-article-next-page (&optional lines)
14075   "Show the next page of the current article.
14076 If end of article, return non-nil.  Otherwise return nil.
14077 Argument LINES specifies lines to be scrolled up."
14078   (interactive "p")
14079   (move-to-window-line -1)
14080   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14081   (if (save-excursion
14082         (end-of-line)
14083         (and (pos-visible-in-window-p)  ;Not continuation line.
14084              (eobp)))
14085       ;; Nothing in this page.
14086       (if (or (not gnus-break-pages)
14087               (save-excursion
14088                 (save-restriction
14089                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14090           t                             ;Nothing more.
14091         (gnus-narrow-to-page 1)         ;Go to next page.
14092         nil)
14093     ;; More in this page.
14094     (condition-case ()
14095         (scroll-up lines)
14096       (end-of-buffer
14097        ;; Long lines may cause an end-of-buffer error.
14098        (goto-char (point-max))))
14099     nil))
14100
14101 (defun gnus-article-prev-page (&optional lines)
14102   "Show previous page of current article.
14103 Argument LINES specifies lines to be scrolled down."
14104   (interactive "p")
14105   (move-to-window-line 0)
14106   (if (and gnus-break-pages
14107            (bobp)
14108            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14109       (progn
14110         (gnus-narrow-to-page -1)        ;Go to previous page.
14111         (goto-char (point-max))
14112         (recenter -1))
14113     (condition-case ()
14114         (scroll-down lines)
14115       (error nil))))
14116
14117 (defun gnus-article-refer-article ()
14118   "Read article specified by message-id around point."
14119   (interactive)
14120   (let ((point (point)))
14121     (search-forward ">" nil t)          ;Move point to end of "<....>".
14122     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14123         (let ((message-id (match-string 1)))
14124           (goto-char point)
14125           (set-buffer gnus-summary-buffer)
14126           (gnus-summary-refer-article message-id))
14127       (goto-char (point))
14128       (error "No references around point"))))
14129
14130 (defun gnus-article-show-summary ()
14131   "Reconfigure windows to show summary buffer."
14132   (interactive)
14133   (gnus-configure-windows 'article)
14134   (gnus-summary-goto-subject gnus-current-article))
14135
14136 (defun gnus-article-describe-briefly ()
14137   "Describe article mode commands briefly."
14138   (interactive)
14139   (gnus-message 6
14140                 (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")))
14141
14142 (defun gnus-article-summary-command ()
14143   "Execute the last keystroke in the summary buffer."
14144   (interactive)
14145   (let ((obuf (current-buffer))
14146         (owin (current-window-configuration))
14147         func)
14148     (switch-to-buffer gnus-summary-buffer 'norecord)
14149     (setq func (lookup-key (current-local-map) (this-command-keys)))
14150     (call-interactively func)
14151     (set-buffer obuf)
14152     (set-window-configuration owin)
14153     (set-window-point (get-buffer-window (current-buffer)) (point))))
14154
14155 (defun gnus-article-summary-command-nosave ()
14156   "Execute the last keystroke in the summary buffer."
14157   (interactive)
14158   (let (func)
14159     (pop-to-buffer gnus-summary-buffer 'norecord)
14160     (setq func (lookup-key (current-local-map) (this-command-keys)))
14161     (call-interactively func)))
14162
14163 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14164   "Read a summary buffer key sequence and execute it from the article buffer."
14165   (interactive "P")
14166   (let ((nosaves
14167          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14168            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14169            "=" "^" "\M-^"))
14170         keys)
14171     (save-excursion
14172       (set-buffer gnus-summary-buffer)
14173       (push (or key last-command-event) unread-command-events)
14174       (setq keys (read-key-sequence nil)))
14175     (message "")
14176
14177     (if (member keys nosaves)
14178         (let (func)
14179           (pop-to-buffer gnus-summary-buffer 'norecord)
14180           (if (setq func (lookup-key (current-local-map) keys))
14181               (call-interactively func)
14182             (ding)))
14183       (let ((obuf (current-buffer))
14184             (owin (current-window-configuration))
14185             (opoint (point))
14186             func in-buffer)
14187         (if not-restore-window
14188             (pop-to-buffer gnus-summary-buffer 'norecord)
14189           (switch-to-buffer gnus-summary-buffer 'norecord))
14190         (setq in-buffer (current-buffer))
14191         (if (setq func (lookup-key (current-local-map) keys))
14192             (call-interactively func)
14193           (ding))
14194         (when (eq in-buffer (current-buffer))
14195           (set-buffer obuf)
14196           (unless not-restore-window
14197             (set-window-configuration owin))
14198           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14199
14200 \f
14201 ;;;
14202 ;;; Kill file handling.
14203 ;;;
14204
14205 ;;;###autoload
14206 (defalias 'gnus-batch-kill 'gnus-batch-score)
14207 ;;;###autoload
14208 (defun gnus-batch-score ()
14209   "Run batched scoring.
14210 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14211 Newsgroups is a list of strings in Bnews format.  If you want to score
14212 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14213 score the alt hierarchy, you'd say \"!alt.all\"."
14214   (interactive)
14215   (let* ((yes-and-no
14216           (gnus-newsrc-parse-options
14217            (apply (function concat)
14218                   (mapcar (lambda (g) (concat g " "))
14219                           command-line-args-left))))
14220          (gnus-expert-user t)
14221          (nnmail-spool-file nil)
14222          (gnus-use-dribble-file nil)
14223          (yes (car yes-and-no))
14224          (no (cdr yes-and-no))
14225          group newsrc entry
14226          ;; Disable verbose message.
14227          gnus-novice-user gnus-large-newsgroup)
14228     ;; Eat all arguments.
14229     (setq command-line-args-left nil)
14230     ;; Start Gnus.
14231     (gnus)
14232     ;; Apply kills to specified newsgroups in command line arguments.
14233     (setq newsrc (cdr gnus-newsrc-alist))
14234     (while newsrc
14235       (setq group (car (car newsrc)))
14236       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14237       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14238                (and (car entry)
14239                     (or (eq (car entry) t)
14240                         (not (zerop (car entry)))))
14241                (if yes (string-match yes group) t)
14242                (or (null no) (not (string-match no group))))
14243           (progn
14244             (gnus-summary-read-group group nil t nil t)
14245             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14246                  (gnus-summary-exit))))
14247       (setq newsrc (cdr newsrc)))
14248     ;; Exit Emacs.
14249     (switch-to-buffer gnus-group-buffer)
14250     (gnus-group-save-newsrc)))
14251
14252 (defun gnus-apply-kill-file ()
14253   "Apply a kill file to the current newsgroup.
14254 Returns the number of articles marked as read."
14255   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14256           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14257       (gnus-apply-kill-file-internal)
14258     0))
14259
14260 (defun gnus-kill-save-kill-buffer ()
14261   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14262     (when (get-file-buffer file)
14263       (save-excursion
14264         (set-buffer (get-file-buffer file))
14265         (and (buffer-modified-p) (save-buffer))
14266         (kill-buffer (current-buffer))))))
14267
14268 (defvar gnus-kill-file-name "KILL"
14269   "Suffix of the kill files.")
14270
14271 (defun gnus-newsgroup-kill-file (newsgroup)
14272   "Return the name of a kill file name for NEWSGROUP.
14273 If NEWSGROUP is nil, return the global kill file name instead."
14274   (cond 
14275    ;; The global KILL file is placed at top of the directory.
14276    ((or (null newsgroup)
14277         (string-equal newsgroup ""))
14278     (expand-file-name gnus-kill-file-name
14279                       (or gnus-kill-files-directory "~/News")))
14280    ;; Append ".KILL" to newsgroup name.
14281    ((gnus-use-long-file-name 'not-kill)
14282     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14283                               "." gnus-kill-file-name)
14284                       (or gnus-kill-files-directory "~/News")))
14285    ;; Place "KILL" under the hierarchical directory.
14286    (t
14287     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14288                               "/" gnus-kill-file-name)
14289                       (or gnus-kill-files-directory "~/News")))))
14290
14291 \f
14292 ;;;
14293 ;;; Dribble file
14294 ;;;
14295
14296 (defvar gnus-dribble-ignore nil)
14297 (defvar gnus-dribble-eval-file nil)
14298
14299 (defun gnus-dribble-file-name ()
14300   "Return the dribble file for the current .newsrc."
14301   (concat
14302    (if gnus-dribble-directory
14303        (concat (file-name-as-directory gnus-dribble-directory)
14304                (file-name-nondirectory gnus-current-startup-file))
14305      gnus-current-startup-file)
14306    "-dribble"))
14307
14308 (defun gnus-dribble-enter (string)
14309   "Enter STRING into the dribble buffer."
14310   (if (and (not gnus-dribble-ignore)
14311            gnus-dribble-buffer
14312            (buffer-name gnus-dribble-buffer))
14313       (let ((obuf (current-buffer)))
14314         (set-buffer gnus-dribble-buffer)
14315         (insert string "\n")
14316         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14317         (set-buffer obuf))))
14318
14319 (defun gnus-dribble-read-file ()
14320   "Read the dribble file from disk."
14321   (let ((dribble-file (gnus-dribble-file-name)))
14322     (save-excursion
14323       (set-buffer (setq gnus-dribble-buffer
14324                         (get-buffer-create
14325                          (file-name-nondirectory dribble-file))))
14326       (gnus-add-current-to-buffer-list)
14327       (erase-buffer)
14328       (setq buffer-file-name dribble-file)
14329       (auto-save-mode t)
14330       (buffer-disable-undo (current-buffer))
14331       (bury-buffer (current-buffer))
14332       (set-buffer-modified-p nil)
14333       (let ((auto (make-auto-save-file-name))
14334             (gnus-dribble-ignore t))
14335         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14336           ;; Load whichever file is newest -- the auto save file
14337           ;; or the "real" file.
14338           (if (file-newer-than-file-p auto dribble-file)
14339               (insert-file-contents auto)
14340             (insert-file-contents dribble-file))
14341           (unless (zerop (buffer-size))
14342             (set-buffer-modified-p t))
14343           ;; Set the file modes to reflect the .newsrc file modes.
14344           (save-buffer)
14345           (when (file-exists-p gnus-current-startup-file)
14346             (set-file-modes dribble-file
14347                             (file-modes gnus-current-startup-file)))
14348           ;; Possibly eval the file later.
14349           (when (gnus-y-or-n-p
14350                  "Auto-save file exists.  Do you want to read it? ")
14351             (setq gnus-dribble-eval-file t)))))))
14352
14353 (defun gnus-dribble-eval-file ()
14354   (if (not gnus-dribble-eval-file)
14355       ()
14356     (setq gnus-dribble-eval-file nil)
14357     (save-excursion
14358       (let ((gnus-dribble-ignore t))
14359         (set-buffer gnus-dribble-buffer)
14360         (eval-buffer (current-buffer))))))
14361
14362 (defun gnus-dribble-delete-file ()
14363   (if (file-exists-p (gnus-dribble-file-name))
14364       (delete-file (gnus-dribble-file-name)))
14365   (if gnus-dribble-buffer
14366       (save-excursion
14367         (set-buffer gnus-dribble-buffer)
14368         (let ((auto (make-auto-save-file-name)))
14369           (if (file-exists-p auto)
14370               (delete-file auto))
14371           (erase-buffer)
14372           (set-buffer-modified-p nil)))))
14373
14374 (defun gnus-dribble-save ()
14375   (if (and gnus-dribble-buffer
14376            (buffer-name gnus-dribble-buffer))
14377       (save-excursion
14378         (set-buffer gnus-dribble-buffer)
14379         (save-buffer))))
14380
14381 (defun gnus-dribble-clear ()
14382   (save-excursion
14383     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14384         (progn
14385           (set-buffer gnus-dribble-buffer)
14386           (erase-buffer)
14387           (set-buffer-modified-p nil)
14388           (setq buffer-saved-size (buffer-size))))))
14389
14390 \f
14391 ;;;
14392 ;;; Server Communication
14393 ;;;
14394
14395 (defun gnus-start-news-server (&optional confirm)
14396   "Open a method for getting news.
14397 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14398   (let (how)
14399     (if gnus-current-select-method
14400         ;; Stream is already opened.
14401         nil
14402       ;; Open NNTP server.
14403       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14404       (if confirm
14405           (progn
14406             ;; Read server name with completion.
14407             (setq gnus-nntp-server
14408                   (completing-read "NNTP server: "
14409                                    (mapcar (lambda (server) (list server))
14410                                            (cons (list gnus-nntp-server)
14411                                                  gnus-secondary-servers))
14412                                    nil nil gnus-nntp-server))))
14413
14414       (if (and gnus-nntp-server
14415                (stringp gnus-nntp-server)
14416                (not (string= gnus-nntp-server "")))
14417           (setq gnus-select-method
14418                 (cond ((or (string= gnus-nntp-server "")
14419                            (string= gnus-nntp-server "::"))
14420                        (list 'nnspool (system-name)))
14421                       ((string-match "^:" gnus-nntp-server)
14422                        (list 'nnmh gnus-nntp-server
14423                              (list 'nnmh-directory
14424                                    (file-name-as-directory
14425                                     (expand-file-name
14426                                      (concat "~/" (substring
14427                                                    gnus-nntp-server 1)))))
14428                              (list 'nnmh-get-new-mail nil)))
14429                       (t
14430                        (list 'nntp gnus-nntp-server)))))
14431
14432       (setq how (car gnus-select-method))
14433       (cond ((eq how 'nnspool)
14434              (require 'nnspool)
14435              (gnus-message 5 "Looking up local news spool..."))
14436             ((eq how 'nnmh)
14437              (require 'nnmh)
14438              (gnus-message 5 "Looking up mh spool..."))
14439             (t
14440              (require 'nntp)))
14441       (setq gnus-current-select-method gnus-select-method)
14442       (run-hooks 'gnus-open-server-hook)
14443       (or
14444        ;; gnus-open-server-hook might have opened it
14445        (gnus-server-opened gnus-select-method)
14446        (gnus-open-server gnus-select-method)
14447        (gnus-y-or-n-p
14448         (format
14449          "%s (%s) open error: '%s'.     Continue? "
14450          (car gnus-select-method) (cadr gnus-select-method)
14451          (gnus-status-message gnus-select-method)))
14452        (progn
14453          (gnus-message 1 "Couldn't open server on %s"
14454                        (nth 1 gnus-select-method))
14455          (ding)
14456          nil)))))
14457
14458 (defun gnus-check-group (group)
14459   "Try to make sure that the server where GROUP exists is alive."
14460   (let ((method (gnus-find-method-for-group group)))
14461     (or (gnus-server-opened method)
14462         (gnus-open-server method))))
14463
14464 (defun gnus-check-server (&optional method)
14465   "Check whether the connection to METHOD is down.
14466 If METHOD is nil, use `gnus-select-method'.
14467 If it is down, start it up (again)."
14468   (let ((method (or method gnus-select-method)))
14469     ;; Transform virtual server names into select methods.
14470     (when (stringp method)
14471       (setq method (gnus-server-to-method method)))
14472     (if (gnus-server-opened method)
14473         ;; The stream is already opened.
14474         t
14475       ;; Open the server.
14476       (gnus-message 5 "Opening %s server%s..." (car method)
14477                     (if (equal (nth 1 method) "") ""
14478                       (format " on %s" (nth 1 method))))
14479       (run-hooks 'gnus-open-server-hook)
14480       (prog1
14481           (gnus-open-server method)
14482         (message "")))))
14483
14484 (defun gnus-get-function (method function)
14485   "Return a function symbol based on METHOD and FUNCTION."
14486   ;; Translate server names into methods.
14487   (unless method
14488     (error "Attempted use of a nil select method"))
14489   (when (stringp method)
14490     (setq method (gnus-server-to-method method)))
14491   (let ((func (intern (format "%s-%s" (car method) function))))
14492     ;; If the functions isn't bound, we require the backend in
14493     ;; question.
14494     (unless (fboundp func)
14495       (require (car method))
14496       (unless (fboundp func)
14497         ;; This backend doesn't implement this function.
14498         (error "No such function: %s" func)))
14499     func))
14500
14501 \f
14502 ;;;
14503 ;;; Interface functions to the backends.
14504 ;;;
14505
14506 (defun gnus-open-server (method)
14507   "Open a connection to METHOD."
14508   (let ((elem (assoc method gnus-opened-servers)))
14509     ;; If this method was previously denied, we just return nil.
14510     (if (eq (nth 1 elem) 'denied)
14511         (progn
14512           (gnus-message 1 "Denied server")
14513           nil)
14514       ;; Open the server.
14515       (let ((result
14516              (funcall (gnus-get-function method 'open-server)
14517                       (nth 1 method) (nthcdr 2 method))))
14518         ;; If this hasn't been opened before, we add it to the list.
14519         (unless elem
14520           (setq elem (list method nil)
14521                 gnus-opened-servers (cons elem gnus-opened-servers)))
14522         ;; Set the status of this server.
14523         (setcar (cdr elem) (if result 'ok 'denied))
14524         ;; Return the result from the "open" call.
14525         result))))
14526
14527 (defun gnus-close-server (method)
14528   "Close the connection to METHOD."
14529   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14530
14531 (defun gnus-request-list (method)
14532   "Request the active file from METHOD."
14533   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14534
14535 (defun gnus-request-list-newsgroups (method)
14536   "Request the newsgroups file from METHOD."
14537   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14538
14539 (defun gnus-request-newgroups (date method)
14540   "Request all new groups since DATE from METHOD."
14541   (funcall (gnus-get-function method 'request-newgroups)
14542            date (nth 1 method)))
14543
14544 (defun gnus-server-opened (method)
14545   "Check whether a connection to METHOD has been opened."
14546   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14547
14548 (defun gnus-status-message (method)
14549   "Return the status message from METHOD.
14550 If METHOD is a string, it is interpreted as a group name.   The method
14551 this group uses will be queried."
14552   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14553                   method)))
14554     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14555
14556 (defun gnus-request-group (group &optional dont-check method)
14557   "Request GROUP.  If DONT-CHECK, no information is required."
14558   (let ((method (or method (gnus-find-method-for-group group))))
14559     (funcall (gnus-get-function method 'request-group)
14560              (gnus-group-real-name group) (nth 1 method) dont-check)))
14561
14562 (defun gnus-request-asynchronous (group &optional articles)
14563   "Request that GROUP behave asynchronously.
14564 ARTICLES is the `data' of the group."
14565   (let ((method (gnus-find-method-for-group group)))
14566     (funcall (gnus-get-function method 'request-asynchronous)
14567              (gnus-group-real-name group) (nth 1 method) articles)))
14568
14569 (defun gnus-list-active-group (group)
14570   "Request active information on GROUP."
14571   (let ((method (gnus-find-method-for-group group))
14572         (func 'list-active-group))
14573     (when (gnus-check-backend-function func group)
14574       (funcall (gnus-get-function method func)
14575                (gnus-group-real-name group) (nth 1 method)))))
14576
14577 (defun gnus-request-group-description (group)
14578   "Request a description of GROUP."
14579   (let ((method (gnus-find-method-for-group group))
14580         (func 'request-group-description))
14581     (when (gnus-check-backend-function func group)
14582       (funcall (gnus-get-function method func)
14583                (gnus-group-real-name group) (nth 1 method)))))
14584
14585 (defun gnus-close-group (group)
14586   "Request the GROUP be closed."
14587   (let ((method (gnus-find-method-for-group group)))
14588     (funcall (gnus-get-function method 'close-group)
14589              (gnus-group-real-name group) (nth 1 method))))
14590
14591 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14592   "Request headers for ARTICLES in GROUP.
14593 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14594   (let ((method (gnus-find-method-for-group group)))
14595     (if (and gnus-use-cache (numberp (car articles)))
14596         (gnus-cache-retrieve-headers articles group fetch-old)
14597       (funcall (gnus-get-function method 'retrieve-headers)
14598                articles (gnus-group-real-name group) (nth 1 method)
14599                fetch-old))))
14600
14601 (defun gnus-retrieve-groups (groups method)
14602   "Request active information on GROUPS from METHOD."
14603   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14604
14605 (defun gnus-request-type (group &optional article)
14606   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14607   (let ((method (gnus-find-method-for-group group)))
14608     (if (not (gnus-check-backend-function 'request-type (car method)))
14609         'unknown
14610       (funcall (gnus-get-function method 'request-type)
14611                (gnus-group-real-name group) article))))
14612
14613 (defun gnus-request-update-mark (group article mark)
14614   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14615   (let ((method (gnus-find-method-for-group group)))
14616     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14617         mark
14618       (funcall (gnus-get-function method 'request-update-mark)
14619                (gnus-group-real-name group) article mark))))
14620
14621 (defun gnus-request-article (article group &optional buffer)
14622   "Request the ARTICLE in GROUP.
14623 ARTICLE can either be an article number or an article Message-ID.
14624 If BUFFER, insert the article in that group."
14625   (let ((method (gnus-find-method-for-group group)))
14626     (funcall (gnus-get-function method 'request-article)
14627              article (gnus-group-real-name group) (nth 1 method) buffer)))
14628
14629 (defun gnus-request-head (article group)
14630   "Request the head of ARTICLE in GROUP."
14631   (let ((method (gnus-find-method-for-group group)))
14632     (funcall (gnus-get-function method 'request-head)
14633              article (gnus-group-real-name group) (nth 1 method))))
14634
14635 (defun gnus-request-body (article group)
14636   "Request the body of ARTICLE in GROUP."
14637   (let ((method (gnus-find-method-for-group group)))
14638     (funcall (gnus-get-function method 'request-body)
14639              article (gnus-group-real-name group) (nth 1 method))))
14640
14641 (defun gnus-request-post (method)
14642   "Post the current buffer using METHOD."
14643   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14644
14645 (defun gnus-request-scan (group method)
14646   "Request a SCAN being performed in GROUP from METHOD.
14647 If GROUP is nil, all groups on METHOD are scanned."
14648   (let ((method (if group (gnus-find-method-for-group group) method)))
14649     (funcall (gnus-get-function method 'request-scan)
14650              (and group (gnus-group-real-name group)) (nth 1 method))))
14651
14652 (defsubst gnus-request-update-info (info method)
14653   "Request that METHOD update INFO."
14654   (when (gnus-check-backend-function 'request-update-info (car method))
14655     (funcall (gnus-get-function method 'request-update-info)
14656              (gnus-group-real-name (gnus-info-group info))
14657              info (nth 1 method))))
14658
14659 (defun gnus-request-expire-articles (articles group &optional force)
14660   (let ((method (gnus-find-method-for-group group)))
14661     (funcall (gnus-get-function method 'request-expire-articles)
14662              articles (gnus-group-real-name group) (nth 1 method)
14663              force)))
14664
14665 (defun gnus-request-move-article
14666   (article group server accept-function &optional last)
14667   (let ((method (gnus-find-method-for-group group)))
14668     (funcall (gnus-get-function method 'request-move-article)
14669              article (gnus-group-real-name group)
14670              (nth 1 method) accept-function last)))
14671
14672 (defun gnus-request-accept-article (group &optional last method)
14673   ;; Make sure there's a newline at the end of the article.
14674   (goto-char (point-max))
14675   (unless (bolp)
14676     (insert "\n"))
14677   (let ((func (if (symbolp group) group
14678                 (car (or method (gnus-find-method-for-group group))))))
14679     (funcall (intern (format "%s-request-accept-article" func))
14680              (if (stringp group) (gnus-group-real-name group) group)
14681              last)))
14682
14683 (defun gnus-request-replace-article (article group buffer)
14684   (let ((func (car (gnus-find-method-for-group group))))
14685     (funcall (intern (format "%s-request-replace-article" func))
14686              article (gnus-group-real-name group) buffer)))
14687
14688 (defun gnus-request-associate-buffer (group)
14689   (let ((method (gnus-find-method-for-group group)))
14690     (funcall (gnus-get-function method 'request-associate-buffer)
14691              (gnus-group-real-name group))))
14692
14693 (defun gnus-request-restore-buffer (article group)
14694   "Request a new buffer restored to the state of ARTICLE."
14695   (let ((method (gnus-find-method-for-group group)))
14696     (funcall (gnus-get-function method 'request-restore-buffer)
14697              article (gnus-group-real-name group) (nth 1 method))))
14698
14699 (defun gnus-request-create-group (group &optional method)
14700   (let ((method (or method (gnus-find-method-for-group group))))
14701     (funcall (gnus-get-function method 'request-create-group)
14702              (gnus-group-real-name group) (nth 1 method))))
14703
14704 (defun gnus-request-delete-group (group &optional force)
14705   (let ((method (gnus-find-method-for-group group)))
14706     (funcall (gnus-get-function method 'request-delete-group)
14707              (gnus-group-real-name group) force (nth 1 method))))
14708
14709 (defun gnus-request-rename-group (group new-name)
14710   (let ((method (gnus-find-method-for-group group)))
14711     (funcall (gnus-get-function method 'request-rename-group)
14712              (gnus-group-real-name group)
14713              (gnus-group-real-name new-name) (nth 1 method))))
14714
14715 (defun gnus-member-of-valid (symbol group)
14716   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14717   (memq symbol (assoc
14718                 (symbol-name (car (gnus-find-method-for-group group)))
14719                 gnus-valid-select-methods)))
14720
14721 (defun gnus-method-option-p (method option)
14722   "Return non-nil if select METHOD has OPTION as a parameter."
14723   (memq option (assoc (format "%s" (car method))
14724                       gnus-valid-select-methods)))
14725
14726 (defun gnus-server-extend-method (group method)
14727   ;; This function "extends" a virtual server.  If the server is
14728   ;; "hello", and the select method is ("hello" (my-var "something"))
14729   ;; in the group "alt.alt", this will result in a new virtual server
14730   ;; called "hello+alt.alt".
14731   (let ((entry
14732          (gnus-copy-sequence
14733           (if (equal (car method) "native") gnus-select-method
14734             (cdr (assoc (car method) gnus-server-alist))))))
14735     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14736     (nconc entry (cdr method))))
14737
14738 (defun gnus-find-method-for-group (group &optional info)
14739   "Find the select method that GROUP uses."
14740   (or gnus-override-method
14741       (and (not group)
14742            gnus-select-method)
14743       (let ((info (or info (gnus-get-info group)))
14744             method)
14745         (if (or (not info)
14746                 (not (setq method (gnus-info-method info))))
14747             (setq method gnus-select-method)
14748           (setq method
14749                 (cond ((stringp method)
14750                        (gnus-server-to-method method))
14751                       ((stringp (car method))
14752                        (gnus-server-extend-method group method))
14753                       (t
14754                        method)))
14755           (gnus-server-add-address method)))))
14756
14757 (defun gnus-check-backend-function (func group)
14758   "Check whether GROUP supports function FUNC."
14759   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14760                   group)))
14761     (fboundp (intern (format "%s-%s" method func)))))
14762
14763 (defun gnus-methods-using (feature)
14764   "Find all methods that have FEATURE."
14765   (let ((valids gnus-valid-select-methods)
14766         outs)
14767     (while valids
14768       (if (memq feature (car valids))
14769           (setq outs (cons (car valids) outs)))
14770       (setq valids (cdr valids)))
14771     outs))
14772
14773 \f
14774 ;;;
14775 ;;; Active & Newsrc File Handling
14776 ;;;
14777
14778 (defun gnus-setup-news (&optional rawfile level)
14779   "Setup news information.
14780 If RAWFILE is non-nil, the .newsrc file will also be read.
14781 If LEVEL is non-nil, the news will be set up at level LEVEL."
14782   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14783     ;; Clear some variables to re-initialize news information.
14784     (if init (setq gnus-newsrc-alist nil
14785                    gnus-active-hashtb nil))
14786
14787     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14788     (if init (gnus-read-newsrc-file rawfile))
14789
14790     (when (and (not (assoc "archive" gnus-server-alist))
14791                gnus-message-archive-method)
14792       (push (cons "archive" gnus-message-archive-method)
14793             gnus-server-alist))
14794
14795     ;; If we don't read the complete active file, we fill in the
14796     ;; hashtb here.
14797     (if (or (null gnus-read-active-file)
14798             (eq gnus-read-active-file 'some))
14799         (gnus-update-active-hashtb-from-killed))
14800
14801     ;; Read the active file and create `gnus-active-hashtb'.
14802     ;; If `gnus-read-active-file' is nil, then we just create an empty
14803     ;; hash table.  The partial filling out of the hash table will be
14804     ;; done in `gnus-get-unread-articles'.
14805     (and gnus-read-active-file
14806          (not level)
14807          (gnus-read-active-file))
14808
14809     (or gnus-active-hashtb
14810         (setq gnus-active-hashtb (make-vector 4095 0)))
14811
14812     ;; Initialize the cache.
14813     (when gnus-use-cache
14814       (gnus-cache-open))
14815
14816     ;; Possibly eval the dribble file.
14817     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14818
14819     (gnus-update-format-specifications)
14820
14821     ;; Find new newsgroups and treat them.
14822     (if (and init gnus-check-new-newsgroups (not level)
14823              (gnus-check-server gnus-select-method))
14824         (gnus-find-new-newsgroups))
14825
14826     ;; Find the number of unread articles in each non-dead group.
14827     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14828       (gnus-get-unread-articles level))
14829
14830     (if (and init gnus-check-bogus-newsgroups
14831              gnus-read-active-file (not level)
14832              (gnus-server-opened gnus-select-method))
14833         (gnus-check-bogus-newsgroups))))
14834
14835 (defun gnus-find-new-newsgroups (&optional arg)
14836   "Search for new newsgroups and add them.
14837 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14838 The `-n' option line from .newsrc is respected.
14839 If ARG (the prefix), use the `ask-server' method to query
14840 the server for new groups."
14841   (interactive "P")
14842   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14843                        (null gnus-read-active-file)
14844                        (eq gnus-read-active-file 'some))
14845                    'ask-server gnus-check-new-newsgroups)))
14846     (unless (gnus-check-first-time-used)
14847       (if (or (consp check)
14848               (eq check 'ask-server))
14849           ;; Ask the server for new groups.
14850           (gnus-ask-server-for-new-groups)
14851         ;; Go through the active hashtb and look for new groups.
14852         (let ((groups 0)
14853               group new-newsgroups)
14854           (gnus-message 5 "Looking for new newsgroups...")
14855           (unless gnus-have-read-active-file
14856             (gnus-read-active-file))
14857           (setq gnus-newsrc-last-checked-date (current-time-string))
14858           (unless gnus-killed-hashtb
14859             (gnus-make-hashtable-from-killed))
14860           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14861           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14862           (mapatoms
14863            (lambda (sym)
14864              (if (or (null (setq group (symbol-name sym)))
14865                      (not (boundp sym))
14866                      (null (symbol-value sym))
14867                      (gnus-gethash group gnus-killed-hashtb)
14868                      (gnus-gethash group gnus-newsrc-hashtb))
14869                  ()
14870                (let ((do-sub (gnus-matches-options-n group)))
14871                  (cond
14872                   ((eq do-sub 'subscribe)
14873                    (setq groups (1+ groups))
14874                    (gnus-sethash group group gnus-killed-hashtb)
14875                    (funcall gnus-subscribe-options-newsgroup-method group))
14876                   ((eq do-sub 'ignore)
14877                    nil)
14878                   (t
14879                    (setq groups (1+ groups))
14880                    (gnus-sethash group group gnus-killed-hashtb)
14881                    (if gnus-subscribe-hierarchical-interactive
14882                        (setq new-newsgroups (cons group new-newsgroups))
14883                      (funcall gnus-subscribe-newsgroup-method group)))))))
14884            gnus-active-hashtb)
14885           (when new-newsgroups
14886             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14887           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14888           (if (> groups 0)
14889               (gnus-message 6 "%d new newsgroup%s arrived."
14890                             groups (if (> groups 1) "s have" " has"))
14891             (gnus-message 6 "No new newsgroups.")))))))
14892
14893 (defun gnus-matches-options-n (group)
14894   ;; Returns `subscribe' if the group is to be unconditionally
14895   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14896   ;; no match for the group.
14897
14898   ;; First we check the two user variables.
14899   (cond
14900    ((and gnus-options-subscribe
14901          (string-match gnus-options-subscribe group))
14902     'subscribe)
14903    ((and gnus-auto-subscribed-groups
14904          (string-match gnus-auto-subscribed-groups group))
14905     'subscribe)
14906    ((and gnus-options-not-subscribe
14907          (string-match gnus-options-not-subscribe group))
14908     'ignore)
14909    ;; Then we go through the list that was retrieved from the .newsrc
14910    ;; file.  This list has elements on the form
14911    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14912    ;; is in the reverse order of the options line) is returned.
14913    (t
14914     (let ((regs gnus-newsrc-options-n))
14915       (while (and regs
14916                   (not (string-match (car (car regs)) group)))
14917         (setq regs (cdr regs)))
14918       (and regs (cdr (car regs)))))))
14919
14920 (defun gnus-ask-server-for-new-groups ()
14921   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14922          (methods (cons gnus-select-method
14923                         (nconc
14924                          (when gnus-message-archive-method
14925                            (list "archive"))
14926                          (append
14927                           (and (consp gnus-check-new-newsgroups)
14928                                gnus-check-new-newsgroups)
14929                           gnus-secondary-select-methods))))
14930          (groups 0)
14931          (new-date (current-time-string))
14932          group new-newsgroups got-new method hashtb
14933          gnus-override-subscribe-method)
14934     ;; Go through both primary and secondary select methods and
14935     ;; request new newsgroups.
14936     (while (setq method (gnus-server-get-method nil (pop methods)))
14937       (setq new-newsgroups nil)
14938       (setq gnus-override-subscribe-method method)
14939       (when (and (gnus-check-server method)
14940                  (gnus-request-newgroups date method))
14941         (save-excursion
14942           (setq got-new t)
14943           (setq hashtb (gnus-make-hashtable 100))
14944           (set-buffer nntp-server-buffer)
14945           ;; Enter all the new groups into a hashtable.
14946           (gnus-active-to-gnus-format method hashtb 'ignore)))
14947       ;; Now all new groups from `method' are in `hashtb'.
14948       (mapatoms
14949        (lambda (group-sym)
14950          (if (or (null (setq group (symbol-name group-sym)))
14951                  (null (symbol-value group-sym))
14952                  (gnus-gethash group gnus-newsrc-hashtb)
14953                  (member group gnus-zombie-list)
14954                  (member group gnus-killed-list))
14955              ;; The group is already known.
14956              ()
14957            ;; Make this group active.
14958            (when (symbol-value group-sym)
14959              (gnus-set-active group (symbol-value group-sym)))
14960            ;; Check whether we want it or not.
14961            (let ((do-sub (gnus-matches-options-n group)))
14962              (cond
14963               ((eq do-sub 'subscribe)
14964                (incf groups)
14965                (gnus-sethash group group gnus-killed-hashtb)
14966                (funcall gnus-subscribe-options-newsgroup-method group))
14967               ((eq do-sub 'ignore)
14968                nil)
14969               (t
14970                (incf groups)
14971                (gnus-sethash group group gnus-killed-hashtb)
14972                (if gnus-subscribe-hierarchical-interactive
14973                    (push group new-newsgroups)
14974                  (funcall gnus-subscribe-newsgroup-method group)))))))
14975        hashtb)
14976       (when new-newsgroups
14977         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
14978     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14979     (when (> groups 0)
14980       (gnus-message 6 "%d new newsgroup%s arrived."
14981                     groups (if (> groups 1) "s have" " has")))
14982     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14983     got-new))
14984
14985 (defun gnus-check-first-time-used ()
14986   (if (or (> (length gnus-newsrc-alist) 1)
14987           (file-exists-p gnus-startup-file)
14988           (file-exists-p (concat gnus-startup-file ".el"))
14989           (file-exists-p (concat gnus-startup-file ".eld")))
14990       nil
14991     (gnus-message 6 "First time user; subscribing you to default groups")
14992     (unless gnus-have-read-active-file
14993       (gnus-read-active-file))
14994     (setq gnus-newsrc-last-checked-date (current-time-string))
14995     (let ((groups gnus-default-subscribed-newsgroups)
14996           group)
14997       (if (eq groups t)
14998           nil
14999         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15000         (mapatoms
15001          (lambda (sym)
15002            (if (null (setq group (symbol-name sym)))
15003                ()
15004              (let ((do-sub (gnus-matches-options-n group)))
15005                (cond
15006                 ((eq do-sub 'subscribe)
15007                  (gnus-sethash group group gnus-killed-hashtb)
15008                  (funcall gnus-subscribe-options-newsgroup-method group))
15009                 ((eq do-sub 'ignore)
15010                  nil)
15011                 (t
15012                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15013          gnus-active-hashtb)
15014         (while groups
15015           (if (gnus-active (car groups))
15016               (gnus-group-change-level
15017                (car groups) gnus-level-default-subscribed gnus-level-killed))
15018           (setq groups (cdr groups)))
15019         (gnus-group-make-help-group)
15020         (and gnus-novice-user
15021              (gnus-message 7 "`A k' to list killed groups"))))))
15022
15023 (defun gnus-subscribe-group (group previous &optional method)
15024   (gnus-group-change-level
15025    (if method
15026        (list t group gnus-level-default-subscribed nil nil method)
15027      group)
15028    gnus-level-default-subscribed gnus-level-killed previous t))
15029
15030 ;; `gnus-group-change-level' is the fundamental function for changing
15031 ;; subscription levels of newsgroups.  This might mean just changing
15032 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15033 ;; again, which subscribes/unsubscribes a group, which is equally
15034 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15035 ;; from 8-9 to 1-7 means that you remove the group from the list of
15036 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15037 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15038 ;; which is trivial.
15039 ;; ENTRY can either be a string (newsgroup name) or a list (if
15040 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15041 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15042 ;; entries.
15043 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15044 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15045 ;; after.
15046 (defun gnus-group-change-level (entry level &optional oldlevel
15047                                       previous fromkilled)
15048   (let (group info active num)
15049     ;; Glean what info we can from the arguments
15050     (if (consp entry)
15051         (if fromkilled (setq group (nth 1 entry))
15052           (setq group (car (nth 2 entry))))
15053       (setq group entry))
15054     (if (and (stringp entry)
15055              oldlevel
15056              (< oldlevel gnus-level-zombie))
15057         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15058     (if (and (not oldlevel)
15059              (consp entry))
15060         (setq oldlevel (car (cdr (nth 2 entry)))))
15061     (if (stringp previous)
15062         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15063
15064     (if (and (>= oldlevel gnus-level-zombie)
15065              (gnus-gethash group gnus-newsrc-hashtb))
15066         ;; We are trying to subscribe a group that is already
15067         ;; subscribed.
15068         ()                              ; Do nothing.
15069
15070       (or (gnus-ephemeral-group-p group)
15071           (gnus-dribble-enter
15072            (format "(gnus-group-change-level %S %S %S %S %S)"
15073                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15074
15075       ;; Then we remove the newgroup from any old structures, if needed.
15076       ;; If the group was killed, we remove it from the killed or zombie
15077       ;; list.  If not, and it is in fact going to be killed, we remove
15078       ;; it from the newsrc hash table and assoc.
15079       (cond
15080        ((>= oldlevel gnus-level-zombie)
15081         (if (= oldlevel gnus-level-zombie)
15082             (setq gnus-zombie-list (delete group gnus-zombie-list))
15083           (setq gnus-killed-list (delete group gnus-killed-list))))
15084        (t
15085         (if (and (>= level gnus-level-zombie)
15086                  entry)
15087             (progn
15088               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15089               (if (nth 3 entry)
15090                   (setcdr (gnus-gethash (car (nth 3 entry))
15091                                         gnus-newsrc-hashtb)
15092                           (cdr entry)))
15093               (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
15094
15095       ;; Finally we enter (if needed) the list where it is supposed to
15096       ;; go, and change the subscription level.  If it is to be killed,
15097       ;; we enter it into the killed or zombie list.
15098       (cond 
15099        ((>= level gnus-level-zombie)
15100         ;; Remove from the hash table.
15101         (gnus-sethash group nil gnus-newsrc-hashtb)
15102         ;; We do not enter foreign groups into the list of dead
15103         ;; groups.
15104         (unless (gnus-group-foreign-p group)
15105           (if (= level gnus-level-zombie)
15106               (setq gnus-zombie-list (cons group gnus-zombie-list))
15107             (setq gnus-killed-list (cons group gnus-killed-list)))))
15108        (t
15109         ;; If the list is to be entered into the newsrc assoc, and
15110         ;; it was killed, we have to create an entry in the newsrc
15111         ;; hashtb format and fix the pointers in the newsrc assoc.
15112         (if (< oldlevel gnus-level-zombie)
15113             ;; It was alive, and it is going to stay alive, so we
15114             ;; just change the level and don't change any pointers or
15115             ;; hash table entries.
15116             (setcar (cdr (car (cdr (cdr entry)))) level)
15117           (if (listp entry)
15118               (setq info (cdr entry)
15119                     num (car entry))
15120             (setq active (gnus-active group))
15121             (setq num
15122                   (if active (- (1+ (cdr active)) (car active)) t))
15123             ;; Check whether the group is foreign.  If so, the
15124             ;; foreign select method has to be entered into the
15125             ;; info.
15126             (let ((method (or gnus-override-subscribe-method
15127                               (gnus-group-method group))))
15128               (if (eq method gnus-select-method)
15129                   (setq info (list group level nil))
15130                 (setq info (list group level nil nil method)))))
15131           (unless previous
15132             (setq previous
15133                   (let ((p gnus-newsrc-alist))
15134                     (while (cdr (cdr p))
15135                       (setq p (cdr p)))
15136                     p)))
15137           (setq entry (cons info (cdr (cdr previous))))
15138           (if (cdr previous)
15139               (progn
15140                 (setcdr (cdr previous) entry)
15141                 (gnus-sethash group (cons num (cdr previous))
15142                               gnus-newsrc-hashtb))
15143             (setcdr previous entry)
15144             (gnus-sethash group (cons num previous)
15145                           gnus-newsrc-hashtb))
15146           (when (cdr entry)
15147             (setcdr (gnus-gethash (car (car (cdr entry))) gnus-newsrc-hashtb)
15148                     entry)))))
15149       (when gnus-group-change-level-function
15150         (funcall gnus-group-change-level-function group level oldlevel)))))
15151
15152 (defun gnus-kill-newsgroup (newsgroup)
15153   "Obsolete function.  Kills a newsgroup."
15154   (gnus-group-change-level
15155    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15156
15157 (defun gnus-check-bogus-newsgroups (&optional confirm)
15158   "Remove bogus newsgroups.
15159 If CONFIRM is non-nil, the user has to confirm the deletion of every
15160 newsgroup."
15161   (let ((newsrc (cdr gnus-newsrc-alist))
15162         bogus group entry info)
15163     (gnus-message 5 "Checking bogus newsgroups...")
15164     (unless gnus-have-read-active-file
15165       (gnus-read-active-file))
15166     (when (member gnus-select-method gnus-have-read-active-file)
15167       ;; Find all bogus newsgroup that are subscribed.
15168       (while newsrc
15169         (setq info (pop newsrc)
15170               group (gnus-info-group info))
15171         (unless (or (gnus-active group) ; Active
15172                     (gnus-info-method info) ; Foreign
15173                     (and confirm
15174                          (not (gnus-y-or-n-p
15175                                (format "Remove bogus newsgroup: %s " group)))))
15176           ;; Found a bogus newsgroup.
15177           (push group bogus)))
15178       ;; Remove all bogus subscribed groups by first killing them, and
15179       ;; then removing them from the list of killed groups.
15180       (while bogus
15181         (when (setq entry (gnus-gethash (setq group (pop bogus))
15182                                         gnus-newsrc-hashtb))
15183           (gnus-group-change-level entry gnus-level-killed)
15184           (setq gnus-killed-list (delete group gnus-killed-list))))
15185       ;; Then we remove all bogus groups from the list of killed and
15186       ;; zombie groups.  They are are removed without confirmation.
15187       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15188             killed)
15189         (while dead-lists
15190           (setq killed (symbol-value (car dead-lists)))
15191           (while killed
15192             (unless (gnus-active (setq group (pop killed)))
15193               ;; The group is bogus.
15194               ;; !!!Slow as hell.
15195               (set (car dead-lists)
15196                    (delete group (symbol-value (car dead-lists))))))
15197           (setq dead-lists (cdr dead-lists))))
15198       (gnus-message 5 "Checking bogus newsgroups...done"))))
15199
15200 (defun gnus-check-duplicate-killed-groups ()
15201   "Remove duplicates from the list of killed groups."
15202   (interactive)
15203   (let ((killed gnus-killed-list))
15204     (while killed
15205       (gnus-message 9 "%d" (length killed))
15206       (setcdr killed (delete (car killed) (cdr killed)))
15207       (setq killed (cdr killed)))))
15208
15209 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15210 ;; and compute how many unread articles there are in each group.
15211 (defun gnus-get-unread-articles (&optional level)
15212   (let* ((newsrc (cdr gnus-newsrc-alist))
15213          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15214          (foreign-level
15215           (min
15216            (cond ((and gnus-activate-foreign-newsgroups
15217                        (not (numberp gnus-activate-foreign-newsgroups)))
15218                   (1+ gnus-level-subscribed))
15219                  ((numberp gnus-activate-foreign-newsgroups)
15220                   gnus-activate-foreign-newsgroups)
15221                  (t 0))
15222            level))
15223          info group active virtuals method)
15224     (gnus-message 5 "Checking new news...")
15225
15226     (while newsrc
15227       (setq info (car newsrc)
15228             group (gnus-info-group info)
15229             active (gnus-active group))
15230
15231       ;; Check newsgroups.  If the user doesn't want to check them, or
15232       ;; they can't be checked (for instance, if the news server can't
15233       ;; be reached) we just set the number of unread articles in this
15234       ;; newsgroup to t.  This means that Gnus thinks that there are
15235       ;; unread articles, but it has no idea how many.
15236       (if (and (setq method (gnus-info-method info))
15237                (not (gnus-server-equal
15238                      gnus-select-method
15239                      (gnus-server-get-method nil method)))
15240                (not (gnus-secondary-method-p method)))
15241           ;; These groups are foreign.  Check the level.
15242           (when (<= (gnus-info-level info) foreign-level)
15243             (setq active (gnus-activate-group group 'scan))
15244             (gnus-close-group group))
15245
15246         ;; These groups are native or secondary.
15247         (when (and (<= (gnus-info-level info) level)
15248                    (not gnus-read-active-file))
15249           (setq active (gnus-activate-group group 'scan))
15250           (gnus-close-group group)))
15251
15252       (if active
15253           (gnus-get-unread-articles-in-group info active t)
15254         ;; The group couldn't be reached, so we nix out the number of
15255         ;; unread articles and stuff.
15256         (gnus-set-active group nil)
15257         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
15258
15259       (setq newsrc (cdr newsrc)))
15260
15261     (gnus-message 5 "Checking new news...done")))
15262
15263 ;; Create a hash table out of the newsrc alist.  The `car's of the
15264 ;; alist elements are used as keys.
15265 (defun gnus-make-hashtable-from-newsrc-alist ()
15266   (let ((alist gnus-newsrc-alist)
15267         (ohashtb gnus-newsrc-hashtb)
15268         prev)
15269     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15270     (setq alist
15271           (setq prev (setq gnus-newsrc-alist
15272                            (if (equal (car (car gnus-newsrc-alist))
15273                                       "dummy.group")
15274                                gnus-newsrc-alist
15275                              (cons (list "dummy.group" 0 nil) alist)))))
15276     (while alist
15277       (gnus-sethash
15278        (car (car alist))
15279        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb)))
15280              prev)
15281        gnus-newsrc-hashtb)
15282       (setq prev alist
15283             alist (cdr alist)))))
15284
15285 (defun gnus-make-hashtable-from-killed ()
15286   "Create a hash table from the killed and zombie lists."
15287   (let ((lists '(gnus-killed-list gnus-zombie-list))
15288         list)
15289     (setq gnus-killed-hashtb
15290           (gnus-make-hashtable
15291            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15292     (while lists
15293       (setq list (symbol-value (car lists)))
15294       (setq lists (cdr lists))
15295       (while list
15296         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15297         (setq list (cdr list))))))
15298
15299 (defun gnus-get-unread-articles-in-group (info active &optional update)
15300   (when active
15301     ;; Allow the backend to update the info in the group.
15302     (when update 
15303       (gnus-request-update-info
15304        info (gnus-find-method-for-group (gnus-info-group info))))
15305     (let* ((range (gnus-info-read info))
15306            (num 0)
15307            (marked (gnus-info-marks info)))
15308       ;; If a cache is present, we may have to alter the active info.
15309       (and gnus-use-cache
15310            (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15311       ;; Modify the list of read articles according to what articles
15312       ;; are available; then tally the unread articles and add the
15313       ;; number to the group hash table entry.
15314       (cond
15315        ((zerop (cdr active))
15316         (setq num 0))
15317        ((not range)
15318         (setq num (- (1+ (cdr active)) (car active))))
15319        ((not (listp (cdr range)))
15320         ;; Fix a single (num . num) range according to the
15321         ;; active hash table.
15322         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15323         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15324         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15325         ;; Compute number of unread articles.
15326         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15327        (t
15328         ;; The read list is a list of ranges.  Fix them according to
15329         ;; the active hash table.
15330         ;; First peel off any elements that are below the lower
15331         ;; active limit.
15332         (while (and (cdr range)
15333                     (>= (car active)
15334                         (or (and (atom (car (cdr range))) (car (cdr range)))
15335                             (car (car (cdr range))))))
15336           (if (numberp (car range))
15337               (setcar range
15338                       (cons (car range)
15339                             (or (and (numberp (car (cdr range)))
15340                                      (car (cdr range)))
15341                                 (cdr (car (cdr range))))))
15342             (setcdr (car range)
15343                     (or (and (numberp (nth 1 range)) (nth 1 range))
15344                         (cdr (car (cdr range))))))
15345           (setcdr range (cdr (cdr range))))
15346         ;; Adjust the first element to be the same as the lower limit.
15347         (if (and (not (atom (car range)))
15348                  (< (cdr (car range)) (car active)))
15349             (setcdr (car range) (1- (car active))))
15350         ;; Then we want to peel off any elements that are higher
15351         ;; than the upper active limit.
15352         (let ((srange range))
15353           ;; Go past all legal elements.
15354           (while (and (cdr srange)
15355                       (<= (or (and (atom (car (cdr srange)))
15356                                    (car (cdr srange)))
15357                               (car (car (cdr srange)))) (cdr active)))
15358             (setq srange (cdr srange)))
15359           (if (cdr srange)
15360               ;; Nuke all remaining illegal elements.
15361               (setcdr srange nil))
15362
15363           ;; Adjust the final element.
15364           (if (and (not (atom (car srange)))
15365                    (> (cdr (car srange)) (cdr active)))
15366               (setcdr (car srange) (cdr active))))
15367         ;; Compute the number of unread articles.
15368         (while range
15369           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15370                                       (cdr (car range))))
15371                               (or (and (atom (car range)) (car range))
15372                                   (car (car range))))))
15373           (setq range (cdr range)))
15374         (setq num (max 0 (- (cdr active) num)))))
15375       ;; Set the number of unread articles.
15376       (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
15377       num)))
15378
15379 (defun gnus-activate-group (group &optional scan)
15380   ;; Check whether a group has been activated or not.
15381   ;; If SCAN, request a scan of that group as well.
15382   (let ((method (gnus-find-method-for-group group))
15383         active)
15384     (and (gnus-check-server method)
15385          ;; We escape all bugs and quit here to make it possible to
15386          ;; continue if a group is so out-there that it reports bugs
15387          ;; and stuff.
15388          (progn
15389            (and scan
15390                 (gnus-check-backend-function 'request-scan (car method))
15391                 (gnus-request-scan group method))
15392            t)
15393          (condition-case ()
15394              (gnus-request-group group)
15395         ;   (error nil)
15396            (quit nil))
15397          (save-excursion
15398            (set-buffer nntp-server-buffer)
15399            (goto-char (point-min))
15400            ;; Parse the result we got from `gnus-request-group'.
15401            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15402                 (progn
15403                   (goto-char (match-beginning 1))
15404                   (gnus-set-active
15405                    group (setq active (cons (read (current-buffer))
15406                                             (read (current-buffer)))))
15407                   ;; Return the new active info.
15408                   active))))))
15409
15410 (defun gnus-update-read-articles (group unread)
15411   "Update the list of read and ticked articles in GROUP using the
15412 UNREAD and TICKED lists.
15413 Note: UNSELECTED has to be sorted over `<'.
15414 Returns whether the updating was successful."
15415   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15416          (entry (gnus-gethash group gnus-newsrc-hashtb))
15417          (info (nth 2 entry))
15418          (marked (gnus-info-marks info))
15419          (prev 1)
15420          (unread (sort (copy-sequence unread) '<))
15421          read)
15422     (if (or (not info) (not active))
15423         ;; There is no info on this group if it was, in fact,
15424         ;; killed.  Gnus stores no information on killed groups, so
15425         ;; there's nothing to be done.
15426         ;; One could store the information somewhere temporarily,
15427         ;; perhaps...  Hmmm...
15428         ()
15429       ;; Remove any negative articles numbers.
15430       (while (and unread (< (car unread) 0))
15431         (setq unread (cdr unread)))
15432       ;; Remove any expired article numbers
15433       (while (and unread (< (car unread) (car active)))
15434         (setq unread (cdr unread)))
15435       ;; Compute the ranges of read articles by looking at the list of
15436       ;; unread articles.
15437       (while unread
15438         (if (/= (car unread) prev)
15439             (setq read (cons (if (= prev (1- (car unread))) prev
15440                                (cons prev (1- (car unread)))) read)))
15441         (setq prev (1+ (car unread)))
15442         (setq unread (cdr unread)))
15443       (when (<= prev (cdr active))
15444         (setq read (cons (cons prev (cdr active)) read)))
15445       ;; Enter this list into the group info.
15446       (gnus-info-set-read
15447        info (if (> (length read) 1) (nreverse read) read))
15448       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15449       (gnus-get-unread-articles-in-group info (gnus-active group))
15450       t)))
15451
15452 (defun gnus-make-articles-unread (group articles)
15453   "Mark ARTICLES in GROUP as unread."
15454   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15455                           (gnus-gethash (gnus-group-real-name group)
15456                                         gnus-newsrc-hashtb))))
15457          (ranges (gnus-info-read info))
15458          news article)
15459     (while articles
15460       (when (gnus-member-of-range
15461              (setq article (pop articles)) ranges)
15462         (setq news (cons article news))))
15463     (when news
15464       (gnus-info-set-read
15465        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15466       (gnus-group-update-group group t))))
15467
15468 ;; Enter all dead groups into the hashtb.
15469 (defun gnus-update-active-hashtb-from-killed ()
15470   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15471         (lists (list gnus-killed-list gnus-zombie-list))
15472         killed)
15473     (while lists
15474       (setq killed (car lists))
15475       (while killed
15476         (gnus-sethash (car killed) nil hashtb)
15477         (setq killed (cdr killed)))
15478       (setq lists (cdr lists)))))
15479
15480 ;; Get the active file(s) from the backend(s).
15481 (defun gnus-read-active-file ()
15482   (gnus-group-set-mode-line)
15483   (let ((methods 
15484          (append
15485           (if (gnus-check-server gnus-select-method)
15486               ;; The native server is available.
15487               (cons gnus-select-method gnus-secondary-select-methods)
15488             ;; The native server is down, so we just do the
15489             ;; secondary ones.
15490             gnus-secondary-select-methods)
15491           ;; Also read from the archive server.
15492           (when gnus-message-archive-method
15493             (list "archive"))))
15494         list-type)
15495     (setq gnus-have-read-active-file nil)
15496     (save-excursion
15497       (set-buffer nntp-server-buffer)
15498       (while methods
15499         (let* ((method (if (stringp (car methods))
15500                            (gnus-server-get-method nil (car methods))
15501                          (car methods)))
15502                (where (nth 1 method))
15503                (mesg (format "Reading active file%s via %s..."
15504                              (if (and where (not (zerop (length where))))
15505                                  (concat " from " where) "")
15506                              (car method))))
15507           (gnus-message 5 mesg)
15508           (if (not (gnus-check-server method))
15509               ()
15510             ;; Request that the backend scan its incoming messages.
15511             (and (gnus-check-backend-function 'request-scan (car method))
15512                  (gnus-request-scan nil method))
15513             (cond
15514              ((and (eq gnus-read-active-file 'some)
15515                    (gnus-check-backend-function 'retrieve-groups (car method)))
15516               (let ((newsrc (cdr gnus-newsrc-alist))
15517                     (gmethod (gnus-server-get-method nil method))
15518                     groups)
15519                 (while newsrc
15520                   (and (gnus-server-equal
15521                         (gnus-find-method-for-group
15522                          (car (car newsrc)) (car newsrc))
15523                         gmethod)
15524                        (setq groups (cons (gnus-group-real-name
15525                                            (car (car newsrc))) groups)))
15526                   (setq newsrc (cdr newsrc)))
15527                 (gnus-check-server method)
15528                 (setq list-type (gnus-retrieve-groups groups method))
15529                 (cond
15530                  ((not list-type)
15531                   (gnus-message
15532                    1 "Cannot read partial active file from %s server."
15533                    (car method))
15534                   (ding)
15535                   (sit-for 2))
15536                  ((eq list-type 'active)
15537                   (gnus-active-to-gnus-format method gnus-active-hashtb))
15538                  (t
15539                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
15540              (t
15541               (if (not (gnus-request-list method))
15542                   (progn
15543                     (unless (equal method gnus-message-archive-method)
15544                       (gnus-message 1 "Cannot read active file from %s server."
15545                                     (car method))
15546                       (ding)))
15547                 (gnus-active-to-gnus-format method)
15548                 ;; We mark this active file as read.
15549                 (push method gnus-have-read-active-file)
15550                 (gnus-message 5 "%sdone" mesg))))))
15551         (setq methods (cdr methods))))))
15552
15553 ;; Read an active file and place the results in `gnus-active-hashtb'.
15554 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15555   (unless method
15556     (setq method gnus-select-method))
15557   (let ((cur (current-buffer))
15558         (hashtb (or hashtb
15559                     (if (and gnus-active-hashtb
15560                              (not (equal method gnus-select-method)))
15561                         gnus-active-hashtb
15562                       (setq gnus-active-hashtb
15563                             (if (equal method gnus-select-method)
15564                                 (gnus-make-hashtable
15565                                  (count-lines (point-min) (point-max)))
15566                               (gnus-make-hashtable 4096))))))
15567         (flag-hashtb (gnus-make-hashtable 60)))
15568     ;; Delete unnecessary lines.
15569     (goto-char (point-min))
15570     (while (search-forward "\nto." nil t)
15571       (delete-region (1+ (match-beginning 0))
15572                      (progn (forward-line 1) (point))))
15573     (or (string= gnus-ignored-newsgroups "")
15574         (progn
15575           (goto-char (point-min))
15576           (delete-matching-lines gnus-ignored-newsgroups)))
15577     ;; Make the group names readable as a lisp expression even if they
15578     ;; contain special characters.
15579     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15580     (goto-char (point-max))
15581     (while (re-search-backward "[][';?()#]" nil t)
15582       (insert ?\\))
15583     ;; If these are groups from a foreign select method, we insert the
15584     ;; group prefix in front of the group names.
15585     (and method (not (gnus-server-equal
15586                       (gnus-server-get-method nil method)
15587                       (gnus-server-get-method nil gnus-select-method)))
15588          (let ((prefix (gnus-group-prefixed-name "" method)))
15589            (goto-char (point-min))
15590            (while (and (not (eobp))
15591                        (progn (insert prefix)
15592                               (zerop (forward-line 1)))))))
15593     ;; Store the active file in a hash table.
15594     (goto-char (point-min))
15595     (if (string-match "%[oO]" gnus-group-line-format)
15596         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15597         ;; If we want information on moderated groups, we use this
15598         ;; loop...
15599         (let* ((mod-hashtb (make-vector 7 0))
15600                (m (intern "m" mod-hashtb))
15601                group max min)
15602           (while (not (eobp))
15603             (condition-case nil
15604                 (progn
15605                   (narrow-to-region (point) (gnus-point-at-eol))
15606                   (setq group (let ((obarray hashtb)) (read cur)))
15607                   (if (and (numberp (setq max (read cur)))
15608                            (numberp (setq min (read cur)))
15609                            (progn
15610                              (skip-chars-forward " \t")
15611                              (not
15612                               (or (= (following-char) ?=)
15613                                   (= (following-char) ?x)
15614                                   (= (following-char) ?j)))))
15615                       (set group (cons min max))
15616                     (set group nil))
15617                   ;; Enter moderated groups into a list.
15618                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15619                       (setq gnus-moderated-list
15620                             (cons (symbol-name group) gnus-moderated-list))))
15621               (error
15622                (and group
15623                     (symbolp group)
15624                     (set group nil))))
15625             (widen)
15626             (forward-line 1)))
15627       ;; And if we do not care about moderation, we use this loop,
15628       ;; which is faster.
15629       (let (group max min)
15630         (while (not (eobp))
15631           (condition-case ()
15632               (progn
15633                 (narrow-to-region (point) (gnus-point-at-eol))
15634                 ;; group gets set to a symbol interned in the hash table
15635                 ;; (what a hack!!) - jwz
15636                 (setq group (let ((obarray hashtb)) (read cur)))
15637                 (if (and (numberp (setq max (read cur)))
15638                          (numberp (setq min (read cur)))
15639                          (progn
15640                            (skip-chars-forward " \t")
15641                            (not
15642                             (or (= (following-char) ?=)
15643                                 (= (following-char) ?x)
15644                                 (= (following-char) ?j)))))
15645                     (set group (cons min max))
15646                   (set group nil)))
15647             (error
15648              (progn
15649                (and group
15650                     (symbolp group)
15651                     (set group nil))
15652                (or ignore-errors
15653                    (gnus-message 3 "Warning - illegal active: %s"
15654                                  (buffer-substring
15655                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15656           (widen)
15657           (forward-line 1))))))
15658
15659 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15660   ;; Parse a "groups" active file.
15661   (let ((cur (current-buffer))
15662         (hashtb (or hashtb
15663                     (if (and method gnus-active-hashtb)
15664                         gnus-active-hashtb
15665                       (setq gnus-active-hashtb
15666                             (gnus-make-hashtable
15667                              (count-lines (point-min) (point-max)))))))
15668         (prefix (and method
15669                      (not (gnus-server-equal
15670                            (gnus-server-get-method nil method)
15671                            (gnus-server-get-method nil gnus-select-method)))
15672                      (gnus-group-prefixed-name "" method))))
15673
15674     (goto-char (point-min))
15675     ;; We split this into to separate loops, one with the prefix
15676     ;; and one without to speed the reading up somewhat.
15677     (if prefix
15678         (let (min max opoint group)
15679           (while (not (eobp))
15680             (condition-case ()
15681                 (progn
15682                   (read cur) (read cur)
15683                   (setq min (read cur)
15684                         max (read cur)
15685                         opoint (point))
15686                   (skip-chars-forward " \t")
15687                   (insert prefix)
15688                   (goto-char opoint)
15689                   (set (let ((obarray hashtb)) (read cur))
15690                        (cons min max)))
15691               (error (and group (symbolp group) (set group nil))))
15692             (forward-line 1)))
15693       (let (min max group)
15694         (while (not (eobp))
15695           (condition-case ()
15696               (if (= (following-char) ?2)
15697                   (progn
15698                     (read cur) (read cur)
15699                     (setq min (read cur)
15700                           max (read cur))
15701                     (set (setq group (let ((obarray hashtb)) (read cur)))
15702                          (cons min max))))
15703             (error (and group (symbolp group) (set group nil))))
15704           (forward-line 1))))))
15705
15706 (defun gnus-read-newsrc-file (&optional force)
15707   "Read startup file.
15708 If FORCE is non-nil, the .newsrc file is read."
15709   ;; Reset variables that might be defined in the .newsrc.eld file.
15710   (let ((variables gnus-variable-list))
15711     (while variables
15712       (set (car variables) nil)
15713       (setq variables (cdr variables))))
15714   (let* ((newsrc-file gnus-current-startup-file)
15715          (quick-file (concat newsrc-file ".el")))
15716     (save-excursion
15717       ;; We always load the .newsrc.eld file.  If always contains
15718       ;; much information that can not be gotten from the .newsrc
15719       ;; file (ticked articles, killed groups, foreign methods, etc.)
15720       (gnus-read-newsrc-el-file quick-file)
15721
15722       (if (or force
15723               (and (file-newer-than-file-p newsrc-file quick-file)
15724                    (file-newer-than-file-p newsrc-file
15725                                            (concat quick-file "d")))
15726               (not gnus-newsrc-alist))
15727           ;; We read the .newsrc file.  Note that if there if a
15728           ;; .newsrc.eld file exists, it has already been read, and
15729           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15730           ;; the .newsrc file, Gnus will only use the information it
15731           ;; can find there for changing the data already read -
15732           ;; ie. reading the .newsrc file will not trash the data
15733           ;; already read (except for read articles).
15734           (save-excursion
15735             (gnus-message 5 "Reading %s..." newsrc-file)
15736             (set-buffer (find-file-noselect newsrc-file))
15737             (buffer-disable-undo (current-buffer))
15738             (gnus-newsrc-to-gnus-format)
15739             (kill-buffer (current-buffer))
15740             (gnus-message 5 "Reading %s...done" newsrc-file)))
15741
15742       ;; Read any slave files.
15743       (or gnus-slave
15744           (gnus-master-read-slave-newsrc)))))
15745
15746 (defun gnus-read-newsrc-el-file (file)
15747   (let ((ding-file (concat file "d")))
15748     ;; We always, always read the .eld file.
15749     (gnus-message 5 "Reading %s..." ding-file)
15750     (let (gnus-newsrc-assoc)
15751       (condition-case nil
15752           (load ding-file t t t)
15753         (error
15754          (gnus-message 1 "Error in %s" ding-file)
15755          (ding)))
15756       (when gnus-newsrc-assoc
15757         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15758     (gnus-make-hashtable-from-newsrc-alist)
15759     (when (file-newer-than-file-p file ding-file)
15760       ;; Old format quick file
15761       (gnus-message 5 "Reading %s..." file)
15762       ;; The .el file is newer than the .eld file, so we read that one
15763       ;; as well.
15764       (gnus-read-old-newsrc-el-file file))))
15765
15766 ;; Parse the old-style quick startup file
15767 (defun gnus-read-old-newsrc-el-file (file)
15768   (let (newsrc killed marked group m)
15769     (prog1
15770         (let ((gnus-killed-assoc nil)
15771               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15772           (prog1
15773               (condition-case nil
15774                   (load file t t t)
15775                 (error nil))
15776             (setq newsrc gnus-newsrc-assoc
15777                   killed gnus-killed-assoc
15778                   marked gnus-marked-assoc)))
15779       (setq gnus-newsrc-alist nil)
15780       (while newsrc
15781         (setq group (car newsrc))
15782         (let ((info (gnus-get-info (car group))))
15783           (if info
15784               (progn
15785                 (gnus-info-set-read info (cdr (cdr group)))
15786                 (gnus-info-set-level
15787                  info (if (nth 1 group) gnus-level-default-subscribed
15788                         gnus-level-default-unsubscribed))
15789                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15790             (setq gnus-newsrc-alist
15791                   (cons
15792                    (setq info
15793                          (list (car group)
15794                                (if (nth 1 group) gnus-level-default-subscribed
15795                                  gnus-level-default-unsubscribed)
15796                                (cdr (cdr group))))
15797                    gnus-newsrc-alist)))
15798           (if (setq m (assoc (car group) marked))
15799               (gnus-info-set-marks
15800                info (cons (list (cons 'tick (gnus-compress-sequence
15801                                              (sort (cdr m) '<) t)))
15802                           nil))))
15803         (setq newsrc (cdr newsrc)))
15804       (setq newsrc killed)
15805       (while newsrc
15806         (setcar newsrc (car (car newsrc)))
15807         (setq newsrc (cdr newsrc)))
15808       (setq gnus-killed-list killed))
15809     ;; The .el file version of this variable does not begin with
15810     ;; "options", while the .eld version does, so we just add it if it
15811     ;; isn't there.
15812     (and
15813      gnus-newsrc-options
15814      (progn
15815        (and (not (string-match "^ *options" gnus-newsrc-options))
15816             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15817        (and (not (string-match "\n$" gnus-newsrc-options))
15818             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15819        ;; Finally, if we read some options lines, we parse them.
15820        (or (string= gnus-newsrc-options "")
15821            (gnus-newsrc-parse-options gnus-newsrc-options))))
15822
15823     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15824     (gnus-make-hashtable-from-newsrc-alist)))
15825
15826 (defun gnus-make-newsrc-file (file)
15827   "Make server dependent file name by catenating FILE and server host name."
15828   (let* ((file (expand-file-name file nil))
15829          (real-file (concat file "-" (nth 1 gnus-select-method))))
15830     (if (or (file-exists-p real-file)
15831             (file-exists-p (concat real-file ".el"))
15832             (file-exists-p (concat real-file ".eld")))
15833         real-file file)))
15834
15835 (defun gnus-newsrc-to-gnus-format ()
15836   (setq gnus-newsrc-options "")
15837   (setq gnus-newsrc-options-n nil)
15838
15839   (or gnus-active-hashtb
15840       (setq gnus-active-hashtb (make-vector 4095 0)))
15841   (let ((buf (current-buffer))
15842         (already-read (> (length gnus-newsrc-alist) 1))
15843         group subscribed options-symbol newsrc Options-symbol
15844         symbol reads num1)
15845     (goto-char (point-min))
15846     ;; We intern the symbol `options' in the active hashtb so that we
15847     ;; can `eq' against it later.
15848     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15849     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15850
15851     (while (not (eobp))
15852       ;; We first read the first word on the line by narrowing and
15853       ;; then reading into `gnus-active-hashtb'.  Most groups will
15854       ;; already exist in that hashtb, so this will save some string
15855       ;; space.
15856       (narrow-to-region
15857        (point)
15858        (progn (skip-chars-forward "^ \t!:\n") (point)))
15859       (goto-char (point-min))
15860       (setq symbol
15861             (and (/= (point-min) (point-max))
15862                  (let ((obarray gnus-active-hashtb)) (read buf))))
15863       (widen)
15864       ;; Now, the symbol we have read is either `options' or a group
15865       ;; name.  If it is an options line, we just add it to a string.
15866       (cond
15867        ((or (eq symbol options-symbol)
15868             (eq symbol Options-symbol))
15869         (setq gnus-newsrc-options
15870               ;; This concating is quite inefficient, but since our
15871               ;; thorough studies show that approx 99.37% of all
15872               ;; .newsrc files only contain a single options line, we
15873               ;; don't give a damn, frankly, my dear.
15874               (concat gnus-newsrc-options
15875                       (buffer-substring
15876                        (gnus-point-at-bol)
15877                        ;; Options may continue on the next line.
15878                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15879                                 (progn (beginning-of-line) (point)))
15880                            (point)))))
15881         (forward-line -1))
15882        (symbol
15883         ;; Group names can be just numbers.  
15884         (when (numberp symbol) 
15885           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15886         (or (boundp symbol) (set symbol nil))
15887         ;; It was a group name.
15888         (setq subscribed (= (following-char) ?:)
15889               group (symbol-name symbol)
15890               reads nil)
15891         (if (eolp)
15892             ;; If the line ends here, this is clearly a buggy line, so
15893             ;; we put point a the beginning of line and let the cond
15894             ;; below do the error handling.
15895             (beginning-of-line)
15896           ;; We skip to the beginning of the ranges.
15897           (skip-chars-forward "!: \t"))
15898         ;; We are now at the beginning of the list of read articles.
15899         ;; We read them range by range.
15900         (while
15901             (cond
15902              ((looking-at "[0-9]+")
15903               ;; We narrow and read a number instead of buffer-substring/
15904               ;; string-to-int because it's faster.  narrow/widen is
15905               ;; faster than save-restriction/narrow, and save-restriction
15906               ;; produces a garbage object.
15907               (setq num1 (progn
15908                            (narrow-to-region (match-beginning 0) (match-end 0))
15909                            (read buf)))
15910               (widen)
15911               ;; If the next character is a dash, then this is a range.
15912               (if (= (following-char) ?-)
15913                   (progn
15914                     ;; We read the upper bound of the range.
15915                     (forward-char 1)
15916                     (if (not (looking-at "[0-9]+"))
15917                         ;; This is a buggy line, by we pretend that
15918                         ;; it's kinda OK.  Perhaps the user should be
15919                         ;; dinged?
15920                         (setq reads (cons num1 reads))
15921                       (setq reads
15922                             (cons
15923                              (cons num1
15924                                    (progn
15925                                      (narrow-to-region (match-beginning 0)
15926                                                        (match-end 0))
15927                                      (read buf)))
15928                              reads))
15929                       (widen)))
15930                 ;; It was just a simple number, so we add it to the
15931                 ;; list of ranges.
15932                 (setq reads (cons num1 reads)))
15933               ;; If the next char in ?\n, then we have reached the end
15934               ;; of the line and return nil.
15935               (/= (following-char) ?\n))
15936              ((= (following-char) ?\n)
15937               ;; End of line, so we end.
15938               nil)
15939              (t
15940               ;; Not numbers and not eol, so this might be a buggy
15941               ;; line...
15942               (or (eobp)
15943                   ;; If it was eob instead of ?\n, we allow it.
15944                   (progn
15945                     ;; The line was buggy.
15946                     (setq group nil)
15947                     (gnus-message 3 "Mangled line: %s"
15948                                   (buffer-substring (gnus-point-at-bol)
15949                                                     (gnus-point-at-eol)))
15950                     (ding)
15951                     (sit-for 1)))
15952               nil))
15953           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15954           ;; we allow them, because it's a common mistake to put a
15955           ;; space after the comma.
15956           (skip-chars-forward ", "))
15957
15958         ;; We have already read .newsrc.eld, so we gently update the
15959         ;; data in the hash table with the information we have just
15960         ;; read.
15961         (when group
15962           (let ((info (gnus-get-info group))
15963                 level)
15964             (if info
15965                 ;; There is an entry for this file in the alist.
15966                 (progn
15967                   (gnus-info-set-read info (nreverse reads))
15968                   ;; We update the level very gently.  In fact, we
15969                   ;; only change it if there's been a status change
15970                   ;; from subscribed to unsubscribed, or vice versa.
15971                   (setq level (gnus-info-level info))
15972                   (cond ((and (<= level gnus-level-subscribed)
15973                               (not subscribed))
15974                          (setq level (if reads
15975                                          gnus-level-default-unsubscribed
15976                                        (1+ gnus-level-default-unsubscribed))))
15977                         ((and (> level gnus-level-subscribed) subscribed)
15978                          (setq level gnus-level-default-subscribed)))
15979                   (gnus-info-set-level info level))
15980               ;; This is a new group.
15981               (setq info (list group
15982                                (if subscribed
15983                                    gnus-level-default-subscribed
15984                                  (if reads
15985                                      (1+ gnus-level-subscribed)
15986                                    gnus-level-default-unsubscribed))
15987                                (nreverse reads))))
15988             (setq newsrc (cons info newsrc))))))
15989       (forward-line 1))
15990
15991     (setq newsrc (nreverse newsrc))
15992
15993     (if (not already-read)
15994         ()
15995       ;; We now have two newsrc lists - `newsrc', which is what we
15996       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15997       ;; what we've read from .newsrc.eld.  We have to merge these
15998       ;; lists.  We do this by "attaching" any (foreign) groups in the
15999       ;; gnus-newsrc-alist to the (native) group that precedes them.
16000       (let ((rc (cdr gnus-newsrc-alist))
16001             (prev gnus-newsrc-alist)
16002             entry mentry)
16003         (while rc
16004           (or (null (nth 4 (car rc)))   ; It's a native group.
16005               (assoc (car (car rc)) newsrc) ; It's already in the alist.
16006               (if (setq entry (assoc (car (car prev)) newsrc))
16007                   (setcdr (setq mentry (memq entry newsrc))
16008                           (cons (car rc) (cdr mentry)))
16009                 (setq newsrc (cons (car rc) newsrc))))
16010           (setq prev rc
16011                 rc (cdr rc)))))
16012
16013     (setq gnus-newsrc-alist newsrc)
16014     ;; We make the newsrc hashtb.
16015     (gnus-make-hashtable-from-newsrc-alist)
16016
16017     ;; Finally, if we read some options lines, we parse them.
16018     (or (string= gnus-newsrc-options "")
16019         (gnus-newsrc-parse-options gnus-newsrc-options))))
16020
16021 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16022 ;; The return value will be a list on the form
16023 ;; ((regexp1 . ignore)
16024 ;;  (regexp2 . subscribe)...)
16025 ;; When handling new newsgroups, groups that match a `ignore' regexp
16026 ;; will be ignored, and groups that match a `subscribe' regexp will be
16027 ;; subscribed.  A line like
16028 ;; options -n !all rec.all
16029 ;; will lead to a list that looks like
16030 ;; (("^rec\\..+" . subscribe)
16031 ;;  ("^.+" . ignore))
16032 ;; So all "rec.*" groups will be subscribed, while all the other
16033 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16034 ;; different from "options -n rec.all !all".
16035 (defun gnus-newsrc-parse-options (options)
16036   (let (out eol)
16037     (save-excursion
16038       (gnus-set-work-buffer)
16039       (insert (regexp-quote options))
16040       ;; First we treat all continuation lines.
16041       (goto-char (point-min))
16042       (while (re-search-forward "\n[ \t]+" nil t)
16043         (replace-match " " t t))
16044       ;; Then we transform all "all"s into ".+"s.
16045       (goto-char (point-min))
16046       (while (re-search-forward "\\ball\\b" nil t)
16047         (replace-match ".+" t t))
16048       (goto-char (point-min))
16049       ;; We remove all other options than the "-n" ones.
16050       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16051         (replace-match " ")
16052         (forward-char -1))
16053       (goto-char (point-min))
16054
16055       ;; We are only interested in "options -n" lines - we
16056       ;; ignore the other option lines.
16057       (while (re-search-forward "[ \t]-n" nil t)
16058         (setq eol
16059               (or (save-excursion
16060                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16061                          (- (point) 2)))
16062                   (gnus-point-at-eol)))
16063         ;; Search for all "words"...
16064         (while (re-search-forward "[^ \t,\n]+" eol t)
16065           (if (= (char-after (match-beginning 0)) ?!)
16066               ;; If the word begins with a bang (!), this is a "not"
16067               ;; spec.  We put this spec (minus the bang) and the
16068               ;; symbol `ignore' into the list.
16069               (setq out (cons (cons (concat
16070                                      "^" (buffer-substring
16071                                           (1+ (match-beginning 0))
16072                                           (match-end 0)))
16073                                     'ignore) out))
16074             ;; There was no bang, so this is a "yes" spec.
16075             (setq out (cons (cons (concat "^" (match-string 0))
16076                                   'subscribe) out)))))
16077
16078       (setq gnus-newsrc-options-n out))))
16079
16080 (defun gnus-save-newsrc-file (&optional force)
16081   "Save .newsrc file."
16082   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16083   ;; from the variable gnus-newsrc-alist.
16084   (when (and (or gnus-newsrc-alist gnus-killed-list)
16085              gnus-current-startup-file)
16086     (save-excursion
16087       (if (and (or gnus-use-dribble-file gnus-slave)
16088                (not force)
16089                (or (not gnus-dribble-buffer)
16090                    (not (buffer-name gnus-dribble-buffer))
16091                    (zerop (save-excursion
16092                             (set-buffer gnus-dribble-buffer)
16093                             (buffer-size)))))
16094           (gnus-message 4 "(No changes need to be saved)")
16095         (run-hooks 'gnus-save-newsrc-hook)
16096         (if gnus-slave
16097             (gnus-slave-save-newsrc)
16098           ;; Save .newsrc.
16099           (when gnus-save-newsrc-file
16100             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16101             (gnus-gnus-to-newsrc-format)
16102             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16103           ;; Save .newsrc.eld.
16104           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16105           (make-local-variable 'version-control)
16106           (setq version-control 'never)
16107           (setq buffer-file-name
16108                 (concat gnus-current-startup-file ".eld"))
16109           (gnus-add-current-to-buffer-list)
16110           (buffer-disable-undo (current-buffer))
16111           (erase-buffer)
16112           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16113           (gnus-gnus-to-quick-newsrc-format)
16114           (run-hooks 'gnus-save-quick-newsrc-hook)
16115           (save-buffer)
16116           (kill-buffer (current-buffer))
16117           (gnus-message
16118            5 "Saving %s.eld...done" gnus-current-startup-file))
16119         (gnus-dribble-delete-file)))))
16120
16121 (defun gnus-gnus-to-quick-newsrc-format ()
16122   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16123   (insert ";; Gnus startup file.\n")
16124   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16125   (insert ";; to read .newsrc.\n")
16126   (insert "(setq gnus-newsrc-file-version "
16127           (prin1-to-string gnus-version) ")\n")
16128   (let ((variables
16129          (if gnus-save-killed-list gnus-variable-list
16130            ;; Remove the `gnus-killed-list' from the list of variables
16131            ;; to be saved, if required.
16132            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16133         ;; Peel off the "dummy" group.
16134         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16135         variable)
16136     ;; Insert the variables into the file.
16137     (while variables
16138       (when (and (boundp (setq variable (pop variables)))
16139                  (symbol-value variable))
16140         (insert "(setq " (symbol-name variable) " '"
16141                 (prin1-to-string (symbol-value variable)) ")\n")))))
16142
16143 (defun gnus-gnus-to-newsrc-format ()
16144   ;; Generate and save the .newsrc file.
16145   (let ((newsrc (cdr gnus-newsrc-alist))
16146         info ranges range)
16147     (save-excursion
16148       (set-buffer (create-file-buffer gnus-current-startup-file))
16149       (setq buffer-file-name gnus-current-startup-file)
16150       (buffer-disable-undo (current-buffer))
16151       (erase-buffer)
16152       ;; Write options.
16153       (if gnus-newsrc-options (insert gnus-newsrc-options))
16154       ;; Write subscribed and unsubscribed.
16155       (while newsrc
16156         (setq info (car newsrc))
16157         (if (not (gnus-info-method info))
16158             ;; Don't write foreign groups to .newsrc.
16159             (progn
16160               (insert (gnus-info-group info)
16161                       (if (> (gnus-info-level info) gnus-level-subscribed)
16162                           "!" ":"))
16163               (if (setq ranges (gnus-info-read info))
16164                   (progn
16165                     (insert " ")
16166                     (if (not (listp (cdr ranges)))
16167                         (if (= (car ranges) (cdr ranges))
16168                             (insert (int-to-string (car ranges)))
16169                           (insert (int-to-string (car ranges)) "-"
16170                                   (int-to-string (cdr ranges))))
16171                       (while ranges
16172                         (setq range (car ranges)
16173                               ranges (cdr ranges))
16174                         (if (or (atom range) (= (car range) (cdr range)))
16175                             (insert (int-to-string
16176                                      (or (and (atom range) range)
16177                                          (car range))))
16178                           (insert (int-to-string (car range)) "-"
16179                                   (int-to-string (cdr range))))
16180                         (if ranges (insert ","))))))
16181               (insert "\n")))
16182         (setq newsrc (cdr newsrc)))
16183       (make-local-variable 'version-control)
16184       (setq version-control 'never)
16185       ;; It has been reported that sometime the modtime on the .newsrc
16186       ;; file seems to be off.  We really do want to overwrite it, so
16187       ;; we clear the modtime here before saving.  It's a bit odd,
16188       ;; though...
16189       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16190       ;; delete the silly thing entirely first.  but this fails to provide
16191       ;; such niceties as .newsrc~ creation.
16192       (if gnus-modtime-botch
16193           (delete-file gnus-startup-file)
16194         (clear-visited-file-modtime))
16195       (run-hooks 'gnus-save-standard-newsrc-hook)
16196       (save-buffer)
16197       (kill-buffer (current-buffer)))))
16198
16199 \f
16200 ;;;
16201 ;;; Slave functions.
16202 ;;;
16203
16204 (defun gnus-slave-save-newsrc ()
16205   (save-excursion
16206     (set-buffer gnus-dribble-buffer)
16207     (let ((slave-name
16208            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16209       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16210
16211 (defun gnus-master-read-slave-newsrc ()
16212   (let ((slave-files
16213          (directory-files
16214           (file-name-directory gnus-current-startup-file)
16215           t (concat
16216              "^" (regexp-quote
16217                   (concat
16218                    (file-name-nondirectory gnus-current-startup-file)
16219                    "-slave-")))
16220           t))
16221         file)
16222     (if (not slave-files)
16223         ()                              ; There are no slave files to read.
16224       (gnus-message 7 "Reading slave newsrcs...")
16225       (save-excursion
16226         (set-buffer (get-buffer-create " *gnus slave*"))
16227         (buffer-disable-undo (current-buffer))
16228         (setq slave-files
16229               (sort (mapcar (lambda (file)
16230                               (list (nth 5 (file-attributes file)) file))
16231                             slave-files)
16232                     (lambda (f1 f2)
16233                       (or (< (car (car f1)) (car (car f2)))
16234                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16235         (while slave-files
16236           (erase-buffer)
16237           (setq file (nth 1 (car slave-files)))
16238           (insert-file-contents file)
16239           (if (condition-case ()
16240                   (progn
16241                     (eval-buffer (current-buffer))
16242                     t)
16243                 (error
16244                  (gnus-message 3 "Possible error in %s" file)
16245                  (ding)
16246                  (sit-for 2)
16247                  nil))
16248               (or gnus-slave ; Slaves shouldn't delete these files.
16249                   (condition-case ()
16250                       (delete-file file)
16251                     (error nil))))
16252           (setq slave-files (cdr slave-files))))
16253       (gnus-message 7 "Reading slave newsrcs...done"))))
16254
16255 \f
16256 ;;;
16257 ;;; Group description.
16258 ;;;
16259
16260 (defun gnus-read-all-descriptions-files ()
16261   (let ((methods (cons gnus-select-method 
16262                        (nconc
16263                         (when gnus-message-archive-method
16264                           (list "archive"))
16265                         gnus-secondary-select-methods))))
16266     (while methods
16267       (gnus-read-descriptions-file (car methods))
16268       (setq methods (cdr methods)))
16269     t))
16270
16271 (defun gnus-read-descriptions-file (&optional method)
16272   (let ((method (or method gnus-select-method)))
16273     (when (stringp method)
16274       (setq method (gnus-server-to-method method)))
16275     ;; We create the hashtable whether we manage to read the desc file
16276     ;; to avoid trying to re-read after a failed read.
16277     (or gnus-description-hashtb
16278         (setq gnus-description-hashtb
16279               (gnus-make-hashtable (length gnus-active-hashtb))))
16280     ;; Mark this method's desc file as read.
16281     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16282                   gnus-description-hashtb)
16283
16284     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16285     (cond
16286      ((not (gnus-check-server method))
16287       (gnus-message 1 "Couldn't open server")
16288       nil)
16289      ((not (gnus-request-list-newsgroups method))
16290       (gnus-message 1 "Couldn't read newsgroups descriptions")
16291       nil)
16292      (t
16293       (let (group)
16294         (save-excursion
16295           (save-restriction
16296             (set-buffer nntp-server-buffer)
16297             (goto-char (point-min))
16298             (if (or (search-forward "\n.\n" nil t)
16299                     (goto-char (point-max)))
16300                 (progn
16301                   (beginning-of-line)
16302                   (narrow-to-region (point-min) (point))))
16303             (goto-char (point-min))
16304             (while (not (eobp))
16305               ;; If we get an error, we set group to 0, which is not a
16306               ;; symbol...
16307               (setq group
16308                     (condition-case ()
16309                         (let ((obarray gnus-description-hashtb))
16310                           ;; Group is set to a symbol interned in this
16311                           ;; hash table.
16312                           (read nntp-server-buffer))
16313                       (error 0)))
16314               (skip-chars-forward " \t")
16315               ;; ...  which leads to this line being effectively ignored.
16316               (and (symbolp group)
16317                    (set group (buffer-substring
16318                                (point) (progn (end-of-line) (point)))))
16319               (forward-line 1))))
16320         (gnus-message 5 "Reading descriptions file...done")
16321         t)))))
16322
16323 (defun gnus-group-get-description (group)
16324   "Get the description of a group by sending XGTITLE to the server."
16325   (when (gnus-request-group-description group)
16326     (save-excursion
16327       (set-buffer nntp-server-buffer)
16328       (goto-char (point-min))
16329       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16330         (match-string 1)))))
16331
16332 \f
16333 ;;;
16334 ;;; Buffering of read articles.
16335 ;;;
16336
16337 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16338 (defvar gnus-backlog-articles nil)
16339 (defvar gnus-backlog-hashtb nil)
16340
16341 (defun gnus-backlog-buffer ()
16342   "Return the backlog buffer."
16343   (or (get-buffer gnus-backlog-buffer)
16344       (save-excursion
16345         (set-buffer (get-buffer-create gnus-backlog-buffer))
16346         (buffer-disable-undo (current-buffer))
16347         (setq buffer-read-only t)
16348         (gnus-add-current-to-buffer-list)
16349         (get-buffer gnus-backlog-buffer))))
16350
16351 (defun gnus-backlog-setup ()
16352   "Initialize backlog variables."
16353   (unless gnus-backlog-hashtb
16354     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16355
16356 (defun gnus-backlog-shutdown ()
16357   "Clear all backlog variables and buffers."
16358   (when (get-buffer gnus-backlog-buffer)
16359     (kill-buffer gnus-backlog-buffer))
16360   (setq gnus-backlog-hashtb nil
16361         gnus-backlog-articles nil))
16362
16363 (defun gnus-backlog-enter-article (group number buffer)
16364   (gnus-backlog-setup)
16365   (let ((ident (intern (concat group ":" (int-to-string number))
16366                        gnus-backlog-hashtb))
16367         b)
16368     (if (memq ident gnus-backlog-articles)
16369         () ; It's already kept.
16370       ;; Remove the oldest article, if necessary.
16371       (and (numberp gnus-keep-backlog)
16372            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16373            (gnus-backlog-remove-oldest-article))
16374       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16375       ;; Insert the new article.
16376       (save-excursion
16377         (set-buffer (gnus-backlog-buffer))
16378         (let (buffer-read-only)
16379           (goto-char (point-max))
16380           (or (bolp) (insert "\n"))
16381           (setq b (point))
16382           (insert-buffer-substring buffer)
16383           ;; Tag the beginning of the article with the ident.
16384           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16385
16386 (defun gnus-backlog-remove-oldest-article ()
16387   (save-excursion
16388     (set-buffer (gnus-backlog-buffer))
16389     (goto-char (point-min))
16390     (if (zerop (buffer-size))
16391         () ; The buffer is empty.
16392       (let ((ident (get-text-property (point) 'gnus-backlog))
16393             buffer-read-only)
16394         ;; Remove the ident from the list of articles.
16395         (when ident
16396           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16397         ;; Delete the article itself.
16398         (delete-region
16399          (point) (next-single-property-change
16400                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16401
16402 (defun gnus-backlog-request-article (group number buffer)
16403   (gnus-backlog-setup)
16404   (let ((ident (intern (concat group ":" (int-to-string number))
16405                        gnus-backlog-hashtb))
16406         beg end)
16407     (when (memq ident gnus-backlog-articles)
16408       ;; It was in the backlog.
16409       (save-excursion
16410         (set-buffer (gnus-backlog-buffer))
16411         (if (not (setq beg (text-property-any
16412                             (point-min) (point-max) 'gnus-backlog
16413                             ident)))
16414             ;; It wasn't in the backlog after all.
16415             (ignore
16416              (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16417           ;; Find the end (i. e., the beginning of the next article).
16418           (setq end
16419                 (next-single-property-change
16420                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16421       (let ((buffer-read-only nil))
16422         (erase-buffer)
16423         (insert-buffer-substring gnus-backlog-buffer beg end)
16424         t))))
16425
16426 ;; Allow redefinition of Gnus functions.
16427
16428 (gnus-ems-redefine)
16429
16430 (provide 'gnus)
16431
16432 ;;; gnus.el ends here