*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval '(run-hooks 'gnus-load-hook))
29
30 (require 'mail-utils)
31 (require 'timezone)
32 (require 'nnheader)
33
34 (eval-when-compile (require 'cl))
35
36 ;; Site dependent variables.  These variables should be defined in
37 ;; paths.el.
38
39 (defvar gnus-default-nntp-server nil
40   "Specify a default NNTP server.
41 This variable should be defined in paths.el, and should never be set
42 by the user.
43 If you want to change servers, you should use `gnus-select-method'.
44 See the documentation to that variable.")
45
46 (defvar gnus-backup-default-subscribed-newsgroups
47   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
48   "Default default new newsgroups the first time Gnus is run.
49 Should be set in paths.el, and shouldn't be touched by the user.")
50
51 (defvar gnus-local-domain nil
52   "Local domain name without a host name.
53 The DOMAINNAME environment variable is used instead if it is defined.
54 If the `system-name' function returns the full Internet name, there is
55 no need to set this variable.")
56
57 (defvar gnus-local-organization nil
58   "String with a description of what organization (if any) the user belongs to.
59 The ORGANIZATION environment variable is used instead if it is defined.
60 If this variable contains a function, this function will be called
61 with the current newsgroup name as the argument.  The function should
62 return a string.
63
64 In any case, if the string (either in the variable, in the environment
65 variable, or returned by the function) is a file name, the contents of
66 this file will be used as the organization.")
67
68 (defvar gnus-use-generic-from nil
69   "If nil, the full host name will be the system name prepended to the domain name.
70 If this is a string, the full host name will be this string.
71 If this is non-nil, non-string, the domain name will be used as the
72 full host name.")
73
74 (defvar gnus-use-generic-path nil
75   "If nil, use the NNTP server name in the Path header.
76 If stringp, use this; if non-nil, use no host name (user name only).")
77
78
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@ftp.uu.net:/usenet/news.answers/"
190     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
191     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
192     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
193     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
194 ;    "/ftp@ftp.Germany.EU.net:/pub/newsarchive/news.answers/"
195     "/ftp@ftp.sunet.se:/pub/usenet/"
196     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
197     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
198     "/ftp@ftp.hk.super.net:/mirror/faqs/")
199   "*Directory where the group FAQs are stored.
200 This will most commonly be on a remote machine, and the file will be
201 fetched by ange-ftp.
202
203 This variable can also be a list of directories.  In that case, the
204 first element in the list will be used by default, and the others will
205 be used as backup sites.
206
207 Note that Gnus uses an aol machine as the default directory.  If this
208 feels fundamentally unclean, just think of it as a way to finally get
209 something of value back from them.
210
211 If the default site is too slow, try one of these:
212
213    North America: mirrors.aol.com                /pub/rtfm/usenet
214                   ftp.seas.gwu.edu               /pub/rtfm
215                   rtfm.mit.edu                   /pub/usenet/news.answers
216    Europe:        ftp.uni-paderborn.de           /pub/FAQ
217                   src.doc.ic.ac.uk               /usenet/news-FAQS
218                   ftp.sunet.se                   /pub/usenet
219    Asia:          nctuccca.edu.tw                /USENET/FAQ
220                   hwarang.postech.ac.kr          /pub/usenet/news.answers
221                   ftp.hk.super.net               /mirror/faqs")
222
223 (defvar gnus-group-archive-directory
224   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
225   "*The address of the (ding) archives.")
226
227 (defvar gnus-group-recent-archive-directory
228   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
229   "*The address of the most recent (ding) articles.")
230
231 (defvar gnus-default-subscribed-newsgroups nil
232   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
233 It should be a list of strings.
234 If it is `t', Gnus will not do anything special the first time it is
235 started; it'll just use the normal newsgroups subscription methods.")
236
237 (defvar gnus-use-cross-reference t
238   "*Non-nil means that cross referenced articles will be marked as read.
239 If nil, ignore cross references.  If t, mark articles as read in
240 subscribed newsgroups.  If neither t nor nil, mark as read in all
241 newsgroups.")
242
243 (defvar gnus-single-article-buffer t
244   "*If non-nil, display all articles in the same buffer.
245 If nil, each group will get its own article buffer.")
246
247 (defvar gnus-use-dribble-file t
248   "*Non-nil means that Gnus will use a dribble file to store user updates.
249 If Emacs should crash without saving the .newsrc files, complete
250 information can be restored from the dribble file.")
251
252 (defvar gnus-dribble-directory nil
253   "*The directory where dribble files will be saved.
254 If this variable is nil, the directory where the .newsrc files are
255 saved will be used.")
256
257 (defvar gnus-asynchronous nil
258   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
259
260 (defvar gnus-kill-summary-on-exit t
261   "*If non-nil, kill the summary buffer when you exit from it.
262 If nil, the summary will become a \"*Dead Summary*\" buffer, and
263 it will be killed sometime later.")
264
265 (defvar gnus-large-newsgroup 200
266   "*The number of articles which indicates a large newsgroup.
267 If the number of articles in a newsgroup is greater than this value,
268 confirmation is required for selecting the newsgroup.")
269
270 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
271 (defvar gnus-no-groups-message "No news is horrible news"
272   "*Message displayed by Gnus when no groups are available.")
273
274 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
275   "*Non-nil means that the default name of a file to save articles in is the group name.
276 If it's nil, the directory form of the group name is used instead.
277
278 If this variable is a list, and the list contains the element
279 `not-score', long file names will not be used for score files; if it
280 contains the element `not-save', long file names will not be used for
281 saving; and if it contains the element `not-kill', long file names
282 will not be used for kill files.")
283
284 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
285   "*Name of the directory articles will be saved in (default \"~/News\").
286 Initialized from the SAVEDIR environment variable.")
287
288 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
289   "*Name of the directory where kill files will be stored (default \"~/News\").
290 Initialized from the SAVEDIR environment variable.")
291
292 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
293   "*A function to save articles in your favorite format.
294 The function must be interactively callable (in other words, it must
295 be an Emacs command).
296
297 Gnus provides the following functions:
298
299 * gnus-summary-save-in-rmail (Rmail format)
300 * gnus-summary-save-in-mail (Unix mail format)
301 * gnus-summary-save-in-folder (MH folder)
302 * gnus-summary-save-in-file (article format).
303 * gnus-summary-save-in-vm (use VM's folder format).")
304
305 (defvar gnus-prompt-before-saving 'always
306   "*This variable says how much prompting is to be done when saving articles.
307 If it is nil, no prompting will be done, and the articles will be
308 saved to the default files.  If this variable is `always', each and
309 every article that is saved will be preceded by a prompt, even when
310 saving large batches of articles.  If this variable is neither nil not
311 `always', there the user will be prompted once for a file name for
312 each invocation of the saving commands.")
313
314 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
315   "*A function generating a file name to save articles in Rmail format.
316 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
317
318 (defvar gnus-mail-save-name (function gnus-plain-save-name)
319   "*A function generating a file name to save articles in Unix mail format.
320 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
321
322 (defvar gnus-folder-save-name (function gnus-folder-save-name)
323   "*A function generating a file name to save articles in MH folder.
324 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
325
326 (defvar gnus-file-save-name (function gnus-numeric-save-name)
327   "*A function generating a file name to save articles in article format.
328 The function is called with NEWSGROUP, HEADERS, and optional
329 LAST-FILE.")
330
331 (defvar gnus-split-methods
332   '((gnus-article-archive-name))
333   "*Variable used to suggest where articles are to be saved.
334 For instance, if you would like to save articles related to Gnus in
335 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
336 you could set this variable to something like:
337
338  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
339    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
340
341 This variable is an alist where the where the key is the match and the
342 value is a list of possible files to save in if the match is non-nil.
343
344 If the match is a string, it is used as a regexp match on the
345 article.  If the match is a symbol, that symbol will be funcalled
346 from the buffer of the article to be saved with the newsgroup as the
347 parameter.  If it is a list, it will be evaled in the same buffer.
348
349 If this form or function returns a string, this string will be used as
350 a possible file name; and if it returns a non-nil list, that list will
351 be used as possible file names.")
352
353 (defvar gnus-move-split-methods nil
354   "*Variable used to suggest where articles are to be moved to.
355 It uses the same syntax as the `gnus-split-methods' variable.")
356
357 (defvar gnus-save-score nil
358   "*If non-nil, save group scoring info.")
359
360 (defvar gnus-use-adaptive-scoring nil
361   "*If non-nil, use some adaptive scoring scheme.")
362
363 (defvar gnus-use-cache nil
364   "*If nil, Gnus will ignore the article cache.
365 If `passive', it will allow entering (and reading) articles
366 explicitly entered into the cache.  If anything else, use the
367 cache to the full extent of the law.")
368
369 (defvar gnus-use-trees nil
370   "*If non-nil, display a thread tree buffer.")
371
372 (defvar gnus-keep-backlog nil
373   "*If non-nil, Gnus will keep read articles for later re-retrieval.
374 If it is a number N, then Gnus will only keep the last N articles
375 read.  If it is neither nil nor a number, Gnus will keep all read
376 articles.  This is not a good idea.")
377
378 (defvar gnus-use-nocem nil
379   "*If non-nil, Gnus will read NoCeM cancel messages.")
380
381 (defvar gnus-use-demon nil
382   "If non-nil, Gnus might use some demons.")
383
384 (defvar gnus-use-scoring t
385   "*If non-nil, enable scoring.")
386
387 (defvar gnus-use-picons nil
388   "*If non-nil, display picons.")
389
390 (defvar gnus-fetch-old-headers nil
391   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
392 If an unread article in the group refers to an older, already read (or
393 just marked as read) article, the old article will not normally be
394 displayed in the Summary buffer.  If this variable is non-nil, Gnus
395 will attempt to grab the headers to the old articles, and thereby
396 build complete threads.  If it has the value `some', only enough
397 headers to connect otherwise loose threads will be displayed.
398 This variable can also be a number.  In that case, no more than that
399 number of old headers will be fetched.
400
401 The server has to support NOV for any of this to work.")
402
403 ;see gnus-cus.el
404 ;(defvar gnus-visual t
405 ;  "*If non-nil, will do various highlighting.
406 ;If nil, no mouse highlights (or any other highlights) will be
407 ;performed.  This might speed up Gnus some when generating large group
408 ;and summary buffers.")
409
410 (defvar gnus-novice-user t
411   "*Non-nil means that you are a usenet novice.
412 If non-nil, verbose messages may be displayed and confirmations may be
413 required.")
414
415 (defvar gnus-expert-user nil
416   "*Non-nil means that you will never be asked for confirmation about anything.
417 And that means *anything*.")
418
419 (defvar gnus-verbose 7
420   "*Integer that says how verbose Gnus should be.
421 The higher the number, the more messages Gnus will flash to say what
422 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
423 display most important messages; and at ten, Gnus will keep on
424 jabbering all the time.")
425
426 (defvar gnus-keep-same-level nil
427   "*Non-nil means that the next newsgroup after the current will be on the same level.
428 When you type, for instance, `n' after reading the last article in the
429 current newsgroup, you will go to the next newsgroup.  If this variable
430 is nil, the next newsgroup will be the next from the group
431 buffer.
432 If this variable is non-nil, Gnus will either put you in the
433 next newsgroup with the same level, or, if no such newsgroup is
434 available, the next newsgroup with the lowest possible level higher
435 than the current level.
436 If this variable is `best', Gnus will make the next newsgroup the one
437 with the best level.")
438
439 (defvar gnus-summary-make-false-root 'adopt
440   "*nil means that Gnus won't gather loose threads.
441 If the root of a thread has expired or been read in a previous
442 session, the information necessary to build a complete thread has been
443 lost.  Instead of having many small sub-threads from this original thread
444 scattered all over the summary buffer, Gnus can gather them.
445
446 If non-nil, Gnus will try to gather all loose sub-threads from an
447 original thread into one large thread.
448
449 If this variable is non-nil, it should be one of `none', `adopt',
450 `dummy' or `empty'.
451
452 If this variable is `none', Gnus will not make a false root, but just
453 present the sub-threads after another.
454 If this variable is `dummy', Gnus will create a dummy root that will
455 have all the sub-threads as children.
456 If this variable is `adopt', Gnus will make one of the \"children\"
457 the parent and mark all the step-children as such.
458 If this variable is `empty', the \"children\" are printed with empty
459 subject fields.  (Or rather, they will be printed with a string
460 given by the `gnus-summary-same-subject' variable.)")
461
462 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
463   "*A regexp to match subjects to be excluded from loose thread gathering.
464 As loose thread gathering is done on subjects only, that means that
465 there can be many false gatherings performed.  By rooting out certain
466 common subjects, gathering might become saner.")
467
468 (defvar gnus-summary-gather-subject-limit nil
469   "*Maximum length of subject comparisons when gathering loose threads.
470 Use nil to compare full subjects.  Setting this variable to a low
471 number will help gather threads that have been corrupted by
472 newsreaders chopping off subject lines, but it might also mean that
473 unrelated articles that have subject that happen to begin with the
474 same few characters will be incorrectly gathered.
475
476 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
477 comparing subjects.")
478
479 (defvar gnus-simplify-ignored-prefixes nil
480   "*Regexp, matches for which are removed from subject lines when simplifying.")
481
482 (defvar gnus-build-sparse-threads nil
483   "*If non-nil, fill in the gaps in threads.
484 If `some', only fill in the gaps that are needed to tie loose threads
485 together.  If `more', fill in all leaf nodes that Gnus can find.  If
486 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
487
488 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
489   "Function used for gathering loose threads.
490 There are two pre-defined functions: `gnus-gather-threads-by-subject',
491 which only takes Subjects into consideration; and
492 `gnus-gather-threads-by-references', which compared the References
493 headers of the articles to find matches.")
494
495 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
496 (defvar gnus-summary-same-subject ""
497   "*String indicating that the current article has the same subject as the previous.
498 This variable will only be used if the value of
499 `gnus-summary-make-false-root' is `empty'.")
500
501 (defvar gnus-summary-goto-unread t
502   "*If non-nil, marking commands will go to the next unread article.
503 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
504 whether it is read or not.")
505
506 (defvar gnus-group-goto-unread t
507   "*If non-nil, movement commands will go to the next unread and subscribed group.")
508
509 (defvar gnus-goto-next-group-when-activating t
510   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
511
512 (defvar gnus-check-new-newsgroups t
513   "*Non-nil means that Gnus will add new newsgroups at startup.
514 If this variable is `ask-server', Gnus will ask the server for new
515 groups since the last time it checked.  This means that the killed list
516 is no longer necessary, so you could set `gnus-save-killed-list' to
517 nil.
518
519 A variant is to have this variable be a list of select methods.  Gnus
520 will then use the `ask-server' method on all these select methods to
521 query for new groups from all those servers.
522
523 Eg.
524   (setq gnus-check-new-newsgroups
525         '((nntp \"some.server\") (nntp \"other.server\")))
526
527 If this variable is nil, then you have to tell Gnus explicitly to
528 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
529
530 (defvar gnus-check-bogus-newsgroups nil
531   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
532 If this variable is nil, then you have to tell Gnus explicitly to
533 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
534
535 (defvar gnus-read-active-file t
536   "*Non-nil means that Gnus will read the entire active file at startup.
537 If this variable is nil, Gnus will only know about the groups in your
538 `.newsrc' file.
539
540 If this variable is `some', Gnus will try to only read the relevant
541 parts of the active file from the server.  Not all servers support
542 this, and it might be quite slow with other servers, but this should
543 generally be faster than both the t and nil value.
544
545 If you set this variable to nil or `some', you probably still want to
546 be told about new newsgroups that arrive.  To do that, set
547 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
548 properly with all servers.")
549
550 (defvar gnus-level-subscribed 5
551   "*Groups with levels less than or equal to this variable are subscribed.")
552
553 (defvar gnus-level-unsubscribed 7
554   "*Groups with levels less than or equal to this variable are unsubscribed.
555 Groups with levels less than `gnus-level-subscribed', which should be
556 less than this variable, are subscribed.")
557
558 (defvar gnus-level-zombie 8
559   "*Groups with this level are zombie groups.")
560
561 (defvar gnus-level-killed 9
562   "*Groups with this level are killed.")
563
564 (defvar gnus-level-default-subscribed 3
565   "*New subscribed groups will be subscribed at this level.")
566
567 (defvar gnus-level-default-unsubscribed 6
568   "*New unsubscribed groups will be unsubscribed at this level.")
569
570 (defvar gnus-activate-level (1+ gnus-level-subscribed)
571   "*Groups higher than this level won't be activated on startup.
572 Setting this variable to something log might save lots of time when
573 you have many groups that you aren't interested in.")
574
575 (defvar gnus-activate-foreign-newsgroups 4
576   "*If nil, Gnus will not check foreign newsgroups at startup.
577 If it is non-nil, it should be a number between one and nine.  Foreign
578 newsgroups that have a level lower or equal to this number will be
579 activated on startup.  For instance, if you want to active all
580 subscribed newsgroups, but not the rest, you'd set this variable to
581 `gnus-level-subscribed'.
582
583 If you subscribe to lots of newsgroups from different servers, startup
584 might take a while.  By setting this variable to nil, you'll save time,
585 but you won't be told how many unread articles there are in the
586 groups.")
587
588 (defvar gnus-save-newsrc-file t
589   "*Non-nil means that Gnus will save the `.newsrc' file.
590 Gnus always saves its own startup file, which is called
591 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
592 be readily understood by other newsreaders.  If you don't plan on
593 using other newsreaders, set this variable to nil to save some time on
594 exit.")
595
596 (defvar gnus-save-killed-list t
597   "*If non-nil, save the list of killed groups to the startup file.
598 If you set this variable to nil, you'll save both time (when starting
599 and quitting) and space (both memory and disk), but it will also mean
600 that Gnus has no record of which groups are new and which are old, so
601 the automatic new newsgroups subscription methods become meaningless.
602
603 You should always set `gnus-check-new-newsgroups' to `ask-server' or
604 nil if you set this variable to nil.")
605
606 (defvar gnus-interactive-catchup t
607   "*If non-nil, require your confirmation when catching up a group.")
608
609 (defvar gnus-interactive-post t
610   "*If non-nil, group name will be asked for when posting.")
611
612 (defvar gnus-interactive-exit t
613   "*If non-nil, require your confirmation when exiting Gnus.")
614
615 (defvar gnus-kill-killed t
616   "*If non-nil, Gnus will apply kill files to already killed articles.
617 If it is nil, Gnus will never apply kill files to articles that have
618 already been through the scoring process, which might very well save lots
619 of time.")
620
621 (defvar gnus-extract-address-components 'gnus-extract-address-components
622   "*Function for extracting address components from a From header.
623 Two pre-defined function exist: `gnus-extract-address-components',
624 which is the default, quite fast, and too simplistic solution, and
625 `mail-extract-address-components', which works much better, but is
626 slower.")
627
628 (defvar gnus-summary-default-score 0
629   "*Default article score level.
630 If this variable is nil, scoring will be disabled.")
631
632 (defvar gnus-summary-zcore-fuzz 0
633   "*Fuzziness factor for the zcore in the summary buffer.
634 Articles with scores closer than this to `gnus-summary-default-score'
635 will not be marked.")
636
637 (defvar gnus-simplify-subject-fuzzy-regexp nil
638   "*Strings to be removed when doing fuzzy matches.
639 This can either be a regular expression or list of regular expressions
640 that will be removed from subject strings if fuzzy subject
641 simplification is selected.")
642
643 (defvar gnus-permanently-visible-groups nil
644   "*Regexp to match groups that should always be listed in the group buffer.
645 This means that they will still be listed when there are no unread
646 articles in the groups.")
647
648 (defvar gnus-list-groups-with-ticked-articles t
649   "*If non-nil, list groups that have only ticked articles.
650 If nil, only list groups that have unread articles.")
651
652 (defvar gnus-group-default-list-level gnus-level-subscribed
653   "*Default listing level.
654 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
655
656 (defvar gnus-group-use-permanent-levels nil
657   "*If non-nil, once you set a level, Gnus will use this level.")
658
659 (defvar gnus-group-list-inactive-groups t
660   "*If non-nil, inactive groups will be listed.")
661
662 (defvar gnus-show-mime nil
663   "*If non-nil, do mime processing of articles.
664 The articles will simply be fed to the function given by
665 `gnus-show-mime-method'.")
666
667 (defvar gnus-strict-mime t
668   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
669
670 (defvar gnus-show-mime-method 'metamail-buffer
671   "*Function to process a MIME message.
672 The function is called from the article buffer.")
673
674 (defvar gnus-decode-encoded-word-method (lambda ())
675   "*Function to decode a MIME encoded-words.
676 The function is called from the article buffer.")
677
678 (defvar gnus-show-threads t
679   "*If non-nil, display threads in summary mode.")
680
681 (defvar gnus-thread-hide-subtree nil
682   "*If non-nil, hide all threads initially.
683 If threads are hidden, you have to run the command
684 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
685 to expose hidden threads.")
686
687 (defvar gnus-thread-hide-killed t
688   "*If non-nil, hide killed threads automatically.")
689
690 (defvar gnus-thread-ignore-subject nil
691   "*If non-nil, ignore subjects and do all threading based on the Reference header.
692 If nil, which is the default, articles that have different subjects
693 from their parents will start separate threads.")
694
695 (defvar gnus-thread-operation-ignore-subject t
696   "*If non-nil, subjects will be ignored when doing thread commands.
697 This affects commands like `gnus-summary-kill-thread' and
698 `gnus-summary-lower-thread'.
699
700 If this variable is nil, articles in the same thread with different
701 subjects will not be included in the operation in question.  If this
702 variable is `fuzzy', only articles that have subjects that are fuzzily
703 equal will be included.")
704
705 (defvar gnus-thread-indent-level 4
706   "*Number that says how much each sub-thread should be indented.")
707
708 (defvar gnus-ignored-newsgroups
709   (purecopy (mapconcat 'identity
710                        '("^to\\."       ; not "real" groups
711                          "^[0-9. \t]+ " ; all digits in name
712                          "[][\"#'()]"   ; bogus characters
713                          )
714                        "\\|"))
715   "*A regexp to match uninteresting newsgroups in the active file.
716 Any lines in the active file matching this regular expression are
717 removed from the newsgroup list before anything else is done to it,
718 thus making them effectively non-existent.")
719
720 (defvar gnus-ignored-headers
721   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
722   "*All headers that match this regexp will be hidden.
723 This variable can also be a list of regexps of headers to be ignored.
724 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
725
726 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
727   "*All headers that do not match this regexp will be hidden.
728 This variable can also be a list of regexp of headers to remain visible.
729 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
730
731 (defvar gnus-sorted-header-list
732   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
733     "^Cc:" "^Date:" "^Organization:")
734   "*This variable is a list of regular expressions.
735 If it is non-nil, headers that match the regular expressions will
736 be placed first in the article buffer in the sequence specified by
737 this list.")
738
739 (defvar gnus-boring-article-headers
740   '(empty followup-to reply-to)
741   "*Headers that are only to be displayed if they have interesting data.
742 Possible values in this list are `empty', `newsgroups', `followup-to',
743 `reply-to', and `date'.")
744
745 (defvar gnus-show-all-headers nil
746   "*If non-nil, don't hide any headers.")
747
748 (defvar gnus-save-all-headers t
749   "*If non-nil, don't remove any headers before saving.")
750
751 (defvar gnus-saved-headers gnus-visible-headers
752   "*Headers to keep if `gnus-save-all-headers' is nil.
753 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
754 If that variable is nil, however, all headers that match this regexp
755 will be kept while the rest will be deleted before saving.")
756
757 (defvar gnus-inhibit-startup-message nil
758   "*If non-nil, the startup message will not be displayed.")
759
760 (defvar gnus-signature-separator "^-- *$"
761   "Regexp matching signature separator.")
762
763 (defvar gnus-auto-extend-newsgroup t
764   "*If non-nil, extend newsgroup forward and backward when requested.")
765
766 (defvar gnus-auto-select-first t
767   "*If nil, don't select the first unread article when entering a group.
768 If this variable is `best', select the highest-scored unread article
769 in the group.  If neither nil nor `best', select the first unread
770 article.
771
772 If you want to prevent automatic selection of the first unread article
773 in some newsgroups, set the variable to nil in
774 `gnus-select-group-hook'.")
775
776 (defvar gnus-auto-select-next t
777   "*If non-nil, offer to go to the next group from the end of the previous.
778 If the value is t and the next newsgroup is empty, Gnus will exit
779 summary mode and go back to group mode.  If the value is neither nil
780 nor t, Gnus will select the following unread newsgroup.  In
781 particular, if the value is the symbol `quietly', the next unread
782 newsgroup will be selected without any confirmation, and if it is
783 `almost-quietly', the next group will be selected without any
784 confirmation if you are located on the last article in the group.
785 Finally, if this variable is `slightly-quietly', the `Z n' command
786 will go to the next group without confirmation.")
787
788 (defvar gnus-auto-select-same nil
789   "*If non-nil, select the next article with the same subject.")
790
791 (defvar gnus-summary-check-current nil
792   "*If non-nil, consider the current article when moving.
793 The \"unread\" movement commands will stay on the same line if the
794 current article is unread.")
795
796 (defvar gnus-auto-center-summary t
797   "*If non-nil, always center the current summary buffer.")
798
799 (defvar gnus-break-pages t
800   "*If non-nil, do page breaking on articles.
801 The page delimiter is specified by the `gnus-page-delimiter'
802 variable.")
803
804 (defvar gnus-page-delimiter "^\^L"
805   "*Regexp describing what to use as article page delimiters.
806 The default value is \"^\^L\", which is a form linefeed at the
807 beginning of a line.")
808
809 (defvar gnus-use-full-window t
810   "*If non-nil, use the entire Emacs screen.")
811
812 (defvar gnus-window-configuration nil
813   "Obsolete variable.  See `gnus-buffer-configuration'.")
814
815 (defvar gnus-window-min-width 2
816   "*Minimum width of Gnus buffers.")
817
818 (defvar gnus-window-min-height 1
819   "*Minimum height of Gnus buffers.")
820
821 (defvar gnus-buffer-configuration
822   '((group
823      (vertical 1.0
824                (group 1.0 point)
825                (if gnus-carpal '(group-carpal 4))))
826     (summary
827      (vertical 1.0
828                (summary 1.0 point)
829                (if gnus-carpal '(summary-carpal 4))))
830     (article
831      (cond 
832       (gnus-use-picons
833        '(frame 1.0
834                (vertical 1.0
835                          (summary 0.25 point)
836                          (if gnus-carpal '(summary-carpal 4))
837                          (article 1.0))
838                (vertical ((height . 5) (width . 15)
839                           (user-position . t)
840                           (left . -1) (top . 1))
841                          (picons 1.0))))
842       (gnus-use-trees
843        '(vertical 1.0
844                   (summary 0.25 point)
845                   (tree 0.25)
846                   (article 1.0)))
847       (t
848        '(vertical 1.0
849                  (summary 0.25 point)
850                  (if gnus-carpal '(summary-carpal 4))
851                  (if gnus-use-trees '(tree 0.25))
852                  (article 1.0)))))
853     (server
854      (vertical 1.0
855                (server 1.0 point)
856                (if gnus-carpal '(server-carpal 2))))
857     (browse
858      (vertical 1.0
859                (browse 1.0 point)
860                (if gnus-carpal '(browse-carpal 2))))
861     (group-mail
862      (vertical 1.0
863                (mail 1.0 point)))
864     (summary-mail
865      (vertical 1.0
866                (mail 1.0 point)))
867     (summary-reply
868      (vertical 1.0
869                (article 0.5)
870                (mail 1.0 point)))
871     (pick
872      (vertical 1.0
873                (article 1.0 point)))
874     (info
875      (vertical 1.0
876                (info 1.0 point)))
877     (summary-faq
878      (vertical 1.0
879                (summary 0.25)
880                (faq 1.0 point)))
881     (edit-group
882      (vertical 1.0
883                (group 0.5)
884                (edit-group 1.0 point)))
885     (edit-server
886      (vertical 1.0
887                (server 0.5)
888                (edit-server 1.0 point)))
889     (edit-score
890      (vertical 1.0
891                (summary 0.25)
892                (edit-score 1.0 point)))
893     (post
894      (vertical 1.0
895                (post 1.0 point)))
896     (reply
897      (vertical 1.0
898                (article 0.5)
899                (mail 1.0 point)))
900     (mail-forward
901      (vertical 1.0
902                (mail 1.0 point)))
903     (post-forward
904      (vertical 1.0
905                (post 1.0 point)))
906     (reply-yank
907      (vertical 1.0
908                (mail 1.0 point)))
909     (mail-bounce
910      (vertical 1.0
911                (article 0.5)
912                (mail 1.0 point)))
913     (draft
914      (vertical 1.0
915                (draft 1.0 point)))
916     (pipe
917      (vertical 1.0
918                (summary 0.25 point)
919                (if gnus-carpal '(summary-carpal 4))
920                ("*Shell Command Output*" 1.0)))
921     (followup
922      (vertical 1.0
923                (article 0.5)
924                (post 1.0 point)))
925     (followup-yank
926      (vertical 1.0
927                (post 1.0 point))))
928   "Window configuration for all possible Gnus buffers.
929 This variable is a list of lists.  Each of these lists has a NAME and
930 a RULE.  The NAMEs are commonsense names like `group', which names a
931 rule used when displaying the group buffer; `summary', which names a
932 rule for what happens when you enter a group and do not display an
933 article buffer; and so on.  See the value of this variable for a
934 complete list of NAMEs.
935
936 Each RULE is a list of vectors.  The first element in this vector is
937 the name of the buffer to be displayed; the second element is the
938 percentage of the screen this buffer is to occupy (a number in the
939 0.0-0.99 range); the optional third element is `point', which should
940 be present to denote which buffer point is to go to after making this
941 buffer configuration.")
942
943 (defvar gnus-window-to-buffer
944   '((group . gnus-group-buffer)
945     (summary . gnus-summary-buffer)
946     (article . gnus-article-buffer)
947     (server . gnus-server-buffer)
948     (browse . "*Gnus Browse Server*")
949     (edit-group . gnus-group-edit-buffer)
950     (edit-server . gnus-server-edit-buffer)
951     (group-carpal . gnus-carpal-group-buffer)
952     (summary-carpal . gnus-carpal-summary-buffer)
953     (server-carpal . gnus-carpal-server-buffer)
954     (browse-carpal . gnus-carpal-browse-buffer)
955     (edit-score . gnus-score-edit-buffer)
956     (mail . gnus-mail-buffer)
957     (post . gnus-post-news-buffer)
958     (faq . gnus-faq-buffer)
959     (picons . "*Picons*")
960     (tree . gnus-tree-buffer)
961     (info . gnus-info-buffer)
962     (draft . gnus-draft-buffer))
963   "Mapping from short symbols to buffer names or buffer variables.")
964
965 (defvar gnus-carpal nil
966   "*If non-nil, display clickable icons.")
967
968 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
969   "*Function called with a group name when new group is detected.
970 A few pre-made functions are supplied: `gnus-subscribe-randomly'
971 inserts new groups at the beginning of the list of groups;
972 `gnus-subscribe-alphabetically' inserts new groups in strict
973 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
974 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
975 for your decision; `gnus-subscribe-killed' kills all new groups.")
976
977 ;; Suggested by a bug report by Hallvard B Furuseth.
978 ;; <h.b.furuseth@usit.uio.no>.
979 (defvar gnus-subscribe-options-newsgroup-method
980   (function gnus-subscribe-alphabetically)
981   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
982 If, for instance, you want to subscribe to all newsgroups in the
983 \"no\" and \"alt\" hierarchies, you'd put the following in your
984 .newsrc file:
985
986 options -n no.all alt.all
987
988 Gnus will the subscribe all new newsgroups in these hierarchies with
989 the subscription method in this variable.")
990
991 (defvar gnus-subscribe-hierarchical-interactive nil
992   "*If non-nil, Gnus will offer to subscribe hierarchically.
993 When a new hierarchy appears, Gnus will ask the user:
994
995 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
996
997 If the user pressed `d', Gnus will descend the hierarchy, `y' will
998 subscribe to all newsgroups in the hierarchy and `s' will skip this
999 hierarchy in its entirety.")
1000
1001 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1002   "*Function used for sorting the group buffer.
1003 This function will be called with group info entries as the arguments
1004 for the groups to be sorted.  Pre-made functions include
1005 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1006 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1007 `gnus-group-sort-by-rank'.
1008
1009 This variable can also be a list of sorting functions.  In that case,
1010 the most significant sort function should be the last function in the
1011 list.")
1012
1013 ;; Mark variables suggested by Thomas Michanek
1014 ;; <Thomas.Michanek@telelogic.se>.
1015 (defvar gnus-unread-mark ? 
1016   "*Mark used for unread articles.")
1017 (defvar gnus-ticked-mark ?!
1018   "*Mark used for ticked articles.")
1019 (defvar gnus-dormant-mark ??
1020   "*Mark used for dormant articles.")
1021 (defvar gnus-del-mark ?r
1022   "*Mark used for del'd articles.")
1023 (defvar gnus-read-mark ?R
1024   "*Mark used for read articles.")
1025 (defvar gnus-expirable-mark ?E
1026   "*Mark used for expirable articles.")
1027 (defvar gnus-killed-mark ?K
1028   "*Mark used for killed articles.")
1029 (defvar gnus-souped-mark ?F
1030   "*Mark used for killed articles.")
1031 (defvar gnus-kill-file-mark ?X
1032   "*Mark used for articles killed by kill files.")
1033 (defvar gnus-low-score-mark ?Y
1034   "*Mark used for articles with a low score.")
1035 (defvar gnus-catchup-mark ?C
1036   "*Mark used for articles that are caught up.")
1037 (defvar gnus-replied-mark ?A
1038   "*Mark used for articles that have been replied to.")
1039 (defvar gnus-cached-mark ?*
1040   "*Mark used for articles that are in the cache.")
1041 (defvar gnus-saved-mark ?S
1042   "*Mark used for articles that have been saved to.")
1043 (defvar gnus-process-mark ?#
1044   "*Process mark.")
1045 (defvar gnus-ancient-mark ?O
1046   "*Mark used for ancient articles.")
1047 (defvar gnus-sparse-mark ?Q
1048   "*Mark used for sparsely reffed articles.")
1049 (defvar gnus-canceled-mark ?G
1050   "*Mark used for canceled articles.")
1051 (defvar gnus-score-over-mark ?+
1052   "*Score mark used for articles with high scores.")
1053 (defvar gnus-score-below-mark ?-
1054   "*Score mark used for articles with low scores.")
1055 (defvar gnus-empty-thread-mark ? 
1056   "*There is no thread under the article.")
1057 (defvar gnus-not-empty-thread-mark ?=
1058   "*There is a thread under the article.")
1059
1060 (defvar gnus-view-pseudo-asynchronously nil
1061   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1062
1063 (defvar gnus-view-pseudos nil
1064   "*If `automatic', pseudo-articles will be viewed automatically.
1065 If `not-confirm', pseudos will be viewed automatically, and the user
1066 will not be asked to confirm the command.")
1067
1068 (defvar gnus-view-pseudos-separately t
1069   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1070 If nil, all files that use the same viewing command will be given as a
1071 list of parameters to that command.")
1072
1073 (defvar gnus-insert-pseudo-articles t
1074   "*If non-nil, insert pseudo-articles when decoding articles.")
1075
1076 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)\n"
1077   "*Format of group lines.
1078 It works along the same lines as a normal formatting string,
1079 with some simple extensions.
1080
1081 %M    Only marked articles (character, \"*\" or \" \")
1082 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1083 %L    Level of subscribedness (integer)
1084 %N    Number of unread articles (integer)
1085 %I    Number of dormant articles (integer)
1086 %i    Number of ticked and dormant (integer)
1087 %T    Number of ticked articles (integer)
1088 %R    Number of read articles (integer)
1089 %t    Total number of articles (integer)
1090 %y    Number of unread, unticked articles (integer)
1091 %G    Group name (string)
1092 %g    Qualified group name (string)
1093 %D    Group description (string)
1094 %s    Select method (string)
1095 %o    Moderated group (char, \"m\")
1096 %p    Process mark (char)
1097 %O    Moderated group (string, \"(m)\" or \"\")
1098 %P    Topic indentation (string)
1099 %n    Select from where (string)
1100 %z    A string that look like `<%s:%n>' if a foreign select method is used
1101 %u    User defined specifier.  The next character in the format string should
1102       be a letter.  Gnus will call the function gnus-user-format-function-X,
1103       where X is the letter following %u.  The function will be passed the
1104       current header as argument.  The function should return a string, which
1105       will be inserted into the buffer just like information from any other
1106       group specifier.
1107
1108 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1109 the mouse point move inside the area.  There can only be one such area.
1110
1111 Note that this format specification is not always respected.  For
1112 reasons of efficiency, when listing killed groups, this specification
1113 is ignored altogether.  If the spec is changed considerably, your
1114 output may end up looking strange when listing both alive and killed
1115 groups.
1116
1117 If you use %o or %O, reading the active file will be slower and quite
1118 a bit of extra memory will be used. %D will also worsen performance.
1119 Also note that if you change the format specification to include any
1120 of these specs, you must probably re-start Gnus to see them go into
1121 effect.")
1122
1123 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1124   "*The format specification of the lines in the summary buffer.
1125
1126 It works along the same lines as a normal formatting string,
1127 with some simple extensions.
1128
1129 %N   Article number, left padded with spaces (string)
1130 %S   Subject (string)
1131 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1132 %n   Name of the poster (string)
1133 %a   Extracted name of the poster (string)
1134 %A   Extracted address of the poster (string)
1135 %F   Contents of the From: header (string)
1136 %x   Contents of the Xref: header (string)
1137 %D   Date of the article (string)
1138 %d   Date of the article (string) in DD-MMM format
1139 %M   Message-id of the article (string)
1140 %r   References of the article (string)
1141 %c   Number of characters in the article (integer)
1142 %L   Number of lines in the article (integer)
1143 %I   Indentation based on thread level (a string of spaces)
1144 %T   A string with two possible values: 80 spaces if the article
1145      is on thread level two or larger and 0 spaces on level one
1146 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1147 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1148 %[   Opening bracket (character, \"[\" or \"<\")
1149 %]   Closing bracket (character, \"]\" or \">\")
1150 %>   Spaces of length thread-level (string)
1151 %<   Spaces of length (- 20 thread-level) (string)
1152 %i   Article score (number)
1153 %z   Article zcore (character)
1154 %t   Number of articles under the current thread (number).
1155 %e   Whether the thread is empty or not (character).
1156 %u   User defined specifier.  The next character in the format string should
1157      be a letter.  Gnus will call the function gnus-user-format-function-X,
1158      where X is the letter following %u.  The function will be passed the
1159      current header as argument.  The function should return a string, which
1160      will be inserted into the summary just like information from any other
1161      summary specifier.
1162
1163 Text between %( and %) will be highlighted with `gnus-mouse-face'
1164 when the mouse point is placed inside the area.  There can only be one
1165 such area.
1166
1167 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1168 with care.  For reasons of efficiency, Gnus will compute what column
1169 these characters will end up in, and \"hard-code\" that.  This means that
1170 it is illegal to have these specs after a variable-length spec.  Well,
1171 you might not be arrested, but your summary buffer will look strange,
1172 which is bad enough.
1173
1174 The smart choice is to have these specs as for to the left as
1175 possible.
1176
1177 This restriction may disappear in later versions of Gnus.")
1178
1179 (defvar gnus-summary-dummy-line-format
1180   "*  %(:                          :%) %S\n"
1181   "*The format specification for the dummy roots in the summary buffer.
1182 It works along the same lines as a normal formatting string,
1183 with some simple extensions.
1184
1185 %S  The subject")
1186
1187 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1188   "*The format specification for the summary mode line.
1189 It works along the same lines as a normal formatting string,
1190 with some simple extensions:
1191
1192 %G  Group name
1193 %p  Unprefixed group name
1194 %A  Current article number
1195 %V  Gnus version
1196 %U  Number of unread articles in the group
1197 %e  Number of unselected articles in the group
1198 %Z  A string with unread/unselected article counts
1199 %g  Shortish group name
1200 %S  Subject of the current article
1201 %u  User-defined spec
1202 %s  Current score file name
1203 %d  Number of dormant articles
1204 %r  Number of articles that have been marked as read in this session
1205 %E  Number of articles expunged by the score files")
1206
1207 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1208   "*The format specification for the article mode line.
1209 See `gnus-summary-mode-line-format' for a closer description.")
1210
1211 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1212   "*The format specification for the group mode line.
1213 It works along the same lines as a normal formatting string,
1214 with some simple extensions:
1215
1216 %S   The native news server.
1217 %M   The native select method.")
1218
1219 (defvar gnus-valid-select-methods
1220   '(("nntp" post address prompt-address)
1221     ("nnspool" post)
1222     ("nnvirtual" post-mail virtual prompt-address)
1223     ("nnmbox" mail respool)
1224     ("nnml" mail respool)
1225     ("nnmh" mail respool)
1226     ("nndir" post-mail prompt-address address)
1227     ("nneething" none prompt-address)
1228     ("nndoc" none prompt-address)
1229     ("nnbabyl" mail respool)
1230     ("nnkiboze" post virtual)
1231     ("nnsoup" post-mail)
1232     ("nnfolder" mail respool))
1233   "An alist of valid select methods.
1234 The first element of each list lists should be a string with the name
1235 of the select method.  The other elements may be be the category of
1236 this method (ie. `post', `mail', `none' or whatever) or other
1237 properties that this method has (like being respoolable).
1238 If you implement a new select method, all you should have to change is
1239 this variable.  I think.")
1240
1241 (defvar gnus-updated-mode-lines '(group article summary tree)
1242   "*List of buffers that should update their mode lines.
1243 The list may contain the symbols `group', `article' and `summary'.  If
1244 the corresponding symbol is present, Gnus will keep that mode line
1245 updated with information that may be pertinent.
1246 If this variable is nil, screen refresh may be quicker.")
1247
1248 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1249 (defvar gnus-mode-non-string-length nil
1250   "*Max length of mode-line non-string contents.
1251 If this is nil, Gnus will take space as is needed, leaving the rest
1252 of the modeline intact.")
1253
1254 ;see gnus-cus.el
1255 ;(defvar gnus-mouse-face 'highlight
1256 ;  "*Face used for mouse highlighting in Gnus.
1257 ;No mouse highlights will be done if `gnus-visual' is nil.")
1258
1259 (defvar gnus-summary-mark-below nil
1260   "*Mark all articles with a score below this variable as read.
1261 This variable is local to each summary buffer and usually set by the
1262 score file.")
1263
1264 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1265   "*List of functions used for sorting articles in the summary buffer.
1266 This variable is only used when not using a threaded display.")
1267
1268 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1269   "*List of functions used for sorting threads in the summary buffer.
1270 By default, threads are sorted by article number.
1271
1272 Each function takes two threads and return non-nil if the first thread
1273 should be sorted before the other.  If you use more than one function,
1274 the primary sort function should be the last.  You should probably
1275 always include `gnus-thread-sort-by-number' in the list of sorting
1276 functions -- preferably first.
1277
1278 Ready-mady functions include `gnus-thread-sort-by-number',
1279 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1280 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1281 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1282
1283 (defvar gnus-thread-score-function '+
1284   "*Function used for calculating the total score of a thread.
1285
1286 The function is called with the scores of the article and each
1287 subthread and should then return the score of the thread.
1288
1289 Some functions you can use are `+', `max', or `min'.")
1290
1291 (defvar gnus-summary-expunge-below nil
1292   "All articles that have a score less than this variable will be expunged.")
1293
1294 (defvar gnus-thread-expunge-below nil
1295   "All threads that have a total score less than this variable will be expunged.
1296 See `gnus-thread-score-function' for en explanation of what a
1297 \"thread score\" is.")
1298
1299 (defvar gnus-auto-subscribed-groups
1300   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1301   "*All new groups that match this regexp will be subscribed automatically.
1302 Note that this variable only deals with new groups.  It has no effect
1303 whatsoever on old groups.")
1304
1305 (defvar gnus-options-subscribe nil
1306   "*All new groups matching this regexp will be subscribed unconditionally.
1307 Note that this variable deals only with new newsgroups.  This variable
1308 does not affect old newsgroups.")
1309
1310 (defvar gnus-options-not-subscribe nil
1311   "*All new groups matching this regexp will be ignored.
1312 Note that this variable deals only with new newsgroups.  This variable
1313 does not affect old (already subscribed) newsgroups.")
1314
1315 (defvar gnus-auto-expirable-newsgroups nil
1316   "*Groups in which to automatically mark read articles as expirable.
1317 If non-nil, this should be a regexp that should match all groups in
1318 which to perform auto-expiry.  This only makes sense for mail groups.")
1319
1320 (defvar gnus-total-expirable-newsgroups nil
1321   "*Groups in which to perform expiry of all read articles.
1322 Use with extreme caution.  All groups that match this regexp will be
1323 expiring - which means that all read articles will be deleted after
1324 (say) one week.  (This only goes for mail groups and the like, of
1325 course.)")
1326
1327 (defvar gnus-group-uncollapsed-levels 1
1328   "Number of group name elements to leave alone when making a short group name.")
1329
1330 (defvar gnus-hidden-properties '(invisible t intangible t)
1331   "Property list to use for hiding text.")
1332
1333 (defvar gnus-modtime-botch nil
1334   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1335 due to the bogus appearance that .newsrc was modified on disc.")
1336
1337 ;; Hooks.
1338
1339 (defvar gnus-group-mode-hook nil
1340   "*A hook for Gnus group mode.")
1341
1342 (defvar gnus-summary-mode-hook nil
1343   "*A hook for Gnus summary mode.
1344 This hook is run before any variables are set in the summary buffer.")
1345
1346 (defvar gnus-article-mode-hook nil
1347   "*A hook for Gnus article mode.")
1348
1349 (defvar gnus-summary-prepare-exit-hook nil
1350   "*A hook called when preparing to exit from the summary buffer.
1351 It calls `gnus-summary-expire-articles' by default.")
1352 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1353
1354 (defvar gnus-summary-exit-hook nil
1355   "*A hook called on exit from the summary buffer.")
1356
1357 (defvar gnus-group-catchup-group-hook nil
1358   "*A hook run when catching up a group from the group buffer.")
1359
1360 (defvar gnus-open-server-hook nil
1361   "*A hook called just before opening connection to the news server.")
1362
1363 (defvar gnus-load-hook nil
1364   "*A hook run while Gnus is loaded.")
1365
1366 (defvar gnus-startup-hook nil
1367   "*A hook called at startup.
1368 This hook is called after Gnus is connected to the NNTP server.")
1369
1370 (defvar gnus-get-new-news-hook nil
1371   "*A hook run just before Gnus checks for new news.")
1372
1373 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1374   "*A function that is called to generate the group buffer.
1375 The function is called with three arguments: The first is a number;
1376 all group with a level less or equal to that number should be listed,
1377 if the second is non-nil, empty groups should also be displayed.  If
1378 the third is non-nil, it is a number.  No groups with a level lower
1379 than this number should be displayed.
1380
1381 The only current function implemented is `gnus-group-prepare-flat'.")
1382
1383 (defvar gnus-group-prepare-hook nil
1384   "*A hook called after the group buffer has been generated.
1385 If you want to modify the group buffer, you can use this hook.")
1386
1387 (defvar gnus-summary-prepare-hook nil
1388   "*A hook called after the summary buffer has been generated.
1389 If you want to modify the summary buffer, you can use this hook.")
1390
1391 (defvar gnus-summary-generate-hook nil
1392   "*A hook run just before generating the summary buffer.
1393 This hook is commonly used to customize threading variables and the
1394 like.")
1395
1396 (defvar gnus-article-prepare-hook nil
1397   "*A hook called after an article has been prepared in the article buffer.
1398 If you want to run a special decoding program like nkf, use this hook.")
1399
1400 ;(defvar gnus-article-display-hook nil
1401 ;  "*A hook called after the article is displayed in the article buffer.
1402 ;The hook is designed to change the contents of the article
1403 ;buffer.  Typical functions that this hook may contain are
1404 ;`gnus-article-hide-headers' (hide selected headers),
1405 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1406 ;`gnus-article-hide-signature' (hide signature) and
1407 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1408 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1409 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1410 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1411
1412 (defvar gnus-article-x-face-command
1413   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1414   "String or function to be executed to display an X-Face header.
1415 If it is a string, the command will be executed in a sub-shell
1416 asynchronously.  The compressed face will be piped to this command.")
1417
1418 (defvar gnus-article-x-face-too-ugly nil
1419   "Regexp matching posters whose face shouldn't be shown automatically.")
1420
1421 (defvar gnus-select-group-hook nil
1422   "*A hook called when a newsgroup is selected.
1423
1424 If you'd like to simplify subjects like the
1425 `gnus-summary-next-same-subject' command does, you can use the
1426 following hook:
1427
1428  (setq gnus-select-group-hook
1429       (list
1430         (lambda ()
1431           (mapcar (lambda (header)
1432                      (mail-header-set-subject
1433                       header
1434                       (gnus-simplify-subject
1435                        (mail-header-subject header) 're-only)))
1436                   gnus-newsgroup-headers))))")
1437
1438 (defvar gnus-select-article-hook nil
1439   "*A hook called when an article is selected.")
1440
1441 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1442   "*A hook called to apply kill files to a group.
1443 This hook is intended to apply a kill file to the selected newsgroup.
1444 The function `gnus-apply-kill-file' is called by default.
1445
1446 Since a general kill file is too heavy to use only for a few
1447 newsgroups, I recommend you to use a lighter hook function.  For
1448 example, if you'd like to apply a kill file to articles which contains
1449 a string `rmgroup' in subject in newsgroup `control', you can use the
1450 following hook:
1451
1452  (setq gnus-apply-kill-hook
1453       (list
1454         (lambda ()
1455           (cond ((string-match \"control\" gnus-newsgroup-name)
1456                  (gnus-kill \"Subject\" \"rmgroup\")
1457                  (gnus-expunge \"X\"))))))")
1458
1459 (defvar gnus-visual-mark-article-hook
1460   (list 'gnus-highlight-selected-summary)
1461   "*Hook run after selecting an article in the summary buffer.
1462 It is meant to be used for highlighting the article in some way.  It
1463 is not run if `gnus-visual' is nil.")
1464
1465 (defvar gnus-parse-headers-hook nil
1466   "*A hook called before parsing the headers.")
1467
1468 (defvar gnus-exit-group-hook nil
1469   "*A hook called when exiting (not quitting) summary mode.")
1470
1471 (defvar gnus-suspend-gnus-hook nil
1472   "*A hook called when suspending (not exiting) Gnus.")
1473
1474 (defvar gnus-exit-gnus-hook nil
1475   "*A hook called when exiting Gnus.")
1476
1477 (defvar gnus-save-newsrc-hook nil
1478   "*A hook called before saving any of the newsrc files.")
1479
1480 (defvar gnus-save-quick-newsrc-hook nil
1481   "*A hook called just before saving the quick newsrc file.
1482 Can be used to turn version control on or off.")
1483
1484 (defvar gnus-save-standard-newsrc-hook nil
1485   "*A hook called just before saving the standard newsrc file.
1486 Can be used to turn version control on or off.")
1487
1488 (defvar gnus-summary-update-hook
1489   (list 'gnus-summary-highlight-line)
1490   "*A hook called when a summary line is changed.
1491 The hook will not be called if `gnus-visual' is nil.
1492
1493 The default function `gnus-summary-highlight-line' will
1494 highlight the line according to the `gnus-summary-highlight'
1495 variable.")
1496
1497 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1498   "*A hook called when a group line is changed.
1499 The hook will not be called if `gnus-visual' is nil.
1500
1501 The default function `gnus-group-highlight-line' will
1502 highlight the line according to the `gnus-group-highlight'
1503 variable.")
1504
1505 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1506   "*A hook called when an article is selected for the first time.
1507 The hook is intended to mark an article as read (or unread)
1508 automatically when it is selected.")
1509
1510 (defvar gnus-group-change-level-function nil
1511   "Function run when a group level is changed.
1512 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1513
1514 ;; Remove any hilit infestation.
1515 (add-hook 'gnus-startup-hook
1516           (lambda ()
1517             (remove-hook 'gnus-summary-prepare-hook
1518                          'hilit-rehighlight-buffer-quietly)
1519             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1520             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1521             (remove-hook 'gnus-article-prepare-hook
1522                          'hilit-rehighlight-buffer-quietly)))
1523
1524
1525 \f
1526 ;; Internal variables
1527
1528 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1529
1530 (defvar gnus-goto-missing-group-function nil)
1531
1532 (defvar gnus-override-subscribe-method nil)
1533
1534 (defvar gnus-group-goto-next-group-function nil
1535   "Function to override finding the next group after listing groups.")
1536
1537 (defconst gnus-article-mark-lists
1538   '((marked . tick) (replied . reply)
1539     (expirable . expire) (killed . killed)
1540     (bookmarks . bookmark) (dormant . dormant)
1541     (scored . score) (saved . save)
1542     (cached . cache)))
1543
1544 ;; Avoid highlighting in kill files.
1545 (defvar gnus-summary-inhibit-highlight nil)
1546 (defvar gnus-newsgroup-selected-overlay nil)
1547
1548 (defvar gnus-inhibit-hiding nil)
1549 (defvar gnus-group-indentation "")
1550 (defvar gnus-inhibit-limiting nil)
1551
1552 (defvar gnus-article-mode-map nil)
1553 (defvar gnus-dribble-buffer nil)
1554 (defvar gnus-headers-retrieved-by nil)
1555 (defvar gnus-article-reply nil)
1556 (defvar gnus-override-method nil)
1557 (defvar gnus-article-check-size nil)
1558
1559 (defvar gnus-nocem-hashtb nil)
1560
1561 (defvar gnus-current-score-file nil)
1562 (defvar gnus-newsgroup-adaptive-score-file nil)
1563 (defvar gnus-scores-exclude-files nil)
1564
1565 (defvar gnus-opened-servers nil)
1566
1567 (defvar gnus-current-move-group nil)
1568
1569 (defvar gnus-newsgroup-dependencies nil)
1570 (defvar gnus-newsgroup-async nil)
1571 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1572
1573 (defvar gnus-newsgroup-adaptive nil)
1574
1575 (defvar gnus-summary-display-table nil)
1576 (defvar gnus-summary-display-article-function nil)
1577
1578 (defvar gnus-summary-highlight-line-function nil
1579   "Function called after highlighting a summary line.")
1580
1581 (defvar gnus-group-line-format-alist
1582   `((?M gnus-tmp-marked-mark ?c)
1583     (?S gnus-tmp-subscribed ?c)
1584     (?L gnus-tmp-level ?d)
1585     (?N (cond ((eq number t) "*" )
1586               ((numberp number) 
1587                (int-to-string
1588                 (+ number
1589                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1590                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1591               (t number)) ?s)
1592     (?R gnus-tmp-number-of-read ?s)
1593     (?t gnus-tmp-number-total ?d)
1594     (?y gnus-tmp-number-of-unread ?s)
1595     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1596     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1597     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1598            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1599     (?g gnus-tmp-group ?s)
1600     (?G gnus-tmp-qualified-group ?s)
1601     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1602     (?D gnus-tmp-newsgroup-description ?s)
1603     (?o gnus-tmp-moderated ?c)
1604     (?O gnus-tmp-moderated-string ?s)
1605     (?p gnus-tmp-process-marked ?c)
1606     (?s gnus-tmp-news-server ?s)
1607     (?n gnus-tmp-news-method ?s)
1608     (?P gnus-group-indentation ?s)
1609     (?z gnus-tmp-news-method-string ?s)
1610     (?u gnus-tmp-user-defined ?s)))
1611
1612 (defvar gnus-summary-line-format-alist
1613   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1614     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1615     (?s gnus-tmp-subject-or-nil ?s)
1616     (?n gnus-tmp-name ?s)
1617     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1618         ?s)
1619     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1620             gnus-tmp-from) ?s)
1621     (?F gnus-tmp-from ?s)
1622     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1623     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1624     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1625     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1626     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1627     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1628     (?L gnus-tmp-lines ?d)
1629     (?I gnus-tmp-indentation ?s)
1630     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1631     (?R gnus-tmp-replied ?c)
1632     (?\[ gnus-tmp-opening-bracket ?c)
1633     (?\] gnus-tmp-closing-bracket ?c)
1634     (?\> (make-string gnus-tmp-level ? ) ?s)
1635     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1636     (?i gnus-tmp-score ?d)
1637     (?z gnus-tmp-score-char ?c)
1638     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1639     (?U gnus-tmp-unread ?c)
1640     (?t (gnus-summary-number-of-articles-in-thread
1641          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1642         ?d)
1643     (?e (gnus-summary-number-of-articles-in-thread
1644          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1645         ?c)
1646     (?u gnus-tmp-user-defined ?s))
1647   "An alist of format specifications that can appear in summary lines,
1648 and what variables they correspond with, along with the type of the
1649 variable (string, integer, character, etc).")
1650
1651 (defvar gnus-summary-dummy-line-format-alist
1652   (` ((?S gnus-tmp-subject ?s)
1653       (?N gnus-tmp-number ?d)
1654       (?u gnus-tmp-user-defined ?s))))
1655
1656 (defvar gnus-summary-mode-line-format-alist
1657   (` ((?G gnus-tmp-group-name ?s)
1658       (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1659       (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1660       (?A gnus-tmp-article-number ?d)
1661       (?Z gnus-tmp-unread-and-unselected ?s)
1662       (?V gnus-version ?s)
1663       (?U gnus-tmp-unread ?d)
1664       (?S gnus-tmp-subject ?s)
1665       (?e gnus-tmp-unselected ?d)
1666       (?u gnus-tmp-user-defined ?s)
1667       (?d (length gnus-newsgroup-dormant) ?d)
1668       (?t (length gnus-newsgroup-marked) ?d)
1669       (?r (length gnus-newsgroup-reads) ?d)
1670       (?E gnus-newsgroup-expunged-tally ?d)
1671       (?s (gnus-current-score-file-nondirectory) ?s))))
1672
1673 (defvar gnus-article-mode-line-format-alist
1674   gnus-summary-mode-line-format-alist)
1675
1676 (defvar gnus-group-mode-line-format-alist
1677   (` ((?S gnus-tmp-news-server ?s)
1678       (?M gnus-tmp-news-method ?s)
1679       (?u gnus-tmp-user-defined ?s))))
1680
1681 (defvar gnus-have-read-active-file nil)
1682
1683 (defconst gnus-maintainer
1684   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1685   "The mail address of the Gnus maintainers.")
1686
1687 (defconst gnus-version "September Gnus v0.37"
1688   "Version number for this version of Gnus.")
1689
1690 (defvar gnus-info-nodes
1691   '((gnus-group-mode            "(gnus)The Group Buffer")
1692     (gnus-summary-mode          "(gnus)The Summary Buffer")
1693     (gnus-article-mode          "(gnus)The Article Buffer"))
1694   "Assoc list of major modes and related Info nodes.")
1695
1696 (defvar gnus-group-buffer "*Group*")
1697 (defvar gnus-summary-buffer "*Summary*")
1698 (defvar gnus-article-buffer "*Article*")
1699 (defvar gnus-server-buffer "*Server*")
1700
1701 (defvar gnus-work-buffer " *gnus work*")
1702
1703 (defvar gnus-original-article-buffer " *Original Article*")
1704 (defvar gnus-original-article nil)
1705
1706 (defvar gnus-buffer-list nil
1707   "Gnus buffers that should be killed on exit.")
1708
1709 (defvar gnus-server-alist nil
1710   "List of available servers.")
1711
1712 (defvar gnus-slave nil
1713   "Whether this Gnus is a slave or not.")
1714
1715 (defvar gnus-variable-list
1716   '(gnus-newsrc-options gnus-newsrc-options-n
1717     gnus-newsrc-last-checked-date
1718     gnus-newsrc-alist gnus-server-alist
1719     gnus-killed-list gnus-zombie-list
1720     gnus-topic-topology gnus-topic-alist
1721     gnus-format-specs)
1722   "Gnus variables saved in the quick startup file.")
1723
1724 (defvar gnus-newsrc-options nil
1725   "Options line in the .newsrc file.")
1726
1727 (defvar gnus-newsrc-options-n nil
1728   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1729
1730 (defvar gnus-newsrc-last-checked-date nil
1731   "Date Gnus last asked server for new newsgroups.")
1732
1733 (defvar gnus-topic-topology nil
1734   "The complete topic hierarchy.")
1735
1736 (defvar gnus-topic-alist nil
1737   "The complete topic-group alist.")
1738
1739 (defvar gnus-newsrc-alist nil
1740   "Assoc list of read articles.
1741 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1742
1743 (defvar gnus-newsrc-hashtb nil
1744   "Hashtable of gnus-newsrc-alist.")
1745
1746 (defvar gnus-killed-list nil
1747   "List of killed newsgroups.")
1748
1749 (defvar gnus-killed-hashtb nil
1750   "Hash table equivalent of gnus-killed-list.")
1751
1752 (defvar gnus-zombie-list nil
1753   "List of almost dead newsgroups.")
1754
1755 (defvar gnus-description-hashtb nil
1756   "Descriptions of newsgroups.")
1757
1758 (defvar gnus-list-of-killed-groups nil
1759   "List of newsgroups that have recently been killed by the user.")
1760
1761 (defvar gnus-active-hashtb nil
1762   "Hashtable of active articles.")
1763
1764 (defvar gnus-moderated-list nil
1765   "List of moderated newsgroups.")
1766
1767 (defvar gnus-group-marked nil)
1768
1769 (defvar gnus-current-startup-file nil
1770   "Startup file for the current host.")
1771
1772 (defvar gnus-last-search-regexp nil
1773   "Default regexp for article search command.")
1774
1775 (defvar gnus-last-shell-command nil
1776   "Default shell command on article.")
1777
1778 (defvar gnus-current-select-method nil
1779   "The current method for selecting a newsgroup.")
1780
1781 (defvar gnus-group-list-mode nil)
1782
1783 (defvar gnus-article-internal-prepare-hook nil)
1784
1785 (defvar gnus-newsgroup-name nil)
1786 (defvar gnus-newsgroup-begin nil)
1787 (defvar gnus-newsgroup-end nil)
1788 (defvar gnus-newsgroup-last-rmail nil)
1789 (defvar gnus-newsgroup-last-mail nil)
1790 (defvar gnus-newsgroup-last-folder nil)
1791 (defvar gnus-newsgroup-last-file nil)
1792 (defvar gnus-newsgroup-auto-expire nil)
1793 (defvar gnus-newsgroup-active nil)
1794
1795 (defvar gnus-newsgroup-data nil)
1796 (defvar gnus-newsgroup-data-reverse nil)
1797 (defvar gnus-newsgroup-limit nil)
1798 (defvar gnus-newsgroup-limits nil)
1799
1800 (defvar gnus-newsgroup-unreads nil
1801   "List of unread articles in the current newsgroup.")
1802
1803 (defvar gnus-newsgroup-unselected nil
1804   "List of unselected unread articles in the current newsgroup.")
1805
1806 (defvar gnus-newsgroup-reads nil
1807   "Alist of read articles and article marks in the current newsgroup.")
1808
1809 (defvar gnus-newsgroup-expunged-tally nil)
1810
1811 (defvar gnus-newsgroup-marked nil
1812   "List of ticked articles in the current newsgroup (a subset of unread art).")
1813
1814 (defvar gnus-newsgroup-killed nil
1815   "List of ranges of articles that have been through the scoring process.")
1816
1817 (defvar gnus-newsgroup-cached nil
1818   "List of articles that come from the article cache.")
1819
1820 (defvar gnus-newsgroup-saved nil
1821   "List of articles that have been saved.")
1822
1823 (defvar gnus-newsgroup-kill-headers nil)
1824
1825 (defvar gnus-newsgroup-replied nil
1826   "List of articles that have been replied to in the current newsgroup.")
1827
1828 (defvar gnus-newsgroup-expirable nil
1829   "List of articles in the current newsgroup that can be expired.")
1830
1831 (defvar gnus-newsgroup-processable nil
1832   "List of articles in the current newsgroup that can be processed.")
1833
1834 (defvar gnus-newsgroup-bookmarks nil
1835   "List of articles in the current newsgroup that have bookmarks.")
1836
1837 (defvar gnus-newsgroup-dormant nil
1838   "List of dormant articles in the current newsgroup.")
1839
1840 (defvar gnus-newsgroup-scored nil
1841   "List of scored articles in the current newsgroup.")
1842
1843 (defvar gnus-newsgroup-headers nil
1844   "List of article headers in the current newsgroup.")
1845
1846 (defvar gnus-newsgroup-threads nil)
1847
1848 (defvar gnus-newsgroup-prepared nil
1849   "Whether the current group has been prepared properly.")
1850
1851 (defvar gnus-newsgroup-ancient nil
1852   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-sparse nil)
1855
1856 (defvar gnus-current-article nil)
1857 (defvar gnus-article-current nil)
1858 (defvar gnus-current-headers nil)
1859 (defvar gnus-have-all-headers nil)
1860 (defvar gnus-last-article nil)
1861 (defvar gnus-newsgroup-history nil)
1862 (defvar gnus-current-kill-article nil)
1863
1864 ;; Save window configuration.
1865 (defvar gnus-prev-winconf nil)
1866
1867 (defvar gnus-summary-mark-positions nil)
1868 (defvar gnus-group-mark-positions nil)
1869
1870 (defvar gnus-reffed-article-number nil)
1871
1872 ;;; Let the byte-compiler know that we know about this variable.
1873 (defvar rmail-default-rmail-file)
1874
1875 (defvar gnus-cache-removable-articles nil)
1876
1877 (defvar gnus-dead-summary nil)
1878
1879 (defconst gnus-summary-local-variables
1880   '(gnus-newsgroup-name
1881     gnus-newsgroup-begin gnus-newsgroup-end
1882     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1883     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1884     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1885     gnus-newsgroup-unselected gnus-newsgroup-marked
1886     gnus-newsgroup-reads gnus-newsgroup-saved
1887     gnus-newsgroup-replied gnus-newsgroup-expirable
1888     gnus-newsgroup-processable gnus-newsgroup-killed
1889     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1890     gnus-newsgroup-headers gnus-newsgroup-threads
1891     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1892     gnus-current-article gnus-current-headers gnus-have-all-headers
1893     gnus-last-article gnus-article-internal-prepare-hook
1894     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1895     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1896     gnus-newsgroup-async 
1897     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1898     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1899     gnus-newsgroup-history gnus-newsgroup-ancient
1900     gnus-newsgroup-sparse
1901     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1902     gnus-newsgroup-adaptive-score-file
1903     (gnus-newsgroup-expunged-tally . 0)
1904     gnus-cache-removeable-articles gnus-newsgroup-cached
1905     gnus-newsgroup-data gnus-newsgroup-data-reverse
1906     gnus-newsgroup-limit gnus-newsgroup-limits)
1907   "Variables that are buffer-local to the summary buffers.")
1908
1909 (defconst gnus-bug-message
1910   "Sending a bug report to the Gnus Towers.
1911 ========================================
1912
1913 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1914 be sent to the Gnus Bug Exterminators.
1915
1916 At the bottom of the buffer you'll see lots of variable settings.
1917 Please do not delete those.  They will tell the Bug People what your
1918 environment is, so that it will be easier to locate the bugs.
1919
1920 If you have found a bug that makes Emacs go \"beep\", set
1921 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1922 and include the backtrace in your bug report.
1923
1924 Please describe the bug in annoying, painstaking detail.
1925
1926 Thank you for your help in stamping out bugs.
1927 ")
1928
1929 ;;; End of variables.
1930
1931 ;; Define some autoload functions Gnus might use.
1932 (eval-and-compile
1933
1934   ;; This little mapcar goes through the list below and marks the
1935   ;; symbols in question as autoloaded functions.
1936   (mapcar
1937    (lambda (package)
1938      (let ((interactive (nth 1 (memq ':interactive package))))
1939        (mapcar
1940         (lambda (function)
1941           (let (keymap)
1942             (when (consp function)
1943               (setq keymap (car (memq 'keymap function)))
1944               (setq function (car function)))
1945             (autoload function (car package) nil interactive keymap)))
1946         (if (eq (nth 1 package) ':interactive)
1947             (cdddr package)
1948           (cdr package)))))
1949    '(("metamail" metamail-buffer)
1950      ("info" Info-goto-node)
1951      ("hexl" hexl-hex-string-to-integer)
1952      ("pp" pp pp-to-string pp-eval-expression)
1953      ("mail-extr" mail-extract-address-components)
1954      ("nnmail" nnmail-split-fancy nnmail-article-group)
1955      ("nnvirtual" nnvirtual-catchup-group)
1956      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1957       timezone-make-sortable-date timezone-make-time-string)
1958      ("sendmail" mail-position-on-field mail-setup)
1959      ("rmailout" rmail-output)
1960      ("rnewspost" news-mail-other-window news-reply-yank-original
1961       news-caesar-buffer-body)
1962      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1963       rmail-show-message)
1964      ("gnus-soup" :interactive t
1965       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1966       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1967      ("nnsoup" nnsoup-pack-replies)
1968      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1969       gnus-Folder-save-name gnus-folder-save-name)
1970      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1971      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1972       gnus-server-make-menu-bar gnus-article-make-menu-bar
1973       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1974       gnus-summary-highlight-line gnus-carpal-setup-buffer
1975       gnus-group-highlight-line
1976       gnus-article-add-button gnus-insert-next-page-button
1977       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1978      ("gnus-vis" :interactive t
1979       gnus-article-push-button gnus-article-press-button
1980       gnus-article-highlight gnus-article-highlight-some
1981       gnus-article-highlight-headers gnus-article-highlight-signature
1982       gnus-article-add-buttons gnus-article-add-buttons-to-head
1983       gnus-article-next-button gnus-article-prev-button)
1984      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1985       gnus-demon-add-disconnection gnus-demon-add-handler
1986       gnus-demon-remove-handler)
1987      ("gnus-demon" :interactive t
1988       gnus-demon-init gnus-demon-cancel)
1989      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1990       gnus-tree-open gnus-tree-close)
1991      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close)
1992      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
1993      ("gnus-srvr" gnus-browse-foreign-server)
1994      ("gnus-cite" :interactive t
1995       gnus-article-highlight-citation gnus-article-hide-citation-maybe
1996       gnus-article-hide-citation gnus-article-fill-cited-article)
1997      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
1998       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
1999       gnus-execute gnus-expunge)
2000      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2001       gnus-cache-possibly-remove-articles gnus-cache-request-article
2002       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2003       gnus-cache-enter-remove-article gnus-cached-article-p
2004       gnus-cache-open gnus-cache-close)
2005      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2006       gnus-cache-remove-article)
2007      ("gnus-score" :interactive t
2008       gnus-summary-increase-score gnus-summary-lower-score
2009       gnus-score-flush-cache gnus-score-close
2010       gnus-score-raise-same-subject-and-select
2011       gnus-score-raise-same-subject gnus-score-default
2012       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2013       gnus-score-lower-same-subject gnus-score-lower-thread
2014       gnus-possibly-score-headers)
2015      ("gnus-score"
2016       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2017       gnus-current-score-file-nondirectory gnus-score-adaptive
2018       gnus-score-find-trace gnus-score-file-name)
2019      ("gnus-edit" :interactive t gnus-score-customize)
2020      ("gnus-topic" :interactive t gnus-topic-mode)
2021      ("gnus-topic" gnus-topic-remove-group)
2022      ("gnus-salt" :interactive t gnus-pick-mode)
2023      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2024      ("gnus-uu" :interactive t
2025       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2026       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2027       gnus-uu-mark-by-regexp gnus-uu-mark-all
2028       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2029       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2030       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2031       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2032       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2033       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2034       gnus-uu-decode-binhex-view)
2035      ("gnus-msg" (gnus-summary-send-map keymap)
2036       gnus-mail-yank-original gnus-mail-send-and-exit
2037       gnus-sendmail-setup-mail gnus-article-mail
2038       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2039      ("gnus-msg" :interactive t
2040       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2041       gnus-summary-followup gnus-summary-followup-with-original
2042       gnus-summary-followup-and-reply
2043       gnus-summary-followup-and-reply-with-original
2044       gnus-summary-cancel-article gnus-summary-supersede-article
2045       gnus-post-news gnus-inews-news gnus-cancel-news
2046       gnus-summary-reply gnus-summary-reply-with-original
2047       gnus-summary-mail-forward gnus-summary-mail-other-window
2048       gnus-bug)
2049      ("gnus-picon" :interactive t gnus-article-display-picons
2050       gnus-group-display-picons gnus-picons-article-display-x-face)
2051      ("gnus-vm" gnus-vm-mail-setup)
2052      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2053       gnus-summary-save-article-vm gnus-yank-article))))
2054
2055 \f
2056
2057 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2058 ;; If you want the cursor to go somewhere else, set these two
2059 ;; functions in some startup hook to whatever you want.
2060 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2061 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2062
2063 ;;; Various macros and substs.
2064
2065 (defun gnus-header-from (header)
2066   (mail-header-from header))
2067
2068 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2069   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2070   `(let ((GnusStartBufferWindow (selected-window)))
2071      (unwind-protect
2072          (progn
2073            (pop-to-buffer ,buffer)
2074            ,@forms)
2075        (select-window GnusStartBufferWindow))))
2076
2077 (defmacro gnus-gethash (string hashtable)
2078   "Get hash value of STRING in HASHTABLE."
2079   `(symbol-value (intern-soft ,string ,hashtable)))
2080
2081 (defmacro gnus-sethash (string value hashtable)
2082   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2083   `(set (intern ,string ,hashtable) ,value))
2084
2085 (defmacro gnus-intern-safe (string hashtable)
2086   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2087   `(let ((symbol (intern ,string ,hashtable)))
2088      (or (boundp symbol)
2089          (set symbol nil))
2090      symbol))
2091
2092 (defmacro gnus-group-unread (group)
2093   "Get the currently computed number of unread articles in GROUP."
2094   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2095
2096 (defmacro gnus-group-entry (group)
2097   "Get the newsrc entry for GROUP."
2098   `(gnus-gethash ,group gnus-newsrc-hashtb))
2099
2100 (defmacro gnus-active (group)
2101   "Get active info on GROUP."
2102   `(gnus-gethash ,group gnus-active-hashtb))
2103
2104 (defmacro gnus-set-active (group active)
2105   "Set GROUP's active info."
2106   `(gnus-sethash ,group ,active gnus-active-hashtb))
2107
2108 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2109 ;;   function `substring' might cut on a middle of multi-octet
2110 ;;   character.
2111 (defun gnus-truncate-string (str width)
2112   (substring str 0 width))
2113
2114 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2115 ;; to limit the length of a string.  This function is necessary since
2116 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2117 (defsubst gnus-limit-string (str width)
2118   (if (> (length str) width)
2119       (substring str 0 width)
2120     str))
2121
2122 (defsubst gnus-simplify-subject-re (subject)
2123   "Remove \"Re:\" from subject lines."
2124   (if (string-match "^[Rr][Ee]: *" subject)
2125       (substring subject (match-end 0))
2126     subject))
2127
2128 (defsubst gnus-goto-char (point)
2129   (and point (goto-char point)))
2130
2131 (defmacro gnus-buffer-exists-p (buffer)
2132   `(and ,buffer
2133         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2134                  ,buffer)))
2135
2136 (defmacro gnus-kill-buffer (buffer)
2137   `(let ((buf ,buffer))
2138      (if (gnus-buffer-exists-p buf)
2139          (kill-buffer buf))))
2140
2141 (defsubst gnus-point-at-bol ()
2142   "Return point at the beginning of the line."
2143   (let ((p (point)))
2144     (beginning-of-line)
2145     (prog1
2146         (point)
2147       (goto-char p))))
2148
2149 (defsubst gnus-point-at-eol ()
2150   "Return point at the end of the line."
2151   (let ((p (point)))
2152     (end-of-line)
2153     (prog1
2154         (point)
2155       (goto-char p))))
2156
2157 ;; Delete the current line (and the next N lines.);
2158 (defmacro gnus-delete-line (&optional n)
2159   `(delete-region (progn (beginning-of-line) (point))
2160                   (progn (forward-line ,(or n 1)) (point))))
2161
2162 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2163 (defvar gnus-init-inhibit nil)
2164 (defun gnus-read-init-file (&optional inhibit-next)
2165   (if gnus-init-inhibit
2166       (setq gnus-init-inhibit nil)
2167     (setq gnus-init-inhibit inhibit-next)
2168     (and gnus-init-file
2169          (or (and (file-exists-p gnus-init-file)
2170                   ;; Don't try to load a directory.
2171                   (not (file-directory-p gnus-init-file)))
2172              (file-exists-p (concat gnus-init-file ".el"))
2173              (file-exists-p (concat gnus-init-file ".elc")))
2174          (condition-case var
2175              (load gnus-init-file nil t)
2176            (error
2177             (error "Error in %s: %s" gnus-init-file var))))))
2178
2179 ;; Info access macros.
2180
2181 (defmacro gnus-info-group (info)
2182   `(nth 0 ,info))
2183 (defmacro gnus-info-rank (info)
2184   `(nth 1 ,info))
2185 (defmacro gnus-info-read (info)
2186   `(nth 2 ,info))
2187 (defmacro gnus-info-marks (info)
2188   `(nth 3 ,info))
2189 (defmacro gnus-info-method (info)
2190   `(nth 4 ,info))
2191 (defmacro gnus-info-params (info)
2192   `(nth 5 ,info))
2193
2194 (defmacro gnus-info-level (info)
2195   `(let ((rank (gnus-info-rank ,info)))
2196      (if (consp rank)
2197          (car rank)
2198        rank)))
2199 (defmacro gnus-info-score (info)
2200   `(let ((rank (gnus-info-rank ,info)))
2201      (or (and (consp rank) (cdr rank)) 0)))
2202
2203 (defmacro gnus-info-set-group (info group)
2204   `(setcar ,info ,group))
2205 (defmacro gnus-info-set-rank (info rank)
2206   `(setcar (nthcdr 1 ,info) ,rank))
2207 (defmacro gnus-info-set-read (info read)
2208   `(setcar (nthcdr 2 ,info) ,read))
2209 (defmacro gnus-info-set-marks (info marks)
2210   `(setcar (nthcdr 3 ,info) ,marks))
2211 (defmacro gnus-info-set-method (info method)
2212   `(setcar (nthcdr 4 ,info) ,method))
2213 (defmacro gnus-info-set-params (info params)
2214   `(setcar (nthcdr 5 ,info) ,params))
2215
2216 (defmacro gnus-info-set-level (info level)
2217   `(let ((rank (cdr ,info)))
2218      (if (consp (car rank))
2219          (setcar (car rank) ,level)
2220        (setcar rank ,level))))
2221 (defmacro gnus-info-set-score (info score)
2222   `(let ((rank (cdr ,info)))
2223      (if (consp (car rank))
2224          (setcdr (car rank) ,score)
2225        (setcar rank (cons (car rank) ,score)))))
2226
2227 (defmacro gnus-get-info (group)
2228   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2229
2230 (defun gnus-byte-code (func)
2231   "Return a form that can be `eval'ed based on FUNC."
2232   (let ((fval (symbol-function func)))
2233     (if (byte-code-function-p fval)
2234         (let ((flist (append fval nil)))
2235           (setcar flist 'byte-code)
2236           flist)
2237       (cons 'progn (cdr (cdr fval))))))
2238
2239 ;;; Load the compatability functions.
2240
2241 (require 'gnus-cus)
2242 (require 'gnus-ems)
2243
2244 \f
2245
2246 ;; Format specs.  The chunks below are the machine-generated forms
2247 ;; that are to be evaled as the result of the default format strings.
2248 ;; We write them in here to get them byte-compiled.  That way the
2249 ;; default actions will be quite fast, while still retaining the full
2250 ;; flexibility of the user-defined format specs.
2251
2252 ;; First we have lots of dummy defvars to let the compiler know these
2253 ;; are really dynamic variables.
2254
2255 (defvar gnus-tmp-unread)
2256 (defvar gnus-tmp-replied)
2257 (defvar gnus-tmp-score-char)
2258 (defvar gnus-tmp-indentation)
2259 (defvar gnus-tmp-opening-bracket)
2260 (defvar gnus-tmp-lines)
2261 (defvar gnus-tmp-name)
2262 (defvar gnus-tmp-closing-bracket)
2263 (defvar gnus-tmp-subject-or-nil)
2264 (defvar gnus-tmp-subject)
2265 (defvar gnus-tmp-marked)
2266 (defvar gnus-tmp-marked-mark)
2267 (defvar gnus-tmp-subscribed)
2268 (defvar gnus-tmp-process-marked)
2269 (defvar gnus-tmp-number-of-unread)
2270 (defvar gnus-tmp-group-name)
2271 (defvar gnus-tmp-group)
2272 (defvar gnus-tmp-article-number)
2273 (defvar gnus-tmp-unread-and-unselected)
2274 (defvar gnus-tmp-news-method)
2275 (defvar gnus-tmp-news-server)
2276 (defvar gnus-tmp-article-number)
2277 (defvar gnus-mouse-face)
2278 (defvar gnus-mouse-face-prop)
2279
2280 (defun gnus-summary-line-format-spec ()
2281   (insert gnus-tmp-unread gnus-tmp-replied
2282           gnus-tmp-score-char gnus-tmp-indentation)
2283   (put-text-property
2284    (point)
2285    (progn
2286      (insert
2287       gnus-tmp-opening-bracket
2288       (format "%4d: %-20s"
2289               gnus-tmp-lines
2290               (if (> (length gnus-tmp-name) 20)
2291                   (substring gnus-tmp-name 0 20)
2292                 gnus-tmp-name))
2293       gnus-tmp-closing-bracket)
2294      (point))
2295    gnus-mouse-face-prop gnus-mouse-face)
2296   (insert " " gnus-tmp-subject-or-nil "\n"))
2297
2298 (defvar gnus-summary-line-format-spec
2299   (gnus-byte-code 'gnus-summary-line-format-spec))
2300
2301 (defun gnus-summary-dummy-line-format-spec ()
2302   (insert "*  ")
2303   (put-text-property
2304    (point)
2305    (progn
2306      (insert ":                          :")
2307      (point))
2308    gnus-mouse-face-prop gnus-mouse-face)
2309   (insert " " gnus-tmp-subject "\n"))
2310
2311 (defvar gnus-summary-dummy-line-format-spec
2312   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2313
2314 (defun gnus-group-line-format-spec ()
2315   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2316           gnus-tmp-process-marked
2317           gnus-group-indentation
2318           (format "%5s: " gnus-tmp-number-of-unread))
2319   (put-text-property
2320    (point)
2321    (progn
2322      (insert gnus-tmp-group "\n")
2323      (1- (point)))
2324    gnus-mouse-face-prop gnus-mouse-face))
2325 (defvar gnus-group-line-format-spec
2326   (gnus-byte-code 'gnus-group-line-format-spec))
2327
2328 (defvar gnus-format-specs
2329   `((version . ,emacs-version)
2330     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2331     (summary-dummy ,gnus-summary-dummy-line-format
2332                    ,gnus-summary-dummy-line-format-spec)
2333     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2334
2335 (defvar gnus-article-mode-line-format-spec nil)
2336 (defvar gnus-summary-mode-line-format-spec nil)
2337 (defvar gnus-group-mode-line-format-spec nil)
2338
2339 ;;; Phew.  All that gruft is over, fortunately.
2340
2341 \f
2342 ;;;
2343 ;;; Gnus Utility Functions
2344 ;;;
2345
2346 (defun gnus-extract-address-components (from)
2347   (let (name address)
2348     ;; First find the address - the thing with the @ in it.  This may
2349     ;; not be accurate in mail addresses, but does the trick most of
2350     ;; the time in news messages.
2351     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2352         (setq address (substring from (match-beginning 0) (match-end 0))))
2353     ;; Then we check whether the "name <address>" format is used.
2354     (and address
2355          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2356          ;; Linear white space is not required.
2357          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2358          (and (setq name (substring from 0 (match-beginning 0)))
2359               ;; Strip any quotes from the name.
2360               (string-match "\".*\"" name)
2361               (setq name (substring name 1 (1- (match-end 0))))))
2362     ;; If not, then "address (name)" is used.
2363     (or name
2364         (and (string-match "(.+)" from)
2365              (setq name (substring from (1+ (match-beginning 0))
2366                                    (1- (match-end 0)))))
2367         (and (string-match "()" from)
2368              (setq name address))
2369         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2370         ;; XOVER might not support folded From headers.
2371         (and (string-match "(.*" from)
2372              (setq name (substring from (1+ (match-beginning 0))
2373                                    (match-end 0)))))
2374     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2375     (list (or name from) (or address from))))
2376
2377 (defun gnus-fetch-field (field)
2378   "Return the value of the header FIELD of current article."
2379   (save-excursion
2380     (save-restriction
2381       (let ((case-fold-search t))
2382         (nnheader-narrow-to-headers)
2383         (mail-fetch-field field)))))
2384
2385 (defun gnus-goto-colon ()
2386   (beginning-of-line)
2387   (search-forward ":" (gnus-point-at-eol) t))
2388
2389 ;;;###autoload
2390 (defun gnus-update-format (var)
2391   "Update the format specification near point."
2392   (interactive
2393    (list
2394     (save-excursion
2395       (eval-defun nil)
2396       ;; Find the end of the current word.
2397       (re-search-forward "[ \t\n]" nil t)
2398       ;; Search backward.
2399       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2400         (match-string 1)))))
2401   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2402                               (match-string 1 var))))
2403          (entry (assq type gnus-format-specs))
2404          value spec)
2405     (when entry
2406       (setq gnus-format-specs (delq entry gnus-format-specs)))
2407     (set
2408      (intern (format "%s-spec" var))
2409      (gnus-parse-format (setq value (symbol-value (intern var)))
2410                         (symbol-value (intern (format "%s-alist" var)))
2411                         (not (string-match "mode" var))))
2412     (setq spec (symbol-value (intern (format "%s-spec" var))))
2413     (push (list type value spec) gnus-format-specs)
2414
2415     (pop-to-buffer "*Gnus Format*")
2416     (erase-buffer)
2417     (lisp-interaction-mode)
2418     (insert (pp-to-string spec))))
2419
2420
2421 (defun gnus-update-format-specifications (&optional force)
2422   "Update all (necessary) format specifications."
2423   ;; Make the indentation array.
2424   (gnus-make-thread-indent-array)
2425
2426   ;; See whether all the stored info needs to be flushed.
2427   (when (or force
2428             (not (equal emacs-version
2429                         (cdr (assq 'version gnus-format-specs)))))
2430     (setq gnus-format-specs nil))
2431
2432   ;; Go through all the formats and see whether they need updating.
2433   (let ((types '(summary summary-dummy group
2434                          summary-mode group-mode article-mode))
2435         new-format entry type val)
2436     (while (setq type (pop types))
2437       (setq new-format (symbol-value
2438                         (intern (format "gnus-%s-line-format" type))))
2439       (setq entry (cdr (assq type gnus-format-specs)))
2440       (if (and entry
2441                (equal (car entry) new-format))
2442           ;; Use the old format.
2443           (set (intern (format "gnus-%s-line-format-spec" type))
2444                (cadr entry))
2445         ;; This is a new format.
2446         (setq val
2447               (if (not (stringp new-format))
2448                   ;; This is a function call or something.
2449                   new-format
2450                 ;; This is a "real" format.
2451                 (gnus-parse-format
2452                  new-format
2453                  (symbol-value
2454                   (intern (format "gnus-%s-line-format-alist"
2455                                   (if (eq type 'article-mode)
2456                                       'summary-mode type))))
2457                  (not (string-match "mode$" (symbol-name type))))))
2458         ;; Enter the new format spec into the list.
2459         (if entry
2460             (progn
2461               (setcar (cdr entry) val)
2462               (setcar entry new-format))
2463           (push (list type new-format val) gnus-format-specs))
2464         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2465
2466   (gnus-update-group-mark-positions)
2467   (gnus-update-summary-mark-positions)
2468
2469   ;; See whether we need to read the description file.
2470   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2471            (not gnus-description-hashtb)
2472            gnus-read-active-file)
2473       (gnus-read-all-descriptions-files)))
2474
2475 (defun gnus-update-summary-mark-positions ()
2476   "Compute where the summary marks are to go."
2477   (save-excursion
2478     (let ((gnus-replied-mark 129)
2479           (gnus-score-below-mark 130)
2480           (gnus-score-over-mark 130)
2481           (thread nil)
2482           (gnus-visual nil)
2483           pos)
2484       (gnus-set-work-buffer)
2485       (gnus-summary-insert-line
2486        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2487       (goto-char (point-min))
2488       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2489                                          (- (point) 2)))))
2490       (goto-char (point-min))
2491       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
2492                                           (- (point) 2))) pos))
2493       (goto-char (point-min))
2494       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
2495                                         (- (point) 2))) pos))
2496       (setq gnus-summary-mark-positions pos))))
2497
2498 (defun gnus-update-group-mark-positions ()
2499   (save-excursion
2500     (let ((gnus-process-mark 128)
2501           (gnus-group-marked '("dummy.group")))
2502       (gnus-set-active "dummy.group" '(0 . 0))
2503       (gnus-set-work-buffer)
2504       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2505       (goto-char (point-min))
2506       (setq gnus-group-mark-positions
2507             (list (cons 'process (and (search-forward "\200" nil t)
2508                                       (- (point) 2))))))))
2509
2510 (defvar gnus-mouse-face-0 'highlight)
2511 (defvar gnus-mouse-face-1 'highlight)
2512 (defvar gnus-mouse-face-2 'highlight)
2513 (defvar gnus-mouse-face-3 'highlight)
2514 (defvar gnus-mouse-face-4 'highlight)
2515
2516 (defun gnus-mouse-face-function (form type)
2517   `(put-text-property
2518     (point) (progn ,@form (point))
2519     gnus-mouse-face-prop
2520     ,(if (equal type 0)
2521          'gnus-mouse-face
2522        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2523
2524 (defvar gnus-face-0 'bold)
2525 (defvar gnus-face-1 'italic)
2526 (defvar gnus-face-2 'bold-italic)
2527 (defvar gnus-face-3 'bold)
2528 (defvar gnus-face-4 'bold)
2529
2530 (defun gnus-face-face-function (form type)
2531   `(put-text-property
2532     (point) (progn ,@form (point))
2533     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2534
2535 (defun gnus-max-width-function (el max-width)
2536   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2537   (if (symbolp el)
2538       `(if (> (length ,el) ,max-width)
2539            (substring ,el 0 ,max-width)
2540          ,el)
2541     `(let ((val (eval ,el)))
2542        (if (numberp val)
2543            (setq val (int-to-string val)))
2544        (if (> (length val) ,max-width)
2545            (substring val 0 ,max-width)
2546          val))))
2547
2548 (defun gnus-parse-format (format spec-alist &optional insert)
2549   ;; This function parses the FORMAT string with the help of the
2550   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2551   ;; string.  If the FORMAT string contains the specifiers %( and %)
2552   ;; the text between them will have the mouse-face text property.
2553   (if (string-match
2554        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2555        format)
2556       (gnus-parse-complex-format format spec-alist)
2557     ;; This is a simple format.
2558     (gnus-parse-simple-format format spec-alist insert)))
2559
2560 (defun gnus-parse-complex-format (format spec-alist)
2561   (save-excursion
2562     (gnus-set-work-buffer)
2563     (insert format)
2564     (goto-char (point-min))
2565     (while (re-search-forward "\"" nil t)
2566       (replace-match "\\\"" nil t))
2567     (goto-char (point-min))
2568     (insert "(\"")
2569     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2570       (let ((number (if (match-beginning 1)
2571                         (match-string 1) "0"))
2572             (delim (aref (match-string 2) 0)))
2573         (if (or (= delim ?\() (= delim ?\{))
2574             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2575                                    " " number " \""))
2576           (replace-match "\")\""))))
2577     (goto-char (point-max))
2578     (insert "\")")
2579     (goto-char (point-min))
2580     (let ((form (read (current-buffer))))
2581       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2582
2583 (defun gnus-complex-form-to-spec (form spec-alist)
2584   (delq nil
2585         (mapcar
2586          (lambda (sform)
2587            (if (stringp sform)
2588                (gnus-parse-simple-format sform spec-alist t)
2589              (funcall (intern (format "gnus-%s-face-function"
2590                                       (car sform)))
2591                       (gnus-complex-form-to-spec
2592                        (cdr (cdr sform)) spec-alist)
2593                       (nth 1 sform))))
2594          form)))
2595
2596 (defun gnus-parse-simple-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 a
2599   ;; string.
2600   (let ((max-width 0)
2601         spec flist fstring newspec elem beg result dontinsert)
2602     (save-excursion
2603       (gnus-set-work-buffer)
2604       (insert format)
2605       (goto-char (point-min))
2606       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2607                                 nil t)
2608         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2609               (setq newspec "%"
2610                     beg (1+ (match-beginning 0)))
2611           ;; First check if there are any specs that look anything like
2612           ;; "%12,12A", ie. with a "max width specification".  These have
2613           ;; to be treated specially.
2614           (if (setq beg (match-beginning 1))
2615               (setq max-width
2616                     (string-to-int
2617                      (buffer-substring
2618                       (1+ (match-beginning 1)) (match-end 1))))
2619             (setq max-width 0)
2620             (setq beg (match-beginning 2)))
2621           ;; Find the specification from `spec-alist'.
2622           (unless (setq elem (cdr (assq spec spec-alist)))
2623             (setq elem '("*" ?s)))
2624           ;; Treat user defined format specifiers specially.
2625           (when (eq (car elem) 'gnus-tmp-user-defined)
2626             (setq elem
2627                   (list
2628                    (list (intern (concat "gnus-user-format-function-"
2629                                          (match-string 3)))
2630                          'gnus-tmp-header) ?s))
2631             (delete-region (match-beginning 3) (match-end 3)))
2632           (if (not (zerop max-width))
2633               (let ((el (car elem)))
2634                 (cond ((= (car (cdr elem)) ?c)
2635                        (setq el (list 'char-to-string el)))
2636                       ((= (car (cdr elem)) ?d)
2637                        (setq el (list 'int-to-string el))))
2638                 (setq flist (cons (gnus-max-width-function el max-width)
2639                                   flist))
2640                 (setq newspec ?s))
2641             (progn
2642               (setq flist (cons (car elem) flist))
2643               (setq newspec (car (cdr elem))))))
2644         ;; Remove the old specification (and possibly a ",12" string).
2645         (delete-region beg (match-end 2))
2646         ;; Insert the new specification.
2647         (goto-char beg)
2648         (insert newspec))
2649       (setq fstring (buffer-substring 1 (point-max))))
2650     ;; Do some postprocessing to increase efficiency.
2651     (setq
2652      result
2653      (cond
2654       ;; Emptyness.
2655       ((string= fstring "")
2656        nil)
2657       ;; Not a format string.
2658       ((not (string-match "%" fstring))
2659        (list fstring))
2660       ;; A format string with just a single string spec.
2661       ((string= fstring "%s")
2662        (list (car flist)))
2663       ;; A single character.
2664       ((string= fstring "%c")
2665        (list (car flist)))
2666       ;; A single number.
2667       ((string= fstring "%d")
2668        (setq dontinsert)
2669        (if insert
2670            (list `(princ ,(car flist)))
2671          (list `(int-to-string ,(car flist)))))
2672       ;; Just lots of chars and strings.
2673       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2674        (nreverse flist))
2675       ;; A single string spec at the beginning of the spec.
2676       ((string-match "\\`%[sc][^%]+\\'" fstring)
2677        (list (car flist) (substring fstring 2)))
2678       ;; A single string spec in the middle of the spec.
2679       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2680        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2681       ;; A single string spec in the end of the spec.
2682       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2683        (list (match-string 1 fstring) (car flist)))
2684       ;; A more complex spec.
2685       (t
2686        (list (cons 'format (cons fstring (nreverse flist)))))))
2687
2688     (if insert
2689         (when result
2690           (if dontinsert
2691               result
2692             (cons 'insert result)))
2693       (cond ((stringp result)
2694              result)
2695             ((consp result)
2696              (cons 'concat result))
2697             (t "")))))
2698
2699 (defun gnus-eval-format (format &optional alist props)
2700   "Eval the format variable FORMAT, using ALIST.
2701 If PROPS, insert the result."
2702   (let ((form (gnus-parse-format format alist props)))
2703     (if props
2704         (add-text-properties (point) (progn (eval form) (point)) props)
2705       (eval form))))
2706
2707 (defun gnus-remove-text-with-property (prop)
2708   "Delete all text in the current buffer with text property PROP."
2709   (save-excursion
2710     (goto-char (point-min))
2711     (while (not (eobp))
2712       (while (get-text-property (point) prop)
2713         (delete-char 1))
2714       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2715
2716 (defun gnus-set-work-buffer ()
2717   (if (get-buffer gnus-work-buffer)
2718       (progn
2719         (set-buffer gnus-work-buffer)
2720         (erase-buffer))
2721     (set-buffer (get-buffer-create gnus-work-buffer))
2722     (kill-all-local-variables)
2723     (buffer-disable-undo (current-buffer))
2724     (gnus-add-current-to-buffer-list)))
2725
2726 ;; Article file names when saving.
2727
2728 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2729   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2730 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2731 Otherwise, it is like ~/News/news/group/num."
2732   (let ((default
2733           (expand-file-name
2734            (concat (if (gnus-use-long-file-name 'not-save)
2735                        (gnus-capitalize-newsgroup newsgroup)
2736                      (gnus-newsgroup-directory-form newsgroup))
2737                    "/" (int-to-string (mail-header-number headers)))
2738            (or gnus-article-save-directory "~/News"))))
2739     (if (and last-file
2740              (string-equal (file-name-directory default)
2741                            (file-name-directory last-file))
2742              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2743         default
2744       (or last-file default))))
2745
2746 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2747   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2748 If variable `gnus-use-long-file-name' is non-nil, it is
2749 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2750   (let ((default
2751           (expand-file-name
2752            (concat (if (gnus-use-long-file-name 'not-save)
2753                        newsgroup
2754                      (gnus-newsgroup-directory-form newsgroup))
2755                    "/" (int-to-string (mail-header-number headers)))
2756            (or gnus-article-save-directory "~/News"))))
2757     (if (and last-file
2758              (string-equal (file-name-directory default)
2759                            (file-name-directory last-file))
2760              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2761         default
2762       (or last-file default))))
2763
2764 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2765   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2766 If variable `gnus-use-long-file-name' is non-nil, it is
2767 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2768   (or last-file
2769       (expand-file-name
2770        (if (gnus-use-long-file-name 'not-save)
2771            (gnus-capitalize-newsgroup newsgroup)
2772          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2773        (or gnus-article-save-directory "~/News"))))
2774
2775 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2776   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2777 If variable `gnus-use-long-file-name' is non-nil, it is
2778 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2779   (or last-file
2780       (expand-file-name
2781        (if (gnus-use-long-file-name 'not-save)
2782            newsgroup
2783          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2784        (or gnus-article-save-directory "~/News"))))
2785
2786 ;; For subscribing new newsgroup
2787
2788 (defun gnus-subscribe-hierarchical-interactive (groups)
2789   (let ((groups (sort groups 'string<))
2790         prefixes prefix start ans group starts)
2791     (while groups
2792       (setq prefixes (list "^"))
2793       (while (and groups prefixes)
2794         (while (not (string-match (car prefixes) (car groups)))
2795           (setq prefixes (cdr prefixes)))
2796         (setq prefix (car prefixes))
2797         (setq start (1- (length prefix)))
2798         (if (and (string-match "[^\\.]\\." (car groups) start)
2799                  (cdr groups)
2800                  (setq prefix
2801                        (concat "^" (substring (car groups) 0 (match-end 0))))
2802                  (string-match prefix (car (cdr groups))))
2803             (progn
2804               (setq prefixes (cons prefix prefixes))
2805               (message "Descend hierarchy %s? ([y]nsq): "
2806                        (substring prefix 1 (1- (length prefix))))
2807               (setq ans (read-char))
2808               (cond ((= ans ?n)
2809                      (while (and groups
2810                                  (string-match prefix
2811                                                (setq group (car groups))))
2812                        (setq gnus-killed-list
2813                              (cons group gnus-killed-list))
2814                        (gnus-sethash group group gnus-killed-hashtb)
2815                        (setq groups (cdr groups)))
2816                      (setq starts (cdr starts)))
2817                     ((= ans ?s)
2818                      (while (and groups
2819                                  (string-match prefix
2820                                                (setq group (car groups))))
2821                        (gnus-sethash group group gnus-killed-hashtb)
2822                        (gnus-subscribe-alphabetically (car groups))
2823                        (setq groups (cdr groups)))
2824                      (setq starts (cdr starts)))
2825                     ((= ans ?q)
2826                      (while groups
2827                        (setq group (car groups))
2828                        (setq gnus-killed-list (cons group gnus-killed-list))
2829                        (gnus-sethash group group gnus-killed-hashtb)
2830                        (setq groups (cdr groups))))
2831                     (t nil)))
2832           (message "Subscribe %s? ([n]yq)" (car groups))
2833           (setq ans (read-char))
2834           (setq group (car groups))
2835           (cond ((= ans ?y)
2836                  (gnus-subscribe-alphabetically (car groups))
2837                  (gnus-sethash group group gnus-killed-hashtb))
2838                 ((= ans ?q)
2839                  (while groups
2840                    (setq group (car groups))
2841                    (setq gnus-killed-list (cons group gnus-killed-list))
2842                    (gnus-sethash group group gnus-killed-hashtb)
2843                    (setq groups (cdr groups))))
2844                 (t
2845                  (setq gnus-killed-list (cons group gnus-killed-list))
2846                  (gnus-sethash group group gnus-killed-hashtb)))
2847           (setq groups (cdr groups)))))))
2848
2849 (defun gnus-subscribe-randomly (newsgroup)
2850   "Subscribe new NEWSGROUP by making it the first newsgroup."
2851   (gnus-subscribe-newsgroup newsgroup))
2852
2853 (defun gnus-subscribe-alphabetically (newgroup)
2854   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2855   (let ((groups (cdr gnus-newsrc-alist))
2856         before)
2857     (while (and (not before) groups)
2858       (if (string< newgroup (car (car groups)))
2859           (setq before (car (car groups)))
2860         (setq groups (cdr groups))))
2861     (gnus-subscribe-newsgroup newgroup before)))
2862
2863 (defun gnus-subscribe-hierarchically (newgroup)
2864   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2865   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2866   (save-excursion
2867     (set-buffer (find-file-noselect gnus-current-startup-file))
2868     (let ((groupkey newgroup)
2869           before)
2870       (while (and (not before) groupkey)
2871         (goto-char (point-min))
2872         (let ((groupkey-re
2873                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2874           (while (and (re-search-forward groupkey-re nil t)
2875                       (progn
2876                         (setq before (match-string 1))
2877                         (string< before newgroup)))))
2878         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2879         (setq groupkey
2880               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2881                   (substring groupkey (match-beginning 1) (match-end 1)))))
2882       (gnus-subscribe-newsgroup newgroup before))))
2883
2884 (defun gnus-subscribe-interactively (group)
2885   "Subscribe the new GROUP interactively.
2886 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2887 it is killed."
2888   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2889       (gnus-subscribe-hierarchically group)
2890     (push group gnus-killed-list)))
2891
2892 (defun gnus-subscribe-zombies (group)
2893   "Make the new GROUP into a zombie group."
2894   (push group gnus-zombie-list))
2895
2896 (defun gnus-subscribe-killed (group)
2897   "Make the new GROUP a killed group."
2898   (push group gnus-killed-list))
2899
2900 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2901   "Subscribe new NEWSGROUP.
2902 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2903 the first newsgroup."
2904   ;; We subscribe the group by changing its level to `subscribed'.
2905   (gnus-group-change-level
2906    newsgroup gnus-level-default-subscribed
2907    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2908   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2909
2910 ;; For directories
2911
2912 (defun gnus-newsgroup-directory-form (newsgroup)
2913   "Make hierarchical directory name from NEWSGROUP name."
2914   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2915         (len (length newsgroup))
2916         idx)
2917     ;; If this is a foreign group, we don't want to translate the
2918     ;; entire name.
2919     (if (setq idx (string-match ":" newsgroup))
2920         (aset newsgroup idx ?/)
2921       (setq idx 0))
2922     ;; Replace all occurrences of `.' with `/'.
2923     (while (< idx len)
2924       (if (= (aref newsgroup idx) ?.)
2925           (aset newsgroup idx ?/))
2926       (setq idx (1+ idx)))
2927     newsgroup))
2928
2929 (defun gnus-newsgroup-savable-name (group)
2930   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2931   ;; with dots.
2932   (nnheader-replace-chars-in-string group ?/ ?.))
2933
2934 (defun gnus-make-directory (dir)
2935   "Make DIRECTORY recursively."
2936   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2937   ;; of the many mysteries of the universe.
2938   (let* ((dir (expand-file-name dir default-directory))
2939          dirs err)
2940     (if (string-match "/$" dir)
2941         (setq dir (substring dir 0 (match-beginning 0))))
2942     ;; First go down the path until we find a directory that exists.
2943     (while (not (file-exists-p dir))
2944       (setq dirs (cons dir dirs))
2945       (string-match "/[^/]+$" dir)
2946       (setq dir (substring dir 0 (match-beginning 0))))
2947     ;; Then create all the subdirs.
2948     (while (and dirs (not err))
2949       (condition-case ()
2950           (make-directory (car dirs))
2951         (error (setq err t)))
2952       (setq dirs (cdr dirs)))
2953     ;; We return whether we were successful or not.
2954     (not dirs)))
2955
2956 (defun gnus-capitalize-newsgroup (newsgroup)
2957   "Capitalize NEWSGROUP name."
2958   (and (not (zerop (length newsgroup)))
2959        (concat (char-to-string (upcase (aref newsgroup 0)))
2960                (substring newsgroup 1))))
2961
2962 ;; Various... things.
2963
2964 (defun gnus-simplify-subject (subject &optional re-only)
2965   "Remove `Re:' and words in parentheses.
2966 If RE-ONLY is non-nil, strip leading `Re:'s only."
2967   (let ((case-fold-search t))           ;Ignore case.
2968     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
2969     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
2970       (setq subject (substring subject (match-end 0))))
2971     ;; Remove uninteresting prefixes.
2972     (if (and (not re-only)
2973              gnus-simplify-ignored-prefixes
2974              (string-match gnus-simplify-ignored-prefixes subject))
2975         (setq subject (substring subject (match-end 0))))
2976     ;; Remove words in parentheses from end.
2977     (unless re-only
2978       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2979         (setq subject (substring subject 0 (match-beginning 0)))))
2980     ;; Return subject string.
2981     subject))
2982
2983 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2984 ;; all whitespace.
2985 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2986 (defun gnus-simplify-buffer-fuzzy ()
2987   (goto-char (point-min))
2988   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
2989   (goto-char (match-beginning 0))
2990   (while (or
2991           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
2992           (looking-at "^[[].*:[ \t].*[]]$"))
2993     (goto-char (point-min))
2994     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2995                               nil t)
2996       (replace-match "" t t))
2997     (goto-char (point-min))
2998     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
2999       (goto-char (match-end 0))
3000       (delete-char -1)
3001       (delete-region
3002        (progn (goto-char (match-beginning 0)))
3003        (re-search-forward ":"))))
3004   (goto-char (point-min))
3005   (while (re-search-forward "[ \t\n]*[[{(][^()]*[]})][ \t]*$" nil t)
3006     (replace-match "" t t))
3007   (goto-char (point-min))
3008   (while (re-search-forward "[ \t]+" nil t)
3009     (replace-match " " t t))
3010   (goto-char (point-min))
3011   (while (re-search-forward "[ \t]+$" nil t)
3012     (replace-match "" t t))
3013   (goto-char (point-min))
3014   (while (re-search-forward "^[ \t]+" nil t)
3015     (replace-match "" t t))
3016   (goto-char (point-min))
3017   (if gnus-simplify-subject-fuzzy-regexp
3018       (if (listp gnus-simplify-subject-fuzzy-regexp)
3019           (let ((list gnus-simplify-subject-fuzzy-regexp))
3020             (while list
3021               (goto-char (point-min))
3022               (while (re-search-forward (car list) nil t)
3023                 (replace-match "" t t))
3024               (setq list (cdr list))))
3025         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3026           (replace-match "" t t)))))
3027
3028 (defun gnus-simplify-subject-fuzzy (subject)
3029   "Siplify a subject string fuzzily."
3030   (save-excursion
3031     (gnus-set-work-buffer)
3032     (let ((case-fold-search t))
3033       (insert subject)
3034       (inline (gnus-simplify-buffer-fuzzy))
3035       (buffer-string))))
3036
3037 ;; Add the current buffer to the list of buffers to be killed on exit.
3038 (defun gnus-add-current-to-buffer-list ()
3039   (or (memq (current-buffer) gnus-buffer-list)
3040       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3041
3042 (defun gnus-string> (s1 s2)
3043   (not (or (string< s1 s2)
3044            (string= s1 s2))))
3045
3046 ;;; General various misc type functions.
3047
3048 (defun gnus-clear-system ()
3049   "Clear all variables and buffers."
3050   ;; Clear Gnus variables.
3051   (let ((variables gnus-variable-list))
3052     (while variables
3053       (set (car variables) nil)
3054       (setq variables (cdr variables))))
3055   ;; Clear other internal variables.
3056   (setq gnus-list-of-killed-groups nil
3057         gnus-have-read-active-file nil
3058         gnus-newsrc-alist nil
3059         gnus-newsrc-hashtb nil
3060         gnus-killed-list nil
3061         gnus-zombie-list nil
3062         gnus-killed-hashtb nil
3063         gnus-active-hashtb nil
3064         gnus-moderated-list nil
3065         gnus-description-hashtb nil
3066         gnus-newsgroup-headers nil
3067         gnus-newsgroup-name nil
3068         gnus-server-alist nil
3069         gnus-opened-servers nil
3070         gnus-current-select-method nil)
3071   ;; Reset any score variables.
3072   (and gnus-use-scoring (gnus-score-close))
3073   ;; Kill the startup file.
3074   (and gnus-current-startup-file
3075        (get-file-buffer gnus-current-startup-file)
3076        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3077   ;; Save any cache buffers.
3078   (and gnus-use-cache (gnus-cache-save-buffers))
3079   ;; Clear the dribble buffer.
3080   (gnus-dribble-clear)
3081   ;; Close down NoCeM.
3082   (and gnus-use-nocem (gnus-nocem-close))
3083   ;; Shut down the demons.
3084   (and gnus-use-demon (gnus-demon-cancel))
3085   ;; Kill global KILL file buffer.
3086   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
3087       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3088   (gnus-kill-buffer nntp-server-buffer)
3089   ;; Backlog.
3090   (and gnus-keep-backlog (gnus-backlog-shutdown))
3091   ;; Kill Gnus buffers.
3092   (while gnus-buffer-list
3093     (gnus-kill-buffer (car gnus-buffer-list))
3094     (setq gnus-buffer-list (cdr gnus-buffer-list))))
3095
3096 (defun gnus-windows-old-to-new (setting)
3097   ;; First we take care of the really, really old Gnus 3 actions.
3098   (if (symbolp setting)
3099       (setq setting
3100             (cond ((memq setting '(SelectArticle))
3101                    'article)
3102                   ((memq setting '(SelectSubject ExpandSubject))
3103                    'summary)
3104                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
3105                    'group)
3106                   (t setting))))
3107   (if (or (listp setting)
3108           (not (and gnus-window-configuration
3109                     (memq setting '(group summary article)))))
3110       setting
3111     (let* ((setting (if (eq setting 'group)
3112                         (if (assq 'newsgroup gnus-window-configuration)
3113                             'newsgroup
3114                           'newsgroups) setting))
3115            (elem (car (cdr (assq setting gnus-window-configuration))))
3116            (total (apply '+ elem))
3117            (types '(group summary article))
3118            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3119            (i 0)
3120            perc
3121            out)
3122       (while (< i 3)
3123         (or (not (numberp (nth i elem)))
3124             (zerop (nth i elem))
3125             (progn
3126               (setq perc  (/ (float (nth 0 elem)) total))
3127               (setq out (cons (if (eq pbuf (nth i types))
3128                                   (vector (nth i types) perc 'point)
3129                                 (vector (nth i types) perc))
3130                               out))))
3131         (setq i (1+ i)))
3132       (list (nreverse out)))))
3133
3134 (defun gnus-add-configuration (conf)
3135   "Add the window configuration CONF to `gnus-buffer-configuration'."
3136   (setq gnus-buffer-configuration
3137         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3138                          gnus-buffer-configuration))))
3139
3140 (defvar gnus-frame-list nil)
3141
3142 (defun gnus-configure-frame (split &optional window)
3143   "Split WINDOW according to SPLIT."
3144   (unless window
3145     (setq window (get-buffer-window (current-buffer))))
3146   (select-window window)
3147   ;; This might be an old-stylee buffer config.
3148   (when (vectorp split)
3149     (setq split (append split nil)))
3150   (when (or (consp (car split))
3151             (vectorp (car split)))
3152     (push 1.0 split)
3153     (push 'vertical split))
3154   ;; The SPLIT might be something that is to be evaled to
3155   ;; return a new SPLIT.
3156   (while (and (not (assq (car split) gnus-window-to-buffer))
3157               (gnus-functionp (car split)))
3158     (setq split (eval split)))
3159   (let* ((type (car split))
3160          (subs (cddr split))
3161          (len (if (eq type 'horizontal) (window-width) (window-height)))
3162          (total 0)
3163          (window-min-width (or gnus-window-min-width window-min-width))
3164          (window-min-height (or gnus-window-min-height window-min-height))
3165          s result new-win rest comp-subs size sub)
3166     (cond
3167      ;; Nothing to do here.
3168      ((null split))
3169      ;; Don't switch buffers.
3170      ((null type)
3171       (and (memq 'point split) window))
3172      ;; This is a buffer to be selected.
3173      ((not (memq type '(frame horizontal vertical)))
3174       (let ((buffer (cond ((stringp type) type)
3175                           (t (cdr (assq type gnus-window-to-buffer)))))
3176             buf)
3177         (unless buffer
3178           (error "Illegal buffer type: %s" type))
3179         (unless (setq buf (get-buffer (if (symbolp buffer)
3180                                           (symbol-value buffer) buffer)))
3181           (setq buf (get-buffer-create (if (symbolp buffer)
3182                                            (symbol-value buffer) buffer))))
3183         (switch-to-buffer buf)
3184         ;; We return the window if it has the `point' spec.
3185         (and (memq 'point split) window)))
3186      ;; This is a frame split.
3187      ((eq type 'frame)
3188       (unless gnus-frame-list
3189         (setq gnus-frame-list (list (window-frame
3190                                      (get-buffer-window (current-buffer))))))
3191       (let ((i 0)
3192             params frame fresult)
3193         (while (< i (length subs))
3194           ;; Frame parameter is gotten from the sub-split.
3195           (setq params (cadr (elt subs i)))
3196           ;; It should be a list.
3197           (unless (listp params)
3198             (setq params nil))
3199           ;; Create a new frame?
3200           (unless (setq frame (elt gnus-frame-list i))
3201             (nconc gnus-frame-list (list (setq frame (make-frame params)))))
3202           ;; Is the old frame still alive?
3203           (unless (frame-live-p frame)
3204             (setcar (nthcdr i gnus-frame-list)
3205                     (setq frame (make-frame params))))
3206           ;; Select the frame in question and do more splits there.
3207           (select-frame frame)
3208           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3209           (incf i))
3210         ;; Select the frame that has the selected buffer.
3211         (when fresult
3212           (select-frame (window-frame fresult)))))
3213      ;; This is a normal split.
3214      (t
3215       (when (> (length subs) 0)
3216         ;; First we have to compute the sizes of all new windows.
3217         (while subs
3218           (setq sub (append (pop subs) nil))
3219           (while (and (not (assq (car sub) gnus-window-to-buffer))
3220                       (gnus-functionp (car sub)))
3221             (setq sub (eval sub)))
3222           (when sub
3223             (push sub comp-subs)
3224             (setq size (cadar comp-subs))
3225             (cond ((equal size 1.0)
3226                    (setq rest (car comp-subs))
3227                    (setq s 0))
3228                   ((floatp size)
3229                    (setq s (floor (* size len))))
3230                   ((integerp size)
3231                    (setq s size))
3232                   (t
3233                    (error "Illegal size: %s" size)))
3234             ;; Try to make sure that we are inside the safe limits.
3235             (cond ((zerop s))
3236                   ((eq type 'horizontal)
3237                    (setq s (max s window-min-width)))
3238                   ((eq type 'vertical)
3239                    (setq s (max s window-min-height))))
3240             (setcar (cdar comp-subs) s)
3241             (incf total s)))
3242         ;; Take care of the "1.0" spec.
3243         (if rest
3244             (setcar (cdr rest) (- len total))
3245           (error "No 1.0 specs in %s" split))
3246         ;; The we do the actual splitting in a nice recursive
3247         ;; fashion.
3248         (setq comp-subs (nreverse comp-subs))
3249         (while comp-subs
3250           (if (null (cdr comp-subs))
3251               (setq new-win window)
3252             (setq new-win
3253                   (split-window window (cadar comp-subs)
3254                                 (eq type 'horizontal))))
3255           (setq result (or (gnus-configure-frame
3256                             (car comp-subs) window) result))
3257           (select-window new-win)
3258           (setq window new-win)
3259           (setq comp-subs (cdr comp-subs))))
3260       ;; Return the proper window, if any.
3261       (when result
3262         (select-window result))))))
3263
3264 (defvar gnus-frame-split-p nil)
3265
3266 (defun gnus-configure-windows (setting &optional force)
3267   (setq setting (gnus-windows-old-to-new setting))
3268   (let ((split (if (symbolp setting)
3269                    (car (cdr (assq setting gnus-buffer-configuration)))
3270                  setting))
3271         (in-buf (current-buffer))
3272         rule val w height hor ohor heights sub jump-buffer
3273         rel total to-buf all-visible)
3274
3275     (setq gnus-frame-split-p nil)
3276
3277     (unless split
3278       (error "No such setting: %s" setting))
3279
3280     (if (and (not force)
3281              (setq all-visible (gnus-all-windows-visible-p split)))
3282         ;; All the windows mentioned are already visible, so we just
3283         ;; put point in the assigned buffer, and do not touch the
3284         ;; winconf.
3285         (select-window all-visible)
3286
3287       ;; Either remove all windows or just remove all Gnus windows.
3288       (let ((frame (selected-frame)))
3289         (unwind-protect
3290             (if gnus-use-full-window
3291                 ;; We want to remove all other windows.
3292                 (if (not gnus-frame-split-p)
3293                     ;; This is not a `frame' split, so we ignore the
3294                     ;; other frames.  
3295                     (delete-other-windows)
3296                   ;; This is a `frame' split, so we delete all windows
3297                   ;; on all frames.
3298                   (mapcar 
3299                    (lambda (frame)
3300                      (unless (eq (cdr (assq 'minibuffer
3301                                             (frame-parameters frame)))
3302                                  'only)
3303                        (select-frame frame)
3304                        (delete-other-windows)))
3305                    (frame-list)))
3306               ;; Just remove some windows.
3307               (gnus-remove-some-windows)
3308               (switch-to-buffer nntp-server-buffer))
3309           (select-frame frame)))
3310
3311       (switch-to-buffer nntp-server-buffer)
3312       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3313
3314 (defun gnus-all-windows-visible-p (split)
3315   (when (vectorp split)
3316     (setq split (append split nil)))
3317   (when (or (consp (car split))
3318             (vectorp (car split)))
3319     (push 1.0 split)
3320     (push 'vertical split))
3321   ;; The SPLIT might be something that is to be evaled to
3322   ;; return a new SPLIT.
3323   (while (and (not (assq (car split) gnus-window-to-buffer))
3324               (gnus-functionp (car split)))
3325     (setq split (eval split)))
3326   (let* ((type (elt split 0)))
3327     (cond
3328      ((null split)
3329       t)
3330      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3331       (let ((buffer (cond ((stringp type) type)
3332                           (t (cdr (assq type gnus-window-to-buffer)))))
3333             win buf)
3334         (unless buffer
3335           (error "Illegal buffer type: %s" type))
3336         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3337                                       buffer)))
3338           (setq win (get-buffer-window buf t)))
3339         (when win
3340           (if (memq 'point split)
3341               win
3342             t))))
3343      (t
3344       (when (eq type 'frame)
3345         (setq gnus-frame-split-p t))
3346       (let ((n (mapcar 'gnus-all-windows-visible-p
3347                        (cdr (cdr split))))
3348             (win t))
3349         (while n
3350           (cond ((windowp (car n))
3351                  (setq win (car n)))
3352                 ((null (car n))
3353                  (setq win nil)))
3354           (setq n (cdr n)))
3355         win)))))
3356
3357 (defun gnus-window-top-edge (&optional window)
3358   (nth 1 (window-edges window)))
3359
3360 (defun gnus-remove-some-windows ()
3361   (let ((buffers gnus-window-to-buffer)
3362         buf bufs lowest-buf lowest)
3363     (save-excursion
3364       ;; Remove windows on all known Gnus buffers.
3365       (while buffers
3366         (setq buf (cdr (car buffers)))
3367         (if (symbolp buf)
3368             (setq buf (and (boundp buf) (symbol-value buf))))
3369         (and buf
3370              (get-buffer-window buf)
3371              (progn
3372                (setq bufs (cons buf bufs))
3373                (pop-to-buffer buf)
3374                (if (or (not lowest)
3375                        (< (gnus-window-top-edge) lowest))
3376                    (progn
3377                      (setq lowest (gnus-window-top-edge))
3378                      (setq lowest-buf buf)))))
3379         (setq buffers (cdr buffers)))
3380       ;; Remove windows on *all* summary buffers.
3381       (let (wins)
3382         (walk-windows
3383          (lambda (win)
3384            (let ((buf (window-buffer win)))
3385              (if (string-match  "^\\*Summary" (buffer-name buf))
3386                  (progn
3387                    (setq bufs (cons buf bufs))
3388                    (pop-to-buffer buf)
3389                    (if (or (not lowest)
3390                            (< (gnus-window-top-edge) lowest))
3391                        (progn
3392                          (setq lowest-buf buf)
3393                          (setq lowest (gnus-window-top-edge))))))))))
3394       (and lowest-buf
3395            (progn
3396              (pop-to-buffer lowest-buf)
3397              (switch-to-buffer nntp-server-buffer)))
3398       (while bufs
3399         (and (not (eq (car bufs) lowest-buf))
3400              (delete-windows-on (car bufs)))
3401         (setq bufs (cdr bufs))))))
3402
3403 (defun gnus-version ()
3404   "Version numbers of this version of Gnus."
3405   (interactive)
3406   (let ((methods gnus-valid-select-methods)
3407         (mess gnus-version)
3408         meth)
3409     ;; Go through all the legal select methods and add their version
3410     ;; numbers to the total version string.  Only the backends that are
3411     ;; currently in use will have their message numbers taken into
3412     ;; consideration.
3413     (while methods
3414       (setq meth (intern (concat (car (car methods)) "-version")))
3415       (and (boundp meth)
3416            (stringp (symbol-value meth))
3417            (setq mess (concat mess "; " (symbol-value meth))))
3418       (setq methods (cdr methods)))
3419     (gnus-message 2 mess)))
3420
3421 (defun gnus-info-find-node ()
3422   "Find Info documentation of Gnus."
3423   (interactive)
3424   ;; Enlarge info window if needed.
3425   (let ((mode major-mode)
3426         gnus-info-buffer)
3427     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))
3428     (setq gnus-info-buffer (current-buffer))
3429     (gnus-configure-windows 'info)))
3430
3431 (defun gnus-days-between (date1 date2)
3432   ;; Return the number of days between date1 and date2.
3433   (- (gnus-day-number date1) (gnus-day-number date2)))
3434
3435 (defun gnus-day-number (date)
3436   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3437                      (timezone-parse-date date))))
3438     (timezone-absolute-from-gregorian
3439      (nth 1 dat) (nth 2 dat) (car dat))))
3440
3441 (defun gnus-encode-date (date)
3442   "Convert DATE to internal time."
3443   (let* ((parse (timezone-parse-date date))
3444          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3445          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3446     (encode-time (caddr time) (cadr time) (car time)
3447                  (caddr date) (cadr date) (car date) (nth 4 date))))
3448
3449 (defun gnus-time-minus (t1 t2)
3450   "Subtract two internal times."
3451   (let ((borrow (< (cadr t1) (cadr t2))))
3452     (list (- (car t1) (car t2) (if borrow 1 0))
3453           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3454
3455 (defun gnus-file-newer-than (file date)
3456   (let ((fdate (nth 5 (file-attributes file))))
3457     (or (> (car fdate) (car date))
3458         (and (= (car fdate) (car date))
3459              (> (nth 1 fdate) (nth 1 date))))))
3460
3461 (defmacro gnus-define-keys (keymap &rest plist)
3462   "Define all keys in PLIST in KEYMAP."
3463   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3464
3465 (defun gnus-define-keys-1 (keymap plist)
3466   (when (null keymap)
3467     (error "Can't set keys in a null keymap"))
3468   (cond ((symbolp keymap)
3469          (setq keymap (symbol-value keymap)))
3470         ((listp keymap)
3471          (set (car keymap) nil)
3472          (define-prefix-command (car keymap))
3473          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3474          (setq keymap (symbol-value (car keymap)))))
3475   (let (key)
3476     (while plist
3477       (when (symbolp (setq key (pop plist)))
3478         (setq key (symbol-value key)))
3479       (define-key keymap key (pop plist)))))
3480
3481 (defun gnus-group-read-only-p (&optional group)
3482   "Check whether GROUP supports editing or not.
3483 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3484 that that variable is buffer-local to the summary buffers."
3485   (let ((group (or group gnus-newsgroup-name)))
3486     (not (gnus-check-backend-function 'request-replace-article group))))
3487
3488 (defun gnus-group-total-expirable-p (group)
3489   "Check whether GROUP is total-expirable or not."
3490   (let ((params (gnus-info-params (gnus-get-info group))))
3491     (or (memq 'total-expire params)
3492         (cdr (assq 'total-expire params)) ; (total-expire . t)
3493         (and gnus-total-expirable-newsgroups ; Check var.
3494              (string-match gnus-total-expirable-newsgroups group)))))
3495
3496 (defun gnus-group-auto-expirable-p (group)
3497   "Check whether GROUP is total-expirable or not."
3498   (let ((params (gnus-info-params (gnus-get-info group))))
3499     (or (memq 'auto-expire params)
3500         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3501         (and gnus-auto-expirable-newsgroups ; Check var.
3502              (string-match gnus-auto-expirable-newsgroups group)))))
3503
3504 (defun gnus-virtual-group-p (group)
3505   "Say whether GROUP is virtual or not."
3506   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3507                         gnus-valid-select-methods)))
3508
3509 (defsubst gnus-simplify-subject-fully (subject)
3510   "Simplify a subject string according to the user's wishes."
3511   (cond
3512    ((null gnus-summary-gather-subject-limit)
3513     (gnus-simplify-subject-re subject))
3514    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3515     (gnus-simplify-subject-fuzzy subject))
3516    ((numberp gnus-summary-gather-subject-limit)
3517     (gnus-limit-string (gnus-simplify-subject-re subject)
3518                        gnus-summary-gather-subject-limit))
3519    (t
3520     subject)))
3521
3522 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3523   "Check whether two subjects are equal.  If optional argument
3524 simple-first is t, first argument is already simplified."
3525   (cond
3526    ((null simple-first)
3527     (equal (gnus-simplify-subject-fully s1)
3528            (gnus-simplify-subject-fully s2)))
3529    (t
3530     (equal s1
3531            (gnus-simplify-subject-fully s2)))))
3532
3533 ;; Returns a list of writable groups.
3534 (defun gnus-writable-groups ()
3535   (let ((alist gnus-newsrc-alist)
3536         groups)
3537     (while alist
3538       (or (gnus-group-read-only-p (car (car alist)))
3539           (setq groups (cons (car (car alist)) groups)))
3540       (setq alist (cdr alist)))
3541     (nreverse groups)))
3542
3543 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3544 ;; the echo area.
3545 (defun gnus-y-or-n-p (prompt)
3546   (prog1
3547       (y-or-n-p prompt)
3548     (message "")))
3549
3550 (defun gnus-yes-or-no-p (prompt)
3551   (prog1
3552       (yes-or-no-p prompt)
3553     (message "")))
3554
3555 ;; Check whether to use long file names.
3556 (defun gnus-use-long-file-name (symbol)
3557   ;; The variable has to be set...
3558   (and gnus-use-long-file-name
3559        ;; If it isn't a list, then we return t.
3560        (or (not (listp gnus-use-long-file-name))
3561            ;; If it is a list, and the list contains `symbol', we
3562            ;; return nil.
3563            (not (memq symbol gnus-use-long-file-name)))))
3564
3565 ;; I suspect there's a better way, but I haven't taken the time to do
3566 ;; it yet. -erik selberg@cs.washington.edu
3567 (defun gnus-dd-mmm (messy-date)
3568   "Return a string like DD-MMM from a big messy string"
3569   (let ((datevec (timezone-parse-date messy-date)))
3570     (format "%2s-%s"
3571             (or (aref datevec 2) "??")
3572             (capitalize
3573              (or (car
3574                   (nth (1- (string-to-number (aref datevec 1)))
3575                        timezone-months-assoc))
3576                  "???")))))
3577
3578 ;; Make a hash table (default and minimum size is 255).
3579 ;; Optional argument HASHSIZE specifies the table size.
3580 (defun gnus-make-hashtable (&optional hashsize)
3581   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3582
3583 ;; Make a number that is suitable for hashing; bigger than MIN and one
3584 ;; less than 2^x.
3585 (defun gnus-create-hash-size (min)
3586   (let ((i 1))
3587     (while (< i min)
3588       (setq i (* 2 i)))
3589     (1- i)))
3590
3591 ;; Show message if message has a lower level than `gnus-verbose'.
3592 ;; Guideline for numbers:
3593 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3594 ;; for things that take a long time, 7 - not very important messages
3595 ;; on stuff, 9 - messages inside loops.
3596 (defun gnus-message (level &rest args)
3597   (if (<= level gnus-verbose)
3598       (apply 'message args)
3599     ;; We have to do this format thingy here even if the result isn't
3600     ;; shown - the return value has to be the same as the return value
3601     ;; from `message'.
3602     (apply 'format args)))
3603
3604 (defun gnus-functionp (form)
3605   "Return non-nil if FORM is funcallable."
3606   (or (and (symbolp form) (fboundp form))
3607       (and (listp form) (eq (car form) 'lambda))))
3608
3609 ;; Generate a unique new group name.
3610 (defun gnus-generate-new-group-name (leaf)
3611   (let ((name leaf)
3612         (num 0))
3613     (while (gnus-gethash name gnus-newsrc-hashtb)
3614       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3615     name))
3616
3617 ;; Find out whether the gnus-visual TYPE is wanted.
3618 (defun gnus-visual-p (&optional type class)
3619   (and gnus-visual                      ; Has to be non-nil, at least.
3620        (if (not type)                   ; We don't care about type.
3621            gnus-visual
3622          (if (listp gnus-visual)        ; It's a list, so we check it.
3623              (or (memq type gnus-visual)
3624                  (memq class gnus-visual))
3625            t))))
3626
3627 (defun gnus-parent-id (references)
3628   "Return the last Message-ID in REFERENCES."
3629   (when (and references
3630              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3631     (substring references (match-beginning 1) (match-end 1))))
3632
3633 (defun gnus-split-references (references)
3634   "Return a list of Message-IDs in REFERENCES."
3635   (let ((beg 0)
3636         ids)
3637     (while (string-match "<[^>]+>" references beg)
3638       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3639             ids))
3640     (nreverse ids)))
3641
3642 (defun gnus-ephemeral-group-p (group)
3643   "Say whether GROUP is ephemeral or not."
3644   (assoc 'quit-config (gnus-find-method-for-group group)))
3645
3646 (defun gnus-group-quit-config (group)
3647   "Return the quit-config of GROUP."
3648   (nth 1 (assoc 'quit-config (gnus-find-method-for-group group))))
3649
3650 (defun gnus-simplify-mode-line ()
3651   "Make mode lines a bit simpler."
3652   (setq mode-line-modified "-- ")
3653   (when (listp mode-line-format)
3654     (make-local-variable 'mode-line-format)
3655     (setq mode-line-format (copy-sequence mode-line-format))
3656     (when (equal (nth 3 mode-line-format) "   ")
3657       (setcar (nthcdr 3 mode-line-format) " "))))
3658
3659 ;;; List and range functions
3660
3661 (defun gnus-last-element (list)
3662   "Return last element of LIST."
3663   (while (cdr list)
3664     (setq list (cdr list)))
3665   (car list))
3666
3667 (defun gnus-copy-sequence (list)
3668   "Do a complete, total copy of a list."
3669   (if (and (consp list) (not (consp (cdr list))))
3670       (cons (car list) (cdr list))
3671     (mapcar (lambda (elem) (if (consp elem)
3672                                (if (consp (cdr elem))
3673                                    (gnus-copy-sequence elem)
3674                                  (cons (car elem) (cdr elem)))
3675                              elem))
3676             list)))
3677
3678 (defun gnus-set-difference (list1 list2)
3679   "Return a list of elements of LIST1 that do not appear in LIST2."
3680   (let ((list1 (copy-sequence list1)))
3681     (while list2
3682       (setq list1 (delq (car list2) list1))
3683       (setq list2 (cdr list2)))
3684     list1))
3685
3686 (defun gnus-sorted-complement (list1 list2)
3687   "Return a list of elements of LIST1 that do not appear in LIST2.
3688 Both lists have to be sorted over <."
3689   (let (out)
3690     (if (or (null list1) (null list2))
3691         (or list1 list2)
3692       (while (and list1 list2)
3693         (cond ((= (car list1) (car list2))
3694                (setq list1 (cdr list1)
3695                      list2 (cdr list2)))
3696               ((< (car list1) (car list2))
3697                (setq out (cons (car list1) out))
3698                (setq list1 (cdr list1)))
3699               (t
3700                (setq out (cons (car list2) out))
3701                (setq list2 (cdr list2)))))
3702       (nconc (nreverse out) (or list1 list2)))))
3703
3704 (defun gnus-intersection (list1 list2)
3705   (let ((result nil))
3706     (while list2
3707       (if (memq (car list2) list1)
3708           (setq result (cons (car list2) result)))
3709       (setq list2 (cdr list2)))
3710     result))
3711
3712 (defun gnus-sorted-intersection (list1 list2)
3713   ;; LIST1 and LIST2 have to be sorted over <.
3714   (let (out)
3715     (while (and list1 list2)
3716       (cond ((= (car list1) (car list2))
3717              (setq out (cons (car list1) out)
3718                    list1 (cdr list1)
3719                    list2 (cdr list2)))
3720             ((< (car list1) (car list2))
3721              (setq list1 (cdr list1)))
3722             (t
3723              (setq list2 (cdr list2)))))
3724     (nreverse out)))
3725
3726 (defun gnus-set-sorted-intersection (list1 list2)
3727   ;; LIST1 and LIST2 have to be sorted over <.
3728   ;; This function modifies LIST1.
3729   (let* ((top (cons nil list1))
3730          (prev top))
3731     (while (and list1 list2)
3732       (cond ((= (car list1) (car list2))
3733              (setq prev list1
3734                    list1 (cdr list1)
3735                    list2 (cdr list2)))
3736             ((< (car list1) (car list2))
3737              (setcdr prev (cdr list1))
3738              (setq list1 (cdr list1)))
3739             (t
3740              (setq list2 (cdr list2)))))
3741     (setcdr prev nil)
3742     (cdr top)))
3743
3744 (defun gnus-compress-sequence (numbers &optional always-list)
3745   "Convert list of numbers to a list of ranges or a single range.
3746 If ALWAYS-LIST is non-nil, this function will always release a list of
3747 ranges."
3748   (let* ((first (car numbers))
3749          (last (car numbers))
3750          result)
3751     (if (null numbers)
3752         nil
3753       (if (not (listp (cdr numbers)))
3754           numbers
3755         (while numbers
3756           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3757                 ((= (1+ last) (car numbers)) ;Still in sequence
3758                  (setq last (car numbers)))
3759                 (t                      ;End of one sequence
3760                  (setq result
3761                        (cons (if (= first last) first
3762                                (cons first last)) result))
3763                  (setq first (car numbers))
3764                  (setq last  (car numbers))))
3765           (setq numbers (cdr numbers)))
3766         (if (and (not always-list) (null result))
3767             (if (= first last) (list first) (cons first last))
3768           (nreverse (cons (if (= first last) first (cons first last))
3769                           result)))))))
3770
3771 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3772 (defun gnus-uncompress-range (ranges)
3773   "Expand a list of ranges into a list of numbers.
3774 RANGES is either a single range on the form `(num . num)' or a list of
3775 these ranges."
3776   (let (first last result)
3777     (cond
3778      ((null ranges)
3779       nil)
3780      ((not (listp (cdr ranges)))
3781       (setq first (car ranges))
3782       (setq last (cdr ranges))
3783       (while (<= first last)
3784         (setq result (cons first result))
3785         (setq first (1+ first)))
3786       (nreverse result))
3787      (t
3788       (while ranges
3789         (if (atom (car ranges))
3790             (if (numberp (car ranges))
3791                 (setq result (cons (car ranges) result)))
3792           (setq first (car (car ranges)))
3793           (setq last  (cdr (car ranges)))
3794           (while (<= first last)
3795             (setq result (cons first result))
3796             (setq first (1+ first))))
3797         (setq ranges (cdr ranges)))
3798       (nreverse result)))))
3799
3800 (defun gnus-add-to-range (ranges list)
3801   "Return a list of ranges that has all articles from both RANGES and LIST.
3802 Note: LIST has to be sorted over `<'."
3803   (if (not ranges)
3804       (gnus-compress-sequence list t)
3805     (setq list (copy-sequence list))
3806     (or (listp (cdr ranges))
3807         (setq ranges (list ranges)))
3808     (let ((out ranges)
3809           ilist lowest highest temp)
3810       (while (and ranges list)
3811         (setq ilist list)
3812         (setq lowest (or (and (atom (car ranges)) (car ranges))
3813                          (car (car ranges))))
3814         (while (and list (cdr list) (< (car (cdr list)) lowest))
3815           (setq list (cdr list)))
3816         (if (< (car ilist) lowest)
3817             (progn
3818               (setq temp list)
3819               (setq list (cdr list))
3820               (setcdr temp nil)
3821               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3822         (setq highest (or (and (atom (car ranges)) (car ranges))
3823                           (cdr (car ranges))))
3824         (while (and list (<= (car list) highest))
3825           (setq list (cdr list)))
3826         (setq ranges (cdr ranges)))
3827       (if list
3828           (setq out (nconc (gnus-compress-sequence list t) out)))
3829       (setq out (sort out (lambda (r1 r2)
3830                             (< (or (and (atom r1) r1) (car r1))
3831                                (or (and (atom r2) r2) (car r2))))))
3832       (setq ranges out)
3833       (while ranges
3834         (if (atom (car ranges))
3835             (if (cdr ranges)
3836                 (if (atom (car (cdr ranges)))
3837                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3838                         (progn
3839                           (setcar ranges (cons (car ranges)
3840                                                (car (cdr ranges))))
3841                           (setcdr ranges (cdr (cdr ranges)))))
3842                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3843                       (progn
3844                         (setcar (car (cdr ranges)) (car ranges))
3845                         (setcar ranges (car (cdr ranges)))
3846                         (setcdr ranges (cdr (cdr ranges)))))))
3847           (if (cdr ranges)
3848               (if (atom (car (cdr ranges)))
3849                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3850                       (progn
3851                         (setcdr (car ranges) (car (cdr ranges)))
3852                         (setcdr ranges (cdr (cdr ranges)))))
3853                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3854                     (progn
3855                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3856                       (setcdr ranges (cdr (cdr ranges))))))))
3857         (setq ranges (cdr ranges)))
3858       out)))
3859
3860 (defun gnus-remove-from-range (ranges list)
3861   "Return a list of ranges that has all articles from LIST removed from RANGES.
3862 Note: LIST has to be sorted over `<'."
3863   ;; !!! This function shouldn't look like this, but I've got a headache.
3864   (gnus-compress-sequence
3865    (gnus-sorted-complement
3866     (gnus-uncompress-range ranges) list)))
3867
3868 (defun gnus-member-of-range (number ranges)
3869   (if (not (listp (cdr ranges)))
3870       (and (>= number (car ranges))
3871            (<= number (cdr ranges)))
3872     (let ((not-stop t))
3873       (while (and ranges
3874                   (if (numberp (car ranges))
3875                       (>= number (car ranges))
3876                     (>= number (car (car ranges))))
3877                   not-stop)
3878         (if (if (numberp (car ranges))
3879                 (= number (car ranges))
3880               (and (>= number (car (car ranges)))
3881                    (<= number (cdr (car ranges)))))
3882             (setq not-stop nil))
3883         (setq ranges (cdr ranges)))
3884       (not not-stop))))
3885
3886 (defun gnus-range-length (range)
3887   "Return the length RANGE would have if uncompressed."
3888   (length (gnus-uncompress-range range)))
3889
3890 (defun gnus-sublist-p (list sublist)
3891   "Test whether all elements in SUBLIST are members of LIST."
3892   (let ((sublistp t))
3893     (while sublist
3894       (unless (memq (pop sublist) list)
3895         (setq sublistp nil
3896               sublist nil)))
3897     sublistp))
3898
3899 \f
3900 ;;;
3901 ;;; Gnus group mode
3902 ;;;
3903
3904 (defvar gnus-group-mode-map nil)
3905 (put 'gnus-group-mode 'mode-class 'special)
3906
3907 (unless gnus-group-mode-map
3908   (setq gnus-group-mode-map (make-keymap))
3909   (suppress-keymap gnus-group-mode-map)
3910
3911   (gnus-define-keys
3912    gnus-group-mode-map
3913    " " gnus-group-read-group
3914    "=" gnus-group-select-group
3915    "\M- " gnus-group-unhidden-select-group
3916    "\r" gnus-group-select-group
3917    "\M-\r" gnus-group-quick-select-group
3918    "j" gnus-group-jump-to-group
3919    "n" gnus-group-next-unread-group
3920    "p" gnus-group-prev-unread-group
3921    "\177" gnus-group-prev-unread-group
3922    "N" gnus-group-next-group
3923    "P" gnus-group-prev-group
3924    "\M-n" gnus-group-next-unread-group-same-level
3925    "\M-p" gnus-group-prev-unread-group-same-level
3926    "," gnus-group-best-unread-group
3927    "." gnus-group-first-unread-group
3928    "u" gnus-group-unsubscribe-current-group
3929    "U" gnus-group-unsubscribe-group
3930    "c" gnus-group-catchup-current
3931    "C" gnus-group-catchup-current-all
3932    "l" gnus-group-list-groups
3933    "L" gnus-group-list-all-groups
3934    "m" gnus-group-mail
3935    "g" gnus-group-get-new-news
3936    "\M-g" gnus-group-get-new-news-this-group
3937    "R" gnus-group-restart
3938    "r" gnus-group-read-init-file
3939    "B" gnus-group-browse-foreign-server
3940    "b" gnus-group-check-bogus-groups
3941    "F" gnus-find-new-newsgroups
3942    "\C-c\C-d" gnus-group-describe-group
3943    "\M-d" gnus-group-describe-all-groups
3944    "\C-c\C-a" gnus-group-apropos
3945    "\C-c\M-\C-a" gnus-group-description-apropos
3946    "a" gnus-group-post-news
3947    "\ek" gnus-group-edit-local-kill
3948    "\eK" gnus-group-edit-global-kill
3949    "\C-k" gnus-group-kill-group
3950    "\C-y" gnus-group-yank-group
3951    "\C-w" gnus-group-kill-region
3952    "\C-x\C-t" gnus-group-transpose-groups
3953    "\C-c\C-l" gnus-group-list-killed
3954    "\C-c\C-x" gnus-group-expire-articles
3955    "\C-c\M-\C-x" gnus-group-expire-all-groups
3956    "V" gnus-version
3957    "s" gnus-group-save-newsrc
3958    "z" gnus-group-suspend
3959    "Z" gnus-group-clear-dribble
3960    "q" gnus-group-exit
3961    "Q" gnus-group-quit
3962    "?" gnus-group-describe-briefly
3963    "\C-c\C-i" gnus-info-find-node
3964    "\M-e" gnus-group-edit-group-method
3965    "^" gnus-group-enter-server-mode
3966    gnus-mouse-2 gnus-mouse-pick-group
3967    "<" beginning-of-buffer
3968    ">" end-of-buffer
3969    "\C-c\C-b" gnus-bug
3970    "\C-c\C-s" gnus-group-sort-groups
3971    "t" gnus-topic-mode
3972    "\C-c\M-g" gnus-activate-all-groups
3973    "\M-&" gnus-group-universal-argument
3974    "#" gnus-group-mark-group
3975    "\M-#" gnus-group-unmark-group)
3976
3977   (gnus-define-keys
3978    (gnus-group-mark-map "M" gnus-group-mode-map)
3979    "m" gnus-group-mark-group
3980    "u" gnus-group-unmark-group
3981    "w" gnus-group-mark-region
3982    "m" gnus-group-mark-buffer
3983    "r" gnus-group-mark-regexp
3984    "U" gnus-group-unmark-all-groups)
3985
3986   (gnus-define-keys
3987    (gnus-group-group-map "G" gnus-group-mode-map)
3988    "d" gnus-group-make-directory-group
3989    "h" gnus-group-make-help-group
3990    "a" gnus-group-make-archive-group
3991    "k" gnus-group-make-kiboze-group
3992    "m" gnus-group-make-group
3993    "E" gnus-group-edit-group
3994    "e" gnus-group-edit-group-method
3995    "p" gnus-group-edit-group-parameters
3996    "v" gnus-group-add-to-virtual
3997    "V" gnus-group-make-empty-virtual
3998    "D" gnus-group-enter-directory
3999    "f" gnus-group-make-doc-group
4000    "r" gnus-group-rename-group
4001    "\177" gnus-group-delete-group)
4002
4003    (gnus-define-keys
4004     (gnus-group-soup-map "s" gnus-group-group-map)
4005     "b" gnus-group-brew-soup
4006     "w" gnus-soup-save-areas
4007     "s" gnus-soup-send-replies
4008     "p" gnus-soup-pack-packet
4009     "r" nnsoup-pack-replies)
4010
4011    (gnus-define-keys
4012     (gnus-group-sort-map "S" gnus-group-group-map)
4013     "s" gnus-group-sort-groups
4014     "a" gnus-group-sort-groups-by-alphabet
4015     "u" gnus-group-sort-groups-by-unread
4016     "l" gnus-group-sort-groups-by-level
4017     "v" gnus-group-sort-groups-by-score
4018     "r" gnus-group-sort-groups-by-rank
4019     "m" gnus-group-sort-groups-by-method)
4020
4021    (gnus-define-keys
4022     (gnus-group-list-map "A" gnus-group-mode-map)
4023     "k" gnus-group-list-killed
4024     "z" gnus-group-list-zombies
4025     "s" gnus-group-list-groups
4026     "u" gnus-group-list-all-groups
4027     "A" gnus-group-list-active
4028     "a" gnus-group-apropos
4029     "d" gnus-group-description-apropos
4030     "m" gnus-group-list-matching
4031     "M" gnus-group-list-all-matching
4032     "l" gnus-group-list-level)
4033
4034    (gnus-define-keys
4035     (gnus-group-score-map "W" gnus-group-mode-map)
4036     "f" gnus-score-flush-cache)
4037
4038    (gnus-define-keys
4039     (gnus-group-help-map "H" gnus-group-mode-map)
4040     "f" gnus-group-fetch-faq)
4041
4042    (gnus-define-keys
4043     (gnus-group-sub-map "S" gnus-group-mode-map)
4044     "l" gnus-group-set-current-level
4045     "t" gnus-group-unsubscribe-current-group
4046     "s" gnus-group-unsubscribe-group
4047     "k" gnus-group-kill-group
4048     "y" gnus-group-yank-group
4049     "w" gnus-group-kill-region
4050     "\C-k" gnus-group-kill-level
4051     "z" gnus-group-kill-all-zombies))
4052
4053 (defun gnus-group-mode ()
4054   "Major mode for reading news.
4055
4056 All normal editing commands are switched off.
4057 \\<gnus-group-mode-map>
4058 The group buffer lists (some of) the groups available.  For instance,
4059 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4060 lists all zombie groups.
4061
4062 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4063 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4064
4065 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4066
4067 The following commands are available:
4068
4069 \\{gnus-group-mode-map}"
4070   (interactive)
4071   (when (and menu-bar-mode
4072              (gnus-visual-p 'group-menu 'menu))
4073     (gnus-group-make-menu-bar))
4074   (kill-all-local-variables)
4075   (gnus-simplify-mode-line)
4076   (setq major-mode 'gnus-group-mode)
4077   (setq mode-name "Group")
4078   (gnus-group-set-mode-line)
4079   (setq mode-line-process nil)
4080   (use-local-map gnus-group-mode-map)
4081   (buffer-disable-undo (current-buffer))
4082   (setq truncate-lines t)
4083   (setq buffer-read-only t)
4084   (run-hooks 'gnus-group-mode-hook))
4085
4086 (defun gnus-mouse-pick-group (e)
4087   "Enter the group under the mouse pointer."
4088   (interactive "e")
4089   (mouse-set-point e)
4090   (gnus-group-read-group nil))
4091
4092 ;; Look at LEVEL and find out what the level is really supposed to be.
4093 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4094 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4095 (defun gnus-group-default-level (&optional level number-or-nil)
4096   (cond
4097    (gnus-group-use-permanent-levels
4098     (setq gnus-group-default-list-level
4099           (or level gnus-group-default-list-level))
4100     (or gnus-group-default-list-level gnus-level-subscribed))
4101    (number-or-nil
4102     level)
4103    (t
4104     (or level gnus-group-default-list-level gnus-level-subscribed))))
4105
4106 ;;;###autoload
4107 (defun gnus-slave-no-server (&optional arg)
4108   "Read network news as a slave, without connecting to local server"
4109   (interactive "P")
4110   (gnus-no-server arg t))
4111
4112 ;;;###autoload
4113 (defun gnus-no-server (&optional arg slave)
4114   "Read network news.
4115 If ARG is a positive number, Gnus will use that as the
4116 startup level.  If ARG is nil, Gnus will be started at level 2.
4117 If ARG is non-nil and not a positive number, Gnus will
4118 prompt the user for the name of an NNTP server to use.
4119 As opposed to `gnus', this command will not connect to the local server."
4120   (interactive "P")
4121   (make-local-variable 'gnus-group-use-permanent-levels)
4122   (setq gnus-group-use-permanent-levels t)
4123   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4124
4125 ;;;###autoload
4126 (defun gnus-slave (&optional arg)
4127   "Read news as a slave."
4128   (interactive "P")
4129   (gnus arg nil 'slave))
4130
4131 ;;;###autoload
4132 (defun gnus-other-frame (&optional arg)
4133   "Pop up a frame to read news."
4134   (interactive "P")
4135   (if (get-buffer gnus-group-buffer)
4136       (let ((pop-up-frames t))
4137         (gnus arg))
4138     (select-frame (make-frame))
4139     (gnus arg)))
4140
4141 ;;;###autoload
4142 (defun gnus (&optional arg dont-connect slave)
4143   "Read network news.
4144 If ARG is non-nil and a positive number, Gnus will use that as the
4145 startup level.  If ARG is non-nil and not a positive number, Gnus will
4146 prompt the user for the name of an NNTP server to use."
4147   (interactive "P")
4148
4149   (if (get-buffer gnus-group-buffer)
4150       (progn
4151         (switch-to-buffer gnus-group-buffer)
4152         (gnus-group-get-new-news))
4153
4154     (gnus-clear-system)
4155     (nnheader-init-server-buffer)
4156     (gnus-read-init-file)
4157     (setq gnus-slave slave)
4158
4159     (gnus-group-setup-buffer)
4160     (let ((buffer-read-only nil))
4161       (erase-buffer)
4162       (if (not gnus-inhibit-startup-message)
4163           (progn
4164             (gnus-group-startup-message)
4165             (sit-for 0))))
4166
4167     (let ((level (and (numberp arg) (> arg 0) arg))
4168           did-connect)
4169       (unwind-protect
4170           (progn
4171             (or dont-connect
4172                 (setq did-connect
4173                       (gnus-start-news-server (and arg (not level))))))
4174         (if (and (not dont-connect)
4175                  (not did-connect))
4176             (gnus-group-quit)
4177           (run-hooks 'gnus-startup-hook)
4178           ;; NNTP server is successfully open.
4179
4180           ;; Find the current startup file name.
4181           (setq gnus-current-startup-file
4182                 (gnus-make-newsrc-file gnus-startup-file))
4183
4184           ;; Read the dribble file.
4185           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4186
4187           (gnus-summary-make-display-table)
4188           ;; Do the actual startup.
4189           (gnus-setup-news nil level)
4190           ;; Generate the group buffer.
4191           (gnus-group-list-groups level)
4192           (gnus-group-first-unread-group)
4193           (gnus-configure-windows 'group)
4194           (gnus-group-set-mode-line))))))
4195
4196 (defun gnus-unload ()
4197   "Unload all Gnus features."
4198   (interactive)
4199   (or (boundp 'load-history)
4200       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4201   (let ((history load-history)
4202         feature)
4203     (while history
4204       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4205            (setq feature (cdr (assq 'provide (car history))))
4206            (unload-feature feature 'force))
4207       (setq history (cdr history)))))
4208
4209 (defun gnus-compile ()
4210   "Byte-compile the user-defined format specs."
4211   (interactive)
4212   (let ((entries gnus-format-specs)
4213         entry gnus-tmp-func)
4214     (save-excursion
4215       (gnus-message 7 "Compiling format specs...")
4216
4217       (while entries
4218         (setq entry (pop entries))
4219         (if (eq (car entry) 'version)
4220             (setq gnus-format-specs (delq entry gnus-format-specs))
4221           (when (and (listp (caddr entry))
4222                      (not (eq 'byte-code (caaddr entry))))
4223             (fset 'gnus-tmp-func
4224                   `(lambda () ,(caddr entry)))
4225             (byte-compile 'gnus-tmp-func)
4226             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4227
4228       (push (cons 'version emacs-version) gnus-format-specs)
4229
4230       (gnus-message 7 "Compiling user specs...done"))))
4231
4232 (defun gnus-indent-rigidly (start end arg)
4233   "Indent rigidly using only spaces and no tabs."
4234   (save-excursion
4235     (save-restriction
4236       (narrow-to-region start end)
4237       (indent-rigidly start end arg)
4238       (goto-char (point-min))
4239       (while (search-forward "\t" nil t)
4240         (replace-match "        " t t)))))
4241
4242 (defun gnus-group-startup-message (&optional x y)
4243   "Insert startup message in current buffer."
4244   ;; Insert the message.
4245   (erase-buffer)
4246   (insert
4247    (format "              %s
4248           _    ___ _             _
4249           _ ___ __ ___  __    _ ___
4250           __   _     ___    __  ___
4251               _           ___     _
4252              _  _ __             _
4253              ___   __            _
4254                    __           _
4255                     _      _   _
4256                    _      _    _
4257                       _  _    _
4258                   __  ___
4259                  _   _ _     _
4260                 _   _
4261               _    _
4262              _    _
4263             _
4264           __
4265
4266 "
4267            ""))
4268   ;; And then hack it.
4269   (gnus-indent-rigidly (point-min) (point-max)
4270                        (/ (max (- (window-width) (or x 46)) 0) 2))
4271   (goto-char (point-min))
4272   (forward-line 1)
4273   (let* ((pheight (count-lines (point-min) (point-max)))
4274          (wheight (window-height))
4275          (rest (- wheight pheight)))
4276     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4277   ;; Fontify some.
4278   (goto-char (point-min))
4279   (and (search-forward "Praxis" nil t)
4280        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4281   (goto-char (point-min))
4282   (let* ((mode-string (gnus-group-set-mode-line)))
4283     (setq mode-line-buffer-identification
4284           (list (concat gnus-version (substring (car mode-string) 4))))
4285     (set-buffer-modified-p t)))
4286
4287 (defun gnus-group-setup-buffer ()
4288   (or (get-buffer gnus-group-buffer)
4289       (progn
4290         (switch-to-buffer gnus-group-buffer)
4291         (gnus-add-current-to-buffer-list)
4292         (gnus-group-mode)
4293         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4294
4295 (defun gnus-group-list-groups (&optional level unread lowest)
4296   "List newsgroups with level LEVEL or lower that have unread articles.
4297 Default is all subscribed groups.
4298 If argument UNREAD is non-nil, groups with no unread articles are also
4299 listed."
4300   (interactive (list (if current-prefix-arg
4301                          (prefix-numeric-value current-prefix-arg)
4302                        (or
4303                         (gnus-group-default-level nil t)
4304                         gnus-group-default-list-level
4305                         gnus-level-subscribed))))
4306   (or level
4307       (setq level (car gnus-group-list-mode)
4308             unread (cdr gnus-group-list-mode)))
4309   (setq level (gnus-group-default-level level))
4310   (gnus-group-setup-buffer)             ;May call from out of group buffer
4311   (gnus-update-format-specifications)
4312   (let ((case-fold-search nil)
4313         (props (text-properties-at (gnus-point-at-bol)))
4314         (group (gnus-group-group-name)))
4315     (funcall gnus-group-prepare-function level unread lowest)
4316     (if (zerop (buffer-size))
4317         (gnus-message 5 gnus-no-groups-message)
4318       (goto-char (point-max))
4319       (when (or (not gnus-group-goto-next-group-function)
4320                 (not (funcall gnus-group-goto-next-group-function 
4321                               group props)))
4322         (if (not group)
4323             ;; Go to the first group with unread articles.
4324             (gnus-group-search-forward t)
4325           ;; Find the right group to put point on.  If the current group
4326           ;; has disappeared in the new listing, try to find the next
4327           ;; one.        If no next one can be found, just leave point at the
4328           ;; first newsgroup in the buffer.
4329           (if (not (gnus-goto-char
4330                     (text-property-any
4331                      (point-min) (point-max)
4332                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4333               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4334                 (while (and newsrc
4335                             (not (gnus-goto-char
4336                                   (text-property-any
4337                                    (point-min) (point-max) 'gnus-group
4338                                    (gnus-intern-safe
4339                                     (car (car newsrc)) gnus-active-hashtb)))))
4340                   (setq newsrc (cdr newsrc)))
4341                 (or newsrc (progn (goto-char (point-max))
4342                                   (forward-line -1)))))))
4343       ;; Adjust cursor point.
4344       (gnus-group-position-point))))
4345
4346 (defun gnus-group-list-level (level &optional all)
4347   "List groups on LEVEL.
4348 If ALL (the prefix), also list groups that have no unread articles."
4349   (interactive "nList groups on level: \nP")
4350   (gnus-group-list-groups level all level))
4351
4352 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4353   "List all newsgroups with unread articles of level LEVEL or lower.
4354 If ALL is non-nil, list groups that have no unread articles.
4355 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4356 If REGEXP, only list groups matching REGEXP."
4357   (set-buffer gnus-group-buffer)
4358   (let ((buffer-read-only nil)
4359         (newsrc (cdr gnus-newsrc-alist))
4360         (lowest (or lowest 1))
4361         info clevel unread group params)
4362     (erase-buffer)
4363     (if (< lowest gnus-level-zombie)
4364         ;; List living groups.
4365         (while newsrc
4366           (setq info (car newsrc)
4367                 group (gnus-info-group info)
4368                 params (gnus-info-params info)
4369                 newsrc (cdr newsrc)
4370                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4371           (and unread                   ; This group might be bogus
4372                (or (not regexp)
4373                    (string-match regexp group))
4374                (<= (setq clevel (gnus-info-level info)) level)
4375                (>= clevel lowest)
4376                (or all                  ; We list all groups?
4377                    (and gnus-group-list-inactive-groups
4378                         (eq unread t))  ; We list unactivated groups
4379                    (> unread 0)         ; We list groups with unread articles
4380                    (and gnus-list-groups-with-ticked-articles
4381                         (cdr (assq 'tick (gnus-info-marks info))))
4382                                         ; And groups with tickeds
4383                    ;; Check for permanent visibility.
4384                    (and gnus-permanently-visible-groups
4385                         (string-match gnus-permanently-visible-groups
4386                                       group))
4387                    (memq 'visible params)
4388                    (cdr (assq 'visible params)))
4389                (gnus-group-insert-group-line
4390                 group (gnus-info-level info)
4391                 (gnus-info-marks info) unread (gnus-info-method info)))))
4392
4393     ;; List dead groups.
4394     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4395          (gnus-group-prepare-flat-list-dead
4396           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4397           gnus-level-zombie ?Z
4398           regexp))
4399     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4400          (gnus-group-prepare-flat-list-dead
4401           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4402           gnus-level-killed ?K regexp))
4403
4404     (gnus-group-set-mode-line)
4405     (setq gnus-group-list-mode (cons level all))
4406     (run-hooks 'gnus-group-prepare-hook)))
4407
4408 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4409   ;; List zombies and killed lists somewhat faster, which was
4410   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4411   ;; this by ignoring the group format specification altogether.
4412   (let (group beg)
4413     (if regexp
4414         ;; This loop is used when listing groups that match some
4415         ;; regexp.
4416         (while groups
4417           (setq group (pop groups))
4418           (when (string-match regexp group)
4419             (add-text-properties
4420              (point) (prog1 (1+ (point))
4421                        (insert " " mark "     *: " group "\n"))
4422              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4423                    'gnus-unread t
4424                    'gnus-level level))))
4425       ;; This loop is used when listing all groups.
4426       (while groups
4427         (add-text-properties
4428          (point) (prog1 (1+ (point))
4429                    (insert " " mark "     *: "
4430                            (setq group (pop groups)) "\n"))
4431          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4432                'gnus-unread t
4433                'gnus-level level))))))
4434
4435 (defmacro gnus-group-real-name (group)
4436   "Find the real name of a foreign newsgroup."
4437   `(let ((gname ,group))
4438      (if (string-match ":[^:]+$" gname)
4439          (substring gname (1+ (match-beginning 0)))
4440        gname)))
4441
4442 (defsubst gnus-server-add-address (method)
4443   (let ((method-name (symbol-name (car method))))
4444     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4445              (not (assq (intern (concat method-name "-address")) method)))
4446         (append method (list (list (intern (concat method-name "-address"))
4447                                    (nth 1 method))))
4448       method)))
4449
4450 (defsubst gnus-server-get-method (group method)
4451   ;; Input either a server name, and extended server name, or a
4452   ;; select method, and return a select method.
4453   (cond ((stringp method)
4454          (gnus-server-to-method method))
4455         ((and (stringp (car method)) group)
4456          (gnus-server-extend-method group method))
4457         (t
4458          (gnus-server-add-address method))))
4459
4460 (defun gnus-server-to-method (server)
4461   "Map virtual server names to select methods."
4462   (or (and (equal server "native") gnus-select-method)
4463       (cdr (assoc server gnus-server-alist))))
4464
4465 (defmacro gnus-server-equal (ss1 ss2)
4466   "Say whether two servers are equal."
4467   `(let ((s1 ,ss1)
4468          (s2 ,ss2))
4469      (or (equal s1 s2)
4470          (and (= (length s1) (length s2))
4471               (progn
4472                 (while (and s1 (member (car s1) s2))
4473                   (setq s1 (cdr s1)))
4474                 (null s1))))))
4475
4476 (defun gnus-group-prefixed-name (group method)
4477   "Return the whole name from GROUP and METHOD."
4478   (and (stringp method) (setq method (gnus-server-to-method method)))
4479   (concat (format "%s" (car method))
4480           (if (and
4481                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
4482                (not (string= (nth 1 method) "")))
4483               (concat "+" (nth 1 method)))
4484           ":" group))
4485
4486 (defun gnus-group-real-prefix (group)
4487   "Return the prefix of the current group name."
4488   (if (string-match "^[^:]+:" group)
4489       (substring group 0 (match-end 0))
4490     ""))
4491
4492 (defun gnus-group-method-name (group)
4493   "Return the method used for selecting GROUP."
4494   (let ((prefix (gnus-group-real-prefix group)))
4495     (if (equal prefix "")
4496         gnus-select-method
4497       (if (string-match "^[^\\+]+\\+" prefix)
4498           (list (intern (substring prefix 0 (1- (match-end 0))))
4499                 (substring prefix (match-end 0) (1- (length prefix))))
4500         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
4501
4502 (defsubst gnus-secondary-method-p (method)
4503   "Return whether METHOD is a secondary select method."
4504   (let ((methods gnus-secondary-select-methods)
4505         (gmethod (gnus-server-get-method nil method)))
4506     (while (and methods
4507                 (not (equal (gnus-server-get-method nil (car methods))
4508                             gmethod)))
4509       (setq methods (cdr methods)))
4510     methods))
4511
4512 (defun gnus-group-foreign-p (group)
4513   "Say whether a group is foreign or not."
4514   (and (not (gnus-group-native-p group))
4515        (not (gnus-group-secondary-p group))))
4516
4517 (defun gnus-group-native-p (group)
4518   "Say whether the group is native or not."
4519   (not (string-match ":" group)))
4520
4521 (defun gnus-group-secondary-p (group)
4522   "Say whether the group is secondary or not."
4523   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4524
4525 (defun gnus-group-get-parameter (group &optional symbol)
4526   "Returns the group parameters for GROUP.
4527 If SYMBOL, return the value of that symbol in the group parameters."
4528   (let ((params (gnus-info-params (gnus-get-info group))))
4529     (if symbol
4530         (gnus-group-parameter-value params symbol)
4531       params)))
4532
4533 (defun gnus-group-parameter-value (params symbol)
4534   "Return the value of SYMBOL in group PARAMS."
4535   (or (car (memq symbol params))        ; It's either a simple symbol
4536       (cdr (assq symbol params))))      ; or a cons.
4537
4538 (defun gnus-group-add-parameter (group param)
4539   "Add parameter PARAM to GROUP."
4540   (let ((info (gnus-get-info group)))
4541     (if (not info)
4542         () ; This is a dead group.  We just ignore it.
4543       ;; Cons the new param to the old one and update.
4544       (gnus-group-set-info (cons param (gnus-info-params info))
4545                            group 'params))))
4546
4547 (defun gnus-group-add-score (group &optional score)
4548   "Add SCORE to the GROUP score.
4549 If SCORE is nil, add 1 to the score of GROUP."
4550   (let ((info (gnus-get-info group)))
4551     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4552
4553 (defun gnus-summary-bubble-group ()
4554   "Increase the score of the current group.
4555 This is a handy function to add to `gnus-summary-exit-hook' to
4556 increase the score of each group you read."
4557   (gnus-group-add-score gnus-newsgroup-name))
4558
4559 (defun gnus-group-set-info (info &optional method-only-group part)
4560   (let* ((entry (gnus-gethash
4561                  (or method-only-group (gnus-info-group info))
4562                  gnus-newsrc-hashtb))
4563          (part-info info)
4564          (info (if method-only-group (nth 2 entry) info)))
4565     (when method-only-group
4566       (unless entry
4567         (error "Trying to change non-existent group %s" method-only-group))
4568       ;; We have received parts of the actual group info - either the
4569       ;; select method or the group parameters.  We first check
4570       ;; whether we have to extend the info, and if so, do that.
4571       (let ((len (length info))
4572             (total (if (eq part 'method) 5 6)))
4573         (when (< len total)
4574           (setcdr (nthcdr (1- len) info)
4575                   (make-list (- total len) nil)))
4576         ;; Then we enter the new info.
4577         (setcar (nthcdr (1- total) info) part-info)))
4578     (unless entry
4579       ;; This is a new group, so we just create it.
4580       (save-excursion
4581         (set-buffer gnus-group-buffer)
4582         (if (gnus-info-method info)
4583             ;; It's a foreign group...
4584             (gnus-group-make-group
4585              (gnus-group-real-name (gnus-info-group info))
4586              (prin1-to-string (car (gnus-info-method info)))
4587              (nth 1 (gnus-info-method info)))
4588           ;; It's a native group.
4589           (gnus-group-make-group (gnus-info-group info)))
4590         (gnus-message 6 "Note: New group created")
4591         (setq entry
4592               (gnus-gethash (gnus-group-prefixed-name
4593                              (gnus-group-real-name (gnus-info-group info))
4594                              (or (gnus-info-method info) gnus-select-method))
4595                             gnus-newsrc-hashtb))))
4596     ;; Whether it was a new group or not, we now have the entry, so we
4597     ;; can do the update.
4598     (if entry
4599         (progn
4600           (setcar (nthcdr 2 entry) info)
4601           (when (and (not (eq (car entry) t))
4602                      (gnus-active (gnus-info-group info)))
4603             (let ((marked (gnus-info-marks info)))
4604               (setcar entry (length (gnus-list-of-unread-articles
4605                                      (car info)))))))
4606       (error "No such group: %s" (gnus-info-group info)))))
4607
4608 (defun gnus-group-set-method-info (group select-method)
4609   (gnus-group-set-info select-method group 'method))
4610
4611 (defun gnus-group-set-params-info (group params)
4612   (gnus-group-set-info params group 'params))
4613
4614 (defun gnus-group-update-group-line ()
4615   "Update the current line in the group buffer."
4616   (let* ((buffer-read-only nil)
4617          (group (gnus-group-group-name))
4618          (gnus-group-indentation (gnus-group-group-indentation))
4619          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4620     (and entry
4621          (not (gnus-ephemeral-group-p group))
4622          (gnus-dribble-enter
4623           (concat "(gnus-group-set-info '"
4624                   (prin1-to-string (nth 2 entry)) ")")))
4625     (gnus-delete-line)
4626     (gnus-group-insert-group-line-info group)
4627     (forward-line -1)
4628     (gnus-group-position-point)))
4629
4630 (defun gnus-group-insert-group-line-info (group)
4631   "Insert GROUP on the current line."
4632   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4633         active info)
4634     (if entry
4635         (progn
4636           ;; (Un)subscribed group.
4637           (setq info (nth 2 entry))
4638           (gnus-group-insert-group-line
4639            group (gnus-info-level info) (gnus-info-marks info)
4640            (or (car entry) t) (gnus-info-method info)))
4641       ;; This group is dead.
4642       (gnus-group-insert-group-line
4643        group
4644        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4645        nil
4646        (if (setq active (gnus-active group))
4647            (- (1+ (cdr active)) (car active)) 0)
4648        nil))))
4649
4650 (defun gnus-group-insert-group-line
4651   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4652                   gnus-tmp-method)
4653   "Insert a group line in the group buffer."
4654   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4655          (gnus-tmp-number-total
4656           (if gnus-tmp-active
4657               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4658             0))
4659          (gnus-tmp-number-of-unread
4660           (if (numberp number) (int-to-string (max 0 number))
4661             "*"))
4662          (gnus-tmp-number-of-read
4663           (if (numberp number)
4664               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4665             "*"))
4666          (gnus-tmp-subscribed
4667           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4668                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4669                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4670                 (t ?K)))
4671          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4672          (gnus-tmp-newsgroup-description
4673           (if gnus-description-hashtb
4674               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4675             ""))
4676          (gnus-tmp-moderated
4677           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4678          (gnus-tmp-moderated-string
4679           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4680          (gnus-tmp-method
4681           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4682          (gnus-tmp-news-server (or (car (cdr gnus-tmp-method)) ""))
4683          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4684          (gnus-tmp-news-method-string
4685           (if gnus-tmp-method
4686               (format "(%s:%s)" (car gnus-tmp-method)
4687                       (car (cdr gnus-tmp-method))) ""))
4688          (gnus-tmp-marked-mark
4689           (if (and (numberp number)
4690                    (zerop number)
4691                    (cdr (assq 'tick gnus-tmp-marked)))
4692               ?* ? ))
4693          (gnus-tmp-process-marked
4694           (if (member gnus-tmp-group gnus-group-marked)
4695               gnus-process-mark ? ))
4696          (buffer-read-only nil)
4697          header gnus-tmp-header)                        ; passed as parameter to user-funcs.
4698     (beginning-of-line)
4699     (add-text-properties
4700      (point)
4701      (prog1 (1+ (point))
4702        ;; Insert the text.
4703        (eval gnus-group-line-format-spec))
4704      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4705        gnus-unread ,(if (numberp number)
4706                         (string-to-int gnus-tmp-number-of-unread)
4707                       t)
4708        gnus-marked ,gnus-tmp-marked-mark
4709        gnus-indentation ,gnus-group-indentation
4710        gnus-level ,gnus-tmp-level))
4711     (when (gnus-visual-p 'group-highlight 'highlight)
4712       (forward-line -1)
4713       (run-hooks 'gnus-group-update-hook)
4714       (forward-line))
4715     ;; Allow XEmacs to remove front-sticky text properties.
4716     (gnus-group-remove-excess-properties)))
4717
4718 (defun gnus-group-update-group (group &optional visible-only)
4719   "Update all lines where GROUP appear.
4720 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4721 already."
4722   (save-excursion
4723     (set-buffer gnus-group-buffer)
4724     ;; The buffer may be narrowed.
4725     (save-restriction
4726       (widen)
4727       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4728             (loc (point-min))
4729             found buffer-read-only visible)
4730         ;; Enter the current status into the dribble buffer.
4731         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4732           (if (and entry (not (gnus-ephemeral-group-p group)))
4733               (gnus-dribble-enter
4734                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4735                        ")"))))
4736         ;; Find all group instances.  If topics are in use, each group
4737         ;; may be listed in more than once.
4738         (while (setq loc (text-property-any
4739                           loc (point-max) 'gnus-group ident))
4740           (setq found t)
4741           (goto-char loc)
4742           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4743             (gnus-delete-line)
4744             (gnus-group-insert-group-line-info group))
4745           (setq loc (1+ loc)))
4746         (unless (or found visible-only)
4747           ;; No such line in the buffer, find out where it's supposed to
4748           ;; go, and insert it there (or at the end of the buffer).
4749           (if gnus-goto-missing-group-function
4750               (funcall gnus-goto-missing-group-function group)
4751             (let ((entry (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
4752               (while (and entry (car entry)
4753                           (not
4754                            (gnus-goto-char
4755                             (text-property-any
4756                              (point-min) (point-max)
4757                              'gnus-group (gnus-intern-safe
4758                                           (car (car entry))
4759                                           gnus-active-hashtb)))))
4760                 (setq entry (cdr entry)))
4761               (or entry (goto-char (point-max)))))
4762           ;; Finally insert the line.
4763           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4764             (gnus-group-insert-group-line-info group)))
4765         (gnus-group-set-mode-line)))))
4766
4767 (defun gnus-group-set-mode-line ()
4768   (when (memq 'group gnus-updated-mode-lines)
4769     (let* ((gformat (or gnus-group-mode-line-format-spec
4770                         (setq gnus-group-mode-line-format-spec
4771                               (gnus-parse-format
4772                                gnus-group-mode-line-format
4773                                gnus-group-mode-line-format-alist))))
4774            (gnus-tmp-news-server (car (cdr gnus-select-method)))
4775            (gnus-tmp-news-method (car gnus-select-method))
4776            (max-len 60)
4777            gnus-tmp-header                      ;Dummy binding for user-defined formats
4778            ;; Get the resulting string.
4779            (mode-string (eval gformat)))
4780       ;; If the line is too long, we chop it off.
4781       (when (> (length mode-string) max-len)
4782         (setq mode-string (substring mode-string 0 (- max-len 4))))
4783       (prog1
4784           (setq mode-line-buffer-identification (list mode-string))
4785         (set-buffer-modified-p t)))))
4786
4787 (defun gnus-group-group-name ()
4788   "Get the name of the newsgroup on the current line."
4789   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4790     (and group (symbol-name group))))
4791
4792 (defun gnus-group-group-level ()
4793   "Get the level of the newsgroup on the current line."
4794   (get-text-property (gnus-point-at-bol) 'gnus-level))
4795
4796 (defun gnus-group-group-indentation ()
4797   "Get the indentation of the newsgroup on the current line."
4798   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation) ""))
4799
4800 (defun gnus-group-group-unread ()
4801   "Get the number of unread articles of the newsgroup on the current line."
4802   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4803
4804 (defun gnus-group-search-forward (&optional backward all level first-too)
4805   "Find the next newsgroup with unread articles.
4806 If BACKWARD is non-nil, find the previous newsgroup instead.
4807 If ALL is non-nil, just find any newsgroup.
4808 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4809 group exists.
4810 If FIRST-TOO, the current line is also eligible as a target."
4811   (let ((way (if backward -1 1))
4812         (low gnus-level-killed)
4813         (beg (point))
4814         pos found lev)
4815     (if (and backward (progn (beginning-of-line)) (bobp))
4816         nil
4817       (or first-too (forward-line way))
4818       (while (and
4819               (not (eobp))
4820               (not (setq
4821                     found
4822                     (and (or all
4823                              (and
4824                               (let ((unread
4825                                      (get-text-property (point) 'gnus-unread)))
4826                                 (and (numberp unread) (> unread 0)))
4827                               (setq lev (get-text-property (point)
4828                                                            'gnus-level))
4829                               (<= lev gnus-level-subscribed)))
4830                          (or (not level)
4831                              (and (setq lev (get-text-property (point)
4832                                                                'gnus-level))
4833                                   (or (= lev level)
4834                                       (and (< lev low)
4835                                            (< level lev)
4836                                            (progn
4837                                              (setq low lev)
4838                                              (setq pos (point))
4839                                              nil))))))))
4840               (zerop (forward-line way)))))
4841     (if found
4842         (progn (gnus-group-position-point) t)
4843       (goto-char (or pos beg))
4844       (and pos t))))
4845
4846 ;;; Gnus group mode commands
4847
4848 ;; Group marking.
4849
4850 (defun gnus-group-mark-group (n &optional unmark no-advance)
4851   "Mark the current group."
4852   (interactive "p")
4853   (let ((buffer-read-only nil)
4854         group)
4855     (while
4856         (and (> n 0)
4857              (setq group (gnus-group-group-name))
4858              (progn
4859                (beginning-of-line)
4860                (forward-char
4861                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4862                (delete-char 1)
4863                (if unmark
4864                    (progn
4865                      (insert " ")
4866                      (setq gnus-group-marked (delete group gnus-group-marked)))
4867                  (insert "#")
4868                  (setq gnus-group-marked
4869                        (cons group (delete group gnus-group-marked))))
4870                t)
4871              (or no-advance (zerop (gnus-group-next-group 1))))
4872       (setq n (1- n)))
4873     (gnus-summary-position-point)
4874     n))
4875
4876 (defun gnus-group-unmark-group (n)
4877   "Remove the mark from the current group."
4878   (interactive "p")
4879   (gnus-group-mark-group n 'unmark)
4880   (gnus-group-position-point))
4881
4882 (defun gnus-group-unmark-all-groups ()
4883   "Unmark all groups."
4884   (let ((groups gnus-group-marked))
4885     (save-excursion
4886       (while groups
4887         (gnus-group-remove-mark (pop groups)))))
4888   (gnus-group-position-point))
4889
4890 (defun gnus-group-mark-region (unmark beg end)
4891   "Mark all groups between point and mark.
4892 If UNMARK, remove the mark instead."
4893   (interactive "P\nr")
4894   (let ((num (count-lines beg end)))
4895     (save-excursion
4896       (goto-char beg)
4897       (- num (gnus-group-mark-group num unmark)))))
4898
4899 (defun gnus-group-mark-buffer (&optional unmark)
4900   "Mark all groups in the buffer.
4901 If UNMARK, remove the mark instead."
4902   (interactive "P")
4903   (gnus-group-mark-region unmark (point-min) (point-max)))
4904
4905 (defun gnus-group-mark-regexp (regexp)
4906   "Mark all groups that match some regexp."
4907   (interactive "sMark (regexp): ")
4908   (let ((alist (cdr gnus-newsrc-alist))
4909         group)
4910     (while alist
4911       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
4912         (gnus-group-set-mark group))))
4913   (gnus-group-position-point))
4914
4915 (defun gnus-group-remove-mark (group)
4916   "Remove the process mark from GROUP and move point there.
4917 Return nil if the group isn't displayed."
4918   (if (gnus-group-goto-group group)
4919       (save-excursion
4920         (gnus-group-mark-group 1 'unmark t)
4921         t)
4922     (setq gnus-group-marked
4923           (delete group gnus-group-marked))
4924     nil))
4925
4926 (defun gnus-group-set-mark (group)
4927   "Set the process mark on GROUP."
4928   (if (gnus-group-goto-group group)
4929       (save-excursion
4930         (gnus-group-mark-group 1 nil t))
4931     (setq gnus-group-marked
4932           (cons group (delete group gnus-group-marked)))))
4933
4934 (defun gnus-group-universal-argument (arg &optional groups func)
4935   "Perform any command on all groups accoring to the process/prefix convention."
4936   (interactive "P")
4937   (let ((groups (or groups (gnus-group-process-prefix arg)))
4938         group func)
4939     (if (eq (setq func (or func
4940                            (key-binding
4941                             (read-key-sequence
4942                              (substitute-command-keys
4943                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
4944             'undefined)
4945         (progn
4946           (message "Undefined key")
4947           (ding))
4948       (while groups
4949         (gnus-group-remove-mark (setq group (pop groups)))
4950         (command-execute func))))
4951   (gnus-group-position-point))
4952
4953 (defun gnus-group-process-prefix (n)
4954   "Return a list of groups to work on.
4955 Take into consideration N (the prefix) and the list of marked groups."
4956   (cond
4957    (n
4958     (setq n (prefix-numeric-value n))
4959     ;; There is a prefix, so we return a list of the N next
4960     ;; groups.
4961     (let ((way (if (< n 0) -1 1))
4962           (n (abs n))
4963           group groups)
4964       (save-excursion
4965         (while (and (> n 0)
4966                     (setq group (gnus-group-group-name)))
4967           (setq groups (cons group groups))
4968           (setq n (1- n))
4969           (gnus-group-next-group way)))
4970       (nreverse groups)))
4971    ((and (boundp 'transient-mark-mode)
4972          transient-mark-mode
4973          mark-active)
4974     ;; Work on the region between point and mark.
4975     (let ((max (max (point) (mark)))
4976           groups)
4977       (save-excursion
4978         (goto-char (min (point) (mark)))
4979         (while
4980             (and
4981              (push (gnus-group-group-name) groups)
4982              (zerop (gnus-group-next-group 1))
4983              (< (point) max)))
4984         (nreverse groups))))
4985    (gnus-group-marked
4986     ;; No prefix, but a list of marked articles.
4987     (reverse gnus-group-marked))
4988    (t
4989     ;; Neither marked articles or a prefix, so we return the
4990     ;; current group.
4991     (let ((group (gnus-group-group-name)))
4992       (and group (list group))))))
4993
4994 ;; Selecting groups.
4995
4996 (defun gnus-group-read-group (&optional all no-article group)
4997   "Read news in this newsgroup.
4998 If the prefix argument ALL is non-nil, already read articles become
4999 readable.  IF ALL is a number, fetch this number of articles.  If the
5000 optional argument NO-ARTICLE is non-nil, no article will be
5001 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5002 group."
5003   (interactive "P")
5004   (let ((group (or group (gnus-group-group-name)))
5005         number active marked entry)
5006     (or group (error "No group on current line"))
5007     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5008                                             group gnus-newsrc-hashtb)))))
5009     ;; This group might be a dead group.  In that case we have to get
5010     ;; the number of unread articles from `gnus-active-hashtb'.
5011     (setq number
5012           (cond ((numberp all) all)
5013                 (entry (car entry))
5014                 ((setq active (gnus-active group))
5015                  (- (1+ (cdr active)) (car active)))))
5016     (gnus-summary-read-group
5017      group (or all (and (numberp number)
5018                         (zerop (+ number (length (cdr (assq 'tick marked)))
5019                                   (length (cdr (assq 'dormant marked)))))))
5020      no-article)))
5021
5022 (defun gnus-group-select-group (&optional all)
5023   "Select this newsgroup.
5024 No article is selected automatically.
5025 If ALL is non-nil, already read articles become readable.
5026 If ALL is a number, fetch this number of articles."
5027   (interactive "P")
5028   (gnus-group-read-group all t))
5029
5030 (defun gnus-group-quick-select-group (&optional all)
5031   "Select the current group \"quickly\".
5032 This means that no highlighting or scoring will be performed."
5033   (interactive "P")
5034   (let (gnus-visual
5035         gnus-score-find-score-files-function
5036         gnus-apply-kill-hook
5037         gnus-summary-expunge-below)
5038     (gnus-group-read-group all t)))
5039
5040 (defun gnus-group-visible-select-group (&optional all)
5041   "Select the current group without hiding any articles."
5042   (interactive "P")
5043   (let ((gnus-inhibit-limiting t))
5044     (gnus-group-read-group all t)))
5045
5046 ;;;###autoload
5047 (defun gnus-fetch-group (group)
5048   "Start Gnus if necessary and enter GROUP.
5049 Returns whether the fetching was successful or not."
5050   (interactive "sGroup name: ")
5051   (or (get-buffer gnus-group-buffer)
5052       (gnus))
5053   (gnus-group-select-group))
5054
5055 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5056 ;; if selection was successful.
5057 (defun gnus-group-read-ephemeral-group
5058   (group method &optional activate quit-config)
5059   (let ((group (if (gnus-group-foreign-p group) group
5060                  (gnus-group-prefixed-name group method))))
5061     (gnus-sethash
5062      group
5063      (list t nil (list group gnus-level-default-subscribed nil nil
5064                        (append method
5065                                (list
5066                                 (list 'quit-config
5067                                       (if quit-config quit-config
5068                                         (cons (current-buffer) 'summary)))))))
5069      gnus-newsrc-hashtb)
5070     (set-buffer gnus-group-buffer)
5071     (or (gnus-check-server method)
5072         (error "Unable to contact server: %s" (gnus-status-message method)))
5073     (if activate (or (gnus-request-group group)
5074                      (error "Couldn't request group")))
5075     (condition-case ()
5076         (gnus-group-read-group t t group)
5077       (error nil)
5078       (quit nil))))
5079
5080 (defun gnus-group-jump-to-group (group)
5081   "Jump to newsgroup GROUP."
5082   (interactive
5083    (list (completing-read
5084           "Group: " gnus-active-hashtb nil
5085           (memq gnus-select-method gnus-have-read-active-file))))
5086
5087   (if (equal group "")
5088       (error "Empty group name"))
5089
5090   (let ((b (text-property-any
5091             (point-min) (point-max)
5092             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5093     (unless (gnus-ephemeral-group-p group)
5094       (if b
5095           ;; Either go to the line in the group buffer...
5096           (goto-char b)
5097         ;; ... or insert the line.
5098         (or
5099          (gnus-active group)
5100          (gnus-activate-group group)
5101          (error "%s error: %s" group (gnus-status-message group)))
5102
5103         (gnus-group-update-group group)
5104         (goto-char (text-property-any
5105                     (point-min) (point-max)
5106                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5107     ;; Adjust cursor point.
5108     (gnus-group-position-point)))
5109
5110 (defun gnus-group-goto-group (group)
5111   "Goto to newsgroup GROUP."
5112   (when group
5113     (let ((b (text-property-any (point-min) (point-max)
5114                                 'gnus-group (gnus-intern-safe
5115                                              group gnus-active-hashtb))))
5116       (and b (goto-char b)))))
5117
5118 (defun gnus-group-next-group (n)
5119   "Go to next N'th newsgroup.
5120 If N is negative, search backward instead.
5121 Returns the difference between N and the number of skips actually
5122 done."
5123   (interactive "p")
5124   (gnus-group-next-unread-group n t))
5125
5126 (defun gnus-group-next-unread-group (n &optional all level)
5127   "Go to next N'th unread newsgroup.
5128 If N is negative, search backward instead.
5129 If ALL is non-nil, choose any newsgroup, unread or not.
5130 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5131 such group can be found, the next group with a level higher than
5132 LEVEL.
5133 Returns the difference between N and the number of skips actually
5134 made."
5135   (interactive "p")
5136   (let ((backward (< n 0))
5137         (n (abs n)))
5138     (while (and (> n 0)
5139                 (gnus-group-search-forward
5140                  backward (or (not gnus-group-goto-unread) all) level))
5141       (setq n (1- n)))
5142     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5143                                (if level " on this level or higher" "")))
5144     n))
5145
5146 (defun gnus-group-prev-group (n)
5147   "Go to previous N'th newsgroup.
5148 Returns the difference between N and the number of skips actually
5149 done."
5150   (interactive "p")
5151   (gnus-group-next-unread-group (- n) t))
5152
5153 (defun gnus-group-prev-unread-group (n)
5154   "Go to previous N'th unread newsgroup.
5155 Returns the difference between N and the number of skips actually
5156 done."
5157   (interactive "p")
5158   (gnus-group-next-unread-group (- n)))
5159
5160 (defun gnus-group-next-unread-group-same-level (n)
5161   "Go to next N'th unread newsgroup on the same level.
5162 If N is negative, search backward instead.
5163 Returns the difference between N and the number of skips actually
5164 done."
5165   (interactive "p")
5166   (gnus-group-next-unread-group n t (gnus-group-group-level))
5167   (gnus-group-position-point))
5168
5169 (defun gnus-group-prev-unread-group-same-level (n)
5170   "Go to next N'th unread newsgroup on the same level.
5171 Returns the difference between N and the number of skips actually
5172 done."
5173   (interactive "p")
5174   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5175   (gnus-group-position-point))
5176
5177 (defun gnus-group-best-unread-group (&optional exclude-group)
5178   "Go to the group with the highest level.
5179 If EXCLUDE-GROUP, do not go to that group."
5180   (interactive)
5181   (goto-char (point-min))
5182   (let ((best 100000)
5183         unread best-point)
5184     (while (setq unread (get-text-property (point) 'gnus-unread))
5185       (if (and (numberp unread) (> unread 0))
5186           (progn
5187             (if (and (< (get-text-property (point) 'gnus-level) best)
5188                      (or (not exclude-group)
5189                          (not (equal exclude-group (gnus-group-group-name)))))
5190                 (progn
5191                   (setq best (get-text-property (point) 'gnus-level))
5192                   (setq best-point (point))))))
5193       (forward-line 1))
5194     (if best-point (goto-char best-point))
5195     (gnus-summary-position-point)
5196     (and best-point (gnus-group-group-name))))
5197
5198 (defun gnus-group-first-unread-group ()
5199   "Go to the first group with unread articles."
5200   (interactive)
5201   (prog1
5202       (let ((opoint (point))
5203             unread)
5204         (goto-char (point-min))
5205         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5206                 (and (numberp unread)   ; Not a topic.
5207                      (not (zerop unread))) ; Has unread articles.
5208                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5209             (point)                     ; Success.
5210           (goto-char opoint)
5211           nil))                         ; Not success.
5212     (gnus-group-position-point)))
5213
5214 (defun gnus-group-enter-server-mode ()
5215   "Jump to the server buffer."
5216   (interactive)
5217   (gnus-enter-server-buffer))
5218
5219 (defun gnus-group-make-group (name &optional method address)
5220   "Add a new newsgroup.
5221 The user will be prompted for a NAME, for a select METHOD, and an
5222 ADDRESS."
5223   (interactive
5224    (cons
5225     (read-string "Group name: ")
5226     (let ((method
5227            (completing-read
5228             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5229             nil t)))
5230       (if (assoc method gnus-valid-select-methods)
5231           (list method
5232                 (if (memq 'prompt-address
5233                           (assoc method gnus-valid-select-methods))
5234                     (read-string "Address: ")
5235                   ""))
5236         (list method nil)))))
5237
5238   (save-excursion
5239     (set-buffer gnus-group-buffer)
5240     (let* ((meth (and method (if address (list (intern method) address)
5241                                method)))
5242            (nname (if method (gnus-group-prefixed-name name meth) name))
5243            info)
5244       (and (gnus-gethash nname gnus-newsrc-hashtb)
5245            (error "Group %s already exists" nname))
5246       (gnus-group-change-level
5247        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5248        gnus-level-default-subscribed gnus-level-killed
5249        (and (gnus-group-group-name)
5250             (gnus-gethash (gnus-group-group-name)
5251                           gnus-newsrc-hashtb))
5252        t)
5253       (gnus-set-active nname (cons 1 0))
5254       (or (gnus-ephemeral-group-p name)
5255           (gnus-dribble-enter
5256            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5257       (gnus-group-insert-group-line-info nname)
5258
5259       (if (assoc method gnus-valid-select-methods)
5260           (require (intern method)))
5261       (and (gnus-check-backend-function 'request-create-group nname)
5262            (gnus-request-create-group nname))
5263       t)))
5264
5265 (defun gnus-group-delete-group (group &optional force)
5266   "Delete the current group.
5267 If FORCE (the prefix) is non-nil, all the articles in the group will
5268 be deleted.  This is \"deleted\" as in \"removed forever from the face
5269 of the Earth\".  There is no undo."
5270   (interactive
5271    (list (gnus-group-group-name)
5272          current-prefix-arg))
5273   (or group (error "No group to rename"))
5274   (or (gnus-check-backend-function 'request-delete-group group)
5275       (error "This backend does not support group deletion"))
5276   (prog1
5277       (if (not (gnus-yes-or-no-p
5278                 (format
5279                  "Do you really want to delete %s%s? "
5280                  group (if force " and all its contents" ""))))
5281           () ; Whew!
5282         (gnus-message 6 "Deleting group %s..." group)
5283         (if (not (gnus-request-delete-group group force))
5284             (progn
5285               (gnus-message 3 "Couldn't delete group %s" group)
5286               (ding))
5287           (gnus-message 6 "Deleting group %s...done" group)
5288           (gnus-group-goto-group group)
5289           (gnus-group-kill-group 1 t)
5290           t))
5291     (gnus-group-position-point)))
5292
5293 (defun gnus-group-rename-group (group new-name)
5294   (interactive
5295    (list
5296     (gnus-group-group-name)
5297     (progn
5298       (or (gnus-check-backend-function
5299            'request-rename-group (gnus-group-group-name))
5300           (error "This backend does not support renaming groups"))
5301       (read-string "New group name: "))))
5302
5303   (or (gnus-check-backend-function 'request-rename-group group)
5304       (error "This backend does not support renaming groups"))
5305
5306   (or group (error "No group to rename"))
5307   (and (string-match "^[ \t]*$" new-name)
5308        (error "Not a valid group name"))
5309
5310   ;; We find the proper prefixed name.
5311   (setq new-name
5312         (gnus-group-prefixed-name
5313          (gnus-group-real-name new-name)
5314          (gnus-info-method (gnus-get-info group))))
5315
5316   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5317   (prog1
5318       (if (not (gnus-request-rename-group group new-name))
5319           (progn
5320             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5321             (ding))
5322         ;; We rename the group internally by killing it...
5323         (gnus-group-goto-group group)
5324         (gnus-group-kill-group)
5325         ;; ... changing its name ...
5326         (setcar (cdr (car gnus-list-of-killed-groups))
5327                 new-name)
5328         ;; ... and then yanking it.  Magic!
5329         (gnus-group-yank-group)
5330         (gnus-set-active new-name (gnus-active group))
5331         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5332         new-name)
5333     (gnus-group-position-point)))
5334
5335
5336 (defun gnus-group-edit-group (group &optional part)
5337   "Edit the group on the current line."
5338   (interactive (list (gnus-group-group-name)))
5339   (let ((done-func '(lambda ()
5340                       "Exit editing mode and update the information."
5341                       (interactive)
5342                       (gnus-group-edit-group-done 'part 'group)))
5343         (part (or part 'info))
5344         (winconf (current-window-configuration))
5345         info)
5346     (or group (error "No group on current line"))
5347     (or (setq info (gnus-get-info group))
5348         (error "Killed group; can't be edited"))
5349     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5350     (gnus-configure-windows 'edit-group)
5351     (gnus-add-current-to-buffer-list)
5352     (emacs-lisp-mode)
5353     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5354     (use-local-map (copy-keymap emacs-lisp-mode-map))
5355     (local-set-key "\C-c\C-c" done-func)
5356     (make-local-variable 'gnus-prev-winconf)
5357     (setq gnus-prev-winconf winconf)
5358     ;; We modify the func to let it know what part it is editing.
5359     (setcar (cdr (nth 4 done-func)) (list 'quote part))
5360     (setcar (cdr (cdr (nth 4 done-func))) group)
5361     (erase-buffer)
5362     (insert
5363      (cond
5364       ((eq part 'method)
5365        ";; Type `C-c C-c' after editing the select method.\n\n")
5366       ((eq part 'params)
5367        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5368       ((eq part 'info)
5369        ";; Type `C-c C-c' after editing the group info.\n\n")))
5370     (insert
5371      (pp-to-string
5372       (cond ((eq part 'method)
5373              (or (gnus-info-method info) "native"))
5374             ((eq part 'params)
5375              (gnus-info-params info))
5376             (t info)))
5377      "\n")))
5378
5379 (defun gnus-group-edit-group-method (group)
5380   "Edit the select method of GROUP."
5381   (interactive (list (gnus-group-group-name)))
5382   (gnus-group-edit-group group 'method))
5383
5384 (defun gnus-group-edit-group-parameters (group)
5385   "Edit the group parameters of GROUP."
5386   (interactive (list (gnus-group-group-name)))
5387   (gnus-group-edit-group group 'params))
5388
5389 (defun gnus-group-edit-group-done (part group)
5390   "Get info from buffer, update variables and jump to the group buffer."
5391   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5392   (goto-char (point-min))
5393   (let* ((form (read (current-buffer)))
5394          (winconf gnus-prev-winconf)
5395          (new-group (when (eq part 'info)
5396                       (if (or (not (nth 4 form))
5397                               (gnus-server-equal
5398                                gnus-select-method (nth 4 form)))
5399                           (gnus-group-real-name (car form))
5400                         (gnus-group-prefixed-name
5401                          (gnus-group-real-name (car form)) (nth 4 form))))))
5402     ;; Set the info.
5403     (if (eq part 'info)
5404         (progn
5405           (when new-group (setcar form new-group))
5406           (gnus-group-set-info form))
5407       (gnus-group-set-info form group part))
5408     (kill-buffer (current-buffer))
5409     (and winconf (set-window-configuration winconf))
5410     (set-buffer gnus-group-buffer)
5411     (when (and new-group
5412              (not (equal new-group group)))
5413       (when (gnus-group-goto-group group)
5414         (gnus-group-kill-group 1))
5415       (gnus-activate-group new-group))
5416     (gnus-group-update-group (or new-group group))
5417     (gnus-group-position-point)))
5418
5419 (defun gnus-group-make-help-group ()
5420   "Create the Gnus documentation group."
5421   (interactive)
5422   (let ((path load-path)
5423         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5424         file dir)
5425     (and (gnus-gethash name gnus-newsrc-hashtb)
5426          (error "Documentation group already exists"))
5427     (while path
5428       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5429             file nil)
5430       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5431                 (file-exists-p
5432                  (setq file (concat (file-name-directory
5433                                      (directory-file-name dir))
5434                                     "etc/gnus-tut.txt"))))
5435         (setq path nil)))
5436     (if (not file)
5437         (message "Couldn't find doc group")
5438       (gnus-group-make-group
5439        (gnus-group-real-name name)
5440        (list 'nndoc name
5441              (list 'nndoc-address file)
5442              (list 'nndoc-article-type 'mbox)))))
5443   (gnus-group-position-point))
5444
5445 (defun gnus-group-make-doc-group (file type)
5446   "Create a group that uses a single file as the source."
5447   (interactive
5448    (list (read-file-name "File name: ")
5449          (and current-prefix-arg 'ask)))
5450   (when (eq type 'ask)
5451     (let ((err "")
5452           char found)
5453       (while (not found)
5454         (message
5455          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5456          err)
5457         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5458                           ((= char ?b) 'babyl)
5459                           ((= char ?d) 'digest)
5460                           ((= char ?f) 'forward)
5461                           ((= char ?a) 'mmfd)
5462                           (t (setq err (format "%c unknown. " char))
5463                              nil))))
5464       (setq type found)))
5465   (let* ((file (expand-file-name file))
5466          (name (gnus-generate-new-group-name
5467                 (gnus-group-prefixed-name
5468                  (file-name-nondirectory file) '(nndoc "")))))
5469     (gnus-group-make-group
5470      (gnus-group-real-name name)
5471      (list 'nndoc name
5472            (list 'nndoc-address file)
5473            (list 'nndoc-article-type (or type 'guess))))
5474     (forward-line -1)
5475     (gnus-group-position-point)))
5476
5477 (defun gnus-group-make-archive-group (&optional all)
5478   "Create the (ding) Gnus archive group of the most recent articles.
5479 Given a prefix, create a full group."
5480   (interactive "P")
5481   (let ((group (gnus-group-prefixed-name
5482                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5483     (and (gnus-gethash group gnus-newsrc-hashtb)
5484          (error "Archive group already exists"))
5485     (gnus-group-make-group
5486      (gnus-group-real-name group)
5487      (list 'nndir (if all "hpc" "edu")
5488            (list 'nndir-directory
5489                  (if all gnus-group-archive-directory
5490                    gnus-group-recent-archive-directory)))))
5491   (forward-line -1)
5492   (gnus-group-position-point))
5493
5494 (defun gnus-group-make-directory-group (dir)
5495   "Create an nndir group.
5496 The user will be prompted for a directory.  The contents of this
5497 directory will be used as a newsgroup.  The directory should contain
5498 mail messages or news articles in files that have numeric names."
5499   (interactive
5500    (list (read-file-name "Create group from directory: ")))
5501   (or (file-exists-p dir) (error "No such directory"))
5502   (or (file-directory-p dir) (error "Not a directory"))
5503   (let ((ext "")
5504         (i 0)
5505         group)
5506     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5507       (setq group
5508             (gnus-group-prefixed-name
5509              (concat (file-name-as-directory (directory-file-name dir))
5510                      ext)
5511              '(nndir "")))
5512       (setq ext (format "<%d>" (setq i (1+ i)))))
5513     (gnus-group-make-group
5514      (gnus-group-real-name group)
5515      (list 'nndir group (list 'nndir-directory dir))))
5516   (forward-line -1)
5517   (gnus-group-position-point))
5518
5519 (defun gnus-group-make-kiboze-group (group address scores)
5520   "Create an nnkiboze group.
5521 The user will be prompted for a name, a regexp to match groups, and
5522 score file entries for articles to include in the group."
5523   (interactive
5524    (list
5525     (read-string "nnkiboze group name: ")
5526     (read-string "Source groups (regexp): ")
5527     (let ((headers (mapcar (lambda (group) (list group))
5528                            '("subject" "from" "number" "date" "message-id"
5529                              "references" "chars" "lines" "xref"
5530                              "followup" "all" "body" "head")))
5531           scores header regexp regexps)
5532       (while (not (equal "" (setq header (completing-read
5533                                           "Match on header: " headers nil t))))
5534         (setq regexps nil)
5535         (while (not (equal "" (setq regexp (read-string
5536                                             (format "Match on %s (string): "
5537                                                     header)))))
5538           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5539         (setq scores (cons (cons header regexps) scores)))
5540       scores)))
5541   (gnus-group-make-group group "nnkiboze" address)
5542   (save-excursion
5543     (gnus-set-work-buffer)
5544     (let (emacs-lisp-mode-hook)
5545       (pp scores (current-buffer)))
5546     (write-region (point-min) (point-max)
5547                   (gnus-score-file-name (concat "nnkiboze:" group))))
5548   (forward-line -1)
5549   (gnus-group-position-point))
5550
5551 (defun gnus-group-add-to-virtual (n vgroup)
5552   "Add the current group to a virtual group."
5553   (interactive
5554    (list current-prefix-arg
5555          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5556                           "nnvirtual:")))
5557   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5558       (error "%s is not an nnvirtual group" vgroup))
5559   (let* ((groups (gnus-group-process-prefix n))
5560          (method (gnus-info-method (gnus-get-info vgroup))))
5561     (setcar (cdr method)
5562             (concat
5563              (nth 1 method) "\\|"
5564              (mapconcat
5565               (lambda (s)
5566                 (gnus-group-remove-mark s)
5567                 (concat "\\(^" (regexp-quote s) "$\\)"))
5568               groups "\\|"))))
5569   (gnus-group-position-point))
5570
5571 (defun gnus-group-make-empty-virtual (group)
5572   "Create a new, fresh, empty virtual group."
5573   (interactive "sCreate new, empty virtual group: ")
5574   (let* ((method (list 'nnvirtual "^$"))
5575          (pgroup (gnus-group-prefixed-name group method)))
5576     ;; Check whether it exists already.
5577     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5578          (error "Group %s already exists." pgroup))
5579     ;; Subscribe the new group after the group on the current line.
5580     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5581     (gnus-group-update-group pgroup)
5582     (forward-line -1)
5583     (gnus-group-position-point)))
5584
5585 (defun gnus-group-enter-directory (dir)
5586   "Enter an ephemeral nneething group."
5587   (interactive "DDirectory to read: ")
5588   (let* ((method (list 'nneething dir))
5589          (leaf (gnus-group-prefixed-name
5590                 (file-name-nondirectory (directory-file-name dir))
5591                 method))
5592          (name (gnus-generate-new-group-name leaf)))
5593     (let ((nneething-read-only t))
5594       (or (gnus-group-read-ephemeral-group
5595            name method t
5596            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5597                                       'summary 'group)))
5598           (error "Couldn't enter %s" dir)))))
5599
5600 ;; Group sorting commands
5601 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5602
5603 (defun gnus-group-sort-groups (func &optional reverse)
5604   "Sort the group buffer according to FUNC.
5605 If REVERSE, reverse the sorting order."
5606   (interactive (list gnus-group-sort-function
5607                      current-prefix-arg))
5608   (let ((func (cond 
5609                ((not (listp func)) func)
5610                ((null func) func)
5611                ((= 1 (length func)) (car func))
5612                (t `(lambda (t1 t2)
5613                      ,(gnus-make-sort-function 
5614                        (reverse func)))))))
5615     ;; We peel off the dummy group from the alist.
5616     (when func
5617       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5618         (pop gnus-newsrc-alist))
5619       ;; Do the sorting.
5620       (setq gnus-newsrc-alist
5621             (sort gnus-newsrc-alist func))
5622       (when reverse
5623         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5624       ;; Regenerate the hash table.
5625       (gnus-make-hashtable-from-newsrc-alist)
5626       (gnus-group-list-groups))))
5627
5628 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5629   "Sort the group buffer alphabetically by group name.
5630 If REVERSE, sort in reverse order."
5631   (interactive "P")
5632   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5633
5634 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5635   "Sort the group buffer by number of unread articles.
5636 If REVERSE, sort in reverse order."
5637   (interactive "P")
5638   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5639
5640 (defun gnus-group-sort-groups-by-level (&optional reverse)
5641   "Sort the group buffer by group level.
5642 If REVERSE, sort in reverse order."
5643   (interactive "P")
5644   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5645
5646 (defun gnus-group-sort-groups-by-score (&optional reverse)
5647   "Sort the group buffer by group score.
5648 If REVERSE, sort in reverse order."
5649   (interactive "P")
5650   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5651
5652 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5653   "Sort the group buffer by group rank.
5654 If REVERSE, sort in reverse order."
5655   (interactive "P")
5656   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5657
5658 (defun gnus-group-sort-groups-by-method (&optional reverse)
5659   "Sort the group buffer alphabetically by backend name.
5660 If REVERSE, sort in reverse order."
5661   (interactive "P")
5662   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5663
5664 (defun gnus-group-sort-by-alphabet (info1 info2)
5665   "Sort alphabetically."
5666   (string< (gnus-info-group info1) (gnus-info-group info2)))
5667
5668 (defun gnus-group-sort-by-unread (info1 info2)
5669   "Sort by number of unread articles."
5670   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5671         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5672     (< (or (and (numberp n1) n1) 0)
5673        (or (and (numberp n2) n2) 0))))
5674
5675 (defun gnus-group-sort-by-level (info1 info2)
5676   "Sort by level."
5677   (< (gnus-info-level info1) (gnus-info-level info2)))
5678
5679 (defun gnus-group-sort-by-method (info1 info2)
5680   "Sort alphabetically by backend name."
5681   (string< (symbol-name (car (gnus-find-method-for-group
5682                               (gnus-info-group info1) info1)))
5683            (symbol-name (car (gnus-find-method-for-group
5684                               (gnus-info-group info2) info2)))))
5685
5686 (defun gnus-group-sort-by-score (info1 info2)
5687   "Sort by group score."
5688   (< (gnus-info-score info1) (gnus-info-score info2)))
5689
5690 (defun gnus-group-sort-by-rank (info1 info2)
5691   "Sort by level and score."
5692   (let ((level1 (gnus-info-level info1))
5693         (level2 (gnus-info-level info2)))
5694     (or (< level1 level2)
5695         (and (= level1 level2)
5696              (< (gnus-info-score info1) (gnus-info-score info2))))))
5697
5698 ;; Group catching up.
5699
5700 (defun gnus-group-catchup-current (&optional n all)
5701   "Mark all articles not marked as unread in current newsgroup as read.
5702 If prefix argument N is numeric, the ARG next newsgroups will be
5703 caught up.  If ALL is non-nil, marked articles will also be marked as
5704 read.  Cross references (Xref: header) of articles are ignored.
5705 The difference between N and actual number of newsgroups that were
5706 caught up is returned."
5707   (interactive "P")
5708   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5709                gnus-expert-user
5710                (gnus-y-or-n-p
5711                 (if all
5712                     "Do you really want to mark all articles as read? "
5713                   "Mark all unread articles as read? "))))
5714       n
5715     (let ((groups (gnus-group-process-prefix n))
5716           (ret 0))
5717       (while groups
5718         ;; Virtual groups have to be given special treatment.
5719         (let ((method (gnus-find-method-for-group (car groups))))
5720           (if (eq 'nnvirtual (car method))
5721               (nnvirtual-catchup-group
5722                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5723         (gnus-group-remove-mark (car groups))
5724         (if (prog1
5725                 (gnus-group-goto-group (car groups))
5726               (gnus-group-catchup (car groups) all))
5727             (gnus-group-update-group-line)
5728           (setq ret (1+ ret)))
5729         (setq groups (cdr groups)))
5730       (gnus-group-next-unread-group 1)
5731       ret)))
5732
5733 (defun gnus-group-catchup-current-all (&optional n)
5734   "Mark all articles in current newsgroup as read.
5735 Cross references (Xref: header) of articles are ignored."
5736   (interactive "P")
5737   (gnus-group-catchup-current n 'all))
5738
5739 (defun gnus-group-catchup (group &optional all)
5740   "Mark all articles in GROUP as read.
5741 If ALL is non-nil, all articles are marked as read.
5742 The return value is the number of articles that were marked as read,
5743 or nil if no action could be taken."
5744   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5745          (num (car entry)))
5746     ;; Do the updating only if the newsgroup isn't killed.
5747     (if (not (numberp (car entry)))
5748         (gnus-message 1 "Can't catch up; non-active group")
5749       ;; Do auto-expirable marks if that's required.
5750       (when (gnus-group-auto-expirable-p group)
5751         (gnus-add-marked-articles
5752          group 'expire (gnus-list-of-unread-articles group))
5753         (when all
5754           (let ((marks (nth 3 (nth 2 entry))))
5755             (gnus-add-marked-articles
5756              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5757             (gnus-add-marked-articles
5758              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5759       (when entry
5760         (gnus-update-read-articles group nil)
5761         ;; Also nix out the lists of marks and dormants.
5762         (when all
5763           (gnus-add-marked-articles group 'tick nil nil 'force)
5764           (gnus-add-marked-articles group 'dormant nil nil 'force))
5765         (run-hooks 'gnus-group-catchup-group-hook)
5766         num))))
5767
5768 (defun gnus-group-expire-articles (&optional n)
5769   "Expire all expirable articles in the current newsgroup."
5770   (interactive "P")
5771   (let ((groups (gnus-group-process-prefix n))
5772         group)
5773     (unless groups
5774       (error "No groups to expire"))
5775     (while (setq group (pop groups))
5776       (gnus-group-remove-mark group)
5777       (when (gnus-check-backend-function 'request-expire-articles group)
5778         (gnus-message 6 "Expiring articles in %s..." group)
5779         (let* ((info (gnus-get-info group))
5780                (expirable (if (gnus-group-total-expirable-p group)
5781                               (cons nil (gnus-list-of-read-articles group))
5782                             (assq 'expire (gnus-info-marks info))))
5783                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5784           (when expirable
5785             (setcdr expirable
5786                     (gnus-compress-sequence
5787                      (if expiry-wait
5788                          (let ((nnmail-expiry-wait-function nil)
5789                                (nnmail-expiry-wait expiry-wait))
5790                            (gnus-request-expire-articles
5791                             (gnus-uncompress-sequence (cdr expirable)) group))
5792                        (gnus-request-expire-articles
5793                         (gnus-uncompress-sequence (cdr expirable))
5794                         group)))))
5795           (gnus-message 6 "Expiring articles in %s...done" group)))
5796       (gnus-group-position-point))))
5797
5798
5799 (defun gnus-group-expire-all-groups ()
5800   "Expire all expirable articles in all newsgroups."
5801   (interactive)
5802   (save-excursion
5803     (gnus-message 5 "Expiring...")
5804     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5805                                      (cdr gnus-newsrc-alist))))
5806       (gnus-group-expire-articles nil)))
5807   (gnus-group-position-point)
5808   (gnus-message 5 "Expiring...done"))
5809
5810 (defun gnus-group-set-current-level (n level)
5811   "Set the level of the next N groups to LEVEL."
5812   (interactive
5813    (list
5814     current-prefix-arg
5815     (string-to-int
5816      (let ((s (read-string
5817                (format "Level (default %s): " (gnus-group-group-level)))))
5818        (if (string-match "^\\s-*$" s)
5819            (int-to-string (gnus-group-group-level))
5820          s)))))
5821   (or (and (>= level 1) (<= level gnus-level-killed))
5822       (error "Illegal level: %d" level))
5823   (let ((groups (gnus-group-process-prefix n))
5824         group)
5825     (while groups
5826       (setq group (car groups)
5827             groups (cdr groups))
5828       (gnus-group-remove-mark group)
5829       (gnus-message 6 "Changed level of %s from %d to %d"
5830                     group (or (gnus-group-group-level) gnus-level-killed)
5831                     level)
5832       (gnus-group-change-level
5833        group level (or (gnus-group-group-level) gnus-level-killed))
5834       (gnus-group-update-group-line)))
5835   (gnus-group-position-point))
5836
5837 (defun gnus-group-unsubscribe-current-group (&optional n)
5838   "Toggle subscription of the current group.
5839 If given numerical prefix, toggle the N next groups."
5840   (interactive "P")
5841   (let ((groups (gnus-group-process-prefix n))
5842         group)
5843     (while groups
5844       (setq group (car groups)
5845             groups (cdr groups))
5846       (gnus-group-remove-mark group)
5847       (gnus-group-unsubscribe-group
5848        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5849                  gnus-level-default-unsubscribed
5850                gnus-level-default-subscribed) t)
5851       (gnus-group-update-group-line))
5852     (gnus-group-next-group 1)))
5853
5854 (defun gnus-group-unsubscribe-group (group &optional level silent)
5855   "Toggle subscription to GROUP.
5856 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5857 group line."
5858   (interactive
5859    (list (completing-read
5860           "Group: " gnus-active-hashtb nil
5861           (memq gnus-select-method gnus-have-read-active-file))))
5862   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5863     (cond
5864      ((string-match "^[ \t]$" group)
5865       (error "Empty group name"))
5866      (newsrc
5867       ;; Toggle subscription flag.
5868       (gnus-group-change-level
5869        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
5870                                       gnus-level-subscribed)
5871                                   (1+ gnus-level-subscribed)
5872                                 gnus-level-default-subscribed)))
5873       (unless silent
5874         (gnus-group-update-group group)))
5875      ((and (stringp group)
5876            (or (not (memq gnus-select-method gnus-have-read-active-file))
5877                (gnus-active group)))
5878       ;; Add new newsgroup.
5879       (gnus-group-change-level
5880        group
5881        (if level level gnus-level-default-subscribed)
5882        (or (and (member group gnus-zombie-list)
5883                 gnus-level-zombie)
5884            gnus-level-killed)
5885        (and (gnus-group-group-name)
5886             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5887       (unless silent
5888         (gnus-group-update-group group)))
5889      (t (error "No such newsgroup: %s" group)))
5890     (gnus-group-position-point)))
5891
5892 (defun gnus-group-transpose-groups (n)
5893   "Move the current newsgroup up N places.
5894 If given a negative prefix, move down instead.  The difference between
5895 N and the number of steps taken is returned."
5896   (interactive "p")
5897   (or (gnus-group-group-name)
5898       (error "No group on current line"))
5899   (gnus-group-kill-group 1)
5900   (prog1
5901       (forward-line (- n))
5902     (gnus-group-yank-group)
5903     (gnus-group-position-point)))
5904
5905 (defun gnus-group-kill-all-zombies ()
5906   "Kill all zombie newsgroups."
5907   (interactive)
5908   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
5909   (setq gnus-zombie-list nil)
5910   (gnus-group-list-groups))
5911
5912 (defun gnus-group-kill-region (begin end)
5913   "Kill newsgroups in current region (excluding current point).
5914 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
5915   (interactive "r")
5916   (let ((lines
5917          ;; Count lines.
5918          (save-excursion
5919            (count-lines
5920             (progn
5921               (goto-char begin)
5922               (beginning-of-line)
5923               (point))
5924             (progn
5925               (goto-char end)
5926               (beginning-of-line)
5927               (point))))))
5928     (goto-char begin)
5929     (beginning-of-line)                 ;Important when LINES < 1
5930     (gnus-group-kill-group lines)))
5931
5932 (defun gnus-group-kill-group (&optional n discard)
5933   "Kill the next N groups.
5934 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
5935 However, only groups that were alive can be yanked; already killed
5936 groups or zombie groups can't be yanked.
5937 The return value is the name of the group that was killed, or a list
5938 of groups killed."
5939   (interactive "P")
5940   (let ((buffer-read-only nil)
5941         (groups (gnus-group-process-prefix n))
5942         group entry level out)
5943     (if (< (length groups) 10)
5944         ;; This is faster when there are few groups.
5945         (while groups
5946           (push (setq group (pop groups)) out)
5947           (gnus-group-remove-mark group)
5948           (setq level (gnus-group-group-level))
5949           (gnus-delete-line)
5950           (if (and (not discard)
5951                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
5952               (setq gnus-list-of-killed-groups
5953                     (cons (cons (car entry) (nth 2 entry))
5954                           gnus-list-of-killed-groups)))
5955           (gnus-group-change-level
5956            (if entry entry group) gnus-level-killed (if entry nil level)))
5957       ;; If there are lots and lots of groups to be killed, we use
5958       ;; this thing instead.
5959       (let (entry)
5960         (setq groups (nreverse groups))
5961         (while groups
5962           (gnus-group-remove-mark (car groups))
5963           (gnus-delete-line)
5964           (when (setq entry (gnus-gethash (pop groups) gnus-newsrc-hashtb))
5965             (push (cons (car entry) (nth 2 entry))
5966                   gnus-list-of-killed-groups)
5967             (setcdr (cdr entry) (cdr (cdr (cdr entry))))))
5968         (gnus-make-hashtable-from-newsrc-alist)))
5969
5970     (gnus-group-position-point)
5971     (if (< (length out) 2) (car out) (nreverse out))))
5972
5973 (defun gnus-group-yank-group (&optional arg)
5974   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
5975 inserting it before the current newsgroup.  The numeric ARG specifies
5976 how many newsgroups are to be yanked.  The name of the newsgroup yanked
5977 is returned, or (if several groups are yanked) a list of yanked groups
5978 is returned."
5979   (interactive "p")
5980   (setq arg (or arg 1))
5981   (let (info group prev out)
5982     (while (>= (decf arg) 0)
5983       (if (not (setq info (pop gnus-list-of-killed-groups)))
5984           (error "No more newsgroups to yank"))
5985       (push (setq group (nth 1 info)) out)
5986       ;; Find which newsgroup to insert this one before - search
5987       ;; backward until something suitable is found.  If there are no
5988       ;; other newsgroups in this buffer, just make this newsgroup the
5989       ;; first newsgroup.
5990       (setq prev (gnus-group-group-name))
5991       (gnus-group-change-level
5992        info (nth 2 info) gnus-level-killed
5993        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
5994        t)
5995       (gnus-group-insert-group-line-info group))
5996     (forward-line -1)
5997     (gnus-group-position-point)
5998     (if (< (length out) 2) (car out) (nreverse out))))
5999
6000 (defun gnus-group-kill-level (level)
6001   "Kill all groups that is on a certain LEVEL."
6002   (interactive "nKill all groups on level: ")
6003   (cond
6004    ((= level gnus-level-zombie)
6005     (setq gnus-killed-list
6006           (nconc gnus-zombie-list gnus-killed-list))
6007     (setq gnus-zombie-list nil))
6008    ((and (< level gnus-level-zombie)
6009          (> level 0)
6010          (or gnus-expert-user
6011              (gnus-yes-or-no-p
6012               (format
6013                "Do you really want to kill all groups on level %d? "
6014                level))))
6015     (let* ((prev gnus-newsrc-alist)
6016            (alist (cdr prev)))
6017       (while alist
6018         (if (= (gnus-info-level level) level)
6019             (setcdr prev (cdr alist))
6020           (setq prev alist))
6021         (setq alist (cdr alist)))
6022       (gnus-make-hashtable-from-newsrc-alist)
6023       (gnus-group-list-groups)))
6024    (t
6025     (error "Can't kill; illegal level: %d" level))))
6026
6027 (defun gnus-group-list-all-groups (&optional arg)
6028   "List all newsgroups with level ARG or lower.
6029 Default is gnus-level-unsubscribed, which lists all subscribed and most
6030 unsubscribed groups."
6031   (interactive "P")
6032   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6033
6034 ;; Redefine this to list ALL killed groups if prefix arg used.
6035 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6036 (defun gnus-group-list-killed (&optional arg)
6037   "List all killed newsgroups in the group buffer.
6038 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6039 entail asking the server for the groups."
6040   (interactive "P")
6041   ;; Find all possible killed newsgroups if arg.
6042   (when arg
6043     ;; First make sure active file has been read.
6044     (unless gnus-have-read-active-file
6045       (let ((gnus-read-active-file t))
6046         (gnus-read-active-file)))
6047     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6048     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6049     (mapatoms
6050      (lambda (sym)
6051        (let ((groups 0)
6052              (group (symbol-name sym)))
6053          (if (or (null group)
6054                  (gnus-gethash group gnus-killed-hashtb)
6055                  (gnus-gethash group gnus-newsrc-hashtb))
6056              ()
6057            (let ((do-sub (gnus-matches-options-n group)))
6058              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6059                  ()
6060                (setq groups (1+ groups))
6061                (setq gnus-killed-list
6062                      (cons group gnus-killed-list))
6063                (gnus-sethash group group gnus-killed-hashtb))))))
6064      gnus-active-hashtb))
6065   (if (not gnus-killed-list)
6066       (gnus-message 6 "No killed groups")
6067     (let (gnus-group-list-mode)
6068       (funcall gnus-group-prepare-function
6069                gnus-level-killed t gnus-level-killed))
6070     (goto-char (point-min)))
6071   (gnus-group-position-point))
6072
6073 (defun gnus-group-list-zombies ()
6074   "List all zombie newsgroups in the group buffer."
6075   (interactive)
6076   (if (not gnus-zombie-list)
6077       (gnus-message 6 "No zombie groups")
6078     (let (gnus-group-list-mode)
6079       (funcall gnus-group-prepare-function
6080                gnus-level-zombie t gnus-level-zombie))
6081     (goto-char (point-min)))
6082   (gnus-group-position-point))
6083
6084 (defun gnus-group-list-active ()
6085   "List all groups that are available from the server(s)."
6086   (interactive)
6087   ;; First we make sure that we have really read the active file.
6088   (unless gnus-have-read-active-file
6089     (let ((gnus-read-active-file t))
6090       (gnus-read-active-file)))
6091   ;; Find all groups and sort them.
6092   (let ((groups
6093          (sort
6094           (let (list)
6095             (mapatoms
6096              (lambda (sym)
6097                (and (symbol-value sym)
6098                     (setq list (cons (symbol-name sym) list))))
6099              gnus-active-hashtb)
6100             list)
6101           'string<))
6102         (buffer-read-only nil))
6103     (erase-buffer)
6104     (while groups
6105       (gnus-group-insert-group-line-info (car groups))
6106       (setq groups (cdr groups)))
6107     (goto-char (point-min))))
6108
6109 (defun gnus-activate-all-groups (level)
6110   "Activate absolutely all groups."
6111   (interactive (list 7))
6112   (let ((gnus-activate-level level)
6113         (gnus-activate-foreign-newsgroups level))
6114     (gnus-group-get-new-news)))
6115
6116 (defun gnus-group-get-new-news (&optional arg)
6117   "Get newly arrived articles.
6118 If ARG is a number, it specifies which levels you are interested in
6119 re-scanning.  If ARG is non-nil and not a number, this will force
6120 \"hard\" re-reading of the active files from all servers."
6121   (interactive "P")
6122   (run-hooks 'gnus-get-new-news-hook)
6123   ;; We might read in new NoCeM messages here.
6124   (and gnus-use-nocem (gnus-nocem-scan-groups))
6125   ;; If ARG is not a number, then we read the active file.
6126   (and arg
6127        (not (numberp arg))
6128        (progn
6129          (let ((gnus-read-active-file t))
6130            (gnus-read-active-file))
6131          (setq arg nil)))
6132
6133   (setq arg (gnus-group-default-level arg t))
6134   (if (and gnus-read-active-file (not arg))
6135       (progn
6136         (gnus-read-active-file)
6137         (gnus-get-unread-articles arg))
6138     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6139       (gnus-get-unread-articles arg)))
6140   (gnus-group-list-groups))
6141
6142 (defun gnus-group-get-new-news-this-group (&optional n)
6143   "Check for newly arrived news in the current group (and the N-1 next groups).
6144 The difference between N and the number of newsgroup checked is returned.
6145 If N is negative, this group and the N-1 previous groups will be checked."
6146   (interactive "P")
6147   (let* ((groups (gnus-group-process-prefix n))
6148          (ret (if (numberp n) (- n (length groups)) 0))
6149          group)
6150     (while groups
6151       (setq group (car groups)
6152             groups (cdr groups))
6153       (gnus-group-remove-mark group)
6154       (unless (gnus-get-new-news-in-group group)
6155         (ding)
6156         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6157     (when gnus-goto-next-group-when-activating
6158       (gnus-group-next-unread-group 1 t))
6159     (gnus-summary-position-point)
6160     ret))
6161
6162 (defun gnus-get-new-news-in-group (group)
6163   (when (and group (gnus-activate-group group 'scan))
6164     (gnus-get-unread-articles-in-group
6165      (gnus-get-info group) (gnus-active group) t)
6166     (when (gnus-group-goto-group group)
6167       (gnus-group-update-group-line))
6168     t))
6169
6170 (defun gnus-group-fetch-faq (group &optional faq-dir)
6171   "Fetch the FAQ for the current group."
6172   (interactive
6173    (list
6174     (gnus-group-real-name (gnus-group-group-name))
6175     (cond (current-prefix-arg
6176            (completing-read
6177             "Faq dir: " (and (listp gnus-group-faq-directory)
6178                              gnus-group-faq-directory))))))
6179   (or faq-dir
6180       (setq faq-dir (if (listp gnus-group-faq-directory)
6181                         (car gnus-group-faq-directory)
6182                       gnus-group-faq-directory)))
6183   (or group (error "No group name given"))
6184   (let ((file (concat (file-name-as-directory faq-dir)
6185                       (gnus-group-real-name group))))
6186     (if (not (file-exists-p file))
6187         (error "No such file: %s" file)
6188       (find-file file))))
6189
6190 (defun gnus-group-describe-group (force &optional group)
6191   "Display a description of the current newsgroup."
6192   (interactive (list current-prefix-arg (gnus-group-group-name)))
6193   (and force (setq gnus-description-hashtb nil))
6194   (let ((method (gnus-find-method-for-group group))
6195         desc)
6196     (or group (error "No group name given"))
6197     (and (or (and gnus-description-hashtb
6198                   ;; We check whether this group's method has been
6199                   ;; queried for a description file.
6200                   (gnus-gethash
6201                    (gnus-group-prefixed-name "" method)
6202                    gnus-description-hashtb))
6203              (setq desc (gnus-group-get-description group))
6204              (gnus-read-descriptions-file method))
6205          (message
6206           (or desc (gnus-gethash group gnus-description-hashtb)
6207               "No description available")))))
6208
6209 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6210 (defun gnus-group-describe-all-groups (&optional force)
6211   "Pop up a buffer with descriptions of all newsgroups."
6212   (interactive "P")
6213   (and force (setq gnus-description-hashtb nil))
6214   (if (not (or gnus-description-hashtb
6215                (gnus-read-all-descriptions-files)))
6216       (error "Couldn't request descriptions file"))
6217   (let ((buffer-read-only nil)
6218         b)
6219     (erase-buffer)
6220     (mapatoms
6221      (lambda (group)
6222        (setq b (point))
6223        (insert (format "      *: %-20s %s\n" (symbol-name group)
6224                        (symbol-value group)))
6225        (add-text-properties
6226         b (1+ b) (list 'gnus-group group
6227                        'gnus-unread t 'gnus-marked nil
6228                        'gnus-level (1+ gnus-level-subscribed))))
6229      gnus-description-hashtb)
6230     (goto-char (point-min))
6231     (gnus-group-position-point)))
6232
6233 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6234 (defun gnus-group-apropos (regexp &optional search-description)
6235   "List all newsgroups that have names that match a regexp."
6236   (interactive "sGnus apropos (regexp): ")
6237   (let ((prev "")
6238         (obuf (current-buffer))
6239         groups des)
6240     ;; Go through all newsgroups that are known to Gnus.
6241     (mapatoms
6242      (lambda (group)
6243        (and (symbol-name group)
6244             (string-match regexp (symbol-name group))
6245             (setq groups (cons (symbol-name group) groups))))
6246      gnus-active-hashtb)
6247     ;; Go through all descriptions that are known to Gnus.
6248     (if search-description
6249         (mapatoms
6250          (lambda (group)
6251            (and (string-match regexp (symbol-value group))
6252                 (gnus-active (symbol-name group))
6253                 (setq groups (cons (symbol-name group) groups))))
6254          gnus-description-hashtb))
6255     (if (not groups)
6256         (gnus-message 3 "No groups matched \"%s\"." regexp)
6257       ;; Print out all the groups.
6258       (save-excursion
6259         (pop-to-buffer "*Gnus Help*")
6260         (buffer-disable-undo (current-buffer))
6261         (erase-buffer)
6262         (setq groups (sort groups 'string<))
6263         (while groups
6264           ;; Groups may be entered twice into the list of groups.
6265           (if (not (string= (car groups) prev))
6266               (progn
6267                 (insert (setq prev (car groups)) "\n")
6268                 (if (and gnus-description-hashtb
6269                          (setq des (gnus-gethash (car groups)
6270                                                  gnus-description-hashtb)))
6271                     (insert "  " des "\n"))))
6272           (setq groups (cdr groups)))
6273         (goto-char (point-min))))
6274     (pop-to-buffer obuf)))
6275
6276 (defun gnus-group-description-apropos (regexp)
6277   "List all newsgroups that have names or descriptions that match a regexp."
6278   (interactive "sGnus description apropos (regexp): ")
6279   (if (not (or gnus-description-hashtb
6280                (gnus-read-all-descriptions-files)))
6281       (error "Couldn't request descriptions file"))
6282   (gnus-group-apropos regexp t))
6283
6284 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6285 (defun gnus-group-list-matching (level regexp &optional all lowest)
6286   "List all groups with unread articles that match REGEXP.
6287 If the prefix LEVEL is non-nil, it should be a number that says which
6288 level to cut off listing groups.
6289 If ALL, also list groups with no unread articles.
6290 If LOWEST, don't list groups with level lower than LOWEST."
6291   (interactive "P\nsList newsgroups matching: ")
6292   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6293                            all (or lowest 1) regexp)
6294   (goto-char (point-min))
6295   (gnus-group-position-point))
6296
6297 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6298   "List all groups that match REGEXP.
6299 If the prefix LEVEL is non-nil, it should be a number that says which
6300 level to cut off listing groups.
6301 If LOWEST, don't list groups with level lower than LOWEST."
6302   (interactive "P\nsList newsgroups matching: ")
6303   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6304
6305 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6306 (defun gnus-group-save-newsrc (&optional force)
6307   "Save the Gnus startup files.
6308 If FORCE, force saving whether it is necessary or not."
6309   (interactive "P")
6310   (gnus-save-newsrc-file force))
6311
6312 (defun gnus-group-restart (&optional arg)
6313   "Force Gnus to read the .newsrc file."
6314   (interactive "P")
6315   (gnus-save-newsrc-file)
6316   (gnus-setup-news 'force)
6317   (gnus-group-list-groups arg))
6318
6319 (defun gnus-group-read-init-file ()
6320   "Read the Gnus elisp init file."
6321   (interactive)
6322   (gnus-read-init-file))
6323
6324 (defun gnus-group-check-bogus-groups (&optional silent)
6325   "Check bogus newsgroups.
6326 If given a prefix, don't ask for confirmation before removing a bogus
6327 group."
6328   (interactive "P")
6329   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6330   (gnus-group-list-groups))
6331
6332 (defun gnus-group-edit-global-kill (&optional article group)
6333   "Edit the global kill file.
6334 If GROUP, edit that local kill file instead."
6335   (interactive "P")
6336   (setq gnus-current-kill-article article)
6337   (gnus-kill-file-edit-file group)
6338   (gnus-message
6339    6
6340    (substitute-command-keys
6341     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6342             (if group "local" "global")))))
6343
6344 (defun gnus-group-edit-local-kill (article group)
6345   "Edit a local kill file."
6346   (interactive (list nil (gnus-group-group-name)))
6347   (gnus-group-edit-global-kill article group))
6348
6349 (defun gnus-group-force-update ()
6350   "Update `.newsrc' file."
6351   (interactive)
6352   (gnus-save-newsrc-file))
6353
6354 (defun gnus-group-suspend ()
6355   "Suspend the current Gnus session.
6356 In fact, cleanup buffers except for group mode buffer.
6357 The hook gnus-suspend-gnus-hook is called before actually suspending."
6358   (interactive)
6359   (run-hooks 'gnus-suspend-gnus-hook)
6360   ;; Kill Gnus buffers except for group mode buffer.
6361   (let ((group-buf (get-buffer gnus-group-buffer)))
6362     ;; Do this on a separate list in case the user does a ^G before we finish
6363     (let ((gnus-buffer-list
6364            (delq group-buf (delq gnus-dribble-buffer
6365                                  (append gnus-buffer-list nil)))))
6366       (while gnus-buffer-list
6367         (gnus-kill-buffer (car gnus-buffer-list))
6368         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6369     (if group-buf
6370         (progn
6371           (setq gnus-buffer-list (list group-buf))
6372           (bury-buffer group-buf)
6373           (delete-windows-on group-buf t)))))
6374
6375 (defun gnus-group-clear-dribble ()
6376   "Clear all information from the dribble buffer."
6377   (interactive)
6378   (gnus-dribble-clear)
6379   (gnus-message 7 "Cleared dribble buffer"))
6380
6381 (defun gnus-group-exit ()
6382   "Quit reading news after updating .newsrc.eld and .newsrc.
6383 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6384   (interactive)
6385   (if (or noninteractive                ;For gnus-batch-kill
6386           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6387           (not gnus-interactive-exit)   ;Without confirmation
6388           gnus-expert-user
6389           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6390       (progn
6391         (run-hooks 'gnus-exit-gnus-hook)
6392         ;; Offer to save data from non-quitted summary buffers.
6393         (gnus-offer-save-summaries)
6394         ;; Save the newsrc file(s).
6395         (gnus-save-newsrc-file)
6396         ;; Kill-em-all.
6397         (gnus-close-backends)
6398         ;; Shut down the cache.
6399         (when gnus-use-cache
6400           (gnus-cache-close))
6401         ;; Reset everything.
6402         (gnus-clear-system))))
6403
6404 (defun gnus-close-backends ()
6405   ;; Send a close request to all backends that support such a request.
6406   (let ((methods gnus-valid-select-methods)
6407         func)
6408     (while methods
6409       (if (fboundp (setq func (intern (concat (car (car methods))
6410                                               "-request-close"))))
6411           (funcall func))
6412       (setq methods (cdr methods)))))
6413
6414 (defun gnus-group-quit ()
6415   "Quit reading news without updating .newsrc.eld or .newsrc.
6416 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6417   (interactive)
6418   (when (or noninteractive              ;For gnus-batch-kill
6419             (zerop (buffer-size))
6420             (not (gnus-server-opened gnus-select-method))
6421             gnus-expert-user
6422             (not gnus-current-startup-file)
6423             (gnus-yes-or-no-p
6424              (format "Quit reading news without saving %s? "
6425                      (file-name-nondirectory gnus-current-startup-file))))
6426     (run-hooks 'gnus-exit-gnus-hook)
6427     (if gnus-use-full-window
6428         (delete-other-windows)
6429       (gnus-remove-some-windows))
6430     (gnus-dribble-save)
6431     (gnus-close-backends)
6432     ;; Shut down the cache.
6433     (when gnus-use-cache
6434       (gnus-cache-close))
6435     (gnus-clear-system)))
6436
6437 (defun gnus-offer-save-summaries ()
6438   "Offer to save all active summary buffers."
6439   (save-excursion
6440     (let ((buflist (buffer-list))
6441           buffers bufname)
6442       ;; Go through all buffers and find all summaries.
6443       (while buflist
6444         (and (setq bufname (buffer-name (car buflist)))
6445              (string-match "Summary" bufname)
6446              (save-excursion
6447                (set-buffer bufname)
6448                ;; We check that this is, indeed, a summary buffer.
6449                (and (eq major-mode 'gnus-summary-mode)
6450                     ;; Also make sure this isn't bogus.
6451                     gnus-newsgroup-prepared))
6452              (push bufname buffers))
6453         (setq buflist (cdr buflist)))
6454       ;; Go through all these summary buffers and offer to save them.
6455       (when buffers
6456         (map-y-or-n-p
6457          "Update summary buffer %s? "
6458          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6459          buffers)))))
6460
6461 (defun gnus-group-describe-briefly ()
6462   "Give a one line description of the group mode commands."
6463   (interactive)
6464   (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")))
6465
6466 (defun gnus-group-browse-foreign-server (method)
6467   "Browse a foreign news server.
6468 If called interactively, this function will ask for a select method
6469  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6470 If not, METHOD should be a list where the first element is the method
6471 and the second element is the address."
6472   (interactive
6473    (list (let ((how (completing-read
6474                      "Which backend: "
6475                      (append gnus-valid-select-methods gnus-server-alist)
6476                      nil t (cons "nntp" 0))))
6477            ;; We either got a backend name or a virtual server name.
6478            ;; If the first, we also need an address.
6479            (if (assoc how gnus-valid-select-methods)
6480                (list (intern how)
6481                      ;; Suggested by mapjph@bath.ac.uk.
6482                      (completing-read
6483                       "Address: "
6484                       (mapcar (lambda (server) (list server))
6485                               gnus-secondary-servers)))
6486              ;; We got a server name, so we find the method.
6487              (gnus-server-to-method how)))))
6488   (gnus-browse-foreign-server method))
6489
6490 \f
6491 ;;;
6492 ;;; Gnus summary mode
6493 ;;;
6494
6495 (defvar gnus-summary-mode-map nil)
6496
6497 (put 'gnus-summary-mode 'mode-class 'special)
6498
6499 (unless gnus-summary-mode-map
6500   (setq gnus-summary-mode-map (make-keymap))
6501   (suppress-keymap gnus-summary-mode-map)
6502
6503   ;; Non-orthogonal keys
6504
6505   (gnus-define-keys
6506    gnus-summary-mode-map
6507    " " gnus-summary-next-page
6508    "\177" gnus-summary-prev-page
6509    "\r" gnus-summary-scroll-up
6510    "n" gnus-summary-next-unread-article
6511    "p" gnus-summary-prev-unread-article
6512    "N" gnus-summary-next-article
6513    "P" gnus-summary-prev-article
6514    "\M-\C-n" gnus-summary-next-same-subject
6515    "\M-\C-p" gnus-summary-prev-same-subject
6516    "\M-n" gnus-summary-next-unread-subject
6517    "\M-p" gnus-summary-prev-unread-subject
6518    "." gnus-summary-first-unread-article
6519    "," gnus-summary-best-unread-article
6520    "\M-s" gnus-summary-search-article-forward
6521    "\M-r" gnus-summary-search-article-backward
6522    "<" gnus-summary-beginning-of-article
6523    ">" gnus-summary-end-of-article
6524    "j" gnus-summary-goto-article
6525    "^" gnus-summary-refer-parent-article
6526    "\M-^" gnus-summary-refer-article
6527    "u" gnus-summary-tick-article-forward
6528    "!" gnus-summary-tick-article-forward
6529    "U" gnus-summary-tick-article-backward
6530    "d" gnus-summary-mark-as-read-forward
6531    "D" gnus-summary-mark-as-read-backward
6532    "E" gnus-summary-mark-as-expirable
6533    "\M-u" gnus-summary-clear-mark-forward
6534    "\M-U" gnus-summary-clear-mark-backward
6535    "k" gnus-summary-kill-same-subject-and-select
6536    "\C-k" gnus-summary-kill-same-subject
6537    "\M-\C-k" gnus-summary-kill-thread
6538    "\M-\C-l" gnus-summary-lower-thread
6539    "e" gnus-summary-edit-article
6540    "#" gnus-summary-mark-as-processable
6541    "\M-#" gnus-summary-unmark-as-processable
6542    "\M-\C-t" gnus-summary-toggle-threads
6543    "\M-\C-s" gnus-summary-show-thread
6544    "\M-\C-h" gnus-summary-hide-thread
6545    "\M-\C-f" gnus-summary-next-thread
6546    "\M-\C-b" gnus-summary-prev-thread
6547    "\M-\C-u" gnus-summary-up-thread
6548    "\M-\C-d" gnus-summary-down-thread
6549    "&" gnus-summary-execute-command
6550    "c" gnus-summary-catchup-and-exit
6551    "\C-w" gnus-summary-mark-region-as-read
6552    "\C-t" gnus-summary-toggle-truncation
6553    "?" gnus-summary-mark-as-dormant
6554    "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6555    "\C-c\C-s\C-n" gnus-summary-sort-by-number
6556    "\C-c\C-s\C-a" gnus-summary-sort-by-author
6557    "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6558    "\C-c\C-s\C-d" gnus-summary-sort-by-date
6559    "\C-c\C-s\C-i" gnus-summary-sort-by-score
6560    "=" gnus-summary-expand-window
6561    "\C-x\C-s" gnus-summary-reselect-current-group
6562    "\M-g" gnus-summary-rescan-group
6563    "w" gnus-summary-stop-page-breaking
6564    "\C-c\C-r" gnus-summary-caesar-message
6565    "\M-t" gnus-summary-toggle-mime
6566    "f" gnus-summary-followup
6567    "F" gnus-summary-followup-with-original
6568    "C" gnus-summary-cancel-article
6569    "r" gnus-summary-reply
6570    "R" gnus-summary-reply-with-original
6571    "\C-c\C-f" gnus-summary-mail-forward
6572    "o" gnus-summary-save-article
6573    "\C-o" gnus-summary-save-article-mail
6574    "|" gnus-summary-pipe-output
6575    "\M-k" gnus-summary-edit-local-kill
6576    "\M-K" gnus-summary-edit-global-kill
6577    "V" gnus-version
6578    "\C-c\C-d" gnus-summary-describe-group
6579    "q" gnus-summary-exit
6580    "Q" gnus-summary-exit-no-update
6581    "\C-c\C-i" gnus-info-find-node
6582    gnus-mouse-2 gnus-mouse-pick-article
6583    "m" gnus-summary-mail-other-window
6584    "a" gnus-summary-post-news
6585    "x" gnus-summary-limit-to-unread
6586    "s" gnus-summary-isearch-article
6587    "t" gnus-article-hide-headers
6588    "g" gnus-summary-show-article
6589    "l" gnus-summary-goto-last-article
6590    "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6591    "\C-d" gnus-summary-enter-digest-group
6592    "\C-c\C-b" gnus-bug
6593    "*" gnus-cache-enter-article
6594    "\M-*" gnus-cache-remove-article
6595    "\M-&" gnus-summary-universal-argument
6596    "\C-l" gnus-recenter
6597    "I" gnus-summary-increase-score
6598    "L" gnus-summary-lower-score
6599
6600    "V" gnus-summary-score-map
6601    "X" gnus-uu-extract-map
6602    "S" gnus-summary-send-map)
6603
6604   ;; Sort of orthogonal keymap
6605   (gnus-define-keys
6606    (gnus-summary-mark-map "M" gnus-summary-mode-map)
6607    "t" gnus-summary-tick-article-forward
6608    "!" gnus-summary-tick-article-forward
6609    "d" gnus-summary-mark-as-read-forward
6610    "r" gnus-summary-mark-as-read-forward
6611    "c" gnus-summary-clear-mark-forward
6612    " " gnus-summary-clear-mark-forward
6613    "e" gnus-summary-mark-as-expirable
6614    "x" gnus-summary-mark-as-expirable
6615    "?" gnus-summary-mark-as-dormant
6616    "b" gnus-summary-set-bookmark
6617    "B" gnus-summary-remove-bookmark
6618    "#" gnus-summary-mark-as-processable
6619    "\M-#" gnus-summary-unmark-as-processable
6620    "S" gnus-summary-limit-include-expunged
6621    "C" gnus-summary-catchup
6622    "H" gnus-summary-catchup-to-here
6623    "\C-c" gnus-summary-catchup-all
6624    "k" gnus-summary-kill-same-subject-and-select
6625    "K" gnus-summary-kill-same-subject
6626    "P" gnus-uu-mark-map)
6627
6628   (gnus-define-keys
6629    (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6630    "c" gnus-summary-clear-above
6631    "u" gnus-summary-tick-above
6632    "m" gnus-summary-mark-above
6633    "k" gnus-summary-kill-below)
6634
6635   (gnus-define-keys
6636    (gnus-summary-limit-map "/" gnus-summary-mode-map)
6637    "/" gnus-summary-limit-to-subject
6638    "n" gnus-summary-limit-to-articles
6639    "w" gnus-summary-pop-limit
6640    "s" gnus-summary-limit-to-subject
6641    "a" gnus-summary-limit-to-author
6642    "u" gnus-summary-limit-to-unread
6643    "m" gnus-summary-limit-to-marks
6644    "v" gnus-summary-limit-to-score
6645    "D" gnus-summary-limit-include-dormant
6646    "d" gnus-summary-limit-exclude-dormant
6647 ;;  "t" gnus-summary-limit-exclude-thread
6648    "E" gnus-summary-limit-include-expunged
6649    "c" gnus-summary-limit-exclude-childless-dormant
6650    "C" gnus-summary-limit-mark-excluded-as-read)
6651
6652   (gnus-define-keys
6653    (gnus-summary-goto-map "G" gnus-summary-mode-map)
6654    "n" gnus-summary-next-unread-article
6655    "p" gnus-summary-prev-unread-article
6656    "N" gnus-summary-next-article
6657    "P" gnus-summary-prev-article
6658    "\C-n" gnus-summary-next-same-subject
6659    "\C-p" gnus-summary-prev-same-subject
6660    "\M-n" gnus-summary-next-unread-subject
6661    "\M-p" gnus-summary-prev-unread-subject
6662    "f" gnus-summary-first-unread-article
6663    "b" gnus-summary-best-unread-article
6664    "g" gnus-summary-goto-subject
6665    "l" gnus-summary-goto-last-article
6666    "p" gnus-summary-pop-article)
6667
6668   (gnus-define-keys
6669    (gnus-summary-thread-map "T" gnus-summary-mode-map)
6670    "k" gnus-summary-kill-thread
6671    "l" gnus-summary-lower-thread
6672    "i" gnus-summary-raise-thread
6673    "T" gnus-summary-toggle-threads
6674    "t" gnus-summary-rethread-current
6675    "^" gnus-summary-reparent-thread
6676    "s" gnus-summary-show-thread
6677    "S" gnus-summary-show-all-threads
6678    "h" gnus-summary-hide-thread
6679    "H" gnus-summary-hide-all-threads
6680    "n" gnus-summary-next-thread
6681    "p" gnus-summary-prev-thread
6682    "u" gnus-summary-up-thread
6683    "o" gnus-summary-top-thread
6684    "d" gnus-summary-down-thread
6685    "#" gnus-uu-mark-thread
6686    "\M-#" gnus-uu-unmark-thread)
6687
6688   (gnus-define-keys
6689    (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6690    "c" gnus-summary-catchup-and-exit
6691    "C" gnus-summary-catchup-all-and-exit
6692    "E" gnus-summary-exit-no-update
6693    "Q" gnus-summary-exit
6694    "Z" gnus-summary-exit
6695    "n" gnus-summary-catchup-and-goto-next-group
6696    "R" gnus-summary-reselect-current-group
6697    "G" gnus-summary-rescan-group
6698    "N" gnus-summary-next-group
6699    "P" gnus-summary-prev-group)
6700
6701   (gnus-define-keys
6702    (gnus-summary-article-map "A" gnus-summary-mode-map)
6703    " " gnus-summary-next-page
6704    "n" gnus-summary-next-page
6705    "\177" gnus-summary-prev-page
6706    "p" gnus-summary-prev-page
6707    "\r" gnus-summary-scroll-up
6708    "<" gnus-summary-beginning-of-article
6709    ">" gnus-summary-end-of-article
6710    "b" gnus-summary-beginning-of-article
6711    "e" gnus-summary-end-of-article
6712    "^" gnus-summary-refer-parent-article
6713    "r" gnus-summary-refer-parent-article
6714    "R" gnus-summary-refer-references
6715    "g" gnus-summary-show-article
6716    "s" gnus-summary-isearch-article)
6717
6718   (gnus-define-keys
6719    (gnus-summary-wash-map "W" gnus-summary-mode-map)
6720    "b" gnus-article-add-buttons
6721    "B" gnus-article-add-buttons-to-head
6722    "o" gnus-article-treat-overstrike
6723 ;;  "w" gnus-article-word-wrap
6724    "w" gnus-article-fill-cited-article
6725    "c" gnus-article-remove-cr
6726    "L" gnus-article-remove-trailing-blank-lines
6727    "q" gnus-article-de-quoted-unreadable
6728    "f" gnus-article-display-x-face
6729    "l" gnus-summary-stop-page-breaking
6730    "r" gnus-summary-caesar-message
6731    "t" gnus-article-hide-headers
6732    "v" gnus-summary-verbose-headers
6733    "m" gnus-summary-toggle-mime)
6734
6735   (gnus-define-keys
6736    (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6737    "a" gnus-article-hide
6738    "h" gnus-article-hide-headers
6739    "b" gnus-article-hide-boring-headers
6740    "s" gnus-article-hide-signature
6741    "c" gnus-article-hide-citation
6742    "p" gnus-article-hide-pgp
6743    "\C-c" gnus-article-hide-citation-maybe)
6744
6745   (gnus-define-keys
6746    (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6747    "a" gnus-article-highlight
6748    "h" gnus-article-highlight-headers
6749    "c" gnus-article-highlight-citation
6750    "s" gnus-article-highlight-signature)
6751
6752   (gnus-define-keys
6753    (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6754    "z" gnus-article-date-ut
6755    "u" gnus-article-date-ut
6756    "l" gnus-article-date-local
6757    "e" gnus-article-date-lapsed
6758    "o" gnus-article-date-original)
6759
6760   (gnus-define-keys
6761    (gnus-summary-help-map "H" gnus-summary-mode-map)
6762    "v" gnus-version
6763    "f" gnus-summary-fetch-faq
6764    "d" gnus-summary-describe-group
6765    "h" gnus-summary-describe-briefly
6766    "i" gnus-info-find-node)
6767
6768   (gnus-define-keys
6769    (gnus-summary-backend-map "B" gnus-summary-mode-map)
6770    "e" gnus-summary-expire-articles
6771    "\M-\C-e" gnus-summary-expire-articles-now
6772    "\177" gnus-summary-delete-article
6773    "m" gnus-summary-move-article
6774    "r" gnus-summary-respool-article
6775    "w" gnus-summary-edit-article
6776    "c" gnus-summary-copy-article
6777    "B" gnus-summary-crosspost-article
6778    "q" gnus-summary-respool-query
6779    "i" gnus-summary-import-article)
6780
6781   (gnus-define-keys
6782    (gnus-summary-save-map "O" gnus-summary-mode-map)
6783    "o" gnus-summary-save-article
6784    "m" gnus-summary-save-article-mail
6785    "r" gnus-summary-save-article-rmail
6786    "f" gnus-summary-save-article-file
6787    "b" gnus-summary-save-article-body-file
6788    "h" gnus-summary-save-article-folder
6789    "v" gnus-summary-save-article-vm
6790    "p" gnus-summary-pipe-output
6791    "s" gnus-soup-add-article)
6792   )
6793
6794
6795 \f
6796
6797 (defun gnus-summary-mode (&optional group)
6798   "Major mode for reading articles.
6799
6800 All normal editing commands are switched off.
6801 \\<gnus-summary-mode-map>
6802 Each line in this buffer represents one article.  To read an
6803 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6804 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6805 respectively.
6806
6807 You can also post articles and send mail from this buffer.  To
6808 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6809 of an article, type `\\[gnus-summary-reply]'.
6810
6811 There are approx. one gazillion commands you can execute in this
6812 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6813
6814 The following commands are available:
6815
6816 \\{gnus-summary-mode-map}"
6817   (interactive)
6818   (when (and menu-bar-mode
6819              (gnus-visual-p 'summary-menu 'menu))
6820     (gnus-summary-make-menu-bar))
6821   (kill-all-local-variables)
6822   (let ((locals gnus-summary-local-variables))
6823     (while locals
6824       (if (consp (car locals))
6825           (progn
6826             (make-local-variable (car (car locals)))
6827             (set (car (car locals)) (eval (cdr (car locals)))))
6828         (make-local-variable (car locals))
6829         (set (car locals) nil))
6830       (setq locals (cdr locals))))
6831   (gnus-make-thread-indent-array)
6832   (gnus-simplify-mode-line)
6833   (setq major-mode 'gnus-summary-mode)
6834   (setq mode-name "Summary")
6835   (make-local-variable 'minor-mode-alist)
6836   (use-local-map gnus-summary-mode-map)
6837   (buffer-disable-undo (current-buffer))
6838   (setq buffer-read-only t)             ;Disable modification
6839   (setq truncate-lines t)
6840   (setq selective-display t)
6841   (setq selective-display-ellipses t)   ;Display `...'
6842   (setq buffer-display-table gnus-summary-display-table)
6843   (setq gnus-newsgroup-name group)
6844   (run-hooks 'gnus-summary-mode-hook))
6845
6846 (defun gnus-summary-make-display-table ()
6847   ;; Change the display table.  Odd characters have a tendency to mess
6848   ;; up nicely formatted displays - we make all possible glyphs
6849   ;; display only a single character.
6850
6851   ;; We start from the standard display table, if any.
6852   (setq gnus-summary-display-table
6853         (or (copy-sequence standard-display-table)
6854             (make-display-table)))
6855   ;; Nix out all the control chars...
6856   (let ((i 32))
6857     (while (>= (setq i (1- i)) 0)
6858       (aset gnus-summary-display-table i [??])))
6859   ;; ... but not newline and cr, of course. (cr is necessary for the
6860   ;; selective display).
6861   (aset gnus-summary-display-table ?\n nil)
6862   (aset gnus-summary-display-table ?\r nil)
6863   ;; We nix out any glyphs over 126 that are not set already.
6864   (let ((i 256))
6865     (while (>= (setq i (1- i)) 127)
6866       ;; Only modify if the entry is nil.
6867       (or (aref gnus-summary-display-table i)
6868           (aset gnus-summary-display-table i [??])))))
6869
6870 (defun gnus-summary-clear-local-variables ()
6871   (let ((locals gnus-summary-local-variables))
6872     (while locals
6873       (if (consp (car locals))
6874           (and (vectorp (car (car locals)))
6875                (set (car (car locals)) nil))
6876         (and (vectorp (car locals))
6877              (set (car locals) nil)))
6878       (setq locals (cdr locals)))))
6879
6880 ;; Summary data functions.
6881
6882 (defmacro gnus-data-number (data)
6883   `(car ,data))
6884
6885 (defmacro gnus-data-set-number (data number)
6886   `(setcar ,data ,number))
6887
6888 (defmacro gnus-data-mark (data)
6889   `(nth 1 ,data))
6890
6891 (defmacro gnus-data-set-mark (data mark)
6892   `(setcar (nthcdr 1 ,data) ,mark))
6893
6894 (defmacro gnus-data-pos (data)
6895   `(nth 2 ,data))
6896
6897 (defmacro gnus-data-set-pos (data pos)
6898   `(setcar (nthcdr 2 ,data) ,pos))
6899
6900 (defmacro gnus-data-header (data)
6901   `(nth 3 ,data))
6902
6903 (defmacro gnus-data-level (data)
6904   `(nth 4 ,data))
6905
6906 (defmacro gnus-data-unread-p (data)
6907   `(= (nth 1 ,data) gnus-unread-mark))
6908
6909 (defmacro gnus-data-pseudo-p (data)
6910   `(consp (nth 3 ,data)))
6911
6912 (defmacro gnus-data-find (number)
6913   `(assq ,number gnus-newsgroup-data))
6914
6915 (defmacro gnus-data-find-list (number &optional data)
6916   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
6917      (memq (assq ,number bdata)
6918            bdata)))
6919
6920 (defmacro gnus-data-make (number mark pos header level)
6921   `(list ,number ,mark ,pos ,header ,level))
6922
6923 (defun gnus-data-enter (after-article number mark pos header level offset)
6924   (let ((data (gnus-data-find-list after-article)))
6925     (or data (error "No such article: %d" after-article))
6926     (setcdr data (cons (gnus-data-make number mark pos header level)
6927                        (cdr data)))
6928     (setq gnus-newsgroup-data-reverse nil)
6929     (gnus-data-update-list (cdr (cdr data)) offset)))
6930
6931 (defun gnus-data-enter-list (after-article list &optional offset)
6932   (when list
6933     (let ((data (and after-article (gnus-data-find-list after-article)))
6934           (ilist list))
6935       (or data (not after-article) (error "No such article: %d" after-article))
6936       ;; Find the last element in the list to be spliced into the main
6937       ;; list.
6938       (while (cdr list)
6939         (setq list (cdr list)))
6940       (if (not data)
6941           (progn
6942             (setcdr list gnus-newsgroup-data)
6943             (setq gnus-newsgroup-data ilist)
6944             (and offset (gnus-data-update-list (cdr list) offset)))
6945         (setcdr list (cdr data))
6946         (setcdr data ilist)
6947         (and offset (gnus-data-update-list (cdr data) offset)))
6948       (setq gnus-newsgroup-data-reverse nil))))
6949
6950 (defun gnus-data-remove (article &optional offset)
6951   (let ((data gnus-newsgroup-data))
6952     (if (= (gnus-data-number (car data)) article)
6953         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
6954               gnus-newsgroup-data-reverse nil)
6955       (while (cdr data)
6956         (and (= (gnus-data-number (car (cdr data))) article)
6957              (progn
6958                (setcdr data (cdr (cdr data)))
6959                (and offset (gnus-data-update-list (cdr data) offset))
6960                (setq data nil
6961                      gnus-newsgroup-data-reverse nil)))
6962         (setq data (cdr data))))))
6963
6964 (defmacro gnus-data-list (backward)
6965   `(if ,backward
6966        (or gnus-newsgroup-data-reverse
6967            (setq gnus-newsgroup-data-reverse
6968                  (reverse gnus-newsgroup-data)))
6969      gnus-newsgroup-data))
6970
6971 (defun gnus-data-update-list (data offset)
6972   "Add OFFSET to the POS of all data entries in DATA."
6973   (while data
6974     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
6975     (setq data (cdr data))))
6976
6977 (defun gnus-data-compute-positions ()
6978   "Compute the positions of all articles."
6979   (let ((data gnus-newsgroup-data)
6980         pos)
6981     (while data
6982       (when (setq pos (text-property-any
6983                        (point-min) (point-max)
6984                        'gnus-number (gnus-data-number (car data))))
6985         (gnus-data-set-pos (car data) (+ pos 3)))
6986       (setq data (cdr data)))))
6987
6988 (defun gnus-summary-article-pseudo-p (article)
6989   "Say whether this article is a pseudo article or not."
6990   (not (vectorp (gnus-data-header (gnus-data-find article)))))
6991
6992 (defun gnus-article-parent-p (number)
6993   "Say whether this article is a parent or not."
6994   (let* ((data (gnus-data-find-list number)))
6995     (and (cdr data)                     ; There has to be an article after...
6996          (< (gnus-data-level (car data)) ; And it has to have a higher level.
6997             (gnus-data-level (nth 1 data))))))
6998
6999 (defmacro gnus-summary-skip-intangible ()
7000   "If the current article is intangible, then jump to a different article."
7001   '(let ((to (get-text-property (point) 'gnus-intangible)))
7002     (when to
7003       (gnus-summary-goto-subject to))))
7004
7005 (defmacro gnus-summary-article-intangible-p ()
7006   "Say whether this article is intangible or not."
7007   '(get-text-property (point) 'gnus-intangible))
7008
7009 ;; Some summary mode macros.
7010
7011 (defmacro gnus-summary-article-number ()
7012   "The article number of the article on the current line.
7013 If there isn's an article number here, then we return the current
7014 article number."
7015   '(progn
7016      (gnus-summary-skip-intangible)
7017      (or (get-text-property (point) 'gnus-number)
7018          (gnus-summary-last-subject))))
7019
7020 (defmacro gnus-summary-article-header (&optional number)
7021   `(gnus-data-header (gnus-data-find
7022                       ,(or number '(gnus-summary-article-number)))))
7023
7024 (defmacro gnus-summary-thread-level (&optional number)
7025   `(if (and (eq gnus-summary-make-false-root 'dummy)
7026             (get-text-property (point) 'gnus-intangible))
7027        0
7028      (gnus-data-level (gnus-data-find
7029                        ,(or number '(gnus-summary-article-number))))))
7030
7031 (defmacro gnus-summary-article-mark (&optional number)
7032   `(gnus-data-mark (gnus-data-find
7033                     ,(or number '(gnus-summary-article-number)))))
7034
7035 (defmacro gnus-summary-article-pos (&optional number)
7036   `(gnus-data-pos (gnus-data-find
7037                    ,(or number '(gnus-summary-article-number)))))
7038
7039 (defmacro gnus-summary-article-subject (&optional number)
7040   "Return current subject string or nil if nothing."
7041   `(let ((headers
7042           ,(if number
7043                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7044              '(gnus-data-header (assq (gnus-summary-article-number)
7045                                       gnus-newsgroup-data)))))
7046      (and headers
7047           (vectorp headers)
7048           (mail-header-subject headers))))
7049
7050 (defmacro gnus-summary-article-score (&optional number)
7051   "Return current article score."
7052   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7053                   gnus-newsgroup-scored))
7054        gnus-summary-default-score 0))
7055
7056 (defun gnus-summary-article-children (&optional number)
7057   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7058          (level (gnus-data-level (car data)))
7059          l children)
7060     (while (and (setq data (cdr data))
7061                 (> (setq l (gnus-data-level (car data))) level))
7062       (and (= (1+ level) l)
7063            (setq children (cons (gnus-data-number (car data))
7064                                 children))))
7065     (nreverse children)))
7066
7067 (defun gnus-summary-article-parent (&optional number)
7068   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7069                                     (gnus-data-list t)))
7070          (level (gnus-data-level (car data)))
7071          l)
7072     (if (zerop level)
7073         () ; This is a root.
7074       ;; We search until we find an article with a level less than
7075       ;; this one.  That function has to be the parent.
7076       (while (and (setq data (cdr data))
7077                   (not (< (gnus-data-level (car data)) level))))
7078       (and data (gnus-data-number (car data))))))
7079
7080
7081 ;; Various summary mode internalish functions.
7082
7083 (defun gnus-mouse-pick-article (e)
7084   (interactive "e")
7085   (mouse-set-point e)
7086   (gnus-summary-next-page nil t))
7087
7088 (defun gnus-summary-setup-buffer (group)
7089   "Initialize summary buffer."
7090   (let ((buffer (concat "*Summary " group "*")))
7091     (if (get-buffer buffer)
7092         (progn
7093           (set-buffer buffer)
7094           (setq gnus-summary-buffer (current-buffer))
7095           (not gnus-newsgroup-prepared))
7096       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7097       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7098       (gnus-add-current-to-buffer-list)
7099       (gnus-summary-mode group)
7100       (when gnus-carpal
7101         (gnus-carpal-setup-buffer 'summary))
7102       (unless gnus-single-article-buffer
7103         (make-local-variable 'gnus-article-buffer)
7104         (make-local-variable 'gnus-original-article-buffer))
7105       (setq gnus-newsgroup-name group)
7106       t)))
7107
7108 (defun gnus-set-global-variables ()
7109   ;; Set the global equivalents of the summary buffer-local variables
7110   ;; to the latest values they had.  These reflect the summary buffer
7111   ;; that was in action when the last article was fetched.
7112   (when (eq major-mode 'gnus-summary-mode)
7113     (setq gnus-summary-buffer (current-buffer))
7114     (let ((name gnus-newsgroup-name)
7115           (marked gnus-newsgroup-marked)
7116           (unread gnus-newsgroup-unreads)
7117           (headers gnus-current-headers)
7118           (data gnus-newsgroup-data)
7119           (article-buffer gnus-article-buffer)
7120           (score-file gnus-current-score-file))
7121       (save-excursion
7122         (set-buffer gnus-group-buffer)
7123         (setq gnus-newsgroup-name name)
7124         (setq gnus-newsgroup-marked marked)
7125         (setq gnus-newsgroup-unreads unread)
7126         (setq gnus-current-headers headers)
7127         (setq gnus-newsgroup-data data)
7128         (setq gnus-article-buffer article-buffer)
7129         (setq gnus-current-score-file score-file)))))
7130
7131 (defun gnus-summary-last-article-p (&optional article)
7132   "Return whether ARTICLE is the last article in the buffer."
7133   (if (not (setq article (or article (gnus-summary-article-number))))
7134       t ; All non-existant numbers are the last article. :-)
7135     (cdr (gnus-data-find-list article))))
7136
7137 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7138   "Insert a dummy root in the summary buffer."
7139   (beginning-of-line)
7140   (add-text-properties
7141    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7142    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7143
7144 (defvar gnus-thread-indent-array nil)
7145 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7146 (defun gnus-make-thread-indent-array ()
7147   (let ((n 200))
7148     (if (and gnus-thread-indent-array
7149              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7150         nil
7151       (setq gnus-thread-indent-array (make-vector 201 "")
7152             gnus-thread-indent-array-level gnus-thread-indent-level)
7153       (while (>= n 0)
7154         (aset gnus-thread-indent-array n
7155               (make-string (* n gnus-thread-indent-level) ? ))
7156         (setq n (1- n))))))
7157
7158 (defun gnus-summary-insert-line
7159   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7160                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7161                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7162   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7163          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7164          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7165          (gnus-tmp-score-char
7166           (if (or (null gnus-summary-default-score)
7167                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7168                       gnus-summary-zcore-fuzz)) ? 
7169             (if (< gnus-tmp-score gnus-summary-default-score)
7170                 gnus-score-below-mark gnus-score-over-mark)))
7171          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7172                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7173                                   gnus-cached-mark)
7174                                  (gnus-tmp-replied gnus-replied-mark)
7175                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7176                                   gnus-saved-mark)
7177                                  (t gnus-unread-mark)))
7178          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7179          (gnus-tmp-name
7180           (cond
7181            ((string-match "(.+)" gnus-tmp-from)
7182             (substring gnus-tmp-from
7183                        (1+ (match-beginning 0)) (1- (match-end 0))))
7184            ((string-match "<[^>]+> *$" gnus-tmp-from)
7185             (let ((beg (match-beginning 0)))
7186               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7187                        (substring gnus-tmp-from (1+ (match-beginning 0))
7188                                   (1- (match-end 0))))
7189                   (substring gnus-tmp-from 0 beg))))
7190            (t gnus-tmp-from)))
7191          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7192          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7193          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7194          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7195          (buffer-read-only nil))
7196     (when (string= gnus-tmp-name "")
7197       (setq gnus-tmp-name gnus-tmp-from))
7198     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7199     (put-text-property
7200      (point)
7201      (progn (eval gnus-summary-line-format-spec) (point))
7202      'gnus-number gnus-tmp-number)
7203     (when (gnus-visual-p 'summary-highlight 'highlight)
7204       (forward-line -1)
7205       (run-hooks 'gnus-summary-update-hook)
7206       (forward-line 1))))
7207
7208 (defun gnus-summary-update-line (&optional dont-update)
7209   ;; Update summary line after change.
7210   (when (and gnus-summary-default-score
7211              (not gnus-summary-inhibit-highlight))
7212     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7213            (article (gnus-summary-article-number))
7214            (score (gnus-summary-article-score article)))
7215       (unless dont-update
7216         (if (and gnus-summary-mark-below
7217                  (< (gnus-summary-article-score)
7218                     gnus-summary-mark-below))
7219             ;; This article has a low score, so we mark it as read.
7220             (when (memq article gnus-newsgroup-unreads)
7221               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7222           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7223             ;; This article was previously marked as read on account
7224             ;; of a low score, but now it has risen, so we mark it as
7225             ;; unread.
7226             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7227         (gnus-summary-update-mark
7228          (if (or (null gnus-summary-default-score)
7229                  (<= (abs (- score gnus-summary-default-score))
7230                      gnus-summary-zcore-fuzz)) ? 
7231            (if (< score gnus-summary-default-score)
7232                gnus-score-below-mark gnus-score-over-mark)) 'score))
7233       ;; Do visual highlighting.
7234       (when (gnus-visual-p 'summary-highlight 'highlight)
7235         (run-hooks 'gnus-summary-update-hook)))))
7236
7237 (defvar gnus-tmp-new-adopts nil)
7238
7239 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7240   ;; Sum up all elements (and sub-elements) in a list.
7241   (let* ((number
7242           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7243           (cond
7244            ((and (consp thread) (cdr thread))
7245             (apply
7246              '+ 1 (mapcar
7247                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7248            ((null thread)
7249             1)
7250            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7251             1)
7252            (t 1))))
7253     (when (and level (zerop level) gnus-tmp-new-adopts)
7254       (incf number
7255             (apply '+ (mapcar
7256                        'gnus-summary-number-of-articles-in-thread
7257                        gnus-tmp-new-adopts))))
7258     (if char
7259         (if (> number 1) gnus-not-empty-thread-mark
7260           gnus-empty-thread-mark)
7261       number)))
7262
7263 (defun gnus-summary-set-local-parameters (group)
7264  "Go through the local params of GROUP and set all variable specs in that list."
7265   (let ((params (gnus-info-params (gnus-get-info group)))
7266         elem)
7267     (while params
7268       (setq elem (car params)
7269             params (cdr params))
7270       (and (consp elem)                 ; Has to be a cons.
7271            (consp (cdr elem))           ; The cdr has to be a list.
7272            (symbolp (car elem))         ; Has to be a symbol in there.
7273            (progn                       ; So we set it.
7274              (make-local-variable (car elem))
7275              (set (car elem) (eval (nth 1 elem))))))))
7276
7277 (defun gnus-summary-read-group
7278   (group &optional show-all no-article kill-buffer no-display)
7279   "Start reading news in newsgroup GROUP.
7280 If SHOW-ALL is non-nil, already read articles are also listed.
7281 If NO-ARTICLE is non-nil, no article is selected initially.
7282 If NO-DISPLAY, don't generate a summary buffer."
7283   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7284   (let* ((new-group (gnus-summary-setup-buffer group))
7285          (quit-config (gnus-group-quit-config group))
7286          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7287     (cond
7288      ;; This summary buffer exists already, so we just select it.
7289      ((not new-group)
7290       (gnus-set-global-variables)
7291       (when kill-buffer
7292         (gnus-kill-or-deaden-summary kill-buffer))
7293       (gnus-configure-windows 'summary 'force)
7294       (gnus-set-mode-line 'summary)
7295       (gnus-summary-position-point)
7296       (message "")
7297       t)
7298      ;; We couldn't select this group.
7299      ((null did-select)
7300       (when (and (eq major-mode 'gnus-summary-mode)
7301                  (not (equal (current-buffer) kill-buffer)))
7302         (kill-buffer (current-buffer))
7303         (if (not quit-config)
7304             (progn
7305               (set-buffer gnus-group-buffer)
7306               (gnus-group-jump-to-group group)
7307               (gnus-group-next-unread-group 1))
7308           (if (not (buffer-name (car quit-config)))
7309               (gnus-configure-windows 'group 'force)
7310             (set-buffer (car quit-config))
7311             (and (eq major-mode 'gnus-summary-mode)
7312                  (gnus-set-global-variables))
7313             (gnus-configure-windows (cdr quit-config)))))
7314       (gnus-message 3 "Can't select group")
7315       nil)
7316      ;; The user did a `C-g' while prompting for number of articles,
7317      ;; so we exit this group.
7318      ((eq did-select 'quit)
7319       (and (eq major-mode 'gnus-summary-mode)
7320            (not (equal (current-buffer) kill-buffer))
7321            (kill-buffer (current-buffer)))
7322       (when kill-buffer
7323         (gnus-kill-or-deaden-summary kill-buffer))
7324       (if (not quit-config)
7325           (progn
7326             (set-buffer gnus-group-buffer)
7327             (gnus-group-jump-to-group group)
7328             (gnus-group-next-unread-group 1)
7329             (gnus-configure-windows 'group 'force))
7330         (if (not (buffer-name (car quit-config)))
7331             (gnus-configure-windows 'group 'force)
7332           (set-buffer (car quit-config))
7333           (and (eq major-mode 'gnus-summary-mode)
7334                (gnus-set-global-variables))
7335           (gnus-configure-windows (cdr quit-config))))
7336       ;; Finally signal the quit.
7337       (signal 'quit nil))
7338      ;; The group was successfully selected.
7339      (t
7340       (gnus-set-global-variables)
7341       ;; Save the active value in effect when the group was entered.
7342       (setq gnus-newsgroup-active
7343             (gnus-copy-sequence
7344              (gnus-active gnus-newsgroup-name)))
7345       ;; You can change the summary buffer in some way with this hook.
7346       (run-hooks 'gnus-select-group-hook)
7347       ;; Set any local variables in the group parameters.
7348       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7349       (gnus-update-format-specifications)
7350       ;; Do score processing.
7351       (when gnus-use-scoring
7352         (gnus-possibly-score-headers))
7353       ;; Check whether to fill in the gaps in the threads.
7354       (when gnus-build-sparse-threads
7355         (gnus-build-sparse-threads))
7356       ;; Find the initial limit.
7357       (gnus-summary-initial-limit show-all)
7358       ;; Generate the summary buffer.
7359       (unless no-display
7360         (gnus-summary-prepare))
7361       (when gnus-use-trees
7362         (gnus-tree-open group)
7363         (setq gnus-summary-highlight-line-function
7364               'gnus-tree-highlight-article))
7365       ;; If the summary buffer is empty, but there are some low-scored
7366       ;; articles or some excluded dormants, we include these in the
7367       ;; buffer.
7368       (when (and (zerop (buffer-size))
7369                  (not no-display))
7370         (cond (gnus-newsgroup-dormant
7371                (gnus-summary-limit-include-dormant))
7372               ((and gnus-newsgroup-scored show-all)
7373                (gnus-summary-limit-include-expunged))))
7374       ;; Function `gnus-apply-kill-file' must be called in this hook.
7375       (run-hooks 'gnus-apply-kill-hook)
7376       (if (and (zerop (buffer-size))
7377                (not no-display))
7378           (progn
7379             ;; This newsgroup is empty.
7380             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7381             (gnus-message 6 "No unread news")
7382             (when kill-buffer
7383               (gnus-kill-or-deaden-summary kill-buffer))
7384             ;; Return nil from this function.
7385             nil)
7386         ;; Hide conversation thread subtrees.  We cannot do this in
7387         ;; gnus-summary-prepare-hook since kill processing may not
7388         ;; work with hidden articles.
7389         (and gnus-show-threads
7390              gnus-thread-hide-subtree
7391              (gnus-summary-hide-all-threads))
7392         ;; Show first unread article if requested.
7393         (if (and (not no-article)
7394                  (not no-display)
7395                  gnus-newsgroup-unreads
7396                  gnus-auto-select-first)
7397             (if (eq gnus-auto-select-first 'best)
7398                 (gnus-summary-best-unread-article)
7399               (gnus-summary-first-unread-article))
7400           ;; Don't select any articles, just move point to the first
7401           ;; article in the group.
7402           (goto-char (point-min))
7403           (gnus-summary-position-point)
7404           (gnus-set-mode-line 'summary)
7405           (gnus-configure-windows 'summary 'force))
7406         ;; If we are in async mode, we send some info to the backend.
7407         (when gnus-newsgroup-async
7408           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7409         (when kill-buffer
7410           (gnus-kill-or-deaden-summary kill-buffer))
7411         (when (get-buffer-window gnus-group-buffer)
7412           ;; Gotta use windows, because recenter does wierd stuff if
7413           ;; the current buffer ain't the displayed window.
7414           (let ((owin (selected-window)))
7415             (select-window (get-buffer-window gnus-group-buffer))
7416             (when (gnus-group-goto-group group)
7417               (recenter))
7418             (select-window owin))))
7419       ;; Mark this buffer as "prepared".
7420       (setq gnus-newsgroup-prepared t)
7421       t))))
7422
7423 (defun gnus-summary-prepare ()
7424   "Generate the summary buffer."
7425   (let ((buffer-read-only nil))
7426     (erase-buffer)
7427     (setq gnus-newsgroup-data nil
7428           gnus-newsgroup-data-reverse nil)
7429     (run-hooks 'gnus-summary-generate-hook)
7430     ;; Generate the buffer, either with threads or without.
7431     (when gnus-newsgroup-headers
7432       (gnus-summary-prepare-threads
7433        (if gnus-show-threads
7434            (gnus-sort-gathered-threads
7435             (funcall gnus-summary-thread-gathering-function
7436                      (gnus-sort-threads
7437                       (gnus-cut-threads (gnus-make-threads)))))
7438          ;; Unthreaded display.
7439          (gnus-sort-articles gnus-newsgroup-headers))))
7440     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7441     ;; Call hooks for modifying summary buffer.
7442     (goto-char (point-min))
7443     (run-hooks 'gnus-summary-prepare-hook)))
7444
7445 (defun gnus-gather-threads-by-subject (threads)
7446   "Gather threads by looking at Subject headers."
7447   (if (not gnus-summary-make-false-root)
7448       threads
7449     (let ((hashtb (gnus-make-hashtable 1023))
7450           (prev threads)
7451           (result threads)
7452           subject hthread whole-subject)
7453       (while threads
7454         (setq whole-subject (mail-header-subject (car (car threads))))
7455         (if (and gnus-summary-gather-exclude-subject
7456                  (string-match gnus-summary-gather-exclude-subject
7457                                whole-subject))
7458             () ; We don't want to do anything with this article.
7459           ;; We simplify the subject before looking it up in the
7460           ;; hash table.
7461           (setq subject
7462                 (cond
7463                  ;; Truncate the subject.
7464                  ((numberp gnus-summary-gather-subject-limit)
7465                   (setq subject (gnus-simplify-subject-re whole-subject))
7466                   (if (> (length subject) gnus-summary-gather-subject-limit)
7467                       (substring subject 0 gnus-summary-gather-subject-limit)
7468                     subject))
7469                  ;; Fuzzily simplify it.
7470                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7471                   (gnus-simplify-subject-fuzzy whole-subject))
7472                  ;; Just remove the leading "Re:".
7473                  (t
7474                   (gnus-simplify-subject-re whole-subject))))
7475
7476           (if (setq hthread (gnus-gethash subject hashtb))
7477               (progn
7478                 ;; We enter a dummy root into the thread, if we
7479                 ;; haven't done that already.
7480                 (unless (stringp (car (car hthread)))
7481                   (setcar hthread (list whole-subject (car hthread))))
7482                 ;; We add this new gathered thread to this gathered
7483                 ;; thread.
7484                 (setcdr (car hthread)
7485                         (nconc (cdr (car hthread)) (list (car threads))))
7486                 ;; Remove it from the list of threads.
7487                 (setcdr prev (cdr threads))
7488                 (setq threads prev))
7489             ;; Enter this thread into the hash table.
7490             (gnus-sethash subject threads hashtb)))
7491         (setq prev threads)
7492         (setq threads (cdr threads)))
7493       result)))
7494
7495 (defun gnus-summary-gather-threads-by-references (threads)
7496   "Gather threads by looking at References headers."
7497   (let ((idhashtb (gnus-make-hashtable 1023))
7498         (thhashtb (gnus-make-hashtable 1023))
7499         (prev threads)
7500         (result threads)
7501         ids references id gthread gid entered)
7502     (while threads
7503       (when (setq references (mail-header-references (caar threads)))
7504         (setq id (mail-header-id (caar threads)))
7505         (setq ids (gnus-split-references references))
7506         (setq entered nil)
7507         (while ids
7508           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7509               (progn
7510                 (gnus-sethash (car ids) id idhashtb)
7511                 (gnus-sethash id threads thhashtb))
7512             (setq gthread (gnus-gethash gid thhashtb))
7513             (unless entered
7514               ;; We enter a dummy root into the thread, if we
7515               ;; haven't done that already.
7516               (unless (stringp (caar gthread))
7517                 (setcar gthread (list (mail-header-subject (caar gthread))
7518                                       (car gthread))))
7519               ;; We add this new gathered thread to this gathered
7520               ;; thread.
7521               (setcdr (car gthread)
7522                       (nconc (cdar gthread) (list (car threads)))))
7523             ;; Add it into the thread hash table.
7524             (gnus-sethash id gthread thhashtb)
7525             (setq entered t)
7526             ;; Remove it from the list of threads.
7527             (setcdr prev (cdr threads))
7528             (setq threads prev))
7529           (setq ids (cdr ids))))
7530       (setq prev threads)
7531       (setq threads (cdr threads)))
7532     result))
7533
7534 (defun gnus-sort-gathered-threads (threads)
7535   "Sort subtreads inside each gathered thread by article number."
7536   (let ((result threads))
7537     (while threads
7538       (when (stringp (car (car threads)))
7539         (setcdr (car threads)
7540                 (sort (cdr (car threads)) 'gnus-thread-sort-by-number)))
7541       (setq threads (cdr threads)))
7542     result))
7543
7544 (defun gnus-make-threads ()
7545   "Go through the dependency hashtb and find the roots.  Return all threads."
7546   (let (threads)
7547     (mapatoms
7548      (lambda (refs)
7549        (unless (car (symbol-value refs))
7550          ;; These threads do not refer back to any other articles,
7551          ;; so they're roots.
7552          (setq threads (append (cdr (symbol-value refs)) threads))))
7553      gnus-newsgroup-dependencies)
7554     threads))
7555
7556 (defun gnus-build-sparse-threads ()
7557   (let ((headers gnus-newsgroup-headers)
7558         (deps gnus-newsgroup-dependencies)
7559         header references generation relations 
7560         cthread subject child end pthread relation)
7561     ;; First we create an alist of generations/relations, where 
7562     ;; generations is how much we trust the ralation, and the relation
7563     ;; is parent/child.
7564     (gnus-message 7 "Making sparse threads...")
7565     (save-excursion
7566       (nnheader-set-temp-buffer " *gnus sparse threads*")
7567       (while (setq header (pop headers))
7568         (when (and (setq references (mail-header-references header))
7569                    (not (string= references "")))
7570           (insert references)
7571           (setq child (downcase (mail-header-id header))
7572                 subject (mail-header-subject header))
7573           (setq generation 0)
7574           (while (search-backward ">" nil t)
7575             (setq end (1+ (point)))
7576             (when (search-backward "<" nil t)
7577               (push (list (incf generation) 
7578                           child (setq child (downcase
7579                                              (buffer-substring (point) end)))
7580                           subject)
7581                     relations)))
7582           (push (list (1+ generation) child nil subject) relations)
7583           (erase-buffer)))
7584       (kill-buffer (current-buffer)))
7585     ;; Sort over trustworthiness.
7586     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7587     (while (setq relation (pop relations))
7588       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7589                 (unless (car (symbol-value cthread))
7590                   ;; Make this article the parent of these threads.
7591                   (setcar (symbol-value cthread)
7592                           (vector gnus-reffed-article-number 
7593                                   (cadddr relation) 
7594                                   "" ""
7595                                   (cadr relation) 
7596                                   (or (caddr relation) "") 0 0 "")))
7597               (set cthread (list (vector gnus-reffed-article-number
7598                                          (cadddr relation) 
7599                                          "" "" (cadr relation) 
7600                                          (or (caddr relation) "") 0 0 ""))))
7601         (push gnus-reffed-article-number gnus-newsgroup-limit)
7602         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7603         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7604               gnus-newsgroup-reads)
7605         (decf gnus-reffed-article-number)
7606         ;; Make this new thread the child of its parent.
7607         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7608             (setcdr (symbol-value pthread)
7609                     (nconc (cdr (symbol-value pthread))
7610                            (list (symbol-value cthread))))
7611           (set pthread (list nil (symbol-value cthread))))))
7612     (gnus-message 7 "Making sparse threads...done")))
7613
7614 (defun gnus-build-old-threads ()
7615   ;; Look at all the articles that refer back to old articles, and
7616   ;; fetch the headers for the articles that aren't there.  This will
7617   ;; build complete threads - if the roots haven't been expired by the
7618   ;; server, that is.
7619   (let (id heads)
7620     (mapatoms
7621      (lambda (refs)
7622        (when (not (car (symbol-value refs)))
7623          (setq heads (cdr (symbol-value refs)))
7624          (while heads
7625            (if (memq (mail-header-number (car (car heads)))
7626                      gnus-newsgroup-dormant)
7627                (setq heads (cdr heads))
7628              (setq id (symbol-name refs))
7629              (while (and (setq id (gnus-build-get-header id))
7630                          (not (car (gnus-gethash
7631                                     id gnus-newsgroup-dependencies)))))
7632              (setq heads nil)))))
7633      gnus-newsgroup-dependencies)))
7634
7635 (defun gnus-build-get-header (id)
7636   ;; Look through the buffer of NOV lines and find the header to
7637   ;; ID.  Enter this line into the dependencies hash table, and return
7638   ;; the id of the parent article (if any).
7639   (let ((deps gnus-newsgroup-dependencies)
7640         found header)
7641     (prog1
7642         (save-excursion
7643           (set-buffer nntp-server-buffer)
7644           (goto-char (point-min))
7645           (while (and (not found) (search-forward id nil t))
7646             (beginning-of-line)
7647             (setq found (looking-at
7648                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7649                                  (regexp-quote id))))
7650             (or found (beginning-of-line 2)))
7651           (when found
7652             (let (ref)
7653               (beginning-of-line)
7654               (and
7655                (setq header (gnus-nov-parse-line
7656                              (read (current-buffer)) deps))
7657                (gnus-parent-id (mail-header-references header))))))
7658       (when header
7659         (let ((number (mail-header-number header)))
7660           (push number gnus-newsgroup-limit)
7661           (push header gnus-newsgroup-headers)
7662           (if (memq number gnus-newsgroup-unselected)
7663               (progn
7664                 (push number gnus-newsgroup-unreads)
7665                 (setq gnus-newsgroup-unselected
7666                       (delq number gnus-newsgroup-unselected)))
7667             (push number gnus-newsgroup-ancient)))))))
7668
7669 (defun gnus-summary-update-article (article &optional header)
7670   "Update ARTICLE in the summary buffer."
7671   (let ((id (mail-header-id (gnus-summary-article-header article)))
7672         (data (gnus-data-find article)))
7673     (setcar (gnus-id-to-thread id) nil)
7674     (gnus-summary-insert-subject id)
7675     ;; Set the (possibly) new article number in the data structure.
7676     (gnus-data-set-number data (gnus-id-to-article id))))
7677
7678 (defun gnus-rebuild-thread (id)
7679   "Rebuild the thread containing ID."
7680   (let ((dep gnus-newsgroup-dependencies)
7681         (buffer-read-only nil)
7682         current headers refs thread art data)
7683     (if (not gnus-show-threads)
7684         (setq thread (list (car (gnus-id-to-thread id))))
7685       ;; Get the thread this article is part of.
7686       (setq thread (gnus-remove-thread id)))
7687     (setq current (save-excursion
7688                     (and (zerop (forward-line -1))
7689                          (gnus-summary-article-number))))
7690     ;; If this is a gathered thread, we have to go some re-gathering.
7691     (when (stringp (car thread))
7692       (let ((subject (car thread))
7693             roots thr)
7694         (setq thread (cdr thread))
7695         (while thread
7696           (unless (memq (setq thr (gnus-id-to-thread
7697                                       (gnus-root-id
7698                                        (mail-header-id (car (car thread))))))
7699                         roots)
7700             (push thr roots))
7701           (setq thread (cdr thread)))
7702         ;; We now have all (unique) roots.
7703         (if (= (length roots) 1)
7704             ;; All the loose roots are now one solid root.
7705             (setq thread (car roots))
7706           (setq thread (cons subject (gnus-sort-threads roots))))))
7707     (let ((beg (point))
7708           threads)
7709       ;; We then insert this thread into the summary buffer.
7710       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7711         (gnus-summary-prepare-threads (list thread))
7712         (setq data (nreverse gnus-newsgroup-data))
7713         (setq threads gnus-newsgroup-threads))
7714       ;; We splice the new data into the data structure.
7715       (gnus-data-enter-list current data)
7716       (gnus-data-compute-positions)
7717       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7718
7719 (defun gnus-id-to-thread (id)
7720   "Return the (sub-)thread where ID appears."
7721   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7722
7723 (defun gnus-id-to-article (id)
7724   "Return the article number of ID."
7725   (let ((thread (gnus-id-to-thread id)))
7726     (when thread
7727       (mail-header-number (car thread)))))
7728
7729 (defun gnus-id-to-header (id)
7730   "Return the article headers of ID."
7731   (car (gnus-id-to-thread id)))
7732
7733 (defun gnus-article-displayed-root-p (article)
7734   "Say whether ARTICLE is a root(ish) article."
7735   (let ((level (gnus-summary-thread-level article))
7736         particle)
7737     (cond 
7738      ((null level) nil)
7739      ((zerop level) t)
7740      ((and (= 1 level)
7741            (null (setq particle (gnus-id-to-article
7742                                  (gnus-parent-id 
7743                                   (mail-header-references 
7744                                    (gnus-summary-article-header article))))))
7745            (null (gnus-summary-thread-level particle)))))))
7746
7747 (defun gnus-root-id (id)
7748   "Return the id of the root of the thread where ID appears."
7749   (let (last-id prev)
7750     (while (and id (setq prev (car (gnus-gethash
7751                                     (downcase id)
7752                                     gnus-newsgroup-dependencies))))
7753       (setq last-id id
7754             id (gnus-parent-id (mail-header-references prev))))
7755     last-id))
7756
7757 (defun gnus-remove-thread (id &optional dont-remove)
7758   "Remove the thread that has ID in it."
7759   (let ((dep gnus-newsgroup-dependencies)
7760         headers thread prev last-id)
7761     ;; First go up in this thread until we find the root.
7762     (setq last-id (gnus-root-id id))
7763     (setq headers (list (car (gnus-id-to-thread last-id))
7764                         (car (car (cdr (gnus-id-to-thread last-id))))))
7765     ;; We have now found the real root of this thread.  It might have
7766     ;; been gathered into some loose thread, so we have to search
7767     ;; through the threads to find the thread we wanted.
7768     (let ((threads gnus-newsgroup-threads)
7769           sub)
7770       (while threads
7771         (setq sub (car threads))
7772         (if (stringp (car sub))
7773             ;; This is a gathered threads, so we look at the roots
7774             ;; below it to find whether this article in in this
7775             ;; gathered root.
7776             (progn
7777               (setq sub (cdr sub))
7778               (while sub
7779                 (when (member (car (car sub)) headers)
7780                   (setq thread (car threads)
7781                         threads nil
7782                         sub nil))
7783                 (setq sub (cdr sub))))
7784           ;; It's an ordinary thread, so we check it.
7785           (when (eq (car sub) (car headers))
7786             (setq thread sub
7787                   threads nil)))
7788         (setq threads (cdr threads)))
7789       ;; If this article is in no thread, then it's a root.
7790       (if thread
7791           (unless dont-remove
7792             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7793         (setq thread (gnus-gethash (downcase last-id) dep)))
7794       (when thread
7795         (prog1
7796             thread ; We return this thread.
7797           (unless dont-remove
7798             (if (stringp (car thread))
7799                 (progn
7800                   ;; If we use dummy roots, then we have to remove the
7801                   ;; dummy root as well.
7802                   (when (eq gnus-summary-make-false-root 'dummy)
7803                     ;; Uhm.
7804                     )
7805                   (setq thread (cdr thread))
7806                   (while thread
7807                     (gnus-remove-thread-1 (car thread))
7808                     (setq thread (cdr thread))))
7809               (gnus-remove-thread-1 thread))))))))
7810
7811 (defun gnus-remove-thread-1 (thread)
7812   "Remove the thread THREAD recursively."
7813   (let ((number (mail-header-number (car thread)))
7814         pos)
7815     (when (setq pos (text-property-any
7816                      (point-min) (point-max) 'gnus-number number))
7817       (goto-char pos)
7818       (gnus-delete-line)
7819       (gnus-data-remove number))
7820     (setq thread (cdr thread))
7821     (while thread
7822       (gnus-remove-thread-1 (car thread))
7823       (setq thread (cdr thread)))))
7824
7825 (defun gnus-sort-threads (threads)
7826   "Sort THREADS."
7827   (if (not gnus-thread-sort-functions)
7828       threads
7829     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7830                     (car gnus-thread-sort-functions)
7831                   `(lambda (t1 t2)
7832                      ,(gnus-make-sort-function 
7833                        (reverse gnus-thread-sort-functions))))))
7834       (gnus-message 7 "Sorting threads...")
7835       (prog1
7836           (sort threads func)
7837         (gnus-message 7 "Sorting threads...done")))))
7838
7839 (defun gnus-sort-articles (articles)
7840   "Sort ARTICLES."
7841   (when gnus-article-sort-functions
7842     (let ((func (if (= 1 (length gnus-article-sort-functions))
7843                     (car gnus-article-sort-functions)
7844                   `(lambda (t1 t2)
7845                      ,(gnus-make-sort-function 
7846                        (reverse gnus-article-sort-functions))))))
7847       (gnus-message 7 "Sorting articles...")
7848       (prog1
7849           (setq gnus-newsgroup-headers (sort articles func))
7850         (gnus-message 7 "Sorting articles...done")))))
7851
7852 (defun gnus-make-sort-function (funs)
7853   "Return a composite sort condition based on the functions in FUNC."
7854   (if (cdr funs)
7855       `(or (,(car funs) t1 t2)
7856            (and (not (,(car funs) t2 t1))
7857                 ,(gnus-make-sort-function (cdr funs))))
7858     `(,(car funs) t1 t2)))
7859                  
7860 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7861 (defmacro gnus-thread-header (thread)
7862   ;; Return header of first article in THREAD.
7863   ;; Note that THREAD must never, ever be anything else than a variable -
7864   ;; using some other form will lead to serious barfage.
7865   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7866   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7867   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
7868         (vector thread) 2))
7869
7870 (defsubst gnus-article-sort-by-number (h1 h2)
7871   "Sort articles by article number."
7872   (< (mail-header-number h1)
7873      (mail-header-number h2)))
7874
7875 (defun gnus-thread-sort-by-number (h1 h2)
7876   "Sort threads by root article number."
7877   (gnus-article-sort-by-number
7878    (gnus-thread-header h1) (gnus-thread-header h2)))
7879
7880 (defsubst gnus-article-sort-by-author (h1 h2)
7881   "Sort articles by root author."
7882   (string-lessp
7883    (let ((extract (funcall
7884                    gnus-extract-address-components
7885                    (mail-header-from h1))))
7886      (or (car extract) (cdr extract)))
7887    (let ((extract (funcall
7888                    gnus-extract-address-components
7889                    (mail-header-from h2))))
7890      (or (car extract) (cdr extract)))))
7891
7892 (defun gnus-thread-sort-by-author (h1 h2)
7893   "Sort threads by root author."
7894   (gnus-article-sort-by-author
7895    (gnus-thread-header h1)  (gnus-thread-header h2)))
7896
7897 (defsubst gnus-article-sort-by-subject (h1 h2)
7898   "Sort articles by root subject."
7899   (string-lessp
7900    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
7901    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
7902
7903 (defun gnus-thread-sort-by-subject (h1 h2)
7904   "Sort threads by root subject."
7905   (gnus-article-sort-by-subject
7906    (gnus-thread-header h1) (gnus-thread-header h2)))
7907
7908 (defsubst gnus-article-sort-by-date (h1 h2)
7909   "Sort articles by root article date."
7910   (string-lessp
7911    (gnus-sortable-date (mail-header-date h1))
7912    (gnus-sortable-date (mail-header-date h2))))
7913
7914 (defun gnus-thread-sort-by-date (h1 h2)
7915   "Sort threads by root article date."
7916   (gnus-article-sort-by-date
7917    (gnus-thread-header h1) (gnus-thread-header h2)))
7918
7919 (defsubst gnus-article-sort-by-score (h1 h2)
7920   "Sort articles by root article score.
7921 Unscored articles will be counted as having a score of zero."
7922   (> (or (cdr (assq (mail-header-number h1)
7923                     gnus-newsgroup-scored))
7924          gnus-summary-default-score 0)
7925      (or (cdr (assq (mail-header-number h2)
7926                     gnus-newsgroup-scored))
7927          gnus-summary-default-score 0)))
7928
7929 (defun gnus-thread-sort-by-score (h1 h2)
7930   "Sort threads by root article score."
7931   (gnus-article-sort-by-score
7932    (gnus-thread-header h1) (gnus-thread-header h2)))
7933
7934 (defun gnus-thread-sort-by-total-score (h1 h2)
7935   "Sort threads by the sum of all scores in the thread.
7936 Unscored articles will be counted as having a score of zero."
7937   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
7938
7939 (defun gnus-thread-total-score (thread)
7940   ;;  This function find the total score of THREAD.
7941   (if (consp thread)
7942       (if (stringp (car thread))
7943           (apply gnus-thread-score-function 0
7944                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
7945         (gnus-thread-total-score-1 thread))
7946     (gnus-thread-total-score-1 (list thread))))
7947
7948 (defun gnus-thread-total-score-1 (root)
7949   ;; This function find the total score of the thread below ROOT.
7950   (setq root (car root))
7951   (apply gnus-thread-score-function
7952          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
7953              gnus-summary-default-score 0)
7954          (mapcar 'gnus-thread-total-score
7955                  (cdr (gnus-gethash (downcase (mail-header-id root))
7956                                     gnus-newsgroup-dependencies)))))
7957
7958 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7959 (defvar gnus-tmp-prev-subject nil)
7960 (defvar gnus-tmp-false-parent nil)
7961 (defvar gnus-tmp-root-expunged nil)
7962 (defvar gnus-tmp-dummy-line nil)
7963
7964 (defun gnus-summary-prepare-threads (threads)
7965   "Prepare summary buffer from THREADS and indentation LEVEL.
7966 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
7967 or a straight list of headers."
7968   (gnus-message 7 "Generating summary...")
7969
7970   (setq gnus-newsgroup-threads threads)
7971   (beginning-of-line)
7972
7973   (let ((gnus-tmp-level 0)
7974         (default-score (or gnus-summary-default-score 0))
7975         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
7976         thread number subject stack state gnus-tmp-gathered beg-match
7977         new-roots gnus-tmp-new-adopts thread-end
7978         gnus-tmp-header gnus-tmp-unread
7979         gnus-tmp-replied gnus-tmp-subject-or-nil
7980         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
7981         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
7982         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
7983
7984     (setq gnus-tmp-prev-subject nil)
7985
7986     (if (vectorp (car threads))
7987         ;; If this is a straight (sic) list of headers, then a
7988         ;; threaded summary display isn't required, so we just create
7989         ;; an unthreaded one.
7990         (gnus-summary-prepare-unthreaded threads)
7991
7992       ;; Do the threaded display.
7993
7994       (while (or threads stack gnus-tmp-new-adopts new-roots)
7995
7996         (if (and (= gnus-tmp-level 0)
7997                  (not (setq gnus-tmp-dummy-line nil))
7998                  (or (not stack)
7999                      (= (car (car stack)) 0))
8000                  (not gnus-tmp-false-parent)
8001                  (or gnus-tmp-new-adopts new-roots))
8002             (if gnus-tmp-new-adopts
8003                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8004                       thread (list (car gnus-tmp-new-adopts))
8005                       gnus-tmp-header (car (car thread))
8006                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8007               (if new-roots
8008                   (setq thread (list (car new-roots))
8009                         gnus-tmp-header (car (car thread))
8010                         new-roots (cdr new-roots))))
8011
8012           (if threads
8013               ;; If there are some threads, we do them before the
8014               ;; threads on the stack.
8015               (setq thread threads
8016                     gnus-tmp-header (car (car thread)))
8017             ;; There were no current threads, so we pop something off
8018             ;; the stack.
8019             (setq state (car stack)
8020                   gnus-tmp-level (car state)
8021                   thread (cdr state)
8022                   stack (cdr stack)
8023                   gnus-tmp-header (car (car thread)))))
8024
8025         (setq gnus-tmp-false-parent nil)
8026         (setq gnus-tmp-root-expunged nil)
8027         (setq thread-end nil)
8028
8029         (if (stringp gnus-tmp-header)
8030             ;; The header is a dummy root.
8031             (cond
8032              ((eq gnus-summary-make-false-root 'adopt)
8033               ;; We let the first article adopt the rest.
8034               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8035                                                (cdr (cdr (car thread)))))
8036               (setq gnus-tmp-gathered
8037                     (nconc (mapcar
8038                             (lambda (h) (mail-header-number (car h)))
8039                             (cdr (cdr (car thread))))
8040                            gnus-tmp-gathered))
8041               (setq thread (cons (list (car (car thread))
8042                                        (car (cdr (car thread))))
8043                                  (cdr thread)))
8044               (setq gnus-tmp-level -1
8045                     gnus-tmp-false-parent t))
8046              ((eq gnus-summary-make-false-root 'empty)
8047               ;; We print adopted articles with empty subject fields.
8048               (setq gnus-tmp-gathered
8049                     (nconc (mapcar
8050                             (lambda (h) (mail-header-number (car h)))
8051                             (cdr (cdr (car thread))))
8052                            gnus-tmp-gathered))
8053               (setq gnus-tmp-level -1))
8054              ((eq gnus-summary-make-false-root 'dummy)
8055               ;; We remember that we probably want to output a dummy
8056               ;; root.
8057               (setq gnus-tmp-dummy-line gnus-tmp-header)
8058               (setq gnus-tmp-prev-subject gnus-tmp-header))
8059              (t
8060               ;; We do not make a root for the gathered
8061               ;; sub-threads at all.
8062               (setq gnus-tmp-level -1)))
8063
8064           (setq number (mail-header-number gnus-tmp-header)
8065                 subject (mail-header-subject gnus-tmp-header))
8066
8067           (cond
8068            ;; If the thread has changed subject, we might want to make
8069            ;; this subthread into a root.
8070            ((and (null gnus-thread-ignore-subject)
8071                  (not (zerop gnus-tmp-level))
8072                  gnus-tmp-prev-subject
8073                  (not (inline
8074                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8075             (setq new-roots (nconc new-roots (list (car thread)))
8076                   thread-end t
8077                   gnus-tmp-header nil))
8078            ;; If the article lies outside the current limit,
8079            ;; then we do not display it.
8080            ((and (not (memq number gnus-newsgroup-limit))
8081                  (not gnus-tmp-dummy-line))
8082             (setq gnus-tmp-gathered
8083                   (nconc (mapcar
8084                           (lambda (h) (mail-header-number (car h)))
8085                           (cdr (car thread)))
8086                          gnus-tmp-gathered))
8087             (setq gnus-tmp-new-adopts (if (cdr (car thread))
8088                                           (append gnus-tmp-new-adopts
8089                                                   (cdr (car thread)))
8090                                         gnus-tmp-new-adopts)
8091                   thread-end t
8092                   gnus-tmp-header nil)
8093             (when (zerop gnus-tmp-level)
8094               (setq gnus-tmp-root-expunged t)))
8095            ;; Perhaps this article is to be marked as read?
8096            ((and gnus-summary-mark-below
8097                  (< (or (cdr (assq number gnus-newsgroup-scored))
8098                         default-score)
8099                     gnus-summary-mark-below))
8100             (setq gnus-newsgroup-unreads
8101                   (delq number gnus-newsgroup-unreads))
8102             (if gnus-newsgroup-auto-expire
8103                 (push number gnus-newsgroup-expirable)
8104               (push (cons number gnus-low-score-mark)
8105                     gnus-newsgroup-reads))))
8106
8107           (when gnus-tmp-header
8108             ;; We may have an old dummy line to output before this
8109             ;; article.
8110             (if gnus-tmp-dummy-line
8111                 (progn
8112                   (gnus-summary-insert-dummy-line
8113                    gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8114                   (setq gnus-tmp-dummy-line nil))
8115
8116               ;; Compute the mark.
8117               (setq
8118                gnus-tmp-unread
8119                (cond
8120                 ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8121                 ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8122                 ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8123                 ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8124                 (t (or (cdr (assq number gnus-newsgroup-reads))
8125                        gnus-ancient-mark))))
8126
8127               (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8128                                     gnus-tmp-header gnus-tmp-level)
8129                     gnus-newsgroup-data)
8130
8131               ;; Actually insert the line.
8132               (setq
8133                gnus-tmp-subject-or-nil
8134                (cond
8135                 ((and gnus-thread-ignore-subject
8136                       gnus-tmp-prev-subject
8137                       (not (inline (gnus-subject-equal
8138                                     gnus-tmp-prev-subject subject))))
8139                  subject)
8140                 ((zerop gnus-tmp-level)
8141                  (if (and (eq gnus-summary-make-false-root 'empty)
8142                           (memq number gnus-tmp-gathered)
8143                           gnus-tmp-prev-subject
8144                           (inline (gnus-subject-equal
8145                                    gnus-tmp-prev-subject subject)))
8146                      gnus-summary-same-subject
8147                    subject))
8148                 (t gnus-summary-same-subject)))
8149               (if (and (eq gnus-summary-make-false-root 'adopt)
8150                        (= gnus-tmp-level 1)
8151                        (memq number gnus-tmp-gathered))
8152                   (setq gnus-tmp-opening-bracket ?\<
8153                         gnus-tmp-closing-bracket ?\>)
8154                 (setq gnus-tmp-opening-bracket ?\[
8155                       gnus-tmp-closing-bracket ?\]))
8156               (setq
8157                gnus-tmp-indentation
8158                (aref gnus-thread-indent-array gnus-tmp-level)
8159                gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8160                gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8161                                   gnus-summary-default-score 0)
8162                gnus-tmp-score-char
8163                (if (or (null gnus-summary-default-score)
8164                        (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8165                            gnus-summary-zcore-fuzz)) ? 
8166                  (if (< gnus-tmp-score gnus-summary-default-score)
8167                      gnus-score-below-mark gnus-score-over-mark))
8168                gnus-tmp-replied
8169                (cond ((memq number gnus-newsgroup-processable)
8170                       gnus-process-mark)
8171                      ((memq number gnus-newsgroup-cached)
8172                       gnus-cached-mark)
8173                      ((memq number gnus-newsgroup-replied)
8174                       gnus-replied-mark)
8175                      (t gnus-unread-mark))
8176                gnus-tmp-from (mail-header-from gnus-tmp-header)
8177                gnus-tmp-name
8178                (cond
8179                 ((string-match "(.+)" gnus-tmp-from)
8180                  (substring gnus-tmp-from
8181                             (1+ (match-beginning 0)) (1- (match-end 0))))
8182                 ((string-match "<[^>]+> *$" gnus-tmp-from)
8183                  (setq beg-match (match-beginning 0))
8184                  (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8185                           (substring gnus-tmp-from (1+ (match-beginning 0))
8186                                      (1- (match-end 0))))
8187                      (substring gnus-tmp-from 0 beg-match)))
8188                 (t gnus-tmp-from)))
8189               (when (string= gnus-tmp-name "")
8190                 (setq gnus-tmp-name gnus-tmp-from))
8191               (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8192               (put-text-property
8193                (point)
8194                (progn (eval gnus-summary-line-format-spec) (point))
8195                'gnus-number number)
8196               (when gnus-visual-p
8197                 (forward-line -1)
8198                 (run-hooks 'gnus-summary-update-hook)
8199                 (forward-line 1))
8200
8201               )
8202
8203             (setq gnus-tmp-prev-subject subject)))
8204
8205         (when (nth 1 thread)
8206           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8207         (incf gnus-tmp-level)
8208         (setq threads (if thread-end nil (cdr (car thread))))
8209         (unless threads
8210           (setq gnus-tmp-level 0)))))
8211   (gnus-message 7 "Generating summary...done"))
8212
8213 (defun gnus-summary-prepare-unthreaded (headers)
8214   "Generate an unthreaded summary buffer based on HEADERS."
8215   (let (header number mark)
8216
8217     (while headers
8218       (setq header (car headers)
8219             headers (cdr headers)
8220             number (mail-header-number header))
8221
8222       ;; We may have to root out some bad articles...
8223       (when (memq number gnus-newsgroup-limit)
8224         (when (and gnus-summary-mark-below
8225                    (< (or (cdr (assq number gnus-newsgroup-scored))
8226                           gnus-summary-default-score 0)
8227                       gnus-summary-mark-below))
8228           (setq gnus-newsgroup-unreads
8229                 (delq number gnus-newsgroup-unreads))
8230           (if gnus-newsgroup-auto-expire
8231               (push number gnus-newsgroup-expirable)
8232             (push (cons number gnus-low-score-mark)
8233                   gnus-newsgroup-reads)))
8234
8235         (setq mark
8236               (cond
8237                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8238                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8239                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8240                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8241                (t (or (cdr (assq number gnus-newsgroup-reads))
8242                       gnus-ancient-mark))))
8243         (setq gnus-newsgroup-data
8244               (cons (gnus-data-make number mark (1+ (point)) header 0)
8245                     gnus-newsgroup-data))
8246         (gnus-summary-insert-line
8247          header 0 nil mark (memq number gnus-newsgroup-replied)
8248          (memq number gnus-newsgroup-expirable)
8249          (mail-header-subject header) nil
8250          (cdr (assq number gnus-newsgroup-scored))
8251          (memq number gnus-newsgroup-processable))))))
8252
8253 (defun gnus-select-newsgroup (group &optional read-all)
8254   "Select newsgroup GROUP.
8255 If READ-ALL is non-nil, all articles in the group are selected."
8256   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8257          (info (nth 2 entry))
8258          articles fetched-articles cached)
8259
8260     (or (gnus-check-server
8261          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8262         (error "Couldn't open server"))
8263
8264     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8265         (gnus-activate-group group) ; Or we can activate it...
8266         (progn ; Or we bug out.
8267           (kill-buffer (current-buffer))
8268           (error "Couldn't request group %s: %s"
8269                  group (gnus-status-message group))))
8270
8271     (setq gnus-newsgroup-name group)
8272     (setq gnus-newsgroup-unselected nil)
8273     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8274
8275     (and gnus-asynchronous
8276          (gnus-check-backend-function
8277           'request-asynchronous gnus-newsgroup-name)
8278          (setq gnus-newsgroup-async
8279                (gnus-request-asynchronous gnus-newsgroup-name)))
8280
8281     ;; Adjust and set lists of article marks.
8282     (when info
8283       (gnus-adjust-marked-articles info))
8284
8285     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8286     (when (gnus-virtual-group-p group)
8287       (setq cached gnus-newsgroup-cached))
8288
8289     (setq gnus-newsgroup-unreads
8290           (gnus-set-difference
8291            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8292            gnus-newsgroup-dormant))
8293
8294     (setq gnus-newsgroup-processable nil)
8295
8296     (setq articles (gnus-articles-to-read group read-all))
8297
8298     (cond
8299      ((null articles)
8300       (gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8301       'quit)
8302      ((eq articles 0) nil)
8303      (t
8304       ;; Init the dependencies hash table.
8305       (setq gnus-newsgroup-dependencies
8306             (gnus-make-hashtable (length articles)))
8307       ;; Retrieve the headers and read them in.
8308       (gnus-message 5 "Fetching headers...")
8309       (setq gnus-newsgroup-headers
8310             (if (eq 'nov
8311                     (setq gnus-headers-retrieved-by
8312                           (gnus-retrieve-headers
8313                            articles gnus-newsgroup-name
8314                            ;; We might want to fetch old headers, but
8315                            ;; not if there is only 1 article.
8316                            (and gnus-fetch-old-headers
8317                                 (or (and
8318                                      (not (eq gnus-fetch-old-headers 'some))
8319                                      (not (numberp gnus-fetch-old-headers)))
8320                                     (> (length articles) 1))))))
8321                 (gnus-get-newsgroup-headers-xover articles)
8322               (gnus-get-newsgroup-headers)))
8323       (gnus-message 5 "Fetching headers...done")
8324
8325       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8326       (when cached
8327         (setq gnus-newsgroup-cached cached))
8328
8329       ;; Set the initial limit.
8330       (setq gnus-newsgroup-limit (copy-sequence articles))
8331       ;; Remove canceled articles from the list of unread articles.
8332       (setq gnus-newsgroup-unreads
8333             (gnus-set-sorted-intersection
8334              gnus-newsgroup-unreads
8335              (setq fetched-articles
8336                    (mapcar (lambda (headers) (mail-header-number headers))
8337                            gnus-newsgroup-headers))))
8338       ;; Removed marked articles that do not exist.
8339       (gnus-update-missing-marks
8340        (gnus-sorted-complement fetched-articles articles))
8341       ;; We might want to build some more threads first.
8342       (and gnus-fetch-old-headers
8343            (eq gnus-headers-retrieved-by 'nov)
8344            (gnus-build-old-threads))
8345       ;; Check whether auto-expire is to be done in this group.
8346       (setq gnus-newsgroup-auto-expire
8347             (gnus-group-auto-expirable-p group))
8348       ;; First and last article in this newsgroup.
8349       (and gnus-newsgroup-headers
8350            (setq gnus-newsgroup-begin
8351                  (mail-header-number (car gnus-newsgroup-headers)))
8352            (setq gnus-newsgroup-end
8353                  (mail-header-number
8354                   (gnus-last-element gnus-newsgroup-headers))))
8355       (setq gnus-reffed-article-number -1)
8356       ;; GROUP is successfully selected.
8357       (or gnus-newsgroup-headers t)))))
8358
8359 (defun gnus-articles-to-read (group read-all)
8360   ;; Find out what articles the user wants to read.
8361   (let* ((articles
8362           ;; Select all articles if `read-all' is non-nil, or if there
8363           ;; are no unread articles.
8364           (if (or read-all
8365                   (and (zerop (length gnus-newsgroup-marked))
8366                        (zerop (length gnus-newsgroup-unreads))))
8367               (gnus-uncompress-range (gnus-active group))
8368             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8369                           (copy-sequence gnus-newsgroup-unreads))
8370                   '<)))
8371          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8372          (scored (length scored-list))
8373          (number (length articles))
8374          (marked (+ (length gnus-newsgroup-marked)
8375                     (length gnus-newsgroup-dormant)))
8376          (select
8377           (cond
8378            ((numberp read-all)
8379             read-all)
8380            (t
8381             (condition-case ()
8382                 (cond
8383                  ((and (or (<= scored marked) (= scored number))
8384                        (numberp gnus-large-newsgroup)
8385                        (> number gnus-large-newsgroup))
8386                   (let ((input
8387                          (read-string
8388                           (format
8389                            "How many articles from %s (default %d): "
8390                            gnus-newsgroup-name number))))
8391                     (if (string-match "^[ \t]*$" input) number input)))
8392                  ((and (> scored marked) (< scored number))
8393                   (let ((input
8394                          (read-string
8395                           (format "%s %s (%d scored, %d total): "
8396                                   "How many articles from"
8397                                   group scored number))))
8398                     (if (string-match "^[ \t]*$" input)
8399                         number input)))
8400                  (t number))
8401               (quit nil))))))
8402     (setq select (if (stringp select) (string-to-number select) select))
8403     (if (or (null select) (zerop select))
8404         select
8405       (if (and (not (zerop scored)) (<= (abs select) scored))
8406           (progn
8407             (setq articles (sort scored-list '<))
8408             (setq number (length articles)))
8409         (setq articles (copy-sequence articles)))
8410
8411       (if (< (abs select) number)
8412           (if (< select 0)
8413               ;; Select the N oldest articles.
8414               (setcdr (nthcdr (1- (abs select)) articles) nil)
8415             ;; Select the N most recent articles.
8416             (setq articles (nthcdr (- number select) articles))))
8417       (setq gnus-newsgroup-unselected
8418             (gnus-sorted-intersection
8419              gnus-newsgroup-unreads
8420              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8421       articles)))
8422
8423 (defun gnus-killed-articles (killed articles)
8424   (let (out)
8425     (while articles
8426       (if (inline (gnus-member-of-range (car articles) killed))
8427           (setq out (cons (car articles) out)))
8428       (setq articles (cdr articles)))
8429     out))
8430
8431 (defun gnus-uncompress-marks (marks)
8432   "Uncompress the mark ranges in MARKS."
8433   (let ((uncompressed '(score bookmark))
8434         out)
8435     (while marks
8436       (if (memq (caar marks) uncompressed)
8437           (push (car marks) out)
8438         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8439       (setq marks (cdr marks)))
8440     out))
8441
8442 (defun gnus-adjust-marked-articles (info)
8443   "Set all article lists and remove all marks that are no longer legal."
8444   (let* ((marked-lists (gnus-info-marks info))
8445          (active (gnus-active (gnus-info-group info)))
8446          (min (car active))
8447          (max (cdr active))
8448          (types gnus-article-mark-lists)
8449          (uncompressed '(score bookmark))
8450          marks var articles article mark)
8451
8452     (while marked-lists
8453       (setq marks (pop marked-lists))
8454       (set (setq var (intern (format "gnus-newsgroup-%s"
8455                                      (car (rassq (setq mark (car marks))
8456                                                  types)))))
8457            (if (memq (car marks) uncompressed) (cdr marks)
8458              (gnus-uncompress-range (cdr marks))))
8459
8460       (setq articles (symbol-value var))
8461
8462       ;; All articles have to be subsets of the active articles.
8463       (cond
8464        ;; Adjust "simple" lists.
8465        ((memq mark '(tick dormant expirable reply killed save))
8466         (while articles
8467           (when (or (< (setq article (pop articles)) min) (> article max))
8468             (set var (delq article (symbol-value var))))))
8469        ;; Adjust assocs.
8470        ((memq mark '(score bookmark))
8471         (while articles
8472           (when (or (< (car (setq article (pop articles))) min)
8473                     (> (car article) max))
8474             (set var (delq article (symbol-value var))))))))))
8475
8476 (defun gnus-update-missing-marks (missing)
8477   "Go through the list of MISSING articles and remove them mark lists."
8478   (when missing
8479     (let ((types gnus-article-mark-lists)
8480           var m)
8481       ;; Go through all types.
8482       (while types
8483         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8484         (when (symbol-value var)
8485           ;; This list has articles.  So we delete all missing articles
8486           ;; from it.
8487           (setq m missing)
8488           (while m
8489             (set var (delq (pop m) (symbol-value var)))))))))
8490
8491 (defun gnus-update-marks ()
8492   "Enter the various lists of marked articles into the newsgroup info list."
8493   (let ((types gnus-article-mark-lists)
8494         (info (gnus-get-info gnus-newsgroup-name))
8495         (uncompressed '(score bookmark killed))
8496         var type list newmarked symbol)
8497     (when info
8498       ;; Add all marks lists that are non-nil to the list of marks lists.
8499       (while types
8500         (setq type (pop types))
8501         (when (setq list (symbol-value
8502                           (setq symbol
8503                                 (intern (format "gnus-newsgroup-%s"
8504                                                 (car type))))))
8505           (push (cons (cdr type)
8506                       (if (memq (cdr type) uncompressed) list
8507                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8508                 newmarked)))
8509
8510       ;; Enter these new marks into the info of the group.
8511       (if (nthcdr 3 info)
8512           (setcar (nthcdr 3 info) newmarked)
8513         ;; Add the marks lists to the end of the info.
8514         (when newmarked
8515           (setcdr (nthcdr 2 info) (list newmarked))))
8516
8517       ;; Cut off the end of the info if there's nothing else there.
8518       (let ((i 5))
8519         (while (and (> i 2)
8520                     (not (nth i info)))
8521           (when (nthcdr (decf i) info)
8522             (setcdr (nthcdr i info) nil)))))))
8523
8524 (defun gnus-add-marked-articles (group type articles &optional info force)
8525   ;; Add ARTICLES of TYPE to the info of GROUP.
8526   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8527   ;; add, but replace marked articles of TYPE with ARTICLES.
8528   (let ((info (or info (gnus-get-info group)))
8529         (uncompressed '(score bookmark killed))
8530         marked m)
8531     (or (not info)
8532         (and (not (setq marked (nthcdr 3 info)))
8533              (setcdr (nthcdr 2 info)
8534                      (list (list (cons type (gnus-compress-sequence
8535                                              articles t))))))
8536         (and (not (setq m (assq type (car marked))))
8537              (setcar marked
8538                      (cons (cons type (gnus-compress-sequence articles t) )
8539                            (car marked))))
8540         (if force
8541             (setcdr m (gnus-compress-sequence articles t))
8542           (setcdr m (gnus-compress-sequence
8543                      (sort (nconc (gnus-uncompress-range m)
8544                                   (copy-sequence articles)) '<) t))))))
8545
8546 (defun gnus-set-mode-line (where)
8547   "This function sets the mode line of the article or summary buffers.
8548 If WHERE is `summary', the summary mode line format will be used."
8549   ;; Is this mode line one we keep updated?
8550   (when (memq where gnus-updated-mode-lines)
8551     (let (mode-string)
8552       (save-excursion
8553         ;; We evaluate this in the summary buffer since these
8554         ;; variables are buffer-local to that buffer.
8555         (set-buffer gnus-summary-buffer)
8556         ;; We bind all these variables that are used in the `eval' form
8557         ;; below.
8558         (let* ((mformat (symbol-value
8559                          (intern
8560                           (format "gnus-%s-mode-line-format-spec" where))))
8561                (gnus-tmp-group-name gnus-newsgroup-name)
8562                (gnus-tmp-article-number (or gnus-current-article 0))
8563                (gnus-tmp-unread gnus-newsgroup-unreads)
8564                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8565                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8566                (gnus-tmp-unread-and-unselected
8567                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8568                             (zerop gnus-tmp-unselected)) "")
8569                       ((zerop gnus-tmp-unselected)
8570                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8571                       (t (format "{%d(+%d) more}"
8572                                  gnus-tmp-unread-and-unticked
8573                                  gnus-tmp-unselected))))
8574                (gnus-tmp-subject
8575                 (if (and gnus-current-headers
8576                          (vectorp gnus-current-headers))
8577                     (mail-header-subject gnus-current-headers) ""))
8578                max-len
8579                gnus-tmp-header);; passed as argument to any user-format-funcs
8580           (setq mode-string (eval mformat))
8581           (setq max-len (max 4 (if gnus-mode-non-string-length
8582                                    (- (frame-width)
8583                                       gnus-mode-non-string-length)
8584                                  (length mode-string))))
8585           ;; We might have to chop a bit of the string off...
8586           (when (> (length mode-string) max-len)
8587             (setq mode-string
8588                   (concat (gnus-truncate-string mode-string (- max-len 3))
8589                           "...")))
8590           ;; Pad the mode string a bit.
8591           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8592       ;; Update the mode line.
8593       (setq mode-line-buffer-identification (list mode-string))
8594       (set-buffer-modified-p t))))
8595
8596 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8597   "Go through the HEADERS list and add all Xrefs to a hash table.
8598 The resulting hash table is returned, or nil if no Xrefs were found."
8599   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8600          (virtual (gnus-virtual-group-p from-newsgroup))
8601          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8602          (xref-hashtb (make-vector 63 0))
8603          start group entry number xrefs header)
8604     (while headers
8605       (setq header (pop headers))
8606       (when (and (setq xrefs (mail-header-xref header))
8607                  (not (memq (setq number (mail-header-number header))
8608                             unreads)))
8609         (setq start 0)
8610         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8611           (setq start (match-end 0))
8612           (setq group (concat prefix (substring xrefs (match-beginning 1)
8613                                                 (match-end 1))))
8614           (setq number
8615                 (string-to-int (substring xrefs (match-beginning 2)
8616                                           (match-end 2))))
8617           (if (setq entry (gnus-gethash group xref-hashtb))
8618               (setcdr entry (cons number (cdr entry)))
8619             (gnus-sethash group (cons number nil) xref-hashtb)))))
8620     (and start xref-hashtb)))
8621
8622 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8623   "Look through all the headers and mark the Xrefs as read."
8624   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8625         name entry info xref-hashtb idlist method nth4)
8626     (save-excursion
8627       (set-buffer gnus-group-buffer)
8628       (when (setq xref-hashtb
8629                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8630         (mapatoms
8631          (lambda (group)
8632            (unless (string= from-newsgroup (setq name (symbol-name group)))
8633              (setq idlist (symbol-value group))
8634              ;; Dead groups are not updated.
8635              (and (prog1
8636                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8637                             info (nth 2 entry))
8638                     (if (stringp (setq nth4 (gnus-info-method info)))
8639                         (setq nth4 (gnus-server-to-method nth4))))
8640                   ;; Only do the xrefs if the group has the same
8641                   ;; select method as the group we have just read.
8642                   (or (gnus-methods-equal-p
8643                        nth4 (gnus-find-method-for-group from-newsgroup))
8644                       virtual
8645                       (equal nth4 (setq method (gnus-find-method-for-group
8646                                                 from-newsgroup)))
8647                       (and (equal (car nth4) (car method))
8648                            (equal (nth 1 nth4) (nth 1 method))))
8649                   gnus-use-cross-reference
8650                   (or (not (eq gnus-use-cross-reference t))
8651                       virtual
8652                       ;; Only do cross-references on subscribed
8653                       ;; groups, if that is what is wanted.
8654                       (<= (gnus-info-level info) gnus-level-subscribed))
8655                   (gnus-group-make-articles-read name idlist))))
8656          xref-hashtb)))))
8657
8658 (defun gnus-group-make-articles-read (group articles)
8659   (let* ((num 0)
8660          (entry (gnus-gethash group gnus-newsrc-hashtb))
8661          (info (nth 2 entry))
8662          (active (gnus-active group))
8663          range)
8664     ;; First peel off all illegal article numbers.
8665     (if active
8666         (let ((ids articles)
8667               id first)
8668           (while ids
8669             (setq id (car ids))
8670             (if (and first (> id (cdr active)))
8671                 (progn
8672                   ;; We'll end up in this situation in one particular
8673                   ;; obscure situation.  If you re-scan a group and get
8674                   ;; a new article that is cross-posted to a different
8675                   ;; group that has not been re-scanned, you might get
8676                   ;; crossposted article that has a higher number than
8677                   ;; Gnus believes possible.  So we re-activate this
8678                   ;; group as well.  This might mean doing the
8679                   ;; crossposting thingy will *increase* the number
8680                   ;; of articles in some groups.  Tsk, tsk.
8681                   (setq active (or (gnus-activate-group group) active))))
8682             (if (or (> id (cdr active))
8683                     (< id (car active)))
8684                 (setq articles (delq id articles)))
8685             (setq ids (cdr ids)))))
8686     ;; If the read list is nil, we init it.
8687     (and active
8688          (null (gnus-info-read info))
8689          (> (car active) 1)
8690          (gnus-info-set-read info (cons 1 (1- (car active)))))
8691     ;; Then we add the read articles to the range.
8692     (gnus-info-set-read
8693      info
8694      (setq range
8695            (gnus-add-to-range
8696             (gnus-info-read info) (setq articles (sort articles '<)))))
8697     ;; Then we have to re-compute how many unread
8698     ;; articles there are in this group.
8699     (if active
8700         (progn
8701           (cond
8702            ((not range)
8703             (setq num (- (1+ (cdr active)) (car active))))
8704            ((not (listp (cdr range)))
8705             (setq num (- (cdr active) (- (1+ (cdr range))
8706                                          (car range)))))
8707            (t
8708             (while range
8709               (if (numberp (car range))
8710                   (setq num (1+ num))
8711                 (setq num (+ num (- (1+ (cdr (car range)))
8712                                     (car (car range))))))
8713               (setq range (cdr range)))
8714             (setq num (- (cdr active) num))))
8715           ;; Update the number of unread articles.
8716           (setcar entry num)
8717           ;; Update the group buffer.
8718           (gnus-group-update-group group t)))))
8719
8720 (defun gnus-methods-equal-p (m1 m2)
8721   (let ((m1 (or m1 gnus-select-method))
8722         (m2 (or m2 gnus-select-method)))
8723     (or (equal m1 m2)
8724         (and (eq (car m1) (car m2))
8725              (or (not (memq 'address (assoc (symbol-name (car m1))
8726                                             gnus-valid-select-methods)))
8727                  (equal (nth 1 m1) (nth 1 m2)))))))
8728
8729 (defsubst gnus-header-value ()
8730   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8731
8732 (defvar gnus-newsgroup-none-id 0)
8733
8734 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8735   (let ((cur nntp-server-buffer)
8736         (dependencies
8737          (or dependencies
8738              (save-excursion (set-buffer gnus-summary-buffer)
8739                              gnus-newsgroup-dependencies)))
8740         headers id id-dep ref-dep end ref)
8741     (save-excursion
8742       (set-buffer nntp-server-buffer)
8743       (let ((case-fold-search t)
8744             in-reply-to header number p lines)
8745         (goto-char (point-min))
8746         ;; Search to the beginning of the next header.  Error messages
8747         ;; do not begin with 2 or 3.
8748         (while (re-search-forward "^[23][0-9]+ " nil t)
8749           (setq id nil
8750                 ref nil)
8751           ;; This implementation of this function, with nine
8752           ;; search-forwards instead of the one re-search-forward and
8753           ;; a case (which basically was the old function) is actually
8754           ;; about twice as fast, even though it looks messier.  You
8755           ;; can't have everything, I guess.  Speed and elegance
8756           ;; doesn't always go hand in hand.
8757           (setq
8758            header
8759            (vector
8760             ;; Number.
8761             (prog1
8762                 (read cur)
8763               (end-of-line)
8764               (setq p (point))
8765               (narrow-to-region (point)
8766                                 (or (and (search-forward "\n.\n" nil t)
8767                                          (- (point) 2))
8768                                     (point))))
8769             ;; Subject.
8770             (progn
8771               (goto-char p)
8772               (if (search-forward "\nsubject: " nil t)
8773                   (gnus-header-value) "(none)"))
8774             ;; From.
8775             (progn
8776               (goto-char p)
8777               (if (search-forward "\nfrom: " nil t)
8778                   (gnus-header-value) "(nobody)"))
8779             ;; Date.
8780             (progn
8781               (goto-char p)
8782               (if (search-forward "\ndate: " nil t)
8783                   (gnus-header-value) ""))
8784             ;; Message-ID.
8785             (progn
8786               (goto-char p)
8787               (if (search-forward "\nmessage-id: " nil t)
8788                   (setq id (gnus-header-value))
8789                 ;; If there was no message-id, we just fake one to make
8790                 ;; subsequent routines simpler.
8791                 (setq id (concat "none+"
8792                                  (int-to-string
8793                                   (setq gnus-newsgroup-none-id
8794                                         (1+ gnus-newsgroup-none-id)))))))
8795             ;; References.
8796             (progn
8797               (goto-char p)
8798               (if (search-forward "\nreferences: " nil t)
8799                   (prog1
8800                       (gnus-header-value)
8801                     (setq end (match-end 0))
8802                     (save-excursion
8803                       (setq ref
8804                             (downcase
8805                              (buffer-substring
8806                               (progn
8807                                 (end-of-line)
8808                                 (search-backward ">" end t)
8809                                 (1+ (point)))
8810                               (progn
8811                                 (search-backward "<" end t)
8812                                 (point)))))))
8813                 ;; Get the references from the in-reply-to header if there
8814                 ;; were no references and the in-reply-to header looks
8815                 ;; promising.
8816                 (if (and (search-forward "\nin-reply-to: " nil t)
8817                          (setq in-reply-to (gnus-header-value))
8818                          (string-match "<[^>]+>" in-reply-to))
8819                     (prog1
8820                         (setq ref (substring in-reply-to (match-beginning 0)
8821                                              (match-end 0)))
8822                       (setq ref (downcase ref))))
8823                 (setq ref "")))
8824             ;; Chars.
8825             0
8826             ;; Lines.
8827             (progn
8828               (goto-char p)
8829               (if (search-forward "\nlines: " nil t)
8830                   (if (numberp (setq lines (read cur)))
8831                       lines 0)
8832                 0))
8833             ;; Xref.
8834             (progn
8835               (goto-char p)
8836               (and (search-forward "\nxref: " nil t)
8837                    (gnus-header-value)))))
8838           (if (and gnus-nocem-hashtb
8839                    (gnus-gethash id gnus-nocem-hashtb))
8840               ;; Banned article.
8841               (setq header nil)
8842             ;; We do the threading while we read the headers.  The
8843             ;; message-id and the last reference are both entered into
8844             ;; the same hash table.  Some tippy-toeing around has to be
8845             ;; done in case an article has arrived before the article
8846             ;; which it refers to.
8847             (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8848                 (if (and (car (symbol-value id-dep))
8849                          (not force-new))
8850                     ;; An article with this Message-ID has already
8851                     ;; been seen, so we ignore this one, except we add
8852                     ;; any additional Xrefs (in case the two articles
8853                     ;; came from different servers).
8854                     (progn
8855                       (mail-header-set-xref
8856                        (car (symbol-value id-dep))
8857                        (concat (or (mail-header-xref
8858                                     (car (symbol-value id-dep))) "")
8859                                (or (mail-header-xref header) "")))
8860                       (setq header nil))
8861                   (setcar (symbol-value id-dep) header))
8862               (set id-dep (list header))))
8863           (when header
8864             (if (boundp (setq ref-dep (intern ref dependencies)))
8865                 (setcdr (symbol-value ref-dep)
8866                         (nconc (cdr (symbol-value ref-dep))
8867                                (list (symbol-value id-dep))))
8868               (set ref-dep (list nil (symbol-value id-dep))))
8869             (setq headers (cons header headers)))
8870           (goto-char (point-max))
8871           (widen))
8872         (nreverse headers)))))
8873
8874 ;; The following macros and functions were written by Felix Lee
8875 ;; <flee@cse.psu.edu>.
8876
8877 (defmacro gnus-nov-read-integer ()
8878   '(prog1
8879        (if (= (following-char) ?\t)
8880            0
8881          (let ((num (condition-case nil (read buffer) (error nil))))
8882            (if (numberp num) num 0)))
8883      (or (eobp) (forward-char 1))))
8884
8885 (defmacro gnus-nov-skip-field ()
8886   '(search-forward "\t" eol 'move))
8887
8888 (defmacro gnus-nov-field ()
8889   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
8890
8891 ;; Goes through the xover lines and returns a list of vectors
8892 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
8893   "Parse the news overview data in the server buffer, and return a
8894 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
8895   ;; Get the Xref when the users reads the articles since most/some
8896   ;; NNTP servers do not include Xrefs when using XOVER.
8897   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
8898   (let ((cur nntp-server-buffer)
8899         (dependencies gnus-newsgroup-dependencies)
8900         number headers header)
8901     (save-excursion
8902       (set-buffer nntp-server-buffer)
8903       ;; Allow the user to mangle the headers before parsing them.
8904       (run-hooks 'gnus-parse-headers-hook)
8905       ;; Allow the user to mangle the headers before parsing them.
8906       (run-hooks 'gnus-parse-headers-hook)
8907       (goto-char (point-min))
8908       (while (and sequence (not (eobp)))
8909         (setq number (read cur))
8910         (while (and sequence (< (car sequence) number))
8911           (setq sequence (cdr sequence)))
8912         (and sequence
8913              (eq number (car sequence))
8914              (progn
8915                (setq sequence (cdr sequence))
8916                (if (setq header
8917                          (inline (gnus-nov-parse-line
8918                                   number dependencies force-new)))
8919                    (setq headers (cons header headers)))))
8920         (forward-line 1))
8921       (setq headers (nreverse headers)))
8922     headers))
8923
8924 ;; This function has to be called with point after the article number
8925 ;; on the beginning of the line.
8926 (defun gnus-nov-parse-line (number dependencies &optional force-new)
8927   (let ((none 0)
8928         (eol (gnus-point-at-eol))
8929         (buffer (current-buffer))
8930         header ref id id-dep ref-dep)
8931
8932     ;; overview: [num subject from date id refs chars lines misc]
8933     (narrow-to-region (point) eol)
8934     (or (eobp) (forward-char))
8935
8936     (condition-case nil
8937         (setq header
8938               (vector
8939                number                   ; number
8940                (gnus-nov-field)         ; subject
8941                (gnus-nov-field)         ; from
8942                (gnus-nov-field)         ; date
8943                (setq id (or (gnus-nov-field)
8944                             (concat "none+"
8945                                     (int-to-string
8946                                      (setq none (1+ none)))))) ; id
8947                (progn
8948                  (save-excursion
8949                    (let ((beg (point)))
8950                      (search-forward "\t" eol)
8951                      (if (search-backward ">" beg t)
8952                          (setq ref
8953                                (downcase
8954                                 (buffer-substring
8955                                  (1+ (point))
8956                                  (progn
8957                                    (search-backward "<" beg t)
8958                                    (point)))))
8959                        (setq ref nil))))
8960                  (gnus-nov-field))      ; refs
8961                (gnus-nov-read-integer)  ; chars
8962                (gnus-nov-read-integer)  ; lines
8963                (if (= (following-char) ?\n)
8964                    nil
8965                  (gnus-nov-field))      ; misc
8966                ))
8967       (error (progn
8968                (ding)
8969                (gnus-message 4 "Strange nov line")
8970                (setq header nil)
8971                (goto-char eol))))
8972
8973     (widen)
8974
8975     ;; We build the thread tree.
8976     (and header
8977          (if (and gnus-nocem-hashtb
8978                   (gnus-gethash id gnus-nocem-hashtb))
8979              ;; Banned article.
8980              (setq header nil)
8981            (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8982                (if (and (car (symbol-value id-dep))
8983                         (not force-new))
8984                    ;; An article with this Message-ID has already been seen,
8985                    ;; so we ignore this one, except we add any additional
8986                    ;; Xrefs (in case the two articles came from different
8987                    ;; servers.
8988                    (progn
8989                      (mail-header-set-xref
8990                       (car (symbol-value id-dep))
8991                       (concat (or (mail-header-xref
8992                                    (car (symbol-value id-dep))) "")
8993                               (or (mail-header-xref header) "")))
8994                      (setq header nil))
8995                  (setcar (symbol-value id-dep) header))
8996              (set id-dep (list header)))))
8997     (if header
8998         (progn
8999           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9000               (setcdr (symbol-value ref-dep)
9001                       (nconc (cdr (symbol-value ref-dep))
9002                              (list (symbol-value id-dep))))
9003             (set ref-dep (list nil (symbol-value id-dep))))))
9004     header))
9005
9006 (defun gnus-article-get-xrefs ()
9007   "Fill in the Xref value in `gnus-current-headers', if necessary.
9008 This is meant to be called in `gnus-article-internal-prepare-hook'."
9009   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9010                                  gnus-current-headers)))
9011     (or (not gnus-use-cross-reference)
9012         (not headers)
9013         (and (mail-header-xref headers)
9014              (not (string= (mail-header-xref headers) "")))
9015         (let ((case-fold-search t)
9016               xref)
9017           (save-restriction
9018             (nnheader-narrow-to-headers)
9019             (goto-char (point-min))
9020             (if (or (and (eq (downcase (following-char)) ?x)
9021                          (looking-at "Xref:"))
9022                     (search-forward "\nXref:" nil t))
9023                 (progn
9024                   (goto-char (1+ (match-end 0)))
9025                   (setq xref (buffer-substring (point)
9026                                                (progn (end-of-line) (point))))
9027                   (mail-header-set-xref headers xref))))))))
9028
9029 (defun gnus-summary-insert-subject (id)
9030   "Find article ID and insert the summary line for that article."
9031   (let ((header (gnus-read-header id))
9032         (number (and (numberp id) id)))
9033     (when header
9034       ;; Rebuild the thread that this article is part of and go to the
9035       ;; article we have fetched.
9036       (gnus-rebuild-thread (mail-header-id header))
9037       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9038     (when (and (numberp number)
9039                (> number 0))
9040       ;; We have to update the boundaries even if we can't fetch the
9041       ;; article if ID is a number -- so that the next `P' or `N'
9042       ;; command will fetch the previous (or next) article even
9043       ;; if the one we tried to fetch this time has been canceled.
9044       (and (> number gnus-newsgroup-end)
9045            (setq gnus-newsgroup-end number))
9046       (and (< number gnus-newsgroup-begin)
9047            (setq gnus-newsgroup-begin number))
9048       (setq gnus-newsgroup-unselected
9049             (delq number gnus-newsgroup-unselected)))
9050     ;; Report back a success?
9051     (and header number)))
9052
9053 (defun gnus-summary-work-articles (n)
9054   "Return a list of articles to be worked upon.  The prefix argument,
9055 the list of process marked articles, and the current article will be
9056 taken into consideration."
9057   (cond
9058    ((and n (numberp n))
9059     ;; A numerical prefix has been given.
9060     (let ((backward (< n 0))
9061           (n (abs n))
9062           articles article)
9063       (save-excursion
9064         (while
9065             (and (> n 0)
9066                  (push (setq article (gnus-summary-article-number))
9067                        articles)
9068                  (if backward
9069                      (gnus-summary-find-prev nil article)
9070                    (gnus-summary-find-next nil article)))
9071           (decf n)))
9072       (nreverse articles)))
9073    ((and (boundp 'transient-mark-mode)
9074          transient-mark-mode
9075          mark-active)
9076     ;; Work on the region between point and mark.
9077     (let ((max (max (point) (mark)))
9078           articles article)
9079       (save-excursion
9080         (goto-char (min (point) (mark)))
9081         (while
9082             (and
9083              (push (setq article (gnus-summary-article-number)) articles)
9084              (gnus-summary-find-next nil article)
9085              (< (point) max)))
9086         (nreverse articles))))
9087    (gnus-newsgroup-processable
9088     ;; There are process-marked articles present.
9089     (reverse gnus-newsgroup-processable))
9090    (t
9091     ;; Just return the current article.
9092     (list (gnus-summary-article-number)))))
9093
9094 (defun gnus-summary-search-group (&optional backward use-level)
9095   "Search for next unread newsgroup.
9096 If optional argument BACKWARD is non-nil, search backward instead."
9097   (save-excursion
9098     (set-buffer gnus-group-buffer)
9099     (if (gnus-group-search-forward
9100          backward nil (if use-level (gnus-group-group-level) nil))
9101         (gnus-group-group-name))))
9102
9103 (defun gnus-summary-best-group (&optional exclude-group)
9104   "Find the name of the best unread group.
9105 If EXCLUDE-GROUP, do not go to this group."
9106   (save-excursion
9107     (set-buffer gnus-group-buffer)
9108     (save-excursion
9109       (gnus-group-best-unread-group exclude-group))))
9110
9111 (defun gnus-summary-find-next (&optional unread article backward)
9112   (if backward (gnus-summary-find-prev)
9113     (let* ((article (or article (gnus-summary-article-number)))
9114            (arts (gnus-data-find-list article))
9115            result)
9116       (when (or (not gnus-summary-check-current)
9117                 (not unread)
9118                 (not (gnus-data-unread-p (car arts))))
9119         (setq arts (cdr arts)))
9120       (when (setq result
9121                   (if unread
9122                       (progn
9123                         (while arts
9124                           (when (gnus-data-unread-p (car arts))
9125                             (setq result (car arts)
9126                                   arts nil))
9127                           (setq arts (cdr arts)))
9128                         result)
9129                     (car arts)))
9130         (goto-char (gnus-data-pos result))
9131         (gnus-data-number result)))))
9132
9133 (defun gnus-summary-find-prev (&optional unread article)
9134   (let* ((article (or article (gnus-summary-article-number)))
9135          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9136          result)
9137     (when (or (not gnus-summary-check-current)
9138               (not unread)
9139               (not (gnus-data-unread-p (car arts))))
9140       (setq arts (cdr arts)))
9141     (if (setq result
9142               (if unread
9143                   (progn
9144                     (while arts
9145                       (and (gnus-data-unread-p (car arts))
9146                            (setq result (car arts)
9147                                  arts nil))
9148                       (setq arts (cdr arts)))
9149                     result)
9150                 (car arts)))
9151         (progn
9152           (goto-char (gnus-data-pos result))
9153           (gnus-data-number result)))))
9154
9155 (defun gnus-summary-find-subject (subject &optional unread backward article)
9156   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9157          (article (or article (gnus-summary-article-number)))
9158          (articles (gnus-data-list backward))
9159          (arts (gnus-data-find-list article articles))
9160          result)
9161     (when (or (not gnus-summary-check-current)
9162               (not unread)
9163               (not (gnus-data-unread-p (car arts))))
9164       (setq arts (cdr arts)))
9165     (while arts
9166       (and (or (not unread)
9167                (gnus-data-unread-p (car arts)))
9168            (vectorp (gnus-data-header (car arts)))
9169            (gnus-subject-equal
9170             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9171            (setq result (car arts)
9172                  arts nil))
9173       (setq arts (cdr arts)))
9174     (and result
9175          (goto-char (gnus-data-pos result))
9176          (gnus-data-number result))))
9177
9178 (defun gnus-summary-search-forward (&optional unread subject backward)
9179   (cond (subject
9180          (gnus-summary-find-subject subject unread backward))
9181         (backward
9182          (gnus-summary-find-prev unread))
9183         (t
9184          (gnus-summary-find-next unread))))
9185
9186 (defun gnus-recenter (&optional n)
9187   "Center point in window and redisplay frame.
9188 Also do horizontal recentering."
9189   (interactive)
9190   (gnus-horizontal-recenter)
9191   (recenter n))
9192
9193 (defun gnus-summary-recenter ()
9194   "Center point in the summary window.
9195 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9196 displayed, no centering will be performed."
9197   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9198   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9199   (let* ((top (cond ((< (window-height) 4) 0)
9200                     ((< (window-height) 7) 1)
9201                     (t 2)))
9202          (height (1- (window-height)))
9203          (bottom (save-excursion (goto-char (point-max))
9204                                  (forward-line (- height))
9205                                  (point)))
9206          (window (get-buffer-window (current-buffer))))
9207     ;; The user has to want it.
9208     (when gnus-auto-center-summary
9209       (when (get-buffer-window gnus-article-buffer)
9210        ;; Only do recentering when the article buffer is displayed,
9211        ;; Set the window start to either `bottom', which is the biggest
9212        ;; possible valid number, or the second line from the top,
9213        ;; whichever is the least.
9214        (set-window-start
9215         window (min bottom (save-excursion 
9216                              (forward-line (- top)) (point)))))
9217       ;; Do horizontal recentering while we're at it.
9218       (when (get-buffer-window (current-buffer) t)
9219         (let ((selected (selected-window)))
9220           (select-window (get-buffer-window (current-buffer) t))
9221           (gnus-summary-position-point)
9222           (gnus-horizontal-recenter)
9223           (select-window selected))))))
9224
9225 (defun gnus-horizontal-recenter ()
9226   "Recenter the current buffer horizontally."
9227   (if (< (current-column) (/ (window-width) 2))
9228       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9229     (let* ((orig (point))
9230            (end (window-end (get-buffer-window (current-buffer) t)))
9231            (max 0))
9232       ;; Find the longest line currently displayed in the window.
9233       (goto-char (window-start))
9234       (while (and (not (eobp)) 
9235                   (< (point) end))
9236         (end-of-line)
9237         (setq max (max max (current-column)))
9238         (forward-line 1))
9239       (goto-char orig)
9240       ;; Scroll horizontally to center (sort of) the point.
9241       (if (> max (window-width))
9242           (set-window-hscroll 
9243            (get-buffer-window (current-buffer) t)
9244            (min (- (current-column) (/ (window-width) 3))
9245                 (+ 2 (- max (window-width)))))
9246         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9247       max)))
9248
9249 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9250 (defun gnus-short-group-name (group &optional levels)
9251   "Collapse GROUP name LEVELS."
9252   (let* ((name "") 
9253          (foreign "")
9254          (depth 0) 
9255          (skip 1)
9256          (levels (or levels
9257                      (progn
9258                        (while (string-match "\\." group skip)
9259                          (setq skip (match-end 0)
9260                                depth (+ depth 1)))
9261                        depth))))
9262     (if (string-match ":" group)
9263         (setq foreign (substring group 0 (match-end 0))
9264               group (substring group (match-end 0))))
9265     (while group
9266       (if (and (string-match "\\." group)
9267                (> levels (- gnus-group-uncollapsed-levels 1)))
9268           (setq name (concat name (substring group 0 1))
9269                 group (substring group (match-end 0))
9270                 levels (- levels 1)
9271                 name (concat name "."))
9272         (setq name (concat foreign name group)
9273               group nil)))
9274     name))
9275
9276 (defun gnus-summary-jump-to-group (newsgroup)
9277   "Move point to NEWSGROUP in group mode buffer."
9278   ;; Keep update point of group mode buffer if visible.
9279   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9280       (save-window-excursion
9281         ;; Take care of tree window mode.
9282         (if (get-buffer-window gnus-group-buffer)
9283             (pop-to-buffer gnus-group-buffer))
9284         (gnus-group-jump-to-group newsgroup))
9285     (save-excursion
9286       ;; Take care of tree window mode.
9287       (if (get-buffer-window gnus-group-buffer)
9288           (pop-to-buffer gnus-group-buffer)
9289         (set-buffer gnus-group-buffer))
9290       (gnus-group-jump-to-group newsgroup))))
9291
9292 ;; This function returns a list of article numbers based on the
9293 ;; difference between the ranges of read articles in this group and
9294 ;; the range of active articles.
9295 (defun gnus-list-of-unread-articles (group)
9296   (let* ((read (gnus-info-read (gnus-get-info group)))
9297          (active (gnus-active group))
9298          (last (cdr active))
9299          first nlast unread)
9300     ;; If none are read, then all are unread.
9301     (if (not read)
9302         (setq first (car active))
9303       ;; If the range of read articles is a single range, then the
9304       ;; first unread article is the article after the last read
9305       ;; article.  Sounds logical, doesn't it?
9306       (if (not (listp (cdr read)))
9307           (setq first (1+ (cdr read)))
9308         ;; `read' is a list of ranges.
9309         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9310                                 (car (car read)))) 1)
9311             (setq first 1))
9312         (while read
9313           (if first
9314               (while (< first nlast)
9315                 (setq unread (cons first unread))
9316                 (setq first (1+ first))))
9317           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9318           (setq nlast (if (atom (car (cdr read)))
9319                           (car (cdr read))
9320                         (car (car (cdr read)))))
9321           (setq read (cdr read)))))
9322     ;; And add the last unread articles.
9323     (while (<= first last)
9324       (setq unread (cons first unread))
9325       (setq first (1+ first)))
9326     ;; Return the list of unread articles.
9327     (nreverse unread)))
9328
9329 (defun gnus-list-of-read-articles (group)
9330   "Return a list of unread, unticked and non-dormant articles."
9331   (let* ((info (gnus-get-info group))
9332          (marked (gnus-info-marks info))
9333          (active (gnus-active group)))
9334     (and info active
9335          (gnus-set-difference
9336           (gnus-sorted-complement
9337            (gnus-uncompress-range active)
9338            (gnus-list-of-unread-articles group))
9339           (append
9340            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9341            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9342
9343 ;; Various summary commands
9344
9345 (defun gnus-summary-universal-argument (arg)
9346   "Perform any operation on all articles that are process/prefixed."
9347   (interactive "P")
9348   (gnus-set-global-variables)
9349   (let ((articles (gnus-summary-work-articles arg))
9350         func article)
9351     (if (eq
9352          (setq
9353           func
9354           (key-binding
9355            (read-key-sequence
9356             (substitute-command-keys
9357              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9358              ))))
9359          'undefined)
9360         (progn
9361           (message "Undefined key")
9362           (ding))
9363       (save-excursion
9364         (while articles
9365           (gnus-summary-goto-subject (setq article (pop articles)))
9366           (command-execute func)
9367           (gnus-summary-remove-process-mark article)))))
9368   (gnus-summary-position-point))
9369
9370 (defun gnus-summary-toggle-truncation (&optional arg)
9371   "Toggle truncation of summary lines.
9372 With arg, turn line truncation on iff arg is positive."
9373   (interactive "P")
9374   (setq truncate-lines
9375         (if (null arg) (not truncate-lines)
9376           (> (prefix-numeric-value arg) 0)))
9377   (redraw-display))
9378
9379 (defun gnus-summary-reselect-current-group (&optional all rescan)
9380   "Exit and then reselect the current newsgroup.
9381 The prefix argument ALL means to select all articles."
9382   (interactive "P")
9383   (gnus-set-global-variables)
9384   (let ((current-subject (gnus-summary-article-number))
9385         (group gnus-newsgroup-name))
9386     (setq gnus-newsgroup-begin nil)
9387     (gnus-summary-exit)
9388     ;; We have to adjust the point of group mode buffer because the
9389     ;; current point was moved to the next unread newsgroup by
9390     ;; exiting.
9391     (gnus-summary-jump-to-group group)
9392     (when rescan
9393       (save-excursion
9394         (gnus-group-get-new-news-this-group 1)))
9395     (gnus-group-read-group all t)
9396     (gnus-summary-goto-subject current-subject)))
9397
9398 (defun gnus-summary-rescan-group (&optional all)
9399   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9400   (interactive "P")
9401   (gnus-summary-reselect-current-group all t))
9402
9403 (defun gnus-summary-update-info ()
9404   (let* ((group gnus-newsgroup-name))
9405     (when gnus-newsgroup-kill-headers
9406       (setq gnus-newsgroup-killed
9407             (gnus-compress-sequence
9408              (nconc
9409               (gnus-set-sorted-intersection
9410                (gnus-uncompress-range gnus-newsgroup-killed)
9411                (setq gnus-newsgroup-unselected
9412                      (sort gnus-newsgroup-unselected '<)))
9413               (setq gnus-newsgroup-unreads
9414                     (sort gnus-newsgroup-unreads '<))) t)))
9415     (unless (listp (cdr gnus-newsgroup-killed))
9416       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9417     (let ((headers gnus-newsgroup-headers))
9418       (gnus-close-group group)
9419       (run-hooks 'gnus-exit-group-hook)
9420       (unless gnus-save-score
9421         (setq gnus-newsgroup-scored nil))
9422       ;; Set the new ranges of read articles.
9423       (gnus-update-read-articles
9424        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9425       ;; Set the current article marks.
9426       (gnus-update-marks)
9427       ;; Do the cross-ref thing.
9428       (when gnus-use-cross-reference
9429         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9430       ;; Do adaptive scoring, and possibly save score files.
9431       (when gnus-newsgroup-adaptive
9432         (gnus-score-adaptive))
9433       (when gnus-use-scoring
9434         (gnus-score-save))
9435       ;; Do not switch windows but change the buffer to work.
9436       (set-buffer gnus-group-buffer)
9437       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9438           (gnus-group-update-group group)))))
9439
9440 (defun gnus-summary-exit (&optional temporary)
9441   "Exit reading current newsgroup, and then return to group selection mode.
9442 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9443   (interactive)
9444   (gnus-set-global-variables)
9445   (gnus-kill-save-kill-buffer)
9446   (let* ((group gnus-newsgroup-name)
9447          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9448          (mode major-mode)
9449          (buf (current-buffer)))
9450     (run-hooks 'gnus-summary-prepare-exit-hook)
9451     (when gnus-use-cache
9452       (gnus-cache-possibly-remove-articles)
9453       (gnus-cache-save-buffers))
9454     (when gnus-use-trees
9455       (gnus-tree-close group))
9456     ;; Make all changes in this group permanent.
9457     (gnus-summary-update-info)
9458     ;; Make sure where I was, and go to next newsgroup.
9459     (set-buffer gnus-group-buffer)
9460     (unless quit-config
9461       (gnus-group-jump-to-group group)
9462       (gnus-group-next-unread-group 1))
9463     (run-hooks 'gnus-summary-exit-hook)
9464     (if temporary
9465         nil                             ;Nothing to do.
9466       ;; If we have several article buffers, we kill them at exit.
9467       (unless gnus-single-article-buffer
9468         (gnus-kill-buffer gnus-article-buffer)
9469         (gnus-kill-buffer gnus-original-article-buffer))
9470       (set-buffer buf)
9471       (if (not gnus-kill-summary-on-exit)
9472           (gnus-deaden-summary)
9473         ;; We set all buffer-local variables to nil.  It is unclear why
9474         ;; this is needed, but if we don't, buffer-local variables are
9475         ;; not garbage-collected, it seems.  This would the lead to en
9476         ;; ever-growing Emacs.
9477         (gnus-summary-clear-local-variables)
9478         (when (get-buffer gnus-article-buffer)
9479           (bury-buffer gnus-article-buffer))
9480         ;; We clear the global counterparts of the buffer-local
9481         ;; variables as well, just to be on the safe side.
9482         (gnus-configure-windows 'group 'force)
9483         (gnus-summary-clear-local-variables)
9484         ;; Return to group mode buffer.
9485         (if (eq mode 'gnus-summary-mode)
9486             (gnus-kill-buffer buf)))
9487       (setq gnus-current-select-method gnus-select-method)
9488       (pop-to-buffer gnus-group-buffer)
9489       ;; Clear the current group name.
9490       (if (not quit-config)
9491           (progn
9492             (gnus-group-jump-to-group group)
9493             (gnus-group-next-unread-group 1)
9494             (gnus-configure-windows 'group 'force))
9495         (if (not (buffer-name (car quit-config)))
9496             (gnus-configure-windows 'group 'force)
9497           (set-buffer (car quit-config))
9498           (and (eq major-mode 'gnus-summary-mode)
9499                (gnus-set-global-variables))
9500           (gnus-configure-windows (cdr quit-config))))
9501       (unless quit-config
9502         (setq gnus-newsgroup-name nil)))))
9503
9504 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9505 (defun gnus-summary-exit-no-update (&optional no-questions)
9506   "Quit reading current newsgroup without updating read article info."
9507   (interactive)
9508   (gnus-set-global-variables)
9509   (let* ((group gnus-newsgroup-name)
9510          (quit-config (gnus-group-quit-config group)))
9511     (when (or no-questions
9512               gnus-expert-user
9513               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9514       ;; If we have several article buffers, we kill them at exit.
9515       (unless gnus-single-article-buffer
9516         (gnus-kill-buffer gnus-article-buffer)
9517         (gnus-kill-buffer gnus-original-article-buffer))
9518       (if (not gnus-kill-summary-on-exit)
9519           (gnus-deaden-summary)
9520         (gnus-close-group group)
9521         (gnus-summary-clear-local-variables)
9522         (set-buffer gnus-group-buffer)
9523         (gnus-summary-clear-local-variables)
9524         (when (get-buffer gnus-summary-buffer)
9525           (kill-buffer gnus-summary-buffer)))
9526       (when gnus-use-trees
9527         (gnus-tree-close group))
9528       (when (get-buffer gnus-article-buffer)
9529         (bury-buffer gnus-article-buffer))
9530       ;; Return to the group buffer.
9531       (gnus-configure-windows 'group 'force)
9532       ;; Clear the current group name.
9533       (setq gnus-newsgroup-name nil)
9534       (when (equal (gnus-group-group-name) group)
9535         (gnus-group-next-unread-group 1))
9536       (when quit-config
9537         (if (not (buffer-name (car quit-config)))
9538             (gnus-configure-windows 'group 'force)
9539           (set-buffer (car quit-config))
9540           (when (eq major-mode 'gnus-summary-mode)
9541             (gnus-set-global-variables))
9542           (gnus-configure-windows (cdr quit-config)))))))
9543
9544 ;;; Dead summaries.
9545
9546 (defvar gnus-dead-summary-mode-map nil)
9547
9548 (if gnus-dead-summary-mode-map
9549     nil
9550   (setq gnus-dead-summary-mode-map (make-keymap))
9551   (suppress-keymap gnus-dead-summary-mode-map)
9552   (substitute-key-definition
9553    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9554   (let ((keys '("\C-d" "\r" "\177")))
9555     (while keys
9556       (define-key gnus-dead-summary-mode-map
9557         (pop keys) 'gnus-summary-wake-up-the-dead))))
9558
9559 (defvar gnus-dead-summary-mode nil
9560   "Minor mode for Gnus summary buffers.")
9561
9562 (defun gnus-dead-summary-mode (&optional arg)
9563   "Minor mode for Gnus summary buffers."
9564   (interactive "P")
9565   (when (eq major-mode 'gnus-summary-mode)
9566     (make-local-variable 'gnus-dead-summary-mode)
9567     (setq gnus-dead-summary-mode
9568           (if (null arg) (not gnus-dead-summary-mode)
9569             (> (prefix-numeric-value arg) 0)))
9570     (when gnus-dead-summary-mode
9571       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9572         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9573       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9574         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9575               minor-mode-map-alist)))))
9576
9577 (defun gnus-deaden-summary ()
9578   "Make the current summary buffer into a dead summary buffer."
9579   ;; Kill any previous dead summary buffer.
9580   (when (and gnus-dead-summary
9581              (buffer-name gnus-dead-summary))
9582     (save-excursion
9583       (set-buffer gnus-dead-summary)
9584       (when gnus-dead-summary-mode
9585         (kill-buffer (current-buffer)))))
9586   ;; Make this the current dead summary.
9587   (setq gnus-dead-summary (current-buffer))
9588   (gnus-dead-summary-mode 1)
9589   (let ((name (buffer-name)))
9590     (when (string-match "Summary" name)
9591       (rename-buffer
9592        (concat (substring name 0 (match-beginning 0)) "Dead "
9593                (substring name (match-beginning 0))) t))))
9594
9595 (defun gnus-kill-or-deaden-summary (buffer)
9596   "Kill or deaden the summary BUFFER."
9597   (cond (gnus-kill-summary-on-exit
9598          (when (and gnus-use-trees
9599                     (and (get-buffer buffer)
9600                          (buffer-name (get-buffer buffer))))
9601            (save-excursion
9602              (set-buffer (get-buffer buffer))
9603              (gnus-tree-close gnus-newsgroup-name)))
9604          (gnus-kill-buffer buffer))
9605         ((and (get-buffer buffer)
9606               (buffer-name (get-buffer buffer)))
9607          (save-excursion
9608            (set-buffer buffer)
9609            (gnus-deaden-summary)))))
9610
9611 (defun gnus-summary-wake-up-the-dead (&rest args)
9612   "Wake up the dead summary buffer."
9613   (interactive)
9614   (gnus-dead-summary-mode -1)
9615   (let ((name (buffer-name)))
9616     (when (string-match "Dead " name)
9617       (rename-buffer
9618        (concat (substring name 0 (match-beginning 0))
9619                (substring name (match-end 0))) t)))
9620   (gnus-message 3 "This dead summary is now alive again"))
9621
9622 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9623 (defun gnus-summary-fetch-faq (&optional faq-dir)
9624   "Fetch the FAQ for the current group.
9625 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9626 in."
9627   (interactive
9628    (list
9629     (if current-prefix-arg
9630         (completing-read
9631          "Faq dir: " (and (listp gnus-group-faq-directory)
9632                           gnus-group-faq-directory)))))
9633   (let (gnus-faq-buffer)
9634     (and (setq gnus-faq-buffer
9635                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9636          (gnus-configure-windows 'summary-faq))))
9637
9638 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9639 (defun gnus-summary-describe-group (&optional force)
9640   "Describe the current newsgroup."
9641   (interactive "P")
9642   (gnus-group-describe-group force gnus-newsgroup-name))
9643
9644 (defun gnus-summary-describe-briefly ()
9645   "Describe summary mode commands briefly."
9646   (interactive)
9647   (gnus-message 6
9648                 (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")))
9649
9650 ;; Walking around group mode buffer from summary mode.
9651
9652 (defun gnus-summary-next-group (&optional no-article target-group backward)
9653   "Exit current newsgroup and then select next unread newsgroup.
9654 If prefix argument NO-ARTICLE is non-nil, no article is selected
9655 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9656 previous group instead."
9657   (interactive "P")
9658   (gnus-set-global-variables)
9659   (let ((current-group gnus-newsgroup-name)
9660         (current-buffer (current-buffer))
9661         entered)
9662     ;; First we semi-exit this group to update Xrefs and all variables.
9663     ;; We can't do a real exit, because the window conf must remain
9664     ;; the same in case the user is prompted for info, and we don't
9665     ;; want the window conf to change before that...
9666     (gnus-summary-exit t)
9667     (while (not entered)
9668       ;; Then we find what group we are supposed to enter.
9669       (set-buffer gnus-group-buffer)
9670       (gnus-group-jump-to-group current-group)
9671       (setq target-group
9672             (or target-group
9673                 (if (eq gnus-keep-same-level 'best)
9674                     (gnus-summary-best-group gnus-newsgroup-name)
9675                   (gnus-summary-search-group backward gnus-keep-same-level))))
9676       (if (not target-group)
9677           ;; There are no further groups, so we return to the group
9678           ;; buffer.
9679           (progn
9680             (gnus-message 5 "Returning to the group buffer")
9681             (setq entered t)
9682             (set-buffer current-buffer)
9683             (gnus-summary-exit))
9684         ;; We try to enter the target group.
9685         (gnus-group-jump-to-group target-group)
9686         (let ((unreads (gnus-group-group-unread)))
9687           (if (and (or (eq t unreads)
9688                        (and unreads (not (zerop unreads))))
9689                    (gnus-summary-read-group
9690                     target-group nil no-article current-buffer))
9691               (setq entered t)
9692             (setq current-group target-group
9693                   target-group nil)))))))
9694
9695 (defun gnus-summary-prev-group (&optional no-article)
9696   "Exit current newsgroup and then select previous unread newsgroup.
9697 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9698   (interactive "P")
9699   (gnus-summary-next-group no-article nil t))
9700
9701 ;; Walking around summary lines.
9702
9703 (defun gnus-summary-first-subject (&optional unread)
9704   "Go to the first unread subject.
9705 If UNREAD is non-nil, go to the first unread article.
9706 Returns the article selected or nil if there are no unread articles."
9707   (interactive "P")
9708   (prog1
9709       (cond
9710        ;; Empty summary.
9711        ((null gnus-newsgroup-data)
9712         (gnus-message 3 "No articles in the group")
9713         nil)
9714        ;; Pick the first article.
9715        ((not unread)
9716         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9717         (gnus-data-number (car gnus-newsgroup-data)))
9718        ;; No unread articles.
9719        ((null gnus-newsgroup-unreads)
9720         (gnus-message 3 "No more unread articles")
9721         nil)
9722        ;; Find the first unread article.
9723        (t
9724         (let ((data gnus-newsgroup-data))
9725           (while (and data
9726                       (not (gnus-data-unread-p (car data))))
9727             (setq data (cdr data)))
9728           (if data
9729               (progn
9730                 (goto-char (gnus-data-pos (car data)))
9731                 (gnus-data-number (car data)))))))
9732     (gnus-summary-position-point)))
9733
9734 (defun gnus-summary-next-subject (n &optional unread dont-display)
9735   "Go to next N'th summary line.
9736 If N is negative, go to the previous N'th subject line.
9737 If UNREAD is non-nil, only unread articles are selected.
9738 The difference between N and the actual number of steps taken is
9739 returned."
9740   (interactive "p")
9741   (let ((backward (< n 0))
9742         (n (abs n)))
9743     (while (and (> n 0)
9744                 (if backward
9745                     (gnus-summary-find-prev unread)
9746                   (gnus-summary-find-next unread)))
9747       (setq n (1- n)))
9748     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9749                                (if unread " unread" "")))
9750     (or dont-display
9751         (progn
9752           (gnus-summary-recenter)
9753           (gnus-summary-position-point)))
9754     n))
9755
9756 (defun gnus-summary-next-unread-subject (n)
9757   "Go to next N'th unread summary line."
9758   (interactive "p")
9759   (gnus-summary-next-subject n t))
9760
9761 (defun gnus-summary-prev-subject (n &optional unread)
9762   "Go to previous N'th summary line.
9763 If optional argument UNREAD is non-nil, only unread article is selected."
9764   (interactive "p")
9765   (gnus-summary-next-subject (- n) unread))
9766
9767 (defun gnus-summary-prev-unread-subject (n)
9768   "Go to previous N'th unread summary line."
9769   (interactive "p")
9770   (gnus-summary-next-subject (- n) t))
9771
9772 (defun gnus-summary-goto-subject (article &optional force silent)
9773   "Go the subject line of ARTICLE.
9774 If FORCE, also allow jumping to articles not currently shown."
9775   (let ((b (point))
9776         (data (gnus-data-find article)))
9777     ;; We read in the article if we have to.
9778     (and (not data)
9779          force
9780          (gnus-summary-insert-subject article)
9781          (setq data (gnus-data-find article)))
9782     (goto-char b)
9783     (if (not data)
9784         (progn
9785           (unless silent
9786             (gnus-message 3 "Can't find article %d" article))
9787           nil)
9788       (goto-char (gnus-data-pos data))
9789       article)))
9790
9791 ;; Walking around summary lines with displaying articles.
9792
9793 (defun gnus-summary-expand-window (&optional arg)
9794   "Make the summary buffer take up the entire Emacs frame.
9795 Given a prefix, will force an `article' buffer configuration."
9796   (interactive "P")
9797   (gnus-set-global-variables)
9798   (if arg
9799       (gnus-configure-windows 'article 'force)
9800     (gnus-configure-windows 'summary 'force)))
9801
9802 (defun gnus-summary-display-article (article &optional all-header)
9803   "Display ARTICLE in article buffer."
9804   (gnus-set-global-variables)
9805   (if (null article)
9806       nil
9807     (prog1
9808         (if gnus-summary-display-article-function
9809             (funcall gnus-summary-display-article-function article all-header)
9810           (gnus-article-prepare article all-header))
9811       (run-hooks 'gnus-select-article-hook)
9812       (gnus-summary-recenter)
9813       (gnus-summary-goto-subject article)
9814       (when gnus-use-trees
9815         (gnus-possibly-generate-tree article)
9816         (gnus-highlight-selected-tree article))
9817       ;; Successfully display article.
9818       (gnus-article-set-window-start
9819        (cdr (assq article gnus-newsgroup-bookmarks)))
9820       t)))
9821
9822 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9823   "Select the current article.
9824 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9825 non-nil, the article will be re-fetched even if it already present in
9826 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9827 be displayed."
9828   (let ((article (or article (gnus-summary-article-number)))
9829         (all-headers (not (not all-headers))) ;Must be T or NIL.
9830         gnus-summary-display-article-function
9831         did)
9832     (and (not pseudo)
9833          (gnus-summary-article-pseudo-p article)
9834          (error "This is a pseudo-article."))
9835     (prog1
9836         (save-excursion
9837           (set-buffer gnus-summary-buffer)
9838           (if (or (and gnus-single-article-buffer
9839                        (or (null gnus-current-article)
9840                            (null gnus-article-current)
9841                            (null (get-buffer gnus-article-buffer))
9842                            (not (eq article (cdr gnus-article-current)))
9843                            (not (equal (car gnus-article-current)
9844                                        gnus-newsgroup-name))))
9845                   (and (not gnus-single-article-buffer)
9846                        (null gnus-current-article))
9847                   force)
9848               ;; The requested article is different from the current article.
9849               (prog1
9850                   (gnus-summary-display-article article all-headers)
9851                 (setq did article))
9852             (if (or all-headers gnus-show-all-headers)
9853                 (gnus-article-show-all-headers))
9854             'old))
9855       (if did
9856           (gnus-article-set-window-start
9857            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9858
9859 (defun gnus-summary-set-current-mark (&optional current-mark)
9860   "Obsolete function."
9861   nil)
9862
9863 (defun gnus-summary-next-article (&optional unread subject backward push)
9864   "Select the next article.
9865 If UNREAD, only unread articles are selected.
9866 If SUBJECT, only articles with SUBJECT are selected.
9867 If BACKWARD, the previous article is selected instead of the next."
9868   (interactive "P")
9869   (gnus-set-global-variables)
9870   (let (header)
9871     (cond
9872      ;; Is there such an article?
9873      ((and (gnus-summary-search-forward unread subject backward)
9874            (or (gnus-summary-display-article (gnus-summary-article-number))
9875                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9876       (gnus-summary-position-point))
9877      ;; If not, we try the first unread, if that is wanted.
9878      ((and subject
9879            gnus-auto-select-same
9880            (or (gnus-summary-first-unread-article)
9881                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9882       (gnus-summary-position-point)
9883       (gnus-message 6 "Wrapped"))
9884      ;; Try to get next/previous article not displayed in this group.
9885      ((and gnus-auto-extend-newsgroup
9886            (not unread) (not subject))
9887       (gnus-summary-goto-article
9888        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
9889        nil t))
9890      ;; Go to next/previous group.
9891      (t
9892       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9893           (gnus-summary-jump-to-group gnus-newsgroup-name))
9894       (let ((cmd last-command-char)
9895             (group
9896              (if (eq gnus-keep-same-level 'best)
9897                  (gnus-summary-best-group gnus-newsgroup-name)
9898                (gnus-summary-search-group backward gnus-keep-same-level))))
9899         ;; For some reason, the group window gets selected.  We change
9900         ;; it back.
9901         (select-window (get-buffer-window (current-buffer)))
9902         ;; Select next unread newsgroup automagically.
9903         (cond
9904          ((not gnus-auto-select-next)
9905           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
9906          ((or (eq gnus-auto-select-next 'quietly)
9907               (and (eq gnus-auto-select-next 'slightly-quietly)
9908                    push)
9909               (and (eq gnus-auto-select-next 'almost-quietly)
9910                    (gnus-summary-last-article-p)))
9911           ;; Select quietly.
9912           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
9913               (gnus-summary-exit)
9914             (gnus-message 7 "No more%s articles (%s)..."
9915                           (if unread " unread" "")
9916                           (if group (concat "selecting " group)
9917                             "exiting"))
9918             (gnus-summary-next-group nil group backward)))
9919          (t
9920           (gnus-summary-walk-group-buffer
9921            gnus-newsgroup-name cmd unread backward))))))))
9922
9923 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
9924   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
9925                       (?\C-p (gnus-group-prev-unread-group 1))))
9926         keve key group ended)
9927     (save-excursion
9928       (set-buffer gnus-group-buffer)
9929       (gnus-summary-jump-to-group from-group)
9930       (setq group
9931             (if (eq gnus-keep-same-level 'best)
9932                 (gnus-summary-best-group gnus-newsgroup-name)
9933               (gnus-summary-search-group backward gnus-keep-same-level))))
9934     (while (not ended)
9935       (gnus-message
9936        5 "No more%s articles%s" (if unread " unread" "")
9937        (if (and group
9938                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
9939            (format " (Type %s for %s [%s])"
9940                    (single-key-description cmd) group
9941                    (car (gnus-gethash group gnus-newsrc-hashtb)))
9942          (format " (Type %s to exit %s)"
9943                  (single-key-description cmd)
9944                  gnus-newsgroup-name)))
9945       ;; Confirm auto selection.
9946       (setq key (car (setq keve (gnus-read-event-char))))
9947       (setq ended t)
9948       (cond
9949        ((assq key keystrokes)
9950         (let ((obuf (current-buffer)))
9951           (switch-to-buffer gnus-group-buffer)
9952           (and group
9953                (gnus-group-jump-to-group group))
9954           (eval (car (cdr (assq key keystrokes))))
9955           (setq group (gnus-group-group-name))
9956           (switch-to-buffer obuf))
9957         (setq ended nil))
9958        ((equal key cmd)
9959         (if (or (not group)
9960                 (gnus-ephemeral-group-p gnus-newsgroup-name))
9961             (gnus-summary-exit)
9962           (gnus-summary-next-group nil group backward)))
9963        (t
9964         (push (cdr keve) unread-command-events))))))
9965
9966 (defun gnus-read-event-char ()
9967   "Get the next event."
9968   (let ((event (read-event)))
9969     (cons (and (numberp event) event) event)))
9970
9971 (defun gnus-summary-next-unread-article ()
9972   "Select unread article after current one."
9973   (interactive)
9974   (gnus-summary-next-article t (and gnus-auto-select-same
9975                                     (gnus-summary-article-subject))))
9976
9977 (defun gnus-summary-prev-article (&optional unread subject)
9978   "Select the article after the current one.
9979 If UNREAD is non-nil, only unread articles are selected."
9980   (interactive "P")
9981   (gnus-summary-next-article unread subject t))
9982
9983 (defun gnus-summary-prev-unread-article ()
9984   "Select unred article before current one."
9985   (interactive)
9986   (gnus-summary-prev-article t (and gnus-auto-select-same
9987                                     (gnus-summary-article-subject))))
9988
9989 (defun gnus-summary-next-page (&optional lines circular)
9990   "Show next page of the selected article.
9991 If at the end of the current article, select the next article.
9992 LINES says how many lines should be scrolled up.
9993
9994 If CIRCULAR is non-nil, go to the start of the article instead of
9995 selecting the next article when reaching the end of the current
9996 article."
9997   (interactive "P")
9998   (setq gnus-summary-buffer (current-buffer))
9999   (gnus-set-global-variables)
10000   (let ((article (gnus-summary-article-number))
10001         (endp nil))
10002     (gnus-configure-windows 'article)
10003     (if (or (null gnus-current-article)
10004             (null gnus-article-current)
10005             (/= article (cdr gnus-article-current))
10006             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10007         ;; Selected subject is different from current article's.
10008         (gnus-summary-display-article article)
10009       (gnus-eval-in-buffer-window
10010        gnus-article-buffer
10011        (setq endp (gnus-article-next-page lines)))
10012       (if endp
10013           (cond (circular
10014                  (gnus-summary-beginning-of-article))
10015                 (lines
10016                  (gnus-message 3 "End of message"))
10017                 ((null lines)
10018                  (if (eq gnus-summary-goto-unread 'always)
10019                      (gnus-summary-next-article)
10020                    (gnus-summary-next-unread-article))))))
10021     (gnus-summary-recenter)
10022     (gnus-summary-position-point)))
10023
10024 (defun gnus-summary-prev-page (&optional lines)
10025   "Show previous page of selected article.
10026 Argument LINES specifies lines to be scrolled down."
10027   (interactive "P")
10028   (gnus-set-global-variables)
10029   (let ((article (gnus-summary-article-number)))
10030     (gnus-configure-windows 'article)
10031     (if (or (null gnus-current-article)
10032             (null gnus-article-current)
10033             (/= article (cdr gnus-article-current))
10034             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10035         ;; Selected subject is different from current article's.
10036         (gnus-summary-display-article article)
10037       (gnus-summary-recenter)
10038       (gnus-eval-in-buffer-window gnus-article-buffer
10039                                   (gnus-article-prev-page lines))))
10040   (gnus-summary-position-point))
10041
10042 (defun gnus-summary-scroll-up (lines)
10043   "Scroll up (or down) one line current article.
10044 Argument LINES specifies lines to be scrolled up (or down if negative)."
10045   (interactive "p")
10046   (gnus-set-global-variables)
10047   (gnus-configure-windows 'article)
10048   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10049     (gnus-eval-in-buffer-window
10050      gnus-article-buffer
10051      (cond ((> lines 0)
10052             (if (gnus-article-next-page lines)
10053                 (gnus-message 3 "End of message")))
10054            ((< lines 0)
10055             (gnus-article-prev-page (- lines))))))
10056   (gnus-summary-recenter)
10057   (gnus-summary-position-point))
10058
10059 (defun gnus-summary-next-same-subject ()
10060   "Select next article which has the same subject as current one."
10061   (interactive)
10062   (gnus-set-global-variables)
10063   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10064
10065 (defun gnus-summary-prev-same-subject ()
10066   "Select previous article which has the same subject as current one."
10067   (interactive)
10068   (gnus-set-global-variables)
10069   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10070
10071 (defun gnus-summary-next-unread-same-subject ()
10072   "Select next unread article which has the same subject as current one."
10073   (interactive)
10074   (gnus-set-global-variables)
10075   (gnus-summary-next-article t (gnus-summary-article-subject)))
10076
10077 (defun gnus-summary-prev-unread-same-subject ()
10078   "Select previous unread article which has the same subject as current one."
10079   (interactive)
10080   (gnus-set-global-variables)
10081   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10082
10083 (defun gnus-summary-first-unread-article ()
10084   "Select the first unread article.
10085 Return nil if there are no unread articles."
10086   (interactive)
10087   (gnus-set-global-variables)
10088   (prog1
10089       (if (gnus-summary-first-subject t)
10090           (progn
10091             (gnus-summary-show-thread)
10092             (gnus-summary-first-subject t)
10093             (gnus-summary-display-article (gnus-summary-article-number))))
10094     (gnus-summary-position-point)))
10095
10096 (defun gnus-summary-best-unread-article ()
10097   "Select the unread article with the highest score."
10098   (interactive)
10099   (gnus-set-global-variables)
10100   (let ((best -1000000)
10101         (data gnus-newsgroup-data)
10102         article score)
10103     (while data
10104       (and (gnus-data-unread-p (car data))
10105            (> (setq score
10106                     (gnus-summary-article-score (gnus-data-number (car data))))
10107               best)
10108            (setq best score
10109                  article (gnus-data-number (car data))))
10110       (setq data (cdr data)))
10111     (if article
10112         (gnus-summary-goto-article article)
10113       (error "No unread articles"))
10114     (gnus-summary-position-point)))
10115
10116 (defun gnus-summary-last-subject ()
10117   "Go to the last displayed subject line in the group."
10118   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10119     (when article
10120       (gnus-summary-goto-subject article))))
10121
10122 (defun gnus-summary-goto-article (article &optional all-headers force)
10123   "Fetch ARTICLE and display it if it exists.
10124 If ALL-HEADERS is non-nil, no header lines are hidden."
10125   (interactive
10126    (list
10127     (string-to-int
10128      (completing-read
10129       "Article number: "
10130       (mapcar (lambda (number) (list (int-to-string number)))
10131               gnus-newsgroup-limit)))
10132     current-prefix-arg
10133     t))
10134   (prog1
10135       (if (gnus-summary-goto-subject article force)
10136           (gnus-summary-display-article article all-headers)
10137         (gnus-message 4 "Couldn't go to article %s" article) nil)
10138     (gnus-summary-position-point)))
10139
10140 (defun gnus-summary-goto-last-article ()
10141   "Go to the previously read article."
10142   (interactive)
10143   (prog1
10144       (and gnus-last-article
10145            (gnus-summary-goto-article gnus-last-article))
10146     (gnus-summary-position-point)))
10147
10148 (defun gnus-summary-pop-article (number)
10149   "Pop one article off the history and go to the previous.
10150 NUMBER articles will be popped off."
10151   (interactive "p")
10152   (let (to)
10153     (setq gnus-newsgroup-history
10154           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10155     (if to
10156         (gnus-summary-goto-article (car to))
10157       (error "Article history empty")))
10158   (gnus-summary-position-point))
10159
10160 ;; Summary commands and functions for limiting the summary buffer.
10161
10162 (defun gnus-summary-limit-to-articles (n)
10163   "Limit the summary buffer to the next N articles.
10164 If not given a prefix, use the process marked articles instead."
10165   (interactive "P")
10166   (gnus-set-global-variables)
10167   (prog1
10168       (let ((articles (gnus-summary-work-articles n)))
10169         (setq gnus-newsgroup-processable nil)
10170         (gnus-summary-limit articles))
10171     (gnus-summary-position-point)))
10172
10173 (defun gnus-summary-pop-limit (&optional total)
10174   "Restore the previous limit.
10175 If given a prefix, remove all limits."
10176   (interactive "P")
10177   (gnus-set-global-variables)
10178   (when total 
10179     (setq gnus-newsgroup-limits
10180           (list (mapcar (lambda (h) (mail-header-number h))
10181                         gnus-newsgroup-headers))))
10182   (unless gnus-newsgroup-limits
10183     (error "No limit to pop"))
10184   (prog1
10185       (gnus-summary-limit nil 'pop)
10186     (gnus-summary-position-point)))
10187
10188 (defun gnus-summary-limit-to-subject (subject &optional header)
10189   "Limit the summary buffer to articles that have subjects that match a regexp."
10190   (interactive "sRegexp: ")
10191   (unless header
10192     (setq header "subject"))
10193   (when (not (equal "" subject))
10194     (prog1
10195         (let ((articles (gnus-summary-find-matching "subject" subject 'all)))
10196           (or articles (error "Found no matches for \"%s\"" subject))
10197           (gnus-summary-limit articles))
10198       (gnus-summary-position-point))))
10199
10200 (defun gnus-summary-limit-to-author (from)
10201   "Limit the summary buffer to articles that have authors that match a regexp."
10202   (interactive "sRegexp: ")
10203   (gnus-summary-limit-to-subject from "from"))
10204
10205 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10206 (make-obsolete
10207  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10208
10209 (defun gnus-summary-limit-to-unread (&optional all)
10210   "Limit the summary buffer to articles that are not marked as read.
10211 If ALL is non-nil, limit strictly to unread articles."
10212   (interactive "P")
10213   (if all
10214       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10215     (gnus-summary-limit-to-marks
10216      ;; Concat all the marks that say that an article is read and have
10217      ;; those removed.
10218      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10219            gnus-killed-mark gnus-kill-file-mark
10220            gnus-low-score-mark gnus-expirable-mark
10221            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10222      'reverse)))
10223
10224 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10225 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10226
10227 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10228   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10229 If REVERSE, limit the summary buffer to articles that are not marked
10230 with MARKS.  MARKS can either be a string of marks or a list of marks.
10231 Returns how many articles were removed."
10232   (interactive "sMarks: ")
10233   (gnus-set-global-variables)
10234   (prog1
10235       (let ((data gnus-newsgroup-data)
10236             (marks (if (listp marks) marks
10237                      (append marks nil))) ; Transform to list.
10238             articles)
10239         (while data
10240           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10241                  (memq (gnus-data-mark (car data)) marks))
10242                (setq articles (cons (gnus-data-number (car data)) articles)))
10243           (setq data (cdr data)))
10244         (gnus-summary-limit articles))
10245     (gnus-summary-position-point)))
10246
10247 (defun gnus-summary-limit-to-score (&optional score)
10248   "Limit to articles with score at or above SCORE."
10249   (interactive "P")
10250   (gnus-set-global-variables)
10251   (setq score (if score
10252                   (prefix-numeric-value score)
10253                 (or gnus-summary-default-score 0)))
10254   (let ((data gnus-newsgroup-data)
10255         articles)
10256     (while data
10257       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10258                 score)
10259         (push (gnus-data-number (car data)) articles))
10260       (setq data (cdr data)))
10261     (prog1
10262         (gnus-summary-limit articles)
10263       (gnus-summary-position-point))))
10264
10265 (defun gnus-summary-limit-include-dormant ()
10266   "Display all the hidden articles that are marked as dormant."
10267   (interactive)
10268   (gnus-set-global-variables)
10269   (or gnus-newsgroup-dormant
10270       (error "There are no dormant articles in this group"))
10271   (prog1
10272       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10273     (gnus-summary-position-point)))
10274
10275 (defun gnus-summary-limit-exclude-dormant ()
10276   "Hide all dormant articles."
10277   (interactive)
10278   (gnus-set-global-variables)
10279   (prog1
10280       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10281     (gnus-summary-position-point)))
10282
10283 (defun gnus-summary-limit-exclude-childless-dormant ()
10284   "Hide all dormant articles that have no children."
10285   (interactive)
10286   (gnus-set-global-variables)
10287   (let ((data gnus-newsgroup-data)
10288         articles)
10289     ;; Find all articles that are either not dormant or have
10290     ;; children.
10291     (while data
10292       (and (or (not (= (gnus-data-mark (car data)) gnus-dormant-mark))
10293                (gnus-article-parent-p (gnus-data-number (car data))))
10294            (setq articles (cons (gnus-data-number (car data))
10295                                 articles)))
10296       (setq data (cdr data)))
10297     ;; Do the limiting.
10298     (prog1
10299         (gnus-summary-limit articles)
10300       (gnus-summary-position-point))))
10301
10302 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10303   "Mark all unread excluded articles as read.
10304 If ALL, mark even excluded ticked and dormants as read."
10305   (interactive "P")
10306   (let ((articles (gnus-sorted-complement
10307                    (sort
10308                     (mapcar (lambda (h) (mail-header-number h))
10309                             gnus-newsgroup-headers)
10310                     '<)
10311                    (sort gnus-newsgroup-limit '<)))
10312         article)
10313     (setq gnus-newsgroup-unreads nil)
10314     (if all
10315         (setq gnus-newsgroup-dormant nil
10316               gnus-newsgroup-marked nil
10317               gnus-newsgroup-reads
10318               (nconc
10319                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10320                gnus-newsgroup-reads))
10321       (while (setq article (pop articles))
10322         (unless (or (memq article gnus-newsgroup-dormant)
10323                     (memq article gnus-newsgroup-marked))
10324           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10325
10326
10327 (defun gnus-summary-limit (articles &optional pop)
10328   (if pop
10329       ;; We pop the previous limit off the stack and use that.
10330       (setq articles (car gnus-newsgroup-limits)
10331             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10332     ;; We use the new limit, so we push the old limit on the stack.
10333     (setq gnus-newsgroup-limits
10334           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10335   ;; Set the limit.
10336   (setq gnus-newsgroup-limit articles)
10337   (let ((total (length gnus-newsgroup-data))
10338         (data (gnus-data-find-list (gnus-summary-article-number)))
10339         found)
10340     ;; This will do all the work of generating the new summary buffer
10341     ;; according to the new limit.
10342     (gnus-summary-prepare)
10343     ;; Hide any threads, possibly.
10344     (and gnus-show-threads
10345          gnus-thread-hide-subtree
10346          (gnus-summary-hide-all-threads))
10347     ;; Try to return to the article you were at, or one in the
10348     ;; neighborhood.
10349     (if data
10350         ;; We try to find some article after the current one.
10351         (while data
10352           (and (gnus-summary-goto-subject
10353                 (gnus-data-number (car data)) nil t)
10354                (setq data nil
10355                      found t))
10356           (setq data (cdr data))))
10357     (or found
10358         ;; If there is no data, that means that we were after the last
10359         ;; article.  The same goes when we can't find any articles
10360         ;; after the current one.
10361         (progn
10362           (goto-char (point-max))
10363           (gnus-summary-find-prev)))
10364     ;; We return how many articles were removed from the summary
10365     ;; buffer as a result of the new limit.
10366     (- total (length gnus-newsgroup-data))))
10367
10368 (defsubst gnus-cut-thread (thread)
10369   "Go forwards in the thread until we find an article that we want to display."
10370   (if (eq gnus-fetch-old-headers 'some)
10371       (while (and thread
10372                   (memq (mail-header-number (car thread)) 
10373                         gnus-newsgroup-ancient)
10374                   (<= (length (cdr thread)) 1))
10375         (setq thread (cadr thread)))
10376     (while (and thread
10377                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10378                 (= (length (cdr thread)) 1))
10379       (setq thread (cadr thread))))
10380   thread)
10381
10382 (defun gnus-cut-threads (threads)
10383   "Cut off all uninteresting articles from the beginning of threads."
10384   (when (or (eq gnus-fetch-old-headers 'some)
10385             (eq gnus-build-sparse-threads 'some)
10386             (eq gnus-build-sparse-threads 'more))
10387     (let ((th threads))
10388       (while th
10389         (setcar th (gnus-cut-thread (car th)))
10390         (setq th (cdr th)))))
10391   threads)
10392
10393 (defun gnus-summary-initial-limit (&optional show-if-empty)
10394   "Figure out what the initial limit is supposed to be on group entry.
10395 This entails weeding out unwanted dormants, low-scored articles,
10396 fetch-old-headers verbiage, and so on."
10397   ;; Most groups have nothing to remove.
10398   (if (or gnus-inhibit-limiting
10399           (and (null gnus-newsgroup-dormant)
10400                (not (eq gnus-fetch-old-headers 'some))
10401                (null gnus-summary-expunge-below)
10402                (not (eq gnus-build-sparse-threads 'some))
10403                (not (eq gnus-build-sparse-threads 'more))
10404                (null gnus-thread-expunge-below)))
10405       () ; Do nothing.
10406     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10407     (setq gnus-newsgroup-limit nil)
10408     (mapatoms
10409      (lambda (node)
10410        (unless (car (symbol-value node))
10411          ;; These threads have no parents -- they are roots.
10412          (let ((nodes (cdr (symbol-value node)))
10413                thread)
10414            (while nodes
10415              (if (and gnus-thread-expunge-below
10416                       (< (gnus-thread-total-score (car nodes))
10417                          gnus-thread-expunge-below))
10418                  (gnus-expunge-thread (pop nodes))
10419                (setq thread (pop nodes))
10420                (gnus-summary-limit-children thread))))))
10421      gnus-newsgroup-dependencies)
10422     ;; If this limitation resulted in an empty group, we might
10423     ;; pop the previous limit and use it instead.
10424     (when (and (not gnus-newsgroup-limit)
10425                show-if-empty)
10426       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10427     gnus-newsgroup-limit))
10428
10429 (defun gnus-summary-limit-children (thread)
10430   "Return 1 if this subthread is visible and 0 if it is not."
10431   ;; First we get the number of visible children to this thread.  This
10432   ;; is done by recursing down the thread using this function, so this
10433   ;; will really go down to a leaf article first, before slowly
10434   ;; working its way up towards the root.
10435   (when thread
10436     (let ((children
10437            (if (cdr thread)
10438                (apply '+ (mapcar 'gnus-summary-limit-children
10439                                  (cdr thread)))
10440              0))
10441           (number (mail-header-number (car thread)))
10442           score)
10443       (if (or
10444            ;; If this article is dormant and has absolutely no visible
10445            ;; children, then this article isn't visible.
10446            (and (memq number gnus-newsgroup-dormant)
10447                 (= children 0))
10448            ;; If this is a "fetch-old-headered" and there is only one
10449            ;; visible child (or less), then we don't want this article.
10450            (and (eq gnus-fetch-old-headers 'some)
10451                 (memq number gnus-newsgroup-ancient)
10452                 (zerop children))
10453            ;; If this is a sparsely inserted article with no children,
10454            ;; we don't want it.
10455            (and (eq gnus-build-sparse-threads 'some)
10456                 (memq number gnus-newsgroup-sparse)
10457                 (zerop children))
10458            ;; If we use expunging, and this article is really
10459            ;; low-scored, then we don't want this article.
10460            (when (and gnus-summary-expunge-below
10461                       (< (setq score
10462                                (or (cdr (assq number gnus-newsgroup-scored))
10463                                    gnus-summary-default-score))
10464                          gnus-summary-expunge-below))
10465              ;; We increase the expunge-tally here, but that has
10466              ;; nothing to do with the limits, really.
10467              (incf gnus-newsgroup-expunged-tally)
10468              ;; We also mark as read here, if that's wanted.
10469              (when (and gnus-summary-mark-below
10470                         (< score gnus-summary-mark-below))
10471                (setq gnus-newsgroup-unreads
10472                      (delq number gnus-newsgroup-unreads))
10473                (if gnus-newsgroup-auto-expire
10474                    (push number gnus-newsgroup-expirable)
10475                  (push (cons number gnus-low-score-mark)
10476                        gnus-newsgroup-reads)))
10477              t))
10478           ;; Nope, invisible article.
10479           0
10480         ;; Ok, this article is to be visible, so we add it to the limit
10481         ;; and return 1.
10482         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10483         1))))
10484
10485 (defun gnus-expunge-thread (thread)
10486   "Mark all articles in THREAD as read."
10487   (let* ((number (mail-header-number (car thread))))
10488     (incf gnus-newsgroup-expunged-tally)
10489     ;; We also mark as read here, if that's wanted.
10490     (setq gnus-newsgroup-unreads
10491           (delq number gnus-newsgroup-unreads))
10492     (if gnus-newsgroup-auto-expire
10493         (push number gnus-newsgroup-expirable)
10494       (push (cons number gnus-low-score-mark)
10495             gnus-newsgroup-reads)))
10496   ;; Go recursively through all subthreads.
10497   (mapcar 'gnus-expunge-thread (cdr thread)))
10498
10499 ;; Summary article oriented commands
10500
10501 (defun gnus-summary-refer-parent-article (n)
10502   "Refer parent article N times.
10503 The difference between N and the number of articles fetched is returned."
10504   (interactive "p")
10505   (gnus-set-global-variables)
10506   (while
10507       (and
10508        (> n 0)
10509        (let* ((header (gnus-summary-article-header))
10510               (ref
10511                ;; If we try to find the parent of the currently
10512                ;; displayed article, then we take a look at the actual
10513                ;; References header, since this is slightly more
10514                ;; reliable than the References field we got from the
10515                ;; server.
10516                (if (and (eq (mail-header-number header)
10517                             (cdr gnus-article-current))
10518                         (equal gnus-newsgroup-name
10519                                (car gnus-article-current)))
10520                    (save-excursion
10521                      (set-buffer gnus-original-article-buffer)
10522                      (nnheader-narrow-to-headers)
10523                      (prog1
10524                          (mail-fetch-field "references")
10525                        (widen)))
10526                  ;; It's not the current article, so we take a bet on
10527                  ;; the value we got from the server.
10528                  (mail-header-references header))))
10529          (if ref
10530              (or (gnus-summary-refer-article (gnus-parent-id ref))
10531                  (gnus-message 1 "Couldn't find parent"))
10532            (gnus-message 1 "No references in article %d"
10533                          (gnus-summary-article-number))
10534            nil)))
10535     (setq n (1- n)))
10536   (gnus-summary-position-point)
10537   n)
10538
10539 (defun gnus-summary-refer-references ()
10540   "Fetch all articles mentioned in the References header.
10541 Return how many articles were fetched."
10542   (interactive)
10543   (gnus-set-global-variables)
10544   (let ((ref (mail-header-references (gnus-summary-article-header)))
10545         (current (gnus-summary-article-number))
10546         (n 0))
10547     ;; For each Message-ID in the References header...
10548     (while (string-match "<[^>]*>" ref)
10549       (incf n)
10550       ;; ... fetch that article.
10551       (gnus-summary-refer-article
10552        (prog1 (match-string 0 ref)
10553          (setq ref (substring ref (match-end 0))))))
10554     (gnus-summary-goto-subject current)
10555     (gnus-summary-position-point)
10556     n))
10557
10558 (defun gnus-summary-refer-article (message-id)
10559   "Fetch an article specified by MESSAGE-ID."
10560   (interactive "sMessage-ID: ")
10561   (when (and (stringp message-id)
10562              (not (zerop (length message-id))))
10563     ;; Construct the correct Message-ID if necessary.
10564     ;; Suggested by tale@pawl.rpi.edu.
10565     (unless (string-match "^<" message-id)
10566       (setq message-id (concat "<" message-id)))
10567     (unless (string-match ">$" message-id)
10568       (setq message-id (concat message-id ">")))
10569     (let ((header (car (gnus-gethash (downcase message-id)
10570                                      gnus-newsgroup-dependencies))))
10571       (if header
10572           ;; The article is present in the buffer, to we just go to it.
10573           (gnus-summary-goto-article (mail-header-number header) nil t)
10574         ;; We fetch the article
10575         (let ((gnus-override-method gnus-refer-article-method)
10576               number)
10577           ;; Start the special refer-article method, if necessary.
10578           (when gnus-refer-article-method
10579             (gnus-check-server gnus-refer-article-method))
10580           ;; Fetch the header, and display the article.
10581           (if (setq number (gnus-summary-insert-subject message-id))
10582               (gnus-summary-select-article nil nil nil number)
10583             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10584
10585 (defun gnus-summary-enter-digest-group (&optional force)
10586   "Enter a digest group based on the current article."
10587   (interactive "P")
10588   (gnus-set-global-variables)
10589   (gnus-summary-select-article)
10590   (let ((name (format "%s-%d"
10591                       (gnus-group-prefixed-name
10592                        gnus-newsgroup-name (list 'nndoc ""))
10593                       gnus-current-article))
10594         (ogroup gnus-newsgroup-name)
10595         (case-fold-search t)
10596         (buf (current-buffer))
10597         dig)
10598     (save-excursion
10599       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10600       (insert-buffer-substring gnus-original-article-buffer)
10601       (narrow-to-region
10602        (goto-char (point-min))
10603        (or (search-forward "\n\n" nil t) (point)))
10604       (goto-char (point-min))
10605       (delete-matching-lines "^\\(Path\\):")
10606       (widen))
10607     (unwind-protect
10608         (if (gnus-group-read-ephemeral-group
10609              name `(nndoc ,name (nndoc-address
10610                                  ,(get-buffer dig))
10611                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10612             ;; Make all postings to this group go to the parent group.
10613             (setcdr (nthcdr 4 (gnus-get-info name))
10614                     (list (list (cons 'to-group ogroup))))
10615           ;; Couldn't select this doc group.
10616           (switch-to-buffer buf)
10617           (gnus-set-global-variables)
10618           (gnus-configure-windows 'summary)
10619           (gnus-message 3 "Article couldn't be entered?"))
10620       (kill-buffer dig))))
10621
10622 (defun gnus-summary-isearch-article (&optional regexp-p)
10623   "Do incremental search forward on the current article.
10624 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10625   (interactive "P")
10626   (gnus-set-global-variables)
10627   (gnus-summary-select-article)
10628   (gnus-configure-windows 'article)
10629   (gnus-eval-in-buffer-window
10630    gnus-article-buffer
10631    (goto-char (point-min))
10632    (isearch-forward regexp-p)))
10633
10634 (defun gnus-summary-search-article-forward (regexp &optional backward)
10635   "Search for an article containing REGEXP forward.
10636 If BACKWARD, search backward instead."
10637   (interactive
10638    (list (read-string
10639           (format "Search article %s (regexp%s): "
10640                   (if current-prefix-arg "backward" "forward")
10641                   (if gnus-last-search-regexp
10642                       (concat ", default " gnus-last-search-regexp)
10643                     "")))
10644          current-prefix-arg))
10645   (gnus-set-global-variables)
10646   (if (string-equal regexp "")
10647       (setq regexp (or gnus-last-search-regexp ""))
10648     (setq gnus-last-search-regexp regexp))
10649   (if (gnus-summary-search-article regexp backward)
10650       (gnus-article-set-window-start
10651        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10652     (error "Search failed: \"%s\"" regexp)))
10653
10654 (defun gnus-summary-search-article-backward (regexp)
10655   "Search for an article containing REGEXP backward."
10656   (interactive
10657    (list (read-string
10658           (format "Search article backward (regexp%s): "
10659                   (if gnus-last-search-regexp
10660                       (concat ", default " gnus-last-search-regexp)
10661                     "")))))
10662   (gnus-summary-search-article-forward regexp 'backward))
10663
10664 (defun gnus-summary-search-article (regexp &optional backward)
10665   "Search for an article containing REGEXP.
10666 Optional argument BACKWARD means do search for backward.
10667 gnus-select-article-hook is not called during the search."
10668   (let ((gnus-select-article-hook nil)  ;Disable hook.
10669         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10670         (re-search
10671          (if backward
10672              (function re-search-backward) (function re-search-forward)))
10673         (found nil)
10674         (last nil))
10675     ;; Hidden thread subtrees must be searched for ,too.
10676     (gnus-summary-show-all-threads)
10677     ;; First of all, search current article.
10678     ;; We don't want to read article again from NNTP server nor reset
10679     ;; current point.
10680     (gnus-summary-select-article)
10681     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10682     (setq last gnus-current-article)
10683     (gnus-eval-in-buffer-window
10684      gnus-article-buffer
10685      (save-restriction
10686        (widen)
10687        ;; Begin search from current point.
10688        (setq found (funcall re-search regexp nil t))))
10689     ;; Then search next articles.
10690     (while (and (not found)
10691                 (gnus-summary-display-article
10692                  (if backward (gnus-summary-find-prev)
10693                    (gnus-summary-find-next))))
10694       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10695       (gnus-eval-in-buffer-window
10696        gnus-article-buffer
10697        (save-restriction
10698          (widen)
10699          (goto-char (if backward (point-max) (point-min)))
10700          (setq found (funcall re-search regexp nil t)))))
10701     (message "")
10702     ;; Adjust article pointer.
10703     (or (eq last gnus-current-article)
10704         (setq gnus-last-article last))
10705     ;; Return T if found such article.
10706     found))
10707
10708 (defun gnus-summary-find-matching (header regexp &optional backward unread
10709                                           not-case-fold)
10710   "Return a list of all articles that match REGEXP on HEADER.
10711 The search stars on the current article and goes forwards unless
10712 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10713 If UNREAD is non-nil, only unread articles will
10714 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10715 in the comparisons."
10716   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10717                 (gnus-data-find-list
10718                  (gnus-summary-article-number) (gnus-data-list backward))))
10719         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10720         (case-fold-search (not not-case-fold))
10721         articles d)
10722     (or (fboundp (intern (concat "mail-header-" header)))
10723         (error "%s is not a valid header" header))
10724     (while data
10725       (setq d (car data))
10726       (and (or (not unread)             ; We want all articles...
10727                (gnus-data-unread-p d))  ; Or just unreads.
10728            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10729            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10730            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10731       (setq data (cdr data)))
10732     (nreverse articles)))
10733
10734 (defun gnus-summary-execute-command (header regexp command &optional backward)
10735   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10736 If HEADER is an empty string (or nil), the match is done on the entire
10737 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10738   (interactive
10739    (list (let ((completion-ignore-case t))
10740            (completing-read
10741             "Header name: "
10742             (mapcar (lambda (string) (list string))
10743                     '("Number" "Subject" "From" "Lines" "Date"
10744                       "Message-ID" "Xref" "References" "Body"))
10745             nil 'require-match))
10746          (read-string "Regexp: ")
10747          (read-key-sequence "Command: ")
10748          current-prefix-arg))
10749   (when (equal header "Body")
10750     (setq header ""))
10751   (gnus-set-global-variables)
10752   ;; Hidden thread subtrees must be searched as well.
10753   (gnus-summary-show-all-threads)
10754   ;; We don't want to change current point nor window configuration.
10755   (save-excursion
10756     (save-window-excursion
10757       (gnus-message 6 "Executing %s..." (key-description command))
10758       ;; We'd like to execute COMMAND interactively so as to give arguments.
10759       (gnus-execute header regexp
10760                     `(lambda () (call-interactively ',(key-binding command)))
10761                     backward)
10762       (gnus-message 6 "Executing %s...done" (key-description command)))))
10763
10764 (defun gnus-summary-beginning-of-article ()
10765   "Scroll the article back to the beginning."
10766   (interactive)
10767   (gnus-set-global-variables)
10768   (gnus-summary-select-article)
10769   (gnus-configure-windows 'article)
10770   (gnus-eval-in-buffer-window
10771    gnus-article-buffer
10772    (widen)
10773    (goto-char (point-min))
10774    (and gnus-break-pages (gnus-narrow-to-page))))
10775
10776 (defun gnus-summary-end-of-article ()
10777   "Scroll to the end of the article."
10778   (interactive)
10779   (gnus-set-global-variables)
10780   (gnus-summary-select-article)
10781   (gnus-configure-windows 'article)
10782   (gnus-eval-in-buffer-window
10783    gnus-article-buffer
10784    (widen)
10785    (goto-char (point-max))
10786    (recenter -3)
10787    (and gnus-break-pages (gnus-narrow-to-page))))
10788
10789 (defun gnus-summary-show-article (&optional arg)
10790   "Force re-fetching of the current article.
10791 If ARG (the prefix) is non-nil, show the raw article without any
10792 article massaging functions being run."
10793   (interactive "P")
10794   (gnus-set-global-variables)
10795   (if (not arg)
10796       ;; Select the article the normal way.
10797       (gnus-summary-select-article nil 'force)
10798     ;; Bind the article treatment functions to nil.
10799     (let ((gnus-have-all-headers t)
10800           gnus-article-display-hook
10801           gnus-article-prepare-hook
10802           gnus-visual)
10803       (gnus-summary-select-article nil 'force)))
10804 ;  (gnus-configure-windows 'article)
10805   (gnus-summary-position-point))
10806
10807 (defun gnus-summary-verbose-headers (&optional arg)
10808   "Toggle permanent full header display.
10809 If ARG is a positive number, turn header display on.
10810 If ARG is a negative number, turn header display off."
10811   (interactive "P")
10812   (gnus-set-global-variables)
10813   (gnus-summary-toggle-header arg)
10814   (setq gnus-show-all-headers
10815         (cond ((or (not (numberp arg))
10816                    (zerop arg))
10817                (not gnus-show-all-headers))
10818               ((natnump arg)
10819                t))))
10820
10821 (defun gnus-summary-toggle-header (&optional arg)
10822   "Show the headers if they are hidden, or hide them if they are shown.
10823 If ARG is a positive number, show the entire header.
10824 If ARG is a negative number, hide the unwanted header lines."
10825   (interactive "P")
10826   (gnus-set-global-variables)
10827   (save-excursion
10828     (set-buffer gnus-article-buffer)
10829     (let* ((buffer-read-only nil)
10830            (inhibit-point-motion-hooks t)
10831            (hidden (text-property-any
10832                     (goto-char (point-min)) (search-forward "\n\n")
10833                     'invisible t))
10834            e)
10835       (goto-char (point-min))
10836       (when (search-forward "\n\n" nil t)
10837         (delete-region (point-min) (1- (point))))
10838       (goto-char (point-min))
10839       (save-excursion
10840         (set-buffer gnus-original-article-buffer)
10841         (goto-char (point-min))
10842         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10843       (insert-buffer-substring gnus-original-article-buffer 1 e)
10844       (let ((gnus-inhibit-hiding t))
10845         (run-hooks 'gnus-article-display-hook))
10846       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10847           (gnus-article-hide-headers)))))
10848
10849 (defun gnus-summary-show-all-headers ()
10850   "Make all header lines visible."
10851   (interactive)
10852   (gnus-set-global-variables)
10853   (gnus-article-show-all-headers))
10854
10855 (defun gnus-summary-toggle-mime (&optional arg)
10856   "Toggle MIME processing.
10857 If ARG is a positive number, turn MIME processing on."
10858   (interactive "P")
10859   (gnus-set-global-variables)
10860   (setq gnus-show-mime
10861         (if (null arg) (not gnus-show-mime)
10862           (> (prefix-numeric-value arg) 0)))
10863   (gnus-summary-select-article t 'force))
10864
10865 (defun gnus-summary-caesar-message (&optional arg)
10866   "Caesar rotate the current article by 13.
10867 The numerical prefix specifies how manu places to rotate each letter
10868 forward."
10869   (interactive "P")
10870   (gnus-set-global-variables)
10871   (gnus-summary-select-article)
10872   (let ((mail-header-separator ""))
10873     (gnus-eval-in-buffer-window
10874      gnus-article-buffer
10875      (save-restriction
10876        (widen)
10877        (let ((start (window-start)))
10878          (news-caesar-buffer-body arg)
10879          (set-window-start (get-buffer-window (current-buffer)) start))))))
10880
10881 (defun gnus-summary-stop-page-breaking ()
10882   "Stop page breaking in the current article."
10883   (interactive)
10884   (gnus-set-global-variables)
10885   (gnus-summary-select-article)
10886   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
10887
10888 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
10889   "Move the current article to a different newsgroup.
10890 If N is a positive number, move the N next articles.
10891 If N is a negative number, move the N previous articles.
10892 If N is nil and any articles have been marked with the process mark,
10893 move those articles instead.
10894 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
10895 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10896 re-spool using this method.
10897
10898 For this function to work, both the current newsgroup and the
10899 newsgroup that you want to move to have to support the `request-move'
10900 and `request-accept' functions."
10901   (interactive "P")
10902   (unless action (setq action 'move))
10903   (gnus-set-global-variables)
10904   ;; Check whether the source group supports the required functions.
10905   (cond ((and (eq action 'move)
10906               (not (gnus-check-backend-function
10907                     'request-move-article gnus-newsgroup-name)))
10908          (error "The current group does not support article moving"))
10909         ((and (eq action 'crosspost)
10910               (not (gnus-check-backend-function
10911                     'request-replace-article gnus-newsgroup-name)))
10912          (error "The current group does not support article editing")))
10913   (let ((articles (gnus-summary-work-articles n))
10914         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10915         (names '((move "move" "Moving")
10916                  (copy "copy" "Copying")
10917                  (crosspost "crosspost" "Crossposting")))
10918         (copy-buf (save-excursion
10919                     (nnheader-set-temp-buffer " *copy article*")))
10920         art-group to-method new-xref article)
10921     (unless (assq action names)
10922       (error "Unknown action %s" action))
10923     ;; Read the newsgroup name.
10924     (when (and (not to-newsgroup)
10925                (not select-method))
10926       (setq to-newsgroup
10927             (gnus-read-move-group-name
10928              (cadr (assq action names))
10929              gnus-current-move-group articles prefix))
10930       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
10931     (setq to-method (if select-method (list select-method "")
10932                       (gnus-find-method-for-group to-newsgroup)))
10933     ;;(when (equal to-newsgroup gnus-newsgroup-name)
10934     ;;(error "Can't %s to the same group you're already in" action))
10935     ;; Check the method we are to move this article to...
10936     (or (gnus-check-backend-function 'request-accept-article (car to-method))
10937         (error "%s does not support article copying" (car to-method)))
10938     (or (gnus-check-server to-method)
10939         (error "Can't open server %s" (car to-method)))
10940     (gnus-message 6 "%s to %s: %s..."
10941                   (caddr (assq action names))
10942                   (or select-method to-newsgroup) articles)
10943     (while articles
10944       (setq article (pop articles))
10945       (setq
10946        art-group
10947        (cond
10948         ;; Move the article.
10949         ((eq action 'move)
10950          (gnus-request-move-article
10951           article                       ; Article to move
10952           gnus-newsgroup-name           ; From newsgrouo
10953           (nth 1 (gnus-find-method-for-group
10954                   gnus-newsgroup-name)) ; Server
10955           (list 'gnus-request-accept-article
10956                 (if select-method
10957                     (list 'quote select-method)
10958                   to-newsgroup)
10959                 (not articles))         ; Accept form
10960           (not articles)))              ; Only save nov last time
10961         ;; Copy the article.
10962         ((eq action 'copy)
10963          (save-excursion
10964            (set-buffer copy-buf)
10965            (gnus-request-article-this-buffer article gnus-newsgroup-name)
10966            (gnus-request-accept-article
10967             (if select-method select-method to-newsgroup)
10968             (not articles))))
10969         ;; Crosspost the article.
10970         ((eq action 'crosspost)
10971          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
10972            (setq new-xref (concat gnus-newsgroup-name ":" article))
10973            (if (and xref (not (string= xref "")))
10974                (progn
10975                  (when (string-match "^Xref: " xref)
10976                    (setq xref (substring xref (match-end 0))))
10977                  (setq new-xref (concat xref " " new-xref)))
10978              (setq new-xref (concat (system-name) " " new-xref)))
10979            (save-excursion
10980              (set-buffer copy-buf)
10981              (gnus-request-article-this-buffer article gnus-newsgroup-name)
10982              (nnheader-replace-header "xref" new-xref)
10983              (gnus-request-accept-article
10984               (if select-method select-method to-newsgroup)
10985               (not articles)))))))
10986       (if (not art-group)
10987           (gnus-message 1 "Couldn't %s article %s"
10988                         (cadr (assq action names)) article)
10989         (let* ((entry
10990                 (or
10991                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
10992                  (gnus-gethash
10993                   (gnus-group-prefixed-name
10994                    (car art-group)
10995                    (if select-method (list select-method "")
10996                      (gnus-find-method-for-group to-newsgroup)))
10997                   gnus-newsrc-hashtb)))
10998                (info (nth 2 entry)))
10999           ;; Update the group that has been moved to.
11000           (when (and info
11001                      (memq action '(move copy)))
11002             (unless (memq article gnus-newsgroup-unreads)
11003               (gnus-info-set-read
11004                info (gnus-add-to-range (gnus-info-read info)
11005                                        (list (cdr art-group)))))
11006
11007             ;; Copy any marks over to the new group.
11008             (let ((marks gnus-article-mark-lists)
11009                   (to-article (cdr art-group)))
11010
11011               ;; See whether the article is to be put in the cache.
11012               (when gnus-use-cache
11013                 (gnus-cache-possibly-enter-article
11014                  (gnus-info-group info) to-article
11015                  (let ((header (copy-sequence
11016                                 (gnus-summary-article-header article))))
11017                    (mail-header-set-number header to-article)
11018                    header)
11019                  (memq article gnus-newsgroup-marked)
11020                  (memq article gnus-newsgroup-dormant)
11021                  (memq article gnus-newsgroup-unreads)))
11022
11023               (while marks
11024                 (when (memq article (symbol-value
11025                                      (intern (format "gnus-newsgroup-%s"
11026                                                      (caar marks)))))
11027                   (gnus-add-marked-articles
11028                    (gnus-info-group info) (cdar marks)
11029                    (list to-article) info))
11030                 (setq marks (cdr marks)))))
11031
11032           ;; Update the Xref header in this article to point to
11033           ;; the new crossposted article we have just created.
11034           (when (eq action 'crosspost)
11035             (save-excursion
11036               (set-buffer copy-buf)
11037               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11038               (nnheader-replace-header
11039                "xref" (concat new-xref " " (gnus-group-prefixed-name
11040                                             (car art-group) to-method)
11041                               ":" (cdr art-group)))
11042               (gnus-request-replace-article
11043                article gnus-newsgroup-name (current-buffer)))))
11044
11045         (gnus-summary-goto-subject article)
11046         (gnus-summary-mark-article article gnus-canceled-mark))
11047       (gnus-summary-remove-process-mark article))
11048     (gnus-kill-buffer copy-buf)
11049     (gnus-summary-position-point)
11050     (gnus-set-mode-line 'summary)))
11051
11052 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11053   "Move the current article to a different newsgroup.
11054 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11055 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11056 re-spool using this method."
11057   (interactive "P")
11058   (gnus-summary-move-article n nil nil 'copy))
11059
11060 (defun gnus-summary-crosspost-article (&optional n)
11061   "Crosspost the current article to some other group."
11062   (interactive "P")
11063   (gnus-summary-move-article n nil nil 'crosspost))
11064
11065 (defun gnus-summary-respool-article (&optional n respool-method)
11066   "Respool the current article.
11067 The article will be squeezed through the mail spooling process again,
11068 which means that it will be put in some mail newsgroup or other
11069 depending on `nnmail-split-methods'.
11070 If N is a positive number, respool the N next articles.
11071 If N is a negative number, respool the N previous articles.
11072 If N is nil and any articles have been marked with the process mark,
11073 respool those articles instead.
11074
11075 Respooling can be done both from mail groups and \"real\" newsgroups.
11076 In the former case, the articles in question will be moved from the
11077 current group into whatever groups they are destined to.  In the
11078 latter case, they will be copied into the relevant groups."
11079   (interactive "P")
11080   (gnus-set-global-variables)
11081   (let ((respool-methods (gnus-methods-using 'respool))
11082         (methname
11083          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11084     (or respool-method
11085         (setq respool-method
11086               (completing-read
11087                "What method do you want to use when respooling? "
11088                respool-methods nil t methname)))
11089     (or (string= respool-method "")
11090         (if (assoc (symbol-name
11091                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11092                    respool-methods)
11093             (gnus-summary-move-article n nil (intern respool-method))
11094           (gnus-summary-copy-article n nil (intern respool-method))))))
11095
11096 (defun gnus-summary-import-article (file)
11097   "Import a random file into a mail newsgroup."
11098   (interactive "fImport file: ")
11099   (gnus-set-global-variables)
11100   (let ((group gnus-newsgroup-name)
11101         (now (current-time))
11102         atts lines)
11103     (or (gnus-check-backend-function 'request-accept-article group)
11104         (error "%s does not support article importing" group))
11105     (or (file-readable-p file)
11106         (not (file-regular-p file))
11107         (error "Can't read %s" file))
11108     (save-excursion
11109       (set-buffer (get-buffer-create " *import file*"))
11110       (buffer-disable-undo (current-buffer))
11111       (erase-buffer)
11112       (insert-file-contents file)
11113       (goto-char (point-min))
11114       (unless (nnheader-article-p)
11115         ;; This doesn't look like an article, so we fudge some headers.
11116         (setq atts (file-attributes file)
11117               lines (count-lines (point-min) (point-max)))
11118         (insert "From: " (read-string "From: ") "\n"
11119                 "Subject: " (read-string "Subject: ") "\n"
11120                 "Date: " (timezone-make-date-arpa-standard
11121                           (current-time-string (nth 5 atts))
11122                           (current-time-zone now)
11123                           (current-time-zone now)) "\n"
11124                 "Message-ID: " (gnus-inews-message-id) "\n"
11125                 "Lines: " (int-to-string lines) "\n"
11126                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11127       (gnus-request-accept-article group t)
11128       (kill-buffer (current-buffer)))))
11129
11130 (defun gnus-summary-expire-articles ()
11131   "Expire all articles that are marked as expirable in the current group."
11132   (interactive)
11133   (gnus-set-global-variables)
11134   (when (gnus-check-backend-function
11135          'request-expire-articles gnus-newsgroup-name)
11136     ;; This backend supports expiry.
11137     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11138            (expirable (if total
11139                           (gnus-list-of-read-articles gnus-newsgroup-name)
11140                         (setq gnus-newsgroup-expirable
11141                               (sort gnus-newsgroup-expirable '<))))
11142            (expiry-wait (gnus-group-get-parameter
11143                          gnus-newsgroup-name 'expiry-wait))
11144            es)
11145       (when expirable
11146         ;; There are expirable articles in this group, so we run them
11147         ;; through the expiry process.
11148         (gnus-message 6 "Expiring articles...")
11149         ;; The list of articles that weren't expired is returned.
11150         (if expiry-wait
11151             (let ((nnmail-expiry-wait-function nil)
11152                   (nnmail-expiry-wait expiry-wait))
11153               (setq es (gnus-request-expire-articles
11154                         expirable gnus-newsgroup-name)))
11155           (setq es (gnus-request-expire-articles
11156                     expirable gnus-newsgroup-name)))
11157         (or total (setq gnus-newsgroup-expirable es))
11158         ;; We go through the old list of expirable, and mark all
11159         ;; really expired articles as nonexistent.
11160         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11161           (let ((gnus-use-cache nil))
11162             (while expirable
11163               (unless (memq (car expirable) es)
11164                 (when (gnus-data-find (car expirable))
11165                   (gnus-summary-mark-article
11166                    (car expirable) gnus-canceled-mark)))
11167               (setq expirable (cdr expirable)))))
11168         (gnus-message 6 "Expiring articles...done")))))
11169
11170 (defun gnus-summary-expire-articles-now ()
11171   "Expunge all expirable articles in the current group.
11172 This means that *all* articles that are marked as expirable will be
11173 deleted forever, right now."
11174   (interactive)
11175   (gnus-set-global-variables)
11176   (or gnus-expert-user
11177       (gnus-y-or-n-p
11178        "Are you really, really, really sure you want to expunge? ")
11179       (error "Phew!"))
11180   (let ((nnmail-expiry-wait 'immediate)
11181         (nnmail-expiry-wait-function nil))
11182     (gnus-summary-expire-articles)))
11183
11184 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11185 (defun gnus-summary-delete-article (&optional n)
11186   "Delete the N next (mail) articles.
11187 This command actually deletes articles.  This is not a marking
11188 command.  The article will disappear forever from your life, never to
11189 return.
11190 If N is negative, delete backwards.
11191 If N is nil and articles have been marked with the process mark,
11192 delete these instead."
11193   (interactive "P")
11194   (gnus-set-global-variables)
11195   (or (gnus-check-backend-function 'request-expire-articles
11196                                    gnus-newsgroup-name)
11197       (error "The current newsgroup does not support article deletion."))
11198   ;; Compute the list of articles to delete.
11199   (let ((articles (gnus-summary-work-articles n))
11200         not-deleted)
11201     (if (and gnus-novice-user
11202              (not (gnus-y-or-n-p
11203                    (format "Do you really want to delete %s forever? "
11204                            (if (> (length articles) 1) "these articles"
11205                              "this article")))))
11206         ()
11207       ;; Delete the articles.
11208       (setq not-deleted (gnus-request-expire-articles
11209                          articles gnus-newsgroup-name 'force))
11210       (while articles
11211         (gnus-summary-remove-process-mark (car articles))
11212         ;; The backend might not have been able to delete the article
11213         ;; after all.
11214         (or (memq (car articles) not-deleted)
11215             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11216         (setq articles (cdr articles))))
11217     (gnus-summary-position-point)
11218     (gnus-set-mode-line 'summary)
11219     not-deleted))
11220
11221 (defun gnus-summary-edit-article (&optional force)
11222   "Enter into a buffer and edit the current article.
11223 This will have permanent effect only in mail groups.
11224 If FORCE is non-nil, allow editing of articles even in read-only
11225 groups."
11226   (interactive "P")
11227   (save-excursion
11228     (set-buffer gnus-summary-buffer)
11229     (gnus-set-global-variables)
11230     (when (and (not force)
11231                (gnus-group-read-only-p))
11232       (error "The current newsgroup does not support article editing."))
11233     (gnus-summary-select-article t nil t)
11234     (gnus-configure-windows 'article)
11235     (select-window (get-buffer-window gnus-article-buffer))
11236     (gnus-message 6 "C-c C-c to end edits")
11237     (setq buffer-read-only nil)
11238     (text-mode)
11239     (use-local-map (copy-keymap (current-local-map)))
11240     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11241     (buffer-enable-undo)
11242     (widen)
11243     (goto-char (point-min))
11244     (search-forward "\n\n" nil t)))
11245
11246 (defun gnus-summary-edit-article-done ()
11247   "Make edits to the current article permanent."
11248   (interactive)
11249   (if (gnus-group-read-only-p)
11250       (progn
11251         (gnus-summary-edit-article-postpone)
11252         (gnus-message
11253          1 "The current newsgroup does not support article editing.")
11254         (ding))
11255     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11256       (erase-buffer)
11257       (insert buf)
11258       (if (not (gnus-request-replace-article
11259                 (cdr gnus-article-current) (car gnus-article-current)
11260                 (current-buffer)))
11261           (error "Couldn't replace article.")
11262         (gnus-article-mode)
11263         (use-local-map gnus-article-mode-map)
11264         (setq buffer-read-only t)
11265         (buffer-disable-undo (current-buffer))
11266         (gnus-configure-windows 'summary)
11267         (gnus-summary-update-article (cdr gnus-article-current)))
11268       (run-hooks 'gnus-article-display-hook)
11269       (and (gnus-visual-p 'summary-highlight 'highlight)
11270            (run-hooks 'gnus-visual-mark-article-hook)))))
11271
11272 (defun gnus-summary-edit-article-postpone ()
11273   "Postpone changes to the current article."
11274   (interactive)
11275   (gnus-article-mode)
11276   (use-local-map gnus-article-mode-map)
11277   (setq buffer-read-only t)
11278   (buffer-disable-undo (current-buffer))
11279   (gnus-configure-windows 'summary)
11280   (and (gnus-visual-p 'summary-highlight 'highlight)
11281        (run-hooks 'gnus-visual-mark-article-hook)))
11282
11283 (defun gnus-summary-respool-query ()
11284   "Query where the respool algorithm would put this article."
11285   (interactive)
11286   (gnus-set-global-variables)
11287   (gnus-summary-select-article)
11288   (save-excursion
11289     (set-buffer gnus-article-buffer)
11290     (save-restriction
11291       (goto-char (point-min))
11292       (search-forward "\n\n")
11293       (narrow-to-region (point-min) (point))
11294       (pp-eval-expression
11295        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11296
11297 ;; Summary score commands.
11298
11299 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11300
11301 (defun gnus-summary-raise-score (n)
11302   "Raise the score of the current article by N."
11303   (interactive "p")
11304   (gnus-set-global-variables)
11305   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11306
11307 (defun gnus-summary-set-score (n)
11308   "Set the score of the current article to N."
11309   (interactive "p")
11310   (gnus-set-global-variables)
11311   (save-excursion
11312     (gnus-summary-show-thread)
11313     (let ((buffer-read-only nil))
11314       ;; Set score.
11315       (gnus-summary-update-mark
11316        (if (= n (or gnus-summary-default-score 0)) ? 
11317          (if (< n (or gnus-summary-default-score 0))
11318              gnus-score-below-mark gnus-score-over-mark)) 'score))
11319     (let* ((article (gnus-summary-article-number))
11320            (score (assq article gnus-newsgroup-scored)))
11321       (if score (setcdr score n)
11322         (setq gnus-newsgroup-scored
11323               (cons (cons article n) gnus-newsgroup-scored))))
11324     (gnus-summary-update-line)))
11325
11326 (defun gnus-summary-current-score ()
11327   "Return the score of the current article."
11328   (interactive)
11329   (gnus-set-global-variables)
11330   (message "%s" (gnus-summary-article-score)))
11331
11332 ;; Summary marking commands.
11333
11334 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11335   "Mark articles which has the same subject as read, and then select the next.
11336 If UNMARK is positive, remove any kind of mark.
11337 If UNMARK is negative, tick articles."
11338   (interactive "P")
11339   (gnus-set-global-variables)
11340   (if unmark
11341       (setq unmark (prefix-numeric-value unmark)))
11342   (let ((count
11343          (gnus-summary-mark-same-subject
11344           (gnus-summary-article-subject) unmark)))
11345     ;; Select next unread article.  If auto-select-same mode, should
11346     ;; select the first unread article.
11347     (gnus-summary-next-article t (and gnus-auto-select-same
11348                                       (gnus-summary-article-subject)))
11349     (gnus-message 7 "%d article%s marked as %s"
11350                   count (if (= count 1) " is" "s are")
11351                   (if unmark "unread" "read"))))
11352
11353 (defun gnus-summary-kill-same-subject (&optional unmark)
11354   "Mark articles which has the same subject as read.
11355 If UNMARK is positive, remove any kind of mark.
11356 If UNMARK is negative, tick articles."
11357   (interactive "P")
11358   (gnus-set-global-variables)
11359   (if unmark
11360       (setq unmark (prefix-numeric-value unmark)))
11361   (let ((count
11362          (gnus-summary-mark-same-subject
11363           (gnus-summary-article-subject) unmark)))
11364     ;; If marked as read, go to next unread subject.
11365     (if (null unmark)
11366         ;; Go to next unread subject.
11367         (gnus-summary-next-subject 1 t))
11368     (gnus-message 7 "%d articles are marked as %s"
11369                   count (if unmark "unread" "read"))))
11370
11371 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11372   "Mark articles with same SUBJECT as read, and return marked number.
11373 If optional argument UNMARK is positive, remove any kinds of marks.
11374 If optional argument UNMARK is negative, mark articles as unread instead."
11375   (let ((count 1))
11376     (save-excursion
11377       (cond
11378        ((null unmark)                   ; Mark as read.
11379         (while (and
11380                 (progn
11381                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11382                   (gnus-summary-show-thread) t)
11383                 (gnus-summary-find-subject subject))
11384           (setq count (1+ count))))
11385        ((> unmark 0)                    ; Tick.
11386         (while (and
11387                 (progn
11388                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11389                   (gnus-summary-show-thread) t)
11390                 (gnus-summary-find-subject subject))
11391           (setq count (1+ count))))
11392        (t                               ; Mark as unread.
11393         (while (and
11394                 (progn
11395                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11396                   (gnus-summary-show-thread) t)
11397                 (gnus-summary-find-subject subject))
11398           (setq count (1+ count)))))
11399       (gnus-set-mode-line 'summary)
11400       ;; Return the number of marked articles.
11401       count)))
11402
11403 (defun gnus-summary-mark-as-processable (n &optional unmark)
11404   "Set the process mark on the next N articles.
11405 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11406 the process mark instead.  The difference between N and the actual
11407 number of articles marked is returned."
11408   (interactive "p")
11409   (gnus-set-global-variables)
11410   (let ((backward (< n 0))
11411         (n (abs n)))
11412     (while (and
11413             (> n 0)
11414             (if unmark
11415                 (gnus-summary-remove-process-mark
11416                  (gnus-summary-article-number))
11417               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11418             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11419       (setq n (1- n)))
11420     (if (/= 0 n) (gnus-message 7 "No more articles"))
11421     (gnus-summary-recenter)
11422     (gnus-summary-position-point)
11423     n))
11424
11425 (defun gnus-summary-unmark-as-processable (n)
11426   "Remove the process mark from the next N articles.
11427 If N is negative, mark backward instead.  The difference between N and
11428 the actual number of articles marked is returned."
11429   (interactive "p")
11430   (gnus-set-global-variables)
11431   (gnus-summary-mark-as-processable n t))
11432
11433 (defun gnus-summary-unmark-all-processable ()
11434   "Remove the process mark from all articles."
11435   (interactive)
11436   (gnus-set-global-variables)
11437   (save-excursion
11438     (while gnus-newsgroup-processable
11439       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11440   (gnus-summary-position-point))
11441
11442 (defun gnus-summary-mark-as-expirable (n)
11443   "Mark N articles forward as expirable.
11444 If N is negative, mark backward instead.  The difference between N and
11445 the actual number of articles marked is returned."
11446   (interactive "p")
11447   (gnus-set-global-variables)
11448   (gnus-summary-mark-forward n gnus-expirable-mark))
11449
11450 (defun gnus-summary-mark-article-as-replied (article)
11451   "Mark ARTICLE replied and update the summary line."
11452   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11453   (let ((buffer-read-only nil))
11454     (when (gnus-summary-goto-subject article)
11455       (gnus-summary-update-secondary-mark article))))
11456
11457 (defun gnus-summary-set-bookmark (article)
11458   "Set a bookmark in current article."
11459   (interactive (list (gnus-summary-article-number)))
11460   (gnus-set-global-variables)
11461   (if (or (not (get-buffer gnus-article-buffer))
11462           (not gnus-current-article)
11463           (not gnus-article-current)
11464           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11465       (error "No current article selected"))
11466   ;; Remove old bookmark, if one exists.
11467   (let ((old (assq article gnus-newsgroup-bookmarks)))
11468     (if old (setq gnus-newsgroup-bookmarks
11469                   (delq old gnus-newsgroup-bookmarks))))
11470   ;; Set the new bookmark, which is on the form
11471   ;; (article-number . line-number-in-body).
11472   (setq gnus-newsgroup-bookmarks
11473         (cons
11474          (cons article
11475                (save-excursion
11476                  (set-buffer gnus-article-buffer)
11477                  (count-lines
11478                   (min (point)
11479                        (save-excursion
11480                          (goto-char (point-min))
11481                          (search-forward "\n\n" nil t)
11482                          (point)))
11483                   (point))))
11484          gnus-newsgroup-bookmarks))
11485   (gnus-message 6 "A bookmark has been added to the current article."))
11486
11487 (defun gnus-summary-remove-bookmark (article)
11488   "Remove the bookmark from the current article."
11489   (interactive (list (gnus-summary-article-number)))
11490   (gnus-set-global-variables)
11491   ;; Remove old bookmark, if one exists.
11492   (let ((old (assq article gnus-newsgroup-bookmarks)))
11493     (if old
11494         (progn
11495           (setq gnus-newsgroup-bookmarks
11496                 (delq old gnus-newsgroup-bookmarks))
11497           (gnus-message 6 "Removed bookmark."))
11498       (gnus-message 6 "No bookmark in current article."))))
11499
11500 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11501 (defun gnus-summary-mark-as-dormant (n)
11502   "Mark N articles forward as dormant.
11503 If N is negative, mark backward instead.  The difference between N and
11504 the actual number of articles marked is returned."
11505   (interactive "p")
11506   (gnus-set-global-variables)
11507   (gnus-summary-mark-forward n gnus-dormant-mark))
11508
11509 (defun gnus-summary-set-process-mark (article)
11510   "Set the process mark on ARTICLE and update the summary line."
11511   (setq gnus-newsgroup-processable
11512         (cons article
11513               (delq article gnus-newsgroup-processable)))
11514   (when (gnus-summary-goto-subject article)
11515     (gnus-summary-show-thread)
11516     (gnus-summary-update-secondary-mark article)))
11517
11518 (defun gnus-summary-remove-process-mark (article)
11519   "Remove the process mark from ARTICLE and update the summary line."
11520   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11521   (when (gnus-summary-goto-subject article)
11522     (gnus-summary-show-thread)
11523     (gnus-summary-update-secondary-mark article)))
11524
11525 (defun gnus-summary-set-saved-mark (article)
11526   "Set the process mark on ARTICLE and update the summary line."
11527   (push article gnus-newsgroup-saved)
11528   (when (gnus-summary-goto-subject article)
11529     (gnus-summary-update-secondary-mark article)))
11530
11531 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11532   "Mark N articles as read forwards.
11533 If N is negative, mark backwards instead.
11534 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11535 marked as unread.
11536 The difference between N and the actual number of articles marked is
11537 returned."
11538   (interactive "p")
11539   (gnus-set-global-variables)
11540   (let ((backward (< n 0))
11541         (gnus-summary-goto-unread
11542          (and gnus-summary-goto-unread
11543               (not (eq gnus-summary-goto-unread 'never))
11544               (not (memq mark (list gnus-unread-mark
11545                                     gnus-ticked-mark gnus-dormant-mark)))))
11546         (n (abs n))
11547         (mark (or mark gnus-del-mark)))
11548     (while (and (> n 0)
11549                 (gnus-summary-mark-article nil mark no-expire)
11550                 (zerop (gnus-summary-next-subject
11551                         (if backward -1 1)
11552                         (and gnus-summary-goto-unread
11553                              (not (eq gnus-summary-goto-unread 'never)))
11554                         t)))
11555       (setq n (1- n)))
11556     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11557     (gnus-summary-recenter)
11558     (gnus-summary-position-point)
11559     (gnus-set-mode-line 'summary)
11560     n))
11561
11562 (defun gnus-summary-mark-article-as-read (mark)
11563   "Mark the current article quickly as read with MARK."
11564   (let ((article (gnus-summary-article-number)))
11565     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11566     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11567     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11568     (setq gnus-newsgroup-reads
11569           (cons (cons article mark) gnus-newsgroup-reads))
11570     ;; Possibly remove from cache, if that is used.
11571     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11572     ;; Allow the backend to change the mark.
11573     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11574     ;; Check for auto-expiry.
11575     (when (and gnus-newsgroup-auto-expire
11576                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11577                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11578                    (= mark gnus-ancient-mark)
11579                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11580       (setq mark gnus-expirable-mark)
11581       (push article gnus-newsgroup-expirable))
11582     ;; Set the mark in the buffer.
11583     (gnus-summary-update-mark mark 'unread)
11584     t))
11585
11586 (defun gnus-summary-mark-article-as-unread (mark)
11587   "Mark the current article quickly as unread with MARK."
11588   (let ((article (gnus-summary-article-number)))
11589     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11590     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11591     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11592     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11593     (cond ((= mark gnus-ticked-mark)
11594            (push article gnus-newsgroup-marked))
11595           ((= mark gnus-dormant-mark)
11596            (push article gnus-newsgroup-dormant))
11597           (t
11598            (push article gnus-newsgroup-unreads)))
11599     (setq gnus-newsgroup-reads
11600           (delq (assq article gnus-newsgroup-reads)
11601                 gnus-newsgroup-reads))
11602
11603     ;; See whether the article is to be put in the cache.
11604     (and gnus-use-cache
11605          (vectorp (gnus-summary-article-header article))
11606          (save-excursion
11607            (gnus-cache-possibly-enter-article
11608             gnus-newsgroup-name article
11609             (gnus-summary-article-header article)
11610             (= mark gnus-ticked-mark)
11611             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11612
11613     ;; Fix the mark.
11614     (gnus-summary-update-mark mark 'unread)
11615     t))
11616
11617 (defun gnus-summary-mark-article (&optional article mark no-expire)
11618   "Mark ARTICLE with MARK.  MARK can be any character.
11619 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11620 `??' (dormant) and `?E' (expirable).
11621 If MARK is nil, then the default character `?D' is used.
11622 If ARTICLE is nil, then the article on the current line will be
11623 marked."
11624   ;; The mark might be a string.
11625   (and (stringp mark)
11626        (setq mark (aref mark 0)))
11627   ;; If no mark is given, then we check auto-expiring.
11628   (and (not no-expire)
11629        gnus-newsgroup-auto-expire
11630        (or (not mark)
11631            (and (numberp mark)
11632                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11633                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11634                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11635        (setq mark gnus-expirable-mark))
11636   (let* ((mark (or mark gnus-del-mark))
11637          (article (or article (gnus-summary-article-number))))
11638     (or article (error "No article on current line"))
11639     (if (or (= mark gnus-unread-mark)
11640             (= mark gnus-ticked-mark)
11641             (= mark gnus-dormant-mark))
11642         (gnus-mark-article-as-unread article mark)
11643       (gnus-mark-article-as-read article mark))
11644
11645     ;; See whether the article is to be put in the cache.
11646     (and gnus-use-cache
11647          (not (= mark gnus-canceled-mark))
11648          (vectorp (gnus-summary-article-header article))
11649          (save-excursion
11650            (gnus-cache-possibly-enter-article
11651             gnus-newsgroup-name article
11652             (gnus-summary-article-header article)
11653             (= mark gnus-ticked-mark)
11654             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11655
11656     (if (gnus-summary-goto-subject article nil t)
11657         (let ((buffer-read-only nil))
11658           (gnus-summary-show-thread)
11659           ;; Fix the mark.
11660           (gnus-summary-update-mark mark 'unread)
11661           t))))
11662
11663 (defun gnus-summary-update-secondary-mark (article)
11664   "Update the secondary (read, process, cache) mark."
11665   (gnus-summary-update-mark
11666    (cond ((memq article gnus-newsgroup-processable)
11667           gnus-process-mark)
11668          ((memq article gnus-newsgroup-cached)
11669           gnus-cached-mark)
11670          ((memq article gnus-newsgroup-replied)
11671           gnus-replied-mark)
11672          ((memq article gnus-newsgroup-saved)
11673           gnus-saved-mark)
11674          (t gnus-unread-mark))
11675    'replied)
11676   (when (gnus-visual-p 'summary-highlight 'highlight)
11677     (run-hooks 'gnus-summary-update-hook))
11678   t)
11679
11680 (defun gnus-summary-update-mark (mark type)
11681   (beginning-of-line)
11682   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11683         (buffer-read-only nil))
11684     (when forward
11685       ;; Go to the right position on the line.
11686       (forward-char forward)
11687       ;; Replace the old mark with the new mark.
11688       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11689       ;; Optionally update the marks by some user rule.
11690       (when (eq type 'unread)
11691         (gnus-data-set-mark
11692          (gnus-data-find (gnus-summary-article-number)) mark)
11693         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11694
11695 (defun gnus-mark-article-as-read (article &optional mark)
11696   "Enter ARTICLE in the pertinent lists and remove it from others."
11697   ;; Make the article expirable.
11698   (let ((mark (or mark gnus-del-mark)))
11699     (if (= mark gnus-expirable-mark)
11700         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11701       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11702     ;; Remove from unread and marked lists.
11703     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11704     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11705     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11706     (push (cons article mark) gnus-newsgroup-reads)
11707     ;; Possibly remove from cache, if that is used.
11708     (when gnus-use-cache
11709       (gnus-cache-enter-remove-article article))))
11710
11711 (defun gnus-mark-article-as-unread (article &optional mark)
11712   "Enter ARTICLE in the pertinent lists and remove it from others."
11713   (let ((mark (or mark gnus-ticked-mark)))
11714     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11715     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11716     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11717     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11718     (cond ((= mark gnus-ticked-mark)
11719            (push article gnus-newsgroup-marked))
11720           ((= mark gnus-dormant-mark)
11721            (push article gnus-newsgroup-dormant))
11722           (t
11723            (push article gnus-newsgroup-unreads)))
11724     (setq gnus-newsgroup-reads
11725           (delq (assq article gnus-newsgroup-reads)
11726                 gnus-newsgroup-reads))))
11727
11728 (defalias 'gnus-summary-mark-as-unread-forward
11729   'gnus-summary-tick-article-forward)
11730 (make-obsolete 'gnus-summary-mark-as-unread-forward
11731                'gnus-summary-tick-article-forward)
11732 (defun gnus-summary-tick-article-forward (n)
11733   "Tick N articles forwards.
11734 If N is negative, tick backwards instead.
11735 The difference between N and the number of articles ticked is returned."
11736   (interactive "p")
11737   (gnus-summary-mark-forward n gnus-ticked-mark))
11738
11739 (defalias 'gnus-summary-mark-as-unread-backward
11740   'gnus-summary-tick-article-backward)
11741 (make-obsolete 'gnus-summary-mark-as-unread-backward
11742                'gnus-summary-tick-article-backward)
11743 (defun gnus-summary-tick-article-backward (n)
11744   "Tick N articles backwards.
11745 The difference between N and the number of articles ticked is returned."
11746   (interactive "p")
11747   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11748
11749 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11750 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11751 (defun gnus-summary-tick-article (&optional article clear-mark)
11752   "Mark current article as unread.
11753 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11754 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11755   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11756                                        gnus-ticked-mark)))
11757
11758 (defun gnus-summary-mark-as-read-forward (n)
11759   "Mark N articles as read forwards.
11760 If N is negative, mark backwards instead.
11761 The difference between N and the actual number of articles marked is
11762 returned."
11763   (interactive "p")
11764   (gnus-summary-mark-forward n gnus-del-mark t))
11765
11766 (defun gnus-summary-mark-as-read-backward (n)
11767   "Mark the N articles as read backwards.
11768 The difference between N and the actual number of articles marked is
11769 returned."
11770   (interactive "p")
11771   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11772
11773 (defun gnus-summary-mark-as-read (&optional article mark)
11774   "Mark current article as read.
11775 ARTICLE specifies the article to be marked as read.
11776 MARK specifies a string to be inserted at the beginning of the line."
11777   (gnus-summary-mark-article article mark))
11778
11779 (defun gnus-summary-clear-mark-forward (n)
11780   "Clear marks from N articles forward.
11781 If N is negative, clear backward instead.
11782 The difference between N and the number of marks cleared is returned."
11783   (interactive "p")
11784   (gnus-summary-mark-forward n gnus-unread-mark))
11785
11786 (defun gnus-summary-clear-mark-backward (n)
11787   "Clear marks from N articles backward.
11788 The difference between N and the number of marks cleared is returned."
11789   (interactive "p")
11790   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11791
11792 (defun gnus-summary-mark-unread-as-read ()
11793   "Intended to be used by `gnus-summary-mark-article-hook'."
11794   (when (memq gnus-current-article gnus-newsgroup-unreads)
11795     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11796
11797 (defun gnus-summary-mark-region-as-read (point mark all)
11798   "Mark all unread articles between point and mark as read.
11799 If given a prefix, mark all articles between point and mark as read,
11800 even ticked and dormant ones."
11801   (interactive "r\nP")
11802   (save-excursion
11803     (let (article)
11804       (goto-char point)
11805       (beginning-of-line)
11806       (while (and
11807               (< (point) mark)
11808               (progn
11809                 (when (or all
11810                           (memq (setq article (gnus-summary-article-number))
11811                                 gnus-newsgroup-unreads))
11812                   (gnus-summary-mark-article article gnus-del-mark))
11813                 t)
11814               (gnus-summary-find-next))))))
11815
11816 (defun gnus-summary-mark-below (score mark)
11817   "Mark articles with score less than SCORE with MARK."
11818   (interactive "P\ncMark: ")
11819   (gnus-set-global-variables)
11820   (setq score (if score
11821                   (prefix-numeric-value score)
11822                 (or gnus-summary-default-score 0)))
11823   (save-excursion
11824     (set-buffer gnus-summary-buffer)
11825     (goto-char (point-min))
11826     (while 
11827         (progn
11828           (and (< (gnus-summary-article-score) score)
11829                (gnus-summary-mark-article nil mark))
11830           (gnus-summary-find-next)))))
11831
11832 (defun gnus-summary-kill-below (&optional score)
11833   "Mark articles with score below SCORE as read."
11834   (interactive "P")
11835   (gnus-set-global-variables)
11836   (gnus-summary-mark-below score gnus-killed-mark))
11837
11838 (defun gnus-summary-clear-above (&optional score)
11839   "Clear all marks from articles with score above SCORE."
11840   (interactive "P")
11841   (gnus-set-global-variables)
11842   (gnus-summary-mark-above score gnus-unread-mark))
11843
11844 (defun gnus-summary-tick-above (&optional score)
11845   "Tick all articles with score above SCORE."
11846   (interactive "P")
11847   (gnus-set-global-variables)
11848   (gnus-summary-mark-above score gnus-ticked-mark))
11849
11850 (defun gnus-summary-mark-above (score mark)
11851   "Mark articles with score over SCORE with MARK."
11852   (interactive "P\ncMark: ")
11853   (gnus-set-global-variables)
11854   (setq score (if score
11855                   (prefix-numeric-value score)
11856                 (or gnus-summary-default-score 0)))
11857   (save-excursion
11858     (set-buffer gnus-summary-buffer)
11859     (goto-char (point-min))
11860     (while (and (progn
11861                   (if (> (gnus-summary-article-score) score)
11862                       (gnus-summary-mark-article nil mark))
11863                   t)
11864                 (gnus-summary-find-next)))))
11865
11866 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11867 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11868 (defun gnus-summary-limit-include-expunged ()
11869   "Display all the hidden articles that were expunged for low scores."
11870   (interactive)
11871   (gnus-set-global-variables)
11872   (let ((buffer-read-only nil))
11873     (let ((scored gnus-newsgroup-scored)
11874           headers h)
11875       (while scored
11876         (or (gnus-summary-goto-subject (car (car scored)))
11877             (and (setq h (gnus-summary-article-header (car (car scored))))
11878                  (< (cdr (car scored)) gnus-summary-expunge-below)
11879                  (setq headers (cons h headers))))
11880         (setq scored (cdr scored)))
11881       (or headers (error "No expunged articles hidden."))
11882       (goto-char (point-min))
11883       (gnus-summary-prepare-unthreaded (nreverse headers)))
11884     (goto-char (point-min))
11885     (gnus-summary-position-point)))
11886
11887 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
11888   "Mark all articles not marked as unread in this newsgroup as read.
11889 If prefix argument ALL is non-nil, all articles are marked as read.
11890 If QUIETLY is non-nil, no questions will be asked.
11891 If TO-HERE is non-nil, it should be a point in the buffer.  All
11892 articles before this point will be marked as read.
11893 The number of articles marked as read is returned."
11894   (interactive "P")
11895   (gnus-set-global-variables)
11896   (prog1
11897       (if (or quietly
11898               (not gnus-interactive-catchup) ;Without confirmation?
11899               gnus-expert-user
11900               (gnus-y-or-n-p
11901                (if all
11902                    "Mark absolutely all articles as read? "
11903                  "Mark all unread articles as read? ")))
11904           (if (and not-mark
11905                    (not gnus-newsgroup-adaptive)
11906                    (not gnus-newsgroup-auto-expire))
11907               (progn
11908                 (when all
11909                   (setq gnus-newsgroup-marked nil
11910                         gnus-newsgroup-dormant nil))
11911                 (setq gnus-newsgroup-unreads nil))
11912             ;; We actually mark all articles as canceled, which we
11913             ;; have to do when using auto-expiry or adaptive scoring.
11914             (gnus-summary-show-all-threads)
11915             (if (gnus-summary-first-subject (not all))
11916                 (while (and
11917                         (if to-here (< (point) to-here) t)
11918                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11919                         (gnus-summary-find-next (not all)))))
11920             (unless to-here
11921               (setq gnus-newsgroup-unreads nil))
11922             (gnus-set-mode-line 'summary)))
11923     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
11924       (if (and (not to-here) (eq 'nnvirtual (car method)))
11925           (nnvirtual-catchup-group
11926            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
11927     (gnus-summary-position-point)))
11928
11929 (defun gnus-summary-catchup-to-here (&optional all)
11930   "Mark all unticked articles before the current one as read.
11931 If ALL is non-nil, also mark ticked and dormant articles as read."
11932   (interactive "P")
11933   (gnus-set-global-variables)
11934   (save-excursion
11935     (let ((beg (point)))
11936       ;; We check that there are unread articles.
11937       (when (or all (gnus-summary-find-prev))
11938         (gnus-summary-catchup all t beg))))
11939   (gnus-summary-position-point))
11940
11941 (defun gnus-summary-catchup-all (&optional quietly)
11942   "Mark all articles in this newsgroup as read."
11943   (interactive "P")
11944   (gnus-set-global-variables)
11945   (gnus-summary-catchup t quietly))
11946
11947 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11948   "Mark all articles not marked as unread in this newsgroup as read, then exit.
11949 If prefix argument ALL is non-nil, all articles are marked as read."
11950   (interactive "P")
11951   (gnus-set-global-variables)
11952   (gnus-summary-catchup all quietly nil 'fast)
11953   ;; Select next newsgroup or exit.
11954   (if (eq gnus-auto-select-next 'quietly)
11955       (gnus-summary-next-group nil)
11956     (gnus-summary-exit)))
11957
11958 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11959   "Mark all articles in this newsgroup as read, and then exit."
11960   (interactive "P")
11961   (gnus-set-global-variables)
11962   (gnus-summary-catchup-and-exit t quietly))
11963
11964 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
11965 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11966   "Mark all articles in this group as read and select the next group.
11967 If given a prefix, mark all articles, unread as well as ticked, as
11968 read."
11969   (interactive "P")
11970   (gnus-set-global-variables)
11971   (save-excursion
11972     (gnus-summary-catchup all))
11973   (gnus-summary-next-article t nil nil t))
11974
11975 ;; Thread-based commands.
11976
11977 (defun gnus-summary-articles-in-thread (&optional article)
11978   "Return a list of all articles in the current thread.
11979 If ARTICLE is non-nil, return all articles in the thread that starts
11980 with that article."
11981   (let* ((article (or article (gnus-summary-article-number)))
11982          (data (gnus-data-find-list article))
11983          (top-level (gnus-data-level (car data)))
11984          (top-subject
11985           (cond ((null gnus-thread-operation-ignore-subject)
11986                  (gnus-simplify-subject-re
11987                   (mail-header-subject (gnus-data-header (car data)))))
11988                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11989                  (gnus-simplify-subject-fuzzy
11990                   (mail-header-subject (gnus-data-header (car data)))))
11991                 (t nil)))
11992          articles)
11993     (if (not data)
11994         ()                              ; This article doesn't exist.
11995       (while data
11996         (and (or (not top-subject)
11997                  (string= top-subject
11998                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11999                               (gnus-simplify-subject-fuzzy
12000                                (mail-header-subject
12001                                 (gnus-data-header (car data))))
12002                             (gnus-simplify-subject-re
12003                              (mail-header-subject
12004                               (gnus-data-header (car data)))))))
12005              (setq articles (cons (gnus-data-number (car data)) articles)))
12006         (if (and (setq data (cdr data))
12007                  (> (gnus-data-level (car data)) top-level))
12008             ()
12009           (setq data nil)))
12010       ;; Return the list of articles.
12011       (nreverse articles))))
12012
12013 (defun gnus-summary-rethread-current ()
12014   "Rethread the thread the current article is part of."
12015   (interactive)
12016   (gnus-set-global-variables)
12017   (let* ((gnus-show-threads t)
12018          (article (gnus-summary-article-number))
12019          (id (mail-header-id (gnus-summary-article-header)))
12020          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12021     (unless id
12022       (error "No article on the current line"))
12023     (gnus-rebuild-thread id)
12024     (gnus-summary-goto-subject article)))
12025
12026 (defun gnus-summary-reparent-thread ()
12027   "Make current article child of the marked (or previous) article.
12028
12029 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12030 is non-nil or the Subject: of both articles are the same."
12031   (interactive)
12032   (or (not (gnus-group-read-only-p))
12033       (error "The current newsgroup does not support article editing."))
12034   (or (<= (length gnus-newsgroup-processable) 1)
12035       (error "No more than one article may be marked."))
12036   (save-window-excursion
12037     (let ((gnus-article-buffer " *reparent*")
12038           (current-article (gnus-summary-article-number))
12039           ; first grab the marked article, otherwise one line up.
12040           (parent-article (if (not (null gnus-newsgroup-processable))
12041                               (car gnus-newsgroup-processable)
12042                             (save-excursion
12043                               (if (eq (forward-line -1) 0)
12044                                   (gnus-summary-article-number)
12045                                 (error "Beginning of summary buffer."))))))
12046       (or (not (eq current-article parent-article))
12047           (error "An article may not be self-referential."))
12048       (let ((message-id (mail-header-id 
12049                          (gnus-summary-article-header parent-article))))
12050         (or (and message-id (not (equal message-id "")))
12051             (error "No message-id in desired parent."))
12052         (gnus-summary-select-article t t nil current-article)
12053         (set-buffer gnus-article-buffer)
12054         (setq buffer-read-only nil)
12055         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12056           (erase-buffer)
12057           (insert buf))
12058         (goto-char (point-min))
12059         (if (search-forward-regexp "^References: " nil t)
12060             (insert message-id " " )
12061           (insert "References: " message-id "\n"))
12062         (or (gnus-request-replace-article current-article
12063                                           (car gnus-article-current)
12064                                           gnus-article-buffer)
12065             (error "Couldn't replace article."))
12066         (set-buffer gnus-summary-buffer)
12067         (gnus-summary-unmark-all-processable)
12068         (gnus-summary-rethread-current)
12069         (message "Article %d is now the child of article %d."
12070                  current-article parent-article)))))
12071
12072 (defun gnus-summary-toggle-threads (&optional arg)
12073   "Toggle showing conversation threads.
12074 If ARG is positive number, turn showing conversation threads on."
12075   (interactive "P")
12076   (gnus-set-global-variables)
12077   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12078     (setq gnus-show-threads
12079           (if (null arg) (not gnus-show-threads)
12080             (> (prefix-numeric-value arg) 0)))
12081     (gnus-summary-prepare)
12082     (gnus-summary-goto-subject current)
12083     (gnus-summary-position-point)))
12084
12085 (defun gnus-summary-show-all-threads ()
12086   "Show all threads."
12087   (interactive)
12088   (gnus-set-global-variables)
12089   (save-excursion
12090     (let ((buffer-read-only nil))
12091       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12092   (gnus-summary-position-point))
12093
12094 (defun gnus-summary-show-thread ()
12095   "Show thread subtrees.
12096 Returns nil if no thread was there to be shown."
12097   (interactive)
12098   (gnus-set-global-variables)
12099   (let ((buffer-read-only nil)
12100         (orig (point))
12101         ;; first goto end then to beg, to have point at beg after let
12102         (end (progn (end-of-line) (point)))
12103         (beg (progn (beginning-of-line) (point))))
12104     (prog1
12105         ;; Any hidden lines here?
12106         (search-forward "\r" end t)
12107       (subst-char-in-region beg end ?\^M ?\n t)
12108       (goto-char orig)
12109       (gnus-summary-position-point))))
12110
12111 (defun gnus-summary-hide-all-threads ()
12112   "Hide all thread subtrees."
12113   (interactive)
12114   (gnus-set-global-variables)
12115   (save-excursion
12116     (goto-char (point-min))
12117     (gnus-summary-hide-thread)
12118     (while (zerop (gnus-summary-next-thread 1 t))
12119       (gnus-summary-hide-thread)))
12120   (gnus-summary-position-point))
12121
12122 (defun gnus-summary-hide-thread ()
12123   "Hide thread subtrees.
12124 Returns nil if no threads were there to be hidden."
12125   (interactive)
12126   (gnus-set-global-variables)
12127   (let ((buffer-read-only nil)
12128         (start (point))
12129         (article (gnus-summary-article-number))
12130         end)
12131     ;; Go forward until either the buffer ends or the subthread
12132     ;; ends.
12133     (when (and (not (eobp))
12134                (or (zerop (gnus-summary-next-thread 1 t))
12135                    (goto-char (point-max))))
12136       (setq end (point))
12137       (prog1
12138           (if (and (> (point) start)
12139                    (search-backward "\n" start t))
12140               (progn
12141                 (subst-char-in-region start (point) ?\n ?\^M)
12142                 (gnus-summary-goto-subject article))
12143             (goto-char start)
12144             nil)
12145         (gnus-summary-position-point)))))
12146
12147 (defun gnus-summary-go-to-next-thread (&optional previous)
12148   "Go to the same level (or less) next thread.
12149 If PREVIOUS is non-nil, go to previous thread instead.
12150 Return the article number moved to, or nil if moving was impossible."
12151   (let* ((level (gnus-summary-thread-level))
12152          (article (gnus-summary-article-number))
12153          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12154          oart)
12155     (while data
12156       (if (<= (gnus-data-level (car data)) level)
12157           (setq oart (gnus-data-number (car data))
12158                 data nil)
12159         (setq data (cdr data))))
12160     (and oart
12161          (gnus-summary-goto-subject oart))))
12162
12163 (defun gnus-summary-next-thread (n &optional silent)
12164   "Go to the same level next N'th thread.
12165 If N is negative, search backward instead.
12166 Returns the difference between N and the number of skips actually
12167 done.
12168
12169 If SILENT, don't output messages."
12170   (interactive "p")
12171   (gnus-set-global-variables)
12172   (let ((backward (< n 0))
12173         (n (abs n))
12174         old dum)
12175     (while (and (> n 0)
12176                 (setq old (save-excursion (forward-line 1) (point)))
12177                 (gnus-summary-go-to-next-thread backward))
12178       (when (and (eq gnus-summary-make-false-root 'dummy)
12179                  (setq dum (text-property-not-all
12180                             old (point) 'gnus-intangible nil)))
12181         (goto-char dum))
12182       (decf n))
12183     (gnus-summary-position-point)
12184     (when (and (not silent) (/= 0 n))
12185       (gnus-message 7 "No more threads"))
12186     n))
12187
12188 (defun gnus-summary-prev-thread (n)
12189   "Go to the same level previous N'th thread.
12190 Returns the difference between N and the number of skips actually
12191 done."
12192   (interactive "p")
12193   (gnus-set-global-variables)
12194   (gnus-summary-next-thread (- n)))
12195
12196 (defun gnus-summary-go-down-thread ()
12197   "Go down one level in the current thread."
12198   (let ((children (gnus-summary-article-children)))
12199     (and children
12200          (gnus-summary-goto-subject (car children)))))
12201
12202 (defun gnus-summary-go-up-thread ()
12203   "Go up one level in the current thread."
12204   (let ((parent (gnus-summary-article-parent)))
12205     (and parent
12206          (gnus-summary-goto-subject parent))))
12207
12208 (defun gnus-summary-down-thread (n)
12209   "Go down thread N steps.
12210 If N is negative, go up instead.
12211 Returns the difference between N and how many steps down that were
12212 taken."
12213   (interactive "p")
12214   (gnus-set-global-variables)
12215   (let ((up (< n 0))
12216         (n (abs n)))
12217     (while (and (> n 0)
12218                 (if up (gnus-summary-go-up-thread)
12219                   (gnus-summary-go-down-thread)))
12220       (setq n (1- n)))
12221     (gnus-summary-position-point)
12222     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12223     n))
12224
12225 (defun gnus-summary-up-thread (n)
12226   "Go up thread N steps.
12227 If N is negative, go up instead.
12228 Returns the difference between N and how many steps down that were
12229 taken."
12230   (interactive "p")
12231   (gnus-set-global-variables)
12232   (gnus-summary-down-thread (- n)))
12233
12234 (defun gnus-summary-top-thread ()
12235   "Go to the top of the thread."
12236   (interactive)
12237   (gnus-set-global-variables)
12238   (while (gnus-summary-go-up-thread))
12239   (gnus-summary-article-number))
12240
12241 (defun gnus-summary-kill-thread (&optional unmark)
12242   "Mark articles under current thread as read.
12243 If the prefix argument is positive, remove any kinds of marks.
12244 If the prefix argument is negative, tick articles instead."
12245   (interactive "P")
12246   (gnus-set-global-variables)
12247   (if unmark
12248       (setq unmark (prefix-numeric-value unmark)))
12249   (let ((articles (gnus-summary-articles-in-thread)))
12250     (save-excursion
12251       ;; Expand the thread.
12252       (gnus-summary-show-thread)
12253       ;; Mark all the articles.
12254       (while articles
12255         (gnus-summary-goto-subject (car articles))
12256         (cond ((null unmark)
12257                (gnus-summary-mark-article-as-read gnus-killed-mark))
12258               ((> unmark 0)
12259                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12260               (t
12261                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12262         (setq articles (cdr articles))))
12263     ;; Hide killed subtrees.
12264     (and (null unmark)
12265          gnus-thread-hide-killed
12266          (gnus-summary-hide-thread))
12267     ;; If marked as read, go to next unread subject.
12268     (if (null unmark)
12269         ;; Go to next unread subject.
12270         (gnus-summary-next-subject 1 t)))
12271   (gnus-set-mode-line 'summary))
12272
12273 ;; Summary sorting commands
12274
12275 (defun gnus-summary-sort-by-number (&optional reverse)
12276   "Sort summary buffer by article number.
12277 Argument REVERSE means reverse order."
12278   (interactive "P")
12279   (gnus-summary-sort 'number reverse))
12280
12281 (defun gnus-summary-sort-by-author (&optional reverse)
12282   "Sort summary buffer by author name alphabetically.
12283 If case-fold-search is non-nil, case of letters is ignored.
12284 Argument REVERSE means reverse order."
12285   (interactive "P")
12286   (gnus-summary-sort 'author reverse))
12287
12288 (defun gnus-summary-sort-by-subject (&optional reverse)
12289   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12290 If case-fold-search is non-nil, case of letters is ignored.
12291 Argument REVERSE means reverse order."
12292   (interactive "P")
12293   (gnus-summary-sort 'subject reverse))
12294
12295 (defun gnus-summary-sort-by-date (&optional reverse)
12296   "Sort summary buffer by date.
12297 Argument REVERSE means reverse order."
12298   (interactive "P")
12299   (gnus-summary-sort 'date reverse))
12300
12301 (defun gnus-summary-sort-by-score (&optional reverse)
12302   "Sort summary buffer by score.
12303 Argument REVERSE means reverse order."
12304   (interactive "P")
12305   (gnus-summary-sort 'score reverse))
12306
12307 (defun gnus-summary-sort (predicate reverse)
12308   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12309   (gnus-set-global-variables)
12310   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12311          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12312          (gnus-thread-sort-functions
12313           (list
12314            (if (not reverse)
12315                thread
12316              `(lambda (t1 t2)
12317                 (,thread t2 t1)))))
12318          (gnus-article-sort-functions
12319           (list
12320            (if (not reverse)
12321                article
12322              `(lambda (t1 t2)
12323                 (,article t2 t1)))))
12324          (buffer-read-only)
12325          (gnus-summary-prepare-hook nil))
12326     ;; We do the sorting by regenerating the threads.
12327     (gnus-summary-prepare)
12328     ;; Hide subthreads if needed.
12329     (when (and gnus-show-threads gnus-thread-hide-subtree)
12330       (gnus-summary-hide-all-threads)))
12331   ;; If in async mode, we send some info to the backend.
12332   (when gnus-newsgroup-async
12333     (gnus-request-asynchronous
12334      gnus-newsgroup-name gnus-newsgroup-data)))
12335
12336 (defun gnus-sortable-date (date)
12337   "Make sortable string by string-lessp from DATE.
12338 Timezone package is used."
12339   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12340          (year (aref date 0))
12341          (month (aref date 1))
12342          (day (aref date 2)))
12343     (timezone-make-sortable-date
12344      year month day
12345      (timezone-make-time-string
12346       (aref date 3) (aref date 4) (aref date 5)))))
12347
12348
12349 ;; Summary saving commands.
12350
12351 (defun gnus-summary-save-article (&optional n not-saved)
12352   "Save the current article using the default saver function.
12353 If N is a positive number, save the N next articles.
12354 If N is a negative number, save the N previous articles.
12355 If N is nil and any articles have been marked with the process mark,
12356 save those articles instead.
12357 The variable `gnus-default-article-saver' specifies the saver function."
12358   (interactive "P")
12359   (gnus-set-global-variables)
12360   (let ((articles (gnus-summary-work-articles n))
12361         file header article)
12362     (while articles
12363       (setq header (gnus-summary-article-header
12364                     (setq article (pop articles))))
12365       (if (not (vectorp header))
12366           ;; This is a pseudo-article.
12367           (if (assq 'name header)
12368               (gnus-copy-file (cdr (assq 'name header)))
12369             (gnus-message 1 "Article %d is unsaveable" article))
12370         ;; This is a real article.
12371         (save-window-excursion
12372           (gnus-summary-select-article t nil nil article))
12373         (unless gnus-save-all-headers
12374           ;; Remove headers accoring to `gnus-saved-headers'.
12375           (let ((gnus-visible-headers
12376                  (or gnus-saved-headers gnus-visible-headers)))
12377             (gnus-article-hide-headers nil t)))
12378         ;; Remove any X-Gnus lines.
12379         (save-excursion
12380           (set-buffer gnus-article-buffer)
12381           (save-restriction
12382             (let ((buffer-read-only nil))
12383               (nnheader-narrow-to-headers)
12384               (while (re-search-forward "^X-Gnus" nil t)
12385                 (gnus-delete-line)))))
12386         (save-window-excursion
12387           (if (not gnus-default-article-saver)
12388               (error "No default saver is defined.")
12389             (setq file (funcall
12390                         gnus-default-article-saver
12391                         (cond
12392                          ((not gnus-prompt-before-saving)
12393                           'default)
12394                          ((eq gnus-prompt-before-saving 'always)
12395                           nil)
12396                          (t file))))))
12397         (gnus-summary-remove-process-mark article)
12398         (unless not-saved
12399           (gnus-summary-set-saved-mark article))))
12400     (gnus-summary-position-point)
12401     n))
12402
12403 (defun gnus-summary-pipe-output (&optional arg)
12404   "Pipe the current article to a subprocess.
12405 If N is a positive number, pipe the N next articles.
12406 If N is a negative number, pipe the N previous articles.
12407 If N is nil and any articles have been marked with the process mark,
12408 pipe those articles instead."
12409   (interactive "P")
12410   (gnus-set-global-variables)
12411   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12412     (gnus-summary-save-article arg t))
12413   (gnus-configure-windows 'pipe))
12414
12415 (defun gnus-summary-save-article-mail (&optional arg)
12416   "Append the current article to an mail file.
12417 If N is a positive number, save the N next articles.
12418 If N is a negative number, save the N previous articles.
12419 If N is nil and any articles have been marked with the process mark,
12420 save those articles instead."
12421   (interactive "P")
12422   (gnus-set-global-variables)
12423   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12424     (gnus-summary-save-article arg)))
12425
12426 (defun gnus-summary-save-article-rmail (&optional arg)
12427   "Append the current article to an rmail file.
12428 If N is a positive number, save the N next articles.
12429 If N is a negative number, save the N previous articles.
12430 If N is nil and any articles have been marked with the process mark,
12431 save those articles instead."
12432   (interactive "P")
12433   (gnus-set-global-variables)
12434   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12435     (gnus-summary-save-article arg)))
12436
12437 (defun gnus-summary-save-article-file (&optional arg)
12438   "Append the current article to a file.
12439 If N is a positive number, save the N next articles.
12440 If N is a negative number, save the N previous articles.
12441 If N is nil and any articles have been marked with the process mark,
12442 save those articles instead."
12443   (interactive "P")
12444   (gnus-set-global-variables)
12445   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12446     (gnus-summary-save-article arg)))
12447
12448 (defun gnus-summary-save-article-body-file (&optional arg)
12449   "Append the current article body to a file.
12450 If N is a positive number, save the N next articles.
12451 If N is a negative number, save the N previous articles.
12452 If N is nil and any articles have been marked with the process mark,
12453 save those articles instead."
12454   (interactive "P")
12455   (gnus-set-global-variables)
12456   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12457     (gnus-summary-save-article arg)))
12458
12459 (defun gnus-get-split-value (methods)
12460   "Return a value based on the split METHODS."
12461   (let (split-name method result match)
12462     (when methods
12463       (save-excursion
12464         (set-buffer gnus-original-article-buffer)
12465         (save-restriction
12466           (nnheader-narrow-to-headers)
12467           (while methods
12468             (goto-char (point-min))
12469             (setq method (pop methods))
12470             (setq match (pop method))
12471             (when (cond
12472                    ((stringp match)
12473                     ;; Regular expression.
12474                     (condition-case ()
12475                         (re-search-forward match nil t)
12476                       (error nil)))
12477                    ((gnus-functionp match)
12478                     ;; Function.
12479                     (save-restriction
12480                       (widen)
12481                       (setq result (funcall match gnus-newsgroup-name))))
12482                    ((consp match)
12483                     ;; Form.
12484                     (save-restriction
12485                       (widen)
12486                       (setq result (eval match)))))
12487               (setq split-name (append (cdr methods) split-name))
12488               (cond ((stringp result)
12489                      (push result split-name))
12490                     ((consp result)
12491                      (setq split-name (append result split-name)))))))))
12492     split-name))
12493
12494 (defun gnus-read-move-group-name (prompt default articles prefix)
12495   "Read a group name."
12496   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12497          (prom
12498           (format "Where do you want to %s %s? "
12499                   prompt
12500                   (if (> (length articles) 1)
12501                       (format "these %d articles" (length articles))
12502                     "this article")))
12503          (to-newsgroup
12504           (cond
12505            ((null split-name)
12506             (completing-read
12507              (concat prom
12508                      (if default
12509                          (format "(default %s) " default)
12510                        ""))
12511              gnus-active-hashtb nil nil prefix))
12512            ((= 1 (length split-name))
12513             (completing-read prom gnus-active-hashtb
12514                              nil nil (cons (car split-name) 0)))
12515            (t
12516             (completing-read
12517              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12518
12519     (when to-newsgroup
12520       (if (or (string= to-newsgroup "")
12521               (string= to-newsgroup prefix))
12522           (setq to-newsgroup (or default "")))
12523       (or (gnus-active to-newsgroup)
12524           (gnus-activate-group to-newsgroup)
12525           (error "No such group: %s" to-newsgroup)))
12526     to-newsgroup))
12527
12528 (defun gnus-read-save-file-name (prompt default-name)
12529   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12530          (file
12531           ;; Let the split methods have their say.
12532           (cond
12533            ;; No split name was found.
12534            ((null split-name)
12535             (read-file-name
12536              (concat prompt " (default "
12537                      (file-name-nondirectory default-name) ") ")
12538              (file-name-directory default-name)
12539              default-name))
12540            ;; A single split name was found
12541            ((= 1 (length split-name))
12542             (read-file-name
12543              (concat prompt " (default " (car split-name) ") ")
12544              gnus-article-save-directory
12545              (concat gnus-article-save-directory (car split-name))))
12546            ;; A list of splits was found.
12547            (t
12548             (setq split-name (mapcar (lambda (el) (list el))
12549                                      (nreverse split-name)))
12550             (let ((result (completing-read
12551                            (concat prompt " ") split-name nil nil)))
12552               (concat gnus-article-save-directory
12553                       (if (string= result "")
12554                           (car (car split-name))
12555                         result)))))))
12556     ;; If we have read a directory, we append the default file name.
12557     (when (file-directory-p file)
12558       (setq file (concat (file-name-as-directory file)
12559                          (file-name-nondirectory default-name))))
12560     ;; Possibly translate some charaters.
12561     (nnheader-translate-file-chars file)))
12562
12563 (defun gnus-article-archive-name (group)
12564   "Return the first instance of an \"Archive-name\" in the current buffer."
12565   (let ((case-fold-search t))
12566     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12567       (match-string 1))))
12568
12569 (defun gnus-summary-save-in-rmail (&optional filename)
12570   "Append this article to Rmail file.
12571 Optional argument FILENAME specifies file name.
12572 Directory to save to is default to `gnus-article-save-directory' which
12573 is initialized from the SAVEDIR environment variable."
12574   (interactive)
12575   (gnus-set-global-variables)
12576   (let ((default-name
12577           (funcall gnus-rmail-save-name gnus-newsgroup-name
12578                    gnus-current-headers gnus-newsgroup-last-rmail)))
12579     (setq filename
12580           (cond ((eq filename 'default)
12581                  default-name)
12582                 (filename filename)
12583                 (t (gnus-read-save-file-name
12584                     "Save in rmail file:" default-name))))
12585     (gnus-make-directory (file-name-directory filename))
12586     (gnus-eval-in-buffer-window
12587      gnus-original-article-buffer
12588      (save-excursion
12589        (save-restriction
12590          (widen)
12591          (gnus-output-to-rmail filename))))
12592     ;; Remember the directory name to save articles
12593     (setq gnus-newsgroup-last-rmail filename)))
12594
12595 (defun gnus-summary-save-in-mail (&optional filename)
12596   "Append this article to Unix mail file.
12597 Optional argument FILENAME specifies file name.
12598 Directory to save to is default to `gnus-article-save-directory' which
12599 is initialized from the SAVEDIR environment variable."
12600   (interactive)
12601   (gnus-set-global-variables)
12602   (let ((default-name
12603           (funcall gnus-mail-save-name gnus-newsgroup-name
12604                    gnus-current-headers gnus-newsgroup-last-mail)))
12605     (setq filename
12606           (cond ((eq filename 'default)
12607                  default-name)
12608                 (filename filename)
12609                 (t (gnus-read-save-file-name
12610                     "Save in Unix mail file:" default-name))))
12611     (setq filename
12612           (expand-file-name filename
12613                             (and default-name
12614                                  (file-name-directory default-name))))
12615     (gnus-make-directory (file-name-directory filename))
12616     (gnus-eval-in-buffer-window
12617      gnus-original-article-buffer
12618      (save-excursion
12619        (save-restriction
12620          (widen)
12621          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12622              (gnus-output-to-rmail filename)
12623            (let ((mail-use-rfc822 t))
12624              (rmail-output filename 1 t t))))))
12625     ;; Remember the directory name to save articles.
12626     (setq gnus-newsgroup-last-mail filename)))
12627
12628 (defun gnus-summary-save-in-file (&optional filename)
12629   "Append this article to file.
12630 Optional argument FILENAME specifies file name.
12631 Directory to save to is default to `gnus-article-save-directory' which
12632 is initialized from the SAVEDIR environment variable."
12633   (interactive)
12634   (gnus-set-global-variables)
12635   (let ((default-name
12636           (funcall gnus-file-save-name gnus-newsgroup-name
12637                    gnus-current-headers gnus-newsgroup-last-file)))
12638     (setq filename
12639           (cond ((eq filename 'default)
12640                  default-name)
12641                 (filename filename)
12642                 (t (gnus-read-save-file-name
12643                     "Save in file:" default-name))))
12644     (gnus-make-directory (file-name-directory filename))
12645     (gnus-eval-in-buffer-window
12646      gnus-article-buffer
12647      (save-excursion
12648        (save-restriction
12649          (widen)
12650          (gnus-output-to-file filename))))
12651     ;; Remember the directory name to save articles.
12652     (setq gnus-newsgroup-last-file filename)))
12653
12654 (defun gnus-summary-save-body-in-file (&optional filename)
12655   "Append this article body to a file.
12656 Optional argument FILENAME specifies file name.
12657 The directory to save in defaults to `gnus-article-save-directory' which
12658 is initialized from the SAVEDIR environment variable."
12659   (interactive)
12660   (gnus-set-global-variables)
12661   (let ((default-name
12662           (funcall gnus-file-save-name gnus-newsgroup-name
12663                    gnus-current-headers gnus-newsgroup-last-file)))
12664     (setq filename
12665           (cond ((eq filename 'default)
12666                  default-name)
12667                 (filename filename)
12668                 (t (gnus-read-save-file-name
12669                     "Save body in file:" default-name))))
12670     (gnus-make-directory (file-name-directory filename))
12671     (gnus-eval-in-buffer-window
12672      gnus-article-buffer
12673      (save-excursion
12674        (save-restriction
12675          (widen)
12676          (goto-char (point-min))
12677          (and (search-forward "\n\n" nil t)
12678               (narrow-to-region (point) (point-max)))
12679          (gnus-output-to-file filename))))
12680     ;; Remember the directory name to save articles.
12681     (setq gnus-newsgroup-last-file filename)))
12682
12683 (defun gnus-summary-save-in-pipe (&optional command)
12684   "Pipe this article to subprocess."
12685   (interactive)
12686   (gnus-set-global-variables)
12687   (setq command
12688         (cond ((eq command 'default)
12689                gnus-last-shell-command)
12690               (command command)
12691               (t (read-string "Shell command on article: "
12692                               gnus-last-shell-command))))
12693   (if (string-equal command "")
12694       (setq command gnus-last-shell-command))
12695   (gnus-eval-in-buffer-window
12696    gnus-article-buffer
12697    (save-restriction
12698      (widen)
12699      (shell-command-on-region (point-min) (point-max) command nil)))
12700   (setq gnus-last-shell-command command))
12701
12702 ;; Summary extract commands
12703
12704 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12705   (let ((buffer-read-only nil)
12706         (article (gnus-summary-article-number))
12707         after-article b e)
12708     (or (gnus-summary-goto-subject article)
12709         (error (format "No such article: %d" article)))
12710     (gnus-summary-position-point)
12711     ;; If all commands are to be bunched up on one line, we collect
12712     ;; them here.
12713     (if gnus-view-pseudos-separately
12714         ()
12715       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12716             files action)
12717         (while ps
12718           (setq action (cdr (assq 'action (car ps))))
12719           (setq files (list (cdr (assq 'name (car ps)))))
12720           (while (and ps (cdr ps)
12721                       (string= (or action "1")
12722                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12723             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12724             (setcdr ps (cdr (cdr ps))))
12725           (if (not files)
12726               ()
12727             (if (not (string-match "%s" action))
12728                 (setq files (cons " " files)))
12729             (setq files (cons " " files))
12730             (and (assq 'execute (car ps))
12731                  (setcdr (assq 'execute (car ps))
12732                          (funcall (if (string-match "%s" action)
12733                                       'format 'concat)
12734                                   action
12735                                   (mapconcat (lambda (f) f) files " ")))))
12736           (setq ps (cdr ps)))))
12737     (if (and gnus-view-pseudos (not not-view))
12738         (while pslist
12739           (and (assq 'execute (car pslist))
12740                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12741                                      (eq gnus-view-pseudos 'not-confirm)))
12742           (setq pslist (cdr pslist)))
12743       (save-excursion
12744         (while pslist
12745           (setq after-article (or (cdr (assq 'article (car pslist)))
12746                                   (gnus-summary-article-number)))
12747           (gnus-summary-goto-subject after-article)
12748           (forward-line 1)
12749           (setq b (point))
12750           (insert "          " (file-name-nondirectory
12751                                 (cdr (assq 'name (car pslist))))
12752                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12753           (setq e (point))
12754           (forward-line -1)             ; back to `b'
12755           (add-text-properties
12756            b e (list 'gnus-number gnus-reffed-article-number
12757                      gnus-mouse-face-prop gnus-mouse-face))
12758           (gnus-data-enter
12759            after-article gnus-reffed-article-number
12760            gnus-unread-mark b (car pslist) 0 (- e b))
12761           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12762           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12763           (setq pslist (cdr pslist)))))))
12764
12765 (defun gnus-pseudos< (p1 p2)
12766   (let ((c1 (cdr (assq 'action p1)))
12767         (c2 (cdr (assq 'action p2))))
12768     (and c1 c2 (string< c1 c2))))
12769
12770 (defun gnus-request-pseudo-article (props)
12771   (cond ((assq 'execute props)
12772          (gnus-execute-command (cdr (assq 'execute props)))))
12773   (let ((gnus-current-article (gnus-summary-article-number)))
12774     (run-hooks 'gnus-mark-article-hook)))
12775
12776 (defun gnus-execute-command (command &optional automatic)
12777   (save-excursion
12778     (gnus-article-setup-buffer)
12779     (set-buffer gnus-article-buffer)
12780     (let ((command (if automatic command (read-string "Command: " command)))
12781           (buffer-read-only nil))
12782       (erase-buffer)
12783       (insert "$ " command "\n\n")
12784       (if gnus-view-pseudo-asynchronously
12785           (start-process "gnus-execute" nil "sh" "-c" command)
12786         (call-process "sh" nil t nil "-c" command)))))
12787
12788 (defun gnus-copy-file (file &optional to)
12789   "Copy FILE to TO."
12790   (interactive
12791    (list (read-file-name "Copy file: " default-directory)
12792          (read-file-name "Copy file to: " default-directory)))
12793   (gnus-set-global-variables)
12794   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12795   (and (file-directory-p to)
12796        (setq to (concat (file-name-as-directory to)
12797                         (file-name-nondirectory file))))
12798   (copy-file file to))
12799
12800 ;; Summary kill commands.
12801
12802 (defun gnus-summary-edit-global-kill (article)
12803   "Edit the \"global\" kill file."
12804   (interactive (list (gnus-summary-article-number)))
12805   (gnus-set-global-variables)
12806   (gnus-group-edit-global-kill article))
12807
12808 (defun gnus-summary-edit-local-kill ()
12809   "Edit a local kill file applied to the current newsgroup."
12810   (interactive)
12811   (gnus-set-global-variables)
12812   (setq gnus-current-headers (gnus-summary-article-header))
12813   (gnus-set-global-variables)
12814   (gnus-group-edit-local-kill
12815    (gnus-summary-article-number) gnus-newsgroup-name))
12816
12817 \f
12818 ;;;
12819 ;;; Gnus article mode
12820 ;;;
12821
12822 (put 'gnus-article-mode 'mode-class 'special)
12823
12824 (if gnus-article-mode-map
12825     nil
12826   (setq gnus-article-mode-map (make-keymap))
12827   (suppress-keymap gnus-article-mode-map)
12828
12829   (gnus-define-keys
12830    gnus-article-mode-map
12831    " " gnus-article-goto-next-page
12832    "\177" gnus-article-goto-prev-page
12833    "\C-c^" gnus-article-refer-article
12834    "h" gnus-article-show-summary
12835    "s" gnus-article-show-summary
12836    "\C-c\C-m" gnus-article-mail
12837    "?" gnus-article-describe-briefly
12838    gnus-mouse-2 gnus-article-push-button
12839    "\r" gnus-article-press-button
12840    "\t" gnus-article-next-button
12841    "\M-\t" gnus-article-prev-button
12842    "\C-c\C-b" gnus-bug)
12843
12844   (substitute-key-definition
12845    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
12846
12847
12848 (defun gnus-article-mode ()
12849   "Major mode for displaying an article.
12850
12851 All normal editing commands are switched off.
12852
12853 The following commands are available:
12854
12855 \\<gnus-article-mode-map>
12856 \\[gnus-article-next-page]\t Scroll the article one page forwards
12857 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12858 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12859 \\[gnus-article-show-summary]\t Display the summary buffer
12860 \\[gnus-article-mail]\t Send a reply to the address near point
12861 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12862 \\[gnus-info-find-node]\t Go to the Gnus info node"
12863   (interactive)
12864   (when (and menu-bar-mode
12865              (gnus-visual-p 'article-menu 'menu))
12866     (gnus-article-make-menu-bar))
12867   (kill-all-local-variables)
12868   (gnus-simplify-mode-line)
12869   (setq mode-name "Article")
12870   (setq major-mode 'gnus-article-mode)
12871   (make-local-variable 'minor-mode-alist)
12872   (or (assq 'gnus-show-mime minor-mode-alist)
12873       (setq minor-mode-alist
12874             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
12875   (use-local-map gnus-article-mode-map)
12876   (make-local-variable 'page-delimiter)
12877   (setq page-delimiter gnus-page-delimiter)
12878   (buffer-disable-undo (current-buffer))
12879   (setq buffer-read-only t)             ;Disable modification
12880   (run-hooks 'gnus-article-mode-hook))
12881
12882 (defun gnus-article-setup-buffer ()
12883   "Initialize the article buffer."
12884   (let* ((name (if gnus-single-article-buffer "*Article*"
12885                  (concat "*Article " gnus-newsgroup-name "*")))
12886          (original
12887           (progn (string-match "\\*Article" name)
12888                  (concat " *Original Article"
12889                          (substring name (match-end 0))))))
12890     (setq gnus-article-buffer name)
12891     (setq gnus-original-article-buffer original)
12892     ;; This might be a variable local to the summary buffer.
12893     (unless gnus-single-article-buffer
12894       (save-excursion
12895         (set-buffer gnus-summary-buffer)
12896         (setq gnus-article-buffer name)
12897         (setq gnus-original-article-buffer original))
12898       (make-local-variable 'gnus-summary-buffer))
12899     (if (get-buffer name)
12900         (save-excursion
12901           (set-buffer name)
12902           (buffer-disable-undo (current-buffer))
12903           (setq buffer-read-only t)
12904           (gnus-add-current-to-buffer-list)
12905           (or (eq major-mode 'gnus-article-mode)
12906               (gnus-article-mode))
12907           (current-buffer))
12908       (save-excursion
12909         (set-buffer (get-buffer-create name))
12910         (gnus-add-current-to-buffer-list)
12911         (gnus-article-mode)
12912         (current-buffer)))))
12913
12914 ;; Set article window start at LINE, where LINE is the number of lines
12915 ;; from the head of the article.
12916 (defun gnus-article-set-window-start (&optional line)
12917   (set-window-start
12918    (get-buffer-window gnus-article-buffer)
12919    (save-excursion
12920      (set-buffer gnus-article-buffer)
12921      (goto-char (point-min))
12922      (if (not line)
12923          (point-min)
12924        (gnus-message 6 "Moved to bookmark")
12925        (search-forward "\n\n" nil t)
12926        (forward-line line)
12927        (point)))))
12928
12929 (defun gnus-kill-all-overlays ()
12930   "Delete all overlays in the current buffer."
12931   (when (fboundp 'overlay-lists)
12932     (let* ((overlayss (overlay-lists))
12933            (buffer-read-only nil)
12934            (overlays (nconc (car overlayss) (cdr overlayss))))
12935       (while overlays
12936         (delete-overlay (pop overlays))))))
12937
12938 (defun gnus-request-article-this-buffer (article group)
12939   "Get an article and insert it into this buffer."
12940   (prog1
12941       (save-excursion
12942         (erase-buffer)
12943         (gnus-kill-all-overlays)
12944         (setq group (or group gnus-newsgroup-name))
12945
12946         ;; Open server if it has closed.
12947         (gnus-check-server (gnus-find-method-for-group group))
12948
12949         ;; Using `gnus-request-article' directly will insert the article into
12950         ;; `nntp-server-buffer' - so we'll save some time by not having to
12951         ;; copy it from the server buffer into the article buffer.
12952
12953         ;; We only request an article by message-id when we do not have the
12954         ;; headers for it, so we'll have to get those.
12955         (when (stringp article)
12956           (let ((gnus-override-method gnus-refer-article-method))
12957             (gnus-read-header article)))
12958
12959         ;; If the article number is negative, that means that this article
12960         ;; doesn't belong in this newsgroup (possibly), so we find its
12961         ;; message-id and request it by id instead of number.
12962         (when (numberp article)
12963           (save-excursion
12964             (set-buffer gnus-summary-buffer)
12965             (let ((header (gnus-summary-article-header article)))
12966               (if (< article 0)
12967                   (cond 
12968                    ((memq article gnus-newsgroup-sparse)
12969                     ;; This is a sparse gap article.
12970                     (setq article (mail-header-id header)))
12971                    ((vectorp header)
12972                     ;; It's a real article.
12973                     (setq article (mail-header-id header)))
12974                    (t
12975                     ;; It is an extracted pseudo-article.
12976                     (setq article 'pseudo)
12977                     (gnus-request-pseudo-article header))))
12978                 
12979               (let ((method (gnus-find-method-for-group 
12980                              gnus-newsgroup-name)))
12981                 (if (not (eq (car method) 'nneething))
12982                     ()
12983                   (let ((dir (concat (file-name-as-directory (nth 1 method))
12984                                      (mail-header-subject header))))
12985                     (if (file-directory-p dir)
12986                         (progn
12987                           (setq article 'nneething)
12988                           (gnus-group-enter-directory dir)))))))))
12989
12990         (cond
12991          ;; We first check `gnus-original-article-buffer'.
12992          ((and (equal (car gnus-original-article) group)
12993                (eq (cdr gnus-original-article) article)
12994                (get-buffer gnus-original-article-buffer))
12995           (insert-buffer-substring gnus-original-article-buffer)
12996           'article)
12997          ;; Check the backlog.
12998          ((and gnus-keep-backlog
12999                (gnus-backlog-request-article group article (current-buffer)))
13000           'article)
13001          ;; Check the cache.
13002          ((and gnus-use-cache
13003                (numberp article)
13004                (gnus-cache-request-article article group))
13005           'article)
13006          ;; Get the article and put into the article buffer.
13007          ((or (stringp article) (numberp article))
13008           (let ((gnus-override-method
13009                  (and (stringp article) gnus-refer-article-method))
13010                 (buffer-read-only nil))
13011             (erase-buffer)
13012             (gnus-kill-all-overlays)
13013             (if (gnus-request-article article group (current-buffer))
13014                 (progn
13015                   (and gnus-keep-backlog
13016                        (gnus-backlog-enter-article
13017                         group article (current-buffer)))
13018                   'article))))
13019          ;; It was a pseudo.
13020          (t article)))
13021
13022     ;; Take the article from the original article buffer
13023     ;; and place it in the buffer it's supposed to be in.
13024     (setq gnus-original-article (cons group article))
13025     (when (equal (buffer-name (current-buffer))
13026                  (buffer-name (get-buffer gnus-article-buffer)))
13027       (save-excursion
13028         (if (get-buffer gnus-original-article-buffer)
13029             (set-buffer (get-buffer gnus-original-article-buffer))
13030           (set-buffer (get-buffer-create gnus-original-article-buffer))
13031           (buffer-disable-undo (current-buffer))
13032           (setq major-mode 'gnus-original-article-mode)
13033           (setq buffer-read-only t)
13034           (gnus-add-current-to-buffer-list))
13035         (let (buffer-read-only)
13036           (erase-buffer)
13037           (insert-buffer-substring gnus-article-buffer))))
13038     
13039     ;; Update sparse articles.
13040     (when (memq article gnus-newsgroup-sparse)
13041       (gnus-summary-update-article article))))
13042
13043 (defun gnus-read-header (id)
13044   "Read the headers of article ID and enter them into the Gnus system."
13045   (let ((group gnus-newsgroup-name)
13046         (headers gnus-newsgroup-headers)
13047         header where)
13048     ;; First we check to see whether the header in question is already
13049     ;; fetched.
13050     (if (stringp id)
13051         ;; This is a Message-ID.
13052         (setq header (gnus-id-to-header id))
13053       ;; This is an article number.
13054       (setq header (gnus-summary-article-header id)))
13055     (if header
13056         ;; We have found the header.
13057         header
13058       ;; We have to really fetch the header to this article.
13059       (when (setq where
13060                   (if (gnus-check-backend-function 'request-head group)
13061                       (gnus-request-head id group)
13062                     (gnus-request-article id group)))
13063         (save-excursion
13064           (set-buffer nntp-server-buffer)
13065           (and (search-forward "\n\n" nil t)
13066                (delete-region (1- (point)) (point-max)))
13067           (goto-char (point-max))
13068           (insert ".\n")
13069           (goto-char (point-min))
13070           (insert "211 "
13071                   (int-to-string
13072                    (cond
13073                     ((numberp id)
13074                      id)
13075                     ((cdr where)
13076                      (cdr where))
13077                     (t
13078                      gnus-reffed-article-number)))
13079                   " Article retrieved.\n"))
13080         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13081             () ; Malformed head.
13082           (if (and (stringp id)
13083                    (not (string= (gnus-group-real-name group)
13084                                  (car where))))
13085               ;; If we fetched by Message-ID and the article came
13086               ;; from a different group, we fudge some bogus article
13087               ;; numbers for this article.
13088               (mail-header-set-number header gnus-reffed-article-number))
13089           (decf gnus-reffed-article-number)
13090           (push header gnus-newsgroup-headers)
13091           (setq gnus-current-headers header)
13092           (push (mail-header-number header) gnus-newsgroup-limit)
13093           header)))))
13094
13095 (defun gnus-article-prepare (article &optional all-headers header)
13096   "Prepare ARTICLE in article mode buffer.
13097 ARTICLE should either be an article number or a Message-ID.
13098 If ARTICLE is an id, HEADER should be the article headers.
13099 If ALL-HEADERS is non-nil, no headers are hidden."
13100   (save-excursion
13101     ;; Make sure we start in a summary buffer.
13102     (unless (eq major-mode 'gnus-summary-mode)
13103       (set-buffer gnus-summary-buffer))
13104     (setq gnus-summary-buffer (current-buffer))
13105     ;; Make sure the connection to the server is alive.
13106     (unless (gnus-server-opened
13107              (gnus-find-method-for-group gnus-newsgroup-name))
13108       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13109       (gnus-request-group gnus-newsgroup-name t))
13110     (let* ((article (if header (mail-header-number header) article))
13111            (summary-buffer (current-buffer))
13112            (internal-hook gnus-article-internal-prepare-hook)
13113            (group gnus-newsgroup-name)
13114            result)
13115       (save-excursion
13116         (gnus-article-setup-buffer)
13117         (set-buffer gnus-article-buffer)
13118         ;; Deactivate active regions.
13119         (when (and (boundp 'transient-mark-mode)
13120                    transient-mark-mode)
13121           (setq mark-active nil))
13122         (if (not (setq result (let ((buffer-read-only nil))
13123                                 (gnus-request-article-this-buffer
13124                                  article group))))
13125             ;; There is no such article.
13126             (save-excursion
13127               (when (and (numberp article)
13128                          (not (memq article gnus-newsgroup-sparse)))
13129                 (setq gnus-article-current
13130                       (cons gnus-newsgroup-name article))
13131                 (set-buffer gnus-summary-buffer)
13132                 (setq gnus-current-article article)
13133                 (gnus-summary-mark-article article gnus-canceled-mark))
13134               (unless (memq article gnus-newsgroup-sparse)
13135                 (gnus-message
13136                  1 "No such article (may have expired or been canceled)")
13137                 (ding)
13138                 nil))
13139           (if (or (eq result 'pseudo) (eq result 'nneething))
13140               (progn
13141                 (save-excursion
13142                   (set-buffer summary-buffer)
13143                   (setq gnus-last-article gnus-current-article
13144                         gnus-newsgroup-history (cons gnus-current-article
13145                                                      gnus-newsgroup-history)
13146                         gnus-current-article 0
13147                         gnus-current-headers nil
13148                         gnus-article-current nil)
13149                   (if (eq result 'nneething)
13150                       (gnus-configure-windows 'summary)
13151                     (gnus-configure-windows 'article))
13152                   (gnus-set-global-variables))
13153                 (gnus-set-mode-line 'article))
13154             ;; The result from the `request' was an actual article -
13155             ;; or at least some text that is now displayed in the
13156             ;; article buffer.
13157             (if (and (numberp article)
13158                      (not (eq article gnus-current-article)))
13159                 ;; Seems like a new article has been selected.
13160                 ;; `gnus-current-article' must be an article number.
13161                 (save-excursion
13162                   (set-buffer summary-buffer)
13163                   (setq gnus-last-article gnus-current-article
13164                         gnus-newsgroup-history (cons gnus-current-article
13165                                                      gnus-newsgroup-history)
13166                         gnus-current-article article
13167                         gnus-current-headers
13168                         (gnus-summary-article-header gnus-current-article)
13169                         gnus-article-current
13170                         (cons gnus-newsgroup-name gnus-current-article))
13171                   (unless (vectorp gnus-current-headers)
13172                     (setq gnus-current-headers nil))
13173                   (gnus-summary-show-thread)
13174                   (run-hooks 'gnus-mark-article-hook)
13175                   (gnus-set-mode-line 'summary)
13176                   (and (gnus-visual-p 'article-highlight 'highlight)
13177                        (run-hooks 'gnus-visual-mark-article-hook))
13178                   ;; Set the global newsgroup variables here.
13179                   ;; Suggested by Jim Sisolak
13180                   ;; <sisolak@trans4.neep.wisc.edu>.
13181                   (gnus-set-global-variables)
13182                   (setq gnus-have-all-headers
13183                         (or all-headers gnus-show-all-headers))
13184                   (and gnus-use-cache
13185                        (vectorp (gnus-summary-article-header article))
13186                        (gnus-cache-possibly-enter-article
13187                         group article
13188                         (gnus-summary-article-header article)
13189                         (memq article gnus-newsgroup-marked)
13190                         (memq article gnus-newsgroup-dormant)
13191                         (memq article gnus-newsgroup-unreads)))))
13192             ;; Hooks for getting information from the article.
13193             ;; This hook must be called before being narrowed.
13194             (let (buffer-read-only)
13195               (run-hooks 'internal-hook)
13196               (run-hooks 'gnus-article-prepare-hook)
13197               ;; Decode MIME message.
13198               (if gnus-show-mime
13199                   (if (or (not gnus-strict-mime)
13200                           (gnus-fetch-field "Mime-Version"))
13201                       (funcall gnus-show-mime-method)
13202                     (funcall gnus-decode-encoded-word-method)))
13203               ;; Perform the article display hooks.
13204               (run-hooks 'gnus-article-display-hook))
13205             ;; Do page break.
13206             (goto-char (point-min))
13207             (and gnus-break-pages (gnus-narrow-to-page))
13208             (gnus-set-mode-line 'article)
13209             (gnus-configure-windows 'article)
13210             (goto-char (point-min))
13211             t))))))
13212
13213 (defun gnus-article-show-all-headers ()
13214   "Show all article headers in article mode buffer."
13215   (save-excursion
13216     (gnus-article-setup-buffer)
13217     (set-buffer gnus-article-buffer)
13218     (let ((buffer-read-only nil))
13219       (remove-text-properties (point-min) (point-max)
13220                               gnus-hidden-properties))))
13221
13222 (defun gnus-article-hide-headers-if-wanted ()
13223   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13224 Provided for backwards compatibility."
13225   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13226       gnus-inhibit-hiding
13227       (gnus-article-hide-headers)))
13228
13229 (defun gnus-article-hide-headers (&optional arg delete)
13230   "Toggle whether to hide unwanted headers and possibly sort them as well.
13231 If given a negative prefix, always show; if given a positive prefix,
13232 always hide."
13233   (interactive "P")
13234   (unless (gnus-article-check-hidden-text 'headers arg)
13235     ;; This function might be inhibited.
13236     (unless gnus-inhibit-hiding
13237       (save-excursion
13238         (set-buffer gnus-article-buffer)
13239         (save-restriction
13240           (let ((buffer-read-only nil)
13241                 (ignored (when (not (stringp gnus-visible-headers))
13242                            (cond ((stringp gnus-ignored-headers)
13243                                   gnus-ignored-headers)
13244                                  ((listp gnus-ignored-headers)
13245                                   (mapconcat 'identity gnus-ignored-headers
13246                                              "\\|")))))
13247                 (visible
13248                  (cond ((stringp gnus-visible-headers)
13249                         gnus-visible-headers)
13250                        ((listp gnus-visible-headers)
13251                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13252                 want-list beg want-l)
13253             ;; First we narrow to just the headers.
13254             (widen)
13255             (goto-char (point-min))
13256             ;; Hide any "From " lines at the beginning of (mail) articles.
13257             (while (looking-at "From ")
13258               (forward-line 1))
13259             (unless (bobp)
13260               (add-text-properties
13261                (point-min) (point)
13262                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13263             ;; Then treat the rest of the header lines.
13264             (narrow-to-region
13265              (point)
13266              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13267             ;; Then we use the two regular expressions
13268             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13269             ;; select which header lines is to remain visible in the
13270             ;; article buffer.
13271             (goto-char (point-min))
13272             (while (re-search-forward "^[^ \t]*:" nil t)
13273               (beginning-of-line)
13274               ;; We add the headers we want to keep to a list and delete
13275               ;; them from the buffer.
13276               (if (or (and visible (looking-at visible))
13277                       (and ignored (not (looking-at ignored))))
13278                   (progn
13279                     (push (buffer-substring
13280                            (setq beg (point))
13281                            (progn
13282                              (forward-line 1)
13283                              ;; Be sure to get multi-line headers...
13284                              (re-search-forward "^[^ \t]*:" nil t)
13285                              (beginning-of-line)
13286                              (point)))
13287                           want-list)
13288                     (delete-region beg (point)))
13289                 (forward-line 1)))
13290             ;; Sort the headers that we want to display.
13291             (setq want-list (sort want-list 'gnus-article-header-less))
13292             (goto-char (point-min))
13293             (while want-list
13294               (insert (pop want-list)))
13295             ;; We make the unwanted headers invisible.
13296             (if delete
13297                 (delete-region (point-min) (point-max))
13298               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13299               (add-text-properties
13300                (point) (point-max)
13301                (nconc (list 'gnus-type 'headers)
13302                       gnus-hidden-properties)))))))))
13303
13304 (defsubst gnus-article-header-rank (header)
13305   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13306   (let ((list gnus-sorted-header-list)
13307         (i 0))
13308     (while list
13309       (when (string-match (car list) header)
13310         (setq list nil))
13311       (setq list (cdr list))
13312       (incf i))
13313     i))
13314
13315 (defun gnus-article-header-less (h1 h2)
13316   "Say whether string H1 is \"less\" than string H2."
13317   (< (gnus-article-header-rank h1)
13318      (gnus-article-header-rank h2)))
13319
13320 (defun gnus-article-hide-boring-headers (&optional arg)
13321   "Toggle hiding of headers that aren't very interesting.
13322 If given a negative prefix, always show; if given a positive prefix,
13323 always hide."
13324   (interactive "P")
13325   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13326     (save-excursion
13327       (set-buffer gnus-article-buffer)
13328       (save-restriction
13329         (let ((buffer-read-only nil)
13330               (list gnus-boring-article-headers)
13331               (inhibit-point-motion-hooks t)
13332               elem)
13333           (nnheader-narrow-to-headers)
13334           (while list
13335             (setq elem (pop list))
13336             (goto-char (point-min))
13337             (cond
13338              ;; Hide empty headers.
13339              ((eq elem 'empty)
13340               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13341                 (forward-line -1)
13342                 (add-text-properties
13343                  (progn (beginning-of-line) (point))
13344                  (progn 
13345                    (end-of-line)
13346                    (if (re-search-forward "^[^ \t]" nil t)
13347                        (match-beginning 0)
13348                      (point-max)))
13349                  (nconc (list 'gnus-type 'boring-headers)
13350                         gnus-hidden-properties))))
13351              ;; Hide boring Newsgroups header.
13352              ((eq elem 'newsgroups)
13353               (when (equal (mail-fetch-field "newsgroups")
13354                            (gnus-group-real-name gnus-newsgroup-name))
13355                 (gnus-article-hide-header "newsgroups")))
13356              ((eq elem 'followup-to)
13357               (when (equal (mail-fetch-field "followup-to")
13358                            (mail-fetch-field "newsgroups"))
13359                 (gnus-article-hide-header "followup-to")))
13360              ((eq elem 'reply-to)
13361               (let ((from (mail-fetch-field "from"))
13362                     (reply-to (mail-fetch-field "reply-to")))
13363                 (when (and
13364                        from reply-to
13365                        (equal 
13366                         (nth 1 (mail-extract-address-components from))
13367                         (nth 1 (mail-extract-address-components reply-to))))
13368                   (gnus-article-hide-header "reply-to"))))
13369              ((eq elem 'date)
13370               (let ((date (mail-fetch-field "date")))
13371                 (when (and date
13372                            (< (gnus-days-between date (current-time-string))
13373                               4))
13374                   (gnus-article-hide-header "date")))))))))))
13375
13376 (defun gnus-article-hide-header (header)
13377   (save-excursion
13378     (goto-char (point-min))
13379     (when (re-search-forward (concat "^" header ":") nil t)
13380       (add-text-properties
13381        (progn (beginning-of-line) (point))
13382        (progn 
13383          (end-of-line)
13384          (if (re-search-forward "^[^ \t]" nil t)
13385              (match-beginning 0)
13386            (point-max)))
13387        (nconc (list 'gnus-type 'boring-headers)
13388               gnus-hidden-properties)))))
13389
13390 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13391 (defun gnus-article-treat-overstrike ()
13392   "Translate overstrikes into bold text."
13393   (interactive)
13394   (save-excursion
13395     (set-buffer gnus-article-buffer)
13396     (let ((buffer-read-only nil))
13397       (while (search-forward "\b" nil t)
13398         (let ((next (following-char))
13399               (previous (char-after (- (point) 2))))
13400           (cond ((eq next previous)
13401                  (put-text-property (- (point) 2) (point) 'invisible t)
13402                  (put-text-property (point) (1+ (point)) 'face 'bold))
13403                 ((eq next ?_)
13404                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13405                  (put-text-property
13406                   (- (point) 2) (1- (point)) 'face 'underline))
13407                 ((eq previous ?_)
13408                  (put-text-property (- (point) 2) (point) 'invisible t)
13409                  (put-text-property
13410                   (point) (1+ (point))  'face 'underline))))))))
13411
13412 (defun gnus-article-word-wrap ()
13413   "Format too long lines."
13414   (interactive)
13415   (save-excursion
13416     (set-buffer gnus-article-buffer)
13417     (let ((buffer-read-only nil)
13418           p)
13419       (widen)
13420       (goto-char (point-min))
13421       (search-forward "\n\n" nil t)
13422       (end-of-line 1)
13423       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13424             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13425             (adaptive-fill-mode t))
13426         (while (not (eobp))
13427           (and (>= (current-column) (min fill-column (window-width)))
13428                (/= (preceding-char) ?:)
13429                (fill-paragraph nil))
13430           (end-of-line 2))))))
13431
13432 (defun gnus-article-remove-cr ()
13433   "Remove carriage returns from an article."
13434   (interactive)
13435   (save-excursion
13436     (set-buffer gnus-article-buffer)
13437     (let ((buffer-read-only nil))
13438       (goto-char (point-min))
13439       (while (search-forward "\r" nil t)
13440         (replace-match "" t t)))))
13441
13442 (defun gnus-article-remove-trailing-blank-lines ()
13443   "Remove all trailing blank lines from the article."
13444   (interactive)
13445   (save-excursion
13446     (set-buffer gnus-article-buffer)
13447     (let ((buffer-read-only nil))
13448       (goto-char (point-max))
13449       (delete-region
13450        (point)
13451        (progn
13452          (while (looking-at "^[ \t]*$")
13453            (forward-line -1))
13454          (forward-line 1)
13455          (point))))))
13456
13457 (defun gnus-article-display-x-face (&optional force)
13458   "Look for an X-Face header and display it if present."
13459   (interactive (list 'force))
13460   (save-excursion
13461     (set-buffer gnus-article-buffer)
13462     ;; Delete the old process, if any.
13463     (when (process-status "gnus-x-face")
13464       (delete-process "gnus-x-face"))
13465     (let ((inhibit-point-motion-hooks t)
13466           (case-fold-search nil)
13467           from)
13468       (save-restriction
13469         (nnheader-narrow-to-headers)
13470         (setq from (mail-fetch-field "from"))
13471         (goto-char (point-min))
13472         (when (and gnus-article-x-face-command
13473                    (or force
13474                        ;; Check whether this face is censored.
13475                        (not gnus-article-x-face-too-ugly)
13476                        (and gnus-article-x-face-too-ugly from
13477                             (not (string-match gnus-article-x-face-too-ugly
13478                                                from))))
13479                    ;; Has to be present.
13480                    (re-search-forward "^X-Face: " nil t))
13481           ;; We now have the area of the buffer where the X-Face is stored.
13482           (let ((beg (point))
13483                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13484             ;; We display the face.
13485             (if (symbolp gnus-article-x-face-command)
13486                 ;; The command is a lisp function, so we call it.
13487                 (if (gnus-functionp gnus-article-x-face-command)
13488                     (funcall gnus-article-x-face-command beg end)
13489                   (error "%s is not a function" gnus-article-x-face-command))
13490               ;; The command is a string, so we interpret the command
13491               ;; as a, well, command, and fork it off.
13492               (let ((process-connection-type nil))
13493                 (process-kill-without-query
13494                  (start-process
13495                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13496                 (process-send-region "gnus-x-face" beg end)
13497                 (process-send-eof "gnus-x-face")))))))))
13498
13499 (defun gnus-headers-decode-quoted-printable ()
13500   "Hack to remove QP encoding from headers."
13501   (let ((case-fold-search t)
13502         (inhibit-point-motion-hooks t)
13503         string)
13504     (goto-char (point-min))
13505     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13506       (setq string (match-string 1))
13507       (narrow-to-region (match-beginning 0) (match-end 0))
13508       (delete-region (point-min) (point-max))
13509       (insert string)
13510       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13511       (subst-char-in-region (point-min) (point-max) ?_ ? )
13512       (widen)
13513       (goto-char (point-min)))))
13514
13515 (defun gnus-article-de-quoted-unreadable (&optional force)
13516   "Do a naive translation of a quoted-printable-encoded article.
13517 This is in no way, shape or form meant as a replacement for real MIME
13518 processing, but is simply a stop-gap measure until MIME support is
13519 written.
13520 If FORCE, decode the article whether it is marked as quoted-printable
13521 or not."
13522   (interactive (list 'force))
13523   (save-excursion
13524     (set-buffer gnus-article-buffer)
13525     (let ((case-fold-search t)
13526           (buffer-read-only nil)
13527           (type (gnus-fetch-field "content-transfer-encoding")))
13528       (when (or force
13529                 (and type (string-match "quoted-printable" type)))
13530         (gnus-headers-decode-quoted-printable)
13531         (goto-char (point-min))
13532         (search-forward "\n\n" nil 'move)
13533         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13534
13535 (defun gnus-mime-decode-quoted-printable (from to)
13536   "Decode Quoted-Printable in the region between FROM and TO."
13537   (goto-char from)
13538   (while (search-forward "=" to t)
13539     (cond ((eq (following-char) ?\n)
13540            (delete-char -1)
13541            (delete-char 1))
13542           ((looking-at "[0-9A-F][0-9A-F]")
13543            (delete-char -1)
13544            (insert (hexl-hex-string-to-integer
13545                     (buffer-substring (point) (+ 2 (point)))))
13546            (delete-char 2))
13547           ((looking-at "=")
13548            (delete-char 1))
13549           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13550
13551 (defun gnus-article-hide-pgp (&optional arg)
13552   "Toggle hiding of any PGP headers and signatures in the current article.
13553 If given a negative prefix, always show; if given a positive prefix,
13554 always hide."
13555   (interactive "P")
13556   (unless (gnus-article-check-hidden-text 'pgp arg)
13557     (save-excursion
13558       (set-buffer gnus-article-buffer)
13559       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13560             buffer-read-only beg end)
13561         (widen)
13562         (goto-char (point-min))
13563         ;; Hide the "header".
13564         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13565              (add-text-properties (match-beginning 0) (match-end 0) props))
13566         (setq beg (point))
13567         ;; Hide the actual signature.
13568         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13569              (setq end (match-beginning 0))
13570              (add-text-properties
13571               (match-beginning 0)
13572               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13573                   (match-end 0)
13574                 ;; Perhaps we shouldn't hide to the end of the buffer
13575                 ;; if there is no end to the signature?
13576                 (point-max))
13577               props))
13578         ;; Hide "- " PGP quotation markers.
13579         (when (and beg end)
13580           (narrow-to-region beg end)
13581           (goto-char (point-min))
13582           (while (re-search-forward "^- " nil t)
13583             (add-text-properties (match-beginning 0) (match-end 0) props))
13584           (widen))))))
13585
13586 (defun gnus-article-hide-signature (&optional arg)
13587   "Hide the signature in the current article.
13588 If given a negative prefix, always show; if given a positive prefix,
13589 always hide."
13590   (interactive "P")
13591   (unless (gnus-article-check-hidden-text 'signature arg)
13592     (save-excursion
13593       (set-buffer gnus-article-buffer)
13594       (save-restriction
13595         (let ((buffer-read-only nil))
13596           (when (gnus-narrow-to-signature)
13597             (add-text-properties
13598              (point-min) (point-max)
13599              (nconc (list 'gnus-type 'signature)
13600                     gnus-hidden-properties))))))))
13601
13602 (defvar gnus-signature-limit nil
13603   "Provide a limit to what is considered a signature.
13604 If it is a number, no signature may not be longer (in characters) than
13605 that number.  If it is a function, the function will be called without
13606 any parameters, and if it returns nil, there is no signature in the
13607 buffer.  If it is a string, it will be used as a regexp.  If it
13608 matches, the text in question is not a signature.")
13609
13610 (defun gnus-narrow-to-signature ()
13611   "Narrow to the signature."
13612   (widen)
13613   (goto-char (point-max))
13614   (when (re-search-backward gnus-signature-separator nil t)
13615     (forward-line 1)
13616     (when (or (null gnus-signature-limit)
13617               (and (numberp gnus-signature-limit)
13618                    (< (- (point-max) (point)) gnus-signature-limit))
13619               (and (gnus-functionp gnus-signature-limit)
13620                    (funcall gnus-signature-limit))
13621               (and (stringp gnus-signature-limit)
13622                    (not (re-search-forward gnus-signature-limit nil t))))
13623       (narrow-to-region (point) (point-max))
13624       t)))
13625
13626 (defun gnus-article-check-hidden-text (type arg)
13627   "Return nil if hiding is necessary."
13628   (save-excursion
13629     (set-buffer gnus-article-buffer)
13630     (let ((hide (gnus-article-hidden-text-p type)))
13631       (cond ((or (and (null arg) (eq hide 'hidden))
13632                  (and arg (< 0 (prefix-numeric-value arg))))
13633              (gnus-article-show-hidden-text type))
13634             ((eq hide 'shown)
13635              (gnus-article-show-hidden-text type t))
13636             (t nil)))))
13637
13638 (defun gnus-article-hidden-text-p (type)
13639   "Say whether the current buffer contains hidden text of type TYPE."
13640   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13641         prop)
13642     (when pos
13643       (if (get-text-property pos 'invisible)
13644           'hidden
13645         'shown))))
13646
13647 (defun gnus-article-hide (&optional arg force)
13648   "Hide all the gruft in the current article.
13649 This means that PGP stuff, signatures, cited text and (some)
13650 headers will be hidden.
13651 If given a prefix, show the hidden text instead."
13652   (interactive (list current-prefix-arg 'force))
13653   (gnus-article-hide-headers arg)
13654   (gnus-article-hide-pgp arg)
13655   (gnus-article-hide-citation-maybe arg force)
13656   (gnus-article-hide-signature arg))
13657
13658 (defun gnus-article-show-hidden-text (type &optional hide)
13659   "Show all hidden text of type TYPE.
13660 If HIDE, hide the text instead."
13661   (save-excursion
13662     (set-buffer gnus-article-buffer)
13663     (let ((buffer-read-only nil)
13664           (inhibit-point-motion-hooks t)
13665           (beg (point)))
13666       (while (gnus-goto-char (text-property-any
13667                               beg (point-max) 'gnus-type type))
13668         (setq beg (point))
13669         (forward-char)
13670         (if hide
13671             (add-text-properties beg (point) gnus-hidden-properties)
13672           (remove-text-properties beg (point) gnus-hidden-properties))
13673         (setq beg (point)))
13674       t)))
13675
13676 (defvar gnus-article-time-units
13677   `((year . ,(* 365.25 24 60 60))
13678     (week . ,(* 7 24 60 60))
13679     (day . ,(* 24 60 60))
13680     (hour . ,(* 60 60))
13681     (minute . 60)
13682     (second . 1))
13683   "Mapping from time units to seconds.")
13684
13685 (defun gnus-article-date-ut (&optional type highlight)
13686   "Convert DATE date to universal time in the current article.
13687 If TYPE is `local', convert to local time; if it is `lapsed', output
13688 how much time has lapsed since DATE."
13689   (interactive (list 'ut t))
13690   (let* ((header (or gnus-current-headers
13691                      (gnus-summary-article-header) ""))
13692          (date (and (vectorp header) (mail-header-date header)))
13693          (date-regexp "^Date: \\|^X-Sent: ")
13694          (now (current-time))
13695          (inhibit-point-motion-hooks t))
13696     (when (and date (not (string= date "")))
13697       (save-excursion
13698         (set-buffer gnus-article-buffer)
13699         (save-restriction
13700           (nnheader-narrow-to-headers)
13701           (let ((buffer-read-only nil))
13702             ;; Delete any old Date headers.
13703             (if (zerop (nnheader-remove-header date-regexp t))
13704                 (beginning-of-line)
13705               (goto-char (point-max)))
13706             (insert
13707              (cond
13708               ;; Convert to the local timezone.  We have to slap a
13709               ;; `condition-case' round the calls to the timezone
13710               ;; functions since they aren't particularly resistant to
13711               ;; buggy dates.
13712               ((eq type 'local)
13713                (concat "Date: " (condition-case ()
13714                                     (timezone-make-date-arpa-standard date)
13715                                   (error date))
13716                        "\n"))
13717               ;; Convert to Universal Time.
13718               ((eq type 'ut)
13719                (concat "Date: "
13720                        (condition-case ()
13721                            (timezone-make-date-arpa-standard date nil "UT")
13722                          (error date))
13723                        "\n"))
13724               ;; Get the original date from the article.
13725               ((eq type 'original)
13726                (concat "Date: " date "\n"))
13727               ;; Do an X-Sent lapsed format.
13728               ((eq type 'lapsed)
13729                ;; If the date is seriously mangled, the timezone
13730                ;; functions are liable to bug out, so we condition-case
13731                ;; the entire thing.
13732                (let* ((real-time
13733                        (condition-case ()
13734                            (gnus-time-minus
13735                             (gnus-encode-date
13736                              (timezone-make-date-arpa-standard
13737                               (current-time-string now)
13738                               (current-time-zone now) "UT"))
13739                             (gnus-encode-date
13740                              (timezone-make-date-arpa-standard
13741                               date nil "UT")))
13742                          (error '(0 0))))
13743                       (real-sec (+ (* (float (car real-time)) 65536)
13744                                    (cadr real-time)))
13745                       (sec (abs real-sec))
13746                       num prev)
13747                  (if (zerop sec)
13748                      "X-Sent: Now\n"
13749                    (concat
13750                     "X-Sent: "
13751                     ;; This is a bit convoluted, but basically we go
13752                     ;; through the time units for years, weeks, etc,
13753                     ;; and divide things to see whether that results
13754                     ;; in positive answers.
13755                     (mapconcat
13756                      (lambda (unit)
13757                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13758                            ;; The (remaining) seconds are too few to
13759                            ;; be divided into this time unit.
13760                            ""
13761                          ;; It's big enough, so we output it.
13762                          (setq sec (- sec (* num (cdr unit))))
13763                          (prog1
13764                              (concat (if prev ", " "") (int-to-string
13765                                                         (floor num))
13766                                      " " (symbol-name (car unit))
13767                                      (if (> num 1) "s" ""))
13768                            (setq prev t))))
13769                      gnus-article-time-units "")
13770                     ;; If dates are odd, then it might appear like the
13771                     ;; article was sent in the future.
13772                     (if (> real-sec 0)
13773                         " ago\n"
13774                       " in the future\n")))))
13775               (t
13776                (error "Unknown conversion type: %s" type)))))
13777           ;; Do highlighting.
13778           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13779             (gnus-article-highlight-headers)))))))
13780
13781 (defun gnus-article-date-local (&optional highlight)
13782   "Convert the current article date to the local timezone."
13783   (interactive (list t))
13784   (gnus-article-date-ut 'local highlight))
13785
13786 (defun gnus-article-date-original (&optional highlight)
13787   "Convert the current article date to what it was originally.
13788 This is only useful if you have used some other date conversion
13789 function and want to see what the date was before converting."
13790   (interactive (list t))
13791   (gnus-article-date-ut 'original highlight))
13792
13793 (defun gnus-article-date-lapsed (&optional highlight)
13794   "Convert the current article date to time lapsed since it was sent."
13795   (interactive (list t))
13796   (gnus-article-date-ut 'lapsed highlight))
13797
13798 (defun gnus-article-maybe-highlight ()
13799   "Do some article highlighting if `gnus-visual' is non-nil."
13800   (if (gnus-visual-p 'article-highlight 'highlight)
13801       (gnus-article-highlight-some)))
13802
13803 ;; Article savers.
13804
13805 (defun gnus-output-to-rmail (file-name)
13806   "Append the current article to an Rmail file named FILE-NAME."
13807   (require 'rmail)
13808   ;; Most of these codes are borrowed from rmailout.el.
13809   (setq file-name (expand-file-name file-name))
13810   (setq rmail-default-rmail-file file-name)
13811   (let ((artbuf (current-buffer))
13812         (tmpbuf (get-buffer-create " *Gnus-output*")))
13813     (save-excursion
13814       (or (get-file-buffer file-name)
13815           (file-exists-p file-name)
13816           (if (gnus-yes-or-no-p
13817                (concat "\"" file-name "\" does not exist, create it? "))
13818               (let ((file-buffer (create-file-buffer file-name)))
13819                 (save-excursion
13820                   (set-buffer file-buffer)
13821                   (rmail-insert-rmail-file-header)
13822                   (let ((require-final-newline nil))
13823                     (write-region (point-min) (point-max) file-name t 1)))
13824                 (kill-buffer file-buffer))
13825             (error "Output file does not exist")))
13826       (set-buffer tmpbuf)
13827       (buffer-disable-undo (current-buffer))
13828       (erase-buffer)
13829       (insert-buffer-substring artbuf)
13830       (gnus-convert-article-to-rmail)
13831       ;; Decide whether to append to a file or to an Emacs buffer.
13832       (let ((outbuf (get-file-buffer file-name)))
13833         (if (not outbuf)
13834             (append-to-file (point-min) (point-max) file-name)
13835           ;; File has been visited, in buffer OUTBUF.
13836           (set-buffer outbuf)
13837           (let ((buffer-read-only nil)
13838                 (msg (and (boundp 'rmail-current-message)
13839                           (symbol-value 'rmail-current-message))))
13840             ;; If MSG is non-nil, buffer is in RMAIL mode.
13841             (if msg
13842                 (progn (widen)
13843                        (narrow-to-region (point-max) (point-max))))
13844             (insert-buffer-substring tmpbuf)
13845             (if msg
13846                 (progn
13847                   (goto-char (point-min))
13848                   (widen)
13849                   (search-backward "\^_")
13850                   (narrow-to-region (point) (point-max))
13851                   (goto-char (1+ (point-min)))
13852                   (rmail-count-new-messages t)
13853                   (rmail-show-message msg)))))))
13854     (kill-buffer tmpbuf)))
13855
13856 (defun gnus-output-to-file (file-name)
13857   "Append the current article to a file named FILE-NAME."
13858   (setq file-name (expand-file-name file-name))
13859   (let ((artbuf (current-buffer))
13860         (tmpbuf (get-buffer-create " *Gnus-output*")))
13861     (save-excursion
13862       (set-buffer tmpbuf)
13863       (buffer-disable-undo (current-buffer))
13864       (erase-buffer)
13865       (insert-buffer-substring artbuf)
13866       ;; Append newline at end of the buffer as separator, and then
13867       ;; save it to file.
13868       (goto-char (point-max))
13869       (insert "\n")
13870       (append-to-file (point-min) (point-max) file-name))
13871     (kill-buffer tmpbuf)))
13872
13873 (defun gnus-convert-article-to-rmail ()
13874   "Convert article in current buffer to Rmail message format."
13875   (let ((buffer-read-only nil))
13876     ;; Convert article directly into Babyl format.
13877     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
13878     (goto-char (point-min))
13879     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
13880     (while (search-forward "\n\^_" nil t) ;single char
13881       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
13882     (goto-char (point-max))
13883     (insert "\^_")))
13884
13885 (defun gnus-narrow-to-page (&optional arg)
13886   "Narrow the article buffer to a page.
13887 If given a numerical ARG, move forward ARG pages."
13888   (interactive "P")
13889   (setq arg (if arg (prefix-numeric-value arg) 0))
13890   (save-excursion
13891     (set-buffer gnus-article-buffer)
13892     (goto-char (point-min))
13893     (widen)
13894     (when (gnus-visual-p 'page-marker)
13895       (let ((buffer-read-only nil))
13896         (gnus-remove-text-with-property 'gnus-prev)
13897         (gnus-remove-text-with-property 'gnus-next)))
13898     (when
13899         (cond ((< arg 0)
13900                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
13901               ((> arg 0)
13902                (re-search-forward page-delimiter nil 'move arg)))
13903       (goto-char (match-end 0)))
13904     (narrow-to-region
13905      (point)
13906      (if (re-search-forward page-delimiter nil 'move)
13907          (match-beginning 0)
13908        (point)))
13909     (when (and (gnus-visual-p 'page-marker)
13910                (not (= (point-min) 1)))
13911       (save-excursion
13912         (goto-char (point-min))
13913         (gnus-insert-prev-page-button)))
13914     (when (and (gnus-visual-p 'page-marker)
13915                (not (= (1- (point-max)) (buffer-size))))
13916       (save-excursion
13917         (goto-char (point-max))
13918         (gnus-insert-next-page-button)))))
13919
13920
13921 ;; Article mode commands
13922
13923 (defun gnus-article-goto-next-page ()
13924   "Show the next page of the article."
13925   (interactive)
13926   (when (gnus-article-next-page)
13927     (gnus-article-read-summary-keys nil ?n)))
13928
13929 (defun gnus-article-goto-prev-page ()
13930   "Show the next page of the article."
13931   (interactive)
13932   (if (bobp) (gnus-article-read-summary-keys nil ?n)
13933     (gnus-article-prev-page nil)))
13934
13935 (defun gnus-article-next-page (&optional lines)
13936   "Show the next page of the current article.
13937 If end of article, return non-nil.  Otherwise return nil.
13938 Argument LINES specifies lines to be scrolled up."
13939   (interactive "p")
13940   (move-to-window-line -1)
13941   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
13942   (if (save-excursion
13943         (end-of-line)
13944         (and (pos-visible-in-window-p)  ;Not continuation line.
13945              (eobp)))
13946       ;; Nothing in this page.
13947       (if (or (not gnus-break-pages)
13948               (save-excursion
13949                 (save-restriction
13950                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
13951           t                             ;Nothing more.
13952         (gnus-narrow-to-page 1)         ;Go to next page.
13953         nil)
13954     ;; More in this page.
13955     (condition-case ()
13956         (scroll-up lines)
13957       (end-of-buffer
13958        ;; Long lines may cause an end-of-buffer error.
13959        (goto-char (point-max))))
13960     nil))
13961
13962 (defun gnus-article-prev-page (&optional lines)
13963   "Show previous page of current article.
13964 Argument LINES specifies lines to be scrolled down."
13965   (interactive "p")
13966   (move-to-window-line 0)
13967   (if (and gnus-break-pages
13968            (bobp)
13969            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
13970       (progn
13971         (gnus-narrow-to-page -1)        ;Go to previous page.
13972         (goto-char (point-max))
13973         (recenter -1))
13974     (condition-case ()
13975         (scroll-down lines)
13976       (error nil))))
13977
13978 (defun gnus-article-refer-article ()
13979   "Read article specified by message-id around point."
13980   (interactive)
13981   (let ((point (point)))
13982     (search-forward ">" nil t)          ;Move point to end of "<....>".
13983     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
13984         (let ((message-id (match-string 1)))
13985           (goto-char point)
13986           (set-buffer gnus-summary-buffer)
13987           (gnus-summary-refer-article message-id))
13988       (goto-char (point))
13989       (error "No references around point"))))
13990
13991 (defun gnus-article-show-summary ()
13992   "Reconfigure windows to show summary buffer."
13993   (interactive)
13994   (gnus-configure-windows 'article)
13995   (gnus-summary-goto-subject gnus-current-article))
13996
13997 (defun gnus-article-describe-briefly ()
13998   "Describe article mode commands briefly."
13999   (interactive)
14000   (gnus-message 6
14001                 (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")))
14002
14003 (defun gnus-article-summary-command ()
14004   "Execute the last keystroke in the summary buffer."
14005   (interactive)
14006   (let ((obuf (current-buffer))
14007         (owin (current-window-configuration))
14008         func)
14009     (switch-to-buffer gnus-summary-buffer 'norecord)
14010     (setq func (lookup-key (current-local-map) (this-command-keys)))
14011     (call-interactively func)
14012     (set-buffer obuf)
14013     (set-window-configuration owin)
14014     (set-window-point (get-buffer-window (current-buffer)) (point))))
14015
14016 (defun gnus-article-summary-command-nosave ()
14017   "Execute the last keystroke in the summary buffer."
14018   (interactive)
14019   (let (func)
14020     (pop-to-buffer gnus-summary-buffer 'norecord)
14021     (setq func (lookup-key (current-local-map) (this-command-keys)))
14022     (call-interactively func)))
14023
14024 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14025   "Read a summary buffer key sequence and execute it from the article buffer."
14026   (interactive "P")
14027   (let ((nosaves
14028          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14029            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14030            "=" "^" "\M-^"))
14031         keys)
14032     (save-excursion
14033       (set-buffer gnus-summary-buffer)
14034       (push (or key last-command-event) unread-command-events)
14035       (setq keys (read-key-sequence nil)))
14036     (message "")
14037
14038     (if (member keys nosaves)
14039         (let (func)
14040           (pop-to-buffer gnus-summary-buffer 'norecord)
14041           (if (setq func (lookup-key (current-local-map) keys))
14042               (call-interactively func)
14043             (ding)))
14044       (let ((obuf (current-buffer))
14045             (owin (current-window-configuration))
14046             (opoint (point))
14047             func in-buffer)
14048         (if not-restore-window
14049             (pop-to-buffer gnus-summary-buffer 'norecord)
14050           (switch-to-buffer gnus-summary-buffer 'norecord))
14051         (setq in-buffer (current-buffer))
14052         (if (setq func (lookup-key (current-local-map) keys))
14053             (call-interactively func)
14054           (ding))
14055         (when (eq in-buffer (current-buffer))
14056           (set-buffer obuf)
14057           (unless not-restore-window
14058             (set-window-configuration owin))
14059           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14060
14061 \f
14062 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
14063
14064 ;;;###autoload
14065 (defalias 'gnus-batch-kill 'gnus-batch-score)
14066 ;;;###autoload
14067 (defun gnus-batch-score ()
14068   "Run batched scoring.
14069 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14070 Newsgroups is a list of strings in Bnews format.  If you want to score
14071 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14072 score the alt hierarchy, you'd say \"!alt.all\"."
14073   (interactive)
14074   (let* ((yes-and-no
14075           (gnus-newsrc-parse-options
14076            (apply (function concat)
14077                   (mapcar (lambda (g) (concat g " "))
14078                           command-line-args-left))))
14079          (gnus-expert-user t)
14080          (nnmail-spool-file nil)
14081          (gnus-use-dribble-file nil)
14082          (yes (car yes-and-no))
14083          (no (cdr yes-and-no))
14084          group newsrc entry
14085          ;; Disable verbose message.
14086          gnus-novice-user gnus-large-newsgroup)
14087     ;; Eat all arguments.
14088     (setq command-line-args-left nil)
14089     ;; Start Gnus.
14090     (gnus)
14091     ;; Apply kills to specified newsgroups in command line arguments.
14092     (setq newsrc (cdr gnus-newsrc-alist))
14093     (while newsrc
14094       (setq group (car (car newsrc)))
14095       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14096       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14097                (and (car entry)
14098                     (or (eq (car entry) t)
14099                         (not (zerop (car entry)))))
14100                (if yes (string-match yes group) t)
14101                (or (null no) (not (string-match no group))))
14102           (progn
14103             (gnus-summary-read-group group nil t nil t)
14104             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14105                  (gnus-summary-exit))))
14106       (setq newsrc (cdr newsrc)))
14107     ;; Exit Emacs.
14108     (switch-to-buffer gnus-group-buffer)
14109     (gnus-group-save-newsrc)))
14110
14111 (defun gnus-apply-kill-file ()
14112   "Apply a kill file to the current newsgroup.
14113 Returns the number of articles marked as read."
14114   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14115           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14116       (gnus-apply-kill-file-internal)
14117     0))
14118
14119 (defun gnus-kill-save-kill-buffer ()
14120   (save-excursion
14121     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14122       (if (get-file-buffer file)
14123           (progn
14124             (set-buffer (get-file-buffer file))
14125             (and (buffer-modified-p) (save-buffer))
14126             (kill-buffer (current-buffer)))))))
14127
14128 (defvar gnus-kill-file-name "KILL"
14129   "Suffix of the kill files.")
14130
14131 (defun gnus-newsgroup-kill-file (newsgroup)
14132   "Return the name of a kill file name for NEWSGROUP.
14133 If NEWSGROUP is nil, return the global kill file name instead."
14134   (cond ((or (null newsgroup)
14135              (string-equal newsgroup ""))
14136          ;; The global KILL file is placed at top of the directory.
14137          (expand-file-name gnus-kill-file-name
14138                            (or gnus-kill-files-directory "~/News")))
14139         ((gnus-use-long-file-name 'not-kill)
14140          ;; Append ".KILL" to newsgroup name.
14141          (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14142                                    "." gnus-kill-file-name)
14143                            (or gnus-kill-files-directory "~/News")))
14144         (t
14145          ;; Place "KILL" under the hierarchical directory.
14146          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14147                                    "/" gnus-kill-file-name)
14148                            (or gnus-kill-files-directory "~/News")))))
14149
14150 \f
14151 ;;;
14152 ;;; Dribble file
14153 ;;;
14154
14155 (defvar gnus-dribble-ignore nil)
14156 (defvar gnus-dribble-eval-file nil)
14157
14158 (defun gnus-dribble-file-name ()
14159   "Return the dribble file for the current .newsrc."
14160   (concat
14161    (if gnus-dribble-directory
14162        (concat (file-name-as-directory gnus-dribble-directory)
14163                (file-name-nondirectory gnus-current-startup-file))
14164      gnus-current-startup-file)
14165    "-dribble"))
14166
14167 (defun gnus-dribble-enter (string)
14168   "Enter STRING into the dribble buffer."
14169   (if (and (not gnus-dribble-ignore)
14170            gnus-dribble-buffer
14171            (buffer-name gnus-dribble-buffer))
14172       (let ((obuf (current-buffer)))
14173         (set-buffer gnus-dribble-buffer)
14174         (insert string "\n")
14175         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14176         (set-buffer obuf))))
14177
14178 (defun gnus-dribble-read-file ()
14179   "Read the dribble file from disk."
14180   (let ((dribble-file (gnus-dribble-file-name)))
14181     (save-excursion
14182       (set-buffer (setq gnus-dribble-buffer
14183                         (get-buffer-create
14184                          (file-name-nondirectory dribble-file))))
14185       (gnus-add-current-to-buffer-list)
14186       (erase-buffer)
14187       (setq buffer-file-name dribble-file)
14188       (auto-save-mode t)
14189       (buffer-disable-undo (current-buffer))
14190       (bury-buffer (current-buffer))
14191       (set-buffer-modified-p nil)
14192       (let ((auto (make-auto-save-file-name))
14193             (gnus-dribble-ignore t))
14194         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14195           ;; Load whichever file is newest -- the auto save file
14196           ;; or the "real" file.
14197           (if (file-newer-than-file-p auto dribble-file)
14198               (insert-file-contents auto)
14199             (insert-file-contents dribble-file))
14200           (unless (zerop (buffer-size))
14201             (set-buffer-modified-p t))
14202           ;; Set the file modes to reflect the .newsrc file modes.
14203           (save-buffer)
14204           (when (file-exists-p gnus-current-startup-file)
14205             (set-file-modes dribble-file
14206                             (file-modes gnus-current-startup-file)))
14207           ;; Possibly eval the file later.
14208           (when (gnus-y-or-n-p
14209                  "Auto-save file exists.  Do you want to read it? ")
14210             (setq gnus-dribble-eval-file t)))))))
14211
14212 (defun gnus-dribble-eval-file ()
14213   (if (not gnus-dribble-eval-file)
14214       ()
14215     (setq gnus-dribble-eval-file nil)
14216     (save-excursion
14217       (let ((gnus-dribble-ignore t))
14218         (set-buffer gnus-dribble-buffer)
14219         (eval-buffer (current-buffer))))))
14220
14221 (defun gnus-dribble-delete-file ()
14222   (if (file-exists-p (gnus-dribble-file-name))
14223       (delete-file (gnus-dribble-file-name)))
14224   (if gnus-dribble-buffer
14225       (save-excursion
14226         (set-buffer gnus-dribble-buffer)
14227         (let ((auto (make-auto-save-file-name)))
14228           (if (file-exists-p auto)
14229               (delete-file auto))
14230           (erase-buffer)
14231           (set-buffer-modified-p nil)))))
14232
14233 (defun gnus-dribble-save ()
14234   (if (and gnus-dribble-buffer
14235            (buffer-name gnus-dribble-buffer))
14236       (save-excursion
14237         (set-buffer gnus-dribble-buffer)
14238         (save-buffer))))
14239
14240 (defun gnus-dribble-clear ()
14241   (save-excursion
14242     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14243         (progn
14244           (set-buffer gnus-dribble-buffer)
14245           (erase-buffer)
14246           (set-buffer-modified-p nil)
14247           (setq buffer-saved-size (buffer-size))))))
14248
14249 ;;;
14250 ;;; Server Communication
14251 ;;;
14252
14253 (defun gnus-start-news-server (&optional confirm)
14254   "Open a method for getting news.
14255 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14256   (let (how)
14257     (if gnus-current-select-method
14258         ;; Stream is already opened.
14259         nil
14260       ;; Open NNTP server.
14261       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14262       (if confirm
14263           (progn
14264             ;; Read server name with completion.
14265             (setq gnus-nntp-server
14266                   (completing-read "NNTP server: "
14267                                    (mapcar (lambda (server) (list server))
14268                                            (cons (list gnus-nntp-server)
14269                                                  gnus-secondary-servers))
14270                                    nil nil gnus-nntp-server))))
14271
14272       (if (and gnus-nntp-server
14273                (stringp gnus-nntp-server)
14274                (not (string= gnus-nntp-server "")))
14275           (setq gnus-select-method
14276                 (cond ((or (string= gnus-nntp-server "")
14277                            (string= gnus-nntp-server "::"))
14278                        (list 'nnspool (system-name)))
14279                       ((string-match "^:" gnus-nntp-server)
14280                        (list 'nnmh gnus-nntp-server
14281                              (list 'nnmh-directory
14282                                    (file-name-as-directory
14283                                     (expand-file-name
14284                                      (concat "~/" (substring
14285                                                    gnus-nntp-server 1)))))
14286                              (list 'nnmh-get-new-mail nil)))
14287                       (t
14288                        (list 'nntp gnus-nntp-server)))))
14289
14290       (setq how (car gnus-select-method))
14291       (cond ((eq how 'nnspool)
14292              (require 'nnspool)
14293              (gnus-message 5 "Looking up local news spool..."))
14294             ((eq how 'nnmh)
14295              (require 'nnmh)
14296              (gnus-message 5 "Looking up mh spool..."))
14297             (t
14298              (require 'nntp)))
14299       (setq gnus-current-select-method gnus-select-method)
14300       (run-hooks 'gnus-open-server-hook)
14301       (or
14302        ;; gnus-open-server-hook might have opened it
14303        (gnus-server-opened gnus-select-method)
14304        (gnus-open-server gnus-select-method)
14305        (gnus-y-or-n-p
14306         (format
14307          "%s (%s) open error: '%s'.     Continue? "
14308          (car gnus-select-method) (cadr gnus-select-method)
14309          (gnus-status-message gnus-select-method)))
14310        (progn
14311          (gnus-message 1 "Couldn't open server on %s"
14312                        (nth 1 gnus-select-method))
14313          (ding)
14314          nil)))))
14315
14316 (defun gnus-check-group (group)
14317   "Try to make sure that the server where GROUP exists is alive."
14318   (let ((method (gnus-find-method-for-group group)))
14319     (or (gnus-server-opened method)
14320         (gnus-open-server method))))
14321
14322 (defun gnus-check-server (&optional method)
14323   "Check whether the connection to METHOD is down.
14324 If METHOD is nil, use `gnus-select-method'.
14325 If it is down, start it up (again)."
14326   (let ((method (or method gnus-select-method)))
14327     ;; Transform virtual server names into select methods.
14328     (when (stringp method)
14329       (setq method (gnus-server-to-method method)))
14330     (if (gnus-server-opened method)
14331         ;; The stream is already opened.
14332         t
14333       ;; Open the server.
14334       (gnus-message 5 "Opening %s server on %s..." (car method) (nth 1 method))
14335       (run-hooks 'gnus-open-server-hook)
14336       (prog1
14337           (gnus-open-server method)
14338         (message "")))))
14339
14340 (defun gnus-get-function (method function)
14341   "Return a function symbol based on METHOD and FUNCTION."
14342   ;; Translate server names into methods.
14343   (unless method
14344     (error "Attempted use of a nil select method"))
14345   (when (stringp method)
14346     (setq method (gnus-server-to-method method)))
14347   (let ((func (intern (format "%s-%s" (car method) function))))
14348     ;; If the functions isn't bound, we require the backend in
14349     ;; question.
14350     (unless (fboundp func)
14351       (require (car method))
14352       (unless (fboundp func)
14353         ;; This backend doesn't implement this function.
14354         (error "No such function: %s" func)))
14355     func))
14356
14357 ;;; Interface functions to the backends.
14358
14359 (defun gnus-open-server (method)
14360   "Open a connection to METHOD."
14361   (let ((elem (assoc method gnus-opened-servers)))
14362     ;; If this method was previously denied, we just return nil.
14363     (if (eq (nth 1 elem) 'denied)
14364         (progn
14365           (gnus-message 1 "Denied server")
14366           nil)
14367       ;; Open the server.
14368       (let ((result
14369              (funcall (gnus-get-function method 'open-server)
14370                       (nth 1 method) (nthcdr 2 method))))
14371         ;; If this hasn't been opened before, we add it to the list.
14372         (unless elem
14373           (setq elem (list method nil)
14374                 gnus-opened-servers (cons elem gnus-opened-servers)))
14375         ;; Set the status of this server.
14376         (setcar (cdr elem) (if result 'ok 'denied))
14377         ;; Return the result from the "open" call.
14378         result))))
14379
14380 (defun gnus-close-server (method)
14381   "Close the connection to METHOD."
14382   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14383
14384 (defun gnus-request-list (method)
14385   "Request the active file from METHOD."
14386   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14387
14388 (defun gnus-request-list-newsgroups (method)
14389   "Request the newsgroups file from METHOD."
14390   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14391
14392 (defun gnus-request-newgroups (date method)
14393   "Request all new groups since DATE from METHOD."
14394   (funcall (gnus-get-function method 'request-newgroups)
14395            date (nth 1 method)))
14396
14397 (defun gnus-server-opened (method)
14398   "Check whether a connection to METHOD has been opened."
14399   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14400
14401 (defun gnus-status-message (method)
14402   "Return the status message from METHOD.
14403 If METHOD is a string, it is interpreted as a group name.   The method
14404 this group uses will be queried."
14405   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14406                   method)))
14407     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14408
14409 (defun gnus-request-group (group &optional dont-check method)
14410   "Request GROUP.  If DONT-CHECK, no information is required."
14411   (let ((method (or method (gnus-find-method-for-group group))))
14412     (funcall (gnus-get-function method 'request-group)
14413              (gnus-group-real-name group) (nth 1 method) dont-check)))
14414
14415 (defun gnus-request-asynchronous (group &optional articles)
14416   "Request that GROUP behave asynchronously.
14417 ARTICLES is the `data' of the group."
14418   (let ((method (gnus-find-method-for-group group)))
14419     (funcall (gnus-get-function method 'request-asynchronous)
14420              (gnus-group-real-name group) (nth 1 method) articles)))
14421
14422 (defun gnus-list-active-group (group)
14423   "Request active information on GROUP."
14424   (let ((method (gnus-find-method-for-group group))
14425         (func 'list-active-group))
14426     (when (gnus-check-backend-function func group)
14427       (funcall (gnus-get-function method func)
14428                (gnus-group-real-name group) (nth 1 method)))))
14429
14430 (defun gnus-request-group-description (group)
14431   "Request a description of GROUP."
14432   (let ((method (gnus-find-method-for-group group))
14433         (func 'request-group-description))
14434     (when (gnus-check-backend-function func group)
14435       (funcall (gnus-get-function method func)
14436                (gnus-group-real-name group) (nth 1 method)))))
14437
14438 (defun gnus-close-group (group)
14439   "Request the GROUP be closed."
14440   (let ((method (gnus-find-method-for-group group)))
14441     (funcall (gnus-get-function method 'close-group)
14442              (gnus-group-real-name group) (nth 1 method))))
14443
14444 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14445   "Request headers for ARTICLES in GROUP.
14446 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14447   (let ((method (gnus-find-method-for-group group)))
14448     (if (and gnus-use-cache (numberp (car articles)))
14449         (gnus-cache-retrieve-headers articles group fetch-old)
14450       (funcall (gnus-get-function method 'retrieve-headers)
14451                articles (gnus-group-real-name group) (nth 1 method)
14452                fetch-old))))
14453
14454 (defun gnus-retrieve-groups (groups method)
14455   "Request active information on GROUPS from METHOD."
14456   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14457
14458 (defun gnus-request-type (group &optional article)
14459   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14460   (let ((method (gnus-find-method-for-group group)))
14461     (if (not (gnus-check-backend-function 'request-type (car method)))
14462         'unknown
14463       (funcall (gnus-get-function method 'request-type)
14464                (gnus-group-real-name group) article))))
14465
14466 (defun gnus-request-update-mark (group article mark)
14467   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14468   (let ((method (gnus-find-method-for-group group)))
14469     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14470         mark
14471       (funcall (gnus-get-function method 'request-update-mark)
14472                (gnus-group-real-name group) article mark))))
14473
14474 (defun gnus-request-article (article group &optional buffer)
14475   "Request the ARTICLE in GROUP.
14476 ARTICLE can either be an article number or an article Message-ID.
14477 If BUFFER, insert the article in that group."
14478   (let ((method (gnus-find-method-for-group group)))
14479     (funcall (gnus-get-function method 'request-article)
14480              article (gnus-group-real-name group) (nth 1 method) buffer)))
14481
14482 (defun gnus-request-head (article group)
14483   "Request the head of ARTICLE in GROUP."
14484   (let ((method (gnus-find-method-for-group group)))
14485     (funcall (gnus-get-function method 'request-head)
14486              article (gnus-group-real-name group) (nth 1 method))))
14487
14488 (defun gnus-request-body (article group)
14489   "Request the body of ARTICLE in GROUP."
14490   (let ((method (gnus-find-method-for-group group)))
14491     (funcall (gnus-get-function method 'request-body)
14492              article (gnus-group-real-name group) (nth 1 method))))
14493
14494 (defun gnus-request-post (method)
14495   "Post the current buffer using METHOD."
14496   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14497
14498 (defun gnus-request-scan (group method)
14499   "Request a SCAN being performed in GROUP from METHOD.
14500 If GROUP is nil, all groups on METHOD are scanned."
14501   (let ((method (if group (gnus-find-method-for-group group) method)))
14502     (funcall (gnus-get-function method 'request-scan)
14503              (and group (gnus-group-real-name group)) (nth 1 method))))
14504
14505 (defsubst gnus-request-update-info (info method)
14506   "Request that METHOD update INFO."
14507   (when (gnus-check-backend-function 'request-update-info (car method))
14508     (funcall (gnus-get-function method 'request-update-info)
14509              (gnus-group-real-name (gnus-info-group info))
14510              info (nth 1 method))))
14511
14512 (defun gnus-request-expire-articles (articles group &optional force)
14513   (let ((method (gnus-find-method-for-group group)))
14514     (funcall (gnus-get-function method 'request-expire-articles)
14515              articles (gnus-group-real-name group) (nth 1 method)
14516              force)))
14517
14518 (defun gnus-request-move-article
14519   (article group server accept-function &optional last)
14520   (let ((method (gnus-find-method-for-group group)))
14521     (funcall (gnus-get-function method 'request-move-article)
14522              article (gnus-group-real-name group)
14523              (nth 1 method) accept-function last)))
14524
14525 (defun gnus-request-accept-article (group &optional last method)
14526   ;; Make sure there's a newline at the end of the article.
14527   (goto-char (point-max))
14528   (unless (bolp)
14529     (insert "\n"))
14530   (let ((func (if (symbolp group) group
14531                 (car (or method (gnus-find-method-for-group group))))))
14532     (funcall (intern (format "%s-request-accept-article" func))
14533              (if (stringp group) (gnus-group-real-name group) group)
14534              last)))
14535
14536 (defun gnus-request-replace-article (article group buffer)
14537   (let ((func (car (gnus-find-method-for-group group))))
14538     (funcall (intern (format "%s-request-replace-article" func))
14539              article (gnus-group-real-name group) buffer)))
14540
14541 (defun gnus-request-associate-buffer (group)
14542   (let ((method (gnus-find-method-for-group group)))
14543     (funcall (gnus-get-function method 'request-associate-buffer)
14544              (gnus-group-real-name group))))
14545
14546 (defun gnus-request-restore-buffer (article group)
14547   "Request a new buffer restored to the state of ARTICLE."
14548   (let ((method (gnus-find-method-for-group group)))
14549     (funcall (gnus-get-function method 'request-restore-buffer)
14550              article (gnus-group-real-name group) (nth 1 method))))
14551
14552 (defun gnus-request-create-group (group &optional method)
14553   (let ((method (or method (gnus-find-method-for-group group))))
14554     (funcall (gnus-get-function method 'request-create-group)
14555              (gnus-group-real-name group) (nth 1 method))))
14556
14557 (defun gnus-request-delete-group (group &optional force)
14558   (let ((method (gnus-find-method-for-group group)))
14559     (funcall (gnus-get-function method 'request-delete-group)
14560              (gnus-group-real-name group) force (nth 1 method))))
14561
14562 (defun gnus-request-rename-group (group new-name)
14563   (let ((method (gnus-find-method-for-group group)))
14564     (funcall (gnus-get-function method 'request-rename-group)
14565              (gnus-group-real-name group)
14566              (gnus-group-real-name new-name) (nth 1 method))))
14567
14568 (defun gnus-member-of-valid (symbol group)
14569   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14570   (memq symbol (assoc
14571                 (symbol-name (car (gnus-find-method-for-group group)))
14572                 gnus-valid-select-methods)))
14573
14574 (defun gnus-method-option-p (method option)
14575   "Return non-nil if select METHOD has OPTION as a parameter."
14576   (memq option (assoc (format "%s" (car method))
14577                       gnus-valid-select-methods)))
14578
14579 (defun gnus-server-extend-method (group method)
14580   ;; This function "extends" a virtual server.  If the server is
14581   ;; "hello", and the select method is ("hello" (my-var "something"))
14582   ;; in the group "alt.alt", this will result in a new virtual server
14583   ;; called "hello+alt.alt".
14584   (let ((entry
14585          (gnus-copy-sequence
14586           (if (equal (car method) "native") gnus-select-method
14587             (cdr (assoc (car method) gnus-server-alist))))))
14588     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14589     (nconc entry (cdr method))))
14590
14591 (defun gnus-find-method-for-group (group &optional info)
14592   "Find the select method that GROUP uses."
14593   (or gnus-override-method
14594       (and (not group)
14595            gnus-select-method)
14596       (let ((info (or info (gnus-get-info group)))
14597             method)
14598         (if (or (not info)
14599                 (not (setq method (gnus-info-method info))))
14600             (setq method gnus-select-method)
14601           (setq method
14602                 (cond ((stringp method)
14603                        (gnus-server-to-method method))
14604                       ((stringp (car method))
14605                        (gnus-server-extend-method group method))
14606                       (t
14607                        method))))
14608         (gnus-server-add-address method))))
14609
14610 (defun gnus-check-backend-function (func group)
14611   "Check whether GROUP supports function FUNC."
14612   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14613                   group)))
14614     (fboundp (intern (format "%s-%s" method func)))))
14615
14616 (defun gnus-methods-using (feature)
14617   "Find all methods that have FEATURE."
14618   (let ((valids gnus-valid-select-methods)
14619         outs)
14620     (while valids
14621       (if (memq feature (car valids))
14622           (setq outs (cons (car valids) outs)))
14623       (setq valids (cdr valids)))
14624     outs))
14625
14626 ;;;
14627 ;;; Active & Newsrc File Handling
14628 ;;;
14629
14630 (defun gnus-setup-news (&optional rawfile level)
14631   "Setup news information.
14632 If RAWFILE is non-nil, the .newsrc file will also be read.
14633 If LEVEL is non-nil, the news will be set up at level LEVEL."
14634   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14635     ;; Clear some variables to re-initialize news information.
14636     (if init (setq gnus-newsrc-alist nil
14637                    gnus-active-hashtb nil))
14638
14639     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14640     (if init (gnus-read-newsrc-file rawfile))
14641
14642     ;; If we don't read the complete active file, we fill in the
14643     ;; hashtb here.
14644     (if (or (null gnus-read-active-file)
14645             (eq gnus-read-active-file 'some))
14646         (gnus-update-active-hashtb-from-killed))
14647
14648     ;; Read the active file and create `gnus-active-hashtb'.
14649     ;; If `gnus-read-active-file' is nil, then we just create an empty
14650     ;; hash table.  The partial filling out of the hash table will be
14651     ;; done in `gnus-get-unread-articles'.
14652     (and gnus-read-active-file
14653          (not level)
14654          (gnus-read-active-file))
14655
14656     (or gnus-active-hashtb
14657         (setq gnus-active-hashtb (make-vector 4095 0)))
14658
14659     ;; Initialize the cache.
14660     (when gnus-use-cache
14661       (gnus-cache-open))
14662
14663     ;; Possibly eval the dribble file.
14664     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14665
14666     (gnus-update-format-specifications)
14667
14668     ;; Find new newsgroups and treat them.
14669     (if (and init gnus-check-new-newsgroups (not level)
14670              (gnus-check-server gnus-select-method))
14671         (gnus-find-new-newsgroups))
14672
14673     ;; Find the number of unread articles in each non-dead group.
14674     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14675       (gnus-get-unread-articles level))
14676
14677     (if (and init gnus-check-bogus-newsgroups
14678              gnus-read-active-file (not level)
14679              (gnus-server-opened gnus-select-method))
14680         (gnus-check-bogus-newsgroups))))
14681
14682 (defun gnus-find-new-newsgroups (&optional arg)
14683   "Search for new newsgroups and add them.
14684 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14685 The `-n' option line from .newsrc is respected.
14686 If ARG (the prefix), use the `ask-server' method to query
14687 the server for new groups."
14688   (interactive "P")
14689   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14690                        (null gnus-read-active-file)
14691                        (eq gnus-read-active-file 'some))
14692                    'ask-server gnus-check-new-newsgroups)))
14693     (unless (gnus-check-first-time-used)
14694       (if (or (consp check)
14695               (eq check 'ask-server))
14696           (gnus-ask-server-for-new-groups)
14697         (let ((groups 0)
14698               group new-newsgroups)
14699           (gnus-message 5 "Looking for new newsgroups...")
14700           (or gnus-have-read-active-file (gnus-read-active-file))
14701           (setq gnus-newsrc-last-checked-date (current-time-string))
14702           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
14703           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14704           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14705           (mapatoms
14706            (lambda (sym)
14707              (if (or (null (setq group (symbol-name sym)))
14708                      (not (boundp sym))
14709                      (null (symbol-value sym))
14710                      (gnus-gethash group gnus-killed-hashtb)
14711                      (gnus-gethash group gnus-newsrc-hashtb))
14712                  ()
14713                (let ((do-sub (gnus-matches-options-n group)))
14714                  (cond
14715                   ((eq do-sub 'subscribe)
14716                    (setq groups (1+ groups))
14717                    (gnus-sethash group group gnus-killed-hashtb)
14718                    (funcall gnus-subscribe-options-newsgroup-method group))
14719                   ((eq do-sub 'ignore)
14720                    nil)
14721                   (t
14722                    (setq groups (1+ groups))
14723                    (gnus-sethash group group gnus-killed-hashtb)
14724                    (if gnus-subscribe-hierarchical-interactive
14725                        (setq new-newsgroups (cons group new-newsgroups))
14726                      (funcall gnus-subscribe-newsgroup-method group)))))))
14727            gnus-active-hashtb)
14728           (if new-newsgroups
14729               (gnus-subscribe-hierarchical-interactive new-newsgroups))
14730           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14731           (if (> groups 0)
14732               (gnus-message 6 "%d new newsgroup%s arrived."
14733                             groups (if (> groups 1) "s have" " has"))
14734             (gnus-message 6 "No new newsgroups.")))))))
14735
14736 (defun gnus-matches-options-n (group)
14737   ;; Returns `subscribe' if the group is to be unconditionally
14738   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14739   ;; no match for the group.
14740
14741   ;; First we check the two user variables.
14742   (cond
14743    ((and gnus-options-subscribe
14744          (string-match gnus-options-subscribe group))
14745     'subscribe)
14746    ((and gnus-auto-subscribed-groups
14747          (string-match gnus-auto-subscribed-groups group))
14748     'subscribe)
14749    ((and gnus-options-not-subscribe
14750          (string-match gnus-options-not-subscribe group))
14751     'ignore)
14752    ;; Then we go through the list that was retrieved from the .newsrc
14753    ;; file.  This list has elements on the form
14754    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14755    ;; is in the reverse order of the options line) is returned.
14756    (t
14757     (let ((regs gnus-newsrc-options-n))
14758       (while (and regs
14759                   (not (string-match (car (car regs)) group)))
14760         (setq regs (cdr regs)))
14761       (and regs (cdr (car regs)))))))
14762
14763 (defun gnus-ask-server-for-new-groups ()
14764   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14765          (methods (cons gnus-select-method
14766                         (cons
14767                          gnus-message-archive-method
14768                          (append
14769                           (and (consp gnus-check-new-newsgroups)
14770                                gnus-check-new-newsgroups)
14771                           gnus-secondary-select-methods))))
14772          (groups 0)
14773          (new-date (current-time-string))
14774          group new-newsgroups got-new method hashtb
14775          gnus-override-subscribe-method)
14776     ;; Go through both primary and secondary select methods and
14777     ;; request new newsgroups.
14778     (while (setq method (gnus-server-get-method nil (pop methods)))
14779       (setq gnus-override-subscribe-method method)
14780       (when (and (gnus-check-server method)
14781                  (gnus-request-newgroups date method))
14782         (save-excursion
14783           (setq got-new t)
14784           (setq hashtb (gnus-make-hashtable 100))
14785           (set-buffer nntp-server-buffer)
14786           ;; Enter all the new groups into a hashtable.
14787           (gnus-active-to-gnus-format method hashtb 'ignore)))
14788       ;; Now all new groups from `method' are in `hashtb'.
14789       (mapatoms
14790        (lambda (group-sym)
14791          (if (or (null (setq group (symbol-name group-sym)))
14792                  (null (symbol-value group-sym))
14793                  (gnus-gethash group gnus-newsrc-hashtb)
14794                  (member group gnus-zombie-list)
14795                  (member group gnus-killed-list))
14796              ;; The group is already known.
14797              ()
14798            ;; Make this group active.
14799            (when (symbol-value group-sym)
14800              (gnus-set-active group (symbol-value group-sym)))
14801            ;; Check whether we want it or not.
14802            (let ((do-sub (gnus-matches-options-n group)))
14803              (cond
14804               ((eq do-sub 'subscribe)
14805                (incf groups)
14806                (gnus-sethash group group gnus-killed-hashtb)
14807                (funcall gnus-subscribe-options-newsgroup-method group))
14808               ((eq do-sub 'ignore)
14809                nil)
14810               (t
14811                (incf groups)
14812                (gnus-sethash group group gnus-killed-hashtb)
14813                (if gnus-subscribe-hierarchical-interactive
14814                    (push group new-newsgroups)
14815                  (funcall gnus-subscribe-newsgroup-method group)))))))
14816        hashtb)
14817       (when new-newsgroups
14818         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
14819     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14820     (when (> groups 0)
14821       (gnus-message 6 "%d new newsgroup%s arrived."
14822                     groups (if (> groups 1) "s have" " has")))
14823     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14824     got-new))
14825
14826 (defun gnus-check-first-time-used ()
14827   (if (or (> (length gnus-newsrc-alist) 1)
14828           (file-exists-p gnus-startup-file)
14829           (file-exists-p (concat gnus-startup-file ".el"))
14830           (file-exists-p (concat gnus-startup-file ".eld")))
14831       nil
14832     (gnus-message 6 "First time user; subscribing you to default groups")
14833     (or gnus-have-read-active-file (gnus-read-active-file))
14834     (setq gnus-newsrc-last-checked-date (current-time-string))
14835     (let ((groups gnus-default-subscribed-newsgroups)
14836           group)
14837       (if (eq groups t)
14838           nil
14839         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
14840         (mapatoms
14841          (lambda (sym)
14842            (if (null (setq group (symbol-name sym)))
14843                ()
14844              (let ((do-sub (gnus-matches-options-n group)))
14845                (cond
14846                 ((eq do-sub 'subscribe)
14847                  (gnus-sethash group group gnus-killed-hashtb)
14848                  (funcall gnus-subscribe-options-newsgroup-method group))
14849                 ((eq do-sub 'ignore)
14850                  nil)
14851                 (t
14852                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
14853          gnus-active-hashtb)
14854         (while groups
14855           (if (gnus-active (car groups))
14856               (gnus-group-change-level
14857                (car groups) gnus-level-default-subscribed gnus-level-killed))
14858           (setq groups (cdr groups)))
14859         (gnus-group-make-help-group)
14860         (and gnus-novice-user
14861              (gnus-message 7 "`A k' to list killed groups"))))))
14862
14863 (defun gnus-subscribe-group (group previous &optional method)
14864   (gnus-group-change-level
14865    (if method
14866        (list t group gnus-level-default-subscribed nil nil method)
14867      group)
14868    gnus-level-default-subscribed gnus-level-killed previous t))
14869
14870 ;; `gnus-group-change-level' is the fundamental function for changing
14871 ;; subscription levels of newsgroups.  This might mean just changing
14872 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
14873 ;; again, which subscribes/unsubscribes a group, which is equally
14874 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
14875 ;; from 8-9 to 1-7 means that you remove the group from the list of
14876 ;; killed (or zombie) groups and add them to the (kinda) subscribed
14877 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
14878 ;; which is trivial.
14879 ;; ENTRY can either be a string (newsgroup name) or a list (if
14880 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
14881 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
14882 ;; entries.
14883 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
14884 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
14885 ;; after.
14886 (defun gnus-group-change-level (entry level &optional oldlevel
14887                                       previous fromkilled)
14888   (let (group info active num)
14889     ;; Glean what info we can from the arguments
14890     (if (consp entry)
14891         (if fromkilled (setq group (nth 1 entry))
14892           (setq group (car (nth 2 entry))))
14893       (setq group entry))
14894     (if (and (stringp entry)
14895              oldlevel
14896              (< oldlevel gnus-level-zombie))
14897         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
14898     (if (and (not oldlevel)
14899              (consp entry))
14900         (setq oldlevel (car (cdr (nth 2 entry)))))
14901     (if (stringp previous)
14902         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
14903
14904     (if (and (>= oldlevel gnus-level-zombie)
14905              (gnus-gethash group gnus-newsrc-hashtb))
14906         ;; We are trying to subscribe a group that is already
14907         ;; subscribed.
14908         ()                              ; Do nothing.
14909
14910       (or (gnus-ephemeral-group-p group)
14911           (gnus-dribble-enter
14912            (format "(gnus-group-change-level %S %S %S %S %S)"
14913                    group level oldlevel (car (nth 2 previous)) fromkilled)))
14914
14915       ;; Then we remove the newgroup from any old structures, if needed.
14916       ;; If the group was killed, we remove it from the killed or zombie
14917       ;; list.  If not, and it is in fact going to be killed, we remove
14918       ;; it from the newsrc hash table and assoc.
14919       (cond ((>= oldlevel gnus-level-zombie)
14920              (if (= oldlevel gnus-level-zombie)
14921                  (setq gnus-zombie-list (delete group gnus-zombie-list))
14922                (setq gnus-killed-list (delete group gnus-killed-list))))
14923             (t
14924              (if (and (>= level gnus-level-zombie)
14925                       entry)
14926                  (progn
14927                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
14928                    (if (nth 3 entry)
14929                        (setcdr (gnus-gethash (car (nth 3 entry))
14930                                              gnus-newsrc-hashtb)
14931                                (cdr entry)))
14932                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
14933
14934       ;; Finally we enter (if needed) the list where it is supposed to
14935       ;; go, and change the subscription level.  If it is to be killed,
14936       ;; we enter it into the killed or zombie list.
14937       (cond ((>= level gnus-level-zombie)
14938              ;; Remove from the hash table.
14939              (gnus-sethash group nil gnus-newsrc-hashtb)
14940              ;; We do not enter foreign groups into the list of dead
14941              ;; groups.
14942              (unless (gnus-group-foreign-p group)
14943                (if (= level gnus-level-zombie)
14944                    (setq gnus-zombie-list (cons group gnus-zombie-list))
14945                  (setq gnus-killed-list (cons group gnus-killed-list)))))
14946             (t
14947              ;; If the list is to be entered into the newsrc assoc, and
14948              ;; it was killed, we have to create an entry in the newsrc
14949              ;; hashtb format and fix the pointers in the newsrc assoc.
14950              (if (>= oldlevel gnus-level-zombie)
14951                  (progn
14952                    (if (listp entry)
14953                        (progn
14954                          (setq info (cdr entry))
14955                          (setq num (car entry)))
14956                      (setq active (gnus-active group))
14957                      (setq num
14958                            (if active (- (1+ (cdr active)) (car active)) t))
14959                      ;; Check whether the group is foreign.  If so, the
14960                      ;; foreign select method has to be entered into the
14961                      ;; info.
14962                      (let ((method (or gnus-override-subscribe-method
14963                                        (gnus-group-method-name group))))
14964                        (if (eq method gnus-select-method)
14965                            (setq info (list group level nil))
14966                          (setq info (list group level nil nil method)))))
14967                    (or previous
14968                        (setq previous
14969                              (let ((p gnus-newsrc-alist))
14970                                (while (cdr (cdr p))
14971                                  (setq p (cdr p)))
14972                                p)))
14973                    (setq entry (cons info (cdr (cdr previous))))
14974                    (if (cdr previous)
14975                        (progn
14976                          (setcdr (cdr previous) entry)
14977                          (gnus-sethash group (cons num (cdr previous))
14978                                        gnus-newsrc-hashtb))
14979                      (setcdr previous entry)
14980                      (gnus-sethash group (cons num previous)
14981                                    gnus-newsrc-hashtb))
14982                    (if (cdr entry)
14983                        (setcdr (gnus-gethash (car (car (cdr entry)))
14984                                              gnus-newsrc-hashtb)
14985                                entry)))
14986                ;; It was alive, and it is going to stay alive, so we
14987                ;; just change the level and don't change any pointers or
14988                ;; hash table entries.
14989                (setcar (cdr (car (cdr (cdr entry)))) level))))
14990       (when gnus-group-change-level-function
14991         (funcall gnus-group-change-level-function group level oldlevel)))))
14992
14993 (defun gnus-kill-newsgroup (newsgroup)
14994   "Obsolete function.  Kills a newsgroup."
14995   (gnus-group-change-level
14996    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
14997
14998 (defun gnus-check-bogus-newsgroups (&optional confirm)
14999   "Remove bogus newsgroups.
15000 If CONFIRM is non-nil, the user has to confirm the deletion of every
15001 newsgroup."
15002   (let ((newsrc (cdr gnus-newsrc-alist))
15003         bogus group entry info)
15004     (gnus-message 5 "Checking bogus newsgroups...")
15005     (unless gnus-have-read-active-file
15006       (gnus-read-active-file))
15007     (when (member gnus-select-method gnus-have-read-active-file)
15008       ;; Find all bogus newsgroup that are subscribed.
15009       (while newsrc
15010         (setq info (pop newsrc)
15011               group (gnus-info-group info))
15012         (unless (or (gnus-active group) ; Active
15013                     (gnus-info-method info) ; Foreign
15014                     (and confirm
15015                          (not (gnus-y-or-n-p
15016                                (format "Remove bogus newsgroup: %s " group)))))
15017           ;; Found a bogus newsgroup.
15018           (push group bogus)))
15019       ;; Remove all bogus subscribed groups by first killing them, and
15020       ;; then removing them from the list of killed groups.
15021       (while bogus
15022         (when (setq entry (gnus-gethash (setq group (pop bogus))
15023                                         gnus-newsrc-hashtb))
15024           (gnus-group-change-level entry gnus-level-killed)
15025           (setq gnus-killed-list (delete group gnus-killed-list))))
15026       ;; Then we remove all bogus groups from the list of killed and
15027       ;; zombie groups.  They are are removed without confirmation.
15028       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15029             killed)
15030         (while dead-lists
15031           (setq killed (symbol-value (car dead-lists)))
15032           (while killed
15033             (unless (gnus-active (setq group (pop killed)))
15034               ;; The group is bogus.
15035               ;; !!!Slow as hell.
15036               (set (car dead-lists)
15037                    (delete group (symbol-value (car dead-lists))))))
15038           (setq dead-lists (cdr dead-lists))))
15039       (gnus-message 5 "Checking bogus newsgroups...done"))))
15040
15041 (defun gnus-check-duplicate-killed-groups ()
15042   "Remove duplicates from the list of killed groups."
15043   (interactive)
15044   (let ((killed gnus-killed-list))
15045     (while killed
15046       (gnus-message 9 "%d" (length killed))
15047       (setcdr killed (delete (car killed) (cdr killed)))
15048       (setq killed (cdr killed)))))
15049
15050 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15051 ;; and compute how many unread articles there are in each group.
15052 (defun gnus-get-unread-articles (&optional level)
15053   (let* ((newsrc (cdr gnus-newsrc-alist))
15054          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15055          (foreign-level
15056           (min
15057            (cond ((and gnus-activate-foreign-newsgroups
15058                        (not (numberp gnus-activate-foreign-newsgroups)))
15059                   (1+ gnus-level-subscribed))
15060                  ((numberp gnus-activate-foreign-newsgroups)
15061                   gnus-activate-foreign-newsgroups)
15062                  (t 0))
15063            level))
15064          info group active virtuals method fmethod)
15065     (gnus-message 5 "Checking new news...")
15066
15067     (while newsrc
15068       (setq info (car newsrc)
15069             group (gnus-info-group info)
15070             active (gnus-active group))
15071
15072       ;; Check newsgroups.  If the user doesn't want to check them, or
15073       ;; they can't be checked (for instance, if the news server can't
15074       ;; be reached) we just set the number of unread articles in this
15075       ;; newsgroup to t.  This means that Gnus thinks that there are
15076       ;; unread articles, but it has no idea how many.
15077       (if (and (setq method (gnus-info-method info))
15078                (not (gnus-server-equal
15079                      gnus-select-method
15080                      (setq fmethod (gnus-server-get-method nil method))))
15081                (not (gnus-secondary-method-p method)))
15082           ;; These groups are foreign.  Check the level.
15083           (if (<= (gnus-info-level info) foreign-level)
15084               (setq active (gnus-activate-group (gnus-info-group info) 'scan)))
15085
15086         ;; These groups are native or secondary.
15087         (if (<= (gnus-info-level info) level)
15088             (or gnus-read-active-file
15089                 (setq active (gnus-activate-group
15090                               (gnus-info-group info) 'scan)))))
15091
15092       (if active
15093           (gnus-get-unread-articles-in-group info active t)
15094         ;; The group couldn't be reached, so we nix out the number of
15095         ;; unread articles and stuff.
15096         (gnus-set-active group nil)
15097         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
15098
15099       (setq newsrc (cdr newsrc)))
15100
15101     (gnus-message 5 "Checking new news...done")))
15102
15103 ;; Create a hash table out of the newsrc alist.  The `car's of the
15104 ;; alist elements are used as keys.
15105 (defun gnus-make-hashtable-from-newsrc-alist ()
15106   (let ((alist gnus-newsrc-alist)
15107         (ohashtb gnus-newsrc-hashtb)
15108         prev)
15109     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15110     (setq alist
15111           (setq prev (setq gnus-newsrc-alist
15112                            (if (equal (car (car gnus-newsrc-alist))
15113                                       "dummy.group")
15114                                gnus-newsrc-alist
15115                              (cons (list "dummy.group" 0 nil) alist)))))
15116     (while alist
15117       (gnus-sethash
15118        (car (car alist))
15119        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb)))
15120              prev)
15121        gnus-newsrc-hashtb)
15122       (setq prev alist
15123             alist (cdr alist)))))
15124
15125 (defun gnus-make-hashtable-from-killed ()
15126   "Create a hash table from the killed and zombie lists."
15127   (let ((lists '(gnus-killed-list gnus-zombie-list))
15128         list)
15129     (setq gnus-killed-hashtb
15130           (gnus-make-hashtable
15131            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15132     (while lists
15133       (setq list (symbol-value (car lists)))
15134       (setq lists (cdr lists))
15135       (while list
15136         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15137         (setq list (cdr list))))))
15138
15139 (defun gnus-get-unread-articles-in-group (info active &optional update)
15140   ;; Allow the backend to update the info in the group.
15141   (when update
15142     (gnus-request-update-info
15143      info (gnus-find-method-for-group (gnus-info-group info))))
15144   (let* ((range (gnus-info-read info))
15145          (num 0)
15146          (marked (gnus-info-marks info)))
15147     ;; If a cache is present, we may have to alter the active info.
15148     (and gnus-use-cache
15149          (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15150     ;; Modify the list of read articles according to what articles
15151     ;; are available; then tally the unread articles and add the
15152     ;; number to the group hash table entry.
15153     (cond
15154      ((zerop (cdr active))
15155       (setq num 0))
15156      ((not range)
15157       (setq num (- (1+ (cdr active)) (car active))))
15158      ((not (listp (cdr range)))
15159       ;; Fix a single (num . num) range according to the
15160       ;; active hash table.
15161       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15162       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15163       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15164       ;; Compute number of unread articles.
15165       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15166      (t
15167       ;; The read list is a list of ranges.  Fix them according to
15168       ;; the active hash table.
15169       ;; First peel off any elements that are below the lower
15170       ;; active limit.
15171       (while (and (cdr range)
15172                   (>= (car active)
15173                       (or (and (atom (car (cdr range))) (car (cdr range)))
15174                           (car (car (cdr range))))))
15175         (if (numberp (car range))
15176             (setcar range
15177                     (cons (car range)
15178                           (or (and (numberp (car (cdr range)))
15179                                    (car (cdr range)))
15180                               (cdr (car (cdr range))))))
15181           (setcdr (car range)
15182                   (or (and (numberp (nth 1 range)) (nth 1 range))
15183                       (cdr (car (cdr range))))))
15184         (setcdr range (cdr (cdr range))))
15185       ;; Adjust the first element to be the same as the lower limit.
15186       (if (and (not (atom (car range)))
15187                (< (cdr (car range)) (car active)))
15188           (setcdr (car range) (1- (car active))))
15189       ;; Then we want to peel off any elements that are higher
15190       ;; than the upper active limit.
15191       (let ((srange range))
15192         ;; Go past all legal elements.
15193         (while (and (cdr srange)
15194                     (<= (or (and (atom (car (cdr srange)))
15195                                  (car (cdr srange)))
15196                             (car (car (cdr srange)))) (cdr active)))
15197           (setq srange (cdr srange)))
15198         (if (cdr srange)
15199             ;; Nuke all remaining illegal elements.
15200             (setcdr srange nil))
15201
15202         ;; Adjust the final element.
15203         (if (and (not (atom (car srange)))
15204                  (> (cdr (car srange)) (cdr active)))
15205             (setcdr (car srange) (cdr active))))
15206       ;; Compute the number of unread articles.
15207       (while range
15208         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15209                                     (cdr (car range))))
15210                             (or (and (atom (car range)) (car range))
15211                                 (car (car range))))))
15212         (setq range (cdr range)))
15213       (setq num (max 0 (- (cdr active) num)))))
15214     ;; Set the number of unread articles.
15215     (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
15216     num))
15217
15218 (defun gnus-activate-group (group &optional scan)
15219   ;; Check whether a group has been activated or not.
15220   ;; If SCAN, request a scan of that group as well.
15221   (let ((method (gnus-find-method-for-group group))
15222         active)
15223     (and (gnus-check-server method)
15224          ;; We escape all bugs and quit here to make it possible to
15225          ;; continue if a group is so out-there that it reports bugs
15226          ;; and stuff.
15227          (progn
15228            (and scan
15229                 (gnus-check-backend-function 'request-scan (car method))
15230                 (gnus-request-scan group method))
15231            t)
15232          (condition-case ()
15233              (gnus-request-group group)
15234         ;   (error nil)
15235            (quit nil))
15236          (save-excursion
15237            (set-buffer nntp-server-buffer)
15238            (goto-char (point-min))
15239            ;; Parse the result we got from `gnus-request-group'.
15240            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15241                 (progn
15242                   (goto-char (match-beginning 1))
15243                   (gnus-set-active
15244                    group (setq active (cons (read (current-buffer))
15245                                             (read (current-buffer)))))
15246                   ;; Return the new active info.
15247                   active))))))
15248
15249 (defun gnus-update-read-articles (group unread)
15250   "Update the list of read and ticked articles in GROUP using the
15251 UNREAD and TICKED lists.
15252 Note: UNSELECTED has to be sorted over `<'.
15253 Returns whether the updating was successful."
15254   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15255          (entry (gnus-gethash group gnus-newsrc-hashtb))
15256          (info (nth 2 entry))
15257          (marked (gnus-info-marks info))
15258          (prev 1)
15259          (unread (sort (copy-sequence unread) '<))
15260          read)
15261     (if (or (not info) (not active))
15262         ;; There is no info on this group if it was, in fact,
15263         ;; killed.  Gnus stores no information on killed groups, so
15264         ;; there's nothing to be done.
15265         ;; One could store the information somewhere temporarily,
15266         ;; perhaps...  Hmmm...
15267         ()
15268       ;; Remove any negative articles numbers.
15269       (while (and unread (< (car unread) 0))
15270         (setq unread (cdr unread)))
15271       ;; Remove any expired article numbers
15272       (while (and unread (< (car unread) (car active)))
15273         (setq unread (cdr unread)))
15274       ;; Compute the ranges of read articles by looking at the list of
15275       ;; unread articles.
15276       (while unread
15277         (if (/= (car unread) prev)
15278             (setq read (cons (if (= prev (1- (car unread))) prev
15279                                (cons prev (1- (car unread)))) read)))
15280         (setq prev (1+ (car unread)))
15281         (setq unread (cdr unread)))
15282       (when (<= prev (cdr active))
15283         (setq read (cons (cons prev (cdr active)) read)))
15284       ;; Enter this list into the group info.
15285       (gnus-info-set-read
15286        info (if (> (length read) 1) (nreverse read) read))
15287       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15288       (gnus-get-unread-articles-in-group info (gnus-active group))
15289       t)))
15290
15291 (defun gnus-make-articles-unread (group articles)
15292   "Mark ARTICLES in GROUP as unread."
15293   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15294                           (gnus-gethash (gnus-group-real-name group)
15295                                         gnus-newsrc-hashtb))))
15296          (ranges (gnus-info-read info))
15297          news article)
15298     (while articles
15299       (when (gnus-member-of-range
15300              (setq article (pop articles)) ranges)
15301         (setq news (cons article news))))
15302     (when news
15303       (gnus-info-set-read
15304        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15305       (gnus-group-update-group group t))))
15306
15307 ;; Enter all dead groups into the hashtb.
15308 (defun gnus-update-active-hashtb-from-killed ()
15309   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15310         (lists (list gnus-killed-list gnus-zombie-list))
15311         killed)
15312     (while lists
15313       (setq killed (car lists))
15314       (while killed
15315         (gnus-sethash (car killed) nil hashtb)
15316         (setq killed (cdr killed)))
15317       (setq lists (cdr lists)))))
15318
15319 ;; Get the active file(s) from the backend(s).
15320 (defun gnus-read-active-file ()
15321   (gnus-group-set-mode-line)
15322   (let ((methods (nconc (copy-sequence
15323                          (if (gnus-check-server gnus-select-method)
15324                              ;; The native server is available.
15325                              (cons gnus-select-method 
15326                                    gnus-secondary-select-methods)
15327                            ;; The native server is down, so we just do the
15328                            ;; secondary ones.
15329                            gnus-secondary-select-methods))
15330                         (list gnus-message-archive-method)))
15331         list-type)
15332     (setq gnus-have-read-active-file nil)
15333     (save-excursion
15334       (set-buffer nntp-server-buffer)
15335       (while methods
15336         (let* ((method (gnus-server-get-method nil (car methods)))
15337                (where (nth 1 method))
15338                (mesg (format "Reading active file%s via %s..."
15339                              (if (and where (not (zerop (length where))))
15340                                  (concat " from " where) "")
15341                              (car method))))
15342           (gnus-message 5 mesg)
15343           (if (not (gnus-check-server method))
15344               ()
15345             ;; Request that the backend scan its incoming messages.
15346             (and (gnus-check-backend-function 'request-scan (car method))
15347                  (gnus-request-scan nil method))
15348             (cond
15349              ((and (eq gnus-read-active-file 'some)
15350                    (gnus-check-backend-function 'retrieve-groups (car method)))
15351               (let ((newsrc (cdr gnus-newsrc-alist))
15352                     (gmethod (gnus-server-get-method nil method))
15353                     groups)
15354                 (while newsrc
15355                   (and (gnus-server-equal
15356                         (gnus-find-method-for-group
15357                          (car (car newsrc)) (car newsrc))
15358                         gmethod)
15359                        (setq groups (cons (gnus-group-real-name
15360                                            (car (car newsrc))) groups)))
15361                   (setq newsrc (cdr newsrc)))
15362                 (gnus-check-server method)
15363                 (setq list-type (gnus-retrieve-groups groups method))
15364                 (cond
15365                  ((not list-type)
15366                   (gnus-message
15367                    1 "Cannot read partial active file from %s server."
15368                    (car method))
15369                   (ding)
15370                   (sit-for 2))
15371                  ((eq list-type 'active)
15372                   (gnus-active-to-gnus-format method gnus-active-hashtb))
15373                  (t
15374                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
15375              (t
15376               (if (not (gnus-request-list method))
15377                   (progn
15378                     (unless (equal method gnus-message-archive-method)
15379                       (gnus-message 1 "Cannot read active file from %s server."
15380                                     (car method))
15381                       (ding)))
15382                 (gnus-active-to-gnus-format method)
15383                 ;; We mark this active file as read.
15384                 (setq gnus-have-read-active-file
15385                       (cons method gnus-have-read-active-file))
15386                 (gnus-message 5 "%sdone" mesg))))))
15387         (setq methods (cdr methods))))))
15388
15389 ;; Read an active file and place the results in `gnus-active-hashtb'.
15390 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15391   (unless method
15392     (setq method gnus-select-method))
15393   (let ((cur (current-buffer))
15394         (hashtb (or hashtb
15395                     (if (and gnus-active-hashtb
15396                              (not (equal method gnus-select-method)))
15397                         gnus-active-hashtb
15398                       (setq gnus-active-hashtb
15399                             (if (equal method gnus-select-method)
15400                                 (gnus-make-hashtable
15401                                  (count-lines (point-min) (point-max)))
15402                               (gnus-make-hashtable 4096))))))
15403         (flag-hashtb (gnus-make-hashtable 60)))
15404     ;; Delete unnecessary lines.
15405     (goto-char (point-min))
15406     (while (search-forward "\nto." nil t)
15407       (delete-region (1+ (match-beginning 0))
15408                      (progn (forward-line 1) (point))))
15409     (or (string= gnus-ignored-newsgroups "")
15410         (progn
15411           (goto-char (point-min))
15412           (delete-matching-lines gnus-ignored-newsgroups)))
15413     ;; Make the group names readable as a lisp expression even if they
15414     ;; contain special characters.
15415     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15416     (goto-char (point-max))
15417     (while (re-search-backward "[][';?()#]" nil t)
15418       (insert ?\\))
15419     ;; If these are groups from a foreign select method, we insert the
15420     ;; group prefix in front of the group names.
15421     (and method (not (gnus-server-equal
15422                       (gnus-server-get-method nil method)
15423                       (gnus-server-get-method nil gnus-select-method)))
15424          (let ((prefix (gnus-group-prefixed-name "" method)))
15425            (goto-char (point-min))
15426            (while (and (not (eobp))
15427                        (progn (insert prefix)
15428                               (zerop (forward-line 1)))))))
15429     ;; Store the active file in a hash table.
15430     (goto-char (point-min))
15431     (if (string-match "%[oO]" gnus-group-line-format)
15432         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15433         ;; If we want information on moderated groups, we use this
15434         ;; loop...
15435         (let* ((mod-hashtb (make-vector 7 0))
15436                (m (intern "m" mod-hashtb))
15437                group max min)
15438           (while (not (eobp))
15439             (condition-case nil
15440                 (progn
15441                   (narrow-to-region (point) (gnus-point-at-eol))
15442                   (setq group (let ((obarray hashtb)) (read cur)))
15443                   (if (and (numberp (setq max (read cur)))
15444                            (numberp (setq min (read cur)))
15445                            (progn
15446                              (skip-chars-forward " \t")
15447                              (not
15448                               (or (= (following-char) ?=)
15449                                   (= (following-char) ?x)
15450                                   (= (following-char) ?j)))))
15451                       (set group (cons min max))
15452                     (set group nil))
15453                   ;; Enter moderated groups into a list.
15454                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15455                       (setq gnus-moderated-list
15456                             (cons (symbol-name group) gnus-moderated-list))))
15457               (error
15458                (and group
15459                     (symbolp group)
15460                     (set group nil))))
15461             (widen)
15462             (forward-line 1)))
15463       ;; And if we do not care about moderation, we use this loop,
15464       ;; which is faster.
15465       (let (group max min)
15466         (while (not (eobp))
15467           (condition-case ()
15468               (progn
15469                 (narrow-to-region (point) (gnus-point-at-eol))
15470                 ;; group gets set to a symbol interned in the hash table
15471                 ;; (what a hack!!) - jwz
15472                 (setq group (let ((obarray hashtb)) (read cur)))
15473                 (if (and (numberp (setq max (read cur)))
15474                          (numberp (setq min (read cur)))
15475                          (progn
15476                            (skip-chars-forward " \t")
15477                            (not
15478                             (or (= (following-char) ?=)
15479                                 (= (following-char) ?x)
15480                                 (= (following-char) ?j)))))
15481                     (set group (cons min max))
15482                   (set group nil)))
15483             (error
15484              (progn
15485                (and group
15486                     (symbolp group)
15487                     (set group nil))
15488                (or ignore-errors
15489                    (gnus-message 3 "Warning - illegal active: %s"
15490                                  (buffer-substring
15491                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15492           (widen)
15493           (forward-line 1))))))
15494
15495 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15496   ;; Parse a "groups" active file.
15497   (let ((cur (current-buffer))
15498         (hashtb (or hashtb
15499                     (if (and method gnus-active-hashtb)
15500                         gnus-active-hashtb
15501                       (setq gnus-active-hashtb
15502                             (gnus-make-hashtable
15503                              (count-lines (point-min) (point-max)))))))
15504         (prefix (and method
15505                      (not (gnus-server-equal
15506                            (gnus-server-get-method nil method)
15507                            (gnus-server-get-method nil gnus-select-method)))
15508                      (gnus-group-prefixed-name "" method))))
15509
15510     (goto-char (point-min))
15511     ;; We split this into to separate loops, one with the prefix
15512     ;; and one without to speed the reading up somewhat.
15513     (if prefix
15514         (let (min max opoint group)
15515           (while (not (eobp))
15516             (condition-case ()
15517                 (progn
15518                   (read cur) (read cur)
15519                   (setq min (read cur)
15520                         max (read cur)
15521                         opoint (point))
15522                   (skip-chars-forward " \t")
15523                   (insert prefix)
15524                   (goto-char opoint)
15525                   (set (let ((obarray hashtb)) (read cur))
15526                        (cons min max)))
15527               (error (and group (symbolp group) (set group nil))))
15528             (forward-line 1)))
15529       (let (min max group)
15530         (while (not (eobp))
15531           (condition-case ()
15532               (if (= (following-char) ?2)
15533                   (progn
15534                     (read cur) (read cur)
15535                     (setq min (read cur)
15536                           max (read cur))
15537                     (set (setq group (let ((obarray hashtb)) (read cur)))
15538                          (cons min max))))
15539             (error (and group (symbolp group) (set group nil))))
15540           (forward-line 1))))))
15541
15542 (defun gnus-read-newsrc-file (&optional force)
15543   "Read startup file.
15544 If FORCE is non-nil, the .newsrc file is read."
15545   ;; Reset variables that might be defined in the .newsrc.eld file.
15546   (let ((variables gnus-variable-list))
15547     (while variables
15548       (set (car variables) nil)
15549       (setq variables (cdr variables))))
15550   (let* ((newsrc-file gnus-current-startup-file)
15551          (quick-file (concat newsrc-file ".el")))
15552     (save-excursion
15553       ;; We always load the .newsrc.eld file.  If always contains
15554       ;; much information that can not be gotten from the .newsrc
15555       ;; file (ticked articles, killed groups, foreign methods, etc.)
15556       (gnus-read-newsrc-el-file quick-file)
15557
15558       (if (or force
15559               (and (file-newer-than-file-p newsrc-file quick-file)
15560                    (file-newer-than-file-p newsrc-file
15561                                            (concat quick-file "d")))
15562               (not gnus-newsrc-alist))
15563           ;; We read the .newsrc file.  Note that if there if a
15564           ;; .newsrc.eld file exists, it has already been read, and
15565           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15566           ;; the .newsrc file, Gnus will only use the information it
15567           ;; can find there for changing the data already read -
15568           ;; ie. reading the .newsrc file will not trash the data
15569           ;; already read (except for read articles).
15570           (save-excursion
15571             (gnus-message 5 "Reading %s..." newsrc-file)
15572             (set-buffer (find-file-noselect newsrc-file))
15573             (buffer-disable-undo (current-buffer))
15574             (gnus-newsrc-to-gnus-format)
15575             (kill-buffer (current-buffer))
15576             (gnus-message 5 "Reading %s...done" newsrc-file)))
15577
15578       ;; Read any slave files.
15579       (or gnus-slave
15580           (gnus-master-read-slave-newsrc)))))
15581
15582 (defun gnus-read-newsrc-el-file (file)
15583   (let ((ding-file (concat file "d")))
15584     ;; We always, always read the .eld file.
15585     (gnus-message 5 "Reading %s..." ding-file)
15586     (let (gnus-newsrc-assoc)
15587       (condition-case nil
15588           (load ding-file t t t)
15589         (error
15590          (gnus-message 1 "Error in %s" ding-file)
15591          (ding)))
15592       (when gnus-newsrc-assoc
15593         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15594     (gnus-make-hashtable-from-newsrc-alist)
15595     (when (file-newer-than-file-p file ding-file)
15596       ;; Old format quick file
15597       (gnus-message 5 "Reading %s..." file)
15598       ;; The .el file is newer than the .eld file, so we read that one
15599       ;; as well.
15600       (gnus-read-old-newsrc-el-file file))))
15601
15602 ;; Parse the old-style quick startup file
15603 (defun gnus-read-old-newsrc-el-file (file)
15604   (let (newsrc killed marked group m)
15605     (prog1
15606         (let ((gnus-killed-assoc nil)
15607               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15608           (prog1
15609               (condition-case nil
15610                   (load file t t t)
15611                 (error nil))
15612             (setq newsrc gnus-newsrc-assoc
15613                   killed gnus-killed-assoc
15614                   marked gnus-marked-assoc)))
15615       (setq gnus-newsrc-alist nil)
15616       (while newsrc
15617         (setq group (car newsrc))
15618         (let ((info (gnus-get-info (car group))))
15619           (if info
15620               (progn
15621                 (gnus-info-set-read info (cdr (cdr group)))
15622                 (gnus-info-set-level
15623                  info (if (nth 1 group) gnus-level-default-subscribed
15624                         gnus-level-default-unsubscribed))
15625                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15626             (setq gnus-newsrc-alist
15627                   (cons
15628                    (setq info
15629                          (list (car group)
15630                                (if (nth 1 group) gnus-level-default-subscribed
15631                                  gnus-level-default-unsubscribed)
15632                                (cdr (cdr group))))
15633                    gnus-newsrc-alist)))
15634           (if (setq m (assoc (car group) marked))
15635               (gnus-info-set-marks
15636                info (cons (list (cons 'tick (gnus-compress-sequence
15637                                              (sort (cdr m) '<) t)))
15638                           nil))))
15639         (setq newsrc (cdr newsrc)))
15640       (setq newsrc killed)
15641       (while newsrc
15642         (setcar newsrc (car (car newsrc)))
15643         (setq newsrc (cdr newsrc)))
15644       (setq gnus-killed-list killed))
15645     ;; The .el file version of this variable does not begin with
15646     ;; "options", while the .eld version does, so we just add it if it
15647     ;; isn't there.
15648     (and
15649      gnus-newsrc-options
15650      (progn
15651        (and (not (string-match "^ *options" gnus-newsrc-options))
15652             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15653        (and (not (string-match "\n$" gnus-newsrc-options))
15654             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15655        ;; Finally, if we read some options lines, we parse them.
15656        (or (string= gnus-newsrc-options "")
15657            (gnus-newsrc-parse-options gnus-newsrc-options))))
15658
15659     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15660     (gnus-make-hashtable-from-newsrc-alist)))
15661
15662 (defun gnus-make-newsrc-file (file)
15663   "Make server dependent file name by catenating FILE and server host name."
15664   (let* ((file (expand-file-name file nil))
15665          (real-file (concat file "-" (nth 1 gnus-select-method))))
15666     (if (or (file-exists-p real-file)
15667             (file-exists-p (concat real-file ".el"))
15668             (file-exists-p (concat real-file ".eld")))
15669         real-file file)))
15670
15671 (defun gnus-newsrc-to-gnus-format ()
15672   (setq gnus-newsrc-options "")
15673   (setq gnus-newsrc-options-n nil)
15674
15675   (or gnus-active-hashtb
15676       (setq gnus-active-hashtb (make-vector 4095 0)))
15677   (let ((buf (current-buffer))
15678         (already-read (> (length gnus-newsrc-alist) 1))
15679         group subscribed options-symbol newsrc Options-symbol
15680         symbol reads num1)
15681     (goto-char (point-min))
15682     ;; We intern the symbol `options' in the active hashtb so that we
15683     ;; can `eq' against it later.
15684     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15685     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15686
15687     (while (not (eobp))
15688       ;; We first read the first word on the line by narrowing and
15689       ;; then reading into `gnus-active-hashtb'.  Most groups will
15690       ;; already exist in that hashtb, so this will save some string
15691       ;; space.
15692       (narrow-to-region
15693        (point)
15694        (progn (skip-chars-forward "^ \t!:\n") (point)))
15695       (goto-char (point-min))
15696       (setq symbol
15697             (and (/= (point-min) (point-max))
15698                  (let ((obarray gnus-active-hashtb)) (read buf))))
15699       (widen)
15700       ;; Now, the symbol we have read is either `options' or a group
15701       ;; name.  If it is an options line, we just add it to a string.
15702       (cond
15703        ((or (eq symbol options-symbol)
15704             (eq symbol Options-symbol))
15705         (setq gnus-newsrc-options
15706               ;; This concating is quite inefficient, but since our
15707               ;; thorough studies show that approx 99.37% of all
15708               ;; .newsrc files only contain a single options line, we
15709               ;; don't give a damn, frankly, my dear.
15710               (concat gnus-newsrc-options
15711                       (buffer-substring
15712                        (gnus-point-at-bol)
15713                        ;; Options may continue on the next line.
15714                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15715                                 (progn (beginning-of-line) (point)))
15716                            (point)))))
15717         (forward-line -1))
15718        (symbol
15719         ;; Group names can be just numbers.  
15720         (when (numberp symbol) 
15721           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15722         (or (boundp symbol) (set symbol nil))
15723         ;; It was a group name.
15724         (setq subscribed (= (following-char) ?:)
15725               group (symbol-name symbol)
15726               reads nil)
15727         (if (eolp)
15728             ;; If the line ends here, this is clearly a buggy line, so
15729             ;; we put point a the beginning of line and let the cond
15730             ;; below do the error handling.
15731             (beginning-of-line)
15732           ;; We skip to the beginning of the ranges.
15733           (skip-chars-forward "!: \t"))
15734         ;; We are now at the beginning of the list of read articles.
15735         ;; We read them range by range.
15736         (while
15737             (cond
15738              ((looking-at "[0-9]+")
15739               ;; We narrow and read a number instead of buffer-substring/
15740               ;; string-to-int because it's faster.  narrow/widen is
15741               ;; faster than save-restriction/narrow, and save-restriction
15742               ;; produces a garbage object.
15743               (setq num1 (progn
15744                            (narrow-to-region (match-beginning 0) (match-end 0))
15745                            (read buf)))
15746               (widen)
15747               ;; If the next character is a dash, then this is a range.
15748               (if (= (following-char) ?-)
15749                   (progn
15750                     ;; We read the upper bound of the range.
15751                     (forward-char 1)
15752                     (if (not (looking-at "[0-9]+"))
15753                         ;; This is a buggy line, by we pretend that
15754                         ;; it's kinda OK.  Perhaps the user should be
15755                         ;; dinged?
15756                         (setq reads (cons num1 reads))
15757                       (setq reads
15758                             (cons
15759                              (cons num1
15760                                    (progn
15761                                      (narrow-to-region (match-beginning 0)
15762                                                        (match-end 0))
15763                                      (read buf)))
15764                              reads))
15765                       (widen)))
15766                 ;; It was just a simple number, so we add it to the
15767                 ;; list of ranges.
15768                 (setq reads (cons num1 reads)))
15769               ;; If the next char in ?\n, then we have reached the end
15770               ;; of the line and return nil.
15771               (/= (following-char) ?\n))
15772              ((= (following-char) ?\n)
15773               ;; End of line, so we end.
15774               nil)
15775              (t
15776               ;; Not numbers and not eol, so this might be a buggy
15777               ;; line...
15778               (or (eobp)
15779                   ;; If it was eob instead of ?\n, we allow it.
15780                   (progn
15781                     ;; The line was buggy.
15782                     (setq group nil)
15783                     (gnus-message 3 "Mangled line: %s"
15784                                   (buffer-substring (gnus-point-at-bol)
15785                                                     (gnus-point-at-eol)))
15786                     (ding)
15787                     (sit-for 1)))
15788               nil))
15789           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15790           ;; we allow them, because it's a common mistake to put a
15791           ;; space after the comma.
15792           (skip-chars-forward ", "))
15793
15794         ;; We have already read .newsrc.eld, so we gently update the
15795         ;; data in the hash table with the information we have just
15796         ;; read.
15797         (when group
15798           (let ((info (gnus-get-info group))
15799                 level)
15800             (if info
15801                 ;; There is an entry for this file in the alist.
15802                 (progn
15803                   (gnus-info-set-read info (nreverse reads))
15804                   ;; We update the level very gently.  In fact, we
15805                   ;; only change it if there's been a status change
15806                   ;; from subscribed to unsubscribed, or vice versa.
15807                   (setq level (gnus-info-level info))
15808                   (cond ((and (<= level gnus-level-subscribed)
15809                               (not subscribed))
15810                          (setq level (if reads
15811                                          gnus-level-default-unsubscribed
15812                                        (1+ gnus-level-default-unsubscribed))))
15813                         ((and (> level gnus-level-subscribed) subscribed)
15814                          (setq level gnus-level-default-subscribed)))
15815                   (gnus-info-set-level info level))
15816               ;; This is a new group.
15817               (setq info (list group
15818                                (if subscribed
15819                                    gnus-level-default-subscribed
15820                                  (if reads
15821                                      (1+ gnus-level-subscribed)
15822                                    gnus-level-default-unsubscribed))
15823                                (nreverse reads))))
15824             (setq newsrc (cons info newsrc))))))
15825       (forward-line 1))
15826
15827     (setq newsrc (nreverse newsrc))
15828
15829     (if (not already-read)
15830         ()
15831       ;; We now have two newsrc lists - `newsrc', which is what we
15832       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15833       ;; what we've read from .newsrc.eld.  We have to merge these
15834       ;; lists.  We do this by "attaching" any (foreign) groups in the
15835       ;; gnus-newsrc-alist to the (native) group that precedes them.
15836       (let ((rc (cdr gnus-newsrc-alist))
15837             (prev gnus-newsrc-alist)
15838             entry mentry)
15839         (while rc
15840           (or (null (nth 4 (car rc)))   ; It's a native group.
15841               (assoc (car (car rc)) newsrc) ; It's already in the alist.
15842               (if (setq entry (assoc (car (car prev)) newsrc))
15843                   (setcdr (setq mentry (memq entry newsrc))
15844                           (cons (car rc) (cdr mentry)))
15845                 (setq newsrc (cons (car rc) newsrc))))
15846           (setq prev rc
15847                 rc (cdr rc)))))
15848
15849     (setq gnus-newsrc-alist newsrc)
15850     ;; We make the newsrc hashtb.
15851     (gnus-make-hashtable-from-newsrc-alist)
15852
15853     ;; Finally, if we read some options lines, we parse them.
15854     (or (string= gnus-newsrc-options "")
15855         (gnus-newsrc-parse-options gnus-newsrc-options))))
15856
15857 ;; Parse options lines to find "options -n !all rec.all" and stuff.
15858 ;; The return value will be a list on the form
15859 ;; ((regexp1 . ignore)
15860 ;;  (regexp2 . subscribe)...)
15861 ;; When handling new newsgroups, groups that match a `ignore' regexp
15862 ;; will be ignored, and groups that match a `subscribe' regexp will be
15863 ;; subscribed.  A line like
15864 ;; options -n !all rec.all
15865 ;; will lead to a list that looks like
15866 ;; (("^rec\\..+" . subscribe)
15867 ;;  ("^.+" . ignore))
15868 ;; So all "rec.*" groups will be subscribed, while all the other
15869 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
15870 ;; different from "options -n rec.all !all".
15871 (defun gnus-newsrc-parse-options (options)
15872   (let (out eol)
15873     (save-excursion
15874       (gnus-set-work-buffer)
15875       (insert (regexp-quote options))
15876       ;; First we treat all continuation lines.
15877       (goto-char (point-min))
15878       (while (re-search-forward "\n[ \t]+" nil t)
15879         (replace-match " " t t))
15880       ;; Then we transform all "all"s into ".+"s.
15881       (goto-char (point-min))
15882       (while (re-search-forward "\\ball\\b" nil t)
15883         (replace-match ".+" t t))
15884       (goto-char (point-min))
15885       ;; We remove all other options than the "-n" ones.
15886       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
15887         (replace-match " ")
15888         (forward-char -1))
15889       (goto-char (point-min))
15890
15891       ;; We are only interested in "options -n" lines - we
15892       ;; ignore the other option lines.
15893       (while (re-search-forward "[ \t]-n" nil t)
15894         (setq eol
15895               (or (save-excursion
15896                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
15897                          (- (point) 2)))
15898                   (gnus-point-at-eol)))
15899         ;; Search for all "words"...
15900         (while (re-search-forward "[^ \t,\n]+" eol t)
15901           (if (= (char-after (match-beginning 0)) ?!)
15902               ;; If the word begins with a bang (!), this is a "not"
15903               ;; spec.  We put this spec (minus the bang) and the
15904               ;; symbol `ignore' into the list.
15905               (setq out (cons (cons (concat
15906                                      "^" (buffer-substring
15907                                           (1+ (match-beginning 0))
15908                                           (match-end 0)))
15909                                     'ignore) out))
15910             ;; There was no bang, so this is a "yes" spec.
15911             (setq out (cons (cons (concat "^" (match-string 0))
15912                                   'subscribe) out)))))
15913
15914       (setq gnus-newsrc-options-n out))))
15915
15916 (defun gnus-save-newsrc-file (&optional force)
15917   "Save .newsrc file."
15918   ;; Note: We cannot save .newsrc file if all newsgroups are removed
15919   ;; from the variable gnus-newsrc-alist.
15920   (when (and (or gnus-newsrc-alist gnus-killed-list)
15921              gnus-current-startup-file)
15922     (save-excursion
15923       (if (and (or gnus-use-dribble-file gnus-slave)
15924                (not force)
15925                (or (not gnus-dribble-buffer)
15926                    (not (buffer-name gnus-dribble-buffer))
15927                    (zerop (save-excursion
15928                             (set-buffer gnus-dribble-buffer)
15929                             (buffer-size)))))
15930           (gnus-message 4 "(No changes need to be saved)")
15931         (run-hooks 'gnus-save-newsrc-hook)
15932         (if gnus-slave
15933             (gnus-slave-save-newsrc)
15934           ;; Save .newsrc.
15935           (when gnus-save-newsrc-file
15936             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
15937             (gnus-gnus-to-newsrc-format)
15938             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
15939           ;; Save .newsrc.eld.
15940           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
15941           (make-local-variable 'version-control)
15942           (setq version-control 'never)
15943           (setq buffer-file-name
15944                 (concat gnus-current-startup-file ".eld"))
15945           (gnus-add-current-to-buffer-list)
15946           (buffer-disable-undo (current-buffer))
15947           (erase-buffer)
15948           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
15949           (gnus-gnus-to-quick-newsrc-format)
15950           (run-hooks 'gnus-save-quick-newsrc-hook)
15951           (save-buffer)
15952           (kill-buffer (current-buffer))
15953           (gnus-message
15954            5 "Saving %s.eld...done" gnus-current-startup-file))
15955         (gnus-dribble-delete-file)))))
15956
15957 (defun gnus-gnus-to-quick-newsrc-format ()
15958   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
15959   (insert ";; Gnus startup file.\n")
15960   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
15961   (insert ";; to read .newsrc.\n")
15962   (insert "(setq gnus-newsrc-file-version "
15963           (prin1-to-string gnus-version) ")\n")
15964   (let ((variables
15965          (if gnus-save-killed-list gnus-variable-list
15966            ;; Remove the `gnus-killed-list' from the list of variables
15967            ;; to be saved, if required.
15968            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
15969         ;; Peel off the "dummy" group.
15970         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
15971         variable)
15972     ;; Insert the variables into the file.
15973     (while variables
15974       (when (and (boundp (setq variable (pop variables)))
15975                  (symbol-value variable))
15976         (insert "(setq " (symbol-name variable) " '"
15977                 (prin1-to-string (symbol-value variable)) ")\n")))))
15978
15979 (defun gnus-gnus-to-newsrc-format ()
15980   ;; Generate and save the .newsrc file.
15981   (let ((newsrc (cdr gnus-newsrc-alist))
15982         info ranges range)
15983     (save-excursion
15984       (set-buffer (create-file-buffer gnus-current-startup-file))
15985       (setq buffer-file-name gnus-current-startup-file)
15986       (buffer-disable-undo (current-buffer))
15987       (erase-buffer)
15988       ;; Write options.
15989       (if gnus-newsrc-options (insert gnus-newsrc-options))
15990       ;; Write subscribed and unsubscribed.
15991       (while newsrc
15992         (setq info (car newsrc))
15993         (if (not (gnus-info-method info))
15994             ;; Don't write foreign groups to .newsrc.
15995             (progn
15996               (insert (gnus-info-group info)
15997                       (if (> (gnus-info-level info) gnus-level-subscribed)
15998                           "!" ":"))
15999               (if (setq ranges (gnus-info-read info))
16000                   (progn
16001                     (insert " ")
16002                     (if (not (listp (cdr ranges)))
16003                         (if (= (car ranges) (cdr ranges))
16004                             (insert (int-to-string (car ranges)))
16005                           (insert (int-to-string (car ranges)) "-"
16006                                   (int-to-string (cdr ranges))))
16007                       (while ranges
16008                         (setq range (car ranges)
16009                               ranges (cdr ranges))
16010                         (if (or (atom range) (= (car range) (cdr range)))
16011                             (insert (int-to-string
16012                                      (or (and (atom range) range)
16013                                          (car range))))
16014                           (insert (int-to-string (car range)) "-"
16015                                   (int-to-string (cdr range))))
16016                         (if ranges (insert ","))))))
16017               (insert "\n")))
16018         (setq newsrc (cdr newsrc)))
16019       (make-local-variable 'version-control)
16020       (setq version-control 'never)
16021       ;; It has been reported that sometime the modtime on the .newsrc
16022       ;; file seems to be off.  We really do want to overwrite it, so
16023       ;; we clear the modtime here before saving.  It's a bit odd,
16024       ;; though...
16025       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16026       ;; delete the silly thing entirely first.  but this fails to provide
16027       ;; such niceties as .newsrc~ creation.
16028       (if gnus-modtime-botch
16029           (delete-file gnus-startup-file)
16030         (clear-visited-file-modtime))
16031       (run-hooks 'gnus-save-standard-newsrc-hook)
16032       (save-buffer)
16033       (kill-buffer (current-buffer)))))
16034
16035
16036 ;;; Slave functions.
16037
16038 (defun gnus-slave-save-newsrc ()
16039   (save-excursion
16040     (set-buffer gnus-dribble-buffer)
16041     (let ((slave-name
16042            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16043       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16044
16045 (defun gnus-master-read-slave-newsrc ()
16046   (let ((slave-files
16047          (directory-files
16048           (file-name-directory gnus-current-startup-file)
16049           t (concat
16050              "^" (regexp-quote
16051                   (concat
16052                    (file-name-nondirectory gnus-current-startup-file)
16053                    "-slave-")))
16054           t))
16055         file)
16056     (if (not slave-files)
16057         ()                              ; There are no slave files to read.
16058       (gnus-message 7 "Reading slave newsrcs...")
16059       (save-excursion
16060         (set-buffer (get-buffer-create " *gnus slave*"))
16061         (buffer-disable-undo (current-buffer))
16062         (setq slave-files
16063               (sort (mapcar (lambda (file)
16064                               (list (nth 5 (file-attributes file)) file))
16065                             slave-files)
16066                     (lambda (f1 f2)
16067                       (or (< (car (car f1)) (car (car f2)))
16068                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16069         (while slave-files
16070           (erase-buffer)
16071           (setq file (nth 1 (car slave-files)))
16072           (insert-file-contents file)
16073           (if (condition-case ()
16074                   (progn
16075                     (eval-buffer (current-buffer))
16076                     t)
16077                 (error
16078                  (gnus-message 3 "Possible error in %s" file)
16079                  (ding)
16080                  (sit-for 2)
16081                  nil))
16082               (or gnus-slave ; Slaves shouldn't delete these files.
16083                   (condition-case ()
16084                       (delete-file file)
16085                     (error nil))))
16086           (setq slave-files (cdr slave-files))))
16087       (gnus-message 7 "Reading slave newsrcs...done"))))
16088
16089
16090 ;;; Group description.
16091
16092 (defun gnus-read-all-descriptions-files ()
16093   (let ((methods (cons gnus-select-method 
16094                        (cons gnus-message-archive-method
16095                              gnus-secondary-select-methods))))
16096     (while methods
16097       (gnus-read-descriptions-file (car methods))
16098       (setq methods (cdr methods)))
16099     t))
16100
16101 (defun gnus-read-descriptions-file (&optional method)
16102   (let ((method (or method gnus-select-method)))
16103     ;; We create the hashtable whether we manage to read the desc file
16104     ;; to avoid trying to re-read after a failed read.
16105     (or gnus-description-hashtb
16106         (setq gnus-description-hashtb
16107               (gnus-make-hashtable (length gnus-active-hashtb))))
16108     ;; Mark this method's desc file as read.
16109     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16110                   gnus-description-hashtb)
16111
16112     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16113     (cond
16114      ((not (gnus-check-server method))
16115       (gnus-message 1 "Couldn't open server")
16116       nil)
16117      ((not (gnus-request-list-newsgroups method))
16118       (gnus-message 1 "Couldn't read newsgroups descriptions")
16119       nil)
16120      (t
16121       (let (group)
16122         (save-excursion
16123           (save-restriction
16124             (set-buffer nntp-server-buffer)
16125             (goto-char (point-min))
16126             (if (or (search-forward "\n.\n" nil t)
16127                     (goto-char (point-max)))
16128                 (progn
16129                   (beginning-of-line)
16130                   (narrow-to-region (point-min) (point))))
16131             (goto-char (point-min))
16132             (while (not (eobp))
16133               ;; If we get an error, we set group to 0, which is not a
16134               ;; symbol...
16135               (setq group
16136                     (condition-case ()
16137                         (let ((obarray gnus-description-hashtb))
16138                           ;; Group is set to a symbol interned in this
16139                           ;; hash table.
16140                           (read nntp-server-buffer))
16141                       (error 0)))
16142               (skip-chars-forward " \t")
16143               ;; ...  which leads to this line being effectively ignored.
16144               (and (symbolp group)
16145                    (set group (buffer-substring
16146                                (point) (progn (end-of-line) (point)))))
16147               (forward-line 1))))
16148         (gnus-message 5 "Reading descriptions file...done")
16149         t)))))
16150
16151 (defun gnus-group-get-description (group)
16152   "Get the description of a group by sending XGTITLE to the server."
16153   (when (gnus-request-group-description group)
16154     (save-excursion
16155       (set-buffer nntp-server-buffer)
16156       (goto-char (point-min))
16157       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16158         (match-string 1)))))
16159
16160 ;;;
16161 ;;; Buffering of read articles.
16162 ;;;
16163
16164 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16165 (defvar gnus-backlog-articles nil)
16166 (defvar gnus-backlog-hashtb nil)
16167
16168 (defun gnus-backlog-buffer ()
16169   "Return the backlog buffer."
16170   (or (get-buffer gnus-backlog-buffer)
16171       (save-excursion
16172         (set-buffer (get-buffer-create gnus-backlog-buffer))
16173         (buffer-disable-undo (current-buffer))
16174         (setq buffer-read-only t)
16175         (gnus-add-current-to-buffer-list)
16176         (get-buffer gnus-backlog-buffer))))
16177
16178 (defun gnus-backlog-setup ()
16179   "Initialize backlog variables."
16180   (unless gnus-backlog-hashtb
16181     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16182
16183 (defun gnus-backlog-shutdown ()
16184   "Clear all backlog variables and buffers."
16185   (when (get-buffer gnus-backlog-buffer)
16186     (kill-buffer gnus-backlog-buffer))
16187   (setq gnus-backlog-hashtb nil
16188         gnus-backlog-articles nil))
16189
16190 (defun gnus-backlog-enter-article (group number buffer)
16191   (gnus-backlog-setup)
16192   (let ((ident (intern (concat group ":" (int-to-string number))
16193                        gnus-backlog-hashtb))
16194         b)
16195     (if (memq ident gnus-backlog-articles)
16196         () ; It's already kept.
16197       ;; Remove the oldest article, if necessary.
16198       (and (numberp gnus-keep-backlog)
16199            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16200            (gnus-backlog-remove-oldest-article))
16201       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16202       ;; Insert the new article.
16203       (save-excursion
16204         (set-buffer (gnus-backlog-buffer))
16205         (let (buffer-read-only)
16206           (goto-char (point-max))
16207           (or (bolp) (insert "\n"))
16208           (setq b (point))
16209           (insert-buffer-substring buffer)
16210           ;; Tag the beginning of the article with the ident.
16211           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16212
16213 (defun gnus-backlog-remove-oldest-article ()
16214   (save-excursion
16215     (set-buffer (gnus-backlog-buffer))
16216     (goto-char (point-min))
16217     (if (zerop (buffer-size))
16218         () ; The buffer is empty.
16219       (let ((ident (get-text-property (point) 'gnus-backlog))
16220             buffer-read-only)
16221         ;; Remove the ident from the list of articles.
16222         (when ident
16223           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16224         ;; Delete the article itself.
16225         (delete-region
16226          (point) (next-single-property-change
16227                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16228
16229 (defun gnus-backlog-request-article (group number buffer)
16230   (gnus-backlog-setup)
16231   (let ((ident (intern (concat group ":" (int-to-string number))
16232                        gnus-backlog-hashtb))
16233         beg end)
16234     (when (memq ident gnus-backlog-articles)
16235       ;; It was in the backlog.
16236       (save-excursion
16237         (set-buffer (gnus-backlog-buffer))
16238         (if (not (setq beg (text-property-any
16239                             (point-min) (point-max) 'gnus-backlog
16240                             ident)))
16241             ;; It wasn't in the backlog after all.
16242             (progn
16243               (setq gnus-backlog-articles (delq ident gnus-backlog-articles))
16244               nil)
16245           ;; Find the end (i. e., the beginning of the next article).
16246           (setq end
16247                 (next-single-property-change
16248                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16249       (let ((buffer-read-only nil))
16250         (erase-buffer)
16251         (insert-buffer-substring gnus-backlog-buffer beg end)
16252         t))))
16253
16254 ;; Allow redefinition of Gnus functions.
16255
16256 (gnus-ems-redefine)
16257
16258 (provide 'gnus)
16259
16260 ;;; gnus.el ends here