*** 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.  
1353 Its use is due to the bogus appearance that .newsrc was modified on
1354 disc.")
1355
1356 ;; Hooks.
1357
1358 (defvar gnus-group-mode-hook nil
1359   "*A hook for Gnus group mode.")
1360
1361 (defvar gnus-summary-mode-hook nil
1362   "*A hook for Gnus summary mode.
1363 This hook is run before any variables are set in the summary buffer.")
1364
1365 (defvar gnus-article-mode-hook nil
1366   "*A hook for Gnus article mode.")
1367
1368 (defvar gnus-summary-prepare-exit-hook nil
1369   "*A hook called when preparing to exit from the summary buffer.
1370 It calls `gnus-summary-expire-articles' by default.")
1371 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1372
1373 (defvar gnus-summary-exit-hook nil
1374   "*A hook called on exit from the summary buffer.")
1375
1376 (defvar gnus-group-catchup-group-hook nil
1377   "*A hook run when catching up a group from the group buffer.")
1378
1379 (defvar gnus-open-server-hook nil
1380   "*A hook called just before opening connection to the news server.")
1381
1382 (defvar gnus-load-hook nil
1383   "*A hook run while Gnus is loaded.")
1384
1385 (defvar gnus-startup-hook nil
1386   "*A hook called at startup.
1387 This hook is called after Gnus is connected to the NNTP server.")
1388
1389 (defvar gnus-get-new-news-hook nil
1390   "*A hook run just before Gnus checks for new news.")
1391
1392 (defvar gnus-after-getting-new-news-hook nil
1393   "*A hook run after Gnus checks for new news.")
1394
1395 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1396   "*A function that is called to generate the group buffer.
1397 The function is called with three arguments: The first is a number;
1398 all group with a level less or equal to that number should be listed,
1399 if the second is non-nil, empty groups should also be displayed.  If
1400 the third is non-nil, it is a number.  No groups with a level lower
1401 than this number should be displayed.
1402
1403 The only current function implemented is `gnus-group-prepare-flat'.")
1404
1405 (defvar gnus-group-prepare-hook nil
1406   "*A hook called after the group buffer has been generated.
1407 If you want to modify the group buffer, you can use this hook.")
1408
1409 (defvar gnus-summary-prepare-hook nil
1410   "*A hook called after the summary buffer has been generated.
1411 If you want to modify the summary buffer, you can use this hook.")
1412
1413 (defvar gnus-summary-generate-hook nil
1414   "*A hook run just before generating the summary buffer.
1415 This hook is commonly used to customize threading variables and the
1416 like.")
1417
1418 (defvar gnus-article-prepare-hook nil
1419   "*A hook called after an article has been prepared in the article buffer.
1420 If you want to run a special decoding program like nkf, use this hook.")
1421
1422 ;(defvar gnus-article-display-hook nil
1423 ;  "*A hook called after the article is displayed in the article buffer.
1424 ;The hook is designed to change the contents of the article
1425 ;buffer.  Typical functions that this hook may contain are
1426 ;`gnus-article-hide-headers' (hide selected headers),
1427 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1428 ;`gnus-article-hide-signature' (hide signature) and
1429 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1432 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1433
1434 (defvar gnus-article-x-face-command
1435   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1436   "String or function to be executed to display an X-Face header.
1437 If it is a string, the command will be executed in a sub-shell
1438 asynchronously.  The compressed face will be piped to this command.")
1439
1440 (defvar gnus-article-x-face-too-ugly nil
1441   "Regexp matching posters whose face shouldn't be shown automatically.")
1442
1443 (defvar gnus-select-group-hook nil
1444   "*A hook called when a newsgroup is selected.
1445
1446 If you'd like to simplify subjects like the
1447 `gnus-summary-next-same-subject' command does, you can use the
1448 following hook:
1449
1450  (setq gnus-select-group-hook
1451       (list
1452         (lambda ()
1453           (mapcar (lambda (header)
1454                      (mail-header-set-subject
1455                       header
1456                       (gnus-simplify-subject
1457                        (mail-header-subject header) 're-only)))
1458                   gnus-newsgroup-headers))))")
1459
1460 (defvar gnus-select-article-hook nil
1461   "*A hook called when an article is selected.")
1462
1463 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1464   "*A hook called to apply kill files to a group.
1465 This hook is intended to apply a kill file to the selected newsgroup.
1466 The function `gnus-apply-kill-file' is called by default.
1467
1468 Since a general kill file is too heavy to use only for a few
1469 newsgroups, I recommend you to use a lighter hook function.  For
1470 example, if you'd like to apply a kill file to articles which contains
1471 a string `rmgroup' in subject in newsgroup `control', you can use the
1472 following hook:
1473
1474  (setq gnus-apply-kill-hook
1475       (list
1476         (lambda ()
1477           (cond ((string-match \"control\" gnus-newsgroup-name)
1478                  (gnus-kill \"Subject\" \"rmgroup\")
1479                  (gnus-expunge \"X\"))))))")
1480
1481 (defvar gnus-visual-mark-article-hook
1482   (list 'gnus-highlight-selected-summary)
1483   "*Hook run after selecting an article in the summary buffer.
1484 It is meant to be used for highlighting the article in some way.  It
1485 is not run if `gnus-visual' is nil.")
1486
1487 (defvar gnus-parse-headers-hook nil
1488   "*A hook called before parsing the headers.")
1489
1490 (defvar gnus-exit-group-hook nil
1491   "*A hook called when exiting (not quitting) summary mode.")
1492
1493 (defvar gnus-suspend-gnus-hook nil
1494   "*A hook called when suspending (not exiting) Gnus.")
1495
1496 (defvar gnus-exit-gnus-hook nil
1497   "*A hook called when exiting Gnus.")
1498
1499 (defvar gnus-after-exiting-gnus-hook nil
1500   "*A hook called after exiting Gnus.")
1501
1502 (defvar gnus-save-newsrc-hook nil
1503   "*A hook called before saving any of the newsrc files.")
1504
1505 (defvar gnus-save-quick-newsrc-hook nil
1506   "*A hook called just before saving the quick newsrc file.
1507 Can be used to turn version control on or off.")
1508
1509 (defvar gnus-save-standard-newsrc-hook nil
1510   "*A hook called just before saving the standard newsrc file.
1511 Can be used to turn version control on or off.")
1512
1513 (defvar gnus-summary-update-hook
1514   (list 'gnus-summary-highlight-line)
1515   "*A hook called when a summary line is changed.
1516 The hook will not be called if `gnus-visual' is nil.
1517
1518 The default function `gnus-summary-highlight-line' will
1519 highlight the line according to the `gnus-summary-highlight'
1520 variable.")
1521
1522 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1523   "*A hook called when a group line is changed.
1524 The hook will not be called if `gnus-visual' is nil.
1525
1526 The default function `gnus-group-highlight-line' will
1527 highlight the line according to the `gnus-group-highlight'
1528 variable.")
1529
1530 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1531   "*A hook called when an article is selected for the first time.
1532 The hook is intended to mark an article as read (or unread)
1533 automatically when it is selected.")
1534
1535 (defvar gnus-group-change-level-function nil
1536   "Function run when a group level is changed.
1537 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1538
1539 ;; Remove any hilit infestation.
1540 (add-hook 'gnus-startup-hook
1541           (lambda ()
1542             (remove-hook 'gnus-summary-prepare-hook
1543                          'hilit-rehighlight-buffer-quietly)
1544             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1545             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1546             (remove-hook 'gnus-article-prepare-hook
1547                          'hilit-rehighlight-buffer-quietly)))
1548
1549 \f
1550 ;; Internal variables
1551
1552 (defvar gnus-server-alist nil
1553   "List of available servers.")
1554
1555 (defvar gnus-group-indentation-function nil)
1556
1557 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1558
1559 (defvar gnus-goto-missing-group-function nil)
1560
1561 (defvar gnus-override-subscribe-method nil)
1562
1563 (defvar gnus-group-goto-next-group-function nil
1564   "Function to override finding the next group after listing groups.")
1565
1566 (defconst gnus-article-mark-lists
1567   '((marked . tick) (replied . reply)
1568     (expirable . expire) (killed . killed)
1569     (bookmarks . bookmark) (dormant . dormant)
1570     (scored . score) (saved . save)
1571     (cached . cache)
1572     ))
1573
1574 ;; Avoid highlighting in kill files.
1575 (defvar gnus-summary-inhibit-highlight nil)
1576 (defvar gnus-newsgroup-selected-overlay nil)
1577
1578 (defvar gnus-inhibit-hiding nil)
1579 (defvar gnus-group-indentation "")
1580 (defvar gnus-inhibit-limiting nil)
1581 (defvar gnus-created-frames nil)
1582
1583 (defvar gnus-article-mode-map nil)
1584 (defvar gnus-dribble-buffer nil)
1585 (defvar gnus-headers-retrieved-by nil)
1586 (defvar gnus-article-reply nil)
1587 (defvar gnus-override-method nil)
1588 (defvar gnus-article-check-size nil)
1589
1590 (defvar gnus-current-score-file nil)
1591 (defvar gnus-newsgroup-adaptive-score-file nil)
1592 (defvar gnus-scores-exclude-files nil)
1593
1594 (defvar gnus-opened-servers nil)
1595
1596 (defvar gnus-current-move-group nil)
1597
1598 (defvar gnus-newsgroup-dependencies nil)
1599 (defvar gnus-newsgroup-async nil)
1600 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1601
1602 (defvar gnus-newsgroup-adaptive nil)
1603
1604 (defvar gnus-summary-display-table nil)
1605 (defvar gnus-summary-display-article-function nil)
1606
1607 (defvar gnus-summary-highlight-line-function nil
1608   "Function called after highlighting a summary line.")
1609
1610 (defvar gnus-group-line-format-alist
1611   `((?M gnus-tmp-marked-mark ?c)
1612     (?S gnus-tmp-subscribed ?c)
1613     (?L gnus-tmp-level ?d)
1614     (?N (cond ((eq number t) "*" )
1615               ((numberp number) 
1616                (int-to-string
1617                 (+ number
1618                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1619                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1620               (t number)) ?s)
1621     (?R gnus-tmp-number-of-read ?s)
1622     (?t gnus-tmp-number-total ?d)
1623     (?y gnus-tmp-number-of-unread ?s)
1624     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1625     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1626     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1627            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1628     (?g gnus-tmp-group ?s)
1629     (?G gnus-tmp-qualified-group ?s)
1630     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1631     (?D gnus-tmp-newsgroup-description ?s)
1632     (?o gnus-tmp-moderated ?c)
1633     (?O gnus-tmp-moderated-string ?s)
1634     (?p gnus-tmp-process-marked ?c)
1635     (?s gnus-tmp-news-server ?s)
1636     (?n gnus-tmp-news-method ?s)
1637     (?P gnus-group-indentation ?s)
1638     (?l gnus-tmp-grouplens ?s)
1639     (?z gnus-tmp-news-method-string ?s)
1640     (?u gnus-tmp-user-defined ?s)))
1641
1642 (defvar gnus-summary-line-format-alist
1643   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1644     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1645     (?s gnus-tmp-subject-or-nil ?s)
1646     (?n gnus-tmp-name ?s)
1647     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1648         ?s)
1649     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1650             gnus-tmp-from) ?s)
1651     (?F gnus-tmp-from ?s)
1652     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1653     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1654     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1655     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1656     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1657     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1658     (?L gnus-tmp-lines ?d)
1659     (?I gnus-tmp-indentation ?s)
1660     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1661     (?R gnus-tmp-replied ?c)
1662     (?\[ gnus-tmp-opening-bracket ?c)
1663     (?\] gnus-tmp-closing-bracket ?c)
1664     (?\> (make-string gnus-tmp-level ? ) ?s)
1665     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1666     (?i gnus-tmp-score ?d)
1667     (?z gnus-tmp-score-char ?c)
1668     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1669     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1670     (?U gnus-tmp-unread ?c)
1671     (?t (gnus-summary-number-of-articles-in-thread
1672          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1673         ?d)
1674     (?e (gnus-summary-number-of-articles-in-thread
1675          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1676         ?c)
1677     (?u gnus-tmp-user-defined ?s))
1678   "An alist of format specifications that can appear in summary lines,
1679 and what variables they correspond with, along with the type of the
1680 variable (string, integer, character, etc).")
1681
1682 (defvar gnus-summary-dummy-line-format-alist
1683   `((?S gnus-tmp-subject ?s)
1684     (?N gnus-tmp-number ?d)
1685     (?u gnus-tmp-user-defined ?s)))
1686
1687 (defvar gnus-summary-mode-line-format-alist
1688   `((?G gnus-tmp-group-name ?s)
1689     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1690     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1691     (?A gnus-tmp-article-number ?d)
1692     (?Z gnus-tmp-unread-and-unselected ?s)
1693     (?V gnus-version ?s)
1694     (?U gnus-tmp-unread ?d)
1695     (?S gnus-tmp-subject ?s)
1696     (?e gnus-tmp-unselected ?d)
1697     (?u gnus-tmp-user-defined ?s)
1698     (?d (length gnus-newsgroup-dormant) ?d)
1699     (?t (length gnus-newsgroup-marked) ?d)
1700     (?r (length gnus-newsgroup-reads) ?d)
1701     (?E gnus-newsgroup-expunged-tally ?d)
1702     (?s (gnus-current-score-file-nondirectory) ?s)))
1703
1704 (defvar gnus-article-mode-line-format-alist
1705   gnus-summary-mode-line-format-alist)
1706
1707 (defvar gnus-group-mode-line-format-alist
1708   `((?S gnus-tmp-news-server ?s)
1709     (?M gnus-tmp-news-method ?s)
1710     (?u gnus-tmp-user-defined ?s)))
1711
1712 (defvar gnus-have-read-active-file nil)
1713
1714 (defconst gnus-maintainer
1715   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1716   "The mail address of the Gnus maintainers.")
1717
1718 (defconst gnus-version "September Gnus v0.53"
1719   "Version number for this version of Gnus.")
1720
1721 (defvar gnus-info-nodes
1722   '((gnus-group-mode            "(gnus)The Group Buffer")
1723     (gnus-summary-mode          "(gnus)The Summary Buffer")
1724     (gnus-article-mode          "(gnus)The Article Buffer"))
1725   "Assoc list of major modes and related Info nodes.")
1726
1727 (defvar gnus-group-buffer "*Group*")
1728 (defvar gnus-summary-buffer "*Summary*")
1729 (defvar gnus-article-buffer "*Article*")
1730 (defvar gnus-server-buffer "*Server*")
1731
1732 (defvar gnus-work-buffer " *gnus work*")
1733
1734 (defvar gnus-original-article-buffer " *Original Article*")
1735 (defvar gnus-original-article nil)
1736
1737 (defvar gnus-buffer-list nil
1738   "Gnus buffers that should be killed on exit.")
1739
1740 (defvar gnus-slave nil
1741   "Whether this Gnus is a slave or not.")
1742
1743 (defvar gnus-variable-list
1744   '(gnus-newsrc-options gnus-newsrc-options-n
1745     gnus-newsrc-last-checked-date
1746     gnus-newsrc-alist gnus-server-alist
1747     gnus-killed-list gnus-zombie-list
1748     gnus-topic-topology gnus-topic-alist
1749     gnus-format-specs)
1750   "Gnus variables saved in the quick startup file.")
1751
1752 (defvar gnus-newsrc-options nil
1753   "Options line in the .newsrc file.")
1754
1755 (defvar gnus-newsrc-options-n nil
1756   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1757
1758 (defvar gnus-newsrc-last-checked-date nil
1759   "Date Gnus last asked server for new newsgroups.")
1760
1761 (defvar gnus-topic-topology nil
1762   "The complete topic hierarchy.")
1763
1764 (defvar gnus-topic-alist nil
1765   "The complete topic-group alist.")
1766
1767 (defvar gnus-newsrc-alist nil
1768   "Assoc list of read articles.
1769 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1770
1771 (defvar gnus-newsrc-hashtb nil
1772   "Hashtable of gnus-newsrc-alist.")
1773
1774 (defvar gnus-killed-list nil
1775   "List of killed newsgroups.")
1776
1777 (defvar gnus-killed-hashtb nil
1778   "Hash table equivalent of gnus-killed-list.")
1779
1780 (defvar gnus-zombie-list nil
1781   "List of almost dead newsgroups.")
1782
1783 (defvar gnus-description-hashtb nil
1784   "Descriptions of newsgroups.")
1785
1786 (defvar gnus-list-of-killed-groups nil
1787   "List of newsgroups that have recently been killed by the user.")
1788
1789 (defvar gnus-active-hashtb nil
1790   "Hashtable of active articles.")
1791
1792 (defvar gnus-moderated-list nil
1793   "List of moderated newsgroups.")
1794
1795 (defvar gnus-group-marked nil)
1796
1797 (defvar gnus-current-startup-file nil
1798   "Startup file for the current host.")
1799
1800 (defvar gnus-last-search-regexp nil
1801   "Default regexp for article search command.")
1802
1803 (defvar gnus-last-shell-command nil
1804   "Default shell command on article.")
1805
1806 (defvar gnus-current-select-method nil
1807   "The current method for selecting a newsgroup.")
1808
1809 (defvar gnus-group-list-mode nil)
1810
1811 (defvar gnus-article-internal-prepare-hook nil)
1812
1813 (defvar gnus-newsgroup-name nil)
1814 (defvar gnus-newsgroup-begin nil)
1815 (defvar gnus-newsgroup-end nil)
1816 (defvar gnus-newsgroup-last-rmail nil)
1817 (defvar gnus-newsgroup-last-mail nil)
1818 (defvar gnus-newsgroup-last-folder nil)
1819 (defvar gnus-newsgroup-last-file nil)
1820 (defvar gnus-newsgroup-auto-expire nil)
1821 (defvar gnus-newsgroup-active nil)
1822
1823 (defvar gnus-newsgroup-data nil)
1824 (defvar gnus-newsgroup-data-reverse nil)
1825 (defvar gnus-newsgroup-limit nil)
1826 (defvar gnus-newsgroup-limits nil)
1827
1828 (defvar gnus-newsgroup-unreads nil
1829   "List of unread articles in the current newsgroup.")
1830
1831 (defvar gnus-newsgroup-unselected nil
1832   "List of unselected unread articles in the current newsgroup.")
1833
1834 (defvar gnus-newsgroup-reads nil
1835   "Alist of read articles and article marks in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-expunged-tally nil)
1838
1839 (defvar gnus-newsgroup-marked nil
1840   "List of ticked articles in the current newsgroup (a subset of unread art).")
1841
1842 (defvar gnus-newsgroup-killed nil
1843   "List of ranges of articles that have been through the scoring process.")
1844
1845 (defvar gnus-newsgroup-cached nil
1846   "List of articles that come from the article cache.")
1847
1848 (defvar gnus-newsgroup-saved nil
1849   "List of articles that have been saved.")
1850
1851 (defvar gnus-newsgroup-kill-headers nil)
1852
1853 (defvar gnus-newsgroup-replied nil
1854   "List of articles that have been replied to in the current newsgroup.")
1855
1856 (defvar gnus-newsgroup-expirable nil
1857   "List of articles in the current newsgroup that can be expired.")
1858
1859 (defvar gnus-newsgroup-processable nil
1860   "List of articles in the current newsgroup that can be processed.")
1861
1862 (defvar gnus-newsgroup-bookmarks nil
1863   "List of articles in the current newsgroup that have bookmarks.")
1864
1865 (defvar gnus-newsgroup-dormant nil
1866   "List of dormant articles in the current newsgroup.")
1867
1868 (defvar gnus-newsgroup-scored nil
1869   "List of scored articles in the current newsgroup.")
1870
1871 (defvar gnus-newsgroup-headers nil
1872   "List of article headers in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-threads nil)
1875
1876 (defvar gnus-newsgroup-prepared nil
1877   "Whether the current group has been prepared properly.")
1878
1879 (defvar gnus-newsgroup-ancient nil
1880   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-sparse nil)
1883
1884 (defvar gnus-current-article nil)
1885 (defvar gnus-article-current nil)
1886 (defvar gnus-current-headers nil)
1887 (defvar gnus-have-all-headers nil)
1888 (defvar gnus-last-article nil)
1889 (defvar gnus-newsgroup-history nil)
1890 (defvar gnus-current-kill-article nil)
1891
1892 ;; Save window configuration.
1893 (defvar gnus-prev-winconf nil)
1894
1895 (defvar gnus-summary-mark-positions nil)
1896 (defvar gnus-group-mark-positions nil)
1897
1898 (defvar gnus-reffed-article-number nil)
1899
1900 ;;; Let the byte-compiler know that we know about this variable.
1901 (defvar rmail-default-rmail-file)
1902
1903 (defvar gnus-cache-removable-articles nil)
1904
1905 (defvar gnus-dead-summary nil)
1906
1907 (defconst gnus-summary-local-variables
1908   '(gnus-newsgroup-name
1909     gnus-newsgroup-begin gnus-newsgroup-end
1910     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1911     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1912     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1913     gnus-newsgroup-unselected gnus-newsgroup-marked
1914     gnus-newsgroup-reads gnus-newsgroup-saved
1915     gnus-newsgroup-replied gnus-newsgroup-expirable
1916     gnus-newsgroup-processable gnus-newsgroup-killed
1917     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1918     gnus-newsgroup-headers gnus-newsgroup-threads
1919     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1920     gnus-current-article gnus-current-headers gnus-have-all-headers
1921     gnus-last-article gnus-article-internal-prepare-hook
1922     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1923     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1924     gnus-newsgroup-async 
1925     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1926     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1927     gnus-newsgroup-history gnus-newsgroup-ancient
1928     gnus-newsgroup-sparse
1929     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1930     gnus-newsgroup-adaptive-score-file
1931     (gnus-newsgroup-expunged-tally . 0)
1932     gnus-cache-removable-articles gnus-newsgroup-cached
1933     gnus-newsgroup-data gnus-newsgroup-data-reverse
1934     gnus-newsgroup-limit gnus-newsgroup-limits)
1935   "Variables that are buffer-local to the summary buffers.")
1936
1937 (defconst gnus-bug-message
1938   "Sending a bug report to the Gnus Towers.
1939 ========================================
1940
1941 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1942 be sent to the Gnus Bug Exterminators.
1943
1944 At the bottom of the buffer you'll see lots of variable settings.
1945 Please do not delete those.  They will tell the Bug People what your
1946 environment is, so that it will be easier to locate the bugs.
1947
1948 If you have found a bug that makes Emacs go \"beep\", set
1949 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1950 and include the backtrace in your bug report.
1951
1952 Please describe the bug in annoying, painstaking detail.
1953
1954 Thank you for your help in stamping out bugs.
1955 ")
1956
1957 ;;; End of variables.
1958
1959 ;; Define some autoload functions Gnus might use.
1960 (eval-and-compile
1961
1962   ;; This little mapcar goes through the list below and marks the
1963   ;; symbols in question as autoloaded functions.
1964   (mapcar
1965    (lambda (package)
1966      (let ((interactive (nth 1 (memq ':interactive package))))
1967        (mapcar
1968         (lambda (function)
1969           (let (keymap)
1970             (when (consp function)
1971               (setq keymap (car (memq 'keymap function)))
1972               (setq function (car function)))
1973             (autoload function (car package) nil interactive keymap)))
1974         (if (eq (nth 1 package) ':interactive)
1975             (cdddr package)
1976           (cdr package)))))
1977    '(("metamail" metamail-buffer)
1978      ("info" Info-goto-node)
1979      ("hexl" hexl-hex-string-to-integer)
1980      ("pp" pp pp-to-string pp-eval-expression)
1981      ("mail-extr" mail-extract-address-components)
1982      ("nnmail" nnmail-split-fancy nnmail-article-group)
1983      ("nnvirtual" nnvirtual-catchup-group)
1984      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1985       timezone-make-sortable-date timezone-make-time-string)
1986      ("sendmail" mail-position-on-field mail-setup)
1987      ("rmailout" rmail-output)
1988      ("rnewspost" news-mail-other-window news-reply-yank-original
1989       news-caesar-buffer-body)
1990      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1991       rmail-show-message)
1992      ("gnus-soup" :interactive t
1993       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1994       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1995      ("nnsoup" nnsoup-pack-replies)
1996      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1997       gnus-Folder-save-name gnus-folder-save-name)
1998      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1999      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2000       gnus-server-make-menu-bar gnus-article-make-menu-bar
2001       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2002       gnus-summary-highlight-line gnus-carpal-setup-buffer
2003       gnus-group-highlight-line
2004       gnus-article-add-button gnus-insert-next-page-button
2005       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2006      ("gnus-vis" :interactive t
2007       gnus-article-push-button gnus-article-press-button
2008       gnus-article-highlight gnus-article-highlight-some
2009       gnus-article-highlight-headers gnus-article-highlight-signature
2010       gnus-article-add-buttons gnus-article-add-buttons-to-head
2011       gnus-article-next-button gnus-article-prev-button)
2012      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2013       gnus-demon-add-disconnection gnus-demon-add-handler
2014       gnus-demon-remove-handler)
2015      ("gnus-demon" :interactive t
2016       gnus-demon-init gnus-demon-cancel)
2017      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2018       gnus-tree-open gnus-tree-close)
2019      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2020       gnus-nocem-unwanted-article-p)
2021      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2022      ("gnus-srvr" gnus-browse-foreign-server)
2023      ("gnus-cite" :interactive t
2024       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2025       gnus-article-hide-citation gnus-article-fill-cited-article)
2026      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2027       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2028       gnus-execute gnus-expunge)
2029      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2030       gnus-cache-possibly-remove-articles gnus-cache-request-article
2031       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2032       gnus-cache-enter-remove-article gnus-cached-article-p
2033       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2034      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2035       gnus-cache-remove-article)
2036      ("gnus-score" :interactive t
2037       gnus-summary-increase-score gnus-summary-lower-score
2038       gnus-score-flush-cache gnus-score-close
2039       gnus-score-raise-same-subject-and-select
2040       gnus-score-raise-same-subject gnus-score-default
2041       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2042       gnus-score-lower-same-subject gnus-score-lower-thread
2043       gnus-possibly-score-headers)
2044      ("gnus-score"
2045       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2046       gnus-current-score-file-nondirectory gnus-score-adaptive
2047       gnus-score-find-trace gnus-score-file-name)
2048      ("gnus-edit" :interactive t gnus-score-customize)
2049      ("gnus-topic" :interactive t gnus-topic-mode)
2050      ("gnus-topic" gnus-topic-remove-group)
2051      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2052      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2053      ("gnus-uu" :interactive t
2054       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2055       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2056       gnus-uu-mark-by-regexp gnus-uu-mark-all
2057       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2058       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2059       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2060       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2061       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2062       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2063       gnus-uu-decode-binhex-view)
2064      ("gnus-msg" (gnus-summary-send-map keymap)
2065       gnus-mail-yank-original gnus-mail-send-and-exit
2066       gnus-sendmail-setup-mail gnus-article-mail
2067       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2068      ("gnus-msg" :interactive t
2069       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2070       gnus-summary-followup gnus-summary-followup-with-original
2071       gnus-summary-followup-and-reply
2072       gnus-summary-followup-and-reply-with-original
2073       gnus-summary-cancel-article gnus-summary-supersede-article
2074       gnus-post-news gnus-inews-news gnus-cancel-news
2075       gnus-summary-reply gnus-summary-reply-with-original
2076       gnus-summary-mail-forward gnus-summary-mail-other-window
2077       gnus-bug)
2078      ("gnus-picon" :interactive t gnus-article-display-picons
2079       gnus-group-display-picons gnus-picons-article-display-x-face)
2080      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2081       gnus-grouplens-mode)
2082      ("gnus-vm" gnus-vm-mail-setup)
2083      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2084       gnus-summary-save-article-vm gnus-yank-article))))
2085
2086 \f
2087
2088 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2089 ;; If you want the cursor to go somewhere else, set these two
2090 ;; functions in some startup hook to whatever you want.
2091 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2092 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2093
2094 ;;; Various macros and substs.
2095
2096 (defun gnus-header-from (header)
2097   (mail-header-from header))
2098
2099 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2100   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2101   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2102     `(let ((,tempvar (selected-window)))
2103        (unwind-protect
2104            (progn
2105              (pop-to-buffer ,buffer)
2106              ,@forms)
2107          (select-window ,tempvar)))))
2108
2109 (defmacro gnus-gethash (string hashtable)
2110   "Get hash value of STRING in HASHTABLE."
2111   `(symbol-value (intern-soft ,string ,hashtable)))
2112
2113 (defmacro gnus-sethash (string value hashtable)
2114   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2115   `(set (intern ,string ,hashtable) ,value))
2116
2117 (defmacro gnus-intern-safe (string hashtable)
2118   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2119   `(let ((symbol (intern ,string ,hashtable)))
2120      (or (boundp symbol)
2121          (set symbol nil))
2122      symbol))
2123
2124 (defmacro gnus-group-unread (group)
2125   "Get the currently computed number of unread articles in GROUP."
2126   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2127
2128 (defmacro gnus-group-entry (group)
2129   "Get the newsrc entry for GROUP."
2130   `(gnus-gethash ,group gnus-newsrc-hashtb))
2131
2132 (defmacro gnus-active (group)
2133   "Get active info on GROUP."
2134   `(gnus-gethash ,group gnus-active-hashtb))
2135
2136 (defmacro gnus-set-active (group active)
2137   "Set GROUP's active info."
2138   `(gnus-sethash ,group ,active gnus-active-hashtb))
2139
2140 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2141 ;;   function `substring' might cut on a middle of multi-octet
2142 ;;   character.
2143 (defun gnus-truncate-string (str width)
2144   (substring str 0 width))
2145
2146 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2147 ;; to limit the length of a string.  This function is necessary since
2148 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2149 (defsubst gnus-limit-string (str width)
2150   (if (> (length str) width)
2151       (substring str 0 width)
2152     str))
2153
2154 (defsubst gnus-simplify-subject-re (subject)
2155   "Remove \"Re:\" from subject lines."
2156   (if (string-match "^[Rr][Ee]: *" subject)
2157       (substring subject (match-end 0))
2158     subject))
2159
2160 (defsubst gnus-functionp (form)
2161   "Return non-nil if FORM is funcallable."
2162   (or (and (symbolp form) (fboundp form))
2163       (and (listp form) (eq (car form) 'lambda))))
2164
2165 (defsubst gnus-goto-char (point)
2166   (and point (goto-char point)))
2167
2168 (defmacro gnus-buffer-exists-p (buffer)
2169   `(let ((buffer ,buffer))
2170      (and buffer
2171           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2172                    buffer))))
2173
2174 (defmacro gnus-kill-buffer (buffer)
2175   `(let ((buf ,buffer))
2176      (if (gnus-buffer-exists-p buf)
2177          (kill-buffer buf))))
2178
2179 (defsubst gnus-point-at-bol ()
2180   "Return point at the beginning of the line."
2181   (let ((p (point)))
2182     (beginning-of-line)
2183     (prog1
2184         (point)
2185       (goto-char p))))
2186
2187 (defsubst gnus-point-at-eol ()
2188   "Return point at the end of the line."
2189   (let ((p (point)))
2190     (end-of-line)
2191     (prog1
2192         (point)
2193       (goto-char p))))
2194
2195 (defun gnus-alive-p ()
2196   "Say whether Gnus is running or not."
2197   (and gnus-group-buffer
2198        (get-buffer gnus-group-buffer)))
2199
2200 ;; Delete the current line (and the next N lines.);
2201 (defmacro gnus-delete-line (&optional n)
2202   `(delete-region (progn (beginning-of-line) (point))
2203                   (progn (forward-line ,(or n 1)) (point))))
2204
2205 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2206 (defvar gnus-init-inhibit nil)
2207 (defun gnus-read-init-file (&optional inhibit-next)
2208   (if gnus-init-inhibit
2209       (setq gnus-init-inhibit nil)
2210     (setq gnus-init-inhibit inhibit-next)
2211     (and gnus-init-file
2212          (or (and (file-exists-p gnus-init-file)
2213                   ;; Don't try to load a directory.
2214                   (not (file-directory-p gnus-init-file)))
2215              (file-exists-p (concat gnus-init-file ".el"))
2216              (file-exists-p (concat gnus-init-file ".elc")))
2217          (condition-case var
2218              (load gnus-init-file nil t)
2219            (error
2220             (error "Error in %s: %s" gnus-init-file var))))))
2221
2222 ;; Info access macros.
2223
2224 (defmacro gnus-info-group (info)
2225   `(nth 0 ,info))
2226 (defmacro gnus-info-rank (info)
2227   `(nth 1 ,info))
2228 (defmacro gnus-info-read (info)
2229   `(nth 2 ,info))
2230 (defmacro gnus-info-marks (info)
2231   `(nth 3 ,info))
2232 (defmacro gnus-info-method (info)
2233   `(nth 4 ,info))
2234 (defmacro gnus-info-params (info)
2235   `(nth 5 ,info))
2236
2237 (defmacro gnus-info-level (info)
2238   `(let ((rank (gnus-info-rank ,info)))
2239      (if (consp rank)
2240          (car rank)
2241        rank)))
2242 (defmacro gnus-info-score (info)
2243   `(let ((rank (gnus-info-rank ,info)))
2244      (or (and (consp rank) (cdr rank)) 0)))
2245
2246 (defmacro gnus-info-set-group (info group)
2247   `(setcar ,info ,group))
2248 (defmacro gnus-info-set-rank (info rank)
2249   `(setcar (nthcdr 1 ,info) ,rank))
2250 (defmacro gnus-info-set-read (info read)
2251   `(setcar (nthcdr 2 ,info) ,read))
2252 (defmacro gnus-info-set-marks (info marks)
2253   `(setcar (nthcdr 3 ,info) ,marks))
2254 (defmacro gnus-info-set-method (info method)
2255   `(setcar (nthcdr 4 ,info) ,method))
2256 (defmacro gnus-info-set-params (info params)
2257   `(setcar (nthcdr 5 ,info) ,params))
2258
2259 (defmacro gnus-info-set-level (info level)
2260   `(let ((rank (cdr ,info)))
2261      (if (consp (car rank))
2262          (setcar (car rank) ,level)
2263        (setcar rank ,level))))
2264 (defmacro gnus-info-set-score (info score)
2265   `(let ((rank (cdr ,info)))
2266      (if (consp (car rank))
2267          (setcdr (car rank) ,score)
2268        (setcar rank (cons (car rank) ,score)))))
2269
2270 (defmacro gnus-get-info (group)
2271   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2272
2273 (defun gnus-byte-code (func)
2274   "Return a form that can be `eval'ed based on FUNC."
2275   (let ((fval (symbol-function func)))
2276     (if (byte-code-function-p fval)
2277         (let ((flist (append fval nil)))
2278           (setcar flist 'byte-code)
2279           flist)
2280       (cons 'progn (cddr fval)))))
2281
2282 ;;; Load the compatability functions.
2283
2284 (require 'gnus-cus)
2285 (require 'gnus-ems)
2286
2287 \f
2288
2289 ;; Format specs.  The chunks below are the machine-generated forms
2290 ;; that are to be evaled as the result of the default format strings.
2291 ;; We write them in here to get them byte-compiled.  That way the
2292 ;; default actions will be quite fast, while still retaining the full
2293 ;; flexibility of the user-defined format specs.
2294
2295 ;; First we have lots of dummy defvars to let the compiler know these
2296 ;; are really dynamic variables.
2297
2298 (defvar gnus-tmp-unread)
2299 (defvar gnus-tmp-replied)
2300 (defvar gnus-tmp-score-char)
2301 (defvar gnus-tmp-indentation)
2302 (defvar gnus-tmp-opening-bracket)
2303 (defvar gnus-tmp-lines)
2304 (defvar gnus-tmp-name)
2305 (defvar gnus-tmp-closing-bracket)
2306 (defvar gnus-tmp-subject-or-nil)
2307 (defvar gnus-tmp-subject)
2308 (defvar gnus-tmp-marked)
2309 (defvar gnus-tmp-marked-mark)
2310 (defvar gnus-tmp-subscribed)
2311 (defvar gnus-tmp-process-marked)
2312 (defvar gnus-tmp-number-of-unread)
2313 (defvar gnus-tmp-group-name)
2314 (defvar gnus-tmp-group)
2315 (defvar gnus-tmp-article-number)
2316 (defvar gnus-tmp-unread-and-unselected)
2317 (defvar gnus-tmp-news-method)
2318 (defvar gnus-tmp-news-server)
2319 (defvar gnus-tmp-article-number)
2320 (defvar gnus-mouse-face)
2321 (defvar gnus-mouse-face-prop)
2322
2323 (defun gnus-summary-line-format-spec ()
2324   (insert gnus-tmp-unread gnus-tmp-replied
2325           gnus-tmp-score-char gnus-tmp-indentation)
2326   (put-text-property
2327    (point)
2328    (progn
2329      (insert
2330       gnus-tmp-opening-bracket
2331       (format "%4d: %-20s"
2332               gnus-tmp-lines
2333               (if (> (length gnus-tmp-name) 20)
2334                   (substring gnus-tmp-name 0 20)
2335                 gnus-tmp-name))
2336       gnus-tmp-closing-bracket)
2337      (point))
2338    gnus-mouse-face-prop gnus-mouse-face)
2339   (insert " " gnus-tmp-subject-or-nil "\n"))
2340
2341 (defvar gnus-summary-line-format-spec
2342   (gnus-byte-code 'gnus-summary-line-format-spec))
2343
2344 (defun gnus-summary-dummy-line-format-spec ()
2345   (insert "*  ")
2346   (put-text-property
2347    (point)
2348    (progn
2349      (insert ":                          :")
2350      (point))
2351    gnus-mouse-face-prop gnus-mouse-face)
2352   (insert " " gnus-tmp-subject "\n"))
2353
2354 (defvar gnus-summary-dummy-line-format-spec
2355   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2356
2357 (defun gnus-group-line-format-spec ()
2358   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2359           gnus-tmp-process-marked
2360           gnus-group-indentation
2361           (format "%5s: " gnus-tmp-number-of-unread))
2362   (put-text-property
2363    (point)
2364    (progn
2365      (insert gnus-tmp-group "\n")
2366      (1- (point)))
2367    gnus-mouse-face-prop gnus-mouse-face))
2368 (defvar gnus-group-line-format-spec
2369   (gnus-byte-code 'gnus-group-line-format-spec))
2370
2371 (defvar gnus-format-specs
2372   `((version . ,emacs-version)
2373     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2374     (summary-dummy ,gnus-summary-dummy-line-format
2375                    ,gnus-summary-dummy-line-format-spec)
2376     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2377
2378 (defvar gnus-article-mode-line-format-spec nil)
2379 (defvar gnus-summary-mode-line-format-spec nil)
2380 (defvar gnus-group-mode-line-format-spec nil)
2381
2382 ;;; Phew.  All that gruft is over, fortunately.
2383
2384 \f
2385 ;;;
2386 ;;; Gnus Utility Functions
2387 ;;;
2388
2389 (defun gnus-extract-address-components (from)
2390   (let (name address)
2391     ;; First find the address - the thing with the @ in it.  This may
2392     ;; not be accurate in mail addresses, but does the trick most of
2393     ;; the time in news messages.
2394     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2395         (setq address (substring from (match-beginning 0) (match-end 0))))
2396     ;; Then we check whether the "name <address>" format is used.
2397     (and address
2398          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2399          ;; Linear white space is not required.
2400          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2401          (and (setq name (substring from 0 (match-beginning 0)))
2402               ;; Strip any quotes from the name.
2403               (string-match "\".*\"" name)
2404               (setq name (substring name 1 (1- (match-end 0))))))
2405     ;; If not, then "address (name)" is used.
2406     (or name
2407         (and (string-match "(.+)" from)
2408              (setq name (substring from (1+ (match-beginning 0))
2409                                    (1- (match-end 0)))))
2410         (and (string-match "()" from)
2411              (setq name address))
2412         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2413         ;; XOVER might not support folded From headers.
2414         (and (string-match "(.*" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (match-end 0)))))
2417     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2418     (list (or name from) (or address from))))
2419
2420 (defun gnus-fetch-field (field)
2421   "Return the value of the header FIELD of current article."
2422   (save-excursion
2423     (save-restriction
2424       (let ((case-fold-search t))
2425         (nnheader-narrow-to-headers)
2426         (mail-fetch-field field)))))
2427
2428 (defun gnus-goto-colon ()
2429   (beginning-of-line)
2430   (search-forward ":" (gnus-point-at-eol) t))
2431
2432 ;;;###autoload
2433 (defun gnus-update-format (var)
2434   "Update the format specification near point."
2435   (interactive
2436    (list
2437     (save-excursion
2438       (eval-defun nil)
2439       ;; Find the end of the current word.
2440       (re-search-forward "[ \t\n]" nil t)
2441       ;; Search backward.
2442       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2443         (match-string 1)))))
2444   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2445                               (match-string 1 var))))
2446          (entry (assq type gnus-format-specs))
2447          value spec)
2448     (when entry
2449       (setq gnus-format-specs (delq entry gnus-format-specs)))
2450     (set
2451      (intern (format "%s-spec" var))
2452      (gnus-parse-format (setq value (symbol-value (intern var)))
2453                         (symbol-value (intern (format "%s-alist" var)))
2454                         (not (string-match "mode" var))))
2455     (setq spec (symbol-value (intern (format "%s-spec" var))))
2456     (push (list type value spec) gnus-format-specs)
2457
2458     (pop-to-buffer "*Gnus Format*")
2459     (erase-buffer)
2460     (lisp-interaction-mode)
2461     (insert (pp-to-string spec))))
2462
2463 (defun gnus-update-format-specifications (&optional force)
2464   "Update all (necessary) format specifications."
2465   ;; Make the indentation array.
2466   (gnus-make-thread-indent-array)
2467
2468   ;; See whether all the stored info needs to be flushed.
2469   (when (or force
2470             (not (equal emacs-version
2471                         (cdr (assq 'version gnus-format-specs)))))
2472     (setq gnus-format-specs nil))
2473
2474   ;; Go through all the formats and see whether they need updating.
2475   (let ((types '(summary summary-dummy group
2476                          summary-mode group-mode article-mode))
2477         new-format entry type val)
2478     (while (setq type (pop types))
2479       ;; Jump to the proper buffer to find out the value of
2480       ;; the variable, if possible.  (It may be buffer-local.)
2481       (save-excursion
2482         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2483               val)
2484           (when (and (boundp buffer)
2485                      (setq val (symbol-value buffer))
2486                      (get-buffer val)
2487                      (buffer-name (get-buffer val)))
2488             (set-buffer (get-buffer val)))
2489           (setq new-format (symbol-value
2490                             (intern (format "gnus-%s-line-format" type))))))
2491       (setq entry (cdr (assq type gnus-format-specs)))
2492       (if (and entry
2493                (equal (car entry) new-format))
2494           ;; Use the old format.
2495           (set (intern (format "gnus-%s-line-format-spec" type))
2496                (cadr entry))
2497         ;; This is a new format.
2498         (setq val
2499               (if (not (stringp new-format))
2500                   ;; This is a function call or something.
2501                   new-format
2502                 ;; This is a "real" format.
2503                 (gnus-parse-format
2504                  new-format
2505                  (symbol-value
2506                   (intern (format "gnus-%s-line-format-alist"
2507                                   (if (eq type 'article-mode)
2508                                       'summary-mode type))))
2509                  (not (string-match "mode$" (symbol-name type))))))
2510         ;; Enter the new format spec into the list.
2511         (if entry
2512             (progn
2513               (setcar (cdr entry) val)
2514               (setcar entry new-format))
2515           (push (list type new-format val) gnus-format-specs))
2516         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2517
2518   (push (cons 'version emacs-version) gnus-format-specs)
2519
2520   (gnus-update-group-mark-positions)
2521   (gnus-update-summary-mark-positions))
2522
2523 (defun gnus-update-summary-mark-positions ()
2524   "Compute where the summary marks are to go."
2525   (save-excursion
2526     (let ((gnus-replied-mark 129)
2527           (gnus-score-below-mark 130)
2528           (gnus-score-over-mark 130)
2529           (thread nil)
2530           (gnus-visual nil)
2531           pos)
2532       (gnus-set-work-buffer)
2533       (gnus-summary-insert-line
2534        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2535       (goto-char (point-min))
2536       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2537                                          (- (point) 2)))))
2538       (goto-char (point-min))
2539       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2540             pos)
2541       (goto-char (point-min))
2542       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2543             pos)
2544       (setq gnus-summary-mark-positions pos))))
2545
2546 (defun gnus-update-group-mark-positions ()
2547   (save-excursion
2548     (let ((gnus-process-mark 128)
2549           (gnus-group-marked '("dummy.group")))
2550       (gnus-set-active "dummy.group" '(0 . 0))
2551       (gnus-set-work-buffer)
2552       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2553       (goto-char (point-min))
2554       (setq gnus-group-mark-positions
2555             (list (cons 'process (and (search-forward "\200" nil t)
2556                                       (- (point) 2))))))))
2557
2558 (defvar gnus-mouse-face-0 'highlight)
2559 (defvar gnus-mouse-face-1 'highlight)
2560 (defvar gnus-mouse-face-2 'highlight)
2561 (defvar gnus-mouse-face-3 'highlight)
2562 (defvar gnus-mouse-face-4 'highlight)
2563
2564 (defun gnus-mouse-face-function (form type)
2565   `(put-text-property
2566     (point) (progn ,@form (point))
2567     gnus-mouse-face-prop
2568     ,(if (equal type 0)
2569          'gnus-mouse-face
2570        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2571
2572 (defvar gnus-face-0 'bold)
2573 (defvar gnus-face-1 'italic)
2574 (defvar gnus-face-2 'bold-italic)
2575 (defvar gnus-face-3 'bold)
2576 (defvar gnus-face-4 'bold)
2577
2578 (defun gnus-face-face-function (form type)
2579   `(put-text-property
2580     (point) (progn ,@form (point))
2581     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2582
2583 (defun gnus-max-width-function (el max-width)
2584   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2585   (if (symbolp el)
2586       `(if (> (length ,el) ,max-width)
2587            (substring ,el 0 ,max-width)
2588          ,el)
2589     `(let ((val (eval ,el)))
2590        (if (numberp val)
2591            (setq val (int-to-string val)))
2592        (if (> (length val) ,max-width)
2593            (substring val 0 ,max-width)
2594          val))))
2595
2596 (defun gnus-parse-format (format spec-alist &optional insert)
2597   ;; This function parses the FORMAT string with the help of the
2598   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2599   ;; string.  If the FORMAT string contains the specifiers %( and %)
2600   ;; the text between them will have the mouse-face text property.
2601   (if (string-match
2602        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2603        format)
2604       (gnus-parse-complex-format format spec-alist)
2605     ;; This is a simple format.
2606     (gnus-parse-simple-format format spec-alist insert)))
2607
2608 (defun gnus-parse-complex-format (format spec-alist)
2609   (save-excursion
2610     (gnus-set-work-buffer)
2611     (insert format)
2612     (goto-char (point-min))
2613     (while (re-search-forward "\"" nil t)
2614       (replace-match "\\\"" nil t))
2615     (goto-char (point-min))
2616     (insert "(\"")
2617     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2618       (let ((number (if (match-beginning 1)
2619                         (match-string 1) "0"))
2620             (delim (aref (match-string 2) 0)))
2621         (if (or (= delim ?\() (= delim ?\{))
2622             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2623                                    " " number " \""))
2624           (replace-match "\")\""))))
2625     (goto-char (point-max))
2626     (insert "\")")
2627     (goto-char (point-min))
2628     (let ((form (read (current-buffer))))
2629       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2630
2631 (defun gnus-complex-form-to-spec (form spec-alist)
2632   (delq nil
2633         (mapcar
2634          (lambda (sform)
2635            (if (stringp sform)
2636                (gnus-parse-simple-format sform spec-alist t)
2637              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2638                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2639                       (nth 1 sform))))
2640          form)))
2641
2642 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2643   ;; This function parses the FORMAT string with the help of the
2644   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2645   ;; string.
2646   (let ((max-width 0)
2647         spec flist fstring newspec elem beg result dontinsert)
2648     (save-excursion
2649       (gnus-set-work-buffer)
2650       (insert format)
2651       (goto-char (point-min))
2652       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2653                                 nil t)
2654         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2655               (setq newspec "%"
2656                     beg (1+ (match-beginning 0)))
2657           ;; First check if there are any specs that look anything like
2658           ;; "%12,12A", ie. with a "max width specification".  These have
2659           ;; to be treated specially.
2660           (if (setq beg (match-beginning 1))
2661               (setq max-width
2662                     (string-to-int
2663                      (buffer-substring
2664                       (1+ (match-beginning 1)) (match-end 1))))
2665             (setq max-width 0)
2666             (setq beg (match-beginning 2)))
2667           ;; Find the specification from `spec-alist'.
2668           (unless (setq elem (cdr (assq spec spec-alist)))
2669             (setq elem '("*" ?s)))
2670           ;; Treat user defined format specifiers specially.
2671           (when (eq (car elem) 'gnus-tmp-user-defined)
2672             (setq elem
2673                   (list
2674                    (list (intern (concat "gnus-user-format-function-"
2675                                          (match-string 3)))
2676                          'gnus-tmp-header) ?s))
2677             (delete-region (match-beginning 3) (match-end 3)))
2678           (if (not (zerop max-width))
2679               (let ((el (car elem)))
2680                 (cond ((= (cadr elem) ?c)
2681                        (setq el (list 'char-to-string el)))
2682                       ((= (cadr elem) ?d)
2683                        (setq el (list 'int-to-string el))))
2684                 (setq flist (cons (gnus-max-width-function el max-width)
2685                                   flist))
2686                 (setq newspec ?s))
2687             (progn
2688               (setq flist (cons (car elem) flist))
2689               (setq newspec (cadr elem)))))
2690         ;; Remove the old specification (and possibly a ",12" string).
2691         (delete-region beg (match-end 2))
2692         ;; Insert the new specification.
2693         (goto-char beg)
2694         (insert newspec))
2695       (setq fstring (buffer-substring 1 (point-max))))
2696     ;; Do some postprocessing to increase efficiency.
2697     (setq
2698      result
2699      (cond
2700       ;; Emptyness.
2701       ((string= fstring "")
2702        nil)
2703       ;; Not a format string.
2704       ((not (string-match "%" fstring))
2705        (list fstring))
2706       ;; A format string with just a single string spec.
2707       ((string= fstring "%s")
2708        (list (car flist)))
2709       ;; A single character.
2710       ((string= fstring "%c")
2711        (list (car flist)))
2712       ;; A single number.
2713       ((string= fstring "%d")
2714        (setq dontinsert)
2715        (if insert
2716            (list `(princ ,(car flist)))
2717          (list `(int-to-string ,(car flist)))))
2718       ;; Just lots of chars and strings.
2719       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2720        (nreverse flist))
2721       ;; A single string spec at the beginning of the spec.
2722       ((string-match "\\`%[sc][^%]+\\'" fstring)
2723        (list (car flist) (substring fstring 2)))
2724       ;; A single string spec in the middle of the spec.
2725       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2726        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2727       ;; A single string spec in the end of the spec.
2728       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2729        (list (match-string 1 fstring) (car flist)))
2730       ;; A more complex spec.
2731       (t
2732        (list (cons 'format (cons fstring (nreverse flist)))))))
2733
2734     (if insert
2735         (when result
2736           (if dontinsert
2737               result
2738             (cons 'insert result)))
2739       (cond ((stringp result)
2740              result)
2741             ((consp result)
2742              (cons 'concat result))
2743             (t "")))))
2744
2745 (defun gnus-eval-format (format &optional alist props)
2746   "Eval the format variable FORMAT, using ALIST.
2747 If PROPS, insert the result."
2748   (let ((form (gnus-parse-format format alist props)))
2749     (if props
2750         (add-text-properties (point) (progn (eval form) (point)) props)
2751       (eval form))))
2752
2753 (defun gnus-remove-text-with-property (prop)
2754   "Delete all text in the current buffer with text property PROP."
2755   (save-excursion
2756     (goto-char (point-min))
2757     (while (not (eobp))
2758       (while (get-text-property (point) prop)
2759         (delete-char 1))
2760       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2761
2762 (defun gnus-set-work-buffer ()
2763   (if (get-buffer gnus-work-buffer)
2764       (progn
2765         (set-buffer gnus-work-buffer)
2766         (erase-buffer))
2767     (set-buffer (get-buffer-create gnus-work-buffer))
2768     (kill-all-local-variables)
2769     (buffer-disable-undo (current-buffer))
2770     (gnus-add-current-to-buffer-list)))
2771
2772 ;; Article file names when saving.
2773
2774 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2775   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2776 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2777 Otherwise, it is like ~/News/news/group/num."
2778   (let ((default
2779           (expand-file-name
2780            (concat (if (gnus-use-long-file-name 'not-save)
2781                        (gnus-capitalize-newsgroup newsgroup)
2782                      (gnus-newsgroup-directory-form newsgroup))
2783                    "/" (int-to-string (mail-header-number headers)))
2784            (or gnus-article-save-directory "~/News"))))
2785     (if (and last-file
2786              (string-equal (file-name-directory default)
2787                            (file-name-directory last-file))
2788              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2789         default
2790       (or last-file default))))
2791
2792 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2793   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2794 If variable `gnus-use-long-file-name' is non-nil, it is
2795 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2796   (let ((default
2797           (expand-file-name
2798            (concat (if (gnus-use-long-file-name 'not-save)
2799                        newsgroup
2800                      (gnus-newsgroup-directory-form newsgroup))
2801                    "/" (int-to-string (mail-header-number headers)))
2802            (or gnus-article-save-directory "~/News"))))
2803     (if (and last-file
2804              (string-equal (file-name-directory default)
2805                            (file-name-directory last-file))
2806              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2807         default
2808       (or last-file default))))
2809
2810 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2811   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2812 If variable `gnus-use-long-file-name' is non-nil, it is
2813 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2814   (or last-file
2815       (expand-file-name
2816        (if (gnus-use-long-file-name 'not-save)
2817            (gnus-capitalize-newsgroup newsgroup)
2818          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2819        (or gnus-article-save-directory "~/News"))))
2820
2821 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2822   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2823 If variable `gnus-use-long-file-name' is non-nil, it is
2824 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2825   (or last-file
2826       (expand-file-name
2827        (if (gnus-use-long-file-name 'not-save)
2828            newsgroup
2829          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2830        (or gnus-article-save-directory "~/News"))))
2831
2832 ;; For subscribing new newsgroup
2833
2834 (defun gnus-subscribe-hierarchical-interactive (groups)
2835   (let ((groups (sort groups 'string<))
2836         prefixes prefix start ans group starts)
2837     (while groups
2838       (setq prefixes (list "^"))
2839       (while (and groups prefixes)
2840         (while (not (string-match (car prefixes) (car groups)))
2841           (setq prefixes (cdr prefixes)))
2842         (setq prefix (car prefixes))
2843         (setq start (1- (length prefix)))
2844         (if (and (string-match "[^\\.]\\." (car groups) start)
2845                  (cdr groups)
2846                  (setq prefix
2847                        (concat "^" (substring (car groups) 0 (match-end 0))))
2848                  (string-match prefix (cadr groups)))
2849             (progn
2850               (setq prefixes (cons prefix prefixes))
2851               (message "Descend hierarchy %s? ([y]nsq): "
2852                        (substring prefix 1 (1- (length prefix))))
2853               (setq ans (read-char))
2854               (cond ((= ans ?n)
2855                      (while (and groups
2856                                  (string-match prefix
2857                                                (setq group (car groups))))
2858                        (setq gnus-killed-list
2859                              (cons group gnus-killed-list))
2860                        (gnus-sethash group group gnus-killed-hashtb)
2861                        (setq groups (cdr groups)))
2862                      (setq starts (cdr starts)))
2863                     ((= ans ?s)
2864                      (while (and groups
2865                                  (string-match prefix
2866                                                (setq group (car groups))))
2867                        (gnus-sethash group group gnus-killed-hashtb)
2868                        (gnus-subscribe-alphabetically (car groups))
2869                        (setq groups (cdr groups)))
2870                      (setq starts (cdr starts)))
2871                     ((= ans ?q)
2872                      (while groups
2873                        (setq group (car groups))
2874                        (setq gnus-killed-list (cons group gnus-killed-list))
2875                        (gnus-sethash group group gnus-killed-hashtb)
2876                        (setq groups (cdr groups))))
2877                     (t nil)))
2878           (message "Subscribe %s? ([n]yq)" (car groups))
2879           (setq ans (read-char))
2880           (setq group (car groups))
2881           (cond ((= ans ?y)
2882                  (gnus-subscribe-alphabetically (car groups))
2883                  (gnus-sethash group group gnus-killed-hashtb))
2884                 ((= ans ?q)
2885                  (while groups
2886                    (setq group (car groups))
2887                    (setq gnus-killed-list (cons group gnus-killed-list))
2888                    (gnus-sethash group group gnus-killed-hashtb)
2889                    (setq groups (cdr groups))))
2890                 (t
2891                  (setq gnus-killed-list (cons group gnus-killed-list))
2892                  (gnus-sethash group group gnus-killed-hashtb)))
2893           (setq groups (cdr groups)))))))
2894
2895 (defun gnus-subscribe-randomly (newsgroup)
2896   "Subscribe new NEWSGROUP by making it the first newsgroup."
2897   (gnus-subscribe-newsgroup newsgroup))
2898
2899 (defun gnus-subscribe-alphabetically (newgroup)
2900   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2901   (let ((groups (cdr gnus-newsrc-alist))
2902         before)
2903     (while (and (not before) groups)
2904       (if (string< newgroup (caar groups))
2905           (setq before (caar groups))
2906         (setq groups (cdr groups))))
2907     (gnus-subscribe-newsgroup newgroup before)))
2908
2909 (defun gnus-subscribe-hierarchically (newgroup)
2910   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2911   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2912   (save-excursion
2913     (set-buffer (find-file-noselect gnus-current-startup-file))
2914     (let ((groupkey newgroup)
2915           before)
2916       (while (and (not before) groupkey)
2917         (goto-char (point-min))
2918         (let ((groupkey-re
2919                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2920           (while (and (re-search-forward groupkey-re nil t)
2921                       (progn
2922                         (setq before (match-string 1))
2923                         (string< before newgroup)))))
2924         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2925         (setq groupkey
2926               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2927                   (substring groupkey (match-beginning 1) (match-end 1)))))
2928       (gnus-subscribe-newsgroup newgroup before))))
2929
2930 (defun gnus-subscribe-interactively (group)
2931   "Subscribe the new GROUP interactively.
2932 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2933 it is killed."
2934   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2935       (gnus-subscribe-hierarchically group)
2936     (push group gnus-killed-list)))
2937
2938 (defun gnus-subscribe-zombies (group)
2939   "Make the new GROUP into a zombie group."
2940   (push group gnus-zombie-list))
2941
2942 (defun gnus-subscribe-killed (group)
2943   "Make the new GROUP a killed group."
2944   (push group gnus-killed-list))
2945
2946 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2947   "Subscribe new NEWSGROUP.
2948 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2949 the first newsgroup."
2950   ;; We subscribe the group by changing its level to `subscribed'.
2951   (gnus-group-change-level
2952    newsgroup gnus-level-default-subscribed
2953    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2954   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2955
2956 ;; For directories
2957
2958 (defun gnus-newsgroup-directory-form (newsgroup)
2959   "Make hierarchical directory name from NEWSGROUP name."
2960   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2961         (len (length newsgroup))
2962         idx)
2963     ;; If this is a foreign group, we don't want to translate the
2964     ;; entire name.
2965     (if (setq idx (string-match ":" newsgroup))
2966         (aset newsgroup idx ?/)
2967       (setq idx 0))
2968     ;; Replace all occurrences of `.' with `/'.
2969     (while (< idx len)
2970       (if (= (aref newsgroup idx) ?.)
2971           (aset newsgroup idx ?/))
2972       (setq idx (1+ idx)))
2973     newsgroup))
2974
2975 (defun gnus-newsgroup-savable-name (group)
2976   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2977   ;; with dots.
2978   (nnheader-replace-chars-in-string group ?/ ?.))
2979
2980 (defun gnus-make-directory (dir)
2981   "Make DIRECTORY recursively."
2982   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2983   ;; of the many mysteries of the universe.
2984   (let* ((dir (expand-file-name dir default-directory))
2985          dirs err)
2986     (if (string-match "/$" dir)
2987         (setq dir (substring dir 0 (match-beginning 0))))
2988     ;; First go down the path until we find a directory that exists.
2989     (while (not (file-exists-p dir))
2990       (setq dirs (cons dir dirs))
2991       (string-match "/[^/]+$" dir)
2992       (setq dir (substring dir 0 (match-beginning 0))))
2993     ;; Then create all the subdirs.
2994     (while (and dirs (not err))
2995       (condition-case ()
2996           (make-directory (car dirs))
2997         (error (setq err t)))
2998       (setq dirs (cdr dirs)))
2999     ;; We return whether we were successful or not.
3000     (not dirs)))
3001
3002 (defun gnus-capitalize-newsgroup (newsgroup)
3003   "Capitalize NEWSGROUP name."
3004   (and (not (zerop (length newsgroup)))
3005        (concat (char-to-string (upcase (aref newsgroup 0)))
3006                (substring newsgroup 1))))
3007
3008 ;; Various... things.
3009
3010 (defun gnus-simplify-subject (subject &optional re-only)
3011   "Remove `Re:' and words in parentheses.
3012 If RE-ONLY is non-nil, strip leading `Re:'s only."
3013   (let ((case-fold-search t))           ;Ignore case.
3014     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3015     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3016       (setq subject (substring subject (match-end 0))))
3017     ;; Remove uninteresting prefixes.
3018     (if (and (not re-only)
3019              gnus-simplify-ignored-prefixes
3020              (string-match gnus-simplify-ignored-prefixes subject))
3021         (setq subject (substring subject (match-end 0))))
3022     ;; Remove words in parentheses from end.
3023     (unless re-only
3024       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3025         (setq subject (substring subject 0 (match-beginning 0)))))
3026     ;; Return subject string.
3027     subject))
3028
3029 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3030 ;; all whitespace.
3031 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3032 (defun gnus-simplify-buffer-fuzzy ()
3033   (goto-char (point-min))
3034   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
3035   (goto-char (match-beginning 0))
3036   (while (or
3037           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
3038           (looking-at "^[[].*:[ \t].*[]]$"))
3039     (goto-char (point-min))
3040     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
3041                               nil t)
3042       (replace-match "" t t))
3043     (goto-char (point-min))
3044     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
3045       (goto-char (match-end 0))
3046       (delete-char -1)
3047       (delete-region
3048        (progn (goto-char (match-beginning 0)))
3049        (re-search-forward ":"))))
3050   (goto-char (point-min))
3051   (while (re-search-forward "[ \t\n]*[[{(][^()\n]*[]})][ \t]*$" nil t)
3052     (replace-match "" t t))
3053   (goto-char (point-min))
3054   (while (re-search-forward "[ \t]+" nil t)
3055     (replace-match " " t t))
3056   (goto-char (point-min))
3057   (while (re-search-forward "[ \t]+$" nil t)
3058     (replace-match "" t t))
3059   (goto-char (point-min))
3060   (while (re-search-forward "^[ \t]+" nil t)
3061     (replace-match "" t t))
3062   (goto-char (point-min))
3063   (if gnus-simplify-subject-fuzzy-regexp
3064       (if (listp gnus-simplify-subject-fuzzy-regexp)
3065           (let ((list gnus-simplify-subject-fuzzy-regexp))
3066             (while list
3067               (goto-char (point-min))
3068               (while (re-search-forward (car list) nil t)
3069                 (replace-match "" t t))
3070               (setq list (cdr list))))
3071         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3072           (replace-match "" t t)))))
3073
3074 (defun gnus-simplify-subject-fuzzy (subject)
3075   "Siplify a subject string fuzzily."
3076   (save-excursion
3077     (gnus-set-work-buffer)
3078     (let ((case-fold-search t))
3079       (insert subject)
3080       (inline (gnus-simplify-buffer-fuzzy))
3081       (buffer-string))))
3082
3083 ;; Add the current buffer to the list of buffers to be killed on exit.
3084 (defun gnus-add-current-to-buffer-list ()
3085   (or (memq (current-buffer) gnus-buffer-list)
3086       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3087
3088 (defun gnus-string> (s1 s2)
3089   (not (or (string< s1 s2)
3090            (string= s1 s2))))
3091
3092 ;;; General various misc type functions.
3093
3094 (defun gnus-clear-system ()
3095   "Clear all variables and buffers."
3096   ;; Clear Gnus variables.
3097   (let ((variables gnus-variable-list))
3098     (while variables
3099       (set (car variables) nil)
3100       (setq variables (cdr variables))))
3101   ;; Clear other internal variables.
3102   (setq gnus-list-of-killed-groups nil
3103         gnus-have-read-active-file nil
3104         gnus-newsrc-alist nil
3105         gnus-newsrc-hashtb nil
3106         gnus-killed-list nil
3107         gnus-zombie-list nil
3108         gnus-killed-hashtb nil
3109         gnus-active-hashtb nil
3110         gnus-moderated-list nil
3111         gnus-description-hashtb nil
3112         gnus-current-headers nil
3113         gnus-thread-indent-array nil
3114         gnus-newsgroup-headers nil
3115         gnus-newsgroup-name nil
3116         gnus-server-alist nil
3117         gnus-group-list-mode nil
3118         gnus-opened-servers nil
3119         gnus-current-select-method nil)
3120   (gnus-shutdown 'gnus)
3121   ;; Kill the startup file.
3122   (and gnus-current-startup-file
3123        (get-file-buffer gnus-current-startup-file)
3124        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3125   ;; Clear the dribble buffer.
3126   (gnus-dribble-clear)
3127   ;; Kill global KILL file buffer.
3128   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3129     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3130   (gnus-kill-buffer nntp-server-buffer)
3131   ;; Kill Gnus buffers.
3132   (while gnus-buffer-list
3133     (gnus-kill-buffer (pop gnus-buffer-list)))
3134   ;; Remove Gnus frames.
3135   (while gnus-created-frames
3136     (when (frame-live-p (car gnus-created-frames))
3137       ;; We slap a condition-case around this `delete-frame' to ensure 
3138       ;; agains errors if we try do delete the single frame that's left.
3139       (condition-case ()
3140           (delete-frame (car gnus-created-frames))
3141         (error nil)))
3142     (pop gnus-created-frames)))
3143
3144 (defun gnus-windows-old-to-new (setting)
3145   ;; First we take care of the really, really old Gnus 3 actions.
3146   (when (symbolp setting)
3147     (setq setting
3148           ;; Take care of ooold GNUS 3.x values.
3149           (cond ((eq setting 'SelectArticle) 'article)
3150                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3151                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3152                 (t setting))))
3153   (if (or (listp setting)
3154           (not (and gnus-window-configuration
3155                     (memq setting '(group summary article)))))
3156       setting
3157     (let* ((setting (if (eq setting 'group)
3158                         (if (assq 'newsgroup gnus-window-configuration)
3159                             'newsgroup
3160                           'newsgroups) setting))
3161            (elem (cadr (assq setting gnus-window-configuration)))
3162            (total (apply '+ elem))
3163            (types '(group summary article))
3164            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3165            (i 0)
3166            perc
3167            out)
3168       (while (< i 3)
3169         (or (not (numberp (nth i elem)))
3170             (zerop (nth i elem))
3171             (progn
3172               (setq perc  (/ (float (nth 0 elem)) total))
3173               (setq out (cons (if (eq pbuf (nth i types))
3174                                   (vector (nth i types) perc 'point)
3175                                 (vector (nth i types) perc))
3176                               out))))
3177         (setq i (1+ i)))
3178       (list (nreverse out)))))
3179
3180 ;;;###autoload
3181 (defun gnus-add-configuration (conf)
3182   "Add the window configuration CONF to `gnus-buffer-configuration'."
3183   (setq gnus-buffer-configuration
3184         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3185                          gnus-buffer-configuration))))
3186
3187 (defvar gnus-frame-list nil)
3188
3189 (defun gnus-configure-frame (split &optional window)
3190   "Split WINDOW according to SPLIT."
3191   (unless window
3192     (setq window (get-buffer-window (current-buffer))))
3193   (select-window window)
3194   ;; This might be an old-stylee buffer config.
3195   (when (vectorp split)
3196     (setq split (append split nil)))
3197   (when (or (consp (car split))
3198             (vectorp (car split)))
3199     (push 1.0 split)
3200     (push 'vertical split))
3201   ;; The SPLIT might be something that is to be evaled to
3202   ;; return a new SPLIT.
3203   (while (and (not (assq (car split) gnus-window-to-buffer))
3204               (gnus-functionp (car split)))
3205     (setq split (eval split)))
3206   (let* ((type (car split))
3207          (subs (cddr split))
3208          (len (if (eq type 'horizontal) (window-width) (window-height)))
3209          (total 0)
3210          (window-min-width (or gnus-window-min-width window-min-width))
3211          (window-min-height (or gnus-window-min-height window-min-height))
3212          s result new-win rest comp-subs size sub)
3213     (cond
3214      ;; Nothing to do here.
3215      ((null split))
3216      ;; Don't switch buffers.
3217      ((null type)
3218       (and (memq 'point split) window))
3219      ;; This is a buffer to be selected.
3220      ((not (memq type '(frame horizontal vertical)))
3221       (let ((buffer (cond ((stringp type) type)
3222                           (t (cdr (assq type gnus-window-to-buffer)))))
3223             buf)
3224         (unless buffer
3225           (error "Illegal buffer type: %s" type))
3226         (unless (setq buf (get-buffer (if (symbolp buffer)
3227                                           (symbol-value buffer) buffer)))
3228           (setq buf (get-buffer-create (if (symbolp buffer)
3229                                            (symbol-value buffer) buffer))))
3230         (switch-to-buffer buf)
3231         ;; We return the window if it has the `point' spec.
3232         (and (memq 'point split) window)))
3233      ;; This is a frame split.
3234      ((eq type 'frame)
3235       (unless gnus-frame-list
3236         (setq gnus-frame-list (list (window-frame
3237                                      (get-buffer-window (current-buffer))))))
3238       (let ((i 0)
3239             params frame fresult)
3240         (while (< i (length subs))
3241           ;; Frame parameter is gotten from the sub-split.
3242           (setq params (cadr (elt subs i)))
3243           ;; It should be a list.
3244           (unless (listp params)
3245             (setq params nil))
3246           ;; Create a new frame?
3247           (unless (setq frame (elt gnus-frame-list i))
3248             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3249             (push frame gnus-created-frames))
3250           ;; Is the old frame still alive?
3251           (unless (frame-live-p frame)
3252             (setcar (nthcdr i gnus-frame-list)
3253                     (setq frame (make-frame params))))
3254           ;; Select the frame in question and do more splits there.
3255           (select-frame frame)
3256           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3257           (incf i))
3258         ;; Select the frame that has the selected buffer.
3259         (when fresult
3260           (select-frame (window-frame fresult)))))
3261      ;; This is a normal split.
3262      (t
3263       (when (> (length subs) 0)
3264         ;; First we have to compute the sizes of all new windows.
3265         (while subs
3266           (setq sub (append (pop subs) nil))
3267           (while (and (not (assq (car sub) gnus-window-to-buffer))
3268                       (gnus-functionp (car sub)))
3269             (setq sub (eval sub)))
3270           (when sub
3271             (push sub comp-subs)
3272             (setq size (cadar comp-subs))
3273             (cond ((equal size 1.0)
3274                    (setq rest (car comp-subs))
3275                    (setq s 0))
3276                   ((floatp size)
3277                    (setq s (floor (* size len))))
3278                   ((integerp size)
3279                    (setq s size))
3280                   (t
3281                    (error "Illegal size: %s" size)))
3282             ;; Try to make sure that we are inside the safe limits.
3283             (cond ((zerop s))
3284                   ((eq type 'horizontal)
3285                    (setq s (max s window-min-width)))
3286                   ((eq type 'vertical)
3287                    (setq s (max s window-min-height))))
3288             (setcar (cdar comp-subs) s)
3289             (incf total s)))
3290         ;; Take care of the "1.0" spec.
3291         (if rest
3292             (setcar (cdr rest) (- len total))
3293           (error "No 1.0 specs in %s" split))
3294         ;; The we do the actual splitting in a nice recursive
3295         ;; fashion.
3296         (setq comp-subs (nreverse comp-subs))
3297         (while comp-subs
3298           (if (null (cdr comp-subs))
3299               (setq new-win window)
3300             (setq new-win
3301                   (split-window window (cadar comp-subs)
3302                                 (eq type 'horizontal))))
3303           (setq result (or (gnus-configure-frame
3304                             (car comp-subs) window) result))
3305           (select-window new-win)
3306           (setq window new-win)
3307           (setq comp-subs (cdr comp-subs))))
3308       ;; Return the proper window, if any.
3309       (when result
3310         (select-window result))))))
3311
3312 (defvar gnus-frame-split-p nil)
3313
3314 (defun gnus-configure-windows (setting &optional force)
3315   (setq setting (gnus-windows-old-to-new setting))
3316   (let ((split (if (symbolp setting)
3317                    (cadr (assq setting gnus-buffer-configuration))
3318                  setting))
3319         all-visible)
3320
3321     (setq gnus-frame-split-p nil)
3322
3323     (unless split
3324       (error "No such setting: %s" setting))
3325
3326     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3327              (not force))
3328         ;; All the windows mentioned are already visible, so we just
3329         ;; put point in the assigned buffer, and do not touch the
3330         ;; winconf.
3331         (select-window all-visible)
3332
3333       ;; Either remove all windows or just remove all Gnus windows.
3334       (let ((frame (selected-frame)))
3335         (unwind-protect
3336             (if gnus-use-full-window
3337                 ;; We want to remove all other windows.
3338                 (if (not gnus-frame-split-p)
3339                     ;; This is not a `frame' split, so we ignore the
3340                     ;; other frames.  
3341                     (delete-other-windows)
3342                   ;; This is a `frame' split, so we delete all windows
3343                   ;; on all frames.
3344                   (mapcar 
3345                    (lambda (frame)
3346                      (unless (eq (cdr (assq 'minibuffer
3347                                             (frame-parameters frame)))
3348                                  'only)
3349                        (select-frame frame)
3350                        (delete-other-windows)))
3351                    (frame-list)))
3352               ;; Just remove some windows.
3353               (gnus-remove-some-windows)
3354               (switch-to-buffer nntp-server-buffer))
3355           (select-frame frame)))
3356
3357       (switch-to-buffer nntp-server-buffer)
3358       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3359
3360 (defun gnus-all-windows-visible-p (split)
3361   "Say whether all buffers in SPLIT are currently visible.
3362 In particular, the value returned will be the window that
3363 should have point."
3364   (let ((stack (list split))
3365         (all-visible t)
3366         type buffer win buf)
3367     (while (and (setq split (pop stack))
3368                 all-visible)
3369       ;; Be backwards compatible.
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
3382       (setq type (elt split 0))
3383       (cond
3384        ;; Nothing here.
3385        ((null split) t)
3386        ;; A buffer.
3387        ((not (memq type '(horizontal vertical frame)))
3388         (setq buffer (cond ((stringp type) type)
3389                            (t (cdr (assq type gnus-window-to-buffer)))))
3390         (unless buffer
3391           (error "Illegal buffer type: %s" type))
3392         (when (setq buf (get-buffer (if (symbolp buffer)
3393                                         (symbol-value buffer)
3394                                       buffer)))
3395           (setq win (get-buffer-window buf t)))
3396         (if win
3397             (when (memq 'point split)
3398                 (setq all-visible win))
3399           (setq all-visible nil)))
3400        (t
3401         (when (eq type 'frame)
3402           (setq gnus-frame-split-p t))
3403         (setq stack (append (cddr split) stack)))))
3404     (unless (eq all-visible t)
3405       all-visible)))
3406
3407 (defun gnus-window-top-edge (&optional window)
3408   (nth 1 (window-edges window)))
3409
3410 (defun gnus-remove-some-windows ()
3411   (let ((buffers gnus-window-to-buffer)
3412         buf bufs lowest-buf lowest)
3413     (save-excursion
3414       ;; Remove windows on all known Gnus buffers.
3415       (while buffers
3416         (setq buf (cdar buffers))
3417         (if (symbolp buf)
3418             (setq buf (and (boundp buf) (symbol-value buf))))
3419         (and buf
3420              (get-buffer-window buf)
3421              (progn
3422                (setq bufs (cons buf bufs))
3423                (pop-to-buffer buf)
3424                (if (or (not lowest)
3425                        (< (gnus-window-top-edge) lowest))
3426                    (progn
3427                      (setq lowest (gnus-window-top-edge))
3428                      (setq lowest-buf buf)))))
3429         (setq buffers (cdr buffers)))
3430       ;; Remove windows on *all* summary buffers.
3431       (walk-windows
3432        (lambda (win)
3433          (let ((buf (window-buffer win)))
3434            (if (string-match    "^\\*Summary" (buffer-name buf))
3435                (progn
3436                  (setq bufs (cons buf bufs))
3437                  (pop-to-buffer buf)
3438                  (if (or (not lowest)
3439                          (< (gnus-window-top-edge) lowest))
3440                      (progn
3441                        (setq lowest-buf buf)
3442                        (setq lowest (gnus-window-top-edge)))))))))
3443       (and lowest-buf
3444            (progn
3445              (pop-to-buffer lowest-buf)
3446              (switch-to-buffer nntp-server-buffer)))
3447       (while bufs
3448         (and (not (eq (car bufs) lowest-buf))
3449              (delete-windows-on (car bufs)))
3450         (setq bufs (cdr bufs))))))
3451
3452 (defun gnus-version ()
3453   "Version numbers of this version of Gnus."
3454   (interactive)
3455   (let ((methods gnus-valid-select-methods)
3456         (mess gnus-version)
3457         meth)
3458     ;; Go through all the legal select methods and add their version
3459     ;; numbers to the total version string.  Only the backends that are
3460     ;; currently in use will have their message numbers taken into
3461     ;; consideration.
3462     (while methods
3463       (setq meth (intern (concat (caar methods) "-version")))
3464       (and (boundp meth)
3465            (stringp (symbol-value meth))
3466            (setq mess (concat mess "; " (symbol-value meth))))
3467       (setq methods (cdr methods)))
3468     (gnus-message 2 mess)))
3469
3470 (defun gnus-info-find-node ()
3471   "Find Info documentation of Gnus."
3472   (interactive)
3473   ;; Enlarge info window if needed.
3474   (let ((mode major-mode)
3475         gnus-info-buffer)
3476     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3477     (setq gnus-info-buffer (current-buffer))
3478     (gnus-configure-windows 'info)))
3479
3480 (defun gnus-days-between (date1 date2)
3481   ;; Return the number of days between date1 and date2.
3482   (- (gnus-day-number date1) (gnus-day-number date2)))
3483
3484 (defun gnus-day-number (date)
3485   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3486                      (timezone-parse-date date))))
3487     (timezone-absolute-from-gregorian
3488      (nth 1 dat) (nth 2 dat) (car dat))))
3489
3490 (defun gnus-encode-date (date)
3491   "Convert DATE to internal time."
3492   (let* ((parse (timezone-parse-date date))
3493          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3494          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3495     (encode-time (caddr time) (cadr time) (car time)
3496                  (caddr date) (cadr date) (car date) (nth 4 date))))
3497
3498 (defun gnus-time-minus (t1 t2)
3499   "Subtract two internal times."
3500   (let ((borrow (< (cadr t1) (cadr t2))))
3501     (list (- (car t1) (car t2) (if borrow 1 0))
3502           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3503
3504 (defun gnus-file-newer-than (file date)
3505   (let ((fdate (nth 5 (file-attributes file))))
3506     (or (> (car fdate) (car date))
3507         (and (= (car fdate) (car date))
3508              (> (nth 1 fdate) (nth 1 date))))))
3509
3510 (defmacro gnus-local-set-keys (&rest plist)
3511   "Set the keys in PLIST in the current keymap."
3512   `(gnus-define-keys-1 (current-local-map) ',plist))
3513
3514 (defmacro gnus-define-keys (keymap &rest plist)
3515   "Define all keys in PLIST in KEYMAP."
3516   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3517
3518 (put 'gnus-define-keys 'lisp-indent-function 1)
3519 (put 'gnus-define-keys 'lisp-indent-hook 1)
3520 (put 'gnus-define-keymap 'lisp-indent-function 1)
3521 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3522
3523 (defmacro gnus-define-keymap (keymap &rest plist)
3524   "Define all keys in PLIST in KEYMAP."
3525   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3526
3527 (defun gnus-define-keys-1 (keymap plist)
3528   (when (null keymap)
3529     (error "Can't set keys in a null keymap"))
3530   (cond ((symbolp keymap)
3531          (setq keymap (symbol-value keymap)))
3532         ((keymapp keymap))
3533         ((listp keymap)
3534          (set (car keymap) nil)
3535          (define-prefix-command (car keymap))
3536          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3537          (setq keymap (symbol-value (car keymap)))))
3538   (let (key)
3539     (while plist
3540       (when (symbolp (setq key (pop plist)))
3541         (setq key (symbol-value key)))
3542       (define-key keymap key (pop plist)))))
3543
3544 (defun gnus-group-read-only-p (&optional group)
3545   "Check whether GROUP supports editing or not.
3546 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3547 that that variable is buffer-local to the summary buffers."
3548   (let ((group (or group gnus-newsgroup-name)))
3549     (not (gnus-check-backend-function 'request-replace-article group))))
3550
3551 (defun gnus-group-total-expirable-p (group)
3552   "Check whether GROUP is total-expirable or not."
3553   (let ((params (gnus-info-params (gnus-get-info group))))
3554     (or (memq 'total-expire params)
3555         (cdr (assq 'total-expire params)) ; (total-expire . t)
3556         (and gnus-total-expirable-newsgroups ; Check var.
3557              (string-match gnus-total-expirable-newsgroups group)))))
3558
3559 (defun gnus-group-auto-expirable-p (group)
3560   "Check whether GROUP is total-expirable or not."
3561   (let ((params (gnus-info-params (gnus-get-info group))))
3562     (or (memq 'auto-expire params)
3563         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3564         (and gnus-auto-expirable-newsgroups ; Check var.
3565              (string-match gnus-auto-expirable-newsgroups group)))))
3566
3567 (defun gnus-virtual-group-p (group)
3568   "Say whether GROUP is virtual or not."
3569   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3570                         gnus-valid-select-methods)))
3571
3572 (defsubst gnus-simplify-subject-fully (subject)
3573   "Simplify a subject string according to the user's wishes."
3574   (cond
3575    ((null gnus-summary-gather-subject-limit)
3576     (gnus-simplify-subject-re subject))
3577    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3578     (gnus-simplify-subject-fuzzy subject))
3579    ((numberp gnus-summary-gather-subject-limit)
3580     (gnus-limit-string (gnus-simplify-subject-re subject)
3581                        gnus-summary-gather-subject-limit))
3582    (t
3583     subject)))
3584
3585 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3586   "Check whether two subjects are equal.  If optional argument
3587 simple-first is t, first argument is already simplified."
3588   (cond
3589    ((null simple-first)
3590     (equal (gnus-simplify-subject-fully s1)
3591            (gnus-simplify-subject-fully s2)))
3592    (t
3593     (equal s1
3594            (gnus-simplify-subject-fully s2)))))
3595
3596 ;; Returns a list of writable groups.
3597 (defun gnus-writable-groups ()
3598   (let ((alist gnus-newsrc-alist)
3599         groups group)
3600     (while (setq group (car (pop alist)))
3601       (unless (gnus-group-read-only-p group)
3602         (push group groups)))
3603     (nreverse groups)))
3604
3605 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3606 ;; the echo area.
3607 (defun gnus-y-or-n-p (prompt)
3608   (prog1
3609       (y-or-n-p prompt)
3610     (message "")))
3611
3612 (defun gnus-yes-or-no-p (prompt)
3613   (prog1
3614       (yes-or-no-p prompt)
3615     (message "")))
3616
3617 ;; Check whether to use long file names.
3618 (defun gnus-use-long-file-name (symbol)
3619   ;; The variable has to be set...
3620   (and gnus-use-long-file-name
3621        ;; If it isn't a list, then we return t.
3622        (or (not (listp gnus-use-long-file-name))
3623            ;; If it is a list, and the list contains `symbol', we
3624            ;; return nil.
3625            (not (memq symbol gnus-use-long-file-name)))))
3626
3627 ;; I suspect there's a better way, but I haven't taken the time to do
3628 ;; it yet. -erik selberg@cs.washington.edu
3629 (defun gnus-dd-mmm (messy-date)
3630   "Return a string like DD-MMM from a big messy string"
3631   (let ((datevec (timezone-parse-date messy-date)))
3632     (format "%2s-%s"
3633             (condition-case ()
3634                 ;; Make sure leading zeroes are stripped.
3635                 (number-to-string (string-to-number (aref datevec 2)))
3636               (error "??"))
3637             (capitalize
3638              (or (car
3639                   (nth (1- (string-to-number (aref datevec 1)))
3640                        timezone-months-assoc))
3641                  "???")))))
3642
3643 ;; Make a hash table (default and minimum size is 255).
3644 ;; Optional argument HASHSIZE specifies the table size.
3645 (defun gnus-make-hashtable (&optional hashsize)
3646   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3647
3648 ;; Make a number that is suitable for hashing; bigger than MIN and one
3649 ;; less than 2^x.
3650 (defun gnus-create-hash-size (min)
3651   (let ((i 1))
3652     (while (< i min)
3653       (setq i (* 2 i)))
3654     (1- i)))
3655
3656 ;; Show message if message has a lower level than `gnus-verbose'.
3657 ;; Guideline for numbers:
3658 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3659 ;; for things that take a long time, 7 - not very important messages
3660 ;; on stuff, 9 - messages inside loops.
3661 (defun gnus-message (level &rest args)
3662   (if (<= level gnus-verbose)
3663       (apply 'message args)
3664     ;; We have to do this format thingy here even if the result isn't
3665     ;; shown - the return value has to be the same as the return value
3666     ;; from `message'.
3667     (apply 'format args)))
3668
3669 ;; Generate a unique new group name.
3670 (defun gnus-generate-new-group-name (leaf)
3671   (let ((name leaf)
3672         (num 0))
3673     (while (gnus-gethash name gnus-newsrc-hashtb)
3674       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3675     name))
3676
3677 (defsubst gnus-hide-text (b e props)
3678   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3679   (add-text-properties b e props)
3680   (when (memq 'intangible props)
3681     (put-text-property (1- b) b 'intangible (cddr (memq 'intangible props)))))
3682
3683 (defsubst gnus-unhide-text (b e)
3684   "Remove hidden text properties from region between B and E."
3685   (remove-text-properties b e gnus-hidden-properties)
3686   (when (memq 'intangible gnus-hidden-properties)
3687     (put-text-property (1- b) b 'intangible nil)))
3688
3689 (defun gnus-hide-text-type (b e type)
3690   "Hide text of TYPE between B and E."
3691   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3692
3693 ;; Find out whether the gnus-visual TYPE is wanted.
3694 (defun gnus-visual-p (&optional type class)
3695   (and gnus-visual                      ; Has to be non-nil, at least.
3696        (if (not type)                   ; We don't care about type.
3697            gnus-visual
3698          (if (listp gnus-visual)        ; It's a list, so we check it.
3699              (or (memq type gnus-visual)
3700                  (memq class gnus-visual))
3701            t))))
3702
3703 (defun gnus-parent-id (references)
3704   "Return the last Message-ID in REFERENCES."
3705   (when (and references
3706              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3707     (substring references (match-beginning 1) (match-end 1))))
3708
3709 (defun gnus-split-references (references)
3710   "Return a list of Message-IDs in REFERENCES."
3711   (let ((beg 0)
3712         ids)
3713     (while (string-match "<[^>]+>" references beg)
3714       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3715             ids))
3716     (nreverse ids)))
3717
3718 (defun gnus-ephemeral-group-p (group)
3719   "Say whether GROUP is ephemeral or not."
3720   (gnus-group-get-parameter group 'quit-config))
3721
3722 (defun gnus-group-quit-config (group)
3723   "Return the quit-config of GROUP."
3724   (gnus-group-get-parameter group 'quit-config))
3725
3726 (defun gnus-simplify-mode-line ()
3727   "Make mode lines a bit simpler."
3728   (setq mode-line-modified "-- ")
3729   (when (listp mode-line-format)
3730     (make-local-variable 'mode-line-format)
3731     (setq mode-line-format (copy-sequence mode-line-format))
3732     (when (equal (nth 3 mode-line-format) "   ")
3733       (setcar (nthcdr 3 mode-line-format) " "))))
3734
3735 ;;; List and range functions
3736
3737 (defun gnus-last-element (list)
3738   "Return last element of LIST."
3739   (while (cdr list)
3740     (setq list (cdr list)))
3741   (car list))
3742
3743 (defun gnus-copy-sequence (list)
3744   "Do a complete, total copy of a list."
3745   (if (and (consp list) (not (consp (cdr list))))
3746       (cons (car list) (cdr list))
3747     (mapcar (lambda (elem) (if (consp elem)
3748                                (if (consp (cdr elem))
3749                                    (gnus-copy-sequence elem)
3750                                  (cons (car elem) (cdr elem)))
3751                              elem))
3752             list)))
3753
3754 (defun gnus-set-difference (list1 list2)
3755   "Return a list of elements of LIST1 that do not appear in LIST2."
3756   (let ((list1 (copy-sequence list1)))
3757     (while list2
3758       (setq list1 (delq (car list2) list1))
3759       (setq list2 (cdr list2)))
3760     list1))
3761
3762 (defun gnus-sorted-complement (list1 list2)
3763   "Return a list of elements of LIST1 that do not appear in LIST2.
3764 Both lists have to be sorted over <."
3765   (let (out)
3766     (if (or (null list1) (null list2))
3767         (or list1 list2)
3768       (while (and list1 list2)
3769         (cond ((= (car list1) (car list2))
3770                (setq list1 (cdr list1)
3771                      list2 (cdr list2)))
3772               ((< (car list1) (car list2))
3773                (setq out (cons (car list1) out))
3774                (setq list1 (cdr list1)))
3775               (t
3776                (setq out (cons (car list2) out))
3777                (setq list2 (cdr list2)))))
3778       (nconc (nreverse out) (or list1 list2)))))
3779
3780 (defun gnus-intersection (list1 list2)
3781   (let ((result nil))
3782     (while list2
3783       (if (memq (car list2) list1)
3784           (setq result (cons (car list2) result)))
3785       (setq list2 (cdr list2)))
3786     result))
3787
3788 (defun gnus-sorted-intersection (list1 list2)
3789   ;; LIST1 and LIST2 have to be sorted over <.
3790   (let (out)
3791     (while (and list1 list2)
3792       (cond ((= (car list1) (car list2))
3793              (setq out (cons (car list1) out)
3794                    list1 (cdr list1)
3795                    list2 (cdr list2)))
3796             ((< (car list1) (car list2))
3797              (setq list1 (cdr list1)))
3798             (t
3799              (setq list2 (cdr list2)))))
3800     (nreverse out)))
3801
3802 (defun gnus-set-sorted-intersection (list1 list2)
3803   ;; LIST1 and LIST2 have to be sorted over <.
3804   ;; This function modifies LIST1.
3805   (let* ((top (cons nil list1))
3806          (prev top))
3807     (while (and list1 list2)
3808       (cond ((= (car list1) (car list2))
3809              (setq prev list1
3810                    list1 (cdr list1)
3811                    list2 (cdr list2)))
3812             ((< (car list1) (car list2))
3813              (setcdr prev (cdr list1))
3814              (setq list1 (cdr list1)))
3815             (t
3816              (setq list2 (cdr list2)))))
3817     (setcdr prev nil)
3818     (cdr top)))
3819
3820 (defun gnus-compress-sequence (numbers &optional always-list)
3821   "Convert list of numbers to a list of ranges or a single range.
3822 If ALWAYS-LIST is non-nil, this function will always release a list of
3823 ranges."
3824   (let* ((first (car numbers))
3825          (last (car numbers))
3826          result)
3827     (if (null numbers)
3828         nil
3829       (if (not (listp (cdr numbers)))
3830           numbers
3831         (while numbers
3832           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3833                 ((= (1+ last) (car numbers)) ;Still in sequence
3834                  (setq last (car numbers)))
3835                 (t                      ;End of one sequence
3836                  (setq result
3837                        (cons (if (= first last) first
3838                                (cons first last)) result))
3839                  (setq first (car numbers))
3840                  (setq last  (car numbers))))
3841           (setq numbers (cdr numbers)))
3842         (if (and (not always-list) (null result))
3843             (if (= first last) (list first) (cons first last))
3844           (nreverse (cons (if (= first last) first (cons first last))
3845                           result)))))))
3846
3847 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3848 (defun gnus-uncompress-range (ranges)
3849   "Expand a list of ranges into a list of numbers.
3850 RANGES is either a single range on the form `(num . num)' or a list of
3851 these ranges."
3852   (let (first last result)
3853     (cond
3854      ((null ranges)
3855       nil)
3856      ((not (listp (cdr ranges)))
3857       (setq first (car ranges))
3858       (setq last (cdr ranges))
3859       (while (<= first last)
3860         (setq result (cons first result))
3861         (setq first (1+ first)))
3862       (nreverse result))
3863      (t
3864       (while ranges
3865         (if (atom (car ranges))
3866             (if (numberp (car ranges))
3867                 (setq result (cons (car ranges) result)))
3868           (setq first (caar ranges))
3869           (setq last  (cdar ranges))
3870           (while (<= first last)
3871             (setq result (cons first result))
3872             (setq first (1+ first))))
3873         (setq ranges (cdr ranges)))
3874       (nreverse result)))))
3875
3876 (defun gnus-add-to-range (ranges list)
3877   "Return a list of ranges that has all articles from both RANGES and LIST.
3878 Note: LIST has to be sorted over `<'."
3879   (if (not ranges)
3880       (gnus-compress-sequence list t)
3881     (setq list (copy-sequence list))
3882     (or (listp (cdr ranges))
3883         (setq ranges (list ranges)))
3884     (let ((out ranges)
3885           ilist lowest highest temp)
3886       (while (and ranges list)
3887         (setq ilist list)
3888         (setq lowest (or (and (atom (car ranges)) (car ranges))
3889                          (caar ranges)))
3890         (while (and list (cdr list) (< (cadr list) lowest))
3891           (setq list (cdr list)))
3892         (if (< (car ilist) lowest)
3893             (progn
3894               (setq temp list)
3895               (setq list (cdr list))
3896               (setcdr temp nil)
3897               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3898         (setq highest (or (and (atom (car ranges)) (car ranges))
3899                           (cdar ranges)))
3900         (while (and list (<= (car list) highest))
3901           (setq list (cdr list)))
3902         (setq ranges (cdr ranges)))
3903       (if list
3904           (setq out (nconc (gnus-compress-sequence list t) out)))
3905       (setq out (sort out (lambda (r1 r2)
3906                             (< (or (and (atom r1) r1) (car r1))
3907                                (or (and (atom r2) r2) (car r2))))))
3908       (setq ranges out)
3909       (while ranges
3910         (if (atom (car ranges))
3911             (if (cdr ranges)
3912                 (if (atom (cadr ranges))
3913                     (if (= (1+ (car ranges)) (cadr ranges))
3914                         (progn
3915                           (setcar ranges (cons (car ranges)
3916                                                (cadr ranges)))
3917                           (setcdr ranges (cddr ranges))))
3918                   (if (= (1+ (car ranges)) (caadr ranges))
3919                       (progn
3920                         (setcar (cadr ranges) (car ranges))
3921                         (setcar ranges (cadr ranges))
3922                         (setcdr ranges (cddr ranges))))))
3923           (if (cdr ranges)
3924               (if (atom (cadr ranges))
3925                   (if (= (1+ (cdar ranges)) (cadr ranges))
3926                       (progn
3927                         (setcdr (car ranges) (cadr ranges))
3928                         (setcdr ranges (cddr ranges))))
3929                 (if (= (1+ (cdar ranges)) (caadr ranges))
3930                     (progn
3931                       (setcdr (car ranges) (cdadr ranges))
3932                       (setcdr ranges (cddr ranges)))))))
3933         (setq ranges (cdr ranges)))
3934       out)))
3935
3936 (defun gnus-remove-from-range (ranges list)
3937   "Return a list of ranges that has all articles from LIST removed from RANGES.
3938 Note: LIST has to be sorted over `<'."
3939   ;; !!! This function shouldn't look like this, but I've got a headache.
3940   (gnus-compress-sequence
3941    (gnus-sorted-complement
3942     (gnus-uncompress-range ranges) list)))
3943
3944 (defun gnus-member-of-range (number ranges)
3945   (if (not (listp (cdr ranges)))
3946       (and (>= number (car ranges))
3947            (<= number (cdr ranges)))
3948     (let ((not-stop t))
3949       (while (and ranges
3950                   (if (numberp (car ranges))
3951                       (>= number (car ranges))
3952                     (>= number (caar ranges)))
3953                   not-stop)
3954         (if (if (numberp (car ranges))
3955                 (= number (car ranges))
3956               (and (>= number (caar ranges))
3957                    (<= number (cdar ranges))))
3958             (setq not-stop nil))
3959         (setq ranges (cdr ranges)))
3960       (not not-stop))))
3961
3962 (defun gnus-range-length (range)
3963   "Return the length RANGE would have if uncompressed."
3964   (length (gnus-uncompress-range range)))
3965
3966 (defun gnus-sublist-p (list sublist)
3967   "Test whether all elements in SUBLIST are members of LIST."
3968   (let ((sublistp t))
3969     (while sublist
3970       (unless (memq (pop sublist) list)
3971         (setq sublistp nil
3972               sublist nil)))
3973     sublistp))
3974
3975 \f
3976 ;;;
3977 ;;; Gnus group mode
3978 ;;;
3979
3980 (defvar gnus-group-mode-map nil)
3981 (put 'gnus-group-mode 'mode-class 'special)
3982
3983 (unless gnus-group-mode-map
3984   (setq gnus-group-mode-map (make-keymap))
3985   (suppress-keymap gnus-group-mode-map)
3986
3987   (gnus-define-keys gnus-group-mode-map
3988     " " gnus-group-read-group
3989     "=" gnus-group-select-group
3990     "\r" gnus-group-select-group
3991     "\M-\r" gnus-group-quick-select-group
3992     "j" gnus-group-jump-to-group
3993     "n" gnus-group-next-unread-group
3994     "p" gnus-group-prev-unread-group
3995     "\177" gnus-group-prev-unread-group
3996     [delete] gnus-group-prev-unread-group
3997     "N" gnus-group-next-group
3998     "P" gnus-group-prev-group
3999     "\M-n" gnus-group-next-unread-group-same-level
4000     "\M-p" gnus-group-prev-unread-group-same-level
4001     "," gnus-group-best-unread-group
4002     "." gnus-group-first-unread-group
4003     "u" gnus-group-unsubscribe-current-group
4004     "U" gnus-group-unsubscribe-group
4005     "c" gnus-group-catchup-current
4006     "C" gnus-group-catchup-current-all
4007     "l" gnus-group-list-groups
4008     "L" gnus-group-list-all-groups
4009     "m" gnus-group-mail
4010     "g" gnus-group-get-new-news
4011     "\M-g" gnus-group-get-new-news-this-group
4012     "R" gnus-group-restart
4013     "r" gnus-group-read-init-file
4014     "B" gnus-group-browse-foreign-server
4015     "b" gnus-group-check-bogus-groups
4016     "F" gnus-find-new-newsgroups
4017     "\C-c\C-d" gnus-group-describe-group
4018     "\M-d" gnus-group-describe-all-groups
4019     "\C-c\C-a" gnus-group-apropos
4020     "\C-c\M-\C-a" gnus-group-description-apropos
4021     "a" gnus-group-post-news
4022     "\ek" gnus-group-edit-local-kill
4023     "\eK" gnus-group-edit-global-kill
4024     "\C-k" gnus-group-kill-group
4025     "\C-y" gnus-group-yank-group
4026     "\C-w" gnus-group-kill-region
4027     "\C-x\C-t" gnus-group-transpose-groups
4028     "\C-c\C-l" gnus-group-list-killed
4029     "\C-c\C-x" gnus-group-expire-articles
4030     "\C-c\M-\C-x" gnus-group-expire-all-groups
4031     "V" gnus-version
4032     "s" gnus-group-save-newsrc
4033     "z" gnus-group-suspend
4034     "Z" gnus-group-clear-dribble
4035     "q" gnus-group-exit
4036     "Q" gnus-group-quit
4037     "?" gnus-group-describe-briefly
4038     "\C-c\C-i" gnus-info-find-node
4039     "\M-e" gnus-group-edit-group-method
4040     "^" gnus-group-enter-server-mode
4041     gnus-mouse-2 gnus-mouse-pick-group
4042     "<" beginning-of-buffer
4043     ">" end-of-buffer
4044     "\C-c\C-b" gnus-bug
4045     "\C-c\C-s" gnus-group-sort-groups
4046     "t" gnus-topic-mode
4047     "\C-c\M-g" gnus-activate-all-groups
4048     "\M-&" gnus-group-universal-argument
4049     "#" gnus-group-mark-group
4050     "\M-#" gnus-group-unmark-group)
4051
4052   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4053     "m" gnus-group-mark-group
4054     "u" gnus-group-unmark-group
4055     "w" gnus-group-mark-region
4056     "m" gnus-group-mark-buffer
4057     "r" gnus-group-mark-regexp
4058     "U" gnus-group-unmark-all-groups)
4059
4060   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4061     "d" gnus-group-make-directory-group
4062     "h" gnus-group-make-help-group
4063     "a" gnus-group-make-archive-group
4064     "k" gnus-group-make-kiboze-group
4065     "m" gnus-group-make-group
4066     "E" gnus-group-edit-group
4067     "e" gnus-group-edit-group-method
4068     "p" gnus-group-edit-group-parameters
4069     "v" gnus-group-add-to-virtual
4070     "V" gnus-group-make-empty-virtual
4071     "D" gnus-group-enter-directory
4072     "f" gnus-group-make-doc-group
4073     "r" gnus-group-rename-group
4074     "\177" gnus-group-delete-group
4075     [delete] gnus-group-delete-group)
4076
4077    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4078      "b" gnus-group-brew-soup
4079      "w" gnus-soup-save-areas
4080      "s" gnus-soup-send-replies
4081      "p" gnus-soup-pack-packet
4082      "r" nnsoup-pack-replies)
4083
4084    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4085      "s" gnus-group-sort-groups
4086      "a" gnus-group-sort-groups-by-alphabet
4087      "u" gnus-group-sort-groups-by-unread
4088      "l" gnus-group-sort-groups-by-level
4089      "v" gnus-group-sort-groups-by-score
4090      "r" gnus-group-sort-groups-by-rank
4091      "m" gnus-group-sort-groups-by-method)
4092
4093    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4094      "k" gnus-group-list-killed
4095      "z" gnus-group-list-zombies
4096      "s" gnus-group-list-groups
4097      "u" gnus-group-list-all-groups
4098      "A" gnus-group-list-active
4099      "a" gnus-group-apropos
4100      "d" gnus-group-description-apropos
4101      "m" gnus-group-list-matching
4102      "M" gnus-group-list-all-matching
4103      "l" gnus-group-list-level)
4104
4105    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4106      "f" gnus-score-flush-cache)
4107
4108    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4109      "f" gnus-group-fetch-faq)
4110
4111    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4112      "l" gnus-group-set-current-level
4113      "t" gnus-group-unsubscribe-current-group
4114      "s" gnus-group-unsubscribe-group
4115      "k" gnus-group-kill-group
4116      "y" gnus-group-yank-group
4117      "w" gnus-group-kill-region
4118      "\C-k" gnus-group-kill-level
4119      "z" gnus-group-kill-all-zombies))
4120
4121 (defun gnus-group-mode ()
4122   "Major mode for reading news.
4123
4124 All normal editing commands are switched off.
4125 \\<gnus-group-mode-map>
4126 The group buffer lists (some of) the groups available.  For instance,
4127 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4128 lists all zombie groups.
4129
4130 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4131 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4132
4133 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4134
4135 The following commands are available:
4136
4137 \\{gnus-group-mode-map}"
4138   (interactive)
4139   (when (and menu-bar-mode
4140              (gnus-visual-p 'group-menu 'menu))
4141     (gnus-group-make-menu-bar))
4142   (kill-all-local-variables)
4143   (gnus-simplify-mode-line)
4144   (setq major-mode 'gnus-group-mode)
4145   (setq mode-name "Group")
4146   (gnus-group-set-mode-line)
4147   (setq mode-line-process nil)
4148   (use-local-map gnus-group-mode-map)
4149   (buffer-disable-undo (current-buffer))
4150   (setq truncate-lines t)
4151   (setq buffer-read-only t)
4152   (run-hooks 'gnus-group-mode-hook))
4153
4154 (defun gnus-mouse-pick-group (e)
4155   "Enter the group under the mouse pointer."
4156   (interactive "e")
4157   (mouse-set-point e)
4158   (gnus-group-read-group nil))
4159
4160 ;; Look at LEVEL and find out what the level is really supposed to be.
4161 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4162 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4163 (defun gnus-group-default-level (&optional level number-or-nil)
4164   (cond
4165    (gnus-group-use-permanent-levels
4166     (setq gnus-group-default-list-level
4167           (or level gnus-group-default-list-level))
4168     (or gnus-group-default-list-level gnus-level-subscribed))
4169    (number-or-nil
4170     level)
4171    (t
4172     (or level gnus-group-default-list-level gnus-level-subscribed))))
4173
4174 ;;;###autoload
4175 (defun gnus-slave-no-server (&optional arg)
4176   "Read network news as a slave, without connecting to local server"
4177   (interactive "P")
4178   (gnus-no-server arg t))
4179
4180 ;;;###autoload
4181 (defun gnus-no-server (&optional arg slave)
4182   "Read network news.
4183 If ARG is a positive number, Gnus will use that as the
4184 startup level.  If ARG is nil, Gnus will be started at level 2.
4185 If ARG is non-nil and not a positive number, Gnus will
4186 prompt the user for the name of an NNTP server to use.
4187 As opposed to `gnus', this command will not connect to the local server."
4188   (interactive "P")
4189   (let ((gnus-group-use-permanent-levels t))
4190     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4191   (make-local-variable 'gnus-group-use-permanent-levels)
4192   (setq gnus-group-use-permanent-levels t))
4193
4194 ;;;###autoload
4195 (defun gnus-slave (&optional arg)
4196   "Read news as a slave."
4197   (interactive "P")
4198   (gnus arg nil 'slave))
4199
4200 ;;;###autoload
4201 (defun gnus-other-frame (&optional arg)
4202   "Pop up a frame to read news."
4203   (interactive "P")
4204   (if (get-buffer gnus-group-buffer)
4205       (let ((pop-up-frames t))
4206         (gnus arg))
4207     (select-frame (make-frame))
4208     (gnus arg)))
4209
4210 ;;;###autoload
4211 (defun gnus (&optional arg dont-connect slave)
4212   "Read network news.
4213 If ARG is non-nil and a positive number, Gnus will use that as the
4214 startup level.  If ARG is non-nil and not a positive number, Gnus will
4215 prompt the user for the name of an NNTP server to use."
4216   (interactive "P")
4217
4218   (if (get-buffer gnus-group-buffer)
4219       (progn
4220         (switch-to-buffer gnus-group-buffer)
4221         (gnus-group-get-new-news))
4222
4223     (gnus-clear-system)
4224     (nnheader-init-server-buffer)
4225     (gnus-read-init-file)
4226     (setq gnus-slave slave)
4227
4228     (gnus-group-setup-buffer)
4229     (let ((buffer-read-only nil))
4230       (erase-buffer)
4231       (if (not gnus-inhibit-startup-message)
4232           (progn
4233             (gnus-group-startup-message)
4234             (sit-for 0))))
4235
4236     (let ((level (and (numberp arg) (> arg 0) arg))
4237           did-connect)
4238       (unwind-protect
4239           (progn
4240             (or dont-connect
4241                 (setq did-connect
4242                       (gnus-start-news-server (and arg (not level))))))
4243         (if (and (not dont-connect)
4244                  (not did-connect))
4245             (gnus-group-quit)
4246           (run-hooks 'gnus-startup-hook)
4247           ;; NNTP server is successfully open.
4248
4249           ;; Find the current startup file name.
4250           (setq gnus-current-startup-file
4251                 (gnus-make-newsrc-file gnus-startup-file))
4252
4253           ;; Read the dribble file.
4254           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4255
4256           ;; Allow using GroupLens predictions.
4257           (when gnus-use-grouplens
4258             (bbb-login)
4259             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4260
4261           (gnus-summary-make-display-table)
4262           ;; Do the actual startup.
4263           (gnus-setup-news nil level dont-connect)
4264           ;; Generate the group buffer.
4265           (gnus-group-list-groups level)
4266           (gnus-group-first-unread-group)
4267           (gnus-configure-windows 'group)
4268           (gnus-group-set-mode-line))))))
4269
4270 (defun gnus-unload ()
4271   "Unload all Gnus features."
4272   (interactive)
4273   (or (boundp 'load-history)
4274       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4275   (let ((history load-history)
4276         feature)
4277     (while history
4278       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4279            (setq feature (cdr (assq 'provide (car history))))
4280            (unload-feature feature 'force))
4281       (setq history (cdr history)))))
4282
4283 (defun gnus-compile ()
4284   "Byte-compile the user-defined format specs."
4285   (interactive)
4286   (let ((entries gnus-format-specs)
4287         entry gnus-tmp-func)
4288     (save-excursion
4289       (gnus-message 7 "Compiling format specs...")
4290
4291       (while entries
4292         (setq entry (pop entries))
4293         (if (eq (car entry) 'version)
4294             (setq gnus-format-specs (delq entry gnus-format-specs))
4295           (when (and (listp (caddr entry))
4296                      (not (eq 'byte-code (caaddr entry))))
4297             (fset 'gnus-tmp-func
4298                   `(lambda () ,(caddr entry)))
4299             (byte-compile 'gnus-tmp-func)
4300             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4301
4302       (push (cons 'version emacs-version) gnus-format-specs)
4303
4304       (gnus-message 7 "Compiling user specs...done"))))
4305
4306 (defun gnus-indent-rigidly (start end arg)
4307   "Indent rigidly using only spaces and no tabs."
4308   (save-excursion
4309     (save-restriction
4310       (narrow-to-region start end)
4311       (indent-rigidly start end arg)
4312       (goto-char (point-min))
4313       (while (search-forward "\t" nil t)
4314         (replace-match "        " t t)))))
4315
4316 (defun gnus-group-startup-message (&optional x y)
4317   "Insert startup message in current buffer."
4318   ;; Insert the message.
4319   (erase-buffer)
4320   (insert
4321    (format "              %s
4322           _    ___ _             _
4323           _ ___ __ ___  __    _ ___
4324           __   _     ___    __  ___
4325               _           ___     _
4326              _  _ __             _
4327              ___   __            _
4328                    __           _
4329                     _      _   _
4330                    _      _    _
4331                       _  _    _
4332                   __  ___
4333                  _   _ _     _
4334                 _   _
4335               _    _
4336              _    _
4337             _
4338           __
4339
4340 "
4341            ""))
4342   ;; And then hack it.
4343   (gnus-indent-rigidly (point-min) (point-max)
4344                        (/ (max (- (window-width) (or x 46)) 0) 2))
4345   (goto-char (point-min))
4346   (forward-line 1)
4347   (let* ((pheight (count-lines (point-min) (point-max)))
4348          (wheight (window-height))
4349          (rest (- wheight pheight)))
4350     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4351   ;; Fontify some.
4352   (goto-char (point-min))
4353   (and (search-forward "Praxis" nil t)
4354        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4355   (goto-char (point-min))
4356   (let* ((mode-string (gnus-group-set-mode-line)))
4357     (setq mode-line-buffer-identification
4358           (list (concat gnus-version (substring (car mode-string) 4))))
4359     (set-buffer-modified-p t)))
4360
4361 (defun gnus-group-setup-buffer ()
4362   (or (get-buffer gnus-group-buffer)
4363       (progn
4364         (switch-to-buffer gnus-group-buffer)
4365         (gnus-add-current-to-buffer-list)
4366         (gnus-group-mode)
4367         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4368
4369 (defun gnus-group-list-groups (&optional level unread lowest)
4370   "List newsgroups with level LEVEL or lower that have unread articles.
4371 Default is all subscribed groups.
4372 If argument UNREAD is non-nil, groups with no unread articles are also
4373 listed."
4374   (interactive (list (if current-prefix-arg
4375                          (prefix-numeric-value current-prefix-arg)
4376                        (or
4377                         (gnus-group-default-level nil t)
4378                         gnus-group-default-list-level
4379                         gnus-level-subscribed))))
4380   (or level
4381       (setq level (car gnus-group-list-mode)
4382             unread (cdr gnus-group-list-mode)))
4383   (setq level (gnus-group-default-level level))
4384   (gnus-group-setup-buffer)             ;May call from out of group buffer
4385   (gnus-update-format-specifications)
4386   (let ((case-fold-search nil)
4387         (props (text-properties-at (gnus-point-at-bol)))
4388         (group (gnus-group-group-name)))
4389     (set-buffer gnus-group-buffer)
4390     (funcall gnus-group-prepare-function level unread lowest)
4391     (if (zerop (buffer-size))
4392         (gnus-message 5 gnus-no-groups-message)
4393       (goto-char (point-max))
4394       (when (or (not gnus-group-goto-next-group-function)
4395                 (not (funcall gnus-group-goto-next-group-function 
4396                               group props)))
4397         (if (not group)
4398             ;; Go to the first group with unread articles.
4399             (gnus-group-search-forward t)
4400           ;; Find the right group to put point on.  If the current group
4401           ;; has disappeared in the new listing, try to find the next
4402           ;; one.        If no next one can be found, just leave point at the
4403           ;; first newsgroup in the buffer.
4404           (if (not (gnus-goto-char
4405                     (text-property-any
4406                      (point-min) (point-max)
4407                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4408               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4409                 (while (and newsrc
4410                             (not (gnus-goto-char
4411                                   (text-property-any
4412                                    (point-min) (point-max) 'gnus-group
4413                                    (gnus-intern-safe
4414                                     (caar newsrc) gnus-active-hashtb)))))
4415                   (setq newsrc (cdr newsrc)))
4416                 (or newsrc (progn (goto-char (point-max))
4417                                   (forward-line -1)))))))
4418       ;; Adjust cursor point.
4419       (gnus-group-position-point))))
4420
4421 (defun gnus-group-list-level (level &optional all)
4422   "List groups on LEVEL.
4423 If ALL (the prefix), also list groups that have no unread articles."
4424   (interactive "nList groups on level: \nP")
4425   (gnus-group-list-groups level all level))
4426
4427 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4428   "List all newsgroups with unread articles of level LEVEL or lower.
4429 If ALL is non-nil, list groups that have no unread articles.
4430 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4431 If REGEXP, only list groups matching REGEXP."
4432   (set-buffer gnus-group-buffer)
4433   (let ((buffer-read-only nil)
4434         (newsrc (cdr gnus-newsrc-alist))
4435         (lowest (or lowest 1))
4436         info clevel unread group params)
4437     (erase-buffer)
4438     (if (< lowest gnus-level-zombie)
4439         ;; List living groups.
4440         (while newsrc
4441           (setq info (car newsrc)
4442                 group (gnus-info-group info)
4443                 params (gnus-info-params info)
4444                 newsrc (cdr newsrc)
4445                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4446           (and unread                   ; This group might be bogus
4447                (or (not regexp)
4448                    (string-match regexp group))
4449                (<= (setq clevel (gnus-info-level info)) level)
4450                (>= clevel lowest)
4451                (or all                  ; We list all groups?
4452                    (if (eq unread t)    ; Unactivated?
4453                        gnus-group-list-inactive-groups ; We list unactivated 
4454                      (> unread 0))      ; We list groups with unread articles
4455                    (and gnus-list-groups-with-ticked-articles
4456                         (cdr (assq 'tick (gnus-info-marks info))))
4457                                         ; And groups with tickeds
4458                    ;; Check for permanent visibility.
4459                    (and gnus-permanently-visible-groups
4460                         (string-match gnus-permanently-visible-groups
4461                                       group))
4462                    (memq 'visible params)
4463                    (cdr (assq 'visible params)))
4464                (gnus-group-insert-group-line
4465                 group (gnus-info-level info)
4466                 (gnus-info-marks info) unread (gnus-info-method info)))))
4467
4468     ;; List dead groups.
4469     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4470          (gnus-group-prepare-flat-list-dead
4471           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4472           gnus-level-zombie ?Z
4473           regexp))
4474     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4475          (gnus-group-prepare-flat-list-dead
4476           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4477           gnus-level-killed ?K regexp))
4478
4479     (gnus-group-set-mode-line)
4480     (setq gnus-group-list-mode (cons level all))
4481     (run-hooks 'gnus-group-prepare-hook)))
4482
4483 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4484   ;; List zombies and killed lists somewhat faster, which was
4485   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4486   ;; this by ignoring the group format specification altogether.
4487   (let (group)
4488     (if regexp
4489         ;; This loop is used when listing groups that match some
4490         ;; regexp.
4491         (while groups
4492           (setq group (pop groups))
4493           (when (string-match regexp group)
4494             (add-text-properties
4495              (point) (prog1 (1+ (point))
4496                        (insert " " mark "     *: " group "\n"))
4497              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4498                    'gnus-unread t
4499                    'gnus-level level))))
4500       ;; This loop is used when listing all groups.
4501       (while groups
4502         (add-text-properties
4503          (point) (prog1 (1+ (point))
4504                    (insert " " mark "     *: "
4505                            (setq group (pop groups)) "\n"))
4506          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4507                'gnus-unread t
4508                'gnus-level level))))))
4509
4510 (defmacro gnus-group-real-name (group)
4511   "Find the real name of a foreign newsgroup."
4512   `(let ((gname ,group))
4513      (if (string-match ":[^:]+$" gname)
4514          (substring gname (1+ (match-beginning 0)))
4515        gname)))
4516
4517 (defsubst gnus-server-add-address (method)
4518   (let ((method-name (symbol-name (car method))))
4519     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4520              (not (assq (intern (concat method-name "-address")) method)))
4521         (append method (list (list (intern (concat method-name "-address"))
4522                                    (nth 1 method))))
4523       method)))
4524
4525 (defsubst gnus-server-get-method (group method)
4526   ;; Input either a server name, and extended server name, or a
4527   ;; select method, and return a select method.
4528   (cond ((stringp method)
4529          (gnus-server-to-method method))
4530         ((and (stringp (car method)) group)
4531          (gnus-server-extend-method group method))
4532         (t
4533          (gnus-server-add-address method))))
4534
4535 (defun gnus-server-to-method (server)
4536   "Map virtual server names to select methods."
4537   (or 
4538    ;; Perhaps this is the native server?
4539    (and (equal server "native") gnus-select-method)
4540    ;; It should be in the server alist.
4541    (cdr (assoc server gnus-server-alist))
4542    ;; If not, we look through all the opened server
4543    ;; to see whether we can find it there.
4544    (let ((opened gnus-opened-servers))
4545      (while (and opened
4546                  (not (equal server (format "%s:%s" (caaar opened)
4547                                             (cadaar opened)))))
4548        (pop opened))
4549      (caar opened))))
4550
4551 (defmacro gnus-method-equal (ss1 ss2)
4552   "Say whether two servers are equal."
4553   `(let ((s1 ,ss1)
4554          (s2 ,ss2))
4555      (or (equal s1 s2)
4556          (and (= (length s1) (length s2))
4557               (progn
4558                 (while (and s1 (member (car s1) s2))
4559                   (setq s1 (cdr s1)))
4560                 (null s1))))))
4561
4562 (defun gnus-server-equal (m1 m2)
4563   "Say whether two methods are equal."
4564   (let ((m1 (cond ((null m1) gnus-select-method)
4565                   ((stringp m1) (gnus-server-to-method m1))
4566                   (t m1)))
4567         (m2 (cond ((null m2) gnus-select-method)
4568                   ((stringp m2) (gnus-server-to-method m2))
4569                   (t m2))))
4570     (gnus-method-equal m1 m2)))
4571
4572 (defun gnus-group-prefixed-name (group method)
4573   "Return the whole name from GROUP and METHOD."
4574   (and (stringp method) (setq method (gnus-server-to-method method)))
4575   (concat (format "%s" (car method))
4576           (if (and
4577                (or (assoc (format "%s" (car method)) 
4578                           (gnus-methods-using 'address))
4579                    (gnus-server-equal method gnus-message-archive-method))
4580                (nth 1 method)
4581                (not (string= (nth 1 method) "")))
4582               (concat "+" (nth 1 method)))
4583           ":" group))
4584
4585 (defun gnus-group-real-prefix (group)
4586   "Return the prefix of the current group name."
4587   (if (string-match "^[^:]+:" group)
4588       (substring group 0 (match-end 0))
4589     ""))
4590
4591 (defun gnus-group-method (group)
4592   "Return the server or method used for selecting GROUP."
4593   (let ((prefix (gnus-group-real-prefix group)))
4594     (if (equal prefix "")
4595         gnus-select-method
4596       (let ((servers gnus-opened-servers)
4597             (server "")
4598             backend possible found)
4599         (if (string-match "^[^\\+]+\\+" prefix)
4600             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4601                   server (substring prefix (match-end 0) (1- (length prefix))))
4602           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4603         (while servers
4604           (when (eq (caaar servers) backend)
4605             (setq possible (caar servers))
4606             (when (equal (cadaar servers) server)
4607               (setq found (caar servers))))
4608           (pop servers))
4609         (or (car (rassoc found gnus-server-alist))
4610             found
4611             (car (rassoc possible gnus-server-alist))
4612             possible
4613             (list backend server))))))
4614
4615 (defsubst gnus-secondary-method-p (method)
4616   "Return whether METHOD is a secondary select method."
4617   (let ((methods gnus-secondary-select-methods)
4618         (gmethod (gnus-server-get-method nil method)))
4619     (while (and methods
4620                 (not (equal (gnus-server-get-method nil (car methods))
4621                             gmethod)))
4622       (setq methods (cdr methods)))
4623     methods))
4624
4625 (defun gnus-group-foreign-p (group)
4626   "Say whether a group is foreign or not."
4627   (and (not (gnus-group-native-p group))
4628        (not (gnus-group-secondary-p group))))
4629
4630 (defun gnus-group-native-p (group)
4631   "Say whether the group is native or not."
4632   (not (string-match ":" group)))
4633
4634 (defun gnus-group-secondary-p (group)
4635   "Say whether the group is secondary or not."
4636   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4637
4638 (defun gnus-group-get-parameter (group &optional symbol)
4639   "Returns the group parameters for GROUP.
4640 If SYMBOL, return the value of that symbol in the group parameters."
4641   (let ((params (gnus-info-params (gnus-get-info group))))
4642     (if symbol
4643         (gnus-group-parameter-value params symbol)
4644       params)))
4645
4646 (defun gnus-group-parameter-value (params symbol)
4647   "Return the value of SYMBOL in group PARAMS."
4648   (or (car (memq symbol params))        ; It's either a simple symbol
4649       (cdr (assq symbol params))))      ; or a cons.
4650
4651 (defun gnus-group-add-parameter (group param)
4652   "Add parameter PARAM to GROUP."
4653   (let ((info (gnus-get-info group)))
4654     (if (not info)
4655         () ; This is a dead group.  We just ignore it.
4656       ;; Cons the new param to the old one and update.
4657       (gnus-group-set-info (cons param (gnus-info-params info))
4658                            group 'params))))
4659
4660 (defun gnus-group-set-parameter (group name value)
4661   "Set parameter NAME to VALUE in GROUP."
4662   (let ((info (gnus-get-info group)))
4663     (if (not info)
4664         () ; This is a dead group.  We just ignore it.
4665       (let ((old-params (gnus-info-params info))
4666             (new-params (list (cons name value))))
4667         (while old-params
4668           (if (or (not (listp (car old-params)))
4669                   (not (eq (caar old-params) name)))
4670               (setq new-params (append new-params (list (car old-params)))))
4671           (setq old-params (cdr old-params)))
4672         (gnus-group-set-info new-params group 'params)))))
4673
4674 (defun gnus-group-add-score (group &optional score)
4675   "Add SCORE to the GROUP score.
4676 If SCORE is nil, add 1 to the score of GROUP."
4677   (let ((info (gnus-get-info group)))
4678     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4679
4680 (defun gnus-summary-bubble-group ()
4681   "Increase the score of the current group.
4682 This is a handy function to add to `gnus-summary-exit-hook' to
4683 increase the score of each group you read."
4684   (gnus-group-add-score gnus-newsgroup-name))
4685
4686 (defun gnus-group-set-info (info &optional method-only-group part)
4687   (let* ((entry (gnus-gethash
4688                  (or method-only-group (gnus-info-group info))
4689                  gnus-newsrc-hashtb))
4690          (part-info info)
4691          (info (if method-only-group (nth 2 entry) info))
4692          method)
4693     (when method-only-group
4694       (unless entry
4695         (error "Trying to change non-existent group %s" method-only-group))
4696       ;; We have received parts of the actual group info - either the
4697       ;; select method or the group parameters.  We first check
4698       ;; whether we have to extend the info, and if so, do that.
4699       (let ((len (length info))
4700             (total (if (eq part 'method) 5 6)))
4701         (when (< len total)
4702           (setcdr (nthcdr (1- len) info)
4703                   (make-list (- total len) nil)))
4704         ;; Then we enter the new info.
4705         (setcar (nthcdr (1- total) info) part-info)))
4706     (unless entry
4707       ;; This is a new group, so we just create it.
4708       (save-excursion
4709         (set-buffer gnus-group-buffer)
4710         (setq method (gnus-info-method info))
4711         (when (gnus-server-equal method "native")
4712           (setq method nil))
4713         (if method
4714             ;; It's a foreign group...
4715             (gnus-group-make-group
4716              (gnus-group-real-name (gnus-info-group info))
4717              (if (stringp method) method
4718                (prin1-to-string (car method)))
4719              (and (consp method)
4720                   (nth 1 (gnus-info-method info))))
4721           ;; It's a native group.
4722           (gnus-group-make-group (gnus-info-group info)))
4723         (gnus-message 6 "Note: New group created")
4724         (setq entry
4725               (gnus-gethash (gnus-group-prefixed-name
4726                              (gnus-group-real-name (gnus-info-group info))
4727                              (or (gnus-info-method info) gnus-select-method))
4728                             gnus-newsrc-hashtb))))
4729     ;; Whether it was a new group or not, we now have the entry, so we
4730     ;; can do the update.
4731     (if entry
4732         (progn
4733           (setcar (nthcdr 2 entry) info)
4734           (when (and (not (eq (car entry) t))
4735                      (gnus-active (gnus-info-group info)))
4736             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4737       (error "No such group: %s" (gnus-info-group info)))))
4738
4739 (defun gnus-group-set-method-info (group select-method)
4740   (gnus-group-set-info select-method group 'method))
4741
4742 (defun gnus-group-set-params-info (group params)
4743   (gnus-group-set-info params group 'params))
4744
4745 (defun gnus-group-update-group-line ()
4746   "Update the current line in the group buffer."
4747   (let* ((buffer-read-only nil)
4748          (group (gnus-group-group-name))
4749          (gnus-group-indentation (gnus-group-group-indentation))
4750          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4751     (and entry
4752          (not (gnus-ephemeral-group-p group))
4753          (gnus-dribble-enter
4754           (concat "(gnus-group-set-info '"
4755                   (prin1-to-string (nth 2 entry)) ")")))
4756     (gnus-delete-line)
4757     (gnus-group-insert-group-line-info group)
4758     (forward-line -1)
4759     (gnus-group-position-point)))
4760
4761 (defun gnus-group-insert-group-line-info (group)
4762   "Insert GROUP on the current line."
4763   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4764         active info)
4765     (if entry
4766         (progn
4767           ;; (Un)subscribed group.
4768           (setq info (nth 2 entry))
4769           (gnus-group-insert-group-line
4770            group (gnus-info-level info) (gnus-info-marks info)
4771            (or (car entry) t) (gnus-info-method info)))
4772       ;; This group is dead.
4773       (gnus-group-insert-group-line
4774        group
4775        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4776        nil
4777        (if (setq active (gnus-active group))
4778            (- (1+ (cdr active)) (car active)) 0)
4779        nil))))
4780
4781 (defun gnus-group-insert-group-line
4782   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4783                   gnus-tmp-method)
4784   "Insert a group line in the group buffer."
4785   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4786          (gnus-tmp-number-total
4787           (if gnus-tmp-active
4788               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4789             0))
4790          (gnus-tmp-number-of-unread
4791           (if (numberp number) (int-to-string (max 0 number))
4792             "*"))
4793          (gnus-tmp-number-of-read
4794           (if (numberp number)
4795               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4796             "*"))
4797          (gnus-tmp-subscribed
4798           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4799                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4800                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4801                 (t ?K)))
4802          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4803          (gnus-tmp-newsgroup-description
4804           (if gnus-description-hashtb
4805               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4806             ""))
4807          (gnus-tmp-moderated
4808           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4809          (gnus-tmp-moderated-string
4810           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4811          (gnus-tmp-method
4812           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4813          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4814          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4815          (gnus-tmp-news-method-string
4816           (if gnus-tmp-method
4817               (format "(%s:%s)" (car gnus-tmp-method)
4818                       (cadr gnus-tmp-method)) ""))
4819          (gnus-tmp-marked-mark
4820           (if (and (numberp number)
4821                    (zerop number)
4822                    (cdr (assq 'tick gnus-tmp-marked)))
4823               ?* ? ))
4824          (gnus-tmp-process-marked
4825           (if (member gnus-tmp-group gnus-group-marked)
4826               gnus-process-mark ? ))
4827          (gnus-tmp-grouplens
4828           (or (and gnus-use-grouplens
4829                    (bbb-grouplens-group-p gnus-tmp-group))
4830               ""))
4831          (buffer-read-only nil)
4832          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4833     (beginning-of-line)
4834     (add-text-properties
4835      (point)
4836      (prog1 (1+ (point))
4837        ;; Insert the text.
4838        (eval gnus-group-line-format-spec))
4839      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4840        gnus-unread ,(if (numberp number)
4841                         (string-to-int gnus-tmp-number-of-unread)
4842                       t)
4843        gnus-marked ,gnus-tmp-marked-mark
4844        gnus-indentation ,gnus-group-indentation
4845        gnus-level ,gnus-tmp-level))
4846     (when (gnus-visual-p 'group-highlight 'highlight)
4847       (forward-line -1)
4848       (run-hooks 'gnus-group-update-hook)
4849       (forward-line))
4850     ;; Allow XEmacs to remove front-sticky text properties.
4851     (gnus-group-remove-excess-properties)))
4852
4853 (defun gnus-group-update-group (group &optional visible-only)
4854   "Update all lines where GROUP appear.
4855 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4856 already."
4857   (save-excursion
4858     (set-buffer gnus-group-buffer)
4859     ;; The buffer may be narrowed.
4860     (save-restriction
4861       (widen)
4862       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4863             (loc (point-min))
4864             found buffer-read-only)
4865         ;; Enter the current status into the dribble buffer.
4866         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4867           (if (and entry (not (gnus-ephemeral-group-p group)))
4868               (gnus-dribble-enter
4869                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4870                        ")"))))
4871         ;; Find all group instances.  If topics are in use, each group
4872         ;; may be listed in more than once.
4873         (while (setq loc (text-property-any
4874                           loc (point-max) 'gnus-group ident))
4875           (setq found t)
4876           (goto-char loc)
4877           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4878             (gnus-delete-line)
4879             (gnus-group-insert-group-line-info group))
4880           (setq loc (1+ loc)))
4881         (unless (or found visible-only)
4882           ;; No such line in the buffer, find out where it's supposed to
4883           ;; go, and insert it there (or at the end of the buffer).
4884           (if gnus-goto-missing-group-function
4885               (funcall gnus-goto-missing-group-function group)
4886             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4887               (while (and entry (car entry)
4888                           (not
4889                            (gnus-goto-char
4890                             (text-property-any
4891                              (point-min) (point-max)
4892                              'gnus-group (gnus-intern-safe
4893                                           (caar entry) gnus-active-hashtb)))))
4894                 (setq entry (cdr entry)))
4895               (or entry (goto-char (point-max)))))
4896           ;; Finally insert the line.
4897           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4898             (gnus-group-insert-group-line-info group)))
4899         (gnus-group-set-mode-line)))))
4900
4901 (defun gnus-group-set-mode-line ()
4902   (when (memq 'group gnus-updated-mode-lines)
4903     (let* ((gformat (or gnus-group-mode-line-format-spec
4904                         (setq gnus-group-mode-line-format-spec
4905                               (gnus-parse-format
4906                                gnus-group-mode-line-format
4907                                gnus-group-mode-line-format-alist))))
4908            (gnus-tmp-news-server (cadr gnus-select-method))
4909            (gnus-tmp-news-method (car gnus-select-method))
4910            (max-len 60)
4911            gnus-tmp-header                      ;Dummy binding for user-defined formats
4912            ;; Get the resulting string.
4913            (mode-string (eval gformat)))
4914       ;; If the line is too long, we chop it off.
4915       (when (> (length mode-string) max-len)
4916         (setq mode-string (substring mode-string 0 (- max-len 4))))
4917       (prog1
4918           (setq mode-line-buffer-identification (list mode-string))
4919         (set-buffer-modified-p t)))))
4920
4921 (defun gnus-group-group-name ()
4922   "Get the name of the newsgroup on the current line."
4923   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4924     (and group (symbol-name group))))
4925
4926 (defun gnus-group-group-level ()
4927   "Get the level of the newsgroup on the current line."
4928   (get-text-property (gnus-point-at-bol) 'gnus-level))
4929
4930 (defun gnus-group-group-indentation ()
4931   "Get the indentation of the newsgroup on the current line."
4932   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4933       (and gnus-group-indentation-function
4934            (funcall gnus-group-indentation-function))
4935       ""))
4936
4937 (defun gnus-group-group-unread ()
4938   "Get the number of unread articles of the newsgroup on the current line."
4939   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4940
4941 (defun gnus-group-search-forward (&optional backward all level first-too)
4942   "Find the next newsgroup with unread articles.
4943 If BACKWARD is non-nil, find the previous newsgroup instead.
4944 If ALL is non-nil, just find any newsgroup.
4945 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4946 group exists.
4947 If FIRST-TOO, the current line is also eligible as a target."
4948   (let ((way (if backward -1 1))
4949         (low gnus-level-killed)
4950         (beg (point))
4951         pos found lev)
4952     (if (and backward (progn (beginning-of-line)) (bobp))
4953         nil
4954       (or first-too (forward-line way))
4955       (while (and
4956               (not (eobp))
4957               (not (setq
4958                     found
4959                     (and (or all
4960                              (and
4961                               (let ((unread
4962                                      (get-text-property (point) 'gnus-unread)))
4963                                 (and (numberp unread) (> unread 0)))
4964                               (setq lev (get-text-property (point)
4965                                                            'gnus-level))
4966                               (<= lev gnus-level-subscribed)))
4967                          (or (not level)
4968                              (and (setq lev (get-text-property (point)
4969                                                                'gnus-level))
4970                                   (or (= lev level)
4971                                       (and (< lev low)
4972                                            (< level lev)
4973                                            (progn
4974                                              (setq low lev)
4975                                              (setq pos (point))
4976                                              nil))))))))
4977               (zerop (forward-line way)))))
4978     (if found
4979         (progn (gnus-group-position-point) t)
4980       (goto-char (or pos beg))
4981       (and pos t))))
4982
4983 ;;; Gnus group mode commands
4984
4985 ;; Group marking.
4986
4987 (defun gnus-group-mark-group (n &optional unmark no-advance)
4988   "Mark the current group."
4989   (interactive "p")
4990   (let ((buffer-read-only nil)
4991         group)
4992     (while
4993         (and (> n 0)
4994              (setq group (gnus-group-group-name))
4995              (progn
4996                (beginning-of-line)
4997                (forward-char
4998                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4999                (delete-char 1)
5000                (if unmark
5001                    (progn
5002                      (insert " ")
5003                      (setq gnus-group-marked (delete group gnus-group-marked)))
5004                  (insert "#")
5005                  (setq gnus-group-marked
5006                        (cons group (delete group gnus-group-marked))))
5007                t)
5008              (or no-advance (zerop (gnus-group-next-group 1))))
5009       (setq n (1- n)))
5010     (gnus-summary-position-point)
5011     n))
5012
5013 (defun gnus-group-unmark-group (n)
5014   "Remove the mark from the current group."
5015   (interactive "p")
5016   (gnus-group-mark-group n 'unmark)
5017   (gnus-group-position-point))
5018
5019 (defun gnus-group-unmark-all-groups ()
5020   "Unmark all groups."
5021   (interactive)
5022   (let ((groups gnus-group-marked))
5023     (save-excursion
5024       (while groups
5025         (gnus-group-remove-mark (pop groups)))))
5026   (gnus-group-position-point))
5027
5028 (defun gnus-group-mark-region (unmark beg end)
5029   "Mark all groups between point and mark.
5030 If UNMARK, remove the mark instead."
5031   (interactive "P\nr")
5032   (let ((num (count-lines beg end)))
5033     (save-excursion
5034       (goto-char beg)
5035       (- num (gnus-group-mark-group num unmark)))))
5036
5037 (defun gnus-group-mark-buffer (&optional unmark)
5038   "Mark all groups in the buffer.
5039 If UNMARK, remove the mark instead."
5040   (interactive "P")
5041   (gnus-group-mark-region unmark (point-min) (point-max)))
5042
5043 (defun gnus-group-mark-regexp (regexp)
5044   "Mark all groups that match some regexp."
5045   (interactive "sMark (regexp): ")
5046   (let ((alist (cdr gnus-newsrc-alist))
5047         group)
5048     (while alist
5049       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5050         (gnus-group-set-mark group))))
5051   (gnus-group-position-point))
5052
5053 (defun gnus-group-remove-mark (group)
5054   "Remove the process mark from GROUP and move point there.
5055 Return nil if the group isn't displayed."
5056   (if (gnus-group-goto-group group)
5057       (save-excursion
5058         (gnus-group-mark-group 1 'unmark t)
5059         t)
5060     (setq gnus-group-marked
5061           (delete group gnus-group-marked))
5062     nil))
5063
5064 (defun gnus-group-set-mark (group)
5065   "Set the process mark on GROUP."
5066   (if (gnus-group-goto-group group) 
5067       (save-excursion
5068         (gnus-group-mark-group 1 nil t))
5069     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5070
5071 (defun gnus-group-universal-argument (arg &optional groups func)
5072   "Perform any command on all groups accoring to the process/prefix convention."
5073   (interactive "P")
5074   (let ((groups (or groups (gnus-group-process-prefix arg)))
5075         group func)
5076     (if (eq (setq func (or func
5077                            (key-binding
5078                             (read-key-sequence
5079                              (substitute-command-keys
5080                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5081             'undefined)
5082         (progn
5083           (message "Undefined key")
5084           (ding))
5085       (while groups
5086         (gnus-group-remove-mark (setq group (pop groups)))
5087         (command-execute func))))
5088   (gnus-group-position-point))
5089
5090 (defun gnus-group-process-prefix (n)
5091   "Return a list of groups to work on.
5092 Take into consideration N (the prefix) and the list of marked groups."
5093   (cond
5094    (n
5095     (setq n (prefix-numeric-value n))
5096     ;; There is a prefix, so we return a list of the N next
5097     ;; groups.
5098     (let ((way (if (< n 0) -1 1))
5099           (n (abs n))
5100           group groups)
5101       (save-excursion
5102         (while (and (> n 0)
5103                     (setq group (gnus-group-group-name)))
5104           (setq groups (cons group groups))
5105           (setq n (1- n))
5106           (gnus-group-next-group way)))
5107       (nreverse groups)))
5108    ((and (boundp 'transient-mark-mode)
5109          transient-mark-mode
5110          mark-active)
5111     ;; Work on the region between point and mark.
5112     (let ((max (max (point) (mark)))
5113           groups)
5114       (save-excursion
5115         (goto-char (min (point) (mark)))
5116         (while
5117             (and
5118              (push (gnus-group-group-name) groups)
5119              (zerop (gnus-group-next-group 1))
5120              (< (point) max)))
5121         (nreverse groups))))
5122    (gnus-group-marked
5123     ;; No prefix, but a list of marked articles.
5124     (reverse gnus-group-marked))
5125    (t
5126     ;; Neither marked articles or a prefix, so we return the
5127     ;; current group.
5128     (let ((group (gnus-group-group-name)))
5129       (and group (list group))))))
5130
5131 ;; Selecting groups.
5132
5133 (defun gnus-group-read-group (&optional all no-article group)
5134   "Read news in this newsgroup.
5135 If the prefix argument ALL is non-nil, already read articles become
5136 readable.  IF ALL is a number, fetch this number of articles.  If the
5137 optional argument NO-ARTICLE is non-nil, no article will be
5138 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5139 group."
5140   (interactive "P")
5141   (let ((group (or group (gnus-group-group-name)))
5142         number active marked entry)
5143     (or group (error "No group on current line"))
5144     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5145                                             group gnus-newsrc-hashtb)))))
5146     ;; This group might be a dead group.  In that case we have to get
5147     ;; the number of unread articles from `gnus-active-hashtb'.
5148     (setq number
5149           (cond ((numberp all) all)
5150                 (entry (car entry))
5151                 ((setq active (gnus-active group))
5152                  (- (1+ (cdr active)) (car active)))))
5153     (gnus-summary-read-group
5154      group (or all (and (numberp number)
5155                         (zerop (+ number (length (cdr (assq 'tick marked)))
5156                                   (length (cdr (assq 'dormant marked)))))))
5157      no-article)))
5158
5159 (defun gnus-group-select-group (&optional all)
5160   "Select this newsgroup.
5161 No article is selected automatically.
5162 If ALL is non-nil, already read articles become readable.
5163 If ALL is a number, fetch this number of articles."
5164   (interactive "P")
5165   (gnus-group-read-group all t))
5166
5167 (defun gnus-group-quick-select-group (&optional all)
5168   "Select the current group \"quickly\".
5169 This means that no highlighting or scoring will be performed."
5170   (interactive "P")
5171   (let (gnus-visual
5172         gnus-score-find-score-files-function
5173         gnus-apply-kill-hook
5174         gnus-summary-expunge-below)
5175     (gnus-group-read-group all t)))
5176
5177 (defun gnus-group-visible-select-group (&optional all)
5178   "Select the current group without hiding any articles."
5179   (interactive "P")
5180   (let ((gnus-inhibit-limiting t))
5181     (gnus-group-read-group all t)))
5182
5183 ;;;###autoload
5184 (defun gnus-fetch-group (group)
5185   "Start Gnus if necessary and enter GROUP.
5186 Returns whether the fetching was successful or not."
5187   (interactive "sGroup name: ")
5188   (or (get-buffer gnus-group-buffer)
5189       (gnus))
5190   (gnus-group-read-group nil nil group))
5191
5192 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5193 ;; if selection was successful.
5194 (defun gnus-group-read-ephemeral-group
5195   (group method &optional activate quit-config)
5196   (let ((group (if (gnus-group-foreign-p group) group
5197                  (gnus-group-prefixed-name group method))))
5198     (gnus-sethash
5199      group
5200      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5201                      ((quit-config . ,(if quit-config quit-config
5202                                         (cons (current-buffer) 'summary))))))
5203      gnus-newsrc-hashtb)
5204     (set-buffer gnus-group-buffer)
5205     (or (gnus-check-server method)
5206         (error "Unable to contact server: %s" (gnus-status-message method)))
5207     (if activate (or (gnus-request-group group)
5208                      (error "Couldn't request group")))
5209     (condition-case ()
5210         (gnus-group-read-group t t group)
5211       (error nil)
5212       (quit nil))))
5213
5214 (defun gnus-group-jump-to-group (group)
5215   "Jump to newsgroup GROUP."
5216   (interactive
5217    (list (completing-read
5218           "Group: " gnus-active-hashtb nil
5219           (memq gnus-select-method gnus-have-read-active-file))))
5220
5221   (if (equal group "")
5222       (error "Empty group name"))
5223
5224   (let ((b (text-property-any
5225             (point-min) (point-max)
5226             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5227     (unless (gnus-ephemeral-group-p group)
5228       (if b
5229           ;; Either go to the line in the group buffer...
5230           (goto-char b)
5231         ;; ... or insert the line.
5232         (or
5233          (gnus-active group)
5234          (gnus-activate-group group)
5235          (error "%s error: %s" group (gnus-status-message group)))
5236
5237         (gnus-group-update-group group)
5238         (goto-char (text-property-any
5239                     (point-min) (point-max)
5240                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5241     ;; Adjust cursor point.
5242     (gnus-group-position-point)))
5243
5244 (defun gnus-group-goto-group (group)
5245   "Goto to newsgroup GROUP."
5246   (when group
5247     (let ((b (text-property-any (point-min) (point-max)
5248                                 'gnus-group (gnus-intern-safe
5249                                              group gnus-active-hashtb))))
5250       (and b (goto-char b)))))
5251
5252 (defun gnus-group-next-group (n)
5253   "Go to next N'th newsgroup.
5254 If N is negative, search backward instead.
5255 Returns the difference between N and the number of skips actually
5256 done."
5257   (interactive "p")
5258   (gnus-group-next-unread-group n t))
5259
5260 (defun gnus-group-next-unread-group (n &optional all level)
5261   "Go to next N'th unread newsgroup.
5262 If N is negative, search backward instead.
5263 If ALL is non-nil, choose any newsgroup, unread or not.
5264 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5265 such group can be found, the next group with a level higher than
5266 LEVEL.
5267 Returns the difference between N and the number of skips actually
5268 made."
5269   (interactive "p")
5270   (let ((backward (< n 0))
5271         (n (abs n)))
5272     (while (and (> n 0)
5273                 (gnus-group-search-forward
5274                  backward (or (not gnus-group-goto-unread) all) level))
5275       (setq n (1- n)))
5276     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5277                                (if level " on this level or higher" "")))
5278     n))
5279
5280 (defun gnus-group-prev-group (n)
5281   "Go to previous N'th 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) t))
5286
5287 (defun gnus-group-prev-unread-group (n)
5288   "Go to previous N'th unread newsgroup.
5289 Returns the difference between N and the number of skips actually
5290 done."
5291   (interactive "p")
5292   (gnus-group-next-unread-group (- n)))
5293
5294 (defun gnus-group-next-unread-group-same-level (n)
5295   "Go to next N'th unread newsgroup on the same level.
5296 If N is negative, search backward instead.
5297 Returns the difference between N and the number of skips actually
5298 done."
5299   (interactive "p")
5300   (gnus-group-next-unread-group n t (gnus-group-group-level))
5301   (gnus-group-position-point))
5302
5303 (defun gnus-group-prev-unread-group-same-level (n)
5304   "Go to next N'th unread newsgroup on the same level.
5305 Returns the difference between N and the number of skips actually
5306 done."
5307   (interactive "p")
5308   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5309   (gnus-group-position-point))
5310
5311 (defun gnus-group-best-unread-group (&optional exclude-group)
5312   "Go to the group with the highest level.
5313 If EXCLUDE-GROUP, do not go to that group."
5314   (interactive)
5315   (goto-char (point-min))
5316   (let ((best 100000)
5317         unread best-point)
5318     (while (setq unread (get-text-property (point) 'gnus-unread))
5319       (if (and (numberp unread) (> unread 0))
5320           (progn
5321             (if (and (< (get-text-property (point) 'gnus-level) best)
5322                      (or (not exclude-group)
5323                          (not (equal exclude-group (gnus-group-group-name)))))
5324                 (progn
5325                   (setq best (get-text-property (point) 'gnus-level))
5326                   (setq best-point (point))))))
5327       (forward-line 1))
5328     (if best-point (goto-char best-point))
5329     (gnus-summary-position-point)
5330     (and best-point (gnus-group-group-name))))
5331
5332 (defun gnus-group-first-unread-group ()
5333   "Go to the first group with unread articles."
5334   (interactive)
5335   (prog1
5336       (let ((opoint (point))
5337             unread)
5338         (goto-char (point-min))
5339         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5340                 (and (numberp unread)   ; Not a topic.
5341                      (not (zerop unread))) ; Has unread articles.
5342                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5343             (point)                     ; Success.
5344           (goto-char opoint)
5345           nil))                         ; Not success.
5346     (gnus-group-position-point)))
5347
5348 (defun gnus-group-enter-server-mode ()
5349   "Jump to the server buffer."
5350   (interactive)
5351   (gnus-enter-server-buffer))
5352
5353 (defun gnus-group-make-group (name &optional method address)
5354   "Add a new newsgroup.
5355 The user will be prompted for a NAME, for a select METHOD, and an
5356 ADDRESS."
5357   (interactive
5358    (cons
5359     (read-string "Group name: ")
5360     (let ((method
5361            (completing-read
5362             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5363             nil t)))
5364       (if (assoc method gnus-valid-select-methods)
5365           (list method
5366                 (if (memq 'prompt-address
5367                           (assoc method gnus-valid-select-methods))
5368                     (read-string "Address: ")
5369                   ""))
5370         (list method "")))))
5371
5372   (save-excursion
5373     (set-buffer gnus-group-buffer)
5374     (let* ((meth (and method (if address (list (intern method) address)
5375                                method)))
5376            (nname (if method (gnus-group-prefixed-name name meth) name))
5377            info)
5378       (and (gnus-gethash nname gnus-newsrc-hashtb)
5379            (error "Group %s already exists" nname))
5380       (gnus-group-change-level
5381        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5382        gnus-level-default-subscribed gnus-level-killed
5383        (and (gnus-group-group-name)
5384             (gnus-gethash (gnus-group-group-name)
5385                           gnus-newsrc-hashtb))
5386        t)
5387       (gnus-set-active nname (cons 1 0))
5388       (or (gnus-ephemeral-group-p name)
5389           (gnus-dribble-enter
5390            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5391       (gnus-group-insert-group-line-info nname)
5392
5393       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5394         (require (car meth)))
5395       (gnus-check-server meth)
5396       (and (gnus-check-backend-function 'request-create-group nname)
5397            (gnus-request-create-group nname))
5398       t)))
5399
5400 (defun gnus-group-delete-group (group &optional force)
5401   "Delete the current group.
5402 If FORCE (the prefix) is non-nil, all the articles in the group will
5403 be deleted.  This is \"deleted\" as in \"removed forever from the face
5404 of the Earth\".  There is no undo."
5405   (interactive
5406    (list (gnus-group-group-name)
5407          current-prefix-arg))
5408   (or group (error "No group to rename"))
5409   (or (gnus-check-backend-function 'request-delete-group group)
5410       (error "This backend does not support group deletion"))
5411   (prog1
5412       (if (not (gnus-yes-or-no-p
5413                 (format
5414                  "Do you really want to delete %s%s? "
5415                  group (if force " and all its contents" ""))))
5416           () ; Whew!
5417         (gnus-message 6 "Deleting group %s..." group)
5418         (if (not (gnus-request-delete-group group force))
5419             (progn
5420               (gnus-message 3 "Couldn't delete group %s" group)
5421               (ding))
5422           (gnus-message 6 "Deleting group %s...done" group)
5423           (gnus-group-goto-group group)
5424           (gnus-group-kill-group 1 t)
5425           (gnus-sethash group nil gnus-active-hashtb)
5426           t))
5427     (gnus-group-position-point)))
5428
5429 (defun gnus-group-rename-group (group new-name)
5430   (interactive
5431    (list
5432     (gnus-group-group-name)
5433     (progn
5434       (or (gnus-check-backend-function
5435            'request-rename-group (gnus-group-group-name))
5436           (error "This backend does not support renaming groups"))
5437       (read-string "New group name: "))))
5438
5439   (or (gnus-check-backend-function 'request-rename-group group)
5440       (error "This backend does not support renaming groups"))
5441
5442   (or group (error "No group to rename"))
5443   (and (string-match "^[ \t]*$" new-name)
5444        (error "Not a valid group name"))
5445
5446   ;; We find the proper prefixed name.
5447   (setq new-name
5448         (gnus-group-prefixed-name
5449          (gnus-group-real-name new-name)
5450          (gnus-info-method (gnus-get-info group))))
5451
5452   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5453   (prog1
5454       (if (not (gnus-request-rename-group group new-name))
5455           (progn
5456             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5457             (ding))
5458         ;; We rename the group internally by killing it...
5459         (gnus-group-goto-group group)
5460         (gnus-group-kill-group)
5461         ;; ... changing its name ...
5462         (setcar (cdar gnus-list-of-killed-groups) new-name)
5463         ;; ... and then yanking it.  Magic!
5464         (gnus-group-yank-group)
5465         (gnus-set-active new-name (gnus-active group))
5466         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5467         new-name)
5468     (gnus-group-position-point)))
5469
5470 (defun gnus-group-edit-group (group &optional part)
5471   "Edit the group on the current line."
5472   (interactive (list (gnus-group-group-name)))
5473   (let* ((part (or part 'info))
5474          (done-func `(lambda ()
5475                        "Exit editing mode and update the information."
5476                        (interactive)
5477                        (gnus-group-edit-group-done ',part ,group)))
5478          (winconf (current-window-configuration))
5479          info)
5480     (or group (error "No group on current line"))
5481     (or (setq info (gnus-get-info group))
5482         (error "Killed group; can't be edited"))
5483     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5484     (gnus-configure-windows 'edit-group)
5485     (gnus-add-current-to-buffer-list)
5486     (emacs-lisp-mode)
5487     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5488     (use-local-map (copy-keymap emacs-lisp-mode-map))
5489     (local-set-key "\C-c\C-c" done-func)
5490     (make-local-variable 'gnus-prev-winconf)
5491     (setq gnus-prev-winconf winconf)
5492     (erase-buffer)
5493     (insert
5494      (cond
5495       ((eq part 'method)
5496        ";; Type `C-c C-c' after editing the select method.\n\n")
5497       ((eq part 'params)
5498        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5499       ((eq part 'info)
5500        ";; Type `C-c C-c' after editing the group info.\n\n")))
5501     (insert
5502      (pp-to-string
5503       (cond ((eq part 'method)
5504              (or (gnus-info-method info) "native"))
5505             ((eq part 'params)
5506              (gnus-info-params info))
5507             (t info)))
5508      "\n")))
5509
5510 (defun gnus-group-edit-group-method (group)
5511   "Edit the select method of GROUP."
5512   (interactive (list (gnus-group-group-name)))
5513   (gnus-group-edit-group group 'method))
5514
5515 (defun gnus-group-edit-group-parameters (group)
5516   "Edit the group parameters of GROUP."
5517   (interactive (list (gnus-group-group-name)))
5518   (gnus-group-edit-group group 'params))
5519
5520 (defun gnus-group-edit-group-done (part group)
5521   "Get info from buffer, update variables and jump to the group buffer."
5522   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5523   (goto-char (point-min))
5524   (let* ((form (read (current-buffer)))
5525          (winconf gnus-prev-winconf)
5526          (method (cond ((eq part 'info) (nth 4 form))
5527                        ((eq part 'method) form)
5528                        (t nil)))
5529          (info (cond ((eq part 'info) form)
5530                      ((eq part 'method) (gnus-get-info group))
5531                      (t nil)))
5532          (new-group (if info
5533                       (if (or (not method)
5534                               (gnus-server-equal
5535                                gnus-select-method method))
5536                           (gnus-group-real-name (car info))
5537                         (gnus-group-prefixed-name
5538                          (gnus-group-real-name (car info)) method))
5539                       nil)))
5540     (when (and new-group
5541                (not (equal new-group group)))
5542       (when (gnus-group-goto-group group)
5543         (gnus-group-kill-group 1))
5544       (gnus-activate-group new-group))
5545     ;; Set the info.
5546     (if (and info new-group)
5547         (progn
5548           (setq info (gnus-copy-sequence info))
5549           (setcar info new-group)
5550           (unless (gnus-server-equal method "native")
5551             (unless (nthcdr 3 info)
5552               (nconc info (list nil nil)))
5553             (unless (nthcdr 4 info)
5554               (nconc info (list nil)))
5555             (gnus-info-set-method info method))
5556           (gnus-group-set-info info))
5557       (gnus-group-set-info form (or new-group group) part))
5558     (kill-buffer (current-buffer))
5559     (and winconf (set-window-configuration winconf))
5560     (set-buffer gnus-group-buffer)
5561     (gnus-group-update-group (or new-group group))
5562     (gnus-group-position-point)))
5563
5564 (defun gnus-group-make-help-group ()
5565   "Create the Gnus documentation group."
5566   (interactive)
5567   (let ((path load-path)
5568         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5569         file dir)
5570     (and (gnus-gethash name gnus-newsrc-hashtb)
5571          (error "Documentation group already exists"))
5572     (while path
5573       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5574             file nil)
5575       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5576                 (file-exists-p
5577                  (setq file (concat (file-name-directory
5578                                      (directory-file-name dir))
5579                                     "etc/gnus-tut.txt"))))
5580         (setq path nil)))
5581     (if (not file)
5582         (message "Couldn't find doc group")
5583       (gnus-group-make-group
5584        (gnus-group-real-name name)
5585        (list 'nndoc name
5586              (list 'nndoc-address file)
5587              (list 'nndoc-article-type 'mbox)))))
5588   (gnus-group-position-point))
5589
5590 (defun gnus-group-make-doc-group (file type)
5591   "Create a group that uses a single file as the source."
5592   (interactive
5593    (list (read-file-name "File name: ")
5594          (and current-prefix-arg 'ask)))
5595   (when (eq type 'ask)
5596     (let ((err "")
5597           char found)
5598       (while (not found)
5599         (message
5600          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5601          err)
5602         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5603                           ((= char ?b) 'babyl)
5604                           ((= char ?d) 'digest)
5605                           ((= char ?f) 'forward)
5606                           ((= char ?a) 'mmfd)
5607                           (t (setq err (format "%c unknown. " char))
5608                              nil))))
5609       (setq type found)))
5610   (let* ((file (expand-file-name file))
5611          (name (gnus-generate-new-group-name
5612                 (gnus-group-prefixed-name
5613                  (file-name-nondirectory file) '(nndoc "")))))
5614     (gnus-group-make-group
5615      (gnus-group-real-name name)
5616      (list 'nndoc name
5617            (list 'nndoc-address file)
5618            (list 'nndoc-article-type (or type 'guess))))
5619     (forward-line -1)
5620     (gnus-group-position-point)))
5621
5622 (defun gnus-group-make-archive-group (&optional all)
5623   "Create the (ding) Gnus archive group of the most recent articles.
5624 Given a prefix, create a full group."
5625   (interactive "P")
5626   (let ((group (gnus-group-prefixed-name
5627                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5628     (and (gnus-gethash group gnus-newsrc-hashtb)
5629          (error "Archive group already exists"))
5630     (gnus-group-make-group
5631      (gnus-group-real-name group)
5632      (list 'nndir (if all "hpc" "edu")
5633            (list 'nndir-directory
5634                  (if all gnus-group-archive-directory
5635                    gnus-group-recent-archive-directory)))))
5636   (forward-line -1)
5637   (gnus-group-position-point))
5638
5639 (defun gnus-group-make-directory-group (dir)
5640   "Create an nndir group.
5641 The user will be prompted for a directory.  The contents of this
5642 directory will be used as a newsgroup.  The directory should contain
5643 mail messages or news articles in files that have numeric names."
5644   (interactive
5645    (list (read-file-name "Create group from directory: ")))
5646   (or (file-exists-p dir) (error "No such directory"))
5647   (or (file-directory-p dir) (error "Not a directory"))
5648   (let ((ext "")
5649         (i 0)
5650         group)
5651     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5652       (setq group
5653             (gnus-group-prefixed-name
5654              (concat (file-name-as-directory (directory-file-name dir))
5655                      ext)
5656              '(nndir "")))
5657       (setq ext (format "<%d>" (setq i (1+ i)))))
5658     (gnus-group-make-group
5659      (gnus-group-real-name group)
5660      (list 'nndir group (list 'nndir-directory dir))))
5661   (forward-line -1)
5662   (gnus-group-position-point))
5663
5664 (defun gnus-group-make-kiboze-group (group address scores)
5665   "Create an nnkiboze group.
5666 The user will be prompted for a name, a regexp to match groups, and
5667 score file entries for articles to include in the group."
5668   (interactive
5669    (list
5670     (read-string "nnkiboze group name: ")
5671     (read-string "Source groups (regexp): ")
5672     (let ((headers (mapcar (lambda (group) (list group))
5673                            '("subject" "from" "number" "date" "message-id"
5674                              "references" "chars" "lines" "xref"
5675                              "followup" "all" "body" "head")))
5676           scores header regexp regexps)
5677       (while (not (equal "" (setq header (completing-read
5678                                           "Match on header: " headers nil t))))
5679         (setq regexps nil)
5680         (while (not (equal "" (setq regexp (read-string
5681                                             (format "Match on %s (string): "
5682                                                     header)))))
5683           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5684         (setq scores (cons (cons header regexps) scores)))
5685       scores)))
5686   (gnus-group-make-group group "nnkiboze" address)
5687   (save-excursion
5688     (gnus-set-work-buffer)
5689     (let (emacs-lisp-mode-hook)
5690       (pp scores (current-buffer)))
5691     (write-region (point-min) (point-max)
5692                   (gnus-score-file-name (concat "nnkiboze:" group))))
5693   (forward-line -1)
5694   (gnus-group-position-point))
5695
5696 (defun gnus-group-add-to-virtual (n vgroup)
5697   "Add the current group to a virtual group."
5698   (interactive
5699    (list current-prefix-arg
5700          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5701                           "nnvirtual:")))
5702   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5703       (error "%s is not an nnvirtual group" vgroup))
5704   (let* ((groups (gnus-group-process-prefix n))
5705          (method (gnus-info-method (gnus-get-info vgroup))))
5706     (setcar (cdr method)
5707             (concat
5708              (nth 1 method) "\\|"
5709              (mapconcat
5710               (lambda (s)
5711                 (gnus-group-remove-mark s)
5712                 (concat "\\(^" (regexp-quote s) "$\\)"))
5713               groups "\\|"))))
5714   (gnus-group-position-point))
5715
5716 (defun gnus-group-make-empty-virtual (group)
5717   "Create a new, fresh, empty virtual group."
5718   (interactive "sCreate new, empty virtual group: ")
5719   (let* ((method (list 'nnvirtual "^$"))
5720          (pgroup (gnus-group-prefixed-name group method)))
5721     ;; Check whether it exists already.
5722     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5723          (error "Group %s already exists." pgroup))
5724     ;; Subscribe the new group after the group on the current line.
5725     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5726     (gnus-group-update-group pgroup)
5727     (forward-line -1)
5728     (gnus-group-position-point)))
5729
5730 (defun gnus-group-enter-directory (dir)
5731   "Enter an ephemeral nneething group."
5732   (interactive "DDirectory to read: ")
5733   (let* ((method (list 'nneething dir))
5734          (leaf (gnus-group-prefixed-name
5735                 (file-name-nondirectory (directory-file-name dir))
5736                 method))
5737          (name (gnus-generate-new-group-name leaf)))
5738     (let ((nneething-read-only t))
5739       (or (gnus-group-read-ephemeral-group
5740            name method t
5741            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5742                                       'summary 'group)))
5743           (error "Couldn't enter %s" dir)))))
5744
5745 ;; Group sorting commands
5746 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5747
5748 (defun gnus-group-sort-groups (func &optional reverse)
5749   "Sort the group buffer according to FUNC.
5750 If REVERSE, reverse the sorting order."
5751   (interactive (list gnus-group-sort-function
5752                      current-prefix-arg))
5753   (let ((func (cond 
5754                ((not (listp func)) func)
5755                ((null func) func)
5756                ((= 1 (length func)) (car func))
5757                (t `(lambda (t1 t2)
5758                      ,(gnus-make-sort-function 
5759                        (reverse func)))))))
5760     ;; We peel off the dummy group from the alist.
5761     (when func
5762       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5763         (pop gnus-newsrc-alist))
5764       ;; Do the sorting.
5765       (setq gnus-newsrc-alist
5766             (sort gnus-newsrc-alist func))
5767       (when reverse
5768         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5769       ;; Regenerate the hash table.
5770       (gnus-make-hashtable-from-newsrc-alist)
5771       (gnus-group-list-groups))))
5772
5773 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5774   "Sort the group buffer alphabetically by group name.
5775 If REVERSE, sort in reverse order."
5776   (interactive "P")
5777   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5778
5779 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5780   "Sort the group buffer by number of unread articles.
5781 If REVERSE, sort in reverse order."
5782   (interactive "P")
5783   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5784
5785 (defun gnus-group-sort-groups-by-level (&optional reverse)
5786   "Sort the group buffer by group level.
5787 If REVERSE, sort in reverse order."
5788   (interactive "P")
5789   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5790
5791 (defun gnus-group-sort-groups-by-score (&optional reverse)
5792   "Sort the group buffer by group score.
5793 If REVERSE, sort in reverse order."
5794   (interactive "P")
5795   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5796
5797 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5798   "Sort the group buffer by group rank.
5799 If REVERSE, sort in reverse order."
5800   (interactive "P")
5801   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5802
5803 (defun gnus-group-sort-groups-by-method (&optional reverse)
5804   "Sort the group buffer alphabetically by backend name.
5805 If REVERSE, sort in reverse order."
5806   (interactive "P")
5807   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5808
5809 (defun gnus-group-sort-by-alphabet (info1 info2)
5810   "Sort alphabetically."
5811   (string< (gnus-info-group info1) (gnus-info-group info2)))
5812
5813 (defun gnus-group-sort-by-unread (info1 info2)
5814   "Sort by number of unread articles."
5815   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5816         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5817     (< (or (and (numberp n1) n1) 0)
5818        (or (and (numberp n2) n2) 0))))
5819
5820 (defun gnus-group-sort-by-level (info1 info2)
5821   "Sort by level."
5822   (< (gnus-info-level info1) (gnus-info-level info2)))
5823
5824 (defun gnus-group-sort-by-method (info1 info2)
5825   "Sort alphabetically by backend name."
5826   (string< (symbol-name (car (gnus-find-method-for-group
5827                               (gnus-info-group info1) info1)))
5828            (symbol-name (car (gnus-find-method-for-group
5829                               (gnus-info-group info2) info2)))))
5830
5831 (defun gnus-group-sort-by-score (info1 info2)
5832   "Sort by group score."
5833   (< (gnus-info-score info1) (gnus-info-score info2)))
5834
5835 (defun gnus-group-sort-by-rank (info1 info2)
5836   "Sort by level and score."
5837   (let ((level1 (gnus-info-level info1))
5838         (level2 (gnus-info-level info2)))
5839     (or (< level1 level2)
5840         (and (= level1 level2)
5841              (< (gnus-info-score info1) (gnus-info-score info2))))))
5842
5843 ;; Group catching up.
5844
5845 (defun gnus-group-catchup-current (&optional n all)
5846   "Mark all articles not marked as unread in current newsgroup as read.
5847 If prefix argument N is numeric, the ARG next newsgroups will be
5848 caught up.  If ALL is non-nil, marked articles will also be marked as
5849 read.  Cross references (Xref: header) of articles are ignored.
5850 The difference between N and actual number of newsgroups that were
5851 caught up is returned."
5852   (interactive "P")
5853   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5854                gnus-expert-user
5855                (gnus-y-or-n-p
5856                 (if all
5857                     "Do you really want to mark all articles as read? "
5858                   "Mark all unread articles as read? "))))
5859       n
5860     (let ((groups (gnus-group-process-prefix n))
5861           (ret 0))
5862       (while groups
5863         ;; Virtual groups have to be given special treatment.
5864         (let ((method (gnus-find-method-for-group (car groups))))
5865           (if (eq 'nnvirtual (car method))
5866               (nnvirtual-catchup-group
5867                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5868         (gnus-group-remove-mark (car groups))
5869         (if (prog1
5870                 (gnus-group-goto-group (car groups))
5871               (gnus-group-catchup (car groups) all))
5872             (gnus-group-update-group-line)
5873           (setq ret (1+ ret)))
5874         (setq groups (cdr groups)))
5875       (gnus-group-next-unread-group 1)
5876       ret)))
5877
5878 (defun gnus-group-catchup-current-all (&optional n)
5879   "Mark all articles in current newsgroup as read.
5880 Cross references (Xref: header) of articles are ignored."
5881   (interactive "P")
5882   (gnus-group-catchup-current n 'all))
5883
5884 (defun gnus-group-catchup (group &optional all)
5885   "Mark all articles in GROUP as read.
5886 If ALL is non-nil, all articles are marked as read.
5887 The return value is the number of articles that were marked as read,
5888 or nil if no action could be taken."
5889   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5890          (num (car entry)))
5891     ;; Do the updating only if the newsgroup isn't killed.
5892     (if (not (numberp (car entry)))
5893         (gnus-message 1 "Can't catch up; non-active group")
5894       ;; Do auto-expirable marks if that's required.
5895       (when (gnus-group-auto-expirable-p group)
5896         (gnus-add-marked-articles
5897          group 'expire (gnus-list-of-unread-articles group))
5898         (when all
5899           (let ((marks (nth 3 (nth 2 entry))))
5900             (gnus-add-marked-articles
5901              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5902             (gnus-add-marked-articles
5903              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5904       (when entry
5905         (gnus-update-read-articles group nil)
5906         ;; Also nix out the lists of marks and dormants.
5907         (when all
5908           (gnus-add-marked-articles group 'tick nil nil 'force)
5909           (gnus-add-marked-articles group 'dormant nil nil 'force))
5910         (run-hooks 'gnus-group-catchup-group-hook)
5911         num))))
5912
5913 (defun gnus-group-expire-articles (&optional n)
5914   "Expire all expirable articles in the current newsgroup."
5915   (interactive "P")
5916   (let ((groups (gnus-group-process-prefix n))
5917         group)
5918     (unless groups
5919       (error "No groups to expire"))
5920     (while (setq group (pop groups))
5921       (gnus-group-remove-mark group)
5922       (when (gnus-check-backend-function 'request-expire-articles group)
5923         (gnus-message 6 "Expiring articles in %s..." group)
5924         (let* ((info (gnus-get-info group))
5925                (expirable (if (gnus-group-total-expirable-p group)
5926                               (cons nil (gnus-list-of-read-articles group))
5927                             (assq 'expire (gnus-info-marks info))))
5928                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5929           (when expirable
5930             (setcdr
5931              expirable
5932              (gnus-compress-sequence
5933               (if expiry-wait
5934                   ;; We set the expiry variables to the groupp
5935                   ;; parameter. 
5936                   (let ((nnmail-expiry-wait-function nil)
5937                         (nnmail-expiry-wait expiry-wait))
5938                     (gnus-request-expire-articles
5939                      (gnus-uncompress-sequence (cdr expirable)) group))
5940                 ;; Just expire using the normal expiry values.
5941                 (gnus-request-expire-articles
5942                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5943           (gnus-message 6 "Expiring articles in %s...done" group)))
5944       (gnus-group-position-point))))
5945
5946 (defun gnus-group-expire-all-groups ()
5947   "Expire all expirable articles in all newsgroups."
5948   (interactive)
5949   (save-excursion
5950     (gnus-message 5 "Expiring...")
5951     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5952                                      (cdr gnus-newsrc-alist))))
5953       (gnus-group-expire-articles nil)))
5954   (gnus-group-position-point)
5955   (gnus-message 5 "Expiring...done"))
5956
5957 (defun gnus-group-set-current-level (n level)
5958   "Set the level of the next N groups to LEVEL."
5959   (interactive
5960    (list
5961     current-prefix-arg
5962     (string-to-int
5963      (let ((s (read-string
5964                (format "Level (default %s): "
5965                        (or (gnus-group-group-level) 
5966                            gnus-level-default-subscribed)))))
5967        (if (string-match "^\\s-*$" s)
5968            (int-to-string (or (gnus-group-group-level) 
5969                               gnus-level-default-subscribed))
5970          s)))))
5971   (or (and (>= level 1) (<= level gnus-level-killed))
5972       (error "Illegal level: %d" level))
5973   (let ((groups (gnus-group-process-prefix n))
5974         group)
5975     (while (setq group (pop groups))
5976       (gnus-group-remove-mark group)
5977       (gnus-message 6 "Changed level of %s from %d to %d"
5978                     group (or (gnus-group-group-level) gnus-level-killed)
5979                     level)
5980       (gnus-group-change-level
5981        group level (or (gnus-group-group-level) gnus-level-killed))
5982       (gnus-group-update-group-line)))
5983   (gnus-group-position-point))
5984
5985 (defun gnus-group-unsubscribe-current-group (&optional n)
5986   "Toggle subscription of the current group.
5987 If given numerical prefix, toggle the N next groups."
5988   (interactive "P")
5989   (let ((groups (gnus-group-process-prefix n))
5990         group)
5991     (while groups
5992       (setq group (car groups)
5993             groups (cdr groups))
5994       (gnus-group-remove-mark group)
5995       (gnus-group-unsubscribe-group
5996        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5997                  gnus-level-default-unsubscribed
5998                gnus-level-default-subscribed) t)
5999       (gnus-group-update-group-line))
6000     (gnus-group-next-group 1)))
6001
6002 (defun gnus-group-unsubscribe-group (group &optional level silent)
6003   "Toggle subscription to GROUP.
6004 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6005 group line."
6006   (interactive
6007    (list (completing-read
6008           "Group: " gnus-active-hashtb nil
6009           (memq gnus-select-method gnus-have-read-active-file))))
6010   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6011     (cond
6012      ((string-match "^[ \t]$" group)
6013       (error "Empty group name"))
6014      (newsrc
6015       ;; Toggle subscription flag.
6016       (gnus-group-change-level
6017        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6018                                       gnus-level-subscribed)
6019                                   (1+ gnus-level-subscribed)
6020                                 gnus-level-default-subscribed)))
6021       (unless silent
6022         (gnus-group-update-group group)))
6023      ((and (stringp group)
6024            (or (not (memq gnus-select-method gnus-have-read-active-file))
6025                (gnus-active group)))
6026       ;; Add new newsgroup.
6027       (gnus-group-change-level
6028        group
6029        (if level level gnus-level-default-subscribed)
6030        (or (and (member group gnus-zombie-list)
6031                 gnus-level-zombie)
6032            gnus-level-killed)
6033        (and (gnus-group-group-name)
6034             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6035       (unless silent
6036         (gnus-group-update-group group)))
6037      (t (error "No such newsgroup: %s" group)))
6038     (gnus-group-position-point)))
6039
6040 (defun gnus-group-transpose-groups (n)
6041   "Move the current newsgroup up N places.
6042 If given a negative prefix, move down instead.  The difference between
6043 N and the number of steps taken is returned."
6044   (interactive "p")
6045   (or (gnus-group-group-name)
6046       (error "No group on current line"))
6047   (gnus-group-kill-group 1)
6048   (prog1
6049       (forward-line (- n))
6050     (gnus-group-yank-group)
6051     (gnus-group-position-point)))
6052
6053 (defun gnus-group-kill-all-zombies ()
6054   "Kill all zombie newsgroups."
6055   (interactive)
6056   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6057   (setq gnus-zombie-list nil)
6058   (gnus-group-list-groups))
6059
6060 (defun gnus-group-kill-region (begin end)
6061   "Kill newsgroups in current region (excluding current point).
6062 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6063   (interactive "r")
6064   (let ((lines
6065          ;; Count lines.
6066          (save-excursion
6067            (count-lines
6068             (progn
6069               (goto-char begin)
6070               (beginning-of-line)
6071               (point))
6072             (progn
6073               (goto-char end)
6074               (beginning-of-line)
6075               (point))))))
6076     (goto-char begin)
6077     (beginning-of-line)                 ;Important when LINES < 1
6078     (gnus-group-kill-group lines)))
6079
6080 (defun gnus-group-kill-group (&optional n discard)
6081   "Kill the next N groups.
6082 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6083 However, only groups that were alive can be yanked; already killed
6084 groups or zombie groups can't be yanked.
6085 The return value is the name of the group that was killed, or a list
6086 of groups killed."
6087   (interactive "P")
6088   (let ((buffer-read-only nil)
6089         (groups (gnus-group-process-prefix n))
6090         group entry level out)
6091     (if (< (length groups) 10)
6092         ;; This is faster when there are few groups.
6093         (while groups
6094           (push (setq group (pop groups)) out)
6095           (gnus-group-remove-mark group)
6096           (setq level (gnus-group-group-level))
6097           (gnus-delete-line)
6098           (when (and (not discard)
6099                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6100             (push (cons (car entry) (nth 2 entry))
6101                   gnus-list-of-killed-groups))
6102           (gnus-group-change-level
6103            (if entry entry group) gnus-level-killed (if entry nil level)))
6104       ;; If there are lots and lots of groups to be killed, we use
6105       ;; this thing instead.
6106       (let (entry)
6107         (setq groups (nreverse groups))
6108         (while groups
6109           (gnus-group-remove-mark (setq group (pop groups)))
6110           (gnus-delete-line)
6111           (cond
6112            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6113             (push (cons (car entry) (nth 2 entry))
6114                   gnus-list-of-killed-groups)
6115             (setcdr (cdr entry) (cdddr entry)))
6116            ((member group gnus-zombie-list)
6117             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6118         (gnus-make-hashtable-from-newsrc-alist)))
6119
6120     (gnus-group-position-point)
6121     (if (< (length out) 2) (car out) (nreverse out))))
6122
6123 (defun gnus-group-yank-group (&optional arg)
6124   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6125 inserting it before the current newsgroup.  The numeric ARG specifies
6126 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6127 is returned, or (if several groups are yanked) a list of yanked groups
6128 is returned."
6129   (interactive "p")
6130   (setq arg (or arg 1))
6131   (let (info group prev out)
6132     (while (>= (decf arg) 0)
6133       (if (not (setq info (pop gnus-list-of-killed-groups)))
6134           (error "No more newsgroups to yank"))
6135       (push (setq group (nth 1 info)) out)
6136       ;; Find which newsgroup to insert this one before - search
6137       ;; backward until something suitable is found.  If there are no
6138       ;; other newsgroups in this buffer, just make this newsgroup the
6139       ;; first newsgroup.
6140       (setq prev (gnus-group-group-name))
6141       (gnus-group-change-level
6142        info (gnus-info-level (cdr info)) gnus-level-killed
6143        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6144        t)
6145       (gnus-group-insert-group-line-info group))
6146     (forward-line -1)
6147     (gnus-group-position-point)
6148     (if (< (length out) 2) (car out) (nreverse out))))
6149
6150 (defun gnus-group-kill-level (level)
6151   "Kill all groups that is on a certain LEVEL."
6152   (interactive "nKill all groups on level: ")
6153   (cond
6154    ((= level gnus-level-zombie)
6155     (setq gnus-killed-list
6156           (nconc gnus-zombie-list gnus-killed-list))
6157     (setq gnus-zombie-list nil))
6158    ((and (< level gnus-level-zombie)
6159          (> level 0)
6160          (or gnus-expert-user
6161              (gnus-yes-or-no-p
6162               (format
6163                "Do you really want to kill all groups on level %d? "
6164                level))))
6165     (let* ((prev gnus-newsrc-alist)
6166            (alist (cdr prev)))
6167       (while alist
6168         (if (= (gnus-info-level level) level)
6169             (setcdr prev (cdr alist))
6170           (setq prev alist))
6171         (setq alist (cdr alist)))
6172       (gnus-make-hashtable-from-newsrc-alist)
6173       (gnus-group-list-groups)))
6174    (t
6175     (error "Can't kill; illegal level: %d" level))))
6176
6177 (defun gnus-group-list-all-groups (&optional arg)
6178   "List all newsgroups with level ARG or lower.
6179 Default is gnus-level-unsubscribed, which lists all subscribed and most
6180 unsubscribed groups."
6181   (interactive "P")
6182   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6183
6184 ;; Redefine this to list ALL killed groups if prefix arg used.
6185 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6186 (defun gnus-group-list-killed (&optional arg)
6187   "List all killed newsgroups in the group buffer.
6188 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6189 entail asking the server for the groups."
6190   (interactive "P")
6191   ;; Find all possible killed newsgroups if arg.
6192   (when arg
6193     ;; First make sure active file has been read.
6194     (unless gnus-have-read-active-file
6195       (let ((gnus-read-active-file t))
6196         (gnus-read-active-file)))
6197     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6198     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6199     (mapatoms
6200      (lambda (sym)
6201        (let ((groups 0)
6202              (group (symbol-name sym)))
6203          (if (or (null group)
6204                  (gnus-gethash group gnus-killed-hashtb)
6205                  (gnus-gethash group gnus-newsrc-hashtb))
6206              ()
6207            (let ((do-sub (gnus-matches-options-n group)))
6208              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6209                  ()
6210                (setq groups (1+ groups))
6211                (setq gnus-killed-list
6212                      (cons group gnus-killed-list))
6213                (gnus-sethash group group gnus-killed-hashtb))))))
6214      gnus-active-hashtb))
6215   (if (not gnus-killed-list)
6216       (gnus-message 6 "No killed groups")
6217     (let (gnus-group-list-mode)
6218       (funcall gnus-group-prepare-function
6219                gnus-level-killed t gnus-level-killed))
6220     (goto-char (point-min)))
6221   (gnus-group-position-point))
6222
6223 (defun gnus-group-list-zombies ()
6224   "List all zombie newsgroups in the group buffer."
6225   (interactive)
6226   (if (not gnus-zombie-list)
6227       (gnus-message 6 "No zombie groups")
6228     (let (gnus-group-list-mode)
6229       (funcall gnus-group-prepare-function
6230                gnus-level-zombie t gnus-level-zombie))
6231     (goto-char (point-min)))
6232   (gnus-group-position-point))
6233
6234 (defun gnus-group-list-active ()
6235   "List all groups that are available from the server(s)."
6236   (interactive)
6237   ;; First we make sure that we have really read the active file.
6238   (unless gnus-have-read-active-file
6239     (let ((gnus-read-active-file t))
6240       (gnus-read-active-file)))
6241   ;; Find all groups and sort them.
6242   (let ((groups
6243          (sort
6244           (let (list)
6245             (mapatoms
6246              (lambda (sym)
6247                (and (symbol-value sym)
6248                     (setq list (cons (symbol-name sym) list))))
6249              gnus-active-hashtb)
6250             list)
6251           'string<))
6252         (buffer-read-only nil))
6253     (erase-buffer)
6254     (while groups
6255       (gnus-group-insert-group-line-info (car groups))
6256       (setq groups (cdr groups)))
6257     (goto-char (point-min))))
6258
6259 (defun gnus-activate-all-groups (level)
6260   "Activate absolutely all groups."
6261   (interactive (list 7))
6262   (let ((gnus-activate-level level)
6263         (gnus-activate-foreign-newsgroups level))
6264     (gnus-group-get-new-news)))
6265
6266 (defun gnus-group-get-new-news (&optional arg)
6267   "Get newly arrived articles.
6268 If ARG is a number, it specifies which levels you are interested in
6269 re-scanning.  If ARG is non-nil and not a number, this will force
6270 \"hard\" re-reading of the active files from all servers."
6271   (interactive "P")
6272   (run-hooks 'gnus-get-new-news-hook)
6273   ;; We might read in new NoCeM messages here.
6274   (when (and gnus-use-nocem 
6275              (null arg))
6276     (gnus-nocem-scan-groups))
6277   ;; If ARG is not a number, then we read the active file.
6278   (when (and arg (not (numberp arg)))
6279     (let ((gnus-read-active-file t))
6280       (gnus-read-active-file))
6281     (setq arg nil))
6282
6283   (setq arg (gnus-group-default-level arg t))
6284   (if (and gnus-read-active-file (not arg))
6285       (progn
6286         (gnus-read-active-file)
6287         (gnus-get-unread-articles arg))
6288     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6289       (gnus-get-unread-articles arg)))
6290   (run-hooks 'gnus-after-getting-new-news-hook)
6291   (gnus-group-list-groups))
6292
6293 (defun gnus-group-get-new-news-this-group (&optional n)
6294   "Check for newly arrived news in the current group (and the N-1 next groups).
6295 The difference between N and the number of newsgroup checked is returned.
6296 If N is negative, this group and the N-1 previous groups will be checked."
6297   (interactive "P")
6298   (let* ((groups (gnus-group-process-prefix n))
6299          (ret (if (numberp n) (- n (length groups)) 0))
6300          group)
6301     (while groups
6302       (setq group (car groups)
6303             groups (cdr groups))
6304       (gnus-group-remove-mark group)
6305       (unless (gnus-get-new-news-in-group group)
6306         (ding)
6307         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6308     (when gnus-goto-next-group-when-activating
6309       (gnus-group-next-unread-group 1 t))
6310     (gnus-summary-position-point)
6311     ret))
6312
6313 (defun gnus-get-new-news-in-group (group)
6314   (when (and group (gnus-activate-group group 'scan))
6315     (gnus-get-unread-articles-in-group
6316      (gnus-get-info group) (gnus-active group) t)
6317     (gnus-close-group group)
6318     (when (gnus-group-goto-group group)
6319       (gnus-group-update-group-line))
6320     t))
6321
6322 (defun gnus-group-fetch-faq (group &optional faq-dir)
6323   "Fetch the FAQ for the current group."
6324   (interactive
6325    (list
6326     (gnus-group-real-name (gnus-group-group-name))
6327     (cond (current-prefix-arg
6328            (completing-read
6329             "Faq dir: " (and (listp gnus-group-faq-directory)
6330                              gnus-group-faq-directory))))))
6331   (or faq-dir
6332       (setq faq-dir (if (listp gnus-group-faq-directory)
6333                         (car gnus-group-faq-directory)
6334                       gnus-group-faq-directory)))
6335   (or group (error "No group name given"))
6336   (let ((file (concat (file-name-as-directory faq-dir)
6337                       (gnus-group-real-name group))))
6338     (if (not (file-exists-p file))
6339         (error "No such file: %s" file)
6340       (find-file file))))
6341
6342 (defun gnus-group-describe-group (force &optional group)
6343   "Display a description of the current newsgroup."
6344   (interactive (list current-prefix-arg (gnus-group-group-name)))
6345   (and force (setq gnus-description-hashtb nil))
6346   (let ((method (gnus-find-method-for-group group))
6347         desc)
6348     (or group (error "No group name given"))
6349     (and (or (and gnus-description-hashtb
6350                   ;; We check whether this group's method has been
6351                   ;; queried for a description file.
6352                   (gnus-gethash
6353                    (gnus-group-prefixed-name "" method)
6354                    gnus-description-hashtb))
6355              (setq desc (gnus-group-get-description group))
6356              (gnus-read-descriptions-file method))
6357          (message
6358           (or desc (gnus-gethash group gnus-description-hashtb)
6359               "No description available")))))
6360
6361 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6362 (defun gnus-group-describe-all-groups (&optional force)
6363   "Pop up a buffer with descriptions of all newsgroups."
6364   (interactive "P")
6365   (and force (setq gnus-description-hashtb nil))
6366   (if (not (or gnus-description-hashtb
6367                (gnus-read-all-descriptions-files)))
6368       (error "Couldn't request descriptions file"))
6369   (let ((buffer-read-only nil)
6370         b)
6371     (erase-buffer)
6372     (mapatoms
6373      (lambda (group)
6374        (setq b (point))
6375        (insert (format "      *: %-20s %s\n" (symbol-name group)
6376                        (symbol-value group)))
6377        (add-text-properties
6378         b (1+ b) (list 'gnus-group group
6379                        'gnus-unread t 'gnus-marked nil
6380                        'gnus-level (1+ gnus-level-subscribed))))
6381      gnus-description-hashtb)
6382     (goto-char (point-min))
6383     (gnus-group-position-point)))
6384
6385 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6386 (defun gnus-group-apropos (regexp &optional search-description)
6387   "List all newsgroups that have names that match a regexp."
6388   (interactive "sGnus apropos (regexp): ")
6389   (let ((prev "")
6390         (obuf (current-buffer))
6391         groups des)
6392     ;; Go through all newsgroups that are known to Gnus.
6393     (mapatoms
6394      (lambda (group)
6395        (and (symbol-name group)
6396             (string-match regexp (symbol-name group))
6397             (setq groups (cons (symbol-name group) groups))))
6398      gnus-active-hashtb)
6399     ;; Also go through all descriptions that are known to Gnus.
6400     (when search-description
6401       (mapatoms
6402        (lambda (group)
6403          (and (string-match regexp (symbol-value group))
6404               (gnus-active (symbol-name group))
6405               (setq groups (cons (symbol-name group) groups))))
6406        gnus-description-hashtb))
6407     (if (not groups)
6408         (gnus-message 3 "No groups matched \"%s\"." regexp)
6409       ;; Print out all the groups.
6410       (save-excursion
6411         (pop-to-buffer "*Gnus Help*")
6412         (buffer-disable-undo (current-buffer))
6413         (erase-buffer)
6414         (setq groups (sort groups 'string<))
6415         (while groups
6416           ;; Groups may be entered twice into the list of groups.
6417           (if (not (string= (car groups) prev))
6418               (progn
6419                 (insert (setq prev (car groups)) "\n")
6420                 (if (and gnus-description-hashtb
6421                          (setq des (gnus-gethash (car groups)
6422                                                  gnus-description-hashtb)))
6423                     (insert "  " des "\n"))))
6424           (setq groups (cdr groups)))
6425         (goto-char (point-min))))
6426     (pop-to-buffer obuf)))
6427
6428 (defun gnus-group-description-apropos (regexp)
6429   "List all newsgroups that have names or descriptions that match a regexp."
6430   (interactive "sGnus description apropos (regexp): ")
6431   (if (not (or gnus-description-hashtb
6432                (gnus-read-all-descriptions-files)))
6433       (error "Couldn't request descriptions file"))
6434   (gnus-group-apropos regexp t))
6435
6436 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6437 (defun gnus-group-list-matching (level regexp &optional all lowest)
6438   "List all groups with unread articles that match REGEXP.
6439 If the prefix LEVEL is non-nil, it should be a number that says which
6440 level to cut off listing groups.
6441 If ALL, also list groups with no unread articles.
6442 If LOWEST, don't list groups with level lower than LOWEST."
6443   (interactive "P\nsList newsgroups matching: ")
6444   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6445                            all (or lowest 1) regexp)
6446   (goto-char (point-min))
6447   (gnus-group-position-point))
6448
6449 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6450   "List all groups that match REGEXP.
6451 If the prefix LEVEL is non-nil, it should be a number that says which
6452 level to cut off listing groups.
6453 If LOWEST, don't list groups with level lower than LOWEST."
6454   (interactive "P\nsList newsgroups matching: ")
6455   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6456
6457 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6458 (defun gnus-group-save-newsrc (&optional force)
6459   "Save the Gnus startup files.
6460 If FORCE, force saving whether it is necessary or not."
6461   (interactive "P")
6462   (gnus-save-newsrc-file force))
6463
6464 (defun gnus-group-restart (&optional arg)
6465   "Force Gnus to read the .newsrc file."
6466   (interactive "P")
6467   (when (gnus-yes-or-no-p
6468          (format "Are you sure you want to read %s? "
6469                  gnus-current-startup-file))
6470     (gnus-save-newsrc-file)
6471     (gnus-setup-news 'force)
6472     (gnus-group-list-groups arg)))
6473
6474 (defun gnus-group-read-init-file ()
6475   "Read the Gnus elisp init file."
6476   (interactive)
6477   (gnus-read-init-file))
6478
6479 (defun gnus-group-check-bogus-groups (&optional silent)
6480   "Check bogus newsgroups.
6481 If given a prefix, don't ask for confirmation before removing a bogus
6482 group."
6483   (interactive "P")
6484   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6485   (gnus-group-list-groups))
6486
6487 (defun gnus-group-edit-global-kill (&optional article group)
6488   "Edit the global kill file.
6489 If GROUP, edit that local kill file instead."
6490   (interactive "P")
6491   (setq gnus-current-kill-article article)
6492   (gnus-kill-file-edit-file group)
6493   (gnus-message
6494    6
6495    (substitute-command-keys
6496     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6497             (if group "local" "global")))))
6498
6499 (defun gnus-group-edit-local-kill (article group)
6500   "Edit a local kill file."
6501   (interactive (list nil (gnus-group-group-name)))
6502   (gnus-group-edit-global-kill article group))
6503
6504 (defun gnus-group-force-update ()
6505   "Update `.newsrc' file."
6506   (interactive)
6507   (gnus-save-newsrc-file))
6508
6509 (defun gnus-group-suspend ()
6510   "Suspend the current Gnus session.
6511 In fact, cleanup buffers except for group mode buffer.
6512 The hook gnus-suspend-gnus-hook is called before actually suspending."
6513   (interactive)
6514   (run-hooks 'gnus-suspend-gnus-hook)
6515   ;; Kill Gnus buffers except for group mode buffer.
6516   (let ((group-buf (get-buffer gnus-group-buffer)))
6517     ;; Do this on a separate list in case the user does a ^G before we finish
6518     (let ((gnus-buffer-list
6519            (delq group-buf (delq gnus-dribble-buffer
6520                                  (append gnus-buffer-list nil)))))
6521       (while gnus-buffer-list
6522         (gnus-kill-buffer (car gnus-buffer-list))
6523         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6524     (if group-buf
6525         (progn
6526           (setq gnus-buffer-list (list group-buf))
6527           (bury-buffer group-buf)
6528           (delete-windows-on group-buf t)))))
6529
6530 (defun gnus-group-clear-dribble ()
6531   "Clear all information from the dribble buffer."
6532   (interactive)
6533   (gnus-dribble-clear)
6534   (gnus-message 7 "Cleared dribble buffer"))
6535
6536 (defun gnus-group-exit ()
6537   "Quit reading news after updating .newsrc.eld and .newsrc.
6538 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6539   (interactive)
6540   (when 
6541       (or noninteractive                ;For gnus-batch-kill
6542           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6543           (not gnus-interactive-exit)   ;Without confirmation
6544           gnus-expert-user
6545           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6546     (run-hooks 'gnus-exit-gnus-hook)
6547     ;; Offer to save data from non-quitted summary buffers.
6548     (gnus-offer-save-summaries)
6549     ;; Save the newsrc file(s).
6550     (gnus-save-newsrc-file)
6551     ;; Kill-em-all.
6552     (gnus-close-backends)
6553     ;; Reset everything.
6554     (gnus-clear-system)
6555     ;; Allow the user to do things after cleaning up.
6556     (run-hooks 'gnus-after-exiting-gnus-hook)))
6557
6558 (defun gnus-close-backends ()
6559   ;; Send a close request to all backends that support such a request.
6560   (let ((methods gnus-valid-select-methods)
6561         func)
6562     (while methods
6563       (if (fboundp (setq func (intern (concat (caar methods)
6564                                               "-request-close"))))
6565           (funcall func))
6566       (setq methods (cdr methods)))))
6567
6568 (defun gnus-group-quit ()
6569   "Quit reading news without updating .newsrc.eld or .newsrc.
6570 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6571   (interactive)
6572   (when (or noninteractive              ;For gnus-batch-kill
6573             (zerop (buffer-size))
6574             (not (gnus-server-opened gnus-select-method))
6575             gnus-expert-user
6576             (not gnus-current-startup-file)
6577             (gnus-yes-or-no-p
6578              (format "Quit reading news without saving %s? "
6579                      (file-name-nondirectory gnus-current-startup-file))))
6580     (run-hooks 'gnus-exit-gnus-hook)
6581     (if gnus-use-full-window
6582         (delete-other-windows)
6583       (gnus-remove-some-windows))
6584     (gnus-dribble-save)
6585     (gnus-close-backends)
6586     (gnus-clear-system)
6587     ;; Allow the user to do things after cleaning up.
6588     (run-hooks 'gnus-after-exiting-gnus-hook)))
6589
6590 (defun gnus-offer-save-summaries ()
6591   "Offer to save all active summary buffers."
6592   (save-excursion
6593     (let ((buflist (buffer-list))
6594           buffers bufname)
6595       ;; Go through all buffers and find all summaries.
6596       (while buflist
6597         (and (setq bufname (buffer-name (car buflist)))
6598              (string-match "Summary" bufname)
6599              (save-excursion
6600                (set-buffer bufname)
6601                ;; We check that this is, indeed, a summary buffer.
6602                (and (eq major-mode 'gnus-summary-mode)
6603                     ;; Also make sure this isn't bogus.
6604                     gnus-newsgroup-prepared))
6605              (push bufname buffers))
6606         (setq buflist (cdr buflist)))
6607       ;; Go through all these summary buffers and offer to save them.
6608       (when buffers
6609         (map-y-or-n-p
6610          "Update summary buffer %s? "
6611          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6612          buffers)))))
6613
6614 (defun gnus-group-describe-briefly ()
6615   "Give a one line description of the group mode commands."
6616   (interactive)
6617   (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")))
6618
6619 (defun gnus-group-browse-foreign-server (method)
6620   "Browse a foreign news server.
6621 If called interactively, this function will ask for a select method
6622  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6623 If not, METHOD should be a list where the first element is the method
6624 and the second element is the address."
6625   (interactive
6626    (list (let ((how (completing-read
6627                      "Which backend: "
6628                      (append gnus-valid-select-methods gnus-server-alist)
6629                      nil t (cons "nntp" 0))))
6630            ;; We either got a backend name or a virtual server name.
6631            ;; If the first, we also need an address.
6632            (if (assoc how gnus-valid-select-methods)
6633                (list (intern how)
6634                      ;; Suggested by mapjph@bath.ac.uk.
6635                      (completing-read
6636                       "Address: "
6637                       (mapcar (lambda (server) (list server))
6638                               gnus-secondary-servers)))
6639              ;; We got a server name, so we find the method.
6640              (gnus-server-to-method how)))))
6641   (gnus-browse-foreign-server method))
6642
6643 \f
6644 ;;;
6645 ;;; Gnus summary mode
6646 ;;;
6647
6648 (defvar gnus-summary-mode-map nil)
6649
6650 (put 'gnus-summary-mode 'mode-class 'special)
6651
6652 (unless gnus-summary-mode-map
6653   (setq gnus-summary-mode-map (make-keymap))
6654   (suppress-keymap gnus-summary-mode-map)
6655
6656   ;; Non-orthogonal keys
6657
6658   (gnus-define-keys gnus-summary-mode-map
6659     " " gnus-summary-next-page
6660     "\177" gnus-summary-prev-page
6661     [delete] gnus-summary-prev-page
6662     "\r" gnus-summary-scroll-up
6663     "n" gnus-summary-next-unread-article
6664     "p" gnus-summary-prev-unread-article
6665     "N" gnus-summary-next-article
6666     "P" gnus-summary-prev-article
6667     "\M-\C-n" gnus-summary-next-same-subject
6668     "\M-\C-p" gnus-summary-prev-same-subject
6669     "\M-n" gnus-summary-next-unread-subject
6670     "\M-p" gnus-summary-prev-unread-subject
6671     "." gnus-summary-first-unread-article
6672     "," gnus-summary-best-unread-article
6673     "\M-s" gnus-summary-search-article-forward
6674     "\M-r" gnus-summary-search-article-backward
6675     "<" gnus-summary-beginning-of-article
6676     ">" gnus-summary-end-of-article
6677     "j" gnus-summary-goto-article
6678     "^" gnus-summary-refer-parent-article
6679     "\M-^" gnus-summary-refer-article
6680     "u" gnus-summary-tick-article-forward
6681     "!" gnus-summary-tick-article-forward
6682     "U" gnus-summary-tick-article-backward
6683     "d" gnus-summary-mark-as-read-forward
6684     "D" gnus-summary-mark-as-read-backward
6685     "E" gnus-summary-mark-as-expirable
6686     "\M-u" gnus-summary-clear-mark-forward
6687     "\M-U" gnus-summary-clear-mark-backward
6688     "k" gnus-summary-kill-same-subject-and-select
6689     "\C-k" gnus-summary-kill-same-subject
6690     "\M-\C-k" gnus-summary-kill-thread
6691     "\M-\C-l" gnus-summary-lower-thread
6692     "e" gnus-summary-edit-article
6693     "#" gnus-summary-mark-as-processable
6694     "\M-#" gnus-summary-unmark-as-processable
6695     "\M-\C-t" gnus-summary-toggle-threads
6696     "\M-\C-s" gnus-summary-show-thread
6697     "\M-\C-h" gnus-summary-hide-thread
6698     "\M-\C-f" gnus-summary-next-thread
6699     "\M-\C-b" gnus-summary-prev-thread
6700     "\M-\C-u" gnus-summary-up-thread
6701     "\M-\C-d" gnus-summary-down-thread
6702     "&" gnus-summary-execute-command
6703     "c" gnus-summary-catchup-and-exit
6704     "\C-w" gnus-summary-mark-region-as-read
6705     "\C-t" gnus-summary-toggle-truncation
6706     "?" gnus-summary-mark-as-dormant
6707     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6708     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6709     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6710     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6711     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6712     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6713     "=" gnus-summary-expand-window
6714     "\C-x\C-s" gnus-summary-reselect-current-group
6715     "\M-g" gnus-summary-rescan-group
6716     "w" gnus-summary-stop-page-breaking
6717     "\C-c\C-r" gnus-summary-caesar-message
6718     "\M-t" gnus-summary-toggle-mime
6719     "f" gnus-summary-followup
6720     "F" gnus-summary-followup-with-original
6721     "C" gnus-summary-cancel-article
6722     "r" gnus-summary-reply
6723     "R" gnus-summary-reply-with-original
6724     "\C-c\C-f" gnus-summary-mail-forward
6725     "o" gnus-summary-save-article
6726     "\C-o" gnus-summary-save-article-mail
6727     "|" gnus-summary-pipe-output
6728     "\M-k" gnus-summary-edit-local-kill
6729     "\M-K" gnus-summary-edit-global-kill
6730     "V" gnus-version
6731     "\C-c\C-d" gnus-summary-describe-group
6732     "q" gnus-summary-exit
6733     "Q" gnus-summary-exit-no-update
6734     "\C-c\C-i" gnus-info-find-node
6735     gnus-mouse-2 gnus-mouse-pick-article
6736     "m" gnus-summary-mail-other-window
6737     "a" gnus-summary-post-news
6738     "x" gnus-summary-limit-to-unread
6739     "s" gnus-summary-isearch-article
6740     "t" gnus-article-hide-headers
6741     "g" gnus-summary-show-article
6742     "l" gnus-summary-goto-last-article
6743     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6744     "\C-d" gnus-summary-enter-digest-group
6745     "\C-c\C-b" gnus-bug
6746     "*" gnus-cache-enter-article
6747     "\M-*" gnus-cache-remove-article
6748     "\M-&" gnus-summary-universal-argument
6749     "\C-l" gnus-recenter
6750     "I" gnus-summary-increase-score
6751     "L" gnus-summary-lower-score
6752
6753     "V" gnus-summary-score-map
6754     "X" gnus-uu-extract-map
6755     "S" gnus-summary-send-map)
6756
6757   ;; Sort of orthogonal keymap
6758   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6759     "t" gnus-summary-tick-article-forward
6760     "!" gnus-summary-tick-article-forward
6761     "d" gnus-summary-mark-as-read-forward
6762     "r" gnus-summary-mark-as-read-forward
6763     "c" gnus-summary-clear-mark-forward
6764     " " gnus-summary-clear-mark-forward
6765     "e" gnus-summary-mark-as-expirable
6766     "x" gnus-summary-mark-as-expirable
6767     "?" gnus-summary-mark-as-dormant
6768     "b" gnus-summary-set-bookmark
6769     "B" gnus-summary-remove-bookmark
6770     "#" gnus-summary-mark-as-processable
6771     "\M-#" gnus-summary-unmark-as-processable
6772     "S" gnus-summary-limit-include-expunged
6773     "C" gnus-summary-catchup
6774     "H" gnus-summary-catchup-to-here
6775     "\C-c" gnus-summary-catchup-all
6776     "k" gnus-summary-kill-same-subject-and-select
6777     "K" gnus-summary-kill-same-subject
6778     "P" gnus-uu-mark-map)
6779
6780   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6781     "c" gnus-summary-clear-above
6782     "u" gnus-summary-tick-above
6783     "m" gnus-summary-mark-above
6784     "k" gnus-summary-kill-below)
6785
6786   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6787     "/" gnus-summary-limit-to-subject
6788     "n" gnus-summary-limit-to-articles
6789     "w" gnus-summary-pop-limit
6790     "s" gnus-summary-limit-to-subject
6791     "a" gnus-summary-limit-to-author
6792     "u" gnus-summary-limit-to-unread
6793     "m" gnus-summary-limit-to-marks
6794     "v" gnus-summary-limit-to-score
6795     "D" gnus-summary-limit-include-dormant
6796     "d" gnus-summary-limit-exclude-dormant
6797     ;;  "t" gnus-summary-limit-exclude-thread
6798     "E" gnus-summary-limit-include-expunged
6799     "c" gnus-summary-limit-exclude-childless-dormant
6800     "C" gnus-summary-limit-mark-excluded-as-read)
6801
6802   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6803     "n" gnus-summary-next-unread-article
6804     "p" gnus-summary-prev-unread-article
6805     "N" gnus-summary-next-article
6806     "P" gnus-summary-prev-article
6807     "\C-n" gnus-summary-next-same-subject
6808     "\C-p" gnus-summary-prev-same-subject
6809     "\M-n" gnus-summary-next-unread-subject
6810     "\M-p" gnus-summary-prev-unread-subject
6811     "f" gnus-summary-first-unread-article
6812     "b" gnus-summary-best-unread-article
6813     "j" gnus-summary-goto-article
6814     "g" gnus-summary-goto-subject
6815     "l" gnus-summary-goto-last-article
6816     "p" gnus-summary-pop-article)
6817
6818   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6819     "k" gnus-summary-kill-thread
6820     "l" gnus-summary-lower-thread
6821     "i" gnus-summary-raise-thread
6822     "T" gnus-summary-toggle-threads
6823     "t" gnus-summary-rethread-current
6824     "^" gnus-summary-reparent-thread
6825     "s" gnus-summary-show-thread
6826     "S" gnus-summary-show-all-threads
6827     "h" gnus-summary-hide-thread
6828     "H" gnus-summary-hide-all-threads
6829     "n" gnus-summary-next-thread
6830     "p" gnus-summary-prev-thread
6831     "u" gnus-summary-up-thread
6832     "o" gnus-summary-top-thread
6833     "d" gnus-summary-down-thread
6834     "#" gnus-uu-mark-thread
6835     "\M-#" gnus-uu-unmark-thread)
6836
6837   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6838     "c" gnus-summary-catchup-and-exit
6839     "C" gnus-summary-catchup-all-and-exit
6840     "E" gnus-summary-exit-no-update
6841     "Q" gnus-summary-exit
6842     "Z" gnus-summary-exit
6843     "n" gnus-summary-catchup-and-goto-next-group
6844     "R" gnus-summary-reselect-current-group
6845     "G" gnus-summary-rescan-group
6846     "N" gnus-summary-next-group
6847     "P" gnus-summary-prev-group)
6848
6849   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6850     " " gnus-summary-next-page
6851     "n" gnus-summary-next-page
6852     "\177" gnus-summary-prev-page
6853     [delete] gnus-summary-prev-page
6854     "p" gnus-summary-prev-page
6855     "\r" gnus-summary-scroll-up
6856     "<" gnus-summary-beginning-of-article
6857     ">" gnus-summary-end-of-article
6858     "b" gnus-summary-beginning-of-article
6859     "e" gnus-summary-end-of-article
6860     "^" gnus-summary-refer-parent-article
6861     "r" gnus-summary-refer-parent-article
6862     "R" gnus-summary-refer-references
6863     "g" gnus-summary-show-article
6864     "s" gnus-summary-isearch-article)
6865
6866   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6867     "b" gnus-article-add-buttons
6868     "B" gnus-article-add-buttons-to-head
6869     "o" gnus-article-treat-overstrike
6870     ;;  "w" gnus-article-word-wrap
6871     "w" gnus-article-fill-cited-article
6872     "c" gnus-article-remove-cr
6873     "L" gnus-article-remove-trailing-blank-lines
6874     "q" gnus-article-de-quoted-unreadable
6875     "f" gnus-article-display-x-face
6876     "l" gnus-summary-stop-page-breaking
6877     "r" gnus-summary-caesar-message
6878     "t" gnus-article-hide-headers
6879     "v" gnus-summary-verbose-headers
6880     "m" gnus-summary-toggle-mime)
6881
6882   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6883     "a" gnus-article-hide
6884     "h" gnus-article-hide-headers
6885     "b" gnus-article-hide-boring-headers
6886     "s" gnus-article-hide-signature
6887     "c" gnus-article-hide-citation
6888     "p" gnus-article-hide-pgp
6889     "\C-c" gnus-article-hide-citation-maybe)
6890
6891   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6892     "a" gnus-article-highlight
6893     "h" gnus-article-highlight-headers
6894     "c" gnus-article-highlight-citation
6895     "s" gnus-article-highlight-signature)
6896
6897   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6898     "z" gnus-article-date-ut
6899     "u" gnus-article-date-ut
6900     "l" gnus-article-date-local
6901     "e" gnus-article-date-lapsed
6902     "o" gnus-article-date-original)
6903
6904   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6905     "v" gnus-version
6906     "f" gnus-summary-fetch-faq
6907     "d" gnus-summary-describe-group
6908     "h" gnus-summary-describe-briefly
6909     "i" gnus-info-find-node)
6910
6911   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6912     "e" gnus-summary-expire-articles
6913     "\M-\C-e" gnus-summary-expire-articles-now
6914     "\177" gnus-summary-delete-article
6915     [delete] gnus-summary-delete-article
6916     "m" gnus-summary-move-article
6917     "r" gnus-summary-respool-article
6918     "w" gnus-summary-edit-article
6919     "c" gnus-summary-copy-article
6920     "B" gnus-summary-crosspost-article
6921     "q" gnus-summary-respool-query
6922     "i" gnus-summary-import-article)
6923
6924   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6925     "o" gnus-summary-save-article
6926     "m" gnus-summary-save-article-mail
6927     "r" gnus-summary-save-article-rmail
6928     "f" gnus-summary-save-article-file
6929     "b" gnus-summary-save-article-body-file
6930     "h" gnus-summary-save-article-folder
6931     "v" gnus-summary-save-article-vm
6932     "p" gnus-summary-pipe-output
6933     "s" gnus-soup-add-article)
6934   )
6935
6936 \f
6937
6938 (defun gnus-summary-mode (&optional group)
6939   "Major mode for reading articles.
6940
6941 All normal editing commands are switched off.
6942 \\<gnus-summary-mode-map>
6943 Each line in this buffer represents one article.  To read an
6944 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6945 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6946 respectively.
6947
6948 You can also post articles and send mail from this buffer.  To
6949 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6950 of an article, type `\\[gnus-summary-reply]'.
6951
6952 There are approx. one gazillion commands you can execute in this
6953 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6954
6955 The following commands are available:
6956
6957 \\{gnus-summary-mode-map}"
6958   (interactive)
6959   (when (and menu-bar-mode
6960              (gnus-visual-p 'summary-menu 'menu))
6961     (gnus-summary-make-menu-bar))
6962   (kill-all-local-variables)
6963   (let ((locals gnus-summary-local-variables))
6964     (while locals
6965       (if (consp (car locals))
6966           (progn
6967             (make-local-variable (caar locals))
6968             (set (caar locals) (eval (cdar locals))))
6969         (make-local-variable (car locals))
6970         (set (car locals) nil))
6971       (setq locals (cdr locals))))
6972   (gnus-make-thread-indent-array)
6973   (gnus-simplify-mode-line)
6974   (setq major-mode 'gnus-summary-mode)
6975   (setq mode-name "Summary")
6976   (make-local-variable 'minor-mode-alist)
6977   (use-local-map gnus-summary-mode-map)
6978   (buffer-disable-undo (current-buffer))
6979   (setq buffer-read-only t)             ;Disable modification
6980   (setq truncate-lines t)
6981   (setq selective-display t)
6982   (setq selective-display-ellipses t)   ;Display `...'
6983   (setq buffer-display-table gnus-summary-display-table)
6984   (setq gnus-newsgroup-name group)
6985   (run-hooks 'gnus-summary-mode-hook))
6986
6987 (defun gnus-summary-make-display-table ()
6988   ;; Change the display table.  Odd characters have a tendency to mess
6989   ;; up nicely formatted displays - we make all possible glyphs
6990   ;; display only a single character.
6991
6992   ;; We start from the standard display table, if any.
6993   (setq gnus-summary-display-table
6994         (or (copy-sequence standard-display-table)
6995             (make-display-table)))
6996   ;; Nix out all the control chars...
6997   (let ((i 32))
6998     (while (>= (setq i (1- i)) 0)
6999       (aset gnus-summary-display-table i [??])))
7000   ;; ... but not newline and cr, of course. (cr is necessary for the
7001   ;; selective display).
7002   (aset gnus-summary-display-table ?\n nil)
7003   (aset gnus-summary-display-table ?\r nil)
7004   ;; We nix out any glyphs over 126 that are not set already.
7005   (let ((i 256))
7006     (while (>= (setq i (1- i)) 127)
7007       ;; Only modify if the entry is nil.
7008       (or (aref gnus-summary-display-table i)
7009           (aset gnus-summary-display-table i [??])))))
7010
7011 (defun gnus-summary-clear-local-variables ()
7012   (let ((locals gnus-summary-local-variables))
7013     (while locals
7014       (if (consp (car locals))
7015           (and (vectorp (caar locals))
7016                (set (caar locals) nil))
7017         (and (vectorp (car locals))
7018              (set (car locals) nil)))
7019       (setq locals (cdr locals)))))
7020
7021 ;; Summary data functions.
7022
7023 (defmacro gnus-data-number (data)
7024   `(car ,data))
7025
7026 (defmacro gnus-data-set-number (data number)
7027   `(setcar ,data ,number))
7028
7029 (defmacro gnus-data-mark (data)
7030   `(nth 1 ,data))
7031
7032 (defmacro gnus-data-set-mark (data mark)
7033   `(setcar (nthcdr 1 ,data) ,mark))
7034
7035 (defmacro gnus-data-pos (data)
7036   `(nth 2 ,data))
7037
7038 (defmacro gnus-data-set-pos (data pos)
7039   `(setcar (nthcdr 2 ,data) ,pos))
7040
7041 (defmacro gnus-data-header (data)
7042   `(nth 3 ,data))
7043
7044 (defmacro gnus-data-level (data)
7045   `(nth 4 ,data))
7046
7047 (defmacro gnus-data-unread-p (data)
7048   `(= (nth 1 ,data) gnus-unread-mark))
7049
7050 (defmacro gnus-data-pseudo-p (data)
7051   `(consp (nth 3 ,data)))
7052
7053 (defmacro gnus-data-find (number)
7054   `(assq ,number gnus-newsgroup-data))
7055
7056 (defmacro gnus-data-find-list (number &optional data)
7057   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7058      (memq (assq ,number bdata)
7059            bdata)))
7060
7061 (defmacro gnus-data-make (number mark pos header level)
7062   `(list ,number ,mark ,pos ,header ,level))
7063
7064 (defun gnus-data-enter (after-article number mark pos header level offset)
7065   (let ((data (gnus-data-find-list after-article)))
7066     (or data (error "No such article: %d" after-article))
7067     (setcdr data (cons (gnus-data-make number mark pos header level)
7068                        (cdr data)))
7069     (setq gnus-newsgroup-data-reverse nil)
7070     (gnus-data-update-list (cddr data) offset)))
7071
7072 (defun gnus-data-enter-list (after-article list &optional offset)
7073   (when list
7074     (let ((data (and after-article (gnus-data-find-list after-article)))
7075           (ilist list))
7076       (or data (not after-article) (error "No such article: %d" after-article))
7077       ;; Find the last element in the list to be spliced into the main
7078       ;; list.
7079       (while (cdr list)
7080         (setq list (cdr list)))
7081       (if (not data)
7082           (progn
7083             (setcdr list gnus-newsgroup-data)
7084             (setq gnus-newsgroup-data ilist)
7085             (and offset (gnus-data-update-list (cdr list) offset)))
7086         (setcdr list (cdr data))
7087         (setcdr data ilist)
7088         (and offset (gnus-data-update-list (cdr data) offset)))
7089       (setq gnus-newsgroup-data-reverse nil))))
7090
7091 (defun gnus-data-remove (article &optional offset)
7092   (let ((data gnus-newsgroup-data))
7093     (if (= (gnus-data-number (car data)) article)
7094         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7095               gnus-newsgroup-data-reverse nil)
7096       (while (cdr data)
7097         (and (= (gnus-data-number (cadr data)) article)
7098              (progn
7099                (setcdr data (cddr data))
7100                (and offset (gnus-data-update-list (cdr data) offset))
7101                (setq data nil
7102                      gnus-newsgroup-data-reverse nil)))
7103         (setq data (cdr data))))))
7104
7105 (defmacro gnus-data-list (backward)
7106   `(if ,backward
7107        (or gnus-newsgroup-data-reverse
7108            (setq gnus-newsgroup-data-reverse
7109                  (reverse gnus-newsgroup-data)))
7110      gnus-newsgroup-data))
7111
7112 (defun gnus-data-update-list (data offset)
7113   "Add OFFSET to the POS of all data entries in DATA."
7114   (while data
7115     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7116     (setq data (cdr data))))
7117
7118 (defun gnus-data-compute-positions ()
7119   "Compute the positions of all articles."
7120   (let ((data gnus-newsgroup-data)
7121         pos)
7122     (while data
7123       (when (setq pos (text-property-any
7124                        (point-min) (point-max)
7125                        'gnus-number (gnus-data-number (car data))))
7126         (gnus-data-set-pos (car data) (+ pos 3)))
7127       (setq data (cdr data)))))
7128
7129 (defun gnus-summary-article-pseudo-p (article)
7130   "Say whether this article is a pseudo article or not."
7131   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7132
7133 (defun gnus-article-parent-p (number)
7134   "Say whether this article is a parent or not."
7135   (let ((data (gnus-data-find-list number)))
7136     (and (cdr data)                     ; There has to be an article after...
7137          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7138             (gnus-data-level (nth 1 data))))))
7139
7140 (defun gnus-article-children (number)
7141   "Return a list of all children to NUMBER."
7142   (let* ((data (gnus-data-find-list number))
7143          (level (gnus-data-level (car data)))
7144          children)
7145     (setq data (cdr data))
7146     (while (and data            
7147                 (= (gnus-data-level (car data)) (1+ level)))
7148       (push (gnus-data-number (car data)) children)
7149       (setq data (cdr data)))
7150     children))
7151
7152 (defmacro gnus-summary-skip-intangible ()
7153   "If the current article is intangible, then jump to a different article."
7154   '(let ((to (get-text-property (point) 'gnus-intangible)))
7155     (and to (gnus-summary-goto-subject to))))
7156
7157 (defmacro gnus-summary-article-intangible-p ()
7158   "Say whether this article is intangible or not."
7159   '(get-text-property (point) 'gnus-intangible))
7160
7161 ;; Some summary mode macros.
7162
7163 (defmacro gnus-summary-article-number ()
7164   "The article number of the article on the current line.
7165 If there isn's an article number here, then we return the current
7166 article number."
7167   '(progn
7168      (gnus-summary-skip-intangible)
7169      (or (get-text-property (point) 'gnus-number)
7170          (gnus-summary-last-subject))))
7171
7172 (defmacro gnus-summary-article-header (&optional number)
7173   `(gnus-data-header (gnus-data-find
7174                       ,(or number '(gnus-summary-article-number)))))
7175
7176 (defmacro gnus-summary-thread-level (&optional number)
7177   `(if (and (eq gnus-summary-make-false-root 'dummy)
7178             (get-text-property (point) 'gnus-intangible))
7179        0
7180      (gnus-data-level (gnus-data-find
7181                        ,(or number '(gnus-summary-article-number))))))
7182
7183 (defmacro gnus-summary-article-mark (&optional number)
7184   `(gnus-data-mark (gnus-data-find
7185                     ,(or number '(gnus-summary-article-number)))))
7186
7187 (defmacro gnus-summary-article-pos (&optional number)
7188   `(gnus-data-pos (gnus-data-find
7189                    ,(or number '(gnus-summary-article-number)))))
7190
7191 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7192 (defmacro gnus-summary-article-subject (&optional number)
7193   "Return current subject string or nil if nothing."
7194   `(let ((headers
7195           ,(if number
7196                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7197              '(gnus-data-header (assq (gnus-summary-article-number)
7198                                       gnus-newsgroup-data)))))
7199      (and headers
7200           (vectorp headers)
7201           (mail-header-subject headers))))
7202
7203 (defmacro gnus-summary-article-score (&optional number)
7204   "Return current article score."
7205   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7206                   gnus-newsgroup-scored))
7207        gnus-summary-default-score 0))
7208
7209 (defun gnus-summary-article-children (&optional number)
7210   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7211          (level (gnus-data-level (car data)))
7212          l children)
7213     (while (and (setq data (cdr data))
7214                 (> (setq l (gnus-data-level (car data))) level))
7215       (and (= (1+ level) l)
7216            (setq children (cons (gnus-data-number (car data))
7217                                 children))))
7218     (nreverse children)))
7219
7220 (defun gnus-summary-article-parent (&optional number)
7221   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7222                                     (gnus-data-list t)))
7223          (level (gnus-data-level (car data))))
7224     (if (zerop level)
7225         () ; This is a root.
7226       ;; We search until we find an article with a level less than
7227       ;; this one.  That function has to be the parent.
7228       (while (and (setq data (cdr data))
7229                   (not (< (gnus-data-level (car data)) level))))
7230       (and data (gnus-data-number (car data))))))
7231
7232 (defun gnus-unread-mark-p (mark)
7233   "Say whether MARK is the unread mark."
7234   (= mark gnus-unread-mark))
7235
7236 (defun gnus-read-mark-p (mark)
7237   "Say whether MARK is one of the marks that mark as read.
7238 This is all marks except unread, ticked, dormant, and expirable."
7239   (not (or (= mark gnus-unread-mark)
7240            (= mark gnus-ticked-mark)
7241            (= mark gnus-dormant-mark)
7242            (= mark gnus-expirable-mark))))
7243
7244 ;; Various summary mode internalish functions.
7245
7246 (defun gnus-mouse-pick-article (e)
7247   (interactive "e")
7248   (mouse-set-point e)
7249   (gnus-summary-next-page nil t))
7250
7251 (defun gnus-summary-setup-buffer (group)
7252   "Initialize summary buffer."
7253   (let ((buffer (concat "*Summary " group "*")))
7254     (if (get-buffer buffer)
7255         (progn
7256           (set-buffer buffer)
7257           (setq gnus-summary-buffer (current-buffer))
7258           (not gnus-newsgroup-prepared))
7259       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7260       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7261       (gnus-add-current-to-buffer-list)
7262       (gnus-summary-mode group)
7263       (when gnus-carpal
7264         (gnus-carpal-setup-buffer 'summary))
7265       (unless gnus-single-article-buffer
7266         (make-local-variable 'gnus-article-buffer)
7267         (make-local-variable 'gnus-article-current)
7268         (make-local-variable 'gnus-original-article-buffer))
7269       (setq gnus-newsgroup-name group)
7270       t)))
7271
7272 (defun gnus-set-global-variables ()
7273   ;; Set the global equivalents of the summary buffer-local variables
7274   ;; to the latest values they had.  These reflect the summary buffer
7275   ;; that was in action when the last article was fetched.
7276   (when (eq major-mode 'gnus-summary-mode)
7277     (setq gnus-summary-buffer (current-buffer))
7278     (let ((name gnus-newsgroup-name)
7279           (marked gnus-newsgroup-marked)
7280           (unread gnus-newsgroup-unreads)
7281           (headers gnus-current-headers)
7282           (data gnus-newsgroup-data)
7283           (summary gnus-summary-buffer)
7284           (article-buffer gnus-article-buffer)
7285           (original gnus-original-article-buffer)
7286           (gac gnus-article-current)
7287           (score-file gnus-current-score-file))
7288       (save-excursion
7289         (set-buffer gnus-group-buffer)
7290         (setq gnus-newsgroup-name name)
7291         (setq gnus-newsgroup-marked marked)
7292         (setq gnus-newsgroup-unreads unread)
7293         (setq gnus-current-headers headers)
7294         (setq gnus-newsgroup-data data)
7295         (setq gnus-article-current gac)
7296         (setq gnus-summary-buffer summary)
7297         (setq gnus-article-buffer article-buffer)
7298         (setq gnus-original-article-buffer original)
7299         (setq gnus-current-score-file score-file)))))
7300
7301 (defun gnus-summary-last-article-p (&optional article)
7302   "Return whether ARTICLE is the last article in the buffer."
7303   (if (not (setq article (or article (gnus-summary-article-number))))
7304       t ; All non-existant numbers are the last article. :-)
7305     (cdr (gnus-data-find-list article))))
7306
7307 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7308   "Insert a dummy root in the summary buffer."
7309   (beginning-of-line)
7310   (add-text-properties
7311    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7312    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7313
7314 (defvar gnus-thread-indent-array nil)
7315 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7316 (defun gnus-make-thread-indent-array ()
7317   (let ((n 200))
7318     (unless (and gnus-thread-indent-array
7319                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7320       (setq gnus-thread-indent-array (make-vector 201 "")
7321             gnus-thread-indent-array-level gnus-thread-indent-level)
7322       (while (>= n 0)
7323         (aset gnus-thread-indent-array n
7324               (make-string (* n gnus-thread-indent-level) ? ))
7325         (setq n (1- n))))))
7326
7327 (defun gnus-summary-insert-line
7328   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7329                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7330                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7331   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7332          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7333          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7334          (gnus-tmp-score-char
7335           (if (or (null gnus-summary-default-score)
7336                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7337                       gnus-summary-zcore-fuzz)) ? 
7338             (if (< gnus-tmp-score gnus-summary-default-score)
7339                 gnus-score-below-mark gnus-score-over-mark)))
7340          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7341                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7342                                   gnus-cached-mark)
7343                                  (gnus-tmp-replied gnus-replied-mark)
7344                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7345                                   gnus-saved-mark)
7346                                  (t gnus-unread-mark)))
7347          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7348          (gnus-tmp-name
7349           (cond
7350            ((string-match "(.+)" gnus-tmp-from)
7351             (substring gnus-tmp-from
7352                        (1+ (match-beginning 0)) (1- (match-end 0))))
7353            ((string-match "<[^>]+> *$" gnus-tmp-from)
7354             (let ((beg (match-beginning 0)))
7355               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7356                        (substring gnus-tmp-from (1+ (match-beginning 0))
7357                                   (1- (match-end 0))))
7358                   (substring gnus-tmp-from 0 beg))))
7359            (t gnus-tmp-from)))
7360          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7361          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7362          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7363          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7364          (buffer-read-only nil))
7365     (when (string= gnus-tmp-name "")
7366       (setq gnus-tmp-name gnus-tmp-from))
7367     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7368     (put-text-property
7369      (point)
7370      (progn (eval gnus-summary-line-format-spec) (point))
7371      'gnus-number gnus-tmp-number)
7372     (when (gnus-visual-p 'summary-highlight 'highlight)
7373       (forward-line -1)
7374       (run-hooks 'gnus-summary-update-hook)
7375       (forward-line 1))))
7376
7377 (defun gnus-summary-update-line (&optional dont-update)
7378   ;; Update summary line after change.
7379   (when (and gnus-summary-default-score
7380              (not gnus-summary-inhibit-highlight))
7381     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7382            (article (gnus-summary-article-number))
7383            (score (gnus-summary-article-score article)))
7384       (unless dont-update
7385         (if (and gnus-summary-mark-below
7386                  (< (gnus-summary-article-score)
7387                     gnus-summary-mark-below))
7388             ;; This article has a low score, so we mark it as read.
7389             (when (memq article gnus-newsgroup-unreads)
7390               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7391           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7392             ;; This article was previously marked as read on account
7393             ;; of a low score, but now it has risen, so we mark it as
7394             ;; unread.
7395             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7396         (gnus-summary-update-mark
7397          (if (or (null gnus-summary-default-score)
7398                  (<= (abs (- score gnus-summary-default-score))
7399                      gnus-summary-zcore-fuzz)) ? 
7400            (if (< score gnus-summary-default-score)
7401                gnus-score-below-mark gnus-score-over-mark)) 'score))
7402       ;; Do visual highlighting.
7403       (when (gnus-visual-p 'summary-highlight 'highlight)
7404         (run-hooks 'gnus-summary-update-hook)))))
7405
7406 (defvar gnus-tmp-new-adopts nil)
7407
7408 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7409   ;; Sum up all elements (and sub-elements) in a list.
7410   (let* ((number
7411           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7412           (cond
7413            ((and (consp thread) (cdr thread))
7414             (apply
7415              '+ 1 (mapcar
7416                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7417            ((null thread)
7418             1)
7419            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7420             1)
7421            (t 1))))
7422     (when (and level (zerop level) gnus-tmp-new-adopts)
7423       (incf number
7424             (apply '+ (mapcar
7425                        'gnus-summary-number-of-articles-in-thread
7426                        gnus-tmp-new-adopts))))
7427     (if char
7428         (if (> number 1) gnus-not-empty-thread-mark
7429           gnus-empty-thread-mark)
7430       number)))
7431
7432 (defun gnus-summary-set-local-parameters (group)
7433  "Go through the local params of GROUP and set all variable specs in that list."
7434   (let ((params (gnus-info-params (gnus-get-info group)))
7435         elem)
7436     (while params
7437       (setq elem (car params)
7438             params (cdr params))
7439       (and (consp elem)                 ; Has to be a cons.
7440            (consp (cdr elem))           ; The cdr has to be a list.
7441            (symbolp (car elem))         ; Has to be a symbol in there.
7442            (not (memq (car elem) 
7443                       '(quit-config to-address to-list to-group)))
7444            (progn                       ; So we set it.
7445              (make-local-variable (car elem))
7446              (set (car elem) (eval (nth 1 elem))))))))
7447
7448 (defun gnus-summary-read-group (group &optional show-all no-article
7449                                       kill-buffer no-display)
7450   "Start reading news in newsgroup GROUP.
7451 If SHOW-ALL is non-nil, already read articles are also listed.
7452 If NO-ARTICLE is non-nil, no article is selected initially.
7453 If NO-DISPLAY, don't generate a summary buffer."
7454   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7455   (let* ((new-group (gnus-summary-setup-buffer group))
7456          (quit-config (gnus-group-quit-config group))
7457          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7458     (cond
7459      ;; This summary buffer exists already, so we just select it.
7460      ((not new-group)
7461       (gnus-set-global-variables)
7462       (when kill-buffer
7463         (gnus-kill-or-deaden-summary kill-buffer))
7464       (gnus-configure-windows 'summary 'force)
7465       (gnus-set-mode-line 'summary)
7466       (gnus-summary-position-point)
7467       (message "")
7468       t)
7469      ;; We couldn't select this group.
7470      ((null did-select)
7471       (when (and (eq major-mode 'gnus-summary-mode)
7472                  (not (equal (current-buffer) kill-buffer)))
7473         (kill-buffer (current-buffer))
7474         (if (not quit-config)
7475             (progn
7476               (set-buffer gnus-group-buffer)
7477               (gnus-group-jump-to-group group)
7478               (gnus-group-next-unread-group 1))
7479           (if (not (buffer-name (car quit-config)))
7480               (gnus-configure-windows 'group 'force)
7481             (set-buffer (car quit-config))
7482             (and (eq major-mode 'gnus-summary-mode)
7483                  (gnus-set-global-variables))
7484             (gnus-configure-windows (cdr quit-config)))))
7485       (gnus-message 3 "Can't select group")
7486       nil)
7487      ;; The user did a `C-g' while prompting for number of articles,
7488      ;; so we exit this group.
7489      ((eq did-select 'quit)
7490       (and (eq major-mode 'gnus-summary-mode)
7491            (not (equal (current-buffer) kill-buffer))
7492            (kill-buffer (current-buffer)))
7493       (when kill-buffer
7494         (gnus-kill-or-deaden-summary kill-buffer))
7495       (if (not quit-config)
7496           (progn
7497             (set-buffer gnus-group-buffer)
7498             (gnus-group-jump-to-group group)
7499             (gnus-group-next-unread-group 1)
7500             (gnus-configure-windows 'group 'force))
7501         (if (not (buffer-name (car quit-config)))
7502             (gnus-configure-windows 'group 'force)
7503           (set-buffer (car quit-config))
7504           (and (eq major-mode 'gnus-summary-mode)
7505                (gnus-set-global-variables))
7506           (gnus-configure-windows (cdr quit-config))))
7507       ;; Finally signal the quit.
7508       (signal 'quit nil))
7509      ;; The group was successfully selected.
7510      (t
7511       (gnus-set-global-variables)
7512       ;; Save the active value in effect when the group was entered.
7513       (setq gnus-newsgroup-active
7514             (gnus-copy-sequence
7515              (gnus-active gnus-newsgroup-name)))
7516       ;; You can change the summary buffer in some way with this hook.
7517       (run-hooks 'gnus-select-group-hook)
7518       ;; Set any local variables in the group parameters.
7519       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7520       (gnus-update-format-specifications)
7521       ;; Do score processing.
7522       (when gnus-use-scoring
7523         (gnus-possibly-score-headers))
7524       ;; Check whether to fill in the gaps in the threads.
7525       (when gnus-build-sparse-threads
7526         (gnus-build-sparse-threads))
7527       ;; Find the initial limit.
7528       (if show-all
7529           (let ((gnus-newsgroup-dormant nil))
7530             (gnus-summary-initial-limit show-all))
7531         (gnus-summary-initial-limit show-all))
7532       ;; Generate the summary buffer.
7533       (unless no-display
7534         (gnus-summary-prepare))
7535       (when gnus-use-trees
7536         (gnus-tree-open group)
7537         (setq gnus-summary-highlight-line-function
7538               'gnus-tree-highlight-article))
7539       ;; If the summary buffer is empty, but there are some low-scored
7540       ;; articles or some excluded dormants, we include these in the
7541       ;; buffer.
7542       (when (and (zerop (buffer-size))
7543                  (not no-display))
7544         (cond (gnus-newsgroup-dormant
7545                (gnus-summary-limit-include-dormant))
7546               ((and gnus-newsgroup-scored show-all)
7547                (gnus-summary-limit-include-expunged))))
7548       ;; Function `gnus-apply-kill-file' must be called in this hook.
7549       (run-hooks 'gnus-apply-kill-hook)
7550       (if (and (zerop (buffer-size))
7551                (not no-display))
7552           (progn
7553             ;; This newsgroup is empty.
7554             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7555             (gnus-message 6 "No unread news")
7556             (when kill-buffer
7557               (gnus-kill-or-deaden-summary kill-buffer))
7558             ;; Return nil from this function.
7559             nil)
7560         ;; Hide conversation thread subtrees.  We cannot do this in
7561         ;; gnus-summary-prepare-hook since kill processing may not
7562         ;; work with hidden articles.
7563         (and gnus-show-threads
7564              gnus-thread-hide-subtree
7565              (gnus-summary-hide-all-threads))
7566         ;; Show first unread article if requested.
7567         (if (and (not no-article)
7568                  (not no-display)
7569                  gnus-newsgroup-unreads
7570                  gnus-auto-select-first)
7571             (if (eq gnus-auto-select-first 'best)
7572                 (gnus-summary-best-unread-article)
7573               (gnus-summary-first-unread-article))
7574           ;; Don't select any articles, just move point to the first
7575           ;; article in the group.
7576           (goto-char (point-min))
7577           (gnus-summary-position-point)
7578           (gnus-set-mode-line 'summary)
7579           (gnus-configure-windows 'summary 'force))
7580         ;; If we are in async mode, we send some info to the backend.
7581         (when gnus-newsgroup-async
7582           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7583         (when kill-buffer
7584           (gnus-kill-or-deaden-summary kill-buffer))
7585         (when (get-buffer-window gnus-group-buffer t)
7586           ;; Gotta use windows, because recenter does wierd stuff if
7587           ;; the current buffer ain't the displayed window.
7588           (let ((owin (selected-window)))
7589             (select-window (get-buffer-window gnus-group-buffer t))
7590             (when (gnus-group-goto-group group)
7591               (recenter))
7592             (select-window owin))))
7593       ;; Mark this buffer as "prepared".
7594       (setq gnus-newsgroup-prepared t)
7595       t))))
7596
7597 (defun gnus-summary-prepare ()
7598   "Generate the summary buffer."
7599   (let ((buffer-read-only nil))
7600     (erase-buffer)
7601     (setq gnus-newsgroup-data nil
7602           gnus-newsgroup-data-reverse nil)
7603     (run-hooks 'gnus-summary-generate-hook)
7604     ;; Generate the buffer, either with threads or without.
7605     (when gnus-newsgroup-headers
7606       (gnus-summary-prepare-threads
7607        (if gnus-show-threads
7608            (gnus-sort-gathered-threads
7609             (funcall gnus-summary-thread-gathering-function
7610                      (gnus-sort-threads
7611                       (gnus-cut-threads (gnus-make-threads)))))
7612          ;; Unthreaded display.
7613          (gnus-sort-articles gnus-newsgroup-headers))))
7614     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7615     ;; Call hooks for modifying summary buffer.
7616     (goto-char (point-min))
7617     (run-hooks 'gnus-summary-prepare-hook)))
7618
7619 (defun gnus-gather-threads-by-subject (threads)
7620   "Gather threads by looking at Subject headers."
7621   (if (not gnus-summary-make-false-root)
7622       threads
7623     (let ((hashtb (gnus-make-hashtable 1023))
7624           (prev threads)
7625           (result threads)
7626           subject hthread whole-subject)
7627       (while threads
7628         (setq whole-subject (mail-header-subject (caar threads)))
7629         (if (and gnus-summary-gather-exclude-subject
7630                  (string-match gnus-summary-gather-exclude-subject
7631                                whole-subject))
7632             () ; We don't want to do anything with this article.
7633           ;; We simplify the subject before looking it up in the
7634           ;; hash table.
7635           (setq subject
7636                 (cond
7637                  ;; Truncate the subject.
7638                  ((numberp gnus-summary-gather-subject-limit)
7639                   (setq subject (gnus-simplify-subject-re whole-subject))
7640                   (if (> (length subject) gnus-summary-gather-subject-limit)
7641                       (substring subject 0 gnus-summary-gather-subject-limit)
7642                     subject))
7643                  ;; Fuzzily simplify it.
7644                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7645                   (gnus-simplify-subject-fuzzy whole-subject))
7646                  ;; Just remove the leading "Re:".
7647                  (t
7648                   (gnus-simplify-subject-re whole-subject))))
7649
7650           (if (setq hthread (gnus-gethash subject hashtb))
7651               (progn
7652                 ;; We enter a dummy root into the thread, if we
7653                 ;; haven't done that already.
7654                 (unless (stringp (caar hthread))
7655                   (setcar hthread (list whole-subject (car hthread))))
7656                 ;; We add this new gathered thread to this gathered
7657                 ;; thread.
7658                 (setcdr (car hthread)
7659                         (nconc (cdar hthread) (list (car threads))))
7660                 ;; Remove it from the list of threads.
7661                 (setcdr prev (cdr threads))
7662                 (setq threads prev))
7663             ;; Enter this thread into the hash table.
7664             (gnus-sethash subject threads hashtb)))
7665         (setq prev threads)
7666         (setq threads (cdr threads)))
7667       result)))
7668
7669 (defun gnus-gather-threads-by-references (threads)
7670   "Gather threads by looking at References headers."
7671   (let ((idhashtb (gnus-make-hashtable 1023))
7672         (thhashtb (gnus-make-hashtable 1023))
7673         (prev threads)
7674         (result threads)
7675         ids references id gthread gid entered)
7676     (while threads
7677       (when (setq references (mail-header-references (caar threads)))
7678         (setq id (mail-header-id (caar threads)))
7679         (setq ids (gnus-split-references references))
7680         (setq entered nil)
7681         (while ids
7682           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7683               (progn
7684                 (gnus-sethash (car ids) id idhashtb)
7685                 (gnus-sethash id threads thhashtb))
7686             (setq gthread (gnus-gethash gid thhashtb))
7687             (unless entered
7688               ;; We enter a dummy root into the thread, if we
7689               ;; haven't done that already.
7690               (unless (stringp (caar gthread))
7691                 (setcar gthread (list (mail-header-subject (caar gthread))
7692                                       (car gthread))))
7693               ;; We add this new gathered thread to this gathered
7694               ;; thread.
7695               (setcdr (car gthread)
7696                       (nconc (cdar gthread) (list (car threads)))))
7697             ;; Add it into the thread hash table.
7698             (gnus-sethash id gthread thhashtb)
7699             (setq entered t)
7700             ;; Remove it from the list of threads.
7701             (setcdr prev (cdr threads))
7702             (setq threads prev))
7703           (setq ids (cdr ids))))
7704       (setq prev threads)
7705       (setq threads (cdr threads)))
7706     result))
7707
7708 (defun gnus-sort-gathered-threads (threads)
7709   "Sort subtreads inside each gathered thread by article number."
7710   (let ((result threads))
7711     (while threads
7712       (when (stringp (caar threads))
7713         (setcdr (car threads)
7714                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7715       (setq threads (cdr threads)))
7716     result))
7717
7718 (defun gnus-make-threads ()
7719   "Go through the dependency hashtb and find the roots.  Return all threads."
7720   (let (threads)
7721     (mapatoms
7722      (lambda (refs)
7723        (unless (car (symbol-value refs))
7724          ;; These threads do not refer back to any other articles,
7725          ;; so they're roots.
7726          (setq threads (append (cdr (symbol-value refs)) threads))))
7727      gnus-newsgroup-dependencies)
7728     threads))
7729
7730 (defun gnus-build-sparse-threads ()
7731   (let ((headers gnus-newsgroup-headers)
7732         (deps gnus-newsgroup-dependencies)
7733         header references generation relations 
7734         cthread subject child end pthread relation)
7735     ;; First we create an alist of generations/relations, where 
7736     ;; generations is how much we trust the ralation, and the relation
7737     ;; is parent/child.
7738     (gnus-message 7 "Making sparse threads...")
7739     (save-excursion
7740       (nnheader-set-temp-buffer " *gnus sparse threads*")
7741       (while (setq header (pop headers))
7742         (when (and (setq references (mail-header-references header))
7743                    (not (string= references "")))
7744           (insert references)
7745           (setq child (mail-header-id header)
7746                 subject (mail-header-subject header))
7747           (setq generation 0)
7748           (while (search-backward ">" nil t)
7749             (setq end (1+ (point)))
7750             (when (search-backward "<" nil t)
7751               (push (list (incf generation) 
7752                           child (setq child (buffer-substring (point) end))
7753                           subject)
7754                     relations)))
7755           (push (list (1+ generation) child nil subject) relations)
7756           (erase-buffer)))
7757       (kill-buffer (current-buffer)))
7758     ;; Sort over trustworthiness.
7759     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7760     (while (setq relation (pop relations))
7761       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7762                 (unless (car (symbol-value cthread))
7763                   ;; Make this article the parent of these threads.
7764                   (setcar (symbol-value cthread)
7765                           (vector gnus-reffed-article-number 
7766                                   (cadddr relation) 
7767                                   "" ""
7768                                   (cadr relation) 
7769                                   (or (caddr relation) "") 0 0 "")))
7770               (set cthread (list (vector gnus-reffed-article-number
7771                                          (cadddr relation) 
7772                                          "" "" (cadr relation) 
7773                                          (or (caddr relation) "") 0 0 ""))))
7774         (push gnus-reffed-article-number gnus-newsgroup-limit)
7775         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7776         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7777               gnus-newsgroup-reads)
7778         (decf gnus-reffed-article-number)
7779         ;; Make this new thread the child of its parent.
7780         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7781             (setcdr (symbol-value pthread)
7782                     (nconc (cdr (symbol-value pthread))
7783                            (list (symbol-value cthread))))
7784           (set pthread (list nil (symbol-value cthread))))))
7785     (gnus-message 7 "Making sparse threads...done")))
7786
7787 (defun gnus-build-old-threads ()
7788   ;; Look at all the articles that refer back to old articles, and
7789   ;; fetch the headers for the articles that aren't there.  This will
7790   ;; build complete threads - if the roots haven't been expired by the
7791   ;; server, that is.
7792   (let (id heads)
7793     (mapatoms
7794      (lambda (refs)
7795        (when (not (car (symbol-value refs)))
7796          (setq heads (cdr (symbol-value refs)))
7797          (while heads
7798            (if (memq (mail-header-number (caar heads))
7799                      gnus-newsgroup-dormant)
7800                (setq heads (cdr heads))
7801              (setq id (symbol-name refs))
7802              (while (and (setq id (gnus-build-get-header id))
7803                          (not (car (gnus-gethash
7804                                     id gnus-newsgroup-dependencies)))))
7805              (setq heads nil)))))
7806      gnus-newsgroup-dependencies)))
7807
7808 (defun gnus-build-get-header (id)
7809   ;; Look through the buffer of NOV lines and find the header to
7810   ;; ID.  Enter this line into the dependencies hash table, and return
7811   ;; the id of the parent article (if any).
7812   (let ((deps gnus-newsgroup-dependencies)
7813         found header)
7814     (prog1
7815         (save-excursion
7816           (set-buffer nntp-server-buffer)
7817           (goto-char (point-min))
7818           (while (and (not found) (search-forward id nil t))
7819             (beginning-of-line)
7820             (setq found (looking-at
7821                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7822                                  (regexp-quote id))))
7823             (or found (beginning-of-line 2)))
7824           (when found
7825             (beginning-of-line)
7826             (and
7827              (setq header (gnus-nov-parse-line
7828                            (read (current-buffer)) deps))
7829              (gnus-parent-id (mail-header-references header)))))
7830       (when header
7831         (let ((number (mail-header-number header)))
7832           (push number gnus-newsgroup-limit)
7833           (push header gnus-newsgroup-headers)
7834           (if (memq number gnus-newsgroup-unselected)
7835               (progn
7836                 (push number gnus-newsgroup-unreads)
7837                 (setq gnus-newsgroup-unselected
7838                       (delq number gnus-newsgroup-unselected)))
7839             (push number gnus-newsgroup-ancient)))))))
7840
7841 (defun gnus-summary-update-article (article &optional header)
7842   "Update ARTICLE in the summary buffer."
7843   (let ((id (mail-header-id (gnus-summary-article-header article)))
7844         (data (gnus-data-find article)))
7845     (setcar (gnus-id-to-thread id) nil)
7846     (gnus-summary-insert-subject id)
7847     ;; Set the (possibly) new article number in the data structure.
7848     (gnus-data-set-number data (gnus-id-to-article id))))
7849
7850 (defun gnus-rebuild-thread (id)
7851   "Rebuild the thread containing ID."
7852   (let ((buffer-read-only nil)
7853         current thread data)
7854     (if (not gnus-show-threads)
7855         (setq thread (list (car (gnus-id-to-thread id))))
7856       ;; Get the thread this article is part of.
7857       (setq thread (gnus-remove-thread id)))
7858     (setq current (save-excursion
7859                     (and (zerop (forward-line -1))
7860                          (gnus-summary-article-number))))
7861     ;; If this is a gathered thread, we have to go some re-gathering.
7862     (when (stringp (car thread))
7863       (let ((subject (car thread))
7864             roots thr)
7865         (setq thread (cdr thread))
7866         (while thread
7867           (unless (memq (setq thr (gnus-id-to-thread
7868                                       (gnus-root-id
7869                                        (mail-header-id (caar thread)))))
7870                         roots)
7871             (push thr roots))
7872           (setq thread (cdr thread)))
7873         ;; We now have all (unique) roots.
7874         (if (= (length roots) 1)
7875             ;; All the loose roots are now one solid root.
7876             (setq thread (car roots))
7877           (setq thread (cons subject (gnus-sort-threads roots))))))
7878     (let (threads)
7879       ;; We then insert this thread into the summary buffer.
7880       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7881         (gnus-summary-prepare-threads (list thread))
7882         (setq data (nreverse gnus-newsgroup-data))
7883         (setq threads gnus-newsgroup-threads))
7884       ;; We splice the new data into the data structure.
7885       (gnus-data-enter-list current data)
7886       (gnus-data-compute-positions)
7887       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7888
7889 (defun gnus-id-to-thread (id)
7890   "Return the (sub-)thread where ID appears."
7891   (gnus-gethash id gnus-newsgroup-dependencies))
7892
7893 (defun gnus-id-to-article (id)
7894   "Return the article number of ID."
7895   (let ((thread (gnus-id-to-thread id)))
7896     (when thread
7897       (mail-header-number (car thread)))))
7898
7899 (defun gnus-id-to-header (id)
7900   "Return the article headers of ID."
7901   (car (gnus-id-to-thread id)))
7902
7903 (defun gnus-article-displayed-root-p (article)
7904   "Say whether ARTICLE is a root(ish) article."
7905   (let ((level (gnus-summary-thread-level article))
7906         particle)
7907     (cond 
7908      ((null level) nil)
7909      ((zerop level) t)
7910      ((and (= 1 level)
7911            (null (setq particle (gnus-id-to-article
7912                                  (gnus-parent-id 
7913                                   (mail-header-references 
7914                                    (gnus-summary-article-header article))))))
7915            (null (gnus-summary-thread-level particle)))))))
7916
7917 (defun gnus-root-id (id)
7918   "Return the id of the root of the thread where ID appears."
7919   (let (last-id prev)
7920     (while (and id (setq prev (car (gnus-gethash 
7921                                     id gnus-newsgroup-dependencies))))
7922       (setq last-id id
7923             id (gnus-parent-id (mail-header-references prev))))
7924     last-id))
7925
7926 (defun gnus-remove-thread (id &optional dont-remove)
7927   "Remove the thread that has ID in it."
7928   (let ((dep gnus-newsgroup-dependencies)
7929         headers thread last-id)
7930     ;; First go up in this thread until we find the root.
7931     (setq last-id (gnus-root-id id))
7932     (setq headers (list (car (gnus-id-to-thread last-id))
7933                         (caadr (gnus-id-to-thread last-id))))
7934     ;; We have now found the real root of this thread.  It might have
7935     ;; been gathered into some loose thread, so we have to search
7936     ;; through the threads to find the thread we wanted.
7937     (let ((threads gnus-newsgroup-threads)
7938           sub)
7939       (while threads
7940         (setq sub (car threads))
7941         (if (stringp (car sub))
7942             ;; This is a gathered threads, so we look at the roots
7943             ;; below it to find whether this article in in this
7944             ;; gathered root.
7945             (progn
7946               (setq sub (cdr sub))
7947               (while sub
7948                 (when (member (caar sub) headers)
7949                   (setq thread (car threads)
7950                         threads nil
7951                         sub nil))
7952                 (setq sub (cdr sub))))
7953           ;; It's an ordinary thread, so we check it.
7954           (when (eq (car sub) (car headers))
7955             (setq thread sub
7956                   threads nil)))
7957         (setq threads (cdr threads)))
7958       ;; If this article is in no thread, then it's a root.
7959       (if thread
7960           (unless dont-remove
7961             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7962         (setq thread (gnus-gethash last-id dep)))
7963       (when thread
7964         (prog1
7965             thread ; We return this thread.
7966           (unless dont-remove
7967             (if (stringp (car thread))
7968                 (progn
7969                   ;; If we use dummy roots, then we have to remove the
7970                   ;; dummy root as well.
7971                   (when (eq gnus-summary-make-false-root 'dummy)
7972                     ;; Uhm.
7973                     )
7974                   (setq thread (cdr thread))
7975                   (while thread
7976                     (gnus-remove-thread-1 (car thread))
7977                     (setq thread (cdr thread))))
7978               (gnus-remove-thread-1 thread))))))))
7979
7980 (defun gnus-remove-thread-1 (thread)
7981   "Remove the thread THREAD recursively."
7982   (let ((number (mail-header-number (car thread)))
7983         pos)
7984     (when (setq pos (text-property-any
7985                      (point-min) (point-max) 'gnus-number number))
7986       (goto-char pos)
7987       (gnus-delete-line)
7988       (gnus-data-remove number))
7989     (setq thread (cdr thread))
7990     (while thread
7991       (gnus-remove-thread-1 (car thread))
7992       (setq thread (cdr thread)))))
7993
7994 (defun gnus-sort-threads (threads)
7995   "Sort THREADS."
7996   (if (not gnus-thread-sort-functions)
7997       threads
7998     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7999                     (car gnus-thread-sort-functions)
8000                   `(lambda (t1 t2)
8001                      ,(gnus-make-sort-function 
8002                        (reverse gnus-thread-sort-functions))))))
8003       (gnus-message 7 "Sorting threads...")
8004       (prog1
8005           (sort threads func)
8006         (gnus-message 7 "Sorting threads...done")))))
8007
8008 (defun gnus-sort-articles (articles)
8009   "Sort ARTICLES."
8010   (when gnus-article-sort-functions
8011     (let ((func (if (= 1 (length gnus-article-sort-functions))
8012                     (car gnus-article-sort-functions)
8013                   `(lambda (t1 t2)
8014                      ,(gnus-make-sort-function 
8015                        (reverse gnus-article-sort-functions))))))
8016       (gnus-message 7 "Sorting articles...")
8017       (prog1
8018           (setq gnus-newsgroup-headers (sort articles func))
8019         (gnus-message 7 "Sorting articles...done")))))
8020
8021 (defun gnus-make-sort-function (funs)
8022   "Return a composite sort condition based on the functions in FUNC."
8023   (if (cdr funs)
8024       `(or (,(car funs) t1 t2)
8025            (and (not (,(car funs) t2 t1))
8026                 ,(gnus-make-sort-function (cdr funs))))
8027     `(,(car funs) t1 t2)))
8028                  
8029 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8030 (defmacro gnus-thread-header (thread)
8031   ;; Return header of first article in THREAD.
8032   ;; Note that THREAD must never, ever be anything else than a variable -
8033   ;; using some other form will lead to serious barfage.
8034   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8035   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8036   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8037         (vector thread) 2))
8038
8039 (defsubst gnus-article-sort-by-number (h1 h2)
8040   "Sort articles by article number."
8041   (< (mail-header-number h1)
8042      (mail-header-number h2)))
8043
8044 (defun gnus-thread-sort-by-number (h1 h2)
8045   "Sort threads by root article number."
8046   (gnus-article-sort-by-number
8047    (gnus-thread-header h1) (gnus-thread-header h2)))
8048
8049 (defsubst gnus-article-sort-by-author (h1 h2)
8050   "Sort articles by root author."
8051   (string-lessp
8052    (let ((extract (funcall
8053                    gnus-extract-address-components
8054                    (mail-header-from h1))))
8055      (or (car extract) (cdr extract)))
8056    (let ((extract (funcall
8057                    gnus-extract-address-components
8058                    (mail-header-from h2))))
8059      (or (car extract) (cdr extract)))))
8060
8061 (defun gnus-thread-sort-by-author (h1 h2)
8062   "Sort threads by root author."
8063   (gnus-article-sort-by-author
8064    (gnus-thread-header h1)  (gnus-thread-header h2)))
8065
8066 (defsubst gnus-article-sort-by-subject (h1 h2)
8067   "Sort articles by root subject."
8068   (string-lessp
8069    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8070    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8071
8072 (defun gnus-thread-sort-by-subject (h1 h2)
8073   "Sort threads by root subject."
8074   (gnus-article-sort-by-subject
8075    (gnus-thread-header h1) (gnus-thread-header h2)))
8076
8077 (defsubst gnus-article-sort-by-date (h1 h2)
8078   "Sort articles by root article date."
8079   (string-lessp
8080    (gnus-sortable-date (mail-header-date h1))
8081    (gnus-sortable-date (mail-header-date h2))))
8082
8083 (defun gnus-thread-sort-by-date (h1 h2)
8084   "Sort threads by root article date."
8085   (gnus-article-sort-by-date
8086    (gnus-thread-header h1) (gnus-thread-header h2)))
8087
8088 (defsubst gnus-article-sort-by-score (h1 h2)
8089   "Sort articles by root article score.
8090 Unscored articles will be counted as having a score of zero."
8091   (> (or (cdr (assq (mail-header-number h1)
8092                     gnus-newsgroup-scored))
8093          gnus-summary-default-score 0)
8094      (or (cdr (assq (mail-header-number h2)
8095                     gnus-newsgroup-scored))
8096          gnus-summary-default-score 0)))
8097
8098 (defun gnus-thread-sort-by-score (h1 h2)
8099   "Sort threads by root article score."
8100   (gnus-article-sort-by-score
8101    (gnus-thread-header h1) (gnus-thread-header h2)))
8102
8103 (defun gnus-thread-sort-by-total-score (h1 h2)
8104   "Sort threads by the sum of all scores in the thread.
8105 Unscored articles will be counted as having a score of zero."
8106   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8107
8108 (defun gnus-thread-total-score (thread)
8109   ;;  This function find the total score of THREAD.
8110   (if (consp thread)
8111       (if (stringp (car thread))
8112           (apply gnus-thread-score-function 0
8113                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8114         (gnus-thread-total-score-1 thread))
8115     (gnus-thread-total-score-1 (list thread))))
8116
8117 (defun gnus-thread-total-score-1 (root)
8118   ;; This function find the total score of the thread below ROOT.
8119   (setq root (car root))
8120   (apply gnus-thread-score-function
8121          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8122              gnus-summary-default-score 0)
8123          (mapcar 'gnus-thread-total-score
8124                  (cdr (gnus-gethash (mail-header-id root)
8125                                     gnus-newsgroup-dependencies)))))
8126
8127 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8128 (defvar gnus-tmp-prev-subject nil)
8129 (defvar gnus-tmp-false-parent nil)
8130 (defvar gnus-tmp-root-expunged nil)
8131 (defvar gnus-tmp-dummy-line nil)
8132
8133 (defun gnus-summary-prepare-threads (threads)
8134   "Prepare summary buffer from THREADS and indentation LEVEL.
8135 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8136 or a straight list of headers."
8137   (gnus-message 7 "Generating summary...")
8138
8139   (setq gnus-newsgroup-threads threads)
8140   (beginning-of-line)
8141
8142   (let ((gnus-tmp-level 0)
8143         (default-score (or gnus-summary-default-score 0))
8144         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8145         thread number subject stack state gnus-tmp-gathered beg-match
8146         new-roots gnus-tmp-new-adopts thread-end
8147         gnus-tmp-header gnus-tmp-unread
8148         gnus-tmp-replied gnus-tmp-subject-or-nil
8149         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8150         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8151         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8152
8153     (setq gnus-tmp-prev-subject nil)
8154
8155     (if (vectorp (car threads))
8156         ;; If this is a straight (sic) list of headers, then a
8157         ;; threaded summary display isn't required, so we just create
8158         ;; an unthreaded one.
8159         (gnus-summary-prepare-unthreaded threads)
8160
8161       ;; Do the threaded display.
8162
8163       (while (or threads stack gnus-tmp-new-adopts new-roots)
8164
8165         (if (and (= gnus-tmp-level 0)
8166                  (not (setq gnus-tmp-dummy-line nil))
8167                  (or (not stack)
8168                      (= (caar stack) 0))
8169                  (not gnus-tmp-false-parent)
8170                  (or gnus-tmp-new-adopts new-roots))
8171             (if gnus-tmp-new-adopts
8172                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8173                       thread (list (car gnus-tmp-new-adopts))
8174                       gnus-tmp-header (caar thread)
8175                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8176               (if new-roots
8177                   (setq thread (list (car new-roots))
8178                         gnus-tmp-header (caar thread)
8179                         new-roots (cdr new-roots))))
8180
8181           (if threads
8182               ;; If there are some threads, we do them before the
8183               ;; threads on the stack.
8184               (setq thread threads
8185                     gnus-tmp-header (caar thread))
8186             ;; There were no current threads, so we pop something off
8187             ;; the stack.
8188             (setq state (car stack)
8189                   gnus-tmp-level (car state)
8190                   thread (cdr state)
8191                   stack (cdr stack)
8192                   gnus-tmp-header (caar thread))))
8193
8194         (setq gnus-tmp-false-parent nil)
8195         (setq gnus-tmp-root-expunged nil)
8196         (setq thread-end nil)
8197
8198         (if (stringp gnus-tmp-header)
8199             ;; The header is a dummy root.
8200             (cond
8201              ((eq gnus-summary-make-false-root 'adopt)
8202               ;; We let the first article adopt the rest.
8203               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8204                                                (cddar thread)))
8205               (setq gnus-tmp-gathered
8206                     (nconc (mapcar
8207                             (lambda (h) (mail-header-number (car h)))
8208                             (cddar thread))
8209                            gnus-tmp-gathered))
8210               (setq thread (cons (list (caar thread)
8211                                        (cadar thread))
8212                                  (cdr thread)))
8213               (setq gnus-tmp-level -1
8214                     gnus-tmp-false-parent t))
8215              ((eq gnus-summary-make-false-root 'empty)
8216               ;; We print adopted articles with empty subject fields.
8217               (setq gnus-tmp-gathered
8218                     (nconc (mapcar
8219                             (lambda (h) (mail-header-number (car h)))
8220                             (cddar thread))
8221                            gnus-tmp-gathered))
8222               (setq gnus-tmp-level -1))
8223              ((eq gnus-summary-make-false-root 'dummy)
8224               ;; We remember that we probably want to output a dummy
8225               ;; root.
8226               (setq gnus-tmp-dummy-line gnus-tmp-header)
8227               (setq gnus-tmp-prev-subject gnus-tmp-header))
8228              (t
8229               ;; We do not make a root for the gathered
8230               ;; sub-threads at all.
8231               (setq gnus-tmp-level -1)))
8232
8233           (setq number (mail-header-number gnus-tmp-header)
8234                 subject (mail-header-subject gnus-tmp-header))
8235
8236           (cond
8237            ;; If the thread has changed subject, we might want to make
8238            ;; this subthread into a root.
8239            ((and (null gnus-thread-ignore-subject)
8240                  (not (zerop gnus-tmp-level))
8241                  gnus-tmp-prev-subject
8242                  (not (inline
8243                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8244             (setq new-roots (nconc new-roots (list (car thread)))
8245                   thread-end t
8246                   gnus-tmp-header nil))
8247            ;; If the article lies outside the current limit,
8248            ;; then we do not display it.
8249            ((and (not (memq number gnus-newsgroup-limit))
8250                  (not gnus-tmp-dummy-line))
8251             (setq gnus-tmp-gathered
8252                   (nconc (mapcar
8253                           (lambda (h) (mail-header-number (car h)))
8254                           (cdar thread))
8255                          gnus-tmp-gathered))
8256             (setq gnus-tmp-new-adopts (if (cdar thread)
8257                                           (append gnus-tmp-new-adopts
8258                                                   (cdar thread))
8259                                         gnus-tmp-new-adopts)
8260                   thread-end t
8261                   gnus-tmp-header nil)
8262             (when (zerop gnus-tmp-level)
8263               (setq gnus-tmp-root-expunged t)))
8264            ;; Perhaps this article is to be marked as read?
8265            ((and gnus-summary-mark-below
8266                  (< (or (cdr (assq number gnus-newsgroup-scored))
8267                         default-score)
8268                     gnus-summary-mark-below)
8269                  ;; Don't touch sparse articles.
8270                  (not (memq number gnus-newsgroup-sparse)))
8271             (setq gnus-newsgroup-unreads
8272                   (delq number gnus-newsgroup-unreads))
8273             (if gnus-newsgroup-auto-expire
8274                 (push number gnus-newsgroup-expirable)
8275               (push (cons number gnus-low-score-mark)
8276                     gnus-newsgroup-reads))))
8277
8278           (when gnus-tmp-header
8279             ;; We may have an old dummy line to output before this
8280             ;; article.
8281             (when gnus-tmp-dummy-line
8282               (gnus-summary-insert-dummy-line
8283                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8284               (setq gnus-tmp-dummy-line nil))
8285
8286             ;; Compute the mark.
8287             (setq
8288              gnus-tmp-unread
8289              (cond
8290               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8291               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8292               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8293               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8294               (t (or (cdr (assq number gnus-newsgroup-reads))
8295                      gnus-ancient-mark))))
8296
8297             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8298                                   gnus-tmp-header gnus-tmp-level)
8299                   gnus-newsgroup-data)
8300
8301             ;; Actually insert the line.
8302             (setq
8303              gnus-tmp-subject-or-nil
8304              (cond
8305               ((and gnus-thread-ignore-subject
8306                     gnus-tmp-prev-subject
8307                     (not (inline (gnus-subject-equal
8308                                   gnus-tmp-prev-subject subject))))
8309                subject)
8310               ((zerop gnus-tmp-level)
8311                (if (and (eq gnus-summary-make-false-root 'empty)
8312                         (memq number gnus-tmp-gathered)
8313                         gnus-tmp-prev-subject
8314                         (inline (gnus-subject-equal
8315                                  gnus-tmp-prev-subject subject)))
8316                    gnus-summary-same-subject
8317                  subject))
8318               (t gnus-summary-same-subject)))
8319             (if (and (eq gnus-summary-make-false-root 'adopt)
8320                      (= gnus-tmp-level 1)
8321                      (memq number gnus-tmp-gathered))
8322                 (setq gnus-tmp-opening-bracket ?\<
8323                       gnus-tmp-closing-bracket ?\>)
8324               (setq gnus-tmp-opening-bracket ?\[
8325                     gnus-tmp-closing-bracket ?\]))
8326             (setq
8327              gnus-tmp-indentation
8328              (aref gnus-thread-indent-array gnus-tmp-level)
8329              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8330              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8331                                 gnus-summary-default-score 0)
8332              gnus-tmp-score-char
8333              (if (or (null gnus-summary-default-score)
8334                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8335                          gnus-summary-zcore-fuzz)) ? 
8336                (if (< gnus-tmp-score gnus-summary-default-score)
8337                    gnus-score-below-mark gnus-score-over-mark))
8338              gnus-tmp-replied
8339              (cond ((memq number gnus-newsgroup-processable)
8340                     gnus-process-mark)
8341                    ((memq number gnus-newsgroup-cached)
8342                     gnus-cached-mark)
8343                    ((memq number gnus-newsgroup-replied)
8344                     gnus-replied-mark)
8345                    (t gnus-unread-mark))
8346              gnus-tmp-from (mail-header-from gnus-tmp-header)
8347              gnus-tmp-name
8348              (cond
8349               ((string-match "(.+)" gnus-tmp-from)
8350                (substring gnus-tmp-from
8351                           (1+ (match-beginning 0)) (1- (match-end 0))))
8352               ((string-match "<[^>]+> *$" gnus-tmp-from)
8353                (setq beg-match (match-beginning 0))
8354                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8355                         (substring gnus-tmp-from (1+ (match-beginning 0))
8356                                    (1- (match-end 0))))
8357                    (substring gnus-tmp-from 0 beg-match)))
8358               (t gnus-tmp-from)))
8359             (when (string= gnus-tmp-name "")
8360               (setq gnus-tmp-name gnus-tmp-from))
8361             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8362             (put-text-property
8363              (point)
8364              (progn (eval gnus-summary-line-format-spec) (point))
8365              'gnus-number number)
8366             (when gnus-visual-p
8367               (forward-line -1)
8368               (run-hooks 'gnus-summary-update-hook)
8369               (forward-line 1))
8370
8371             (setq gnus-tmp-prev-subject subject)))
8372
8373         (when (nth 1 thread)
8374           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8375         (incf gnus-tmp-level)
8376         (setq threads (if thread-end nil (cdar thread)))
8377         (unless threads
8378           (setq gnus-tmp-level 0)))))
8379   (gnus-message 7 "Generating summary...done"))
8380
8381 (defun gnus-summary-prepare-unthreaded (headers)
8382   "Generate an unthreaded summary buffer based on HEADERS."
8383   (let (header number mark)
8384
8385     (while headers
8386       (setq header (car headers)
8387             headers (cdr headers)
8388             number (mail-header-number header))
8389
8390       ;; We may have to root out some bad articles...
8391       (when (memq number gnus-newsgroup-limit)
8392         (when (and gnus-summary-mark-below
8393                    (< (or (cdr (assq number gnus-newsgroup-scored))
8394                           gnus-summary-default-score 0)
8395                       gnus-summary-mark-below))
8396           (setq gnus-newsgroup-unreads
8397                 (delq number gnus-newsgroup-unreads))
8398           (if gnus-newsgroup-auto-expire
8399               (push number gnus-newsgroup-expirable)
8400             (push (cons number gnus-low-score-mark)
8401                   gnus-newsgroup-reads)))
8402
8403         (setq mark
8404               (cond
8405                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8406                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8407                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8408                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8409                (t (or (cdr (assq number gnus-newsgroup-reads))
8410                       gnus-ancient-mark))))
8411         (setq gnus-newsgroup-data
8412               (cons (gnus-data-make number mark (1+ (point)) header 0)
8413                     gnus-newsgroup-data))
8414         (gnus-summary-insert-line
8415          header 0 nil mark (memq number gnus-newsgroup-replied)
8416          (memq number gnus-newsgroup-expirable)
8417          (mail-header-subject header) nil
8418          (cdr (assq number gnus-newsgroup-scored))
8419          (memq number gnus-newsgroup-processable))))))
8420
8421 (defun gnus-select-newsgroup (group &optional read-all)
8422   "Select newsgroup GROUP.
8423 If READ-ALL is non-nil, all articles in the group are selected."
8424   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8425          (info (nth 2 entry))
8426          articles fetched-articles cached)
8427
8428     (or (gnus-check-server
8429          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8430         (error "Couldn't open server"))
8431
8432     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8433         (gnus-activate-group group) ; Or we can activate it...
8434         (progn ; Or we bug out.
8435           (when (equal major-mode 'gnus-summary-mode)
8436             (kill-buffer (current-buffer)))
8437           (error "Couldn't request group %s: %s"
8438                  group (gnus-status-message group))))
8439
8440     (setq gnus-newsgroup-name group)
8441     (setq gnus-newsgroup-unselected nil)
8442     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8443
8444     (and gnus-asynchronous
8445          (gnus-check-backend-function
8446           'request-asynchronous gnus-newsgroup-name)
8447          (setq gnus-newsgroup-async
8448                (gnus-request-asynchronous gnus-newsgroup-name)))
8449
8450     ;; Adjust and set lists of article marks.
8451     (when info
8452       (gnus-adjust-marked-articles info))
8453
8454     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8455     (when (gnus-virtual-group-p group)
8456       (setq cached gnus-newsgroup-cached))
8457
8458     (setq gnus-newsgroup-unreads
8459           (gnus-set-difference
8460            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8461            gnus-newsgroup-dormant))
8462
8463     (setq gnus-newsgroup-processable nil)
8464
8465     (setq articles (gnus-articles-to-read group read-all))
8466
8467     (cond
8468      ((null articles)
8469       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8470       'quit)
8471      ((eq articles 0) nil)
8472      (t
8473       ;; Init the dependencies hash table.
8474       (setq gnus-newsgroup-dependencies
8475             (gnus-make-hashtable (length articles)))
8476       ;; Retrieve the headers and read them in.
8477       (gnus-message 5 "Fetching headers...")
8478       (setq gnus-newsgroup-headers
8479             (if (eq 'nov
8480                     (setq gnus-headers-retrieved-by
8481                           (gnus-retrieve-headers
8482                            articles gnus-newsgroup-name
8483                            ;; We might want to fetch old headers, but
8484                            ;; not if there is only 1 article.
8485                            (and gnus-fetch-old-headers
8486                                 (or (and
8487                                      (not (eq gnus-fetch-old-headers 'some))
8488                                      (not (numberp gnus-fetch-old-headers)))
8489                                     (> (length articles) 1))))))
8490                 (gnus-get-newsgroup-headers-xover articles)
8491               (gnus-get-newsgroup-headers)))
8492       (gnus-message 5 "Fetching headers...done")
8493
8494       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8495       (when cached
8496         (setq gnus-newsgroup-cached cached))
8497
8498       ;; Set the initial limit.
8499       (setq gnus-newsgroup-limit (copy-sequence articles))
8500       ;; Remove canceled articles from the list of unread articles.
8501       (setq gnus-newsgroup-unreads
8502             (gnus-set-sorted-intersection
8503              gnus-newsgroup-unreads
8504              (setq fetched-articles
8505                    (mapcar (lambda (headers) (mail-header-number headers))
8506                            gnus-newsgroup-headers))))
8507       ;; Removed marked articles that do not exist.
8508       (gnus-update-missing-marks
8509        (gnus-sorted-complement fetched-articles articles))
8510       ;; We might want to build some more threads first.
8511       (and gnus-fetch-old-headers
8512            (eq gnus-headers-retrieved-by 'nov)
8513            (gnus-build-old-threads))
8514       ;; Check whether auto-expire is to be done in this group.
8515       (setq gnus-newsgroup-auto-expire
8516             (gnus-group-auto-expirable-p group))
8517       ;; Set up the article buffer now, if necessary.
8518       (unless gnus-single-article-buffer
8519         (gnus-article-setup-buffer))
8520       ;; First and last article in this newsgroup.
8521       (and gnus-newsgroup-headers
8522            (setq gnus-newsgroup-begin
8523                  (mail-header-number (car gnus-newsgroup-headers)))
8524            (setq gnus-newsgroup-end
8525                  (mail-header-number
8526                   (gnus-last-element gnus-newsgroup-headers))))
8527       (setq gnus-reffed-article-number -1)
8528       ;; GROUP is successfully selected.
8529       (or gnus-newsgroup-headers t)))))
8530
8531 (defun gnus-articles-to-read (group read-all)
8532   ;; Find out what articles the user wants to read.
8533   (let* ((articles
8534           ;; Select all articles if `read-all' is non-nil, or if there
8535           ;; are no unread articles.
8536           (if (or read-all
8537                   (and (zerop (length gnus-newsgroup-marked))
8538                        (zerop (length gnus-newsgroup-unreads))))
8539               (gnus-uncompress-range (gnus-active group))
8540             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8541                           (copy-sequence gnus-newsgroup-unreads))
8542                   '<)))
8543          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8544          (scored (length scored-list))
8545          (number (length articles))
8546          (marked (+ (length gnus-newsgroup-marked)
8547                     (length gnus-newsgroup-dormant)))
8548          (select
8549           (cond
8550            ((numberp read-all)
8551             read-all)
8552            (t
8553             (condition-case ()
8554                 (cond
8555                  ((and (or (<= scored marked) (= scored number))
8556                        (numberp gnus-large-newsgroup)
8557                        (> number gnus-large-newsgroup))
8558                   (let ((input
8559                          (read-string
8560                           (format
8561                            "How many articles from %s (default %d): "
8562                            gnus-newsgroup-name number))))
8563                     (if (string-match "^[ \t]*$" input) number input)))
8564                  ((and (> scored marked) (< scored number))
8565                   (let ((input
8566                          (read-string
8567                           (format "%s %s (%d scored, %d total): "
8568                                   "How many articles from"
8569                                   group scored number))))
8570                     (if (string-match "^[ \t]*$" input)
8571                         number input)))
8572                  (t number))
8573               (quit nil))))))
8574     (setq select (if (stringp select) (string-to-number select) select))
8575     (if (or (null select) (zerop select))
8576         select
8577       (if (and (not (zerop scored)) (<= (abs select) scored))
8578           (progn
8579             (setq articles (sort scored-list '<))
8580             (setq number (length articles)))
8581         (setq articles (copy-sequence articles)))
8582
8583       (if (< (abs select) number)
8584           (if (< select 0)
8585               ;; Select the N oldest articles.
8586               (setcdr (nthcdr (1- (abs select)) articles) nil)
8587             ;; Select the N most recent articles.
8588             (setq articles (nthcdr (- number select) articles))))
8589       (setq gnus-newsgroup-unselected
8590             (gnus-sorted-intersection
8591              gnus-newsgroup-unreads
8592              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8593       articles)))
8594
8595 (defun gnus-killed-articles (killed articles)
8596   (let (out)
8597     (while articles
8598       (if (inline (gnus-member-of-range (car articles) killed))
8599           (setq out (cons (car articles) out)))
8600       (setq articles (cdr articles)))
8601     out))
8602
8603 (defun gnus-uncompress-marks (marks)
8604   "Uncompress the mark ranges in MARKS."
8605   (let ((uncompressed '(score bookmark))
8606         out)
8607     (while marks
8608       (if (memq (caar marks) uncompressed)
8609           (push (car marks) out)
8610         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8611       (setq marks (cdr marks)))
8612     out))
8613
8614 (defun gnus-adjust-marked-articles (info)
8615   "Set all article lists and remove all marks that are no longer legal."
8616   (let* ((marked-lists (gnus-info-marks info))
8617          (active (gnus-active (gnus-info-group info)))
8618          (min (car active))
8619          (max (cdr active))
8620          (types gnus-article-mark-lists)
8621          (uncompressed '(score bookmark))
8622          marks var articles article mark)
8623
8624     (while marked-lists
8625       (setq marks (pop marked-lists))
8626       (set (setq var (intern (format "gnus-newsgroup-%s"
8627                                      (car (rassq (setq mark (car marks))
8628                                                  types)))))
8629            (if (memq (car marks) uncompressed) (cdr marks)
8630              (gnus-uncompress-range (cdr marks))))
8631
8632       (setq articles (symbol-value var))
8633
8634       ;; All articles have to be subsets of the active articles.
8635       (cond
8636        ;; Adjust "simple" lists.
8637        ((memq mark '(tick dormant expirable reply killed save))
8638         (while articles
8639           (when (or (< (setq article (pop articles)) min) (> article max))
8640             (set var (delq article (symbol-value var))))))
8641        ;; Adjust assocs.
8642        ((memq mark '(score bookmark))
8643         (while articles
8644           (when (or (< (car (setq article (pop articles))) min)
8645                     (> (car article) max))
8646             (set var (delq article (symbol-value var))))))))))
8647
8648 (defun gnus-update-missing-marks (missing)
8649   "Go through the list of MISSING articles and remove them mark lists."
8650   (when missing
8651     (let ((types gnus-article-mark-lists)
8652           var m)
8653       ;; Go through all types.
8654       (while types
8655         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8656         (when (symbol-value var)
8657           ;; This list has articles.  So we delete all missing articles
8658           ;; from it.
8659           (setq m missing)
8660           (while m
8661             (set var (delq (pop m) (symbol-value var)))))))))
8662
8663 (defun gnus-update-marks ()
8664   "Enter the various lists of marked articles into the newsgroup info list."
8665   (let ((types gnus-article-mark-lists)
8666         (info (gnus-get-info gnus-newsgroup-name))
8667         (uncompressed '(score bookmark killed))
8668         type list newmarked symbol)
8669     (when info
8670       ;; Add all marks lists that are non-nil to the list of marks lists.
8671       (while types
8672         (setq type (pop types))
8673         (when (setq list (symbol-value
8674                           (setq symbol
8675                                 (intern (format "gnus-newsgroup-%s"
8676                                                 (car type))))))
8677           (push (cons (cdr type)
8678                       (if (memq (cdr type) uncompressed) list
8679                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8680                 newmarked)))
8681
8682       ;; Enter these new marks into the info of the group.
8683       (if (nthcdr 3 info)
8684           (setcar (nthcdr 3 info) newmarked)
8685         ;; Add the marks lists to the end of the info.
8686         (when newmarked
8687           (setcdr (nthcdr 2 info) (list newmarked))))
8688
8689       ;; Cut off the end of the info if there's nothing else there.
8690       (let ((i 5))
8691         (while (and (> i 2)
8692                     (not (nth i info)))
8693           (when (nthcdr (decf i) info)
8694             (setcdr (nthcdr i info) nil)))))))
8695
8696 (defun gnus-add-marked-articles (group type articles &optional info force)
8697   ;; Add ARTICLES of TYPE to the info of GROUP.
8698   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8699   ;; add, but replace marked articles of TYPE with ARTICLES.
8700   (let ((info (or info (gnus-get-info group)))
8701         (uncompressed '(score bookmark killed))
8702         marked m)
8703     (or (not info)
8704         (and (not (setq marked (nthcdr 3 info)))
8705              (or (null articles)
8706                  (setcdr (nthcdr 2 info)
8707                          (list (list (cons type (gnus-compress-sequence
8708                                                  articles t)))))))
8709         (and (not (setq m (assq type (car marked))))
8710              (or (null articles)
8711                  (setcar marked
8712                          (cons (cons type (gnus-compress-sequence articles t) )
8713                                (car marked)))))
8714         (if force
8715             (if (null articles)
8716                 (setcar (nthcdr 3 info)
8717                         (delq (assq type (car marked)) (car marked)))
8718               (setcdr m (gnus-compress-sequence articles t)))
8719           (setcdr m (gnus-compress-sequence
8720                      (sort (nconc (gnus-uncompress-range m)
8721                                   (copy-sequence articles)) '<) t))))))
8722
8723 (defun gnus-set-mode-line (where)
8724   "This function sets the mode line of the article or summary buffers.
8725 If WHERE is `summary', the summary mode line format will be used."
8726   ;; Is this mode line one we keep updated?
8727   (when (memq where gnus-updated-mode-lines)
8728     (let (mode-string)
8729       (save-excursion
8730         ;; We evaluate this in the summary buffer since these
8731         ;; variables are buffer-local to that buffer.
8732         (set-buffer gnus-summary-buffer)
8733         ;; We bind all these variables that are used in the `eval' form
8734         ;; below.
8735         (let* ((mformat (symbol-value
8736                          (intern
8737                           (format "gnus-%s-mode-line-format-spec" where))))
8738                (gnus-tmp-group-name gnus-newsgroup-name)
8739                (gnus-tmp-article-number (or gnus-current-article 0))
8740                (gnus-tmp-unread gnus-newsgroup-unreads)
8741                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8742                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8743                (gnus-tmp-unread-and-unselected
8744                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8745                             (zerop gnus-tmp-unselected)) "")
8746                       ((zerop gnus-tmp-unselected)
8747                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8748                       (t (format "{%d(+%d) more}"
8749                                  gnus-tmp-unread-and-unticked
8750                                  gnus-tmp-unselected))))
8751                (gnus-tmp-subject
8752                 (if (and gnus-current-headers
8753                          (vectorp gnus-current-headers))
8754                     (mail-header-subject gnus-current-headers) ""))
8755                max-len
8756                gnus-tmp-header);; passed as argument to any user-format-funcs
8757           (setq mode-string (eval mformat))
8758           (setq max-len (max 4 (if gnus-mode-non-string-length
8759                                    (- (frame-width)
8760                                       gnus-mode-non-string-length)
8761                                  (length mode-string))))
8762           ;; We might have to chop a bit of the string off...
8763           (when (> (length mode-string) max-len)
8764             (setq mode-string
8765                   (concat (gnus-truncate-string mode-string (- max-len 3))
8766                           "...")))
8767           ;; Pad the mode string a bit.
8768           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8769       ;; Update the mode line.
8770       (setq mode-line-buffer-identification (list mode-string))
8771       (set-buffer-modified-p t))))
8772
8773 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8774   "Go through the HEADERS list and add all Xrefs to a hash table.
8775 The resulting hash table is returned, or nil if no Xrefs were found."
8776   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8777          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8778          (xref-hashtb (make-vector 63 0))
8779          start group entry number xrefs header)
8780     (while headers
8781       (setq header (pop headers))
8782       (when (and (setq xrefs (mail-header-xref header))
8783                  (not (memq (setq number (mail-header-number header))
8784                             unreads)))
8785         (setq start 0)
8786         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8787           (setq start (match-end 0))
8788           (setq group (concat prefix (substring xrefs (match-beginning 1)
8789                                                 (match-end 1))))
8790           (setq number
8791                 (string-to-int (substring xrefs (match-beginning 2)
8792                                           (match-end 2))))
8793           (if (setq entry (gnus-gethash group xref-hashtb))
8794               (setcdr entry (cons number (cdr entry)))
8795             (gnus-sethash group (cons number nil) xref-hashtb)))))
8796     (and start xref-hashtb)))
8797
8798 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8799   "Look through all the headers and mark the Xrefs as read."
8800   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8801         name entry info xref-hashtb idlist method nth4)
8802     (save-excursion
8803       (set-buffer gnus-group-buffer)
8804       (when (setq xref-hashtb
8805                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8806         (mapatoms
8807          (lambda (group)
8808            (unless (string= from-newsgroup (setq name (symbol-name group)))
8809              (setq idlist (symbol-value group))
8810              ;; Dead groups are not updated.
8811              (and (prog1
8812                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8813                             info (nth 2 entry))
8814                     (if (stringp (setq nth4 (gnus-info-method info)))
8815                         (setq nth4 (gnus-server-to-method nth4))))
8816                   ;; Only do the xrefs if the group has the same
8817                   ;; select method as the group we have just read.
8818                   (or (gnus-methods-equal-p
8819                        nth4 (gnus-find-method-for-group from-newsgroup))
8820                       virtual
8821                       (equal nth4 (setq method (gnus-find-method-for-group
8822                                                 from-newsgroup)))
8823                       (and (equal (car nth4) (car method))
8824                            (equal (nth 1 nth4) (nth 1 method))))
8825                   gnus-use-cross-reference
8826                   (or (not (eq gnus-use-cross-reference t))
8827                       virtual
8828                       ;; Only do cross-references on subscribed
8829                       ;; groups, if that is what is wanted.
8830                       (<= (gnus-info-level info) gnus-level-subscribed))
8831                   (gnus-group-make-articles-read name idlist))))
8832          xref-hashtb)))))
8833
8834 (defun gnus-group-make-articles-read (group articles)
8835   (let* ((num 0)
8836          (entry (gnus-gethash group gnus-newsrc-hashtb))
8837          (info (nth 2 entry))
8838          (active (gnus-active group))
8839          range)
8840     ;; First peel off all illegal article numbers.
8841     (if active
8842         (let ((ids articles)
8843               id first)
8844           (while ids
8845             (setq id (car ids))
8846             (if (and first (> id (cdr active)))
8847                 (progn
8848                   ;; We'll end up in this situation in one particular
8849                   ;; obscure situation.  If you re-scan a group and get
8850                   ;; a new article that is cross-posted to a different
8851                   ;; group that has not been re-scanned, you might get
8852                   ;; crossposted article that has a higher number than
8853                   ;; Gnus believes possible.  So we re-activate this
8854                   ;; group as well.  This might mean doing the
8855                   ;; crossposting thingy will *increase* the number
8856                   ;; of articles in some groups.  Tsk, tsk.
8857                   (setq active (or (gnus-activate-group group) active))))
8858             (if (or (> id (cdr active))
8859                     (< id (car active)))
8860                 (setq articles (delq id articles)))
8861             (setq ids (cdr ids)))))
8862     ;; If the read list is nil, we init it.
8863     (and active
8864          (null (gnus-info-read info))
8865          (> (car active) 1)
8866          (gnus-info-set-read info (cons 1 (1- (car active)))))
8867     ;; Then we add the read articles to the range.
8868     (gnus-info-set-read
8869      info
8870      (setq range
8871            (gnus-add-to-range
8872             (gnus-info-read info) (setq articles (sort articles '<)))))
8873     ;; Then we have to re-compute how many unread
8874     ;; articles there are in this group.
8875     (if active
8876         (progn
8877           (cond
8878            ((not range)
8879             (setq num (- (1+ (cdr active)) (car active))))
8880            ((not (listp (cdr range)))
8881             (setq num (- (cdr active) (- (1+ (cdr range))
8882                                          (car range)))))
8883            (t
8884             (while range
8885               (if (numberp (car range))
8886                   (setq num (1+ num))
8887                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8888               (setq range (cdr range)))
8889             (setq num (- (cdr active) num))))
8890           ;; Update the number of unread articles.
8891           (setcar entry num)
8892           ;; Update the group buffer.
8893           (gnus-group-update-group group t)))))
8894
8895 (defun gnus-methods-equal-p (m1 m2)
8896   (let ((m1 (or m1 gnus-select-method))
8897         (m2 (or m2 gnus-select-method)))
8898     (or (equal m1 m2)
8899         (and (eq (car m1) (car m2))
8900              (or (not (memq 'address (assoc (symbol-name (car m1))
8901                                             gnus-valid-select-methods)))
8902                  (equal (nth 1 m1) (nth 1 m2)))))))
8903
8904 (defsubst gnus-header-value ()
8905   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8906
8907 (defvar gnus-newsgroup-none-id 0)
8908
8909 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8910   (let ((cur nntp-server-buffer)
8911         (dependencies
8912          (or dependencies
8913              (save-excursion (set-buffer gnus-summary-buffer)
8914                              gnus-newsgroup-dependencies)))
8915         headers id id-dep ref-dep end ref)
8916     (save-excursion
8917       (set-buffer nntp-server-buffer)
8918       (let ((case-fold-search t)
8919             in-reply-to header p lines)
8920         (goto-char (point-min))
8921         ;; Search to the beginning of the next header.  Error messages
8922         ;; do not begin with 2 or 3.
8923         (while (re-search-forward "^[23][0-9]+ " nil t)
8924           (setq id nil
8925                 ref nil)
8926           ;; This implementation of this function, with nine
8927           ;; search-forwards instead of the one re-search-forward and
8928           ;; a case (which basically was the old function) is actually
8929           ;; about twice as fast, even though it looks messier.  You
8930           ;; can't have everything, I guess.  Speed and elegance
8931           ;; doesn't always go hand in hand.
8932           (setq
8933            header
8934            (vector
8935             ;; Number.
8936             (prog1
8937                 (read cur)
8938               (end-of-line)
8939               (setq p (point))
8940               (narrow-to-region (point)
8941                                 (or (and (search-forward "\n.\n" nil t)
8942                                          (- (point) 2))
8943                                     (point))))
8944             ;; Subject.
8945             (progn
8946               (goto-char p)
8947               (if (search-forward "\nsubject: " nil t)
8948                   (gnus-header-value) "(none)"))
8949             ;; From.
8950             (progn
8951               (goto-char p)
8952               (if (search-forward "\nfrom: " nil t)
8953                   (gnus-header-value) "(nobody)"))
8954             ;; Date.
8955             (progn
8956               (goto-char p)
8957               (if (search-forward "\ndate: " nil t)
8958                   (gnus-header-value) ""))
8959             ;; Message-ID.
8960             (progn
8961               (goto-char p)
8962               (if (search-forward "\nmessage-id: " nil t)
8963                   (setq id (gnus-header-value))
8964                 ;; If there was no message-id, we just fake one to make
8965                 ;; subsequent routines simpler.
8966                 (setq id (concat "none+"
8967                                  (int-to-string
8968                                   (setq gnus-newsgroup-none-id
8969                                         (1+ gnus-newsgroup-none-id)))))))
8970             ;; References.
8971             (progn
8972               (goto-char p)
8973               (if (search-forward "\nreferences: " nil t)
8974                   (prog1
8975                       (gnus-header-value)
8976                     (setq end (match-end 0))
8977                     (save-excursion
8978                       (setq ref
8979                             (buffer-substring
8980                              (progn
8981                                (end-of-line)
8982                                (search-backward ">" end t)
8983                                (1+ (point)))
8984                              (progn
8985                                (search-backward "<" end t)
8986                                (point))))))
8987                 ;; Get the references from the in-reply-to header if there
8988                 ;; were no references and the in-reply-to header looks
8989                 ;; promising.
8990                 (if (and (search-forward "\nin-reply-to: " nil t)
8991                          (setq in-reply-to (gnus-header-value))
8992                          (string-match "<[^>]+>" in-reply-to))
8993                     (prog1
8994                         (setq ref (substring in-reply-to (match-beginning 0)
8995                                              (match-end 0)))
8996                       (setq ref ref)))
8997                 (setq ref "")))
8998             ;; Chars.
8999             0
9000             ;; Lines.
9001             (progn
9002               (goto-char p)
9003               (if (search-forward "\nlines: " nil t)
9004                   (if (numberp (setq lines (read cur)))
9005                       lines 0)
9006                 0))
9007             ;; Xref.
9008             (progn
9009               (goto-char p)
9010               (and (search-forward "\nxref: " nil t)
9011                    (gnus-header-value)))))
9012           ;; We do the threading while we read the headers.  The
9013           ;; message-id and the last reference are both entered into
9014           ;; the same hash table.  Some tippy-toeing around has to be
9015           ;; done in case an article has arrived before the article
9016           ;; which it refers to.
9017           (if (boundp (setq id-dep (intern id dependencies)))
9018               (if (and (car (symbol-value id-dep))
9019                        (not force-new))
9020                   ;; An article with this Message-ID has already
9021                   ;; been seen, so we ignore this one, except we add
9022                   ;; any additional Xrefs (in case the two articles
9023                   ;; came from different servers).
9024                   (progn
9025                     (mail-header-set-xref
9026                      (car (symbol-value id-dep))
9027                      (concat (or (mail-header-xref
9028                                   (car (symbol-value id-dep))) "")
9029                              (or (mail-header-xref header) "")))
9030                     (setq header nil))
9031                 (setcar (symbol-value id-dep) header))
9032             (set id-dep (list header)))
9033           (when header
9034             (if (boundp (setq ref-dep (intern ref dependencies)))
9035                 (setcdr (symbol-value ref-dep)
9036                         (nconc (cdr (symbol-value ref-dep))
9037                                (list (symbol-value id-dep))))
9038               (set ref-dep (list nil (symbol-value id-dep))))
9039             (setq headers (cons header headers)))
9040           (goto-char (point-max))
9041           (widen))
9042         (nreverse headers)))))
9043
9044 ;; The following macros and functions were written by Felix Lee
9045 ;; <flee@cse.psu.edu>.
9046
9047 (defmacro gnus-nov-read-integer ()
9048   '(prog1
9049        (if (= (following-char) ?\t)
9050            0
9051          (let ((num (condition-case nil (read buffer) (error nil))))
9052            (if (numberp num) num 0)))
9053      (or (eobp) (forward-char 1))))
9054
9055 (defmacro gnus-nov-skip-field ()
9056   '(search-forward "\t" eol 'move))
9057
9058 (defmacro gnus-nov-field ()
9059   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9060
9061 ;; Goes through the xover lines and returns a list of vectors
9062 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9063   "Parse the news overview data in the server buffer, and return a
9064 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9065   ;; Get the Xref when the users reads the articles since most/some
9066   ;; NNTP servers do not include Xrefs when using XOVER.
9067   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9068   (let ((cur nntp-server-buffer)
9069         (dependencies gnus-newsgroup-dependencies)
9070         number headers header)
9071     (save-excursion
9072       (set-buffer nntp-server-buffer)
9073       ;; Allow the user to mangle the headers before parsing them.
9074       (run-hooks 'gnus-parse-headers-hook)
9075       ;; Allow the user to mangle the headers before parsing them.
9076       (run-hooks 'gnus-parse-headers-hook)
9077       (goto-char (point-min))
9078       (while (and sequence (not (eobp)))
9079         (setq number (read cur))
9080         (while (and sequence (< (car sequence) number))
9081           (setq sequence (cdr sequence)))
9082         (and sequence
9083              (eq number (car sequence))
9084              (progn
9085                (setq sequence (cdr sequence))
9086                (if (setq header
9087                          (inline (gnus-nov-parse-line
9088                                   number dependencies force-new)))
9089                    (setq headers (cons header headers)))))
9090         (forward-line 1))
9091       (setq headers (nreverse headers)))
9092     headers))
9093
9094 ;; This function has to be called with point after the article number
9095 ;; on the beginning of the line.
9096 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9097   (let ((none 0)
9098         (eol (gnus-point-at-eol))
9099         (buffer (current-buffer))
9100         header ref id id-dep ref-dep)
9101
9102     ;; overview: [num subject from date id refs chars lines misc]
9103     (narrow-to-region (point) eol)
9104     (or (eobp) (forward-char))
9105
9106     (condition-case nil
9107         (setq header
9108               (vector
9109                number                   ; number
9110                (gnus-nov-field)         ; subject
9111                (gnus-nov-field)         ; from
9112                (gnus-nov-field)         ; date
9113                (setq id (or (gnus-nov-field)
9114                             (concat "none+"
9115                                     (int-to-string
9116                                      (setq none (1+ none)))))) ; id
9117                (progn
9118                  (save-excursion
9119                    (let ((beg (point)))
9120                      (search-forward "\t" eol)
9121                      (if (search-backward ">" beg t)
9122                          (setq ref
9123                                (buffer-substring
9124                                 (1+ (point))
9125                                 (search-backward "<" beg t)))
9126                        (setq ref nil))))
9127                  (gnus-nov-field))      ; refs
9128                (gnus-nov-read-integer)  ; chars
9129                (gnus-nov-read-integer)  ; lines
9130                (if (= (following-char) ?\n)
9131                    nil
9132                  (gnus-nov-field))      ; misc
9133                ))
9134       (error (progn
9135                (ding)
9136                (gnus-message 4 "Strange nov line")
9137                (setq header nil)
9138                (goto-char eol))))
9139
9140     (widen)
9141
9142     ;; We build the thread tree.
9143     (when header
9144       (if (boundp (setq id-dep (intern id dependencies)))
9145           (if (and (car (symbol-value id-dep))
9146                    (not force-new))
9147               ;; An article with this Message-ID has already been seen,
9148               ;; so we ignore this one, except we add any additional
9149               ;; Xrefs (in case the two articles came from different
9150               ;; servers.
9151               (progn
9152                 (mail-header-set-xref
9153                  (car (symbol-value id-dep))
9154                  (concat (or (mail-header-xref
9155                               (car (symbol-value id-dep))) "")
9156                          (or (mail-header-xref header) "")))
9157                 (setq header nil))
9158             (setcar (symbol-value id-dep) header))
9159         (set id-dep (list header))))
9160     (if header
9161         (progn
9162           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9163               (setcdr (symbol-value ref-dep)
9164                       (nconc (cdr (symbol-value ref-dep))
9165                              (list (symbol-value id-dep))))
9166             (set ref-dep (list nil (symbol-value id-dep))))))
9167     header))
9168
9169 (defun gnus-article-get-xrefs ()
9170   "Fill in the Xref value in `gnus-current-headers', if necessary.
9171 This is meant to be called in `gnus-article-internal-prepare-hook'."
9172   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9173                                  gnus-current-headers)))
9174     (or (not gnus-use-cross-reference)
9175         (not headers)
9176         (and (mail-header-xref headers)
9177              (not (string= (mail-header-xref headers) "")))
9178         (let ((case-fold-search t)
9179               xref)
9180           (save-restriction
9181             (nnheader-narrow-to-headers)
9182             (goto-char (point-min))
9183             (if (or (and (eq (downcase (following-char)) ?x)
9184                          (looking-at "Xref:"))
9185                     (search-forward "\nXref:" nil t))
9186                 (progn
9187                   (goto-char (1+ (match-end 0)))
9188                   (setq xref (buffer-substring (point)
9189                                                (progn (end-of-line) (point))))
9190                   (mail-header-set-xref headers xref))))))))
9191
9192 (defun gnus-summary-insert-subject (id)
9193   "Find article ID and insert the summary line for that article."
9194   (let ((header (gnus-read-header id))
9195         (number (and (numberp id) id)))
9196     (when header
9197       ;; Rebuild the thread that this article is part of and go to the
9198       ;; article we have fetched.
9199       (gnus-rebuild-thread (mail-header-id header))
9200       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9201     (when (and (numberp number)
9202                (> number 0))
9203       ;; We have to update the boundaries even if we can't fetch the
9204       ;; article if ID is a number -- so that the next `P' or `N'
9205       ;; command will fetch the previous (or next) article even
9206       ;; if the one we tried to fetch this time has been canceled.
9207       (and (> number gnus-newsgroup-end)
9208            (setq gnus-newsgroup-end number))
9209       (and (< number gnus-newsgroup-begin)
9210            (setq gnus-newsgroup-begin number))
9211       (setq gnus-newsgroup-unselected
9212             (delq number gnus-newsgroup-unselected)))
9213     ;; Report back a success?
9214     (and header number)))
9215
9216 (defun gnus-summary-work-articles (n)
9217   "Return a list of articles to be worked upon.  The prefix argument,
9218 the list of process marked articles, and the current article will be
9219 taken into consideration."
9220   (cond
9221    ((and n (numberp n))
9222     ;; A numerical prefix has been given.
9223     (let ((backward (< n 0))
9224           (n (abs n))
9225           articles article)
9226       (save-excursion
9227         (while
9228             (and (> n 0)
9229                  (push (setq article (gnus-summary-article-number))
9230                        articles)
9231                  (if backward
9232                      (gnus-summary-find-prev nil article)
9233                    (gnus-summary-find-next nil article)))
9234           (decf n)))
9235       (nreverse articles)))
9236    ((and (boundp 'transient-mark-mode)
9237          transient-mark-mode
9238          mark-active)
9239     ;; Work on the region between point and mark.
9240     (let ((max (max (point) (mark)))
9241           articles article)
9242       (save-excursion
9243         (goto-char (min (point) (mark)))
9244         (while
9245             (and
9246              (push (setq article (gnus-summary-article-number)) articles)
9247              (gnus-summary-find-next nil article)
9248              (< (point) max)))
9249         (nreverse articles))))
9250    (gnus-newsgroup-processable
9251     ;; There are process-marked articles present.
9252     (reverse gnus-newsgroup-processable))
9253    (t
9254     ;; Just return the current article.
9255     (list (gnus-summary-article-number)))))
9256
9257 (defun gnus-summary-search-group (&optional backward use-level)
9258   "Search for next unread newsgroup.
9259 If optional argument BACKWARD is non-nil, search backward instead."
9260   (save-excursion
9261     (set-buffer gnus-group-buffer)
9262     (if (gnus-group-search-forward
9263          backward nil (if use-level (gnus-group-group-level) nil))
9264         (gnus-group-group-name))))
9265
9266 (defun gnus-summary-best-group (&optional exclude-group)
9267   "Find the name of the best unread group.
9268 If EXCLUDE-GROUP, do not go to this group."
9269   (save-excursion
9270     (set-buffer gnus-group-buffer)
9271     (save-excursion
9272       (gnus-group-best-unread-group exclude-group))))
9273
9274 (defun gnus-summary-find-next (&optional unread article backward)
9275   (if backward (gnus-summary-find-prev)
9276     (let* ((article (or article (gnus-summary-article-number)))
9277            (arts (gnus-data-find-list article))
9278            result)
9279       (when (or (not gnus-summary-check-current)
9280                 (not unread)
9281                 (not (gnus-data-unread-p (car arts))))
9282         (setq arts (cdr arts)))
9283       (when (setq result
9284                   (if unread
9285                       (progn
9286                         (while arts
9287                           (when (gnus-data-unread-p (car arts))
9288                             (setq result (car arts)
9289                                   arts nil))
9290                           (setq arts (cdr arts)))
9291                         result)
9292                     (car arts)))
9293         (goto-char (gnus-data-pos result))
9294         (gnus-data-number result)))))
9295
9296 (defun gnus-summary-find-prev (&optional unread article)
9297   (let* ((article (or article (gnus-summary-article-number)))
9298          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9299          result)
9300     (when (or (not gnus-summary-check-current)
9301               (not unread)
9302               (not (gnus-data-unread-p (car arts))))
9303       (setq arts (cdr arts)))
9304     (if (setq result
9305               (if unread
9306                   (progn
9307                     (while arts
9308                       (and (gnus-data-unread-p (car arts))
9309                            (setq result (car arts)
9310                                  arts nil))
9311                       (setq arts (cdr arts)))
9312                     result)
9313                 (car arts)))
9314         (progn
9315           (goto-char (gnus-data-pos result))
9316           (gnus-data-number result)))))
9317
9318 (defun gnus-summary-find-subject (subject &optional unread backward article)
9319   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9320          (article (or article (gnus-summary-article-number)))
9321          (articles (gnus-data-list backward))
9322          (arts (gnus-data-find-list article articles))
9323          result)
9324     (when (or (not gnus-summary-check-current)
9325               (not unread)
9326               (not (gnus-data-unread-p (car arts))))
9327       (setq arts (cdr arts)))
9328     (while arts
9329       (and (or (not unread)
9330                (gnus-data-unread-p (car arts)))
9331            (vectorp (gnus-data-header (car arts)))
9332            (gnus-subject-equal
9333             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9334            (setq result (car arts)
9335                  arts nil))
9336       (setq arts (cdr arts)))
9337     (and result
9338          (goto-char (gnus-data-pos result))
9339          (gnus-data-number result))))
9340
9341 (defun gnus-summary-search-forward (&optional unread subject backward)
9342   "Search forward for an article.
9343 If UNREAD, look for unread articles.  If SUBJECT, look for
9344 articles with that subject.  If BACKWARD, search backward instead."
9345   (cond (subject (gnus-summary-find-subject subject unread backward))
9346         (backward (gnus-summary-find-prev unread))
9347         (t (gnus-summary-find-next unread))))
9348
9349 (defun gnus-recenter (&optional n)
9350   "Center point in window and redisplay frame.
9351 Also do horizontal recentering."
9352   (interactive "P")
9353   (when (and gnus-auto-center-summary
9354              (not (eq gnus-auto-center-summary 'vertical)))
9355     (gnus-horizontal-recenter))
9356   (recenter n))
9357
9358 (defun gnus-summary-recenter ()
9359   "Center point in the summary window.
9360 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9361 displayed, no centering will be performed."
9362   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9363   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9364   (let* ((top (cond ((< (window-height) 4) 0)
9365                     ((< (window-height) 7) 1)
9366                     (t 2)))
9367          (height (1- (window-height)))
9368          (bottom (save-excursion (goto-char (point-max))
9369                                  (forward-line (- height))
9370                                  (point)))
9371          (window (get-buffer-window (current-buffer))))
9372     ;; The user has to want it.
9373     (when gnus-auto-center-summary
9374       (when (get-buffer-window gnus-article-buffer)
9375        ;; Only do recentering when the article buffer is displayed,
9376        ;; Set the window start to either `bottom', which is the biggest
9377        ;; possible valid number, or the second line from the top,
9378        ;; whichever is the least.
9379        (set-window-start
9380         window (min bottom (save-excursion 
9381                              (forward-line (- top)) (point)))))
9382       ;; Do horizontal recentering while we're at it.
9383       (when (and (get-buffer-window (current-buffer) t)
9384                  (not (eq gnus-auto-center-summary 'vertical)))
9385         (let ((selected (selected-window)))
9386           (select-window (get-buffer-window (current-buffer) t))
9387           (gnus-summary-position-point)
9388           (gnus-horizontal-recenter)
9389           (select-window selected))))))
9390
9391 (defun gnus-horizontal-recenter ()
9392   "Recenter the current buffer horizontally."
9393   (if (< (current-column) (/ (window-width) 2))
9394       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9395     (let* ((orig (point))
9396            (end (window-end (get-buffer-window (current-buffer) t)))
9397            (max 0))
9398       ;; Find the longest line currently displayed in the window.
9399       (goto-char (window-start))
9400       (while (and (not (eobp)) 
9401                   (< (point) end))
9402         (end-of-line)
9403         (setq max (max max (current-column)))
9404         (forward-line 1))
9405       (goto-char orig)
9406       ;; Scroll horizontally to center (sort of) the point.
9407       (if (> max (window-width))
9408           (set-window-hscroll 
9409            (get-buffer-window (current-buffer) t)
9410            (min (- (current-column) (/ (window-width) 3))
9411                 (+ 2 (- max (window-width)))))
9412         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9413       max)))
9414
9415 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9416 (defun gnus-short-group-name (group &optional levels)
9417   "Collapse GROUP name LEVELS."
9418   (let* ((name "") 
9419          (foreign "")
9420          (depth 0) 
9421          (skip 1)
9422          (levels (or levels
9423                      (progn
9424                        (while (string-match "\\." group skip)
9425                          (setq skip (match-end 0)
9426                                depth (+ depth 1)))
9427                        depth))))
9428     (if (string-match ":" group)
9429         (setq foreign (substring group 0 (match-end 0))
9430               group (substring group (match-end 0))))
9431     (while group
9432       (if (and (string-match "\\." group)
9433                (> levels (- gnus-group-uncollapsed-levels 1)))
9434           (setq name (concat name (substring group 0 1))
9435                 group (substring group (match-end 0))
9436                 levels (- levels 1)
9437                 name (concat name "."))
9438         (setq name (concat foreign name group)
9439               group nil)))
9440     name))
9441
9442 (defun gnus-summary-jump-to-group (newsgroup)
9443   "Move point to NEWSGROUP in group mode buffer."
9444   ;; Keep update point of group mode buffer if visible.
9445   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9446       (save-window-excursion
9447         ;; Take care of tree window mode.
9448         (if (get-buffer-window gnus-group-buffer)
9449             (pop-to-buffer gnus-group-buffer))
9450         (gnus-group-jump-to-group newsgroup))
9451     (save-excursion
9452       ;; Take care of tree window mode.
9453       (if (get-buffer-window gnus-group-buffer)
9454           (pop-to-buffer gnus-group-buffer)
9455         (set-buffer gnus-group-buffer))
9456       (gnus-group-jump-to-group newsgroup))))
9457
9458 ;; This function returns a list of article numbers based on the
9459 ;; difference between the ranges of read articles in this group and
9460 ;; the range of active articles.
9461 (defun gnus-list-of-unread-articles (group)
9462   (let* ((read (gnus-info-read (gnus-get-info group)))
9463          (active (gnus-active group))
9464          (last (cdr active))
9465          first nlast unread)
9466     ;; If none are read, then all are unread.
9467     (if (not read)
9468         (setq first (car active))
9469       ;; If the range of read articles is a single range, then the
9470       ;; first unread article is the article after the last read
9471       ;; article.  Sounds logical, doesn't it?
9472       (if (not (listp (cdr read)))
9473           (setq first (1+ (cdr read)))
9474         ;; `read' is a list of ranges.
9475         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9476                                 (caar read))) 1)
9477             (setq first 1))
9478         (while read
9479           (if first
9480               (while (< first nlast)
9481                 (setq unread (cons first unread))
9482                 (setq first (1+ first))))
9483           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9484           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9485           (setq read (cdr read)))))
9486     ;; And add the last unread articles.
9487     (while (<= first last)
9488       (setq unread (cons first unread))
9489       (setq first (1+ first)))
9490     ;; Return the list of unread articles.
9491     (nreverse unread)))
9492
9493 (defun gnus-list-of-read-articles (group)
9494   "Return a list of unread, unticked and non-dormant articles."
9495   (let* ((info (gnus-get-info group))
9496          (marked (gnus-info-marks info))
9497          (active (gnus-active group)))
9498     (and info active
9499          (gnus-set-difference
9500           (gnus-sorted-complement
9501            (gnus-uncompress-range active)
9502            (gnus-list-of-unread-articles group))
9503           (append
9504            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9505            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9506
9507 ;; Various summary commands
9508
9509 (defun gnus-summary-universal-argument (arg)
9510   "Perform any operation on all articles that are process/prefixed."
9511   (interactive "P")
9512   (gnus-set-global-variables)
9513   (let ((articles (gnus-summary-work-articles arg))
9514         func article)
9515     (if (eq
9516          (setq
9517           func
9518           (key-binding
9519            (read-key-sequence
9520             (substitute-command-keys
9521              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9522              ))))
9523          'undefined)
9524         (progn
9525           (message "Undefined key")
9526           (ding))
9527       (save-excursion
9528         (while articles
9529           (gnus-summary-goto-subject (setq article (pop articles)))
9530           (command-execute func)
9531           (gnus-summary-remove-process-mark article)))))
9532   (gnus-summary-position-point))
9533
9534 (defun gnus-summary-toggle-truncation (&optional arg)
9535   "Toggle truncation of summary lines.
9536 With arg, turn line truncation on iff arg is positive."
9537   (interactive "P")
9538   (setq truncate-lines
9539         (if (null arg) (not truncate-lines)
9540           (> (prefix-numeric-value arg) 0)))
9541   (redraw-display))
9542
9543 (defun gnus-summary-reselect-current-group (&optional all rescan)
9544   "Exit and then reselect the current newsgroup.
9545 The prefix argument ALL means to select all articles."
9546   (interactive "P")
9547   (gnus-set-global-variables)
9548   (let ((current-subject (gnus-summary-article-number))
9549         (group gnus-newsgroup-name))
9550     (setq gnus-newsgroup-begin nil)
9551     (gnus-summary-exit)
9552     ;; We have to adjust the point of group mode buffer because the
9553     ;; current point was moved to the next unread newsgroup by
9554     ;; exiting.
9555     (gnus-summary-jump-to-group group)
9556     (when rescan
9557       (save-excursion
9558         (gnus-group-get-new-news-this-group 1)))
9559     (gnus-group-read-group all t)
9560     (gnus-summary-goto-subject current-subject)))
9561
9562 (defun gnus-summary-rescan-group (&optional all)
9563   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9564   (interactive "P")
9565   (gnus-summary-reselect-current-group all t))
9566
9567 (defun gnus-summary-update-info ()
9568   (let* ((group gnus-newsgroup-name))
9569     (when gnus-newsgroup-kill-headers
9570       (setq gnus-newsgroup-killed
9571             (gnus-compress-sequence
9572              (nconc
9573               (gnus-set-sorted-intersection
9574                (gnus-uncompress-range gnus-newsgroup-killed)
9575                (setq gnus-newsgroup-unselected
9576                      (sort gnus-newsgroup-unselected '<)))
9577               (setq gnus-newsgroup-unreads
9578                     (sort gnus-newsgroup-unreads '<))) t)))
9579     (unless (listp (cdr gnus-newsgroup-killed))
9580       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9581     (let ((headers gnus-newsgroup-headers))
9582       (run-hooks 'gnus-exit-group-hook)
9583       (unless gnus-save-score
9584         (setq gnus-newsgroup-scored nil))
9585       ;; Set the new ranges of read articles.
9586       (gnus-update-read-articles
9587        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9588       ;; Set the current article marks.
9589       (gnus-update-marks)
9590       ;; Do the cross-ref thing.
9591       (when gnus-use-cross-reference
9592         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9593       ;; Do adaptive scoring, and possibly save score files.
9594       (when gnus-newsgroup-adaptive
9595         (gnus-score-adaptive))
9596       (when gnus-use-scoring
9597         (gnus-score-save))
9598       ;; Do not switch windows but change the buffer to work.
9599       (set-buffer gnus-group-buffer)
9600       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9601           (gnus-group-update-group group)))))
9602
9603 (defun gnus-summary-exit (&optional temporary)
9604   "Exit reading current newsgroup, and then return to group selection mode.
9605 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9606   (interactive)
9607   (gnus-set-global-variables)
9608   (gnus-kill-save-kill-buffer)
9609   (let* ((group gnus-newsgroup-name)
9610          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9611          (mode major-mode)
9612          (buf (current-buffer)))
9613     (run-hooks 'gnus-summary-prepare-exit-hook)
9614     ;; If we have several article buffers, we kill them at exit.
9615     (unless gnus-single-article-buffer
9616       (gnus-kill-buffer gnus-article-buffer)
9617       (gnus-kill-buffer gnus-original-article-buffer))
9618     (when gnus-use-cache
9619       (gnus-cache-possibly-remove-articles)
9620       (gnus-cache-save-buffers))
9621     (when gnus-use-trees
9622       (gnus-tree-close group))
9623     ;; Make all changes in this group permanent.
9624     (unless quit-config
9625       (gnus-summary-update-info))
9626     (gnus-close-group group)
9627     ;; Make sure where I was, and go to next newsgroup.
9628     (set-buffer gnus-group-buffer)
9629     (unless quit-config
9630       (gnus-group-jump-to-group group)
9631       (gnus-group-next-unread-group 1))
9632     (run-hooks 'gnus-summary-exit-hook)
9633     (if temporary
9634         nil                             ;Nothing to do.
9635       ;; If we have several article buffers, we kill them at exit.
9636       (unless gnus-single-article-buffer
9637         (gnus-kill-buffer gnus-article-buffer)
9638         (gnus-kill-buffer gnus-original-article-buffer))
9639       (set-buffer buf)
9640       (if (not gnus-kill-summary-on-exit)
9641           (gnus-deaden-summary)
9642         ;; We set all buffer-local variables to nil.  It is unclear why
9643         ;; this is needed, but if we don't, buffer-local variables are
9644         ;; not garbage-collected, it seems.  This would the lead to en
9645         ;; ever-growing Emacs.
9646         (gnus-summary-clear-local-variables)
9647         (when (get-buffer gnus-article-buffer)
9648           (bury-buffer gnus-article-buffer))
9649         ;; We clear the global counterparts of the buffer-local
9650         ;; variables as well, just to be on the safe side.
9651         (gnus-configure-windows 'group 'force)
9652         (gnus-summary-clear-local-variables)
9653         ;; Return to group mode buffer.
9654         (if (eq mode 'gnus-summary-mode)
9655             (gnus-kill-buffer buf)))
9656       (setq gnus-current-select-method gnus-select-method)
9657       (pop-to-buffer gnus-group-buffer)
9658       ;; Clear the current group name.
9659       (if (not quit-config)
9660           (progn
9661             (gnus-group-jump-to-group group)
9662             (gnus-group-next-unread-group 1)
9663             (gnus-configure-windows 'group 'force))
9664         (if (not (buffer-name (car quit-config)))
9665             (gnus-configure-windows 'group 'force)
9666           (set-buffer (car quit-config))
9667           (and (eq major-mode 'gnus-summary-mode)
9668                (gnus-set-global-variables))
9669           (gnus-configure-windows (cdr quit-config))))
9670       (unless quit-config
9671         (setq gnus-newsgroup-name nil)))))
9672
9673 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9674 (defun gnus-summary-exit-no-update (&optional no-questions)
9675   "Quit reading current newsgroup without updating read article info."
9676   (interactive)
9677   (gnus-set-global-variables)
9678   (let* ((group gnus-newsgroup-name)
9679          (quit-config (gnus-group-quit-config group)))
9680     (when (or no-questions
9681               gnus-expert-user
9682               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9683       ;; If we have several article buffers, we kill them at exit.
9684       (unless gnus-single-article-buffer
9685         (gnus-kill-buffer gnus-article-buffer)
9686         (gnus-kill-buffer gnus-original-article-buffer))
9687       (if (not gnus-kill-summary-on-exit)
9688           (gnus-deaden-summary)
9689         (gnus-close-group group)
9690         (gnus-summary-clear-local-variables)
9691         (set-buffer gnus-group-buffer)
9692         (gnus-summary-clear-local-variables)
9693         (when (get-buffer gnus-summary-buffer)
9694           (kill-buffer gnus-summary-buffer)))
9695       (when gnus-use-trees
9696         (gnus-tree-close group))
9697       (when (get-buffer gnus-article-buffer)
9698         (bury-buffer gnus-article-buffer))
9699       ;; Return to the group buffer.
9700       (gnus-configure-windows 'group 'force)
9701       ;; Clear the current group name.
9702       (setq gnus-newsgroup-name nil)
9703       (when (equal (gnus-group-group-name) group)
9704         (gnus-group-next-unread-group 1))
9705       (when quit-config
9706         (if (not (buffer-name (car quit-config)))
9707             (gnus-configure-windows 'group 'force)
9708           (set-buffer (car quit-config))
9709           (when (eq major-mode 'gnus-summary-mode)
9710             (gnus-set-global-variables))
9711           (gnus-configure-windows (cdr quit-config)))))))
9712
9713 ;;; Dead summaries.
9714
9715 (defvar gnus-dead-summary-mode-map nil)
9716
9717 (if gnus-dead-summary-mode-map
9718     nil
9719   (setq gnus-dead-summary-mode-map (make-keymap))
9720   (suppress-keymap gnus-dead-summary-mode-map)
9721   (substitute-key-definition
9722    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9723   (let ((keys '("\C-d" "\r" "\177")))
9724     (while keys
9725       (define-key gnus-dead-summary-mode-map
9726         (pop keys) 'gnus-summary-wake-up-the-dead))))
9727
9728 (defvar gnus-dead-summary-mode nil
9729   "Minor mode for Gnus summary buffers.")
9730
9731 (defun gnus-dead-summary-mode (&optional arg)
9732   "Minor mode for Gnus summary buffers."
9733   (interactive "P")
9734   (when (eq major-mode 'gnus-summary-mode)
9735     (make-local-variable 'gnus-dead-summary-mode)
9736     (setq gnus-dead-summary-mode
9737           (if (null arg) (not gnus-dead-summary-mode)
9738             (> (prefix-numeric-value arg) 0)))
9739     (when gnus-dead-summary-mode
9740       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9741         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9742       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9743         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9744               minor-mode-map-alist)))))
9745
9746 (defun gnus-deaden-summary ()
9747   "Make the current summary buffer into a dead summary buffer."
9748   ;; Kill any previous dead summary buffer.
9749   (when (and gnus-dead-summary
9750              (buffer-name gnus-dead-summary))
9751     (save-excursion
9752       (set-buffer gnus-dead-summary)
9753       (when gnus-dead-summary-mode
9754         (kill-buffer (current-buffer)))))
9755   ;; Make this the current dead summary.
9756   (setq gnus-dead-summary (current-buffer))
9757   (gnus-dead-summary-mode 1)
9758   (let ((name (buffer-name)))
9759     (when (string-match "Summary" name)
9760       (rename-buffer
9761        (concat (substring name 0 (match-beginning 0)) "Dead "
9762                (substring name (match-beginning 0))) t))))
9763
9764 (defun gnus-kill-or-deaden-summary (buffer)
9765   "Kill or deaden the summary BUFFER."
9766   (cond (gnus-kill-summary-on-exit
9767          (when (and gnus-use-trees
9768                     (and (get-buffer buffer)
9769                          (buffer-name (get-buffer buffer))))
9770            (save-excursion
9771              (set-buffer (get-buffer buffer))
9772              (gnus-tree-close gnus-newsgroup-name)))
9773          (gnus-kill-buffer buffer))
9774         ((and (get-buffer buffer)
9775               (buffer-name (get-buffer buffer)))
9776          (save-excursion
9777            (set-buffer buffer)
9778            (gnus-deaden-summary)))))
9779
9780 (defun gnus-summary-wake-up-the-dead (&rest args)
9781   "Wake up the dead summary buffer."
9782   (interactive)
9783   (gnus-dead-summary-mode -1)
9784   (let ((name (buffer-name)))
9785     (when (string-match "Dead " name)
9786       (rename-buffer
9787        (concat (substring name 0 (match-beginning 0))
9788                (substring name (match-end 0))) t)))
9789   (gnus-message 3 "This dead summary is now alive again"))
9790
9791 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9792 (defun gnus-summary-fetch-faq (&optional faq-dir)
9793   "Fetch the FAQ for the current group.
9794 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9795 in."
9796   (interactive
9797    (list
9798     (if current-prefix-arg
9799         (completing-read
9800          "Faq dir: " (and (listp gnus-group-faq-directory)
9801                           gnus-group-faq-directory)))))
9802   (let (gnus-faq-buffer)
9803     (and (setq gnus-faq-buffer
9804                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9805          (gnus-configure-windows 'summary-faq))))
9806
9807 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9808 (defun gnus-summary-describe-group (&optional force)
9809   "Describe the current newsgroup."
9810   (interactive "P")
9811   (gnus-group-describe-group force gnus-newsgroup-name))
9812
9813 (defun gnus-summary-describe-briefly ()
9814   "Describe summary mode commands briefly."
9815   (interactive)
9816   (gnus-message 6
9817                 (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")))
9818
9819 ;; Walking around group mode buffer from summary mode.
9820
9821 (defun gnus-summary-next-group (&optional no-article target-group backward)
9822   "Exit current newsgroup and then select next unread newsgroup.
9823 If prefix argument NO-ARTICLE is non-nil, no article is selected
9824 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9825 previous group instead."
9826   (interactive "P")
9827   (gnus-set-global-variables)
9828   (let ((current-group gnus-newsgroup-name)
9829         (current-buffer (current-buffer))
9830         entered)
9831     ;; First we semi-exit this group to update Xrefs and all variables.
9832     ;; We can't do a real exit, because the window conf must remain
9833     ;; the same in case the user is prompted for info, and we don't
9834     ;; want the window conf to change before that...
9835     (gnus-summary-exit t)
9836     (while (not entered)
9837       ;; Then we find what group we are supposed to enter.
9838       (set-buffer gnus-group-buffer)
9839       (gnus-group-jump-to-group current-group)
9840       (setq target-group
9841             (or target-group
9842                 (if (eq gnus-keep-same-level 'best)
9843                     (gnus-summary-best-group gnus-newsgroup-name)
9844                   (gnus-summary-search-group backward gnus-keep-same-level))))
9845       (if (not target-group)
9846           ;; There are no further groups, so we return to the group
9847           ;; buffer.
9848           (progn
9849             (gnus-message 5 "Returning to the group buffer")
9850             (setq entered t)
9851             (set-buffer current-buffer)
9852             (gnus-summary-exit))
9853         ;; We try to enter the target group.
9854         (gnus-group-jump-to-group target-group)
9855         (let ((unreads (gnus-group-group-unread)))
9856           (if (and (or (eq t unreads)
9857                        (and unreads (not (zerop unreads))))
9858                    (gnus-summary-read-group
9859                     target-group nil no-article current-buffer))
9860               (setq entered t)
9861             (setq current-group target-group
9862                   target-group nil)))))))
9863
9864 (defun gnus-summary-prev-group (&optional no-article)
9865   "Exit current newsgroup and then select previous unread newsgroup.
9866 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9867   (interactive "P")
9868   (gnus-summary-next-group no-article nil t))
9869
9870 ;; Walking around summary lines.
9871
9872 (defun gnus-summary-first-subject (&optional unread)
9873   "Go to the first unread subject.
9874 If UNREAD is non-nil, go to the first unread article.
9875 Returns the article selected or nil if there are no unread articles."
9876   (interactive "P")
9877   (prog1
9878       (cond
9879        ;; Empty summary.
9880        ((null gnus-newsgroup-data)
9881         (gnus-message 3 "No articles in the group")
9882         nil)
9883        ;; Pick the first article.
9884        ((not unread)
9885         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9886         (gnus-data-number (car gnus-newsgroup-data)))
9887        ;; No unread articles.
9888        ((null gnus-newsgroup-unreads)
9889         (gnus-message 3 "No more unread articles")
9890         nil)
9891        ;; Find the first unread article.
9892        (t
9893         (let ((data gnus-newsgroup-data))
9894           (while (and data
9895                       (not (gnus-data-unread-p (car data))))
9896             (setq data (cdr data)))
9897           (if data
9898               (progn
9899                 (goto-char (gnus-data-pos (car data)))
9900                 (gnus-data-number (car data)))))))
9901     (gnus-summary-position-point)))
9902
9903 (defun gnus-summary-next-subject (n &optional unread dont-display)
9904   "Go to next N'th summary line.
9905 If N is negative, go to the previous N'th subject line.
9906 If UNREAD is non-nil, only unread articles are selected.
9907 The difference between N and the actual number of steps taken is
9908 returned."
9909   (interactive "p")
9910   (let ((backward (< n 0))
9911         (n (abs n)))
9912     (while (and (> n 0)
9913                 (if backward
9914                     (gnus-summary-find-prev unread)
9915                   (gnus-summary-find-next unread)))
9916       (setq n (1- n)))
9917     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9918                                (if unread " unread" "")))
9919     (unless dont-display
9920       (gnus-summary-recenter)
9921       (gnus-summary-position-point))
9922     n))
9923
9924 (defun gnus-summary-next-unread-subject (n)
9925   "Go to next N'th unread summary line."
9926   (interactive "p")
9927   (gnus-summary-next-subject n t))
9928
9929 (defun gnus-summary-prev-subject (n &optional unread)
9930   "Go to previous N'th summary line.
9931 If optional argument UNREAD is non-nil, only unread article is selected."
9932   (interactive "p")
9933   (gnus-summary-next-subject (- n) unread))
9934
9935 (defun gnus-summary-prev-unread-subject (n)
9936   "Go to previous N'th unread summary line."
9937   (interactive "p")
9938   (gnus-summary-next-subject (- n) t))
9939
9940 (defun gnus-summary-goto-subject (article &optional force silent)
9941   "Go the subject line of ARTICLE.
9942 If FORCE, also allow jumping to articles not currently shown."
9943   (let ((b (point))
9944         (data (gnus-data-find article)))
9945     ;; We read in the article if we have to.
9946     (and (not data)
9947          force
9948          (gnus-summary-insert-subject article)
9949          (setq data (gnus-data-find article)))
9950     (goto-char b)
9951     (if (not data)
9952         (progn
9953           (unless silent
9954             (gnus-message 3 "Can't find article %d" article))
9955           nil)
9956       (goto-char (gnus-data-pos data))
9957       article)))
9958
9959 ;; Walking around summary lines with displaying articles.
9960
9961 (defun gnus-summary-expand-window (&optional arg)
9962   "Make the summary buffer take up the entire Emacs frame.
9963 Given a prefix, will force an `article' buffer configuration."
9964   (interactive "P")
9965   (gnus-set-global-variables)
9966   (if arg
9967       (gnus-configure-windows 'article 'force)
9968     (gnus-configure-windows 'summary 'force)))
9969
9970 (defun gnus-summary-display-article (article &optional all-header)
9971   "Display ARTICLE in article buffer."
9972   (gnus-set-global-variables)
9973   (if (null article)
9974       nil
9975     (prog1
9976         (if gnus-summary-display-article-function
9977             (funcall gnus-summary-display-article-function article all-header)
9978           (gnus-article-prepare article all-header))
9979       (run-hooks 'gnus-select-article-hook)
9980       (gnus-summary-recenter)
9981       (gnus-summary-goto-subject article)
9982       (when gnus-use-trees
9983         (gnus-possibly-generate-tree article)
9984         (gnus-highlight-selected-tree article))
9985       ;; Successfully display article.
9986       (gnus-article-set-window-start
9987        (cdr (assq article gnus-newsgroup-bookmarks)))
9988       t)))
9989
9990 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9991   "Select the current article.
9992 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9993 non-nil, the article will be re-fetched even if it already present in
9994 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9995 be displayed."
9996   (let ((article (or article (gnus-summary-article-number)))
9997         (all-headers (not (not all-headers))) ;Must be T or NIL.
9998         gnus-summary-display-article-function
9999         did)
10000     (and (not pseudo)
10001          (gnus-summary-article-pseudo-p article)
10002          (error "This is a pseudo-article."))
10003     (prog1
10004         (save-excursion
10005           (set-buffer gnus-summary-buffer)
10006           (if (or (and gnus-single-article-buffer
10007                        (or (null gnus-current-article)
10008                            (null gnus-article-current)
10009                            (null (get-buffer gnus-article-buffer))
10010                            (not (eq article (cdr gnus-article-current)))
10011                            (not (equal (car gnus-article-current)
10012                                        gnus-newsgroup-name))))
10013                   (and (not gnus-single-article-buffer)
10014                        (null gnus-current-article))
10015                   force)
10016               ;; The requested article is different from the current article.
10017               (prog1
10018                   (gnus-summary-display-article article all-headers)
10019                 (setq did article))
10020             (if (or all-headers gnus-show-all-headers)
10021                 (gnus-article-show-all-headers))
10022             'old))
10023       (if did
10024           (gnus-article-set-window-start
10025            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10026
10027 (defun gnus-summary-set-current-mark (&optional current-mark)
10028   "Obsolete function."
10029   nil)
10030
10031 (defun gnus-summary-next-article (&optional unread subject backward push)
10032   "Select the next article.
10033 If UNREAD, only unread articles are selected.
10034 If SUBJECT, only articles with SUBJECT are selected.
10035 If BACKWARD, the previous article is selected instead of the next."
10036   (interactive "P")
10037   (gnus-set-global-variables)
10038   (cond
10039    ;; Is there such an article?
10040    ((and (gnus-summary-search-forward unread subject backward)
10041          (or (gnus-summary-display-article (gnus-summary-article-number))
10042              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10043     (gnus-summary-position-point))
10044    ;; If not, we try the first unread, if that is wanted.
10045    ((and subject
10046          gnus-auto-select-same
10047          (or (gnus-summary-first-unread-article)
10048              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10049     (gnus-summary-position-point)
10050     (gnus-message 6 "Wrapped"))
10051    ;; Try to get next/previous article not displayed in this group.
10052    ((and gnus-auto-extend-newsgroup
10053          (not unread) (not subject))
10054     (gnus-summary-goto-article
10055      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10056      nil t))
10057    ;; Go to next/previous group.
10058    (t
10059     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10060         (gnus-summary-jump-to-group gnus-newsgroup-name))
10061     (let ((cmd last-command-char)
10062           (group
10063            (if (eq gnus-keep-same-level 'best)
10064                (gnus-summary-best-group gnus-newsgroup-name)
10065              (gnus-summary-search-group backward gnus-keep-same-level))))
10066       ;; For some reason, the group window gets selected.  We change
10067       ;; it back.
10068       (select-window (get-buffer-window (current-buffer)))
10069       ;; Select next unread newsgroup automagically.
10070       (cond
10071        ((not gnus-auto-select-next)
10072         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10073        ((or (eq gnus-auto-select-next 'quietly)
10074             (and (eq gnus-auto-select-next 'slightly-quietly)
10075                  push)
10076             (and (eq gnus-auto-select-next 'almost-quietly)
10077                  (gnus-summary-last-article-p)))
10078         ;; Select quietly.
10079         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10080             (gnus-summary-exit)
10081           (gnus-message 7 "No more%s articles (%s)..."
10082                         (if unread " unread" "")
10083                         (if group (concat "selecting " group)
10084                           "exiting"))
10085           (gnus-summary-next-group nil group backward)))
10086        (t
10087         (gnus-summary-walk-group-buffer
10088          gnus-newsgroup-name cmd unread backward)))))))
10089
10090 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10091   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10092                       (?\C-p (gnus-group-prev-unread-group 1))))
10093         keve key group ended)
10094     (save-excursion
10095       (set-buffer gnus-group-buffer)
10096       (gnus-summary-jump-to-group from-group)
10097       (setq group
10098             (if (eq gnus-keep-same-level 'best)
10099                 (gnus-summary-best-group gnus-newsgroup-name)
10100               (gnus-summary-search-group backward gnus-keep-same-level))))
10101     (while (not ended)
10102       (gnus-message
10103        5 "No more%s articles%s" (if unread " unread" "")
10104        (if (and group
10105                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10106            (format " (Type %s for %s [%s])"
10107                    (single-key-description cmd) group
10108                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10109          (format " (Type %s to exit %s)"
10110                  (single-key-description cmd)
10111                  gnus-newsgroup-name)))
10112       ;; Confirm auto selection.
10113       (setq key (car (setq keve (gnus-read-event-char))))
10114       (setq ended t)
10115       (cond
10116        ((assq key keystrokes)
10117         (let ((obuf (current-buffer)))
10118           (switch-to-buffer gnus-group-buffer)
10119           (and group
10120                (gnus-group-jump-to-group group))
10121           (eval (cadr (assq key keystrokes)))
10122           (setq group (gnus-group-group-name))
10123           (switch-to-buffer obuf))
10124         (setq ended nil))
10125        ((equal key cmd)
10126         (if (or (not group)
10127                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10128             (gnus-summary-exit)
10129           (gnus-summary-next-group nil group backward)))
10130        (t
10131         (push (cdr keve) unread-command-events))))))
10132
10133 (defun gnus-read-event-char ()
10134   "Get the next event."
10135   (let ((event (read-event)))
10136     (cons (and (numberp event) event) event)))
10137
10138 (defun gnus-summary-next-unread-article ()
10139   "Select unread article after current one."
10140   (interactive)
10141   (gnus-summary-next-article t (and gnus-auto-select-same
10142                                     (gnus-summary-article-subject))))
10143
10144 (defun gnus-summary-prev-article (&optional unread subject)
10145   "Select the article after the current one.
10146 If UNREAD is non-nil, only unread articles are selected."
10147   (interactive "P")
10148   (gnus-summary-next-article unread subject t))
10149
10150 (defun gnus-summary-prev-unread-article ()
10151   "Select unred article before current one."
10152   (interactive)
10153   (gnus-summary-prev-article t (and gnus-auto-select-same
10154                                     (gnus-summary-article-subject))))
10155
10156 (defun gnus-summary-next-page (&optional lines circular)
10157   "Show next page of the selected article.
10158 If at the end of the current article, select the next article.
10159 LINES says how many lines should be scrolled up.
10160
10161 If CIRCULAR is non-nil, go to the start of the article instead of
10162 selecting the next article when reaching the end of the current
10163 article."
10164   (interactive "P")
10165   (setq gnus-summary-buffer (current-buffer))
10166   (gnus-set-global-variables)
10167   (let ((article (gnus-summary-article-number))
10168         (endp nil))
10169     (gnus-configure-windows 'article)
10170     (if (or (null gnus-current-article)
10171             (null gnus-article-current)
10172             (/= article (cdr gnus-article-current))
10173             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10174         ;; Selected subject is different from current article's.
10175         (gnus-summary-display-article article)
10176       (gnus-eval-in-buffer-window
10177        gnus-article-buffer
10178        (setq endp (gnus-article-next-page lines)))
10179       (if endp
10180           (cond (circular
10181                  (gnus-summary-beginning-of-article))
10182                 (lines
10183                  (gnus-message 3 "End of message"))
10184                 ((null lines)
10185                  (if (and (eq gnus-summary-goto-unread 'never)
10186                           (not (eq article gnus-newsgroup-end)))
10187                      (gnus-summary-next-article)
10188                    (gnus-summary-next-unread-article))))))
10189     (gnus-summary-recenter)
10190     (gnus-summary-position-point)))
10191
10192 (defun gnus-summary-prev-page (&optional lines)
10193   "Show previous page of selected article.
10194 Argument LINES specifies lines to be scrolled down."
10195   (interactive "P")
10196   (gnus-set-global-variables)
10197   (let ((article (gnus-summary-article-number)))
10198     (gnus-configure-windows 'article)
10199     (if (or (null gnus-current-article)
10200             (null gnus-article-current)
10201             (/= article (cdr gnus-article-current))
10202             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10203         ;; Selected subject is different from current article's.
10204         (gnus-summary-display-article article)
10205       (gnus-summary-recenter)
10206       (gnus-eval-in-buffer-window gnus-article-buffer
10207                                   (gnus-article-prev-page lines))))
10208   (gnus-summary-position-point))
10209
10210 (defun gnus-summary-scroll-up (lines)
10211   "Scroll up (or down) one line current article.
10212 Argument LINES specifies lines to be scrolled up (or down if negative)."
10213   (interactive "p")
10214   (gnus-set-global-variables)
10215   (gnus-configure-windows 'article)
10216   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10217     (gnus-eval-in-buffer-window
10218      gnus-article-buffer
10219      (cond ((> lines 0)
10220             (if (gnus-article-next-page lines)
10221                 (gnus-message 3 "End of message")))
10222            ((< lines 0)
10223             (gnus-article-prev-page (- lines))))))
10224   (gnus-summary-recenter)
10225   (gnus-summary-position-point))
10226
10227 (defun gnus-summary-next-same-subject ()
10228   "Select next article which has the same subject as current one."
10229   (interactive)
10230   (gnus-set-global-variables)
10231   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10232
10233 (defun gnus-summary-prev-same-subject ()
10234   "Select previous article which has the same subject as current one."
10235   (interactive)
10236   (gnus-set-global-variables)
10237   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10238
10239 (defun gnus-summary-next-unread-same-subject ()
10240   "Select next unread article which has the same subject as current one."
10241   (interactive)
10242   (gnus-set-global-variables)
10243   (gnus-summary-next-article t (gnus-summary-article-subject)))
10244
10245 (defun gnus-summary-prev-unread-same-subject ()
10246   "Select previous unread article which has the same subject as current one."
10247   (interactive)
10248   (gnus-set-global-variables)
10249   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10250
10251 (defun gnus-summary-first-unread-article ()
10252   "Select the first unread article.
10253 Return nil if there are no unread articles."
10254   (interactive)
10255   (gnus-set-global-variables)
10256   (prog1
10257       (if (gnus-summary-first-subject t)
10258           (progn
10259             (gnus-summary-show-thread)
10260             (gnus-summary-first-subject t)
10261             (gnus-summary-display-article (gnus-summary-article-number))))
10262     (gnus-summary-position-point)))
10263
10264 (defun gnus-summary-best-unread-article ()
10265   "Select the unread article with the highest score."
10266   (interactive)
10267   (gnus-set-global-variables)
10268   (let ((best -1000000)
10269         (data gnus-newsgroup-data)
10270         article score)
10271     (while data
10272       (and (gnus-data-unread-p (car data))
10273            (> (setq score
10274                     (gnus-summary-article-score (gnus-data-number (car data))))
10275               best)
10276            (setq best score
10277                  article (gnus-data-number (car data))))
10278       (setq data (cdr data)))
10279     (if article
10280         (gnus-summary-goto-article article)
10281       (error "No unread articles"))
10282     (gnus-summary-position-point)))
10283
10284 (defun gnus-summary-last-subject ()
10285   "Go to the last displayed subject line in the group."
10286   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10287     (when article
10288       (gnus-summary-goto-subject article))))
10289
10290 (defun gnus-summary-goto-article (article &optional all-headers force)
10291   "Fetch ARTICLE and display it if it exists.
10292 If ALL-HEADERS is non-nil, no header lines are hidden."
10293   (interactive
10294    (list
10295     (string-to-int
10296      (completing-read
10297       "Article number: "
10298       (mapcar (lambda (number) (list (int-to-string number)))
10299               gnus-newsgroup-limit)))
10300     current-prefix-arg
10301     t))
10302   (prog1
10303       (if (gnus-summary-goto-subject article force)
10304           (gnus-summary-display-article article all-headers)
10305         (gnus-message 4 "Couldn't go to article %s" article) nil)
10306     (gnus-summary-position-point)))
10307
10308 (defun gnus-summary-goto-last-article ()
10309   "Go to the previously read article."
10310   (interactive)
10311   (prog1
10312       (and gnus-last-article
10313            (gnus-summary-goto-article gnus-last-article))
10314     (gnus-summary-position-point)))
10315
10316 (defun gnus-summary-pop-article (number)
10317   "Pop one article off the history and go to the previous.
10318 NUMBER articles will be popped off."
10319   (interactive "p")
10320   (let (to)
10321     (setq gnus-newsgroup-history
10322           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10323     (if to
10324         (gnus-summary-goto-article (car to))
10325       (error "Article history empty")))
10326   (gnus-summary-position-point))
10327
10328 ;; Summary commands and functions for limiting the summary buffer.
10329
10330 (defun gnus-summary-limit-to-articles (n)
10331   "Limit the summary buffer to the next N articles.
10332 If not given a prefix, use the process marked articles instead."
10333   (interactive "P")
10334   (gnus-set-global-variables)
10335   (prog1
10336       (let ((articles (gnus-summary-work-articles n)))
10337         (setq gnus-newsgroup-processable nil)
10338         (gnus-summary-limit articles))
10339     (gnus-summary-position-point)))
10340
10341 (defun gnus-summary-pop-limit (&optional total)
10342   "Restore the previous limit.
10343 If given a prefix, remove all limits."
10344   (interactive "P")
10345   (gnus-set-global-variables)
10346   (when total 
10347     (setq gnus-newsgroup-limits
10348           (list (mapcar (lambda (h) (mail-header-number h))
10349                         gnus-newsgroup-headers))))
10350   (unless gnus-newsgroup-limits
10351     (error "No limit to pop"))
10352   (prog1
10353       (gnus-summary-limit nil 'pop)
10354     (gnus-summary-position-point)))
10355
10356 (defun gnus-summary-limit-to-subject (subject &optional header)
10357   "Limit the summary buffer to articles that have subjects that match a regexp."
10358   (interactive "sRegexp: ")
10359   (unless header
10360     (setq header "subject"))
10361   (when (not (equal "" subject))
10362     (prog1
10363         (let ((articles (gnus-summary-find-matching
10364                          (or header "subject") subject 'all)))
10365           (or articles (error "Found no matches for \"%s\"" subject))
10366           (gnus-summary-limit articles))
10367       (gnus-summary-position-point))))
10368
10369 (defun gnus-summary-limit-to-author (from)
10370   "Limit the summary buffer to articles that have authors that match a regexp."
10371   (interactive "sRegexp: ")
10372   (gnus-summary-limit-to-subject from "from"))
10373
10374 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10375 (make-obsolete
10376  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10377
10378 (defun gnus-summary-limit-to-unread (&optional all)
10379   "Limit the summary buffer to articles that are not marked as read.
10380 If ALL is non-nil, limit strictly to unread articles."
10381   (interactive "P")
10382   (if all
10383       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10384     (gnus-summary-limit-to-marks
10385      ;; Concat all the marks that say that an article is read and have
10386      ;; those removed.
10387      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10388            gnus-killed-mark gnus-kill-file-mark
10389            gnus-low-score-mark gnus-expirable-mark
10390            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10391      'reverse)))
10392
10393 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10394 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10395
10396 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10397   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10398 If REVERSE, limit the summary buffer to articles that are not marked
10399 with MARKS.  MARKS can either be a string of marks or a list of marks.
10400 Returns how many articles were removed."
10401   (interactive "sMarks: ")
10402   (gnus-set-global-variables)
10403   (prog1
10404       (let ((data gnus-newsgroup-data)
10405             (marks (if (listp marks) marks
10406                      (append marks nil))) ; Transform to list.
10407             articles)
10408         (while data
10409           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10410                  (memq (gnus-data-mark (car data)) marks))
10411                (setq articles (cons (gnus-data-number (car data)) articles)))
10412           (setq data (cdr data)))
10413         (gnus-summary-limit articles))
10414     (gnus-summary-position-point)))
10415
10416 (defun gnus-summary-limit-to-score (&optional score)
10417   "Limit to articles with score at or above SCORE."
10418   (interactive "P")
10419   (gnus-set-global-variables)
10420   (setq score (if score
10421                   (prefix-numeric-value score)
10422                 (or gnus-summary-default-score 0)))
10423   (let ((data gnus-newsgroup-data)
10424         articles)
10425     (while data
10426       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10427                 score)
10428         (push (gnus-data-number (car data)) articles))
10429       (setq data (cdr data)))
10430     (prog1
10431         (gnus-summary-limit articles)
10432       (gnus-summary-position-point))))
10433
10434 (defun gnus-summary-limit-include-dormant ()
10435   "Display all the hidden articles that are marked as dormant."
10436   (interactive)
10437   (gnus-set-global-variables)
10438   (or gnus-newsgroup-dormant
10439       (error "There are no dormant articles in this group"))
10440   (prog1
10441       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10442     (gnus-summary-position-point)))
10443
10444 (defun gnus-summary-limit-exclude-dormant ()
10445   "Hide all dormant articles."
10446   (interactive)
10447   (gnus-set-global-variables)
10448   (prog1
10449       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10450     (gnus-summary-position-point)))
10451
10452 (defun gnus-summary-limit-exclude-childless-dormant ()
10453   "Hide all dormant articles that have no children."
10454   (interactive)
10455   (gnus-set-global-variables)
10456   (let ((data (gnus-data-list t))
10457         articles d children)
10458     ;; Find all articles that are either not dormant or have
10459     ;; children.
10460     (while (setq d (pop data))
10461       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10462                 (and (setq children 
10463                            (gnus-article-children (gnus-data-number d)))
10464                      (let (found)
10465                        (while children
10466                          (when (memq (car children) articles)
10467                            (setq children nil
10468                                  found t))
10469                          (pop children))
10470                        found)))
10471         (push (gnus-data-number d) articles)))
10472     ;; Do the limiting.
10473     (prog1
10474         (gnus-summary-limit articles)
10475       (gnus-summary-position-point))))
10476
10477 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10478   "Mark all unread excluded articles as read.
10479 If ALL, mark even excluded ticked and dormants as read."
10480   (interactive "P")
10481   (let ((articles (gnus-sorted-complement
10482                    (sort
10483                     (mapcar (lambda (h) (mail-header-number h))
10484                             gnus-newsgroup-headers)
10485                     '<)
10486                    (sort gnus-newsgroup-limit '<)))
10487         article)
10488     (setq gnus-newsgroup-unreads nil)
10489     (if all
10490         (setq gnus-newsgroup-dormant nil
10491               gnus-newsgroup-marked nil
10492               gnus-newsgroup-reads
10493               (nconc
10494                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10495                gnus-newsgroup-reads))
10496       (while (setq article (pop articles))
10497         (unless (or (memq article gnus-newsgroup-dormant)
10498                     (memq article gnus-newsgroup-marked))
10499           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10500
10501 (defun gnus-summary-limit (articles &optional pop)
10502   (if pop
10503       ;; We pop the previous limit off the stack and use that.
10504       (setq articles (car gnus-newsgroup-limits)
10505             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10506     ;; We use the new limit, so we push the old limit on the stack.
10507     (setq gnus-newsgroup-limits
10508           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10509   ;; Set the limit.
10510   (setq gnus-newsgroup-limit articles)
10511   (let ((total (length gnus-newsgroup-data))
10512         (data (gnus-data-find-list (gnus-summary-article-number)))
10513         found)
10514     ;; This will do all the work of generating the new summary buffer
10515     ;; according to the new limit.
10516     (gnus-summary-prepare)
10517     ;; Hide any threads, possibly.
10518     (and gnus-show-threads
10519          gnus-thread-hide-subtree
10520          (gnus-summary-hide-all-threads))
10521     ;; Try to return to the article you were at, or one in the
10522     ;; neighborhood.
10523     (if data
10524         ;; We try to find some article after the current one.
10525         (while data
10526           (and (gnus-summary-goto-subject
10527                 (gnus-data-number (car data)) nil t)
10528                (setq data nil
10529                      found t))
10530           (setq data (cdr data))))
10531     (or found
10532         ;; If there is no data, that means that we were after the last
10533         ;; article.  The same goes when we can't find any articles
10534         ;; after the current one.
10535         (progn
10536           (goto-char (point-max))
10537           (gnus-summary-find-prev)))
10538     ;; We return how many articles were removed from the summary
10539     ;; buffer as a result of the new limit.
10540     (- total (length gnus-newsgroup-data))))
10541
10542 (defsubst gnus-cut-thread (thread)
10543   "Go forwards in the thread until we find an article that we want to display."
10544   (if (eq gnus-fetch-old-headers 'some)
10545       (while (and thread
10546                   (memq (mail-header-number (car thread)) 
10547                         gnus-newsgroup-ancient)
10548                   (<= (length (cdr thread)) 1))
10549         (setq thread (cadr thread)))
10550     (while (and thread
10551                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10552                 (= (length (cdr thread)) 1))
10553       (setq thread (cadr thread))))
10554   thread)
10555
10556 (defun gnus-cut-threads (threads)
10557   "Cut off all uninteresting articles from the beginning of threads."
10558   (when (or (eq gnus-fetch-old-headers 'some)
10559             (eq gnus-build-sparse-threads 'some)
10560             (eq gnus-build-sparse-threads 'more))
10561     (let ((th threads))
10562       (while th
10563         (setcar th (gnus-cut-thread (car th)))
10564         (setq th (cdr th)))))
10565   threads)
10566
10567 (defun gnus-summary-initial-limit (&optional show-if-empty)
10568   "Figure out what the initial limit is supposed to be on group entry.
10569 This entails weeding out unwanted dormants, low-scored articles,
10570 fetch-old-headers verbiage, and so on."
10571   ;; Most groups have nothing to remove.
10572   (if (or gnus-inhibit-limiting
10573           (and (null gnus-newsgroup-dormant)
10574                (not (eq gnus-fetch-old-headers 'some))
10575                (null gnus-summary-expunge-below)
10576                (not (eq gnus-build-sparse-threads 'some))
10577                (not (eq gnus-build-sparse-threads 'more))
10578                (null gnus-thread-expunge-below)
10579                (not gnus-use-nocem)))
10580       () ; Do nothing.
10581     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10582     (setq gnus-newsgroup-limit nil)
10583     (mapatoms
10584      (lambda (node)
10585        (unless (car (symbol-value node))
10586          ;; These threads have no parents -- they are roots.
10587          (let ((nodes (cdr (symbol-value node)))
10588                thread)
10589            (while nodes
10590              (if (and gnus-thread-expunge-below
10591                       (< (gnus-thread-total-score (car nodes))
10592                          gnus-thread-expunge-below))
10593                  (gnus-expunge-thread (pop nodes))
10594                (setq thread (pop nodes))
10595                (gnus-summary-limit-children thread))))))
10596      gnus-newsgroup-dependencies)
10597     ;; If this limitation resulted in an empty group, we might
10598     ;; pop the previous limit and use it instead.
10599     (when (and (not gnus-newsgroup-limit)
10600                show-if-empty)
10601       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10602     gnus-newsgroup-limit))
10603
10604 (defun gnus-summary-limit-children (thread)
10605   "Return 1 if this subthread is visible and 0 if it is not."
10606   ;; First we get the number of visible children to this thread.  This
10607   ;; is done by recursing down the thread using this function, so this
10608   ;; will really go down to a leaf article first, before slowly
10609   ;; working its way up towards the root.
10610   (when thread
10611     (let ((children
10612            (if (cdr thread)
10613                (apply '+ (mapcar 'gnus-summary-limit-children
10614                                  (cdr thread)))
10615              0))
10616           (number (mail-header-number (car thread)))
10617           score)
10618       (if (or
10619            ;; If this article is dormant and has absolutely no visible
10620            ;; children, then this article isn't visible.
10621            (and (memq number gnus-newsgroup-dormant)
10622                 (= children 0))
10623            ;; If this is a "fetch-old-headered" and there is only one
10624            ;; visible child (or less), then we don't want this article.
10625            (and (eq gnus-fetch-old-headers 'some)
10626                 (memq number gnus-newsgroup-ancient)
10627                 (zerop children))
10628            ;; If this is a sparsely inserted article with no children,
10629            ;; we don't want it.
10630            (and (eq gnus-build-sparse-threads 'some)
10631                 (memq number gnus-newsgroup-sparse)
10632                 (zerop children))
10633            ;; If we use expunging, and this article is really
10634            ;; low-scored, then we don't want this article.
10635            (when (and gnus-summary-expunge-below
10636                       (< (setq score
10637                                (or (cdr (assq number gnus-newsgroup-scored))
10638                                    gnus-summary-default-score))
10639                          gnus-summary-expunge-below))
10640              ;; We increase the expunge-tally here, but that has
10641              ;; nothing to do with the limits, really.
10642              (incf gnus-newsgroup-expunged-tally)
10643              ;; We also mark as read here, if that's wanted.
10644              (when (and gnus-summary-mark-below
10645                         (< score gnus-summary-mark-below))
10646                (setq gnus-newsgroup-unreads
10647                      (delq number gnus-newsgroup-unreads))
10648                (if gnus-newsgroup-auto-expire
10649                    (push number gnus-newsgroup-expirable)
10650                  (push (cons number gnus-low-score-mark)
10651                        gnus-newsgroup-reads)))
10652              t)
10653            (and gnus-use-nocem
10654                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10655           ;; Nope, invisible article.
10656           0
10657         ;; Ok, this article is to be visible, so we add it to the limit
10658         ;; and return 1.
10659         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10660         1))))
10661
10662 (defun gnus-expunge-thread (thread)
10663   "Mark all articles in THREAD as read."
10664   (let* ((number (mail-header-number (car thread))))
10665     (incf gnus-newsgroup-expunged-tally)
10666     ;; We also mark as read here, if that's wanted.
10667     (setq gnus-newsgroup-unreads
10668           (delq number gnus-newsgroup-unreads))
10669     (if gnus-newsgroup-auto-expire
10670         (push number gnus-newsgroup-expirable)
10671       (push (cons number gnus-low-score-mark)
10672             gnus-newsgroup-reads)))
10673   ;; Go recursively through all subthreads.
10674   (mapcar 'gnus-expunge-thread (cdr thread)))
10675
10676 ;; Summary article oriented commands
10677
10678 (defun gnus-summary-refer-parent-article (n)
10679   "Refer parent article N times.
10680 The difference between N and the number of articles fetched is returned."
10681   (interactive "p")
10682   (gnus-set-global-variables)
10683   (while
10684       (and
10685        (> n 0)
10686        (let* ((header (gnus-summary-article-header))
10687               (ref
10688                ;; If we try to find the parent of the currently
10689                ;; displayed article, then we take a look at the actual
10690                ;; References header, since this is slightly more
10691                ;; reliable than the References field we got from the
10692                ;; server.
10693                (if (and (eq (mail-header-number header)
10694                             (cdr gnus-article-current))
10695                         (equal gnus-newsgroup-name
10696                                (car gnus-article-current)))
10697                    (save-excursion
10698                      (set-buffer gnus-original-article-buffer)
10699                      (nnheader-narrow-to-headers)
10700                      (prog1
10701                          (mail-fetch-field "references")
10702                        (widen)))
10703                  ;; It's not the current article, so we take a bet on
10704                  ;; the value we got from the server.
10705                  (mail-header-references header))))
10706          (if (setq ref (or ref (mail-header-references header)))
10707              (or (gnus-summary-refer-article (gnus-parent-id ref))
10708                  (gnus-message 1 "Couldn't find parent"))
10709            (gnus-message 1 "No references in article %d"
10710                          (gnus-summary-article-number))
10711            nil)))
10712     (setq n (1- n)))
10713   (gnus-summary-position-point)
10714   n)
10715
10716 (defun gnus-summary-refer-references ()
10717   "Fetch all articles mentioned in the References header.
10718 Return how many articles were fetched."
10719   (interactive)
10720   (gnus-set-global-variables)
10721   (let ((ref (mail-header-references (gnus-summary-article-header)))
10722         (current (gnus-summary-article-number))
10723         (n 0))
10724     ;; For each Message-ID in the References header...
10725     (while (string-match "<[^>]*>" ref)
10726       (incf n)
10727       ;; ... fetch that article.
10728       (gnus-summary-refer-article
10729        (prog1 (match-string 0 ref)
10730          (setq ref (substring ref (match-end 0))))))
10731     (gnus-summary-goto-subject current)
10732     (gnus-summary-position-point)
10733     n))
10734
10735 (defun gnus-summary-refer-article (message-id)
10736   "Fetch an article specified by MESSAGE-ID."
10737   (interactive "sMessage-ID: ")
10738   (when (and (stringp message-id)
10739              (not (zerop (length message-id))))
10740     ;; Construct the correct Message-ID if necessary.
10741     ;; Suggested by tale@pawl.rpi.edu.
10742     (unless (string-match "^<" message-id)
10743       (setq message-id (concat "<" message-id)))
10744     (unless (string-match ">$" message-id)
10745       (setq message-id (concat message-id ">")))
10746     (let ((header (car (gnus-gethash message-id
10747                                      gnus-newsgroup-dependencies))))
10748       (if header
10749           ;; The article is present in the buffer, to we just go to it.
10750           (gnus-summary-goto-article (mail-header-number header) nil t)
10751         ;; We fetch the article
10752         (let ((gnus-override-method gnus-refer-article-method)
10753               number)
10754           ;; Start the special refer-article method, if necessary.
10755           (when gnus-refer-article-method
10756             (gnus-check-server gnus-refer-article-method))
10757           ;; Fetch the header, and display the article.
10758           (if (setq number (gnus-summary-insert-subject message-id))
10759               (gnus-summary-select-article nil nil nil number)
10760             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10761
10762 (defun gnus-summary-enter-digest-group (&optional force)
10763   "Enter a digest group based on the current article."
10764   (interactive "P")
10765   (gnus-set-global-variables)
10766   (gnus-summary-select-article)
10767   (let ((name (format "%s-%d"
10768                       (gnus-group-prefixed-name
10769                        gnus-newsgroup-name (list 'nndoc ""))
10770                       gnus-current-article))
10771         (ogroup gnus-newsgroup-name)
10772         (case-fold-search t)
10773         (buf (current-buffer))
10774         dig)
10775     (save-excursion
10776       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10777       (insert-buffer-substring gnus-original-article-buffer)
10778       (narrow-to-region
10779        (goto-char (point-min))
10780        (or (search-forward "\n\n" nil t) (point)))
10781       (goto-char (point-min))
10782       (delete-matching-lines "^\\(Path\\):\\|^From ")
10783       (widen))
10784     (unwind-protect
10785         (if (gnus-group-read-ephemeral-group
10786              name `(nndoc ,name (nndoc-address
10787                                  ,(get-buffer dig))
10788                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10789             ;; Make all postings to this group go to the parent group.
10790             (nconc (gnus-info-params (gnus-get-info name))
10791                    (list (cons 'to-group ogroup)))
10792           ;; Couldn't select this doc group.
10793           (switch-to-buffer buf)
10794           (gnus-set-global-variables)
10795           (gnus-configure-windows 'summary)
10796           (gnus-message 3 "Article couldn't be entered?"))
10797       (kill-buffer dig))))
10798
10799 (defun gnus-summary-isearch-article (&optional regexp-p)
10800   "Do incremental search forward on the current article.
10801 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10802   (interactive "P")
10803   (gnus-set-global-variables)
10804   (gnus-summary-select-article)
10805   (gnus-configure-windows 'article)
10806   (gnus-eval-in-buffer-window
10807    gnus-article-buffer
10808    (goto-char (point-min))
10809    (isearch-forward regexp-p)))
10810
10811 (defun gnus-summary-search-article-forward (regexp &optional backward)
10812   "Search for an article containing REGEXP forward.
10813 If BACKWARD, search backward instead."
10814   (interactive
10815    (list (read-string
10816           (format "Search article %s (regexp%s): "
10817                   (if current-prefix-arg "backward" "forward")
10818                   (if gnus-last-search-regexp
10819                       (concat ", default " gnus-last-search-regexp)
10820                     "")))
10821          current-prefix-arg))
10822   (gnus-set-global-variables)
10823   (if (string-equal regexp "")
10824       (setq regexp (or gnus-last-search-regexp ""))
10825     (setq gnus-last-search-regexp regexp))
10826   (if (gnus-summary-search-article regexp backward)
10827       (gnus-article-set-window-start
10828        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10829     (error "Search failed: \"%s\"" regexp)))
10830
10831 (defun gnus-summary-search-article-backward (regexp)
10832   "Search for an article containing REGEXP backward."
10833   (interactive
10834    (list (read-string
10835           (format "Search article backward (regexp%s): "
10836                   (if gnus-last-search-regexp
10837                       (concat ", default " gnus-last-search-regexp)
10838                     "")))))
10839   (gnus-summary-search-article-forward regexp 'backward))
10840
10841 (defun gnus-summary-search-article (regexp &optional backward)
10842   "Search for an article containing REGEXP.
10843 Optional argument BACKWARD means do search for backward.
10844 gnus-select-article-hook is not called during the search."
10845   (let ((gnus-select-article-hook nil)  ;Disable hook.
10846         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10847         (re-search
10848          (if backward
10849              (function re-search-backward) (function re-search-forward)))
10850         (found nil)
10851         (last nil))
10852     ;; Hidden thread subtrees must be searched for ,too.
10853     (gnus-summary-show-all-threads)
10854     ;; First of all, search current article.
10855     ;; We don't want to read article again from NNTP server nor reset
10856     ;; current point.
10857     (gnus-summary-select-article)
10858     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10859     (setq last gnus-current-article)
10860     (gnus-eval-in-buffer-window
10861      gnus-article-buffer
10862      (save-restriction
10863        (widen)
10864        ;; Begin search from current point.
10865        (setq found (funcall re-search regexp nil t))))
10866     ;; Then search next articles.
10867     (while (and (not found)
10868                 (gnus-summary-display-article
10869                  (if backward (gnus-summary-find-prev)
10870                    (gnus-summary-find-next))))
10871       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10872       (gnus-eval-in-buffer-window
10873        gnus-article-buffer
10874        (save-restriction
10875          (widen)
10876          (goto-char (if backward (point-max) (point-min)))
10877          (setq found (funcall re-search regexp nil t)))))
10878     (message "")
10879     ;; Adjust article pointer.
10880     (or (eq last gnus-current-article)
10881         (setq gnus-last-article last))
10882     ;; Return T if found such article.
10883     found))
10884
10885 (defun gnus-summary-find-matching (header regexp &optional backward unread
10886                                           not-case-fold)
10887   "Return a list of all articles that match REGEXP on HEADER.
10888 The search stars on the current article and goes forwards unless
10889 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10890 If UNREAD is non-nil, only unread articles will
10891 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10892 in the comparisons."
10893   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10894                 (gnus-data-find-list
10895                  (gnus-summary-article-number) (gnus-data-list backward))))
10896         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10897         (case-fold-search (not not-case-fold))
10898         articles d)
10899     (or (fboundp (intern (concat "mail-header-" header)))
10900         (error "%s is not a valid header" header))
10901     (while data
10902       (setq d (car data))
10903       (and (or (not unread)             ; We want all articles...
10904                (gnus-data-unread-p d))  ; Or just unreads.
10905            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10906            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10907            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10908       (setq data (cdr data)))
10909     (nreverse articles)))
10910
10911 (defun gnus-summary-execute-command (header regexp command &optional backward)
10912   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10913 If HEADER is an empty string (or nil), the match is done on the entire
10914 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10915   (interactive
10916    (list (let ((completion-ignore-case t))
10917            (completing-read
10918             "Header name: "
10919             (mapcar (lambda (string) (list string))
10920                     '("Number" "Subject" "From" "Lines" "Date"
10921                       "Message-ID" "Xref" "References" "Body"))
10922             nil 'require-match))
10923          (read-string "Regexp: ")
10924          (read-key-sequence "Command: ")
10925          current-prefix-arg))
10926   (when (equal header "Body")
10927     (setq header ""))
10928   (gnus-set-global-variables)
10929   ;; Hidden thread subtrees must be searched as well.
10930   (gnus-summary-show-all-threads)
10931   ;; We don't want to change current point nor window configuration.
10932   (save-excursion
10933     (save-window-excursion
10934       (gnus-message 6 "Executing %s..." (key-description command))
10935       ;; We'd like to execute COMMAND interactively so as to give arguments.
10936       (gnus-execute header regexp
10937                     `(lambda () (call-interactively ',(key-binding command)))
10938                     backward)
10939       (gnus-message 6 "Executing %s...done" (key-description command)))))
10940
10941 (defun gnus-summary-beginning-of-article ()
10942   "Scroll the article back to the beginning."
10943   (interactive)
10944   (gnus-set-global-variables)
10945   (gnus-summary-select-article)
10946   (gnus-configure-windows 'article)
10947   (gnus-eval-in-buffer-window
10948    gnus-article-buffer
10949    (widen)
10950    (goto-char (point-min))
10951    (and gnus-break-pages (gnus-narrow-to-page))))
10952
10953 (defun gnus-summary-end-of-article ()
10954   "Scroll to the end of the article."
10955   (interactive)
10956   (gnus-set-global-variables)
10957   (gnus-summary-select-article)
10958   (gnus-configure-windows 'article)
10959   (gnus-eval-in-buffer-window
10960    gnus-article-buffer
10961    (widen)
10962    (goto-char (point-max))
10963    (recenter -3)
10964    (and gnus-break-pages (gnus-narrow-to-page))))
10965
10966 (defun gnus-summary-show-article (&optional arg)
10967   "Force re-fetching of the current article.
10968 If ARG (the prefix) is non-nil, show the raw article without any
10969 article massaging functions being run."
10970   (interactive "P")
10971   (gnus-set-global-variables)
10972   (if (not arg)
10973       ;; Select the article the normal way.
10974       (gnus-summary-select-article nil 'force)
10975     ;; Bind the article treatment functions to nil.
10976     (let ((gnus-have-all-headers t)
10977           gnus-article-display-hook
10978           gnus-article-prepare-hook
10979           gnus-visual)
10980       (gnus-summary-select-article nil 'force)))
10981 ;  (gnus-configure-windows 'article)
10982   (gnus-summary-position-point))
10983
10984 (defun gnus-summary-verbose-headers (&optional arg)
10985   "Toggle permanent full header display.
10986 If ARG is a positive number, turn header display on.
10987 If ARG is a negative number, turn header display off."
10988   (interactive "P")
10989   (gnus-set-global-variables)
10990   (gnus-summary-toggle-header arg)
10991   (setq gnus-show-all-headers
10992         (cond ((or (not (numberp arg))
10993                    (zerop arg))
10994                (not gnus-show-all-headers))
10995               ((natnump arg)
10996                t))))
10997
10998 (defun gnus-summary-toggle-header (&optional arg)
10999   "Show the headers if they are hidden, or hide them if they are shown.
11000 If ARG is a positive number, show the entire header.
11001 If ARG is a negative number, hide the unwanted header lines."
11002   (interactive "P")
11003   (gnus-set-global-variables)
11004   (save-excursion
11005     (set-buffer gnus-article-buffer)
11006     (let* ((buffer-read-only nil)
11007            (inhibit-point-motion-hooks t)
11008            (hidden (text-property-any
11009                     (goto-char (point-min)) (search-forward "\n\n")
11010                     'invisible t))
11011            e)
11012       (goto-char (point-min))
11013       (when (search-forward "\n\n" nil t)
11014         (delete-region (point-min) (1- (point))))
11015       (goto-char (point-min))
11016       (save-excursion
11017         (set-buffer gnus-original-article-buffer)
11018         (goto-char (point-min))
11019         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11020       (insert-buffer-substring gnus-original-article-buffer 1 e)
11021       (let ((gnus-inhibit-hiding t))
11022         (run-hooks 'gnus-article-display-hook))
11023       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11024           (gnus-article-hide-headers)))))
11025
11026 (defun gnus-summary-show-all-headers ()
11027   "Make all header lines visible."
11028   (interactive)
11029   (gnus-set-global-variables)
11030   (gnus-article-show-all-headers))
11031
11032 (defun gnus-summary-toggle-mime (&optional arg)
11033   "Toggle MIME processing.
11034 If ARG is a positive number, turn MIME processing on."
11035   (interactive "P")
11036   (gnus-set-global-variables)
11037   (setq gnus-show-mime
11038         (if (null arg) (not gnus-show-mime)
11039           (> (prefix-numeric-value arg) 0)))
11040   (gnus-summary-select-article t 'force))
11041
11042 (defun gnus-summary-caesar-message (&optional arg)
11043   "Caesar rotate the current article by 13.
11044 The numerical prefix specifies how manu places to rotate each letter
11045 forward."
11046   (interactive "P")
11047   (gnus-set-global-variables)
11048   (gnus-summary-select-article)
11049   (let ((mail-header-separator ""))
11050     (gnus-eval-in-buffer-window
11051      gnus-article-buffer
11052      (save-restriction
11053        (widen)
11054        (let ((start (window-start)))
11055          (news-caesar-buffer-body arg)
11056          (set-window-start (get-buffer-window (current-buffer)) start))))))
11057
11058 (defun gnus-summary-stop-page-breaking ()
11059   "Stop page breaking in the current article."
11060   (interactive)
11061   (gnus-set-global-variables)
11062   (gnus-summary-select-article)
11063   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11064
11065 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11066   "Move the current article to a different newsgroup.
11067 If N is a positive number, move the N next articles.
11068 If N is a negative number, move the N previous articles.
11069 If N is nil and any articles have been marked with the process mark,
11070 move those articles instead.
11071 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11072 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11073 re-spool using this method.
11074
11075 For this function to work, both the current newsgroup and the
11076 newsgroup that you want to move to have to support the `request-move'
11077 and `request-accept' functions."
11078   (interactive "P")
11079   (unless action (setq action 'move))
11080   (gnus-set-global-variables)
11081   ;; Check whether the source group supports the required functions.
11082   (cond ((and (eq action 'move)
11083               (not (gnus-check-backend-function
11084                     'request-move-article gnus-newsgroup-name)))
11085          (error "The current group does not support article moving"))
11086         ((and (eq action 'crosspost)
11087               (not (gnus-check-backend-function
11088                     'request-replace-article gnus-newsgroup-name)))
11089          (error "The current group does not support article editing")))
11090   (let ((articles (gnus-summary-work-articles n))
11091         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11092         (names '((move "move" "Moving")
11093                  (copy "copy" "Copying")
11094                  (crosspost "crosspost" "Crossposting")))
11095         (copy-buf (save-excursion
11096                     (nnheader-set-temp-buffer " *copy article*")))
11097         art-group to-method new-xref article)
11098     (unless (assq action names)
11099       (error "Unknown action %s" action))
11100     ;; Read the newsgroup name.
11101     (when (and (not to-newsgroup)
11102                (not select-method))
11103       (setq to-newsgroup
11104             (gnus-read-move-group-name
11105              (cadr (assq action names))
11106              gnus-current-move-group articles prefix))
11107       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11108     (setq to-method (if select-method (list select-method "")
11109                       (gnus-find-method-for-group to-newsgroup)))
11110     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11111     ;;(error "Can't %s to the same group you're already in" action))
11112     ;; Check the method we are to move this article to...
11113     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11114         (error "%s does not support article copying" (car to-method)))
11115     (or (gnus-check-server to-method)
11116         (error "Can't open server %s" (car to-method)))
11117     (gnus-message 6 "%s to %s: %s..."
11118                   (caddr (assq action names))
11119                   (or select-method to-newsgroup) articles)
11120     (while articles
11121       (setq article (pop articles))
11122       (setq
11123        art-group
11124        (cond
11125         ;; Move the article.
11126         ((eq action 'move)
11127          (gnus-request-move-article
11128           article                       ; Article to move
11129           gnus-newsgroup-name           ; From newsgrouo
11130           (nth 1 (gnus-find-method-for-group
11131                   gnus-newsgroup-name)) ; Server
11132           (list 'gnus-request-accept-article
11133                 (if select-method
11134                     (list 'quote select-method)
11135                   to-newsgroup)
11136                 (not articles))         ; Accept form
11137           (not articles)))              ; Only save nov last time
11138         ;; Copy the article.
11139         ((eq action 'copy)
11140          (save-excursion
11141            (set-buffer copy-buf)
11142            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11143            (gnus-request-accept-article
11144             (if select-method select-method to-newsgroup)
11145             (not articles))))
11146         ;; Crosspost the article.
11147         ((eq action 'crosspost)
11148          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11149            (setq new-xref (concat gnus-newsgroup-name ":" article))
11150            (if (and xref (not (string= xref "")))
11151                (progn
11152                  (when (string-match "^Xref: " xref)
11153                    (setq xref (substring xref (match-end 0))))
11154                  (setq new-xref (concat xref " " new-xref)))
11155              (setq new-xref (concat (system-name) " " new-xref)))
11156            (save-excursion
11157              (set-buffer copy-buf)
11158              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11159              (nnheader-replace-header "xref" new-xref)
11160              (gnus-request-accept-article
11161               (if select-method select-method to-newsgroup)
11162               (not articles)))))))
11163       (if (not art-group)
11164           (gnus-message 1 "Couldn't %s article %s"
11165                         (cadr (assq action names)) article)
11166         (let* ((entry
11167                 (or
11168                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11169                  (gnus-gethash
11170                   (gnus-group-prefixed-name
11171                    (car art-group)
11172                    (if select-method (list select-method "")
11173                      (gnus-find-method-for-group to-newsgroup)))
11174                   gnus-newsrc-hashtb)))
11175                (info (nth 2 entry)))
11176           ;; Update the group that has been moved to.
11177           (when (and info
11178                      (memq action '(move copy)))
11179             (unless (memq article gnus-newsgroup-unreads)
11180               (gnus-info-set-read
11181                info (gnus-add-to-range (gnus-info-read info)
11182                                        (list (cdr art-group)))))
11183
11184             ;; Copy any marks over to the new group.
11185             (let ((marks gnus-article-mark-lists)
11186                   (to-article (cdr art-group)))
11187
11188               ;; See whether the article is to be put in the cache.
11189               (when gnus-use-cache
11190                 (gnus-cache-possibly-enter-article
11191                  (gnus-info-group info) to-article
11192                  (let ((header (copy-sequence
11193                                 (gnus-summary-article-header article))))
11194                    (mail-header-set-number header to-article)
11195                    header)
11196                  (memq article gnus-newsgroup-marked)
11197                  (memq article gnus-newsgroup-dormant)
11198                  (memq article gnus-newsgroup-unreads)))
11199
11200               (while marks
11201                 (when (memq article (symbol-value
11202                                      (intern (format "gnus-newsgroup-%s"
11203                                                      (caar marks)))))
11204                   (gnus-add-marked-articles
11205                    (gnus-info-group info) (cdar marks)
11206                    (list to-article) info))
11207                 (setq marks (cdr marks)))))
11208
11209           ;; Update the Xref header in this article to point to
11210           ;; the new crossposted article we have just created.
11211           (when (eq action 'crosspost)
11212             (save-excursion
11213               (set-buffer copy-buf)
11214               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11215               (nnheader-replace-header
11216                "xref" (concat new-xref " " (gnus-group-prefixed-name
11217                                             (car art-group) to-method)
11218                               ":" (cdr art-group)))
11219               (gnus-request-replace-article
11220                article gnus-newsgroup-name (current-buffer)))))
11221
11222         (gnus-summary-goto-subject article)
11223         (when (eq action 'move)
11224           (gnus-summary-mark-article article gnus-canceled-mark)))
11225       (gnus-summary-remove-process-mark article))
11226     (gnus-kill-buffer copy-buf)
11227     (gnus-summary-position-point)
11228     (gnus-set-mode-line 'summary)))
11229
11230 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11231   "Move the current article to a different newsgroup.
11232 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11233 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11234 re-spool using this method."
11235   (interactive "P")
11236   (gnus-summary-move-article n nil select-method 'copy))
11237
11238 (defun gnus-summary-crosspost-article (&optional n)
11239   "Crosspost the current article to some other group."
11240   (interactive "P")
11241   (gnus-summary-move-article n nil nil 'crosspost))
11242
11243 (defun gnus-summary-respool-article (&optional n respool-method)
11244   "Respool the current article.
11245 The article will be squeezed through the mail spooling process again,
11246 which means that it will be put in some mail newsgroup or other
11247 depending on `nnmail-split-methods'.
11248 If N is a positive number, respool the N next articles.
11249 If N is a negative number, respool the N previous articles.
11250 If N is nil and any articles have been marked with the process mark,
11251 respool those articles instead.
11252
11253 Respooling can be done both from mail groups and \"real\" newsgroups.
11254 In the former case, the articles in question will be moved from the
11255 current group into whatever groups they are destined to.  In the
11256 latter case, they will be copied into the relevant groups."
11257   (interactive "P")
11258   (gnus-set-global-variables)
11259   (let ((respool-methods (gnus-methods-using 'respool))
11260         (methname
11261          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11262     (or respool-method
11263         (setq respool-method
11264               (completing-read
11265                "What method do you want to use when respooling? "
11266                respool-methods nil t methname)))
11267     (or (string= respool-method "")
11268         (if (assoc (symbol-name
11269                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11270                    respool-methods)
11271             (gnus-summary-move-article n nil (intern respool-method))
11272           (gnus-summary-copy-article n nil (intern respool-method))))))
11273
11274 (defun gnus-summary-import-article (file)
11275   "Import a random file into a mail newsgroup."
11276   (interactive "fImport file: ")
11277   (gnus-set-global-variables)
11278   (let ((group gnus-newsgroup-name)
11279         (now (current-time))
11280         atts lines)
11281     (or (gnus-check-backend-function 'request-accept-article group)
11282         (error "%s does not support article importing" group))
11283     (or (file-readable-p file)
11284         (not (file-regular-p file))
11285         (error "Can't read %s" file))
11286     (save-excursion
11287       (set-buffer (get-buffer-create " *import file*"))
11288       (buffer-disable-undo (current-buffer))
11289       (erase-buffer)
11290       (insert-file-contents file)
11291       (goto-char (point-min))
11292       (unless (nnheader-article-p)
11293         ;; This doesn't look like an article, so we fudge some headers.
11294         (setq atts (file-attributes file)
11295               lines (count-lines (point-min) (point-max)))
11296         (insert "From: " (read-string "From: ") "\n"
11297                 "Subject: " (read-string "Subject: ") "\n"
11298                 "Date: " (timezone-make-date-arpa-standard
11299                           (current-time-string (nth 5 atts))
11300                           (current-time-zone now)
11301                           (current-time-zone now)) "\n"
11302                 "Message-ID: " (gnus-inews-message-id) "\n"
11303                 "Lines: " (int-to-string lines) "\n"
11304                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11305       (gnus-request-accept-article group t)
11306       (kill-buffer (current-buffer)))))
11307
11308 (defun gnus-summary-expire-articles ()
11309   "Expire all articles that are marked as expirable in the current group."
11310   (interactive)
11311   (gnus-set-global-variables)
11312   (when (gnus-check-backend-function
11313          'request-expire-articles gnus-newsgroup-name)
11314     ;; This backend supports expiry.
11315     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11316            (expirable (if total
11317                           (gnus-list-of-read-articles gnus-newsgroup-name)
11318                         (setq gnus-newsgroup-expirable
11319                               (sort gnus-newsgroup-expirable '<))))
11320            (expiry-wait (gnus-group-get-parameter
11321                          gnus-newsgroup-name 'expiry-wait))
11322            es)
11323       (when expirable
11324         ;; There are expirable articles in this group, so we run them
11325         ;; through the expiry process.
11326         (gnus-message 6 "Expiring articles...")
11327         ;; The list of articles that weren't expired is returned.
11328         (if expiry-wait
11329             (let ((nnmail-expiry-wait-function nil)
11330                   (nnmail-expiry-wait expiry-wait))
11331               (setq es (gnus-request-expire-articles
11332                         expirable gnus-newsgroup-name)))
11333           (setq es (gnus-request-expire-articles
11334                     expirable gnus-newsgroup-name)))
11335         (or total (setq gnus-newsgroup-expirable es))
11336         ;; We go through the old list of expirable, and mark all
11337         ;; really expired articles as nonexistent.
11338         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11339           (let ((gnus-use-cache nil))
11340             (while expirable
11341               (unless (memq (car expirable) es)
11342                 (when (gnus-data-find (car expirable))
11343                   (gnus-summary-mark-article
11344                    (car expirable) gnus-canceled-mark)))
11345               (setq expirable (cdr expirable)))))
11346         (gnus-message 6 "Expiring articles...done")))))
11347
11348 (defun gnus-summary-expire-articles-now ()
11349   "Expunge all expirable articles in the current group.
11350 This means that *all* articles that are marked as expirable will be
11351 deleted forever, right now."
11352   (interactive)
11353   (gnus-set-global-variables)
11354   (or gnus-expert-user
11355       (gnus-y-or-n-p
11356        "Are you really, really, really sure you want to expunge? ")
11357       (error "Phew!"))
11358   (let ((nnmail-expiry-wait 'immediate)
11359         (nnmail-expiry-wait-function nil))
11360     (gnus-summary-expire-articles)))
11361
11362 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11363 (defun gnus-summary-delete-article (&optional n)
11364   "Delete the N next (mail) articles.
11365 This command actually deletes articles.  This is not a marking
11366 command.  The article will disappear forever from your life, never to
11367 return.
11368 If N is negative, delete backwards.
11369 If N is nil and articles have been marked with the process mark,
11370 delete these instead."
11371   (interactive "P")
11372   (gnus-set-global-variables)
11373   (or (gnus-check-backend-function 'request-expire-articles
11374                                    gnus-newsgroup-name)
11375       (error "The current newsgroup does not support article deletion."))
11376   ;; Compute the list of articles to delete.
11377   (let ((articles (gnus-summary-work-articles n))
11378         not-deleted)
11379     (if (and gnus-novice-user
11380              (not (gnus-y-or-n-p
11381                    (format "Do you really want to delete %s forever? "
11382                            (if (> (length articles) 1) "these articles"
11383                              "this article")))))
11384         ()
11385       ;; Delete the articles.
11386       (setq not-deleted (gnus-request-expire-articles
11387                          articles gnus-newsgroup-name 'force))
11388       (while articles
11389         (gnus-summary-remove-process-mark (car articles))
11390         ;; The backend might not have been able to delete the article
11391         ;; after all.
11392         (or (memq (car articles) not-deleted)
11393             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11394         (setq articles (cdr articles))))
11395     (gnus-summary-position-point)
11396     (gnus-set-mode-line 'summary)
11397     not-deleted))
11398
11399 (defun gnus-summary-edit-article (&optional force)
11400   "Enter into a buffer and edit the current article.
11401 This will have permanent effect only in mail groups.
11402 If FORCE is non-nil, allow editing of articles even in read-only
11403 groups."
11404   (interactive "P")
11405   (save-excursion
11406     (set-buffer gnus-summary-buffer)
11407     (gnus-set-global-variables)
11408     (when (and (not force)
11409                (gnus-group-read-only-p))
11410       (error "The current newsgroup does not support article editing."))
11411     (gnus-summary-select-article t nil t)
11412     (gnus-configure-windows 'article)
11413     (select-window (get-buffer-window gnus-article-buffer))
11414     (gnus-message 6 "C-c C-c to end edits")
11415     (setq buffer-read-only nil)
11416     (text-mode)
11417     (use-local-map (copy-keymap (current-local-map)))
11418     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11419     (buffer-enable-undo)
11420     (widen)
11421     (goto-char (point-min))
11422     (search-forward "\n\n" nil t)))
11423
11424 (defun gnus-summary-edit-article-done ()
11425   "Make edits to the current article permanent."
11426   (interactive)
11427   (if (gnus-group-read-only-p)
11428       (progn
11429         (gnus-summary-edit-article-postpone)
11430         (gnus-message
11431          1 "The current newsgroup does not support article editing.")
11432         (ding))
11433     (let ((buf (format "%s" (buffer-string))))
11434       (erase-buffer)
11435       (insert buf)
11436       (if (not (gnus-request-replace-article
11437                 (cdr gnus-article-current) (car gnus-article-current)
11438                 (current-buffer)))
11439           (error "Couldn't replace article.")
11440         (gnus-article-mode)
11441         (use-local-map gnus-article-mode-map)
11442         (setq buffer-read-only t)
11443         (buffer-disable-undo (current-buffer))
11444         (gnus-configure-windows 'summary)
11445         (gnus-summary-update-article (cdr gnus-article-current))
11446         (when gnus-use-cache
11447           (gnus-cache-update-article 
11448            (cdr gnus-article-current) (car gnus-article-current))))
11449       (run-hooks 'gnus-article-display-hook)
11450       (and (gnus-visual-p 'summary-highlight 'highlight)
11451            (run-hooks 'gnus-visual-mark-article-hook)))))
11452
11453 (defun gnus-summary-edit-article-postpone ()
11454   "Postpone changes to the current article."
11455   (interactive)
11456   (gnus-article-mode)
11457   (use-local-map gnus-article-mode-map)
11458   (setq buffer-read-only t)
11459   (buffer-disable-undo (current-buffer))
11460   (gnus-configure-windows 'summary)
11461   (and (gnus-visual-p 'summary-highlight 'highlight)
11462        (run-hooks 'gnus-visual-mark-article-hook)))
11463
11464 (defun gnus-summary-respool-query ()
11465   "Query where the respool algorithm would put this article."
11466   (interactive)
11467   (gnus-set-global-variables)
11468   (gnus-summary-select-article)
11469   (save-excursion
11470     (set-buffer gnus-article-buffer)
11471     (save-restriction
11472       (goto-char (point-min))
11473       (search-forward "\n\n")
11474       (narrow-to-region (point-min) (point))
11475       (pp-eval-expression
11476        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11477
11478 ;; Summary score commands.
11479
11480 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11481
11482 (defun gnus-summary-raise-score (n)
11483   "Raise the score of the current article by N."
11484   (interactive "p")
11485   (gnus-set-global-variables)
11486   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11487
11488 (defun gnus-summary-set-score (n)
11489   "Set the score of the current article to N."
11490   (interactive "p")
11491   (gnus-set-global-variables)
11492   (save-excursion
11493     (gnus-summary-show-thread)
11494     (let ((buffer-read-only nil))
11495       ;; Set score.
11496       (gnus-summary-update-mark
11497        (if (= n (or gnus-summary-default-score 0)) ? 
11498          (if (< n (or gnus-summary-default-score 0))
11499              gnus-score-below-mark gnus-score-over-mark)) 'score))
11500     (let* ((article (gnus-summary-article-number))
11501            (score (assq article gnus-newsgroup-scored)))
11502       (if score (setcdr score n)
11503         (setq gnus-newsgroup-scored
11504               (cons (cons article n) gnus-newsgroup-scored))))
11505     (gnus-summary-update-line)))
11506
11507 (defun gnus-summary-current-score ()
11508   "Return the score of the current article."
11509   (interactive)
11510   (gnus-set-global-variables)
11511   (message "%s" (gnus-summary-article-score)))
11512
11513 ;; Summary marking commands.
11514
11515 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11516   "Mark articles which has the same subject as read, and then select the next.
11517 If UNMARK is positive, remove any kind of mark.
11518 If UNMARK is negative, tick articles."
11519   (interactive "P")
11520   (gnus-set-global-variables)
11521   (if unmark
11522       (setq unmark (prefix-numeric-value unmark)))
11523   (let ((count
11524          (gnus-summary-mark-same-subject
11525           (gnus-summary-article-subject) unmark)))
11526     ;; Select next unread article.  If auto-select-same mode, should
11527     ;; select the first unread article.
11528     (gnus-summary-next-article t (and gnus-auto-select-same
11529                                       (gnus-summary-article-subject)))
11530     (gnus-message 7 "%d article%s marked as %s"
11531                   count (if (= count 1) " is" "s are")
11532                   (if unmark "unread" "read"))))
11533
11534 (defun gnus-summary-kill-same-subject (&optional unmark)
11535   "Mark articles which has the same subject as read.
11536 If UNMARK is positive, remove any kind of mark.
11537 If UNMARK is negative, tick articles."
11538   (interactive "P")
11539   (gnus-set-global-variables)
11540   (if unmark
11541       (setq unmark (prefix-numeric-value unmark)))
11542   (let ((count
11543          (gnus-summary-mark-same-subject
11544           (gnus-summary-article-subject) unmark)))
11545     ;; If marked as read, go to next unread subject.
11546     (if (null unmark)
11547         ;; Go to next unread subject.
11548         (gnus-summary-next-subject 1 t))
11549     (gnus-message 7 "%d articles are marked as %s"
11550                   count (if unmark "unread" "read"))))
11551
11552 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11553   "Mark articles with same SUBJECT as read, and return marked number.
11554 If optional argument UNMARK is positive, remove any kinds of marks.
11555 If optional argument UNMARK is negative, mark articles as unread instead."
11556   (let ((count 1))
11557     (save-excursion
11558       (cond
11559        ((null unmark)                   ; Mark as read.
11560         (while (and
11561                 (progn
11562                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11563                   (gnus-summary-show-thread) t)
11564                 (gnus-summary-find-subject subject))
11565           (setq count (1+ count))))
11566        ((> unmark 0)                    ; Tick.
11567         (while (and
11568                 (progn
11569                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11570                   (gnus-summary-show-thread) t)
11571                 (gnus-summary-find-subject subject))
11572           (setq count (1+ count))))
11573        (t                               ; Mark as unread.
11574         (while (and
11575                 (progn
11576                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11577                   (gnus-summary-show-thread) t)
11578                 (gnus-summary-find-subject subject))
11579           (setq count (1+ count)))))
11580       (gnus-set-mode-line 'summary)
11581       ;; Return the number of marked articles.
11582       count)))
11583
11584 (defun gnus-summary-mark-as-processable (n &optional unmark)
11585   "Set the process mark on the next N articles.
11586 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11587 the process mark instead.  The difference between N and the actual
11588 number of articles marked is returned."
11589   (interactive "p")
11590   (gnus-set-global-variables)
11591   (let ((backward (< n 0))
11592         (n (abs n)))
11593     (while (and
11594             (> n 0)
11595             (if unmark
11596                 (gnus-summary-remove-process-mark
11597                  (gnus-summary-article-number))
11598               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11599             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11600       (setq n (1- n)))
11601     (if (/= 0 n) (gnus-message 7 "No more articles"))
11602     (gnus-summary-recenter)
11603     (gnus-summary-position-point)
11604     n))
11605
11606 (defun gnus-summary-unmark-as-processable (n)
11607   "Remove the process mark from the next N articles.
11608 If N is negative, mark backward instead.  The difference between N and
11609 the actual number of articles marked is returned."
11610   (interactive "p")
11611   (gnus-set-global-variables)
11612   (gnus-summary-mark-as-processable n t))
11613
11614 (defun gnus-summary-unmark-all-processable ()
11615   "Remove the process mark from all articles."
11616   (interactive)
11617   (gnus-set-global-variables)
11618   (save-excursion
11619     (while gnus-newsgroup-processable
11620       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11621   (gnus-summary-position-point))
11622
11623 (defun gnus-summary-mark-as-expirable (n)
11624   "Mark N articles forward as expirable.
11625 If N is negative, mark backward instead.  The difference between N and
11626 the actual number of articles marked is returned."
11627   (interactive "p")
11628   (gnus-set-global-variables)
11629   (gnus-summary-mark-forward n gnus-expirable-mark))
11630
11631 (defun gnus-summary-mark-article-as-replied (article)
11632   "Mark ARTICLE replied and update the summary line."
11633   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11634   (let ((buffer-read-only nil))
11635     (when (gnus-summary-goto-subject article)
11636       (gnus-summary-update-secondary-mark article))))
11637
11638 (defun gnus-summary-set-bookmark (article)
11639   "Set a bookmark in current article."
11640   (interactive (list (gnus-summary-article-number)))
11641   (gnus-set-global-variables)
11642   (if (or (not (get-buffer gnus-article-buffer))
11643           (not gnus-current-article)
11644           (not gnus-article-current)
11645           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11646       (error "No current article selected"))
11647   ;; Remove old bookmark, if one exists.
11648   (let ((old (assq article gnus-newsgroup-bookmarks)))
11649     (if old (setq gnus-newsgroup-bookmarks
11650                   (delq old gnus-newsgroup-bookmarks))))
11651   ;; Set the new bookmark, which is on the form
11652   ;; (article-number . line-number-in-body).
11653   (setq gnus-newsgroup-bookmarks
11654         (cons
11655          (cons article
11656                (save-excursion
11657                  (set-buffer gnus-article-buffer)
11658                  (count-lines
11659                   (min (point)
11660                        (save-excursion
11661                          (goto-char (point-min))
11662                          (search-forward "\n\n" nil t)
11663                          (point)))
11664                   (point))))
11665          gnus-newsgroup-bookmarks))
11666   (gnus-message 6 "A bookmark has been added to the current article."))
11667
11668 (defun gnus-summary-remove-bookmark (article)
11669   "Remove the bookmark from the current article."
11670   (interactive (list (gnus-summary-article-number)))
11671   (gnus-set-global-variables)
11672   ;; Remove old bookmark, if one exists.
11673   (let ((old (assq article gnus-newsgroup-bookmarks)))
11674     (if old
11675         (progn
11676           (setq gnus-newsgroup-bookmarks
11677                 (delq old gnus-newsgroup-bookmarks))
11678           (gnus-message 6 "Removed bookmark."))
11679       (gnus-message 6 "No bookmark in current article."))))
11680
11681 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11682 (defun gnus-summary-mark-as-dormant (n)
11683   "Mark N articles forward as dormant.
11684 If N is negative, mark backward instead.  The difference between N and
11685 the actual number of articles marked is returned."
11686   (interactive "p")
11687   (gnus-set-global-variables)
11688   (gnus-summary-mark-forward n gnus-dormant-mark))
11689
11690 (defun gnus-summary-set-process-mark (article)
11691   "Set the process mark on ARTICLE and update the summary line."
11692   (setq gnus-newsgroup-processable
11693         (cons article
11694               (delq article gnus-newsgroup-processable)))
11695   (when (gnus-summary-goto-subject article)
11696     (gnus-summary-show-thread)
11697     (gnus-summary-update-secondary-mark article)))
11698
11699 (defun gnus-summary-remove-process-mark (article)
11700   "Remove the process mark from ARTICLE and update the summary line."
11701   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11702   (when (gnus-summary-goto-subject article)
11703     (gnus-summary-show-thread)
11704     (gnus-summary-update-secondary-mark article)))
11705
11706 (defun gnus-summary-set-saved-mark (article)
11707   "Set the process mark on ARTICLE and update the summary line."
11708   (push article gnus-newsgroup-saved)
11709   (when (gnus-summary-goto-subject article)
11710     (gnus-summary-update-secondary-mark article)))
11711
11712 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11713   "Mark N articles as read forwards.
11714 If N is negative, mark backwards instead.
11715 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11716 marked as unread.
11717 The difference between N and the actual number of articles marked is
11718 returned."
11719   (interactive "p")
11720   (gnus-set-global-variables)
11721   (let ((backward (< n 0))
11722         (gnus-summary-goto-unread
11723          (and gnus-summary-goto-unread
11724               (not (eq gnus-summary-goto-unread 'never))
11725               (not (memq mark (list gnus-unread-mark
11726                                     gnus-ticked-mark gnus-dormant-mark)))))
11727         (n (abs n))
11728         (mark (or mark gnus-del-mark)))
11729     (while (and (> n 0)
11730                 (gnus-summary-mark-article nil mark no-expire)
11731                 (zerop (gnus-summary-next-subject
11732                         (if backward -1 1)
11733                         (and gnus-summary-goto-unread
11734                              (not (eq gnus-summary-goto-unread 'never)))
11735                         t)))
11736       (setq n (1- n)))
11737     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11738     (gnus-summary-recenter)
11739     (gnus-summary-position-point)
11740     (gnus-set-mode-line 'summary)
11741     n))
11742
11743 (defun gnus-summary-mark-article-as-read (mark)
11744   "Mark the current article quickly as read with MARK."
11745   (let ((article (gnus-summary-article-number)))
11746     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11747     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11748     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11749     (setq gnus-newsgroup-reads
11750           (cons (cons article mark) gnus-newsgroup-reads))
11751     ;; Possibly remove from cache, if that is used.
11752     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11753     ;; Allow the backend to change the mark.
11754     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11755     ;; Check for auto-expiry.
11756     (when (and gnus-newsgroup-auto-expire
11757                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11758                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11759                    (= mark gnus-ancient-mark)
11760                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11761       (setq mark gnus-expirable-mark)
11762       (push article gnus-newsgroup-expirable))
11763     ;; Set the mark in the buffer.
11764     (gnus-summary-update-mark mark 'unread)
11765     t))
11766
11767 (defun gnus-summary-mark-article-as-unread (mark)
11768   "Mark the current article quickly as unread with MARK."
11769   (let ((article (gnus-summary-article-number)))
11770     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11771     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11772     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11773     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11774     (cond ((= mark gnus-ticked-mark)
11775            (push article gnus-newsgroup-marked))
11776           ((= mark gnus-dormant-mark)
11777            (push article gnus-newsgroup-dormant))
11778           (t
11779            (push article gnus-newsgroup-unreads)))
11780     (setq gnus-newsgroup-reads
11781           (delq (assq article gnus-newsgroup-reads)
11782                 gnus-newsgroup-reads))
11783
11784     ;; See whether the article is to be put in the cache.
11785     (and gnus-use-cache
11786          (vectorp (gnus-summary-article-header article))
11787          (save-excursion
11788            (gnus-cache-possibly-enter-article
11789             gnus-newsgroup-name article
11790             (gnus-summary-article-header article)
11791             (= mark gnus-ticked-mark)
11792             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11793
11794     ;; Fix the mark.
11795     (gnus-summary-update-mark mark 'unread)
11796     t))
11797
11798 (defun gnus-summary-mark-article (&optional article mark no-expire)
11799   "Mark ARTICLE with MARK.  MARK can be any character.
11800 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11801 `??' (dormant) and `?E' (expirable).
11802 If MARK is nil, then the default character `?D' is used.
11803 If ARTICLE is nil, then the article on the current line will be
11804 marked."
11805   ;; The mark might be a string.
11806   (and (stringp mark)
11807        (setq mark (aref mark 0)))
11808   ;; If no mark is given, then we check auto-expiring.
11809   (and (not no-expire)
11810        gnus-newsgroup-auto-expire
11811        (or (not mark)
11812            (and (numberp mark)
11813                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11814                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11815                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11816        (setq mark gnus-expirable-mark))
11817   (let* ((mark (or mark gnus-del-mark))
11818          (article (or article (gnus-summary-article-number))))
11819     (or article (error "No article on current line"))
11820     (if (or (= mark gnus-unread-mark)
11821             (= mark gnus-ticked-mark)
11822             (= mark gnus-dormant-mark))
11823         (gnus-mark-article-as-unread article mark)
11824       (gnus-mark-article-as-read article mark))
11825
11826     ;; See whether the article is to be put in the cache.
11827     (and gnus-use-cache
11828          (not (= mark gnus-canceled-mark))
11829          (vectorp (gnus-summary-article-header article))
11830          (save-excursion
11831            (gnus-cache-possibly-enter-article
11832             gnus-newsgroup-name article
11833             (gnus-summary-article-header article)
11834             (= mark gnus-ticked-mark)
11835             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11836
11837     (if (gnus-summary-goto-subject article nil t)
11838         (let ((buffer-read-only nil))
11839           (gnus-summary-show-thread)
11840           ;; Fix the mark.
11841           (gnus-summary-update-mark mark 'unread)
11842           t))))
11843
11844 (defun gnus-summary-update-secondary-mark (article)
11845   "Update the secondary (read, process, cache) mark."
11846   (gnus-summary-update-mark
11847    (cond ((memq article gnus-newsgroup-processable)
11848           gnus-process-mark)
11849          ((memq article gnus-newsgroup-cached)
11850           gnus-cached-mark)
11851          ((memq article gnus-newsgroup-replied)
11852           gnus-replied-mark)
11853          ((memq article gnus-newsgroup-saved)
11854           gnus-saved-mark)
11855          (t gnus-unread-mark))
11856    'replied)
11857   (when (gnus-visual-p 'summary-highlight 'highlight)
11858     (run-hooks 'gnus-summary-update-hook))
11859   t)
11860
11861 (defun gnus-summary-update-mark (mark type)
11862   (beginning-of-line)
11863   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11864         (buffer-read-only nil))
11865     (when forward
11866       ;; Go to the right position on the line.
11867       (forward-char forward)
11868       ;; Replace the old mark with the new mark.
11869       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11870       ;; Optionally update the marks by some user rule.
11871       (when (eq type 'unread)
11872         (gnus-data-set-mark
11873          (gnus-data-find (gnus-summary-article-number)) mark)
11874         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11875
11876 (defun gnus-mark-article-as-read (article &optional mark)
11877   "Enter ARTICLE in the pertinent lists and remove it from others."
11878   ;; Make the article expirable.
11879   (let ((mark (or mark gnus-del-mark)))
11880     (if (= mark gnus-expirable-mark)
11881         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11882       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11883     ;; Remove from unread and marked lists.
11884     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11885     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11886     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11887     (push (cons article mark) gnus-newsgroup-reads)
11888     ;; Possibly remove from cache, if that is used.
11889     (when gnus-use-cache
11890       (gnus-cache-enter-remove-article article))))
11891
11892 (defun gnus-mark-article-as-unread (article &optional mark)
11893   "Enter ARTICLE in the pertinent lists and remove it from others."
11894   (let ((mark (or mark gnus-ticked-mark)))
11895     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11896     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11897     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11898     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11899     (cond ((= mark gnus-ticked-mark)
11900            (push article gnus-newsgroup-marked))
11901           ((= mark gnus-dormant-mark)
11902            (push article gnus-newsgroup-dormant))
11903           (t
11904            (push article gnus-newsgroup-unreads)))
11905     (setq gnus-newsgroup-reads
11906           (delq (assq article gnus-newsgroup-reads)
11907                 gnus-newsgroup-reads))))
11908
11909 (defalias 'gnus-summary-mark-as-unread-forward
11910   'gnus-summary-tick-article-forward)
11911 (make-obsolete 'gnus-summary-mark-as-unread-forward
11912                'gnus-summary-tick-article-forward)
11913 (defun gnus-summary-tick-article-forward (n)
11914   "Tick N articles forwards.
11915 If N is negative, tick backwards instead.
11916 The difference between N and the number of articles ticked is returned."
11917   (interactive "p")
11918   (gnus-summary-mark-forward n gnus-ticked-mark))
11919
11920 (defalias 'gnus-summary-mark-as-unread-backward
11921   'gnus-summary-tick-article-backward)
11922 (make-obsolete 'gnus-summary-mark-as-unread-backward
11923                'gnus-summary-tick-article-backward)
11924 (defun gnus-summary-tick-article-backward (n)
11925   "Tick N articles backwards.
11926 The difference between N and the number of articles ticked is returned."
11927   (interactive "p")
11928   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11929
11930 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11931 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11932 (defun gnus-summary-tick-article (&optional article clear-mark)
11933   "Mark current article as unread.
11934 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11935 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11936   (interactive)
11937   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11938                                        gnus-ticked-mark)))
11939
11940 (defun gnus-summary-mark-as-read-forward (n)
11941   "Mark N articles as read forwards.
11942 If N is negative, mark backwards instead.
11943 The difference between N and the actual number of articles marked is
11944 returned."
11945   (interactive "p")
11946   (gnus-summary-mark-forward n gnus-del-mark t))
11947
11948 (defun gnus-summary-mark-as-read-backward (n)
11949   "Mark the N articles as read backwards.
11950 The difference between N and the actual number of articles marked is
11951 returned."
11952   (interactive "p")
11953   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11954
11955 (defun gnus-summary-mark-as-read (&optional article mark)
11956   "Mark current article as read.
11957 ARTICLE specifies the article to be marked as read.
11958 MARK specifies a string to be inserted at the beginning of the line."
11959   (gnus-summary-mark-article article mark))
11960
11961 (defun gnus-summary-clear-mark-forward (n)
11962   "Clear marks from N articles forward.
11963 If N is negative, clear backward instead.
11964 The difference between N and the number of marks cleared is returned."
11965   (interactive "p")
11966   (gnus-summary-mark-forward n gnus-unread-mark))
11967
11968 (defun gnus-summary-clear-mark-backward (n)
11969   "Clear marks from N articles backward.
11970 The difference between N and the number of marks cleared is returned."
11971   (interactive "p")
11972   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11973
11974 (defun gnus-summary-mark-unread-as-read ()
11975   "Intended to be used by `gnus-summary-mark-article-hook'."
11976   (when (memq gnus-current-article gnus-newsgroup-unreads)
11977     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11978
11979 (defun gnus-summary-mark-read-and-unread-as-read ()
11980   "Intended to be used by `gnus-summary-mark-article-hook'."
11981   (let ((mark (gnus-summary-article-mark)))
11982     (when (or (gnus-unread-mark-p mark)
11983               (gnus-read-mark-p mark))
11984       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11985
11986 (defun gnus-summary-mark-region-as-read (point mark all)
11987   "Mark all unread articles between point and mark as read.
11988 If given a prefix, mark all articles between point and mark as read,
11989 even ticked and dormant ones."
11990   (interactive "r\nP")
11991   (save-excursion
11992     (let (article)
11993       (goto-char point)
11994       (beginning-of-line)
11995       (while (and
11996               (< (point) mark)
11997               (progn
11998                 (when (or all
11999                           (memq (setq article (gnus-summary-article-number))
12000                                 gnus-newsgroup-unreads))
12001                   (gnus-summary-mark-article article gnus-del-mark))
12002                 t)
12003               (gnus-summary-find-next))))))
12004
12005 (defun gnus-summary-mark-below (score mark)
12006   "Mark articles with score less than SCORE with MARK."
12007   (interactive "P\ncMark: ")
12008   (gnus-set-global-variables)
12009   (setq score (if score
12010                   (prefix-numeric-value score)
12011                 (or gnus-summary-default-score 0)))
12012   (save-excursion
12013     (set-buffer gnus-summary-buffer)
12014     (goto-char (point-min))
12015     (while 
12016         (progn
12017           (and (< (gnus-summary-article-score) score)
12018                (gnus-summary-mark-article nil mark))
12019           (gnus-summary-find-next)))))
12020
12021 (defun gnus-summary-kill-below (&optional score)
12022   "Mark articles with score below SCORE as read."
12023   (interactive "P")
12024   (gnus-set-global-variables)
12025   (gnus-summary-mark-below score gnus-killed-mark))
12026
12027 (defun gnus-summary-clear-above (&optional score)
12028   "Clear all marks from articles with score above SCORE."
12029   (interactive "P")
12030   (gnus-set-global-variables)
12031   (gnus-summary-mark-above score gnus-unread-mark))
12032
12033 (defun gnus-summary-tick-above (&optional score)
12034   "Tick all articles with score above SCORE."
12035   (interactive "P")
12036   (gnus-set-global-variables)
12037   (gnus-summary-mark-above score gnus-ticked-mark))
12038
12039 (defun gnus-summary-mark-above (score mark)
12040   "Mark articles with score over SCORE with MARK."
12041   (interactive "P\ncMark: ")
12042   (gnus-set-global-variables)
12043   (setq score (if score
12044                   (prefix-numeric-value score)
12045                 (or gnus-summary-default-score 0)))
12046   (save-excursion
12047     (set-buffer gnus-summary-buffer)
12048     (goto-char (point-min))
12049     (while (and (progn
12050                   (if (> (gnus-summary-article-score) score)
12051                       (gnus-summary-mark-article nil mark))
12052                   t)
12053                 (gnus-summary-find-next)))))
12054
12055 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12056 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12057 (defun gnus-summary-limit-include-expunged ()
12058   "Display all the hidden articles that were expunged for low scores."
12059   (interactive)
12060   (gnus-set-global-variables)
12061   (let ((buffer-read-only nil))
12062     (let ((scored gnus-newsgroup-scored)
12063           headers h)
12064       (while scored
12065         (or (gnus-summary-goto-subject (caar scored))
12066             (and (setq h (gnus-summary-article-header (caar scored)))
12067                  (< (cdar scored) gnus-summary-expunge-below)
12068                  (setq headers (cons h headers))))
12069         (setq scored (cdr scored)))
12070       (or headers (error "No expunged articles hidden."))
12071       (goto-char (point-min))
12072       (gnus-summary-prepare-unthreaded (nreverse headers)))
12073     (goto-char (point-min))
12074     (gnus-summary-position-point)))
12075
12076 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12077   "Mark all articles not marked as unread in this newsgroup as read.
12078 If prefix argument ALL is non-nil, all articles are marked as read.
12079 If QUIETLY is non-nil, no questions will be asked.
12080 If TO-HERE is non-nil, it should be a point in the buffer.  All
12081 articles before this point will be marked as read.
12082 The number of articles marked as read is returned."
12083   (interactive "P")
12084   (gnus-set-global-variables)
12085   (prog1
12086       (if (or quietly
12087               (not gnus-interactive-catchup) ;Without confirmation?
12088               gnus-expert-user
12089               (gnus-y-or-n-p
12090                (if all
12091                    "Mark absolutely all articles as read? "
12092                  "Mark all unread articles as read? ")))
12093           (if (and not-mark
12094                    (not gnus-newsgroup-adaptive)
12095                    (not gnus-newsgroup-auto-expire))
12096               (progn
12097                 (when all
12098                   (setq gnus-newsgroup-marked nil
12099                         gnus-newsgroup-dormant nil))
12100                 (setq gnus-newsgroup-unreads nil))
12101             ;; We actually mark all articles as canceled, which we
12102             ;; have to do when using auto-expiry or adaptive scoring.
12103             (gnus-summary-show-all-threads)
12104             (if (gnus-summary-first-subject (not all))
12105                 (while (and
12106                         (if to-here (< (point) to-here) t)
12107                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12108                         (gnus-summary-find-next (not all)))))
12109             (unless to-here
12110               (setq gnus-newsgroup-unreads nil))
12111             (gnus-set-mode-line 'summary)))
12112     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12113       (if (and (not to-here) (eq 'nnvirtual (car method)))
12114           (nnvirtual-catchup-group
12115            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12116     (gnus-summary-position-point)))
12117
12118 (defun gnus-summary-catchup-to-here (&optional all)
12119   "Mark all unticked articles before the current one as read.
12120 If ALL is non-nil, also mark ticked and dormant articles as read."
12121   (interactive "P")
12122   (gnus-set-global-variables)
12123   (save-excursion
12124     (let ((beg (point)))
12125       ;; We check that there are unread articles.
12126       (when (or all (gnus-summary-find-prev))
12127         (gnus-summary-catchup all t beg))))
12128   (gnus-summary-position-point))
12129
12130 (defun gnus-summary-catchup-all (&optional quietly)
12131   "Mark all articles in this newsgroup as read."
12132   (interactive "P")
12133   (gnus-set-global-variables)
12134   (gnus-summary-catchup t quietly))
12135
12136 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12137   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12138 If prefix argument ALL is non-nil, all articles are marked as read."
12139   (interactive "P")
12140   (gnus-set-global-variables)
12141   (gnus-summary-catchup all quietly nil 'fast)
12142   ;; Select next newsgroup or exit.
12143   (if (eq gnus-auto-select-next 'quietly)
12144       (gnus-summary-next-group nil)
12145     (gnus-summary-exit)))
12146
12147 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12148   "Mark all articles in this newsgroup as read, and then exit."
12149   (interactive "P")
12150   (gnus-set-global-variables)
12151   (gnus-summary-catchup-and-exit t quietly))
12152
12153 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12154 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12155   "Mark all articles in this group as read and select the next group.
12156 If given a prefix, mark all articles, unread as well as ticked, as
12157 read."
12158   (interactive "P")
12159   (gnus-set-global-variables)
12160   (save-excursion
12161     (gnus-summary-catchup all))
12162   (gnus-summary-next-article t nil nil t))
12163
12164 ;; Thread-based commands.
12165
12166 (defun gnus-summary-articles-in-thread (&optional article)
12167   "Return a list of all articles in the current thread.
12168 If ARTICLE is non-nil, return all articles in the thread that starts
12169 with that article."
12170   (let* ((article (or article (gnus-summary-article-number)))
12171          (data (gnus-data-find-list article))
12172          (top-level (gnus-data-level (car data)))
12173          (top-subject
12174           (cond ((null gnus-thread-operation-ignore-subject)
12175                  (gnus-simplify-subject-re
12176                   (mail-header-subject (gnus-data-header (car data)))))
12177                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12178                  (gnus-simplify-subject-fuzzy
12179                   (mail-header-subject (gnus-data-header (car data)))))
12180                 (t nil)))
12181          articles)
12182     (if (not data)
12183         ()                              ; This article doesn't exist.
12184       (while data
12185         (and (or (not top-subject)
12186                  (string= top-subject
12187                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12188                               (gnus-simplify-subject-fuzzy
12189                                (mail-header-subject
12190                                 (gnus-data-header (car data))))
12191                             (gnus-simplify-subject-re
12192                              (mail-header-subject
12193                               (gnus-data-header (car data)))))))
12194              (setq articles (cons (gnus-data-number (car data)) articles)))
12195         (if (and (setq data (cdr data))
12196                  (> (gnus-data-level (car data)) top-level))
12197             ()
12198           (setq data nil)))
12199       ;; Return the list of articles.
12200       (nreverse articles))))
12201
12202 (defun gnus-summary-rethread-current ()
12203   "Rethread the thread the current article is part of."
12204   (interactive)
12205   (gnus-set-global-variables)
12206   (let* ((gnus-show-threads t)
12207          (article (gnus-summary-article-number))
12208          (id (mail-header-id (gnus-summary-article-header)))
12209          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12210     (unless id
12211       (error "No article on the current line"))
12212     (gnus-rebuild-thread id)
12213     (gnus-summary-goto-subject article)))
12214
12215 (defun gnus-summary-reparent-thread ()
12216   "Make current article child of the marked (or previous) article.
12217
12218 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12219 is non-nil or the Subject: of both articles are the same."
12220   (interactive)
12221   (or (not (gnus-group-read-only-p))
12222       (error "The current newsgroup does not support article editing."))
12223   (or (<= (length gnus-newsgroup-processable) 1)
12224       (error "No more than one article may be marked."))
12225   (save-window-excursion
12226     (let ((gnus-article-buffer " *reparent*")
12227           (current-article (gnus-summary-article-number))
12228           ; first grab the marked article, otherwise one line up.
12229           (parent-article (if (not (null gnus-newsgroup-processable))
12230                               (car gnus-newsgroup-processable)
12231                             (save-excursion
12232                               (if (eq (forward-line -1) 0)
12233                                   (gnus-summary-article-number)
12234                                 (error "Beginning of summary buffer."))))))
12235       (or (not (eq current-article parent-article))
12236           (error "An article may not be self-referential."))
12237       (let ((message-id (mail-header-id 
12238                          (gnus-summary-article-header parent-article))))
12239         (or (and message-id (not (equal message-id "")))
12240             (error "No message-id in desired parent."))
12241         (gnus-summary-select-article t t nil current-article)
12242         (set-buffer gnus-article-buffer)
12243         (setq buffer-read-only nil)
12244         (let ((buf (format "%s" (buffer-string))))
12245           (erase-buffer)
12246           (insert buf))
12247         (goto-char (point-min))
12248         (if (search-forward-regexp "^References: " nil t)
12249             (insert message-id " " )
12250           (insert "References: " message-id "\n"))
12251         (or (gnus-request-replace-article current-article
12252                                           (car gnus-article-current)
12253                                           gnus-article-buffer)
12254             (error "Couldn't replace article."))
12255         (set-buffer gnus-summary-buffer)
12256         (gnus-summary-unmark-all-processable)
12257         (gnus-summary-rethread-current)
12258         (message "Article %d is now the child of article %d."
12259                  current-article parent-article)))))
12260
12261 (defun gnus-summary-toggle-threads (&optional arg)
12262   "Toggle showing conversation threads.
12263 If ARG is positive number, turn showing conversation threads on."
12264   (interactive "P")
12265   (gnus-set-global-variables)
12266   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12267     (setq gnus-show-threads
12268           (if (null arg) (not gnus-show-threads)
12269             (> (prefix-numeric-value arg) 0)))
12270     (gnus-summary-prepare)
12271     (gnus-summary-goto-subject current)
12272     (gnus-summary-position-point)))
12273
12274 (defun gnus-summary-show-all-threads ()
12275   "Show all threads."
12276   (interactive)
12277   (gnus-set-global-variables)
12278   (save-excursion
12279     (let ((buffer-read-only nil))
12280       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12281   (gnus-summary-position-point))
12282
12283 (defun gnus-summary-show-thread ()
12284   "Show thread subtrees.
12285 Returns nil if no thread was there to be shown."
12286   (interactive)
12287   (gnus-set-global-variables)
12288   (let ((buffer-read-only nil)
12289         (orig (point))
12290         ;; first goto end then to beg, to have point at beg after let
12291         (end (progn (end-of-line) (point)))
12292         (beg (progn (beginning-of-line) (point))))
12293     (prog1
12294         ;; Any hidden lines here?
12295         (search-forward "\r" end t)
12296       (subst-char-in-region beg end ?\^M ?\n t)
12297       (goto-char orig)
12298       (gnus-summary-position-point))))
12299
12300 (defun gnus-summary-hide-all-threads ()
12301   "Hide all thread subtrees."
12302   (interactive)
12303   (gnus-set-global-variables)
12304   (save-excursion
12305     (goto-char (point-min))
12306     (gnus-summary-hide-thread)
12307     (while (zerop (gnus-summary-next-thread 1 t))
12308       (gnus-summary-hide-thread)))
12309   (gnus-summary-position-point))
12310
12311 (defun gnus-summary-hide-thread ()
12312   "Hide thread subtrees.
12313 Returns nil if no threads were there to be hidden."
12314   (interactive)
12315   (gnus-set-global-variables)
12316   (let ((buffer-read-only nil)
12317         (start (point))
12318         (article (gnus-summary-article-number)))
12319     (goto-char start)
12320     ;; Go forward until either the buffer ends or the subthread
12321     ;; ends.
12322     (when (and (not (eobp))
12323                (or (zerop (gnus-summary-next-thread 1 t))
12324                    (goto-char (point-max))))
12325       (prog1
12326           (if (and (> (point) start)
12327                    (search-backward "\n" start t))
12328               (progn
12329                 (subst-char-in-region start (point) ?\n ?\^M)
12330                 (gnus-summary-goto-subject article))
12331             (goto-char start)
12332             nil)
12333         ;;(gnus-summary-position-point)
12334         ))))
12335
12336 (defun gnus-summary-go-to-next-thread (&optional previous)
12337   "Go to the same level (or less) next thread.
12338 If PREVIOUS is non-nil, go to previous thread instead.
12339 Return the article number moved to, or nil if moving was impossible."
12340   (let ((level (gnus-summary-thread-level))
12341         (way (if previous -1 1))
12342         (beg (point)))
12343     (forward-line way)
12344     (while (and (not (eobp))
12345                 (< level (gnus-summary-thread-level)))
12346       (forward-line way))
12347     (if (eobp)
12348         (progn
12349           (goto-char beg)
12350           nil)
12351       (setq beg (point))
12352       (prog1
12353           (gnus-summary-article-number)
12354         (goto-char beg)))))
12355
12356 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12357   "Go to the same level (or less) next thread.
12358 If PREVIOUS is non-nil, go to previous thread instead.
12359 Return the article number moved to, or nil if moving was impossible."
12360   (if (and (eq gnus-summary-make-false-root 'dummy)
12361            (gnus-summary-article-intangible-p))
12362       (let ((beg (point)))
12363         (while (and (zerop (forward-line 1))
12364                     (not (gnus-summary-article-intangible-p))
12365                     (not (zerop (save-excursion 
12366                                   (gnus-summary-thread-level))))))
12367         (if (eobp)
12368             (progn
12369               (goto-char beg)
12370               nil)
12371           (point)))
12372     (let* ((level (gnus-summary-thread-level))
12373            (article (gnus-summary-article-number))
12374            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12375            oart)
12376       (while data
12377         (if (<= (gnus-data-level (car data)) level)
12378             (setq oart (gnus-data-number (car data))
12379                   data nil)
12380           (setq data (cdr data))))
12381       (and oart
12382            (gnus-summary-goto-subject oart)))))
12383
12384 (defun gnus-summary-next-thread (n &optional silent)
12385   "Go to the same level next N'th thread.
12386 If N is negative, search backward instead.
12387 Returns the difference between N and the number of skips actually
12388 done.
12389
12390 If SILENT, don't output messages."
12391   (interactive "p")
12392   (gnus-set-global-variables)
12393   (let ((backward (< n 0))
12394         (n (abs n))
12395         old dum int)
12396     (while (and (> n 0)
12397                 (gnus-summary-go-to-next-thread backward))
12398       (decf n))
12399     (unless silent 
12400       (gnus-summary-position-point))
12401     (when (and (not silent) (/= 0 n))
12402       (gnus-message 7 "No more threads"))
12403     n))
12404
12405 (defun gnus-summary-prev-thread (n)
12406   "Go to the same level previous N'th thread.
12407 Returns the difference between N and the number of skips actually
12408 done."
12409   (interactive "p")
12410   (gnus-set-global-variables)
12411   (gnus-summary-next-thread (- n)))
12412
12413 (defun gnus-summary-go-down-thread ()
12414   "Go down one level in the current thread."
12415   (let ((children (gnus-summary-article-children)))
12416     (and children
12417          (gnus-summary-goto-subject (car children)))))
12418
12419 (defun gnus-summary-go-up-thread ()
12420   "Go up one level in the current thread."
12421   (let ((parent (gnus-summary-article-parent)))
12422     (and parent
12423          (gnus-summary-goto-subject parent))))
12424
12425 (defun gnus-summary-down-thread (n)
12426   "Go down thread N steps.
12427 If N is negative, go up instead.
12428 Returns the difference between N and how many steps down that were
12429 taken."
12430   (interactive "p")
12431   (gnus-set-global-variables)
12432   (let ((up (< n 0))
12433         (n (abs n)))
12434     (while (and (> n 0)
12435                 (if up (gnus-summary-go-up-thread)
12436                   (gnus-summary-go-down-thread)))
12437       (setq n (1- n)))
12438     (gnus-summary-position-point)
12439     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12440     n))
12441
12442 (defun gnus-summary-up-thread (n)
12443   "Go up thread N steps.
12444 If N is negative, go up instead.
12445 Returns the difference between N and how many steps down that were
12446 taken."
12447   (interactive "p")
12448   (gnus-set-global-variables)
12449   (gnus-summary-down-thread (- n)))
12450
12451 (defun gnus-summary-top-thread ()
12452   "Go to the top of the thread."
12453   (interactive)
12454   (gnus-set-global-variables)
12455   (while (gnus-summary-go-up-thread))
12456   (gnus-summary-article-number))
12457
12458 (defun gnus-summary-kill-thread (&optional unmark)
12459   "Mark articles under current thread as read.
12460 If the prefix argument is positive, remove any kinds of marks.
12461 If the prefix argument is negative, tick articles instead."
12462   (interactive "P")
12463   (gnus-set-global-variables)
12464   (if unmark
12465       (setq unmark (prefix-numeric-value unmark)))
12466   (let ((articles (gnus-summary-articles-in-thread)))
12467     (save-excursion
12468       ;; Expand the thread.
12469       (gnus-summary-show-thread)
12470       ;; Mark all the articles.
12471       (while articles
12472         (gnus-summary-goto-subject (car articles))
12473         (cond ((null unmark)
12474                (gnus-summary-mark-article-as-read gnus-killed-mark))
12475               ((> unmark 0)
12476                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12477               (t
12478                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12479         (setq articles (cdr articles))))
12480     ;; Hide killed subtrees.
12481     (and (null unmark)
12482          gnus-thread-hide-killed
12483          (gnus-summary-hide-thread))
12484     ;; If marked as read, go to next unread subject.
12485     (if (null unmark)
12486         ;; Go to next unread subject.
12487         (gnus-summary-next-subject 1 t)))
12488   (gnus-set-mode-line 'summary))
12489
12490 ;; Summary sorting commands
12491
12492 (defun gnus-summary-sort-by-number (&optional reverse)
12493   "Sort summary buffer by article number.
12494 Argument REVERSE means reverse order."
12495   (interactive "P")
12496   (gnus-summary-sort 'number reverse))
12497
12498 (defun gnus-summary-sort-by-author (&optional reverse)
12499   "Sort summary buffer by author name alphabetically.
12500 If case-fold-search is non-nil, case of letters is ignored.
12501 Argument REVERSE means reverse order."
12502   (interactive "P")
12503   (gnus-summary-sort 'author reverse))
12504
12505 (defun gnus-summary-sort-by-subject (&optional reverse)
12506   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12507 If case-fold-search is non-nil, case of letters is ignored.
12508 Argument REVERSE means reverse order."
12509   (interactive "P")
12510   (gnus-summary-sort 'subject reverse))
12511
12512 (defun gnus-summary-sort-by-date (&optional reverse)
12513   "Sort summary buffer by date.
12514 Argument REVERSE means reverse order."
12515   (interactive "P")
12516   (gnus-summary-sort 'date reverse))
12517
12518 (defun gnus-summary-sort-by-score (&optional reverse)
12519   "Sort summary buffer by score.
12520 Argument REVERSE means reverse order."
12521   (interactive "P")
12522   (gnus-summary-sort 'score reverse))
12523
12524 (defun gnus-summary-sort (predicate reverse)
12525   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12526   (gnus-set-global-variables)
12527   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12528          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12529          (gnus-thread-sort-functions
12530           (list
12531            (if (not reverse)
12532                thread
12533              `(lambda (t1 t2)
12534                 (,thread t2 t1)))))
12535          (gnus-article-sort-functions
12536           (list
12537            (if (not reverse)
12538                article
12539              `(lambda (t1 t2)
12540                 (,article t2 t1)))))
12541          (buffer-read-only)
12542          (gnus-summary-prepare-hook nil))
12543     ;; We do the sorting by regenerating the threads.
12544     (gnus-summary-prepare)
12545     ;; Hide subthreads if needed.
12546     (when (and gnus-show-threads gnus-thread-hide-subtree)
12547       (gnus-summary-hide-all-threads)))
12548   ;; If in async mode, we send some info to the backend.
12549   (when gnus-newsgroup-async
12550     (gnus-request-asynchronous
12551      gnus-newsgroup-name gnus-newsgroup-data)))
12552
12553 (defun gnus-sortable-date (date)
12554   "Make sortable string by string-lessp from DATE.
12555 Timezone package is used."
12556   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12557          (year (aref date 0))
12558          (month (aref date 1))
12559          (day (aref date 2)))
12560     (timezone-make-sortable-date
12561      year month day
12562      (timezone-make-time-string
12563       (aref date 3) (aref date 4) (aref date 5)))))
12564
12565 ;; Summary saving commands.
12566
12567 (defun gnus-summary-save-article (&optional n not-saved)
12568   "Save the current article using the default saver function.
12569 If N is a positive number, save the N next articles.
12570 If N is a negative number, save the N previous articles.
12571 If N is nil and any articles have been marked with the process mark,
12572 save those articles instead.
12573 The variable `gnus-default-article-saver' specifies the saver function."
12574   (interactive "P")
12575   (gnus-set-global-variables)
12576   (let ((articles (gnus-summary-work-articles n))
12577         file header article)
12578     (while articles
12579       (setq header (gnus-summary-article-header
12580                     (setq article (pop articles))))
12581       (if (not (vectorp header))
12582           ;; This is a pseudo-article.
12583           (if (assq 'name header)
12584               (gnus-copy-file (cdr (assq 'name header)))
12585             (gnus-message 1 "Article %d is unsaveable" article))
12586         ;; This is a real article.
12587         (save-window-excursion
12588           (gnus-summary-select-article t nil nil article))
12589         (unless gnus-save-all-headers
12590           ;; Remove headers accoring to `gnus-saved-headers'.
12591           (let ((gnus-visible-headers
12592                  (or gnus-saved-headers gnus-visible-headers)))
12593             (gnus-article-hide-headers nil t)))
12594         ;; Remove any X-Gnus lines.
12595         (save-excursion
12596           (set-buffer gnus-article-buffer)
12597           (save-restriction
12598             (let ((buffer-read-only nil))
12599               (nnheader-narrow-to-headers)
12600               (while (re-search-forward "^X-Gnus" nil t)
12601                 (gnus-delete-line)))))
12602         (save-window-excursion
12603           (if (not gnus-default-article-saver)
12604               (error "No default saver is defined.")
12605             (setq file (funcall
12606                         gnus-default-article-saver
12607                         (cond
12608                          ((not gnus-prompt-before-saving)
12609                           'default)
12610                          ((eq gnus-prompt-before-saving 'always)
12611                           nil)
12612                          (t file))))))
12613         (gnus-summary-remove-process-mark article)
12614         (unless not-saved
12615           (gnus-summary-set-saved-mark article))))
12616     (gnus-summary-position-point)
12617     n))
12618
12619 (defun gnus-summary-pipe-output (&optional arg)
12620   "Pipe the current article to a subprocess.
12621 If N is a positive number, pipe the N next articles.
12622 If N is a negative number, pipe the N previous articles.
12623 If N is nil and any articles have been marked with the process mark,
12624 pipe those articles instead."
12625   (interactive "P")
12626   (gnus-set-global-variables)
12627   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12628     (gnus-summary-save-article arg t))
12629   (gnus-configure-windows 'pipe))
12630
12631 (defun gnus-summary-save-article-mail (&optional arg)
12632   "Append the current article to an mail file.
12633 If N is a positive number, save the N next articles.
12634 If N is a negative number, save the N previous articles.
12635 If N is nil and any articles have been marked with the process mark,
12636 save those articles instead."
12637   (interactive "P")
12638   (gnus-set-global-variables)
12639   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12640     (gnus-summary-save-article arg)))
12641
12642 (defun gnus-summary-save-article-rmail (&optional arg)
12643   "Append the current article to an rmail file.
12644 If N is a positive number, save the N next articles.
12645 If N is a negative number, save the N previous articles.
12646 If N is nil and any articles have been marked with the process mark,
12647 save those articles instead."
12648   (interactive "P")
12649   (gnus-set-global-variables)
12650   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12651     (gnus-summary-save-article arg)))
12652
12653 (defun gnus-summary-save-article-file (&optional arg)
12654   "Append the current article to a file.
12655 If N is a positive number, save the N next articles.
12656 If N is a negative number, save the N previous articles.
12657 If N is nil and any articles have been marked with the process mark,
12658 save those articles instead."
12659   (interactive "P")
12660   (gnus-set-global-variables)
12661   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12662     (gnus-summary-save-article arg)))
12663
12664 (defun gnus-summary-save-article-body-file (&optional arg)
12665   "Append the current article body to a file.
12666 If N is a positive number, save the N next articles.
12667 If N is a negative number, save the N previous articles.
12668 If N is nil and any articles have been marked with the process mark,
12669 save those articles instead."
12670   (interactive "P")
12671   (gnus-set-global-variables)
12672   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12673     (gnus-summary-save-article arg)))
12674
12675 (defun gnus-get-split-value (methods)
12676   "Return a value based on the split METHODS."
12677   (let (split-name method result match)
12678     (when methods
12679       (save-excursion
12680         (set-buffer gnus-original-article-buffer)
12681         (save-restriction
12682           (nnheader-narrow-to-headers)
12683           (while methods
12684             (goto-char (point-min))
12685             (setq method (pop methods))
12686             (setq match (car method))
12687             (when (cond
12688                    ((stringp match)
12689                     ;; Regular expression.
12690                     (condition-case ()
12691                         (re-search-forward match nil t)
12692                       (error nil)))
12693                    ((gnus-functionp match)
12694                     ;; Function.
12695                     (save-restriction
12696                       (widen)
12697                       (setq result (funcall match gnus-newsgroup-name))))
12698                    ((consp match)
12699                     ;; Form.
12700                     (save-restriction
12701                       (widen)
12702                       (setq result (eval match)))))
12703               (setq split-name (append (cdr method) split-name))
12704               (cond ((stringp result)
12705                      (push result split-name))
12706                     ((consp result)
12707                      (setq split-name (append result split-name)))))))))
12708     split-name))
12709
12710 (defun gnus-read-move-group-name (prompt default articles prefix)
12711   "Read a group name."
12712   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12713          (prom
12714           (format "Where do you want to %s %s? "
12715                   prompt
12716                   (if (> (length articles) 1)
12717                       (format "these %d articles" (length articles))
12718                     "this article")))
12719          (to-newsgroup
12720           (cond
12721            ((null split-name)
12722             (completing-read
12723              (concat prom
12724                      (if default
12725                          (format "(default %s) " default)
12726                        ""))
12727              gnus-active-hashtb nil nil prefix))
12728            ((= 1 (length split-name))
12729             (completing-read prom gnus-active-hashtb
12730                              nil nil (cons (car split-name) 0)))
12731            (t
12732             (completing-read
12733              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12734
12735     (when to-newsgroup
12736       (if (or (string= to-newsgroup "")
12737               (string= to-newsgroup prefix))
12738           (setq to-newsgroup (or default "")))
12739       (or (gnus-active to-newsgroup)
12740           (gnus-activate-group to-newsgroup)
12741           (error "No such group: %s" to-newsgroup)))
12742     to-newsgroup))
12743
12744 (defun gnus-read-save-file-name (prompt default-name)
12745   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12746          (file
12747           ;; Let the split methods have their say.
12748           (cond
12749            ;; No split name was found.
12750            ((null split-name)
12751             (read-file-name
12752              (concat prompt " (default "
12753                      (file-name-nondirectory default-name) ") ")
12754              (file-name-directory default-name)
12755              default-name))
12756            ;; A single split name was found
12757            ((= 1 (length split-name))
12758             (read-file-name
12759              (concat prompt " (default " (car split-name) ") ")
12760              gnus-article-save-directory
12761              (concat gnus-article-save-directory (car split-name))))
12762            ;; A list of splits was found.
12763            (t
12764             (setq split-name (mapcar (lambda (el) (list el))
12765                                      (nreverse split-name)))
12766             (let ((result (completing-read
12767                            (concat prompt " ") split-name nil nil)))
12768               (concat gnus-article-save-directory
12769                       (if (string= result "")
12770                           (caar split-name)
12771                         result)))))))
12772     ;; If we have read a directory, we append the default file name.
12773     (when (file-directory-p file)
12774       (setq file (concat (file-name-as-directory file)
12775                          (file-name-nondirectory default-name))))
12776     ;; Possibly translate some charaters.
12777     (nnheader-translate-file-chars file)))
12778
12779 (defun gnus-article-archive-name (group)
12780   "Return the first instance of an \"Archive-name\" in the current buffer."
12781   (let ((case-fold-search t))
12782     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12783       (match-string 1))))
12784
12785 (defun gnus-summary-save-in-rmail (&optional filename)
12786   "Append this article to Rmail file.
12787 Optional argument FILENAME specifies file name.
12788 Directory to save to is default to `gnus-article-save-directory' which
12789 is initialized from the SAVEDIR environment variable."
12790   (interactive)
12791   (gnus-set-global-variables)
12792   (let ((default-name
12793           (funcall gnus-rmail-save-name gnus-newsgroup-name
12794                    gnus-current-headers gnus-newsgroup-last-rmail)))
12795     (setq filename
12796           (cond ((eq filename 'default)
12797                  default-name)
12798                 (filename filename)
12799                 (t (gnus-read-save-file-name
12800                     "Save in rmail file:" default-name))))
12801     (gnus-make-directory (file-name-directory filename))
12802     (gnus-eval-in-buffer-window
12803      gnus-original-article-buffer
12804      (save-excursion
12805        (save-restriction
12806          (widen)
12807          (gnus-output-to-rmail filename))))
12808     ;; Remember the directory name to save articles
12809     (setq gnus-newsgroup-last-rmail filename)))
12810
12811 (defun gnus-summary-save-in-mail (&optional filename)
12812   "Append this article to Unix mail file.
12813 Optional argument FILENAME specifies file name.
12814 Directory to save to is default to `gnus-article-save-directory' which
12815 is initialized from the SAVEDIR environment variable."
12816   (interactive)
12817   (gnus-set-global-variables)
12818   (let ((default-name
12819           (funcall gnus-mail-save-name gnus-newsgroup-name
12820                    gnus-current-headers gnus-newsgroup-last-mail)))
12821     (setq filename
12822           (cond ((eq filename 'default)
12823                  default-name)
12824                 (filename filename)
12825                 (t (gnus-read-save-file-name
12826                     "Save in Unix mail file:" default-name))))
12827     (setq filename
12828           (expand-file-name filename
12829                             (and default-name
12830                                  (file-name-directory default-name))))
12831     (gnus-make-directory (file-name-directory filename))
12832     (gnus-eval-in-buffer-window
12833      gnus-original-article-buffer
12834      (save-excursion
12835        (save-restriction
12836          (widen)
12837          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12838              (gnus-output-to-rmail filename)
12839            (let ((mail-use-rfc822 t))
12840              (rmail-output filename 1 t t))))))
12841     ;; Remember the directory name to save articles.
12842     (setq gnus-newsgroup-last-mail filename)))
12843
12844 (defun gnus-summary-save-in-file (&optional filename)
12845   "Append this article to file.
12846 Optional argument FILENAME specifies file name.
12847 Directory to save to is default to `gnus-article-save-directory' which
12848 is initialized from the SAVEDIR environment variable."
12849   (interactive)
12850   (gnus-set-global-variables)
12851   (let ((default-name
12852           (funcall gnus-file-save-name gnus-newsgroup-name
12853                    gnus-current-headers gnus-newsgroup-last-file)))
12854     (setq filename
12855           (cond ((eq filename 'default)
12856                  default-name)
12857                 (filename filename)
12858                 (t (gnus-read-save-file-name
12859                     "Save in file:" default-name))))
12860     (gnus-make-directory (file-name-directory filename))
12861     (gnus-eval-in-buffer-window
12862      gnus-original-article-buffer
12863      (save-excursion
12864        (save-restriction
12865          (widen)
12866          (gnus-output-to-file filename))))
12867     ;; Remember the directory name to save articles.
12868     (setq gnus-newsgroup-last-file filename)))
12869
12870 (defun gnus-summary-save-body-in-file (&optional filename)
12871   "Append this article body to a file.
12872 Optional argument FILENAME specifies file name.
12873 The directory to save in defaults to `gnus-article-save-directory' which
12874 is initialized from the SAVEDIR environment variable."
12875   (interactive)
12876   (gnus-set-global-variables)
12877   (let ((default-name
12878           (funcall gnus-file-save-name gnus-newsgroup-name
12879                    gnus-current-headers gnus-newsgroup-last-file)))
12880     (setq filename
12881           (cond ((eq filename 'default)
12882                  default-name)
12883                 (filename filename)
12884                 (t (gnus-read-save-file-name
12885                     "Save body in file:" default-name))))
12886     (gnus-make-directory (file-name-directory filename))
12887     (gnus-eval-in-buffer-window
12888      gnus-article-buffer
12889      (save-excursion
12890        (save-restriction
12891          (widen)
12892          (goto-char (point-min))
12893          (and (search-forward "\n\n" nil t)
12894               (narrow-to-region (point) (point-max)))
12895          (gnus-output-to-file filename))))
12896     ;; Remember the directory name to save articles.
12897     (setq gnus-newsgroup-last-file filename)))
12898
12899 (defun gnus-summary-save-in-pipe (&optional command)
12900   "Pipe this article to subprocess."
12901   (interactive)
12902   (gnus-set-global-variables)
12903   (setq command
12904         (cond ((eq command 'default)
12905                gnus-last-shell-command)
12906               (command command)
12907               (t (read-string "Shell command on article: "
12908                               gnus-last-shell-command))))
12909   (if (string-equal command "")
12910       (setq command gnus-last-shell-command))
12911   (gnus-eval-in-buffer-window
12912    gnus-article-buffer
12913    (save-restriction
12914      (widen)
12915      (shell-command-on-region (point-min) (point-max) command nil)))
12916   (setq gnus-last-shell-command command))
12917
12918 ;; Summary extract commands
12919
12920 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12921   (let ((buffer-read-only nil)
12922         (article (gnus-summary-article-number))
12923         after-article b e)
12924     (or (gnus-summary-goto-subject article)
12925         (error (format "No such article: %d" article)))
12926     (gnus-summary-position-point)
12927     ;; If all commands are to be bunched up on one line, we collect
12928     ;; them here.
12929     (if gnus-view-pseudos-separately
12930         ()
12931       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12932             files action)
12933         (while ps
12934           (setq action (cdr (assq 'action (car ps))))
12935           (setq files (list (cdr (assq 'name (car ps)))))
12936           (while (and ps (cdr ps)
12937                       (string= (or action "1")
12938                                (or (cdr (assq 'action (cadr ps))) "2")))
12939             (setq files (cons (cdr (assq 'name (cadr ps))) files))
12940             (setcdr ps (cddr ps)))
12941           (if (not files)
12942               ()
12943             (if (not (string-match "%s" action))
12944                 (setq files (cons " " files)))
12945             (setq files (cons " " files))
12946             (and (assq 'execute (car ps))
12947                  (setcdr (assq 'execute (car ps))
12948                          (funcall (if (string-match "%s" action)
12949                                       'format 'concat)
12950                                   action
12951                                   (mapconcat (lambda (f) f) files " ")))))
12952           (setq ps (cdr ps)))))
12953     (if (and gnus-view-pseudos (not not-view))
12954         (while pslist
12955           (and (assq 'execute (car pslist))
12956                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12957                                      (eq gnus-view-pseudos 'not-confirm)))
12958           (setq pslist (cdr pslist)))
12959       (save-excursion
12960         (while pslist
12961           (setq after-article (or (cdr (assq 'article (car pslist)))
12962                                   (gnus-summary-article-number)))
12963           (gnus-summary-goto-subject after-article)
12964           (forward-line 1)
12965           (setq b (point))
12966           (insert "          " (file-name-nondirectory
12967                                 (cdr (assq 'name (car pslist))))
12968                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12969           (setq e (point))
12970           (forward-line -1)             ; back to `b'
12971           (add-text-properties
12972            b e (list 'gnus-number gnus-reffed-article-number
12973                      gnus-mouse-face-prop gnus-mouse-face))
12974           (gnus-data-enter
12975            after-article gnus-reffed-article-number
12976            gnus-unread-mark b (car pslist) 0 (- e b))
12977           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12978           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12979           (setq pslist (cdr pslist)))))))
12980
12981 (defun gnus-pseudos< (p1 p2)
12982   (let ((c1 (cdr (assq 'action p1)))
12983         (c2 (cdr (assq 'action p2))))
12984     (and c1 c2 (string< c1 c2))))
12985
12986 (defun gnus-request-pseudo-article (props)
12987   (cond ((assq 'execute props)
12988          (gnus-execute-command (cdr (assq 'execute props)))))
12989   (let ((gnus-current-article (gnus-summary-article-number)))
12990     (run-hooks 'gnus-mark-article-hook)))
12991
12992 (defun gnus-execute-command (command &optional automatic)
12993   (save-excursion
12994     (gnus-article-setup-buffer)
12995     (set-buffer gnus-article-buffer)
12996     (let ((command (if automatic command (read-string "Command: " command)))
12997           (buffer-read-only nil))
12998       (erase-buffer)
12999       (insert "$ " command "\n\n")
13000       (if gnus-view-pseudo-asynchronously
13001           (start-process "gnus-execute" nil "sh" "-c" command)
13002         (call-process "sh" nil t nil "-c" command)))))
13003
13004 (defun gnus-copy-file (file &optional to)
13005   "Copy FILE to TO."
13006   (interactive
13007    (list (read-file-name "Copy file: " default-directory)
13008          (read-file-name "Copy file to: " default-directory)))
13009   (gnus-set-global-variables)
13010   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13011   (and (file-directory-p to)
13012        (setq to (concat (file-name-as-directory to)
13013                         (file-name-nondirectory file))))
13014   (copy-file file to))
13015
13016 ;; Summary kill commands.
13017
13018 (defun gnus-summary-edit-global-kill (article)
13019   "Edit the \"global\" kill file."
13020   (interactive (list (gnus-summary-article-number)))
13021   (gnus-set-global-variables)
13022   (gnus-group-edit-global-kill article))
13023
13024 (defun gnus-summary-edit-local-kill ()
13025   "Edit a local kill file applied to the current newsgroup."
13026   (interactive)
13027   (gnus-set-global-variables)
13028   (setq gnus-current-headers (gnus-summary-article-header))
13029   (gnus-set-global-variables)
13030   (gnus-group-edit-local-kill
13031    (gnus-summary-article-number) gnus-newsgroup-name))
13032
13033 \f
13034 ;;;
13035 ;;; Gnus article mode
13036 ;;;
13037
13038 (put 'gnus-article-mode 'mode-class 'special)
13039
13040 (if gnus-article-mode-map
13041     nil
13042   (setq gnus-article-mode-map (make-keymap))
13043   (suppress-keymap gnus-article-mode-map)
13044
13045   (gnus-define-keys gnus-article-mode-map
13046     " " gnus-article-goto-next-page
13047     "\177" gnus-article-goto-prev-page
13048     [delete] gnus-article-goto-prev-page
13049     "\C-c^" gnus-article-refer-article
13050     "h" gnus-article-show-summary
13051     "s" gnus-article-show-summary
13052     "\C-c\C-m" gnus-article-mail
13053     "?" gnus-article-describe-briefly
13054     gnus-mouse-2 gnus-article-push-button
13055     "\r" gnus-article-press-button
13056     "\t" gnus-article-next-button
13057     "\M-\t" gnus-article-prev-button
13058     "\C-c\C-b" gnus-bug)
13059
13060   (substitute-key-definition
13061    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13062
13063 (defun gnus-article-mode ()
13064   "Major mode for displaying an article.
13065
13066 All normal editing commands are switched off.
13067
13068 The following commands are available:
13069
13070 \\<gnus-article-mode-map>
13071 \\[gnus-article-next-page]\t Scroll the article one page forwards
13072 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13073 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13074 \\[gnus-article-show-summary]\t Display the summary buffer
13075 \\[gnus-article-mail]\t Send a reply to the address near point
13076 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13077 \\[gnus-info-find-node]\t Go to the Gnus info node"
13078   (interactive)
13079   (when (and menu-bar-mode
13080              (gnus-visual-p 'article-menu 'menu))
13081     (gnus-article-make-menu-bar))
13082   (kill-all-local-variables)
13083   (gnus-simplify-mode-line)
13084   (setq mode-name "Article")
13085   (setq major-mode 'gnus-article-mode)
13086   (make-local-variable 'minor-mode-alist)
13087   (or (assq 'gnus-show-mime minor-mode-alist)
13088       (setq minor-mode-alist
13089             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13090   (use-local-map gnus-article-mode-map)
13091   (make-local-variable 'page-delimiter)
13092   (setq page-delimiter gnus-page-delimiter)
13093   (buffer-disable-undo (current-buffer))
13094   (setq buffer-read-only t)             ;Disable modification
13095   (run-hooks 'gnus-article-mode-hook))
13096
13097 (defun gnus-article-setup-buffer ()
13098   "Initialize the article buffer."
13099   (let* ((name (if gnus-single-article-buffer "*Article*"
13100                  (concat "*Article " gnus-newsgroup-name "*")))
13101          (original
13102           (progn (string-match "\\*Article" name)
13103                  (concat " *Original Article"
13104                          (substring name (match-end 0))))))
13105     (setq gnus-article-buffer name)
13106     (setq gnus-original-article-buffer original)
13107     ;; This might be a variable local to the summary buffer.
13108     (unless gnus-single-article-buffer
13109       (save-excursion
13110         (set-buffer gnus-summary-buffer)
13111         (setq gnus-article-buffer name)
13112         (setq gnus-original-article-buffer original)
13113         (gnus-set-global-variables))
13114       (make-local-variable 'gnus-summary-buffer))
13115     (if (get-buffer name)
13116         (save-excursion
13117           (set-buffer name)
13118           (buffer-disable-undo (current-buffer))
13119           (setq buffer-read-only t)
13120           (gnus-add-current-to-buffer-list)
13121           (or (eq major-mode 'gnus-article-mode)
13122               (gnus-article-mode))
13123           (current-buffer))
13124       (save-excursion
13125         (set-buffer (get-buffer-create name))
13126         (gnus-add-current-to-buffer-list)
13127         (gnus-article-mode)
13128         (current-buffer)))))
13129
13130 ;; Set article window start at LINE, where LINE is the number of lines
13131 ;; from the head of the article.
13132 (defun gnus-article-set-window-start (&optional line)
13133   (set-window-start
13134    (get-buffer-window gnus-article-buffer)
13135    (save-excursion
13136      (set-buffer gnus-article-buffer)
13137      (goto-char (point-min))
13138      (if (not line)
13139          (point-min)
13140        (gnus-message 6 "Moved to bookmark")
13141        (search-forward "\n\n" nil t)
13142        (forward-line line)
13143        (point)))))
13144
13145 (defun gnus-kill-all-overlays ()
13146   "Delete all overlays in the current buffer."
13147   (when (fboundp 'overlay-lists)
13148     (let* ((overlayss (overlay-lists))
13149            (buffer-read-only nil)
13150            (overlays (nconc (car overlayss) (cdr overlayss))))
13151       (while overlays
13152         (delete-overlay (pop overlays))))))
13153
13154 (defun gnus-request-article-this-buffer (article group)
13155   "Get an article and insert it into this buffer."
13156   (prog1
13157       (save-excursion
13158         (erase-buffer)
13159         (gnus-kill-all-overlays)
13160         (setq group (or group gnus-newsgroup-name))
13161
13162         ;; Open server if it has closed.
13163         (gnus-check-server (gnus-find-method-for-group group))
13164
13165         ;; Using `gnus-request-article' directly will insert the article into
13166         ;; `nntp-server-buffer' - so we'll save some time by not having to
13167         ;; copy it from the server buffer into the article buffer.
13168
13169         ;; We only request an article by message-id when we do not have the
13170         ;; headers for it, so we'll have to get those.
13171         (when (stringp article)
13172           (let ((gnus-override-method gnus-refer-article-method))
13173             (gnus-read-header article)))
13174
13175         ;; If the article number is negative, that means that this article
13176         ;; doesn't belong in this newsgroup (possibly), so we find its
13177         ;; message-id and request it by id instead of number.
13178         (when (and (numberp article)
13179                    gnus-summary-buffer
13180                    (get-buffer gnus-summary-buffer)
13181                    (buffer-name (get-buffer gnus-summary-buffer)))
13182           (save-excursion
13183             (set-buffer gnus-summary-buffer)
13184             (let ((header (gnus-summary-article-header article)))
13185               (if (< article 0)
13186                   (cond 
13187                    ((memq article gnus-newsgroup-sparse)
13188                     ;; This is a sparse gap article.
13189                     (setq article (mail-header-id header)))
13190                    ((vectorp header)
13191                     ;; It's a real article.
13192                     (setq article (mail-header-id header)))
13193                    (t
13194                     ;; It is an extracted pseudo-article.
13195                     (setq article 'pseudo)
13196                     (gnus-request-pseudo-article header))))
13197                 
13198               (let ((method (gnus-find-method-for-group 
13199                              gnus-newsgroup-name)))
13200                 (if (not (eq (car method) 'nneething))
13201                     ()
13202                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13203                                      (mail-header-subject header))))
13204                     (if (file-directory-p dir)
13205                         (progn
13206                           (setq article 'nneething)
13207                           (gnus-group-enter-directory dir)))))))))
13208
13209         (cond
13210          ;; We first check `gnus-original-article-buffer'.
13211          ((and (equal (car gnus-original-article) group)
13212                (eq (cdr gnus-original-article) article)
13213                (get-buffer gnus-original-article-buffer))
13214           (insert-buffer-substring gnus-original-article-buffer)
13215           'article)
13216          ;; Check the backlog.
13217          ((and gnus-keep-backlog
13218                (gnus-backlog-request-article group article (current-buffer)))
13219           'article)
13220          ;; Check the cache.
13221          ((and gnus-use-cache
13222                (numberp article)
13223                (gnus-cache-request-article article group))
13224           'article)
13225          ;; Get the article and put into the article buffer.
13226          ((or (stringp article) (numberp article))
13227           (let ((gnus-override-method
13228                  (and (stringp article) gnus-refer-article-method))
13229                 (buffer-read-only nil))
13230             (erase-buffer)
13231             (gnus-kill-all-overlays)
13232             (if (gnus-request-article article group (current-buffer))
13233                 (progn
13234                   (and gnus-keep-backlog
13235                        (gnus-backlog-enter-article
13236                         group article (current-buffer)))
13237                   'article))))
13238          ;; It was a pseudo.
13239          (t article)))
13240
13241     ;; Take the article from the original article buffer
13242     ;; and place it in the buffer it's supposed to be in.
13243     (when (and (get-buffer gnus-article-buffer)
13244                (equal (buffer-name (current-buffer))
13245                       (buffer-name (get-buffer gnus-article-buffer))))
13246       (save-excursion
13247         (setq gnus-original-article (cons group article))
13248         (if (get-buffer gnus-original-article-buffer)
13249             (set-buffer (get-buffer gnus-original-article-buffer))
13250           (set-buffer (get-buffer-create gnus-original-article-buffer))
13251           (buffer-disable-undo (current-buffer))
13252           (setq major-mode 'gnus-original-article-mode)
13253           (setq buffer-read-only t)
13254           (gnus-add-current-to-buffer-list))
13255         (let (buffer-read-only)
13256           (erase-buffer)
13257           (insert-buffer-substring gnus-article-buffer))))
13258     
13259     ;; Update sparse articles.
13260     (when (memq article gnus-newsgroup-sparse)
13261       (gnus-summary-update-article article))))
13262
13263 (defun gnus-read-header (id)
13264   "Read the headers of article ID and enter them into the Gnus system."
13265   (let ((group gnus-newsgroup-name)
13266         header where)
13267     ;; First we check to see whether the header in question is already
13268     ;; fetched.
13269     (if (stringp id)
13270         ;; This is a Message-ID.
13271         (setq header (gnus-id-to-header id))
13272       ;; This is an article number.
13273       (setq header (gnus-summary-article-header id)))
13274     (if header
13275         ;; We have found the header.
13276         header
13277       ;; We have to really fetch the header to this article.
13278       (when (setq where
13279                   (if (gnus-check-backend-function 'request-head group)
13280                       (gnus-request-head id group)
13281                     (gnus-request-article id group)))
13282         (save-excursion
13283           (set-buffer nntp-server-buffer)
13284           (and (search-forward "\n\n" nil t)
13285                (delete-region (1- (point)) (point-max)))
13286           (goto-char (point-max))
13287           (insert ".\n")
13288           (goto-char (point-min))
13289           (insert "211 ")
13290           (princ (cond
13291                   ((numberp id) id)
13292                   ((cdr where) (cdr where))
13293                   (t gnus-reffed-article-number))
13294                  (current-buffer))
13295           (insert " Article retrieved.\n"))
13296         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13297             () ; Malformed head.
13298           (if (and (stringp id)
13299                    (not (string= (gnus-group-real-name group)
13300                                  (car where))))
13301               ;; If we fetched by Message-ID and the article came
13302               ;; from a different group, we fudge some bogus article
13303               ;; numbers for this article.
13304               (mail-header-set-number header gnus-reffed-article-number))
13305           (decf gnus-reffed-article-number)
13306           (push header gnus-newsgroup-headers)
13307           (setq gnus-current-headers header)
13308           (push (mail-header-number header) gnus-newsgroup-limit)
13309           header)))))
13310
13311 (defun gnus-article-prepare (article &optional all-headers header)
13312   "Prepare ARTICLE in article mode buffer.
13313 ARTICLE should either be an article number or a Message-ID.
13314 If ARTICLE is an id, HEADER should be the article headers.
13315 If ALL-HEADERS is non-nil, no headers are hidden."
13316   (save-excursion
13317     ;; Make sure we start in a summary buffer.
13318     (unless (eq major-mode 'gnus-summary-mode)
13319       (set-buffer gnus-summary-buffer))
13320     (setq gnus-summary-buffer (current-buffer))
13321     ;; Make sure the connection to the server is alive.
13322     (unless (gnus-server-opened
13323              (gnus-find-method-for-group gnus-newsgroup-name))
13324       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13325       (gnus-request-group gnus-newsgroup-name t))
13326     (let* ((article (if header (mail-header-number header) article))
13327            (summary-buffer (current-buffer))
13328            (internal-hook gnus-article-internal-prepare-hook)
13329            (group gnus-newsgroup-name)
13330            result)
13331       (save-excursion
13332         (gnus-article-setup-buffer)
13333         (set-buffer gnus-article-buffer)
13334         ;; Deactivate active regions.
13335         (when (and (boundp 'transient-mark-mode)
13336                    transient-mark-mode)
13337           (setq mark-active nil))
13338         (if (not (setq result (let ((buffer-read-only nil))
13339                                 (gnus-request-article-this-buffer
13340                                  article group))))
13341             ;; There is no such article.
13342             (save-excursion
13343               (when (and (numberp article)
13344                          (not (memq article gnus-newsgroup-sparse)))
13345                 (setq gnus-article-current
13346                       (cons gnus-newsgroup-name article))
13347                 (set-buffer gnus-summary-buffer)
13348                 (setq gnus-current-article article)
13349                 (gnus-summary-mark-article article gnus-canceled-mark))
13350               (unless (memq article gnus-newsgroup-sparse)
13351                 (gnus-message
13352                  1 "No such article (may have expired or been canceled)")
13353                 (ding)
13354                 nil))
13355           (if (or (eq result 'pseudo) (eq result 'nneething))
13356               (progn
13357                 (save-excursion
13358                   (set-buffer summary-buffer)
13359                   (setq gnus-last-article gnus-current-article
13360                         gnus-newsgroup-history (cons gnus-current-article
13361                                                      gnus-newsgroup-history)
13362                         gnus-current-article 0
13363                         gnus-current-headers nil
13364                         gnus-article-current nil)
13365                   (if (eq result 'nneething)
13366                       (gnus-configure-windows 'summary)
13367                     (gnus-configure-windows 'article))
13368                   (gnus-set-global-variables))
13369                 (gnus-set-mode-line 'article))
13370             ;; The result from the `request' was an actual article -
13371             ;; or at least some text that is now displayed in the
13372             ;; article buffer.
13373             (if (and (numberp article)
13374                      (not (eq article gnus-current-article)))
13375                 ;; Seems like a new article has been selected.
13376                 ;; `gnus-current-article' must be an article number.
13377                 (save-excursion
13378                   (set-buffer summary-buffer)
13379                   (setq gnus-last-article gnus-current-article
13380                         gnus-newsgroup-history (cons gnus-current-article
13381                                                      gnus-newsgroup-history)
13382                         gnus-current-article article
13383                         gnus-current-headers
13384                         (gnus-summary-article-header gnus-current-article)
13385                         gnus-article-current
13386                         (cons gnus-newsgroup-name gnus-current-article))
13387                   (unless (vectorp gnus-current-headers)
13388                     (setq gnus-current-headers nil))
13389                   (gnus-summary-show-thread)
13390                   (run-hooks 'gnus-mark-article-hook)
13391                   (gnus-set-mode-line 'summary)
13392                   (and (gnus-visual-p 'article-highlight 'highlight)
13393                        (run-hooks 'gnus-visual-mark-article-hook))
13394                   ;; Set the global newsgroup variables here.
13395                   ;; Suggested by Jim Sisolak
13396                   ;; <sisolak@trans4.neep.wisc.edu>.
13397                   (gnus-set-global-variables)
13398                   (setq gnus-have-all-headers
13399                         (or all-headers gnus-show-all-headers))
13400                   (and gnus-use-cache
13401                        (vectorp (gnus-summary-article-header article))
13402                        (gnus-cache-possibly-enter-article
13403                         group article
13404                         (gnus-summary-article-header article)
13405                         (memq article gnus-newsgroup-marked)
13406                         (memq article gnus-newsgroup-dormant)
13407                         (memq article gnus-newsgroup-unreads)))))
13408             ;; Hooks for getting information from the article.
13409             ;; This hook must be called before being narrowed.
13410             (let (buffer-read-only)
13411               (run-hooks 'internal-hook)
13412               (run-hooks 'gnus-article-prepare-hook)
13413               ;; Decode MIME message.
13414               (if gnus-show-mime
13415                   (if (or (not gnus-strict-mime)
13416                           (gnus-fetch-field "Mime-Version"))
13417                       (funcall gnus-show-mime-method)
13418                     (funcall gnus-decode-encoded-word-method)))
13419               ;; Perform the article display hooks.
13420               (run-hooks 'gnus-article-display-hook))
13421             ;; Do page break.
13422             (goto-char (point-min))
13423             (and gnus-break-pages (gnus-narrow-to-page))
13424             (gnus-set-mode-line 'article)
13425             (gnus-configure-windows 'article)
13426             (goto-char (point-min))
13427             t))))))
13428
13429 (defun gnus-article-show-all-headers ()
13430   "Show all article headers in article mode buffer."
13431   (save-excursion
13432     (gnus-article-setup-buffer)
13433     (set-buffer gnus-article-buffer)
13434     (let ((buffer-read-only nil))
13435       (gnus-unhide-text (point-min) (point-max)))))
13436
13437 (defun gnus-article-hide-headers-if-wanted ()
13438   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13439 Provided for backwards compatibility."
13440   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13441       gnus-inhibit-hiding
13442       (gnus-article-hide-headers)))
13443
13444 (defun gnus-article-hide-headers (&optional arg delete)
13445   "Toggle whether to hide unwanted headers and possibly sort them as well.
13446 If given a negative prefix, always show; if given a positive prefix,
13447 always hide."
13448   (interactive "P")
13449   (unless (gnus-article-check-hidden-text 'headers arg)
13450     ;; This function might be inhibited.
13451     (unless gnus-inhibit-hiding
13452       (save-excursion
13453         (set-buffer gnus-article-buffer)
13454         (save-restriction
13455           (let ((buffer-read-only nil)
13456                 (props (nconc (list 'gnus-type 'headers)
13457                               gnus-hidden-properties))
13458                 (ignored (when (not (stringp gnus-visible-headers))
13459                            (cond ((stringp gnus-ignored-headers)
13460                                   gnus-ignored-headers)
13461                                  ((listp gnus-ignored-headers)
13462                                   (mapconcat 'identity gnus-ignored-headers
13463                                              "\\|")))))
13464                 (visible
13465                  (cond ((stringp gnus-visible-headers)
13466                         gnus-visible-headers)
13467                        ((and gnus-visible-headers
13468                              (listp gnus-visible-headers))
13469                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13470                 want-list beg)
13471             ;; First we narrow to just the headers.
13472             (widen)
13473             (goto-char (point-min))
13474             ;; Hide any "From " lines at the beginning of (mail) articles.
13475             (while (looking-at "From ")
13476               (forward-line 1))
13477             (unless (bobp)
13478               (gnus-hide-text (point-min) (point) props))
13479             ;; Then treat the rest of the header lines.
13480             (narrow-to-region
13481              (point)
13482              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13483             ;; Then we use the two regular expressions
13484             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13485             ;; select which header lines is to remain visible in the
13486             ;; article buffer.
13487             (goto-char (point-min))
13488             (while (re-search-forward "^[^ \t]*:" nil t)
13489               (beginning-of-line)
13490               ;; We add the headers we want to keep to a list and delete
13491               ;; them from the buffer.
13492               (if (or (and visible (looking-at visible))
13493                       (and ignored (not (looking-at ignored))))
13494                   (progn
13495                     (push (buffer-substring
13496                            (setq beg (point))
13497                            (progn
13498                              (forward-line 1)
13499                              ;; Be sure to get multi-line headers...
13500                              (re-search-forward "^[^ \t]*:" nil t)
13501                              (beginning-of-line)
13502                              (point)))
13503                           want-list)
13504                     (delete-region beg (point)))
13505                 (forward-line 1)))
13506             ;; Sort the headers that we want to display.
13507             (setq want-list (sort want-list 'gnus-article-header-less))
13508             (goto-char (point-min))
13509             (while want-list
13510               (insert (pop want-list)))
13511             ;; We make the unwanted headers invisible.
13512             (if delete
13513                 (delete-region (point-min) (point-max))
13514               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13515               (gnus-hide-text-type (point) (point-max) 'headers))))))))
13516
13517 (defsubst gnus-article-header-rank (header)
13518   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13519   (let ((list gnus-sorted-header-list)
13520         (i 0))
13521     (while list
13522       (when (string-match (car list) header)
13523         (setq list nil))
13524       (setq list (cdr list))
13525       (incf i))
13526     i))
13527
13528 (defun gnus-article-header-less (h1 h2)
13529   "Say whether string H1 is \"less\" than string H2."
13530   (< (gnus-article-header-rank h1)
13531      (gnus-article-header-rank h2)))
13532
13533 (defun gnus-article-hide-boring-headers (&optional arg)
13534   "Toggle hiding of headers that aren't very interesting.
13535 If given a negative prefix, always show; if given a positive prefix,
13536 always hide."
13537   (interactive "P")
13538   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13539     (save-excursion
13540       (set-buffer gnus-article-buffer)
13541       (save-restriction
13542         (let ((buffer-read-only nil)
13543               (list gnus-boring-article-headers)
13544               (inhibit-point-motion-hooks t)
13545               elem)
13546           (nnheader-narrow-to-headers)
13547           (while list
13548             (setq elem (pop list))
13549             (goto-char (point-min))
13550             (cond
13551              ;; Hide empty headers.
13552              ((eq elem 'empty)
13553               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13554                 (forward-line -1)
13555                 (gnus-hide-text-type
13556                  (progn (beginning-of-line) (point))
13557                  (progn 
13558                    (end-of-line)
13559                    (if (re-search-forward "^[^ \t]" nil t)
13560                        (match-beginning 0)
13561                      (point-max)))
13562                  'boring-headers)))
13563              ;; Hide boring Newsgroups header.
13564              ((eq elem 'newsgroups)
13565               (when (equal (mail-fetch-field "newsgroups")
13566                            (gnus-group-real-name gnus-newsgroup-name))
13567                 (gnus-article-hide-header "newsgroups")))
13568              ((eq elem 'followup-to)
13569               (when (equal (mail-fetch-field "followup-to")
13570                            (mail-fetch-field "newsgroups"))
13571                 (gnus-article-hide-header "followup-to")))
13572              ((eq elem 'reply-to)
13573               (let ((from (mail-fetch-field "from"))
13574                     (reply-to (mail-fetch-field "reply-to")))
13575                 (when (and
13576                        from reply-to
13577                        (equal 
13578                         (nth 1 (mail-extract-address-components from))
13579                         (nth 1 (mail-extract-address-components reply-to))))
13580                   (gnus-article-hide-header "reply-to"))))
13581              ((eq elem 'date)
13582               (let ((date (mail-fetch-field "date")))
13583                 (when (and date
13584                            (< (gnus-days-between date (current-time-string))
13585                               4))
13586                   (gnus-article-hide-header "date")))))))))))
13587
13588 (defun gnus-article-hide-header (header)
13589   (save-excursion
13590     (goto-char (point-min))
13591     (when (re-search-forward (concat "^" header ":") nil t)
13592       (gnus-hide-text-type
13593        (progn (beginning-of-line) (point))
13594        (progn 
13595          (end-of-line)
13596          (if (re-search-forward "^[^ \t]" nil t)
13597              (match-beginning 0)
13598            (point-max)))
13599        'boring-headers))))
13600
13601 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13602 (defun gnus-article-treat-overstrike ()
13603   "Translate overstrikes into bold text."
13604   (interactive)
13605   (save-excursion
13606     (set-buffer gnus-article-buffer)
13607     (let ((buffer-read-only nil))
13608       (while (search-forward "\b" nil t)
13609         (let ((next (following-char))
13610               (previous (char-after (- (point) 2))))
13611           (cond ((eq next previous)
13612                  (put-text-property (- (point) 2) (point) 'invisible t)
13613                  (put-text-property (point) (1+ (point)) 'face 'bold))
13614                 ((eq next ?_)
13615                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13616                  (put-text-property
13617                   (- (point) 2) (1- (point)) 'face 'underline))
13618                 ((eq previous ?_)
13619                  (put-text-property (- (point) 2) (point) 'invisible t)
13620                  (put-text-property
13621                   (point) (1+ (point))  'face 'underline))))))))
13622
13623 (defun gnus-article-word-wrap ()
13624   "Format too long lines."
13625   (interactive)
13626   (save-excursion
13627     (set-buffer gnus-article-buffer)
13628     (let ((buffer-read-only nil))
13629       (widen)
13630       (goto-char (point-min))
13631       (search-forward "\n\n" nil t)
13632       (end-of-line 1)
13633       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13634             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13635             (adaptive-fill-mode t))
13636         (while (not (eobp))
13637           (and (>= (current-column) (min fill-column (window-width)))
13638                (/= (preceding-char) ?:)
13639                (fill-paragraph nil))
13640           (end-of-line 2))))))
13641
13642 (defun gnus-article-remove-cr ()
13643   "Remove carriage returns from an article."
13644   (interactive)
13645   (save-excursion
13646     (set-buffer gnus-article-buffer)
13647     (let ((buffer-read-only nil))
13648       (goto-char (point-min))
13649       (while (search-forward "\r" nil t)
13650         (replace-match "" t t)))))
13651
13652 (defun gnus-article-remove-trailing-blank-lines ()
13653   "Remove all trailing blank lines from the article."
13654   (interactive)
13655   (save-excursion
13656     (set-buffer gnus-article-buffer)
13657     (let ((buffer-read-only nil))
13658       (goto-char (point-max))
13659       (delete-region
13660        (point)
13661        (progn
13662          (while (looking-at "^[ \t]*$")
13663            (forward-line -1))
13664          (forward-line 1)
13665          (point))))))
13666
13667 (defun gnus-article-display-x-face (&optional force)
13668   "Look for an X-Face header and display it if present."
13669   (interactive (list 'force))
13670   (save-excursion
13671     (set-buffer gnus-article-buffer)
13672     ;; Delete the old process, if any.
13673     (when (process-status "gnus-x-face")
13674       (delete-process "gnus-x-face"))
13675     (let ((inhibit-point-motion-hooks t)
13676           (case-fold-search nil)
13677           from)
13678       (save-restriction
13679         (nnheader-narrow-to-headers)
13680         (setq from (mail-fetch-field "from"))
13681         (goto-char (point-min))
13682         (when (and gnus-article-x-face-command
13683                    (or force
13684                        ;; Check whether this face is censored.
13685                        (not gnus-article-x-face-too-ugly)
13686                        (and gnus-article-x-face-too-ugly from
13687                             (not (string-match gnus-article-x-face-too-ugly
13688                                                from))))
13689                    ;; Has to be present.
13690                    (re-search-forward "^X-Face: " nil t))
13691           ;; We now have the area of the buffer where the X-Face is stored.
13692           (let ((beg (point))
13693                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13694             ;; We display the face.
13695             (if (symbolp gnus-article-x-face-command)
13696                 ;; The command is a lisp function, so we call it.
13697                 (if (gnus-functionp gnus-article-x-face-command)
13698                     (funcall gnus-article-x-face-command beg end)
13699                   (error "%s is not a function" gnus-article-x-face-command))
13700               ;; The command is a string, so we interpret the command
13701               ;; as a, well, command, and fork it off.
13702               (let ((process-connection-type nil))
13703                 (process-kill-without-query
13704                  (start-process
13705                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13706                 (process-send-region "gnus-x-face" beg end)
13707                 (process-send-eof "gnus-x-face")))))))))
13708
13709 (defun gnus-headers-decode-quoted-printable ()
13710   "Hack to remove QP encoding from headers."
13711   (let ((case-fold-search t)
13712         (inhibit-point-motion-hooks t)
13713         string)
13714     (goto-char (point-min))
13715     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13716       (setq string (match-string 1))
13717       (narrow-to-region (match-beginning 0) (match-end 0))
13718       (delete-region (point-min) (point-max))
13719       (insert string)
13720       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13721       (subst-char-in-region (point-min) (point-max) ?_ ? )
13722       (widen)
13723       (goto-char (point-min)))))
13724
13725 (defun gnus-article-de-quoted-unreadable (&optional force)
13726   "Do a naive translation of a quoted-printable-encoded article.
13727 This is in no way, shape or form meant as a replacement for real MIME
13728 processing, but is simply a stop-gap measure until MIME support is
13729 written.
13730 If FORCE, decode the article whether it is marked as quoted-printable
13731 or not."
13732   (interactive (list 'force))
13733   (save-excursion
13734     (set-buffer gnus-article-buffer)
13735     (let ((case-fold-search t)
13736           (buffer-read-only nil)
13737           (type (gnus-fetch-field "content-transfer-encoding")))
13738       (when (or force
13739                 (and type (string-match "quoted-printable" type)))
13740         (gnus-headers-decode-quoted-printable)
13741         (goto-char (point-min))
13742         (search-forward "\n\n" nil 'move)
13743         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13744
13745 (defun gnus-mime-decode-quoted-printable (from to)
13746   "Decode Quoted-Printable in the region between FROM and TO."
13747   (goto-char from)
13748   (while (search-forward "=" to t)
13749     (cond ((eq (following-char) ?\n)
13750            (delete-char -1)
13751            (delete-char 1))
13752           ((looking-at "[0-9A-F][0-9A-F]")
13753            (delete-char -1)
13754            (insert (hexl-hex-string-to-integer
13755                     (buffer-substring (point) (+ 2 (point)))))
13756            (delete-char 2))
13757           ((looking-at "=")
13758            (delete-char 1))
13759           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13760
13761 (defun gnus-article-hide-pgp (&optional arg)
13762   "Toggle hiding of any PGP headers and signatures in the current article.
13763 If given a negative prefix, always show; if given a positive prefix,
13764 always hide."
13765   (interactive "P")
13766   (unless (gnus-article-check-hidden-text 'pgp arg)
13767     (save-excursion
13768       (set-buffer gnus-article-buffer)
13769       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13770             buffer-read-only beg end)
13771         (widen)
13772         (goto-char (point-min))
13773         ;; Hide the "header".
13774         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13775              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13776         (setq beg (point))
13777         ;; Hide the actual signature.
13778         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13779              (setq end (match-beginning 0))
13780              (gnus-hide-text
13781               (match-beginning 0)
13782               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13783                   (match-end 0)
13784                 ;; Perhaps we shouldn't hide to the end of the buffer
13785                 ;; if there is no end to the signature?
13786                 (point-max))
13787               props))
13788         ;; Hide "- " PGP quotation markers.
13789         (when (and beg end)
13790           (narrow-to-region beg end)
13791           (goto-char (point-min))
13792           (while (re-search-forward "^- " nil t)
13793             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13794           (widen))))))
13795
13796 (defun gnus-article-hide-signature (&optional arg)
13797   "Hide the signature in the current article.
13798 If given a negative prefix, always show; if given a positive prefix,
13799 always hide."
13800   (interactive "P")
13801   (unless (gnus-article-check-hidden-text 'signature arg)
13802     (save-excursion
13803       (set-buffer gnus-article-buffer)
13804       (save-restriction
13805         (let ((buffer-read-only nil))
13806           (when (gnus-narrow-to-signature)
13807             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13808
13809 (defun gnus-article-strip-leading-blank-lines ()
13810   "Remove all blank lines from the beginning of the article."
13811   (interactive)
13812   (save-excursion
13813     (set-buffer gnus-article-buffer)
13814     (let (buffer-read-only)
13815       (goto-char (point-min))
13816       (when (search-forward "\n\n" nil t)
13817         (while (looking-at "[ \t]$")
13818           (gnus-delete-line))))))
13819
13820 (defun gnus-narrow-to-signature ()
13821   "Narrow to the signature."
13822   (widen)
13823   (goto-char (point-max))
13824   (when (re-search-backward gnus-signature-separator nil t)
13825     (forward-line 1)
13826     (when (or (null gnus-signature-limit)
13827               (and (numberp gnus-signature-limit)
13828                    (< (- (point-max) (point)) gnus-signature-limit))
13829               (and (gnus-functionp gnus-signature-limit)
13830                    (funcall gnus-signature-limit))
13831               (and (stringp gnus-signature-limit)
13832                    (not (re-search-forward gnus-signature-limit nil t))))
13833       (narrow-to-region (point) (point-max))
13834       t)))
13835
13836 (defun gnus-article-check-hidden-text (type arg)
13837   "Return nil if hiding is necessary."
13838   (save-excursion
13839     (set-buffer gnus-article-buffer)
13840     (let ((hide (gnus-article-hidden-text-p type)))
13841       (cond ((or (and (null arg) (eq hide 'hidden))
13842                  (and arg (< 0 (prefix-numeric-value arg))))
13843              (gnus-article-show-hidden-text type))
13844             ((eq hide 'shown)
13845              (gnus-article-show-hidden-text type t))
13846             (t nil)))))
13847
13848 (defun gnus-article-hidden-text-p (type)
13849   "Say whether the current buffer contains hidden text of type TYPE."
13850   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13851     (when pos
13852       (if (get-text-property pos 'invisible)
13853           'hidden
13854         'shown))))
13855
13856 (defun gnus-article-hide (&optional arg force)
13857   "Hide all the gruft in the current article.
13858 This means that PGP stuff, signatures, cited text and (some)
13859 headers will be hidden.
13860 If given a prefix, show the hidden text instead."
13861   (interactive (list current-prefix-arg 'force))
13862   (gnus-article-hide-headers arg)
13863   (gnus-article-hide-pgp arg)
13864   (gnus-article-hide-citation-maybe arg force)
13865   (gnus-article-hide-signature arg))
13866
13867 (defun gnus-article-show-hidden-text (type &optional hide)
13868   "Show all hidden text of type TYPE.
13869 If HIDE, hide the text instead."
13870   (save-excursion
13871     (set-buffer gnus-article-buffer)
13872     (let ((buffer-read-only nil)
13873           (inhibit-point-motion-hooks t)
13874           (beg (point-min)))
13875       (while (gnus-goto-char (text-property-any
13876                               beg (point-max) 'gnus-type type))
13877         (setq beg (point))
13878         (forward-char)
13879         (if hide
13880             (gnus-hide-text beg (point) gnus-hidden-properties)
13881           (gnus-unhide-text beg (point)))
13882         (setq beg (point)))
13883       t)))
13884
13885 (defvar gnus-article-time-units
13886   `((year . ,(* 365.25 24 60 60))
13887     (week . ,(* 7 24 60 60))
13888     (day . ,(* 24 60 60))
13889     (hour . ,(* 60 60))
13890     (minute . 60)
13891     (second . 1))
13892   "Mapping from time units to seconds.")
13893
13894 (defun gnus-article-date-ut (&optional type highlight)
13895   "Convert DATE date to universal time in the current article.
13896 If TYPE is `local', convert to local time; if it is `lapsed', output
13897 how much time has lapsed since DATE."
13898   (interactive (list 'ut t))
13899   (let* ((header (or gnus-current-headers
13900                      (gnus-summary-article-header) ""))
13901          (date (and (vectorp header) (mail-header-date header)))
13902          (date-regexp "^Date: \\|^X-Sent: ")
13903          (now (current-time))
13904          (inhibit-point-motion-hooks t))
13905     (when (and date (not (string= date "")))
13906       (save-excursion
13907         (set-buffer gnus-article-buffer)
13908         (save-restriction
13909           (nnheader-narrow-to-headers)
13910           (let ((buffer-read-only nil))
13911             ;; Delete any old Date headers.
13912             (if (zerop (nnheader-remove-header date-regexp t))
13913                 (beginning-of-line)
13914               (goto-char (point-max)))
13915             (insert
13916              (cond
13917               ;; Convert to the local timezone.  We have to slap a
13918               ;; `condition-case' round the calls to the timezone
13919               ;; functions since they aren't particularly resistant to
13920               ;; buggy dates.
13921               ((eq type 'local)
13922                (concat "Date: " (condition-case ()
13923                                     (timezone-make-date-arpa-standard date)
13924                                   (error date))
13925                        "\n"))
13926               ;; Convert to Universal Time.
13927               ((eq type 'ut)
13928                (concat "Date: "
13929                        (condition-case ()
13930                            (timezone-make-date-arpa-standard date nil "UT")
13931                          (error date))
13932                        "\n"))
13933               ;; Get the original date from the article.
13934               ((eq type 'original)
13935                (concat "Date: " date "\n"))
13936               ;; Do an X-Sent lapsed format.
13937               ((eq type 'lapsed)
13938                ;; If the date is seriously mangled, the timezone
13939                ;; functions are liable to bug out, so we condition-case
13940                ;; the entire thing.
13941                (let* ((real-time
13942                        (condition-case ()
13943                            (gnus-time-minus
13944                             (gnus-encode-date
13945                              (timezone-make-date-arpa-standard
13946                               (current-time-string now)
13947                               (current-time-zone now) "UT"))
13948                             (gnus-encode-date
13949                              (timezone-make-date-arpa-standard
13950                               date nil "UT")))
13951                          (error '(0 0))))
13952                       (real-sec (+ (* (float (car real-time)) 65536)
13953                                    (cadr real-time)))
13954                       (sec (abs real-sec))
13955                       num prev)
13956                  (if (zerop sec)
13957                      "X-Sent: Now\n"
13958                    (concat
13959                     "X-Sent: "
13960                     ;; This is a bit convoluted, but basically we go
13961                     ;; through the time units for years, weeks, etc,
13962                     ;; and divide things to see whether that results
13963                     ;; in positive answers.
13964                     (mapconcat
13965                      (lambda (unit)
13966                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13967                            ;; The (remaining) seconds are too few to
13968                            ;; be divided into this time unit.
13969                            ""
13970                          ;; It's big enough, so we output it.
13971                          (setq sec (- sec (* num (cdr unit))))
13972                          (prog1
13973                              (concat (if prev ", " "") (int-to-string
13974                                                         (floor num))
13975                                      " " (symbol-name (car unit))
13976                                      (if (> num 1) "s" ""))
13977                            (setq prev t))))
13978                      gnus-article-time-units "")
13979                     ;; If dates are odd, then it might appear like the
13980                     ;; article was sent in the future.
13981                     (if (> real-sec 0)
13982                         " ago\n"
13983                       " in the future\n")))))
13984               (t
13985                (error "Unknown conversion type: %s" type)))))
13986           ;; Do highlighting.
13987           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13988             (gnus-article-highlight-headers)))))))
13989
13990 (defun gnus-article-date-local (&optional highlight)
13991   "Convert the current article date to the local timezone."
13992   (interactive (list t))
13993   (gnus-article-date-ut 'local highlight))
13994
13995 (defun gnus-article-date-original (&optional highlight)
13996   "Convert the current article date to what it was originally.
13997 This is only useful if you have used some other date conversion
13998 function and want to see what the date was before converting."
13999   (interactive (list t))
14000   (gnus-article-date-ut 'original highlight))
14001
14002 (defun gnus-article-date-lapsed (&optional highlight)
14003   "Convert the current article date to time lapsed since it was sent."
14004   (interactive (list t))
14005   (gnus-article-date-ut 'lapsed highlight))
14006
14007 (defun gnus-article-maybe-highlight ()
14008   "Do some article highlighting if `gnus-visual' is non-nil."
14009   (if (gnus-visual-p 'article-highlight 'highlight)
14010       (gnus-article-highlight-some)))
14011
14012 ;; Article savers.
14013
14014 (defun gnus-output-to-rmail (file-name)
14015   "Append the current article to an Rmail file named FILE-NAME."
14016   (require 'rmail)
14017   ;; Most of these codes are borrowed from rmailout.el.
14018   (setq file-name (expand-file-name file-name))
14019   (setq rmail-default-rmail-file file-name)
14020   (let ((artbuf (current-buffer))
14021         (tmpbuf (get-buffer-create " *Gnus-output*")))
14022     (save-excursion
14023       (or (get-file-buffer file-name)
14024           (file-exists-p file-name)
14025           (if (gnus-yes-or-no-p
14026                (concat "\"" file-name "\" does not exist, create it? "))
14027               (let ((file-buffer (create-file-buffer file-name)))
14028                 (save-excursion
14029                   (set-buffer file-buffer)
14030                   (rmail-insert-rmail-file-header)
14031                   (let ((require-final-newline nil))
14032                     (write-region (point-min) (point-max) file-name t 1)))
14033                 (kill-buffer file-buffer))
14034             (error "Output file does not exist")))
14035       (set-buffer tmpbuf)
14036       (buffer-disable-undo (current-buffer))
14037       (erase-buffer)
14038       (insert-buffer-substring artbuf)
14039       (gnus-convert-article-to-rmail)
14040       ;; Decide whether to append to a file or to an Emacs buffer.
14041       (let ((outbuf (get-file-buffer file-name)))
14042         (if (not outbuf)
14043             (append-to-file (point-min) (point-max) file-name)
14044           ;; File has been visited, in buffer OUTBUF.
14045           (set-buffer outbuf)
14046           (let ((buffer-read-only nil)
14047                 (msg (and (boundp 'rmail-current-message)
14048                           (symbol-value 'rmail-current-message))))
14049             ;; If MSG is non-nil, buffer is in RMAIL mode.
14050             (if msg
14051                 (progn (widen)
14052                        (narrow-to-region (point-max) (point-max))))
14053             (insert-buffer-substring tmpbuf)
14054             (if msg
14055                 (progn
14056                   (goto-char (point-min))
14057                   (widen)
14058                   (search-backward "\^_")
14059                   (narrow-to-region (point) (point-max))
14060                   (goto-char (1+ (point-min)))
14061                   (rmail-count-new-messages t)
14062                   (rmail-show-message msg)))))))
14063     (kill-buffer tmpbuf)))
14064
14065 (defun gnus-output-to-file (file-name)
14066   "Append the current article to a file named FILE-NAME."
14067   (setq file-name (expand-file-name file-name))
14068   (let ((artbuf (current-buffer))
14069         (tmpbuf (get-buffer-create " *Gnus-output*")))
14070     (save-excursion
14071       (set-buffer tmpbuf)
14072       (buffer-disable-undo (current-buffer))
14073       (erase-buffer)
14074       (insert-buffer-substring artbuf)
14075       ;; Append newline at end of the buffer as separator, and then
14076       ;; save it to file.
14077       (goto-char (point-max))
14078       (insert "\n")
14079       (append-to-file (point-min) (point-max) file-name))
14080     (kill-buffer tmpbuf)))
14081
14082 (defun gnus-convert-article-to-rmail ()
14083   "Convert article in current buffer to Rmail message format."
14084   (let ((buffer-read-only nil))
14085     ;; Convert article directly into Babyl format.
14086     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14087     (goto-char (point-min))
14088     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14089     (while (search-forward "\n\^_" nil t) ;single char
14090       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14091     (goto-char (point-max))
14092     (insert "\^_")))
14093
14094 (defun gnus-narrow-to-page (&optional arg)
14095   "Narrow the article buffer to a page.
14096 If given a numerical ARG, move forward ARG pages."
14097   (interactive "P")
14098   (setq arg (if arg (prefix-numeric-value arg) 0))
14099   (save-excursion
14100     (set-buffer gnus-article-buffer)
14101     (goto-char (point-min))
14102     (widen)
14103     (when (gnus-visual-p 'page-marker)
14104       (let ((buffer-read-only nil))
14105         (gnus-remove-text-with-property 'gnus-prev)
14106         (gnus-remove-text-with-property 'gnus-next)))
14107     (when
14108         (cond ((< arg 0)
14109                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14110               ((> arg 0)
14111                (re-search-forward page-delimiter nil 'move arg)))
14112       (goto-char (match-end 0)))
14113     (narrow-to-region
14114      (point)
14115      (if (re-search-forward page-delimiter nil 'move)
14116          (match-beginning 0)
14117        (point)))
14118     (when (and (gnus-visual-p 'page-marker)
14119                (not (= (point-min) 1)))
14120       (save-excursion
14121         (goto-char (point-min))
14122         (gnus-insert-prev-page-button)))
14123     (when (and (gnus-visual-p 'page-marker)
14124                (not (= (1- (point-max)) (buffer-size))))
14125       (save-excursion
14126         (goto-char (point-max))
14127         (gnus-insert-next-page-button)))))
14128
14129 ;; Article mode commands
14130
14131 (defun gnus-article-goto-next-page ()
14132   "Show the next page of the article."
14133   (interactive)
14134   (when (gnus-article-next-page)
14135     (gnus-article-read-summary-keys nil ?n)))
14136
14137 (defun gnus-article-goto-prev-page ()
14138   "Show the next page of the article."
14139   (interactive)
14140   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14141     (gnus-article-prev-page nil)))
14142
14143 (defun gnus-article-next-page (&optional lines)
14144   "Show the next page of the current article.
14145 If end of article, return non-nil.  Otherwise return nil.
14146 Argument LINES specifies lines to be scrolled up."
14147   (interactive "p")
14148   (move-to-window-line -1)
14149   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14150   (if (save-excursion
14151         (end-of-line)
14152         (and (pos-visible-in-window-p)  ;Not continuation line.
14153              (eobp)))
14154       ;; Nothing in this page.
14155       (if (or (not gnus-break-pages)
14156               (save-excursion
14157                 (save-restriction
14158                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14159           t                             ;Nothing more.
14160         (gnus-narrow-to-page 1)         ;Go to next page.
14161         nil)
14162     ;; More in this page.
14163     (condition-case ()
14164         (scroll-up lines)
14165       (end-of-buffer
14166        ;; Long lines may cause an end-of-buffer error.
14167        (goto-char (point-max))))
14168     (move-to-window-line 0)
14169     nil))
14170
14171 (defun gnus-article-prev-page (&optional lines)
14172   "Show previous page of current article.
14173 Argument LINES specifies lines to be scrolled down."
14174   (interactive "p")
14175   (move-to-window-line 0)
14176   (if (and gnus-break-pages
14177            (bobp)
14178            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14179       (progn
14180         (gnus-narrow-to-page -1)        ;Go to previous page.
14181         (goto-char (point-max))
14182         (recenter -1))
14183     (prog1
14184         (condition-case ()
14185             (scroll-down lines)
14186           (error nil))
14187       (move-to-window-line 0))))
14188
14189 (defun gnus-article-refer-article ()
14190   "Read article specified by message-id around point."
14191   (interactive)
14192   (let ((point (point)))
14193     (search-forward ">" nil t)          ;Move point to end of "<....>".
14194     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14195         (let ((message-id (match-string 1)))
14196           (goto-char point)
14197           (set-buffer gnus-summary-buffer)
14198           (gnus-summary-refer-article message-id))
14199       (goto-char (point))
14200       (error "No references around point"))))
14201
14202 (defun gnus-article-show-summary ()
14203   "Reconfigure windows to show summary buffer."
14204   (interactive)
14205   (gnus-configure-windows 'article)
14206   (gnus-summary-goto-subject gnus-current-article))
14207
14208 (defun gnus-article-describe-briefly ()
14209   "Describe article mode commands briefly."
14210   (interactive)
14211   (gnus-message 6
14212                 (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")))
14213
14214 (defun gnus-article-summary-command ()
14215   "Execute the last keystroke in the summary buffer."
14216   (interactive)
14217   (let ((obuf (current-buffer))
14218         (owin (current-window-configuration))
14219         func)
14220     (switch-to-buffer gnus-summary-buffer 'norecord)
14221     (setq func (lookup-key (current-local-map) (this-command-keys)))
14222     (call-interactively func)
14223     (set-buffer obuf)
14224     (set-window-configuration owin)
14225     (set-window-point (get-buffer-window (current-buffer)) (point))))
14226
14227 (defun gnus-article-summary-command-nosave ()
14228   "Execute the last keystroke in the summary buffer."
14229   (interactive)
14230   (let (func)
14231     (pop-to-buffer gnus-summary-buffer 'norecord)
14232     (setq func (lookup-key (current-local-map) (this-command-keys)))
14233     (call-interactively func)))
14234
14235 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14236   "Read a summary buffer key sequence and execute it from the article buffer."
14237   (interactive "P")
14238   (let ((nosaves
14239          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14240            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14241            "=" "^" "\M-^"))
14242         keys)
14243     (save-excursion
14244       (set-buffer gnus-summary-buffer)
14245       (push (or key last-command-event) unread-command-events)
14246       (setq keys (read-key-sequence nil)))
14247     (message "")
14248
14249     (if (member keys nosaves)
14250         (let (func)
14251           (pop-to-buffer gnus-summary-buffer 'norecord)
14252           (if (setq func (lookup-key (current-local-map) keys))
14253               (call-interactively func)
14254             (ding)))
14255       (let ((obuf (current-buffer))
14256             (owin (current-window-configuration))
14257             (opoint (point))
14258             func in-buffer)
14259         (if not-restore-window
14260             (pop-to-buffer gnus-summary-buffer 'norecord)
14261           (switch-to-buffer gnus-summary-buffer 'norecord))
14262         (setq in-buffer (current-buffer))
14263         (if (setq func (lookup-key (current-local-map) keys))
14264             (call-interactively func)
14265           (ding))
14266         (when (eq in-buffer (current-buffer))
14267           (set-buffer obuf)
14268           (unless not-restore-window
14269             (set-window-configuration owin))
14270           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14271
14272 \f
14273 ;;;
14274 ;;; Kill file handling.
14275 ;;;
14276
14277 ;;;###autoload
14278 (defalias 'gnus-batch-kill 'gnus-batch-score)
14279 ;;;###autoload
14280 (defun gnus-batch-score ()
14281   "Run batched scoring.
14282 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14283 Newsgroups is a list of strings in Bnews format.  If you want to score
14284 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14285 score the alt hierarchy, you'd say \"!alt.all\"."
14286   (interactive)
14287   (let* ((yes-and-no
14288           (gnus-newsrc-parse-options
14289            (apply (function concat)
14290                   (mapcar (lambda (g) (concat g " "))
14291                           command-line-args-left))))
14292          (gnus-expert-user t)
14293          (nnmail-spool-file nil)
14294          (gnus-use-dribble-file nil)
14295          (yes (car yes-and-no))
14296          (no (cdr yes-and-no))
14297          group newsrc entry
14298          ;; Disable verbose message.
14299          gnus-novice-user gnus-large-newsgroup)
14300     ;; Eat all arguments.
14301     (setq command-line-args-left nil)
14302     ;; Start Gnus.
14303     (gnus)
14304     ;; Apply kills to specified newsgroups in command line arguments.
14305     (setq newsrc (cdr gnus-newsrc-alist))
14306     (while newsrc
14307       (setq group (caar newsrc))
14308       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14309       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14310                (and (car entry)
14311                     (or (eq (car entry) t)
14312                         (not (zerop (car entry)))))
14313                (if yes (string-match yes group) t)
14314                (or (null no) (not (string-match no group))))
14315           (progn
14316             (gnus-summary-read-group group nil t nil t)
14317             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14318                  (gnus-summary-exit))))
14319       (setq newsrc (cdr newsrc)))
14320     ;; Exit Emacs.
14321     (switch-to-buffer gnus-group-buffer)
14322     (gnus-group-save-newsrc)))
14323
14324 (defun gnus-apply-kill-file ()
14325   "Apply a kill file to the current newsgroup.
14326 Returns the number of articles marked as read."
14327   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14328           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14329       (gnus-apply-kill-file-internal)
14330     0))
14331
14332 (defun gnus-kill-save-kill-buffer ()
14333   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14334     (when (get-file-buffer file)
14335       (save-excursion
14336         (set-buffer (get-file-buffer file))
14337         (and (buffer-modified-p) (save-buffer))
14338         (kill-buffer (current-buffer))))))
14339
14340 (defvar gnus-kill-file-name "KILL"
14341   "Suffix of the kill files.")
14342
14343 (defun gnus-newsgroup-kill-file (newsgroup)
14344   "Return the name of a kill file name for NEWSGROUP.
14345 If NEWSGROUP is nil, return the global kill file name instead."
14346   (cond 
14347    ;; The global KILL file is placed at top of the directory.
14348    ((or (null newsgroup)
14349         (string-equal newsgroup ""))
14350     (expand-file-name gnus-kill-file-name
14351                       (or gnus-kill-files-directory "~/News")))
14352    ;; Append ".KILL" to newsgroup name.
14353    ((gnus-use-long-file-name 'not-kill)
14354     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14355                               "." gnus-kill-file-name)
14356                       (or gnus-kill-files-directory "~/News")))
14357    ;; Place "KILL" under the hierarchical directory.
14358    (t
14359     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14360                               "/" gnus-kill-file-name)
14361                       (or gnus-kill-files-directory "~/News")))))
14362
14363 \f
14364 ;;;
14365 ;;; Dribble file
14366 ;;;
14367
14368 (defvar gnus-dribble-ignore nil)
14369 (defvar gnus-dribble-eval-file nil)
14370
14371 (defun gnus-dribble-file-name ()
14372   "Return the dribble file for the current .newsrc."
14373   (concat
14374    (if gnus-dribble-directory
14375        (concat (file-name-as-directory gnus-dribble-directory)
14376                (file-name-nondirectory gnus-current-startup-file))
14377      gnus-current-startup-file)
14378    "-dribble"))
14379
14380 (defun gnus-dribble-enter (string)
14381   "Enter STRING into the dribble buffer."
14382   (if (and (not gnus-dribble-ignore)
14383            gnus-dribble-buffer
14384            (buffer-name gnus-dribble-buffer))
14385       (let ((obuf (current-buffer)))
14386         (set-buffer gnus-dribble-buffer)
14387         (insert string "\n")
14388         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14389         (set-buffer obuf))))
14390
14391 (defun gnus-dribble-read-file ()
14392   "Read the dribble file from disk."
14393   (let ((dribble-file (gnus-dribble-file-name)))
14394     (save-excursion
14395       (set-buffer (setq gnus-dribble-buffer
14396                         (get-buffer-create
14397                          (file-name-nondirectory dribble-file))))
14398       (gnus-add-current-to-buffer-list)
14399       (erase-buffer)
14400       (setq buffer-file-name dribble-file)
14401       (auto-save-mode t)
14402       (buffer-disable-undo (current-buffer))
14403       (bury-buffer (current-buffer))
14404       (set-buffer-modified-p nil)
14405       (let ((auto (make-auto-save-file-name))
14406             (gnus-dribble-ignore t))
14407         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14408           ;; Load whichever file is newest -- the auto save file
14409           ;; or the "real" file.
14410           (if (file-newer-than-file-p auto dribble-file)
14411               (insert-file-contents auto)
14412             (insert-file-contents dribble-file))
14413           (unless (zerop (buffer-size))
14414             (set-buffer-modified-p t))
14415           ;; Set the file modes to reflect the .newsrc file modes.
14416           (save-buffer)
14417           (when (file-exists-p gnus-current-startup-file)
14418             (set-file-modes dribble-file
14419                             (file-modes gnus-current-startup-file)))
14420           ;; Possibly eval the file later.
14421           (when (gnus-y-or-n-p
14422                  "Auto-save file exists.  Do you want to read it? ")
14423             (setq gnus-dribble-eval-file t)))))))
14424
14425 (defun gnus-dribble-eval-file ()
14426   (if (not gnus-dribble-eval-file)
14427       ()
14428     (setq gnus-dribble-eval-file nil)
14429     (save-excursion
14430       (let ((gnus-dribble-ignore t))
14431         (set-buffer gnus-dribble-buffer)
14432         (eval-buffer (current-buffer))))))
14433
14434 (defun gnus-dribble-delete-file ()
14435   (if (file-exists-p (gnus-dribble-file-name))
14436       (delete-file (gnus-dribble-file-name)))
14437   (if gnus-dribble-buffer
14438       (save-excursion
14439         (set-buffer gnus-dribble-buffer)
14440         (let ((auto (make-auto-save-file-name)))
14441           (if (file-exists-p auto)
14442               (delete-file auto))
14443           (erase-buffer)
14444           (set-buffer-modified-p nil)))))
14445
14446 (defun gnus-dribble-save ()
14447   (if (and gnus-dribble-buffer
14448            (buffer-name gnus-dribble-buffer))
14449       (save-excursion
14450         (set-buffer gnus-dribble-buffer)
14451         (save-buffer))))
14452
14453 (defun gnus-dribble-clear ()
14454   (save-excursion
14455     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14456         (progn
14457           (set-buffer gnus-dribble-buffer)
14458           (erase-buffer)
14459           (set-buffer-modified-p nil)
14460           (setq buffer-saved-size (buffer-size))))))
14461
14462 \f
14463 ;;;
14464 ;;; Server Communication
14465 ;;;
14466
14467 (defun gnus-start-news-server (&optional confirm)
14468   "Open a method for getting news.
14469 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14470   (let (how)
14471     (if gnus-current-select-method
14472         ;; Stream is already opened.
14473         nil
14474       ;; Open NNTP server.
14475       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14476       (if confirm
14477           (progn
14478             ;; Read server name with completion.
14479             (setq gnus-nntp-server
14480                   (completing-read "NNTP server: "
14481                                    (mapcar (lambda (server) (list server))
14482                                            (cons (list gnus-nntp-server)
14483                                                  gnus-secondary-servers))
14484                                    nil nil gnus-nntp-server))))
14485
14486       (if (and gnus-nntp-server
14487                (stringp gnus-nntp-server)
14488                (not (string= gnus-nntp-server "")))
14489           (setq gnus-select-method
14490                 (cond ((or (string= gnus-nntp-server "")
14491                            (string= gnus-nntp-server "::"))
14492                        (list 'nnspool (system-name)))
14493                       ((string-match "^:" gnus-nntp-server)
14494                        (list 'nnmh gnus-nntp-server
14495                              (list 'nnmh-directory
14496                                    (file-name-as-directory
14497                                     (expand-file-name
14498                                      (concat "~/" (substring
14499                                                    gnus-nntp-server 1)))))
14500                              (list 'nnmh-get-new-mail nil)))
14501                       (t
14502                        (list 'nntp gnus-nntp-server)))))
14503
14504       (setq how (car gnus-select-method))
14505       (cond ((eq how 'nnspool)
14506              (require 'nnspool)
14507              (gnus-message 5 "Looking up local news spool..."))
14508             ((eq how 'nnmh)
14509              (require 'nnmh)
14510              (gnus-message 5 "Looking up mh spool..."))
14511             (t
14512              (require 'nntp)))
14513       (setq gnus-current-select-method gnus-select-method)
14514       (run-hooks 'gnus-open-server-hook)
14515       (or
14516        ;; gnus-open-server-hook might have opened it
14517        (gnus-server-opened gnus-select-method)
14518        (gnus-open-server gnus-select-method)
14519        (gnus-y-or-n-p
14520         (format
14521          "%s (%s) open error: '%s'.     Continue? "
14522          (car gnus-select-method) (cadr gnus-select-method)
14523          (gnus-status-message gnus-select-method)))
14524        (progn
14525          (gnus-message 1 "Couldn't open server on %s"
14526                        (nth 1 gnus-select-method))
14527          (ding)
14528          nil)))))
14529
14530 (defun gnus-check-group (group)
14531   "Try to make sure that the server where GROUP exists is alive."
14532   (let ((method (gnus-find-method-for-group group)))
14533     (or (gnus-server-opened method)
14534         (gnus-open-server method))))
14535
14536 (defun gnus-check-server (&optional method silent)
14537   "Check whether the connection to METHOD is down.
14538 If METHOD is nil, use `gnus-select-method'.
14539 If it is down, start it up (again)."
14540   (let ((method (or method gnus-select-method)))
14541     ;; Transform virtual server names into select methods.
14542     (when (stringp method)
14543       (setq method (gnus-server-to-method method)))
14544     (if (gnus-server-opened method)
14545         ;; The stream is already opened.
14546         t
14547       ;; Open the server.
14548       (unless silent
14549         (gnus-message 5 "Opening %s server%s..." (car method)
14550                       (if (equal (nth 1 method) "") ""
14551                         (format " on %s" (nth 1 method)))))
14552       (run-hooks 'gnus-open-server-hook)
14553       (prog1
14554           (gnus-open-server method)
14555         (unless silent
14556           (message ""))))))
14557
14558 (defun gnus-get-function (method function)
14559   "Return a function symbol based on METHOD and FUNCTION."
14560   ;; Translate server names into methods.
14561   (unless method
14562     (error "Attempted use of a nil select method"))
14563   (when (stringp method)
14564     (setq method (gnus-server-to-method method)))
14565   (let ((func (intern (format "%s-%s" (car method) function))))
14566     ;; If the functions isn't bound, we require the backend in
14567     ;; question.
14568     (unless (fboundp func)
14569       (require (car method))
14570       (unless (fboundp func)
14571         ;; This backend doesn't implement this function.
14572         (error "No such function: %s" func)))
14573     func))
14574
14575 \f
14576 ;;;
14577 ;;; Interface functions to the backends.
14578 ;;;
14579
14580 (defun gnus-open-server (method)
14581   "Open a connection to METHOD."
14582   (when (stringp method)
14583     (setq method (gnus-server-to-method method)))
14584   (let ((elem (assoc method gnus-opened-servers)))
14585     ;; If this method was previously denied, we just return nil.
14586     (if (eq (nth 1 elem) 'denied)
14587         (progn
14588           (gnus-message 1 "Denied server")
14589           nil)
14590       ;; Open the server.
14591       (let ((result
14592              (funcall (gnus-get-function method 'open-server)
14593                       (nth 1 method) (nthcdr 2 method))))
14594         ;; If this hasn't been opened before, we add it to the list.
14595         (unless elem
14596           (setq elem (list method nil)
14597                 gnus-opened-servers (cons elem gnus-opened-servers)))
14598         ;; Set the status of this server.
14599         (setcar (cdr elem) (if result 'ok 'denied))
14600         ;; Return the result from the "open" call.
14601         result))))
14602
14603 (defun gnus-close-server (method)
14604   "Close the connection to METHOD."
14605   (when (stringp method)
14606     (setq method (gnus-server-to-method method)))
14607   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14608
14609 (defun gnus-request-list (method)
14610   "Request the active file from METHOD."
14611   (when (stringp method)
14612     (setq method (gnus-server-to-method method)))
14613   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14614
14615 (defun gnus-request-list-newsgroups (method)
14616   "Request the newsgroups file from METHOD."
14617   (when (stringp method)
14618     (setq method (gnus-server-to-method method)))
14619   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14620
14621 (defun gnus-request-newgroups (date method)
14622   "Request all new groups since DATE from METHOD."
14623   (when (stringp method)
14624     (setq method (gnus-server-to-method method)))
14625   (funcall (gnus-get-function method 'request-newgroups)
14626            date (nth 1 method)))
14627
14628 (defun gnus-server-opened (method)
14629   "Check whether a connection to METHOD has been opened."
14630   (when (stringp method)
14631     (setq method (gnus-server-to-method method)))
14632   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14633
14634 (defun gnus-status-message (method)
14635   "Return the status message from METHOD.
14636 If METHOD is a string, it is interpreted as a group name.   The method
14637 this group uses will be queried."
14638   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14639                   method)))
14640     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14641
14642 (defun gnus-request-group (group &optional dont-check method)
14643   "Request GROUP.  If DONT-CHECK, no information is required."
14644   (let ((method (or method (gnus-find-method-for-group group))))
14645     (when (stringp method)
14646       (setq method (gnus-server-to-method method)))
14647     (funcall (gnus-get-function method 'request-group)
14648              (gnus-group-real-name group) (nth 1 method) dont-check)))
14649
14650 (defun gnus-request-asynchronous (group &optional articles)
14651   "Request that GROUP behave asynchronously.
14652 ARTICLES is the `data' of the group."
14653   (let ((method (gnus-find-method-for-group group)))
14654     (funcall (gnus-get-function method 'request-asynchronous)
14655              (gnus-group-real-name group) (nth 1 method) articles)))
14656
14657 (defun gnus-list-active-group (group)
14658   "Request active information on GROUP."
14659   (let ((method (gnus-find-method-for-group group))
14660         (func 'list-active-group))
14661     (when (gnus-check-backend-function func group)
14662       (funcall (gnus-get-function method func)
14663                (gnus-group-real-name group) (nth 1 method)))))
14664
14665 (defun gnus-request-group-description (group)
14666   "Request a description of GROUP."
14667   (let ((method (gnus-find-method-for-group group))
14668         (func 'request-group-description))
14669     (when (gnus-check-backend-function func group)
14670       (funcall (gnus-get-function method func)
14671                (gnus-group-real-name group) (nth 1 method)))))
14672
14673 (defun gnus-close-group (group)
14674   "Request the GROUP be closed."
14675   (let ((method (gnus-find-method-for-group group)))
14676     (funcall (gnus-get-function method 'close-group)
14677              (gnus-group-real-name group) (nth 1 method))))
14678
14679 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14680   "Request headers for ARTICLES in GROUP.
14681 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14682   (let ((method (gnus-find-method-for-group group)))
14683     (if (and gnus-use-cache (numberp (car articles)))
14684         (gnus-cache-retrieve-headers articles group fetch-old)
14685       (funcall (gnus-get-function method 'retrieve-headers)
14686                articles (gnus-group-real-name group) (nth 1 method)
14687                fetch-old))))
14688
14689 (defun gnus-retrieve-groups (groups method)
14690   "Request active information on GROUPS from METHOD."
14691   (when (stringp method)
14692     (setq method (gnus-server-to-method method)))
14693   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14694
14695 (defun gnus-request-type (group &optional article)
14696   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14697   (let ((method (gnus-find-method-for-group group)))
14698     (if (not (gnus-check-backend-function 'request-type (car method)))
14699         'unknown
14700       (funcall (gnus-get-function method 'request-type)
14701                (gnus-group-real-name group) article))))
14702
14703 (defun gnus-request-update-mark (group article mark)
14704   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14705   (let ((method (gnus-find-method-for-group group)))
14706     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14707         mark
14708       (funcall (gnus-get-function method 'request-update-mark)
14709                (gnus-group-real-name group) article mark))))
14710
14711 (defun gnus-request-article (article group &optional buffer)
14712   "Request the ARTICLE in GROUP.
14713 ARTICLE can either be an article number or an article Message-ID.
14714 If BUFFER, insert the article in that group."
14715   (let ((method (gnus-find-method-for-group group)))
14716     (funcall (gnus-get-function method 'request-article)
14717              article (gnus-group-real-name group) (nth 1 method) buffer)))
14718
14719 (defun gnus-request-head (article group)
14720   "Request the head of ARTICLE in GROUP."
14721   (let ((method (gnus-find-method-for-group group)))
14722     (funcall (gnus-get-function method 'request-head)
14723              article (gnus-group-real-name group) (nth 1 method))))
14724
14725 (defun gnus-request-body (article group)
14726   "Request the body of ARTICLE in GROUP."
14727   (let ((method (gnus-find-method-for-group group)))
14728     (funcall (gnus-get-function method 'request-body)
14729              article (gnus-group-real-name group) (nth 1 method))))
14730
14731 (defun gnus-request-post (method)
14732   "Post the current buffer using METHOD."
14733   (when (stringp method)
14734     (setq method (gnus-server-to-method method)))
14735   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14736
14737 (defun gnus-request-scan (group method)
14738   "Request a SCAN being performed in GROUP from METHOD.
14739 If GROUP is nil, all groups on METHOD are scanned."
14740   (let ((method (if group (gnus-find-method-for-group group) method)))
14741     (funcall (gnus-get-function method 'request-scan)
14742              (and group (gnus-group-real-name group)) (nth 1 method))))
14743
14744 (defsubst gnus-request-update-info (info method)
14745   "Request that METHOD update INFO."
14746   (when (stringp method)
14747     (setq method (gnus-server-to-method method)))
14748   (when (gnus-check-backend-function 'request-update-info (car method))
14749     (funcall (gnus-get-function method 'request-update-info)
14750              (gnus-group-real-name (gnus-info-group info))
14751              info (nth 1 method))))
14752
14753 (defun gnus-request-expire-articles (articles group &optional force)
14754   (let ((method (gnus-find-method-for-group group)))
14755     (funcall (gnus-get-function method 'request-expire-articles)
14756              articles (gnus-group-real-name group) (nth 1 method)
14757              force)))
14758
14759 (defun gnus-request-move-article
14760   (article group server accept-function &optional last)
14761   (let ((method (gnus-find-method-for-group group)))
14762     (funcall (gnus-get-function method 'request-move-article)
14763              article (gnus-group-real-name group)
14764              (nth 1 method) accept-function last)))
14765
14766 (defun gnus-request-accept-article (group &optional last method)
14767   ;; Make sure there's a newline at the end of the article.
14768   (when (stringp method)
14769     (setq method (gnus-server-to-method method)))
14770   (goto-char (point-max))
14771   (unless (bolp)
14772     (insert "\n"))
14773   (let ((func (if (symbolp group) group
14774                 (car (or method (gnus-find-method-for-group group))))))
14775     (funcall (intern (format "%s-request-accept-article" func))
14776              (if (stringp group) (gnus-group-real-name group) group)
14777              last)))
14778
14779 (defun gnus-request-replace-article (article group buffer)
14780   (let ((func (car (gnus-find-method-for-group group))))
14781     (funcall (intern (format "%s-request-replace-article" func))
14782              article (gnus-group-real-name group) buffer)))
14783
14784 (defun gnus-request-associate-buffer (group)
14785   (let ((method (gnus-find-method-for-group group)))
14786     (funcall (gnus-get-function method 'request-associate-buffer)
14787              (gnus-group-real-name group))))
14788
14789 (defun gnus-request-restore-buffer (article group)
14790   "Request a new buffer restored to the state of ARTICLE."
14791   (let ((method (gnus-find-method-for-group group)))
14792     (funcall (gnus-get-function method 'request-restore-buffer)
14793              article (gnus-group-real-name group) (nth 1 method))))
14794
14795 (defun gnus-request-create-group (group &optional method)
14796   (when (stringp method)
14797     (setq method (gnus-server-to-method method)))
14798   (let ((method (or method (gnus-find-method-for-group group))))
14799     (funcall (gnus-get-function method 'request-create-group)
14800              (gnus-group-real-name group) (nth 1 method))))
14801
14802 (defun gnus-request-delete-group (group &optional force)
14803   (let ((method (gnus-find-method-for-group group)))
14804     (funcall (gnus-get-function method 'request-delete-group)
14805              (gnus-group-real-name group) force (nth 1 method))))
14806
14807 (defun gnus-request-rename-group (group new-name)
14808   (let ((method (gnus-find-method-for-group group)))
14809     (funcall (gnus-get-function method 'request-rename-group)
14810              (gnus-group-real-name group)
14811              (gnus-group-real-name new-name) (nth 1 method))))
14812
14813 (defun gnus-member-of-valid (symbol group)
14814   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14815   (memq symbol (assoc
14816                 (symbol-name (car (gnus-find-method-for-group group)))
14817                 gnus-valid-select-methods)))
14818
14819 (defun gnus-method-option-p (method option)
14820   "Return non-nil if select METHOD has OPTION as a parameter."
14821   (when (stringp method)
14822     (setq method (gnus-server-to-method method)))
14823   (memq option (assoc (format "%s" (car method))
14824                       gnus-valid-select-methods)))
14825
14826 (defun gnus-server-extend-method (group method)
14827   ;; This function "extends" a virtual server.  If the server is
14828   ;; "hello", and the select method is ("hello" (my-var "something"))
14829   ;; in the group "alt.alt", this will result in a new virtual server
14830   ;; called "hello+alt.alt".
14831   (let ((entry
14832          (gnus-copy-sequence
14833           (if (equal (car method) "native") gnus-select-method
14834             (cdr (assoc (car method) gnus-server-alist))))))
14835     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14836     (nconc entry (cdr method))))
14837
14838 (defun gnus-find-method-for-group (group &optional info)
14839   "Find the select method that GROUP uses."
14840   (or gnus-override-method
14841       (and (not group)
14842            gnus-select-method)
14843       (let ((info (or info (gnus-get-info group)))
14844             method)
14845         (if (or (not info)
14846                 (not (setq method (gnus-info-method info)))
14847                 (equal method "native"))
14848             gnus-select-method
14849           (setq method
14850                 (cond ((stringp method)
14851                        (gnus-server-to-method method))
14852                       ((stringp (car method))
14853                        (gnus-server-extend-method group method))
14854                       (t
14855                        method)))
14856           (if (equal (cadr method) "")
14857               method
14858             (gnus-server-add-address method))))))
14859
14860 (defun gnus-check-backend-function (func group)
14861   "Check whether GROUP supports function FUNC."
14862   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14863                   group)))
14864     (fboundp (intern (format "%s-%s" method func)))))
14865
14866 (defun gnus-methods-using (feature)
14867   "Find all methods that have FEATURE."
14868   (let ((valids gnus-valid-select-methods)
14869         outs)
14870     (while valids
14871       (if (memq feature (car valids))
14872           (setq outs (cons (car valids) outs)))
14873       (setq valids (cdr valids)))
14874     outs))
14875
14876 \f
14877 ;;;
14878 ;;; Active & Newsrc File Handling
14879 ;;;
14880
14881 (defun gnus-setup-news (&optional rawfile level dont-connect)
14882   "Setup news information.
14883 If RAWFILE is non-nil, the .newsrc file will also be read.
14884 If LEVEL is non-nil, the news will be set up at level LEVEL."
14885   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14886
14887     (when init 
14888       ;; Clear some variables to re-initialize news information.
14889       (setq gnus-newsrc-alist nil
14890             gnus-active-hashtb nil)
14891       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14892       (gnus-read-newsrc-file rawfile))
14893
14894     (when (and (not (assoc "archive" gnus-server-alist))
14895                gnus-message-archive-method)
14896       (push (cons "archive" gnus-message-archive-method)
14897             gnus-server-alist))
14898
14899     ;; If we don't read the complete active file, we fill in the
14900     ;; hashtb here.
14901     (if (or (null gnus-read-active-file)
14902             (eq gnus-read-active-file 'some))
14903         (gnus-update-active-hashtb-from-killed))
14904
14905     ;; Read the active file and create `gnus-active-hashtb'.
14906     ;; If `gnus-read-active-file' is nil, then we just create an empty
14907     ;; hash table.  The partial filling out of the hash table will be
14908     ;; done in `gnus-get-unread-articles'.
14909     (and gnus-read-active-file
14910          (not level)
14911          (gnus-read-active-file))
14912
14913     (or gnus-active-hashtb
14914         (setq gnus-active-hashtb (make-vector 4095 0)))
14915
14916     ;; Initialize the cache.
14917     (when gnus-use-cache
14918       (gnus-cache-open))
14919
14920     ;; Possibly eval the dribble file.
14921     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14922
14923     (gnus-update-format-specifications)
14924
14925     ;; See whether we need to read the description file.
14926     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
14927              (not gnus-description-hashtb)
14928              (not dont-connect)
14929              gnus-read-active-file)
14930         (gnus-read-all-descriptions-files))
14931
14932     ;; Find new newsgroups and treat them.
14933     (if (and init gnus-check-new-newsgroups (not level)
14934              (gnus-check-server gnus-select-method))
14935         (gnus-find-new-newsgroups))
14936
14937     ;; We might read in new NoCeM messages here.
14938     (when gnus-use-nocem 
14939       (gnus-nocem-scan-groups))
14940
14941     ;; Find the number of unread articles in each non-dead group.
14942     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14943       (gnus-get-unread-articles level))
14944
14945     (if (and init gnus-check-bogus-newsgroups
14946              gnus-read-active-file (not level)
14947              (gnus-server-opened gnus-select-method))
14948         (gnus-check-bogus-newsgroups))))
14949
14950 (defun gnus-find-new-newsgroups (&optional arg)
14951   "Search for new newsgroups and add them.
14952 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14953 The `-n' option line from .newsrc is respected.
14954 If ARG (the prefix), use the `ask-server' method to query
14955 the server for new groups."
14956   (interactive "P")
14957   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14958                        (null gnus-read-active-file)
14959                        (eq gnus-read-active-file 'some))
14960                    'ask-server gnus-check-new-newsgroups)))
14961     (unless (gnus-check-first-time-used)
14962       (if (or (consp check)
14963               (eq check 'ask-server))
14964           ;; Ask the server for new groups.
14965           (gnus-ask-server-for-new-groups)
14966         ;; Go through the active hashtb and look for new groups.
14967         (let ((groups 0)
14968               group new-newsgroups)
14969           (gnus-message 5 "Looking for new newsgroups...")
14970           (unless gnus-have-read-active-file
14971             (gnus-read-active-file))
14972           (setq gnus-newsrc-last-checked-date (current-time-string))
14973           (unless gnus-killed-hashtb
14974             (gnus-make-hashtable-from-killed))
14975           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14976           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14977           (mapatoms
14978            (lambda (sym)
14979              (if (or (null (setq group (symbol-name sym)))
14980                      (not (boundp sym))
14981                      (null (symbol-value sym))
14982                      (gnus-gethash group gnus-killed-hashtb)
14983                      (gnus-gethash group gnus-newsrc-hashtb))
14984                  ()
14985                (let ((do-sub (gnus-matches-options-n group)))
14986                  (cond
14987                   ((eq do-sub 'subscribe)
14988                    (setq groups (1+ groups))
14989                    (gnus-sethash group group gnus-killed-hashtb)
14990                    (funcall gnus-subscribe-options-newsgroup-method group))
14991                   ((eq do-sub 'ignore)
14992                    nil)
14993                   (t
14994                    (setq groups (1+ groups))
14995                    (gnus-sethash group group gnus-killed-hashtb)
14996                    (if gnus-subscribe-hierarchical-interactive
14997                        (setq new-newsgroups (cons group new-newsgroups))
14998                      (funcall gnus-subscribe-newsgroup-method group)))))))
14999            gnus-active-hashtb)
15000           (when new-newsgroups
15001             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15002           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15003           (if (> groups 0)
15004               (gnus-message 6 "%d new newsgroup%s arrived."
15005                             groups (if (> groups 1) "s have" " has"))
15006             (gnus-message 6 "No new newsgroups.")))))))
15007
15008 (defun gnus-matches-options-n (group)
15009   ;; Returns `subscribe' if the group is to be unconditionally
15010   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15011   ;; no match for the group.
15012
15013   ;; First we check the two user variables.
15014   (cond
15015    ((and gnus-options-subscribe
15016          (string-match gnus-options-subscribe group))
15017     'subscribe)
15018    ((and gnus-auto-subscribed-groups
15019          (string-match gnus-auto-subscribed-groups group))
15020     'subscribe)
15021    ((and gnus-options-not-subscribe
15022          (string-match gnus-options-not-subscribe group))
15023     'ignore)
15024    ;; Then we go through the list that was retrieved from the .newsrc
15025    ;; file.  This list has elements on the form
15026    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15027    ;; is in the reverse order of the options line) is returned.
15028    (t
15029     (let ((regs gnus-newsrc-options-n))
15030       (while (and regs
15031                   (not (string-match (caar regs) group)))
15032         (setq regs (cdr regs)))
15033       (and regs (cdar regs))))))
15034
15035 (defun gnus-ask-server-for-new-groups ()
15036   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15037          (methods (cons gnus-select-method
15038                         (nconc
15039                          (when gnus-message-archive-method
15040                            (list "archive"))
15041                          (append
15042                           (and (consp gnus-check-new-newsgroups)
15043                                gnus-check-new-newsgroups)
15044                           gnus-secondary-select-methods))))
15045          (groups 0)
15046          (new-date (current-time-string))
15047          group new-newsgroups got-new method hashtb
15048          gnus-override-subscribe-method)
15049     ;; Go through both primary and secondary select methods and
15050     ;; request new newsgroups.
15051     (while (setq method (gnus-server-get-method nil (pop methods)))
15052       (setq new-newsgroups nil)
15053       (setq gnus-override-subscribe-method method)
15054       (when (and (gnus-check-server method)
15055                  (gnus-request-newgroups date method))
15056         (save-excursion
15057           (setq got-new t)
15058           (setq hashtb (gnus-make-hashtable 100))
15059           (set-buffer nntp-server-buffer)
15060           ;; Enter all the new groups into a hashtable.
15061           (gnus-active-to-gnus-format method hashtb 'ignore)))
15062       ;; Now all new groups from `method' are in `hashtb'.
15063       (mapatoms
15064        (lambda (group-sym)
15065          (if (or (null (setq group (symbol-name group-sym)))
15066                  (null (symbol-value group-sym))
15067                  (gnus-gethash group gnus-newsrc-hashtb)
15068                  (member group gnus-zombie-list)
15069                  (member group gnus-killed-list))
15070              ;; The group is already known.
15071              ()
15072            ;; Make this group active.
15073            (when (symbol-value group-sym)
15074              (gnus-set-active group (symbol-value group-sym)))
15075            ;; Check whether we want it or not.
15076            (let ((do-sub (gnus-matches-options-n group)))
15077              (cond
15078               ((eq do-sub 'subscribe)
15079                (incf groups)
15080                (gnus-sethash group group gnus-killed-hashtb)
15081                (funcall gnus-subscribe-options-newsgroup-method group))
15082               ((eq do-sub 'ignore)
15083                nil)
15084               (t
15085                (incf groups)
15086                (gnus-sethash group group gnus-killed-hashtb)
15087                (if gnus-subscribe-hierarchical-interactive
15088                    (push group new-newsgroups)
15089                  (funcall gnus-subscribe-newsgroup-method group)))))))
15090        hashtb)
15091       (when new-newsgroups
15092         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15093     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15094     (when (> groups 0)
15095       (gnus-message 6 "%d new newsgroup%s arrived."
15096                     groups (if (> groups 1) "s have" " has")))
15097     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15098     got-new))
15099
15100 (defun gnus-check-first-time-used ()
15101   (if (or (> (length gnus-newsrc-alist) 1)
15102           (file-exists-p gnus-startup-file)
15103           (file-exists-p (concat gnus-startup-file ".el"))
15104           (file-exists-p (concat gnus-startup-file ".eld")))
15105       nil
15106     (gnus-message 6 "First time user; subscribing you to default groups")
15107     (unless gnus-have-read-active-file
15108       (gnus-read-active-file))
15109     (setq gnus-newsrc-last-checked-date (current-time-string))
15110     (let ((groups gnus-default-subscribed-newsgroups)
15111           group)
15112       (if (eq groups t)
15113           nil
15114         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15115         (mapatoms
15116          (lambda (sym)
15117            (if (null (setq group (symbol-name sym)))
15118                ()
15119              (let ((do-sub (gnus-matches-options-n group)))
15120                (cond
15121                 ((eq do-sub 'subscribe)
15122                  (gnus-sethash group group gnus-killed-hashtb)
15123                  (funcall gnus-subscribe-options-newsgroup-method group))
15124                 ((eq do-sub 'ignore)
15125                  nil)
15126                 (t
15127                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15128          gnus-active-hashtb)
15129         (while groups
15130           (if (gnus-active (car groups))
15131               (gnus-group-change-level
15132                (car groups) gnus-level-default-subscribed gnus-level-killed))
15133           (setq groups (cdr groups)))
15134         (gnus-group-make-help-group)
15135         (and gnus-novice-user
15136              (gnus-message 7 "`A k' to list killed groups"))))))
15137
15138 (defun gnus-subscribe-group (group previous &optional method)
15139   (gnus-group-change-level
15140    (if method
15141        (list t group gnus-level-default-subscribed nil nil method)
15142      group)
15143    gnus-level-default-subscribed gnus-level-killed previous t))
15144
15145 ;; `gnus-group-change-level' is the fundamental function for changing
15146 ;; subscription levels of newsgroups.  This might mean just changing
15147 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15148 ;; again, which subscribes/unsubscribes a group, which is equally
15149 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15150 ;; from 8-9 to 1-7 means that you remove the group from the list of
15151 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15152 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15153 ;; which is trivial.
15154 ;; ENTRY can either be a string (newsgroup name) or a list (if
15155 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15156 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15157 ;; entries.
15158 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15159 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15160 ;; after.
15161 (defun gnus-group-change-level (entry level &optional oldlevel
15162                                       previous fromkilled)
15163   (let (group info active num)
15164     ;; Glean what info we can from the arguments
15165     (if (consp entry)
15166         (if fromkilled (setq group (nth 1 entry))
15167           (setq group (car (nth 2 entry))))
15168       (setq group entry))
15169     (if (and (stringp entry)
15170              oldlevel
15171              (< oldlevel gnus-level-zombie))
15172         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15173     (if (and (not oldlevel)
15174              (consp entry))
15175         (setq oldlevel (gnus-info-level (nth 2 entry)))
15176       (setq oldlevel (or oldlevel 9)))
15177     (if (stringp previous)
15178         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15179
15180     (if (and (>= oldlevel gnus-level-zombie)
15181              (gnus-gethash group gnus-newsrc-hashtb))
15182         ;; We are trying to subscribe a group that is already
15183         ;; subscribed.
15184         ()                              ; Do nothing.
15185
15186       (or (gnus-ephemeral-group-p group)
15187           (gnus-dribble-enter
15188            (format "(gnus-group-change-level %S %S %S %S %S)"
15189                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15190
15191       ;; Then we remove the newgroup from any old structures, if needed.
15192       ;; If the group was killed, we remove it from the killed or zombie
15193       ;; list.  If not, and it is in fact going to be killed, we remove
15194       ;; it from the newsrc hash table and assoc.
15195       (cond
15196        ((>= oldlevel gnus-level-zombie)
15197         (if (= oldlevel gnus-level-zombie)
15198             (setq gnus-zombie-list (delete group gnus-zombie-list))
15199           (setq gnus-killed-list (delete group gnus-killed-list))))
15200        (t
15201         (if (and (>= level gnus-level-zombie)
15202                  entry)
15203             (progn
15204               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15205               (if (nth 3 entry)
15206                   (setcdr (gnus-gethash (car (nth 3 entry))
15207                                         gnus-newsrc-hashtb)
15208                           (cdr entry)))
15209               (setcdr (cdr entry) (cdddr entry))))))
15210
15211       ;; Finally we enter (if needed) the list where it is supposed to
15212       ;; go, and change the subscription level.  If it is to be killed,
15213       ;; we enter it into the killed or zombie list.
15214       (cond 
15215        ((>= level gnus-level-zombie)
15216         ;; Remove from the hash table.
15217         (gnus-sethash group nil gnus-newsrc-hashtb)
15218         ;; We do not enter foreign groups into the list of dead
15219         ;; groups.
15220         (unless (gnus-group-foreign-p group)
15221           (if (= level gnus-level-zombie)
15222               (setq gnus-zombie-list (cons group gnus-zombie-list))
15223             (setq gnus-killed-list (cons group gnus-killed-list)))))
15224        (t
15225         ;; If the list is to be entered into the newsrc assoc, and
15226         ;; it was killed, we have to create an entry in the newsrc
15227         ;; hashtb format and fix the pointers in the newsrc assoc.
15228         (if (< oldlevel gnus-level-zombie)
15229             ;; It was alive, and it is going to stay alive, so we
15230             ;; just change the level and don't change any pointers or
15231             ;; hash table entries.
15232             (setcar (cdaddr entry) level)
15233           (if (listp entry)
15234               (setq info (cdr entry)
15235                     num (car entry))
15236             (setq active (gnus-active group))
15237             (setq num
15238                   (if active (- (1+ (cdr active)) (car active)) t))
15239             ;; Check whether the group is foreign.  If so, the
15240             ;; foreign select method has to be entered into the
15241             ;; info.
15242             (let ((method (or gnus-override-subscribe-method
15243                               (gnus-group-method group))))
15244               (if (eq method gnus-select-method)
15245                   (setq info (list group level nil))
15246                 (setq info (list group level nil nil method)))))
15247           (unless previous
15248             (setq previous
15249                   (let ((p gnus-newsrc-alist))
15250                     (while (cddr p)
15251                       (setq p (cdr p)))
15252                     p)))
15253           (setq entry (cons info (cddr previous)))
15254           (if (cdr previous)
15255               (progn
15256                 (setcdr (cdr previous) entry)
15257                 (gnus-sethash group (cons num (cdr previous))
15258                               gnus-newsrc-hashtb))
15259             (setcdr previous entry)
15260             (gnus-sethash group (cons num previous)
15261                           gnus-newsrc-hashtb))
15262           (when (cdr entry)
15263             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15264       (when gnus-group-change-level-function
15265         (funcall gnus-group-change-level-function group level oldlevel)))))
15266
15267 (defun gnus-kill-newsgroup (newsgroup)
15268   "Obsolete function.  Kills a newsgroup."
15269   (gnus-group-change-level
15270    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15271
15272 (defun gnus-check-bogus-newsgroups (&optional confirm)
15273   "Remove bogus newsgroups.
15274 If CONFIRM is non-nil, the user has to confirm the deletion of every
15275 newsgroup."
15276   (let ((newsrc (cdr gnus-newsrc-alist))
15277         bogus group entry info)
15278     (gnus-message 5 "Checking bogus newsgroups...")
15279     (unless gnus-have-read-active-file
15280       (gnus-read-active-file))
15281     (when (member gnus-select-method gnus-have-read-active-file)
15282       ;; Find all bogus newsgroup that are subscribed.
15283       (while newsrc
15284         (setq info (pop newsrc)
15285               group (gnus-info-group info))
15286         (unless (or (gnus-active group) ; Active
15287                     (gnus-info-method info) ; Foreign
15288                     (and confirm
15289                          (not (gnus-y-or-n-p
15290                                (format "Remove bogus newsgroup: %s " group)))))
15291           ;; Found a bogus newsgroup.
15292           (push group bogus)))
15293       ;; Remove all bogus subscribed groups by first killing them, and
15294       ;; then removing them from the list of killed groups.
15295       (while bogus
15296         (when (setq entry (gnus-gethash (setq group (pop bogus))
15297                                         gnus-newsrc-hashtb))
15298           (gnus-group-change-level entry gnus-level-killed)
15299           (setq gnus-killed-list (delete group gnus-killed-list))))
15300       ;; Then we remove all bogus groups from the list of killed and
15301       ;; zombie groups.  They are are removed without confirmation.
15302       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15303             killed)
15304         (while dead-lists
15305           (setq killed (symbol-value (car dead-lists)))
15306           (while killed
15307             (unless (gnus-active (setq group (pop killed)))
15308               ;; The group is bogus.
15309               ;; !!!Slow as hell.
15310               (set (car dead-lists)
15311                    (delete group (symbol-value (car dead-lists))))))
15312           (setq dead-lists (cdr dead-lists))))
15313       (gnus-message 5 "Checking bogus newsgroups...done"))))
15314
15315 (defun gnus-check-duplicate-killed-groups ()
15316   "Remove duplicates from the list of killed groups."
15317   (interactive)
15318   (let ((killed gnus-killed-list))
15319     (while killed
15320       (gnus-message 9 "%d" (length killed))
15321       (setcdr killed (delete (car killed) (cdr killed)))
15322       (setq killed (cdr killed)))))
15323
15324 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15325 ;; and compute how many unread articles there are in each group.
15326 (defun gnus-get-unread-articles (&optional level)
15327   (let* ((newsrc (cdr gnus-newsrc-alist))
15328          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15329          (foreign-level
15330           (min
15331            (cond ((and gnus-activate-foreign-newsgroups
15332                        (not (numberp gnus-activate-foreign-newsgroups)))
15333                   (1+ gnus-level-subscribed))
15334                  ((numberp gnus-activate-foreign-newsgroups)
15335                   gnus-activate-foreign-newsgroups)
15336                  (t 0))
15337            level))
15338          info group active method)
15339     (gnus-message 5 "Checking new news...")
15340
15341     (while newsrc
15342       (setq active (gnus-active (setq group (gnus-info-group
15343                                              (setq info (pop newsrc))))))
15344
15345       ;; Check newsgroups.  If the user doesn't want to check them, or
15346       ;; they can't be checked (for instance, if the news server can't
15347       ;; be reached) we just set the number of unread articles in this
15348       ;; newsgroup to t.  This means that Gnus thinks that there are
15349       ;; unread articles, but it has no idea how many.
15350       (if (and (setq method (gnus-info-method info))
15351                (not (gnus-server-equal
15352                      gnus-select-method
15353                      (gnus-server-get-method nil method)))
15354                (not (gnus-secondary-method-p method)))
15355           ;; These groups are foreign.  Check the level.
15356           (when (<= (gnus-info-level info) foreign-level)
15357             (setq active (gnus-activate-group group 'scan))
15358             (gnus-close-group group))
15359
15360         ;; These groups are native or secondary.
15361         (when (and (<= (gnus-info-level info) level)
15362                    (not gnus-read-active-file))
15363           (setq active (gnus-activate-group group 'scan))
15364           (gnus-close-group group)))
15365
15366       (if active
15367           (gnus-get-unread-articles-in-group info active t)
15368         ;; The group couldn't be reached, so we nix out the number of
15369         ;; unread articles and stuff.
15370         (gnus-set-active group nil)
15371         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15372
15373     (gnus-message 5 "Checking new news...done")))
15374
15375 ;; Create a hash table out of the newsrc alist.  The `car's of the
15376 ;; alist elements are used as keys.
15377 (defun gnus-make-hashtable-from-newsrc-alist ()
15378   (let ((alist gnus-newsrc-alist)
15379         (ohashtb gnus-newsrc-hashtb)
15380         prev)
15381     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15382     (setq alist
15383           (setq prev (setq gnus-newsrc-alist
15384                            (if (equal (caar gnus-newsrc-alist)
15385                                       "dummy.group")
15386                                gnus-newsrc-alist
15387                              (cons (list "dummy.group" 0 nil) alist)))))
15388     (while alist
15389       (gnus-sethash
15390        (caar alist)
15391        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15392              prev)
15393        gnus-newsrc-hashtb)
15394       (setq prev alist
15395             alist (cdr alist)))))
15396
15397 (defun gnus-make-hashtable-from-killed ()
15398   "Create a hash table from the killed and zombie lists."
15399   (let ((lists '(gnus-killed-list gnus-zombie-list))
15400         list)
15401     (setq gnus-killed-hashtb
15402           (gnus-make-hashtable
15403            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15404     (while (setq list (symbol-value (pop lists)))
15405       (while list
15406         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15407
15408 (defun gnus-get-unread-articles-in-group (info active &optional update)
15409   (when active
15410     ;; Allow the backend to update the info in the group.
15411     (when (and update 
15412                (gnus-request-update-info
15413                 info (gnus-find-method-for-group (gnus-info-group info))))
15414       (gnus-activate-group (gnus-info-group info)))
15415     (let* ((range (gnus-info-read info))
15416            (num 0))
15417       ;; If a cache is present, we may have to alter the active info.
15418       (when (and gnus-use-cache info)
15419         (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15420       ;; Modify the list of read articles according to what articles
15421       ;; are available; then tally the unread articles and add the
15422       ;; number to the group hash table entry.
15423       (cond
15424        ((zerop (cdr active))
15425         (setq num 0))
15426        ((not range)
15427         (setq num (- (1+ (cdr active)) (car active))))
15428        ((not (listp (cdr range)))
15429         ;; Fix a single (num . num) range according to the
15430         ;; active hash table.
15431         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15432         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15433         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15434         ;; Compute number of unread articles.
15435         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15436        (t
15437         ;; The read list is a list of ranges.  Fix them according to
15438         ;; the active hash table.
15439         ;; First peel off any elements that are below the lower
15440         ;; active limit.
15441         (while (and (cdr range)
15442                     (>= (car active)
15443                         (or (and (atom (cadr range)) (cadr range))
15444                             (caadr range))))
15445           (if (numberp (car range))
15446               (setcar range
15447                       (cons (car range)
15448                             (or (and (numberp (cadr range))
15449                                      (cadr range))
15450                                 (cdadr range))))
15451             (setcdr (car range)
15452                     (or (and (numberp (nth 1 range)) (nth 1 range))
15453                         (cdadr range))))
15454           (setcdr range (cddr range)))
15455         ;; Adjust the first element to be the same as the lower limit.
15456         (if (and (not (atom (car range)))
15457                  (< (cdar range) (car active)))
15458             (setcdr (car range) (1- (car active))))
15459         ;; Then we want to peel off any elements that are higher
15460         ;; than the upper active limit.
15461         (let ((srange range))
15462           ;; Go past all legal elements.
15463           (while (and (cdr srange)
15464                       (<= (or (and (atom (cadr srange))
15465                                    (cadr srange))
15466                               (caadr srange)) (cdr active)))
15467             (setq srange (cdr srange)))
15468           (if (cdr srange)
15469               ;; Nuke all remaining illegal elements.
15470               (setcdr srange nil))
15471
15472           ;; Adjust the final element.
15473           (if (and (not (atom (car srange)))
15474                    (> (cdar srange) (cdr active)))
15475               (setcdr (car srange) (cdr active))))
15476         ;; Compute the number of unread articles.
15477         (while range
15478           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15479                                       (cdar range)))
15480                               (or (and (atom (car range)) (car range))
15481                                   (caar range)))))
15482           (setq range (cdr range)))
15483         (setq num (max 0 (- (cdr active) num)))))
15484       ;; Set the number of unread articles.
15485       (when info
15486         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15487       num)))
15488
15489 (defun gnus-activate-group (group &optional scan)
15490   ;; Check whether a group has been activated or not.
15491   ;; If SCAN, request a scan of that group as well.
15492   (let ((method (gnus-find-method-for-group group))
15493         active)
15494     (and (gnus-check-server method)
15495          ;; We escape all bugs and quit here to make it possible to
15496          ;; continue if a group is so out-there that it reports bugs
15497          ;; and stuff.
15498          (progn
15499            (and scan
15500                 (gnus-check-backend-function 'request-scan (car method))
15501                 (gnus-request-scan group method))
15502            t)
15503          (condition-case ()
15504              (gnus-request-group group)
15505         ;   (error nil)
15506            (quit nil))
15507          (save-excursion
15508            (set-buffer nntp-server-buffer)
15509            (goto-char (point-min))
15510            ;; Parse the result we got from `gnus-request-group'.
15511            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15512                 (progn
15513                   (goto-char (match-beginning 1))
15514                   (gnus-set-active
15515                    group (setq active (cons (read (current-buffer))
15516                                             (read (current-buffer)))))
15517                   ;; Return the new active info.
15518                   active))))))
15519
15520 (defun gnus-update-read-articles (group unread)
15521   "Update the list of read and ticked articles in GROUP using the
15522 UNREAD and TICKED lists.
15523 Note: UNSELECTED has to be sorted over `<'.
15524 Returns whether the updating was successful."
15525   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15526          (entry (gnus-gethash group gnus-newsrc-hashtb))
15527          (info (nth 2 entry))
15528          (prev 1)
15529          (unread (sort (copy-sequence unread) '<))
15530          read)
15531     (if (or (not info) (not active))
15532         ;; There is no info on this group if it was, in fact,
15533         ;; killed.  Gnus stores no information on killed groups, so
15534         ;; there's nothing to be done.
15535         ;; One could store the information somewhere temporarily,
15536         ;; perhaps...  Hmmm...
15537         ()
15538       ;; Remove any negative articles numbers.
15539       (while (and unread (< (car unread) 0))
15540         (setq unread (cdr unread)))
15541       ;; Remove any expired article numbers
15542       (while (and unread (< (car unread) (car active)))
15543         (setq unread (cdr unread)))
15544       ;; Compute the ranges of read articles by looking at the list of
15545       ;; unread articles.
15546       (while unread
15547         (if (/= (car unread) prev)
15548             (setq read (cons (if (= prev (1- (car unread))) prev
15549                                (cons prev (1- (car unread)))) read)))
15550         (setq prev (1+ (car unread)))
15551         (setq unread (cdr unread)))
15552       (when (<= prev (cdr active))
15553         (setq read (cons (cons prev (cdr active)) read)))
15554       ;; Enter this list into the group info.
15555       (gnus-info-set-read
15556        info (if (> (length read) 1) (nreverse read) read))
15557       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15558       (gnus-get-unread-articles-in-group info (gnus-active group))
15559       t)))
15560
15561 (defun gnus-make-articles-unread (group articles)
15562   "Mark ARTICLES in GROUP as unread."
15563   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15564                           (gnus-gethash (gnus-group-real-name group)
15565                                         gnus-newsrc-hashtb))))
15566          (ranges (gnus-info-read info))
15567          news article)
15568     (while articles
15569       (when (gnus-member-of-range
15570              (setq article (pop articles)) ranges)
15571         (setq news (cons article news))))
15572     (when news
15573       (gnus-info-set-read
15574        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15575       (gnus-group-update-group group t))))
15576
15577 ;; Enter all dead groups into the hashtb.
15578 (defun gnus-update-active-hashtb-from-killed ()
15579   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15580         (lists (list gnus-killed-list gnus-zombie-list))
15581         killed)
15582     (while lists
15583       (setq killed (car lists))
15584       (while killed
15585         (gnus-sethash (car killed) nil hashtb)
15586         (setq killed (cdr killed)))
15587       (setq lists (cdr lists)))))
15588
15589 ;; Get the active file(s) from the backend(s).
15590 (defun gnus-read-active-file ()
15591   (gnus-group-set-mode-line)
15592   (let ((methods 
15593          (append
15594           (if (gnus-check-server gnus-select-method)
15595               ;; The native server is available.
15596               (cons gnus-select-method gnus-secondary-select-methods)
15597             ;; The native server is down, so we just do the
15598             ;; secondary ones.
15599             gnus-secondary-select-methods)
15600           ;; Also read from the archive server.
15601           (when gnus-message-archive-method
15602             (list "archive"))))
15603         list-type)
15604     (setq gnus-have-read-active-file nil)
15605     (save-excursion
15606       (set-buffer nntp-server-buffer)
15607       (while methods
15608         (let* ((method (if (stringp (car methods))
15609                            (gnus-server-get-method nil (car methods))
15610                          (car methods)))
15611                (where (nth 1 method))
15612                (mesg (format "Reading active file%s via %s..."
15613                              (if (and where (not (zerop (length where))))
15614                                  (concat " from " where) "")
15615                              (car method))))
15616           (gnus-message 5 mesg)
15617           (when (gnus-check-server method)
15618             ;; Request that the backend scan its incoming messages.
15619             (and (gnus-check-backend-function 'request-scan (car method))
15620                  (gnus-request-scan nil method))
15621             (cond
15622              ((and (eq gnus-read-active-file 'some)
15623                    (gnus-check-backend-function 'retrieve-groups (car method)))
15624               (let ((newsrc (cdr gnus-newsrc-alist))
15625                     (gmethod (if (stringp method)
15626                                  (gnus-server-get-method nil method)
15627                                method))
15628                     groups info)
15629                 (while (setq info (pop newsrc))
15630                   (when (gnus-server-equal
15631                          (gnus-find-method-for-group 
15632                           (gnus-info-group info) info)
15633                          gmethod)
15634                     (push (gnus-group-real-name (gnus-info-group info)) 
15635                           groups)))
15636                 (when groups
15637                   (gnus-check-server method)
15638                   (setq list-type (gnus-retrieve-groups groups method))
15639                   (cond
15640                    ((not list-type)
15641                     (gnus-message
15642                      1 "Cannot read partial active file from %s server."
15643                      (car method))
15644                     (ding)
15645                     (sit-for 2))
15646                    ((eq list-type 'active)
15647                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15648                    (t
15649                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15650              (t
15651               (if (not (gnus-request-list method))
15652                   (progn
15653                     (unless (equal method gnus-message-archive-method)
15654                       (gnus-message 1 "Cannot read active file from %s server."
15655                                     (car method))
15656                       (ding)))
15657                 (gnus-active-to-gnus-format method)
15658                 ;; We mark this active file as read.
15659                 (push method gnus-have-read-active-file)
15660                 (gnus-message 5 "%sdone" mesg))))))
15661         (setq methods (cdr methods))))))
15662
15663 ;; Read an active file and place the results in `gnus-active-hashtb'.
15664 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15665   (unless method
15666     (setq method gnus-select-method))
15667   (let ((cur (current-buffer))
15668         (hashtb (or hashtb
15669                     (if (and gnus-active-hashtb
15670                              (not (equal method gnus-select-method)))
15671                         gnus-active-hashtb
15672                       (setq gnus-active-hashtb
15673                             (if (equal method gnus-select-method)
15674                                 (gnus-make-hashtable
15675                                  (count-lines (point-min) (point-max)))
15676                               (gnus-make-hashtable 4096)))))))
15677     ;; Delete unnecessary lines.
15678     (goto-char (point-min))
15679     (while (search-forward "\nto." nil t)
15680       (delete-region (1+ (match-beginning 0))
15681                      (progn (forward-line 1) (point))))
15682     (or (string= gnus-ignored-newsgroups "")
15683         (progn
15684           (goto-char (point-min))
15685           (delete-matching-lines gnus-ignored-newsgroups)))
15686     ;; Make the group names readable as a lisp expression even if they
15687     ;; contain special characters.
15688     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15689     (goto-char (point-max))
15690     (while (re-search-backward "[][';?()#]" nil t)
15691       (insert ?\\))
15692     ;; If these are groups from a foreign select method, we insert the
15693     ;; group prefix in front of the group names.
15694     (and method (not (gnus-server-equal
15695                       (gnus-server-get-method nil method)
15696                       (gnus-server-get-method nil gnus-select-method)))
15697          (let ((prefix (gnus-group-prefixed-name "" method)))
15698            (goto-char (point-min))
15699            (while (and (not (eobp))
15700                        (progn (insert prefix)
15701                               (zerop (forward-line 1)))))))
15702     ;; Store the active file in a hash table.
15703     (goto-char (point-min))
15704     (if (string-match "%[oO]" gnus-group-line-format)
15705         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15706         ;; If we want information on moderated groups, we use this
15707         ;; loop...
15708         (let* ((mod-hashtb (make-vector 7 0))
15709                (m (intern "m" mod-hashtb))
15710                group max min)
15711           (while (not (eobp))
15712             (condition-case nil
15713                 (progn
15714                   (narrow-to-region (point) (gnus-point-at-eol))
15715                   (setq group (let ((obarray hashtb)) (read cur)))
15716                   (if (and (numberp (setq max (read cur)))
15717                            (numberp (setq min (read cur)))
15718                            (progn
15719                              (skip-chars-forward " \t")
15720                              (not
15721                               (or (= (following-char) ?=)
15722                                   (= (following-char) ?x)
15723                                   (= (following-char) ?j)))))
15724                       (set group (cons min max))
15725                     (set group nil))
15726                   ;; Enter moderated groups into a list.
15727                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15728                       (setq gnus-moderated-list
15729                             (cons (symbol-name group) gnus-moderated-list))))
15730               (error
15731                (and group
15732                     (symbolp group)
15733                     (set group nil))))
15734             (widen)
15735             (forward-line 1)))
15736       ;; And if we do not care about moderation, we use this loop,
15737       ;; which is faster.
15738       (let (group max min)
15739         (while (not (eobp))
15740           (condition-case ()
15741               (progn
15742                 (narrow-to-region (point) (gnus-point-at-eol))
15743                 ;; group gets set to a symbol interned in the hash table
15744                 ;; (what a hack!!) - jwz
15745                 (setq group (let ((obarray hashtb)) (read cur)))
15746                 (if (and (numberp (setq max (read cur)))
15747                          (numberp (setq min (read cur)))
15748                          (progn
15749                            (skip-chars-forward " \t")
15750                            (not
15751                             (or (= (following-char) ?=)
15752                                 (= (following-char) ?x)
15753                                 (= (following-char) ?j)))))
15754                     (set group (cons min max))
15755                   (set group nil)))
15756             (error
15757              (progn
15758                (and group
15759                     (symbolp group)
15760                     (set group nil))
15761                (or ignore-errors
15762                    (gnus-message 3 "Warning - illegal active: %s"
15763                                  (buffer-substring
15764                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15765           (widen)
15766           (forward-line 1))))))
15767
15768 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15769   ;; Parse a "groups" active file.
15770   (let ((cur (current-buffer))
15771         (hashtb (or hashtb
15772                     (if (and method gnus-active-hashtb)
15773                         gnus-active-hashtb
15774                       (setq gnus-active-hashtb
15775                             (gnus-make-hashtable
15776                              (count-lines (point-min) (point-max)))))))
15777         (prefix (and method
15778                      (not (gnus-server-equal
15779                            (gnus-server-get-method nil method)
15780                            (gnus-server-get-method nil gnus-select-method)))
15781                      (gnus-group-prefixed-name "" method))))
15782
15783     (goto-char (point-min))
15784     ;; We split this into to separate loops, one with the prefix
15785     ;; and one without to speed the reading up somewhat.
15786     (if prefix
15787         (let (min max opoint group)
15788           (while (not (eobp))
15789             (condition-case ()
15790                 (progn
15791                   (read cur) (read cur)
15792                   (setq min (read cur)
15793                         max (read cur)
15794                         opoint (point))
15795                   (skip-chars-forward " \t")
15796                   (insert prefix)
15797                   (goto-char opoint)
15798                   (set (let ((obarray hashtb)) (read cur))
15799                        (cons min max)))
15800               (error (and group (symbolp group) (set group nil))))
15801             (forward-line 1)))
15802       (let (min max group)
15803         (while (not (eobp))
15804           (condition-case ()
15805               (if (= (following-char) ?2)
15806                   (progn
15807                     (read cur) (read cur)
15808                     (setq min (read cur)
15809                           max (read cur))
15810                     (set (setq group (let ((obarray hashtb)) (read cur)))
15811                          (cons min max))))
15812             (error (and group (symbolp group) (set group nil))))
15813           (forward-line 1))))))
15814
15815 (defun gnus-read-newsrc-file (&optional force)
15816   "Read startup file.
15817 If FORCE is non-nil, the .newsrc file is read."
15818   ;; Reset variables that might be defined in the .newsrc.eld file.
15819   (let ((variables gnus-variable-list))
15820     (while variables
15821       (set (car variables) nil)
15822       (setq variables (cdr variables))))
15823   (let* ((newsrc-file gnus-current-startup-file)
15824          (quick-file (concat newsrc-file ".el")))
15825     (save-excursion
15826       ;; We always load the .newsrc.eld file.  If always contains
15827       ;; much information that can not be gotten from the .newsrc
15828       ;; file (ticked articles, killed groups, foreign methods, etc.)
15829       (gnus-read-newsrc-el-file quick-file)
15830
15831       (if (and (file-exists-p gnus-current-startup-file)
15832                (or force
15833                    (and (file-newer-than-file-p newsrc-file quick-file)
15834                         (file-newer-than-file-p newsrc-file
15835                                                 (concat quick-file "d")))
15836                    (not gnus-newsrc-alist)))
15837           ;; We read the .newsrc file.  Note that if there if a
15838           ;; .newsrc.eld file exists, it has already been read, and
15839           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15840           ;; the .newsrc file, Gnus will only use the information it
15841           ;; can find there for changing the data already read -
15842           ;; ie. reading the .newsrc file will not trash the data
15843           ;; already read (except for read articles).
15844           (save-excursion
15845             (gnus-message 5 "Reading %s..." newsrc-file)
15846             (set-buffer (find-file-noselect newsrc-file))
15847             (buffer-disable-undo (current-buffer))
15848             (gnus-newsrc-to-gnus-format)
15849             (kill-buffer (current-buffer))
15850             (gnus-message 5 "Reading %s...done" newsrc-file)))
15851
15852       ;; Read any slave files.
15853       (or gnus-slave
15854           (gnus-master-read-slave-newsrc)))))
15855
15856 (defun gnus-read-newsrc-el-file (file)
15857   (let ((ding-file (concat file "d")))
15858     ;; We always, always read the .eld file.
15859     (gnus-message 5 "Reading %s..." ding-file)
15860     (let (gnus-newsrc-assoc)
15861       (condition-case nil
15862           (load ding-file t t t)
15863         (error
15864          (gnus-message 1 "Error in %s" ding-file)
15865          (ding)))
15866       (when gnus-newsrc-assoc
15867         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15868     (gnus-make-hashtable-from-newsrc-alist)
15869     (when (file-newer-than-file-p file ding-file)
15870       ;; Old format quick file
15871       (gnus-message 5 "Reading %s..." file)
15872       ;; The .el file is newer than the .eld file, so we read that one
15873       ;; as well.
15874       (gnus-read-old-newsrc-el-file file))))
15875
15876 ;; Parse the old-style quick startup file
15877 (defun gnus-read-old-newsrc-el-file (file)
15878   (let (newsrc killed marked group m info)
15879     (prog1
15880         (let ((gnus-killed-assoc nil)
15881               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15882           (prog1
15883               (condition-case nil
15884                   (load file t t t)
15885                 (error nil))
15886             (setq newsrc gnus-newsrc-assoc
15887                   killed gnus-killed-assoc
15888                   marked gnus-marked-assoc)))
15889       (setq gnus-newsrc-alist nil)
15890       (while (setq info (gnus-get-info (setq group (pop newsrc))))
15891         (if info
15892             (progn
15893               (gnus-info-set-read info (cddr group))
15894               (gnus-info-set-level
15895                info (if (nth 1 group) gnus-level-default-subscribed
15896                       gnus-level-default-unsubscribed))
15897               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15898           (push (setq info
15899                       (list (car group)
15900                             (if (nth 1 group) gnus-level-default-subscribed
15901                               gnus-level-default-unsubscribed)
15902                             (cddr group)))
15903                 gnus-newsrc-alist))
15904         ;; Copy marks into info.
15905         (when (setq m (assoc (car group) marked))
15906           (unless (nthcdr 3 info)
15907             (nconc info (list nil)))
15908           (gnus-info-set-marks
15909            info (list (cons 'tick (gnus-compress-sequence 
15910                                    (sort (cdr m) '<) t))))))
15911       (setq newsrc killed)
15912       (while newsrc
15913         (setcar newsrc (caar newsrc))
15914         (setq newsrc (cdr newsrc)))
15915       (setq gnus-killed-list killed))
15916     ;; The .el file version of this variable does not begin with
15917     ;; "options", while the .eld version does, so we just add it if it
15918     ;; isn't there.
15919     (and
15920      gnus-newsrc-options
15921      (progn
15922        (and (not (string-match "^ *options" gnus-newsrc-options))
15923             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15924        (and (not (string-match "\n$" gnus-newsrc-options))
15925             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15926        ;; Finally, if we read some options lines, we parse them.
15927        (or (string= gnus-newsrc-options "")
15928            (gnus-newsrc-parse-options gnus-newsrc-options))))
15929
15930     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15931     (gnus-make-hashtable-from-newsrc-alist)))
15932
15933 (defun gnus-make-newsrc-file (file)
15934   "Make server dependent file name by catenating FILE and server host name."
15935   (let* ((file (expand-file-name file nil))
15936          (real-file (concat file "-" (nth 1 gnus-select-method))))
15937     (if (or (file-exists-p real-file)
15938             (file-exists-p (concat real-file ".el"))
15939             (file-exists-p (concat real-file ".eld")))
15940         real-file file)))
15941
15942 (defun gnus-newsrc-to-gnus-format ()
15943   (setq gnus-newsrc-options "")
15944   (setq gnus-newsrc-options-n nil)
15945
15946   (or gnus-active-hashtb
15947       (setq gnus-active-hashtb (make-vector 4095 0)))
15948   (let ((buf (current-buffer))
15949         (already-read (> (length gnus-newsrc-alist) 1))
15950         group subscribed options-symbol newsrc Options-symbol
15951         symbol reads num1)
15952     (goto-char (point-min))
15953     ;; We intern the symbol `options' in the active hashtb so that we
15954     ;; can `eq' against it later.
15955     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15956     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15957
15958     (while (not (eobp))
15959       ;; We first read the first word on the line by narrowing and
15960       ;; then reading into `gnus-active-hashtb'.  Most groups will
15961       ;; already exist in that hashtb, so this will save some string
15962       ;; space.
15963       (narrow-to-region
15964        (point)
15965        (progn (skip-chars-forward "^ \t!:\n") (point)))
15966       (goto-char (point-min))
15967       (setq symbol
15968             (and (/= (point-min) (point-max))
15969                  (let ((obarray gnus-active-hashtb)) (read buf))))
15970       (widen)
15971       ;; Now, the symbol we have read is either `options' or a group
15972       ;; name.  If it is an options line, we just add it to a string.
15973       (cond
15974        ((or (eq symbol options-symbol)
15975             (eq symbol Options-symbol))
15976         (setq gnus-newsrc-options
15977               ;; This concating is quite inefficient, but since our
15978               ;; thorough studies show that approx 99.37% of all
15979               ;; .newsrc files only contain a single options line, we
15980               ;; don't give a damn, frankly, my dear.
15981               (concat gnus-newsrc-options
15982                       (buffer-substring
15983                        (gnus-point-at-bol)
15984                        ;; Options may continue on the next line.
15985                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15986                                 (progn (beginning-of-line) (point)))
15987                            (point)))))
15988         (forward-line -1))
15989        (symbol
15990         ;; Group names can be just numbers.  
15991         (when (numberp symbol) 
15992           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15993         (or (boundp symbol) (set symbol nil))
15994         ;; It was a group name.
15995         (setq subscribed (= (following-char) ?:)
15996               group (symbol-name symbol)
15997               reads nil)
15998         (if (eolp)
15999             ;; If the line ends here, this is clearly a buggy line, so
16000             ;; we put point a the beginning of line and let the cond
16001             ;; below do the error handling.
16002             (beginning-of-line)
16003           ;; We skip to the beginning of the ranges.
16004           (skip-chars-forward "!: \t"))
16005         ;; We are now at the beginning of the list of read articles.
16006         ;; We read them range by range.
16007         (while
16008             (cond
16009              ((looking-at "[0-9]+")
16010               ;; We narrow and read a number instead of buffer-substring/
16011               ;; string-to-int because it's faster.  narrow/widen is
16012               ;; faster than save-restriction/narrow, and save-restriction
16013               ;; produces a garbage object.
16014               (setq num1 (progn
16015                            (narrow-to-region (match-beginning 0) (match-end 0))
16016                            (read buf)))
16017               (widen)
16018               ;; If the next character is a dash, then this is a range.
16019               (if (= (following-char) ?-)
16020                   (progn
16021                     ;; We read the upper bound of the range.
16022                     (forward-char 1)
16023                     (if (not (looking-at "[0-9]+"))
16024                         ;; This is a buggy line, by we pretend that
16025                         ;; it's kinda OK.  Perhaps the user should be
16026                         ;; dinged?
16027                         (setq reads (cons num1 reads))
16028                       (setq reads
16029                             (cons
16030                              (cons num1
16031                                    (progn
16032                                      (narrow-to-region (match-beginning 0)
16033                                                        (match-end 0))
16034                                      (read buf)))
16035                              reads))
16036                       (widen)))
16037                 ;; It was just a simple number, so we add it to the
16038                 ;; list of ranges.
16039                 (setq reads (cons num1 reads)))
16040               ;; If the next char in ?\n, then we have reached the end
16041               ;; of the line and return nil.
16042               (/= (following-char) ?\n))
16043              ((= (following-char) ?\n)
16044               ;; End of line, so we end.
16045               nil)
16046              (t
16047               ;; Not numbers and not eol, so this might be a buggy
16048               ;; line...
16049               (or (eobp)
16050                   ;; If it was eob instead of ?\n, we allow it.
16051                   (progn
16052                     ;; The line was buggy.
16053                     (setq group nil)
16054                     (gnus-message 3 "Mangled line: %s"
16055                                   (buffer-substring (gnus-point-at-bol)
16056                                                     (gnus-point-at-eol)))
16057                     (ding)
16058                     (sit-for 1)))
16059               nil))
16060           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16061           ;; we allow them, because it's a common mistake to put a
16062           ;; space after the comma.
16063           (skip-chars-forward ", "))
16064
16065         ;; We have already read .newsrc.eld, so we gently update the
16066         ;; data in the hash table with the information we have just
16067         ;; read.
16068         (when group
16069           (let ((info (gnus-get-info group))
16070                 level)
16071             (if info
16072                 ;; There is an entry for this file in the alist.
16073                 (progn
16074                   (gnus-info-set-read info (nreverse reads))
16075                   ;; We update the level very gently.  In fact, we
16076                   ;; only change it if there's been a status change
16077                   ;; from subscribed to unsubscribed, or vice versa.
16078                   (setq level (gnus-info-level info))
16079                   (cond ((and (<= level gnus-level-subscribed)
16080                               (not subscribed))
16081                          (setq level (if reads
16082                                          gnus-level-default-unsubscribed
16083                                        (1+ gnus-level-default-unsubscribed))))
16084                         ((and (> level gnus-level-subscribed) subscribed)
16085                          (setq level gnus-level-default-subscribed)))
16086                   (gnus-info-set-level info level))
16087               ;; This is a new group.
16088               (setq info (list group
16089                                (if subscribed
16090                                    gnus-level-default-subscribed
16091                                  (if reads
16092                                      (1+ gnus-level-subscribed)
16093                                    gnus-level-default-unsubscribed))
16094                                (nreverse reads))))
16095             (setq newsrc (cons info newsrc))))))
16096       (forward-line 1))
16097
16098     (setq newsrc (nreverse newsrc))
16099
16100     (if (not already-read)
16101         ()
16102       ;; We now have two newsrc lists - `newsrc', which is what we
16103       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16104       ;; what we've read from .newsrc.eld.  We have to merge these
16105       ;; lists.  We do this by "attaching" any (foreign) groups in the
16106       ;; gnus-newsrc-alist to the (native) group that precedes them.
16107       (let ((rc (cdr gnus-newsrc-alist))
16108             (prev gnus-newsrc-alist)
16109             entry mentry)
16110         (while rc
16111           (or (null (nth 4 (car rc)))   ; It's a native group.
16112               (assoc (caar rc) newsrc) ; It's already in the alist.
16113               (if (setq entry (assoc (caar prev) newsrc))
16114                   (setcdr (setq mentry (memq entry newsrc))
16115                           (cons (car rc) (cdr mentry)))
16116                 (setq newsrc (cons (car rc) newsrc))))
16117           (setq prev rc
16118                 rc (cdr rc)))))
16119
16120     (setq gnus-newsrc-alist newsrc)
16121     ;; We make the newsrc hashtb.
16122     (gnus-make-hashtable-from-newsrc-alist)
16123
16124     ;; Finally, if we read some options lines, we parse them.
16125     (or (string= gnus-newsrc-options "")
16126         (gnus-newsrc-parse-options gnus-newsrc-options))))
16127
16128 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16129 ;; The return value will be a list on the form
16130 ;; ((regexp1 . ignore)
16131 ;;  (regexp2 . subscribe)...)
16132 ;; When handling new newsgroups, groups that match a `ignore' regexp
16133 ;; will be ignored, and groups that match a `subscribe' regexp will be
16134 ;; subscribed.  A line like
16135 ;; options -n !all rec.all
16136 ;; will lead to a list that looks like
16137 ;; (("^rec\\..+" . subscribe)
16138 ;;  ("^.+" . ignore))
16139 ;; So all "rec.*" groups will be subscribed, while all the other
16140 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16141 ;; different from "options -n rec.all !all".
16142 (defun gnus-newsrc-parse-options (options)
16143   (let (out eol)
16144     (save-excursion
16145       (gnus-set-work-buffer)
16146       (insert (regexp-quote options))
16147       ;; First we treat all continuation lines.
16148       (goto-char (point-min))
16149       (while (re-search-forward "\n[ \t]+" nil t)
16150         (replace-match " " t t))
16151       ;; Then we transform all "all"s into ".+"s.
16152       (goto-char (point-min))
16153       (while (re-search-forward "\\ball\\b" nil t)
16154         (replace-match ".+" t t))
16155       (goto-char (point-min))
16156       ;; We remove all other options than the "-n" ones.
16157       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16158         (replace-match " ")
16159         (forward-char -1))
16160       (goto-char (point-min))
16161
16162       ;; We are only interested in "options -n" lines - we
16163       ;; ignore the other option lines.
16164       (while (re-search-forward "[ \t]-n" nil t)
16165         (setq eol
16166               (or (save-excursion
16167                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16168                          (- (point) 2)))
16169                   (gnus-point-at-eol)))
16170         ;; Search for all "words"...
16171         (while (re-search-forward "[^ \t,\n]+" eol t)
16172           (if (= (char-after (match-beginning 0)) ?!)
16173               ;; If the word begins with a bang (!), this is a "not"
16174               ;; spec.  We put this spec (minus the bang) and the
16175               ;; symbol `ignore' into the list.
16176               (setq out (cons (cons (concat
16177                                      "^" (buffer-substring
16178                                           (1+ (match-beginning 0))
16179                                           (match-end 0)))
16180                                     'ignore) out))
16181             ;; There was no bang, so this is a "yes" spec.
16182             (setq out (cons (cons (concat "^" (match-string 0))
16183                                   'subscribe) out)))))
16184
16185       (setq gnus-newsrc-options-n out))))
16186
16187 (defun gnus-save-newsrc-file (&optional force)
16188   "Save .newsrc file."
16189   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16190   ;; from the variable gnus-newsrc-alist.
16191   (when (and (or gnus-newsrc-alist gnus-killed-list)
16192              gnus-current-startup-file)
16193     (save-excursion
16194       (if (and (or gnus-use-dribble-file gnus-slave)
16195                (not force)
16196                (or (not gnus-dribble-buffer)
16197                    (not (buffer-name gnus-dribble-buffer))
16198                    (zerop (save-excursion
16199                             (set-buffer gnus-dribble-buffer)
16200                             (buffer-size)))))
16201           (gnus-message 4 "(No changes need to be saved)")
16202         (run-hooks 'gnus-save-newsrc-hook)
16203         (if gnus-slave
16204             (gnus-slave-save-newsrc)
16205           ;; Save .newsrc.
16206           (when gnus-save-newsrc-file
16207             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16208             (gnus-gnus-to-newsrc-format)
16209             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16210           ;; Save .newsrc.eld.
16211           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16212           (make-local-variable 'version-control)
16213           (setq version-control 'never)
16214           (setq buffer-file-name
16215                 (concat gnus-current-startup-file ".eld"))
16216           (gnus-add-current-to-buffer-list)
16217           (buffer-disable-undo (current-buffer))
16218           (erase-buffer)
16219           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16220           (gnus-gnus-to-quick-newsrc-format)
16221           (run-hooks 'gnus-save-quick-newsrc-hook)
16222           (save-buffer)
16223           (kill-buffer (current-buffer))
16224           (gnus-message
16225            5 "Saving %s.eld...done" gnus-current-startup-file))
16226         (gnus-dribble-delete-file)))))
16227
16228 (defun gnus-gnus-to-quick-newsrc-format ()
16229   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16230   (insert ";; Gnus startup file.\n")
16231   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16232   (insert ";; to read .newsrc.\n")
16233   (insert "(setq gnus-newsrc-file-version "
16234           (prin1-to-string gnus-version) ")\n")
16235   (let ((variables
16236          (if gnus-save-killed-list gnus-variable-list
16237            ;; Remove the `gnus-killed-list' from the list of variables
16238            ;; to be saved, if required.
16239            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16240         ;; Peel off the "dummy" group.
16241         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16242         variable)
16243     ;; Insert the variables into the file.
16244     (while variables
16245       (when (and (boundp (setq variable (pop variables)))
16246                  (symbol-value variable))
16247         (insert "(setq " (symbol-name variable) " '")
16248         (prin1 (symbol-value variable) (current-buffer))
16249         (insert ")\n")))))
16250
16251 (defun gnus-gnus-to-newsrc-format ()
16252   ;; Generate and save the .newsrc file.
16253   (save-excursion
16254     (set-buffer (create-file-buffer gnus-current-startup-file))
16255     (let ((newsrc (cdr gnus-newsrc-alist))
16256           (standard-output (current-buffer))
16257           info ranges range method)
16258       (setq buffer-file-name gnus-current-startup-file)
16259       (buffer-disable-undo (current-buffer))
16260       (erase-buffer)
16261       ;; Write options.
16262       (if gnus-newsrc-options (insert gnus-newsrc-options))
16263       ;; Write subscribed and unsubscribed.
16264       (while (setq info (pop newsrc))
16265         ;; Don't write foreign groups to .newsrc.
16266         (when (or (null (setq method (gnus-info-method info)))
16267                   (equal method "native")
16268                   (gnus-server-equal method gnus-select-method))
16269           (insert (gnus-info-group info)
16270                   (if (> (gnus-info-level info) gnus-level-subscribed)
16271                       "!" ":"))
16272           (when (setq ranges (gnus-info-read info))
16273             (insert " ")
16274             (if (not (listp (cdr ranges)))
16275                 (if (= (car ranges) (cdr ranges))
16276                     (princ (car ranges))
16277                   (princ (car ranges))
16278                   (insert "-")
16279                   (princ (cdr ranges)))
16280               (while (setq range (pop ranges))
16281                 (if (or (atom range) (= (car range) (cdr range)))
16282                     (princ (or (and (atom range) range) (car range)))
16283                   (princ (car range))
16284                   (insert "-")
16285                   (princ (cdr range)))
16286                 (if ranges (insert ",")))))
16287           (insert "\n")))
16288       (make-local-variable 'version-control)
16289       (setq version-control 'never)
16290       ;; It has been reported that sometime the modtime on the .newsrc
16291       ;; file seems to be off.  We really do want to overwrite it, so
16292       ;; we clear the modtime here before saving.  It's a bit odd,
16293       ;; though...
16294       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16295       ;; delete the silly thing entirely first.  but this fails to provide
16296       ;; such niceties as .newsrc~ creation.
16297       (if gnus-modtime-botch
16298           (delete-file gnus-startup-file)
16299         (clear-visited-file-modtime))
16300       (run-hooks 'gnus-save-standard-newsrc-hook)
16301       (save-buffer)
16302       (kill-buffer (current-buffer)))))
16303
16304 \f
16305 ;;;
16306 ;;; Slave functions.
16307 ;;;
16308
16309 (defun gnus-slave-save-newsrc ()
16310   (save-excursion
16311     (set-buffer gnus-dribble-buffer)
16312     (let ((slave-name
16313            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16314       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16315
16316 (defun gnus-master-read-slave-newsrc ()
16317   (let ((slave-files
16318          (directory-files
16319           (file-name-directory gnus-current-startup-file)
16320           t (concat
16321              "^" (regexp-quote
16322                   (concat
16323                    (file-name-nondirectory gnus-current-startup-file)
16324                    "-slave-")))
16325           t))
16326         file)
16327     (if (not slave-files)
16328         ()                              ; There are no slave files to read.
16329       (gnus-message 7 "Reading slave newsrcs...")
16330       (save-excursion
16331         (set-buffer (get-buffer-create " *gnus slave*"))
16332         (buffer-disable-undo (current-buffer))
16333         (setq slave-files
16334               (sort (mapcar (lambda (file)
16335                               (list (nth 5 (file-attributes file)) file))
16336                             slave-files)
16337                     (lambda (f1 f2)
16338                       (or (< (caar f1) (caar f2))
16339                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16340         (while slave-files
16341           (erase-buffer)
16342           (setq file (nth 1 (car slave-files)))
16343           (insert-file-contents file)
16344           (if (condition-case ()
16345                   (progn
16346                     (eval-buffer (current-buffer))
16347                     t)
16348                 (error
16349                  (gnus-message 3 "Possible error in %s" file)
16350                  (ding)
16351                  (sit-for 2)
16352                  nil))
16353               (or gnus-slave ; Slaves shouldn't delete these files.
16354                   (condition-case ()
16355                       (delete-file file)
16356                     (error nil))))
16357           (setq slave-files (cdr slave-files))))
16358       (gnus-message 7 "Reading slave newsrcs...done"))))
16359
16360 \f
16361 ;;;
16362 ;;; Group description.
16363 ;;;
16364
16365 (defun gnus-read-all-descriptions-files ()
16366   (let ((methods (cons gnus-select-method 
16367                        (nconc
16368                         (when gnus-message-archive-method
16369                           (list "archive"))
16370                         gnus-secondary-select-methods))))
16371     (while methods
16372       (gnus-read-descriptions-file (car methods))
16373       (setq methods (cdr methods)))
16374     t))
16375
16376 (defun gnus-read-descriptions-file (&optional method)
16377   (let ((method (or method gnus-select-method)))
16378     (when (stringp method)
16379       (setq method (gnus-server-to-method method)))
16380     ;; We create the hashtable whether we manage to read the desc file
16381     ;; to avoid trying to re-read after a failed read.
16382     (or gnus-description-hashtb
16383         (setq gnus-description-hashtb
16384               (gnus-make-hashtable (length gnus-active-hashtb))))
16385     ;; Mark this method's desc file as read.
16386     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16387                   gnus-description-hashtb)
16388
16389     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16390     (cond
16391      ((not (gnus-check-server method))
16392       (gnus-message 1 "Couldn't open server")
16393       nil)
16394      ((not (gnus-request-list-newsgroups method))
16395       (gnus-message 1 "Couldn't read newsgroups descriptions")
16396       nil)
16397      (t
16398       (let (group)
16399         (save-excursion
16400           (save-restriction
16401             (set-buffer nntp-server-buffer)
16402             (goto-char (point-min))
16403             (if (or (search-forward "\n.\n" nil t)
16404                     (goto-char (point-max)))
16405                 (progn
16406                   (beginning-of-line)
16407                   (narrow-to-region (point-min) (point))))
16408             (goto-char (point-min))
16409             (while (not (eobp))
16410               ;; If we get an error, we set group to 0, which is not a
16411               ;; symbol...
16412               (setq group
16413                     (condition-case ()
16414                         (let ((obarray gnus-description-hashtb))
16415                           ;; Group is set to a symbol interned in this
16416                           ;; hash table.
16417                           (read nntp-server-buffer))
16418                       (error 0)))
16419               (skip-chars-forward " \t")
16420               ;; ...  which leads to this line being effectively ignored.
16421               (and (symbolp group)
16422                    (set group (buffer-substring
16423                                (point) (progn (end-of-line) (point)))))
16424               (forward-line 1))))
16425         (gnus-message 5 "Reading descriptions file...done")
16426         t)))))
16427
16428 (defun gnus-group-get-description (group)
16429   "Get the description of a group by sending XGTITLE to the server."
16430   (when (gnus-request-group-description group)
16431     (save-excursion
16432       (set-buffer nntp-server-buffer)
16433       (goto-char (point-min))
16434       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16435         (match-string 1)))))
16436
16437 \f
16438 ;;;
16439 ;;; Shutdown
16440 ;;;
16441
16442 (defvar gnus-shutdown-alist nil)
16443
16444 (defun gnus-add-shutdown (function &rest symbols)
16445   "Run FUNCTION whenever one of SYMBOLS is shut down."
16446   (push (cons function symbols) gnus-shutdown-alist))
16447
16448 (defun gnus-shutdown (symbol)
16449   "Shut down everything that waits for SYMBOL."
16450   (let ((alist gnus-shutdown-alist)
16451         entry)
16452     (while (setq entry (pop alist))
16453       (when (memq symbol (cdr entry))
16454         (funcall (car entry))))))
16455
16456 \f
16457 ;;;
16458 ;;; Buffering of read articles.
16459 ;;;
16460
16461 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16462 (defvar gnus-backlog-articles nil)
16463 (defvar gnus-backlog-hashtb nil)
16464
16465 (defun gnus-backlog-buffer ()
16466   "Return the backlog buffer."
16467   (or (get-buffer gnus-backlog-buffer)
16468       (save-excursion
16469         (set-buffer (get-buffer-create gnus-backlog-buffer))
16470         (buffer-disable-undo (current-buffer))
16471         (setq buffer-read-only t)
16472         (gnus-add-current-to-buffer-list)
16473         (get-buffer gnus-backlog-buffer))))
16474
16475 (defun gnus-backlog-setup ()
16476   "Initialize backlog variables."
16477   (unless gnus-backlog-hashtb
16478     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16479
16480 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16481
16482 (defun gnus-backlog-shutdown ()
16483   "Clear all backlog variables and buffers."
16484   (when (get-buffer gnus-backlog-buffer)
16485     (kill-buffer gnus-backlog-buffer))
16486   (setq gnus-backlog-hashtb nil
16487         gnus-backlog-articles nil))
16488
16489 (defun gnus-backlog-enter-article (group number buffer)
16490   (gnus-backlog-setup)
16491   (let ((ident (intern (concat group ":" (int-to-string number))
16492                        gnus-backlog-hashtb))
16493         b)
16494     (if (memq ident gnus-backlog-articles)
16495         () ; It's already kept.
16496       ;; Remove the oldest article, if necessary.
16497       (and (numberp gnus-keep-backlog)
16498            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16499            (gnus-backlog-remove-oldest-article))
16500       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16501       ;; Insert the new article.
16502       (save-excursion
16503         (set-buffer (gnus-backlog-buffer))
16504         (let (buffer-read-only)
16505           (goto-char (point-max))
16506           (or (bolp) (insert "\n"))
16507           (setq b (point))
16508           (insert-buffer-substring buffer)
16509           ;; Tag the beginning of the article with the ident.
16510           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16511
16512 (defun gnus-backlog-remove-oldest-article ()
16513   (save-excursion
16514     (set-buffer (gnus-backlog-buffer))
16515     (goto-char (point-min))
16516     (if (zerop (buffer-size))
16517         () ; The buffer is empty.
16518       (let ((ident (get-text-property (point) 'gnus-backlog))
16519             buffer-read-only)
16520         ;; Remove the ident from the list of articles.
16521         (when ident
16522           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16523         ;; Delete the article itself.
16524         (delete-region
16525          (point) (next-single-property-change
16526                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16527
16528 (defun gnus-backlog-request-article (group number buffer)
16529   (when (numberp number)
16530     (gnus-backlog-setup)
16531     (let ((ident (intern (concat group ":" (int-to-string number))
16532                          gnus-backlog-hashtb))
16533           beg end)
16534       (when (memq ident gnus-backlog-articles)
16535         ;; It was in the backlog.
16536         (save-excursion
16537           (set-buffer (gnus-backlog-buffer))
16538           (if (not (setq beg (text-property-any
16539                               (point-min) (point-max) 'gnus-backlog
16540                               ident)))
16541               ;; It wasn't in the backlog after all.
16542               (ignore
16543                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16544             ;; Find the end (i. e., the beginning of the next article).
16545             (setq end
16546                   (next-single-property-change
16547                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16548         (let ((buffer-read-only nil))
16549           (erase-buffer)
16550           (insert-buffer-substring gnus-backlog-buffer beg end)
16551           t)))))
16552
16553 ;; Allow redefinition of Gnus functions.
16554
16555 (gnus-ems-redefine)
16556
16557 (provide 'gnus)
16558
16559 ;;; gnus.el ends here