*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34
35 (eval-when-compile (require 'cl))
36
37 ;; Site dependent variables.  These variables should be defined in
38 ;; paths.el.
39
40 (defvar gnus-default-nntp-server nil
41   "Specify a default NNTP server.
42 This variable should be defined in paths.el, and should never be set
43 by the user.
44 If you want to change servers, you should use `gnus-select-method'.
45 See the documentation to that variable.")
46
47 (defvar gnus-backup-default-subscribed-newsgroups
48   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
49   "Default default new newsgroups the first time Gnus is run.
50 Should be set in paths.el, and shouldn't be touched by the user.")
51
52 (defvar gnus-local-domain nil
53   "Local domain name without a host name.
54 The DOMAINNAME environment variable is used instead if it is defined.
55 If the `system-name' function returns the full Internet name, there is
56 no need to set this variable.")
57
58 (defvar gnus-local-organization nil
59   "String with a description of what organization (if any) the user belongs to.
60 The ORGANIZATION environment variable is used instead if it is defined.
61 If this variable contains a function, this function will be called
62 with the current newsgroup name as the argument.  The function should
63 return a string.
64
65 In any case, if the string (either in the variable, in the environment
66 variable, or returned by the function) is a file name, the contents of
67 this file will be used as the organization.")
68
69 (defvar gnus-use-generic-from nil
70   "If nil, the full host name will be the system name prepended to the domain name.
71 If this is a string, the full host name will be this string.
72 If this is non-nil, non-string, the domain name will be used as the
73 full host name.")
74
75 (defvar gnus-use-generic-path nil
76   "If nil, use the NNTP server name in the Path header.
77 If stringp, use this; if non-nil, use no host name (user name only).")
78
79 ;; Customization variables
80
81 ;; Don't touch this variable.
82 (defvar gnus-nntp-service "nntp"
83   "*NNTP service name (\"nntp\" or 119).
84 This is an obsolete variable, which is scarcely used.  If you use an
85 nntp server for your newsgroup and want to change the port number
86 used to 899, you would say something along these lines:
87
88  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
89
90 (defvar gnus-nntpserver-file "/etc/nntpserver"
91   "*A file with only the name of the nntp server in it.")
92
93 ;; This function is used to check both the environment variable
94 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
95 ;; an nntp server name default.
96 (defun gnus-getenv-nntpserver ()
97   (or (getenv "NNTPSERVER")
98       (and (file-readable-p gnus-nntpserver-file)
99            (save-excursion
100              (set-buffer (get-buffer-create " *gnus nntp*"))
101              (buffer-disable-undo (current-buffer))
102              (insert-file-contents gnus-nntpserver-file)
103              (let ((name (buffer-string)))
104                (prog1
105                    (if (string-match "^[ \t\n]*$" name)
106                        nil
107                      name)
108                  (kill-buffer (current-buffer))))))))
109
110 (defvar gnus-select-method
111   (nconc
112    (list 'nntp (or (condition-case ()
113                        (gnus-getenv-nntpserver)
114                      (error nil))
115                    (if (and gnus-default-nntp-server
116                             (not (string= gnus-default-nntp-server "")))
117                        gnus-default-nntp-server)
118                    (system-name)))
119    (if (or (null gnus-nntp-service)
120            (equal gnus-nntp-service "nntp"))
121        nil
122      (list gnus-nntp-service)))
123   "*Default method for selecting a newsgroup.
124 This variable should be a list, where the first element is how the
125 news is to be fetched, the second is the address.
126
127 For instance, if you want to get your news via NNTP from
128 \"flab.flab.edu\", you could say:
129
130 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
131
132 If you want to use your local spool, say:
133
134 (setq gnus-select-method (list 'nnspool (system-name)))
135
136 If you use this variable, you must set `gnus-nntp-server' to nil.
137
138 There is a lot more to know about select methods and virtual servers -
139 see the manual for details.")
140
141 (defvar gnus-message-archive-method 
142   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
143              (nnfolder-active-file "~/Mail/archive/active")
144              (nnfolder-get-new-mail nil)
145              (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.")
148
149 (defvar gnus-refer-article-method nil
150   "*Preferred method for fetching an article by Message-ID.
151 If you are reading news from the local spool (with nnspool), fetching
152 articles by Message-ID is painfully slow.  By setting this method to an
153 nntp method, you might get acceptable results.
154
155 The value of this variable must be a valid select method as discussed
156 in the documentation of `gnus-select-method'.")
157
158 (defvar gnus-secondary-select-methods nil
159   "*A list of secondary methods that will be used for reading news.
160 This is a list where each element is a complete select method (see
161 `gnus-select-method').
162
163 If, for instance, you want to read your mail with the nnml backend,
164 you could set this variable:
165
166 (setq gnus-secondary-select-methods '((nnml \"\")))")
167
168 (defvar gnus-secondary-servers nil
169   "*List of NNTP servers that the user can choose between interactively.
170 To make Gnus query you for a server, you have to give `gnus' a
171 non-numeric prefix - `C-u M-x gnus', in short.")
172
173 (defvar gnus-nntp-server nil
174   "*The name of the host running the NNTP server.
175 This variable is semi-obsolete.  Use the `gnus-select-method'
176 variable instead.")
177
178 (defvar gnus-startup-file "~/.newsrc"
179   "*Your `.newsrc' file.
180 `.newsrc-SERVER' will be used instead if that exists.")
181
182 (defvar gnus-init-file "~/.gnus"
183   "*Your Gnus elisp startup file.
184 If a file with the .el or .elc suffixes exist, it will be read
185 instead.")
186
187 (defvar gnus-group-faq-directory
188   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
189     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
190     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
191     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
192     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
193     "/ftp@ftp.sunet.se:/pub/usenet/"
194     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
195     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
196     "/ftp@ftp.hk.super.net:/mirror/faqs/")
197   "*Directory where the group FAQs are stored.
198 This will most commonly be on a remote machine, and the file will be
199 fetched by ange-ftp.
200
201 This variable can also be a list of directories.  In that case, the
202 first element in the list will be used by default, and the others will
203 be used as backup sites.
204
205 Note that Gnus uses an aol machine as the default directory.  If this
206 feels fundamentally unclean, just think of it as a way to finally get
207 something of value back from them.
208
209 If the default site is too slow, try one of these:
210
211    North America: mirrors.aol.com                /pub/rtfm/usenet
212                   ftp.seas.gwu.edu               /pub/rtfm
213                   rtfm.mit.edu                   /pub/usenet/news.answers
214    Europe:        ftp.uni-paderborn.de           /pub/FAQ
215                   src.doc.ic.ac.uk               /usenet/news-FAQS
216                   ftp.sunet.se                   /pub/usenet
217    Asia:          nctuccca.edu.tw                /USENET/FAQ
218                   hwarang.postech.ac.kr          /pub/usenet/news.answers
219                   ftp.hk.super.net               /mirror/faqs")
220
221 (defvar gnus-group-archive-directory
222   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
223   "*The address of the (ding) archives.")
224
225 (defvar gnus-group-recent-archive-directory
226   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
227   "*The address of the most recent (ding) articles.")
228
229 (defvar gnus-default-subscribed-newsgroups nil
230   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
231 It should be a list of strings.
232 If it is `t', Gnus will not do anything special the first time it is
233 started; it'll just use the normal newsgroups subscription methods.")
234
235 (defvar gnus-use-cross-reference t
236   "*Non-nil means that cross referenced articles will be marked as read.
237 If nil, ignore cross references.  If t, mark articles as read in
238 subscribed newsgroups.  If neither t nor nil, mark as read in all
239 newsgroups.")
240
241 (defvar gnus-single-article-buffer t
242   "*If non-nil, display all articles in the same buffer.
243 If nil, each group will get its own article buffer.")
244
245 (defvar gnus-use-dribble-file t
246   "*Non-nil means that Gnus will use a dribble file to store user updates.
247 If Emacs should crash without saving the .newsrc files, complete
248 information can be restored from the dribble file.")
249
250 (defvar gnus-dribble-directory nil
251   "*The directory where dribble files will be saved.
252 If this variable is nil, the directory where the .newsrc files are
253 saved will be used.")
254
255 (defvar gnus-asynchronous nil
256   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
257
258 (defvar gnus-kill-summary-on-exit t
259   "*If non-nil, kill the summary buffer when you exit from it.
260 If nil, the summary will become a \"*Dead Summary*\" buffer, and
261 it will be killed sometime later.")
262
263 (defvar gnus-large-newsgroup 200
264   "*The number of articles which indicates a large newsgroup.
265 If the number of articles in a newsgroup is greater than this value,
266 confirmation is required for selecting the newsgroup.")
267
268 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
269 (defvar gnus-no-groups-message "No news is horrible news"
270   "*Message displayed by Gnus when no groups are available.")
271
272 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
273   "*Non-nil means that the default name of a file to save articles in is the group name.
274 If it's nil, the directory form of the group name is used instead.
275
276 If this variable is a list, and the list contains the element
277 `not-score', long file names will not be used for score files; if it
278 contains the element `not-save', long file names will not be used for
279 saving; and if it contains the element `not-kill', long file names
280 will not be used for kill files.")
281
282 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
283   "*Name of the directory articles will be saved in (default \"~/News\").
284 Initialized from the SAVEDIR environment variable.")
285
286 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
287   "*Name of the directory where kill files will be stored (default \"~/News\").
288 Initialized from the SAVEDIR environment variable.")
289
290 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
291   "*A function to save articles in your favorite format.
292 The function must be interactively callable (in other words, it must
293 be an Emacs command).
294
295 Gnus provides the following functions:
296
297 * gnus-summary-save-in-rmail (Rmail format)
298 * gnus-summary-save-in-mail (Unix mail format)
299 * gnus-summary-save-in-folder (MH folder)
300 * gnus-summary-save-in-file (article format).
301 * gnus-summary-save-in-vm (use VM's folder format).")
302
303 (defvar gnus-prompt-before-saving 'always
304   "*This variable says how much prompting is to be done when saving articles.
305 If it is nil, no prompting will be done, and the articles will be
306 saved to the default files.  If this variable is `always', each and
307 every article that is saved will be preceded by a prompt, even when
308 saving large batches of articles.  If this variable is neither nil not
309 `always', there the user will be prompted once for a file name for
310 each invocation of the saving commands.")
311
312 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
313   "*A function generating a file name to save articles in Rmail format.
314 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
315
316 (defvar gnus-mail-save-name (function gnus-plain-save-name)
317   "*A function generating a file name to save articles in Unix mail format.
318 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
319
320 (defvar gnus-folder-save-name (function gnus-folder-save-name)
321   "*A function generating a file name to save articles in MH folder.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
323
324 (defvar gnus-file-save-name (function gnus-numeric-save-name)
325   "*A function generating a file name to save articles in article format.
326 The function is called with NEWSGROUP, HEADERS, and optional
327 LAST-FILE.")
328
329 (defvar gnus-split-methods
330   '((gnus-article-archive-name))
331   "*Variable used to suggest where articles are to be saved.
332 For instance, if you would like to save articles related to Gnus in
333 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
334 you could set this variable to something like:
335
336  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
337    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
338
339 This variable is an alist where the where the key is the match and the
340 value is a list of possible files to save in if the match is non-nil.
341
342 If the match is a string, it is used as a regexp match on the
343 article.  If the match is a symbol, that symbol will be funcalled
344 from the buffer of the article to be saved with the newsgroup as the
345 parameter.  If it is a list, it will be evaled in the same buffer.
346
347 If this form or function returns a string, this string will be used as
348 a possible file name; and if it returns a non-nil list, that list will
349 be used as possible file names.")
350
351 (defvar gnus-move-split-methods nil
352   "*Variable used to suggest where articles are to be moved to.
353 It uses the same syntax as the `gnus-split-methods' variable.")
354
355 (defvar gnus-save-score nil
356   "*If non-nil, save group scoring info.")
357
358 (defvar gnus-use-adaptive-scoring nil
359   "*If non-nil, use some adaptive scoring scheme.")
360
361 (defvar gnus-use-cache nil
362   "*If nil, Gnus will ignore the article cache.
363 If `passive', it will allow entering (and reading) articles
364 explicitly entered into the cache.  If anything else, use the
365 cache to the full extent of the law.")
366
367 (defvar gnus-use-trees nil
368   "*If non-nil, display a thread tree buffer.")
369
370 (defvar gnus-use-grouplens nil
371   "*If non-nil, use GroupLens ratings.")
372
373 (defvar gnus-keep-backlog nil
374   "*If non-nil, Gnus will keep read articles for later re-retrieval.
375 If it is a number N, then Gnus will only keep the last N articles
376 read.  If it is neither nil nor a number, Gnus will keep all read
377 articles.  This is not a good idea.")
378
379 (defvar gnus-use-nocem nil
380   "*If non-nil, Gnus will read NoCeM cancel messages.")
381
382 (defvar gnus-use-demon nil
383   "If non-nil, Gnus might use some demons.")
384
385 (defvar gnus-use-scoring t
386   "*If non-nil, enable scoring.")
387
388 (defvar gnus-use-picons nil
389   "*If non-nil, display picons.")
390
391 (defvar gnus-fetch-old-headers nil
392   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
393 If an unread article in the group refers to an older, already read (or
394 just marked as read) article, the old article will not normally be
395 displayed in the Summary buffer.  If this variable is non-nil, Gnus
396 will attempt to grab the headers to the old articles, and thereby
397 build complete threads.  If it has the value `some', only enough
398 headers to connect otherwise loose threads will be displayed.
399 This variable can also be a number.  In that case, no more than that
400 number of old headers will be fetched.
401
402 The server has to support NOV for any of this to work.")
403
404 ;see gnus-cus.el
405 ;(defvar gnus-visual t
406 ;  "*If non-nil, will do various highlighting.
407 ;If nil, no mouse highlights (or any other highlights) will be
408 ;performed.  This might speed up Gnus some when generating large group
409 ;and summary buffers.")
410
411 (defvar gnus-novice-user t
412   "*Non-nil means that you are a usenet novice.
413 If non-nil, verbose messages may be displayed and confirmations may be
414 required.")
415
416 (defvar gnus-expert-user nil
417   "*Non-nil means that you will never be asked for confirmation about anything.
418 And that means *anything*.")
419
420 (defvar gnus-verbose 7
421   "*Integer that says how verbose Gnus should be.
422 The higher the number, the more messages Gnus will flash to say what
423 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
424 display most important messages; and at ten, Gnus will keep on
425 jabbering all the time.")
426
427 (defvar gnus-keep-same-level nil
428   "*Non-nil means that the next newsgroup after the current will be on the same level.
429 When you type, for instance, `n' after reading the last article in the
430 current newsgroup, you will go to the next newsgroup.  If this variable
431 is nil, the next newsgroup will be the next from the group
432 buffer.
433 If this variable is non-nil, Gnus will either put you in the
434 next newsgroup with the same level, or, if no such newsgroup is
435 available, the next newsgroup with the lowest possible level higher
436 than the current level.
437 If this variable is `best', Gnus will make the next newsgroup the one
438 with the best level.")
439
440 (defvar gnus-summary-make-false-root 'adopt
441   "*nil means that Gnus won't gather loose threads.
442 If the root of a thread has expired or been read in a previous
443 session, the information necessary to build a complete thread has been
444 lost.  Instead of having many small sub-threads from this original thread
445 scattered all over the summary buffer, Gnus can gather them.
446
447 If non-nil, Gnus will try to gather all loose sub-threads from an
448 original thread into one large thread.
449
450 If this variable is non-nil, it should be one of `none', `adopt',
451 `dummy' or `empty'.
452
453 If this variable is `none', Gnus will not make a false root, but just
454 present the sub-threads after another.
455 If this variable is `dummy', Gnus will create a dummy root that will
456 have all the sub-threads as children.
457 If this variable is `adopt', Gnus will make one of the \"children\"
458 the parent and mark all the step-children as such.
459 If this variable is `empty', the \"children\" are printed with empty
460 subject fields.  (Or rather, they will be printed with a string
461 given by the `gnus-summary-same-subject' variable.)")
462
463 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
464   "*A regexp to match subjects to be excluded from loose thread gathering.
465 As loose thread gathering is done on subjects only, that means that
466 there can be many false gatherings performed.  By rooting out certain
467 common subjects, gathering might become saner.")
468
469 (defvar gnus-summary-gather-subject-limit nil
470   "*Maximum length of subject comparisons when gathering loose threads.
471 Use nil to compare full subjects.  Setting this variable to a low
472 number will help gather threads that have been corrupted by
473 newsreaders chopping off subject lines, but it might also mean that
474 unrelated articles that have subject that happen to begin with the
475 same few characters will be incorrectly gathered.
476
477 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
478 comparing subjects.")
479
480 (defvar gnus-simplify-ignored-prefixes nil
481   "*Regexp, matches for which are removed from subject lines when simplifying.")
482
483 (defvar gnus-build-sparse-threads nil
484   "*If non-nil, fill in the gaps in threads.
485 If `some', only fill in the gaps that are needed to tie loose threads
486 together.  If `more', fill in all leaf nodes that Gnus can find.  If
487 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
488
489 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
490   "Function used for gathering loose threads.
491 There are two pre-defined functions: `gnus-gather-threads-by-subject',
492 which only takes Subjects into consideration; and
493 `gnus-gather-threads-by-references', which compared the References
494 headers of the articles to find matches.")
495
496 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
497 (defvar gnus-summary-same-subject ""
498   "*String indicating that the current article has the same subject as the previous.
499 This variable will only be used if the value of
500 `gnus-summary-make-false-root' is `empty'.")
501
502 (defvar gnus-summary-goto-unread t
503   "*If non-nil, marking commands will go to the next unread article.
504 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
505 whether it is read or not.")
506
507 (defvar gnus-group-goto-unread t
508   "*If non-nil, movement commands will go to the next unread and subscribed group.")
509
510 (defvar gnus-goto-next-group-when-activating t
511   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
512
513 (defvar gnus-check-new-newsgroups t
514   "*Non-nil means that Gnus will add new newsgroups at startup.
515 If this variable is `ask-server', Gnus will ask the server for new
516 groups since the last time it checked.  This means that the killed list
517 is no longer necessary, so you could set `gnus-save-killed-list' to
518 nil.
519
520 A variant is to have this variable be a list of select methods.  Gnus
521 will then use the `ask-server' method on all these select methods to
522 query for new groups from all those servers.
523
524 Eg.
525   (setq gnus-check-new-newsgroups
526         '((nntp \"some.server\") (nntp \"other.server\")))
527
528 If this variable is nil, then you have to tell Gnus explicitly to
529 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
530
531 (defvar gnus-check-bogus-newsgroups nil
532   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
533 If this variable is nil, then you have to tell Gnus explicitly to
534 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
535
536 (defvar gnus-read-active-file t
537   "*Non-nil means that Gnus will read the entire active file at startup.
538 If this variable is nil, Gnus will only know about the groups in your
539 `.newsrc' file.
540
541 If this variable is `some', Gnus will try to only read the relevant
542 parts of the active file from the server.  Not all servers support
543 this, and it might be quite slow with other servers, but this should
544 generally be faster than both the t and nil value.
545
546 If you set this variable to nil or `some', you probably still want to
547 be told about new newsgroups that arrive.  To do that, set
548 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
549 properly with all servers.")
550
551 (defvar gnus-level-subscribed 5
552   "*Groups with levels less than or equal to this variable are subscribed.")
553
554 (defvar gnus-level-unsubscribed 7
555   "*Groups with levels less than or equal to this variable are unsubscribed.
556 Groups with levels less than `gnus-level-subscribed', which should be
557 less than this variable, are subscribed.")
558
559 (defvar gnus-level-zombie 8
560   "*Groups with this level are zombie groups.")
561
562 (defvar gnus-level-killed 9
563   "*Groups with this level are killed.")
564
565 (defvar gnus-level-default-subscribed 3
566   "*New subscribed groups will be subscribed at this level.")
567
568 (defvar gnus-level-default-unsubscribed 6
569   "*New unsubscribed groups will be unsubscribed at this level.")
570
571 (defvar gnus-activate-level (1+ gnus-level-subscribed)
572   "*Groups higher than this level won't be activated on startup.
573 Setting this variable to something log might save lots of time when
574 you have many groups that you aren't interested in.")
575
576 (defvar gnus-activate-foreign-newsgroups 4
577   "*If nil, Gnus will not check foreign newsgroups at startup.
578 If it is non-nil, it should be a number between one and nine.  Foreign
579 newsgroups that have a level lower or equal to this number will be
580 activated on startup.  For instance, if you want to active all
581 subscribed newsgroups, but not the rest, you'd set this variable to
582 `gnus-level-subscribed'.
583
584 If you subscribe to lots of newsgroups from different servers, startup
585 might take a while.  By setting this variable to nil, you'll save time,
586 but you won't be told how many unread articles there are in the
587 groups.")
588
589 (defvar gnus-save-newsrc-file t
590   "*Non-nil means that Gnus will save the `.newsrc' file.
591 Gnus always saves its own startup file, which is called
592 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
593 be readily understood by other newsreaders.  If you don't plan on
594 using other newsreaders, set this variable to nil to save some time on
595 exit.")
596
597 (defvar gnus-save-killed-list t
598   "*If non-nil, save the list of killed groups to the startup file.
599 If you set this variable to nil, you'll save both time (when starting
600 and quitting) and space (both memory and disk), but it will also mean
601 that Gnus has no record of which groups are new and which are old, so
602 the automatic new newsgroups subscription methods become meaningless.
603
604 You should always set `gnus-check-new-newsgroups' to `ask-server' or
605 nil if you set this variable to nil.")
606
607 (defvar gnus-interactive-catchup t
608   "*If non-nil, require your confirmation when catching up a group.")
609
610 (defvar gnus-interactive-post t
611   "*If non-nil, group name will be asked for when posting.")
612
613 (defvar gnus-interactive-exit t
614   "*If non-nil, require your confirmation when exiting Gnus.")
615
616 (defvar gnus-kill-killed t
617   "*If non-nil, Gnus will apply kill files to already killed articles.
618 If it is nil, Gnus will never apply kill files to articles that have
619 already been through the scoring process, which might very well save lots
620 of time.")
621
622 (defvar gnus-extract-address-components 'gnus-extract-address-components
623   "*Function for extracting address components from a From header.
624 Two pre-defined function exist: `gnus-extract-address-components',
625 which is the default, quite fast, and too simplistic solution, and
626 `mail-extract-address-components', which works much better, but is
627 slower.")
628
629 (defvar gnus-summary-default-score 0
630   "*Default article score level.
631 If this variable is nil, scoring will be disabled.")
632
633 (defvar gnus-summary-zcore-fuzz 0
634   "*Fuzziness factor for the zcore in the summary buffer.
635 Articles with scores closer than this to `gnus-summary-default-score'
636 will not be marked.")
637
638 (defvar gnus-simplify-subject-fuzzy-regexp nil
639   "*Strings to be removed when doing fuzzy matches.
640 This can either be a regular expression or list of regular expressions
641 that will be removed from subject strings if fuzzy subject
642 simplification is selected.")
643
644 (defvar gnus-permanently-visible-groups nil
645   "*Regexp to match groups that should always be listed in the group buffer.
646 This means that they will still be listed when there are no unread
647 articles in the groups.")
648
649 (defvar gnus-list-groups-with-ticked-articles t
650   "*If non-nil, list groups that have only ticked articles.
651 If nil, only list groups that have unread articles.")
652
653 (defvar gnus-group-default-list-level gnus-level-subscribed
654   "*Default listing level.
655 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
656
657 (defvar gnus-group-use-permanent-levels nil
658   "*If non-nil, once you set a level, Gnus will use this level.")
659
660 (defvar gnus-group-list-inactive-groups t
661   "*If non-nil, inactive groups will be listed.")
662
663 (defvar gnus-show-mime nil
664   "*If non-nil, do mime processing of articles.
665 The articles will simply be fed to the function given by
666 `gnus-show-mime-method'.")
667
668 (defvar gnus-strict-mime t
669   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
670
671 (defvar gnus-show-mime-method 'metamail-buffer
672   "*Function to process a MIME message.
673 The function is called from the article buffer.")
674
675 (defvar gnus-decode-encoded-word-method (lambda ())
676   "*Function to decode a MIME encoded-words.
677 The function is called from the article buffer.")
678
679 (defvar gnus-show-threads t
680   "*If non-nil, display threads in summary mode.")
681
682 (defvar gnus-thread-hide-subtree nil
683   "*If non-nil, hide all threads initially.
684 If threads are hidden, you have to run the command
685 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
686 to expose hidden threads.")
687
688 (defvar gnus-thread-hide-killed t
689   "*If non-nil, hide killed threads automatically.")
690
691 (defvar gnus-thread-ignore-subject nil
692   "*If non-nil, ignore subjects and do all threading based on the Reference header.
693 If nil, which is the default, articles that have different subjects
694 from their parents will start separate threads.")
695
696 (defvar gnus-thread-operation-ignore-subject t
697   "*If non-nil, subjects will be ignored when doing thread commands.
698 This affects commands like `gnus-summary-kill-thread' and
699 `gnus-summary-lower-thread'.
700
701 If this variable is nil, articles in the same thread with different
702 subjects will not be included in the operation in question.  If this
703 variable is `fuzzy', only articles that have subjects that are fuzzily
704 equal will be included.")
705
706 (defvar gnus-thread-indent-level 4
707   "*Number that says how much each sub-thread should be indented.")
708
709 (defvar gnus-ignored-newsgroups
710   (purecopy (mapconcat 'identity
711                        '("^to\\."       ; not "real" groups
712                          "^[0-9. \t]+ " ; all digits in name
713                          "[][\"#'()]"   ; bogus characters
714                          )
715                        "\\|"))
716   "*A regexp to match uninteresting newsgroups in the active file.
717 Any lines in the active file matching this regular expression are
718 removed from the newsgroup list before anything else is done to it,
719 thus making them effectively non-existent.")
720
721 (defvar gnus-ignored-headers
722   "^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:"
723   "*All headers that match this regexp will be hidden.
724 This variable can also be a list of regexps of headers to be ignored.
725 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
726
727 (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-"
728   "*All headers that do not match this regexp will be hidden.
729 This variable can also be a list of regexp of headers to remain visible.
730 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
731
732 (defvar gnus-sorted-header-list
733   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
734     "^Cc:" "^Date:" "^Organization:")
735   "*This variable is a list of regular expressions.
736 If it is non-nil, headers that match the regular expressions will
737 be placed first in the article buffer in the sequence specified by
738 this list.")
739
740 (defvar gnus-boring-article-headers
741   '(empty followup-to reply-to)
742   "*Headers that are only to be displayed if they have interesting data.
743 Possible values in this list are `empty', `newsgroups', `followup-to',
744 `reply-to', and `date'.")
745
746 (defvar gnus-show-all-headers nil
747   "*If non-nil, don't hide any headers.")
748
749 (defvar gnus-save-all-headers t
750   "*If non-nil, don't remove any headers before saving.")
751
752 (defvar gnus-saved-headers gnus-visible-headers
753   "*Headers to keep if `gnus-save-all-headers' is nil.
754 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
755 If that variable is nil, however, all headers that match this regexp
756 will be kept while the rest will be deleted before saving.")
757
758 (defvar gnus-inhibit-startup-message nil
759   "*If non-nil, the startup message will not be displayed.")
760
761 (defvar gnus-signature-separator "^-- *$"
762   "Regexp matching signature separator.")
763
764 (defvar gnus-signature-limit nil
765   "Provide a limit to what is considered a signature.
766 If it is a number, no signature may not be longer (in characters) than
767 that number.  If it is a function, the function will be called without
768 any parameters, and if it returns nil, there is no signature in the
769 buffer.  If it is a string, it will be used as a regexp.  If it
770 matches, the text in question is not a signature.")
771
772 (defvar gnus-auto-extend-newsgroup t
773   "*If non-nil, extend newsgroup forward and backward when requested.")
774
775 (defvar gnus-auto-select-first t
776   "*If nil, don't select the first unread article when entering a group.
777 If this variable is `best', select the highest-scored unread article
778 in the group.  If neither nil nor `best', select the first unread
779 article.
780
781 If you want to prevent automatic selection of the first unread article
782 in some newsgroups, set the variable to nil in
783 `gnus-select-group-hook'.")
784
785 (defvar gnus-auto-select-next t
786   "*If non-nil, offer to go to the next group from the end of the previous.
787 If the value is t and the next newsgroup is empty, Gnus will exit
788 summary mode and go back to group mode.  If the value is neither nil
789 nor t, Gnus will select the following unread newsgroup.  In
790 particular, if the value is the symbol `quietly', the next unread
791 newsgroup will be selected without any confirmation, and if it is
792 `almost-quietly', the next group will be selected without any
793 confirmation if you are located on the last article in the group.
794 Finally, if this variable is `slightly-quietly', the `Z n' command
795 will go to the next group without confirmation.")
796
797 (defvar gnus-auto-select-same nil
798   "*If non-nil, select the next article with the same subject.")
799
800 (defvar gnus-summary-check-current nil
801   "*If non-nil, consider the current article when moving.
802 The \"unread\" movement commands will stay on the same line if the
803 current article is unread.")
804
805 (defvar gnus-auto-center-summary t
806   "*If non-nil, always center the current summary buffer.
807 In particular, if `vertical' do only vertical recentering.  If non-nil
808 and non-`vertical', do both horizontal and vertical recentering.")
809
810 (defvar gnus-break-pages t
811   "*If non-nil, do page breaking on articles.
812 The page delimiter is specified by the `gnus-page-delimiter'
813 variable.")
814
815 (defvar gnus-page-delimiter "^\^L"
816   "*Regexp describing what to use as article page delimiters.
817 The default value is \"^\^L\", which is a form linefeed at the
818 beginning of a line.")
819
820 (defvar gnus-use-full-window t
821   "*If non-nil, use the entire Emacs screen.")
822
823 (defvar gnus-window-configuration nil
824   "Obsolete variable.  See `gnus-buffer-configuration'.")
825
826 (defvar gnus-window-min-width 2
827   "*Minimum width of Gnus buffers.")
828
829 (defvar gnus-window-min-height 1
830   "*Minimum height of Gnus buffers.")
831
832 (defvar gnus-buffer-configuration
833   '((group
834      (vertical 1.0
835                (group 1.0 point)
836                (if gnus-carpal '(group-carpal 4))))
837     (summary
838      (vertical 1.0
839                (summary 1.0 point)
840                (if gnus-carpal '(summary-carpal 4))))
841     (article
842      (cond 
843       (gnus-use-picons
844        '(frame 1.0
845                (vertical 1.0
846                          (summary 0.25 point)
847                          (if gnus-carpal '(summary-carpal 4))
848                          (article 1.0))
849                (vertical ((height . 5) (width . 15)
850                           (user-position . t)
851                           (left . -1) (top . 1))
852                          (picons 1.0))))
853       (gnus-use-trees
854        '(vertical 1.0
855                   (summary 0.25 point)
856                   (tree 0.25)
857                   (article 1.0)))
858       (t
859        '(vertical 1.0
860                  (summary 0.25 point)
861                  (if gnus-carpal '(summary-carpal 4))
862                  (if gnus-use-trees '(tree 0.25))
863                  (article 1.0)))))
864     (server
865      (vertical 1.0
866                (server 1.0 point)
867                (if gnus-carpal '(server-carpal 2))))
868     (browse
869      (vertical 1.0
870                (browse 1.0 point)
871                (if gnus-carpal '(browse-carpal 2))))
872     (group-mail
873      (vertical 1.0
874                (mail 1.0 point)))
875     (group-post
876      (vertical 1.0
877                (post 1.0 point)))
878     (summary-mail
879      (vertical 1.0
880                (mail 1.0 point)))
881     (summary-reply
882      (vertical 1.0
883                (article-copy 0.5)
884                (mail 1.0 point)))
885     (pick
886      (vertical 1.0
887                (article 1.0 point)))
888     (info
889      (vertical 1.0
890                (info 1.0 point)))
891     (summary-faq
892      (vertical 1.0
893                (summary 0.25)
894                (faq 1.0 point)))
895     (edit-group
896      (vertical 1.0
897                (group 0.5)
898                (edit-group 1.0 point)))
899     (edit-server
900      (vertical 1.0
901                (server 0.5)
902                (edit-server 1.0 point)))
903     (edit-score
904      (vertical 1.0
905                (summary 0.25)
906                (edit-score 1.0 point)))
907     (post
908      (vertical 1.0
909                (post 1.0 point)))
910     (reply
911      (vertical 1.0
912                (article-copy 0.5)
913                (mail 1.0 point)))
914     (mail-forward
915      (vertical 1.0
916                (mail 1.0 point)))
917     (post-forward
918      (vertical 1.0
919                (post 1.0 point)))
920     (reply-yank
921      (vertical 1.0
922                (mail 1.0 point)))
923     (mail-bounce
924      (vertical 1.0
925                (article 0.5)
926                (mail 1.0 point)))
927     (draft
928      (vertical 1.0
929                (draft 1.0 point)))
930     (pipe
931      (vertical 1.0
932                (summary 0.25 point)
933                (if gnus-carpal '(summary-carpal 4))
934                ("*Shell Command Output*" 1.0)))
935     (followup
936      (vertical 1.0
937                (article-copy 0.5)
938                (post 1.0 point)))
939     (followup-yank
940      (vertical 1.0
941                (post 1.0 point))))
942   "Window configuration for all possible Gnus buffers.
943 This variable is a list of lists.  Each of these lists has a NAME and
944 a RULE.  The NAMEs are commonsense names like `group', which names a
945 rule used when displaying the group buffer; `summary', which names a
946 rule for what happens when you enter a group and do not display an
947 article buffer; and so on.  See the value of this variable for a
948 complete list of NAMEs.
949
950 Each RULE is a list of vectors.  The first element in this vector is
951 the name of the buffer to be displayed; the second element is the
952 percentage of the screen this buffer is to occupy (a number in the
953 0.0-0.99 range); the optional third element is `point', which should
954 be present to denote which buffer point is to go to after making this
955 buffer configuration.")
956
957 (defvar gnus-window-to-buffer
958   '((group . gnus-group-buffer)
959     (summary . gnus-summary-buffer)
960     (article . gnus-article-buffer)
961     (server . gnus-server-buffer)
962     (browse . "*Gnus Browse Server*")
963     (edit-group . gnus-group-edit-buffer)
964     (edit-server . gnus-server-edit-buffer)
965     (group-carpal . gnus-carpal-group-buffer)
966     (summary-carpal . gnus-carpal-summary-buffer)
967     (server-carpal . gnus-carpal-server-buffer)
968     (browse-carpal . gnus-carpal-browse-buffer)
969     (edit-score . gnus-score-edit-buffer)
970     (mail . gnus-mail-buffer)
971     (post . gnus-post-news-buffer)
972     (faq . gnus-faq-buffer)
973     (picons . "*Picons*")
974     (tree . gnus-tree-buffer)
975     (info . gnus-info-buffer)
976     (article-copy . gnus-article-copy)
977     (draft . gnus-draft-buffer))
978   "Mapping from short symbols to buffer names or buffer variables.")
979
980 (defvar gnus-carpal nil
981   "*If non-nil, display clickable icons.")
982
983 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
984   "*Function called with a group name when new group is detected.
985 A few pre-made functions are supplied: `gnus-subscribe-randomly'
986 inserts new groups at the beginning of the list of groups;
987 `gnus-subscribe-alphabetically' inserts new groups in strict
988 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
989 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
990 for your decision; `gnus-subscribe-killed' kills all new groups.")
991
992 ;; Suggested by a bug report by Hallvard B Furuseth.
993 ;; <h.b.furuseth@usit.uio.no>.
994 (defvar gnus-subscribe-options-newsgroup-method
995   (function gnus-subscribe-alphabetically)
996   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
997 If, for instance, you want to subscribe to all newsgroups in the
998 \"no\" and \"alt\" hierarchies, you'd put the following in your
999 .newsrc file:
1000
1001 options -n no.all alt.all
1002
1003 Gnus will the subscribe all new newsgroups in these hierarchies with
1004 the subscription method in this variable.")
1005
1006 (defvar gnus-subscribe-hierarchical-interactive nil
1007   "*If non-nil, Gnus will offer to subscribe hierarchically.
1008 When a new hierarchy appears, Gnus will ask the user:
1009
1010 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1011
1012 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1013 subscribe to all newsgroups in the hierarchy and `s' will skip this
1014 hierarchy in its entirety.")
1015
1016 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1017   "*Function used for sorting the group buffer.
1018 This function will be called with group info entries as the arguments
1019 for the groups to be sorted.  Pre-made functions include
1020 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1021 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1022 `gnus-group-sort-by-rank'.
1023
1024 This variable can also be a list of sorting functions.  In that case,
1025 the most significant sort function should be the last function in the
1026 list.")
1027
1028 ;; Mark variables suggested by Thomas Michanek
1029 ;; <Thomas.Michanek@telelogic.se>.
1030 (defvar gnus-unread-mark ? 
1031   "*Mark used for unread articles.")
1032 (defvar gnus-ticked-mark ?!
1033   "*Mark used for ticked articles.")
1034 (defvar gnus-dormant-mark ??
1035   "*Mark used for dormant articles.")
1036 (defvar gnus-del-mark ?r
1037   "*Mark used for del'd articles.")
1038 (defvar gnus-read-mark ?R
1039   "*Mark used for read articles.")
1040 (defvar gnus-expirable-mark ?E
1041   "*Mark used for expirable articles.")
1042 (defvar gnus-killed-mark ?K
1043   "*Mark used for killed articles.")
1044 (defvar gnus-souped-mark ?F
1045   "*Mark used for killed articles.")
1046 (defvar gnus-kill-file-mark ?X
1047   "*Mark used for articles killed by kill files.")
1048 (defvar gnus-low-score-mark ?Y
1049   "*Mark used for articles with a low score.")
1050 (defvar gnus-catchup-mark ?C
1051   "*Mark used for articles that are caught up.")
1052 (defvar gnus-replied-mark ?A
1053   "*Mark used for articles that have been replied to.")
1054 (defvar gnus-cached-mark ?*
1055   "*Mark used for articles that are in the cache.")
1056 (defvar gnus-saved-mark ?S
1057   "*Mark used for articles that have been saved to.")
1058 (defvar gnus-process-mark ?#
1059   "*Process mark.")
1060 (defvar gnus-ancient-mark ?O
1061   "*Mark used for ancient articles.")
1062 (defvar gnus-sparse-mark ?Q
1063   "*Mark used for sparsely reffed articles.")
1064 (defvar gnus-canceled-mark ?G
1065   "*Mark used for canceled articles.")
1066 (defvar gnus-score-over-mark ?+
1067   "*Score mark used for articles with high scores.")
1068 (defvar gnus-score-below-mark ?-
1069   "*Score mark used for articles with low scores.")
1070 (defvar gnus-empty-thread-mark ? 
1071   "*There is no thread under the article.")
1072 (defvar gnus-not-empty-thread-mark ?=
1073   "*There is a thread under the article.")
1074
1075 (defvar gnus-view-pseudo-asynchronously nil
1076   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1077
1078 (defvar gnus-view-pseudos nil
1079   "*If `automatic', pseudo-articles will be viewed automatically.
1080 If `not-confirm', pseudos will be viewed automatically, and the user
1081 will not be asked to confirm the command.")
1082
1083 (defvar gnus-view-pseudos-separately t
1084   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1085 If nil, all files that use the same viewing command will be given as a
1086 list of parameters to that command.")
1087
1088 (defvar gnus-insert-pseudo-articles t
1089   "*If non-nil, insert pseudo-articles when decoding articles.")
1090
1091 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1092   "*Format of group lines.
1093 It works along the same lines as a normal formatting string,
1094 with some simple extensions.
1095
1096 %M    Only marked articles (character, \"*\" or \" \")
1097 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1098 %L    Level of subscribedness (integer)
1099 %N    Number of unread articles (integer)
1100 %I    Number of dormant articles (integer)
1101 %i    Number of ticked and dormant (integer)
1102 %T    Number of ticked articles (integer)
1103 %R    Number of read articles (integer)
1104 %t    Total number of articles (integer)
1105 %y    Number of unread, unticked articles (integer)
1106 %G    Group name (string)
1107 %g    Qualified group name (string)
1108 %D    Group description (string)
1109 %s    Select method (string)
1110 %o    Moderated group (char, \"m\")
1111 %p    Process mark (char)
1112 %O    Moderated group (string, \"(m)\" or \"\")
1113 %P    Topic indentation (string)
1114 %l    Whether there are GroupLens predictions for this group (string)
1115 %n    Select from where (string)
1116 %z    A string that look like `<%s:%n>' if a foreign select method is used
1117 %u    User defined specifier.  The next character in the format string should
1118       be a letter.  Gnus will call the function gnus-user-format-function-X,
1119       where X is the letter following %u.  The function will be passed the
1120       current header as argument.  The function should return a string, which
1121       will be inserted into the buffer just like information from any other
1122       group specifier.
1123
1124 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1125 the mouse point move inside the area.  There can only be one such area.
1126
1127 Note that this format specification is not always respected.  For
1128 reasons of efficiency, when listing killed groups, this specification
1129 is ignored altogether.  If the spec is changed considerably, your
1130 output may end up looking strange when listing both alive and killed
1131 groups.
1132
1133 If you use %o or %O, reading the active file will be slower and quite
1134 a bit of extra memory will be used. %D will also worsen performance.
1135 Also note that if you change the format specification to include any
1136 of these specs, you must probably re-start Gnus to see them go into
1137 effect.")
1138
1139 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1140   "*The format specification of the lines in the summary buffer.
1141
1142 It works along the same lines as a normal formatting string,
1143 with some simple extensions.
1144
1145 %N   Article number, left padded with spaces (string)
1146 %S   Subject (string)
1147 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1148 %n   Name of the poster (string)
1149 %a   Extracted name of the poster (string)
1150 %A   Extracted address of the poster (string)
1151 %F   Contents of the From: header (string)
1152 %x   Contents of the Xref: header (string)
1153 %D   Date of the article (string)
1154 %d   Date of the article (string) in DD-MMM format
1155 %M   Message-id of the article (string)
1156 %r   References of the article (string)
1157 %c   Number of characters in the article (integer)
1158 %L   Number of lines in the article (integer)
1159 %I   Indentation based on thread level (a string of spaces)
1160 %T   A string with two possible values: 80 spaces if the article
1161      is on thread level two or larger and 0 spaces on level one
1162 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1163 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1164 %[   Opening bracket (character, \"[\" or \"<\")
1165 %]   Closing bracket (character, \"]\" or \">\")
1166 %>   Spaces of length thread-level (string)
1167 %<   Spaces of length (- 20 thread-level) (string)
1168 %i   Article score (number)
1169 %z   Article zcore (character)
1170 %t   Number of articles under the current thread (number).
1171 %e   Whether the thread is empty or not (character).
1172 %l   GroupLens score (number)
1173 %u   User defined specifier.  The next character in the format string should
1174      be a letter.  Gnus will call the function gnus-user-format-function-X,
1175      where X is the letter following %u.  The function will be passed the
1176      current header as argument.  The function should return a string, which
1177      will be inserted into the summary just like information from any other
1178      summary specifier.
1179
1180 Text between %( and %) will be highlighted with `gnus-mouse-face'
1181 when the mouse point is placed inside the area.  There can only be one
1182 such area.
1183
1184 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1185 with care.  For reasons of efficiency, Gnus will compute what column
1186 these characters will end up in, and \"hard-code\" that.  This means that
1187 it is illegal to have these specs after a variable-length spec.  Well,
1188 you might not be arrested, but your summary buffer will look strange,
1189 which is bad enough.
1190
1191 The smart choice is to have these specs as for to the left as
1192 possible.
1193
1194 This restriction may disappear in later versions of Gnus.")
1195
1196 (defvar gnus-summary-dummy-line-format
1197   "*  %(:                          :%) %S\n"
1198   "*The format specification for the dummy roots in the summary buffer.
1199 It works along the same lines as a normal formatting string,
1200 with some simple extensions.
1201
1202 %S  The subject")
1203
1204 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1205   "*The format specification for the summary mode line.
1206 It works along the same lines as a normal formatting string,
1207 with some simple extensions:
1208
1209 %G  Group name
1210 %p  Unprefixed group name
1211 %A  Current article number
1212 %V  Gnus version
1213 %U  Number of unread articles in the group
1214 %e  Number of unselected articles in the group
1215 %Z  A string with unread/unselected article counts
1216 %g  Shortish group name
1217 %S  Subject of the current article
1218 %u  User-defined spec
1219 %s  Current score file name
1220 %d  Number of dormant articles
1221 %r  Number of articles that have been marked as read in this session
1222 %E  Number of articles expunged by the score files")
1223
1224 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1225   "*The format specification for the article mode line.
1226 See `gnus-summary-mode-line-format' for a closer description.")
1227
1228 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1229   "*The format specification for the group mode line.
1230 It works along the same lines as a normal formatting string,
1231 with some simple extensions:
1232
1233 %S   The native news server.
1234 %M   The native select method.")
1235
1236 (defvar gnus-valid-select-methods
1237   '(("nntp" post address prompt-address)
1238     ("nnspool" post address)
1239     ("nnvirtual" post-mail virtual prompt-address)
1240     ("nnmbox" mail respool address)
1241     ("nnml" mail respool address)
1242     ("nnmh" mail respool address)
1243     ("nndir" post-mail prompt-address address)
1244     ("nneething" none address prompt-address)
1245     ("nndoc" none address prompt-address)
1246     ("nnbabyl" mail address respool)
1247     ("nnkiboze" post address virtual)
1248     ("nnsoup" post-mail address)
1249     ("nndraft" post-mail)
1250     ("nnfolder" mail respool address))
1251   "An alist of valid select methods.
1252 The first element of each list lists should be a string with the name
1253 of the select method.  The other elements may be be the category of
1254 this method (ie. `post', `mail', `none' or whatever) or other
1255 properties that this method has (like being respoolable).
1256 If you implement a new select method, all you should have to change is
1257 this variable.  I think.")
1258
1259 (defvar gnus-updated-mode-lines '(group article summary tree)
1260   "*List of buffers that should update their mode lines.
1261 The list may contain the symbols `group', `article' and `summary'.  If
1262 the corresponding symbol is present, Gnus will keep that mode line
1263 updated with information that may be pertinent.
1264 If this variable is nil, screen refresh may be quicker.")
1265
1266 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1267 (defvar gnus-mode-non-string-length nil
1268   "*Max length of mode-line non-string contents.
1269 If this is nil, Gnus will take space as is needed, leaving the rest
1270 of the modeline intact.")
1271
1272 ;see gnus-cus.el
1273 ;(defvar gnus-mouse-face 'highlight
1274 ;  "*Face used for mouse highlighting in Gnus.
1275 ;No mouse highlights will be done if `gnus-visual' is nil.")
1276
1277 (defvar gnus-summary-mark-below nil
1278   "*Mark all articles with a score below this variable as read.
1279 This variable is local to each summary buffer and usually set by the
1280 score file.")
1281
1282 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1283   "*List of functions used for sorting articles in the summary buffer.
1284 This variable is only used when not using a threaded display.")
1285
1286 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1287   "*List of functions used for sorting threads in the summary buffer.
1288 By default, threads are sorted by article number.
1289
1290 Each function takes two threads and return non-nil if the first thread
1291 should be sorted before the other.  If you use more than one function,
1292 the primary sort function should be the last.  You should probably
1293 always include `gnus-thread-sort-by-number' in the list of sorting
1294 functions -- preferably first.
1295
1296 Ready-mady functions include `gnus-thread-sort-by-number',
1297 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1298 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1299 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1300
1301 (defvar gnus-thread-score-function '+
1302   "*Function used for calculating the total score of a thread.
1303
1304 The function is called with the scores of the article and each
1305 subthread and should then return the score of the thread.
1306
1307 Some functions you can use are `+', `max', or `min'.")
1308
1309 (defvar gnus-summary-expunge-below nil
1310   "All articles that have a score less than this variable will be expunged.")
1311
1312 (defvar gnus-thread-expunge-below nil
1313   "All threads that have a total score less than this variable will be expunged.
1314 See `gnus-thread-score-function' for en explanation of what a
1315 \"thread score\" is.")
1316
1317 (defvar gnus-auto-subscribed-groups
1318   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1319   "*All new groups that match this regexp will be subscribed automatically.
1320 Note that this variable only deals with new groups.  It has no effect
1321 whatsoever on old groups.")
1322
1323 (defvar gnus-options-subscribe nil
1324   "*All new groups matching this regexp will be subscribed unconditionally.
1325 Note that this variable deals only with new newsgroups.  This variable
1326 does not affect old newsgroups.")
1327
1328 (defvar gnus-options-not-subscribe nil
1329   "*All new groups matching this regexp will be ignored.
1330 Note that this variable deals only with new newsgroups.  This variable
1331 does not affect old (already subscribed) newsgroups.")
1332
1333 (defvar gnus-auto-expirable-newsgroups nil
1334   "*Groups in which to automatically mark read articles as expirable.
1335 If non-nil, this should be a regexp that should match all groups in
1336 which to perform auto-expiry.  This only makes sense for mail groups.")
1337
1338 (defvar gnus-total-expirable-newsgroups nil
1339   "*Groups in which to perform expiry of all read articles.
1340 Use with extreme caution.  All groups that match this regexp will be
1341 expiring - which means that all read articles will be deleted after
1342 (say) one week.  (This only goes for mail groups and the like, of
1343 course.)")
1344
1345 (defvar gnus-group-uncollapsed-levels 1
1346   "Number of group name elements to leave alone when making a short group name.")
1347
1348 (defvar gnus-hidden-properties '(invisible t intangible t)
1349   "Property list to use for hiding text.")
1350
1351 (defvar gnus-modtime-botch nil
1352   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1353 due to the bogus appearance that .newsrc was modified on disc.")
1354
1355 ;; Hooks.
1356
1357 (defvar gnus-group-mode-hook nil
1358   "*A hook for Gnus group mode.")
1359
1360 (defvar gnus-summary-mode-hook nil
1361   "*A hook for Gnus summary mode.
1362 This hook is run before any variables are set in the summary buffer.")
1363
1364 (defvar gnus-article-mode-hook nil
1365   "*A hook for Gnus article mode.")
1366
1367 (defvar gnus-summary-prepare-exit-hook nil
1368   "*A hook called when preparing to exit from the summary buffer.
1369 It calls `gnus-summary-expire-articles' by default.")
1370 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1371
1372 (defvar gnus-summary-exit-hook nil
1373   "*A hook called on exit from the summary buffer.")
1374
1375 (defvar gnus-group-catchup-group-hook nil
1376   "*A hook run when catching up a group from the group buffer.")
1377
1378 (defvar gnus-open-server-hook nil
1379   "*A hook called just before opening connection to the news server.")
1380
1381 (defvar gnus-load-hook nil
1382   "*A hook run while Gnus is loaded.")
1383
1384 (defvar gnus-startup-hook nil
1385   "*A hook called at startup.
1386 This hook is called after Gnus is connected to the NNTP server.")
1387
1388 (defvar gnus-get-new-news-hook nil
1389   "*A hook run just before Gnus checks for new news.")
1390
1391 (defvar gnus-after-getting-new-news-hook nil
1392   "*A hook run after Gnus checks for new news.")
1393
1394 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1395   "*A function that is called to generate the group buffer.
1396 The function is called with three arguments: The first is a number;
1397 all group with a level less or equal to that number should be listed,
1398 if the second is non-nil, empty groups should also be displayed.  If
1399 the third is non-nil, it is a number.  No groups with a level lower
1400 than this number should be displayed.
1401
1402 The only current function implemented is `gnus-group-prepare-flat'.")
1403
1404 (defvar gnus-group-prepare-hook nil
1405   "*A hook called after the group buffer has been generated.
1406 If you want to modify the group buffer, you can use this hook.")
1407
1408 (defvar gnus-summary-prepare-hook nil
1409   "*A hook called after the summary buffer has been generated.
1410 If you want to modify the summary buffer, you can use this hook.")
1411
1412 (defvar gnus-summary-generate-hook nil
1413   "*A hook run just before generating the summary buffer.
1414 This hook is commonly used to customize threading variables and the
1415 like.")
1416
1417 (defvar gnus-article-prepare-hook nil
1418   "*A hook called after an article has been prepared in the article buffer.
1419 If you want to run a special decoding program like nkf, use this hook.")
1420
1421 ;(defvar gnus-article-display-hook nil
1422 ;  "*A hook called after the article is displayed in the article buffer.
1423 ;The hook is designed to change the contents of the article
1424 ;buffer.  Typical functions that this hook may contain are
1425 ;`gnus-article-hide-headers' (hide selected headers),
1426 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1427 ;`gnus-article-hide-signature' (hide signature) and
1428 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1429 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1432
1433 (defvar gnus-article-x-face-command
1434   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1435   "String or function to be executed to display an X-Face header.
1436 If it is a string, the command will be executed in a sub-shell
1437 asynchronously.  The compressed face will be piped to this command.")
1438
1439 (defvar gnus-article-x-face-too-ugly nil
1440   "Regexp matching posters whose face shouldn't be shown automatically.")
1441
1442 (defvar gnus-select-group-hook nil
1443   "*A hook called when a newsgroup is selected.
1444
1445 If you'd like to simplify subjects like the
1446 `gnus-summary-next-same-subject' command does, you can use the
1447 following hook:
1448
1449  (setq gnus-select-group-hook
1450       (list
1451         (lambda ()
1452           (mapcar (lambda (header)
1453                      (mail-header-set-subject
1454                       header
1455                       (gnus-simplify-subject
1456                        (mail-header-subject header) 're-only)))
1457                   gnus-newsgroup-headers))))")
1458
1459 (defvar gnus-select-article-hook nil
1460   "*A hook called when an article is selected.")
1461
1462 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1463   "*A hook called to apply kill files to a group.
1464 This hook is intended to apply a kill file to the selected newsgroup.
1465 The function `gnus-apply-kill-file' is called by default.
1466
1467 Since a general kill file is too heavy to use only for a few
1468 newsgroups, I recommend you to use a lighter hook function.  For
1469 example, if you'd like to apply a kill file to articles which contains
1470 a string `rmgroup' in subject in newsgroup `control', you can use the
1471 following hook:
1472
1473  (setq gnus-apply-kill-hook
1474       (list
1475         (lambda ()
1476           (cond ((string-match \"control\" gnus-newsgroup-name)
1477                  (gnus-kill \"Subject\" \"rmgroup\")
1478                  (gnus-expunge \"X\"))))))")
1479
1480 (defvar gnus-visual-mark-article-hook
1481   (list 'gnus-highlight-selected-summary)
1482   "*Hook run after selecting an article in the summary buffer.
1483 It is meant to be used for highlighting the article in some way.  It
1484 is not run if `gnus-visual' is nil.")
1485
1486 (defvar gnus-parse-headers-hook nil
1487   "*A hook called before parsing the headers.")
1488
1489 (defvar gnus-exit-group-hook nil
1490   "*A hook called when exiting (not quitting) summary mode.")
1491
1492 (defvar gnus-suspend-gnus-hook nil
1493   "*A hook called when suspending (not exiting) Gnus.")
1494
1495 (defvar gnus-exit-gnus-hook nil
1496   "*A hook called when exiting Gnus.")
1497
1498 (defvar gnus-after-exiting-gnus-hook nil
1499   "*A hook called after exiting Gnus.")
1500
1501 (defvar gnus-save-newsrc-hook nil
1502   "*A hook called before saving any of the newsrc files.")
1503
1504 (defvar gnus-save-quick-newsrc-hook nil
1505   "*A hook called just before saving the quick newsrc file.
1506 Can be used to turn version control on or off.")
1507
1508 (defvar gnus-save-standard-newsrc-hook nil
1509   "*A hook called just before saving the standard newsrc file.
1510 Can be used to turn version control on or off.")
1511
1512 (defvar gnus-summary-update-hook
1513   (list 'gnus-summary-highlight-line)
1514   "*A hook called when a summary line is changed.
1515 The hook will not be called if `gnus-visual' is nil.
1516
1517 The default function `gnus-summary-highlight-line' will
1518 highlight the line according to the `gnus-summary-highlight'
1519 variable.")
1520
1521 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1522   "*A hook called when a group line is changed.
1523 The hook will not be called if `gnus-visual' is nil.
1524
1525 The default function `gnus-group-highlight-line' will
1526 highlight the line according to the `gnus-group-highlight'
1527 variable.")
1528
1529 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1530   "*A hook called when an article is selected for the first time.
1531 The hook is intended to mark an article as read (or unread)
1532 automatically when it is selected.")
1533
1534 (defvar gnus-group-change-level-function nil
1535   "Function run when a group level is changed.
1536 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1537
1538 ;; Remove any hilit infestation.
1539 (add-hook 'gnus-startup-hook
1540           (lambda ()
1541             (remove-hook 'gnus-summary-prepare-hook
1542                          'hilit-rehighlight-buffer-quietly)
1543             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1544             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1545             (remove-hook 'gnus-article-prepare-hook
1546                          'hilit-rehighlight-buffer-quietly)))
1547
1548 \f
1549 ;; Internal variables
1550
1551 (defvar gnus-server-alist nil
1552   "List of available servers.")
1553
1554 (defvar gnus-topic-active-topology nil)
1555 (defvar gnus-topic-active-alist nil)
1556
1557 (defvar gnus-group-indentation-function nil)
1558
1559 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1560
1561 (defvar gnus-goto-missing-group-function nil)
1562
1563 (defvar gnus-override-subscribe-method nil)
1564
1565 (defvar gnus-group-goto-next-group-function nil
1566   "Function to override finding the next group after listing groups.")
1567
1568 (defconst gnus-article-mark-lists
1569   '((marked . tick) (replied . reply)
1570     (expirable . expire) (killed . killed)
1571     (bookmarks . bookmark) (dormant . dormant)
1572     (scored . score) (saved . save)
1573     (cached . cache)
1574     ))
1575
1576 ;; Avoid highlighting in kill files.
1577 (defvar gnus-summary-inhibit-highlight nil)
1578 (defvar gnus-newsgroup-selected-overlay nil)
1579
1580 (defvar gnus-inhibit-hiding nil)
1581 (defvar gnus-group-indentation "")
1582 (defvar gnus-inhibit-limiting nil)
1583 (defvar gnus-created-frames nil)
1584
1585 (defvar gnus-article-mode-map nil)
1586 (defvar gnus-dribble-buffer nil)
1587 (defvar gnus-headers-retrieved-by nil)
1588 (defvar gnus-article-reply nil)
1589 (defvar gnus-override-method nil)
1590 (defvar gnus-article-check-size nil)
1591
1592 (defvar gnus-current-score-file nil)
1593 (defvar gnus-newsgroup-adaptive-score-file nil)
1594 (defvar gnus-scores-exclude-files nil)
1595
1596 (defvar gnus-opened-servers nil)
1597
1598 (defvar gnus-current-move-group nil)
1599
1600 (defvar gnus-newsgroup-dependencies nil)
1601 (defvar gnus-newsgroup-async nil)
1602 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1603
1604 (defvar gnus-newsgroup-adaptive nil)
1605
1606 (defvar gnus-summary-display-table nil)
1607 (defvar gnus-summary-display-article-function nil)
1608
1609 (defvar gnus-summary-highlight-line-function nil
1610   "Function called after highlighting a summary line.")
1611
1612 (defvar gnus-group-line-format-alist
1613   `((?M gnus-tmp-marked-mark ?c)
1614     (?S gnus-tmp-subscribed ?c)
1615     (?L gnus-tmp-level ?d)
1616     (?N (cond ((eq number t) "*" )
1617               ((numberp number) 
1618                (int-to-string
1619                 (+ number
1620                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1621                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1622               (t number)) ?s)
1623     (?R gnus-tmp-number-of-read ?s)
1624     (?t gnus-tmp-number-total ?d)
1625     (?y gnus-tmp-number-of-unread ?s)
1626     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1627     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1628     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1629            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1630     (?g gnus-tmp-group ?s)
1631     (?G gnus-tmp-qualified-group ?s)
1632     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1633     (?D gnus-tmp-newsgroup-description ?s)
1634     (?o gnus-tmp-moderated ?c)
1635     (?O gnus-tmp-moderated-string ?s)
1636     (?p gnus-tmp-process-marked ?c)
1637     (?s gnus-tmp-news-server ?s)
1638     (?n gnus-tmp-news-method ?s)
1639     (?P gnus-group-indentation ?s)
1640     (?l gnus-tmp-grouplens ?s)
1641     (?z gnus-tmp-news-method-string ?s)
1642     (?u gnus-tmp-user-defined ?s)))
1643
1644 (defvar gnus-summary-line-format-alist
1645   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1646     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1647     (?s gnus-tmp-subject-or-nil ?s)
1648     (?n gnus-tmp-name ?s)
1649     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1650         ?s)
1651     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1652             gnus-tmp-from) ?s)
1653     (?F gnus-tmp-from ?s)
1654     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1655     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1656     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1657     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1658     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1659     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1660     (?L gnus-tmp-lines ?d)
1661     (?I gnus-tmp-indentation ?s)
1662     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1663     (?R gnus-tmp-replied ?c)
1664     (?\[ gnus-tmp-opening-bracket ?c)
1665     (?\] gnus-tmp-closing-bracket ?c)
1666     (?\> (make-string gnus-tmp-level ? ) ?s)
1667     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1668     (?i gnus-tmp-score ?d)
1669     (?z gnus-tmp-score-char ?c)
1670     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1671     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1672     (?U gnus-tmp-unread ?c)
1673     (?t (gnus-summary-number-of-articles-in-thread
1674          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1675         ?d)
1676     (?e (gnus-summary-number-of-articles-in-thread
1677          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1678         ?c)
1679     (?u gnus-tmp-user-defined ?s))
1680   "An alist of format specifications that can appear in summary lines,
1681 and what variables they correspond with, along with the type of the
1682 variable (string, integer, character, etc).")
1683
1684 (defvar gnus-summary-dummy-line-format-alist
1685   `((?S gnus-tmp-subject ?s)
1686     (?N gnus-tmp-number ?d)
1687     (?u gnus-tmp-user-defined ?s)))
1688
1689 (defvar gnus-summary-mode-line-format-alist
1690   `((?G gnus-tmp-group-name ?s)
1691     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1692     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1693     (?A gnus-tmp-article-number ?d)
1694     (?Z gnus-tmp-unread-and-unselected ?s)
1695     (?V gnus-version ?s)
1696     (?U gnus-tmp-unread ?d)
1697     (?S gnus-tmp-subject ?s)
1698     (?e gnus-tmp-unselected ?d)
1699     (?u gnus-tmp-user-defined ?s)
1700     (?d (length gnus-newsgroup-dormant) ?d)
1701     (?t (length gnus-newsgroup-marked) ?d)
1702     (?r (length gnus-newsgroup-reads) ?d)
1703     (?E gnus-newsgroup-expunged-tally ?d)
1704     (?s (gnus-current-score-file-nondirectory) ?s)))
1705
1706 (defvar gnus-article-mode-line-format-alist
1707   gnus-summary-mode-line-format-alist)
1708
1709 (defvar gnus-group-mode-line-format-alist
1710   `((?S gnus-tmp-news-server ?s)
1711     (?M gnus-tmp-news-method ?s)
1712     (?u gnus-tmp-user-defined ?s)))
1713
1714 (defvar gnus-have-read-active-file nil)
1715
1716 (defconst gnus-maintainer
1717   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1718   "The mail address of the Gnus maintainers.")
1719
1720 (defconst gnus-version "September Gnus v0.51"
1721   "Version number for this version of Gnus.")
1722
1723 (defvar gnus-info-nodes
1724   '((gnus-group-mode            "(gnus)The Group Buffer")
1725     (gnus-summary-mode          "(gnus)The Summary Buffer")
1726     (gnus-article-mode          "(gnus)The Article Buffer"))
1727   "Assoc list of major modes and related Info nodes.")
1728
1729 (defvar gnus-group-buffer "*Group*")
1730 (defvar gnus-summary-buffer "*Summary*")
1731 (defvar gnus-article-buffer "*Article*")
1732 (defvar gnus-server-buffer "*Server*")
1733
1734 (defvar gnus-work-buffer " *gnus work*")
1735
1736 (defvar gnus-original-article-buffer " *Original Article*")
1737 (defvar gnus-original-article nil)
1738
1739 (defvar gnus-buffer-list nil
1740   "Gnus buffers that should be killed on exit.")
1741
1742 (defvar gnus-slave nil
1743   "Whether this Gnus is a slave or not.")
1744
1745 (defvar gnus-variable-list
1746   '(gnus-newsrc-options gnus-newsrc-options-n
1747     gnus-newsrc-last-checked-date
1748     gnus-newsrc-alist gnus-server-alist
1749     gnus-killed-list gnus-zombie-list
1750     gnus-topic-topology gnus-topic-alist
1751     gnus-format-specs)
1752   "Gnus variables saved in the quick startup file.")
1753
1754 (defvar gnus-newsrc-options nil
1755   "Options line in the .newsrc file.")
1756
1757 (defvar gnus-newsrc-options-n nil
1758   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1759
1760 (defvar gnus-newsrc-last-checked-date nil
1761   "Date Gnus last asked server for new newsgroups.")
1762
1763 (defvar gnus-topic-topology nil
1764   "The complete topic hierarchy.")
1765
1766 (defvar gnus-topic-alist nil
1767   "The complete topic-group alist.")
1768
1769 (defvar gnus-newsrc-alist nil
1770   "Assoc list of read articles.
1771 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1772
1773 (defvar gnus-newsrc-hashtb nil
1774   "Hashtable of gnus-newsrc-alist.")
1775
1776 (defvar gnus-killed-list nil
1777   "List of killed newsgroups.")
1778
1779 (defvar gnus-killed-hashtb nil
1780   "Hash table equivalent of gnus-killed-list.")
1781
1782 (defvar gnus-zombie-list nil
1783   "List of almost dead newsgroups.")
1784
1785 (defvar gnus-description-hashtb nil
1786   "Descriptions of newsgroups.")
1787
1788 (defvar gnus-list-of-killed-groups nil
1789   "List of newsgroups that have recently been killed by the user.")
1790
1791 (defvar gnus-active-hashtb nil
1792   "Hashtable of active articles.")
1793
1794 (defvar gnus-moderated-list nil
1795   "List of moderated newsgroups.")
1796
1797 (defvar gnus-group-marked nil)
1798
1799 (defvar gnus-current-startup-file nil
1800   "Startup file for the current host.")
1801
1802 (defvar gnus-last-search-regexp nil
1803   "Default regexp for article search command.")
1804
1805 (defvar gnus-last-shell-command nil
1806   "Default shell command on article.")
1807
1808 (defvar gnus-current-select-method nil
1809   "The current method for selecting a newsgroup.")
1810
1811 (defvar gnus-group-list-mode nil)
1812
1813 (defvar gnus-article-internal-prepare-hook nil)
1814
1815 (defvar gnus-newsgroup-name nil)
1816 (defvar gnus-newsgroup-begin nil)
1817 (defvar gnus-newsgroup-end nil)
1818 (defvar gnus-newsgroup-last-rmail nil)
1819 (defvar gnus-newsgroup-last-mail nil)
1820 (defvar gnus-newsgroup-last-folder nil)
1821 (defvar gnus-newsgroup-last-file nil)
1822 (defvar gnus-newsgroup-auto-expire nil)
1823 (defvar gnus-newsgroup-active nil)
1824
1825 (defvar gnus-newsgroup-data nil)
1826 (defvar gnus-newsgroup-data-reverse nil)
1827 (defvar gnus-newsgroup-limit nil)
1828 (defvar gnus-newsgroup-limits nil)
1829
1830 (defvar gnus-newsgroup-unreads nil
1831   "List of unread articles in the current newsgroup.")
1832
1833 (defvar gnus-newsgroup-unselected nil
1834   "List of unselected unread articles in the current newsgroup.")
1835
1836 (defvar gnus-newsgroup-reads nil
1837   "Alist of read articles and article marks in the current newsgroup.")
1838
1839 (defvar gnus-newsgroup-expunged-tally nil)
1840
1841 (defvar gnus-newsgroup-marked nil
1842   "List of ticked articles in the current newsgroup (a subset of unread art).")
1843
1844 (defvar gnus-newsgroup-killed nil
1845   "List of ranges of articles that have been through the scoring process.")
1846
1847 (defvar gnus-newsgroup-cached nil
1848   "List of articles that come from the article cache.")
1849
1850 (defvar gnus-newsgroup-saved nil
1851   "List of articles that have been saved.")
1852
1853 (defvar gnus-newsgroup-kill-headers nil)
1854
1855 (defvar gnus-newsgroup-replied nil
1856   "List of articles that have been replied to in the current newsgroup.")
1857
1858 (defvar gnus-newsgroup-expirable nil
1859   "List of articles in the current newsgroup that can be expired.")
1860
1861 (defvar gnus-newsgroup-processable nil
1862   "List of articles in the current newsgroup that can be processed.")
1863
1864 (defvar gnus-newsgroup-bookmarks nil
1865   "List of articles in the current newsgroup that have bookmarks.")
1866
1867 (defvar gnus-newsgroup-dormant nil
1868   "List of dormant articles in the current newsgroup.")
1869
1870 (defvar gnus-newsgroup-scored nil
1871   "List of scored articles in the current newsgroup.")
1872
1873 (defvar gnus-newsgroup-headers nil
1874   "List of article headers in the current newsgroup.")
1875
1876 (defvar gnus-newsgroup-threads nil)
1877
1878 (defvar gnus-newsgroup-prepared nil
1879   "Whether the current group has been prepared properly.")
1880
1881 (defvar gnus-newsgroup-ancient nil
1882   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1883
1884 (defvar gnus-newsgroup-sparse nil)
1885
1886 (defvar gnus-current-article nil)
1887 (defvar gnus-article-current nil)
1888 (defvar gnus-current-headers nil)
1889 (defvar gnus-have-all-headers nil)
1890 (defvar gnus-last-article nil)
1891 (defvar gnus-newsgroup-history nil)
1892 (defvar gnus-current-kill-article nil)
1893
1894 ;; Save window configuration.
1895 (defvar gnus-prev-winconf nil)
1896
1897 (defvar gnus-summary-mark-positions nil)
1898 (defvar gnus-group-mark-positions nil)
1899
1900 (defvar gnus-reffed-article-number nil)
1901
1902 ;;; Let the byte-compiler know that we know about this variable.
1903 (defvar rmail-default-rmail-file)
1904
1905 (defvar gnus-cache-removable-articles nil)
1906
1907 (defvar gnus-dead-summary nil)
1908
1909 (defconst gnus-summary-local-variables
1910   '(gnus-newsgroup-name
1911     gnus-newsgroup-begin gnus-newsgroup-end
1912     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1913     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1914     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1915     gnus-newsgroup-unselected gnus-newsgroup-marked
1916     gnus-newsgroup-reads gnus-newsgroup-saved
1917     gnus-newsgroup-replied gnus-newsgroup-expirable
1918     gnus-newsgroup-processable gnus-newsgroup-killed
1919     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1920     gnus-newsgroup-headers gnus-newsgroup-threads
1921     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1922     gnus-current-article gnus-current-headers gnus-have-all-headers
1923     gnus-last-article gnus-article-internal-prepare-hook
1924     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1925     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1926     gnus-newsgroup-async 
1927     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1928     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1929     gnus-newsgroup-history gnus-newsgroup-ancient
1930     gnus-newsgroup-sparse
1931     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1932     gnus-newsgroup-adaptive-score-file
1933     (gnus-newsgroup-expunged-tally . 0)
1934     gnus-cache-removable-articles gnus-newsgroup-cached
1935     gnus-newsgroup-data gnus-newsgroup-data-reverse
1936     gnus-newsgroup-limit gnus-newsgroup-limits)
1937   "Variables that are buffer-local to the summary buffers.")
1938
1939 (defconst gnus-bug-message
1940   "Sending a bug report to the Gnus Towers.
1941 ========================================
1942
1943 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1944 be sent to the Gnus Bug Exterminators.
1945
1946 At the bottom of the buffer you'll see lots of variable settings.
1947 Please do not delete those.  They will tell the Bug People what your
1948 environment is, so that it will be easier to locate the bugs.
1949
1950 If you have found a bug that makes Emacs go \"beep\", set
1951 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1952 and include the backtrace in your bug report.
1953
1954 Please describe the bug in annoying, painstaking detail.
1955
1956 Thank you for your help in stamping out bugs.
1957 ")
1958
1959 ;;; End of variables.
1960
1961 ;; Define some autoload functions Gnus might use.
1962 (eval-and-compile
1963
1964   ;; This little mapcar goes through the list below and marks the
1965   ;; symbols in question as autoloaded functions.
1966   (mapcar
1967    (lambda (package)
1968      (let ((interactive (nth 1 (memq ':interactive package))))
1969        (mapcar
1970         (lambda (function)
1971           (let (keymap)
1972             (when (consp function)
1973               (setq keymap (car (memq 'keymap function)))
1974               (setq function (car function)))
1975             (autoload function (car package) nil interactive keymap)))
1976         (if (eq (nth 1 package) ':interactive)
1977             (cdddr package)
1978           (cdr package)))))
1979    '(("metamail" metamail-buffer)
1980      ("info" Info-goto-node)
1981      ("hexl" hexl-hex-string-to-integer)
1982      ("pp" pp pp-to-string pp-eval-expression)
1983      ("mail-extr" mail-extract-address-components)
1984      ("nnmail" nnmail-split-fancy nnmail-article-group)
1985      ("nnvirtual" nnvirtual-catchup-group)
1986      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1987       timezone-make-sortable-date timezone-make-time-string)
1988      ("sendmail" mail-position-on-field mail-setup)
1989      ("rmailout" rmail-output)
1990      ("rnewspost" news-mail-other-window news-reply-yank-original
1991       news-caesar-buffer-body)
1992      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1993       rmail-show-message)
1994      ("gnus-soup" :interactive t
1995       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1996       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1997      ("nnsoup" nnsoup-pack-replies)
1998      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1999       gnus-Folder-save-name gnus-folder-save-name)
2000      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2001      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2002       gnus-server-make-menu-bar gnus-article-make-menu-bar
2003       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2004       gnus-summary-highlight-line gnus-carpal-setup-buffer
2005       gnus-group-highlight-line
2006       gnus-article-add-button gnus-insert-next-page-button
2007       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2008      ("gnus-vis" :interactive t
2009       gnus-article-push-button gnus-article-press-button
2010       gnus-article-highlight gnus-article-highlight-some
2011       gnus-article-highlight-headers gnus-article-highlight-signature
2012       gnus-article-add-buttons gnus-article-add-buttons-to-head
2013       gnus-article-next-button gnus-article-prev-button)
2014      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2015       gnus-demon-add-disconnection gnus-demon-add-handler
2016       gnus-demon-remove-handler)
2017      ("gnus-demon" :interactive t
2018       gnus-demon-init gnus-demon-cancel)
2019      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2020       gnus-tree-open gnus-tree-close)
2021      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2022       gnus-nocem-unwanted-article-p)
2023      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2024      ("gnus-srvr" gnus-browse-foreign-server)
2025      ("gnus-cite" :interactive t
2026       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2027       gnus-article-hide-citation gnus-article-fill-cited-article)
2028      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2029       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2030       gnus-execute gnus-expunge)
2031      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2032       gnus-cache-possibly-remove-articles gnus-cache-request-article
2033       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2034       gnus-cache-enter-remove-article gnus-cached-article-p
2035       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2036      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2037       gnus-cache-remove-article)
2038      ("gnus-score" :interactive t
2039       gnus-summary-increase-score gnus-summary-lower-score
2040       gnus-score-flush-cache gnus-score-close
2041       gnus-score-raise-same-subject-and-select
2042       gnus-score-raise-same-subject gnus-score-default
2043       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2044       gnus-score-lower-same-subject gnus-score-lower-thread
2045       gnus-possibly-score-headers)
2046      ("gnus-score"
2047       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2048       gnus-current-score-file-nondirectory gnus-score-adaptive
2049       gnus-score-find-trace gnus-score-file-name)
2050      ("gnus-edit" :interactive t gnus-score-customize)
2051      ("gnus-topic" :interactive t gnus-topic-mode)
2052      ("gnus-topic" gnus-topic-remove-group)
2053      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2054      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2055      ("gnus-uu" :interactive t
2056       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2057       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2058       gnus-uu-mark-by-regexp gnus-uu-mark-all
2059       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2060       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2061       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2062       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2063       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2064       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2065       gnus-uu-decode-binhex-view)
2066      ("gnus-msg" (gnus-summary-send-map keymap)
2067       gnus-mail-yank-original gnus-mail-send-and-exit
2068       gnus-sendmail-setup-mail gnus-article-mail
2069       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2070      ("gnus-msg" :interactive t
2071       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2072       gnus-summary-followup gnus-summary-followup-with-original
2073       gnus-summary-followup-and-reply
2074       gnus-summary-followup-and-reply-with-original
2075       gnus-summary-cancel-article gnus-summary-supersede-article
2076       gnus-post-news gnus-inews-news gnus-cancel-news
2077       gnus-summary-reply gnus-summary-reply-with-original
2078       gnus-summary-mail-forward gnus-summary-mail-other-window
2079       gnus-bug)
2080      ("gnus-picon" :interactive t gnus-article-display-picons
2081       gnus-group-display-picons gnus-picons-article-display-x-face)
2082      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2083       gnus-grouplens-mode)
2084      ("gnus-vm" gnus-vm-mail-setup)
2085      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2086       gnus-summary-save-article-vm gnus-yank-article))))
2087
2088 \f
2089
2090 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2091 ;; If you want the cursor to go somewhere else, set these two
2092 ;; functions in some startup hook to whatever you want.
2093 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2094 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2095
2096 ;;; Various macros and substs.
2097
2098 (defun gnus-header-from (header)
2099   (mail-header-from header))
2100
2101 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2102   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2103   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2104     `(let ((,tempvar (selected-window)))
2105        (unwind-protect
2106            (progn
2107              (pop-to-buffer ,buffer)
2108              ,@forms)
2109          (select-window ,tempvar)))))
2110
2111 (defmacro gnus-gethash (string hashtable)
2112   "Get hash value of STRING in HASHTABLE."
2113   `(symbol-value (intern-soft ,string ,hashtable)))
2114
2115 (defmacro gnus-sethash (string value hashtable)
2116   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2117   `(set (intern ,string ,hashtable) ,value))
2118
2119 (defmacro gnus-intern-safe (string hashtable)
2120   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2121   `(let ((symbol (intern ,string ,hashtable)))
2122      (or (boundp symbol)
2123          (set symbol nil))
2124      symbol))
2125
2126 (defmacro gnus-group-unread (group)
2127   "Get the currently computed number of unread articles in GROUP."
2128   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2129
2130 (defmacro gnus-group-entry (group)
2131   "Get the newsrc entry for GROUP."
2132   `(gnus-gethash ,group gnus-newsrc-hashtb))
2133
2134 (defmacro gnus-active (group)
2135   "Get active info on GROUP."
2136   `(gnus-gethash ,group gnus-active-hashtb))
2137
2138 (defmacro gnus-set-active (group active)
2139   "Set GROUP's active info."
2140   `(gnus-sethash ,group ,active gnus-active-hashtb))
2141
2142 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2143 ;;   function `substring' might cut on a middle of multi-octet
2144 ;;   character.
2145 (defun gnus-truncate-string (str width)
2146   (substring str 0 width))
2147
2148 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2149 ;; to limit the length of a string.  This function is necessary since
2150 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2151 (defsubst gnus-limit-string (str width)
2152   (if (> (length str) width)
2153       (substring str 0 width)
2154     str))
2155
2156 (defsubst gnus-simplify-subject-re (subject)
2157   "Remove \"Re:\" from subject lines."
2158   (if (string-match "^[Rr][Ee]: *" subject)
2159       (substring subject (match-end 0))
2160     subject))
2161
2162 (defsubst gnus-goto-char (point)
2163   (and point (goto-char point)))
2164
2165 (defmacro gnus-buffer-exists-p (buffer)
2166   `(let ((buffer ,buffer))
2167      (and buffer
2168           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2169                    buffer))))
2170
2171 (defmacro gnus-kill-buffer (buffer)
2172   `(let ((buf ,buffer))
2173      (if (gnus-buffer-exists-p buf)
2174          (kill-buffer buf))))
2175
2176 (defsubst gnus-point-at-bol ()
2177   "Return point at the beginning of the line."
2178   (let ((p (point)))
2179     (beginning-of-line)
2180     (prog1
2181         (point)
2182       (goto-char p))))
2183
2184 (defsubst gnus-point-at-eol ()
2185   "Return point at the end of the line."
2186   (let ((p (point)))
2187     (end-of-line)
2188     (prog1
2189         (point)
2190       (goto-char p))))
2191
2192 (defun gnus-alive-p ()
2193   "Say whether Gnus is running or not."
2194   (and gnus-group-buffer
2195        (get-buffer gnus-group-buffer)))
2196
2197 ;; Delete the current line (and the next N lines.);
2198 (defmacro gnus-delete-line (&optional n)
2199   `(delete-region (progn (beginning-of-line) (point))
2200                   (progn (forward-line ,(or n 1)) (point))))
2201
2202 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2203 (defvar gnus-init-inhibit nil)
2204 (defun gnus-read-init-file (&optional inhibit-next)
2205   (if gnus-init-inhibit
2206       (setq gnus-init-inhibit nil)
2207     (setq gnus-init-inhibit inhibit-next)
2208     (and gnus-init-file
2209          (or (and (file-exists-p gnus-init-file)
2210                   ;; Don't try to load a directory.
2211                   (not (file-directory-p gnus-init-file)))
2212              (file-exists-p (concat gnus-init-file ".el"))
2213              (file-exists-p (concat gnus-init-file ".elc")))
2214          (condition-case var
2215              (load gnus-init-file nil t)
2216            (error
2217             (error "Error in %s: %s" gnus-init-file var))))))
2218
2219 ;; Info access macros.
2220
2221 (defmacro gnus-info-group (info)
2222   `(nth 0 ,info))
2223 (defmacro gnus-info-rank (info)
2224   `(nth 1 ,info))
2225 (defmacro gnus-info-read (info)
2226   `(nth 2 ,info))
2227 (defmacro gnus-info-marks (info)
2228   `(nth 3 ,info))
2229 (defmacro gnus-info-method (info)
2230   `(nth 4 ,info))
2231 (defmacro gnus-info-params (info)
2232   `(nth 5 ,info))
2233
2234 (defmacro gnus-info-level (info)
2235   `(let ((rank (gnus-info-rank ,info)))
2236      (if (consp rank)
2237          (car rank)
2238        rank)))
2239 (defmacro gnus-info-score (info)
2240   `(let ((rank (gnus-info-rank ,info)))
2241      (or (and (consp rank) (cdr rank)) 0)))
2242
2243 (defmacro gnus-info-set-group (info group)
2244   `(setcar ,info ,group))
2245 (defmacro gnus-info-set-rank (info rank)
2246   `(setcar (nthcdr 1 ,info) ,rank))
2247 (defmacro gnus-info-set-read (info read)
2248   `(setcar (nthcdr 2 ,info) ,read))
2249 (defmacro gnus-info-set-marks (info marks)
2250   `(setcar (nthcdr 3 ,info) ,marks))
2251 (defmacro gnus-info-set-method (info method)
2252   `(setcar (nthcdr 4 ,info) ,method))
2253 (defmacro gnus-info-set-params (info params)
2254   `(setcar (nthcdr 5 ,info) ,params))
2255
2256 (defmacro gnus-info-set-level (info level)
2257   `(let ((rank (cdr ,info)))
2258      (if (consp (car rank))
2259          (setcar (car rank) ,level)
2260        (setcar rank ,level))))
2261 (defmacro gnus-info-set-score (info score)
2262   `(let ((rank (cdr ,info)))
2263      (if (consp (car rank))
2264          (setcdr (car rank) ,score)
2265        (setcar rank (cons (car rank) ,score)))))
2266
2267 (defmacro gnus-get-info (group)
2268   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2269
2270 (defun gnus-byte-code (func)
2271   "Return a form that can be `eval'ed based on FUNC."
2272   (let ((fval (symbol-function func)))
2273     (if (byte-code-function-p fval)
2274         (let ((flist (append fval nil)))
2275           (setcar flist 'byte-code)
2276           flist)
2277       (cons 'progn (cddr fval)))))
2278
2279 ;;; Load the compatability functions.
2280
2281 (require 'gnus-cus)
2282 (require 'gnus-ems)
2283
2284 \f
2285
2286 ;; Format specs.  The chunks below are the machine-generated forms
2287 ;; that are to be evaled as the result of the default format strings.
2288 ;; We write them in here to get them byte-compiled.  That way the
2289 ;; default actions will be quite fast, while still retaining the full
2290 ;; flexibility of the user-defined format specs.
2291
2292 ;; First we have lots of dummy defvars to let the compiler know these
2293 ;; are really dynamic variables.
2294
2295 (defvar gnus-tmp-unread)
2296 (defvar gnus-tmp-replied)
2297 (defvar gnus-tmp-score-char)
2298 (defvar gnus-tmp-indentation)
2299 (defvar gnus-tmp-opening-bracket)
2300 (defvar gnus-tmp-lines)
2301 (defvar gnus-tmp-name)
2302 (defvar gnus-tmp-closing-bracket)
2303 (defvar gnus-tmp-subject-or-nil)
2304 (defvar gnus-tmp-subject)
2305 (defvar gnus-tmp-marked)
2306 (defvar gnus-tmp-marked-mark)
2307 (defvar gnus-tmp-subscribed)
2308 (defvar gnus-tmp-process-marked)
2309 (defvar gnus-tmp-number-of-unread)
2310 (defvar gnus-tmp-group-name)
2311 (defvar gnus-tmp-group)
2312 (defvar gnus-tmp-article-number)
2313 (defvar gnus-tmp-unread-and-unselected)
2314 (defvar gnus-tmp-news-method)
2315 (defvar gnus-tmp-news-server)
2316 (defvar gnus-tmp-article-number)
2317 (defvar gnus-mouse-face)
2318 (defvar gnus-mouse-face-prop)
2319
2320 (defun gnus-summary-line-format-spec ()
2321   (insert gnus-tmp-unread gnus-tmp-replied
2322           gnus-tmp-score-char gnus-tmp-indentation)
2323   (put-text-property
2324    (point)
2325    (progn
2326      (insert
2327       gnus-tmp-opening-bracket
2328       (format "%4d: %-20s"
2329               gnus-tmp-lines
2330               (if (> (length gnus-tmp-name) 20)
2331                   (substring gnus-tmp-name 0 20)
2332                 gnus-tmp-name))
2333       gnus-tmp-closing-bracket)
2334      (point))
2335    gnus-mouse-face-prop gnus-mouse-face)
2336   (insert " " gnus-tmp-subject-or-nil "\n"))
2337
2338 (defvar gnus-summary-line-format-spec
2339   (gnus-byte-code 'gnus-summary-line-format-spec))
2340
2341 (defun gnus-summary-dummy-line-format-spec ()
2342   (insert "*  ")
2343   (put-text-property
2344    (point)
2345    (progn
2346      (insert ":                          :")
2347      (point))
2348    gnus-mouse-face-prop gnus-mouse-face)
2349   (insert " " gnus-tmp-subject "\n"))
2350
2351 (defvar gnus-summary-dummy-line-format-spec
2352   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2353
2354 (defun gnus-group-line-format-spec ()
2355   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2356           gnus-tmp-process-marked
2357           gnus-group-indentation
2358           (format "%5s: " gnus-tmp-number-of-unread))
2359   (put-text-property
2360    (point)
2361    (progn
2362      (insert gnus-tmp-group "\n")
2363      (1- (point)))
2364    gnus-mouse-face-prop gnus-mouse-face))
2365 (defvar gnus-group-line-format-spec
2366   (gnus-byte-code 'gnus-group-line-format-spec))
2367
2368 (defvar gnus-format-specs
2369   `((version . ,emacs-version)
2370     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2371     (summary-dummy ,gnus-summary-dummy-line-format
2372                    ,gnus-summary-dummy-line-format-spec)
2373     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2374
2375 (defvar gnus-article-mode-line-format-spec nil)
2376 (defvar gnus-summary-mode-line-format-spec nil)
2377 (defvar gnus-group-mode-line-format-spec nil)
2378
2379 ;;; Phew.  All that gruft is over, fortunately.
2380
2381 \f
2382 ;;;
2383 ;;; Gnus Utility Functions
2384 ;;;
2385
2386 (defun gnus-extract-address-components (from)
2387   (let (name address)
2388     ;; First find the address - the thing with the @ in it.  This may
2389     ;; not be accurate in mail addresses, but does the trick most of
2390     ;; the time in news messages.
2391     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2392         (setq address (substring from (match-beginning 0) (match-end 0))))
2393     ;; Then we check whether the "name <address>" format is used.
2394     (and address
2395          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2396          ;; Linear white space is not required.
2397          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2398          (and (setq name (substring from 0 (match-beginning 0)))
2399               ;; Strip any quotes from the name.
2400               (string-match "\".*\"" name)
2401               (setq name (substring name 1 (1- (match-end 0))))))
2402     ;; If not, then "address (name)" is used.
2403     (or name
2404         (and (string-match "(.+)" from)
2405              (setq name (substring from (1+ (match-beginning 0))
2406                                    (1- (match-end 0)))))
2407         (and (string-match "()" from)
2408              (setq name address))
2409         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2410         ;; XOVER might not support folded From headers.
2411         (and (string-match "(.*" from)
2412              (setq name (substring from (1+ (match-beginning 0))
2413                                    (match-end 0)))))
2414     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2415     (list (or name from) (or address from))))
2416
2417 (defun gnus-fetch-field (field)
2418   "Return the value of the header FIELD of current article."
2419   (save-excursion
2420     (save-restriction
2421       (let ((case-fold-search t))
2422         (nnheader-narrow-to-headers)
2423         (mail-fetch-field field)))))
2424
2425 (defun gnus-goto-colon ()
2426   (beginning-of-line)
2427   (search-forward ":" (gnus-point-at-eol) t))
2428
2429 ;;;###autoload
2430 (defun gnus-update-format (var)
2431   "Update the format specification near point."
2432   (interactive
2433    (list
2434     (save-excursion
2435       (eval-defun nil)
2436       ;; Find the end of the current word.
2437       (re-search-forward "[ \t\n]" nil t)
2438       ;; Search backward.
2439       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2440         (match-string 1)))))
2441   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2442                               (match-string 1 var))))
2443          (entry (assq type gnus-format-specs))
2444          value spec)
2445     (when entry
2446       (setq gnus-format-specs (delq entry gnus-format-specs)))
2447     (set
2448      (intern (format "%s-spec" var))
2449      (gnus-parse-format (setq value (symbol-value (intern var)))
2450                         (symbol-value (intern (format "%s-alist" var)))
2451                         (not (string-match "mode" var))))
2452     (setq spec (symbol-value (intern (format "%s-spec" var))))
2453     (push (list type value spec) gnus-format-specs)
2454
2455     (pop-to-buffer "*Gnus Format*")
2456     (erase-buffer)
2457     (lisp-interaction-mode)
2458     (insert (pp-to-string spec))))
2459
2460 (defun gnus-update-format-specifications (&optional force)
2461   "Update all (necessary) format specifications."
2462   ;; Make the indentation array.
2463   (gnus-make-thread-indent-array)
2464
2465   ;; See whether all the stored info needs to be flushed.
2466   (when (or force
2467             (not (equal emacs-version
2468                         (cdr (assq 'version gnus-format-specs)))))
2469     (setq gnus-format-specs nil))
2470
2471   ;; Go through all the formats and see whether they need updating.
2472   (let ((types '(summary summary-dummy group
2473                          summary-mode group-mode article-mode))
2474         new-format entry type val)
2475     (while (setq type (pop types))
2476       ;; Jump to the proper buffer to find out the value of
2477       ;; the variable, if possible.  (It may be buffer-local.)
2478       (save-excursion
2479         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2480               val)
2481           (when (and (boundp buffer)
2482                      (setq val (symbol-value buffer))
2483                      (get-buffer val)
2484                      (buffer-name (get-buffer val)))
2485             (set-buffer (get-buffer val)))
2486           (setq new-format (symbol-value
2487                             (intern (format "gnus-%s-line-format" type))))))
2488       (setq entry (cdr (assq type gnus-format-specs)))
2489       (if (and entry
2490                (equal (car entry) new-format))
2491           ;; Use the old format.
2492           (set (intern (format "gnus-%s-line-format-spec" type))
2493                (cadr entry))
2494         ;; This is a new format.
2495         (setq val
2496               (if (not (stringp new-format))
2497                   ;; This is a function call or something.
2498                   new-format
2499                 ;; This is a "real" format.
2500                 (gnus-parse-format
2501                  new-format
2502                  (symbol-value
2503                   (intern (format "gnus-%s-line-format-alist"
2504                                   (if (eq type 'article-mode)
2505                                       'summary-mode type))))
2506                  (not (string-match "mode$" (symbol-name type))))))
2507         ;; Enter the new format spec into the list.
2508         (if entry
2509             (progn
2510               (setcar (cdr entry) val)
2511               (setcar entry new-format))
2512           (push (list type new-format val) gnus-format-specs))
2513         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2514
2515   (gnus-update-group-mark-positions)
2516   (gnus-update-summary-mark-positions))
2517
2518 (defun gnus-update-summary-mark-positions ()
2519   "Compute where the summary marks are to go."
2520   (save-excursion
2521     (let ((gnus-replied-mark 129)
2522           (gnus-score-below-mark 130)
2523           (gnus-score-over-mark 130)
2524           (thread nil)
2525           (gnus-visual nil)
2526           pos)
2527       (gnus-set-work-buffer)
2528       (gnus-summary-insert-line
2529        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2530       (goto-char (point-min))
2531       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2532                                          (- (point) 2)))))
2533       (goto-char (point-min))
2534       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2535             pos)
2536       (goto-char (point-min))
2537       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2538             pos)
2539       (setq gnus-summary-mark-positions pos))))
2540
2541 (defun gnus-update-group-mark-positions ()
2542   (save-excursion
2543     (let ((gnus-process-mark 128)
2544           (gnus-group-marked '("dummy.group")))
2545       (gnus-set-active "dummy.group" '(0 . 0))
2546       (gnus-set-work-buffer)
2547       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2548       (goto-char (point-min))
2549       (setq gnus-group-mark-positions
2550             (list (cons 'process (and (search-forward "\200" nil t)
2551                                       (- (point) 2))))))))
2552
2553 (defvar gnus-mouse-face-0 'highlight)
2554 (defvar gnus-mouse-face-1 'highlight)
2555 (defvar gnus-mouse-face-2 'highlight)
2556 (defvar gnus-mouse-face-3 'highlight)
2557 (defvar gnus-mouse-face-4 'highlight)
2558
2559 (defun gnus-mouse-face-function (form type)
2560   `(put-text-property
2561     (point) (progn ,@form (point))
2562     gnus-mouse-face-prop
2563     ,(if (equal type 0)
2564          'gnus-mouse-face
2565        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2566
2567 (defvar gnus-face-0 'bold)
2568 (defvar gnus-face-1 'italic)
2569 (defvar gnus-face-2 'bold-italic)
2570 (defvar gnus-face-3 'bold)
2571 (defvar gnus-face-4 'bold)
2572
2573 (defun gnus-face-face-function (form type)
2574   `(put-text-property
2575     (point) (progn ,@form (point))
2576     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2577
2578 (defun gnus-max-width-function (el max-width)
2579   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2580   (if (symbolp el)
2581       `(if (> (length ,el) ,max-width)
2582            (substring ,el 0 ,max-width)
2583          ,el)
2584     `(let ((val (eval ,el)))
2585        (if (numberp val)
2586            (setq val (int-to-string val)))
2587        (if (> (length val) ,max-width)
2588            (substring val 0 ,max-width)
2589          val))))
2590
2591 (defun gnus-parse-format (format spec-alist &optional insert)
2592   ;; This function parses the FORMAT string with the help of the
2593   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2594   ;; string.  If the FORMAT string contains the specifiers %( and %)
2595   ;; the text between them will have the mouse-face text property.
2596   (if (string-match
2597        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2598        format)
2599       (gnus-parse-complex-format format spec-alist)
2600     ;; This is a simple format.
2601     (gnus-parse-simple-format format spec-alist insert)))
2602
2603 (defun gnus-parse-complex-format (format spec-alist)
2604   (save-excursion
2605     (gnus-set-work-buffer)
2606     (insert format)
2607     (goto-char (point-min))
2608     (while (re-search-forward "\"" nil t)
2609       (replace-match "\\\"" nil t))
2610     (goto-char (point-min))
2611     (insert "(\"")
2612     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2613       (let ((number (if (match-beginning 1)
2614                         (match-string 1) "0"))
2615             (delim (aref (match-string 2) 0)))
2616         (if (or (= delim ?\() (= delim ?\{))
2617             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2618                                    " " number " \""))
2619           (replace-match "\")\""))))
2620     (goto-char (point-max))
2621     (insert "\")")
2622     (goto-char (point-min))
2623     (let ((form (read (current-buffer))))
2624       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2625
2626 (defun gnus-complex-form-to-spec (form spec-alist)
2627   (delq nil
2628         (mapcar
2629          (lambda (sform)
2630            (if (stringp sform)
2631                (gnus-parse-simple-format sform spec-alist t)
2632              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2633                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2634                       (nth 1 sform))))
2635          form)))
2636
2637 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2638   ;; This function parses the FORMAT string with the help of the
2639   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2640   ;; string.
2641   (let ((max-width 0)
2642         spec flist fstring newspec elem beg result dontinsert)
2643     (save-excursion
2644       (gnus-set-work-buffer)
2645       (insert format)
2646       (goto-char (point-min))
2647       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2648                                 nil t)
2649         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2650               (setq newspec "%"
2651                     beg (1+ (match-beginning 0)))
2652           ;; First check if there are any specs that look anything like
2653           ;; "%12,12A", ie. with a "max width specification".  These have
2654           ;; to be treated specially.
2655           (if (setq beg (match-beginning 1))
2656               (setq max-width
2657                     (string-to-int
2658                      (buffer-substring
2659                       (1+ (match-beginning 1)) (match-end 1))))
2660             (setq max-width 0)
2661             (setq beg (match-beginning 2)))
2662           ;; Find the specification from `spec-alist'.
2663           (unless (setq elem (cdr (assq spec spec-alist)))
2664             (setq elem '("*" ?s)))
2665           ;; Treat user defined format specifiers specially.
2666           (when (eq (car elem) 'gnus-tmp-user-defined)
2667             (setq elem
2668                   (list
2669                    (list (intern (concat "gnus-user-format-function-"
2670                                          (match-string 3)))
2671                          'gnus-tmp-header) ?s))
2672             (delete-region (match-beginning 3) (match-end 3)))
2673           (if (not (zerop max-width))
2674               (let ((el (car elem)))
2675                 (cond ((= (cadr elem) ?c)
2676                        (setq el (list 'char-to-string el)))
2677                       ((= (cadr elem) ?d)
2678                        (setq el (list 'int-to-string el))))
2679                 (setq flist (cons (gnus-max-width-function el max-width)
2680                                   flist))
2681                 (setq newspec ?s))
2682             (progn
2683               (setq flist (cons (car elem) flist))
2684               (setq newspec (cadr elem)))))
2685         ;; Remove the old specification (and possibly a ",12" string).
2686         (delete-region beg (match-end 2))
2687         ;; Insert the new specification.
2688         (goto-char beg)
2689         (insert newspec))
2690       (setq fstring (buffer-substring 1 (point-max))))
2691     ;; Do some postprocessing to increase efficiency.
2692     (setq
2693      result
2694      (cond
2695       ;; Emptyness.
2696       ((string= fstring "")
2697        nil)
2698       ;; Not a format string.
2699       ((not (string-match "%" fstring))
2700        (list fstring))
2701       ;; A format string with just a single string spec.
2702       ((string= fstring "%s")
2703        (list (car flist)))
2704       ;; A single character.
2705       ((string= fstring "%c")
2706        (list (car flist)))
2707       ;; A single number.
2708       ((string= fstring "%d")
2709        (setq dontinsert)
2710        (if insert
2711            (list `(princ ,(car flist)))
2712          (list `(int-to-string ,(car flist)))))
2713       ;; Just lots of chars and strings.
2714       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2715        (nreverse flist))
2716       ;; A single string spec at the beginning of the spec.
2717       ((string-match "\\`%[sc][^%]+\\'" fstring)
2718        (list (car flist) (substring fstring 2)))
2719       ;; A single string spec in the middle of the spec.
2720       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2721        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2722       ;; A single string spec in the end of the spec.
2723       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2724        (list (match-string 1 fstring) (car flist)))
2725       ;; A more complex spec.
2726       (t
2727        (list (cons 'format (cons fstring (nreverse flist)))))))
2728
2729     (if insert
2730         (when result
2731           (if dontinsert
2732               result
2733             (cons 'insert result)))
2734       (cond ((stringp result)
2735              result)
2736             ((consp result)
2737              (cons 'concat result))
2738             (t "")))))
2739
2740 (defun gnus-eval-format (format &optional alist props)
2741   "Eval the format variable FORMAT, using ALIST.
2742 If PROPS, insert the result."
2743   (let ((form (gnus-parse-format format alist props)))
2744     (if props
2745         (add-text-properties (point) (progn (eval form) (point)) props)
2746       (eval form))))
2747
2748 (defun gnus-remove-text-with-property (prop)
2749   "Delete all text in the current buffer with text property PROP."
2750   (save-excursion
2751     (goto-char (point-min))
2752     (while (not (eobp))
2753       (while (get-text-property (point) prop)
2754         (delete-char 1))
2755       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2756
2757 (defun gnus-set-work-buffer ()
2758   (if (get-buffer gnus-work-buffer)
2759       (progn
2760         (set-buffer gnus-work-buffer)
2761         (erase-buffer))
2762     (set-buffer (get-buffer-create gnus-work-buffer))
2763     (kill-all-local-variables)
2764     (buffer-disable-undo (current-buffer))
2765     (gnus-add-current-to-buffer-list)))
2766
2767 ;; Article file names when saving.
2768
2769 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2770   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2771 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2772 Otherwise, it is like ~/News/news/group/num."
2773   (let ((default
2774           (expand-file-name
2775            (concat (if (gnus-use-long-file-name 'not-save)
2776                        (gnus-capitalize-newsgroup newsgroup)
2777                      (gnus-newsgroup-directory-form newsgroup))
2778                    "/" (int-to-string (mail-header-number headers)))
2779            (or gnus-article-save-directory "~/News"))))
2780     (if (and last-file
2781              (string-equal (file-name-directory default)
2782                            (file-name-directory last-file))
2783              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2784         default
2785       (or last-file default))))
2786
2787 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2788   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2789 If variable `gnus-use-long-file-name' is non-nil, it is
2790 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2791   (let ((default
2792           (expand-file-name
2793            (concat (if (gnus-use-long-file-name 'not-save)
2794                        newsgroup
2795                      (gnus-newsgroup-directory-form newsgroup))
2796                    "/" (int-to-string (mail-header-number headers)))
2797            (or gnus-article-save-directory "~/News"))))
2798     (if (and last-file
2799              (string-equal (file-name-directory default)
2800                            (file-name-directory last-file))
2801              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2802         default
2803       (or last-file default))))
2804
2805 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2806   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2807 If variable `gnus-use-long-file-name' is non-nil, it is
2808 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2809   (or last-file
2810       (expand-file-name
2811        (if (gnus-use-long-file-name 'not-save)
2812            (gnus-capitalize-newsgroup newsgroup)
2813          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2814        (or gnus-article-save-directory "~/News"))))
2815
2816 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2817   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2818 If variable `gnus-use-long-file-name' is non-nil, it is
2819 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2820   (or last-file
2821       (expand-file-name
2822        (if (gnus-use-long-file-name 'not-save)
2823            newsgroup
2824          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2825        (or gnus-article-save-directory "~/News"))))
2826
2827 ;; For subscribing new newsgroup
2828
2829 (defun gnus-subscribe-hierarchical-interactive (groups)
2830   (let ((groups (sort groups 'string<))
2831         prefixes prefix start ans group starts)
2832     (while groups
2833       (setq prefixes (list "^"))
2834       (while (and groups prefixes)
2835         (while (not (string-match (car prefixes) (car groups)))
2836           (setq prefixes (cdr prefixes)))
2837         (setq prefix (car prefixes))
2838         (setq start (1- (length prefix)))
2839         (if (and (string-match "[^\\.]\\." (car groups) start)
2840                  (cdr groups)
2841                  (setq prefix
2842                        (concat "^" (substring (car groups) 0 (match-end 0))))
2843                  (string-match prefix (cadr groups)))
2844             (progn
2845               (setq prefixes (cons prefix prefixes))
2846               (message "Descend hierarchy %s? ([y]nsq): "
2847                        (substring prefix 1 (1- (length prefix))))
2848               (setq ans (read-char))
2849               (cond ((= ans ?n)
2850                      (while (and groups
2851                                  (string-match prefix
2852                                                (setq group (car groups))))
2853                        (setq gnus-killed-list
2854                              (cons group gnus-killed-list))
2855                        (gnus-sethash group group gnus-killed-hashtb)
2856                        (setq groups (cdr groups)))
2857                      (setq starts (cdr starts)))
2858                     ((= ans ?s)
2859                      (while (and groups
2860                                  (string-match prefix
2861                                                (setq group (car groups))))
2862                        (gnus-sethash group group gnus-killed-hashtb)
2863                        (gnus-subscribe-alphabetically (car groups))
2864                        (setq groups (cdr groups)))
2865                      (setq starts (cdr starts)))
2866                     ((= ans ?q)
2867                      (while groups
2868                        (setq group (car groups))
2869                        (setq gnus-killed-list (cons group gnus-killed-list))
2870                        (gnus-sethash group group gnus-killed-hashtb)
2871                        (setq groups (cdr groups))))
2872                     (t nil)))
2873           (message "Subscribe %s? ([n]yq)" (car groups))
2874           (setq ans (read-char))
2875           (setq group (car groups))
2876           (cond ((= ans ?y)
2877                  (gnus-subscribe-alphabetically (car groups))
2878                  (gnus-sethash group group gnus-killed-hashtb))
2879                 ((= ans ?q)
2880                  (while groups
2881                    (setq group (car groups))
2882                    (setq gnus-killed-list (cons group gnus-killed-list))
2883                    (gnus-sethash group group gnus-killed-hashtb)
2884                    (setq groups (cdr groups))))
2885                 (t
2886                  (setq gnus-killed-list (cons group gnus-killed-list))
2887                  (gnus-sethash group group gnus-killed-hashtb)))
2888           (setq groups (cdr groups)))))))
2889
2890 (defun gnus-subscribe-randomly (newsgroup)
2891   "Subscribe new NEWSGROUP by making it the first newsgroup."
2892   (gnus-subscribe-newsgroup newsgroup))
2893
2894 (defun gnus-subscribe-alphabetically (newgroup)
2895   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2896   (let ((groups (cdr gnus-newsrc-alist))
2897         before)
2898     (while (and (not before) groups)
2899       (if (string< newgroup (caar groups))
2900           (setq before (caar groups))
2901         (setq groups (cdr groups))))
2902     (gnus-subscribe-newsgroup newgroup before)))
2903
2904 (defun gnus-subscribe-hierarchically (newgroup)
2905   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2906   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2907   (save-excursion
2908     (set-buffer (find-file-noselect gnus-current-startup-file))
2909     (let ((groupkey newgroup)
2910           before)
2911       (while (and (not before) groupkey)
2912         (goto-char (point-min))
2913         (let ((groupkey-re
2914                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2915           (while (and (re-search-forward groupkey-re nil t)
2916                       (progn
2917                         (setq before (match-string 1))
2918                         (string< before newgroup)))))
2919         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2920         (setq groupkey
2921               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2922                   (substring groupkey (match-beginning 1) (match-end 1)))))
2923       (gnus-subscribe-newsgroup newgroup before))))
2924
2925 (defun gnus-subscribe-interactively (group)
2926   "Subscribe the new GROUP interactively.
2927 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2928 it is killed."
2929   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2930       (gnus-subscribe-hierarchically group)
2931     (push group gnus-killed-list)))
2932
2933 (defun gnus-subscribe-zombies (group)
2934   "Make the new GROUP into a zombie group."
2935   (push group gnus-zombie-list))
2936
2937 (defun gnus-subscribe-killed (group)
2938   "Make the new GROUP a killed group."
2939   (push group gnus-killed-list))
2940
2941 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2942   "Subscribe new NEWSGROUP.
2943 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2944 the first newsgroup."
2945   ;; We subscribe the group by changing its level to `subscribed'.
2946   (gnus-group-change-level
2947    newsgroup gnus-level-default-subscribed
2948    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2949   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2950
2951 ;; For directories
2952
2953 (defun gnus-newsgroup-directory-form (newsgroup)
2954   "Make hierarchical directory name from NEWSGROUP name."
2955   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2956         (len (length newsgroup))
2957         idx)
2958     ;; If this is a foreign group, we don't want to translate the
2959     ;; entire name.
2960     (if (setq idx (string-match ":" newsgroup))
2961         (aset newsgroup idx ?/)
2962       (setq idx 0))
2963     ;; Replace all occurrences of `.' with `/'.
2964     (while (< idx len)
2965       (if (= (aref newsgroup idx) ?.)
2966           (aset newsgroup idx ?/))
2967       (setq idx (1+ idx)))
2968     newsgroup))
2969
2970 (defun gnus-newsgroup-savable-name (group)
2971   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2972   ;; with dots.
2973   (nnheader-replace-chars-in-string group ?/ ?.))
2974
2975 (defun gnus-make-directory (dir)
2976   "Make DIRECTORY recursively."
2977   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2978   ;; of the many mysteries of the universe.
2979   (let* ((dir (expand-file-name dir default-directory))
2980          dirs err)
2981     (if (string-match "/$" dir)
2982         (setq dir (substring dir 0 (match-beginning 0))))
2983     ;; First go down the path until we find a directory that exists.
2984     (while (not (file-exists-p dir))
2985       (setq dirs (cons dir dirs))
2986       (string-match "/[^/]+$" dir)
2987       (setq dir (substring dir 0 (match-beginning 0))))
2988     ;; Then create all the subdirs.
2989     (while (and dirs (not err))
2990       (condition-case ()
2991           (make-directory (car dirs))
2992         (error (setq err t)))
2993       (setq dirs (cdr dirs)))
2994     ;; We return whether we were successful or not.
2995     (not dirs)))
2996
2997 (defun gnus-capitalize-newsgroup (newsgroup)
2998   "Capitalize NEWSGROUP name."
2999   (and (not (zerop (length newsgroup)))
3000        (concat (char-to-string (upcase (aref newsgroup 0)))
3001                (substring newsgroup 1))))
3002
3003 ;; Various... things.
3004
3005 (defun gnus-simplify-subject (subject &optional re-only)
3006   "Remove `Re:' and words in parentheses.
3007 If RE-ONLY is non-nil, strip leading `Re:'s only."
3008   (let ((case-fold-search t))           ;Ignore case.
3009     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3010     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3011       (setq subject (substring subject (match-end 0))))
3012     ;; Remove uninteresting prefixes.
3013     (if (and (not re-only)
3014              gnus-simplify-ignored-prefixes
3015              (string-match gnus-simplify-ignored-prefixes subject))
3016         (setq subject (substring subject (match-end 0))))
3017     ;; Remove words in parentheses from end.
3018     (unless re-only
3019       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3020         (setq subject (substring subject 0 (match-beginning 0)))))
3021     ;; Return subject string.
3022     subject))
3023
3024 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3025 ;; all whitespace.
3026 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3027 (defun gnus-simplify-buffer-fuzzy ()
3028   (goto-char (point-min))
3029   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
3030   (goto-char (match-beginning 0))
3031   (while (or
3032           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
3033           (looking-at "^[[].*:[ \t].*[]]$"))
3034     (goto-char (point-min))
3035     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
3036                               nil t)
3037       (replace-match "" t t))
3038     (goto-char (point-min))
3039     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
3040       (goto-char (match-end 0))
3041       (delete-char -1)
3042       (delete-region
3043        (progn (goto-char (match-beginning 0)))
3044        (re-search-forward ":"))))
3045   (goto-char (point-min))
3046   (while (re-search-forward "[ \t\n]*[[{(][^()\n]*[]})][ \t]*$" nil t)
3047     (replace-match "" t t))
3048   (goto-char (point-min))
3049   (while (re-search-forward "[ \t]+" nil t)
3050     (replace-match " " t t))
3051   (goto-char (point-min))
3052   (while (re-search-forward "[ \t]+$" nil t)
3053     (replace-match "" t t))
3054   (goto-char (point-min))
3055   (while (re-search-forward "^[ \t]+" nil t)
3056     (replace-match "" t t))
3057   (goto-char (point-min))
3058   (if gnus-simplify-subject-fuzzy-regexp
3059       (if (listp gnus-simplify-subject-fuzzy-regexp)
3060           (let ((list gnus-simplify-subject-fuzzy-regexp))
3061             (while list
3062               (goto-char (point-min))
3063               (while (re-search-forward (car list) nil t)
3064                 (replace-match "" t t))
3065               (setq list (cdr list))))
3066         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3067           (replace-match "" t t)))))
3068
3069 (defun gnus-simplify-subject-fuzzy (subject)
3070   "Siplify a subject string fuzzily."
3071   (save-excursion
3072     (gnus-set-work-buffer)
3073     (let ((case-fold-search t))
3074       (insert subject)
3075       (inline (gnus-simplify-buffer-fuzzy))
3076       (buffer-string))))
3077
3078 ;; Add the current buffer to the list of buffers to be killed on exit.
3079 (defun gnus-add-current-to-buffer-list ()
3080   (or (memq (current-buffer) gnus-buffer-list)
3081       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3082
3083 (defun gnus-string> (s1 s2)
3084   (not (or (string< s1 s2)
3085            (string= s1 s2))))
3086
3087 ;;; General various misc type functions.
3088
3089 (defun gnus-clear-system ()
3090   "Clear all variables and buffers."
3091   ;; Clear Gnus variables.
3092   (let ((variables gnus-variable-list))
3093     (while variables
3094       (set (car variables) nil)
3095       (setq variables (cdr variables))))
3096   ;; Clear other internal variables.
3097   (setq gnus-list-of-killed-groups nil
3098         gnus-have-read-active-file nil
3099         gnus-newsrc-alist nil
3100         gnus-newsrc-hashtb nil
3101         gnus-killed-list nil
3102         gnus-zombie-list nil
3103         gnus-killed-hashtb nil
3104         gnus-active-hashtb nil
3105         gnus-moderated-list nil
3106         gnus-description-hashtb nil
3107         gnus-newsgroup-headers nil
3108         gnus-newsgroup-name nil
3109         gnus-server-alist nil
3110         gnus-group-list-mode nil
3111         gnus-topic-active-topology nil
3112         gnus-topic-active-alist nil
3113         gnus-opened-servers nil
3114         gnus-current-select-method nil)
3115   ;; Reset any score variables.
3116   (when gnus-use-scoring 
3117     (gnus-score-close))
3118   ;; Kill the startup file.
3119   (and gnus-current-startup-file
3120        (get-file-buffer gnus-current-startup-file)
3121        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3122   ;; Save any cache buffers.
3123   (when gnus-use-cache 
3124     (gnus-cache-save-buffers))
3125   ;; Clear the dribble buffer.
3126   (gnus-dribble-clear)
3127   ;; Close down NoCeM.
3128   (when gnus-use-nocem 
3129     (gnus-nocem-close))
3130   ;; Shut down the demons.
3131   (when gnus-use-demon
3132     (gnus-demon-cancel))
3133   ;; Kill global KILL file buffer.
3134   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3135     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3136   (gnus-kill-buffer nntp-server-buffer)
3137   ;; Backlog.
3138   (when gnus-keep-backlog
3139     (gnus-backlog-shutdown))
3140   ;; Kill Gnus buffers.
3141   (while gnus-buffer-list
3142     (gnus-kill-buffer (pop gnus-buffer-list)))
3143   ;; Remove Gnus frames.
3144   (while gnus-created-frames
3145     (when (frame-live-p (car gnus-created-frames))
3146       ;; We slap a condition-case around this `delete-frame' to ensure 
3147       ;; agains errors if we try do delete the single frame that's left.
3148       (condition-case ()
3149           (delete-frame (car gnus-created-frames))
3150         (error nil)))
3151     (pop gnus-created-frames)))
3152
3153 (defun gnus-windows-old-to-new (setting)
3154   ;; First we take care of the really, really old Gnus 3 actions.
3155   (when (symbolp setting)
3156     (setq setting
3157           ;; Take care of ooold GNUS 3.x values.
3158           (cond ((eq setting 'SelectArticle) 'article)
3159                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3160                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3161                 (t setting))))
3162   (if (or (listp setting)
3163           (not (and gnus-window-configuration
3164                     (memq setting '(group summary article)))))
3165       setting
3166     (let* ((setting (if (eq setting 'group)
3167                         (if (assq 'newsgroup gnus-window-configuration)
3168                             'newsgroup
3169                           'newsgroups) setting))
3170            (elem (cadr (assq setting gnus-window-configuration)))
3171            (total (apply '+ elem))
3172            (types '(group summary article))
3173            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3174            (i 0)
3175            perc
3176            out)
3177       (while (< i 3)
3178         (or (not (numberp (nth i elem)))
3179             (zerop (nth i elem))
3180             (progn
3181               (setq perc  (/ (float (nth 0 elem)) total))
3182               (setq out (cons (if (eq pbuf (nth i types))
3183                                   (vector (nth i types) perc 'point)
3184                                 (vector (nth i types) perc))
3185                               out))))
3186         (setq i (1+ i)))
3187       (list (nreverse out)))))
3188
3189 ;;;###autoload
3190 (defun gnus-add-configuration (conf)
3191   "Add the window configuration CONF to `gnus-buffer-configuration'."
3192   (setq gnus-buffer-configuration
3193         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3194                          gnus-buffer-configuration))))
3195
3196 (defvar gnus-frame-list nil)
3197
3198 (defun gnus-configure-frame (split &optional window)
3199   "Split WINDOW according to SPLIT."
3200   (unless window
3201     (setq window (get-buffer-window (current-buffer))))
3202   (select-window window)
3203   ;; This might be an old-stylee buffer config.
3204   (when (vectorp split)
3205     (setq split (append split nil)))
3206   (when (or (consp (car split))
3207             (vectorp (car split)))
3208     (push 1.0 split)
3209     (push 'vertical split))
3210   ;; The SPLIT might be something that is to be evaled to
3211   ;; return a new SPLIT.
3212   (while (and (not (assq (car split) gnus-window-to-buffer))
3213               (gnus-functionp (car split)))
3214     (setq split (eval split)))
3215   (let* ((type (car split))
3216          (subs (cddr split))
3217          (len (if (eq type 'horizontal) (window-width) (window-height)))
3218          (total 0)
3219          (window-min-width (or gnus-window-min-width window-min-width))
3220          (window-min-height (or gnus-window-min-height window-min-height))
3221          s result new-win rest comp-subs size sub)
3222     (cond
3223      ;; Nothing to do here.
3224      ((null split))
3225      ;; Don't switch buffers.
3226      ((null type)
3227       (and (memq 'point split) window))
3228      ;; This is a buffer to be selected.
3229      ((not (memq type '(frame horizontal vertical)))
3230       (let ((buffer (cond ((stringp type) type)
3231                           (t (cdr (assq type gnus-window-to-buffer)))))
3232             buf)
3233         (unless buffer
3234           (error "Illegal buffer type: %s" type))
3235         (unless (setq buf (get-buffer (if (symbolp buffer)
3236                                           (symbol-value buffer) buffer)))
3237           (setq buf (get-buffer-create (if (symbolp buffer)
3238                                            (symbol-value buffer) buffer))))
3239         (switch-to-buffer buf)
3240         ;; We return the window if it has the `point' spec.
3241         (and (memq 'point split) window)))
3242      ;; This is a frame split.
3243      ((eq type 'frame)
3244       (unless gnus-frame-list
3245         (setq gnus-frame-list (list (window-frame
3246                                      (get-buffer-window (current-buffer))))))
3247       (let ((i 0)
3248             params frame fresult)
3249         (while (< i (length subs))
3250           ;; Frame parameter is gotten from the sub-split.
3251           (setq params (cadr (elt subs i)))
3252           ;; It should be a list.
3253           (unless (listp params)
3254             (setq params nil))
3255           ;; Create a new frame?
3256           (unless (setq frame (elt gnus-frame-list i))
3257             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3258             (push frame gnus-created-frames))
3259           ;; Is the old frame still alive?
3260           (unless (frame-live-p frame)
3261             (setcar (nthcdr i gnus-frame-list)
3262                     (setq frame (make-frame params))))
3263           ;; Select the frame in question and do more splits there.
3264           (select-frame frame)
3265           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3266           (incf i))
3267         ;; Select the frame that has the selected buffer.
3268         (when fresult
3269           (select-frame (window-frame fresult)))))
3270      ;; This is a normal split.
3271      (t
3272       (when (> (length subs) 0)
3273         ;; First we have to compute the sizes of all new windows.
3274         (while subs
3275           (setq sub (append (pop subs) nil))
3276           (while (and (not (assq (car sub) gnus-window-to-buffer))
3277                       (gnus-functionp (car sub)))
3278             (setq sub (eval sub)))
3279           (when sub
3280             (push sub comp-subs)
3281             (setq size (cadar comp-subs))
3282             (cond ((equal size 1.0)
3283                    (setq rest (car comp-subs))
3284                    (setq s 0))
3285                   ((floatp size)
3286                    (setq s (floor (* size len))))
3287                   ((integerp size)
3288                    (setq s size))
3289                   (t
3290                    (error "Illegal size: %s" size)))
3291             ;; Try to make sure that we are inside the safe limits.
3292             (cond ((zerop s))
3293                   ((eq type 'horizontal)
3294                    (setq s (max s window-min-width)))
3295                   ((eq type 'vertical)
3296                    (setq s (max s window-min-height))))
3297             (setcar (cdar comp-subs) s)
3298             (incf total s)))
3299         ;; Take care of the "1.0" spec.
3300         (if rest
3301             (setcar (cdr rest) (- len total))
3302           (error "No 1.0 specs in %s" split))
3303         ;; The we do the actual splitting in a nice recursive
3304         ;; fashion.
3305         (setq comp-subs (nreverse comp-subs))
3306         (while comp-subs
3307           (if (null (cdr comp-subs))
3308               (setq new-win window)
3309             (setq new-win
3310                   (split-window window (cadar comp-subs)
3311                                 (eq type 'horizontal))))
3312           (setq result (or (gnus-configure-frame
3313                             (car comp-subs) window) result))
3314           (select-window new-win)
3315           (setq window new-win)
3316           (setq comp-subs (cdr comp-subs))))
3317       ;; Return the proper window, if any.
3318       (when result
3319         (select-window result))))))
3320
3321 (defvar gnus-frame-split-p nil)
3322
3323 (defun gnus-configure-windows (setting &optional force)
3324   (setq setting (gnus-windows-old-to-new setting))
3325   (let ((split (if (symbolp setting)
3326                    (cadr (assq setting gnus-buffer-configuration))
3327                  setting))
3328         all-visible)
3329
3330     (setq gnus-frame-split-p nil)
3331
3332     (unless split
3333       (error "No such setting: %s" setting))
3334
3335     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3336              (not force))
3337         ;; All the windows mentioned are already visible, so we just
3338         ;; put point in the assigned buffer, and do not touch the
3339         ;; winconf.
3340         (select-window all-visible)
3341
3342       ;; Either remove all windows or just remove all Gnus windows.
3343       (let ((frame (selected-frame)))
3344         (unwind-protect
3345             (if gnus-use-full-window
3346                 ;; We want to remove all other windows.
3347                 (if (not gnus-frame-split-p)
3348                     ;; This is not a `frame' split, so we ignore the
3349                     ;; other frames.  
3350                     (delete-other-windows)
3351                   ;; This is a `frame' split, so we delete all windows
3352                   ;; on all frames.
3353                   (mapcar 
3354                    (lambda (frame)
3355                      (unless (eq (cdr (assq 'minibuffer
3356                                             (frame-parameters frame)))
3357                                  'only)
3358                        (select-frame frame)
3359                        (delete-other-windows)))
3360                    (frame-list)))
3361               ;; Just remove some windows.
3362               (gnus-remove-some-windows)
3363               (switch-to-buffer nntp-server-buffer))
3364           (select-frame frame)))
3365
3366       (switch-to-buffer nntp-server-buffer)
3367       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3368
3369 (defun gnus-all-windows-visible-p (split)
3370   (when (vectorp split)
3371     (setq split (append split nil)))
3372   (when (or (consp (car split))
3373             (vectorp (car split)))
3374     (push 1.0 split)
3375     (push 'vertical split))
3376   ;; The SPLIT might be something that is to be evaled to
3377   ;; return a new SPLIT.
3378   (while (and (not (assq (car split) gnus-window-to-buffer))
3379               (gnus-functionp (car split)))
3380     (setq split (eval split)))
3381   (let* ((type (elt split 0)))
3382     (cond
3383      ((null split)
3384       t)
3385      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3386       (let ((buffer (cond ((stringp type) type)
3387                           (t (cdr (assq type gnus-window-to-buffer)))))
3388             win buf)
3389         (unless buffer
3390           (error "Illegal buffer type: %s" type))
3391         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3392                                       buffer)))
3393           (setq win (get-buffer-window buf t)))
3394         (when win
3395           (if (memq 'point split)
3396               win
3397             t))))
3398      (t
3399       (when (eq type 'frame)
3400         (setq gnus-frame-split-p t))
3401       (let ((n (mapcar 'gnus-all-windows-visible-p
3402                        (cddr split)))
3403             (win t))
3404         (while n
3405           (cond ((windowp (car n))
3406                  (setq win (car n)))
3407                 ((null (car n))
3408                  (setq win nil)))
3409           (setq n (cdr n)))
3410         win)))))
3411
3412 (defun gnus-window-top-edge (&optional window)
3413   (nth 1 (window-edges window)))
3414
3415 (defun gnus-remove-some-windows ()
3416   (let ((buffers gnus-window-to-buffer)
3417         buf bufs lowest-buf lowest)
3418     (save-excursion
3419       ;; Remove windows on all known Gnus buffers.
3420       (while buffers
3421         (setq buf (cdar buffers))
3422         (if (symbolp buf)
3423             (setq buf (and (boundp buf) (symbol-value buf))))
3424         (and buf
3425              (get-buffer-window buf)
3426              (progn
3427                (setq bufs (cons buf bufs))
3428                (pop-to-buffer buf)
3429                (if (or (not lowest)
3430                        (< (gnus-window-top-edge) lowest))
3431                    (progn
3432                      (setq lowest (gnus-window-top-edge))
3433                      (setq lowest-buf buf)))))
3434         (setq buffers (cdr buffers)))
3435       ;; Remove windows on *all* summary buffers.
3436       (walk-windows
3437        (lambda (win)
3438          (let ((buf (window-buffer win)))
3439            (if (string-match    "^\\*Summary" (buffer-name buf))
3440                (progn
3441                  (setq bufs (cons buf bufs))
3442                  (pop-to-buffer buf)
3443                  (if (or (not lowest)
3444                          (< (gnus-window-top-edge) lowest))
3445                      (progn
3446                        (setq lowest-buf buf)
3447                        (setq lowest (gnus-window-top-edge)))))))))
3448       (and lowest-buf
3449            (progn
3450              (pop-to-buffer lowest-buf)
3451              (switch-to-buffer nntp-server-buffer)))
3452       (while bufs
3453         (and (not (eq (car bufs) lowest-buf))
3454              (delete-windows-on (car bufs)))
3455         (setq bufs (cdr bufs))))))
3456
3457 (defun gnus-version ()
3458   "Version numbers of this version of Gnus."
3459   (interactive)
3460   (let ((methods gnus-valid-select-methods)
3461         (mess gnus-version)
3462         meth)
3463     ;; Go through all the legal select methods and add their version
3464     ;; numbers to the total version string.  Only the backends that are
3465     ;; currently in use will have their message numbers taken into
3466     ;; consideration.
3467     (while methods
3468       (setq meth (intern (concat (caar methods) "-version")))
3469       (and (boundp meth)
3470            (stringp (symbol-value meth))
3471            (setq mess (concat mess "; " (symbol-value meth))))
3472       (setq methods (cdr methods)))
3473     (gnus-message 2 mess)))
3474
3475 (defun gnus-info-find-node ()
3476   "Find Info documentation of Gnus."
3477   (interactive)
3478   ;; Enlarge info window if needed.
3479   (let ((mode major-mode)
3480         gnus-info-buffer)
3481     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3482     (setq gnus-info-buffer (current-buffer))
3483     (gnus-configure-windows 'info)))
3484
3485 (defun gnus-days-between (date1 date2)
3486   ;; Return the number of days between date1 and date2.
3487   (- (gnus-day-number date1) (gnus-day-number date2)))
3488
3489 (defun gnus-day-number (date)
3490   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3491                      (timezone-parse-date date))))
3492     (timezone-absolute-from-gregorian
3493      (nth 1 dat) (nth 2 dat) (car dat))))
3494
3495 (defun gnus-encode-date (date)
3496   "Convert DATE to internal time."
3497   (let* ((parse (timezone-parse-date date))
3498          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3499          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3500     (encode-time (caddr time) (cadr time) (car time)
3501                  (caddr date) (cadr date) (car date) (nth 4 date))))
3502
3503 (defun gnus-time-minus (t1 t2)
3504   "Subtract two internal times."
3505   (let ((borrow (< (cadr t1) (cadr t2))))
3506     (list (- (car t1) (car t2) (if borrow 1 0))
3507           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3508
3509 (defun gnus-file-newer-than (file date)
3510   (let ((fdate (nth 5 (file-attributes file))))
3511     (or (> (car fdate) (car date))
3512         (and (= (car fdate) (car date))
3513              (> (nth 1 fdate) (nth 1 date))))))
3514
3515 (defmacro gnus-local-set-keys (&rest plist)
3516   "Set the keys in PLIST in the current keymap."
3517   `(gnus-define-keys-1 (current-local-map) ',plist))
3518
3519 (defmacro gnus-define-keys (keymap &rest plist)
3520   "Define all keys in PLIST in KEYMAP."
3521   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3522
3523 (put 'gnus-define-keys 'lisp-indent-function 1)
3524 (put 'gnus-define-keys 'lisp-indent-hook 1)
3525 (put 'gnus-define-keymap 'lisp-indent-function 1)
3526 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3527
3528 (defmacro gnus-define-keymap (keymap &rest plist)
3529   "Define all keys in PLIST in KEYMAP."
3530   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3531
3532 (defun gnus-define-keys-1 (keymap plist)
3533   (when (null keymap)
3534     (error "Can't set keys in a null keymap"))
3535   (cond ((symbolp keymap)
3536          (setq keymap (symbol-value keymap)))
3537         ((keymapp keymap))
3538         ((listp keymap)
3539          (set (car keymap) nil)
3540          (define-prefix-command (car keymap))
3541          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3542          (setq keymap (symbol-value (car keymap)))))
3543   (let (key)
3544     (while plist
3545       (when (symbolp (setq key (pop plist)))
3546         (setq key (symbol-value key)))
3547       (define-key keymap key (pop plist)))))
3548
3549 (defun gnus-group-read-only-p (&optional group)
3550   "Check whether GROUP supports editing or not.
3551 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3552 that that variable is buffer-local to the summary buffers."
3553   (let ((group (or group gnus-newsgroup-name)))
3554     (not (gnus-check-backend-function 'request-replace-article group))))
3555
3556 (defun gnus-group-total-expirable-p (group)
3557   "Check whether GROUP is total-expirable or not."
3558   (let ((params (gnus-info-params (gnus-get-info group))))
3559     (or (memq 'total-expire params)
3560         (cdr (assq 'total-expire params)) ; (total-expire . t)
3561         (and gnus-total-expirable-newsgroups ; Check var.
3562              (string-match gnus-total-expirable-newsgroups group)))))
3563
3564 (defun gnus-group-auto-expirable-p (group)
3565   "Check whether GROUP is total-expirable or not."
3566   (let ((params (gnus-info-params (gnus-get-info group))))
3567     (or (memq 'auto-expire params)
3568         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3569         (and gnus-auto-expirable-newsgroups ; Check var.
3570              (string-match gnus-auto-expirable-newsgroups group)))))
3571
3572 (defun gnus-virtual-group-p (group)
3573   "Say whether GROUP is virtual or not."
3574   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3575                         gnus-valid-select-methods)))
3576
3577 (defsubst gnus-simplify-subject-fully (subject)
3578   "Simplify a subject string according to the user's wishes."
3579   (cond
3580    ((null gnus-summary-gather-subject-limit)
3581     (gnus-simplify-subject-re subject))
3582    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3583     (gnus-simplify-subject-fuzzy subject))
3584    ((numberp gnus-summary-gather-subject-limit)
3585     (gnus-limit-string (gnus-simplify-subject-re subject)
3586                        gnus-summary-gather-subject-limit))
3587    (t
3588     subject)))
3589
3590 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3591   "Check whether two subjects are equal.  If optional argument
3592 simple-first is t, first argument is already simplified."
3593   (cond
3594    ((null simple-first)
3595     (equal (gnus-simplify-subject-fully s1)
3596            (gnus-simplify-subject-fully s2)))
3597    (t
3598     (equal s1
3599            (gnus-simplify-subject-fully s2)))))
3600
3601 ;; Returns a list of writable groups.
3602 (defun gnus-writable-groups ()
3603   (let ((alist gnus-newsrc-alist)
3604         groups group)
3605     (while (setq group (car (pop alist)))
3606       (unless (gnus-group-read-only-p group)
3607         (push group groups)))
3608     (nreverse groups)))
3609
3610 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3611 ;; the echo area.
3612 (defun gnus-y-or-n-p (prompt)
3613   (prog1
3614       (y-or-n-p prompt)
3615     (message "")))
3616
3617 (defun gnus-yes-or-no-p (prompt)
3618   (prog1
3619       (yes-or-no-p prompt)
3620     (message "")))
3621
3622 ;; Check whether to use long file names.
3623 (defun gnus-use-long-file-name (symbol)
3624   ;; The variable has to be set...
3625   (and gnus-use-long-file-name
3626        ;; If it isn't a list, then we return t.
3627        (or (not (listp gnus-use-long-file-name))
3628            ;; If it is a list, and the list contains `symbol', we
3629            ;; return nil.
3630            (not (memq symbol gnus-use-long-file-name)))))
3631
3632 ;; I suspect there's a better way, but I haven't taken the time to do
3633 ;; it yet. -erik selberg@cs.washington.edu
3634 (defun gnus-dd-mmm (messy-date)
3635   "Return a string like DD-MMM from a big messy string"
3636   (let ((datevec (timezone-parse-date messy-date)))
3637     (format "%2s-%s"
3638             (condition-case ()
3639                 ;; Make sure leading zeroes are stripped.
3640                 (number-to-string (string-to-number (aref datevec 2)))
3641               (error "??"))
3642             (capitalize
3643              (or (car
3644                   (nth (1- (string-to-number (aref datevec 1)))
3645                        timezone-months-assoc))
3646                  "???")))))
3647
3648 ;; Make a hash table (default and minimum size is 255).
3649 ;; Optional argument HASHSIZE specifies the table size.
3650 (defun gnus-make-hashtable (&optional hashsize)
3651   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3652
3653 ;; Make a number that is suitable for hashing; bigger than MIN and one
3654 ;; less than 2^x.
3655 (defun gnus-create-hash-size (min)
3656   (let ((i 1))
3657     (while (< i min)
3658       (setq i (* 2 i)))
3659     (1- i)))
3660
3661 ;; Show message if message has a lower level than `gnus-verbose'.
3662 ;; Guideline for numbers:
3663 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3664 ;; for things that take a long time, 7 - not very important messages
3665 ;; on stuff, 9 - messages inside loops.
3666 (defun gnus-message (level &rest args)
3667   (if (<= level gnus-verbose)
3668       (apply 'message args)
3669     ;; We have to do this format thingy here even if the result isn't
3670     ;; shown - the return value has to be the same as the return value
3671     ;; from `message'.
3672     (apply 'format args)))
3673
3674 (defun gnus-functionp (form)
3675   "Return non-nil if FORM is funcallable."
3676   (or (and (symbolp form) (fboundp form))
3677       (and (listp form) (eq (car form) 'lambda))))
3678
3679 ;; Generate a unique new group name.
3680 (defun gnus-generate-new-group-name (leaf)
3681   (let ((name leaf)
3682         (num 0))
3683     (while (gnus-gethash name gnus-newsrc-hashtb)
3684       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3685     name))
3686
3687 ;; Find out whether the gnus-visual TYPE is wanted.
3688 (defun gnus-visual-p (&optional type class)
3689   (and gnus-visual                      ; Has to be non-nil, at least.
3690        (if (not type)                   ; We don't care about type.
3691            gnus-visual
3692          (if (listp gnus-visual)        ; It's a list, so we check it.
3693              (or (memq type gnus-visual)
3694                  (memq class gnus-visual))
3695            t))))
3696
3697 (defun gnus-parent-id (references)
3698   "Return the last Message-ID in REFERENCES."
3699   (when (and references
3700              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3701     (substring references (match-beginning 1) (match-end 1))))
3702
3703 (defun gnus-split-references (references)
3704   "Return a list of Message-IDs in REFERENCES."
3705   (let ((beg 0)
3706         ids)
3707     (while (string-match "<[^>]+>" references beg)
3708       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3709             ids))
3710     (nreverse ids)))
3711
3712 (defun gnus-ephemeral-group-p (group)
3713   "Say whether GROUP is ephemeral or not."
3714   (gnus-group-get-parameter group 'quit-config))
3715
3716 (defun gnus-group-quit-config (group)
3717   "Return the quit-config of GROUP."
3718   (gnus-group-get-parameter group 'quit-config))
3719
3720 (defun gnus-simplify-mode-line ()
3721   "Make mode lines a bit simpler."
3722   (setq mode-line-modified "-- ")
3723   (when (listp mode-line-format)
3724     (make-local-variable 'mode-line-format)
3725     (setq mode-line-format (copy-sequence mode-line-format))
3726     (when (equal (nth 3 mode-line-format) "   ")
3727       (setcar (nthcdr 3 mode-line-format) " "))))
3728
3729 ;;; List and range functions
3730
3731 (defun gnus-last-element (list)
3732   "Return last element of LIST."
3733   (while (cdr list)
3734     (setq list (cdr list)))
3735   (car list))
3736
3737 (defun gnus-copy-sequence (list)
3738   "Do a complete, total copy of a list."
3739   (if (and (consp list) (not (consp (cdr list))))
3740       (cons (car list) (cdr list))
3741     (mapcar (lambda (elem) (if (consp elem)
3742                                (if (consp (cdr elem))
3743                                    (gnus-copy-sequence elem)
3744                                  (cons (car elem) (cdr elem)))
3745                              elem))
3746             list)))
3747
3748 (defun gnus-set-difference (list1 list2)
3749   "Return a list of elements of LIST1 that do not appear in LIST2."
3750   (let ((list1 (copy-sequence list1)))
3751     (while list2
3752       (setq list1 (delq (car list2) list1))
3753       (setq list2 (cdr list2)))
3754     list1))
3755
3756 (defun gnus-sorted-complement (list1 list2)
3757   "Return a list of elements of LIST1 that do not appear in LIST2.
3758 Both lists have to be sorted over <."
3759   (let (out)
3760     (if (or (null list1) (null list2))
3761         (or list1 list2)
3762       (while (and list1 list2)
3763         (cond ((= (car list1) (car list2))
3764                (setq list1 (cdr list1)
3765                      list2 (cdr list2)))
3766               ((< (car list1) (car list2))
3767                (setq out (cons (car list1) out))
3768                (setq list1 (cdr list1)))
3769               (t
3770                (setq out (cons (car list2) out))
3771                (setq list2 (cdr list2)))))
3772       (nconc (nreverse out) (or list1 list2)))))
3773
3774 (defun gnus-intersection (list1 list2)
3775   (let ((result nil))
3776     (while list2
3777       (if (memq (car list2) list1)
3778           (setq result (cons (car list2) result)))
3779       (setq list2 (cdr list2)))
3780     result))
3781
3782 (defun gnus-sorted-intersection (list1 list2)
3783   ;; LIST1 and LIST2 have to be sorted over <.
3784   (let (out)
3785     (while (and list1 list2)
3786       (cond ((= (car list1) (car list2))
3787              (setq out (cons (car list1) out)
3788                    list1 (cdr list1)
3789                    list2 (cdr list2)))
3790             ((< (car list1) (car list2))
3791              (setq list1 (cdr list1)))
3792             (t
3793              (setq list2 (cdr list2)))))
3794     (nreverse out)))
3795
3796 (defun gnus-set-sorted-intersection (list1 list2)
3797   ;; LIST1 and LIST2 have to be sorted over <.
3798   ;; This function modifies LIST1.
3799   (let* ((top (cons nil list1))
3800          (prev top))
3801     (while (and list1 list2)
3802       (cond ((= (car list1) (car list2))
3803              (setq prev list1
3804                    list1 (cdr list1)
3805                    list2 (cdr list2)))
3806             ((< (car list1) (car list2))
3807              (setcdr prev (cdr list1))
3808              (setq list1 (cdr list1)))
3809             (t
3810              (setq list2 (cdr list2)))))
3811     (setcdr prev nil)
3812     (cdr top)))
3813
3814 (defun gnus-compress-sequence (numbers &optional always-list)
3815   "Convert list of numbers to a list of ranges or a single range.
3816 If ALWAYS-LIST is non-nil, this function will always release a list of
3817 ranges."
3818   (let* ((first (car numbers))
3819          (last (car numbers))
3820          result)
3821     (if (null numbers)
3822         nil
3823       (if (not (listp (cdr numbers)))
3824           numbers
3825         (while numbers
3826           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3827                 ((= (1+ last) (car numbers)) ;Still in sequence
3828                  (setq last (car numbers)))
3829                 (t                      ;End of one sequence
3830                  (setq result
3831                        (cons (if (= first last) first
3832                                (cons first last)) result))
3833                  (setq first (car numbers))
3834                  (setq last  (car numbers))))
3835           (setq numbers (cdr numbers)))
3836         (if (and (not always-list) (null result))
3837             (if (= first last) (list first) (cons first last))
3838           (nreverse (cons (if (= first last) first (cons first last))
3839                           result)))))))
3840
3841 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3842 (defun gnus-uncompress-range (ranges)
3843   "Expand a list of ranges into a list of numbers.
3844 RANGES is either a single range on the form `(num . num)' or a list of
3845 these ranges."
3846   (let (first last result)
3847     (cond
3848      ((null ranges)
3849       nil)
3850      ((not (listp (cdr ranges)))
3851       (setq first (car ranges))
3852       (setq last (cdr ranges))
3853       (while (<= first last)
3854         (setq result (cons first result))
3855         (setq first (1+ first)))
3856       (nreverse result))
3857      (t
3858       (while ranges
3859         (if (atom (car ranges))
3860             (if (numberp (car ranges))
3861                 (setq result (cons (car ranges) result)))
3862           (setq first (caar ranges))
3863           (setq last  (cdar ranges))
3864           (while (<= first last)
3865             (setq result (cons first result))
3866             (setq first (1+ first))))
3867         (setq ranges (cdr ranges)))
3868       (nreverse result)))))
3869
3870 (defun gnus-add-to-range (ranges list)
3871   "Return a list of ranges that has all articles from both RANGES and LIST.
3872 Note: LIST has to be sorted over `<'."
3873   (if (not ranges)
3874       (gnus-compress-sequence list t)
3875     (setq list (copy-sequence list))
3876     (or (listp (cdr ranges))
3877         (setq ranges (list ranges)))
3878     (let ((out ranges)
3879           ilist lowest highest temp)
3880       (while (and ranges list)
3881         (setq ilist list)
3882         (setq lowest (or (and (atom (car ranges)) (car ranges))
3883                          (caar ranges)))
3884         (while (and list (cdr list) (< (cadr list) lowest))
3885           (setq list (cdr list)))
3886         (if (< (car ilist) lowest)
3887             (progn
3888               (setq temp list)
3889               (setq list (cdr list))
3890               (setcdr temp nil)
3891               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3892         (setq highest (or (and (atom (car ranges)) (car ranges))
3893                           (cdar ranges)))
3894         (while (and list (<= (car list) highest))
3895           (setq list (cdr list)))
3896         (setq ranges (cdr ranges)))
3897       (if list
3898           (setq out (nconc (gnus-compress-sequence list t) out)))
3899       (setq out (sort out (lambda (r1 r2)
3900                             (< (or (and (atom r1) r1) (car r1))
3901                                (or (and (atom r2) r2) (car r2))))))
3902       (setq ranges out)
3903       (while ranges
3904         (if (atom (car ranges))
3905             (if (cdr ranges)
3906                 (if (atom (cadr ranges))
3907                     (if (= (1+ (car ranges)) (cadr ranges))
3908                         (progn
3909                           (setcar ranges (cons (car ranges)
3910                                                (cadr ranges)))
3911                           (setcdr ranges (cddr ranges))))
3912                   (if (= (1+ (car ranges)) (caadr ranges))
3913                       (progn
3914                         (setcar (cadr ranges) (car ranges))
3915                         (setcar ranges (cadr ranges))
3916                         (setcdr ranges (cddr ranges))))))
3917           (if (cdr ranges)
3918               (if (atom (cadr ranges))
3919                   (if (= (1+ (cdar ranges)) (cadr ranges))
3920                       (progn
3921                         (setcdr (car ranges) (cadr ranges))
3922                         (setcdr ranges (cddr ranges))))
3923                 (if (= (1+ (cdar ranges)) (caadr ranges))
3924                     (progn
3925                       (setcdr (car ranges) (cdadr ranges))
3926                       (setcdr ranges (cddr ranges)))))))
3927         (setq ranges (cdr ranges)))
3928       out)))
3929
3930 (defun gnus-remove-from-range (ranges list)
3931   "Return a list of ranges that has all articles from LIST removed from RANGES.
3932 Note: LIST has to be sorted over `<'."
3933   ;; !!! This function shouldn't look like this, but I've got a headache.
3934   (gnus-compress-sequence
3935    (gnus-sorted-complement
3936     (gnus-uncompress-range ranges) list)))
3937
3938 (defun gnus-member-of-range (number ranges)
3939   (if (not (listp (cdr ranges)))
3940       (and (>= number (car ranges))
3941            (<= number (cdr ranges)))
3942     (let ((not-stop t))
3943       (while (and ranges
3944                   (if (numberp (car ranges))
3945                       (>= number (car ranges))
3946                     (>= number (caar ranges)))
3947                   not-stop)
3948         (if (if (numberp (car ranges))
3949                 (= number (car ranges))
3950               (and (>= number (caar ranges))
3951                    (<= number (cdar ranges))))
3952             (setq not-stop nil))
3953         (setq ranges (cdr ranges)))
3954       (not not-stop))))
3955
3956 (defun gnus-range-length (range)
3957   "Return the length RANGE would have if uncompressed."
3958   (length (gnus-uncompress-range range)))
3959
3960 (defun gnus-sublist-p (list sublist)
3961   "Test whether all elements in SUBLIST are members of LIST."
3962   (let ((sublistp t))
3963     (while sublist
3964       (unless (memq (pop sublist) list)
3965         (setq sublistp nil
3966               sublist nil)))
3967     sublistp))
3968
3969 \f
3970 ;;;
3971 ;;; Gnus group mode
3972 ;;;
3973
3974 (defvar gnus-group-mode-map nil)
3975 (put 'gnus-group-mode 'mode-class 'special)
3976
3977 (unless gnus-group-mode-map
3978   (setq gnus-group-mode-map (make-keymap))
3979   (suppress-keymap gnus-group-mode-map)
3980
3981   (gnus-define-keys gnus-group-mode-map
3982     " " gnus-group-read-group
3983     "=" gnus-group-select-group
3984     "\r" gnus-group-select-group
3985     "\M-\r" gnus-group-quick-select-group
3986     "j" gnus-group-jump-to-group
3987     "n" gnus-group-next-unread-group
3988     "p" gnus-group-prev-unread-group
3989     "\177" gnus-group-prev-unread-group
3990     [delete] gnus-group-prev-unread-group
3991     "N" gnus-group-next-group
3992     "P" gnus-group-prev-group
3993     "\M-n" gnus-group-next-unread-group-same-level
3994     "\M-p" gnus-group-prev-unread-group-same-level
3995     "," gnus-group-best-unread-group
3996     "." gnus-group-first-unread-group
3997     "u" gnus-group-unsubscribe-current-group
3998     "U" gnus-group-unsubscribe-group
3999     "c" gnus-group-catchup-current
4000     "C" gnus-group-catchup-current-all
4001     "l" gnus-group-list-groups
4002     "L" gnus-group-list-all-groups
4003     "m" gnus-group-mail
4004     "g" gnus-group-get-new-news
4005     "\M-g" gnus-group-get-new-news-this-group
4006     "R" gnus-group-restart
4007     "r" gnus-group-read-init-file
4008     "B" gnus-group-browse-foreign-server
4009     "b" gnus-group-check-bogus-groups
4010     "F" gnus-find-new-newsgroups
4011     "\C-c\C-d" gnus-group-describe-group
4012     "\M-d" gnus-group-describe-all-groups
4013     "\C-c\C-a" gnus-group-apropos
4014     "\C-c\M-\C-a" gnus-group-description-apropos
4015     "a" gnus-group-post-news
4016     "\ek" gnus-group-edit-local-kill
4017     "\eK" gnus-group-edit-global-kill
4018     "\C-k" gnus-group-kill-group
4019     "\C-y" gnus-group-yank-group
4020     "\C-w" gnus-group-kill-region
4021     "\C-x\C-t" gnus-group-transpose-groups
4022     "\C-c\C-l" gnus-group-list-killed
4023     "\C-c\C-x" gnus-group-expire-articles
4024     "\C-c\M-\C-x" gnus-group-expire-all-groups
4025     "V" gnus-version
4026     "s" gnus-group-save-newsrc
4027     "z" gnus-group-suspend
4028     "Z" gnus-group-clear-dribble
4029     "q" gnus-group-exit
4030     "Q" gnus-group-quit
4031     "?" gnus-group-describe-briefly
4032     "\C-c\C-i" gnus-info-find-node
4033     "\M-e" gnus-group-edit-group-method
4034     "^" gnus-group-enter-server-mode
4035     gnus-mouse-2 gnus-mouse-pick-group
4036     "<" beginning-of-buffer
4037     ">" end-of-buffer
4038     "\C-c\C-b" gnus-bug
4039     "\C-c\C-s" gnus-group-sort-groups
4040     "t" gnus-topic-mode
4041     "\C-c\M-g" gnus-activate-all-groups
4042     "\M-&" gnus-group-universal-argument
4043     "#" gnus-group-mark-group
4044     "\M-#" gnus-group-unmark-group)
4045
4046   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4047     "m" gnus-group-mark-group
4048     "u" gnus-group-unmark-group
4049     "w" gnus-group-mark-region
4050     "m" gnus-group-mark-buffer
4051     "r" gnus-group-mark-regexp
4052     "U" gnus-group-unmark-all-groups)
4053
4054   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4055     "d" gnus-group-make-directory-group
4056     "h" gnus-group-make-help-group
4057     "a" gnus-group-make-archive-group
4058     "k" gnus-group-make-kiboze-group
4059     "m" gnus-group-make-group
4060     "E" gnus-group-edit-group
4061     "e" gnus-group-edit-group-method
4062     "p" gnus-group-edit-group-parameters
4063     "v" gnus-group-add-to-virtual
4064     "V" gnus-group-make-empty-virtual
4065     "D" gnus-group-enter-directory
4066     "f" gnus-group-make-doc-group
4067     "r" gnus-group-rename-group
4068     "\177" gnus-group-delete-group
4069     [delete] gnus-group-delete-group)
4070
4071    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4072      "b" gnus-group-brew-soup
4073      "w" gnus-soup-save-areas
4074      "s" gnus-soup-send-replies
4075      "p" gnus-soup-pack-packet
4076      "r" nnsoup-pack-replies)
4077
4078    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4079      "s" gnus-group-sort-groups
4080      "a" gnus-group-sort-groups-by-alphabet
4081      "u" gnus-group-sort-groups-by-unread
4082      "l" gnus-group-sort-groups-by-level
4083      "v" gnus-group-sort-groups-by-score
4084      "r" gnus-group-sort-groups-by-rank
4085      "m" gnus-group-sort-groups-by-method)
4086
4087    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4088      "k" gnus-group-list-killed
4089      "z" gnus-group-list-zombies
4090      "s" gnus-group-list-groups
4091      "u" gnus-group-list-all-groups
4092      "A" gnus-group-list-active
4093      "a" gnus-group-apropos
4094      "d" gnus-group-description-apropos
4095      "m" gnus-group-list-matching
4096      "M" gnus-group-list-all-matching
4097      "l" gnus-group-list-level)
4098
4099    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4100      "f" gnus-score-flush-cache)
4101
4102    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4103      "f" gnus-group-fetch-faq)
4104
4105    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4106      "l" gnus-group-set-current-level
4107      "t" gnus-group-unsubscribe-current-group
4108      "s" gnus-group-unsubscribe-group
4109      "k" gnus-group-kill-group
4110      "y" gnus-group-yank-group
4111      "w" gnus-group-kill-region
4112      "\C-k" gnus-group-kill-level
4113      "z" gnus-group-kill-all-zombies))
4114
4115 (defun gnus-group-mode ()
4116   "Major mode for reading news.
4117
4118 All normal editing commands are switched off.
4119 \\<gnus-group-mode-map>
4120 The group buffer lists (some of) the groups available.  For instance,
4121 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4122 lists all zombie groups.
4123
4124 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4125 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4126
4127 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4128
4129 The following commands are available:
4130
4131 \\{gnus-group-mode-map}"
4132   (interactive)
4133   (when (and menu-bar-mode
4134              (gnus-visual-p 'group-menu 'menu))
4135     (gnus-group-make-menu-bar))
4136   (kill-all-local-variables)
4137   (gnus-simplify-mode-line)
4138   (setq major-mode 'gnus-group-mode)
4139   (setq mode-name "Group")
4140   (gnus-group-set-mode-line)
4141   (setq mode-line-process nil)
4142   (use-local-map gnus-group-mode-map)
4143   (buffer-disable-undo (current-buffer))
4144   (setq truncate-lines t)
4145   (setq buffer-read-only t)
4146   (run-hooks 'gnus-group-mode-hook))
4147
4148 (defun gnus-mouse-pick-group (e)
4149   "Enter the group under the mouse pointer."
4150   (interactive "e")
4151   (mouse-set-point e)
4152   (gnus-group-read-group nil))
4153
4154 ;; Look at LEVEL and find out what the level is really supposed to be.
4155 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4156 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4157 (defun gnus-group-default-level (&optional level number-or-nil)
4158   (cond
4159    (gnus-group-use-permanent-levels
4160     (setq gnus-group-default-list-level
4161           (or level gnus-group-default-list-level))
4162     (or gnus-group-default-list-level gnus-level-subscribed))
4163    (number-or-nil
4164     level)
4165    (t
4166     (or level gnus-group-default-list-level gnus-level-subscribed))))
4167
4168 ;;;###autoload
4169 (defun gnus-slave-no-server (&optional arg)
4170   "Read network news as a slave, without connecting to local server"
4171   (interactive "P")
4172   (gnus-no-server arg t))
4173
4174 ;;;###autoload
4175 (defun gnus-no-server (&optional arg slave)
4176   "Read network news.
4177 If ARG is a positive number, Gnus will use that as the
4178 startup level.  If ARG is nil, Gnus will be started at level 2.
4179 If ARG is non-nil and not a positive number, Gnus will
4180 prompt the user for the name of an NNTP server to use.
4181 As opposed to `gnus', this command will not connect to the local server."
4182   (interactive "P")
4183   (make-local-variable 'gnus-group-use-permanent-levels)
4184   (setq gnus-group-use-permanent-levels t)
4185   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4186
4187 ;;;###autoload
4188 (defun gnus-slave (&optional arg)
4189   "Read news as a slave."
4190   (interactive "P")
4191   (gnus arg nil 'slave))
4192
4193 ;;;###autoload
4194 (defun gnus-other-frame (&optional arg)
4195   "Pop up a frame to read news."
4196   (interactive "P")
4197   (if (get-buffer gnus-group-buffer)
4198       (let ((pop-up-frames t))
4199         (gnus arg))
4200     (select-frame (make-frame))
4201     (gnus arg)))
4202
4203 ;;;###autoload
4204 (defun gnus (&optional arg dont-connect slave)
4205   "Read network news.
4206 If ARG is non-nil and a positive number, Gnus will use that as the
4207 startup level.  If ARG is non-nil and not a positive number, Gnus will
4208 prompt the user for the name of an NNTP server to use."
4209   (interactive "P")
4210
4211   (if (get-buffer gnus-group-buffer)
4212       (progn
4213         (switch-to-buffer gnus-group-buffer)
4214         (gnus-group-get-new-news))
4215
4216     (gnus-clear-system)
4217     (nnheader-init-server-buffer)
4218     (gnus-read-init-file)
4219     (setq gnus-slave slave)
4220
4221     (gnus-group-setup-buffer)
4222     (let ((buffer-read-only nil))
4223       (erase-buffer)
4224       (if (not gnus-inhibit-startup-message)
4225           (progn
4226             (gnus-group-startup-message)
4227             (sit-for 0))))
4228
4229     (let ((level (and (numberp arg) (> arg 0) arg))
4230           did-connect)
4231       (unwind-protect
4232           (progn
4233             (or dont-connect
4234                 (setq did-connect
4235                       (gnus-start-news-server (and arg (not level))))))
4236         (if (and (not dont-connect)
4237                  (not did-connect))
4238             (gnus-group-quit)
4239           (run-hooks 'gnus-startup-hook)
4240           ;; NNTP server is successfully open.
4241
4242           ;; Find the current startup file name.
4243           (setq gnus-current-startup-file
4244                 (gnus-make-newsrc-file gnus-startup-file))
4245
4246           ;; Read the dribble file.
4247           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4248
4249           ;; Allow using GroupLens predictions.
4250           (when gnus-use-grouplens
4251             (bbb-login)
4252             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4253
4254           (gnus-summary-make-display-table)
4255           ;; Do the actual startup.
4256           (gnus-setup-news nil level dont-connect)
4257           ;; Generate the group buffer.
4258           (gnus-group-list-groups level)
4259           (gnus-group-first-unread-group)
4260           (gnus-configure-windows 'group)
4261           (gnus-group-set-mode-line))))))
4262
4263 (defun gnus-unload ()
4264   "Unload all Gnus features."
4265   (interactive)
4266   (or (boundp 'load-history)
4267       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4268   (let ((history load-history)
4269         feature)
4270     (while history
4271       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4272            (setq feature (cdr (assq 'provide (car history))))
4273            (unload-feature feature 'force))
4274       (setq history (cdr history)))))
4275
4276 (defun gnus-compile ()
4277   "Byte-compile the user-defined format specs."
4278   (interactive)
4279   (let ((entries gnus-format-specs)
4280         entry gnus-tmp-func)
4281     (save-excursion
4282       (gnus-message 7 "Compiling format specs...")
4283
4284       (while entries
4285         (setq entry (pop entries))
4286         (if (eq (car entry) 'version)
4287             (setq gnus-format-specs (delq entry gnus-format-specs))
4288           (when (and (listp (caddr entry))
4289                      (not (eq 'byte-code (caaddr entry))))
4290             (fset 'gnus-tmp-func
4291                   `(lambda () ,(caddr entry)))
4292             (byte-compile 'gnus-tmp-func)
4293             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4294
4295       (push (cons 'version emacs-version) gnus-format-specs)
4296
4297       (gnus-message 7 "Compiling user specs...done"))))
4298
4299 (defun gnus-indent-rigidly (start end arg)
4300   "Indent rigidly using only spaces and no tabs."
4301   (save-excursion
4302     (save-restriction
4303       (narrow-to-region start end)
4304       (indent-rigidly start end arg)
4305       (goto-char (point-min))
4306       (while (search-forward "\t" nil t)
4307         (replace-match "        " t t)))))
4308
4309 (defun gnus-group-startup-message (&optional x y)
4310   "Insert startup message in current buffer."
4311   ;; Insert the message.
4312   (erase-buffer)
4313   (insert
4314    (format "              %s
4315           _    ___ _             _
4316           _ ___ __ ___  __    _ ___
4317           __   _     ___    __  ___
4318               _           ___     _
4319              _  _ __             _
4320              ___   __            _
4321                    __           _
4322                     _      _   _
4323                    _      _    _
4324                       _  _    _
4325                   __  ___
4326                  _   _ _     _
4327                 _   _
4328               _    _
4329              _    _
4330             _
4331           __
4332
4333 "
4334            ""))
4335   ;; And then hack it.
4336   (gnus-indent-rigidly (point-min) (point-max)
4337                        (/ (max (- (window-width) (or x 46)) 0) 2))
4338   (goto-char (point-min))
4339   (forward-line 1)
4340   (let* ((pheight (count-lines (point-min) (point-max)))
4341          (wheight (window-height))
4342          (rest (- wheight pheight)))
4343     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4344   ;; Fontify some.
4345   (goto-char (point-min))
4346   (and (search-forward "Praxis" nil t)
4347        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4348   (goto-char (point-min))
4349   (let* ((mode-string (gnus-group-set-mode-line)))
4350     (setq mode-line-buffer-identification
4351           (list (concat gnus-version (substring (car mode-string) 4))))
4352     (set-buffer-modified-p t)))
4353
4354 (defun gnus-group-setup-buffer ()
4355   (or (get-buffer gnus-group-buffer)
4356       (progn
4357         (switch-to-buffer gnus-group-buffer)
4358         (gnus-add-current-to-buffer-list)
4359         (gnus-group-mode)
4360         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4361
4362 (defun gnus-group-list-groups (&optional level unread lowest)
4363   "List newsgroups with level LEVEL or lower that have unread articles.
4364 Default is all subscribed groups.
4365 If argument UNREAD is non-nil, groups with no unread articles are also
4366 listed."
4367   (interactive (list (if current-prefix-arg
4368                          (prefix-numeric-value current-prefix-arg)
4369                        (or
4370                         (gnus-group-default-level nil t)
4371                         gnus-group-default-list-level
4372                         gnus-level-subscribed))))
4373   (or level
4374       (setq level (car gnus-group-list-mode)
4375             unread (cdr gnus-group-list-mode)))
4376   (setq level (gnus-group-default-level level))
4377   (gnus-group-setup-buffer)             ;May call from out of group buffer
4378   (gnus-update-format-specifications)
4379   (let ((case-fold-search nil)
4380         (props (text-properties-at (gnus-point-at-bol)))
4381         (group (gnus-group-group-name)))
4382     (set-buffer gnus-group-buffer)
4383     (funcall gnus-group-prepare-function level unread lowest)
4384     (if (zerop (buffer-size))
4385         (gnus-message 5 gnus-no-groups-message)
4386       (goto-char (point-max))
4387       (when (or (not gnus-group-goto-next-group-function)
4388                 (not (funcall gnus-group-goto-next-group-function 
4389                               group props)))
4390         (if (not group)
4391             ;; Go to the first group with unread articles.
4392             (gnus-group-search-forward t)
4393           ;; Find the right group to put point on.  If the current group
4394           ;; has disappeared in the new listing, try to find the next
4395           ;; one.        If no next one can be found, just leave point at the
4396           ;; first newsgroup in the buffer.
4397           (if (not (gnus-goto-char
4398                     (text-property-any
4399                      (point-min) (point-max)
4400                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4401               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4402                 (while (and newsrc
4403                             (not (gnus-goto-char
4404                                   (text-property-any
4405                                    (point-min) (point-max) 'gnus-group
4406                                    (gnus-intern-safe
4407                                     (caar newsrc) gnus-active-hashtb)))))
4408                   (setq newsrc (cdr newsrc)))
4409                 (or newsrc (progn (goto-char (point-max))
4410                                   (forward-line -1)))))))
4411       ;; Adjust cursor point.
4412       (gnus-group-position-point))))
4413
4414 (defun gnus-group-list-level (level &optional all)
4415   "List groups on LEVEL.
4416 If ALL (the prefix), also list groups that have no unread articles."
4417   (interactive "nList groups on level: \nP")
4418   (gnus-group-list-groups level all level))
4419
4420 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4421   "List all newsgroups with unread articles of level LEVEL or lower.
4422 If ALL is non-nil, list groups that have no unread articles.
4423 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4424 If REGEXP, only list groups matching REGEXP."
4425   (set-buffer gnus-group-buffer)
4426   (let ((buffer-read-only nil)
4427         (newsrc (cdr gnus-newsrc-alist))
4428         (lowest (or lowest 1))
4429         info clevel unread group params)
4430     (erase-buffer)
4431     (if (< lowest gnus-level-zombie)
4432         ;; List living groups.
4433         (while newsrc
4434           (setq info (car newsrc)
4435                 group (gnus-info-group info)
4436                 params (gnus-info-params info)
4437                 newsrc (cdr newsrc)
4438                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4439           (and unread                   ; This group might be bogus
4440                (or (not regexp)
4441                    (string-match regexp group))
4442                (<= (setq clevel (gnus-info-level info)) level)
4443                (>= clevel lowest)
4444                (or all                  ; We list all groups?
4445                    (and gnus-group-list-inactive-groups
4446                         (eq unread t))  ; We list unactivated groups
4447                    (> unread 0)         ; We list groups with unread articles
4448                    (and gnus-list-groups-with-ticked-articles
4449                         (cdr (assq 'tick (gnus-info-marks info))))
4450                                         ; And groups with tickeds
4451                    ;; Check for permanent visibility.
4452                    (and gnus-permanently-visible-groups
4453                         (string-match gnus-permanently-visible-groups
4454                                       group))
4455                    (memq 'visible params)
4456                    (cdr (assq 'visible params)))
4457                (gnus-group-insert-group-line
4458                 group (gnus-info-level info)
4459                 (gnus-info-marks info) unread (gnus-info-method info)))))
4460
4461     ;; List dead groups.
4462     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4463          (gnus-group-prepare-flat-list-dead
4464           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4465           gnus-level-zombie ?Z
4466           regexp))
4467     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4468          (gnus-group-prepare-flat-list-dead
4469           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4470           gnus-level-killed ?K regexp))
4471
4472     (gnus-group-set-mode-line)
4473     (setq gnus-group-list-mode (cons level all))
4474     (run-hooks 'gnus-group-prepare-hook)))
4475
4476 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4477   ;; List zombies and killed lists somewhat faster, which was
4478   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4479   ;; this by ignoring the group format specification altogether.
4480   (let (group)
4481     (if regexp
4482         ;; This loop is used when listing groups that match some
4483         ;; regexp.
4484         (while groups
4485           (setq group (pop groups))
4486           (when (string-match regexp group)
4487             (add-text-properties
4488              (point) (prog1 (1+ (point))
4489                        (insert " " mark "     *: " group "\n"))
4490              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4491                    'gnus-unread t
4492                    'gnus-level level))))
4493       ;; This loop is used when listing all groups.
4494       (while groups
4495         (add-text-properties
4496          (point) (prog1 (1+ (point))
4497                    (insert " " mark "     *: "
4498                            (setq group (pop groups)) "\n"))
4499          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4500                'gnus-unread t
4501                'gnus-level level))))))
4502
4503 (defmacro gnus-group-real-name (group)
4504   "Find the real name of a foreign newsgroup."
4505   `(let ((gname ,group))
4506      (if (string-match ":[^:]+$" gname)
4507          (substring gname (1+ (match-beginning 0)))
4508        gname)))
4509
4510 (defsubst gnus-server-add-address (method)
4511   (let ((method-name (symbol-name (car method))))
4512     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4513              (not (assq (intern (concat method-name "-address")) method)))
4514         (append method (list (list (intern (concat method-name "-address"))
4515                                    (nth 1 method))))
4516       method)))
4517
4518 (defsubst gnus-server-get-method (group method)
4519   ;; Input either a server name, and extended server name, or a
4520   ;; select method, and return a select method.
4521   (cond ((stringp method)
4522          (gnus-server-to-method method))
4523         ((and (stringp (car method)) group)
4524          (gnus-server-extend-method group method))
4525         (t
4526          (gnus-server-add-address method))))
4527
4528 (defun gnus-server-to-method (server)
4529   "Map virtual server names to select methods."
4530   (or 
4531    ;; Perhaps this is the native server?
4532    (and (equal server "native") gnus-select-method)
4533    ;; It should be in the server alist.
4534    (cdr (assoc server gnus-server-alist))
4535    ;; If not, we look through all the opened server
4536    ;; to see whether we can find it there.
4537    (let ((opened gnus-opened-servers))
4538      (while (and opened
4539                  (not (equal server (format "%s:%s" (caaar opened)
4540                                             (cadaar opened)))))
4541        (pop opened))
4542      (caar opened))))
4543
4544 (defmacro gnus-method-equal (ss1 ss2)
4545   "Say whether two servers are equal."
4546   `(let ((s1 ,ss1)
4547          (s2 ,ss2))
4548      (or (equal s1 s2)
4549          (and (= (length s1) (length s2))
4550               (progn
4551                 (while (and s1 (member (car s1) s2))
4552                   (setq s1 (cdr s1)))
4553                 (null s1))))))
4554
4555 (defun gnus-server-equal (m1 m2)
4556   "Say whether two methods are equal."
4557   (let ((m1 (cond ((null m1) gnus-select-method)
4558                   ((stringp m1) (gnus-server-to-method m1))
4559                   (t m1)))
4560         (m2 (cond ((null m2) gnus-select-method)
4561                   ((stringp m2) (gnus-server-to-method m2))
4562                   (t m2))))
4563     (gnus-method-equal m1 m2)))
4564
4565 (defun gnus-group-prefixed-name (group method)
4566   "Return the whole name from GROUP and METHOD."
4567   (and (stringp method) (setq method (gnus-server-to-method method)))
4568   (concat (format "%s" (car method))
4569           (if (and
4570                (or (assoc (format "%s" (car method)) 
4571                           (gnus-methods-using 'address))
4572                    (gnus-server-equal method gnus-message-archive-method))
4573                (nth 1 method)
4574                (not (string= (nth 1 method) "")))
4575               (concat "+" (nth 1 method)))
4576           ":" group))
4577
4578 (defun gnus-group-real-prefix (group)
4579   "Return the prefix of the current group name."
4580   (if (string-match "^[^:]+:" group)
4581       (substring group 0 (match-end 0))
4582     ""))
4583
4584 (defun gnus-group-method (group)
4585   "Return the server or method used for selecting GROUP."
4586   (let ((prefix (gnus-group-real-prefix group)))
4587     (if (equal prefix "")
4588         gnus-select-method
4589       (let ((servers gnus-opened-servers)
4590             (server "")
4591             backend possible found)
4592         (if (string-match "^[^\\+]+\\+" prefix)
4593             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4594                   server (substring prefix (match-end 0) (1- (length prefix))))
4595           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4596         (while servers
4597           (when (eq (caaar servers) backend)
4598             (setq possible (caar servers))
4599             (when (equal (cadaar servers) server)
4600               (setq found (caar servers))))
4601           (pop servers))
4602         (or (car (rassoc found gnus-server-alist))
4603             found
4604             (car (rassoc possible gnus-server-alist))
4605             possible
4606             (list backend server))))))
4607
4608 (defsubst gnus-secondary-method-p (method)
4609   "Return whether METHOD is a secondary select method."
4610   (let ((methods gnus-secondary-select-methods)
4611         (gmethod (gnus-server-get-method nil method)))
4612     (while (and methods
4613                 (not (equal (gnus-server-get-method nil (car methods))
4614                             gmethod)))
4615       (setq methods (cdr methods)))
4616     methods))
4617
4618 (defun gnus-group-foreign-p (group)
4619   "Say whether a group is foreign or not."
4620   (and (not (gnus-group-native-p group))
4621        (not (gnus-group-secondary-p group))))
4622
4623 (defun gnus-group-native-p (group)
4624   "Say whether the group is native or not."
4625   (not (string-match ":" group)))
4626
4627 (defun gnus-group-secondary-p (group)
4628   "Say whether the group is secondary or not."
4629   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4630
4631 (defun gnus-group-get-parameter (group &optional symbol)
4632   "Returns the group parameters for GROUP.
4633 If SYMBOL, return the value of that symbol in the group parameters."
4634   (let ((params (gnus-info-params (gnus-get-info group))))
4635     (if symbol
4636         (gnus-group-parameter-value params symbol)
4637       params)))
4638
4639 (defun gnus-group-parameter-value (params symbol)
4640   "Return the value of SYMBOL in group PARAMS."
4641   (or (car (memq symbol params))        ; It's either a simple symbol
4642       (cdr (assq symbol params))))      ; or a cons.
4643
4644 (defun gnus-group-add-parameter (group param)
4645   "Add parameter PARAM to GROUP."
4646   (let ((info (gnus-get-info group)))
4647     (if (not info)
4648         () ; This is a dead group.  We just ignore it.
4649       ;; Cons the new param to the old one and update.
4650       (gnus-group-set-info (cons param (gnus-info-params info))
4651                            group 'params))))
4652
4653 (defun gnus-group-set-parameter (group name value)
4654   "Set parameter NAME to VALUE in GROUP."
4655   (let ((info (gnus-get-info group)))
4656     (if (not info)
4657         () ; This is a dead group.  We just ignore it.
4658       (let ((old-params (gnus-info-params info))
4659             (new-params (list (cons name value))))
4660         (while old-params
4661           (if (or (not (listp (car old-params)))
4662                   (not (eq (caar old-params) name)))
4663               (setq new-params (append new-params (list (car old-params)))))
4664           (setq old-params (cdr old-params)))
4665         (gnus-group-set-info new-params group 'params)))))
4666
4667 (defun gnus-group-add-score (group &optional score)
4668   "Add SCORE to the GROUP score.
4669 If SCORE is nil, add 1 to the score of GROUP."
4670   (let ((info (gnus-get-info group)))
4671     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4672
4673 (defun gnus-summary-bubble-group ()
4674   "Increase the score of the current group.
4675 This is a handy function to add to `gnus-summary-exit-hook' to
4676 increase the score of each group you read."
4677   (gnus-group-add-score gnus-newsgroup-name))
4678
4679 (defun gnus-group-set-info (info &optional method-only-group part)
4680   (let* ((entry (gnus-gethash
4681                  (or method-only-group (gnus-info-group info))
4682                  gnus-newsrc-hashtb))
4683          (part-info info)
4684          (info (if method-only-group (nth 2 entry) info))
4685          method)
4686     (when method-only-group
4687       (unless entry
4688         (error "Trying to change non-existent group %s" method-only-group))
4689       ;; We have received parts of the actual group info - either the
4690       ;; select method or the group parameters.  We first check
4691       ;; whether we have to extend the info, and if so, do that.
4692       (let ((len (length info))
4693             (total (if (eq part 'method) 5 6)))
4694         (when (< len total)
4695           (setcdr (nthcdr (1- len) info)
4696                   (make-list (- total len) nil)))
4697         ;; Then we enter the new info.
4698         (setcar (nthcdr (1- total) info) part-info)))
4699     (unless entry
4700       ;; This is a new group, so we just create it.
4701       (save-excursion
4702         (set-buffer gnus-group-buffer)
4703         (setq method (gnus-info-method info))
4704         (when (gnus-server-equal method "native")
4705           (setq method nil))
4706         (if method
4707             ;; It's a foreign group...
4708             (gnus-group-make-group
4709              (gnus-group-real-name (gnus-info-group info))
4710              (if (stringp method) method
4711                (prin1-to-string (car method)))
4712              (and (consp method)
4713                   (nth 1 (gnus-info-method info))))
4714           ;; It's a native group.
4715           (gnus-group-make-group (gnus-info-group info)))
4716         (gnus-message 6 "Note: New group created")
4717         (setq entry
4718               (gnus-gethash (gnus-group-prefixed-name
4719                              (gnus-group-real-name (gnus-info-group info))
4720                              (or (gnus-info-method info) gnus-select-method))
4721                             gnus-newsrc-hashtb))))
4722     ;; Whether it was a new group or not, we now have the entry, so we
4723     ;; can do the update.
4724     (if entry
4725         (progn
4726           (setcar (nthcdr 2 entry) info)
4727           (when (and (not (eq (car entry) t))
4728                      (gnus-active (gnus-info-group info)))
4729             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4730       (error "No such group: %s" (gnus-info-group info)))))
4731
4732 (defun gnus-group-set-method-info (group select-method)
4733   (gnus-group-set-info select-method group 'method))
4734
4735 (defun gnus-group-set-params-info (group params)
4736   (gnus-group-set-info params group 'params))
4737
4738 (defun gnus-group-update-group-line ()
4739   "Update the current line in the group buffer."
4740   (let* ((buffer-read-only nil)
4741          (group (gnus-group-group-name))
4742          (gnus-group-indentation (gnus-group-group-indentation))
4743          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4744     (and entry
4745          (not (gnus-ephemeral-group-p group))
4746          (gnus-dribble-enter
4747           (concat "(gnus-group-set-info '"
4748                   (prin1-to-string (nth 2 entry)) ")")))
4749     (gnus-delete-line)
4750     (gnus-group-insert-group-line-info group)
4751     (forward-line -1)
4752     (gnus-group-position-point)))
4753
4754 (defun gnus-group-insert-group-line-info (group)
4755   "Insert GROUP on the current line."
4756   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4757         active info)
4758     (if entry
4759         (progn
4760           ;; (Un)subscribed group.
4761           (setq info (nth 2 entry))
4762           (gnus-group-insert-group-line
4763            group (gnus-info-level info) (gnus-info-marks info)
4764            (or (car entry) t) (gnus-info-method info)))
4765       ;; This group is dead.
4766       (gnus-group-insert-group-line
4767        group
4768        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4769        nil
4770        (if (setq active (gnus-active group))
4771            (- (1+ (cdr active)) (car active)) 0)
4772        nil))))
4773
4774 (defun gnus-group-insert-group-line
4775   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4776                   gnus-tmp-method)
4777   "Insert a group line in the group buffer."
4778   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4779          (gnus-tmp-number-total
4780           (if gnus-tmp-active
4781               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4782             0))
4783          (gnus-tmp-number-of-unread
4784           (if (numberp number) (int-to-string (max 0 number))
4785             "*"))
4786          (gnus-tmp-number-of-read
4787           (if (numberp number)
4788               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4789             "*"))
4790          (gnus-tmp-subscribed
4791           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4792                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4793                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4794                 (t ?K)))
4795          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4796          (gnus-tmp-newsgroup-description
4797           (if gnus-description-hashtb
4798               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4799             ""))
4800          (gnus-tmp-moderated
4801           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4802          (gnus-tmp-moderated-string
4803           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4804          (gnus-tmp-method
4805           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4806          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4807          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4808          (gnus-tmp-news-method-string
4809           (if gnus-tmp-method
4810               (format "(%s:%s)" (car gnus-tmp-method)
4811                       (cadr gnus-tmp-method)) ""))
4812          (gnus-tmp-marked-mark
4813           (if (and (numberp number)
4814                    (zerop number)
4815                    (cdr (assq 'tick gnus-tmp-marked)))
4816               ?* ? ))
4817          (gnus-tmp-process-marked
4818           (if (member gnus-tmp-group gnus-group-marked)
4819               gnus-process-mark ? ))
4820          (gnus-tmp-grouplens
4821           (or (and gnus-use-grouplens
4822                    (bbb-grouplens-group-p gnus-tmp-group))
4823               ""))
4824          (buffer-read-only nil)
4825          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4826     (beginning-of-line)
4827     (add-text-properties
4828      (point)
4829      (prog1 (1+ (point))
4830        ;; Insert the text.
4831        (eval gnus-group-line-format-spec))
4832      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4833        gnus-unread ,(if (numberp number)
4834                         (string-to-int gnus-tmp-number-of-unread)
4835                       t)
4836        gnus-marked ,gnus-tmp-marked-mark
4837        gnus-indentation ,gnus-group-indentation
4838        gnus-level ,gnus-tmp-level))
4839     (when (gnus-visual-p 'group-highlight 'highlight)
4840       (forward-line -1)
4841       (run-hooks 'gnus-group-update-hook)
4842       (forward-line))
4843     ;; Allow XEmacs to remove front-sticky text properties.
4844     (gnus-group-remove-excess-properties)))
4845
4846 (defun gnus-group-update-group (group &optional visible-only)
4847   "Update all lines where GROUP appear.
4848 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4849 already."
4850   (save-excursion
4851     (set-buffer gnus-group-buffer)
4852     ;; The buffer may be narrowed.
4853     (save-restriction
4854       (widen)
4855       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4856             (loc (point-min))
4857             found buffer-read-only)
4858         ;; Enter the current status into the dribble buffer.
4859         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4860           (if (and entry (not (gnus-ephemeral-group-p group)))
4861               (gnus-dribble-enter
4862                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4863                        ")"))))
4864         ;; Find all group instances.  If topics are in use, each group
4865         ;; may be listed in more than once.
4866         (while (setq loc (text-property-any
4867                           loc (point-max) 'gnus-group ident))
4868           (setq found t)
4869           (goto-char loc)
4870           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4871             (gnus-delete-line)
4872             (gnus-group-insert-group-line-info group))
4873           (setq loc (1+ loc)))
4874         (unless (or found visible-only)
4875           ;; No such line in the buffer, find out where it's supposed to
4876           ;; go, and insert it there (or at the end of the buffer).
4877           (if gnus-goto-missing-group-function
4878               (funcall gnus-goto-missing-group-function group)
4879             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4880               (while (and entry (car entry)
4881                           (not
4882                            (gnus-goto-char
4883                             (text-property-any
4884                              (point-min) (point-max)
4885                              'gnus-group (gnus-intern-safe
4886                                           (caar entry) gnus-active-hashtb)))))
4887                 (setq entry (cdr entry)))
4888               (or entry (goto-char (point-max)))))
4889           ;; Finally insert the line.
4890           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4891             (gnus-group-insert-group-line-info group)))
4892         (gnus-group-set-mode-line)))))
4893
4894 (defun gnus-group-set-mode-line ()
4895   (when (memq 'group gnus-updated-mode-lines)
4896     (let* ((gformat (or gnus-group-mode-line-format-spec
4897                         (setq gnus-group-mode-line-format-spec
4898                               (gnus-parse-format
4899                                gnus-group-mode-line-format
4900                                gnus-group-mode-line-format-alist))))
4901            (gnus-tmp-news-server (cadr gnus-select-method))
4902            (gnus-tmp-news-method (car gnus-select-method))
4903            (max-len 60)
4904            gnus-tmp-header                      ;Dummy binding for user-defined formats
4905            ;; Get the resulting string.
4906            (mode-string (eval gformat)))
4907       ;; If the line is too long, we chop it off.
4908       (when (> (length mode-string) max-len)
4909         (setq mode-string (substring mode-string 0 (- max-len 4))))
4910       (prog1
4911           (setq mode-line-buffer-identification (list mode-string))
4912         (set-buffer-modified-p t)))))
4913
4914 (defun gnus-group-group-name ()
4915   "Get the name of the newsgroup on the current line."
4916   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4917     (and group (symbol-name group))))
4918
4919 (defun gnus-group-group-level ()
4920   "Get the level of the newsgroup on the current line."
4921   (get-text-property (gnus-point-at-bol) 'gnus-level))
4922
4923 (defun gnus-group-group-indentation ()
4924   "Get the indentation of the newsgroup on the current line."
4925   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4926       (and gnus-group-indentation-function
4927            (funcall gnus-group-indentation-function))
4928       ""))
4929
4930 (defun gnus-group-group-unread ()
4931   "Get the number of unread articles of the newsgroup on the current line."
4932   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4933
4934 (defun gnus-group-search-forward (&optional backward all level first-too)
4935   "Find the next newsgroup with unread articles.
4936 If BACKWARD is non-nil, find the previous newsgroup instead.
4937 If ALL is non-nil, just find any newsgroup.
4938 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4939 group exists.
4940 If FIRST-TOO, the current line is also eligible as a target."
4941   (let ((way (if backward -1 1))
4942         (low gnus-level-killed)
4943         (beg (point))
4944         pos found lev)
4945     (if (and backward (progn (beginning-of-line)) (bobp))
4946         nil
4947       (or first-too (forward-line way))
4948       (while (and
4949               (not (eobp))
4950               (not (setq
4951                     found
4952                     (and (or all
4953                              (and
4954                               (let ((unread
4955                                      (get-text-property (point) 'gnus-unread)))
4956                                 (and (numberp unread) (> unread 0)))
4957                               (setq lev (get-text-property (point)
4958                                                            'gnus-level))
4959                               (<= lev gnus-level-subscribed)))
4960                          (or (not level)
4961                              (and (setq lev (get-text-property (point)
4962                                                                'gnus-level))
4963                                   (or (= lev level)
4964                                       (and (< lev low)
4965                                            (< level lev)
4966                                            (progn
4967                                              (setq low lev)
4968                                              (setq pos (point))
4969                                              nil))))))))
4970               (zerop (forward-line way)))))
4971     (if found
4972         (progn (gnus-group-position-point) t)
4973       (goto-char (or pos beg))
4974       (and pos t))))
4975
4976 ;;; Gnus group mode commands
4977
4978 ;; Group marking.
4979
4980 (defun gnus-group-mark-group (n &optional unmark no-advance)
4981   "Mark the current group."
4982   (interactive "p")
4983   (let ((buffer-read-only nil)
4984         group)
4985     (while
4986         (and (> n 0)
4987              (setq group (gnus-group-group-name))
4988              (progn
4989                (beginning-of-line)
4990                (forward-char
4991                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4992                (delete-char 1)
4993                (if unmark
4994                    (progn
4995                      (insert " ")
4996                      (setq gnus-group-marked (delete group gnus-group-marked)))
4997                  (insert "#")
4998                  (setq gnus-group-marked
4999                        (cons group (delete group gnus-group-marked))))
5000                t)
5001              (or no-advance (zerop (gnus-group-next-group 1))))
5002       (setq n (1- n)))
5003     (gnus-summary-position-point)
5004     n))
5005
5006 (defun gnus-group-unmark-group (n)
5007   "Remove the mark from the current group."
5008   (interactive "p")
5009   (gnus-group-mark-group n 'unmark)
5010   (gnus-group-position-point))
5011
5012 (defun gnus-group-unmark-all-groups ()
5013   "Unmark all groups."
5014   (interactive)
5015   (let ((groups gnus-group-marked))
5016     (save-excursion
5017       (while groups
5018         (gnus-group-remove-mark (pop groups)))))
5019   (gnus-group-position-point))
5020
5021 (defun gnus-group-mark-region (unmark beg end)
5022   "Mark all groups between point and mark.
5023 If UNMARK, remove the mark instead."
5024   (interactive "P\nr")
5025   (let ((num (count-lines beg end)))
5026     (save-excursion
5027       (goto-char beg)
5028       (- num (gnus-group-mark-group num unmark)))))
5029
5030 (defun gnus-group-mark-buffer (&optional unmark)
5031   "Mark all groups in the buffer.
5032 If UNMARK, remove the mark instead."
5033   (interactive "P")
5034   (gnus-group-mark-region unmark (point-min) (point-max)))
5035
5036 (defun gnus-group-mark-regexp (regexp)
5037   "Mark all groups that match some regexp."
5038   (interactive "sMark (regexp): ")
5039   (let ((alist (cdr gnus-newsrc-alist))
5040         group)
5041     (while alist
5042       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5043         (gnus-group-set-mark group))))
5044   (gnus-group-position-point))
5045
5046 (defun gnus-group-remove-mark (group)
5047   "Remove the process mark from GROUP and move point there.
5048 Return nil if the group isn't displayed."
5049   (if (gnus-group-goto-group group)
5050       (save-excursion
5051         (gnus-group-mark-group 1 'unmark t)
5052         t)
5053     (setq gnus-group-marked
5054           (delete group gnus-group-marked))
5055     nil))
5056
5057 (defun gnus-group-set-mark (group)
5058   "Set the process mark on GROUP."
5059   (if (gnus-group-goto-group group) 
5060       (save-excursion
5061         (gnus-group-mark-group 1 nil t))
5062     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5063
5064 (defun gnus-group-universal-argument (arg &optional groups func)
5065   "Perform any command on all groups accoring to the process/prefix convention."
5066   (interactive "P")
5067   (let ((groups (or groups (gnus-group-process-prefix arg)))
5068         group func)
5069     (if (eq (setq func (or func
5070                            (key-binding
5071                             (read-key-sequence
5072                              (substitute-command-keys
5073                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5074             'undefined)
5075         (progn
5076           (message "Undefined key")
5077           (ding))
5078       (while groups
5079         (gnus-group-remove-mark (setq group (pop groups)))
5080         (command-execute func))))
5081   (gnus-group-position-point))
5082
5083 (defun gnus-group-process-prefix (n)
5084   "Return a list of groups to work on.
5085 Take into consideration N (the prefix) and the list of marked groups."
5086   (cond
5087    (n
5088     (setq n (prefix-numeric-value n))
5089     ;; There is a prefix, so we return a list of the N next
5090     ;; groups.
5091     (let ((way (if (< n 0) -1 1))
5092           (n (abs n))
5093           group groups)
5094       (save-excursion
5095         (while (and (> n 0)
5096                     (setq group (gnus-group-group-name)))
5097           (setq groups (cons group groups))
5098           (setq n (1- n))
5099           (gnus-group-next-group way)))
5100       (nreverse groups)))
5101    ((and (boundp 'transient-mark-mode)
5102          transient-mark-mode
5103          mark-active)
5104     ;; Work on the region between point and mark.
5105     (let ((max (max (point) (mark)))
5106           groups)
5107       (save-excursion
5108         (goto-char (min (point) (mark)))
5109         (while
5110             (and
5111              (push (gnus-group-group-name) groups)
5112              (zerop (gnus-group-next-group 1))
5113              (< (point) max)))
5114         (nreverse groups))))
5115    (gnus-group-marked
5116     ;; No prefix, but a list of marked articles.
5117     (reverse gnus-group-marked))
5118    (t
5119     ;; Neither marked articles or a prefix, so we return the
5120     ;; current group.
5121     (let ((group (gnus-group-group-name)))
5122       (and group (list group))))))
5123
5124 ;; Selecting groups.
5125
5126 (defun gnus-group-read-group (&optional all no-article group)
5127   "Read news in this newsgroup.
5128 If the prefix argument ALL is non-nil, already read articles become
5129 readable.  IF ALL is a number, fetch this number of articles.  If the
5130 optional argument NO-ARTICLE is non-nil, no article will be
5131 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5132 group."
5133   (interactive "P")
5134   (let ((group (or group (gnus-group-group-name)))
5135         number active marked entry)
5136     (or group (error "No group on current line"))
5137     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5138                                             group gnus-newsrc-hashtb)))))
5139     ;; This group might be a dead group.  In that case we have to get
5140     ;; the number of unread articles from `gnus-active-hashtb'.
5141     (setq number
5142           (cond ((numberp all) all)
5143                 (entry (car entry))
5144                 ((setq active (gnus-active group))
5145                  (- (1+ (cdr active)) (car active)))))
5146     (gnus-summary-read-group
5147      group (or all (and (numberp number)
5148                         (zerop (+ number (length (cdr (assq 'tick marked)))
5149                                   (length (cdr (assq 'dormant marked)))))))
5150      no-article)))
5151
5152 (defun gnus-group-select-group (&optional all)
5153   "Select this newsgroup.
5154 No article is selected automatically.
5155 If ALL is non-nil, already read articles become readable.
5156 If ALL is a number, fetch this number of articles."
5157   (interactive "P")
5158   (gnus-group-read-group all t))
5159
5160 (defun gnus-group-quick-select-group (&optional all)
5161   "Select the current group \"quickly\".
5162 This means that no highlighting or scoring will be performed."
5163   (interactive "P")
5164   (let (gnus-visual
5165         gnus-score-find-score-files-function
5166         gnus-apply-kill-hook
5167         gnus-summary-expunge-below)
5168     (gnus-group-read-group all t)))
5169
5170 (defun gnus-group-visible-select-group (&optional all)
5171   "Select the current group without hiding any articles."
5172   (interactive "P")
5173   (let ((gnus-inhibit-limiting t))
5174     (gnus-group-read-group all t)))
5175
5176 ;;;###autoload
5177 (defun gnus-fetch-group (group)
5178   "Start Gnus if necessary and enter GROUP.
5179 Returns whether the fetching was successful or not."
5180   (interactive "sGroup name: ")
5181   (or (get-buffer gnus-group-buffer)
5182       (gnus))
5183   (gnus-group-read-group nil nil group))
5184
5185 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5186 ;; if selection was successful.
5187 (defun gnus-group-read-ephemeral-group
5188   (group method &optional activate quit-config)
5189   (let ((group (if (gnus-group-foreign-p group) group
5190                  (gnus-group-prefixed-name group method))))
5191     (gnus-sethash
5192      group
5193      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5194                      ((quit-config . ,(if quit-config quit-config
5195                                         (cons (current-buffer) 'summary))))))
5196      gnus-newsrc-hashtb)
5197     (set-buffer gnus-group-buffer)
5198     (or (gnus-check-server method)
5199         (error "Unable to contact server: %s" (gnus-status-message method)))
5200     (if activate (or (gnus-request-group group)
5201                      (error "Couldn't request group")))
5202     (condition-case ()
5203         (gnus-group-read-group t t group)
5204       (error nil)
5205       (quit nil))))
5206
5207 (defun gnus-group-jump-to-group (group)
5208   "Jump to newsgroup GROUP."
5209   (interactive
5210    (list (completing-read
5211           "Group: " gnus-active-hashtb nil
5212           (memq gnus-select-method gnus-have-read-active-file))))
5213
5214   (if (equal group "")
5215       (error "Empty group name"))
5216
5217   (let ((b (text-property-any
5218             (point-min) (point-max)
5219             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5220     (unless (gnus-ephemeral-group-p group)
5221       (if b
5222           ;; Either go to the line in the group buffer...
5223           (goto-char b)
5224         ;; ... or insert the line.
5225         (or
5226          (gnus-active group)
5227          (gnus-activate-group group)
5228          (error "%s error: %s" group (gnus-status-message group)))
5229
5230         (gnus-group-update-group group)
5231         (goto-char (text-property-any
5232                     (point-min) (point-max)
5233                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5234     ;; Adjust cursor point.
5235     (gnus-group-position-point)))
5236
5237 (defun gnus-group-goto-group (group)
5238   "Goto to newsgroup GROUP."
5239   (when group
5240     (let ((b (text-property-any (point-min) (point-max)
5241                                 'gnus-group (gnus-intern-safe
5242                                              group gnus-active-hashtb))))
5243       (and b (goto-char b)))))
5244
5245 (defun gnus-group-next-group (n)
5246   "Go to next N'th newsgroup.
5247 If N is negative, search backward instead.
5248 Returns the difference between N and the number of skips actually
5249 done."
5250   (interactive "p")
5251   (gnus-group-next-unread-group n t))
5252
5253 (defun gnus-group-next-unread-group (n &optional all level)
5254   "Go to next N'th unread newsgroup.
5255 If N is negative, search backward instead.
5256 If ALL is non-nil, choose any newsgroup, unread or not.
5257 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5258 such group can be found, the next group with a level higher than
5259 LEVEL.
5260 Returns the difference between N and the number of skips actually
5261 made."
5262   (interactive "p")
5263   (let ((backward (< n 0))
5264         (n (abs n)))
5265     (while (and (> n 0)
5266                 (gnus-group-search-forward
5267                  backward (or (not gnus-group-goto-unread) all) level))
5268       (setq n (1- n)))
5269     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5270                                (if level " on this level or higher" "")))
5271     n))
5272
5273 (defun gnus-group-prev-group (n)
5274   "Go to previous N'th newsgroup.
5275 Returns the difference between N and the number of skips actually
5276 done."
5277   (interactive "p")
5278   (gnus-group-next-unread-group (- n) t))
5279
5280 (defun gnus-group-prev-unread-group (n)
5281   "Go to previous N'th unread newsgroup.
5282 Returns the difference between N and the number of skips actually
5283 done."
5284   (interactive "p")
5285   (gnus-group-next-unread-group (- n)))
5286
5287 (defun gnus-group-next-unread-group-same-level (n)
5288   "Go to next N'th unread newsgroup on the same level.
5289 If N is negative, search backward instead.
5290 Returns the difference between N and the number of skips actually
5291 done."
5292   (interactive "p")
5293   (gnus-group-next-unread-group n t (gnus-group-group-level))
5294   (gnus-group-position-point))
5295
5296 (defun gnus-group-prev-unread-group-same-level (n)
5297   "Go to next N'th unread newsgroup on the same level.
5298 Returns the difference between N and the number of skips actually
5299 done."
5300   (interactive "p")
5301   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5302   (gnus-group-position-point))
5303
5304 (defun gnus-group-best-unread-group (&optional exclude-group)
5305   "Go to the group with the highest level.
5306 If EXCLUDE-GROUP, do not go to that group."
5307   (interactive)
5308   (goto-char (point-min))
5309   (let ((best 100000)
5310         unread best-point)
5311     (while (setq unread (get-text-property (point) 'gnus-unread))
5312       (if (and (numberp unread) (> unread 0))
5313           (progn
5314             (if (and (< (get-text-property (point) 'gnus-level) best)
5315                      (or (not exclude-group)
5316                          (not (equal exclude-group (gnus-group-group-name)))))
5317                 (progn
5318                   (setq best (get-text-property (point) 'gnus-level))
5319                   (setq best-point (point))))))
5320       (forward-line 1))
5321     (if best-point (goto-char best-point))
5322     (gnus-summary-position-point)
5323     (and best-point (gnus-group-group-name))))
5324
5325 (defun gnus-group-first-unread-group ()
5326   "Go to the first group with unread articles."
5327   (interactive)
5328   (prog1
5329       (let ((opoint (point))
5330             unread)
5331         (goto-char (point-min))
5332         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5333                 (and (numberp unread)   ; Not a topic.
5334                      (not (zerop unread))) ; Has unread articles.
5335                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5336             (point)                     ; Success.
5337           (goto-char opoint)
5338           nil))                         ; Not success.
5339     (gnus-group-position-point)))
5340
5341 (defun gnus-group-enter-server-mode ()
5342   "Jump to the server buffer."
5343   (interactive)
5344   (gnus-enter-server-buffer))
5345
5346 (defun gnus-group-make-group (name &optional method address)
5347   "Add a new newsgroup.
5348 The user will be prompted for a NAME, for a select METHOD, and an
5349 ADDRESS."
5350   (interactive
5351    (cons
5352     (read-string "Group name: ")
5353     (let ((method
5354            (completing-read
5355             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5356             nil t)))
5357       (if (assoc method gnus-valid-select-methods)
5358           (list method
5359                 (if (memq 'prompt-address
5360                           (assoc method gnus-valid-select-methods))
5361                     (read-string "Address: ")
5362                   ""))
5363         (list method "")))))
5364
5365   (save-excursion
5366     (set-buffer gnus-group-buffer)
5367     (let* ((meth (and method (if address (list (intern method) address)
5368                                method)))
5369            (nname (if method (gnus-group-prefixed-name name meth) name))
5370            info)
5371       (and (gnus-gethash nname gnus-newsrc-hashtb)
5372            (error "Group %s already exists" nname))
5373       (gnus-group-change-level
5374        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5375        gnus-level-default-subscribed gnus-level-killed
5376        (and (gnus-group-group-name)
5377             (gnus-gethash (gnus-group-group-name)
5378                           gnus-newsrc-hashtb))
5379        t)
5380       (gnus-set-active nname (cons 1 0))
5381       (or (gnus-ephemeral-group-p name)
5382           (gnus-dribble-enter
5383            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5384       (gnus-group-insert-group-line-info nname)
5385
5386       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5387         (require (car meth)))
5388       (gnus-check-server meth)
5389       (and (gnus-check-backend-function 'request-create-group nname)
5390            (gnus-request-create-group nname))
5391       t)))
5392
5393 (defun gnus-group-delete-group (group &optional force)
5394   "Delete the current group.
5395 If FORCE (the prefix) is non-nil, all the articles in the group will
5396 be deleted.  This is \"deleted\" as in \"removed forever from the face
5397 of the Earth\".  There is no undo."
5398   (interactive
5399    (list (gnus-group-group-name)
5400          current-prefix-arg))
5401   (or group (error "No group to rename"))
5402   (or (gnus-check-backend-function 'request-delete-group group)
5403       (error "This backend does not support group deletion"))
5404   (prog1
5405       (if (not (gnus-yes-or-no-p
5406                 (format
5407                  "Do you really want to delete %s%s? "
5408                  group (if force " and all its contents" ""))))
5409           () ; Whew!
5410         (gnus-message 6 "Deleting group %s..." group)
5411         (if (not (gnus-request-delete-group group force))
5412             (progn
5413               (gnus-message 3 "Couldn't delete group %s" group)
5414               (ding))
5415           (gnus-message 6 "Deleting group %s...done" group)
5416           (gnus-group-goto-group group)
5417           (gnus-group-kill-group 1 t)
5418           t))
5419     (gnus-group-position-point)))
5420
5421 (defun gnus-group-rename-group (group new-name)
5422   (interactive
5423    (list
5424     (gnus-group-group-name)
5425     (progn
5426       (or (gnus-check-backend-function
5427            'request-rename-group (gnus-group-group-name))
5428           (error "This backend does not support renaming groups"))
5429       (read-string "New group name: "))))
5430
5431   (or (gnus-check-backend-function 'request-rename-group group)
5432       (error "This backend does not support renaming groups"))
5433
5434   (or group (error "No group to rename"))
5435   (and (string-match "^[ \t]*$" new-name)
5436        (error "Not a valid group name"))
5437
5438   ;; We find the proper prefixed name.
5439   (setq new-name
5440         (gnus-group-prefixed-name
5441          (gnus-group-real-name new-name)
5442          (gnus-info-method (gnus-get-info group))))
5443
5444   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5445   (prog1
5446       (if (not (gnus-request-rename-group group new-name))
5447           (progn
5448             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5449             (ding))
5450         ;; We rename the group internally by killing it...
5451         (gnus-group-goto-group group)
5452         (gnus-group-kill-group)
5453         ;; ... changing its name ...
5454         (setcar (cdar gnus-list-of-killed-groups) new-name)
5455         ;; ... and then yanking it.  Magic!
5456         (gnus-group-yank-group)
5457         (gnus-set-active new-name (gnus-active group))
5458         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5459         new-name)
5460     (gnus-group-position-point)))
5461
5462 (defun gnus-group-edit-group (group &optional part)
5463   "Edit the group on the current line."
5464   (interactive (list (gnus-group-group-name)))
5465   (let* ((part (or part 'info))
5466          (done-func `(lambda ()
5467                        "Exit editing mode and update the information."
5468                        (interactive)
5469                        (gnus-group-edit-group-done ',part ,group)))
5470          (winconf (current-window-configuration))
5471          info)
5472     (or group (error "No group on current line"))
5473     (or (setq info (gnus-get-info group))
5474         (error "Killed group; can't be edited"))
5475     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5476     (gnus-configure-windows 'edit-group)
5477     (gnus-add-current-to-buffer-list)
5478     (emacs-lisp-mode)
5479     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5480     (use-local-map (copy-keymap emacs-lisp-mode-map))
5481     (local-set-key "\C-c\C-c" done-func)
5482     (make-local-variable 'gnus-prev-winconf)
5483     (setq gnus-prev-winconf winconf)
5484     (erase-buffer)
5485     (insert
5486      (cond
5487       ((eq part 'method)
5488        ";; Type `C-c C-c' after editing the select method.\n\n")
5489       ((eq part 'params)
5490        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5491       ((eq part 'info)
5492        ";; Type `C-c C-c' after editing the group info.\n\n")))
5493     (insert
5494      (pp-to-string
5495       (cond ((eq part 'method)
5496              (or (gnus-info-method info) "native"))
5497             ((eq part 'params)
5498              (gnus-info-params info))
5499             (t info)))
5500      "\n")))
5501
5502 (defun gnus-group-edit-group-method (group)
5503   "Edit the select method of GROUP."
5504   (interactive (list (gnus-group-group-name)))
5505   (gnus-group-edit-group group 'method))
5506
5507 (defun gnus-group-edit-group-parameters (group)
5508   "Edit the group parameters of GROUP."
5509   (interactive (list (gnus-group-group-name)))
5510   (gnus-group-edit-group group 'params))
5511
5512 (defun gnus-group-edit-group-done (part group)
5513   "Get info from buffer, update variables and jump to the group buffer."
5514   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5515   (goto-char (point-min))
5516   (let* ((form (read (current-buffer)))
5517          (winconf gnus-prev-winconf)
5518          (method (cond ((eq part 'info) (nth 4 form))
5519                        ((eq part 'method) form)
5520                        (t nil)))
5521          (info (cond ((eq part 'info) form)
5522                      ((eq part 'method) (gnus-get-info group))
5523                      (t nil)))
5524          (new-group (if info
5525                       (if (or (not method)
5526                               (gnus-server-equal
5527                                gnus-select-method method))
5528                           (gnus-group-real-name (car info))
5529                         (gnus-group-prefixed-name
5530                          (gnus-group-real-name (car info)) method))
5531                       nil)))
5532     (when (and new-group
5533                (not (equal new-group group)))
5534       (when (gnus-group-goto-group group)
5535         (gnus-group-kill-group 1))
5536       (gnus-activate-group new-group))
5537     ;; Set the info.
5538     (if (and info new-group)
5539         (progn
5540           (setq info (gnus-copy-sequence info))
5541           (setcar info new-group)
5542           (unless (gnus-server-equal method "native")
5543             (unless (nthcdr 3 info)
5544               (nconc info (list nil nil)))
5545             (unless (nthcdr 4 info)
5546               (nconc info (list nil)))
5547             (gnus-info-set-method info method))
5548           (gnus-group-set-info info))
5549       (gnus-group-set-info form (or new-group group) part))
5550     (kill-buffer (current-buffer))
5551     (and winconf (set-window-configuration winconf))
5552     (set-buffer gnus-group-buffer)
5553     (gnus-group-update-group (or new-group group))
5554     (gnus-group-position-point)))
5555
5556 (defun gnus-group-make-help-group ()
5557   "Create the Gnus documentation group."
5558   (interactive)
5559   (let ((path load-path)
5560         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5561         file dir)
5562     (and (gnus-gethash name gnus-newsrc-hashtb)
5563          (error "Documentation group already exists"))
5564     (while path
5565       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5566             file nil)
5567       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5568                 (file-exists-p
5569                  (setq file (concat (file-name-directory
5570                                      (directory-file-name dir))
5571                                     "etc/gnus-tut.txt"))))
5572         (setq path nil)))
5573     (if (not file)
5574         (message "Couldn't find doc group")
5575       (gnus-group-make-group
5576        (gnus-group-real-name name)
5577        (list 'nndoc name
5578              (list 'nndoc-address file)
5579              (list 'nndoc-article-type 'mbox)))))
5580   (gnus-group-position-point))
5581
5582 (defun gnus-group-make-doc-group (file type)
5583   "Create a group that uses a single file as the source."
5584   (interactive
5585    (list (read-file-name "File name: ")
5586          (and current-prefix-arg 'ask)))
5587   (when (eq type 'ask)
5588     (let ((err "")
5589           char found)
5590       (while (not found)
5591         (message
5592          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5593          err)
5594         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5595                           ((= char ?b) 'babyl)
5596                           ((= char ?d) 'digest)
5597                           ((= char ?f) 'forward)
5598                           ((= char ?a) 'mmfd)
5599                           (t (setq err (format "%c unknown. " char))
5600                              nil))))
5601       (setq type found)))
5602   (let* ((file (expand-file-name file))
5603          (name (gnus-generate-new-group-name
5604                 (gnus-group-prefixed-name
5605                  (file-name-nondirectory file) '(nndoc "")))))
5606     (gnus-group-make-group
5607      (gnus-group-real-name name)
5608      (list 'nndoc name
5609            (list 'nndoc-address file)
5610            (list 'nndoc-article-type (or type 'guess))))
5611     (forward-line -1)
5612     (gnus-group-position-point)))
5613
5614 (defun gnus-group-make-archive-group (&optional all)
5615   "Create the (ding) Gnus archive group of the most recent articles.
5616 Given a prefix, create a full group."
5617   (interactive "P")
5618   (let ((group (gnus-group-prefixed-name
5619                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5620     (and (gnus-gethash group gnus-newsrc-hashtb)
5621          (error "Archive group already exists"))
5622     (gnus-group-make-group
5623      (gnus-group-real-name group)
5624      (list 'nndir (if all "hpc" "edu")
5625            (list 'nndir-directory
5626                  (if all gnus-group-archive-directory
5627                    gnus-group-recent-archive-directory)))))
5628   (forward-line -1)
5629   (gnus-group-position-point))
5630
5631 (defun gnus-group-make-directory-group (dir)
5632   "Create an nndir group.
5633 The user will be prompted for a directory.  The contents of this
5634 directory will be used as a newsgroup.  The directory should contain
5635 mail messages or news articles in files that have numeric names."
5636   (interactive
5637    (list (read-file-name "Create group from directory: ")))
5638   (or (file-exists-p dir) (error "No such directory"))
5639   (or (file-directory-p dir) (error "Not a directory"))
5640   (let ((ext "")
5641         (i 0)
5642         group)
5643     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5644       (setq group
5645             (gnus-group-prefixed-name
5646              (concat (file-name-as-directory (directory-file-name dir))
5647                      ext)
5648              '(nndir "")))
5649       (setq ext (format "<%d>" (setq i (1+ i)))))
5650     (gnus-group-make-group
5651      (gnus-group-real-name group)
5652      (list 'nndir group (list 'nndir-directory dir))))
5653   (forward-line -1)
5654   (gnus-group-position-point))
5655
5656 (defun gnus-group-make-kiboze-group (group address scores)
5657   "Create an nnkiboze group.
5658 The user will be prompted for a name, a regexp to match groups, and
5659 score file entries for articles to include in the group."
5660   (interactive
5661    (list
5662     (read-string "nnkiboze group name: ")
5663     (read-string "Source groups (regexp): ")
5664     (let ((headers (mapcar (lambda (group) (list group))
5665                            '("subject" "from" "number" "date" "message-id"
5666                              "references" "chars" "lines" "xref"
5667                              "followup" "all" "body" "head")))
5668           scores header regexp regexps)
5669       (while (not (equal "" (setq header (completing-read
5670                                           "Match on header: " headers nil t))))
5671         (setq regexps nil)
5672         (while (not (equal "" (setq regexp (read-string
5673                                             (format "Match on %s (string): "
5674                                                     header)))))
5675           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5676         (setq scores (cons (cons header regexps) scores)))
5677       scores)))
5678   (gnus-group-make-group group "nnkiboze" address)
5679   (save-excursion
5680     (gnus-set-work-buffer)
5681     (let (emacs-lisp-mode-hook)
5682       (pp scores (current-buffer)))
5683     (write-region (point-min) (point-max)
5684                   (gnus-score-file-name (concat "nnkiboze:" group))))
5685   (forward-line -1)
5686   (gnus-group-position-point))
5687
5688 (defun gnus-group-add-to-virtual (n vgroup)
5689   "Add the current group to a virtual group."
5690   (interactive
5691    (list current-prefix-arg
5692          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5693                           "nnvirtual:")))
5694   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5695       (error "%s is not an nnvirtual group" vgroup))
5696   (let* ((groups (gnus-group-process-prefix n))
5697          (method (gnus-info-method (gnus-get-info vgroup))))
5698     (setcar (cdr method)
5699             (concat
5700              (nth 1 method) "\\|"
5701              (mapconcat
5702               (lambda (s)
5703                 (gnus-group-remove-mark s)
5704                 (concat "\\(^" (regexp-quote s) "$\\)"))
5705               groups "\\|"))))
5706   (gnus-group-position-point))
5707
5708 (defun gnus-group-make-empty-virtual (group)
5709   "Create a new, fresh, empty virtual group."
5710   (interactive "sCreate new, empty virtual group: ")
5711   (let* ((method (list 'nnvirtual "^$"))
5712          (pgroup (gnus-group-prefixed-name group method)))
5713     ;; Check whether it exists already.
5714     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5715          (error "Group %s already exists." pgroup))
5716     ;; Subscribe the new group after the group on the current line.
5717     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5718     (gnus-group-update-group pgroup)
5719     (forward-line -1)
5720     (gnus-group-position-point)))
5721
5722 (defun gnus-group-enter-directory (dir)
5723   "Enter an ephemeral nneething group."
5724   (interactive "DDirectory to read: ")
5725   (let* ((method (list 'nneething dir))
5726          (leaf (gnus-group-prefixed-name
5727                 (file-name-nondirectory (directory-file-name dir))
5728                 method))
5729          (name (gnus-generate-new-group-name leaf)))
5730     (let ((nneething-read-only t))
5731       (or (gnus-group-read-ephemeral-group
5732            name method t
5733            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5734                                       'summary 'group)))
5735           (error "Couldn't enter %s" dir)))))
5736
5737 ;; Group sorting commands
5738 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5739
5740 (defun gnus-group-sort-groups (func &optional reverse)
5741   "Sort the group buffer according to FUNC.
5742 If REVERSE, reverse the sorting order."
5743   (interactive (list gnus-group-sort-function
5744                      current-prefix-arg))
5745   (let ((func (cond 
5746                ((not (listp func)) func)
5747                ((null func) func)
5748                ((= 1 (length func)) (car func))
5749                (t `(lambda (t1 t2)
5750                      ,(gnus-make-sort-function 
5751                        (reverse func)))))))
5752     ;; We peel off the dummy group from the alist.
5753     (when func
5754       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5755         (pop gnus-newsrc-alist))
5756       ;; Do the sorting.
5757       (setq gnus-newsrc-alist
5758             (sort gnus-newsrc-alist func))
5759       (when reverse
5760         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5761       ;; Regenerate the hash table.
5762       (gnus-make-hashtable-from-newsrc-alist)
5763       (gnus-group-list-groups))))
5764
5765 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5766   "Sort the group buffer alphabetically by group name.
5767 If REVERSE, sort in reverse order."
5768   (interactive "P")
5769   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5770
5771 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5772   "Sort the group buffer by number of unread articles.
5773 If REVERSE, sort in reverse order."
5774   (interactive "P")
5775   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5776
5777 (defun gnus-group-sort-groups-by-level (&optional reverse)
5778   "Sort the group buffer by group level.
5779 If REVERSE, sort in reverse order."
5780   (interactive "P")
5781   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5782
5783 (defun gnus-group-sort-groups-by-score (&optional reverse)
5784   "Sort the group buffer by group score.
5785 If REVERSE, sort in reverse order."
5786   (interactive "P")
5787   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5788
5789 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5790   "Sort the group buffer by group rank.
5791 If REVERSE, sort in reverse order."
5792   (interactive "P")
5793   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5794
5795 (defun gnus-group-sort-groups-by-method (&optional reverse)
5796   "Sort the group buffer alphabetically by backend name.
5797 If REVERSE, sort in reverse order."
5798   (interactive "P")
5799   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5800
5801 (defun gnus-group-sort-by-alphabet (info1 info2)
5802   "Sort alphabetically."
5803   (string< (gnus-info-group info1) (gnus-info-group info2)))
5804
5805 (defun gnus-group-sort-by-unread (info1 info2)
5806   "Sort by number of unread articles."
5807   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5808         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5809     (< (or (and (numberp n1) n1) 0)
5810        (or (and (numberp n2) n2) 0))))
5811
5812 (defun gnus-group-sort-by-level (info1 info2)
5813   "Sort by level."
5814   (< (gnus-info-level info1) (gnus-info-level info2)))
5815
5816 (defun gnus-group-sort-by-method (info1 info2)
5817   "Sort alphabetically by backend name."
5818   (string< (symbol-name (car (gnus-find-method-for-group
5819                               (gnus-info-group info1) info1)))
5820            (symbol-name (car (gnus-find-method-for-group
5821                               (gnus-info-group info2) info2)))))
5822
5823 (defun gnus-group-sort-by-score (info1 info2)
5824   "Sort by group score."
5825   (< (gnus-info-score info1) (gnus-info-score info2)))
5826
5827 (defun gnus-group-sort-by-rank (info1 info2)
5828   "Sort by level and score."
5829   (let ((level1 (gnus-info-level info1))
5830         (level2 (gnus-info-level info2)))
5831     (or (< level1 level2)
5832         (and (= level1 level2)
5833              (< (gnus-info-score info1) (gnus-info-score info2))))))
5834
5835 ;; Group catching up.
5836
5837 (defun gnus-group-catchup-current (&optional n all)
5838   "Mark all articles not marked as unread in current newsgroup as read.
5839 If prefix argument N is numeric, the ARG next newsgroups will be
5840 caught up.  If ALL is non-nil, marked articles will also be marked as
5841 read.  Cross references (Xref: header) of articles are ignored.
5842 The difference between N and actual number of newsgroups that were
5843 caught up is returned."
5844   (interactive "P")
5845   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5846                gnus-expert-user
5847                (gnus-y-or-n-p
5848                 (if all
5849                     "Do you really want to mark all articles as read? "
5850                   "Mark all unread articles as read? "))))
5851       n
5852     (let ((groups (gnus-group-process-prefix n))
5853           (ret 0))
5854       (while groups
5855         ;; Virtual groups have to be given special treatment.
5856         (let ((method (gnus-find-method-for-group (car groups))))
5857           (if (eq 'nnvirtual (car method))
5858               (nnvirtual-catchup-group
5859                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5860         (gnus-group-remove-mark (car groups))
5861         (if (prog1
5862                 (gnus-group-goto-group (car groups))
5863               (gnus-group-catchup (car groups) all))
5864             (gnus-group-update-group-line)
5865           (setq ret (1+ ret)))
5866         (setq groups (cdr groups)))
5867       (gnus-group-next-unread-group 1)
5868       ret)))
5869
5870 (defun gnus-group-catchup-current-all (&optional n)
5871   "Mark all articles in current newsgroup as read.
5872 Cross references (Xref: header) of articles are ignored."
5873   (interactive "P")
5874   (gnus-group-catchup-current n 'all))
5875
5876 (defun gnus-group-catchup (group &optional all)
5877   "Mark all articles in GROUP as read.
5878 If ALL is non-nil, all articles are marked as read.
5879 The return value is the number of articles that were marked as read,
5880 or nil if no action could be taken."
5881   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5882          (num (car entry)))
5883     ;; Do the updating only if the newsgroup isn't killed.
5884     (if (not (numberp (car entry)))
5885         (gnus-message 1 "Can't catch up; non-active group")
5886       ;; Do auto-expirable marks if that's required.
5887       (when (gnus-group-auto-expirable-p group)
5888         (gnus-add-marked-articles
5889          group 'expire (gnus-list-of-unread-articles group))
5890         (when all
5891           (let ((marks (nth 3 (nth 2 entry))))
5892             (gnus-add-marked-articles
5893              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5894             (gnus-add-marked-articles
5895              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5896       (when entry
5897         (gnus-update-read-articles group nil)
5898         ;; Also nix out the lists of marks and dormants.
5899         (when all
5900           (gnus-add-marked-articles group 'tick nil nil 'force)
5901           (gnus-add-marked-articles group 'dormant nil nil 'force))
5902         (run-hooks 'gnus-group-catchup-group-hook)
5903         num))))
5904
5905 (defun gnus-group-expire-articles (&optional n)
5906   "Expire all expirable articles in the current newsgroup."
5907   (interactive "P")
5908   (let ((groups (gnus-group-process-prefix n))
5909         group)
5910     (unless groups
5911       (error "No groups to expire"))
5912     (while (setq group (pop groups))
5913       (gnus-group-remove-mark group)
5914       (when (gnus-check-backend-function 'request-expire-articles group)
5915         (gnus-message 6 "Expiring articles in %s..." group)
5916         (let* ((info (gnus-get-info group))
5917                (expirable (if (gnus-group-total-expirable-p group)
5918                               (cons nil (gnus-list-of-read-articles group))
5919                             (assq 'expire (gnus-info-marks info))))
5920                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5921           (when expirable
5922             (setcdr
5923              expirable
5924              (gnus-compress-sequence
5925               (if expiry-wait
5926                   ;; We set the expiry variables to the groupp
5927                   ;; parameter. 
5928                   (let ((nnmail-expiry-wait-function nil)
5929                         (nnmail-expiry-wait expiry-wait))
5930                     (gnus-request-expire-articles
5931                      (gnus-uncompress-sequence (cdr expirable)) group))
5932                 ;; Just expire using the normal expiry values.
5933                 (gnus-request-expire-articles
5934                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5935           (gnus-message 6 "Expiring articles in %s...done" group)))
5936       (gnus-group-position-point))))
5937
5938 (defun gnus-group-expire-all-groups ()
5939   "Expire all expirable articles in all newsgroups."
5940   (interactive)
5941   (save-excursion
5942     (gnus-message 5 "Expiring...")
5943     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5944                                      (cdr gnus-newsrc-alist))))
5945       (gnus-group-expire-articles nil)))
5946   (gnus-group-position-point)
5947   (gnus-message 5 "Expiring...done"))
5948
5949 (defun gnus-group-set-current-level (n level)
5950   "Set the level of the next N groups to LEVEL."
5951   (interactive
5952    (list
5953     current-prefix-arg
5954     (string-to-int
5955      (let ((s (read-string
5956                (format "Level (default %s): "
5957                        (or (gnus-group-group-level) 
5958                            gnus-level-default-subscribed)))))
5959        (if (string-match "^\\s-*$" s)
5960            (int-to-string (or (gnus-group-group-level) 
5961                               gnus-level-default-subscribed))
5962          s)))))
5963   (or (and (>= level 1) (<= level gnus-level-killed))
5964       (error "Illegal level: %d" level))
5965   (let ((groups (gnus-group-process-prefix n))
5966         group)
5967     (while (setq group (pop groups))
5968       (gnus-group-remove-mark group)
5969       (gnus-message 6 "Changed level of %s from %d to %d"
5970                     group (or (gnus-group-group-level) gnus-level-killed)
5971                     level)
5972       (gnus-group-change-level
5973        group level (or (gnus-group-group-level) gnus-level-killed))
5974       (gnus-group-update-group-line)))
5975   (gnus-group-position-point))
5976
5977 (defun gnus-group-unsubscribe-current-group (&optional n)
5978   "Toggle subscription of the current group.
5979 If given numerical prefix, toggle the N next groups."
5980   (interactive "P")
5981   (let ((groups (gnus-group-process-prefix n))
5982         group)
5983     (while groups
5984       (setq group (car groups)
5985             groups (cdr groups))
5986       (gnus-group-remove-mark group)
5987       (gnus-group-unsubscribe-group
5988        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5989                  gnus-level-default-unsubscribed
5990                gnus-level-default-subscribed) t)
5991       (gnus-group-update-group-line))
5992     (gnus-group-next-group 1)))
5993
5994 (defun gnus-group-unsubscribe-group (group &optional level silent)
5995   "Toggle subscription to GROUP.
5996 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5997 group line."
5998   (interactive
5999    (list (completing-read
6000           "Group: " gnus-active-hashtb nil
6001           (memq gnus-select-method gnus-have-read-active-file))))
6002   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6003     (cond
6004      ((string-match "^[ \t]$" group)
6005       (error "Empty group name"))
6006      (newsrc
6007       ;; Toggle subscription flag.
6008       (gnus-group-change-level
6009        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6010                                       gnus-level-subscribed)
6011                                   (1+ gnus-level-subscribed)
6012                                 gnus-level-default-subscribed)))
6013       (unless silent
6014         (gnus-group-update-group group)))
6015      ((and (stringp group)
6016            (or (not (memq gnus-select-method gnus-have-read-active-file))
6017                (gnus-active group)))
6018       ;; Add new newsgroup.
6019       (gnus-group-change-level
6020        group
6021        (if level level gnus-level-default-subscribed)
6022        (or (and (member group gnus-zombie-list)
6023                 gnus-level-zombie)
6024            gnus-level-killed)
6025        (and (gnus-group-group-name)
6026             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6027       (unless silent
6028         (gnus-group-update-group group)))
6029      (t (error "No such newsgroup: %s" group)))
6030     (gnus-group-position-point)))
6031
6032 (defun gnus-group-transpose-groups (n)
6033   "Move the current newsgroup up N places.
6034 If given a negative prefix, move down instead.  The difference between
6035 N and the number of steps taken is returned."
6036   (interactive "p")
6037   (or (gnus-group-group-name)
6038       (error "No group on current line"))
6039   (gnus-group-kill-group 1)
6040   (prog1
6041       (forward-line (- n))
6042     (gnus-group-yank-group)
6043     (gnus-group-position-point)))
6044
6045 (defun gnus-group-kill-all-zombies ()
6046   "Kill all zombie newsgroups."
6047   (interactive)
6048   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6049   (setq gnus-zombie-list nil)
6050   (gnus-group-list-groups))
6051
6052 (defun gnus-group-kill-region (begin end)
6053   "Kill newsgroups in current region (excluding current point).
6054 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6055   (interactive "r")
6056   (let ((lines
6057          ;; Count lines.
6058          (save-excursion
6059            (count-lines
6060             (progn
6061               (goto-char begin)
6062               (beginning-of-line)
6063               (point))
6064             (progn
6065               (goto-char end)
6066               (beginning-of-line)
6067               (point))))))
6068     (goto-char begin)
6069     (beginning-of-line)                 ;Important when LINES < 1
6070     (gnus-group-kill-group lines)))
6071
6072 (defun gnus-group-kill-group (&optional n discard)
6073   "Kill the next N groups.
6074 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6075 However, only groups that were alive can be yanked; already killed
6076 groups or zombie groups can't be yanked.
6077 The return value is the name of the group that was killed, or a list
6078 of groups killed."
6079   (interactive "P")
6080   (let ((buffer-read-only nil)
6081         (groups (gnus-group-process-prefix n))
6082         group entry level out)
6083     (if (< (length groups) 10)
6084         ;; This is faster when there are few groups.
6085         (while groups
6086           (push (setq group (pop groups)) out)
6087           (gnus-group-remove-mark group)
6088           (setq level (gnus-group-group-level))
6089           (gnus-delete-line)
6090           (when (and (not discard)
6091                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6092             (push (cons (car entry) (nth 2 entry))
6093                   gnus-list-of-killed-groups))
6094           (gnus-group-change-level
6095            (if entry entry group) gnus-level-killed (if entry nil level)))
6096       ;; If there are lots and lots of groups to be killed, we use
6097       ;; this thing instead.
6098       (let (entry)
6099         (setq groups (nreverse groups))
6100         (while groups
6101           (gnus-group-remove-mark (setq group (pop groups)))
6102           (gnus-delete-line)
6103           (cond
6104            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6105             (push (cons (car entry) (nth 2 entry))
6106                   gnus-list-of-killed-groups)
6107             (setcdr (cdr entry) (cdddr entry)))
6108            ((member group gnus-zombie-list)
6109             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6110         (gnus-make-hashtable-from-newsrc-alist)))
6111
6112     (gnus-group-position-point)
6113     (if (< (length out) 2) (car out) (nreverse out))))
6114
6115 (defun gnus-group-yank-group (&optional arg)
6116   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6117 inserting it before the current newsgroup.  The numeric ARG specifies
6118 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6119 is returned, or (if several groups are yanked) a list of yanked groups
6120 is returned."
6121   (interactive "p")
6122   (setq arg (or arg 1))
6123   (let (info group prev out)
6124     (while (>= (decf arg) 0)
6125       (if (not (setq info (pop gnus-list-of-killed-groups)))
6126           (error "No more newsgroups to yank"))
6127       (push (setq group (nth 1 info)) out)
6128       ;; Find which newsgroup to insert this one before - search
6129       ;; backward until something suitable is found.  If there are no
6130       ;; other newsgroups in this buffer, just make this newsgroup the
6131       ;; first newsgroup.
6132       (setq prev (gnus-group-group-name))
6133       (gnus-group-change-level
6134        info (gnus-info-level (cdr info)) gnus-level-killed
6135        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6136        t)
6137       (gnus-group-insert-group-line-info group))
6138     (forward-line -1)
6139     (gnus-group-position-point)
6140     (if (< (length out) 2) (car out) (nreverse out))))
6141
6142 (defun gnus-group-kill-level (level)
6143   "Kill all groups that is on a certain LEVEL."
6144   (interactive "nKill all groups on level: ")
6145   (cond
6146    ((= level gnus-level-zombie)
6147     (setq gnus-killed-list
6148           (nconc gnus-zombie-list gnus-killed-list))
6149     (setq gnus-zombie-list nil))
6150    ((and (< level gnus-level-zombie)
6151          (> level 0)
6152          (or gnus-expert-user
6153              (gnus-yes-or-no-p
6154               (format
6155                "Do you really want to kill all groups on level %d? "
6156                level))))
6157     (let* ((prev gnus-newsrc-alist)
6158            (alist (cdr prev)))
6159       (while alist
6160         (if (= (gnus-info-level level) level)
6161             (setcdr prev (cdr alist))
6162           (setq prev alist))
6163         (setq alist (cdr alist)))
6164       (gnus-make-hashtable-from-newsrc-alist)
6165       (gnus-group-list-groups)))
6166    (t
6167     (error "Can't kill; illegal level: %d" level))))
6168
6169 (defun gnus-group-list-all-groups (&optional arg)
6170   "List all newsgroups with level ARG or lower.
6171 Default is gnus-level-unsubscribed, which lists all subscribed and most
6172 unsubscribed groups."
6173   (interactive "P")
6174   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6175
6176 ;; Redefine this to list ALL killed groups if prefix arg used.
6177 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6178 (defun gnus-group-list-killed (&optional arg)
6179   "List all killed newsgroups in the group buffer.
6180 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6181 entail asking the server for the groups."
6182   (interactive "P")
6183   ;; Find all possible killed newsgroups if arg.
6184   (when arg
6185     ;; First make sure active file has been read.
6186     (unless gnus-have-read-active-file
6187       (let ((gnus-read-active-file t))
6188         (gnus-read-active-file)))
6189     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6190     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6191     (mapatoms
6192      (lambda (sym)
6193        (let ((groups 0)
6194              (group (symbol-name sym)))
6195          (if (or (null group)
6196                  (gnus-gethash group gnus-killed-hashtb)
6197                  (gnus-gethash group gnus-newsrc-hashtb))
6198              ()
6199            (let ((do-sub (gnus-matches-options-n group)))
6200              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6201                  ()
6202                (setq groups (1+ groups))
6203                (setq gnus-killed-list
6204                      (cons group gnus-killed-list))
6205                (gnus-sethash group group gnus-killed-hashtb))))))
6206      gnus-active-hashtb))
6207   (if (not gnus-killed-list)
6208       (gnus-message 6 "No killed groups")
6209     (let (gnus-group-list-mode)
6210       (funcall gnus-group-prepare-function
6211                gnus-level-killed t gnus-level-killed))
6212     (goto-char (point-min)))
6213   (gnus-group-position-point))
6214
6215 (defun gnus-group-list-zombies ()
6216   "List all zombie newsgroups in the group buffer."
6217   (interactive)
6218   (if (not gnus-zombie-list)
6219       (gnus-message 6 "No zombie groups")
6220     (let (gnus-group-list-mode)
6221       (funcall gnus-group-prepare-function
6222                gnus-level-zombie t gnus-level-zombie))
6223     (goto-char (point-min)))
6224   (gnus-group-position-point))
6225
6226 (defun gnus-group-list-active ()
6227   "List all groups that are available from the server(s)."
6228   (interactive)
6229   ;; First we make sure that we have really read the active file.
6230   (unless gnus-have-read-active-file
6231     (let ((gnus-read-active-file t))
6232       (gnus-read-active-file)))
6233   ;; Find all groups and sort them.
6234   (let ((groups
6235          (sort
6236           (let (list)
6237             (mapatoms
6238              (lambda (sym)
6239                (and (symbol-value sym)
6240                     (setq list (cons (symbol-name sym) list))))
6241              gnus-active-hashtb)
6242             list)
6243           'string<))
6244         (buffer-read-only nil))
6245     (erase-buffer)
6246     (while groups
6247       (gnus-group-insert-group-line-info (car groups))
6248       (setq groups (cdr groups)))
6249     (goto-char (point-min))))
6250
6251 (defun gnus-activate-all-groups (level)
6252   "Activate absolutely all groups."
6253   (interactive (list 7))
6254   (let ((gnus-activate-level level)
6255         (gnus-activate-foreign-newsgroups level))
6256     (gnus-group-get-new-news)))
6257
6258 (defun gnus-group-get-new-news (&optional arg)
6259   "Get newly arrived articles.
6260 If ARG is a number, it specifies which levels you are interested in
6261 re-scanning.  If ARG is non-nil and not a number, this will force
6262 \"hard\" re-reading of the active files from all servers."
6263   (interactive "P")
6264   (run-hooks 'gnus-get-new-news-hook)
6265   ;; We might read in new NoCeM messages here.
6266   (when (and gnus-use-nocem 
6267              (null arg))
6268     (gnus-nocem-scan-groups))
6269   ;; If ARG is not a number, then we read the active file.
6270   (when (and arg (not (numberp arg)))
6271     (let ((gnus-read-active-file t))
6272       (gnus-read-active-file))
6273     (setq arg nil))
6274
6275   (setq arg (gnus-group-default-level arg t))
6276   (if (and gnus-read-active-file (not arg))
6277       (progn
6278         (gnus-read-active-file)
6279         (gnus-get-unread-articles arg))
6280     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6281       (gnus-get-unread-articles arg)))
6282   (run-hooks 'gnus-after-getting-new-news-hook)
6283   (gnus-group-list-groups))
6284
6285 (defun gnus-group-get-new-news-this-group (&optional n)
6286   "Check for newly arrived news in the current group (and the N-1 next groups).
6287 The difference between N and the number of newsgroup checked is returned.
6288 If N is negative, this group and the N-1 previous groups will be checked."
6289   (interactive "P")
6290   (let* ((groups (gnus-group-process-prefix n))
6291          (ret (if (numberp n) (- n (length groups)) 0))
6292          group)
6293     (while groups
6294       (setq group (car groups)
6295             groups (cdr groups))
6296       (gnus-group-remove-mark group)
6297       (unless (gnus-get-new-news-in-group group)
6298         (ding)
6299         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6300     (when gnus-goto-next-group-when-activating
6301       (gnus-group-next-unread-group 1 t))
6302     (gnus-summary-position-point)
6303     ret))
6304
6305 (defun gnus-get-new-news-in-group (group)
6306   (when (and group (gnus-activate-group group 'scan))
6307     (gnus-get-unread-articles-in-group
6308      (gnus-get-info group) (gnus-active group) t)
6309     (gnus-close-group group)
6310     (when (gnus-group-goto-group group)
6311       (gnus-group-update-group-line))
6312     t))
6313
6314 (defun gnus-group-fetch-faq (group &optional faq-dir)
6315   "Fetch the FAQ for the current group."
6316   (interactive
6317    (list
6318     (gnus-group-real-name (gnus-group-group-name))
6319     (cond (current-prefix-arg
6320            (completing-read
6321             "Faq dir: " (and (listp gnus-group-faq-directory)
6322                              gnus-group-faq-directory))))))
6323   (or faq-dir
6324       (setq faq-dir (if (listp gnus-group-faq-directory)
6325                         (car gnus-group-faq-directory)
6326                       gnus-group-faq-directory)))
6327   (or group (error "No group name given"))
6328   (let ((file (concat (file-name-as-directory faq-dir)
6329                       (gnus-group-real-name group))))
6330     (if (not (file-exists-p file))
6331         (error "No such file: %s" file)
6332       (find-file file))))
6333
6334 (defun gnus-group-describe-group (force &optional group)
6335   "Display a description of the current newsgroup."
6336   (interactive (list current-prefix-arg (gnus-group-group-name)))
6337   (and force (setq gnus-description-hashtb nil))
6338   (let ((method (gnus-find-method-for-group group))
6339         desc)
6340     (or group (error "No group name given"))
6341     (and (or (and gnus-description-hashtb
6342                   ;; We check whether this group's method has been
6343                   ;; queried for a description file.
6344                   (gnus-gethash
6345                    (gnus-group-prefixed-name "" method)
6346                    gnus-description-hashtb))
6347              (setq desc (gnus-group-get-description group))
6348              (gnus-read-descriptions-file method))
6349          (message
6350           (or desc (gnus-gethash group gnus-description-hashtb)
6351               "No description available")))))
6352
6353 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6354 (defun gnus-group-describe-all-groups (&optional force)
6355   "Pop up a buffer with descriptions of all newsgroups."
6356   (interactive "P")
6357   (and force (setq gnus-description-hashtb nil))
6358   (if (not (or gnus-description-hashtb
6359                (gnus-read-all-descriptions-files)))
6360       (error "Couldn't request descriptions file"))
6361   (let ((buffer-read-only nil)
6362         b)
6363     (erase-buffer)
6364     (mapatoms
6365      (lambda (group)
6366        (setq b (point))
6367        (insert (format "      *: %-20s %s\n" (symbol-name group)
6368                        (symbol-value group)))
6369        (add-text-properties
6370         b (1+ b) (list 'gnus-group group
6371                        'gnus-unread t 'gnus-marked nil
6372                        'gnus-level (1+ gnus-level-subscribed))))
6373      gnus-description-hashtb)
6374     (goto-char (point-min))
6375     (gnus-group-position-point)))
6376
6377 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6378 (defun gnus-group-apropos (regexp &optional search-description)
6379   "List all newsgroups that have names that match a regexp."
6380   (interactive "sGnus apropos (regexp): ")
6381   (let ((prev "")
6382         (obuf (current-buffer))
6383         groups des)
6384     ;; Go through all newsgroups that are known to Gnus.
6385     (mapatoms
6386      (lambda (group)
6387        (and (symbol-name group)
6388             (string-match regexp (symbol-name group))
6389             (setq groups (cons (symbol-name group) groups))))
6390      gnus-active-hashtb)
6391     ;; Go through all descriptions that are known to Gnus.
6392     (if search-description
6393         (mapatoms
6394          (lambda (group)
6395            (and (string-match regexp (symbol-value group))
6396                 (gnus-active (symbol-name group))
6397                 (setq groups (cons (symbol-name group) groups))))
6398          gnus-description-hashtb))
6399     (if (not groups)
6400         (gnus-message 3 "No groups matched \"%s\"." regexp)
6401       ;; Print out all the groups.
6402       (save-excursion
6403         (pop-to-buffer "*Gnus Help*")
6404         (buffer-disable-undo (current-buffer))
6405         (erase-buffer)
6406         (setq groups (sort groups 'string<))
6407         (while groups
6408           ;; Groups may be entered twice into the list of groups.
6409           (if (not (string= (car groups) prev))
6410               (progn
6411                 (insert (setq prev (car groups)) "\n")
6412                 (if (and gnus-description-hashtb
6413                          (setq des (gnus-gethash (car groups)
6414                                                  gnus-description-hashtb)))
6415                     (insert "  " des "\n"))))
6416           (setq groups (cdr groups)))
6417         (goto-char (point-min))))
6418     (pop-to-buffer obuf)))
6419
6420 (defun gnus-group-description-apropos (regexp)
6421   "List all newsgroups that have names or descriptions that match a regexp."
6422   (interactive "sGnus description apropos (regexp): ")
6423   (if (not (or gnus-description-hashtb
6424                (gnus-read-all-descriptions-files)))
6425       (error "Couldn't request descriptions file"))
6426   (gnus-group-apropos regexp t))
6427
6428 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6429 (defun gnus-group-list-matching (level regexp &optional all lowest)
6430   "List all groups with unread articles that match REGEXP.
6431 If the prefix LEVEL is non-nil, it should be a number that says which
6432 level to cut off listing groups.
6433 If ALL, also list groups with no unread articles.
6434 If LOWEST, don't list groups with level lower than LOWEST."
6435   (interactive "P\nsList newsgroups matching: ")
6436   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6437                            all (or lowest 1) regexp)
6438   (goto-char (point-min))
6439   (gnus-group-position-point))
6440
6441 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6442   "List all groups that match REGEXP.
6443 If the prefix LEVEL is non-nil, it should be a number that says which
6444 level to cut off listing groups.
6445 If LOWEST, don't list groups with level lower than LOWEST."
6446   (interactive "P\nsList newsgroups matching: ")
6447   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6448
6449 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6450 (defun gnus-group-save-newsrc (&optional force)
6451   "Save the Gnus startup files.
6452 If FORCE, force saving whether it is necessary or not."
6453   (interactive "P")
6454   (gnus-save-newsrc-file force))
6455
6456 (defun gnus-group-restart (&optional arg)
6457   "Force Gnus to read the .newsrc file."
6458   (interactive "P")
6459   (when (gnus-yes-or-no-p
6460          (format "Are you sure you want to read %s? "
6461                  gnus-current-startup-file))
6462     (gnus-save-newsrc-file)
6463     (gnus-setup-news 'force)
6464     (gnus-group-list-groups arg)))
6465
6466 (defun gnus-group-read-init-file ()
6467   "Read the Gnus elisp init file."
6468   (interactive)
6469   (gnus-read-init-file))
6470
6471 (defun gnus-group-check-bogus-groups (&optional silent)
6472   "Check bogus newsgroups.
6473 If given a prefix, don't ask for confirmation before removing a bogus
6474 group."
6475   (interactive "P")
6476   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6477   (gnus-group-list-groups))
6478
6479 (defun gnus-group-edit-global-kill (&optional article group)
6480   "Edit the global kill file.
6481 If GROUP, edit that local kill file instead."
6482   (interactive "P")
6483   (setq gnus-current-kill-article article)
6484   (gnus-kill-file-edit-file group)
6485   (gnus-message
6486    6
6487    (substitute-command-keys
6488     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6489             (if group "local" "global")))))
6490
6491 (defun gnus-group-edit-local-kill (article group)
6492   "Edit a local kill file."
6493   (interactive (list nil (gnus-group-group-name)))
6494   (gnus-group-edit-global-kill article group))
6495
6496 (defun gnus-group-force-update ()
6497   "Update `.newsrc' file."
6498   (interactive)
6499   (gnus-save-newsrc-file))
6500
6501 (defun gnus-group-suspend ()
6502   "Suspend the current Gnus session.
6503 In fact, cleanup buffers except for group mode buffer.
6504 The hook gnus-suspend-gnus-hook is called before actually suspending."
6505   (interactive)
6506   (run-hooks 'gnus-suspend-gnus-hook)
6507   ;; Kill Gnus buffers except for group mode buffer.
6508   (let ((group-buf (get-buffer gnus-group-buffer)))
6509     ;; Do this on a separate list in case the user does a ^G before we finish
6510     (let ((gnus-buffer-list
6511            (delq group-buf (delq gnus-dribble-buffer
6512                                  (append gnus-buffer-list nil)))))
6513       (while gnus-buffer-list
6514         (gnus-kill-buffer (car gnus-buffer-list))
6515         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6516     (if group-buf
6517         (progn
6518           (setq gnus-buffer-list (list group-buf))
6519           (bury-buffer group-buf)
6520           (delete-windows-on group-buf t)))))
6521
6522 (defun gnus-group-clear-dribble ()
6523   "Clear all information from the dribble buffer."
6524   (interactive)
6525   (gnus-dribble-clear)
6526   (gnus-message 7 "Cleared dribble buffer"))
6527
6528 (defun gnus-group-exit ()
6529   "Quit reading news after updating .newsrc.eld and .newsrc.
6530 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6531   (interactive)
6532   (when 
6533       (or noninteractive                ;For gnus-batch-kill
6534           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6535           (not gnus-interactive-exit)   ;Without confirmation
6536           gnus-expert-user
6537           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6538     (run-hooks 'gnus-exit-gnus-hook)
6539     ;; Close down GroupLens.
6540     (when gnus-use-grouplens
6541       (bbb-logout))
6542     ;; Offer to save data from non-quitted summary buffers.
6543     (gnus-offer-save-summaries)
6544     ;; Save the newsrc file(s).
6545     (gnus-save-newsrc-file)
6546     ;; Kill-em-all.
6547     (gnus-close-backends)
6548     ;; Shut down the cache.
6549     (when gnus-use-cache
6550       (gnus-cache-close))
6551     ;; Reset everything.
6552     (gnus-clear-system)
6553     ;; Allow the user to do things after cleaning up.
6554     (run-hooks 'gnus-after-exiting-gnus-hook)))
6555
6556 (defun gnus-close-backends ()
6557   ;; Send a close request to all backends that support such a request.
6558   (let ((methods gnus-valid-select-methods)
6559         func)
6560     (while methods
6561       (if (fboundp (setq func (intern (concat (caar methods)
6562                                               "-request-close"))))
6563           (funcall func))
6564       (setq methods (cdr methods)))))
6565
6566 (defun gnus-group-quit ()
6567   "Quit reading news without updating .newsrc.eld or .newsrc.
6568 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6569   (interactive)
6570   (when (or noninteractive              ;For gnus-batch-kill
6571             (zerop (buffer-size))
6572             (not (gnus-server-opened gnus-select-method))
6573             gnus-expert-user
6574             (not gnus-current-startup-file)
6575             (gnus-yes-or-no-p
6576              (format "Quit reading news without saving %s? "
6577                      (file-name-nondirectory gnus-current-startup-file))))
6578     (run-hooks 'gnus-exit-gnus-hook)
6579     (if gnus-use-full-window
6580         (delete-other-windows)
6581       (gnus-remove-some-windows))
6582     (gnus-dribble-save)
6583     (gnus-close-backends)
6584     ;; Shut down the cache.
6585     (when gnus-use-cache
6586       (gnus-cache-close))
6587     (gnus-clear-system)
6588     ;; Allow the user to do things after cleaning up.
6589     (run-hooks 'gnus-after-exiting-gnus-hook)))
6590
6591 (defun gnus-offer-save-summaries ()
6592   "Offer to save all active summary buffers."
6593   (save-excursion
6594     (let ((buflist (buffer-list))
6595           buffers bufname)
6596       ;; Go through all buffers and find all summaries.
6597       (while buflist
6598         (and (setq bufname (buffer-name (car buflist)))
6599              (string-match "Summary" bufname)
6600              (save-excursion
6601                (set-buffer bufname)
6602                ;; We check that this is, indeed, a summary buffer.
6603                (and (eq major-mode 'gnus-summary-mode)
6604                     ;; Also make sure this isn't bogus.
6605                     gnus-newsgroup-prepared))
6606              (push bufname buffers))
6607         (setq buflist (cdr buflist)))
6608       ;; Go through all these summary buffers and offer to save them.
6609       (when buffers
6610         (map-y-or-n-p
6611          "Update summary buffer %s? "
6612          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6613          buffers)))))
6614
6615 (defun gnus-group-describe-briefly ()
6616   "Give a one line description of the group mode commands."
6617   (interactive)
6618   (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")))
6619
6620 (defun gnus-group-browse-foreign-server (method)
6621   "Browse a foreign news server.
6622 If called interactively, this function will ask for a select method
6623  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6624 If not, METHOD should be a list where the first element is the method
6625 and the second element is the address."
6626   (interactive
6627    (list (let ((how (completing-read
6628                      "Which backend: "
6629                      (append gnus-valid-select-methods gnus-server-alist)
6630                      nil t (cons "nntp" 0))))
6631            ;; We either got a backend name or a virtual server name.
6632            ;; If the first, we also need an address.
6633            (if (assoc how gnus-valid-select-methods)
6634                (list (intern how)
6635                      ;; Suggested by mapjph@bath.ac.uk.
6636                      (completing-read
6637                       "Address: "
6638                       (mapcar (lambda (server) (list server))
6639                               gnus-secondary-servers)))
6640              ;; We got a server name, so we find the method.
6641              (gnus-server-to-method how)))))
6642   (gnus-browse-foreign-server method))
6643
6644 \f
6645 ;;;
6646 ;;; Gnus summary mode
6647 ;;;
6648
6649 (defvar gnus-summary-mode-map nil)
6650
6651 (put 'gnus-summary-mode 'mode-class 'special)
6652
6653 (unless gnus-summary-mode-map
6654   (setq gnus-summary-mode-map (make-keymap))
6655   (suppress-keymap gnus-summary-mode-map)
6656
6657   ;; Non-orthogonal keys
6658
6659   (gnus-define-keys gnus-summary-mode-map
6660     " " gnus-summary-next-page
6661     "\177" gnus-summary-prev-page
6662     [delete] gnus-summary-prev-page
6663     "\r" gnus-summary-scroll-up
6664     "n" gnus-summary-next-unread-article
6665     "p" gnus-summary-prev-unread-article
6666     "N" gnus-summary-next-article
6667     "P" gnus-summary-prev-article
6668     "\M-\C-n" gnus-summary-next-same-subject
6669     "\M-\C-p" gnus-summary-prev-same-subject
6670     "\M-n" gnus-summary-next-unread-subject
6671     "\M-p" gnus-summary-prev-unread-subject
6672     "." gnus-summary-first-unread-article
6673     "," gnus-summary-best-unread-article
6674     "\M-s" gnus-summary-search-article-forward
6675     "\M-r" gnus-summary-search-article-backward
6676     "<" gnus-summary-beginning-of-article
6677     ">" gnus-summary-end-of-article
6678     "j" gnus-summary-goto-article
6679     "^" gnus-summary-refer-parent-article
6680     "\M-^" gnus-summary-refer-article
6681     "u" gnus-summary-tick-article-forward
6682     "!" gnus-summary-tick-article-forward
6683     "U" gnus-summary-tick-article-backward
6684     "d" gnus-summary-mark-as-read-forward
6685     "D" gnus-summary-mark-as-read-backward
6686     "E" gnus-summary-mark-as-expirable
6687     "\M-u" gnus-summary-clear-mark-forward
6688     "\M-U" gnus-summary-clear-mark-backward
6689     "k" gnus-summary-kill-same-subject-and-select
6690     "\C-k" gnus-summary-kill-same-subject
6691     "\M-\C-k" gnus-summary-kill-thread
6692     "\M-\C-l" gnus-summary-lower-thread
6693     "e" gnus-summary-edit-article
6694     "#" gnus-summary-mark-as-processable
6695     "\M-#" gnus-summary-unmark-as-processable
6696     "\M-\C-t" gnus-summary-toggle-threads
6697     "\M-\C-s" gnus-summary-show-thread
6698     "\M-\C-h" gnus-summary-hide-thread
6699     "\M-\C-f" gnus-summary-next-thread
6700     "\M-\C-b" gnus-summary-prev-thread
6701     "\M-\C-u" gnus-summary-up-thread
6702     "\M-\C-d" gnus-summary-down-thread
6703     "&" gnus-summary-execute-command
6704     "c" gnus-summary-catchup-and-exit
6705     "\C-w" gnus-summary-mark-region-as-read
6706     "\C-t" gnus-summary-toggle-truncation
6707     "?" gnus-summary-mark-as-dormant
6708     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6709     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6710     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6711     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6712     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6713     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6714     "=" gnus-summary-expand-window
6715     "\C-x\C-s" gnus-summary-reselect-current-group
6716     "\M-g" gnus-summary-rescan-group
6717     "w" gnus-summary-stop-page-breaking
6718     "\C-c\C-r" gnus-summary-caesar-message
6719     "\M-t" gnus-summary-toggle-mime
6720     "f" gnus-summary-followup
6721     "F" gnus-summary-followup-with-original
6722     "C" gnus-summary-cancel-article
6723     "r" gnus-summary-reply
6724     "R" gnus-summary-reply-with-original
6725     "\C-c\C-f" gnus-summary-mail-forward
6726     "o" gnus-summary-save-article
6727     "\C-o" gnus-summary-save-article-mail
6728     "|" gnus-summary-pipe-output
6729     "\M-k" gnus-summary-edit-local-kill
6730     "\M-K" gnus-summary-edit-global-kill
6731     "V" gnus-version
6732     "\C-c\C-d" gnus-summary-describe-group
6733     "q" gnus-summary-exit
6734     "Q" gnus-summary-exit-no-update
6735     "\C-c\C-i" gnus-info-find-node
6736     gnus-mouse-2 gnus-mouse-pick-article
6737     "m" gnus-summary-mail-other-window
6738     "a" gnus-summary-post-news
6739     "x" gnus-summary-limit-to-unread
6740     "s" gnus-summary-isearch-article
6741     "t" gnus-article-hide-headers
6742     "g" gnus-summary-show-article
6743     "l" gnus-summary-goto-last-article
6744     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6745     "\C-d" gnus-summary-enter-digest-group
6746     "\C-c\C-b" gnus-bug
6747     "*" gnus-cache-enter-article
6748     "\M-*" gnus-cache-remove-article
6749     "\M-&" gnus-summary-universal-argument
6750     "\C-l" gnus-recenter
6751     "I" gnus-summary-increase-score
6752     "L" gnus-summary-lower-score
6753
6754     "V" gnus-summary-score-map
6755     "X" gnus-uu-extract-map
6756     "S" gnus-summary-send-map)
6757
6758   ;; Sort of orthogonal keymap
6759   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6760     "t" gnus-summary-tick-article-forward
6761     "!" gnus-summary-tick-article-forward
6762     "d" gnus-summary-mark-as-read-forward
6763     "r" gnus-summary-mark-as-read-forward
6764     "c" gnus-summary-clear-mark-forward
6765     " " gnus-summary-clear-mark-forward
6766     "e" gnus-summary-mark-as-expirable
6767     "x" gnus-summary-mark-as-expirable
6768     "?" gnus-summary-mark-as-dormant
6769     "b" gnus-summary-set-bookmark
6770     "B" gnus-summary-remove-bookmark
6771     "#" gnus-summary-mark-as-processable
6772     "\M-#" gnus-summary-unmark-as-processable
6773     "S" gnus-summary-limit-include-expunged
6774     "C" gnus-summary-catchup
6775     "H" gnus-summary-catchup-to-here
6776     "\C-c" gnus-summary-catchup-all
6777     "k" gnus-summary-kill-same-subject-and-select
6778     "K" gnus-summary-kill-same-subject
6779     "P" gnus-uu-mark-map)
6780
6781   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6782     "c" gnus-summary-clear-above
6783     "u" gnus-summary-tick-above
6784     "m" gnus-summary-mark-above
6785     "k" gnus-summary-kill-below)
6786
6787   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6788     "/" gnus-summary-limit-to-subject
6789     "n" gnus-summary-limit-to-articles
6790     "w" gnus-summary-pop-limit
6791     "s" gnus-summary-limit-to-subject
6792     "a" gnus-summary-limit-to-author
6793     "u" gnus-summary-limit-to-unread
6794     "m" gnus-summary-limit-to-marks
6795     "v" gnus-summary-limit-to-score
6796     "D" gnus-summary-limit-include-dormant
6797     "d" gnus-summary-limit-exclude-dormant
6798     ;;  "t" gnus-summary-limit-exclude-thread
6799     "E" gnus-summary-limit-include-expunged
6800     "c" gnus-summary-limit-exclude-childless-dormant
6801     "C" gnus-summary-limit-mark-excluded-as-read)
6802
6803   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6804     "n" gnus-summary-next-unread-article
6805     "p" gnus-summary-prev-unread-article
6806     "N" gnus-summary-next-article
6807     "P" gnus-summary-prev-article
6808     "\C-n" gnus-summary-next-same-subject
6809     "\C-p" gnus-summary-prev-same-subject
6810     "\M-n" gnus-summary-next-unread-subject
6811     "\M-p" gnus-summary-prev-unread-subject
6812     "f" gnus-summary-first-unread-article
6813     "b" gnus-summary-best-unread-article
6814     "j" gnus-summary-goto-article
6815     "g" gnus-summary-goto-subject
6816     "l" gnus-summary-goto-last-article
6817     "p" gnus-summary-pop-article)
6818
6819   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6820     "k" gnus-summary-kill-thread
6821     "l" gnus-summary-lower-thread
6822     "i" gnus-summary-raise-thread
6823     "T" gnus-summary-toggle-threads
6824     "t" gnus-summary-rethread-current
6825     "^" gnus-summary-reparent-thread
6826     "s" gnus-summary-show-thread
6827     "S" gnus-summary-show-all-threads
6828     "h" gnus-summary-hide-thread
6829     "H" gnus-summary-hide-all-threads
6830     "n" gnus-summary-next-thread
6831     "p" gnus-summary-prev-thread
6832     "u" gnus-summary-up-thread
6833     "o" gnus-summary-top-thread
6834     "d" gnus-summary-down-thread
6835     "#" gnus-uu-mark-thread
6836     "\M-#" gnus-uu-unmark-thread)
6837
6838   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6839     "c" gnus-summary-catchup-and-exit
6840     "C" gnus-summary-catchup-all-and-exit
6841     "E" gnus-summary-exit-no-update
6842     "Q" gnus-summary-exit
6843     "Z" gnus-summary-exit
6844     "n" gnus-summary-catchup-and-goto-next-group
6845     "R" gnus-summary-reselect-current-group
6846     "G" gnus-summary-rescan-group
6847     "N" gnus-summary-next-group
6848     "P" gnus-summary-prev-group)
6849
6850   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6851     " " gnus-summary-next-page
6852     "n" gnus-summary-next-page
6853     "\177" gnus-summary-prev-page
6854     [delete] gnus-summary-prev-page
6855     "p" gnus-summary-prev-page
6856     "\r" gnus-summary-scroll-up
6857     "<" gnus-summary-beginning-of-article
6858     ">" gnus-summary-end-of-article
6859     "b" gnus-summary-beginning-of-article
6860     "e" gnus-summary-end-of-article
6861     "^" gnus-summary-refer-parent-article
6862     "r" gnus-summary-refer-parent-article
6863     "R" gnus-summary-refer-references
6864     "g" gnus-summary-show-article
6865     "s" gnus-summary-isearch-article)
6866
6867   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6868     "b" gnus-article-add-buttons
6869     "B" gnus-article-add-buttons-to-head
6870     "o" gnus-article-treat-overstrike
6871     ;;  "w" gnus-article-word-wrap
6872     "w" gnus-article-fill-cited-article
6873     "c" gnus-article-remove-cr
6874     "L" gnus-article-remove-trailing-blank-lines
6875     "q" gnus-article-de-quoted-unreadable
6876     "f" gnus-article-display-x-face
6877     "l" gnus-summary-stop-page-breaking
6878     "r" gnus-summary-caesar-message
6879     "t" gnus-article-hide-headers
6880     "v" gnus-summary-verbose-headers
6881     "m" gnus-summary-toggle-mime)
6882
6883   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6884     "a" gnus-article-hide
6885     "h" gnus-article-hide-headers
6886     "b" gnus-article-hide-boring-headers
6887     "s" gnus-article-hide-signature
6888     "c" gnus-article-hide-citation
6889     "p" gnus-article-hide-pgp
6890     "\C-c" gnus-article-hide-citation-maybe)
6891
6892   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6893     "a" gnus-article-highlight
6894     "h" gnus-article-highlight-headers
6895     "c" gnus-article-highlight-citation
6896     "s" gnus-article-highlight-signature)
6897
6898   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6899     "z" gnus-article-date-ut
6900     "u" gnus-article-date-ut
6901     "l" gnus-article-date-local
6902     "e" gnus-article-date-lapsed
6903     "o" gnus-article-date-original)
6904
6905   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6906     "v" gnus-version
6907     "f" gnus-summary-fetch-faq
6908     "d" gnus-summary-describe-group
6909     "h" gnus-summary-describe-briefly
6910     "i" gnus-info-find-node)
6911
6912   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6913     "e" gnus-summary-expire-articles
6914     "\M-\C-e" gnus-summary-expire-articles-now
6915     "\177" gnus-summary-delete-article
6916     [delete] gnus-summary-delete-article
6917     "m" gnus-summary-move-article
6918     "r" gnus-summary-respool-article
6919     "w" gnus-summary-edit-article
6920     "c" gnus-summary-copy-article
6921     "B" gnus-summary-crosspost-article
6922     "q" gnus-summary-respool-query
6923     "i" gnus-summary-import-article)
6924
6925   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6926     "o" gnus-summary-save-article
6927     "m" gnus-summary-save-article-mail
6928     "r" gnus-summary-save-article-rmail
6929     "f" gnus-summary-save-article-file
6930     "b" gnus-summary-save-article-body-file
6931     "h" gnus-summary-save-article-folder
6932     "v" gnus-summary-save-article-vm
6933     "p" gnus-summary-pipe-output
6934     "s" gnus-soup-add-article)
6935   )
6936
6937 \f
6938
6939 (defun gnus-summary-mode (&optional group)
6940   "Major mode for reading articles.
6941
6942 All normal editing commands are switched off.
6943 \\<gnus-summary-mode-map>
6944 Each line in this buffer represents one article.  To read an
6945 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6946 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6947 respectively.
6948
6949 You can also post articles and send mail from this buffer.  To
6950 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6951 of an article, type `\\[gnus-summary-reply]'.
6952
6953 There are approx. one gazillion commands you can execute in this
6954 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6955
6956 The following commands are available:
6957
6958 \\{gnus-summary-mode-map}"
6959   (interactive)
6960   (when (and menu-bar-mode
6961              (gnus-visual-p 'summary-menu 'menu))
6962     (gnus-summary-make-menu-bar))
6963   (kill-all-local-variables)
6964   (let ((locals gnus-summary-local-variables))
6965     (while locals
6966       (if (consp (car locals))
6967           (progn
6968             (make-local-variable (caar locals))
6969             (set (caar locals) (eval (cdar locals))))
6970         (make-local-variable (car locals))
6971         (set (car locals) nil))
6972       (setq locals (cdr locals))))
6973   (gnus-make-thread-indent-array)
6974   (gnus-simplify-mode-line)
6975   (setq major-mode 'gnus-summary-mode)
6976   (setq mode-name "Summary")
6977   (make-local-variable 'minor-mode-alist)
6978   (use-local-map gnus-summary-mode-map)
6979   (buffer-disable-undo (current-buffer))
6980   (setq buffer-read-only t)             ;Disable modification
6981   (setq truncate-lines t)
6982   (setq selective-display t)
6983   (setq selective-display-ellipses t)   ;Display `...'
6984   (setq buffer-display-table gnus-summary-display-table)
6985   (setq gnus-newsgroup-name group)
6986   (run-hooks 'gnus-summary-mode-hook))
6987
6988 (defun gnus-summary-make-display-table ()
6989   ;; Change the display table.  Odd characters have a tendency to mess
6990   ;; up nicely formatted displays - we make all possible glyphs
6991   ;; display only a single character.
6992
6993   ;; We start from the standard display table, if any.
6994   (setq gnus-summary-display-table
6995         (or (copy-sequence standard-display-table)
6996             (make-display-table)))
6997   ;; Nix out all the control chars...
6998   (let ((i 32))
6999     (while (>= (setq i (1- i)) 0)
7000       (aset gnus-summary-display-table i [??])))
7001   ;; ... but not newline and cr, of course. (cr is necessary for the
7002   ;; selective display).
7003   (aset gnus-summary-display-table ?\n nil)
7004   (aset gnus-summary-display-table ?\r nil)
7005   ;; We nix out any glyphs over 126 that are not set already.
7006   (let ((i 256))
7007     (while (>= (setq i (1- i)) 127)
7008       ;; Only modify if the entry is nil.
7009       (or (aref gnus-summary-display-table i)
7010           (aset gnus-summary-display-table i [??])))))
7011
7012 (defun gnus-summary-clear-local-variables ()
7013   (let ((locals gnus-summary-local-variables))
7014     (while locals
7015       (if (consp (car locals))
7016           (and (vectorp (caar locals))
7017                (set (caar locals) nil))
7018         (and (vectorp (car locals))
7019              (set (car locals) nil)))
7020       (setq locals (cdr locals)))))
7021
7022 ;; Summary data functions.
7023
7024 (defmacro gnus-data-number (data)
7025   `(car ,data))
7026
7027 (defmacro gnus-data-set-number (data number)
7028   `(setcar ,data ,number))
7029
7030 (defmacro gnus-data-mark (data)
7031   `(nth 1 ,data))
7032
7033 (defmacro gnus-data-set-mark (data mark)
7034   `(setcar (nthcdr 1 ,data) ,mark))
7035
7036 (defmacro gnus-data-pos (data)
7037   `(nth 2 ,data))
7038
7039 (defmacro gnus-data-set-pos (data pos)
7040   `(setcar (nthcdr 2 ,data) ,pos))
7041
7042 (defmacro gnus-data-header (data)
7043   `(nth 3 ,data))
7044
7045 (defmacro gnus-data-level (data)
7046   `(nth 4 ,data))
7047
7048 (defmacro gnus-data-unread-p (data)
7049   `(= (nth 1 ,data) gnus-unread-mark))
7050
7051 (defmacro gnus-data-pseudo-p (data)
7052   `(consp (nth 3 ,data)))
7053
7054 (defmacro gnus-data-find (number)
7055   `(assq ,number gnus-newsgroup-data))
7056
7057 (defmacro gnus-data-find-list (number &optional data)
7058   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7059      (memq (assq ,number bdata)
7060            bdata)))
7061
7062 (defmacro gnus-data-make (number mark pos header level)
7063   `(list ,number ,mark ,pos ,header ,level))
7064
7065 (defun gnus-data-enter (after-article number mark pos header level offset)
7066   (let ((data (gnus-data-find-list after-article)))
7067     (or data (error "No such article: %d" after-article))
7068     (setcdr data (cons (gnus-data-make number mark pos header level)
7069                        (cdr data)))
7070     (setq gnus-newsgroup-data-reverse nil)
7071     (gnus-data-update-list (cddr data) offset)))
7072
7073 (defun gnus-data-enter-list (after-article list &optional offset)
7074   (when list
7075     (let ((data (and after-article (gnus-data-find-list after-article)))
7076           (ilist list))
7077       (or data (not after-article) (error "No such article: %d" after-article))
7078       ;; Find the last element in the list to be spliced into the main
7079       ;; list.
7080       (while (cdr list)
7081         (setq list (cdr list)))
7082       (if (not data)
7083           (progn
7084             (setcdr list gnus-newsgroup-data)
7085             (setq gnus-newsgroup-data ilist)
7086             (and offset (gnus-data-update-list (cdr list) offset)))
7087         (setcdr list (cdr data))
7088         (setcdr data ilist)
7089         (and offset (gnus-data-update-list (cdr data) offset)))
7090       (setq gnus-newsgroup-data-reverse nil))))
7091
7092 (defun gnus-data-remove (article &optional offset)
7093   (let ((data gnus-newsgroup-data))
7094     (if (= (gnus-data-number (car data)) article)
7095         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7096               gnus-newsgroup-data-reverse nil)
7097       (while (cdr data)
7098         (and (= (gnus-data-number (cadr data)) article)
7099              (progn
7100                (setcdr data (cddr data))
7101                (and offset (gnus-data-update-list (cdr data) offset))
7102                (setq data nil
7103                      gnus-newsgroup-data-reverse nil)))
7104         (setq data (cdr data))))))
7105
7106 (defmacro gnus-data-list (backward)
7107   `(if ,backward
7108        (or gnus-newsgroup-data-reverse
7109            (setq gnus-newsgroup-data-reverse
7110                  (reverse gnus-newsgroup-data)))
7111      gnus-newsgroup-data))
7112
7113 (defun gnus-data-update-list (data offset)
7114   "Add OFFSET to the POS of all data entries in DATA."
7115   (while data
7116     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7117     (setq data (cdr data))))
7118
7119 (defun gnus-data-compute-positions ()
7120   "Compute the positions of all articles."
7121   (let ((data gnus-newsgroup-data)
7122         pos)
7123     (while data
7124       (when (setq pos (text-property-any
7125                        (point-min) (point-max)
7126                        'gnus-number (gnus-data-number (car data))))
7127         (gnus-data-set-pos (car data) (+ pos 3)))
7128       (setq data (cdr data)))))
7129
7130 (defun gnus-summary-article-pseudo-p (article)
7131   "Say whether this article is a pseudo article or not."
7132   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7133
7134 (defun gnus-article-parent-p (number)
7135   "Say whether this article is a parent or not."
7136   (let ((data (gnus-data-find-list number)))
7137     (and (cdr data)                     ; There has to be an article after...
7138          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7139             (gnus-data-level (nth 1 data))))))
7140
7141 (defun gnus-article-children (number)
7142   "Return a list of all children to NUMBER."
7143   (let* ((data (gnus-data-find-list number))
7144          (level (gnus-data-level (car data)))
7145          children)
7146     (setq data (cdr data))
7147     (while (and data            
7148                 (= (gnus-data-level (car data)) (1+ level)))
7149       (push (gnus-data-number (car data)) children)
7150       (setq data (cdr data)))
7151     children))
7152
7153 (defmacro gnus-summary-skip-intangible ()
7154   "If the current article is intangible, then jump to a different article."
7155   '(let ((to (get-text-property (point) 'gnus-intangible)))
7156     (and to (gnus-summary-goto-subject to))))
7157
7158 (defmacro gnus-summary-article-intangible-p ()
7159   "Say whether this article is intangible or not."
7160   '(get-text-property (point) 'gnus-intangible))
7161
7162 ;; Some summary mode macros.
7163
7164 (defmacro gnus-summary-article-number ()
7165   "The article number of the article on the current line.
7166 If there isn's an article number here, then we return the current
7167 article number."
7168   '(progn
7169      (gnus-summary-skip-intangible)
7170      (or (get-text-property (point) 'gnus-number)
7171          (gnus-summary-last-subject))))
7172
7173 (defmacro gnus-summary-article-header (&optional number)
7174   `(gnus-data-header (gnus-data-find
7175                       ,(or number '(gnus-summary-article-number)))))
7176
7177 (defmacro gnus-summary-thread-level (&optional number)
7178   `(if (and (eq gnus-summary-make-false-root 'dummy)
7179             (get-text-property (point) 'gnus-intangible))
7180        0
7181      (gnus-data-level (gnus-data-find
7182                        ,(or number '(gnus-summary-article-number))))))
7183
7184 (defmacro gnus-summary-article-mark (&optional number)
7185   `(gnus-data-mark (gnus-data-find
7186                     ,(or number '(gnus-summary-article-number)))))
7187
7188 (defmacro gnus-summary-article-pos (&optional number)
7189   `(gnus-data-pos (gnus-data-find
7190                    ,(or number '(gnus-summary-article-number)))))
7191
7192 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7193 (defmacro gnus-summary-article-subject (&optional number)
7194   "Return current subject string or nil if nothing."
7195   `(let ((headers
7196           ,(if number
7197                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7198              '(gnus-data-header (assq (gnus-summary-article-number)
7199                                       gnus-newsgroup-data)))))
7200      (and headers
7201           (vectorp headers)
7202           (mail-header-subject headers))))
7203
7204 (defmacro gnus-summary-article-score (&optional number)
7205   "Return current article score."
7206   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7207                   gnus-newsgroup-scored))
7208        gnus-summary-default-score 0))
7209
7210 (defun gnus-summary-article-children (&optional number)
7211   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7212          (level (gnus-data-level (car data)))
7213          l children)
7214     (while (and (setq data (cdr data))
7215                 (> (setq l (gnus-data-level (car data))) level))
7216       (and (= (1+ level) l)
7217            (setq children (cons (gnus-data-number (car data))
7218                                 children))))
7219     (nreverse children)))
7220
7221 (defun gnus-summary-article-parent (&optional number)
7222   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7223                                     (gnus-data-list t)))
7224          (level (gnus-data-level (car data))))
7225     (if (zerop level)
7226         () ; This is a root.
7227       ;; We search until we find an article with a level less than
7228       ;; this one.  That function has to be the parent.
7229       (while (and (setq data (cdr data))
7230                   (not (< (gnus-data-level (car data)) level))))
7231       (and data (gnus-data-number (car data))))))
7232
7233 (defun gnus-unread-mark-p (mark)
7234   "Say whether MARK is the unread mark."
7235   (= mark gnus-unread-mark))
7236
7237 (defun gnus-read-mark-p (mark)
7238   "Say whether MARK is one of the marks that mark as read.
7239 This is all marks except unread, ticked, dormant, and expirable."
7240   (not (or (= mark gnus-unread-mark)
7241            (= mark gnus-ticked-mark)
7242            (= mark gnus-dormant-mark)
7243            (= mark gnus-expirable-mark))))
7244
7245 ;; Various summary mode internalish functions.
7246
7247 (defun gnus-mouse-pick-article (e)
7248   (interactive "e")
7249   (mouse-set-point e)
7250   (gnus-summary-next-page nil t))
7251
7252 (defun gnus-summary-setup-buffer (group)
7253   "Initialize summary buffer."
7254   (let ((buffer (concat "*Summary " group "*")))
7255     (if (get-buffer buffer)
7256         (progn
7257           (set-buffer buffer)
7258           (setq gnus-summary-buffer (current-buffer))
7259           (not gnus-newsgroup-prepared))
7260       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7261       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7262       (gnus-add-current-to-buffer-list)
7263       (gnus-summary-mode group)
7264       (when gnus-carpal
7265         (gnus-carpal-setup-buffer 'summary))
7266       (unless gnus-single-article-buffer
7267         (make-local-variable 'gnus-article-buffer)
7268         (make-local-variable 'gnus-article-current)
7269         (make-local-variable 'gnus-original-article-buffer))
7270       (setq gnus-newsgroup-name group)
7271       t)))
7272
7273 (defun gnus-set-global-variables ()
7274   ;; Set the global equivalents of the summary buffer-local variables
7275   ;; to the latest values they had.  These reflect the summary buffer
7276   ;; that was in action when the last article was fetched.
7277   (when (eq major-mode 'gnus-summary-mode)
7278     (setq gnus-summary-buffer (current-buffer))
7279     (let ((name gnus-newsgroup-name)
7280           (marked gnus-newsgroup-marked)
7281           (unread gnus-newsgroup-unreads)
7282           (headers gnus-current-headers)
7283           (data gnus-newsgroup-data)
7284           (summary gnus-summary-buffer)
7285           (article-buffer gnus-article-buffer)
7286           (original gnus-original-article-buffer)
7287           (gac gnus-article-current)
7288           (score-file gnus-current-score-file))
7289       (save-excursion
7290         (set-buffer gnus-group-buffer)
7291         (setq gnus-newsgroup-name name)
7292         (setq gnus-newsgroup-marked marked)
7293         (setq gnus-newsgroup-unreads unread)
7294         (setq gnus-current-headers headers)
7295         (setq gnus-newsgroup-data data)
7296         (setq gnus-article-current gac)
7297         (setq gnus-summary-buffer summary)
7298         (setq gnus-article-buffer article-buffer)
7299         (setq gnus-original-article-buffer original)
7300         (setq gnus-current-score-file score-file)))))
7301
7302 (defun gnus-summary-last-article-p (&optional article)
7303   "Return whether ARTICLE is the last article in the buffer."
7304   (if (not (setq article (or article (gnus-summary-article-number))))
7305       t ; All non-existant numbers are the last article. :-)
7306     (cdr (gnus-data-find-list article))))
7307
7308 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7309   "Insert a dummy root in the summary buffer."
7310   (beginning-of-line)
7311   (add-text-properties
7312    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7313    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7314
7315 (defvar gnus-thread-indent-array nil)
7316 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7317 (defun gnus-make-thread-indent-array ()
7318   (let ((n 200))
7319     (if (and gnus-thread-indent-array
7320              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7321         nil
7322       (setq gnus-thread-indent-array (make-vector 201 "")
7323             gnus-thread-indent-array-level gnus-thread-indent-level)
7324       (while (>= n 0)
7325         (aset gnus-thread-indent-array n
7326               (make-string (* n gnus-thread-indent-level) ? ))
7327         (setq n (1- n))))))
7328
7329 (defun gnus-summary-insert-line
7330   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7331                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7332                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7333   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7334          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7335          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7336          (gnus-tmp-score-char
7337           (if (or (null gnus-summary-default-score)
7338                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7339                       gnus-summary-zcore-fuzz)) ? 
7340             (if (< gnus-tmp-score gnus-summary-default-score)
7341                 gnus-score-below-mark gnus-score-over-mark)))
7342          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7343                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7344                                   gnus-cached-mark)
7345                                  (gnus-tmp-replied gnus-replied-mark)
7346                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7347                                   gnus-saved-mark)
7348                                  (t gnus-unread-mark)))
7349          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7350          (gnus-tmp-name
7351           (cond
7352            ((string-match "(.+)" gnus-tmp-from)
7353             (substring gnus-tmp-from
7354                        (1+ (match-beginning 0)) (1- (match-end 0))))
7355            ((string-match "<[^>]+> *$" gnus-tmp-from)
7356             (let ((beg (match-beginning 0)))
7357               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7358                        (substring gnus-tmp-from (1+ (match-beginning 0))
7359                                   (1- (match-end 0))))
7360                   (substring gnus-tmp-from 0 beg))))
7361            (t gnus-tmp-from)))
7362          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7363          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7364          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7365          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7366          (buffer-read-only nil))
7367     (when (string= gnus-tmp-name "")
7368       (setq gnus-tmp-name gnus-tmp-from))
7369     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7370     (put-text-property
7371      (point)
7372      (progn (eval gnus-summary-line-format-spec) (point))
7373      'gnus-number gnus-tmp-number)
7374     (when (gnus-visual-p 'summary-highlight 'highlight)
7375       (forward-line -1)
7376       (run-hooks 'gnus-summary-update-hook)
7377       (forward-line 1))))
7378
7379 (defun gnus-summary-update-line (&optional dont-update)
7380   ;; Update summary line after change.
7381   (when (and gnus-summary-default-score
7382              (not gnus-summary-inhibit-highlight))
7383     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7384            (article (gnus-summary-article-number))
7385            (score (gnus-summary-article-score article)))
7386       (unless dont-update
7387         (if (and gnus-summary-mark-below
7388                  (< (gnus-summary-article-score)
7389                     gnus-summary-mark-below))
7390             ;; This article has a low score, so we mark it as read.
7391             (when (memq article gnus-newsgroup-unreads)
7392               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7393           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7394             ;; This article was previously marked as read on account
7395             ;; of a low score, but now it has risen, so we mark it as
7396             ;; unread.
7397             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7398         (gnus-summary-update-mark
7399          (if (or (null gnus-summary-default-score)
7400                  (<= (abs (- score gnus-summary-default-score))
7401                      gnus-summary-zcore-fuzz)) ? 
7402            (if (< score gnus-summary-default-score)
7403                gnus-score-below-mark gnus-score-over-mark)) 'score))
7404       ;; Do visual highlighting.
7405       (when (gnus-visual-p 'summary-highlight 'highlight)
7406         (run-hooks 'gnus-summary-update-hook)))))
7407
7408 (defvar gnus-tmp-new-adopts nil)
7409
7410 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7411   ;; Sum up all elements (and sub-elements) in a list.
7412   (let* ((number
7413           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7414           (cond
7415            ((and (consp thread) (cdr thread))
7416             (apply
7417              '+ 1 (mapcar
7418                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7419            ((null thread)
7420             1)
7421            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7422             1)
7423            (t 1))))
7424     (when (and level (zerop level) gnus-tmp-new-adopts)
7425       (incf number
7426             (apply '+ (mapcar
7427                        'gnus-summary-number-of-articles-in-thread
7428                        gnus-tmp-new-adopts))))
7429     (if char
7430         (if (> number 1) gnus-not-empty-thread-mark
7431           gnus-empty-thread-mark)
7432       number)))
7433
7434 (defun gnus-summary-set-local-parameters (group)
7435  "Go through the local params of GROUP and set all variable specs in that list."
7436   (let ((params (gnus-info-params (gnus-get-info group)))
7437         elem)
7438     (while params
7439       (setq elem (car params)
7440             params (cdr params))
7441       (and (consp elem)                 ; Has to be a cons.
7442            (consp (cdr elem))           ; The cdr has to be a list.
7443            (symbolp (car elem))         ; Has to be a symbol in there.
7444            (not (memq (car elem) 
7445                       '(quit-config to-address to-list to-group)))
7446            (progn                       ; So we set it.
7447              (make-local-variable (car elem))
7448              (set (car elem) (eval (nth 1 elem))))))))
7449
7450 (defun gnus-summary-read-group (group &optional show-all no-article
7451                                       kill-buffer no-display)
7452   "Start reading news in newsgroup GROUP.
7453 If SHOW-ALL is non-nil, already read articles are also listed.
7454 If NO-ARTICLE is non-nil, no article is selected initially.
7455 If NO-DISPLAY, don't generate a summary buffer."
7456   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7457   (let* ((new-group (gnus-summary-setup-buffer group))
7458          (quit-config (gnus-group-quit-config group))
7459          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7460     (cond
7461      ;; This summary buffer exists already, so we just select it.
7462      ((not new-group)
7463       (gnus-set-global-variables)
7464       (when kill-buffer
7465         (gnus-kill-or-deaden-summary kill-buffer))
7466       (gnus-configure-windows 'summary 'force)
7467       (gnus-set-mode-line 'summary)
7468       (gnus-summary-position-point)
7469       (message "")
7470       t)
7471      ;; We couldn't select this group.
7472      ((null did-select)
7473       (when (and (eq major-mode 'gnus-summary-mode)
7474                  (not (equal (current-buffer) kill-buffer)))
7475         (kill-buffer (current-buffer))
7476         (if (not quit-config)
7477             (progn
7478               (set-buffer gnus-group-buffer)
7479               (gnus-group-jump-to-group group)
7480               (gnus-group-next-unread-group 1))
7481           (if (not (buffer-name (car quit-config)))
7482               (gnus-configure-windows 'group 'force)
7483             (set-buffer (car quit-config))
7484             (and (eq major-mode 'gnus-summary-mode)
7485                  (gnus-set-global-variables))
7486             (gnus-configure-windows (cdr quit-config)))))
7487       (gnus-message 3 "Can't select group")
7488       nil)
7489      ;; The user did a `C-g' while prompting for number of articles,
7490      ;; so we exit this group.
7491      ((eq did-select 'quit)
7492       (and (eq major-mode 'gnus-summary-mode)
7493            (not (equal (current-buffer) kill-buffer))
7494            (kill-buffer (current-buffer)))
7495       (when kill-buffer
7496         (gnus-kill-or-deaden-summary kill-buffer))
7497       (if (not quit-config)
7498           (progn
7499             (set-buffer gnus-group-buffer)
7500             (gnus-group-jump-to-group group)
7501             (gnus-group-next-unread-group 1)
7502             (gnus-configure-windows 'group 'force))
7503         (if (not (buffer-name (car quit-config)))
7504             (gnus-configure-windows 'group 'force)
7505           (set-buffer (car quit-config))
7506           (and (eq major-mode 'gnus-summary-mode)
7507                (gnus-set-global-variables))
7508           (gnus-configure-windows (cdr quit-config))))
7509       ;; Finally signal the quit.
7510       (signal 'quit nil))
7511      ;; The group was successfully selected.
7512      (t
7513       (gnus-set-global-variables)
7514       ;; Save the active value in effect when the group was entered.
7515       (setq gnus-newsgroup-active
7516             (gnus-copy-sequence
7517              (gnus-active gnus-newsgroup-name)))
7518       ;; You can change the summary buffer in some way with this hook.
7519       (run-hooks 'gnus-select-group-hook)
7520       ;; Set any local variables in the group parameters.
7521       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7522       (gnus-update-format-specifications)
7523       ;; Do score processing.
7524       (when gnus-use-scoring
7525         (gnus-possibly-score-headers))
7526       ;; Check whether to fill in the gaps in the threads.
7527       (when gnus-build-sparse-threads
7528         (gnus-build-sparse-threads))
7529       ;; Find the initial limit.
7530       (if show-all
7531           (let ((gnus-newsgroup-dormant nil))
7532             (gnus-summary-initial-limit show-all))
7533         (gnus-summary-initial-limit show-all))
7534       ;; Generate the summary buffer.
7535       (unless no-display
7536         (gnus-summary-prepare))
7537       (when gnus-use-trees
7538         (gnus-tree-open group)
7539         (setq gnus-summary-highlight-line-function
7540               'gnus-tree-highlight-article))
7541       ;; If the summary buffer is empty, but there are some low-scored
7542       ;; articles or some excluded dormants, we include these in the
7543       ;; buffer.
7544       (when (and (zerop (buffer-size))
7545                  (not no-display))
7546         (cond (gnus-newsgroup-dormant
7547                (gnus-summary-limit-include-dormant))
7548               ((and gnus-newsgroup-scored show-all)
7549                (gnus-summary-limit-include-expunged))))
7550       ;; Function `gnus-apply-kill-file' must be called in this hook.
7551       (run-hooks 'gnus-apply-kill-hook)
7552       (if (and (zerop (buffer-size))
7553                (not no-display))
7554           (progn
7555             ;; This newsgroup is empty.
7556             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7557             (gnus-message 6 "No unread news")
7558             (when kill-buffer
7559               (gnus-kill-or-deaden-summary kill-buffer))
7560             ;; Return nil from this function.
7561             nil)
7562         ;; Hide conversation thread subtrees.  We cannot do this in
7563         ;; gnus-summary-prepare-hook since kill processing may not
7564         ;; work with hidden articles.
7565         (and gnus-show-threads
7566              gnus-thread-hide-subtree
7567              (gnus-summary-hide-all-threads))
7568         ;; Show first unread article if requested.
7569         (if (and (not no-article)
7570                  (not no-display)
7571                  gnus-newsgroup-unreads
7572                  gnus-auto-select-first)
7573             (if (eq gnus-auto-select-first 'best)
7574                 (gnus-summary-best-unread-article)
7575               (gnus-summary-first-unread-article))
7576           ;; Don't select any articles, just move point to the first
7577           ;; article in the group.
7578           (goto-char (point-min))
7579           (gnus-summary-position-point)
7580           (gnus-set-mode-line 'summary)
7581           (gnus-configure-windows 'summary 'force))
7582         ;; If we are in async mode, we send some info to the backend.
7583         (when gnus-newsgroup-async
7584           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7585         (when kill-buffer
7586           (gnus-kill-or-deaden-summary kill-buffer))
7587         (when (get-buffer-window gnus-group-buffer t)
7588           ;; Gotta use windows, because recenter does wierd stuff if
7589           ;; the current buffer ain't the displayed window.
7590           (let ((owin (selected-window)))
7591             (select-window (get-buffer-window gnus-group-buffer t))
7592             (when (gnus-group-goto-group group)
7593               (recenter))
7594             (select-window owin))))
7595       ;; Mark this buffer as "prepared".
7596       (setq gnus-newsgroup-prepared t)
7597       t))))
7598
7599 (defun gnus-summary-prepare ()
7600   "Generate the summary buffer."
7601   (let ((buffer-read-only nil))
7602     (erase-buffer)
7603     (setq gnus-newsgroup-data nil
7604           gnus-newsgroup-data-reverse nil)
7605     (run-hooks 'gnus-summary-generate-hook)
7606     ;; Generate the buffer, either with threads or without.
7607     (when gnus-newsgroup-headers
7608       (gnus-summary-prepare-threads
7609        (if gnus-show-threads
7610            (gnus-sort-gathered-threads
7611             (funcall gnus-summary-thread-gathering-function
7612                      (gnus-sort-threads
7613                       (gnus-cut-threads (gnus-make-threads)))))
7614          ;; Unthreaded display.
7615          (gnus-sort-articles gnus-newsgroup-headers))))
7616     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7617     ;; Call hooks for modifying summary buffer.
7618     (goto-char (point-min))
7619     (run-hooks 'gnus-summary-prepare-hook)))
7620
7621 (defun gnus-gather-threads-by-subject (threads)
7622   "Gather threads by looking at Subject headers."
7623   (if (not gnus-summary-make-false-root)
7624       threads
7625     (let ((hashtb (gnus-make-hashtable 1023))
7626           (prev threads)
7627           (result threads)
7628           subject hthread whole-subject)
7629       (while threads
7630         (setq whole-subject (mail-header-subject (caar threads)))
7631         (if (and gnus-summary-gather-exclude-subject
7632                  (string-match gnus-summary-gather-exclude-subject
7633                                whole-subject))
7634             () ; We don't want to do anything with this article.
7635           ;; We simplify the subject before looking it up in the
7636           ;; hash table.
7637           (setq subject
7638                 (cond
7639                  ;; Truncate the subject.
7640                  ((numberp gnus-summary-gather-subject-limit)
7641                   (setq subject (gnus-simplify-subject-re whole-subject))
7642                   (if (> (length subject) gnus-summary-gather-subject-limit)
7643                       (substring subject 0 gnus-summary-gather-subject-limit)
7644                     subject))
7645                  ;; Fuzzily simplify it.
7646                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7647                   (gnus-simplify-subject-fuzzy whole-subject))
7648                  ;; Just remove the leading "Re:".
7649                  (t
7650                   (gnus-simplify-subject-re whole-subject))))
7651
7652           (if (setq hthread (gnus-gethash subject hashtb))
7653               (progn
7654                 ;; We enter a dummy root into the thread, if we
7655                 ;; haven't done that already.
7656                 (unless (stringp (caar hthread))
7657                   (setcar hthread (list whole-subject (car hthread))))
7658                 ;; We add this new gathered thread to this gathered
7659                 ;; thread.
7660                 (setcdr (car hthread)
7661                         (nconc (cdar hthread) (list (car threads))))
7662                 ;; Remove it from the list of threads.
7663                 (setcdr prev (cdr threads))
7664                 (setq threads prev))
7665             ;; Enter this thread into the hash table.
7666             (gnus-sethash subject threads hashtb)))
7667         (setq prev threads)
7668         (setq threads (cdr threads)))
7669       result)))
7670
7671 (defun gnus-gather-threads-by-references (threads)
7672   "Gather threads by looking at References headers."
7673   (let ((idhashtb (gnus-make-hashtable 1023))
7674         (thhashtb (gnus-make-hashtable 1023))
7675         (prev threads)
7676         (result threads)
7677         ids references id gthread gid entered)
7678     (while threads
7679       (when (setq references (mail-header-references (caar threads)))
7680         (setq id (mail-header-id (caar threads)))
7681         (setq ids (gnus-split-references references))
7682         (setq entered nil)
7683         (while ids
7684           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7685               (progn
7686                 (gnus-sethash (car ids) id idhashtb)
7687                 (gnus-sethash id threads thhashtb))
7688             (setq gthread (gnus-gethash gid thhashtb))
7689             (unless entered
7690               ;; We enter a dummy root into the thread, if we
7691               ;; haven't done that already.
7692               (unless (stringp (caar gthread))
7693                 (setcar gthread (list (mail-header-subject (caar gthread))
7694                                       (car gthread))))
7695               ;; We add this new gathered thread to this gathered
7696               ;; thread.
7697               (setcdr (car gthread)
7698                       (nconc (cdar gthread) (list (car threads)))))
7699             ;; Add it into the thread hash table.
7700             (gnus-sethash id gthread thhashtb)
7701             (setq entered t)
7702             ;; Remove it from the list of threads.
7703             (setcdr prev (cdr threads))
7704             (setq threads prev))
7705           (setq ids (cdr ids))))
7706       (setq prev threads)
7707       (setq threads (cdr threads)))
7708     result))
7709
7710 (defun gnus-sort-gathered-threads (threads)
7711   "Sort subtreads inside each gathered thread by article number."
7712   (let ((result threads))
7713     (while threads
7714       (when (stringp (caar threads))
7715         (setcdr (car threads)
7716                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7717       (setq threads (cdr threads)))
7718     result))
7719
7720 (defun gnus-make-threads ()
7721   "Go through the dependency hashtb and find the roots.  Return all threads."
7722   (let (threads)
7723     (mapatoms
7724      (lambda (refs)
7725        (unless (car (symbol-value refs))
7726          ;; These threads do not refer back to any other articles,
7727          ;; so they're roots.
7728          (setq threads (append (cdr (symbol-value refs)) threads))))
7729      gnus-newsgroup-dependencies)
7730     threads))
7731
7732 (defun gnus-build-sparse-threads ()
7733   (let ((headers gnus-newsgroup-headers)
7734         (deps gnus-newsgroup-dependencies)
7735         header references generation relations 
7736         cthread subject child end pthread relation)
7737     ;; First we create an alist of generations/relations, where 
7738     ;; generations is how much we trust the ralation, and the relation
7739     ;; is parent/child.
7740     (gnus-message 7 "Making sparse threads...")
7741     (save-excursion
7742       (nnheader-set-temp-buffer " *gnus sparse threads*")
7743       (while (setq header (pop headers))
7744         (when (and (setq references (mail-header-references header))
7745                    (not (string= references "")))
7746           (insert references)
7747           (setq child (downcase (mail-header-id header))
7748                 subject (mail-header-subject header))
7749           (setq generation 0)
7750           (while (search-backward ">" nil t)
7751             (setq end (1+ (point)))
7752             (when (search-backward "<" nil t)
7753               (push (list (incf generation) 
7754                           child (setq child (downcase
7755                                              (buffer-substring (point) end)))
7756                           subject)
7757                     relations)))
7758           (push (list (1+ generation) child nil subject) relations)
7759           (erase-buffer)))
7760       (kill-buffer (current-buffer)))
7761     ;; Sort over trustworthiness.
7762     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7763     (while (setq relation (pop relations))
7764       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7765                 (unless (car (symbol-value cthread))
7766                   ;; Make this article the parent of these threads.
7767                   (setcar (symbol-value cthread)
7768                           (vector gnus-reffed-article-number 
7769                                   (cadddr relation) 
7770                                   "" ""
7771                                   (cadr relation) 
7772                                   (or (caddr relation) "") 0 0 "")))
7773               (set cthread (list (vector gnus-reffed-article-number
7774                                          (cadddr relation) 
7775                                          "" "" (cadr relation) 
7776                                          (or (caddr relation) "") 0 0 ""))))
7777         (push gnus-reffed-article-number gnus-newsgroup-limit)
7778         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7779         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7780               gnus-newsgroup-reads)
7781         (decf gnus-reffed-article-number)
7782         ;; Make this new thread the child of its parent.
7783         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7784             (setcdr (symbol-value pthread)
7785                     (nconc (cdr (symbol-value pthread))
7786                            (list (symbol-value cthread))))
7787           (set pthread (list nil (symbol-value cthread))))))
7788     (gnus-message 7 "Making sparse threads...done")))
7789
7790 (defun gnus-build-old-threads ()
7791   ;; Look at all the articles that refer back to old articles, and
7792   ;; fetch the headers for the articles that aren't there.  This will
7793   ;; build complete threads - if the roots haven't been expired by the
7794   ;; server, that is.
7795   (let (id heads)
7796     (mapatoms
7797      (lambda (refs)
7798        (when (not (car (symbol-value refs)))
7799          (setq heads (cdr (symbol-value refs)))
7800          (while heads
7801            (if (memq (mail-header-number (caar heads))
7802                      gnus-newsgroup-dormant)
7803                (setq heads (cdr heads))
7804              (setq id (symbol-name refs))
7805              (while (and (setq id (gnus-build-get-header id))
7806                          (not (car (gnus-gethash
7807                                     id gnus-newsgroup-dependencies)))))
7808              (setq heads nil)))))
7809      gnus-newsgroup-dependencies)))
7810
7811 (defun gnus-build-get-header (id)
7812   ;; Look through the buffer of NOV lines and find the header to
7813   ;; ID.  Enter this line into the dependencies hash table, and return
7814   ;; the id of the parent article (if any).
7815   (let ((deps gnus-newsgroup-dependencies)
7816         found header)
7817     (prog1
7818         (save-excursion
7819           (set-buffer nntp-server-buffer)
7820           (goto-char (point-min))
7821           (while (and (not found) (search-forward id nil t))
7822             (beginning-of-line)
7823             (setq found (looking-at
7824                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7825                                  (regexp-quote id))))
7826             (or found (beginning-of-line 2)))
7827           (when found
7828             (beginning-of-line)
7829             (and
7830              (setq header (gnus-nov-parse-line
7831                            (read (current-buffer)) deps))
7832              (gnus-parent-id (mail-header-references header)))))
7833       (when header
7834         (let ((number (mail-header-number header)))
7835           (push number gnus-newsgroup-limit)
7836           (push header gnus-newsgroup-headers)
7837           (if (memq number gnus-newsgroup-unselected)
7838               (progn
7839                 (push number gnus-newsgroup-unreads)
7840                 (setq gnus-newsgroup-unselected
7841                       (delq number gnus-newsgroup-unselected)))
7842             (push number gnus-newsgroup-ancient)))))))
7843
7844 (defun gnus-summary-update-article (article &optional header)
7845   "Update ARTICLE in the summary buffer."
7846   (let ((id (mail-header-id (gnus-summary-article-header article)))
7847         (data (gnus-data-find article)))
7848     (setcar (gnus-id-to-thread id) nil)
7849     (gnus-summary-insert-subject id)
7850     ;; Set the (possibly) new article number in the data structure.
7851     (gnus-data-set-number data (gnus-id-to-article id))))
7852
7853 (defun gnus-rebuild-thread (id)
7854   "Rebuild the thread containing ID."
7855   (let ((buffer-read-only nil)
7856         current thread data)
7857     (if (not gnus-show-threads)
7858         (setq thread (list (car (gnus-id-to-thread id))))
7859       ;; Get the thread this article is part of.
7860       (setq thread (gnus-remove-thread id)))
7861     (setq current (save-excursion
7862                     (and (zerop (forward-line -1))
7863                          (gnus-summary-article-number))))
7864     ;; If this is a gathered thread, we have to go some re-gathering.
7865     (when (stringp (car thread))
7866       (let ((subject (car thread))
7867             roots thr)
7868         (setq thread (cdr thread))
7869         (while thread
7870           (unless (memq (setq thr (gnus-id-to-thread
7871                                       (gnus-root-id
7872                                        (mail-header-id (caar thread)))))
7873                         roots)
7874             (push thr roots))
7875           (setq thread (cdr thread)))
7876         ;; We now have all (unique) roots.
7877         (if (= (length roots) 1)
7878             ;; All the loose roots are now one solid root.
7879             (setq thread (car roots))
7880           (setq thread (cons subject (gnus-sort-threads roots))))))
7881     (let (threads)
7882       ;; We then insert this thread into the summary buffer.
7883       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7884         (gnus-summary-prepare-threads (list thread))
7885         (setq data (nreverse gnus-newsgroup-data))
7886         (setq threads gnus-newsgroup-threads))
7887       ;; We splice the new data into the data structure.
7888       (gnus-data-enter-list current data)
7889       (gnus-data-compute-positions)
7890       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7891
7892 (defun gnus-id-to-thread (id)
7893   "Return the (sub-)thread where ID appears."
7894   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7895
7896 (defun gnus-id-to-article (id)
7897   "Return the article number of ID."
7898   (let ((thread (gnus-id-to-thread id)))
7899     (when thread
7900       (mail-header-number (car thread)))))
7901
7902 (defun gnus-id-to-header (id)
7903   "Return the article headers of ID."
7904   (car (gnus-id-to-thread id)))
7905
7906 (defun gnus-article-displayed-root-p (article)
7907   "Say whether ARTICLE is a root(ish) article."
7908   (let ((level (gnus-summary-thread-level article))
7909         particle)
7910     (cond 
7911      ((null level) nil)
7912      ((zerop level) t)
7913      ((and (= 1 level)
7914            (null (setq particle (gnus-id-to-article
7915                                  (gnus-parent-id 
7916                                   (mail-header-references 
7917                                    (gnus-summary-article-header article))))))
7918            (null (gnus-summary-thread-level particle)))))))
7919
7920 (defun gnus-root-id (id)
7921   "Return the id of the root of the thread where ID appears."
7922   (let (last-id prev)
7923     (while (and id (setq prev (car (gnus-gethash
7924                                     (downcase id)
7925                                     gnus-newsgroup-dependencies))))
7926       (setq last-id id
7927             id (gnus-parent-id (mail-header-references prev))))
7928     last-id))
7929
7930 (defun gnus-remove-thread (id &optional dont-remove)
7931   "Remove the thread that has ID in it."
7932   (let ((dep gnus-newsgroup-dependencies)
7933         headers thread last-id)
7934     ;; First go up in this thread until we find the root.
7935     (setq last-id (gnus-root-id id))
7936     (setq headers (list (car (gnus-id-to-thread last-id))
7937                         (caadr (gnus-id-to-thread last-id))))
7938     ;; We have now found the real root of this thread.  It might have
7939     ;; been gathered into some loose thread, so we have to search
7940     ;; through the threads to find the thread we wanted.
7941     (let ((threads gnus-newsgroup-threads)
7942           sub)
7943       (while threads
7944         (setq sub (car threads))
7945         (if (stringp (car sub))
7946             ;; This is a gathered threads, so we look at the roots
7947             ;; below it to find whether this article in in this
7948             ;; gathered root.
7949             (progn
7950               (setq sub (cdr sub))
7951               (while sub
7952                 (when (member (caar sub) headers)
7953                   (setq thread (car threads)
7954                         threads nil
7955                         sub nil))
7956                 (setq sub (cdr sub))))
7957           ;; It's an ordinary thread, so we check it.
7958           (when (eq (car sub) (car headers))
7959             (setq thread sub
7960                   threads nil)))
7961         (setq threads (cdr threads)))
7962       ;; If this article is in no thread, then it's a root.
7963       (if thread
7964           (unless dont-remove
7965             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7966         (setq thread (gnus-gethash (downcase last-id) dep)))
7967       (when thread
7968         (prog1
7969             thread ; We return this thread.
7970           (unless dont-remove
7971             (if (stringp (car thread))
7972                 (progn
7973                   ;; If we use dummy roots, then we have to remove the
7974                   ;; dummy root as well.
7975                   (when (eq gnus-summary-make-false-root 'dummy)
7976                     ;; Uhm.
7977                     )
7978                   (setq thread (cdr thread))
7979                   (while thread
7980                     (gnus-remove-thread-1 (car thread))
7981                     (setq thread (cdr thread))))
7982               (gnus-remove-thread-1 thread))))))))
7983
7984 (defun gnus-remove-thread-1 (thread)
7985   "Remove the thread THREAD recursively."
7986   (let ((number (mail-header-number (car thread)))
7987         pos)
7988     (when (setq pos (text-property-any
7989                      (point-min) (point-max) 'gnus-number number))
7990       (goto-char pos)
7991       (gnus-delete-line)
7992       (gnus-data-remove number))
7993     (setq thread (cdr thread))
7994     (while thread
7995       (gnus-remove-thread-1 (car thread))
7996       (setq thread (cdr thread)))))
7997
7998 (defun gnus-sort-threads (threads)
7999   "Sort THREADS."
8000   (if (not gnus-thread-sort-functions)
8001       threads
8002     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8003                     (car gnus-thread-sort-functions)
8004                   `(lambda (t1 t2)
8005                      ,(gnus-make-sort-function 
8006                        (reverse gnus-thread-sort-functions))))))
8007       (gnus-message 7 "Sorting threads...")
8008       (prog1
8009           (sort threads func)
8010         (gnus-message 7 "Sorting threads...done")))))
8011
8012 (defun gnus-sort-articles (articles)
8013   "Sort ARTICLES."
8014   (when gnus-article-sort-functions
8015     (let ((func (if (= 1 (length gnus-article-sort-functions))
8016                     (car gnus-article-sort-functions)
8017                   `(lambda (t1 t2)
8018                      ,(gnus-make-sort-function 
8019                        (reverse gnus-article-sort-functions))))))
8020       (gnus-message 7 "Sorting articles...")
8021       (prog1
8022           (setq gnus-newsgroup-headers (sort articles func))
8023         (gnus-message 7 "Sorting articles...done")))))
8024
8025 (defun gnus-make-sort-function (funs)
8026   "Return a composite sort condition based on the functions in FUNC."
8027   (if (cdr funs)
8028       `(or (,(car funs) t1 t2)
8029            (and (not (,(car funs) t2 t1))
8030                 ,(gnus-make-sort-function (cdr funs))))
8031     `(,(car funs) t1 t2)))
8032                  
8033 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8034 (defmacro gnus-thread-header (thread)
8035   ;; Return header of first article in THREAD.
8036   ;; Note that THREAD must never, ever be anything else than a variable -
8037   ;; using some other form will lead to serious barfage.
8038   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8039   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8040   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8041         (vector thread) 2))
8042
8043 (defsubst gnus-article-sort-by-number (h1 h2)
8044   "Sort articles by article number."
8045   (< (mail-header-number h1)
8046      (mail-header-number h2)))
8047
8048 (defun gnus-thread-sort-by-number (h1 h2)
8049   "Sort threads by root article number."
8050   (gnus-article-sort-by-number
8051    (gnus-thread-header h1) (gnus-thread-header h2)))
8052
8053 (defsubst gnus-article-sort-by-author (h1 h2)
8054   "Sort articles by root author."
8055   (string-lessp
8056    (let ((extract (funcall
8057                    gnus-extract-address-components
8058                    (mail-header-from h1))))
8059      (or (car extract) (cdr extract)))
8060    (let ((extract (funcall
8061                    gnus-extract-address-components
8062                    (mail-header-from h2))))
8063      (or (car extract) (cdr extract)))))
8064
8065 (defun gnus-thread-sort-by-author (h1 h2)
8066   "Sort threads by root author."
8067   (gnus-article-sort-by-author
8068    (gnus-thread-header h1)  (gnus-thread-header h2)))
8069
8070 (defsubst gnus-article-sort-by-subject (h1 h2)
8071   "Sort articles by root subject."
8072   (string-lessp
8073    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8074    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8075
8076 (defun gnus-thread-sort-by-subject (h1 h2)
8077   "Sort threads by root subject."
8078   (gnus-article-sort-by-subject
8079    (gnus-thread-header h1) (gnus-thread-header h2)))
8080
8081 (defsubst gnus-article-sort-by-date (h1 h2)
8082   "Sort articles by root article date."
8083   (string-lessp
8084    (gnus-sortable-date (mail-header-date h1))
8085    (gnus-sortable-date (mail-header-date h2))))
8086
8087 (defun gnus-thread-sort-by-date (h1 h2)
8088   "Sort threads by root article date."
8089   (gnus-article-sort-by-date
8090    (gnus-thread-header h1) (gnus-thread-header h2)))
8091
8092 (defsubst gnus-article-sort-by-score (h1 h2)
8093   "Sort articles by root article score.
8094 Unscored articles will be counted as having a score of zero."
8095   (> (or (cdr (assq (mail-header-number h1)
8096                     gnus-newsgroup-scored))
8097          gnus-summary-default-score 0)
8098      (or (cdr (assq (mail-header-number h2)
8099                     gnus-newsgroup-scored))
8100          gnus-summary-default-score 0)))
8101
8102 (defun gnus-thread-sort-by-score (h1 h2)
8103   "Sort threads by root article score."
8104   (gnus-article-sort-by-score
8105    (gnus-thread-header h1) (gnus-thread-header h2)))
8106
8107 (defun gnus-thread-sort-by-total-score (h1 h2)
8108   "Sort threads by the sum of all scores in the thread.
8109 Unscored articles will be counted as having a score of zero."
8110   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8111
8112 (defun gnus-thread-total-score (thread)
8113   ;;  This function find the total score of THREAD.
8114   (if (consp thread)
8115       (if (stringp (car thread))
8116           (apply gnus-thread-score-function 0
8117                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8118         (gnus-thread-total-score-1 thread))
8119     (gnus-thread-total-score-1 (list thread))))
8120
8121 (defun gnus-thread-total-score-1 (root)
8122   ;; This function find the total score of the thread below ROOT.
8123   (setq root (car root))
8124   (apply gnus-thread-score-function
8125          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8126              gnus-summary-default-score 0)
8127          (mapcar 'gnus-thread-total-score
8128                  (cdr (gnus-gethash (downcase (mail-header-id root))
8129                                     gnus-newsgroup-dependencies)))))
8130
8131 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8132 (defvar gnus-tmp-prev-subject nil)
8133 (defvar gnus-tmp-false-parent nil)
8134 (defvar gnus-tmp-root-expunged nil)
8135 (defvar gnus-tmp-dummy-line nil)
8136
8137 (defun gnus-summary-prepare-threads (threads)
8138   "Prepare summary buffer from THREADS and indentation LEVEL.
8139 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8140 or a straight list of headers."
8141   (gnus-message 7 "Generating summary...")
8142
8143   (setq gnus-newsgroup-threads threads)
8144   (beginning-of-line)
8145
8146   (let ((gnus-tmp-level 0)
8147         (default-score (or gnus-summary-default-score 0))
8148         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8149         thread number subject stack state gnus-tmp-gathered beg-match
8150         new-roots gnus-tmp-new-adopts thread-end
8151         gnus-tmp-header gnus-tmp-unread
8152         gnus-tmp-replied gnus-tmp-subject-or-nil
8153         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8154         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8155         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8156
8157     (setq gnus-tmp-prev-subject nil)
8158
8159     (if (vectorp (car threads))
8160         ;; If this is a straight (sic) list of headers, then a
8161         ;; threaded summary display isn't required, so we just create
8162         ;; an unthreaded one.
8163         (gnus-summary-prepare-unthreaded threads)
8164
8165       ;; Do the threaded display.
8166
8167       (while (or threads stack gnus-tmp-new-adopts new-roots)
8168
8169         (if (and (= gnus-tmp-level 0)
8170                  (not (setq gnus-tmp-dummy-line nil))
8171                  (or (not stack)
8172                      (= (caar stack) 0))
8173                  (not gnus-tmp-false-parent)
8174                  (or gnus-tmp-new-adopts new-roots))
8175             (if gnus-tmp-new-adopts
8176                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8177                       thread (list (car gnus-tmp-new-adopts))
8178                       gnus-tmp-header (caar thread)
8179                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8180               (if new-roots
8181                   (setq thread (list (car new-roots))
8182                         gnus-tmp-header (caar thread)
8183                         new-roots (cdr new-roots))))
8184
8185           (if threads
8186               ;; If there are some threads, we do them before the
8187               ;; threads on the stack.
8188               (setq thread threads
8189                     gnus-tmp-header (caar thread))
8190             ;; There were no current threads, so we pop something off
8191             ;; the stack.
8192             (setq state (car stack)
8193                   gnus-tmp-level (car state)
8194                   thread (cdr state)
8195                   stack (cdr stack)
8196                   gnus-tmp-header (caar thread))))
8197
8198         (setq gnus-tmp-false-parent nil)
8199         (setq gnus-tmp-root-expunged nil)
8200         (setq thread-end nil)
8201
8202         (if (stringp gnus-tmp-header)
8203             ;; The header is a dummy root.
8204             (cond
8205              ((eq gnus-summary-make-false-root 'adopt)
8206               ;; We let the first article adopt the rest.
8207               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8208                                                (cddar thread)))
8209               (setq gnus-tmp-gathered
8210                     (nconc (mapcar
8211                             (lambda (h) (mail-header-number (car h)))
8212                             (cddar thread))
8213                            gnus-tmp-gathered))
8214               (setq thread (cons (list (caar thread)
8215                                        (cadar thread))
8216                                  (cdr thread)))
8217               (setq gnus-tmp-level -1
8218                     gnus-tmp-false-parent t))
8219              ((eq gnus-summary-make-false-root 'empty)
8220               ;; We print adopted articles with empty subject fields.
8221               (setq gnus-tmp-gathered
8222                     (nconc (mapcar
8223                             (lambda (h) (mail-header-number (car h)))
8224                             (cddar thread))
8225                            gnus-tmp-gathered))
8226               (setq gnus-tmp-level -1))
8227              ((eq gnus-summary-make-false-root 'dummy)
8228               ;; We remember that we probably want to output a dummy
8229               ;; root.
8230               (setq gnus-tmp-dummy-line gnus-tmp-header)
8231               (setq gnus-tmp-prev-subject gnus-tmp-header))
8232              (t
8233               ;; We do not make a root for the gathered
8234               ;; sub-threads at all.
8235               (setq gnus-tmp-level -1)))
8236
8237           (setq number (mail-header-number gnus-tmp-header)
8238                 subject (mail-header-subject gnus-tmp-header))
8239
8240           (cond
8241            ;; If the thread has changed subject, we might want to make
8242            ;; this subthread into a root.
8243            ((and (null gnus-thread-ignore-subject)
8244                  (not (zerop gnus-tmp-level))
8245                  gnus-tmp-prev-subject
8246                  (not (inline
8247                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8248             (setq new-roots (nconc new-roots (list (car thread)))
8249                   thread-end t
8250                   gnus-tmp-header nil))
8251            ;; If the article lies outside the current limit,
8252            ;; then we do not display it.
8253            ((and (not (memq number gnus-newsgroup-limit))
8254                  (not gnus-tmp-dummy-line))
8255             (setq gnus-tmp-gathered
8256                   (nconc (mapcar
8257                           (lambda (h) (mail-header-number (car h)))
8258                           (cdar thread))
8259                          gnus-tmp-gathered))
8260             (setq gnus-tmp-new-adopts (if (cdar thread)
8261                                           (append gnus-tmp-new-adopts
8262                                                   (cdar thread))
8263                                         gnus-tmp-new-adopts)
8264                   thread-end t
8265                   gnus-tmp-header nil)
8266             (when (zerop gnus-tmp-level)
8267               (setq gnus-tmp-root-expunged t)))
8268            ;; Perhaps this article is to be marked as read?
8269            ((and gnus-summary-mark-below
8270                  (< (or (cdr (assq number gnus-newsgroup-scored))
8271                         default-score)
8272                     gnus-summary-mark-below)
8273                  ;; Don't touch sparse articles.
8274                  (not (memq number gnus-newsgroup-sparse)))
8275             (setq gnus-newsgroup-unreads
8276                   (delq number gnus-newsgroup-unreads))
8277             (if gnus-newsgroup-auto-expire
8278                 (push number gnus-newsgroup-expirable)
8279               (push (cons number gnus-low-score-mark)
8280                     gnus-newsgroup-reads))))
8281
8282           (when gnus-tmp-header
8283             ;; We may have an old dummy line to output before this
8284             ;; article.
8285             (when gnus-tmp-dummy-line
8286               (gnus-summary-insert-dummy-line
8287                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8288               (setq gnus-tmp-dummy-line nil))
8289
8290             ;; Compute the mark.
8291             (setq
8292              gnus-tmp-unread
8293              (cond
8294               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8295               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8296               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8297               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8298               (t (or (cdr (assq number gnus-newsgroup-reads))
8299                      gnus-ancient-mark))))
8300
8301             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8302                                   gnus-tmp-header gnus-tmp-level)
8303                   gnus-newsgroup-data)
8304
8305             ;; Actually insert the line.
8306             (setq
8307              gnus-tmp-subject-or-nil
8308              (cond
8309               ((and gnus-thread-ignore-subject
8310                     gnus-tmp-prev-subject
8311                     (not (inline (gnus-subject-equal
8312                                   gnus-tmp-prev-subject subject))))
8313                subject)
8314               ((zerop gnus-tmp-level)
8315                (if (and (eq gnus-summary-make-false-root 'empty)
8316                         (memq number gnus-tmp-gathered)
8317                         gnus-tmp-prev-subject
8318                         (inline (gnus-subject-equal
8319                                  gnus-tmp-prev-subject subject)))
8320                    gnus-summary-same-subject
8321                  subject))
8322               (t gnus-summary-same-subject)))
8323             (if (and (eq gnus-summary-make-false-root 'adopt)
8324                      (= gnus-tmp-level 1)
8325                      (memq number gnus-tmp-gathered))
8326                 (setq gnus-tmp-opening-bracket ?\<
8327                       gnus-tmp-closing-bracket ?\>)
8328               (setq gnus-tmp-opening-bracket ?\[
8329                     gnus-tmp-closing-bracket ?\]))
8330             (setq
8331              gnus-tmp-indentation
8332              (aref gnus-thread-indent-array gnus-tmp-level)
8333              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8334              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8335                                 gnus-summary-default-score 0)
8336              gnus-tmp-score-char
8337              (if (or (null gnus-summary-default-score)
8338                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8339                          gnus-summary-zcore-fuzz)) ? 
8340                (if (< gnus-tmp-score gnus-summary-default-score)
8341                    gnus-score-below-mark gnus-score-over-mark))
8342              gnus-tmp-replied
8343              (cond ((memq number gnus-newsgroup-processable)
8344                     gnus-process-mark)
8345                    ((memq number gnus-newsgroup-cached)
8346                     gnus-cached-mark)
8347                    ((memq number gnus-newsgroup-replied)
8348                     gnus-replied-mark)
8349                    (t gnus-unread-mark))
8350              gnus-tmp-from (mail-header-from gnus-tmp-header)
8351              gnus-tmp-name
8352              (cond
8353               ((string-match "(.+)" gnus-tmp-from)
8354                (substring gnus-tmp-from
8355                           (1+ (match-beginning 0)) (1- (match-end 0))))
8356               ((string-match "<[^>]+> *$" gnus-tmp-from)
8357                (setq beg-match (match-beginning 0))
8358                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8359                         (substring gnus-tmp-from (1+ (match-beginning 0))
8360                                    (1- (match-end 0))))
8361                    (substring gnus-tmp-from 0 beg-match)))
8362               (t gnus-tmp-from)))
8363             (when (string= gnus-tmp-name "")
8364               (setq gnus-tmp-name gnus-tmp-from))
8365             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8366             (put-text-property
8367              (point)
8368              (progn (eval gnus-summary-line-format-spec) (point))
8369              'gnus-number number)
8370             (when gnus-visual-p
8371               (forward-line -1)
8372               (run-hooks 'gnus-summary-update-hook)
8373               (forward-line 1))
8374
8375             (setq gnus-tmp-prev-subject subject)))
8376
8377         (when (nth 1 thread)
8378           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8379         (incf gnus-tmp-level)
8380         (setq threads (if thread-end nil (cdar thread)))
8381         (unless threads
8382           (setq gnus-tmp-level 0)))))
8383   (gnus-message 7 "Generating summary...done"))
8384
8385 (defun gnus-summary-prepare-unthreaded (headers)
8386   "Generate an unthreaded summary buffer based on HEADERS."
8387   (let (header number mark)
8388
8389     (while headers
8390       (setq header (car headers)
8391             headers (cdr headers)
8392             number (mail-header-number header))
8393
8394       ;; We may have to root out some bad articles...
8395       (when (memq number gnus-newsgroup-limit)
8396         (when (and gnus-summary-mark-below
8397                    (< (or (cdr (assq number gnus-newsgroup-scored))
8398                           gnus-summary-default-score 0)
8399                       gnus-summary-mark-below))
8400           (setq gnus-newsgroup-unreads
8401                 (delq number gnus-newsgroup-unreads))
8402           (if gnus-newsgroup-auto-expire
8403               (push number gnus-newsgroup-expirable)
8404             (push (cons number gnus-low-score-mark)
8405                   gnus-newsgroup-reads)))
8406
8407         (setq mark
8408               (cond
8409                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8410                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8411                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8412                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8413                (t (or (cdr (assq number gnus-newsgroup-reads))
8414                       gnus-ancient-mark))))
8415         (setq gnus-newsgroup-data
8416               (cons (gnus-data-make number mark (1+ (point)) header 0)
8417                     gnus-newsgroup-data))
8418         (gnus-summary-insert-line
8419          header 0 nil mark (memq number gnus-newsgroup-replied)
8420          (memq number gnus-newsgroup-expirable)
8421          (mail-header-subject header) nil
8422          (cdr (assq number gnus-newsgroup-scored))
8423          (memq number gnus-newsgroup-processable))))))
8424
8425 (defun gnus-select-newsgroup (group &optional read-all)
8426   "Select newsgroup GROUP.
8427 If READ-ALL is non-nil, all articles in the group are selected."
8428   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8429          (info (nth 2 entry))
8430          articles fetched-articles cached)
8431
8432     (or (gnus-check-server
8433          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8434         (error "Couldn't open server"))
8435
8436     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8437         (gnus-activate-group group) ; Or we can activate it...
8438         (progn ; Or we bug out.
8439           (when (equal major-mode 'gnus-summary-mode)
8440             (kill-buffer (current-buffer)))
8441           (error "Couldn't request group %s: %s"
8442                  group (gnus-status-message group))))
8443
8444     (setq gnus-newsgroup-name group)
8445     (setq gnus-newsgroup-unselected nil)
8446     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8447
8448     (and gnus-asynchronous
8449          (gnus-check-backend-function
8450           'request-asynchronous gnus-newsgroup-name)
8451          (setq gnus-newsgroup-async
8452                (gnus-request-asynchronous gnus-newsgroup-name)))
8453
8454     ;; Adjust and set lists of article marks.
8455     (when info
8456       (gnus-adjust-marked-articles info))
8457
8458     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8459     (when (gnus-virtual-group-p group)
8460       (setq cached gnus-newsgroup-cached))
8461
8462     (setq gnus-newsgroup-unreads
8463           (gnus-set-difference
8464            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8465            gnus-newsgroup-dormant))
8466
8467     (setq gnus-newsgroup-processable nil)
8468
8469     (setq articles (gnus-articles-to-read group read-all))
8470
8471     (cond
8472      ((null articles)
8473       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8474       'quit)
8475      ((eq articles 0) nil)
8476      (t
8477       ;; Init the dependencies hash table.
8478       (setq gnus-newsgroup-dependencies
8479             (gnus-make-hashtable (length articles)))
8480       ;; Retrieve the headers and read them in.
8481       (gnus-message 5 "Fetching headers...")
8482       (setq gnus-newsgroup-headers
8483             (if (eq 'nov
8484                     (setq gnus-headers-retrieved-by
8485                           (gnus-retrieve-headers
8486                            articles gnus-newsgroup-name
8487                            ;; We might want to fetch old headers, but
8488                            ;; not if there is only 1 article.
8489                            (and gnus-fetch-old-headers
8490                                 (or (and
8491                                      (not (eq gnus-fetch-old-headers 'some))
8492                                      (not (numberp gnus-fetch-old-headers)))
8493                                     (> (length articles) 1))))))
8494                 (gnus-get-newsgroup-headers-xover articles)
8495               (gnus-get-newsgroup-headers)))
8496       (gnus-message 5 "Fetching headers...done")
8497
8498       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8499       (when cached
8500         (setq gnus-newsgroup-cached cached))
8501
8502       ;; Set the initial limit.
8503       (setq gnus-newsgroup-limit (copy-sequence articles))
8504       ;; Remove canceled articles from the list of unread articles.
8505       (setq gnus-newsgroup-unreads
8506             (gnus-set-sorted-intersection
8507              gnus-newsgroup-unreads
8508              (setq fetched-articles
8509                    (mapcar (lambda (headers) (mail-header-number headers))
8510                            gnus-newsgroup-headers))))
8511       ;; Removed marked articles that do not exist.
8512       (gnus-update-missing-marks
8513        (gnus-sorted-complement fetched-articles articles))
8514       ;; We might want to build some more threads first.
8515       (and gnus-fetch-old-headers
8516            (eq gnus-headers-retrieved-by 'nov)
8517            (gnus-build-old-threads))
8518       ;; Check whether auto-expire is to be done in this group.
8519       (setq gnus-newsgroup-auto-expire
8520             (gnus-group-auto-expirable-p group))
8521       ;; Set up the article buffer now, if necessary.
8522       (unless gnus-single-article-buffer
8523         (gnus-article-setup-buffer))
8524       ;; First and last article in this newsgroup.
8525       (and gnus-newsgroup-headers
8526            (setq gnus-newsgroup-begin
8527                  (mail-header-number (car gnus-newsgroup-headers)))
8528            (setq gnus-newsgroup-end
8529                  (mail-header-number
8530                   (gnus-last-element gnus-newsgroup-headers))))
8531       (setq gnus-reffed-article-number -1)
8532       ;; GROUP is successfully selected.
8533       (or gnus-newsgroup-headers t)))))
8534
8535 (defun gnus-articles-to-read (group read-all)
8536   ;; Find out what articles the user wants to read.
8537   (let* ((articles
8538           ;; Select all articles if `read-all' is non-nil, or if there
8539           ;; are no unread articles.
8540           (if (or read-all
8541                   (and (zerop (length gnus-newsgroup-marked))
8542                        (zerop (length gnus-newsgroup-unreads))))
8543               (gnus-uncompress-range (gnus-active group))
8544             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8545                           (copy-sequence gnus-newsgroup-unreads))
8546                   '<)))
8547          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8548          (scored (length scored-list))
8549          (number (length articles))
8550          (marked (+ (length gnus-newsgroup-marked)
8551                     (length gnus-newsgroup-dormant)))
8552          (select
8553           (cond
8554            ((numberp read-all)
8555             read-all)
8556            (t
8557             (condition-case ()
8558                 (cond
8559                  ((and (or (<= scored marked) (= scored number))
8560                        (numberp gnus-large-newsgroup)
8561                        (> number gnus-large-newsgroup))
8562                   (let ((input
8563                          (read-string
8564                           (format
8565                            "How many articles from %s (default %d): "
8566                            gnus-newsgroup-name number))))
8567                     (if (string-match "^[ \t]*$" input) number input)))
8568                  ((and (> scored marked) (< scored number))
8569                   (let ((input
8570                          (read-string
8571                           (format "%s %s (%d scored, %d total): "
8572                                   "How many articles from"
8573                                   group scored number))))
8574                     (if (string-match "^[ \t]*$" input)
8575                         number input)))
8576                  (t number))
8577               (quit nil))))))
8578     (setq select (if (stringp select) (string-to-number select) select))
8579     (if (or (null select) (zerop select))
8580         select
8581       (if (and (not (zerop scored)) (<= (abs select) scored))
8582           (progn
8583             (setq articles (sort scored-list '<))
8584             (setq number (length articles)))
8585         (setq articles (copy-sequence articles)))
8586
8587       (if (< (abs select) number)
8588           (if (< select 0)
8589               ;; Select the N oldest articles.
8590               (setcdr (nthcdr (1- (abs select)) articles) nil)
8591             ;; Select the N most recent articles.
8592             (setq articles (nthcdr (- number select) articles))))
8593       (setq gnus-newsgroup-unselected
8594             (gnus-sorted-intersection
8595              gnus-newsgroup-unreads
8596              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8597       articles)))
8598
8599 (defun gnus-killed-articles (killed articles)
8600   (let (out)
8601     (while articles
8602       (if (inline (gnus-member-of-range (car articles) killed))
8603           (setq out (cons (car articles) out)))
8604       (setq articles (cdr articles)))
8605     out))
8606
8607 (defun gnus-uncompress-marks (marks)
8608   "Uncompress the mark ranges in MARKS."
8609   (let ((uncompressed '(score bookmark))
8610         out)
8611     (while marks
8612       (if (memq (caar marks) uncompressed)
8613           (push (car marks) out)
8614         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8615       (setq marks (cdr marks)))
8616     out))
8617
8618 (defun gnus-adjust-marked-articles (info)
8619   "Set all article lists and remove all marks that are no longer legal."
8620   (let* ((marked-lists (gnus-info-marks info))
8621          (active (gnus-active (gnus-info-group info)))
8622          (min (car active))
8623          (max (cdr active))
8624          (types gnus-article-mark-lists)
8625          (uncompressed '(score bookmark))
8626          marks var articles article mark)
8627
8628     (while marked-lists
8629       (setq marks (pop marked-lists))
8630       (set (setq var (intern (format "gnus-newsgroup-%s"
8631                                      (car (rassq (setq mark (car marks))
8632                                                  types)))))
8633            (if (memq (car marks) uncompressed) (cdr marks)
8634              (gnus-uncompress-range (cdr marks))))
8635
8636       (setq articles (symbol-value var))
8637
8638       ;; All articles have to be subsets of the active articles.
8639       (cond
8640        ;; Adjust "simple" lists.
8641        ((memq mark '(tick dormant expirable reply killed save))
8642         (while articles
8643           (when (or (< (setq article (pop articles)) min) (> article max))
8644             (set var (delq article (symbol-value var))))))
8645        ;; Adjust assocs.
8646        ((memq mark '(score bookmark))
8647         (while articles
8648           (when (or (< (car (setq article (pop articles))) min)
8649                     (> (car article) max))
8650             (set var (delq article (symbol-value var))))))))))
8651
8652 (defun gnus-update-missing-marks (missing)
8653   "Go through the list of MISSING articles and remove them mark lists."
8654   (when missing
8655     (let ((types gnus-article-mark-lists)
8656           var m)
8657       ;; Go through all types.
8658       (while types
8659         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8660         (when (symbol-value var)
8661           ;; This list has articles.  So we delete all missing articles
8662           ;; from it.
8663           (setq m missing)
8664           (while m
8665             (set var (delq (pop m) (symbol-value var)))))))))
8666
8667 (defun gnus-update-marks ()
8668   "Enter the various lists of marked articles into the newsgroup info list."
8669   (let ((types gnus-article-mark-lists)
8670         (info (gnus-get-info gnus-newsgroup-name))
8671         (uncompressed '(score bookmark killed))
8672         type list newmarked symbol)
8673     (when info
8674       ;; Add all marks lists that are non-nil to the list of marks lists.
8675       (while types
8676         (setq type (pop types))
8677         (when (setq list (symbol-value
8678                           (setq symbol
8679                                 (intern (format "gnus-newsgroup-%s"
8680                                                 (car type))))))
8681           (push (cons (cdr type)
8682                       (if (memq (cdr type) uncompressed) list
8683                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8684                 newmarked)))
8685
8686       ;; Enter these new marks into the info of the group.
8687       (if (nthcdr 3 info)
8688           (setcar (nthcdr 3 info) newmarked)
8689         ;; Add the marks lists to the end of the info.
8690         (when newmarked
8691           (setcdr (nthcdr 2 info) (list newmarked))))
8692
8693       ;; Cut off the end of the info if there's nothing else there.
8694       (let ((i 5))
8695         (while (and (> i 2)
8696                     (not (nth i info)))
8697           (when (nthcdr (decf i) info)
8698             (setcdr (nthcdr i info) nil)))))))
8699
8700 (defun gnus-add-marked-articles (group type articles &optional info force)
8701   ;; Add ARTICLES of TYPE to the info of GROUP.
8702   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8703   ;; add, but replace marked articles of TYPE with ARTICLES.
8704   (let ((info (or info (gnus-get-info group)))
8705         (uncompressed '(score bookmark killed))
8706         marked m)
8707     (or (not info)
8708         (and (not (setq marked (nthcdr 3 info)))
8709              (or (null articles)
8710                  (setcdr (nthcdr 2 info)
8711                          (list (list (cons type (gnus-compress-sequence
8712                                                  articles t)))))))
8713         (and (not (setq m (assq type (car marked))))
8714              (or (null articles)
8715                  (setcar marked
8716                          (cons (cons type (gnus-compress-sequence articles t) )
8717                                (car marked)))))
8718         (if force
8719             (if (null articles)
8720                 (setcar (nthcdr 3 info)
8721                         (delq (assq type (car marked)) (car marked)))
8722               (setcdr m (gnus-compress-sequence articles t)))
8723           (setcdr m (gnus-compress-sequence
8724                      (sort (nconc (gnus-uncompress-range m)
8725                                   (copy-sequence articles)) '<) t))))))
8726
8727 (defun gnus-set-mode-line (where)
8728   "This function sets the mode line of the article or summary buffers.
8729 If WHERE is `summary', the summary mode line format will be used."
8730   ;; Is this mode line one we keep updated?
8731   (when (memq where gnus-updated-mode-lines)
8732     (let (mode-string)
8733       (save-excursion
8734         ;; We evaluate this in the summary buffer since these
8735         ;; variables are buffer-local to that buffer.
8736         (set-buffer gnus-summary-buffer)
8737         ;; We bind all these variables that are used in the `eval' form
8738         ;; below.
8739         (let* ((mformat (symbol-value
8740                          (intern
8741                           (format "gnus-%s-mode-line-format-spec" where))))
8742                (gnus-tmp-group-name gnus-newsgroup-name)
8743                (gnus-tmp-article-number (or gnus-current-article 0))
8744                (gnus-tmp-unread gnus-newsgroup-unreads)
8745                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8746                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8747                (gnus-tmp-unread-and-unselected
8748                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8749                             (zerop gnus-tmp-unselected)) "")
8750                       ((zerop gnus-tmp-unselected)
8751                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8752                       (t (format "{%d(+%d) more}"
8753                                  gnus-tmp-unread-and-unticked
8754                                  gnus-tmp-unselected))))
8755                (gnus-tmp-subject
8756                 (if (and gnus-current-headers
8757                          (vectorp gnus-current-headers))
8758                     (mail-header-subject gnus-current-headers) ""))
8759                max-len
8760                gnus-tmp-header);; passed as argument to any user-format-funcs
8761           (setq mode-string (eval mformat))
8762           (setq max-len (max 4 (if gnus-mode-non-string-length
8763                                    (- (frame-width)
8764                                       gnus-mode-non-string-length)
8765                                  (length mode-string))))
8766           ;; We might have to chop a bit of the string off...
8767           (when (> (length mode-string) max-len)
8768             (setq mode-string
8769                   (concat (gnus-truncate-string mode-string (- max-len 3))
8770                           "...")))
8771           ;; Pad the mode string a bit.
8772           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8773       ;; Update the mode line.
8774       (setq mode-line-buffer-identification (list mode-string))
8775       (set-buffer-modified-p t))))
8776
8777 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8778   "Go through the HEADERS list and add all Xrefs to a hash table.
8779 The resulting hash table is returned, or nil if no Xrefs were found."
8780   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8781          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8782          (xref-hashtb (make-vector 63 0))
8783          start group entry number xrefs header)
8784     (while headers
8785       (setq header (pop headers))
8786       (when (and (setq xrefs (mail-header-xref header))
8787                  (not (memq (setq number (mail-header-number header))
8788                             unreads)))
8789         (setq start 0)
8790         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8791           (setq start (match-end 0))
8792           (setq group (concat prefix (substring xrefs (match-beginning 1)
8793                                                 (match-end 1))))
8794           (setq number
8795                 (string-to-int (substring xrefs (match-beginning 2)
8796                                           (match-end 2))))
8797           (if (setq entry (gnus-gethash group xref-hashtb))
8798               (setcdr entry (cons number (cdr entry)))
8799             (gnus-sethash group (cons number nil) xref-hashtb)))))
8800     (and start xref-hashtb)))
8801
8802 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8803   "Look through all the headers and mark the Xrefs as read."
8804   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8805         name entry info xref-hashtb idlist method nth4)
8806     (save-excursion
8807       (set-buffer gnus-group-buffer)
8808       (when (setq xref-hashtb
8809                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8810         (mapatoms
8811          (lambda (group)
8812            (unless (string= from-newsgroup (setq name (symbol-name group)))
8813              (setq idlist (symbol-value group))
8814              ;; Dead groups are not updated.
8815              (and (prog1
8816                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8817                             info (nth 2 entry))
8818                     (if (stringp (setq nth4 (gnus-info-method info)))
8819                         (setq nth4 (gnus-server-to-method nth4))))
8820                   ;; Only do the xrefs if the group has the same
8821                   ;; select method as the group we have just read.
8822                   (or (gnus-methods-equal-p
8823                        nth4 (gnus-find-method-for-group from-newsgroup))
8824                       virtual
8825                       (equal nth4 (setq method (gnus-find-method-for-group
8826                                                 from-newsgroup)))
8827                       (and (equal (car nth4) (car method))
8828                            (equal (nth 1 nth4) (nth 1 method))))
8829                   gnus-use-cross-reference
8830                   (or (not (eq gnus-use-cross-reference t))
8831                       virtual
8832                       ;; Only do cross-references on subscribed
8833                       ;; groups, if that is what is wanted.
8834                       (<= (gnus-info-level info) gnus-level-subscribed))
8835                   (gnus-group-make-articles-read name idlist))))
8836          xref-hashtb)))))
8837
8838 (defun gnus-group-make-articles-read (group articles)
8839   (let* ((num 0)
8840          (entry (gnus-gethash group gnus-newsrc-hashtb))
8841          (info (nth 2 entry))
8842          (active (gnus-active group))
8843          range)
8844     ;; First peel off all illegal article numbers.
8845     (if active
8846         (let ((ids articles)
8847               id first)
8848           (while ids
8849             (setq id (car ids))
8850             (if (and first (> id (cdr active)))
8851                 (progn
8852                   ;; We'll end up in this situation in one particular
8853                   ;; obscure situation.  If you re-scan a group and get
8854                   ;; a new article that is cross-posted to a different
8855                   ;; group that has not been re-scanned, you might get
8856                   ;; crossposted article that has a higher number than
8857                   ;; Gnus believes possible.  So we re-activate this
8858                   ;; group as well.  This might mean doing the
8859                   ;; crossposting thingy will *increase* the number
8860                   ;; of articles in some groups.  Tsk, tsk.
8861                   (setq active (or (gnus-activate-group group) active))))
8862             (if (or (> id (cdr active))
8863                     (< id (car active)))
8864                 (setq articles (delq id articles)))
8865             (setq ids (cdr ids)))))
8866     ;; If the read list is nil, we init it.
8867     (and active
8868          (null (gnus-info-read info))
8869          (> (car active) 1)
8870          (gnus-info-set-read info (cons 1 (1- (car active)))))
8871     ;; Then we add the read articles to the range.
8872     (gnus-info-set-read
8873      info
8874      (setq range
8875            (gnus-add-to-range
8876             (gnus-info-read info) (setq articles (sort articles '<)))))
8877     ;; Then we have to re-compute how many unread
8878     ;; articles there are in this group.
8879     (if active
8880         (progn
8881           (cond
8882            ((not range)
8883             (setq num (- (1+ (cdr active)) (car active))))
8884            ((not (listp (cdr range)))
8885             (setq num (- (cdr active) (- (1+ (cdr range))
8886                                          (car range)))))
8887            (t
8888             (while range
8889               (if (numberp (car range))
8890                   (setq num (1+ num))
8891                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8892               (setq range (cdr range)))
8893             (setq num (- (cdr active) num))))
8894           ;; Update the number of unread articles.
8895           (setcar entry num)
8896           ;; Update the group buffer.
8897           (gnus-group-update-group group t)))))
8898
8899 (defun gnus-methods-equal-p (m1 m2)
8900   (let ((m1 (or m1 gnus-select-method))
8901         (m2 (or m2 gnus-select-method)))
8902     (or (equal m1 m2)
8903         (and (eq (car m1) (car m2))
8904              (or (not (memq 'address (assoc (symbol-name (car m1))
8905                                             gnus-valid-select-methods)))
8906                  (equal (nth 1 m1) (nth 1 m2)))))))
8907
8908 (defsubst gnus-header-value ()
8909   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8910
8911 (defvar gnus-newsgroup-none-id 0)
8912
8913 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8914   (let ((cur nntp-server-buffer)
8915         (dependencies
8916          (or dependencies
8917              (save-excursion (set-buffer gnus-summary-buffer)
8918                              gnus-newsgroup-dependencies)))
8919         headers id id-dep ref-dep end ref)
8920     (save-excursion
8921       (set-buffer nntp-server-buffer)
8922       (let ((case-fold-search t)
8923             in-reply-to header p lines)
8924         (goto-char (point-min))
8925         ;; Search to the beginning of the next header.  Error messages
8926         ;; do not begin with 2 or 3.
8927         (while (re-search-forward "^[23][0-9]+ " nil t)
8928           (setq id nil
8929                 ref nil)
8930           ;; This implementation of this function, with nine
8931           ;; search-forwards instead of the one re-search-forward and
8932           ;; a case (which basically was the old function) is actually
8933           ;; about twice as fast, even though it looks messier.  You
8934           ;; can't have everything, I guess.  Speed and elegance
8935           ;; doesn't always go hand in hand.
8936           (setq
8937            header
8938            (vector
8939             ;; Number.
8940             (prog1
8941                 (read cur)
8942               (end-of-line)
8943               (setq p (point))
8944               (narrow-to-region (point)
8945                                 (or (and (search-forward "\n.\n" nil t)
8946                                          (- (point) 2))
8947                                     (point))))
8948             ;; Subject.
8949             (progn
8950               (goto-char p)
8951               (if (search-forward "\nsubject: " nil t)
8952                   (gnus-header-value) "(none)"))
8953             ;; From.
8954             (progn
8955               (goto-char p)
8956               (if (search-forward "\nfrom: " nil t)
8957                   (gnus-header-value) "(nobody)"))
8958             ;; Date.
8959             (progn
8960               (goto-char p)
8961               (if (search-forward "\ndate: " nil t)
8962                   (gnus-header-value) ""))
8963             ;; Message-ID.
8964             (progn
8965               (goto-char p)
8966               (if (search-forward "\nmessage-id: " nil t)
8967                   (setq id (gnus-header-value))
8968                 ;; If there was no message-id, we just fake one to make
8969                 ;; subsequent routines simpler.
8970                 (setq id (concat "none+"
8971                                  (int-to-string
8972                                   (setq gnus-newsgroup-none-id
8973                                         (1+ gnus-newsgroup-none-id)))))))
8974             ;; References.
8975             (progn
8976               (goto-char p)
8977               (if (search-forward "\nreferences: " nil t)
8978                   (prog1
8979                       (gnus-header-value)
8980                     (setq end (match-end 0))
8981                     (save-excursion
8982                       (setq ref
8983                             (downcase
8984                              (buffer-substring
8985                               (progn
8986                                 (end-of-line)
8987                                 (search-backward ">" end t)
8988                                 (1+ (point)))
8989                               (progn
8990                                 (search-backward "<" end t)
8991                                 (point)))))))
8992                 ;; Get the references from the in-reply-to header if there
8993                 ;; were no references and the in-reply-to header looks
8994                 ;; promising.
8995                 (if (and (search-forward "\nin-reply-to: " nil t)
8996                          (setq in-reply-to (gnus-header-value))
8997                          (string-match "<[^>]+>" in-reply-to))
8998                     (prog1
8999                         (setq ref (substring in-reply-to (match-beginning 0)
9000                                              (match-end 0)))
9001                       (setq ref (downcase ref))))
9002                 (setq ref "")))
9003             ;; Chars.
9004             0
9005             ;; Lines.
9006             (progn
9007               (goto-char p)
9008               (if (search-forward "\nlines: " nil t)
9009                   (if (numberp (setq lines (read cur)))
9010                       lines 0)
9011                 0))
9012             ;; Xref.
9013             (progn
9014               (goto-char p)
9015               (and (search-forward "\nxref: " nil t)
9016                    (gnus-header-value)))))
9017           ;; We do the threading while we read the headers.  The
9018           ;; message-id and the last reference are both entered into
9019           ;; the same hash table.  Some tippy-toeing around has to be
9020           ;; done in case an article has arrived before the article
9021           ;; which it refers to.
9022           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9023               (if (and (car (symbol-value id-dep))
9024                        (not force-new))
9025                   ;; An article with this Message-ID has already
9026                   ;; been seen, so we ignore this one, except we add
9027                   ;; any additional Xrefs (in case the two articles
9028                   ;; came from different servers).
9029                   (progn
9030                     (mail-header-set-xref
9031                      (car (symbol-value id-dep))
9032                      (concat (or (mail-header-xref
9033                                   (car (symbol-value id-dep))) "")
9034                              (or (mail-header-xref header) "")))
9035                     (setq header nil))
9036                 (setcar (symbol-value id-dep) header))
9037             (set id-dep (list header)))
9038           (when header
9039             (if (boundp (setq ref-dep (intern ref dependencies)))
9040                 (setcdr (symbol-value ref-dep)
9041                         (nconc (cdr (symbol-value ref-dep))
9042                                (list (symbol-value id-dep))))
9043               (set ref-dep (list nil (symbol-value id-dep))))
9044             (setq headers (cons header headers)))
9045           (goto-char (point-max))
9046           (widen))
9047         (nreverse headers)))))
9048
9049 ;; The following macros and functions were written by Felix Lee
9050 ;; <flee@cse.psu.edu>.
9051
9052 (defmacro gnus-nov-read-integer ()
9053   '(prog1
9054        (if (= (following-char) ?\t)
9055            0
9056          (let ((num (condition-case nil (read buffer) (error nil))))
9057            (if (numberp num) num 0)))
9058      (or (eobp) (forward-char 1))))
9059
9060 (defmacro gnus-nov-skip-field ()
9061   '(search-forward "\t" eol 'move))
9062
9063 (defmacro gnus-nov-field ()
9064   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9065
9066 ;; Goes through the xover lines and returns a list of vectors
9067 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9068   "Parse the news overview data in the server buffer, and return a
9069 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9070   ;; Get the Xref when the users reads the articles since most/some
9071   ;; NNTP servers do not include Xrefs when using XOVER.
9072   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9073   (let ((cur nntp-server-buffer)
9074         (dependencies gnus-newsgroup-dependencies)
9075         number headers header)
9076     (save-excursion
9077       (set-buffer nntp-server-buffer)
9078       ;; Allow the user to mangle the headers before parsing them.
9079       (run-hooks 'gnus-parse-headers-hook)
9080       ;; Allow the user to mangle the headers before parsing them.
9081       (run-hooks 'gnus-parse-headers-hook)
9082       (goto-char (point-min))
9083       (while (and sequence (not (eobp)))
9084         (setq number (read cur))
9085         (while (and sequence (< (car sequence) number))
9086           (setq sequence (cdr sequence)))
9087         (and sequence
9088              (eq number (car sequence))
9089              (progn
9090                (setq sequence (cdr sequence))
9091                (if (setq header
9092                          (inline (gnus-nov-parse-line
9093                                   number dependencies force-new)))
9094                    (setq headers (cons header headers)))))
9095         (forward-line 1))
9096       (setq headers (nreverse headers)))
9097     headers))
9098
9099 ;; This function has to be called with point after the article number
9100 ;; on the beginning of the line.
9101 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9102   (let ((none 0)
9103         (eol (gnus-point-at-eol))
9104         (buffer (current-buffer))
9105         header ref id id-dep ref-dep)
9106
9107     ;; overview: [num subject from date id refs chars lines misc]
9108     (narrow-to-region (point) eol)
9109     (or (eobp) (forward-char))
9110
9111     (condition-case nil
9112         (setq header
9113               (vector
9114                number                   ; number
9115                (gnus-nov-field)         ; subject
9116                (gnus-nov-field)         ; from
9117                (gnus-nov-field)         ; date
9118                (setq id (or (gnus-nov-field)
9119                             (concat "none+"
9120                                     (int-to-string
9121                                      (setq none (1+ none)))))) ; id
9122                (progn
9123                  (save-excursion
9124                    (let ((beg (point)))
9125                      (search-forward "\t" eol)
9126                      (if (search-backward ">" beg t)
9127                          (setq ref
9128                                (downcase
9129                                 (buffer-substring
9130                                  (1+ (point))
9131                                  (progn
9132                                    (search-backward "<" beg t)
9133                                    (point)))))
9134                        (setq ref nil))))
9135                  (gnus-nov-field))      ; refs
9136                (gnus-nov-read-integer)  ; chars
9137                (gnus-nov-read-integer)  ; lines
9138                (if (= (following-char) ?\n)
9139                    nil
9140                  (gnus-nov-field))      ; misc
9141                ))
9142       (error (progn
9143                (ding)
9144                (gnus-message 4 "Strange nov line")
9145                (setq header nil)
9146                (goto-char eol))))
9147
9148     (widen)
9149
9150     ;; We build the thread tree.
9151     (when header
9152       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9153           (if (and (car (symbol-value id-dep))
9154                    (not force-new))
9155               ;; An article with this Message-ID has already been seen,
9156               ;; so we ignore this one, except we add any additional
9157               ;; Xrefs (in case the two articles came from different
9158               ;; servers.
9159               (progn
9160                 (mail-header-set-xref
9161                  (car (symbol-value id-dep))
9162                  (concat (or (mail-header-xref
9163                               (car (symbol-value id-dep))) "")
9164                          (or (mail-header-xref header) "")))
9165                 (setq header nil))
9166             (setcar (symbol-value id-dep) header))
9167         (set id-dep (list header))))
9168     (if header
9169         (progn
9170           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9171               (setcdr (symbol-value ref-dep)
9172                       (nconc (cdr (symbol-value ref-dep))
9173                              (list (symbol-value id-dep))))
9174             (set ref-dep (list nil (symbol-value id-dep))))))
9175     header))
9176
9177 (defun gnus-article-get-xrefs ()
9178   "Fill in the Xref value in `gnus-current-headers', if necessary.
9179 This is meant to be called in `gnus-article-internal-prepare-hook'."
9180   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9181                                  gnus-current-headers)))
9182     (or (not gnus-use-cross-reference)
9183         (not headers)
9184         (and (mail-header-xref headers)
9185              (not (string= (mail-header-xref headers) "")))
9186         (let ((case-fold-search t)
9187               xref)
9188           (save-restriction
9189             (nnheader-narrow-to-headers)
9190             (goto-char (point-min))
9191             (if (or (and (eq (downcase (following-char)) ?x)
9192                          (looking-at "Xref:"))
9193                     (search-forward "\nXref:" nil t))
9194                 (progn
9195                   (goto-char (1+ (match-end 0)))
9196                   (setq xref (buffer-substring (point)
9197                                                (progn (end-of-line) (point))))
9198                   (mail-header-set-xref headers xref))))))))
9199
9200 (defun gnus-summary-insert-subject (id)
9201   "Find article ID and insert the summary line for that article."
9202   (let ((header (gnus-read-header id))
9203         (number (and (numberp id) id)))
9204     (when header
9205       ;; Rebuild the thread that this article is part of and go to the
9206       ;; article we have fetched.
9207       (gnus-rebuild-thread (mail-header-id header))
9208       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9209     (when (and (numberp number)
9210                (> number 0))
9211       ;; We have to update the boundaries even if we can't fetch the
9212       ;; article if ID is a number -- so that the next `P' or `N'
9213       ;; command will fetch the previous (or next) article even
9214       ;; if the one we tried to fetch this time has been canceled.
9215       (and (> number gnus-newsgroup-end)
9216            (setq gnus-newsgroup-end number))
9217       (and (< number gnus-newsgroup-begin)
9218            (setq gnus-newsgroup-begin number))
9219       (setq gnus-newsgroup-unselected
9220             (delq number gnus-newsgroup-unselected)))
9221     ;; Report back a success?
9222     (and header number)))
9223
9224 (defun gnus-summary-work-articles (n)
9225   "Return a list of articles to be worked upon.  The prefix argument,
9226 the list of process marked articles, and the current article will be
9227 taken into consideration."
9228   (cond
9229    ((and n (numberp n))
9230     ;; A numerical prefix has been given.
9231     (let ((backward (< n 0))
9232           (n (abs n))
9233           articles article)
9234       (save-excursion
9235         (while
9236             (and (> n 0)
9237                  (push (setq article (gnus-summary-article-number))
9238                        articles)
9239                  (if backward
9240                      (gnus-summary-find-prev nil article)
9241                    (gnus-summary-find-next nil article)))
9242           (decf n)))
9243       (nreverse articles)))
9244    ((and (boundp 'transient-mark-mode)
9245          transient-mark-mode
9246          mark-active)
9247     ;; Work on the region between point and mark.
9248     (let ((max (max (point) (mark)))
9249           articles article)
9250       (save-excursion
9251         (goto-char (min (point) (mark)))
9252         (while
9253             (and
9254              (push (setq article (gnus-summary-article-number)) articles)
9255              (gnus-summary-find-next nil article)
9256              (< (point) max)))
9257         (nreverse articles))))
9258    (gnus-newsgroup-processable
9259     ;; There are process-marked articles present.
9260     (reverse gnus-newsgroup-processable))
9261    (t
9262     ;; Just return the current article.
9263     (list (gnus-summary-article-number)))))
9264
9265 (defun gnus-summary-search-group (&optional backward use-level)
9266   "Search for next unread newsgroup.
9267 If optional argument BACKWARD is non-nil, search backward instead."
9268   (save-excursion
9269     (set-buffer gnus-group-buffer)
9270     (if (gnus-group-search-forward
9271          backward nil (if use-level (gnus-group-group-level) nil))
9272         (gnus-group-group-name))))
9273
9274 (defun gnus-summary-best-group (&optional exclude-group)
9275   "Find the name of the best unread group.
9276 If EXCLUDE-GROUP, do not go to this group."
9277   (save-excursion
9278     (set-buffer gnus-group-buffer)
9279     (save-excursion
9280       (gnus-group-best-unread-group exclude-group))))
9281
9282 (defun gnus-summary-find-next (&optional unread article backward)
9283   (if backward (gnus-summary-find-prev)
9284     (let* ((article (or article (gnus-summary-article-number)))
9285            (arts (gnus-data-find-list article))
9286            result)
9287       (when (or (not gnus-summary-check-current)
9288                 (not unread)
9289                 (not (gnus-data-unread-p (car arts))))
9290         (setq arts (cdr arts)))
9291       (when (setq result
9292                   (if unread
9293                       (progn
9294                         (while arts
9295                           (when (gnus-data-unread-p (car arts))
9296                             (setq result (car arts)
9297                                   arts nil))
9298                           (setq arts (cdr arts)))
9299                         result)
9300                     (car arts)))
9301         (goto-char (gnus-data-pos result))
9302         (gnus-data-number result)))))
9303
9304 (defun gnus-summary-find-prev (&optional unread article)
9305   (let* ((article (or article (gnus-summary-article-number)))
9306          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9307          result)
9308     (when (or (not gnus-summary-check-current)
9309               (not unread)
9310               (not (gnus-data-unread-p (car arts))))
9311       (setq arts (cdr arts)))
9312     (if (setq result
9313               (if unread
9314                   (progn
9315                     (while arts
9316                       (and (gnus-data-unread-p (car arts))
9317                            (setq result (car arts)
9318                                  arts nil))
9319                       (setq arts (cdr arts)))
9320                     result)
9321                 (car arts)))
9322         (progn
9323           (goto-char (gnus-data-pos result))
9324           (gnus-data-number result)))))
9325
9326 (defun gnus-summary-find-subject (subject &optional unread backward article)
9327   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9328          (article (or article (gnus-summary-article-number)))
9329          (articles (gnus-data-list backward))
9330          (arts (gnus-data-find-list article articles))
9331          result)
9332     (when (or (not gnus-summary-check-current)
9333               (not unread)
9334               (not (gnus-data-unread-p (car arts))))
9335       (setq arts (cdr arts)))
9336     (while arts
9337       (and (or (not unread)
9338                (gnus-data-unread-p (car arts)))
9339            (vectorp (gnus-data-header (car arts)))
9340            (gnus-subject-equal
9341             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9342            (setq result (car arts)
9343                  arts nil))
9344       (setq arts (cdr arts)))
9345     (and result
9346          (goto-char (gnus-data-pos result))
9347          (gnus-data-number result))))
9348
9349 (defun gnus-summary-search-forward (&optional unread subject backward)
9350   "Search forward for an article.
9351 If UNREAD, look for unread articles.  If SUBJECT, look for
9352 articles with that subject.  If BACKWARD, search backward instead."
9353   (cond (subject (gnus-summary-find-subject subject unread backward))
9354         (backward (gnus-summary-find-prev unread))
9355         (t (gnus-summary-find-next unread))))
9356
9357 (defun gnus-recenter (&optional n)
9358   "Center point in window and redisplay frame.
9359 Also do horizontal recentering."
9360   (interactive "P")
9361   (when (and gnus-auto-center-summary
9362              (not (eq gnus-auto-center-summary 'vertical)))
9363     (gnus-horizontal-recenter))
9364   (recenter n))
9365
9366 (defun gnus-summary-recenter ()
9367   "Center point in the summary window.
9368 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9369 displayed, no centering will be performed."
9370   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9371   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9372   (let* ((top (cond ((< (window-height) 4) 0)
9373                     ((< (window-height) 7) 1)
9374                     (t 2)))
9375          (height (1- (window-height)))
9376          (bottom (save-excursion (goto-char (point-max))
9377                                  (forward-line (- height))
9378                                  (point)))
9379          (window (get-buffer-window (current-buffer))))
9380     ;; The user has to want it.
9381     (when gnus-auto-center-summary
9382       (when (get-buffer-window gnus-article-buffer)
9383        ;; Only do recentering when the article buffer is displayed,
9384        ;; Set the window start to either `bottom', which is the biggest
9385        ;; possible valid number, or the second line from the top,
9386        ;; whichever is the least.
9387        (set-window-start
9388         window (min bottom (save-excursion 
9389                              (forward-line (- top)) (point)))))
9390       ;; Do horizontal recentering while we're at it.
9391       (when (and (get-buffer-window (current-buffer) t)
9392                  (not (eq gnus-auto-center-summary 'vertical)))
9393         (let ((selected (selected-window)))
9394           (select-window (get-buffer-window (current-buffer) t))
9395           (gnus-summary-position-point)
9396           (gnus-horizontal-recenter)
9397           (select-window selected))))))
9398
9399 (defun gnus-horizontal-recenter ()
9400   "Recenter the current buffer horizontally."
9401   (if (< (current-column) (/ (window-width) 2))
9402       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9403     (let* ((orig (point))
9404            (end (window-end (get-buffer-window (current-buffer) t)))
9405            (max 0))
9406       ;; Find the longest line currently displayed in the window.
9407       (goto-char (window-start))
9408       (while (and (not (eobp)) 
9409                   (< (point) end))
9410         (end-of-line)
9411         (setq max (max max (current-column)))
9412         (forward-line 1))
9413       (goto-char orig)
9414       ;; Scroll horizontally to center (sort of) the point.
9415       (if (> max (window-width))
9416           (set-window-hscroll 
9417            (get-buffer-window (current-buffer) t)
9418            (min (- (current-column) (/ (window-width) 3))
9419                 (+ 2 (- max (window-width)))))
9420         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9421       max)))
9422
9423 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9424 (defun gnus-short-group-name (group &optional levels)
9425   "Collapse GROUP name LEVELS."
9426   (let* ((name "") 
9427          (foreign "")
9428          (depth 0) 
9429          (skip 1)
9430          (levels (or levels
9431                      (progn
9432                        (while (string-match "\\." group skip)
9433                          (setq skip (match-end 0)
9434                                depth (+ depth 1)))
9435                        depth))))
9436     (if (string-match ":" group)
9437         (setq foreign (substring group 0 (match-end 0))
9438               group (substring group (match-end 0))))
9439     (while group
9440       (if (and (string-match "\\." group)
9441                (> levels (- gnus-group-uncollapsed-levels 1)))
9442           (setq name (concat name (substring group 0 1))
9443                 group (substring group (match-end 0))
9444                 levels (- levels 1)
9445                 name (concat name "."))
9446         (setq name (concat foreign name group)
9447               group nil)))
9448     name))
9449
9450 (defun gnus-summary-jump-to-group (newsgroup)
9451   "Move point to NEWSGROUP in group mode buffer."
9452   ;; Keep update point of group mode buffer if visible.
9453   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9454       (save-window-excursion
9455         ;; Take care of tree window mode.
9456         (if (get-buffer-window gnus-group-buffer)
9457             (pop-to-buffer gnus-group-buffer))
9458         (gnus-group-jump-to-group newsgroup))
9459     (save-excursion
9460       ;; Take care of tree window mode.
9461       (if (get-buffer-window gnus-group-buffer)
9462           (pop-to-buffer gnus-group-buffer)
9463         (set-buffer gnus-group-buffer))
9464       (gnus-group-jump-to-group newsgroup))))
9465
9466 ;; This function returns a list of article numbers based on the
9467 ;; difference between the ranges of read articles in this group and
9468 ;; the range of active articles.
9469 (defun gnus-list-of-unread-articles (group)
9470   (let* ((read (gnus-info-read (gnus-get-info group)))
9471          (active (gnus-active group))
9472          (last (cdr active))
9473          first nlast unread)
9474     ;; If none are read, then all are unread.
9475     (if (not read)
9476         (setq first (car active))
9477       ;; If the range of read articles is a single range, then the
9478       ;; first unread article is the article after the last read
9479       ;; article.  Sounds logical, doesn't it?
9480       (if (not (listp (cdr read)))
9481           (setq first (1+ (cdr read)))
9482         ;; `read' is a list of ranges.
9483         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9484                                 (caar read))) 1)
9485             (setq first 1))
9486         (while read
9487           (if first
9488               (while (< first nlast)
9489                 (setq unread (cons first unread))
9490                 (setq first (1+ first))))
9491           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9492           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9493           (setq read (cdr read)))))
9494     ;; And add the last unread articles.
9495     (while (<= first last)
9496       (setq unread (cons first unread))
9497       (setq first (1+ first)))
9498     ;; Return the list of unread articles.
9499     (nreverse unread)))
9500
9501 (defun gnus-list-of-read-articles (group)
9502   "Return a list of unread, unticked and non-dormant articles."
9503   (let* ((info (gnus-get-info group))
9504          (marked (gnus-info-marks info))
9505          (active (gnus-active group)))
9506     (and info active
9507          (gnus-set-difference
9508           (gnus-sorted-complement
9509            (gnus-uncompress-range active)
9510            (gnus-list-of-unread-articles group))
9511           (append
9512            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9513            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9514
9515 ;; Various summary commands
9516
9517 (defun gnus-summary-universal-argument (arg)
9518   "Perform any operation on all articles that are process/prefixed."
9519   (interactive "P")
9520   (gnus-set-global-variables)
9521   (let ((articles (gnus-summary-work-articles arg))
9522         func article)
9523     (if (eq
9524          (setq
9525           func
9526           (key-binding
9527            (read-key-sequence
9528             (substitute-command-keys
9529              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9530              ))))
9531          'undefined)
9532         (progn
9533           (message "Undefined key")
9534           (ding))
9535       (save-excursion
9536         (while articles
9537           (gnus-summary-goto-subject (setq article (pop articles)))
9538           (command-execute func)
9539           (gnus-summary-remove-process-mark article)))))
9540   (gnus-summary-position-point))
9541
9542 (defun gnus-summary-toggle-truncation (&optional arg)
9543   "Toggle truncation of summary lines.
9544 With arg, turn line truncation on iff arg is positive."
9545   (interactive "P")
9546   (setq truncate-lines
9547         (if (null arg) (not truncate-lines)
9548           (> (prefix-numeric-value arg) 0)))
9549   (redraw-display))
9550
9551 (defun gnus-summary-reselect-current-group (&optional all rescan)
9552   "Exit and then reselect the current newsgroup.
9553 The prefix argument ALL means to select all articles."
9554   (interactive "P")
9555   (gnus-set-global-variables)
9556   (let ((current-subject (gnus-summary-article-number))
9557         (group gnus-newsgroup-name))
9558     (setq gnus-newsgroup-begin nil)
9559     (gnus-summary-exit)
9560     ;; We have to adjust the point of group mode buffer because the
9561     ;; current point was moved to the next unread newsgroup by
9562     ;; exiting.
9563     (gnus-summary-jump-to-group group)
9564     (when rescan
9565       (save-excursion
9566         (gnus-group-get-new-news-this-group 1)))
9567     (gnus-group-read-group all t)
9568     (gnus-summary-goto-subject current-subject)))
9569
9570 (defun gnus-summary-rescan-group (&optional all)
9571   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9572   (interactive "P")
9573   (gnus-summary-reselect-current-group all t))
9574
9575 (defun gnus-summary-update-info ()
9576   (let* ((group gnus-newsgroup-name))
9577     (when gnus-newsgroup-kill-headers
9578       (setq gnus-newsgroup-killed
9579             (gnus-compress-sequence
9580              (nconc
9581               (gnus-set-sorted-intersection
9582                (gnus-uncompress-range gnus-newsgroup-killed)
9583                (setq gnus-newsgroup-unselected
9584                      (sort gnus-newsgroup-unselected '<)))
9585               (setq gnus-newsgroup-unreads
9586                     (sort gnus-newsgroup-unreads '<))) t)))
9587     (unless (listp (cdr gnus-newsgroup-killed))
9588       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9589     (let ((headers gnus-newsgroup-headers))
9590       (run-hooks 'gnus-exit-group-hook)
9591       (unless gnus-save-score
9592         (setq gnus-newsgroup-scored nil))
9593       ;; Set the new ranges of read articles.
9594       (gnus-update-read-articles
9595        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9596       ;; Set the current article marks.
9597       (gnus-update-marks)
9598       ;; Do the cross-ref thing.
9599       (when gnus-use-cross-reference
9600         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9601       ;; Do adaptive scoring, and possibly save score files.
9602       (when gnus-newsgroup-adaptive
9603         (gnus-score-adaptive))
9604       (when gnus-use-scoring
9605         (gnus-score-save))
9606       ;; Do not switch windows but change the buffer to work.
9607       (set-buffer gnus-group-buffer)
9608       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9609           (gnus-group-update-group group)))))
9610
9611 (defun gnus-summary-exit (&optional temporary)
9612   "Exit reading current newsgroup, and then return to group selection mode.
9613 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9614   (interactive)
9615   (gnus-set-global-variables)
9616   (gnus-kill-save-kill-buffer)
9617   (let* ((group gnus-newsgroup-name)
9618          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9619          (mode major-mode)
9620          (buf (current-buffer)))
9621     (run-hooks 'gnus-summary-prepare-exit-hook)
9622     ;; If we have several article buffers, we kill them at exit.
9623     (unless gnus-single-article-buffer
9624       (gnus-kill-buffer gnus-article-buffer)
9625       (gnus-kill-buffer gnus-original-article-buffer))
9626     (when gnus-use-cache
9627       (gnus-cache-possibly-remove-articles)
9628       (gnus-cache-save-buffers))
9629     (when gnus-use-trees
9630       (gnus-tree-close group))
9631     ;; Make all changes in this group permanent.
9632     (unless quit-config
9633       (gnus-summary-update-info))
9634     (gnus-close-group group)
9635     ;; Make sure where I was, and go to next newsgroup.
9636     (set-buffer gnus-group-buffer)
9637     (unless quit-config
9638       (gnus-group-jump-to-group group)
9639       (gnus-group-next-unread-group 1))
9640     (run-hooks 'gnus-summary-exit-hook)
9641     (if temporary
9642         nil                             ;Nothing to do.
9643       ;; If we have several article buffers, we kill them at exit.
9644       (unless gnus-single-article-buffer
9645         (gnus-kill-buffer gnus-article-buffer)
9646         (gnus-kill-buffer gnus-original-article-buffer))
9647       (set-buffer buf)
9648       (if (not gnus-kill-summary-on-exit)
9649           (gnus-deaden-summary)
9650         ;; We set all buffer-local variables to nil.  It is unclear why
9651         ;; this is needed, but if we don't, buffer-local variables are
9652         ;; not garbage-collected, it seems.  This would the lead to en
9653         ;; ever-growing Emacs.
9654         (gnus-summary-clear-local-variables)
9655         (when (get-buffer gnus-article-buffer)
9656           (bury-buffer gnus-article-buffer))
9657         ;; We clear the global counterparts of the buffer-local
9658         ;; variables as well, just to be on the safe side.
9659         (gnus-configure-windows 'group 'force)
9660         (gnus-summary-clear-local-variables)
9661         ;; Return to group mode buffer.
9662         (if (eq mode 'gnus-summary-mode)
9663             (gnus-kill-buffer buf)))
9664       (setq gnus-current-select-method gnus-select-method)
9665       (pop-to-buffer gnus-group-buffer)
9666       ;; Clear the current group name.
9667       (if (not quit-config)
9668           (progn
9669             (gnus-group-jump-to-group group)
9670             (gnus-group-next-unread-group 1)
9671             (gnus-configure-windows 'group 'force))
9672         (if (not (buffer-name (car quit-config)))
9673             (gnus-configure-windows 'group 'force)
9674           (set-buffer (car quit-config))
9675           (and (eq major-mode 'gnus-summary-mode)
9676                (gnus-set-global-variables))
9677           (gnus-configure-windows (cdr quit-config))))
9678       (unless quit-config
9679         (setq gnus-newsgroup-name nil)))))
9680
9681 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9682 (defun gnus-summary-exit-no-update (&optional no-questions)
9683   "Quit reading current newsgroup without updating read article info."
9684   (interactive)
9685   (gnus-set-global-variables)
9686   (let* ((group gnus-newsgroup-name)
9687          (quit-config (gnus-group-quit-config group)))
9688     (when (or no-questions
9689               gnus-expert-user
9690               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9691       ;; If we have several article buffers, we kill them at exit.
9692       (unless gnus-single-article-buffer
9693         (gnus-kill-buffer gnus-article-buffer)
9694         (gnus-kill-buffer gnus-original-article-buffer))
9695       (if (not gnus-kill-summary-on-exit)
9696           (gnus-deaden-summary)
9697         (gnus-close-group group)
9698         (gnus-summary-clear-local-variables)
9699         (set-buffer gnus-group-buffer)
9700         (gnus-summary-clear-local-variables)
9701         (when (get-buffer gnus-summary-buffer)
9702           (kill-buffer gnus-summary-buffer)))
9703       (when gnus-use-trees
9704         (gnus-tree-close group))
9705       (when (get-buffer gnus-article-buffer)
9706         (bury-buffer gnus-article-buffer))
9707       ;; Return to the group buffer.
9708       (gnus-configure-windows 'group 'force)
9709       ;; Clear the current group name.
9710       (setq gnus-newsgroup-name nil)
9711       (when (equal (gnus-group-group-name) group)
9712         (gnus-group-next-unread-group 1))
9713       (when quit-config
9714         (if (not (buffer-name (car quit-config)))
9715             (gnus-configure-windows 'group 'force)
9716           (set-buffer (car quit-config))
9717           (when (eq major-mode 'gnus-summary-mode)
9718             (gnus-set-global-variables))
9719           (gnus-configure-windows (cdr quit-config)))))))
9720
9721 ;;; Dead summaries.
9722
9723 (defvar gnus-dead-summary-mode-map nil)
9724
9725 (if gnus-dead-summary-mode-map
9726     nil
9727   (setq gnus-dead-summary-mode-map (make-keymap))
9728   (suppress-keymap gnus-dead-summary-mode-map)
9729   (substitute-key-definition
9730    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9731   (let ((keys '("\C-d" "\r" "\177")))
9732     (while keys
9733       (define-key gnus-dead-summary-mode-map
9734         (pop keys) 'gnus-summary-wake-up-the-dead))))
9735
9736 (defvar gnus-dead-summary-mode nil
9737   "Minor mode for Gnus summary buffers.")
9738
9739 (defun gnus-dead-summary-mode (&optional arg)
9740   "Minor mode for Gnus summary buffers."
9741   (interactive "P")
9742   (when (eq major-mode 'gnus-summary-mode)
9743     (make-local-variable 'gnus-dead-summary-mode)
9744     (setq gnus-dead-summary-mode
9745           (if (null arg) (not gnus-dead-summary-mode)
9746             (> (prefix-numeric-value arg) 0)))
9747     (when gnus-dead-summary-mode
9748       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9749         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9750       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9751         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9752               minor-mode-map-alist)))))
9753
9754 (defun gnus-deaden-summary ()
9755   "Make the current summary buffer into a dead summary buffer."
9756   ;; Kill any previous dead summary buffer.
9757   (when (and gnus-dead-summary
9758              (buffer-name gnus-dead-summary))
9759     (save-excursion
9760       (set-buffer gnus-dead-summary)
9761       (when gnus-dead-summary-mode
9762         (kill-buffer (current-buffer)))))
9763   ;; Make this the current dead summary.
9764   (setq gnus-dead-summary (current-buffer))
9765   (gnus-dead-summary-mode 1)
9766   (let ((name (buffer-name)))
9767     (when (string-match "Summary" name)
9768       (rename-buffer
9769        (concat (substring name 0 (match-beginning 0)) "Dead "
9770                (substring name (match-beginning 0))) t))))
9771
9772 (defun gnus-kill-or-deaden-summary (buffer)
9773   "Kill or deaden the summary BUFFER."
9774   (cond (gnus-kill-summary-on-exit
9775          (when (and gnus-use-trees
9776                     (and (get-buffer buffer)
9777                          (buffer-name (get-buffer buffer))))
9778            (save-excursion
9779              (set-buffer (get-buffer buffer))
9780              (gnus-tree-close gnus-newsgroup-name)))
9781          (gnus-kill-buffer buffer))
9782         ((and (get-buffer buffer)
9783               (buffer-name (get-buffer buffer)))
9784          (save-excursion
9785            (set-buffer buffer)
9786            (gnus-deaden-summary)))))
9787
9788 (defun gnus-summary-wake-up-the-dead (&rest args)
9789   "Wake up the dead summary buffer."
9790   (interactive)
9791   (gnus-dead-summary-mode -1)
9792   (let ((name (buffer-name)))
9793     (when (string-match "Dead " name)
9794       (rename-buffer
9795        (concat (substring name 0 (match-beginning 0))
9796                (substring name (match-end 0))) t)))
9797   (gnus-message 3 "This dead summary is now alive again"))
9798
9799 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9800 (defun gnus-summary-fetch-faq (&optional faq-dir)
9801   "Fetch the FAQ for the current group.
9802 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9803 in."
9804   (interactive
9805    (list
9806     (if current-prefix-arg
9807         (completing-read
9808          "Faq dir: " (and (listp gnus-group-faq-directory)
9809                           gnus-group-faq-directory)))))
9810   (let (gnus-faq-buffer)
9811     (and (setq gnus-faq-buffer
9812                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9813          (gnus-configure-windows 'summary-faq))))
9814
9815 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9816 (defun gnus-summary-describe-group (&optional force)
9817   "Describe the current newsgroup."
9818   (interactive "P")
9819   (gnus-group-describe-group force gnus-newsgroup-name))
9820
9821 (defun gnus-summary-describe-briefly ()
9822   "Describe summary mode commands briefly."
9823   (interactive)
9824   (gnus-message 6
9825                 (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")))
9826
9827 ;; Walking around group mode buffer from summary mode.
9828
9829 (defun gnus-summary-next-group (&optional no-article target-group backward)
9830   "Exit current newsgroup and then select next unread newsgroup.
9831 If prefix argument NO-ARTICLE is non-nil, no article is selected
9832 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9833 previous group instead."
9834   (interactive "P")
9835   (gnus-set-global-variables)
9836   (let ((current-group gnus-newsgroup-name)
9837         (current-buffer (current-buffer))
9838         entered)
9839     ;; First we semi-exit this group to update Xrefs and all variables.
9840     ;; We can't do a real exit, because the window conf must remain
9841     ;; the same in case the user is prompted for info, and we don't
9842     ;; want the window conf to change before that...
9843     (gnus-summary-exit t)
9844     (while (not entered)
9845       ;; Then we find what group we are supposed to enter.
9846       (set-buffer gnus-group-buffer)
9847       (gnus-group-jump-to-group current-group)
9848       (setq target-group
9849             (or target-group
9850                 (if (eq gnus-keep-same-level 'best)
9851                     (gnus-summary-best-group gnus-newsgroup-name)
9852                   (gnus-summary-search-group backward gnus-keep-same-level))))
9853       (if (not target-group)
9854           ;; There are no further groups, so we return to the group
9855           ;; buffer.
9856           (progn
9857             (gnus-message 5 "Returning to the group buffer")
9858             (setq entered t)
9859             (set-buffer current-buffer)
9860             (gnus-summary-exit))
9861         ;; We try to enter the target group.
9862         (gnus-group-jump-to-group target-group)
9863         (let ((unreads (gnus-group-group-unread)))
9864           (if (and (or (eq t unreads)
9865                        (and unreads (not (zerop unreads))))
9866                    (gnus-summary-read-group
9867                     target-group nil no-article current-buffer))
9868               (setq entered t)
9869             (setq current-group target-group
9870                   target-group nil)))))))
9871
9872 (defun gnus-summary-prev-group (&optional no-article)
9873   "Exit current newsgroup and then select previous unread newsgroup.
9874 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9875   (interactive "P")
9876   (gnus-summary-next-group no-article nil t))
9877
9878 ;; Walking around summary lines.
9879
9880 (defun gnus-summary-first-subject (&optional unread)
9881   "Go to the first unread subject.
9882 If UNREAD is non-nil, go to the first unread article.
9883 Returns the article selected or nil if there are no unread articles."
9884   (interactive "P")
9885   (prog1
9886       (cond
9887        ;; Empty summary.
9888        ((null gnus-newsgroup-data)
9889         (gnus-message 3 "No articles in the group")
9890         nil)
9891        ;; Pick the first article.
9892        ((not unread)
9893         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9894         (gnus-data-number (car gnus-newsgroup-data)))
9895        ;; No unread articles.
9896        ((null gnus-newsgroup-unreads)
9897         (gnus-message 3 "No more unread articles")
9898         nil)
9899        ;; Find the first unread article.
9900        (t
9901         (let ((data gnus-newsgroup-data))
9902           (while (and data
9903                       (not (gnus-data-unread-p (car data))))
9904             (setq data (cdr data)))
9905           (if data
9906               (progn
9907                 (goto-char (gnus-data-pos (car data)))
9908                 (gnus-data-number (car data)))))))
9909     (gnus-summary-position-point)))
9910
9911 (defun gnus-summary-next-subject (n &optional unread dont-display)
9912   "Go to next N'th summary line.
9913 If N is negative, go to the previous N'th subject line.
9914 If UNREAD is non-nil, only unread articles are selected.
9915 The difference between N and the actual number of steps taken is
9916 returned."
9917   (interactive "p")
9918   (let ((backward (< n 0))
9919         (n (abs n)))
9920     (while (and (> n 0)
9921                 (if backward
9922                     (gnus-summary-find-prev unread)
9923                   (gnus-summary-find-next unread)))
9924       (setq n (1- n)))
9925     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9926                                (if unread " unread" "")))
9927     (unless dont-display
9928       (gnus-summary-recenter)
9929       (gnus-summary-position-point))
9930     n))
9931
9932 (defun gnus-summary-next-unread-subject (n)
9933   "Go to next N'th unread summary line."
9934   (interactive "p")
9935   (gnus-summary-next-subject n t))
9936
9937 (defun gnus-summary-prev-subject (n &optional unread)
9938   "Go to previous N'th summary line.
9939 If optional argument UNREAD is non-nil, only unread article is selected."
9940   (interactive "p")
9941   (gnus-summary-next-subject (- n) unread))
9942
9943 (defun gnus-summary-prev-unread-subject (n)
9944   "Go to previous N'th unread summary line."
9945   (interactive "p")
9946   (gnus-summary-next-subject (- n) t))
9947
9948 (defun gnus-summary-goto-subject (article &optional force silent)
9949   "Go the subject line of ARTICLE.
9950 If FORCE, also allow jumping to articles not currently shown."
9951   (let ((b (point))
9952         (data (gnus-data-find article)))
9953     ;; We read in the article if we have to.
9954     (and (not data)
9955          force
9956          (gnus-summary-insert-subject article)
9957          (setq data (gnus-data-find article)))
9958     (goto-char b)
9959     (if (not data)
9960         (progn
9961           (unless silent
9962             (gnus-message 3 "Can't find article %d" article))
9963           nil)
9964       (goto-char (gnus-data-pos data))
9965       article)))
9966
9967 ;; Walking around summary lines with displaying articles.
9968
9969 (defun gnus-summary-expand-window (&optional arg)
9970   "Make the summary buffer take up the entire Emacs frame.
9971 Given a prefix, will force an `article' buffer configuration."
9972   (interactive "P")
9973   (gnus-set-global-variables)
9974   (if arg
9975       (gnus-configure-windows 'article 'force)
9976     (gnus-configure-windows 'summary 'force)))
9977
9978 (defun gnus-summary-display-article (article &optional all-header)
9979   "Display ARTICLE in article buffer."
9980   (gnus-set-global-variables)
9981   (if (null article)
9982       nil
9983     (prog1
9984         (if gnus-summary-display-article-function
9985             (funcall gnus-summary-display-article-function article all-header)
9986           (gnus-article-prepare article all-header))
9987       (run-hooks 'gnus-select-article-hook)
9988       (gnus-summary-recenter)
9989       (gnus-summary-goto-subject article)
9990       (when gnus-use-trees
9991         (gnus-possibly-generate-tree article)
9992         (gnus-highlight-selected-tree article))
9993       ;; Successfully display article.
9994       (gnus-article-set-window-start
9995        (cdr (assq article gnus-newsgroup-bookmarks)))
9996       t)))
9997
9998 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9999   "Select the current article.
10000 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10001 non-nil, the article will be re-fetched even if it already present in
10002 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10003 be displayed."
10004   (let ((article (or article (gnus-summary-article-number)))
10005         (all-headers (not (not all-headers))) ;Must be T or NIL.
10006         gnus-summary-display-article-function
10007         did)
10008     (and (not pseudo)
10009          (gnus-summary-article-pseudo-p article)
10010          (error "This is a pseudo-article."))
10011     (prog1
10012         (save-excursion
10013           (set-buffer gnus-summary-buffer)
10014           (if (or (and gnus-single-article-buffer
10015                        (or (null gnus-current-article)
10016                            (null gnus-article-current)
10017                            (null (get-buffer gnus-article-buffer))
10018                            (not (eq article (cdr gnus-article-current)))
10019                            (not (equal (car gnus-article-current)
10020                                        gnus-newsgroup-name))))
10021                   (and (not gnus-single-article-buffer)
10022                        (null gnus-current-article))
10023                   force)
10024               ;; The requested article is different from the current article.
10025               (prog1
10026                   (gnus-summary-display-article article all-headers)
10027                 (setq did article))
10028             (if (or all-headers gnus-show-all-headers)
10029                 (gnus-article-show-all-headers))
10030             'old))
10031       (if did
10032           (gnus-article-set-window-start
10033            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10034
10035 (defun gnus-summary-set-current-mark (&optional current-mark)
10036   "Obsolete function."
10037   nil)
10038
10039 (defun gnus-summary-next-article (&optional unread subject backward push)
10040   "Select the next article.
10041 If UNREAD, only unread articles are selected.
10042 If SUBJECT, only articles with SUBJECT are selected.
10043 If BACKWARD, the previous article is selected instead of the next."
10044   (interactive "P")
10045   (gnus-set-global-variables)
10046   (cond
10047    ;; Is there such an article?
10048    ((and (gnus-summary-search-forward unread subject backward)
10049          (or (gnus-summary-display-article (gnus-summary-article-number))
10050              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10051     (gnus-summary-position-point))
10052    ;; If not, we try the first unread, if that is wanted.
10053    ((and subject
10054          gnus-auto-select-same
10055          (or (gnus-summary-first-unread-article)
10056              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10057     (gnus-summary-position-point)
10058     (gnus-message 6 "Wrapped"))
10059    ;; Try to get next/previous article not displayed in this group.
10060    ((and gnus-auto-extend-newsgroup
10061          (not unread) (not subject))
10062     (gnus-summary-goto-article
10063      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10064      nil t))
10065    ;; Go to next/previous group.
10066    (t
10067     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10068         (gnus-summary-jump-to-group gnus-newsgroup-name))
10069     (let ((cmd last-command-char)
10070           (group
10071            (if (eq gnus-keep-same-level 'best)
10072                (gnus-summary-best-group gnus-newsgroup-name)
10073              (gnus-summary-search-group backward gnus-keep-same-level))))
10074       ;; For some reason, the group window gets selected.  We change
10075       ;; it back.
10076       (select-window (get-buffer-window (current-buffer)))
10077       ;; Select next unread newsgroup automagically.
10078       (cond
10079        ((not gnus-auto-select-next)
10080         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10081        ((or (eq gnus-auto-select-next 'quietly)
10082             (and (eq gnus-auto-select-next 'slightly-quietly)
10083                  push)
10084             (and (eq gnus-auto-select-next 'almost-quietly)
10085                  (gnus-summary-last-article-p)))
10086         ;; Select quietly.
10087         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10088             (gnus-summary-exit)
10089           (gnus-message 7 "No more%s articles (%s)..."
10090                         (if unread " unread" "")
10091                         (if group (concat "selecting " group)
10092                           "exiting"))
10093           (gnus-summary-next-group nil group backward)))
10094        (t
10095         (gnus-summary-walk-group-buffer
10096          gnus-newsgroup-name cmd unread backward)))))))
10097
10098 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10099   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10100                       (?\C-p (gnus-group-prev-unread-group 1))))
10101         keve key group ended)
10102     (save-excursion
10103       (set-buffer gnus-group-buffer)
10104       (gnus-summary-jump-to-group from-group)
10105       (setq group
10106             (if (eq gnus-keep-same-level 'best)
10107                 (gnus-summary-best-group gnus-newsgroup-name)
10108               (gnus-summary-search-group backward gnus-keep-same-level))))
10109     (while (not ended)
10110       (gnus-message
10111        5 "No more%s articles%s" (if unread " unread" "")
10112        (if (and group
10113                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10114            (format " (Type %s for %s [%s])"
10115                    (single-key-description cmd) group
10116                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10117          (format " (Type %s to exit %s)"
10118                  (single-key-description cmd)
10119                  gnus-newsgroup-name)))
10120       ;; Confirm auto selection.
10121       (setq key (car (setq keve (gnus-read-event-char))))
10122       (setq ended t)
10123       (cond
10124        ((assq key keystrokes)
10125         (let ((obuf (current-buffer)))
10126           (switch-to-buffer gnus-group-buffer)
10127           (and group
10128                (gnus-group-jump-to-group group))
10129           (eval (cadr (assq key keystrokes)))
10130           (setq group (gnus-group-group-name))
10131           (switch-to-buffer obuf))
10132         (setq ended nil))
10133        ((equal key cmd)
10134         (if (or (not group)
10135                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10136             (gnus-summary-exit)
10137           (gnus-summary-next-group nil group backward)))
10138        (t
10139         (push (cdr keve) unread-command-events))))))
10140
10141 (defun gnus-read-event-char ()
10142   "Get the next event."
10143   (let ((event (read-event)))
10144     (cons (and (numberp event) event) event)))
10145
10146 (defun gnus-summary-next-unread-article ()
10147   "Select unread article after current one."
10148   (interactive)
10149   (gnus-summary-next-article t (and gnus-auto-select-same
10150                                     (gnus-summary-article-subject))))
10151
10152 (defun gnus-summary-prev-article (&optional unread subject)
10153   "Select the article after the current one.
10154 If UNREAD is non-nil, only unread articles are selected."
10155   (interactive "P")
10156   (gnus-summary-next-article unread subject t))
10157
10158 (defun gnus-summary-prev-unread-article ()
10159   "Select unred article before current one."
10160   (interactive)
10161   (gnus-summary-prev-article t (and gnus-auto-select-same
10162                                     (gnus-summary-article-subject))))
10163
10164 (defun gnus-summary-next-page (&optional lines circular)
10165   "Show next page of the selected article.
10166 If at the end of the current article, select the next article.
10167 LINES says how many lines should be scrolled up.
10168
10169 If CIRCULAR is non-nil, go to the start of the article instead of
10170 selecting the next article when reaching the end of the current
10171 article."
10172   (interactive "P")
10173   (setq gnus-summary-buffer (current-buffer))
10174   (gnus-set-global-variables)
10175   (let ((article (gnus-summary-article-number))
10176         (endp nil))
10177     (gnus-configure-windows 'article)
10178     (if (or (null gnus-current-article)
10179             (null gnus-article-current)
10180             (/= article (cdr gnus-article-current))
10181             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10182         ;; Selected subject is different from current article's.
10183         (gnus-summary-display-article article)
10184       (gnus-eval-in-buffer-window
10185        gnus-article-buffer
10186        (setq endp (gnus-article-next-page lines)))
10187       (if endp
10188           (cond (circular
10189                  (gnus-summary-beginning-of-article))
10190                 (lines
10191                  (gnus-message 3 "End of message"))
10192                 ((null lines)
10193                  (if (and (eq gnus-summary-goto-unread 'never)
10194                           (not (eq article gnus-newsgroup-end)))
10195                      (gnus-summary-next-article)
10196                    (gnus-summary-next-unread-article))))))
10197     (gnus-summary-recenter)
10198     (gnus-summary-position-point)))
10199
10200 (defun gnus-summary-prev-page (&optional lines)
10201   "Show previous page of selected article.
10202 Argument LINES specifies lines to be scrolled down."
10203   (interactive "P")
10204   (gnus-set-global-variables)
10205   (let ((article (gnus-summary-article-number)))
10206     (gnus-configure-windows 'article)
10207     (if (or (null gnus-current-article)
10208             (null gnus-article-current)
10209             (/= article (cdr gnus-article-current))
10210             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10211         ;; Selected subject is different from current article's.
10212         (gnus-summary-display-article article)
10213       (gnus-summary-recenter)
10214       (gnus-eval-in-buffer-window gnus-article-buffer
10215                                   (gnus-article-prev-page lines))))
10216   (gnus-summary-position-point))
10217
10218 (defun gnus-summary-scroll-up (lines)
10219   "Scroll up (or down) one line current article.
10220 Argument LINES specifies lines to be scrolled up (or down if negative)."
10221   (interactive "p")
10222   (gnus-set-global-variables)
10223   (gnus-configure-windows 'article)
10224   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10225     (gnus-eval-in-buffer-window
10226      gnus-article-buffer
10227      (cond ((> lines 0)
10228             (if (gnus-article-next-page lines)
10229                 (gnus-message 3 "End of message")))
10230            ((< lines 0)
10231             (gnus-article-prev-page (- lines))))))
10232   (gnus-summary-recenter)
10233   (gnus-summary-position-point))
10234
10235 (defun gnus-summary-next-same-subject ()
10236   "Select next article which has the same subject as current one."
10237   (interactive)
10238   (gnus-set-global-variables)
10239   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10240
10241 (defun gnus-summary-prev-same-subject ()
10242   "Select previous article which has the same subject as current one."
10243   (interactive)
10244   (gnus-set-global-variables)
10245   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10246
10247 (defun gnus-summary-next-unread-same-subject ()
10248   "Select next unread article which has the same subject as current one."
10249   (interactive)
10250   (gnus-set-global-variables)
10251   (gnus-summary-next-article t (gnus-summary-article-subject)))
10252
10253 (defun gnus-summary-prev-unread-same-subject ()
10254   "Select previous unread article which has the same subject as current one."
10255   (interactive)
10256   (gnus-set-global-variables)
10257   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10258
10259 (defun gnus-summary-first-unread-article ()
10260   "Select the first unread article.
10261 Return nil if there are no unread articles."
10262   (interactive)
10263   (gnus-set-global-variables)
10264   (prog1
10265       (if (gnus-summary-first-subject t)
10266           (progn
10267             (gnus-summary-show-thread)
10268             (gnus-summary-first-subject t)
10269             (gnus-summary-display-article (gnus-summary-article-number))))
10270     (gnus-summary-position-point)))
10271
10272 (defun gnus-summary-best-unread-article ()
10273   "Select the unread article with the highest score."
10274   (interactive)
10275   (gnus-set-global-variables)
10276   (let ((best -1000000)
10277         (data gnus-newsgroup-data)
10278         article score)
10279     (while data
10280       (and (gnus-data-unread-p (car data))
10281            (> (setq score
10282                     (gnus-summary-article-score (gnus-data-number (car data))))
10283               best)
10284            (setq best score
10285                  article (gnus-data-number (car data))))
10286       (setq data (cdr data)))
10287     (if article
10288         (gnus-summary-goto-article article)
10289       (error "No unread articles"))
10290     (gnus-summary-position-point)))
10291
10292 (defun gnus-summary-last-subject ()
10293   "Go to the last displayed subject line in the group."
10294   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10295     (when article
10296       (gnus-summary-goto-subject article))))
10297
10298 (defun gnus-summary-goto-article (article &optional all-headers force)
10299   "Fetch ARTICLE and display it if it exists.
10300 If ALL-HEADERS is non-nil, no header lines are hidden."
10301   (interactive
10302    (list
10303     (string-to-int
10304      (completing-read
10305       "Article number: "
10306       (mapcar (lambda (number) (list (int-to-string number)))
10307               gnus-newsgroup-limit)))
10308     current-prefix-arg
10309     t))
10310   (prog1
10311       (if (gnus-summary-goto-subject article force)
10312           (gnus-summary-display-article article all-headers)
10313         (gnus-message 4 "Couldn't go to article %s" article) nil)
10314     (gnus-summary-position-point)))
10315
10316 (defun gnus-summary-goto-last-article ()
10317   "Go to the previously read article."
10318   (interactive)
10319   (prog1
10320       (and gnus-last-article
10321            (gnus-summary-goto-article gnus-last-article))
10322     (gnus-summary-position-point)))
10323
10324 (defun gnus-summary-pop-article (number)
10325   "Pop one article off the history and go to the previous.
10326 NUMBER articles will be popped off."
10327   (interactive "p")
10328   (let (to)
10329     (setq gnus-newsgroup-history
10330           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10331     (if to
10332         (gnus-summary-goto-article (car to))
10333       (error "Article history empty")))
10334   (gnus-summary-position-point))
10335
10336 ;; Summary commands and functions for limiting the summary buffer.
10337
10338 (defun gnus-summary-limit-to-articles (n)
10339   "Limit the summary buffer to the next N articles.
10340 If not given a prefix, use the process marked articles instead."
10341   (interactive "P")
10342   (gnus-set-global-variables)
10343   (prog1
10344       (let ((articles (gnus-summary-work-articles n)))
10345         (setq gnus-newsgroup-processable nil)
10346         (gnus-summary-limit articles))
10347     (gnus-summary-position-point)))
10348
10349 (defun gnus-summary-pop-limit (&optional total)
10350   "Restore the previous limit.
10351 If given a prefix, remove all limits."
10352   (interactive "P")
10353   (gnus-set-global-variables)
10354   (when total 
10355     (setq gnus-newsgroup-limits
10356           (list (mapcar (lambda (h) (mail-header-number h))
10357                         gnus-newsgroup-headers))))
10358   (unless gnus-newsgroup-limits
10359     (error "No limit to pop"))
10360   (prog1
10361       (gnus-summary-limit nil 'pop)
10362     (gnus-summary-position-point)))
10363
10364 (defun gnus-summary-limit-to-subject (subject &optional header)
10365   "Limit the summary buffer to articles that have subjects that match a regexp."
10366   (interactive "sRegexp: ")
10367   (unless header
10368     (setq header "subject"))
10369   (when (not (equal "" subject))
10370     (prog1
10371         (let ((articles (gnus-summary-find-matching
10372                          (or header "subject") subject 'all)))
10373           (or articles (error "Found no matches for \"%s\"" subject))
10374           (gnus-summary-limit articles))
10375       (gnus-summary-position-point))))
10376
10377 (defun gnus-summary-limit-to-author (from)
10378   "Limit the summary buffer to articles that have authors that match a regexp."
10379   (interactive "sRegexp: ")
10380   (gnus-summary-limit-to-subject from "from"))
10381
10382 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10383 (make-obsolete
10384  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10385
10386 (defun gnus-summary-limit-to-unread (&optional all)
10387   "Limit the summary buffer to articles that are not marked as read.
10388 If ALL is non-nil, limit strictly to unread articles."
10389   (interactive "P")
10390   (if all
10391       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10392     (gnus-summary-limit-to-marks
10393      ;; Concat all the marks that say that an article is read and have
10394      ;; those removed.
10395      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10396            gnus-killed-mark gnus-kill-file-mark
10397            gnus-low-score-mark gnus-expirable-mark
10398            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10399      'reverse)))
10400
10401 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10402 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10403
10404 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10405   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10406 If REVERSE, limit the summary buffer to articles that are not marked
10407 with MARKS.  MARKS can either be a string of marks or a list of marks.
10408 Returns how many articles were removed."
10409   (interactive "sMarks: ")
10410   (gnus-set-global-variables)
10411   (prog1
10412       (let ((data gnus-newsgroup-data)
10413             (marks (if (listp marks) marks
10414                      (append marks nil))) ; Transform to list.
10415             articles)
10416         (while data
10417           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10418                  (memq (gnus-data-mark (car data)) marks))
10419                (setq articles (cons (gnus-data-number (car data)) articles)))
10420           (setq data (cdr data)))
10421         (gnus-summary-limit articles))
10422     (gnus-summary-position-point)))
10423
10424 (defun gnus-summary-limit-to-score (&optional score)
10425   "Limit to articles with score at or above SCORE."
10426   (interactive "P")
10427   (gnus-set-global-variables)
10428   (setq score (if score
10429                   (prefix-numeric-value score)
10430                 (or gnus-summary-default-score 0)))
10431   (let ((data gnus-newsgroup-data)
10432         articles)
10433     (while data
10434       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10435                 score)
10436         (push (gnus-data-number (car data)) articles))
10437       (setq data (cdr data)))
10438     (prog1
10439         (gnus-summary-limit articles)
10440       (gnus-summary-position-point))))
10441
10442 (defun gnus-summary-limit-include-dormant ()
10443   "Display all the hidden articles that are marked as dormant."
10444   (interactive)
10445   (gnus-set-global-variables)
10446   (or gnus-newsgroup-dormant
10447       (error "There are no dormant articles in this group"))
10448   (prog1
10449       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10450     (gnus-summary-position-point)))
10451
10452 (defun gnus-summary-limit-exclude-dormant ()
10453   "Hide all dormant articles."
10454   (interactive)
10455   (gnus-set-global-variables)
10456   (prog1
10457       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10458     (gnus-summary-position-point)))
10459
10460 (defun gnus-summary-limit-exclude-childless-dormant ()
10461   "Hide all dormant articles that have no children."
10462   (interactive)
10463   (gnus-set-global-variables)
10464   (let ((data (gnus-data-list t))
10465         articles d children)
10466     ;; Find all articles that are either not dormant or have
10467     ;; children.
10468     (while (setq d (pop data))
10469       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10470                 (and (setq children 
10471                            (gnus-article-children (gnus-data-number d)))
10472                      (let (found)
10473                        (while children
10474                          (when (memq (car children) articles)
10475                            (setq children nil
10476                                  found t))
10477                          (pop children))
10478                        found)))
10479         (push (gnus-data-number d) articles)))
10480     ;; Do the limiting.
10481     (prog1
10482         (gnus-summary-limit articles)
10483       (gnus-summary-position-point))))
10484
10485 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10486   "Mark all unread excluded articles as read.
10487 If ALL, mark even excluded ticked and dormants as read."
10488   (interactive "P")
10489   (let ((articles (gnus-sorted-complement
10490                    (sort
10491                     (mapcar (lambda (h) (mail-header-number h))
10492                             gnus-newsgroup-headers)
10493                     '<)
10494                    (sort gnus-newsgroup-limit '<)))
10495         article)
10496     (setq gnus-newsgroup-unreads nil)
10497     (if all
10498         (setq gnus-newsgroup-dormant nil
10499               gnus-newsgroup-marked nil
10500               gnus-newsgroup-reads
10501               (nconc
10502                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10503                gnus-newsgroup-reads))
10504       (while (setq article (pop articles))
10505         (unless (or (memq article gnus-newsgroup-dormant)
10506                     (memq article gnus-newsgroup-marked))
10507           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10508
10509 (defun gnus-summary-limit (articles &optional pop)
10510   (if pop
10511       ;; We pop the previous limit off the stack and use that.
10512       (setq articles (car gnus-newsgroup-limits)
10513             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10514     ;; We use the new limit, so we push the old limit on the stack.
10515     (setq gnus-newsgroup-limits
10516           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10517   ;; Set the limit.
10518   (setq gnus-newsgroup-limit articles)
10519   (let ((total (length gnus-newsgroup-data))
10520         (data (gnus-data-find-list (gnus-summary-article-number)))
10521         found)
10522     ;; This will do all the work of generating the new summary buffer
10523     ;; according to the new limit.
10524     (gnus-summary-prepare)
10525     ;; Hide any threads, possibly.
10526     (and gnus-show-threads
10527          gnus-thread-hide-subtree
10528          (gnus-summary-hide-all-threads))
10529     ;; Try to return to the article you were at, or one in the
10530     ;; neighborhood.
10531     (if data
10532         ;; We try to find some article after the current one.
10533         (while data
10534           (and (gnus-summary-goto-subject
10535                 (gnus-data-number (car data)) nil t)
10536                (setq data nil
10537                      found t))
10538           (setq data (cdr data))))
10539     (or found
10540         ;; If there is no data, that means that we were after the last
10541         ;; article.  The same goes when we can't find any articles
10542         ;; after the current one.
10543         (progn
10544           (goto-char (point-max))
10545           (gnus-summary-find-prev)))
10546     ;; We return how many articles were removed from the summary
10547     ;; buffer as a result of the new limit.
10548     (- total (length gnus-newsgroup-data))))
10549
10550 (defsubst gnus-cut-thread (thread)
10551   "Go forwards in the thread until we find an article that we want to display."
10552   (if (eq gnus-fetch-old-headers 'some)
10553       (while (and thread
10554                   (memq (mail-header-number (car thread)) 
10555                         gnus-newsgroup-ancient)
10556                   (<= (length (cdr thread)) 1))
10557         (setq thread (cadr thread)))
10558     (while (and thread
10559                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10560                 (= (length (cdr thread)) 1))
10561       (setq thread (cadr thread))))
10562   thread)
10563
10564 (defun gnus-cut-threads (threads)
10565   "Cut off all uninteresting articles from the beginning of threads."
10566   (when (or (eq gnus-fetch-old-headers 'some)
10567             (eq gnus-build-sparse-threads 'some)
10568             (eq gnus-build-sparse-threads 'more))
10569     (let ((th threads))
10570       (while th
10571         (setcar th (gnus-cut-thread (car th)))
10572         (setq th (cdr th)))))
10573   threads)
10574
10575 (defun gnus-summary-initial-limit (&optional show-if-empty)
10576   "Figure out what the initial limit is supposed to be on group entry.
10577 This entails weeding out unwanted dormants, low-scored articles,
10578 fetch-old-headers verbiage, and so on."
10579   ;; Most groups have nothing to remove.
10580   (if (or gnus-inhibit-limiting
10581           (and (null gnus-newsgroup-dormant)
10582                (not (eq gnus-fetch-old-headers 'some))
10583                (null gnus-summary-expunge-below)
10584                (not (eq gnus-build-sparse-threads 'some))
10585                (not (eq gnus-build-sparse-threads 'more))
10586                (null gnus-thread-expunge-below)
10587                (not gnus-use-nocem)))
10588       () ; Do nothing.
10589     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10590     (setq gnus-newsgroup-limit nil)
10591     (mapatoms
10592      (lambda (node)
10593        (unless (car (symbol-value node))
10594          ;; These threads have no parents -- they are roots.
10595          (let ((nodes (cdr (symbol-value node)))
10596                thread)
10597            (while nodes
10598              (if (and gnus-thread-expunge-below
10599                       (< (gnus-thread-total-score (car nodes))
10600                          gnus-thread-expunge-below))
10601                  (gnus-expunge-thread (pop nodes))
10602                (setq thread (pop nodes))
10603                (gnus-summary-limit-children thread))))))
10604      gnus-newsgroup-dependencies)
10605     ;; If this limitation resulted in an empty group, we might
10606     ;; pop the previous limit and use it instead.
10607     (when (and (not gnus-newsgroup-limit)
10608                show-if-empty)
10609       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10610     gnus-newsgroup-limit))
10611
10612 (defun gnus-summary-limit-children (thread)
10613   "Return 1 if this subthread is visible and 0 if it is not."
10614   ;; First we get the number of visible children to this thread.  This
10615   ;; is done by recursing down the thread using this function, so this
10616   ;; will really go down to a leaf article first, before slowly
10617   ;; working its way up towards the root.
10618   (when thread
10619     (let ((children
10620            (if (cdr thread)
10621                (apply '+ (mapcar 'gnus-summary-limit-children
10622                                  (cdr thread)))
10623              0))
10624           (number (mail-header-number (car thread)))
10625           score)
10626       (if (or
10627            ;; If this article is dormant and has absolutely no visible
10628            ;; children, then this article isn't visible.
10629            (and (memq number gnus-newsgroup-dormant)
10630                 (= children 0))
10631            ;; If this is a "fetch-old-headered" and there is only one
10632            ;; visible child (or less), then we don't want this article.
10633            (and (eq gnus-fetch-old-headers 'some)
10634                 (memq number gnus-newsgroup-ancient)
10635                 (zerop children))
10636            ;; If this is a sparsely inserted article with no children,
10637            ;; we don't want it.
10638            (and (eq gnus-build-sparse-threads 'some)
10639                 (memq number gnus-newsgroup-sparse)
10640                 (zerop children))
10641            ;; If we use expunging, and this article is really
10642            ;; low-scored, then we don't want this article.
10643            (when (and gnus-summary-expunge-below
10644                       (< (setq score
10645                                (or (cdr (assq number gnus-newsgroup-scored))
10646                                    gnus-summary-default-score))
10647                          gnus-summary-expunge-below))
10648              ;; We increase the expunge-tally here, but that has
10649              ;; nothing to do with the limits, really.
10650              (incf gnus-newsgroup-expunged-tally)
10651              ;; We also mark as read here, if that's wanted.
10652              (when (and gnus-summary-mark-below
10653                         (< score gnus-summary-mark-below))
10654                (setq gnus-newsgroup-unreads
10655                      (delq number gnus-newsgroup-unreads))
10656                (if gnus-newsgroup-auto-expire
10657                    (push number gnus-newsgroup-expirable)
10658                  (push (cons number gnus-low-score-mark)
10659                        gnus-newsgroup-reads)))
10660              t)
10661            (and gnus-use-nocem
10662                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10663           ;; Nope, invisible article.
10664           0
10665         ;; Ok, this article is to be visible, so we add it to the limit
10666         ;; and return 1.
10667         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10668         1))))
10669
10670 (defun gnus-expunge-thread (thread)
10671   "Mark all articles in THREAD as read."
10672   (let* ((number (mail-header-number (car thread))))
10673     (incf gnus-newsgroup-expunged-tally)
10674     ;; We also mark as read here, if that's wanted.
10675     (setq gnus-newsgroup-unreads
10676           (delq number gnus-newsgroup-unreads))
10677     (if gnus-newsgroup-auto-expire
10678         (push number gnus-newsgroup-expirable)
10679       (push (cons number gnus-low-score-mark)
10680             gnus-newsgroup-reads)))
10681   ;; Go recursively through all subthreads.
10682   (mapcar 'gnus-expunge-thread (cdr thread)))
10683
10684 ;; Summary article oriented commands
10685
10686 (defun gnus-summary-refer-parent-article (n)
10687   "Refer parent article N times.
10688 The difference between N and the number of articles fetched is returned."
10689   (interactive "p")
10690   (gnus-set-global-variables)
10691   (while
10692       (and
10693        (> n 0)
10694        (let* ((header (gnus-summary-article-header))
10695               (ref
10696                ;; If we try to find the parent of the currently
10697                ;; displayed article, then we take a look at the actual
10698                ;; References header, since this is slightly more
10699                ;; reliable than the References field we got from the
10700                ;; server.
10701                (if (and (eq (mail-header-number header)
10702                             (cdr gnus-article-current))
10703                         (equal gnus-newsgroup-name
10704                                (car gnus-article-current)))
10705                    (save-excursion
10706                      (set-buffer gnus-original-article-buffer)
10707                      (nnheader-narrow-to-headers)
10708                      (prog1
10709                          (mail-fetch-field "references")
10710                        (widen)))
10711                  ;; It's not the current article, so we take a bet on
10712                  ;; the value we got from the server.
10713                  (mail-header-references header))))
10714          (if (setq ref (or ref (mail-header-references header)))
10715              (or (gnus-summary-refer-article (gnus-parent-id ref))
10716                  (gnus-message 1 "Couldn't find parent"))
10717            (gnus-message 1 "No references in article %d"
10718                          (gnus-summary-article-number))
10719            nil)))
10720     (setq n (1- n)))
10721   (gnus-summary-position-point)
10722   n)
10723
10724 (defun gnus-summary-refer-references ()
10725   "Fetch all articles mentioned in the References header.
10726 Return how many articles were fetched."
10727   (interactive)
10728   (gnus-set-global-variables)
10729   (let ((ref (mail-header-references (gnus-summary-article-header)))
10730         (current (gnus-summary-article-number))
10731         (n 0))
10732     ;; For each Message-ID in the References header...
10733     (while (string-match "<[^>]*>" ref)
10734       (incf n)
10735       ;; ... fetch that article.
10736       (gnus-summary-refer-article
10737        (prog1 (match-string 0 ref)
10738          (setq ref (substring ref (match-end 0))))))
10739     (gnus-summary-goto-subject current)
10740     (gnus-summary-position-point)
10741     n))
10742
10743 (defun gnus-summary-refer-article (message-id)
10744   "Fetch an article specified by MESSAGE-ID."
10745   (interactive "sMessage-ID: ")
10746   (when (and (stringp message-id)
10747              (not (zerop (length message-id))))
10748     ;; Construct the correct Message-ID if necessary.
10749     ;; Suggested by tale@pawl.rpi.edu.
10750     (unless (string-match "^<" message-id)
10751       (setq message-id (concat "<" message-id)))
10752     (unless (string-match ">$" message-id)
10753       (setq message-id (concat message-id ">")))
10754     (let ((header (car (gnus-gethash (downcase message-id)
10755                                      gnus-newsgroup-dependencies))))
10756       (if header
10757           ;; The article is present in the buffer, to we just go to it.
10758           (gnus-summary-goto-article (mail-header-number header) nil t)
10759         ;; We fetch the article
10760         (let ((gnus-override-method gnus-refer-article-method)
10761               number)
10762           ;; Start the special refer-article method, if necessary.
10763           (when gnus-refer-article-method
10764             (gnus-check-server gnus-refer-article-method))
10765           ;; Fetch the header, and display the article.
10766           (if (setq number (gnus-summary-insert-subject message-id))
10767               (gnus-summary-select-article nil nil nil number)
10768             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10769
10770 (defun gnus-summary-enter-digest-group (&optional force)
10771   "Enter a digest group based on the current article."
10772   (interactive "P")
10773   (gnus-set-global-variables)
10774   (gnus-summary-select-article)
10775   (let ((name (format "%s-%d"
10776                       (gnus-group-prefixed-name
10777                        gnus-newsgroup-name (list 'nndoc ""))
10778                       gnus-current-article))
10779         (ogroup gnus-newsgroup-name)
10780         (case-fold-search t)
10781         (buf (current-buffer))
10782         dig)
10783     (save-excursion
10784       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10785       (insert-buffer-substring gnus-original-article-buffer)
10786       (narrow-to-region
10787        (goto-char (point-min))
10788        (or (search-forward "\n\n" nil t) (point)))
10789       (goto-char (point-min))
10790       (delete-matching-lines "^\\(Path\\):\\|^From ")
10791       (widen))
10792     (unwind-protect
10793         (if (gnus-group-read-ephemeral-group
10794              name `(nndoc ,name (nndoc-address
10795                                  ,(get-buffer dig))
10796                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10797             ;; Make all postings to this group go to the parent group.
10798             (nconc (gnus-info-params (gnus-get-info name))
10799                    (list (cons 'to-group ogroup)))
10800           ;; Couldn't select this doc group.
10801           (switch-to-buffer buf)
10802           (gnus-set-global-variables)
10803           (gnus-configure-windows 'summary)
10804           (gnus-message 3 "Article couldn't be entered?"))
10805       (kill-buffer dig))))
10806
10807 (defun gnus-summary-isearch-article (&optional regexp-p)
10808   "Do incremental search forward on the current article.
10809 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10810   (interactive "P")
10811   (gnus-set-global-variables)
10812   (gnus-summary-select-article)
10813   (gnus-configure-windows 'article)
10814   (gnus-eval-in-buffer-window
10815    gnus-article-buffer
10816    (goto-char (point-min))
10817    (isearch-forward regexp-p)))
10818
10819 (defun gnus-summary-search-article-forward (regexp &optional backward)
10820   "Search for an article containing REGEXP forward.
10821 If BACKWARD, search backward instead."
10822   (interactive
10823    (list (read-string
10824           (format "Search article %s (regexp%s): "
10825                   (if current-prefix-arg "backward" "forward")
10826                   (if gnus-last-search-regexp
10827                       (concat ", default " gnus-last-search-regexp)
10828                     "")))
10829          current-prefix-arg))
10830   (gnus-set-global-variables)
10831   (if (string-equal regexp "")
10832       (setq regexp (or gnus-last-search-regexp ""))
10833     (setq gnus-last-search-regexp regexp))
10834   (if (gnus-summary-search-article regexp backward)
10835       (gnus-article-set-window-start
10836        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10837     (error "Search failed: \"%s\"" regexp)))
10838
10839 (defun gnus-summary-search-article-backward (regexp)
10840   "Search for an article containing REGEXP backward."
10841   (interactive
10842    (list (read-string
10843           (format "Search article backward (regexp%s): "
10844                   (if gnus-last-search-regexp
10845                       (concat ", default " gnus-last-search-regexp)
10846                     "")))))
10847   (gnus-summary-search-article-forward regexp 'backward))
10848
10849 (defun gnus-summary-search-article (regexp &optional backward)
10850   "Search for an article containing REGEXP.
10851 Optional argument BACKWARD means do search for backward.
10852 gnus-select-article-hook is not called during the search."
10853   (let ((gnus-select-article-hook nil)  ;Disable hook.
10854         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10855         (re-search
10856          (if backward
10857              (function re-search-backward) (function re-search-forward)))
10858         (found nil)
10859         (last nil))
10860     ;; Hidden thread subtrees must be searched for ,too.
10861     (gnus-summary-show-all-threads)
10862     ;; First of all, search current article.
10863     ;; We don't want to read article again from NNTP server nor reset
10864     ;; current point.
10865     (gnus-summary-select-article)
10866     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10867     (setq last gnus-current-article)
10868     (gnus-eval-in-buffer-window
10869      gnus-article-buffer
10870      (save-restriction
10871        (widen)
10872        ;; Begin search from current point.
10873        (setq found (funcall re-search regexp nil t))))
10874     ;; Then search next articles.
10875     (while (and (not found)
10876                 (gnus-summary-display-article
10877                  (if backward (gnus-summary-find-prev)
10878                    (gnus-summary-find-next))))
10879       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10880       (gnus-eval-in-buffer-window
10881        gnus-article-buffer
10882        (save-restriction
10883          (widen)
10884          (goto-char (if backward (point-max) (point-min)))
10885          (setq found (funcall re-search regexp nil t)))))
10886     (message "")
10887     ;; Adjust article pointer.
10888     (or (eq last gnus-current-article)
10889         (setq gnus-last-article last))
10890     ;; Return T if found such article.
10891     found))
10892
10893 (defun gnus-summary-find-matching (header regexp &optional backward unread
10894                                           not-case-fold)
10895   "Return a list of all articles that match REGEXP on HEADER.
10896 The search stars on the current article and goes forwards unless
10897 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10898 If UNREAD is non-nil, only unread articles will
10899 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10900 in the comparisons."
10901   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10902                 (gnus-data-find-list
10903                  (gnus-summary-article-number) (gnus-data-list backward))))
10904         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10905         (case-fold-search (not not-case-fold))
10906         articles d)
10907     (or (fboundp (intern (concat "mail-header-" header)))
10908         (error "%s is not a valid header" header))
10909     (while data
10910       (setq d (car data))
10911       (and (or (not unread)             ; We want all articles...
10912                (gnus-data-unread-p d))  ; Or just unreads.
10913            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10914            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10915            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10916       (setq data (cdr data)))
10917     (nreverse articles)))
10918
10919 (defun gnus-summary-execute-command (header regexp command &optional backward)
10920   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10921 If HEADER is an empty string (or nil), the match is done on the entire
10922 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10923   (interactive
10924    (list (let ((completion-ignore-case t))
10925            (completing-read
10926             "Header name: "
10927             (mapcar (lambda (string) (list string))
10928                     '("Number" "Subject" "From" "Lines" "Date"
10929                       "Message-ID" "Xref" "References" "Body"))
10930             nil 'require-match))
10931          (read-string "Regexp: ")
10932          (read-key-sequence "Command: ")
10933          current-prefix-arg))
10934   (when (equal header "Body")
10935     (setq header ""))
10936   (gnus-set-global-variables)
10937   ;; Hidden thread subtrees must be searched as well.
10938   (gnus-summary-show-all-threads)
10939   ;; We don't want to change current point nor window configuration.
10940   (save-excursion
10941     (save-window-excursion
10942       (gnus-message 6 "Executing %s..." (key-description command))
10943       ;; We'd like to execute COMMAND interactively so as to give arguments.
10944       (gnus-execute header regexp
10945                     `(lambda () (call-interactively ',(key-binding command)))
10946                     backward)
10947       (gnus-message 6 "Executing %s...done" (key-description command)))))
10948
10949 (defun gnus-summary-beginning-of-article ()
10950   "Scroll the article back to the beginning."
10951   (interactive)
10952   (gnus-set-global-variables)
10953   (gnus-summary-select-article)
10954   (gnus-configure-windows 'article)
10955   (gnus-eval-in-buffer-window
10956    gnus-article-buffer
10957    (widen)
10958    (goto-char (point-min))
10959    (and gnus-break-pages (gnus-narrow-to-page))))
10960
10961 (defun gnus-summary-end-of-article ()
10962   "Scroll to the end of the article."
10963   (interactive)
10964   (gnus-set-global-variables)
10965   (gnus-summary-select-article)
10966   (gnus-configure-windows 'article)
10967   (gnus-eval-in-buffer-window
10968    gnus-article-buffer
10969    (widen)
10970    (goto-char (point-max))
10971    (recenter -3)
10972    (and gnus-break-pages (gnus-narrow-to-page))))
10973
10974 (defun gnus-summary-show-article (&optional arg)
10975   "Force re-fetching of the current article.
10976 If ARG (the prefix) is non-nil, show the raw article without any
10977 article massaging functions being run."
10978   (interactive "P")
10979   (gnus-set-global-variables)
10980   (if (not arg)
10981       ;; Select the article the normal way.
10982       (gnus-summary-select-article nil 'force)
10983     ;; Bind the article treatment functions to nil.
10984     (let ((gnus-have-all-headers t)
10985           gnus-article-display-hook
10986           gnus-article-prepare-hook
10987           gnus-visual)
10988       (gnus-summary-select-article nil 'force)))
10989 ;  (gnus-configure-windows 'article)
10990   (gnus-summary-position-point))
10991
10992 (defun gnus-summary-verbose-headers (&optional arg)
10993   "Toggle permanent full header display.
10994 If ARG is a positive number, turn header display on.
10995 If ARG is a negative number, turn header display off."
10996   (interactive "P")
10997   (gnus-set-global-variables)
10998   (gnus-summary-toggle-header arg)
10999   (setq gnus-show-all-headers
11000         (cond ((or (not (numberp arg))
11001                    (zerop arg))
11002                (not gnus-show-all-headers))
11003               ((natnump arg)
11004                t))))
11005
11006 (defun gnus-summary-toggle-header (&optional arg)
11007   "Show the headers if they are hidden, or hide them if they are shown.
11008 If ARG is a positive number, show the entire header.
11009 If ARG is a negative number, hide the unwanted header lines."
11010   (interactive "P")
11011   (gnus-set-global-variables)
11012   (save-excursion
11013     (set-buffer gnus-article-buffer)
11014     (let* ((buffer-read-only nil)
11015            (inhibit-point-motion-hooks t)
11016            (hidden (text-property-any
11017                     (goto-char (point-min)) (search-forward "\n\n")
11018                     'invisible t))
11019            e)
11020       (goto-char (point-min))
11021       (when (search-forward "\n\n" nil t)
11022         (delete-region (point-min) (1- (point))))
11023       (goto-char (point-min))
11024       (save-excursion
11025         (set-buffer gnus-original-article-buffer)
11026         (goto-char (point-min))
11027         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11028       (insert-buffer-substring gnus-original-article-buffer 1 e)
11029       (let ((gnus-inhibit-hiding t))
11030         (run-hooks 'gnus-article-display-hook))
11031       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11032           (gnus-article-hide-headers)))))
11033
11034 (defun gnus-summary-show-all-headers ()
11035   "Make all header lines visible."
11036   (interactive)
11037   (gnus-set-global-variables)
11038   (gnus-article-show-all-headers))
11039
11040 (defun gnus-summary-toggle-mime (&optional arg)
11041   "Toggle MIME processing.
11042 If ARG is a positive number, turn MIME processing on."
11043   (interactive "P")
11044   (gnus-set-global-variables)
11045   (setq gnus-show-mime
11046         (if (null arg) (not gnus-show-mime)
11047           (> (prefix-numeric-value arg) 0)))
11048   (gnus-summary-select-article t 'force))
11049
11050 (defun gnus-summary-caesar-message (&optional arg)
11051   "Caesar rotate the current article by 13.
11052 The numerical prefix specifies how manu places to rotate each letter
11053 forward."
11054   (interactive "P")
11055   (gnus-set-global-variables)
11056   (gnus-summary-select-article)
11057   (let ((mail-header-separator ""))
11058     (gnus-eval-in-buffer-window
11059      gnus-article-buffer
11060      (save-restriction
11061        (widen)
11062        (let ((start (window-start)))
11063          (news-caesar-buffer-body arg)
11064          (set-window-start (get-buffer-window (current-buffer)) start))))))
11065
11066 (defun gnus-summary-stop-page-breaking ()
11067   "Stop page breaking in the current article."
11068   (interactive)
11069   (gnus-set-global-variables)
11070   (gnus-summary-select-article)
11071   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11072
11073 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11074   "Move the current article to a different newsgroup.
11075 If N is a positive number, move the N next articles.
11076 If N is a negative number, move the N previous articles.
11077 If N is nil and any articles have been marked with the process mark,
11078 move those articles instead.
11079 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11080 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11081 re-spool using this method.
11082
11083 For this function to work, both the current newsgroup and the
11084 newsgroup that you want to move to have to support the `request-move'
11085 and `request-accept' functions."
11086   (interactive "P")
11087   (unless action (setq action 'move))
11088   (gnus-set-global-variables)
11089   ;; Check whether the source group supports the required functions.
11090   (cond ((and (eq action 'move)
11091               (not (gnus-check-backend-function
11092                     'request-move-article gnus-newsgroup-name)))
11093          (error "The current group does not support article moving"))
11094         ((and (eq action 'crosspost)
11095               (not (gnus-check-backend-function
11096                     'request-replace-article gnus-newsgroup-name)))
11097          (error "The current group does not support article editing")))
11098   (let ((articles (gnus-summary-work-articles n))
11099         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11100         (names '((move "move" "Moving")
11101                  (copy "copy" "Copying")
11102                  (crosspost "crosspost" "Crossposting")))
11103         (copy-buf (save-excursion
11104                     (nnheader-set-temp-buffer " *copy article*")))
11105         art-group to-method new-xref article)
11106     (unless (assq action names)
11107       (error "Unknown action %s" action))
11108     ;; Read the newsgroup name.
11109     (when (and (not to-newsgroup)
11110                (not select-method))
11111       (setq to-newsgroup
11112             (gnus-read-move-group-name
11113              (cadr (assq action names))
11114              gnus-current-move-group articles prefix))
11115       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11116     (setq to-method (if select-method (list select-method "")
11117                       (gnus-find-method-for-group to-newsgroup)))
11118     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11119     ;;(error "Can't %s to the same group you're already in" action))
11120     ;; Check the method we are to move this article to...
11121     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11122         (error "%s does not support article copying" (car to-method)))
11123     (or (gnus-check-server to-method)
11124         (error "Can't open server %s" (car to-method)))
11125     (gnus-message 6 "%s to %s: %s..."
11126                   (caddr (assq action names))
11127                   (or select-method to-newsgroup) articles)
11128     (while articles
11129       (setq article (pop articles))
11130       (setq
11131        art-group
11132        (cond
11133         ;; Move the article.
11134         ((eq action 'move)
11135          (gnus-request-move-article
11136           article                       ; Article to move
11137           gnus-newsgroup-name           ; From newsgrouo
11138           (nth 1 (gnus-find-method-for-group
11139                   gnus-newsgroup-name)) ; Server
11140           (list 'gnus-request-accept-article
11141                 (if select-method
11142                     (list 'quote select-method)
11143                   to-newsgroup)
11144                 (not articles))         ; Accept form
11145           (not articles)))              ; Only save nov last time
11146         ;; Copy the article.
11147         ((eq action 'copy)
11148          (save-excursion
11149            (set-buffer copy-buf)
11150            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11151            (gnus-request-accept-article
11152             (if select-method select-method to-newsgroup)
11153             (not articles))))
11154         ;; Crosspost the article.
11155         ((eq action 'crosspost)
11156          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11157            (setq new-xref (concat gnus-newsgroup-name ":" article))
11158            (if (and xref (not (string= xref "")))
11159                (progn
11160                  (when (string-match "^Xref: " xref)
11161                    (setq xref (substring xref (match-end 0))))
11162                  (setq new-xref (concat xref " " new-xref)))
11163              (setq new-xref (concat (system-name) " " new-xref)))
11164            (save-excursion
11165              (set-buffer copy-buf)
11166              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11167              (nnheader-replace-header "xref" new-xref)
11168              (gnus-request-accept-article
11169               (if select-method select-method to-newsgroup)
11170               (not articles)))))))
11171       (if (not art-group)
11172           (gnus-message 1 "Couldn't %s article %s"
11173                         (cadr (assq action names)) article)
11174         (let* ((entry
11175                 (or
11176                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11177                  (gnus-gethash
11178                   (gnus-group-prefixed-name
11179                    (car art-group)
11180                    (if select-method (list select-method "")
11181                      (gnus-find-method-for-group to-newsgroup)))
11182                   gnus-newsrc-hashtb)))
11183                (info (nth 2 entry)))
11184           ;; Update the group that has been moved to.
11185           (when (and info
11186                      (memq action '(move copy)))
11187             (unless (memq article gnus-newsgroup-unreads)
11188               (gnus-info-set-read
11189                info (gnus-add-to-range (gnus-info-read info)
11190                                        (list (cdr art-group)))))
11191
11192             ;; Copy any marks over to the new group.
11193             (let ((marks gnus-article-mark-lists)
11194                   (to-article (cdr art-group)))
11195
11196               ;; See whether the article is to be put in the cache.
11197               (when gnus-use-cache
11198                 (gnus-cache-possibly-enter-article
11199                  (gnus-info-group info) to-article
11200                  (let ((header (copy-sequence
11201                                 (gnus-summary-article-header article))))
11202                    (mail-header-set-number header to-article)
11203                    header)
11204                  (memq article gnus-newsgroup-marked)
11205                  (memq article gnus-newsgroup-dormant)
11206                  (memq article gnus-newsgroup-unreads)))
11207
11208               (while marks
11209                 (when (memq article (symbol-value
11210                                      (intern (format "gnus-newsgroup-%s"
11211                                                      (caar marks)))))
11212                   (gnus-add-marked-articles
11213                    (gnus-info-group info) (cdar marks)
11214                    (list to-article) info))
11215                 (setq marks (cdr marks)))))
11216
11217           ;; Update the Xref header in this article to point to
11218           ;; the new crossposted article we have just created.
11219           (when (eq action 'crosspost)
11220             (save-excursion
11221               (set-buffer copy-buf)
11222               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11223               (nnheader-replace-header
11224                "xref" (concat new-xref " " (gnus-group-prefixed-name
11225                                             (car art-group) to-method)
11226                               ":" (cdr art-group)))
11227               (gnus-request-replace-article
11228                article gnus-newsgroup-name (current-buffer)))))
11229
11230         (gnus-summary-goto-subject article)
11231         (when (eq action 'move)
11232           (gnus-summary-mark-article article gnus-canceled-mark)))
11233       (gnus-summary-remove-process-mark article))
11234     (gnus-kill-buffer copy-buf)
11235     (gnus-summary-position-point)
11236     (gnus-set-mode-line 'summary)))
11237
11238 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11239   "Move the current article to a different newsgroup.
11240 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11241 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11242 re-spool using this method."
11243   (interactive "P")
11244   (gnus-summary-move-article n nil select-method 'copy))
11245
11246 (defun gnus-summary-crosspost-article (&optional n)
11247   "Crosspost the current article to some other group."
11248   (interactive "P")
11249   (gnus-summary-move-article n nil nil 'crosspost))
11250
11251 (defun gnus-summary-respool-article (&optional n respool-method)
11252   "Respool the current article.
11253 The article will be squeezed through the mail spooling process again,
11254 which means that it will be put in some mail newsgroup or other
11255 depending on `nnmail-split-methods'.
11256 If N is a positive number, respool the N next articles.
11257 If N is a negative number, respool the N previous articles.
11258 If N is nil and any articles have been marked with the process mark,
11259 respool those articles instead.
11260
11261 Respooling can be done both from mail groups and \"real\" newsgroups.
11262 In the former case, the articles in question will be moved from the
11263 current group into whatever groups they are destined to.  In the
11264 latter case, they will be copied into the relevant groups."
11265   (interactive "P")
11266   (gnus-set-global-variables)
11267   (let ((respool-methods (gnus-methods-using 'respool))
11268         (methname
11269          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11270     (or respool-method
11271         (setq respool-method
11272               (completing-read
11273                "What method do you want to use when respooling? "
11274                respool-methods nil t methname)))
11275     (or (string= respool-method "")
11276         (if (assoc (symbol-name
11277                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11278                    respool-methods)
11279             (gnus-summary-move-article n nil (intern respool-method))
11280           (gnus-summary-copy-article n nil (intern respool-method))))))
11281
11282 (defun gnus-summary-import-article (file)
11283   "Import a random file into a mail newsgroup."
11284   (interactive "fImport file: ")
11285   (gnus-set-global-variables)
11286   (let ((group gnus-newsgroup-name)
11287         (now (current-time))
11288         atts lines)
11289     (or (gnus-check-backend-function 'request-accept-article group)
11290         (error "%s does not support article importing" group))
11291     (or (file-readable-p file)
11292         (not (file-regular-p file))
11293         (error "Can't read %s" file))
11294     (save-excursion
11295       (set-buffer (get-buffer-create " *import file*"))
11296       (buffer-disable-undo (current-buffer))
11297       (erase-buffer)
11298       (insert-file-contents file)
11299       (goto-char (point-min))
11300       (unless (nnheader-article-p)
11301         ;; This doesn't look like an article, so we fudge some headers.
11302         (setq atts (file-attributes file)
11303               lines (count-lines (point-min) (point-max)))
11304         (insert "From: " (read-string "From: ") "\n"
11305                 "Subject: " (read-string "Subject: ") "\n"
11306                 "Date: " (timezone-make-date-arpa-standard
11307                           (current-time-string (nth 5 atts))
11308                           (current-time-zone now)
11309                           (current-time-zone now)) "\n"
11310                 "Message-ID: " (gnus-inews-message-id) "\n"
11311                 "Lines: " (int-to-string lines) "\n"
11312                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11313       (gnus-request-accept-article group t)
11314       (kill-buffer (current-buffer)))))
11315
11316 (defun gnus-summary-expire-articles ()
11317   "Expire all articles that are marked as expirable in the current group."
11318   (interactive)
11319   (gnus-set-global-variables)
11320   (when (gnus-check-backend-function
11321          'request-expire-articles gnus-newsgroup-name)
11322     ;; This backend supports expiry.
11323     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11324            (expirable (if total
11325                           (gnus-list-of-read-articles gnus-newsgroup-name)
11326                         (setq gnus-newsgroup-expirable
11327                               (sort gnus-newsgroup-expirable '<))))
11328            (expiry-wait (gnus-group-get-parameter
11329                          gnus-newsgroup-name 'expiry-wait))
11330            es)
11331       (when expirable
11332         ;; There are expirable articles in this group, so we run them
11333         ;; through the expiry process.
11334         (gnus-message 6 "Expiring articles...")
11335         ;; The list of articles that weren't expired is returned.
11336         (if expiry-wait
11337             (let ((nnmail-expiry-wait-function nil)
11338                   (nnmail-expiry-wait expiry-wait))
11339               (setq es (gnus-request-expire-articles
11340                         expirable gnus-newsgroup-name)))
11341           (setq es (gnus-request-expire-articles
11342                     expirable gnus-newsgroup-name)))
11343         (or total (setq gnus-newsgroup-expirable es))
11344         ;; We go through the old list of expirable, and mark all
11345         ;; really expired articles as nonexistent.
11346         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11347           (let ((gnus-use-cache nil))
11348             (while expirable
11349               (unless (memq (car expirable) es)
11350                 (when (gnus-data-find (car expirable))
11351                   (gnus-summary-mark-article
11352                    (car expirable) gnus-canceled-mark)))
11353               (setq expirable (cdr expirable)))))
11354         (gnus-message 6 "Expiring articles...done")))))
11355
11356 (defun gnus-summary-expire-articles-now ()
11357   "Expunge all expirable articles in the current group.
11358 This means that *all* articles that are marked as expirable will be
11359 deleted forever, right now."
11360   (interactive)
11361   (gnus-set-global-variables)
11362   (or gnus-expert-user
11363       (gnus-y-or-n-p
11364        "Are you really, really, really sure you want to expunge? ")
11365       (error "Phew!"))
11366   (let ((nnmail-expiry-wait 'immediate)
11367         (nnmail-expiry-wait-function nil))
11368     (gnus-summary-expire-articles)))
11369
11370 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11371 (defun gnus-summary-delete-article (&optional n)
11372   "Delete the N next (mail) articles.
11373 This command actually deletes articles.  This is not a marking
11374 command.  The article will disappear forever from your life, never to
11375 return.
11376 If N is negative, delete backwards.
11377 If N is nil and articles have been marked with the process mark,
11378 delete these instead."
11379   (interactive "P")
11380   (gnus-set-global-variables)
11381   (or (gnus-check-backend-function 'request-expire-articles
11382                                    gnus-newsgroup-name)
11383       (error "The current newsgroup does not support article deletion."))
11384   ;; Compute the list of articles to delete.
11385   (let ((articles (gnus-summary-work-articles n))
11386         not-deleted)
11387     (if (and gnus-novice-user
11388              (not (gnus-y-or-n-p
11389                    (format "Do you really want to delete %s forever? "
11390                            (if (> (length articles) 1) "these articles"
11391                              "this article")))))
11392         ()
11393       ;; Delete the articles.
11394       (setq not-deleted (gnus-request-expire-articles
11395                          articles gnus-newsgroup-name 'force))
11396       (while articles
11397         (gnus-summary-remove-process-mark (car articles))
11398         ;; The backend might not have been able to delete the article
11399         ;; after all.
11400         (or (memq (car articles) not-deleted)
11401             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11402         (setq articles (cdr articles))))
11403     (gnus-summary-position-point)
11404     (gnus-set-mode-line 'summary)
11405     not-deleted))
11406
11407 (defun gnus-summary-edit-article (&optional force)
11408   "Enter into a buffer and edit the current article.
11409 This will have permanent effect only in mail groups.
11410 If FORCE is non-nil, allow editing of articles even in read-only
11411 groups."
11412   (interactive "P")
11413   (save-excursion
11414     (set-buffer gnus-summary-buffer)
11415     (gnus-set-global-variables)
11416     (when (and (not force)
11417                (gnus-group-read-only-p))
11418       (error "The current newsgroup does not support article editing."))
11419     (gnus-summary-select-article t nil t)
11420     (gnus-configure-windows 'article)
11421     (select-window (get-buffer-window gnus-article-buffer))
11422     (gnus-message 6 "C-c C-c to end edits")
11423     (setq buffer-read-only nil)
11424     (text-mode)
11425     (use-local-map (copy-keymap (current-local-map)))
11426     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11427     (buffer-enable-undo)
11428     (widen)
11429     (goto-char (point-min))
11430     (search-forward "\n\n" nil t)))
11431
11432 (defun gnus-summary-edit-article-done ()
11433   "Make edits to the current article permanent."
11434   (interactive)
11435   (if (gnus-group-read-only-p)
11436       (progn
11437         (gnus-summary-edit-article-postpone)
11438         (gnus-message
11439          1 "The current newsgroup does not support article editing.")
11440         (ding))
11441     (let ((buf (format "%s" (buffer-string))))
11442       (erase-buffer)
11443       (insert buf)
11444       (if (not (gnus-request-replace-article
11445                 (cdr gnus-article-current) (car gnus-article-current)
11446                 (current-buffer)))
11447           (error "Couldn't replace article.")
11448         (gnus-article-mode)
11449         (use-local-map gnus-article-mode-map)
11450         (setq buffer-read-only t)
11451         (buffer-disable-undo (current-buffer))
11452         (gnus-configure-windows 'summary)
11453         (gnus-summary-update-article (cdr gnus-article-current))
11454         (when gnus-use-cache
11455           (gnus-cache-update-article 
11456            (cdr gnus-article-current) (car gnus-article-current))))
11457       (run-hooks 'gnus-article-display-hook)
11458       (and (gnus-visual-p 'summary-highlight 'highlight)
11459            (run-hooks 'gnus-visual-mark-article-hook)))))
11460
11461 (defun gnus-summary-edit-article-postpone ()
11462   "Postpone changes to the current article."
11463   (interactive)
11464   (gnus-article-mode)
11465   (use-local-map gnus-article-mode-map)
11466   (setq buffer-read-only t)
11467   (buffer-disable-undo (current-buffer))
11468   (gnus-configure-windows 'summary)
11469   (and (gnus-visual-p 'summary-highlight 'highlight)
11470        (run-hooks 'gnus-visual-mark-article-hook)))
11471
11472 (defun gnus-summary-respool-query ()
11473   "Query where the respool algorithm would put this article."
11474   (interactive)
11475   (gnus-set-global-variables)
11476   (gnus-summary-select-article)
11477   (save-excursion
11478     (set-buffer gnus-article-buffer)
11479     (save-restriction
11480       (goto-char (point-min))
11481       (search-forward "\n\n")
11482       (narrow-to-region (point-min) (point))
11483       (pp-eval-expression
11484        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11485
11486 ;; Summary score commands.
11487
11488 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11489
11490 (defun gnus-summary-raise-score (n)
11491   "Raise the score of the current article by N."
11492   (interactive "p")
11493   (gnus-set-global-variables)
11494   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11495
11496 (defun gnus-summary-set-score (n)
11497   "Set the score of the current article to N."
11498   (interactive "p")
11499   (gnus-set-global-variables)
11500   (save-excursion
11501     (gnus-summary-show-thread)
11502     (let ((buffer-read-only nil))
11503       ;; Set score.
11504       (gnus-summary-update-mark
11505        (if (= n (or gnus-summary-default-score 0)) ? 
11506          (if (< n (or gnus-summary-default-score 0))
11507              gnus-score-below-mark gnus-score-over-mark)) 'score))
11508     (let* ((article (gnus-summary-article-number))
11509            (score (assq article gnus-newsgroup-scored)))
11510       (if score (setcdr score n)
11511         (setq gnus-newsgroup-scored
11512               (cons (cons article n) gnus-newsgroup-scored))))
11513     (gnus-summary-update-line)))
11514
11515 (defun gnus-summary-current-score ()
11516   "Return the score of the current article."
11517   (interactive)
11518   (gnus-set-global-variables)
11519   (message "%s" (gnus-summary-article-score)))
11520
11521 ;; Summary marking commands.
11522
11523 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11524   "Mark articles which has the same subject as read, and then select the next.
11525 If UNMARK is positive, remove any kind of mark.
11526 If UNMARK is negative, tick articles."
11527   (interactive "P")
11528   (gnus-set-global-variables)
11529   (if unmark
11530       (setq unmark (prefix-numeric-value unmark)))
11531   (let ((count
11532          (gnus-summary-mark-same-subject
11533           (gnus-summary-article-subject) unmark)))
11534     ;; Select next unread article.  If auto-select-same mode, should
11535     ;; select the first unread article.
11536     (gnus-summary-next-article t (and gnus-auto-select-same
11537                                       (gnus-summary-article-subject)))
11538     (gnus-message 7 "%d article%s marked as %s"
11539                   count (if (= count 1) " is" "s are")
11540                   (if unmark "unread" "read"))))
11541
11542 (defun gnus-summary-kill-same-subject (&optional unmark)
11543   "Mark articles which has the same subject as read.
11544 If UNMARK is positive, remove any kind of mark.
11545 If UNMARK is negative, tick articles."
11546   (interactive "P")
11547   (gnus-set-global-variables)
11548   (if unmark
11549       (setq unmark (prefix-numeric-value unmark)))
11550   (let ((count
11551          (gnus-summary-mark-same-subject
11552           (gnus-summary-article-subject) unmark)))
11553     ;; If marked as read, go to next unread subject.
11554     (if (null unmark)
11555         ;; Go to next unread subject.
11556         (gnus-summary-next-subject 1 t))
11557     (gnus-message 7 "%d articles are marked as %s"
11558                   count (if unmark "unread" "read"))))
11559
11560 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11561   "Mark articles with same SUBJECT as read, and return marked number.
11562 If optional argument UNMARK is positive, remove any kinds of marks.
11563 If optional argument UNMARK is negative, mark articles as unread instead."
11564   (let ((count 1))
11565     (save-excursion
11566       (cond
11567        ((null unmark)                   ; Mark as read.
11568         (while (and
11569                 (progn
11570                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11571                   (gnus-summary-show-thread) t)
11572                 (gnus-summary-find-subject subject))
11573           (setq count (1+ count))))
11574        ((> unmark 0)                    ; Tick.
11575         (while (and
11576                 (progn
11577                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11578                   (gnus-summary-show-thread) t)
11579                 (gnus-summary-find-subject subject))
11580           (setq count (1+ count))))
11581        (t                               ; Mark as unread.
11582         (while (and
11583                 (progn
11584                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11585                   (gnus-summary-show-thread) t)
11586                 (gnus-summary-find-subject subject))
11587           (setq count (1+ count)))))
11588       (gnus-set-mode-line 'summary)
11589       ;; Return the number of marked articles.
11590       count)))
11591
11592 (defun gnus-summary-mark-as-processable (n &optional unmark)
11593   "Set the process mark on the next N articles.
11594 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11595 the process mark instead.  The difference between N and the actual
11596 number of articles marked is returned."
11597   (interactive "p")
11598   (gnus-set-global-variables)
11599   (let ((backward (< n 0))
11600         (n (abs n)))
11601     (while (and
11602             (> n 0)
11603             (if unmark
11604                 (gnus-summary-remove-process-mark
11605                  (gnus-summary-article-number))
11606               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11607             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11608       (setq n (1- n)))
11609     (if (/= 0 n) (gnus-message 7 "No more articles"))
11610     (gnus-summary-recenter)
11611     (gnus-summary-position-point)
11612     n))
11613
11614 (defun gnus-summary-unmark-as-processable (n)
11615   "Remove the process mark from the next N articles.
11616 If N is negative, mark backward instead.  The difference between N and
11617 the actual number of articles marked is returned."
11618   (interactive "p")
11619   (gnus-set-global-variables)
11620   (gnus-summary-mark-as-processable n t))
11621
11622 (defun gnus-summary-unmark-all-processable ()
11623   "Remove the process mark from all articles."
11624   (interactive)
11625   (gnus-set-global-variables)
11626   (save-excursion
11627     (while gnus-newsgroup-processable
11628       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11629   (gnus-summary-position-point))
11630
11631 (defun gnus-summary-mark-as-expirable (n)
11632   "Mark N articles forward as expirable.
11633 If N is negative, mark backward instead.  The difference between N and
11634 the actual number of articles marked is returned."
11635   (interactive "p")
11636   (gnus-set-global-variables)
11637   (gnus-summary-mark-forward n gnus-expirable-mark))
11638
11639 (defun gnus-summary-mark-article-as-replied (article)
11640   "Mark ARTICLE replied and update the summary line."
11641   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11642   (let ((buffer-read-only nil))
11643     (when (gnus-summary-goto-subject article)
11644       (gnus-summary-update-secondary-mark article))))
11645
11646 (defun gnus-summary-set-bookmark (article)
11647   "Set a bookmark in current article."
11648   (interactive (list (gnus-summary-article-number)))
11649   (gnus-set-global-variables)
11650   (if (or (not (get-buffer gnus-article-buffer))
11651           (not gnus-current-article)
11652           (not gnus-article-current)
11653           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11654       (error "No current article selected"))
11655   ;; Remove old bookmark, if one exists.
11656   (let ((old (assq article gnus-newsgroup-bookmarks)))
11657     (if old (setq gnus-newsgroup-bookmarks
11658                   (delq old gnus-newsgroup-bookmarks))))
11659   ;; Set the new bookmark, which is on the form
11660   ;; (article-number . line-number-in-body).
11661   (setq gnus-newsgroup-bookmarks
11662         (cons
11663          (cons article
11664                (save-excursion
11665                  (set-buffer gnus-article-buffer)
11666                  (count-lines
11667                   (min (point)
11668                        (save-excursion
11669                          (goto-char (point-min))
11670                          (search-forward "\n\n" nil t)
11671                          (point)))
11672                   (point))))
11673          gnus-newsgroup-bookmarks))
11674   (gnus-message 6 "A bookmark has been added to the current article."))
11675
11676 (defun gnus-summary-remove-bookmark (article)
11677   "Remove the bookmark from the current article."
11678   (interactive (list (gnus-summary-article-number)))
11679   (gnus-set-global-variables)
11680   ;; Remove old bookmark, if one exists.
11681   (let ((old (assq article gnus-newsgroup-bookmarks)))
11682     (if old
11683         (progn
11684           (setq gnus-newsgroup-bookmarks
11685                 (delq old gnus-newsgroup-bookmarks))
11686           (gnus-message 6 "Removed bookmark."))
11687       (gnus-message 6 "No bookmark in current article."))))
11688
11689 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11690 (defun gnus-summary-mark-as-dormant (n)
11691   "Mark N articles forward as dormant.
11692 If N is negative, mark backward instead.  The difference between N and
11693 the actual number of articles marked is returned."
11694   (interactive "p")
11695   (gnus-set-global-variables)
11696   (gnus-summary-mark-forward n gnus-dormant-mark))
11697
11698 (defun gnus-summary-set-process-mark (article)
11699   "Set the process mark on ARTICLE and update the summary line."
11700   (setq gnus-newsgroup-processable
11701         (cons article
11702               (delq article gnus-newsgroup-processable)))
11703   (when (gnus-summary-goto-subject article)
11704     (gnus-summary-show-thread)
11705     (gnus-summary-update-secondary-mark article)))
11706
11707 (defun gnus-summary-remove-process-mark (article)
11708   "Remove the process mark from ARTICLE and update the summary line."
11709   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11710   (when (gnus-summary-goto-subject article)
11711     (gnus-summary-show-thread)
11712     (gnus-summary-update-secondary-mark article)))
11713
11714 (defun gnus-summary-set-saved-mark (article)
11715   "Set the process mark on ARTICLE and update the summary line."
11716   (push article gnus-newsgroup-saved)
11717   (when (gnus-summary-goto-subject article)
11718     (gnus-summary-update-secondary-mark article)))
11719
11720 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11721   "Mark N articles as read forwards.
11722 If N is negative, mark backwards instead.
11723 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11724 marked as unread.
11725 The difference between N and the actual number of articles marked is
11726 returned."
11727   (interactive "p")
11728   (gnus-set-global-variables)
11729   (let ((backward (< n 0))
11730         (gnus-summary-goto-unread
11731          (and gnus-summary-goto-unread
11732               (not (eq gnus-summary-goto-unread 'never))
11733               (not (memq mark (list gnus-unread-mark
11734                                     gnus-ticked-mark gnus-dormant-mark)))))
11735         (n (abs n))
11736         (mark (or mark gnus-del-mark)))
11737     (while (and (> n 0)
11738                 (gnus-summary-mark-article nil mark no-expire)
11739                 (zerop (gnus-summary-next-subject
11740                         (if backward -1 1)
11741                         (and gnus-summary-goto-unread
11742                              (not (eq gnus-summary-goto-unread 'never)))
11743                         t)))
11744       (setq n (1- n)))
11745     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11746     (gnus-summary-recenter)
11747     (gnus-summary-position-point)
11748     (gnus-set-mode-line 'summary)
11749     n))
11750
11751 (defun gnus-summary-mark-article-as-read (mark)
11752   "Mark the current article quickly as read with MARK."
11753   (let ((article (gnus-summary-article-number)))
11754     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11755     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11756     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11757     (setq gnus-newsgroup-reads
11758           (cons (cons article mark) gnus-newsgroup-reads))
11759     ;; Possibly remove from cache, if that is used.
11760     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11761     ;; Allow the backend to change the mark.
11762     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11763     ;; Check for auto-expiry.
11764     (when (and gnus-newsgroup-auto-expire
11765                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11766                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11767                    (= mark gnus-ancient-mark)
11768                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11769       (setq mark gnus-expirable-mark)
11770       (push article gnus-newsgroup-expirable))
11771     ;; Set the mark in the buffer.
11772     (gnus-summary-update-mark mark 'unread)
11773     t))
11774
11775 (defun gnus-summary-mark-article-as-unread (mark)
11776   "Mark the current article quickly as unread with MARK."
11777   (let ((article (gnus-summary-article-number)))
11778     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11779     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11780     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11781     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11782     (cond ((= mark gnus-ticked-mark)
11783            (push article gnus-newsgroup-marked))
11784           ((= mark gnus-dormant-mark)
11785            (push article gnus-newsgroup-dormant))
11786           (t
11787            (push article gnus-newsgroup-unreads)))
11788     (setq gnus-newsgroup-reads
11789           (delq (assq article gnus-newsgroup-reads)
11790                 gnus-newsgroup-reads))
11791
11792     ;; See whether the article is to be put in the cache.
11793     (and gnus-use-cache
11794          (vectorp (gnus-summary-article-header article))
11795          (save-excursion
11796            (gnus-cache-possibly-enter-article
11797             gnus-newsgroup-name article
11798             (gnus-summary-article-header article)
11799             (= mark gnus-ticked-mark)
11800             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11801
11802     ;; Fix the mark.
11803     (gnus-summary-update-mark mark 'unread)
11804     t))
11805
11806 (defun gnus-summary-mark-article (&optional article mark no-expire)
11807   "Mark ARTICLE with MARK.  MARK can be any character.
11808 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11809 `??' (dormant) and `?E' (expirable).
11810 If MARK is nil, then the default character `?D' is used.
11811 If ARTICLE is nil, then the article on the current line will be
11812 marked."
11813   ;; The mark might be a string.
11814   (and (stringp mark)
11815        (setq mark (aref mark 0)))
11816   ;; If no mark is given, then we check auto-expiring.
11817   (and (not no-expire)
11818        gnus-newsgroup-auto-expire
11819        (or (not mark)
11820            (and (numberp mark)
11821                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11822                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11823                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11824        (setq mark gnus-expirable-mark))
11825   (let* ((mark (or mark gnus-del-mark))
11826          (article (or article (gnus-summary-article-number))))
11827     (or article (error "No article on current line"))
11828     (if (or (= mark gnus-unread-mark)
11829             (= mark gnus-ticked-mark)
11830             (= mark gnus-dormant-mark))
11831         (gnus-mark-article-as-unread article mark)
11832       (gnus-mark-article-as-read article mark))
11833
11834     ;; See whether the article is to be put in the cache.
11835     (and gnus-use-cache
11836          (not (= mark gnus-canceled-mark))
11837          (vectorp (gnus-summary-article-header article))
11838          (save-excursion
11839            (gnus-cache-possibly-enter-article
11840             gnus-newsgroup-name article
11841             (gnus-summary-article-header article)
11842             (= mark gnus-ticked-mark)
11843             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11844
11845     (if (gnus-summary-goto-subject article nil t)
11846         (let ((buffer-read-only nil))
11847           (gnus-summary-show-thread)
11848           ;; Fix the mark.
11849           (gnus-summary-update-mark mark 'unread)
11850           t))))
11851
11852 (defun gnus-summary-update-secondary-mark (article)
11853   "Update the secondary (read, process, cache) mark."
11854   (gnus-summary-update-mark
11855    (cond ((memq article gnus-newsgroup-processable)
11856           gnus-process-mark)
11857          ((memq article gnus-newsgroup-cached)
11858           gnus-cached-mark)
11859          ((memq article gnus-newsgroup-replied)
11860           gnus-replied-mark)
11861          ((memq article gnus-newsgroup-saved)
11862           gnus-saved-mark)
11863          (t gnus-unread-mark))
11864    'replied)
11865   (when (gnus-visual-p 'summary-highlight 'highlight)
11866     (run-hooks 'gnus-summary-update-hook))
11867   t)
11868
11869 (defun gnus-summary-update-mark (mark type)
11870   (beginning-of-line)
11871   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11872         (buffer-read-only nil))
11873     (when forward
11874       ;; Go to the right position on the line.
11875       (forward-char forward)
11876       ;; Replace the old mark with the new mark.
11877       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11878       ;; Optionally update the marks by some user rule.
11879       (when (eq type 'unread)
11880         (gnus-data-set-mark
11881          (gnus-data-find (gnus-summary-article-number)) mark)
11882         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11883
11884 (defun gnus-mark-article-as-read (article &optional mark)
11885   "Enter ARTICLE in the pertinent lists and remove it from others."
11886   ;; Make the article expirable.
11887   (let ((mark (or mark gnus-del-mark)))
11888     (if (= mark gnus-expirable-mark)
11889         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11890       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11891     ;; Remove from unread and marked lists.
11892     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11893     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11894     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11895     (push (cons article mark) gnus-newsgroup-reads)
11896     ;; Possibly remove from cache, if that is used.
11897     (when gnus-use-cache
11898       (gnus-cache-enter-remove-article article))))
11899
11900 (defun gnus-mark-article-as-unread (article &optional mark)
11901   "Enter ARTICLE in the pertinent lists and remove it from others."
11902   (let ((mark (or mark gnus-ticked-mark)))
11903     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11904     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11905     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11906     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11907     (cond ((= mark gnus-ticked-mark)
11908            (push article gnus-newsgroup-marked))
11909           ((= mark gnus-dormant-mark)
11910            (push article gnus-newsgroup-dormant))
11911           (t
11912            (push article gnus-newsgroup-unreads)))
11913     (setq gnus-newsgroup-reads
11914           (delq (assq article gnus-newsgroup-reads)
11915                 gnus-newsgroup-reads))))
11916
11917 (defalias 'gnus-summary-mark-as-unread-forward
11918   'gnus-summary-tick-article-forward)
11919 (make-obsolete 'gnus-summary-mark-as-unread-forward
11920                'gnus-summary-tick-article-forward)
11921 (defun gnus-summary-tick-article-forward (n)
11922   "Tick N articles forwards.
11923 If N is negative, tick backwards instead.
11924 The difference between N and the number of articles ticked is returned."
11925   (interactive "p")
11926   (gnus-summary-mark-forward n gnus-ticked-mark))
11927
11928 (defalias 'gnus-summary-mark-as-unread-backward
11929   'gnus-summary-tick-article-backward)
11930 (make-obsolete 'gnus-summary-mark-as-unread-backward
11931                'gnus-summary-tick-article-backward)
11932 (defun gnus-summary-tick-article-backward (n)
11933   "Tick N articles backwards.
11934 The difference between N and the number of articles ticked is returned."
11935   (interactive "p")
11936   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11937
11938 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11939 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11940 (defun gnus-summary-tick-article (&optional article clear-mark)
11941   "Mark current article as unread.
11942 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11943 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11944   (interactive)
11945   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11946                                        gnus-ticked-mark)))
11947
11948 (defun gnus-summary-mark-as-read-forward (n)
11949   "Mark N articles as read forwards.
11950 If N is negative, mark backwards instead.
11951 The difference between N and the actual number of articles marked is
11952 returned."
11953   (interactive "p")
11954   (gnus-summary-mark-forward n gnus-del-mark t))
11955
11956 (defun gnus-summary-mark-as-read-backward (n)
11957   "Mark the N articles as read backwards.
11958 The difference between N and the actual number of articles marked is
11959 returned."
11960   (interactive "p")
11961   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11962
11963 (defun gnus-summary-mark-as-read (&optional article mark)
11964   "Mark current article as read.
11965 ARTICLE specifies the article to be marked as read.
11966 MARK specifies a string to be inserted at the beginning of the line."
11967   (gnus-summary-mark-article article mark))
11968
11969 (defun gnus-summary-clear-mark-forward (n)
11970   "Clear marks from N articles forward.
11971 If N is negative, clear backward instead.
11972 The difference between N and the number of marks cleared is returned."
11973   (interactive "p")
11974   (gnus-summary-mark-forward n gnus-unread-mark))
11975
11976 (defun gnus-summary-clear-mark-backward (n)
11977   "Clear marks from N articles backward.
11978 The difference between N and the number of marks cleared is returned."
11979   (interactive "p")
11980   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11981
11982 (defun gnus-summary-mark-unread-as-read ()
11983   "Intended to be used by `gnus-summary-mark-article-hook'."
11984   (when (memq gnus-current-article gnus-newsgroup-unreads)
11985     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11986
11987 (defun gnus-summary-mark-read-and-unread-as-read ()
11988   "Intended to be used by `gnus-summary-mark-article-hook'."
11989   (let ((mark (gnus-summary-article-mark)))
11990     (when (or (gnus-unread-mark-p mark)
11991               (gnus-read-mark-p mark))
11992       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11993
11994 (defun gnus-summary-mark-region-as-read (point mark all)
11995   "Mark all unread articles between point and mark as read.
11996 If given a prefix, mark all articles between point and mark as read,
11997 even ticked and dormant ones."
11998   (interactive "r\nP")
11999   (save-excursion
12000     (let (article)
12001       (goto-char point)
12002       (beginning-of-line)
12003       (while (and
12004               (< (point) mark)
12005               (progn
12006                 (when (or all
12007                           (memq (setq article (gnus-summary-article-number))
12008                                 gnus-newsgroup-unreads))
12009                   (gnus-summary-mark-article article gnus-del-mark))
12010                 t)
12011               (gnus-summary-find-next))))))
12012
12013 (defun gnus-summary-mark-below (score mark)
12014   "Mark articles with score less than SCORE with MARK."
12015   (interactive "P\ncMark: ")
12016   (gnus-set-global-variables)
12017   (setq score (if score
12018                   (prefix-numeric-value score)
12019                 (or gnus-summary-default-score 0)))
12020   (save-excursion
12021     (set-buffer gnus-summary-buffer)
12022     (goto-char (point-min))
12023     (while 
12024         (progn
12025           (and (< (gnus-summary-article-score) score)
12026                (gnus-summary-mark-article nil mark))
12027           (gnus-summary-find-next)))))
12028
12029 (defun gnus-summary-kill-below (&optional score)
12030   "Mark articles with score below SCORE as read."
12031   (interactive "P")
12032   (gnus-set-global-variables)
12033   (gnus-summary-mark-below score gnus-killed-mark))
12034
12035 (defun gnus-summary-clear-above (&optional score)
12036   "Clear all marks from articles with score above SCORE."
12037   (interactive "P")
12038   (gnus-set-global-variables)
12039   (gnus-summary-mark-above score gnus-unread-mark))
12040
12041 (defun gnus-summary-tick-above (&optional score)
12042   "Tick all articles with score above SCORE."
12043   (interactive "P")
12044   (gnus-set-global-variables)
12045   (gnus-summary-mark-above score gnus-ticked-mark))
12046
12047 (defun gnus-summary-mark-above (score mark)
12048   "Mark articles with score over SCORE with MARK."
12049   (interactive "P\ncMark: ")
12050   (gnus-set-global-variables)
12051   (setq score (if score
12052                   (prefix-numeric-value score)
12053                 (or gnus-summary-default-score 0)))
12054   (save-excursion
12055     (set-buffer gnus-summary-buffer)
12056     (goto-char (point-min))
12057     (while (and (progn
12058                   (if (> (gnus-summary-article-score) score)
12059                       (gnus-summary-mark-article nil mark))
12060                   t)
12061                 (gnus-summary-find-next)))))
12062
12063 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12064 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12065 (defun gnus-summary-limit-include-expunged ()
12066   "Display all the hidden articles that were expunged for low scores."
12067   (interactive)
12068   (gnus-set-global-variables)
12069   (let ((buffer-read-only nil))
12070     (let ((scored gnus-newsgroup-scored)
12071           headers h)
12072       (while scored
12073         (or (gnus-summary-goto-subject (caar scored))
12074             (and (setq h (gnus-summary-article-header (caar scored)))
12075                  (< (cdar scored) gnus-summary-expunge-below)
12076                  (setq headers (cons h headers))))
12077         (setq scored (cdr scored)))
12078       (or headers (error "No expunged articles hidden."))
12079       (goto-char (point-min))
12080       (gnus-summary-prepare-unthreaded (nreverse headers)))
12081     (goto-char (point-min))
12082     (gnus-summary-position-point)))
12083
12084 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12085   "Mark all articles not marked as unread in this newsgroup as read.
12086 If prefix argument ALL is non-nil, all articles are marked as read.
12087 If QUIETLY is non-nil, no questions will be asked.
12088 If TO-HERE is non-nil, it should be a point in the buffer.  All
12089 articles before this point will be marked as read.
12090 The number of articles marked as read is returned."
12091   (interactive "P")
12092   (gnus-set-global-variables)
12093   (prog1
12094       (if (or quietly
12095               (not gnus-interactive-catchup) ;Without confirmation?
12096               gnus-expert-user
12097               (gnus-y-or-n-p
12098                (if all
12099                    "Mark absolutely all articles as read? "
12100                  "Mark all unread articles as read? ")))
12101           (if (and not-mark
12102                    (not gnus-newsgroup-adaptive)
12103                    (not gnus-newsgroup-auto-expire))
12104               (progn
12105                 (when all
12106                   (setq gnus-newsgroup-marked nil
12107                         gnus-newsgroup-dormant nil))
12108                 (setq gnus-newsgroup-unreads nil))
12109             ;; We actually mark all articles as canceled, which we
12110             ;; have to do when using auto-expiry or adaptive scoring.
12111             (gnus-summary-show-all-threads)
12112             (if (gnus-summary-first-subject (not all))
12113                 (while (and
12114                         (if to-here (< (point) to-here) t)
12115                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12116                         (gnus-summary-find-next (not all)))))
12117             (unless to-here
12118               (setq gnus-newsgroup-unreads nil))
12119             (gnus-set-mode-line 'summary)))
12120     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12121       (if (and (not to-here) (eq 'nnvirtual (car method)))
12122           (nnvirtual-catchup-group
12123            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12124     (gnus-summary-position-point)))
12125
12126 (defun gnus-summary-catchup-to-here (&optional all)
12127   "Mark all unticked articles before the current one as read.
12128 If ALL is non-nil, also mark ticked and dormant articles as read."
12129   (interactive "P")
12130   (gnus-set-global-variables)
12131   (save-excursion
12132     (let ((beg (point)))
12133       ;; We check that there are unread articles.
12134       (when (or all (gnus-summary-find-prev))
12135         (gnus-summary-catchup all t beg))))
12136   (gnus-summary-position-point))
12137
12138 (defun gnus-summary-catchup-all (&optional quietly)
12139   "Mark all articles in this newsgroup as read."
12140   (interactive "P")
12141   (gnus-set-global-variables)
12142   (gnus-summary-catchup t quietly))
12143
12144 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12145   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12146 If prefix argument ALL is non-nil, all articles are marked as read."
12147   (interactive "P")
12148   (gnus-set-global-variables)
12149   (gnus-summary-catchup all quietly nil 'fast)
12150   ;; Select next newsgroup or exit.
12151   (if (eq gnus-auto-select-next 'quietly)
12152       (gnus-summary-next-group nil)
12153     (gnus-summary-exit)))
12154
12155 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12156   "Mark all articles in this newsgroup as read, and then exit."
12157   (interactive "P")
12158   (gnus-set-global-variables)
12159   (gnus-summary-catchup-and-exit t quietly))
12160
12161 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12162 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12163   "Mark all articles in this group as read and select the next group.
12164 If given a prefix, mark all articles, unread as well as ticked, as
12165 read."
12166   (interactive "P")
12167   (gnus-set-global-variables)
12168   (save-excursion
12169     (gnus-summary-catchup all))
12170   (gnus-summary-next-article t nil nil t))
12171
12172 ;; Thread-based commands.
12173
12174 (defun gnus-summary-articles-in-thread (&optional article)
12175   "Return a list of all articles in the current thread.
12176 If ARTICLE is non-nil, return all articles in the thread that starts
12177 with that article."
12178   (let* ((article (or article (gnus-summary-article-number)))
12179          (data (gnus-data-find-list article))
12180          (top-level (gnus-data-level (car data)))
12181          (top-subject
12182           (cond ((null gnus-thread-operation-ignore-subject)
12183                  (gnus-simplify-subject-re
12184                   (mail-header-subject (gnus-data-header (car data)))))
12185                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12186                  (gnus-simplify-subject-fuzzy
12187                   (mail-header-subject (gnus-data-header (car data)))))
12188                 (t nil)))
12189          articles)
12190     (if (not data)
12191         ()                              ; This article doesn't exist.
12192       (while data
12193         (and (or (not top-subject)
12194                  (string= top-subject
12195                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12196                               (gnus-simplify-subject-fuzzy
12197                                (mail-header-subject
12198                                 (gnus-data-header (car data))))
12199                             (gnus-simplify-subject-re
12200                              (mail-header-subject
12201                               (gnus-data-header (car data)))))))
12202              (setq articles (cons (gnus-data-number (car data)) articles)))
12203         (if (and (setq data (cdr data))
12204                  (> (gnus-data-level (car data)) top-level))
12205             ()
12206           (setq data nil)))
12207       ;; Return the list of articles.
12208       (nreverse articles))))
12209
12210 (defun gnus-summary-rethread-current ()
12211   "Rethread the thread the current article is part of."
12212   (interactive)
12213   (gnus-set-global-variables)
12214   (let* ((gnus-show-threads t)
12215          (article (gnus-summary-article-number))
12216          (id (mail-header-id (gnus-summary-article-header)))
12217          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12218     (unless id
12219       (error "No article on the current line"))
12220     (gnus-rebuild-thread id)
12221     (gnus-summary-goto-subject article)))
12222
12223 (defun gnus-summary-reparent-thread ()
12224   "Make current article child of the marked (or previous) article.
12225
12226 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12227 is non-nil or the Subject: of both articles are the same."
12228   (interactive)
12229   (or (not (gnus-group-read-only-p))
12230       (error "The current newsgroup does not support article editing."))
12231   (or (<= (length gnus-newsgroup-processable) 1)
12232       (error "No more than one article may be marked."))
12233   (save-window-excursion
12234     (let ((gnus-article-buffer " *reparent*")
12235           (current-article (gnus-summary-article-number))
12236           ; first grab the marked article, otherwise one line up.
12237           (parent-article (if (not (null gnus-newsgroup-processable))
12238                               (car gnus-newsgroup-processable)
12239                             (save-excursion
12240                               (if (eq (forward-line -1) 0)
12241                                   (gnus-summary-article-number)
12242                                 (error "Beginning of summary buffer."))))))
12243       (or (not (eq current-article parent-article))
12244           (error "An article may not be self-referential."))
12245       (let ((message-id (mail-header-id 
12246                          (gnus-summary-article-header parent-article))))
12247         (or (and message-id (not (equal message-id "")))
12248             (error "No message-id in desired parent."))
12249         (gnus-summary-select-article t t nil current-article)
12250         (set-buffer gnus-article-buffer)
12251         (setq buffer-read-only nil)
12252         (let ((buf (format "%s" (buffer-string))))
12253           (erase-buffer)
12254           (insert buf))
12255         (goto-char (point-min))
12256         (if (search-forward-regexp "^References: " nil t)
12257             (insert message-id " " )
12258           (insert "References: " message-id "\n"))
12259         (or (gnus-request-replace-article current-article
12260                                           (car gnus-article-current)
12261                                           gnus-article-buffer)
12262             (error "Couldn't replace article."))
12263         (set-buffer gnus-summary-buffer)
12264         (gnus-summary-unmark-all-processable)
12265         (gnus-summary-rethread-current)
12266         (message "Article %d is now the child of article %d."
12267                  current-article parent-article)))))
12268
12269 (defun gnus-summary-toggle-threads (&optional arg)
12270   "Toggle showing conversation threads.
12271 If ARG is positive number, turn showing conversation threads on."
12272   (interactive "P")
12273   (gnus-set-global-variables)
12274   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12275     (setq gnus-show-threads
12276           (if (null arg) (not gnus-show-threads)
12277             (> (prefix-numeric-value arg) 0)))
12278     (gnus-summary-prepare)
12279     (gnus-summary-goto-subject current)
12280     (gnus-summary-position-point)))
12281
12282 (defun gnus-summary-show-all-threads ()
12283   "Show all threads."
12284   (interactive)
12285   (gnus-set-global-variables)
12286   (save-excursion
12287     (let ((buffer-read-only nil))
12288       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12289   (gnus-summary-position-point))
12290
12291 (defun gnus-summary-show-thread ()
12292   "Show thread subtrees.
12293 Returns nil if no thread was there to be shown."
12294   (interactive)
12295   (gnus-set-global-variables)
12296   (let ((buffer-read-only nil)
12297         (orig (point))
12298         ;; first goto end then to beg, to have point at beg after let
12299         (end (progn (end-of-line) (point)))
12300         (beg (progn (beginning-of-line) (point))))
12301     (prog1
12302         ;; Any hidden lines here?
12303         (search-forward "\r" end t)
12304       (subst-char-in-region beg end ?\^M ?\n t)
12305       (goto-char orig)
12306       (gnus-summary-position-point))))
12307
12308 (defun gnus-summary-hide-all-threads ()
12309   "Hide all thread subtrees."
12310   (interactive)
12311   (gnus-set-global-variables)
12312   (save-excursion
12313     (goto-char (point-min))
12314     (gnus-summary-hide-thread)
12315     (while (zerop (gnus-summary-next-thread 1 t))
12316       (gnus-summary-hide-thread)))
12317   (gnus-summary-position-point))
12318
12319 (defun gnus-summary-hide-thread ()
12320   "Hide thread subtrees.
12321 Returns nil if no threads were there to be hidden."
12322   (interactive)
12323   (gnus-set-global-variables)
12324   (let ((buffer-read-only nil)
12325         (start (point))
12326         (article (gnus-summary-article-number)))
12327     (goto-char start)
12328     ;; Go forward until either the buffer ends or the subthread
12329     ;; ends.
12330     (when (and (not (eobp))
12331                (or (zerop (gnus-summary-next-thread 1 t))
12332                    (goto-char (point-max))))
12333       (prog1
12334           (if (and (> (point) start)
12335                    (search-backward "\n" start t))
12336               (progn
12337                 (subst-char-in-region start (point) ?\n ?\^M)
12338                 (gnus-summary-goto-subject article))
12339             (goto-char start)
12340             nil)
12341         ;;(gnus-summary-position-point)
12342         ))))
12343
12344 (defun gnus-summary-go-to-next-thread (&optional previous)
12345   "Go to the same level (or less) next thread.
12346 If PREVIOUS is non-nil, go to previous thread instead.
12347 Return the article number moved to, or nil if moving was impossible."
12348   (if (and (eq gnus-summary-make-false-root 'dummy)
12349            (gnus-summary-article-intangible-p))
12350       (let ((beg (point)))
12351         (while (and (zerop (forward-line 1))
12352                     (not (gnus-summary-article-intangible-p))))
12353         (if (eobp)
12354             (progn
12355               (goto-char beg)
12356               nil)
12357           (point)))
12358     (let* ((level (gnus-summary-thread-level))
12359            (article (gnus-summary-article-number))
12360            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12361            oart)
12362       (while data
12363         (if (<= (gnus-data-level (car data)) level)
12364             (setq oart (gnus-data-number (car data))
12365                   data nil)
12366           (setq data (cdr data))))
12367       (and oart
12368            (gnus-summary-goto-subject oart)))))
12369
12370 (defun gnus-summary-next-thread (n &optional silent)
12371   "Go to the same level next N'th thread.
12372 If N is negative, search backward instead.
12373 Returns the difference between N and the number of skips actually
12374 done.
12375
12376 If SILENT, don't output messages."
12377   (interactive "p")
12378   (gnus-set-global-variables)
12379   (let ((backward (< n 0))
12380         (n (abs n))
12381         old dum int)
12382     (while (and (> n 0)
12383                 (setq old (save-excursion 
12384                             (forward-line 1) 
12385                             (setq int (gnus-summary-article-intangible-p))
12386                             (point)))
12387                 (or int 
12388                     (gnus-summary-go-to-next-thread backward)))
12389       (when (and (eq gnus-summary-make-false-root 'dummy)
12390                  (setq dum (text-property-not-all
12391                             old (point) 'gnus-intangible nil)))
12392         (goto-char dum))
12393       (decf n))
12394     (unless silent 
12395       (gnus-summary-position-point))
12396     (when (and (not silent) (/= 0 n))
12397       (gnus-message 7 "No more threads"))
12398     n))
12399
12400 (defun gnus-summary-prev-thread (n)
12401   "Go to the same level previous N'th thread.
12402 Returns the difference between N and the number of skips actually
12403 done."
12404   (interactive "p")
12405   (gnus-set-global-variables)
12406   (gnus-summary-next-thread (- n)))
12407
12408 (defun gnus-summary-go-down-thread ()
12409   "Go down one level in the current thread."
12410   (let ((children (gnus-summary-article-children)))
12411     (and children
12412          (gnus-summary-goto-subject (car children)))))
12413
12414 (defun gnus-summary-go-up-thread ()
12415   "Go up one level in the current thread."
12416   (let ((parent (gnus-summary-article-parent)))
12417     (and parent
12418          (gnus-summary-goto-subject parent))))
12419
12420 (defun gnus-summary-down-thread (n)
12421   "Go down thread N steps.
12422 If N is negative, go up instead.
12423 Returns the difference between N and how many steps down that were
12424 taken."
12425   (interactive "p")
12426   (gnus-set-global-variables)
12427   (let ((up (< n 0))
12428         (n (abs n)))
12429     (while (and (> n 0)
12430                 (if up (gnus-summary-go-up-thread)
12431                   (gnus-summary-go-down-thread)))
12432       (setq n (1- n)))
12433     (gnus-summary-position-point)
12434     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12435     n))
12436
12437 (defun gnus-summary-up-thread (n)
12438   "Go up thread N steps.
12439 If N is negative, go up instead.
12440 Returns the difference between N and how many steps down that were
12441 taken."
12442   (interactive "p")
12443   (gnus-set-global-variables)
12444   (gnus-summary-down-thread (- n)))
12445
12446 (defun gnus-summary-top-thread ()
12447   "Go to the top of the thread."
12448   (interactive)
12449   (gnus-set-global-variables)
12450   (while (gnus-summary-go-up-thread))
12451   (gnus-summary-article-number))
12452
12453 (defun gnus-summary-kill-thread (&optional unmark)
12454   "Mark articles under current thread as read.
12455 If the prefix argument is positive, remove any kinds of marks.
12456 If the prefix argument is negative, tick articles instead."
12457   (interactive "P")
12458   (gnus-set-global-variables)
12459   (if unmark
12460       (setq unmark (prefix-numeric-value unmark)))
12461   (let ((articles (gnus-summary-articles-in-thread)))
12462     (save-excursion
12463       ;; Expand the thread.
12464       (gnus-summary-show-thread)
12465       ;; Mark all the articles.
12466       (while articles
12467         (gnus-summary-goto-subject (car articles))
12468         (cond ((null unmark)
12469                (gnus-summary-mark-article-as-read gnus-killed-mark))
12470               ((> unmark 0)
12471                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12472               (t
12473                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12474         (setq articles (cdr articles))))
12475     ;; Hide killed subtrees.
12476     (and (null unmark)
12477          gnus-thread-hide-killed
12478          (gnus-summary-hide-thread))
12479     ;; If marked as read, go to next unread subject.
12480     (if (null unmark)
12481         ;; Go to next unread subject.
12482         (gnus-summary-next-subject 1 t)))
12483   (gnus-set-mode-line 'summary))
12484
12485 ;; Summary sorting commands
12486
12487 (defun gnus-summary-sort-by-number (&optional reverse)
12488   "Sort summary buffer by article number.
12489 Argument REVERSE means reverse order."
12490   (interactive "P")
12491   (gnus-summary-sort 'number reverse))
12492
12493 (defun gnus-summary-sort-by-author (&optional reverse)
12494   "Sort summary buffer by author name alphabetically.
12495 If case-fold-search is non-nil, case of letters is ignored.
12496 Argument REVERSE means reverse order."
12497   (interactive "P")
12498   (gnus-summary-sort 'author reverse))
12499
12500 (defun gnus-summary-sort-by-subject (&optional reverse)
12501   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12502 If case-fold-search is non-nil, case of letters is ignored.
12503 Argument REVERSE means reverse order."
12504   (interactive "P")
12505   (gnus-summary-sort 'subject reverse))
12506
12507 (defun gnus-summary-sort-by-date (&optional reverse)
12508   "Sort summary buffer by date.
12509 Argument REVERSE means reverse order."
12510   (interactive "P")
12511   (gnus-summary-sort 'date reverse))
12512
12513 (defun gnus-summary-sort-by-score (&optional reverse)
12514   "Sort summary buffer by score.
12515 Argument REVERSE means reverse order."
12516   (interactive "P")
12517   (gnus-summary-sort 'score reverse))
12518
12519 (defun gnus-summary-sort (predicate reverse)
12520   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12521   (gnus-set-global-variables)
12522   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12523          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12524          (gnus-thread-sort-functions
12525           (list
12526            (if (not reverse)
12527                thread
12528              `(lambda (t1 t2)
12529                 (,thread t2 t1)))))
12530          (gnus-article-sort-functions
12531           (list
12532            (if (not reverse)
12533                article
12534              `(lambda (t1 t2)
12535                 (,article t2 t1)))))
12536          (buffer-read-only)
12537          (gnus-summary-prepare-hook nil))
12538     ;; We do the sorting by regenerating the threads.
12539     (gnus-summary-prepare)
12540     ;; Hide subthreads if needed.
12541     (when (and gnus-show-threads gnus-thread-hide-subtree)
12542       (gnus-summary-hide-all-threads)))
12543   ;; If in async mode, we send some info to the backend.
12544   (when gnus-newsgroup-async
12545     (gnus-request-asynchronous
12546      gnus-newsgroup-name gnus-newsgroup-data)))
12547
12548 (defun gnus-sortable-date (date)
12549   "Make sortable string by string-lessp from DATE.
12550 Timezone package is used."
12551   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12552          (year (aref date 0))
12553          (month (aref date 1))
12554          (day (aref date 2)))
12555     (timezone-make-sortable-date
12556      year month day
12557      (timezone-make-time-string
12558       (aref date 3) (aref date 4) (aref date 5)))))
12559
12560 ;; Summary saving commands.
12561
12562 (defun gnus-summary-save-article (&optional n not-saved)
12563   "Save the current article using the default saver function.
12564 If N is a positive number, save the N next articles.
12565 If N is a negative number, save the N previous articles.
12566 If N is nil and any articles have been marked with the process mark,
12567 save those articles instead.
12568 The variable `gnus-default-article-saver' specifies the saver function."
12569   (interactive "P")
12570   (gnus-set-global-variables)
12571   (let ((articles (gnus-summary-work-articles n))
12572         file header article)
12573     (while articles
12574       (setq header (gnus-summary-article-header
12575                     (setq article (pop articles))))
12576       (if (not (vectorp header))
12577           ;; This is a pseudo-article.
12578           (if (assq 'name header)
12579               (gnus-copy-file (cdr (assq 'name header)))
12580             (gnus-message 1 "Article %d is unsaveable" article))
12581         ;; This is a real article.
12582         (save-window-excursion
12583           (gnus-summary-select-article t nil nil article))
12584         (unless gnus-save-all-headers
12585           ;; Remove headers accoring to `gnus-saved-headers'.
12586           (let ((gnus-visible-headers
12587                  (or gnus-saved-headers gnus-visible-headers)))
12588             (gnus-article-hide-headers nil t)))
12589         ;; Remove any X-Gnus lines.
12590         (save-excursion
12591           (set-buffer gnus-article-buffer)
12592           (save-restriction
12593             (let ((buffer-read-only nil))
12594               (nnheader-narrow-to-headers)
12595               (while (re-search-forward "^X-Gnus" nil t)
12596                 (gnus-delete-line)))))
12597         (save-window-excursion
12598           (if (not gnus-default-article-saver)
12599               (error "No default saver is defined.")
12600             (setq file (funcall
12601                         gnus-default-article-saver
12602                         (cond
12603                          ((not gnus-prompt-before-saving)
12604                           'default)
12605                          ((eq gnus-prompt-before-saving 'always)
12606                           nil)
12607                          (t file))))))
12608         (gnus-summary-remove-process-mark article)
12609         (unless not-saved
12610           (gnus-summary-set-saved-mark article))))
12611     (gnus-summary-position-point)
12612     n))
12613
12614 (defun gnus-summary-pipe-output (&optional arg)
12615   "Pipe the current article to a subprocess.
12616 If N is a positive number, pipe the N next articles.
12617 If N is a negative number, pipe the N previous articles.
12618 If N is nil and any articles have been marked with the process mark,
12619 pipe those articles instead."
12620   (interactive "P")
12621   (gnus-set-global-variables)
12622   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12623     (gnus-summary-save-article arg t))
12624   (gnus-configure-windows 'pipe))
12625
12626 (defun gnus-summary-save-article-mail (&optional arg)
12627   "Append the current article to an mail file.
12628 If N is a positive number, save the N next articles.
12629 If N is a negative number, save the N previous articles.
12630 If N is nil and any articles have been marked with the process mark,
12631 save those articles instead."
12632   (interactive "P")
12633   (gnus-set-global-variables)
12634   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12635     (gnus-summary-save-article arg)))
12636
12637 (defun gnus-summary-save-article-rmail (&optional arg)
12638   "Append the current article to an rmail file.
12639 If N is a positive number, save the N next articles.
12640 If N is a negative number, save the N previous articles.
12641 If N is nil and any articles have been marked with the process mark,
12642 save those articles instead."
12643   (interactive "P")
12644   (gnus-set-global-variables)
12645   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12646     (gnus-summary-save-article arg)))
12647
12648 (defun gnus-summary-save-article-file (&optional arg)
12649   "Append the current article to a file.
12650 If N is a positive number, save the N next articles.
12651 If N is a negative number, save the N previous articles.
12652 If N is nil and any articles have been marked with the process mark,
12653 save those articles instead."
12654   (interactive "P")
12655   (gnus-set-global-variables)
12656   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12657     (gnus-summary-save-article arg)))
12658
12659 (defun gnus-summary-save-article-body-file (&optional arg)
12660   "Append the current article body to a file.
12661 If N is a positive number, save the N next articles.
12662 If N is a negative number, save the N previous articles.
12663 If N is nil and any articles have been marked with the process mark,
12664 save those articles instead."
12665   (interactive "P")
12666   (gnus-set-global-variables)
12667   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12668     (gnus-summary-save-article arg)))
12669
12670 (defun gnus-get-split-value (methods)
12671   "Return a value based on the split METHODS."
12672   (let (split-name method result match)
12673     (when methods
12674       (save-excursion
12675         (set-buffer gnus-original-article-buffer)
12676         (save-restriction
12677           (nnheader-narrow-to-headers)
12678           (while methods
12679             (goto-char (point-min))
12680             (setq method (pop methods))
12681             (setq match (car method))
12682             (when (cond
12683                    ((stringp match)
12684                     ;; Regular expression.
12685                     (condition-case ()
12686                         (re-search-forward match nil t)
12687                       (error nil)))
12688                    ((gnus-functionp match)
12689                     ;; Function.
12690                     (save-restriction
12691                       (widen)
12692                       (setq result (funcall match gnus-newsgroup-name))))
12693                    ((consp match)
12694                     ;; Form.
12695                     (save-restriction
12696                       (widen)
12697                       (setq result (eval match)))))
12698               (setq split-name (append (cdr method) split-name))
12699               (cond ((stringp result)
12700                      (push result split-name))
12701                     ((consp result)
12702                      (setq split-name (append result split-name)))))))))
12703     split-name))
12704
12705 (defun gnus-read-move-group-name (prompt default articles prefix)
12706   "Read a group name."
12707   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12708          (prom
12709           (format "Where do you want to %s %s? "
12710                   prompt
12711                   (if (> (length articles) 1)
12712                       (format "these %d articles" (length articles))
12713                     "this article")))
12714          (to-newsgroup
12715           (cond
12716            ((null split-name)
12717             (completing-read
12718              (concat prom
12719                      (if default
12720                          (format "(default %s) " default)
12721                        ""))
12722              gnus-active-hashtb nil nil prefix))
12723            ((= 1 (length split-name))
12724             (completing-read prom gnus-active-hashtb
12725                              nil nil (cons (car split-name) 0)))
12726            (t
12727             (completing-read
12728              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12729
12730     (when to-newsgroup
12731       (if (or (string= to-newsgroup "")
12732               (string= to-newsgroup prefix))
12733           (setq to-newsgroup (or default "")))
12734       (or (gnus-active to-newsgroup)
12735           (gnus-activate-group to-newsgroup)
12736           (error "No such group: %s" to-newsgroup)))
12737     to-newsgroup))
12738
12739 (defun gnus-read-save-file-name (prompt default-name)
12740   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12741          (file
12742           ;; Let the split methods have their say.
12743           (cond
12744            ;; No split name was found.
12745            ((null split-name)
12746             (read-file-name
12747              (concat prompt " (default "
12748                      (file-name-nondirectory default-name) ") ")
12749              (file-name-directory default-name)
12750              default-name))
12751            ;; A single split name was found
12752            ((= 1 (length split-name))
12753             (read-file-name
12754              (concat prompt " (default " (car split-name) ") ")
12755              gnus-article-save-directory
12756              (concat gnus-article-save-directory (car split-name))))
12757            ;; A list of splits was found.
12758            (t
12759             (setq split-name (mapcar (lambda (el) (list el))
12760                                      (nreverse split-name)))
12761             (let ((result (completing-read
12762                            (concat prompt " ") split-name nil nil)))
12763               (concat gnus-article-save-directory
12764                       (if (string= result "")
12765                           (caar split-name)
12766                         result)))))))
12767     ;; If we have read a directory, we append the default file name.
12768     (when (file-directory-p file)
12769       (setq file (concat (file-name-as-directory file)
12770                          (file-name-nondirectory default-name))))
12771     ;; Possibly translate some charaters.
12772     (nnheader-translate-file-chars file)))
12773
12774 (defun gnus-article-archive-name (group)
12775   "Return the first instance of an \"Archive-name\" in the current buffer."
12776   (let ((case-fold-search t))
12777     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12778       (match-string 1))))
12779
12780 (defun gnus-summary-save-in-rmail (&optional filename)
12781   "Append this article to Rmail file.
12782 Optional argument FILENAME specifies file name.
12783 Directory to save to is default to `gnus-article-save-directory' which
12784 is initialized from the SAVEDIR environment variable."
12785   (interactive)
12786   (gnus-set-global-variables)
12787   (let ((default-name
12788           (funcall gnus-rmail-save-name gnus-newsgroup-name
12789                    gnus-current-headers gnus-newsgroup-last-rmail)))
12790     (setq filename
12791           (cond ((eq filename 'default)
12792                  default-name)
12793                 (filename filename)
12794                 (t (gnus-read-save-file-name
12795                     "Save in rmail file:" default-name))))
12796     (gnus-make-directory (file-name-directory filename))
12797     (gnus-eval-in-buffer-window
12798      gnus-original-article-buffer
12799      (save-excursion
12800        (save-restriction
12801          (widen)
12802          (gnus-output-to-rmail filename))))
12803     ;; Remember the directory name to save articles
12804     (setq gnus-newsgroup-last-rmail filename)))
12805
12806 (defun gnus-summary-save-in-mail (&optional filename)
12807   "Append this article to Unix mail file.
12808 Optional argument FILENAME specifies file name.
12809 Directory to save to is default to `gnus-article-save-directory' which
12810 is initialized from the SAVEDIR environment variable."
12811   (interactive)
12812   (gnus-set-global-variables)
12813   (let ((default-name
12814           (funcall gnus-mail-save-name gnus-newsgroup-name
12815                    gnus-current-headers gnus-newsgroup-last-mail)))
12816     (setq filename
12817           (cond ((eq filename 'default)
12818                  default-name)
12819                 (filename filename)
12820                 (t (gnus-read-save-file-name
12821                     "Save in Unix mail file:" default-name))))
12822     (setq filename
12823           (expand-file-name filename
12824                             (and default-name
12825                                  (file-name-directory default-name))))
12826     (gnus-make-directory (file-name-directory filename))
12827     (gnus-eval-in-buffer-window
12828      gnus-original-article-buffer
12829      (save-excursion
12830        (save-restriction
12831          (widen)
12832          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12833              (gnus-output-to-rmail filename)
12834            (let ((mail-use-rfc822 t))
12835              (rmail-output filename 1 t t))))))
12836     ;; Remember the directory name to save articles.
12837     (setq gnus-newsgroup-last-mail filename)))
12838
12839 (defun gnus-summary-save-in-file (&optional filename)
12840   "Append this article to file.
12841 Optional argument FILENAME specifies file name.
12842 Directory to save to is default to `gnus-article-save-directory' which
12843 is initialized from the SAVEDIR environment variable."
12844   (interactive)
12845   (gnus-set-global-variables)
12846   (let ((default-name
12847           (funcall gnus-file-save-name gnus-newsgroup-name
12848                    gnus-current-headers gnus-newsgroup-last-file)))
12849     (setq filename
12850           (cond ((eq filename 'default)
12851                  default-name)
12852                 (filename filename)
12853                 (t (gnus-read-save-file-name
12854                     "Save in file:" default-name))))
12855     (gnus-make-directory (file-name-directory filename))
12856     (gnus-eval-in-buffer-window
12857      gnus-original-article-buffer
12858      (save-excursion
12859        (save-restriction
12860          (widen)
12861          (gnus-output-to-file filename))))
12862     ;; Remember the directory name to save articles.
12863     (setq gnus-newsgroup-last-file filename)))
12864
12865 (defun gnus-summary-save-body-in-file (&optional filename)
12866   "Append this article body to a file.
12867 Optional argument FILENAME specifies file name.
12868 The directory to save in defaults to `gnus-article-save-directory' which
12869 is initialized from the SAVEDIR environment variable."
12870   (interactive)
12871   (gnus-set-global-variables)
12872   (let ((default-name
12873           (funcall gnus-file-save-name gnus-newsgroup-name
12874                    gnus-current-headers gnus-newsgroup-last-file)))
12875     (setq filename
12876           (cond ((eq filename 'default)
12877                  default-name)
12878                 (filename filename)
12879                 (t (gnus-read-save-file-name
12880                     "Save body in file:" default-name))))
12881     (gnus-make-directory (file-name-directory filename))
12882     (gnus-eval-in-buffer-window
12883      gnus-article-buffer
12884      (save-excursion
12885        (save-restriction
12886          (widen)
12887          (goto-char (point-min))
12888          (and (search-forward "\n\n" nil t)
12889               (narrow-to-region (point) (point-max)))
12890          (gnus-output-to-file filename))))
12891     ;; Remember the directory name to save articles.
12892     (setq gnus-newsgroup-last-file filename)))
12893
12894 (defun gnus-summary-save-in-pipe (&optional command)
12895   "Pipe this article to subprocess."
12896   (interactive)
12897   (gnus-set-global-variables)
12898   (setq command
12899         (cond ((eq command 'default)
12900                gnus-last-shell-command)
12901               (command command)
12902               (t (read-string "Shell command on article: "
12903                               gnus-last-shell-command))))
12904   (if (string-equal command "")
12905       (setq command gnus-last-shell-command))
12906   (gnus-eval-in-buffer-window
12907    gnus-article-buffer
12908    (save-restriction
12909      (widen)
12910      (shell-command-on-region (point-min) (point-max) command nil)))
12911   (setq gnus-last-shell-command command))
12912
12913 ;; Summary extract commands
12914
12915 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12916   (let ((buffer-read-only nil)
12917         (article (gnus-summary-article-number))
12918         after-article b e)
12919     (or (gnus-summary-goto-subject article)
12920         (error (format "No such article: %d" article)))
12921     (gnus-summary-position-point)
12922     ;; If all commands are to be bunched up on one line, we collect
12923     ;; them here.
12924     (if gnus-view-pseudos-separately
12925         ()
12926       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12927             files action)
12928         (while ps
12929           (setq action (cdr (assq 'action (car ps))))
12930           (setq files (list (cdr (assq 'name (car ps)))))
12931           (while (and ps (cdr ps)
12932                       (string= (or action "1")
12933                                (or (cdr (assq 'action (cadr ps))) "2")))
12934             (setq files (cons (cdr (assq 'name (cadr ps))) files))
12935             (setcdr ps (cddr ps)))
12936           (if (not files)
12937               ()
12938             (if (not (string-match "%s" action))
12939                 (setq files (cons " " files)))
12940             (setq files (cons " " files))
12941             (and (assq 'execute (car ps))
12942                  (setcdr (assq 'execute (car ps))
12943                          (funcall (if (string-match "%s" action)
12944                                       'format 'concat)
12945                                   action
12946                                   (mapconcat (lambda (f) f) files " ")))))
12947           (setq ps (cdr ps)))))
12948     (if (and gnus-view-pseudos (not not-view))
12949         (while pslist
12950           (and (assq 'execute (car pslist))
12951                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12952                                      (eq gnus-view-pseudos 'not-confirm)))
12953           (setq pslist (cdr pslist)))
12954       (save-excursion
12955         (while pslist
12956           (setq after-article (or (cdr (assq 'article (car pslist)))
12957                                   (gnus-summary-article-number)))
12958           (gnus-summary-goto-subject after-article)
12959           (forward-line 1)
12960           (setq b (point))
12961           (insert "          " (file-name-nondirectory
12962                                 (cdr (assq 'name (car pslist))))
12963                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12964           (setq e (point))
12965           (forward-line -1)             ; back to `b'
12966           (add-text-properties
12967            b e (list 'gnus-number gnus-reffed-article-number
12968                      gnus-mouse-face-prop gnus-mouse-face))
12969           (gnus-data-enter
12970            after-article gnus-reffed-article-number
12971            gnus-unread-mark b (car pslist) 0 (- e b))
12972           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12973           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12974           (setq pslist (cdr pslist)))))))
12975
12976 (defun gnus-pseudos< (p1 p2)
12977   (let ((c1 (cdr (assq 'action p1)))
12978         (c2 (cdr (assq 'action p2))))
12979     (and c1 c2 (string< c1 c2))))
12980
12981 (defun gnus-request-pseudo-article (props)
12982   (cond ((assq 'execute props)
12983          (gnus-execute-command (cdr (assq 'execute props)))))
12984   (let ((gnus-current-article (gnus-summary-article-number)))
12985     (run-hooks 'gnus-mark-article-hook)))
12986
12987 (defun gnus-execute-command (command &optional automatic)
12988   (save-excursion
12989     (gnus-article-setup-buffer)
12990     (set-buffer gnus-article-buffer)
12991     (let ((command (if automatic command (read-string "Command: " command)))
12992           (buffer-read-only nil))
12993       (erase-buffer)
12994       (insert "$ " command "\n\n")
12995       (if gnus-view-pseudo-asynchronously
12996           (start-process "gnus-execute" nil "sh" "-c" command)
12997         (call-process "sh" nil t nil "-c" command)))))
12998
12999 (defun gnus-copy-file (file &optional to)
13000   "Copy FILE to TO."
13001   (interactive
13002    (list (read-file-name "Copy file: " default-directory)
13003          (read-file-name "Copy file to: " default-directory)))
13004   (gnus-set-global-variables)
13005   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13006   (and (file-directory-p to)
13007        (setq to (concat (file-name-as-directory to)
13008                         (file-name-nondirectory file))))
13009   (copy-file file to))
13010
13011 ;; Summary kill commands.
13012
13013 (defun gnus-summary-edit-global-kill (article)
13014   "Edit the \"global\" kill file."
13015   (interactive (list (gnus-summary-article-number)))
13016   (gnus-set-global-variables)
13017   (gnus-group-edit-global-kill article))
13018
13019 (defun gnus-summary-edit-local-kill ()
13020   "Edit a local kill file applied to the current newsgroup."
13021   (interactive)
13022   (gnus-set-global-variables)
13023   (setq gnus-current-headers (gnus-summary-article-header))
13024   (gnus-set-global-variables)
13025   (gnus-group-edit-local-kill
13026    (gnus-summary-article-number) gnus-newsgroup-name))
13027
13028 \f
13029 ;;;
13030 ;;; Gnus article mode
13031 ;;;
13032
13033 (put 'gnus-article-mode 'mode-class 'special)
13034
13035 (if gnus-article-mode-map
13036     nil
13037   (setq gnus-article-mode-map (make-keymap))
13038   (suppress-keymap gnus-article-mode-map)
13039
13040   (gnus-define-keys gnus-article-mode-map
13041     " " gnus-article-goto-next-page
13042     "\177" gnus-article-goto-prev-page
13043     [delete] gnus-article-goto-prev-page
13044     "\C-c^" gnus-article-refer-article
13045     "h" gnus-article-show-summary
13046     "s" gnus-article-show-summary
13047     "\C-c\C-m" gnus-article-mail
13048     "?" gnus-article-describe-briefly
13049     gnus-mouse-2 gnus-article-push-button
13050     "\r" gnus-article-press-button
13051     "\t" gnus-article-next-button
13052     "\M-\t" gnus-article-prev-button
13053     "\C-c\C-b" gnus-bug)
13054
13055   (substitute-key-definition
13056    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13057
13058 (defun gnus-article-mode ()
13059   "Major mode for displaying an article.
13060
13061 All normal editing commands are switched off.
13062
13063 The following commands are available:
13064
13065 \\<gnus-article-mode-map>
13066 \\[gnus-article-next-page]\t Scroll the article one page forwards
13067 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13068 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13069 \\[gnus-article-show-summary]\t Display the summary buffer
13070 \\[gnus-article-mail]\t Send a reply to the address near point
13071 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13072 \\[gnus-info-find-node]\t Go to the Gnus info node"
13073   (interactive)
13074   (when (and menu-bar-mode
13075              (gnus-visual-p 'article-menu 'menu))
13076     (gnus-article-make-menu-bar))
13077   (kill-all-local-variables)
13078   (gnus-simplify-mode-line)
13079   (setq mode-name "Article")
13080   (setq major-mode 'gnus-article-mode)
13081   (make-local-variable 'minor-mode-alist)
13082   (or (assq 'gnus-show-mime minor-mode-alist)
13083       (setq minor-mode-alist
13084             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13085   (use-local-map gnus-article-mode-map)
13086   (make-local-variable 'page-delimiter)
13087   (setq page-delimiter gnus-page-delimiter)
13088   (buffer-disable-undo (current-buffer))
13089   (setq buffer-read-only t)             ;Disable modification
13090   (run-hooks 'gnus-article-mode-hook))
13091
13092 (defun gnus-article-setup-buffer ()
13093   "Initialize the article buffer."
13094   (let* ((name (if gnus-single-article-buffer "*Article*"
13095                  (concat "*Article " gnus-newsgroup-name "*")))
13096          (original
13097           (progn (string-match "\\*Article" name)
13098                  (concat " *Original Article"
13099                          (substring name (match-end 0))))))
13100     (setq gnus-article-buffer name)
13101     (setq gnus-original-article-buffer original)
13102     ;; This might be a variable local to the summary buffer.
13103     (unless gnus-single-article-buffer
13104       (save-excursion
13105         (set-buffer gnus-summary-buffer)
13106         (setq gnus-article-buffer name)
13107         (setq gnus-original-article-buffer original)
13108         (gnus-set-global-variables))
13109       (make-local-variable 'gnus-summary-buffer))
13110     (if (get-buffer name)
13111         (save-excursion
13112           (set-buffer name)
13113           (buffer-disable-undo (current-buffer))
13114           (setq buffer-read-only t)
13115           (gnus-add-current-to-buffer-list)
13116           (or (eq major-mode 'gnus-article-mode)
13117               (gnus-article-mode))
13118           (current-buffer))
13119       (save-excursion
13120         (set-buffer (get-buffer-create name))
13121         (gnus-add-current-to-buffer-list)
13122         (gnus-article-mode)
13123         (current-buffer)))))
13124
13125 ;; Set article window start at LINE, where LINE is the number of lines
13126 ;; from the head of the article.
13127 (defun gnus-article-set-window-start (&optional line)
13128   (set-window-start
13129    (get-buffer-window gnus-article-buffer)
13130    (save-excursion
13131      (set-buffer gnus-article-buffer)
13132      (goto-char (point-min))
13133      (if (not line)
13134          (point-min)
13135        (gnus-message 6 "Moved to bookmark")
13136        (search-forward "\n\n" nil t)
13137        (forward-line line)
13138        (point)))))
13139
13140 (defun gnus-kill-all-overlays ()
13141   "Delete all overlays in the current buffer."
13142   (when (fboundp 'overlay-lists)
13143     (let* ((overlayss (overlay-lists))
13144            (buffer-read-only nil)
13145            (overlays (nconc (car overlayss) (cdr overlayss))))
13146       (while overlays
13147         (delete-overlay (pop overlays))))))
13148
13149 (defun gnus-request-article-this-buffer (article group)
13150   "Get an article and insert it into this buffer."
13151   (prog1
13152       (save-excursion
13153         (erase-buffer)
13154         (gnus-kill-all-overlays)
13155         (setq group (or group gnus-newsgroup-name))
13156
13157         ;; Open server if it has closed.
13158         (gnus-check-server (gnus-find-method-for-group group))
13159
13160         ;; Using `gnus-request-article' directly will insert the article into
13161         ;; `nntp-server-buffer' - so we'll save some time by not having to
13162         ;; copy it from the server buffer into the article buffer.
13163
13164         ;; We only request an article by message-id when we do not have the
13165         ;; headers for it, so we'll have to get those.
13166         (when (stringp article)
13167           (let ((gnus-override-method gnus-refer-article-method))
13168             (gnus-read-header article)))
13169
13170         ;; If the article number is negative, that means that this article
13171         ;; doesn't belong in this newsgroup (possibly), so we find its
13172         ;; message-id and request it by id instead of number.
13173         (when (and (numberp article)
13174                    gnus-summary-buffer
13175                    (buffer-name gnus-summary-buffer))
13176           (save-excursion
13177             (set-buffer gnus-summary-buffer)
13178             (let ((header (gnus-summary-article-header article)))
13179               (if (< article 0)
13180                   (cond 
13181                    ((memq article gnus-newsgroup-sparse)
13182                     ;; This is a sparse gap article.
13183                     (setq article (mail-header-id header)))
13184                    ((vectorp header)
13185                     ;; It's a real article.
13186                     (setq article (mail-header-id header)))
13187                    (t
13188                     ;; It is an extracted pseudo-article.
13189                     (setq article 'pseudo)
13190                     (gnus-request-pseudo-article header))))
13191                 
13192               (let ((method (gnus-find-method-for-group 
13193                              gnus-newsgroup-name)))
13194                 (if (not (eq (car method) 'nneething))
13195                     ()
13196                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13197                                      (mail-header-subject header))))
13198                     (if (file-directory-p dir)
13199                         (progn
13200                           (setq article 'nneething)
13201                           (gnus-group-enter-directory dir)))))))))
13202
13203         (cond
13204          ;; We first check `gnus-original-article-buffer'.
13205          ((and (equal (car gnus-original-article) group)
13206                (eq (cdr gnus-original-article) article)
13207                (get-buffer gnus-original-article-buffer))
13208           (insert-buffer-substring gnus-original-article-buffer)
13209           'article)
13210          ;; Check the backlog.
13211          ((and gnus-keep-backlog
13212                (gnus-backlog-request-article group article (current-buffer)))
13213           'article)
13214          ;; Check the cache.
13215          ((and gnus-use-cache
13216                (numberp article)
13217                (gnus-cache-request-article article group))
13218           'article)
13219          ;; Get the article and put into the article buffer.
13220          ((or (stringp article) (numberp article))
13221           (let ((gnus-override-method
13222                  (and (stringp article) gnus-refer-article-method))
13223                 (buffer-read-only nil))
13224             (erase-buffer)
13225             (gnus-kill-all-overlays)
13226             (if (gnus-request-article article group (current-buffer))
13227                 (progn
13228                   (and gnus-keep-backlog
13229                        (gnus-backlog-enter-article
13230                         group article (current-buffer)))
13231                   'article))))
13232          ;; It was a pseudo.
13233          (t article)))
13234
13235     ;; Take the article from the original article buffer
13236     ;; and place it in the buffer it's supposed to be in.
13237     (setq gnus-original-article (cons group article))
13238     (when (and (get-buffer gnus-article-buffer)
13239                (equal (buffer-name (current-buffer))
13240                       (buffer-name (get-buffer gnus-article-buffer))))
13241       (save-excursion
13242         (if (get-buffer gnus-original-article-buffer)
13243             (set-buffer (get-buffer gnus-original-article-buffer))
13244           (set-buffer (get-buffer-create gnus-original-article-buffer))
13245           (buffer-disable-undo (current-buffer))
13246           (setq major-mode 'gnus-original-article-mode)
13247           (setq buffer-read-only t)
13248           (gnus-add-current-to-buffer-list))
13249         (let (buffer-read-only)
13250           (erase-buffer)
13251           (insert-buffer-substring gnus-article-buffer))))
13252     
13253     ;; Update sparse articles.
13254     (when (memq article gnus-newsgroup-sparse)
13255       (gnus-summary-update-article article))))
13256
13257 (defun gnus-read-header (id)
13258   "Read the headers of article ID and enter them into the Gnus system."
13259   (let ((group gnus-newsgroup-name)
13260         header where)
13261     ;; First we check to see whether the header in question is already
13262     ;; fetched.
13263     (if (stringp id)
13264         ;; This is a Message-ID.
13265         (setq header (gnus-id-to-header id))
13266       ;; This is an article number.
13267       (setq header (gnus-summary-article-header id)))
13268     (if header
13269         ;; We have found the header.
13270         header
13271       ;; We have to really fetch the header to this article.
13272       (when (setq where
13273                   (if (gnus-check-backend-function 'request-head group)
13274                       (gnus-request-head id group)
13275                     (gnus-request-article id group)))
13276         (save-excursion
13277           (set-buffer nntp-server-buffer)
13278           (and (search-forward "\n\n" nil t)
13279                (delete-region (1- (point)) (point-max)))
13280           (goto-char (point-max))
13281           (insert ".\n")
13282           (goto-char (point-min))
13283           (insert "211 "
13284                   (int-to-string
13285                    (cond
13286                     ((numberp id)
13287                      id)
13288                     ((cdr where)
13289                      (cdr where))
13290                     (t
13291                      gnus-reffed-article-number)))
13292                   " Article retrieved.\n"))
13293         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13294             () ; Malformed head.
13295           (if (and (stringp id)
13296                    (not (string= (gnus-group-real-name group)
13297                                  (car where))))
13298               ;; If we fetched by Message-ID and the article came
13299               ;; from a different group, we fudge some bogus article
13300               ;; numbers for this article.
13301               (mail-header-set-number header gnus-reffed-article-number))
13302           (decf gnus-reffed-article-number)
13303           (push header gnus-newsgroup-headers)
13304           (setq gnus-current-headers header)
13305           (push (mail-header-number header) gnus-newsgroup-limit)
13306           header)))))
13307
13308 (defun gnus-article-prepare (article &optional all-headers header)
13309   "Prepare ARTICLE in article mode buffer.
13310 ARTICLE should either be an article number or a Message-ID.
13311 If ARTICLE is an id, HEADER should be the article headers.
13312 If ALL-HEADERS is non-nil, no headers are hidden."
13313   (save-excursion
13314     ;; Make sure we start in a summary buffer.
13315     (unless (eq major-mode 'gnus-summary-mode)
13316       (set-buffer gnus-summary-buffer))
13317     (setq gnus-summary-buffer (current-buffer))
13318     ;; Make sure the connection to the server is alive.
13319     (unless (gnus-server-opened
13320              (gnus-find-method-for-group gnus-newsgroup-name))
13321       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13322       (gnus-request-group gnus-newsgroup-name t))
13323     (let* ((article (if header (mail-header-number header) article))
13324            (summary-buffer (current-buffer))
13325            (internal-hook gnus-article-internal-prepare-hook)
13326            (group gnus-newsgroup-name)
13327            result)
13328       (save-excursion
13329         (gnus-article-setup-buffer)
13330         (set-buffer gnus-article-buffer)
13331         ;; Deactivate active regions.
13332         (when (and (boundp 'transient-mark-mode)
13333                    transient-mark-mode)
13334           (setq mark-active nil))
13335         (if (not (setq result (let ((buffer-read-only nil))
13336                                 (gnus-request-article-this-buffer
13337                                  article group))))
13338             ;; There is no such article.
13339             (save-excursion
13340               (when (and (numberp article)
13341                          (not (memq article gnus-newsgroup-sparse)))
13342                 (setq gnus-article-current
13343                       (cons gnus-newsgroup-name article))
13344                 (set-buffer gnus-summary-buffer)
13345                 (setq gnus-current-article article)
13346                 (gnus-summary-mark-article article gnus-canceled-mark))
13347               (unless (memq article gnus-newsgroup-sparse)
13348                 (gnus-message
13349                  1 "No such article (may have expired or been canceled)")
13350                 (ding)
13351                 nil))
13352           (if (or (eq result 'pseudo) (eq result 'nneething))
13353               (progn
13354                 (save-excursion
13355                   (set-buffer summary-buffer)
13356                   (setq gnus-last-article gnus-current-article
13357                         gnus-newsgroup-history (cons gnus-current-article
13358                                                      gnus-newsgroup-history)
13359                         gnus-current-article 0
13360                         gnus-current-headers nil
13361                         gnus-article-current nil)
13362                   (if (eq result 'nneething)
13363                       (gnus-configure-windows 'summary)
13364                     (gnus-configure-windows 'article))
13365                   (gnus-set-global-variables))
13366                 (gnus-set-mode-line 'article))
13367             ;; The result from the `request' was an actual article -
13368             ;; or at least some text that is now displayed in the
13369             ;; article buffer.
13370             (if (and (numberp article)
13371                      (not (eq article gnus-current-article)))
13372                 ;; Seems like a new article has been selected.
13373                 ;; `gnus-current-article' must be an article number.
13374                 (save-excursion
13375                   (set-buffer summary-buffer)
13376                   (setq gnus-last-article gnus-current-article
13377                         gnus-newsgroup-history (cons gnus-current-article
13378                                                      gnus-newsgroup-history)
13379                         gnus-current-article article
13380                         gnus-current-headers
13381                         (gnus-summary-article-header gnus-current-article)
13382                         gnus-article-current
13383                         (cons gnus-newsgroup-name gnus-current-article))
13384                   (unless (vectorp gnus-current-headers)
13385                     (setq gnus-current-headers nil))
13386                   (gnus-summary-show-thread)
13387                   (run-hooks 'gnus-mark-article-hook)
13388                   (gnus-set-mode-line 'summary)
13389                   (and (gnus-visual-p 'article-highlight 'highlight)
13390                        (run-hooks 'gnus-visual-mark-article-hook))
13391                   ;; Set the global newsgroup variables here.
13392                   ;; Suggested by Jim Sisolak
13393                   ;; <sisolak@trans4.neep.wisc.edu>.
13394                   (gnus-set-global-variables)
13395                   (setq gnus-have-all-headers
13396                         (or all-headers gnus-show-all-headers))
13397                   (and gnus-use-cache
13398                        (vectorp (gnus-summary-article-header article))
13399                        (gnus-cache-possibly-enter-article
13400                         group article
13401                         (gnus-summary-article-header article)
13402                         (memq article gnus-newsgroup-marked)
13403                         (memq article gnus-newsgroup-dormant)
13404                         (memq article gnus-newsgroup-unreads)))))
13405             ;; Hooks for getting information from the article.
13406             ;; This hook must be called before being narrowed.
13407             (let (buffer-read-only)
13408               (run-hooks 'internal-hook)
13409               (run-hooks 'gnus-article-prepare-hook)
13410               ;; Decode MIME message.
13411               (if gnus-show-mime
13412                   (if (or (not gnus-strict-mime)
13413                           (gnus-fetch-field "Mime-Version"))
13414                       (funcall gnus-show-mime-method)
13415                     (funcall gnus-decode-encoded-word-method)))
13416               ;; Perform the article display hooks.
13417               (run-hooks 'gnus-article-display-hook))
13418             ;; Do page break.
13419             (goto-char (point-min))
13420             (and gnus-break-pages (gnus-narrow-to-page))
13421             (gnus-set-mode-line 'article)
13422             (gnus-configure-windows 'article)
13423             (goto-char (point-min))
13424             t))))))
13425
13426 (defun gnus-article-show-all-headers ()
13427   "Show all article headers in article mode buffer."
13428   (save-excursion
13429     (gnus-article-setup-buffer)
13430     (set-buffer gnus-article-buffer)
13431     (let ((buffer-read-only nil))
13432       (remove-text-properties (point-min) (point-max)
13433                               gnus-hidden-properties))))
13434
13435 (defun gnus-article-hide-headers-if-wanted ()
13436   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13437 Provided for backwards compatibility."
13438   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13439       gnus-inhibit-hiding
13440       (gnus-article-hide-headers)))
13441
13442 (defun gnus-article-hide-headers (&optional arg delete)
13443   "Toggle whether to hide unwanted headers and possibly sort them as well.
13444 If given a negative prefix, always show; if given a positive prefix,
13445 always hide."
13446   (interactive "P")
13447   (unless (gnus-article-check-hidden-text 'headers arg)
13448     ;; This function might be inhibited.
13449     (unless gnus-inhibit-hiding
13450       (save-excursion
13451         (set-buffer gnus-article-buffer)
13452         (save-restriction
13453           (let ((buffer-read-only nil)
13454                 (ignored (when (not (stringp gnus-visible-headers))
13455                            (cond ((stringp gnus-ignored-headers)
13456                                   gnus-ignored-headers)
13457                                  ((listp gnus-ignored-headers)
13458                                   (mapconcat 'identity gnus-ignored-headers
13459                                              "\\|")))))
13460                 (visible
13461                  (cond ((stringp gnus-visible-headers)
13462                         gnus-visible-headers)
13463                        ((and gnus-visible-headers
13464                              (listp gnus-visible-headers))
13465                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13466                 want-list beg)
13467             ;; First we narrow to just the headers.
13468             (widen)
13469             (goto-char (point-min))
13470             ;; Hide any "From " lines at the beginning of (mail) articles.
13471             (while (looking-at "From ")
13472               (forward-line 1))
13473             (unless (bobp)
13474               (add-text-properties
13475                (point-min) (point)
13476                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13477             ;; Then treat the rest of the header lines.
13478             (narrow-to-region
13479              (point)
13480              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13481             ;; Then we use the two regular expressions
13482             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13483             ;; select which header lines is to remain visible in the
13484             ;; article buffer.
13485             (goto-char (point-min))
13486             (while (re-search-forward "^[^ \t]*:" nil t)
13487               (beginning-of-line)
13488               ;; We add the headers we want to keep to a list and delete
13489               ;; them from the buffer.
13490               (if (or (and visible (looking-at visible))
13491                       (and ignored (not (looking-at ignored))))
13492                   (progn
13493                     (push (buffer-substring
13494                            (setq beg (point))
13495                            (progn
13496                              (forward-line 1)
13497                              ;; Be sure to get multi-line headers...
13498                              (re-search-forward "^[^ \t]*:" nil t)
13499                              (beginning-of-line)
13500                              (point)))
13501                           want-list)
13502                     (delete-region beg (point)))
13503                 (forward-line 1)))
13504             ;; Sort the headers that we want to display.
13505             (setq want-list (sort want-list 'gnus-article-header-less))
13506             (goto-char (point-min))
13507             (while want-list
13508               (insert (pop want-list)))
13509             ;; We make the unwanted headers invisible.
13510             (if delete
13511                 (delete-region (point-min) (point-max))
13512               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13513               (add-text-properties
13514                (point) (point-max)
13515                (nconc (list 'gnus-type 'headers)
13516                       gnus-hidden-properties)))))))))
13517
13518 (defsubst gnus-article-header-rank (header)
13519   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13520   (let ((list gnus-sorted-header-list)
13521         (i 0))
13522     (while list
13523       (when (string-match (car list) header)
13524         (setq list nil))
13525       (setq list (cdr list))
13526       (incf i))
13527     i))
13528
13529 (defun gnus-article-header-less (h1 h2)
13530   "Say whether string H1 is \"less\" than string H2."
13531   (< (gnus-article-header-rank h1)
13532      (gnus-article-header-rank h2)))
13533
13534 (defun gnus-article-hide-boring-headers (&optional arg)
13535   "Toggle hiding of headers that aren't very interesting.
13536 If given a negative prefix, always show; if given a positive prefix,
13537 always hide."
13538   (interactive "P")
13539   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13540     (save-excursion
13541       (set-buffer gnus-article-buffer)
13542       (save-restriction
13543         (let ((buffer-read-only nil)
13544               (list gnus-boring-article-headers)
13545               (inhibit-point-motion-hooks t)
13546               elem)
13547           (nnheader-narrow-to-headers)
13548           (while list
13549             (setq elem (pop list))
13550             (goto-char (point-min))
13551             (cond
13552              ;; Hide empty headers.
13553              ((eq elem 'empty)
13554               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13555                 (forward-line -1)
13556                 (add-text-properties
13557                  (progn (beginning-of-line) (point))
13558                  (progn 
13559                    (end-of-line)
13560                    (if (re-search-forward "^[^ \t]" nil t)
13561                        (match-beginning 0)
13562                      (point-max)))
13563                  (nconc (list 'gnus-type 'boring-headers)
13564                         gnus-hidden-properties))))
13565              ;; Hide boring Newsgroups header.
13566              ((eq elem 'newsgroups)
13567               (when (equal (mail-fetch-field "newsgroups")
13568                            (gnus-group-real-name gnus-newsgroup-name))
13569                 (gnus-article-hide-header "newsgroups")))
13570              ((eq elem 'followup-to)
13571               (when (equal (mail-fetch-field "followup-to")
13572                            (mail-fetch-field "newsgroups"))
13573                 (gnus-article-hide-header "followup-to")))
13574              ((eq elem 'reply-to)
13575               (let ((from (mail-fetch-field "from"))
13576                     (reply-to (mail-fetch-field "reply-to")))
13577                 (when (and
13578                        from reply-to
13579                        (equal 
13580                         (nth 1 (mail-extract-address-components from))
13581                         (nth 1 (mail-extract-address-components reply-to))))
13582                   (gnus-article-hide-header "reply-to"))))
13583              ((eq elem 'date)
13584               (let ((date (mail-fetch-field "date")))
13585                 (when (and date
13586                            (< (gnus-days-between date (current-time-string))
13587                               4))
13588                   (gnus-article-hide-header "date")))))))))))
13589
13590 (defun gnus-article-hide-header (header)
13591   (save-excursion
13592     (goto-char (point-min))
13593     (when (re-search-forward (concat "^" header ":") nil t)
13594       (add-text-properties
13595        (progn (beginning-of-line) (point))
13596        (progn 
13597          (end-of-line)
13598          (if (re-search-forward "^[^ \t]" nil t)
13599              (match-beginning 0)
13600            (point-max)))
13601        (nconc (list 'gnus-type 'boring-headers)
13602               gnus-hidden-properties)))))
13603
13604 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13605 (defun gnus-article-treat-overstrike ()
13606   "Translate overstrikes into bold text."
13607   (interactive)
13608   (save-excursion
13609     (set-buffer gnus-article-buffer)
13610     (let ((buffer-read-only nil))
13611       (while (search-forward "\b" nil t)
13612         (let ((next (following-char))
13613               (previous (char-after (- (point) 2))))
13614           (cond ((eq next previous)
13615                  (put-text-property (- (point) 2) (point) 'invisible t)
13616                  (put-text-property (point) (1+ (point)) 'face 'bold))
13617                 ((eq next ?_)
13618                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13619                  (put-text-property
13620                   (- (point) 2) (1- (point)) 'face 'underline))
13621                 ((eq previous ?_)
13622                  (put-text-property (- (point) 2) (point) 'invisible t)
13623                  (put-text-property
13624                   (point) (1+ (point))  'face 'underline))))))))
13625
13626 (defun gnus-article-word-wrap ()
13627   "Format too long lines."
13628   (interactive)
13629   (save-excursion
13630     (set-buffer gnus-article-buffer)
13631     (let ((buffer-read-only nil))
13632       (widen)
13633       (goto-char (point-min))
13634       (search-forward "\n\n" nil t)
13635       (end-of-line 1)
13636       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13637             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13638             (adaptive-fill-mode t))
13639         (while (not (eobp))
13640           (and (>= (current-column) (min fill-column (window-width)))
13641                (/= (preceding-char) ?:)
13642                (fill-paragraph nil))
13643           (end-of-line 2))))))
13644
13645 (defun gnus-article-remove-cr ()
13646   "Remove carriage returns from an article."
13647   (interactive)
13648   (save-excursion
13649     (set-buffer gnus-article-buffer)
13650     (let ((buffer-read-only nil))
13651       (goto-char (point-min))
13652       (while (search-forward "\r" nil t)
13653         (replace-match "" t t)))))
13654
13655 (defun gnus-article-remove-trailing-blank-lines ()
13656   "Remove all trailing blank lines from the article."
13657   (interactive)
13658   (save-excursion
13659     (set-buffer gnus-article-buffer)
13660     (let ((buffer-read-only nil))
13661       (goto-char (point-max))
13662       (delete-region
13663        (point)
13664        (progn
13665          (while (looking-at "^[ \t]*$")
13666            (forward-line -1))
13667          (forward-line 1)
13668          (point))))))
13669
13670 (defun gnus-article-display-x-face (&optional force)
13671   "Look for an X-Face header and display it if present."
13672   (interactive (list 'force))
13673   (save-excursion
13674     (set-buffer gnus-article-buffer)
13675     ;; Delete the old process, if any.
13676     (when (process-status "gnus-x-face")
13677       (delete-process "gnus-x-face"))
13678     (let ((inhibit-point-motion-hooks t)
13679           (case-fold-search nil)
13680           from)
13681       (save-restriction
13682         (nnheader-narrow-to-headers)
13683         (setq from (mail-fetch-field "from"))
13684         (goto-char (point-min))
13685         (when (and gnus-article-x-face-command
13686                    (or force
13687                        ;; Check whether this face is censored.
13688                        (not gnus-article-x-face-too-ugly)
13689                        (and gnus-article-x-face-too-ugly from
13690                             (not (string-match gnus-article-x-face-too-ugly
13691                                                from))))
13692                    ;; Has to be present.
13693                    (re-search-forward "^X-Face: " nil t))
13694           ;; We now have the area of the buffer where the X-Face is stored.
13695           (let ((beg (point))
13696                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13697             ;; We display the face.
13698             (if (symbolp gnus-article-x-face-command)
13699                 ;; The command is a lisp function, so we call it.
13700                 (if (gnus-functionp gnus-article-x-face-command)
13701                     (funcall gnus-article-x-face-command beg end)
13702                   (error "%s is not a function" gnus-article-x-face-command))
13703               ;; The command is a string, so we interpret the command
13704               ;; as a, well, command, and fork it off.
13705               (let ((process-connection-type nil))
13706                 (process-kill-without-query
13707                  (start-process
13708                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13709                 (process-send-region "gnus-x-face" beg end)
13710                 (process-send-eof "gnus-x-face")))))))))
13711
13712 (defun gnus-headers-decode-quoted-printable ()
13713   "Hack to remove QP encoding from headers."
13714   (let ((case-fold-search t)
13715         (inhibit-point-motion-hooks t)
13716         string)
13717     (goto-char (point-min))
13718     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13719       (setq string (match-string 1))
13720       (narrow-to-region (match-beginning 0) (match-end 0))
13721       (delete-region (point-min) (point-max))
13722       (insert string)
13723       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13724       (subst-char-in-region (point-min) (point-max) ?_ ? )
13725       (widen)
13726       (goto-char (point-min)))))
13727
13728 (defun gnus-article-de-quoted-unreadable (&optional force)
13729   "Do a naive translation of a quoted-printable-encoded article.
13730 This is in no way, shape or form meant as a replacement for real MIME
13731 processing, but is simply a stop-gap measure until MIME support is
13732 written.
13733 If FORCE, decode the article whether it is marked as quoted-printable
13734 or not."
13735   (interactive (list 'force))
13736   (save-excursion
13737     (set-buffer gnus-article-buffer)
13738     (let ((case-fold-search t)
13739           (buffer-read-only nil)
13740           (type (gnus-fetch-field "content-transfer-encoding")))
13741       (when (or force
13742                 (and type (string-match "quoted-printable" type)))
13743         (gnus-headers-decode-quoted-printable)
13744         (goto-char (point-min))
13745         (search-forward "\n\n" nil 'move)
13746         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13747
13748 (defun gnus-mime-decode-quoted-printable (from to)
13749   "Decode Quoted-Printable in the region between FROM and TO."
13750   (goto-char from)
13751   (while (search-forward "=" to t)
13752     (cond ((eq (following-char) ?\n)
13753            (delete-char -1)
13754            (delete-char 1))
13755           ((looking-at "[0-9A-F][0-9A-F]")
13756            (delete-char -1)
13757            (insert (hexl-hex-string-to-integer
13758                     (buffer-substring (point) (+ 2 (point)))))
13759            (delete-char 2))
13760           ((looking-at "=")
13761            (delete-char 1))
13762           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13763
13764 (defun gnus-article-hide-pgp (&optional arg)
13765   "Toggle hiding of any PGP headers and signatures in the current article.
13766 If given a negative prefix, always show; if given a positive prefix,
13767 always hide."
13768   (interactive "P")
13769   (unless (gnus-article-check-hidden-text 'pgp arg)
13770     (save-excursion
13771       (set-buffer gnus-article-buffer)
13772       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13773             buffer-read-only beg end)
13774         (widen)
13775         (goto-char (point-min))
13776         ;; Hide the "header".
13777         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13778              (add-text-properties (match-beginning 0) (match-end 0) props))
13779         (setq beg (point))
13780         ;; Hide the actual signature.
13781         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13782              (setq end (match-beginning 0))
13783              (add-text-properties
13784               (match-beginning 0)
13785               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13786                   (match-end 0)
13787                 ;; Perhaps we shouldn't hide to the end of the buffer
13788                 ;; if there is no end to the signature?
13789                 (point-max))
13790               props))
13791         ;; Hide "- " PGP quotation markers.
13792         (when (and beg end)
13793           (narrow-to-region beg end)
13794           (goto-char (point-min))
13795           (while (re-search-forward "^- " nil t)
13796             (add-text-properties (match-beginning 0) (match-end 0) props))
13797           (widen))))))
13798
13799 (defun gnus-article-hide-signature (&optional arg)
13800   "Hide the signature in the current article.
13801 If given a negative prefix, always show; if given a positive prefix,
13802 always hide."
13803   (interactive "P")
13804   (unless (gnus-article-check-hidden-text 'signature arg)
13805     (save-excursion
13806       (set-buffer gnus-article-buffer)
13807       (save-restriction
13808         (let ((buffer-read-only nil))
13809           (when (gnus-narrow-to-signature)
13810             (add-text-properties
13811              (point-min) (point-max)
13812              (nconc (list 'gnus-type 'signature)
13813                     gnus-hidden-properties))))))))
13814
13815 (defun gnus-narrow-to-signature ()
13816   "Narrow to the signature."
13817   (widen)
13818   (goto-char (point-max))
13819   (when (re-search-backward gnus-signature-separator nil t)
13820     (forward-line 1)
13821     (when (or (null gnus-signature-limit)
13822               (and (numberp gnus-signature-limit)
13823                    (< (- (point-max) (point)) gnus-signature-limit))
13824               (and (gnus-functionp gnus-signature-limit)
13825                    (funcall gnus-signature-limit))
13826               (and (stringp gnus-signature-limit)
13827                    (not (re-search-forward gnus-signature-limit nil t))))
13828       (narrow-to-region (point) (point-max))
13829       t)))
13830
13831 (defun gnus-article-check-hidden-text (type arg)
13832   "Return nil if hiding is necessary."
13833   (save-excursion
13834     (set-buffer gnus-article-buffer)
13835     (let ((hide (gnus-article-hidden-text-p type)))
13836       (cond ((or (and (null arg) (eq hide 'hidden))
13837                  (and arg (< 0 (prefix-numeric-value arg))))
13838              (gnus-article-show-hidden-text type))
13839             ((eq hide 'shown)
13840              (gnus-article-show-hidden-text type t))
13841             (t nil)))))
13842
13843 (defun gnus-article-hidden-text-p (type)
13844   "Say whether the current buffer contains hidden text of type TYPE."
13845   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13846     (when pos
13847       (if (get-text-property pos 'invisible)
13848           'hidden
13849         'shown))))
13850
13851 (defun gnus-article-hide (&optional arg force)
13852   "Hide all the gruft in the current article.
13853 This means that PGP stuff, signatures, cited text and (some)
13854 headers will be hidden.
13855 If given a prefix, show the hidden text instead."
13856   (interactive (list current-prefix-arg 'force))
13857   (gnus-article-hide-headers arg)
13858   (gnus-article-hide-pgp arg)
13859   (gnus-article-hide-citation-maybe arg force)
13860   (gnus-article-hide-signature arg))
13861
13862 (defun gnus-article-show-hidden-text (type &optional hide)
13863   "Show all hidden text of type TYPE.
13864 If HIDE, hide the text instead."
13865   (save-excursion
13866     (set-buffer gnus-article-buffer)
13867     (let ((buffer-read-only nil)
13868           (inhibit-point-motion-hooks t)
13869           (beg (point-min)))
13870       (while (gnus-goto-char (text-property-any
13871                               beg (point-max) 'gnus-type type))
13872         (setq beg (point))
13873         (forward-char)
13874         (if hide
13875             (add-text-properties beg (point) gnus-hidden-properties)
13876           (remove-text-properties beg (point) gnus-hidden-properties))
13877         (setq beg (point)))
13878       t)))
13879
13880 (defvar gnus-article-time-units
13881   `((year . ,(* 365.25 24 60 60))
13882     (week . ,(* 7 24 60 60))
13883     (day . ,(* 24 60 60))
13884     (hour . ,(* 60 60))
13885     (minute . 60)
13886     (second . 1))
13887   "Mapping from time units to seconds.")
13888
13889 (defun gnus-article-date-ut (&optional type highlight)
13890   "Convert DATE date to universal time in the current article.
13891 If TYPE is `local', convert to local time; if it is `lapsed', output
13892 how much time has lapsed since DATE."
13893   (interactive (list 'ut t))
13894   (let* ((header (or gnus-current-headers
13895                      (gnus-summary-article-header) ""))
13896          (date (and (vectorp header) (mail-header-date header)))
13897          (date-regexp "^Date: \\|^X-Sent: ")
13898          (now (current-time))
13899          (inhibit-point-motion-hooks t))
13900     (when (and date (not (string= date "")))
13901       (save-excursion
13902         (set-buffer gnus-article-buffer)
13903         (save-restriction
13904           (nnheader-narrow-to-headers)
13905           (let ((buffer-read-only nil))
13906             ;; Delete any old Date headers.
13907             (if (zerop (nnheader-remove-header date-regexp t))
13908                 (beginning-of-line)
13909               (goto-char (point-max)))
13910             (insert
13911              (cond
13912               ;; Convert to the local timezone.  We have to slap a
13913               ;; `condition-case' round the calls to the timezone
13914               ;; functions since they aren't particularly resistant to
13915               ;; buggy dates.
13916               ((eq type 'local)
13917                (concat "Date: " (condition-case ()
13918                                     (timezone-make-date-arpa-standard date)
13919                                   (error date))
13920                        "\n"))
13921               ;; Convert to Universal Time.
13922               ((eq type 'ut)
13923                (concat "Date: "
13924                        (condition-case ()
13925                            (timezone-make-date-arpa-standard date nil "UT")
13926                          (error date))
13927                        "\n"))
13928               ;; Get the original date from the article.
13929               ((eq type 'original)
13930                (concat "Date: " date "\n"))
13931               ;; Do an X-Sent lapsed format.
13932               ((eq type 'lapsed)
13933                ;; If the date is seriously mangled, the timezone
13934                ;; functions are liable to bug out, so we condition-case
13935                ;; the entire thing.
13936                (let* ((real-time
13937                        (condition-case ()
13938                            (gnus-time-minus
13939                             (gnus-encode-date
13940                              (timezone-make-date-arpa-standard
13941                               (current-time-string now)
13942                               (current-time-zone now) "UT"))
13943                             (gnus-encode-date
13944                              (timezone-make-date-arpa-standard
13945                               date nil "UT")))
13946                          (error '(0 0))))
13947                       (real-sec (+ (* (float (car real-time)) 65536)
13948                                    (cadr real-time)))
13949                       (sec (abs real-sec))
13950                       num prev)
13951                  (if (zerop sec)
13952                      "X-Sent: Now\n"
13953                    (concat
13954                     "X-Sent: "
13955                     ;; This is a bit convoluted, but basically we go
13956                     ;; through the time units for years, weeks, etc,
13957                     ;; and divide things to see whether that results
13958                     ;; in positive answers.
13959                     (mapconcat
13960                      (lambda (unit)
13961                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13962                            ;; The (remaining) seconds are too few to
13963                            ;; be divided into this time unit.
13964                            ""
13965                          ;; It's big enough, so we output it.
13966                          (setq sec (- sec (* num (cdr unit))))
13967                          (prog1
13968                              (concat (if prev ", " "") (int-to-string
13969                                                         (floor num))
13970                                      " " (symbol-name (car unit))
13971                                      (if (> num 1) "s" ""))
13972                            (setq prev t))))
13973                      gnus-article-time-units "")
13974                     ;; If dates are odd, then it might appear like the
13975                     ;; article was sent in the future.
13976                     (if (> real-sec 0)
13977                         " ago\n"
13978                       " in the future\n")))))
13979               (t
13980                (error "Unknown conversion type: %s" type)))))
13981           ;; Do highlighting.
13982           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13983             (gnus-article-highlight-headers)))))))
13984
13985 (defun gnus-article-date-local (&optional highlight)
13986   "Convert the current article date to the local timezone."
13987   (interactive (list t))
13988   (gnus-article-date-ut 'local highlight))
13989
13990 (defun gnus-article-date-original (&optional highlight)
13991   "Convert the current article date to what it was originally.
13992 This is only useful if you have used some other date conversion
13993 function and want to see what the date was before converting."
13994   (interactive (list t))
13995   (gnus-article-date-ut 'original highlight))
13996
13997 (defun gnus-article-date-lapsed (&optional highlight)
13998   "Convert the current article date to time lapsed since it was sent."
13999   (interactive (list t))
14000   (gnus-article-date-ut 'lapsed highlight))
14001
14002 (defun gnus-article-maybe-highlight ()
14003   "Do some article highlighting if `gnus-visual' is non-nil."
14004   (if (gnus-visual-p 'article-highlight 'highlight)
14005       (gnus-article-highlight-some)))
14006
14007 ;; Article savers.
14008
14009 (defun gnus-output-to-rmail (file-name)
14010   "Append the current article to an Rmail file named FILE-NAME."
14011   (require 'rmail)
14012   ;; Most of these codes are borrowed from rmailout.el.
14013   (setq file-name (expand-file-name file-name))
14014   (setq rmail-default-rmail-file file-name)
14015   (let ((artbuf (current-buffer))
14016         (tmpbuf (get-buffer-create " *Gnus-output*")))
14017     (save-excursion
14018       (or (get-file-buffer file-name)
14019           (file-exists-p file-name)
14020           (if (gnus-yes-or-no-p
14021                (concat "\"" file-name "\" does not exist, create it? "))
14022               (let ((file-buffer (create-file-buffer file-name)))
14023                 (save-excursion
14024                   (set-buffer file-buffer)
14025                   (rmail-insert-rmail-file-header)
14026                   (let ((require-final-newline nil))
14027                     (write-region (point-min) (point-max) file-name t 1)))
14028                 (kill-buffer file-buffer))
14029             (error "Output file does not exist")))
14030       (set-buffer tmpbuf)
14031       (buffer-disable-undo (current-buffer))
14032       (erase-buffer)
14033       (insert-buffer-substring artbuf)
14034       (gnus-convert-article-to-rmail)
14035       ;; Decide whether to append to a file or to an Emacs buffer.
14036       (let ((outbuf (get-file-buffer file-name)))
14037         (if (not outbuf)
14038             (append-to-file (point-min) (point-max) file-name)
14039           ;; File has been visited, in buffer OUTBUF.
14040           (set-buffer outbuf)
14041           (let ((buffer-read-only nil)
14042                 (msg (and (boundp 'rmail-current-message)
14043                           (symbol-value 'rmail-current-message))))
14044             ;; If MSG is non-nil, buffer is in RMAIL mode.
14045             (if msg
14046                 (progn (widen)
14047                        (narrow-to-region (point-max) (point-max))))
14048             (insert-buffer-substring tmpbuf)
14049             (if msg
14050                 (progn
14051                   (goto-char (point-min))
14052                   (widen)
14053                   (search-backward "\^_")
14054                   (narrow-to-region (point) (point-max))
14055                   (goto-char (1+ (point-min)))
14056                   (rmail-count-new-messages t)
14057                   (rmail-show-message msg)))))))
14058     (kill-buffer tmpbuf)))
14059
14060 (defun gnus-output-to-file (file-name)
14061   "Append the current article to a file named FILE-NAME."
14062   (setq file-name (expand-file-name file-name))
14063   (let ((artbuf (current-buffer))
14064         (tmpbuf (get-buffer-create " *Gnus-output*")))
14065     (save-excursion
14066       (set-buffer tmpbuf)
14067       (buffer-disable-undo (current-buffer))
14068       (erase-buffer)
14069       (insert-buffer-substring artbuf)
14070       ;; Append newline at end of the buffer as separator, and then
14071       ;; save it to file.
14072       (goto-char (point-max))
14073       (insert "\n")
14074       (append-to-file (point-min) (point-max) file-name))
14075     (kill-buffer tmpbuf)))
14076
14077 (defun gnus-convert-article-to-rmail ()
14078   "Convert article in current buffer to Rmail message format."
14079   (let ((buffer-read-only nil))
14080     ;; Convert article directly into Babyl format.
14081     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14082     (goto-char (point-min))
14083     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14084     (while (search-forward "\n\^_" nil t) ;single char
14085       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14086     (goto-char (point-max))
14087     (insert "\^_")))
14088
14089 (defun gnus-narrow-to-page (&optional arg)
14090   "Narrow the article buffer to a page.
14091 If given a numerical ARG, move forward ARG pages."
14092   (interactive "P")
14093   (setq arg (if arg (prefix-numeric-value arg) 0))
14094   (save-excursion
14095     (set-buffer gnus-article-buffer)
14096     (goto-char (point-min))
14097     (widen)
14098     (when (gnus-visual-p 'page-marker)
14099       (let ((buffer-read-only nil))
14100         (gnus-remove-text-with-property 'gnus-prev)
14101         (gnus-remove-text-with-property 'gnus-next)))
14102     (when
14103         (cond ((< arg 0)
14104                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14105               ((> arg 0)
14106                (re-search-forward page-delimiter nil 'move arg)))
14107       (goto-char (match-end 0)))
14108     (narrow-to-region
14109      (point)
14110      (if (re-search-forward page-delimiter nil 'move)
14111          (match-beginning 0)
14112        (point)))
14113     (when (and (gnus-visual-p 'page-marker)
14114                (not (= (point-min) 1)))
14115       (save-excursion
14116         (goto-char (point-min))
14117         (gnus-insert-prev-page-button)))
14118     (when (and (gnus-visual-p 'page-marker)
14119                (not (= (1- (point-max)) (buffer-size))))
14120       (save-excursion
14121         (goto-char (point-max))
14122         (gnus-insert-next-page-button)))))
14123
14124 ;; Article mode commands
14125
14126 (defun gnus-article-goto-next-page ()
14127   "Show the next page of the article."
14128   (interactive)
14129   (when (gnus-article-next-page)
14130     (gnus-article-read-summary-keys nil ?n)))
14131
14132 (defun gnus-article-goto-prev-page ()
14133   "Show the next page of the article."
14134   (interactive)
14135   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14136     (gnus-article-prev-page nil)))
14137
14138 (defun gnus-article-next-page (&optional lines)
14139   "Show the next page of the current article.
14140 If end of article, return non-nil.  Otherwise return nil.
14141 Argument LINES specifies lines to be scrolled up."
14142   (interactive "p")
14143   (move-to-window-line -1)
14144   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14145   (if (save-excursion
14146         (end-of-line)
14147         (and (pos-visible-in-window-p)  ;Not continuation line.
14148              (eobp)))
14149       ;; Nothing in this page.
14150       (if (or (not gnus-break-pages)
14151               (save-excursion
14152                 (save-restriction
14153                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14154           t                             ;Nothing more.
14155         (gnus-narrow-to-page 1)         ;Go to next page.
14156         nil)
14157     ;; More in this page.
14158     (condition-case ()
14159         (scroll-up lines)
14160       (end-of-buffer
14161        ;; Long lines may cause an end-of-buffer error.
14162        (goto-char (point-max))))
14163     (move-to-window-line 0)
14164     nil))
14165
14166 (defun gnus-article-prev-page (&optional lines)
14167   "Show previous page of current article.
14168 Argument LINES specifies lines to be scrolled down."
14169   (interactive "p")
14170   (move-to-window-line 0)
14171   (if (and gnus-break-pages
14172            (bobp)
14173            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14174       (progn
14175         (gnus-narrow-to-page -1)        ;Go to previous page.
14176         (goto-char (point-max))
14177         (recenter -1))
14178     (prog1
14179         (condition-case ()
14180             (scroll-down lines)
14181           (error nil))
14182       (move-to-window-line 0))))
14183
14184 (defun gnus-article-refer-article ()
14185   "Read article specified by message-id around point."
14186   (interactive)
14187   (let ((point (point)))
14188     (search-forward ">" nil t)          ;Move point to end of "<....>".
14189     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14190         (let ((message-id (match-string 1)))
14191           (goto-char point)
14192           (set-buffer gnus-summary-buffer)
14193           (gnus-summary-refer-article message-id))
14194       (goto-char (point))
14195       (error "No references around point"))))
14196
14197 (defun gnus-article-show-summary ()
14198   "Reconfigure windows to show summary buffer."
14199   (interactive)
14200   (gnus-configure-windows 'article)
14201   (gnus-summary-goto-subject gnus-current-article))
14202
14203 (defun gnus-article-describe-briefly ()
14204   "Describe article mode commands briefly."
14205   (interactive)
14206   (gnus-message 6
14207                 (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")))
14208
14209 (defun gnus-article-summary-command ()
14210   "Execute the last keystroke in the summary buffer."
14211   (interactive)
14212   (let ((obuf (current-buffer))
14213         (owin (current-window-configuration))
14214         func)
14215     (switch-to-buffer gnus-summary-buffer 'norecord)
14216     (setq func (lookup-key (current-local-map) (this-command-keys)))
14217     (call-interactively func)
14218     (set-buffer obuf)
14219     (set-window-configuration owin)
14220     (set-window-point (get-buffer-window (current-buffer)) (point))))
14221
14222 (defun gnus-article-summary-command-nosave ()
14223   "Execute the last keystroke in the summary buffer."
14224   (interactive)
14225   (let (func)
14226     (pop-to-buffer gnus-summary-buffer 'norecord)
14227     (setq func (lookup-key (current-local-map) (this-command-keys)))
14228     (call-interactively func)))
14229
14230 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14231   "Read a summary buffer key sequence and execute it from the article buffer."
14232   (interactive "P")
14233   (let ((nosaves
14234          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14235            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14236            "=" "^" "\M-^"))
14237         keys)
14238     (save-excursion
14239       (set-buffer gnus-summary-buffer)
14240       (push (or key last-command-event) unread-command-events)
14241       (setq keys (read-key-sequence nil)))
14242     (message "")
14243
14244     (if (member keys nosaves)
14245         (let (func)
14246           (pop-to-buffer gnus-summary-buffer 'norecord)
14247           (if (setq func (lookup-key (current-local-map) keys))
14248               (call-interactively func)
14249             (ding)))
14250       (let ((obuf (current-buffer))
14251             (owin (current-window-configuration))
14252             (opoint (point))
14253             func in-buffer)
14254         (if not-restore-window
14255             (pop-to-buffer gnus-summary-buffer 'norecord)
14256           (switch-to-buffer gnus-summary-buffer 'norecord))
14257         (setq in-buffer (current-buffer))
14258         (if (setq func (lookup-key (current-local-map) keys))
14259             (call-interactively func)
14260           (ding))
14261         (when (eq in-buffer (current-buffer))
14262           (set-buffer obuf)
14263           (unless not-restore-window
14264             (set-window-configuration owin))
14265           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14266
14267 \f
14268 ;;;
14269 ;;; Kill file handling.
14270 ;;;
14271
14272 ;;;###autoload
14273 (defalias 'gnus-batch-kill 'gnus-batch-score)
14274 ;;;###autoload
14275 (defun gnus-batch-score ()
14276   "Run batched scoring.
14277 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14278 Newsgroups is a list of strings in Bnews format.  If you want to score
14279 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14280 score the alt hierarchy, you'd say \"!alt.all\"."
14281   (interactive)
14282   (let* ((yes-and-no
14283           (gnus-newsrc-parse-options
14284            (apply (function concat)
14285                   (mapcar (lambda (g) (concat g " "))
14286                           command-line-args-left))))
14287          (gnus-expert-user t)
14288          (nnmail-spool-file nil)
14289          (gnus-use-dribble-file nil)
14290          (yes (car yes-and-no))
14291          (no (cdr yes-and-no))
14292          group newsrc entry
14293          ;; Disable verbose message.
14294          gnus-novice-user gnus-large-newsgroup)
14295     ;; Eat all arguments.
14296     (setq command-line-args-left nil)
14297     ;; Start Gnus.
14298     (gnus)
14299     ;; Apply kills to specified newsgroups in command line arguments.
14300     (setq newsrc (cdr gnus-newsrc-alist))
14301     (while newsrc
14302       (setq group (caar newsrc))
14303       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14304       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14305                (and (car entry)
14306                     (or (eq (car entry) t)
14307                         (not (zerop (car entry)))))
14308                (if yes (string-match yes group) t)
14309                (or (null no) (not (string-match no group))))
14310           (progn
14311             (gnus-summary-read-group group nil t nil t)
14312             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14313                  (gnus-summary-exit))))
14314       (setq newsrc (cdr newsrc)))
14315     ;; Exit Emacs.
14316     (switch-to-buffer gnus-group-buffer)
14317     (gnus-group-save-newsrc)))
14318
14319 (defun gnus-apply-kill-file ()
14320   "Apply a kill file to the current newsgroup.
14321 Returns the number of articles marked as read."
14322   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14323           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14324       (gnus-apply-kill-file-internal)
14325     0))
14326
14327 (defun gnus-kill-save-kill-buffer ()
14328   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14329     (when (get-file-buffer file)
14330       (save-excursion
14331         (set-buffer (get-file-buffer file))
14332         (and (buffer-modified-p) (save-buffer))
14333         (kill-buffer (current-buffer))))))
14334
14335 (defvar gnus-kill-file-name "KILL"
14336   "Suffix of the kill files.")
14337
14338 (defun gnus-newsgroup-kill-file (newsgroup)
14339   "Return the name of a kill file name for NEWSGROUP.
14340 If NEWSGROUP is nil, return the global kill file name instead."
14341   (cond 
14342    ;; The global KILL file is placed at top of the directory.
14343    ((or (null newsgroup)
14344         (string-equal newsgroup ""))
14345     (expand-file-name gnus-kill-file-name
14346                       (or gnus-kill-files-directory "~/News")))
14347    ;; Append ".KILL" to newsgroup name.
14348    ((gnus-use-long-file-name 'not-kill)
14349     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14350                               "." gnus-kill-file-name)
14351                       (or gnus-kill-files-directory "~/News")))
14352    ;; Place "KILL" under the hierarchical directory.
14353    (t
14354     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14355                               "/" gnus-kill-file-name)
14356                       (or gnus-kill-files-directory "~/News")))))
14357
14358 \f
14359 ;;;
14360 ;;; Dribble file
14361 ;;;
14362
14363 (defvar gnus-dribble-ignore nil)
14364 (defvar gnus-dribble-eval-file nil)
14365
14366 (defun gnus-dribble-file-name ()
14367   "Return the dribble file for the current .newsrc."
14368   (concat
14369    (if gnus-dribble-directory
14370        (concat (file-name-as-directory gnus-dribble-directory)
14371                (file-name-nondirectory gnus-current-startup-file))
14372      gnus-current-startup-file)
14373    "-dribble"))
14374
14375 (defun gnus-dribble-enter (string)
14376   "Enter STRING into the dribble buffer."
14377   (if (and (not gnus-dribble-ignore)
14378            gnus-dribble-buffer
14379            (buffer-name gnus-dribble-buffer))
14380       (let ((obuf (current-buffer)))
14381         (set-buffer gnus-dribble-buffer)
14382         (insert string "\n")
14383         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14384         (set-buffer obuf))))
14385
14386 (defun gnus-dribble-read-file ()
14387   "Read the dribble file from disk."
14388   (let ((dribble-file (gnus-dribble-file-name)))
14389     (save-excursion
14390       (set-buffer (setq gnus-dribble-buffer
14391                         (get-buffer-create
14392                          (file-name-nondirectory dribble-file))))
14393       (gnus-add-current-to-buffer-list)
14394       (erase-buffer)
14395       (setq buffer-file-name dribble-file)
14396       (auto-save-mode t)
14397       (buffer-disable-undo (current-buffer))
14398       (bury-buffer (current-buffer))
14399       (set-buffer-modified-p nil)
14400       (let ((auto (make-auto-save-file-name))
14401             (gnus-dribble-ignore t))
14402         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14403           ;; Load whichever file is newest -- the auto save file
14404           ;; or the "real" file.
14405           (if (file-newer-than-file-p auto dribble-file)
14406               (insert-file-contents auto)
14407             (insert-file-contents dribble-file))
14408           (unless (zerop (buffer-size))
14409             (set-buffer-modified-p t))
14410           ;; Set the file modes to reflect the .newsrc file modes.
14411           (save-buffer)
14412           (when (file-exists-p gnus-current-startup-file)
14413             (set-file-modes dribble-file
14414                             (file-modes gnus-current-startup-file)))
14415           ;; Possibly eval the file later.
14416           (when (gnus-y-or-n-p
14417                  "Auto-save file exists.  Do you want to read it? ")
14418             (setq gnus-dribble-eval-file t)))))))
14419
14420 (defun gnus-dribble-eval-file ()
14421   (if (not gnus-dribble-eval-file)
14422       ()
14423     (setq gnus-dribble-eval-file nil)
14424     (save-excursion
14425       (let ((gnus-dribble-ignore t))
14426         (set-buffer gnus-dribble-buffer)
14427         (eval-buffer (current-buffer))))))
14428
14429 (defun gnus-dribble-delete-file ()
14430   (if (file-exists-p (gnus-dribble-file-name))
14431       (delete-file (gnus-dribble-file-name)))
14432   (if gnus-dribble-buffer
14433       (save-excursion
14434         (set-buffer gnus-dribble-buffer)
14435         (let ((auto (make-auto-save-file-name)))
14436           (if (file-exists-p auto)
14437               (delete-file auto))
14438           (erase-buffer)
14439           (set-buffer-modified-p nil)))))
14440
14441 (defun gnus-dribble-save ()
14442   (if (and gnus-dribble-buffer
14443            (buffer-name gnus-dribble-buffer))
14444       (save-excursion
14445         (set-buffer gnus-dribble-buffer)
14446         (save-buffer))))
14447
14448 (defun gnus-dribble-clear ()
14449   (save-excursion
14450     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14451         (progn
14452           (set-buffer gnus-dribble-buffer)
14453           (erase-buffer)
14454           (set-buffer-modified-p nil)
14455           (setq buffer-saved-size (buffer-size))))))
14456
14457 \f
14458 ;;;
14459 ;;; Server Communication
14460 ;;;
14461
14462 (defun gnus-start-news-server (&optional confirm)
14463   "Open a method for getting news.
14464 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14465   (let (how)
14466     (if gnus-current-select-method
14467         ;; Stream is already opened.
14468         nil
14469       ;; Open NNTP server.
14470       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14471       (if confirm
14472           (progn
14473             ;; Read server name with completion.
14474             (setq gnus-nntp-server
14475                   (completing-read "NNTP server: "
14476                                    (mapcar (lambda (server) (list server))
14477                                            (cons (list gnus-nntp-server)
14478                                                  gnus-secondary-servers))
14479                                    nil nil gnus-nntp-server))))
14480
14481       (if (and gnus-nntp-server
14482                (stringp gnus-nntp-server)
14483                (not (string= gnus-nntp-server "")))
14484           (setq gnus-select-method
14485                 (cond ((or (string= gnus-nntp-server "")
14486                            (string= gnus-nntp-server "::"))
14487                        (list 'nnspool (system-name)))
14488                       ((string-match "^:" gnus-nntp-server)
14489                        (list 'nnmh gnus-nntp-server
14490                              (list 'nnmh-directory
14491                                    (file-name-as-directory
14492                                     (expand-file-name
14493                                      (concat "~/" (substring
14494                                                    gnus-nntp-server 1)))))
14495                              (list 'nnmh-get-new-mail nil)))
14496                       (t
14497                        (list 'nntp gnus-nntp-server)))))
14498
14499       (setq how (car gnus-select-method))
14500       (cond ((eq how 'nnspool)
14501              (require 'nnspool)
14502              (gnus-message 5 "Looking up local news spool..."))
14503             ((eq how 'nnmh)
14504              (require 'nnmh)
14505              (gnus-message 5 "Looking up mh spool..."))
14506             (t
14507              (require 'nntp)))
14508       (setq gnus-current-select-method gnus-select-method)
14509       (run-hooks 'gnus-open-server-hook)
14510       (or
14511        ;; gnus-open-server-hook might have opened it
14512        (gnus-server-opened gnus-select-method)
14513        (gnus-open-server gnus-select-method)
14514        (gnus-y-or-n-p
14515         (format
14516          "%s (%s) open error: '%s'.     Continue? "
14517          (car gnus-select-method) (cadr gnus-select-method)
14518          (gnus-status-message gnus-select-method)))
14519        (progn
14520          (gnus-message 1 "Couldn't open server on %s"
14521                        (nth 1 gnus-select-method))
14522          (ding)
14523          nil)))))
14524
14525 (defun gnus-check-group (group)
14526   "Try to make sure that the server where GROUP exists is alive."
14527   (let ((method (gnus-find-method-for-group group)))
14528     (or (gnus-server-opened method)
14529         (gnus-open-server method))))
14530
14531 (defun gnus-check-server (&optional method silent)
14532   "Check whether the connection to METHOD is down.
14533 If METHOD is nil, use `gnus-select-method'.
14534 If it is down, start it up (again)."
14535   (let ((method (or method gnus-select-method)))
14536     ;; Transform virtual server names into select methods.
14537     (when (stringp method)
14538       (setq method (gnus-server-to-method method)))
14539     (if (gnus-server-opened method)
14540         ;; The stream is already opened.
14541         t
14542       ;; Open the server.
14543       (unless silent
14544         (gnus-message 5 "Opening %s server%s..." (car method)
14545                       (if (equal (nth 1 method) "") ""
14546                         (format " on %s" (nth 1 method)))))
14547       (run-hooks 'gnus-open-server-hook)
14548       (prog1
14549           (gnus-open-server method)
14550         (unless silent
14551           (message ""))))))
14552
14553 (defun gnus-get-function (method function)
14554   "Return a function symbol based on METHOD and FUNCTION."
14555   ;; Translate server names into methods.
14556   (unless method
14557     (error "Attempted use of a nil select method"))
14558   (when (stringp method)
14559     (setq method (gnus-server-to-method method)))
14560   (let ((func (intern (format "%s-%s" (car method) function))))
14561     ;; If the functions isn't bound, we require the backend in
14562     ;; question.
14563     (unless (fboundp func)
14564       (require (car method))
14565       (unless (fboundp func)
14566         ;; This backend doesn't implement this function.
14567         (error "No such function: %s" func)))
14568     func))
14569
14570 \f
14571 ;;;
14572 ;;; Interface functions to the backends.
14573 ;;;
14574
14575 (defun gnus-open-server (method)
14576   "Open a connection to METHOD."
14577   (let ((elem (assoc method gnus-opened-servers)))
14578     ;; If this method was previously denied, we just return nil.
14579     (if (eq (nth 1 elem) 'denied)
14580         (progn
14581           (gnus-message 1 "Denied server")
14582           nil)
14583       ;; Open the server.
14584       (let ((result
14585              (funcall (gnus-get-function method 'open-server)
14586                       (nth 1 method) (nthcdr 2 method))))
14587         ;; If this hasn't been opened before, we add it to the list.
14588         (unless elem
14589           (setq elem (list method nil)
14590                 gnus-opened-servers (cons elem gnus-opened-servers)))
14591         ;; Set the status of this server.
14592         (setcar (cdr elem) (if result 'ok 'denied))
14593         ;; Return the result from the "open" call.
14594         result))))
14595
14596 (defun gnus-close-server (method)
14597   "Close the connection to METHOD."
14598   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14599
14600 (defun gnus-request-list (method)
14601   "Request the active file from METHOD."
14602   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14603
14604 (defun gnus-request-list-newsgroups (method)
14605   "Request the newsgroups file from METHOD."
14606   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14607
14608 (defun gnus-request-newgroups (date method)
14609   "Request all new groups since DATE from METHOD."
14610   (funcall (gnus-get-function method 'request-newgroups)
14611            date (nth 1 method)))
14612
14613 (defun gnus-server-opened (method)
14614   "Check whether a connection to METHOD has been opened."
14615   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14616
14617 (defun gnus-status-message (method)
14618   "Return the status message from METHOD.
14619 If METHOD is a string, it is interpreted as a group name.   The method
14620 this group uses will be queried."
14621   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14622                   method)))
14623     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14624
14625 (defun gnus-request-group (group &optional dont-check method)
14626   "Request GROUP.  If DONT-CHECK, no information is required."
14627   (let ((method (or method (gnus-find-method-for-group group))))
14628     (funcall (gnus-get-function method 'request-group)
14629              (gnus-group-real-name group) (nth 1 method) dont-check)))
14630
14631 (defun gnus-request-asynchronous (group &optional articles)
14632   "Request that GROUP behave asynchronously.
14633 ARTICLES is the `data' of the group."
14634   (let ((method (gnus-find-method-for-group group)))
14635     (funcall (gnus-get-function method 'request-asynchronous)
14636              (gnus-group-real-name group) (nth 1 method) articles)))
14637
14638 (defun gnus-list-active-group (group)
14639   "Request active information on GROUP."
14640   (let ((method (gnus-find-method-for-group group))
14641         (func 'list-active-group))
14642     (when (gnus-check-backend-function func group)
14643       (funcall (gnus-get-function method func)
14644                (gnus-group-real-name group) (nth 1 method)))))
14645
14646 (defun gnus-request-group-description (group)
14647   "Request a description of GROUP."
14648   (let ((method (gnus-find-method-for-group group))
14649         (func 'request-group-description))
14650     (when (gnus-check-backend-function func group)
14651       (funcall (gnus-get-function method func)
14652                (gnus-group-real-name group) (nth 1 method)))))
14653
14654 (defun gnus-close-group (group)
14655   "Request the GROUP be closed."
14656   (let ((method (gnus-find-method-for-group group)))
14657     (funcall (gnus-get-function method 'close-group)
14658              (gnus-group-real-name group) (nth 1 method))))
14659
14660 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14661   "Request headers for ARTICLES in GROUP.
14662 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14663   (let ((method (gnus-find-method-for-group group)))
14664     (if (and gnus-use-cache (numberp (car articles)))
14665         (gnus-cache-retrieve-headers articles group fetch-old)
14666       (funcall (gnus-get-function method 'retrieve-headers)
14667                articles (gnus-group-real-name group) (nth 1 method)
14668                fetch-old))))
14669
14670 (defun gnus-retrieve-groups (groups method)
14671   "Request active information on GROUPS from METHOD."
14672   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14673
14674 (defun gnus-request-type (group &optional article)
14675   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14676   (let ((method (gnus-find-method-for-group group)))
14677     (if (not (gnus-check-backend-function 'request-type (car method)))
14678         'unknown
14679       (funcall (gnus-get-function method 'request-type)
14680                (gnus-group-real-name group) article))))
14681
14682 (defun gnus-request-update-mark (group article mark)
14683   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14684   (let ((method (gnus-find-method-for-group group)))
14685     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14686         mark
14687       (funcall (gnus-get-function method 'request-update-mark)
14688                (gnus-group-real-name group) article mark))))
14689
14690 (defun gnus-request-article (article group &optional buffer)
14691   "Request the ARTICLE in GROUP.
14692 ARTICLE can either be an article number or an article Message-ID.
14693 If BUFFER, insert the article in that group."
14694   (let ((method (gnus-find-method-for-group group)))
14695     (funcall (gnus-get-function method 'request-article)
14696              article (gnus-group-real-name group) (nth 1 method) buffer)))
14697
14698 (defun gnus-request-head (article group)
14699   "Request the head of ARTICLE in GROUP."
14700   (let ((method (gnus-find-method-for-group group)))
14701     (funcall (gnus-get-function method 'request-head)
14702              article (gnus-group-real-name group) (nth 1 method))))
14703
14704 (defun gnus-request-body (article group)
14705   "Request the body of ARTICLE in GROUP."
14706   (let ((method (gnus-find-method-for-group group)))
14707     (funcall (gnus-get-function method 'request-body)
14708              article (gnus-group-real-name group) (nth 1 method))))
14709
14710 (defun gnus-request-post (method)
14711   "Post the current buffer using METHOD."
14712   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14713
14714 (defun gnus-request-scan (group method)
14715   "Request a SCAN being performed in GROUP from METHOD.
14716 If GROUP is nil, all groups on METHOD are scanned."
14717   (let ((method (if group (gnus-find-method-for-group group) method)))
14718     (funcall (gnus-get-function method 'request-scan)
14719              (and group (gnus-group-real-name group)) (nth 1 method))))
14720
14721 (defsubst gnus-request-update-info (info method)
14722   "Request that METHOD update INFO."
14723   (when (gnus-check-backend-function 'request-update-info (car method))
14724     (funcall (gnus-get-function method 'request-update-info)
14725              (gnus-group-real-name (gnus-info-group info))
14726              info (nth 1 method))))
14727
14728 (defun gnus-request-expire-articles (articles group &optional force)
14729   (let ((method (gnus-find-method-for-group group)))
14730     (funcall (gnus-get-function method 'request-expire-articles)
14731              articles (gnus-group-real-name group) (nth 1 method)
14732              force)))
14733
14734 (defun gnus-request-move-article
14735   (article group server accept-function &optional last)
14736   (let ((method (gnus-find-method-for-group group)))
14737     (funcall (gnus-get-function method 'request-move-article)
14738              article (gnus-group-real-name group)
14739              (nth 1 method) accept-function last)))
14740
14741 (defun gnus-request-accept-article (group &optional last method)
14742   ;; Make sure there's a newline at the end of the article.
14743   (goto-char (point-max))
14744   (unless (bolp)
14745     (insert "\n"))
14746   (let ((func (if (symbolp group) group
14747                 (car (or method (gnus-find-method-for-group group))))))
14748     (funcall (intern (format "%s-request-accept-article" func))
14749              (if (stringp group) (gnus-group-real-name group) group)
14750              last)))
14751
14752 (defun gnus-request-replace-article (article group buffer)
14753   (let ((func (car (gnus-find-method-for-group group))))
14754     (funcall (intern (format "%s-request-replace-article" func))
14755              article (gnus-group-real-name group) buffer)))
14756
14757 (defun gnus-request-associate-buffer (group)
14758   (let ((method (gnus-find-method-for-group group)))
14759     (funcall (gnus-get-function method 'request-associate-buffer)
14760              (gnus-group-real-name group))))
14761
14762 (defun gnus-request-restore-buffer (article group)
14763   "Request a new buffer restored to the state of ARTICLE."
14764   (let ((method (gnus-find-method-for-group group)))
14765     (funcall (gnus-get-function method 'request-restore-buffer)
14766              article (gnus-group-real-name group) (nth 1 method))))
14767
14768 (defun gnus-request-create-group (group &optional method)
14769   (let ((method (or method (gnus-find-method-for-group group))))
14770     (funcall (gnus-get-function method 'request-create-group)
14771              (gnus-group-real-name group) (nth 1 method))))
14772
14773 (defun gnus-request-delete-group (group &optional force)
14774   (let ((method (gnus-find-method-for-group group)))
14775     (funcall (gnus-get-function method 'request-delete-group)
14776              (gnus-group-real-name group) force (nth 1 method))))
14777
14778 (defun gnus-request-rename-group (group new-name)
14779   (let ((method (gnus-find-method-for-group group)))
14780     (funcall (gnus-get-function method 'request-rename-group)
14781              (gnus-group-real-name group)
14782              (gnus-group-real-name new-name) (nth 1 method))))
14783
14784 (defun gnus-member-of-valid (symbol group)
14785   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14786   (memq symbol (assoc
14787                 (symbol-name (car (gnus-find-method-for-group group)))
14788                 gnus-valid-select-methods)))
14789
14790 (defun gnus-method-option-p (method option)
14791   "Return non-nil if select METHOD has OPTION as a parameter."
14792   (memq option (assoc (format "%s" (car method))
14793                       gnus-valid-select-methods)))
14794
14795 (defun gnus-server-extend-method (group method)
14796   ;; This function "extends" a virtual server.  If the server is
14797   ;; "hello", and the select method is ("hello" (my-var "something"))
14798   ;; in the group "alt.alt", this will result in a new virtual server
14799   ;; called "hello+alt.alt".
14800   (let ((entry
14801          (gnus-copy-sequence
14802           (if (equal (car method) "native") gnus-select-method
14803             (cdr (assoc (car method) gnus-server-alist))))))
14804     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14805     (nconc entry (cdr method))))
14806
14807 (defun gnus-find-method-for-group (group &optional info)
14808   "Find the select method that GROUP uses."
14809   (or gnus-override-method
14810       (and (not group)
14811            gnus-select-method)
14812       (let ((info (or info (gnus-get-info group)))
14813             method)
14814         (if (or (not info)
14815                 (not (setq method (gnus-info-method info)))
14816                 (equal method "native"))
14817             gnus-select-method
14818           (setq method
14819                 (cond ((stringp method)
14820                        (gnus-server-to-method method))
14821                       ((stringp (car method))
14822                        (gnus-server-extend-method group method))
14823                       (t
14824                        method)))
14825           (if (equal (cadr method) "")
14826               method
14827             (gnus-server-add-address method))))))
14828
14829 (defun gnus-check-backend-function (func group)
14830   "Check whether GROUP supports function FUNC."
14831   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14832                   group)))
14833     (fboundp (intern (format "%s-%s" method func)))))
14834
14835 (defun gnus-methods-using (feature)
14836   "Find all methods that have FEATURE."
14837   (let ((valids gnus-valid-select-methods)
14838         outs)
14839     (while valids
14840       (if (memq feature (car valids))
14841           (setq outs (cons (car valids) outs)))
14842       (setq valids (cdr valids)))
14843     outs))
14844
14845 \f
14846 ;;;
14847 ;;; Active & Newsrc File Handling
14848 ;;;
14849
14850 (defun gnus-setup-news (&optional rawfile level dont-connect)
14851   "Setup news information.
14852 If RAWFILE is non-nil, the .newsrc file will also be read.
14853 If LEVEL is non-nil, the news will be set up at level LEVEL."
14854   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14855
14856     (when init 
14857       ;; Clear some variables to re-initialize news information.
14858       (setq gnus-newsrc-alist nil
14859             gnus-active-hashtb nil)
14860       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14861       (gnus-read-newsrc-file rawfile))
14862
14863     (when (and (not (assoc "archive" gnus-server-alist))
14864                gnus-message-archive-method)
14865       (push (cons "archive" gnus-message-archive-method)
14866             gnus-server-alist))
14867
14868     ;; If we don't read the complete active file, we fill in the
14869     ;; hashtb here.
14870     (if (or (null gnus-read-active-file)
14871             (eq gnus-read-active-file 'some))
14872         (gnus-update-active-hashtb-from-killed))
14873
14874     ;; Read the active file and create `gnus-active-hashtb'.
14875     ;; If `gnus-read-active-file' is nil, then we just create an empty
14876     ;; hash table.  The partial filling out of the hash table will be
14877     ;; done in `gnus-get-unread-articles'.
14878     (and gnus-read-active-file
14879          (not level)
14880          (gnus-read-active-file))
14881
14882     (or gnus-active-hashtb
14883         (setq gnus-active-hashtb (make-vector 4095 0)))
14884
14885     ;; Initialize the cache.
14886     (when gnus-use-cache
14887       (gnus-cache-open))
14888
14889     ;; Possibly eval the dribble file.
14890     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14891
14892     (gnus-update-format-specifications)
14893
14894     ;; See whether we need to read the description file.
14895     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
14896              (not gnus-description-hashtb)
14897              (not dont-connect)
14898              gnus-read-active-file)
14899         (gnus-read-all-descriptions-files))
14900
14901     ;; Find new newsgroups and treat them.
14902     (if (and init gnus-check-new-newsgroups (not level)
14903              (gnus-check-server gnus-select-method))
14904         (gnus-find-new-newsgroups))
14905
14906     ;; Find the number of unread articles in each non-dead group.
14907     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14908       (gnus-get-unread-articles level))
14909
14910     (if (and init gnus-check-bogus-newsgroups
14911              gnus-read-active-file (not level)
14912              (gnus-server-opened gnus-select-method))
14913         (gnus-check-bogus-newsgroups))))
14914
14915 (defun gnus-find-new-newsgroups (&optional arg)
14916   "Search for new newsgroups and add them.
14917 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14918 The `-n' option line from .newsrc is respected.
14919 If ARG (the prefix), use the `ask-server' method to query
14920 the server for new groups."
14921   (interactive "P")
14922   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14923                        (null gnus-read-active-file)
14924                        (eq gnus-read-active-file 'some))
14925                    'ask-server gnus-check-new-newsgroups)))
14926     (unless (gnus-check-first-time-used)
14927       (if (or (consp check)
14928               (eq check 'ask-server))
14929           ;; Ask the server for new groups.
14930           (gnus-ask-server-for-new-groups)
14931         ;; Go through the active hashtb and look for new groups.
14932         (let ((groups 0)
14933               group new-newsgroups)
14934           (gnus-message 5 "Looking for new newsgroups...")
14935           (unless gnus-have-read-active-file
14936             (gnus-read-active-file))
14937           (setq gnus-newsrc-last-checked-date (current-time-string))
14938           (unless gnus-killed-hashtb
14939             (gnus-make-hashtable-from-killed))
14940           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14941           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14942           (mapatoms
14943            (lambda (sym)
14944              (if (or (null (setq group (symbol-name sym)))
14945                      (not (boundp sym))
14946                      (null (symbol-value sym))
14947                      (gnus-gethash group gnus-killed-hashtb)
14948                      (gnus-gethash group gnus-newsrc-hashtb))
14949                  ()
14950                (let ((do-sub (gnus-matches-options-n group)))
14951                  (cond
14952                   ((eq do-sub 'subscribe)
14953                    (setq groups (1+ groups))
14954                    (gnus-sethash group group gnus-killed-hashtb)
14955                    (funcall gnus-subscribe-options-newsgroup-method group))
14956                   ((eq do-sub 'ignore)
14957                    nil)
14958                   (t
14959                    (setq groups (1+ groups))
14960                    (gnus-sethash group group gnus-killed-hashtb)
14961                    (if gnus-subscribe-hierarchical-interactive
14962                        (setq new-newsgroups (cons group new-newsgroups))
14963                      (funcall gnus-subscribe-newsgroup-method group)))))))
14964            gnus-active-hashtb)
14965           (when new-newsgroups
14966             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14967           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14968           (if (> groups 0)
14969               (gnus-message 6 "%d new newsgroup%s arrived."
14970                             groups (if (> groups 1) "s have" " has"))
14971             (gnus-message 6 "No new newsgroups.")))))))
14972
14973 (defun gnus-matches-options-n (group)
14974   ;; Returns `subscribe' if the group is to be unconditionally
14975   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14976   ;; no match for the group.
14977
14978   ;; First we check the two user variables.
14979   (cond
14980    ((and gnus-options-subscribe
14981          (string-match gnus-options-subscribe group))
14982     'subscribe)
14983    ((and gnus-auto-subscribed-groups
14984          (string-match gnus-auto-subscribed-groups group))
14985     'subscribe)
14986    ((and gnus-options-not-subscribe
14987          (string-match gnus-options-not-subscribe group))
14988     'ignore)
14989    ;; Then we go through the list that was retrieved from the .newsrc
14990    ;; file.  This list has elements on the form
14991    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14992    ;; is in the reverse order of the options line) is returned.
14993    (t
14994     (let ((regs gnus-newsrc-options-n))
14995       (while (and regs
14996                   (not (string-match (caar regs) group)))
14997         (setq regs (cdr regs)))
14998       (and regs (cdar regs))))))
14999
15000 (defun gnus-ask-server-for-new-groups ()
15001   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15002          (methods (cons gnus-select-method
15003                         (nconc
15004                          (when gnus-message-archive-method
15005                            (list "archive"))
15006                          (append
15007                           (and (consp gnus-check-new-newsgroups)
15008                                gnus-check-new-newsgroups)
15009                           gnus-secondary-select-methods))))
15010          (groups 0)
15011          (new-date (current-time-string))
15012          group new-newsgroups got-new method hashtb
15013          gnus-override-subscribe-method)
15014     ;; Go through both primary and secondary select methods and
15015     ;; request new newsgroups.
15016     (while (setq method (gnus-server-get-method nil (pop methods)))
15017       (setq new-newsgroups nil)
15018       (setq gnus-override-subscribe-method method)
15019       (when (and (gnus-check-server method)
15020                  (gnus-request-newgroups date method))
15021         (save-excursion
15022           (setq got-new t)
15023           (setq hashtb (gnus-make-hashtable 100))
15024           (set-buffer nntp-server-buffer)
15025           ;; Enter all the new groups into a hashtable.
15026           (gnus-active-to-gnus-format method hashtb 'ignore)))
15027       ;; Now all new groups from `method' are in `hashtb'.
15028       (mapatoms
15029        (lambda (group-sym)
15030          (if (or (null (setq group (symbol-name group-sym)))
15031                  (null (symbol-value group-sym))
15032                  (gnus-gethash group gnus-newsrc-hashtb)
15033                  (member group gnus-zombie-list)
15034                  (member group gnus-killed-list))
15035              ;; The group is already known.
15036              ()
15037            ;; Make this group active.
15038            (when (symbol-value group-sym)
15039              (gnus-set-active group (symbol-value group-sym)))
15040            ;; Check whether we want it or not.
15041            (let ((do-sub (gnus-matches-options-n group)))
15042              (cond
15043               ((eq do-sub 'subscribe)
15044                (incf groups)
15045                (gnus-sethash group group gnus-killed-hashtb)
15046                (funcall gnus-subscribe-options-newsgroup-method group))
15047               ((eq do-sub 'ignore)
15048                nil)
15049               (t
15050                (incf groups)
15051                (gnus-sethash group group gnus-killed-hashtb)
15052                (if gnus-subscribe-hierarchical-interactive
15053                    (push group new-newsgroups)
15054                  (funcall gnus-subscribe-newsgroup-method group)))))))
15055        hashtb)
15056       (when new-newsgroups
15057         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15058     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15059     (when (> groups 0)
15060       (gnus-message 6 "%d new newsgroup%s arrived."
15061                     groups (if (> groups 1) "s have" " has")))
15062     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15063     got-new))
15064
15065 (defun gnus-check-first-time-used ()
15066   (if (or (> (length gnus-newsrc-alist) 1)
15067           (file-exists-p gnus-startup-file)
15068           (file-exists-p (concat gnus-startup-file ".el"))
15069           (file-exists-p (concat gnus-startup-file ".eld")))
15070       nil
15071     (gnus-message 6 "First time user; subscribing you to default groups")
15072     (unless gnus-have-read-active-file
15073       (gnus-read-active-file))
15074     (setq gnus-newsrc-last-checked-date (current-time-string))
15075     (let ((groups gnus-default-subscribed-newsgroups)
15076           group)
15077       (if (eq groups t)
15078           nil
15079         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15080         (mapatoms
15081          (lambda (sym)
15082            (if (null (setq group (symbol-name sym)))
15083                ()
15084              (let ((do-sub (gnus-matches-options-n group)))
15085                (cond
15086                 ((eq do-sub 'subscribe)
15087                  (gnus-sethash group group gnus-killed-hashtb)
15088                  (funcall gnus-subscribe-options-newsgroup-method group))
15089                 ((eq do-sub 'ignore)
15090                  nil)
15091                 (t
15092                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15093          gnus-active-hashtb)
15094         (while groups
15095           (if (gnus-active (car groups))
15096               (gnus-group-change-level
15097                (car groups) gnus-level-default-subscribed gnus-level-killed))
15098           (setq groups (cdr groups)))
15099         (gnus-group-make-help-group)
15100         (and gnus-novice-user
15101              (gnus-message 7 "`A k' to list killed groups"))))))
15102
15103 (defun gnus-subscribe-group (group previous &optional method)
15104   (gnus-group-change-level
15105    (if method
15106        (list t group gnus-level-default-subscribed nil nil method)
15107      group)
15108    gnus-level-default-subscribed gnus-level-killed previous t))
15109
15110 ;; `gnus-group-change-level' is the fundamental function for changing
15111 ;; subscription levels of newsgroups.  This might mean just changing
15112 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15113 ;; again, which subscribes/unsubscribes a group, which is equally
15114 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15115 ;; from 8-9 to 1-7 means that you remove the group from the list of
15116 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15117 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15118 ;; which is trivial.
15119 ;; ENTRY can either be a string (newsgroup name) or a list (if
15120 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15121 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15122 ;; entries.
15123 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15124 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15125 ;; after.
15126 (defun gnus-group-change-level (entry level &optional oldlevel
15127                                       previous fromkilled)
15128   (let (group info active num)
15129     ;; Glean what info we can from the arguments
15130     (if (consp entry)
15131         (if fromkilled (setq group (nth 1 entry))
15132           (setq group (car (nth 2 entry))))
15133       (setq group entry))
15134     (if (and (stringp entry)
15135              oldlevel
15136              (< oldlevel gnus-level-zombie))
15137         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15138     (if (and (not oldlevel)
15139              (consp entry))
15140         (setq oldlevel (gnus-info-level (nth 2 entry)))
15141       (setq oldlevel (or oldlevel 9)))
15142     (if (stringp previous)
15143         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15144
15145     (if (and (>= oldlevel gnus-level-zombie)
15146              (gnus-gethash group gnus-newsrc-hashtb))
15147         ;; We are trying to subscribe a group that is already
15148         ;; subscribed.
15149         ()                              ; Do nothing.
15150
15151       (or (gnus-ephemeral-group-p group)
15152           (gnus-dribble-enter
15153            (format "(gnus-group-change-level %S %S %S %S %S)"
15154                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15155
15156       ;; Then we remove the newgroup from any old structures, if needed.
15157       ;; If the group was killed, we remove it from the killed or zombie
15158       ;; list.  If not, and it is in fact going to be killed, we remove
15159       ;; it from the newsrc hash table and assoc.
15160       (cond
15161        ((>= oldlevel gnus-level-zombie)
15162         (if (= oldlevel gnus-level-zombie)
15163             (setq gnus-zombie-list (delete group gnus-zombie-list))
15164           (setq gnus-killed-list (delete group gnus-killed-list))))
15165        (t
15166         (if (and (>= level gnus-level-zombie)
15167                  entry)
15168             (progn
15169               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15170               (if (nth 3 entry)
15171                   (setcdr (gnus-gethash (car (nth 3 entry))
15172                                         gnus-newsrc-hashtb)
15173                           (cdr entry)))
15174               (setcdr (cdr entry) (cdddr entry))))))
15175
15176       ;; Finally we enter (if needed) the list where it is supposed to
15177       ;; go, and change the subscription level.  If it is to be killed,
15178       ;; we enter it into the killed or zombie list.
15179       (cond 
15180        ((>= level gnus-level-zombie)
15181         ;; Remove from the hash table.
15182         (gnus-sethash group nil gnus-newsrc-hashtb)
15183         ;; We do not enter foreign groups into the list of dead
15184         ;; groups.
15185         (unless (gnus-group-foreign-p group)
15186           (if (= level gnus-level-zombie)
15187               (setq gnus-zombie-list (cons group gnus-zombie-list))
15188             (setq gnus-killed-list (cons group gnus-killed-list)))))
15189        (t
15190         ;; If the list is to be entered into the newsrc assoc, and
15191         ;; it was killed, we have to create an entry in the newsrc
15192         ;; hashtb format and fix the pointers in the newsrc assoc.
15193         (if (< oldlevel gnus-level-zombie)
15194             ;; It was alive, and it is going to stay alive, so we
15195             ;; just change the level and don't change any pointers or
15196             ;; hash table entries.
15197             (setcar (cdaddr entry) level)
15198           (if (listp entry)
15199               (setq info (cdr entry)
15200                     num (car entry))
15201             (setq active (gnus-active group))
15202             (setq num
15203                   (if active (- (1+ (cdr active)) (car active)) t))
15204             ;; Check whether the group is foreign.  If so, the
15205             ;; foreign select method has to be entered into the
15206             ;; info.
15207             (let ((method (or gnus-override-subscribe-method
15208                               (gnus-group-method group))))
15209               (if (eq method gnus-select-method)
15210                   (setq info (list group level nil))
15211                 (setq info (list group level nil nil method)))))
15212           (unless previous
15213             (setq previous
15214                   (let ((p gnus-newsrc-alist))
15215                     (while (cddr p)
15216                       (setq p (cdr p)))
15217                     p)))
15218           (setq entry (cons info (cddr previous)))
15219           (if (cdr previous)
15220               (progn
15221                 (setcdr (cdr previous) entry)
15222                 (gnus-sethash group (cons num (cdr previous))
15223                               gnus-newsrc-hashtb))
15224             (setcdr previous entry)
15225             (gnus-sethash group (cons num previous)
15226                           gnus-newsrc-hashtb))
15227           (when (cdr entry)
15228             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15229       (when gnus-group-change-level-function
15230         (funcall gnus-group-change-level-function group level oldlevel)))))
15231
15232 (defun gnus-kill-newsgroup (newsgroup)
15233   "Obsolete function.  Kills a newsgroup."
15234   (gnus-group-change-level
15235    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15236
15237 (defun gnus-check-bogus-newsgroups (&optional confirm)
15238   "Remove bogus newsgroups.
15239 If CONFIRM is non-nil, the user has to confirm the deletion of every
15240 newsgroup."
15241   (let ((newsrc (cdr gnus-newsrc-alist))
15242         bogus group entry info)
15243     (gnus-message 5 "Checking bogus newsgroups...")
15244     (unless gnus-have-read-active-file
15245       (gnus-read-active-file))
15246     (when (member gnus-select-method gnus-have-read-active-file)
15247       ;; Find all bogus newsgroup that are subscribed.
15248       (while newsrc
15249         (setq info (pop newsrc)
15250               group (gnus-info-group info))
15251         (unless (or (gnus-active group) ; Active
15252                     (gnus-info-method info) ; Foreign
15253                     (and confirm
15254                          (not (gnus-y-or-n-p
15255                                (format "Remove bogus newsgroup: %s " group)))))
15256           ;; Found a bogus newsgroup.
15257           (push group bogus)))
15258       ;; Remove all bogus subscribed groups by first killing them, and
15259       ;; then removing them from the list of killed groups.
15260       (while bogus
15261         (when (setq entry (gnus-gethash (setq group (pop bogus))
15262                                         gnus-newsrc-hashtb))
15263           (gnus-group-change-level entry gnus-level-killed)
15264           (setq gnus-killed-list (delete group gnus-killed-list))))
15265       ;; Then we remove all bogus groups from the list of killed and
15266       ;; zombie groups.  They are are removed without confirmation.
15267       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15268             killed)
15269         (while dead-lists
15270           (setq killed (symbol-value (car dead-lists)))
15271           (while killed
15272             (unless (gnus-active (setq group (pop killed)))
15273               ;; The group is bogus.
15274               ;; !!!Slow as hell.
15275               (set (car dead-lists)
15276                    (delete group (symbol-value (car dead-lists))))))
15277           (setq dead-lists (cdr dead-lists))))
15278       (gnus-message 5 "Checking bogus newsgroups...done"))))
15279
15280 (defun gnus-check-duplicate-killed-groups ()
15281   "Remove duplicates from the list of killed groups."
15282   (interactive)
15283   (let ((killed gnus-killed-list))
15284     (while killed
15285       (gnus-message 9 "%d" (length killed))
15286       (setcdr killed (delete (car killed) (cdr killed)))
15287       (setq killed (cdr killed)))))
15288
15289 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15290 ;; and compute how many unread articles there are in each group.
15291 (defun gnus-get-unread-articles (&optional level)
15292   (let* ((newsrc (cdr gnus-newsrc-alist))
15293          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15294          (foreign-level
15295           (min
15296            (cond ((and gnus-activate-foreign-newsgroups
15297                        (not (numberp gnus-activate-foreign-newsgroups)))
15298                   (1+ gnus-level-subscribed))
15299                  ((numberp gnus-activate-foreign-newsgroups)
15300                   gnus-activate-foreign-newsgroups)
15301                  (t 0))
15302            level))
15303          info group active method)
15304     (gnus-message 5 "Checking new news...")
15305
15306     (while newsrc
15307       (setq active (gnus-active (setq group (gnus-info-group
15308                                              (setq info (pop newsrc))))))
15309
15310       ;; Check newsgroups.  If the user doesn't want to check them, or
15311       ;; they can't be checked (for instance, if the news server can't
15312       ;; be reached) we just set the number of unread articles in this
15313       ;; newsgroup to t.  This means that Gnus thinks that there are
15314       ;; unread articles, but it has no idea how many.
15315       (if (and (setq method (gnus-info-method info))
15316                (not (gnus-server-equal
15317                      gnus-select-method
15318                      (gnus-server-get-method nil method)))
15319                (not (gnus-secondary-method-p method)))
15320           ;; These groups are foreign.  Check the level.
15321           (when (<= (gnus-info-level info) foreign-level)
15322             (setq active (gnus-activate-group group 'scan))
15323             (gnus-close-group group))
15324
15325         ;; These groups are native or secondary.
15326         (when (and (<= (gnus-info-level info) level)
15327                    (not gnus-read-active-file))
15328           (setq active (gnus-activate-group group 'scan))
15329           (gnus-close-group group)))
15330
15331       (if active
15332           (gnus-get-unread-articles-in-group info active t)
15333         ;; The group couldn't be reached, so we nix out the number of
15334         ;; unread articles and stuff.
15335         (gnus-set-active group nil)
15336         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15337
15338     (gnus-message 5 "Checking new news...done")))
15339
15340 ;; Create a hash table out of the newsrc alist.  The `car's of the
15341 ;; alist elements are used as keys.
15342 (defun gnus-make-hashtable-from-newsrc-alist ()
15343   (let ((alist gnus-newsrc-alist)
15344         (ohashtb gnus-newsrc-hashtb)
15345         prev)
15346     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15347     (setq alist
15348           (setq prev (setq gnus-newsrc-alist
15349                            (if (equal (caar gnus-newsrc-alist)
15350                                       "dummy.group")
15351                                gnus-newsrc-alist
15352                              (cons (list "dummy.group" 0 nil) alist)))))
15353     (while alist
15354       (gnus-sethash
15355        (caar alist)
15356        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15357              prev)
15358        gnus-newsrc-hashtb)
15359       (setq prev alist
15360             alist (cdr alist)))))
15361
15362 (defun gnus-make-hashtable-from-killed ()
15363   "Create a hash table from the killed and zombie lists."
15364   (let ((lists '(gnus-killed-list gnus-zombie-list))
15365         list)
15366     (setq gnus-killed-hashtb
15367           (gnus-make-hashtable
15368            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15369     (while lists
15370       (setq list (symbol-value (car lists)))
15371       (setq lists (cdr lists))
15372       (while list
15373         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15374         (setq list (cdr list))))))
15375
15376 (defun gnus-get-unread-articles-in-group (info active &optional update)
15377   (when active
15378     ;; Allow the backend to update the info in the group.
15379     (when (and update 
15380                (gnus-request-update-info
15381                 info (gnus-find-method-for-group (gnus-info-group info))))
15382       (gnus-activate-group (gnus-info-group info)))
15383     (let* ((range (gnus-info-read info))
15384            (num 0))
15385       ;; If a cache is present, we may have to alter the active info.
15386       (when (and gnus-use-cache info)
15387         (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15388       ;; Modify the list of read articles according to what articles
15389       ;; are available; then tally the unread articles and add the
15390       ;; number to the group hash table entry.
15391       (cond
15392        ((zerop (cdr active))
15393         (setq num 0))
15394        ((not range)
15395         (setq num (- (1+ (cdr active)) (car active))))
15396        ((not (listp (cdr range)))
15397         ;; Fix a single (num . num) range according to the
15398         ;; active hash table.
15399         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15400         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15401         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15402         ;; Compute number of unread articles.
15403         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15404        (t
15405         ;; The read list is a list of ranges.  Fix them according to
15406         ;; the active hash table.
15407         ;; First peel off any elements that are below the lower
15408         ;; active limit.
15409         (while (and (cdr range)
15410                     (>= (car active)
15411                         (or (and (atom (cadr range)) (cadr range))
15412                             (caadr range))))
15413           (if (numberp (car range))
15414               (setcar range
15415                       (cons (car range)
15416                             (or (and (numberp (cadr range))
15417                                      (cadr range))
15418                                 (cdadr range))))
15419             (setcdr (car range)
15420                     (or (and (numberp (nth 1 range)) (nth 1 range))
15421                         (cdadr range))))
15422           (setcdr range (cddr range)))
15423         ;; Adjust the first element to be the same as the lower limit.
15424         (if (and (not (atom (car range)))
15425                  (< (cdar range) (car active)))
15426             (setcdr (car range) (1- (car active))))
15427         ;; Then we want to peel off any elements that are higher
15428         ;; than the upper active limit.
15429         (let ((srange range))
15430           ;; Go past all legal elements.
15431           (while (and (cdr srange)
15432                       (<= (or (and (atom (cadr srange))
15433                                    (cadr srange))
15434                               (caadr srange)) (cdr active)))
15435             (setq srange (cdr srange)))
15436           (if (cdr srange)
15437               ;; Nuke all remaining illegal elements.
15438               (setcdr srange nil))
15439
15440           ;; Adjust the final element.
15441           (if (and (not (atom (car srange)))
15442                    (> (cdar srange) (cdr active)))
15443               (setcdr (car srange) (cdr active))))
15444         ;; Compute the number of unread articles.
15445         (while range
15446           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15447                                       (cdar range)))
15448                               (or (and (atom (car range)) (car range))
15449                                   (caar range)))))
15450           (setq range (cdr range)))
15451         (setq num (max 0 (- (cdr active) num)))))
15452       ;; Set the number of unread articles.
15453       (when info
15454         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15455       num)))
15456
15457 (defun gnus-activate-group (group &optional scan)
15458   ;; Check whether a group has been activated or not.
15459   ;; If SCAN, request a scan of that group as well.
15460   (let ((method (gnus-find-method-for-group group))
15461         active)
15462     (and (gnus-check-server method)
15463          ;; We escape all bugs and quit here to make it possible to
15464          ;; continue if a group is so out-there that it reports bugs
15465          ;; and stuff.
15466          (progn
15467            (and scan
15468                 (gnus-check-backend-function 'request-scan (car method))
15469                 (gnus-request-scan group method))
15470            t)
15471          (condition-case ()
15472              (gnus-request-group group)
15473         ;   (error nil)
15474            (quit nil))
15475          (save-excursion
15476            (set-buffer nntp-server-buffer)
15477            (goto-char (point-min))
15478            ;; Parse the result we got from `gnus-request-group'.
15479            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15480                 (progn
15481                   (goto-char (match-beginning 1))
15482                   (gnus-set-active
15483                    group (setq active (cons (read (current-buffer))
15484                                             (read (current-buffer)))))
15485                   ;; Return the new active info.
15486                   active))))))
15487
15488 (defun gnus-update-read-articles (group unread)
15489   "Update the list of read and ticked articles in GROUP using the
15490 UNREAD and TICKED lists.
15491 Note: UNSELECTED has to be sorted over `<'.
15492 Returns whether the updating was successful."
15493   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15494          (entry (gnus-gethash group gnus-newsrc-hashtb))
15495          (info (nth 2 entry))
15496          (prev 1)
15497          (unread (sort (copy-sequence unread) '<))
15498          read)
15499     (if (or (not info) (not active))
15500         ;; There is no info on this group if it was, in fact,
15501         ;; killed.  Gnus stores no information on killed groups, so
15502         ;; there's nothing to be done.
15503         ;; One could store the information somewhere temporarily,
15504         ;; perhaps...  Hmmm...
15505         ()
15506       ;; Remove any negative articles numbers.
15507       (while (and unread (< (car unread) 0))
15508         (setq unread (cdr unread)))
15509       ;; Remove any expired article numbers
15510       (while (and unread (< (car unread) (car active)))
15511         (setq unread (cdr unread)))
15512       ;; Compute the ranges of read articles by looking at the list of
15513       ;; unread articles.
15514       (while unread
15515         (if (/= (car unread) prev)
15516             (setq read (cons (if (= prev (1- (car unread))) prev
15517                                (cons prev (1- (car unread)))) read)))
15518         (setq prev (1+ (car unread)))
15519         (setq unread (cdr unread)))
15520       (when (<= prev (cdr active))
15521         (setq read (cons (cons prev (cdr active)) read)))
15522       ;; Enter this list into the group info.
15523       (gnus-info-set-read
15524        info (if (> (length read) 1) (nreverse read) read))
15525       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15526       (gnus-get-unread-articles-in-group info (gnus-active group))
15527       t)))
15528
15529 (defun gnus-make-articles-unread (group articles)
15530   "Mark ARTICLES in GROUP as unread."
15531   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15532                           (gnus-gethash (gnus-group-real-name group)
15533                                         gnus-newsrc-hashtb))))
15534          (ranges (gnus-info-read info))
15535          news article)
15536     (while articles
15537       (when (gnus-member-of-range
15538              (setq article (pop articles)) ranges)
15539         (setq news (cons article news))))
15540     (when news
15541       (gnus-info-set-read
15542        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15543       (gnus-group-update-group group t))))
15544
15545 ;; Enter all dead groups into the hashtb.
15546 (defun gnus-update-active-hashtb-from-killed ()
15547   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15548         (lists (list gnus-killed-list gnus-zombie-list))
15549         killed)
15550     (while lists
15551       (setq killed (car lists))
15552       (while killed
15553         (gnus-sethash (car killed) nil hashtb)
15554         (setq killed (cdr killed)))
15555       (setq lists (cdr lists)))))
15556
15557 ;; Get the active file(s) from the backend(s).
15558 (defun gnus-read-active-file ()
15559   (gnus-group-set-mode-line)
15560   (let ((methods 
15561          (append
15562           (if (gnus-check-server gnus-select-method)
15563               ;; The native server is available.
15564               (cons gnus-select-method gnus-secondary-select-methods)
15565             ;; The native server is down, so we just do the
15566             ;; secondary ones.
15567             gnus-secondary-select-methods)
15568           ;; Also read from the archive server.
15569           (when gnus-message-archive-method
15570             (list "archive"))))
15571         list-type)
15572     (setq gnus-have-read-active-file nil)
15573     (save-excursion
15574       (set-buffer nntp-server-buffer)
15575       (while methods
15576         (let* ((method (if (stringp (car methods))
15577                            (gnus-server-get-method nil (car methods))
15578                          (car methods)))
15579                (where (nth 1 method))
15580                (mesg (format "Reading active file%s via %s..."
15581                              (if (and where (not (zerop (length where))))
15582                                  (concat " from " where) "")
15583                              (car method))))
15584           (gnus-message 5 mesg)
15585           (when (gnus-check-server method)
15586             ;; Request that the backend scan its incoming messages.
15587             (and (gnus-check-backend-function 'request-scan (car method))
15588                  (gnus-request-scan nil method))
15589             (cond
15590              ((and (eq gnus-read-active-file 'some)
15591                    (gnus-check-backend-function 'retrieve-groups (car method)))
15592               (let ((newsrc (cdr gnus-newsrc-alist))
15593                     (gmethod (if (stringp method)
15594                                  (gnus-server-get-method nil method)
15595                                method))
15596                     groups info)
15597                 (while (setq info (pop newsrc))
15598                   (when (gnus-server-equal
15599                          (gnus-find-method-for-group 
15600                           (gnus-info-group info) info)
15601                          gmethod)
15602                     (push (gnus-group-real-name (gnus-info-group info)) 
15603                           groups)))
15604                 (when groups
15605                   (gnus-check-server method)
15606                   (setq list-type (gnus-retrieve-groups groups method))
15607                   (cond
15608                    ((not list-type)
15609                     (gnus-message
15610                      1 "Cannot read partial active file from %s server."
15611                      (car method))
15612                     (ding)
15613                     (sit-for 2))
15614                    ((eq list-type 'active)
15615                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15616                    (t
15617                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15618              (t
15619               (if (not (gnus-request-list method))
15620                   (progn
15621                     (unless (equal method gnus-message-archive-method)
15622                       (gnus-message 1 "Cannot read active file from %s server."
15623                                     (car method))
15624                       (ding)))
15625                 (gnus-active-to-gnus-format method)
15626                 ;; We mark this active file as read.
15627                 (push method gnus-have-read-active-file)
15628                 (gnus-message 5 "%sdone" mesg))))))
15629         (setq methods (cdr methods))))))
15630
15631 ;; Read an active file and place the results in `gnus-active-hashtb'.
15632 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15633   (unless method
15634     (setq method gnus-select-method))
15635   (let ((cur (current-buffer))
15636         (hashtb (or hashtb
15637                     (if (and gnus-active-hashtb
15638                              (not (equal method gnus-select-method)))
15639                         gnus-active-hashtb
15640                       (setq gnus-active-hashtb
15641                             (if (equal method gnus-select-method)
15642                                 (gnus-make-hashtable
15643                                  (count-lines (point-min) (point-max)))
15644                               (gnus-make-hashtable 4096)))))))
15645     ;; Delete unnecessary lines.
15646     (goto-char (point-min))
15647     (while (search-forward "\nto." nil t)
15648       (delete-region (1+ (match-beginning 0))
15649                      (progn (forward-line 1) (point))))
15650     (or (string= gnus-ignored-newsgroups "")
15651         (progn
15652           (goto-char (point-min))
15653           (delete-matching-lines gnus-ignored-newsgroups)))
15654     ;; Make the group names readable as a lisp expression even if they
15655     ;; contain special characters.
15656     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15657     (goto-char (point-max))
15658     (while (re-search-backward "[][';?()#]" nil t)
15659       (insert ?\\))
15660     ;; If these are groups from a foreign select method, we insert the
15661     ;; group prefix in front of the group names.
15662     (and method (not (gnus-server-equal
15663                       (gnus-server-get-method nil method)
15664                       (gnus-server-get-method nil gnus-select-method)))
15665          (let ((prefix (gnus-group-prefixed-name "" method)))
15666            (goto-char (point-min))
15667            (while (and (not (eobp))
15668                        (progn (insert prefix)
15669                               (zerop (forward-line 1)))))))
15670     ;; Store the active file in a hash table.
15671     (goto-char (point-min))
15672     (if (string-match "%[oO]" gnus-group-line-format)
15673         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15674         ;; If we want information on moderated groups, we use this
15675         ;; loop...
15676         (let* ((mod-hashtb (make-vector 7 0))
15677                (m (intern "m" mod-hashtb))
15678                group max min)
15679           (while (not (eobp))
15680             (condition-case nil
15681                 (progn
15682                   (narrow-to-region (point) (gnus-point-at-eol))
15683                   (setq group (let ((obarray hashtb)) (read cur)))
15684                   (if (and (numberp (setq max (read cur)))
15685                            (numberp (setq min (read cur)))
15686                            (progn
15687                              (skip-chars-forward " \t")
15688                              (not
15689                               (or (= (following-char) ?=)
15690                                   (= (following-char) ?x)
15691                                   (= (following-char) ?j)))))
15692                       (set group (cons min max))
15693                     (set group nil))
15694                   ;; Enter moderated groups into a list.
15695                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15696                       (setq gnus-moderated-list
15697                             (cons (symbol-name group) gnus-moderated-list))))
15698               (error
15699                (and group
15700                     (symbolp group)
15701                     (set group nil))))
15702             (widen)
15703             (forward-line 1)))
15704       ;; And if we do not care about moderation, we use this loop,
15705       ;; which is faster.
15706       (let (group max min)
15707         (while (not (eobp))
15708           (condition-case ()
15709               (progn
15710                 (narrow-to-region (point) (gnus-point-at-eol))
15711                 ;; group gets set to a symbol interned in the hash table
15712                 ;; (what a hack!!) - jwz
15713                 (setq group (let ((obarray hashtb)) (read cur)))
15714                 (if (and (numberp (setq max (read cur)))
15715                          (numberp (setq min (read cur)))
15716                          (progn
15717                            (skip-chars-forward " \t")
15718                            (not
15719                             (or (= (following-char) ?=)
15720                                 (= (following-char) ?x)
15721                                 (= (following-char) ?j)))))
15722                     (set group (cons min max))
15723                   (set group nil)))
15724             (error
15725              (progn
15726                (and group
15727                     (symbolp group)
15728                     (set group nil))
15729                (or ignore-errors
15730                    (gnus-message 3 "Warning - illegal active: %s"
15731                                  (buffer-substring
15732                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15733           (widen)
15734           (forward-line 1))))))
15735
15736 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15737   ;; Parse a "groups" active file.
15738   (let ((cur (current-buffer))
15739         (hashtb (or hashtb
15740                     (if (and method gnus-active-hashtb)
15741                         gnus-active-hashtb
15742                       (setq gnus-active-hashtb
15743                             (gnus-make-hashtable
15744                              (count-lines (point-min) (point-max)))))))
15745         (prefix (and method
15746                      (not (gnus-server-equal
15747                            (gnus-server-get-method nil method)
15748                            (gnus-server-get-method nil gnus-select-method)))
15749                      (gnus-group-prefixed-name "" method))))
15750
15751     (goto-char (point-min))
15752     ;; We split this into to separate loops, one with the prefix
15753     ;; and one without to speed the reading up somewhat.
15754     (if prefix
15755         (let (min max opoint group)
15756           (while (not (eobp))
15757             (condition-case ()
15758                 (progn
15759                   (read cur) (read cur)
15760                   (setq min (read cur)
15761                         max (read cur)
15762                         opoint (point))
15763                   (skip-chars-forward " \t")
15764                   (insert prefix)
15765                   (goto-char opoint)
15766                   (set (let ((obarray hashtb)) (read cur))
15767                        (cons min max)))
15768               (error (and group (symbolp group) (set group nil))))
15769             (forward-line 1)))
15770       (let (min max group)
15771         (while (not (eobp))
15772           (condition-case ()
15773               (if (= (following-char) ?2)
15774                   (progn
15775                     (read cur) (read cur)
15776                     (setq min (read cur)
15777                           max (read cur))
15778                     (set (setq group (let ((obarray hashtb)) (read cur)))
15779                          (cons min max))))
15780             (error (and group (symbolp group) (set group nil))))
15781           (forward-line 1))))))
15782
15783 (defun gnus-read-newsrc-file (&optional force)
15784   "Read startup file.
15785 If FORCE is non-nil, the .newsrc file is read."
15786   ;; Reset variables that might be defined in the .newsrc.eld file.
15787   (let ((variables gnus-variable-list))
15788     (while variables
15789       (set (car variables) nil)
15790       (setq variables (cdr variables))))
15791   (let* ((newsrc-file gnus-current-startup-file)
15792          (quick-file (concat newsrc-file ".el")))
15793     (save-excursion
15794       ;; We always load the .newsrc.eld file.  If always contains
15795       ;; much information that can not be gotten from the .newsrc
15796       ;; file (ticked articles, killed groups, foreign methods, etc.)
15797       (gnus-read-newsrc-el-file quick-file)
15798
15799       (if (and (file-exists-p gnus-current-startup-file)
15800                (or force
15801                    (and (file-newer-than-file-p newsrc-file quick-file)
15802                         (file-newer-than-file-p newsrc-file
15803                                                 (concat quick-file "d")))
15804                    (not gnus-newsrc-alist)))
15805           ;; We read the .newsrc file.  Note that if there if a
15806           ;; .newsrc.eld file exists, it has already been read, and
15807           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15808           ;; the .newsrc file, Gnus will only use the information it
15809           ;; can find there for changing the data already read -
15810           ;; ie. reading the .newsrc file will not trash the data
15811           ;; already read (except for read articles).
15812           (save-excursion
15813             (gnus-message 5 "Reading %s..." newsrc-file)
15814             (set-buffer (find-file-noselect newsrc-file))
15815             (buffer-disable-undo (current-buffer))
15816             (gnus-newsrc-to-gnus-format)
15817             (kill-buffer (current-buffer))
15818             (gnus-message 5 "Reading %s...done" newsrc-file)))
15819
15820       ;; Read any slave files.
15821       (or gnus-slave
15822           (gnus-master-read-slave-newsrc)))))
15823
15824 (defun gnus-read-newsrc-el-file (file)
15825   (let ((ding-file (concat file "d")))
15826     ;; We always, always read the .eld file.
15827     (gnus-message 5 "Reading %s..." ding-file)
15828     (let (gnus-newsrc-assoc)
15829       (condition-case nil
15830           (load ding-file t t t)
15831         (error
15832          (gnus-message 1 "Error in %s" ding-file)
15833          (ding)))
15834       (when gnus-newsrc-assoc
15835         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15836     (gnus-make-hashtable-from-newsrc-alist)
15837     (when (file-newer-than-file-p file ding-file)
15838       ;; Old format quick file
15839       (gnus-message 5 "Reading %s..." file)
15840       ;; The .el file is newer than the .eld file, so we read that one
15841       ;; as well.
15842       (gnus-read-old-newsrc-el-file file))))
15843
15844 ;; Parse the old-style quick startup file
15845 (defun gnus-read-old-newsrc-el-file (file)
15846   (let (newsrc killed marked group m info)
15847     (prog1
15848         (let ((gnus-killed-assoc nil)
15849               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15850           (prog1
15851               (condition-case nil
15852                   (load file t t t)
15853                 (error nil))
15854             (setq newsrc gnus-newsrc-assoc
15855                   killed gnus-killed-assoc
15856                   marked gnus-marked-assoc)))
15857       (setq gnus-newsrc-alist nil)
15858       (while (setq info (gnus-get-info (setq group (pop newsrc))))
15859         (if info
15860             (progn
15861               (gnus-info-set-read info (cddr group))
15862               (gnus-info-set-level
15863                info (if (nth 1 group) gnus-level-default-subscribed
15864                       gnus-level-default-unsubscribed))
15865               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15866           (push (setq info
15867                       (list (car group)
15868                             (if (nth 1 group) gnus-level-default-subscribed
15869                               gnus-level-default-unsubscribed)
15870                             (cddr group)))
15871                 gnus-newsrc-alist))
15872         ;; Copy marks into info.
15873         (when (setq m (assoc (car group) marked))
15874           (unless (nthcdr 3 info)
15875             (nconc info (list nil)))
15876           (gnus-info-set-marks
15877            info (list (cons 'tick (gnus-compress-sequence 
15878                                    (sort (cdr m) '<) t))))))
15879       (setq newsrc killed)
15880       (while newsrc
15881         (setcar newsrc (caar newsrc))
15882         (setq newsrc (cdr newsrc)))
15883       (setq gnus-killed-list killed))
15884     ;; The .el file version of this variable does not begin with
15885     ;; "options", while the .eld version does, so we just add it if it
15886     ;; isn't there.
15887     (and
15888      gnus-newsrc-options
15889      (progn
15890        (and (not (string-match "^ *options" gnus-newsrc-options))
15891             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15892        (and (not (string-match "\n$" gnus-newsrc-options))
15893             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15894        ;; Finally, if we read some options lines, we parse them.
15895        (or (string= gnus-newsrc-options "")
15896            (gnus-newsrc-parse-options gnus-newsrc-options))))
15897
15898     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15899     (gnus-make-hashtable-from-newsrc-alist)))
15900
15901 (defun gnus-make-newsrc-file (file)
15902   "Make server dependent file name by catenating FILE and server host name."
15903   (let* ((file (expand-file-name file nil))
15904          (real-file (concat file "-" (nth 1 gnus-select-method))))
15905     (if (or (file-exists-p real-file)
15906             (file-exists-p (concat real-file ".el"))
15907             (file-exists-p (concat real-file ".eld")))
15908         real-file file)))
15909
15910 (defun gnus-newsrc-to-gnus-format ()
15911   (setq gnus-newsrc-options "")
15912   (setq gnus-newsrc-options-n nil)
15913
15914   (or gnus-active-hashtb
15915       (setq gnus-active-hashtb (make-vector 4095 0)))
15916   (let ((buf (current-buffer))
15917         (already-read (> (length gnus-newsrc-alist) 1))
15918         group subscribed options-symbol newsrc Options-symbol
15919         symbol reads num1)
15920     (goto-char (point-min))
15921     ;; We intern the symbol `options' in the active hashtb so that we
15922     ;; can `eq' against it later.
15923     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15924     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15925
15926     (while (not (eobp))
15927       ;; We first read the first word on the line by narrowing and
15928       ;; then reading into `gnus-active-hashtb'.  Most groups will
15929       ;; already exist in that hashtb, so this will save some string
15930       ;; space.
15931       (narrow-to-region
15932        (point)
15933        (progn (skip-chars-forward "^ \t!:\n") (point)))
15934       (goto-char (point-min))
15935       (setq symbol
15936             (and (/= (point-min) (point-max))
15937                  (let ((obarray gnus-active-hashtb)) (read buf))))
15938       (widen)
15939       ;; Now, the symbol we have read is either `options' or a group
15940       ;; name.  If it is an options line, we just add it to a string.
15941       (cond
15942        ((or (eq symbol options-symbol)
15943             (eq symbol Options-symbol))
15944         (setq gnus-newsrc-options
15945               ;; This concating is quite inefficient, but since our
15946               ;; thorough studies show that approx 99.37% of all
15947               ;; .newsrc files only contain a single options line, we
15948               ;; don't give a damn, frankly, my dear.
15949               (concat gnus-newsrc-options
15950                       (buffer-substring
15951                        (gnus-point-at-bol)
15952                        ;; Options may continue on the next line.
15953                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15954                                 (progn (beginning-of-line) (point)))
15955                            (point)))))
15956         (forward-line -1))
15957        (symbol
15958         ;; Group names can be just numbers.  
15959         (when (numberp symbol) 
15960           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15961         (or (boundp symbol) (set symbol nil))
15962         ;; It was a group name.
15963         (setq subscribed (= (following-char) ?:)
15964               group (symbol-name symbol)
15965               reads nil)
15966         (if (eolp)
15967             ;; If the line ends here, this is clearly a buggy line, so
15968             ;; we put point a the beginning of line and let the cond
15969             ;; below do the error handling.
15970             (beginning-of-line)
15971           ;; We skip to the beginning of the ranges.
15972           (skip-chars-forward "!: \t"))
15973         ;; We are now at the beginning of the list of read articles.
15974         ;; We read them range by range.
15975         (while
15976             (cond
15977              ((looking-at "[0-9]+")
15978               ;; We narrow and read a number instead of buffer-substring/
15979               ;; string-to-int because it's faster.  narrow/widen is
15980               ;; faster than save-restriction/narrow, and save-restriction
15981               ;; produces a garbage object.
15982               (setq num1 (progn
15983                            (narrow-to-region (match-beginning 0) (match-end 0))
15984                            (read buf)))
15985               (widen)
15986               ;; If the next character is a dash, then this is a range.
15987               (if (= (following-char) ?-)
15988                   (progn
15989                     ;; We read the upper bound of the range.
15990                     (forward-char 1)
15991                     (if (not (looking-at "[0-9]+"))
15992                         ;; This is a buggy line, by we pretend that
15993                         ;; it's kinda OK.  Perhaps the user should be
15994                         ;; dinged?
15995                         (setq reads (cons num1 reads))
15996                       (setq reads
15997                             (cons
15998                              (cons num1
15999                                    (progn
16000                                      (narrow-to-region (match-beginning 0)
16001                                                        (match-end 0))
16002                                      (read buf)))
16003                              reads))
16004                       (widen)))
16005                 ;; It was just a simple number, so we add it to the
16006                 ;; list of ranges.
16007                 (setq reads (cons num1 reads)))
16008               ;; If the next char in ?\n, then we have reached the end
16009               ;; of the line and return nil.
16010               (/= (following-char) ?\n))
16011              ((= (following-char) ?\n)
16012               ;; End of line, so we end.
16013               nil)
16014              (t
16015               ;; Not numbers and not eol, so this might be a buggy
16016               ;; line...
16017               (or (eobp)
16018                   ;; If it was eob instead of ?\n, we allow it.
16019                   (progn
16020                     ;; The line was buggy.
16021                     (setq group nil)
16022                     (gnus-message 3 "Mangled line: %s"
16023                                   (buffer-substring (gnus-point-at-bol)
16024                                                     (gnus-point-at-eol)))
16025                     (ding)
16026                     (sit-for 1)))
16027               nil))
16028           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16029           ;; we allow them, because it's a common mistake to put a
16030           ;; space after the comma.
16031           (skip-chars-forward ", "))
16032
16033         ;; We have already read .newsrc.eld, so we gently update the
16034         ;; data in the hash table with the information we have just
16035         ;; read.
16036         (when group
16037           (let ((info (gnus-get-info group))
16038                 level)
16039             (if info
16040                 ;; There is an entry for this file in the alist.
16041                 (progn
16042                   (gnus-info-set-read info (nreverse reads))
16043                   ;; We update the level very gently.  In fact, we
16044                   ;; only change it if there's been a status change
16045                   ;; from subscribed to unsubscribed, or vice versa.
16046                   (setq level (gnus-info-level info))
16047                   (cond ((and (<= level gnus-level-subscribed)
16048                               (not subscribed))
16049                          (setq level (if reads
16050                                          gnus-level-default-unsubscribed
16051                                        (1+ gnus-level-default-unsubscribed))))
16052                         ((and (> level gnus-level-subscribed) subscribed)
16053                          (setq level gnus-level-default-subscribed)))
16054                   (gnus-info-set-level info level))
16055               ;; This is a new group.
16056               (setq info (list group
16057                                (if subscribed
16058                                    gnus-level-default-subscribed
16059                                  (if reads
16060                                      (1+ gnus-level-subscribed)
16061                                    gnus-level-default-unsubscribed))
16062                                (nreverse reads))))
16063             (setq newsrc (cons info newsrc))))))
16064       (forward-line 1))
16065
16066     (setq newsrc (nreverse newsrc))
16067
16068     (if (not already-read)
16069         ()
16070       ;; We now have two newsrc lists - `newsrc', which is what we
16071       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16072       ;; what we've read from .newsrc.eld.  We have to merge these
16073       ;; lists.  We do this by "attaching" any (foreign) groups in the
16074       ;; gnus-newsrc-alist to the (native) group that precedes them.
16075       (let ((rc (cdr gnus-newsrc-alist))
16076             (prev gnus-newsrc-alist)
16077             entry mentry)
16078         (while rc
16079           (or (null (nth 4 (car rc)))   ; It's a native group.
16080               (assoc (caar rc) newsrc) ; It's already in the alist.
16081               (if (setq entry (assoc (caar prev) newsrc))
16082                   (setcdr (setq mentry (memq entry newsrc))
16083                           (cons (car rc) (cdr mentry)))
16084                 (setq newsrc (cons (car rc) newsrc))))
16085           (setq prev rc
16086                 rc (cdr rc)))))
16087
16088     (setq gnus-newsrc-alist newsrc)
16089     ;; We make the newsrc hashtb.
16090     (gnus-make-hashtable-from-newsrc-alist)
16091
16092     ;; Finally, if we read some options lines, we parse them.
16093     (or (string= gnus-newsrc-options "")
16094         (gnus-newsrc-parse-options gnus-newsrc-options))))
16095
16096 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16097 ;; The return value will be a list on the form
16098 ;; ((regexp1 . ignore)
16099 ;;  (regexp2 . subscribe)...)
16100 ;; When handling new newsgroups, groups that match a `ignore' regexp
16101 ;; will be ignored, and groups that match a `subscribe' regexp will be
16102 ;; subscribed.  A line like
16103 ;; options -n !all rec.all
16104 ;; will lead to a list that looks like
16105 ;; (("^rec\\..+" . subscribe)
16106 ;;  ("^.+" . ignore))
16107 ;; So all "rec.*" groups will be subscribed, while all the other
16108 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16109 ;; different from "options -n rec.all !all".
16110 (defun gnus-newsrc-parse-options (options)
16111   (let (out eol)
16112     (save-excursion
16113       (gnus-set-work-buffer)
16114       (insert (regexp-quote options))
16115       ;; First we treat all continuation lines.
16116       (goto-char (point-min))
16117       (while (re-search-forward "\n[ \t]+" nil t)
16118         (replace-match " " t t))
16119       ;; Then we transform all "all"s into ".+"s.
16120       (goto-char (point-min))
16121       (while (re-search-forward "\\ball\\b" nil t)
16122         (replace-match ".+" t t))
16123       (goto-char (point-min))
16124       ;; We remove all other options than the "-n" ones.
16125       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16126         (replace-match " ")
16127         (forward-char -1))
16128       (goto-char (point-min))
16129
16130       ;; We are only interested in "options -n" lines - we
16131       ;; ignore the other option lines.
16132       (while (re-search-forward "[ \t]-n" nil t)
16133         (setq eol
16134               (or (save-excursion
16135                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16136                          (- (point) 2)))
16137                   (gnus-point-at-eol)))
16138         ;; Search for all "words"...
16139         (while (re-search-forward "[^ \t,\n]+" eol t)
16140           (if (= (char-after (match-beginning 0)) ?!)
16141               ;; If the word begins with a bang (!), this is a "not"
16142               ;; spec.  We put this spec (minus the bang) and the
16143               ;; symbol `ignore' into the list.
16144               (setq out (cons (cons (concat
16145                                      "^" (buffer-substring
16146                                           (1+ (match-beginning 0))
16147                                           (match-end 0)))
16148                                     'ignore) out))
16149             ;; There was no bang, so this is a "yes" spec.
16150             (setq out (cons (cons (concat "^" (match-string 0))
16151                                   'subscribe) out)))))
16152
16153       (setq gnus-newsrc-options-n out))))
16154
16155 (defun gnus-save-newsrc-file (&optional force)
16156   "Save .newsrc file."
16157   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16158   ;; from the variable gnus-newsrc-alist.
16159   (when (and (or gnus-newsrc-alist gnus-killed-list)
16160              gnus-current-startup-file)
16161     (save-excursion
16162       (if (and (or gnus-use-dribble-file gnus-slave)
16163                (not force)
16164                (or (not gnus-dribble-buffer)
16165                    (not (buffer-name gnus-dribble-buffer))
16166                    (zerop (save-excursion
16167                             (set-buffer gnus-dribble-buffer)
16168                             (buffer-size)))))
16169           (gnus-message 4 "(No changes need to be saved)")
16170         (run-hooks 'gnus-save-newsrc-hook)
16171         (if gnus-slave
16172             (gnus-slave-save-newsrc)
16173           ;; Save .newsrc.
16174           (when gnus-save-newsrc-file
16175             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16176             (gnus-gnus-to-newsrc-format)
16177             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16178           ;; Save .newsrc.eld.
16179           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16180           (make-local-variable 'version-control)
16181           (setq version-control 'never)
16182           (setq buffer-file-name
16183                 (concat gnus-current-startup-file ".eld"))
16184           (gnus-add-current-to-buffer-list)
16185           (buffer-disable-undo (current-buffer))
16186           (erase-buffer)
16187           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16188           (gnus-gnus-to-quick-newsrc-format)
16189           (run-hooks 'gnus-save-quick-newsrc-hook)
16190           (save-buffer)
16191           (kill-buffer (current-buffer))
16192           (gnus-message
16193            5 "Saving %s.eld...done" gnus-current-startup-file))
16194         (gnus-dribble-delete-file)))))
16195
16196 (defun gnus-gnus-to-quick-newsrc-format ()
16197   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16198   (insert ";; Gnus startup file.\n")
16199   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16200   (insert ";; to read .newsrc.\n")
16201   (insert "(setq gnus-newsrc-file-version "
16202           (prin1-to-string gnus-version) ")\n")
16203   (let ((variables
16204          (if gnus-save-killed-list gnus-variable-list
16205            ;; Remove the `gnus-killed-list' from the list of variables
16206            ;; to be saved, if required.
16207            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16208         ;; Peel off the "dummy" group.
16209         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16210         variable)
16211     ;; Insert the variables into the file.
16212     (while variables
16213       (when (and (boundp (setq variable (pop variables)))
16214                  (symbol-value variable))
16215         (insert "(setq " (symbol-name variable) " '")
16216         (prin1 (symbol-value variable) (current-buffer))
16217         (insert ")\n")))))
16218
16219 (defun gnus-gnus-to-newsrc-format ()
16220   ;; Generate and save the .newsrc file.
16221   (let ((newsrc (cdr gnus-newsrc-alist))
16222         info ranges range)
16223     (save-excursion
16224       (set-buffer (create-file-buffer gnus-current-startup-file))
16225       (setq buffer-file-name gnus-current-startup-file)
16226       (buffer-disable-undo (current-buffer))
16227       (erase-buffer)
16228       ;; Write options.
16229       (if gnus-newsrc-options (insert gnus-newsrc-options))
16230       ;; Write subscribed and unsubscribed.
16231       (while (setq info (pop newsrc))
16232         ;; Don't write foreign groups to .newsrc.
16233         (when (gnus-server-equal (gnus-info-method info) gnus-select-method)
16234           (insert (gnus-info-group info)
16235                   (if (> (gnus-info-level info) gnus-level-subscribed)
16236                       "!" ":"))
16237           (when (setq ranges (gnus-info-read info))
16238             (insert " ")
16239             (if (not (listp (cdr ranges)))
16240                 (if (= (car ranges) (cdr ranges))
16241                     (insert (int-to-string (car ranges)))
16242                   (insert (int-to-string (car ranges)) "-"
16243                           (int-to-string (cdr ranges))))
16244               (while ranges
16245                 (setq range (car ranges)
16246                       ranges (cdr ranges))
16247                 (if (or (atom range) (= (car range) (cdr range)))
16248                     (insert (int-to-string
16249                              (or (and (atom range) range)
16250                                  (car range))))
16251                   (insert (int-to-string (car range)) "-"
16252                           (int-to-string (cdr range))))
16253                 (if ranges (insert ",")))))
16254           (insert "\n")))
16255       (make-local-variable 'version-control)
16256       (setq version-control 'never)
16257       ;; It has been reported that sometime the modtime on the .newsrc
16258       ;; file seems to be off.  We really do want to overwrite it, so
16259       ;; we clear the modtime here before saving.  It's a bit odd,
16260       ;; though...
16261       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16262       ;; delete the silly thing entirely first.  but this fails to provide
16263       ;; such niceties as .newsrc~ creation.
16264       (if gnus-modtime-botch
16265           (delete-file gnus-startup-file)
16266         (clear-visited-file-modtime))
16267       (run-hooks 'gnus-save-standard-newsrc-hook)
16268       (save-buffer)
16269       (kill-buffer (current-buffer)))))
16270
16271 \f
16272 ;;;
16273 ;;; Slave functions.
16274 ;;;
16275
16276 (defun gnus-slave-save-newsrc ()
16277   (save-excursion
16278     (set-buffer gnus-dribble-buffer)
16279     (let ((slave-name
16280            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16281       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16282
16283 (defun gnus-master-read-slave-newsrc ()
16284   (let ((slave-files
16285          (directory-files
16286           (file-name-directory gnus-current-startup-file)
16287           t (concat
16288              "^" (regexp-quote
16289                   (concat
16290                    (file-name-nondirectory gnus-current-startup-file)
16291                    "-slave-")))
16292           t))
16293         file)
16294     (if (not slave-files)
16295         ()                              ; There are no slave files to read.
16296       (gnus-message 7 "Reading slave newsrcs...")
16297       (save-excursion
16298         (set-buffer (get-buffer-create " *gnus slave*"))
16299         (buffer-disable-undo (current-buffer))
16300         (setq slave-files
16301               (sort (mapcar (lambda (file)
16302                               (list (nth 5 (file-attributes file)) file))
16303                             slave-files)
16304                     (lambda (f1 f2)
16305                       (or (< (caar f1) (caar f2))
16306                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16307         (while slave-files
16308           (erase-buffer)
16309           (setq file (nth 1 (car slave-files)))
16310           (insert-file-contents file)
16311           (if (condition-case ()
16312                   (progn
16313                     (eval-buffer (current-buffer))
16314                     t)
16315                 (error
16316                  (gnus-message 3 "Possible error in %s" file)
16317                  (ding)
16318                  (sit-for 2)
16319                  nil))
16320               (or gnus-slave ; Slaves shouldn't delete these files.
16321                   (condition-case ()
16322                       (delete-file file)
16323                     (error nil))))
16324           (setq slave-files (cdr slave-files))))
16325       (gnus-message 7 "Reading slave newsrcs...done"))))
16326
16327 \f
16328 ;;;
16329 ;;; Group description.
16330 ;;;
16331
16332 (defun gnus-read-all-descriptions-files ()
16333   (let ((methods (cons gnus-select-method 
16334                        (nconc
16335                         (when gnus-message-archive-method
16336                           (list "archive"))
16337                         gnus-secondary-select-methods))))
16338     (while methods
16339       (gnus-read-descriptions-file (car methods))
16340       (setq methods (cdr methods)))
16341     t))
16342
16343 (defun gnus-read-descriptions-file (&optional method)
16344   (let ((method (or method gnus-select-method)))
16345     (when (stringp method)
16346       (setq method (gnus-server-to-method method)))
16347     ;; We create the hashtable whether we manage to read the desc file
16348     ;; to avoid trying to re-read after a failed read.
16349     (or gnus-description-hashtb
16350         (setq gnus-description-hashtb
16351               (gnus-make-hashtable (length gnus-active-hashtb))))
16352     ;; Mark this method's desc file as read.
16353     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16354                   gnus-description-hashtb)
16355
16356     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16357     (cond
16358      ((not (gnus-check-server method))
16359       (gnus-message 1 "Couldn't open server")
16360       nil)
16361      ((not (gnus-request-list-newsgroups method))
16362       (gnus-message 1 "Couldn't read newsgroups descriptions")
16363       nil)
16364      (t
16365       (let (group)
16366         (save-excursion
16367           (save-restriction
16368             (set-buffer nntp-server-buffer)
16369             (goto-char (point-min))
16370             (if (or (search-forward "\n.\n" nil t)
16371                     (goto-char (point-max)))
16372                 (progn
16373                   (beginning-of-line)
16374                   (narrow-to-region (point-min) (point))))
16375             (goto-char (point-min))
16376             (while (not (eobp))
16377               ;; If we get an error, we set group to 0, which is not a
16378               ;; symbol...
16379               (setq group
16380                     (condition-case ()
16381                         (let ((obarray gnus-description-hashtb))
16382                           ;; Group is set to a symbol interned in this
16383                           ;; hash table.
16384                           (read nntp-server-buffer))
16385                       (error 0)))
16386               (skip-chars-forward " \t")
16387               ;; ...  which leads to this line being effectively ignored.
16388               (and (symbolp group)
16389                    (set group (buffer-substring
16390                                (point) (progn (end-of-line) (point)))))
16391               (forward-line 1))))
16392         (gnus-message 5 "Reading descriptions file...done")
16393         t)))))
16394
16395 (defun gnus-group-get-description (group)
16396   "Get the description of a group by sending XGTITLE to the server."
16397   (when (gnus-request-group-description group)
16398     (save-excursion
16399       (set-buffer nntp-server-buffer)
16400       (goto-char (point-min))
16401       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16402         (match-string 1)))))
16403
16404 \f
16405 ;;;
16406 ;;; Buffering of read articles.
16407 ;;;
16408
16409 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16410 (defvar gnus-backlog-articles nil)
16411 (defvar gnus-backlog-hashtb nil)
16412
16413 (defun gnus-backlog-buffer ()
16414   "Return the backlog buffer."
16415   (or (get-buffer gnus-backlog-buffer)
16416       (save-excursion
16417         (set-buffer (get-buffer-create gnus-backlog-buffer))
16418         (buffer-disable-undo (current-buffer))
16419         (setq buffer-read-only t)
16420         (gnus-add-current-to-buffer-list)
16421         (get-buffer gnus-backlog-buffer))))
16422
16423 (defun gnus-backlog-setup ()
16424   "Initialize backlog variables."
16425   (unless gnus-backlog-hashtb
16426     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16427
16428 (defun gnus-backlog-shutdown ()
16429   "Clear all backlog variables and buffers."
16430   (when (get-buffer gnus-backlog-buffer)
16431     (kill-buffer gnus-backlog-buffer))
16432   (setq gnus-backlog-hashtb nil
16433         gnus-backlog-articles nil))
16434
16435 (defun gnus-backlog-enter-article (group number buffer)
16436   (gnus-backlog-setup)
16437   (let ((ident (intern (concat group ":" (int-to-string number))
16438                        gnus-backlog-hashtb))
16439         b)
16440     (if (memq ident gnus-backlog-articles)
16441         () ; It's already kept.
16442       ;; Remove the oldest article, if necessary.
16443       (and (numberp gnus-keep-backlog)
16444            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16445            (gnus-backlog-remove-oldest-article))
16446       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16447       ;; Insert the new article.
16448       (save-excursion
16449         (set-buffer (gnus-backlog-buffer))
16450         (let (buffer-read-only)
16451           (goto-char (point-max))
16452           (or (bolp) (insert "\n"))
16453           (setq b (point))
16454           (insert-buffer-substring buffer)
16455           ;; Tag the beginning of the article with the ident.
16456           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16457
16458 (defun gnus-backlog-remove-oldest-article ()
16459   (save-excursion
16460     (set-buffer (gnus-backlog-buffer))
16461     (goto-char (point-min))
16462     (if (zerop (buffer-size))
16463         () ; The buffer is empty.
16464       (let ((ident (get-text-property (point) 'gnus-backlog))
16465             buffer-read-only)
16466         ;; Remove the ident from the list of articles.
16467         (when ident
16468           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16469         ;; Delete the article itself.
16470         (delete-region
16471          (point) (next-single-property-change
16472                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16473
16474 (defun gnus-backlog-request-article (group number buffer)
16475   (when (numberp number)
16476     (gnus-backlog-setup)
16477     (let ((ident (intern (concat group ":" (int-to-string number))
16478                          gnus-backlog-hashtb))
16479           beg end)
16480       (when (memq ident gnus-backlog-articles)
16481         ;; It was in the backlog.
16482         (save-excursion
16483           (set-buffer (gnus-backlog-buffer))
16484           (if (not (setq beg (text-property-any
16485                               (point-min) (point-max) 'gnus-backlog
16486                               ident)))
16487               ;; It wasn't in the backlog after all.
16488               (ignore
16489                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16490             ;; Find the end (i. e., the beginning of the next article).
16491             (setq end
16492                   (next-single-property-change
16493                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16494         (let ((buffer-read-only nil))
16495           (erase-buffer)
16496           (insert-buffer-substring gnus-backlog-buffer beg end)
16497           t)))))
16498
16499 ;; Allow redefinition of Gnus functions.
16500
16501 (gnus-ems-redefine)
16502
16503 (provide 'gnus)
16504
16505 ;;; gnus.el ends here