*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34
35 (eval-when-compile (require 'cl))
36
37 ;; Site dependent variables.  These variables should be defined in
38 ;; paths.el.
39
40 (defvar gnus-default-nntp-server nil
41   "Specify a default NNTP server.
42 This variable should be defined in paths.el, and should never be set
43 by the user.
44 If you want to change servers, you should use `gnus-select-method'.
45 See the documentation to that variable.")
46
47 (defvar gnus-backup-default-subscribed-newsgroups
48   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
49   "Default default new newsgroups the first time Gnus is run.
50 Should be set in paths.el, and shouldn't be touched by the user.")
51
52 (defvar gnus-local-domain nil
53   "Local domain name without a host name.
54 The DOMAINNAME environment variable is used instead if it is defined.
55 If the `system-name' function returns the full Internet name, there is
56 no need to set this variable.")
57
58 (defvar gnus-local-organization nil
59   "String with a description of what organization (if any) the user belongs to.
60 The ORGANIZATION environment variable is used instead if it is defined.
61 If this variable contains a function, this function will be called
62 with the current newsgroup name as the argument.  The function should
63 return a string.
64
65 In any case, if the string (either in the variable, in the environment
66 variable, or returned by the function) is a file name, the contents of
67 this file will be used as the organization.")
68
69 (defvar gnus-use-generic-from nil
70   "If nil, the full host name will be the system name prepended to the domain name.
71 If this is a string, the full host name will be this string.
72 If this is non-nil, non-string, the domain name will be used as the
73 full host name.")
74
75 (defvar gnus-use-generic-path nil
76   "If nil, use the NNTP server name in the Path header.
77 If stringp, use this; if non-nil, use no host name (user name only).")
78
79 ;; Customization variables
80
81 ;; Don't touch this variable.
82 (defvar gnus-nntp-service "nntp"
83   "*NNTP service name (\"nntp\" or 119).
84 This is an obsolete variable, which is scarcely used.  If you use an
85 nntp server for your newsgroup and want to change the port number
86 used to 899, you would say something along these lines:
87
88  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
89
90 (defvar gnus-nntpserver-file "/etc/nntpserver"
91   "*A file with only the name of the nntp server in it.")
92
93 ;; This function is used to check both the environment variable
94 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
95 ;; an nntp server name default.
96 (defun gnus-getenv-nntpserver ()
97   (or (getenv "NNTPSERVER")
98       (and (file-readable-p gnus-nntpserver-file)
99            (save-excursion
100              (set-buffer (get-buffer-create " *gnus nntp*"))
101              (buffer-disable-undo (current-buffer))
102              (insert-file-contents gnus-nntpserver-file)
103              (let ((name (buffer-string)))
104                (prog1
105                    (if (string-match "^[ \t\n]*$" name)
106                        nil
107                      name)
108                  (kill-buffer (current-buffer))))))))
109
110 (defvar gnus-select-method
111   (nconc
112    (list 'nntp (or (condition-case ()
113                        (gnus-getenv-nntpserver)
114                      (error nil))
115                    (if (and gnus-default-nntp-server
116                             (not (string= gnus-default-nntp-server "")))
117                        gnus-default-nntp-server)
118                    (system-name)))
119    (if (or (null gnus-nntp-service)
120            (equal gnus-nntp-service "nntp"))
121        nil
122      (list gnus-nntp-service)))
123   "*Default method for selecting a newsgroup.
124 This variable should be a list, where the first element is how the
125 news is to be fetched, the second is the address.
126
127 For instance, if you want to get your news via NNTP from
128 \"flab.flab.edu\", you could say:
129
130 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
131
132 If you want to use your local spool, say:
133
134 (setq gnus-select-method (list 'nnspool (system-name)))
135
136 If you use this variable, you must set `gnus-nntp-server' to nil.
137
138 There is a lot more to know about select methods and virtual servers -
139 see the manual for details.")
140
141 (defvar gnus-message-archive-method 
142   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
143              (nnfolder-active-file "~/Mail/archive/active")
144              (nnfolder-get-new-mail nil)
145              (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.")
148
149 (defvar gnus-refer-article-method nil
150   "*Preferred method for fetching an article by Message-ID.
151 If you are reading news from the local spool (with nnspool), fetching
152 articles by Message-ID is painfully slow.  By setting this method to an
153 nntp method, you might get acceptable results.
154
155 The value of this variable must be a valid select method as discussed
156 in the documentation of `gnus-select-method'.")
157
158 (defvar gnus-secondary-select-methods nil
159   "*A list of secondary methods that will be used for reading news.
160 This is a list where each element is a complete select method (see
161 `gnus-select-method').
162
163 If, for instance, you want to read your mail with the nnml backend,
164 you could set this variable:
165
166 (setq gnus-secondary-select-methods '((nnml \"\")))")
167
168 (defvar gnus-secondary-servers nil
169   "*List of NNTP servers that the user can choose between interactively.
170 To make Gnus query you for a server, you have to give `gnus' a
171 non-numeric prefix - `C-u M-x gnus', in short.")
172
173 (defvar gnus-nntp-server nil
174   "*The name of the host running the NNTP server.
175 This variable is semi-obsolete.  Use the `gnus-select-method'
176 variable instead.")
177
178 (defvar gnus-startup-file "~/.newsrc"
179   "*Your `.newsrc' file.
180 `.newsrc-SERVER' will be used instead if that exists.")
181
182 (defvar gnus-init-file "~/.gnus"
183   "*Your Gnus elisp startup file.
184 If a file with the .el or .elc suffixes exist, it will be read
185 instead.")
186
187 (defvar gnus-group-faq-directory
188   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
189     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
190     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
191     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
192     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
193     "/ftp@ftp.sunet.se:/pub/usenet/"
194     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
195     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
196     "/ftp@ftp.hk.super.net:/mirror/faqs/")
197   "*Directory where the group FAQs are stored.
198 This will most commonly be on a remote machine, and the file will be
199 fetched by ange-ftp.
200
201 This variable can also be a list of directories.  In that case, the
202 first element in the list will be used by default, and the others will
203 be used as backup sites.
204
205 Note that Gnus uses an aol machine as the default directory.  If this
206 feels fundamentally unclean, just think of it as a way to finally get
207 something of value back from them.
208
209 If the default site is too slow, try one of these:
210
211    North America: mirrors.aol.com                /pub/rtfm/usenet
212                   ftp.seas.gwu.edu               /pub/rtfm
213                   rtfm.mit.edu                   /pub/usenet/news.answers
214    Europe:        ftp.uni-paderborn.de           /pub/FAQ
215                   src.doc.ic.ac.uk               /usenet/news-FAQS
216                   ftp.sunet.se                   /pub/usenet
217    Asia:          nctuccca.edu.tw                /USENET/FAQ
218                   hwarang.postech.ac.kr          /pub/usenet/news.answers
219                   ftp.hk.super.net               /mirror/faqs")
220
221 (defvar gnus-group-archive-directory
222   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
223   "*The address of the (ding) archives.")
224
225 (defvar gnus-group-recent-archive-directory
226   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
227   "*The address of the most recent (ding) articles.")
228
229 (defvar gnus-default-subscribed-newsgroups nil
230   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
231 It should be a list of strings.
232 If it is `t', Gnus will not do anything special the first time it is
233 started; it'll just use the normal newsgroups subscription methods.")
234
235 (defvar gnus-use-cross-reference t
236   "*Non-nil means that cross referenced articles will be marked as read.
237 If nil, ignore cross references.  If t, mark articles as read in
238 subscribed newsgroups.  If neither t nor nil, mark as read in all
239 newsgroups.")
240
241 (defvar gnus-single-article-buffer t
242   "*If non-nil, display all articles in the same buffer.
243 If nil, each group will get its own article buffer.")
244
245 (defvar gnus-use-dribble-file t
246   "*Non-nil means that Gnus will use a dribble file to store user updates.
247 If Emacs should crash without saving the .newsrc files, complete
248 information can be restored from the dribble file.")
249
250 (defvar gnus-dribble-directory nil
251   "*The directory where dribble files will be saved.
252 If this variable is nil, the directory where the .newsrc files are
253 saved will be used.")
254
255 (defvar gnus-asynchronous nil
256   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
257
258 (defvar gnus-kill-summary-on-exit t
259   "*If non-nil, kill the summary buffer when you exit from it.
260 If nil, the summary will become a \"*Dead Summary*\" buffer, and
261 it will be killed sometime later.")
262
263 (defvar gnus-large-newsgroup 200
264   "*The number of articles which indicates a large newsgroup.
265 If the number of articles in a newsgroup is greater than this value,
266 confirmation is required for selecting the newsgroup.")
267
268 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
269 (defvar gnus-no-groups-message "No news is horrible news"
270   "*Message displayed by Gnus when no groups are available.")
271
272 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
273   "*Non-nil means that the default name of a file to save articles in is the group name.
274 If it's nil, the directory form of the group name is used instead.
275
276 If this variable is a list, and the list contains the element
277 `not-score', long file names will not be used for score files; if it
278 contains the element `not-save', long file names will not be used for
279 saving; and if it contains the element `not-kill', long file names
280 will not be used for kill files.")
281
282 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
283   "*Name of the directory articles will be saved in (default \"~/News\").
284 Initialized from the SAVEDIR environment variable.")
285
286 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
287   "*Name of the directory where kill files will be stored (default \"~/News\").
288 Initialized from the SAVEDIR environment variable.")
289
290 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
291   "*A function to save articles in your favorite format.
292 The function must be interactively callable (in other words, it must
293 be an Emacs command).
294
295 Gnus provides the following functions:
296
297 * gnus-summary-save-in-rmail (Rmail format)
298 * gnus-summary-save-in-mail (Unix mail format)
299 * gnus-summary-save-in-folder (MH folder)
300 * gnus-summary-save-in-file (article format).
301 * gnus-summary-save-in-vm (use VM's folder format).")
302
303 (defvar gnus-prompt-before-saving 'always
304   "*This variable says how much prompting is to be done when saving articles.
305 If it is nil, no prompting will be done, and the articles will be
306 saved to the default files.  If this variable is `always', each and
307 every article that is saved will be preceded by a prompt, even when
308 saving large batches of articles.  If this variable is neither nil not
309 `always', there the user will be prompted once for a file name for
310 each invocation of the saving commands.")
311
312 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
313   "*A function generating a file name to save articles in Rmail format.
314 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
315
316 (defvar gnus-mail-save-name (function gnus-plain-save-name)
317   "*A function generating a file name to save articles in Unix mail format.
318 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
319
320 (defvar gnus-folder-save-name (function gnus-folder-save-name)
321   "*A function generating a file name to save articles in MH folder.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
323
324 (defvar gnus-file-save-name (function gnus-numeric-save-name)
325   "*A function generating a file name to save articles in article format.
326 The function is called with NEWSGROUP, HEADERS, and optional
327 LAST-FILE.")
328
329 (defvar gnus-split-methods
330   '((gnus-article-archive-name))
331   "*Variable used to suggest where articles are to be saved.
332 For instance, if you would like to save articles related to Gnus in
333 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
334 you could set this variable to something like:
335
336  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
337    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
338
339 This variable is an alist where the where the key is the match and the
340 value is a list of possible files to save in if the match is non-nil.
341
342 If the match is a string, it is used as a regexp match on the
343 article.  If the match is a symbol, that symbol will be funcalled
344 from the buffer of the article to be saved with the newsgroup as the
345 parameter.  If it is a list, it will be evaled in the same buffer.
346
347 If this form or function returns a string, this string will be used as
348 a possible file name; and if it returns a non-nil list, that list will
349 be used as possible file names.")
350
351 (defvar gnus-move-split-methods nil
352   "*Variable used to suggest where articles are to be moved to.
353 It uses the same syntax as the `gnus-split-methods' variable.")
354
355 (defvar gnus-save-score nil
356   "*If non-nil, save group scoring info.")
357
358 (defvar gnus-use-adaptive-scoring nil
359   "*If non-nil, use some adaptive scoring scheme.")
360
361 (defvar gnus-use-cache nil
362   "*If nil, Gnus will ignore the article cache.
363 If `passive', it will allow entering (and reading) articles
364 explicitly entered into the cache.  If anything else, use the
365 cache to the full extent of the law.")
366
367 (defvar gnus-use-trees nil
368   "*If non-nil, display a thread tree buffer.")
369
370 (defvar gnus-use-grouplens nil
371   "*If non-nil, use GroupLens ratings.")
372
373 (defvar gnus-keep-backlog nil
374   "*If non-nil, Gnus will keep read articles for later re-retrieval.
375 If it is a number N, then Gnus will only keep the last N articles
376 read.  If it is neither nil nor a number, Gnus will keep all read
377 articles.  This is not a good idea.")
378
379 (defvar gnus-use-nocem nil
380   "*If non-nil, Gnus will read NoCeM cancel messages.")
381
382 (defvar gnus-use-demon nil
383   "If non-nil, Gnus might use some demons.")
384
385 (defvar gnus-use-scoring t
386   "*If non-nil, enable scoring.")
387
388 (defvar gnus-use-picons nil
389   "*If non-nil, display picons.")
390
391 (defvar gnus-fetch-old-headers nil
392   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
393 If an unread article in the group refers to an older, already read (or
394 just marked as read) article, the old article will not normally be
395 displayed in the Summary buffer.  If this variable is non-nil, Gnus
396 will attempt to grab the headers to the old articles, and thereby
397 build complete threads.  If it has the value `some', only enough
398 headers to connect otherwise loose threads will be displayed.
399 This variable can also be a number.  In that case, no more than that
400 number of old headers will be fetched.
401
402 The server has to support NOV for any of this to work.")
403
404 ;see gnus-cus.el
405 ;(defvar gnus-visual t
406 ;  "*If non-nil, will do various highlighting.
407 ;If nil, no mouse highlights (or any other highlights) will be
408 ;performed.  This might speed up Gnus some when generating large group
409 ;and summary buffers.")
410
411 (defvar gnus-novice-user t
412   "*Non-nil means that you are a usenet novice.
413 If non-nil, verbose messages may be displayed and confirmations may be
414 required.")
415
416 (defvar gnus-expert-user nil
417   "*Non-nil means that you will never be asked for confirmation about anything.
418 And that means *anything*.")
419
420 (defvar gnus-verbose 7
421   "*Integer that says how verbose Gnus should be.
422 The higher the number, the more messages Gnus will flash to say what
423 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
424 display most important messages; and at ten, Gnus will keep on
425 jabbering all the time.")
426
427 (defvar gnus-keep-same-level nil
428   "*Non-nil means that the next newsgroup after the current will be on the same level.
429 When you type, for instance, `n' after reading the last article in the
430 current newsgroup, you will go to the next newsgroup.  If this variable
431 is nil, the next newsgroup will be the next from the group
432 buffer.
433 If this variable is non-nil, Gnus will either put you in the
434 next newsgroup with the same level, or, if no such newsgroup is
435 available, the next newsgroup with the lowest possible level higher
436 than the current level.
437 If this variable is `best', Gnus will make the next newsgroup the one
438 with the best level.")
439
440 (defvar gnus-summary-make-false-root 'adopt
441   "*nil means that Gnus won't gather loose threads.
442 If the root of a thread has expired or been read in a previous
443 session, the information necessary to build a complete thread has been
444 lost.  Instead of having many small sub-threads from this original thread
445 scattered all over the summary buffer, Gnus can gather them.
446
447 If non-nil, Gnus will try to gather all loose sub-threads from an
448 original thread into one large thread.
449
450 If this variable is non-nil, it should be one of `none', `adopt',
451 `dummy' or `empty'.
452
453 If this variable is `none', Gnus will not make a false root, but just
454 present the sub-threads after another.
455 If this variable is `dummy', Gnus will create a dummy root that will
456 have all the sub-threads as children.
457 If this variable is `adopt', Gnus will make one of the \"children\"
458 the parent and mark all the step-children as such.
459 If this variable is `empty', the \"children\" are printed with empty
460 subject fields.  (Or rather, they will be printed with a string
461 given by the `gnus-summary-same-subject' variable.)")
462
463 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
464   "*A regexp to match subjects to be excluded from loose thread gathering.
465 As loose thread gathering is done on subjects only, that means that
466 there can be many false gatherings performed.  By rooting out certain
467 common subjects, gathering might become saner.")
468
469 (defvar gnus-summary-gather-subject-limit nil
470   "*Maximum length of subject comparisons when gathering loose threads.
471 Use nil to compare full subjects.  Setting this variable to a low
472 number will help gather threads that have been corrupted by
473 newsreaders chopping off subject lines, but it might also mean that
474 unrelated articles that have subject that happen to begin with the
475 same few characters will be incorrectly gathered.
476
477 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
478 comparing subjects.")
479
480 (defvar gnus-simplify-ignored-prefixes nil
481   "*Regexp, matches for which are removed from subject lines when simplifying.")
482
483 (defvar gnus-build-sparse-threads nil
484   "*If non-nil, fill in the gaps in threads.
485 If `some', only fill in the gaps that are needed to tie loose threads
486 together.  If `more', fill in all leaf nodes that Gnus can find.  If
487 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
488
489 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
490   "Function used for gathering loose threads.
491 There are two pre-defined functions: `gnus-gather-threads-by-subject',
492 which only takes Subjects into consideration; and
493 `gnus-gather-threads-by-references', which compared the References
494 headers of the articles to find matches.")
495
496 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
497 (defvar gnus-summary-same-subject ""
498   "*String indicating that the current article has the same subject as the previous.
499 This variable will only be used if the value of
500 `gnus-summary-make-false-root' is `empty'.")
501
502 (defvar gnus-summary-goto-unread t
503   "*If non-nil, marking commands will go to the next unread article.
504 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
505 whether it is read or not.")
506
507 (defvar gnus-group-goto-unread t
508   "*If non-nil, movement commands will go to the next unread and subscribed group.")
509
510 (defvar gnus-goto-next-group-when-activating t
511   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
512
513 (defvar gnus-check-new-newsgroups t
514   "*Non-nil means that Gnus will add new newsgroups at startup.
515 If this variable is `ask-server', Gnus will ask the server for new
516 groups since the last time it checked.  This means that the killed list
517 is no longer necessary, so you could set `gnus-save-killed-list' to
518 nil.
519
520 A variant is to have this variable be a list of select methods.  Gnus
521 will then use the `ask-server' method on all these select methods to
522 query for new groups from all those servers.
523
524 Eg.
525   (setq gnus-check-new-newsgroups
526         '((nntp \"some.server\") (nntp \"other.server\")))
527
528 If this variable is nil, then you have to tell Gnus explicitly to
529 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
530
531 (defvar gnus-check-bogus-newsgroups nil
532   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
533 If this variable is nil, then you have to tell Gnus explicitly to
534 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
535
536 (defvar gnus-read-active-file t
537   "*Non-nil means that Gnus will read the entire active file at startup.
538 If this variable is nil, Gnus will only know about the groups in your
539 `.newsrc' file.
540
541 If this variable is `some', Gnus will try to only read the relevant
542 parts of the active file from the server.  Not all servers support
543 this, and it might be quite slow with other servers, but this should
544 generally be faster than both the t and nil value.
545
546 If you set this variable to nil or `some', you probably still want to
547 be told about new newsgroups that arrive.  To do that, set
548 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
549 properly with all servers.")
550
551 (defvar gnus-level-subscribed 5
552   "*Groups with levels less than or equal to this variable are subscribed.")
553
554 (defvar gnus-level-unsubscribed 7
555   "*Groups with levels less than or equal to this variable are unsubscribed.
556 Groups with levels less than `gnus-level-subscribed', which should be
557 less than this variable, are subscribed.")
558
559 (defvar gnus-level-zombie 8
560   "*Groups with this level are zombie groups.")
561
562 (defvar gnus-level-killed 9
563   "*Groups with this level are killed.")
564
565 (defvar gnus-level-default-subscribed 3
566   "*New subscribed groups will be subscribed at this level.")
567
568 (defvar gnus-level-default-unsubscribed 6
569   "*New unsubscribed groups will be unsubscribed at this level.")
570
571 (defvar gnus-activate-level (1+ gnus-level-subscribed)
572   "*Groups higher than this level won't be activated on startup.
573 Setting this variable to something log might save lots of time when
574 you have many groups that you aren't interested in.")
575
576 (defvar gnus-activate-foreign-newsgroups 4
577   "*If nil, Gnus will not check foreign newsgroups at startup.
578 If it is non-nil, it should be a number between one and nine.  Foreign
579 newsgroups that have a level lower or equal to this number will be
580 activated on startup.  For instance, if you want to active all
581 subscribed newsgroups, but not the rest, you'd set this variable to
582 `gnus-level-subscribed'.
583
584 If you subscribe to lots of newsgroups from different servers, startup
585 might take a while.  By setting this variable to nil, you'll save time,
586 but you won't be told how many unread articles there are in the
587 groups.")
588
589 (defvar gnus-save-newsrc-file t
590   "*Non-nil means that Gnus will save the `.newsrc' file.
591 Gnus always saves its own startup file, which is called
592 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
593 be readily understood by other newsreaders.  If you don't plan on
594 using other newsreaders, set this variable to nil to save some time on
595 exit.")
596
597 (defvar gnus-save-killed-list t
598   "*If non-nil, save the list of killed groups to the startup file.
599 If you set this variable to nil, you'll save both time (when starting
600 and quitting) and space (both memory and disk), but it will also mean
601 that Gnus has no record of which groups are new and which are old, so
602 the automatic new newsgroups subscription methods become meaningless.
603
604 You should always set `gnus-check-new-newsgroups' to `ask-server' or
605 nil if you set this variable to nil.")
606
607 (defvar gnus-interactive-catchup t
608   "*If non-nil, require your confirmation when catching up a group.")
609
610 (defvar gnus-interactive-post t
611   "*If non-nil, group name will be asked for when posting.")
612
613 (defvar gnus-interactive-exit t
614   "*If non-nil, require your confirmation when exiting Gnus.")
615
616 (defvar gnus-kill-killed t
617   "*If non-nil, Gnus will apply kill files to already killed articles.
618 If it is nil, Gnus will never apply kill files to articles that have
619 already been through the scoring process, which might very well save lots
620 of time.")
621
622 (defvar gnus-extract-address-components 'gnus-extract-address-components
623   "*Function for extracting address components from a From header.
624 Two pre-defined function exist: `gnus-extract-address-components',
625 which is the default, quite fast, and too simplistic solution, and
626 `mail-extract-address-components', which works much better, but is
627 slower.")
628
629 (defvar gnus-summary-default-score 0
630   "*Default article score level.
631 If this variable is nil, scoring will be disabled.")
632
633 (defvar gnus-summary-zcore-fuzz 0
634   "*Fuzziness factor for the zcore in the summary buffer.
635 Articles with scores closer than this to `gnus-summary-default-score'
636 will not be marked.")
637
638 (defvar gnus-simplify-subject-fuzzy-regexp nil
639   "*Strings to be removed when doing fuzzy matches.
640 This can either be a regular expression or list of regular expressions
641 that will be removed from subject strings if fuzzy subject
642 simplification is selected.")
643
644 (defvar gnus-permanently-visible-groups nil
645   "*Regexp to match groups that should always be listed in the group buffer.
646 This means that they will still be listed when there are no unread
647 articles in the groups.")
648
649 (defvar gnus-list-groups-with-ticked-articles t
650   "*If non-nil, list groups that have only ticked articles.
651 If nil, only list groups that have unread articles.")
652
653 (defvar gnus-group-default-list-level gnus-level-subscribed
654   "*Default listing level.
655 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
656
657 (defvar gnus-group-use-permanent-levels nil
658   "*If non-nil, once you set a level, Gnus will use this level.")
659
660 (defvar gnus-group-list-inactive-groups t
661   "*If non-nil, inactive groups will be listed.")
662
663 (defvar gnus-show-mime nil
664   "*If non-nil, do mime processing of articles.
665 The articles will simply be fed to the function given by
666 `gnus-show-mime-method'.")
667
668 (defvar gnus-strict-mime t
669   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
670
671 (defvar gnus-show-mime-method 'metamail-buffer
672   "*Function to process a MIME message.
673 The function is called from the article buffer.")
674
675 (defvar gnus-decode-encoded-word-method (lambda ())
676   "*Function to decode a MIME encoded-words.
677 The function is called from the article buffer.")
678
679 (defvar gnus-show-threads t
680   "*If non-nil, display threads in summary mode.")
681
682 (defvar gnus-thread-hide-subtree nil
683   "*If non-nil, hide all threads initially.
684 If threads are hidden, you have to run the command
685 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
686 to expose hidden threads.")
687
688 (defvar gnus-thread-hide-killed t
689   "*If non-nil, hide killed threads automatically.")
690
691 (defvar gnus-thread-ignore-subject nil
692   "*If non-nil, ignore subjects and do all threading based on the Reference header.
693 If nil, which is the default, articles that have different subjects
694 from their parents will start separate threads.")
695
696 (defvar gnus-thread-operation-ignore-subject t
697   "*If non-nil, subjects will be ignored when doing thread commands.
698 This affects commands like `gnus-summary-kill-thread' and
699 `gnus-summary-lower-thread'.
700
701 If this variable is nil, articles in the same thread with different
702 subjects will not be included in the operation in question.  If this
703 variable is `fuzzy', only articles that have subjects that are fuzzily
704 equal will be included.")
705
706 (defvar gnus-thread-indent-level 4
707   "*Number that says how much each sub-thread should be indented.")
708
709 (defvar gnus-ignored-newsgroups
710   (purecopy (mapconcat 'identity
711                        '("^to\\."       ; not "real" groups
712                          "^[0-9. \t]+ " ; all digits in name
713                          "[][\"#'()]"   ; bogus characters
714                          )
715                        "\\|"))
716   "*A regexp to match uninteresting newsgroups in the active file.
717 Any lines in the active file matching this regular expression are
718 removed from the newsgroup list before anything else is done to it,
719 thus making them effectively non-existent.")
720
721 (defvar gnus-ignored-headers
722   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
723   "*All headers that match this regexp will be hidden.
724 This variable can also be a list of regexps of headers to be ignored.
725 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
726
727 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
728   "*All headers that do not match this regexp will be hidden.
729 This variable can also be a list of regexp of headers to remain visible.
730 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
731
732 (defvar gnus-sorted-header-list
733   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
734     "^Cc:" "^Date:" "^Organization:")
735   "*This variable is a list of regular expressions.
736 If it is non-nil, headers that match the regular expressions will
737 be placed first in the article buffer in the sequence specified by
738 this list.")
739
740 (defvar gnus-boring-article-headers
741   '(empty followup-to reply-to)
742   "*Headers that are only to be displayed if they have interesting data.
743 Possible values in this list are `empty', `newsgroups', `followup-to',
744 `reply-to', and `date'.")
745
746 (defvar gnus-show-all-headers nil
747   "*If non-nil, don't hide any headers.")
748
749 (defvar gnus-save-all-headers t
750   "*If non-nil, don't remove any headers before saving.")
751
752 (defvar gnus-saved-headers gnus-visible-headers
753   "*Headers to keep if `gnus-save-all-headers' is nil.
754 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
755 If that variable is nil, however, all headers that match this regexp
756 will be kept while the rest will be deleted before saving.")
757
758 (defvar gnus-inhibit-startup-message nil
759   "*If non-nil, the startup message will not be displayed.")
760
761 (defvar gnus-signature-separator "^-- *$"
762   "Regexp matching signature separator.")
763
764 (defvar gnus-signature-limit nil
765   "Provide a limit to what is considered a signature.
766 If it is a number, no signature may not be longer (in characters) than
767 that number.  If it is a function, the function will be called without
768 any parameters, and if it returns nil, there is no signature in the
769 buffer.  If it is a string, it will be used as a regexp.  If it
770 matches, the text in question is not a signature.")
771
772 (defvar gnus-auto-extend-newsgroup t
773   "*If non-nil, extend newsgroup forward and backward when requested.")
774
775 (defvar gnus-auto-select-first t
776   "*If nil, don't select the first unread article when entering a group.
777 If this variable is `best', select the highest-scored unread article
778 in the group.  If neither nil nor `best', select the first unread
779 article.
780
781 If you want to prevent automatic selection of the first unread article
782 in some newsgroups, set the variable to nil in
783 `gnus-select-group-hook'.")
784
785 (defvar gnus-auto-select-next t
786   "*If non-nil, offer to go to the next group from the end of the previous.
787 If the value is t and the next newsgroup is empty, Gnus will exit
788 summary mode and go back to group mode.  If the value is neither nil
789 nor t, Gnus will select the following unread newsgroup.  In
790 particular, if the value is the symbol `quietly', the next unread
791 newsgroup will be selected without any confirmation, and if it is
792 `almost-quietly', the next group will be selected without any
793 confirmation if you are located on the last article in the group.
794 Finally, if this variable is `slightly-quietly', the `Z n' command
795 will go to the next group without confirmation.")
796
797 (defvar gnus-auto-select-same nil
798   "*If non-nil, select the next article with the same subject.")
799
800 (defvar gnus-summary-check-current nil
801   "*If non-nil, consider the current article when moving.
802 The \"unread\" movement commands will stay on the same line if the
803 current article is unread.")
804
805 (defvar gnus-auto-center-summary t
806   "*If non-nil, always center the current summary buffer.
807 In particular, if `vertical' do only vertical recentering.  If non-nil
808 and non-`vertical', do both horizontal and vertical recentering.")
809
810 (defvar gnus-break-pages t
811   "*If non-nil, do page breaking on articles.
812 The page delimiter is specified by the `gnus-page-delimiter'
813 variable.")
814
815 (defvar gnus-page-delimiter "^\^L"
816   "*Regexp describing what to use as article page delimiters.
817 The default value is \"^\^L\", which is a form linefeed at the
818 beginning of a line.")
819
820 (defvar gnus-use-full-window t
821   "*If non-nil, use the entire Emacs screen.")
822
823 (defvar gnus-window-configuration nil
824   "Obsolete variable.  See `gnus-buffer-configuration'.")
825
826 (defvar gnus-window-min-width 2
827   "*Minimum width of Gnus buffers.")
828
829 (defvar gnus-window-min-height 1
830   "*Minimum height of Gnus buffers.")
831
832 (defvar gnus-buffer-configuration
833   '((group
834      (vertical 1.0
835                (group 1.0 point)
836                (if gnus-carpal '(group-carpal 4))))
837     (summary
838      (vertical 1.0
839                (summary 1.0 point)
840                (if gnus-carpal '(summary-carpal 4))))
841     (article
842      (cond 
843       (gnus-use-picons
844        '(frame 1.0
845                (vertical 1.0
846                          (summary 0.25 point)
847                          (if gnus-carpal '(summary-carpal 4))
848                          (article 1.0))
849                (vertical ((height . 5) (width . 15)
850                           (user-position . t)
851                           (left . -1) (top . 1))
852                          (picons 1.0))))
853       (gnus-use-trees
854        '(vertical 1.0
855                   (summary 0.25 point)
856                   (tree 0.25)
857                   (article 1.0)))
858       (t
859        '(vertical 1.0
860                  (summary 0.25 point)
861                  (if gnus-carpal '(summary-carpal 4))
862                  (if gnus-use-trees '(tree 0.25))
863                  (article 1.0)))))
864     (server
865      (vertical 1.0
866                (server 1.0 point)
867                (if gnus-carpal '(server-carpal 2))))
868     (browse
869      (vertical 1.0
870                (browse 1.0 point)
871                (if gnus-carpal '(browse-carpal 2))))
872     (group-mail
873      (vertical 1.0
874                (mail 1.0 point)))
875     (group-post
876      (vertical 1.0
877                (post 1.0 point)))
878     (summary-mail
879      (vertical 1.0
880                (mail 1.0 point)))
881     (summary-reply
882      (vertical 1.0
883                (article-copy 0.5)
884                (mail 1.0 point)))
885     (pick
886      (vertical 1.0
887                (article 1.0 point)))
888     (info
889      (vertical 1.0
890                (info 1.0 point)))
891     (summary-faq
892      (vertical 1.0
893                (summary 0.25)
894                (faq 1.0 point)))
895     (edit-group
896      (vertical 1.0
897                (group 0.5)
898                (edit-group 1.0 point)))
899     (edit-server
900      (vertical 1.0
901                (server 0.5)
902                (edit-server 1.0 point)))
903     (edit-score
904      (vertical 1.0
905                (summary 0.25)
906                (edit-score 1.0 point)))
907     (post
908      (vertical 1.0
909                (post 1.0 point)))
910     (reply
911      (vertical 1.0
912                (article-copy 0.5)
913                (mail 1.0 point)))
914     (mail-forward
915      (vertical 1.0
916                (mail 1.0 point)))
917     (post-forward
918      (vertical 1.0
919                (post 1.0 point)))
920     (reply-yank
921      (vertical 1.0
922                (mail 1.0 point)))
923     (mail-bounce
924      (vertical 1.0
925                (article 0.5)
926                (mail 1.0 point)))
927     (draft
928      (vertical 1.0
929                (draft 1.0 point)))
930     (pipe
931      (vertical 1.0
932                (summary 0.25 point)
933                (if gnus-carpal '(summary-carpal 4))
934                ("*Shell Command Output*" 1.0)))
935     (followup
936      (vertical 1.0
937                (article-copy 0.5)
938                (post 1.0 point)))
939     (followup-yank
940      (vertical 1.0
941                (post 1.0 point))))
942   "Window configuration for all possible Gnus buffers.
943 This variable is a list of lists.  Each of these lists has a NAME and
944 a RULE.  The NAMEs are commonsense names like `group', which names a
945 rule used when displaying the group buffer; `summary', which names a
946 rule for what happens when you enter a group and do not display an
947 article buffer; and so on.  See the value of this variable for a
948 complete list of NAMEs.
949
950 Each RULE is a list of vectors.  The first element in this vector is
951 the name of the buffer to be displayed; the second element is the
952 percentage of the screen this buffer is to occupy (a number in the
953 0.0-0.99 range); the optional third element is `point', which should
954 be present to denote which buffer point is to go to after making this
955 buffer configuration.")
956
957 (defvar gnus-window-to-buffer
958   '((group . gnus-group-buffer)
959     (summary . gnus-summary-buffer)
960     (article . gnus-article-buffer)
961     (server . gnus-server-buffer)
962     (browse . "*Gnus Browse Server*")
963     (edit-group . gnus-group-edit-buffer)
964     (edit-server . gnus-server-edit-buffer)
965     (group-carpal . gnus-carpal-group-buffer)
966     (summary-carpal . gnus-carpal-summary-buffer)
967     (server-carpal . gnus-carpal-server-buffer)
968     (browse-carpal . gnus-carpal-browse-buffer)
969     (edit-score . gnus-score-edit-buffer)
970     (mail . gnus-mail-buffer)
971     (post . gnus-post-news-buffer)
972     (faq . gnus-faq-buffer)
973     (picons . "*Picons*")
974     (tree . gnus-tree-buffer)
975     (info . gnus-info-buffer)
976     (article-copy . gnus-article-copy)
977     (draft . gnus-draft-buffer))
978   "Mapping from short symbols to buffer names or buffer variables.")
979
980 (defvar gnus-carpal nil
981   "*If non-nil, display clickable icons.")
982
983 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
984   "*Function called with a group name when new group is detected.
985 A few pre-made functions are supplied: `gnus-subscribe-randomly'
986 inserts new groups at the beginning of the list of groups;
987 `gnus-subscribe-alphabetically' inserts new groups in strict
988 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
989 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
990 for your decision; `gnus-subscribe-killed' kills all new groups.")
991
992 ;; Suggested by a bug report by Hallvard B Furuseth.
993 ;; <h.b.furuseth@usit.uio.no>.
994 (defvar gnus-subscribe-options-newsgroup-method
995   (function gnus-subscribe-alphabetically)
996   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
997 If, for instance, you want to subscribe to all newsgroups in the
998 \"no\" and \"alt\" hierarchies, you'd put the following in your
999 .newsrc file:
1000
1001 options -n no.all alt.all
1002
1003 Gnus will the subscribe all new newsgroups in these hierarchies with
1004 the subscription method in this variable.")
1005
1006 (defvar gnus-subscribe-hierarchical-interactive nil
1007   "*If non-nil, Gnus will offer to subscribe hierarchically.
1008 When a new hierarchy appears, Gnus will ask the user:
1009
1010 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1011
1012 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1013 subscribe to all newsgroups in the hierarchy and `s' will skip this
1014 hierarchy in its entirety.")
1015
1016 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1017   "*Function used for sorting the group buffer.
1018 This function will be called with group info entries as the arguments
1019 for the groups to be sorted.  Pre-made functions include
1020 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1021 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1022 `gnus-group-sort-by-rank'.
1023
1024 This variable can also be a list of sorting functions.  In that case,
1025 the most significant sort function should be the last function in the
1026 list.")
1027
1028 ;; Mark variables suggested by Thomas Michanek
1029 ;; <Thomas.Michanek@telelogic.se>.
1030 (defvar gnus-unread-mark ? 
1031   "*Mark used for unread articles.")
1032 (defvar gnus-ticked-mark ?!
1033   "*Mark used for ticked articles.")
1034 (defvar gnus-dormant-mark ??
1035   "*Mark used for dormant articles.")
1036 (defvar gnus-del-mark ?r
1037   "*Mark used for del'd articles.")
1038 (defvar gnus-read-mark ?R
1039   "*Mark used for read articles.")
1040 (defvar gnus-expirable-mark ?E
1041   "*Mark used for expirable articles.")
1042 (defvar gnus-killed-mark ?K
1043   "*Mark used for killed articles.")
1044 (defvar gnus-souped-mark ?F
1045   "*Mark used for killed articles.")
1046 (defvar gnus-kill-file-mark ?X
1047   "*Mark used for articles killed by kill files.")
1048 (defvar gnus-low-score-mark ?Y
1049   "*Mark used for articles with a low score.")
1050 (defvar gnus-catchup-mark ?C
1051   "*Mark used for articles that are caught up.")
1052 (defvar gnus-replied-mark ?A
1053   "*Mark used for articles that have been replied to.")
1054 (defvar gnus-cached-mark ?*
1055   "*Mark used for articles that are in the cache.")
1056 (defvar gnus-saved-mark ?S
1057   "*Mark used for articles that have been saved to.")
1058 (defvar gnus-process-mark ?#
1059   "*Process mark.")
1060 (defvar gnus-ancient-mark ?O
1061   "*Mark used for ancient articles.")
1062 (defvar gnus-sparse-mark ?Q
1063   "*Mark used for sparsely reffed articles.")
1064 (defvar gnus-canceled-mark ?G
1065   "*Mark used for canceled articles.")
1066 (defvar gnus-score-over-mark ?+
1067   "*Score mark used for articles with high scores.")
1068 (defvar gnus-score-below-mark ?-
1069   "*Score mark used for articles with low scores.")
1070 (defvar gnus-empty-thread-mark ? 
1071   "*There is no thread under the article.")
1072 (defvar gnus-not-empty-thread-mark ?=
1073   "*There is a thread under the article.")
1074
1075 (defvar gnus-view-pseudo-asynchronously nil
1076   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1077
1078 (defvar gnus-view-pseudos nil
1079   "*If `automatic', pseudo-articles will be viewed automatically.
1080 If `not-confirm', pseudos will be viewed automatically, and the user
1081 will not be asked to confirm the command.")
1082
1083 (defvar gnus-view-pseudos-separately t
1084   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1085 If nil, all files that use the same viewing command will be given as a
1086 list of parameters to that command.")
1087
1088 (defvar gnus-insert-pseudo-articles t
1089   "*If non-nil, insert pseudo-articles when decoding articles.")
1090
1091 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1092   "*Format of group lines.
1093 It works along the same lines as a normal formatting string,
1094 with some simple extensions.
1095
1096 %M    Only marked articles (character, \"*\" or \" \")
1097 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1098 %L    Level of subscribedness (integer)
1099 %N    Number of unread articles (integer)
1100 %I    Number of dormant articles (integer)
1101 %i    Number of ticked and dormant (integer)
1102 %T    Number of ticked articles (integer)
1103 %R    Number of read articles (integer)
1104 %t    Total number of articles (integer)
1105 %y    Number of unread, unticked articles (integer)
1106 %G    Group name (string)
1107 %g    Qualified group name (string)
1108 %D    Group description (string)
1109 %s    Select method (string)
1110 %o    Moderated group (char, \"m\")
1111 %p    Process mark (char)
1112 %O    Moderated group (string, \"(m)\" or \"\")
1113 %P    Topic indentation (string)
1114 %l    Whether there are GroupLens predictions for this group (string)
1115 %n    Select from where (string)
1116 %z    A string that look like `<%s:%n>' if a foreign select method is used
1117 %u    User defined specifier.  The next character in the format string should
1118       be a letter.  Gnus will call the function gnus-user-format-function-X,
1119       where X is the letter following %u.  The function will be passed the
1120       current header as argument.  The function should return a string, which
1121       will be inserted into the buffer just like information from any other
1122       group specifier.
1123
1124 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1125 the mouse point move inside the area.  There can only be one such area.
1126
1127 Note that this format specification is not always respected.  For
1128 reasons of efficiency, when listing killed groups, this specification
1129 is ignored altogether.  If the spec is changed considerably, your
1130 output may end up looking strange when listing both alive and killed
1131 groups.
1132
1133 If you use %o or %O, reading the active file will be slower and quite
1134 a bit of extra memory will be used. %D will also worsen performance.
1135 Also note that if you change the format specification to include any
1136 of these specs, you must probably re-start Gnus to see them go into
1137 effect.")
1138
1139 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1140   "*The format specification of the lines in the summary buffer.
1141
1142 It works along the same lines as a normal formatting string,
1143 with some simple extensions.
1144
1145 %N   Article number, left padded with spaces (string)
1146 %S   Subject (string)
1147 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1148 %n   Name of the poster (string)
1149 %a   Extracted name of the poster (string)
1150 %A   Extracted address of the poster (string)
1151 %F   Contents of the From: header (string)
1152 %x   Contents of the Xref: header (string)
1153 %D   Date of the article (string)
1154 %d   Date of the article (string) in DD-MMM format
1155 %M   Message-id of the article (string)
1156 %r   References of the article (string)
1157 %c   Number of characters in the article (integer)
1158 %L   Number of lines in the article (integer)
1159 %I   Indentation based on thread level (a string of spaces)
1160 %T   A string with two possible values: 80 spaces if the article
1161      is on thread level two or larger and 0 spaces on level one
1162 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1163 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1164 %[   Opening bracket (character, \"[\" or \"<\")
1165 %]   Closing bracket (character, \"]\" or \">\")
1166 %>   Spaces of length thread-level (string)
1167 %<   Spaces of length (- 20 thread-level) (string)
1168 %i   Article score (number)
1169 %z   Article zcore (character)
1170 %t   Number of articles under the current thread (number).
1171 %e   Whether the thread is empty or not (character).
1172 %l   GroupLens score (number)
1173 %u   User defined specifier.  The next character in the format string should
1174      be a letter.  Gnus will call the function gnus-user-format-function-X,
1175      where X is the letter following %u.  The function will be passed the
1176      current header as argument.  The function should return a string, which
1177      will be inserted into the summary just like information from any other
1178      summary specifier.
1179
1180 Text between %( and %) will be highlighted with `gnus-mouse-face'
1181 when the mouse point is placed inside the area.  There can only be one
1182 such area.
1183
1184 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1185 with care.  For reasons of efficiency, Gnus will compute what column
1186 these characters will end up in, and \"hard-code\" that.  This means that
1187 it is illegal to have these specs after a variable-length spec.  Well,
1188 you might not be arrested, but your summary buffer will look strange,
1189 which is bad enough.
1190
1191 The smart choice is to have these specs as for to the left as
1192 possible.
1193
1194 This restriction may disappear in later versions of Gnus.")
1195
1196 (defvar gnus-summary-dummy-line-format
1197   "*  %(:                          :%) %S\n"
1198   "*The format specification for the dummy roots in the summary buffer.
1199 It works along the same lines as a normal formatting string,
1200 with some simple extensions.
1201
1202 %S  The subject")
1203
1204 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1205   "*The format specification for the summary mode line.
1206 It works along the same lines as a normal formatting string,
1207 with some simple extensions:
1208
1209 %G  Group name
1210 %p  Unprefixed group name
1211 %A  Current article number
1212 %V  Gnus version
1213 %U  Number of unread articles in the group
1214 %e  Number of unselected articles in the group
1215 %Z  A string with unread/unselected article counts
1216 %g  Shortish group name
1217 %S  Subject of the current article
1218 %u  User-defined spec
1219 %s  Current score file name
1220 %d  Number of dormant articles
1221 %r  Number of articles that have been marked as read in this session
1222 %E  Number of articles expunged by the score files")
1223
1224 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1225   "*The format specification for the article mode line.
1226 See `gnus-summary-mode-line-format' for a closer description.")
1227
1228 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1229   "*The format specification for the group mode line.
1230 It works along the same lines as a normal formatting string,
1231 with some simple extensions:
1232
1233 %S   The native news server.
1234 %M   The native select method.")
1235
1236 (defvar gnus-valid-select-methods
1237   '(("nntp" post address prompt-address)
1238     ("nnspool" post address)
1239     ("nnvirtual" post-mail virtual prompt-address)
1240     ("nnmbox" mail respool address)
1241     ("nnml" mail respool address)
1242     ("nnmh" mail respool address)
1243     ("nndir" post-mail prompt-address address)
1244     ("nneething" none address prompt-address)
1245     ("nndoc" none address prompt-address)
1246     ("nnbabyl" mail address respool)
1247     ("nnkiboze" post address virtual)
1248     ("nnsoup" post-mail address)
1249     ("nndraft" post-mail)
1250     ("nnfolder" mail respool address))
1251   "An alist of valid select methods.
1252 The first element of each list lists should be a string with the name
1253 of the select method.  The other elements may be be the category of
1254 this method (ie. `post', `mail', `none' or whatever) or other
1255 properties that this method has (like being respoolable).
1256 If you implement a new select method, all you should have to change is
1257 this variable.  I think.")
1258
1259 (defvar gnus-updated-mode-lines '(group article summary tree)
1260   "*List of buffers that should update their mode lines.
1261 The list may contain the symbols `group', `article' and `summary'.  If
1262 the corresponding symbol is present, Gnus will keep that mode line
1263 updated with information that may be pertinent.
1264 If this variable is nil, screen refresh may be quicker.")
1265
1266 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1267 (defvar gnus-mode-non-string-length nil
1268   "*Max length of mode-line non-string contents.
1269 If this is nil, Gnus will take space as is needed, leaving the rest
1270 of the modeline intact.")
1271
1272 ;see gnus-cus.el
1273 ;(defvar gnus-mouse-face 'highlight
1274 ;  "*Face used for mouse highlighting in Gnus.
1275 ;No mouse highlights will be done if `gnus-visual' is nil.")
1276
1277 (defvar gnus-summary-mark-below nil
1278   "*Mark all articles with a score below this variable as read.
1279 This variable is local to each summary buffer and usually set by the
1280 score file.")
1281
1282 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1283   "*List of functions used for sorting articles in the summary buffer.
1284 This variable is only used when not using a threaded display.")
1285
1286 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1287   "*List of functions used for sorting threads in the summary buffer.
1288 By default, threads are sorted by article number.
1289
1290 Each function takes two threads and return non-nil if the first thread
1291 should be sorted before the other.  If you use more than one function,
1292 the primary sort function should be the last.  You should probably
1293 always include `gnus-thread-sort-by-number' in the list of sorting
1294 functions -- preferably first.
1295
1296 Ready-mady functions include `gnus-thread-sort-by-number',
1297 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1298 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1299 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1300
1301 (defvar gnus-thread-score-function '+
1302   "*Function used for calculating the total score of a thread.
1303
1304 The function is called with the scores of the article and each
1305 subthread and should then return the score of the thread.
1306
1307 Some functions you can use are `+', `max', or `min'.")
1308
1309 (defvar gnus-summary-expunge-below nil
1310   "All articles that have a score less than this variable will be expunged.")
1311
1312 (defvar gnus-thread-expunge-below nil
1313   "All threads that have a total score less than this variable will be expunged.
1314 See `gnus-thread-score-function' for en explanation of what a
1315 \"thread score\" is.")
1316
1317 (defvar gnus-auto-subscribed-groups
1318   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1319   "*All new groups that match this regexp will be subscribed automatically.
1320 Note that this variable only deals with new groups.  It has no effect
1321 whatsoever on old groups.")
1322
1323 (defvar gnus-options-subscribe nil
1324   "*All new groups matching this regexp will be subscribed unconditionally.
1325 Note that this variable deals only with new newsgroups.  This variable
1326 does not affect old newsgroups.")
1327
1328 (defvar gnus-options-not-subscribe nil
1329   "*All new groups matching this regexp will be ignored.
1330 Note that this variable deals only with new newsgroups.  This variable
1331 does not affect old (already subscribed) newsgroups.")
1332
1333 (defvar gnus-auto-expirable-newsgroups nil
1334   "*Groups in which to automatically mark read articles as expirable.
1335 If non-nil, this should be a regexp that should match all groups in
1336 which to perform auto-expiry.  This only makes sense for mail groups.")
1337
1338 (defvar gnus-total-expirable-newsgroups nil
1339   "*Groups in which to perform expiry of all read articles.
1340 Use with extreme caution.  All groups that match this regexp will be
1341 expiring - which means that all read articles will be deleted after
1342 (say) one week.  (This only goes for mail groups and the like, of
1343 course.)")
1344
1345 (defvar gnus-group-uncollapsed-levels 1
1346   "Number of group name elements to leave alone when making a short group name.")
1347
1348 (defvar gnus-hidden-properties '(invisible t intangible t)
1349   "Property list to use for hiding text.")
1350
1351 (defvar gnus-modtime-botch nil
1352   "*Non-nil means .newsrc should be deleted prior to save.  
1353 Its use is due to the bogus appearance that .newsrc was modified on
1354 disc.")
1355
1356 ;; Hooks.
1357
1358 (defvar gnus-group-mode-hook nil
1359   "*A hook for Gnus group mode.")
1360
1361 (defvar gnus-summary-mode-hook nil
1362   "*A hook for Gnus summary mode.
1363 This hook is run before any variables are set in the summary buffer.")
1364
1365 (defvar gnus-article-mode-hook nil
1366   "*A hook for Gnus article mode.")
1367
1368 (defvar gnus-summary-prepare-exit-hook nil
1369   "*A hook called when preparing to exit from the summary buffer.
1370 It calls `gnus-summary-expire-articles' by default.")
1371 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1372
1373 (defvar gnus-summary-exit-hook nil
1374   "*A hook called on exit from the summary buffer.")
1375
1376 (defvar gnus-group-catchup-group-hook nil
1377   "*A hook run when catching up a group from the group buffer.")
1378
1379 (defvar gnus-open-server-hook nil
1380   "*A hook called just before opening connection to the news server.")
1381
1382 (defvar gnus-load-hook nil
1383   "*A hook run while Gnus is loaded.")
1384
1385 (defvar gnus-startup-hook nil
1386   "*A hook called at startup.
1387 This hook is called after Gnus is connected to the NNTP server.")
1388
1389 (defvar gnus-get-new-news-hook nil
1390   "*A hook run just before Gnus checks for new news.")
1391
1392 (defvar gnus-after-getting-new-news-hook nil
1393   "*A hook run after Gnus checks for new news.")
1394
1395 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1396   "*A function that is called to generate the group buffer.
1397 The function is called with three arguments: The first is a number;
1398 all group with a level less or equal to that number should be listed,
1399 if the second is non-nil, empty groups should also be displayed.  If
1400 the third is non-nil, it is a number.  No groups with a level lower
1401 than this number should be displayed.
1402
1403 The only current function implemented is `gnus-group-prepare-flat'.")
1404
1405 (defvar gnus-group-prepare-hook nil
1406   "*A hook called after the group buffer has been generated.
1407 If you want to modify the group buffer, you can use this hook.")
1408
1409 (defvar gnus-summary-prepare-hook nil
1410   "*A hook called after the summary buffer has been generated.
1411 If you want to modify the summary buffer, you can use this hook.")
1412
1413 (defvar gnus-summary-generate-hook nil
1414   "*A hook run just before generating the summary buffer.
1415 This hook is commonly used to customize threading variables and the
1416 like.")
1417
1418 (defvar gnus-article-prepare-hook nil
1419   "*A hook called after an article has been prepared in the article buffer.
1420 If you want to run a special decoding program like nkf, use this hook.")
1421
1422 ;(defvar gnus-article-display-hook nil
1423 ;  "*A hook called after the article is displayed in the article buffer.
1424 ;The hook is designed to change the contents of the article
1425 ;buffer.  Typical functions that this hook may contain are
1426 ;`gnus-article-hide-headers' (hide selected headers),
1427 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1428 ;`gnus-article-hide-signature' (hide signature) and
1429 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1432 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1433
1434 (defvar gnus-article-x-face-command
1435   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1436   "String or function to be executed to display an X-Face header.
1437 If it is a string, the command will be executed in a sub-shell
1438 asynchronously.  The compressed face will be piped to this command.")
1439
1440 (defvar gnus-article-x-face-too-ugly nil
1441   "Regexp matching posters whose face shouldn't be shown automatically.")
1442
1443 (defvar gnus-select-group-hook nil
1444   "*A hook called when a newsgroup is selected.
1445
1446 If you'd like to simplify subjects like the
1447 `gnus-summary-next-same-subject' command does, you can use the
1448 following hook:
1449
1450  (setq gnus-select-group-hook
1451       (list
1452         (lambda ()
1453           (mapcar (lambda (header)
1454                      (mail-header-set-subject
1455                       header
1456                       (gnus-simplify-subject
1457                        (mail-header-subject header) 're-only)))
1458                   gnus-newsgroup-headers))))")
1459
1460 (defvar gnus-select-article-hook nil
1461   "*A hook called when an article is selected.")
1462
1463 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1464   "*A hook called to apply kill files to a group.
1465 This hook is intended to apply a kill file to the selected newsgroup.
1466 The function `gnus-apply-kill-file' is called by default.
1467
1468 Since a general kill file is too heavy to use only for a few
1469 newsgroups, I recommend you to use a lighter hook function.  For
1470 example, if you'd like to apply a kill file to articles which contains
1471 a string `rmgroup' in subject in newsgroup `control', you can use the
1472 following hook:
1473
1474  (setq gnus-apply-kill-hook
1475       (list
1476         (lambda ()
1477           (cond ((string-match \"control\" gnus-newsgroup-name)
1478                  (gnus-kill \"Subject\" \"rmgroup\")
1479                  (gnus-expunge \"X\"))))))")
1480
1481 (defvar gnus-visual-mark-article-hook
1482   (list 'gnus-highlight-selected-summary)
1483   "*Hook run after selecting an article in the summary buffer.
1484 It is meant to be used for highlighting the article in some way.  It
1485 is not run if `gnus-visual' is nil.")
1486
1487 (defvar gnus-parse-headers-hook nil
1488   "*A hook called before parsing the headers.")
1489
1490 (defvar gnus-exit-group-hook nil
1491   "*A hook called when exiting (not quitting) summary mode.")
1492
1493 (defvar gnus-suspend-gnus-hook nil
1494   "*A hook called when suspending (not exiting) Gnus.")
1495
1496 (defvar gnus-exit-gnus-hook nil
1497   "*A hook called when exiting Gnus.")
1498
1499 (defvar gnus-after-exiting-gnus-hook nil
1500   "*A hook called after exiting Gnus.")
1501
1502 (defvar gnus-save-newsrc-hook nil
1503   "*A hook called before saving any of the newsrc files.")
1504
1505 (defvar gnus-save-quick-newsrc-hook nil
1506   "*A hook called just before saving the quick newsrc file.
1507 Can be used to turn version control on or off.")
1508
1509 (defvar gnus-save-standard-newsrc-hook nil
1510   "*A hook called just before saving the standard newsrc file.
1511 Can be used to turn version control on or off.")
1512
1513 (defvar gnus-summary-update-hook
1514   (list 'gnus-summary-highlight-line)
1515   "*A hook called when a summary line is changed.
1516 The hook will not be called if `gnus-visual' is nil.
1517
1518 The default function `gnus-summary-highlight-line' will
1519 highlight the line according to the `gnus-summary-highlight'
1520 variable.")
1521
1522 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1523   "*A hook called when a group line is changed.
1524 The hook will not be called if `gnus-visual' is nil.
1525
1526 The default function `gnus-group-highlight-line' will
1527 highlight the line according to the `gnus-group-highlight'
1528 variable.")
1529
1530 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1531   "*A hook called when an article is selected for the first time.
1532 The hook is intended to mark an article as read (or unread)
1533 automatically when it is selected.")
1534
1535 (defvar gnus-group-change-level-function nil
1536   "Function run when a group level is changed.
1537 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1538
1539 ;; Remove any hilit infestation.
1540 (add-hook 'gnus-startup-hook
1541           (lambda ()
1542             (remove-hook 'gnus-summary-prepare-hook
1543                          'hilit-rehighlight-buffer-quietly)
1544             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1545             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1546             (remove-hook 'gnus-article-prepare-hook
1547                          'hilit-rehighlight-buffer-quietly)))
1548
1549 \f
1550 ;; Internal variables
1551
1552 (defvar gnus-server-alist nil
1553   "List of available servers.")
1554
1555 (defvar gnus-group-indentation-function nil)
1556
1557 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1558
1559 (defvar gnus-goto-missing-group-function nil)
1560
1561 (defvar gnus-override-subscribe-method nil)
1562
1563 (defvar gnus-group-goto-next-group-function nil
1564   "Function to override finding the next group after listing groups.")
1565
1566 (defconst gnus-article-mark-lists
1567   '((marked . tick) (replied . reply)
1568     (expirable . expire) (killed . killed)
1569     (bookmarks . bookmark) (dormant . dormant)
1570     (scored . score) (saved . save)
1571     (cached . cache)
1572     ))
1573
1574 ;; Avoid highlighting in kill files.
1575 (defvar gnus-summary-inhibit-highlight nil)
1576 (defvar gnus-newsgroup-selected-overlay nil)
1577
1578 (defvar gnus-inhibit-hiding nil)
1579 (defvar gnus-group-indentation "")
1580 (defvar gnus-inhibit-limiting nil)
1581 (defvar gnus-created-frames nil)
1582
1583 (defvar gnus-article-mode-map nil)
1584 (defvar gnus-dribble-buffer nil)
1585 (defvar gnus-headers-retrieved-by nil)
1586 (defvar gnus-article-reply nil)
1587 (defvar gnus-override-method nil)
1588 (defvar gnus-article-check-size nil)
1589
1590 (defvar gnus-current-score-file nil)
1591 (defvar gnus-newsgroup-adaptive-score-file nil)
1592 (defvar gnus-scores-exclude-files nil)
1593
1594 (defvar gnus-opened-servers nil)
1595
1596 (defvar gnus-current-move-group nil)
1597
1598 (defvar gnus-newsgroup-dependencies nil)
1599 (defvar gnus-newsgroup-async nil)
1600 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1601
1602 (defvar gnus-newsgroup-adaptive nil)
1603
1604 (defvar gnus-summary-display-table nil)
1605 (defvar gnus-summary-display-article-function nil)
1606
1607 (defvar gnus-summary-highlight-line-function nil
1608   "Function called after highlighting a summary line.")
1609
1610 (defvar gnus-group-line-format-alist
1611   `((?M gnus-tmp-marked-mark ?c)
1612     (?S gnus-tmp-subscribed ?c)
1613     (?L gnus-tmp-level ?d)
1614     (?N (cond ((eq number t) "*" )
1615               ((numberp number) 
1616                (int-to-string
1617                 (+ number
1618                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1619                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1620               (t number)) ?s)
1621     (?R gnus-tmp-number-of-read ?s)
1622     (?t gnus-tmp-number-total ?d)
1623     (?y gnus-tmp-number-of-unread ?s)
1624     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1625     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1626     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1627            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1628     (?g gnus-tmp-group ?s)
1629     (?G gnus-tmp-qualified-group ?s)
1630     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1631     (?D gnus-tmp-newsgroup-description ?s)
1632     (?o gnus-tmp-moderated ?c)
1633     (?O gnus-tmp-moderated-string ?s)
1634     (?p gnus-tmp-process-marked ?c)
1635     (?s gnus-tmp-news-server ?s)
1636     (?n gnus-tmp-news-method ?s)
1637     (?P gnus-group-indentation ?s)
1638     (?l gnus-tmp-grouplens ?s)
1639     (?z gnus-tmp-news-method-string ?s)
1640     (?u gnus-tmp-user-defined ?s)))
1641
1642 (defvar gnus-summary-line-format-alist
1643   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1644     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1645     (?s gnus-tmp-subject-or-nil ?s)
1646     (?n gnus-tmp-name ?s)
1647     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1648         ?s)
1649     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1650             gnus-tmp-from) ?s)
1651     (?F gnus-tmp-from ?s)
1652     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1653     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1654     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1655     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1656     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1657     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1658     (?L gnus-tmp-lines ?d)
1659     (?I gnus-tmp-indentation ?s)
1660     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1661     (?R gnus-tmp-replied ?c)
1662     (?\[ gnus-tmp-opening-bracket ?c)
1663     (?\] gnus-tmp-closing-bracket ?c)
1664     (?\> (make-string gnus-tmp-level ? ) ?s)
1665     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1666     (?i gnus-tmp-score ?d)
1667     (?z gnus-tmp-score-char ?c)
1668     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1669     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1670     (?U gnus-tmp-unread ?c)
1671     (?t (gnus-summary-number-of-articles-in-thread
1672          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1673         ?d)
1674     (?e (gnus-summary-number-of-articles-in-thread
1675          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1676         ?c)
1677     (?u gnus-tmp-user-defined ?s))
1678   "An alist of format specifications that can appear in summary lines,
1679 and what variables they correspond with, along with the type of the
1680 variable (string, integer, character, etc).")
1681
1682 (defvar gnus-summary-dummy-line-format-alist
1683   `((?S gnus-tmp-subject ?s)
1684     (?N gnus-tmp-number ?d)
1685     (?u gnus-tmp-user-defined ?s)))
1686
1687 (defvar gnus-summary-mode-line-format-alist
1688   `((?G gnus-tmp-group-name ?s)
1689     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1690     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1691     (?A gnus-tmp-article-number ?d)
1692     (?Z gnus-tmp-unread-and-unselected ?s)
1693     (?V gnus-version ?s)
1694     (?U gnus-tmp-unread ?d)
1695     (?S gnus-tmp-subject ?s)
1696     (?e gnus-tmp-unselected ?d)
1697     (?u gnus-tmp-user-defined ?s)
1698     (?d (length gnus-newsgroup-dormant) ?d)
1699     (?t (length gnus-newsgroup-marked) ?d)
1700     (?r (length gnus-newsgroup-reads) ?d)
1701     (?E gnus-newsgroup-expunged-tally ?d)
1702     (?s (gnus-current-score-file-nondirectory) ?s)))
1703
1704 (defvar gnus-article-mode-line-format-alist
1705   gnus-summary-mode-line-format-alist)
1706
1707 (defvar gnus-group-mode-line-format-alist
1708   `((?S gnus-tmp-news-server ?s)
1709     (?M gnus-tmp-news-method ?s)
1710     (?u gnus-tmp-user-defined ?s)))
1711
1712 (defvar gnus-have-read-active-file nil)
1713
1714 (defconst gnus-maintainer
1715   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1716   "The mail address of the Gnus maintainers.")
1717
1718 (defconst gnus-version "September Gnus v0.52"
1719   "Version number for this version of Gnus.")
1720
1721 (defvar gnus-info-nodes
1722   '((gnus-group-mode            "(gnus)The Group Buffer")
1723     (gnus-summary-mode          "(gnus)The Summary Buffer")
1724     (gnus-article-mode          "(gnus)The Article Buffer"))
1725   "Assoc list of major modes and related Info nodes.")
1726
1727 (defvar gnus-group-buffer "*Group*")
1728 (defvar gnus-summary-buffer "*Summary*")
1729 (defvar gnus-article-buffer "*Article*")
1730 (defvar gnus-server-buffer "*Server*")
1731
1732 (defvar gnus-work-buffer " *gnus work*")
1733
1734 (defvar gnus-original-article-buffer " *Original Article*")
1735 (defvar gnus-original-article nil)
1736
1737 (defvar gnus-buffer-list nil
1738   "Gnus buffers that should be killed on exit.")
1739
1740 (defvar gnus-slave nil
1741   "Whether this Gnus is a slave or not.")
1742
1743 (defvar gnus-variable-list
1744   '(gnus-newsrc-options gnus-newsrc-options-n
1745     gnus-newsrc-last-checked-date
1746     gnus-newsrc-alist gnus-server-alist
1747     gnus-killed-list gnus-zombie-list
1748     gnus-topic-topology gnus-topic-alist
1749     gnus-format-specs)
1750   "Gnus variables saved in the quick startup file.")
1751
1752 (defvar gnus-newsrc-options nil
1753   "Options line in the .newsrc file.")
1754
1755 (defvar gnus-newsrc-options-n nil
1756   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1757
1758 (defvar gnus-newsrc-last-checked-date nil
1759   "Date Gnus last asked server for new newsgroups.")
1760
1761 (defvar gnus-topic-topology nil
1762   "The complete topic hierarchy.")
1763
1764 (defvar gnus-topic-alist nil
1765   "The complete topic-group alist.")
1766
1767 (defvar gnus-newsrc-alist nil
1768   "Assoc list of read articles.
1769 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1770
1771 (defvar gnus-newsrc-hashtb nil
1772   "Hashtable of gnus-newsrc-alist.")
1773
1774 (defvar gnus-killed-list nil
1775   "List of killed newsgroups.")
1776
1777 (defvar gnus-killed-hashtb nil
1778   "Hash table equivalent of gnus-killed-list.")
1779
1780 (defvar gnus-zombie-list nil
1781   "List of almost dead newsgroups.")
1782
1783 (defvar gnus-description-hashtb nil
1784   "Descriptions of newsgroups.")
1785
1786 (defvar gnus-list-of-killed-groups nil
1787   "List of newsgroups that have recently been killed by the user.")
1788
1789 (defvar gnus-active-hashtb nil
1790   "Hashtable of active articles.")
1791
1792 (defvar gnus-moderated-list nil
1793   "List of moderated newsgroups.")
1794
1795 (defvar gnus-group-marked nil)
1796
1797 (defvar gnus-current-startup-file nil
1798   "Startup file for the current host.")
1799
1800 (defvar gnus-last-search-regexp nil
1801   "Default regexp for article search command.")
1802
1803 (defvar gnus-last-shell-command nil
1804   "Default shell command on article.")
1805
1806 (defvar gnus-current-select-method nil
1807   "The current method for selecting a newsgroup.")
1808
1809 (defvar gnus-group-list-mode nil)
1810
1811 (defvar gnus-article-internal-prepare-hook nil)
1812
1813 (defvar gnus-newsgroup-name nil)
1814 (defvar gnus-newsgroup-begin nil)
1815 (defvar gnus-newsgroup-end nil)
1816 (defvar gnus-newsgroup-last-rmail nil)
1817 (defvar gnus-newsgroup-last-mail nil)
1818 (defvar gnus-newsgroup-last-folder nil)
1819 (defvar gnus-newsgroup-last-file nil)
1820 (defvar gnus-newsgroup-auto-expire nil)
1821 (defvar gnus-newsgroup-active nil)
1822
1823 (defvar gnus-newsgroup-data nil)
1824 (defvar gnus-newsgroup-data-reverse nil)
1825 (defvar gnus-newsgroup-limit nil)
1826 (defvar gnus-newsgroup-limits nil)
1827
1828 (defvar gnus-newsgroup-unreads nil
1829   "List of unread articles in the current newsgroup.")
1830
1831 (defvar gnus-newsgroup-unselected nil
1832   "List of unselected unread articles in the current newsgroup.")
1833
1834 (defvar gnus-newsgroup-reads nil
1835   "Alist of read articles and article marks in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-expunged-tally nil)
1838
1839 (defvar gnus-newsgroup-marked nil
1840   "List of ticked articles in the current newsgroup (a subset of unread art).")
1841
1842 (defvar gnus-newsgroup-killed nil
1843   "List of ranges of articles that have been through the scoring process.")
1844
1845 (defvar gnus-newsgroup-cached nil
1846   "List of articles that come from the article cache.")
1847
1848 (defvar gnus-newsgroup-saved nil
1849   "List of articles that have been saved.")
1850
1851 (defvar gnus-newsgroup-kill-headers nil)
1852
1853 (defvar gnus-newsgroup-replied nil
1854   "List of articles that have been replied to in the current newsgroup.")
1855
1856 (defvar gnus-newsgroup-expirable nil
1857   "List of articles in the current newsgroup that can be expired.")
1858
1859 (defvar gnus-newsgroup-processable nil
1860   "List of articles in the current newsgroup that can be processed.")
1861
1862 (defvar gnus-newsgroup-bookmarks nil
1863   "List of articles in the current newsgroup that have bookmarks.")
1864
1865 (defvar gnus-newsgroup-dormant nil
1866   "List of dormant articles in the current newsgroup.")
1867
1868 (defvar gnus-newsgroup-scored nil
1869   "List of scored articles in the current newsgroup.")
1870
1871 (defvar gnus-newsgroup-headers nil
1872   "List of article headers in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-threads nil)
1875
1876 (defvar gnus-newsgroup-prepared nil
1877   "Whether the current group has been prepared properly.")
1878
1879 (defvar gnus-newsgroup-ancient nil
1880   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-sparse nil)
1883
1884 (defvar gnus-current-article nil)
1885 (defvar gnus-article-current nil)
1886 (defvar gnus-current-headers nil)
1887 (defvar gnus-have-all-headers nil)
1888 (defvar gnus-last-article nil)
1889 (defvar gnus-newsgroup-history nil)
1890 (defvar gnus-current-kill-article nil)
1891
1892 ;; Save window configuration.
1893 (defvar gnus-prev-winconf nil)
1894
1895 (defvar gnus-summary-mark-positions nil)
1896 (defvar gnus-group-mark-positions nil)
1897
1898 (defvar gnus-reffed-article-number nil)
1899
1900 ;;; Let the byte-compiler know that we know about this variable.
1901 (defvar rmail-default-rmail-file)
1902
1903 (defvar gnus-cache-removable-articles nil)
1904
1905 (defvar gnus-dead-summary nil)
1906
1907 (defconst gnus-summary-local-variables
1908   '(gnus-newsgroup-name
1909     gnus-newsgroup-begin gnus-newsgroup-end
1910     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1911     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1912     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1913     gnus-newsgroup-unselected gnus-newsgroup-marked
1914     gnus-newsgroup-reads gnus-newsgroup-saved
1915     gnus-newsgroup-replied gnus-newsgroup-expirable
1916     gnus-newsgroup-processable gnus-newsgroup-killed
1917     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1918     gnus-newsgroup-headers gnus-newsgroup-threads
1919     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1920     gnus-current-article gnus-current-headers gnus-have-all-headers
1921     gnus-last-article gnus-article-internal-prepare-hook
1922     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1923     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1924     gnus-newsgroup-async 
1925     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1926     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1927     gnus-newsgroup-history gnus-newsgroup-ancient
1928     gnus-newsgroup-sparse
1929     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1930     gnus-newsgroup-adaptive-score-file
1931     (gnus-newsgroup-expunged-tally . 0)
1932     gnus-cache-removable-articles gnus-newsgroup-cached
1933     gnus-newsgroup-data gnus-newsgroup-data-reverse
1934     gnus-newsgroup-limit gnus-newsgroup-limits)
1935   "Variables that are buffer-local to the summary buffers.")
1936
1937 (defconst gnus-bug-message
1938   "Sending a bug report to the Gnus Towers.
1939 ========================================
1940
1941 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1942 be sent to the Gnus Bug Exterminators.
1943
1944 At the bottom of the buffer you'll see lots of variable settings.
1945 Please do not delete those.  They will tell the Bug People what your
1946 environment is, so that it will be easier to locate the bugs.
1947
1948 If you have found a bug that makes Emacs go \"beep\", set
1949 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1950 and include the backtrace in your bug report.
1951
1952 Please describe the bug in annoying, painstaking detail.
1953
1954 Thank you for your help in stamping out bugs.
1955 ")
1956
1957 ;;; End of variables.
1958
1959 ;; Define some autoload functions Gnus might use.
1960 (eval-and-compile
1961
1962   ;; This little mapcar goes through the list below and marks the
1963   ;; symbols in question as autoloaded functions.
1964   (mapcar
1965    (lambda (package)
1966      (let ((interactive (nth 1 (memq ':interactive package))))
1967        (mapcar
1968         (lambda (function)
1969           (let (keymap)
1970             (when (consp function)
1971               (setq keymap (car (memq 'keymap function)))
1972               (setq function (car function)))
1973             (autoload function (car package) nil interactive keymap)))
1974         (if (eq (nth 1 package) ':interactive)
1975             (cdddr package)
1976           (cdr package)))))
1977    '(("metamail" metamail-buffer)
1978      ("info" Info-goto-node)
1979      ("hexl" hexl-hex-string-to-integer)
1980      ("pp" pp pp-to-string pp-eval-expression)
1981      ("mail-extr" mail-extract-address-components)
1982      ("nnmail" nnmail-split-fancy nnmail-article-group)
1983      ("nnvirtual" nnvirtual-catchup-group)
1984      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1985       timezone-make-sortable-date timezone-make-time-string)
1986      ("sendmail" mail-position-on-field mail-setup)
1987      ("rmailout" rmail-output)
1988      ("rnewspost" news-mail-other-window news-reply-yank-original
1989       news-caesar-buffer-body)
1990      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1991       rmail-show-message)
1992      ("gnus-soup" :interactive t
1993       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1994       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1995      ("nnsoup" nnsoup-pack-replies)
1996      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1997       gnus-Folder-save-name gnus-folder-save-name)
1998      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1999      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2000       gnus-server-make-menu-bar gnus-article-make-menu-bar
2001       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2002       gnus-summary-highlight-line gnus-carpal-setup-buffer
2003       gnus-group-highlight-line
2004       gnus-article-add-button gnus-insert-next-page-button
2005       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2006      ("gnus-vis" :interactive t
2007       gnus-article-push-button gnus-article-press-button
2008       gnus-article-highlight gnus-article-highlight-some
2009       gnus-article-highlight-headers gnus-article-highlight-signature
2010       gnus-article-add-buttons gnus-article-add-buttons-to-head
2011       gnus-article-next-button gnus-article-prev-button)
2012      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2013       gnus-demon-add-disconnection gnus-demon-add-handler
2014       gnus-demon-remove-handler)
2015      ("gnus-demon" :interactive t
2016       gnus-demon-init gnus-demon-cancel)
2017      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2018       gnus-tree-open gnus-tree-close)
2019      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2020       gnus-nocem-unwanted-article-p)
2021      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2022      ("gnus-srvr" gnus-browse-foreign-server)
2023      ("gnus-cite" :interactive t
2024       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2025       gnus-article-hide-citation gnus-article-fill-cited-article)
2026      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2027       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2028       gnus-execute gnus-expunge)
2029      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2030       gnus-cache-possibly-remove-articles gnus-cache-request-article
2031       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2032       gnus-cache-enter-remove-article gnus-cached-article-p
2033       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2034      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2035       gnus-cache-remove-article)
2036      ("gnus-score" :interactive t
2037       gnus-summary-increase-score gnus-summary-lower-score
2038       gnus-score-flush-cache gnus-score-close
2039       gnus-score-raise-same-subject-and-select
2040       gnus-score-raise-same-subject gnus-score-default
2041       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2042       gnus-score-lower-same-subject gnus-score-lower-thread
2043       gnus-possibly-score-headers)
2044      ("gnus-score"
2045       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2046       gnus-current-score-file-nondirectory gnus-score-adaptive
2047       gnus-score-find-trace gnus-score-file-name)
2048      ("gnus-edit" :interactive t gnus-score-customize)
2049      ("gnus-topic" :interactive t gnus-topic-mode)
2050      ("gnus-topic" gnus-topic-remove-group)
2051      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2052      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2053      ("gnus-uu" :interactive t
2054       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2055       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2056       gnus-uu-mark-by-regexp gnus-uu-mark-all
2057       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2058       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2059       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2060       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2061       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2062       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2063       gnus-uu-decode-binhex-view)
2064      ("gnus-msg" (gnus-summary-send-map keymap)
2065       gnus-mail-yank-original gnus-mail-send-and-exit
2066       gnus-sendmail-setup-mail gnus-article-mail
2067       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2068      ("gnus-msg" :interactive t
2069       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2070       gnus-summary-followup gnus-summary-followup-with-original
2071       gnus-summary-followup-and-reply
2072       gnus-summary-followup-and-reply-with-original
2073       gnus-summary-cancel-article gnus-summary-supersede-article
2074       gnus-post-news gnus-inews-news gnus-cancel-news
2075       gnus-summary-reply gnus-summary-reply-with-original
2076       gnus-summary-mail-forward gnus-summary-mail-other-window
2077       gnus-bug)
2078      ("gnus-picon" :interactive t gnus-article-display-picons
2079       gnus-group-display-picons gnus-picons-article-display-x-face)
2080      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2081       gnus-grouplens-mode)
2082      ("gnus-vm" gnus-vm-mail-setup)
2083      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2084       gnus-summary-save-article-vm gnus-yank-article))))
2085
2086 \f
2087
2088 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2089 ;; If you want the cursor to go somewhere else, set these two
2090 ;; functions in some startup hook to whatever you want.
2091 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2092 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2093
2094 ;;; Various macros and substs.
2095
2096 (defun gnus-header-from (header)
2097   (mail-header-from header))
2098
2099 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2100   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2101   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2102     `(let ((,tempvar (selected-window)))
2103        (unwind-protect
2104            (progn
2105              (pop-to-buffer ,buffer)
2106              ,@forms)
2107          (select-window ,tempvar)))))
2108
2109 (defmacro gnus-gethash (string hashtable)
2110   "Get hash value of STRING in HASHTABLE."
2111   `(symbol-value (intern-soft ,string ,hashtable)))
2112
2113 (defmacro gnus-sethash (string value hashtable)
2114   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2115   `(set (intern ,string ,hashtable) ,value))
2116
2117 (defmacro gnus-intern-safe (string hashtable)
2118   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2119   `(let ((symbol (intern ,string ,hashtable)))
2120      (or (boundp symbol)
2121          (set symbol nil))
2122      symbol))
2123
2124 (defmacro gnus-group-unread (group)
2125   "Get the currently computed number of unread articles in GROUP."
2126   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2127
2128 (defmacro gnus-group-entry (group)
2129   "Get the newsrc entry for GROUP."
2130   `(gnus-gethash ,group gnus-newsrc-hashtb))
2131
2132 (defmacro gnus-active (group)
2133   "Get active info on GROUP."
2134   `(gnus-gethash ,group gnus-active-hashtb))
2135
2136 (defmacro gnus-set-active (group active)
2137   "Set GROUP's active info."
2138   `(gnus-sethash ,group ,active gnus-active-hashtb))
2139
2140 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2141 ;;   function `substring' might cut on a middle of multi-octet
2142 ;;   character.
2143 (defun gnus-truncate-string (str width)
2144   (substring str 0 width))
2145
2146 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2147 ;; to limit the length of a string.  This function is necessary since
2148 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2149 (defsubst gnus-limit-string (str width)
2150   (if (> (length str) width)
2151       (substring str 0 width)
2152     str))
2153
2154 (defsubst gnus-simplify-subject-re (subject)
2155   "Remove \"Re:\" from subject lines."
2156   (if (string-match "^[Rr][Ee]: *" subject)
2157       (substring subject (match-end 0))
2158     subject))
2159
2160 (defsubst gnus-goto-char (point)
2161   (and point (goto-char point)))
2162
2163 (defmacro gnus-buffer-exists-p (buffer)
2164   `(let ((buffer ,buffer))
2165      (and buffer
2166           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2167                    buffer))))
2168
2169 (defmacro gnus-kill-buffer (buffer)
2170   `(let ((buf ,buffer))
2171      (if (gnus-buffer-exists-p buf)
2172          (kill-buffer buf))))
2173
2174 (defsubst gnus-point-at-bol ()
2175   "Return point at the beginning of the line."
2176   (let ((p (point)))
2177     (beginning-of-line)
2178     (prog1
2179         (point)
2180       (goto-char p))))
2181
2182 (defsubst gnus-point-at-eol ()
2183   "Return point at the end of the line."
2184   (let ((p (point)))
2185     (end-of-line)
2186     (prog1
2187         (point)
2188       (goto-char p))))
2189
2190 (defun gnus-alive-p ()
2191   "Say whether Gnus is running or not."
2192   (and gnus-group-buffer
2193        (get-buffer gnus-group-buffer)))
2194
2195 ;; Delete the current line (and the next N lines.);
2196 (defmacro gnus-delete-line (&optional n)
2197   `(delete-region (progn (beginning-of-line) (point))
2198                   (progn (forward-line ,(or n 1)) (point))))
2199
2200 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2201 (defvar gnus-init-inhibit nil)
2202 (defun gnus-read-init-file (&optional inhibit-next)
2203   (if gnus-init-inhibit
2204       (setq gnus-init-inhibit nil)
2205     (setq gnus-init-inhibit inhibit-next)
2206     (and gnus-init-file
2207          (or (and (file-exists-p gnus-init-file)
2208                   ;; Don't try to load a directory.
2209                   (not (file-directory-p gnus-init-file)))
2210              (file-exists-p (concat gnus-init-file ".el"))
2211              (file-exists-p (concat gnus-init-file ".elc")))
2212          (condition-case var
2213              (load gnus-init-file nil t)
2214            (error
2215             (error "Error in %s: %s" gnus-init-file var))))))
2216
2217 ;; Info access macros.
2218
2219 (defmacro gnus-info-group (info)
2220   `(nth 0 ,info))
2221 (defmacro gnus-info-rank (info)
2222   `(nth 1 ,info))
2223 (defmacro gnus-info-read (info)
2224   `(nth 2 ,info))
2225 (defmacro gnus-info-marks (info)
2226   `(nth 3 ,info))
2227 (defmacro gnus-info-method (info)
2228   `(nth 4 ,info))
2229 (defmacro gnus-info-params (info)
2230   `(nth 5 ,info))
2231
2232 (defmacro gnus-info-level (info)
2233   `(let ((rank (gnus-info-rank ,info)))
2234      (if (consp rank)
2235          (car rank)
2236        rank)))
2237 (defmacro gnus-info-score (info)
2238   `(let ((rank (gnus-info-rank ,info)))
2239      (or (and (consp rank) (cdr rank)) 0)))
2240
2241 (defmacro gnus-info-set-group (info group)
2242   `(setcar ,info ,group))
2243 (defmacro gnus-info-set-rank (info rank)
2244   `(setcar (nthcdr 1 ,info) ,rank))
2245 (defmacro gnus-info-set-read (info read)
2246   `(setcar (nthcdr 2 ,info) ,read))
2247 (defmacro gnus-info-set-marks (info marks)
2248   `(setcar (nthcdr 3 ,info) ,marks))
2249 (defmacro gnus-info-set-method (info method)
2250   `(setcar (nthcdr 4 ,info) ,method))
2251 (defmacro gnus-info-set-params (info params)
2252   `(setcar (nthcdr 5 ,info) ,params))
2253
2254 (defmacro gnus-info-set-level (info level)
2255   `(let ((rank (cdr ,info)))
2256      (if (consp (car rank))
2257          (setcar (car rank) ,level)
2258        (setcar rank ,level))))
2259 (defmacro gnus-info-set-score (info score)
2260   `(let ((rank (cdr ,info)))
2261      (if (consp (car rank))
2262          (setcdr (car rank) ,score)
2263        (setcar rank (cons (car rank) ,score)))))
2264
2265 (defmacro gnus-get-info (group)
2266   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2267
2268 (defun gnus-byte-code (func)
2269   "Return a form that can be `eval'ed based on FUNC."
2270   (let ((fval (symbol-function func)))
2271     (if (byte-code-function-p fval)
2272         (let ((flist (append fval nil)))
2273           (setcar flist 'byte-code)
2274           flist)
2275       (cons 'progn (cddr fval)))))
2276
2277 ;;; Load the compatability functions.
2278
2279 (require 'gnus-cus)
2280 (require 'gnus-ems)
2281
2282 \f
2283
2284 ;; Format specs.  The chunks below are the machine-generated forms
2285 ;; that are to be evaled as the result of the default format strings.
2286 ;; We write them in here to get them byte-compiled.  That way the
2287 ;; default actions will be quite fast, while still retaining the full
2288 ;; flexibility of the user-defined format specs.
2289
2290 ;; First we have lots of dummy defvars to let the compiler know these
2291 ;; are really dynamic variables.
2292
2293 (defvar gnus-tmp-unread)
2294 (defvar gnus-tmp-replied)
2295 (defvar gnus-tmp-score-char)
2296 (defvar gnus-tmp-indentation)
2297 (defvar gnus-tmp-opening-bracket)
2298 (defvar gnus-tmp-lines)
2299 (defvar gnus-tmp-name)
2300 (defvar gnus-tmp-closing-bracket)
2301 (defvar gnus-tmp-subject-or-nil)
2302 (defvar gnus-tmp-subject)
2303 (defvar gnus-tmp-marked)
2304 (defvar gnus-tmp-marked-mark)
2305 (defvar gnus-tmp-subscribed)
2306 (defvar gnus-tmp-process-marked)
2307 (defvar gnus-tmp-number-of-unread)
2308 (defvar gnus-tmp-group-name)
2309 (defvar gnus-tmp-group)
2310 (defvar gnus-tmp-article-number)
2311 (defvar gnus-tmp-unread-and-unselected)
2312 (defvar gnus-tmp-news-method)
2313 (defvar gnus-tmp-news-server)
2314 (defvar gnus-tmp-article-number)
2315 (defvar gnus-mouse-face)
2316 (defvar gnus-mouse-face-prop)
2317
2318 (defun gnus-summary-line-format-spec ()
2319   (insert gnus-tmp-unread gnus-tmp-replied
2320           gnus-tmp-score-char gnus-tmp-indentation)
2321   (put-text-property
2322    (point)
2323    (progn
2324      (insert
2325       gnus-tmp-opening-bracket
2326       (format "%4d: %-20s"
2327               gnus-tmp-lines
2328               (if (> (length gnus-tmp-name) 20)
2329                   (substring gnus-tmp-name 0 20)
2330                 gnus-tmp-name))
2331       gnus-tmp-closing-bracket)
2332      (point))
2333    gnus-mouse-face-prop gnus-mouse-face)
2334   (insert " " gnus-tmp-subject-or-nil "\n"))
2335
2336 (defvar gnus-summary-line-format-spec
2337   (gnus-byte-code 'gnus-summary-line-format-spec))
2338
2339 (defun gnus-summary-dummy-line-format-spec ()
2340   (insert "*  ")
2341   (put-text-property
2342    (point)
2343    (progn
2344      (insert ":                          :")
2345      (point))
2346    gnus-mouse-face-prop gnus-mouse-face)
2347   (insert " " gnus-tmp-subject "\n"))
2348
2349 (defvar gnus-summary-dummy-line-format-spec
2350   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2351
2352 (defun gnus-group-line-format-spec ()
2353   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2354           gnus-tmp-process-marked
2355           gnus-group-indentation
2356           (format "%5s: " gnus-tmp-number-of-unread))
2357   (put-text-property
2358    (point)
2359    (progn
2360      (insert gnus-tmp-group "\n")
2361      (1- (point)))
2362    gnus-mouse-face-prop gnus-mouse-face))
2363 (defvar gnus-group-line-format-spec
2364   (gnus-byte-code 'gnus-group-line-format-spec))
2365
2366 (defvar gnus-format-specs
2367   `((version . ,emacs-version)
2368     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2369     (summary-dummy ,gnus-summary-dummy-line-format
2370                    ,gnus-summary-dummy-line-format-spec)
2371     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2372
2373 (defvar gnus-article-mode-line-format-spec nil)
2374 (defvar gnus-summary-mode-line-format-spec nil)
2375 (defvar gnus-group-mode-line-format-spec nil)
2376
2377 ;;; Phew.  All that gruft is over, fortunately.
2378
2379 \f
2380 ;;;
2381 ;;; Gnus Utility Functions
2382 ;;;
2383
2384 (defun gnus-extract-address-components (from)
2385   (let (name address)
2386     ;; First find the address - the thing with the @ in it.  This may
2387     ;; not be accurate in mail addresses, but does the trick most of
2388     ;; the time in news messages.
2389     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2390         (setq address (substring from (match-beginning 0) (match-end 0))))
2391     ;; Then we check whether the "name <address>" format is used.
2392     (and address
2393          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2394          ;; Linear white space is not required.
2395          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2396          (and (setq name (substring from 0 (match-beginning 0)))
2397               ;; Strip any quotes from the name.
2398               (string-match "\".*\"" name)
2399               (setq name (substring name 1 (1- (match-end 0))))))
2400     ;; If not, then "address (name)" is used.
2401     (or name
2402         (and (string-match "(.+)" from)
2403              (setq name (substring from (1+ (match-beginning 0))
2404                                    (1- (match-end 0)))))
2405         (and (string-match "()" from)
2406              (setq name address))
2407         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2408         ;; XOVER might not support folded From headers.
2409         (and (string-match "(.*" from)
2410              (setq name (substring from (1+ (match-beginning 0))
2411                                    (match-end 0)))))
2412     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2413     (list (or name from) (or address from))))
2414
2415 (defun gnus-fetch-field (field)
2416   "Return the value of the header FIELD of current article."
2417   (save-excursion
2418     (save-restriction
2419       (let ((case-fold-search t))
2420         (nnheader-narrow-to-headers)
2421         (mail-fetch-field field)))))
2422
2423 (defun gnus-goto-colon ()
2424   (beginning-of-line)
2425   (search-forward ":" (gnus-point-at-eol) t))
2426
2427 ;;;###autoload
2428 (defun gnus-update-format (var)
2429   "Update the format specification near point."
2430   (interactive
2431    (list
2432     (save-excursion
2433       (eval-defun nil)
2434       ;; Find the end of the current word.
2435       (re-search-forward "[ \t\n]" nil t)
2436       ;; Search backward.
2437       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2438         (match-string 1)))))
2439   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2440                               (match-string 1 var))))
2441          (entry (assq type gnus-format-specs))
2442          value spec)
2443     (when entry
2444       (setq gnus-format-specs (delq entry gnus-format-specs)))
2445     (set
2446      (intern (format "%s-spec" var))
2447      (gnus-parse-format (setq value (symbol-value (intern var)))
2448                         (symbol-value (intern (format "%s-alist" var)))
2449                         (not (string-match "mode" var))))
2450     (setq spec (symbol-value (intern (format "%s-spec" var))))
2451     (push (list type value spec) gnus-format-specs)
2452
2453     (pop-to-buffer "*Gnus Format*")
2454     (erase-buffer)
2455     (lisp-interaction-mode)
2456     (insert (pp-to-string spec))))
2457
2458 (defun gnus-update-format-specifications (&optional force)
2459   "Update all (necessary) format specifications."
2460   ;; Make the indentation array.
2461   (gnus-make-thread-indent-array)
2462
2463   ;; See whether all the stored info needs to be flushed.
2464   (when (or force
2465             (not (equal emacs-version
2466                         (cdr (assq 'version gnus-format-specs)))))
2467     (setq gnus-format-specs nil))
2468
2469   ;; Go through all the formats and see whether they need updating.
2470   (let ((types '(summary summary-dummy group
2471                          summary-mode group-mode article-mode))
2472         new-format entry type val)
2473     (while (setq type (pop types))
2474       ;; Jump to the proper buffer to find out the value of
2475       ;; the variable, if possible.  (It may be buffer-local.)
2476       (save-excursion
2477         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2478               val)
2479           (when (and (boundp buffer)
2480                      (setq val (symbol-value buffer))
2481                      (get-buffer val)
2482                      (buffer-name (get-buffer val)))
2483             (set-buffer (get-buffer val)))
2484           (setq new-format (symbol-value
2485                             (intern (format "gnus-%s-line-format" type))))))
2486       (setq entry (cdr (assq type gnus-format-specs)))
2487       (if (and entry
2488                (equal (car entry) new-format))
2489           ;; Use the old format.
2490           (set (intern (format "gnus-%s-line-format-spec" type))
2491                (cadr entry))
2492         ;; This is a new format.
2493         (setq val
2494               (if (not (stringp new-format))
2495                   ;; This is a function call or something.
2496                   new-format
2497                 ;; This is a "real" format.
2498                 (gnus-parse-format
2499                  new-format
2500                  (symbol-value
2501                   (intern (format "gnus-%s-line-format-alist"
2502                                   (if (eq type 'article-mode)
2503                                       'summary-mode type))))
2504                  (not (string-match "mode$" (symbol-name type))))))
2505         ;; Enter the new format spec into the list.
2506         (if entry
2507             (progn
2508               (setcar (cdr entry) val)
2509               (setcar entry new-format))
2510           (push (list type new-format val) gnus-format-specs))
2511         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2512
2513   (gnus-update-group-mark-positions)
2514   (gnus-update-summary-mark-positions))
2515
2516 (defun gnus-update-summary-mark-positions ()
2517   "Compute where the summary marks are to go."
2518   (save-excursion
2519     (let ((gnus-replied-mark 129)
2520           (gnus-score-below-mark 130)
2521           (gnus-score-over-mark 130)
2522           (thread nil)
2523           (gnus-visual nil)
2524           pos)
2525       (gnus-set-work-buffer)
2526       (gnus-summary-insert-line
2527        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2528       (goto-char (point-min))
2529       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2530                                          (- (point) 2)))))
2531       (goto-char (point-min))
2532       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2533             pos)
2534       (goto-char (point-min))
2535       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2536             pos)
2537       (setq gnus-summary-mark-positions pos))))
2538
2539 (defun gnus-update-group-mark-positions ()
2540   (save-excursion
2541     (let ((gnus-process-mark 128)
2542           (gnus-group-marked '("dummy.group")))
2543       (gnus-set-active "dummy.group" '(0 . 0))
2544       (gnus-set-work-buffer)
2545       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2546       (goto-char (point-min))
2547       (setq gnus-group-mark-positions
2548             (list (cons 'process (and (search-forward "\200" nil t)
2549                                       (- (point) 2))))))))
2550
2551 (defvar gnus-mouse-face-0 'highlight)
2552 (defvar gnus-mouse-face-1 'highlight)
2553 (defvar gnus-mouse-face-2 'highlight)
2554 (defvar gnus-mouse-face-3 'highlight)
2555 (defvar gnus-mouse-face-4 'highlight)
2556
2557 (defun gnus-mouse-face-function (form type)
2558   `(put-text-property
2559     (point) (progn ,@form (point))
2560     gnus-mouse-face-prop
2561     ,(if (equal type 0)
2562          'gnus-mouse-face
2563        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2564
2565 (defvar gnus-face-0 'bold)
2566 (defvar gnus-face-1 'italic)
2567 (defvar gnus-face-2 'bold-italic)
2568 (defvar gnus-face-3 'bold)
2569 (defvar gnus-face-4 'bold)
2570
2571 (defun gnus-face-face-function (form type)
2572   `(put-text-property
2573     (point) (progn ,@form (point))
2574     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2575
2576 (defun gnus-max-width-function (el max-width)
2577   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2578   (if (symbolp el)
2579       `(if (> (length ,el) ,max-width)
2580            (substring ,el 0 ,max-width)
2581          ,el)
2582     `(let ((val (eval ,el)))
2583        (if (numberp val)
2584            (setq val (int-to-string val)))
2585        (if (> (length val) ,max-width)
2586            (substring val 0 ,max-width)
2587          val))))
2588
2589 (defun gnus-parse-format (format spec-alist &optional insert)
2590   ;; This function parses the FORMAT string with the help of the
2591   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2592   ;; string.  If the FORMAT string contains the specifiers %( and %)
2593   ;; the text between them will have the mouse-face text property.
2594   (if (string-match
2595        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2596        format)
2597       (gnus-parse-complex-format format spec-alist)
2598     ;; This is a simple format.
2599     (gnus-parse-simple-format format spec-alist insert)))
2600
2601 (defun gnus-parse-complex-format (format spec-alist)
2602   (save-excursion
2603     (gnus-set-work-buffer)
2604     (insert format)
2605     (goto-char (point-min))
2606     (while (re-search-forward "\"" nil t)
2607       (replace-match "\\\"" nil t))
2608     (goto-char (point-min))
2609     (insert "(\"")
2610     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2611       (let ((number (if (match-beginning 1)
2612                         (match-string 1) "0"))
2613             (delim (aref (match-string 2) 0)))
2614         (if (or (= delim ?\() (= delim ?\{))
2615             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2616                                    " " number " \""))
2617           (replace-match "\")\""))))
2618     (goto-char (point-max))
2619     (insert "\")")
2620     (goto-char (point-min))
2621     (let ((form (read (current-buffer))))
2622       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2623
2624 (defun gnus-complex-form-to-spec (form spec-alist)
2625   (delq nil
2626         (mapcar
2627          (lambda (sform)
2628            (if (stringp sform)
2629                (gnus-parse-simple-format sform spec-alist t)
2630              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2631                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2632                       (nth 1 sform))))
2633          form)))
2634
2635 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2636   ;; This function parses the FORMAT string with the help of the
2637   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2638   ;; string.
2639   (let ((max-width 0)
2640         spec flist fstring newspec elem beg result dontinsert)
2641     (save-excursion
2642       (gnus-set-work-buffer)
2643       (insert format)
2644       (goto-char (point-min))
2645       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2646                                 nil t)
2647         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2648               (setq newspec "%"
2649                     beg (1+ (match-beginning 0)))
2650           ;; First check if there are any specs that look anything like
2651           ;; "%12,12A", ie. with a "max width specification".  These have
2652           ;; to be treated specially.
2653           (if (setq beg (match-beginning 1))
2654               (setq max-width
2655                     (string-to-int
2656                      (buffer-substring
2657                       (1+ (match-beginning 1)) (match-end 1))))
2658             (setq max-width 0)
2659             (setq beg (match-beginning 2)))
2660           ;; Find the specification from `spec-alist'.
2661           (unless (setq elem (cdr (assq spec spec-alist)))
2662             (setq elem '("*" ?s)))
2663           ;; Treat user defined format specifiers specially.
2664           (when (eq (car elem) 'gnus-tmp-user-defined)
2665             (setq elem
2666                   (list
2667                    (list (intern (concat "gnus-user-format-function-"
2668                                          (match-string 3)))
2669                          'gnus-tmp-header) ?s))
2670             (delete-region (match-beginning 3) (match-end 3)))
2671           (if (not (zerop max-width))
2672               (let ((el (car elem)))
2673                 (cond ((= (cadr elem) ?c)
2674                        (setq el (list 'char-to-string el)))
2675                       ((= (cadr elem) ?d)
2676                        (setq el (list 'int-to-string el))))
2677                 (setq flist (cons (gnus-max-width-function el max-width)
2678                                   flist))
2679                 (setq newspec ?s))
2680             (progn
2681               (setq flist (cons (car elem) flist))
2682               (setq newspec (cadr elem)))))
2683         ;; Remove the old specification (and possibly a ",12" string).
2684         (delete-region beg (match-end 2))
2685         ;; Insert the new specification.
2686         (goto-char beg)
2687         (insert newspec))
2688       (setq fstring (buffer-substring 1 (point-max))))
2689     ;; Do some postprocessing to increase efficiency.
2690     (setq
2691      result
2692      (cond
2693       ;; Emptyness.
2694       ((string= fstring "")
2695        nil)
2696       ;; Not a format string.
2697       ((not (string-match "%" fstring))
2698        (list fstring))
2699       ;; A format string with just a single string spec.
2700       ((string= fstring "%s")
2701        (list (car flist)))
2702       ;; A single character.
2703       ((string= fstring "%c")
2704        (list (car flist)))
2705       ;; A single number.
2706       ((string= fstring "%d")
2707        (setq dontinsert)
2708        (if insert
2709            (list `(princ ,(car flist)))
2710          (list `(int-to-string ,(car flist)))))
2711       ;; Just lots of chars and strings.
2712       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2713        (nreverse flist))
2714       ;; A single string spec at the beginning of the spec.
2715       ((string-match "\\`%[sc][^%]+\\'" fstring)
2716        (list (car flist) (substring fstring 2)))
2717       ;; A single string spec in the middle of the spec.
2718       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2719        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2720       ;; A single string spec in the end of the spec.
2721       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2722        (list (match-string 1 fstring) (car flist)))
2723       ;; A more complex spec.
2724       (t
2725        (list (cons 'format (cons fstring (nreverse flist)))))))
2726
2727     (if insert
2728         (when result
2729           (if dontinsert
2730               result
2731             (cons 'insert result)))
2732       (cond ((stringp result)
2733              result)
2734             ((consp result)
2735              (cons 'concat result))
2736             (t "")))))
2737
2738 (defun gnus-eval-format (format &optional alist props)
2739   "Eval the format variable FORMAT, using ALIST.
2740 If PROPS, insert the result."
2741   (let ((form (gnus-parse-format format alist props)))
2742     (if props
2743         (add-text-properties (point) (progn (eval form) (point)) props)
2744       (eval form))))
2745
2746 (defun gnus-remove-text-with-property (prop)
2747   "Delete all text in the current buffer with text property PROP."
2748   (save-excursion
2749     (goto-char (point-min))
2750     (while (not (eobp))
2751       (while (get-text-property (point) prop)
2752         (delete-char 1))
2753       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2754
2755 (defun gnus-set-work-buffer ()
2756   (if (get-buffer gnus-work-buffer)
2757       (progn
2758         (set-buffer gnus-work-buffer)
2759         (erase-buffer))
2760     (set-buffer (get-buffer-create gnus-work-buffer))
2761     (kill-all-local-variables)
2762     (buffer-disable-undo (current-buffer))
2763     (gnus-add-current-to-buffer-list)))
2764
2765 ;; Article file names when saving.
2766
2767 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2768   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2769 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2770 Otherwise, it is like ~/News/news/group/num."
2771   (let ((default
2772           (expand-file-name
2773            (concat (if (gnus-use-long-file-name 'not-save)
2774                        (gnus-capitalize-newsgroup newsgroup)
2775                      (gnus-newsgroup-directory-form newsgroup))
2776                    "/" (int-to-string (mail-header-number headers)))
2777            (or gnus-article-save-directory "~/News"))))
2778     (if (and last-file
2779              (string-equal (file-name-directory default)
2780                            (file-name-directory last-file))
2781              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2782         default
2783       (or last-file default))))
2784
2785 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2786   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2787 If variable `gnus-use-long-file-name' is non-nil, it is
2788 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2789   (let ((default
2790           (expand-file-name
2791            (concat (if (gnus-use-long-file-name 'not-save)
2792                        newsgroup
2793                      (gnus-newsgroup-directory-form newsgroup))
2794                    "/" (int-to-string (mail-header-number headers)))
2795            (or gnus-article-save-directory "~/News"))))
2796     (if (and last-file
2797              (string-equal (file-name-directory default)
2798                            (file-name-directory last-file))
2799              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2800         default
2801       (or last-file default))))
2802
2803 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2804   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2805 If variable `gnus-use-long-file-name' is non-nil, it is
2806 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2807   (or last-file
2808       (expand-file-name
2809        (if (gnus-use-long-file-name 'not-save)
2810            (gnus-capitalize-newsgroup newsgroup)
2811          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2812        (or gnus-article-save-directory "~/News"))))
2813
2814 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2815   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2816 If variable `gnus-use-long-file-name' is non-nil, it is
2817 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2818   (or last-file
2819       (expand-file-name
2820        (if (gnus-use-long-file-name 'not-save)
2821            newsgroup
2822          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2823        (or gnus-article-save-directory "~/News"))))
2824
2825 ;; For subscribing new newsgroup
2826
2827 (defun gnus-subscribe-hierarchical-interactive (groups)
2828   (let ((groups (sort groups 'string<))
2829         prefixes prefix start ans group starts)
2830     (while groups
2831       (setq prefixes (list "^"))
2832       (while (and groups prefixes)
2833         (while (not (string-match (car prefixes) (car groups)))
2834           (setq prefixes (cdr prefixes)))
2835         (setq prefix (car prefixes))
2836         (setq start (1- (length prefix)))
2837         (if (and (string-match "[^\\.]\\." (car groups) start)
2838                  (cdr groups)
2839                  (setq prefix
2840                        (concat "^" (substring (car groups) 0 (match-end 0))))
2841                  (string-match prefix (cadr groups)))
2842             (progn
2843               (setq prefixes (cons prefix prefixes))
2844               (message "Descend hierarchy %s? ([y]nsq): "
2845                        (substring prefix 1 (1- (length prefix))))
2846               (setq ans (read-char))
2847               (cond ((= ans ?n)
2848                      (while (and groups
2849                                  (string-match prefix
2850                                                (setq group (car groups))))
2851                        (setq gnus-killed-list
2852                              (cons group gnus-killed-list))
2853                        (gnus-sethash group group gnus-killed-hashtb)
2854                        (setq groups (cdr groups)))
2855                      (setq starts (cdr starts)))
2856                     ((= ans ?s)
2857                      (while (and groups
2858                                  (string-match prefix
2859                                                (setq group (car groups))))
2860                        (gnus-sethash group group gnus-killed-hashtb)
2861                        (gnus-subscribe-alphabetically (car groups))
2862                        (setq groups (cdr groups)))
2863                      (setq starts (cdr starts)))
2864                     ((= ans ?q)
2865                      (while groups
2866                        (setq group (car groups))
2867                        (setq gnus-killed-list (cons group gnus-killed-list))
2868                        (gnus-sethash group group gnus-killed-hashtb)
2869                        (setq groups (cdr groups))))
2870                     (t nil)))
2871           (message "Subscribe %s? ([n]yq)" (car groups))
2872           (setq ans (read-char))
2873           (setq group (car groups))
2874           (cond ((= ans ?y)
2875                  (gnus-subscribe-alphabetically (car groups))
2876                  (gnus-sethash group group gnus-killed-hashtb))
2877                 ((= ans ?q)
2878                  (while groups
2879                    (setq group (car groups))
2880                    (setq gnus-killed-list (cons group gnus-killed-list))
2881                    (gnus-sethash group group gnus-killed-hashtb)
2882                    (setq groups (cdr groups))))
2883                 (t
2884                  (setq gnus-killed-list (cons group gnus-killed-list))
2885                  (gnus-sethash group group gnus-killed-hashtb)))
2886           (setq groups (cdr groups)))))))
2887
2888 (defun gnus-subscribe-randomly (newsgroup)
2889   "Subscribe new NEWSGROUP by making it the first newsgroup."
2890   (gnus-subscribe-newsgroup newsgroup))
2891
2892 (defun gnus-subscribe-alphabetically (newgroup)
2893   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2894   (let ((groups (cdr gnus-newsrc-alist))
2895         before)
2896     (while (and (not before) groups)
2897       (if (string< newgroup (caar groups))
2898           (setq before (caar groups))
2899         (setq groups (cdr groups))))
2900     (gnus-subscribe-newsgroup newgroup before)))
2901
2902 (defun gnus-subscribe-hierarchically (newgroup)
2903   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2904   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2905   (save-excursion
2906     (set-buffer (find-file-noselect gnus-current-startup-file))
2907     (let ((groupkey newgroup)
2908           before)
2909       (while (and (not before) groupkey)
2910         (goto-char (point-min))
2911         (let ((groupkey-re
2912                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2913           (while (and (re-search-forward groupkey-re nil t)
2914                       (progn
2915                         (setq before (match-string 1))
2916                         (string< before newgroup)))))
2917         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2918         (setq groupkey
2919               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2920                   (substring groupkey (match-beginning 1) (match-end 1)))))
2921       (gnus-subscribe-newsgroup newgroup before))))
2922
2923 (defun gnus-subscribe-interactively (group)
2924   "Subscribe the new GROUP interactively.
2925 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2926 it is killed."
2927   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2928       (gnus-subscribe-hierarchically group)
2929     (push group gnus-killed-list)))
2930
2931 (defun gnus-subscribe-zombies (group)
2932   "Make the new GROUP into a zombie group."
2933   (push group gnus-zombie-list))
2934
2935 (defun gnus-subscribe-killed (group)
2936   "Make the new GROUP a killed group."
2937   (push group gnus-killed-list))
2938
2939 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2940   "Subscribe new NEWSGROUP.
2941 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2942 the first newsgroup."
2943   ;; We subscribe the group by changing its level to `subscribed'.
2944   (gnus-group-change-level
2945    newsgroup gnus-level-default-subscribed
2946    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2947   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2948
2949 ;; For directories
2950
2951 (defun gnus-newsgroup-directory-form (newsgroup)
2952   "Make hierarchical directory name from NEWSGROUP name."
2953   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2954         (len (length newsgroup))
2955         idx)
2956     ;; If this is a foreign group, we don't want to translate the
2957     ;; entire name.
2958     (if (setq idx (string-match ":" newsgroup))
2959         (aset newsgroup idx ?/)
2960       (setq idx 0))
2961     ;; Replace all occurrences of `.' with `/'.
2962     (while (< idx len)
2963       (if (= (aref newsgroup idx) ?.)
2964           (aset newsgroup idx ?/))
2965       (setq idx (1+ idx)))
2966     newsgroup))
2967
2968 (defun gnus-newsgroup-savable-name (group)
2969   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2970   ;; with dots.
2971   (nnheader-replace-chars-in-string group ?/ ?.))
2972
2973 (defun gnus-make-directory (dir)
2974   "Make DIRECTORY recursively."
2975   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2976   ;; of the many mysteries of the universe.
2977   (let* ((dir (expand-file-name dir default-directory))
2978          dirs err)
2979     (if (string-match "/$" dir)
2980         (setq dir (substring dir 0 (match-beginning 0))))
2981     ;; First go down the path until we find a directory that exists.
2982     (while (not (file-exists-p dir))
2983       (setq dirs (cons dir dirs))
2984       (string-match "/[^/]+$" dir)
2985       (setq dir (substring dir 0 (match-beginning 0))))
2986     ;; Then create all the subdirs.
2987     (while (and dirs (not err))
2988       (condition-case ()
2989           (make-directory (car dirs))
2990         (error (setq err t)))
2991       (setq dirs (cdr dirs)))
2992     ;; We return whether we were successful or not.
2993     (not dirs)))
2994
2995 (defun gnus-capitalize-newsgroup (newsgroup)
2996   "Capitalize NEWSGROUP name."
2997   (and (not (zerop (length newsgroup)))
2998        (concat (char-to-string (upcase (aref newsgroup 0)))
2999                (substring newsgroup 1))))
3000
3001 ;; Various... things.
3002
3003 (defun gnus-simplify-subject (subject &optional re-only)
3004   "Remove `Re:' and words in parentheses.
3005 If RE-ONLY is non-nil, strip leading `Re:'s only."
3006   (let ((case-fold-search t))           ;Ignore case.
3007     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3008     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3009       (setq subject (substring subject (match-end 0))))
3010     ;; Remove uninteresting prefixes.
3011     (if (and (not re-only)
3012              gnus-simplify-ignored-prefixes
3013              (string-match gnus-simplify-ignored-prefixes subject))
3014         (setq subject (substring subject (match-end 0))))
3015     ;; Remove words in parentheses from end.
3016     (unless re-only
3017       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3018         (setq subject (substring subject 0 (match-beginning 0)))))
3019     ;; Return subject string.
3020     subject))
3021
3022 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3023 ;; all whitespace.
3024 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3025 (defun gnus-simplify-buffer-fuzzy ()
3026   (goto-char (point-min))
3027   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
3028   (goto-char (match-beginning 0))
3029   (while (or
3030           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
3031           (looking-at "^[[].*:[ \t].*[]]$"))
3032     (goto-char (point-min))
3033     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
3034                               nil t)
3035       (replace-match "" t t))
3036     (goto-char (point-min))
3037     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
3038       (goto-char (match-end 0))
3039       (delete-char -1)
3040       (delete-region
3041        (progn (goto-char (match-beginning 0)))
3042        (re-search-forward ":"))))
3043   (goto-char (point-min))
3044   (while (re-search-forward "[ \t\n]*[[{(][^()\n]*[]})][ \t]*$" nil t)
3045     (replace-match "" t t))
3046   (goto-char (point-min))
3047   (while (re-search-forward "[ \t]+" nil t)
3048     (replace-match " " t t))
3049   (goto-char (point-min))
3050   (while (re-search-forward "[ \t]+$" nil t)
3051     (replace-match "" t t))
3052   (goto-char (point-min))
3053   (while (re-search-forward "^[ \t]+" nil t)
3054     (replace-match "" t t))
3055   (goto-char (point-min))
3056   (if gnus-simplify-subject-fuzzy-regexp
3057       (if (listp gnus-simplify-subject-fuzzy-regexp)
3058           (let ((list gnus-simplify-subject-fuzzy-regexp))
3059             (while list
3060               (goto-char (point-min))
3061               (while (re-search-forward (car list) nil t)
3062                 (replace-match "" t t))
3063               (setq list (cdr list))))
3064         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3065           (replace-match "" t t)))))
3066
3067 (defun gnus-simplify-subject-fuzzy (subject)
3068   "Siplify a subject string fuzzily."
3069   (save-excursion
3070     (gnus-set-work-buffer)
3071     (let ((case-fold-search t))
3072       (insert subject)
3073       (inline (gnus-simplify-buffer-fuzzy))
3074       (buffer-string))))
3075
3076 ;; Add the current buffer to the list of buffers to be killed on exit.
3077 (defun gnus-add-current-to-buffer-list ()
3078   (or (memq (current-buffer) gnus-buffer-list)
3079       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3080
3081 (defun gnus-string> (s1 s2)
3082   (not (or (string< s1 s2)
3083            (string= s1 s2))))
3084
3085 ;;; General various misc type functions.
3086
3087 (defun gnus-clear-system ()
3088   "Clear all variables and buffers."
3089   ;; Clear Gnus variables.
3090   (let ((variables gnus-variable-list))
3091     (while variables
3092       (set (car variables) nil)
3093       (setq variables (cdr variables))))
3094   ;; Clear other internal variables.
3095   (setq gnus-list-of-killed-groups nil
3096         gnus-have-read-active-file nil
3097         gnus-newsrc-alist nil
3098         gnus-newsrc-hashtb nil
3099         gnus-killed-list nil
3100         gnus-zombie-list nil
3101         gnus-killed-hashtb nil
3102         gnus-active-hashtb nil
3103         gnus-moderated-list nil
3104         gnus-description-hashtb nil
3105         gnus-current-headers nil
3106         gnus-thread-indent-array nil
3107         gnus-newsgroup-headers nil
3108         gnus-newsgroup-name nil
3109         gnus-server-alist nil
3110         gnus-group-list-mode nil
3111         gnus-opened-servers nil
3112         gnus-current-select-method nil)
3113   (gnus-shutdown 'gnus)
3114   ;; Kill the startup file.
3115   (and gnus-current-startup-file
3116        (get-file-buffer gnus-current-startup-file)
3117        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3118   ;; Clear the dribble buffer.
3119   (gnus-dribble-clear)
3120   ;; Kill global KILL file buffer.
3121   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3122     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3123   (gnus-kill-buffer nntp-server-buffer)
3124   ;; Kill Gnus buffers.
3125   (while gnus-buffer-list
3126     (gnus-kill-buffer (pop gnus-buffer-list)))
3127   ;; Remove Gnus frames.
3128   (while gnus-created-frames
3129     (when (frame-live-p (car gnus-created-frames))
3130       ;; We slap a condition-case around this `delete-frame' to ensure 
3131       ;; agains errors if we try do delete the single frame that's left.
3132       (condition-case ()
3133           (delete-frame (car gnus-created-frames))
3134         (error nil)))
3135     (pop gnus-created-frames)))
3136
3137 (defun gnus-windows-old-to-new (setting)
3138   ;; First we take care of the really, really old Gnus 3 actions.
3139   (when (symbolp setting)
3140     (setq setting
3141           ;; Take care of ooold GNUS 3.x values.
3142           (cond ((eq setting 'SelectArticle) 'article)
3143                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3144                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3145                 (t setting))))
3146   (if (or (listp setting)
3147           (not (and gnus-window-configuration
3148                     (memq setting '(group summary article)))))
3149       setting
3150     (let* ((setting (if (eq setting 'group)
3151                         (if (assq 'newsgroup gnus-window-configuration)
3152                             'newsgroup
3153                           'newsgroups) setting))
3154            (elem (cadr (assq setting gnus-window-configuration)))
3155            (total (apply '+ elem))
3156            (types '(group summary article))
3157            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3158            (i 0)
3159            perc
3160            out)
3161       (while (< i 3)
3162         (or (not (numberp (nth i elem)))
3163             (zerop (nth i elem))
3164             (progn
3165               (setq perc  (/ (float (nth 0 elem)) total))
3166               (setq out (cons (if (eq pbuf (nth i types))
3167                                   (vector (nth i types) perc 'point)
3168                                 (vector (nth i types) perc))
3169                               out))))
3170         (setq i (1+ i)))
3171       (list (nreverse out)))))
3172
3173 ;;;###autoload
3174 (defun gnus-add-configuration (conf)
3175   "Add the window configuration CONF to `gnus-buffer-configuration'."
3176   (setq gnus-buffer-configuration
3177         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3178                          gnus-buffer-configuration))))
3179
3180 (defvar gnus-frame-list nil)
3181
3182 (defun gnus-configure-frame (split &optional window)
3183   "Split WINDOW according to SPLIT."
3184   (unless window
3185     (setq window (get-buffer-window (current-buffer))))
3186   (select-window window)
3187   ;; This might be an old-stylee buffer config.
3188   (when (vectorp split)
3189     (setq split (append split nil)))
3190   (when (or (consp (car split))
3191             (vectorp (car split)))
3192     (push 1.0 split)
3193     (push 'vertical split))
3194   ;; The SPLIT might be something that is to be evaled to
3195   ;; return a new SPLIT.
3196   (while (and (not (assq (car split) gnus-window-to-buffer))
3197               (gnus-functionp (car split)))
3198     (setq split (eval split)))
3199   (let* ((type (car split))
3200          (subs (cddr split))
3201          (len (if (eq type 'horizontal) (window-width) (window-height)))
3202          (total 0)
3203          (window-min-width (or gnus-window-min-width window-min-width))
3204          (window-min-height (or gnus-window-min-height window-min-height))
3205          s result new-win rest comp-subs size sub)
3206     (cond
3207      ;; Nothing to do here.
3208      ((null split))
3209      ;; Don't switch buffers.
3210      ((null type)
3211       (and (memq 'point split) window))
3212      ;; This is a buffer to be selected.
3213      ((not (memq type '(frame horizontal vertical)))
3214       (let ((buffer (cond ((stringp type) type)
3215                           (t (cdr (assq type gnus-window-to-buffer)))))
3216             buf)
3217         (unless buffer
3218           (error "Illegal buffer type: %s" type))
3219         (unless (setq buf (get-buffer (if (symbolp buffer)
3220                                           (symbol-value buffer) buffer)))
3221           (setq buf (get-buffer-create (if (symbolp buffer)
3222                                            (symbol-value buffer) buffer))))
3223         (switch-to-buffer buf)
3224         ;; We return the window if it has the `point' spec.
3225         (and (memq 'point split) window)))
3226      ;; This is a frame split.
3227      ((eq type 'frame)
3228       (unless gnus-frame-list
3229         (setq gnus-frame-list (list (window-frame
3230                                      (get-buffer-window (current-buffer))))))
3231       (let ((i 0)
3232             params frame fresult)
3233         (while (< i (length subs))
3234           ;; Frame parameter is gotten from the sub-split.
3235           (setq params (cadr (elt subs i)))
3236           ;; It should be a list.
3237           (unless (listp params)
3238             (setq params nil))
3239           ;; Create a new frame?
3240           (unless (setq frame (elt gnus-frame-list i))
3241             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3242             (push frame gnus-created-frames))
3243           ;; Is the old frame still alive?
3244           (unless (frame-live-p frame)
3245             (setcar (nthcdr i gnus-frame-list)
3246                     (setq frame (make-frame params))))
3247           ;; Select the frame in question and do more splits there.
3248           (select-frame frame)
3249           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3250           (incf i))
3251         ;; Select the frame that has the selected buffer.
3252         (when fresult
3253           (select-frame (window-frame fresult)))))
3254      ;; This is a normal split.
3255      (t
3256       (when (> (length subs) 0)
3257         ;; First we have to compute the sizes of all new windows.
3258         (while subs
3259           (setq sub (append (pop subs) nil))
3260           (while (and (not (assq (car sub) gnus-window-to-buffer))
3261                       (gnus-functionp (car sub)))
3262             (setq sub (eval sub)))
3263           (when sub
3264             (push sub comp-subs)
3265             (setq size (cadar comp-subs))
3266             (cond ((equal size 1.0)
3267                    (setq rest (car comp-subs))
3268                    (setq s 0))
3269                   ((floatp size)
3270                    (setq s (floor (* size len))))
3271                   ((integerp size)
3272                    (setq s size))
3273                   (t
3274                    (error "Illegal size: %s" size)))
3275             ;; Try to make sure that we are inside the safe limits.
3276             (cond ((zerop s))
3277                   ((eq type 'horizontal)
3278                    (setq s (max s window-min-width)))
3279                   ((eq type 'vertical)
3280                    (setq s (max s window-min-height))))
3281             (setcar (cdar comp-subs) s)
3282             (incf total s)))
3283         ;; Take care of the "1.0" spec.
3284         (if rest
3285             (setcar (cdr rest) (- len total))
3286           (error "No 1.0 specs in %s" split))
3287         ;; The we do the actual splitting in a nice recursive
3288         ;; fashion.
3289         (setq comp-subs (nreverse comp-subs))
3290         (while comp-subs
3291           (if (null (cdr comp-subs))
3292               (setq new-win window)
3293             (setq new-win
3294                   (split-window window (cadar comp-subs)
3295                                 (eq type 'horizontal))))
3296           (setq result (or (gnus-configure-frame
3297                             (car comp-subs) window) result))
3298           (select-window new-win)
3299           (setq window new-win)
3300           (setq comp-subs (cdr comp-subs))))
3301       ;; Return the proper window, if any.
3302       (when result
3303         (select-window result))))))
3304
3305 (defvar gnus-frame-split-p nil)
3306
3307 (defun gnus-configure-windows (setting &optional force)
3308   (setq setting (gnus-windows-old-to-new setting))
3309   (let ((split (if (symbolp setting)
3310                    (cadr (assq setting gnus-buffer-configuration))
3311                  setting))
3312         all-visible)
3313
3314     (setq gnus-frame-split-p nil)
3315
3316     (unless split
3317       (error "No such setting: %s" setting))
3318
3319     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3320              (not force))
3321         ;; All the windows mentioned are already visible, so we just
3322         ;; put point in the assigned buffer, and do not touch the
3323         ;; winconf.
3324         (select-window all-visible)
3325
3326       ;; Either remove all windows or just remove all Gnus windows.
3327       (let ((frame (selected-frame)))
3328         (unwind-protect
3329             (if gnus-use-full-window
3330                 ;; We want to remove all other windows.
3331                 (if (not gnus-frame-split-p)
3332                     ;; This is not a `frame' split, so we ignore the
3333                     ;; other frames.  
3334                     (delete-other-windows)
3335                   ;; This is a `frame' split, so we delete all windows
3336                   ;; on all frames.
3337                   (mapcar 
3338                    (lambda (frame)
3339                      (unless (eq (cdr (assq 'minibuffer
3340                                             (frame-parameters frame)))
3341                                  'only)
3342                        (select-frame frame)
3343                        (delete-other-windows)))
3344                    (frame-list)))
3345               ;; Just remove some windows.
3346               (gnus-remove-some-windows)
3347               (switch-to-buffer nntp-server-buffer))
3348           (select-frame frame)))
3349
3350       (switch-to-buffer nntp-server-buffer)
3351       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3352
3353 (defun gnus-all-windows-visible-p (split)
3354   (when (vectorp split)
3355     (setq split (append split nil)))
3356   (when (or (consp (car split))
3357             (vectorp (car split)))
3358     (push 1.0 split)
3359     (push 'vertical split))
3360   ;; The SPLIT might be something that is to be evaled to
3361   ;; return a new SPLIT.
3362   (while (and (not (assq (car split) gnus-window-to-buffer))
3363               (gnus-functionp (car split)))
3364     (setq split (eval split)))
3365   (let* ((type (elt split 0)))
3366     (cond
3367      ((null split)
3368       t)
3369      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3370       (let ((buffer (cond ((stringp type) type)
3371                           (t (cdr (assq type gnus-window-to-buffer)))))
3372             win buf)
3373         (unless buffer
3374           (error "Illegal buffer type: %s" type))
3375         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3376                                       buffer)))
3377           (setq win (get-buffer-window buf t)))
3378         (when win
3379           (if (memq 'point split)
3380               win
3381             t))))
3382      (t
3383       (when (eq type 'frame)
3384         (setq gnus-frame-split-p t))
3385       (let ((n (mapcar 'gnus-all-windows-visible-p
3386                        (cddr split)))
3387             (win t))
3388         (while n
3389           (cond ((windowp (car n))
3390                  (setq win (car n)))
3391                 ((null (car n))
3392                  (setq win nil)))
3393           (setq n (cdr n)))
3394         win)))))
3395
3396 (defun gnus-window-top-edge (&optional window)
3397   (nth 1 (window-edges window)))
3398
3399 (defun gnus-remove-some-windows ()
3400   (let ((buffers gnus-window-to-buffer)
3401         buf bufs lowest-buf lowest)
3402     (save-excursion
3403       ;; Remove windows on all known Gnus buffers.
3404       (while buffers
3405         (setq buf (cdar buffers))
3406         (if (symbolp buf)
3407             (setq buf (and (boundp buf) (symbol-value buf))))
3408         (and buf
3409              (get-buffer-window buf)
3410              (progn
3411                (setq bufs (cons buf bufs))
3412                (pop-to-buffer buf)
3413                (if (or (not lowest)
3414                        (< (gnus-window-top-edge) lowest))
3415                    (progn
3416                      (setq lowest (gnus-window-top-edge))
3417                      (setq lowest-buf buf)))))
3418         (setq buffers (cdr buffers)))
3419       ;; Remove windows on *all* summary buffers.
3420       (walk-windows
3421        (lambda (win)
3422          (let ((buf (window-buffer win)))
3423            (if (string-match    "^\\*Summary" (buffer-name buf))
3424                (progn
3425                  (setq bufs (cons buf bufs))
3426                  (pop-to-buffer buf)
3427                  (if (or (not lowest)
3428                          (< (gnus-window-top-edge) lowest))
3429                      (progn
3430                        (setq lowest-buf buf)
3431                        (setq lowest (gnus-window-top-edge)))))))))
3432       (and lowest-buf
3433            (progn
3434              (pop-to-buffer lowest-buf)
3435              (switch-to-buffer nntp-server-buffer)))
3436       (while bufs
3437         (and (not (eq (car bufs) lowest-buf))
3438              (delete-windows-on (car bufs)))
3439         (setq bufs (cdr bufs))))))
3440
3441 (defun gnus-version ()
3442   "Version numbers of this version of Gnus."
3443   (interactive)
3444   (let ((methods gnus-valid-select-methods)
3445         (mess gnus-version)
3446         meth)
3447     ;; Go through all the legal select methods and add their version
3448     ;; numbers to the total version string.  Only the backends that are
3449     ;; currently in use will have their message numbers taken into
3450     ;; consideration.
3451     (while methods
3452       (setq meth (intern (concat (caar methods) "-version")))
3453       (and (boundp meth)
3454            (stringp (symbol-value meth))
3455            (setq mess (concat mess "; " (symbol-value meth))))
3456       (setq methods (cdr methods)))
3457     (gnus-message 2 mess)))
3458
3459 (defun gnus-info-find-node ()
3460   "Find Info documentation of Gnus."
3461   (interactive)
3462   ;; Enlarge info window if needed.
3463   (let ((mode major-mode)
3464         gnus-info-buffer)
3465     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3466     (setq gnus-info-buffer (current-buffer))
3467     (gnus-configure-windows 'info)))
3468
3469 (defun gnus-days-between (date1 date2)
3470   ;; Return the number of days between date1 and date2.
3471   (- (gnus-day-number date1) (gnus-day-number date2)))
3472
3473 (defun gnus-day-number (date)
3474   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3475                      (timezone-parse-date date))))
3476     (timezone-absolute-from-gregorian
3477      (nth 1 dat) (nth 2 dat) (car dat))))
3478
3479 (defun gnus-encode-date (date)
3480   "Convert DATE to internal time."
3481   (let* ((parse (timezone-parse-date date))
3482          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3483          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3484     (encode-time (caddr time) (cadr time) (car time)
3485                  (caddr date) (cadr date) (car date) (nth 4 date))))
3486
3487 (defun gnus-time-minus (t1 t2)
3488   "Subtract two internal times."
3489   (let ((borrow (< (cadr t1) (cadr t2))))
3490     (list (- (car t1) (car t2) (if borrow 1 0))
3491           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3492
3493 (defun gnus-file-newer-than (file date)
3494   (let ((fdate (nth 5 (file-attributes file))))
3495     (or (> (car fdate) (car date))
3496         (and (= (car fdate) (car date))
3497              (> (nth 1 fdate) (nth 1 date))))))
3498
3499 (defmacro gnus-local-set-keys (&rest plist)
3500   "Set the keys in PLIST in the current keymap."
3501   `(gnus-define-keys-1 (current-local-map) ',plist))
3502
3503 (defmacro gnus-define-keys (keymap &rest plist)
3504   "Define all keys in PLIST in KEYMAP."
3505   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3506
3507 (put 'gnus-define-keys 'lisp-indent-function 1)
3508 (put 'gnus-define-keys 'lisp-indent-hook 1)
3509 (put 'gnus-define-keymap 'lisp-indent-function 1)
3510 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3511
3512 (defmacro gnus-define-keymap (keymap &rest plist)
3513   "Define all keys in PLIST in KEYMAP."
3514   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3515
3516 (defun gnus-define-keys-1 (keymap plist)
3517   (when (null keymap)
3518     (error "Can't set keys in a null keymap"))
3519   (cond ((symbolp keymap)
3520          (setq keymap (symbol-value keymap)))
3521         ((keymapp keymap))
3522         ((listp keymap)
3523          (set (car keymap) nil)
3524          (define-prefix-command (car keymap))
3525          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3526          (setq keymap (symbol-value (car keymap)))))
3527   (let (key)
3528     (while plist
3529       (when (symbolp (setq key (pop plist)))
3530         (setq key (symbol-value key)))
3531       (define-key keymap key (pop plist)))))
3532
3533 (defun gnus-group-read-only-p (&optional group)
3534   "Check whether GROUP supports editing or not.
3535 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3536 that that variable is buffer-local to the summary buffers."
3537   (let ((group (or group gnus-newsgroup-name)))
3538     (not (gnus-check-backend-function 'request-replace-article group))))
3539
3540 (defun gnus-group-total-expirable-p (group)
3541   "Check whether GROUP is total-expirable or not."
3542   (let ((params (gnus-info-params (gnus-get-info group))))
3543     (or (memq 'total-expire params)
3544         (cdr (assq 'total-expire params)) ; (total-expire . t)
3545         (and gnus-total-expirable-newsgroups ; Check var.
3546              (string-match gnus-total-expirable-newsgroups group)))))
3547
3548 (defun gnus-group-auto-expirable-p (group)
3549   "Check whether GROUP is total-expirable or not."
3550   (let ((params (gnus-info-params (gnus-get-info group))))
3551     (or (memq 'auto-expire params)
3552         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3553         (and gnus-auto-expirable-newsgroups ; Check var.
3554              (string-match gnus-auto-expirable-newsgroups group)))))
3555
3556 (defun gnus-virtual-group-p (group)
3557   "Say whether GROUP is virtual or not."
3558   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3559                         gnus-valid-select-methods)))
3560
3561 (defsubst gnus-simplify-subject-fully (subject)
3562   "Simplify a subject string according to the user's wishes."
3563   (cond
3564    ((null gnus-summary-gather-subject-limit)
3565     (gnus-simplify-subject-re subject))
3566    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3567     (gnus-simplify-subject-fuzzy subject))
3568    ((numberp gnus-summary-gather-subject-limit)
3569     (gnus-limit-string (gnus-simplify-subject-re subject)
3570                        gnus-summary-gather-subject-limit))
3571    (t
3572     subject)))
3573
3574 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3575   "Check whether two subjects are equal.  If optional argument
3576 simple-first is t, first argument is already simplified."
3577   (cond
3578    ((null simple-first)
3579     (equal (gnus-simplify-subject-fully s1)
3580            (gnus-simplify-subject-fully s2)))
3581    (t
3582     (equal s1
3583            (gnus-simplify-subject-fully s2)))))
3584
3585 ;; Returns a list of writable groups.
3586 (defun gnus-writable-groups ()
3587   (let ((alist gnus-newsrc-alist)
3588         groups group)
3589     (while (setq group (car (pop alist)))
3590       (unless (gnus-group-read-only-p group)
3591         (push group groups)))
3592     (nreverse groups)))
3593
3594 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3595 ;; the echo area.
3596 (defun gnus-y-or-n-p (prompt)
3597   (prog1
3598       (y-or-n-p prompt)
3599     (message "")))
3600
3601 (defun gnus-yes-or-no-p (prompt)
3602   (prog1
3603       (yes-or-no-p prompt)
3604     (message "")))
3605
3606 ;; Check whether to use long file names.
3607 (defun gnus-use-long-file-name (symbol)
3608   ;; The variable has to be set...
3609   (and gnus-use-long-file-name
3610        ;; If it isn't a list, then we return t.
3611        (or (not (listp gnus-use-long-file-name))
3612            ;; If it is a list, and the list contains `symbol', we
3613            ;; return nil.
3614            (not (memq symbol gnus-use-long-file-name)))))
3615
3616 ;; I suspect there's a better way, but I haven't taken the time to do
3617 ;; it yet. -erik selberg@cs.washington.edu
3618 (defun gnus-dd-mmm (messy-date)
3619   "Return a string like DD-MMM from a big messy string"
3620   (let ((datevec (timezone-parse-date messy-date)))
3621     (format "%2s-%s"
3622             (condition-case ()
3623                 ;; Make sure leading zeroes are stripped.
3624                 (number-to-string (string-to-number (aref datevec 2)))
3625               (error "??"))
3626             (capitalize
3627              (or (car
3628                   (nth (1- (string-to-number (aref datevec 1)))
3629                        timezone-months-assoc))
3630                  "???")))))
3631
3632 ;; Make a hash table (default and minimum size is 255).
3633 ;; Optional argument HASHSIZE specifies the table size.
3634 (defun gnus-make-hashtable (&optional hashsize)
3635   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3636
3637 ;; Make a number that is suitable for hashing; bigger than MIN and one
3638 ;; less than 2^x.
3639 (defun gnus-create-hash-size (min)
3640   (let ((i 1))
3641     (while (< i min)
3642       (setq i (* 2 i)))
3643     (1- i)))
3644
3645 ;; Show message if message has a lower level than `gnus-verbose'.
3646 ;; Guideline for numbers:
3647 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3648 ;; for things that take a long time, 7 - not very important messages
3649 ;; on stuff, 9 - messages inside loops.
3650 (defun gnus-message (level &rest args)
3651   (if (<= level gnus-verbose)
3652       (apply 'message args)
3653     ;; We have to do this format thingy here even if the result isn't
3654     ;; shown - the return value has to be the same as the return value
3655     ;; from `message'.
3656     (apply 'format args)))
3657
3658 (defun gnus-functionp (form)
3659   "Return non-nil if FORM is funcallable."
3660   (or (and (symbolp form) (fboundp form))
3661       (and (listp form) (eq (car form) 'lambda))))
3662
3663 ;; Generate a unique new group name.
3664 (defun gnus-generate-new-group-name (leaf)
3665   (let ((name leaf)
3666         (num 0))
3667     (while (gnus-gethash name gnus-newsrc-hashtb)
3668       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3669     name))
3670
3671 ;; Find out whether the gnus-visual TYPE is wanted.
3672 (defun gnus-visual-p (&optional type class)
3673   (and gnus-visual                      ; Has to be non-nil, at least.
3674        (if (not type)                   ; We don't care about type.
3675            gnus-visual
3676          (if (listp gnus-visual)        ; It's a list, so we check it.
3677              (or (memq type gnus-visual)
3678                  (memq class gnus-visual))
3679            t))))
3680
3681 (defun gnus-parent-id (references)
3682   "Return the last Message-ID in REFERENCES."
3683   (when (and references
3684              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3685     (substring references (match-beginning 1) (match-end 1))))
3686
3687 (defun gnus-split-references (references)
3688   "Return a list of Message-IDs in REFERENCES."
3689   (let ((beg 0)
3690         ids)
3691     (while (string-match "<[^>]+>" references beg)
3692       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3693             ids))
3694     (nreverse ids)))
3695
3696 (defun gnus-ephemeral-group-p (group)
3697   "Say whether GROUP is ephemeral or not."
3698   (gnus-group-get-parameter group 'quit-config))
3699
3700 (defun gnus-group-quit-config (group)
3701   "Return the quit-config of GROUP."
3702   (gnus-group-get-parameter group 'quit-config))
3703
3704 (defun gnus-simplify-mode-line ()
3705   "Make mode lines a bit simpler."
3706   (setq mode-line-modified "-- ")
3707   (when (listp mode-line-format)
3708     (make-local-variable 'mode-line-format)
3709     (setq mode-line-format (copy-sequence mode-line-format))
3710     (when (equal (nth 3 mode-line-format) "   ")
3711       (setcar (nthcdr 3 mode-line-format) " "))))
3712
3713 ;;; List and range functions
3714
3715 (defun gnus-last-element (list)
3716   "Return last element of LIST."
3717   (while (cdr list)
3718     (setq list (cdr list)))
3719   (car list))
3720
3721 (defun gnus-copy-sequence (list)
3722   "Do a complete, total copy of a list."
3723   (if (and (consp list) (not (consp (cdr list))))
3724       (cons (car list) (cdr list))
3725     (mapcar (lambda (elem) (if (consp elem)
3726                                (if (consp (cdr elem))
3727                                    (gnus-copy-sequence elem)
3728                                  (cons (car elem) (cdr elem)))
3729                              elem))
3730             list)))
3731
3732 (defun gnus-set-difference (list1 list2)
3733   "Return a list of elements of LIST1 that do not appear in LIST2."
3734   (let ((list1 (copy-sequence list1)))
3735     (while list2
3736       (setq list1 (delq (car list2) list1))
3737       (setq list2 (cdr list2)))
3738     list1))
3739
3740 (defun gnus-sorted-complement (list1 list2)
3741   "Return a list of elements of LIST1 that do not appear in LIST2.
3742 Both lists have to be sorted over <."
3743   (let (out)
3744     (if (or (null list1) (null list2))
3745         (or list1 list2)
3746       (while (and list1 list2)
3747         (cond ((= (car list1) (car list2))
3748                (setq list1 (cdr list1)
3749                      list2 (cdr list2)))
3750               ((< (car list1) (car list2))
3751                (setq out (cons (car list1) out))
3752                (setq list1 (cdr list1)))
3753               (t
3754                (setq out (cons (car list2) out))
3755                (setq list2 (cdr list2)))))
3756       (nconc (nreverse out) (or list1 list2)))))
3757
3758 (defun gnus-intersection (list1 list2)
3759   (let ((result nil))
3760     (while list2
3761       (if (memq (car list2) list1)
3762           (setq result (cons (car list2) result)))
3763       (setq list2 (cdr list2)))
3764     result))
3765
3766 (defun gnus-sorted-intersection (list1 list2)
3767   ;; LIST1 and LIST2 have to be sorted over <.
3768   (let (out)
3769     (while (and list1 list2)
3770       (cond ((= (car list1) (car list2))
3771              (setq out (cons (car list1) out)
3772                    list1 (cdr list1)
3773                    list2 (cdr list2)))
3774             ((< (car list1) (car list2))
3775              (setq list1 (cdr list1)))
3776             (t
3777              (setq list2 (cdr list2)))))
3778     (nreverse out)))
3779
3780 (defun gnus-set-sorted-intersection (list1 list2)
3781   ;; LIST1 and LIST2 have to be sorted over <.
3782   ;; This function modifies LIST1.
3783   (let* ((top (cons nil list1))
3784          (prev top))
3785     (while (and list1 list2)
3786       (cond ((= (car list1) (car list2))
3787              (setq prev list1
3788                    list1 (cdr list1)
3789                    list2 (cdr list2)))
3790             ((< (car list1) (car list2))
3791              (setcdr prev (cdr list1))
3792              (setq list1 (cdr list1)))
3793             (t
3794              (setq list2 (cdr list2)))))
3795     (setcdr prev nil)
3796     (cdr top)))
3797
3798 (defun gnus-compress-sequence (numbers &optional always-list)
3799   "Convert list of numbers to a list of ranges or a single range.
3800 If ALWAYS-LIST is non-nil, this function will always release a list of
3801 ranges."
3802   (let* ((first (car numbers))
3803          (last (car numbers))
3804          result)
3805     (if (null numbers)
3806         nil
3807       (if (not (listp (cdr numbers)))
3808           numbers
3809         (while numbers
3810           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3811                 ((= (1+ last) (car numbers)) ;Still in sequence
3812                  (setq last (car numbers)))
3813                 (t                      ;End of one sequence
3814                  (setq result
3815                        (cons (if (= first last) first
3816                                (cons first last)) result))
3817                  (setq first (car numbers))
3818                  (setq last  (car numbers))))
3819           (setq numbers (cdr numbers)))
3820         (if (and (not always-list) (null result))
3821             (if (= first last) (list first) (cons first last))
3822           (nreverse (cons (if (= first last) first (cons first last))
3823                           result)))))))
3824
3825 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3826 (defun gnus-uncompress-range (ranges)
3827   "Expand a list of ranges into a list of numbers.
3828 RANGES is either a single range on the form `(num . num)' or a list of
3829 these ranges."
3830   (let (first last result)
3831     (cond
3832      ((null ranges)
3833       nil)
3834      ((not (listp (cdr ranges)))
3835       (setq first (car ranges))
3836       (setq last (cdr ranges))
3837       (while (<= first last)
3838         (setq result (cons first result))
3839         (setq first (1+ first)))
3840       (nreverse result))
3841      (t
3842       (while ranges
3843         (if (atom (car ranges))
3844             (if (numberp (car ranges))
3845                 (setq result (cons (car ranges) result)))
3846           (setq first (caar ranges))
3847           (setq last  (cdar ranges))
3848           (while (<= first last)
3849             (setq result (cons first result))
3850             (setq first (1+ first))))
3851         (setq ranges (cdr ranges)))
3852       (nreverse result)))))
3853
3854 (defun gnus-add-to-range (ranges list)
3855   "Return a list of ranges that has all articles from both RANGES and LIST.
3856 Note: LIST has to be sorted over `<'."
3857   (if (not ranges)
3858       (gnus-compress-sequence list t)
3859     (setq list (copy-sequence list))
3860     (or (listp (cdr ranges))
3861         (setq ranges (list ranges)))
3862     (let ((out ranges)
3863           ilist lowest highest temp)
3864       (while (and ranges list)
3865         (setq ilist list)
3866         (setq lowest (or (and (atom (car ranges)) (car ranges))
3867                          (caar ranges)))
3868         (while (and list (cdr list) (< (cadr list) lowest))
3869           (setq list (cdr list)))
3870         (if (< (car ilist) lowest)
3871             (progn
3872               (setq temp list)
3873               (setq list (cdr list))
3874               (setcdr temp nil)
3875               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3876         (setq highest (or (and (atom (car ranges)) (car ranges))
3877                           (cdar ranges)))
3878         (while (and list (<= (car list) highest))
3879           (setq list (cdr list)))
3880         (setq ranges (cdr ranges)))
3881       (if list
3882           (setq out (nconc (gnus-compress-sequence list t) out)))
3883       (setq out (sort out (lambda (r1 r2)
3884                             (< (or (and (atom r1) r1) (car r1))
3885                                (or (and (atom r2) r2) (car r2))))))
3886       (setq ranges out)
3887       (while ranges
3888         (if (atom (car ranges))
3889             (if (cdr ranges)
3890                 (if (atom (cadr ranges))
3891                     (if (= (1+ (car ranges)) (cadr ranges))
3892                         (progn
3893                           (setcar ranges (cons (car ranges)
3894                                                (cadr ranges)))
3895                           (setcdr ranges (cddr ranges))))
3896                   (if (= (1+ (car ranges)) (caadr ranges))
3897                       (progn
3898                         (setcar (cadr ranges) (car ranges))
3899                         (setcar ranges (cadr ranges))
3900                         (setcdr ranges (cddr ranges))))))
3901           (if (cdr ranges)
3902               (if (atom (cadr ranges))
3903                   (if (= (1+ (cdar ranges)) (cadr ranges))
3904                       (progn
3905                         (setcdr (car ranges) (cadr ranges))
3906                         (setcdr ranges (cddr ranges))))
3907                 (if (= (1+ (cdar ranges)) (caadr ranges))
3908                     (progn
3909                       (setcdr (car ranges) (cdadr ranges))
3910                       (setcdr ranges (cddr ranges)))))))
3911         (setq ranges (cdr ranges)))
3912       out)))
3913
3914 (defun gnus-remove-from-range (ranges list)
3915   "Return a list of ranges that has all articles from LIST removed from RANGES.
3916 Note: LIST has to be sorted over `<'."
3917   ;; !!! This function shouldn't look like this, but I've got a headache.
3918   (gnus-compress-sequence
3919    (gnus-sorted-complement
3920     (gnus-uncompress-range ranges) list)))
3921
3922 (defun gnus-member-of-range (number ranges)
3923   (if (not (listp (cdr ranges)))
3924       (and (>= number (car ranges))
3925            (<= number (cdr ranges)))
3926     (let ((not-stop t))
3927       (while (and ranges
3928                   (if (numberp (car ranges))
3929                       (>= number (car ranges))
3930                     (>= number (caar ranges)))
3931                   not-stop)
3932         (if (if (numberp (car ranges))
3933                 (= number (car ranges))
3934               (and (>= number (caar ranges))
3935                    (<= number (cdar ranges))))
3936             (setq not-stop nil))
3937         (setq ranges (cdr ranges)))
3938       (not not-stop))))
3939
3940 (defun gnus-range-length (range)
3941   "Return the length RANGE would have if uncompressed."
3942   (length (gnus-uncompress-range range)))
3943
3944 (defun gnus-sublist-p (list sublist)
3945   "Test whether all elements in SUBLIST are members of LIST."
3946   (let ((sublistp t))
3947     (while sublist
3948       (unless (memq (pop sublist) list)
3949         (setq sublistp nil
3950               sublist nil)))
3951     sublistp))
3952
3953 \f
3954 ;;;
3955 ;;; Gnus group mode
3956 ;;;
3957
3958 (defvar gnus-group-mode-map nil)
3959 (put 'gnus-group-mode 'mode-class 'special)
3960
3961 (unless gnus-group-mode-map
3962   (setq gnus-group-mode-map (make-keymap))
3963   (suppress-keymap gnus-group-mode-map)
3964
3965   (gnus-define-keys gnus-group-mode-map
3966     " " gnus-group-read-group
3967     "=" gnus-group-select-group
3968     "\r" gnus-group-select-group
3969     "\M-\r" gnus-group-quick-select-group
3970     "j" gnus-group-jump-to-group
3971     "n" gnus-group-next-unread-group
3972     "p" gnus-group-prev-unread-group
3973     "\177" gnus-group-prev-unread-group
3974     [delete] gnus-group-prev-unread-group
3975     "N" gnus-group-next-group
3976     "P" gnus-group-prev-group
3977     "\M-n" gnus-group-next-unread-group-same-level
3978     "\M-p" gnus-group-prev-unread-group-same-level
3979     "," gnus-group-best-unread-group
3980     "." gnus-group-first-unread-group
3981     "u" gnus-group-unsubscribe-current-group
3982     "U" gnus-group-unsubscribe-group
3983     "c" gnus-group-catchup-current
3984     "C" gnus-group-catchup-current-all
3985     "l" gnus-group-list-groups
3986     "L" gnus-group-list-all-groups
3987     "m" gnus-group-mail
3988     "g" gnus-group-get-new-news
3989     "\M-g" gnus-group-get-new-news-this-group
3990     "R" gnus-group-restart
3991     "r" gnus-group-read-init-file
3992     "B" gnus-group-browse-foreign-server
3993     "b" gnus-group-check-bogus-groups
3994     "F" gnus-find-new-newsgroups
3995     "\C-c\C-d" gnus-group-describe-group
3996     "\M-d" gnus-group-describe-all-groups
3997     "\C-c\C-a" gnus-group-apropos
3998     "\C-c\M-\C-a" gnus-group-description-apropos
3999     "a" gnus-group-post-news
4000     "\ek" gnus-group-edit-local-kill
4001     "\eK" gnus-group-edit-global-kill
4002     "\C-k" gnus-group-kill-group
4003     "\C-y" gnus-group-yank-group
4004     "\C-w" gnus-group-kill-region
4005     "\C-x\C-t" gnus-group-transpose-groups
4006     "\C-c\C-l" gnus-group-list-killed
4007     "\C-c\C-x" gnus-group-expire-articles
4008     "\C-c\M-\C-x" gnus-group-expire-all-groups
4009     "V" gnus-version
4010     "s" gnus-group-save-newsrc
4011     "z" gnus-group-suspend
4012     "Z" gnus-group-clear-dribble
4013     "q" gnus-group-exit
4014     "Q" gnus-group-quit
4015     "?" gnus-group-describe-briefly
4016     "\C-c\C-i" gnus-info-find-node
4017     "\M-e" gnus-group-edit-group-method
4018     "^" gnus-group-enter-server-mode
4019     gnus-mouse-2 gnus-mouse-pick-group
4020     "<" beginning-of-buffer
4021     ">" end-of-buffer
4022     "\C-c\C-b" gnus-bug
4023     "\C-c\C-s" gnus-group-sort-groups
4024     "t" gnus-topic-mode
4025     "\C-c\M-g" gnus-activate-all-groups
4026     "\M-&" gnus-group-universal-argument
4027     "#" gnus-group-mark-group
4028     "\M-#" gnus-group-unmark-group)
4029
4030   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4031     "m" gnus-group-mark-group
4032     "u" gnus-group-unmark-group
4033     "w" gnus-group-mark-region
4034     "m" gnus-group-mark-buffer
4035     "r" gnus-group-mark-regexp
4036     "U" gnus-group-unmark-all-groups)
4037
4038   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4039     "d" gnus-group-make-directory-group
4040     "h" gnus-group-make-help-group
4041     "a" gnus-group-make-archive-group
4042     "k" gnus-group-make-kiboze-group
4043     "m" gnus-group-make-group
4044     "E" gnus-group-edit-group
4045     "e" gnus-group-edit-group-method
4046     "p" gnus-group-edit-group-parameters
4047     "v" gnus-group-add-to-virtual
4048     "V" gnus-group-make-empty-virtual
4049     "D" gnus-group-enter-directory
4050     "f" gnus-group-make-doc-group
4051     "r" gnus-group-rename-group
4052     "\177" gnus-group-delete-group
4053     [delete] gnus-group-delete-group)
4054
4055    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4056      "b" gnus-group-brew-soup
4057      "w" gnus-soup-save-areas
4058      "s" gnus-soup-send-replies
4059      "p" gnus-soup-pack-packet
4060      "r" nnsoup-pack-replies)
4061
4062    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4063      "s" gnus-group-sort-groups
4064      "a" gnus-group-sort-groups-by-alphabet
4065      "u" gnus-group-sort-groups-by-unread
4066      "l" gnus-group-sort-groups-by-level
4067      "v" gnus-group-sort-groups-by-score
4068      "r" gnus-group-sort-groups-by-rank
4069      "m" gnus-group-sort-groups-by-method)
4070
4071    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4072      "k" gnus-group-list-killed
4073      "z" gnus-group-list-zombies
4074      "s" gnus-group-list-groups
4075      "u" gnus-group-list-all-groups
4076      "A" gnus-group-list-active
4077      "a" gnus-group-apropos
4078      "d" gnus-group-description-apropos
4079      "m" gnus-group-list-matching
4080      "M" gnus-group-list-all-matching
4081      "l" gnus-group-list-level)
4082
4083    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4084      "f" gnus-score-flush-cache)
4085
4086    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4087      "f" gnus-group-fetch-faq)
4088
4089    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4090      "l" gnus-group-set-current-level
4091      "t" gnus-group-unsubscribe-current-group
4092      "s" gnus-group-unsubscribe-group
4093      "k" gnus-group-kill-group
4094      "y" gnus-group-yank-group
4095      "w" gnus-group-kill-region
4096      "\C-k" gnus-group-kill-level
4097      "z" gnus-group-kill-all-zombies))
4098
4099 (defun gnus-group-mode ()
4100   "Major mode for reading news.
4101
4102 All normal editing commands are switched off.
4103 \\<gnus-group-mode-map>
4104 The group buffer lists (some of) the groups available.  For instance,
4105 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4106 lists all zombie groups.
4107
4108 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4109 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4110
4111 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4112
4113 The following commands are available:
4114
4115 \\{gnus-group-mode-map}"
4116   (interactive)
4117   (when (and menu-bar-mode
4118              (gnus-visual-p 'group-menu 'menu))
4119     (gnus-group-make-menu-bar))
4120   (kill-all-local-variables)
4121   (gnus-simplify-mode-line)
4122   (setq major-mode 'gnus-group-mode)
4123   (setq mode-name "Group")
4124   (gnus-group-set-mode-line)
4125   (setq mode-line-process nil)
4126   (use-local-map gnus-group-mode-map)
4127   (buffer-disable-undo (current-buffer))
4128   (setq truncate-lines t)
4129   (setq buffer-read-only t)
4130   (run-hooks 'gnus-group-mode-hook))
4131
4132 (defun gnus-mouse-pick-group (e)
4133   "Enter the group under the mouse pointer."
4134   (interactive "e")
4135   (mouse-set-point e)
4136   (gnus-group-read-group nil))
4137
4138 ;; Look at LEVEL and find out what the level is really supposed to be.
4139 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4140 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4141 (defun gnus-group-default-level (&optional level number-or-nil)
4142   (cond
4143    (gnus-group-use-permanent-levels
4144     (setq gnus-group-default-list-level
4145           (or level gnus-group-default-list-level))
4146     (or gnus-group-default-list-level gnus-level-subscribed))
4147    (number-or-nil
4148     level)
4149    (t
4150     (or level gnus-group-default-list-level gnus-level-subscribed))))
4151
4152 ;;;###autoload
4153 (defun gnus-slave-no-server (&optional arg)
4154   "Read network news as a slave, without connecting to local server"
4155   (interactive "P")
4156   (gnus-no-server arg t))
4157
4158 ;;;###autoload
4159 (defun gnus-no-server (&optional arg slave)
4160   "Read network news.
4161 If ARG is a positive number, Gnus will use that as the
4162 startup level.  If ARG is nil, Gnus will be started at level 2.
4163 If ARG is non-nil and not a positive number, Gnus will
4164 prompt the user for the name of an NNTP server to use.
4165 As opposed to `gnus', this command will not connect to the local server."
4166   (interactive "P")
4167   (let ((gnus-group-use-permanent-levels t))
4168     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4169   (make-local-variable 'gnus-group-use-permanent-levels)
4170   (setq gnus-group-use-permanent-levels t))
4171
4172 ;;;###autoload
4173 (defun gnus-slave (&optional arg)
4174   "Read news as a slave."
4175   (interactive "P")
4176   (gnus arg nil 'slave))
4177
4178 ;;;###autoload
4179 (defun gnus-other-frame (&optional arg)
4180   "Pop up a frame to read news."
4181   (interactive "P")
4182   (if (get-buffer gnus-group-buffer)
4183       (let ((pop-up-frames t))
4184         (gnus arg))
4185     (select-frame (make-frame))
4186     (gnus arg)))
4187
4188 ;;;###autoload
4189 (defun gnus (&optional arg dont-connect slave)
4190   "Read network news.
4191 If ARG is non-nil and a positive number, Gnus will use that as the
4192 startup level.  If ARG is non-nil and not a positive number, Gnus will
4193 prompt the user for the name of an NNTP server to use."
4194   (interactive "P")
4195
4196   (if (get-buffer gnus-group-buffer)
4197       (progn
4198         (switch-to-buffer gnus-group-buffer)
4199         (gnus-group-get-new-news))
4200
4201     (gnus-clear-system)
4202     (nnheader-init-server-buffer)
4203     (gnus-read-init-file)
4204     (setq gnus-slave slave)
4205
4206     (gnus-group-setup-buffer)
4207     (let ((buffer-read-only nil))
4208       (erase-buffer)
4209       (if (not gnus-inhibit-startup-message)
4210           (progn
4211             (gnus-group-startup-message)
4212             (sit-for 0))))
4213
4214     (let ((level (and (numberp arg) (> arg 0) arg))
4215           did-connect)
4216       (unwind-protect
4217           (progn
4218             (or dont-connect
4219                 (setq did-connect
4220                       (gnus-start-news-server (and arg (not level))))))
4221         (if (and (not dont-connect)
4222                  (not did-connect))
4223             (gnus-group-quit)
4224           (run-hooks 'gnus-startup-hook)
4225           ;; NNTP server is successfully open.
4226
4227           ;; Find the current startup file name.
4228           (setq gnus-current-startup-file
4229                 (gnus-make-newsrc-file gnus-startup-file))
4230
4231           ;; Read the dribble file.
4232           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4233
4234           ;; Allow using GroupLens predictions.
4235           (when gnus-use-grouplens
4236             (bbb-login)
4237             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4238
4239           (gnus-summary-make-display-table)
4240           ;; Do the actual startup.
4241           (gnus-setup-news nil level dont-connect)
4242           ;; Generate the group buffer.
4243           (gnus-group-list-groups level)
4244           (gnus-group-first-unread-group)
4245           (gnus-configure-windows 'group)
4246           (gnus-group-set-mode-line))))))
4247
4248 (defun gnus-unload ()
4249   "Unload all Gnus features."
4250   (interactive)
4251   (or (boundp 'load-history)
4252       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4253   (let ((history load-history)
4254         feature)
4255     (while history
4256       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4257            (setq feature (cdr (assq 'provide (car history))))
4258            (unload-feature feature 'force))
4259       (setq history (cdr history)))))
4260
4261 (defun gnus-compile ()
4262   "Byte-compile the user-defined format specs."
4263   (interactive)
4264   (let ((entries gnus-format-specs)
4265         entry gnus-tmp-func)
4266     (save-excursion
4267       (gnus-message 7 "Compiling format specs...")
4268
4269       (while entries
4270         (setq entry (pop entries))
4271         (if (eq (car entry) 'version)
4272             (setq gnus-format-specs (delq entry gnus-format-specs))
4273           (when (and (listp (caddr entry))
4274                      (not (eq 'byte-code (caaddr entry))))
4275             (fset 'gnus-tmp-func
4276                   `(lambda () ,(caddr entry)))
4277             (byte-compile 'gnus-tmp-func)
4278             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4279
4280       (push (cons 'version emacs-version) gnus-format-specs)
4281
4282       (gnus-message 7 "Compiling user specs...done"))))
4283
4284 (defun gnus-indent-rigidly (start end arg)
4285   "Indent rigidly using only spaces and no tabs."
4286   (save-excursion
4287     (save-restriction
4288       (narrow-to-region start end)
4289       (indent-rigidly start end arg)
4290       (goto-char (point-min))
4291       (while (search-forward "\t" nil t)
4292         (replace-match "        " t t)))))
4293
4294 (defun gnus-group-startup-message (&optional x y)
4295   "Insert startup message in current buffer."
4296   ;; Insert the message.
4297   (erase-buffer)
4298   (insert
4299    (format "              %s
4300           _    ___ _             _
4301           _ ___ __ ___  __    _ ___
4302           __   _     ___    __  ___
4303               _           ___     _
4304              _  _ __             _
4305              ___   __            _
4306                    __           _
4307                     _      _   _
4308                    _      _    _
4309                       _  _    _
4310                   __  ___
4311                  _   _ _     _
4312                 _   _
4313               _    _
4314              _    _
4315             _
4316           __
4317
4318 "
4319            ""))
4320   ;; And then hack it.
4321   (gnus-indent-rigidly (point-min) (point-max)
4322                        (/ (max (- (window-width) (or x 46)) 0) 2))
4323   (goto-char (point-min))
4324   (forward-line 1)
4325   (let* ((pheight (count-lines (point-min) (point-max)))
4326          (wheight (window-height))
4327          (rest (- wheight pheight)))
4328     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4329   ;; Fontify some.
4330   (goto-char (point-min))
4331   (and (search-forward "Praxis" nil t)
4332        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4333   (goto-char (point-min))
4334   (let* ((mode-string (gnus-group-set-mode-line)))
4335     (setq mode-line-buffer-identification
4336           (list (concat gnus-version (substring (car mode-string) 4))))
4337     (set-buffer-modified-p t)))
4338
4339 (defun gnus-group-setup-buffer ()
4340   (or (get-buffer gnus-group-buffer)
4341       (progn
4342         (switch-to-buffer gnus-group-buffer)
4343         (gnus-add-current-to-buffer-list)
4344         (gnus-group-mode)
4345         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4346
4347 (defun gnus-group-list-groups (&optional level unread lowest)
4348   "List newsgroups with level LEVEL or lower that have unread articles.
4349 Default is all subscribed groups.
4350 If argument UNREAD is non-nil, groups with no unread articles are also
4351 listed."
4352   (interactive (list (if current-prefix-arg
4353                          (prefix-numeric-value current-prefix-arg)
4354                        (or
4355                         (gnus-group-default-level nil t)
4356                         gnus-group-default-list-level
4357                         gnus-level-subscribed))))
4358   (or level
4359       (setq level (car gnus-group-list-mode)
4360             unread (cdr gnus-group-list-mode)))
4361   (setq level (gnus-group-default-level level))
4362   (gnus-group-setup-buffer)             ;May call from out of group buffer
4363   (gnus-update-format-specifications)
4364   (let ((case-fold-search nil)
4365         (props (text-properties-at (gnus-point-at-bol)))
4366         (group (gnus-group-group-name)))
4367     (set-buffer gnus-group-buffer)
4368     (funcall gnus-group-prepare-function level unread lowest)
4369     (if (zerop (buffer-size))
4370         (gnus-message 5 gnus-no-groups-message)
4371       (goto-char (point-max))
4372       (when (or (not gnus-group-goto-next-group-function)
4373                 (not (funcall gnus-group-goto-next-group-function 
4374                               group props)))
4375         (if (not group)
4376             ;; Go to the first group with unread articles.
4377             (gnus-group-search-forward t)
4378           ;; Find the right group to put point on.  If the current group
4379           ;; has disappeared in the new listing, try to find the next
4380           ;; one.        If no next one can be found, just leave point at the
4381           ;; first newsgroup in the buffer.
4382           (if (not (gnus-goto-char
4383                     (text-property-any
4384                      (point-min) (point-max)
4385                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4386               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4387                 (while (and newsrc
4388                             (not (gnus-goto-char
4389                                   (text-property-any
4390                                    (point-min) (point-max) 'gnus-group
4391                                    (gnus-intern-safe
4392                                     (caar newsrc) gnus-active-hashtb)))))
4393                   (setq newsrc (cdr newsrc)))
4394                 (or newsrc (progn (goto-char (point-max))
4395                                   (forward-line -1)))))))
4396       ;; Adjust cursor point.
4397       (gnus-group-position-point))))
4398
4399 (defun gnus-group-list-level (level &optional all)
4400   "List groups on LEVEL.
4401 If ALL (the prefix), also list groups that have no unread articles."
4402   (interactive "nList groups on level: \nP")
4403   (gnus-group-list-groups level all level))
4404
4405 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4406   "List all newsgroups with unread articles of level LEVEL or lower.
4407 If ALL is non-nil, list groups that have no unread articles.
4408 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4409 If REGEXP, only list groups matching REGEXP."
4410   (set-buffer gnus-group-buffer)
4411   (let ((buffer-read-only nil)
4412         (newsrc (cdr gnus-newsrc-alist))
4413         (lowest (or lowest 1))
4414         info clevel unread group params)
4415     (erase-buffer)
4416     (if (< lowest gnus-level-zombie)
4417         ;; List living groups.
4418         (while newsrc
4419           (setq info (car newsrc)
4420                 group (gnus-info-group info)
4421                 params (gnus-info-params info)
4422                 newsrc (cdr newsrc)
4423                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4424           (and unread                   ; This group might be bogus
4425                (or (not regexp)
4426                    (string-match regexp group))
4427                (<= (setq clevel (gnus-info-level info)) level)
4428                (>= clevel lowest)
4429                (or all                  ; We list all groups?
4430                    (and gnus-group-list-inactive-groups
4431                         (eq unread t))  ; We list unactivated groups
4432                    (> unread 0)         ; We list groups with unread articles
4433                    (and gnus-list-groups-with-ticked-articles
4434                         (cdr (assq 'tick (gnus-info-marks info))))
4435                                         ; And groups with tickeds
4436                    ;; Check for permanent visibility.
4437                    (and gnus-permanently-visible-groups
4438                         (string-match gnus-permanently-visible-groups
4439                                       group))
4440                    (memq 'visible params)
4441                    (cdr (assq 'visible params)))
4442                (gnus-group-insert-group-line
4443                 group (gnus-info-level info)
4444                 (gnus-info-marks info) unread (gnus-info-method info)))))
4445
4446     ;; List dead groups.
4447     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4448          (gnus-group-prepare-flat-list-dead
4449           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4450           gnus-level-zombie ?Z
4451           regexp))
4452     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4453          (gnus-group-prepare-flat-list-dead
4454           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4455           gnus-level-killed ?K regexp))
4456
4457     (gnus-group-set-mode-line)
4458     (setq gnus-group-list-mode (cons level all))
4459     (run-hooks 'gnus-group-prepare-hook)))
4460
4461 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4462   ;; List zombies and killed lists somewhat faster, which was
4463   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4464   ;; this by ignoring the group format specification altogether.
4465   (let (group)
4466     (if regexp
4467         ;; This loop is used when listing groups that match some
4468         ;; regexp.
4469         (while groups
4470           (setq group (pop groups))
4471           (when (string-match regexp group)
4472             (add-text-properties
4473              (point) (prog1 (1+ (point))
4474                        (insert " " mark "     *: " group "\n"))
4475              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4476                    'gnus-unread t
4477                    'gnus-level level))))
4478       ;; This loop is used when listing all groups.
4479       (while groups
4480         (add-text-properties
4481          (point) (prog1 (1+ (point))
4482                    (insert " " mark "     *: "
4483                            (setq group (pop groups)) "\n"))
4484          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4485                'gnus-unread t
4486                'gnus-level level))))))
4487
4488 (defmacro gnus-group-real-name (group)
4489   "Find the real name of a foreign newsgroup."
4490   `(let ((gname ,group))
4491      (if (string-match ":[^:]+$" gname)
4492          (substring gname (1+ (match-beginning 0)))
4493        gname)))
4494
4495 (defsubst gnus-server-add-address (method)
4496   (let ((method-name (symbol-name (car method))))
4497     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4498              (not (assq (intern (concat method-name "-address")) method)))
4499         (append method (list (list (intern (concat method-name "-address"))
4500                                    (nth 1 method))))
4501       method)))
4502
4503 (defsubst gnus-server-get-method (group method)
4504   ;; Input either a server name, and extended server name, or a
4505   ;; select method, and return a select method.
4506   (cond ((stringp method)
4507          (gnus-server-to-method method))
4508         ((and (stringp (car method)) group)
4509          (gnus-server-extend-method group method))
4510         (t
4511          (gnus-server-add-address method))))
4512
4513 (defun gnus-server-to-method (server)
4514   "Map virtual server names to select methods."
4515   (or 
4516    ;; Perhaps this is the native server?
4517    (and (equal server "native") gnus-select-method)
4518    ;; It should be in the server alist.
4519    (cdr (assoc server gnus-server-alist))
4520    ;; If not, we look through all the opened server
4521    ;; to see whether we can find it there.
4522    (let ((opened gnus-opened-servers))
4523      (while (and opened
4524                  (not (equal server (format "%s:%s" (caaar opened)
4525                                             (cadaar opened)))))
4526        (pop opened))
4527      (caar opened))))
4528
4529 (defmacro gnus-method-equal (ss1 ss2)
4530   "Say whether two servers are equal."
4531   `(let ((s1 ,ss1)
4532          (s2 ,ss2))
4533      (or (equal s1 s2)
4534          (and (= (length s1) (length s2))
4535               (progn
4536                 (while (and s1 (member (car s1) s2))
4537                   (setq s1 (cdr s1)))
4538                 (null s1))))))
4539
4540 (defun gnus-server-equal (m1 m2)
4541   "Say whether two methods are equal."
4542   (let ((m1 (cond ((null m1) gnus-select-method)
4543                   ((stringp m1) (gnus-server-to-method m1))
4544                   (t m1)))
4545         (m2 (cond ((null m2) gnus-select-method)
4546                   ((stringp m2) (gnus-server-to-method m2))
4547                   (t m2))))
4548     (gnus-method-equal m1 m2)))
4549
4550 (defun gnus-group-prefixed-name (group method)
4551   "Return the whole name from GROUP and METHOD."
4552   (and (stringp method) (setq method (gnus-server-to-method method)))
4553   (concat (format "%s" (car method))
4554           (if (and
4555                (or (assoc (format "%s" (car method)) 
4556                           (gnus-methods-using 'address))
4557                    (gnus-server-equal method gnus-message-archive-method))
4558                (nth 1 method)
4559                (not (string= (nth 1 method) "")))
4560               (concat "+" (nth 1 method)))
4561           ":" group))
4562
4563 (defun gnus-group-real-prefix (group)
4564   "Return the prefix of the current group name."
4565   (if (string-match "^[^:]+:" group)
4566       (substring group 0 (match-end 0))
4567     ""))
4568
4569 (defun gnus-group-method (group)
4570   "Return the server or method used for selecting GROUP."
4571   (let ((prefix (gnus-group-real-prefix group)))
4572     (if (equal prefix "")
4573         gnus-select-method
4574       (let ((servers gnus-opened-servers)
4575             (server "")
4576             backend possible found)
4577         (if (string-match "^[^\\+]+\\+" prefix)
4578             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4579                   server (substring prefix (match-end 0) (1- (length prefix))))
4580           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4581         (while servers
4582           (when (eq (caaar servers) backend)
4583             (setq possible (caar servers))
4584             (when (equal (cadaar servers) server)
4585               (setq found (caar servers))))
4586           (pop servers))
4587         (or (car (rassoc found gnus-server-alist))
4588             found
4589             (car (rassoc possible gnus-server-alist))
4590             possible
4591             (list backend server))))))
4592
4593 (defsubst gnus-secondary-method-p (method)
4594   "Return whether METHOD is a secondary select method."
4595   (let ((methods gnus-secondary-select-methods)
4596         (gmethod (gnus-server-get-method nil method)))
4597     (while (and methods
4598                 (not (equal (gnus-server-get-method nil (car methods))
4599                             gmethod)))
4600       (setq methods (cdr methods)))
4601     methods))
4602
4603 (defun gnus-group-foreign-p (group)
4604   "Say whether a group is foreign or not."
4605   (and (not (gnus-group-native-p group))
4606        (not (gnus-group-secondary-p group))))
4607
4608 (defun gnus-group-native-p (group)
4609   "Say whether the group is native or not."
4610   (not (string-match ":" group)))
4611
4612 (defun gnus-group-secondary-p (group)
4613   "Say whether the group is secondary or not."
4614   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4615
4616 (defun gnus-group-get-parameter (group &optional symbol)
4617   "Returns the group parameters for GROUP.
4618 If SYMBOL, return the value of that symbol in the group parameters."
4619   (let ((params (gnus-info-params (gnus-get-info group))))
4620     (if symbol
4621         (gnus-group-parameter-value params symbol)
4622       params)))
4623
4624 (defun gnus-group-parameter-value (params symbol)
4625   "Return the value of SYMBOL in group PARAMS."
4626   (or (car (memq symbol params))        ; It's either a simple symbol
4627       (cdr (assq symbol params))))      ; or a cons.
4628
4629 (defun gnus-group-add-parameter (group param)
4630   "Add parameter PARAM to GROUP."
4631   (let ((info (gnus-get-info group)))
4632     (if (not info)
4633         () ; This is a dead group.  We just ignore it.
4634       ;; Cons the new param to the old one and update.
4635       (gnus-group-set-info (cons param (gnus-info-params info))
4636                            group 'params))))
4637
4638 (defun gnus-group-set-parameter (group name value)
4639   "Set parameter NAME to VALUE in GROUP."
4640   (let ((info (gnus-get-info group)))
4641     (if (not info)
4642         () ; This is a dead group.  We just ignore it.
4643       (let ((old-params (gnus-info-params info))
4644             (new-params (list (cons name value))))
4645         (while old-params
4646           (if (or (not (listp (car old-params)))
4647                   (not (eq (caar old-params) name)))
4648               (setq new-params (append new-params (list (car old-params)))))
4649           (setq old-params (cdr old-params)))
4650         (gnus-group-set-info new-params group 'params)))))
4651
4652 (defun gnus-group-add-score (group &optional score)
4653   "Add SCORE to the GROUP score.
4654 If SCORE is nil, add 1 to the score of GROUP."
4655   (let ((info (gnus-get-info group)))
4656     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4657
4658 (defun gnus-summary-bubble-group ()
4659   "Increase the score of the current group.
4660 This is a handy function to add to `gnus-summary-exit-hook' to
4661 increase the score of each group you read."
4662   (gnus-group-add-score gnus-newsgroup-name))
4663
4664 (defun gnus-group-set-info (info &optional method-only-group part)
4665   (let* ((entry (gnus-gethash
4666                  (or method-only-group (gnus-info-group info))
4667                  gnus-newsrc-hashtb))
4668          (part-info info)
4669          (info (if method-only-group (nth 2 entry) info))
4670          method)
4671     (when method-only-group
4672       (unless entry
4673         (error "Trying to change non-existent group %s" method-only-group))
4674       ;; We have received parts of the actual group info - either the
4675       ;; select method or the group parameters.  We first check
4676       ;; whether we have to extend the info, and if so, do that.
4677       (let ((len (length info))
4678             (total (if (eq part 'method) 5 6)))
4679         (when (< len total)
4680           (setcdr (nthcdr (1- len) info)
4681                   (make-list (- total len) nil)))
4682         ;; Then we enter the new info.
4683         (setcar (nthcdr (1- total) info) part-info)))
4684     (unless entry
4685       ;; This is a new group, so we just create it.
4686       (save-excursion
4687         (set-buffer gnus-group-buffer)
4688         (setq method (gnus-info-method info))
4689         (when (gnus-server-equal method "native")
4690           (setq method nil))
4691         (if method
4692             ;; It's a foreign group...
4693             (gnus-group-make-group
4694              (gnus-group-real-name (gnus-info-group info))
4695              (if (stringp method) method
4696                (prin1-to-string (car method)))
4697              (and (consp method)
4698                   (nth 1 (gnus-info-method info))))
4699           ;; It's a native group.
4700           (gnus-group-make-group (gnus-info-group info)))
4701         (gnus-message 6 "Note: New group created")
4702         (setq entry
4703               (gnus-gethash (gnus-group-prefixed-name
4704                              (gnus-group-real-name (gnus-info-group info))
4705                              (or (gnus-info-method info) gnus-select-method))
4706                             gnus-newsrc-hashtb))))
4707     ;; Whether it was a new group or not, we now have the entry, so we
4708     ;; can do the update.
4709     (if entry
4710         (progn
4711           (setcar (nthcdr 2 entry) info)
4712           (when (and (not (eq (car entry) t))
4713                      (gnus-active (gnus-info-group info)))
4714             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4715       (error "No such group: %s" (gnus-info-group info)))))
4716
4717 (defun gnus-group-set-method-info (group select-method)
4718   (gnus-group-set-info select-method group 'method))
4719
4720 (defun gnus-group-set-params-info (group params)
4721   (gnus-group-set-info params group 'params))
4722
4723 (defun gnus-group-update-group-line ()
4724   "Update the current line in the group buffer."
4725   (let* ((buffer-read-only nil)
4726          (group (gnus-group-group-name))
4727          (gnus-group-indentation (gnus-group-group-indentation))
4728          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4729     (and entry
4730          (not (gnus-ephemeral-group-p group))
4731          (gnus-dribble-enter
4732           (concat "(gnus-group-set-info '"
4733                   (prin1-to-string (nth 2 entry)) ")")))
4734     (gnus-delete-line)
4735     (gnus-group-insert-group-line-info group)
4736     (forward-line -1)
4737     (gnus-group-position-point)))
4738
4739 (defun gnus-group-insert-group-line-info (group)
4740   "Insert GROUP on the current line."
4741   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4742         active info)
4743     (if entry
4744         (progn
4745           ;; (Un)subscribed group.
4746           (setq info (nth 2 entry))
4747           (gnus-group-insert-group-line
4748            group (gnus-info-level info) (gnus-info-marks info)
4749            (or (car entry) t) (gnus-info-method info)))
4750       ;; This group is dead.
4751       (gnus-group-insert-group-line
4752        group
4753        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4754        nil
4755        (if (setq active (gnus-active group))
4756            (- (1+ (cdr active)) (car active)) 0)
4757        nil))))
4758
4759 (defun gnus-group-insert-group-line
4760   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4761                   gnus-tmp-method)
4762   "Insert a group line in the group buffer."
4763   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4764          (gnus-tmp-number-total
4765           (if gnus-tmp-active
4766               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4767             0))
4768          (gnus-tmp-number-of-unread
4769           (if (numberp number) (int-to-string (max 0 number))
4770             "*"))
4771          (gnus-tmp-number-of-read
4772           (if (numberp number)
4773               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4774             "*"))
4775          (gnus-tmp-subscribed
4776           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4777                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4778                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4779                 (t ?K)))
4780          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4781          (gnus-tmp-newsgroup-description
4782           (if gnus-description-hashtb
4783               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4784             ""))
4785          (gnus-tmp-moderated
4786           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4787          (gnus-tmp-moderated-string
4788           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4789          (gnus-tmp-method
4790           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4791          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4792          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4793          (gnus-tmp-news-method-string
4794           (if gnus-tmp-method
4795               (format "(%s:%s)" (car gnus-tmp-method)
4796                       (cadr gnus-tmp-method)) ""))
4797          (gnus-tmp-marked-mark
4798           (if (and (numberp number)
4799                    (zerop number)
4800                    (cdr (assq 'tick gnus-tmp-marked)))
4801               ?* ? ))
4802          (gnus-tmp-process-marked
4803           (if (member gnus-tmp-group gnus-group-marked)
4804               gnus-process-mark ? ))
4805          (gnus-tmp-grouplens
4806           (or (and gnus-use-grouplens
4807                    (bbb-grouplens-group-p gnus-tmp-group))
4808               ""))
4809          (buffer-read-only nil)
4810          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4811     (beginning-of-line)
4812     (add-text-properties
4813      (point)
4814      (prog1 (1+ (point))
4815        ;; Insert the text.
4816        (eval gnus-group-line-format-spec))
4817      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4818        gnus-unread ,(if (numberp number)
4819                         (string-to-int gnus-tmp-number-of-unread)
4820                       t)
4821        gnus-marked ,gnus-tmp-marked-mark
4822        gnus-indentation ,gnus-group-indentation
4823        gnus-level ,gnus-tmp-level))
4824     (when (gnus-visual-p 'group-highlight 'highlight)
4825       (forward-line -1)
4826       (run-hooks 'gnus-group-update-hook)
4827       (forward-line))
4828     ;; Allow XEmacs to remove front-sticky text properties.
4829     (gnus-group-remove-excess-properties)))
4830
4831 (defun gnus-group-update-group (group &optional visible-only)
4832   "Update all lines where GROUP appear.
4833 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4834 already."
4835   (save-excursion
4836     (set-buffer gnus-group-buffer)
4837     ;; The buffer may be narrowed.
4838     (save-restriction
4839       (widen)
4840       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4841             (loc (point-min))
4842             found buffer-read-only)
4843         ;; Enter the current status into the dribble buffer.
4844         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4845           (if (and entry (not (gnus-ephemeral-group-p group)))
4846               (gnus-dribble-enter
4847                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4848                        ")"))))
4849         ;; Find all group instances.  If topics are in use, each group
4850         ;; may be listed in more than once.
4851         (while (setq loc (text-property-any
4852                           loc (point-max) 'gnus-group ident))
4853           (setq found t)
4854           (goto-char loc)
4855           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4856             (gnus-delete-line)
4857             (gnus-group-insert-group-line-info group))
4858           (setq loc (1+ loc)))
4859         (unless (or found visible-only)
4860           ;; No such line in the buffer, find out where it's supposed to
4861           ;; go, and insert it there (or at the end of the buffer).
4862           (if gnus-goto-missing-group-function
4863               (funcall gnus-goto-missing-group-function group)
4864             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4865               (while (and entry (car entry)
4866                           (not
4867                            (gnus-goto-char
4868                             (text-property-any
4869                              (point-min) (point-max)
4870                              'gnus-group (gnus-intern-safe
4871                                           (caar entry) gnus-active-hashtb)))))
4872                 (setq entry (cdr entry)))
4873               (or entry (goto-char (point-max)))))
4874           ;; Finally insert the line.
4875           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4876             (gnus-group-insert-group-line-info group)))
4877         (gnus-group-set-mode-line)))))
4878
4879 (defun gnus-group-set-mode-line ()
4880   (when (memq 'group gnus-updated-mode-lines)
4881     (let* ((gformat (or gnus-group-mode-line-format-spec
4882                         (setq gnus-group-mode-line-format-spec
4883                               (gnus-parse-format
4884                                gnus-group-mode-line-format
4885                                gnus-group-mode-line-format-alist))))
4886            (gnus-tmp-news-server (cadr gnus-select-method))
4887            (gnus-tmp-news-method (car gnus-select-method))
4888            (max-len 60)
4889            gnus-tmp-header                      ;Dummy binding for user-defined formats
4890            ;; Get the resulting string.
4891            (mode-string (eval gformat)))
4892       ;; If the line is too long, we chop it off.
4893       (when (> (length mode-string) max-len)
4894         (setq mode-string (substring mode-string 0 (- max-len 4))))
4895       (prog1
4896           (setq mode-line-buffer-identification (list mode-string))
4897         (set-buffer-modified-p t)))))
4898
4899 (defun gnus-group-group-name ()
4900   "Get the name of the newsgroup on the current line."
4901   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4902     (and group (symbol-name group))))
4903
4904 (defun gnus-group-group-level ()
4905   "Get the level of the newsgroup on the current line."
4906   (get-text-property (gnus-point-at-bol) 'gnus-level))
4907
4908 (defun gnus-group-group-indentation ()
4909   "Get the indentation of the newsgroup on the current line."
4910   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4911       (and gnus-group-indentation-function
4912            (funcall gnus-group-indentation-function))
4913       ""))
4914
4915 (defun gnus-group-group-unread ()
4916   "Get the number of unread articles of the newsgroup on the current line."
4917   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4918
4919 (defun gnus-group-search-forward (&optional backward all level first-too)
4920   "Find the next newsgroup with unread articles.
4921 If BACKWARD is non-nil, find the previous newsgroup instead.
4922 If ALL is non-nil, just find any newsgroup.
4923 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4924 group exists.
4925 If FIRST-TOO, the current line is also eligible as a target."
4926   (let ((way (if backward -1 1))
4927         (low gnus-level-killed)
4928         (beg (point))
4929         pos found lev)
4930     (if (and backward (progn (beginning-of-line)) (bobp))
4931         nil
4932       (or first-too (forward-line way))
4933       (while (and
4934               (not (eobp))
4935               (not (setq
4936                     found
4937                     (and (or all
4938                              (and
4939                               (let ((unread
4940                                      (get-text-property (point) 'gnus-unread)))
4941                                 (and (numberp unread) (> unread 0)))
4942                               (setq lev (get-text-property (point)
4943                                                            'gnus-level))
4944                               (<= lev gnus-level-subscribed)))
4945                          (or (not level)
4946                              (and (setq lev (get-text-property (point)
4947                                                                'gnus-level))
4948                                   (or (= lev level)
4949                                       (and (< lev low)
4950                                            (< level lev)
4951                                            (progn
4952                                              (setq low lev)
4953                                              (setq pos (point))
4954                                              nil))))))))
4955               (zerop (forward-line way)))))
4956     (if found
4957         (progn (gnus-group-position-point) t)
4958       (goto-char (or pos beg))
4959       (and pos t))))
4960
4961 ;;; Gnus group mode commands
4962
4963 ;; Group marking.
4964
4965 (defun gnus-group-mark-group (n &optional unmark no-advance)
4966   "Mark the current group."
4967   (interactive "p")
4968   (let ((buffer-read-only nil)
4969         group)
4970     (while
4971         (and (> n 0)
4972              (setq group (gnus-group-group-name))
4973              (progn
4974                (beginning-of-line)
4975                (forward-char
4976                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4977                (delete-char 1)
4978                (if unmark
4979                    (progn
4980                      (insert " ")
4981                      (setq gnus-group-marked (delete group gnus-group-marked)))
4982                  (insert "#")
4983                  (setq gnus-group-marked
4984                        (cons group (delete group gnus-group-marked))))
4985                t)
4986              (or no-advance (zerop (gnus-group-next-group 1))))
4987       (setq n (1- n)))
4988     (gnus-summary-position-point)
4989     n))
4990
4991 (defun gnus-group-unmark-group (n)
4992   "Remove the mark from the current group."
4993   (interactive "p")
4994   (gnus-group-mark-group n 'unmark)
4995   (gnus-group-position-point))
4996
4997 (defun gnus-group-unmark-all-groups ()
4998   "Unmark all groups."
4999   (interactive)
5000   (let ((groups gnus-group-marked))
5001     (save-excursion
5002       (while groups
5003         (gnus-group-remove-mark (pop groups)))))
5004   (gnus-group-position-point))
5005
5006 (defun gnus-group-mark-region (unmark beg end)
5007   "Mark all groups between point and mark.
5008 If UNMARK, remove the mark instead."
5009   (interactive "P\nr")
5010   (let ((num (count-lines beg end)))
5011     (save-excursion
5012       (goto-char beg)
5013       (- num (gnus-group-mark-group num unmark)))))
5014
5015 (defun gnus-group-mark-buffer (&optional unmark)
5016   "Mark all groups in the buffer.
5017 If UNMARK, remove the mark instead."
5018   (interactive "P")
5019   (gnus-group-mark-region unmark (point-min) (point-max)))
5020
5021 (defun gnus-group-mark-regexp (regexp)
5022   "Mark all groups that match some regexp."
5023   (interactive "sMark (regexp): ")
5024   (let ((alist (cdr gnus-newsrc-alist))
5025         group)
5026     (while alist
5027       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5028         (gnus-group-set-mark group))))
5029   (gnus-group-position-point))
5030
5031 (defun gnus-group-remove-mark (group)
5032   "Remove the process mark from GROUP and move point there.
5033 Return nil if the group isn't displayed."
5034   (if (gnus-group-goto-group group)
5035       (save-excursion
5036         (gnus-group-mark-group 1 'unmark t)
5037         t)
5038     (setq gnus-group-marked
5039           (delete group gnus-group-marked))
5040     nil))
5041
5042 (defun gnus-group-set-mark (group)
5043   "Set the process mark on GROUP."
5044   (if (gnus-group-goto-group group) 
5045       (save-excursion
5046         (gnus-group-mark-group 1 nil t))
5047     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5048
5049 (defun gnus-group-universal-argument (arg &optional groups func)
5050   "Perform any command on all groups accoring to the process/prefix convention."
5051   (interactive "P")
5052   (let ((groups (or groups (gnus-group-process-prefix arg)))
5053         group func)
5054     (if (eq (setq func (or func
5055                            (key-binding
5056                             (read-key-sequence
5057                              (substitute-command-keys
5058                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5059             'undefined)
5060         (progn
5061           (message "Undefined key")
5062           (ding))
5063       (while groups
5064         (gnus-group-remove-mark (setq group (pop groups)))
5065         (command-execute func))))
5066   (gnus-group-position-point))
5067
5068 (defun gnus-group-process-prefix (n)
5069   "Return a list of groups to work on.
5070 Take into consideration N (the prefix) and the list of marked groups."
5071   (cond
5072    (n
5073     (setq n (prefix-numeric-value n))
5074     ;; There is a prefix, so we return a list of the N next
5075     ;; groups.
5076     (let ((way (if (< n 0) -1 1))
5077           (n (abs n))
5078           group groups)
5079       (save-excursion
5080         (while (and (> n 0)
5081                     (setq group (gnus-group-group-name)))
5082           (setq groups (cons group groups))
5083           (setq n (1- n))
5084           (gnus-group-next-group way)))
5085       (nreverse groups)))
5086    ((and (boundp 'transient-mark-mode)
5087          transient-mark-mode
5088          mark-active)
5089     ;; Work on the region between point and mark.
5090     (let ((max (max (point) (mark)))
5091           groups)
5092       (save-excursion
5093         (goto-char (min (point) (mark)))
5094         (while
5095             (and
5096              (push (gnus-group-group-name) groups)
5097              (zerop (gnus-group-next-group 1))
5098              (< (point) max)))
5099         (nreverse groups))))
5100    (gnus-group-marked
5101     ;; No prefix, but a list of marked articles.
5102     (reverse gnus-group-marked))
5103    (t
5104     ;; Neither marked articles or a prefix, so we return the
5105     ;; current group.
5106     (let ((group (gnus-group-group-name)))
5107       (and group (list group))))))
5108
5109 ;; Selecting groups.
5110
5111 (defun gnus-group-read-group (&optional all no-article group)
5112   "Read news in this newsgroup.
5113 If the prefix argument ALL is non-nil, already read articles become
5114 readable.  IF ALL is a number, fetch this number of articles.  If the
5115 optional argument NO-ARTICLE is non-nil, no article will be
5116 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5117 group."
5118   (interactive "P")
5119   (let ((group (or group (gnus-group-group-name)))
5120         number active marked entry)
5121     (or group (error "No group on current line"))
5122     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5123                                             group gnus-newsrc-hashtb)))))
5124     ;; This group might be a dead group.  In that case we have to get
5125     ;; the number of unread articles from `gnus-active-hashtb'.
5126     (setq number
5127           (cond ((numberp all) all)
5128                 (entry (car entry))
5129                 ((setq active (gnus-active group))
5130                  (- (1+ (cdr active)) (car active)))))
5131     (gnus-summary-read-group
5132      group (or all (and (numberp number)
5133                         (zerop (+ number (length (cdr (assq 'tick marked)))
5134                                   (length (cdr (assq 'dormant marked)))))))
5135      no-article)))
5136
5137 (defun gnus-group-select-group (&optional all)
5138   "Select this newsgroup.
5139 No article is selected automatically.
5140 If ALL is non-nil, already read articles become readable.
5141 If ALL is a number, fetch this number of articles."
5142   (interactive "P")
5143   (gnus-group-read-group all t))
5144
5145 (defun gnus-group-quick-select-group (&optional all)
5146   "Select the current group \"quickly\".
5147 This means that no highlighting or scoring will be performed."
5148   (interactive "P")
5149   (let (gnus-visual
5150         gnus-score-find-score-files-function
5151         gnus-apply-kill-hook
5152         gnus-summary-expunge-below)
5153     (gnus-group-read-group all t)))
5154
5155 (defun gnus-group-visible-select-group (&optional all)
5156   "Select the current group without hiding any articles."
5157   (interactive "P")
5158   (let ((gnus-inhibit-limiting t))
5159     (gnus-group-read-group all t)))
5160
5161 ;;;###autoload
5162 (defun gnus-fetch-group (group)
5163   "Start Gnus if necessary and enter GROUP.
5164 Returns whether the fetching was successful or not."
5165   (interactive "sGroup name: ")
5166   (or (get-buffer gnus-group-buffer)
5167       (gnus))
5168   (gnus-group-read-group nil nil group))
5169
5170 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5171 ;; if selection was successful.
5172 (defun gnus-group-read-ephemeral-group
5173   (group method &optional activate quit-config)
5174   (let ((group (if (gnus-group-foreign-p group) group
5175                  (gnus-group-prefixed-name group method))))
5176     (gnus-sethash
5177      group
5178      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5179                      ((quit-config . ,(if quit-config quit-config
5180                                         (cons (current-buffer) 'summary))))))
5181      gnus-newsrc-hashtb)
5182     (set-buffer gnus-group-buffer)
5183     (or (gnus-check-server method)
5184         (error "Unable to contact server: %s" (gnus-status-message method)))
5185     (if activate (or (gnus-request-group group)
5186                      (error "Couldn't request group")))
5187     (condition-case ()
5188         (gnus-group-read-group t t group)
5189       (error nil)
5190       (quit nil))))
5191
5192 (defun gnus-group-jump-to-group (group)
5193   "Jump to newsgroup GROUP."
5194   (interactive
5195    (list (completing-read
5196           "Group: " gnus-active-hashtb nil
5197           (memq gnus-select-method gnus-have-read-active-file))))
5198
5199   (if (equal group "")
5200       (error "Empty group name"))
5201
5202   (let ((b (text-property-any
5203             (point-min) (point-max)
5204             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5205     (unless (gnus-ephemeral-group-p group)
5206       (if b
5207           ;; Either go to the line in the group buffer...
5208           (goto-char b)
5209         ;; ... or insert the line.
5210         (or
5211          (gnus-active group)
5212          (gnus-activate-group group)
5213          (error "%s error: %s" group (gnus-status-message group)))
5214
5215         (gnus-group-update-group group)
5216         (goto-char (text-property-any
5217                     (point-min) (point-max)
5218                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5219     ;; Adjust cursor point.
5220     (gnus-group-position-point)))
5221
5222 (defun gnus-group-goto-group (group)
5223   "Goto to newsgroup GROUP."
5224   (when group
5225     (let ((b (text-property-any (point-min) (point-max)
5226                                 'gnus-group (gnus-intern-safe
5227                                              group gnus-active-hashtb))))
5228       (and b (goto-char b)))))
5229
5230 (defun gnus-group-next-group (n)
5231   "Go to next N'th newsgroup.
5232 If N is negative, search backward instead.
5233 Returns the difference between N and the number of skips actually
5234 done."
5235   (interactive "p")
5236   (gnus-group-next-unread-group n t))
5237
5238 (defun gnus-group-next-unread-group (n &optional all level)
5239   "Go to next N'th unread newsgroup.
5240 If N is negative, search backward instead.
5241 If ALL is non-nil, choose any newsgroup, unread or not.
5242 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5243 such group can be found, the next group with a level higher than
5244 LEVEL.
5245 Returns the difference between N and the number of skips actually
5246 made."
5247   (interactive "p")
5248   (let ((backward (< n 0))
5249         (n (abs n)))
5250     (while (and (> n 0)
5251                 (gnus-group-search-forward
5252                  backward (or (not gnus-group-goto-unread) all) level))
5253       (setq n (1- n)))
5254     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5255                                (if level " on this level or higher" "")))
5256     n))
5257
5258 (defun gnus-group-prev-group (n)
5259   "Go to previous N'th newsgroup.
5260 Returns the difference between N and the number of skips actually
5261 done."
5262   (interactive "p")
5263   (gnus-group-next-unread-group (- n) t))
5264
5265 (defun gnus-group-prev-unread-group (n)
5266   "Go to previous N'th unread newsgroup.
5267 Returns the difference between N and the number of skips actually
5268 done."
5269   (interactive "p")
5270   (gnus-group-next-unread-group (- n)))
5271
5272 (defun gnus-group-next-unread-group-same-level (n)
5273   "Go to next N'th unread newsgroup on the same level.
5274 If N is negative, search backward instead.
5275 Returns the difference between N and the number of skips actually
5276 done."
5277   (interactive "p")
5278   (gnus-group-next-unread-group n t (gnus-group-group-level))
5279   (gnus-group-position-point))
5280
5281 (defun gnus-group-prev-unread-group-same-level (n)
5282   "Go to next N'th unread newsgroup on the same level.
5283 Returns the difference between N and the number of skips actually
5284 done."
5285   (interactive "p")
5286   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5287   (gnus-group-position-point))
5288
5289 (defun gnus-group-best-unread-group (&optional exclude-group)
5290   "Go to the group with the highest level.
5291 If EXCLUDE-GROUP, do not go to that group."
5292   (interactive)
5293   (goto-char (point-min))
5294   (let ((best 100000)
5295         unread best-point)
5296     (while (setq unread (get-text-property (point) 'gnus-unread))
5297       (if (and (numberp unread) (> unread 0))
5298           (progn
5299             (if (and (< (get-text-property (point) 'gnus-level) best)
5300                      (or (not exclude-group)
5301                          (not (equal exclude-group (gnus-group-group-name)))))
5302                 (progn
5303                   (setq best (get-text-property (point) 'gnus-level))
5304                   (setq best-point (point))))))
5305       (forward-line 1))
5306     (if best-point (goto-char best-point))
5307     (gnus-summary-position-point)
5308     (and best-point (gnus-group-group-name))))
5309
5310 (defun gnus-group-first-unread-group ()
5311   "Go to the first group with unread articles."
5312   (interactive)
5313   (prog1
5314       (let ((opoint (point))
5315             unread)
5316         (goto-char (point-min))
5317         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5318                 (and (numberp unread)   ; Not a topic.
5319                      (not (zerop unread))) ; Has unread articles.
5320                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5321             (point)                     ; Success.
5322           (goto-char opoint)
5323           nil))                         ; Not success.
5324     (gnus-group-position-point)))
5325
5326 (defun gnus-group-enter-server-mode ()
5327   "Jump to the server buffer."
5328   (interactive)
5329   (gnus-enter-server-buffer))
5330
5331 (defun gnus-group-make-group (name &optional method address)
5332   "Add a new newsgroup.
5333 The user will be prompted for a NAME, for a select METHOD, and an
5334 ADDRESS."
5335   (interactive
5336    (cons
5337     (read-string "Group name: ")
5338     (let ((method
5339            (completing-read
5340             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5341             nil t)))
5342       (if (assoc method gnus-valid-select-methods)
5343           (list method
5344                 (if (memq 'prompt-address
5345                           (assoc method gnus-valid-select-methods))
5346                     (read-string "Address: ")
5347                   ""))
5348         (list method "")))))
5349
5350   (save-excursion
5351     (set-buffer gnus-group-buffer)
5352     (let* ((meth (and method (if address (list (intern method) address)
5353                                method)))
5354            (nname (if method (gnus-group-prefixed-name name meth) name))
5355            info)
5356       (and (gnus-gethash nname gnus-newsrc-hashtb)
5357            (error "Group %s already exists" nname))
5358       (gnus-group-change-level
5359        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5360        gnus-level-default-subscribed gnus-level-killed
5361        (and (gnus-group-group-name)
5362             (gnus-gethash (gnus-group-group-name)
5363                           gnus-newsrc-hashtb))
5364        t)
5365       (gnus-set-active nname (cons 1 0))
5366       (or (gnus-ephemeral-group-p name)
5367           (gnus-dribble-enter
5368            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5369       (gnus-group-insert-group-line-info nname)
5370
5371       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5372         (require (car meth)))
5373       (gnus-check-server meth)
5374       (and (gnus-check-backend-function 'request-create-group nname)
5375            (gnus-request-create-group nname))
5376       t)))
5377
5378 (defun gnus-group-delete-group (group &optional force)
5379   "Delete the current group.
5380 If FORCE (the prefix) is non-nil, all the articles in the group will
5381 be deleted.  This is \"deleted\" as in \"removed forever from the face
5382 of the Earth\".  There is no undo."
5383   (interactive
5384    (list (gnus-group-group-name)
5385          current-prefix-arg))
5386   (or group (error "No group to rename"))
5387   (or (gnus-check-backend-function 'request-delete-group group)
5388       (error "This backend does not support group deletion"))
5389   (prog1
5390       (if (not (gnus-yes-or-no-p
5391                 (format
5392                  "Do you really want to delete %s%s? "
5393                  group (if force " and all its contents" ""))))
5394           () ; Whew!
5395         (gnus-message 6 "Deleting group %s..." group)
5396         (if (not (gnus-request-delete-group group force))
5397             (progn
5398               (gnus-message 3 "Couldn't delete group %s" group)
5399               (ding))
5400           (gnus-message 6 "Deleting group %s...done" group)
5401           (gnus-group-goto-group group)
5402           (gnus-group-kill-group 1 t)
5403           (gnus-sethash group nil gnus-active-hashtb)
5404           t))
5405     (gnus-group-position-point)))
5406
5407 (defun gnus-group-rename-group (group new-name)
5408   (interactive
5409    (list
5410     (gnus-group-group-name)
5411     (progn
5412       (or (gnus-check-backend-function
5413            'request-rename-group (gnus-group-group-name))
5414           (error "This backend does not support renaming groups"))
5415       (read-string "New group name: "))))
5416
5417   (or (gnus-check-backend-function 'request-rename-group group)
5418       (error "This backend does not support renaming groups"))
5419
5420   (or group (error "No group to rename"))
5421   (and (string-match "^[ \t]*$" new-name)
5422        (error "Not a valid group name"))
5423
5424   ;; We find the proper prefixed name.
5425   (setq new-name
5426         (gnus-group-prefixed-name
5427          (gnus-group-real-name new-name)
5428          (gnus-info-method (gnus-get-info group))))
5429
5430   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5431   (prog1
5432       (if (not (gnus-request-rename-group group new-name))
5433           (progn
5434             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5435             (ding))
5436         ;; We rename the group internally by killing it...
5437         (gnus-group-goto-group group)
5438         (gnus-group-kill-group)
5439         ;; ... changing its name ...
5440         (setcar (cdar gnus-list-of-killed-groups) new-name)
5441         ;; ... and then yanking it.  Magic!
5442         (gnus-group-yank-group)
5443         (gnus-set-active new-name (gnus-active group))
5444         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5445         new-name)
5446     (gnus-group-position-point)))
5447
5448 (defun gnus-group-edit-group (group &optional part)
5449   "Edit the group on the current line."
5450   (interactive (list (gnus-group-group-name)))
5451   (let* ((part (or part 'info))
5452          (done-func `(lambda ()
5453                        "Exit editing mode and update the information."
5454                        (interactive)
5455                        (gnus-group-edit-group-done ',part ,group)))
5456          (winconf (current-window-configuration))
5457          info)
5458     (or group (error "No group on current line"))
5459     (or (setq info (gnus-get-info group))
5460         (error "Killed group; can't be edited"))
5461     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5462     (gnus-configure-windows 'edit-group)
5463     (gnus-add-current-to-buffer-list)
5464     (emacs-lisp-mode)
5465     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5466     (use-local-map (copy-keymap emacs-lisp-mode-map))
5467     (local-set-key "\C-c\C-c" done-func)
5468     (make-local-variable 'gnus-prev-winconf)
5469     (setq gnus-prev-winconf winconf)
5470     (erase-buffer)
5471     (insert
5472      (cond
5473       ((eq part 'method)
5474        ";; Type `C-c C-c' after editing the select method.\n\n")
5475       ((eq part 'params)
5476        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5477       ((eq part 'info)
5478        ";; Type `C-c C-c' after editing the group info.\n\n")))
5479     (insert
5480      (pp-to-string
5481       (cond ((eq part 'method)
5482              (or (gnus-info-method info) "native"))
5483             ((eq part 'params)
5484              (gnus-info-params info))
5485             (t info)))
5486      "\n")))
5487
5488 (defun gnus-group-edit-group-method (group)
5489   "Edit the select method of GROUP."
5490   (interactive (list (gnus-group-group-name)))
5491   (gnus-group-edit-group group 'method))
5492
5493 (defun gnus-group-edit-group-parameters (group)
5494   "Edit the group parameters of GROUP."
5495   (interactive (list (gnus-group-group-name)))
5496   (gnus-group-edit-group group 'params))
5497
5498 (defun gnus-group-edit-group-done (part group)
5499   "Get info from buffer, update variables and jump to the group buffer."
5500   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5501   (goto-char (point-min))
5502   (let* ((form (read (current-buffer)))
5503          (winconf gnus-prev-winconf)
5504          (method (cond ((eq part 'info) (nth 4 form))
5505                        ((eq part 'method) form)
5506                        (t nil)))
5507          (info (cond ((eq part 'info) form)
5508                      ((eq part 'method) (gnus-get-info group))
5509                      (t nil)))
5510          (new-group (if info
5511                       (if (or (not method)
5512                               (gnus-server-equal
5513                                gnus-select-method method))
5514                           (gnus-group-real-name (car info))
5515                         (gnus-group-prefixed-name
5516                          (gnus-group-real-name (car info)) method))
5517                       nil)))
5518     (when (and new-group
5519                (not (equal new-group group)))
5520       (when (gnus-group-goto-group group)
5521         (gnus-group-kill-group 1))
5522       (gnus-activate-group new-group))
5523     ;; Set the info.
5524     (if (and info new-group)
5525         (progn
5526           (setq info (gnus-copy-sequence info))
5527           (setcar info new-group)
5528           (unless (gnus-server-equal method "native")
5529             (unless (nthcdr 3 info)
5530               (nconc info (list nil nil)))
5531             (unless (nthcdr 4 info)
5532               (nconc info (list nil)))
5533             (gnus-info-set-method info method))
5534           (gnus-group-set-info info))
5535       (gnus-group-set-info form (or new-group group) part))
5536     (kill-buffer (current-buffer))
5537     (and winconf (set-window-configuration winconf))
5538     (set-buffer gnus-group-buffer)
5539     (gnus-group-update-group (or new-group group))
5540     (gnus-group-position-point)))
5541
5542 (defun gnus-group-make-help-group ()
5543   "Create the Gnus documentation group."
5544   (interactive)
5545   (let ((path load-path)
5546         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5547         file dir)
5548     (and (gnus-gethash name gnus-newsrc-hashtb)
5549          (error "Documentation group already exists"))
5550     (while path
5551       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5552             file nil)
5553       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5554                 (file-exists-p
5555                  (setq file (concat (file-name-directory
5556                                      (directory-file-name dir))
5557                                     "etc/gnus-tut.txt"))))
5558         (setq path nil)))
5559     (if (not file)
5560         (message "Couldn't find doc group")
5561       (gnus-group-make-group
5562        (gnus-group-real-name name)
5563        (list 'nndoc name
5564              (list 'nndoc-address file)
5565              (list 'nndoc-article-type 'mbox)))))
5566   (gnus-group-position-point))
5567
5568 (defun gnus-group-make-doc-group (file type)
5569   "Create a group that uses a single file as the source."
5570   (interactive
5571    (list (read-file-name "File name: ")
5572          (and current-prefix-arg 'ask)))
5573   (when (eq type 'ask)
5574     (let ((err "")
5575           char found)
5576       (while (not found)
5577         (message
5578          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5579          err)
5580         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5581                           ((= char ?b) 'babyl)
5582                           ((= char ?d) 'digest)
5583                           ((= char ?f) 'forward)
5584                           ((= char ?a) 'mmfd)
5585                           (t (setq err (format "%c unknown. " char))
5586                              nil))))
5587       (setq type found)))
5588   (let* ((file (expand-file-name file))
5589          (name (gnus-generate-new-group-name
5590                 (gnus-group-prefixed-name
5591                  (file-name-nondirectory file) '(nndoc "")))))
5592     (gnus-group-make-group
5593      (gnus-group-real-name name)
5594      (list 'nndoc name
5595            (list 'nndoc-address file)
5596            (list 'nndoc-article-type (or type 'guess))))
5597     (forward-line -1)
5598     (gnus-group-position-point)))
5599
5600 (defun gnus-group-make-archive-group (&optional all)
5601   "Create the (ding) Gnus archive group of the most recent articles.
5602 Given a prefix, create a full group."
5603   (interactive "P")
5604   (let ((group (gnus-group-prefixed-name
5605                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5606     (and (gnus-gethash group gnus-newsrc-hashtb)
5607          (error "Archive group already exists"))
5608     (gnus-group-make-group
5609      (gnus-group-real-name group)
5610      (list 'nndir (if all "hpc" "edu")
5611            (list 'nndir-directory
5612                  (if all gnus-group-archive-directory
5613                    gnus-group-recent-archive-directory)))))
5614   (forward-line -1)
5615   (gnus-group-position-point))
5616
5617 (defun gnus-group-make-directory-group (dir)
5618   "Create an nndir group.
5619 The user will be prompted for a directory.  The contents of this
5620 directory will be used as a newsgroup.  The directory should contain
5621 mail messages or news articles in files that have numeric names."
5622   (interactive
5623    (list (read-file-name "Create group from directory: ")))
5624   (or (file-exists-p dir) (error "No such directory"))
5625   (or (file-directory-p dir) (error "Not a directory"))
5626   (let ((ext "")
5627         (i 0)
5628         group)
5629     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5630       (setq group
5631             (gnus-group-prefixed-name
5632              (concat (file-name-as-directory (directory-file-name dir))
5633                      ext)
5634              '(nndir "")))
5635       (setq ext (format "<%d>" (setq i (1+ i)))))
5636     (gnus-group-make-group
5637      (gnus-group-real-name group)
5638      (list 'nndir group (list 'nndir-directory dir))))
5639   (forward-line -1)
5640   (gnus-group-position-point))
5641
5642 (defun gnus-group-make-kiboze-group (group address scores)
5643   "Create an nnkiboze group.
5644 The user will be prompted for a name, a regexp to match groups, and
5645 score file entries for articles to include in the group."
5646   (interactive
5647    (list
5648     (read-string "nnkiboze group name: ")
5649     (read-string "Source groups (regexp): ")
5650     (let ((headers (mapcar (lambda (group) (list group))
5651                            '("subject" "from" "number" "date" "message-id"
5652                              "references" "chars" "lines" "xref"
5653                              "followup" "all" "body" "head")))
5654           scores header regexp regexps)
5655       (while (not (equal "" (setq header (completing-read
5656                                           "Match on header: " headers nil t))))
5657         (setq regexps nil)
5658         (while (not (equal "" (setq regexp (read-string
5659                                             (format "Match on %s (string): "
5660                                                     header)))))
5661           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5662         (setq scores (cons (cons header regexps) scores)))
5663       scores)))
5664   (gnus-group-make-group group "nnkiboze" address)
5665   (save-excursion
5666     (gnus-set-work-buffer)
5667     (let (emacs-lisp-mode-hook)
5668       (pp scores (current-buffer)))
5669     (write-region (point-min) (point-max)
5670                   (gnus-score-file-name (concat "nnkiboze:" group))))
5671   (forward-line -1)
5672   (gnus-group-position-point))
5673
5674 (defun gnus-group-add-to-virtual (n vgroup)
5675   "Add the current group to a virtual group."
5676   (interactive
5677    (list current-prefix-arg
5678          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5679                           "nnvirtual:")))
5680   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5681       (error "%s is not an nnvirtual group" vgroup))
5682   (let* ((groups (gnus-group-process-prefix n))
5683          (method (gnus-info-method (gnus-get-info vgroup))))
5684     (setcar (cdr method)
5685             (concat
5686              (nth 1 method) "\\|"
5687              (mapconcat
5688               (lambda (s)
5689                 (gnus-group-remove-mark s)
5690                 (concat "\\(^" (regexp-quote s) "$\\)"))
5691               groups "\\|"))))
5692   (gnus-group-position-point))
5693
5694 (defun gnus-group-make-empty-virtual (group)
5695   "Create a new, fresh, empty virtual group."
5696   (interactive "sCreate new, empty virtual group: ")
5697   (let* ((method (list 'nnvirtual "^$"))
5698          (pgroup (gnus-group-prefixed-name group method)))
5699     ;; Check whether it exists already.
5700     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5701          (error "Group %s already exists." pgroup))
5702     ;; Subscribe the new group after the group on the current line.
5703     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5704     (gnus-group-update-group pgroup)
5705     (forward-line -1)
5706     (gnus-group-position-point)))
5707
5708 (defun gnus-group-enter-directory (dir)
5709   "Enter an ephemeral nneething group."
5710   (interactive "DDirectory to read: ")
5711   (let* ((method (list 'nneething dir))
5712          (leaf (gnus-group-prefixed-name
5713                 (file-name-nondirectory (directory-file-name dir))
5714                 method))
5715          (name (gnus-generate-new-group-name leaf)))
5716     (let ((nneething-read-only t))
5717       (or (gnus-group-read-ephemeral-group
5718            name method t
5719            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5720                                       'summary 'group)))
5721           (error "Couldn't enter %s" dir)))))
5722
5723 ;; Group sorting commands
5724 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5725
5726 (defun gnus-group-sort-groups (func &optional reverse)
5727   "Sort the group buffer according to FUNC.
5728 If REVERSE, reverse the sorting order."
5729   (interactive (list gnus-group-sort-function
5730                      current-prefix-arg))
5731   (let ((func (cond 
5732                ((not (listp func)) func)
5733                ((null func) func)
5734                ((= 1 (length func)) (car func))
5735                (t `(lambda (t1 t2)
5736                      ,(gnus-make-sort-function 
5737                        (reverse func)))))))
5738     ;; We peel off the dummy group from the alist.
5739     (when func
5740       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5741         (pop gnus-newsrc-alist))
5742       ;; Do the sorting.
5743       (setq gnus-newsrc-alist
5744             (sort gnus-newsrc-alist func))
5745       (when reverse
5746         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5747       ;; Regenerate the hash table.
5748       (gnus-make-hashtable-from-newsrc-alist)
5749       (gnus-group-list-groups))))
5750
5751 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5752   "Sort the group buffer alphabetically by group name.
5753 If REVERSE, sort in reverse order."
5754   (interactive "P")
5755   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5756
5757 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5758   "Sort the group buffer by number of unread articles.
5759 If REVERSE, sort in reverse order."
5760   (interactive "P")
5761   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5762
5763 (defun gnus-group-sort-groups-by-level (&optional reverse)
5764   "Sort the group buffer by group level.
5765 If REVERSE, sort in reverse order."
5766   (interactive "P")
5767   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5768
5769 (defun gnus-group-sort-groups-by-score (&optional reverse)
5770   "Sort the group buffer by group score.
5771 If REVERSE, sort in reverse order."
5772   (interactive "P")
5773   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5774
5775 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5776   "Sort the group buffer by group rank.
5777 If REVERSE, sort in reverse order."
5778   (interactive "P")
5779   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5780
5781 (defun gnus-group-sort-groups-by-method (&optional reverse)
5782   "Sort the group buffer alphabetically by backend name.
5783 If REVERSE, sort in reverse order."
5784   (interactive "P")
5785   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5786
5787 (defun gnus-group-sort-by-alphabet (info1 info2)
5788   "Sort alphabetically."
5789   (string< (gnus-info-group info1) (gnus-info-group info2)))
5790
5791 (defun gnus-group-sort-by-unread (info1 info2)
5792   "Sort by number of unread articles."
5793   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5794         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5795     (< (or (and (numberp n1) n1) 0)
5796        (or (and (numberp n2) n2) 0))))
5797
5798 (defun gnus-group-sort-by-level (info1 info2)
5799   "Sort by level."
5800   (< (gnus-info-level info1) (gnus-info-level info2)))
5801
5802 (defun gnus-group-sort-by-method (info1 info2)
5803   "Sort alphabetically by backend name."
5804   (string< (symbol-name (car (gnus-find-method-for-group
5805                               (gnus-info-group info1) info1)))
5806            (symbol-name (car (gnus-find-method-for-group
5807                               (gnus-info-group info2) info2)))))
5808
5809 (defun gnus-group-sort-by-score (info1 info2)
5810   "Sort by group score."
5811   (< (gnus-info-score info1) (gnus-info-score info2)))
5812
5813 (defun gnus-group-sort-by-rank (info1 info2)
5814   "Sort by level and score."
5815   (let ((level1 (gnus-info-level info1))
5816         (level2 (gnus-info-level info2)))
5817     (or (< level1 level2)
5818         (and (= level1 level2)
5819              (< (gnus-info-score info1) (gnus-info-score info2))))))
5820
5821 ;; Group catching up.
5822
5823 (defun gnus-group-catchup-current (&optional n all)
5824   "Mark all articles not marked as unread in current newsgroup as read.
5825 If prefix argument N is numeric, the ARG next newsgroups will be
5826 caught up.  If ALL is non-nil, marked articles will also be marked as
5827 read.  Cross references (Xref: header) of articles are ignored.
5828 The difference between N and actual number of newsgroups that were
5829 caught up is returned."
5830   (interactive "P")
5831   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5832                gnus-expert-user
5833                (gnus-y-or-n-p
5834                 (if all
5835                     "Do you really want to mark all articles as read? "
5836                   "Mark all unread articles as read? "))))
5837       n
5838     (let ((groups (gnus-group-process-prefix n))
5839           (ret 0))
5840       (while groups
5841         ;; Virtual groups have to be given special treatment.
5842         (let ((method (gnus-find-method-for-group (car groups))))
5843           (if (eq 'nnvirtual (car method))
5844               (nnvirtual-catchup-group
5845                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5846         (gnus-group-remove-mark (car groups))
5847         (if (prog1
5848                 (gnus-group-goto-group (car groups))
5849               (gnus-group-catchup (car groups) all))
5850             (gnus-group-update-group-line)
5851           (setq ret (1+ ret)))
5852         (setq groups (cdr groups)))
5853       (gnus-group-next-unread-group 1)
5854       ret)))
5855
5856 (defun gnus-group-catchup-current-all (&optional n)
5857   "Mark all articles in current newsgroup as read.
5858 Cross references (Xref: header) of articles are ignored."
5859   (interactive "P")
5860   (gnus-group-catchup-current n 'all))
5861
5862 (defun gnus-group-catchup (group &optional all)
5863   "Mark all articles in GROUP as read.
5864 If ALL is non-nil, all articles are marked as read.
5865 The return value is the number of articles that were marked as read,
5866 or nil if no action could be taken."
5867   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5868          (num (car entry)))
5869     ;; Do the updating only if the newsgroup isn't killed.
5870     (if (not (numberp (car entry)))
5871         (gnus-message 1 "Can't catch up; non-active group")
5872       ;; Do auto-expirable marks if that's required.
5873       (when (gnus-group-auto-expirable-p group)
5874         (gnus-add-marked-articles
5875          group 'expire (gnus-list-of-unread-articles group))
5876         (when all
5877           (let ((marks (nth 3 (nth 2 entry))))
5878             (gnus-add-marked-articles
5879              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5880             (gnus-add-marked-articles
5881              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5882       (when entry
5883         (gnus-update-read-articles group nil)
5884         ;; Also nix out the lists of marks and dormants.
5885         (when all
5886           (gnus-add-marked-articles group 'tick nil nil 'force)
5887           (gnus-add-marked-articles group 'dormant nil nil 'force))
5888         (run-hooks 'gnus-group-catchup-group-hook)
5889         num))))
5890
5891 (defun gnus-group-expire-articles (&optional n)
5892   "Expire all expirable articles in the current newsgroup."
5893   (interactive "P")
5894   (let ((groups (gnus-group-process-prefix n))
5895         group)
5896     (unless groups
5897       (error "No groups to expire"))
5898     (while (setq group (pop groups))
5899       (gnus-group-remove-mark group)
5900       (when (gnus-check-backend-function 'request-expire-articles group)
5901         (gnus-message 6 "Expiring articles in %s..." group)
5902         (let* ((info (gnus-get-info group))
5903                (expirable (if (gnus-group-total-expirable-p group)
5904                               (cons nil (gnus-list-of-read-articles group))
5905                             (assq 'expire (gnus-info-marks info))))
5906                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5907           (when expirable
5908             (setcdr
5909              expirable
5910              (gnus-compress-sequence
5911               (if expiry-wait
5912                   ;; We set the expiry variables to the groupp
5913                   ;; parameter. 
5914                   (let ((nnmail-expiry-wait-function nil)
5915                         (nnmail-expiry-wait expiry-wait))
5916                     (gnus-request-expire-articles
5917                      (gnus-uncompress-sequence (cdr expirable)) group))
5918                 ;; Just expire using the normal expiry values.
5919                 (gnus-request-expire-articles
5920                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5921           (gnus-message 6 "Expiring articles in %s...done" group)))
5922       (gnus-group-position-point))))
5923
5924 (defun gnus-group-expire-all-groups ()
5925   "Expire all expirable articles in all newsgroups."
5926   (interactive)
5927   (save-excursion
5928     (gnus-message 5 "Expiring...")
5929     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5930                                      (cdr gnus-newsrc-alist))))
5931       (gnus-group-expire-articles nil)))
5932   (gnus-group-position-point)
5933   (gnus-message 5 "Expiring...done"))
5934
5935 (defun gnus-group-set-current-level (n level)
5936   "Set the level of the next N groups to LEVEL."
5937   (interactive
5938    (list
5939     current-prefix-arg
5940     (string-to-int
5941      (let ((s (read-string
5942                (format "Level (default %s): "
5943                        (or (gnus-group-group-level) 
5944                            gnus-level-default-subscribed)))))
5945        (if (string-match "^\\s-*$" s)
5946            (int-to-string (or (gnus-group-group-level) 
5947                               gnus-level-default-subscribed))
5948          s)))))
5949   (or (and (>= level 1) (<= level gnus-level-killed))
5950       (error "Illegal level: %d" level))
5951   (let ((groups (gnus-group-process-prefix n))
5952         group)
5953     (while (setq group (pop groups))
5954       (gnus-group-remove-mark group)
5955       (gnus-message 6 "Changed level of %s from %d to %d"
5956                     group (or (gnus-group-group-level) gnus-level-killed)
5957                     level)
5958       (gnus-group-change-level
5959        group level (or (gnus-group-group-level) gnus-level-killed))
5960       (gnus-group-update-group-line)))
5961   (gnus-group-position-point))
5962
5963 (defun gnus-group-unsubscribe-current-group (&optional n)
5964   "Toggle subscription of the current group.
5965 If given numerical prefix, toggle the N next groups."
5966   (interactive "P")
5967   (let ((groups (gnus-group-process-prefix n))
5968         group)
5969     (while groups
5970       (setq group (car groups)
5971             groups (cdr groups))
5972       (gnus-group-remove-mark group)
5973       (gnus-group-unsubscribe-group
5974        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5975                  gnus-level-default-unsubscribed
5976                gnus-level-default-subscribed) t)
5977       (gnus-group-update-group-line))
5978     (gnus-group-next-group 1)))
5979
5980 (defun gnus-group-unsubscribe-group (group &optional level silent)
5981   "Toggle subscription to GROUP.
5982 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5983 group line."
5984   (interactive
5985    (list (completing-read
5986           "Group: " gnus-active-hashtb nil
5987           (memq gnus-select-method gnus-have-read-active-file))))
5988   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5989     (cond
5990      ((string-match "^[ \t]$" group)
5991       (error "Empty group name"))
5992      (newsrc
5993       ;; Toggle subscription flag.
5994       (gnus-group-change-level
5995        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
5996                                       gnus-level-subscribed)
5997                                   (1+ gnus-level-subscribed)
5998                                 gnus-level-default-subscribed)))
5999       (unless silent
6000         (gnus-group-update-group group)))
6001      ((and (stringp group)
6002            (or (not (memq gnus-select-method gnus-have-read-active-file))
6003                (gnus-active group)))
6004       ;; Add new newsgroup.
6005       (gnus-group-change-level
6006        group
6007        (if level level gnus-level-default-subscribed)
6008        (or (and (member group gnus-zombie-list)
6009                 gnus-level-zombie)
6010            gnus-level-killed)
6011        (and (gnus-group-group-name)
6012             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6013       (unless silent
6014         (gnus-group-update-group group)))
6015      (t (error "No such newsgroup: %s" group)))
6016     (gnus-group-position-point)))
6017
6018 (defun gnus-group-transpose-groups (n)
6019   "Move the current newsgroup up N places.
6020 If given a negative prefix, move down instead.  The difference between
6021 N and the number of steps taken is returned."
6022   (interactive "p")
6023   (or (gnus-group-group-name)
6024       (error "No group on current line"))
6025   (gnus-group-kill-group 1)
6026   (prog1
6027       (forward-line (- n))
6028     (gnus-group-yank-group)
6029     (gnus-group-position-point)))
6030
6031 (defun gnus-group-kill-all-zombies ()
6032   "Kill all zombie newsgroups."
6033   (interactive)
6034   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6035   (setq gnus-zombie-list nil)
6036   (gnus-group-list-groups))
6037
6038 (defun gnus-group-kill-region (begin end)
6039   "Kill newsgroups in current region (excluding current point).
6040 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6041   (interactive "r")
6042   (let ((lines
6043          ;; Count lines.
6044          (save-excursion
6045            (count-lines
6046             (progn
6047               (goto-char begin)
6048               (beginning-of-line)
6049               (point))
6050             (progn
6051               (goto-char end)
6052               (beginning-of-line)
6053               (point))))))
6054     (goto-char begin)
6055     (beginning-of-line)                 ;Important when LINES < 1
6056     (gnus-group-kill-group lines)))
6057
6058 (defun gnus-group-kill-group (&optional n discard)
6059   "Kill the next N groups.
6060 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6061 However, only groups that were alive can be yanked; already killed
6062 groups or zombie groups can't be yanked.
6063 The return value is the name of the group that was killed, or a list
6064 of groups killed."
6065   (interactive "P")
6066   (let ((buffer-read-only nil)
6067         (groups (gnus-group-process-prefix n))
6068         group entry level out)
6069     (if (< (length groups) 10)
6070         ;; This is faster when there are few groups.
6071         (while groups
6072           (push (setq group (pop groups)) out)
6073           (gnus-group-remove-mark group)
6074           (setq level (gnus-group-group-level))
6075           (gnus-delete-line)
6076           (when (and (not discard)
6077                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6078             (push (cons (car entry) (nth 2 entry))
6079                   gnus-list-of-killed-groups))
6080           (gnus-group-change-level
6081            (if entry entry group) gnus-level-killed (if entry nil level)))
6082       ;; If there are lots and lots of groups to be killed, we use
6083       ;; this thing instead.
6084       (let (entry)
6085         (setq groups (nreverse groups))
6086         (while groups
6087           (gnus-group-remove-mark (setq group (pop groups)))
6088           (gnus-delete-line)
6089           (cond
6090            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6091             (push (cons (car entry) (nth 2 entry))
6092                   gnus-list-of-killed-groups)
6093             (setcdr (cdr entry) (cdddr entry)))
6094            ((member group gnus-zombie-list)
6095             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6096         (gnus-make-hashtable-from-newsrc-alist)))
6097
6098     (gnus-group-position-point)
6099     (if (< (length out) 2) (car out) (nreverse out))))
6100
6101 (defun gnus-group-yank-group (&optional arg)
6102   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6103 inserting it before the current newsgroup.  The numeric ARG specifies
6104 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6105 is returned, or (if several groups are yanked) a list of yanked groups
6106 is returned."
6107   (interactive "p")
6108   (setq arg (or arg 1))
6109   (let (info group prev out)
6110     (while (>= (decf arg) 0)
6111       (if (not (setq info (pop gnus-list-of-killed-groups)))
6112           (error "No more newsgroups to yank"))
6113       (push (setq group (nth 1 info)) out)
6114       ;; Find which newsgroup to insert this one before - search
6115       ;; backward until something suitable is found.  If there are no
6116       ;; other newsgroups in this buffer, just make this newsgroup the
6117       ;; first newsgroup.
6118       (setq prev (gnus-group-group-name))
6119       (gnus-group-change-level
6120        info (gnus-info-level (cdr info)) gnus-level-killed
6121        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6122        t)
6123       (gnus-group-insert-group-line-info group))
6124     (forward-line -1)
6125     (gnus-group-position-point)
6126     (if (< (length out) 2) (car out) (nreverse out))))
6127
6128 (defun gnus-group-kill-level (level)
6129   "Kill all groups that is on a certain LEVEL."
6130   (interactive "nKill all groups on level: ")
6131   (cond
6132    ((= level gnus-level-zombie)
6133     (setq gnus-killed-list
6134           (nconc gnus-zombie-list gnus-killed-list))
6135     (setq gnus-zombie-list nil))
6136    ((and (< level gnus-level-zombie)
6137          (> level 0)
6138          (or gnus-expert-user
6139              (gnus-yes-or-no-p
6140               (format
6141                "Do you really want to kill all groups on level %d? "
6142                level))))
6143     (let* ((prev gnus-newsrc-alist)
6144            (alist (cdr prev)))
6145       (while alist
6146         (if (= (gnus-info-level level) level)
6147             (setcdr prev (cdr alist))
6148           (setq prev alist))
6149         (setq alist (cdr alist)))
6150       (gnus-make-hashtable-from-newsrc-alist)
6151       (gnus-group-list-groups)))
6152    (t
6153     (error "Can't kill; illegal level: %d" level))))
6154
6155 (defun gnus-group-list-all-groups (&optional arg)
6156   "List all newsgroups with level ARG or lower.
6157 Default is gnus-level-unsubscribed, which lists all subscribed and most
6158 unsubscribed groups."
6159   (interactive "P")
6160   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6161
6162 ;; Redefine this to list ALL killed groups if prefix arg used.
6163 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6164 (defun gnus-group-list-killed (&optional arg)
6165   "List all killed newsgroups in the group buffer.
6166 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6167 entail asking the server for the groups."
6168   (interactive "P")
6169   ;; Find all possible killed newsgroups if arg.
6170   (when arg
6171     ;; First make sure active file has been read.
6172     (unless gnus-have-read-active-file
6173       (let ((gnus-read-active-file t))
6174         (gnus-read-active-file)))
6175     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6176     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6177     (mapatoms
6178      (lambda (sym)
6179        (let ((groups 0)
6180              (group (symbol-name sym)))
6181          (if (or (null group)
6182                  (gnus-gethash group gnus-killed-hashtb)
6183                  (gnus-gethash group gnus-newsrc-hashtb))
6184              ()
6185            (let ((do-sub (gnus-matches-options-n group)))
6186              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6187                  ()
6188                (setq groups (1+ groups))
6189                (setq gnus-killed-list
6190                      (cons group gnus-killed-list))
6191                (gnus-sethash group group gnus-killed-hashtb))))))
6192      gnus-active-hashtb))
6193   (if (not gnus-killed-list)
6194       (gnus-message 6 "No killed groups")
6195     (let (gnus-group-list-mode)
6196       (funcall gnus-group-prepare-function
6197                gnus-level-killed t gnus-level-killed))
6198     (goto-char (point-min)))
6199   (gnus-group-position-point))
6200
6201 (defun gnus-group-list-zombies ()
6202   "List all zombie newsgroups in the group buffer."
6203   (interactive)
6204   (if (not gnus-zombie-list)
6205       (gnus-message 6 "No zombie groups")
6206     (let (gnus-group-list-mode)
6207       (funcall gnus-group-prepare-function
6208                gnus-level-zombie t gnus-level-zombie))
6209     (goto-char (point-min)))
6210   (gnus-group-position-point))
6211
6212 (defun gnus-group-list-active ()
6213   "List all groups that are available from the server(s)."
6214   (interactive)
6215   ;; First we make sure that we have really read the active file.
6216   (unless gnus-have-read-active-file
6217     (let ((gnus-read-active-file t))
6218       (gnus-read-active-file)))
6219   ;; Find all groups and sort them.
6220   (let ((groups
6221          (sort
6222           (let (list)
6223             (mapatoms
6224              (lambda (sym)
6225                (and (symbol-value sym)
6226                     (setq list (cons (symbol-name sym) list))))
6227              gnus-active-hashtb)
6228             list)
6229           'string<))
6230         (buffer-read-only nil))
6231     (erase-buffer)
6232     (while groups
6233       (gnus-group-insert-group-line-info (car groups))
6234       (setq groups (cdr groups)))
6235     (goto-char (point-min))))
6236
6237 (defun gnus-activate-all-groups (level)
6238   "Activate absolutely all groups."
6239   (interactive (list 7))
6240   (let ((gnus-activate-level level)
6241         (gnus-activate-foreign-newsgroups level))
6242     (gnus-group-get-new-news)))
6243
6244 (defun gnus-group-get-new-news (&optional arg)
6245   "Get newly arrived articles.
6246 If ARG is a number, it specifies which levels you are interested in
6247 re-scanning.  If ARG is non-nil and not a number, this will force
6248 \"hard\" re-reading of the active files from all servers."
6249   (interactive "P")
6250   (run-hooks 'gnus-get-new-news-hook)
6251   ;; We might read in new NoCeM messages here.
6252   (when (and gnus-use-nocem 
6253              (null arg))
6254     (gnus-nocem-scan-groups))
6255   ;; If ARG is not a number, then we read the active file.
6256   (when (and arg (not (numberp arg)))
6257     (let ((gnus-read-active-file t))
6258       (gnus-read-active-file))
6259     (setq arg nil))
6260
6261   (setq arg (gnus-group-default-level arg t))
6262   (if (and gnus-read-active-file (not arg))
6263       (progn
6264         (gnus-read-active-file)
6265         (gnus-get-unread-articles arg))
6266     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6267       (gnus-get-unread-articles arg)))
6268   (run-hooks 'gnus-after-getting-new-news-hook)
6269   (gnus-group-list-groups))
6270
6271 (defun gnus-group-get-new-news-this-group (&optional n)
6272   "Check for newly arrived news in the current group (and the N-1 next groups).
6273 The difference between N and the number of newsgroup checked is returned.
6274 If N is negative, this group and the N-1 previous groups will be checked."
6275   (interactive "P")
6276   (let* ((groups (gnus-group-process-prefix n))
6277          (ret (if (numberp n) (- n (length groups)) 0))
6278          group)
6279     (while groups
6280       (setq group (car groups)
6281             groups (cdr groups))
6282       (gnus-group-remove-mark group)
6283       (unless (gnus-get-new-news-in-group group)
6284         (ding)
6285         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6286     (when gnus-goto-next-group-when-activating
6287       (gnus-group-next-unread-group 1 t))
6288     (gnus-summary-position-point)
6289     ret))
6290
6291 (defun gnus-get-new-news-in-group (group)
6292   (when (and group (gnus-activate-group group 'scan))
6293     (gnus-get-unread-articles-in-group
6294      (gnus-get-info group) (gnus-active group) t)
6295     (gnus-close-group group)
6296     (when (gnus-group-goto-group group)
6297       (gnus-group-update-group-line))
6298     t))
6299
6300 (defun gnus-group-fetch-faq (group &optional faq-dir)
6301   "Fetch the FAQ for the current group."
6302   (interactive
6303    (list
6304     (gnus-group-real-name (gnus-group-group-name))
6305     (cond (current-prefix-arg
6306            (completing-read
6307             "Faq dir: " (and (listp gnus-group-faq-directory)
6308                              gnus-group-faq-directory))))))
6309   (or faq-dir
6310       (setq faq-dir (if (listp gnus-group-faq-directory)
6311                         (car gnus-group-faq-directory)
6312                       gnus-group-faq-directory)))
6313   (or group (error "No group name given"))
6314   (let ((file (concat (file-name-as-directory faq-dir)
6315                       (gnus-group-real-name group))))
6316     (if (not (file-exists-p file))
6317         (error "No such file: %s" file)
6318       (find-file file))))
6319
6320 (defun gnus-group-describe-group (force &optional group)
6321   "Display a description of the current newsgroup."
6322   (interactive (list current-prefix-arg (gnus-group-group-name)))
6323   (and force (setq gnus-description-hashtb nil))
6324   (let ((method (gnus-find-method-for-group group))
6325         desc)
6326     (or group (error "No group name given"))
6327     (and (or (and gnus-description-hashtb
6328                   ;; We check whether this group's method has been
6329                   ;; queried for a description file.
6330                   (gnus-gethash
6331                    (gnus-group-prefixed-name "" method)
6332                    gnus-description-hashtb))
6333              (setq desc (gnus-group-get-description group))
6334              (gnus-read-descriptions-file method))
6335          (message
6336           (or desc (gnus-gethash group gnus-description-hashtb)
6337               "No description available")))))
6338
6339 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6340 (defun gnus-group-describe-all-groups (&optional force)
6341   "Pop up a buffer with descriptions of all newsgroups."
6342   (interactive "P")
6343   (and force (setq gnus-description-hashtb nil))
6344   (if (not (or gnus-description-hashtb
6345                (gnus-read-all-descriptions-files)))
6346       (error "Couldn't request descriptions file"))
6347   (let ((buffer-read-only nil)
6348         b)
6349     (erase-buffer)
6350     (mapatoms
6351      (lambda (group)
6352        (setq b (point))
6353        (insert (format "      *: %-20s %s\n" (symbol-name group)
6354                        (symbol-value group)))
6355        (add-text-properties
6356         b (1+ b) (list 'gnus-group group
6357                        'gnus-unread t 'gnus-marked nil
6358                        'gnus-level (1+ gnus-level-subscribed))))
6359      gnus-description-hashtb)
6360     (goto-char (point-min))
6361     (gnus-group-position-point)))
6362
6363 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6364 (defun gnus-group-apropos (regexp &optional search-description)
6365   "List all newsgroups that have names that match a regexp."
6366   (interactive "sGnus apropos (regexp): ")
6367   (let ((prev "")
6368         (obuf (current-buffer))
6369         groups des)
6370     ;; Go through all newsgroups that are known to Gnus.
6371     (mapatoms
6372      (lambda (group)
6373        (and (symbol-name group)
6374             (string-match regexp (symbol-name group))
6375             (setq groups (cons (symbol-name group) groups))))
6376      gnus-active-hashtb)
6377     ;; Go through all descriptions that are known to Gnus.
6378     (if search-description
6379         (mapatoms
6380          (lambda (group)
6381            (and (string-match regexp (symbol-value group))
6382                 (gnus-active (symbol-name group))
6383                 (setq groups (cons (symbol-name group) groups))))
6384          gnus-description-hashtb))
6385     (if (not groups)
6386         (gnus-message 3 "No groups matched \"%s\"." regexp)
6387       ;; Print out all the groups.
6388       (save-excursion
6389         (pop-to-buffer "*Gnus Help*")
6390         (buffer-disable-undo (current-buffer))
6391         (erase-buffer)
6392         (setq groups (sort groups 'string<))
6393         (while groups
6394           ;; Groups may be entered twice into the list of groups.
6395           (if (not (string= (car groups) prev))
6396               (progn
6397                 (insert (setq prev (car groups)) "\n")
6398                 (if (and gnus-description-hashtb
6399                          (setq des (gnus-gethash (car groups)
6400                                                  gnus-description-hashtb)))
6401                     (insert "  " des "\n"))))
6402           (setq groups (cdr groups)))
6403         (goto-char (point-min))))
6404     (pop-to-buffer obuf)))
6405
6406 (defun gnus-group-description-apropos (regexp)
6407   "List all newsgroups that have names or descriptions that match a regexp."
6408   (interactive "sGnus description apropos (regexp): ")
6409   (if (not (or gnus-description-hashtb
6410                (gnus-read-all-descriptions-files)))
6411       (error "Couldn't request descriptions file"))
6412   (gnus-group-apropos regexp t))
6413
6414 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6415 (defun gnus-group-list-matching (level regexp &optional all lowest)
6416   "List all groups with unread articles that match REGEXP.
6417 If the prefix LEVEL is non-nil, it should be a number that says which
6418 level to cut off listing groups.
6419 If ALL, also list groups with no unread articles.
6420 If LOWEST, don't list groups with level lower than LOWEST."
6421   (interactive "P\nsList newsgroups matching: ")
6422   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6423                            all (or lowest 1) regexp)
6424   (goto-char (point-min))
6425   (gnus-group-position-point))
6426
6427 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6428   "List all groups that match REGEXP.
6429 If the prefix LEVEL is non-nil, it should be a number that says which
6430 level to cut off listing groups.
6431 If LOWEST, don't list groups with level lower than LOWEST."
6432   (interactive "P\nsList newsgroups matching: ")
6433   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6434
6435 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6436 (defun gnus-group-save-newsrc (&optional force)
6437   "Save the Gnus startup files.
6438 If FORCE, force saving whether it is necessary or not."
6439   (interactive "P")
6440   (gnus-save-newsrc-file force))
6441
6442 (defun gnus-group-restart (&optional arg)
6443   "Force Gnus to read the .newsrc file."
6444   (interactive "P")
6445   (when (gnus-yes-or-no-p
6446          (format "Are you sure you want to read %s? "
6447                  gnus-current-startup-file))
6448     (gnus-save-newsrc-file)
6449     (gnus-setup-news 'force)
6450     (gnus-group-list-groups arg)))
6451
6452 (defun gnus-group-read-init-file ()
6453   "Read the Gnus elisp init file."
6454   (interactive)
6455   (gnus-read-init-file))
6456
6457 (defun gnus-group-check-bogus-groups (&optional silent)
6458   "Check bogus newsgroups.
6459 If given a prefix, don't ask for confirmation before removing a bogus
6460 group."
6461   (interactive "P")
6462   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6463   (gnus-group-list-groups))
6464
6465 (defun gnus-group-edit-global-kill (&optional article group)
6466   "Edit the global kill file.
6467 If GROUP, edit that local kill file instead."
6468   (interactive "P")
6469   (setq gnus-current-kill-article article)
6470   (gnus-kill-file-edit-file group)
6471   (gnus-message
6472    6
6473    (substitute-command-keys
6474     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6475             (if group "local" "global")))))
6476
6477 (defun gnus-group-edit-local-kill (article group)
6478   "Edit a local kill file."
6479   (interactive (list nil (gnus-group-group-name)))
6480   (gnus-group-edit-global-kill article group))
6481
6482 (defun gnus-group-force-update ()
6483   "Update `.newsrc' file."
6484   (interactive)
6485   (gnus-save-newsrc-file))
6486
6487 (defun gnus-group-suspend ()
6488   "Suspend the current Gnus session.
6489 In fact, cleanup buffers except for group mode buffer.
6490 The hook gnus-suspend-gnus-hook is called before actually suspending."
6491   (interactive)
6492   (run-hooks 'gnus-suspend-gnus-hook)
6493   ;; Kill Gnus buffers except for group mode buffer.
6494   (let ((group-buf (get-buffer gnus-group-buffer)))
6495     ;; Do this on a separate list in case the user does a ^G before we finish
6496     (let ((gnus-buffer-list
6497            (delq group-buf (delq gnus-dribble-buffer
6498                                  (append gnus-buffer-list nil)))))
6499       (while gnus-buffer-list
6500         (gnus-kill-buffer (car gnus-buffer-list))
6501         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6502     (if group-buf
6503         (progn
6504           (setq gnus-buffer-list (list group-buf))
6505           (bury-buffer group-buf)
6506           (delete-windows-on group-buf t)))))
6507
6508 (defun gnus-group-clear-dribble ()
6509   "Clear all information from the dribble buffer."
6510   (interactive)
6511   (gnus-dribble-clear)
6512   (gnus-message 7 "Cleared dribble buffer"))
6513
6514 (defun gnus-group-exit ()
6515   "Quit reading news after updating .newsrc.eld and .newsrc.
6516 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6517   (interactive)
6518   (when 
6519       (or noninteractive                ;For gnus-batch-kill
6520           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6521           (not gnus-interactive-exit)   ;Without confirmation
6522           gnus-expert-user
6523           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6524     (run-hooks 'gnus-exit-gnus-hook)
6525     ;; Offer to save data from non-quitted summary buffers.
6526     (gnus-offer-save-summaries)
6527     ;; Save the newsrc file(s).
6528     (gnus-save-newsrc-file)
6529     ;; Kill-em-all.
6530     (gnus-close-backends)
6531     ;; Reset everything.
6532     (gnus-clear-system)
6533     ;; Allow the user to do things after cleaning up.
6534     (run-hooks 'gnus-after-exiting-gnus-hook)))
6535
6536 (defun gnus-close-backends ()
6537   ;; Send a close request to all backends that support such a request.
6538   (let ((methods gnus-valid-select-methods)
6539         func)
6540     (while methods
6541       (if (fboundp (setq func (intern (concat (caar methods)
6542                                               "-request-close"))))
6543           (funcall func))
6544       (setq methods (cdr methods)))))
6545
6546 (defun gnus-group-quit ()
6547   "Quit reading news without updating .newsrc.eld or .newsrc.
6548 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6549   (interactive)
6550   (when (or noninteractive              ;For gnus-batch-kill
6551             (zerop (buffer-size))
6552             (not (gnus-server-opened gnus-select-method))
6553             gnus-expert-user
6554             (not gnus-current-startup-file)
6555             (gnus-yes-or-no-p
6556              (format "Quit reading news without saving %s? "
6557                      (file-name-nondirectory gnus-current-startup-file))))
6558     (run-hooks 'gnus-exit-gnus-hook)
6559     (if gnus-use-full-window
6560         (delete-other-windows)
6561       (gnus-remove-some-windows))
6562     (gnus-dribble-save)
6563     (gnus-close-backends)
6564     (gnus-clear-system)
6565     ;; Allow the user to do things after cleaning up.
6566     (run-hooks 'gnus-after-exiting-gnus-hook)))
6567
6568 (defun gnus-offer-save-summaries ()
6569   "Offer to save all active summary buffers."
6570   (save-excursion
6571     (let ((buflist (buffer-list))
6572           buffers bufname)
6573       ;; Go through all buffers and find all summaries.
6574       (while buflist
6575         (and (setq bufname (buffer-name (car buflist)))
6576              (string-match "Summary" bufname)
6577              (save-excursion
6578                (set-buffer bufname)
6579                ;; We check that this is, indeed, a summary buffer.
6580                (and (eq major-mode 'gnus-summary-mode)
6581                     ;; Also make sure this isn't bogus.
6582                     gnus-newsgroup-prepared))
6583              (push bufname buffers))
6584         (setq buflist (cdr buflist)))
6585       ;; Go through all these summary buffers and offer to save them.
6586       (when buffers
6587         (map-y-or-n-p
6588          "Update summary buffer %s? "
6589          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6590          buffers)))))
6591
6592 (defun gnus-group-describe-briefly ()
6593   "Give a one line description of the group mode commands."
6594   (interactive)
6595   (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")))
6596
6597 (defun gnus-group-browse-foreign-server (method)
6598   "Browse a foreign news server.
6599 If called interactively, this function will ask for a select method
6600  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6601 If not, METHOD should be a list where the first element is the method
6602 and the second element is the address."
6603   (interactive
6604    (list (let ((how (completing-read
6605                      "Which backend: "
6606                      (append gnus-valid-select-methods gnus-server-alist)
6607                      nil t (cons "nntp" 0))))
6608            ;; We either got a backend name or a virtual server name.
6609            ;; If the first, we also need an address.
6610            (if (assoc how gnus-valid-select-methods)
6611                (list (intern how)
6612                      ;; Suggested by mapjph@bath.ac.uk.
6613                      (completing-read
6614                       "Address: "
6615                       (mapcar (lambda (server) (list server))
6616                               gnus-secondary-servers)))
6617              ;; We got a server name, so we find the method.
6618              (gnus-server-to-method how)))))
6619   (gnus-browse-foreign-server method))
6620
6621 \f
6622 ;;;
6623 ;;; Gnus summary mode
6624 ;;;
6625
6626 (defvar gnus-summary-mode-map nil)
6627
6628 (put 'gnus-summary-mode 'mode-class 'special)
6629
6630 (unless gnus-summary-mode-map
6631   (setq gnus-summary-mode-map (make-keymap))
6632   (suppress-keymap gnus-summary-mode-map)
6633
6634   ;; Non-orthogonal keys
6635
6636   (gnus-define-keys gnus-summary-mode-map
6637     " " gnus-summary-next-page
6638     "\177" gnus-summary-prev-page
6639     [delete] gnus-summary-prev-page
6640     "\r" gnus-summary-scroll-up
6641     "n" gnus-summary-next-unread-article
6642     "p" gnus-summary-prev-unread-article
6643     "N" gnus-summary-next-article
6644     "P" gnus-summary-prev-article
6645     "\M-\C-n" gnus-summary-next-same-subject
6646     "\M-\C-p" gnus-summary-prev-same-subject
6647     "\M-n" gnus-summary-next-unread-subject
6648     "\M-p" gnus-summary-prev-unread-subject
6649     "." gnus-summary-first-unread-article
6650     "," gnus-summary-best-unread-article
6651     "\M-s" gnus-summary-search-article-forward
6652     "\M-r" gnus-summary-search-article-backward
6653     "<" gnus-summary-beginning-of-article
6654     ">" gnus-summary-end-of-article
6655     "j" gnus-summary-goto-article
6656     "^" gnus-summary-refer-parent-article
6657     "\M-^" gnus-summary-refer-article
6658     "u" gnus-summary-tick-article-forward
6659     "!" gnus-summary-tick-article-forward
6660     "U" gnus-summary-tick-article-backward
6661     "d" gnus-summary-mark-as-read-forward
6662     "D" gnus-summary-mark-as-read-backward
6663     "E" gnus-summary-mark-as-expirable
6664     "\M-u" gnus-summary-clear-mark-forward
6665     "\M-U" gnus-summary-clear-mark-backward
6666     "k" gnus-summary-kill-same-subject-and-select
6667     "\C-k" gnus-summary-kill-same-subject
6668     "\M-\C-k" gnus-summary-kill-thread
6669     "\M-\C-l" gnus-summary-lower-thread
6670     "e" gnus-summary-edit-article
6671     "#" gnus-summary-mark-as-processable
6672     "\M-#" gnus-summary-unmark-as-processable
6673     "\M-\C-t" gnus-summary-toggle-threads
6674     "\M-\C-s" gnus-summary-show-thread
6675     "\M-\C-h" gnus-summary-hide-thread
6676     "\M-\C-f" gnus-summary-next-thread
6677     "\M-\C-b" gnus-summary-prev-thread
6678     "\M-\C-u" gnus-summary-up-thread
6679     "\M-\C-d" gnus-summary-down-thread
6680     "&" gnus-summary-execute-command
6681     "c" gnus-summary-catchup-and-exit
6682     "\C-w" gnus-summary-mark-region-as-read
6683     "\C-t" gnus-summary-toggle-truncation
6684     "?" gnus-summary-mark-as-dormant
6685     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6686     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6687     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6688     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6689     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6690     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6691     "=" gnus-summary-expand-window
6692     "\C-x\C-s" gnus-summary-reselect-current-group
6693     "\M-g" gnus-summary-rescan-group
6694     "w" gnus-summary-stop-page-breaking
6695     "\C-c\C-r" gnus-summary-caesar-message
6696     "\M-t" gnus-summary-toggle-mime
6697     "f" gnus-summary-followup
6698     "F" gnus-summary-followup-with-original
6699     "C" gnus-summary-cancel-article
6700     "r" gnus-summary-reply
6701     "R" gnus-summary-reply-with-original
6702     "\C-c\C-f" gnus-summary-mail-forward
6703     "o" gnus-summary-save-article
6704     "\C-o" gnus-summary-save-article-mail
6705     "|" gnus-summary-pipe-output
6706     "\M-k" gnus-summary-edit-local-kill
6707     "\M-K" gnus-summary-edit-global-kill
6708     "V" gnus-version
6709     "\C-c\C-d" gnus-summary-describe-group
6710     "q" gnus-summary-exit
6711     "Q" gnus-summary-exit-no-update
6712     "\C-c\C-i" gnus-info-find-node
6713     gnus-mouse-2 gnus-mouse-pick-article
6714     "m" gnus-summary-mail-other-window
6715     "a" gnus-summary-post-news
6716     "x" gnus-summary-limit-to-unread
6717     "s" gnus-summary-isearch-article
6718     "t" gnus-article-hide-headers
6719     "g" gnus-summary-show-article
6720     "l" gnus-summary-goto-last-article
6721     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6722     "\C-d" gnus-summary-enter-digest-group
6723     "\C-c\C-b" gnus-bug
6724     "*" gnus-cache-enter-article
6725     "\M-*" gnus-cache-remove-article
6726     "\M-&" gnus-summary-universal-argument
6727     "\C-l" gnus-recenter
6728     "I" gnus-summary-increase-score
6729     "L" gnus-summary-lower-score
6730
6731     "V" gnus-summary-score-map
6732     "X" gnus-uu-extract-map
6733     "S" gnus-summary-send-map)
6734
6735   ;; Sort of orthogonal keymap
6736   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6737     "t" gnus-summary-tick-article-forward
6738     "!" gnus-summary-tick-article-forward
6739     "d" gnus-summary-mark-as-read-forward
6740     "r" gnus-summary-mark-as-read-forward
6741     "c" gnus-summary-clear-mark-forward
6742     " " gnus-summary-clear-mark-forward
6743     "e" gnus-summary-mark-as-expirable
6744     "x" gnus-summary-mark-as-expirable
6745     "?" gnus-summary-mark-as-dormant
6746     "b" gnus-summary-set-bookmark
6747     "B" gnus-summary-remove-bookmark
6748     "#" gnus-summary-mark-as-processable
6749     "\M-#" gnus-summary-unmark-as-processable
6750     "S" gnus-summary-limit-include-expunged
6751     "C" gnus-summary-catchup
6752     "H" gnus-summary-catchup-to-here
6753     "\C-c" gnus-summary-catchup-all
6754     "k" gnus-summary-kill-same-subject-and-select
6755     "K" gnus-summary-kill-same-subject
6756     "P" gnus-uu-mark-map)
6757
6758   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6759     "c" gnus-summary-clear-above
6760     "u" gnus-summary-tick-above
6761     "m" gnus-summary-mark-above
6762     "k" gnus-summary-kill-below)
6763
6764   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6765     "/" gnus-summary-limit-to-subject
6766     "n" gnus-summary-limit-to-articles
6767     "w" gnus-summary-pop-limit
6768     "s" gnus-summary-limit-to-subject
6769     "a" gnus-summary-limit-to-author
6770     "u" gnus-summary-limit-to-unread
6771     "m" gnus-summary-limit-to-marks
6772     "v" gnus-summary-limit-to-score
6773     "D" gnus-summary-limit-include-dormant
6774     "d" gnus-summary-limit-exclude-dormant
6775     ;;  "t" gnus-summary-limit-exclude-thread
6776     "E" gnus-summary-limit-include-expunged
6777     "c" gnus-summary-limit-exclude-childless-dormant
6778     "C" gnus-summary-limit-mark-excluded-as-read)
6779
6780   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6781     "n" gnus-summary-next-unread-article
6782     "p" gnus-summary-prev-unread-article
6783     "N" gnus-summary-next-article
6784     "P" gnus-summary-prev-article
6785     "\C-n" gnus-summary-next-same-subject
6786     "\C-p" gnus-summary-prev-same-subject
6787     "\M-n" gnus-summary-next-unread-subject
6788     "\M-p" gnus-summary-prev-unread-subject
6789     "f" gnus-summary-first-unread-article
6790     "b" gnus-summary-best-unread-article
6791     "j" gnus-summary-goto-article
6792     "g" gnus-summary-goto-subject
6793     "l" gnus-summary-goto-last-article
6794     "p" gnus-summary-pop-article)
6795
6796   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6797     "k" gnus-summary-kill-thread
6798     "l" gnus-summary-lower-thread
6799     "i" gnus-summary-raise-thread
6800     "T" gnus-summary-toggle-threads
6801     "t" gnus-summary-rethread-current
6802     "^" gnus-summary-reparent-thread
6803     "s" gnus-summary-show-thread
6804     "S" gnus-summary-show-all-threads
6805     "h" gnus-summary-hide-thread
6806     "H" gnus-summary-hide-all-threads
6807     "n" gnus-summary-next-thread
6808     "p" gnus-summary-prev-thread
6809     "u" gnus-summary-up-thread
6810     "o" gnus-summary-top-thread
6811     "d" gnus-summary-down-thread
6812     "#" gnus-uu-mark-thread
6813     "\M-#" gnus-uu-unmark-thread)
6814
6815   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6816     "c" gnus-summary-catchup-and-exit
6817     "C" gnus-summary-catchup-all-and-exit
6818     "E" gnus-summary-exit-no-update
6819     "Q" gnus-summary-exit
6820     "Z" gnus-summary-exit
6821     "n" gnus-summary-catchup-and-goto-next-group
6822     "R" gnus-summary-reselect-current-group
6823     "G" gnus-summary-rescan-group
6824     "N" gnus-summary-next-group
6825     "P" gnus-summary-prev-group)
6826
6827   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6828     " " gnus-summary-next-page
6829     "n" gnus-summary-next-page
6830     "\177" gnus-summary-prev-page
6831     [delete] gnus-summary-prev-page
6832     "p" gnus-summary-prev-page
6833     "\r" gnus-summary-scroll-up
6834     "<" gnus-summary-beginning-of-article
6835     ">" gnus-summary-end-of-article
6836     "b" gnus-summary-beginning-of-article
6837     "e" gnus-summary-end-of-article
6838     "^" gnus-summary-refer-parent-article
6839     "r" gnus-summary-refer-parent-article
6840     "R" gnus-summary-refer-references
6841     "g" gnus-summary-show-article
6842     "s" gnus-summary-isearch-article)
6843
6844   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6845     "b" gnus-article-add-buttons
6846     "B" gnus-article-add-buttons-to-head
6847     "o" gnus-article-treat-overstrike
6848     ;;  "w" gnus-article-word-wrap
6849     "w" gnus-article-fill-cited-article
6850     "c" gnus-article-remove-cr
6851     "L" gnus-article-remove-trailing-blank-lines
6852     "q" gnus-article-de-quoted-unreadable
6853     "f" gnus-article-display-x-face
6854     "l" gnus-summary-stop-page-breaking
6855     "r" gnus-summary-caesar-message
6856     "t" gnus-article-hide-headers
6857     "v" gnus-summary-verbose-headers
6858     "m" gnus-summary-toggle-mime)
6859
6860   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6861     "a" gnus-article-hide
6862     "h" gnus-article-hide-headers
6863     "b" gnus-article-hide-boring-headers
6864     "s" gnus-article-hide-signature
6865     "c" gnus-article-hide-citation
6866     "p" gnus-article-hide-pgp
6867     "\C-c" gnus-article-hide-citation-maybe)
6868
6869   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6870     "a" gnus-article-highlight
6871     "h" gnus-article-highlight-headers
6872     "c" gnus-article-highlight-citation
6873     "s" gnus-article-highlight-signature)
6874
6875   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6876     "z" gnus-article-date-ut
6877     "u" gnus-article-date-ut
6878     "l" gnus-article-date-local
6879     "e" gnus-article-date-lapsed
6880     "o" gnus-article-date-original)
6881
6882   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6883     "v" gnus-version
6884     "f" gnus-summary-fetch-faq
6885     "d" gnus-summary-describe-group
6886     "h" gnus-summary-describe-briefly
6887     "i" gnus-info-find-node)
6888
6889   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6890     "e" gnus-summary-expire-articles
6891     "\M-\C-e" gnus-summary-expire-articles-now
6892     "\177" gnus-summary-delete-article
6893     [delete] gnus-summary-delete-article
6894     "m" gnus-summary-move-article
6895     "r" gnus-summary-respool-article
6896     "w" gnus-summary-edit-article
6897     "c" gnus-summary-copy-article
6898     "B" gnus-summary-crosspost-article
6899     "q" gnus-summary-respool-query
6900     "i" gnus-summary-import-article)
6901
6902   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6903     "o" gnus-summary-save-article
6904     "m" gnus-summary-save-article-mail
6905     "r" gnus-summary-save-article-rmail
6906     "f" gnus-summary-save-article-file
6907     "b" gnus-summary-save-article-body-file
6908     "h" gnus-summary-save-article-folder
6909     "v" gnus-summary-save-article-vm
6910     "p" gnus-summary-pipe-output
6911     "s" gnus-soup-add-article)
6912   )
6913
6914 \f
6915
6916 (defun gnus-summary-mode (&optional group)
6917   "Major mode for reading articles.
6918
6919 All normal editing commands are switched off.
6920 \\<gnus-summary-mode-map>
6921 Each line in this buffer represents one article.  To read an
6922 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6923 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6924 respectively.
6925
6926 You can also post articles and send mail from this buffer.  To
6927 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6928 of an article, type `\\[gnus-summary-reply]'.
6929
6930 There are approx. one gazillion commands you can execute in this
6931 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6932
6933 The following commands are available:
6934
6935 \\{gnus-summary-mode-map}"
6936   (interactive)
6937   (when (and menu-bar-mode
6938              (gnus-visual-p 'summary-menu 'menu))
6939     (gnus-summary-make-menu-bar))
6940   (kill-all-local-variables)
6941   (let ((locals gnus-summary-local-variables))
6942     (while locals
6943       (if (consp (car locals))
6944           (progn
6945             (make-local-variable (caar locals))
6946             (set (caar locals) (eval (cdar locals))))
6947         (make-local-variable (car locals))
6948         (set (car locals) nil))
6949       (setq locals (cdr locals))))
6950   (gnus-make-thread-indent-array)
6951   (gnus-simplify-mode-line)
6952   (setq major-mode 'gnus-summary-mode)
6953   (setq mode-name "Summary")
6954   (make-local-variable 'minor-mode-alist)
6955   (use-local-map gnus-summary-mode-map)
6956   (buffer-disable-undo (current-buffer))
6957   (setq buffer-read-only t)             ;Disable modification
6958   (setq truncate-lines t)
6959   (setq selective-display t)
6960   (setq selective-display-ellipses t)   ;Display `...'
6961   (setq buffer-display-table gnus-summary-display-table)
6962   (setq gnus-newsgroup-name group)
6963   (run-hooks 'gnus-summary-mode-hook))
6964
6965 (defun gnus-summary-make-display-table ()
6966   ;; Change the display table.  Odd characters have a tendency to mess
6967   ;; up nicely formatted displays - we make all possible glyphs
6968   ;; display only a single character.
6969
6970   ;; We start from the standard display table, if any.
6971   (setq gnus-summary-display-table
6972         (or (copy-sequence standard-display-table)
6973             (make-display-table)))
6974   ;; Nix out all the control chars...
6975   (let ((i 32))
6976     (while (>= (setq i (1- i)) 0)
6977       (aset gnus-summary-display-table i [??])))
6978   ;; ... but not newline and cr, of course. (cr is necessary for the
6979   ;; selective display).
6980   (aset gnus-summary-display-table ?\n nil)
6981   (aset gnus-summary-display-table ?\r nil)
6982   ;; We nix out any glyphs over 126 that are not set already.
6983   (let ((i 256))
6984     (while (>= (setq i (1- i)) 127)
6985       ;; Only modify if the entry is nil.
6986       (or (aref gnus-summary-display-table i)
6987           (aset gnus-summary-display-table i [??])))))
6988
6989 (defun gnus-summary-clear-local-variables ()
6990   (let ((locals gnus-summary-local-variables))
6991     (while locals
6992       (if (consp (car locals))
6993           (and (vectorp (caar locals))
6994                (set (caar locals) nil))
6995         (and (vectorp (car locals))
6996              (set (car locals) nil)))
6997       (setq locals (cdr locals)))))
6998
6999 ;; Summary data functions.
7000
7001 (defmacro gnus-data-number (data)
7002   `(car ,data))
7003
7004 (defmacro gnus-data-set-number (data number)
7005   `(setcar ,data ,number))
7006
7007 (defmacro gnus-data-mark (data)
7008   `(nth 1 ,data))
7009
7010 (defmacro gnus-data-set-mark (data mark)
7011   `(setcar (nthcdr 1 ,data) ,mark))
7012
7013 (defmacro gnus-data-pos (data)
7014   `(nth 2 ,data))
7015
7016 (defmacro gnus-data-set-pos (data pos)
7017   `(setcar (nthcdr 2 ,data) ,pos))
7018
7019 (defmacro gnus-data-header (data)
7020   `(nth 3 ,data))
7021
7022 (defmacro gnus-data-level (data)
7023   `(nth 4 ,data))
7024
7025 (defmacro gnus-data-unread-p (data)
7026   `(= (nth 1 ,data) gnus-unread-mark))
7027
7028 (defmacro gnus-data-pseudo-p (data)
7029   `(consp (nth 3 ,data)))
7030
7031 (defmacro gnus-data-find (number)
7032   `(assq ,number gnus-newsgroup-data))
7033
7034 (defmacro gnus-data-find-list (number &optional data)
7035   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7036      (memq (assq ,number bdata)
7037            bdata)))
7038
7039 (defmacro gnus-data-make (number mark pos header level)
7040   `(list ,number ,mark ,pos ,header ,level))
7041
7042 (defun gnus-data-enter (after-article number mark pos header level offset)
7043   (let ((data (gnus-data-find-list after-article)))
7044     (or data (error "No such article: %d" after-article))
7045     (setcdr data (cons (gnus-data-make number mark pos header level)
7046                        (cdr data)))
7047     (setq gnus-newsgroup-data-reverse nil)
7048     (gnus-data-update-list (cddr data) offset)))
7049
7050 (defun gnus-data-enter-list (after-article list &optional offset)
7051   (when list
7052     (let ((data (and after-article (gnus-data-find-list after-article)))
7053           (ilist list))
7054       (or data (not after-article) (error "No such article: %d" after-article))
7055       ;; Find the last element in the list to be spliced into the main
7056       ;; list.
7057       (while (cdr list)
7058         (setq list (cdr list)))
7059       (if (not data)
7060           (progn
7061             (setcdr list gnus-newsgroup-data)
7062             (setq gnus-newsgroup-data ilist)
7063             (and offset (gnus-data-update-list (cdr list) offset)))
7064         (setcdr list (cdr data))
7065         (setcdr data ilist)
7066         (and offset (gnus-data-update-list (cdr data) offset)))
7067       (setq gnus-newsgroup-data-reverse nil))))
7068
7069 (defun gnus-data-remove (article &optional offset)
7070   (let ((data gnus-newsgroup-data))
7071     (if (= (gnus-data-number (car data)) article)
7072         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7073               gnus-newsgroup-data-reverse nil)
7074       (while (cdr data)
7075         (and (= (gnus-data-number (cadr data)) article)
7076              (progn
7077                (setcdr data (cddr data))
7078                (and offset (gnus-data-update-list (cdr data) offset))
7079                (setq data nil
7080                      gnus-newsgroup-data-reverse nil)))
7081         (setq data (cdr data))))))
7082
7083 (defmacro gnus-data-list (backward)
7084   `(if ,backward
7085        (or gnus-newsgroup-data-reverse
7086            (setq gnus-newsgroup-data-reverse
7087                  (reverse gnus-newsgroup-data)))
7088      gnus-newsgroup-data))
7089
7090 (defun gnus-data-update-list (data offset)
7091   "Add OFFSET to the POS of all data entries in DATA."
7092   (while data
7093     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7094     (setq data (cdr data))))
7095
7096 (defun gnus-data-compute-positions ()
7097   "Compute the positions of all articles."
7098   (let ((data gnus-newsgroup-data)
7099         pos)
7100     (while data
7101       (when (setq pos (text-property-any
7102                        (point-min) (point-max)
7103                        'gnus-number (gnus-data-number (car data))))
7104         (gnus-data-set-pos (car data) (+ pos 3)))
7105       (setq data (cdr data)))))
7106
7107 (defun gnus-summary-article-pseudo-p (article)
7108   "Say whether this article is a pseudo article or not."
7109   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7110
7111 (defun gnus-article-parent-p (number)
7112   "Say whether this article is a parent or not."
7113   (let ((data (gnus-data-find-list number)))
7114     (and (cdr data)                     ; There has to be an article after...
7115          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7116             (gnus-data-level (nth 1 data))))))
7117
7118 (defun gnus-article-children (number)
7119   "Return a list of all children to NUMBER."
7120   (let* ((data (gnus-data-find-list number))
7121          (level (gnus-data-level (car data)))
7122          children)
7123     (setq data (cdr data))
7124     (while (and data            
7125                 (= (gnus-data-level (car data)) (1+ level)))
7126       (push (gnus-data-number (car data)) children)
7127       (setq data (cdr data)))
7128     children))
7129
7130 (defmacro gnus-summary-skip-intangible ()
7131   "If the current article is intangible, then jump to a different article."
7132   '(let ((to (get-text-property (point) 'gnus-intangible)))
7133     (and to (gnus-summary-goto-subject to))))
7134
7135 (defmacro gnus-summary-article-intangible-p ()
7136   "Say whether this article is intangible or not."
7137   '(get-text-property (point) 'gnus-intangible))
7138
7139 ;; Some summary mode macros.
7140
7141 (defmacro gnus-summary-article-number ()
7142   "The article number of the article on the current line.
7143 If there isn's an article number here, then we return the current
7144 article number."
7145   '(progn
7146      (gnus-summary-skip-intangible)
7147      (or (get-text-property (point) 'gnus-number)
7148          (gnus-summary-last-subject))))
7149
7150 (defmacro gnus-summary-article-header (&optional number)
7151   `(gnus-data-header (gnus-data-find
7152                       ,(or number '(gnus-summary-article-number)))))
7153
7154 (defmacro gnus-summary-thread-level (&optional number)
7155   `(if (and (eq gnus-summary-make-false-root 'dummy)
7156             (get-text-property (point) 'gnus-intangible))
7157        0
7158      (gnus-data-level (gnus-data-find
7159                        ,(or number '(gnus-summary-article-number))))))
7160
7161 (defmacro gnus-summary-article-mark (&optional number)
7162   `(gnus-data-mark (gnus-data-find
7163                     ,(or number '(gnus-summary-article-number)))))
7164
7165 (defmacro gnus-summary-article-pos (&optional number)
7166   `(gnus-data-pos (gnus-data-find
7167                    ,(or number '(gnus-summary-article-number)))))
7168
7169 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7170 (defmacro gnus-summary-article-subject (&optional number)
7171   "Return current subject string or nil if nothing."
7172   `(let ((headers
7173           ,(if number
7174                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7175              '(gnus-data-header (assq (gnus-summary-article-number)
7176                                       gnus-newsgroup-data)))))
7177      (and headers
7178           (vectorp headers)
7179           (mail-header-subject headers))))
7180
7181 (defmacro gnus-summary-article-score (&optional number)
7182   "Return current article score."
7183   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7184                   gnus-newsgroup-scored))
7185        gnus-summary-default-score 0))
7186
7187 (defun gnus-summary-article-children (&optional number)
7188   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7189          (level (gnus-data-level (car data)))
7190          l children)
7191     (while (and (setq data (cdr data))
7192                 (> (setq l (gnus-data-level (car data))) level))
7193       (and (= (1+ level) l)
7194            (setq children (cons (gnus-data-number (car data))
7195                                 children))))
7196     (nreverse children)))
7197
7198 (defun gnus-summary-article-parent (&optional number)
7199   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7200                                     (gnus-data-list t)))
7201          (level (gnus-data-level (car data))))
7202     (if (zerop level)
7203         () ; This is a root.
7204       ;; We search until we find an article with a level less than
7205       ;; this one.  That function has to be the parent.
7206       (while (and (setq data (cdr data))
7207                   (not (< (gnus-data-level (car data)) level))))
7208       (and data (gnus-data-number (car data))))))
7209
7210 (defun gnus-unread-mark-p (mark)
7211   "Say whether MARK is the unread mark."
7212   (= mark gnus-unread-mark))
7213
7214 (defun gnus-read-mark-p (mark)
7215   "Say whether MARK is one of the marks that mark as read.
7216 This is all marks except unread, ticked, dormant, and expirable."
7217   (not (or (= mark gnus-unread-mark)
7218            (= mark gnus-ticked-mark)
7219            (= mark gnus-dormant-mark)
7220            (= mark gnus-expirable-mark))))
7221
7222 ;; Various summary mode internalish functions.
7223
7224 (defun gnus-mouse-pick-article (e)
7225   (interactive "e")
7226   (mouse-set-point e)
7227   (gnus-summary-next-page nil t))
7228
7229 (defun gnus-summary-setup-buffer (group)
7230   "Initialize summary buffer."
7231   (let ((buffer (concat "*Summary " group "*")))
7232     (if (get-buffer buffer)
7233         (progn
7234           (set-buffer buffer)
7235           (setq gnus-summary-buffer (current-buffer))
7236           (not gnus-newsgroup-prepared))
7237       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7238       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7239       (gnus-add-current-to-buffer-list)
7240       (gnus-summary-mode group)
7241       (when gnus-carpal
7242         (gnus-carpal-setup-buffer 'summary))
7243       (unless gnus-single-article-buffer
7244         (make-local-variable 'gnus-article-buffer)
7245         (make-local-variable 'gnus-article-current)
7246         (make-local-variable 'gnus-original-article-buffer))
7247       (setq gnus-newsgroup-name group)
7248       t)))
7249
7250 (defun gnus-set-global-variables ()
7251   ;; Set the global equivalents of the summary buffer-local variables
7252   ;; to the latest values they had.  These reflect the summary buffer
7253   ;; that was in action when the last article was fetched.
7254   (when (eq major-mode 'gnus-summary-mode)
7255     (setq gnus-summary-buffer (current-buffer))
7256     (let ((name gnus-newsgroup-name)
7257           (marked gnus-newsgroup-marked)
7258           (unread gnus-newsgroup-unreads)
7259           (headers gnus-current-headers)
7260           (data gnus-newsgroup-data)
7261           (summary gnus-summary-buffer)
7262           (article-buffer gnus-article-buffer)
7263           (original gnus-original-article-buffer)
7264           (gac gnus-article-current)
7265           (score-file gnus-current-score-file))
7266       (save-excursion
7267         (set-buffer gnus-group-buffer)
7268         (setq gnus-newsgroup-name name)
7269         (setq gnus-newsgroup-marked marked)
7270         (setq gnus-newsgroup-unreads unread)
7271         (setq gnus-current-headers headers)
7272         (setq gnus-newsgroup-data data)
7273         (setq gnus-article-current gac)
7274         (setq gnus-summary-buffer summary)
7275         (setq gnus-article-buffer article-buffer)
7276         (setq gnus-original-article-buffer original)
7277         (setq gnus-current-score-file score-file)))))
7278
7279 (defun gnus-summary-last-article-p (&optional article)
7280   "Return whether ARTICLE is the last article in the buffer."
7281   (if (not (setq article (or article (gnus-summary-article-number))))
7282       t ; All non-existant numbers are the last article. :-)
7283     (cdr (gnus-data-find-list article))))
7284
7285 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7286   "Insert a dummy root in the summary buffer."
7287   (beginning-of-line)
7288   (add-text-properties
7289    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7290    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7291
7292 (defvar gnus-thread-indent-array nil)
7293 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7294 (defun gnus-make-thread-indent-array ()
7295   (let ((n 200))
7296     (if (and gnus-thread-indent-array
7297              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7298         nil
7299       (setq gnus-thread-indent-array (make-vector 201 "")
7300             gnus-thread-indent-array-level gnus-thread-indent-level)
7301       (while (>= n 0)
7302         (aset gnus-thread-indent-array n
7303               (make-string (* n gnus-thread-indent-level) ? ))
7304         (setq n (1- n))))))
7305
7306 (defun gnus-summary-insert-line
7307   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7308                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7309                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7310   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7311          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7312          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7313          (gnus-tmp-score-char
7314           (if (or (null gnus-summary-default-score)
7315                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7316                       gnus-summary-zcore-fuzz)) ? 
7317             (if (< gnus-tmp-score gnus-summary-default-score)
7318                 gnus-score-below-mark gnus-score-over-mark)))
7319          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7320                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7321                                   gnus-cached-mark)
7322                                  (gnus-tmp-replied gnus-replied-mark)
7323                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7324                                   gnus-saved-mark)
7325                                  (t gnus-unread-mark)))
7326          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7327          (gnus-tmp-name
7328           (cond
7329            ((string-match "(.+)" gnus-tmp-from)
7330             (substring gnus-tmp-from
7331                        (1+ (match-beginning 0)) (1- (match-end 0))))
7332            ((string-match "<[^>]+> *$" gnus-tmp-from)
7333             (let ((beg (match-beginning 0)))
7334               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7335                        (substring gnus-tmp-from (1+ (match-beginning 0))
7336                                   (1- (match-end 0))))
7337                   (substring gnus-tmp-from 0 beg))))
7338            (t gnus-tmp-from)))
7339          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7340          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7341          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7342          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7343          (buffer-read-only nil))
7344     (when (string= gnus-tmp-name "")
7345       (setq gnus-tmp-name gnus-tmp-from))
7346     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7347     (put-text-property
7348      (point)
7349      (progn (eval gnus-summary-line-format-spec) (point))
7350      'gnus-number gnus-tmp-number)
7351     (when (gnus-visual-p 'summary-highlight 'highlight)
7352       (forward-line -1)
7353       (run-hooks 'gnus-summary-update-hook)
7354       (forward-line 1))))
7355
7356 (defun gnus-summary-update-line (&optional dont-update)
7357   ;; Update summary line after change.
7358   (when (and gnus-summary-default-score
7359              (not gnus-summary-inhibit-highlight))
7360     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7361            (article (gnus-summary-article-number))
7362            (score (gnus-summary-article-score article)))
7363       (unless dont-update
7364         (if (and gnus-summary-mark-below
7365                  (< (gnus-summary-article-score)
7366                     gnus-summary-mark-below))
7367             ;; This article has a low score, so we mark it as read.
7368             (when (memq article gnus-newsgroup-unreads)
7369               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7370           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7371             ;; This article was previously marked as read on account
7372             ;; of a low score, but now it has risen, so we mark it as
7373             ;; unread.
7374             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7375         (gnus-summary-update-mark
7376          (if (or (null gnus-summary-default-score)
7377                  (<= (abs (- score gnus-summary-default-score))
7378                      gnus-summary-zcore-fuzz)) ? 
7379            (if (< score gnus-summary-default-score)
7380                gnus-score-below-mark gnus-score-over-mark)) 'score))
7381       ;; Do visual highlighting.
7382       (when (gnus-visual-p 'summary-highlight 'highlight)
7383         (run-hooks 'gnus-summary-update-hook)))))
7384
7385 (defvar gnus-tmp-new-adopts nil)
7386
7387 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7388   ;; Sum up all elements (and sub-elements) in a list.
7389   (let* ((number
7390           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7391           (cond
7392            ((and (consp thread) (cdr thread))
7393             (apply
7394              '+ 1 (mapcar
7395                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7396            ((null thread)
7397             1)
7398            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7399             1)
7400            (t 1))))
7401     (when (and level (zerop level) gnus-tmp-new-adopts)
7402       (incf number
7403             (apply '+ (mapcar
7404                        'gnus-summary-number-of-articles-in-thread
7405                        gnus-tmp-new-adopts))))
7406     (if char
7407         (if (> number 1) gnus-not-empty-thread-mark
7408           gnus-empty-thread-mark)
7409       number)))
7410
7411 (defun gnus-summary-set-local-parameters (group)
7412  "Go through the local params of GROUP and set all variable specs in that list."
7413   (let ((params (gnus-info-params (gnus-get-info group)))
7414         elem)
7415     (while params
7416       (setq elem (car params)
7417             params (cdr params))
7418       (and (consp elem)                 ; Has to be a cons.
7419            (consp (cdr elem))           ; The cdr has to be a list.
7420            (symbolp (car elem))         ; Has to be a symbol in there.
7421            (not (memq (car elem) 
7422                       '(quit-config to-address to-list to-group)))
7423            (progn                       ; So we set it.
7424              (make-local-variable (car elem))
7425              (set (car elem) (eval (nth 1 elem))))))))
7426
7427 (defun gnus-summary-read-group (group &optional show-all no-article
7428                                       kill-buffer no-display)
7429   "Start reading news in newsgroup GROUP.
7430 If SHOW-ALL is non-nil, already read articles are also listed.
7431 If NO-ARTICLE is non-nil, no article is selected initially.
7432 If NO-DISPLAY, don't generate a summary buffer."
7433   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7434   (let* ((new-group (gnus-summary-setup-buffer group))
7435          (quit-config (gnus-group-quit-config group))
7436          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7437     (cond
7438      ;; This summary buffer exists already, so we just select it.
7439      ((not new-group)
7440       (gnus-set-global-variables)
7441       (when kill-buffer
7442         (gnus-kill-or-deaden-summary kill-buffer))
7443       (gnus-configure-windows 'summary 'force)
7444       (gnus-set-mode-line 'summary)
7445       (gnus-summary-position-point)
7446       (message "")
7447       t)
7448      ;; We couldn't select this group.
7449      ((null did-select)
7450       (when (and (eq major-mode 'gnus-summary-mode)
7451                  (not (equal (current-buffer) kill-buffer)))
7452         (kill-buffer (current-buffer))
7453         (if (not quit-config)
7454             (progn
7455               (set-buffer gnus-group-buffer)
7456               (gnus-group-jump-to-group group)
7457               (gnus-group-next-unread-group 1))
7458           (if (not (buffer-name (car quit-config)))
7459               (gnus-configure-windows 'group 'force)
7460             (set-buffer (car quit-config))
7461             (and (eq major-mode 'gnus-summary-mode)
7462                  (gnus-set-global-variables))
7463             (gnus-configure-windows (cdr quit-config)))))
7464       (gnus-message 3 "Can't select group")
7465       nil)
7466      ;; The user did a `C-g' while prompting for number of articles,
7467      ;; so we exit this group.
7468      ((eq did-select 'quit)
7469       (and (eq major-mode 'gnus-summary-mode)
7470            (not (equal (current-buffer) kill-buffer))
7471            (kill-buffer (current-buffer)))
7472       (when kill-buffer
7473         (gnus-kill-or-deaden-summary kill-buffer))
7474       (if (not quit-config)
7475           (progn
7476             (set-buffer gnus-group-buffer)
7477             (gnus-group-jump-to-group group)
7478             (gnus-group-next-unread-group 1)
7479             (gnus-configure-windows 'group 'force))
7480         (if (not (buffer-name (car quit-config)))
7481             (gnus-configure-windows 'group 'force)
7482           (set-buffer (car quit-config))
7483           (and (eq major-mode 'gnus-summary-mode)
7484                (gnus-set-global-variables))
7485           (gnus-configure-windows (cdr quit-config))))
7486       ;; Finally signal the quit.
7487       (signal 'quit nil))
7488      ;; The group was successfully selected.
7489      (t
7490       (gnus-set-global-variables)
7491       ;; Save the active value in effect when the group was entered.
7492       (setq gnus-newsgroup-active
7493             (gnus-copy-sequence
7494              (gnus-active gnus-newsgroup-name)))
7495       ;; You can change the summary buffer in some way with this hook.
7496       (run-hooks 'gnus-select-group-hook)
7497       ;; Set any local variables in the group parameters.
7498       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7499       (gnus-update-format-specifications)
7500       ;; Do score processing.
7501       (when gnus-use-scoring
7502         (gnus-possibly-score-headers))
7503       ;; Check whether to fill in the gaps in the threads.
7504       (when gnus-build-sparse-threads
7505         (gnus-build-sparse-threads))
7506       ;; Find the initial limit.
7507       (if show-all
7508           (let ((gnus-newsgroup-dormant nil))
7509             (gnus-summary-initial-limit show-all))
7510         (gnus-summary-initial-limit show-all))
7511       ;; Generate the summary buffer.
7512       (unless no-display
7513         (gnus-summary-prepare))
7514       (when gnus-use-trees
7515         (gnus-tree-open group)
7516         (setq gnus-summary-highlight-line-function
7517               'gnus-tree-highlight-article))
7518       ;; If the summary buffer is empty, but there are some low-scored
7519       ;; articles or some excluded dormants, we include these in the
7520       ;; buffer.
7521       (when (and (zerop (buffer-size))
7522                  (not no-display))
7523         (cond (gnus-newsgroup-dormant
7524                (gnus-summary-limit-include-dormant))
7525               ((and gnus-newsgroup-scored show-all)
7526                (gnus-summary-limit-include-expunged))))
7527       ;; Function `gnus-apply-kill-file' must be called in this hook.
7528       (run-hooks 'gnus-apply-kill-hook)
7529       (if (and (zerop (buffer-size))
7530                (not no-display))
7531           (progn
7532             ;; This newsgroup is empty.
7533             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7534             (gnus-message 6 "No unread news")
7535             (when kill-buffer
7536               (gnus-kill-or-deaden-summary kill-buffer))
7537             ;; Return nil from this function.
7538             nil)
7539         ;; Hide conversation thread subtrees.  We cannot do this in
7540         ;; gnus-summary-prepare-hook since kill processing may not
7541         ;; work with hidden articles.
7542         (and gnus-show-threads
7543              gnus-thread-hide-subtree
7544              (gnus-summary-hide-all-threads))
7545         ;; Show first unread article if requested.
7546         (if (and (not no-article)
7547                  (not no-display)
7548                  gnus-newsgroup-unreads
7549                  gnus-auto-select-first)
7550             (if (eq gnus-auto-select-first 'best)
7551                 (gnus-summary-best-unread-article)
7552               (gnus-summary-first-unread-article))
7553           ;; Don't select any articles, just move point to the first
7554           ;; article in the group.
7555           (goto-char (point-min))
7556           (gnus-summary-position-point)
7557           (gnus-set-mode-line 'summary)
7558           (gnus-configure-windows 'summary 'force))
7559         ;; If we are in async mode, we send some info to the backend.
7560         (when gnus-newsgroup-async
7561           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7562         (when kill-buffer
7563           (gnus-kill-or-deaden-summary kill-buffer))
7564         (when (get-buffer-window gnus-group-buffer t)
7565           ;; Gotta use windows, because recenter does wierd stuff if
7566           ;; the current buffer ain't the displayed window.
7567           (let ((owin (selected-window)))
7568             (select-window (get-buffer-window gnus-group-buffer t))
7569             (when (gnus-group-goto-group group)
7570               (recenter))
7571             (select-window owin))))
7572       ;; Mark this buffer as "prepared".
7573       (setq gnus-newsgroup-prepared t)
7574       t))))
7575
7576 (defun gnus-summary-prepare ()
7577   "Generate the summary buffer."
7578   (let ((buffer-read-only nil))
7579     (erase-buffer)
7580     (setq gnus-newsgroup-data nil
7581           gnus-newsgroup-data-reverse nil)
7582     (run-hooks 'gnus-summary-generate-hook)
7583     ;; Generate the buffer, either with threads or without.
7584     (when gnus-newsgroup-headers
7585       (gnus-summary-prepare-threads
7586        (if gnus-show-threads
7587            (gnus-sort-gathered-threads
7588             (funcall gnus-summary-thread-gathering-function
7589                      (gnus-sort-threads
7590                       (gnus-cut-threads (gnus-make-threads)))))
7591          ;; Unthreaded display.
7592          (gnus-sort-articles gnus-newsgroup-headers))))
7593     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7594     ;; Call hooks for modifying summary buffer.
7595     (goto-char (point-min))
7596     (run-hooks 'gnus-summary-prepare-hook)))
7597
7598 (defun gnus-gather-threads-by-subject (threads)
7599   "Gather threads by looking at Subject headers."
7600   (if (not gnus-summary-make-false-root)
7601       threads
7602     (let ((hashtb (gnus-make-hashtable 1023))
7603           (prev threads)
7604           (result threads)
7605           subject hthread whole-subject)
7606       (while threads
7607         (setq whole-subject (mail-header-subject (caar threads)))
7608         (if (and gnus-summary-gather-exclude-subject
7609                  (string-match gnus-summary-gather-exclude-subject
7610                                whole-subject))
7611             () ; We don't want to do anything with this article.
7612           ;; We simplify the subject before looking it up in the
7613           ;; hash table.
7614           (setq subject
7615                 (cond
7616                  ;; Truncate the subject.
7617                  ((numberp gnus-summary-gather-subject-limit)
7618                   (setq subject (gnus-simplify-subject-re whole-subject))
7619                   (if (> (length subject) gnus-summary-gather-subject-limit)
7620                       (substring subject 0 gnus-summary-gather-subject-limit)
7621                     subject))
7622                  ;; Fuzzily simplify it.
7623                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7624                   (gnus-simplify-subject-fuzzy whole-subject))
7625                  ;; Just remove the leading "Re:".
7626                  (t
7627                   (gnus-simplify-subject-re whole-subject))))
7628
7629           (if (setq hthread (gnus-gethash subject hashtb))
7630               (progn
7631                 ;; We enter a dummy root into the thread, if we
7632                 ;; haven't done that already.
7633                 (unless (stringp (caar hthread))
7634                   (setcar hthread (list whole-subject (car hthread))))
7635                 ;; We add this new gathered thread to this gathered
7636                 ;; thread.
7637                 (setcdr (car hthread)
7638                         (nconc (cdar hthread) (list (car threads))))
7639                 ;; Remove it from the list of threads.
7640                 (setcdr prev (cdr threads))
7641                 (setq threads prev))
7642             ;; Enter this thread into the hash table.
7643             (gnus-sethash subject threads hashtb)))
7644         (setq prev threads)
7645         (setq threads (cdr threads)))
7646       result)))
7647
7648 (defun gnus-gather-threads-by-references (threads)
7649   "Gather threads by looking at References headers."
7650   (let ((idhashtb (gnus-make-hashtable 1023))
7651         (thhashtb (gnus-make-hashtable 1023))
7652         (prev threads)
7653         (result threads)
7654         ids references id gthread gid entered)
7655     (while threads
7656       (when (setq references (mail-header-references (caar threads)))
7657         (setq id (mail-header-id (caar threads)))
7658         (setq ids (gnus-split-references references))
7659         (setq entered nil)
7660         (while ids
7661           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7662               (progn
7663                 (gnus-sethash (car ids) id idhashtb)
7664                 (gnus-sethash id threads thhashtb))
7665             (setq gthread (gnus-gethash gid thhashtb))
7666             (unless entered
7667               ;; We enter a dummy root into the thread, if we
7668               ;; haven't done that already.
7669               (unless (stringp (caar gthread))
7670                 (setcar gthread (list (mail-header-subject (caar gthread))
7671                                       (car gthread))))
7672               ;; We add this new gathered thread to this gathered
7673               ;; thread.
7674               (setcdr (car gthread)
7675                       (nconc (cdar gthread) (list (car threads)))))
7676             ;; Add it into the thread hash table.
7677             (gnus-sethash id gthread thhashtb)
7678             (setq entered t)
7679             ;; Remove it from the list of threads.
7680             (setcdr prev (cdr threads))
7681             (setq threads prev))
7682           (setq ids (cdr ids))))
7683       (setq prev threads)
7684       (setq threads (cdr threads)))
7685     result))
7686
7687 (defun gnus-sort-gathered-threads (threads)
7688   "Sort subtreads inside each gathered thread by article number."
7689   (let ((result threads))
7690     (while threads
7691       (when (stringp (caar threads))
7692         (setcdr (car threads)
7693                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7694       (setq threads (cdr threads)))
7695     result))
7696
7697 (defun gnus-make-threads ()
7698   "Go through the dependency hashtb and find the roots.  Return all threads."
7699   (let (threads)
7700     (mapatoms
7701      (lambda (refs)
7702        (unless (car (symbol-value refs))
7703          ;; These threads do not refer back to any other articles,
7704          ;; so they're roots.
7705          (setq threads (append (cdr (symbol-value refs)) threads))))
7706      gnus-newsgroup-dependencies)
7707     threads))
7708
7709 (defun gnus-build-sparse-threads ()
7710   (let ((headers gnus-newsgroup-headers)
7711         (deps gnus-newsgroup-dependencies)
7712         header references generation relations 
7713         cthread subject child end pthread relation)
7714     ;; First we create an alist of generations/relations, where 
7715     ;; generations is how much we trust the ralation, and the relation
7716     ;; is parent/child.
7717     (gnus-message 7 "Making sparse threads...")
7718     (save-excursion
7719       (nnheader-set-temp-buffer " *gnus sparse threads*")
7720       (while (setq header (pop headers))
7721         (when (and (setq references (mail-header-references header))
7722                    (not (string= references "")))
7723           (insert references)
7724           (setq child (downcase (mail-header-id header))
7725                 subject (mail-header-subject header))
7726           (setq generation 0)
7727           (while (search-backward ">" nil t)
7728             (setq end (1+ (point)))
7729             (when (search-backward "<" nil t)
7730               (push (list (incf generation) 
7731                           child (setq child (downcase
7732                                              (buffer-substring (point) end)))
7733                           subject)
7734                     relations)))
7735           (push (list (1+ generation) child nil subject) relations)
7736           (erase-buffer)))
7737       (kill-buffer (current-buffer)))
7738     ;; Sort over trustworthiness.
7739     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7740     (while (setq relation (pop relations))
7741       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7742                 (unless (car (symbol-value cthread))
7743                   ;; Make this article the parent of these threads.
7744                   (setcar (symbol-value cthread)
7745                           (vector gnus-reffed-article-number 
7746                                   (cadddr relation) 
7747                                   "" ""
7748                                   (cadr relation) 
7749                                   (or (caddr relation) "") 0 0 "")))
7750               (set cthread (list (vector gnus-reffed-article-number
7751                                          (cadddr relation) 
7752                                          "" "" (cadr relation) 
7753                                          (or (caddr relation) "") 0 0 ""))))
7754         (push gnus-reffed-article-number gnus-newsgroup-limit)
7755         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7756         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7757               gnus-newsgroup-reads)
7758         (decf gnus-reffed-article-number)
7759         ;; Make this new thread the child of its parent.
7760         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7761             (setcdr (symbol-value pthread)
7762                     (nconc (cdr (symbol-value pthread))
7763                            (list (symbol-value cthread))))
7764           (set pthread (list nil (symbol-value cthread))))))
7765     (gnus-message 7 "Making sparse threads...done")))
7766
7767 (defun gnus-build-old-threads ()
7768   ;; Look at all the articles that refer back to old articles, and
7769   ;; fetch the headers for the articles that aren't there.  This will
7770   ;; build complete threads - if the roots haven't been expired by the
7771   ;; server, that is.
7772   (let (id heads)
7773     (mapatoms
7774      (lambda (refs)
7775        (when (not (car (symbol-value refs)))
7776          (setq heads (cdr (symbol-value refs)))
7777          (while heads
7778            (if (memq (mail-header-number (caar heads))
7779                      gnus-newsgroup-dormant)
7780                (setq heads (cdr heads))
7781              (setq id (symbol-name refs))
7782              (while (and (setq id (gnus-build-get-header id))
7783                          (not (car (gnus-gethash
7784                                     id gnus-newsgroup-dependencies)))))
7785              (setq heads nil)))))
7786      gnus-newsgroup-dependencies)))
7787
7788 (defun gnus-build-get-header (id)
7789   ;; Look through the buffer of NOV lines and find the header to
7790   ;; ID.  Enter this line into the dependencies hash table, and return
7791   ;; the id of the parent article (if any).
7792   (let ((deps gnus-newsgroup-dependencies)
7793         found header)
7794     (prog1
7795         (save-excursion
7796           (set-buffer nntp-server-buffer)
7797           (goto-char (point-min))
7798           (while (and (not found) (search-forward id nil t))
7799             (beginning-of-line)
7800             (setq found (looking-at
7801                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7802                                  (regexp-quote id))))
7803             (or found (beginning-of-line 2)))
7804           (when found
7805             (beginning-of-line)
7806             (and
7807              (setq header (gnus-nov-parse-line
7808                            (read (current-buffer)) deps))
7809              (gnus-parent-id (mail-header-references header)))))
7810       (when header
7811         (let ((number (mail-header-number header)))
7812           (push number gnus-newsgroup-limit)
7813           (push header gnus-newsgroup-headers)
7814           (if (memq number gnus-newsgroup-unselected)
7815               (progn
7816                 (push number gnus-newsgroup-unreads)
7817                 (setq gnus-newsgroup-unselected
7818                       (delq number gnus-newsgroup-unselected)))
7819             (push number gnus-newsgroup-ancient)))))))
7820
7821 (defun gnus-summary-update-article (article &optional header)
7822   "Update ARTICLE in the summary buffer."
7823   (let ((id (mail-header-id (gnus-summary-article-header article)))
7824         (data (gnus-data-find article)))
7825     (setcar (gnus-id-to-thread id) nil)
7826     (gnus-summary-insert-subject id)
7827     ;; Set the (possibly) new article number in the data structure.
7828     (gnus-data-set-number data (gnus-id-to-article id))))
7829
7830 (defun gnus-rebuild-thread (id)
7831   "Rebuild the thread containing ID."
7832   (let ((buffer-read-only nil)
7833         current thread data)
7834     (if (not gnus-show-threads)
7835         (setq thread (list (car (gnus-id-to-thread id))))
7836       ;; Get the thread this article is part of.
7837       (setq thread (gnus-remove-thread id)))
7838     (setq current (save-excursion
7839                     (and (zerop (forward-line -1))
7840                          (gnus-summary-article-number))))
7841     ;; If this is a gathered thread, we have to go some re-gathering.
7842     (when (stringp (car thread))
7843       (let ((subject (car thread))
7844             roots thr)
7845         (setq thread (cdr thread))
7846         (while thread
7847           (unless (memq (setq thr (gnus-id-to-thread
7848                                       (gnus-root-id
7849                                        (mail-header-id (caar thread)))))
7850                         roots)
7851             (push thr roots))
7852           (setq thread (cdr thread)))
7853         ;; We now have all (unique) roots.
7854         (if (= (length roots) 1)
7855             ;; All the loose roots are now one solid root.
7856             (setq thread (car roots))
7857           (setq thread (cons subject (gnus-sort-threads roots))))))
7858     (let (threads)
7859       ;; We then insert this thread into the summary buffer.
7860       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7861         (gnus-summary-prepare-threads (list thread))
7862         (setq data (nreverse gnus-newsgroup-data))
7863         (setq threads gnus-newsgroup-threads))
7864       ;; We splice the new data into the data structure.
7865       (gnus-data-enter-list current data)
7866       (gnus-data-compute-positions)
7867       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7868
7869 (defun gnus-id-to-thread (id)
7870   "Return the (sub-)thread where ID appears."
7871   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7872
7873 (defun gnus-id-to-article (id)
7874   "Return the article number of ID."
7875   (let ((thread (gnus-id-to-thread id)))
7876     (when thread
7877       (mail-header-number (car thread)))))
7878
7879 (defun gnus-id-to-header (id)
7880   "Return the article headers of ID."
7881   (car (gnus-id-to-thread id)))
7882
7883 (defun gnus-article-displayed-root-p (article)
7884   "Say whether ARTICLE is a root(ish) article."
7885   (let ((level (gnus-summary-thread-level article))
7886         particle)
7887     (cond 
7888      ((null level) nil)
7889      ((zerop level) t)
7890      ((and (= 1 level)
7891            (null (setq particle (gnus-id-to-article
7892                                  (gnus-parent-id 
7893                                   (mail-header-references 
7894                                    (gnus-summary-article-header article))))))
7895            (null (gnus-summary-thread-level particle)))))))
7896
7897 (defun gnus-root-id (id)
7898   "Return the id of the root of the thread where ID appears."
7899   (let (last-id prev)
7900     (while (and id (setq prev (car (gnus-gethash
7901                                     (downcase id)
7902                                     gnus-newsgroup-dependencies))))
7903       (setq last-id id
7904             id (gnus-parent-id (mail-header-references prev))))
7905     last-id))
7906
7907 (defun gnus-remove-thread (id &optional dont-remove)
7908   "Remove the thread that has ID in it."
7909   (let ((dep gnus-newsgroup-dependencies)
7910         headers thread last-id)
7911     ;; First go up in this thread until we find the root.
7912     (setq last-id (gnus-root-id id))
7913     (setq headers (list (car (gnus-id-to-thread last-id))
7914                         (caadr (gnus-id-to-thread last-id))))
7915     ;; We have now found the real root of this thread.  It might have
7916     ;; been gathered into some loose thread, so we have to search
7917     ;; through the threads to find the thread we wanted.
7918     (let ((threads gnus-newsgroup-threads)
7919           sub)
7920       (while threads
7921         (setq sub (car threads))
7922         (if (stringp (car sub))
7923             ;; This is a gathered threads, so we look at the roots
7924             ;; below it to find whether this article in in this
7925             ;; gathered root.
7926             (progn
7927               (setq sub (cdr sub))
7928               (while sub
7929                 (when (member (caar sub) headers)
7930                   (setq thread (car threads)
7931                         threads nil
7932                         sub nil))
7933                 (setq sub (cdr sub))))
7934           ;; It's an ordinary thread, so we check it.
7935           (when (eq (car sub) (car headers))
7936             (setq thread sub
7937                   threads nil)))
7938         (setq threads (cdr threads)))
7939       ;; If this article is in no thread, then it's a root.
7940       (if thread
7941           (unless dont-remove
7942             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7943         (setq thread (gnus-gethash (downcase last-id) dep)))
7944       (when thread
7945         (prog1
7946             thread ; We return this thread.
7947           (unless dont-remove
7948             (if (stringp (car thread))
7949                 (progn
7950                   ;; If we use dummy roots, then we have to remove the
7951                   ;; dummy root as well.
7952                   (when (eq gnus-summary-make-false-root 'dummy)
7953                     ;; Uhm.
7954                     )
7955                   (setq thread (cdr thread))
7956                   (while thread
7957                     (gnus-remove-thread-1 (car thread))
7958                     (setq thread (cdr thread))))
7959               (gnus-remove-thread-1 thread))))))))
7960
7961 (defun gnus-remove-thread-1 (thread)
7962   "Remove the thread THREAD recursively."
7963   (let ((number (mail-header-number (car thread)))
7964         pos)
7965     (when (setq pos (text-property-any
7966                      (point-min) (point-max) 'gnus-number number))
7967       (goto-char pos)
7968       (gnus-delete-line)
7969       (gnus-data-remove number))
7970     (setq thread (cdr thread))
7971     (while thread
7972       (gnus-remove-thread-1 (car thread))
7973       (setq thread (cdr thread)))))
7974
7975 (defun gnus-sort-threads (threads)
7976   "Sort THREADS."
7977   (if (not gnus-thread-sort-functions)
7978       threads
7979     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7980                     (car gnus-thread-sort-functions)
7981                   `(lambda (t1 t2)
7982                      ,(gnus-make-sort-function 
7983                        (reverse gnus-thread-sort-functions))))))
7984       (gnus-message 7 "Sorting threads...")
7985       (prog1
7986           (sort threads func)
7987         (gnus-message 7 "Sorting threads...done")))))
7988
7989 (defun gnus-sort-articles (articles)
7990   "Sort ARTICLES."
7991   (when gnus-article-sort-functions
7992     (let ((func (if (= 1 (length gnus-article-sort-functions))
7993                     (car gnus-article-sort-functions)
7994                   `(lambda (t1 t2)
7995                      ,(gnus-make-sort-function 
7996                        (reverse gnus-article-sort-functions))))))
7997       (gnus-message 7 "Sorting articles...")
7998       (prog1
7999           (setq gnus-newsgroup-headers (sort articles func))
8000         (gnus-message 7 "Sorting articles...done")))))
8001
8002 (defun gnus-make-sort-function (funs)
8003   "Return a composite sort condition based on the functions in FUNC."
8004   (if (cdr funs)
8005       `(or (,(car funs) t1 t2)
8006            (and (not (,(car funs) t2 t1))
8007                 ,(gnus-make-sort-function (cdr funs))))
8008     `(,(car funs) t1 t2)))
8009                  
8010 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8011 (defmacro gnus-thread-header (thread)
8012   ;; Return header of first article in THREAD.
8013   ;; Note that THREAD must never, ever be anything else than a variable -
8014   ;; using some other form will lead to serious barfage.
8015   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8016   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8017   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8018         (vector thread) 2))
8019
8020 (defsubst gnus-article-sort-by-number (h1 h2)
8021   "Sort articles by article number."
8022   (< (mail-header-number h1)
8023      (mail-header-number h2)))
8024
8025 (defun gnus-thread-sort-by-number (h1 h2)
8026   "Sort threads by root article number."
8027   (gnus-article-sort-by-number
8028    (gnus-thread-header h1) (gnus-thread-header h2)))
8029
8030 (defsubst gnus-article-sort-by-author (h1 h2)
8031   "Sort articles by root author."
8032   (string-lessp
8033    (let ((extract (funcall
8034                    gnus-extract-address-components
8035                    (mail-header-from h1))))
8036      (or (car extract) (cdr extract)))
8037    (let ((extract (funcall
8038                    gnus-extract-address-components
8039                    (mail-header-from h2))))
8040      (or (car extract) (cdr extract)))))
8041
8042 (defun gnus-thread-sort-by-author (h1 h2)
8043   "Sort threads by root author."
8044   (gnus-article-sort-by-author
8045    (gnus-thread-header h1)  (gnus-thread-header h2)))
8046
8047 (defsubst gnus-article-sort-by-subject (h1 h2)
8048   "Sort articles by root subject."
8049   (string-lessp
8050    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8051    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8052
8053 (defun gnus-thread-sort-by-subject (h1 h2)
8054   "Sort threads by root subject."
8055   (gnus-article-sort-by-subject
8056    (gnus-thread-header h1) (gnus-thread-header h2)))
8057
8058 (defsubst gnus-article-sort-by-date (h1 h2)
8059   "Sort articles by root article date."
8060   (string-lessp
8061    (gnus-sortable-date (mail-header-date h1))
8062    (gnus-sortable-date (mail-header-date h2))))
8063
8064 (defun gnus-thread-sort-by-date (h1 h2)
8065   "Sort threads by root article date."
8066   (gnus-article-sort-by-date
8067    (gnus-thread-header h1) (gnus-thread-header h2)))
8068
8069 (defsubst gnus-article-sort-by-score (h1 h2)
8070   "Sort articles by root article score.
8071 Unscored articles will be counted as having a score of zero."
8072   (> (or (cdr (assq (mail-header-number h1)
8073                     gnus-newsgroup-scored))
8074          gnus-summary-default-score 0)
8075      (or (cdr (assq (mail-header-number h2)
8076                     gnus-newsgroup-scored))
8077          gnus-summary-default-score 0)))
8078
8079 (defun gnus-thread-sort-by-score (h1 h2)
8080   "Sort threads by root article score."
8081   (gnus-article-sort-by-score
8082    (gnus-thread-header h1) (gnus-thread-header h2)))
8083
8084 (defun gnus-thread-sort-by-total-score (h1 h2)
8085   "Sort threads by the sum of all scores in the thread.
8086 Unscored articles will be counted as having a score of zero."
8087   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8088
8089 (defun gnus-thread-total-score (thread)
8090   ;;  This function find the total score of THREAD.
8091   (if (consp thread)
8092       (if (stringp (car thread))
8093           (apply gnus-thread-score-function 0
8094                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8095         (gnus-thread-total-score-1 thread))
8096     (gnus-thread-total-score-1 (list thread))))
8097
8098 (defun gnus-thread-total-score-1 (root)
8099   ;; This function find the total score of the thread below ROOT.
8100   (setq root (car root))
8101   (apply gnus-thread-score-function
8102          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8103              gnus-summary-default-score 0)
8104          (mapcar 'gnus-thread-total-score
8105                  (cdr (gnus-gethash (downcase (mail-header-id root))
8106                                     gnus-newsgroup-dependencies)))))
8107
8108 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8109 (defvar gnus-tmp-prev-subject nil)
8110 (defvar gnus-tmp-false-parent nil)
8111 (defvar gnus-tmp-root-expunged nil)
8112 (defvar gnus-tmp-dummy-line nil)
8113
8114 (defun gnus-summary-prepare-threads (threads)
8115   "Prepare summary buffer from THREADS and indentation LEVEL.
8116 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8117 or a straight list of headers."
8118   (gnus-message 7 "Generating summary...")
8119
8120   (setq gnus-newsgroup-threads threads)
8121   (beginning-of-line)
8122
8123   (let ((gnus-tmp-level 0)
8124         (default-score (or gnus-summary-default-score 0))
8125         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8126         thread number subject stack state gnus-tmp-gathered beg-match
8127         new-roots gnus-tmp-new-adopts thread-end
8128         gnus-tmp-header gnus-tmp-unread
8129         gnus-tmp-replied gnus-tmp-subject-or-nil
8130         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8131         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8132         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8133
8134     (setq gnus-tmp-prev-subject nil)
8135
8136     (if (vectorp (car threads))
8137         ;; If this is a straight (sic) list of headers, then a
8138         ;; threaded summary display isn't required, so we just create
8139         ;; an unthreaded one.
8140         (gnus-summary-prepare-unthreaded threads)
8141
8142       ;; Do the threaded display.
8143
8144       (while (or threads stack gnus-tmp-new-adopts new-roots)
8145
8146         (if (and (= gnus-tmp-level 0)
8147                  (not (setq gnus-tmp-dummy-line nil))
8148                  (or (not stack)
8149                      (= (caar stack) 0))
8150                  (not gnus-tmp-false-parent)
8151                  (or gnus-tmp-new-adopts new-roots))
8152             (if gnus-tmp-new-adopts
8153                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8154                       thread (list (car gnus-tmp-new-adopts))
8155                       gnus-tmp-header (caar thread)
8156                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8157               (if new-roots
8158                   (setq thread (list (car new-roots))
8159                         gnus-tmp-header (caar thread)
8160                         new-roots (cdr new-roots))))
8161
8162           (if threads
8163               ;; If there are some threads, we do them before the
8164               ;; threads on the stack.
8165               (setq thread threads
8166                     gnus-tmp-header (caar thread))
8167             ;; There were no current threads, so we pop something off
8168             ;; the stack.
8169             (setq state (car stack)
8170                   gnus-tmp-level (car state)
8171                   thread (cdr state)
8172                   stack (cdr stack)
8173                   gnus-tmp-header (caar thread))))
8174
8175         (setq gnus-tmp-false-parent nil)
8176         (setq gnus-tmp-root-expunged nil)
8177         (setq thread-end nil)
8178
8179         (if (stringp gnus-tmp-header)
8180             ;; The header is a dummy root.
8181             (cond
8182              ((eq gnus-summary-make-false-root 'adopt)
8183               ;; We let the first article adopt the rest.
8184               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8185                                                (cddar thread)))
8186               (setq gnus-tmp-gathered
8187                     (nconc (mapcar
8188                             (lambda (h) (mail-header-number (car h)))
8189                             (cddar thread))
8190                            gnus-tmp-gathered))
8191               (setq thread (cons (list (caar thread)
8192                                        (cadar thread))
8193                                  (cdr thread)))
8194               (setq gnus-tmp-level -1
8195                     gnus-tmp-false-parent t))
8196              ((eq gnus-summary-make-false-root 'empty)
8197               ;; We print adopted articles with empty subject fields.
8198               (setq gnus-tmp-gathered
8199                     (nconc (mapcar
8200                             (lambda (h) (mail-header-number (car h)))
8201                             (cddar thread))
8202                            gnus-tmp-gathered))
8203               (setq gnus-tmp-level -1))
8204              ((eq gnus-summary-make-false-root 'dummy)
8205               ;; We remember that we probably want to output a dummy
8206               ;; root.
8207               (setq gnus-tmp-dummy-line gnus-tmp-header)
8208               (setq gnus-tmp-prev-subject gnus-tmp-header))
8209              (t
8210               ;; We do not make a root for the gathered
8211               ;; sub-threads at all.
8212               (setq gnus-tmp-level -1)))
8213
8214           (setq number (mail-header-number gnus-tmp-header)
8215                 subject (mail-header-subject gnus-tmp-header))
8216
8217           (cond
8218            ;; If the thread has changed subject, we might want to make
8219            ;; this subthread into a root.
8220            ((and (null gnus-thread-ignore-subject)
8221                  (not (zerop gnus-tmp-level))
8222                  gnus-tmp-prev-subject
8223                  (not (inline
8224                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8225             (setq new-roots (nconc new-roots (list (car thread)))
8226                   thread-end t
8227                   gnus-tmp-header nil))
8228            ;; If the article lies outside the current limit,
8229            ;; then we do not display it.
8230            ((and (not (memq number gnus-newsgroup-limit))
8231                  (not gnus-tmp-dummy-line))
8232             (setq gnus-tmp-gathered
8233                   (nconc (mapcar
8234                           (lambda (h) (mail-header-number (car h)))
8235                           (cdar thread))
8236                          gnus-tmp-gathered))
8237             (setq gnus-tmp-new-adopts (if (cdar thread)
8238                                           (append gnus-tmp-new-adopts
8239                                                   (cdar thread))
8240                                         gnus-tmp-new-adopts)
8241                   thread-end t
8242                   gnus-tmp-header nil)
8243             (when (zerop gnus-tmp-level)
8244               (setq gnus-tmp-root-expunged t)))
8245            ;; Perhaps this article is to be marked as read?
8246            ((and gnus-summary-mark-below
8247                  (< (or (cdr (assq number gnus-newsgroup-scored))
8248                         default-score)
8249                     gnus-summary-mark-below)
8250                  ;; Don't touch sparse articles.
8251                  (not (memq number gnus-newsgroup-sparse)))
8252             (setq gnus-newsgroup-unreads
8253                   (delq number gnus-newsgroup-unreads))
8254             (if gnus-newsgroup-auto-expire
8255                 (push number gnus-newsgroup-expirable)
8256               (push (cons number gnus-low-score-mark)
8257                     gnus-newsgroup-reads))))
8258
8259           (when gnus-tmp-header
8260             ;; We may have an old dummy line to output before this
8261             ;; article.
8262             (when gnus-tmp-dummy-line
8263               (gnus-summary-insert-dummy-line
8264                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8265               (setq gnus-tmp-dummy-line nil))
8266
8267             ;; Compute the mark.
8268             (setq
8269              gnus-tmp-unread
8270              (cond
8271               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8272               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8273               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8274               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8275               (t (or (cdr (assq number gnus-newsgroup-reads))
8276                      gnus-ancient-mark))))
8277
8278             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8279                                   gnus-tmp-header gnus-tmp-level)
8280                   gnus-newsgroup-data)
8281
8282             ;; Actually insert the line.
8283             (setq
8284              gnus-tmp-subject-or-nil
8285              (cond
8286               ((and gnus-thread-ignore-subject
8287                     gnus-tmp-prev-subject
8288                     (not (inline (gnus-subject-equal
8289                                   gnus-tmp-prev-subject subject))))
8290                subject)
8291               ((zerop gnus-tmp-level)
8292                (if (and (eq gnus-summary-make-false-root 'empty)
8293                         (memq number gnus-tmp-gathered)
8294                         gnus-tmp-prev-subject
8295                         (inline (gnus-subject-equal
8296                                  gnus-tmp-prev-subject subject)))
8297                    gnus-summary-same-subject
8298                  subject))
8299               (t gnus-summary-same-subject)))
8300             (if (and (eq gnus-summary-make-false-root 'adopt)
8301                      (= gnus-tmp-level 1)
8302                      (memq number gnus-tmp-gathered))
8303                 (setq gnus-tmp-opening-bracket ?\<
8304                       gnus-tmp-closing-bracket ?\>)
8305               (setq gnus-tmp-opening-bracket ?\[
8306                     gnus-tmp-closing-bracket ?\]))
8307             (setq
8308              gnus-tmp-indentation
8309              (aref gnus-thread-indent-array gnus-tmp-level)
8310              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8311              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8312                                 gnus-summary-default-score 0)
8313              gnus-tmp-score-char
8314              (if (or (null gnus-summary-default-score)
8315                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8316                          gnus-summary-zcore-fuzz)) ? 
8317                (if (< gnus-tmp-score gnus-summary-default-score)
8318                    gnus-score-below-mark gnus-score-over-mark))
8319              gnus-tmp-replied
8320              (cond ((memq number gnus-newsgroup-processable)
8321                     gnus-process-mark)
8322                    ((memq number gnus-newsgroup-cached)
8323                     gnus-cached-mark)
8324                    ((memq number gnus-newsgroup-replied)
8325                     gnus-replied-mark)
8326                    (t gnus-unread-mark))
8327              gnus-tmp-from (mail-header-from gnus-tmp-header)
8328              gnus-tmp-name
8329              (cond
8330               ((string-match "(.+)" gnus-tmp-from)
8331                (substring gnus-tmp-from
8332                           (1+ (match-beginning 0)) (1- (match-end 0))))
8333               ((string-match "<[^>]+> *$" gnus-tmp-from)
8334                (setq beg-match (match-beginning 0))
8335                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8336                         (substring gnus-tmp-from (1+ (match-beginning 0))
8337                                    (1- (match-end 0))))
8338                    (substring gnus-tmp-from 0 beg-match)))
8339               (t gnus-tmp-from)))
8340             (when (string= gnus-tmp-name "")
8341               (setq gnus-tmp-name gnus-tmp-from))
8342             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8343             (put-text-property
8344              (point)
8345              (progn (eval gnus-summary-line-format-spec) (point))
8346              'gnus-number number)
8347             (when gnus-visual-p
8348               (forward-line -1)
8349               (run-hooks 'gnus-summary-update-hook)
8350               (forward-line 1))
8351
8352             (setq gnus-tmp-prev-subject subject)))
8353
8354         (when (nth 1 thread)
8355           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8356         (incf gnus-tmp-level)
8357         (setq threads (if thread-end nil (cdar thread)))
8358         (unless threads
8359           (setq gnus-tmp-level 0)))))
8360   (gnus-message 7 "Generating summary...done"))
8361
8362 (defun gnus-summary-prepare-unthreaded (headers)
8363   "Generate an unthreaded summary buffer based on HEADERS."
8364   (let (header number mark)
8365
8366     (while headers
8367       (setq header (car headers)
8368             headers (cdr headers)
8369             number (mail-header-number header))
8370
8371       ;; We may have to root out some bad articles...
8372       (when (memq number gnus-newsgroup-limit)
8373         (when (and gnus-summary-mark-below
8374                    (< (or (cdr (assq number gnus-newsgroup-scored))
8375                           gnus-summary-default-score 0)
8376                       gnus-summary-mark-below))
8377           (setq gnus-newsgroup-unreads
8378                 (delq number gnus-newsgroup-unreads))
8379           (if gnus-newsgroup-auto-expire
8380               (push number gnus-newsgroup-expirable)
8381             (push (cons number gnus-low-score-mark)
8382                   gnus-newsgroup-reads)))
8383
8384         (setq mark
8385               (cond
8386                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8387                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8388                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8389                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8390                (t (or (cdr (assq number gnus-newsgroup-reads))
8391                       gnus-ancient-mark))))
8392         (setq gnus-newsgroup-data
8393               (cons (gnus-data-make number mark (1+ (point)) header 0)
8394                     gnus-newsgroup-data))
8395         (gnus-summary-insert-line
8396          header 0 nil mark (memq number gnus-newsgroup-replied)
8397          (memq number gnus-newsgroup-expirable)
8398          (mail-header-subject header) nil
8399          (cdr (assq number gnus-newsgroup-scored))
8400          (memq number gnus-newsgroup-processable))))))
8401
8402 (defun gnus-select-newsgroup (group &optional read-all)
8403   "Select newsgroup GROUP.
8404 If READ-ALL is non-nil, all articles in the group are selected."
8405   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8406          (info (nth 2 entry))
8407          articles fetched-articles cached)
8408
8409     (or (gnus-check-server
8410          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8411         (error "Couldn't open server"))
8412
8413     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8414         (gnus-activate-group group) ; Or we can activate it...
8415         (progn ; Or we bug out.
8416           (when (equal major-mode 'gnus-summary-mode)
8417             (kill-buffer (current-buffer)))
8418           (error "Couldn't request group %s: %s"
8419                  group (gnus-status-message group))))
8420
8421     (setq gnus-newsgroup-name group)
8422     (setq gnus-newsgroup-unselected nil)
8423     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8424
8425     (and gnus-asynchronous
8426          (gnus-check-backend-function
8427           'request-asynchronous gnus-newsgroup-name)
8428          (setq gnus-newsgroup-async
8429                (gnus-request-asynchronous gnus-newsgroup-name)))
8430
8431     ;; Adjust and set lists of article marks.
8432     (when info
8433       (gnus-adjust-marked-articles info))
8434
8435     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8436     (when (gnus-virtual-group-p group)
8437       (setq cached gnus-newsgroup-cached))
8438
8439     (setq gnus-newsgroup-unreads
8440           (gnus-set-difference
8441            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8442            gnus-newsgroup-dormant))
8443
8444     (setq gnus-newsgroup-processable nil)
8445
8446     (setq articles (gnus-articles-to-read group read-all))
8447
8448     (cond
8449      ((null articles)
8450       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8451       'quit)
8452      ((eq articles 0) nil)
8453      (t
8454       ;; Init the dependencies hash table.
8455       (setq gnus-newsgroup-dependencies
8456             (gnus-make-hashtable (length articles)))
8457       ;; Retrieve the headers and read them in.
8458       (gnus-message 5 "Fetching headers...")
8459       (setq gnus-newsgroup-headers
8460             (if (eq 'nov
8461                     (setq gnus-headers-retrieved-by
8462                           (gnus-retrieve-headers
8463                            articles gnus-newsgroup-name
8464                            ;; We might want to fetch old headers, but
8465                            ;; not if there is only 1 article.
8466                            (and gnus-fetch-old-headers
8467                                 (or (and
8468                                      (not (eq gnus-fetch-old-headers 'some))
8469                                      (not (numberp gnus-fetch-old-headers)))
8470                                     (> (length articles) 1))))))
8471                 (gnus-get-newsgroup-headers-xover articles)
8472               (gnus-get-newsgroup-headers)))
8473       (gnus-message 5 "Fetching headers...done")
8474
8475       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8476       (when cached
8477         (setq gnus-newsgroup-cached cached))
8478
8479       ;; Set the initial limit.
8480       (setq gnus-newsgroup-limit (copy-sequence articles))
8481       ;; Remove canceled articles from the list of unread articles.
8482       (setq gnus-newsgroup-unreads
8483             (gnus-set-sorted-intersection
8484              gnus-newsgroup-unreads
8485              (setq fetched-articles
8486                    (mapcar (lambda (headers) (mail-header-number headers))
8487                            gnus-newsgroup-headers))))
8488       ;; Removed marked articles that do not exist.
8489       (gnus-update-missing-marks
8490        (gnus-sorted-complement fetched-articles articles))
8491       ;; We might want to build some more threads first.
8492       (and gnus-fetch-old-headers
8493            (eq gnus-headers-retrieved-by 'nov)
8494            (gnus-build-old-threads))
8495       ;; Check whether auto-expire is to be done in this group.
8496       (setq gnus-newsgroup-auto-expire
8497             (gnus-group-auto-expirable-p group))
8498       ;; Set up the article buffer now, if necessary.
8499       (unless gnus-single-article-buffer
8500         (gnus-article-setup-buffer))
8501       ;; First and last article in this newsgroup.
8502       (and gnus-newsgroup-headers
8503            (setq gnus-newsgroup-begin
8504                  (mail-header-number (car gnus-newsgroup-headers)))
8505            (setq gnus-newsgroup-end
8506                  (mail-header-number
8507                   (gnus-last-element gnus-newsgroup-headers))))
8508       (setq gnus-reffed-article-number -1)
8509       ;; GROUP is successfully selected.
8510       (or gnus-newsgroup-headers t)))))
8511
8512 (defun gnus-articles-to-read (group read-all)
8513   ;; Find out what articles the user wants to read.
8514   (let* ((articles
8515           ;; Select all articles if `read-all' is non-nil, or if there
8516           ;; are no unread articles.
8517           (if (or read-all
8518                   (and (zerop (length gnus-newsgroup-marked))
8519                        (zerop (length gnus-newsgroup-unreads))))
8520               (gnus-uncompress-range (gnus-active group))
8521             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8522                           (copy-sequence gnus-newsgroup-unreads))
8523                   '<)))
8524          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8525          (scored (length scored-list))
8526          (number (length articles))
8527          (marked (+ (length gnus-newsgroup-marked)
8528                     (length gnus-newsgroup-dormant)))
8529          (select
8530           (cond
8531            ((numberp read-all)
8532             read-all)
8533            (t
8534             (condition-case ()
8535                 (cond
8536                  ((and (or (<= scored marked) (= scored number))
8537                        (numberp gnus-large-newsgroup)
8538                        (> number gnus-large-newsgroup))
8539                   (let ((input
8540                          (read-string
8541                           (format
8542                            "How many articles from %s (default %d): "
8543                            gnus-newsgroup-name number))))
8544                     (if (string-match "^[ \t]*$" input) number input)))
8545                  ((and (> scored marked) (< scored number))
8546                   (let ((input
8547                          (read-string
8548                           (format "%s %s (%d scored, %d total): "
8549                                   "How many articles from"
8550                                   group scored number))))
8551                     (if (string-match "^[ \t]*$" input)
8552                         number input)))
8553                  (t number))
8554               (quit nil))))))
8555     (setq select (if (stringp select) (string-to-number select) select))
8556     (if (or (null select) (zerop select))
8557         select
8558       (if (and (not (zerop scored)) (<= (abs select) scored))
8559           (progn
8560             (setq articles (sort scored-list '<))
8561             (setq number (length articles)))
8562         (setq articles (copy-sequence articles)))
8563
8564       (if (< (abs select) number)
8565           (if (< select 0)
8566               ;; Select the N oldest articles.
8567               (setcdr (nthcdr (1- (abs select)) articles) nil)
8568             ;; Select the N most recent articles.
8569             (setq articles (nthcdr (- number select) articles))))
8570       (setq gnus-newsgroup-unselected
8571             (gnus-sorted-intersection
8572              gnus-newsgroup-unreads
8573              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8574       articles)))
8575
8576 (defun gnus-killed-articles (killed articles)
8577   (let (out)
8578     (while articles
8579       (if (inline (gnus-member-of-range (car articles) killed))
8580           (setq out (cons (car articles) out)))
8581       (setq articles (cdr articles)))
8582     out))
8583
8584 (defun gnus-uncompress-marks (marks)
8585   "Uncompress the mark ranges in MARKS."
8586   (let ((uncompressed '(score bookmark))
8587         out)
8588     (while marks
8589       (if (memq (caar marks) uncompressed)
8590           (push (car marks) out)
8591         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8592       (setq marks (cdr marks)))
8593     out))
8594
8595 (defun gnus-adjust-marked-articles (info)
8596   "Set all article lists and remove all marks that are no longer legal."
8597   (let* ((marked-lists (gnus-info-marks info))
8598          (active (gnus-active (gnus-info-group info)))
8599          (min (car active))
8600          (max (cdr active))
8601          (types gnus-article-mark-lists)
8602          (uncompressed '(score bookmark))
8603          marks var articles article mark)
8604
8605     (while marked-lists
8606       (setq marks (pop marked-lists))
8607       (set (setq var (intern (format "gnus-newsgroup-%s"
8608                                      (car (rassq (setq mark (car marks))
8609                                                  types)))))
8610            (if (memq (car marks) uncompressed) (cdr marks)
8611              (gnus-uncompress-range (cdr marks))))
8612
8613       (setq articles (symbol-value var))
8614
8615       ;; All articles have to be subsets of the active articles.
8616       (cond
8617        ;; Adjust "simple" lists.
8618        ((memq mark '(tick dormant expirable reply killed save))
8619         (while articles
8620           (when (or (< (setq article (pop articles)) min) (> article max))
8621             (set var (delq article (symbol-value var))))))
8622        ;; Adjust assocs.
8623        ((memq mark '(score bookmark))
8624         (while articles
8625           (when (or (< (car (setq article (pop articles))) min)
8626                     (> (car article) max))
8627             (set var (delq article (symbol-value var))))))))))
8628
8629 (defun gnus-update-missing-marks (missing)
8630   "Go through the list of MISSING articles and remove them mark lists."
8631   (when missing
8632     (let ((types gnus-article-mark-lists)
8633           var m)
8634       ;; Go through all types.
8635       (while types
8636         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8637         (when (symbol-value var)
8638           ;; This list has articles.  So we delete all missing articles
8639           ;; from it.
8640           (setq m missing)
8641           (while m
8642             (set var (delq (pop m) (symbol-value var)))))))))
8643
8644 (defun gnus-update-marks ()
8645   "Enter the various lists of marked articles into the newsgroup info list."
8646   (let ((types gnus-article-mark-lists)
8647         (info (gnus-get-info gnus-newsgroup-name))
8648         (uncompressed '(score bookmark killed))
8649         type list newmarked symbol)
8650     (when info
8651       ;; Add all marks lists that are non-nil to the list of marks lists.
8652       (while types
8653         (setq type (pop types))
8654         (when (setq list (symbol-value
8655                           (setq symbol
8656                                 (intern (format "gnus-newsgroup-%s"
8657                                                 (car type))))))
8658           (push (cons (cdr type)
8659                       (if (memq (cdr type) uncompressed) list
8660                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8661                 newmarked)))
8662
8663       ;; Enter these new marks into the info of the group.
8664       (if (nthcdr 3 info)
8665           (setcar (nthcdr 3 info) newmarked)
8666         ;; Add the marks lists to the end of the info.
8667         (when newmarked
8668           (setcdr (nthcdr 2 info) (list newmarked))))
8669
8670       ;; Cut off the end of the info if there's nothing else there.
8671       (let ((i 5))
8672         (while (and (> i 2)
8673                     (not (nth i info)))
8674           (when (nthcdr (decf i) info)
8675             (setcdr (nthcdr i info) nil)))))))
8676
8677 (defun gnus-add-marked-articles (group type articles &optional info force)
8678   ;; Add ARTICLES of TYPE to the info of GROUP.
8679   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8680   ;; add, but replace marked articles of TYPE with ARTICLES.
8681   (let ((info (or info (gnus-get-info group)))
8682         (uncompressed '(score bookmark killed))
8683         marked m)
8684     (or (not info)
8685         (and (not (setq marked (nthcdr 3 info)))
8686              (or (null articles)
8687                  (setcdr (nthcdr 2 info)
8688                          (list (list (cons type (gnus-compress-sequence
8689                                                  articles t)))))))
8690         (and (not (setq m (assq type (car marked))))
8691              (or (null articles)
8692                  (setcar marked
8693                          (cons (cons type (gnus-compress-sequence articles t) )
8694                                (car marked)))))
8695         (if force
8696             (if (null articles)
8697                 (setcar (nthcdr 3 info)
8698                         (delq (assq type (car marked)) (car marked)))
8699               (setcdr m (gnus-compress-sequence articles t)))
8700           (setcdr m (gnus-compress-sequence
8701                      (sort (nconc (gnus-uncompress-range m)
8702                                   (copy-sequence articles)) '<) t))))))
8703
8704 (defun gnus-set-mode-line (where)
8705   "This function sets the mode line of the article or summary buffers.
8706 If WHERE is `summary', the summary mode line format will be used."
8707   ;; Is this mode line one we keep updated?
8708   (when (memq where gnus-updated-mode-lines)
8709     (let (mode-string)
8710       (save-excursion
8711         ;; We evaluate this in the summary buffer since these
8712         ;; variables are buffer-local to that buffer.
8713         (set-buffer gnus-summary-buffer)
8714         ;; We bind all these variables that are used in the `eval' form
8715         ;; below.
8716         (let* ((mformat (symbol-value
8717                          (intern
8718                           (format "gnus-%s-mode-line-format-spec" where))))
8719                (gnus-tmp-group-name gnus-newsgroup-name)
8720                (gnus-tmp-article-number (or gnus-current-article 0))
8721                (gnus-tmp-unread gnus-newsgroup-unreads)
8722                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8723                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8724                (gnus-tmp-unread-and-unselected
8725                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8726                             (zerop gnus-tmp-unselected)) "")
8727                       ((zerop gnus-tmp-unselected)
8728                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8729                       (t (format "{%d(+%d) more}"
8730                                  gnus-tmp-unread-and-unticked
8731                                  gnus-tmp-unselected))))
8732                (gnus-tmp-subject
8733                 (if (and gnus-current-headers
8734                          (vectorp gnus-current-headers))
8735                     (mail-header-subject gnus-current-headers) ""))
8736                max-len
8737                gnus-tmp-header);; passed as argument to any user-format-funcs
8738           (setq mode-string (eval mformat))
8739           (setq max-len (max 4 (if gnus-mode-non-string-length
8740                                    (- (frame-width)
8741                                       gnus-mode-non-string-length)
8742                                  (length mode-string))))
8743           ;; We might have to chop a bit of the string off...
8744           (when (> (length mode-string) max-len)
8745             (setq mode-string
8746                   (concat (gnus-truncate-string mode-string (- max-len 3))
8747                           "...")))
8748           ;; Pad the mode string a bit.
8749           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8750       ;; Update the mode line.
8751       (setq mode-line-buffer-identification (list mode-string))
8752       (set-buffer-modified-p t))))
8753
8754 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8755   "Go through the HEADERS list and add all Xrefs to a hash table.
8756 The resulting hash table is returned, or nil if no Xrefs were found."
8757   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8758          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8759          (xref-hashtb (make-vector 63 0))
8760          start group entry number xrefs header)
8761     (while headers
8762       (setq header (pop headers))
8763       (when (and (setq xrefs (mail-header-xref header))
8764                  (not (memq (setq number (mail-header-number header))
8765                             unreads)))
8766         (setq start 0)
8767         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8768           (setq start (match-end 0))
8769           (setq group (concat prefix (substring xrefs (match-beginning 1)
8770                                                 (match-end 1))))
8771           (setq number
8772                 (string-to-int (substring xrefs (match-beginning 2)
8773                                           (match-end 2))))
8774           (if (setq entry (gnus-gethash group xref-hashtb))
8775               (setcdr entry (cons number (cdr entry)))
8776             (gnus-sethash group (cons number nil) xref-hashtb)))))
8777     (and start xref-hashtb)))
8778
8779 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8780   "Look through all the headers and mark the Xrefs as read."
8781   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8782         name entry info xref-hashtb idlist method nth4)
8783     (save-excursion
8784       (set-buffer gnus-group-buffer)
8785       (when (setq xref-hashtb
8786                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8787         (mapatoms
8788          (lambda (group)
8789            (unless (string= from-newsgroup (setq name (symbol-name group)))
8790              (setq idlist (symbol-value group))
8791              ;; Dead groups are not updated.
8792              (and (prog1
8793                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8794                             info (nth 2 entry))
8795                     (if (stringp (setq nth4 (gnus-info-method info)))
8796                         (setq nth4 (gnus-server-to-method nth4))))
8797                   ;; Only do the xrefs if the group has the same
8798                   ;; select method as the group we have just read.
8799                   (or (gnus-methods-equal-p
8800                        nth4 (gnus-find-method-for-group from-newsgroup))
8801                       virtual
8802                       (equal nth4 (setq method (gnus-find-method-for-group
8803                                                 from-newsgroup)))
8804                       (and (equal (car nth4) (car method))
8805                            (equal (nth 1 nth4) (nth 1 method))))
8806                   gnus-use-cross-reference
8807                   (or (not (eq gnus-use-cross-reference t))
8808                       virtual
8809                       ;; Only do cross-references on subscribed
8810                       ;; groups, if that is what is wanted.
8811                       (<= (gnus-info-level info) gnus-level-subscribed))
8812                   (gnus-group-make-articles-read name idlist))))
8813          xref-hashtb)))))
8814
8815 (defun gnus-group-make-articles-read (group articles)
8816   (let* ((num 0)
8817          (entry (gnus-gethash group gnus-newsrc-hashtb))
8818          (info (nth 2 entry))
8819          (active (gnus-active group))
8820          range)
8821     ;; First peel off all illegal article numbers.
8822     (if active
8823         (let ((ids articles)
8824               id first)
8825           (while ids
8826             (setq id (car ids))
8827             (if (and first (> id (cdr active)))
8828                 (progn
8829                   ;; We'll end up in this situation in one particular
8830                   ;; obscure situation.  If you re-scan a group and get
8831                   ;; a new article that is cross-posted to a different
8832                   ;; group that has not been re-scanned, you might get
8833                   ;; crossposted article that has a higher number than
8834                   ;; Gnus believes possible.  So we re-activate this
8835                   ;; group as well.  This might mean doing the
8836                   ;; crossposting thingy will *increase* the number
8837                   ;; of articles in some groups.  Tsk, tsk.
8838                   (setq active (or (gnus-activate-group group) active))))
8839             (if (or (> id (cdr active))
8840                     (< id (car active)))
8841                 (setq articles (delq id articles)))
8842             (setq ids (cdr ids)))))
8843     ;; If the read list is nil, we init it.
8844     (and active
8845          (null (gnus-info-read info))
8846          (> (car active) 1)
8847          (gnus-info-set-read info (cons 1 (1- (car active)))))
8848     ;; Then we add the read articles to the range.
8849     (gnus-info-set-read
8850      info
8851      (setq range
8852            (gnus-add-to-range
8853             (gnus-info-read info) (setq articles (sort articles '<)))))
8854     ;; Then we have to re-compute how many unread
8855     ;; articles there are in this group.
8856     (if active
8857         (progn
8858           (cond
8859            ((not range)
8860             (setq num (- (1+ (cdr active)) (car active))))
8861            ((not (listp (cdr range)))
8862             (setq num (- (cdr active) (- (1+ (cdr range))
8863                                          (car range)))))
8864            (t
8865             (while range
8866               (if (numberp (car range))
8867                   (setq num (1+ num))
8868                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8869               (setq range (cdr range)))
8870             (setq num (- (cdr active) num))))
8871           ;; Update the number of unread articles.
8872           (setcar entry num)
8873           ;; Update the group buffer.
8874           (gnus-group-update-group group t)))))
8875
8876 (defun gnus-methods-equal-p (m1 m2)
8877   (let ((m1 (or m1 gnus-select-method))
8878         (m2 (or m2 gnus-select-method)))
8879     (or (equal m1 m2)
8880         (and (eq (car m1) (car m2))
8881              (or (not (memq 'address (assoc (symbol-name (car m1))
8882                                             gnus-valid-select-methods)))
8883                  (equal (nth 1 m1) (nth 1 m2)))))))
8884
8885 (defsubst gnus-header-value ()
8886   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8887
8888 (defvar gnus-newsgroup-none-id 0)
8889
8890 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8891   (let ((cur nntp-server-buffer)
8892         (dependencies
8893          (or dependencies
8894              (save-excursion (set-buffer gnus-summary-buffer)
8895                              gnus-newsgroup-dependencies)))
8896         headers id id-dep ref-dep end ref)
8897     (save-excursion
8898       (set-buffer nntp-server-buffer)
8899       (let ((case-fold-search t)
8900             in-reply-to header p lines)
8901         (goto-char (point-min))
8902         ;; Search to the beginning of the next header.  Error messages
8903         ;; do not begin with 2 or 3.
8904         (while (re-search-forward "^[23][0-9]+ " nil t)
8905           (setq id nil
8906                 ref nil)
8907           ;; This implementation of this function, with nine
8908           ;; search-forwards instead of the one re-search-forward and
8909           ;; a case (which basically was the old function) is actually
8910           ;; about twice as fast, even though it looks messier.  You
8911           ;; can't have everything, I guess.  Speed and elegance
8912           ;; doesn't always go hand in hand.
8913           (setq
8914            header
8915            (vector
8916             ;; Number.
8917             (prog1
8918                 (read cur)
8919               (end-of-line)
8920               (setq p (point))
8921               (narrow-to-region (point)
8922                                 (or (and (search-forward "\n.\n" nil t)
8923                                          (- (point) 2))
8924                                     (point))))
8925             ;; Subject.
8926             (progn
8927               (goto-char p)
8928               (if (search-forward "\nsubject: " nil t)
8929                   (gnus-header-value) "(none)"))
8930             ;; From.
8931             (progn
8932               (goto-char p)
8933               (if (search-forward "\nfrom: " nil t)
8934                   (gnus-header-value) "(nobody)"))
8935             ;; Date.
8936             (progn
8937               (goto-char p)
8938               (if (search-forward "\ndate: " nil t)
8939                   (gnus-header-value) ""))
8940             ;; Message-ID.
8941             (progn
8942               (goto-char p)
8943               (if (search-forward "\nmessage-id: " nil t)
8944                   (setq id (gnus-header-value))
8945                 ;; If there was no message-id, we just fake one to make
8946                 ;; subsequent routines simpler.
8947                 (setq id (concat "none+"
8948                                  (int-to-string
8949                                   (setq gnus-newsgroup-none-id
8950                                         (1+ gnus-newsgroup-none-id)))))))
8951             ;; References.
8952             (progn
8953               (goto-char p)
8954               (if (search-forward "\nreferences: " nil t)
8955                   (prog1
8956                       (gnus-header-value)
8957                     (setq end (match-end 0))
8958                     (save-excursion
8959                       (setq ref
8960                             (downcase
8961                              (buffer-substring
8962                               (progn
8963                                 (end-of-line)
8964                                 (search-backward ">" end t)
8965                                 (1+ (point)))
8966                               (progn
8967                                 (search-backward "<" end t)
8968                                 (point)))))))
8969                 ;; Get the references from the in-reply-to header if there
8970                 ;; were no references and the in-reply-to header looks
8971                 ;; promising.
8972                 (if (and (search-forward "\nin-reply-to: " nil t)
8973                          (setq in-reply-to (gnus-header-value))
8974                          (string-match "<[^>]+>" in-reply-to))
8975                     (prog1
8976                         (setq ref (substring in-reply-to (match-beginning 0)
8977                                              (match-end 0)))
8978                       (setq ref (downcase ref))))
8979                 (setq ref "")))
8980             ;; Chars.
8981             0
8982             ;; Lines.
8983             (progn
8984               (goto-char p)
8985               (if (search-forward "\nlines: " nil t)
8986                   (if (numberp (setq lines (read cur)))
8987                       lines 0)
8988                 0))
8989             ;; Xref.
8990             (progn
8991               (goto-char p)
8992               (and (search-forward "\nxref: " nil t)
8993                    (gnus-header-value)))))
8994           ;; We do the threading while we read the headers.  The
8995           ;; message-id and the last reference are both entered into
8996           ;; the same hash table.  Some tippy-toeing around has to be
8997           ;; done in case an article has arrived before the article
8998           ;; which it refers to.
8999           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9000               (if (and (car (symbol-value id-dep))
9001                        (not force-new))
9002                   ;; An article with this Message-ID has already
9003                   ;; been seen, so we ignore this one, except we add
9004                   ;; any additional Xrefs (in case the two articles
9005                   ;; came from different servers).
9006                   (progn
9007                     (mail-header-set-xref
9008                      (car (symbol-value id-dep))
9009                      (concat (or (mail-header-xref
9010                                   (car (symbol-value id-dep))) "")
9011                              (or (mail-header-xref header) "")))
9012                     (setq header nil))
9013                 (setcar (symbol-value id-dep) header))
9014             (set id-dep (list header)))
9015           (when header
9016             (if (boundp (setq ref-dep (intern ref dependencies)))
9017                 (setcdr (symbol-value ref-dep)
9018                         (nconc (cdr (symbol-value ref-dep))
9019                                (list (symbol-value id-dep))))
9020               (set ref-dep (list nil (symbol-value id-dep))))
9021             (setq headers (cons header headers)))
9022           (goto-char (point-max))
9023           (widen))
9024         (nreverse headers)))))
9025
9026 ;; The following macros and functions were written by Felix Lee
9027 ;; <flee@cse.psu.edu>.
9028
9029 (defmacro gnus-nov-read-integer ()
9030   '(prog1
9031        (if (= (following-char) ?\t)
9032            0
9033          (let ((num (condition-case nil (read buffer) (error nil))))
9034            (if (numberp num) num 0)))
9035      (or (eobp) (forward-char 1))))
9036
9037 (defmacro gnus-nov-skip-field ()
9038   '(search-forward "\t" eol 'move))
9039
9040 (defmacro gnus-nov-field ()
9041   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9042
9043 ;; Goes through the xover lines and returns a list of vectors
9044 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9045   "Parse the news overview data in the server buffer, and return a
9046 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9047   ;; Get the Xref when the users reads the articles since most/some
9048   ;; NNTP servers do not include Xrefs when using XOVER.
9049   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9050   (let ((cur nntp-server-buffer)
9051         (dependencies gnus-newsgroup-dependencies)
9052         number headers header)
9053     (save-excursion
9054       (set-buffer nntp-server-buffer)
9055       ;; Allow the user to mangle the headers before parsing them.
9056       (run-hooks 'gnus-parse-headers-hook)
9057       ;; Allow the user to mangle the headers before parsing them.
9058       (run-hooks 'gnus-parse-headers-hook)
9059       (goto-char (point-min))
9060       (while (and sequence (not (eobp)))
9061         (setq number (read cur))
9062         (while (and sequence (< (car sequence) number))
9063           (setq sequence (cdr sequence)))
9064         (and sequence
9065              (eq number (car sequence))
9066              (progn
9067                (setq sequence (cdr sequence))
9068                (if (setq header
9069                          (inline (gnus-nov-parse-line
9070                                   number dependencies force-new)))
9071                    (setq headers (cons header headers)))))
9072         (forward-line 1))
9073       (setq headers (nreverse headers)))
9074     headers))
9075
9076 ;; This function has to be called with point after the article number
9077 ;; on the beginning of the line.
9078 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9079   (let ((none 0)
9080         (eol (gnus-point-at-eol))
9081         (buffer (current-buffer))
9082         header ref id id-dep ref-dep)
9083
9084     ;; overview: [num subject from date id refs chars lines misc]
9085     (narrow-to-region (point) eol)
9086     (or (eobp) (forward-char))
9087
9088     (condition-case nil
9089         (setq header
9090               (vector
9091                number                   ; number
9092                (gnus-nov-field)         ; subject
9093                (gnus-nov-field)         ; from
9094                (gnus-nov-field)         ; date
9095                (setq id (or (gnus-nov-field)
9096                             (concat "none+"
9097                                     (int-to-string
9098                                      (setq none (1+ none)))))) ; id
9099                (progn
9100                  (save-excursion
9101                    (let ((beg (point)))
9102                      (search-forward "\t" eol)
9103                      (if (search-backward ">" beg t)
9104                          (setq ref
9105                                (downcase
9106                                 (buffer-substring
9107                                  (1+ (point))
9108                                  (progn
9109                                    (search-backward "<" beg t)
9110                                    (point)))))
9111                        (setq ref nil))))
9112                  (gnus-nov-field))      ; refs
9113                (gnus-nov-read-integer)  ; chars
9114                (gnus-nov-read-integer)  ; lines
9115                (if (= (following-char) ?\n)
9116                    nil
9117                  (gnus-nov-field))      ; misc
9118                ))
9119       (error (progn
9120                (ding)
9121                (gnus-message 4 "Strange nov line")
9122                (setq header nil)
9123                (goto-char eol))))
9124
9125     (widen)
9126
9127     ;; We build the thread tree.
9128     (when header
9129       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9130           (if (and (car (symbol-value id-dep))
9131                    (not force-new))
9132               ;; An article with this Message-ID has already been seen,
9133               ;; so we ignore this one, except we add any additional
9134               ;; Xrefs (in case the two articles came from different
9135               ;; servers.
9136               (progn
9137                 (mail-header-set-xref
9138                  (car (symbol-value id-dep))
9139                  (concat (or (mail-header-xref
9140                               (car (symbol-value id-dep))) "")
9141                          (or (mail-header-xref header) "")))
9142                 (setq header nil))
9143             (setcar (symbol-value id-dep) header))
9144         (set id-dep (list header))))
9145     (if header
9146         (progn
9147           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9148               (setcdr (symbol-value ref-dep)
9149                       (nconc (cdr (symbol-value ref-dep))
9150                              (list (symbol-value id-dep))))
9151             (set ref-dep (list nil (symbol-value id-dep))))))
9152     header))
9153
9154 (defun gnus-article-get-xrefs ()
9155   "Fill in the Xref value in `gnus-current-headers', if necessary.
9156 This is meant to be called in `gnus-article-internal-prepare-hook'."
9157   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9158                                  gnus-current-headers)))
9159     (or (not gnus-use-cross-reference)
9160         (not headers)
9161         (and (mail-header-xref headers)
9162              (not (string= (mail-header-xref headers) "")))
9163         (let ((case-fold-search t)
9164               xref)
9165           (save-restriction
9166             (nnheader-narrow-to-headers)
9167             (goto-char (point-min))
9168             (if (or (and (eq (downcase (following-char)) ?x)
9169                          (looking-at "Xref:"))
9170                     (search-forward "\nXref:" nil t))
9171                 (progn
9172                   (goto-char (1+ (match-end 0)))
9173                   (setq xref (buffer-substring (point)
9174                                                (progn (end-of-line) (point))))
9175                   (mail-header-set-xref headers xref))))))))
9176
9177 (defun gnus-summary-insert-subject (id)
9178   "Find article ID and insert the summary line for that article."
9179   (let ((header (gnus-read-header id))
9180         (number (and (numberp id) id)))
9181     (when header
9182       ;; Rebuild the thread that this article is part of and go to the
9183       ;; article we have fetched.
9184       (gnus-rebuild-thread (mail-header-id header))
9185       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9186     (when (and (numberp number)
9187                (> number 0))
9188       ;; We have to update the boundaries even if we can't fetch the
9189       ;; article if ID is a number -- so that the next `P' or `N'
9190       ;; command will fetch the previous (or next) article even
9191       ;; if the one we tried to fetch this time has been canceled.
9192       (and (> number gnus-newsgroup-end)
9193            (setq gnus-newsgroup-end number))
9194       (and (< number gnus-newsgroup-begin)
9195            (setq gnus-newsgroup-begin number))
9196       (setq gnus-newsgroup-unselected
9197             (delq number gnus-newsgroup-unselected)))
9198     ;; Report back a success?
9199     (and header number)))
9200
9201 (defun gnus-summary-work-articles (n)
9202   "Return a list of articles to be worked upon.  The prefix argument,
9203 the list of process marked articles, and the current article will be
9204 taken into consideration."
9205   (cond
9206    ((and n (numberp n))
9207     ;; A numerical prefix has been given.
9208     (let ((backward (< n 0))
9209           (n (abs n))
9210           articles article)
9211       (save-excursion
9212         (while
9213             (and (> n 0)
9214                  (push (setq article (gnus-summary-article-number))
9215                        articles)
9216                  (if backward
9217                      (gnus-summary-find-prev nil article)
9218                    (gnus-summary-find-next nil article)))
9219           (decf n)))
9220       (nreverse articles)))
9221    ((and (boundp 'transient-mark-mode)
9222          transient-mark-mode
9223          mark-active)
9224     ;; Work on the region between point and mark.
9225     (let ((max (max (point) (mark)))
9226           articles article)
9227       (save-excursion
9228         (goto-char (min (point) (mark)))
9229         (while
9230             (and
9231              (push (setq article (gnus-summary-article-number)) articles)
9232              (gnus-summary-find-next nil article)
9233              (< (point) max)))
9234         (nreverse articles))))
9235    (gnus-newsgroup-processable
9236     ;; There are process-marked articles present.
9237     (reverse gnus-newsgroup-processable))
9238    (t
9239     ;; Just return the current article.
9240     (list (gnus-summary-article-number)))))
9241
9242 (defun gnus-summary-search-group (&optional backward use-level)
9243   "Search for next unread newsgroup.
9244 If optional argument BACKWARD is non-nil, search backward instead."
9245   (save-excursion
9246     (set-buffer gnus-group-buffer)
9247     (if (gnus-group-search-forward
9248          backward nil (if use-level (gnus-group-group-level) nil))
9249         (gnus-group-group-name))))
9250
9251 (defun gnus-summary-best-group (&optional exclude-group)
9252   "Find the name of the best unread group.
9253 If EXCLUDE-GROUP, do not go to this group."
9254   (save-excursion
9255     (set-buffer gnus-group-buffer)
9256     (save-excursion
9257       (gnus-group-best-unread-group exclude-group))))
9258
9259 (defun gnus-summary-find-next (&optional unread article backward)
9260   (if backward (gnus-summary-find-prev)
9261     (let* ((article (or article (gnus-summary-article-number)))
9262            (arts (gnus-data-find-list article))
9263            result)
9264       (when (or (not gnus-summary-check-current)
9265                 (not unread)
9266                 (not (gnus-data-unread-p (car arts))))
9267         (setq arts (cdr arts)))
9268       (when (setq result
9269                   (if unread
9270                       (progn
9271                         (while arts
9272                           (when (gnus-data-unread-p (car arts))
9273                             (setq result (car arts)
9274                                   arts nil))
9275                           (setq arts (cdr arts)))
9276                         result)
9277                     (car arts)))
9278         (goto-char (gnus-data-pos result))
9279         (gnus-data-number result)))))
9280
9281 (defun gnus-summary-find-prev (&optional unread article)
9282   (let* ((article (or article (gnus-summary-article-number)))
9283          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9284          result)
9285     (when (or (not gnus-summary-check-current)
9286               (not unread)
9287               (not (gnus-data-unread-p (car arts))))
9288       (setq arts (cdr arts)))
9289     (if (setq result
9290               (if unread
9291                   (progn
9292                     (while arts
9293                       (and (gnus-data-unread-p (car arts))
9294                            (setq result (car arts)
9295                                  arts nil))
9296                       (setq arts (cdr arts)))
9297                     result)
9298                 (car arts)))
9299         (progn
9300           (goto-char (gnus-data-pos result))
9301           (gnus-data-number result)))))
9302
9303 (defun gnus-summary-find-subject (subject &optional unread backward article)
9304   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9305          (article (or article (gnus-summary-article-number)))
9306          (articles (gnus-data-list backward))
9307          (arts (gnus-data-find-list article articles))
9308          result)
9309     (when (or (not gnus-summary-check-current)
9310               (not unread)
9311               (not (gnus-data-unread-p (car arts))))
9312       (setq arts (cdr arts)))
9313     (while arts
9314       (and (or (not unread)
9315                (gnus-data-unread-p (car arts)))
9316            (vectorp (gnus-data-header (car arts)))
9317            (gnus-subject-equal
9318             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9319            (setq result (car arts)
9320                  arts nil))
9321       (setq arts (cdr arts)))
9322     (and result
9323          (goto-char (gnus-data-pos result))
9324          (gnus-data-number result))))
9325
9326 (defun gnus-summary-search-forward (&optional unread subject backward)
9327   "Search forward for an article.
9328 If UNREAD, look for unread articles.  If SUBJECT, look for
9329 articles with that subject.  If BACKWARD, search backward instead."
9330   (cond (subject (gnus-summary-find-subject subject unread backward))
9331         (backward (gnus-summary-find-prev unread))
9332         (t (gnus-summary-find-next unread))))
9333
9334 (defun gnus-recenter (&optional n)
9335   "Center point in window and redisplay frame.
9336 Also do horizontal recentering."
9337   (interactive "P")
9338   (when (and gnus-auto-center-summary
9339              (not (eq gnus-auto-center-summary 'vertical)))
9340     (gnus-horizontal-recenter))
9341   (recenter n))
9342
9343 (defun gnus-summary-recenter ()
9344   "Center point in the summary window.
9345 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9346 displayed, no centering will be performed."
9347   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9348   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9349   (let* ((top (cond ((< (window-height) 4) 0)
9350                     ((< (window-height) 7) 1)
9351                     (t 2)))
9352          (height (1- (window-height)))
9353          (bottom (save-excursion (goto-char (point-max))
9354                                  (forward-line (- height))
9355                                  (point)))
9356          (window (get-buffer-window (current-buffer))))
9357     ;; The user has to want it.
9358     (when gnus-auto-center-summary
9359       (when (get-buffer-window gnus-article-buffer)
9360        ;; Only do recentering when the article buffer is displayed,
9361        ;; Set the window start to either `bottom', which is the biggest
9362        ;; possible valid number, or the second line from the top,
9363        ;; whichever is the least.
9364        (set-window-start
9365         window (min bottom (save-excursion 
9366                              (forward-line (- top)) (point)))))
9367       ;; Do horizontal recentering while we're at it.
9368       (when (and (get-buffer-window (current-buffer) t)
9369                  (not (eq gnus-auto-center-summary 'vertical)))
9370         (let ((selected (selected-window)))
9371           (select-window (get-buffer-window (current-buffer) t))
9372           (gnus-summary-position-point)
9373           (gnus-horizontal-recenter)
9374           (select-window selected))))))
9375
9376 (defun gnus-horizontal-recenter ()
9377   "Recenter the current buffer horizontally."
9378   (if (< (current-column) (/ (window-width) 2))
9379       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9380     (let* ((orig (point))
9381            (end (window-end (get-buffer-window (current-buffer) t)))
9382            (max 0))
9383       ;; Find the longest line currently displayed in the window.
9384       (goto-char (window-start))
9385       (while (and (not (eobp)) 
9386                   (< (point) end))
9387         (end-of-line)
9388         (setq max (max max (current-column)))
9389         (forward-line 1))
9390       (goto-char orig)
9391       ;; Scroll horizontally to center (sort of) the point.
9392       (if (> max (window-width))
9393           (set-window-hscroll 
9394            (get-buffer-window (current-buffer) t)
9395            (min (- (current-column) (/ (window-width) 3))
9396                 (+ 2 (- max (window-width)))))
9397         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9398       max)))
9399
9400 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9401 (defun gnus-short-group-name (group &optional levels)
9402   "Collapse GROUP name LEVELS."
9403   (let* ((name "") 
9404          (foreign "")
9405          (depth 0) 
9406          (skip 1)
9407          (levels (or levels
9408                      (progn
9409                        (while (string-match "\\." group skip)
9410                          (setq skip (match-end 0)
9411                                depth (+ depth 1)))
9412                        depth))))
9413     (if (string-match ":" group)
9414         (setq foreign (substring group 0 (match-end 0))
9415               group (substring group (match-end 0))))
9416     (while group
9417       (if (and (string-match "\\." group)
9418                (> levels (- gnus-group-uncollapsed-levels 1)))
9419           (setq name (concat name (substring group 0 1))
9420                 group (substring group (match-end 0))
9421                 levels (- levels 1)
9422                 name (concat name "."))
9423         (setq name (concat foreign name group)
9424               group nil)))
9425     name))
9426
9427 (defun gnus-summary-jump-to-group (newsgroup)
9428   "Move point to NEWSGROUP in group mode buffer."
9429   ;; Keep update point of group mode buffer if visible.
9430   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9431       (save-window-excursion
9432         ;; Take care of tree window mode.
9433         (if (get-buffer-window gnus-group-buffer)
9434             (pop-to-buffer gnus-group-buffer))
9435         (gnus-group-jump-to-group newsgroup))
9436     (save-excursion
9437       ;; Take care of tree window mode.
9438       (if (get-buffer-window gnus-group-buffer)
9439           (pop-to-buffer gnus-group-buffer)
9440         (set-buffer gnus-group-buffer))
9441       (gnus-group-jump-to-group newsgroup))))
9442
9443 ;; This function returns a list of article numbers based on the
9444 ;; difference between the ranges of read articles in this group and
9445 ;; the range of active articles.
9446 (defun gnus-list-of-unread-articles (group)
9447   (let* ((read (gnus-info-read (gnus-get-info group)))
9448          (active (gnus-active group))
9449          (last (cdr active))
9450          first nlast unread)
9451     ;; If none are read, then all are unread.
9452     (if (not read)
9453         (setq first (car active))
9454       ;; If the range of read articles is a single range, then the
9455       ;; first unread article is the article after the last read
9456       ;; article.  Sounds logical, doesn't it?
9457       (if (not (listp (cdr read)))
9458           (setq first (1+ (cdr read)))
9459         ;; `read' is a list of ranges.
9460         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9461                                 (caar read))) 1)
9462             (setq first 1))
9463         (while read
9464           (if first
9465               (while (< first nlast)
9466                 (setq unread (cons first unread))
9467                 (setq first (1+ first))))
9468           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9469           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9470           (setq read (cdr read)))))
9471     ;; And add the last unread articles.
9472     (while (<= first last)
9473       (setq unread (cons first unread))
9474       (setq first (1+ first)))
9475     ;; Return the list of unread articles.
9476     (nreverse unread)))
9477
9478 (defun gnus-list-of-read-articles (group)
9479   "Return a list of unread, unticked and non-dormant articles."
9480   (let* ((info (gnus-get-info group))
9481          (marked (gnus-info-marks info))
9482          (active (gnus-active group)))
9483     (and info active
9484          (gnus-set-difference
9485           (gnus-sorted-complement
9486            (gnus-uncompress-range active)
9487            (gnus-list-of-unread-articles group))
9488           (append
9489            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9490            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9491
9492 ;; Various summary commands
9493
9494 (defun gnus-summary-universal-argument (arg)
9495   "Perform any operation on all articles that are process/prefixed."
9496   (interactive "P")
9497   (gnus-set-global-variables)
9498   (let ((articles (gnus-summary-work-articles arg))
9499         func article)
9500     (if (eq
9501          (setq
9502           func
9503           (key-binding
9504            (read-key-sequence
9505             (substitute-command-keys
9506              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9507              ))))
9508          'undefined)
9509         (progn
9510           (message "Undefined key")
9511           (ding))
9512       (save-excursion
9513         (while articles
9514           (gnus-summary-goto-subject (setq article (pop articles)))
9515           (command-execute func)
9516           (gnus-summary-remove-process-mark article)))))
9517   (gnus-summary-position-point))
9518
9519 (defun gnus-summary-toggle-truncation (&optional arg)
9520   "Toggle truncation of summary lines.
9521 With arg, turn line truncation on iff arg is positive."
9522   (interactive "P")
9523   (setq truncate-lines
9524         (if (null arg) (not truncate-lines)
9525           (> (prefix-numeric-value arg) 0)))
9526   (redraw-display))
9527
9528 (defun gnus-summary-reselect-current-group (&optional all rescan)
9529   "Exit and then reselect the current newsgroup.
9530 The prefix argument ALL means to select all articles."
9531   (interactive "P")
9532   (gnus-set-global-variables)
9533   (let ((current-subject (gnus-summary-article-number))
9534         (group gnus-newsgroup-name))
9535     (setq gnus-newsgroup-begin nil)
9536     (gnus-summary-exit)
9537     ;; We have to adjust the point of group mode buffer because the
9538     ;; current point was moved to the next unread newsgroup by
9539     ;; exiting.
9540     (gnus-summary-jump-to-group group)
9541     (when rescan
9542       (save-excursion
9543         (gnus-group-get-new-news-this-group 1)))
9544     (gnus-group-read-group all t)
9545     (gnus-summary-goto-subject current-subject)))
9546
9547 (defun gnus-summary-rescan-group (&optional all)
9548   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9549   (interactive "P")
9550   (gnus-summary-reselect-current-group all t))
9551
9552 (defun gnus-summary-update-info ()
9553   (let* ((group gnus-newsgroup-name))
9554     (when gnus-newsgroup-kill-headers
9555       (setq gnus-newsgroup-killed
9556             (gnus-compress-sequence
9557              (nconc
9558               (gnus-set-sorted-intersection
9559                (gnus-uncompress-range gnus-newsgroup-killed)
9560                (setq gnus-newsgroup-unselected
9561                      (sort gnus-newsgroup-unselected '<)))
9562               (setq gnus-newsgroup-unreads
9563                     (sort gnus-newsgroup-unreads '<))) t)))
9564     (unless (listp (cdr gnus-newsgroup-killed))
9565       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9566     (let ((headers gnus-newsgroup-headers))
9567       (run-hooks 'gnus-exit-group-hook)
9568       (unless gnus-save-score
9569         (setq gnus-newsgroup-scored nil))
9570       ;; Set the new ranges of read articles.
9571       (gnus-update-read-articles
9572        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9573       ;; Set the current article marks.
9574       (gnus-update-marks)
9575       ;; Do the cross-ref thing.
9576       (when gnus-use-cross-reference
9577         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9578       ;; Do adaptive scoring, and possibly save score files.
9579       (when gnus-newsgroup-adaptive
9580         (gnus-score-adaptive))
9581       (when gnus-use-scoring
9582         (gnus-score-save))
9583       ;; Do not switch windows but change the buffer to work.
9584       (set-buffer gnus-group-buffer)
9585       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9586           (gnus-group-update-group group)))))
9587
9588 (defun gnus-summary-exit (&optional temporary)
9589   "Exit reading current newsgroup, and then return to group selection mode.
9590 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9591   (interactive)
9592   (gnus-set-global-variables)
9593   (gnus-kill-save-kill-buffer)
9594   (let* ((group gnus-newsgroup-name)
9595          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9596          (mode major-mode)
9597          (buf (current-buffer)))
9598     (run-hooks 'gnus-summary-prepare-exit-hook)
9599     ;; If we have several article buffers, we kill them at exit.
9600     (unless gnus-single-article-buffer
9601       (gnus-kill-buffer gnus-article-buffer)
9602       (gnus-kill-buffer gnus-original-article-buffer))
9603     (when gnus-use-cache
9604       (gnus-cache-possibly-remove-articles)
9605       (gnus-cache-save-buffers))
9606     (when gnus-use-trees
9607       (gnus-tree-close group))
9608     ;; Make all changes in this group permanent.
9609     (unless quit-config
9610       (gnus-summary-update-info))
9611     (gnus-close-group group)
9612     ;; Make sure where I was, and go to next newsgroup.
9613     (set-buffer gnus-group-buffer)
9614     (unless quit-config
9615       (gnus-group-jump-to-group group)
9616       (gnus-group-next-unread-group 1))
9617     (run-hooks 'gnus-summary-exit-hook)
9618     (if temporary
9619         nil                             ;Nothing to do.
9620       ;; If we have several article buffers, we kill them at exit.
9621       (unless gnus-single-article-buffer
9622         (gnus-kill-buffer gnus-article-buffer)
9623         (gnus-kill-buffer gnus-original-article-buffer))
9624       (set-buffer buf)
9625       (if (not gnus-kill-summary-on-exit)
9626           (gnus-deaden-summary)
9627         ;; We set all buffer-local variables to nil.  It is unclear why
9628         ;; this is needed, but if we don't, buffer-local variables are
9629         ;; not garbage-collected, it seems.  This would the lead to en
9630         ;; ever-growing Emacs.
9631         (gnus-summary-clear-local-variables)
9632         (when (get-buffer gnus-article-buffer)
9633           (bury-buffer gnus-article-buffer))
9634         ;; We clear the global counterparts of the buffer-local
9635         ;; variables as well, just to be on the safe side.
9636         (gnus-configure-windows 'group 'force)
9637         (gnus-summary-clear-local-variables)
9638         ;; Return to group mode buffer.
9639         (if (eq mode 'gnus-summary-mode)
9640             (gnus-kill-buffer buf)))
9641       (setq gnus-current-select-method gnus-select-method)
9642       (pop-to-buffer gnus-group-buffer)
9643       ;; Clear the current group name.
9644       (if (not quit-config)
9645           (progn
9646             (gnus-group-jump-to-group group)
9647             (gnus-group-next-unread-group 1)
9648             (gnus-configure-windows 'group 'force))
9649         (if (not (buffer-name (car quit-config)))
9650             (gnus-configure-windows 'group 'force)
9651           (set-buffer (car quit-config))
9652           (and (eq major-mode 'gnus-summary-mode)
9653                (gnus-set-global-variables))
9654           (gnus-configure-windows (cdr quit-config))))
9655       (unless quit-config
9656         (setq gnus-newsgroup-name nil)))))
9657
9658 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9659 (defun gnus-summary-exit-no-update (&optional no-questions)
9660   "Quit reading current newsgroup without updating read article info."
9661   (interactive)
9662   (gnus-set-global-variables)
9663   (let* ((group gnus-newsgroup-name)
9664          (quit-config (gnus-group-quit-config group)))
9665     (when (or no-questions
9666               gnus-expert-user
9667               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9668       ;; If we have several article buffers, we kill them at exit.
9669       (unless gnus-single-article-buffer
9670         (gnus-kill-buffer gnus-article-buffer)
9671         (gnus-kill-buffer gnus-original-article-buffer))
9672       (if (not gnus-kill-summary-on-exit)
9673           (gnus-deaden-summary)
9674         (gnus-close-group group)
9675         (gnus-summary-clear-local-variables)
9676         (set-buffer gnus-group-buffer)
9677         (gnus-summary-clear-local-variables)
9678         (when (get-buffer gnus-summary-buffer)
9679           (kill-buffer gnus-summary-buffer)))
9680       (when gnus-use-trees
9681         (gnus-tree-close group))
9682       (when (get-buffer gnus-article-buffer)
9683         (bury-buffer gnus-article-buffer))
9684       ;; Return to the group buffer.
9685       (gnus-configure-windows 'group 'force)
9686       ;; Clear the current group name.
9687       (setq gnus-newsgroup-name nil)
9688       (when (equal (gnus-group-group-name) group)
9689         (gnus-group-next-unread-group 1))
9690       (when quit-config
9691         (if (not (buffer-name (car quit-config)))
9692             (gnus-configure-windows 'group 'force)
9693           (set-buffer (car quit-config))
9694           (when (eq major-mode 'gnus-summary-mode)
9695             (gnus-set-global-variables))
9696           (gnus-configure-windows (cdr quit-config)))))))
9697
9698 ;;; Dead summaries.
9699
9700 (defvar gnus-dead-summary-mode-map nil)
9701
9702 (if gnus-dead-summary-mode-map
9703     nil
9704   (setq gnus-dead-summary-mode-map (make-keymap))
9705   (suppress-keymap gnus-dead-summary-mode-map)
9706   (substitute-key-definition
9707    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9708   (let ((keys '("\C-d" "\r" "\177")))
9709     (while keys
9710       (define-key gnus-dead-summary-mode-map
9711         (pop keys) 'gnus-summary-wake-up-the-dead))))
9712
9713 (defvar gnus-dead-summary-mode nil
9714   "Minor mode for Gnus summary buffers.")
9715
9716 (defun gnus-dead-summary-mode (&optional arg)
9717   "Minor mode for Gnus summary buffers."
9718   (interactive "P")
9719   (when (eq major-mode 'gnus-summary-mode)
9720     (make-local-variable 'gnus-dead-summary-mode)
9721     (setq gnus-dead-summary-mode
9722           (if (null arg) (not gnus-dead-summary-mode)
9723             (> (prefix-numeric-value arg) 0)))
9724     (when gnus-dead-summary-mode
9725       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9726         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9727       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9728         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9729               minor-mode-map-alist)))))
9730
9731 (defun gnus-deaden-summary ()
9732   "Make the current summary buffer into a dead summary buffer."
9733   ;; Kill any previous dead summary buffer.
9734   (when (and gnus-dead-summary
9735              (buffer-name gnus-dead-summary))
9736     (save-excursion
9737       (set-buffer gnus-dead-summary)
9738       (when gnus-dead-summary-mode
9739         (kill-buffer (current-buffer)))))
9740   ;; Make this the current dead summary.
9741   (setq gnus-dead-summary (current-buffer))
9742   (gnus-dead-summary-mode 1)
9743   (let ((name (buffer-name)))
9744     (when (string-match "Summary" name)
9745       (rename-buffer
9746        (concat (substring name 0 (match-beginning 0)) "Dead "
9747                (substring name (match-beginning 0))) t))))
9748
9749 (defun gnus-kill-or-deaden-summary (buffer)
9750   "Kill or deaden the summary BUFFER."
9751   (cond (gnus-kill-summary-on-exit
9752          (when (and gnus-use-trees
9753                     (and (get-buffer buffer)
9754                          (buffer-name (get-buffer buffer))))
9755            (save-excursion
9756              (set-buffer (get-buffer buffer))
9757              (gnus-tree-close gnus-newsgroup-name)))
9758          (gnus-kill-buffer buffer))
9759         ((and (get-buffer buffer)
9760               (buffer-name (get-buffer buffer)))
9761          (save-excursion
9762            (set-buffer buffer)
9763            (gnus-deaden-summary)))))
9764
9765 (defun gnus-summary-wake-up-the-dead (&rest args)
9766   "Wake up the dead summary buffer."
9767   (interactive)
9768   (gnus-dead-summary-mode -1)
9769   (let ((name (buffer-name)))
9770     (when (string-match "Dead " name)
9771       (rename-buffer
9772        (concat (substring name 0 (match-beginning 0))
9773                (substring name (match-end 0))) t)))
9774   (gnus-message 3 "This dead summary is now alive again"))
9775
9776 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9777 (defun gnus-summary-fetch-faq (&optional faq-dir)
9778   "Fetch the FAQ for the current group.
9779 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9780 in."
9781   (interactive
9782    (list
9783     (if current-prefix-arg
9784         (completing-read
9785          "Faq dir: " (and (listp gnus-group-faq-directory)
9786                           gnus-group-faq-directory)))))
9787   (let (gnus-faq-buffer)
9788     (and (setq gnus-faq-buffer
9789                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9790          (gnus-configure-windows 'summary-faq))))
9791
9792 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9793 (defun gnus-summary-describe-group (&optional force)
9794   "Describe the current newsgroup."
9795   (interactive "P")
9796   (gnus-group-describe-group force gnus-newsgroup-name))
9797
9798 (defun gnus-summary-describe-briefly ()
9799   "Describe summary mode commands briefly."
9800   (interactive)
9801   (gnus-message 6
9802                 (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")))
9803
9804 ;; Walking around group mode buffer from summary mode.
9805
9806 (defun gnus-summary-next-group (&optional no-article target-group backward)
9807   "Exit current newsgroup and then select next unread newsgroup.
9808 If prefix argument NO-ARTICLE is non-nil, no article is selected
9809 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9810 previous group instead."
9811   (interactive "P")
9812   (gnus-set-global-variables)
9813   (let ((current-group gnus-newsgroup-name)
9814         (current-buffer (current-buffer))
9815         entered)
9816     ;; First we semi-exit this group to update Xrefs and all variables.
9817     ;; We can't do a real exit, because the window conf must remain
9818     ;; the same in case the user is prompted for info, and we don't
9819     ;; want the window conf to change before that...
9820     (gnus-summary-exit t)
9821     (while (not entered)
9822       ;; Then we find what group we are supposed to enter.
9823       (set-buffer gnus-group-buffer)
9824       (gnus-group-jump-to-group current-group)
9825       (setq target-group
9826             (or target-group
9827                 (if (eq gnus-keep-same-level 'best)
9828                     (gnus-summary-best-group gnus-newsgroup-name)
9829                   (gnus-summary-search-group backward gnus-keep-same-level))))
9830       (if (not target-group)
9831           ;; There are no further groups, so we return to the group
9832           ;; buffer.
9833           (progn
9834             (gnus-message 5 "Returning to the group buffer")
9835             (setq entered t)
9836             (set-buffer current-buffer)
9837             (gnus-summary-exit))
9838         ;; We try to enter the target group.
9839         (gnus-group-jump-to-group target-group)
9840         (let ((unreads (gnus-group-group-unread)))
9841           (if (and (or (eq t unreads)
9842                        (and unreads (not (zerop unreads))))
9843                    (gnus-summary-read-group
9844                     target-group nil no-article current-buffer))
9845               (setq entered t)
9846             (setq current-group target-group
9847                   target-group nil)))))))
9848
9849 (defun gnus-summary-prev-group (&optional no-article)
9850   "Exit current newsgroup and then select previous unread newsgroup.
9851 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9852   (interactive "P")
9853   (gnus-summary-next-group no-article nil t))
9854
9855 ;; Walking around summary lines.
9856
9857 (defun gnus-summary-first-subject (&optional unread)
9858   "Go to the first unread subject.
9859 If UNREAD is non-nil, go to the first unread article.
9860 Returns the article selected or nil if there are no unread articles."
9861   (interactive "P")
9862   (prog1
9863       (cond
9864        ;; Empty summary.
9865        ((null gnus-newsgroup-data)
9866         (gnus-message 3 "No articles in the group")
9867         nil)
9868        ;; Pick the first article.
9869        ((not unread)
9870         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9871         (gnus-data-number (car gnus-newsgroup-data)))
9872        ;; No unread articles.
9873        ((null gnus-newsgroup-unreads)
9874         (gnus-message 3 "No more unread articles")
9875         nil)
9876        ;; Find the first unread article.
9877        (t
9878         (let ((data gnus-newsgroup-data))
9879           (while (and data
9880                       (not (gnus-data-unread-p (car data))))
9881             (setq data (cdr data)))
9882           (if data
9883               (progn
9884                 (goto-char (gnus-data-pos (car data)))
9885                 (gnus-data-number (car data)))))))
9886     (gnus-summary-position-point)))
9887
9888 (defun gnus-summary-next-subject (n &optional unread dont-display)
9889   "Go to next N'th summary line.
9890 If N is negative, go to the previous N'th subject line.
9891 If UNREAD is non-nil, only unread articles are selected.
9892 The difference between N and the actual number of steps taken is
9893 returned."
9894   (interactive "p")
9895   (let ((backward (< n 0))
9896         (n (abs n)))
9897     (while (and (> n 0)
9898                 (if backward
9899                     (gnus-summary-find-prev unread)
9900                   (gnus-summary-find-next unread)))
9901       (setq n (1- n)))
9902     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9903                                (if unread " unread" "")))
9904     (unless dont-display
9905       (gnus-summary-recenter)
9906       (gnus-summary-position-point))
9907     n))
9908
9909 (defun gnus-summary-next-unread-subject (n)
9910   "Go to next N'th unread summary line."
9911   (interactive "p")
9912   (gnus-summary-next-subject n t))
9913
9914 (defun gnus-summary-prev-subject (n &optional unread)
9915   "Go to previous N'th summary line.
9916 If optional argument UNREAD is non-nil, only unread article is selected."
9917   (interactive "p")
9918   (gnus-summary-next-subject (- n) unread))
9919
9920 (defun gnus-summary-prev-unread-subject (n)
9921   "Go to previous N'th unread summary line."
9922   (interactive "p")
9923   (gnus-summary-next-subject (- n) t))
9924
9925 (defun gnus-summary-goto-subject (article &optional force silent)
9926   "Go the subject line of ARTICLE.
9927 If FORCE, also allow jumping to articles not currently shown."
9928   (let ((b (point))
9929         (data (gnus-data-find article)))
9930     ;; We read in the article if we have to.
9931     (and (not data)
9932          force
9933          (gnus-summary-insert-subject article)
9934          (setq data (gnus-data-find article)))
9935     (goto-char b)
9936     (if (not data)
9937         (progn
9938           (unless silent
9939             (gnus-message 3 "Can't find article %d" article))
9940           nil)
9941       (goto-char (gnus-data-pos data))
9942       article)))
9943
9944 ;; Walking around summary lines with displaying articles.
9945
9946 (defun gnus-summary-expand-window (&optional arg)
9947   "Make the summary buffer take up the entire Emacs frame.
9948 Given a prefix, will force an `article' buffer configuration."
9949   (interactive "P")
9950   (gnus-set-global-variables)
9951   (if arg
9952       (gnus-configure-windows 'article 'force)
9953     (gnus-configure-windows 'summary 'force)))
9954
9955 (defun gnus-summary-display-article (article &optional all-header)
9956   "Display ARTICLE in article buffer."
9957   (gnus-set-global-variables)
9958   (if (null article)
9959       nil
9960     (prog1
9961         (if gnus-summary-display-article-function
9962             (funcall gnus-summary-display-article-function article all-header)
9963           (gnus-article-prepare article all-header))
9964       (run-hooks 'gnus-select-article-hook)
9965       (gnus-summary-recenter)
9966       (gnus-summary-goto-subject article)
9967       (when gnus-use-trees
9968         (gnus-possibly-generate-tree article)
9969         (gnus-highlight-selected-tree article))
9970       ;; Successfully display article.
9971       (gnus-article-set-window-start
9972        (cdr (assq article gnus-newsgroup-bookmarks)))
9973       t)))
9974
9975 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9976   "Select the current article.
9977 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9978 non-nil, the article will be re-fetched even if it already present in
9979 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9980 be displayed."
9981   (let ((article (or article (gnus-summary-article-number)))
9982         (all-headers (not (not all-headers))) ;Must be T or NIL.
9983         gnus-summary-display-article-function
9984         did)
9985     (and (not pseudo)
9986          (gnus-summary-article-pseudo-p article)
9987          (error "This is a pseudo-article."))
9988     (prog1
9989         (save-excursion
9990           (set-buffer gnus-summary-buffer)
9991           (if (or (and gnus-single-article-buffer
9992                        (or (null gnus-current-article)
9993                            (null gnus-article-current)
9994                            (null (get-buffer gnus-article-buffer))
9995                            (not (eq article (cdr gnus-article-current)))
9996                            (not (equal (car gnus-article-current)
9997                                        gnus-newsgroup-name))))
9998                   (and (not gnus-single-article-buffer)
9999                        (null gnus-current-article))
10000                   force)
10001               ;; The requested article is different from the current article.
10002               (prog1
10003                   (gnus-summary-display-article article all-headers)
10004                 (setq did article))
10005             (if (or all-headers gnus-show-all-headers)
10006                 (gnus-article-show-all-headers))
10007             'old))
10008       (if did
10009           (gnus-article-set-window-start
10010            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10011
10012 (defun gnus-summary-set-current-mark (&optional current-mark)
10013   "Obsolete function."
10014   nil)
10015
10016 (defun gnus-summary-next-article (&optional unread subject backward push)
10017   "Select the next article.
10018 If UNREAD, only unread articles are selected.
10019 If SUBJECT, only articles with SUBJECT are selected.
10020 If BACKWARD, the previous article is selected instead of the next."
10021   (interactive "P")
10022   (gnus-set-global-variables)
10023   (cond
10024    ;; Is there such an article?
10025    ((and (gnus-summary-search-forward unread subject backward)
10026          (or (gnus-summary-display-article (gnus-summary-article-number))
10027              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10028     (gnus-summary-position-point))
10029    ;; If not, we try the first unread, if that is wanted.
10030    ((and subject
10031          gnus-auto-select-same
10032          (or (gnus-summary-first-unread-article)
10033              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10034     (gnus-summary-position-point)
10035     (gnus-message 6 "Wrapped"))
10036    ;; Try to get next/previous article not displayed in this group.
10037    ((and gnus-auto-extend-newsgroup
10038          (not unread) (not subject))
10039     (gnus-summary-goto-article
10040      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10041      nil t))
10042    ;; Go to next/previous group.
10043    (t
10044     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10045         (gnus-summary-jump-to-group gnus-newsgroup-name))
10046     (let ((cmd last-command-char)
10047           (group
10048            (if (eq gnus-keep-same-level 'best)
10049                (gnus-summary-best-group gnus-newsgroup-name)
10050              (gnus-summary-search-group backward gnus-keep-same-level))))
10051       ;; For some reason, the group window gets selected.  We change
10052       ;; it back.
10053       (select-window (get-buffer-window (current-buffer)))
10054       ;; Select next unread newsgroup automagically.
10055       (cond
10056        ((not gnus-auto-select-next)
10057         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10058        ((or (eq gnus-auto-select-next 'quietly)
10059             (and (eq gnus-auto-select-next 'slightly-quietly)
10060                  push)
10061             (and (eq gnus-auto-select-next 'almost-quietly)
10062                  (gnus-summary-last-article-p)))
10063         ;; Select quietly.
10064         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10065             (gnus-summary-exit)
10066           (gnus-message 7 "No more%s articles (%s)..."
10067                         (if unread " unread" "")
10068                         (if group (concat "selecting " group)
10069                           "exiting"))
10070           (gnus-summary-next-group nil group backward)))
10071        (t
10072         (gnus-summary-walk-group-buffer
10073          gnus-newsgroup-name cmd unread backward)))))))
10074
10075 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10076   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10077                       (?\C-p (gnus-group-prev-unread-group 1))))
10078         keve key group ended)
10079     (save-excursion
10080       (set-buffer gnus-group-buffer)
10081       (gnus-summary-jump-to-group from-group)
10082       (setq group
10083             (if (eq gnus-keep-same-level 'best)
10084                 (gnus-summary-best-group gnus-newsgroup-name)
10085               (gnus-summary-search-group backward gnus-keep-same-level))))
10086     (while (not ended)
10087       (gnus-message
10088        5 "No more%s articles%s" (if unread " unread" "")
10089        (if (and group
10090                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10091            (format " (Type %s for %s [%s])"
10092                    (single-key-description cmd) group
10093                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10094          (format " (Type %s to exit %s)"
10095                  (single-key-description cmd)
10096                  gnus-newsgroup-name)))
10097       ;; Confirm auto selection.
10098       (setq key (car (setq keve (gnus-read-event-char))))
10099       (setq ended t)
10100       (cond
10101        ((assq key keystrokes)
10102         (let ((obuf (current-buffer)))
10103           (switch-to-buffer gnus-group-buffer)
10104           (and group
10105                (gnus-group-jump-to-group group))
10106           (eval (cadr (assq key keystrokes)))
10107           (setq group (gnus-group-group-name))
10108           (switch-to-buffer obuf))
10109         (setq ended nil))
10110        ((equal key cmd)
10111         (if (or (not group)
10112                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10113             (gnus-summary-exit)
10114           (gnus-summary-next-group nil group backward)))
10115        (t
10116         (push (cdr keve) unread-command-events))))))
10117
10118 (defun gnus-read-event-char ()
10119   "Get the next event."
10120   (let ((event (read-event)))
10121     (cons (and (numberp event) event) event)))
10122
10123 (defun gnus-summary-next-unread-article ()
10124   "Select unread article after current one."
10125   (interactive)
10126   (gnus-summary-next-article t (and gnus-auto-select-same
10127                                     (gnus-summary-article-subject))))
10128
10129 (defun gnus-summary-prev-article (&optional unread subject)
10130   "Select the article after the current one.
10131 If UNREAD is non-nil, only unread articles are selected."
10132   (interactive "P")
10133   (gnus-summary-next-article unread subject t))
10134
10135 (defun gnus-summary-prev-unread-article ()
10136   "Select unred article before current one."
10137   (interactive)
10138   (gnus-summary-prev-article t (and gnus-auto-select-same
10139                                     (gnus-summary-article-subject))))
10140
10141 (defun gnus-summary-next-page (&optional lines circular)
10142   "Show next page of the selected article.
10143 If at the end of the current article, select the next article.
10144 LINES says how many lines should be scrolled up.
10145
10146 If CIRCULAR is non-nil, go to the start of the article instead of
10147 selecting the next article when reaching the end of the current
10148 article."
10149   (interactive "P")
10150   (setq gnus-summary-buffer (current-buffer))
10151   (gnus-set-global-variables)
10152   (let ((article (gnus-summary-article-number))
10153         (endp nil))
10154     (gnus-configure-windows 'article)
10155     (if (or (null gnus-current-article)
10156             (null gnus-article-current)
10157             (/= article (cdr gnus-article-current))
10158             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10159         ;; Selected subject is different from current article's.
10160         (gnus-summary-display-article article)
10161       (gnus-eval-in-buffer-window
10162        gnus-article-buffer
10163        (setq endp (gnus-article-next-page lines)))
10164       (if endp
10165           (cond (circular
10166                  (gnus-summary-beginning-of-article))
10167                 (lines
10168                  (gnus-message 3 "End of message"))
10169                 ((null lines)
10170                  (if (and (eq gnus-summary-goto-unread 'never)
10171                           (not (eq article gnus-newsgroup-end)))
10172                      (gnus-summary-next-article)
10173                    (gnus-summary-next-unread-article))))))
10174     (gnus-summary-recenter)
10175     (gnus-summary-position-point)))
10176
10177 (defun gnus-summary-prev-page (&optional lines)
10178   "Show previous page of selected article.
10179 Argument LINES specifies lines to be scrolled down."
10180   (interactive "P")
10181   (gnus-set-global-variables)
10182   (let ((article (gnus-summary-article-number)))
10183     (gnus-configure-windows 'article)
10184     (if (or (null gnus-current-article)
10185             (null gnus-article-current)
10186             (/= article (cdr gnus-article-current))
10187             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10188         ;; Selected subject is different from current article's.
10189         (gnus-summary-display-article article)
10190       (gnus-summary-recenter)
10191       (gnus-eval-in-buffer-window gnus-article-buffer
10192                                   (gnus-article-prev-page lines))))
10193   (gnus-summary-position-point))
10194
10195 (defun gnus-summary-scroll-up (lines)
10196   "Scroll up (or down) one line current article.
10197 Argument LINES specifies lines to be scrolled up (or down if negative)."
10198   (interactive "p")
10199   (gnus-set-global-variables)
10200   (gnus-configure-windows 'article)
10201   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10202     (gnus-eval-in-buffer-window
10203      gnus-article-buffer
10204      (cond ((> lines 0)
10205             (if (gnus-article-next-page lines)
10206                 (gnus-message 3 "End of message")))
10207            ((< lines 0)
10208             (gnus-article-prev-page (- lines))))))
10209   (gnus-summary-recenter)
10210   (gnus-summary-position-point))
10211
10212 (defun gnus-summary-next-same-subject ()
10213   "Select next article which has the same subject as current one."
10214   (interactive)
10215   (gnus-set-global-variables)
10216   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10217
10218 (defun gnus-summary-prev-same-subject ()
10219   "Select previous article which has the same subject as current one."
10220   (interactive)
10221   (gnus-set-global-variables)
10222   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10223
10224 (defun gnus-summary-next-unread-same-subject ()
10225   "Select next unread article which has the same subject as current one."
10226   (interactive)
10227   (gnus-set-global-variables)
10228   (gnus-summary-next-article t (gnus-summary-article-subject)))
10229
10230 (defun gnus-summary-prev-unread-same-subject ()
10231   "Select previous unread article which has the same subject as current one."
10232   (interactive)
10233   (gnus-set-global-variables)
10234   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10235
10236 (defun gnus-summary-first-unread-article ()
10237   "Select the first unread article.
10238 Return nil if there are no unread articles."
10239   (interactive)
10240   (gnus-set-global-variables)
10241   (prog1
10242       (if (gnus-summary-first-subject t)
10243           (progn
10244             (gnus-summary-show-thread)
10245             (gnus-summary-first-subject t)
10246             (gnus-summary-display-article (gnus-summary-article-number))))
10247     (gnus-summary-position-point)))
10248
10249 (defun gnus-summary-best-unread-article ()
10250   "Select the unread article with the highest score."
10251   (interactive)
10252   (gnus-set-global-variables)
10253   (let ((best -1000000)
10254         (data gnus-newsgroup-data)
10255         article score)
10256     (while data
10257       (and (gnus-data-unread-p (car data))
10258            (> (setq score
10259                     (gnus-summary-article-score (gnus-data-number (car data))))
10260               best)
10261            (setq best score
10262                  article (gnus-data-number (car data))))
10263       (setq data (cdr data)))
10264     (if article
10265         (gnus-summary-goto-article article)
10266       (error "No unread articles"))
10267     (gnus-summary-position-point)))
10268
10269 (defun gnus-summary-last-subject ()
10270   "Go to the last displayed subject line in the group."
10271   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10272     (when article
10273       (gnus-summary-goto-subject article))))
10274
10275 (defun gnus-summary-goto-article (article &optional all-headers force)
10276   "Fetch ARTICLE and display it if it exists.
10277 If ALL-HEADERS is non-nil, no header lines are hidden."
10278   (interactive
10279    (list
10280     (string-to-int
10281      (completing-read
10282       "Article number: "
10283       (mapcar (lambda (number) (list (int-to-string number)))
10284               gnus-newsgroup-limit)))
10285     current-prefix-arg
10286     t))
10287   (prog1
10288       (if (gnus-summary-goto-subject article force)
10289           (gnus-summary-display-article article all-headers)
10290         (gnus-message 4 "Couldn't go to article %s" article) nil)
10291     (gnus-summary-position-point)))
10292
10293 (defun gnus-summary-goto-last-article ()
10294   "Go to the previously read article."
10295   (interactive)
10296   (prog1
10297       (and gnus-last-article
10298            (gnus-summary-goto-article gnus-last-article))
10299     (gnus-summary-position-point)))
10300
10301 (defun gnus-summary-pop-article (number)
10302   "Pop one article off the history and go to the previous.
10303 NUMBER articles will be popped off."
10304   (interactive "p")
10305   (let (to)
10306     (setq gnus-newsgroup-history
10307           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10308     (if to
10309         (gnus-summary-goto-article (car to))
10310       (error "Article history empty")))
10311   (gnus-summary-position-point))
10312
10313 ;; Summary commands and functions for limiting the summary buffer.
10314
10315 (defun gnus-summary-limit-to-articles (n)
10316   "Limit the summary buffer to the next N articles.
10317 If not given a prefix, use the process marked articles instead."
10318   (interactive "P")
10319   (gnus-set-global-variables)
10320   (prog1
10321       (let ((articles (gnus-summary-work-articles n)))
10322         (setq gnus-newsgroup-processable nil)
10323         (gnus-summary-limit articles))
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-pop-limit (&optional total)
10327   "Restore the previous limit.
10328 If given a prefix, remove all limits."
10329   (interactive "P")
10330   (gnus-set-global-variables)
10331   (when total 
10332     (setq gnus-newsgroup-limits
10333           (list (mapcar (lambda (h) (mail-header-number h))
10334                         gnus-newsgroup-headers))))
10335   (unless gnus-newsgroup-limits
10336     (error "No limit to pop"))
10337   (prog1
10338       (gnus-summary-limit nil 'pop)
10339     (gnus-summary-position-point)))
10340
10341 (defun gnus-summary-limit-to-subject (subject &optional header)
10342   "Limit the summary buffer to articles that have subjects that match a regexp."
10343   (interactive "sRegexp: ")
10344   (unless header
10345     (setq header "subject"))
10346   (when (not (equal "" subject))
10347     (prog1
10348         (let ((articles (gnus-summary-find-matching
10349                          (or header "subject") subject 'all)))
10350           (or articles (error "Found no matches for \"%s\"" subject))
10351           (gnus-summary-limit articles))
10352       (gnus-summary-position-point))))
10353
10354 (defun gnus-summary-limit-to-author (from)
10355   "Limit the summary buffer to articles that have authors that match a regexp."
10356   (interactive "sRegexp: ")
10357   (gnus-summary-limit-to-subject from "from"))
10358
10359 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10360 (make-obsolete
10361  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10362
10363 (defun gnus-summary-limit-to-unread (&optional all)
10364   "Limit the summary buffer to articles that are not marked as read.
10365 If ALL is non-nil, limit strictly to unread articles."
10366   (interactive "P")
10367   (if all
10368       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10369     (gnus-summary-limit-to-marks
10370      ;; Concat all the marks that say that an article is read and have
10371      ;; those removed.
10372      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10373            gnus-killed-mark gnus-kill-file-mark
10374            gnus-low-score-mark gnus-expirable-mark
10375            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10376      'reverse)))
10377
10378 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10379 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10380
10381 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10382   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10383 If REVERSE, limit the summary buffer to articles that are not marked
10384 with MARKS.  MARKS can either be a string of marks or a list of marks.
10385 Returns how many articles were removed."
10386   (interactive "sMarks: ")
10387   (gnus-set-global-variables)
10388   (prog1
10389       (let ((data gnus-newsgroup-data)
10390             (marks (if (listp marks) marks
10391                      (append marks nil))) ; Transform to list.
10392             articles)
10393         (while data
10394           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10395                  (memq (gnus-data-mark (car data)) marks))
10396                (setq articles (cons (gnus-data-number (car data)) articles)))
10397           (setq data (cdr data)))
10398         (gnus-summary-limit articles))
10399     (gnus-summary-position-point)))
10400
10401 (defun gnus-summary-limit-to-score (&optional score)
10402   "Limit to articles with score at or above SCORE."
10403   (interactive "P")
10404   (gnus-set-global-variables)
10405   (setq score (if score
10406                   (prefix-numeric-value score)
10407                 (or gnus-summary-default-score 0)))
10408   (let ((data gnus-newsgroup-data)
10409         articles)
10410     (while data
10411       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10412                 score)
10413         (push (gnus-data-number (car data)) articles))
10414       (setq data (cdr data)))
10415     (prog1
10416         (gnus-summary-limit articles)
10417       (gnus-summary-position-point))))
10418
10419 (defun gnus-summary-limit-include-dormant ()
10420   "Display all the hidden articles that are marked as dormant."
10421   (interactive)
10422   (gnus-set-global-variables)
10423   (or gnus-newsgroup-dormant
10424       (error "There are no dormant articles in this group"))
10425   (prog1
10426       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10427     (gnus-summary-position-point)))
10428
10429 (defun gnus-summary-limit-exclude-dormant ()
10430   "Hide all dormant articles."
10431   (interactive)
10432   (gnus-set-global-variables)
10433   (prog1
10434       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10435     (gnus-summary-position-point)))
10436
10437 (defun gnus-summary-limit-exclude-childless-dormant ()
10438   "Hide all dormant articles that have no children."
10439   (interactive)
10440   (gnus-set-global-variables)
10441   (let ((data (gnus-data-list t))
10442         articles d children)
10443     ;; Find all articles that are either not dormant or have
10444     ;; children.
10445     (while (setq d (pop data))
10446       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10447                 (and (setq children 
10448                            (gnus-article-children (gnus-data-number d)))
10449                      (let (found)
10450                        (while children
10451                          (when (memq (car children) articles)
10452                            (setq children nil
10453                                  found t))
10454                          (pop children))
10455                        found)))
10456         (push (gnus-data-number d) articles)))
10457     ;; Do the limiting.
10458     (prog1
10459         (gnus-summary-limit articles)
10460       (gnus-summary-position-point))))
10461
10462 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10463   "Mark all unread excluded articles as read.
10464 If ALL, mark even excluded ticked and dormants as read."
10465   (interactive "P")
10466   (let ((articles (gnus-sorted-complement
10467                    (sort
10468                     (mapcar (lambda (h) (mail-header-number h))
10469                             gnus-newsgroup-headers)
10470                     '<)
10471                    (sort gnus-newsgroup-limit '<)))
10472         article)
10473     (setq gnus-newsgroup-unreads nil)
10474     (if all
10475         (setq gnus-newsgroup-dormant nil
10476               gnus-newsgroup-marked nil
10477               gnus-newsgroup-reads
10478               (nconc
10479                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10480                gnus-newsgroup-reads))
10481       (while (setq article (pop articles))
10482         (unless (or (memq article gnus-newsgroup-dormant)
10483                     (memq article gnus-newsgroup-marked))
10484           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10485
10486 (defun gnus-summary-limit (articles &optional pop)
10487   (if pop
10488       ;; We pop the previous limit off the stack and use that.
10489       (setq articles (car gnus-newsgroup-limits)
10490             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10491     ;; We use the new limit, so we push the old limit on the stack.
10492     (setq gnus-newsgroup-limits
10493           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10494   ;; Set the limit.
10495   (setq gnus-newsgroup-limit articles)
10496   (let ((total (length gnus-newsgroup-data))
10497         (data (gnus-data-find-list (gnus-summary-article-number)))
10498         found)
10499     ;; This will do all the work of generating the new summary buffer
10500     ;; according to the new limit.
10501     (gnus-summary-prepare)
10502     ;; Hide any threads, possibly.
10503     (and gnus-show-threads
10504          gnus-thread-hide-subtree
10505          (gnus-summary-hide-all-threads))
10506     ;; Try to return to the article you were at, or one in the
10507     ;; neighborhood.
10508     (if data
10509         ;; We try to find some article after the current one.
10510         (while data
10511           (and (gnus-summary-goto-subject
10512                 (gnus-data-number (car data)) nil t)
10513                (setq data nil
10514                      found t))
10515           (setq data (cdr data))))
10516     (or found
10517         ;; If there is no data, that means that we were after the last
10518         ;; article.  The same goes when we can't find any articles
10519         ;; after the current one.
10520         (progn
10521           (goto-char (point-max))
10522           (gnus-summary-find-prev)))
10523     ;; We return how many articles were removed from the summary
10524     ;; buffer as a result of the new limit.
10525     (- total (length gnus-newsgroup-data))))
10526
10527 (defsubst gnus-cut-thread (thread)
10528   "Go forwards in the thread until we find an article that we want to display."
10529   (if (eq gnus-fetch-old-headers 'some)
10530       (while (and thread
10531                   (memq (mail-header-number (car thread)) 
10532                         gnus-newsgroup-ancient)
10533                   (<= (length (cdr thread)) 1))
10534         (setq thread (cadr thread)))
10535     (while (and thread
10536                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10537                 (= (length (cdr thread)) 1))
10538       (setq thread (cadr thread))))
10539   thread)
10540
10541 (defun gnus-cut-threads (threads)
10542   "Cut off all uninteresting articles from the beginning of threads."
10543   (when (or (eq gnus-fetch-old-headers 'some)
10544             (eq gnus-build-sparse-threads 'some)
10545             (eq gnus-build-sparse-threads 'more))
10546     (let ((th threads))
10547       (while th
10548         (setcar th (gnus-cut-thread (car th)))
10549         (setq th (cdr th)))))
10550   threads)
10551
10552 (defun gnus-summary-initial-limit (&optional show-if-empty)
10553   "Figure out what the initial limit is supposed to be on group entry.
10554 This entails weeding out unwanted dormants, low-scored articles,
10555 fetch-old-headers verbiage, and so on."
10556   ;; Most groups have nothing to remove.
10557   (if (or gnus-inhibit-limiting
10558           (and (null gnus-newsgroup-dormant)
10559                (not (eq gnus-fetch-old-headers 'some))
10560                (null gnus-summary-expunge-below)
10561                (not (eq gnus-build-sparse-threads 'some))
10562                (not (eq gnus-build-sparse-threads 'more))
10563                (null gnus-thread-expunge-below)
10564                (not gnus-use-nocem)))
10565       () ; Do nothing.
10566     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10567     (setq gnus-newsgroup-limit nil)
10568     (mapatoms
10569      (lambda (node)
10570        (unless (car (symbol-value node))
10571          ;; These threads have no parents -- they are roots.
10572          (let ((nodes (cdr (symbol-value node)))
10573                thread)
10574            (while nodes
10575              (if (and gnus-thread-expunge-below
10576                       (< (gnus-thread-total-score (car nodes))
10577                          gnus-thread-expunge-below))
10578                  (gnus-expunge-thread (pop nodes))
10579                (setq thread (pop nodes))
10580                (gnus-summary-limit-children thread))))))
10581      gnus-newsgroup-dependencies)
10582     ;; If this limitation resulted in an empty group, we might
10583     ;; pop the previous limit and use it instead.
10584     (when (and (not gnus-newsgroup-limit)
10585                show-if-empty)
10586       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10587     gnus-newsgroup-limit))
10588
10589 (defun gnus-summary-limit-children (thread)
10590   "Return 1 if this subthread is visible and 0 if it is not."
10591   ;; First we get the number of visible children to this thread.  This
10592   ;; is done by recursing down the thread using this function, so this
10593   ;; will really go down to a leaf article first, before slowly
10594   ;; working its way up towards the root.
10595   (when thread
10596     (let ((children
10597            (if (cdr thread)
10598                (apply '+ (mapcar 'gnus-summary-limit-children
10599                                  (cdr thread)))
10600              0))
10601           (number (mail-header-number (car thread)))
10602           score)
10603       (if (or
10604            ;; If this article is dormant and has absolutely no visible
10605            ;; children, then this article isn't visible.
10606            (and (memq number gnus-newsgroup-dormant)
10607                 (= children 0))
10608            ;; If this is a "fetch-old-headered" and there is only one
10609            ;; visible child (or less), then we don't want this article.
10610            (and (eq gnus-fetch-old-headers 'some)
10611                 (memq number gnus-newsgroup-ancient)
10612                 (zerop children))
10613            ;; If this is a sparsely inserted article with no children,
10614            ;; we don't want it.
10615            (and (eq gnus-build-sparse-threads 'some)
10616                 (memq number gnus-newsgroup-sparse)
10617                 (zerop children))
10618            ;; If we use expunging, and this article is really
10619            ;; low-scored, then we don't want this article.
10620            (when (and gnus-summary-expunge-below
10621                       (< (setq score
10622                                (or (cdr (assq number gnus-newsgroup-scored))
10623                                    gnus-summary-default-score))
10624                          gnus-summary-expunge-below))
10625              ;; We increase the expunge-tally here, but that has
10626              ;; nothing to do with the limits, really.
10627              (incf gnus-newsgroup-expunged-tally)
10628              ;; We also mark as read here, if that's wanted.
10629              (when (and gnus-summary-mark-below
10630                         (< score gnus-summary-mark-below))
10631                (setq gnus-newsgroup-unreads
10632                      (delq number gnus-newsgroup-unreads))
10633                (if gnus-newsgroup-auto-expire
10634                    (push number gnus-newsgroup-expirable)
10635                  (push (cons number gnus-low-score-mark)
10636                        gnus-newsgroup-reads)))
10637              t)
10638            (and gnus-use-nocem
10639                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10640           ;; Nope, invisible article.
10641           0
10642         ;; Ok, this article is to be visible, so we add it to the limit
10643         ;; and return 1.
10644         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10645         1))))
10646
10647 (defun gnus-expunge-thread (thread)
10648   "Mark all articles in THREAD as read."
10649   (let* ((number (mail-header-number (car thread))))
10650     (incf gnus-newsgroup-expunged-tally)
10651     ;; We also mark as read here, if that's wanted.
10652     (setq gnus-newsgroup-unreads
10653           (delq number gnus-newsgroup-unreads))
10654     (if gnus-newsgroup-auto-expire
10655         (push number gnus-newsgroup-expirable)
10656       (push (cons number gnus-low-score-mark)
10657             gnus-newsgroup-reads)))
10658   ;; Go recursively through all subthreads.
10659   (mapcar 'gnus-expunge-thread (cdr thread)))
10660
10661 ;; Summary article oriented commands
10662
10663 (defun gnus-summary-refer-parent-article (n)
10664   "Refer parent article N times.
10665 The difference between N and the number of articles fetched is returned."
10666   (interactive "p")
10667   (gnus-set-global-variables)
10668   (while
10669       (and
10670        (> n 0)
10671        (let* ((header (gnus-summary-article-header))
10672               (ref
10673                ;; If we try to find the parent of the currently
10674                ;; displayed article, then we take a look at the actual
10675                ;; References header, since this is slightly more
10676                ;; reliable than the References field we got from the
10677                ;; server.
10678                (if (and (eq (mail-header-number header)
10679                             (cdr gnus-article-current))
10680                         (equal gnus-newsgroup-name
10681                                (car gnus-article-current)))
10682                    (save-excursion
10683                      (set-buffer gnus-original-article-buffer)
10684                      (nnheader-narrow-to-headers)
10685                      (prog1
10686                          (mail-fetch-field "references")
10687                        (widen)))
10688                  ;; It's not the current article, so we take a bet on
10689                  ;; the value we got from the server.
10690                  (mail-header-references header))))
10691          (if (setq ref (or ref (mail-header-references header)))
10692              (or (gnus-summary-refer-article (gnus-parent-id ref))
10693                  (gnus-message 1 "Couldn't find parent"))
10694            (gnus-message 1 "No references in article %d"
10695                          (gnus-summary-article-number))
10696            nil)))
10697     (setq n (1- n)))
10698   (gnus-summary-position-point)
10699   n)
10700
10701 (defun gnus-summary-refer-references ()
10702   "Fetch all articles mentioned in the References header.
10703 Return how many articles were fetched."
10704   (interactive)
10705   (gnus-set-global-variables)
10706   (let ((ref (mail-header-references (gnus-summary-article-header)))
10707         (current (gnus-summary-article-number))
10708         (n 0))
10709     ;; For each Message-ID in the References header...
10710     (while (string-match "<[^>]*>" ref)
10711       (incf n)
10712       ;; ... fetch that article.
10713       (gnus-summary-refer-article
10714        (prog1 (match-string 0 ref)
10715          (setq ref (substring ref (match-end 0))))))
10716     (gnus-summary-goto-subject current)
10717     (gnus-summary-position-point)
10718     n))
10719
10720 (defun gnus-summary-refer-article (message-id)
10721   "Fetch an article specified by MESSAGE-ID."
10722   (interactive "sMessage-ID: ")
10723   (when (and (stringp message-id)
10724              (not (zerop (length message-id))))
10725     ;; Construct the correct Message-ID if necessary.
10726     ;; Suggested by tale@pawl.rpi.edu.
10727     (unless (string-match "^<" message-id)
10728       (setq message-id (concat "<" message-id)))
10729     (unless (string-match ">$" message-id)
10730       (setq message-id (concat message-id ">")))
10731     (let ((header (car (gnus-gethash (downcase message-id)
10732                                      gnus-newsgroup-dependencies))))
10733       (if header
10734           ;; The article is present in the buffer, to we just go to it.
10735           (gnus-summary-goto-article (mail-header-number header) nil t)
10736         ;; We fetch the article
10737         (let ((gnus-override-method gnus-refer-article-method)
10738               number)
10739           ;; Start the special refer-article method, if necessary.
10740           (when gnus-refer-article-method
10741             (gnus-check-server gnus-refer-article-method))
10742           ;; Fetch the header, and display the article.
10743           (if (setq number (gnus-summary-insert-subject message-id))
10744               (gnus-summary-select-article nil nil nil number)
10745             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10746
10747 (defun gnus-summary-enter-digest-group (&optional force)
10748   "Enter a digest group based on the current article."
10749   (interactive "P")
10750   (gnus-set-global-variables)
10751   (gnus-summary-select-article)
10752   (let ((name (format "%s-%d"
10753                       (gnus-group-prefixed-name
10754                        gnus-newsgroup-name (list 'nndoc ""))
10755                       gnus-current-article))
10756         (ogroup gnus-newsgroup-name)
10757         (case-fold-search t)
10758         (buf (current-buffer))
10759         dig)
10760     (save-excursion
10761       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10762       (insert-buffer-substring gnus-original-article-buffer)
10763       (narrow-to-region
10764        (goto-char (point-min))
10765        (or (search-forward "\n\n" nil t) (point)))
10766       (goto-char (point-min))
10767       (delete-matching-lines "^\\(Path\\):\\|^From ")
10768       (widen))
10769     (unwind-protect
10770         (if (gnus-group-read-ephemeral-group
10771              name `(nndoc ,name (nndoc-address
10772                                  ,(get-buffer dig))
10773                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10774             ;; Make all postings to this group go to the parent group.
10775             (nconc (gnus-info-params (gnus-get-info name))
10776                    (list (cons 'to-group ogroup)))
10777           ;; Couldn't select this doc group.
10778           (switch-to-buffer buf)
10779           (gnus-set-global-variables)
10780           (gnus-configure-windows 'summary)
10781           (gnus-message 3 "Article couldn't be entered?"))
10782       (kill-buffer dig))))
10783
10784 (defun gnus-summary-isearch-article (&optional regexp-p)
10785   "Do incremental search forward on the current article.
10786 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10787   (interactive "P")
10788   (gnus-set-global-variables)
10789   (gnus-summary-select-article)
10790   (gnus-configure-windows 'article)
10791   (gnus-eval-in-buffer-window
10792    gnus-article-buffer
10793    (goto-char (point-min))
10794    (isearch-forward regexp-p)))
10795
10796 (defun gnus-summary-search-article-forward (regexp &optional backward)
10797   "Search for an article containing REGEXP forward.
10798 If BACKWARD, search backward instead."
10799   (interactive
10800    (list (read-string
10801           (format "Search article %s (regexp%s): "
10802                   (if current-prefix-arg "backward" "forward")
10803                   (if gnus-last-search-regexp
10804                       (concat ", default " gnus-last-search-regexp)
10805                     "")))
10806          current-prefix-arg))
10807   (gnus-set-global-variables)
10808   (if (string-equal regexp "")
10809       (setq regexp (or gnus-last-search-regexp ""))
10810     (setq gnus-last-search-regexp regexp))
10811   (if (gnus-summary-search-article regexp backward)
10812       (gnus-article-set-window-start
10813        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10814     (error "Search failed: \"%s\"" regexp)))
10815
10816 (defun gnus-summary-search-article-backward (regexp)
10817   "Search for an article containing REGEXP backward."
10818   (interactive
10819    (list (read-string
10820           (format "Search article backward (regexp%s): "
10821                   (if gnus-last-search-regexp
10822                       (concat ", default " gnus-last-search-regexp)
10823                     "")))))
10824   (gnus-summary-search-article-forward regexp 'backward))
10825
10826 (defun gnus-summary-search-article (regexp &optional backward)
10827   "Search for an article containing REGEXP.
10828 Optional argument BACKWARD means do search for backward.
10829 gnus-select-article-hook is not called during the search."
10830   (let ((gnus-select-article-hook nil)  ;Disable hook.
10831         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10832         (re-search
10833          (if backward
10834              (function re-search-backward) (function re-search-forward)))
10835         (found nil)
10836         (last nil))
10837     ;; Hidden thread subtrees must be searched for ,too.
10838     (gnus-summary-show-all-threads)
10839     ;; First of all, search current article.
10840     ;; We don't want to read article again from NNTP server nor reset
10841     ;; current point.
10842     (gnus-summary-select-article)
10843     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10844     (setq last gnus-current-article)
10845     (gnus-eval-in-buffer-window
10846      gnus-article-buffer
10847      (save-restriction
10848        (widen)
10849        ;; Begin search from current point.
10850        (setq found (funcall re-search regexp nil t))))
10851     ;; Then search next articles.
10852     (while (and (not found)
10853                 (gnus-summary-display-article
10854                  (if backward (gnus-summary-find-prev)
10855                    (gnus-summary-find-next))))
10856       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10857       (gnus-eval-in-buffer-window
10858        gnus-article-buffer
10859        (save-restriction
10860          (widen)
10861          (goto-char (if backward (point-max) (point-min)))
10862          (setq found (funcall re-search regexp nil t)))))
10863     (message "")
10864     ;; Adjust article pointer.
10865     (or (eq last gnus-current-article)
10866         (setq gnus-last-article last))
10867     ;; Return T if found such article.
10868     found))
10869
10870 (defun gnus-summary-find-matching (header regexp &optional backward unread
10871                                           not-case-fold)
10872   "Return a list of all articles that match REGEXP on HEADER.
10873 The search stars on the current article and goes forwards unless
10874 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10875 If UNREAD is non-nil, only unread articles will
10876 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10877 in the comparisons."
10878   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10879                 (gnus-data-find-list
10880                  (gnus-summary-article-number) (gnus-data-list backward))))
10881         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10882         (case-fold-search (not not-case-fold))
10883         articles d)
10884     (or (fboundp (intern (concat "mail-header-" header)))
10885         (error "%s is not a valid header" header))
10886     (while data
10887       (setq d (car data))
10888       (and (or (not unread)             ; We want all articles...
10889                (gnus-data-unread-p d))  ; Or just unreads.
10890            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10891            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10892            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10893       (setq data (cdr data)))
10894     (nreverse articles)))
10895
10896 (defun gnus-summary-execute-command (header regexp command &optional backward)
10897   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10898 If HEADER is an empty string (or nil), the match is done on the entire
10899 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10900   (interactive
10901    (list (let ((completion-ignore-case t))
10902            (completing-read
10903             "Header name: "
10904             (mapcar (lambda (string) (list string))
10905                     '("Number" "Subject" "From" "Lines" "Date"
10906                       "Message-ID" "Xref" "References" "Body"))
10907             nil 'require-match))
10908          (read-string "Regexp: ")
10909          (read-key-sequence "Command: ")
10910          current-prefix-arg))
10911   (when (equal header "Body")
10912     (setq header ""))
10913   (gnus-set-global-variables)
10914   ;; Hidden thread subtrees must be searched as well.
10915   (gnus-summary-show-all-threads)
10916   ;; We don't want to change current point nor window configuration.
10917   (save-excursion
10918     (save-window-excursion
10919       (gnus-message 6 "Executing %s..." (key-description command))
10920       ;; We'd like to execute COMMAND interactively so as to give arguments.
10921       (gnus-execute header regexp
10922                     `(lambda () (call-interactively ',(key-binding command)))
10923                     backward)
10924       (gnus-message 6 "Executing %s...done" (key-description command)))))
10925
10926 (defun gnus-summary-beginning-of-article ()
10927   "Scroll the article back to the beginning."
10928   (interactive)
10929   (gnus-set-global-variables)
10930   (gnus-summary-select-article)
10931   (gnus-configure-windows 'article)
10932   (gnus-eval-in-buffer-window
10933    gnus-article-buffer
10934    (widen)
10935    (goto-char (point-min))
10936    (and gnus-break-pages (gnus-narrow-to-page))))
10937
10938 (defun gnus-summary-end-of-article ()
10939   "Scroll to the end of the article."
10940   (interactive)
10941   (gnus-set-global-variables)
10942   (gnus-summary-select-article)
10943   (gnus-configure-windows 'article)
10944   (gnus-eval-in-buffer-window
10945    gnus-article-buffer
10946    (widen)
10947    (goto-char (point-max))
10948    (recenter -3)
10949    (and gnus-break-pages (gnus-narrow-to-page))))
10950
10951 (defun gnus-summary-show-article (&optional arg)
10952   "Force re-fetching of the current article.
10953 If ARG (the prefix) is non-nil, show the raw article without any
10954 article massaging functions being run."
10955   (interactive "P")
10956   (gnus-set-global-variables)
10957   (if (not arg)
10958       ;; Select the article the normal way.
10959       (gnus-summary-select-article nil 'force)
10960     ;; Bind the article treatment functions to nil.
10961     (let ((gnus-have-all-headers t)
10962           gnus-article-display-hook
10963           gnus-article-prepare-hook
10964           gnus-visual)
10965       (gnus-summary-select-article nil 'force)))
10966 ;  (gnus-configure-windows 'article)
10967   (gnus-summary-position-point))
10968
10969 (defun gnus-summary-verbose-headers (&optional arg)
10970   "Toggle permanent full header display.
10971 If ARG is a positive number, turn header display on.
10972 If ARG is a negative number, turn header display off."
10973   (interactive "P")
10974   (gnus-set-global-variables)
10975   (gnus-summary-toggle-header arg)
10976   (setq gnus-show-all-headers
10977         (cond ((or (not (numberp arg))
10978                    (zerop arg))
10979                (not gnus-show-all-headers))
10980               ((natnump arg)
10981                t))))
10982
10983 (defun gnus-summary-toggle-header (&optional arg)
10984   "Show the headers if they are hidden, or hide them if they are shown.
10985 If ARG is a positive number, show the entire header.
10986 If ARG is a negative number, hide the unwanted header lines."
10987   (interactive "P")
10988   (gnus-set-global-variables)
10989   (save-excursion
10990     (set-buffer gnus-article-buffer)
10991     (let* ((buffer-read-only nil)
10992            (inhibit-point-motion-hooks t)
10993            (hidden (text-property-any
10994                     (goto-char (point-min)) (search-forward "\n\n")
10995                     'invisible t))
10996            e)
10997       (goto-char (point-min))
10998       (when (search-forward "\n\n" nil t)
10999         (delete-region (point-min) (1- (point))))
11000       (goto-char (point-min))
11001       (save-excursion
11002         (set-buffer gnus-original-article-buffer)
11003         (goto-char (point-min))
11004         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11005       (insert-buffer-substring gnus-original-article-buffer 1 e)
11006       (let ((gnus-inhibit-hiding t))
11007         (run-hooks 'gnus-article-display-hook))
11008       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11009           (gnus-article-hide-headers)))))
11010
11011 (defun gnus-summary-show-all-headers ()
11012   "Make all header lines visible."
11013   (interactive)
11014   (gnus-set-global-variables)
11015   (gnus-article-show-all-headers))
11016
11017 (defun gnus-summary-toggle-mime (&optional arg)
11018   "Toggle MIME processing.
11019 If ARG is a positive number, turn MIME processing on."
11020   (interactive "P")
11021   (gnus-set-global-variables)
11022   (setq gnus-show-mime
11023         (if (null arg) (not gnus-show-mime)
11024           (> (prefix-numeric-value arg) 0)))
11025   (gnus-summary-select-article t 'force))
11026
11027 (defun gnus-summary-caesar-message (&optional arg)
11028   "Caesar rotate the current article by 13.
11029 The numerical prefix specifies how manu places to rotate each letter
11030 forward."
11031   (interactive "P")
11032   (gnus-set-global-variables)
11033   (gnus-summary-select-article)
11034   (let ((mail-header-separator ""))
11035     (gnus-eval-in-buffer-window
11036      gnus-article-buffer
11037      (save-restriction
11038        (widen)
11039        (let ((start (window-start)))
11040          (news-caesar-buffer-body arg)
11041          (set-window-start (get-buffer-window (current-buffer)) start))))))
11042
11043 (defun gnus-summary-stop-page-breaking ()
11044   "Stop page breaking in the current article."
11045   (interactive)
11046   (gnus-set-global-variables)
11047   (gnus-summary-select-article)
11048   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11049
11050 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11051   "Move the current article to a different newsgroup.
11052 If N is a positive number, move the N next articles.
11053 If N is a negative number, move the N previous articles.
11054 If N is nil and any articles have been marked with the process mark,
11055 move those articles instead.
11056 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11057 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11058 re-spool using this method.
11059
11060 For this function to work, both the current newsgroup and the
11061 newsgroup that you want to move to have to support the `request-move'
11062 and `request-accept' functions."
11063   (interactive "P")
11064   (unless action (setq action 'move))
11065   (gnus-set-global-variables)
11066   ;; Check whether the source group supports the required functions.
11067   (cond ((and (eq action 'move)
11068               (not (gnus-check-backend-function
11069                     'request-move-article gnus-newsgroup-name)))
11070          (error "The current group does not support article moving"))
11071         ((and (eq action 'crosspost)
11072               (not (gnus-check-backend-function
11073                     'request-replace-article gnus-newsgroup-name)))
11074          (error "The current group does not support article editing")))
11075   (let ((articles (gnus-summary-work-articles n))
11076         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11077         (names '((move "move" "Moving")
11078                  (copy "copy" "Copying")
11079                  (crosspost "crosspost" "Crossposting")))
11080         (copy-buf (save-excursion
11081                     (nnheader-set-temp-buffer " *copy article*")))
11082         art-group to-method new-xref article)
11083     (unless (assq action names)
11084       (error "Unknown action %s" action))
11085     ;; Read the newsgroup name.
11086     (when (and (not to-newsgroup)
11087                (not select-method))
11088       (setq to-newsgroup
11089             (gnus-read-move-group-name
11090              (cadr (assq action names))
11091              gnus-current-move-group articles prefix))
11092       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11093     (setq to-method (if select-method (list select-method "")
11094                       (gnus-find-method-for-group to-newsgroup)))
11095     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11096     ;;(error "Can't %s to the same group you're already in" action))
11097     ;; Check the method we are to move this article to...
11098     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11099         (error "%s does not support article copying" (car to-method)))
11100     (or (gnus-check-server to-method)
11101         (error "Can't open server %s" (car to-method)))
11102     (gnus-message 6 "%s to %s: %s..."
11103                   (caddr (assq action names))
11104                   (or select-method to-newsgroup) articles)
11105     (while articles
11106       (setq article (pop articles))
11107       (setq
11108        art-group
11109        (cond
11110         ;; Move the article.
11111         ((eq action 'move)
11112          (gnus-request-move-article
11113           article                       ; Article to move
11114           gnus-newsgroup-name           ; From newsgrouo
11115           (nth 1 (gnus-find-method-for-group
11116                   gnus-newsgroup-name)) ; Server
11117           (list 'gnus-request-accept-article
11118                 (if select-method
11119                     (list 'quote select-method)
11120                   to-newsgroup)
11121                 (not articles))         ; Accept form
11122           (not articles)))              ; Only save nov last time
11123         ;; Copy the article.
11124         ((eq action 'copy)
11125          (save-excursion
11126            (set-buffer copy-buf)
11127            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11128            (gnus-request-accept-article
11129             (if select-method select-method to-newsgroup)
11130             (not articles))))
11131         ;; Crosspost the article.
11132         ((eq action 'crosspost)
11133          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11134            (setq new-xref (concat gnus-newsgroup-name ":" article))
11135            (if (and xref (not (string= xref "")))
11136                (progn
11137                  (when (string-match "^Xref: " xref)
11138                    (setq xref (substring xref (match-end 0))))
11139                  (setq new-xref (concat xref " " new-xref)))
11140              (setq new-xref (concat (system-name) " " new-xref)))
11141            (save-excursion
11142              (set-buffer copy-buf)
11143              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11144              (nnheader-replace-header "xref" new-xref)
11145              (gnus-request-accept-article
11146               (if select-method select-method to-newsgroup)
11147               (not articles)))))))
11148       (if (not art-group)
11149           (gnus-message 1 "Couldn't %s article %s"
11150                         (cadr (assq action names)) article)
11151         (let* ((entry
11152                 (or
11153                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11154                  (gnus-gethash
11155                   (gnus-group-prefixed-name
11156                    (car art-group)
11157                    (if select-method (list select-method "")
11158                      (gnus-find-method-for-group to-newsgroup)))
11159                   gnus-newsrc-hashtb)))
11160                (info (nth 2 entry)))
11161           ;; Update the group that has been moved to.
11162           (when (and info
11163                      (memq action '(move copy)))
11164             (unless (memq article gnus-newsgroup-unreads)
11165               (gnus-info-set-read
11166                info (gnus-add-to-range (gnus-info-read info)
11167                                        (list (cdr art-group)))))
11168
11169             ;; Copy any marks over to the new group.
11170             (let ((marks gnus-article-mark-lists)
11171                   (to-article (cdr art-group)))
11172
11173               ;; See whether the article is to be put in the cache.
11174               (when gnus-use-cache
11175                 (gnus-cache-possibly-enter-article
11176                  (gnus-info-group info) to-article
11177                  (let ((header (copy-sequence
11178                                 (gnus-summary-article-header article))))
11179                    (mail-header-set-number header to-article)
11180                    header)
11181                  (memq article gnus-newsgroup-marked)
11182                  (memq article gnus-newsgroup-dormant)
11183                  (memq article gnus-newsgroup-unreads)))
11184
11185               (while marks
11186                 (when (memq article (symbol-value
11187                                      (intern (format "gnus-newsgroup-%s"
11188                                                      (caar marks)))))
11189                   (gnus-add-marked-articles
11190                    (gnus-info-group info) (cdar marks)
11191                    (list to-article) info))
11192                 (setq marks (cdr marks)))))
11193
11194           ;; Update the Xref header in this article to point to
11195           ;; the new crossposted article we have just created.
11196           (when (eq action 'crosspost)
11197             (save-excursion
11198               (set-buffer copy-buf)
11199               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11200               (nnheader-replace-header
11201                "xref" (concat new-xref " " (gnus-group-prefixed-name
11202                                             (car art-group) to-method)
11203                               ":" (cdr art-group)))
11204               (gnus-request-replace-article
11205                article gnus-newsgroup-name (current-buffer)))))
11206
11207         (gnus-summary-goto-subject article)
11208         (when (eq action 'move)
11209           (gnus-summary-mark-article article gnus-canceled-mark)))
11210       (gnus-summary-remove-process-mark article))
11211     (gnus-kill-buffer copy-buf)
11212     (gnus-summary-position-point)
11213     (gnus-set-mode-line 'summary)))
11214
11215 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11216   "Move the current article to a different newsgroup.
11217 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11218 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11219 re-spool using this method."
11220   (interactive "P")
11221   (gnus-summary-move-article n nil select-method 'copy))
11222
11223 (defun gnus-summary-crosspost-article (&optional n)
11224   "Crosspost the current article to some other group."
11225   (interactive "P")
11226   (gnus-summary-move-article n nil nil 'crosspost))
11227
11228 (defun gnus-summary-respool-article (&optional n respool-method)
11229   "Respool the current article.
11230 The article will be squeezed through the mail spooling process again,
11231 which means that it will be put in some mail newsgroup or other
11232 depending on `nnmail-split-methods'.
11233 If N is a positive number, respool the N next articles.
11234 If N is a negative number, respool the N previous articles.
11235 If N is nil and any articles have been marked with the process mark,
11236 respool those articles instead.
11237
11238 Respooling can be done both from mail groups and \"real\" newsgroups.
11239 In the former case, the articles in question will be moved from the
11240 current group into whatever groups they are destined to.  In the
11241 latter case, they will be copied into the relevant groups."
11242   (interactive "P")
11243   (gnus-set-global-variables)
11244   (let ((respool-methods (gnus-methods-using 'respool))
11245         (methname
11246          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11247     (or respool-method
11248         (setq respool-method
11249               (completing-read
11250                "What method do you want to use when respooling? "
11251                respool-methods nil t methname)))
11252     (or (string= respool-method "")
11253         (if (assoc (symbol-name
11254                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11255                    respool-methods)
11256             (gnus-summary-move-article n nil (intern respool-method))
11257           (gnus-summary-copy-article n nil (intern respool-method))))))
11258
11259 (defun gnus-summary-import-article (file)
11260   "Import a random file into a mail newsgroup."
11261   (interactive "fImport file: ")
11262   (gnus-set-global-variables)
11263   (let ((group gnus-newsgroup-name)
11264         (now (current-time))
11265         atts lines)
11266     (or (gnus-check-backend-function 'request-accept-article group)
11267         (error "%s does not support article importing" group))
11268     (or (file-readable-p file)
11269         (not (file-regular-p file))
11270         (error "Can't read %s" file))
11271     (save-excursion
11272       (set-buffer (get-buffer-create " *import file*"))
11273       (buffer-disable-undo (current-buffer))
11274       (erase-buffer)
11275       (insert-file-contents file)
11276       (goto-char (point-min))
11277       (unless (nnheader-article-p)
11278         ;; This doesn't look like an article, so we fudge some headers.
11279         (setq atts (file-attributes file)
11280               lines (count-lines (point-min) (point-max)))
11281         (insert "From: " (read-string "From: ") "\n"
11282                 "Subject: " (read-string "Subject: ") "\n"
11283                 "Date: " (timezone-make-date-arpa-standard
11284                           (current-time-string (nth 5 atts))
11285                           (current-time-zone now)
11286                           (current-time-zone now)) "\n"
11287                 "Message-ID: " (gnus-inews-message-id) "\n"
11288                 "Lines: " (int-to-string lines) "\n"
11289                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11290       (gnus-request-accept-article group t)
11291       (kill-buffer (current-buffer)))))
11292
11293 (defun gnus-summary-expire-articles ()
11294   "Expire all articles that are marked as expirable in the current group."
11295   (interactive)
11296   (gnus-set-global-variables)
11297   (when (gnus-check-backend-function
11298          'request-expire-articles gnus-newsgroup-name)
11299     ;; This backend supports expiry.
11300     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11301            (expirable (if total
11302                           (gnus-list-of-read-articles gnus-newsgroup-name)
11303                         (setq gnus-newsgroup-expirable
11304                               (sort gnus-newsgroup-expirable '<))))
11305            (expiry-wait (gnus-group-get-parameter
11306                          gnus-newsgroup-name 'expiry-wait))
11307            es)
11308       (when expirable
11309         ;; There are expirable articles in this group, so we run them
11310         ;; through the expiry process.
11311         (gnus-message 6 "Expiring articles...")
11312         ;; The list of articles that weren't expired is returned.
11313         (if expiry-wait
11314             (let ((nnmail-expiry-wait-function nil)
11315                   (nnmail-expiry-wait expiry-wait))
11316               (setq es (gnus-request-expire-articles
11317                         expirable gnus-newsgroup-name)))
11318           (setq es (gnus-request-expire-articles
11319                     expirable gnus-newsgroup-name)))
11320         (or total (setq gnus-newsgroup-expirable es))
11321         ;; We go through the old list of expirable, and mark all
11322         ;; really expired articles as nonexistent.
11323         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11324           (let ((gnus-use-cache nil))
11325             (while expirable
11326               (unless (memq (car expirable) es)
11327                 (when (gnus-data-find (car expirable))
11328                   (gnus-summary-mark-article
11329                    (car expirable) gnus-canceled-mark)))
11330               (setq expirable (cdr expirable)))))
11331         (gnus-message 6 "Expiring articles...done")))))
11332
11333 (defun gnus-summary-expire-articles-now ()
11334   "Expunge all expirable articles in the current group.
11335 This means that *all* articles that are marked as expirable will be
11336 deleted forever, right now."
11337   (interactive)
11338   (gnus-set-global-variables)
11339   (or gnus-expert-user
11340       (gnus-y-or-n-p
11341        "Are you really, really, really sure you want to expunge? ")
11342       (error "Phew!"))
11343   (let ((nnmail-expiry-wait 'immediate)
11344         (nnmail-expiry-wait-function nil))
11345     (gnus-summary-expire-articles)))
11346
11347 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11348 (defun gnus-summary-delete-article (&optional n)
11349   "Delete the N next (mail) articles.
11350 This command actually deletes articles.  This is not a marking
11351 command.  The article will disappear forever from your life, never to
11352 return.
11353 If N is negative, delete backwards.
11354 If N is nil and articles have been marked with the process mark,
11355 delete these instead."
11356   (interactive "P")
11357   (gnus-set-global-variables)
11358   (or (gnus-check-backend-function 'request-expire-articles
11359                                    gnus-newsgroup-name)
11360       (error "The current newsgroup does not support article deletion."))
11361   ;; Compute the list of articles to delete.
11362   (let ((articles (gnus-summary-work-articles n))
11363         not-deleted)
11364     (if (and gnus-novice-user
11365              (not (gnus-y-or-n-p
11366                    (format "Do you really want to delete %s forever? "
11367                            (if (> (length articles) 1) "these articles"
11368                              "this article")))))
11369         ()
11370       ;; Delete the articles.
11371       (setq not-deleted (gnus-request-expire-articles
11372                          articles gnus-newsgroup-name 'force))
11373       (while articles
11374         (gnus-summary-remove-process-mark (car articles))
11375         ;; The backend might not have been able to delete the article
11376         ;; after all.
11377         (or (memq (car articles) not-deleted)
11378             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11379         (setq articles (cdr articles))))
11380     (gnus-summary-position-point)
11381     (gnus-set-mode-line 'summary)
11382     not-deleted))
11383
11384 (defun gnus-summary-edit-article (&optional force)
11385   "Enter into a buffer and edit the current article.
11386 This will have permanent effect only in mail groups.
11387 If FORCE is non-nil, allow editing of articles even in read-only
11388 groups."
11389   (interactive "P")
11390   (save-excursion
11391     (set-buffer gnus-summary-buffer)
11392     (gnus-set-global-variables)
11393     (when (and (not force)
11394                (gnus-group-read-only-p))
11395       (error "The current newsgroup does not support article editing."))
11396     (gnus-summary-select-article t nil t)
11397     (gnus-configure-windows 'article)
11398     (select-window (get-buffer-window gnus-article-buffer))
11399     (gnus-message 6 "C-c C-c to end edits")
11400     (setq buffer-read-only nil)
11401     (text-mode)
11402     (use-local-map (copy-keymap (current-local-map)))
11403     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11404     (buffer-enable-undo)
11405     (widen)
11406     (goto-char (point-min))
11407     (search-forward "\n\n" nil t)))
11408
11409 (defun gnus-summary-edit-article-done ()
11410   "Make edits to the current article permanent."
11411   (interactive)
11412   (if (gnus-group-read-only-p)
11413       (progn
11414         (gnus-summary-edit-article-postpone)
11415         (gnus-message
11416          1 "The current newsgroup does not support article editing.")
11417         (ding))
11418     (let ((buf (format "%s" (buffer-string))))
11419       (erase-buffer)
11420       (insert buf)
11421       (if (not (gnus-request-replace-article
11422                 (cdr gnus-article-current) (car gnus-article-current)
11423                 (current-buffer)))
11424           (error "Couldn't replace article.")
11425         (gnus-article-mode)
11426         (use-local-map gnus-article-mode-map)
11427         (setq buffer-read-only t)
11428         (buffer-disable-undo (current-buffer))
11429         (gnus-configure-windows 'summary)
11430         (gnus-summary-update-article (cdr gnus-article-current))
11431         (when gnus-use-cache
11432           (gnus-cache-update-article 
11433            (cdr gnus-article-current) (car gnus-article-current))))
11434       (run-hooks 'gnus-article-display-hook)
11435       (and (gnus-visual-p 'summary-highlight 'highlight)
11436            (run-hooks 'gnus-visual-mark-article-hook)))))
11437
11438 (defun gnus-summary-edit-article-postpone ()
11439   "Postpone changes to the current article."
11440   (interactive)
11441   (gnus-article-mode)
11442   (use-local-map gnus-article-mode-map)
11443   (setq buffer-read-only t)
11444   (buffer-disable-undo (current-buffer))
11445   (gnus-configure-windows 'summary)
11446   (and (gnus-visual-p 'summary-highlight 'highlight)
11447        (run-hooks 'gnus-visual-mark-article-hook)))
11448
11449 (defun gnus-summary-respool-query ()
11450   "Query where the respool algorithm would put this article."
11451   (interactive)
11452   (gnus-set-global-variables)
11453   (gnus-summary-select-article)
11454   (save-excursion
11455     (set-buffer gnus-article-buffer)
11456     (save-restriction
11457       (goto-char (point-min))
11458       (search-forward "\n\n")
11459       (narrow-to-region (point-min) (point))
11460       (pp-eval-expression
11461        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11462
11463 ;; Summary score commands.
11464
11465 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11466
11467 (defun gnus-summary-raise-score (n)
11468   "Raise the score of the current article by N."
11469   (interactive "p")
11470   (gnus-set-global-variables)
11471   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11472
11473 (defun gnus-summary-set-score (n)
11474   "Set the score of the current article to N."
11475   (interactive "p")
11476   (gnus-set-global-variables)
11477   (save-excursion
11478     (gnus-summary-show-thread)
11479     (let ((buffer-read-only nil))
11480       ;; Set score.
11481       (gnus-summary-update-mark
11482        (if (= n (or gnus-summary-default-score 0)) ? 
11483          (if (< n (or gnus-summary-default-score 0))
11484              gnus-score-below-mark gnus-score-over-mark)) 'score))
11485     (let* ((article (gnus-summary-article-number))
11486            (score (assq article gnus-newsgroup-scored)))
11487       (if score (setcdr score n)
11488         (setq gnus-newsgroup-scored
11489               (cons (cons article n) gnus-newsgroup-scored))))
11490     (gnus-summary-update-line)))
11491
11492 (defun gnus-summary-current-score ()
11493   "Return the score of the current article."
11494   (interactive)
11495   (gnus-set-global-variables)
11496   (message "%s" (gnus-summary-article-score)))
11497
11498 ;; Summary marking commands.
11499
11500 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11501   "Mark articles which has the same subject as read, and then select the next.
11502 If UNMARK is positive, remove any kind of mark.
11503 If UNMARK is negative, tick articles."
11504   (interactive "P")
11505   (gnus-set-global-variables)
11506   (if unmark
11507       (setq unmark (prefix-numeric-value unmark)))
11508   (let ((count
11509          (gnus-summary-mark-same-subject
11510           (gnus-summary-article-subject) unmark)))
11511     ;; Select next unread article.  If auto-select-same mode, should
11512     ;; select the first unread article.
11513     (gnus-summary-next-article t (and gnus-auto-select-same
11514                                       (gnus-summary-article-subject)))
11515     (gnus-message 7 "%d article%s marked as %s"
11516                   count (if (= count 1) " is" "s are")
11517                   (if unmark "unread" "read"))))
11518
11519 (defun gnus-summary-kill-same-subject (&optional unmark)
11520   "Mark articles which has the same subject as read.
11521 If UNMARK is positive, remove any kind of mark.
11522 If UNMARK is negative, tick articles."
11523   (interactive "P")
11524   (gnus-set-global-variables)
11525   (if unmark
11526       (setq unmark (prefix-numeric-value unmark)))
11527   (let ((count
11528          (gnus-summary-mark-same-subject
11529           (gnus-summary-article-subject) unmark)))
11530     ;; If marked as read, go to next unread subject.
11531     (if (null unmark)
11532         ;; Go to next unread subject.
11533         (gnus-summary-next-subject 1 t))
11534     (gnus-message 7 "%d articles are marked as %s"
11535                   count (if unmark "unread" "read"))))
11536
11537 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11538   "Mark articles with same SUBJECT as read, and return marked number.
11539 If optional argument UNMARK is positive, remove any kinds of marks.
11540 If optional argument UNMARK is negative, mark articles as unread instead."
11541   (let ((count 1))
11542     (save-excursion
11543       (cond
11544        ((null unmark)                   ; Mark as read.
11545         (while (and
11546                 (progn
11547                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11548                   (gnus-summary-show-thread) t)
11549                 (gnus-summary-find-subject subject))
11550           (setq count (1+ count))))
11551        ((> unmark 0)                    ; Tick.
11552         (while (and
11553                 (progn
11554                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11555                   (gnus-summary-show-thread) t)
11556                 (gnus-summary-find-subject subject))
11557           (setq count (1+ count))))
11558        (t                               ; Mark as unread.
11559         (while (and
11560                 (progn
11561                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11562                   (gnus-summary-show-thread) t)
11563                 (gnus-summary-find-subject subject))
11564           (setq count (1+ count)))))
11565       (gnus-set-mode-line 'summary)
11566       ;; Return the number of marked articles.
11567       count)))
11568
11569 (defun gnus-summary-mark-as-processable (n &optional unmark)
11570   "Set the process mark on the next N articles.
11571 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11572 the process mark instead.  The difference between N and the actual
11573 number of articles marked is returned."
11574   (interactive "p")
11575   (gnus-set-global-variables)
11576   (let ((backward (< n 0))
11577         (n (abs n)))
11578     (while (and
11579             (> n 0)
11580             (if unmark
11581                 (gnus-summary-remove-process-mark
11582                  (gnus-summary-article-number))
11583               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11584             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11585       (setq n (1- n)))
11586     (if (/= 0 n) (gnus-message 7 "No more articles"))
11587     (gnus-summary-recenter)
11588     (gnus-summary-position-point)
11589     n))
11590
11591 (defun gnus-summary-unmark-as-processable (n)
11592   "Remove the process mark from the next N articles.
11593 If N is negative, mark backward instead.  The difference between N and
11594 the actual number of articles marked is returned."
11595   (interactive "p")
11596   (gnus-set-global-variables)
11597   (gnus-summary-mark-as-processable n t))
11598
11599 (defun gnus-summary-unmark-all-processable ()
11600   "Remove the process mark from all articles."
11601   (interactive)
11602   (gnus-set-global-variables)
11603   (save-excursion
11604     (while gnus-newsgroup-processable
11605       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11606   (gnus-summary-position-point))
11607
11608 (defun gnus-summary-mark-as-expirable (n)
11609   "Mark N articles forward as expirable.
11610 If N is negative, mark backward instead.  The difference between N and
11611 the actual number of articles marked is returned."
11612   (interactive "p")
11613   (gnus-set-global-variables)
11614   (gnus-summary-mark-forward n gnus-expirable-mark))
11615
11616 (defun gnus-summary-mark-article-as-replied (article)
11617   "Mark ARTICLE replied and update the summary line."
11618   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11619   (let ((buffer-read-only nil))
11620     (when (gnus-summary-goto-subject article)
11621       (gnus-summary-update-secondary-mark article))))
11622
11623 (defun gnus-summary-set-bookmark (article)
11624   "Set a bookmark in current article."
11625   (interactive (list (gnus-summary-article-number)))
11626   (gnus-set-global-variables)
11627   (if (or (not (get-buffer gnus-article-buffer))
11628           (not gnus-current-article)
11629           (not gnus-article-current)
11630           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11631       (error "No current article selected"))
11632   ;; Remove old bookmark, if one exists.
11633   (let ((old (assq article gnus-newsgroup-bookmarks)))
11634     (if old (setq gnus-newsgroup-bookmarks
11635                   (delq old gnus-newsgroup-bookmarks))))
11636   ;; Set the new bookmark, which is on the form
11637   ;; (article-number . line-number-in-body).
11638   (setq gnus-newsgroup-bookmarks
11639         (cons
11640          (cons article
11641                (save-excursion
11642                  (set-buffer gnus-article-buffer)
11643                  (count-lines
11644                   (min (point)
11645                        (save-excursion
11646                          (goto-char (point-min))
11647                          (search-forward "\n\n" nil t)
11648                          (point)))
11649                   (point))))
11650          gnus-newsgroup-bookmarks))
11651   (gnus-message 6 "A bookmark has been added to the current article."))
11652
11653 (defun gnus-summary-remove-bookmark (article)
11654   "Remove the bookmark from the current article."
11655   (interactive (list (gnus-summary-article-number)))
11656   (gnus-set-global-variables)
11657   ;; Remove old bookmark, if one exists.
11658   (let ((old (assq article gnus-newsgroup-bookmarks)))
11659     (if old
11660         (progn
11661           (setq gnus-newsgroup-bookmarks
11662                 (delq old gnus-newsgroup-bookmarks))
11663           (gnus-message 6 "Removed bookmark."))
11664       (gnus-message 6 "No bookmark in current article."))))
11665
11666 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11667 (defun gnus-summary-mark-as-dormant (n)
11668   "Mark N articles forward as dormant.
11669 If N is negative, mark backward instead.  The difference between N and
11670 the actual number of articles marked is returned."
11671   (interactive "p")
11672   (gnus-set-global-variables)
11673   (gnus-summary-mark-forward n gnus-dormant-mark))
11674
11675 (defun gnus-summary-set-process-mark (article)
11676   "Set the process mark on ARTICLE and update the summary line."
11677   (setq gnus-newsgroup-processable
11678         (cons article
11679               (delq article gnus-newsgroup-processable)))
11680   (when (gnus-summary-goto-subject article)
11681     (gnus-summary-show-thread)
11682     (gnus-summary-update-secondary-mark article)))
11683
11684 (defun gnus-summary-remove-process-mark (article)
11685   "Remove the process mark from ARTICLE and update the summary line."
11686   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11687   (when (gnus-summary-goto-subject article)
11688     (gnus-summary-show-thread)
11689     (gnus-summary-update-secondary-mark article)))
11690
11691 (defun gnus-summary-set-saved-mark (article)
11692   "Set the process mark on ARTICLE and update the summary line."
11693   (push article gnus-newsgroup-saved)
11694   (when (gnus-summary-goto-subject article)
11695     (gnus-summary-update-secondary-mark article)))
11696
11697 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11698   "Mark N articles as read forwards.
11699 If N is negative, mark backwards instead.
11700 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11701 marked as unread.
11702 The difference between N and the actual number of articles marked is
11703 returned."
11704   (interactive "p")
11705   (gnus-set-global-variables)
11706   (let ((backward (< n 0))
11707         (gnus-summary-goto-unread
11708          (and gnus-summary-goto-unread
11709               (not (eq gnus-summary-goto-unread 'never))
11710               (not (memq mark (list gnus-unread-mark
11711                                     gnus-ticked-mark gnus-dormant-mark)))))
11712         (n (abs n))
11713         (mark (or mark gnus-del-mark)))
11714     (while (and (> n 0)
11715                 (gnus-summary-mark-article nil mark no-expire)
11716                 (zerop (gnus-summary-next-subject
11717                         (if backward -1 1)
11718                         (and gnus-summary-goto-unread
11719                              (not (eq gnus-summary-goto-unread 'never)))
11720                         t)))
11721       (setq n (1- n)))
11722     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11723     (gnus-summary-recenter)
11724     (gnus-summary-position-point)
11725     (gnus-set-mode-line 'summary)
11726     n))
11727
11728 (defun gnus-summary-mark-article-as-read (mark)
11729   "Mark the current article quickly as read with MARK."
11730   (let ((article (gnus-summary-article-number)))
11731     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11732     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11733     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11734     (setq gnus-newsgroup-reads
11735           (cons (cons article mark) gnus-newsgroup-reads))
11736     ;; Possibly remove from cache, if that is used.
11737     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11738     ;; Allow the backend to change the mark.
11739     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11740     ;; Check for auto-expiry.
11741     (when (and gnus-newsgroup-auto-expire
11742                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11743                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11744                    (= mark gnus-ancient-mark)
11745                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11746       (setq mark gnus-expirable-mark)
11747       (push article gnus-newsgroup-expirable))
11748     ;; Set the mark in the buffer.
11749     (gnus-summary-update-mark mark 'unread)
11750     t))
11751
11752 (defun gnus-summary-mark-article-as-unread (mark)
11753   "Mark the current article quickly as unread with MARK."
11754   (let ((article (gnus-summary-article-number)))
11755     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11756     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11757     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11758     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11759     (cond ((= mark gnus-ticked-mark)
11760            (push article gnus-newsgroup-marked))
11761           ((= mark gnus-dormant-mark)
11762            (push article gnus-newsgroup-dormant))
11763           (t
11764            (push article gnus-newsgroup-unreads)))
11765     (setq gnus-newsgroup-reads
11766           (delq (assq article gnus-newsgroup-reads)
11767                 gnus-newsgroup-reads))
11768
11769     ;; See whether the article is to be put in the cache.
11770     (and gnus-use-cache
11771          (vectorp (gnus-summary-article-header article))
11772          (save-excursion
11773            (gnus-cache-possibly-enter-article
11774             gnus-newsgroup-name article
11775             (gnus-summary-article-header article)
11776             (= mark gnus-ticked-mark)
11777             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11778
11779     ;; Fix the mark.
11780     (gnus-summary-update-mark mark 'unread)
11781     t))
11782
11783 (defun gnus-summary-mark-article (&optional article mark no-expire)
11784   "Mark ARTICLE with MARK.  MARK can be any character.
11785 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11786 `??' (dormant) and `?E' (expirable).
11787 If MARK is nil, then the default character `?D' is used.
11788 If ARTICLE is nil, then the article on the current line will be
11789 marked."
11790   ;; The mark might be a string.
11791   (and (stringp mark)
11792        (setq mark (aref mark 0)))
11793   ;; If no mark is given, then we check auto-expiring.
11794   (and (not no-expire)
11795        gnus-newsgroup-auto-expire
11796        (or (not mark)
11797            (and (numberp mark)
11798                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11799                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11800                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11801        (setq mark gnus-expirable-mark))
11802   (let* ((mark (or mark gnus-del-mark))
11803          (article (or article (gnus-summary-article-number))))
11804     (or article (error "No article on current line"))
11805     (if (or (= mark gnus-unread-mark)
11806             (= mark gnus-ticked-mark)
11807             (= mark gnus-dormant-mark))
11808         (gnus-mark-article-as-unread article mark)
11809       (gnus-mark-article-as-read article mark))
11810
11811     ;; See whether the article is to be put in the cache.
11812     (and gnus-use-cache
11813          (not (= mark gnus-canceled-mark))
11814          (vectorp (gnus-summary-article-header article))
11815          (save-excursion
11816            (gnus-cache-possibly-enter-article
11817             gnus-newsgroup-name article
11818             (gnus-summary-article-header article)
11819             (= mark gnus-ticked-mark)
11820             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11821
11822     (if (gnus-summary-goto-subject article nil t)
11823         (let ((buffer-read-only nil))
11824           (gnus-summary-show-thread)
11825           ;; Fix the mark.
11826           (gnus-summary-update-mark mark 'unread)
11827           t))))
11828
11829 (defun gnus-summary-update-secondary-mark (article)
11830   "Update the secondary (read, process, cache) mark."
11831   (gnus-summary-update-mark
11832    (cond ((memq article gnus-newsgroup-processable)
11833           gnus-process-mark)
11834          ((memq article gnus-newsgroup-cached)
11835           gnus-cached-mark)
11836          ((memq article gnus-newsgroup-replied)
11837           gnus-replied-mark)
11838          ((memq article gnus-newsgroup-saved)
11839           gnus-saved-mark)
11840          (t gnus-unread-mark))
11841    'replied)
11842   (when (gnus-visual-p 'summary-highlight 'highlight)
11843     (run-hooks 'gnus-summary-update-hook))
11844   t)
11845
11846 (defun gnus-summary-update-mark (mark type)
11847   (beginning-of-line)
11848   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11849         (buffer-read-only nil))
11850     (when forward
11851       ;; Go to the right position on the line.
11852       (forward-char forward)
11853       ;; Replace the old mark with the new mark.
11854       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11855       ;; Optionally update the marks by some user rule.
11856       (when (eq type 'unread)
11857         (gnus-data-set-mark
11858          (gnus-data-find (gnus-summary-article-number)) mark)
11859         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11860
11861 (defun gnus-mark-article-as-read (article &optional mark)
11862   "Enter ARTICLE in the pertinent lists and remove it from others."
11863   ;; Make the article expirable.
11864   (let ((mark (or mark gnus-del-mark)))
11865     (if (= mark gnus-expirable-mark)
11866         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11867       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11868     ;; Remove from unread and marked lists.
11869     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11870     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11871     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11872     (push (cons article mark) gnus-newsgroup-reads)
11873     ;; Possibly remove from cache, if that is used.
11874     (when gnus-use-cache
11875       (gnus-cache-enter-remove-article article))))
11876
11877 (defun gnus-mark-article-as-unread (article &optional mark)
11878   "Enter ARTICLE in the pertinent lists and remove it from others."
11879   (let ((mark (or mark gnus-ticked-mark)))
11880     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11881     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11882     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11883     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11884     (cond ((= mark gnus-ticked-mark)
11885            (push article gnus-newsgroup-marked))
11886           ((= mark gnus-dormant-mark)
11887            (push article gnus-newsgroup-dormant))
11888           (t
11889            (push article gnus-newsgroup-unreads)))
11890     (setq gnus-newsgroup-reads
11891           (delq (assq article gnus-newsgroup-reads)
11892                 gnus-newsgroup-reads))))
11893
11894 (defalias 'gnus-summary-mark-as-unread-forward
11895   'gnus-summary-tick-article-forward)
11896 (make-obsolete 'gnus-summary-mark-as-unread-forward
11897                'gnus-summary-tick-article-forward)
11898 (defun gnus-summary-tick-article-forward (n)
11899   "Tick N articles forwards.
11900 If N is negative, tick backwards instead.
11901 The difference between N and the number of articles ticked is returned."
11902   (interactive "p")
11903   (gnus-summary-mark-forward n gnus-ticked-mark))
11904
11905 (defalias 'gnus-summary-mark-as-unread-backward
11906   'gnus-summary-tick-article-backward)
11907 (make-obsolete 'gnus-summary-mark-as-unread-backward
11908                'gnus-summary-tick-article-backward)
11909 (defun gnus-summary-tick-article-backward (n)
11910   "Tick N articles backwards.
11911 The difference between N and the number of articles ticked is returned."
11912   (interactive "p")
11913   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11914
11915 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11916 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11917 (defun gnus-summary-tick-article (&optional article clear-mark)
11918   "Mark current article as unread.
11919 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11920 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11921   (interactive)
11922   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11923                                        gnus-ticked-mark)))
11924
11925 (defun gnus-summary-mark-as-read-forward (n)
11926   "Mark N articles as read forwards.
11927 If N is negative, mark backwards instead.
11928 The difference between N and the actual number of articles marked is
11929 returned."
11930   (interactive "p")
11931   (gnus-summary-mark-forward n gnus-del-mark t))
11932
11933 (defun gnus-summary-mark-as-read-backward (n)
11934   "Mark the N articles as read backwards.
11935 The difference between N and the actual number of articles marked is
11936 returned."
11937   (interactive "p")
11938   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11939
11940 (defun gnus-summary-mark-as-read (&optional article mark)
11941   "Mark current article as read.
11942 ARTICLE specifies the article to be marked as read.
11943 MARK specifies a string to be inserted at the beginning of the line."
11944   (gnus-summary-mark-article article mark))
11945
11946 (defun gnus-summary-clear-mark-forward (n)
11947   "Clear marks from N articles forward.
11948 If N is negative, clear backward instead.
11949 The difference between N and the number of marks cleared is returned."
11950   (interactive "p")
11951   (gnus-summary-mark-forward n gnus-unread-mark))
11952
11953 (defun gnus-summary-clear-mark-backward (n)
11954   "Clear marks from N articles backward.
11955 The difference between N and the number of marks cleared is returned."
11956   (interactive "p")
11957   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11958
11959 (defun gnus-summary-mark-unread-as-read ()
11960   "Intended to be used by `gnus-summary-mark-article-hook'."
11961   (when (memq gnus-current-article gnus-newsgroup-unreads)
11962     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11963
11964 (defun gnus-summary-mark-read-and-unread-as-read ()
11965   "Intended to be used by `gnus-summary-mark-article-hook'."
11966   (let ((mark (gnus-summary-article-mark)))
11967     (when (or (gnus-unread-mark-p mark)
11968               (gnus-read-mark-p mark))
11969       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11970
11971 (defun gnus-summary-mark-region-as-read (point mark all)
11972   "Mark all unread articles between point and mark as read.
11973 If given a prefix, mark all articles between point and mark as read,
11974 even ticked and dormant ones."
11975   (interactive "r\nP")
11976   (save-excursion
11977     (let (article)
11978       (goto-char point)
11979       (beginning-of-line)
11980       (while (and
11981               (< (point) mark)
11982               (progn
11983                 (when (or all
11984                           (memq (setq article (gnus-summary-article-number))
11985                                 gnus-newsgroup-unreads))
11986                   (gnus-summary-mark-article article gnus-del-mark))
11987                 t)
11988               (gnus-summary-find-next))))))
11989
11990 (defun gnus-summary-mark-below (score mark)
11991   "Mark articles with score less than SCORE with MARK."
11992   (interactive "P\ncMark: ")
11993   (gnus-set-global-variables)
11994   (setq score (if score
11995                   (prefix-numeric-value score)
11996                 (or gnus-summary-default-score 0)))
11997   (save-excursion
11998     (set-buffer gnus-summary-buffer)
11999     (goto-char (point-min))
12000     (while 
12001         (progn
12002           (and (< (gnus-summary-article-score) score)
12003                (gnus-summary-mark-article nil mark))
12004           (gnus-summary-find-next)))))
12005
12006 (defun gnus-summary-kill-below (&optional score)
12007   "Mark articles with score below SCORE as read."
12008   (interactive "P")
12009   (gnus-set-global-variables)
12010   (gnus-summary-mark-below score gnus-killed-mark))
12011
12012 (defun gnus-summary-clear-above (&optional score)
12013   "Clear all marks from articles with score above SCORE."
12014   (interactive "P")
12015   (gnus-set-global-variables)
12016   (gnus-summary-mark-above score gnus-unread-mark))
12017
12018 (defun gnus-summary-tick-above (&optional score)
12019   "Tick all articles with score above SCORE."
12020   (interactive "P")
12021   (gnus-set-global-variables)
12022   (gnus-summary-mark-above score gnus-ticked-mark))
12023
12024 (defun gnus-summary-mark-above (score mark)
12025   "Mark articles with score over SCORE with MARK."
12026   (interactive "P\ncMark: ")
12027   (gnus-set-global-variables)
12028   (setq score (if score
12029                   (prefix-numeric-value score)
12030                 (or gnus-summary-default-score 0)))
12031   (save-excursion
12032     (set-buffer gnus-summary-buffer)
12033     (goto-char (point-min))
12034     (while (and (progn
12035                   (if (> (gnus-summary-article-score) score)
12036                       (gnus-summary-mark-article nil mark))
12037                   t)
12038                 (gnus-summary-find-next)))))
12039
12040 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12041 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12042 (defun gnus-summary-limit-include-expunged ()
12043   "Display all the hidden articles that were expunged for low scores."
12044   (interactive)
12045   (gnus-set-global-variables)
12046   (let ((buffer-read-only nil))
12047     (let ((scored gnus-newsgroup-scored)
12048           headers h)
12049       (while scored
12050         (or (gnus-summary-goto-subject (caar scored))
12051             (and (setq h (gnus-summary-article-header (caar scored)))
12052                  (< (cdar scored) gnus-summary-expunge-below)
12053                  (setq headers (cons h headers))))
12054         (setq scored (cdr scored)))
12055       (or headers (error "No expunged articles hidden."))
12056       (goto-char (point-min))
12057       (gnus-summary-prepare-unthreaded (nreverse headers)))
12058     (goto-char (point-min))
12059     (gnus-summary-position-point)))
12060
12061 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12062   "Mark all articles not marked as unread in this newsgroup as read.
12063 If prefix argument ALL is non-nil, all articles are marked as read.
12064 If QUIETLY is non-nil, no questions will be asked.
12065 If TO-HERE is non-nil, it should be a point in the buffer.  All
12066 articles before this point will be marked as read.
12067 The number of articles marked as read is returned."
12068   (interactive "P")
12069   (gnus-set-global-variables)
12070   (prog1
12071       (if (or quietly
12072               (not gnus-interactive-catchup) ;Without confirmation?
12073               gnus-expert-user
12074               (gnus-y-or-n-p
12075                (if all
12076                    "Mark absolutely all articles as read? "
12077                  "Mark all unread articles as read? ")))
12078           (if (and not-mark
12079                    (not gnus-newsgroup-adaptive)
12080                    (not gnus-newsgroup-auto-expire))
12081               (progn
12082                 (when all
12083                   (setq gnus-newsgroup-marked nil
12084                         gnus-newsgroup-dormant nil))
12085                 (setq gnus-newsgroup-unreads nil))
12086             ;; We actually mark all articles as canceled, which we
12087             ;; have to do when using auto-expiry or adaptive scoring.
12088             (gnus-summary-show-all-threads)
12089             (if (gnus-summary-first-subject (not all))
12090                 (while (and
12091                         (if to-here (< (point) to-here) t)
12092                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12093                         (gnus-summary-find-next (not all)))))
12094             (unless to-here
12095               (setq gnus-newsgroup-unreads nil))
12096             (gnus-set-mode-line 'summary)))
12097     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12098       (if (and (not to-here) (eq 'nnvirtual (car method)))
12099           (nnvirtual-catchup-group
12100            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12101     (gnus-summary-position-point)))
12102
12103 (defun gnus-summary-catchup-to-here (&optional all)
12104   "Mark all unticked articles before the current one as read.
12105 If ALL is non-nil, also mark ticked and dormant articles as read."
12106   (interactive "P")
12107   (gnus-set-global-variables)
12108   (save-excursion
12109     (let ((beg (point)))
12110       ;; We check that there are unread articles.
12111       (when (or all (gnus-summary-find-prev))
12112         (gnus-summary-catchup all t beg))))
12113   (gnus-summary-position-point))
12114
12115 (defun gnus-summary-catchup-all (&optional quietly)
12116   "Mark all articles in this newsgroup as read."
12117   (interactive "P")
12118   (gnus-set-global-variables)
12119   (gnus-summary-catchup t quietly))
12120
12121 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12122   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12123 If prefix argument ALL is non-nil, all articles are marked as read."
12124   (interactive "P")
12125   (gnus-set-global-variables)
12126   (gnus-summary-catchup all quietly nil 'fast)
12127   ;; Select next newsgroup or exit.
12128   (if (eq gnus-auto-select-next 'quietly)
12129       (gnus-summary-next-group nil)
12130     (gnus-summary-exit)))
12131
12132 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12133   "Mark all articles in this newsgroup as read, and then exit."
12134   (interactive "P")
12135   (gnus-set-global-variables)
12136   (gnus-summary-catchup-and-exit t quietly))
12137
12138 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12139 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12140   "Mark all articles in this group as read and select the next group.
12141 If given a prefix, mark all articles, unread as well as ticked, as
12142 read."
12143   (interactive "P")
12144   (gnus-set-global-variables)
12145   (save-excursion
12146     (gnus-summary-catchup all))
12147   (gnus-summary-next-article t nil nil t))
12148
12149 ;; Thread-based commands.
12150
12151 (defun gnus-summary-articles-in-thread (&optional article)
12152   "Return a list of all articles in the current thread.
12153 If ARTICLE is non-nil, return all articles in the thread that starts
12154 with that article."
12155   (let* ((article (or article (gnus-summary-article-number)))
12156          (data (gnus-data-find-list article))
12157          (top-level (gnus-data-level (car data)))
12158          (top-subject
12159           (cond ((null gnus-thread-operation-ignore-subject)
12160                  (gnus-simplify-subject-re
12161                   (mail-header-subject (gnus-data-header (car data)))))
12162                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12163                  (gnus-simplify-subject-fuzzy
12164                   (mail-header-subject (gnus-data-header (car data)))))
12165                 (t nil)))
12166          articles)
12167     (if (not data)
12168         ()                              ; This article doesn't exist.
12169       (while data
12170         (and (or (not top-subject)
12171                  (string= top-subject
12172                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12173                               (gnus-simplify-subject-fuzzy
12174                                (mail-header-subject
12175                                 (gnus-data-header (car data))))
12176                             (gnus-simplify-subject-re
12177                              (mail-header-subject
12178                               (gnus-data-header (car data)))))))
12179              (setq articles (cons (gnus-data-number (car data)) articles)))
12180         (if (and (setq data (cdr data))
12181                  (> (gnus-data-level (car data)) top-level))
12182             ()
12183           (setq data nil)))
12184       ;; Return the list of articles.
12185       (nreverse articles))))
12186
12187 (defun gnus-summary-rethread-current ()
12188   "Rethread the thread the current article is part of."
12189   (interactive)
12190   (gnus-set-global-variables)
12191   (let* ((gnus-show-threads t)
12192          (article (gnus-summary-article-number))
12193          (id (mail-header-id (gnus-summary-article-header)))
12194          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12195     (unless id
12196       (error "No article on the current line"))
12197     (gnus-rebuild-thread id)
12198     (gnus-summary-goto-subject article)))
12199
12200 (defun gnus-summary-reparent-thread ()
12201   "Make current article child of the marked (or previous) article.
12202
12203 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12204 is non-nil or the Subject: of both articles are the same."
12205   (interactive)
12206   (or (not (gnus-group-read-only-p))
12207       (error "The current newsgroup does not support article editing."))
12208   (or (<= (length gnus-newsgroup-processable) 1)
12209       (error "No more than one article may be marked."))
12210   (save-window-excursion
12211     (let ((gnus-article-buffer " *reparent*")
12212           (current-article (gnus-summary-article-number))
12213           ; first grab the marked article, otherwise one line up.
12214           (parent-article (if (not (null gnus-newsgroup-processable))
12215                               (car gnus-newsgroup-processable)
12216                             (save-excursion
12217                               (if (eq (forward-line -1) 0)
12218                                   (gnus-summary-article-number)
12219                                 (error "Beginning of summary buffer."))))))
12220       (or (not (eq current-article parent-article))
12221           (error "An article may not be self-referential."))
12222       (let ((message-id (mail-header-id 
12223                          (gnus-summary-article-header parent-article))))
12224         (or (and message-id (not (equal message-id "")))
12225             (error "No message-id in desired parent."))
12226         (gnus-summary-select-article t t nil current-article)
12227         (set-buffer gnus-article-buffer)
12228         (setq buffer-read-only nil)
12229         (let ((buf (format "%s" (buffer-string))))
12230           (erase-buffer)
12231           (insert buf))
12232         (goto-char (point-min))
12233         (if (search-forward-regexp "^References: " nil t)
12234             (insert message-id " " )
12235           (insert "References: " message-id "\n"))
12236         (or (gnus-request-replace-article current-article
12237                                           (car gnus-article-current)
12238                                           gnus-article-buffer)
12239             (error "Couldn't replace article."))
12240         (set-buffer gnus-summary-buffer)
12241         (gnus-summary-unmark-all-processable)
12242         (gnus-summary-rethread-current)
12243         (message "Article %d is now the child of article %d."
12244                  current-article parent-article)))))
12245
12246 (defun gnus-summary-toggle-threads (&optional arg)
12247   "Toggle showing conversation threads.
12248 If ARG is positive number, turn showing conversation threads on."
12249   (interactive "P")
12250   (gnus-set-global-variables)
12251   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12252     (setq gnus-show-threads
12253           (if (null arg) (not gnus-show-threads)
12254             (> (prefix-numeric-value arg) 0)))
12255     (gnus-summary-prepare)
12256     (gnus-summary-goto-subject current)
12257     (gnus-summary-position-point)))
12258
12259 (defun gnus-summary-show-all-threads ()
12260   "Show all threads."
12261   (interactive)
12262   (gnus-set-global-variables)
12263   (save-excursion
12264     (let ((buffer-read-only nil))
12265       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12266   (gnus-summary-position-point))
12267
12268 (defun gnus-summary-show-thread ()
12269   "Show thread subtrees.
12270 Returns nil if no thread was there to be shown."
12271   (interactive)
12272   (gnus-set-global-variables)
12273   (let ((buffer-read-only nil)
12274         (orig (point))
12275         ;; first goto end then to beg, to have point at beg after let
12276         (end (progn (end-of-line) (point)))
12277         (beg (progn (beginning-of-line) (point))))
12278     (prog1
12279         ;; Any hidden lines here?
12280         (search-forward "\r" end t)
12281       (subst-char-in-region beg end ?\^M ?\n t)
12282       (goto-char orig)
12283       (gnus-summary-position-point))))
12284
12285 (defun gnus-summary-hide-all-threads ()
12286   "Hide all thread subtrees."
12287   (interactive)
12288   (gnus-set-global-variables)
12289   (save-excursion
12290     (goto-char (point-min))
12291     (gnus-summary-hide-thread)
12292     (while (zerop (gnus-summary-next-thread 1 t))
12293       (gnus-summary-hide-thread)))
12294   (gnus-summary-position-point))
12295
12296 (defun gnus-summary-hide-thread ()
12297   "Hide thread subtrees.
12298 Returns nil if no threads were there to be hidden."
12299   (interactive)
12300   (gnus-set-global-variables)
12301   (let ((buffer-read-only nil)
12302         (start (point))
12303         (article (gnus-summary-article-number)))
12304     (goto-char start)
12305     ;; Go forward until either the buffer ends or the subthread
12306     ;; ends.
12307     (when (and (not (eobp))
12308                (or (zerop (gnus-summary-next-thread 1 t))
12309                    (goto-char (point-max))))
12310       (prog1
12311           (if (and (> (point) start)
12312                    (search-backward "\n" start t))
12313               (progn
12314                 (subst-char-in-region start (point) ?\n ?\^M)
12315                 (gnus-summary-goto-subject article))
12316             (goto-char start)
12317             nil)
12318         ;;(gnus-summary-position-point)
12319         ))))
12320
12321 (defun gnus-summary-go-to-next-thread (&optional previous)
12322   "Go to the same level (or less) next thread.
12323 If PREVIOUS is non-nil, go to previous thread instead.
12324 Return the article number moved to, or nil if moving was impossible."
12325   (if (and (eq gnus-summary-make-false-root 'dummy)
12326            (gnus-summary-article-intangible-p))
12327       (let ((beg (point)))
12328         (while (and (zerop (forward-line 1))
12329                     (not (gnus-summary-article-intangible-p))
12330                     (not (zerop (save-excursion 
12331                                   (gnus-summary-thread-level))))))
12332         (if (eobp)
12333             (progn
12334               (goto-char beg)
12335               nil)
12336           (point)))
12337     (let* ((level (gnus-summary-thread-level))
12338            (article (gnus-summary-article-number))
12339            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12340            oart)
12341       (while data
12342         (if (<= (gnus-data-level (car data)) level)
12343             (setq oart (gnus-data-number (car data))
12344                   data nil)
12345           (setq data (cdr data))))
12346       (and oart
12347            (gnus-summary-goto-subject oart)))))
12348
12349 (defun gnus-summary-next-thread (n &optional silent)
12350   "Go to the same level next N'th thread.
12351 If N is negative, search backward instead.
12352 Returns the difference between N and the number of skips actually
12353 done.
12354
12355 If SILENT, don't output messages."
12356   (interactive "p")
12357   (gnus-set-global-variables)
12358   (let ((backward (< n 0))
12359         (n (abs n))
12360         old dum int)
12361     (while (and (> n 0)
12362                 (setq old (save-excursion 
12363                             (forward-line 1) 
12364                             (setq int (gnus-summary-article-intangible-p))
12365                             (point)))
12366                 (or int 
12367                     (gnus-summary-go-to-next-thread backward)))
12368       (when (and (eq gnus-summary-make-false-root 'dummy)
12369                  (setq dum (text-property-not-all
12370                             old (point) 'gnus-intangible nil)))
12371         (goto-char dum))
12372       (decf n))
12373     (unless silent 
12374       (gnus-summary-position-point))
12375     (when (and (not silent) (/= 0 n))
12376       (gnus-message 7 "No more threads"))
12377     n))
12378
12379 (defun gnus-summary-prev-thread (n)
12380   "Go to the same level previous N'th thread.
12381 Returns the difference between N and the number of skips actually
12382 done."
12383   (interactive "p")
12384   (gnus-set-global-variables)
12385   (gnus-summary-next-thread (- n)))
12386
12387 (defun gnus-summary-go-down-thread ()
12388   "Go down one level in the current thread."
12389   (let ((children (gnus-summary-article-children)))
12390     (and children
12391          (gnus-summary-goto-subject (car children)))))
12392
12393 (defun gnus-summary-go-up-thread ()
12394   "Go up one level in the current thread."
12395   (let ((parent (gnus-summary-article-parent)))
12396     (and parent
12397          (gnus-summary-goto-subject parent))))
12398
12399 (defun gnus-summary-down-thread (n)
12400   "Go down thread N steps.
12401 If N is negative, go up instead.
12402 Returns the difference between N and how many steps down that were
12403 taken."
12404   (interactive "p")
12405   (gnus-set-global-variables)
12406   (let ((up (< n 0))
12407         (n (abs n)))
12408     (while (and (> n 0)
12409                 (if up (gnus-summary-go-up-thread)
12410                   (gnus-summary-go-down-thread)))
12411       (setq n (1- n)))
12412     (gnus-summary-position-point)
12413     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12414     n))
12415
12416 (defun gnus-summary-up-thread (n)
12417   "Go up thread N steps.
12418 If N is negative, go up instead.
12419 Returns the difference between N and how many steps down that were
12420 taken."
12421   (interactive "p")
12422   (gnus-set-global-variables)
12423   (gnus-summary-down-thread (- n)))
12424
12425 (defun gnus-summary-top-thread ()
12426   "Go to the top of the thread."
12427   (interactive)
12428   (gnus-set-global-variables)
12429   (while (gnus-summary-go-up-thread))
12430   (gnus-summary-article-number))
12431
12432 (defun gnus-summary-kill-thread (&optional unmark)
12433   "Mark articles under current thread as read.
12434 If the prefix argument is positive, remove any kinds of marks.
12435 If the prefix argument is negative, tick articles instead."
12436   (interactive "P")
12437   (gnus-set-global-variables)
12438   (if unmark
12439       (setq unmark (prefix-numeric-value unmark)))
12440   (let ((articles (gnus-summary-articles-in-thread)))
12441     (save-excursion
12442       ;; Expand the thread.
12443       (gnus-summary-show-thread)
12444       ;; Mark all the articles.
12445       (while articles
12446         (gnus-summary-goto-subject (car articles))
12447         (cond ((null unmark)
12448                (gnus-summary-mark-article-as-read gnus-killed-mark))
12449               ((> unmark 0)
12450                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12451               (t
12452                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12453         (setq articles (cdr articles))))
12454     ;; Hide killed subtrees.
12455     (and (null unmark)
12456          gnus-thread-hide-killed
12457          (gnus-summary-hide-thread))
12458     ;; If marked as read, go to next unread subject.
12459     (if (null unmark)
12460         ;; Go to next unread subject.
12461         (gnus-summary-next-subject 1 t)))
12462   (gnus-set-mode-line 'summary))
12463
12464 ;; Summary sorting commands
12465
12466 (defun gnus-summary-sort-by-number (&optional reverse)
12467   "Sort summary buffer by article number.
12468 Argument REVERSE means reverse order."
12469   (interactive "P")
12470   (gnus-summary-sort 'number reverse))
12471
12472 (defun gnus-summary-sort-by-author (&optional reverse)
12473   "Sort summary buffer by author name alphabetically.
12474 If case-fold-search is non-nil, case of letters is ignored.
12475 Argument REVERSE means reverse order."
12476   (interactive "P")
12477   (gnus-summary-sort 'author reverse))
12478
12479 (defun gnus-summary-sort-by-subject (&optional reverse)
12480   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12481 If case-fold-search is non-nil, case of letters is ignored.
12482 Argument REVERSE means reverse order."
12483   (interactive "P")
12484   (gnus-summary-sort 'subject reverse))
12485
12486 (defun gnus-summary-sort-by-date (&optional reverse)
12487   "Sort summary buffer by date.
12488 Argument REVERSE means reverse order."
12489   (interactive "P")
12490   (gnus-summary-sort 'date reverse))
12491
12492 (defun gnus-summary-sort-by-score (&optional reverse)
12493   "Sort summary buffer by score.
12494 Argument REVERSE means reverse order."
12495   (interactive "P")
12496   (gnus-summary-sort 'score reverse))
12497
12498 (defun gnus-summary-sort (predicate reverse)
12499   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12500   (gnus-set-global-variables)
12501   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12502          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12503          (gnus-thread-sort-functions
12504           (list
12505            (if (not reverse)
12506                thread
12507              `(lambda (t1 t2)
12508                 (,thread t2 t1)))))
12509          (gnus-article-sort-functions
12510           (list
12511            (if (not reverse)
12512                article
12513              `(lambda (t1 t2)
12514                 (,article t2 t1)))))
12515          (buffer-read-only)
12516          (gnus-summary-prepare-hook nil))
12517     ;; We do the sorting by regenerating the threads.
12518     (gnus-summary-prepare)
12519     ;; Hide subthreads if needed.
12520     (when (and gnus-show-threads gnus-thread-hide-subtree)
12521       (gnus-summary-hide-all-threads)))
12522   ;; If in async mode, we send some info to the backend.
12523   (when gnus-newsgroup-async
12524     (gnus-request-asynchronous
12525      gnus-newsgroup-name gnus-newsgroup-data)))
12526
12527 (defun gnus-sortable-date (date)
12528   "Make sortable string by string-lessp from DATE.
12529 Timezone package is used."
12530   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12531          (year (aref date 0))
12532          (month (aref date 1))
12533          (day (aref date 2)))
12534     (timezone-make-sortable-date
12535      year month day
12536      (timezone-make-time-string
12537       (aref date 3) (aref date 4) (aref date 5)))))
12538
12539 ;; Summary saving commands.
12540
12541 (defun gnus-summary-save-article (&optional n not-saved)
12542   "Save the current article using the default saver function.
12543 If N is a positive number, save the N next articles.
12544 If N is a negative number, save the N previous articles.
12545 If N is nil and any articles have been marked with the process mark,
12546 save those articles instead.
12547 The variable `gnus-default-article-saver' specifies the saver function."
12548   (interactive "P")
12549   (gnus-set-global-variables)
12550   (let ((articles (gnus-summary-work-articles n))
12551         file header article)
12552     (while articles
12553       (setq header (gnus-summary-article-header
12554                     (setq article (pop articles))))
12555       (if (not (vectorp header))
12556           ;; This is a pseudo-article.
12557           (if (assq 'name header)
12558               (gnus-copy-file (cdr (assq 'name header)))
12559             (gnus-message 1 "Article %d is unsaveable" article))
12560         ;; This is a real article.
12561         (save-window-excursion
12562           (gnus-summary-select-article t nil nil article))
12563         (unless gnus-save-all-headers
12564           ;; Remove headers accoring to `gnus-saved-headers'.
12565           (let ((gnus-visible-headers
12566                  (or gnus-saved-headers gnus-visible-headers)))
12567             (gnus-article-hide-headers nil t)))
12568         ;; Remove any X-Gnus lines.
12569         (save-excursion
12570           (set-buffer gnus-article-buffer)
12571           (save-restriction
12572             (let ((buffer-read-only nil))
12573               (nnheader-narrow-to-headers)
12574               (while (re-search-forward "^X-Gnus" nil t)
12575                 (gnus-delete-line)))))
12576         (save-window-excursion
12577           (if (not gnus-default-article-saver)
12578               (error "No default saver is defined.")
12579             (setq file (funcall
12580                         gnus-default-article-saver
12581                         (cond
12582                          ((not gnus-prompt-before-saving)
12583                           'default)
12584                          ((eq gnus-prompt-before-saving 'always)
12585                           nil)
12586                          (t file))))))
12587         (gnus-summary-remove-process-mark article)
12588         (unless not-saved
12589           (gnus-summary-set-saved-mark article))))
12590     (gnus-summary-position-point)
12591     n))
12592
12593 (defun gnus-summary-pipe-output (&optional arg)
12594   "Pipe the current article to a subprocess.
12595 If N is a positive number, pipe the N next articles.
12596 If N is a negative number, pipe the N previous articles.
12597 If N is nil and any articles have been marked with the process mark,
12598 pipe those articles instead."
12599   (interactive "P")
12600   (gnus-set-global-variables)
12601   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12602     (gnus-summary-save-article arg t))
12603   (gnus-configure-windows 'pipe))
12604
12605 (defun gnus-summary-save-article-mail (&optional arg)
12606   "Append the current article to an mail file.
12607 If N is a positive number, save the N next articles.
12608 If N is a negative number, save the N previous articles.
12609 If N is nil and any articles have been marked with the process mark,
12610 save those articles instead."
12611   (interactive "P")
12612   (gnus-set-global-variables)
12613   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12614     (gnus-summary-save-article arg)))
12615
12616 (defun gnus-summary-save-article-rmail (&optional arg)
12617   "Append the current article to an rmail file.
12618 If N is a positive number, save the N next articles.
12619 If N is a negative number, save the N previous articles.
12620 If N is nil and any articles have been marked with the process mark,
12621 save those articles instead."
12622   (interactive "P")
12623   (gnus-set-global-variables)
12624   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12625     (gnus-summary-save-article arg)))
12626
12627 (defun gnus-summary-save-article-file (&optional arg)
12628   "Append the current article to a file.
12629 If N is a positive number, save the N next articles.
12630 If N is a negative number, save the N previous articles.
12631 If N is nil and any articles have been marked with the process mark,
12632 save those articles instead."
12633   (interactive "P")
12634   (gnus-set-global-variables)
12635   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12636     (gnus-summary-save-article arg)))
12637
12638 (defun gnus-summary-save-article-body-file (&optional arg)
12639   "Append the current article body to a file.
12640 If N is a positive number, save the N next articles.
12641 If N is a negative number, save the N previous articles.
12642 If N is nil and any articles have been marked with the process mark,
12643 save those articles instead."
12644   (interactive "P")
12645   (gnus-set-global-variables)
12646   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12647     (gnus-summary-save-article arg)))
12648
12649 (defun gnus-get-split-value (methods)
12650   "Return a value based on the split METHODS."
12651   (let (split-name method result match)
12652     (when methods
12653       (save-excursion
12654         (set-buffer gnus-original-article-buffer)
12655         (save-restriction
12656           (nnheader-narrow-to-headers)
12657           (while methods
12658             (goto-char (point-min))
12659             (setq method (pop methods))
12660             (setq match (car method))
12661             (when (cond
12662                    ((stringp match)
12663                     ;; Regular expression.
12664                     (condition-case ()
12665                         (re-search-forward match nil t)
12666                       (error nil)))
12667                    ((gnus-functionp match)
12668                     ;; Function.
12669                     (save-restriction
12670                       (widen)
12671                       (setq result (funcall match gnus-newsgroup-name))))
12672                    ((consp match)
12673                     ;; Form.
12674                     (save-restriction
12675                       (widen)
12676                       (setq result (eval match)))))
12677               (setq split-name (append (cdr method) split-name))
12678               (cond ((stringp result)
12679                      (push result split-name))
12680                     ((consp result)
12681                      (setq split-name (append result split-name)))))))))
12682     split-name))
12683
12684 (defun gnus-read-move-group-name (prompt default articles prefix)
12685   "Read a group name."
12686   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12687          (prom
12688           (format "Where do you want to %s %s? "
12689                   prompt
12690                   (if (> (length articles) 1)
12691                       (format "these %d articles" (length articles))
12692                     "this article")))
12693          (to-newsgroup
12694           (cond
12695            ((null split-name)
12696             (completing-read
12697              (concat prom
12698                      (if default
12699                          (format "(default %s) " default)
12700                        ""))
12701              gnus-active-hashtb nil nil prefix))
12702            ((= 1 (length split-name))
12703             (completing-read prom gnus-active-hashtb
12704                              nil nil (cons (car split-name) 0)))
12705            (t
12706             (completing-read
12707              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12708
12709     (when to-newsgroup
12710       (if (or (string= to-newsgroup "")
12711               (string= to-newsgroup prefix))
12712           (setq to-newsgroup (or default "")))
12713       (or (gnus-active to-newsgroup)
12714           (gnus-activate-group to-newsgroup)
12715           (error "No such group: %s" to-newsgroup)))
12716     to-newsgroup))
12717
12718 (defun gnus-read-save-file-name (prompt default-name)
12719   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12720          (file
12721           ;; Let the split methods have their say.
12722           (cond
12723            ;; No split name was found.
12724            ((null split-name)
12725             (read-file-name
12726              (concat prompt " (default "
12727                      (file-name-nondirectory default-name) ") ")
12728              (file-name-directory default-name)
12729              default-name))
12730            ;; A single split name was found
12731            ((= 1 (length split-name))
12732             (read-file-name
12733              (concat prompt " (default " (car split-name) ") ")
12734              gnus-article-save-directory
12735              (concat gnus-article-save-directory (car split-name))))
12736            ;; A list of splits was found.
12737            (t
12738             (setq split-name (mapcar (lambda (el) (list el))
12739                                      (nreverse split-name)))
12740             (let ((result (completing-read
12741                            (concat prompt " ") split-name nil nil)))
12742               (concat gnus-article-save-directory
12743                       (if (string= result "")
12744                           (caar split-name)
12745                         result)))))))
12746     ;; If we have read a directory, we append the default file name.
12747     (when (file-directory-p file)
12748       (setq file (concat (file-name-as-directory file)
12749                          (file-name-nondirectory default-name))))
12750     ;; Possibly translate some charaters.
12751     (nnheader-translate-file-chars file)))
12752
12753 (defun gnus-article-archive-name (group)
12754   "Return the first instance of an \"Archive-name\" in the current buffer."
12755   (let ((case-fold-search t))
12756     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12757       (match-string 1))))
12758
12759 (defun gnus-summary-save-in-rmail (&optional filename)
12760   "Append this article to Rmail file.
12761 Optional argument FILENAME specifies file name.
12762 Directory to save to is default to `gnus-article-save-directory' which
12763 is initialized from the SAVEDIR environment variable."
12764   (interactive)
12765   (gnus-set-global-variables)
12766   (let ((default-name
12767           (funcall gnus-rmail-save-name gnus-newsgroup-name
12768                    gnus-current-headers gnus-newsgroup-last-rmail)))
12769     (setq filename
12770           (cond ((eq filename 'default)
12771                  default-name)
12772                 (filename filename)
12773                 (t (gnus-read-save-file-name
12774                     "Save in rmail file:" default-name))))
12775     (gnus-make-directory (file-name-directory filename))
12776     (gnus-eval-in-buffer-window
12777      gnus-original-article-buffer
12778      (save-excursion
12779        (save-restriction
12780          (widen)
12781          (gnus-output-to-rmail filename))))
12782     ;; Remember the directory name to save articles
12783     (setq gnus-newsgroup-last-rmail filename)))
12784
12785 (defun gnus-summary-save-in-mail (&optional filename)
12786   "Append this article to Unix mail file.
12787 Optional argument FILENAME specifies file name.
12788 Directory to save to is default to `gnus-article-save-directory' which
12789 is initialized from the SAVEDIR environment variable."
12790   (interactive)
12791   (gnus-set-global-variables)
12792   (let ((default-name
12793           (funcall gnus-mail-save-name gnus-newsgroup-name
12794                    gnus-current-headers gnus-newsgroup-last-mail)))
12795     (setq filename
12796           (cond ((eq filename 'default)
12797                  default-name)
12798                 (filename filename)
12799                 (t (gnus-read-save-file-name
12800                     "Save in Unix mail file:" default-name))))
12801     (setq filename
12802           (expand-file-name filename
12803                             (and default-name
12804                                  (file-name-directory default-name))))
12805     (gnus-make-directory (file-name-directory filename))
12806     (gnus-eval-in-buffer-window
12807      gnus-original-article-buffer
12808      (save-excursion
12809        (save-restriction
12810          (widen)
12811          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12812              (gnus-output-to-rmail filename)
12813            (let ((mail-use-rfc822 t))
12814              (rmail-output filename 1 t t))))))
12815     ;; Remember the directory name to save articles.
12816     (setq gnus-newsgroup-last-mail filename)))
12817
12818 (defun gnus-summary-save-in-file (&optional filename)
12819   "Append this article to file.
12820 Optional argument FILENAME specifies file name.
12821 Directory to save to is default to `gnus-article-save-directory' which
12822 is initialized from the SAVEDIR environment variable."
12823   (interactive)
12824   (gnus-set-global-variables)
12825   (let ((default-name
12826           (funcall gnus-file-save-name gnus-newsgroup-name
12827                    gnus-current-headers gnus-newsgroup-last-file)))
12828     (setq filename
12829           (cond ((eq filename 'default)
12830                  default-name)
12831                 (filename filename)
12832                 (t (gnus-read-save-file-name
12833                     "Save in file:" default-name))))
12834     (gnus-make-directory (file-name-directory filename))
12835     (gnus-eval-in-buffer-window
12836      gnus-original-article-buffer
12837      (save-excursion
12838        (save-restriction
12839          (widen)
12840          (gnus-output-to-file filename))))
12841     ;; Remember the directory name to save articles.
12842     (setq gnus-newsgroup-last-file filename)))
12843
12844 (defun gnus-summary-save-body-in-file (&optional filename)
12845   "Append this article body to a file.
12846 Optional argument FILENAME specifies file name.
12847 The directory to save in defaults to `gnus-article-save-directory' which
12848 is initialized from the SAVEDIR environment variable."
12849   (interactive)
12850   (gnus-set-global-variables)
12851   (let ((default-name
12852           (funcall gnus-file-save-name gnus-newsgroup-name
12853                    gnus-current-headers gnus-newsgroup-last-file)))
12854     (setq filename
12855           (cond ((eq filename 'default)
12856                  default-name)
12857                 (filename filename)
12858                 (t (gnus-read-save-file-name
12859                     "Save body in file:" default-name))))
12860     (gnus-make-directory (file-name-directory filename))
12861     (gnus-eval-in-buffer-window
12862      gnus-article-buffer
12863      (save-excursion
12864        (save-restriction
12865          (widen)
12866          (goto-char (point-min))
12867          (and (search-forward "\n\n" nil t)
12868               (narrow-to-region (point) (point-max)))
12869          (gnus-output-to-file filename))))
12870     ;; Remember the directory name to save articles.
12871     (setq gnus-newsgroup-last-file filename)))
12872
12873 (defun gnus-summary-save-in-pipe (&optional command)
12874   "Pipe this article to subprocess."
12875   (interactive)
12876   (gnus-set-global-variables)
12877   (setq command
12878         (cond ((eq command 'default)
12879                gnus-last-shell-command)
12880               (command command)
12881               (t (read-string "Shell command on article: "
12882                               gnus-last-shell-command))))
12883   (if (string-equal command "")
12884       (setq command gnus-last-shell-command))
12885   (gnus-eval-in-buffer-window
12886    gnus-article-buffer
12887    (save-restriction
12888      (widen)
12889      (shell-command-on-region (point-min) (point-max) command nil)))
12890   (setq gnus-last-shell-command command))
12891
12892 ;; Summary extract commands
12893
12894 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12895   (let ((buffer-read-only nil)
12896         (article (gnus-summary-article-number))
12897         after-article b e)
12898     (or (gnus-summary-goto-subject article)
12899         (error (format "No such article: %d" article)))
12900     (gnus-summary-position-point)
12901     ;; If all commands are to be bunched up on one line, we collect
12902     ;; them here.
12903     (if gnus-view-pseudos-separately
12904         ()
12905       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12906             files action)
12907         (while ps
12908           (setq action (cdr (assq 'action (car ps))))
12909           (setq files (list (cdr (assq 'name (car ps)))))
12910           (while (and ps (cdr ps)
12911                       (string= (or action "1")
12912                                (or (cdr (assq 'action (cadr ps))) "2")))
12913             (setq files (cons (cdr (assq 'name (cadr ps))) files))
12914             (setcdr ps (cddr ps)))
12915           (if (not files)
12916               ()
12917             (if (not (string-match "%s" action))
12918                 (setq files (cons " " files)))
12919             (setq files (cons " " files))
12920             (and (assq 'execute (car ps))
12921                  (setcdr (assq 'execute (car ps))
12922                          (funcall (if (string-match "%s" action)
12923                                       'format 'concat)
12924                                   action
12925                                   (mapconcat (lambda (f) f) files " ")))))
12926           (setq ps (cdr ps)))))
12927     (if (and gnus-view-pseudos (not not-view))
12928         (while pslist
12929           (and (assq 'execute (car pslist))
12930                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12931                                      (eq gnus-view-pseudos 'not-confirm)))
12932           (setq pslist (cdr pslist)))
12933       (save-excursion
12934         (while pslist
12935           (setq after-article (or (cdr (assq 'article (car pslist)))
12936                                   (gnus-summary-article-number)))
12937           (gnus-summary-goto-subject after-article)
12938           (forward-line 1)
12939           (setq b (point))
12940           (insert "          " (file-name-nondirectory
12941                                 (cdr (assq 'name (car pslist))))
12942                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12943           (setq e (point))
12944           (forward-line -1)             ; back to `b'
12945           (add-text-properties
12946            b e (list 'gnus-number gnus-reffed-article-number
12947                      gnus-mouse-face-prop gnus-mouse-face))
12948           (gnus-data-enter
12949            after-article gnus-reffed-article-number
12950            gnus-unread-mark b (car pslist) 0 (- e b))
12951           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12952           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12953           (setq pslist (cdr pslist)))))))
12954
12955 (defun gnus-pseudos< (p1 p2)
12956   (let ((c1 (cdr (assq 'action p1)))
12957         (c2 (cdr (assq 'action p2))))
12958     (and c1 c2 (string< c1 c2))))
12959
12960 (defun gnus-request-pseudo-article (props)
12961   (cond ((assq 'execute props)
12962          (gnus-execute-command (cdr (assq 'execute props)))))
12963   (let ((gnus-current-article (gnus-summary-article-number)))
12964     (run-hooks 'gnus-mark-article-hook)))
12965
12966 (defun gnus-execute-command (command &optional automatic)
12967   (save-excursion
12968     (gnus-article-setup-buffer)
12969     (set-buffer gnus-article-buffer)
12970     (let ((command (if automatic command (read-string "Command: " command)))
12971           (buffer-read-only nil))
12972       (erase-buffer)
12973       (insert "$ " command "\n\n")
12974       (if gnus-view-pseudo-asynchronously
12975           (start-process "gnus-execute" nil "sh" "-c" command)
12976         (call-process "sh" nil t nil "-c" command)))))
12977
12978 (defun gnus-copy-file (file &optional to)
12979   "Copy FILE to TO."
12980   (interactive
12981    (list (read-file-name "Copy file: " default-directory)
12982          (read-file-name "Copy file to: " default-directory)))
12983   (gnus-set-global-variables)
12984   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12985   (and (file-directory-p to)
12986        (setq to (concat (file-name-as-directory to)
12987                         (file-name-nondirectory file))))
12988   (copy-file file to))
12989
12990 ;; Summary kill commands.
12991
12992 (defun gnus-summary-edit-global-kill (article)
12993   "Edit the \"global\" kill file."
12994   (interactive (list (gnus-summary-article-number)))
12995   (gnus-set-global-variables)
12996   (gnus-group-edit-global-kill article))
12997
12998 (defun gnus-summary-edit-local-kill ()
12999   "Edit a local kill file applied to the current newsgroup."
13000   (interactive)
13001   (gnus-set-global-variables)
13002   (setq gnus-current-headers (gnus-summary-article-header))
13003   (gnus-set-global-variables)
13004   (gnus-group-edit-local-kill
13005    (gnus-summary-article-number) gnus-newsgroup-name))
13006
13007 \f
13008 ;;;
13009 ;;; Gnus article mode
13010 ;;;
13011
13012 (put 'gnus-article-mode 'mode-class 'special)
13013
13014 (if gnus-article-mode-map
13015     nil
13016   (setq gnus-article-mode-map (make-keymap))
13017   (suppress-keymap gnus-article-mode-map)
13018
13019   (gnus-define-keys gnus-article-mode-map
13020     " " gnus-article-goto-next-page
13021     "\177" gnus-article-goto-prev-page
13022     [delete] gnus-article-goto-prev-page
13023     "\C-c^" gnus-article-refer-article
13024     "h" gnus-article-show-summary
13025     "s" gnus-article-show-summary
13026     "\C-c\C-m" gnus-article-mail
13027     "?" gnus-article-describe-briefly
13028     gnus-mouse-2 gnus-article-push-button
13029     "\r" gnus-article-press-button
13030     "\t" gnus-article-next-button
13031     "\M-\t" gnus-article-prev-button
13032     "\C-c\C-b" gnus-bug)
13033
13034   (substitute-key-definition
13035    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13036
13037 (defun gnus-article-mode ()
13038   "Major mode for displaying an article.
13039
13040 All normal editing commands are switched off.
13041
13042 The following commands are available:
13043
13044 \\<gnus-article-mode-map>
13045 \\[gnus-article-next-page]\t Scroll the article one page forwards
13046 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13047 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13048 \\[gnus-article-show-summary]\t Display the summary buffer
13049 \\[gnus-article-mail]\t Send a reply to the address near point
13050 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13051 \\[gnus-info-find-node]\t Go to the Gnus info node"
13052   (interactive)
13053   (when (and menu-bar-mode
13054              (gnus-visual-p 'article-menu 'menu))
13055     (gnus-article-make-menu-bar))
13056   (kill-all-local-variables)
13057   (gnus-simplify-mode-line)
13058   (setq mode-name "Article")
13059   (setq major-mode 'gnus-article-mode)
13060   (make-local-variable 'minor-mode-alist)
13061   (or (assq 'gnus-show-mime minor-mode-alist)
13062       (setq minor-mode-alist
13063             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13064   (use-local-map gnus-article-mode-map)
13065   (make-local-variable 'page-delimiter)
13066   (setq page-delimiter gnus-page-delimiter)
13067   (buffer-disable-undo (current-buffer))
13068   (setq buffer-read-only t)             ;Disable modification
13069   (run-hooks 'gnus-article-mode-hook))
13070
13071 (defun gnus-article-setup-buffer ()
13072   "Initialize the article buffer."
13073   (let* ((name (if gnus-single-article-buffer "*Article*"
13074                  (concat "*Article " gnus-newsgroup-name "*")))
13075          (original
13076           (progn (string-match "\\*Article" name)
13077                  (concat " *Original Article"
13078                          (substring name (match-end 0))))))
13079     (setq gnus-article-buffer name)
13080     (setq gnus-original-article-buffer original)
13081     ;; This might be a variable local to the summary buffer.
13082     (unless gnus-single-article-buffer
13083       (save-excursion
13084         (set-buffer gnus-summary-buffer)
13085         (setq gnus-article-buffer name)
13086         (setq gnus-original-article-buffer original)
13087         (gnus-set-global-variables))
13088       (make-local-variable 'gnus-summary-buffer))
13089     (if (get-buffer name)
13090         (save-excursion
13091           (set-buffer name)
13092           (buffer-disable-undo (current-buffer))
13093           (setq buffer-read-only t)
13094           (gnus-add-current-to-buffer-list)
13095           (or (eq major-mode 'gnus-article-mode)
13096               (gnus-article-mode))
13097           (current-buffer))
13098       (save-excursion
13099         (set-buffer (get-buffer-create name))
13100         (gnus-add-current-to-buffer-list)
13101         (gnus-article-mode)
13102         (current-buffer)))))
13103
13104 ;; Set article window start at LINE, where LINE is the number of lines
13105 ;; from the head of the article.
13106 (defun gnus-article-set-window-start (&optional line)
13107   (set-window-start
13108    (get-buffer-window gnus-article-buffer)
13109    (save-excursion
13110      (set-buffer gnus-article-buffer)
13111      (goto-char (point-min))
13112      (if (not line)
13113          (point-min)
13114        (gnus-message 6 "Moved to bookmark")
13115        (search-forward "\n\n" nil t)
13116        (forward-line line)
13117        (point)))))
13118
13119 (defun gnus-kill-all-overlays ()
13120   "Delete all overlays in the current buffer."
13121   (when (fboundp 'overlay-lists)
13122     (let* ((overlayss (overlay-lists))
13123            (buffer-read-only nil)
13124            (overlays (nconc (car overlayss) (cdr overlayss))))
13125       (while overlays
13126         (delete-overlay (pop overlays))))))
13127
13128 (defun gnus-request-article-this-buffer (article group)
13129   "Get an article and insert it into this buffer."
13130   (prog1
13131       (save-excursion
13132         (erase-buffer)
13133         (gnus-kill-all-overlays)
13134         (setq group (or group gnus-newsgroup-name))
13135
13136         ;; Open server if it has closed.
13137         (gnus-check-server (gnus-find-method-for-group group))
13138
13139         ;; Using `gnus-request-article' directly will insert the article into
13140         ;; `nntp-server-buffer' - so we'll save some time by not having to
13141         ;; copy it from the server buffer into the article buffer.
13142
13143         ;; We only request an article by message-id when we do not have the
13144         ;; headers for it, so we'll have to get those.
13145         (when (stringp article)
13146           (let ((gnus-override-method gnus-refer-article-method))
13147             (gnus-read-header article)))
13148
13149         ;; If the article number is negative, that means that this article
13150         ;; doesn't belong in this newsgroup (possibly), so we find its
13151         ;; message-id and request it by id instead of number.
13152         (when (and (numberp article)
13153                    gnus-summary-buffer
13154                    (get-buffer gnus-summary-buffer)
13155                    (buffer-name (get-buffer gnus-summary-buffer)))
13156           (save-excursion
13157             (set-buffer gnus-summary-buffer)
13158             (let ((header (gnus-summary-article-header article)))
13159               (if (< article 0)
13160                   (cond 
13161                    ((memq article gnus-newsgroup-sparse)
13162                     ;; This is a sparse gap article.
13163                     (setq article (mail-header-id header)))
13164                    ((vectorp header)
13165                     ;; It's a real article.
13166                     (setq article (mail-header-id header)))
13167                    (t
13168                     ;; It is an extracted pseudo-article.
13169                     (setq article 'pseudo)
13170                     (gnus-request-pseudo-article header))))
13171                 
13172               (let ((method (gnus-find-method-for-group 
13173                              gnus-newsgroup-name)))
13174                 (if (not (eq (car method) 'nneething))
13175                     ()
13176                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13177                                      (mail-header-subject header))))
13178                     (if (file-directory-p dir)
13179                         (progn
13180                           (setq article 'nneething)
13181                           (gnus-group-enter-directory dir)))))))))
13182
13183         (cond
13184          ;; We first check `gnus-original-article-buffer'.
13185          ((and (equal (car gnus-original-article) group)
13186                (eq (cdr gnus-original-article) article)
13187                (get-buffer gnus-original-article-buffer))
13188           (insert-buffer-substring gnus-original-article-buffer)
13189           'article)
13190          ;; Check the backlog.
13191          ((and gnus-keep-backlog
13192                (gnus-backlog-request-article group article (current-buffer)))
13193           'article)
13194          ;; Check the cache.
13195          ((and gnus-use-cache
13196                (numberp article)
13197                (gnus-cache-request-article article group))
13198           'article)
13199          ;; Get the article and put into the article buffer.
13200          ((or (stringp article) (numberp article))
13201           (let ((gnus-override-method
13202                  (and (stringp article) gnus-refer-article-method))
13203                 (buffer-read-only nil))
13204             (erase-buffer)
13205             (gnus-kill-all-overlays)
13206             (if (gnus-request-article article group (current-buffer))
13207                 (progn
13208                   (and gnus-keep-backlog
13209                        (gnus-backlog-enter-article
13210                         group article (current-buffer)))
13211                   'article))))
13212          ;; It was a pseudo.
13213          (t article)))
13214
13215     ;; Take the article from the original article buffer
13216     ;; and place it in the buffer it's supposed to be in.
13217     (when (and (get-buffer gnus-article-buffer)
13218                (equal (buffer-name (current-buffer))
13219                       (buffer-name (get-buffer gnus-article-buffer))))
13220       (save-excursion
13221         (setq gnus-original-article (cons group article))
13222         (if (get-buffer gnus-original-article-buffer)
13223             (set-buffer (get-buffer gnus-original-article-buffer))
13224           (set-buffer (get-buffer-create gnus-original-article-buffer))
13225           (buffer-disable-undo (current-buffer))
13226           (setq major-mode 'gnus-original-article-mode)
13227           (setq buffer-read-only t)
13228           (gnus-add-current-to-buffer-list))
13229         (let (buffer-read-only)
13230           (erase-buffer)
13231           (insert-buffer-substring gnus-article-buffer))))
13232     
13233     ;; Update sparse articles.
13234     (when (memq article gnus-newsgroup-sparse)
13235       (gnus-summary-update-article article))))
13236
13237 (defun gnus-read-header (id)
13238   "Read the headers of article ID and enter them into the Gnus system."
13239   (let ((group gnus-newsgroup-name)
13240         header where)
13241     ;; First we check to see whether the header in question is already
13242     ;; fetched.
13243     (if (stringp id)
13244         ;; This is a Message-ID.
13245         (setq header (gnus-id-to-header id))
13246       ;; This is an article number.
13247       (setq header (gnus-summary-article-header id)))
13248     (if header
13249         ;; We have found the header.
13250         header
13251       ;; We have to really fetch the header to this article.
13252       (when (setq where
13253                   (if (gnus-check-backend-function 'request-head group)
13254                       (gnus-request-head id group)
13255                     (gnus-request-article id group)))
13256         (save-excursion
13257           (set-buffer nntp-server-buffer)
13258           (and (search-forward "\n\n" nil t)
13259                (delete-region (1- (point)) (point-max)))
13260           (goto-char (point-max))
13261           (insert ".\n")
13262           (goto-char (point-min))
13263           (insert "211 "
13264                   (int-to-string
13265                    (cond
13266                     ((numberp id)
13267                      id)
13268                     ((cdr where)
13269                      (cdr where))
13270                     (t
13271                      gnus-reffed-article-number)))
13272                   " Article retrieved.\n"))
13273         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13274             () ; Malformed head.
13275           (if (and (stringp id)
13276                    (not (string= (gnus-group-real-name group)
13277                                  (car where))))
13278               ;; If we fetched by Message-ID and the article came
13279               ;; from a different group, we fudge some bogus article
13280               ;; numbers for this article.
13281               (mail-header-set-number header gnus-reffed-article-number))
13282           (decf gnus-reffed-article-number)
13283           (push header gnus-newsgroup-headers)
13284           (setq gnus-current-headers header)
13285           (push (mail-header-number header) gnus-newsgroup-limit)
13286           header)))))
13287
13288 (defun gnus-article-prepare (article &optional all-headers header)
13289   "Prepare ARTICLE in article mode buffer.
13290 ARTICLE should either be an article number or a Message-ID.
13291 If ARTICLE is an id, HEADER should be the article headers.
13292 If ALL-HEADERS is non-nil, no headers are hidden."
13293   (save-excursion
13294     ;; Make sure we start in a summary buffer.
13295     (unless (eq major-mode 'gnus-summary-mode)
13296       (set-buffer gnus-summary-buffer))
13297     (setq gnus-summary-buffer (current-buffer))
13298     ;; Make sure the connection to the server is alive.
13299     (unless (gnus-server-opened
13300              (gnus-find-method-for-group gnus-newsgroup-name))
13301       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13302       (gnus-request-group gnus-newsgroup-name t))
13303     (let* ((article (if header (mail-header-number header) article))
13304            (summary-buffer (current-buffer))
13305            (internal-hook gnus-article-internal-prepare-hook)
13306            (group gnus-newsgroup-name)
13307            result)
13308       (save-excursion
13309         (gnus-article-setup-buffer)
13310         (set-buffer gnus-article-buffer)
13311         ;; Deactivate active regions.
13312         (when (and (boundp 'transient-mark-mode)
13313                    transient-mark-mode)
13314           (setq mark-active nil))
13315         (if (not (setq result (let ((buffer-read-only nil))
13316                                 (gnus-request-article-this-buffer
13317                                  article group))))
13318             ;; There is no such article.
13319             (save-excursion
13320               (when (and (numberp article)
13321                          (not (memq article gnus-newsgroup-sparse)))
13322                 (setq gnus-article-current
13323                       (cons gnus-newsgroup-name article))
13324                 (set-buffer gnus-summary-buffer)
13325                 (setq gnus-current-article article)
13326                 (gnus-summary-mark-article article gnus-canceled-mark))
13327               (unless (memq article gnus-newsgroup-sparse)
13328                 (gnus-message
13329                  1 "No such article (may have expired or been canceled)")
13330                 (ding)
13331                 nil))
13332           (if (or (eq result 'pseudo) (eq result 'nneething))
13333               (progn
13334                 (save-excursion
13335                   (set-buffer summary-buffer)
13336                   (setq gnus-last-article gnus-current-article
13337                         gnus-newsgroup-history (cons gnus-current-article
13338                                                      gnus-newsgroup-history)
13339                         gnus-current-article 0
13340                         gnus-current-headers nil
13341                         gnus-article-current nil)
13342                   (if (eq result 'nneething)
13343                       (gnus-configure-windows 'summary)
13344                     (gnus-configure-windows 'article))
13345                   (gnus-set-global-variables))
13346                 (gnus-set-mode-line 'article))
13347             ;; The result from the `request' was an actual article -
13348             ;; or at least some text that is now displayed in the
13349             ;; article buffer.
13350             (if (and (numberp article)
13351                      (not (eq article gnus-current-article)))
13352                 ;; Seems like a new article has been selected.
13353                 ;; `gnus-current-article' must be an article number.
13354                 (save-excursion
13355                   (set-buffer summary-buffer)
13356                   (setq gnus-last-article gnus-current-article
13357                         gnus-newsgroup-history (cons gnus-current-article
13358                                                      gnus-newsgroup-history)
13359                         gnus-current-article article
13360                         gnus-current-headers
13361                         (gnus-summary-article-header gnus-current-article)
13362                         gnus-article-current
13363                         (cons gnus-newsgroup-name gnus-current-article))
13364                   (unless (vectorp gnus-current-headers)
13365                     (setq gnus-current-headers nil))
13366                   (gnus-summary-show-thread)
13367                   (run-hooks 'gnus-mark-article-hook)
13368                   (gnus-set-mode-line 'summary)
13369                   (and (gnus-visual-p 'article-highlight 'highlight)
13370                        (run-hooks 'gnus-visual-mark-article-hook))
13371                   ;; Set the global newsgroup variables here.
13372                   ;; Suggested by Jim Sisolak
13373                   ;; <sisolak@trans4.neep.wisc.edu>.
13374                   (gnus-set-global-variables)
13375                   (setq gnus-have-all-headers
13376                         (or all-headers gnus-show-all-headers))
13377                   (and gnus-use-cache
13378                        (vectorp (gnus-summary-article-header article))
13379                        (gnus-cache-possibly-enter-article
13380                         group article
13381                         (gnus-summary-article-header article)
13382                         (memq article gnus-newsgroup-marked)
13383                         (memq article gnus-newsgroup-dormant)
13384                         (memq article gnus-newsgroup-unreads)))))
13385             ;; Hooks for getting information from the article.
13386             ;; This hook must be called before being narrowed.
13387             (let (buffer-read-only)
13388               (run-hooks 'internal-hook)
13389               (run-hooks 'gnus-article-prepare-hook)
13390               ;; Decode MIME message.
13391               (if gnus-show-mime
13392                   (if (or (not gnus-strict-mime)
13393                           (gnus-fetch-field "Mime-Version"))
13394                       (funcall gnus-show-mime-method)
13395                     (funcall gnus-decode-encoded-word-method)))
13396               ;; Perform the article display hooks.
13397               (run-hooks 'gnus-article-display-hook))
13398             ;; Do page break.
13399             (goto-char (point-min))
13400             (and gnus-break-pages (gnus-narrow-to-page))
13401             (gnus-set-mode-line 'article)
13402             (gnus-configure-windows 'article)
13403             (goto-char (point-min))
13404             t))))))
13405
13406 (defun gnus-article-show-all-headers ()
13407   "Show all article headers in article mode buffer."
13408   (save-excursion
13409     (gnus-article-setup-buffer)
13410     (set-buffer gnus-article-buffer)
13411     (let ((buffer-read-only nil))
13412       (remove-text-properties (point-min) (point-max)
13413                               gnus-hidden-properties))))
13414
13415 (defun gnus-article-hide-headers-if-wanted ()
13416   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13417 Provided for backwards compatibility."
13418   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13419       gnus-inhibit-hiding
13420       (gnus-article-hide-headers)))
13421
13422 (defun gnus-article-hide-headers (&optional arg delete)
13423   "Toggle whether to hide unwanted headers and possibly sort them as well.
13424 If given a negative prefix, always show; if given a positive prefix,
13425 always hide."
13426   (interactive "P")
13427   (unless (gnus-article-check-hidden-text 'headers arg)
13428     ;; This function might be inhibited.
13429     (unless gnus-inhibit-hiding
13430       (save-excursion
13431         (set-buffer gnus-article-buffer)
13432         (save-restriction
13433           (let ((buffer-read-only nil)
13434                 (ignored (when (not (stringp gnus-visible-headers))
13435                            (cond ((stringp gnus-ignored-headers)
13436                                   gnus-ignored-headers)
13437                                  ((listp gnus-ignored-headers)
13438                                   (mapconcat 'identity gnus-ignored-headers
13439                                              "\\|")))))
13440                 (visible
13441                  (cond ((stringp gnus-visible-headers)
13442                         gnus-visible-headers)
13443                        ((and gnus-visible-headers
13444                              (listp gnus-visible-headers))
13445                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13446                 want-list beg)
13447             ;; First we narrow to just the headers.
13448             (widen)
13449             (goto-char (point-min))
13450             ;; Hide any "From " lines at the beginning of (mail) articles.
13451             (while (looking-at "From ")
13452               (forward-line 1))
13453             (unless (bobp)
13454               (add-text-properties
13455                (point-min) (point)
13456                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13457             ;; Then treat the rest of the header lines.
13458             (narrow-to-region
13459              (point)
13460              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13461             ;; Then we use the two regular expressions
13462             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13463             ;; select which header lines is to remain visible in the
13464             ;; article buffer.
13465             (goto-char (point-min))
13466             (while (re-search-forward "^[^ \t]*:" nil t)
13467               (beginning-of-line)
13468               ;; We add the headers we want to keep to a list and delete
13469               ;; them from the buffer.
13470               (if (or (and visible (looking-at visible))
13471                       (and ignored (not (looking-at ignored))))
13472                   (progn
13473                     (push (buffer-substring
13474                            (setq beg (point))
13475                            (progn
13476                              (forward-line 1)
13477                              ;; Be sure to get multi-line headers...
13478                              (re-search-forward "^[^ \t]*:" nil t)
13479                              (beginning-of-line)
13480                              (point)))
13481                           want-list)
13482                     (delete-region beg (point)))
13483                 (forward-line 1)))
13484             ;; Sort the headers that we want to display.
13485             (setq want-list (sort want-list 'gnus-article-header-less))
13486             (goto-char (point-min))
13487             (while want-list
13488               (insert (pop want-list)))
13489             ;; We make the unwanted headers invisible.
13490             (if delete
13491                 (delete-region (point-min) (point-max))
13492               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13493               (add-text-properties
13494                (point) (point-max)
13495                (nconc (list 'gnus-type 'headers)
13496                       gnus-hidden-properties)))))))))
13497
13498 (defsubst gnus-article-header-rank (header)
13499   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13500   (let ((list gnus-sorted-header-list)
13501         (i 0))
13502     (while list
13503       (when (string-match (car list) header)
13504         (setq list nil))
13505       (setq list (cdr list))
13506       (incf i))
13507     i))
13508
13509 (defun gnus-article-header-less (h1 h2)
13510   "Say whether string H1 is \"less\" than string H2."
13511   (< (gnus-article-header-rank h1)
13512      (gnus-article-header-rank h2)))
13513
13514 (defun gnus-article-hide-boring-headers (&optional arg)
13515   "Toggle hiding of headers that aren't very interesting.
13516 If given a negative prefix, always show; if given a positive prefix,
13517 always hide."
13518   (interactive "P")
13519   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13520     (save-excursion
13521       (set-buffer gnus-article-buffer)
13522       (save-restriction
13523         (let ((buffer-read-only nil)
13524               (list gnus-boring-article-headers)
13525               (inhibit-point-motion-hooks t)
13526               elem)
13527           (nnheader-narrow-to-headers)
13528           (while list
13529             (setq elem (pop list))
13530             (goto-char (point-min))
13531             (cond
13532              ;; Hide empty headers.
13533              ((eq elem 'empty)
13534               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13535                 (forward-line -1)
13536                 (add-text-properties
13537                  (progn (beginning-of-line) (point))
13538                  (progn 
13539                    (end-of-line)
13540                    (if (re-search-forward "^[^ \t]" nil t)
13541                        (match-beginning 0)
13542                      (point-max)))
13543                  (nconc (list 'gnus-type 'boring-headers)
13544                         gnus-hidden-properties))))
13545              ;; Hide boring Newsgroups header.
13546              ((eq elem 'newsgroups)
13547               (when (equal (mail-fetch-field "newsgroups")
13548                            (gnus-group-real-name gnus-newsgroup-name))
13549                 (gnus-article-hide-header "newsgroups")))
13550              ((eq elem 'followup-to)
13551               (when (equal (mail-fetch-field "followup-to")
13552                            (mail-fetch-field "newsgroups"))
13553                 (gnus-article-hide-header "followup-to")))
13554              ((eq elem 'reply-to)
13555               (let ((from (mail-fetch-field "from"))
13556                     (reply-to (mail-fetch-field "reply-to")))
13557                 (when (and
13558                        from reply-to
13559                        (equal 
13560                         (nth 1 (mail-extract-address-components from))
13561                         (nth 1 (mail-extract-address-components reply-to))))
13562                   (gnus-article-hide-header "reply-to"))))
13563              ((eq elem 'date)
13564               (let ((date (mail-fetch-field "date")))
13565                 (when (and date
13566                            (< (gnus-days-between date (current-time-string))
13567                               4))
13568                   (gnus-article-hide-header "date")))))))))))
13569
13570 (defun gnus-article-hide-header (header)
13571   (save-excursion
13572     (goto-char (point-min))
13573     (when (re-search-forward (concat "^" header ":") nil t)
13574       (add-text-properties
13575        (progn (beginning-of-line) (point))
13576        (progn 
13577          (end-of-line)
13578          (if (re-search-forward "^[^ \t]" nil t)
13579              (match-beginning 0)
13580            (point-max)))
13581        (nconc (list 'gnus-type 'boring-headers)
13582               gnus-hidden-properties)))))
13583
13584 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13585 (defun gnus-article-treat-overstrike ()
13586   "Translate overstrikes into bold text."
13587   (interactive)
13588   (save-excursion
13589     (set-buffer gnus-article-buffer)
13590     (let ((buffer-read-only nil))
13591       (while (search-forward "\b" nil t)
13592         (let ((next (following-char))
13593               (previous (char-after (- (point) 2))))
13594           (cond ((eq next previous)
13595                  (put-text-property (- (point) 2) (point) 'invisible t)
13596                  (put-text-property (point) (1+ (point)) 'face 'bold))
13597                 ((eq next ?_)
13598                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13599                  (put-text-property
13600                   (- (point) 2) (1- (point)) 'face 'underline))
13601                 ((eq previous ?_)
13602                  (put-text-property (- (point) 2) (point) 'invisible t)
13603                  (put-text-property
13604                   (point) (1+ (point))  'face 'underline))))))))
13605
13606 (defun gnus-article-word-wrap ()
13607   "Format too long lines."
13608   (interactive)
13609   (save-excursion
13610     (set-buffer gnus-article-buffer)
13611     (let ((buffer-read-only nil))
13612       (widen)
13613       (goto-char (point-min))
13614       (search-forward "\n\n" nil t)
13615       (end-of-line 1)
13616       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13617             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13618             (adaptive-fill-mode t))
13619         (while (not (eobp))
13620           (and (>= (current-column) (min fill-column (window-width)))
13621                (/= (preceding-char) ?:)
13622                (fill-paragraph nil))
13623           (end-of-line 2))))))
13624
13625 (defun gnus-article-remove-cr ()
13626   "Remove carriage returns from an article."
13627   (interactive)
13628   (save-excursion
13629     (set-buffer gnus-article-buffer)
13630     (let ((buffer-read-only nil))
13631       (goto-char (point-min))
13632       (while (search-forward "\r" nil t)
13633         (replace-match "" t t)))))
13634
13635 (defun gnus-article-remove-trailing-blank-lines ()
13636   "Remove all trailing blank lines from the article."
13637   (interactive)
13638   (save-excursion
13639     (set-buffer gnus-article-buffer)
13640     (let ((buffer-read-only nil))
13641       (goto-char (point-max))
13642       (delete-region
13643        (point)
13644        (progn
13645          (while (looking-at "^[ \t]*$")
13646            (forward-line -1))
13647          (forward-line 1)
13648          (point))))))
13649
13650 (defun gnus-article-display-x-face (&optional force)
13651   "Look for an X-Face header and display it if present."
13652   (interactive (list 'force))
13653   (save-excursion
13654     (set-buffer gnus-article-buffer)
13655     ;; Delete the old process, if any.
13656     (when (process-status "gnus-x-face")
13657       (delete-process "gnus-x-face"))
13658     (let ((inhibit-point-motion-hooks t)
13659           (case-fold-search nil)
13660           from)
13661       (save-restriction
13662         (nnheader-narrow-to-headers)
13663         (setq from (mail-fetch-field "from"))
13664         (goto-char (point-min))
13665         (when (and gnus-article-x-face-command
13666                    (or force
13667                        ;; Check whether this face is censored.
13668                        (not gnus-article-x-face-too-ugly)
13669                        (and gnus-article-x-face-too-ugly from
13670                             (not (string-match gnus-article-x-face-too-ugly
13671                                                from))))
13672                    ;; Has to be present.
13673                    (re-search-forward "^X-Face: " nil t))
13674           ;; We now have the area of the buffer where the X-Face is stored.
13675           (let ((beg (point))
13676                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13677             ;; We display the face.
13678             (if (symbolp gnus-article-x-face-command)
13679                 ;; The command is a lisp function, so we call it.
13680                 (if (gnus-functionp gnus-article-x-face-command)
13681                     (funcall gnus-article-x-face-command beg end)
13682                   (error "%s is not a function" gnus-article-x-face-command))
13683               ;; The command is a string, so we interpret the command
13684               ;; as a, well, command, and fork it off.
13685               (let ((process-connection-type nil))
13686                 (process-kill-without-query
13687                  (start-process
13688                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13689                 (process-send-region "gnus-x-face" beg end)
13690                 (process-send-eof "gnus-x-face")))))))))
13691
13692 (defun gnus-headers-decode-quoted-printable ()
13693   "Hack to remove QP encoding from headers."
13694   (let ((case-fold-search t)
13695         (inhibit-point-motion-hooks t)
13696         string)
13697     (goto-char (point-min))
13698     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13699       (setq string (match-string 1))
13700       (narrow-to-region (match-beginning 0) (match-end 0))
13701       (delete-region (point-min) (point-max))
13702       (insert string)
13703       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13704       (subst-char-in-region (point-min) (point-max) ?_ ? )
13705       (widen)
13706       (goto-char (point-min)))))
13707
13708 (defun gnus-article-de-quoted-unreadable (&optional force)
13709   "Do a naive translation of a quoted-printable-encoded article.
13710 This is in no way, shape or form meant as a replacement for real MIME
13711 processing, but is simply a stop-gap measure until MIME support is
13712 written.
13713 If FORCE, decode the article whether it is marked as quoted-printable
13714 or not."
13715   (interactive (list 'force))
13716   (save-excursion
13717     (set-buffer gnus-article-buffer)
13718     (let ((case-fold-search t)
13719           (buffer-read-only nil)
13720           (type (gnus-fetch-field "content-transfer-encoding")))
13721       (when (or force
13722                 (and type (string-match "quoted-printable" type)))
13723         (gnus-headers-decode-quoted-printable)
13724         (goto-char (point-min))
13725         (search-forward "\n\n" nil 'move)
13726         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13727
13728 (defun gnus-mime-decode-quoted-printable (from to)
13729   "Decode Quoted-Printable in the region between FROM and TO."
13730   (goto-char from)
13731   (while (search-forward "=" to t)
13732     (cond ((eq (following-char) ?\n)
13733            (delete-char -1)
13734            (delete-char 1))
13735           ((looking-at "[0-9A-F][0-9A-F]")
13736            (delete-char -1)
13737            (insert (hexl-hex-string-to-integer
13738                     (buffer-substring (point) (+ 2 (point)))))
13739            (delete-char 2))
13740           ((looking-at "=")
13741            (delete-char 1))
13742           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13743
13744 (defun gnus-article-hide-pgp (&optional arg)
13745   "Toggle hiding of any PGP headers and signatures in the current article.
13746 If given a negative prefix, always show; if given a positive prefix,
13747 always hide."
13748   (interactive "P")
13749   (unless (gnus-article-check-hidden-text 'pgp arg)
13750     (save-excursion
13751       (set-buffer gnus-article-buffer)
13752       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13753             buffer-read-only beg end)
13754         (widen)
13755         (goto-char (point-min))
13756         ;; Hide the "header".
13757         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13758              (add-text-properties (match-beginning 0) (match-end 0) props))
13759         (setq beg (point))
13760         ;; Hide the actual signature.
13761         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13762              (setq end (match-beginning 0))
13763              (add-text-properties
13764               (match-beginning 0)
13765               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13766                   (match-end 0)
13767                 ;; Perhaps we shouldn't hide to the end of the buffer
13768                 ;; if there is no end to the signature?
13769                 (point-max))
13770               props))
13771         ;; Hide "- " PGP quotation markers.
13772         (when (and beg end)
13773           (narrow-to-region beg end)
13774           (goto-char (point-min))
13775           (while (re-search-forward "^- " nil t)
13776             (add-text-properties (match-beginning 0) (match-end 0) props))
13777           (widen))))))
13778
13779 (defun gnus-article-hide-signature (&optional arg)
13780   "Hide the signature in the current article.
13781 If given a negative prefix, always show; if given a positive prefix,
13782 always hide."
13783   (interactive "P")
13784   (unless (gnus-article-check-hidden-text 'signature arg)
13785     (save-excursion
13786       (set-buffer gnus-article-buffer)
13787       (save-restriction
13788         (let ((buffer-read-only nil))
13789           (when (gnus-narrow-to-signature)
13790             (add-text-properties
13791              (point-min) (point-max)
13792              (nconc (list 'gnus-type 'signature)
13793                     gnus-hidden-properties))))))))
13794
13795 (defun gnus-article-strip-leading-blank-lines ()
13796   "Remove all blank lines from the beginning of the article."
13797   (interactive)
13798   (save-excursion
13799     (set-buffer gnus-article-buffer)
13800     (let (buffer-read-only)
13801       (goto-char (point-min))
13802       (when (search-forward "\n\n" nil t)
13803         (while (looking-at "[ \t]$")
13804           (gnus-delete-line))))))
13805
13806 (defun gnus-narrow-to-signature ()
13807   "Narrow to the signature."
13808   (widen)
13809   (goto-char (point-max))
13810   (when (re-search-backward gnus-signature-separator nil t)
13811     (forward-line 1)
13812     (when (or (null gnus-signature-limit)
13813               (and (numberp gnus-signature-limit)
13814                    (< (- (point-max) (point)) gnus-signature-limit))
13815               (and (gnus-functionp gnus-signature-limit)
13816                    (funcall gnus-signature-limit))
13817               (and (stringp gnus-signature-limit)
13818                    (not (re-search-forward gnus-signature-limit nil t))))
13819       (narrow-to-region (point) (point-max))
13820       t)))
13821
13822 (defun gnus-article-check-hidden-text (type arg)
13823   "Return nil if hiding is necessary."
13824   (save-excursion
13825     (set-buffer gnus-article-buffer)
13826     (let ((hide (gnus-article-hidden-text-p type)))
13827       (cond ((or (and (null arg) (eq hide 'hidden))
13828                  (and arg (< 0 (prefix-numeric-value arg))))
13829              (gnus-article-show-hidden-text type))
13830             ((eq hide 'shown)
13831              (gnus-article-show-hidden-text type t))
13832             (t nil)))))
13833
13834 (defun gnus-article-hidden-text-p (type)
13835   "Say whether the current buffer contains hidden text of type TYPE."
13836   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13837     (when pos
13838       (if (get-text-property pos 'invisible)
13839           'hidden
13840         'shown))))
13841
13842 (defun gnus-article-hide (&optional arg force)
13843   "Hide all the gruft in the current article.
13844 This means that PGP stuff, signatures, cited text and (some)
13845 headers will be hidden.
13846 If given a prefix, show the hidden text instead."
13847   (interactive (list current-prefix-arg 'force))
13848   (gnus-article-hide-headers arg)
13849   (gnus-article-hide-pgp arg)
13850   (gnus-article-hide-citation-maybe arg force)
13851   (gnus-article-hide-signature arg))
13852
13853 (defun gnus-article-show-hidden-text (type &optional hide)
13854   "Show all hidden text of type TYPE.
13855 If HIDE, hide the text instead."
13856   (save-excursion
13857     (set-buffer gnus-article-buffer)
13858     (let ((buffer-read-only nil)
13859           (inhibit-point-motion-hooks t)
13860           (beg (point-min)))
13861       (while (gnus-goto-char (text-property-any
13862                               beg (point-max) 'gnus-type type))
13863         (setq beg (point))
13864         (forward-char)
13865         (if hide
13866             (add-text-properties beg (point) gnus-hidden-properties)
13867           (remove-text-properties beg (point) gnus-hidden-properties))
13868         (setq beg (point)))
13869       t)))
13870
13871 (defvar gnus-article-time-units
13872   `((year . ,(* 365.25 24 60 60))
13873     (week . ,(* 7 24 60 60))
13874     (day . ,(* 24 60 60))
13875     (hour . ,(* 60 60))
13876     (minute . 60)
13877     (second . 1))
13878   "Mapping from time units to seconds.")
13879
13880 (defun gnus-article-date-ut (&optional type highlight)
13881   "Convert DATE date to universal time in the current article.
13882 If TYPE is `local', convert to local time; if it is `lapsed', output
13883 how much time has lapsed since DATE."
13884   (interactive (list 'ut t))
13885   (let* ((header (or gnus-current-headers
13886                      (gnus-summary-article-header) ""))
13887          (date (and (vectorp header) (mail-header-date header)))
13888          (date-regexp "^Date: \\|^X-Sent: ")
13889          (now (current-time))
13890          (inhibit-point-motion-hooks t))
13891     (when (and date (not (string= date "")))
13892       (save-excursion
13893         (set-buffer gnus-article-buffer)
13894         (save-restriction
13895           (nnheader-narrow-to-headers)
13896           (let ((buffer-read-only nil))
13897             ;; Delete any old Date headers.
13898             (if (zerop (nnheader-remove-header date-regexp t))
13899                 (beginning-of-line)
13900               (goto-char (point-max)))
13901             (insert
13902              (cond
13903               ;; Convert to the local timezone.  We have to slap a
13904               ;; `condition-case' round the calls to the timezone
13905               ;; functions since they aren't particularly resistant to
13906               ;; buggy dates.
13907               ((eq type 'local)
13908                (concat "Date: " (condition-case ()
13909                                     (timezone-make-date-arpa-standard date)
13910                                   (error date))
13911                        "\n"))
13912               ;; Convert to Universal Time.
13913               ((eq type 'ut)
13914                (concat "Date: "
13915                        (condition-case ()
13916                            (timezone-make-date-arpa-standard date nil "UT")
13917                          (error date))
13918                        "\n"))
13919               ;; Get the original date from the article.
13920               ((eq type 'original)
13921                (concat "Date: " date "\n"))
13922               ;; Do an X-Sent lapsed format.
13923               ((eq type 'lapsed)
13924                ;; If the date is seriously mangled, the timezone
13925                ;; functions are liable to bug out, so we condition-case
13926                ;; the entire thing.
13927                (let* ((real-time
13928                        (condition-case ()
13929                            (gnus-time-minus
13930                             (gnus-encode-date
13931                              (timezone-make-date-arpa-standard
13932                               (current-time-string now)
13933                               (current-time-zone now) "UT"))
13934                             (gnus-encode-date
13935                              (timezone-make-date-arpa-standard
13936                               date nil "UT")))
13937                          (error '(0 0))))
13938                       (real-sec (+ (* (float (car real-time)) 65536)
13939                                    (cadr real-time)))
13940                       (sec (abs real-sec))
13941                       num prev)
13942                  (if (zerop sec)
13943                      "X-Sent: Now\n"
13944                    (concat
13945                     "X-Sent: "
13946                     ;; This is a bit convoluted, but basically we go
13947                     ;; through the time units for years, weeks, etc,
13948                     ;; and divide things to see whether that results
13949                     ;; in positive answers.
13950                     (mapconcat
13951                      (lambda (unit)
13952                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13953                            ;; The (remaining) seconds are too few to
13954                            ;; be divided into this time unit.
13955                            ""
13956                          ;; It's big enough, so we output it.
13957                          (setq sec (- sec (* num (cdr unit))))
13958                          (prog1
13959                              (concat (if prev ", " "") (int-to-string
13960                                                         (floor num))
13961                                      " " (symbol-name (car unit))
13962                                      (if (> num 1) "s" ""))
13963                            (setq prev t))))
13964                      gnus-article-time-units "")
13965                     ;; If dates are odd, then it might appear like the
13966                     ;; article was sent in the future.
13967                     (if (> real-sec 0)
13968                         " ago\n"
13969                       " in the future\n")))))
13970               (t
13971                (error "Unknown conversion type: %s" type)))))
13972           ;; Do highlighting.
13973           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13974             (gnus-article-highlight-headers)))))))
13975
13976 (defun gnus-article-date-local (&optional highlight)
13977   "Convert the current article date to the local timezone."
13978   (interactive (list t))
13979   (gnus-article-date-ut 'local highlight))
13980
13981 (defun gnus-article-date-original (&optional highlight)
13982   "Convert the current article date to what it was originally.
13983 This is only useful if you have used some other date conversion
13984 function and want to see what the date was before converting."
13985   (interactive (list t))
13986   (gnus-article-date-ut 'original highlight))
13987
13988 (defun gnus-article-date-lapsed (&optional highlight)
13989   "Convert the current article date to time lapsed since it was sent."
13990   (interactive (list t))
13991   (gnus-article-date-ut 'lapsed highlight))
13992
13993 (defun gnus-article-maybe-highlight ()
13994   "Do some article highlighting if `gnus-visual' is non-nil."
13995   (if (gnus-visual-p 'article-highlight 'highlight)
13996       (gnus-article-highlight-some)))
13997
13998 ;; Article savers.
13999
14000 (defun gnus-output-to-rmail (file-name)
14001   "Append the current article to an Rmail file named FILE-NAME."
14002   (require 'rmail)
14003   ;; Most of these codes are borrowed from rmailout.el.
14004   (setq file-name (expand-file-name file-name))
14005   (setq rmail-default-rmail-file file-name)
14006   (let ((artbuf (current-buffer))
14007         (tmpbuf (get-buffer-create " *Gnus-output*")))
14008     (save-excursion
14009       (or (get-file-buffer file-name)
14010           (file-exists-p file-name)
14011           (if (gnus-yes-or-no-p
14012                (concat "\"" file-name "\" does not exist, create it? "))
14013               (let ((file-buffer (create-file-buffer file-name)))
14014                 (save-excursion
14015                   (set-buffer file-buffer)
14016                   (rmail-insert-rmail-file-header)
14017                   (let ((require-final-newline nil))
14018                     (write-region (point-min) (point-max) file-name t 1)))
14019                 (kill-buffer file-buffer))
14020             (error "Output file does not exist")))
14021       (set-buffer tmpbuf)
14022       (buffer-disable-undo (current-buffer))
14023       (erase-buffer)
14024       (insert-buffer-substring artbuf)
14025       (gnus-convert-article-to-rmail)
14026       ;; Decide whether to append to a file or to an Emacs buffer.
14027       (let ((outbuf (get-file-buffer file-name)))
14028         (if (not outbuf)
14029             (append-to-file (point-min) (point-max) file-name)
14030           ;; File has been visited, in buffer OUTBUF.
14031           (set-buffer outbuf)
14032           (let ((buffer-read-only nil)
14033                 (msg (and (boundp 'rmail-current-message)
14034                           (symbol-value 'rmail-current-message))))
14035             ;; If MSG is non-nil, buffer is in RMAIL mode.
14036             (if msg
14037                 (progn (widen)
14038                        (narrow-to-region (point-max) (point-max))))
14039             (insert-buffer-substring tmpbuf)
14040             (if msg
14041                 (progn
14042                   (goto-char (point-min))
14043                   (widen)
14044                   (search-backward "\^_")
14045                   (narrow-to-region (point) (point-max))
14046                   (goto-char (1+ (point-min)))
14047                   (rmail-count-new-messages t)
14048                   (rmail-show-message msg)))))))
14049     (kill-buffer tmpbuf)))
14050
14051 (defun gnus-output-to-file (file-name)
14052   "Append the current article to a file named FILE-NAME."
14053   (setq file-name (expand-file-name file-name))
14054   (let ((artbuf (current-buffer))
14055         (tmpbuf (get-buffer-create " *Gnus-output*")))
14056     (save-excursion
14057       (set-buffer tmpbuf)
14058       (buffer-disable-undo (current-buffer))
14059       (erase-buffer)
14060       (insert-buffer-substring artbuf)
14061       ;; Append newline at end of the buffer as separator, and then
14062       ;; save it to file.
14063       (goto-char (point-max))
14064       (insert "\n")
14065       (append-to-file (point-min) (point-max) file-name))
14066     (kill-buffer tmpbuf)))
14067
14068 (defun gnus-convert-article-to-rmail ()
14069   "Convert article in current buffer to Rmail message format."
14070   (let ((buffer-read-only nil))
14071     ;; Convert article directly into Babyl format.
14072     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14073     (goto-char (point-min))
14074     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14075     (while (search-forward "\n\^_" nil t) ;single char
14076       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14077     (goto-char (point-max))
14078     (insert "\^_")))
14079
14080 (defun gnus-narrow-to-page (&optional arg)
14081   "Narrow the article buffer to a page.
14082 If given a numerical ARG, move forward ARG pages."
14083   (interactive "P")
14084   (setq arg (if arg (prefix-numeric-value arg) 0))
14085   (save-excursion
14086     (set-buffer gnus-article-buffer)
14087     (goto-char (point-min))
14088     (widen)
14089     (when (gnus-visual-p 'page-marker)
14090       (let ((buffer-read-only nil))
14091         (gnus-remove-text-with-property 'gnus-prev)
14092         (gnus-remove-text-with-property 'gnus-next)))
14093     (when
14094         (cond ((< arg 0)
14095                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14096               ((> arg 0)
14097                (re-search-forward page-delimiter nil 'move arg)))
14098       (goto-char (match-end 0)))
14099     (narrow-to-region
14100      (point)
14101      (if (re-search-forward page-delimiter nil 'move)
14102          (match-beginning 0)
14103        (point)))
14104     (when (and (gnus-visual-p 'page-marker)
14105                (not (= (point-min) 1)))
14106       (save-excursion
14107         (goto-char (point-min))
14108         (gnus-insert-prev-page-button)))
14109     (when (and (gnus-visual-p 'page-marker)
14110                (not (= (1- (point-max)) (buffer-size))))
14111       (save-excursion
14112         (goto-char (point-max))
14113         (gnus-insert-next-page-button)))))
14114
14115 ;; Article mode commands
14116
14117 (defun gnus-article-goto-next-page ()
14118   "Show the next page of the article."
14119   (interactive)
14120   (when (gnus-article-next-page)
14121     (gnus-article-read-summary-keys nil ?n)))
14122
14123 (defun gnus-article-goto-prev-page ()
14124   "Show the next page of the article."
14125   (interactive)
14126   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14127     (gnus-article-prev-page nil)))
14128
14129 (defun gnus-article-next-page (&optional lines)
14130   "Show the next page of the current article.
14131 If end of article, return non-nil.  Otherwise return nil.
14132 Argument LINES specifies lines to be scrolled up."
14133   (interactive "p")
14134   (move-to-window-line -1)
14135   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14136   (if (save-excursion
14137         (end-of-line)
14138         (and (pos-visible-in-window-p)  ;Not continuation line.
14139              (eobp)))
14140       ;; Nothing in this page.
14141       (if (or (not gnus-break-pages)
14142               (save-excursion
14143                 (save-restriction
14144                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14145           t                             ;Nothing more.
14146         (gnus-narrow-to-page 1)         ;Go to next page.
14147         nil)
14148     ;; More in this page.
14149     (condition-case ()
14150         (scroll-up lines)
14151       (end-of-buffer
14152        ;; Long lines may cause an end-of-buffer error.
14153        (goto-char (point-max))))
14154     (move-to-window-line 0)
14155     nil))
14156
14157 (defun gnus-article-prev-page (&optional lines)
14158   "Show previous page of current article.
14159 Argument LINES specifies lines to be scrolled down."
14160   (interactive "p")
14161   (move-to-window-line 0)
14162   (if (and gnus-break-pages
14163            (bobp)
14164            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14165       (progn
14166         (gnus-narrow-to-page -1)        ;Go to previous page.
14167         (goto-char (point-max))
14168         (recenter -1))
14169     (prog1
14170         (condition-case ()
14171             (scroll-down lines)
14172           (error nil))
14173       (move-to-window-line 0))))
14174
14175 (defun gnus-article-refer-article ()
14176   "Read article specified by message-id around point."
14177   (interactive)
14178   (let ((point (point)))
14179     (search-forward ">" nil t)          ;Move point to end of "<....>".
14180     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14181         (let ((message-id (match-string 1)))
14182           (goto-char point)
14183           (set-buffer gnus-summary-buffer)
14184           (gnus-summary-refer-article message-id))
14185       (goto-char (point))
14186       (error "No references around point"))))
14187
14188 (defun gnus-article-show-summary ()
14189   "Reconfigure windows to show summary buffer."
14190   (interactive)
14191   (gnus-configure-windows 'article)
14192   (gnus-summary-goto-subject gnus-current-article))
14193
14194 (defun gnus-article-describe-briefly ()
14195   "Describe article mode commands briefly."
14196   (interactive)
14197   (gnus-message 6
14198                 (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")))
14199
14200 (defun gnus-article-summary-command ()
14201   "Execute the last keystroke in the summary buffer."
14202   (interactive)
14203   (let ((obuf (current-buffer))
14204         (owin (current-window-configuration))
14205         func)
14206     (switch-to-buffer gnus-summary-buffer 'norecord)
14207     (setq func (lookup-key (current-local-map) (this-command-keys)))
14208     (call-interactively func)
14209     (set-buffer obuf)
14210     (set-window-configuration owin)
14211     (set-window-point (get-buffer-window (current-buffer)) (point))))
14212
14213 (defun gnus-article-summary-command-nosave ()
14214   "Execute the last keystroke in the summary buffer."
14215   (interactive)
14216   (let (func)
14217     (pop-to-buffer gnus-summary-buffer 'norecord)
14218     (setq func (lookup-key (current-local-map) (this-command-keys)))
14219     (call-interactively func)))
14220
14221 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14222   "Read a summary buffer key sequence and execute it from the article buffer."
14223   (interactive "P")
14224   (let ((nosaves
14225          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14226            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14227            "=" "^" "\M-^"))
14228         keys)
14229     (save-excursion
14230       (set-buffer gnus-summary-buffer)
14231       (push (or key last-command-event) unread-command-events)
14232       (setq keys (read-key-sequence nil)))
14233     (message "")
14234
14235     (if (member keys nosaves)
14236         (let (func)
14237           (pop-to-buffer gnus-summary-buffer 'norecord)
14238           (if (setq func (lookup-key (current-local-map) keys))
14239               (call-interactively func)
14240             (ding)))
14241       (let ((obuf (current-buffer))
14242             (owin (current-window-configuration))
14243             (opoint (point))
14244             func in-buffer)
14245         (if not-restore-window
14246             (pop-to-buffer gnus-summary-buffer 'norecord)
14247           (switch-to-buffer gnus-summary-buffer 'norecord))
14248         (setq in-buffer (current-buffer))
14249         (if (setq func (lookup-key (current-local-map) keys))
14250             (call-interactively func)
14251           (ding))
14252         (when (eq in-buffer (current-buffer))
14253           (set-buffer obuf)
14254           (unless not-restore-window
14255             (set-window-configuration owin))
14256           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14257
14258 \f
14259 ;;;
14260 ;;; Kill file handling.
14261 ;;;
14262
14263 ;;;###autoload
14264 (defalias 'gnus-batch-kill 'gnus-batch-score)
14265 ;;;###autoload
14266 (defun gnus-batch-score ()
14267   "Run batched scoring.
14268 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14269 Newsgroups is a list of strings in Bnews format.  If you want to score
14270 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14271 score the alt hierarchy, you'd say \"!alt.all\"."
14272   (interactive)
14273   (let* ((yes-and-no
14274           (gnus-newsrc-parse-options
14275            (apply (function concat)
14276                   (mapcar (lambda (g) (concat g " "))
14277                           command-line-args-left))))
14278          (gnus-expert-user t)
14279          (nnmail-spool-file nil)
14280          (gnus-use-dribble-file nil)
14281          (yes (car yes-and-no))
14282          (no (cdr yes-and-no))
14283          group newsrc entry
14284          ;; Disable verbose message.
14285          gnus-novice-user gnus-large-newsgroup)
14286     ;; Eat all arguments.
14287     (setq command-line-args-left nil)
14288     ;; Start Gnus.
14289     (gnus)
14290     ;; Apply kills to specified newsgroups in command line arguments.
14291     (setq newsrc (cdr gnus-newsrc-alist))
14292     (while newsrc
14293       (setq group (caar newsrc))
14294       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14295       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14296                (and (car entry)
14297                     (or (eq (car entry) t)
14298                         (not (zerop (car entry)))))
14299                (if yes (string-match yes group) t)
14300                (or (null no) (not (string-match no group))))
14301           (progn
14302             (gnus-summary-read-group group nil t nil t)
14303             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14304                  (gnus-summary-exit))))
14305       (setq newsrc (cdr newsrc)))
14306     ;; Exit Emacs.
14307     (switch-to-buffer gnus-group-buffer)
14308     (gnus-group-save-newsrc)))
14309
14310 (defun gnus-apply-kill-file ()
14311   "Apply a kill file to the current newsgroup.
14312 Returns the number of articles marked as read."
14313   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14314           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14315       (gnus-apply-kill-file-internal)
14316     0))
14317
14318 (defun gnus-kill-save-kill-buffer ()
14319   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14320     (when (get-file-buffer file)
14321       (save-excursion
14322         (set-buffer (get-file-buffer file))
14323         (and (buffer-modified-p) (save-buffer))
14324         (kill-buffer (current-buffer))))))
14325
14326 (defvar gnus-kill-file-name "KILL"
14327   "Suffix of the kill files.")
14328
14329 (defun gnus-newsgroup-kill-file (newsgroup)
14330   "Return the name of a kill file name for NEWSGROUP.
14331 If NEWSGROUP is nil, return the global kill file name instead."
14332   (cond 
14333    ;; The global KILL file is placed at top of the directory.
14334    ((or (null newsgroup)
14335         (string-equal newsgroup ""))
14336     (expand-file-name gnus-kill-file-name
14337                       (or gnus-kill-files-directory "~/News")))
14338    ;; Append ".KILL" to newsgroup name.
14339    ((gnus-use-long-file-name 'not-kill)
14340     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14341                               "." gnus-kill-file-name)
14342                       (or gnus-kill-files-directory "~/News")))
14343    ;; Place "KILL" under the hierarchical directory.
14344    (t
14345     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14346                               "/" gnus-kill-file-name)
14347                       (or gnus-kill-files-directory "~/News")))))
14348
14349 \f
14350 ;;;
14351 ;;; Dribble file
14352 ;;;
14353
14354 (defvar gnus-dribble-ignore nil)
14355 (defvar gnus-dribble-eval-file nil)
14356
14357 (defun gnus-dribble-file-name ()
14358   "Return the dribble file for the current .newsrc."
14359   (concat
14360    (if gnus-dribble-directory
14361        (concat (file-name-as-directory gnus-dribble-directory)
14362                (file-name-nondirectory gnus-current-startup-file))
14363      gnus-current-startup-file)
14364    "-dribble"))
14365
14366 (defun gnus-dribble-enter (string)
14367   "Enter STRING into the dribble buffer."
14368   (if (and (not gnus-dribble-ignore)
14369            gnus-dribble-buffer
14370            (buffer-name gnus-dribble-buffer))
14371       (let ((obuf (current-buffer)))
14372         (set-buffer gnus-dribble-buffer)
14373         (insert string "\n")
14374         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14375         (set-buffer obuf))))
14376
14377 (defun gnus-dribble-read-file ()
14378   "Read the dribble file from disk."
14379   (let ((dribble-file (gnus-dribble-file-name)))
14380     (save-excursion
14381       (set-buffer (setq gnus-dribble-buffer
14382                         (get-buffer-create
14383                          (file-name-nondirectory dribble-file))))
14384       (gnus-add-current-to-buffer-list)
14385       (erase-buffer)
14386       (setq buffer-file-name dribble-file)
14387       (auto-save-mode t)
14388       (buffer-disable-undo (current-buffer))
14389       (bury-buffer (current-buffer))
14390       (set-buffer-modified-p nil)
14391       (let ((auto (make-auto-save-file-name))
14392             (gnus-dribble-ignore t))
14393         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14394           ;; Load whichever file is newest -- the auto save file
14395           ;; or the "real" file.
14396           (if (file-newer-than-file-p auto dribble-file)
14397               (insert-file-contents auto)
14398             (insert-file-contents dribble-file))
14399           (unless (zerop (buffer-size))
14400             (set-buffer-modified-p t))
14401           ;; Set the file modes to reflect the .newsrc file modes.
14402           (save-buffer)
14403           (when (file-exists-p gnus-current-startup-file)
14404             (set-file-modes dribble-file
14405                             (file-modes gnus-current-startup-file)))
14406           ;; Possibly eval the file later.
14407           (when (gnus-y-or-n-p
14408                  "Auto-save file exists.  Do you want to read it? ")
14409             (setq gnus-dribble-eval-file t)))))))
14410
14411 (defun gnus-dribble-eval-file ()
14412   (if (not gnus-dribble-eval-file)
14413       ()
14414     (setq gnus-dribble-eval-file nil)
14415     (save-excursion
14416       (let ((gnus-dribble-ignore t))
14417         (set-buffer gnus-dribble-buffer)
14418         (eval-buffer (current-buffer))))))
14419
14420 (defun gnus-dribble-delete-file ()
14421   (if (file-exists-p (gnus-dribble-file-name))
14422       (delete-file (gnus-dribble-file-name)))
14423   (if gnus-dribble-buffer
14424       (save-excursion
14425         (set-buffer gnus-dribble-buffer)
14426         (let ((auto (make-auto-save-file-name)))
14427           (if (file-exists-p auto)
14428               (delete-file auto))
14429           (erase-buffer)
14430           (set-buffer-modified-p nil)))))
14431
14432 (defun gnus-dribble-save ()
14433   (if (and gnus-dribble-buffer
14434            (buffer-name gnus-dribble-buffer))
14435       (save-excursion
14436         (set-buffer gnus-dribble-buffer)
14437         (save-buffer))))
14438
14439 (defun gnus-dribble-clear ()
14440   (save-excursion
14441     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14442         (progn
14443           (set-buffer gnus-dribble-buffer)
14444           (erase-buffer)
14445           (set-buffer-modified-p nil)
14446           (setq buffer-saved-size (buffer-size))))))
14447
14448 \f
14449 ;;;
14450 ;;; Server Communication
14451 ;;;
14452
14453 (defun gnus-start-news-server (&optional confirm)
14454   "Open a method for getting news.
14455 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14456   (let (how)
14457     (if gnus-current-select-method
14458         ;; Stream is already opened.
14459         nil
14460       ;; Open NNTP server.
14461       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14462       (if confirm
14463           (progn
14464             ;; Read server name with completion.
14465             (setq gnus-nntp-server
14466                   (completing-read "NNTP server: "
14467                                    (mapcar (lambda (server) (list server))
14468                                            (cons (list gnus-nntp-server)
14469                                                  gnus-secondary-servers))
14470                                    nil nil gnus-nntp-server))))
14471
14472       (if (and gnus-nntp-server
14473                (stringp gnus-nntp-server)
14474                (not (string= gnus-nntp-server "")))
14475           (setq gnus-select-method
14476                 (cond ((or (string= gnus-nntp-server "")
14477                            (string= gnus-nntp-server "::"))
14478                        (list 'nnspool (system-name)))
14479                       ((string-match "^:" gnus-nntp-server)
14480                        (list 'nnmh gnus-nntp-server
14481                              (list 'nnmh-directory
14482                                    (file-name-as-directory
14483                                     (expand-file-name
14484                                      (concat "~/" (substring
14485                                                    gnus-nntp-server 1)))))
14486                              (list 'nnmh-get-new-mail nil)))
14487                       (t
14488                        (list 'nntp gnus-nntp-server)))))
14489
14490       (setq how (car gnus-select-method))
14491       (cond ((eq how 'nnspool)
14492              (require 'nnspool)
14493              (gnus-message 5 "Looking up local news spool..."))
14494             ((eq how 'nnmh)
14495              (require 'nnmh)
14496              (gnus-message 5 "Looking up mh spool..."))
14497             (t
14498              (require 'nntp)))
14499       (setq gnus-current-select-method gnus-select-method)
14500       (run-hooks 'gnus-open-server-hook)
14501       (or
14502        ;; gnus-open-server-hook might have opened it
14503        (gnus-server-opened gnus-select-method)
14504        (gnus-open-server gnus-select-method)
14505        (gnus-y-or-n-p
14506         (format
14507          "%s (%s) open error: '%s'.     Continue? "
14508          (car gnus-select-method) (cadr gnus-select-method)
14509          (gnus-status-message gnus-select-method)))
14510        (progn
14511          (gnus-message 1 "Couldn't open server on %s"
14512                        (nth 1 gnus-select-method))
14513          (ding)
14514          nil)))))
14515
14516 (defun gnus-check-group (group)
14517   "Try to make sure that the server where GROUP exists is alive."
14518   (let ((method (gnus-find-method-for-group group)))
14519     (or (gnus-server-opened method)
14520         (gnus-open-server method))))
14521
14522 (defun gnus-check-server (&optional method silent)
14523   "Check whether the connection to METHOD is down.
14524 If METHOD is nil, use `gnus-select-method'.
14525 If it is down, start it up (again)."
14526   (let ((method (or method gnus-select-method)))
14527     ;; Transform virtual server names into select methods.
14528     (when (stringp method)
14529       (setq method (gnus-server-to-method method)))
14530     (if (gnus-server-opened method)
14531         ;; The stream is already opened.
14532         t
14533       ;; Open the server.
14534       (unless silent
14535         (gnus-message 5 "Opening %s server%s..." (car method)
14536                       (if (equal (nth 1 method) "") ""
14537                         (format " on %s" (nth 1 method)))))
14538       (run-hooks 'gnus-open-server-hook)
14539       (prog1
14540           (gnus-open-server method)
14541         (unless silent
14542           (message ""))))))
14543
14544 (defun gnus-get-function (method function)
14545   "Return a function symbol based on METHOD and FUNCTION."
14546   ;; Translate server names into methods.
14547   (unless method
14548     (error "Attempted use of a nil select method"))
14549   (when (stringp method)
14550     (setq method (gnus-server-to-method method)))
14551   (let ((func (intern (format "%s-%s" (car method) function))))
14552     ;; If the functions isn't bound, we require the backend in
14553     ;; question.
14554     (unless (fboundp func)
14555       (require (car method))
14556       (unless (fboundp func)
14557         ;; This backend doesn't implement this function.
14558         (error "No such function: %s" func)))
14559     func))
14560
14561 \f
14562 ;;;
14563 ;;; Interface functions to the backends.
14564 ;;;
14565
14566 (defun gnus-open-server (method)
14567   "Open a connection to METHOD."
14568   (let ((elem (assoc method gnus-opened-servers)))
14569     ;; If this method was previously denied, we just return nil.
14570     (if (eq (nth 1 elem) 'denied)
14571         (progn
14572           (gnus-message 1 "Denied server")
14573           nil)
14574       ;; Open the server.
14575       (let ((result
14576              (funcall (gnus-get-function method 'open-server)
14577                       (nth 1 method) (nthcdr 2 method))))
14578         ;; If this hasn't been opened before, we add it to the list.
14579         (unless elem
14580           (setq elem (list method nil)
14581                 gnus-opened-servers (cons elem gnus-opened-servers)))
14582         ;; Set the status of this server.
14583         (setcar (cdr elem) (if result 'ok 'denied))
14584         ;; Return the result from the "open" call.
14585         result))))
14586
14587 (defun gnus-close-server (method)
14588   "Close the connection to METHOD."
14589   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14590
14591 (defun gnus-request-list (method)
14592   "Request the active file from METHOD."
14593   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14594
14595 (defun gnus-request-list-newsgroups (method)
14596   "Request the newsgroups file from METHOD."
14597   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14598
14599 (defun gnus-request-newgroups (date method)
14600   "Request all new groups since DATE from METHOD."
14601   (funcall (gnus-get-function method 'request-newgroups)
14602            date (nth 1 method)))
14603
14604 (defun gnus-server-opened (method)
14605   "Check whether a connection to METHOD has been opened."
14606   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14607
14608 (defun gnus-status-message (method)
14609   "Return the status message from METHOD.
14610 If METHOD is a string, it is interpreted as a group name.   The method
14611 this group uses will be queried."
14612   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14613                   method)))
14614     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14615
14616 (defun gnus-request-group (group &optional dont-check method)
14617   "Request GROUP.  If DONT-CHECK, no information is required."
14618   (let ((method (or method (gnus-find-method-for-group group))))
14619     (funcall (gnus-get-function method 'request-group)
14620              (gnus-group-real-name group) (nth 1 method) dont-check)))
14621
14622 (defun gnus-request-asynchronous (group &optional articles)
14623   "Request that GROUP behave asynchronously.
14624 ARTICLES is the `data' of the group."
14625   (let ((method (gnus-find-method-for-group group)))
14626     (funcall (gnus-get-function method 'request-asynchronous)
14627              (gnus-group-real-name group) (nth 1 method) articles)))
14628
14629 (defun gnus-list-active-group (group)
14630   "Request active information on GROUP."
14631   (let ((method (gnus-find-method-for-group group))
14632         (func 'list-active-group))
14633     (when (gnus-check-backend-function func group)
14634       (funcall (gnus-get-function method func)
14635                (gnus-group-real-name group) (nth 1 method)))))
14636
14637 (defun gnus-request-group-description (group)
14638   "Request a description of GROUP."
14639   (let ((method (gnus-find-method-for-group group))
14640         (func 'request-group-description))
14641     (when (gnus-check-backend-function func group)
14642       (funcall (gnus-get-function method func)
14643                (gnus-group-real-name group) (nth 1 method)))))
14644
14645 (defun gnus-close-group (group)
14646   "Request the GROUP be closed."
14647   (let ((method (gnus-find-method-for-group group)))
14648     (funcall (gnus-get-function method 'close-group)
14649              (gnus-group-real-name group) (nth 1 method))))
14650
14651 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14652   "Request headers for ARTICLES in GROUP.
14653 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14654   (let ((method (gnus-find-method-for-group group)))
14655     (if (and gnus-use-cache (numberp (car articles)))
14656         (gnus-cache-retrieve-headers articles group fetch-old)
14657       (funcall (gnus-get-function method 'retrieve-headers)
14658                articles (gnus-group-real-name group) (nth 1 method)
14659                fetch-old))))
14660
14661 (defun gnus-retrieve-groups (groups method)
14662   "Request active information on GROUPS from METHOD."
14663   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14664
14665 (defun gnus-request-type (group &optional article)
14666   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14667   (let ((method (gnus-find-method-for-group group)))
14668     (if (not (gnus-check-backend-function 'request-type (car method)))
14669         'unknown
14670       (funcall (gnus-get-function method 'request-type)
14671                (gnus-group-real-name group) article))))
14672
14673 (defun gnus-request-update-mark (group article mark)
14674   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14675   (let ((method (gnus-find-method-for-group group)))
14676     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14677         mark
14678       (funcall (gnus-get-function method 'request-update-mark)
14679                (gnus-group-real-name group) article mark))))
14680
14681 (defun gnus-request-article (article group &optional buffer)
14682   "Request the ARTICLE in GROUP.
14683 ARTICLE can either be an article number or an article Message-ID.
14684 If BUFFER, insert the article in that group."
14685   (let ((method (gnus-find-method-for-group group)))
14686     (funcall (gnus-get-function method 'request-article)
14687              article (gnus-group-real-name group) (nth 1 method) buffer)))
14688
14689 (defun gnus-request-head (article group)
14690   "Request the head of ARTICLE in GROUP."
14691   (let ((method (gnus-find-method-for-group group)))
14692     (funcall (gnus-get-function method 'request-head)
14693              article (gnus-group-real-name group) (nth 1 method))))
14694
14695 (defun gnus-request-body (article group)
14696   "Request the body of ARTICLE in GROUP."
14697   (let ((method (gnus-find-method-for-group group)))
14698     (funcall (gnus-get-function method 'request-body)
14699              article (gnus-group-real-name group) (nth 1 method))))
14700
14701 (defun gnus-request-post (method)
14702   "Post the current buffer using METHOD."
14703   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14704
14705 (defun gnus-request-scan (group method)
14706   "Request a SCAN being performed in GROUP from METHOD.
14707 If GROUP is nil, all groups on METHOD are scanned."
14708   (let ((method (if group (gnus-find-method-for-group group) method)))
14709     (funcall (gnus-get-function method 'request-scan)
14710              (and group (gnus-group-real-name group)) (nth 1 method))))
14711
14712 (defsubst gnus-request-update-info (info method)
14713   "Request that METHOD update INFO."
14714   (when (gnus-check-backend-function 'request-update-info (car method))
14715     (funcall (gnus-get-function method 'request-update-info)
14716              (gnus-group-real-name (gnus-info-group info))
14717              info (nth 1 method))))
14718
14719 (defun gnus-request-expire-articles (articles group &optional force)
14720   (let ((method (gnus-find-method-for-group group)))
14721     (funcall (gnus-get-function method 'request-expire-articles)
14722              articles (gnus-group-real-name group) (nth 1 method)
14723              force)))
14724
14725 (defun gnus-request-move-article
14726   (article group server accept-function &optional last)
14727   (let ((method (gnus-find-method-for-group group)))
14728     (funcall (gnus-get-function method 'request-move-article)
14729              article (gnus-group-real-name group)
14730              (nth 1 method) accept-function last)))
14731
14732 (defun gnus-request-accept-article (group &optional last method)
14733   ;; Make sure there's a newline at the end of the article.
14734   (goto-char (point-max))
14735   (unless (bolp)
14736     (insert "\n"))
14737   (let ((func (if (symbolp group) group
14738                 (car (or method (gnus-find-method-for-group group))))))
14739     (funcall (intern (format "%s-request-accept-article" func))
14740              (if (stringp group) (gnus-group-real-name group) group)
14741              last)))
14742
14743 (defun gnus-request-replace-article (article group buffer)
14744   (let ((func (car (gnus-find-method-for-group group))))
14745     (funcall (intern (format "%s-request-replace-article" func))
14746              article (gnus-group-real-name group) buffer)))
14747
14748 (defun gnus-request-associate-buffer (group)
14749   (let ((method (gnus-find-method-for-group group)))
14750     (funcall (gnus-get-function method 'request-associate-buffer)
14751              (gnus-group-real-name group))))
14752
14753 (defun gnus-request-restore-buffer (article group)
14754   "Request a new buffer restored to the state of ARTICLE."
14755   (let ((method (gnus-find-method-for-group group)))
14756     (funcall (gnus-get-function method 'request-restore-buffer)
14757              article (gnus-group-real-name group) (nth 1 method))))
14758
14759 (defun gnus-request-create-group (group &optional method)
14760   (let ((method (or method (gnus-find-method-for-group group))))
14761     (funcall (gnus-get-function method 'request-create-group)
14762              (gnus-group-real-name group) (nth 1 method))))
14763
14764 (defun gnus-request-delete-group (group &optional force)
14765   (let ((method (gnus-find-method-for-group group)))
14766     (funcall (gnus-get-function method 'request-delete-group)
14767              (gnus-group-real-name group) force (nth 1 method))))
14768
14769 (defun gnus-request-rename-group (group new-name)
14770   (let ((method (gnus-find-method-for-group group)))
14771     (funcall (gnus-get-function method 'request-rename-group)
14772              (gnus-group-real-name group)
14773              (gnus-group-real-name new-name) (nth 1 method))))
14774
14775 (defun gnus-member-of-valid (symbol group)
14776   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14777   (memq symbol (assoc
14778                 (symbol-name (car (gnus-find-method-for-group group)))
14779                 gnus-valid-select-methods)))
14780
14781 (defun gnus-method-option-p (method option)
14782   "Return non-nil if select METHOD has OPTION as a parameter."
14783   (memq option (assoc (format "%s" (car method))
14784                       gnus-valid-select-methods)))
14785
14786 (defun gnus-server-extend-method (group method)
14787   ;; This function "extends" a virtual server.  If the server is
14788   ;; "hello", and the select method is ("hello" (my-var "something"))
14789   ;; in the group "alt.alt", this will result in a new virtual server
14790   ;; called "hello+alt.alt".
14791   (let ((entry
14792          (gnus-copy-sequence
14793           (if (equal (car method) "native") gnus-select-method
14794             (cdr (assoc (car method) gnus-server-alist))))))
14795     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14796     (nconc entry (cdr method))))
14797
14798 (defun gnus-find-method-for-group (group &optional info)
14799   "Find the select method that GROUP uses."
14800   (or gnus-override-method
14801       (and (not group)
14802            gnus-select-method)
14803       (let ((info (or info (gnus-get-info group)))
14804             method)
14805         (if (or (not info)
14806                 (not (setq method (gnus-info-method info)))
14807                 (equal method "native"))
14808             gnus-select-method
14809           (setq method
14810                 (cond ((stringp method)
14811                        (gnus-server-to-method method))
14812                       ((stringp (car method))
14813                        (gnus-server-extend-method group method))
14814                       (t
14815                        method)))
14816           (if (equal (cadr method) "")
14817               method
14818             (gnus-server-add-address method))))))
14819
14820 (defun gnus-check-backend-function (func group)
14821   "Check whether GROUP supports function FUNC."
14822   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14823                   group)))
14824     (fboundp (intern (format "%s-%s" method func)))))
14825
14826 (defun gnus-methods-using (feature)
14827   "Find all methods that have FEATURE."
14828   (let ((valids gnus-valid-select-methods)
14829         outs)
14830     (while valids
14831       (if (memq feature (car valids))
14832           (setq outs (cons (car valids) outs)))
14833       (setq valids (cdr valids)))
14834     outs))
14835
14836 \f
14837 ;;;
14838 ;;; Active & Newsrc File Handling
14839 ;;;
14840
14841 (defun gnus-setup-news (&optional rawfile level dont-connect)
14842   "Setup news information.
14843 If RAWFILE is non-nil, the .newsrc file will also be read.
14844 If LEVEL is non-nil, the news will be set up at level LEVEL."
14845   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14846
14847     (when init 
14848       ;; Clear some variables to re-initialize news information.
14849       (setq gnus-newsrc-alist nil
14850             gnus-active-hashtb nil)
14851       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14852       (gnus-read-newsrc-file rawfile))
14853
14854     (when (and (not (assoc "archive" gnus-server-alist))
14855                gnus-message-archive-method)
14856       (push (cons "archive" gnus-message-archive-method)
14857             gnus-server-alist))
14858
14859     ;; If we don't read the complete active file, we fill in the
14860     ;; hashtb here.
14861     (if (or (null gnus-read-active-file)
14862             (eq gnus-read-active-file 'some))
14863         (gnus-update-active-hashtb-from-killed))
14864
14865     ;; Read the active file and create `gnus-active-hashtb'.
14866     ;; If `gnus-read-active-file' is nil, then we just create an empty
14867     ;; hash table.  The partial filling out of the hash table will be
14868     ;; done in `gnus-get-unread-articles'.
14869     (and gnus-read-active-file
14870          (not level)
14871          (gnus-read-active-file))
14872
14873     (or gnus-active-hashtb
14874         (setq gnus-active-hashtb (make-vector 4095 0)))
14875
14876     ;; Initialize the cache.
14877     (when gnus-use-cache
14878       (gnus-cache-open))
14879
14880     ;; Possibly eval the dribble file.
14881     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14882
14883     (gnus-update-format-specifications)
14884
14885     ;; See whether we need to read the description file.
14886     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
14887              (not gnus-description-hashtb)
14888              (not dont-connect)
14889              gnus-read-active-file)
14890         (gnus-read-all-descriptions-files))
14891
14892     ;; Find new newsgroups and treat them.
14893     (if (and init gnus-check-new-newsgroups (not level)
14894              (gnus-check-server gnus-select-method))
14895         (gnus-find-new-newsgroups))
14896
14897     ;; Find the number of unread articles in each non-dead group.
14898     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14899       (gnus-get-unread-articles level))
14900
14901     (if (and init gnus-check-bogus-newsgroups
14902              gnus-read-active-file (not level)
14903              (gnus-server-opened gnus-select-method))
14904         (gnus-check-bogus-newsgroups))))
14905
14906 (defun gnus-find-new-newsgroups (&optional arg)
14907   "Search for new newsgroups and add them.
14908 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14909 The `-n' option line from .newsrc is respected.
14910 If ARG (the prefix), use the `ask-server' method to query
14911 the server for new groups."
14912   (interactive "P")
14913   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14914                        (null gnus-read-active-file)
14915                        (eq gnus-read-active-file 'some))
14916                    'ask-server gnus-check-new-newsgroups)))
14917     (unless (gnus-check-first-time-used)
14918       (if (or (consp check)
14919               (eq check 'ask-server))
14920           ;; Ask the server for new groups.
14921           (gnus-ask-server-for-new-groups)
14922         ;; Go through the active hashtb and look for new groups.
14923         (let ((groups 0)
14924               group new-newsgroups)
14925           (gnus-message 5 "Looking for new newsgroups...")
14926           (unless gnus-have-read-active-file
14927             (gnus-read-active-file))
14928           (setq gnus-newsrc-last-checked-date (current-time-string))
14929           (unless gnus-killed-hashtb
14930             (gnus-make-hashtable-from-killed))
14931           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14932           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14933           (mapatoms
14934            (lambda (sym)
14935              (if (or (null (setq group (symbol-name sym)))
14936                      (not (boundp sym))
14937                      (null (symbol-value sym))
14938                      (gnus-gethash group gnus-killed-hashtb)
14939                      (gnus-gethash group gnus-newsrc-hashtb))
14940                  ()
14941                (let ((do-sub (gnus-matches-options-n group)))
14942                  (cond
14943                   ((eq do-sub 'subscribe)
14944                    (setq groups (1+ groups))
14945                    (gnus-sethash group group gnus-killed-hashtb)
14946                    (funcall gnus-subscribe-options-newsgroup-method group))
14947                   ((eq do-sub 'ignore)
14948                    nil)
14949                   (t
14950                    (setq groups (1+ groups))
14951                    (gnus-sethash group group gnus-killed-hashtb)
14952                    (if gnus-subscribe-hierarchical-interactive
14953                        (setq new-newsgroups (cons group new-newsgroups))
14954                      (funcall gnus-subscribe-newsgroup-method group)))))))
14955            gnus-active-hashtb)
14956           (when new-newsgroups
14957             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14958           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14959           (if (> groups 0)
14960               (gnus-message 6 "%d new newsgroup%s arrived."
14961                             groups (if (> groups 1) "s have" " has"))
14962             (gnus-message 6 "No new newsgroups.")))))))
14963
14964 (defun gnus-matches-options-n (group)
14965   ;; Returns `subscribe' if the group is to be unconditionally
14966   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14967   ;; no match for the group.
14968
14969   ;; First we check the two user variables.
14970   (cond
14971    ((and gnus-options-subscribe
14972          (string-match gnus-options-subscribe group))
14973     'subscribe)
14974    ((and gnus-auto-subscribed-groups
14975          (string-match gnus-auto-subscribed-groups group))
14976     'subscribe)
14977    ((and gnus-options-not-subscribe
14978          (string-match gnus-options-not-subscribe group))
14979     'ignore)
14980    ;; Then we go through the list that was retrieved from the .newsrc
14981    ;; file.  This list has elements on the form
14982    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14983    ;; is in the reverse order of the options line) is returned.
14984    (t
14985     (let ((regs gnus-newsrc-options-n))
14986       (while (and regs
14987                   (not (string-match (caar regs) group)))
14988         (setq regs (cdr regs)))
14989       (and regs (cdar regs))))))
14990
14991 (defun gnus-ask-server-for-new-groups ()
14992   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14993          (methods (cons gnus-select-method
14994                         (nconc
14995                          (when gnus-message-archive-method
14996                            (list "archive"))
14997                          (append
14998                           (and (consp gnus-check-new-newsgroups)
14999                                gnus-check-new-newsgroups)
15000                           gnus-secondary-select-methods))))
15001          (groups 0)
15002          (new-date (current-time-string))
15003          group new-newsgroups got-new method hashtb
15004          gnus-override-subscribe-method)
15005     ;; Go through both primary and secondary select methods and
15006     ;; request new newsgroups.
15007     (while (setq method (gnus-server-get-method nil (pop methods)))
15008       (setq new-newsgroups nil)
15009       (setq gnus-override-subscribe-method method)
15010       (when (and (gnus-check-server method)
15011                  (gnus-request-newgroups date method))
15012         (save-excursion
15013           (setq got-new t)
15014           (setq hashtb (gnus-make-hashtable 100))
15015           (set-buffer nntp-server-buffer)
15016           ;; Enter all the new groups into a hashtable.
15017           (gnus-active-to-gnus-format method hashtb 'ignore)))
15018       ;; Now all new groups from `method' are in `hashtb'.
15019       (mapatoms
15020        (lambda (group-sym)
15021          (if (or (null (setq group (symbol-name group-sym)))
15022                  (null (symbol-value group-sym))
15023                  (gnus-gethash group gnus-newsrc-hashtb)
15024                  (member group gnus-zombie-list)
15025                  (member group gnus-killed-list))
15026              ;; The group is already known.
15027              ()
15028            ;; Make this group active.
15029            (when (symbol-value group-sym)
15030              (gnus-set-active group (symbol-value group-sym)))
15031            ;; Check whether we want it or not.
15032            (let ((do-sub (gnus-matches-options-n group)))
15033              (cond
15034               ((eq do-sub 'subscribe)
15035                (incf groups)
15036                (gnus-sethash group group gnus-killed-hashtb)
15037                (funcall gnus-subscribe-options-newsgroup-method group))
15038               ((eq do-sub 'ignore)
15039                nil)
15040               (t
15041                (incf groups)
15042                (gnus-sethash group group gnus-killed-hashtb)
15043                (if gnus-subscribe-hierarchical-interactive
15044                    (push group new-newsgroups)
15045                  (funcall gnus-subscribe-newsgroup-method group)))))))
15046        hashtb)
15047       (when new-newsgroups
15048         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15049     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15050     (when (> groups 0)
15051       (gnus-message 6 "%d new newsgroup%s arrived."
15052                     groups (if (> groups 1) "s have" " has")))
15053     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15054     got-new))
15055
15056 (defun gnus-check-first-time-used ()
15057   (if (or (> (length gnus-newsrc-alist) 1)
15058           (file-exists-p gnus-startup-file)
15059           (file-exists-p (concat gnus-startup-file ".el"))
15060           (file-exists-p (concat gnus-startup-file ".eld")))
15061       nil
15062     (gnus-message 6 "First time user; subscribing you to default groups")
15063     (unless gnus-have-read-active-file
15064       (gnus-read-active-file))
15065     (setq gnus-newsrc-last-checked-date (current-time-string))
15066     (let ((groups gnus-default-subscribed-newsgroups)
15067           group)
15068       (if (eq groups t)
15069           nil
15070         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15071         (mapatoms
15072          (lambda (sym)
15073            (if (null (setq group (symbol-name sym)))
15074                ()
15075              (let ((do-sub (gnus-matches-options-n group)))
15076                (cond
15077                 ((eq do-sub 'subscribe)
15078                  (gnus-sethash group group gnus-killed-hashtb)
15079                  (funcall gnus-subscribe-options-newsgroup-method group))
15080                 ((eq do-sub 'ignore)
15081                  nil)
15082                 (t
15083                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15084          gnus-active-hashtb)
15085         (while groups
15086           (if (gnus-active (car groups))
15087               (gnus-group-change-level
15088                (car groups) gnus-level-default-subscribed gnus-level-killed))
15089           (setq groups (cdr groups)))
15090         (gnus-group-make-help-group)
15091         (and gnus-novice-user
15092              (gnus-message 7 "`A k' to list killed groups"))))))
15093
15094 (defun gnus-subscribe-group (group previous &optional method)
15095   (gnus-group-change-level
15096    (if method
15097        (list t group gnus-level-default-subscribed nil nil method)
15098      group)
15099    gnus-level-default-subscribed gnus-level-killed previous t))
15100
15101 ;; `gnus-group-change-level' is the fundamental function for changing
15102 ;; subscription levels of newsgroups.  This might mean just changing
15103 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15104 ;; again, which subscribes/unsubscribes a group, which is equally
15105 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15106 ;; from 8-9 to 1-7 means that you remove the group from the list of
15107 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15108 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15109 ;; which is trivial.
15110 ;; ENTRY can either be a string (newsgroup name) or a list (if
15111 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15112 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15113 ;; entries.
15114 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15115 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15116 ;; after.
15117 (defun gnus-group-change-level (entry level &optional oldlevel
15118                                       previous fromkilled)
15119   (let (group info active num)
15120     ;; Glean what info we can from the arguments
15121     (if (consp entry)
15122         (if fromkilled (setq group (nth 1 entry))
15123           (setq group (car (nth 2 entry))))
15124       (setq group entry))
15125     (if (and (stringp entry)
15126              oldlevel
15127              (< oldlevel gnus-level-zombie))
15128         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15129     (if (and (not oldlevel)
15130              (consp entry))
15131         (setq oldlevel (gnus-info-level (nth 2 entry)))
15132       (setq oldlevel (or oldlevel 9)))
15133     (if (stringp previous)
15134         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15135
15136     (if (and (>= oldlevel gnus-level-zombie)
15137              (gnus-gethash group gnus-newsrc-hashtb))
15138         ;; We are trying to subscribe a group that is already
15139         ;; subscribed.
15140         ()                              ; Do nothing.
15141
15142       (or (gnus-ephemeral-group-p group)
15143           (gnus-dribble-enter
15144            (format "(gnus-group-change-level %S %S %S %S %S)"
15145                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15146
15147       ;; Then we remove the newgroup from any old structures, if needed.
15148       ;; If the group was killed, we remove it from the killed or zombie
15149       ;; list.  If not, and it is in fact going to be killed, we remove
15150       ;; it from the newsrc hash table and assoc.
15151       (cond
15152        ((>= oldlevel gnus-level-zombie)
15153         (if (= oldlevel gnus-level-zombie)
15154             (setq gnus-zombie-list (delete group gnus-zombie-list))
15155           (setq gnus-killed-list (delete group gnus-killed-list))))
15156        (t
15157         (if (and (>= level gnus-level-zombie)
15158                  entry)
15159             (progn
15160               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15161               (if (nth 3 entry)
15162                   (setcdr (gnus-gethash (car (nth 3 entry))
15163                                         gnus-newsrc-hashtb)
15164                           (cdr entry)))
15165               (setcdr (cdr entry) (cdddr entry))))))
15166
15167       ;; Finally we enter (if needed) the list where it is supposed to
15168       ;; go, and change the subscription level.  If it is to be killed,
15169       ;; we enter it into the killed or zombie list.
15170       (cond 
15171        ((>= level gnus-level-zombie)
15172         ;; Remove from the hash table.
15173         (gnus-sethash group nil gnus-newsrc-hashtb)
15174         ;; We do not enter foreign groups into the list of dead
15175         ;; groups.
15176         (unless (gnus-group-foreign-p group)
15177           (if (= level gnus-level-zombie)
15178               (setq gnus-zombie-list (cons group gnus-zombie-list))
15179             (setq gnus-killed-list (cons group gnus-killed-list)))))
15180        (t
15181         ;; If the list is to be entered into the newsrc assoc, and
15182         ;; it was killed, we have to create an entry in the newsrc
15183         ;; hashtb format and fix the pointers in the newsrc assoc.
15184         (if (< oldlevel gnus-level-zombie)
15185             ;; It was alive, and it is going to stay alive, so we
15186             ;; just change the level and don't change any pointers or
15187             ;; hash table entries.
15188             (setcar (cdaddr entry) level)
15189           (if (listp entry)
15190               (setq info (cdr entry)
15191                     num (car entry))
15192             (setq active (gnus-active group))
15193             (setq num
15194                   (if active (- (1+ (cdr active)) (car active)) t))
15195             ;; Check whether the group is foreign.  If so, the
15196             ;; foreign select method has to be entered into the
15197             ;; info.
15198             (let ((method (or gnus-override-subscribe-method
15199                               (gnus-group-method group))))
15200               (if (eq method gnus-select-method)
15201                   (setq info (list group level nil))
15202                 (setq info (list group level nil nil method)))))
15203           (unless previous
15204             (setq previous
15205                   (let ((p gnus-newsrc-alist))
15206                     (while (cddr p)
15207                       (setq p (cdr p)))
15208                     p)))
15209           (setq entry (cons info (cddr previous)))
15210           (if (cdr previous)
15211               (progn
15212                 (setcdr (cdr previous) entry)
15213                 (gnus-sethash group (cons num (cdr previous))
15214                               gnus-newsrc-hashtb))
15215             (setcdr previous entry)
15216             (gnus-sethash group (cons num previous)
15217                           gnus-newsrc-hashtb))
15218           (when (cdr entry)
15219             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15220       (when gnus-group-change-level-function
15221         (funcall gnus-group-change-level-function group level oldlevel)))))
15222
15223 (defun gnus-kill-newsgroup (newsgroup)
15224   "Obsolete function.  Kills a newsgroup."
15225   (gnus-group-change-level
15226    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15227
15228 (defun gnus-check-bogus-newsgroups (&optional confirm)
15229   "Remove bogus newsgroups.
15230 If CONFIRM is non-nil, the user has to confirm the deletion of every
15231 newsgroup."
15232   (let ((newsrc (cdr gnus-newsrc-alist))
15233         bogus group entry info)
15234     (gnus-message 5 "Checking bogus newsgroups...")
15235     (unless gnus-have-read-active-file
15236       (gnus-read-active-file))
15237     (when (member gnus-select-method gnus-have-read-active-file)
15238       ;; Find all bogus newsgroup that are subscribed.
15239       (while newsrc
15240         (setq info (pop newsrc)
15241               group (gnus-info-group info))
15242         (unless (or (gnus-active group) ; Active
15243                     (gnus-info-method info) ; Foreign
15244                     (and confirm
15245                          (not (gnus-y-or-n-p
15246                                (format "Remove bogus newsgroup: %s " group)))))
15247           ;; Found a bogus newsgroup.
15248           (push group bogus)))
15249       ;; Remove all bogus subscribed groups by first killing them, and
15250       ;; then removing them from the list of killed groups.
15251       (while bogus
15252         (when (setq entry (gnus-gethash (setq group (pop bogus))
15253                                         gnus-newsrc-hashtb))
15254           (gnus-group-change-level entry gnus-level-killed)
15255           (setq gnus-killed-list (delete group gnus-killed-list))))
15256       ;; Then we remove all bogus groups from the list of killed and
15257       ;; zombie groups.  They are are removed without confirmation.
15258       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15259             killed)
15260         (while dead-lists
15261           (setq killed (symbol-value (car dead-lists)))
15262           (while killed
15263             (unless (gnus-active (setq group (pop killed)))
15264               ;; The group is bogus.
15265               ;; !!!Slow as hell.
15266               (set (car dead-lists)
15267                    (delete group (symbol-value (car dead-lists))))))
15268           (setq dead-lists (cdr dead-lists))))
15269       (gnus-message 5 "Checking bogus newsgroups...done"))))
15270
15271 (defun gnus-check-duplicate-killed-groups ()
15272   "Remove duplicates from the list of killed groups."
15273   (interactive)
15274   (let ((killed gnus-killed-list))
15275     (while killed
15276       (gnus-message 9 "%d" (length killed))
15277       (setcdr killed (delete (car killed) (cdr killed)))
15278       (setq killed (cdr killed)))))
15279
15280 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15281 ;; and compute how many unread articles there are in each group.
15282 (defun gnus-get-unread-articles (&optional level)
15283   (let* ((newsrc (cdr gnus-newsrc-alist))
15284          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15285          (foreign-level
15286           (min
15287            (cond ((and gnus-activate-foreign-newsgroups
15288                        (not (numberp gnus-activate-foreign-newsgroups)))
15289                   (1+ gnus-level-subscribed))
15290                  ((numberp gnus-activate-foreign-newsgroups)
15291                   gnus-activate-foreign-newsgroups)
15292                  (t 0))
15293            level))
15294          info group active method)
15295     (gnus-message 5 "Checking new news...")
15296
15297     (while newsrc
15298       (setq active (gnus-active (setq group (gnus-info-group
15299                                              (setq info (pop newsrc))))))
15300
15301       ;; Check newsgroups.  If the user doesn't want to check them, or
15302       ;; they can't be checked (for instance, if the news server can't
15303       ;; be reached) we just set the number of unread articles in this
15304       ;; newsgroup to t.  This means that Gnus thinks that there are
15305       ;; unread articles, but it has no idea how many.
15306       (if (and (setq method (gnus-info-method info))
15307                (not (gnus-server-equal
15308                      gnus-select-method
15309                      (gnus-server-get-method nil method)))
15310                (not (gnus-secondary-method-p method)))
15311           ;; These groups are foreign.  Check the level.
15312           (when (<= (gnus-info-level info) foreign-level)
15313             (setq active (gnus-activate-group group 'scan))
15314             (gnus-close-group group))
15315
15316         ;; These groups are native or secondary.
15317         (when (and (<= (gnus-info-level info) level)
15318                    (not gnus-read-active-file))
15319           (setq active (gnus-activate-group group 'scan))
15320           (gnus-close-group group)))
15321
15322       (if active
15323           (gnus-get-unread-articles-in-group info active t)
15324         ;; The group couldn't be reached, so we nix out the number of
15325         ;; unread articles and stuff.
15326         (gnus-set-active group nil)
15327         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15328
15329     (gnus-message 5 "Checking new news...done")))
15330
15331 ;; Create a hash table out of the newsrc alist.  The `car's of the
15332 ;; alist elements are used as keys.
15333 (defun gnus-make-hashtable-from-newsrc-alist ()
15334   (let ((alist gnus-newsrc-alist)
15335         (ohashtb gnus-newsrc-hashtb)
15336         prev)
15337     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15338     (setq alist
15339           (setq prev (setq gnus-newsrc-alist
15340                            (if (equal (caar gnus-newsrc-alist)
15341                                       "dummy.group")
15342                                gnus-newsrc-alist
15343                              (cons (list "dummy.group" 0 nil) alist)))))
15344     (while alist
15345       (gnus-sethash
15346        (caar alist)
15347        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15348              prev)
15349        gnus-newsrc-hashtb)
15350       (setq prev alist
15351             alist (cdr alist)))))
15352
15353 (defun gnus-make-hashtable-from-killed ()
15354   "Create a hash table from the killed and zombie lists."
15355   (let ((lists '(gnus-killed-list gnus-zombie-list))
15356         list)
15357     (setq gnus-killed-hashtb
15358           (gnus-make-hashtable
15359            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15360     (while lists
15361       (setq list (symbol-value (car lists)))
15362       (setq lists (cdr lists))
15363       (while list
15364         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15365         (setq list (cdr list))))))
15366
15367 (defun gnus-get-unread-articles-in-group (info active &optional update)
15368   (when active
15369     ;; Allow the backend to update the info in the group.
15370     (when (and update 
15371                (gnus-request-update-info
15372                 info (gnus-find-method-for-group (gnus-info-group info))))
15373       (gnus-activate-group (gnus-info-group info)))
15374     (let* ((range (gnus-info-read info))
15375            (num 0))
15376       ;; If a cache is present, we may have to alter the active info.
15377       (when (and gnus-use-cache info)
15378         (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15379       ;; Modify the list of read articles according to what articles
15380       ;; are available; then tally the unread articles and add the
15381       ;; number to the group hash table entry.
15382       (cond
15383        ((zerop (cdr active))
15384         (setq num 0))
15385        ((not range)
15386         (setq num (- (1+ (cdr active)) (car active))))
15387        ((not (listp (cdr range)))
15388         ;; Fix a single (num . num) range according to the
15389         ;; active hash table.
15390         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15391         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15392         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15393         ;; Compute number of unread articles.
15394         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15395        (t
15396         ;; The read list is a list of ranges.  Fix them according to
15397         ;; the active hash table.
15398         ;; First peel off any elements that are below the lower
15399         ;; active limit.
15400         (while (and (cdr range)
15401                     (>= (car active)
15402                         (or (and (atom (cadr range)) (cadr range))
15403                             (caadr range))))
15404           (if (numberp (car range))
15405               (setcar range
15406                       (cons (car range)
15407                             (or (and (numberp (cadr range))
15408                                      (cadr range))
15409                                 (cdadr range))))
15410             (setcdr (car range)
15411                     (or (and (numberp (nth 1 range)) (nth 1 range))
15412                         (cdadr range))))
15413           (setcdr range (cddr range)))
15414         ;; Adjust the first element to be the same as the lower limit.
15415         (if (and (not (atom (car range)))
15416                  (< (cdar range) (car active)))
15417             (setcdr (car range) (1- (car active))))
15418         ;; Then we want to peel off any elements that are higher
15419         ;; than the upper active limit.
15420         (let ((srange range))
15421           ;; Go past all legal elements.
15422           (while (and (cdr srange)
15423                       (<= (or (and (atom (cadr srange))
15424                                    (cadr srange))
15425                               (caadr srange)) (cdr active)))
15426             (setq srange (cdr srange)))
15427           (if (cdr srange)
15428               ;; Nuke all remaining illegal elements.
15429               (setcdr srange nil))
15430
15431           ;; Adjust the final element.
15432           (if (and (not (atom (car srange)))
15433                    (> (cdar srange) (cdr active)))
15434               (setcdr (car srange) (cdr active))))
15435         ;; Compute the number of unread articles.
15436         (while range
15437           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15438                                       (cdar range)))
15439                               (or (and (atom (car range)) (car range))
15440                                   (caar range)))))
15441           (setq range (cdr range)))
15442         (setq num (max 0 (- (cdr active) num)))))
15443       ;; Set the number of unread articles.
15444       (when info
15445         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15446       num)))
15447
15448 (defun gnus-activate-group (group &optional scan)
15449   ;; Check whether a group has been activated or not.
15450   ;; If SCAN, request a scan of that group as well.
15451   (let ((method (gnus-find-method-for-group group))
15452         active)
15453     (and (gnus-check-server method)
15454          ;; We escape all bugs and quit here to make it possible to
15455          ;; continue if a group is so out-there that it reports bugs
15456          ;; and stuff.
15457          (progn
15458            (and scan
15459                 (gnus-check-backend-function 'request-scan (car method))
15460                 (gnus-request-scan group method))
15461            t)
15462          (condition-case ()
15463              (gnus-request-group group)
15464         ;   (error nil)
15465            (quit nil))
15466          (save-excursion
15467            (set-buffer nntp-server-buffer)
15468            (goto-char (point-min))
15469            ;; Parse the result we got from `gnus-request-group'.
15470            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15471                 (progn
15472                   (goto-char (match-beginning 1))
15473                   (gnus-set-active
15474                    group (setq active (cons (read (current-buffer))
15475                                             (read (current-buffer)))))
15476                   ;; Return the new active info.
15477                   active))))))
15478
15479 (defun gnus-update-read-articles (group unread)
15480   "Update the list of read and ticked articles in GROUP using the
15481 UNREAD and TICKED lists.
15482 Note: UNSELECTED has to be sorted over `<'.
15483 Returns whether the updating was successful."
15484   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15485          (entry (gnus-gethash group gnus-newsrc-hashtb))
15486          (info (nth 2 entry))
15487          (prev 1)
15488          (unread (sort (copy-sequence unread) '<))
15489          read)
15490     (if (or (not info) (not active))
15491         ;; There is no info on this group if it was, in fact,
15492         ;; killed.  Gnus stores no information on killed groups, so
15493         ;; there's nothing to be done.
15494         ;; One could store the information somewhere temporarily,
15495         ;; perhaps...  Hmmm...
15496         ()
15497       ;; Remove any negative articles numbers.
15498       (while (and unread (< (car unread) 0))
15499         (setq unread (cdr unread)))
15500       ;; Remove any expired article numbers
15501       (while (and unread (< (car unread) (car active)))
15502         (setq unread (cdr unread)))
15503       ;; Compute the ranges of read articles by looking at the list of
15504       ;; unread articles.
15505       (while unread
15506         (if (/= (car unread) prev)
15507             (setq read (cons (if (= prev (1- (car unread))) prev
15508                                (cons prev (1- (car unread)))) read)))
15509         (setq prev (1+ (car unread)))
15510         (setq unread (cdr unread)))
15511       (when (<= prev (cdr active))
15512         (setq read (cons (cons prev (cdr active)) read)))
15513       ;; Enter this list into the group info.
15514       (gnus-info-set-read
15515        info (if (> (length read) 1) (nreverse read) read))
15516       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15517       (gnus-get-unread-articles-in-group info (gnus-active group))
15518       t)))
15519
15520 (defun gnus-make-articles-unread (group articles)
15521   "Mark ARTICLES in GROUP as unread."
15522   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15523                           (gnus-gethash (gnus-group-real-name group)
15524                                         gnus-newsrc-hashtb))))
15525          (ranges (gnus-info-read info))
15526          news article)
15527     (while articles
15528       (when (gnus-member-of-range
15529              (setq article (pop articles)) ranges)
15530         (setq news (cons article news))))
15531     (when news
15532       (gnus-info-set-read
15533        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15534       (gnus-group-update-group group t))))
15535
15536 ;; Enter all dead groups into the hashtb.
15537 (defun gnus-update-active-hashtb-from-killed ()
15538   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15539         (lists (list gnus-killed-list gnus-zombie-list))
15540         killed)
15541     (while lists
15542       (setq killed (car lists))
15543       (while killed
15544         (gnus-sethash (car killed) nil hashtb)
15545         (setq killed (cdr killed)))
15546       (setq lists (cdr lists)))))
15547
15548 ;; Get the active file(s) from the backend(s).
15549 (defun gnus-read-active-file ()
15550   (gnus-group-set-mode-line)
15551   (let ((methods 
15552          (append
15553           (if (gnus-check-server gnus-select-method)
15554               ;; The native server is available.
15555               (cons gnus-select-method gnus-secondary-select-methods)
15556             ;; The native server is down, so we just do the
15557             ;; secondary ones.
15558             gnus-secondary-select-methods)
15559           ;; Also read from the archive server.
15560           (when gnus-message-archive-method
15561             (list "archive"))))
15562         list-type)
15563     (setq gnus-have-read-active-file nil)
15564     (save-excursion
15565       (set-buffer nntp-server-buffer)
15566       (while methods
15567         (let* ((method (if (stringp (car methods))
15568                            (gnus-server-get-method nil (car methods))
15569                          (car methods)))
15570                (where (nth 1 method))
15571                (mesg (format "Reading active file%s via %s..."
15572                              (if (and where (not (zerop (length where))))
15573                                  (concat " from " where) "")
15574                              (car method))))
15575           (gnus-message 5 mesg)
15576           (when (gnus-check-server method)
15577             ;; Request that the backend scan its incoming messages.
15578             (and (gnus-check-backend-function 'request-scan (car method))
15579                  (gnus-request-scan nil method))
15580             (cond
15581              ((and (eq gnus-read-active-file 'some)
15582                    (gnus-check-backend-function 'retrieve-groups (car method)))
15583               (let ((newsrc (cdr gnus-newsrc-alist))
15584                     (gmethod (if (stringp method)
15585                                  (gnus-server-get-method nil method)
15586                                method))
15587                     groups info)
15588                 (while (setq info (pop newsrc))
15589                   (when (gnus-server-equal
15590                          (gnus-find-method-for-group 
15591                           (gnus-info-group info) info)
15592                          gmethod)
15593                     (push (gnus-group-real-name (gnus-info-group info)) 
15594                           groups)))
15595                 (when groups
15596                   (gnus-check-server method)
15597                   (setq list-type (gnus-retrieve-groups groups method))
15598                   (cond
15599                    ((not list-type)
15600                     (gnus-message
15601                      1 "Cannot read partial active file from %s server."
15602                      (car method))
15603                     (ding)
15604                     (sit-for 2))
15605                    ((eq list-type 'active)
15606                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15607                    (t
15608                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15609              (t
15610               (if (not (gnus-request-list method))
15611                   (progn
15612                     (unless (equal method gnus-message-archive-method)
15613                       (gnus-message 1 "Cannot read active file from %s server."
15614                                     (car method))
15615                       (ding)))
15616                 (gnus-active-to-gnus-format method)
15617                 ;; We mark this active file as read.
15618                 (push method gnus-have-read-active-file)
15619                 (gnus-message 5 "%sdone" mesg))))))
15620         (setq methods (cdr methods))))))
15621
15622 ;; Read an active file and place the results in `gnus-active-hashtb'.
15623 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15624   (unless method
15625     (setq method gnus-select-method))
15626   (let ((cur (current-buffer))
15627         (hashtb (or hashtb
15628                     (if (and gnus-active-hashtb
15629                              (not (equal method gnus-select-method)))
15630                         gnus-active-hashtb
15631                       (setq gnus-active-hashtb
15632                             (if (equal method gnus-select-method)
15633                                 (gnus-make-hashtable
15634                                  (count-lines (point-min) (point-max)))
15635                               (gnus-make-hashtable 4096)))))))
15636     ;; Delete unnecessary lines.
15637     (goto-char (point-min))
15638     (while (search-forward "\nto." nil t)
15639       (delete-region (1+ (match-beginning 0))
15640                      (progn (forward-line 1) (point))))
15641     (or (string= gnus-ignored-newsgroups "")
15642         (progn
15643           (goto-char (point-min))
15644           (delete-matching-lines gnus-ignored-newsgroups)))
15645     ;; Make the group names readable as a lisp expression even if they
15646     ;; contain special characters.
15647     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15648     (goto-char (point-max))
15649     (while (re-search-backward "[][';?()#]" nil t)
15650       (insert ?\\))
15651     ;; If these are groups from a foreign select method, we insert the
15652     ;; group prefix in front of the group names.
15653     (and method (not (gnus-server-equal
15654                       (gnus-server-get-method nil method)
15655                       (gnus-server-get-method nil gnus-select-method)))
15656          (let ((prefix (gnus-group-prefixed-name "" method)))
15657            (goto-char (point-min))
15658            (while (and (not (eobp))
15659                        (progn (insert prefix)
15660                               (zerop (forward-line 1)))))))
15661     ;; Store the active file in a hash table.
15662     (goto-char (point-min))
15663     (if (string-match "%[oO]" gnus-group-line-format)
15664         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15665         ;; If we want information on moderated groups, we use this
15666         ;; loop...
15667         (let* ((mod-hashtb (make-vector 7 0))
15668                (m (intern "m" mod-hashtb))
15669                group max min)
15670           (while (not (eobp))
15671             (condition-case nil
15672                 (progn
15673                   (narrow-to-region (point) (gnus-point-at-eol))
15674                   (setq group (let ((obarray hashtb)) (read cur)))
15675                   (if (and (numberp (setq max (read cur)))
15676                            (numberp (setq min (read cur)))
15677                            (progn
15678                              (skip-chars-forward " \t")
15679                              (not
15680                               (or (= (following-char) ?=)
15681                                   (= (following-char) ?x)
15682                                   (= (following-char) ?j)))))
15683                       (set group (cons min max))
15684                     (set group nil))
15685                   ;; Enter moderated groups into a list.
15686                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15687                       (setq gnus-moderated-list
15688                             (cons (symbol-name group) gnus-moderated-list))))
15689               (error
15690                (and group
15691                     (symbolp group)
15692                     (set group nil))))
15693             (widen)
15694             (forward-line 1)))
15695       ;; And if we do not care about moderation, we use this loop,
15696       ;; which is faster.
15697       (let (group max min)
15698         (while (not (eobp))
15699           (condition-case ()
15700               (progn
15701                 (narrow-to-region (point) (gnus-point-at-eol))
15702                 ;; group gets set to a symbol interned in the hash table
15703                 ;; (what a hack!!) - jwz
15704                 (setq group (let ((obarray hashtb)) (read cur)))
15705                 (if (and (numberp (setq max (read cur)))
15706                          (numberp (setq min (read cur)))
15707                          (progn
15708                            (skip-chars-forward " \t")
15709                            (not
15710                             (or (= (following-char) ?=)
15711                                 (= (following-char) ?x)
15712                                 (= (following-char) ?j)))))
15713                     (set group (cons min max))
15714                   (set group nil)))
15715             (error
15716              (progn
15717                (and group
15718                     (symbolp group)
15719                     (set group nil))
15720                (or ignore-errors
15721                    (gnus-message 3 "Warning - illegal active: %s"
15722                                  (buffer-substring
15723                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15724           (widen)
15725           (forward-line 1))))))
15726
15727 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15728   ;; Parse a "groups" active file.
15729   (let ((cur (current-buffer))
15730         (hashtb (or hashtb
15731                     (if (and method gnus-active-hashtb)
15732                         gnus-active-hashtb
15733                       (setq gnus-active-hashtb
15734                             (gnus-make-hashtable
15735                              (count-lines (point-min) (point-max)))))))
15736         (prefix (and method
15737                      (not (gnus-server-equal
15738                            (gnus-server-get-method nil method)
15739                            (gnus-server-get-method nil gnus-select-method)))
15740                      (gnus-group-prefixed-name "" method))))
15741
15742     (goto-char (point-min))
15743     ;; We split this into to separate loops, one with the prefix
15744     ;; and one without to speed the reading up somewhat.
15745     (if prefix
15746         (let (min max opoint group)
15747           (while (not (eobp))
15748             (condition-case ()
15749                 (progn
15750                   (read cur) (read cur)
15751                   (setq min (read cur)
15752                         max (read cur)
15753                         opoint (point))
15754                   (skip-chars-forward " \t")
15755                   (insert prefix)
15756                   (goto-char opoint)
15757                   (set (let ((obarray hashtb)) (read cur))
15758                        (cons min max)))
15759               (error (and group (symbolp group) (set group nil))))
15760             (forward-line 1)))
15761       (let (min max group)
15762         (while (not (eobp))
15763           (condition-case ()
15764               (if (= (following-char) ?2)
15765                   (progn
15766                     (read cur) (read cur)
15767                     (setq min (read cur)
15768                           max (read cur))
15769                     (set (setq group (let ((obarray hashtb)) (read cur)))
15770                          (cons min max))))
15771             (error (and group (symbolp group) (set group nil))))
15772           (forward-line 1))))))
15773
15774 (defun gnus-read-newsrc-file (&optional force)
15775   "Read startup file.
15776 If FORCE is non-nil, the .newsrc file is read."
15777   ;; Reset variables that might be defined in the .newsrc.eld file.
15778   (let ((variables gnus-variable-list))
15779     (while variables
15780       (set (car variables) nil)
15781       (setq variables (cdr variables))))
15782   (let* ((newsrc-file gnus-current-startup-file)
15783          (quick-file (concat newsrc-file ".el")))
15784     (save-excursion
15785       ;; We always load the .newsrc.eld file.  If always contains
15786       ;; much information that can not be gotten from the .newsrc
15787       ;; file (ticked articles, killed groups, foreign methods, etc.)
15788       (gnus-read-newsrc-el-file quick-file)
15789
15790       (if (and (file-exists-p gnus-current-startup-file)
15791                (or force
15792                    (and (file-newer-than-file-p newsrc-file quick-file)
15793                         (file-newer-than-file-p newsrc-file
15794                                                 (concat quick-file "d")))
15795                    (not gnus-newsrc-alist)))
15796           ;; We read the .newsrc file.  Note that if there if a
15797           ;; .newsrc.eld file exists, it has already been read, and
15798           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15799           ;; the .newsrc file, Gnus will only use the information it
15800           ;; can find there for changing the data already read -
15801           ;; ie. reading the .newsrc file will not trash the data
15802           ;; already read (except for read articles).
15803           (save-excursion
15804             (gnus-message 5 "Reading %s..." newsrc-file)
15805             (set-buffer (find-file-noselect newsrc-file))
15806             (buffer-disable-undo (current-buffer))
15807             (gnus-newsrc-to-gnus-format)
15808             (kill-buffer (current-buffer))
15809             (gnus-message 5 "Reading %s...done" newsrc-file)))
15810
15811       ;; Read any slave files.
15812       (or gnus-slave
15813           (gnus-master-read-slave-newsrc)))))
15814
15815 (defun gnus-read-newsrc-el-file (file)
15816   (let ((ding-file (concat file "d")))
15817     ;; We always, always read the .eld file.
15818     (gnus-message 5 "Reading %s..." ding-file)
15819     (let (gnus-newsrc-assoc)
15820       (condition-case nil
15821           (load ding-file t t t)
15822         (error
15823          (gnus-message 1 "Error in %s" ding-file)
15824          (ding)))
15825       (when gnus-newsrc-assoc
15826         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15827     (gnus-make-hashtable-from-newsrc-alist)
15828     (when (file-newer-than-file-p file ding-file)
15829       ;; Old format quick file
15830       (gnus-message 5 "Reading %s..." file)
15831       ;; The .el file is newer than the .eld file, so we read that one
15832       ;; as well.
15833       (gnus-read-old-newsrc-el-file file))))
15834
15835 ;; Parse the old-style quick startup file
15836 (defun gnus-read-old-newsrc-el-file (file)
15837   (let (newsrc killed marked group m info)
15838     (prog1
15839         (let ((gnus-killed-assoc nil)
15840               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15841           (prog1
15842               (condition-case nil
15843                   (load file t t t)
15844                 (error nil))
15845             (setq newsrc gnus-newsrc-assoc
15846                   killed gnus-killed-assoc
15847                   marked gnus-marked-assoc)))
15848       (setq gnus-newsrc-alist nil)
15849       (while (setq info (gnus-get-info (setq group (pop newsrc))))
15850         (if info
15851             (progn
15852               (gnus-info-set-read info (cddr group))
15853               (gnus-info-set-level
15854                info (if (nth 1 group) gnus-level-default-subscribed
15855                       gnus-level-default-unsubscribed))
15856               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15857           (push (setq info
15858                       (list (car group)
15859                             (if (nth 1 group) gnus-level-default-subscribed
15860                               gnus-level-default-unsubscribed)
15861                             (cddr group)))
15862                 gnus-newsrc-alist))
15863         ;; Copy marks into info.
15864         (when (setq m (assoc (car group) marked))
15865           (unless (nthcdr 3 info)
15866             (nconc info (list nil)))
15867           (gnus-info-set-marks
15868            info (list (cons 'tick (gnus-compress-sequence 
15869                                    (sort (cdr m) '<) t))))))
15870       (setq newsrc killed)
15871       (while newsrc
15872         (setcar newsrc (caar newsrc))
15873         (setq newsrc (cdr newsrc)))
15874       (setq gnus-killed-list killed))
15875     ;; The .el file version of this variable does not begin with
15876     ;; "options", while the .eld version does, so we just add it if it
15877     ;; isn't there.
15878     (and
15879      gnus-newsrc-options
15880      (progn
15881        (and (not (string-match "^ *options" gnus-newsrc-options))
15882             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15883        (and (not (string-match "\n$" gnus-newsrc-options))
15884             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15885        ;; Finally, if we read some options lines, we parse them.
15886        (or (string= gnus-newsrc-options "")
15887            (gnus-newsrc-parse-options gnus-newsrc-options))))
15888
15889     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15890     (gnus-make-hashtable-from-newsrc-alist)))
15891
15892 (defun gnus-make-newsrc-file (file)
15893   "Make server dependent file name by catenating FILE and server host name."
15894   (let* ((file (expand-file-name file nil))
15895          (real-file (concat file "-" (nth 1 gnus-select-method))))
15896     (if (or (file-exists-p real-file)
15897             (file-exists-p (concat real-file ".el"))
15898             (file-exists-p (concat real-file ".eld")))
15899         real-file file)))
15900
15901 (defun gnus-newsrc-to-gnus-format ()
15902   (setq gnus-newsrc-options "")
15903   (setq gnus-newsrc-options-n nil)
15904
15905   (or gnus-active-hashtb
15906       (setq gnus-active-hashtb (make-vector 4095 0)))
15907   (let ((buf (current-buffer))
15908         (already-read (> (length gnus-newsrc-alist) 1))
15909         group subscribed options-symbol newsrc Options-symbol
15910         symbol reads num1)
15911     (goto-char (point-min))
15912     ;; We intern the symbol `options' in the active hashtb so that we
15913     ;; can `eq' against it later.
15914     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15915     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15916
15917     (while (not (eobp))
15918       ;; We first read the first word on the line by narrowing and
15919       ;; then reading into `gnus-active-hashtb'.  Most groups will
15920       ;; already exist in that hashtb, so this will save some string
15921       ;; space.
15922       (narrow-to-region
15923        (point)
15924        (progn (skip-chars-forward "^ \t!:\n") (point)))
15925       (goto-char (point-min))
15926       (setq symbol
15927             (and (/= (point-min) (point-max))
15928                  (let ((obarray gnus-active-hashtb)) (read buf))))
15929       (widen)
15930       ;; Now, the symbol we have read is either `options' or a group
15931       ;; name.  If it is an options line, we just add it to a string.
15932       (cond
15933        ((or (eq symbol options-symbol)
15934             (eq symbol Options-symbol))
15935         (setq gnus-newsrc-options
15936               ;; This concating is quite inefficient, but since our
15937               ;; thorough studies show that approx 99.37% of all
15938               ;; .newsrc files only contain a single options line, we
15939               ;; don't give a damn, frankly, my dear.
15940               (concat gnus-newsrc-options
15941                       (buffer-substring
15942                        (gnus-point-at-bol)
15943                        ;; Options may continue on the next line.
15944                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15945                                 (progn (beginning-of-line) (point)))
15946                            (point)))))
15947         (forward-line -1))
15948        (symbol
15949         ;; Group names can be just numbers.  
15950         (when (numberp symbol) 
15951           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15952         (or (boundp symbol) (set symbol nil))
15953         ;; It was a group name.
15954         (setq subscribed (= (following-char) ?:)
15955               group (symbol-name symbol)
15956               reads nil)
15957         (if (eolp)
15958             ;; If the line ends here, this is clearly a buggy line, so
15959             ;; we put point a the beginning of line and let the cond
15960             ;; below do the error handling.
15961             (beginning-of-line)
15962           ;; We skip to the beginning of the ranges.
15963           (skip-chars-forward "!: \t"))
15964         ;; We are now at the beginning of the list of read articles.
15965         ;; We read them range by range.
15966         (while
15967             (cond
15968              ((looking-at "[0-9]+")
15969               ;; We narrow and read a number instead of buffer-substring/
15970               ;; string-to-int because it's faster.  narrow/widen is
15971               ;; faster than save-restriction/narrow, and save-restriction
15972               ;; produces a garbage object.
15973               (setq num1 (progn
15974                            (narrow-to-region (match-beginning 0) (match-end 0))
15975                            (read buf)))
15976               (widen)
15977               ;; If the next character is a dash, then this is a range.
15978               (if (= (following-char) ?-)
15979                   (progn
15980                     ;; We read the upper bound of the range.
15981                     (forward-char 1)
15982                     (if (not (looking-at "[0-9]+"))
15983                         ;; This is a buggy line, by we pretend that
15984                         ;; it's kinda OK.  Perhaps the user should be
15985                         ;; dinged?
15986                         (setq reads (cons num1 reads))
15987                       (setq reads
15988                             (cons
15989                              (cons num1
15990                                    (progn
15991                                      (narrow-to-region (match-beginning 0)
15992                                                        (match-end 0))
15993                                      (read buf)))
15994                              reads))
15995                       (widen)))
15996                 ;; It was just a simple number, so we add it to the
15997                 ;; list of ranges.
15998                 (setq reads (cons num1 reads)))
15999               ;; If the next char in ?\n, then we have reached the end
16000               ;; of the line and return nil.
16001               (/= (following-char) ?\n))
16002              ((= (following-char) ?\n)
16003               ;; End of line, so we end.
16004               nil)
16005              (t
16006               ;; Not numbers and not eol, so this might be a buggy
16007               ;; line...
16008               (or (eobp)
16009                   ;; If it was eob instead of ?\n, we allow it.
16010                   (progn
16011                     ;; The line was buggy.
16012                     (setq group nil)
16013                     (gnus-message 3 "Mangled line: %s"
16014                                   (buffer-substring (gnus-point-at-bol)
16015                                                     (gnus-point-at-eol)))
16016                     (ding)
16017                     (sit-for 1)))
16018               nil))
16019           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16020           ;; we allow them, because it's a common mistake to put a
16021           ;; space after the comma.
16022           (skip-chars-forward ", "))
16023
16024         ;; We have already read .newsrc.eld, so we gently update the
16025         ;; data in the hash table with the information we have just
16026         ;; read.
16027         (when group
16028           (let ((info (gnus-get-info group))
16029                 level)
16030             (if info
16031                 ;; There is an entry for this file in the alist.
16032                 (progn
16033                   (gnus-info-set-read info (nreverse reads))
16034                   ;; We update the level very gently.  In fact, we
16035                   ;; only change it if there's been a status change
16036                   ;; from subscribed to unsubscribed, or vice versa.
16037                   (setq level (gnus-info-level info))
16038                   (cond ((and (<= level gnus-level-subscribed)
16039                               (not subscribed))
16040                          (setq level (if reads
16041                                          gnus-level-default-unsubscribed
16042                                        (1+ gnus-level-default-unsubscribed))))
16043                         ((and (> level gnus-level-subscribed) subscribed)
16044                          (setq level gnus-level-default-subscribed)))
16045                   (gnus-info-set-level info level))
16046               ;; This is a new group.
16047               (setq info (list group
16048                                (if subscribed
16049                                    gnus-level-default-subscribed
16050                                  (if reads
16051                                      (1+ gnus-level-subscribed)
16052                                    gnus-level-default-unsubscribed))
16053                                (nreverse reads))))
16054             (setq newsrc (cons info newsrc))))))
16055       (forward-line 1))
16056
16057     (setq newsrc (nreverse newsrc))
16058
16059     (if (not already-read)
16060         ()
16061       ;; We now have two newsrc lists - `newsrc', which is what we
16062       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16063       ;; what we've read from .newsrc.eld.  We have to merge these
16064       ;; lists.  We do this by "attaching" any (foreign) groups in the
16065       ;; gnus-newsrc-alist to the (native) group that precedes them.
16066       (let ((rc (cdr gnus-newsrc-alist))
16067             (prev gnus-newsrc-alist)
16068             entry mentry)
16069         (while rc
16070           (or (null (nth 4 (car rc)))   ; It's a native group.
16071               (assoc (caar rc) newsrc) ; It's already in the alist.
16072               (if (setq entry (assoc (caar prev) newsrc))
16073                   (setcdr (setq mentry (memq entry newsrc))
16074                           (cons (car rc) (cdr mentry)))
16075                 (setq newsrc (cons (car rc) newsrc))))
16076           (setq prev rc
16077                 rc (cdr rc)))))
16078
16079     (setq gnus-newsrc-alist newsrc)
16080     ;; We make the newsrc hashtb.
16081     (gnus-make-hashtable-from-newsrc-alist)
16082
16083     ;; Finally, if we read some options lines, we parse them.
16084     (or (string= gnus-newsrc-options "")
16085         (gnus-newsrc-parse-options gnus-newsrc-options))))
16086
16087 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16088 ;; The return value will be a list on the form
16089 ;; ((regexp1 . ignore)
16090 ;;  (regexp2 . subscribe)...)
16091 ;; When handling new newsgroups, groups that match a `ignore' regexp
16092 ;; will be ignored, and groups that match a `subscribe' regexp will be
16093 ;; subscribed.  A line like
16094 ;; options -n !all rec.all
16095 ;; will lead to a list that looks like
16096 ;; (("^rec\\..+" . subscribe)
16097 ;;  ("^.+" . ignore))
16098 ;; So all "rec.*" groups will be subscribed, while all the other
16099 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16100 ;; different from "options -n rec.all !all".
16101 (defun gnus-newsrc-parse-options (options)
16102   (let (out eol)
16103     (save-excursion
16104       (gnus-set-work-buffer)
16105       (insert (regexp-quote options))
16106       ;; First we treat all continuation lines.
16107       (goto-char (point-min))
16108       (while (re-search-forward "\n[ \t]+" nil t)
16109         (replace-match " " t t))
16110       ;; Then we transform all "all"s into ".+"s.
16111       (goto-char (point-min))
16112       (while (re-search-forward "\\ball\\b" nil t)
16113         (replace-match ".+" t t))
16114       (goto-char (point-min))
16115       ;; We remove all other options than the "-n" ones.
16116       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16117         (replace-match " ")
16118         (forward-char -1))
16119       (goto-char (point-min))
16120
16121       ;; We are only interested in "options -n" lines - we
16122       ;; ignore the other option lines.
16123       (while (re-search-forward "[ \t]-n" nil t)
16124         (setq eol
16125               (or (save-excursion
16126                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16127                          (- (point) 2)))
16128                   (gnus-point-at-eol)))
16129         ;; Search for all "words"...
16130         (while (re-search-forward "[^ \t,\n]+" eol t)
16131           (if (= (char-after (match-beginning 0)) ?!)
16132               ;; If the word begins with a bang (!), this is a "not"
16133               ;; spec.  We put this spec (minus the bang) and the
16134               ;; symbol `ignore' into the list.
16135               (setq out (cons (cons (concat
16136                                      "^" (buffer-substring
16137                                           (1+ (match-beginning 0))
16138                                           (match-end 0)))
16139                                     'ignore) out))
16140             ;; There was no bang, so this is a "yes" spec.
16141             (setq out (cons (cons (concat "^" (match-string 0))
16142                                   'subscribe) out)))))
16143
16144       (setq gnus-newsrc-options-n out))))
16145
16146 (defun gnus-save-newsrc-file (&optional force)
16147   "Save .newsrc file."
16148   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16149   ;; from the variable gnus-newsrc-alist.
16150   (when (and (or gnus-newsrc-alist gnus-killed-list)
16151              gnus-current-startup-file)
16152     (save-excursion
16153       (if (and (or gnus-use-dribble-file gnus-slave)
16154                (not force)
16155                (or (not gnus-dribble-buffer)
16156                    (not (buffer-name gnus-dribble-buffer))
16157                    (zerop (save-excursion
16158                             (set-buffer gnus-dribble-buffer)
16159                             (buffer-size)))))
16160           (gnus-message 4 "(No changes need to be saved)")
16161         (run-hooks 'gnus-save-newsrc-hook)
16162         (if gnus-slave
16163             (gnus-slave-save-newsrc)
16164           ;; Save .newsrc.
16165           (when gnus-save-newsrc-file
16166             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16167             (gnus-gnus-to-newsrc-format)
16168             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16169           ;; Save .newsrc.eld.
16170           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16171           (make-local-variable 'version-control)
16172           (setq version-control 'never)
16173           (setq buffer-file-name
16174                 (concat gnus-current-startup-file ".eld"))
16175           (gnus-add-current-to-buffer-list)
16176           (buffer-disable-undo (current-buffer))
16177           (erase-buffer)
16178           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16179           (gnus-gnus-to-quick-newsrc-format)
16180           (run-hooks 'gnus-save-quick-newsrc-hook)
16181           (save-buffer)
16182           (kill-buffer (current-buffer))
16183           (gnus-message
16184            5 "Saving %s.eld...done" gnus-current-startup-file))
16185         (gnus-dribble-delete-file)))))
16186
16187 (defun gnus-gnus-to-quick-newsrc-format ()
16188   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16189   (insert ";; Gnus startup file.\n")
16190   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16191   (insert ";; to read .newsrc.\n")
16192   (insert "(setq gnus-newsrc-file-version "
16193           (prin1-to-string gnus-version) ")\n")
16194   (let ((variables
16195          (if gnus-save-killed-list gnus-variable-list
16196            ;; Remove the `gnus-killed-list' from the list of variables
16197            ;; to be saved, if required.
16198            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16199         ;; Peel off the "dummy" group.
16200         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16201         variable)
16202     ;; Insert the variables into the file.
16203     (while variables
16204       (when (and (boundp (setq variable (pop variables)))
16205                  (symbol-value variable))
16206         (insert "(setq " (symbol-name variable) " '")
16207         (prin1 (symbol-value variable) (current-buffer))
16208         (insert ")\n")))))
16209
16210 (defun gnus-gnus-to-newsrc-format ()
16211   ;; Generate and save the .newsrc file.
16212   (save-excursion
16213     (set-buffer (create-file-buffer gnus-current-startup-file))
16214     (let ((newsrc (cdr gnus-newsrc-alist))
16215           (standard-output (current-buffer))
16216           info ranges range)
16217       (setq buffer-file-name gnus-current-startup-file)
16218       (buffer-disable-undo (current-buffer))
16219       (erase-buffer)
16220       ;; Write options.
16221       (if gnus-newsrc-options (insert gnus-newsrc-options))
16222       ;; Write subscribed and unsubscribed.
16223       (while (setq info (pop newsrc))
16224         ;; Don't write foreign groups to .newsrc.
16225         (when (gnus-server-equal (gnus-info-method info) gnus-select-method)
16226           (insert (gnus-info-group info)
16227                   (if (> (gnus-info-level info) gnus-level-subscribed)
16228                       "!" ":"))
16229           (when (setq ranges (gnus-info-read info))
16230             (insert " ")
16231             (if (not (listp (cdr ranges)))
16232                 (if (= (car ranges) (cdr ranges))
16233                     (princ (car ranges))
16234                   (princ (car ranges))
16235                   (insert "-")
16236                   (princ (cdr ranges)))
16237               (while ranges
16238                 (setq range (car ranges)
16239                       ranges (cdr ranges))
16240                 (if (or (atom range) (= (car range) (cdr range)))
16241                     (princ (or (and (atom range) range) (car range)))
16242                   (princ (car range))
16243                   (insert "-")
16244                   (princ (cdr range)))
16245                 (if ranges (insert ",")))))
16246           (insert "\n")))
16247       (make-local-variable 'version-control)
16248       (setq version-control 'never)
16249       ;; It has been reported that sometime the modtime on the .newsrc
16250       ;; file seems to be off.  We really do want to overwrite it, so
16251       ;; we clear the modtime here before saving.  It's a bit odd,
16252       ;; though...
16253       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16254       ;; delete the silly thing entirely first.  but this fails to provide
16255       ;; such niceties as .newsrc~ creation.
16256       (if gnus-modtime-botch
16257           (delete-file gnus-startup-file)
16258         (clear-visited-file-modtime))
16259       (run-hooks 'gnus-save-standard-newsrc-hook)
16260       (save-buffer)
16261       (kill-buffer (current-buffer)))))
16262
16263 \f
16264 ;;;
16265 ;;; Slave functions.
16266 ;;;
16267
16268 (defun gnus-slave-save-newsrc ()
16269   (save-excursion
16270     (set-buffer gnus-dribble-buffer)
16271     (let ((slave-name
16272            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16273       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16274
16275 (defun gnus-master-read-slave-newsrc ()
16276   (let ((slave-files
16277          (directory-files
16278           (file-name-directory gnus-current-startup-file)
16279           t (concat
16280              "^" (regexp-quote
16281                   (concat
16282                    (file-name-nondirectory gnus-current-startup-file)
16283                    "-slave-")))
16284           t))
16285         file)
16286     (if (not slave-files)
16287         ()                              ; There are no slave files to read.
16288       (gnus-message 7 "Reading slave newsrcs...")
16289       (save-excursion
16290         (set-buffer (get-buffer-create " *gnus slave*"))
16291         (buffer-disable-undo (current-buffer))
16292         (setq slave-files
16293               (sort (mapcar (lambda (file)
16294                               (list (nth 5 (file-attributes file)) file))
16295                             slave-files)
16296                     (lambda (f1 f2)
16297                       (or (< (caar f1) (caar f2))
16298                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16299         (while slave-files
16300           (erase-buffer)
16301           (setq file (nth 1 (car slave-files)))
16302           (insert-file-contents file)
16303           (if (condition-case ()
16304                   (progn
16305                     (eval-buffer (current-buffer))
16306                     t)
16307                 (error
16308                  (gnus-message 3 "Possible error in %s" file)
16309                  (ding)
16310                  (sit-for 2)
16311                  nil))
16312               (or gnus-slave ; Slaves shouldn't delete these files.
16313                   (condition-case ()
16314                       (delete-file file)
16315                     (error nil))))
16316           (setq slave-files (cdr slave-files))))
16317       (gnus-message 7 "Reading slave newsrcs...done"))))
16318
16319 \f
16320 ;;;
16321 ;;; Group description.
16322 ;;;
16323
16324 (defun gnus-read-all-descriptions-files ()
16325   (let ((methods (cons gnus-select-method 
16326                        (nconc
16327                         (when gnus-message-archive-method
16328                           (list "archive"))
16329                         gnus-secondary-select-methods))))
16330     (while methods
16331       (gnus-read-descriptions-file (car methods))
16332       (setq methods (cdr methods)))
16333     t))
16334
16335 (defun gnus-read-descriptions-file (&optional method)
16336   (let ((method (or method gnus-select-method)))
16337     (when (stringp method)
16338       (setq method (gnus-server-to-method method)))
16339     ;; We create the hashtable whether we manage to read the desc file
16340     ;; to avoid trying to re-read after a failed read.
16341     (or gnus-description-hashtb
16342         (setq gnus-description-hashtb
16343               (gnus-make-hashtable (length gnus-active-hashtb))))
16344     ;; Mark this method's desc file as read.
16345     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16346                   gnus-description-hashtb)
16347
16348     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16349     (cond
16350      ((not (gnus-check-server method))
16351       (gnus-message 1 "Couldn't open server")
16352       nil)
16353      ((not (gnus-request-list-newsgroups method))
16354       (gnus-message 1 "Couldn't read newsgroups descriptions")
16355       nil)
16356      (t
16357       (let (group)
16358         (save-excursion
16359           (save-restriction
16360             (set-buffer nntp-server-buffer)
16361             (goto-char (point-min))
16362             (if (or (search-forward "\n.\n" nil t)
16363                     (goto-char (point-max)))
16364                 (progn
16365                   (beginning-of-line)
16366                   (narrow-to-region (point-min) (point))))
16367             (goto-char (point-min))
16368             (while (not (eobp))
16369               ;; If we get an error, we set group to 0, which is not a
16370               ;; symbol...
16371               (setq group
16372                     (condition-case ()
16373                         (let ((obarray gnus-description-hashtb))
16374                           ;; Group is set to a symbol interned in this
16375                           ;; hash table.
16376                           (read nntp-server-buffer))
16377                       (error 0)))
16378               (skip-chars-forward " \t")
16379               ;; ...  which leads to this line being effectively ignored.
16380               (and (symbolp group)
16381                    (set group (buffer-substring
16382                                (point) (progn (end-of-line) (point)))))
16383               (forward-line 1))))
16384         (gnus-message 5 "Reading descriptions file...done")
16385         t)))))
16386
16387 (defun gnus-group-get-description (group)
16388   "Get the description of a group by sending XGTITLE to the server."
16389   (when (gnus-request-group-description group)
16390     (save-excursion
16391       (set-buffer nntp-server-buffer)
16392       (goto-char (point-min))
16393       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16394         (match-string 1)))))
16395
16396 \f
16397 ;;;
16398 ;;; Shutdown
16399 ;;;
16400
16401 (defvar gnus-shutdown-alist nil)
16402
16403 (defun gnus-add-shutdown (function &rest symbols)
16404   "Run FUNCTION whenever one of SYMBOLS is shut down."
16405   (push (cons function symbols) gnus-shutdown-alist))
16406
16407 (defun gnus-shutdown (symbol)
16408   "Shut down everything that waits for SYMBOL."
16409   (let ((alist gnus-shutdown-alist)
16410         entry)
16411     (while (setq entry (pop alist))
16412       (when (memq symbol (cdr entry))
16413         (funcall (car entry))))))
16414
16415 \f
16416 ;;;
16417 ;;; Buffering of read articles.
16418 ;;;
16419
16420 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16421 (defvar gnus-backlog-articles nil)
16422 (defvar gnus-backlog-hashtb nil)
16423
16424 (defun gnus-backlog-buffer ()
16425   "Return the backlog buffer."
16426   (or (get-buffer gnus-backlog-buffer)
16427       (save-excursion
16428         (set-buffer (get-buffer-create gnus-backlog-buffer))
16429         (buffer-disable-undo (current-buffer))
16430         (setq buffer-read-only t)
16431         (gnus-add-current-to-buffer-list)
16432         (get-buffer gnus-backlog-buffer))))
16433
16434 (defun gnus-backlog-setup ()
16435   "Initialize backlog variables."
16436   (unless gnus-backlog-hashtb
16437     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16438
16439 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16440
16441 (defun gnus-backlog-shutdown ()
16442   "Clear all backlog variables and buffers."
16443   (when (get-buffer gnus-backlog-buffer)
16444     (kill-buffer gnus-backlog-buffer))
16445   (setq gnus-backlog-hashtb nil
16446         gnus-backlog-articles nil))
16447
16448 (defun gnus-backlog-enter-article (group number buffer)
16449   (gnus-backlog-setup)
16450   (let ((ident (intern (concat group ":" (int-to-string number))
16451                        gnus-backlog-hashtb))
16452         b)
16453     (if (memq ident gnus-backlog-articles)
16454         () ; It's already kept.
16455       ;; Remove the oldest article, if necessary.
16456       (and (numberp gnus-keep-backlog)
16457            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16458            (gnus-backlog-remove-oldest-article))
16459       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16460       ;; Insert the new article.
16461       (save-excursion
16462         (set-buffer (gnus-backlog-buffer))
16463         (let (buffer-read-only)
16464           (goto-char (point-max))
16465           (or (bolp) (insert "\n"))
16466           (setq b (point))
16467           (insert-buffer-substring buffer)
16468           ;; Tag the beginning of the article with the ident.
16469           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16470
16471 (defun gnus-backlog-remove-oldest-article ()
16472   (save-excursion
16473     (set-buffer (gnus-backlog-buffer))
16474     (goto-char (point-min))
16475     (if (zerop (buffer-size))
16476         () ; The buffer is empty.
16477       (let ((ident (get-text-property (point) 'gnus-backlog))
16478             buffer-read-only)
16479         ;; Remove the ident from the list of articles.
16480         (when ident
16481           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16482         ;; Delete the article itself.
16483         (delete-region
16484          (point) (next-single-property-change
16485                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16486
16487 (defun gnus-backlog-request-article (group number buffer)
16488   (when (numberp number)
16489     (gnus-backlog-setup)
16490     (let ((ident (intern (concat group ":" (int-to-string number))
16491                          gnus-backlog-hashtb))
16492           beg end)
16493       (when (memq ident gnus-backlog-articles)
16494         ;; It was in the backlog.
16495         (save-excursion
16496           (set-buffer (gnus-backlog-buffer))
16497           (if (not (setq beg (text-property-any
16498                               (point-min) (point-max) 'gnus-backlog
16499                               ident)))
16500               ;; It wasn't in the backlog after all.
16501               (ignore
16502                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16503             ;; Find the end (i. e., the beginning of the next article).
16504             (setq end
16505                   (next-single-property-change
16506                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16507         (let ((buffer-read-only nil))
16508           (erase-buffer)
16509           (insert-buffer-substring gnus-backlog-buffer beg end)
16510           t)))))
16511
16512 ;; Allow redefinition of Gnus functions.
16513
16514 (gnus-ems-redefine)
16515
16516 (provide 'gnus)
16517
16518 ;;; gnus.el ends here