*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval '(run-hooks 'gnus-load-hook))
29
30 (require 'mail-utils)
31 (require 'timezone)
32 (require 'nnheader)
33
34 (eval-when-compile (require 'cl))
35
36 ;; Site dependent variables.  These variables should be defined in
37 ;; paths.el.
38
39 (defvar gnus-default-nntp-server nil
40   "Specify a default NNTP server.
41 This variable should be defined in paths.el, and should never be set
42 by the user.
43 If you want to change servers, you should use `gnus-select-method'.
44 See the documentation to that variable.")
45
46 (defvar gnus-backup-default-subscribed-newsgroups
47   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
48   "Default default new newsgroups the first time Gnus is run.
49 Should be set in paths.el, and shouldn't be touched by the user.")
50
51 (defvar gnus-local-domain nil
52   "Local domain name without a host name.
53 The DOMAINNAME environment variable is used instead if it is defined.
54 If the `system-name' function returns the full Internet name, there is
55 no need to set this variable.")
56
57 (defvar gnus-local-organization nil
58   "String with a description of what organization (if any) the user belongs to.
59 The ORGANIZATION environment variable is used instead if it is defined.
60 If this variable contains a function, this function will be called
61 with the current newsgroup name as the argument.  The function should
62 return a string.
63
64 In any case, if the string (either in the variable, in the environment
65 variable, or returned by the function) is a file name, the contents of
66 this file will be used as the organization.")
67
68 (defvar gnus-use-generic-from nil
69   "If nil, the full host name will be the system name prepended to the domain name.
70 If this is a string, the full host name will be this string.
71 If this is non-nil, non-string, the domain name will be used as the
72 full host name.")
73
74 (defvar gnus-use-generic-path nil
75   "If nil, use the NNTP server name in the Path header.
76 If stringp, use this; if non-nil, use no host name (user name only).")
77
78 ;; Customization variables
79
80 ;; Don't touch this variable.
81 (defvar gnus-nntp-service "nntp"
82   "*NNTP service name (\"nntp\" or 119).
83 This is an obsolete variable, which is scarcely used.  If you use an
84 nntp server for your newsgroup and want to change the port number
85 used to 899, you would say something along these lines:
86
87  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
88
89 (defvar gnus-nntpserver-file "/etc/nntpserver"
90   "*A file with only the name of the nntp server in it.")
91
92 ;; This function is used to check both the environment variable
93 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
94 ;; an nntp server name default.
95 (defun gnus-getenv-nntpserver ()
96   (or (getenv "NNTPSERVER")
97       (and (file-readable-p gnus-nntpserver-file)
98            (save-excursion
99              (set-buffer (get-buffer-create " *gnus nntp*"))
100              (buffer-disable-undo (current-buffer))
101              (insert-file-contents gnus-nntpserver-file)
102              (let ((name (buffer-string)))
103                (prog1
104                    (if (string-match "^[ \t\n]*$" name)
105                        nil
106                      name)
107                  (kill-buffer (current-buffer))))))))
108
109 (defvar gnus-select-method
110   (nconc
111    (list 'nntp (or (condition-case ()
112                        (gnus-getenv-nntpserver)
113                      (error nil))
114                    (if (and gnus-default-nntp-server
115                             (not (string= gnus-default-nntp-server "")))
116                        gnus-default-nntp-server)
117                    (system-name)))
118    (if (or (null gnus-nntp-service)
119            (equal gnus-nntp-service "nntp"))
120        nil
121      (list gnus-nntp-service)))
122   "*Default method for selecting a newsgroup.
123 This variable should be a list, where the first element is how the
124 news is to be fetched, the second is the address.
125
126 For instance, if you want to get your news via NNTP from
127 \"flab.flab.edu\", you could say:
128
129 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
130
131 If you want to use your local spool, say:
132
133 (setq gnus-select-method (list 'nnspool (system-name)))
134
135 If you use this variable, you must set `gnus-nntp-server' to nil.
136
137 There is a lot more to know about select methods and virtual servers -
138 see the manual for details.")
139
140 (defvar gnus-message-archive-method 
141   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
142              (nnfolder-active-file "~/Mail/archive/active")
143              (nnfolder-get-new-mail nil)
144              (nnfolder-inhibit-expiry t))
145   "*Method used for archiving messages you've sent.
146 This should be a mail method.")
147
148 (defvar gnus-refer-article-method nil
149   "*Preferred method for fetching an article by Message-ID.
150 If you are reading news from the local spool (with nnspool), fetching
151 articles by Message-ID is painfully slow.  By setting this method to an
152 nntp method, you might get acceptable results.
153
154 The value of this variable must be a valid select method as discussed
155 in the documentation of `gnus-select-method'.")
156
157 (defvar gnus-secondary-select-methods nil
158   "*A list of secondary methods that will be used for reading news.
159 This is a list where each element is a complete select method (see
160 `gnus-select-method').
161
162 If, for instance, you want to read your mail with the nnml backend,
163 you could set this variable:
164
165 (setq gnus-secondary-select-methods '((nnml \"\")))")
166
167 (defvar gnus-secondary-servers nil
168   "*List of NNTP servers that the user can choose between interactively.
169 To make Gnus query you for a server, you have to give `gnus' a
170 non-numeric prefix - `C-u M-x gnus', in short.")
171
172 (defvar gnus-nntp-server nil
173   "*The name of the host running the NNTP server.
174 This variable is semi-obsolete.  Use the `gnus-select-method'
175 variable instead.")
176
177 (defvar gnus-startup-file "~/.newsrc"
178   "*Your `.newsrc' file.
179 `.newsrc-SERVER' will be used instead if that exists.")
180
181 (defvar gnus-init-file "~/.gnus"
182   "*Your Gnus elisp startup file.
183 If a file with the .el or .elc suffixes exist, it will be read
184 instead.")
185
186 (defvar gnus-group-faq-directory
187   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
188     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
189     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
190     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
191     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
192     "/ftp@ftp.sunet.se:/pub/usenet/"
193     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
194     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
195     "/ftp@ftp.hk.super.net:/mirror/faqs/")
196   "*Directory where the group FAQs are stored.
197 This will most commonly be on a remote machine, and the file will be
198 fetched by ange-ftp.
199
200 This variable can also be a list of directories.  In that case, the
201 first element in the list will be used by default, and the others will
202 be used as backup sites.
203
204 Note that Gnus uses an aol machine as the default directory.  If this
205 feels fundamentally unclean, just think of it as a way to finally get
206 something of value back from them.
207
208 If the default site is too slow, try one of these:
209
210    North America: mirrors.aol.com                /pub/rtfm/usenet
211                   ftp.seas.gwu.edu               /pub/rtfm
212                   rtfm.mit.edu                   /pub/usenet/news.answers
213    Europe:        ftp.uni-paderborn.de           /pub/FAQ
214                   src.doc.ic.ac.uk               /usenet/news-FAQS
215                   ftp.sunet.se                   /pub/usenet
216    Asia:          nctuccca.edu.tw                /USENET/FAQ
217                   hwarang.postech.ac.kr          /pub/usenet/news.answers
218                   ftp.hk.super.net               /mirror/faqs")
219
220 (defvar gnus-group-archive-directory
221   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
222   "*The address of the (ding) archives.")
223
224 (defvar gnus-group-recent-archive-directory
225   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
226   "*The address of the most recent (ding) articles.")
227
228 (defvar gnus-default-subscribed-newsgroups nil
229   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
230 It should be a list of strings.
231 If it is `t', Gnus will not do anything special the first time it is
232 started; it'll just use the normal newsgroups subscription methods.")
233
234 (defvar gnus-use-cross-reference t
235   "*Non-nil means that cross referenced articles will be marked as read.
236 If nil, ignore cross references.  If t, mark articles as read in
237 subscribed newsgroups.  If neither t nor nil, mark as read in all
238 newsgroups.")
239
240 (defvar gnus-single-article-buffer t
241   "*If non-nil, display all articles in the same buffer.
242 If nil, each group will get its own article buffer.")
243
244 (defvar gnus-use-dribble-file t
245   "*Non-nil means that Gnus will use a dribble file to store user updates.
246 If Emacs should crash without saving the .newsrc files, complete
247 information can be restored from the dribble file.")
248
249 (defvar gnus-dribble-directory nil
250   "*The directory where dribble files will be saved.
251 If this variable is nil, the directory where the .newsrc files are
252 saved will be used.")
253
254 (defvar gnus-asynchronous nil
255   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
256
257 (defvar gnus-kill-summary-on-exit t
258   "*If non-nil, kill the summary buffer when you exit from it.
259 If nil, the summary will become a \"*Dead Summary*\" buffer, and
260 it will be killed sometime later.")
261
262 (defvar gnus-large-newsgroup 200
263   "*The number of articles which indicates a large newsgroup.
264 If the number of articles in a newsgroup is greater than this value,
265 confirmation is required for selecting the newsgroup.")
266
267 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
268 (defvar gnus-no-groups-message "No news is horrible news"
269   "*Message displayed by Gnus when no groups are available.")
270
271 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
272   "*Non-nil means that the default name of a file to save articles in is the group name.
273 If it's nil, the directory form of the group name is used instead.
274
275 If this variable is a list, and the list contains the element
276 `not-score', long file names will not be used for score files; if it
277 contains the element `not-save', long file names will not be used for
278 saving; and if it contains the element `not-kill', long file names
279 will not be used for kill files.")
280
281 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
282   "*Name of the directory articles will be saved in (default \"~/News\").
283 Initialized from the SAVEDIR environment variable.")
284
285 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
286   "*Name of the directory where kill files will be stored (default \"~/News\").
287 Initialized from the SAVEDIR environment variable.")
288
289 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
290   "*A function to save articles in your favorite format.
291 The function must be interactively callable (in other words, it must
292 be an Emacs command).
293
294 Gnus provides the following functions:
295
296 * gnus-summary-save-in-rmail (Rmail format)
297 * gnus-summary-save-in-mail (Unix mail format)
298 * gnus-summary-save-in-folder (MH folder)
299 * gnus-summary-save-in-file (article format).
300 * gnus-summary-save-in-vm (use VM's folder format).")
301
302 (defvar gnus-prompt-before-saving 'always
303   "*This variable says how much prompting is to be done when saving articles.
304 If it is nil, no prompting will be done, and the articles will be
305 saved to the default files.  If this variable is `always', each and
306 every article that is saved will be preceded by a prompt, even when
307 saving large batches of articles.  If this variable is neither nil not
308 `always', there the user will be prompted once for a file name for
309 each invocation of the saving commands.")
310
311 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
312   "*A function generating a file name to save articles in Rmail format.
313 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
314
315 (defvar gnus-mail-save-name (function gnus-plain-save-name)
316   "*A function generating a file name to save articles in Unix mail format.
317 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
318
319 (defvar gnus-folder-save-name (function gnus-folder-save-name)
320   "*A function generating a file name to save articles in MH folder.
321 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
322
323 (defvar gnus-file-save-name (function gnus-numeric-save-name)
324   "*A function generating a file name to save articles in article format.
325 The function is called with NEWSGROUP, HEADERS, and optional
326 LAST-FILE.")
327
328 (defvar gnus-split-methods
329   '((gnus-article-archive-name))
330   "*Variable used to suggest where articles are to be saved.
331 For instance, if you would like to save articles related to Gnus in
332 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
333 you could set this variable to something like:
334
335  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
336    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
337
338 This variable is an alist where the where the key is the match and the
339 value is a list of possible files to save in if the match is non-nil.
340
341 If the match is a string, it is used as a regexp match on the
342 article.  If the match is a symbol, that symbol will be funcalled
343 from the buffer of the article to be saved with the newsgroup as the
344 parameter.  If it is a list, it will be evaled in the same buffer.
345
346 If this form or function returns a string, this string will be used as
347 a possible file name; and if it returns a non-nil list, that list will
348 be used as possible file names.")
349
350 (defvar gnus-move-split-methods nil
351   "*Variable used to suggest where articles are to be moved to.
352 It uses the same syntax as the `gnus-split-methods' variable.")
353
354 (defvar gnus-save-score nil
355   "*If non-nil, save group scoring info.")
356
357 (defvar gnus-use-adaptive-scoring nil
358   "*If non-nil, use some adaptive scoring scheme.")
359
360 (defvar gnus-use-cache nil
361   "*If nil, Gnus will ignore the article cache.
362 If `passive', it will allow entering (and reading) articles
363 explicitly entered into the cache.  If anything else, use the
364 cache to the full extent of the law.")
365
366 (defvar gnus-use-trees nil
367   "*If non-nil, display a thread tree buffer.")
368
369 (defvar gnus-keep-backlog nil
370   "*If non-nil, Gnus will keep read articles for later re-retrieval.
371 If it is a number N, then Gnus will only keep the last N articles
372 read.  If it is neither nil nor a number, Gnus will keep all read
373 articles.  This is not a good idea.")
374
375 (defvar gnus-use-nocem nil
376   "*If non-nil, Gnus will read NoCeM cancel messages.")
377
378 (defvar gnus-use-demon nil
379   "If non-nil, Gnus might use some demons.")
380
381 (defvar gnus-use-scoring t
382   "*If non-nil, enable scoring.")
383
384 (defvar gnus-use-picons nil
385   "*If non-nil, display picons.")
386
387 (defvar gnus-fetch-old-headers nil
388   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
389 If an unread article in the group refers to an older, already read (or
390 just marked as read) article, the old article will not normally be
391 displayed in the Summary buffer.  If this variable is non-nil, Gnus
392 will attempt to grab the headers to the old articles, and thereby
393 build complete threads.  If it has the value `some', only enough
394 headers to connect otherwise loose threads will be displayed.
395 This variable can also be a number.  In that case, no more than that
396 number of old headers will be fetched.
397
398 The server has to support NOV for any of this to work.")
399
400 ;see gnus-cus.el
401 ;(defvar gnus-visual t
402 ;  "*If non-nil, will do various highlighting.
403 ;If nil, no mouse highlights (or any other highlights) will be
404 ;performed.  This might speed up Gnus some when generating large group
405 ;and summary buffers.")
406
407 (defvar gnus-novice-user t
408   "*Non-nil means that you are a usenet novice.
409 If non-nil, verbose messages may be displayed and confirmations may be
410 required.")
411
412 (defvar gnus-expert-user nil
413   "*Non-nil means that you will never be asked for confirmation about anything.
414 And that means *anything*.")
415
416 (defvar gnus-verbose 7
417   "*Integer that says how verbose Gnus should be.
418 The higher the number, the more messages Gnus will flash to say what
419 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
420 display most important messages; and at ten, Gnus will keep on
421 jabbering all the time.")
422
423 (defvar gnus-keep-same-level nil
424   "*Non-nil means that the next newsgroup after the current will be on the same level.
425 When you type, for instance, `n' after reading the last article in the
426 current newsgroup, you will go to the next newsgroup.  If this variable
427 is nil, the next newsgroup will be the next from the group
428 buffer.
429 If this variable is non-nil, Gnus will either put you in the
430 next newsgroup with the same level, or, if no such newsgroup is
431 available, the next newsgroup with the lowest possible level higher
432 than the current level.
433 If this variable is `best', Gnus will make the next newsgroup the one
434 with the best level.")
435
436 (defvar gnus-summary-make-false-root 'adopt
437   "*nil means that Gnus won't gather loose threads.
438 If the root of a thread has expired or been read in a previous
439 session, the information necessary to build a complete thread has been
440 lost.  Instead of having many small sub-threads from this original thread
441 scattered all over the summary buffer, Gnus can gather them.
442
443 If non-nil, Gnus will try to gather all loose sub-threads from an
444 original thread into one large thread.
445
446 If this variable is non-nil, it should be one of `none', `adopt',
447 `dummy' or `empty'.
448
449 If this variable is `none', Gnus will not make a false root, but just
450 present the sub-threads after another.
451 If this variable is `dummy', Gnus will create a dummy root that will
452 have all the sub-threads as children.
453 If this variable is `adopt', Gnus will make one of the \"children\"
454 the parent and mark all the step-children as such.
455 If this variable is `empty', the \"children\" are printed with empty
456 subject fields.  (Or rather, they will be printed with a string
457 given by the `gnus-summary-same-subject' variable.)")
458
459 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
460   "*A regexp to match subjects to be excluded from loose thread gathering.
461 As loose thread gathering is done on subjects only, that means that
462 there can be many false gatherings performed.  By rooting out certain
463 common subjects, gathering might become saner.")
464
465 (defvar gnus-summary-gather-subject-limit nil
466   "*Maximum length of subject comparisons when gathering loose threads.
467 Use nil to compare full subjects.  Setting this variable to a low
468 number will help gather threads that have been corrupted by
469 newsreaders chopping off subject lines, but it might also mean that
470 unrelated articles that have subject that happen to begin with the
471 same few characters will be incorrectly gathered.
472
473 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
474 comparing subjects.")
475
476 (defvar gnus-simplify-ignored-prefixes nil
477   "*Regexp, matches for which are removed from subject lines when simplifying.")
478
479 (defvar gnus-build-sparse-threads nil
480   "*If non-nil, fill in the gaps in threads.
481 If `some', only fill in the gaps that are needed to tie loose threads
482 together.  If `more', fill in all leaf nodes that Gnus can find.  If
483 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
484
485 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
486   "Function used for gathering loose threads.
487 There are two pre-defined functions: `gnus-gather-threads-by-subject',
488 which only takes Subjects into consideration; and
489 `gnus-gather-threads-by-references', which compared the References
490 headers of the articles to find matches.")
491
492 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
493 (defvar gnus-summary-same-subject ""
494   "*String indicating that the current article has the same subject as the previous.
495 This variable will only be used if the value of
496 `gnus-summary-make-false-root' is `empty'.")
497
498 (defvar gnus-summary-goto-unread t
499   "*If non-nil, marking commands will go to the next unread article.
500 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
501 whether it is read or not.")
502
503 (defvar gnus-group-goto-unread t
504   "*If non-nil, movement commands will go to the next unread and subscribed group.")
505
506 (defvar gnus-goto-next-group-when-activating t
507   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
508
509 (defvar gnus-check-new-newsgroups t
510   "*Non-nil means that Gnus will add new newsgroups at startup.
511 If this variable is `ask-server', Gnus will ask the server for new
512 groups since the last time it checked.  This means that the killed list
513 is no longer necessary, so you could set `gnus-save-killed-list' to
514 nil.
515
516 A variant is to have this variable be a list of select methods.  Gnus
517 will then use the `ask-server' method on all these select methods to
518 query for new groups from all those servers.
519
520 Eg.
521   (setq gnus-check-new-newsgroups
522         '((nntp \"some.server\") (nntp \"other.server\")))
523
524 If this variable is nil, then you have to tell Gnus explicitly to
525 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
526
527 (defvar gnus-check-bogus-newsgroups nil
528   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
529 If this variable is nil, then you have to tell Gnus explicitly to
530 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
531
532 (defvar gnus-read-active-file t
533   "*Non-nil means that Gnus will read the entire active file at startup.
534 If this variable is nil, Gnus will only know about the groups in your
535 `.newsrc' file.
536
537 If this variable is `some', Gnus will try to only read the relevant
538 parts of the active file from the server.  Not all servers support
539 this, and it might be quite slow with other servers, but this should
540 generally be faster than both the t and nil value.
541
542 If you set this variable to nil or `some', you probably still want to
543 be told about new newsgroups that arrive.  To do that, set
544 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
545 properly with all servers.")
546
547 (defvar gnus-level-subscribed 5
548   "*Groups with levels less than or equal to this variable are subscribed.")
549
550 (defvar gnus-level-unsubscribed 7
551   "*Groups with levels less than or equal to this variable are unsubscribed.
552 Groups with levels less than `gnus-level-subscribed', which should be
553 less than this variable, are subscribed.")
554
555 (defvar gnus-level-zombie 8
556   "*Groups with this level are zombie groups.")
557
558 (defvar gnus-level-killed 9
559   "*Groups with this level are killed.")
560
561 (defvar gnus-level-default-subscribed 3
562   "*New subscribed groups will be subscribed at this level.")
563
564 (defvar gnus-level-default-unsubscribed 6
565   "*New unsubscribed groups will be unsubscribed at this level.")
566
567 (defvar gnus-activate-level (1+ gnus-level-subscribed)
568   "*Groups higher than this level won't be activated on startup.
569 Setting this variable to something log might save lots of time when
570 you have many groups that you aren't interested in.")
571
572 (defvar gnus-activate-foreign-newsgroups 4
573   "*If nil, Gnus will not check foreign newsgroups at startup.
574 If it is non-nil, it should be a number between one and nine.  Foreign
575 newsgroups that have a level lower or equal to this number will be
576 activated on startup.  For instance, if you want to active all
577 subscribed newsgroups, but not the rest, you'd set this variable to
578 `gnus-level-subscribed'.
579
580 If you subscribe to lots of newsgroups from different servers, startup
581 might take a while.  By setting this variable to nil, you'll save time,
582 but you won't be told how many unread articles there are in the
583 groups.")
584
585 (defvar gnus-save-newsrc-file t
586   "*Non-nil means that Gnus will save the `.newsrc' file.
587 Gnus always saves its own startup file, which is called
588 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
589 be readily understood by other newsreaders.  If you don't plan on
590 using other newsreaders, set this variable to nil to save some time on
591 exit.")
592
593 (defvar gnus-save-killed-list t
594   "*If non-nil, save the list of killed groups to the startup file.
595 If you set this variable to nil, you'll save both time (when starting
596 and quitting) and space (both memory and disk), but it will also mean
597 that Gnus has no record of which groups are new and which are old, so
598 the automatic new newsgroups subscription methods become meaningless.
599
600 You should always set `gnus-check-new-newsgroups' to `ask-server' or
601 nil if you set this variable to nil.")
602
603 (defvar gnus-interactive-catchup t
604   "*If non-nil, require your confirmation when catching up a group.")
605
606 (defvar gnus-interactive-post t
607   "*If non-nil, group name will be asked for when posting.")
608
609 (defvar gnus-interactive-exit t
610   "*If non-nil, require your confirmation when exiting Gnus.")
611
612 (defvar gnus-kill-killed t
613   "*If non-nil, Gnus will apply kill files to already killed articles.
614 If it is nil, Gnus will never apply kill files to articles that have
615 already been through the scoring process, which might very well save lots
616 of time.")
617
618 (defvar gnus-extract-address-components 'gnus-extract-address-components
619   "*Function for extracting address components from a From header.
620 Two pre-defined function exist: `gnus-extract-address-components',
621 which is the default, quite fast, and too simplistic solution, and
622 `mail-extract-address-components', which works much better, but is
623 slower.")
624
625 (defvar gnus-summary-default-score 0
626   "*Default article score level.
627 If this variable is nil, scoring will be disabled.")
628
629 (defvar gnus-summary-zcore-fuzz 0
630   "*Fuzziness factor for the zcore in the summary buffer.
631 Articles with scores closer than this to `gnus-summary-default-score'
632 will not be marked.")
633
634 (defvar gnus-simplify-subject-fuzzy-regexp nil
635   "*Strings to be removed when doing fuzzy matches.
636 This can either be a regular expression or list of regular expressions
637 that will be removed from subject strings if fuzzy subject
638 simplification is selected.")
639
640 (defvar gnus-permanently-visible-groups nil
641   "*Regexp to match groups that should always be listed in the group buffer.
642 This means that they will still be listed when there are no unread
643 articles in the groups.")
644
645 (defvar gnus-list-groups-with-ticked-articles t
646   "*If non-nil, list groups that have only ticked articles.
647 If nil, only list groups that have unread articles.")
648
649 (defvar gnus-group-default-list-level gnus-level-subscribed
650   "*Default listing level.
651 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
652
653 (defvar gnus-group-use-permanent-levels nil
654   "*If non-nil, once you set a level, Gnus will use this level.")
655
656 (defvar gnus-group-list-inactive-groups t
657   "*If non-nil, inactive groups will be listed.")
658
659 (defvar gnus-show-mime nil
660   "*If non-nil, do mime processing of articles.
661 The articles will simply be fed to the function given by
662 `gnus-show-mime-method'.")
663
664 (defvar gnus-strict-mime t
665   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
666
667 (defvar gnus-show-mime-method 'metamail-buffer
668   "*Function to process a MIME message.
669 The function is called from the article buffer.")
670
671 (defvar gnus-decode-encoded-word-method (lambda ())
672   "*Function to decode a MIME encoded-words.
673 The function is called from the article buffer.")
674
675 (defvar gnus-show-threads t
676   "*If non-nil, display threads in summary mode.")
677
678 (defvar gnus-thread-hide-subtree nil
679   "*If non-nil, hide all threads initially.
680 If threads are hidden, you have to run the command
681 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
682 to expose hidden threads.")
683
684 (defvar gnus-thread-hide-killed t
685   "*If non-nil, hide killed threads automatically.")
686
687 (defvar gnus-thread-ignore-subject nil
688   "*If non-nil, ignore subjects and do all threading based on the Reference header.
689 If nil, which is the default, articles that have different subjects
690 from their parents will start separate threads.")
691
692 (defvar gnus-thread-operation-ignore-subject t
693   "*If non-nil, subjects will be ignored when doing thread commands.
694 This affects commands like `gnus-summary-kill-thread' and
695 `gnus-summary-lower-thread'.
696
697 If this variable is nil, articles in the same thread with different
698 subjects will not be included in the operation in question.  If this
699 variable is `fuzzy', only articles that have subjects that are fuzzily
700 equal will be included.")
701
702 (defvar gnus-thread-indent-level 4
703   "*Number that says how much each sub-thread should be indented.")
704
705 (defvar gnus-ignored-newsgroups
706   (purecopy (mapconcat 'identity
707                        '("^to\\."       ; not "real" groups
708                          "^[0-9. \t]+ " ; all digits in name
709                          "[][\"#'()]"   ; bogus characters
710                          )
711                        "\\|"))
712   "*A regexp to match uninteresting newsgroups in the active file.
713 Any lines in the active file matching this regular expression are
714 removed from the newsgroup list before anything else is done to it,
715 thus making them effectively non-existent.")
716
717 (defvar gnus-ignored-headers
718   "^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:"
719   "*All headers that match this regexp will be hidden.
720 This variable can also be a list of regexps of headers to be ignored.
721 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
722
723 (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-"
724   "*All headers that do not match this regexp will be hidden.
725 This variable can also be a list of regexp of headers to remain visible.
726 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
727
728 (defvar gnus-sorted-header-list
729   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
730     "^Cc:" "^Date:" "^Organization:")
731   "*This variable is a list of regular expressions.
732 If it is non-nil, headers that match the regular expressions will
733 be placed first in the article buffer in the sequence specified by
734 this list.")
735
736 (defvar gnus-boring-article-headers
737   '(empty followup-to reply-to)
738   "*Headers that are only to be displayed if they have interesting data.
739 Possible values in this list are `empty', `newsgroups', `followup-to',
740 `reply-to', and `date'.")
741
742 (defvar gnus-show-all-headers nil
743   "*If non-nil, don't hide any headers.")
744
745 (defvar gnus-save-all-headers t
746   "*If non-nil, don't remove any headers before saving.")
747
748 (defvar gnus-saved-headers gnus-visible-headers
749   "*Headers to keep if `gnus-save-all-headers' is nil.
750 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
751 If that variable is nil, however, all headers that match this regexp
752 will be kept while the rest will be deleted before saving.")
753
754 (defvar gnus-inhibit-startup-message nil
755   "*If non-nil, the startup message will not be displayed.")
756
757 (defvar gnus-signature-separator "^-- *$"
758   "Regexp matching signature separator.")
759
760 (defvar gnus-auto-extend-newsgroup t
761   "*If non-nil, extend newsgroup forward and backward when requested.")
762
763 (defvar gnus-auto-select-first t
764   "*If nil, don't select the first unread article when entering a group.
765 If this variable is `best', select the highest-scored unread article
766 in the group.  If neither nil nor `best', select the first unread
767 article.
768
769 If you want to prevent automatic selection of the first unread article
770 in some newsgroups, set the variable to nil in
771 `gnus-select-group-hook'.")
772
773 (defvar gnus-auto-select-next t
774   "*If non-nil, offer to go to the next group from the end of the previous.
775 If the value is t and the next newsgroup is empty, Gnus will exit
776 summary mode and go back to group mode.  If the value is neither nil
777 nor t, Gnus will select the following unread newsgroup.  In
778 particular, if the value is the symbol `quietly', the next unread
779 newsgroup will be selected without any confirmation, and if it is
780 `almost-quietly', the next group will be selected without any
781 confirmation if you are located on the last article in the group.
782 Finally, if this variable is `slightly-quietly', the `Z n' command
783 will go to the next group without confirmation.")
784
785 (defvar gnus-auto-select-same nil
786   "*If non-nil, select the next article with the same subject.")
787
788 (defvar gnus-summary-check-current nil
789   "*If non-nil, consider the current article when moving.
790 The \"unread\" movement commands will stay on the same line if the
791 current article is unread.")
792
793 (defvar gnus-auto-center-summary t
794   "*If non-nil, always center the current summary buffer.")
795
796 (defvar gnus-break-pages t
797   "*If non-nil, do page breaking on articles.
798 The page delimiter is specified by the `gnus-page-delimiter'
799 variable.")
800
801 (defvar gnus-page-delimiter "^\^L"
802   "*Regexp describing what to use as article page delimiters.
803 The default value is \"^\^L\", which is a form linefeed at the
804 beginning of a line.")
805
806 (defvar gnus-use-full-window t
807   "*If non-nil, use the entire Emacs screen.")
808
809 (defvar gnus-window-configuration nil
810   "Obsolete variable.  See `gnus-buffer-configuration'.")
811
812 (defvar gnus-window-min-width 2
813   "*Minimum width of Gnus buffers.")
814
815 (defvar gnus-window-min-height 1
816   "*Minimum height of Gnus buffers.")
817
818 (defvar gnus-buffer-configuration
819   '((group
820      (vertical 1.0
821                (group 1.0 point)
822                (if gnus-carpal '(group-carpal 4))))
823     (summary
824      (vertical 1.0
825                (summary 1.0 point)
826                (if gnus-carpal '(summary-carpal 4))))
827     (article
828      (cond 
829       (gnus-use-picons
830        '(frame 1.0
831                (vertical 1.0
832                          (summary 0.25 point)
833                          (if gnus-carpal '(summary-carpal 4))
834                          (article 1.0))
835                (vertical ((height . 5) (width . 15)
836                           (user-position . t)
837                           (left . -1) (top . 1))
838                          (picons 1.0))))
839       (gnus-use-trees
840        '(vertical 1.0
841                   (summary 0.25 point)
842                   (tree 0.25)
843                   (article 1.0)))
844       (t
845        '(vertical 1.0
846                  (summary 0.25 point)
847                  (if gnus-carpal '(summary-carpal 4))
848                  (if gnus-use-trees '(tree 0.25))
849                  (article 1.0)))))
850     (server
851      (vertical 1.0
852                (server 1.0 point)
853                (if gnus-carpal '(server-carpal 2))))
854     (browse
855      (vertical 1.0
856                (browse 1.0 point)
857                (if gnus-carpal '(browse-carpal 2))))
858     (group-mail
859      (vertical 1.0
860                (mail 1.0 point)))
861     (summary-mail
862      (vertical 1.0
863                (mail 1.0 point)))
864     (summary-reply
865      (vertical 1.0
866                (article 0.5)
867                (mail 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article 0.5)
896                (mail 1.0 point)))
897     (mail-forward
898      (vertical 1.0
899                (mail 1.0 point)))
900     (post-forward
901      (vertical 1.0
902                (post 1.0 point)))
903     (reply-yank
904      (vertical 1.0
905                (mail 1.0 point)))
906     (mail-bounce
907      (vertical 1.0
908                (article 0.5)
909                (mail 1.0 point)))
910     (draft
911      (vertical 1.0
912                (draft 1.0 point)))
913     (pipe
914      (vertical 1.0
915                (summary 0.25 point)
916                (if gnus-carpal '(summary-carpal 4))
917                ("*Shell Command Output*" 1.0)))
918     (followup
919      (vertical 1.0
920                (article 0.5)
921                (post 1.0 point)))
922     (followup-yank
923      (vertical 1.0
924                (post 1.0 point))))
925   "Window configuration for all possible Gnus buffers.
926 This variable is a list of lists.  Each of these lists has a NAME and
927 a RULE.  The NAMEs are commonsense names like `group', which names a
928 rule used when displaying the group buffer; `summary', which names a
929 rule for what happens when you enter a group and do not display an
930 article buffer; and so on.  See the value of this variable for a
931 complete list of NAMEs.
932
933 Each RULE is a list of vectors.  The first element in this vector is
934 the name of the buffer to be displayed; the second element is the
935 percentage of the screen this buffer is to occupy (a number in the
936 0.0-0.99 range); the optional third element is `point', which should
937 be present to denote which buffer point is to go to after making this
938 buffer configuration.")
939
940 (defvar gnus-window-to-buffer
941   '((group . gnus-group-buffer)
942     (summary . gnus-summary-buffer)
943     (article . gnus-article-buffer)
944     (server . gnus-server-buffer)
945     (browse . "*Gnus Browse Server*")
946     (edit-group . gnus-group-edit-buffer)
947     (edit-server . gnus-server-edit-buffer)
948     (group-carpal . gnus-carpal-group-buffer)
949     (summary-carpal . gnus-carpal-summary-buffer)
950     (server-carpal . gnus-carpal-server-buffer)
951     (browse-carpal . gnus-carpal-browse-buffer)
952     (edit-score . gnus-score-edit-buffer)
953     (mail . gnus-mail-buffer)
954     (post . gnus-post-news-buffer)
955     (faq . gnus-faq-buffer)
956     (picons . "*Picons*")
957     (tree . gnus-tree-buffer)
958     (info . gnus-info-buffer)
959     (draft . gnus-draft-buffer))
960   "Mapping from short symbols to buffer names or buffer variables.")
961
962 (defvar gnus-carpal nil
963   "*If non-nil, display clickable icons.")
964
965 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
966   "*Function called with a group name when new group is detected.
967 A few pre-made functions are supplied: `gnus-subscribe-randomly'
968 inserts new groups at the beginning of the list of groups;
969 `gnus-subscribe-alphabetically' inserts new groups in strict
970 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
971 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
972 for your decision; `gnus-subscribe-killed' kills all new groups.")
973
974 ;; Suggested by a bug report by Hallvard B Furuseth.
975 ;; <h.b.furuseth@usit.uio.no>.
976 (defvar gnus-subscribe-options-newsgroup-method
977   (function gnus-subscribe-alphabetically)
978   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
979 If, for instance, you want to subscribe to all newsgroups in the
980 \"no\" and \"alt\" hierarchies, you'd put the following in your
981 .newsrc file:
982
983 options -n no.all alt.all
984
985 Gnus will the subscribe all new newsgroups in these hierarchies with
986 the subscription method in this variable.")
987
988 (defvar gnus-subscribe-hierarchical-interactive nil
989   "*If non-nil, Gnus will offer to subscribe hierarchically.
990 When a new hierarchy appears, Gnus will ask the user:
991
992 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
993
994 If the user pressed `d', Gnus will descend the hierarchy, `y' will
995 subscribe to all newsgroups in the hierarchy and `s' will skip this
996 hierarchy in its entirety.")
997
998 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
999   "*Function used for sorting the group buffer.
1000 This function will be called with group info entries as the arguments
1001 for the groups to be sorted.  Pre-made functions include
1002 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1003 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1004 `gnus-group-sort-by-rank'.
1005
1006 This variable can also be a list of sorting functions.  In that case,
1007 the most significant sort function should be the last function in the
1008 list.")
1009
1010 ;; Mark variables suggested by Thomas Michanek
1011 ;; <Thomas.Michanek@telelogic.se>.
1012 (defvar gnus-unread-mark ? 
1013   "*Mark used for unread articles.")
1014 (defvar gnus-ticked-mark ?!
1015   "*Mark used for ticked articles.")
1016 (defvar gnus-dormant-mark ??
1017   "*Mark used for dormant articles.")
1018 (defvar gnus-del-mark ?r
1019   "*Mark used for del'd articles.")
1020 (defvar gnus-read-mark ?R
1021   "*Mark used for read articles.")
1022 (defvar gnus-expirable-mark ?E
1023   "*Mark used for expirable articles.")
1024 (defvar gnus-killed-mark ?K
1025   "*Mark used for killed articles.")
1026 (defvar gnus-souped-mark ?F
1027   "*Mark used for killed articles.")
1028 (defvar gnus-kill-file-mark ?X
1029   "*Mark used for articles killed by kill files.")
1030 (defvar gnus-low-score-mark ?Y
1031   "*Mark used for articles with a low score.")
1032 (defvar gnus-catchup-mark ?C
1033   "*Mark used for articles that are caught up.")
1034 (defvar gnus-replied-mark ?A
1035   "*Mark used for articles that have been replied to.")
1036 (defvar gnus-cached-mark ?*
1037   "*Mark used for articles that are in the cache.")
1038 (defvar gnus-saved-mark ?S
1039   "*Mark used for articles that have been saved to.")
1040 (defvar gnus-process-mark ?#
1041   "*Process mark.")
1042 (defvar gnus-ancient-mark ?O
1043   "*Mark used for ancient articles.")
1044 (defvar gnus-sparse-mark ?Q
1045   "*Mark used for sparsely reffed articles.")
1046 (defvar gnus-canceled-mark ?G
1047   "*Mark used for canceled articles.")
1048 (defvar gnus-score-over-mark ?+
1049   "*Score mark used for articles with high scores.")
1050 (defvar gnus-score-below-mark ?-
1051   "*Score mark used for articles with low scores.")
1052 (defvar gnus-empty-thread-mark ? 
1053   "*There is no thread under the article.")
1054 (defvar gnus-not-empty-thread-mark ?=
1055   "*There is a thread under the article.")
1056
1057 (defvar gnus-view-pseudo-asynchronously nil
1058   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1059
1060 (defvar gnus-view-pseudos nil
1061   "*If `automatic', pseudo-articles will be viewed automatically.
1062 If `not-confirm', pseudos will be viewed automatically, and the user
1063 will not be asked to confirm the command.")
1064
1065 (defvar gnus-view-pseudos-separately t
1066   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1067 If nil, all files that use the same viewing command will be given as a
1068 list of parameters to that command.")
1069
1070 (defvar gnus-insert-pseudo-articles t
1071   "*If non-nil, insert pseudo-articles when decoding articles.")
1072
1073 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)\n"
1074   "*Format of group lines.
1075 It works along the same lines as a normal formatting string,
1076 with some simple extensions.
1077
1078 %M    Only marked articles (character, \"*\" or \" \")
1079 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1080 %L    Level of subscribedness (integer)
1081 %N    Number of unread articles (integer)
1082 %I    Number of dormant articles (integer)
1083 %i    Number of ticked and dormant (integer)
1084 %T    Number of ticked articles (integer)
1085 %R    Number of read articles (integer)
1086 %t    Total number of articles (integer)
1087 %y    Number of unread, unticked articles (integer)
1088 %G    Group name (string)
1089 %g    Qualified group name (string)
1090 %D    Group description (string)
1091 %s    Select method (string)
1092 %o    Moderated group (char, \"m\")
1093 %p    Process mark (char)
1094 %O    Moderated group (string, \"(m)\" or \"\")
1095 %P    Topic indentation (string)
1096 %n    Select from where (string)
1097 %z    A string that look like `<%s:%n>' if a foreign select method is used
1098 %u    User defined specifier.  The next character in the format string should
1099       be a letter.  Gnus will call the function gnus-user-format-function-X,
1100       where X is the letter following %u.  The function will be passed the
1101       current header as argument.  The function should return a string, which
1102       will be inserted into the buffer just like information from any other
1103       group specifier.
1104
1105 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1106 the mouse point move inside the area.  There can only be one such area.
1107
1108 Note that this format specification is not always respected.  For
1109 reasons of efficiency, when listing killed groups, this specification
1110 is ignored altogether.  If the spec is changed considerably, your
1111 output may end up looking strange when listing both alive and killed
1112 groups.
1113
1114 If you use %o or %O, reading the active file will be slower and quite
1115 a bit of extra memory will be used. %D will also worsen performance.
1116 Also note that if you change the format specification to include any
1117 of these specs, you must probably re-start Gnus to see them go into
1118 effect.")
1119
1120 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1121   "*The format specification of the lines in the summary buffer.
1122
1123 It works along the same lines as a normal formatting string,
1124 with some simple extensions.
1125
1126 %N   Article number, left padded with spaces (string)
1127 %S   Subject (string)
1128 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1129 %n   Name of the poster (string)
1130 %a   Extracted name of the poster (string)
1131 %A   Extracted address of the poster (string)
1132 %F   Contents of the From: header (string)
1133 %x   Contents of the Xref: header (string)
1134 %D   Date of the article (string)
1135 %d   Date of the article (string) in DD-MMM format
1136 %M   Message-id of the article (string)
1137 %r   References of the article (string)
1138 %c   Number of characters in the article (integer)
1139 %L   Number of lines in the article (integer)
1140 %I   Indentation based on thread level (a string of spaces)
1141 %T   A string with two possible values: 80 spaces if the article
1142      is on thread level two or larger and 0 spaces on level one
1143 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1144 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1145 %[   Opening bracket (character, \"[\" or \"<\")
1146 %]   Closing bracket (character, \"]\" or \">\")
1147 %>   Spaces of length thread-level (string)
1148 %<   Spaces of length (- 20 thread-level) (string)
1149 %i   Article score (number)
1150 %z   Article zcore (character)
1151 %t   Number of articles under the current thread (number).
1152 %e   Whether the thread is empty or not (character).
1153 %u   User defined specifier.  The next character in the format string should
1154      be a letter.  Gnus will call the function gnus-user-format-function-X,
1155      where X is the letter following %u.  The function will be passed the
1156      current header as argument.  The function should return a string, which
1157      will be inserted into the summary just like information from any other
1158      summary specifier.
1159
1160 Text between %( and %) will be highlighted with `gnus-mouse-face'
1161 when the mouse point is placed inside the area.  There can only be one
1162 such area.
1163
1164 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1165 with care.  For reasons of efficiency, Gnus will compute what column
1166 these characters will end up in, and \"hard-code\" that.  This means that
1167 it is illegal to have these specs after a variable-length spec.  Well,
1168 you might not be arrested, but your summary buffer will look strange,
1169 which is bad enough.
1170
1171 The smart choice is to have these specs as for to the left as
1172 possible.
1173
1174 This restriction may disappear in later versions of Gnus.")
1175
1176 (defvar gnus-summary-dummy-line-format
1177   "*  %(:                          :%) %S\n"
1178   "*The format specification for the dummy roots in the summary buffer.
1179 It works along the same lines as a normal formatting string,
1180 with some simple extensions.
1181
1182 %S  The subject")
1183
1184 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1185   "*The format specification for the summary mode line.
1186 It works along the same lines as a normal formatting string,
1187 with some simple extensions:
1188
1189 %G  Group name
1190 %p  Unprefixed group name
1191 %A  Current article number
1192 %V  Gnus version
1193 %U  Number of unread articles in the group
1194 %e  Number of unselected articles in the group
1195 %Z  A string with unread/unselected article counts
1196 %g  Shortish group name
1197 %S  Subject of the current article
1198 %u  User-defined spec
1199 %s  Current score file name
1200 %d  Number of dormant articles
1201 %r  Number of articles that have been marked as read in this session
1202 %E  Number of articles expunged by the score files")
1203
1204 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1205   "*The format specification for the article mode line.
1206 See `gnus-summary-mode-line-format' for a closer description.")
1207
1208 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1209   "*The format specification for the group mode line.
1210 It works along the same lines as a normal formatting string,
1211 with some simple extensions:
1212
1213 %S   The native news server.
1214 %M   The native select method.")
1215
1216 (defvar gnus-valid-select-methods
1217   '(("nntp" post address prompt-address)
1218     ("nnspool" post address)
1219     ("nnvirtual" post-mail virtual prompt-address)
1220     ("nnmbox" mail respool address)
1221     ("nnml" mail respool address)
1222     ("nnmh" mail respool address)
1223     ("nndir" post-mail prompt-address address)
1224     ("nneething" none address prompt-address)
1225     ("nndoc" none address prompt-address)
1226     ("nnbabyl" mail address respool)
1227     ("nnkiboze" post address virtual)
1228     ("nnsoup" post-mail address)
1229     ("nndraft" post-mail)
1230     ("nnfolder" mail respool address))
1231   "An alist of valid select methods.
1232 The first element of each list lists should be a string with the name
1233 of the select method.  The other elements may be be the category of
1234 this method (ie. `post', `mail', `none' or whatever) or other
1235 properties that this method has (like being respoolable).
1236 If you implement a new select method, all you should have to change is
1237 this variable.  I think.")
1238
1239 (defvar gnus-updated-mode-lines '(group article summary tree)
1240   "*List of buffers that should update their mode lines.
1241 The list may contain the symbols `group', `article' and `summary'.  If
1242 the corresponding symbol is present, Gnus will keep that mode line
1243 updated with information that may be pertinent.
1244 If this variable is nil, screen refresh may be quicker.")
1245
1246 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1247 (defvar gnus-mode-non-string-length nil
1248   "*Max length of mode-line non-string contents.
1249 If this is nil, Gnus will take space as is needed, leaving the rest
1250 of the modeline intact.")
1251
1252 ;see gnus-cus.el
1253 ;(defvar gnus-mouse-face 'highlight
1254 ;  "*Face used for mouse highlighting in Gnus.
1255 ;No mouse highlights will be done if `gnus-visual' is nil.")
1256
1257 (defvar gnus-summary-mark-below nil
1258   "*Mark all articles with a score below this variable as read.
1259 This variable is local to each summary buffer and usually set by the
1260 score file.")
1261
1262 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1263   "*List of functions used for sorting articles in the summary buffer.
1264 This variable is only used when not using a threaded display.")
1265
1266 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1267   "*List of functions used for sorting threads in the summary buffer.
1268 By default, threads are sorted by article number.
1269
1270 Each function takes two threads and return non-nil if the first thread
1271 should be sorted before the other.  If you use more than one function,
1272 the primary sort function should be the last.  You should probably
1273 always include `gnus-thread-sort-by-number' in the list of sorting
1274 functions -- preferably first.
1275
1276 Ready-mady functions include `gnus-thread-sort-by-number',
1277 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1278 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1279 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1280
1281 (defvar gnus-thread-score-function '+
1282   "*Function used for calculating the total score of a thread.
1283
1284 The function is called with the scores of the article and each
1285 subthread and should then return the score of the thread.
1286
1287 Some functions you can use are `+', `max', or `min'.")
1288
1289 (defvar gnus-summary-expunge-below nil
1290   "All articles that have a score less than this variable will be expunged.")
1291
1292 (defvar gnus-thread-expunge-below nil
1293   "All threads that have a total score less than this variable will be expunged.
1294 See `gnus-thread-score-function' for en explanation of what a
1295 \"thread score\" is.")
1296
1297 (defvar gnus-auto-subscribed-groups
1298   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1299   "*All new groups that match this regexp will be subscribed automatically.
1300 Note that this variable only deals with new groups.  It has no effect
1301 whatsoever on old groups.")
1302
1303 (defvar gnus-options-subscribe nil
1304   "*All new groups matching this regexp will be subscribed unconditionally.
1305 Note that this variable deals only with new newsgroups.  This variable
1306 does not affect old newsgroups.")
1307
1308 (defvar gnus-options-not-subscribe nil
1309   "*All new groups matching this regexp will be ignored.
1310 Note that this variable deals only with new newsgroups.  This variable
1311 does not affect old (already subscribed) newsgroups.")
1312
1313 (defvar gnus-auto-expirable-newsgroups nil
1314   "*Groups in which to automatically mark read articles as expirable.
1315 If non-nil, this should be a regexp that should match all groups in
1316 which to perform auto-expiry.  This only makes sense for mail groups.")
1317
1318 (defvar gnus-total-expirable-newsgroups nil
1319   "*Groups in which to perform expiry of all read articles.
1320 Use with extreme caution.  All groups that match this regexp will be
1321 expiring - which means that all read articles will be deleted after
1322 (say) one week.  (This only goes for mail groups and the like, of
1323 course.)")
1324
1325 (defvar gnus-group-uncollapsed-levels 1
1326   "Number of group name elements to leave alone when making a short group name.")
1327
1328 (defvar gnus-hidden-properties '(invisible t intangible t)
1329   "Property list to use for hiding text.")
1330
1331 (defvar gnus-modtime-botch nil
1332   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1333 due to the bogus appearance that .newsrc was modified on disc.")
1334
1335 ;; Hooks.
1336
1337 (defvar gnus-group-mode-hook nil
1338   "*A hook for Gnus group mode.")
1339
1340 (defvar gnus-summary-mode-hook nil
1341   "*A hook for Gnus summary mode.
1342 This hook is run before any variables are set in the summary buffer.")
1343
1344 (defvar gnus-article-mode-hook nil
1345   "*A hook for Gnus article mode.")
1346
1347 (defvar gnus-summary-prepare-exit-hook nil
1348   "*A hook called when preparing to exit from the summary buffer.
1349 It calls `gnus-summary-expire-articles' by default.")
1350 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1351
1352 (defvar gnus-summary-exit-hook nil
1353   "*A hook called on exit from the summary buffer.")
1354
1355 (defvar gnus-group-catchup-group-hook nil
1356   "*A hook run when catching up a group from the group buffer.")
1357
1358 (defvar gnus-open-server-hook nil
1359   "*A hook called just before opening connection to the news server.")
1360
1361 (defvar gnus-load-hook nil
1362   "*A hook run while Gnus is loaded.")
1363
1364 (defvar gnus-startup-hook nil
1365   "*A hook called at startup.
1366 This hook is called after Gnus is connected to the NNTP server.")
1367
1368 (defvar gnus-get-new-news-hook nil
1369   "*A hook run just before Gnus checks for new news.")
1370
1371 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1372   "*A function that is called to generate the group buffer.
1373 The function is called with three arguments: The first is a number;
1374 all group with a level less or equal to that number should be listed,
1375 if the second is non-nil, empty groups should also be displayed.  If
1376 the third is non-nil, it is a number.  No groups with a level lower
1377 than this number should be displayed.
1378
1379 The only current function implemented is `gnus-group-prepare-flat'.")
1380
1381 (defvar gnus-group-prepare-hook nil
1382   "*A hook called after the group buffer has been generated.
1383 If you want to modify the group buffer, you can use this hook.")
1384
1385 (defvar gnus-summary-prepare-hook nil
1386   "*A hook called after the summary buffer has been generated.
1387 If you want to modify the summary buffer, you can use this hook.")
1388
1389 (defvar gnus-summary-generate-hook nil
1390   "*A hook run just before generating the summary buffer.
1391 This hook is commonly used to customize threading variables and the
1392 like.")
1393
1394 (defvar gnus-article-prepare-hook nil
1395   "*A hook called after an article has been prepared in the article buffer.
1396 If you want to run a special decoding program like nkf, use this hook.")
1397
1398 ;(defvar gnus-article-display-hook nil
1399 ;  "*A hook called after the article is displayed in the article buffer.
1400 ;The hook is designed to change the contents of the article
1401 ;buffer.  Typical functions that this hook may contain are
1402 ;`gnus-article-hide-headers' (hide selected headers),
1403 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1404 ;`gnus-article-hide-signature' (hide signature) and
1405 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1406 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1407 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1408 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1409
1410 (defvar gnus-article-x-face-command
1411   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1412   "String or function to be executed to display an X-Face header.
1413 If it is a string, the command will be executed in a sub-shell
1414 asynchronously.  The compressed face will be piped to this command.")
1415
1416 (defvar gnus-article-x-face-too-ugly nil
1417   "Regexp matching posters whose face shouldn't be shown automatically.")
1418
1419 (defvar gnus-select-group-hook nil
1420   "*A hook called when a newsgroup is selected.
1421
1422 If you'd like to simplify subjects like the
1423 `gnus-summary-next-same-subject' command does, you can use the
1424 following hook:
1425
1426  (setq gnus-select-group-hook
1427       (list
1428         (lambda ()
1429           (mapcar (lambda (header)
1430                      (mail-header-set-subject
1431                       header
1432                       (gnus-simplify-subject
1433                        (mail-header-subject header) 're-only)))
1434                   gnus-newsgroup-headers))))")
1435
1436 (defvar gnus-select-article-hook nil
1437   "*A hook called when an article is selected.")
1438
1439 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1440   "*A hook called to apply kill files to a group.
1441 This hook is intended to apply a kill file to the selected newsgroup.
1442 The function `gnus-apply-kill-file' is called by default.
1443
1444 Since a general kill file is too heavy to use only for a few
1445 newsgroups, I recommend you to use a lighter hook function.  For
1446 example, if you'd like to apply a kill file to articles which contains
1447 a string `rmgroup' in subject in newsgroup `control', you can use the
1448 following hook:
1449
1450  (setq gnus-apply-kill-hook
1451       (list
1452         (lambda ()
1453           (cond ((string-match \"control\" gnus-newsgroup-name)
1454                  (gnus-kill \"Subject\" \"rmgroup\")
1455                  (gnus-expunge \"X\"))))))")
1456
1457 (defvar gnus-visual-mark-article-hook
1458   (list 'gnus-highlight-selected-summary)
1459   "*Hook run after selecting an article in the summary buffer.
1460 It is meant to be used for highlighting the article in some way.  It
1461 is not run if `gnus-visual' is nil.")
1462
1463 (defvar gnus-parse-headers-hook nil
1464   "*A hook called before parsing the headers.")
1465
1466 (defvar gnus-exit-group-hook nil
1467   "*A hook called when exiting (not quitting) summary mode.")
1468
1469 (defvar gnus-suspend-gnus-hook nil
1470   "*A hook called when suspending (not exiting) Gnus.")
1471
1472 (defvar gnus-exit-gnus-hook nil
1473   "*A hook called when exiting Gnus.")
1474
1475 (defvar gnus-after-exiting-gnus-hook nil
1476   "*A hook called after exiting Gnus.")
1477
1478 (defvar gnus-save-newsrc-hook nil
1479   "*A hook called before saving any of the newsrc files.")
1480
1481 (defvar gnus-save-quick-newsrc-hook nil
1482   "*A hook called just before saving the quick newsrc file.
1483 Can be used to turn version control on or off.")
1484
1485 (defvar gnus-save-standard-newsrc-hook nil
1486   "*A hook called just before saving the standard newsrc file.
1487 Can be used to turn version control on or off.")
1488
1489 (defvar gnus-summary-update-hook
1490   (list 'gnus-summary-highlight-line)
1491   "*A hook called when a summary line is changed.
1492 The hook will not be called if `gnus-visual' is nil.
1493
1494 The default function `gnus-summary-highlight-line' will
1495 highlight the line according to the `gnus-summary-highlight'
1496 variable.")
1497
1498 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1499   "*A hook called when a group line is changed.
1500 The hook will not be called if `gnus-visual' is nil.
1501
1502 The default function `gnus-group-highlight-line' will
1503 highlight the line according to the `gnus-group-highlight'
1504 variable.")
1505
1506 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1507   "*A hook called when an article is selected for the first time.
1508 The hook is intended to mark an article as read (or unread)
1509 automatically when it is selected.")
1510
1511 (defvar gnus-group-change-level-function nil
1512   "Function run when a group level is changed.
1513 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1514
1515 ;; Remove any hilit infestation.
1516 (add-hook 'gnus-startup-hook
1517           (lambda ()
1518             (remove-hook 'gnus-summary-prepare-hook
1519                          'hilit-rehighlight-buffer-quietly)
1520             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1521             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1522             (remove-hook 'gnus-article-prepare-hook
1523                          'hilit-rehighlight-buffer-quietly)))
1524
1525 \f
1526 ;; Internal variables
1527
1528 (defvar gnus-server-alist nil
1529   "List of available servers.")
1530
1531 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1532
1533 (defvar gnus-goto-missing-group-function nil)
1534
1535 (defvar gnus-override-subscribe-method nil)
1536
1537 (defvar gnus-group-goto-next-group-function nil
1538   "Function to override finding the next group after listing groups.")
1539
1540 (defconst gnus-article-mark-lists
1541   '((marked . tick) (replied . reply)
1542     (expirable . expire) (killed . killed)
1543     (bookmarks . bookmark) (dormant . dormant)
1544     (scored . score) (saved . save)
1545     (cached . cache)
1546     ))
1547
1548 ;; Avoid highlighting in kill files.
1549 (defvar gnus-summary-inhibit-highlight nil)
1550 (defvar gnus-newsgroup-selected-overlay nil)
1551
1552 (defvar gnus-inhibit-hiding nil)
1553 (defvar gnus-group-indentation "")
1554 (defvar gnus-inhibit-limiting nil)
1555
1556 (defvar gnus-article-mode-map nil)
1557 (defvar gnus-dribble-buffer nil)
1558 (defvar gnus-headers-retrieved-by nil)
1559 (defvar gnus-article-reply nil)
1560 (defvar gnus-override-method nil)
1561 (defvar gnus-article-check-size nil)
1562
1563 (defvar gnus-current-score-file nil)
1564 (defvar gnus-newsgroup-adaptive-score-file nil)
1565 (defvar gnus-scores-exclude-files nil)
1566
1567 (defvar gnus-opened-servers nil)
1568
1569 (defvar gnus-current-move-group nil)
1570
1571 (defvar gnus-newsgroup-dependencies nil)
1572 (defvar gnus-newsgroup-async nil)
1573 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1574
1575 (defvar gnus-newsgroup-adaptive nil)
1576
1577 (defvar gnus-summary-display-table nil)
1578 (defvar gnus-summary-display-article-function nil)
1579
1580 (defvar gnus-summary-highlight-line-function nil
1581   "Function called after highlighting a summary line.")
1582
1583 (defvar gnus-group-line-format-alist
1584   `((?M gnus-tmp-marked-mark ?c)
1585     (?S gnus-tmp-subscribed ?c)
1586     (?L gnus-tmp-level ?d)
1587     (?N (cond ((eq number t) "*" )
1588               ((numberp number) 
1589                (int-to-string
1590                 (+ number
1591                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1592                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1593               (t number)) ?s)
1594     (?R gnus-tmp-number-of-read ?s)
1595     (?t gnus-tmp-number-total ?d)
1596     (?y gnus-tmp-number-of-unread ?s)
1597     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1598     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1599     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1600            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1601     (?g gnus-tmp-group ?s)
1602     (?G gnus-tmp-qualified-group ?s)
1603     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1604     (?D gnus-tmp-newsgroup-description ?s)
1605     (?o gnus-tmp-moderated ?c)
1606     (?O gnus-tmp-moderated-string ?s)
1607     (?p gnus-tmp-process-marked ?c)
1608     (?s gnus-tmp-news-server ?s)
1609     (?n gnus-tmp-news-method ?s)
1610     (?P gnus-group-indentation ?s)
1611     (?z gnus-tmp-news-method-string ?s)
1612     (?u gnus-tmp-user-defined ?s)))
1613
1614 (defvar gnus-summary-line-format-alist
1615   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1616     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1617     (?s gnus-tmp-subject-or-nil ?s)
1618     (?n gnus-tmp-name ?s)
1619     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1620         ?s)
1621     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1622             gnus-tmp-from) ?s)
1623     (?F gnus-tmp-from ?s)
1624     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1625     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1626     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1627     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1628     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1629     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1630     (?L gnus-tmp-lines ?d)
1631     (?I gnus-tmp-indentation ?s)
1632     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1633     (?R gnus-tmp-replied ?c)
1634     (?\[ gnus-tmp-opening-bracket ?c)
1635     (?\] gnus-tmp-closing-bracket ?c)
1636     (?\> (make-string gnus-tmp-level ? ) ?s)
1637     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1638     (?i gnus-tmp-score ?d)
1639     (?z gnus-tmp-score-char ?c)
1640     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1641     (?U gnus-tmp-unread ?c)
1642     (?t (gnus-summary-number-of-articles-in-thread
1643          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1644         ?d)
1645     (?e (gnus-summary-number-of-articles-in-thread
1646          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1647         ?c)
1648     (?u gnus-tmp-user-defined ?s))
1649   "An alist of format specifications that can appear in summary lines,
1650 and what variables they correspond with, along with the type of the
1651 variable (string, integer, character, etc).")
1652
1653 (defvar gnus-summary-dummy-line-format-alist
1654   `((?S gnus-tmp-subject ?s)
1655     (?N gnus-tmp-number ?d)
1656     (?u gnus-tmp-user-defined ?s)))
1657
1658 (defvar gnus-summary-mode-line-format-alist
1659   `((?G gnus-tmp-group-name ?s)
1660     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1661     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1662     (?A gnus-tmp-article-number ?d)
1663     (?Z gnus-tmp-unread-and-unselected ?s)
1664     (?V gnus-version ?s)
1665     (?U gnus-tmp-unread ?d)
1666     (?S gnus-tmp-subject ?s)
1667     (?e gnus-tmp-unselected ?d)
1668     (?u gnus-tmp-user-defined ?s)
1669     (?d (length gnus-newsgroup-dormant) ?d)
1670     (?t (length gnus-newsgroup-marked) ?d)
1671     (?r (length gnus-newsgroup-reads) ?d)
1672     (?E gnus-newsgroup-expunged-tally ?d)
1673     (?s (gnus-current-score-file-nondirectory) ?s)))
1674
1675 (defvar gnus-article-mode-line-format-alist
1676   gnus-summary-mode-line-format-alist)
1677
1678 (defvar gnus-group-mode-line-format-alist
1679   `((?S gnus-tmp-news-server ?s)
1680     (?M gnus-tmp-news-method ?s)
1681     (?u gnus-tmp-user-defined ?s)))
1682
1683 (defvar gnus-have-read-active-file nil)
1684
1685 (defconst gnus-maintainer
1686   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1687   "The mail address of the Gnus maintainers.")
1688
1689 (defconst gnus-version "September Gnus v0.42"
1690   "Version number for this version of Gnus.")
1691
1692 (defvar gnus-info-nodes
1693   '((gnus-group-mode            "(gnus)The Group Buffer")
1694     (gnus-summary-mode          "(gnus)The Summary Buffer")
1695     (gnus-article-mode          "(gnus)The Article Buffer"))
1696   "Assoc list of major modes and related Info nodes.")
1697
1698 (defvar gnus-group-buffer "*Group*")
1699 (defvar gnus-summary-buffer "*Summary*")
1700 (defvar gnus-article-buffer "*Article*")
1701 (defvar gnus-server-buffer "*Server*")
1702
1703 (defvar gnus-work-buffer " *gnus work*")
1704
1705 (defvar gnus-original-article-buffer " *Original Article*")
1706 (defvar gnus-original-article nil)
1707
1708 (defvar gnus-buffer-list nil
1709   "Gnus buffers that should be killed on exit.")
1710
1711 (defvar gnus-slave nil
1712   "Whether this Gnus is a slave or not.")
1713
1714 (defvar gnus-variable-list
1715   '(gnus-newsrc-options gnus-newsrc-options-n
1716     gnus-newsrc-last-checked-date
1717     gnus-newsrc-alist gnus-server-alist
1718     gnus-killed-list gnus-zombie-list
1719     gnus-topic-topology gnus-topic-alist
1720     gnus-format-specs)
1721   "Gnus variables saved in the quick startup file.")
1722
1723 (defvar gnus-newsrc-options nil
1724   "Options line in the .newsrc file.")
1725
1726 (defvar gnus-newsrc-options-n nil
1727   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1728
1729 (defvar gnus-newsrc-last-checked-date nil
1730   "Date Gnus last asked server for new newsgroups.")
1731
1732 (defvar gnus-topic-topology nil
1733   "The complete topic hierarchy.")
1734
1735 (defvar gnus-topic-alist nil
1736   "The complete topic-group alist.")
1737
1738 (defvar gnus-newsrc-alist nil
1739   "Assoc list of read articles.
1740 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1741
1742 (defvar gnus-newsrc-hashtb nil
1743   "Hashtable of gnus-newsrc-alist.")
1744
1745 (defvar gnus-killed-list nil
1746   "List of killed newsgroups.")
1747
1748 (defvar gnus-killed-hashtb nil
1749   "Hash table equivalent of gnus-killed-list.")
1750
1751 (defvar gnus-zombie-list nil
1752   "List of almost dead newsgroups.")
1753
1754 (defvar gnus-description-hashtb nil
1755   "Descriptions of newsgroups.")
1756
1757 (defvar gnus-list-of-killed-groups nil
1758   "List of newsgroups that have recently been killed by the user.")
1759
1760 (defvar gnus-active-hashtb nil
1761   "Hashtable of active articles.")
1762
1763 (defvar gnus-moderated-list nil
1764   "List of moderated newsgroups.")
1765
1766 (defvar gnus-group-marked nil)
1767
1768 (defvar gnus-current-startup-file nil
1769   "Startup file for the current host.")
1770
1771 (defvar gnus-last-search-regexp nil
1772   "Default regexp for article search command.")
1773
1774 (defvar gnus-last-shell-command nil
1775   "Default shell command on article.")
1776
1777 (defvar gnus-current-select-method nil
1778   "The current method for selecting a newsgroup.")
1779
1780 (defvar gnus-group-list-mode nil)
1781
1782 (defvar gnus-article-internal-prepare-hook nil)
1783
1784 (defvar gnus-newsgroup-name nil)
1785 (defvar gnus-newsgroup-begin nil)
1786 (defvar gnus-newsgroup-end nil)
1787 (defvar gnus-newsgroup-last-rmail nil)
1788 (defvar gnus-newsgroup-last-mail nil)
1789 (defvar gnus-newsgroup-last-folder nil)
1790 (defvar gnus-newsgroup-last-file nil)
1791 (defvar gnus-newsgroup-auto-expire nil)
1792 (defvar gnus-newsgroup-active nil)
1793
1794 (defvar gnus-newsgroup-data nil)
1795 (defvar gnus-newsgroup-data-reverse nil)
1796 (defvar gnus-newsgroup-limit nil)
1797 (defvar gnus-newsgroup-limits nil)
1798
1799 (defvar gnus-newsgroup-unreads nil
1800   "List of unread articles in the current newsgroup.")
1801
1802 (defvar gnus-newsgroup-unselected nil
1803   "List of unselected unread articles in the current newsgroup.")
1804
1805 (defvar gnus-newsgroup-reads nil
1806   "Alist of read articles and article marks in the current newsgroup.")
1807
1808 (defvar gnus-newsgroup-expunged-tally nil)
1809
1810 (defvar gnus-newsgroup-marked nil
1811   "List of ticked articles in the current newsgroup (a subset of unread art).")
1812
1813 (defvar gnus-newsgroup-killed nil
1814   "List of ranges of articles that have been through the scoring process.")
1815
1816 (defvar gnus-newsgroup-cached nil
1817   "List of articles that come from the article cache.")
1818
1819 (defvar gnus-newsgroup-saved nil
1820   "List of articles that have been saved.")
1821
1822 (defvar gnus-newsgroup-kill-headers nil)
1823
1824 (defvar gnus-newsgroup-replied nil
1825   "List of articles that have been replied to in the current newsgroup.")
1826
1827 (defvar gnus-newsgroup-expirable nil
1828   "List of articles in the current newsgroup that can be expired.")
1829
1830 (defvar gnus-newsgroup-processable nil
1831   "List of articles in the current newsgroup that can be processed.")
1832
1833 (defvar gnus-newsgroup-bookmarks nil
1834   "List of articles in the current newsgroup that have bookmarks.")
1835
1836 (defvar gnus-newsgroup-dormant nil
1837   "List of dormant articles in the current newsgroup.")
1838
1839 (defvar gnus-newsgroup-scored nil
1840   "List of scored articles in the current newsgroup.")
1841
1842 (defvar gnus-newsgroup-headers nil
1843   "List of article headers in the current newsgroup.")
1844
1845 (defvar gnus-newsgroup-threads nil)
1846
1847 (defvar gnus-newsgroup-prepared nil
1848   "Whether the current group has been prepared properly.")
1849
1850 (defvar gnus-newsgroup-ancient nil
1851   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1852
1853 (defvar gnus-newsgroup-sparse nil)
1854
1855 (defvar gnus-current-article nil)
1856 (defvar gnus-article-current nil)
1857 (defvar gnus-current-headers nil)
1858 (defvar gnus-have-all-headers nil)
1859 (defvar gnus-last-article nil)
1860 (defvar gnus-newsgroup-history nil)
1861 (defvar gnus-current-kill-article nil)
1862
1863 ;; Save window configuration.
1864 (defvar gnus-prev-winconf nil)
1865
1866 (defvar gnus-summary-mark-positions nil)
1867 (defvar gnus-group-mark-positions nil)
1868
1869 (defvar gnus-reffed-article-number nil)
1870
1871 ;;; Let the byte-compiler know that we know about this variable.
1872 (defvar rmail-default-rmail-file)
1873
1874 (defvar gnus-cache-removable-articles nil)
1875
1876 (defvar gnus-dead-summary nil)
1877
1878 (defconst gnus-summary-local-variables
1879   '(gnus-newsgroup-name
1880     gnus-newsgroup-begin gnus-newsgroup-end
1881     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1882     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1883     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1884     gnus-newsgroup-unselected gnus-newsgroup-marked
1885     gnus-newsgroup-reads gnus-newsgroup-saved
1886     gnus-newsgroup-replied gnus-newsgroup-expirable
1887     gnus-newsgroup-processable gnus-newsgroup-killed
1888     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1889     gnus-newsgroup-headers gnus-newsgroup-threads
1890     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1891     gnus-current-article gnus-current-headers gnus-have-all-headers
1892     gnus-last-article gnus-article-internal-prepare-hook
1893     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1894     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1895     gnus-newsgroup-async 
1896     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1897     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1898     gnus-newsgroup-history gnus-newsgroup-ancient
1899     gnus-newsgroup-sparse
1900     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1901     gnus-newsgroup-adaptive-score-file
1902     (gnus-newsgroup-expunged-tally . 0)
1903     gnus-cache-removeable-articles gnus-newsgroup-cached
1904     gnus-newsgroup-data gnus-newsgroup-data-reverse
1905     gnus-newsgroup-limit gnus-newsgroup-limits)
1906   "Variables that are buffer-local to the summary buffers.")
1907
1908 (defconst gnus-bug-message
1909   "Sending a bug report to the Gnus Towers.
1910 ========================================
1911
1912 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1913 be sent to the Gnus Bug Exterminators.
1914
1915 At the bottom of the buffer you'll see lots of variable settings.
1916 Please do not delete those.  They will tell the Bug People what your
1917 environment is, so that it will be easier to locate the bugs.
1918
1919 If you have found a bug that makes Emacs go \"beep\", set
1920 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1921 and include the backtrace in your bug report.
1922
1923 Please describe the bug in annoying, painstaking detail.
1924
1925 Thank you for your help in stamping out bugs.
1926 ")
1927
1928 ;;; End of variables.
1929
1930 ;; Define some autoload functions Gnus might use.
1931 (eval-and-compile
1932
1933   ;; This little mapcar goes through the list below and marks the
1934   ;; symbols in question as autoloaded functions.
1935   (mapcar
1936    (lambda (package)
1937      (let ((interactive (nth 1 (memq ':interactive package))))
1938        (mapcar
1939         (lambda (function)
1940           (let (keymap)
1941             (when (consp function)
1942               (setq keymap (car (memq 'keymap function)))
1943               (setq function (car function)))
1944             (autoload function (car package) nil interactive keymap)))
1945         (if (eq (nth 1 package) ':interactive)
1946             (cdddr package)
1947           (cdr package)))))
1948    '(("metamail" metamail-buffer)
1949      ("info" Info-goto-node)
1950      ("hexl" hexl-hex-string-to-integer)
1951      ("pp" pp pp-to-string pp-eval-expression)
1952      ("mail-extr" mail-extract-address-components)
1953      ("nnmail" nnmail-split-fancy nnmail-article-group)
1954      ("nnvirtual" nnvirtual-catchup-group)
1955      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1956       timezone-make-sortable-date timezone-make-time-string)
1957      ("sendmail" mail-position-on-field mail-setup)
1958      ("rmailout" rmail-output)
1959      ("rnewspost" news-mail-other-window news-reply-yank-original
1960       news-caesar-buffer-body)
1961      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1962       rmail-show-message)
1963      ("gnus-soup" :interactive t
1964       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1965       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1966      ("nnsoup" nnsoup-pack-replies)
1967      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1968       gnus-Folder-save-name gnus-folder-save-name)
1969      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1970      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1971       gnus-server-make-menu-bar gnus-article-make-menu-bar
1972       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1973       gnus-summary-highlight-line gnus-carpal-setup-buffer
1974       gnus-group-highlight-line
1975       gnus-article-add-button gnus-insert-next-page-button
1976       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1977      ("gnus-vis" :interactive t
1978       gnus-article-push-button gnus-article-press-button
1979       gnus-article-highlight gnus-article-highlight-some
1980       gnus-article-highlight-headers gnus-article-highlight-signature
1981       gnus-article-add-buttons gnus-article-add-buttons-to-head
1982       gnus-article-next-button gnus-article-prev-button)
1983      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1984       gnus-demon-add-disconnection gnus-demon-add-handler
1985       gnus-demon-remove-handler)
1986      ("gnus-demon" :interactive t
1987       gnus-demon-init gnus-demon-cancel)
1988      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1989       gnus-tree-open gnus-tree-close)
1990      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
1991       gnus-nocem-unwanted-article-p)
1992      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
1993      ("gnus-srvr" gnus-browse-foreign-server)
1994      ("gnus-cite" :interactive t
1995       gnus-article-highlight-citation gnus-article-hide-citation-maybe
1996       gnus-article-hide-citation gnus-article-fill-cited-article)
1997      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
1998       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
1999       gnus-execute gnus-expunge)
2000      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2001       gnus-cache-possibly-remove-articles gnus-cache-request-article
2002       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2003       gnus-cache-enter-remove-article gnus-cached-article-p
2004       gnus-cache-open gnus-cache-close)
2005      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2006       gnus-cache-remove-article)
2007      ("gnus-score" :interactive t
2008       gnus-summary-increase-score gnus-summary-lower-score
2009       gnus-score-flush-cache gnus-score-close
2010       gnus-score-raise-same-subject-and-select
2011       gnus-score-raise-same-subject gnus-score-default
2012       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2013       gnus-score-lower-same-subject gnus-score-lower-thread
2014       gnus-possibly-score-headers)
2015      ("gnus-score"
2016       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2017       gnus-current-score-file-nondirectory gnus-score-adaptive
2018       gnus-score-find-trace gnus-score-file-name)
2019      ("gnus-edit" :interactive t gnus-score-customize)
2020      ("gnus-topic" :interactive t gnus-topic-mode)
2021      ("gnus-topic" gnus-topic-remove-group)
2022      ("gnus-salt" :interactive t gnus-pick-mode)
2023      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2024      ("gnus-uu" :interactive t
2025       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2026       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2027       gnus-uu-mark-by-regexp gnus-uu-mark-all
2028       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2029       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2030       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2031       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2032       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2033       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2034       gnus-uu-decode-binhex-view)
2035      ("gnus-msg" (gnus-summary-send-map keymap)
2036       gnus-mail-yank-original gnus-mail-send-and-exit
2037       gnus-sendmail-setup-mail gnus-article-mail
2038       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2039      ("gnus-msg" :interactive t
2040       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2041       gnus-summary-followup gnus-summary-followup-with-original
2042       gnus-summary-followup-and-reply
2043       gnus-summary-followup-and-reply-with-original
2044       gnus-summary-cancel-article gnus-summary-supersede-article
2045       gnus-post-news gnus-inews-news gnus-cancel-news
2046       gnus-summary-reply gnus-summary-reply-with-original
2047       gnus-summary-mail-forward gnus-summary-mail-other-window
2048       gnus-bug)
2049      ("gnus-picon" :interactive t gnus-article-display-picons
2050       gnus-group-display-picons gnus-picons-article-display-x-face)
2051      ("gnus-vm" gnus-vm-mail-setup)
2052      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2053       gnus-summary-save-article-vm gnus-yank-article))))
2054
2055 \f
2056
2057 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2058 ;; If you want the cursor to go somewhere else, set these two
2059 ;; functions in some startup hook to whatever you want.
2060 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2061 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2062
2063 ;;; Various macros and substs.
2064
2065 (defun gnus-header-from (header)
2066   (mail-header-from header))
2067
2068 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2069   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2070   `(let ((GnusStartBufferWindow (selected-window)))
2071      (unwind-protect
2072          (progn
2073            (pop-to-buffer ,buffer)
2074            ,@forms)
2075        (select-window GnusStartBufferWindow))))
2076
2077 (defmacro gnus-gethash (string hashtable)
2078   "Get hash value of STRING in HASHTABLE."
2079   `(symbol-value (intern-soft ,string ,hashtable)))
2080
2081 (defmacro gnus-sethash (string value hashtable)
2082   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2083   `(set (intern ,string ,hashtable) ,value))
2084
2085 (defmacro gnus-intern-safe (string hashtable)
2086   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2087   `(let ((symbol (intern ,string ,hashtable)))
2088      (or (boundp symbol)
2089          (set symbol nil))
2090      symbol))
2091
2092 (defmacro gnus-group-unread (group)
2093   "Get the currently computed number of unread articles in GROUP."
2094   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2095
2096 (defmacro gnus-group-entry (group)
2097   "Get the newsrc entry for GROUP."
2098   `(gnus-gethash ,group gnus-newsrc-hashtb))
2099
2100 (defmacro gnus-active (group)
2101   "Get active info on GROUP."
2102   `(gnus-gethash ,group gnus-active-hashtb))
2103
2104 (defmacro gnus-set-active (group active)
2105   "Set GROUP's active info."
2106   `(gnus-sethash ,group ,active gnus-active-hashtb))
2107
2108 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2109 ;;   function `substring' might cut on a middle of multi-octet
2110 ;;   character.
2111 (defun gnus-truncate-string (str width)
2112   (substring str 0 width))
2113
2114 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2115 ;; to limit the length of a string.  This function is necessary since
2116 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2117 (defsubst gnus-limit-string (str width)
2118   (if (> (length str) width)
2119       (substring str 0 width)
2120     str))
2121
2122 (defsubst gnus-simplify-subject-re (subject)
2123   "Remove \"Re:\" from subject lines."
2124   (if (string-match "^[Rr][Ee]: *" subject)
2125       (substring subject (match-end 0))
2126     subject))
2127
2128 (defsubst gnus-goto-char (point)
2129   (and point (goto-char point)))
2130
2131 (defmacro gnus-buffer-exists-p (buffer)
2132   `(and ,buffer
2133         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2134                  ,buffer)))
2135
2136 (defmacro gnus-kill-buffer (buffer)
2137   `(let ((buf ,buffer))
2138      (if (gnus-buffer-exists-p buf)
2139          (kill-buffer buf))))
2140
2141 (defsubst gnus-point-at-bol ()
2142   "Return point at the beginning of the line."
2143   (let ((p (point)))
2144     (beginning-of-line)
2145     (prog1
2146         (point)
2147       (goto-char p))))
2148
2149 (defsubst gnus-point-at-eol ()
2150   "Return point at the end of the line."
2151   (let ((p (point)))
2152     (end-of-line)
2153     (prog1
2154         (point)
2155       (goto-char p))))
2156
2157 ;; Delete the current line (and the next N lines.);
2158 (defmacro gnus-delete-line (&optional n)
2159   `(delete-region (progn (beginning-of-line) (point))
2160                   (progn (forward-line ,(or n 1)) (point))))
2161
2162 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2163 (defvar gnus-init-inhibit nil)
2164 (defun gnus-read-init-file (&optional inhibit-next)
2165   (if gnus-init-inhibit
2166       (setq gnus-init-inhibit nil)
2167     (setq gnus-init-inhibit inhibit-next)
2168     (and gnus-init-file
2169          (or (and (file-exists-p gnus-init-file)
2170                   ;; Don't try to load a directory.
2171                   (not (file-directory-p gnus-init-file)))
2172              (file-exists-p (concat gnus-init-file ".el"))
2173              (file-exists-p (concat gnus-init-file ".elc")))
2174          (condition-case var
2175              (load gnus-init-file nil t)
2176            (error
2177             (error "Error in %s: %s" gnus-init-file var))))))
2178
2179 ;; Info access macros.
2180
2181 (defmacro gnus-info-group (info)
2182   `(nth 0 ,info))
2183 (defmacro gnus-info-rank (info)
2184   `(nth 1 ,info))
2185 (defmacro gnus-info-read (info)
2186   `(nth 2 ,info))
2187 (defmacro gnus-info-marks (info)
2188   `(nth 3 ,info))
2189 (defmacro gnus-info-method (info)
2190   `(nth 4 ,info))
2191 (defmacro gnus-info-params (info)
2192   `(nth 5 ,info))
2193
2194 (defmacro gnus-info-level (info)
2195   `(let ((rank (gnus-info-rank ,info)))
2196      (if (consp rank)
2197          (car rank)
2198        rank)))
2199 (defmacro gnus-info-score (info)
2200   `(let ((rank (gnus-info-rank ,info)))
2201      (or (and (consp rank) (cdr rank)) 0)))
2202
2203 (defmacro gnus-info-set-group (info group)
2204   `(setcar ,info ,group))
2205 (defmacro gnus-info-set-rank (info rank)
2206   `(setcar (nthcdr 1 ,info) ,rank))
2207 (defmacro gnus-info-set-read (info read)
2208   `(setcar (nthcdr 2 ,info) ,read))
2209 (defmacro gnus-info-set-marks (info marks)
2210   `(setcar (nthcdr 3 ,info) ,marks))
2211 (defmacro gnus-info-set-method (info method)
2212   `(setcar (nthcdr 4 ,info) ,method))
2213 (defmacro gnus-info-set-params (info params)
2214   `(setcar (nthcdr 5 ,info) ,params))
2215
2216 (defmacro gnus-info-set-level (info level)
2217   `(let ((rank (cdr ,info)))
2218      (if (consp (car rank))
2219          (setcar (car rank) ,level)
2220        (setcar rank ,level))))
2221 (defmacro gnus-info-set-score (info score)
2222   `(let ((rank (cdr ,info)))
2223      (if (consp (car rank))
2224          (setcdr (car rank) ,score)
2225        (setcar rank (cons (car rank) ,score)))))
2226
2227 (defmacro gnus-get-info (group)
2228   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2229
2230 (defun gnus-byte-code (func)
2231   "Return a form that can be `eval'ed based on FUNC."
2232   (let ((fval (symbol-function func)))
2233     (if (byte-code-function-p fval)
2234         (let ((flist (append fval nil)))
2235           (setcar flist 'byte-code)
2236           flist)
2237       (cons 'progn (cddr fval)))))
2238
2239 ;;; Load the compatability functions.
2240
2241 (require 'gnus-cus)
2242 (require 'gnus-ems)
2243
2244 \f
2245
2246 ;; Format specs.  The chunks below are the machine-generated forms
2247 ;; that are to be evaled as the result of the default format strings.
2248 ;; We write them in here to get them byte-compiled.  That way the
2249 ;; default actions will be quite fast, while still retaining the full
2250 ;; flexibility of the user-defined format specs.
2251
2252 ;; First we have lots of dummy defvars to let the compiler know these
2253 ;; are really dynamic variables.
2254
2255 (defvar gnus-tmp-unread)
2256 (defvar gnus-tmp-replied)
2257 (defvar gnus-tmp-score-char)
2258 (defvar gnus-tmp-indentation)
2259 (defvar gnus-tmp-opening-bracket)
2260 (defvar gnus-tmp-lines)
2261 (defvar gnus-tmp-name)
2262 (defvar gnus-tmp-closing-bracket)
2263 (defvar gnus-tmp-subject-or-nil)
2264 (defvar gnus-tmp-subject)
2265 (defvar gnus-tmp-marked)
2266 (defvar gnus-tmp-marked-mark)
2267 (defvar gnus-tmp-subscribed)
2268 (defvar gnus-tmp-process-marked)
2269 (defvar gnus-tmp-number-of-unread)
2270 (defvar gnus-tmp-group-name)
2271 (defvar gnus-tmp-group)
2272 (defvar gnus-tmp-article-number)
2273 (defvar gnus-tmp-unread-and-unselected)
2274 (defvar gnus-tmp-news-method)
2275 (defvar gnus-tmp-news-server)
2276 (defvar gnus-tmp-article-number)
2277 (defvar gnus-mouse-face)
2278 (defvar gnus-mouse-face-prop)
2279
2280 (defun gnus-summary-line-format-spec ()
2281   (insert gnus-tmp-unread gnus-tmp-replied
2282           gnus-tmp-score-char gnus-tmp-indentation)
2283   (put-text-property
2284    (point)
2285    (progn
2286      (insert
2287       gnus-tmp-opening-bracket
2288       (format "%4d: %-20s"
2289               gnus-tmp-lines
2290               (if (> (length gnus-tmp-name) 20)
2291                   (substring gnus-tmp-name 0 20)
2292                 gnus-tmp-name))
2293       gnus-tmp-closing-bracket)
2294      (point))
2295    gnus-mouse-face-prop gnus-mouse-face)
2296   (insert " " gnus-tmp-subject-or-nil "\n"))
2297
2298 (defvar gnus-summary-line-format-spec
2299   (gnus-byte-code 'gnus-summary-line-format-spec))
2300
2301 (defun gnus-summary-dummy-line-format-spec ()
2302   (insert "*  ")
2303   (put-text-property
2304    (point)
2305    (progn
2306      (insert ":                          :")
2307      (point))
2308    gnus-mouse-face-prop gnus-mouse-face)
2309   (insert " " gnus-tmp-subject "\n"))
2310
2311 (defvar gnus-summary-dummy-line-format-spec
2312   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2313
2314 (defun gnus-group-line-format-spec ()
2315   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2316           gnus-tmp-process-marked
2317           gnus-group-indentation
2318           (format "%5s: " gnus-tmp-number-of-unread))
2319   (put-text-property
2320    (point)
2321    (progn
2322      (insert gnus-tmp-group "\n")
2323      (1- (point)))
2324    gnus-mouse-face-prop gnus-mouse-face))
2325 (defvar gnus-group-line-format-spec
2326   (gnus-byte-code 'gnus-group-line-format-spec))
2327
2328 (defvar gnus-format-specs
2329   `((version . ,emacs-version)
2330     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2331     (summary-dummy ,gnus-summary-dummy-line-format
2332                    ,gnus-summary-dummy-line-format-spec)
2333     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2334
2335 (defvar gnus-article-mode-line-format-spec nil)
2336 (defvar gnus-summary-mode-line-format-spec nil)
2337 (defvar gnus-group-mode-line-format-spec nil)
2338
2339 ;;; Phew.  All that gruft is over, fortunately.
2340
2341 \f
2342 ;;;
2343 ;;; Gnus Utility Functions
2344 ;;;
2345
2346 (defun gnus-extract-address-components (from)
2347   (let (name address)
2348     ;; First find the address - the thing with the @ in it.  This may
2349     ;; not be accurate in mail addresses, but does the trick most of
2350     ;; the time in news messages.
2351     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2352         (setq address (substring from (match-beginning 0) (match-end 0))))
2353     ;; Then we check whether the "name <address>" format is used.
2354     (and address
2355          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2356          ;; Linear white space is not required.
2357          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2358          (and (setq name (substring from 0 (match-beginning 0)))
2359               ;; Strip any quotes from the name.
2360               (string-match "\".*\"" name)
2361               (setq name (substring name 1 (1- (match-end 0))))))
2362     ;; If not, then "address (name)" is used.
2363     (or name
2364         (and (string-match "(.+)" from)
2365              (setq name (substring from (1+ (match-beginning 0))
2366                                    (1- (match-end 0)))))
2367         (and (string-match "()" from)
2368              (setq name address))
2369         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2370         ;; XOVER might not support folded From headers.
2371         (and (string-match "(.*" from)
2372              (setq name (substring from (1+ (match-beginning 0))
2373                                    (match-end 0)))))
2374     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2375     (list (or name from) (or address from))))
2376
2377 (defun gnus-fetch-field (field)
2378   "Return the value of the header FIELD of current article."
2379   (save-excursion
2380     (save-restriction
2381       (let ((case-fold-search t))
2382         (nnheader-narrow-to-headers)
2383         (mail-fetch-field field)))))
2384
2385 (defun gnus-goto-colon ()
2386   (beginning-of-line)
2387   (search-forward ":" (gnus-point-at-eol) t))
2388
2389 ;;;###autoload
2390 (defun gnus-update-format (var)
2391   "Update the format specification near point."
2392   (interactive
2393    (list
2394     (save-excursion
2395       (eval-defun nil)
2396       ;; Find the end of the current word.
2397       (re-search-forward "[ \t\n]" nil t)
2398       ;; Search backward.
2399       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2400         (match-string 1)))))
2401   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2402                               (match-string 1 var))))
2403          (entry (assq type gnus-format-specs))
2404          value spec)
2405     (when entry
2406       (setq gnus-format-specs (delq entry gnus-format-specs)))
2407     (set
2408      (intern (format "%s-spec" var))
2409      (gnus-parse-format (setq value (symbol-value (intern var)))
2410                         (symbol-value (intern (format "%s-alist" var)))
2411                         (not (string-match "mode" var))))
2412     (setq spec (symbol-value (intern (format "%s-spec" var))))
2413     (push (list type value spec) gnus-format-specs)
2414
2415     (pop-to-buffer "*Gnus Format*")
2416     (erase-buffer)
2417     (lisp-interaction-mode)
2418     (insert (pp-to-string spec))))
2419
2420 (defun gnus-update-format-specifications (&optional force)
2421   "Update all (necessary) format specifications."
2422   ;; Make the indentation array.
2423   (gnus-make-thread-indent-array)
2424
2425   ;; See whether all the stored info needs to be flushed.
2426   (when (or force
2427             (not (equal emacs-version
2428                         (cdr (assq 'version gnus-format-specs)))))
2429     (setq gnus-format-specs nil))
2430
2431   ;; Go through all the formats and see whether they need updating.
2432   (let ((types '(summary summary-dummy group
2433                          summary-mode group-mode article-mode))
2434         new-format entry type val)
2435     (while (setq type (pop types))
2436       (setq new-format (symbol-value
2437                         (intern (format "gnus-%s-line-format" type))))
2438       (setq entry (cdr (assq type gnus-format-specs)))
2439       (if (and entry
2440                (equal (car entry) new-format))
2441           ;; Use the old format.
2442           (set (intern (format "gnus-%s-line-format-spec" type))
2443                (cadr entry))
2444         ;; This is a new format.
2445         (setq val
2446               (if (not (stringp new-format))
2447                   ;; This is a function call or something.
2448                   new-format
2449                 ;; This is a "real" format.
2450                 (gnus-parse-format
2451                  new-format
2452                  (symbol-value
2453                   (intern (format "gnus-%s-line-format-alist"
2454                                   (if (eq type 'article-mode)
2455                                       'summary-mode type))))
2456                  (not (string-match "mode$" (symbol-name type))))))
2457         ;; Enter the new format spec into the list.
2458         (if entry
2459             (progn
2460               (setcar (cdr entry) val)
2461               (setcar entry new-format))
2462           (push (list type new-format val) gnus-format-specs))
2463         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2464
2465   (gnus-update-group-mark-positions)
2466   (gnus-update-summary-mark-positions)
2467
2468   ;; See whether we need to read the description file.
2469   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2470            (not gnus-description-hashtb)
2471            gnus-read-active-file)
2472       (gnus-read-all-descriptions-files)))
2473
2474 (defun gnus-update-summary-mark-positions ()
2475   "Compute where the summary marks are to go."
2476   (save-excursion
2477     (let ((gnus-replied-mark 129)
2478           (gnus-score-below-mark 130)
2479           (gnus-score-over-mark 130)
2480           (thread nil)
2481           (gnus-visual nil)
2482           pos)
2483       (gnus-set-work-buffer)
2484       (gnus-summary-insert-line
2485        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2486       (goto-char (point-min))
2487       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2488                                          (- (point) 2)))))
2489       (goto-char (point-min))
2490       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2491             pos)
2492       (goto-char (point-min))
2493       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2494             pos)
2495       (setq gnus-summary-mark-positions pos))))
2496
2497 (defun gnus-update-group-mark-positions ()
2498   (save-excursion
2499     (let ((gnus-process-mark 128)
2500           (gnus-group-marked '("dummy.group")))
2501       (gnus-set-active "dummy.group" '(0 . 0))
2502       (gnus-set-work-buffer)
2503       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2504       (goto-char (point-min))
2505       (setq gnus-group-mark-positions
2506             (list (cons 'process (and (search-forward "\200" nil t)
2507                                       (- (point) 2))))))))
2508
2509 (defvar gnus-mouse-face-0 'highlight)
2510 (defvar gnus-mouse-face-1 'highlight)
2511 (defvar gnus-mouse-face-2 'highlight)
2512 (defvar gnus-mouse-face-3 'highlight)
2513 (defvar gnus-mouse-face-4 'highlight)
2514
2515 (defun gnus-mouse-face-function (form type)
2516   `(put-text-property
2517     (point) (progn ,@form (point))
2518     gnus-mouse-face-prop
2519     ,(if (equal type 0)
2520          'gnus-mouse-face
2521        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2522
2523 (defvar gnus-face-0 'bold)
2524 (defvar gnus-face-1 'italic)
2525 (defvar gnus-face-2 'bold-italic)
2526 (defvar gnus-face-3 'bold)
2527 (defvar gnus-face-4 'bold)
2528
2529 (defun gnus-face-face-function (form type)
2530   `(put-text-property
2531     (point) (progn ,@form (point))
2532     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2533
2534 (defun gnus-max-width-function (el max-width)
2535   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2536   (if (symbolp el)
2537       `(if (> (length ,el) ,max-width)
2538            (substring ,el 0 ,max-width)
2539          ,el)
2540     `(let ((val (eval ,el)))
2541        (if (numberp val)
2542            (setq val (int-to-string val)))
2543        (if (> (length val) ,max-width)
2544            (substring val 0 ,max-width)
2545          val))))
2546
2547 (defun gnus-parse-format (format spec-alist &optional insert)
2548   ;; This function parses the FORMAT string with the help of the
2549   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2550   ;; string.  If the FORMAT string contains the specifiers %( and %)
2551   ;; the text between them will have the mouse-face text property.
2552   (if (string-match
2553        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2554        format)
2555       (gnus-parse-complex-format format spec-alist)
2556     ;; This is a simple format.
2557     (gnus-parse-simple-format format spec-alist insert)))
2558
2559 (defun gnus-parse-complex-format (format spec-alist)
2560   (save-excursion
2561     (gnus-set-work-buffer)
2562     (insert format)
2563     (goto-char (point-min))
2564     (while (re-search-forward "\"" nil t)
2565       (replace-match "\\\"" nil t))
2566     (goto-char (point-min))
2567     (insert "(\"")
2568     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2569       (let ((number (if (match-beginning 1)
2570                         (match-string 1) "0"))
2571             (delim (aref (match-string 2) 0)))
2572         (if (or (= delim ?\() (= delim ?\{))
2573             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2574                                    " " number " \""))
2575           (replace-match "\")\""))))
2576     (goto-char (point-max))
2577     (insert "\")")
2578     (goto-char (point-min))
2579     (let ((form (read (current-buffer))))
2580       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2581
2582 (defun gnus-complex-form-to-spec (form spec-alist)
2583   (delq nil
2584         (mapcar
2585          (lambda (sform)
2586            (if (stringp sform)
2587                (gnus-parse-simple-format sform spec-alist t)
2588              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2589                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2590                       (nth 1 sform))))
2591          form)))
2592
2593 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2594   ;; This function parses the FORMAT string with the help of the
2595   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2596   ;; string.
2597   (let ((max-width 0)
2598         spec flist fstring newspec elem beg result dontinsert)
2599     (save-excursion
2600       (gnus-set-work-buffer)
2601       (insert format)
2602       (goto-char (point-min))
2603       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2604                                 nil t)
2605         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2606               (setq newspec "%"
2607                     beg (1+ (match-beginning 0)))
2608           ;; First check if there are any specs that look anything like
2609           ;; "%12,12A", ie. with a "max width specification".  These have
2610           ;; to be treated specially.
2611           (if (setq beg (match-beginning 1))
2612               (setq max-width
2613                     (string-to-int
2614                      (buffer-substring
2615                       (1+ (match-beginning 1)) (match-end 1))))
2616             (setq max-width 0)
2617             (setq beg (match-beginning 2)))
2618           ;; Find the specification from `spec-alist'.
2619           (unless (setq elem (cdr (assq spec spec-alist)))
2620             (setq elem '("*" ?s)))
2621           ;; Treat user defined format specifiers specially.
2622           (when (eq (car elem) 'gnus-tmp-user-defined)
2623             (setq elem
2624                   (list
2625                    (list (intern (concat "gnus-user-format-function-"
2626                                          (match-string 3)))
2627                          'gnus-tmp-header) ?s))
2628             (delete-region (match-beginning 3) (match-end 3)))
2629           (if (not (zerop max-width))
2630               (let ((el (car elem)))
2631                 (cond ((= (cadr elem) ?c)
2632                        (setq el (list 'char-to-string el)))
2633                       ((= (cadr elem) ?d)
2634                        (setq el (list 'int-to-string el))))
2635                 (setq flist (cons (gnus-max-width-function el max-width)
2636                                   flist))
2637                 (setq newspec ?s))
2638             (progn
2639               (setq flist (cons (car elem) flist))
2640               (setq newspec (cadr elem)))))
2641         ;; Remove the old specification (and possibly a ",12" string).
2642         (delete-region beg (match-end 2))
2643         ;; Insert the new specification.
2644         (goto-char beg)
2645         (insert newspec))
2646       (setq fstring (buffer-substring 1 (point-max))))
2647     ;; Do some postprocessing to increase efficiency.
2648     (setq
2649      result
2650      (cond
2651       ;; Emptyness.
2652       ((string= fstring "")
2653        nil)
2654       ;; Not a format string.
2655       ((not (string-match "%" fstring))
2656        (list fstring))
2657       ;; A format string with just a single string spec.
2658       ((string= fstring "%s")
2659        (list (car flist)))
2660       ;; A single character.
2661       ((string= fstring "%c")
2662        (list (car flist)))
2663       ;; A single number.
2664       ((string= fstring "%d")
2665        (setq dontinsert)
2666        (if insert
2667            (list `(princ ,(car flist)))
2668          (list `(int-to-string ,(car flist)))))
2669       ;; Just lots of chars and strings.
2670       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2671        (nreverse flist))
2672       ;; A single string spec at the beginning of the spec.
2673       ((string-match "\\`%[sc][^%]+\\'" fstring)
2674        (list (car flist) (substring fstring 2)))
2675       ;; A single string spec in the middle of the spec.
2676       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2677        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2678       ;; A single string spec in the end of the spec.
2679       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2680        (list (match-string 1 fstring) (car flist)))
2681       ;; A more complex spec.
2682       (t
2683        (list (cons 'format (cons fstring (nreverse flist)))))))
2684
2685     (if insert
2686         (when result
2687           (if dontinsert
2688               result
2689             (cons 'insert result)))
2690       (cond ((stringp result)
2691              result)
2692             ((consp result)
2693              (cons 'concat result))
2694             (t "")))))
2695
2696 (defun gnus-eval-format (format &optional alist props)
2697   "Eval the format variable FORMAT, using ALIST.
2698 If PROPS, insert the result."
2699   (let ((form (gnus-parse-format format alist props)))
2700     (if props
2701         (add-text-properties (point) (progn (eval form) (point)) props)
2702       (eval form))))
2703
2704 (defun gnus-remove-text-with-property (prop)
2705   "Delete all text in the current buffer with text property PROP."
2706   (save-excursion
2707     (goto-char (point-min))
2708     (while (not (eobp))
2709       (while (get-text-property (point) prop)
2710         (delete-char 1))
2711       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2712
2713 (defun gnus-set-work-buffer ()
2714   (if (get-buffer gnus-work-buffer)
2715       (progn
2716         (set-buffer gnus-work-buffer)
2717         (erase-buffer))
2718     (set-buffer (get-buffer-create gnus-work-buffer))
2719     (kill-all-local-variables)
2720     (buffer-disable-undo (current-buffer))
2721     (gnus-add-current-to-buffer-list)))
2722
2723 ;; Article file names when saving.
2724
2725 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2726   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2727 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2728 Otherwise, it is like ~/News/news/group/num."
2729   (let ((default
2730           (expand-file-name
2731            (concat (if (gnus-use-long-file-name 'not-save)
2732                        (gnus-capitalize-newsgroup newsgroup)
2733                      (gnus-newsgroup-directory-form newsgroup))
2734                    "/" (int-to-string (mail-header-number headers)))
2735            (or gnus-article-save-directory "~/News"))))
2736     (if (and last-file
2737              (string-equal (file-name-directory default)
2738                            (file-name-directory last-file))
2739              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2740         default
2741       (or last-file default))))
2742
2743 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2744   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2745 If variable `gnus-use-long-file-name' is non-nil, it is
2746 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2747   (let ((default
2748           (expand-file-name
2749            (concat (if (gnus-use-long-file-name 'not-save)
2750                        newsgroup
2751                      (gnus-newsgroup-directory-form newsgroup))
2752                    "/" (int-to-string (mail-header-number headers)))
2753            (or gnus-article-save-directory "~/News"))))
2754     (if (and last-file
2755              (string-equal (file-name-directory default)
2756                            (file-name-directory last-file))
2757              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2758         default
2759       (or last-file default))))
2760
2761 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2762   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2763 If variable `gnus-use-long-file-name' is non-nil, it is
2764 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2765   (or last-file
2766       (expand-file-name
2767        (if (gnus-use-long-file-name 'not-save)
2768            (gnus-capitalize-newsgroup newsgroup)
2769          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2770        (or gnus-article-save-directory "~/News"))))
2771
2772 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2773   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2774 If variable `gnus-use-long-file-name' is non-nil, it is
2775 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2776   (or last-file
2777       (expand-file-name
2778        (if (gnus-use-long-file-name 'not-save)
2779            newsgroup
2780          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2781        (or gnus-article-save-directory "~/News"))))
2782
2783 ;; For subscribing new newsgroup
2784
2785 (defun gnus-subscribe-hierarchical-interactive (groups)
2786   (let ((groups (sort groups 'string<))
2787         prefixes prefix start ans group starts)
2788     (while groups
2789       (setq prefixes (list "^"))
2790       (while (and groups prefixes)
2791         (while (not (string-match (car prefixes) (car groups)))
2792           (setq prefixes (cdr prefixes)))
2793         (setq prefix (car prefixes))
2794         (setq start (1- (length prefix)))
2795         (if (and (string-match "[^\\.]\\." (car groups) start)
2796                  (cdr groups)
2797                  (setq prefix
2798                        (concat "^" (substring (car groups) 0 (match-end 0))))
2799                  (string-match prefix (cadr groups)))
2800             (progn
2801               (setq prefixes (cons prefix prefixes))
2802               (message "Descend hierarchy %s? ([y]nsq): "
2803                        (substring prefix 1 (1- (length prefix))))
2804               (setq ans (read-char))
2805               (cond ((= ans ?n)
2806                      (while (and groups
2807                                  (string-match prefix
2808                                                (setq group (car groups))))
2809                        (setq gnus-killed-list
2810                              (cons group gnus-killed-list))
2811                        (gnus-sethash group group gnus-killed-hashtb)
2812                        (setq groups (cdr groups)))
2813                      (setq starts (cdr starts)))
2814                     ((= ans ?s)
2815                      (while (and groups
2816                                  (string-match prefix
2817                                                (setq group (car groups))))
2818                        (gnus-sethash group group gnus-killed-hashtb)
2819                        (gnus-subscribe-alphabetically (car groups))
2820                        (setq groups (cdr groups)))
2821                      (setq starts (cdr starts)))
2822                     ((= ans ?q)
2823                      (while groups
2824                        (setq group (car groups))
2825                        (setq gnus-killed-list (cons group gnus-killed-list))
2826                        (gnus-sethash group group gnus-killed-hashtb)
2827                        (setq groups (cdr groups))))
2828                     (t nil)))
2829           (message "Subscribe %s? ([n]yq)" (car groups))
2830           (setq ans (read-char))
2831           (setq group (car groups))
2832           (cond ((= ans ?y)
2833                  (gnus-subscribe-alphabetically (car groups))
2834                  (gnus-sethash group group gnus-killed-hashtb))
2835                 ((= ans ?q)
2836                  (while groups
2837                    (setq group (car groups))
2838                    (setq gnus-killed-list (cons group gnus-killed-list))
2839                    (gnus-sethash group group gnus-killed-hashtb)
2840                    (setq groups (cdr groups))))
2841                 (t
2842                  (setq gnus-killed-list (cons group gnus-killed-list))
2843                  (gnus-sethash group group gnus-killed-hashtb)))
2844           (setq groups (cdr groups)))))))
2845
2846 (defun gnus-subscribe-randomly (newsgroup)
2847   "Subscribe new NEWSGROUP by making it the first newsgroup."
2848   (gnus-subscribe-newsgroup newsgroup))
2849
2850 (defun gnus-subscribe-alphabetically (newgroup)
2851   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2852   (let ((groups (cdr gnus-newsrc-alist))
2853         before)
2854     (while (and (not before) groups)
2855       (if (string< newgroup (caar groups))
2856           (setq before (caar groups))
2857         (setq groups (cdr groups))))
2858     (gnus-subscribe-newsgroup newgroup before)))
2859
2860 (defun gnus-subscribe-hierarchically (newgroup)
2861   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2862   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2863   (save-excursion
2864     (set-buffer (find-file-noselect gnus-current-startup-file))
2865     (let ((groupkey newgroup)
2866           before)
2867       (while (and (not before) groupkey)
2868         (goto-char (point-min))
2869         (let ((groupkey-re
2870                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2871           (while (and (re-search-forward groupkey-re nil t)
2872                       (progn
2873                         (setq before (match-string 1))
2874                         (string< before newgroup)))))
2875         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2876         (setq groupkey
2877               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2878                   (substring groupkey (match-beginning 1) (match-end 1)))))
2879       (gnus-subscribe-newsgroup newgroup before))))
2880
2881 (defun gnus-subscribe-interactively (group)
2882   "Subscribe the new GROUP interactively.
2883 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2884 it is killed."
2885   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2886       (gnus-subscribe-hierarchically group)
2887     (push group gnus-killed-list)))
2888
2889 (defun gnus-subscribe-zombies (group)
2890   "Make the new GROUP into a zombie group."
2891   (push group gnus-zombie-list))
2892
2893 (defun gnus-subscribe-killed (group)
2894   "Make the new GROUP a killed group."
2895   (push group gnus-killed-list))
2896
2897 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2898   "Subscribe new NEWSGROUP.
2899 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2900 the first newsgroup."
2901   ;; We subscribe the group by changing its level to `subscribed'.
2902   (gnus-group-change-level
2903    newsgroup gnus-level-default-subscribed
2904    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2905   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2906
2907 ;; For directories
2908
2909 (defun gnus-newsgroup-directory-form (newsgroup)
2910   "Make hierarchical directory name from NEWSGROUP name."
2911   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2912         (len (length newsgroup))
2913         idx)
2914     ;; If this is a foreign group, we don't want to translate the
2915     ;; entire name.
2916     (if (setq idx (string-match ":" newsgroup))
2917         (aset newsgroup idx ?/)
2918       (setq idx 0))
2919     ;; Replace all occurrences of `.' with `/'.
2920     (while (< idx len)
2921       (if (= (aref newsgroup idx) ?.)
2922           (aset newsgroup idx ?/))
2923       (setq idx (1+ idx)))
2924     newsgroup))
2925
2926 (defun gnus-newsgroup-savable-name (group)
2927   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2928   ;; with dots.
2929   (nnheader-replace-chars-in-string group ?/ ?.))
2930
2931 (defun gnus-make-directory (dir)
2932   "Make DIRECTORY recursively."
2933   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2934   ;; of the many mysteries of the universe.
2935   (let* ((dir (expand-file-name dir default-directory))
2936          dirs err)
2937     (if (string-match "/$" dir)
2938         (setq dir (substring dir 0 (match-beginning 0))))
2939     ;; First go down the path until we find a directory that exists.
2940     (while (not (file-exists-p dir))
2941       (setq dirs (cons dir dirs))
2942       (string-match "/[^/]+$" dir)
2943       (setq dir (substring dir 0 (match-beginning 0))))
2944     ;; Then create all the subdirs.
2945     (while (and dirs (not err))
2946       (condition-case ()
2947           (make-directory (car dirs))
2948         (error (setq err t)))
2949       (setq dirs (cdr dirs)))
2950     ;; We return whether we were successful or not.
2951     (not dirs)))
2952
2953 (defun gnus-capitalize-newsgroup (newsgroup)
2954   "Capitalize NEWSGROUP name."
2955   (and (not (zerop (length newsgroup)))
2956        (concat (char-to-string (upcase (aref newsgroup 0)))
2957                (substring newsgroup 1))))
2958
2959 ;; Various... things.
2960
2961 (defun gnus-simplify-subject (subject &optional re-only)
2962   "Remove `Re:' and words in parentheses.
2963 If RE-ONLY is non-nil, strip leading `Re:'s only."
2964   (let ((case-fold-search t))           ;Ignore case.
2965     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
2966     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
2967       (setq subject (substring subject (match-end 0))))
2968     ;; Remove uninteresting prefixes.
2969     (if (and (not re-only)
2970              gnus-simplify-ignored-prefixes
2971              (string-match gnus-simplify-ignored-prefixes subject))
2972         (setq subject (substring subject (match-end 0))))
2973     ;; Remove words in parentheses from end.
2974     (unless re-only
2975       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2976         (setq subject (substring subject 0 (match-beginning 0)))))
2977     ;; Return subject string.
2978     subject))
2979
2980 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2981 ;; all whitespace.
2982 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2983 (defun gnus-simplify-buffer-fuzzy ()
2984   (goto-char (point-min))
2985   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
2986   (goto-char (match-beginning 0))
2987   (while (or
2988           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
2989           (looking-at "^[[].*:[ \t].*[]]$"))
2990     (goto-char (point-min))
2991     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2992                               nil t)
2993       (replace-match "" t t))
2994     (goto-char (point-min))
2995     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
2996       (goto-char (match-end 0))
2997       (delete-char -1)
2998       (delete-region
2999        (progn (goto-char (match-beginning 0)))
3000        (re-search-forward ":"))))
3001   (goto-char (point-min))
3002   (while (re-search-forward "[ \t\n]*[[{(][^()]*[]})][ \t]*$" nil t)
3003     (replace-match "" t t))
3004   (goto-char (point-min))
3005   (while (re-search-forward "[ \t]+" nil t)
3006     (replace-match " " t t))
3007   (goto-char (point-min))
3008   (while (re-search-forward "[ \t]+$" nil t)
3009     (replace-match "" t t))
3010   (goto-char (point-min))
3011   (while (re-search-forward "^[ \t]+" nil t)
3012     (replace-match "" t t))
3013   (goto-char (point-min))
3014   (if gnus-simplify-subject-fuzzy-regexp
3015       (if (listp gnus-simplify-subject-fuzzy-regexp)
3016           (let ((list gnus-simplify-subject-fuzzy-regexp))
3017             (while list
3018               (goto-char (point-min))
3019               (while (re-search-forward (car list) nil t)
3020                 (replace-match "" t t))
3021               (setq list (cdr list))))
3022         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3023           (replace-match "" t t)))))
3024
3025 (defun gnus-simplify-subject-fuzzy (subject)
3026   "Siplify a subject string fuzzily."
3027   (save-excursion
3028     (gnus-set-work-buffer)
3029     (let ((case-fold-search t))
3030       (insert subject)
3031       (inline (gnus-simplify-buffer-fuzzy))
3032       (buffer-string))))
3033
3034 ;; Add the current buffer to the list of buffers to be killed on exit.
3035 (defun gnus-add-current-to-buffer-list ()
3036   (or (memq (current-buffer) gnus-buffer-list)
3037       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3038
3039 (defun gnus-string> (s1 s2)
3040   (not (or (string< s1 s2)
3041            (string= s1 s2))))
3042
3043 ;;; General various misc type functions.
3044
3045 (defun gnus-clear-system ()
3046   "Clear all variables and buffers."
3047   ;; Clear Gnus variables.
3048   (let ((variables gnus-variable-list))
3049     (while variables
3050       (set (car variables) nil)
3051       (setq variables (cdr variables))))
3052   ;; Clear other internal variables.
3053   (setq gnus-list-of-killed-groups nil
3054         gnus-have-read-active-file nil
3055         gnus-newsrc-alist nil
3056         gnus-newsrc-hashtb nil
3057         gnus-killed-list nil
3058         gnus-zombie-list nil
3059         gnus-killed-hashtb nil
3060         gnus-active-hashtb nil
3061         gnus-moderated-list nil
3062         gnus-description-hashtb nil
3063         gnus-newsgroup-headers nil
3064         gnus-newsgroup-name nil
3065         gnus-server-alist nil
3066         gnus-opened-servers nil
3067         gnus-current-select-method nil)
3068   ;; Reset any score variables.
3069   (and gnus-use-scoring (gnus-score-close))
3070   ;; Kill the startup file.
3071   (and gnus-current-startup-file
3072        (get-file-buffer gnus-current-startup-file)
3073        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3074   ;; Save any cache buffers.
3075   (and gnus-use-cache (gnus-cache-save-buffers))
3076   ;; Clear the dribble buffer.
3077   (gnus-dribble-clear)
3078   ;; Close down NoCeM.
3079   (when gnus-use-nocem 
3080     (gnus-nocem-close))
3081   ;; Shut down the demons.
3082   (when gnus-use-demon
3083     (gnus-demon-cancel))
3084   ;; Kill global KILL file buffer.
3085   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
3086       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3087   (gnus-kill-buffer nntp-server-buffer)
3088   ;; Backlog.
3089   (and gnus-keep-backlog (gnus-backlog-shutdown))
3090   ;; Kill Gnus buffers.
3091   (while gnus-buffer-list
3092     (gnus-kill-buffer (car gnus-buffer-list))
3093     (setq gnus-buffer-list (cdr gnus-buffer-list))))
3094
3095 (defun gnus-windows-old-to-new (setting)
3096   ;; First we take care of the really, really old Gnus 3 actions.
3097   (if (symbolp setting)
3098       (setq setting
3099             (cond ((memq setting '(SelectArticle))
3100                    'article)
3101                   ((memq setting '(SelectSubject ExpandSubject))
3102                    'summary)
3103                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
3104                    'group)
3105                   (t setting))))
3106   (if (or (listp setting)
3107           (not (and gnus-window-configuration
3108                     (memq setting '(group summary article)))))
3109       setting
3110     (let* ((setting (if (eq setting 'group)
3111                         (if (assq 'newsgroup gnus-window-configuration)
3112                             'newsgroup
3113                           'newsgroups) setting))
3114            (elem (cadr (assq setting gnus-window-configuration)))
3115            (total (apply '+ elem))
3116            (types '(group summary article))
3117            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3118            (i 0)
3119            perc
3120            out)
3121       (while (< i 3)
3122         (or (not (numberp (nth i elem)))
3123             (zerop (nth i elem))
3124             (progn
3125               (setq perc  (/ (float (nth 0 elem)) total))
3126               (setq out (cons (if (eq pbuf (nth i types))
3127                                   (vector (nth i types) perc 'point)
3128                                 (vector (nth i types) perc))
3129                               out))))
3130         (setq i (1+ i)))
3131       (list (nreverse out)))))
3132
3133 ;;;###autoload
3134 (defun gnus-add-configuration (conf)
3135   "Add the window configuration CONF to `gnus-buffer-configuration'."
3136   (setq gnus-buffer-configuration
3137         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3138                          gnus-buffer-configuration))))
3139
3140 (defvar gnus-frame-list nil)
3141
3142 (defun gnus-configure-frame (split &optional window)
3143   "Split WINDOW according to SPLIT."
3144   (unless window
3145     (setq window (get-buffer-window (current-buffer))))
3146   (select-window window)
3147   ;; This might be an old-stylee buffer config.
3148   (when (vectorp split)
3149     (setq split (append split nil)))
3150   (when (or (consp (car split))
3151             (vectorp (car split)))
3152     (push 1.0 split)
3153     (push 'vertical split))
3154   ;; The SPLIT might be something that is to be evaled to
3155   ;; return a new SPLIT.
3156   (while (and (not (assq (car split) gnus-window-to-buffer))
3157               (gnus-functionp (car split)))
3158     (setq split (eval split)))
3159   (let* ((type (car split))
3160          (subs (cddr split))
3161          (len (if (eq type 'horizontal) (window-width) (window-height)))
3162          (total 0)
3163          (window-min-width (or gnus-window-min-width window-min-width))
3164          (window-min-height (or gnus-window-min-height window-min-height))
3165          s result new-win rest comp-subs size sub)
3166     (cond
3167      ;; Nothing to do here.
3168      ((null split))
3169      ;; Don't switch buffers.
3170      ((null type)
3171       (and (memq 'point split) window))
3172      ;; This is a buffer to be selected.
3173      ((not (memq type '(frame horizontal vertical)))
3174       (let ((buffer (cond ((stringp type) type)
3175                           (t (cdr (assq type gnus-window-to-buffer)))))
3176             buf)
3177         (unless buffer
3178           (error "Illegal buffer type: %s" type))
3179         (unless (setq buf (get-buffer (if (symbolp buffer)
3180                                           (symbol-value buffer) buffer)))
3181           (setq buf (get-buffer-create (if (symbolp buffer)
3182                                            (symbol-value buffer) buffer))))
3183         (switch-to-buffer buf)
3184         ;; We return the window if it has the `point' spec.
3185         (and (memq 'point split) window)))
3186      ;; This is a frame split.
3187      ((eq type 'frame)
3188       (unless gnus-frame-list
3189         (setq gnus-frame-list (list (window-frame
3190                                      (get-buffer-window (current-buffer))))))
3191       (let ((i 0)
3192             params frame fresult)
3193         (while (< i (length subs))
3194           ;; Frame parameter is gotten from the sub-split.
3195           (setq params (cadr (elt subs i)))
3196           ;; It should be a list.
3197           (unless (listp params)
3198             (setq params nil))
3199           ;; Create a new frame?
3200           (unless (setq frame (elt gnus-frame-list i))
3201             (nconc gnus-frame-list (list (setq frame (make-frame params)))))
3202           ;; Is the old frame still alive?
3203           (unless (frame-live-p frame)
3204             (setcar (nthcdr i gnus-frame-list)
3205                     (setq frame (make-frame params))))
3206           ;; Select the frame in question and do more splits there.
3207           (select-frame frame)
3208           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3209           (incf i))
3210         ;; Select the frame that has the selected buffer.
3211         (when fresult
3212           (select-frame (window-frame fresult)))))
3213      ;; This is a normal split.
3214      (t
3215       (when (> (length subs) 0)
3216         ;; First we have to compute the sizes of all new windows.
3217         (while subs
3218           (setq sub (append (pop subs) nil))
3219           (while (and (not (assq (car sub) gnus-window-to-buffer))
3220                       (gnus-functionp (car sub)))
3221             (setq sub (eval sub)))
3222           (when sub
3223             (push sub comp-subs)
3224             (setq size (cadar comp-subs))
3225             (cond ((equal size 1.0)
3226                    (setq rest (car comp-subs))
3227                    (setq s 0))
3228                   ((floatp size)
3229                    (setq s (floor (* size len))))
3230                   ((integerp size)
3231                    (setq s size))
3232                   (t
3233                    (error "Illegal size: %s" size)))
3234             ;; Try to make sure that we are inside the safe limits.
3235             (cond ((zerop s))
3236                   ((eq type 'horizontal)
3237                    (setq s (max s window-min-width)))
3238                   ((eq type 'vertical)
3239                    (setq s (max s window-min-height))))
3240             (setcar (cdar comp-subs) s)
3241             (incf total s)))
3242         ;; Take care of the "1.0" spec.
3243         (if rest
3244             (setcar (cdr rest) (- len total))
3245           (error "No 1.0 specs in %s" split))
3246         ;; The we do the actual splitting in a nice recursive
3247         ;; fashion.
3248         (setq comp-subs (nreverse comp-subs))
3249         (while comp-subs
3250           (if (null (cdr comp-subs))
3251               (setq new-win window)
3252             (setq new-win
3253                   (split-window window (cadar comp-subs)
3254                                 (eq type 'horizontal))))
3255           (setq result (or (gnus-configure-frame
3256                             (car comp-subs) window) result))
3257           (select-window new-win)
3258           (setq window new-win)
3259           (setq comp-subs (cdr comp-subs))))
3260       ;; Return the proper window, if any.
3261       (when result
3262         (select-window result))))))
3263
3264 (defvar gnus-frame-split-p nil)
3265
3266 (defun gnus-configure-windows (setting &optional force)
3267   (setq setting (gnus-windows-old-to-new setting))
3268   (let ((split (if (symbolp setting)
3269                    (cadr (assq setting gnus-buffer-configuration))
3270                  setting))
3271         (in-buf (current-buffer))
3272         rule val w height hor ohor heights sub jump-buffer
3273         rel total to-buf all-visible)
3274
3275     (setq gnus-frame-split-p nil)
3276
3277     (unless split
3278       (error "No such setting: %s" setting))
3279
3280     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3281              (not force))
3282         ;; All the windows mentioned are already visible, so we just
3283         ;; put point in the assigned buffer, and do not touch the
3284         ;; winconf.
3285         (select-window all-visible)
3286
3287       ;; Either remove all windows or just remove all Gnus windows.
3288       (let ((frame (selected-frame)))
3289         (unwind-protect
3290             (if gnus-use-full-window
3291                 ;; We want to remove all other windows.
3292                 (if (not gnus-frame-split-p)
3293                     ;; This is not a `frame' split, so we ignore the
3294                     ;; other frames.  
3295                     (delete-other-windows)
3296                   ;; This is a `frame' split, so we delete all windows
3297                   ;; on all frames.
3298                   (mapcar 
3299                    (lambda (frame)
3300                      (unless (eq (cdr (assq 'minibuffer
3301                                             (frame-parameters frame)))
3302                                  'only)
3303                        (select-frame frame)
3304                        (delete-other-windows)))
3305                    (frame-list)))
3306               ;; Just remove some windows.
3307               (gnus-remove-some-windows)
3308               (switch-to-buffer nntp-server-buffer))
3309           (select-frame frame)))
3310
3311       (switch-to-buffer nntp-server-buffer)
3312       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3313
3314 (defun gnus-all-windows-visible-p (split)
3315   (when (vectorp split)
3316     (setq split (append split nil)))
3317   (when (or (consp (car split))
3318             (vectorp (car split)))
3319     (push 1.0 split)
3320     (push 'vertical split))
3321   ;; The SPLIT might be something that is to be evaled to
3322   ;; return a new SPLIT.
3323   (while (and (not (assq (car split) gnus-window-to-buffer))
3324               (gnus-functionp (car split)))
3325     (setq split (eval split)))
3326   (let* ((type (elt split 0)))
3327     (cond
3328      ((null split)
3329       t)
3330      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3331       (let ((buffer (cond ((stringp type) type)
3332                           (t (cdr (assq type gnus-window-to-buffer)))))
3333             win buf)
3334         (unless buffer
3335           (error "Illegal buffer type: %s" type))
3336         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3337                                       buffer)))
3338           (setq win (get-buffer-window buf t)))
3339         (when win
3340           (if (memq 'point split)
3341               win
3342             t))))
3343      (t
3344       (when (eq type 'frame)
3345         (setq gnus-frame-split-p t))
3346       (let ((n (mapcar 'gnus-all-windows-visible-p
3347                        (cddr split)))
3348             (win t))
3349         (while n
3350           (cond ((windowp (car n))
3351                  (setq win (car n)))
3352                 ((null (car n))
3353                  (setq win nil)))
3354           (setq n (cdr n)))
3355         win)))))
3356
3357 (defun gnus-window-top-edge (&optional window)
3358   (nth 1 (window-edges window)))
3359
3360 (defun gnus-remove-some-windows ()
3361   (let ((buffers gnus-window-to-buffer)
3362         buf bufs lowest-buf lowest)
3363     (save-excursion
3364       ;; Remove windows on all known Gnus buffers.
3365       (while buffers
3366         (setq buf (cdar buffers))
3367         (if (symbolp buf)
3368             (setq buf (and (boundp buf) (symbol-value buf))))
3369         (and buf
3370              (get-buffer-window buf)
3371              (progn
3372                (setq bufs (cons buf bufs))
3373                (pop-to-buffer buf)
3374                (if (or (not lowest)
3375                        (< (gnus-window-top-edge) lowest))
3376                    (progn
3377                      (setq lowest (gnus-window-top-edge))
3378                      (setq lowest-buf buf)))))
3379         (setq buffers (cdr buffers)))
3380       ;; Remove windows on *all* summary buffers.
3381       (let (wins)
3382         (walk-windows
3383          (lambda (win)
3384            (let ((buf (window-buffer win)))
3385              (if (string-match  "^\\*Summary" (buffer-name buf))
3386                  (progn
3387                    (setq bufs (cons buf bufs))
3388                    (pop-to-buffer buf)
3389                    (if (or (not lowest)
3390                            (< (gnus-window-top-edge) lowest))
3391                        (progn
3392                          (setq lowest-buf buf)
3393                          (setq lowest (gnus-window-top-edge))))))))))
3394       (and lowest-buf
3395            (progn
3396              (pop-to-buffer lowest-buf)
3397              (switch-to-buffer nntp-server-buffer)))
3398       (while bufs
3399         (and (not (eq (car bufs) lowest-buf))
3400              (delete-windows-on (car bufs)))
3401         (setq bufs (cdr bufs))))))
3402
3403 (defun gnus-version ()
3404   "Version numbers of this version of Gnus."
3405   (interactive)
3406   (let ((methods gnus-valid-select-methods)
3407         (mess gnus-version)
3408         meth)
3409     ;; Go through all the legal select methods and add their version
3410     ;; numbers to the total version string.  Only the backends that are
3411     ;; currently in use will have their message numbers taken into
3412     ;; consideration.
3413     (while methods
3414       (setq meth (intern (concat (caar methods) "-version")))
3415       (and (boundp meth)
3416            (stringp (symbol-value meth))
3417            (setq mess (concat mess "; " (symbol-value meth))))
3418       (setq methods (cdr methods)))
3419     (gnus-message 2 mess)))
3420
3421 (defun gnus-info-find-node ()
3422   "Find Info documentation of Gnus."
3423   (interactive)
3424   ;; Enlarge info window if needed.
3425   (let ((mode major-mode)
3426         gnus-info-buffer)
3427     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3428     (setq gnus-info-buffer (current-buffer))
3429     (gnus-configure-windows 'info)))
3430
3431 (defun gnus-days-between (date1 date2)
3432   ;; Return the number of days between date1 and date2.
3433   (- (gnus-day-number date1) (gnus-day-number date2)))
3434
3435 (defun gnus-day-number (date)
3436   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3437                      (timezone-parse-date date))))
3438     (timezone-absolute-from-gregorian
3439      (nth 1 dat) (nth 2 dat) (car dat))))
3440
3441 (defun gnus-encode-date (date)
3442   "Convert DATE to internal time."
3443   (let* ((parse (timezone-parse-date date))
3444          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3445          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3446     (encode-time (caddr time) (cadr time) (car time)
3447                  (caddr date) (cadr date) (car date) (nth 4 date))))
3448
3449 (defun gnus-time-minus (t1 t2)
3450   "Subtract two internal times."
3451   (let ((borrow (< (cadr t1) (cadr t2))))
3452     (list (- (car t1) (car t2) (if borrow 1 0))
3453           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3454
3455 (defun gnus-file-newer-than (file date)
3456   (let ((fdate (nth 5 (file-attributes file))))
3457     (or (> (car fdate) (car date))
3458         (and (= (car fdate) (car date))
3459              (> (nth 1 fdate) (nth 1 date))))))
3460
3461 (defmacro gnus-define-keys (keymap &rest plist)
3462   "Define all keys in PLIST in KEYMAP."
3463   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3464
3465 (defmacro gnus-define-keymap (keymap &rest plist)
3466   "Define all keys in PLIST in KEYMAP."
3467   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3468
3469 (defun gnus-define-keys-1 (keymap plist)
3470   (when (null keymap)
3471     (error "Can't set keys in a null keymap"))
3472   (cond ((symbolp keymap)
3473          (setq keymap (symbol-value keymap)))
3474         ((keymapp keymap))
3475         ((listp keymap)
3476          (set (car keymap) nil)
3477          (define-prefix-command (car keymap))
3478          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3479          (setq keymap (symbol-value (car keymap)))))
3480   (let (key)
3481     (while plist
3482       (when (symbolp (setq key (pop plist)))
3483         (setq key (symbol-value key)))
3484       (define-key keymap key (pop plist)))))
3485
3486 (defun gnus-group-read-only-p (&optional group)
3487   "Check whether GROUP supports editing or not.
3488 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3489 that that variable is buffer-local to the summary buffers."
3490   (let ((group (or group gnus-newsgroup-name)))
3491     (not (gnus-check-backend-function 'request-replace-article group))))
3492
3493 (defun gnus-group-total-expirable-p (group)
3494   "Check whether GROUP is total-expirable or not."
3495   (let ((params (gnus-info-params (gnus-get-info group))))
3496     (or (memq 'total-expire params)
3497         (cdr (assq 'total-expire params)) ; (total-expire . t)
3498         (and gnus-total-expirable-newsgroups ; Check var.
3499              (string-match gnus-total-expirable-newsgroups group)))))
3500
3501 (defun gnus-group-auto-expirable-p (group)
3502   "Check whether GROUP is total-expirable or not."
3503   (let ((params (gnus-info-params (gnus-get-info group))))
3504     (or (memq 'auto-expire params)
3505         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3506         (and gnus-auto-expirable-newsgroups ; Check var.
3507              (string-match gnus-auto-expirable-newsgroups group)))))
3508
3509 (defun gnus-virtual-group-p (group)
3510   "Say whether GROUP is virtual or not."
3511   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3512                         gnus-valid-select-methods)))
3513
3514 (defsubst gnus-simplify-subject-fully (subject)
3515   "Simplify a subject string according to the user's wishes."
3516   (cond
3517    ((null gnus-summary-gather-subject-limit)
3518     (gnus-simplify-subject-re subject))
3519    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3520     (gnus-simplify-subject-fuzzy subject))
3521    ((numberp gnus-summary-gather-subject-limit)
3522     (gnus-limit-string (gnus-simplify-subject-re subject)
3523                        gnus-summary-gather-subject-limit))
3524    (t
3525     subject)))
3526
3527 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3528   "Check whether two subjects are equal.  If optional argument
3529 simple-first is t, first argument is already simplified."
3530   (cond
3531    ((null simple-first)
3532     (equal (gnus-simplify-subject-fully s1)
3533            (gnus-simplify-subject-fully s2)))
3534    (t
3535     (equal s1
3536            (gnus-simplify-subject-fully s2)))))
3537
3538 ;; Returns a list of writable groups.
3539 (defun gnus-writable-groups ()
3540   (let ((alist gnus-newsrc-alist)
3541         groups group)
3542     (while (setq group (car (pop alist)))
3543       (unless (gnus-group-read-only-p group)
3544         (push group groups)))
3545     (nreverse groups)))
3546
3547 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3548 ;; the echo area.
3549 (defun gnus-y-or-n-p (prompt)
3550   (prog1
3551       (y-or-n-p prompt)
3552     (message "")))
3553
3554 (defun gnus-yes-or-no-p (prompt)
3555   (prog1
3556       (yes-or-no-p prompt)
3557     (message "")))
3558
3559 ;; Check whether to use long file names.
3560 (defun gnus-use-long-file-name (symbol)
3561   ;; The variable has to be set...
3562   (and gnus-use-long-file-name
3563        ;; If it isn't a list, then we return t.
3564        (or (not (listp gnus-use-long-file-name))
3565            ;; If it is a list, and the list contains `symbol', we
3566            ;; return nil.
3567            (not (memq symbol gnus-use-long-file-name)))))
3568
3569 ;; I suspect there's a better way, but I haven't taken the time to do
3570 ;; it yet. -erik selberg@cs.washington.edu
3571 (defun gnus-dd-mmm (messy-date)
3572   "Return a string like DD-MMM from a big messy string"
3573   (let ((datevec (timezone-parse-date messy-date)))
3574     (format "%2s-%s"
3575             (or (aref datevec 2) "??")
3576             (capitalize
3577              (or (car
3578                   (nth (1- (string-to-number (aref datevec 1)))
3579                        timezone-months-assoc))
3580                  "???")))))
3581
3582 ;; Make a hash table (default and minimum size is 255).
3583 ;; Optional argument HASHSIZE specifies the table size.
3584 (defun gnus-make-hashtable (&optional hashsize)
3585   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3586
3587 ;; Make a number that is suitable for hashing; bigger than MIN and one
3588 ;; less than 2^x.
3589 (defun gnus-create-hash-size (min)
3590   (let ((i 1))
3591     (while (< i min)
3592       (setq i (* 2 i)))
3593     (1- i)))
3594
3595 ;; Show message if message has a lower level than `gnus-verbose'.
3596 ;; Guideline for numbers:
3597 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3598 ;; for things that take a long time, 7 - not very important messages
3599 ;; on stuff, 9 - messages inside loops.
3600 (defun gnus-message (level &rest args)
3601   (if (<= level gnus-verbose)
3602       (apply 'message args)
3603     ;; We have to do this format thingy here even if the result isn't
3604     ;; shown - the return value has to be the same as the return value
3605     ;; from `message'.
3606     (apply 'format args)))
3607
3608 (defun gnus-functionp (form)
3609   "Return non-nil if FORM is funcallable."
3610   (or (and (symbolp form) (fboundp form))
3611       (and (listp form) (eq (car form) 'lambda))))
3612
3613 ;; Generate a unique new group name.
3614 (defun gnus-generate-new-group-name (leaf)
3615   (let ((name leaf)
3616         (num 0))
3617     (while (gnus-gethash name gnus-newsrc-hashtb)
3618       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3619     name))
3620
3621 ;; Find out whether the gnus-visual TYPE is wanted.
3622 (defun gnus-visual-p (&optional type class)
3623   (and gnus-visual                      ; Has to be non-nil, at least.
3624        (if (not type)                   ; We don't care about type.
3625            gnus-visual
3626          (if (listp gnus-visual)        ; It's a list, so we check it.
3627              (or (memq type gnus-visual)
3628                  (memq class gnus-visual))
3629            t))))
3630
3631 (defun gnus-parent-id (references)
3632   "Return the last Message-ID in REFERENCES."
3633   (when (and references
3634              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3635     (substring references (match-beginning 1) (match-end 1))))
3636
3637 (defun gnus-split-references (references)
3638   "Return a list of Message-IDs in REFERENCES."
3639   (let ((beg 0)
3640         ids)
3641     (while (string-match "<[^>]+>" references beg)
3642       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3643             ids))
3644     (nreverse ids)))
3645
3646 (defun gnus-ephemeral-group-p (group)
3647   "Say whether GROUP is ephemeral or not."
3648   (gnus-group-get-parameter group 'quit-config))
3649
3650 (defun gnus-group-quit-config (group)
3651   "Return the quit-config of GROUP."
3652   (gnus-group-get-parameter group 'quit-config))
3653
3654 (defun gnus-simplify-mode-line ()
3655   "Make mode lines a bit simpler."
3656   (setq mode-line-modified "-- ")
3657   (when (listp mode-line-format)
3658     (make-local-variable 'mode-line-format)
3659     (setq mode-line-format (copy-sequence mode-line-format))
3660     (when (equal (nth 3 mode-line-format) "   ")
3661       (setcar (nthcdr 3 mode-line-format) " "))))
3662
3663 ;;; List and range functions
3664
3665 (defun gnus-last-element (list)
3666   "Return last element of LIST."
3667   (while (cdr list)
3668     (setq list (cdr list)))
3669   (car list))
3670
3671 (defun gnus-copy-sequence (list)
3672   "Do a complete, total copy of a list."
3673   (if (and (consp list) (not (consp (cdr list))))
3674       (cons (car list) (cdr list))
3675     (mapcar (lambda (elem) (if (consp elem)
3676                                (if (consp (cdr elem))
3677                                    (gnus-copy-sequence elem)
3678                                  (cons (car elem) (cdr elem)))
3679                              elem))
3680             list)))
3681
3682 (defun gnus-set-difference (list1 list2)
3683   "Return a list of elements of LIST1 that do not appear in LIST2."
3684   (let ((list1 (copy-sequence list1)))
3685     (while list2
3686       (setq list1 (delq (car list2) list1))
3687       (setq list2 (cdr list2)))
3688     list1))
3689
3690 (defun gnus-sorted-complement (list1 list2)
3691   "Return a list of elements of LIST1 that do not appear in LIST2.
3692 Both lists have to be sorted over <."
3693   (let (out)
3694     (if (or (null list1) (null list2))
3695         (or list1 list2)
3696       (while (and list1 list2)
3697         (cond ((= (car list1) (car list2))
3698                (setq list1 (cdr list1)
3699                      list2 (cdr list2)))
3700               ((< (car list1) (car list2))
3701                (setq out (cons (car list1) out))
3702                (setq list1 (cdr list1)))
3703               (t
3704                (setq out (cons (car list2) out))
3705                (setq list2 (cdr list2)))))
3706       (nconc (nreverse out) (or list1 list2)))))
3707
3708 (defun gnus-intersection (list1 list2)
3709   (let ((result nil))
3710     (while list2
3711       (if (memq (car list2) list1)
3712           (setq result (cons (car list2) result)))
3713       (setq list2 (cdr list2)))
3714     result))
3715
3716 (defun gnus-sorted-intersection (list1 list2)
3717   ;; LIST1 and LIST2 have to be sorted over <.
3718   (let (out)
3719     (while (and list1 list2)
3720       (cond ((= (car list1) (car list2))
3721              (setq out (cons (car list1) out)
3722                    list1 (cdr list1)
3723                    list2 (cdr list2)))
3724             ((< (car list1) (car list2))
3725              (setq list1 (cdr list1)))
3726             (t
3727              (setq list2 (cdr list2)))))
3728     (nreverse out)))
3729
3730 (defun gnus-set-sorted-intersection (list1 list2)
3731   ;; LIST1 and LIST2 have to be sorted over <.
3732   ;; This function modifies LIST1.
3733   (let* ((top (cons nil list1))
3734          (prev top))
3735     (while (and list1 list2)
3736       (cond ((= (car list1) (car list2))
3737              (setq prev list1
3738                    list1 (cdr list1)
3739                    list2 (cdr list2)))
3740             ((< (car list1) (car list2))
3741              (setcdr prev (cdr list1))
3742              (setq list1 (cdr list1)))
3743             (t
3744              (setq list2 (cdr list2)))))
3745     (setcdr prev nil)
3746     (cdr top)))
3747
3748 (defun gnus-compress-sequence (numbers &optional always-list)
3749   "Convert list of numbers to a list of ranges or a single range.
3750 If ALWAYS-LIST is non-nil, this function will always release a list of
3751 ranges."
3752   (let* ((first (car numbers))
3753          (last (car numbers))
3754          result)
3755     (if (null numbers)
3756         nil
3757       (if (not (listp (cdr numbers)))
3758           numbers
3759         (while numbers
3760           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3761                 ((= (1+ last) (car numbers)) ;Still in sequence
3762                  (setq last (car numbers)))
3763                 (t                      ;End of one sequence
3764                  (setq result
3765                        (cons (if (= first last) first
3766                                (cons first last)) result))
3767                  (setq first (car numbers))
3768                  (setq last  (car numbers))))
3769           (setq numbers (cdr numbers)))
3770         (if (and (not always-list) (null result))
3771             (if (= first last) (list first) (cons first last))
3772           (nreverse (cons (if (= first last) first (cons first last))
3773                           result)))))))
3774
3775 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3776 (defun gnus-uncompress-range (ranges)
3777   "Expand a list of ranges into a list of numbers.
3778 RANGES is either a single range on the form `(num . num)' or a list of
3779 these ranges."
3780   (let (first last result)
3781     (cond
3782      ((null ranges)
3783       nil)
3784      ((not (listp (cdr ranges)))
3785       (setq first (car ranges))
3786       (setq last (cdr ranges))
3787       (while (<= first last)
3788         (setq result (cons first result))
3789         (setq first (1+ first)))
3790       (nreverse result))
3791      (t
3792       (while ranges
3793         (if (atom (car ranges))
3794             (if (numberp (car ranges))
3795                 (setq result (cons (car ranges) result)))
3796           (setq first (caar ranges))
3797           (setq last  (cdar ranges))
3798           (while (<= first last)
3799             (setq result (cons first result))
3800             (setq first (1+ first))))
3801         (setq ranges (cdr ranges)))
3802       (nreverse result)))))
3803
3804 (defun gnus-add-to-range (ranges list)
3805   "Return a list of ranges that has all articles from both RANGES and LIST.
3806 Note: LIST has to be sorted over `<'."
3807   (if (not ranges)
3808       (gnus-compress-sequence list t)
3809     (setq list (copy-sequence list))
3810     (or (listp (cdr ranges))
3811         (setq ranges (list ranges)))
3812     (let ((out ranges)
3813           ilist lowest highest temp)
3814       (while (and ranges list)
3815         (setq ilist list)
3816         (setq lowest (or (and (atom (car ranges)) (car ranges))
3817                          (caar ranges)))
3818         (while (and list (cdr list) (< (cadr list) lowest))
3819           (setq list (cdr list)))
3820         (if (< (car ilist) lowest)
3821             (progn
3822               (setq temp list)
3823               (setq list (cdr list))
3824               (setcdr temp nil)
3825               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3826         (setq highest (or (and (atom (car ranges)) (car ranges))
3827                           (cdar ranges)))
3828         (while (and list (<= (car list) highest))
3829           (setq list (cdr list)))
3830         (setq ranges (cdr ranges)))
3831       (if list
3832           (setq out (nconc (gnus-compress-sequence list t) out)))
3833       (setq out (sort out (lambda (r1 r2)
3834                             (< (or (and (atom r1) r1) (car r1))
3835                                (or (and (atom r2) r2) (car r2))))))
3836       (setq ranges out)
3837       (while ranges
3838         (if (atom (car ranges))
3839             (if (cdr ranges)
3840                 (if (atom (cadr ranges))
3841                     (if (= (1+ (car ranges)) (cadr ranges))
3842                         (progn
3843                           (setcar ranges (cons (car ranges)
3844                                                (cadr ranges)))
3845                           (setcdr ranges (cddr ranges))))
3846                   (if (= (1+ (car ranges)) (caadr ranges))
3847                       (progn
3848                         (setcar (cadr ranges) (car ranges))
3849                         (setcar ranges (cadr ranges))
3850                         (setcdr ranges (cddr ranges))))))
3851           (if (cdr ranges)
3852               (if (atom (cadr ranges))
3853                   (if (= (1+ (cdar ranges)) (cadr ranges))
3854                       (progn
3855                         (setcdr (car ranges) (cadr ranges))
3856                         (setcdr ranges (cddr ranges))))
3857                 (if (= (1+ (cdar ranges)) (caadr ranges))
3858                     (progn
3859                       (setcdr (car ranges) (cdadr ranges))
3860                       (setcdr ranges (cddr ranges)))))))
3861         (setq ranges (cdr ranges)))
3862       out)))
3863
3864 (defun gnus-remove-from-range (ranges list)
3865   "Return a list of ranges that has all articles from LIST removed from RANGES.
3866 Note: LIST has to be sorted over `<'."
3867   ;; !!! This function shouldn't look like this, but I've got a headache.
3868   (gnus-compress-sequence
3869    (gnus-sorted-complement
3870     (gnus-uncompress-range ranges) list)))
3871
3872 (defun gnus-member-of-range (number ranges)
3873   (if (not (listp (cdr ranges)))
3874       (and (>= number (car ranges))
3875            (<= number (cdr ranges)))
3876     (let ((not-stop t))
3877       (while (and ranges
3878                   (if (numberp (car ranges))
3879                       (>= number (car ranges))
3880                     (>= number (caar ranges)))
3881                   not-stop)
3882         (if (if (numberp (car ranges))
3883                 (= number (car ranges))
3884               (and (>= number (caar ranges))
3885                    (<= number (cdar ranges))))
3886             (setq not-stop nil))
3887         (setq ranges (cdr ranges)))
3888       (not not-stop))))
3889
3890 (defun gnus-range-length (range)
3891   "Return the length RANGE would have if uncompressed."
3892   (length (gnus-uncompress-range range)))
3893
3894 (defun gnus-sublist-p (list sublist)
3895   "Test whether all elements in SUBLIST are members of LIST."
3896   (let ((sublistp t))
3897     (while sublist
3898       (unless (memq (pop sublist) list)
3899         (setq sublistp nil
3900               sublist nil)))
3901     sublistp))
3902
3903 \f
3904 ;;;
3905 ;;; Gnus group mode
3906 ;;;
3907
3908 (defvar gnus-group-mode-map nil)
3909 (put 'gnus-group-mode 'mode-class 'special)
3910
3911 (unless gnus-group-mode-map
3912   (setq gnus-group-mode-map (make-keymap))
3913   (suppress-keymap gnus-group-mode-map)
3914
3915   (gnus-define-keys
3916    gnus-group-mode-map
3917    " " gnus-group-read-group
3918    "=" gnus-group-select-group
3919    "\M- " gnus-group-unhidden-select-group
3920    "\r" gnus-group-select-group
3921    "\M-\r" gnus-group-quick-select-group
3922    "j" gnus-group-jump-to-group
3923    "n" gnus-group-next-unread-group
3924    "p" gnus-group-prev-unread-group
3925    "\177" gnus-group-prev-unread-group
3926    [delete] gnus-group-prev-unread-group
3927    "N" gnus-group-next-group
3928    "P" gnus-group-prev-group
3929    "\M-n" gnus-group-next-unread-group-same-level
3930    "\M-p" gnus-group-prev-unread-group-same-level
3931    "," gnus-group-best-unread-group
3932    "." gnus-group-first-unread-group
3933    "u" gnus-group-unsubscribe-current-group
3934    "U" gnus-group-unsubscribe-group
3935    "c" gnus-group-catchup-current
3936    "C" gnus-group-catchup-current-all
3937    "l" gnus-group-list-groups
3938    "L" gnus-group-list-all-groups
3939    "m" gnus-group-mail
3940    "g" gnus-group-get-new-news
3941    "\M-g" gnus-group-get-new-news-this-group
3942    "R" gnus-group-restart
3943    "r" gnus-group-read-init-file
3944    "B" gnus-group-browse-foreign-server
3945    "b" gnus-group-check-bogus-groups
3946    "F" gnus-find-new-newsgroups
3947    "\C-c\C-d" gnus-group-describe-group
3948    "\M-d" gnus-group-describe-all-groups
3949    "\C-c\C-a" gnus-group-apropos
3950    "\C-c\M-\C-a" gnus-group-description-apropos
3951    "a" gnus-group-post-news
3952    "\ek" gnus-group-edit-local-kill
3953    "\eK" gnus-group-edit-global-kill
3954    "\C-k" gnus-group-kill-group
3955    "\C-y" gnus-group-yank-group
3956    "\C-w" gnus-group-kill-region
3957    "\C-x\C-t" gnus-group-transpose-groups
3958    "\C-c\C-l" gnus-group-list-killed
3959    "\C-c\C-x" gnus-group-expire-articles
3960    "\C-c\M-\C-x" gnus-group-expire-all-groups
3961    "V" gnus-version
3962    "s" gnus-group-save-newsrc
3963    "z" gnus-group-suspend
3964    "Z" gnus-group-clear-dribble
3965    "q" gnus-group-exit
3966    "Q" gnus-group-quit
3967    "?" gnus-group-describe-briefly
3968    "\C-c\C-i" gnus-info-find-node
3969    "\M-e" gnus-group-edit-group-method
3970    "^" gnus-group-enter-server-mode
3971    gnus-mouse-2 gnus-mouse-pick-group
3972    "<" beginning-of-buffer
3973    ">" end-of-buffer
3974    "\C-c\C-b" gnus-bug
3975    "\C-c\C-s" gnus-group-sort-groups
3976    "t" gnus-topic-mode
3977    "\C-c\M-g" gnus-activate-all-groups
3978    "\M-&" gnus-group-universal-argument
3979    "#" gnus-group-mark-group
3980    "\M-#" gnus-group-unmark-group)
3981
3982   (gnus-define-keys
3983    (gnus-group-mark-map "M" gnus-group-mode-map)
3984    "m" gnus-group-mark-group
3985    "u" gnus-group-unmark-group
3986    "w" gnus-group-mark-region
3987    "m" gnus-group-mark-buffer
3988    "r" gnus-group-mark-regexp
3989    "U" gnus-group-unmark-all-groups)
3990
3991   (gnus-define-keys
3992    (gnus-group-group-map "G" gnus-group-mode-map)
3993    "d" gnus-group-make-directory-group
3994    "h" gnus-group-make-help-group
3995    "a" gnus-group-make-archive-group
3996    "k" gnus-group-make-kiboze-group
3997    "m" gnus-group-make-group
3998    "E" gnus-group-edit-group
3999    "e" gnus-group-edit-group-method
4000    "p" gnus-group-edit-group-parameters
4001    "v" gnus-group-add-to-virtual
4002    "V" gnus-group-make-empty-virtual
4003    "D" gnus-group-enter-directory
4004    "f" gnus-group-make-doc-group
4005    "r" gnus-group-rename-group
4006    "\177" gnus-group-delete-group
4007    [delete] gnus-group-delete-group)
4008
4009    (gnus-define-keys
4010     (gnus-group-soup-map "s" gnus-group-group-map)
4011     "b" gnus-group-brew-soup
4012     "w" gnus-soup-save-areas
4013     "s" gnus-soup-send-replies
4014     "p" gnus-soup-pack-packet
4015     "r" nnsoup-pack-replies)
4016
4017    (gnus-define-keys
4018     (gnus-group-sort-map "S" gnus-group-group-map)
4019     "s" gnus-group-sort-groups
4020     "a" gnus-group-sort-groups-by-alphabet
4021     "u" gnus-group-sort-groups-by-unread
4022     "l" gnus-group-sort-groups-by-level
4023     "v" gnus-group-sort-groups-by-score
4024     "r" gnus-group-sort-groups-by-rank
4025     "m" gnus-group-sort-groups-by-method)
4026
4027    (gnus-define-keys
4028     (gnus-group-list-map "A" gnus-group-mode-map)
4029     "k" gnus-group-list-killed
4030     "z" gnus-group-list-zombies
4031     "s" gnus-group-list-groups
4032     "u" gnus-group-list-all-groups
4033     "A" gnus-group-list-active
4034     "a" gnus-group-apropos
4035     "d" gnus-group-description-apropos
4036     "m" gnus-group-list-matching
4037     "M" gnus-group-list-all-matching
4038     "l" gnus-group-list-level)
4039
4040    (gnus-define-keys
4041     (gnus-group-score-map "W" gnus-group-mode-map)
4042     "f" gnus-score-flush-cache)
4043
4044    (gnus-define-keys
4045     (gnus-group-help-map "H" gnus-group-mode-map)
4046     "f" gnus-group-fetch-faq)
4047
4048    (gnus-define-keys
4049     (gnus-group-sub-map "S" gnus-group-mode-map)
4050     "l" gnus-group-set-current-level
4051     "t" gnus-group-unsubscribe-current-group
4052     "s" gnus-group-unsubscribe-group
4053     "k" gnus-group-kill-group
4054     "y" gnus-group-yank-group
4055     "w" gnus-group-kill-region
4056     "\C-k" gnus-group-kill-level
4057     "z" gnus-group-kill-all-zombies))
4058
4059 (defun gnus-group-mode ()
4060   "Major mode for reading news.
4061
4062 All normal editing commands are switched off.
4063 \\<gnus-group-mode-map>
4064 The group buffer lists (some of) the groups available.  For instance,
4065 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4066 lists all zombie groups.
4067
4068 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4069 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4070
4071 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4072
4073 The following commands are available:
4074
4075 \\{gnus-group-mode-map}"
4076   (interactive)
4077   (when (and menu-bar-mode
4078              (gnus-visual-p 'group-menu 'menu))
4079     (gnus-group-make-menu-bar))
4080   (kill-all-local-variables)
4081   (gnus-simplify-mode-line)
4082   (setq major-mode 'gnus-group-mode)
4083   (setq mode-name "Group")
4084   (gnus-group-set-mode-line)
4085   (setq mode-line-process nil)
4086   (use-local-map gnus-group-mode-map)
4087   (buffer-disable-undo (current-buffer))
4088   (setq truncate-lines t)
4089   (setq buffer-read-only t)
4090   (run-hooks 'gnus-group-mode-hook))
4091
4092 (defun gnus-mouse-pick-group (e)
4093   "Enter the group under the mouse pointer."
4094   (interactive "e")
4095   (mouse-set-point e)
4096   (gnus-group-read-group nil))
4097
4098 ;; Look at LEVEL and find out what the level is really supposed to be.
4099 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4100 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4101 (defun gnus-group-default-level (&optional level number-or-nil)
4102   (cond
4103    (gnus-group-use-permanent-levels
4104     (setq gnus-group-default-list-level
4105           (or level gnus-group-default-list-level))
4106     (or gnus-group-default-list-level gnus-level-subscribed))
4107    (number-or-nil
4108     level)
4109    (t
4110     (or level gnus-group-default-list-level gnus-level-subscribed))))
4111
4112 ;;;###autoload
4113 (defun gnus-slave-no-server (&optional arg)
4114   "Read network news as a slave, without connecting to local server"
4115   (interactive "P")
4116   (gnus-no-server arg t))
4117
4118 ;;;###autoload
4119 (defun gnus-no-server (&optional arg slave)
4120   "Read network news.
4121 If ARG is a positive number, Gnus will use that as the
4122 startup level.  If ARG is nil, Gnus will be started at level 2.
4123 If ARG is non-nil and not a positive number, Gnus will
4124 prompt the user for the name of an NNTP server to use.
4125 As opposed to `gnus', this command will not connect to the local server."
4126   (interactive "P")
4127   (make-local-variable 'gnus-group-use-permanent-levels)
4128   (setq gnus-group-use-permanent-levels t)
4129   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4130
4131 ;;;###autoload
4132 (defun gnus-slave (&optional arg)
4133   "Read news as a slave."
4134   (interactive "P")
4135   (gnus arg nil 'slave))
4136
4137 ;;;###autoload
4138 (defun gnus-other-frame (&optional arg)
4139   "Pop up a frame to read news."
4140   (interactive "P")
4141   (if (get-buffer gnus-group-buffer)
4142       (let ((pop-up-frames t))
4143         (gnus arg))
4144     (select-frame (make-frame))
4145     (gnus arg)))
4146
4147 ;;;###autoload
4148 (defun gnus (&optional arg dont-connect slave)
4149   "Read network news.
4150 If ARG is non-nil and a positive number, Gnus will use that as the
4151 startup level.  If ARG is non-nil and not a positive number, Gnus will
4152 prompt the user for the name of an NNTP server to use."
4153   (interactive "P")
4154
4155   (if (get-buffer gnus-group-buffer)
4156       (progn
4157         (switch-to-buffer gnus-group-buffer)
4158         (gnus-group-get-new-news))
4159
4160     (gnus-clear-system)
4161     (nnheader-init-server-buffer)
4162     (gnus-read-init-file)
4163     (setq gnus-slave slave)
4164
4165     (gnus-group-setup-buffer)
4166     (let ((buffer-read-only nil))
4167       (erase-buffer)
4168       (if (not gnus-inhibit-startup-message)
4169           (progn
4170             (gnus-group-startup-message)
4171             (sit-for 0))))
4172
4173     (let ((level (and (numberp arg) (> arg 0) arg))
4174           did-connect)
4175       (unwind-protect
4176           (progn
4177             (or dont-connect
4178                 (setq did-connect
4179                       (gnus-start-news-server (and arg (not level))))))
4180         (if (and (not dont-connect)
4181                  (not did-connect))
4182             (gnus-group-quit)
4183           (run-hooks 'gnus-startup-hook)
4184           ;; NNTP server is successfully open.
4185
4186           ;; Find the current startup file name.
4187           (setq gnus-current-startup-file
4188                 (gnus-make-newsrc-file gnus-startup-file))
4189
4190           ;; Read the dribble file.
4191           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4192
4193           (gnus-summary-make-display-table)
4194           ;; Do the actual startup.
4195           (gnus-setup-news nil level)
4196           ;; Generate the group buffer.
4197           (gnus-group-list-groups level)
4198           (gnus-group-first-unread-group)
4199           (gnus-configure-windows 'group)
4200           (gnus-group-set-mode-line))))))
4201
4202 (defun gnus-unload ()
4203   "Unload all Gnus features."
4204   (interactive)
4205   (or (boundp 'load-history)
4206       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4207   (let ((history load-history)
4208         feature)
4209     (while history
4210       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4211            (setq feature (cdr (assq 'provide (car history))))
4212            (unload-feature feature 'force))
4213       (setq history (cdr history)))))
4214
4215 (defun gnus-compile ()
4216   "Byte-compile the user-defined format specs."
4217   (interactive)
4218   (let ((entries gnus-format-specs)
4219         entry gnus-tmp-func)
4220     (save-excursion
4221       (gnus-message 7 "Compiling format specs...")
4222
4223       (while entries
4224         (setq entry (pop entries))
4225         (if (eq (car entry) 'version)
4226             (setq gnus-format-specs (delq entry gnus-format-specs))
4227           (when (and (listp (caddr entry))
4228                      (not (eq 'byte-code (caaddr entry))))
4229             (fset 'gnus-tmp-func
4230                   `(lambda () ,(caddr entry)))
4231             (byte-compile 'gnus-tmp-func)
4232             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4233
4234       (push (cons 'version emacs-version) gnus-format-specs)
4235
4236       (gnus-message 7 "Compiling user specs...done"))))
4237
4238 (defun gnus-indent-rigidly (start end arg)
4239   "Indent rigidly using only spaces and no tabs."
4240   (save-excursion
4241     (save-restriction
4242       (narrow-to-region start end)
4243       (indent-rigidly start end arg)
4244       (goto-char (point-min))
4245       (while (search-forward "\t" nil t)
4246         (replace-match "        " t t)))))
4247
4248 (defun gnus-group-startup-message (&optional x y)
4249   "Insert startup message in current buffer."
4250   ;; Insert the message.
4251   (erase-buffer)
4252   (insert
4253    (format "              %s
4254           _    ___ _             _
4255           _ ___ __ ___  __    _ ___
4256           __   _     ___    __  ___
4257               _           ___     _
4258              _  _ __             _
4259              ___   __            _
4260                    __           _
4261                     _      _   _
4262                    _      _    _
4263                       _  _    _
4264                   __  ___
4265                  _   _ _     _
4266                 _   _
4267               _    _
4268              _    _
4269             _
4270           __
4271
4272 "
4273            ""))
4274   ;; And then hack it.
4275   (gnus-indent-rigidly (point-min) (point-max)
4276                        (/ (max (- (window-width) (or x 46)) 0) 2))
4277   (goto-char (point-min))
4278   (forward-line 1)
4279   (let* ((pheight (count-lines (point-min) (point-max)))
4280          (wheight (window-height))
4281          (rest (- wheight pheight)))
4282     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4283   ;; Fontify some.
4284   (goto-char (point-min))
4285   (and (search-forward "Praxis" nil t)
4286        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4287   (goto-char (point-min))
4288   (let* ((mode-string (gnus-group-set-mode-line)))
4289     (setq mode-line-buffer-identification
4290           (list (concat gnus-version (substring (car mode-string) 4))))
4291     (set-buffer-modified-p t)))
4292
4293 (defun gnus-group-setup-buffer ()
4294   (or (get-buffer gnus-group-buffer)
4295       (progn
4296         (switch-to-buffer gnus-group-buffer)
4297         (gnus-add-current-to-buffer-list)
4298         (gnus-group-mode)
4299         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4300
4301 (defun gnus-group-list-groups (&optional level unread lowest)
4302   "List newsgroups with level LEVEL or lower that have unread articles.
4303 Default is all subscribed groups.
4304 If argument UNREAD is non-nil, groups with no unread articles are also
4305 listed."
4306   (interactive (list (if current-prefix-arg
4307                          (prefix-numeric-value current-prefix-arg)
4308                        (or
4309                         (gnus-group-default-level nil t)
4310                         gnus-group-default-list-level
4311                         gnus-level-subscribed))))
4312   (or level
4313       (setq level (car gnus-group-list-mode)
4314             unread (cdr gnus-group-list-mode)))
4315   (setq level (gnus-group-default-level level))
4316   (gnus-group-setup-buffer)             ;May call from out of group buffer
4317   (gnus-update-format-specifications)
4318   (let ((case-fold-search nil)
4319         (props (text-properties-at (gnus-point-at-bol)))
4320         (group (gnus-group-group-name)))
4321     (funcall gnus-group-prepare-function level unread lowest)
4322     (if (zerop (buffer-size))
4323         (gnus-message 5 gnus-no-groups-message)
4324       (goto-char (point-max))
4325       (when (or (not gnus-group-goto-next-group-function)
4326                 (not (funcall gnus-group-goto-next-group-function 
4327                               group props)))
4328         (if (not group)
4329             ;; Go to the first group with unread articles.
4330             (gnus-group-search-forward t)
4331           ;; Find the right group to put point on.  If the current group
4332           ;; has disappeared in the new listing, try to find the next
4333           ;; one.        If no next one can be found, just leave point at the
4334           ;; first newsgroup in the buffer.
4335           (if (not (gnus-goto-char
4336                     (text-property-any
4337                      (point-min) (point-max)
4338                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4339               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4340                 (while (and newsrc
4341                             (not (gnus-goto-char
4342                                   (text-property-any
4343                                    (point-min) (point-max) 'gnus-group
4344                                    (gnus-intern-safe
4345                                     (caar newsrc) gnus-active-hashtb)))))
4346                   (setq newsrc (cdr newsrc)))
4347                 (or newsrc (progn (goto-char (point-max))
4348                                   (forward-line -1)))))))
4349       ;; Adjust cursor point.
4350       (gnus-group-position-point))))
4351
4352 (defun gnus-group-list-level (level &optional all)
4353   "List groups on LEVEL.
4354 If ALL (the prefix), also list groups that have no unread articles."
4355   (interactive "nList groups on level: \nP")
4356   (gnus-group-list-groups level all level))
4357
4358 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4359   "List all newsgroups with unread articles of level LEVEL or lower.
4360 If ALL is non-nil, list groups that have no unread articles.
4361 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4362 If REGEXP, only list groups matching REGEXP."
4363   (set-buffer gnus-group-buffer)
4364   (let ((buffer-read-only nil)
4365         (newsrc (cdr gnus-newsrc-alist))
4366         (lowest (or lowest 1))
4367         info clevel unread group params)
4368     (erase-buffer)
4369     (if (< lowest gnus-level-zombie)
4370         ;; List living groups.
4371         (while newsrc
4372           (setq info (car newsrc)
4373                 group (gnus-info-group info)
4374                 params (gnus-info-params info)
4375                 newsrc (cdr newsrc)
4376                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4377           (and unread                   ; This group might be bogus
4378                (or (not regexp)
4379                    (string-match regexp group))
4380                (<= (setq clevel (gnus-info-level info)) level)
4381                (>= clevel lowest)
4382                (or all                  ; We list all groups?
4383                    (and gnus-group-list-inactive-groups
4384                         (eq unread t))  ; We list unactivated groups
4385                    (> unread 0)         ; We list groups with unread articles
4386                    (and gnus-list-groups-with-ticked-articles
4387                         (cdr (assq 'tick (gnus-info-marks info))))
4388                                         ; And groups with tickeds
4389                    ;; Check for permanent visibility.
4390                    (and gnus-permanently-visible-groups
4391                         (string-match gnus-permanently-visible-groups
4392                                       group))
4393                    (memq 'visible params)
4394                    (cdr (assq 'visible params)))
4395                (gnus-group-insert-group-line
4396                 group (gnus-info-level info)
4397                 (gnus-info-marks info) unread (gnus-info-method info)))))
4398
4399     ;; List dead groups.
4400     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4401          (gnus-group-prepare-flat-list-dead
4402           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4403           gnus-level-zombie ?Z
4404           regexp))
4405     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4406          (gnus-group-prepare-flat-list-dead
4407           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4408           gnus-level-killed ?K regexp))
4409
4410     (gnus-group-set-mode-line)
4411     (setq gnus-group-list-mode (cons level all))
4412     (run-hooks 'gnus-group-prepare-hook)))
4413
4414 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4415   ;; List zombies and killed lists somewhat faster, which was
4416   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4417   ;; this by ignoring the group format specification altogether.
4418   (let (group beg)
4419     (if regexp
4420         ;; This loop is used when listing groups that match some
4421         ;; regexp.
4422         (while groups
4423           (setq group (pop groups))
4424           (when (string-match regexp group)
4425             (add-text-properties
4426              (point) (prog1 (1+ (point))
4427                        (insert " " mark "     *: " group "\n"))
4428              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4429                    'gnus-unread t
4430                    'gnus-level level))))
4431       ;; This loop is used when listing all groups.
4432       (while groups
4433         (add-text-properties
4434          (point) (prog1 (1+ (point))
4435                    (insert " " mark "     *: "
4436                            (setq group (pop groups)) "\n"))
4437          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4438                'gnus-unread t
4439                'gnus-level level))))))
4440
4441 (defmacro gnus-group-real-name (group)
4442   "Find the real name of a foreign newsgroup."
4443   `(let ((gname ,group))
4444      (if (string-match ":[^:]+$" gname)
4445          (substring gname (1+ (match-beginning 0)))
4446        gname)))
4447
4448 (defsubst gnus-server-add-address (method)
4449   (let ((method-name (symbol-name (car method))))
4450     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4451              (not (assq (intern (concat method-name "-address")) method)))
4452         (append method (list (list (intern (concat method-name "-address"))
4453                                    (nth 1 method))))
4454       method)))
4455
4456 (defsubst gnus-server-get-method (group method)
4457   ;; Input either a server name, and extended server name, or a
4458   ;; select method, and return a select method.
4459   (cond ((stringp method)
4460          (gnus-server-to-method method))
4461         ((and (stringp (car method)) group)
4462          (gnus-server-extend-method group method))
4463         (t
4464          (gnus-server-add-address method))))
4465
4466 (defun gnus-server-to-method (server)
4467   "Map virtual server names to select methods."
4468   (or 
4469    ;; Perhaps this is the native server?
4470    (and (equal server "native") gnus-select-method)
4471    ;; It should be in the server alist.
4472    (cdr (assoc server gnus-server-alist))
4473    ;; If not, we look through all the opened server
4474    ;; to see whether we can find it there.
4475    (let ((opened gnus-opened-servers))
4476      (while (and opened
4477                  (not (equal server (format "%s:%s" (caaar opened)
4478                                             (cadaar opened)))))
4479        (pop opened))
4480      (caar opened))))
4481
4482 (defmacro gnus-server-equal (ss1 ss2)
4483   "Say whether two servers are equal."
4484   `(let ((s1 ,ss1)
4485          (s2 ,ss2))
4486      (or (equal s1 s2)
4487          (and (= (length s1) (length s2))
4488               (progn
4489                 (while (and s1 (member (car s1) s2))
4490                   (setq s1 (cdr s1)))
4491                 (null s1))))))
4492
4493 (defun gnus-group-prefixed-name (group method)
4494   "Return the whole name from GROUP and METHOD."
4495   (and (stringp method) (setq method (gnus-server-to-method method)))
4496   (concat (format "%s" (car method))
4497           (if (and
4498                (or (assoc (format "%s" (car method)) 
4499                           (gnus-methods-using 'address))
4500                    (equal method gnus-message-archive-method))
4501                (not (string= (nth 1 method) "")))
4502               (concat "+" (nth 1 method)))
4503           ":" group))
4504
4505 (defun gnus-group-real-prefix (group)
4506   "Return the prefix of the current group name."
4507   (if (string-match "^[^:]+:" group)
4508       (substring group 0 (match-end 0))
4509     ""))
4510
4511 (defun gnus-group-method (group)
4512   "Return the server or method used for selecting GROUP."
4513   (let ((prefix (gnus-group-real-prefix group)))
4514     (if (equal prefix "")
4515         gnus-select-method
4516       (let ((servers gnus-opened-servers)
4517             (server "")
4518             backend possible found)
4519         (if (string-match "^[^\\+]+\\+" prefix)
4520             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4521                   server (substring prefix (match-end 0) (1- (length prefix))))
4522           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4523         (while servers
4524           (when (eq (caaar servers) backend)
4525             (setq possible (caar servers))
4526             (when (equal (cadaar servers) server)
4527               (setq found (caar servers))))
4528           (pop servers))
4529         (or (car (rassoc found gnus-server-alist))
4530             found
4531             (car (rassoc possible gnus-server-alist))
4532             possible
4533             (list backend server))))))
4534
4535 (defsubst gnus-secondary-method-p (method)
4536   "Return whether METHOD is a secondary select method."
4537   (let ((methods gnus-secondary-select-methods)
4538         (gmethod (gnus-server-get-method nil method)))
4539     (while (and methods
4540                 (not (equal (gnus-server-get-method nil (car methods))
4541                             gmethod)))
4542       (setq methods (cdr methods)))
4543     methods))
4544
4545 (defun gnus-group-foreign-p (group)
4546   "Say whether a group is foreign or not."
4547   (and (not (gnus-group-native-p group))
4548        (not (gnus-group-secondary-p group))))
4549
4550 (defun gnus-group-native-p (group)
4551   "Say whether the group is native or not."
4552   (not (string-match ":" group)))
4553
4554 (defun gnus-group-secondary-p (group)
4555   "Say whether the group is secondary or not."
4556   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4557
4558 (defun gnus-group-get-parameter (group &optional symbol)
4559   "Returns the group parameters for GROUP.
4560 If SYMBOL, return the value of that symbol in the group parameters."
4561   (let ((params (gnus-info-params (gnus-get-info group))))
4562     (if symbol
4563         (gnus-group-parameter-value params symbol)
4564       params)))
4565
4566 (defun gnus-group-parameter-value (params symbol)
4567   "Return the value of SYMBOL in group PARAMS."
4568   (or (car (memq symbol params))        ; It's either a simple symbol
4569       (cdr (assq symbol params))))      ; or a cons.
4570
4571 (defun gnus-group-add-parameter (group param)
4572   "Add parameter PARAM to GROUP."
4573   (let ((info (gnus-get-info group)))
4574     (if (not info)
4575         () ; This is a dead group.  We just ignore it.
4576       ;; Cons the new param to the old one and update.
4577       (gnus-group-set-info (cons param (gnus-info-params info))
4578                            group 'params))))
4579
4580 (defun gnus-group-set-parameter (group name value)
4581   "Set parameter NAME to VALUE in GROUP."
4582   (let ((info (gnus-get-info group)))
4583     (if (not info)
4584         () ; This is a dead group.  We just ignore it.
4585       (let ((old-params (gnus-info-params info))
4586             (new-params (list (cons name value))))
4587         (while old-params
4588           (if (or (not (listp (car old-params)))
4589                   (not (eq (caar old-params) name)))
4590               (setq new-params (append new-params (list (car old-params)))))
4591           (setq old-params (cdr old-params)))
4592         (gnus-group-set-info new-params group 'params)))))
4593
4594 (defun gnus-group-add-score (group &optional score)
4595   "Add SCORE to the GROUP score.
4596 If SCORE is nil, add 1 to the score of GROUP."
4597   (let ((info (gnus-get-info group)))
4598     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4599
4600 (defun gnus-summary-bubble-group ()
4601   "Increase the score of the current group.
4602 This is a handy function to add to `gnus-summary-exit-hook' to
4603 increase the score of each group you read."
4604   (gnus-group-add-score gnus-newsgroup-name))
4605
4606 (defun gnus-group-set-info (info &optional method-only-group part)
4607   (let* ((entry (gnus-gethash
4608                  (or method-only-group (gnus-info-group info))
4609                  gnus-newsrc-hashtb))
4610          (part-info info)
4611          (info (if method-only-group (nth 2 entry) info)))
4612     (when method-only-group
4613       (unless entry
4614         (error "Trying to change non-existent group %s" method-only-group))
4615       ;; We have received parts of the actual group info - either the
4616       ;; select method or the group parameters.  We first check
4617       ;; whether we have to extend the info, and if so, do that.
4618       (let ((len (length info))
4619             (total (if (eq part 'method) 5 6)))
4620         (when (< len total)
4621           (setcdr (nthcdr (1- len) info)
4622                   (make-list (- total len) nil)))
4623         ;; Then we enter the new info.
4624         (setcar (nthcdr (1- total) info) part-info)))
4625     (unless entry
4626       ;; This is a new group, so we just create it.
4627       (save-excursion
4628         (set-buffer gnus-group-buffer)
4629         (if (gnus-info-method info)
4630             ;; It's a foreign group...
4631             (gnus-group-make-group
4632              (gnus-group-real-name (gnus-info-group info))
4633              (prin1-to-string (car (gnus-info-method info)))
4634              (nth 1 (gnus-info-method info)))
4635           ;; It's a native group.
4636           (gnus-group-make-group (gnus-info-group info)))
4637         (gnus-message 6 "Note: New group created")
4638         (setq entry
4639               (gnus-gethash (gnus-group-prefixed-name
4640                              (gnus-group-real-name (gnus-info-group info))
4641                              (or (gnus-info-method info) gnus-select-method))
4642                             gnus-newsrc-hashtb))))
4643     ;; Whether it was a new group or not, we now have the entry, so we
4644     ;; can do the update.
4645     (if entry
4646         (progn
4647           (setcar (nthcdr 2 entry) info)
4648           (when (and (not (eq (car entry) t))
4649                      (gnus-active (gnus-info-group info)))
4650             (let ((marked (gnus-info-marks info)))
4651               (setcar entry (length (gnus-list-of-unread-articles
4652                                      (car info)))))))
4653       (error "No such group: %s" (gnus-info-group info)))))
4654
4655 (defun gnus-group-set-method-info (group select-method)
4656   (gnus-group-set-info select-method group 'method))
4657
4658 (defun gnus-group-set-params-info (group params)
4659   (gnus-group-set-info params group 'params))
4660
4661 (defun gnus-group-update-group-line ()
4662   "Update the current line in the group buffer."
4663   (let* ((buffer-read-only nil)
4664          (group (gnus-group-group-name))
4665          (gnus-group-indentation (gnus-group-group-indentation))
4666          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4667     (and entry
4668          (not (gnus-ephemeral-group-p group))
4669          (gnus-dribble-enter
4670           (concat "(gnus-group-set-info '"
4671                   (prin1-to-string (nth 2 entry)) ")")))
4672     (gnus-delete-line)
4673     (gnus-group-insert-group-line-info group)
4674     (forward-line -1)
4675     (gnus-group-position-point)))
4676
4677 (defun gnus-group-insert-group-line-info (group)
4678   "Insert GROUP on the current line."
4679   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4680         active info)
4681     (if entry
4682         (progn
4683           ;; (Un)subscribed group.
4684           (setq info (nth 2 entry))
4685           (gnus-group-insert-group-line
4686            group (gnus-info-level info) (gnus-info-marks info)
4687            (or (car entry) t) (gnus-info-method info)))
4688       ;; This group is dead.
4689       (gnus-group-insert-group-line
4690        group
4691        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4692        nil
4693        (if (setq active (gnus-active group))
4694            (- (1+ (cdr active)) (car active)) 0)
4695        nil))))
4696
4697 (defun gnus-group-insert-group-line
4698   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4699                   gnus-tmp-method)
4700   "Insert a group line in the group buffer."
4701   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4702          (gnus-tmp-number-total
4703           (if gnus-tmp-active
4704               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4705             0))
4706          (gnus-tmp-number-of-unread
4707           (if (numberp number) (int-to-string (max 0 number))
4708             "*"))
4709          (gnus-tmp-number-of-read
4710           (if (numberp number)
4711               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4712             "*"))
4713          (gnus-tmp-subscribed
4714           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4715                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4716                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4717                 (t ?K)))
4718          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4719          (gnus-tmp-newsgroup-description
4720           (if gnus-description-hashtb
4721               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4722             ""))
4723          (gnus-tmp-moderated
4724           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4725          (gnus-tmp-moderated-string
4726           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4727          (gnus-tmp-method
4728           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4729          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4730          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4731          (gnus-tmp-news-method-string
4732           (if gnus-tmp-method
4733               (format "(%s:%s)" (car gnus-tmp-method)
4734                       (cadr gnus-tmp-method)) ""))
4735          (gnus-tmp-marked-mark
4736           (if (and (numberp number)
4737                    (zerop number)
4738                    (cdr (assq 'tick gnus-tmp-marked)))
4739               ?* ? ))
4740          (gnus-tmp-process-marked
4741           (if (member gnus-tmp-group gnus-group-marked)
4742               gnus-process-mark ? ))
4743          (buffer-read-only nil)
4744          header gnus-tmp-header)                        ; passed as parameter to user-funcs.
4745     (beginning-of-line)
4746     (add-text-properties
4747      (point)
4748      (prog1 (1+ (point))
4749        ;; Insert the text.
4750        (eval gnus-group-line-format-spec))
4751      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4752        gnus-unread ,(if (numberp number)
4753                         (string-to-int gnus-tmp-number-of-unread)
4754                       t)
4755        gnus-marked ,gnus-tmp-marked-mark
4756        gnus-indentation ,gnus-group-indentation
4757        gnus-level ,gnus-tmp-level))
4758     (when (gnus-visual-p 'group-highlight 'highlight)
4759       (forward-line -1)
4760       (run-hooks 'gnus-group-update-hook)
4761       (forward-line))
4762     ;; Allow XEmacs to remove front-sticky text properties.
4763     (gnus-group-remove-excess-properties)))
4764
4765 (defun gnus-group-update-group (group &optional visible-only)
4766   "Update all lines where GROUP appear.
4767 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4768 already."
4769   (save-excursion
4770     (set-buffer gnus-group-buffer)
4771     ;; The buffer may be narrowed.
4772     (save-restriction
4773       (widen)
4774       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4775             (loc (point-min))
4776             found buffer-read-only visible)
4777         ;; Enter the current status into the dribble buffer.
4778         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4779           (if (and entry (not (gnus-ephemeral-group-p group)))
4780               (gnus-dribble-enter
4781                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4782                        ")"))))
4783         ;; Find all group instances.  If topics are in use, each group
4784         ;; may be listed in more than once.
4785         (while (setq loc (text-property-any
4786                           loc (point-max) 'gnus-group ident))
4787           (setq found t)
4788           (goto-char loc)
4789           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4790             (gnus-delete-line)
4791             (gnus-group-insert-group-line-info group))
4792           (setq loc (1+ loc)))
4793         (unless (or found visible-only)
4794           ;; No such line in the buffer, find out where it's supposed to
4795           ;; go, and insert it there (or at the end of the buffer).
4796           (if gnus-goto-missing-group-function
4797               (funcall gnus-goto-missing-group-function group)
4798             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4799               (while (and entry (car entry)
4800                           (not
4801                            (gnus-goto-char
4802                             (text-property-any
4803                              (point-min) (point-max)
4804                              'gnus-group (gnus-intern-safe
4805                                           (caar entry) gnus-active-hashtb)))))
4806                 (setq entry (cdr entry)))
4807               (or entry (goto-char (point-max)))))
4808           ;; Finally insert the line.
4809           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4810             (gnus-group-insert-group-line-info group)))
4811         (gnus-group-set-mode-line)))))
4812
4813 (defun gnus-group-set-mode-line ()
4814   (when (memq 'group gnus-updated-mode-lines)
4815     (let* ((gformat (or gnus-group-mode-line-format-spec
4816                         (setq gnus-group-mode-line-format-spec
4817                               (gnus-parse-format
4818                                gnus-group-mode-line-format
4819                                gnus-group-mode-line-format-alist))))
4820            (gnus-tmp-news-server (car (cdr gnus-select-method)))
4821            (gnus-tmp-news-method (car gnus-select-method))
4822            (max-len 60)
4823            gnus-tmp-header                      ;Dummy binding for user-defined formats
4824            ;; Get the resulting string.
4825            (mode-string (eval gformat)))
4826       ;; If the line is too long, we chop it off.
4827       (when (> (length mode-string) max-len)
4828         (setq mode-string (substring mode-string 0 (- max-len 4))))
4829       (prog1
4830           (setq mode-line-buffer-identification (list mode-string))
4831         (set-buffer-modified-p t)))))
4832
4833 (defun gnus-group-group-name ()
4834   "Get the name of the newsgroup on the current line."
4835   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4836     (and group (symbol-name group))))
4837
4838 (defun gnus-group-group-level ()
4839   "Get the level of the newsgroup on the current line."
4840   (get-text-property (gnus-point-at-bol) 'gnus-level))
4841
4842 (defun gnus-group-group-indentation ()
4843   "Get the indentation of the newsgroup on the current line."
4844   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation) ""))
4845
4846 (defun gnus-group-group-unread ()
4847   "Get the number of unread articles of the newsgroup on the current line."
4848   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4849
4850 (defun gnus-group-search-forward (&optional backward all level first-too)
4851   "Find the next newsgroup with unread articles.
4852 If BACKWARD is non-nil, find the previous newsgroup instead.
4853 If ALL is non-nil, just find any newsgroup.
4854 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4855 group exists.
4856 If FIRST-TOO, the current line is also eligible as a target."
4857   (let ((way (if backward -1 1))
4858         (low gnus-level-killed)
4859         (beg (point))
4860         pos found lev)
4861     (if (and backward (progn (beginning-of-line)) (bobp))
4862         nil
4863       (or first-too (forward-line way))
4864       (while (and
4865               (not (eobp))
4866               (not (setq
4867                     found
4868                     (and (or all
4869                              (and
4870                               (let ((unread
4871                                      (get-text-property (point) 'gnus-unread)))
4872                                 (and (numberp unread) (> unread 0)))
4873                               (setq lev (get-text-property (point)
4874                                                            'gnus-level))
4875                               (<= lev gnus-level-subscribed)))
4876                          (or (not level)
4877                              (and (setq lev (get-text-property (point)
4878                                                                'gnus-level))
4879                                   (or (= lev level)
4880                                       (and (< lev low)
4881                                            (< level lev)
4882                                            (progn
4883                                              (setq low lev)
4884                                              (setq pos (point))
4885                                              nil))))))))
4886               (zerop (forward-line way)))))
4887     (if found
4888         (progn (gnus-group-position-point) t)
4889       (goto-char (or pos beg))
4890       (and pos t))))
4891
4892 ;;; Gnus group mode commands
4893
4894 ;; Group marking.
4895
4896 (defun gnus-group-mark-group (n &optional unmark no-advance)
4897   "Mark the current group."
4898   (interactive "p")
4899   (let ((buffer-read-only nil)
4900         group)
4901     (while
4902         (and (> n 0)
4903              (setq group (gnus-group-group-name))
4904              (progn
4905                (beginning-of-line)
4906                (forward-char
4907                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4908                (delete-char 1)
4909                (if unmark
4910                    (progn
4911                      (insert " ")
4912                      (setq gnus-group-marked (delete group gnus-group-marked)))
4913                  (insert "#")
4914                  (setq gnus-group-marked
4915                        (cons group (delete group gnus-group-marked))))
4916                t)
4917              (or no-advance (zerop (gnus-group-next-group 1))))
4918       (setq n (1- n)))
4919     (gnus-summary-position-point)
4920     n))
4921
4922 (defun gnus-group-unmark-group (n)
4923   "Remove the mark from the current group."
4924   (interactive "p")
4925   (gnus-group-mark-group n 'unmark)
4926   (gnus-group-position-point))
4927
4928 (defun gnus-group-unmark-all-groups ()
4929   "Unmark all groups."
4930   (let ((groups gnus-group-marked))
4931     (save-excursion
4932       (while groups
4933         (gnus-group-remove-mark (pop groups)))))
4934   (gnus-group-position-point))
4935
4936 (defun gnus-group-mark-region (unmark beg end)
4937   "Mark all groups between point and mark.
4938 If UNMARK, remove the mark instead."
4939   (interactive "P\nr")
4940   (let ((num (count-lines beg end)))
4941     (save-excursion
4942       (goto-char beg)
4943       (- num (gnus-group-mark-group num unmark)))))
4944
4945 (defun gnus-group-mark-buffer (&optional unmark)
4946   "Mark all groups in the buffer.
4947 If UNMARK, remove the mark instead."
4948   (interactive "P")
4949   (gnus-group-mark-region unmark (point-min) (point-max)))
4950
4951 (defun gnus-group-mark-regexp (regexp)
4952   "Mark all groups that match some regexp."
4953   (interactive "sMark (regexp): ")
4954   (let ((alist (cdr gnus-newsrc-alist))
4955         group)
4956     (while alist
4957       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
4958         (gnus-group-set-mark group))))
4959   (gnus-group-position-point))
4960
4961 (defun gnus-group-remove-mark (group)
4962   "Remove the process mark from GROUP and move point there.
4963 Return nil if the group isn't displayed."
4964   (if (gnus-group-goto-group group)
4965       (save-excursion
4966         (gnus-group-mark-group 1 'unmark t)
4967         t)
4968     (setq gnus-group-marked
4969           (delete group gnus-group-marked))
4970     nil))
4971
4972 (defun gnus-group-set-mark (group)
4973   "Set the process mark on GROUP."
4974   (if (gnus-group-goto-group group)
4975       (save-excursion
4976         (gnus-group-mark-group 1 nil t))
4977     (setq gnus-group-marked
4978           (cons group (delete group gnus-group-marked)))))
4979
4980 (defun gnus-group-universal-argument (arg &optional groups func)
4981   "Perform any command on all groups accoring to the process/prefix convention."
4982   (interactive "P")
4983   (let ((groups (or groups (gnus-group-process-prefix arg)))
4984         group func)
4985     (if (eq (setq func (or func
4986                            (key-binding
4987                             (read-key-sequence
4988                              (substitute-command-keys
4989                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
4990             'undefined)
4991         (progn
4992           (message "Undefined key")
4993           (ding))
4994       (while groups
4995         (gnus-group-remove-mark (setq group (pop groups)))
4996         (command-execute func))))
4997   (gnus-group-position-point))
4998
4999 (defun gnus-group-process-prefix (n)
5000   "Return a list of groups to work on.
5001 Take into consideration N (the prefix) and the list of marked groups."
5002   (cond
5003    (n
5004     (setq n (prefix-numeric-value n))
5005     ;; There is a prefix, so we return a list of the N next
5006     ;; groups.
5007     (let ((way (if (< n 0) -1 1))
5008           (n (abs n))
5009           group groups)
5010       (save-excursion
5011         (while (and (> n 0)
5012                     (setq group (gnus-group-group-name)))
5013           (setq groups (cons group groups))
5014           (setq n (1- n))
5015           (gnus-group-next-group way)))
5016       (nreverse groups)))
5017    ((and (boundp 'transient-mark-mode)
5018          transient-mark-mode
5019          mark-active)
5020     ;; Work on the region between point and mark.
5021     (let ((max (max (point) (mark)))
5022           groups)
5023       (save-excursion
5024         (goto-char (min (point) (mark)))
5025         (while
5026             (and
5027              (push (gnus-group-group-name) groups)
5028              (zerop (gnus-group-next-group 1))
5029              (< (point) max)))
5030         (nreverse groups))))
5031    (gnus-group-marked
5032     ;; No prefix, but a list of marked articles.
5033     (reverse gnus-group-marked))
5034    (t
5035     ;; Neither marked articles or a prefix, so we return the
5036     ;; current group.
5037     (let ((group (gnus-group-group-name)))
5038       (and group (list group))))))
5039
5040 ;; Selecting groups.
5041
5042 (defun gnus-group-read-group (&optional all no-article group)
5043   "Read news in this newsgroup.
5044 If the prefix argument ALL is non-nil, already read articles become
5045 readable.  IF ALL is a number, fetch this number of articles.  If the
5046 optional argument NO-ARTICLE is non-nil, no article will be
5047 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5048 group."
5049   (interactive "P")
5050   (let ((group (or group (gnus-group-group-name)))
5051         number active marked entry)
5052     (or group (error "No group on current line"))
5053     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5054                                             group gnus-newsrc-hashtb)))))
5055     ;; This group might be a dead group.  In that case we have to get
5056     ;; the number of unread articles from `gnus-active-hashtb'.
5057     (setq number
5058           (cond ((numberp all) all)
5059                 (entry (car entry))
5060                 ((setq active (gnus-active group))
5061                  (- (1+ (cdr active)) (car active)))))
5062     (gnus-summary-read-group
5063      group (or all (and (numberp number)
5064                         (zerop (+ number (length (cdr (assq 'tick marked)))
5065                                   (length (cdr (assq 'dormant marked)))))))
5066      no-article)))
5067
5068 (defun gnus-group-select-group (&optional all)
5069   "Select this newsgroup.
5070 No article is selected automatically.
5071 If ALL is non-nil, already read articles become readable.
5072 If ALL is a number, fetch this number of articles."
5073   (interactive "P")
5074   (gnus-group-read-group all t))
5075
5076 (defun gnus-group-quick-select-group (&optional all)
5077   "Select the current group \"quickly\".
5078 This means that no highlighting or scoring will be performed."
5079   (interactive "P")
5080   (let (gnus-visual
5081         gnus-score-find-score-files-function
5082         gnus-apply-kill-hook
5083         gnus-summary-expunge-below)
5084     (gnus-group-read-group all t)))
5085
5086 (defun gnus-group-visible-select-group (&optional all)
5087   "Select the current group without hiding any articles."
5088   (interactive "P")
5089   (let ((gnus-inhibit-limiting t))
5090     (gnus-group-read-group all t)))
5091
5092 ;;;###autoload
5093 (defun gnus-fetch-group (group)
5094   "Start Gnus if necessary and enter GROUP.
5095 Returns whether the fetching was successful or not."
5096   (interactive "sGroup name: ")
5097   (or (get-buffer gnus-group-buffer)
5098       (gnus))
5099   (gnus-group-select-group))
5100
5101 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5102 ;; if selection was successful.
5103 (defun gnus-group-read-ephemeral-group
5104   (group method &optional activate quit-config)
5105   (let ((group (if (gnus-group-foreign-p group) group
5106                  (gnus-group-prefixed-name group method))))
5107     (gnus-sethash
5108      group
5109      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5110                      ((quit-config . ,(if quit-config quit-config
5111                                         (cons (current-buffer) 'summary))))))
5112      gnus-newsrc-hashtb)
5113     (set-buffer gnus-group-buffer)
5114     (or (gnus-check-server method)
5115         (error "Unable to contact server: %s" (gnus-status-message method)))
5116     (if activate (or (gnus-request-group group)
5117                      (error "Couldn't request group")))
5118     (condition-case ()
5119         (gnus-group-read-group t t group)
5120       (error nil)
5121       (quit nil))))
5122
5123 (defun gnus-group-jump-to-group (group)
5124   "Jump to newsgroup GROUP."
5125   (interactive
5126    (list (completing-read
5127           "Group: " gnus-active-hashtb nil
5128           (memq gnus-select-method gnus-have-read-active-file))))
5129
5130   (if (equal group "")
5131       (error "Empty group name"))
5132
5133   (let ((b (text-property-any
5134             (point-min) (point-max)
5135             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5136     (unless (gnus-ephemeral-group-p group)
5137       (if b
5138           ;; Either go to the line in the group buffer...
5139           (goto-char b)
5140         ;; ... or insert the line.
5141         (or
5142          (gnus-active group)
5143          (gnus-activate-group group)
5144          (error "%s error: %s" group (gnus-status-message group)))
5145
5146         (gnus-group-update-group group)
5147         (goto-char (text-property-any
5148                     (point-min) (point-max)
5149                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5150     ;; Adjust cursor point.
5151     (gnus-group-position-point)))
5152
5153 (defun gnus-group-goto-group (group)
5154   "Goto to newsgroup GROUP."
5155   (when group
5156     (let ((b (text-property-any (point-min) (point-max)
5157                                 'gnus-group (gnus-intern-safe
5158                                              group gnus-active-hashtb))))
5159       (and b (goto-char b)))))
5160
5161 (defun gnus-group-next-group (n)
5162   "Go to next N'th newsgroup.
5163 If N is negative, search backward instead.
5164 Returns the difference between N and the number of skips actually
5165 done."
5166   (interactive "p")
5167   (gnus-group-next-unread-group n t))
5168
5169 (defun gnus-group-next-unread-group (n &optional all level)
5170   "Go to next N'th unread newsgroup.
5171 If N is negative, search backward instead.
5172 If ALL is non-nil, choose any newsgroup, unread or not.
5173 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5174 such group can be found, the next group with a level higher than
5175 LEVEL.
5176 Returns the difference between N and the number of skips actually
5177 made."
5178   (interactive "p")
5179   (let ((backward (< n 0))
5180         (n (abs n)))
5181     (while (and (> n 0)
5182                 (gnus-group-search-forward
5183                  backward (or (not gnus-group-goto-unread) all) level))
5184       (setq n (1- n)))
5185     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5186                                (if level " on this level or higher" "")))
5187     n))
5188
5189 (defun gnus-group-prev-group (n)
5190   "Go to previous N'th newsgroup.
5191 Returns the difference between N and the number of skips actually
5192 done."
5193   (interactive "p")
5194   (gnus-group-next-unread-group (- n) t))
5195
5196 (defun gnus-group-prev-unread-group (n)
5197   "Go to previous N'th unread newsgroup.
5198 Returns the difference between N and the number of skips actually
5199 done."
5200   (interactive "p")
5201   (gnus-group-next-unread-group (- n)))
5202
5203 (defun gnus-group-next-unread-group-same-level (n)
5204   "Go to next N'th unread newsgroup on the same level.
5205 If N is negative, search backward instead.
5206 Returns the difference between N and the number of skips actually
5207 done."
5208   (interactive "p")
5209   (gnus-group-next-unread-group n t (gnus-group-group-level))
5210   (gnus-group-position-point))
5211
5212 (defun gnus-group-prev-unread-group-same-level (n)
5213   "Go to next N'th unread newsgroup on the same level.
5214 Returns the difference between N and the number of skips actually
5215 done."
5216   (interactive "p")
5217   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5218   (gnus-group-position-point))
5219
5220 (defun gnus-group-best-unread-group (&optional exclude-group)
5221   "Go to the group with the highest level.
5222 If EXCLUDE-GROUP, do not go to that group."
5223   (interactive)
5224   (goto-char (point-min))
5225   (let ((best 100000)
5226         unread best-point)
5227     (while (setq unread (get-text-property (point) 'gnus-unread))
5228       (if (and (numberp unread) (> unread 0))
5229           (progn
5230             (if (and (< (get-text-property (point) 'gnus-level) best)
5231                      (or (not exclude-group)
5232                          (not (equal exclude-group (gnus-group-group-name)))))
5233                 (progn
5234                   (setq best (get-text-property (point) 'gnus-level))
5235                   (setq best-point (point))))))
5236       (forward-line 1))
5237     (if best-point (goto-char best-point))
5238     (gnus-summary-position-point)
5239     (and best-point (gnus-group-group-name))))
5240
5241 (defun gnus-group-first-unread-group ()
5242   "Go to the first group with unread articles."
5243   (interactive)
5244   (prog1
5245       (let ((opoint (point))
5246             unread)
5247         (goto-char (point-min))
5248         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5249                 (and (numberp unread)   ; Not a topic.
5250                      (not (zerop unread))) ; Has unread articles.
5251                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5252             (point)                     ; Success.
5253           (goto-char opoint)
5254           nil))                         ; Not success.
5255     (gnus-group-position-point)))
5256
5257 (defun gnus-group-enter-server-mode ()
5258   "Jump to the server buffer."
5259   (interactive)
5260   (gnus-enter-server-buffer))
5261
5262 (defun gnus-group-make-group (name &optional method address)
5263   "Add a new newsgroup.
5264 The user will be prompted for a NAME, for a select METHOD, and an
5265 ADDRESS."
5266   (interactive
5267    (cons
5268     (read-string "Group name: ")
5269     (let ((method
5270            (completing-read
5271             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5272             nil t)))
5273       (if (assoc method gnus-valid-select-methods)
5274           (list method
5275                 (if (memq 'prompt-address
5276                           (assoc method gnus-valid-select-methods))
5277                     (read-string "Address: ")
5278                   ""))
5279         (list method "")))))
5280
5281   (save-excursion
5282     (set-buffer gnus-group-buffer)
5283     (let* ((meth (and method (if address (list (intern method) address)
5284                                method)))
5285            (nname (if method (gnus-group-prefixed-name name meth) name))
5286            info)
5287       (and (gnus-gethash nname gnus-newsrc-hashtb)
5288            (error "Group %s already exists" nname))
5289       (gnus-group-change-level
5290        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5291        gnus-level-default-subscribed gnus-level-killed
5292        (and (gnus-group-group-name)
5293             (gnus-gethash (gnus-group-group-name)
5294                           gnus-newsrc-hashtb))
5295        t)
5296       (gnus-set-active nname (cons 1 0))
5297       (or (gnus-ephemeral-group-p name)
5298           (gnus-dribble-enter
5299            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5300       (gnus-group-insert-group-line-info nname)
5301
5302       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5303         (require (car meth)))
5304       (gnus-check-server meth)
5305       (and (gnus-check-backend-function 'request-create-group nname)
5306            (gnus-request-create-group nname))
5307       t)))
5308
5309 (defun gnus-group-delete-group (group &optional force)
5310   "Delete the current group.
5311 If FORCE (the prefix) is non-nil, all the articles in the group will
5312 be deleted.  This is \"deleted\" as in \"removed forever from the face
5313 of the Earth\".  There is no undo."
5314   (interactive
5315    (list (gnus-group-group-name)
5316          current-prefix-arg))
5317   (or group (error "No group to rename"))
5318   (or (gnus-check-backend-function 'request-delete-group group)
5319       (error "This backend does not support group deletion"))
5320   (prog1
5321       (if (not (gnus-yes-or-no-p
5322                 (format
5323                  "Do you really want to delete %s%s? "
5324                  group (if force " and all its contents" ""))))
5325           () ; Whew!
5326         (gnus-message 6 "Deleting group %s..." group)
5327         (if (not (gnus-request-delete-group group force))
5328             (progn
5329               (gnus-message 3 "Couldn't delete group %s" group)
5330               (ding))
5331           (gnus-message 6 "Deleting group %s...done" group)
5332           (gnus-group-goto-group group)
5333           (gnus-group-kill-group 1 t)
5334           t))
5335     (gnus-group-position-point)))
5336
5337 (defun gnus-group-rename-group (group new-name)
5338   (interactive
5339    (list
5340     (gnus-group-group-name)
5341     (progn
5342       (or (gnus-check-backend-function
5343            'request-rename-group (gnus-group-group-name))
5344           (error "This backend does not support renaming groups"))
5345       (read-string "New group name: "))))
5346
5347   (or (gnus-check-backend-function 'request-rename-group group)
5348       (error "This backend does not support renaming groups"))
5349
5350   (or group (error "No group to rename"))
5351   (and (string-match "^[ \t]*$" new-name)
5352        (error "Not a valid group name"))
5353
5354   ;; We find the proper prefixed name.
5355   (setq new-name
5356         (gnus-group-prefixed-name
5357          (gnus-group-real-name new-name)
5358          (gnus-info-method (gnus-get-info group))))
5359
5360   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5361   (prog1
5362       (if (not (gnus-request-rename-group group new-name))
5363           (progn
5364             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5365             (ding))
5366         ;; We rename the group internally by killing it...
5367         (gnus-group-goto-group group)
5368         (gnus-group-kill-group)
5369         ;; ... changing its name ...
5370         (setcar (cdr (car gnus-list-of-killed-groups))
5371                 new-name)
5372         ;; ... and then yanking it.  Magic!
5373         (gnus-group-yank-group)
5374         (gnus-set-active new-name (gnus-active group))
5375         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5376         new-name)
5377     (gnus-group-position-point)))
5378
5379 (defun gnus-group-edit-group (group &optional part)
5380   "Edit the group on the current line."
5381   (interactive (list (gnus-group-group-name)))
5382   (let ((done-func '(lambda ()
5383                       "Exit editing mode and update the information."
5384                       (interactive)
5385                       (gnus-group-edit-group-done 'part 'group)))
5386         (part (or part 'info))
5387         (winconf (current-window-configuration))
5388         info)
5389     (or group (error "No group on current line"))
5390     (or (setq info (gnus-get-info group))
5391         (error "Killed group; can't be edited"))
5392     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5393     (gnus-configure-windows 'edit-group)
5394     (gnus-add-current-to-buffer-list)
5395     (emacs-lisp-mode)
5396     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5397     (use-local-map (copy-keymap emacs-lisp-mode-map))
5398     (local-set-key "\C-c\C-c" done-func)
5399     (make-local-variable 'gnus-prev-winconf)
5400     (setq gnus-prev-winconf winconf)
5401     ;; We modify the func to let it know what part it is editing.
5402     (setcar (cdr (nth 4 done-func)) (list 'quote part))
5403     (setcar (cdr (cdr (nth 4 done-func))) group)
5404     (erase-buffer)
5405     (insert
5406      (cond
5407       ((eq part 'method)
5408        ";; Type `C-c C-c' after editing the select method.\n\n")
5409       ((eq part 'params)
5410        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5411       ((eq part 'info)
5412        ";; Type `C-c C-c' after editing the group info.\n\n")))
5413     (insert
5414      (pp-to-string
5415       (cond ((eq part 'method)
5416              (or (gnus-info-method info) "native"))
5417             ((eq part 'params)
5418              (gnus-info-params info))
5419             (t info)))
5420      "\n")))
5421
5422 (defun gnus-group-edit-group-method (group)
5423   "Edit the select method of GROUP."
5424   (interactive (list (gnus-group-group-name)))
5425   (gnus-group-edit-group group 'method))
5426
5427 (defun gnus-group-edit-group-parameters (group)
5428   "Edit the group parameters of GROUP."
5429   (interactive (list (gnus-group-group-name)))
5430   (gnus-group-edit-group group 'params))
5431
5432 (defun gnus-group-edit-group-done (part group)
5433   "Get info from buffer, update variables and jump to the group buffer."
5434   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5435   (goto-char (point-min))
5436   (let* ((form (read (current-buffer)))
5437          (winconf gnus-prev-winconf)
5438          (new-group (when (eq part 'info)
5439                       (if (or (not (nth 4 form))
5440                               (gnus-server-equal
5441                                gnus-select-method (nth 4 form)))
5442                           (gnus-group-real-name (car form))
5443                         (gnus-group-prefixed-name
5444                          (gnus-group-real-name (car form)) (nth 4 form))))))
5445     ;; Set the info.
5446     (if (eq part 'info)
5447         (progn
5448           (when new-group (setcar form new-group))
5449           (gnus-group-set-info form))
5450       (gnus-group-set-info form group part))
5451     (kill-buffer (current-buffer))
5452     (and winconf (set-window-configuration winconf))
5453     (set-buffer gnus-group-buffer)
5454     (when (and new-group
5455              (not (equal new-group group)))
5456       (when (gnus-group-goto-group group)
5457         (gnus-group-kill-group 1))
5458       (gnus-activate-group new-group))
5459     (gnus-group-update-group (or new-group group))
5460     (gnus-group-position-point)))
5461
5462 (defun gnus-group-make-help-group ()
5463   "Create the Gnus documentation group."
5464   (interactive)
5465   (let ((path load-path)
5466         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5467         file dir)
5468     (and (gnus-gethash name gnus-newsrc-hashtb)
5469          (error "Documentation group already exists"))
5470     (while path
5471       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5472             file nil)
5473       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5474                 (file-exists-p
5475                  (setq file (concat (file-name-directory
5476                                      (directory-file-name dir))
5477                                     "etc/gnus-tut.txt"))))
5478         (setq path nil)))
5479     (if (not file)
5480         (message "Couldn't find doc group")
5481       (gnus-group-make-group
5482        (gnus-group-real-name name)
5483        (list 'nndoc name
5484              (list 'nndoc-address file)
5485              (list 'nndoc-article-type 'mbox)))))
5486   (gnus-group-position-point))
5487
5488 (defun gnus-group-make-doc-group (file type)
5489   "Create a group that uses a single file as the source."
5490   (interactive
5491    (list (read-file-name "File name: ")
5492          (and current-prefix-arg 'ask)))
5493   (when (eq type 'ask)
5494     (let ((err "")
5495           char found)
5496       (while (not found)
5497         (message
5498          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5499          err)
5500         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5501                           ((= char ?b) 'babyl)
5502                           ((= char ?d) 'digest)
5503                           ((= char ?f) 'forward)
5504                           ((= char ?a) 'mmfd)
5505                           (t (setq err (format "%c unknown. " char))
5506                              nil))))
5507       (setq type found)))
5508   (let* ((file (expand-file-name file))
5509          (name (gnus-generate-new-group-name
5510                 (gnus-group-prefixed-name
5511                  (file-name-nondirectory file) '(nndoc "")))))
5512     (gnus-group-make-group
5513      (gnus-group-real-name name)
5514      (list 'nndoc name
5515            (list 'nndoc-address file)
5516            (list 'nndoc-article-type (or type 'guess))))
5517     (forward-line -1)
5518     (gnus-group-position-point)))
5519
5520 (defun gnus-group-make-archive-group (&optional all)
5521   "Create the (ding) Gnus archive group of the most recent articles.
5522 Given a prefix, create a full group."
5523   (interactive "P")
5524   (let ((group (gnus-group-prefixed-name
5525                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5526     (and (gnus-gethash group gnus-newsrc-hashtb)
5527          (error "Archive group already exists"))
5528     (gnus-group-make-group
5529      (gnus-group-real-name group)
5530      (list 'nndir (if all "hpc" "edu")
5531            (list 'nndir-directory
5532                  (if all gnus-group-archive-directory
5533                    gnus-group-recent-archive-directory)))))
5534   (forward-line -1)
5535   (gnus-group-position-point))
5536
5537 (defun gnus-group-make-directory-group (dir)
5538   "Create an nndir group.
5539 The user will be prompted for a directory.  The contents of this
5540 directory will be used as a newsgroup.  The directory should contain
5541 mail messages or news articles in files that have numeric names."
5542   (interactive
5543    (list (read-file-name "Create group from directory: ")))
5544   (or (file-exists-p dir) (error "No such directory"))
5545   (or (file-directory-p dir) (error "Not a directory"))
5546   (let ((ext "")
5547         (i 0)
5548         group)
5549     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5550       (setq group
5551             (gnus-group-prefixed-name
5552              (concat (file-name-as-directory (directory-file-name dir))
5553                      ext)
5554              '(nndir "")))
5555       (setq ext (format "<%d>" (setq i (1+ i)))))
5556     (gnus-group-make-group
5557      (gnus-group-real-name group)
5558      (list 'nndir group (list 'nndir-directory dir))))
5559   (forward-line -1)
5560   (gnus-group-position-point))
5561
5562 (defun gnus-group-make-kiboze-group (group address scores)
5563   "Create an nnkiboze group.
5564 The user will be prompted for a name, a regexp to match groups, and
5565 score file entries for articles to include in the group."
5566   (interactive
5567    (list
5568     (read-string "nnkiboze group name: ")
5569     (read-string "Source groups (regexp): ")
5570     (let ((headers (mapcar (lambda (group) (list group))
5571                            '("subject" "from" "number" "date" "message-id"
5572                              "references" "chars" "lines" "xref"
5573                              "followup" "all" "body" "head")))
5574           scores header regexp regexps)
5575       (while (not (equal "" (setq header (completing-read
5576                                           "Match on header: " headers nil t))))
5577         (setq regexps nil)
5578         (while (not (equal "" (setq regexp (read-string
5579                                             (format "Match on %s (string): "
5580                                                     header)))))
5581           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5582         (setq scores (cons (cons header regexps) scores)))
5583       scores)))
5584   (gnus-group-make-group group "nnkiboze" address)
5585   (save-excursion
5586     (gnus-set-work-buffer)
5587     (let (emacs-lisp-mode-hook)
5588       (pp scores (current-buffer)))
5589     (write-region (point-min) (point-max)
5590                   (gnus-score-file-name (concat "nnkiboze:" group))))
5591   (forward-line -1)
5592   (gnus-group-position-point))
5593
5594 (defun gnus-group-add-to-virtual (n vgroup)
5595   "Add the current group to a virtual group."
5596   (interactive
5597    (list current-prefix-arg
5598          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5599                           "nnvirtual:")))
5600   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5601       (error "%s is not an nnvirtual group" vgroup))
5602   (let* ((groups (gnus-group-process-prefix n))
5603          (method (gnus-info-method (gnus-get-info vgroup))))
5604     (setcar (cdr method)
5605             (concat
5606              (nth 1 method) "\\|"
5607              (mapconcat
5608               (lambda (s)
5609                 (gnus-group-remove-mark s)
5610                 (concat "\\(^" (regexp-quote s) "$\\)"))
5611               groups "\\|"))))
5612   (gnus-group-position-point))
5613
5614 (defun gnus-group-make-empty-virtual (group)
5615   "Create a new, fresh, empty virtual group."
5616   (interactive "sCreate new, empty virtual group: ")
5617   (let* ((method (list 'nnvirtual "^$"))
5618          (pgroup (gnus-group-prefixed-name group method)))
5619     ;; Check whether it exists already.
5620     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5621          (error "Group %s already exists." pgroup))
5622     ;; Subscribe the new group after the group on the current line.
5623     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5624     (gnus-group-update-group pgroup)
5625     (forward-line -1)
5626     (gnus-group-position-point)))
5627
5628 (defun gnus-group-enter-directory (dir)
5629   "Enter an ephemeral nneething group."
5630   (interactive "DDirectory to read: ")
5631   (let* ((method (list 'nneething dir))
5632          (leaf (gnus-group-prefixed-name
5633                 (file-name-nondirectory (directory-file-name dir))
5634                 method))
5635          (name (gnus-generate-new-group-name leaf)))
5636     (let ((nneething-read-only t))
5637       (or (gnus-group-read-ephemeral-group
5638            name method t
5639            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5640                                       'summary 'group)))
5641           (error "Couldn't enter %s" dir)))))
5642
5643 ;; Group sorting commands
5644 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5645
5646 (defun gnus-group-sort-groups (func &optional reverse)
5647   "Sort the group buffer according to FUNC.
5648 If REVERSE, reverse the sorting order."
5649   (interactive (list gnus-group-sort-function
5650                      current-prefix-arg))
5651   (let ((func (cond 
5652                ((not (listp func)) func)
5653                ((null func) func)
5654                ((= 1 (length func)) (car func))
5655                (t `(lambda (t1 t2)
5656                      ,(gnus-make-sort-function 
5657                        (reverse func)))))))
5658     ;; We peel off the dummy group from the alist.
5659     (when func
5660       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5661         (pop gnus-newsrc-alist))
5662       ;; Do the sorting.
5663       (setq gnus-newsrc-alist
5664             (sort gnus-newsrc-alist func))
5665       (when reverse
5666         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5667       ;; Regenerate the hash table.
5668       (gnus-make-hashtable-from-newsrc-alist)
5669       (gnus-group-list-groups))))
5670
5671 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5672   "Sort the group buffer alphabetically by group name.
5673 If REVERSE, sort in reverse order."
5674   (interactive "P")
5675   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5676
5677 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5678   "Sort the group buffer by number of unread articles.
5679 If REVERSE, sort in reverse order."
5680   (interactive "P")
5681   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5682
5683 (defun gnus-group-sort-groups-by-level (&optional reverse)
5684   "Sort the group buffer by group level.
5685 If REVERSE, sort in reverse order."
5686   (interactive "P")
5687   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5688
5689 (defun gnus-group-sort-groups-by-score (&optional reverse)
5690   "Sort the group buffer by group score.
5691 If REVERSE, sort in reverse order."
5692   (interactive "P")
5693   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5694
5695 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5696   "Sort the group buffer by group rank.
5697 If REVERSE, sort in reverse order."
5698   (interactive "P")
5699   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5700
5701 (defun gnus-group-sort-groups-by-method (&optional reverse)
5702   "Sort the group buffer alphabetically by backend name.
5703 If REVERSE, sort in reverse order."
5704   (interactive "P")
5705   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5706
5707 (defun gnus-group-sort-by-alphabet (info1 info2)
5708   "Sort alphabetically."
5709   (string< (gnus-info-group info1) (gnus-info-group info2)))
5710
5711 (defun gnus-group-sort-by-unread (info1 info2)
5712   "Sort by number of unread articles."
5713   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5714         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5715     (< (or (and (numberp n1) n1) 0)
5716        (or (and (numberp n2) n2) 0))))
5717
5718 (defun gnus-group-sort-by-level (info1 info2)
5719   "Sort by level."
5720   (< (gnus-info-level info1) (gnus-info-level info2)))
5721
5722 (defun gnus-group-sort-by-method (info1 info2)
5723   "Sort alphabetically by backend name."
5724   (string< (symbol-name (car (gnus-find-method-for-group
5725                               (gnus-info-group info1) info1)))
5726            (symbol-name (car (gnus-find-method-for-group
5727                               (gnus-info-group info2) info2)))))
5728
5729 (defun gnus-group-sort-by-score (info1 info2)
5730   "Sort by group score."
5731   (< (gnus-info-score info1) (gnus-info-score info2)))
5732
5733 (defun gnus-group-sort-by-rank (info1 info2)
5734   "Sort by level and score."
5735   (let ((level1 (gnus-info-level info1))
5736         (level2 (gnus-info-level info2)))
5737     (or (< level1 level2)
5738         (and (= level1 level2)
5739              (< (gnus-info-score info1) (gnus-info-score info2))))))
5740
5741 ;; Group catching up.
5742
5743 (defun gnus-group-catchup-current (&optional n all)
5744   "Mark all articles not marked as unread in current newsgroup as read.
5745 If prefix argument N is numeric, the ARG next newsgroups will be
5746 caught up.  If ALL is non-nil, marked articles will also be marked as
5747 read.  Cross references (Xref: header) of articles are ignored.
5748 The difference between N and actual number of newsgroups that were
5749 caught up is returned."
5750   (interactive "P")
5751   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5752                gnus-expert-user
5753                (gnus-y-or-n-p
5754                 (if all
5755                     "Do you really want to mark all articles as read? "
5756                   "Mark all unread articles as read? "))))
5757       n
5758     (let ((groups (gnus-group-process-prefix n))
5759           (ret 0))
5760       (while groups
5761         ;; Virtual groups have to be given special treatment.
5762         (let ((method (gnus-find-method-for-group (car groups))))
5763           (if (eq 'nnvirtual (car method))
5764               (nnvirtual-catchup-group
5765                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5766         (gnus-group-remove-mark (car groups))
5767         (if (prog1
5768                 (gnus-group-goto-group (car groups))
5769               (gnus-group-catchup (car groups) all))
5770             (gnus-group-update-group-line)
5771           (setq ret (1+ ret)))
5772         (setq groups (cdr groups)))
5773       (gnus-group-next-unread-group 1)
5774       ret)))
5775
5776 (defun gnus-group-catchup-current-all (&optional n)
5777   "Mark all articles in current newsgroup as read.
5778 Cross references (Xref: header) of articles are ignored."
5779   (interactive "P")
5780   (gnus-group-catchup-current n 'all))
5781
5782 (defun gnus-group-catchup (group &optional all)
5783   "Mark all articles in GROUP as read.
5784 If ALL is non-nil, all articles are marked as read.
5785 The return value is the number of articles that were marked as read,
5786 or nil if no action could be taken."
5787   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5788          (num (car entry)))
5789     ;; Do the updating only if the newsgroup isn't killed.
5790     (if (not (numberp (car entry)))
5791         (gnus-message 1 "Can't catch up; non-active group")
5792       ;; Do auto-expirable marks if that's required.
5793       (when (gnus-group-auto-expirable-p group)
5794         (gnus-add-marked-articles
5795          group 'expire (gnus-list-of-unread-articles group))
5796         (when all
5797           (let ((marks (nth 3 (nth 2 entry))))
5798             (gnus-add-marked-articles
5799              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5800             (gnus-add-marked-articles
5801              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5802       (when entry
5803         (gnus-update-read-articles group nil)
5804         ;; Also nix out the lists of marks and dormants.
5805         (when all
5806           (gnus-add-marked-articles group 'tick nil nil 'force)
5807           (gnus-add-marked-articles group 'dormant nil nil 'force))
5808         (run-hooks 'gnus-group-catchup-group-hook)
5809         num))))
5810
5811 (defun gnus-group-expire-articles (&optional n)
5812   "Expire all expirable articles in the current newsgroup."
5813   (interactive "P")
5814   (let ((groups (gnus-group-process-prefix n))
5815         group)
5816     (unless groups
5817       (error "No groups to expire"))
5818     (while (setq group (pop groups))
5819       (gnus-group-remove-mark group)
5820       (when (gnus-check-backend-function 'request-expire-articles group)
5821         (gnus-message 6 "Expiring articles in %s..." group)
5822         (let* ((info (gnus-get-info group))
5823                (expirable (if (gnus-group-total-expirable-p group)
5824                               (cons nil (gnus-list-of-read-articles group))
5825                             (assq 'expire (gnus-info-marks info))))
5826                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5827           (when expirable
5828             (setcdr
5829              expirable
5830              (gnus-compress-sequence
5831               (if expiry-wait
5832                   ;; We set the expiry variables to the groupp
5833                   ;; parameter. 
5834                   (let ((nnmail-expiry-wait-function nil)
5835                         (nnmail-expiry-wait expiry-wait))
5836                     (gnus-request-expire-articles
5837                      (gnus-uncompress-sequence (cdr expirable)) group))
5838                 ;; Just expire using the normal expiry values.
5839                 (gnus-request-expire-articles
5840                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5841           (gnus-message 6 "Expiring articles in %s...done" group)))
5842       (gnus-group-position-point))))
5843
5844 (defun gnus-group-expire-all-groups ()
5845   "Expire all expirable articles in all newsgroups."
5846   (interactive)
5847   (save-excursion
5848     (gnus-message 5 "Expiring...")
5849     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5850                                      (cdr gnus-newsrc-alist))))
5851       (gnus-group-expire-articles nil)))
5852   (gnus-group-position-point)
5853   (gnus-message 5 "Expiring...done"))
5854
5855 (defun gnus-group-set-current-level (n level)
5856   "Set the level of the next N groups to LEVEL."
5857   (interactive
5858    (list
5859     current-prefix-arg
5860     (if (not (gnus-group-group-name))
5861         (error "No group on the current line")
5862       (string-to-int
5863        (let ((s (read-string
5864                  (format "Level (default %s): " (gnus-group-group-level)))))
5865          (if (string-match "^\\s-*$" s)
5866              (int-to-string (gnus-group-group-level))
5867            s))))))
5868   (or (and (>= level 1) (<= level gnus-level-killed))
5869       (error "Illegal level: %d" level))
5870   (let ((groups (gnus-group-process-prefix n))
5871         group)
5872     (while (setq group (pop groups))
5873       (gnus-group-remove-mark group)
5874       (gnus-message 6 "Changed level of %s from %d to %d"
5875                     group (or (gnus-group-group-level) gnus-level-killed)
5876                     level)
5877       (gnus-group-change-level
5878        group level (or (gnus-group-group-level) gnus-level-killed))
5879       (gnus-group-update-group-line)))
5880   (gnus-group-position-point))
5881
5882 (defun gnus-group-unsubscribe-current-group (&optional n)
5883   "Toggle subscription of the current group.
5884 If given numerical prefix, toggle the N next groups."
5885   (interactive "P")
5886   (let ((groups (gnus-group-process-prefix n))
5887         group)
5888     (while groups
5889       (setq group (car groups)
5890             groups (cdr groups))
5891       (gnus-group-remove-mark group)
5892       (gnus-group-unsubscribe-group
5893        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5894                  gnus-level-default-unsubscribed
5895                gnus-level-default-subscribed) t)
5896       (gnus-group-update-group-line))
5897     (gnus-group-next-group 1)))
5898
5899 (defun gnus-group-unsubscribe-group (group &optional level silent)
5900   "Toggle subscription to GROUP.
5901 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5902 group line."
5903   (interactive
5904    (list (completing-read
5905           "Group: " gnus-active-hashtb nil
5906           (memq gnus-select-method gnus-have-read-active-file))))
5907   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5908     (cond
5909      ((string-match "^[ \t]$" group)
5910       (error "Empty group name"))
5911      (newsrc
5912       ;; Toggle subscription flag.
5913       (gnus-group-change-level
5914        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
5915                                       gnus-level-subscribed)
5916                                   (1+ gnus-level-subscribed)
5917                                 gnus-level-default-subscribed)))
5918       (unless silent
5919         (gnus-group-update-group group)))
5920      ((and (stringp group)
5921            (or (not (memq gnus-select-method gnus-have-read-active-file))
5922                (gnus-active group)))
5923       ;; Add new newsgroup.
5924       (gnus-group-change-level
5925        group
5926        (if level level gnus-level-default-subscribed)
5927        (or (and (member group gnus-zombie-list)
5928                 gnus-level-zombie)
5929            gnus-level-killed)
5930        (and (gnus-group-group-name)
5931             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5932       (unless silent
5933         (gnus-group-update-group group)))
5934      (t (error "No such newsgroup: %s" group)))
5935     (gnus-group-position-point)))
5936
5937 (defun gnus-group-transpose-groups (n)
5938   "Move the current newsgroup up N places.
5939 If given a negative prefix, move down instead.  The difference between
5940 N and the number of steps taken is returned."
5941   (interactive "p")
5942   (or (gnus-group-group-name)
5943       (error "No group on current line"))
5944   (gnus-group-kill-group 1)
5945   (prog1
5946       (forward-line (- n))
5947     (gnus-group-yank-group)
5948     (gnus-group-position-point)))
5949
5950 (defun gnus-group-kill-all-zombies ()
5951   "Kill all zombie newsgroups."
5952   (interactive)
5953   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
5954   (setq gnus-zombie-list nil)
5955   (gnus-group-list-groups))
5956
5957 (defun gnus-group-kill-region (begin end)
5958   "Kill newsgroups in current region (excluding current point).
5959 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
5960   (interactive "r")
5961   (let ((lines
5962          ;; Count lines.
5963          (save-excursion
5964            (count-lines
5965             (progn
5966               (goto-char begin)
5967               (beginning-of-line)
5968               (point))
5969             (progn
5970               (goto-char end)
5971               (beginning-of-line)
5972               (point))))))
5973     (goto-char begin)
5974     (beginning-of-line)                 ;Important when LINES < 1
5975     (gnus-group-kill-group lines)))
5976
5977 (defun gnus-group-kill-group (&optional n discard)
5978   "Kill the next N groups.
5979 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
5980 However, only groups that were alive can be yanked; already killed
5981 groups or zombie groups can't be yanked.
5982 The return value is the name of the group that was killed, or a list
5983 of groups killed."
5984   (interactive "P")
5985   (let ((buffer-read-only nil)
5986         (groups (gnus-group-process-prefix n))
5987         group entry level out)
5988     (if (< (length groups) 10)
5989         ;; This is faster when there are few groups.
5990         (while groups
5991           (push (setq group (pop groups)) out)
5992           (gnus-group-remove-mark group)
5993           (setq level (gnus-group-group-level))
5994           (gnus-delete-line)
5995           (if (and (not discard)
5996                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
5997               (setq gnus-list-of-killed-groups
5998                     (cons (cons (car entry) (nth 2 entry))
5999                           gnus-list-of-killed-groups)))
6000           (gnus-group-change-level
6001            (if entry entry group) gnus-level-killed (if entry nil level)))
6002       ;; If there are lots and lots of groups to be killed, we use
6003       ;; this thing instead.
6004       (let (entry)
6005         (setq groups (nreverse groups))
6006         (while groups
6007           (gnus-group-remove-mark (setq group (pop groups)))
6008           (gnus-delete-line)
6009           (cond
6010            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6011             (push (cons (car entry) (nth 2 entry))
6012                   gnus-list-of-killed-groups)
6013             (setcdr (cdr entry) (cdr (cdr (cdr entry)))))
6014            ((member group gnus-zombie-list)
6015             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6016         (gnus-make-hashtable-from-newsrc-alist)))
6017
6018     (gnus-group-position-point)
6019     (if (< (length out) 2) (car out) (nreverse out))))
6020
6021 (defun gnus-group-yank-group (&optional arg)
6022   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6023 inserting it before the current newsgroup.  The numeric ARG specifies
6024 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6025 is returned, or (if several groups are yanked) a list of yanked groups
6026 is returned."
6027   (interactive "p")
6028   (setq arg (or arg 1))
6029   (let (info group prev out)
6030     (while (>= (decf arg) 0)
6031       (if (not (setq info (pop gnus-list-of-killed-groups)))
6032           (error "No more newsgroups to yank"))
6033       (push (setq group (nth 1 info)) out)
6034       ;; Find which newsgroup to insert this one before - search
6035       ;; backward until something suitable is found.  If there are no
6036       ;; other newsgroups in this buffer, just make this newsgroup the
6037       ;; first newsgroup.
6038       (setq prev (gnus-group-group-name))
6039       (gnus-group-change-level
6040        info (nth 2 info) gnus-level-killed
6041        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6042        t)
6043       (gnus-group-insert-group-line-info group))
6044     (forward-line -1)
6045     (gnus-group-position-point)
6046     (if (< (length out) 2) (car out) (nreverse out))))
6047
6048 (defun gnus-group-kill-level (level)
6049   "Kill all groups that is on a certain LEVEL."
6050   (interactive "nKill all groups on level: ")
6051   (cond
6052    ((= level gnus-level-zombie)
6053     (setq gnus-killed-list
6054           (nconc gnus-zombie-list gnus-killed-list))
6055     (setq gnus-zombie-list nil))
6056    ((and (< level gnus-level-zombie)
6057          (> level 0)
6058          (or gnus-expert-user
6059              (gnus-yes-or-no-p
6060               (format
6061                "Do you really want to kill all groups on level %d? "
6062                level))))
6063     (let* ((prev gnus-newsrc-alist)
6064            (alist (cdr prev)))
6065       (while alist
6066         (if (= (gnus-info-level level) level)
6067             (setcdr prev (cdr alist))
6068           (setq prev alist))
6069         (setq alist (cdr alist)))
6070       (gnus-make-hashtable-from-newsrc-alist)
6071       (gnus-group-list-groups)))
6072    (t
6073     (error "Can't kill; illegal level: %d" level))))
6074
6075 (defun gnus-group-list-all-groups (&optional arg)
6076   "List all newsgroups with level ARG or lower.
6077 Default is gnus-level-unsubscribed, which lists all subscribed and most
6078 unsubscribed groups."
6079   (interactive "P")
6080   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6081
6082 ;; Redefine this to list ALL killed groups if prefix arg used.
6083 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6084 (defun gnus-group-list-killed (&optional arg)
6085   "List all killed newsgroups in the group buffer.
6086 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6087 entail asking the server for the groups."
6088   (interactive "P")
6089   ;; Find all possible killed newsgroups if arg.
6090   (when arg
6091     ;; First make sure active file has been read.
6092     (unless gnus-have-read-active-file
6093       (let ((gnus-read-active-file t))
6094         (gnus-read-active-file)))
6095     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6096     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6097     (mapatoms
6098      (lambda (sym)
6099        (let ((groups 0)
6100              (group (symbol-name sym)))
6101          (if (or (null group)
6102                  (gnus-gethash group gnus-killed-hashtb)
6103                  (gnus-gethash group gnus-newsrc-hashtb))
6104              ()
6105            (let ((do-sub (gnus-matches-options-n group)))
6106              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6107                  ()
6108                (setq groups (1+ groups))
6109                (setq gnus-killed-list
6110                      (cons group gnus-killed-list))
6111                (gnus-sethash group group gnus-killed-hashtb))))))
6112      gnus-active-hashtb))
6113   (if (not gnus-killed-list)
6114       (gnus-message 6 "No killed groups")
6115     (let (gnus-group-list-mode)
6116       (funcall gnus-group-prepare-function
6117                gnus-level-killed t gnus-level-killed))
6118     (goto-char (point-min)))
6119   (gnus-group-position-point))
6120
6121 (defun gnus-group-list-zombies ()
6122   "List all zombie newsgroups in the group buffer."
6123   (interactive)
6124   (if (not gnus-zombie-list)
6125       (gnus-message 6 "No zombie groups")
6126     (let (gnus-group-list-mode)
6127       (funcall gnus-group-prepare-function
6128                gnus-level-zombie t gnus-level-zombie))
6129     (goto-char (point-min)))
6130   (gnus-group-position-point))
6131
6132 (defun gnus-group-list-active ()
6133   "List all groups that are available from the server(s)."
6134   (interactive)
6135   ;; First we make sure that we have really read the active file.
6136   (unless gnus-have-read-active-file
6137     (let ((gnus-read-active-file t))
6138       (gnus-read-active-file)))
6139   ;; Find all groups and sort them.
6140   (let ((groups
6141          (sort
6142           (let (list)
6143             (mapatoms
6144              (lambda (sym)
6145                (and (symbol-value sym)
6146                     (setq list (cons (symbol-name sym) list))))
6147              gnus-active-hashtb)
6148             list)
6149           'string<))
6150         (buffer-read-only nil))
6151     (erase-buffer)
6152     (while groups
6153       (gnus-group-insert-group-line-info (car groups))
6154       (setq groups (cdr groups)))
6155     (goto-char (point-min))))
6156
6157 (defun gnus-activate-all-groups (level)
6158   "Activate absolutely all groups."
6159   (interactive (list 7))
6160   (let ((gnus-activate-level level)
6161         (gnus-activate-foreign-newsgroups level))
6162     (gnus-group-get-new-news)))
6163
6164 (defun gnus-group-get-new-news (&optional arg)
6165   "Get newly arrived articles.
6166 If ARG is a number, it specifies which levels you are interested in
6167 re-scanning.  If ARG is non-nil and not a number, this will force
6168 \"hard\" re-reading of the active files from all servers."
6169   (interactive "P")
6170   (run-hooks 'gnus-get-new-news-hook)
6171   ;; We might read in new NoCeM messages here.
6172   (when gnus-use-nocem 
6173     (gnus-nocem-scan-groups))
6174   ;; If ARG is not a number, then we read the active file.
6175   (when (and arg (not (numberp arg)))
6176     (let ((gnus-read-active-file t))
6177       (gnus-read-active-file))
6178     (setq arg nil))
6179
6180   (setq arg (gnus-group-default-level arg t))
6181   (if (and gnus-read-active-file (not arg))
6182       (progn
6183         (gnus-read-active-file)
6184         (gnus-get-unread-articles arg))
6185     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6186       (gnus-get-unread-articles arg)))
6187   (gnus-group-list-groups))
6188
6189 (defun gnus-group-get-new-news-this-group (&optional n)
6190   "Check for newly arrived news in the current group (and the N-1 next groups).
6191 The difference between N and the number of newsgroup checked is returned.
6192 If N is negative, this group and the N-1 previous groups will be checked."
6193   (interactive "P")
6194   (let* ((groups (gnus-group-process-prefix n))
6195          (ret (if (numberp n) (- n (length groups)) 0))
6196          group)
6197     (while groups
6198       (setq group (car groups)
6199             groups (cdr groups))
6200       (gnus-group-remove-mark group)
6201       (unless (gnus-get-new-news-in-group group)
6202         (ding)
6203         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6204     (when gnus-goto-next-group-when-activating
6205       (gnus-group-next-unread-group 1 t))
6206     (gnus-summary-position-point)
6207     ret))
6208
6209 (defun gnus-get-new-news-in-group (group)
6210   (when (and group (gnus-activate-group group 'scan))
6211     (gnus-get-unread-articles-in-group
6212      (gnus-get-info group) (gnus-active group) t)
6213     (when (gnus-group-goto-group group)
6214       (gnus-group-update-group-line))
6215     t))
6216
6217 (defun gnus-group-fetch-faq (group &optional faq-dir)
6218   "Fetch the FAQ for the current group."
6219   (interactive
6220    (list
6221     (gnus-group-real-name (gnus-group-group-name))
6222     (cond (current-prefix-arg
6223            (completing-read
6224             "Faq dir: " (and (listp gnus-group-faq-directory)
6225                              gnus-group-faq-directory))))))
6226   (or faq-dir
6227       (setq faq-dir (if (listp gnus-group-faq-directory)
6228                         (car gnus-group-faq-directory)
6229                       gnus-group-faq-directory)))
6230   (or group (error "No group name given"))
6231   (let ((file (concat (file-name-as-directory faq-dir)
6232                       (gnus-group-real-name group))))
6233     (if (not (file-exists-p file))
6234         (error "No such file: %s" file)
6235       (find-file file))))
6236
6237 (defun gnus-group-describe-group (force &optional group)
6238   "Display a description of the current newsgroup."
6239   (interactive (list current-prefix-arg (gnus-group-group-name)))
6240   (and force (setq gnus-description-hashtb nil))
6241   (let ((method (gnus-find-method-for-group group))
6242         desc)
6243     (or group (error "No group name given"))
6244     (and (or (and gnus-description-hashtb
6245                   ;; We check whether this group's method has been
6246                   ;; queried for a description file.
6247                   (gnus-gethash
6248                    (gnus-group-prefixed-name "" method)
6249                    gnus-description-hashtb))
6250              (setq desc (gnus-group-get-description group))
6251              (gnus-read-descriptions-file method))
6252          (message
6253           (or desc (gnus-gethash group gnus-description-hashtb)
6254               "No description available")))))
6255
6256 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6257 (defun gnus-group-describe-all-groups (&optional force)
6258   "Pop up a buffer with descriptions of all newsgroups."
6259   (interactive "P")
6260   (and force (setq gnus-description-hashtb nil))
6261   (if (not (or gnus-description-hashtb
6262                (gnus-read-all-descriptions-files)))
6263       (error "Couldn't request descriptions file"))
6264   (let ((buffer-read-only nil)
6265         b)
6266     (erase-buffer)
6267     (mapatoms
6268      (lambda (group)
6269        (setq b (point))
6270        (insert (format "      *: %-20s %s\n" (symbol-name group)
6271                        (symbol-value group)))
6272        (add-text-properties
6273         b (1+ b) (list 'gnus-group group
6274                        'gnus-unread t 'gnus-marked nil
6275                        'gnus-level (1+ gnus-level-subscribed))))
6276      gnus-description-hashtb)
6277     (goto-char (point-min))
6278     (gnus-group-position-point)))
6279
6280 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6281 (defun gnus-group-apropos (regexp &optional search-description)
6282   "List all newsgroups that have names that match a regexp."
6283   (interactive "sGnus apropos (regexp): ")
6284   (let ((prev "")
6285         (obuf (current-buffer))
6286         groups des)
6287     ;; Go through all newsgroups that are known to Gnus.
6288     (mapatoms
6289      (lambda (group)
6290        (and (symbol-name group)
6291             (string-match regexp (symbol-name group))
6292             (setq groups (cons (symbol-name group) groups))))
6293      gnus-active-hashtb)
6294     ;; Go through all descriptions that are known to Gnus.
6295     (if search-description
6296         (mapatoms
6297          (lambda (group)
6298            (and (string-match regexp (symbol-value group))
6299                 (gnus-active (symbol-name group))
6300                 (setq groups (cons (symbol-name group) groups))))
6301          gnus-description-hashtb))
6302     (if (not groups)
6303         (gnus-message 3 "No groups matched \"%s\"." regexp)
6304       ;; Print out all the groups.
6305       (save-excursion
6306         (pop-to-buffer "*Gnus Help*")
6307         (buffer-disable-undo (current-buffer))
6308         (erase-buffer)
6309         (setq groups (sort groups 'string<))
6310         (while groups
6311           ;; Groups may be entered twice into the list of groups.
6312           (if (not (string= (car groups) prev))
6313               (progn
6314                 (insert (setq prev (car groups)) "\n")
6315                 (if (and gnus-description-hashtb
6316                          (setq des (gnus-gethash (car groups)
6317                                                  gnus-description-hashtb)))
6318                     (insert "  " des "\n"))))
6319           (setq groups (cdr groups)))
6320         (goto-char (point-min))))
6321     (pop-to-buffer obuf)))
6322
6323 (defun gnus-group-description-apropos (regexp)
6324   "List all newsgroups that have names or descriptions that match a regexp."
6325   (interactive "sGnus description apropos (regexp): ")
6326   (if (not (or gnus-description-hashtb
6327                (gnus-read-all-descriptions-files)))
6328       (error "Couldn't request descriptions file"))
6329   (gnus-group-apropos regexp t))
6330
6331 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6332 (defun gnus-group-list-matching (level regexp &optional all lowest)
6333   "List all groups with unread articles that match REGEXP.
6334 If the prefix LEVEL is non-nil, it should be a number that says which
6335 level to cut off listing groups.
6336 If ALL, also list groups with no unread articles.
6337 If LOWEST, don't list groups with level lower than LOWEST."
6338   (interactive "P\nsList newsgroups matching: ")
6339   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6340                            all (or lowest 1) regexp)
6341   (goto-char (point-min))
6342   (gnus-group-position-point))
6343
6344 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6345   "List all groups that match REGEXP.
6346 If the prefix LEVEL is non-nil, it should be a number that says which
6347 level to cut off listing groups.
6348 If LOWEST, don't list groups with level lower than LOWEST."
6349   (interactive "P\nsList newsgroups matching: ")
6350   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6351
6352 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6353 (defun gnus-group-save-newsrc (&optional force)
6354   "Save the Gnus startup files.
6355 If FORCE, force saving whether it is necessary or not."
6356   (interactive "P")
6357   (gnus-save-newsrc-file force))
6358
6359 (defun gnus-group-restart (&optional arg)
6360   "Force Gnus to read the .newsrc file."
6361   (interactive "P")
6362   (gnus-save-newsrc-file)
6363   (gnus-setup-news 'force)
6364   (gnus-group-list-groups arg))
6365
6366 (defun gnus-group-read-init-file ()
6367   "Read the Gnus elisp init file."
6368   (interactive)
6369   (gnus-read-init-file))
6370
6371 (defun gnus-group-check-bogus-groups (&optional silent)
6372   "Check bogus newsgroups.
6373 If given a prefix, don't ask for confirmation before removing a bogus
6374 group."
6375   (interactive "P")
6376   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6377   (gnus-group-list-groups))
6378
6379 (defun gnus-group-edit-global-kill (&optional article group)
6380   "Edit the global kill file.
6381 If GROUP, edit that local kill file instead."
6382   (interactive "P")
6383   (setq gnus-current-kill-article article)
6384   (gnus-kill-file-edit-file group)
6385   (gnus-message
6386    6
6387    (substitute-command-keys
6388     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6389             (if group "local" "global")))))
6390
6391 (defun gnus-group-edit-local-kill (article group)
6392   "Edit a local kill file."
6393   (interactive (list nil (gnus-group-group-name)))
6394   (gnus-group-edit-global-kill article group))
6395
6396 (defun gnus-group-force-update ()
6397   "Update `.newsrc' file."
6398   (interactive)
6399   (gnus-save-newsrc-file))
6400
6401 (defun gnus-group-suspend ()
6402   "Suspend the current Gnus session.
6403 In fact, cleanup buffers except for group mode buffer.
6404 The hook gnus-suspend-gnus-hook is called before actually suspending."
6405   (interactive)
6406   (run-hooks 'gnus-suspend-gnus-hook)
6407   ;; Kill Gnus buffers except for group mode buffer.
6408   (let ((group-buf (get-buffer gnus-group-buffer)))
6409     ;; Do this on a separate list in case the user does a ^G before we finish
6410     (let ((gnus-buffer-list
6411            (delq group-buf (delq gnus-dribble-buffer
6412                                  (append gnus-buffer-list nil)))))
6413       (while gnus-buffer-list
6414         (gnus-kill-buffer (car gnus-buffer-list))
6415         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6416     (if group-buf
6417         (progn
6418           (setq gnus-buffer-list (list group-buf))
6419           (bury-buffer group-buf)
6420           (delete-windows-on group-buf t)))))
6421
6422 (defun gnus-group-clear-dribble ()
6423   "Clear all information from the dribble buffer."
6424   (interactive)
6425   (gnus-dribble-clear)
6426   (gnus-message 7 "Cleared dribble buffer"))
6427
6428 (defun gnus-group-exit ()
6429   "Quit reading news after updating .newsrc.eld and .newsrc.
6430 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6431   (interactive)
6432   (when 
6433       (or noninteractive                ;For gnus-batch-kill
6434           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6435           (not gnus-interactive-exit)   ;Without confirmation
6436           gnus-expert-user
6437           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6438     (run-hooks 'gnus-exit-gnus-hook)
6439     ;; Offer to save data from non-quitted summary buffers.
6440     (gnus-offer-save-summaries)
6441     ;; Save the newsrc file(s).
6442     (gnus-save-newsrc-file)
6443     ;; Kill-em-all.
6444     (gnus-close-backends)
6445     ;; Shut down the cache.
6446     (when gnus-use-cache
6447       (gnus-cache-close))
6448     ;; Reset everything.
6449     (gnus-clear-system)
6450     ;; Allow the user to do things after cleaning up.
6451     (run-hooks 'gnus-after-exiting-gnus-hook)))
6452
6453 (defun gnus-close-backends ()
6454   ;; Send a close request to all backends that support such a request.
6455   (let ((methods gnus-valid-select-methods)
6456         func)
6457     (while methods
6458       (if (fboundp (setq func (intern (concat (car (car methods))
6459                                               "-request-close"))))
6460           (funcall func))
6461       (setq methods (cdr methods)))))
6462
6463 (defun gnus-group-quit ()
6464   "Quit reading news without updating .newsrc.eld or .newsrc.
6465 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6466   (interactive)
6467   (when (or noninteractive              ;For gnus-batch-kill
6468             (zerop (buffer-size))
6469             (not (gnus-server-opened gnus-select-method))
6470             gnus-expert-user
6471             (not gnus-current-startup-file)
6472             (gnus-yes-or-no-p
6473              (format "Quit reading news without saving %s? "
6474                      (file-name-nondirectory gnus-current-startup-file))))
6475     (run-hooks 'gnus-exit-gnus-hook)
6476     (if gnus-use-full-window
6477         (delete-other-windows)
6478       (gnus-remove-some-windows))
6479     (gnus-dribble-save)
6480     (gnus-close-backends)
6481     ;; Shut down the cache.
6482     (when gnus-use-cache
6483       (gnus-cache-close))
6484     (gnus-clear-system)
6485     ;; Allow the user to do things after cleaning up.
6486     (run-hooks 'gnus-after-exiting-gnus-hook)))
6487
6488 (defun gnus-offer-save-summaries ()
6489   "Offer to save all active summary buffers."
6490   (save-excursion
6491     (let ((buflist (buffer-list))
6492           buffers bufname)
6493       ;; Go through all buffers and find all summaries.
6494       (while buflist
6495         (and (setq bufname (buffer-name (car buflist)))
6496              (string-match "Summary" bufname)
6497              (save-excursion
6498                (set-buffer bufname)
6499                ;; We check that this is, indeed, a summary buffer.
6500                (and (eq major-mode 'gnus-summary-mode)
6501                     ;; Also make sure this isn't bogus.
6502                     gnus-newsgroup-prepared))
6503              (push bufname buffers))
6504         (setq buflist (cdr buflist)))
6505       ;; Go through all these summary buffers and offer to save them.
6506       (when buffers
6507         (map-y-or-n-p
6508          "Update summary buffer %s? "
6509          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6510          buffers)))))
6511
6512 (defun gnus-group-describe-briefly ()
6513   "Give a one line description of the group mode commands."
6514   (interactive)
6515   (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")))
6516
6517 (defun gnus-group-browse-foreign-server (method)
6518   "Browse a foreign news server.
6519 If called interactively, this function will ask for a select method
6520  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6521 If not, METHOD should be a list where the first element is the method
6522 and the second element is the address."
6523   (interactive
6524    (list (let ((how (completing-read
6525                      "Which backend: "
6526                      (append gnus-valid-select-methods gnus-server-alist)
6527                      nil t (cons "nntp" 0))))
6528            ;; We either got a backend name or a virtual server name.
6529            ;; If the first, we also need an address.
6530            (if (assoc how gnus-valid-select-methods)
6531                (list (intern how)
6532                      ;; Suggested by mapjph@bath.ac.uk.
6533                      (completing-read
6534                       "Address: "
6535                       (mapcar (lambda (server) (list server))
6536                               gnus-secondary-servers)))
6537              ;; We got a server name, so we find the method.
6538              (gnus-server-to-method how)))))
6539   (gnus-browse-foreign-server method))
6540
6541 \f
6542 ;;;
6543 ;;; Gnus summary mode
6544 ;;;
6545
6546 (defvar gnus-summary-mode-map nil)
6547
6548 (put 'gnus-summary-mode 'mode-class 'special)
6549
6550 (unless gnus-summary-mode-map
6551   (setq gnus-summary-mode-map (make-keymap))
6552   (suppress-keymap gnus-summary-mode-map)
6553
6554   ;; Non-orthogonal keys
6555
6556   (gnus-define-keys
6557    gnus-summary-mode-map
6558    " " gnus-summary-next-page
6559    "\177" gnus-summary-prev-page
6560    [delete] gnus-summary-prev-page
6561    "\r" gnus-summary-scroll-up
6562    "n" gnus-summary-next-unread-article
6563    "p" gnus-summary-prev-unread-article
6564    "N" gnus-summary-next-article
6565    "P" gnus-summary-prev-article
6566    "\M-\C-n" gnus-summary-next-same-subject
6567    "\M-\C-p" gnus-summary-prev-same-subject
6568    "\M-n" gnus-summary-next-unread-subject
6569    "\M-p" gnus-summary-prev-unread-subject
6570    "." gnus-summary-first-unread-article
6571    "," gnus-summary-best-unread-article
6572    "\M-s" gnus-summary-search-article-forward
6573    "\M-r" gnus-summary-search-article-backward
6574    "<" gnus-summary-beginning-of-article
6575    ">" gnus-summary-end-of-article
6576    "j" gnus-summary-goto-article
6577    "^" gnus-summary-refer-parent-article
6578    "\M-^" gnus-summary-refer-article
6579    "u" gnus-summary-tick-article-forward
6580    "!" gnus-summary-tick-article-forward
6581    "U" gnus-summary-tick-article-backward
6582    "d" gnus-summary-mark-as-read-forward
6583    "D" gnus-summary-mark-as-read-backward
6584    "E" gnus-summary-mark-as-expirable
6585    "\M-u" gnus-summary-clear-mark-forward
6586    "\M-U" gnus-summary-clear-mark-backward
6587    "k" gnus-summary-kill-same-subject-and-select
6588    "\C-k" gnus-summary-kill-same-subject
6589    "\M-\C-k" gnus-summary-kill-thread
6590    "\M-\C-l" gnus-summary-lower-thread
6591    "e" gnus-summary-edit-article
6592    "#" gnus-summary-mark-as-processable
6593    "\M-#" gnus-summary-unmark-as-processable
6594    "\M-\C-t" gnus-summary-toggle-threads
6595    "\M-\C-s" gnus-summary-show-thread
6596    "\M-\C-h" gnus-summary-hide-thread
6597    "\M-\C-f" gnus-summary-next-thread
6598    "\M-\C-b" gnus-summary-prev-thread
6599    "\M-\C-u" gnus-summary-up-thread
6600    "\M-\C-d" gnus-summary-down-thread
6601    "&" gnus-summary-execute-command
6602    "c" gnus-summary-catchup-and-exit
6603    "\C-w" gnus-summary-mark-region-as-read
6604    "\C-t" gnus-summary-toggle-truncation
6605    "?" gnus-summary-mark-as-dormant
6606    "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6607    "\C-c\C-s\C-n" gnus-summary-sort-by-number
6608    "\C-c\C-s\C-a" gnus-summary-sort-by-author
6609    "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6610    "\C-c\C-s\C-d" gnus-summary-sort-by-date
6611    "\C-c\C-s\C-i" gnus-summary-sort-by-score
6612    "=" gnus-summary-expand-window
6613    "\C-x\C-s" gnus-summary-reselect-current-group
6614    "\M-g" gnus-summary-rescan-group
6615    "w" gnus-summary-stop-page-breaking
6616    "\C-c\C-r" gnus-summary-caesar-message
6617    "\M-t" gnus-summary-toggle-mime
6618    "f" gnus-summary-followup
6619    "F" gnus-summary-followup-with-original
6620    "C" gnus-summary-cancel-article
6621    "r" gnus-summary-reply
6622    "R" gnus-summary-reply-with-original
6623    "\C-c\C-f" gnus-summary-mail-forward
6624    "o" gnus-summary-save-article
6625    "\C-o" gnus-summary-save-article-mail
6626    "|" gnus-summary-pipe-output
6627    "\M-k" gnus-summary-edit-local-kill
6628    "\M-K" gnus-summary-edit-global-kill
6629    "V" gnus-version
6630    "\C-c\C-d" gnus-summary-describe-group
6631    "q" gnus-summary-exit
6632    "Q" gnus-summary-exit-no-update
6633    "\C-c\C-i" gnus-info-find-node
6634    gnus-mouse-2 gnus-mouse-pick-article
6635    "m" gnus-summary-mail-other-window
6636    "a" gnus-summary-post-news
6637    "x" gnus-summary-limit-to-unread
6638    "s" gnus-summary-isearch-article
6639    "t" gnus-article-hide-headers
6640    "g" gnus-summary-show-article
6641    "l" gnus-summary-goto-last-article
6642    "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6643    "\C-d" gnus-summary-enter-digest-group
6644    "\C-c\C-b" gnus-bug
6645    "*" gnus-cache-enter-article
6646    "\M-*" gnus-cache-remove-article
6647    "\M-&" gnus-summary-universal-argument
6648    "\C-l" gnus-recenter
6649    "I" gnus-summary-increase-score
6650    "L" gnus-summary-lower-score
6651
6652    "V" gnus-summary-score-map
6653    "X" gnus-uu-extract-map
6654    "S" gnus-summary-send-map)
6655
6656   ;; Sort of orthogonal keymap
6657   (gnus-define-keys
6658    (gnus-summary-mark-map "M" gnus-summary-mode-map)
6659    "t" gnus-summary-tick-article-forward
6660    "!" gnus-summary-tick-article-forward
6661    "d" gnus-summary-mark-as-read-forward
6662    "r" gnus-summary-mark-as-read-forward
6663    "c" gnus-summary-clear-mark-forward
6664    " " gnus-summary-clear-mark-forward
6665    "e" gnus-summary-mark-as-expirable
6666    "x" gnus-summary-mark-as-expirable
6667    "?" gnus-summary-mark-as-dormant
6668    "b" gnus-summary-set-bookmark
6669    "B" gnus-summary-remove-bookmark
6670    "#" gnus-summary-mark-as-processable
6671    "\M-#" gnus-summary-unmark-as-processable
6672    "S" gnus-summary-limit-include-expunged
6673    "C" gnus-summary-catchup
6674    "H" gnus-summary-catchup-to-here
6675    "\C-c" gnus-summary-catchup-all
6676    "k" gnus-summary-kill-same-subject-and-select
6677    "K" gnus-summary-kill-same-subject
6678    "P" gnus-uu-mark-map)
6679
6680   (gnus-define-keys
6681    (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6682    "c" gnus-summary-clear-above
6683    "u" gnus-summary-tick-above
6684    "m" gnus-summary-mark-above
6685    "k" gnus-summary-kill-below)
6686
6687   (gnus-define-keys
6688    (gnus-summary-limit-map "/" gnus-summary-mode-map)
6689    "/" gnus-summary-limit-to-subject
6690    "n" gnus-summary-limit-to-articles
6691    "w" gnus-summary-pop-limit
6692    "s" gnus-summary-limit-to-subject
6693    "a" gnus-summary-limit-to-author
6694    "u" gnus-summary-limit-to-unread
6695    "m" gnus-summary-limit-to-marks
6696    "v" gnus-summary-limit-to-score
6697    "D" gnus-summary-limit-include-dormant
6698    "d" gnus-summary-limit-exclude-dormant
6699 ;;  "t" gnus-summary-limit-exclude-thread
6700    "E" gnus-summary-limit-include-expunged
6701    "c" gnus-summary-limit-exclude-childless-dormant
6702    "C" gnus-summary-limit-mark-excluded-as-read)
6703
6704   (gnus-define-keys
6705    (gnus-summary-goto-map "G" gnus-summary-mode-map)
6706    "n" gnus-summary-next-unread-article
6707    "p" gnus-summary-prev-unread-article
6708    "N" gnus-summary-next-article
6709    "P" gnus-summary-prev-article
6710    "\C-n" gnus-summary-next-same-subject
6711    "\C-p" gnus-summary-prev-same-subject
6712    "\M-n" gnus-summary-next-unread-subject
6713    "\M-p" gnus-summary-prev-unread-subject
6714    "f" gnus-summary-first-unread-article
6715    "b" gnus-summary-best-unread-article
6716    "g" gnus-summary-goto-subject
6717    "l" gnus-summary-goto-last-article
6718    "p" gnus-summary-pop-article)
6719
6720   (gnus-define-keys
6721    (gnus-summary-thread-map "T" gnus-summary-mode-map)
6722    "k" gnus-summary-kill-thread
6723    "l" gnus-summary-lower-thread
6724    "i" gnus-summary-raise-thread
6725    "T" gnus-summary-toggle-threads
6726    "t" gnus-summary-rethread-current
6727    "^" gnus-summary-reparent-thread
6728    "s" gnus-summary-show-thread
6729    "S" gnus-summary-show-all-threads
6730    "h" gnus-summary-hide-thread
6731    "H" gnus-summary-hide-all-threads
6732    "n" gnus-summary-next-thread
6733    "p" gnus-summary-prev-thread
6734    "u" gnus-summary-up-thread
6735    "o" gnus-summary-top-thread
6736    "d" gnus-summary-down-thread
6737    "#" gnus-uu-mark-thread
6738    "\M-#" gnus-uu-unmark-thread)
6739
6740   (gnus-define-keys
6741    (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6742    "c" gnus-summary-catchup-and-exit
6743    "C" gnus-summary-catchup-all-and-exit
6744    "E" gnus-summary-exit-no-update
6745    "Q" gnus-summary-exit
6746    "Z" gnus-summary-exit
6747    "n" gnus-summary-catchup-and-goto-next-group
6748    "R" gnus-summary-reselect-current-group
6749    "G" gnus-summary-rescan-group
6750    "N" gnus-summary-next-group
6751    "P" gnus-summary-prev-group)
6752
6753   (gnus-define-keys
6754    (gnus-summary-article-map "A" gnus-summary-mode-map)
6755    " " gnus-summary-next-page
6756    "n" gnus-summary-next-page
6757    "\177" gnus-summary-prev-page
6758    [delete] gnus-summary-prev-page
6759    "p" gnus-summary-prev-page
6760    "\r" gnus-summary-scroll-up
6761    "<" gnus-summary-beginning-of-article
6762    ">" gnus-summary-end-of-article
6763    "b" gnus-summary-beginning-of-article
6764    "e" gnus-summary-end-of-article
6765    "^" gnus-summary-refer-parent-article
6766    "r" gnus-summary-refer-parent-article
6767    "R" gnus-summary-refer-references
6768    "g" gnus-summary-show-article
6769    "s" gnus-summary-isearch-article)
6770
6771   (gnus-define-keys
6772    (gnus-summary-wash-map "W" gnus-summary-mode-map)
6773    "b" gnus-article-add-buttons
6774    "B" gnus-article-add-buttons-to-head
6775    "o" gnus-article-treat-overstrike
6776 ;;  "w" gnus-article-word-wrap
6777    "w" gnus-article-fill-cited-article
6778    "c" gnus-article-remove-cr
6779    "L" gnus-article-remove-trailing-blank-lines
6780    "q" gnus-article-de-quoted-unreadable
6781    "f" gnus-article-display-x-face
6782    "l" gnus-summary-stop-page-breaking
6783    "r" gnus-summary-caesar-message
6784    "t" gnus-article-hide-headers
6785    "v" gnus-summary-verbose-headers
6786    "m" gnus-summary-toggle-mime)
6787
6788   (gnus-define-keys
6789    (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6790    "a" gnus-article-hide
6791    "h" gnus-article-hide-headers
6792    "b" gnus-article-hide-boring-headers
6793    "s" gnus-article-hide-signature
6794    "c" gnus-article-hide-citation
6795    "p" gnus-article-hide-pgp
6796    "\C-c" gnus-article-hide-citation-maybe)
6797
6798   (gnus-define-keys
6799    (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6800    "a" gnus-article-highlight
6801    "h" gnus-article-highlight-headers
6802    "c" gnus-article-highlight-citation
6803    "s" gnus-article-highlight-signature)
6804
6805   (gnus-define-keys
6806    (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6807    "z" gnus-article-date-ut
6808    "u" gnus-article-date-ut
6809    "l" gnus-article-date-local
6810    "e" gnus-article-date-lapsed
6811    "o" gnus-article-date-original)
6812
6813   (gnus-define-keys
6814    (gnus-summary-help-map "H" gnus-summary-mode-map)
6815    "v" gnus-version
6816    "f" gnus-summary-fetch-faq
6817    "d" gnus-summary-describe-group
6818    "h" gnus-summary-describe-briefly
6819    "i" gnus-info-find-node)
6820
6821   (gnus-define-keys
6822    (gnus-summary-backend-map "B" gnus-summary-mode-map)
6823    "e" gnus-summary-expire-articles
6824    "\M-\C-e" gnus-summary-expire-articles-now
6825    "\177" gnus-summary-delete-article
6826    [delete] gnus-summary-delete-article
6827    "m" gnus-summary-move-article
6828    "r" gnus-summary-respool-article
6829    "w" gnus-summary-edit-article
6830    "c" gnus-summary-copy-article
6831    "B" gnus-summary-crosspost-article
6832    "q" gnus-summary-respool-query
6833    "i" gnus-summary-import-article)
6834
6835   (gnus-define-keys
6836    (gnus-summary-save-map "O" gnus-summary-mode-map)
6837    "o" gnus-summary-save-article
6838    "m" gnus-summary-save-article-mail
6839    "r" gnus-summary-save-article-rmail
6840    "f" gnus-summary-save-article-file
6841    "b" gnus-summary-save-article-body-file
6842    "h" gnus-summary-save-article-folder
6843    "v" gnus-summary-save-article-vm
6844    "p" gnus-summary-pipe-output
6845    "s" gnus-soup-add-article)
6846   )
6847
6848 \f
6849
6850 (defun gnus-summary-mode (&optional group)
6851   "Major mode for reading articles.
6852
6853 All normal editing commands are switched off.
6854 \\<gnus-summary-mode-map>
6855 Each line in this buffer represents one article.  To read an
6856 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6857 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6858 respectively.
6859
6860 You can also post articles and send mail from this buffer.  To
6861 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6862 of an article, type `\\[gnus-summary-reply]'.
6863
6864 There are approx. one gazillion commands you can execute in this
6865 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6866
6867 The following commands are available:
6868
6869 \\{gnus-summary-mode-map}"
6870   (interactive)
6871   (when (and menu-bar-mode
6872              (gnus-visual-p 'summary-menu 'menu))
6873     (gnus-summary-make-menu-bar))
6874   (kill-all-local-variables)
6875   (let ((locals gnus-summary-local-variables))
6876     (while locals
6877       (if (consp (car locals))
6878           (progn
6879             (make-local-variable (car (car locals)))
6880             (set (car (car locals)) (eval (cdr (car locals)))))
6881         (make-local-variable (car locals))
6882         (set (car locals) nil))
6883       (setq locals (cdr locals))))
6884   (gnus-make-thread-indent-array)
6885   (gnus-simplify-mode-line)
6886   (setq major-mode 'gnus-summary-mode)
6887   (setq mode-name "Summary")
6888   (make-local-variable 'minor-mode-alist)
6889   (use-local-map gnus-summary-mode-map)
6890   (buffer-disable-undo (current-buffer))
6891   (setq buffer-read-only t)             ;Disable modification
6892   (setq truncate-lines t)
6893   (setq selective-display t)
6894   (setq selective-display-ellipses t)   ;Display `...'
6895   (setq buffer-display-table gnus-summary-display-table)
6896   (setq gnus-newsgroup-name group)
6897   (run-hooks 'gnus-summary-mode-hook))
6898
6899 (defun gnus-summary-make-display-table ()
6900   ;; Change the display table.  Odd characters have a tendency to mess
6901   ;; up nicely formatted displays - we make all possible glyphs
6902   ;; display only a single character.
6903
6904   ;; We start from the standard display table, if any.
6905   (setq gnus-summary-display-table
6906         (or (copy-sequence standard-display-table)
6907             (make-display-table)))
6908   ;; Nix out all the control chars...
6909   (let ((i 32))
6910     (while (>= (setq i (1- i)) 0)
6911       (aset gnus-summary-display-table i [??])))
6912   ;; ... but not newline and cr, of course. (cr is necessary for the
6913   ;; selective display).
6914   (aset gnus-summary-display-table ?\n nil)
6915   (aset gnus-summary-display-table ?\r nil)
6916   ;; We nix out any glyphs over 126 that are not set already.
6917   (let ((i 256))
6918     (while (>= (setq i (1- i)) 127)
6919       ;; Only modify if the entry is nil.
6920       (or (aref gnus-summary-display-table i)
6921           (aset gnus-summary-display-table i [??])))))
6922
6923 (defun gnus-summary-clear-local-variables ()
6924   (let ((locals gnus-summary-local-variables))
6925     (while locals
6926       (if (consp (car locals))
6927           (and (vectorp (car (car locals)))
6928                (set (car (car locals)) nil))
6929         (and (vectorp (car locals))
6930              (set (car locals) nil)))
6931       (setq locals (cdr locals)))))
6932
6933 ;; Summary data functions.
6934
6935 (defmacro gnus-data-number (data)
6936   `(car ,data))
6937
6938 (defmacro gnus-data-set-number (data number)
6939   `(setcar ,data ,number))
6940
6941 (defmacro gnus-data-mark (data)
6942   `(nth 1 ,data))
6943
6944 (defmacro gnus-data-set-mark (data mark)
6945   `(setcar (nthcdr 1 ,data) ,mark))
6946
6947 (defmacro gnus-data-pos (data)
6948   `(nth 2 ,data))
6949
6950 (defmacro gnus-data-set-pos (data pos)
6951   `(setcar (nthcdr 2 ,data) ,pos))
6952
6953 (defmacro gnus-data-header (data)
6954   `(nth 3 ,data))
6955
6956 (defmacro gnus-data-level (data)
6957   `(nth 4 ,data))
6958
6959 (defmacro gnus-data-unread-p (data)
6960   `(= (nth 1 ,data) gnus-unread-mark))
6961
6962 (defmacro gnus-data-pseudo-p (data)
6963   `(consp (nth 3 ,data)))
6964
6965 (defmacro gnus-data-find (number)
6966   `(assq ,number gnus-newsgroup-data))
6967
6968 (defmacro gnus-data-find-list (number &optional data)
6969   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
6970      (memq (assq ,number bdata)
6971            bdata)))
6972
6973 (defmacro gnus-data-make (number mark pos header level)
6974   `(list ,number ,mark ,pos ,header ,level))
6975
6976 (defun gnus-data-enter (after-article number mark pos header level offset)
6977   (let ((data (gnus-data-find-list after-article)))
6978     (or data (error "No such article: %d" after-article))
6979     (setcdr data (cons (gnus-data-make number mark pos header level)
6980                        (cdr data)))
6981     (setq gnus-newsgroup-data-reverse nil)
6982     (gnus-data-update-list (cdr (cdr data)) offset)))
6983
6984 (defun gnus-data-enter-list (after-article list &optional offset)
6985   (when list
6986     (let ((data (and after-article (gnus-data-find-list after-article)))
6987           (ilist list))
6988       (or data (not after-article) (error "No such article: %d" after-article))
6989       ;; Find the last element in the list to be spliced into the main
6990       ;; list.
6991       (while (cdr list)
6992         (setq list (cdr list)))
6993       (if (not data)
6994           (progn
6995             (setcdr list gnus-newsgroup-data)
6996             (setq gnus-newsgroup-data ilist)
6997             (and offset (gnus-data-update-list (cdr list) offset)))
6998         (setcdr list (cdr data))
6999         (setcdr data ilist)
7000         (and offset (gnus-data-update-list (cdr data) offset)))
7001       (setq gnus-newsgroup-data-reverse nil))))
7002
7003 (defun gnus-data-remove (article &optional offset)
7004   (let ((data gnus-newsgroup-data))
7005     (if (= (gnus-data-number (car data)) article)
7006         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7007               gnus-newsgroup-data-reverse nil)
7008       (while (cdr data)
7009         (and (= (gnus-data-number (car (cdr data))) article)
7010              (progn
7011                (setcdr data (cdr (cdr data)))
7012                (and offset (gnus-data-update-list (cdr data) offset))
7013                (setq data nil
7014                      gnus-newsgroup-data-reverse nil)))
7015         (setq data (cdr data))))))
7016
7017 (defmacro gnus-data-list (backward)
7018   `(if ,backward
7019        (or gnus-newsgroup-data-reverse
7020            (setq gnus-newsgroup-data-reverse
7021                  (reverse gnus-newsgroup-data)))
7022      gnus-newsgroup-data))
7023
7024 (defun gnus-data-update-list (data offset)
7025   "Add OFFSET to the POS of all data entries in DATA."
7026   (while data
7027     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7028     (setq data (cdr data))))
7029
7030 (defun gnus-data-compute-positions ()
7031   "Compute the positions of all articles."
7032   (let ((data gnus-newsgroup-data)
7033         pos)
7034     (while data
7035       (when (setq pos (text-property-any
7036                        (point-min) (point-max)
7037                        'gnus-number (gnus-data-number (car data))))
7038         (gnus-data-set-pos (car data) (+ pos 3)))
7039       (setq data (cdr data)))))
7040
7041 (defun gnus-summary-article-pseudo-p (article)
7042   "Say whether this article is a pseudo article or not."
7043   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7044
7045 (defun gnus-article-parent-p (number)
7046   "Say whether this article is a parent or not."
7047   (let ((data (gnus-data-find-list number)))
7048     (and (cdr data)                     ; There has to be an article after...
7049          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7050             (gnus-data-level (nth 1 data))))))
7051
7052 (defun gnus-article-children (number)
7053   "Return a list of all children to NUMBER."
7054   (let* ((data (gnus-data-find-list number))
7055          (level (gnus-data-level (car data)))
7056          children)
7057     (setq data (cdr data))
7058     (while (and data            
7059                 (= (gnus-data-level (car data)) (1+ level)))
7060       (push (gnus-data-number (car data)) children)
7061       (setq data (cdr data)))
7062     children))
7063
7064 (defmacro gnus-summary-skip-intangible ()
7065   "If the current article is intangible, then jump to a different article."
7066   '(let ((to (get-text-property (point) 'gnus-intangible)))
7067     (and to (gnus-summary-goto-subject to))))
7068
7069 (defmacro gnus-summary-article-intangible-p ()
7070   "Say whether this article is intangible or not."
7071   '(get-text-property (point) 'gnus-intangible))
7072
7073 ;; Some summary mode macros.
7074
7075 (defmacro gnus-summary-article-number ()
7076   "The article number of the article on the current line.
7077 If there isn's an article number here, then we return the current
7078 article number."
7079   '(progn
7080      (gnus-summary-skip-intangible)
7081      (or (get-text-property (point) 'gnus-number)
7082          (gnus-summary-last-subject))))
7083
7084 (defmacro gnus-summary-article-header (&optional number)
7085   `(gnus-data-header (gnus-data-find
7086                       ,(or number '(gnus-summary-article-number)))))
7087
7088 (defmacro gnus-summary-thread-level (&optional number)
7089   `(if (and (eq gnus-summary-make-false-root 'dummy)
7090             (get-text-property (point) 'gnus-intangible))
7091        0
7092      (gnus-data-level (gnus-data-find
7093                        ,(or number '(gnus-summary-article-number))))))
7094
7095 (defmacro gnus-summary-article-mark (&optional number)
7096   `(gnus-data-mark (gnus-data-find
7097                     ,(or number '(gnus-summary-article-number)))))
7098
7099 (defmacro gnus-summary-article-pos (&optional number)
7100   `(gnus-data-pos (gnus-data-find
7101                    ,(or number '(gnus-summary-article-number)))))
7102
7103 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7104 (defmacro gnus-summary-article-subject (&optional number)
7105   "Return current subject string or nil if nothing."
7106   `(let ((headers
7107           ,(if number
7108                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7109              '(gnus-data-header (assq (gnus-summary-article-number)
7110                                       gnus-newsgroup-data)))))
7111      (and headers
7112           (vectorp headers)
7113           (mail-header-subject headers))))
7114
7115 (defmacro gnus-summary-article-score (&optional number)
7116   "Return current article score."
7117   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7118                   gnus-newsgroup-scored))
7119        gnus-summary-default-score 0))
7120
7121 (defun gnus-summary-article-children (&optional number)
7122   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7123          (level (gnus-data-level (car data)))
7124          l children)
7125     (while (and (setq data (cdr data))
7126                 (> (setq l (gnus-data-level (car data))) level))
7127       (and (= (1+ level) l)
7128            (setq children (cons (gnus-data-number (car data))
7129                                 children))))
7130     (nreverse children)))
7131
7132 (defun gnus-summary-article-parent (&optional number)
7133   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7134                                     (gnus-data-list t)))
7135          (level (gnus-data-level (car data)))
7136          l)
7137     (if (zerop level)
7138         () ; This is a root.
7139       ;; We search until we find an article with a level less than
7140       ;; this one.  That function has to be the parent.
7141       (while (and (setq data (cdr data))
7142                   (not (< (gnus-data-level (car data)) level))))
7143       (and data (gnus-data-number (car data))))))
7144
7145 (defun gnus-unread-mark-p (mark)
7146   "Say whether MARK is the unread mark."
7147   (= mark gnus-unread-mark))
7148
7149 (defun gnus-read-mark-p (mark)
7150   "Say whether MARK is one of the marks that mark as read.
7151 This is all marks except unread, ticked, dormant, and expirable."
7152   (not (or (= mark gnus-unread-mark)
7153            (= mark gnus-ticked-mark)
7154            (= mark gnus-dormant-mark)
7155            (= mark gnus-expirable-mark))))
7156
7157 ;; Various summary mode internalish functions.
7158
7159 (defun gnus-mouse-pick-article (e)
7160   (interactive "e")
7161   (mouse-set-point e)
7162   (gnus-summary-next-page nil t))
7163
7164 (defun gnus-summary-setup-buffer (group)
7165   "Initialize summary buffer."
7166   (let ((buffer (concat "*Summary " group "*")))
7167     (if (get-buffer buffer)
7168         (progn
7169           (set-buffer buffer)
7170           (setq gnus-summary-buffer (current-buffer))
7171           (not gnus-newsgroup-prepared))
7172       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7173       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7174       (gnus-add-current-to-buffer-list)
7175       (gnus-summary-mode group)
7176       (when gnus-carpal
7177         (gnus-carpal-setup-buffer 'summary))
7178       (unless gnus-single-article-buffer
7179         (make-local-variable 'gnus-article-buffer)
7180         (make-local-variable 'gnus-original-article-buffer))
7181       (setq gnus-newsgroup-name group)
7182       t)))
7183
7184 (defun gnus-set-global-variables ()
7185   ;; Set the global equivalents of the summary buffer-local variables
7186   ;; to the latest values they had.  These reflect the summary buffer
7187   ;; that was in action when the last article was fetched.
7188   (when (eq major-mode 'gnus-summary-mode)
7189     (setq gnus-summary-buffer (current-buffer))
7190     (let ((name gnus-newsgroup-name)
7191           (marked gnus-newsgroup-marked)
7192           (unread gnus-newsgroup-unreads)
7193           (headers gnus-current-headers)
7194           (data gnus-newsgroup-data)
7195           (article-buffer gnus-article-buffer)
7196           (original gnus-original-article-buffer)
7197           (score-file gnus-current-score-file))
7198       (save-excursion
7199         (set-buffer gnus-group-buffer)
7200         (setq gnus-newsgroup-name name)
7201         (setq gnus-newsgroup-marked marked)
7202         (setq gnus-newsgroup-unreads unread)
7203         (setq gnus-current-headers headers)
7204         (setq gnus-newsgroup-data data)
7205         (setq gnus-article-buffer article-buffer)
7206         (setq gnus-original-article-buffer original)
7207         (setq gnus-current-score-file score-file)))))
7208
7209 (defun gnus-summary-last-article-p (&optional article)
7210   "Return whether ARTICLE is the last article in the buffer."
7211   (if (not (setq article (or article (gnus-summary-article-number))))
7212       t ; All non-existant numbers are the last article. :-)
7213     (cdr (gnus-data-find-list article))))
7214
7215 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7216   "Insert a dummy root in the summary buffer."
7217   (beginning-of-line)
7218   (add-text-properties
7219    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7220    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7221
7222 (defvar gnus-thread-indent-array nil)
7223 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7224 (defun gnus-make-thread-indent-array ()
7225   (let ((n 200))
7226     (if (and gnus-thread-indent-array
7227              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7228         nil
7229       (setq gnus-thread-indent-array (make-vector 201 "")
7230             gnus-thread-indent-array-level gnus-thread-indent-level)
7231       (while (>= n 0)
7232         (aset gnus-thread-indent-array n
7233               (make-string (* n gnus-thread-indent-level) ? ))
7234         (setq n (1- n))))))
7235
7236 (defun gnus-summary-insert-line
7237   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7238                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7239                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7240   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7241          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7242          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7243          (gnus-tmp-score-char
7244           (if (or (null gnus-summary-default-score)
7245                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7246                       gnus-summary-zcore-fuzz)) ? 
7247             (if (< gnus-tmp-score gnus-summary-default-score)
7248                 gnus-score-below-mark gnus-score-over-mark)))
7249          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7250                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7251                                   gnus-cached-mark)
7252                                  (gnus-tmp-replied gnus-replied-mark)
7253                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7254                                   gnus-saved-mark)
7255                                  (t gnus-unread-mark)))
7256          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7257          (gnus-tmp-name
7258           (cond
7259            ((string-match "(.+)" gnus-tmp-from)
7260             (substring gnus-tmp-from
7261                        (1+ (match-beginning 0)) (1- (match-end 0))))
7262            ((string-match "<[^>]+> *$" gnus-tmp-from)
7263             (let ((beg (match-beginning 0)))
7264               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7265                        (substring gnus-tmp-from (1+ (match-beginning 0))
7266                                   (1- (match-end 0))))
7267                   (substring gnus-tmp-from 0 beg))))
7268            (t gnus-tmp-from)))
7269          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7270          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7271          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7272          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7273          (buffer-read-only nil))
7274     (when (string= gnus-tmp-name "")
7275       (setq gnus-tmp-name gnus-tmp-from))
7276     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7277     (put-text-property
7278      (point)
7279      (progn (eval gnus-summary-line-format-spec) (point))
7280      'gnus-number gnus-tmp-number)
7281     (when (gnus-visual-p 'summary-highlight 'highlight)
7282       (forward-line -1)
7283       (run-hooks 'gnus-summary-update-hook)
7284       (forward-line 1))))
7285
7286 (defun gnus-summary-update-line (&optional dont-update)
7287   ;; Update summary line after change.
7288   (when (and gnus-summary-default-score
7289              (not gnus-summary-inhibit-highlight))
7290     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7291            (article (gnus-summary-article-number))
7292            (score (gnus-summary-article-score article)))
7293       (unless dont-update
7294         (if (and gnus-summary-mark-below
7295                  (< (gnus-summary-article-score)
7296                     gnus-summary-mark-below))
7297             ;; This article has a low score, so we mark it as read.
7298             (when (memq article gnus-newsgroup-unreads)
7299               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7300           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7301             ;; This article was previously marked as read on account
7302             ;; of a low score, but now it has risen, so we mark it as
7303             ;; unread.
7304             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7305         (gnus-summary-update-mark
7306          (if (or (null gnus-summary-default-score)
7307                  (<= (abs (- score gnus-summary-default-score))
7308                      gnus-summary-zcore-fuzz)) ? 
7309            (if (< score gnus-summary-default-score)
7310                gnus-score-below-mark gnus-score-over-mark)) 'score))
7311       ;; Do visual highlighting.
7312       (when (gnus-visual-p 'summary-highlight 'highlight)
7313         (run-hooks 'gnus-summary-update-hook)))))
7314
7315 (defvar gnus-tmp-new-adopts nil)
7316
7317 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7318   ;; Sum up all elements (and sub-elements) in a list.
7319   (let* ((number
7320           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7321           (cond
7322            ((and (consp thread) (cdr thread))
7323             (apply
7324              '+ 1 (mapcar
7325                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7326            ((null thread)
7327             1)
7328            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7329             1)
7330            (t 1))))
7331     (when (and level (zerop level) gnus-tmp-new-adopts)
7332       (incf number
7333             (apply '+ (mapcar
7334                        'gnus-summary-number-of-articles-in-thread
7335                        gnus-tmp-new-adopts))))
7336     (if char
7337         (if (> number 1) gnus-not-empty-thread-mark
7338           gnus-empty-thread-mark)
7339       number)))
7340
7341 (defun gnus-summary-set-local-parameters (group)
7342  "Go through the local params of GROUP and set all variable specs in that list."
7343   (let ((params (gnus-info-params (gnus-get-info group)))
7344         elem)
7345     (while params
7346       (setq elem (car params)
7347             params (cdr params))
7348       (and (consp elem)                 ; Has to be a cons.
7349            (consp (cdr elem))           ; The cdr has to be a list.
7350            (symbolp (car elem))         ; Has to be a symbol in there.
7351            (not (memq (car elem) 
7352                       '(quit-config to-address to-list to-group)))
7353            (progn                       ; So we set it.
7354              (make-local-variable (car elem))
7355              (set (car elem) (eval (nth 1 elem))))))))
7356
7357 (defun gnus-summary-read-group
7358   (group &optional show-all no-article kill-buffer no-display)
7359   "Start reading news in newsgroup GROUP.
7360 If SHOW-ALL is non-nil, already read articles are also listed.
7361 If NO-ARTICLE is non-nil, no article is selected initially.
7362 If NO-DISPLAY, don't generate a summary buffer."
7363   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7364   (let* ((new-group (gnus-summary-setup-buffer group))
7365          (quit-config (gnus-group-quit-config group))
7366          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7367     (cond
7368      ;; This summary buffer exists already, so we just select it.
7369      ((not new-group)
7370       (gnus-set-global-variables)
7371       (when kill-buffer
7372         (gnus-kill-or-deaden-summary kill-buffer))
7373       (gnus-configure-windows 'summary 'force)
7374       (gnus-set-mode-line 'summary)
7375       (gnus-summary-position-point)
7376       (message "")
7377       t)
7378      ;; We couldn't select this group.
7379      ((null did-select)
7380       (when (and (eq major-mode 'gnus-summary-mode)
7381                  (not (equal (current-buffer) kill-buffer)))
7382         (kill-buffer (current-buffer))
7383         (if (not quit-config)
7384             (progn
7385               (set-buffer gnus-group-buffer)
7386               (gnus-group-jump-to-group group)
7387               (gnus-group-next-unread-group 1))
7388           (if (not (buffer-name (car quit-config)))
7389               (gnus-configure-windows 'group 'force)
7390             (set-buffer (car quit-config))
7391             (and (eq major-mode 'gnus-summary-mode)
7392                  (gnus-set-global-variables))
7393             (gnus-configure-windows (cdr quit-config)))))
7394       (gnus-message 3 "Can't select group")
7395       nil)
7396      ;; The user did a `C-g' while prompting for number of articles,
7397      ;; so we exit this group.
7398      ((eq did-select 'quit)
7399       (and (eq major-mode 'gnus-summary-mode)
7400            (not (equal (current-buffer) kill-buffer))
7401            (kill-buffer (current-buffer)))
7402       (when kill-buffer
7403         (gnus-kill-or-deaden-summary kill-buffer))
7404       (if (not quit-config)
7405           (progn
7406             (set-buffer gnus-group-buffer)
7407             (gnus-group-jump-to-group group)
7408             (gnus-group-next-unread-group 1)
7409             (gnus-configure-windows 'group 'force))
7410         (if (not (buffer-name (car quit-config)))
7411             (gnus-configure-windows 'group 'force)
7412           (set-buffer (car quit-config))
7413           (and (eq major-mode 'gnus-summary-mode)
7414                (gnus-set-global-variables))
7415           (gnus-configure-windows (cdr quit-config))))
7416       ;; Finally signal the quit.
7417       (signal 'quit nil))
7418      ;; The group was successfully selected.
7419      (t
7420       (gnus-set-global-variables)
7421       ;; Save the active value in effect when the group was entered.
7422       (setq gnus-newsgroup-active
7423             (gnus-copy-sequence
7424              (gnus-active gnus-newsgroup-name)))
7425       ;; You can change the summary buffer in some way with this hook.
7426       (run-hooks 'gnus-select-group-hook)
7427       ;; Set any local variables in the group parameters.
7428       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7429       (gnus-update-format-specifications)
7430       ;; Do score processing.
7431       (when gnus-use-scoring
7432         (gnus-possibly-score-headers))
7433       ;; Check whether to fill in the gaps in the threads.
7434       (when gnus-build-sparse-threads
7435         (gnus-build-sparse-threads))
7436       ;; Find the initial limit.
7437       (if show-all
7438           (let ((gnus-newsgroup-dormant nil))
7439             (gnus-summary-initial-limit show-all))
7440         (gnus-summary-initial-limit show-all))
7441       ;; Generate the summary buffer.
7442       (unless no-display
7443         (gnus-summary-prepare))
7444       (when gnus-use-trees
7445         (gnus-tree-open group)
7446         (setq gnus-summary-highlight-line-function
7447               'gnus-tree-highlight-article))
7448       ;; If the summary buffer is empty, but there are some low-scored
7449       ;; articles or some excluded dormants, we include these in the
7450       ;; buffer.
7451       (when (and (zerop (buffer-size))
7452                  (not no-display))
7453         (cond (gnus-newsgroup-dormant
7454                (gnus-summary-limit-include-dormant))
7455               ((and gnus-newsgroup-scored show-all)
7456                (gnus-summary-limit-include-expunged))))
7457       ;; Function `gnus-apply-kill-file' must be called in this hook.
7458       (run-hooks 'gnus-apply-kill-hook)
7459       (if (and (zerop (buffer-size))
7460                (not no-display))
7461           (progn
7462             ;; This newsgroup is empty.
7463             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7464             (gnus-message 6 "No unread news")
7465             (when kill-buffer
7466               (gnus-kill-or-deaden-summary kill-buffer))
7467             ;; Return nil from this function.
7468             nil)
7469         ;; Hide conversation thread subtrees.  We cannot do this in
7470         ;; gnus-summary-prepare-hook since kill processing may not
7471         ;; work with hidden articles.
7472         (and gnus-show-threads
7473              gnus-thread-hide-subtree
7474              (gnus-summary-hide-all-threads))
7475         ;; Show first unread article if requested.
7476         (if (and (not no-article)
7477                  (not no-display)
7478                  gnus-newsgroup-unreads
7479                  gnus-auto-select-first)
7480             (if (eq gnus-auto-select-first 'best)
7481                 (gnus-summary-best-unread-article)
7482               (gnus-summary-first-unread-article))
7483           ;; Don't select any articles, just move point to the first
7484           ;; article in the group.
7485           (goto-char (point-min))
7486           (gnus-summary-position-point)
7487           (gnus-set-mode-line 'summary)
7488           (gnus-configure-windows 'summary 'force))
7489         ;; If we are in async mode, we send some info to the backend.
7490         (when gnus-newsgroup-async
7491           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7492         (when kill-buffer
7493           (gnus-kill-or-deaden-summary kill-buffer))
7494         (when (get-buffer-window gnus-group-buffer t)
7495           ;; Gotta use windows, because recenter does wierd stuff if
7496           ;; the current buffer ain't the displayed window.
7497           (let ((owin (selected-window)))
7498             (select-window (get-buffer-window gnus-group-buffer t))
7499             (when (gnus-group-goto-group group)
7500               (recenter))
7501             (select-window owin))))
7502       ;; Mark this buffer as "prepared".
7503       (setq gnus-newsgroup-prepared t)
7504       t))))
7505
7506 (defun gnus-summary-prepare ()
7507   "Generate the summary buffer."
7508   (let ((buffer-read-only nil))
7509     (erase-buffer)
7510     (setq gnus-newsgroup-data nil
7511           gnus-newsgroup-data-reverse nil)
7512     (run-hooks 'gnus-summary-generate-hook)
7513     ;; Generate the buffer, either with threads or without.
7514     (when gnus-newsgroup-headers
7515       (gnus-summary-prepare-threads
7516        (if gnus-show-threads
7517            (gnus-sort-gathered-threads
7518             (funcall gnus-summary-thread-gathering-function
7519                      (gnus-sort-threads
7520                       (gnus-cut-threads (gnus-make-threads)))))
7521          ;; Unthreaded display.
7522          (gnus-sort-articles gnus-newsgroup-headers))))
7523     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7524     ;; Call hooks for modifying summary buffer.
7525     (goto-char (point-min))
7526     (run-hooks 'gnus-summary-prepare-hook)))
7527
7528 (defun gnus-gather-threads-by-subject (threads)
7529   "Gather threads by looking at Subject headers."
7530   (if (not gnus-summary-make-false-root)
7531       threads
7532     (let ((hashtb (gnus-make-hashtable 1023))
7533           (prev threads)
7534           (result threads)
7535           subject hthread whole-subject)
7536       (while threads
7537         (setq whole-subject (mail-header-subject (car (car threads))))
7538         (if (and gnus-summary-gather-exclude-subject
7539                  (string-match gnus-summary-gather-exclude-subject
7540                                whole-subject))
7541             () ; We don't want to do anything with this article.
7542           ;; We simplify the subject before looking it up in the
7543           ;; hash table.
7544           (setq subject
7545                 (cond
7546                  ;; Truncate the subject.
7547                  ((numberp gnus-summary-gather-subject-limit)
7548                   (setq subject (gnus-simplify-subject-re whole-subject))
7549                   (if (> (length subject) gnus-summary-gather-subject-limit)
7550                       (substring subject 0 gnus-summary-gather-subject-limit)
7551                     subject))
7552                  ;; Fuzzily simplify it.
7553                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7554                   (gnus-simplify-subject-fuzzy whole-subject))
7555                  ;; Just remove the leading "Re:".
7556                  (t
7557                   (gnus-simplify-subject-re whole-subject))))
7558
7559           (if (setq hthread (gnus-gethash subject hashtb))
7560               (progn
7561                 ;; We enter a dummy root into the thread, if we
7562                 ;; haven't done that already.
7563                 (unless (stringp (car (car hthread)))
7564                   (setcar hthread (list whole-subject (car hthread))))
7565                 ;; We add this new gathered thread to this gathered
7566                 ;; thread.
7567                 (setcdr (car hthread)
7568                         (nconc (cdr (car hthread)) (list (car threads))))
7569                 ;; Remove it from the list of threads.
7570                 (setcdr prev (cdr threads))
7571                 (setq threads prev))
7572             ;; Enter this thread into the hash table.
7573             (gnus-sethash subject threads hashtb)))
7574         (setq prev threads)
7575         (setq threads (cdr threads)))
7576       result)))
7577
7578 (defun gnus-summary-gather-threads-by-references (threads)
7579   "Gather threads by looking at References headers."
7580   (let ((idhashtb (gnus-make-hashtable 1023))
7581         (thhashtb (gnus-make-hashtable 1023))
7582         (prev threads)
7583         (result threads)
7584         ids references id gthread gid entered)
7585     (while threads
7586       (when (setq references (mail-header-references (caar threads)))
7587         (setq id (mail-header-id (caar threads)))
7588         (setq ids (gnus-split-references references))
7589         (setq entered nil)
7590         (while ids
7591           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7592               (progn
7593                 (gnus-sethash (car ids) id idhashtb)
7594                 (gnus-sethash id threads thhashtb))
7595             (setq gthread (gnus-gethash gid thhashtb))
7596             (unless entered
7597               ;; We enter a dummy root into the thread, if we
7598               ;; haven't done that already.
7599               (unless (stringp (caar gthread))
7600                 (setcar gthread (list (mail-header-subject (caar gthread))
7601                                       (car gthread))))
7602               ;; We add this new gathered thread to this gathered
7603               ;; thread.
7604               (setcdr (car gthread)
7605                       (nconc (cdar gthread) (list (car threads)))))
7606             ;; Add it into the thread hash table.
7607             (gnus-sethash id gthread thhashtb)
7608             (setq entered t)
7609             ;; Remove it from the list of threads.
7610             (setcdr prev (cdr threads))
7611             (setq threads prev))
7612           (setq ids (cdr ids))))
7613       (setq prev threads)
7614       (setq threads (cdr threads)))
7615     result))
7616
7617 (defun gnus-sort-gathered-threads (threads)
7618   "Sort subtreads inside each gathered thread by article number."
7619   (let ((result threads))
7620     (while threads
7621       (when (stringp (car (car threads)))
7622         (setcdr (car threads)
7623                 (sort (cdr (car threads)) 'gnus-thread-sort-by-number)))
7624       (setq threads (cdr threads)))
7625     result))
7626
7627 (defun gnus-make-threads ()
7628   "Go through the dependency hashtb and find the roots.  Return all threads."
7629   (let (threads)
7630     (mapatoms
7631      (lambda (refs)
7632        (unless (car (symbol-value refs))
7633          ;; These threads do not refer back to any other articles,
7634          ;; so they're roots.
7635          (setq threads (append (cdr (symbol-value refs)) threads))))
7636      gnus-newsgroup-dependencies)
7637     threads))
7638
7639 (defun gnus-build-sparse-threads ()
7640   (let ((headers gnus-newsgroup-headers)
7641         (deps gnus-newsgroup-dependencies)
7642         header references generation relations 
7643         cthread subject child end pthread relation)
7644     ;; First we create an alist of generations/relations, where 
7645     ;; generations is how much we trust the ralation, and the relation
7646     ;; is parent/child.
7647     (gnus-message 7 "Making sparse threads...")
7648     (save-excursion
7649       (nnheader-set-temp-buffer " *gnus sparse threads*")
7650       (while (setq header (pop headers))
7651         (when (and (setq references (mail-header-references header))
7652                    (not (string= references "")))
7653           (insert references)
7654           (setq child (downcase (mail-header-id header))
7655                 subject (mail-header-subject header))
7656           (setq generation 0)
7657           (while (search-backward ">" nil t)
7658             (setq end (1+ (point)))
7659             (when (search-backward "<" nil t)
7660               (push (list (incf generation) 
7661                           child (setq child (downcase
7662                                              (buffer-substring (point) end)))
7663                           subject)
7664                     relations)))
7665           (push (list (1+ generation) child nil subject) relations)
7666           (erase-buffer)))
7667       (kill-buffer (current-buffer)))
7668     ;; Sort over trustworthiness.
7669     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7670     (while (setq relation (pop relations))
7671       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7672                 (unless (car (symbol-value cthread))
7673                   ;; Make this article the parent of these threads.
7674                   (setcar (symbol-value cthread)
7675                           (vector gnus-reffed-article-number 
7676                                   (cadddr relation) 
7677                                   "" ""
7678                                   (cadr relation) 
7679                                   (or (caddr relation) "") 0 0 "")))
7680               (set cthread (list (vector gnus-reffed-article-number
7681                                          (cadddr relation) 
7682                                          "" "" (cadr relation) 
7683                                          (or (caddr relation) "") 0 0 ""))))
7684         (push gnus-reffed-article-number gnus-newsgroup-limit)
7685         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7686         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7687               gnus-newsgroup-reads)
7688         (decf gnus-reffed-article-number)
7689         ;; Make this new thread the child of its parent.
7690         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7691             (setcdr (symbol-value pthread)
7692                     (nconc (cdr (symbol-value pthread))
7693                            (list (symbol-value cthread))))
7694           (set pthread (list nil (symbol-value cthread))))))
7695     (gnus-message 7 "Making sparse threads...done")))
7696
7697 (defun gnus-build-old-threads ()
7698   ;; Look at all the articles that refer back to old articles, and
7699   ;; fetch the headers for the articles that aren't there.  This will
7700   ;; build complete threads - if the roots haven't been expired by the
7701   ;; server, that is.
7702   (let (id heads)
7703     (mapatoms
7704      (lambda (refs)
7705        (when (not (car (symbol-value refs)))
7706          (setq heads (cdr (symbol-value refs)))
7707          (while heads
7708            (if (memq (mail-header-number (car (car heads)))
7709                      gnus-newsgroup-dormant)
7710                (setq heads (cdr heads))
7711              (setq id (symbol-name refs))
7712              (while (and (setq id (gnus-build-get-header id))
7713                          (not (car (gnus-gethash
7714                                     id gnus-newsgroup-dependencies)))))
7715              (setq heads nil)))))
7716      gnus-newsgroup-dependencies)))
7717
7718 (defun gnus-build-get-header (id)
7719   ;; Look through the buffer of NOV lines and find the header to
7720   ;; ID.  Enter this line into the dependencies hash table, and return
7721   ;; the id of the parent article (if any).
7722   (let ((deps gnus-newsgroup-dependencies)
7723         found header)
7724     (prog1
7725         (save-excursion
7726           (set-buffer nntp-server-buffer)
7727           (goto-char (point-min))
7728           (while (and (not found) (search-forward id nil t))
7729             (beginning-of-line)
7730             (setq found (looking-at
7731                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7732                                  (regexp-quote id))))
7733             (or found (beginning-of-line 2)))
7734           (when found
7735             (let (ref)
7736               (beginning-of-line)
7737               (and
7738                (setq header (gnus-nov-parse-line
7739                              (read (current-buffer)) deps))
7740                (gnus-parent-id (mail-header-references header))))))
7741       (when header
7742         (let ((number (mail-header-number header)))
7743           (push number gnus-newsgroup-limit)
7744           (push header gnus-newsgroup-headers)
7745           (if (memq number gnus-newsgroup-unselected)
7746               (progn
7747                 (push number gnus-newsgroup-unreads)
7748                 (setq gnus-newsgroup-unselected
7749                       (delq number gnus-newsgroup-unselected)))
7750             (push number gnus-newsgroup-ancient)))))))
7751
7752 (defun gnus-summary-update-article (article &optional header)
7753   "Update ARTICLE in the summary buffer."
7754   (let ((id (mail-header-id (gnus-summary-article-header article)))
7755         (data (gnus-data-find article)))
7756     (setcar (gnus-id-to-thread id) nil)
7757     (gnus-summary-insert-subject id)
7758     ;; Set the (possibly) new article number in the data structure.
7759     (gnus-data-set-number data (gnus-id-to-article id))))
7760
7761 (defun gnus-rebuild-thread (id)
7762   "Rebuild the thread containing ID."
7763   (let ((dep gnus-newsgroup-dependencies)
7764         (buffer-read-only nil)
7765         current headers refs thread art data)
7766     (if (not gnus-show-threads)
7767         (setq thread (list (car (gnus-id-to-thread id))))
7768       ;; Get the thread this article is part of.
7769       (setq thread (gnus-remove-thread id)))
7770     (setq current (save-excursion
7771                     (and (zerop (forward-line -1))
7772                          (gnus-summary-article-number))))
7773     ;; If this is a gathered thread, we have to go some re-gathering.
7774     (when (stringp (car thread))
7775       (let ((subject (car thread))
7776             roots thr)
7777         (setq thread (cdr thread))
7778         (while thread
7779           (unless (memq (setq thr (gnus-id-to-thread
7780                                       (gnus-root-id
7781                                        (mail-header-id (car (car thread))))))
7782                         roots)
7783             (push thr roots))
7784           (setq thread (cdr thread)))
7785         ;; We now have all (unique) roots.
7786         (if (= (length roots) 1)
7787             ;; All the loose roots are now one solid root.
7788             (setq thread (car roots))
7789           (setq thread (cons subject (gnus-sort-threads roots))))))
7790     (let ((beg (point))
7791           threads)
7792       ;; We then insert this thread into the summary buffer.
7793       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7794         (gnus-summary-prepare-threads (list thread))
7795         (setq data (nreverse gnus-newsgroup-data))
7796         (setq threads gnus-newsgroup-threads))
7797       ;; We splice the new data into the data structure.
7798       (gnus-data-enter-list current data)
7799       (gnus-data-compute-positions)
7800       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7801
7802 (defun gnus-id-to-thread (id)
7803   "Return the (sub-)thread where ID appears."
7804   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7805
7806 (defun gnus-id-to-article (id)
7807   "Return the article number of ID."
7808   (let ((thread (gnus-id-to-thread id)))
7809     (when thread
7810       (mail-header-number (car thread)))))
7811
7812 (defun gnus-id-to-header (id)
7813   "Return the article headers of ID."
7814   (car (gnus-id-to-thread id)))
7815
7816 (defun gnus-article-displayed-root-p (article)
7817   "Say whether ARTICLE is a root(ish) article."
7818   (let ((level (gnus-summary-thread-level article))
7819         particle)
7820     (cond 
7821      ((null level) nil)
7822      ((zerop level) t)
7823      ((and (= 1 level)
7824            (null (setq particle (gnus-id-to-article
7825                                  (gnus-parent-id 
7826                                   (mail-header-references 
7827                                    (gnus-summary-article-header article))))))
7828            (null (gnus-summary-thread-level particle)))))))
7829
7830 (defun gnus-root-id (id)
7831   "Return the id of the root of the thread where ID appears."
7832   (let (last-id prev)
7833     (while (and id (setq prev (car (gnus-gethash
7834                                     (downcase id)
7835                                     gnus-newsgroup-dependencies))))
7836       (setq last-id id
7837             id (gnus-parent-id (mail-header-references prev))))
7838     last-id))
7839
7840 (defun gnus-remove-thread (id &optional dont-remove)
7841   "Remove the thread that has ID in it."
7842   (let ((dep gnus-newsgroup-dependencies)
7843         headers thread prev last-id)
7844     ;; First go up in this thread until we find the root.
7845     (setq last-id (gnus-root-id id))
7846     (setq headers (list (car (gnus-id-to-thread last-id))
7847                         (car (car (cdr (gnus-id-to-thread last-id))))))
7848     ;; We have now found the real root of this thread.  It might have
7849     ;; been gathered into some loose thread, so we have to search
7850     ;; through the threads to find the thread we wanted.
7851     (let ((threads gnus-newsgroup-threads)
7852           sub)
7853       (while threads
7854         (setq sub (car threads))
7855         (if (stringp (car sub))
7856             ;; This is a gathered threads, so we look at the roots
7857             ;; below it to find whether this article in in this
7858             ;; gathered root.
7859             (progn
7860               (setq sub (cdr sub))
7861               (while sub
7862                 (when (member (car (car sub)) headers)
7863                   (setq thread (car threads)
7864                         threads nil
7865                         sub nil))
7866                 (setq sub (cdr sub))))
7867           ;; It's an ordinary thread, so we check it.
7868           (when (eq (car sub) (car headers))
7869             (setq thread sub
7870                   threads nil)))
7871         (setq threads (cdr threads)))
7872       ;; If this article is in no thread, then it's a root.
7873       (if thread
7874           (unless dont-remove
7875             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7876         (setq thread (gnus-gethash (downcase last-id) dep)))
7877       (when thread
7878         (prog1
7879             thread ; We return this thread.
7880           (unless dont-remove
7881             (if (stringp (car thread))
7882                 (progn
7883                   ;; If we use dummy roots, then we have to remove the
7884                   ;; dummy root as well.
7885                   (when (eq gnus-summary-make-false-root 'dummy)
7886                     ;; Uhm.
7887                     )
7888                   (setq thread (cdr thread))
7889                   (while thread
7890                     (gnus-remove-thread-1 (car thread))
7891                     (setq thread (cdr thread))))
7892               (gnus-remove-thread-1 thread))))))))
7893
7894 (defun gnus-remove-thread-1 (thread)
7895   "Remove the thread THREAD recursively."
7896   (let ((number (mail-header-number (car thread)))
7897         pos)
7898     (when (setq pos (text-property-any
7899                      (point-min) (point-max) 'gnus-number number))
7900       (goto-char pos)
7901       (gnus-delete-line)
7902       (gnus-data-remove number))
7903     (setq thread (cdr thread))
7904     (while thread
7905       (gnus-remove-thread-1 (car thread))
7906       (setq thread (cdr thread)))))
7907
7908 (defun gnus-sort-threads (threads)
7909   "Sort THREADS."
7910   (if (not gnus-thread-sort-functions)
7911       threads
7912     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7913                     (car gnus-thread-sort-functions)
7914                   `(lambda (t1 t2)
7915                      ,(gnus-make-sort-function 
7916                        (reverse gnus-thread-sort-functions))))))
7917       (gnus-message 7 "Sorting threads...")
7918       (prog1
7919           (sort threads func)
7920         (gnus-message 7 "Sorting threads...done")))))
7921
7922 (defun gnus-sort-articles (articles)
7923   "Sort ARTICLES."
7924   (when gnus-article-sort-functions
7925     (let ((func (if (= 1 (length gnus-article-sort-functions))
7926                     (car gnus-article-sort-functions)
7927                   `(lambda (t1 t2)
7928                      ,(gnus-make-sort-function 
7929                        (reverse gnus-article-sort-functions))))))
7930       (gnus-message 7 "Sorting articles...")
7931       (prog1
7932           (setq gnus-newsgroup-headers (sort articles func))
7933         (gnus-message 7 "Sorting articles...done")))))
7934
7935 (defun gnus-make-sort-function (funs)
7936   "Return a composite sort condition based on the functions in FUNC."
7937   (if (cdr funs)
7938       `(or (,(car funs) t1 t2)
7939            (and (not (,(car funs) t2 t1))
7940                 ,(gnus-make-sort-function (cdr funs))))
7941     `(,(car funs) t1 t2)))
7942                  
7943 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7944 (defmacro gnus-thread-header (thread)
7945   ;; Return header of first article in THREAD.
7946   ;; Note that THREAD must never, ever be anything else than a variable -
7947   ;; using some other form will lead to serious barfage.
7948   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7949   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7950   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
7951         (vector thread) 2))
7952
7953 (defsubst gnus-article-sort-by-number (h1 h2)
7954   "Sort articles by article number."
7955   (< (mail-header-number h1)
7956      (mail-header-number h2)))
7957
7958 (defun gnus-thread-sort-by-number (h1 h2)
7959   "Sort threads by root article number."
7960   (gnus-article-sort-by-number
7961    (gnus-thread-header h1) (gnus-thread-header h2)))
7962
7963 (defsubst gnus-article-sort-by-author (h1 h2)
7964   "Sort articles by root author."
7965   (string-lessp
7966    (let ((extract (funcall
7967                    gnus-extract-address-components
7968                    (mail-header-from h1))))
7969      (or (car extract) (cdr extract)))
7970    (let ((extract (funcall
7971                    gnus-extract-address-components
7972                    (mail-header-from h2))))
7973      (or (car extract) (cdr extract)))))
7974
7975 (defun gnus-thread-sort-by-author (h1 h2)
7976   "Sort threads by root author."
7977   (gnus-article-sort-by-author
7978    (gnus-thread-header h1)  (gnus-thread-header h2)))
7979
7980 (defsubst gnus-article-sort-by-subject (h1 h2)
7981   "Sort articles by root subject."
7982   (string-lessp
7983    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
7984    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
7985
7986 (defun gnus-thread-sort-by-subject (h1 h2)
7987   "Sort threads by root subject."
7988   (gnus-article-sort-by-subject
7989    (gnus-thread-header h1) (gnus-thread-header h2)))
7990
7991 (defsubst gnus-article-sort-by-date (h1 h2)
7992   "Sort articles by root article date."
7993   (string-lessp
7994    (gnus-sortable-date (mail-header-date h1))
7995    (gnus-sortable-date (mail-header-date h2))))
7996
7997 (defun gnus-thread-sort-by-date (h1 h2)
7998   "Sort threads by root article date."
7999   (gnus-article-sort-by-date
8000    (gnus-thread-header h1) (gnus-thread-header h2)))
8001
8002 (defsubst gnus-article-sort-by-score (h1 h2)
8003   "Sort articles by root article score.
8004 Unscored articles will be counted as having a score of zero."
8005   (> (or (cdr (assq (mail-header-number h1)
8006                     gnus-newsgroup-scored))
8007          gnus-summary-default-score 0)
8008      (or (cdr (assq (mail-header-number h2)
8009                     gnus-newsgroup-scored))
8010          gnus-summary-default-score 0)))
8011
8012 (defun gnus-thread-sort-by-score (h1 h2)
8013   "Sort threads by root article score."
8014   (gnus-article-sort-by-score
8015    (gnus-thread-header h1) (gnus-thread-header h2)))
8016
8017 (defun gnus-thread-sort-by-total-score (h1 h2)
8018   "Sort threads by the sum of all scores in the thread.
8019 Unscored articles will be counted as having a score of zero."
8020   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8021
8022 (defun gnus-thread-total-score (thread)
8023   ;;  This function find the total score of THREAD.
8024   (if (consp thread)
8025       (if (stringp (car thread))
8026           (apply gnus-thread-score-function 0
8027                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8028         (gnus-thread-total-score-1 thread))
8029     (gnus-thread-total-score-1 (list thread))))
8030
8031 (defun gnus-thread-total-score-1 (root)
8032   ;; This function find the total score of the thread below ROOT.
8033   (setq root (car root))
8034   (apply gnus-thread-score-function
8035          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8036              gnus-summary-default-score 0)
8037          (mapcar 'gnus-thread-total-score
8038                  (cdr (gnus-gethash (downcase (mail-header-id root))
8039                                     gnus-newsgroup-dependencies)))))
8040
8041 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8042 (defvar gnus-tmp-prev-subject nil)
8043 (defvar gnus-tmp-false-parent nil)
8044 (defvar gnus-tmp-root-expunged nil)
8045 (defvar gnus-tmp-dummy-line nil)
8046
8047 (defun gnus-summary-prepare-threads (threads)
8048   "Prepare summary buffer from THREADS and indentation LEVEL.
8049 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8050 or a straight list of headers."
8051   (gnus-message 7 "Generating summary...")
8052
8053   (setq gnus-newsgroup-threads threads)
8054   (beginning-of-line)
8055
8056   (let ((gnus-tmp-level 0)
8057         (default-score (or gnus-summary-default-score 0))
8058         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8059         thread number subject stack state gnus-tmp-gathered beg-match
8060         new-roots gnus-tmp-new-adopts thread-end
8061         gnus-tmp-header gnus-tmp-unread
8062         gnus-tmp-replied gnus-tmp-subject-or-nil
8063         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8064         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8065         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8066
8067     (setq gnus-tmp-prev-subject nil)
8068
8069     (if (vectorp (car threads))
8070         ;; If this is a straight (sic) list of headers, then a
8071         ;; threaded summary display isn't required, so we just create
8072         ;; an unthreaded one.
8073         (gnus-summary-prepare-unthreaded threads)
8074
8075       ;; Do the threaded display.
8076
8077       (while (or threads stack gnus-tmp-new-adopts new-roots)
8078
8079         (if (and (= gnus-tmp-level 0)
8080                  (not (setq gnus-tmp-dummy-line nil))
8081                  (or (not stack)
8082                      (= (car (car stack)) 0))
8083                  (not gnus-tmp-false-parent)
8084                  (or gnus-tmp-new-adopts new-roots))
8085             (if gnus-tmp-new-adopts
8086                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8087                       thread (list (car gnus-tmp-new-adopts))
8088                       gnus-tmp-header (car (car thread))
8089                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8090               (if new-roots
8091                   (setq thread (list (car new-roots))
8092                         gnus-tmp-header (car (car thread))
8093                         new-roots (cdr new-roots))))
8094
8095           (if threads
8096               ;; If there are some threads, we do them before the
8097               ;; threads on the stack.
8098               (setq thread threads
8099                     gnus-tmp-header (car (car thread)))
8100             ;; There were no current threads, so we pop something off
8101             ;; the stack.
8102             (setq state (car stack)
8103                   gnus-tmp-level (car state)
8104                   thread (cdr state)
8105                   stack (cdr stack)
8106                   gnus-tmp-header (car (car thread)))))
8107
8108         (setq gnus-tmp-false-parent nil)
8109         (setq gnus-tmp-root-expunged nil)
8110         (setq thread-end nil)
8111
8112         (if (stringp gnus-tmp-header)
8113             ;; The header is a dummy root.
8114             (cond
8115              ((eq gnus-summary-make-false-root 'adopt)
8116               ;; We let the first article adopt the rest.
8117               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8118                                                (cdr (cdr (car thread)))))
8119               (setq gnus-tmp-gathered
8120                     (nconc (mapcar
8121                             (lambda (h) (mail-header-number (car h)))
8122                             (cdr (cdr (car thread))))
8123                            gnus-tmp-gathered))
8124               (setq thread (cons (list (car (car thread))
8125                                        (car (cdr (car thread))))
8126                                  (cdr thread)))
8127               (setq gnus-tmp-level -1
8128                     gnus-tmp-false-parent t))
8129              ((eq gnus-summary-make-false-root 'empty)
8130               ;; We print adopted articles with empty subject fields.
8131               (setq gnus-tmp-gathered
8132                     (nconc (mapcar
8133                             (lambda (h) (mail-header-number (car h)))
8134                             (cdr (cdr (car thread))))
8135                            gnus-tmp-gathered))
8136               (setq gnus-tmp-level -1))
8137              ((eq gnus-summary-make-false-root 'dummy)
8138               ;; We remember that we probably want to output a dummy
8139               ;; root.
8140               (setq gnus-tmp-dummy-line gnus-tmp-header)
8141               (setq gnus-tmp-prev-subject gnus-tmp-header))
8142              (t
8143               ;; We do not make a root for the gathered
8144               ;; sub-threads at all.
8145               (setq gnus-tmp-level -1)))
8146
8147           (setq number (mail-header-number gnus-tmp-header)
8148                 subject (mail-header-subject gnus-tmp-header))
8149
8150           (cond
8151            ;; If the thread has changed subject, we might want to make
8152            ;; this subthread into a root.
8153            ((and (null gnus-thread-ignore-subject)
8154                  (not (zerop gnus-tmp-level))
8155                  gnus-tmp-prev-subject
8156                  (not (inline
8157                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8158             (setq new-roots (nconc new-roots (list (car thread)))
8159                   thread-end t
8160                   gnus-tmp-header nil))
8161            ;; If the article lies outside the current limit,
8162            ;; then we do not display it.
8163            ((and (not (memq number gnus-newsgroup-limit))
8164                  (not gnus-tmp-dummy-line))
8165             (setq gnus-tmp-gathered
8166                   (nconc (mapcar
8167                           (lambda (h) (mail-header-number (car h)))
8168                           (cdr (car thread)))
8169                          gnus-tmp-gathered))
8170             (setq gnus-tmp-new-adopts (if (cdr (car thread))
8171                                           (append gnus-tmp-new-adopts
8172                                                   (cdr (car thread)))
8173                                         gnus-tmp-new-adopts)
8174                   thread-end t
8175                   gnus-tmp-header nil)
8176             (when (zerop gnus-tmp-level)
8177               (setq gnus-tmp-root-expunged t)))
8178            ;; Perhaps this article is to be marked as read?
8179            ((and gnus-summary-mark-below
8180                  (< (or (cdr (assq number gnus-newsgroup-scored))
8181                         default-score)
8182                     gnus-summary-mark-below))
8183             (setq gnus-newsgroup-unreads
8184                   (delq number gnus-newsgroup-unreads))
8185             (if gnus-newsgroup-auto-expire
8186                 (push number gnus-newsgroup-expirable)
8187               (push (cons number gnus-low-score-mark)
8188                     gnus-newsgroup-reads))))
8189
8190           (when gnus-tmp-header
8191             ;; We may have an old dummy line to output before this
8192             ;; article.
8193             (when gnus-tmp-dummy-line
8194               (gnus-summary-insert-dummy-line
8195                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8196               (setq gnus-tmp-dummy-line nil))
8197
8198             ;; Compute the mark.
8199             (setq
8200              gnus-tmp-unread
8201              (cond
8202               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8203               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8204               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8205               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8206               (t (or (cdr (assq number gnus-newsgroup-reads))
8207                      gnus-ancient-mark))))
8208
8209             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8210                                   gnus-tmp-header gnus-tmp-level)
8211                   gnus-newsgroup-data)
8212
8213             ;; Actually insert the line.
8214             (setq
8215              gnus-tmp-subject-or-nil
8216              (cond
8217               ((and gnus-thread-ignore-subject
8218                     gnus-tmp-prev-subject
8219                     (not (inline (gnus-subject-equal
8220                                   gnus-tmp-prev-subject subject))))
8221                subject)
8222               ((zerop gnus-tmp-level)
8223                (if (and (eq gnus-summary-make-false-root 'empty)
8224                         (memq number gnus-tmp-gathered)
8225                         gnus-tmp-prev-subject
8226                         (inline (gnus-subject-equal
8227                                  gnus-tmp-prev-subject subject)))
8228                    gnus-summary-same-subject
8229                  subject))
8230               (t gnus-summary-same-subject)))
8231             (if (and (eq gnus-summary-make-false-root 'adopt)
8232                      (= gnus-tmp-level 1)
8233                      (memq number gnus-tmp-gathered))
8234                 (setq gnus-tmp-opening-bracket ?\<
8235                       gnus-tmp-closing-bracket ?\>)
8236               (setq gnus-tmp-opening-bracket ?\[
8237                     gnus-tmp-closing-bracket ?\]))
8238             (setq
8239              gnus-tmp-indentation
8240              (aref gnus-thread-indent-array gnus-tmp-level)
8241              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8242              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8243                                 gnus-summary-default-score 0)
8244              gnus-tmp-score-char
8245              (if (or (null gnus-summary-default-score)
8246                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8247                          gnus-summary-zcore-fuzz)) ? 
8248                (if (< gnus-tmp-score gnus-summary-default-score)
8249                    gnus-score-below-mark gnus-score-over-mark))
8250              gnus-tmp-replied
8251              (cond ((memq number gnus-newsgroup-processable)
8252                     gnus-process-mark)
8253                    ((memq number gnus-newsgroup-cached)
8254                     gnus-cached-mark)
8255                    ((memq number gnus-newsgroup-replied)
8256                     gnus-replied-mark)
8257                    (t gnus-unread-mark))
8258              gnus-tmp-from (mail-header-from gnus-tmp-header)
8259              gnus-tmp-name
8260              (cond
8261               ((string-match "(.+)" gnus-tmp-from)
8262                (substring gnus-tmp-from
8263                           (1+ (match-beginning 0)) (1- (match-end 0))))
8264               ((string-match "<[^>]+> *$" gnus-tmp-from)
8265                (setq beg-match (match-beginning 0))
8266                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8267                         (substring gnus-tmp-from (1+ (match-beginning 0))
8268                                    (1- (match-end 0))))
8269                    (substring gnus-tmp-from 0 beg-match)))
8270               (t gnus-tmp-from)))
8271             (when (string= gnus-tmp-name "")
8272               (setq gnus-tmp-name gnus-tmp-from))
8273             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8274             (put-text-property
8275              (point)
8276              (progn (eval gnus-summary-line-format-spec) (point))
8277              'gnus-number number)
8278             (when gnus-visual-p
8279               (forward-line -1)
8280               (run-hooks 'gnus-summary-update-hook)
8281               (forward-line 1))
8282
8283             (setq gnus-tmp-prev-subject subject)))
8284
8285         (when (nth 1 thread)
8286           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8287         (incf gnus-tmp-level)
8288         (setq threads (if thread-end nil (cdr (car thread))))
8289         (unless threads
8290           (setq gnus-tmp-level 0)))))
8291   (gnus-message 7 "Generating summary...done"))
8292
8293 (defun gnus-summary-prepare-unthreaded (headers)
8294   "Generate an unthreaded summary buffer based on HEADERS."
8295   (let (header number mark)
8296
8297     (while headers
8298       (setq header (car headers)
8299             headers (cdr headers)
8300             number (mail-header-number header))
8301
8302       ;; We may have to root out some bad articles...
8303       (when (memq number gnus-newsgroup-limit)
8304         (when (and gnus-summary-mark-below
8305                    (< (or (cdr (assq number gnus-newsgroup-scored))
8306                           gnus-summary-default-score 0)
8307                       gnus-summary-mark-below))
8308           (setq gnus-newsgroup-unreads
8309                 (delq number gnus-newsgroup-unreads))
8310           (if gnus-newsgroup-auto-expire
8311               (push number gnus-newsgroup-expirable)
8312             (push (cons number gnus-low-score-mark)
8313                   gnus-newsgroup-reads)))
8314
8315         (setq mark
8316               (cond
8317                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8318                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8319                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8320                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8321                (t (or (cdr (assq number gnus-newsgroup-reads))
8322                       gnus-ancient-mark))))
8323         (setq gnus-newsgroup-data
8324               (cons (gnus-data-make number mark (1+ (point)) header 0)
8325                     gnus-newsgroup-data))
8326         (gnus-summary-insert-line
8327          header 0 nil mark (memq number gnus-newsgroup-replied)
8328          (memq number gnus-newsgroup-expirable)
8329          (mail-header-subject header) nil
8330          (cdr (assq number gnus-newsgroup-scored))
8331          (memq number gnus-newsgroup-processable))))))
8332
8333 (defun gnus-select-newsgroup (group &optional read-all)
8334   "Select newsgroup GROUP.
8335 If READ-ALL is non-nil, all articles in the group are selected."
8336   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8337          (info (nth 2 entry))
8338          articles fetched-articles cached)
8339
8340     (or (gnus-check-server
8341          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8342         (error "Couldn't open server"))
8343
8344     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8345         (gnus-activate-group group) ; Or we can activate it...
8346         (progn ; Or we bug out.
8347           (kill-buffer (current-buffer))
8348           (error "Couldn't request group %s: %s"
8349                  group (gnus-status-message group))))
8350
8351     (setq gnus-newsgroup-name group)
8352     (setq gnus-newsgroup-unselected nil)
8353     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8354
8355     (and gnus-asynchronous
8356          (gnus-check-backend-function
8357           'request-asynchronous gnus-newsgroup-name)
8358          (setq gnus-newsgroup-async
8359                (gnus-request-asynchronous gnus-newsgroup-name)))
8360
8361     ;; Adjust and set lists of article marks.
8362     (when info
8363       (gnus-adjust-marked-articles info))
8364
8365     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8366     (when (gnus-virtual-group-p group)
8367       (setq cached gnus-newsgroup-cached))
8368
8369     (setq gnus-newsgroup-unreads
8370           (gnus-set-difference
8371            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8372            gnus-newsgroup-dormant))
8373
8374     (setq gnus-newsgroup-processable nil)
8375
8376     (setq articles (gnus-articles-to-read group read-all))
8377
8378     (cond
8379      ((null articles)
8380       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8381       'quit)
8382      ((eq articles 0) nil)
8383      (t
8384       ;; Init the dependencies hash table.
8385       (setq gnus-newsgroup-dependencies
8386             (gnus-make-hashtable (length articles)))
8387       ;; Retrieve the headers and read them in.
8388       (gnus-message 5 "Fetching headers...")
8389       (setq gnus-newsgroup-headers
8390             (if (eq 'nov
8391                     (setq gnus-headers-retrieved-by
8392                           (gnus-retrieve-headers
8393                            articles gnus-newsgroup-name
8394                            ;; We might want to fetch old headers, but
8395                            ;; not if there is only 1 article.
8396                            (and gnus-fetch-old-headers
8397                                 (or (and
8398                                      (not (eq gnus-fetch-old-headers 'some))
8399                                      (not (numberp gnus-fetch-old-headers)))
8400                                     (> (length articles) 1))))))
8401                 (gnus-get-newsgroup-headers-xover articles)
8402               (gnus-get-newsgroup-headers)))
8403       (gnus-message 5 "Fetching headers...done")
8404
8405       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8406       (when cached
8407         (setq gnus-newsgroup-cached cached))
8408
8409       ;; Set the initial limit.
8410       (setq gnus-newsgroup-limit (copy-sequence articles))
8411       ;; Remove canceled articles from the list of unread articles.
8412       (setq gnus-newsgroup-unreads
8413             (gnus-set-sorted-intersection
8414              gnus-newsgroup-unreads
8415              (setq fetched-articles
8416                    (mapcar (lambda (headers) (mail-header-number headers))
8417                            gnus-newsgroup-headers))))
8418       ;; Removed marked articles that do not exist.
8419       (gnus-update-missing-marks
8420        (gnus-sorted-complement fetched-articles articles))
8421       ;; We might want to build some more threads first.
8422       (and gnus-fetch-old-headers
8423            (eq gnus-headers-retrieved-by 'nov)
8424            (gnus-build-old-threads))
8425       ;; Check whether auto-expire is to be done in this group.
8426       (setq gnus-newsgroup-auto-expire
8427             (gnus-group-auto-expirable-p group))
8428       ;; First and last article in this newsgroup.
8429       (and gnus-newsgroup-headers
8430            (setq gnus-newsgroup-begin
8431                  (mail-header-number (car gnus-newsgroup-headers)))
8432            (setq gnus-newsgroup-end
8433                  (mail-header-number
8434                   (gnus-last-element gnus-newsgroup-headers))))
8435       (setq gnus-reffed-article-number -1)
8436       ;; GROUP is successfully selected.
8437       (or gnus-newsgroup-headers t)))))
8438
8439 (defun gnus-articles-to-read (group read-all)
8440   ;; Find out what articles the user wants to read.
8441   (let* ((articles
8442           ;; Select all articles if `read-all' is non-nil, or if there
8443           ;; are no unread articles.
8444           (if (or read-all
8445                   (and (zerop (length gnus-newsgroup-marked))
8446                        (zerop (length gnus-newsgroup-unreads))))
8447               (gnus-uncompress-range (gnus-active group))
8448             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8449                           (copy-sequence gnus-newsgroup-unreads))
8450                   '<)))
8451          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8452          (scored (length scored-list))
8453          (number (length articles))
8454          (marked (+ (length gnus-newsgroup-marked)
8455                     (length gnus-newsgroup-dormant)))
8456          (select
8457           (cond
8458            ((numberp read-all)
8459             read-all)
8460            (t
8461             (condition-case ()
8462                 (cond
8463                  ((and (or (<= scored marked) (= scored number))
8464                        (numberp gnus-large-newsgroup)
8465                        (> number gnus-large-newsgroup))
8466                   (let ((input
8467                          (read-string
8468                           (format
8469                            "How many articles from %s (default %d): "
8470                            gnus-newsgroup-name number))))
8471                     (if (string-match "^[ \t]*$" input) number input)))
8472                  ((and (> scored marked) (< scored number))
8473                   (let ((input
8474                          (read-string
8475                           (format "%s %s (%d scored, %d total): "
8476                                   "How many articles from"
8477                                   group scored number))))
8478                     (if (string-match "^[ \t]*$" input)
8479                         number input)))
8480                  (t number))
8481               (quit nil))))))
8482     (setq select (if (stringp select) (string-to-number select) select))
8483     (if (or (null select) (zerop select))
8484         select
8485       (if (and (not (zerop scored)) (<= (abs select) scored))
8486           (progn
8487             (setq articles (sort scored-list '<))
8488             (setq number (length articles)))
8489         (setq articles (copy-sequence articles)))
8490
8491       (if (< (abs select) number)
8492           (if (< select 0)
8493               ;; Select the N oldest articles.
8494               (setcdr (nthcdr (1- (abs select)) articles) nil)
8495             ;; Select the N most recent articles.
8496             (setq articles (nthcdr (- number select) articles))))
8497       (setq gnus-newsgroup-unselected
8498             (gnus-sorted-intersection
8499              gnus-newsgroup-unreads
8500              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8501       articles)))
8502
8503 (defun gnus-killed-articles (killed articles)
8504   (let (out)
8505     (while articles
8506       (if (inline (gnus-member-of-range (car articles) killed))
8507           (setq out (cons (car articles) out)))
8508       (setq articles (cdr articles)))
8509     out))
8510
8511 (defun gnus-uncompress-marks (marks)
8512   "Uncompress the mark ranges in MARKS."
8513   (let ((uncompressed '(score bookmark))
8514         out)
8515     (while marks
8516       (if (memq (caar marks) uncompressed)
8517           (push (car marks) out)
8518         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8519       (setq marks (cdr marks)))
8520     out))
8521
8522 (defun gnus-adjust-marked-articles (info)
8523   "Set all article lists and remove all marks that are no longer legal."
8524   (let* ((marked-lists (gnus-info-marks info))
8525          (active (gnus-active (gnus-info-group info)))
8526          (min (car active))
8527          (max (cdr active))
8528          (types gnus-article-mark-lists)
8529          (uncompressed '(score bookmark))
8530          marks var articles article mark)
8531
8532     (while marked-lists
8533       (setq marks (pop marked-lists))
8534       (set (setq var (intern (format "gnus-newsgroup-%s"
8535                                      (car (rassq (setq mark (car marks))
8536                                                  types)))))
8537            (if (memq (car marks) uncompressed) (cdr marks)
8538              (gnus-uncompress-range (cdr marks))))
8539
8540       (setq articles (symbol-value var))
8541
8542       ;; All articles have to be subsets of the active articles.
8543       (cond
8544        ;; Adjust "simple" lists.
8545        ((memq mark '(tick dormant expirable reply killed save))
8546         (while articles
8547           (when (or (< (setq article (pop articles)) min) (> article max))
8548             (set var (delq article (symbol-value var))))))
8549        ;; Adjust assocs.
8550        ((memq mark '(score bookmark))
8551         (while articles
8552           (when (or (< (car (setq article (pop articles))) min)
8553                     (> (car article) max))
8554             (set var (delq article (symbol-value var))))))))))
8555
8556 (defun gnus-update-missing-marks (missing)
8557   "Go through the list of MISSING articles and remove them mark lists."
8558   (when missing
8559     (let ((types gnus-article-mark-lists)
8560           var m)
8561       ;; Go through all types.
8562       (while types
8563         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8564         (when (symbol-value var)
8565           ;; This list has articles.  So we delete all missing articles
8566           ;; from it.
8567           (setq m missing)
8568           (while m
8569             (set var (delq (pop m) (symbol-value var)))))))))
8570
8571 (defun gnus-update-marks ()
8572   "Enter the various lists of marked articles into the newsgroup info list."
8573   (let ((types gnus-article-mark-lists)
8574         (info (gnus-get-info gnus-newsgroup-name))
8575         (uncompressed '(score bookmark killed))
8576         var type list newmarked symbol)
8577     (when info
8578       ;; Add all marks lists that are non-nil to the list of marks lists.
8579       (while types
8580         (setq type (pop types))
8581         (when (setq list (symbol-value
8582                           (setq symbol
8583                                 (intern (format "gnus-newsgroup-%s"
8584                                                 (car type))))))
8585           (push (cons (cdr type)
8586                       (if (memq (cdr type) uncompressed) list
8587                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8588                 newmarked)))
8589
8590       ;; Enter these new marks into the info of the group.
8591       (if (nthcdr 3 info)
8592           (setcar (nthcdr 3 info) newmarked)
8593         ;; Add the marks lists to the end of the info.
8594         (when newmarked
8595           (setcdr (nthcdr 2 info) (list newmarked))))
8596
8597       ;; Cut off the end of the info if there's nothing else there.
8598       (let ((i 5))
8599         (while (and (> i 2)
8600                     (not (nth i info)))
8601           (when (nthcdr (decf i) info)
8602             (setcdr (nthcdr i info) nil)))))))
8603
8604 (defun gnus-add-marked-articles (group type articles &optional info force)
8605   ;; Add ARTICLES of TYPE to the info of GROUP.
8606   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8607   ;; add, but replace marked articles of TYPE with ARTICLES.
8608   (let ((info (or info (gnus-get-info group)))
8609         (uncompressed '(score bookmark killed))
8610         marked m)
8611     (or (not info)
8612         (and (not (setq marked (nthcdr 3 info)))
8613              (setcdr (nthcdr 2 info)
8614                      (list (list (cons type (gnus-compress-sequence
8615                                              articles t))))))
8616         (and (not (setq m (assq type (car marked))))
8617              (setcar marked
8618                      (cons (cons type (gnus-compress-sequence articles t) )
8619                            (car marked))))
8620         (if force
8621             (setcdr m (gnus-compress-sequence articles t))
8622           (setcdr m (gnus-compress-sequence
8623                      (sort (nconc (gnus-uncompress-range m)
8624                                   (copy-sequence articles)) '<) t))))))
8625
8626 (defun gnus-set-mode-line (where)
8627   "This function sets the mode line of the article or summary buffers.
8628 If WHERE is `summary', the summary mode line format will be used."
8629   ;; Is this mode line one we keep updated?
8630   (when (memq where gnus-updated-mode-lines)
8631     (let (mode-string)
8632       (save-excursion
8633         ;; We evaluate this in the summary buffer since these
8634         ;; variables are buffer-local to that buffer.
8635         (set-buffer gnus-summary-buffer)
8636         ;; We bind all these variables that are used in the `eval' form
8637         ;; below.
8638         (let* ((mformat (symbol-value
8639                          (intern
8640                           (format "gnus-%s-mode-line-format-spec" where))))
8641                (gnus-tmp-group-name gnus-newsgroup-name)
8642                (gnus-tmp-article-number (or gnus-current-article 0))
8643                (gnus-tmp-unread gnus-newsgroup-unreads)
8644                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8645                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8646                (gnus-tmp-unread-and-unselected
8647                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8648                             (zerop gnus-tmp-unselected)) "")
8649                       ((zerop gnus-tmp-unselected)
8650                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8651                       (t (format "{%d(+%d) more}"
8652                                  gnus-tmp-unread-and-unticked
8653                                  gnus-tmp-unselected))))
8654                (gnus-tmp-subject
8655                 (if (and gnus-current-headers
8656                          (vectorp gnus-current-headers))
8657                     (mail-header-subject gnus-current-headers) ""))
8658                max-len
8659                gnus-tmp-header);; passed as argument to any user-format-funcs
8660           (setq mode-string (eval mformat))
8661           (setq max-len (max 4 (if gnus-mode-non-string-length
8662                                    (- (frame-width)
8663                                       gnus-mode-non-string-length)
8664                                  (length mode-string))))
8665           ;; We might have to chop a bit of the string off...
8666           (when (> (length mode-string) max-len)
8667             (setq mode-string
8668                   (concat (gnus-truncate-string mode-string (- max-len 3))
8669                           "...")))
8670           ;; Pad the mode string a bit.
8671           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8672       ;; Update the mode line.
8673       (setq mode-line-buffer-identification (list mode-string))
8674       (set-buffer-modified-p t))))
8675
8676 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8677   "Go through the HEADERS list and add all Xrefs to a hash table.
8678 The resulting hash table is returned, or nil if no Xrefs were found."
8679   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8680          (virtual (gnus-virtual-group-p from-newsgroup))
8681          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8682          (xref-hashtb (make-vector 63 0))
8683          start group entry number xrefs header)
8684     (while headers
8685       (setq header (pop headers))
8686       (when (and (setq xrefs (mail-header-xref header))
8687                  (not (memq (setq number (mail-header-number header))
8688                             unreads)))
8689         (setq start 0)
8690         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8691           (setq start (match-end 0))
8692           (setq group (concat prefix (substring xrefs (match-beginning 1)
8693                                                 (match-end 1))))
8694           (setq number
8695                 (string-to-int (substring xrefs (match-beginning 2)
8696                                           (match-end 2))))
8697           (if (setq entry (gnus-gethash group xref-hashtb))
8698               (setcdr entry (cons number (cdr entry)))
8699             (gnus-sethash group (cons number nil) xref-hashtb)))))
8700     (and start xref-hashtb)))
8701
8702 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8703   "Look through all the headers and mark the Xrefs as read."
8704   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8705         name entry info xref-hashtb idlist method nth4)
8706     (save-excursion
8707       (set-buffer gnus-group-buffer)
8708       (when (setq xref-hashtb
8709                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8710         (mapatoms
8711          (lambda (group)
8712            (unless (string= from-newsgroup (setq name (symbol-name group)))
8713              (setq idlist (symbol-value group))
8714              ;; Dead groups are not updated.
8715              (and (prog1
8716                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8717                             info (nth 2 entry))
8718                     (if (stringp (setq nth4 (gnus-info-method info)))
8719                         (setq nth4 (gnus-server-to-method nth4))))
8720                   ;; Only do the xrefs if the group has the same
8721                   ;; select method as the group we have just read.
8722                   (or (gnus-methods-equal-p
8723                        nth4 (gnus-find-method-for-group from-newsgroup))
8724                       virtual
8725                       (equal nth4 (setq method (gnus-find-method-for-group
8726                                                 from-newsgroup)))
8727                       (and (equal (car nth4) (car method))
8728                            (equal (nth 1 nth4) (nth 1 method))))
8729                   gnus-use-cross-reference
8730                   (or (not (eq gnus-use-cross-reference t))
8731                       virtual
8732                       ;; Only do cross-references on subscribed
8733                       ;; groups, if that is what is wanted.
8734                       (<= (gnus-info-level info) gnus-level-subscribed))
8735                   (gnus-group-make-articles-read name idlist))))
8736          xref-hashtb)))))
8737
8738 (defun gnus-group-make-articles-read (group articles)
8739   (let* ((num 0)
8740          (entry (gnus-gethash group gnus-newsrc-hashtb))
8741          (info (nth 2 entry))
8742          (active (gnus-active group))
8743          range)
8744     ;; First peel off all illegal article numbers.
8745     (if active
8746         (let ((ids articles)
8747               id first)
8748           (while ids
8749             (setq id (car ids))
8750             (if (and first (> id (cdr active)))
8751                 (progn
8752                   ;; We'll end up in this situation in one particular
8753                   ;; obscure situation.  If you re-scan a group and get
8754                   ;; a new article that is cross-posted to a different
8755                   ;; group that has not been re-scanned, you might get
8756                   ;; crossposted article that has a higher number than
8757                   ;; Gnus believes possible.  So we re-activate this
8758                   ;; group as well.  This might mean doing the
8759                   ;; crossposting thingy will *increase* the number
8760                   ;; of articles in some groups.  Tsk, tsk.
8761                   (setq active (or (gnus-activate-group group) active))))
8762             (if (or (> id (cdr active))
8763                     (< id (car active)))
8764                 (setq articles (delq id articles)))
8765             (setq ids (cdr ids)))))
8766     ;; If the read list is nil, we init it.
8767     (and active
8768          (null (gnus-info-read info))
8769          (> (car active) 1)
8770          (gnus-info-set-read info (cons 1 (1- (car active)))))
8771     ;; Then we add the read articles to the range.
8772     (gnus-info-set-read
8773      info
8774      (setq range
8775            (gnus-add-to-range
8776             (gnus-info-read info) (setq articles (sort articles '<)))))
8777     ;; Then we have to re-compute how many unread
8778     ;; articles there are in this group.
8779     (if active
8780         (progn
8781           (cond
8782            ((not range)
8783             (setq num (- (1+ (cdr active)) (car active))))
8784            ((not (listp (cdr range)))
8785             (setq num (- (cdr active) (- (1+ (cdr range))
8786                                          (car range)))))
8787            (t
8788             (while range
8789               (if (numberp (car range))
8790                   (setq num (1+ num))
8791                 (setq num (+ num (- (1+ (cdr (car range)))
8792                                     (car (car range))))))
8793               (setq range (cdr range)))
8794             (setq num (- (cdr active) num))))
8795           ;; Update the number of unread articles.
8796           (setcar entry num)
8797           ;; Update the group buffer.
8798           (gnus-group-update-group group t)))))
8799
8800 (defun gnus-methods-equal-p (m1 m2)
8801   (let ((m1 (or m1 gnus-select-method))
8802         (m2 (or m2 gnus-select-method)))
8803     (or (equal m1 m2)
8804         (and (eq (car m1) (car m2))
8805              (or (not (memq 'address (assoc (symbol-name (car m1))
8806                                             gnus-valid-select-methods)))
8807                  (equal (nth 1 m1) (nth 1 m2)))))))
8808
8809 (defsubst gnus-header-value ()
8810   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8811
8812 (defvar gnus-newsgroup-none-id 0)
8813
8814 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8815   (let ((cur nntp-server-buffer)
8816         (dependencies
8817          (or dependencies
8818              (save-excursion (set-buffer gnus-summary-buffer)
8819                              gnus-newsgroup-dependencies)))
8820         headers id id-dep ref-dep end ref)
8821     (save-excursion
8822       (set-buffer nntp-server-buffer)
8823       (let ((case-fold-search t)
8824             in-reply-to header number p lines)
8825         (goto-char (point-min))
8826         ;; Search to the beginning of the next header.  Error messages
8827         ;; do not begin with 2 or 3.
8828         (while (re-search-forward "^[23][0-9]+ " nil t)
8829           (setq id nil
8830                 ref nil)
8831           ;; This implementation of this function, with nine
8832           ;; search-forwards instead of the one re-search-forward and
8833           ;; a case (which basically was the old function) is actually
8834           ;; about twice as fast, even though it looks messier.  You
8835           ;; can't have everything, I guess.  Speed and elegance
8836           ;; doesn't always go hand in hand.
8837           (setq
8838            header
8839            (vector
8840             ;; Number.
8841             (prog1
8842                 (read cur)
8843               (end-of-line)
8844               (setq p (point))
8845               (narrow-to-region (point)
8846                                 (or (and (search-forward "\n.\n" nil t)
8847                                          (- (point) 2))
8848                                     (point))))
8849             ;; Subject.
8850             (progn
8851               (goto-char p)
8852               (if (search-forward "\nsubject: " nil t)
8853                   (gnus-header-value) "(none)"))
8854             ;; From.
8855             (progn
8856               (goto-char p)
8857               (if (search-forward "\nfrom: " nil t)
8858                   (gnus-header-value) "(nobody)"))
8859             ;; Date.
8860             (progn
8861               (goto-char p)
8862               (if (search-forward "\ndate: " nil t)
8863                   (gnus-header-value) ""))
8864             ;; Message-ID.
8865             (progn
8866               (goto-char p)
8867               (if (search-forward "\nmessage-id: " nil t)
8868                   (setq id (gnus-header-value))
8869                 ;; If there was no message-id, we just fake one to make
8870                 ;; subsequent routines simpler.
8871                 (setq id (concat "none+"
8872                                  (int-to-string
8873                                   (setq gnus-newsgroup-none-id
8874                                         (1+ gnus-newsgroup-none-id)))))))
8875             ;; References.
8876             (progn
8877               (goto-char p)
8878               (if (search-forward "\nreferences: " nil t)
8879                   (prog1
8880                       (gnus-header-value)
8881                     (setq end (match-end 0))
8882                     (save-excursion
8883                       (setq ref
8884                             (downcase
8885                              (buffer-substring
8886                               (progn
8887                                 (end-of-line)
8888                                 (search-backward ">" end t)
8889                                 (1+ (point)))
8890                               (progn
8891                                 (search-backward "<" end t)
8892                                 (point)))))))
8893                 ;; Get the references from the in-reply-to header if there
8894                 ;; were no references and the in-reply-to header looks
8895                 ;; promising.
8896                 (if (and (search-forward "\nin-reply-to: " nil t)
8897                          (setq in-reply-to (gnus-header-value))
8898                          (string-match "<[^>]+>" in-reply-to))
8899                     (prog1
8900                         (setq ref (substring in-reply-to (match-beginning 0)
8901                                              (match-end 0)))
8902                       (setq ref (downcase ref))))
8903                 (setq ref "")))
8904             ;; Chars.
8905             0
8906             ;; Lines.
8907             (progn
8908               (goto-char p)
8909               (if (search-forward "\nlines: " nil t)
8910                   (if (numberp (setq lines (read cur)))
8911                       lines 0)
8912                 0))
8913             ;; Xref.
8914             (progn
8915               (goto-char p)
8916               (and (search-forward "\nxref: " nil t)
8917                    (gnus-header-value)))))
8918           ;; We do the threading while we read the headers.  The
8919           ;; message-id and the last reference are both entered into
8920           ;; the same hash table.  Some tippy-toeing around has to be
8921           ;; done in case an article has arrived before the article
8922           ;; which it refers to.
8923           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8924               (if (and (car (symbol-value id-dep))
8925                        (not force-new))
8926                   ;; An article with this Message-ID has already
8927                   ;; been seen, so we ignore this one, except we add
8928                   ;; any additional Xrefs (in case the two articles
8929                   ;; came from different servers).
8930                   (progn
8931                     (mail-header-set-xref
8932                      (car (symbol-value id-dep))
8933                      (concat (or (mail-header-xref
8934                                   (car (symbol-value id-dep))) "")
8935                              (or (mail-header-xref header) "")))
8936                     (setq header nil))
8937                 (setcar (symbol-value id-dep) header))
8938             (set id-dep (list header)))
8939           (when header
8940             (if (boundp (setq ref-dep (intern ref dependencies)))
8941                 (setcdr (symbol-value ref-dep)
8942                         (nconc (cdr (symbol-value ref-dep))
8943                                (list (symbol-value id-dep))))
8944               (set ref-dep (list nil (symbol-value id-dep))))
8945             (setq headers (cons header headers)))
8946           (goto-char (point-max))
8947           (widen))
8948         (nreverse headers)))))
8949
8950 ;; The following macros and functions were written by Felix Lee
8951 ;; <flee@cse.psu.edu>.
8952
8953 (defmacro gnus-nov-read-integer ()
8954   '(prog1
8955        (if (= (following-char) ?\t)
8956            0
8957          (let ((num (condition-case nil (read buffer) (error nil))))
8958            (if (numberp num) num 0)))
8959      (or (eobp) (forward-char 1))))
8960
8961 (defmacro gnus-nov-skip-field ()
8962   '(search-forward "\t" eol 'move))
8963
8964 (defmacro gnus-nov-field ()
8965   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
8966
8967 ;; Goes through the xover lines and returns a list of vectors
8968 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
8969   "Parse the news overview data in the server buffer, and return a
8970 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
8971   ;; Get the Xref when the users reads the articles since most/some
8972   ;; NNTP servers do not include Xrefs when using XOVER.
8973   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
8974   (let ((cur nntp-server-buffer)
8975         (dependencies gnus-newsgroup-dependencies)
8976         number headers header)
8977     (save-excursion
8978       (set-buffer nntp-server-buffer)
8979       ;; Allow the user to mangle the headers before parsing them.
8980       (run-hooks 'gnus-parse-headers-hook)
8981       ;; Allow the user to mangle the headers before parsing them.
8982       (run-hooks 'gnus-parse-headers-hook)
8983       (goto-char (point-min))
8984       (while (and sequence (not (eobp)))
8985         (setq number (read cur))
8986         (while (and sequence (< (car sequence) number))
8987           (setq sequence (cdr sequence)))
8988         (and sequence
8989              (eq number (car sequence))
8990              (progn
8991                (setq sequence (cdr sequence))
8992                (if (setq header
8993                          (inline (gnus-nov-parse-line
8994                                   number dependencies force-new)))
8995                    (setq headers (cons header headers)))))
8996         (forward-line 1))
8997       (setq headers (nreverse headers)))
8998     headers))
8999
9000 ;; This function has to be called with point after the article number
9001 ;; on the beginning of the line.
9002 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9003   (let ((none 0)
9004         (eol (gnus-point-at-eol))
9005         (buffer (current-buffer))
9006         header ref id id-dep ref-dep)
9007
9008     ;; overview: [num subject from date id refs chars lines misc]
9009     (narrow-to-region (point) eol)
9010     (or (eobp) (forward-char))
9011
9012     (condition-case nil
9013         (setq header
9014               (vector
9015                number                   ; number
9016                (gnus-nov-field)         ; subject
9017                (gnus-nov-field)         ; from
9018                (gnus-nov-field)         ; date
9019                (setq id (or (gnus-nov-field)
9020                             (concat "none+"
9021                                     (int-to-string
9022                                      (setq none (1+ none)))))) ; id
9023                (progn
9024                  (save-excursion
9025                    (let ((beg (point)))
9026                      (search-forward "\t" eol)
9027                      (if (search-backward ">" beg t)
9028                          (setq ref
9029                                (downcase
9030                                 (buffer-substring
9031                                  (1+ (point))
9032                                  (progn
9033                                    (search-backward "<" beg t)
9034                                    (point)))))
9035                        (setq ref nil))))
9036                  (gnus-nov-field))      ; refs
9037                (gnus-nov-read-integer)  ; chars
9038                (gnus-nov-read-integer)  ; lines
9039                (if (= (following-char) ?\n)
9040                    nil
9041                  (gnus-nov-field))      ; misc
9042                ))
9043       (error (progn
9044                (ding)
9045                (gnus-message 4 "Strange nov line")
9046                (setq header nil)
9047                (goto-char eol))))
9048
9049     (widen)
9050
9051     ;; We build the thread tree.
9052     (when header
9053       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9054           (if (and (car (symbol-value id-dep))
9055                    (not force-new))
9056               ;; An article with this Message-ID has already been seen,
9057               ;; so we ignore this one, except we add any additional
9058               ;; Xrefs (in case the two articles came from different
9059               ;; servers.
9060               (progn
9061                 (mail-header-set-xref
9062                  (car (symbol-value id-dep))
9063                  (concat (or (mail-header-xref
9064                               (car (symbol-value id-dep))) "")
9065                          (or (mail-header-xref header) "")))
9066                 (setq header nil))
9067             (setcar (symbol-value id-dep) header))
9068         (set id-dep (list header))))
9069     (if header
9070         (progn
9071           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9072               (setcdr (symbol-value ref-dep)
9073                       (nconc (cdr (symbol-value ref-dep))
9074                              (list (symbol-value id-dep))))
9075             (set ref-dep (list nil (symbol-value id-dep))))))
9076     header))
9077
9078 (defun gnus-article-get-xrefs ()
9079   "Fill in the Xref value in `gnus-current-headers', if necessary.
9080 This is meant to be called in `gnus-article-internal-prepare-hook'."
9081   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9082                                  gnus-current-headers)))
9083     (or (not gnus-use-cross-reference)
9084         (not headers)
9085         (and (mail-header-xref headers)
9086              (not (string= (mail-header-xref headers) "")))
9087         (let ((case-fold-search t)
9088               xref)
9089           (save-restriction
9090             (nnheader-narrow-to-headers)
9091             (goto-char (point-min))
9092             (if (or (and (eq (downcase (following-char)) ?x)
9093                          (looking-at "Xref:"))
9094                     (search-forward "\nXref:" nil t))
9095                 (progn
9096                   (goto-char (1+ (match-end 0)))
9097                   (setq xref (buffer-substring (point)
9098                                                (progn (end-of-line) (point))))
9099                   (mail-header-set-xref headers xref))))))))
9100
9101 (defun gnus-summary-insert-subject (id)
9102   "Find article ID and insert the summary line for that article."
9103   (let ((header (gnus-read-header id))
9104         (number (and (numberp id) id)))
9105     (when header
9106       ;; Rebuild the thread that this article is part of and go to the
9107       ;; article we have fetched.
9108       (gnus-rebuild-thread (mail-header-id header))
9109       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9110     (when (and (numberp number)
9111                (> number 0))
9112       ;; We have to update the boundaries even if we can't fetch the
9113       ;; article if ID is a number -- so that the next `P' or `N'
9114       ;; command will fetch the previous (or next) article even
9115       ;; if the one we tried to fetch this time has been canceled.
9116       (and (> number gnus-newsgroup-end)
9117            (setq gnus-newsgroup-end number))
9118       (and (< number gnus-newsgroup-begin)
9119            (setq gnus-newsgroup-begin number))
9120       (setq gnus-newsgroup-unselected
9121             (delq number gnus-newsgroup-unselected)))
9122     ;; Report back a success?
9123     (and header number)))
9124
9125 (defun gnus-summary-work-articles (n)
9126   "Return a list of articles to be worked upon.  The prefix argument,
9127 the list of process marked articles, and the current article will be
9128 taken into consideration."
9129   (cond
9130    ((and n (numberp n))
9131     ;; A numerical prefix has been given.
9132     (let ((backward (< n 0))
9133           (n (abs n))
9134           articles article)
9135       (save-excursion
9136         (while
9137             (and (> n 0)
9138                  (push (setq article (gnus-summary-article-number))
9139                        articles)
9140                  (if backward
9141                      (gnus-summary-find-prev nil article)
9142                    (gnus-summary-find-next nil article)))
9143           (decf n)))
9144       (nreverse articles)))
9145    ((and (boundp 'transient-mark-mode)
9146          transient-mark-mode
9147          mark-active)
9148     ;; Work on the region between point and mark.
9149     (let ((max (max (point) (mark)))
9150           articles article)
9151       (save-excursion
9152         (goto-char (min (point) (mark)))
9153         (while
9154             (and
9155              (push (setq article (gnus-summary-article-number)) articles)
9156              (gnus-summary-find-next nil article)
9157              (< (point) max)))
9158         (nreverse articles))))
9159    (gnus-newsgroup-processable
9160     ;; There are process-marked articles present.
9161     (reverse gnus-newsgroup-processable))
9162    (t
9163     ;; Just return the current article.
9164     (list (gnus-summary-article-number)))))
9165
9166 (defun gnus-summary-search-group (&optional backward use-level)
9167   "Search for next unread newsgroup.
9168 If optional argument BACKWARD is non-nil, search backward instead."
9169   (save-excursion
9170     (set-buffer gnus-group-buffer)
9171     (if (gnus-group-search-forward
9172          backward nil (if use-level (gnus-group-group-level) nil))
9173         (gnus-group-group-name))))
9174
9175 (defun gnus-summary-best-group (&optional exclude-group)
9176   "Find the name of the best unread group.
9177 If EXCLUDE-GROUP, do not go to this group."
9178   (save-excursion
9179     (set-buffer gnus-group-buffer)
9180     (save-excursion
9181       (gnus-group-best-unread-group exclude-group))))
9182
9183 (defun gnus-summary-find-next (&optional unread article backward)
9184   (if backward (gnus-summary-find-prev)
9185     (let* ((article (or article (gnus-summary-article-number)))
9186            (arts (gnus-data-find-list article))
9187            result)
9188       (when (or (not gnus-summary-check-current)
9189                 (not unread)
9190                 (not (gnus-data-unread-p (car arts))))
9191         (setq arts (cdr arts)))
9192       (when (setq result
9193                   (if unread
9194                       (progn
9195                         (while arts
9196                           (when (gnus-data-unread-p (car arts))
9197                             (setq result (car arts)
9198                                   arts nil))
9199                           (setq arts (cdr arts)))
9200                         result)
9201                     (car arts)))
9202         (goto-char (gnus-data-pos result))
9203         (gnus-data-number result)))))
9204
9205 (defun gnus-summary-find-prev (&optional unread article)
9206   (let* ((article (or article (gnus-summary-article-number)))
9207          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9208          result)
9209     (when (or (not gnus-summary-check-current)
9210               (not unread)
9211               (not (gnus-data-unread-p (car arts))))
9212       (setq arts (cdr arts)))
9213     (if (setq result
9214               (if unread
9215                   (progn
9216                     (while arts
9217                       (and (gnus-data-unread-p (car arts))
9218                            (setq result (car arts)
9219                                  arts nil))
9220                       (setq arts (cdr arts)))
9221                     result)
9222                 (car arts)))
9223         (progn
9224           (goto-char (gnus-data-pos result))
9225           (gnus-data-number result)))))
9226
9227 (defun gnus-summary-find-subject (subject &optional unread backward article)
9228   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9229          (article (or article (gnus-summary-article-number)))
9230          (articles (gnus-data-list backward))
9231          (arts (gnus-data-find-list article articles))
9232          result)
9233     (when (or (not gnus-summary-check-current)
9234               (not unread)
9235               (not (gnus-data-unread-p (car arts))))
9236       (setq arts (cdr arts)))
9237     (while arts
9238       (and (or (not unread)
9239                (gnus-data-unread-p (car arts)))
9240            (vectorp (gnus-data-header (car arts)))
9241            (gnus-subject-equal
9242             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9243            (setq result (car arts)
9244                  arts nil))
9245       (setq arts (cdr arts)))
9246     (and result
9247          (goto-char (gnus-data-pos result))
9248          (gnus-data-number result))))
9249
9250 (defun gnus-summary-search-forward (&optional unread subject backward)
9251   "Search forward for an article.
9252 If UNREAD, look for unread articles.  If SUBJECT, look for
9253 articles with that subject.  If BACKWARD, search backward instead."
9254   (cond (subject (gnus-summary-find-subject subject unread backward))
9255         (backward (gnus-summary-find-prev unread))
9256         (t (gnus-summary-find-next unread))))
9257
9258 (defun gnus-recenter (&optional n)
9259   "Center point in window and redisplay frame.
9260 Also do horizontal recentering."
9261   (interactive)
9262   (gnus-horizontal-recenter)
9263   (recenter n))
9264
9265 (defun gnus-summary-recenter ()
9266   "Center point in the summary window.
9267 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9268 displayed, no centering will be performed."
9269   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9270   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9271   (let* ((top (cond ((< (window-height) 4) 0)
9272                     ((< (window-height) 7) 1)
9273                     (t 2)))
9274          (height (1- (window-height)))
9275          (bottom (save-excursion (goto-char (point-max))
9276                                  (forward-line (- height))
9277                                  (point)))
9278          (window (get-buffer-window (current-buffer))))
9279     ;; The user has to want it.
9280     (when gnus-auto-center-summary
9281       (when (get-buffer-window gnus-article-buffer)
9282        ;; Only do recentering when the article buffer is displayed,
9283        ;; Set the window start to either `bottom', which is the biggest
9284        ;; possible valid number, or the second line from the top,
9285        ;; whichever is the least.
9286        (set-window-start
9287         window (min bottom (save-excursion 
9288                              (forward-line (- top)) (point)))))
9289       ;; Do horizontal recentering while we're at it.
9290       (when (get-buffer-window (current-buffer) t)
9291         (let ((selected (selected-window)))
9292           (select-window (get-buffer-window (current-buffer) t))
9293           (gnus-summary-position-point)
9294           (gnus-horizontal-recenter)
9295           (select-window selected))))))
9296
9297 (defun gnus-horizontal-recenter ()
9298   "Recenter the current buffer horizontally."
9299   (if (< (current-column) (/ (window-width) 2))
9300       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9301     (let* ((orig (point))
9302            (end (window-end (get-buffer-window (current-buffer) t)))
9303            (max 0))
9304       ;; Find the longest line currently displayed in the window.
9305       (goto-char (window-start))
9306       (while (and (not (eobp)) 
9307                   (< (point) end))
9308         (end-of-line)
9309         (setq max (max max (current-column)))
9310         (forward-line 1))
9311       (goto-char orig)
9312       ;; Scroll horizontally to center (sort of) the point.
9313       (if (> max (window-width))
9314           (set-window-hscroll 
9315            (get-buffer-window (current-buffer) t)
9316            (min (- (current-column) (/ (window-width) 3))
9317                 (+ 2 (- max (window-width)))))
9318         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9319       max)))
9320
9321 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9322 (defun gnus-short-group-name (group &optional levels)
9323   "Collapse GROUP name LEVELS."
9324   (let* ((name "") 
9325          (foreign "")
9326          (depth 0) 
9327          (skip 1)
9328          (levels (or levels
9329                      (progn
9330                        (while (string-match "\\." group skip)
9331                          (setq skip (match-end 0)
9332                                depth (+ depth 1)))
9333                        depth))))
9334     (if (string-match ":" group)
9335         (setq foreign (substring group 0 (match-end 0))
9336               group (substring group (match-end 0))))
9337     (while group
9338       (if (and (string-match "\\." group)
9339                (> levels (- gnus-group-uncollapsed-levels 1)))
9340           (setq name (concat name (substring group 0 1))
9341                 group (substring group (match-end 0))
9342                 levels (- levels 1)
9343                 name (concat name "."))
9344         (setq name (concat foreign name group)
9345               group nil)))
9346     name))
9347
9348 (defun gnus-summary-jump-to-group (newsgroup)
9349   "Move point to NEWSGROUP in group mode buffer."
9350   ;; Keep update point of group mode buffer if visible.
9351   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9352       (save-window-excursion
9353         ;; Take care of tree window mode.
9354         (if (get-buffer-window gnus-group-buffer)
9355             (pop-to-buffer gnus-group-buffer))
9356         (gnus-group-jump-to-group newsgroup))
9357     (save-excursion
9358       ;; Take care of tree window mode.
9359       (if (get-buffer-window gnus-group-buffer)
9360           (pop-to-buffer gnus-group-buffer)
9361         (set-buffer gnus-group-buffer))
9362       (gnus-group-jump-to-group newsgroup))))
9363
9364 ;; This function returns a list of article numbers based on the
9365 ;; difference between the ranges of read articles in this group and
9366 ;; the range of active articles.
9367 (defun gnus-list-of-unread-articles (group)
9368   (let* ((read (gnus-info-read (gnus-get-info group)))
9369          (active (gnus-active group))
9370          (last (cdr active))
9371          first nlast unread)
9372     ;; If none are read, then all are unread.
9373     (if (not read)
9374         (setq first (car active))
9375       ;; If the range of read articles is a single range, then the
9376       ;; first unread article is the article after the last read
9377       ;; article.  Sounds logical, doesn't it?
9378       (if (not (listp (cdr read)))
9379           (setq first (1+ (cdr read)))
9380         ;; `read' is a list of ranges.
9381         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9382                                 (car (car read)))) 1)
9383             (setq first 1))
9384         (while read
9385           (if first
9386               (while (< first nlast)
9387                 (setq unread (cons first unread))
9388                 (setq first (1+ first))))
9389           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9390           (setq nlast (if (atom (car (cdr read)))
9391                           (car (cdr read))
9392                         (car (car (cdr read)))))
9393           (setq read (cdr read)))))
9394     ;; And add the last unread articles.
9395     (while (<= first last)
9396       (setq unread (cons first unread))
9397       (setq first (1+ first)))
9398     ;; Return the list of unread articles.
9399     (nreverse unread)))
9400
9401 (defun gnus-list-of-read-articles (group)
9402   "Return a list of unread, unticked and non-dormant articles."
9403   (let* ((info (gnus-get-info group))
9404          (marked (gnus-info-marks info))
9405          (active (gnus-active group)))
9406     (and info active
9407          (gnus-set-difference
9408           (gnus-sorted-complement
9409            (gnus-uncompress-range active)
9410            (gnus-list-of-unread-articles group))
9411           (append
9412            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9413            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9414
9415 ;; Various summary commands
9416
9417 (defun gnus-summary-universal-argument (arg)
9418   "Perform any operation on all articles that are process/prefixed."
9419   (interactive "P")
9420   (gnus-set-global-variables)
9421   (let ((articles (gnus-summary-work-articles arg))
9422         func article)
9423     (if (eq
9424          (setq
9425           func
9426           (key-binding
9427            (read-key-sequence
9428             (substitute-command-keys
9429              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9430              ))))
9431          'undefined)
9432         (progn
9433           (message "Undefined key")
9434           (ding))
9435       (save-excursion
9436         (while articles
9437           (gnus-summary-goto-subject (setq article (pop articles)))
9438           (command-execute func)
9439           (gnus-summary-remove-process-mark article)))))
9440   (gnus-summary-position-point))
9441
9442 (defun gnus-summary-toggle-truncation (&optional arg)
9443   "Toggle truncation of summary lines.
9444 With arg, turn line truncation on iff arg is positive."
9445   (interactive "P")
9446   (setq truncate-lines
9447         (if (null arg) (not truncate-lines)
9448           (> (prefix-numeric-value arg) 0)))
9449   (redraw-display))
9450
9451 (defun gnus-summary-reselect-current-group (&optional all rescan)
9452   "Exit and then reselect the current newsgroup.
9453 The prefix argument ALL means to select all articles."
9454   (interactive "P")
9455   (gnus-set-global-variables)
9456   (let ((current-subject (gnus-summary-article-number))
9457         (group gnus-newsgroup-name))
9458     (setq gnus-newsgroup-begin nil)
9459     (gnus-summary-exit)
9460     ;; We have to adjust the point of group mode buffer because the
9461     ;; current point was moved to the next unread newsgroup by
9462     ;; exiting.
9463     (gnus-summary-jump-to-group group)
9464     (when rescan
9465       (save-excursion
9466         (gnus-group-get-new-news-this-group 1)))
9467     (gnus-group-read-group all t)
9468     (gnus-summary-goto-subject current-subject)))
9469
9470 (defun gnus-summary-rescan-group (&optional all)
9471   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9472   (interactive "P")
9473   (gnus-summary-reselect-current-group all t))
9474
9475 (defun gnus-summary-update-info ()
9476   (let* ((group gnus-newsgroup-name))
9477     (when gnus-newsgroup-kill-headers
9478       (setq gnus-newsgroup-killed
9479             (gnus-compress-sequence
9480              (nconc
9481               (gnus-set-sorted-intersection
9482                (gnus-uncompress-range gnus-newsgroup-killed)
9483                (setq gnus-newsgroup-unselected
9484                      (sort gnus-newsgroup-unselected '<)))
9485               (setq gnus-newsgroup-unreads
9486                     (sort gnus-newsgroup-unreads '<))) t)))
9487     (unless (listp (cdr gnus-newsgroup-killed))
9488       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9489     (let ((headers gnus-newsgroup-headers))
9490       (gnus-close-group group)
9491       (run-hooks 'gnus-exit-group-hook)
9492       (unless gnus-save-score
9493         (setq gnus-newsgroup-scored nil))
9494       ;; Set the new ranges of read articles.
9495       (gnus-update-read-articles
9496        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9497       ;; Set the current article marks.
9498       (gnus-update-marks)
9499       ;; Do the cross-ref thing.
9500       (when gnus-use-cross-reference
9501         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9502       ;; Do adaptive scoring, and possibly save score files.
9503       (when gnus-newsgroup-adaptive
9504         (gnus-score-adaptive))
9505       (when gnus-use-scoring
9506         (gnus-score-save))
9507       ;; Do not switch windows but change the buffer to work.
9508       (set-buffer gnus-group-buffer)
9509       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9510           (gnus-group-update-group group)))))
9511
9512 (defun gnus-summary-exit (&optional temporary)
9513   "Exit reading current newsgroup, and then return to group selection mode.
9514 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9515   (interactive)
9516   (gnus-set-global-variables)
9517   (gnus-kill-save-kill-buffer)
9518   (let* ((group gnus-newsgroup-name)
9519          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9520          (mode major-mode)
9521          (buf (current-buffer)))
9522     (run-hooks 'gnus-summary-prepare-exit-hook)
9523     ;; If we have several article buffers, we kill them at exit.
9524     (unless gnus-single-article-buffer
9525       (gnus-kill-buffer gnus-article-buffer)
9526       (gnus-kill-buffer gnus-original-article-buffer))
9527     (when gnus-use-cache
9528       (gnus-cache-possibly-remove-articles)
9529       (gnus-cache-save-buffers))
9530     (when gnus-use-trees
9531       (gnus-tree-close group))
9532     ;; Make all changes in this group permanent.
9533     (unless quit-config
9534       (gnus-summary-update-info))
9535     ;; Make sure where I was, and go to next newsgroup.
9536     (set-buffer gnus-group-buffer)
9537     (unless quit-config
9538       (gnus-group-jump-to-group group)
9539       (gnus-group-next-unread-group 1))
9540     (run-hooks 'gnus-summary-exit-hook)
9541     (if temporary
9542         nil                             ;Nothing to do.
9543       ;; If we have several article buffers, we kill them at exit.
9544       (unless gnus-single-article-buffer
9545         (gnus-kill-buffer gnus-article-buffer)
9546         (gnus-kill-buffer gnus-original-article-buffer))
9547       (set-buffer buf)
9548       (if (not gnus-kill-summary-on-exit)
9549           (gnus-deaden-summary)
9550         ;; We set all buffer-local variables to nil.  It is unclear why
9551         ;; this is needed, but if we don't, buffer-local variables are
9552         ;; not garbage-collected, it seems.  This would the lead to en
9553         ;; ever-growing Emacs.
9554         (gnus-summary-clear-local-variables)
9555         (when (get-buffer gnus-article-buffer)
9556           (bury-buffer gnus-article-buffer))
9557         ;; We clear the global counterparts of the buffer-local
9558         ;; variables as well, just to be on the safe side.
9559         (gnus-configure-windows 'group 'force)
9560         (gnus-summary-clear-local-variables)
9561         ;; Return to group mode buffer.
9562         (if (eq mode 'gnus-summary-mode)
9563             (gnus-kill-buffer buf)))
9564       (setq gnus-current-select-method gnus-select-method)
9565       (pop-to-buffer gnus-group-buffer)
9566       ;; Clear the current group name.
9567       (if (not quit-config)
9568           (progn
9569             (gnus-group-jump-to-group group)
9570             (gnus-group-next-unread-group 1)
9571             (gnus-configure-windows 'group 'force))
9572         (if (not (buffer-name (car quit-config)))
9573             (gnus-configure-windows 'group 'force)
9574           (set-buffer (car quit-config))
9575           (and (eq major-mode 'gnus-summary-mode)
9576                (gnus-set-global-variables))
9577           (gnus-configure-windows (cdr quit-config))))
9578       (unless quit-config
9579         (setq gnus-newsgroup-name nil)))))
9580
9581 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9582 (defun gnus-summary-exit-no-update (&optional no-questions)
9583   "Quit reading current newsgroup without updating read article info."
9584   (interactive)
9585   (gnus-set-global-variables)
9586   (let* ((group gnus-newsgroup-name)
9587          (quit-config (gnus-group-quit-config group)))
9588     (when (or no-questions
9589               gnus-expert-user
9590               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9591       ;; If we have several article buffers, we kill them at exit.
9592       (unless gnus-single-article-buffer
9593         (gnus-kill-buffer gnus-article-buffer)
9594         (gnus-kill-buffer gnus-original-article-buffer))
9595       (if (not gnus-kill-summary-on-exit)
9596           (gnus-deaden-summary)
9597         (gnus-close-group group)
9598         (gnus-summary-clear-local-variables)
9599         (set-buffer gnus-group-buffer)
9600         (gnus-summary-clear-local-variables)
9601         (when (get-buffer gnus-summary-buffer)
9602           (kill-buffer gnus-summary-buffer)))
9603       (when gnus-use-trees
9604         (gnus-tree-close group))
9605       (when (get-buffer gnus-article-buffer)
9606         (bury-buffer gnus-article-buffer))
9607       ;; Return to the group buffer.
9608       (gnus-configure-windows 'group 'force)
9609       ;; Clear the current group name.
9610       (setq gnus-newsgroup-name nil)
9611       (when (equal (gnus-group-group-name) group)
9612         (gnus-group-next-unread-group 1))
9613       (when quit-config
9614         (if (not (buffer-name (car quit-config)))
9615             (gnus-configure-windows 'group 'force)
9616           (set-buffer (car quit-config))
9617           (when (eq major-mode 'gnus-summary-mode)
9618             (gnus-set-global-variables))
9619           (gnus-configure-windows (cdr quit-config)))))))
9620
9621 ;;; Dead summaries.
9622
9623 (defvar gnus-dead-summary-mode-map nil)
9624
9625 (if gnus-dead-summary-mode-map
9626     nil
9627   (setq gnus-dead-summary-mode-map (make-keymap))
9628   (suppress-keymap gnus-dead-summary-mode-map)
9629   (substitute-key-definition
9630    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9631   (let ((keys '("\C-d" "\r" "\177")))
9632     (while keys
9633       (define-key gnus-dead-summary-mode-map
9634         (pop keys) 'gnus-summary-wake-up-the-dead))))
9635
9636 (defvar gnus-dead-summary-mode nil
9637   "Minor mode for Gnus summary buffers.")
9638
9639 (defun gnus-dead-summary-mode (&optional arg)
9640   "Minor mode for Gnus summary buffers."
9641   (interactive "P")
9642   (when (eq major-mode 'gnus-summary-mode)
9643     (make-local-variable 'gnus-dead-summary-mode)
9644     (setq gnus-dead-summary-mode
9645           (if (null arg) (not gnus-dead-summary-mode)
9646             (> (prefix-numeric-value arg) 0)))
9647     (when gnus-dead-summary-mode
9648       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9649         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9650       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9651         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9652               minor-mode-map-alist)))))
9653
9654 (defun gnus-deaden-summary ()
9655   "Make the current summary buffer into a dead summary buffer."
9656   ;; Kill any previous dead summary buffer.
9657   (when (and gnus-dead-summary
9658              (buffer-name gnus-dead-summary))
9659     (save-excursion
9660       (set-buffer gnus-dead-summary)
9661       (when gnus-dead-summary-mode
9662         (kill-buffer (current-buffer)))))
9663   ;; Make this the current dead summary.
9664   (setq gnus-dead-summary (current-buffer))
9665   (gnus-dead-summary-mode 1)
9666   (let ((name (buffer-name)))
9667     (when (string-match "Summary" name)
9668       (rename-buffer
9669        (concat (substring name 0 (match-beginning 0)) "Dead "
9670                (substring name (match-beginning 0))) t))))
9671
9672 (defun gnus-kill-or-deaden-summary (buffer)
9673   "Kill or deaden the summary BUFFER."
9674   (cond (gnus-kill-summary-on-exit
9675          (when (and gnus-use-trees
9676                     (and (get-buffer buffer)
9677                          (buffer-name (get-buffer buffer))))
9678            (save-excursion
9679              (set-buffer (get-buffer buffer))
9680              (gnus-tree-close gnus-newsgroup-name)))
9681          (gnus-kill-buffer buffer))
9682         ((and (get-buffer buffer)
9683               (buffer-name (get-buffer buffer)))
9684          (save-excursion
9685            (set-buffer buffer)
9686            (gnus-deaden-summary)))))
9687
9688 (defun gnus-summary-wake-up-the-dead (&rest args)
9689   "Wake up the dead summary buffer."
9690   (interactive)
9691   (gnus-dead-summary-mode -1)
9692   (let ((name (buffer-name)))
9693     (when (string-match "Dead " name)
9694       (rename-buffer
9695        (concat (substring name 0 (match-beginning 0))
9696                (substring name (match-end 0))) t)))
9697   (gnus-message 3 "This dead summary is now alive again"))
9698
9699 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9700 (defun gnus-summary-fetch-faq (&optional faq-dir)
9701   "Fetch the FAQ for the current group.
9702 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9703 in."
9704   (interactive
9705    (list
9706     (if current-prefix-arg
9707         (completing-read
9708          "Faq dir: " (and (listp gnus-group-faq-directory)
9709                           gnus-group-faq-directory)))))
9710   (let (gnus-faq-buffer)
9711     (and (setq gnus-faq-buffer
9712                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9713          (gnus-configure-windows 'summary-faq))))
9714
9715 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9716 (defun gnus-summary-describe-group (&optional force)
9717   "Describe the current newsgroup."
9718   (interactive "P")
9719   (gnus-group-describe-group force gnus-newsgroup-name))
9720
9721 (defun gnus-summary-describe-briefly ()
9722   "Describe summary mode commands briefly."
9723   (interactive)
9724   (gnus-message 6
9725                 (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")))
9726
9727 ;; Walking around group mode buffer from summary mode.
9728
9729 (defun gnus-summary-next-group (&optional no-article target-group backward)
9730   "Exit current newsgroup and then select next unread newsgroup.
9731 If prefix argument NO-ARTICLE is non-nil, no article is selected
9732 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9733 previous group instead."
9734   (interactive "P")
9735   (gnus-set-global-variables)
9736   (let ((current-group gnus-newsgroup-name)
9737         (current-buffer (current-buffer))
9738         entered)
9739     ;; First we semi-exit this group to update Xrefs and all variables.
9740     ;; We can't do a real exit, because the window conf must remain
9741     ;; the same in case the user is prompted for info, and we don't
9742     ;; want the window conf to change before that...
9743     (gnus-summary-exit t)
9744     (while (not entered)
9745       ;; Then we find what group we are supposed to enter.
9746       (set-buffer gnus-group-buffer)
9747       (gnus-group-jump-to-group current-group)
9748       (setq target-group
9749             (or target-group
9750                 (if (eq gnus-keep-same-level 'best)
9751                     (gnus-summary-best-group gnus-newsgroup-name)
9752                   (gnus-summary-search-group backward gnus-keep-same-level))))
9753       (if (not target-group)
9754           ;; There are no further groups, so we return to the group
9755           ;; buffer.
9756           (progn
9757             (gnus-message 5 "Returning to the group buffer")
9758             (setq entered t)
9759             (set-buffer current-buffer)
9760             (gnus-summary-exit))
9761         ;; We try to enter the target group.
9762         (gnus-group-jump-to-group target-group)
9763         (let ((unreads (gnus-group-group-unread)))
9764           (if (and (or (eq t unreads)
9765                        (and unreads (not (zerop unreads))))
9766                    (gnus-summary-read-group
9767                     target-group nil no-article current-buffer))
9768               (setq entered t)
9769             (setq current-group target-group
9770                   target-group nil)))))))
9771
9772 (defun gnus-summary-prev-group (&optional no-article)
9773   "Exit current newsgroup and then select previous unread newsgroup.
9774 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9775   (interactive "P")
9776   (gnus-summary-next-group no-article nil t))
9777
9778 ;; Walking around summary lines.
9779
9780 (defun gnus-summary-first-subject (&optional unread)
9781   "Go to the first unread subject.
9782 If UNREAD is non-nil, go to the first unread article.
9783 Returns the article selected or nil if there are no unread articles."
9784   (interactive "P")
9785   (prog1
9786       (cond
9787        ;; Empty summary.
9788        ((null gnus-newsgroup-data)
9789         (gnus-message 3 "No articles in the group")
9790         nil)
9791        ;; Pick the first article.
9792        ((not unread)
9793         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9794         (gnus-data-number (car gnus-newsgroup-data)))
9795        ;; No unread articles.
9796        ((null gnus-newsgroup-unreads)
9797         (gnus-message 3 "No more unread articles")
9798         nil)
9799        ;; Find the first unread article.
9800        (t
9801         (let ((data gnus-newsgroup-data))
9802           (while (and data
9803                       (not (gnus-data-unread-p (car data))))
9804             (setq data (cdr data)))
9805           (if data
9806               (progn
9807                 (goto-char (gnus-data-pos (car data)))
9808                 (gnus-data-number (car data)))))))
9809     (gnus-summary-position-point)))
9810
9811 (defun gnus-summary-next-subject (n &optional unread dont-display)
9812   "Go to next N'th summary line.
9813 If N is negative, go to the previous N'th subject line.
9814 If UNREAD is non-nil, only unread articles are selected.
9815 The difference between N and the actual number of steps taken is
9816 returned."
9817   (interactive "p")
9818   (let ((backward (< n 0))
9819         (n (abs n)))
9820     (while (and (> n 0)
9821                 (if backward
9822                     (gnus-summary-find-prev unread)
9823                   (gnus-summary-find-next unread)))
9824       (setq n (1- n)))
9825     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9826                                (if unread " unread" "")))
9827     (or dont-display
9828         (progn
9829           (gnus-summary-recenter)
9830           (gnus-summary-position-point)))
9831     n))
9832
9833 (defun gnus-summary-next-unread-subject (n)
9834   "Go to next N'th unread summary line."
9835   (interactive "p")
9836   (gnus-summary-next-subject n t))
9837
9838 (defun gnus-summary-prev-subject (n &optional unread)
9839   "Go to previous N'th summary line.
9840 If optional argument UNREAD is non-nil, only unread article is selected."
9841   (interactive "p")
9842   (gnus-summary-next-subject (- n) unread))
9843
9844 (defun gnus-summary-prev-unread-subject (n)
9845   "Go to previous N'th unread summary line."
9846   (interactive "p")
9847   (gnus-summary-next-subject (- n) t))
9848
9849 (defun gnus-summary-goto-subject (article &optional force silent)
9850   "Go the subject line of ARTICLE.
9851 If FORCE, also allow jumping to articles not currently shown."
9852   (let ((b (point))
9853         (data (gnus-data-find article)))
9854     ;; We read in the article if we have to.
9855     (and (not data)
9856          force
9857          (gnus-summary-insert-subject article)
9858          (setq data (gnus-data-find article)))
9859     (goto-char b)
9860     (if (not data)
9861         (progn
9862           (unless silent
9863             (gnus-message 3 "Can't find article %d" article))
9864           nil)
9865       (goto-char (gnus-data-pos data))
9866       article)))
9867
9868 ;; Walking around summary lines with displaying articles.
9869
9870 (defun gnus-summary-expand-window (&optional arg)
9871   "Make the summary buffer take up the entire Emacs frame.
9872 Given a prefix, will force an `article' buffer configuration."
9873   (interactive "P")
9874   (gnus-set-global-variables)
9875   (if arg
9876       (gnus-configure-windows 'article 'force)
9877     (gnus-configure-windows 'summary 'force)))
9878
9879 (defun gnus-summary-display-article (article &optional all-header)
9880   "Display ARTICLE in article buffer."
9881   (gnus-set-global-variables)
9882   (if (null article)
9883       nil
9884     (prog1
9885         (if gnus-summary-display-article-function
9886             (funcall gnus-summary-display-article-function article all-header)
9887           (gnus-article-prepare article all-header))
9888       (run-hooks 'gnus-select-article-hook)
9889       (gnus-summary-recenter)
9890       (gnus-summary-goto-subject article)
9891       (when gnus-use-trees
9892         (gnus-possibly-generate-tree article)
9893         (gnus-highlight-selected-tree article))
9894       ;; Successfully display article.
9895       (gnus-article-set-window-start
9896        (cdr (assq article gnus-newsgroup-bookmarks)))
9897       t)))
9898
9899 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9900   "Select the current article.
9901 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9902 non-nil, the article will be re-fetched even if it already present in
9903 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9904 be displayed."
9905   (let ((article (or article (gnus-summary-article-number)))
9906         (all-headers (not (not all-headers))) ;Must be T or NIL.
9907         gnus-summary-display-article-function
9908         did)
9909     (and (not pseudo)
9910          (gnus-summary-article-pseudo-p article)
9911          (error "This is a pseudo-article."))
9912     (prog1
9913         (save-excursion
9914           (set-buffer gnus-summary-buffer)
9915           (if (or (and gnus-single-article-buffer
9916                        (or (null gnus-current-article)
9917                            (null gnus-article-current)
9918                            (null (get-buffer gnus-article-buffer))
9919                            (not (eq article (cdr gnus-article-current)))
9920                            (not (equal (car gnus-article-current)
9921                                        gnus-newsgroup-name))))
9922                   (and (not gnus-single-article-buffer)
9923                        (null gnus-current-article))
9924                   force)
9925               ;; The requested article is different from the current article.
9926               (prog1
9927                   (gnus-summary-display-article article all-headers)
9928                 (setq did article))
9929             (if (or all-headers gnus-show-all-headers)
9930                 (gnus-article-show-all-headers))
9931             'old))
9932       (if did
9933           (gnus-article-set-window-start
9934            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9935
9936 (defun gnus-summary-set-current-mark (&optional current-mark)
9937   "Obsolete function."
9938   nil)
9939
9940 (defun gnus-summary-next-article (&optional unread subject backward push)
9941   "Select the next article.
9942 If UNREAD, only unread articles are selected.
9943 If SUBJECT, only articles with SUBJECT are selected.
9944 If BACKWARD, the previous article is selected instead of the next."
9945   (interactive "P")
9946   (gnus-set-global-variables)
9947   (let (header)
9948     (cond
9949      ;; Is there such an article?
9950      ((and (gnus-summary-search-forward unread subject backward)
9951            (or (gnus-summary-display-article (gnus-summary-article-number))
9952                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9953       (gnus-summary-position-point))
9954      ;; If not, we try the first unread, if that is wanted.
9955      ((and subject
9956            gnus-auto-select-same
9957            (or (gnus-summary-first-unread-article)
9958                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9959       (gnus-summary-position-point)
9960       (gnus-message 6 "Wrapped"))
9961      ;; Try to get next/previous article not displayed in this group.
9962      ((and gnus-auto-extend-newsgroup
9963            (not unread) (not subject))
9964       (gnus-summary-goto-article
9965        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
9966        nil t))
9967      ;; Go to next/previous group.
9968      (t
9969       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9970           (gnus-summary-jump-to-group gnus-newsgroup-name))
9971       (let ((cmd last-command-char)
9972             (group
9973              (if (eq gnus-keep-same-level 'best)
9974                  (gnus-summary-best-group gnus-newsgroup-name)
9975                (gnus-summary-search-group backward gnus-keep-same-level))))
9976         ;; For some reason, the group window gets selected.  We change
9977         ;; it back.
9978         (select-window (get-buffer-window (current-buffer)))
9979         ;; Select next unread newsgroup automagically.
9980         (cond
9981          ((not gnus-auto-select-next)
9982           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
9983          ((or (eq gnus-auto-select-next 'quietly)
9984               (and (eq gnus-auto-select-next 'slightly-quietly)
9985                    push)
9986               (and (eq gnus-auto-select-next 'almost-quietly)
9987                    (gnus-summary-last-article-p)))
9988           ;; Select quietly.
9989           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
9990               (gnus-summary-exit)
9991             (gnus-message 7 "No more%s articles (%s)..."
9992                           (if unread " unread" "")
9993                           (if group (concat "selecting " group)
9994                             "exiting"))
9995             (gnus-summary-next-group nil group backward)))
9996          (t
9997           (gnus-summary-walk-group-buffer
9998            gnus-newsgroup-name cmd unread backward))))))))
9999
10000 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10001   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10002                       (?\C-p (gnus-group-prev-unread-group 1))))
10003         keve key group ended)
10004     (save-excursion
10005       (set-buffer gnus-group-buffer)
10006       (gnus-summary-jump-to-group from-group)
10007       (setq group
10008             (if (eq gnus-keep-same-level 'best)
10009                 (gnus-summary-best-group gnus-newsgroup-name)
10010               (gnus-summary-search-group backward gnus-keep-same-level))))
10011     (while (not ended)
10012       (gnus-message
10013        5 "No more%s articles%s" (if unread " unread" "")
10014        (if (and group
10015                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10016            (format " (Type %s for %s [%s])"
10017                    (single-key-description cmd) group
10018                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10019          (format " (Type %s to exit %s)"
10020                  (single-key-description cmd)
10021                  gnus-newsgroup-name)))
10022       ;; Confirm auto selection.
10023       (setq key (car (setq keve (gnus-read-event-char))))
10024       (setq ended t)
10025       (cond
10026        ((assq key keystrokes)
10027         (let ((obuf (current-buffer)))
10028           (switch-to-buffer gnus-group-buffer)
10029           (and group
10030                (gnus-group-jump-to-group group))
10031           (eval (car (cdr (assq key keystrokes))))
10032           (setq group (gnus-group-group-name))
10033           (switch-to-buffer obuf))
10034         (setq ended nil))
10035        ((equal key cmd)
10036         (if (or (not group)
10037                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10038             (gnus-summary-exit)
10039           (gnus-summary-next-group nil group backward)))
10040        (t
10041         (push (cdr keve) unread-command-events))))))
10042
10043 (defun gnus-read-event-char ()
10044   "Get the next event."
10045   (let ((event (read-event)))
10046     (cons (and (numberp event) event) event)))
10047
10048 (defun gnus-summary-next-unread-article ()
10049   "Select unread article after current one."
10050   (interactive)
10051   (gnus-summary-next-article t (and gnus-auto-select-same
10052                                     (gnus-summary-article-subject))))
10053
10054 (defun gnus-summary-prev-article (&optional unread subject)
10055   "Select the article after the current one.
10056 If UNREAD is non-nil, only unread articles are selected."
10057   (interactive "P")
10058   (gnus-summary-next-article unread subject t))
10059
10060 (defun gnus-summary-prev-unread-article ()
10061   "Select unred article before current one."
10062   (interactive)
10063   (gnus-summary-prev-article t (and gnus-auto-select-same
10064                                     (gnus-summary-article-subject))))
10065
10066 (defun gnus-summary-next-page (&optional lines circular)
10067   "Show next page of the selected article.
10068 If at the end of the current article, select the next article.
10069 LINES says how many lines should be scrolled up.
10070
10071 If CIRCULAR is non-nil, go to the start of the article instead of
10072 selecting the next article when reaching the end of the current
10073 article."
10074   (interactive "P")
10075   (setq gnus-summary-buffer (current-buffer))
10076   (gnus-set-global-variables)
10077   (let ((article (gnus-summary-article-number))
10078         (endp nil))
10079     (gnus-configure-windows 'article)
10080     (if (or (null gnus-current-article)
10081             (null gnus-article-current)
10082             (/= article (cdr gnus-article-current))
10083             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10084         ;; Selected subject is different from current article's.
10085         (gnus-summary-display-article article)
10086       (gnus-eval-in-buffer-window
10087        gnus-article-buffer
10088        (setq endp (gnus-article-next-page lines)))
10089       (if endp
10090           (cond (circular
10091                  (gnus-summary-beginning-of-article))
10092                 (lines
10093                  (gnus-message 3 "End of message"))
10094                 ((null lines)
10095                  (if (and (eq gnus-summary-goto-unread 'never)
10096                           (not (eq article gnus-newsgroup-end)))
10097                      (gnus-summary-next-article)
10098                    (gnus-summary-next-unread-article))))))
10099     (gnus-summary-recenter)
10100     (gnus-summary-position-point)))
10101
10102 (defun gnus-summary-prev-page (&optional lines)
10103   "Show previous page of selected article.
10104 Argument LINES specifies lines to be scrolled down."
10105   (interactive "P")
10106   (gnus-set-global-variables)
10107   (let ((article (gnus-summary-article-number)))
10108     (gnus-configure-windows 'article)
10109     (if (or (null gnus-current-article)
10110             (null gnus-article-current)
10111             (/= article (cdr gnus-article-current))
10112             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10113         ;; Selected subject is different from current article's.
10114         (gnus-summary-display-article article)
10115       (gnus-summary-recenter)
10116       (gnus-eval-in-buffer-window gnus-article-buffer
10117                                   (gnus-article-prev-page lines))))
10118   (gnus-summary-position-point))
10119
10120 (defun gnus-summary-scroll-up (lines)
10121   "Scroll up (or down) one line current article.
10122 Argument LINES specifies lines to be scrolled up (or down if negative)."
10123   (interactive "p")
10124   (gnus-set-global-variables)
10125   (gnus-configure-windows 'article)
10126   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10127     (gnus-eval-in-buffer-window
10128      gnus-article-buffer
10129      (cond ((> lines 0)
10130             (if (gnus-article-next-page lines)
10131                 (gnus-message 3 "End of message")))
10132            ((< lines 0)
10133             (gnus-article-prev-page (- lines))))))
10134   (gnus-summary-recenter)
10135   (gnus-summary-position-point))
10136
10137 (defun gnus-summary-next-same-subject ()
10138   "Select next article which has the same subject as current one."
10139   (interactive)
10140   (gnus-set-global-variables)
10141   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10142
10143 (defun gnus-summary-prev-same-subject ()
10144   "Select previous article which has the same subject as current one."
10145   (interactive)
10146   (gnus-set-global-variables)
10147   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10148
10149 (defun gnus-summary-next-unread-same-subject ()
10150   "Select next unread article which has the same subject as current one."
10151   (interactive)
10152   (gnus-set-global-variables)
10153   (gnus-summary-next-article t (gnus-summary-article-subject)))
10154
10155 (defun gnus-summary-prev-unread-same-subject ()
10156   "Select previous unread article which has the same subject as current one."
10157   (interactive)
10158   (gnus-set-global-variables)
10159   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10160
10161 (defun gnus-summary-first-unread-article ()
10162   "Select the first unread article.
10163 Return nil if there are no unread articles."
10164   (interactive)
10165   (gnus-set-global-variables)
10166   (prog1
10167       (if (gnus-summary-first-subject t)
10168           (progn
10169             (gnus-summary-show-thread)
10170             (gnus-summary-first-subject t)
10171             (gnus-summary-display-article (gnus-summary-article-number))))
10172     (gnus-summary-position-point)))
10173
10174 (defun gnus-summary-best-unread-article ()
10175   "Select the unread article with the highest score."
10176   (interactive)
10177   (gnus-set-global-variables)
10178   (let ((best -1000000)
10179         (data gnus-newsgroup-data)
10180         article score)
10181     (while data
10182       (and (gnus-data-unread-p (car data))
10183            (> (setq score
10184                     (gnus-summary-article-score (gnus-data-number (car data))))
10185               best)
10186            (setq best score
10187                  article (gnus-data-number (car data))))
10188       (setq data (cdr data)))
10189     (if article
10190         (gnus-summary-goto-article article)
10191       (error "No unread articles"))
10192     (gnus-summary-position-point)))
10193
10194 (defun gnus-summary-last-subject ()
10195   "Go to the last displayed subject line in the group."
10196   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10197     (when article
10198       (gnus-summary-goto-subject article))))
10199
10200 (defun gnus-summary-goto-article (article &optional all-headers force)
10201   "Fetch ARTICLE and display it if it exists.
10202 If ALL-HEADERS is non-nil, no header lines are hidden."
10203   (interactive
10204    (list
10205     (string-to-int
10206      (completing-read
10207       "Article number: "
10208       (mapcar (lambda (number) (list (int-to-string number)))
10209               gnus-newsgroup-limit)))
10210     current-prefix-arg
10211     t))
10212   (prog1
10213       (if (gnus-summary-goto-subject article force)
10214           (gnus-summary-display-article article all-headers)
10215         (gnus-message 4 "Couldn't go to article %s" article) nil)
10216     (gnus-summary-position-point)))
10217
10218 (defun gnus-summary-goto-last-article ()
10219   "Go to the previously read article."
10220   (interactive)
10221   (prog1
10222       (and gnus-last-article
10223            (gnus-summary-goto-article gnus-last-article))
10224     (gnus-summary-position-point)))
10225
10226 (defun gnus-summary-pop-article (number)
10227   "Pop one article off the history and go to the previous.
10228 NUMBER articles will be popped off."
10229   (interactive "p")
10230   (let (to)
10231     (setq gnus-newsgroup-history
10232           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10233     (if to
10234         (gnus-summary-goto-article (car to))
10235       (error "Article history empty")))
10236   (gnus-summary-position-point))
10237
10238 ;; Summary commands and functions for limiting the summary buffer.
10239
10240 (defun gnus-summary-limit-to-articles (n)
10241   "Limit the summary buffer to the next N articles.
10242 If not given a prefix, use the process marked articles instead."
10243   (interactive "P")
10244   (gnus-set-global-variables)
10245   (prog1
10246       (let ((articles (gnus-summary-work-articles n)))
10247         (setq gnus-newsgroup-processable nil)
10248         (gnus-summary-limit articles))
10249     (gnus-summary-position-point)))
10250
10251 (defun gnus-summary-pop-limit (&optional total)
10252   "Restore the previous limit.
10253 If given a prefix, remove all limits."
10254   (interactive "P")
10255   (gnus-set-global-variables)
10256   (when total 
10257     (setq gnus-newsgroup-limits
10258           (list (mapcar (lambda (h) (mail-header-number h))
10259                         gnus-newsgroup-headers))))
10260   (unless gnus-newsgroup-limits
10261     (error "No limit to pop"))
10262   (prog1
10263       (gnus-summary-limit nil 'pop)
10264     (gnus-summary-position-point)))
10265
10266 (defun gnus-summary-limit-to-subject (subject &optional header)
10267   "Limit the summary buffer to articles that have subjects that match a regexp."
10268   (interactive "sRegexp: ")
10269   (unless header
10270     (setq header "subject"))
10271   (when (not (equal "" subject))
10272     (prog1
10273         (let ((articles (gnus-summary-find-matching
10274                          (or header "subject") subject 'all)))
10275           (or articles (error "Found no matches for \"%s\"" subject))
10276           (gnus-summary-limit articles))
10277       (gnus-summary-position-point))))
10278
10279 (defun gnus-summary-limit-to-author (from)
10280   "Limit the summary buffer to articles that have authors that match a regexp."
10281   (interactive "sRegexp: ")
10282   (gnus-summary-limit-to-subject from "from"))
10283
10284 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10285 (make-obsolete
10286  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10287
10288 (defun gnus-summary-limit-to-unread (&optional all)
10289   "Limit the summary buffer to articles that are not marked as read.
10290 If ALL is non-nil, limit strictly to unread articles."
10291   (interactive "P")
10292   (if all
10293       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10294     (gnus-summary-limit-to-marks
10295      ;; Concat all the marks that say that an article is read and have
10296      ;; those removed.
10297      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10298            gnus-killed-mark gnus-kill-file-mark
10299            gnus-low-score-mark gnus-expirable-mark
10300            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10301      'reverse)))
10302
10303 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10304 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10305
10306 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10307   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10308 If REVERSE, limit the summary buffer to articles that are not marked
10309 with MARKS.  MARKS can either be a string of marks or a list of marks.
10310 Returns how many articles were removed."
10311   (interactive "sMarks: ")
10312   (gnus-set-global-variables)
10313   (prog1
10314       (let ((data gnus-newsgroup-data)
10315             (marks (if (listp marks) marks
10316                      (append marks nil))) ; Transform to list.
10317             articles)
10318         (while data
10319           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10320                  (memq (gnus-data-mark (car data)) marks))
10321                (setq articles (cons (gnus-data-number (car data)) articles)))
10322           (setq data (cdr data)))
10323         (gnus-summary-limit articles))
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-limit-to-score (&optional score)
10327   "Limit to articles with score at or above SCORE."
10328   (interactive "P")
10329   (gnus-set-global-variables)
10330   (setq score (if score
10331                   (prefix-numeric-value score)
10332                 (or gnus-summary-default-score 0)))
10333   (let ((data gnus-newsgroup-data)
10334         articles)
10335     (while data
10336       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10337                 score)
10338         (push (gnus-data-number (car data)) articles))
10339       (setq data (cdr data)))
10340     (prog1
10341         (gnus-summary-limit articles)
10342       (gnus-summary-position-point))))
10343
10344 (defun gnus-summary-limit-include-dormant ()
10345   "Display all the hidden articles that are marked as dormant."
10346   (interactive)
10347   (gnus-set-global-variables)
10348   (or gnus-newsgroup-dormant
10349       (error "There are no dormant articles in this group"))
10350   (prog1
10351       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10352     (gnus-summary-position-point)))
10353
10354 (defun gnus-summary-limit-exclude-dormant ()
10355   "Hide all dormant articles."
10356   (interactive)
10357   (gnus-set-global-variables)
10358   (prog1
10359       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10360     (gnus-summary-position-point)))
10361
10362 (defun gnus-summary-limit-exclude-childless-dormant ()
10363   "Hide all dormant articles that have no children."
10364   (interactive)
10365   (gnus-set-global-variables)
10366   (let ((data (gnus-data-list t))
10367         articles d children)
10368     ;; Find all articles that are either not dormant or have
10369     ;; children.
10370     (while (setq d (pop data))
10371       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10372                 (and (setq children 
10373                            (gnus-article-children (gnus-data-number d)))
10374                      (let (found)
10375                        (while children
10376                          (when (memq (car children) articles)
10377                            (setq children nil
10378                                  found t))
10379                          (pop children))
10380                        found)))
10381         (push (gnus-data-number d) articles)))
10382     ;; Do the limiting.
10383     (prog1
10384         (gnus-summary-limit articles)
10385       (gnus-summary-position-point))))
10386
10387 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10388   "Mark all unread excluded articles as read.
10389 If ALL, mark even excluded ticked and dormants as read."
10390   (interactive "P")
10391   (let ((articles (gnus-sorted-complement
10392                    (sort
10393                     (mapcar (lambda (h) (mail-header-number h))
10394                             gnus-newsgroup-headers)
10395                     '<)
10396                    (sort gnus-newsgroup-limit '<)))
10397         article)
10398     (setq gnus-newsgroup-unreads nil)
10399     (if all
10400         (setq gnus-newsgroup-dormant nil
10401               gnus-newsgroup-marked nil
10402               gnus-newsgroup-reads
10403               (nconc
10404                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10405                gnus-newsgroup-reads))
10406       (while (setq article (pop articles))
10407         (unless (or (memq article gnus-newsgroup-dormant)
10408                     (memq article gnus-newsgroup-marked))
10409           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10410
10411 (defun gnus-summary-limit (articles &optional pop)
10412   (if pop
10413       ;; We pop the previous limit off the stack and use that.
10414       (setq articles (car gnus-newsgroup-limits)
10415             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10416     ;; We use the new limit, so we push the old limit on the stack.
10417     (setq gnus-newsgroup-limits
10418           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10419   ;; Set the limit.
10420   (setq gnus-newsgroup-limit articles)
10421   (let ((total (length gnus-newsgroup-data))
10422         (data (gnus-data-find-list (gnus-summary-article-number)))
10423         found)
10424     ;; This will do all the work of generating the new summary buffer
10425     ;; according to the new limit.
10426     (gnus-summary-prepare)
10427     ;; Hide any threads, possibly.
10428     (and gnus-show-threads
10429          gnus-thread-hide-subtree
10430          (gnus-summary-hide-all-threads))
10431     ;; Try to return to the article you were at, or one in the
10432     ;; neighborhood.
10433     (if data
10434         ;; We try to find some article after the current one.
10435         (while data
10436           (and (gnus-summary-goto-subject
10437                 (gnus-data-number (car data)) nil t)
10438                (setq data nil
10439                      found t))
10440           (setq data (cdr data))))
10441     (or found
10442         ;; If there is no data, that means that we were after the last
10443         ;; article.  The same goes when we can't find any articles
10444         ;; after the current one.
10445         (progn
10446           (goto-char (point-max))
10447           (gnus-summary-find-prev)))
10448     ;; We return how many articles were removed from the summary
10449     ;; buffer as a result of the new limit.
10450     (- total (length gnus-newsgroup-data))))
10451
10452 (defsubst gnus-cut-thread (thread)
10453   "Go forwards in the thread until we find an article that we want to display."
10454   (if (eq gnus-fetch-old-headers 'some)
10455       (while (and thread
10456                   (memq (mail-header-number (car thread)) 
10457                         gnus-newsgroup-ancient)
10458                   (<= (length (cdr thread)) 1))
10459         (setq thread (cadr thread)))
10460     (while (and thread
10461                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10462                 (= (length (cdr thread)) 1))
10463       (setq thread (cadr thread))))
10464   thread)
10465
10466 (defun gnus-cut-threads (threads)
10467   "Cut off all uninteresting articles from the beginning of threads."
10468   (when (or (eq gnus-fetch-old-headers 'some)
10469             (eq gnus-build-sparse-threads 'some)
10470             (eq gnus-build-sparse-threads 'more))
10471     (let ((th threads))
10472       (while th
10473         (setcar th (gnus-cut-thread (car th)))
10474         (setq th (cdr th)))))
10475   threads)
10476
10477 (defun gnus-summary-initial-limit (&optional show-if-empty)
10478   "Figure out what the initial limit is supposed to be on group entry.
10479 This entails weeding out unwanted dormants, low-scored articles,
10480 fetch-old-headers verbiage, and so on."
10481   ;; Most groups have nothing to remove.
10482   (if (or gnus-inhibit-limiting
10483           (and (null gnus-newsgroup-dormant)
10484                (not (eq gnus-fetch-old-headers 'some))
10485                (null gnus-summary-expunge-below)
10486                (not (eq gnus-build-sparse-threads 'some))
10487                (not (eq gnus-build-sparse-threads 'more))
10488                (null gnus-thread-expunge-below)
10489                (not gnus-use-nocem)))
10490       () ; Do nothing.
10491     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10492     (setq gnus-newsgroup-limit nil)
10493     (mapatoms
10494      (lambda (node)
10495        (unless (car (symbol-value node))
10496          ;; These threads have no parents -- they are roots.
10497          (let ((nodes (cdr (symbol-value node)))
10498                thread)
10499            (while nodes
10500              (if (and gnus-thread-expunge-below
10501                       (< (gnus-thread-total-score (car nodes))
10502                          gnus-thread-expunge-below))
10503                  (gnus-expunge-thread (pop nodes))
10504                (setq thread (pop nodes))
10505                (gnus-summary-limit-children thread))))))
10506      gnus-newsgroup-dependencies)
10507     ;; If this limitation resulted in an empty group, we might
10508     ;; pop the previous limit and use it instead.
10509     (when (and (not gnus-newsgroup-limit)
10510                show-if-empty)
10511       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10512     gnus-newsgroup-limit))
10513
10514 (defun gnus-summary-limit-children (thread)
10515   "Return 1 if this subthread is visible and 0 if it is not."
10516   ;; First we get the number of visible children to this thread.  This
10517   ;; is done by recursing down the thread using this function, so this
10518   ;; will really go down to a leaf article first, before slowly
10519   ;; working its way up towards the root.
10520   (when thread
10521     (let ((children
10522            (if (cdr thread)
10523                (apply '+ (mapcar 'gnus-summary-limit-children
10524                                  (cdr thread)))
10525              0))
10526           (number (mail-header-number (car thread)))
10527           score)
10528       (if (or
10529            ;; If this article is dormant and has absolutely no visible
10530            ;; children, then this article isn't visible.
10531            (and (memq number gnus-newsgroup-dormant)
10532                 (= children 0))
10533            ;; If this is a "fetch-old-headered" and there is only one
10534            ;; visible child (or less), then we don't want this article.
10535            (and (eq gnus-fetch-old-headers 'some)
10536                 (memq number gnus-newsgroup-ancient)
10537                 (zerop children))
10538            ;; If this is a sparsely inserted article with no children,
10539            ;; we don't want it.
10540            (and (eq gnus-build-sparse-threads 'some)
10541                 (memq number gnus-newsgroup-sparse)
10542                 (zerop children))
10543            ;; If we use expunging, and this article is really
10544            ;; low-scored, then we don't want this article.
10545            (when (and gnus-summary-expunge-below
10546                       (< (setq score
10547                                (or (cdr (assq number gnus-newsgroup-scored))
10548                                    gnus-summary-default-score))
10549                          gnus-summary-expunge-below))
10550              ;; We increase the expunge-tally here, but that has
10551              ;; nothing to do with the limits, really.
10552              (incf gnus-newsgroup-expunged-tally)
10553              ;; We also mark as read here, if that's wanted.
10554              (when (and gnus-summary-mark-below
10555                         (< score gnus-summary-mark-below))
10556                (setq gnus-newsgroup-unreads
10557                      (delq number gnus-newsgroup-unreads))
10558                (if gnus-newsgroup-auto-expire
10559                    (push number gnus-newsgroup-expirable)
10560                  (push (cons number gnus-low-score-mark)
10561                        gnus-newsgroup-reads)))
10562              t)
10563            (and gnus-use-nocem
10564                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10565           ;; Nope, invisible article.
10566           0
10567         ;; Ok, this article is to be visible, so we add it to the limit
10568         ;; and return 1.
10569         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10570         1))))
10571
10572 (defun gnus-expunge-thread (thread)
10573   "Mark all articles in THREAD as read."
10574   (let* ((number (mail-header-number (car thread))))
10575     (incf gnus-newsgroup-expunged-tally)
10576     ;; We also mark as read here, if that's wanted.
10577     (setq gnus-newsgroup-unreads
10578           (delq number gnus-newsgroup-unreads))
10579     (if gnus-newsgroup-auto-expire
10580         (push number gnus-newsgroup-expirable)
10581       (push (cons number gnus-low-score-mark)
10582             gnus-newsgroup-reads)))
10583   ;; Go recursively through all subthreads.
10584   (mapcar 'gnus-expunge-thread (cdr thread)))
10585
10586 ;; Summary article oriented commands
10587
10588 (defun gnus-summary-refer-parent-article (n)
10589   "Refer parent article N times.
10590 The difference between N and the number of articles fetched is returned."
10591   (interactive "p")
10592   (gnus-set-global-variables)
10593   (while
10594       (and
10595        (> n 0)
10596        (let* ((header (gnus-summary-article-header))
10597               (ref
10598                ;; If we try to find the parent of the currently
10599                ;; displayed article, then we take a look at the actual
10600                ;; References header, since this is slightly more
10601                ;; reliable than the References field we got from the
10602                ;; server.
10603                (if (and (eq (mail-header-number header)
10604                             (cdr gnus-article-current))
10605                         (equal gnus-newsgroup-name
10606                                (car gnus-article-current)))
10607                    (save-excursion
10608                      (set-buffer gnus-original-article-buffer)
10609                      (nnheader-narrow-to-headers)
10610                      (prog1
10611                          (mail-fetch-field "references")
10612                        (widen)))
10613                  ;; It's not the current article, so we take a bet on
10614                  ;; the value we got from the server.
10615                  (mail-header-references header))))
10616          (if (setq ref (or ref (mail-header-references header)))
10617              (or (gnus-summary-refer-article (gnus-parent-id ref))
10618                  (gnus-message 1 "Couldn't find parent"))
10619            (gnus-message 1 "No references in article %d"
10620                          (gnus-summary-article-number))
10621            nil)))
10622     (setq n (1- n)))
10623   (gnus-summary-position-point)
10624   n)
10625
10626 (defun gnus-summary-refer-references ()
10627   "Fetch all articles mentioned in the References header.
10628 Return how many articles were fetched."
10629   (interactive)
10630   (gnus-set-global-variables)
10631   (let ((ref (mail-header-references (gnus-summary-article-header)))
10632         (current (gnus-summary-article-number))
10633         (n 0))
10634     ;; For each Message-ID in the References header...
10635     (while (string-match "<[^>]*>" ref)
10636       (incf n)
10637       ;; ... fetch that article.
10638       (gnus-summary-refer-article
10639        (prog1 (match-string 0 ref)
10640          (setq ref (substring ref (match-end 0))))))
10641     (gnus-summary-goto-subject current)
10642     (gnus-summary-position-point)
10643     n))
10644
10645 (defun gnus-summary-refer-article (message-id)
10646   "Fetch an article specified by MESSAGE-ID."
10647   (interactive "sMessage-ID: ")
10648   (when (and (stringp message-id)
10649              (not (zerop (length message-id))))
10650     ;; Construct the correct Message-ID if necessary.
10651     ;; Suggested by tale@pawl.rpi.edu.
10652     (unless (string-match "^<" message-id)
10653       (setq message-id (concat "<" message-id)))
10654     (unless (string-match ">$" message-id)
10655       (setq message-id (concat message-id ">")))
10656     (let ((header (car (gnus-gethash (downcase message-id)
10657                                      gnus-newsgroup-dependencies))))
10658       (if header
10659           ;; The article is present in the buffer, to we just go to it.
10660           (gnus-summary-goto-article (mail-header-number header) nil t)
10661         ;; We fetch the article
10662         (let ((gnus-override-method gnus-refer-article-method)
10663               number)
10664           ;; Start the special refer-article method, if necessary.
10665           (when gnus-refer-article-method
10666             (gnus-check-server gnus-refer-article-method))
10667           ;; Fetch the header, and display the article.
10668           (if (setq number (gnus-summary-insert-subject message-id))
10669               (gnus-summary-select-article nil nil nil number)
10670             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10671
10672 (defun gnus-summary-enter-digest-group (&optional force)
10673   "Enter a digest group based on the current article."
10674   (interactive "P")
10675   (gnus-set-global-variables)
10676   (gnus-summary-select-article)
10677   (let ((name (format "%s-%d"
10678                       (gnus-group-prefixed-name
10679                        gnus-newsgroup-name (list 'nndoc ""))
10680                       gnus-current-article))
10681         (ogroup gnus-newsgroup-name)
10682         (case-fold-search t)
10683         (buf (current-buffer))
10684         dig)
10685     (save-excursion
10686       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10687       (insert-buffer-substring gnus-original-article-buffer)
10688       (narrow-to-region
10689        (goto-char (point-min))
10690        (or (search-forward "\n\n" nil t) (point)))
10691       (goto-char (point-min))
10692       (delete-matching-lines "^\\(Path\\):\\|^From ")
10693       (widen))
10694     (unwind-protect
10695         (if (gnus-group-read-ephemeral-group
10696              name `(nndoc ,name (nndoc-address
10697                                  ,(get-buffer dig))
10698                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10699             ;; Make all postings to this group go to the parent group.
10700             (nconc (gnus-info-params (gnus-get-info name))
10701                    (list (cons 'to-group ogroup)))
10702           ;; Couldn't select this doc group.
10703           (switch-to-buffer buf)
10704           (gnus-set-global-variables)
10705           (gnus-configure-windows 'summary)
10706           (gnus-message 3 "Article couldn't be entered?"))
10707       (kill-buffer dig))))
10708
10709 (defun gnus-summary-isearch-article (&optional regexp-p)
10710   "Do incremental search forward on the current article.
10711 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10712   (interactive "P")
10713   (gnus-set-global-variables)
10714   (gnus-summary-select-article)
10715   (gnus-configure-windows 'article)
10716   (gnus-eval-in-buffer-window
10717    gnus-article-buffer
10718    (goto-char (point-min))
10719    (isearch-forward regexp-p)))
10720
10721 (defun gnus-summary-search-article-forward (regexp &optional backward)
10722   "Search for an article containing REGEXP forward.
10723 If BACKWARD, search backward instead."
10724   (interactive
10725    (list (read-string
10726           (format "Search article %s (regexp%s): "
10727                   (if current-prefix-arg "backward" "forward")
10728                   (if gnus-last-search-regexp
10729                       (concat ", default " gnus-last-search-regexp)
10730                     "")))
10731          current-prefix-arg))
10732   (gnus-set-global-variables)
10733   (if (string-equal regexp "")
10734       (setq regexp (or gnus-last-search-regexp ""))
10735     (setq gnus-last-search-regexp regexp))
10736   (if (gnus-summary-search-article regexp backward)
10737       (gnus-article-set-window-start
10738        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10739     (error "Search failed: \"%s\"" regexp)))
10740
10741 (defun gnus-summary-search-article-backward (regexp)
10742   "Search for an article containing REGEXP backward."
10743   (interactive
10744    (list (read-string
10745           (format "Search article backward (regexp%s): "
10746                   (if gnus-last-search-regexp
10747                       (concat ", default " gnus-last-search-regexp)
10748                     "")))))
10749   (gnus-summary-search-article-forward regexp 'backward))
10750
10751 (defun gnus-summary-search-article (regexp &optional backward)
10752   "Search for an article containing REGEXP.
10753 Optional argument BACKWARD means do search for backward.
10754 gnus-select-article-hook is not called during the search."
10755   (let ((gnus-select-article-hook nil)  ;Disable hook.
10756         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10757         (re-search
10758          (if backward
10759              (function re-search-backward) (function re-search-forward)))
10760         (found nil)
10761         (last nil))
10762     ;; Hidden thread subtrees must be searched for ,too.
10763     (gnus-summary-show-all-threads)
10764     ;; First of all, search current article.
10765     ;; We don't want to read article again from NNTP server nor reset
10766     ;; current point.
10767     (gnus-summary-select-article)
10768     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10769     (setq last gnus-current-article)
10770     (gnus-eval-in-buffer-window
10771      gnus-article-buffer
10772      (save-restriction
10773        (widen)
10774        ;; Begin search from current point.
10775        (setq found (funcall re-search regexp nil t))))
10776     ;; Then search next articles.
10777     (while (and (not found)
10778                 (gnus-summary-display-article
10779                  (if backward (gnus-summary-find-prev)
10780                    (gnus-summary-find-next))))
10781       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10782       (gnus-eval-in-buffer-window
10783        gnus-article-buffer
10784        (save-restriction
10785          (widen)
10786          (goto-char (if backward (point-max) (point-min)))
10787          (setq found (funcall re-search regexp nil t)))))
10788     (message "")
10789     ;; Adjust article pointer.
10790     (or (eq last gnus-current-article)
10791         (setq gnus-last-article last))
10792     ;; Return T if found such article.
10793     found))
10794
10795 (defun gnus-summary-find-matching (header regexp &optional backward unread
10796                                           not-case-fold)
10797   "Return a list of all articles that match REGEXP on HEADER.
10798 The search stars on the current article and goes forwards unless
10799 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10800 If UNREAD is non-nil, only unread articles will
10801 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10802 in the comparisons."
10803   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10804                 (gnus-data-find-list
10805                  (gnus-summary-article-number) (gnus-data-list backward))))
10806         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10807         (case-fold-search (not not-case-fold))
10808         articles d)
10809     (or (fboundp (intern (concat "mail-header-" header)))
10810         (error "%s is not a valid header" header))
10811     (while data
10812       (setq d (car data))
10813       (and (or (not unread)             ; We want all articles...
10814                (gnus-data-unread-p d))  ; Or just unreads.
10815            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10816            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10817            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10818       (setq data (cdr data)))
10819     (nreverse articles)))
10820
10821 (defun gnus-summary-execute-command (header regexp command &optional backward)
10822   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10823 If HEADER is an empty string (or nil), the match is done on the entire
10824 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10825   (interactive
10826    (list (let ((completion-ignore-case t))
10827            (completing-read
10828             "Header name: "
10829             (mapcar (lambda (string) (list string))
10830                     '("Number" "Subject" "From" "Lines" "Date"
10831                       "Message-ID" "Xref" "References" "Body"))
10832             nil 'require-match))
10833          (read-string "Regexp: ")
10834          (read-key-sequence "Command: ")
10835          current-prefix-arg))
10836   (when (equal header "Body")
10837     (setq header ""))
10838   (gnus-set-global-variables)
10839   ;; Hidden thread subtrees must be searched as well.
10840   (gnus-summary-show-all-threads)
10841   ;; We don't want to change current point nor window configuration.
10842   (save-excursion
10843     (save-window-excursion
10844       (gnus-message 6 "Executing %s..." (key-description command))
10845       ;; We'd like to execute COMMAND interactively so as to give arguments.
10846       (gnus-execute header regexp
10847                     `(lambda () (call-interactively ',(key-binding command)))
10848                     backward)
10849       (gnus-message 6 "Executing %s...done" (key-description command)))))
10850
10851 (defun gnus-summary-beginning-of-article ()
10852   "Scroll the article back to the beginning."
10853   (interactive)
10854   (gnus-set-global-variables)
10855   (gnus-summary-select-article)
10856   (gnus-configure-windows 'article)
10857   (gnus-eval-in-buffer-window
10858    gnus-article-buffer
10859    (widen)
10860    (goto-char (point-min))
10861    (and gnus-break-pages (gnus-narrow-to-page))))
10862
10863 (defun gnus-summary-end-of-article ()
10864   "Scroll to the end of the article."
10865   (interactive)
10866   (gnus-set-global-variables)
10867   (gnus-summary-select-article)
10868   (gnus-configure-windows 'article)
10869   (gnus-eval-in-buffer-window
10870    gnus-article-buffer
10871    (widen)
10872    (goto-char (point-max))
10873    (recenter -3)
10874    (and gnus-break-pages (gnus-narrow-to-page))))
10875
10876 (defun gnus-summary-show-article (&optional arg)
10877   "Force re-fetching of the current article.
10878 If ARG (the prefix) is non-nil, show the raw article without any
10879 article massaging functions being run."
10880   (interactive "P")
10881   (gnus-set-global-variables)
10882   (if (not arg)
10883       ;; Select the article the normal way.
10884       (gnus-summary-select-article nil 'force)
10885     ;; Bind the article treatment functions to nil.
10886     (let ((gnus-have-all-headers t)
10887           gnus-article-display-hook
10888           gnus-article-prepare-hook
10889           gnus-visual)
10890       (gnus-summary-select-article nil 'force)))
10891 ;  (gnus-configure-windows 'article)
10892   (gnus-summary-position-point))
10893
10894 (defun gnus-summary-verbose-headers (&optional arg)
10895   "Toggle permanent full header display.
10896 If ARG is a positive number, turn header display on.
10897 If ARG is a negative number, turn header display off."
10898   (interactive "P")
10899   (gnus-set-global-variables)
10900   (gnus-summary-toggle-header arg)
10901   (setq gnus-show-all-headers
10902         (cond ((or (not (numberp arg))
10903                    (zerop arg))
10904                (not gnus-show-all-headers))
10905               ((natnump arg)
10906                t))))
10907
10908 (defun gnus-summary-toggle-header (&optional arg)
10909   "Show the headers if they are hidden, or hide them if they are shown.
10910 If ARG is a positive number, show the entire header.
10911 If ARG is a negative number, hide the unwanted header lines."
10912   (interactive "P")
10913   (gnus-set-global-variables)
10914   (save-excursion
10915     (set-buffer gnus-article-buffer)
10916     (let* ((buffer-read-only nil)
10917            (inhibit-point-motion-hooks t)
10918            (hidden (text-property-any
10919                     (goto-char (point-min)) (search-forward "\n\n")
10920                     'invisible t))
10921            e)
10922       (goto-char (point-min))
10923       (when (search-forward "\n\n" nil t)
10924         (delete-region (point-min) (1- (point))))
10925       (goto-char (point-min))
10926       (save-excursion
10927         (set-buffer gnus-original-article-buffer)
10928         (goto-char (point-min))
10929         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10930       (insert-buffer-substring gnus-original-article-buffer 1 e)
10931       (let ((gnus-inhibit-hiding t))
10932         (run-hooks 'gnus-article-display-hook))
10933       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10934           (gnus-article-hide-headers)))))
10935
10936 (defun gnus-summary-show-all-headers ()
10937   "Make all header lines visible."
10938   (interactive)
10939   (gnus-set-global-variables)
10940   (gnus-article-show-all-headers))
10941
10942 (defun gnus-summary-toggle-mime (&optional arg)
10943   "Toggle MIME processing.
10944 If ARG is a positive number, turn MIME processing on."
10945   (interactive "P")
10946   (gnus-set-global-variables)
10947   (setq gnus-show-mime
10948         (if (null arg) (not gnus-show-mime)
10949           (> (prefix-numeric-value arg) 0)))
10950   (gnus-summary-select-article t 'force))
10951
10952 (defun gnus-summary-caesar-message (&optional arg)
10953   "Caesar rotate the current article by 13.
10954 The numerical prefix specifies how manu places to rotate each letter
10955 forward."
10956   (interactive "P")
10957   (gnus-set-global-variables)
10958   (gnus-summary-select-article)
10959   (let ((mail-header-separator ""))
10960     (gnus-eval-in-buffer-window
10961      gnus-article-buffer
10962      (save-restriction
10963        (widen)
10964        (let ((start (window-start)))
10965          (news-caesar-buffer-body arg)
10966          (set-window-start (get-buffer-window (current-buffer)) start))))))
10967
10968 (defun gnus-summary-stop-page-breaking ()
10969   "Stop page breaking in the current article."
10970   (interactive)
10971   (gnus-set-global-variables)
10972   (gnus-summary-select-article)
10973   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
10974
10975 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
10976   "Move the current article to a different newsgroup.
10977 If N is a positive number, move the N next articles.
10978 If N is a negative number, move the N previous articles.
10979 If N is nil and any articles have been marked with the process mark,
10980 move those articles instead.
10981 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
10982 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10983 re-spool using this method.
10984
10985 For this function to work, both the current newsgroup and the
10986 newsgroup that you want to move to have to support the `request-move'
10987 and `request-accept' functions."
10988   (interactive "P")
10989   (unless action (setq action 'move))
10990   (gnus-set-global-variables)
10991   ;; Check whether the source group supports the required functions.
10992   (cond ((and (eq action 'move)
10993               (not (gnus-check-backend-function
10994                     'request-move-article gnus-newsgroup-name)))
10995          (error "The current group does not support article moving"))
10996         ((and (eq action 'crosspost)
10997               (not (gnus-check-backend-function
10998                     'request-replace-article gnus-newsgroup-name)))
10999          (error "The current group does not support article editing")))
11000   (let ((articles (gnus-summary-work-articles n))
11001         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11002         (names '((move "move" "Moving")
11003                  (copy "copy" "Copying")
11004                  (crosspost "crosspost" "Crossposting")))
11005         (copy-buf (save-excursion
11006                     (nnheader-set-temp-buffer " *copy article*")))
11007         art-group to-method new-xref article)
11008     (unless (assq action names)
11009       (error "Unknown action %s" action))
11010     ;; Read the newsgroup name.
11011     (when (and (not to-newsgroup)
11012                (not select-method))
11013       (setq to-newsgroup
11014             (gnus-read-move-group-name
11015              (cadr (assq action names))
11016              gnus-current-move-group articles prefix))
11017       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11018     (setq to-method (if select-method (list select-method "")
11019                       (gnus-find-method-for-group to-newsgroup)))
11020     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11021     ;;(error "Can't %s to the same group you're already in" action))
11022     ;; Check the method we are to move this article to...
11023     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11024         (error "%s does not support article copying" (car to-method)))
11025     (or (gnus-check-server to-method)
11026         (error "Can't open server %s" (car to-method)))
11027     (gnus-message 6 "%s to %s: %s..."
11028                   (caddr (assq action names))
11029                   (or select-method to-newsgroup) articles)
11030     (while articles
11031       (setq article (pop articles))
11032       (setq
11033        art-group
11034        (cond
11035         ;; Move the article.
11036         ((eq action 'move)
11037          (gnus-request-move-article
11038           article                       ; Article to move
11039           gnus-newsgroup-name           ; From newsgrouo
11040           (nth 1 (gnus-find-method-for-group
11041                   gnus-newsgroup-name)) ; Server
11042           (list 'gnus-request-accept-article
11043                 (if select-method
11044                     (list 'quote select-method)
11045                   to-newsgroup)
11046                 (not articles))         ; Accept form
11047           (not articles)))              ; Only save nov last time
11048         ;; Copy the article.
11049         ((eq action 'copy)
11050          (save-excursion
11051            (set-buffer copy-buf)
11052            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11053            (gnus-request-accept-article
11054             (if select-method select-method to-newsgroup)
11055             (not articles))))
11056         ;; Crosspost the article.
11057         ((eq action 'crosspost)
11058          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11059            (setq new-xref (concat gnus-newsgroup-name ":" article))
11060            (if (and xref (not (string= xref "")))
11061                (progn
11062                  (when (string-match "^Xref: " xref)
11063                    (setq xref (substring xref (match-end 0))))
11064                  (setq new-xref (concat xref " " new-xref)))
11065              (setq new-xref (concat (system-name) " " new-xref)))
11066            (save-excursion
11067              (set-buffer copy-buf)
11068              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11069              (nnheader-replace-header "xref" new-xref)
11070              (gnus-request-accept-article
11071               (if select-method select-method to-newsgroup)
11072               (not articles)))))))
11073       (if (not art-group)
11074           (gnus-message 1 "Couldn't %s article %s"
11075                         (cadr (assq action names)) article)
11076         (let* ((entry
11077                 (or
11078                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11079                  (gnus-gethash
11080                   (gnus-group-prefixed-name
11081                    (car art-group)
11082                    (if select-method (list select-method "")
11083                      (gnus-find-method-for-group to-newsgroup)))
11084                   gnus-newsrc-hashtb)))
11085                (info (nth 2 entry)))
11086           ;; Update the group that has been moved to.
11087           (when (and info
11088                      (memq action '(move copy)))
11089             (unless (memq article gnus-newsgroup-unreads)
11090               (gnus-info-set-read
11091                info (gnus-add-to-range (gnus-info-read info)
11092                                        (list (cdr art-group)))))
11093
11094             ;; Copy any marks over to the new group.
11095             (let ((marks gnus-article-mark-lists)
11096                   (to-article (cdr art-group)))
11097
11098               ;; See whether the article is to be put in the cache.
11099               (when gnus-use-cache
11100                 (gnus-cache-possibly-enter-article
11101                  (gnus-info-group info) to-article
11102                  (let ((header (copy-sequence
11103                                 (gnus-summary-article-header article))))
11104                    (mail-header-set-number header to-article)
11105                    header)
11106                  (memq article gnus-newsgroup-marked)
11107                  (memq article gnus-newsgroup-dormant)
11108                  (memq article gnus-newsgroup-unreads)))
11109
11110               (while marks
11111                 (when (memq article (symbol-value
11112                                      (intern (format "gnus-newsgroup-%s"
11113                                                      (caar marks)))))
11114                   (gnus-add-marked-articles
11115                    (gnus-info-group info) (cdar marks)
11116                    (list to-article) info))
11117                 (setq marks (cdr marks)))))
11118
11119           ;; Update the Xref header in this article to point to
11120           ;; the new crossposted article we have just created.
11121           (when (eq action 'crosspost)
11122             (save-excursion
11123               (set-buffer copy-buf)
11124               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11125               (nnheader-replace-header
11126                "xref" (concat new-xref " " (gnus-group-prefixed-name
11127                                             (car art-group) to-method)
11128                               ":" (cdr art-group)))
11129               (gnus-request-replace-article
11130                article gnus-newsgroup-name (current-buffer)))))
11131
11132         (gnus-summary-goto-subject article)
11133         (when (eq action 'move)
11134           (gnus-summary-mark-article article gnus-canceled-mark)))
11135       (gnus-summary-remove-process-mark article))
11136     (gnus-kill-buffer copy-buf)
11137     (gnus-summary-position-point)
11138     (gnus-set-mode-line 'summary)))
11139
11140 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11141   "Move the current article to a different newsgroup.
11142 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11143 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11144 re-spool using this method."
11145   (interactive "P")
11146   (gnus-summary-move-article n nil nil 'copy))
11147
11148 (defun gnus-summary-crosspost-article (&optional n)
11149   "Crosspost the current article to some other group."
11150   (interactive "P")
11151   (gnus-summary-move-article n nil nil 'crosspost))
11152
11153 (defun gnus-summary-respool-article (&optional n respool-method)
11154   "Respool the current article.
11155 The article will be squeezed through the mail spooling process again,
11156 which means that it will be put in some mail newsgroup or other
11157 depending on `nnmail-split-methods'.
11158 If N is a positive number, respool the N next articles.
11159 If N is a negative number, respool the N previous articles.
11160 If N is nil and any articles have been marked with the process mark,
11161 respool those articles instead.
11162
11163 Respooling can be done both from mail groups and \"real\" newsgroups.
11164 In the former case, the articles in question will be moved from the
11165 current group into whatever groups they are destined to.  In the
11166 latter case, they will be copied into the relevant groups."
11167   (interactive "P")
11168   (gnus-set-global-variables)
11169   (let ((respool-methods (gnus-methods-using 'respool))
11170         (methname
11171          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11172     (or respool-method
11173         (setq respool-method
11174               (completing-read
11175                "What method do you want to use when respooling? "
11176                respool-methods nil t methname)))
11177     (or (string= respool-method "")
11178         (if (assoc (symbol-name
11179                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11180                    respool-methods)
11181             (gnus-summary-move-article n nil (intern respool-method))
11182           (gnus-summary-copy-article n nil (intern respool-method))))))
11183
11184 (defun gnus-summary-import-article (file)
11185   "Import a random file into a mail newsgroup."
11186   (interactive "fImport file: ")
11187   (gnus-set-global-variables)
11188   (let ((group gnus-newsgroup-name)
11189         (now (current-time))
11190         atts lines)
11191     (or (gnus-check-backend-function 'request-accept-article group)
11192         (error "%s does not support article importing" group))
11193     (or (file-readable-p file)
11194         (not (file-regular-p file))
11195         (error "Can't read %s" file))
11196     (save-excursion
11197       (set-buffer (get-buffer-create " *import file*"))
11198       (buffer-disable-undo (current-buffer))
11199       (erase-buffer)
11200       (insert-file-contents file)
11201       (goto-char (point-min))
11202       (unless (nnheader-article-p)
11203         ;; This doesn't look like an article, so we fudge some headers.
11204         (setq atts (file-attributes file)
11205               lines (count-lines (point-min) (point-max)))
11206         (insert "From: " (read-string "From: ") "\n"
11207                 "Subject: " (read-string "Subject: ") "\n"
11208                 "Date: " (timezone-make-date-arpa-standard
11209                           (current-time-string (nth 5 atts))
11210                           (current-time-zone now)
11211                           (current-time-zone now)) "\n"
11212                 "Message-ID: " (gnus-inews-message-id) "\n"
11213                 "Lines: " (int-to-string lines) "\n"
11214                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11215       (gnus-request-accept-article group t)
11216       (kill-buffer (current-buffer)))))
11217
11218 (defun gnus-summary-expire-articles ()
11219   "Expire all articles that are marked as expirable in the current group."
11220   (interactive)
11221   (gnus-set-global-variables)
11222   (when (gnus-check-backend-function
11223          'request-expire-articles gnus-newsgroup-name)
11224     ;; This backend supports expiry.
11225     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11226            (expirable (if total
11227                           (gnus-list-of-read-articles gnus-newsgroup-name)
11228                         (setq gnus-newsgroup-expirable
11229                               (sort gnus-newsgroup-expirable '<))))
11230            (expiry-wait (gnus-group-get-parameter
11231                          gnus-newsgroup-name 'expiry-wait))
11232            es)
11233       (when expirable
11234         ;; There are expirable articles in this group, so we run them
11235         ;; through the expiry process.
11236         (gnus-message 6 "Expiring articles...")
11237         ;; The list of articles that weren't expired is returned.
11238         (if expiry-wait
11239             (let ((nnmail-expiry-wait-function nil)
11240                   (nnmail-expiry-wait expiry-wait))
11241               (setq es (gnus-request-expire-articles
11242                         expirable gnus-newsgroup-name)))
11243           (setq es (gnus-request-expire-articles
11244                     expirable gnus-newsgroup-name)))
11245         (or total (setq gnus-newsgroup-expirable es))
11246         ;; We go through the old list of expirable, and mark all
11247         ;; really expired articles as nonexistent.
11248         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11249           (let ((gnus-use-cache nil))
11250             (while expirable
11251               (unless (memq (car expirable) es)
11252                 (when (gnus-data-find (car expirable))
11253                   (gnus-summary-mark-article
11254                    (car expirable) gnus-canceled-mark)))
11255               (setq expirable (cdr expirable)))))
11256         (gnus-message 6 "Expiring articles...done")))))
11257
11258 (defun gnus-summary-expire-articles-now ()
11259   "Expunge all expirable articles in the current group.
11260 This means that *all* articles that are marked as expirable will be
11261 deleted forever, right now."
11262   (interactive)
11263   (gnus-set-global-variables)
11264   (or gnus-expert-user
11265       (gnus-y-or-n-p
11266        "Are you really, really, really sure you want to expunge? ")
11267       (error "Phew!"))
11268   (let ((nnmail-expiry-wait 'immediate)
11269         (nnmail-expiry-wait-function nil))
11270     (gnus-summary-expire-articles)))
11271
11272 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11273 (defun gnus-summary-delete-article (&optional n)
11274   "Delete the N next (mail) articles.
11275 This command actually deletes articles.  This is not a marking
11276 command.  The article will disappear forever from your life, never to
11277 return.
11278 If N is negative, delete backwards.
11279 If N is nil and articles have been marked with the process mark,
11280 delete these instead."
11281   (interactive "P")
11282   (gnus-set-global-variables)
11283   (or (gnus-check-backend-function 'request-expire-articles
11284                                    gnus-newsgroup-name)
11285       (error "The current newsgroup does not support article deletion."))
11286   ;; Compute the list of articles to delete.
11287   (let ((articles (gnus-summary-work-articles n))
11288         not-deleted)
11289     (if (and gnus-novice-user
11290              (not (gnus-y-or-n-p
11291                    (format "Do you really want to delete %s forever? "
11292                            (if (> (length articles) 1) "these articles"
11293                              "this article")))))
11294         ()
11295       ;; Delete the articles.
11296       (setq not-deleted (gnus-request-expire-articles
11297                          articles gnus-newsgroup-name 'force))
11298       (while articles
11299         (gnus-summary-remove-process-mark (car articles))
11300         ;; The backend might not have been able to delete the article
11301         ;; after all.
11302         (or (memq (car articles) not-deleted)
11303             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11304         (setq articles (cdr articles))))
11305     (gnus-summary-position-point)
11306     (gnus-set-mode-line 'summary)
11307     not-deleted))
11308
11309 (defun gnus-summary-edit-article (&optional force)
11310   "Enter into a buffer and edit the current article.
11311 This will have permanent effect only in mail groups.
11312 If FORCE is non-nil, allow editing of articles even in read-only
11313 groups."
11314   (interactive "P")
11315   (save-excursion
11316     (set-buffer gnus-summary-buffer)
11317     (gnus-set-global-variables)
11318     (when (and (not force)
11319                (gnus-group-read-only-p))
11320       (error "The current newsgroup does not support article editing."))
11321     (gnus-summary-select-article t nil t)
11322     (gnus-configure-windows 'article)
11323     (select-window (get-buffer-window gnus-article-buffer))
11324     (gnus-message 6 "C-c C-c to end edits")
11325     (setq buffer-read-only nil)
11326     (text-mode)
11327     (use-local-map (copy-keymap (current-local-map)))
11328     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11329     (buffer-enable-undo)
11330     (widen)
11331     (goto-char (point-min))
11332     (search-forward "\n\n" nil t)))
11333
11334 (defun gnus-summary-edit-article-done ()
11335   "Make edits to the current article permanent."
11336   (interactive)
11337   (if (gnus-group-read-only-p)
11338       (progn
11339         (gnus-summary-edit-article-postpone)
11340         (gnus-message
11341          1 "The current newsgroup does not support article editing.")
11342         (ding))
11343     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11344       (erase-buffer)
11345       (insert buf)
11346       (if (not (gnus-request-replace-article
11347                 (cdr gnus-article-current) (car gnus-article-current)
11348                 (current-buffer)))
11349           (error "Couldn't replace article.")
11350         (gnus-article-mode)
11351         (use-local-map gnus-article-mode-map)
11352         (setq buffer-read-only t)
11353         (buffer-disable-undo (current-buffer))
11354         (gnus-configure-windows 'summary)
11355         (gnus-summary-update-article (cdr gnus-article-current)))
11356       (run-hooks 'gnus-article-display-hook)
11357       (and (gnus-visual-p 'summary-highlight 'highlight)
11358            (run-hooks 'gnus-visual-mark-article-hook)))))
11359
11360 (defun gnus-summary-edit-article-postpone ()
11361   "Postpone changes to the current article."
11362   (interactive)
11363   (gnus-article-mode)
11364   (use-local-map gnus-article-mode-map)
11365   (setq buffer-read-only t)
11366   (buffer-disable-undo (current-buffer))
11367   (gnus-configure-windows 'summary)
11368   (and (gnus-visual-p 'summary-highlight 'highlight)
11369        (run-hooks 'gnus-visual-mark-article-hook)))
11370
11371 (defun gnus-summary-respool-query ()
11372   "Query where the respool algorithm would put this article."
11373   (interactive)
11374   (gnus-set-global-variables)
11375   (gnus-summary-select-article)
11376   (save-excursion
11377     (set-buffer gnus-article-buffer)
11378     (save-restriction
11379       (goto-char (point-min))
11380       (search-forward "\n\n")
11381       (narrow-to-region (point-min) (point))
11382       (pp-eval-expression
11383        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11384
11385 ;; Summary score commands.
11386
11387 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11388
11389 (defun gnus-summary-raise-score (n)
11390   "Raise the score of the current article by N."
11391   (interactive "p")
11392   (gnus-set-global-variables)
11393   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11394
11395 (defun gnus-summary-set-score (n)
11396   "Set the score of the current article to N."
11397   (interactive "p")
11398   (gnus-set-global-variables)
11399   (save-excursion
11400     (gnus-summary-show-thread)
11401     (let ((buffer-read-only nil))
11402       ;; Set score.
11403       (gnus-summary-update-mark
11404        (if (= n (or gnus-summary-default-score 0)) ? 
11405          (if (< n (or gnus-summary-default-score 0))
11406              gnus-score-below-mark gnus-score-over-mark)) 'score))
11407     (let* ((article (gnus-summary-article-number))
11408            (score (assq article gnus-newsgroup-scored)))
11409       (if score (setcdr score n)
11410         (setq gnus-newsgroup-scored
11411               (cons (cons article n) gnus-newsgroup-scored))))
11412     (gnus-summary-update-line)))
11413
11414 (defun gnus-summary-current-score ()
11415   "Return the score of the current article."
11416   (interactive)
11417   (gnus-set-global-variables)
11418   (message "%s" (gnus-summary-article-score)))
11419
11420 ;; Summary marking commands.
11421
11422 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11423   "Mark articles which has the same subject as read, and then select the next.
11424 If UNMARK is positive, remove any kind of mark.
11425 If UNMARK is negative, tick articles."
11426   (interactive "P")
11427   (gnus-set-global-variables)
11428   (if unmark
11429       (setq unmark (prefix-numeric-value unmark)))
11430   (let ((count
11431          (gnus-summary-mark-same-subject
11432           (gnus-summary-article-subject) unmark)))
11433     ;; Select next unread article.  If auto-select-same mode, should
11434     ;; select the first unread article.
11435     (gnus-summary-next-article t (and gnus-auto-select-same
11436                                       (gnus-summary-article-subject)))
11437     (gnus-message 7 "%d article%s marked as %s"
11438                   count (if (= count 1) " is" "s are")
11439                   (if unmark "unread" "read"))))
11440
11441 (defun gnus-summary-kill-same-subject (&optional unmark)
11442   "Mark articles which has the same subject as read.
11443 If UNMARK is positive, remove any kind of mark.
11444 If UNMARK is negative, tick articles."
11445   (interactive "P")
11446   (gnus-set-global-variables)
11447   (if unmark
11448       (setq unmark (prefix-numeric-value unmark)))
11449   (let ((count
11450          (gnus-summary-mark-same-subject
11451           (gnus-summary-article-subject) unmark)))
11452     ;; If marked as read, go to next unread subject.
11453     (if (null unmark)
11454         ;; Go to next unread subject.
11455         (gnus-summary-next-subject 1 t))
11456     (gnus-message 7 "%d articles are marked as %s"
11457                   count (if unmark "unread" "read"))))
11458
11459 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11460   "Mark articles with same SUBJECT as read, and return marked number.
11461 If optional argument UNMARK is positive, remove any kinds of marks.
11462 If optional argument UNMARK is negative, mark articles as unread instead."
11463   (let ((count 1))
11464     (save-excursion
11465       (cond
11466        ((null unmark)                   ; Mark as read.
11467         (while (and
11468                 (progn
11469                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11470                   (gnus-summary-show-thread) t)
11471                 (gnus-summary-find-subject subject))
11472           (setq count (1+ count))))
11473        ((> unmark 0)                    ; Tick.
11474         (while (and
11475                 (progn
11476                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11477                   (gnus-summary-show-thread) t)
11478                 (gnus-summary-find-subject subject))
11479           (setq count (1+ count))))
11480        (t                               ; Mark as unread.
11481         (while (and
11482                 (progn
11483                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11484                   (gnus-summary-show-thread) t)
11485                 (gnus-summary-find-subject subject))
11486           (setq count (1+ count)))))
11487       (gnus-set-mode-line 'summary)
11488       ;; Return the number of marked articles.
11489       count)))
11490
11491 (defun gnus-summary-mark-as-processable (n &optional unmark)
11492   "Set the process mark on the next N articles.
11493 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11494 the process mark instead.  The difference between N and the actual
11495 number of articles marked is returned."
11496   (interactive "p")
11497   (gnus-set-global-variables)
11498   (let ((backward (< n 0))
11499         (n (abs n)))
11500     (while (and
11501             (> n 0)
11502             (if unmark
11503                 (gnus-summary-remove-process-mark
11504                  (gnus-summary-article-number))
11505               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11506             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11507       (setq n (1- n)))
11508     (if (/= 0 n) (gnus-message 7 "No more articles"))
11509     (gnus-summary-recenter)
11510     (gnus-summary-position-point)
11511     n))
11512
11513 (defun gnus-summary-unmark-as-processable (n)
11514   "Remove the process mark from the next N articles.
11515 If N is negative, mark backward instead.  The difference between N and
11516 the actual number of articles marked is returned."
11517   (interactive "p")
11518   (gnus-set-global-variables)
11519   (gnus-summary-mark-as-processable n t))
11520
11521 (defun gnus-summary-unmark-all-processable ()
11522   "Remove the process mark from all articles."
11523   (interactive)
11524   (gnus-set-global-variables)
11525   (save-excursion
11526     (while gnus-newsgroup-processable
11527       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11528   (gnus-summary-position-point))
11529
11530 (defun gnus-summary-mark-as-expirable (n)
11531   "Mark N articles forward as expirable.
11532 If N is negative, mark backward instead.  The difference between N and
11533 the actual number of articles marked is returned."
11534   (interactive "p")
11535   (gnus-set-global-variables)
11536   (gnus-summary-mark-forward n gnus-expirable-mark))
11537
11538 (defun gnus-summary-mark-article-as-replied (article)
11539   "Mark ARTICLE replied and update the summary line."
11540   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11541   (let ((buffer-read-only nil))
11542     (when (gnus-summary-goto-subject article)
11543       (gnus-summary-update-secondary-mark article))))
11544
11545 (defun gnus-summary-set-bookmark (article)
11546   "Set a bookmark in current article."
11547   (interactive (list (gnus-summary-article-number)))
11548   (gnus-set-global-variables)
11549   (if (or (not (get-buffer gnus-article-buffer))
11550           (not gnus-current-article)
11551           (not gnus-article-current)
11552           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11553       (error "No current article selected"))
11554   ;; Remove old bookmark, if one exists.
11555   (let ((old (assq article gnus-newsgroup-bookmarks)))
11556     (if old (setq gnus-newsgroup-bookmarks
11557                   (delq old gnus-newsgroup-bookmarks))))
11558   ;; Set the new bookmark, which is on the form
11559   ;; (article-number . line-number-in-body).
11560   (setq gnus-newsgroup-bookmarks
11561         (cons
11562          (cons article
11563                (save-excursion
11564                  (set-buffer gnus-article-buffer)
11565                  (count-lines
11566                   (min (point)
11567                        (save-excursion
11568                          (goto-char (point-min))
11569                          (search-forward "\n\n" nil t)
11570                          (point)))
11571                   (point))))
11572          gnus-newsgroup-bookmarks))
11573   (gnus-message 6 "A bookmark has been added to the current article."))
11574
11575 (defun gnus-summary-remove-bookmark (article)
11576   "Remove the bookmark from the current article."
11577   (interactive (list (gnus-summary-article-number)))
11578   (gnus-set-global-variables)
11579   ;; Remove old bookmark, if one exists.
11580   (let ((old (assq article gnus-newsgroup-bookmarks)))
11581     (if old
11582         (progn
11583           (setq gnus-newsgroup-bookmarks
11584                 (delq old gnus-newsgroup-bookmarks))
11585           (gnus-message 6 "Removed bookmark."))
11586       (gnus-message 6 "No bookmark in current article."))))
11587
11588 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11589 (defun gnus-summary-mark-as-dormant (n)
11590   "Mark N articles forward as dormant.
11591 If N is negative, mark backward instead.  The difference between N and
11592 the actual number of articles marked is returned."
11593   (interactive "p")
11594   (gnus-set-global-variables)
11595   (gnus-summary-mark-forward n gnus-dormant-mark))
11596
11597 (defun gnus-summary-set-process-mark (article)
11598   "Set the process mark on ARTICLE and update the summary line."
11599   (setq gnus-newsgroup-processable
11600         (cons article
11601               (delq article gnus-newsgroup-processable)))
11602   (when (gnus-summary-goto-subject article)
11603     (gnus-summary-show-thread)
11604     (gnus-summary-update-secondary-mark article)))
11605
11606 (defun gnus-summary-remove-process-mark (article)
11607   "Remove the process mark from ARTICLE and update the summary line."
11608   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11609   (when (gnus-summary-goto-subject article)
11610     (gnus-summary-show-thread)
11611     (gnus-summary-update-secondary-mark article)))
11612
11613 (defun gnus-summary-set-saved-mark (article)
11614   "Set the process mark on ARTICLE and update the summary line."
11615   (push article gnus-newsgroup-saved)
11616   (when (gnus-summary-goto-subject article)
11617     (gnus-summary-update-secondary-mark article)))
11618
11619 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11620   "Mark N articles as read forwards.
11621 If N is negative, mark backwards instead.
11622 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11623 marked as unread.
11624 The difference between N and the actual number of articles marked is
11625 returned."
11626   (interactive "p")
11627   (gnus-set-global-variables)
11628   (let ((backward (< n 0))
11629         (gnus-summary-goto-unread
11630          (and gnus-summary-goto-unread
11631               (not (eq gnus-summary-goto-unread 'never))
11632               (not (memq mark (list gnus-unread-mark
11633                                     gnus-ticked-mark gnus-dormant-mark)))))
11634         (n (abs n))
11635         (mark (or mark gnus-del-mark)))
11636     (while (and (> n 0)
11637                 (gnus-summary-mark-article nil mark no-expire)
11638                 (zerop (gnus-summary-next-subject
11639                         (if backward -1 1)
11640                         (and gnus-summary-goto-unread
11641                              (not (eq gnus-summary-goto-unread 'never)))
11642                         t)))
11643       (setq n (1- n)))
11644     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11645     (gnus-summary-recenter)
11646     (gnus-summary-position-point)
11647     (gnus-set-mode-line 'summary)
11648     n))
11649
11650 (defun gnus-summary-mark-article-as-read (mark)
11651   "Mark the current article quickly as read with MARK."
11652   (let ((article (gnus-summary-article-number)))
11653     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11654     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11655     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11656     (setq gnus-newsgroup-reads
11657           (cons (cons article mark) gnus-newsgroup-reads))
11658     ;; Possibly remove from cache, if that is used.
11659     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11660     ;; Allow the backend to change the mark.
11661     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11662     ;; Check for auto-expiry.
11663     (when (and gnus-newsgroup-auto-expire
11664                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11665                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11666                    (= mark gnus-ancient-mark)
11667                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11668       (setq mark gnus-expirable-mark)
11669       (push article gnus-newsgroup-expirable))
11670     ;; Set the mark in the buffer.
11671     (gnus-summary-update-mark mark 'unread)
11672     t))
11673
11674 (defun gnus-summary-mark-article-as-unread (mark)
11675   "Mark the current article quickly as unread with MARK."
11676   (let ((article (gnus-summary-article-number)))
11677     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11678     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11679     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11680     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11681     (cond ((= mark gnus-ticked-mark)
11682            (push article gnus-newsgroup-marked))
11683           ((= mark gnus-dormant-mark)
11684            (push article gnus-newsgroup-dormant))
11685           (t
11686            (push article gnus-newsgroup-unreads)))
11687     (setq gnus-newsgroup-reads
11688           (delq (assq article gnus-newsgroup-reads)
11689                 gnus-newsgroup-reads))
11690
11691     ;; See whether the article is to be put in the cache.
11692     (and gnus-use-cache
11693          (vectorp (gnus-summary-article-header article))
11694          (save-excursion
11695            (gnus-cache-possibly-enter-article
11696             gnus-newsgroup-name article
11697             (gnus-summary-article-header article)
11698             (= mark gnus-ticked-mark)
11699             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11700
11701     ;; Fix the mark.
11702     (gnus-summary-update-mark mark 'unread)
11703     t))
11704
11705 (defun gnus-summary-mark-article (&optional article mark no-expire)
11706   "Mark ARTICLE with MARK.  MARK can be any character.
11707 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11708 `??' (dormant) and `?E' (expirable).
11709 If MARK is nil, then the default character `?D' is used.
11710 If ARTICLE is nil, then the article on the current line will be
11711 marked."
11712   ;; The mark might be a string.
11713   (and (stringp mark)
11714        (setq mark (aref mark 0)))
11715   ;; If no mark is given, then we check auto-expiring.
11716   (and (not no-expire)
11717        gnus-newsgroup-auto-expire
11718        (or (not mark)
11719            (and (numberp mark)
11720                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11721                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11722                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11723        (setq mark gnus-expirable-mark))
11724   (let* ((mark (or mark gnus-del-mark))
11725          (article (or article (gnus-summary-article-number))))
11726     (or article (error "No article on current line"))
11727     (if (or (= mark gnus-unread-mark)
11728             (= mark gnus-ticked-mark)
11729             (= mark gnus-dormant-mark))
11730         (gnus-mark-article-as-unread article mark)
11731       (gnus-mark-article-as-read article mark))
11732
11733     ;; See whether the article is to be put in the cache.
11734     (and gnus-use-cache
11735          (not (= mark gnus-canceled-mark))
11736          (vectorp (gnus-summary-article-header article))
11737          (save-excursion
11738            (gnus-cache-possibly-enter-article
11739             gnus-newsgroup-name article
11740             (gnus-summary-article-header article)
11741             (= mark gnus-ticked-mark)
11742             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11743
11744     (if (gnus-summary-goto-subject article nil t)
11745         (let ((buffer-read-only nil))
11746           (gnus-summary-show-thread)
11747           ;; Fix the mark.
11748           (gnus-summary-update-mark mark 'unread)
11749           t))))
11750
11751 (defun gnus-summary-update-secondary-mark (article)
11752   "Update the secondary (read, process, cache) mark."
11753   (gnus-summary-update-mark
11754    (cond ((memq article gnus-newsgroup-processable)
11755           gnus-process-mark)
11756          ((memq article gnus-newsgroup-cached)
11757           gnus-cached-mark)
11758          ((memq article gnus-newsgroup-replied)
11759           gnus-replied-mark)
11760          ((memq article gnus-newsgroup-saved)
11761           gnus-saved-mark)
11762          (t gnus-unread-mark))
11763    'replied)
11764   (when (gnus-visual-p 'summary-highlight 'highlight)
11765     (run-hooks 'gnus-summary-update-hook))
11766   t)
11767
11768 (defun gnus-summary-update-mark (mark type)
11769   (beginning-of-line)
11770   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11771         (buffer-read-only nil))
11772     (when forward
11773       ;; Go to the right position on the line.
11774       (forward-char forward)
11775       ;; Replace the old mark with the new mark.
11776       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11777       ;; Optionally update the marks by some user rule.
11778       (when (eq type 'unread)
11779         (gnus-data-set-mark
11780          (gnus-data-find (gnus-summary-article-number)) mark)
11781         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11782
11783 (defun gnus-mark-article-as-read (article &optional mark)
11784   "Enter ARTICLE in the pertinent lists and remove it from others."
11785   ;; Make the article expirable.
11786   (let ((mark (or mark gnus-del-mark)))
11787     (if (= mark gnus-expirable-mark)
11788         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11789       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11790     ;; Remove from unread and marked lists.
11791     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11792     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11793     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11794     (push (cons article mark) gnus-newsgroup-reads)
11795     ;; Possibly remove from cache, if that is used.
11796     (when gnus-use-cache
11797       (gnus-cache-enter-remove-article article))))
11798
11799 (defun gnus-mark-article-as-unread (article &optional mark)
11800   "Enter ARTICLE in the pertinent lists and remove it from others."
11801   (let ((mark (or mark gnus-ticked-mark)))
11802     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11803     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11804     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11805     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11806     (cond ((= mark gnus-ticked-mark)
11807            (push article gnus-newsgroup-marked))
11808           ((= mark gnus-dormant-mark)
11809            (push article gnus-newsgroup-dormant))
11810           (t
11811            (push article gnus-newsgroup-unreads)))
11812     (setq gnus-newsgroup-reads
11813           (delq (assq article gnus-newsgroup-reads)
11814                 gnus-newsgroup-reads))))
11815
11816 (defalias 'gnus-summary-mark-as-unread-forward
11817   'gnus-summary-tick-article-forward)
11818 (make-obsolete 'gnus-summary-mark-as-unread-forward
11819                'gnus-summary-tick-article-forward)
11820 (defun gnus-summary-tick-article-forward (n)
11821   "Tick N articles forwards.
11822 If N is negative, tick backwards instead.
11823 The difference between N and the number of articles ticked is returned."
11824   (interactive "p")
11825   (gnus-summary-mark-forward n gnus-ticked-mark))
11826
11827 (defalias 'gnus-summary-mark-as-unread-backward
11828   'gnus-summary-tick-article-backward)
11829 (make-obsolete 'gnus-summary-mark-as-unread-backward
11830                'gnus-summary-tick-article-backward)
11831 (defun gnus-summary-tick-article-backward (n)
11832   "Tick N articles backwards.
11833 The difference between N and the number of articles ticked is returned."
11834   (interactive "p")
11835   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11836
11837 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11838 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11839 (defun gnus-summary-tick-article (&optional article clear-mark)
11840   "Mark current article as unread.
11841 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11842 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11843   (interactive)
11844   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11845                                        gnus-ticked-mark)))
11846
11847 (defun gnus-summary-mark-as-read-forward (n)
11848   "Mark N articles as read forwards.
11849 If N is negative, mark backwards instead.
11850 The difference between N and the actual number of articles marked is
11851 returned."
11852   (interactive "p")
11853   (gnus-summary-mark-forward n gnus-del-mark t))
11854
11855 (defun gnus-summary-mark-as-read-backward (n)
11856   "Mark the N articles as read backwards.
11857 The difference between N and the actual number of articles marked is
11858 returned."
11859   (interactive "p")
11860   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11861
11862 (defun gnus-summary-mark-as-read (&optional article mark)
11863   "Mark current article as read.
11864 ARTICLE specifies the article to be marked as read.
11865 MARK specifies a string to be inserted at the beginning of the line."
11866   (gnus-summary-mark-article article mark))
11867
11868 (defun gnus-summary-clear-mark-forward (n)
11869   "Clear marks from N articles forward.
11870 If N is negative, clear backward instead.
11871 The difference between N and the number of marks cleared is returned."
11872   (interactive "p")
11873   (gnus-summary-mark-forward n gnus-unread-mark))
11874
11875 (defun gnus-summary-clear-mark-backward (n)
11876   "Clear marks from N articles backward.
11877 The difference between N and the number of marks cleared is returned."
11878   (interactive "p")
11879   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11880
11881 (defun gnus-summary-mark-unread-as-read ()
11882   "Intended to be used by `gnus-summary-mark-article-hook'."
11883   (when (memq gnus-current-article gnus-newsgroup-unreads)
11884     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11885
11886 (defun gnus-summary-mark-unread-and-read-as-read ()
11887   "Intended to be used by `gnus-summary-mark-article-hook'."
11888   (let ((mark (gnus-summary-article-mark)))
11889     (when (or (gnus-unread-mark-p mark)
11890               (gnus-read-mark-p mark))
11891       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11892
11893 (defun gnus-summary-mark-region-as-read (point mark all)
11894   "Mark all unread articles between point and mark as read.
11895 If given a prefix, mark all articles between point and mark as read,
11896 even ticked and dormant ones."
11897   (interactive "r\nP")
11898   (save-excursion
11899     (let (article)
11900       (goto-char point)
11901       (beginning-of-line)
11902       (while (and
11903               (< (point) mark)
11904               (progn
11905                 (when (or all
11906                           (memq (setq article (gnus-summary-article-number))
11907                                 gnus-newsgroup-unreads))
11908                   (gnus-summary-mark-article article gnus-del-mark))
11909                 t)
11910               (gnus-summary-find-next))))))
11911
11912 (defun gnus-summary-mark-below (score mark)
11913   "Mark articles with score less than SCORE with MARK."
11914   (interactive "P\ncMark: ")
11915   (gnus-set-global-variables)
11916   (setq score (if score
11917                   (prefix-numeric-value score)
11918                 (or gnus-summary-default-score 0)))
11919   (save-excursion
11920     (set-buffer gnus-summary-buffer)
11921     (goto-char (point-min))
11922     (while 
11923         (progn
11924           (and (< (gnus-summary-article-score) score)
11925                (gnus-summary-mark-article nil mark))
11926           (gnus-summary-find-next)))))
11927
11928 (defun gnus-summary-kill-below (&optional score)
11929   "Mark articles with score below SCORE as read."
11930   (interactive "P")
11931   (gnus-set-global-variables)
11932   (gnus-summary-mark-below score gnus-killed-mark))
11933
11934 (defun gnus-summary-clear-above (&optional score)
11935   "Clear all marks from articles with score above SCORE."
11936   (interactive "P")
11937   (gnus-set-global-variables)
11938   (gnus-summary-mark-above score gnus-unread-mark))
11939
11940 (defun gnus-summary-tick-above (&optional score)
11941   "Tick all articles with score above SCORE."
11942   (interactive "P")
11943   (gnus-set-global-variables)
11944   (gnus-summary-mark-above score gnus-ticked-mark))
11945
11946 (defun gnus-summary-mark-above (score mark)
11947   "Mark articles with score over SCORE with MARK."
11948   (interactive "P\ncMark: ")
11949   (gnus-set-global-variables)
11950   (setq score (if score
11951                   (prefix-numeric-value score)
11952                 (or gnus-summary-default-score 0)))
11953   (save-excursion
11954     (set-buffer gnus-summary-buffer)
11955     (goto-char (point-min))
11956     (while (and (progn
11957                   (if (> (gnus-summary-article-score) score)
11958                       (gnus-summary-mark-article nil mark))
11959                   t)
11960                 (gnus-summary-find-next)))))
11961
11962 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11963 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11964 (defun gnus-summary-limit-include-expunged ()
11965   "Display all the hidden articles that were expunged for low scores."
11966   (interactive)
11967   (gnus-set-global-variables)
11968   (let ((buffer-read-only nil))
11969     (let ((scored gnus-newsgroup-scored)
11970           headers h)
11971       (while scored
11972         (or (gnus-summary-goto-subject (car (car scored)))
11973             (and (setq h (gnus-summary-article-header (car (car scored))))
11974                  (< (cdr (car scored)) gnus-summary-expunge-below)
11975                  (setq headers (cons h headers))))
11976         (setq scored (cdr scored)))
11977       (or headers (error "No expunged articles hidden."))
11978       (goto-char (point-min))
11979       (gnus-summary-prepare-unthreaded (nreverse headers)))
11980     (goto-char (point-min))
11981     (gnus-summary-position-point)))
11982
11983 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
11984   "Mark all articles not marked as unread in this newsgroup as read.
11985 If prefix argument ALL is non-nil, all articles are marked as read.
11986 If QUIETLY is non-nil, no questions will be asked.
11987 If TO-HERE is non-nil, it should be a point in the buffer.  All
11988 articles before this point will be marked as read.
11989 The number of articles marked as read is returned."
11990   (interactive "P")
11991   (gnus-set-global-variables)
11992   (prog1
11993       (if (or quietly
11994               (not gnus-interactive-catchup) ;Without confirmation?
11995               gnus-expert-user
11996               (gnus-y-or-n-p
11997                (if all
11998                    "Mark absolutely all articles as read? "
11999                  "Mark all unread articles as read? ")))
12000           (if (and not-mark
12001                    (not gnus-newsgroup-adaptive)
12002                    (not gnus-newsgroup-auto-expire))
12003               (progn
12004                 (when all
12005                   (setq gnus-newsgroup-marked nil
12006                         gnus-newsgroup-dormant nil))
12007                 (setq gnus-newsgroup-unreads nil))
12008             ;; We actually mark all articles as canceled, which we
12009             ;; have to do when using auto-expiry or adaptive scoring.
12010             (gnus-summary-show-all-threads)
12011             (if (gnus-summary-first-subject (not all))
12012                 (while (and
12013                         (if to-here (< (point) to-here) t)
12014                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12015                         (gnus-summary-find-next (not all)))))
12016             (unless to-here
12017               (setq gnus-newsgroup-unreads nil))
12018             (gnus-set-mode-line 'summary)))
12019     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12020       (if (and (not to-here) (eq 'nnvirtual (car method)))
12021           (nnvirtual-catchup-group
12022            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12023     (gnus-summary-position-point)))
12024
12025 (defun gnus-summary-catchup-to-here (&optional all)
12026   "Mark all unticked articles before the current one as read.
12027 If ALL is non-nil, also mark ticked and dormant articles as read."
12028   (interactive "P")
12029   (gnus-set-global-variables)
12030   (save-excursion
12031     (let ((beg (point)))
12032       ;; We check that there are unread articles.
12033       (when (or all (gnus-summary-find-prev))
12034         (gnus-summary-catchup all t beg))))
12035   (gnus-summary-position-point))
12036
12037 (defun gnus-summary-catchup-all (&optional quietly)
12038   "Mark all articles in this newsgroup as read."
12039   (interactive "P")
12040   (gnus-set-global-variables)
12041   (gnus-summary-catchup t quietly))
12042
12043 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12044   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12045 If prefix argument ALL is non-nil, all articles are marked as read."
12046   (interactive "P")
12047   (gnus-set-global-variables)
12048   (gnus-summary-catchup all quietly nil 'fast)
12049   ;; Select next newsgroup or exit.
12050   (if (eq gnus-auto-select-next 'quietly)
12051       (gnus-summary-next-group nil)
12052     (gnus-summary-exit)))
12053
12054 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12055   "Mark all articles in this newsgroup as read, and then exit."
12056   (interactive "P")
12057   (gnus-set-global-variables)
12058   (gnus-summary-catchup-and-exit t quietly))
12059
12060 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12061 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12062   "Mark all articles in this group as read and select the next group.
12063 If given a prefix, mark all articles, unread as well as ticked, as
12064 read."
12065   (interactive "P")
12066   (gnus-set-global-variables)
12067   (save-excursion
12068     (gnus-summary-catchup all))
12069   (gnus-summary-next-article t nil nil t))
12070
12071 ;; Thread-based commands.
12072
12073 (defun gnus-summary-articles-in-thread (&optional article)
12074   "Return a list of all articles in the current thread.
12075 If ARTICLE is non-nil, return all articles in the thread that starts
12076 with that article."
12077   (let* ((article (or article (gnus-summary-article-number)))
12078          (data (gnus-data-find-list article))
12079          (top-level (gnus-data-level (car data)))
12080          (top-subject
12081           (cond ((null gnus-thread-operation-ignore-subject)
12082                  (gnus-simplify-subject-re
12083                   (mail-header-subject (gnus-data-header (car data)))))
12084                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12085                  (gnus-simplify-subject-fuzzy
12086                   (mail-header-subject (gnus-data-header (car data)))))
12087                 (t nil)))
12088          articles)
12089     (if (not data)
12090         ()                              ; This article doesn't exist.
12091       (while data
12092         (and (or (not top-subject)
12093                  (string= top-subject
12094                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12095                               (gnus-simplify-subject-fuzzy
12096                                (mail-header-subject
12097                                 (gnus-data-header (car data))))
12098                             (gnus-simplify-subject-re
12099                              (mail-header-subject
12100                               (gnus-data-header (car data)))))))
12101              (setq articles (cons (gnus-data-number (car data)) articles)))
12102         (if (and (setq data (cdr data))
12103                  (> (gnus-data-level (car data)) top-level))
12104             ()
12105           (setq data nil)))
12106       ;; Return the list of articles.
12107       (nreverse articles))))
12108
12109 (defun gnus-summary-rethread-current ()
12110   "Rethread the thread the current article is part of."
12111   (interactive)
12112   (gnus-set-global-variables)
12113   (let* ((gnus-show-threads t)
12114          (article (gnus-summary-article-number))
12115          (id (mail-header-id (gnus-summary-article-header)))
12116          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12117     (unless id
12118       (error "No article on the current line"))
12119     (gnus-rebuild-thread id)
12120     (gnus-summary-goto-subject article)))
12121
12122 (defun gnus-summary-reparent-thread ()
12123   "Make current article child of the marked (or previous) article.
12124
12125 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12126 is non-nil or the Subject: of both articles are the same."
12127   (interactive)
12128   (or (not (gnus-group-read-only-p))
12129       (error "The current newsgroup does not support article editing."))
12130   (or (<= (length gnus-newsgroup-processable) 1)
12131       (error "No more than one article may be marked."))
12132   (save-window-excursion
12133     (let ((gnus-article-buffer " *reparent*")
12134           (current-article (gnus-summary-article-number))
12135           ; first grab the marked article, otherwise one line up.
12136           (parent-article (if (not (null gnus-newsgroup-processable))
12137                               (car gnus-newsgroup-processable)
12138                             (save-excursion
12139                               (if (eq (forward-line -1) 0)
12140                                   (gnus-summary-article-number)
12141                                 (error "Beginning of summary buffer."))))))
12142       (or (not (eq current-article parent-article))
12143           (error "An article may not be self-referential."))
12144       (let ((message-id (mail-header-id 
12145                          (gnus-summary-article-header parent-article))))
12146         (or (and message-id (not (equal message-id "")))
12147             (error "No message-id in desired parent."))
12148         (gnus-summary-select-article t t nil current-article)
12149         (set-buffer gnus-article-buffer)
12150         (setq buffer-read-only nil)
12151         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12152           (erase-buffer)
12153           (insert buf))
12154         (goto-char (point-min))
12155         (if (search-forward-regexp "^References: " nil t)
12156             (insert message-id " " )
12157           (insert "References: " message-id "\n"))
12158         (or (gnus-request-replace-article current-article
12159                                           (car gnus-article-current)
12160                                           gnus-article-buffer)
12161             (error "Couldn't replace article."))
12162         (set-buffer gnus-summary-buffer)
12163         (gnus-summary-unmark-all-processable)
12164         (gnus-summary-rethread-current)
12165         (message "Article %d is now the child of article %d."
12166                  current-article parent-article)))))
12167
12168 (defun gnus-summary-toggle-threads (&optional arg)
12169   "Toggle showing conversation threads.
12170 If ARG is positive number, turn showing conversation threads on."
12171   (interactive "P")
12172   (gnus-set-global-variables)
12173   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12174     (setq gnus-show-threads
12175           (if (null arg) (not gnus-show-threads)
12176             (> (prefix-numeric-value arg) 0)))
12177     (gnus-summary-prepare)
12178     (gnus-summary-goto-subject current)
12179     (gnus-summary-position-point)))
12180
12181 (defun gnus-summary-show-all-threads ()
12182   "Show all threads."
12183   (interactive)
12184   (gnus-set-global-variables)
12185   (save-excursion
12186     (let ((buffer-read-only nil))
12187       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12188   (gnus-summary-position-point))
12189
12190 (defun gnus-summary-show-thread ()
12191   "Show thread subtrees.
12192 Returns nil if no thread was there to be shown."
12193   (interactive)
12194   (gnus-set-global-variables)
12195   (let ((buffer-read-only nil)
12196         (orig (point))
12197         ;; first goto end then to beg, to have point at beg after let
12198         (end (progn (end-of-line) (point)))
12199         (beg (progn (beginning-of-line) (point))))
12200     (prog1
12201         ;; Any hidden lines here?
12202         (search-forward "\r" end t)
12203       (subst-char-in-region beg end ?\^M ?\n t)
12204       (goto-char orig)
12205       (gnus-summary-position-point))))
12206
12207 (defun gnus-summary-hide-all-threads ()
12208   "Hide all thread subtrees."
12209   (interactive)
12210   (gnus-set-global-variables)
12211   (save-excursion
12212     (goto-char (point-min))
12213     (gnus-summary-hide-thread)
12214     (while (zerop (gnus-summary-next-thread 1 t))
12215       (gnus-summary-hide-thread)))
12216   (gnus-summary-position-point))
12217
12218 (defun gnus-summary-hide-thread ()
12219   "Hide thread subtrees.
12220 Returns nil if no threads were there to be hidden."
12221   (interactive)
12222   (gnus-set-global-variables)
12223   (let ((buffer-read-only nil)
12224         (start (point))
12225         (article (gnus-summary-article-number))
12226         end)
12227     (goto-char start)
12228     ;; Go forward until either the buffer ends or the subthread
12229     ;; ends.
12230     (when (and (not (eobp))
12231                (or (zerop (gnus-summary-next-thread 1 t))
12232                    (goto-char (point-max))))
12233       (setq end (point))
12234       (prog1
12235           (if (and (> (point) start)
12236                    (search-backward "\n" start t))
12237               (progn
12238                 (subst-char-in-region start (point) ?\n ?\^M)
12239                 (gnus-summary-goto-subject article))
12240             (goto-char start)
12241             nil)
12242         (gnus-summary-position-point)))))
12243
12244 (defun gnus-summary-go-to-next-thread (&optional previous)
12245   "Go to the same level (or less) next thread.
12246 If PREVIOUS is non-nil, go to previous thread instead.
12247 Return the article number moved to, or nil if moving was impossible."
12248   (let* ((level (gnus-summary-thread-level))
12249          (article (gnus-summary-article-number))
12250          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12251          oart)
12252     (while data
12253       (if (<= (gnus-data-level (car data)) level)
12254           (setq oart (gnus-data-number (car data))
12255                 data nil)
12256         (setq data (cdr data))))
12257     (and oart
12258          (gnus-summary-goto-subject oart))))
12259
12260 (defun gnus-summary-next-thread (n &optional silent)
12261   "Go to the same level next N'th thread.
12262 If N is negative, search backward instead.
12263 Returns the difference between N and the number of skips actually
12264 done.
12265
12266 If SILENT, don't output messages."
12267   (interactive "p")
12268   (gnus-set-global-variables)
12269   (let ((backward (< n 0))
12270         (n (abs n))
12271         old dum)
12272     (while (and (> n 0)
12273                 (setq old (save-excursion (forward-line 1) (point)))
12274                 (gnus-summary-go-to-next-thread backward))
12275       (when (and (eq gnus-summary-make-false-root 'dummy)
12276                  (setq dum (text-property-not-all
12277                             old (point) 'gnus-intangible nil)))
12278         (goto-char dum))
12279       (decf n))
12280     (gnus-summary-position-point)
12281     (when (and (not silent) (/= 0 n))
12282       (gnus-message 7 "No more threads"))
12283     n))
12284
12285 (defun gnus-summary-prev-thread (n)
12286   "Go to the same level previous N'th thread.
12287 Returns the difference between N and the number of skips actually
12288 done."
12289   (interactive "p")
12290   (gnus-set-global-variables)
12291   (gnus-summary-next-thread (- n)))
12292
12293 (defun gnus-summary-go-down-thread ()
12294   "Go down one level in the current thread."
12295   (let ((children (gnus-summary-article-children)))
12296     (and children
12297          (gnus-summary-goto-subject (car children)))))
12298
12299 (defun gnus-summary-go-up-thread ()
12300   "Go up one level in the current thread."
12301   (let ((parent (gnus-summary-article-parent)))
12302     (and parent
12303          (gnus-summary-goto-subject parent))))
12304
12305 (defun gnus-summary-down-thread (n)
12306   "Go down thread N steps.
12307 If N is negative, go up instead.
12308 Returns the difference between N and how many steps down that were
12309 taken."
12310   (interactive "p")
12311   (gnus-set-global-variables)
12312   (let ((up (< n 0))
12313         (n (abs n)))
12314     (while (and (> n 0)
12315                 (if up (gnus-summary-go-up-thread)
12316                   (gnus-summary-go-down-thread)))
12317       (setq n (1- n)))
12318     (gnus-summary-position-point)
12319     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12320     n))
12321
12322 (defun gnus-summary-up-thread (n)
12323   "Go up thread N steps.
12324 If N is negative, go up instead.
12325 Returns the difference between N and how many steps down that were
12326 taken."
12327   (interactive "p")
12328   (gnus-set-global-variables)
12329   (gnus-summary-down-thread (- n)))
12330
12331 (defun gnus-summary-top-thread ()
12332   "Go to the top of the thread."
12333   (interactive)
12334   (gnus-set-global-variables)
12335   (while (gnus-summary-go-up-thread))
12336   (gnus-summary-article-number))
12337
12338 (defun gnus-summary-kill-thread (&optional unmark)
12339   "Mark articles under current thread as read.
12340 If the prefix argument is positive, remove any kinds of marks.
12341 If the prefix argument is negative, tick articles instead."
12342   (interactive "P")
12343   (gnus-set-global-variables)
12344   (if unmark
12345       (setq unmark (prefix-numeric-value unmark)))
12346   (let ((articles (gnus-summary-articles-in-thread)))
12347     (save-excursion
12348       ;; Expand the thread.
12349       (gnus-summary-show-thread)
12350       ;; Mark all the articles.
12351       (while articles
12352         (gnus-summary-goto-subject (car articles))
12353         (cond ((null unmark)
12354                (gnus-summary-mark-article-as-read gnus-killed-mark))
12355               ((> unmark 0)
12356                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12357               (t
12358                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12359         (setq articles (cdr articles))))
12360     ;; Hide killed subtrees.
12361     (and (null unmark)
12362          gnus-thread-hide-killed
12363          (gnus-summary-hide-thread))
12364     ;; If marked as read, go to next unread subject.
12365     (if (null unmark)
12366         ;; Go to next unread subject.
12367         (gnus-summary-next-subject 1 t)))
12368   (gnus-set-mode-line 'summary))
12369
12370 ;; Summary sorting commands
12371
12372 (defun gnus-summary-sort-by-number (&optional reverse)
12373   "Sort summary buffer by article number.
12374 Argument REVERSE means reverse order."
12375   (interactive "P")
12376   (gnus-summary-sort 'number reverse))
12377
12378 (defun gnus-summary-sort-by-author (&optional reverse)
12379   "Sort summary buffer by author name alphabetically.
12380 If case-fold-search is non-nil, case of letters is ignored.
12381 Argument REVERSE means reverse order."
12382   (interactive "P")
12383   (gnus-summary-sort 'author reverse))
12384
12385 (defun gnus-summary-sort-by-subject (&optional reverse)
12386   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12387 If case-fold-search is non-nil, case of letters is ignored.
12388 Argument REVERSE means reverse order."
12389   (interactive "P")
12390   (gnus-summary-sort 'subject reverse))
12391
12392 (defun gnus-summary-sort-by-date (&optional reverse)
12393   "Sort summary buffer by date.
12394 Argument REVERSE means reverse order."
12395   (interactive "P")
12396   (gnus-summary-sort 'date reverse))
12397
12398 (defun gnus-summary-sort-by-score (&optional reverse)
12399   "Sort summary buffer by score.
12400 Argument REVERSE means reverse order."
12401   (interactive "P")
12402   (gnus-summary-sort 'score reverse))
12403
12404 (defun gnus-summary-sort (predicate reverse)
12405   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12406   (gnus-set-global-variables)
12407   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12408          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12409          (gnus-thread-sort-functions
12410           (list
12411            (if (not reverse)
12412                thread
12413              `(lambda (t1 t2)
12414                 (,thread t2 t1)))))
12415          (gnus-article-sort-functions
12416           (list
12417            (if (not reverse)
12418                article
12419              `(lambda (t1 t2)
12420                 (,article t2 t1)))))
12421          (buffer-read-only)
12422          (gnus-summary-prepare-hook nil))
12423     ;; We do the sorting by regenerating the threads.
12424     (gnus-summary-prepare)
12425     ;; Hide subthreads if needed.
12426     (when (and gnus-show-threads gnus-thread-hide-subtree)
12427       (gnus-summary-hide-all-threads)))
12428   ;; If in async mode, we send some info to the backend.
12429   (when gnus-newsgroup-async
12430     (gnus-request-asynchronous
12431      gnus-newsgroup-name gnus-newsgroup-data)))
12432
12433 (defun gnus-sortable-date (date)
12434   "Make sortable string by string-lessp from DATE.
12435 Timezone package is used."
12436   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12437          (year (aref date 0))
12438          (month (aref date 1))
12439          (day (aref date 2)))
12440     (timezone-make-sortable-date
12441      year month day
12442      (timezone-make-time-string
12443       (aref date 3) (aref date 4) (aref date 5)))))
12444
12445 ;; Summary saving commands.
12446
12447 (defun gnus-summary-save-article (&optional n not-saved)
12448   "Save the current article using the default saver function.
12449 If N is a positive number, save the N next articles.
12450 If N is a negative number, save the N previous articles.
12451 If N is nil and any articles have been marked with the process mark,
12452 save those articles instead.
12453 The variable `gnus-default-article-saver' specifies the saver function."
12454   (interactive "P")
12455   (gnus-set-global-variables)
12456   (let ((articles (gnus-summary-work-articles n))
12457         file header article)
12458     (while articles
12459       (setq header (gnus-summary-article-header
12460                     (setq article (pop articles))))
12461       (if (not (vectorp header))
12462           ;; This is a pseudo-article.
12463           (if (assq 'name header)
12464               (gnus-copy-file (cdr (assq 'name header)))
12465             (gnus-message 1 "Article %d is unsaveable" article))
12466         ;; This is a real article.
12467         (save-window-excursion
12468           (gnus-summary-select-article t nil nil article))
12469         (unless gnus-save-all-headers
12470           ;; Remove headers accoring to `gnus-saved-headers'.
12471           (let ((gnus-visible-headers
12472                  (or gnus-saved-headers gnus-visible-headers)))
12473             (gnus-article-hide-headers nil t)))
12474         ;; Remove any X-Gnus lines.
12475         (save-excursion
12476           (set-buffer gnus-article-buffer)
12477           (save-restriction
12478             (let ((buffer-read-only nil))
12479               (nnheader-narrow-to-headers)
12480               (while (re-search-forward "^X-Gnus" nil t)
12481                 (gnus-delete-line)))))
12482         (save-window-excursion
12483           (if (not gnus-default-article-saver)
12484               (error "No default saver is defined.")
12485             (setq file (funcall
12486                         gnus-default-article-saver
12487                         (cond
12488                          ((not gnus-prompt-before-saving)
12489                           'default)
12490                          ((eq gnus-prompt-before-saving 'always)
12491                           nil)
12492                          (t file))))))
12493         (gnus-summary-remove-process-mark article)
12494         (unless not-saved
12495           (gnus-summary-set-saved-mark article))))
12496     (gnus-summary-position-point)
12497     n))
12498
12499 (defun gnus-summary-pipe-output (&optional arg)
12500   "Pipe the current article to a subprocess.
12501 If N is a positive number, pipe the N next articles.
12502 If N is a negative number, pipe the N previous articles.
12503 If N is nil and any articles have been marked with the process mark,
12504 pipe those articles instead."
12505   (interactive "P")
12506   (gnus-set-global-variables)
12507   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12508     (gnus-summary-save-article arg t))
12509   (gnus-configure-windows 'pipe))
12510
12511 (defun gnus-summary-save-article-mail (&optional arg)
12512   "Append the current article to an mail file.
12513 If N is a positive number, save the N next articles.
12514 If N is a negative number, save the N previous articles.
12515 If N is nil and any articles have been marked with the process mark,
12516 save those articles instead."
12517   (interactive "P")
12518   (gnus-set-global-variables)
12519   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12520     (gnus-summary-save-article arg)))
12521
12522 (defun gnus-summary-save-article-rmail (&optional arg)
12523   "Append the current article to an rmail file.
12524 If N is a positive number, save the N next articles.
12525 If N is a negative number, save the N previous articles.
12526 If N is nil and any articles have been marked with the process mark,
12527 save those articles instead."
12528   (interactive "P")
12529   (gnus-set-global-variables)
12530   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12531     (gnus-summary-save-article arg)))
12532
12533 (defun gnus-summary-save-article-file (&optional arg)
12534   "Append the current article to a file.
12535 If N is a positive number, save the N next articles.
12536 If N is a negative number, save the N previous articles.
12537 If N is nil and any articles have been marked with the process mark,
12538 save those articles instead."
12539   (interactive "P")
12540   (gnus-set-global-variables)
12541   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12542     (gnus-summary-save-article arg)))
12543
12544 (defun gnus-summary-save-article-body-file (&optional arg)
12545   "Append the current article body to a file.
12546 If N is a positive number, save the N next articles.
12547 If N is a negative number, save the N previous articles.
12548 If N is nil and any articles have been marked with the process mark,
12549 save those articles instead."
12550   (interactive "P")
12551   (gnus-set-global-variables)
12552   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12553     (gnus-summary-save-article arg)))
12554
12555 (defun gnus-get-split-value (methods)
12556   "Return a value based on the split METHODS."
12557   (let (split-name method result match)
12558     (when methods
12559       (save-excursion
12560         (set-buffer gnus-original-article-buffer)
12561         (save-restriction
12562           (nnheader-narrow-to-headers)
12563           (while methods
12564             (goto-char (point-min))
12565             (setq method (pop methods))
12566             (setq match (pop method))
12567             (when (cond
12568                    ((stringp match)
12569                     ;; Regular expression.
12570                     (condition-case ()
12571                         (re-search-forward match nil t)
12572                       (error nil)))
12573                    ((gnus-functionp match)
12574                     ;; Function.
12575                     (save-restriction
12576                       (widen)
12577                       (setq result (funcall match gnus-newsgroup-name))))
12578                    ((consp match)
12579                     ;; Form.
12580                     (save-restriction
12581                       (widen)
12582                       (setq result (eval match)))))
12583               (setq split-name (append (cdr methods) split-name))
12584               (cond ((stringp result)
12585                      (push result split-name))
12586                     ((consp result)
12587                      (setq split-name (append result split-name)))))))))
12588     split-name))
12589
12590 (defun gnus-read-move-group-name (prompt default articles prefix)
12591   "Read a group name."
12592   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12593          (prom
12594           (format "Where do you want to %s %s? "
12595                   prompt
12596                   (if (> (length articles) 1)
12597                       (format "these %d articles" (length articles))
12598                     "this article")))
12599          (to-newsgroup
12600           (cond
12601            ((null split-name)
12602             (completing-read
12603              (concat prom
12604                      (if default
12605                          (format "(default %s) " default)
12606                        ""))
12607              gnus-active-hashtb nil nil prefix))
12608            ((= 1 (length split-name))
12609             (completing-read prom gnus-active-hashtb
12610                              nil nil (cons (car split-name) 0)))
12611            (t
12612             (completing-read
12613              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12614
12615     (when to-newsgroup
12616       (if (or (string= to-newsgroup "")
12617               (string= to-newsgroup prefix))
12618           (setq to-newsgroup (or default "")))
12619       (or (gnus-active to-newsgroup)
12620           (gnus-activate-group to-newsgroup)
12621           (error "No such group: %s" to-newsgroup)))
12622     to-newsgroup))
12623
12624 (defun gnus-read-save-file-name (prompt default-name)
12625   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12626          (file
12627           ;; Let the split methods have their say.
12628           (cond
12629            ;; No split name was found.
12630            ((null split-name)
12631             (read-file-name
12632              (concat prompt " (default "
12633                      (file-name-nondirectory default-name) ") ")
12634              (file-name-directory default-name)
12635              default-name))
12636            ;; A single split name was found
12637            ((= 1 (length split-name))
12638             (read-file-name
12639              (concat prompt " (default " (car split-name) ") ")
12640              gnus-article-save-directory
12641              (concat gnus-article-save-directory (car split-name))))
12642            ;; A list of splits was found.
12643            (t
12644             (setq split-name (mapcar (lambda (el) (list el))
12645                                      (nreverse split-name)))
12646             (let ((result (completing-read
12647                            (concat prompt " ") split-name nil nil)))
12648               (concat gnus-article-save-directory
12649                       (if (string= result "")
12650                           (car (car split-name))
12651                         result)))))))
12652     ;; If we have read a directory, we append the default file name.
12653     (when (file-directory-p file)
12654       (setq file (concat (file-name-as-directory file)
12655                          (file-name-nondirectory default-name))))
12656     ;; Possibly translate some charaters.
12657     (nnheader-translate-file-chars file)))
12658
12659 (defun gnus-article-archive-name (group)
12660   "Return the first instance of an \"Archive-name\" in the current buffer."
12661   (let ((case-fold-search t))
12662     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12663       (match-string 1))))
12664
12665 (defun gnus-summary-save-in-rmail (&optional filename)
12666   "Append this article to Rmail file.
12667 Optional argument FILENAME specifies file name.
12668 Directory to save to is default to `gnus-article-save-directory' which
12669 is initialized from the SAVEDIR environment variable."
12670   (interactive)
12671   (gnus-set-global-variables)
12672   (let ((default-name
12673           (funcall gnus-rmail-save-name gnus-newsgroup-name
12674                    gnus-current-headers gnus-newsgroup-last-rmail)))
12675     (setq filename
12676           (cond ((eq filename 'default)
12677                  default-name)
12678                 (filename filename)
12679                 (t (gnus-read-save-file-name
12680                     "Save in rmail file:" default-name))))
12681     (gnus-make-directory (file-name-directory filename))
12682     (gnus-eval-in-buffer-window
12683      gnus-original-article-buffer
12684      (save-excursion
12685        (save-restriction
12686          (widen)
12687          (gnus-output-to-rmail filename))))
12688     ;; Remember the directory name to save articles
12689     (setq gnus-newsgroup-last-rmail filename)))
12690
12691 (defun gnus-summary-save-in-mail (&optional filename)
12692   "Append this article to Unix mail file.
12693 Optional argument FILENAME specifies file name.
12694 Directory to save to is default to `gnus-article-save-directory' which
12695 is initialized from the SAVEDIR environment variable."
12696   (interactive)
12697   (gnus-set-global-variables)
12698   (let ((default-name
12699           (funcall gnus-mail-save-name gnus-newsgroup-name
12700                    gnus-current-headers gnus-newsgroup-last-mail)))
12701     (setq filename
12702           (cond ((eq filename 'default)
12703                  default-name)
12704                 (filename filename)
12705                 (t (gnus-read-save-file-name
12706                     "Save in Unix mail file:" default-name))))
12707     (setq filename
12708           (expand-file-name filename
12709                             (and default-name
12710                                  (file-name-directory default-name))))
12711     (gnus-make-directory (file-name-directory filename))
12712     (gnus-eval-in-buffer-window
12713      gnus-original-article-buffer
12714      (save-excursion
12715        (save-restriction
12716          (widen)
12717          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12718              (gnus-output-to-rmail filename)
12719            (let ((mail-use-rfc822 t))
12720              (rmail-output filename 1 t t))))))
12721     ;; Remember the directory name to save articles.
12722     (setq gnus-newsgroup-last-mail filename)))
12723
12724 (defun gnus-summary-save-in-file (&optional filename)
12725   "Append this article to file.
12726 Optional argument FILENAME specifies file name.
12727 Directory to save to is default to `gnus-article-save-directory' which
12728 is initialized from the SAVEDIR environment variable."
12729   (interactive)
12730   (gnus-set-global-variables)
12731   (let ((default-name
12732           (funcall gnus-file-save-name gnus-newsgroup-name
12733                    gnus-current-headers gnus-newsgroup-last-file)))
12734     (setq filename
12735           (cond ((eq filename 'default)
12736                  default-name)
12737                 (filename filename)
12738                 (t (gnus-read-save-file-name
12739                     "Save in file:" default-name))))
12740     (gnus-make-directory (file-name-directory filename))
12741     (gnus-eval-in-buffer-window
12742      gnus-original-article-buffer
12743      (save-excursion
12744        (save-restriction
12745          (widen)
12746          (gnus-output-to-file filename))))
12747     ;; Remember the directory name to save articles.
12748     (setq gnus-newsgroup-last-file filename)))
12749
12750 (defun gnus-summary-save-body-in-file (&optional filename)
12751   "Append this article body to a file.
12752 Optional argument FILENAME specifies file name.
12753 The directory to save in defaults to `gnus-article-save-directory' which
12754 is initialized from the SAVEDIR environment variable."
12755   (interactive)
12756   (gnus-set-global-variables)
12757   (let ((default-name
12758           (funcall gnus-file-save-name gnus-newsgroup-name
12759                    gnus-current-headers gnus-newsgroup-last-file)))
12760     (setq filename
12761           (cond ((eq filename 'default)
12762                  default-name)
12763                 (filename filename)
12764                 (t (gnus-read-save-file-name
12765                     "Save body in file:" default-name))))
12766     (gnus-make-directory (file-name-directory filename))
12767     (gnus-eval-in-buffer-window
12768      gnus-article-buffer
12769      (save-excursion
12770        (save-restriction
12771          (widen)
12772          (goto-char (point-min))
12773          (and (search-forward "\n\n" nil t)
12774               (narrow-to-region (point) (point-max)))
12775          (gnus-output-to-file filename))))
12776     ;; Remember the directory name to save articles.
12777     (setq gnus-newsgroup-last-file filename)))
12778
12779 (defun gnus-summary-save-in-pipe (&optional command)
12780   "Pipe this article to subprocess."
12781   (interactive)
12782   (gnus-set-global-variables)
12783   (setq command
12784         (cond ((eq command 'default)
12785                gnus-last-shell-command)
12786               (command command)
12787               (t (read-string "Shell command on article: "
12788                               gnus-last-shell-command))))
12789   (if (string-equal command "")
12790       (setq command gnus-last-shell-command))
12791   (gnus-eval-in-buffer-window
12792    gnus-article-buffer
12793    (save-restriction
12794      (widen)
12795      (shell-command-on-region (point-min) (point-max) command nil)))
12796   (setq gnus-last-shell-command command))
12797
12798 ;; Summary extract commands
12799
12800 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12801   (let ((buffer-read-only nil)
12802         (article (gnus-summary-article-number))
12803         after-article b e)
12804     (or (gnus-summary-goto-subject article)
12805         (error (format "No such article: %d" article)))
12806     (gnus-summary-position-point)
12807     ;; If all commands are to be bunched up on one line, we collect
12808     ;; them here.
12809     (if gnus-view-pseudos-separately
12810         ()
12811       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12812             files action)
12813         (while ps
12814           (setq action (cdr (assq 'action (car ps))))
12815           (setq files (list (cdr (assq 'name (car ps)))))
12816           (while (and ps (cdr ps)
12817                       (string= (or action "1")
12818                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12819             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12820             (setcdr ps (cdr (cdr ps))))
12821           (if (not files)
12822               ()
12823             (if (not (string-match "%s" action))
12824                 (setq files (cons " " files)))
12825             (setq files (cons " " files))
12826             (and (assq 'execute (car ps))
12827                  (setcdr (assq 'execute (car ps))
12828                          (funcall (if (string-match "%s" action)
12829                                       'format 'concat)
12830                                   action
12831                                   (mapconcat (lambda (f) f) files " ")))))
12832           (setq ps (cdr ps)))))
12833     (if (and gnus-view-pseudos (not not-view))
12834         (while pslist
12835           (and (assq 'execute (car pslist))
12836                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12837                                      (eq gnus-view-pseudos 'not-confirm)))
12838           (setq pslist (cdr pslist)))
12839       (save-excursion
12840         (while pslist
12841           (setq after-article (or (cdr (assq 'article (car pslist)))
12842                                   (gnus-summary-article-number)))
12843           (gnus-summary-goto-subject after-article)
12844           (forward-line 1)
12845           (setq b (point))
12846           (insert "          " (file-name-nondirectory
12847                                 (cdr (assq 'name (car pslist))))
12848                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12849           (setq e (point))
12850           (forward-line -1)             ; back to `b'
12851           (add-text-properties
12852            b e (list 'gnus-number gnus-reffed-article-number
12853                      gnus-mouse-face-prop gnus-mouse-face))
12854           (gnus-data-enter
12855            after-article gnus-reffed-article-number
12856            gnus-unread-mark b (car pslist) 0 (- e b))
12857           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12858           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12859           (setq pslist (cdr pslist)))))))
12860
12861 (defun gnus-pseudos< (p1 p2)
12862   (let ((c1 (cdr (assq 'action p1)))
12863         (c2 (cdr (assq 'action p2))))
12864     (and c1 c2 (string< c1 c2))))
12865
12866 (defun gnus-request-pseudo-article (props)
12867   (cond ((assq 'execute props)
12868          (gnus-execute-command (cdr (assq 'execute props)))))
12869   (let ((gnus-current-article (gnus-summary-article-number)))
12870     (run-hooks 'gnus-mark-article-hook)))
12871
12872 (defun gnus-execute-command (command &optional automatic)
12873   (save-excursion
12874     (gnus-article-setup-buffer)
12875     (set-buffer gnus-article-buffer)
12876     (let ((command (if automatic command (read-string "Command: " command)))
12877           (buffer-read-only nil))
12878       (erase-buffer)
12879       (insert "$ " command "\n\n")
12880       (if gnus-view-pseudo-asynchronously
12881           (start-process "gnus-execute" nil "sh" "-c" command)
12882         (call-process "sh" nil t nil "-c" command)))))
12883
12884 (defun gnus-copy-file (file &optional to)
12885   "Copy FILE to TO."
12886   (interactive
12887    (list (read-file-name "Copy file: " default-directory)
12888          (read-file-name "Copy file to: " default-directory)))
12889   (gnus-set-global-variables)
12890   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12891   (and (file-directory-p to)
12892        (setq to (concat (file-name-as-directory to)
12893                         (file-name-nondirectory file))))
12894   (copy-file file to))
12895
12896 ;; Summary kill commands.
12897
12898 (defun gnus-summary-edit-global-kill (article)
12899   "Edit the \"global\" kill file."
12900   (interactive (list (gnus-summary-article-number)))
12901   (gnus-set-global-variables)
12902   (gnus-group-edit-global-kill article))
12903
12904 (defun gnus-summary-edit-local-kill ()
12905   "Edit a local kill file applied to the current newsgroup."
12906   (interactive)
12907   (gnus-set-global-variables)
12908   (setq gnus-current-headers (gnus-summary-article-header))
12909   (gnus-set-global-variables)
12910   (gnus-group-edit-local-kill
12911    (gnus-summary-article-number) gnus-newsgroup-name))
12912
12913 \f
12914 ;;;
12915 ;;; Gnus article mode
12916 ;;;
12917
12918 (put 'gnus-article-mode 'mode-class 'special)
12919
12920 (if gnus-article-mode-map
12921     nil
12922   (setq gnus-article-mode-map (make-keymap))
12923   (suppress-keymap gnus-article-mode-map)
12924
12925   (gnus-define-keys
12926    gnus-article-mode-map
12927    " " gnus-article-goto-next-page
12928    "\177" gnus-article-goto-prev-page
12929    [delete] gnus-article-goto-prev-page
12930    "\C-c^" gnus-article-refer-article
12931    "h" gnus-article-show-summary
12932    "s" gnus-article-show-summary
12933    "\C-c\C-m" gnus-article-mail
12934    "?" gnus-article-describe-briefly
12935    gnus-mouse-2 gnus-article-push-button
12936    "\r" gnus-article-press-button
12937    "\t" gnus-article-next-button
12938    "\M-\t" gnus-article-prev-button
12939    "\C-c\C-b" gnus-bug)
12940
12941   (substitute-key-definition
12942    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
12943
12944 (defun gnus-article-mode ()
12945   "Major mode for displaying an article.
12946
12947 All normal editing commands are switched off.
12948
12949 The following commands are available:
12950
12951 \\<gnus-article-mode-map>
12952 \\[gnus-article-next-page]\t Scroll the article one page forwards
12953 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12954 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12955 \\[gnus-article-show-summary]\t Display the summary buffer
12956 \\[gnus-article-mail]\t Send a reply to the address near point
12957 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12958 \\[gnus-info-find-node]\t Go to the Gnus info node"
12959   (interactive)
12960   (when (and menu-bar-mode
12961              (gnus-visual-p 'article-menu 'menu))
12962     (gnus-article-make-menu-bar))
12963   (kill-all-local-variables)
12964   (gnus-simplify-mode-line)
12965   (setq mode-name "Article")
12966   (setq major-mode 'gnus-article-mode)
12967   (make-local-variable 'minor-mode-alist)
12968   (or (assq 'gnus-show-mime minor-mode-alist)
12969       (setq minor-mode-alist
12970             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
12971   (use-local-map gnus-article-mode-map)
12972   (make-local-variable 'page-delimiter)
12973   (setq page-delimiter gnus-page-delimiter)
12974   (buffer-disable-undo (current-buffer))
12975   (setq buffer-read-only t)             ;Disable modification
12976   (run-hooks 'gnus-article-mode-hook))
12977
12978 (defun gnus-article-setup-buffer ()
12979   "Initialize the article buffer."
12980   (let* ((name (if gnus-single-article-buffer "*Article*"
12981                  (concat "*Article " gnus-newsgroup-name "*")))
12982          (original
12983           (progn (string-match "\\*Article" name)
12984                  (concat " *Original Article"
12985                          (substring name (match-end 0))))))
12986     (setq gnus-article-buffer name)
12987     (setq gnus-original-article-buffer original)
12988     ;; This might be a variable local to the summary buffer.
12989     (unless gnus-single-article-buffer
12990       (save-excursion
12991         (set-buffer gnus-summary-buffer)
12992         (setq gnus-article-buffer name)
12993         (setq gnus-original-article-buffer original)
12994         (gnus-set-global-variables))
12995       (make-local-variable 'gnus-summary-buffer))
12996     (if (get-buffer name)
12997         (save-excursion
12998           (set-buffer name)
12999           (buffer-disable-undo (current-buffer))
13000           (setq buffer-read-only t)
13001           (gnus-add-current-to-buffer-list)
13002           (or (eq major-mode 'gnus-article-mode)
13003               (gnus-article-mode))
13004           (current-buffer))
13005       (save-excursion
13006         (set-buffer (get-buffer-create name))
13007         (gnus-add-current-to-buffer-list)
13008         (gnus-article-mode)
13009         (current-buffer)))))
13010
13011 ;; Set article window start at LINE, where LINE is the number of lines
13012 ;; from the head of the article.
13013 (defun gnus-article-set-window-start (&optional line)
13014   (set-window-start
13015    (get-buffer-window gnus-article-buffer)
13016    (save-excursion
13017      (set-buffer gnus-article-buffer)
13018      (goto-char (point-min))
13019      (if (not line)
13020          (point-min)
13021        (gnus-message 6 "Moved to bookmark")
13022        (search-forward "\n\n" nil t)
13023        (forward-line line)
13024        (point)))))
13025
13026 (defun gnus-kill-all-overlays ()
13027   "Delete all overlays in the current buffer."
13028   (when (fboundp 'overlay-lists)
13029     (let* ((overlayss (overlay-lists))
13030            (buffer-read-only nil)
13031            (overlays (nconc (car overlayss) (cdr overlayss))))
13032       (while overlays
13033         (delete-overlay (pop overlays))))))
13034
13035 (defun gnus-request-article-this-buffer (article group)
13036   "Get an article and insert it into this buffer."
13037   (prog1
13038       (save-excursion
13039         (erase-buffer)
13040         (gnus-kill-all-overlays)
13041         (setq group (or group gnus-newsgroup-name))
13042
13043         ;; Open server if it has closed.
13044         (gnus-check-server (gnus-find-method-for-group group))
13045
13046         ;; Using `gnus-request-article' directly will insert the article into
13047         ;; `nntp-server-buffer' - so we'll save some time by not having to
13048         ;; copy it from the server buffer into the article buffer.
13049
13050         ;; We only request an article by message-id when we do not have the
13051         ;; headers for it, so we'll have to get those.
13052         (when (stringp article)
13053           (let ((gnus-override-method gnus-refer-article-method))
13054             (gnus-read-header article)))
13055
13056         ;; If the article number is negative, that means that this article
13057         ;; doesn't belong in this newsgroup (possibly), so we find its
13058         ;; message-id and request it by id instead of number.
13059         (when (numberp article)
13060           (save-excursion
13061             (set-buffer gnus-summary-buffer)
13062             (let ((header (gnus-summary-article-header article)))
13063               (if (< article 0)
13064                   (cond 
13065                    ((memq article gnus-newsgroup-sparse)
13066                     ;; This is a sparse gap article.
13067                     (setq article (mail-header-id header)))
13068                    ((vectorp header)
13069                     ;; It's a real article.
13070                     (setq article (mail-header-id header)))
13071                    (t
13072                     ;; It is an extracted pseudo-article.
13073                     (setq article 'pseudo)
13074                     (gnus-request-pseudo-article header))))
13075                 
13076               (let ((method (gnus-find-method-for-group 
13077                              gnus-newsgroup-name)))
13078                 (if (not (eq (car method) 'nneething))
13079                     ()
13080                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13081                                      (mail-header-subject header))))
13082                     (if (file-directory-p dir)
13083                         (progn
13084                           (setq article 'nneething)
13085                           (gnus-group-enter-directory dir)))))))))
13086
13087         (cond
13088          ;; We first check `gnus-original-article-buffer'.
13089          ((and (equal (car gnus-original-article) group)
13090                (eq (cdr gnus-original-article) article)
13091                (get-buffer gnus-original-article-buffer))
13092           (insert-buffer-substring gnus-original-article-buffer)
13093           'article)
13094          ;; Check the backlog.
13095          ((and gnus-keep-backlog
13096                (gnus-backlog-request-article group article (current-buffer)))
13097           'article)
13098          ;; Check the cache.
13099          ((and gnus-use-cache
13100                (numberp article)
13101                (gnus-cache-request-article article group))
13102           'article)
13103          ;; Get the article and put into the article buffer.
13104          ((or (stringp article) (numberp article))
13105           (let ((gnus-override-method
13106                  (and (stringp article) gnus-refer-article-method))
13107                 (buffer-read-only nil))
13108             (erase-buffer)
13109             (gnus-kill-all-overlays)
13110             (if (gnus-request-article article group (current-buffer))
13111                 (progn
13112                   (and gnus-keep-backlog
13113                        (gnus-backlog-enter-article
13114                         group article (current-buffer)))
13115                   'article))))
13116          ;; It was a pseudo.
13117          (t article)))
13118
13119     ;; Take the article from the original article buffer
13120     ;; and place it in the buffer it's supposed to be in.
13121     (setq gnus-original-article (cons group article))
13122     (when (and (get-buffer gnus-article-buffer)
13123                (equal (buffer-name (current-buffer))
13124                       (buffer-name (get-buffer gnus-article-buffer))))
13125       (save-excursion
13126         (if (get-buffer gnus-original-article-buffer)
13127             (set-buffer (get-buffer gnus-original-article-buffer))
13128           (set-buffer (get-buffer-create gnus-original-article-buffer))
13129           (buffer-disable-undo (current-buffer))
13130           (setq major-mode 'gnus-original-article-mode)
13131           (setq buffer-read-only t)
13132           (gnus-add-current-to-buffer-list))
13133         (let (buffer-read-only)
13134           (erase-buffer)
13135           (insert-buffer-substring gnus-article-buffer))))
13136     
13137     ;; Update sparse articles.
13138     (when (memq article gnus-newsgroup-sparse)
13139       (gnus-summary-update-article article))))
13140
13141 (defun gnus-read-header (id)
13142   "Read the headers of article ID and enter them into the Gnus system."
13143   (let ((group gnus-newsgroup-name)
13144         (headers gnus-newsgroup-headers)
13145         header where)
13146     ;; First we check to see whether the header in question is already
13147     ;; fetched.
13148     (if (stringp id)
13149         ;; This is a Message-ID.
13150         (setq header (gnus-id-to-header id))
13151       ;; This is an article number.
13152       (setq header (gnus-summary-article-header id)))
13153     (if header
13154         ;; We have found the header.
13155         header
13156       ;; We have to really fetch the header to this article.
13157       (when (setq where
13158                   (if (gnus-check-backend-function 'request-head group)
13159                       (gnus-request-head id group)
13160                     (gnus-request-article id group)))
13161         (save-excursion
13162           (set-buffer nntp-server-buffer)
13163           (and (search-forward "\n\n" nil t)
13164                (delete-region (1- (point)) (point-max)))
13165           (goto-char (point-max))
13166           (insert ".\n")
13167           (goto-char (point-min))
13168           (insert "211 "
13169                   (int-to-string
13170                    (cond
13171                     ((numberp id)
13172                      id)
13173                     ((cdr where)
13174                      (cdr where))
13175                     (t
13176                      gnus-reffed-article-number)))
13177                   " Article retrieved.\n"))
13178         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13179             () ; Malformed head.
13180           (if (and (stringp id)
13181                    (not (string= (gnus-group-real-name group)
13182                                  (car where))))
13183               ;; If we fetched by Message-ID and the article came
13184               ;; from a different group, we fudge some bogus article
13185               ;; numbers for this article.
13186               (mail-header-set-number header gnus-reffed-article-number))
13187           (decf gnus-reffed-article-number)
13188           (push header gnus-newsgroup-headers)
13189           (setq gnus-current-headers header)
13190           (push (mail-header-number header) gnus-newsgroup-limit)
13191           header)))))
13192
13193 (defun gnus-article-prepare (article &optional all-headers header)
13194   "Prepare ARTICLE in article mode buffer.
13195 ARTICLE should either be an article number or a Message-ID.
13196 If ARTICLE is an id, HEADER should be the article headers.
13197 If ALL-HEADERS is non-nil, no headers are hidden."
13198   (save-excursion
13199     ;; Make sure we start in a summary buffer.
13200     (unless (eq major-mode 'gnus-summary-mode)
13201       (set-buffer gnus-summary-buffer))
13202     (setq gnus-summary-buffer (current-buffer))
13203     ;; Make sure the connection to the server is alive.
13204     (unless (gnus-server-opened
13205              (gnus-find-method-for-group gnus-newsgroup-name))
13206       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13207       (gnus-request-group gnus-newsgroup-name t))
13208     (let* ((article (if header (mail-header-number header) article))
13209            (summary-buffer (current-buffer))
13210            (internal-hook gnus-article-internal-prepare-hook)
13211            (group gnus-newsgroup-name)
13212            result)
13213       (save-excursion
13214         (gnus-article-setup-buffer)
13215         (set-buffer gnus-article-buffer)
13216         ;; Deactivate active regions.
13217         (when (and (boundp 'transient-mark-mode)
13218                    transient-mark-mode)
13219           (setq mark-active nil))
13220         (if (not (setq result (let ((buffer-read-only nil))
13221                                 (gnus-request-article-this-buffer
13222                                  article group))))
13223             ;; There is no such article.
13224             (save-excursion
13225               (when (and (numberp article)
13226                          (not (memq article gnus-newsgroup-sparse)))
13227                 (setq gnus-article-current
13228                       (cons gnus-newsgroup-name article))
13229                 (set-buffer gnus-summary-buffer)
13230                 (setq gnus-current-article article)
13231                 (gnus-summary-mark-article article gnus-canceled-mark))
13232               (unless (memq article gnus-newsgroup-sparse)
13233                 (gnus-message
13234                  1 "No such article (may have expired or been canceled)")
13235                 (ding)
13236                 nil))
13237           (if (or (eq result 'pseudo) (eq result 'nneething))
13238               (progn
13239                 (save-excursion
13240                   (set-buffer summary-buffer)
13241                   (setq gnus-last-article gnus-current-article
13242                         gnus-newsgroup-history (cons gnus-current-article
13243                                                      gnus-newsgroup-history)
13244                         gnus-current-article 0
13245                         gnus-current-headers nil
13246                         gnus-article-current nil)
13247                   (if (eq result 'nneething)
13248                       (gnus-configure-windows 'summary)
13249                     (gnus-configure-windows 'article))
13250                   (gnus-set-global-variables))
13251                 (gnus-set-mode-line 'article))
13252             ;; The result from the `request' was an actual article -
13253             ;; or at least some text that is now displayed in the
13254             ;; article buffer.
13255             (if (and (numberp article)
13256                      (not (eq article gnus-current-article)))
13257                 ;; Seems like a new article has been selected.
13258                 ;; `gnus-current-article' must be an article number.
13259                 (save-excursion
13260                   (set-buffer summary-buffer)
13261                   (setq gnus-last-article gnus-current-article
13262                         gnus-newsgroup-history (cons gnus-current-article
13263                                                      gnus-newsgroup-history)
13264                         gnus-current-article article
13265                         gnus-current-headers
13266                         (gnus-summary-article-header gnus-current-article)
13267                         gnus-article-current
13268                         (cons gnus-newsgroup-name gnus-current-article))
13269                   (unless (vectorp gnus-current-headers)
13270                     (setq gnus-current-headers nil))
13271                   (gnus-summary-show-thread)
13272                   (run-hooks 'gnus-mark-article-hook)
13273                   (gnus-set-mode-line 'summary)
13274                   (and (gnus-visual-p 'article-highlight 'highlight)
13275                        (run-hooks 'gnus-visual-mark-article-hook))
13276                   ;; Set the global newsgroup variables here.
13277                   ;; Suggested by Jim Sisolak
13278                   ;; <sisolak@trans4.neep.wisc.edu>.
13279                   (gnus-set-global-variables)
13280                   (setq gnus-have-all-headers
13281                         (or all-headers gnus-show-all-headers))
13282                   (and gnus-use-cache
13283                        (vectorp (gnus-summary-article-header article))
13284                        (gnus-cache-possibly-enter-article
13285                         group article
13286                         (gnus-summary-article-header article)
13287                         (memq article gnus-newsgroup-marked)
13288                         (memq article gnus-newsgroup-dormant)
13289                         (memq article gnus-newsgroup-unreads)))))
13290             ;; Hooks for getting information from the article.
13291             ;; This hook must be called before being narrowed.
13292             (let (buffer-read-only)
13293               (run-hooks 'internal-hook)
13294               (run-hooks 'gnus-article-prepare-hook)
13295               ;; Decode MIME message.
13296               (if gnus-show-mime
13297                   (if (or (not gnus-strict-mime)
13298                           (gnus-fetch-field "Mime-Version"))
13299                       (funcall gnus-show-mime-method)
13300                     (funcall gnus-decode-encoded-word-method)))
13301               ;; Perform the article display hooks.
13302               (run-hooks 'gnus-article-display-hook))
13303             ;; Do page break.
13304             (goto-char (point-min))
13305             (and gnus-break-pages (gnus-narrow-to-page))
13306             (gnus-set-mode-line 'article)
13307             (gnus-configure-windows 'article)
13308             (goto-char (point-min))
13309             t))))))
13310
13311 (defun gnus-article-show-all-headers ()
13312   "Show all article headers in article mode buffer."
13313   (save-excursion
13314     (gnus-article-setup-buffer)
13315     (set-buffer gnus-article-buffer)
13316     (let ((buffer-read-only nil))
13317       (remove-text-properties (point-min) (point-max)
13318                               gnus-hidden-properties))))
13319
13320 (defun gnus-article-hide-headers-if-wanted ()
13321   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13322 Provided for backwards compatibility."
13323   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13324       gnus-inhibit-hiding
13325       (gnus-article-hide-headers)))
13326
13327 (defun gnus-article-hide-headers (&optional arg delete)
13328   "Toggle whether to hide unwanted headers and possibly sort them as well.
13329 If given a negative prefix, always show; if given a positive prefix,
13330 always hide."
13331   (interactive "P")
13332   (unless (gnus-article-check-hidden-text 'headers arg)
13333     ;; This function might be inhibited.
13334     (unless gnus-inhibit-hiding
13335       (save-excursion
13336         (set-buffer gnus-article-buffer)
13337         (save-restriction
13338           (let ((buffer-read-only nil)
13339                 (ignored (when (not (stringp gnus-visible-headers))
13340                            (cond ((stringp gnus-ignored-headers)
13341                                   gnus-ignored-headers)
13342                                  ((listp gnus-ignored-headers)
13343                                   (mapconcat 'identity gnus-ignored-headers
13344                                              "\\|")))))
13345                 (visible
13346                  (cond ((stringp gnus-visible-headers)
13347                         gnus-visible-headers)
13348                        ((and gnus-visible-headers
13349                              (listp gnus-visible-headers))
13350                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13351                 want-list beg want-l)
13352             ;; First we narrow to just the headers.
13353             (widen)
13354             (goto-char (point-min))
13355             ;; Hide any "From " lines at the beginning of (mail) articles.
13356             (while (looking-at "From ")
13357               (forward-line 1))
13358             (unless (bobp)
13359               (add-text-properties
13360                (point-min) (point)
13361                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13362             ;; Then treat the rest of the header lines.
13363             (narrow-to-region
13364              (point)
13365              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13366             ;; Then we use the two regular expressions
13367             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13368             ;; select which header lines is to remain visible in the
13369             ;; article buffer.
13370             (goto-char (point-min))
13371             (while (re-search-forward "^[^ \t]*:" nil t)
13372               (beginning-of-line)
13373               ;; We add the headers we want to keep to a list and delete
13374               ;; them from the buffer.
13375               (if (or (and visible (looking-at visible))
13376                       (and ignored (not (looking-at ignored))))
13377                   (progn
13378                     (push (buffer-substring
13379                            (setq beg (point))
13380                            (progn
13381                              (forward-line 1)
13382                              ;; Be sure to get multi-line headers...
13383                              (re-search-forward "^[^ \t]*:" nil t)
13384                              (beginning-of-line)
13385                              (point)))
13386                           want-list)
13387                     (delete-region beg (point)))
13388                 (forward-line 1)))
13389             ;; Sort the headers that we want to display.
13390             (setq want-list (sort want-list 'gnus-article-header-less))
13391             (goto-char (point-min))
13392             (while want-list
13393               (insert (pop want-list)))
13394             ;; We make the unwanted headers invisible.
13395             (if delete
13396                 (delete-region (point-min) (point-max))
13397               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13398               (add-text-properties
13399                (point) (point-max)
13400                (nconc (list 'gnus-type 'headers)
13401                       gnus-hidden-properties)))))))))
13402
13403 (defsubst gnus-article-header-rank (header)
13404   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13405   (let ((list gnus-sorted-header-list)
13406         (i 0))
13407     (while list
13408       (when (string-match (car list) header)
13409         (setq list nil))
13410       (setq list (cdr list))
13411       (incf i))
13412     i))
13413
13414 (defun gnus-article-header-less (h1 h2)
13415   "Say whether string H1 is \"less\" than string H2."
13416   (< (gnus-article-header-rank h1)
13417      (gnus-article-header-rank h2)))
13418
13419 (defun gnus-article-hide-boring-headers (&optional arg)
13420   "Toggle hiding of headers that aren't very interesting.
13421 If given a negative prefix, always show; if given a positive prefix,
13422 always hide."
13423   (interactive "P")
13424   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13425     (save-excursion
13426       (set-buffer gnus-article-buffer)
13427       (save-restriction
13428         (let ((buffer-read-only nil)
13429               (list gnus-boring-article-headers)
13430               (inhibit-point-motion-hooks t)
13431               elem)
13432           (nnheader-narrow-to-headers)
13433           (while list
13434             (setq elem (pop list))
13435             (goto-char (point-min))
13436             (cond
13437              ;; Hide empty headers.
13438              ((eq elem 'empty)
13439               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13440                 (forward-line -1)
13441                 (add-text-properties
13442                  (progn (beginning-of-line) (point))
13443                  (progn 
13444                    (end-of-line)
13445                    (if (re-search-forward "^[^ \t]" nil t)
13446                        (match-beginning 0)
13447                      (point-max)))
13448                  (nconc (list 'gnus-type 'boring-headers)
13449                         gnus-hidden-properties))))
13450              ;; Hide boring Newsgroups header.
13451              ((eq elem 'newsgroups)
13452               (when (equal (mail-fetch-field "newsgroups")
13453                            (gnus-group-real-name gnus-newsgroup-name))
13454                 (gnus-article-hide-header "newsgroups")))
13455              ((eq elem 'followup-to)
13456               (when (equal (mail-fetch-field "followup-to")
13457                            (mail-fetch-field "newsgroups"))
13458                 (gnus-article-hide-header "followup-to")))
13459              ((eq elem 'reply-to)
13460               (let ((from (mail-fetch-field "from"))
13461                     (reply-to (mail-fetch-field "reply-to")))
13462                 (when (and
13463                        from reply-to
13464                        (equal 
13465                         (nth 1 (mail-extract-address-components from))
13466                         (nth 1 (mail-extract-address-components reply-to))))
13467                   (gnus-article-hide-header "reply-to"))))
13468              ((eq elem 'date)
13469               (let ((date (mail-fetch-field "date")))
13470                 (when (and date
13471                            (< (gnus-days-between date (current-time-string))
13472                               4))
13473                   (gnus-article-hide-header "date")))))))))))
13474
13475 (defun gnus-article-hide-header (header)
13476   (save-excursion
13477     (goto-char (point-min))
13478     (when (re-search-forward (concat "^" header ":") nil t)
13479       (add-text-properties
13480        (progn (beginning-of-line) (point))
13481        (progn 
13482          (end-of-line)
13483          (if (re-search-forward "^[^ \t]" nil t)
13484              (match-beginning 0)
13485            (point-max)))
13486        (nconc (list 'gnus-type 'boring-headers)
13487               gnus-hidden-properties)))))
13488
13489 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13490 (defun gnus-article-treat-overstrike ()
13491   "Translate overstrikes into bold text."
13492   (interactive)
13493   (save-excursion
13494     (set-buffer gnus-article-buffer)
13495     (let ((buffer-read-only nil))
13496       (while (search-forward "\b" nil t)
13497         (let ((next (following-char))
13498               (previous (char-after (- (point) 2))))
13499           (cond ((eq next previous)
13500                  (put-text-property (- (point) 2) (point) 'invisible t)
13501                  (put-text-property (point) (1+ (point)) 'face 'bold))
13502                 ((eq next ?_)
13503                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13504                  (put-text-property
13505                   (- (point) 2) (1- (point)) 'face 'underline))
13506                 ((eq previous ?_)
13507                  (put-text-property (- (point) 2) (point) 'invisible t)
13508                  (put-text-property
13509                   (point) (1+ (point))  'face 'underline))))))))
13510
13511 (defun gnus-article-word-wrap ()
13512   "Format too long lines."
13513   (interactive)
13514   (save-excursion
13515     (set-buffer gnus-article-buffer)
13516     (let ((buffer-read-only nil)
13517           p)
13518       (widen)
13519       (goto-char (point-min))
13520       (search-forward "\n\n" nil t)
13521       (end-of-line 1)
13522       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13523             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13524             (adaptive-fill-mode t))
13525         (while (not (eobp))
13526           (and (>= (current-column) (min fill-column (window-width)))
13527                (/= (preceding-char) ?:)
13528                (fill-paragraph nil))
13529           (end-of-line 2))))))
13530
13531 (defun gnus-article-remove-cr ()
13532   "Remove carriage returns from an article."
13533   (interactive)
13534   (save-excursion
13535     (set-buffer gnus-article-buffer)
13536     (let ((buffer-read-only nil))
13537       (goto-char (point-min))
13538       (while (search-forward "\r" nil t)
13539         (replace-match "" t t)))))
13540
13541 (defun gnus-article-remove-trailing-blank-lines ()
13542   "Remove all trailing blank lines from the article."
13543   (interactive)
13544   (save-excursion
13545     (set-buffer gnus-article-buffer)
13546     (let ((buffer-read-only nil))
13547       (goto-char (point-max))
13548       (delete-region
13549        (point)
13550        (progn
13551          (while (looking-at "^[ \t]*$")
13552            (forward-line -1))
13553          (forward-line 1)
13554          (point))))))
13555
13556 (defun gnus-article-display-x-face (&optional force)
13557   "Look for an X-Face header and display it if present."
13558   (interactive (list 'force))
13559   (save-excursion
13560     (set-buffer gnus-article-buffer)
13561     ;; Delete the old process, if any.
13562     (when (process-status "gnus-x-face")
13563       (delete-process "gnus-x-face"))
13564     (let ((inhibit-point-motion-hooks t)
13565           (case-fold-search nil)
13566           from)
13567       (save-restriction
13568         (nnheader-narrow-to-headers)
13569         (setq from (mail-fetch-field "from"))
13570         (goto-char (point-min))
13571         (when (and gnus-article-x-face-command
13572                    (or force
13573                        ;; Check whether this face is censored.
13574                        (not gnus-article-x-face-too-ugly)
13575                        (and gnus-article-x-face-too-ugly from
13576                             (not (string-match gnus-article-x-face-too-ugly
13577                                                from))))
13578                    ;; Has to be present.
13579                    (re-search-forward "^X-Face: " nil t))
13580           ;; We now have the area of the buffer where the X-Face is stored.
13581           (let ((beg (point))
13582                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13583             ;; We display the face.
13584             (if (symbolp gnus-article-x-face-command)
13585                 ;; The command is a lisp function, so we call it.
13586                 (if (gnus-functionp gnus-article-x-face-command)
13587                     (funcall gnus-article-x-face-command beg end)
13588                   (error "%s is not a function" gnus-article-x-face-command))
13589               ;; The command is a string, so we interpret the command
13590               ;; as a, well, command, and fork it off.
13591               (let ((process-connection-type nil))
13592                 (process-kill-without-query
13593                  (start-process
13594                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13595                 (process-send-region "gnus-x-face" beg end)
13596                 (process-send-eof "gnus-x-face")))))))))
13597
13598 (defun gnus-headers-decode-quoted-printable ()
13599   "Hack to remove QP encoding from headers."
13600   (let ((case-fold-search t)
13601         (inhibit-point-motion-hooks t)
13602         string)
13603     (goto-char (point-min))
13604     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13605       (setq string (match-string 1))
13606       (narrow-to-region (match-beginning 0) (match-end 0))
13607       (delete-region (point-min) (point-max))
13608       (insert string)
13609       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13610       (subst-char-in-region (point-min) (point-max) ?_ ? )
13611       (widen)
13612       (goto-char (point-min)))))
13613
13614 (defun gnus-article-de-quoted-unreadable (&optional force)
13615   "Do a naive translation of a quoted-printable-encoded article.
13616 This is in no way, shape or form meant as a replacement for real MIME
13617 processing, but is simply a stop-gap measure until MIME support is
13618 written.
13619 If FORCE, decode the article whether it is marked as quoted-printable
13620 or not."
13621   (interactive (list 'force))
13622   (save-excursion
13623     (set-buffer gnus-article-buffer)
13624     (let ((case-fold-search t)
13625           (buffer-read-only nil)
13626           (type (gnus-fetch-field "content-transfer-encoding")))
13627       (when (or force
13628                 (and type (string-match "quoted-printable" type)))
13629         (gnus-headers-decode-quoted-printable)
13630         (goto-char (point-min))
13631         (search-forward "\n\n" nil 'move)
13632         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13633
13634 (defun gnus-mime-decode-quoted-printable (from to)
13635   "Decode Quoted-Printable in the region between FROM and TO."
13636   (goto-char from)
13637   (while (search-forward "=" to t)
13638     (cond ((eq (following-char) ?\n)
13639            (delete-char -1)
13640            (delete-char 1))
13641           ((looking-at "[0-9A-F][0-9A-F]")
13642            (delete-char -1)
13643            (insert (hexl-hex-string-to-integer
13644                     (buffer-substring (point) (+ 2 (point)))))
13645            (delete-char 2))
13646           ((looking-at "=")
13647            (delete-char 1))
13648           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13649
13650 (defun gnus-article-hide-pgp (&optional arg)
13651   "Toggle hiding of any PGP headers and signatures in the current article.
13652 If given a negative prefix, always show; if given a positive prefix,
13653 always hide."
13654   (interactive "P")
13655   (unless (gnus-article-check-hidden-text 'pgp arg)
13656     (save-excursion
13657       (set-buffer gnus-article-buffer)
13658       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13659             buffer-read-only beg end)
13660         (widen)
13661         (goto-char (point-min))
13662         ;; Hide the "header".
13663         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13664              (add-text-properties (match-beginning 0) (match-end 0) props))
13665         (setq beg (point))
13666         ;; Hide the actual signature.
13667         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13668              (setq end (match-beginning 0))
13669              (add-text-properties
13670               (match-beginning 0)
13671               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13672                   (match-end 0)
13673                 ;; Perhaps we shouldn't hide to the end of the buffer
13674                 ;; if there is no end to the signature?
13675                 (point-max))
13676               props))
13677         ;; Hide "- " PGP quotation markers.
13678         (when (and beg end)
13679           (narrow-to-region beg end)
13680           (goto-char (point-min))
13681           (while (re-search-forward "^- " nil t)
13682             (add-text-properties (match-beginning 0) (match-end 0) props))
13683           (widen))))))
13684
13685 (defun gnus-article-hide-signature (&optional arg)
13686   "Hide the signature in the current article.
13687 If given a negative prefix, always show; if given a positive prefix,
13688 always hide."
13689   (interactive "P")
13690   (unless (gnus-article-check-hidden-text 'signature arg)
13691     (save-excursion
13692       (set-buffer gnus-article-buffer)
13693       (save-restriction
13694         (let ((buffer-read-only nil))
13695           (when (gnus-narrow-to-signature)
13696             (add-text-properties
13697              (point-min) (point-max)
13698              (nconc (list 'gnus-type 'signature)
13699                     gnus-hidden-properties))))))))
13700
13701 (defvar gnus-signature-limit nil
13702   "Provide a limit to what is considered a signature.
13703 If it is a number, no signature may not be longer (in characters) than
13704 that number.  If it is a function, the function will be called without
13705 any parameters, and if it returns nil, there is no signature in the
13706 buffer.  If it is a string, it will be used as a regexp.  If it
13707 matches, the text in question is not a signature.")
13708
13709 (defun gnus-narrow-to-signature ()
13710   "Narrow to the signature."
13711   (widen)
13712   (goto-char (point-max))
13713   (when (re-search-backward gnus-signature-separator nil t)
13714     (forward-line 1)
13715     (when (or (null gnus-signature-limit)
13716               (and (numberp gnus-signature-limit)
13717                    (< (- (point-max) (point)) gnus-signature-limit))
13718               (and (gnus-functionp gnus-signature-limit)
13719                    (funcall gnus-signature-limit))
13720               (and (stringp gnus-signature-limit)
13721                    (not (re-search-forward gnus-signature-limit nil t))))
13722       (narrow-to-region (point) (point-max))
13723       t)))
13724
13725 (defun gnus-article-check-hidden-text (type arg)
13726   "Return nil if hiding is necessary."
13727   (save-excursion
13728     (set-buffer gnus-article-buffer)
13729     (let ((hide (gnus-article-hidden-text-p type)))
13730       (cond ((or (and (null arg) (eq hide 'hidden))
13731                  (and arg (< 0 (prefix-numeric-value arg))))
13732              (gnus-article-show-hidden-text type))
13733             ((eq hide 'shown)
13734              (gnus-article-show-hidden-text type t))
13735             (t nil)))))
13736
13737 (defun gnus-article-hidden-text-p (type)
13738   "Say whether the current buffer contains hidden text of type TYPE."
13739   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13740         prop)
13741     (when pos
13742       (if (get-text-property pos 'invisible)
13743           'hidden
13744         'shown))))
13745
13746 (defun gnus-article-hide (&optional arg force)
13747   "Hide all the gruft in the current article.
13748 This means that PGP stuff, signatures, cited text and (some)
13749 headers will be hidden.
13750 If given a prefix, show the hidden text instead."
13751   (interactive (list current-prefix-arg 'force))
13752   (gnus-article-hide-headers arg)
13753   (gnus-article-hide-pgp arg)
13754   (gnus-article-hide-citation-maybe arg force)
13755   (gnus-article-hide-signature arg))
13756
13757 (defun gnus-article-show-hidden-text (type &optional hide)
13758   "Show all hidden text of type TYPE.
13759 If HIDE, hide the text instead."
13760   (save-excursion
13761     (set-buffer gnus-article-buffer)
13762     (let ((buffer-read-only nil)
13763           (inhibit-point-motion-hooks t)
13764           (beg (point-min)))
13765       (while (gnus-goto-char (text-property-any
13766                               beg (point-max) 'gnus-type type))
13767         (setq beg (point))
13768         (forward-char)
13769         (if hide
13770             (add-text-properties beg (point) gnus-hidden-properties)
13771           (remove-text-properties beg (point) gnus-hidden-properties))
13772         (setq beg (point)))
13773       t)))
13774
13775 (defvar gnus-article-time-units
13776   `((year . ,(* 365.25 24 60 60))
13777     (week . ,(* 7 24 60 60))
13778     (day . ,(* 24 60 60))
13779     (hour . ,(* 60 60))
13780     (minute . 60)
13781     (second . 1))
13782   "Mapping from time units to seconds.")
13783
13784 (defun gnus-article-date-ut (&optional type highlight)
13785   "Convert DATE date to universal time in the current article.
13786 If TYPE is `local', convert to local time; if it is `lapsed', output
13787 how much time has lapsed since DATE."
13788   (interactive (list 'ut t))
13789   (let* ((header (or gnus-current-headers
13790                      (gnus-summary-article-header) ""))
13791          (date (and (vectorp header) (mail-header-date header)))
13792          (date-regexp "^Date: \\|^X-Sent: ")
13793          (now (current-time))
13794          (inhibit-point-motion-hooks t))
13795     (when (and date (not (string= date "")))
13796       (save-excursion
13797         (set-buffer gnus-article-buffer)
13798         (save-restriction
13799           (nnheader-narrow-to-headers)
13800           (let ((buffer-read-only nil))
13801             ;; Delete any old Date headers.
13802             (if (zerop (nnheader-remove-header date-regexp t))
13803                 (beginning-of-line)
13804               (goto-char (point-max)))
13805             (insert
13806              (cond
13807               ;; Convert to the local timezone.  We have to slap a
13808               ;; `condition-case' round the calls to the timezone
13809               ;; functions since they aren't particularly resistant to
13810               ;; buggy dates.
13811               ((eq type 'local)
13812                (concat "Date: " (condition-case ()
13813                                     (timezone-make-date-arpa-standard date)
13814                                   (error date))
13815                        "\n"))
13816               ;; Convert to Universal Time.
13817               ((eq type 'ut)
13818                (concat "Date: "
13819                        (condition-case ()
13820                            (timezone-make-date-arpa-standard date nil "UT")
13821                          (error date))
13822                        "\n"))
13823               ;; Get the original date from the article.
13824               ((eq type 'original)
13825                (concat "Date: " date "\n"))
13826               ;; Do an X-Sent lapsed format.
13827               ((eq type 'lapsed)
13828                ;; If the date is seriously mangled, the timezone
13829                ;; functions are liable to bug out, so we condition-case
13830                ;; the entire thing.
13831                (let* ((real-time
13832                        (condition-case ()
13833                            (gnus-time-minus
13834                             (gnus-encode-date
13835                              (timezone-make-date-arpa-standard
13836                               (current-time-string now)
13837                               (current-time-zone now) "UT"))
13838                             (gnus-encode-date
13839                              (timezone-make-date-arpa-standard
13840                               date nil "UT")))
13841                          (error '(0 0))))
13842                       (real-sec (+ (* (float (car real-time)) 65536)
13843                                    (cadr real-time)))
13844                       (sec (abs real-sec))
13845                       num prev)
13846                  (if (zerop sec)
13847                      "X-Sent: Now\n"
13848                    (concat
13849                     "X-Sent: "
13850                     ;; This is a bit convoluted, but basically we go
13851                     ;; through the time units for years, weeks, etc,
13852                     ;; and divide things to see whether that results
13853                     ;; in positive answers.
13854                     (mapconcat
13855                      (lambda (unit)
13856                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13857                            ;; The (remaining) seconds are too few to
13858                            ;; be divided into this time unit.
13859                            ""
13860                          ;; It's big enough, so we output it.
13861                          (setq sec (- sec (* num (cdr unit))))
13862                          (prog1
13863                              (concat (if prev ", " "") (int-to-string
13864                                                         (floor num))
13865                                      " " (symbol-name (car unit))
13866                                      (if (> num 1) "s" ""))
13867                            (setq prev t))))
13868                      gnus-article-time-units "")
13869                     ;; If dates are odd, then it might appear like the
13870                     ;; article was sent in the future.
13871                     (if (> real-sec 0)
13872                         " ago\n"
13873                       " in the future\n")))))
13874               (t
13875                (error "Unknown conversion type: %s" type)))))
13876           ;; Do highlighting.
13877           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13878             (gnus-article-highlight-headers)))))))
13879
13880 (defun gnus-article-date-local (&optional highlight)
13881   "Convert the current article date to the local timezone."
13882   (interactive (list t))
13883   (gnus-article-date-ut 'local highlight))
13884
13885 (defun gnus-article-date-original (&optional highlight)
13886   "Convert the current article date to what it was originally.
13887 This is only useful if you have used some other date conversion
13888 function and want to see what the date was before converting."
13889   (interactive (list t))
13890   (gnus-article-date-ut 'original highlight))
13891
13892 (defun gnus-article-date-lapsed (&optional highlight)
13893   "Convert the current article date to time lapsed since it was sent."
13894   (interactive (list t))
13895   (gnus-article-date-ut 'lapsed highlight))
13896
13897 (defun gnus-article-maybe-highlight ()
13898   "Do some article highlighting if `gnus-visual' is non-nil."
13899   (if (gnus-visual-p 'article-highlight 'highlight)
13900       (gnus-article-highlight-some)))
13901
13902 ;; Article savers.
13903
13904 (defun gnus-output-to-rmail (file-name)
13905   "Append the current article to an Rmail file named FILE-NAME."
13906   (require 'rmail)
13907   ;; Most of these codes are borrowed from rmailout.el.
13908   (setq file-name (expand-file-name file-name))
13909   (setq rmail-default-rmail-file file-name)
13910   (let ((artbuf (current-buffer))
13911         (tmpbuf (get-buffer-create " *Gnus-output*")))
13912     (save-excursion
13913       (or (get-file-buffer file-name)
13914           (file-exists-p file-name)
13915           (if (gnus-yes-or-no-p
13916                (concat "\"" file-name "\" does not exist, create it? "))
13917               (let ((file-buffer (create-file-buffer file-name)))
13918                 (save-excursion
13919                   (set-buffer file-buffer)
13920                   (rmail-insert-rmail-file-header)
13921                   (let ((require-final-newline nil))
13922                     (write-region (point-min) (point-max) file-name t 1)))
13923                 (kill-buffer file-buffer))
13924             (error "Output file does not exist")))
13925       (set-buffer tmpbuf)
13926       (buffer-disable-undo (current-buffer))
13927       (erase-buffer)
13928       (insert-buffer-substring artbuf)
13929       (gnus-convert-article-to-rmail)
13930       ;; Decide whether to append to a file or to an Emacs buffer.
13931       (let ((outbuf (get-file-buffer file-name)))
13932         (if (not outbuf)
13933             (append-to-file (point-min) (point-max) file-name)
13934           ;; File has been visited, in buffer OUTBUF.
13935           (set-buffer outbuf)
13936           (let ((buffer-read-only nil)
13937                 (msg (and (boundp 'rmail-current-message)
13938                           (symbol-value 'rmail-current-message))))
13939             ;; If MSG is non-nil, buffer is in RMAIL mode.
13940             (if msg
13941                 (progn (widen)
13942                        (narrow-to-region (point-max) (point-max))))
13943             (insert-buffer-substring tmpbuf)
13944             (if msg
13945                 (progn
13946                   (goto-char (point-min))
13947                   (widen)
13948                   (search-backward "\^_")
13949                   (narrow-to-region (point) (point-max))
13950                   (goto-char (1+ (point-min)))
13951                   (rmail-count-new-messages t)
13952                   (rmail-show-message msg)))))))
13953     (kill-buffer tmpbuf)))
13954
13955 (defun gnus-output-to-file (file-name)
13956   "Append the current article to a file named FILE-NAME."
13957   (setq file-name (expand-file-name file-name))
13958   (let ((artbuf (current-buffer))
13959         (tmpbuf (get-buffer-create " *Gnus-output*")))
13960     (save-excursion
13961       (set-buffer tmpbuf)
13962       (buffer-disable-undo (current-buffer))
13963       (erase-buffer)
13964       (insert-buffer-substring artbuf)
13965       ;; Append newline at end of the buffer as separator, and then
13966       ;; save it to file.
13967       (goto-char (point-max))
13968       (insert "\n")
13969       (append-to-file (point-min) (point-max) file-name))
13970     (kill-buffer tmpbuf)))
13971
13972 (defun gnus-convert-article-to-rmail ()
13973   "Convert article in current buffer to Rmail message format."
13974   (let ((buffer-read-only nil))
13975     ;; Convert article directly into Babyl format.
13976     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
13977     (goto-char (point-min))
13978     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
13979     (while (search-forward "\n\^_" nil t) ;single char
13980       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
13981     (goto-char (point-max))
13982     (insert "\^_")))
13983
13984 (defun gnus-narrow-to-page (&optional arg)
13985   "Narrow the article buffer to a page.
13986 If given a numerical ARG, move forward ARG pages."
13987   (interactive "P")
13988   (setq arg (if arg (prefix-numeric-value arg) 0))
13989   (save-excursion
13990     (set-buffer gnus-article-buffer)
13991     (goto-char (point-min))
13992     (widen)
13993     (when (gnus-visual-p 'page-marker)
13994       (let ((buffer-read-only nil))
13995         (gnus-remove-text-with-property 'gnus-prev)
13996         (gnus-remove-text-with-property 'gnus-next)))
13997     (when
13998         (cond ((< arg 0)
13999                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14000               ((> arg 0)
14001                (re-search-forward page-delimiter nil 'move arg)))
14002       (goto-char (match-end 0)))
14003     (narrow-to-region
14004      (point)
14005      (if (re-search-forward page-delimiter nil 'move)
14006          (match-beginning 0)
14007        (point)))
14008     (when (and (gnus-visual-p 'page-marker)
14009                (not (= (point-min) 1)))
14010       (save-excursion
14011         (goto-char (point-min))
14012         (gnus-insert-prev-page-button)))
14013     (when (and (gnus-visual-p 'page-marker)
14014                (not (= (1- (point-max)) (buffer-size))))
14015       (save-excursion
14016         (goto-char (point-max))
14017         (gnus-insert-next-page-button)))))
14018
14019 ;; Article mode commands
14020
14021 (defun gnus-article-goto-next-page ()
14022   "Show the next page of the article."
14023   (interactive)
14024   (when (gnus-article-next-page)
14025     (gnus-article-read-summary-keys nil ?n)))
14026
14027 (defun gnus-article-goto-prev-page ()
14028   "Show the next page of the article."
14029   (interactive)
14030   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14031     (gnus-article-prev-page nil)))
14032
14033 (defun gnus-article-next-page (&optional lines)
14034   "Show the next page of the current article.
14035 If end of article, return non-nil.  Otherwise return nil.
14036 Argument LINES specifies lines to be scrolled up."
14037   (interactive "p")
14038   (move-to-window-line -1)
14039   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14040   (if (save-excursion
14041         (end-of-line)
14042         (and (pos-visible-in-window-p)  ;Not continuation line.
14043              (eobp)))
14044       ;; Nothing in this page.
14045       (if (or (not gnus-break-pages)
14046               (save-excursion
14047                 (save-restriction
14048                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14049           t                             ;Nothing more.
14050         (gnus-narrow-to-page 1)         ;Go to next page.
14051         nil)
14052     ;; More in this page.
14053     (condition-case ()
14054         (scroll-up lines)
14055       (end-of-buffer
14056        ;; Long lines may cause an end-of-buffer error.
14057        (goto-char (point-max))))
14058     nil))
14059
14060 (defun gnus-article-prev-page (&optional lines)
14061   "Show previous page of current article.
14062 Argument LINES specifies lines to be scrolled down."
14063   (interactive "p")
14064   (move-to-window-line 0)
14065   (if (and gnus-break-pages
14066            (bobp)
14067            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14068       (progn
14069         (gnus-narrow-to-page -1)        ;Go to previous page.
14070         (goto-char (point-max))
14071         (recenter -1))
14072     (condition-case ()
14073         (scroll-down lines)
14074       (error nil))))
14075
14076 (defun gnus-article-refer-article ()
14077   "Read article specified by message-id around point."
14078   (interactive)
14079   (let ((point (point)))
14080     (search-forward ">" nil t)          ;Move point to end of "<....>".
14081     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14082         (let ((message-id (match-string 1)))
14083           (goto-char point)
14084           (set-buffer gnus-summary-buffer)
14085           (gnus-summary-refer-article message-id))
14086       (goto-char (point))
14087       (error "No references around point"))))
14088
14089 (defun gnus-article-show-summary ()
14090   "Reconfigure windows to show summary buffer."
14091   (interactive)
14092   (gnus-configure-windows 'article)
14093   (gnus-summary-goto-subject gnus-current-article))
14094
14095 (defun gnus-article-describe-briefly ()
14096   "Describe article mode commands briefly."
14097   (interactive)
14098   (gnus-message 6
14099                 (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")))
14100
14101 (defun gnus-article-summary-command ()
14102   "Execute the last keystroke in the summary buffer."
14103   (interactive)
14104   (let ((obuf (current-buffer))
14105         (owin (current-window-configuration))
14106         func)
14107     (switch-to-buffer gnus-summary-buffer 'norecord)
14108     (setq func (lookup-key (current-local-map) (this-command-keys)))
14109     (call-interactively func)
14110     (set-buffer obuf)
14111     (set-window-configuration owin)
14112     (set-window-point (get-buffer-window (current-buffer)) (point))))
14113
14114 (defun gnus-article-summary-command-nosave ()
14115   "Execute the last keystroke in the summary buffer."
14116   (interactive)
14117   (let (func)
14118     (pop-to-buffer gnus-summary-buffer 'norecord)
14119     (setq func (lookup-key (current-local-map) (this-command-keys)))
14120     (call-interactively func)))
14121
14122 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14123   "Read a summary buffer key sequence and execute it from the article buffer."
14124   (interactive "P")
14125   (let ((nosaves
14126          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14127            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14128            "=" "^" "\M-^"))
14129         keys)
14130     (save-excursion
14131       (set-buffer gnus-summary-buffer)
14132       (push (or key last-command-event) unread-command-events)
14133       (setq keys (read-key-sequence nil)))
14134     (message "")
14135
14136     (if (member keys nosaves)
14137         (let (func)
14138           (pop-to-buffer gnus-summary-buffer 'norecord)
14139           (if (setq func (lookup-key (current-local-map) keys))
14140               (call-interactively func)
14141             (ding)))
14142       (let ((obuf (current-buffer))
14143             (owin (current-window-configuration))
14144             (opoint (point))
14145             func in-buffer)
14146         (if not-restore-window
14147             (pop-to-buffer gnus-summary-buffer 'norecord)
14148           (switch-to-buffer gnus-summary-buffer 'norecord))
14149         (setq in-buffer (current-buffer))
14150         (if (setq func (lookup-key (current-local-map) keys))
14151             (call-interactively func)
14152           (ding))
14153         (when (eq in-buffer (current-buffer))
14154           (set-buffer obuf)
14155           (unless not-restore-window
14156             (set-window-configuration owin))
14157           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14158
14159 \f
14160 ;;;
14161 ;;; Kill file handling.
14162 ;;;
14163
14164 ;;;###autoload
14165 (defalias 'gnus-batch-kill 'gnus-batch-score)
14166 ;;;###autoload
14167 (defun gnus-batch-score ()
14168   "Run batched scoring.
14169 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14170 Newsgroups is a list of strings in Bnews format.  If you want to score
14171 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14172 score the alt hierarchy, you'd say \"!alt.all\"."
14173   (interactive)
14174   (let* ((yes-and-no
14175           (gnus-newsrc-parse-options
14176            (apply (function concat)
14177                   (mapcar (lambda (g) (concat g " "))
14178                           command-line-args-left))))
14179          (gnus-expert-user t)
14180          (nnmail-spool-file nil)
14181          (gnus-use-dribble-file nil)
14182          (yes (car yes-and-no))
14183          (no (cdr yes-and-no))
14184          group newsrc entry
14185          ;; Disable verbose message.
14186          gnus-novice-user gnus-large-newsgroup)
14187     ;; Eat all arguments.
14188     (setq command-line-args-left nil)
14189     ;; Start Gnus.
14190     (gnus)
14191     ;; Apply kills to specified newsgroups in command line arguments.
14192     (setq newsrc (cdr gnus-newsrc-alist))
14193     (while newsrc
14194       (setq group (car (car newsrc)))
14195       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14196       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14197                (and (car entry)
14198                     (or (eq (car entry) t)
14199                         (not (zerop (car entry)))))
14200                (if yes (string-match yes group) t)
14201                (or (null no) (not (string-match no group))))
14202           (progn
14203             (gnus-summary-read-group group nil t nil t)
14204             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14205                  (gnus-summary-exit))))
14206       (setq newsrc (cdr newsrc)))
14207     ;; Exit Emacs.
14208     (switch-to-buffer gnus-group-buffer)
14209     (gnus-group-save-newsrc)))
14210
14211 (defun gnus-apply-kill-file ()
14212   "Apply a kill file to the current newsgroup.
14213 Returns the number of articles marked as read."
14214   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14215           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14216       (gnus-apply-kill-file-internal)
14217     0))
14218
14219 (defun gnus-kill-save-kill-buffer ()
14220   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14221     (when (get-file-buffer file)
14222       (save-excursion
14223         (set-buffer (get-file-buffer file))
14224         (and (buffer-modified-p) (save-buffer))
14225         (kill-buffer (current-buffer))))))
14226
14227 (defvar gnus-kill-file-name "KILL"
14228   "Suffix of the kill files.")
14229
14230 (defun gnus-newsgroup-kill-file (newsgroup)
14231   "Return the name of a kill file name for NEWSGROUP.
14232 If NEWSGROUP is nil, return the global kill file name instead."
14233   (cond 
14234    ;; The global KILL file is placed at top of the directory.
14235    ((or (null newsgroup)
14236         (string-equal newsgroup ""))
14237     (expand-file-name gnus-kill-file-name
14238                       (or gnus-kill-files-directory "~/News")))
14239    ;; Append ".KILL" to newsgroup name.
14240    ((gnus-use-long-file-name 'not-kill)
14241     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14242                               "." gnus-kill-file-name)
14243                       (or gnus-kill-files-directory "~/News")))
14244    ;; Place "KILL" under the hierarchical directory.
14245    (t
14246     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14247                               "/" gnus-kill-file-name)
14248                       (or gnus-kill-files-directory "~/News")))))
14249
14250 \f
14251 ;;;
14252 ;;; Dribble file
14253 ;;;
14254
14255 (defvar gnus-dribble-ignore nil)
14256 (defvar gnus-dribble-eval-file nil)
14257
14258 (defun gnus-dribble-file-name ()
14259   "Return the dribble file for the current .newsrc."
14260   (concat
14261    (if gnus-dribble-directory
14262        (concat (file-name-as-directory gnus-dribble-directory)
14263                (file-name-nondirectory gnus-current-startup-file))
14264      gnus-current-startup-file)
14265    "-dribble"))
14266
14267 (defun gnus-dribble-enter (string)
14268   "Enter STRING into the dribble buffer."
14269   (if (and (not gnus-dribble-ignore)
14270            gnus-dribble-buffer
14271            (buffer-name gnus-dribble-buffer))
14272       (let ((obuf (current-buffer)))
14273         (set-buffer gnus-dribble-buffer)
14274         (insert string "\n")
14275         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14276         (set-buffer obuf))))
14277
14278 (defun gnus-dribble-read-file ()
14279   "Read the dribble file from disk."
14280   (let ((dribble-file (gnus-dribble-file-name)))
14281     (save-excursion
14282       (set-buffer (setq gnus-dribble-buffer
14283                         (get-buffer-create
14284                          (file-name-nondirectory dribble-file))))
14285       (gnus-add-current-to-buffer-list)
14286       (erase-buffer)
14287       (setq buffer-file-name dribble-file)
14288       (auto-save-mode t)
14289       (buffer-disable-undo (current-buffer))
14290       (bury-buffer (current-buffer))
14291       (set-buffer-modified-p nil)
14292       (let ((auto (make-auto-save-file-name))
14293             (gnus-dribble-ignore t))
14294         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14295           ;; Load whichever file is newest -- the auto save file
14296           ;; or the "real" file.
14297           (if (file-newer-than-file-p auto dribble-file)
14298               (insert-file-contents auto)
14299             (insert-file-contents dribble-file))
14300           (unless (zerop (buffer-size))
14301             (set-buffer-modified-p t))
14302           ;; Set the file modes to reflect the .newsrc file modes.
14303           (save-buffer)
14304           (when (file-exists-p gnus-current-startup-file)
14305             (set-file-modes dribble-file
14306                             (file-modes gnus-current-startup-file)))
14307           ;; Possibly eval the file later.
14308           (when (gnus-y-or-n-p
14309                  "Auto-save file exists.  Do you want to read it? ")
14310             (setq gnus-dribble-eval-file t)))))))
14311
14312 (defun gnus-dribble-eval-file ()
14313   (if (not gnus-dribble-eval-file)
14314       ()
14315     (setq gnus-dribble-eval-file nil)
14316     (save-excursion
14317       (let ((gnus-dribble-ignore t))
14318         (set-buffer gnus-dribble-buffer)
14319         (eval-buffer (current-buffer))))))
14320
14321 (defun gnus-dribble-delete-file ()
14322   (if (file-exists-p (gnus-dribble-file-name))
14323       (delete-file (gnus-dribble-file-name)))
14324   (if gnus-dribble-buffer
14325       (save-excursion
14326         (set-buffer gnus-dribble-buffer)
14327         (let ((auto (make-auto-save-file-name)))
14328           (if (file-exists-p auto)
14329               (delete-file auto))
14330           (erase-buffer)
14331           (set-buffer-modified-p nil)))))
14332
14333 (defun gnus-dribble-save ()
14334   (if (and gnus-dribble-buffer
14335            (buffer-name gnus-dribble-buffer))
14336       (save-excursion
14337         (set-buffer gnus-dribble-buffer)
14338         (save-buffer))))
14339
14340 (defun gnus-dribble-clear ()
14341   (save-excursion
14342     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14343         (progn
14344           (set-buffer gnus-dribble-buffer)
14345           (erase-buffer)
14346           (set-buffer-modified-p nil)
14347           (setq buffer-saved-size (buffer-size))))))
14348
14349 \f
14350 ;;;
14351 ;;; Server Communication
14352 ;;;
14353
14354 (defun gnus-start-news-server (&optional confirm)
14355   "Open a method for getting news.
14356 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14357   (let (how)
14358     (if gnus-current-select-method
14359         ;; Stream is already opened.
14360         nil
14361       ;; Open NNTP server.
14362       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14363       (if confirm
14364           (progn
14365             ;; Read server name with completion.
14366             (setq gnus-nntp-server
14367                   (completing-read "NNTP server: "
14368                                    (mapcar (lambda (server) (list server))
14369                                            (cons (list gnus-nntp-server)
14370                                                  gnus-secondary-servers))
14371                                    nil nil gnus-nntp-server))))
14372
14373       (if (and gnus-nntp-server
14374                (stringp gnus-nntp-server)
14375                (not (string= gnus-nntp-server "")))
14376           (setq gnus-select-method
14377                 (cond ((or (string= gnus-nntp-server "")
14378                            (string= gnus-nntp-server "::"))
14379                        (list 'nnspool (system-name)))
14380                       ((string-match "^:" gnus-nntp-server)
14381                        (list 'nnmh gnus-nntp-server
14382                              (list 'nnmh-directory
14383                                    (file-name-as-directory
14384                                     (expand-file-name
14385                                      (concat "~/" (substring
14386                                                    gnus-nntp-server 1)))))
14387                              (list 'nnmh-get-new-mail nil)))
14388                       (t
14389                        (list 'nntp gnus-nntp-server)))))
14390
14391       (setq how (car gnus-select-method))
14392       (cond ((eq how 'nnspool)
14393              (require 'nnspool)
14394              (gnus-message 5 "Looking up local news spool..."))
14395             ((eq how 'nnmh)
14396              (require 'nnmh)
14397              (gnus-message 5 "Looking up mh spool..."))
14398             (t
14399              (require 'nntp)))
14400       (setq gnus-current-select-method gnus-select-method)
14401       (run-hooks 'gnus-open-server-hook)
14402       (or
14403        ;; gnus-open-server-hook might have opened it
14404        (gnus-server-opened gnus-select-method)
14405        (gnus-open-server gnus-select-method)
14406        (gnus-y-or-n-p
14407         (format
14408          "%s (%s) open error: '%s'.     Continue? "
14409          (car gnus-select-method) (cadr gnus-select-method)
14410          (gnus-status-message gnus-select-method)))
14411        (progn
14412          (gnus-message 1 "Couldn't open server on %s"
14413                        (nth 1 gnus-select-method))
14414          (ding)
14415          nil)))))
14416
14417 (defun gnus-check-group (group)
14418   "Try to make sure that the server where GROUP exists is alive."
14419   (let ((method (gnus-find-method-for-group group)))
14420     (or (gnus-server-opened method)
14421         (gnus-open-server method))))
14422
14423 (defun gnus-check-server (&optional method)
14424   "Check whether the connection to METHOD is down.
14425 If METHOD is nil, use `gnus-select-method'.
14426 If it is down, start it up (again)."
14427   (let ((method (or method gnus-select-method)))
14428     ;; Transform virtual server names into select methods.
14429     (when (stringp method)
14430       (setq method (gnus-server-to-method method)))
14431     (if (gnus-server-opened method)
14432         ;; The stream is already opened.
14433         t
14434       ;; Open the server.
14435       (gnus-message 5 "Opening %s server%s..." (car method)
14436                     (if (equal (nth 1 method) "") ""
14437                       (format " on %s" (nth 1 method))))
14438       (run-hooks 'gnus-open-server-hook)
14439       (prog1
14440           (gnus-open-server method)
14441         (message "")))))
14442
14443 (defun gnus-get-function (method function)
14444   "Return a function symbol based on METHOD and FUNCTION."
14445   ;; Translate server names into methods.
14446   (unless method
14447     (error "Attempted use of a nil select method"))
14448   (when (stringp method)
14449     (setq method (gnus-server-to-method method)))
14450   (let ((func (intern (format "%s-%s" (car method) function))))
14451     ;; If the functions isn't bound, we require the backend in
14452     ;; question.
14453     (unless (fboundp func)
14454       (require (car method))
14455       (unless (fboundp func)
14456         ;; This backend doesn't implement this function.
14457         (error "No such function: %s" func)))
14458     func))
14459
14460 \f
14461 ;;;
14462 ;;; Interface functions to the backends.
14463 ;;;
14464
14465 (defun gnus-open-server (method)
14466   "Open a connection to METHOD."
14467   (let ((elem (assoc method gnus-opened-servers)))
14468     ;; If this method was previously denied, we just return nil.
14469     (if (eq (nth 1 elem) 'denied)
14470         (progn
14471           (gnus-message 1 "Denied server")
14472           nil)
14473       ;; Open the server.
14474       (let ((result
14475              (funcall (gnus-get-function method 'open-server)
14476                       (nth 1 method) (nthcdr 2 method))))
14477         ;; If this hasn't been opened before, we add it to the list.
14478         (unless elem
14479           (setq elem (list method nil)
14480                 gnus-opened-servers (cons elem gnus-opened-servers)))
14481         ;; Set the status of this server.
14482         (setcar (cdr elem) (if result 'ok 'denied))
14483         ;; Return the result from the "open" call.
14484         result))))
14485
14486 (defun gnus-close-server (method)
14487   "Close the connection to METHOD."
14488   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14489
14490 (defun gnus-request-list (method)
14491   "Request the active file from METHOD."
14492   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14493
14494 (defun gnus-request-list-newsgroups (method)
14495   "Request the newsgroups file from METHOD."
14496   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14497
14498 (defun gnus-request-newgroups (date method)
14499   "Request all new groups since DATE from METHOD."
14500   (funcall (gnus-get-function method 'request-newgroups)
14501            date (nth 1 method)))
14502
14503 (defun gnus-server-opened (method)
14504   "Check whether a connection to METHOD has been opened."
14505   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14506
14507 (defun gnus-status-message (method)
14508   "Return the status message from METHOD.
14509 If METHOD is a string, it is interpreted as a group name.   The method
14510 this group uses will be queried."
14511   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14512                   method)))
14513     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14514
14515 (defun gnus-request-group (group &optional dont-check method)
14516   "Request GROUP.  If DONT-CHECK, no information is required."
14517   (let ((method (or method (gnus-find-method-for-group group))))
14518     (funcall (gnus-get-function method 'request-group)
14519              (gnus-group-real-name group) (nth 1 method) dont-check)))
14520
14521 (defun gnus-request-asynchronous (group &optional articles)
14522   "Request that GROUP behave asynchronously.
14523 ARTICLES is the `data' of the group."
14524   (let ((method (gnus-find-method-for-group group)))
14525     (funcall (gnus-get-function method 'request-asynchronous)
14526              (gnus-group-real-name group) (nth 1 method) articles)))
14527
14528 (defun gnus-list-active-group (group)
14529   "Request active information on GROUP."
14530   (let ((method (gnus-find-method-for-group group))
14531         (func 'list-active-group))
14532     (when (gnus-check-backend-function func group)
14533       (funcall (gnus-get-function method func)
14534                (gnus-group-real-name group) (nth 1 method)))))
14535
14536 (defun gnus-request-group-description (group)
14537   "Request a description of GROUP."
14538   (let ((method (gnus-find-method-for-group group))
14539         (func 'request-group-description))
14540     (when (gnus-check-backend-function func group)
14541       (funcall (gnus-get-function method func)
14542                (gnus-group-real-name group) (nth 1 method)))))
14543
14544 (defun gnus-close-group (group)
14545   "Request the GROUP be closed."
14546   (let ((method (gnus-find-method-for-group group)))
14547     (funcall (gnus-get-function method 'close-group)
14548              (gnus-group-real-name group) (nth 1 method))))
14549
14550 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14551   "Request headers for ARTICLES in GROUP.
14552 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14553   (let ((method (gnus-find-method-for-group group)))
14554     (if (and gnus-use-cache (numberp (car articles)))
14555         (gnus-cache-retrieve-headers articles group fetch-old)
14556       (funcall (gnus-get-function method 'retrieve-headers)
14557                articles (gnus-group-real-name group) (nth 1 method)
14558                fetch-old))))
14559
14560 (defun gnus-retrieve-groups (groups method)
14561   "Request active information on GROUPS from METHOD."
14562   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14563
14564 (defun gnus-request-type (group &optional article)
14565   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14566   (let ((method (gnus-find-method-for-group group)))
14567     (if (not (gnus-check-backend-function 'request-type (car method)))
14568         'unknown
14569       (funcall (gnus-get-function method 'request-type)
14570                (gnus-group-real-name group) article))))
14571
14572 (defun gnus-request-update-mark (group article mark)
14573   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14574   (let ((method (gnus-find-method-for-group group)))
14575     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14576         mark
14577       (funcall (gnus-get-function method 'request-update-mark)
14578                (gnus-group-real-name group) article mark))))
14579
14580 (defun gnus-request-article (article group &optional buffer)
14581   "Request the ARTICLE in GROUP.
14582 ARTICLE can either be an article number or an article Message-ID.
14583 If BUFFER, insert the article in that group."
14584   (let ((method (gnus-find-method-for-group group)))
14585     (funcall (gnus-get-function method 'request-article)
14586              article (gnus-group-real-name group) (nth 1 method) buffer)))
14587
14588 (defun gnus-request-head (article group)
14589   "Request the head of ARTICLE in GROUP."
14590   (let ((method (gnus-find-method-for-group group)))
14591     (funcall (gnus-get-function method 'request-head)
14592              article (gnus-group-real-name group) (nth 1 method))))
14593
14594 (defun gnus-request-body (article group)
14595   "Request the body of ARTICLE in GROUP."
14596   (let ((method (gnus-find-method-for-group group)))
14597     (funcall (gnus-get-function method 'request-body)
14598              article (gnus-group-real-name group) (nth 1 method))))
14599
14600 (defun gnus-request-post (method)
14601   "Post the current buffer using METHOD."
14602   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14603
14604 (defun gnus-request-scan (group method)
14605   "Request a SCAN being performed in GROUP from METHOD.
14606 If GROUP is nil, all groups on METHOD are scanned."
14607   (let ((method (if group (gnus-find-method-for-group group) method)))
14608     (funcall (gnus-get-function method 'request-scan)
14609              (and group (gnus-group-real-name group)) (nth 1 method))))
14610
14611 (defsubst gnus-request-update-info (info method)
14612   "Request that METHOD update INFO."
14613   (when (gnus-check-backend-function 'request-update-info (car method))
14614     (funcall (gnus-get-function method 'request-update-info)
14615              (gnus-group-real-name (gnus-info-group info))
14616              info (nth 1 method))))
14617
14618 (defun gnus-request-expire-articles (articles group &optional force)
14619   (let ((method (gnus-find-method-for-group group)))
14620     (funcall (gnus-get-function method 'request-expire-articles)
14621              articles (gnus-group-real-name group) (nth 1 method)
14622              force)))
14623
14624 (defun gnus-request-move-article
14625   (article group server accept-function &optional last)
14626   (let ((method (gnus-find-method-for-group group)))
14627     (funcall (gnus-get-function method 'request-move-article)
14628              article (gnus-group-real-name group)
14629              (nth 1 method) accept-function last)))
14630
14631 (defun gnus-request-accept-article (group &optional last method)
14632   ;; Make sure there's a newline at the end of the article.
14633   (goto-char (point-max))
14634   (unless (bolp)
14635     (insert "\n"))
14636   (let ((func (if (symbolp group) group
14637                 (car (or method (gnus-find-method-for-group group))))))
14638     (funcall (intern (format "%s-request-accept-article" func))
14639              (if (stringp group) (gnus-group-real-name group) group)
14640              last)))
14641
14642 (defun gnus-request-replace-article (article group buffer)
14643   (let ((func (car (gnus-find-method-for-group group))))
14644     (funcall (intern (format "%s-request-replace-article" func))
14645              article (gnus-group-real-name group) buffer)))
14646
14647 (defun gnus-request-associate-buffer (group)
14648   (let ((method (gnus-find-method-for-group group)))
14649     (funcall (gnus-get-function method 'request-associate-buffer)
14650              (gnus-group-real-name group))))
14651
14652 (defun gnus-request-restore-buffer (article group)
14653   "Request a new buffer restored to the state of ARTICLE."
14654   (let ((method (gnus-find-method-for-group group)))
14655     (funcall (gnus-get-function method 'request-restore-buffer)
14656              article (gnus-group-real-name group) (nth 1 method))))
14657
14658 (defun gnus-request-create-group (group &optional method)
14659   (let ((method (or method (gnus-find-method-for-group group))))
14660     (funcall (gnus-get-function method 'request-create-group)
14661              (gnus-group-real-name group) (nth 1 method))))
14662
14663 (defun gnus-request-delete-group (group &optional force)
14664   (let ((method (gnus-find-method-for-group group)))
14665     (funcall (gnus-get-function method 'request-delete-group)
14666              (gnus-group-real-name group) force (nth 1 method))))
14667
14668 (defun gnus-request-rename-group (group new-name)
14669   (let ((method (gnus-find-method-for-group group)))
14670     (funcall (gnus-get-function method 'request-rename-group)
14671              (gnus-group-real-name group)
14672              (gnus-group-real-name new-name) (nth 1 method))))
14673
14674 (defun gnus-member-of-valid (symbol group)
14675   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14676   (memq symbol (assoc
14677                 (symbol-name (car (gnus-find-method-for-group group)))
14678                 gnus-valid-select-methods)))
14679
14680 (defun gnus-method-option-p (method option)
14681   "Return non-nil if select METHOD has OPTION as a parameter."
14682   (memq option (assoc (format "%s" (car method))
14683                       gnus-valid-select-methods)))
14684
14685 (defun gnus-server-extend-method (group method)
14686   ;; This function "extends" a virtual server.  If the server is
14687   ;; "hello", and the select method is ("hello" (my-var "something"))
14688   ;; in the group "alt.alt", this will result in a new virtual server
14689   ;; called "hello+alt.alt".
14690   (let ((entry
14691          (gnus-copy-sequence
14692           (if (equal (car method) "native") gnus-select-method
14693             (cdr (assoc (car method) gnus-server-alist))))))
14694     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14695     (nconc entry (cdr method))))
14696
14697 (defun gnus-find-method-for-group (group &optional info)
14698   "Find the select method that GROUP uses."
14699   (or gnus-override-method
14700       (and (not group)
14701            gnus-select-method)
14702       (let ((info (or info (gnus-get-info group)))
14703             method)
14704         (if (or (not info)
14705                 (not (setq method (gnus-info-method info))))
14706             (setq method gnus-select-method)
14707           (setq method
14708                 (cond ((stringp method)
14709                        (gnus-server-to-method method))
14710                       ((stringp (car method))
14711                        (gnus-server-extend-method group method))
14712                       (t
14713                        method))))
14714         (gnus-server-add-address method))))
14715
14716 (defun gnus-check-backend-function (func group)
14717   "Check whether GROUP supports function FUNC."
14718   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14719                   group)))
14720     (fboundp (intern (format "%s-%s" method func)))))
14721
14722 (defun gnus-methods-using (feature)
14723   "Find all methods that have FEATURE."
14724   (let ((valids gnus-valid-select-methods)
14725         outs)
14726     (while valids
14727       (if (memq feature (car valids))
14728           (setq outs (cons (car valids) outs)))
14729       (setq valids (cdr valids)))
14730     outs))
14731
14732 \f
14733 ;;;
14734 ;;; Active & Newsrc File Handling
14735 ;;;
14736
14737 (defun gnus-setup-news (&optional rawfile level)
14738   "Setup news information.
14739 If RAWFILE is non-nil, the .newsrc file will also be read.
14740 If LEVEL is non-nil, the news will be set up at level LEVEL."
14741   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14742     ;; Clear some variables to re-initialize news information.
14743     (if init (setq gnus-newsrc-alist nil
14744                    gnus-active-hashtb nil))
14745
14746     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14747     (if init (gnus-read-newsrc-file rawfile))
14748
14749     (unless (assoc "archive" gnus-server-alist)
14750       (push (cons "archive" gnus-message-archive-method)
14751             gnus-server-alist))
14752
14753     ;; If we don't read the complete active file, we fill in the
14754     ;; hashtb here.
14755     (if (or (null gnus-read-active-file)
14756             (eq gnus-read-active-file 'some))
14757         (gnus-update-active-hashtb-from-killed))
14758
14759     ;; Read the active file and create `gnus-active-hashtb'.
14760     ;; If `gnus-read-active-file' is nil, then we just create an empty
14761     ;; hash table.  The partial filling out of the hash table will be
14762     ;; done in `gnus-get-unread-articles'.
14763     (and gnus-read-active-file
14764          (not level)
14765          (gnus-read-active-file))
14766
14767     (or gnus-active-hashtb
14768         (setq gnus-active-hashtb (make-vector 4095 0)))
14769
14770     ;; Initialize the cache.
14771     (when gnus-use-cache
14772       (gnus-cache-open))
14773
14774     ;; Possibly eval the dribble file.
14775     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14776
14777     (gnus-update-format-specifications)
14778
14779     ;; Find new newsgroups and treat them.
14780     (if (and init gnus-check-new-newsgroups (not level)
14781              (gnus-check-server gnus-select-method))
14782         (gnus-find-new-newsgroups))
14783
14784     ;; Find the number of unread articles in each non-dead group.
14785     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14786       (gnus-get-unread-articles level))
14787
14788     (if (and init gnus-check-bogus-newsgroups
14789              gnus-read-active-file (not level)
14790              (gnus-server-opened gnus-select-method))
14791         (gnus-check-bogus-newsgroups))))
14792
14793 (defun gnus-find-new-newsgroups (&optional arg)
14794   "Search for new newsgroups and add them.
14795 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14796 The `-n' option line from .newsrc is respected.
14797 If ARG (the prefix), use the `ask-server' method to query
14798 the server for new groups."
14799   (interactive "P")
14800   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14801                        (null gnus-read-active-file)
14802                        (eq gnus-read-active-file 'some))
14803                    'ask-server gnus-check-new-newsgroups)))
14804     (unless (gnus-check-first-time-used)
14805       (if (or (consp check)
14806               (eq check 'ask-server))
14807           ;; Ask the server for new groups.
14808           (gnus-ask-server-for-new-groups)
14809         ;; Go through the active hashtb and look for new groups.
14810         (let ((groups 0)
14811               group new-newsgroups)
14812           (gnus-message 5 "Looking for new newsgroups...")
14813           (unless gnus-have-read-active-file
14814             (gnus-read-active-file))
14815           (setq gnus-newsrc-last-checked-date (current-time-string))
14816           (unless gnus-killed-hashtb
14817             (gnus-make-hashtable-from-killed))
14818           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14819           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14820           (mapatoms
14821            (lambda (sym)
14822              (if (or (null (setq group (symbol-name sym)))
14823                      (not (boundp sym))
14824                      (null (symbol-value sym))
14825                      (gnus-gethash group gnus-killed-hashtb)
14826                      (gnus-gethash group gnus-newsrc-hashtb))
14827                  ()
14828                (let ((do-sub (gnus-matches-options-n group)))
14829                  (cond
14830                   ((eq do-sub 'subscribe)
14831                    (setq groups (1+ groups))
14832                    (gnus-sethash group group gnus-killed-hashtb)
14833                    (funcall gnus-subscribe-options-newsgroup-method group))
14834                   ((eq do-sub 'ignore)
14835                    nil)
14836                   (t
14837                    (setq groups (1+ groups))
14838                    (gnus-sethash group group gnus-killed-hashtb)
14839                    (if gnus-subscribe-hierarchical-interactive
14840                        (setq new-newsgroups (cons group new-newsgroups))
14841                      (funcall gnus-subscribe-newsgroup-method group)))))))
14842            gnus-active-hashtb)
14843           (when new-newsgroups
14844             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14845           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14846           (if (> groups 0)
14847               (gnus-message 6 "%d new newsgroup%s arrived."
14848                             groups (if (> groups 1) "s have" " has"))
14849             (gnus-message 6 "No new newsgroups.")))))))
14850
14851 (defun gnus-matches-options-n (group)
14852   ;; Returns `subscribe' if the group is to be unconditionally
14853   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14854   ;; no match for the group.
14855
14856   ;; First we check the two user variables.
14857   (cond
14858    ((and gnus-options-subscribe
14859          (string-match gnus-options-subscribe group))
14860     'subscribe)
14861    ((and gnus-auto-subscribed-groups
14862          (string-match gnus-auto-subscribed-groups group))
14863     'subscribe)
14864    ((and gnus-options-not-subscribe
14865          (string-match gnus-options-not-subscribe group))
14866     'ignore)
14867    ;; Then we go through the list that was retrieved from the .newsrc
14868    ;; file.  This list has elements on the form
14869    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14870    ;; is in the reverse order of the options line) is returned.
14871    (t
14872     (let ((regs gnus-newsrc-options-n))
14873       (while (and regs
14874                   (not (string-match (car (car regs)) group)))
14875         (setq regs (cdr regs)))
14876       (and regs (cdr (car regs)))))))
14877
14878 (defun gnus-ask-server-for-new-groups ()
14879   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14880          (methods (cons gnus-select-method
14881                         (cons
14882                          "archive"
14883                          (append
14884                           (and (consp gnus-check-new-newsgroups)
14885                                gnus-check-new-newsgroups)
14886                           gnus-secondary-select-methods))))
14887          (groups 0)
14888          (new-date (current-time-string))
14889          group new-newsgroups got-new method hashtb
14890          gnus-override-subscribe-method)
14891     ;; Go through both primary and secondary select methods and
14892     ;; request new newsgroups.
14893     (while (setq method (gnus-server-get-method nil (pop methods)))
14894       (setq gnus-override-subscribe-method method)
14895       (when (and (gnus-check-server method)
14896                  (gnus-request-newgroups date method))
14897         (save-excursion
14898           (setq got-new t)
14899           (setq hashtb (gnus-make-hashtable 100))
14900           (set-buffer nntp-server-buffer)
14901           ;; Enter all the new groups into a hashtable.
14902           (gnus-active-to-gnus-format method hashtb 'ignore)))
14903       ;; Now all new groups from `method' are in `hashtb'.
14904       (mapatoms
14905        (lambda (group-sym)
14906          (if (or (null (setq group (symbol-name group-sym)))
14907                  (null (symbol-value group-sym))
14908                  (gnus-gethash group gnus-newsrc-hashtb)
14909                  (member group gnus-zombie-list)
14910                  (member group gnus-killed-list))
14911              ;; The group is already known.
14912              ()
14913            ;; Make this group active.
14914            (when (symbol-value group-sym)
14915              (gnus-set-active group (symbol-value group-sym)))
14916            ;; Check whether we want it or not.
14917            (let ((do-sub (gnus-matches-options-n group)))
14918              (cond
14919               ((eq do-sub 'subscribe)
14920                (incf groups)
14921                (gnus-sethash group group gnus-killed-hashtb)
14922                (funcall gnus-subscribe-options-newsgroup-method group))
14923               ((eq do-sub 'ignore)
14924                nil)
14925               (t
14926                (incf groups)
14927                (gnus-sethash group group gnus-killed-hashtb)
14928                (if gnus-subscribe-hierarchical-interactive
14929                    (push group new-newsgroups)
14930                  (funcall gnus-subscribe-newsgroup-method group)))))))
14931        hashtb)
14932       (when new-newsgroups
14933         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
14934     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14935     (when (> groups 0)
14936       (gnus-message 6 "%d new newsgroup%s arrived."
14937                     groups (if (> groups 1) "s have" " has")))
14938     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14939     got-new))
14940
14941 (defun gnus-check-first-time-used ()
14942   (if (or (> (length gnus-newsrc-alist) 1)
14943           (file-exists-p gnus-startup-file)
14944           (file-exists-p (concat gnus-startup-file ".el"))
14945           (file-exists-p (concat gnus-startup-file ".eld")))
14946       nil
14947     (gnus-message 6 "First time user; subscribing you to default groups")
14948     (unless gnus-have-read-active-file
14949       (gnus-read-active-file))
14950     (setq gnus-newsrc-last-checked-date (current-time-string))
14951     (let ((groups gnus-default-subscribed-newsgroups)
14952           group)
14953       (if (eq groups t)
14954           nil
14955         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
14956         (mapatoms
14957          (lambda (sym)
14958            (if (null (setq group (symbol-name sym)))
14959                ()
14960              (let ((do-sub (gnus-matches-options-n group)))
14961                (cond
14962                 ((eq do-sub 'subscribe)
14963                  (gnus-sethash group group gnus-killed-hashtb)
14964                  (funcall gnus-subscribe-options-newsgroup-method group))
14965                 ((eq do-sub 'ignore)
14966                  nil)
14967                 (t
14968                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
14969          gnus-active-hashtb)
14970         (while groups
14971           (if (gnus-active (car groups))
14972               (gnus-group-change-level
14973                (car groups) gnus-level-default-subscribed gnus-level-killed))
14974           (setq groups (cdr groups)))
14975         (gnus-group-make-help-group)
14976         (and gnus-novice-user
14977              (gnus-message 7 "`A k' to list killed groups"))))))
14978
14979 (defun gnus-subscribe-group (group previous &optional method)
14980   (gnus-group-change-level
14981    (if method
14982        (list t group gnus-level-default-subscribed nil nil method)
14983      group)
14984    gnus-level-default-subscribed gnus-level-killed previous t))
14985
14986 ;; `gnus-group-change-level' is the fundamental function for changing
14987 ;; subscription levels of newsgroups.  This might mean just changing
14988 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
14989 ;; again, which subscribes/unsubscribes a group, which is equally
14990 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
14991 ;; from 8-9 to 1-7 means that you remove the group from the list of
14992 ;; killed (or zombie) groups and add them to the (kinda) subscribed
14993 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
14994 ;; which is trivial.
14995 ;; ENTRY can either be a string (newsgroup name) or a list (if
14996 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
14997 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
14998 ;; entries.
14999 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15000 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15001 ;; after.
15002 (defun gnus-group-change-level (entry level &optional oldlevel
15003                                       previous fromkilled)
15004   (let (group info active num)
15005     ;; Glean what info we can from the arguments
15006     (if (consp entry)
15007         (if fromkilled (setq group (nth 1 entry))
15008           (setq group (car (nth 2 entry))))
15009       (setq group entry))
15010     (if (and (stringp entry)
15011              oldlevel
15012              (< oldlevel gnus-level-zombie))
15013         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15014     (if (and (not oldlevel)
15015              (consp entry))
15016         (setq oldlevel (car (cdr (nth 2 entry)))))
15017     (if (stringp previous)
15018         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15019
15020     (if (and (>= oldlevel gnus-level-zombie)
15021              (gnus-gethash group gnus-newsrc-hashtb))
15022         ;; We are trying to subscribe a group that is already
15023         ;; subscribed.
15024         ()                              ; Do nothing.
15025
15026       (or (gnus-ephemeral-group-p group)
15027           (gnus-dribble-enter
15028            (format "(gnus-group-change-level %S %S %S %S %S)"
15029                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15030
15031       ;; Then we remove the newgroup from any old structures, if needed.
15032       ;; If the group was killed, we remove it from the killed or zombie
15033       ;; list.  If not, and it is in fact going to be killed, we remove
15034       ;; it from the newsrc hash table and assoc.
15035       (cond
15036        ((>= oldlevel gnus-level-zombie)
15037         (if (= oldlevel gnus-level-zombie)
15038             (setq gnus-zombie-list (delete group gnus-zombie-list))
15039           (setq gnus-killed-list (delete group gnus-killed-list))))
15040        (t
15041         (if (and (>= level gnus-level-zombie)
15042                  entry)
15043             (progn
15044               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15045               (if (nth 3 entry)
15046                   (setcdr (gnus-gethash (car (nth 3 entry))
15047                                         gnus-newsrc-hashtb)
15048                           (cdr entry)))
15049               (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
15050
15051       ;; Finally we enter (if needed) the list where it is supposed to
15052       ;; go, and change the subscription level.  If it is to be killed,
15053       ;; we enter it into the killed or zombie list.
15054       (cond 
15055        ((>= level gnus-level-zombie)
15056         ;; Remove from the hash table.
15057         (gnus-sethash group nil gnus-newsrc-hashtb)
15058         ;; We do not enter foreign groups into the list of dead
15059         ;; groups.
15060         (unless (gnus-group-foreign-p group)
15061           (if (= level gnus-level-zombie)
15062               (setq gnus-zombie-list (cons group gnus-zombie-list))
15063             (setq gnus-killed-list (cons group gnus-killed-list)))))
15064        (t
15065         ;; If the list is to be entered into the newsrc assoc, and
15066         ;; it was killed, we have to create an entry in the newsrc
15067         ;; hashtb format and fix the pointers in the newsrc assoc.
15068         (if (< oldlevel gnus-level-zombie)
15069             ;; It was alive, and it is going to stay alive, so we
15070             ;; just change the level and don't change any pointers or
15071             ;; hash table entries.
15072             (setcar (cdr (car (cdr (cdr entry)))) level)
15073           (if (listp entry)
15074               (setq info (cdr entry)
15075                     num (car entry))
15076             (setq active (gnus-active group))
15077             (setq num
15078                   (if active (- (1+ (cdr active)) (car active)) t))
15079             ;; Check whether the group is foreign.  If so, the
15080             ;; foreign select method has to be entered into the
15081             ;; info.
15082             (let ((method (or gnus-override-subscribe-method
15083                               (gnus-group-method group))))
15084               (if (eq method gnus-select-method)
15085                   (setq info (list group level nil))
15086                 (setq info (list group level nil nil method)))))
15087           (unless previous
15088             (setq previous
15089                   (let ((p gnus-newsrc-alist))
15090                     (while (cdr (cdr p))
15091                       (setq p (cdr p)))
15092                     p)))
15093           (setq entry (cons info (cdr (cdr previous))))
15094           (if (cdr previous)
15095               (progn
15096                 (setcdr (cdr previous) entry)
15097                 (gnus-sethash group (cons num (cdr previous))
15098                               gnus-newsrc-hashtb))
15099             (setcdr previous entry)
15100             (gnus-sethash group (cons num previous)
15101                           gnus-newsrc-hashtb))
15102           (when (cdr entry)
15103             (setcdr (gnus-gethash (car (car (cdr entry))) gnus-newsrc-hashtb)
15104                     entry)))))
15105       (when gnus-group-change-level-function
15106         (funcall gnus-group-change-level-function group level oldlevel)))))
15107
15108 (defun gnus-kill-newsgroup (newsgroup)
15109   "Obsolete function.  Kills a newsgroup."
15110   (gnus-group-change-level
15111    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15112
15113 (defun gnus-check-bogus-newsgroups (&optional confirm)
15114   "Remove bogus newsgroups.
15115 If CONFIRM is non-nil, the user has to confirm the deletion of every
15116 newsgroup."
15117   (let ((newsrc (cdr gnus-newsrc-alist))
15118         bogus group entry info)
15119     (gnus-message 5 "Checking bogus newsgroups...")
15120     (unless gnus-have-read-active-file
15121       (gnus-read-active-file))
15122     (when (member gnus-select-method gnus-have-read-active-file)
15123       ;; Find all bogus newsgroup that are subscribed.
15124       (while newsrc
15125         (setq info (pop newsrc)
15126               group (gnus-info-group info))
15127         (unless (or (gnus-active group) ; Active
15128                     (gnus-info-method info) ; Foreign
15129                     (and confirm
15130                          (not (gnus-y-or-n-p
15131                                (format "Remove bogus newsgroup: %s " group)))))
15132           ;; Found a bogus newsgroup.
15133           (push group bogus)))
15134       ;; Remove all bogus subscribed groups by first killing them, and
15135       ;; then removing them from the list of killed groups.
15136       (while bogus
15137         (when (setq entry (gnus-gethash (setq group (pop bogus))
15138                                         gnus-newsrc-hashtb))
15139           (gnus-group-change-level entry gnus-level-killed)
15140           (setq gnus-killed-list (delete group gnus-killed-list))))
15141       ;; Then we remove all bogus groups from the list of killed and
15142       ;; zombie groups.  They are are removed without confirmation.
15143       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15144             killed)
15145         (while dead-lists
15146           (setq killed (symbol-value (car dead-lists)))
15147           (while killed
15148             (unless (gnus-active (setq group (pop killed)))
15149               ;; The group is bogus.
15150               ;; !!!Slow as hell.
15151               (set (car dead-lists)
15152                    (delete group (symbol-value (car dead-lists))))))
15153           (setq dead-lists (cdr dead-lists))))
15154       (gnus-message 5 "Checking bogus newsgroups...done"))))
15155
15156 (defun gnus-check-duplicate-killed-groups ()
15157   "Remove duplicates from the list of killed groups."
15158   (interactive)
15159   (let ((killed gnus-killed-list))
15160     (while killed
15161       (gnus-message 9 "%d" (length killed))
15162       (setcdr killed (delete (car killed) (cdr killed)))
15163       (setq killed (cdr killed)))))
15164
15165 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15166 ;; and compute how many unread articles there are in each group.
15167 (defun gnus-get-unread-articles (&optional level)
15168   (let* ((newsrc (cdr gnus-newsrc-alist))
15169          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15170          (foreign-level
15171           (min
15172            (cond ((and gnus-activate-foreign-newsgroups
15173                        (not (numberp gnus-activate-foreign-newsgroups)))
15174                   (1+ gnus-level-subscribed))
15175                  ((numberp gnus-activate-foreign-newsgroups)
15176                   gnus-activate-foreign-newsgroups)
15177                  (t 0))
15178            level))
15179          info group active virtuals method)
15180     (gnus-message 5 "Checking new news...")
15181
15182     (while newsrc
15183       (setq info (car newsrc)
15184             group (gnus-info-group info)
15185             active (gnus-active group))
15186
15187       ;; Check newsgroups.  If the user doesn't want to check them, or
15188       ;; they can't be checked (for instance, if the news server can't
15189       ;; be reached) we just set the number of unread articles in this
15190       ;; newsgroup to t.  This means that Gnus thinks that there are
15191       ;; unread articles, but it has no idea how many.
15192       (if (and (setq method (gnus-info-method info))
15193                (not (gnus-server-equal
15194                      gnus-select-method
15195                      (gnus-server-get-method nil method)))
15196                (not (gnus-secondary-method-p method)))
15197           ;; These groups are foreign.  Check the level.
15198           (when (<= (gnus-info-level info) foreign-level)
15199             (setq active (gnus-activate-group group 'scan))
15200             (gnus-close-group group))
15201
15202         ;; These groups are native or secondary.
15203         (when (and (<= (gnus-info-level info) level)
15204                    (not gnus-read-active-file))
15205           (setq active (gnus-activate-group group 'scan))
15206           (gnus-close-group group)))
15207
15208       (if active
15209           (gnus-get-unread-articles-in-group info active t)
15210         ;; The group couldn't be reached, so we nix out the number of
15211         ;; unread articles and stuff.
15212         (gnus-set-active group nil)
15213         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
15214
15215       (setq newsrc (cdr newsrc)))
15216
15217     (gnus-message 5 "Checking new news...done")))
15218
15219 ;; Create a hash table out of the newsrc alist.  The `car's of the
15220 ;; alist elements are used as keys.
15221 (defun gnus-make-hashtable-from-newsrc-alist ()
15222   (let ((alist gnus-newsrc-alist)
15223         (ohashtb gnus-newsrc-hashtb)
15224         prev)
15225     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15226     (setq alist
15227           (setq prev (setq gnus-newsrc-alist
15228                            (if (equal (car (car gnus-newsrc-alist))
15229                                       "dummy.group")
15230                                gnus-newsrc-alist
15231                              (cons (list "dummy.group" 0 nil) alist)))))
15232     (while alist
15233       (gnus-sethash
15234        (car (car alist))
15235        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb)))
15236              prev)
15237        gnus-newsrc-hashtb)
15238       (setq prev alist
15239             alist (cdr alist)))))
15240
15241 (defun gnus-make-hashtable-from-killed ()
15242   "Create a hash table from the killed and zombie lists."
15243   (let ((lists '(gnus-killed-list gnus-zombie-list))
15244         list)
15245     (setq gnus-killed-hashtb
15246           (gnus-make-hashtable
15247            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15248     (while lists
15249       (setq list (symbol-value (car lists)))
15250       (setq lists (cdr lists))
15251       (while list
15252         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15253         (setq list (cdr list))))))
15254
15255 (defun gnus-get-unread-articles-in-group (info active &optional update)
15256   (when active
15257     ;; Allow the backend to update the info in the group.
15258     (when update 
15259       (gnus-request-update-info
15260        info (gnus-find-method-for-group (gnus-info-group info))))
15261     (let* ((range (gnus-info-read info))
15262            (num 0)
15263            (marked (gnus-info-marks info)))
15264       ;; If a cache is present, we may have to alter the active info.
15265       (and gnus-use-cache
15266            (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15267       ;; Modify the list of read articles according to what articles
15268       ;; are available; then tally the unread articles and add the
15269       ;; number to the group hash table entry.
15270       (cond
15271        ((zerop (cdr active))
15272         (setq num 0))
15273        ((not range)
15274         (setq num (- (1+ (cdr active)) (car active))))
15275        ((not (listp (cdr range)))
15276         ;; Fix a single (num . num) range according to the
15277         ;; active hash table.
15278         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15279         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15280         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15281         ;; Compute number of unread articles.
15282         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15283        (t
15284         ;; The read list is a list of ranges.  Fix them according to
15285         ;; the active hash table.
15286         ;; First peel off any elements that are below the lower
15287         ;; active limit.
15288         (while (and (cdr range)
15289                     (>= (car active)
15290                         (or (and (atom (car (cdr range))) (car (cdr range)))
15291                             (car (car (cdr range))))))
15292           (if (numberp (car range))
15293               (setcar range
15294                       (cons (car range)
15295                             (or (and (numberp (car (cdr range)))
15296                                      (car (cdr range)))
15297                                 (cdr (car (cdr range))))))
15298             (setcdr (car range)
15299                     (or (and (numberp (nth 1 range)) (nth 1 range))
15300                         (cdr (car (cdr range))))))
15301           (setcdr range (cdr (cdr range))))
15302         ;; Adjust the first element to be the same as the lower limit.
15303         (if (and (not (atom (car range)))
15304                  (< (cdr (car range)) (car active)))
15305             (setcdr (car range) (1- (car active))))
15306         ;; Then we want to peel off any elements that are higher
15307         ;; than the upper active limit.
15308         (let ((srange range))
15309           ;; Go past all legal elements.
15310           (while (and (cdr srange)
15311                       (<= (or (and (atom (car (cdr srange)))
15312                                    (car (cdr srange)))
15313                               (car (car (cdr srange)))) (cdr active)))
15314             (setq srange (cdr srange)))
15315           (if (cdr srange)
15316               ;; Nuke all remaining illegal elements.
15317               (setcdr srange nil))
15318
15319           ;; Adjust the final element.
15320           (if (and (not (atom (car srange)))
15321                    (> (cdr (car srange)) (cdr active)))
15322               (setcdr (car srange) (cdr active))))
15323         ;; Compute the number of unread articles.
15324         (while range
15325           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15326                                       (cdr (car range))))
15327                               (or (and (atom (car range)) (car range))
15328                                   (car (car range))))))
15329           (setq range (cdr range)))
15330         (setq num (max 0 (- (cdr active) num)))))
15331       ;; Set the number of unread articles.
15332       (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
15333       num)))
15334
15335 (defun gnus-activate-group (group &optional scan)
15336   ;; Check whether a group has been activated or not.
15337   ;; If SCAN, request a scan of that group as well.
15338   (let ((method (gnus-find-method-for-group group))
15339         active)
15340     (and (gnus-check-server method)
15341          ;; We escape all bugs and quit here to make it possible to
15342          ;; continue if a group is so out-there that it reports bugs
15343          ;; and stuff.
15344          (progn
15345            (and scan
15346                 (gnus-check-backend-function 'request-scan (car method))
15347                 (gnus-request-scan group method))
15348            t)
15349          (condition-case ()
15350              (gnus-request-group group)
15351         ;   (error nil)
15352            (quit nil))
15353          (save-excursion
15354            (set-buffer nntp-server-buffer)
15355            (goto-char (point-min))
15356            ;; Parse the result we got from `gnus-request-group'.
15357            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15358                 (progn
15359                   (goto-char (match-beginning 1))
15360                   (gnus-set-active
15361                    group (setq active (cons (read (current-buffer))
15362                                             (read (current-buffer)))))
15363                   ;; Return the new active info.
15364                   active))))))
15365
15366 (defun gnus-update-read-articles (group unread)
15367   "Update the list of read and ticked articles in GROUP using the
15368 UNREAD and TICKED lists.
15369 Note: UNSELECTED has to be sorted over `<'.
15370 Returns whether the updating was successful."
15371   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15372          (entry (gnus-gethash group gnus-newsrc-hashtb))
15373          (info (nth 2 entry))
15374          (marked (gnus-info-marks info))
15375          (prev 1)
15376          (unread (sort (copy-sequence unread) '<))
15377          read)
15378     (if (or (not info) (not active))
15379         ;; There is no info on this group if it was, in fact,
15380         ;; killed.  Gnus stores no information on killed groups, so
15381         ;; there's nothing to be done.
15382         ;; One could store the information somewhere temporarily,
15383         ;; perhaps...  Hmmm...
15384         ()
15385       ;; Remove any negative articles numbers.
15386       (while (and unread (< (car unread) 0))
15387         (setq unread (cdr unread)))
15388       ;; Remove any expired article numbers
15389       (while (and unread (< (car unread) (car active)))
15390         (setq unread (cdr unread)))
15391       ;; Compute the ranges of read articles by looking at the list of
15392       ;; unread articles.
15393       (while unread
15394         (if (/= (car unread) prev)
15395             (setq read (cons (if (= prev (1- (car unread))) prev
15396                                (cons prev (1- (car unread)))) read)))
15397         (setq prev (1+ (car unread)))
15398         (setq unread (cdr unread)))
15399       (when (<= prev (cdr active))
15400         (setq read (cons (cons prev (cdr active)) read)))
15401       ;; Enter this list into the group info.
15402       (gnus-info-set-read
15403        info (if (> (length read) 1) (nreverse read) read))
15404       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15405       (gnus-get-unread-articles-in-group info (gnus-active group))
15406       t)))
15407
15408 (defun gnus-make-articles-unread (group articles)
15409   "Mark ARTICLES in GROUP as unread."
15410   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15411                           (gnus-gethash (gnus-group-real-name group)
15412                                         gnus-newsrc-hashtb))))
15413          (ranges (gnus-info-read info))
15414          news article)
15415     (while articles
15416       (when (gnus-member-of-range
15417              (setq article (pop articles)) ranges)
15418         (setq news (cons article news))))
15419     (when news
15420       (gnus-info-set-read
15421        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15422       (gnus-group-update-group group t))))
15423
15424 ;; Enter all dead groups into the hashtb.
15425 (defun gnus-update-active-hashtb-from-killed ()
15426   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15427         (lists (list gnus-killed-list gnus-zombie-list))
15428         killed)
15429     (while lists
15430       (setq killed (car lists))
15431       (while killed
15432         (gnus-sethash (car killed) nil hashtb)
15433         (setq killed (cdr killed)))
15434       (setq lists (cdr lists)))))
15435
15436 ;; Get the active file(s) from the backend(s).
15437 (defun gnus-read-active-file ()
15438   (gnus-group-set-mode-line)
15439   (let ((methods 
15440          (append
15441           (if (gnus-check-server gnus-select-method)
15442               ;; The native server is available.
15443               (cons gnus-select-method gnus-secondary-select-methods)
15444             ;; The native server is down, so we just do the
15445             ;; secondary ones.
15446             gnus-secondary-select-methods)
15447           ;; Also read from the archive server.
15448           (list "archive")))
15449         list-type)
15450     (setq gnus-have-read-active-file nil)
15451     (save-excursion
15452       (set-buffer nntp-server-buffer)
15453       (while methods
15454         (let* ((method (if (stringp (car methods))
15455                            (gnus-server-get-method nil (car methods))
15456                          (car methods)))
15457                (where (nth 1 method))
15458                (mesg (format "Reading active file%s via %s..."
15459                              (if (and where (not (zerop (length where))))
15460                                  (concat " from " where) "")
15461                              (car method))))
15462           (gnus-message 5 mesg)
15463           (if (not (gnus-check-server method))
15464               ()
15465             ;; Request that the backend scan its incoming messages.
15466             (and (gnus-check-backend-function 'request-scan (car method))
15467                  (gnus-request-scan nil method))
15468             (cond
15469              ((and (eq gnus-read-active-file 'some)
15470                    (gnus-check-backend-function 'retrieve-groups (car method)))
15471               (let ((newsrc (cdr gnus-newsrc-alist))
15472                     (gmethod (gnus-server-get-method nil method))
15473                     groups)
15474                 (while newsrc
15475                   (and (gnus-server-equal
15476                         (gnus-find-method-for-group
15477                          (car (car newsrc)) (car newsrc))
15478                         gmethod)
15479                        (setq groups (cons (gnus-group-real-name
15480                                            (car (car newsrc))) groups)))
15481                   (setq newsrc (cdr newsrc)))
15482                 (gnus-check-server method)
15483                 (setq list-type (gnus-retrieve-groups groups method))
15484                 (cond
15485                  ((not list-type)
15486                   (gnus-message
15487                    1 "Cannot read partial active file from %s server."
15488                    (car method))
15489                   (ding)
15490                   (sit-for 2))
15491                  ((eq list-type 'active)
15492                   (gnus-active-to-gnus-format method gnus-active-hashtb))
15493                  (t
15494                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
15495              (t
15496               (if (not (gnus-request-list method))
15497                   (progn
15498                     (unless (equal method gnus-message-archive-method)
15499                       (gnus-message 1 "Cannot read active file from %s server."
15500                                     (car method))
15501                       (ding)))
15502                 (gnus-active-to-gnus-format method)
15503                 ;; We mark this active file as read.
15504                 (push method gnus-have-read-active-file)
15505                 (gnus-message 5 "%sdone" mesg))))))
15506         (setq methods (cdr methods))))))
15507
15508 ;; Read an active file and place the results in `gnus-active-hashtb'.
15509 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15510   (unless method
15511     (setq method gnus-select-method))
15512   (let ((cur (current-buffer))
15513         (hashtb (or hashtb
15514                     (if (and gnus-active-hashtb
15515                              (not (equal method gnus-select-method)))
15516                         gnus-active-hashtb
15517                       (setq gnus-active-hashtb
15518                             (if (equal method gnus-select-method)
15519                                 (gnus-make-hashtable
15520                                  (count-lines (point-min) (point-max)))
15521                               (gnus-make-hashtable 4096))))))
15522         (flag-hashtb (gnus-make-hashtable 60)))
15523     ;; Delete unnecessary lines.
15524     (goto-char (point-min))
15525     (while (search-forward "\nto." nil t)
15526       (delete-region (1+ (match-beginning 0))
15527                      (progn (forward-line 1) (point))))
15528     (or (string= gnus-ignored-newsgroups "")
15529         (progn
15530           (goto-char (point-min))
15531           (delete-matching-lines gnus-ignored-newsgroups)))
15532     ;; Make the group names readable as a lisp expression even if they
15533     ;; contain special characters.
15534     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15535     (goto-char (point-max))
15536     (while (re-search-backward "[][';?()#]" nil t)
15537       (insert ?\\))
15538     ;; If these are groups from a foreign select method, we insert the
15539     ;; group prefix in front of the group names.
15540     (and method (not (gnus-server-equal
15541                       (gnus-server-get-method nil method)
15542                       (gnus-server-get-method nil gnus-select-method)))
15543          (let ((prefix (gnus-group-prefixed-name "" method)))
15544            (goto-char (point-min))
15545            (while (and (not (eobp))
15546                        (progn (insert prefix)
15547                               (zerop (forward-line 1)))))))
15548     ;; Store the active file in a hash table.
15549     (goto-char (point-min))
15550     (if (string-match "%[oO]" gnus-group-line-format)
15551         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15552         ;; If we want information on moderated groups, we use this
15553         ;; loop...
15554         (let* ((mod-hashtb (make-vector 7 0))
15555                (m (intern "m" mod-hashtb))
15556                group max min)
15557           (while (not (eobp))
15558             (condition-case nil
15559                 (progn
15560                   (narrow-to-region (point) (gnus-point-at-eol))
15561                   (setq group (let ((obarray hashtb)) (read cur)))
15562                   (if (and (numberp (setq max (read cur)))
15563                            (numberp (setq min (read cur)))
15564                            (progn
15565                              (skip-chars-forward " \t")
15566                              (not
15567                               (or (= (following-char) ?=)
15568                                   (= (following-char) ?x)
15569                                   (= (following-char) ?j)))))
15570                       (set group (cons min max))
15571                     (set group nil))
15572                   ;; Enter moderated groups into a list.
15573                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15574                       (setq gnus-moderated-list
15575                             (cons (symbol-name group) gnus-moderated-list))))
15576               (error
15577                (and group
15578                     (symbolp group)
15579                     (set group nil))))
15580             (widen)
15581             (forward-line 1)))
15582       ;; And if we do not care about moderation, we use this loop,
15583       ;; which is faster.
15584       (let (group max min)
15585         (while (not (eobp))
15586           (condition-case ()
15587               (progn
15588                 (narrow-to-region (point) (gnus-point-at-eol))
15589                 ;; group gets set to a symbol interned in the hash table
15590                 ;; (what a hack!!) - jwz
15591                 (setq group (let ((obarray hashtb)) (read cur)))
15592                 (if (and (numberp (setq max (read cur)))
15593                          (numberp (setq min (read cur)))
15594                          (progn
15595                            (skip-chars-forward " \t")
15596                            (not
15597                             (or (= (following-char) ?=)
15598                                 (= (following-char) ?x)
15599                                 (= (following-char) ?j)))))
15600                     (set group (cons min max))
15601                   (set group nil)))
15602             (error
15603              (progn
15604                (and group
15605                     (symbolp group)
15606                     (set group nil))
15607                (or ignore-errors
15608                    (gnus-message 3 "Warning - illegal active: %s"
15609                                  (buffer-substring
15610                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15611           (widen)
15612           (forward-line 1))))))
15613
15614 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15615   ;; Parse a "groups" active file.
15616   (let ((cur (current-buffer))
15617         (hashtb (or hashtb
15618                     (if (and method gnus-active-hashtb)
15619                         gnus-active-hashtb
15620                       (setq gnus-active-hashtb
15621                             (gnus-make-hashtable
15622                              (count-lines (point-min) (point-max)))))))
15623         (prefix (and method
15624                      (not (gnus-server-equal
15625                            (gnus-server-get-method nil method)
15626                            (gnus-server-get-method nil gnus-select-method)))
15627                      (gnus-group-prefixed-name "" method))))
15628
15629     (goto-char (point-min))
15630     ;; We split this into to separate loops, one with the prefix
15631     ;; and one without to speed the reading up somewhat.
15632     (if prefix
15633         (let (min max opoint group)
15634           (while (not (eobp))
15635             (condition-case ()
15636                 (progn
15637                   (read cur) (read cur)
15638                   (setq min (read cur)
15639                         max (read cur)
15640                         opoint (point))
15641                   (skip-chars-forward " \t")
15642                   (insert prefix)
15643                   (goto-char opoint)
15644                   (set (let ((obarray hashtb)) (read cur))
15645                        (cons min max)))
15646               (error (and group (symbolp group) (set group nil))))
15647             (forward-line 1)))
15648       (let (min max group)
15649         (while (not (eobp))
15650           (condition-case ()
15651               (if (= (following-char) ?2)
15652                   (progn
15653                     (read cur) (read cur)
15654                     (setq min (read cur)
15655                           max (read cur))
15656                     (set (setq group (let ((obarray hashtb)) (read cur)))
15657                          (cons min max))))
15658             (error (and group (symbolp group) (set group nil))))
15659           (forward-line 1))))))
15660
15661 (defun gnus-read-newsrc-file (&optional force)
15662   "Read startup file.
15663 If FORCE is non-nil, the .newsrc file is read."
15664   ;; Reset variables that might be defined in the .newsrc.eld file.
15665   (let ((variables gnus-variable-list))
15666     (while variables
15667       (set (car variables) nil)
15668       (setq variables (cdr variables))))
15669   (let* ((newsrc-file gnus-current-startup-file)
15670          (quick-file (concat newsrc-file ".el")))
15671     (save-excursion
15672       ;; We always load the .newsrc.eld file.  If always contains
15673       ;; much information that can not be gotten from the .newsrc
15674       ;; file (ticked articles, killed groups, foreign methods, etc.)
15675       (gnus-read-newsrc-el-file quick-file)
15676
15677       (if (or force
15678               (and (file-newer-than-file-p newsrc-file quick-file)
15679                    (file-newer-than-file-p newsrc-file
15680                                            (concat quick-file "d")))
15681               (not gnus-newsrc-alist))
15682           ;; We read the .newsrc file.  Note that if there if a
15683           ;; .newsrc.eld file exists, it has already been read, and
15684           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15685           ;; the .newsrc file, Gnus will only use the information it
15686           ;; can find there for changing the data already read -
15687           ;; ie. reading the .newsrc file will not trash the data
15688           ;; already read (except for read articles).
15689           (save-excursion
15690             (gnus-message 5 "Reading %s..." newsrc-file)
15691             (set-buffer (find-file-noselect newsrc-file))
15692             (buffer-disable-undo (current-buffer))
15693             (gnus-newsrc-to-gnus-format)
15694             (kill-buffer (current-buffer))
15695             (gnus-message 5 "Reading %s...done" newsrc-file)))
15696
15697       ;; Read any slave files.
15698       (or gnus-slave
15699           (gnus-master-read-slave-newsrc)))))
15700
15701 (defun gnus-read-newsrc-el-file (file)
15702   (let ((ding-file (concat file "d")))
15703     ;; We always, always read the .eld file.
15704     (gnus-message 5 "Reading %s..." ding-file)
15705     (let (gnus-newsrc-assoc)
15706       (condition-case nil
15707           (load ding-file t t t)
15708         (error
15709          (gnus-message 1 "Error in %s" ding-file)
15710          (ding)))
15711       (when gnus-newsrc-assoc
15712         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15713     (gnus-make-hashtable-from-newsrc-alist)
15714     (when (file-newer-than-file-p file ding-file)
15715       ;; Old format quick file
15716       (gnus-message 5 "Reading %s..." file)
15717       ;; The .el file is newer than the .eld file, so we read that one
15718       ;; as well.
15719       (gnus-read-old-newsrc-el-file file))))
15720
15721 ;; Parse the old-style quick startup file
15722 (defun gnus-read-old-newsrc-el-file (file)
15723   (let (newsrc killed marked group m)
15724     (prog1
15725         (let ((gnus-killed-assoc nil)
15726               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15727           (prog1
15728               (condition-case nil
15729                   (load file t t t)
15730                 (error nil))
15731             (setq newsrc gnus-newsrc-assoc
15732                   killed gnus-killed-assoc
15733                   marked gnus-marked-assoc)))
15734       (setq gnus-newsrc-alist nil)
15735       (while newsrc
15736         (setq group (car newsrc))
15737         (let ((info (gnus-get-info (car group))))
15738           (if info
15739               (progn
15740                 (gnus-info-set-read info (cdr (cdr group)))
15741                 (gnus-info-set-level
15742                  info (if (nth 1 group) gnus-level-default-subscribed
15743                         gnus-level-default-unsubscribed))
15744                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15745             (setq gnus-newsrc-alist
15746                   (cons
15747                    (setq info
15748                          (list (car group)
15749                                (if (nth 1 group) gnus-level-default-subscribed
15750                                  gnus-level-default-unsubscribed)
15751                                (cdr (cdr group))))
15752                    gnus-newsrc-alist)))
15753           (if (setq m (assoc (car group) marked))
15754               (gnus-info-set-marks
15755                info (cons (list (cons 'tick (gnus-compress-sequence
15756                                              (sort (cdr m) '<) t)))
15757                           nil))))
15758         (setq newsrc (cdr newsrc)))
15759       (setq newsrc killed)
15760       (while newsrc
15761         (setcar newsrc (car (car newsrc)))
15762         (setq newsrc (cdr newsrc)))
15763       (setq gnus-killed-list killed))
15764     ;; The .el file version of this variable does not begin with
15765     ;; "options", while the .eld version does, so we just add it if it
15766     ;; isn't there.
15767     (and
15768      gnus-newsrc-options
15769      (progn
15770        (and (not (string-match "^ *options" gnus-newsrc-options))
15771             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15772        (and (not (string-match "\n$" gnus-newsrc-options))
15773             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15774        ;; Finally, if we read some options lines, we parse them.
15775        (or (string= gnus-newsrc-options "")
15776            (gnus-newsrc-parse-options gnus-newsrc-options))))
15777
15778     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15779     (gnus-make-hashtable-from-newsrc-alist)))
15780
15781 (defun gnus-make-newsrc-file (file)
15782   "Make server dependent file name by catenating FILE and server host name."
15783   (let* ((file (expand-file-name file nil))
15784          (real-file (concat file "-" (nth 1 gnus-select-method))))
15785     (if (or (file-exists-p real-file)
15786             (file-exists-p (concat real-file ".el"))
15787             (file-exists-p (concat real-file ".eld")))
15788         real-file file)))
15789
15790 (defun gnus-newsrc-to-gnus-format ()
15791   (setq gnus-newsrc-options "")
15792   (setq gnus-newsrc-options-n nil)
15793
15794   (or gnus-active-hashtb
15795       (setq gnus-active-hashtb (make-vector 4095 0)))
15796   (let ((buf (current-buffer))
15797         (already-read (> (length gnus-newsrc-alist) 1))
15798         group subscribed options-symbol newsrc Options-symbol
15799         symbol reads num1)
15800     (goto-char (point-min))
15801     ;; We intern the symbol `options' in the active hashtb so that we
15802     ;; can `eq' against it later.
15803     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15804     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15805
15806     (while (not (eobp))
15807       ;; We first read the first word on the line by narrowing and
15808       ;; then reading into `gnus-active-hashtb'.  Most groups will
15809       ;; already exist in that hashtb, so this will save some string
15810       ;; space.
15811       (narrow-to-region
15812        (point)
15813        (progn (skip-chars-forward "^ \t!:\n") (point)))
15814       (goto-char (point-min))
15815       (setq symbol
15816             (and (/= (point-min) (point-max))
15817                  (let ((obarray gnus-active-hashtb)) (read buf))))
15818       (widen)
15819       ;; Now, the symbol we have read is either `options' or a group
15820       ;; name.  If it is an options line, we just add it to a string.
15821       (cond
15822        ((or (eq symbol options-symbol)
15823             (eq symbol Options-symbol))
15824         (setq gnus-newsrc-options
15825               ;; This concating is quite inefficient, but since our
15826               ;; thorough studies show that approx 99.37% of all
15827               ;; .newsrc files only contain a single options line, we
15828               ;; don't give a damn, frankly, my dear.
15829               (concat gnus-newsrc-options
15830                       (buffer-substring
15831                        (gnus-point-at-bol)
15832                        ;; Options may continue on the next line.
15833                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15834                                 (progn (beginning-of-line) (point)))
15835                            (point)))))
15836         (forward-line -1))
15837        (symbol
15838         ;; Group names can be just numbers.  
15839         (when (numberp symbol) 
15840           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15841         (or (boundp symbol) (set symbol nil))
15842         ;; It was a group name.
15843         (setq subscribed (= (following-char) ?:)
15844               group (symbol-name symbol)
15845               reads nil)
15846         (if (eolp)
15847             ;; If the line ends here, this is clearly a buggy line, so
15848             ;; we put point a the beginning of line and let the cond
15849             ;; below do the error handling.
15850             (beginning-of-line)
15851           ;; We skip to the beginning of the ranges.
15852           (skip-chars-forward "!: \t"))
15853         ;; We are now at the beginning of the list of read articles.
15854         ;; We read them range by range.
15855         (while
15856             (cond
15857              ((looking-at "[0-9]+")
15858               ;; We narrow and read a number instead of buffer-substring/
15859               ;; string-to-int because it's faster.  narrow/widen is
15860               ;; faster than save-restriction/narrow, and save-restriction
15861               ;; produces a garbage object.
15862               (setq num1 (progn
15863                            (narrow-to-region (match-beginning 0) (match-end 0))
15864                            (read buf)))
15865               (widen)
15866               ;; If the next character is a dash, then this is a range.
15867               (if (= (following-char) ?-)
15868                   (progn
15869                     ;; We read the upper bound of the range.
15870                     (forward-char 1)
15871                     (if (not (looking-at "[0-9]+"))
15872                         ;; This is a buggy line, by we pretend that
15873                         ;; it's kinda OK.  Perhaps the user should be
15874                         ;; dinged?
15875                         (setq reads (cons num1 reads))
15876                       (setq reads
15877                             (cons
15878                              (cons num1
15879                                    (progn
15880                                      (narrow-to-region (match-beginning 0)
15881                                                        (match-end 0))
15882                                      (read buf)))
15883                              reads))
15884                       (widen)))
15885                 ;; It was just a simple number, so we add it to the
15886                 ;; list of ranges.
15887                 (setq reads (cons num1 reads)))
15888               ;; If the next char in ?\n, then we have reached the end
15889               ;; of the line and return nil.
15890               (/= (following-char) ?\n))
15891              ((= (following-char) ?\n)
15892               ;; End of line, so we end.
15893               nil)
15894              (t
15895               ;; Not numbers and not eol, so this might be a buggy
15896               ;; line...
15897               (or (eobp)
15898                   ;; If it was eob instead of ?\n, we allow it.
15899                   (progn
15900                     ;; The line was buggy.
15901                     (setq group nil)
15902                     (gnus-message 3 "Mangled line: %s"
15903                                   (buffer-substring (gnus-point-at-bol)
15904                                                     (gnus-point-at-eol)))
15905                     (ding)
15906                     (sit-for 1)))
15907               nil))
15908           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15909           ;; we allow them, because it's a common mistake to put a
15910           ;; space after the comma.
15911           (skip-chars-forward ", "))
15912
15913         ;; We have already read .newsrc.eld, so we gently update the
15914         ;; data in the hash table with the information we have just
15915         ;; read.
15916         (when group
15917           (let ((info (gnus-get-info group))
15918                 level)
15919             (if info
15920                 ;; There is an entry for this file in the alist.
15921                 (progn
15922                   (gnus-info-set-read info (nreverse reads))
15923                   ;; We update the level very gently.  In fact, we
15924                   ;; only change it if there's been a status change
15925                   ;; from subscribed to unsubscribed, or vice versa.
15926                   (setq level (gnus-info-level info))
15927                   (cond ((and (<= level gnus-level-subscribed)
15928                               (not subscribed))
15929                          (setq level (if reads
15930                                          gnus-level-default-unsubscribed
15931                                        (1+ gnus-level-default-unsubscribed))))
15932                         ((and (> level gnus-level-subscribed) subscribed)
15933                          (setq level gnus-level-default-subscribed)))
15934                   (gnus-info-set-level info level))
15935               ;; This is a new group.
15936               (setq info (list group
15937                                (if subscribed
15938                                    gnus-level-default-subscribed
15939                                  (if reads
15940                                      (1+ gnus-level-subscribed)
15941                                    gnus-level-default-unsubscribed))
15942                                (nreverse reads))))
15943             (setq newsrc (cons info newsrc))))))
15944       (forward-line 1))
15945
15946     (setq newsrc (nreverse newsrc))
15947
15948     (if (not already-read)
15949         ()
15950       ;; We now have two newsrc lists - `newsrc', which is what we
15951       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15952       ;; what we've read from .newsrc.eld.  We have to merge these
15953       ;; lists.  We do this by "attaching" any (foreign) groups in the
15954       ;; gnus-newsrc-alist to the (native) group that precedes them.
15955       (let ((rc (cdr gnus-newsrc-alist))
15956             (prev gnus-newsrc-alist)
15957             entry mentry)
15958         (while rc
15959           (or (null (nth 4 (car rc)))   ; It's a native group.
15960               (assoc (car (car rc)) newsrc) ; It's already in the alist.
15961               (if (setq entry (assoc (car (car prev)) newsrc))
15962                   (setcdr (setq mentry (memq entry newsrc))
15963                           (cons (car rc) (cdr mentry)))
15964                 (setq newsrc (cons (car rc) newsrc))))
15965           (setq prev rc
15966                 rc (cdr rc)))))
15967
15968     (setq gnus-newsrc-alist newsrc)
15969     ;; We make the newsrc hashtb.
15970     (gnus-make-hashtable-from-newsrc-alist)
15971
15972     ;; Finally, if we read some options lines, we parse them.
15973     (or (string= gnus-newsrc-options "")
15974         (gnus-newsrc-parse-options gnus-newsrc-options))))
15975
15976 ;; Parse options lines to find "options -n !all rec.all" and stuff.
15977 ;; The return value will be a list on the form
15978 ;; ((regexp1 . ignore)
15979 ;;  (regexp2 . subscribe)...)
15980 ;; When handling new newsgroups, groups that match a `ignore' regexp
15981 ;; will be ignored, and groups that match a `subscribe' regexp will be
15982 ;; subscribed.  A line like
15983 ;; options -n !all rec.all
15984 ;; will lead to a list that looks like
15985 ;; (("^rec\\..+" . subscribe)
15986 ;;  ("^.+" . ignore))
15987 ;; So all "rec.*" groups will be subscribed, while all the other
15988 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
15989 ;; different from "options -n rec.all !all".
15990 (defun gnus-newsrc-parse-options (options)
15991   (let (out eol)
15992     (save-excursion
15993       (gnus-set-work-buffer)
15994       (insert (regexp-quote options))
15995       ;; First we treat all continuation lines.
15996       (goto-char (point-min))
15997       (while (re-search-forward "\n[ \t]+" nil t)
15998         (replace-match " " t t))
15999       ;; Then we transform all "all"s into ".+"s.
16000       (goto-char (point-min))
16001       (while (re-search-forward "\\ball\\b" nil t)
16002         (replace-match ".+" t t))
16003       (goto-char (point-min))
16004       ;; We remove all other options than the "-n" ones.
16005       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16006         (replace-match " ")
16007         (forward-char -1))
16008       (goto-char (point-min))
16009
16010       ;; We are only interested in "options -n" lines - we
16011       ;; ignore the other option lines.
16012       (while (re-search-forward "[ \t]-n" nil t)
16013         (setq eol
16014               (or (save-excursion
16015                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16016                          (- (point) 2)))
16017                   (gnus-point-at-eol)))
16018         ;; Search for all "words"...
16019         (while (re-search-forward "[^ \t,\n]+" eol t)
16020           (if (= (char-after (match-beginning 0)) ?!)
16021               ;; If the word begins with a bang (!), this is a "not"
16022               ;; spec.  We put this spec (minus the bang) and the
16023               ;; symbol `ignore' into the list.
16024               (setq out (cons (cons (concat
16025                                      "^" (buffer-substring
16026                                           (1+ (match-beginning 0))
16027                                           (match-end 0)))
16028                                     'ignore) out))
16029             ;; There was no bang, so this is a "yes" spec.
16030             (setq out (cons (cons (concat "^" (match-string 0))
16031                                   'subscribe) out)))))
16032
16033       (setq gnus-newsrc-options-n out))))
16034
16035 (defun gnus-save-newsrc-file (&optional force)
16036   "Save .newsrc file."
16037   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16038   ;; from the variable gnus-newsrc-alist.
16039   (when (and (or gnus-newsrc-alist gnus-killed-list)
16040              gnus-current-startup-file)
16041     (save-excursion
16042       (if (and (or gnus-use-dribble-file gnus-slave)
16043                (not force)
16044                (or (not gnus-dribble-buffer)
16045                    (not (buffer-name gnus-dribble-buffer))
16046                    (zerop (save-excursion
16047                             (set-buffer gnus-dribble-buffer)
16048                             (buffer-size)))))
16049           (gnus-message 4 "(No changes need to be saved)")
16050         (run-hooks 'gnus-save-newsrc-hook)
16051         (if gnus-slave
16052             (gnus-slave-save-newsrc)
16053           ;; Save .newsrc.
16054           (when gnus-save-newsrc-file
16055             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16056             (gnus-gnus-to-newsrc-format)
16057             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16058           ;; Save .newsrc.eld.
16059           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16060           (make-local-variable 'version-control)
16061           (setq version-control 'never)
16062           (setq buffer-file-name
16063                 (concat gnus-current-startup-file ".eld"))
16064           (gnus-add-current-to-buffer-list)
16065           (buffer-disable-undo (current-buffer))
16066           (erase-buffer)
16067           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16068           (gnus-gnus-to-quick-newsrc-format)
16069           (run-hooks 'gnus-save-quick-newsrc-hook)
16070           (save-buffer)
16071           (kill-buffer (current-buffer))
16072           (gnus-message
16073            5 "Saving %s.eld...done" gnus-current-startup-file))
16074         (gnus-dribble-delete-file)))))
16075
16076 (defun gnus-gnus-to-quick-newsrc-format ()
16077   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16078   (insert ";; Gnus startup file.\n")
16079   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16080   (insert ";; to read .newsrc.\n")
16081   (insert "(setq gnus-newsrc-file-version "
16082           (prin1-to-string gnus-version) ")\n")
16083   (let ((variables
16084          (if gnus-save-killed-list gnus-variable-list
16085            ;; Remove the `gnus-killed-list' from the list of variables
16086            ;; to be saved, if required.
16087            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16088         ;; Peel off the "dummy" group.
16089         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16090         variable)
16091     ;; Insert the variables into the file.
16092     (while variables
16093       (when (and (boundp (setq variable (pop variables)))
16094                  (symbol-value variable))
16095         (insert "(setq " (symbol-name variable) " '"
16096                 (prin1-to-string (symbol-value variable)) ")\n")))))
16097
16098 (defun gnus-gnus-to-newsrc-format ()
16099   ;; Generate and save the .newsrc file.
16100   (let ((newsrc (cdr gnus-newsrc-alist))
16101         info ranges range)
16102     (save-excursion
16103       (set-buffer (create-file-buffer gnus-current-startup-file))
16104       (setq buffer-file-name gnus-current-startup-file)
16105       (buffer-disable-undo (current-buffer))
16106       (erase-buffer)
16107       ;; Write options.
16108       (if gnus-newsrc-options (insert gnus-newsrc-options))
16109       ;; Write subscribed and unsubscribed.
16110       (while newsrc
16111         (setq info (car newsrc))
16112         (if (not (gnus-info-method info))
16113             ;; Don't write foreign groups to .newsrc.
16114             (progn
16115               (insert (gnus-info-group info)
16116                       (if (> (gnus-info-level info) gnus-level-subscribed)
16117                           "!" ":"))
16118               (if (setq ranges (gnus-info-read info))
16119                   (progn
16120                     (insert " ")
16121                     (if (not (listp (cdr ranges)))
16122                         (if (= (car ranges) (cdr ranges))
16123                             (insert (int-to-string (car ranges)))
16124                           (insert (int-to-string (car ranges)) "-"
16125                                   (int-to-string (cdr ranges))))
16126                       (while ranges
16127                         (setq range (car ranges)
16128                               ranges (cdr ranges))
16129                         (if (or (atom range) (= (car range) (cdr range)))
16130                             (insert (int-to-string
16131                                      (or (and (atom range) range)
16132                                          (car range))))
16133                           (insert (int-to-string (car range)) "-"
16134                                   (int-to-string (cdr range))))
16135                         (if ranges (insert ","))))))
16136               (insert "\n")))
16137         (setq newsrc (cdr newsrc)))
16138       (make-local-variable 'version-control)
16139       (setq version-control 'never)
16140       ;; It has been reported that sometime the modtime on the .newsrc
16141       ;; file seems to be off.  We really do want to overwrite it, so
16142       ;; we clear the modtime here before saving.  It's a bit odd,
16143       ;; though...
16144       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16145       ;; delete the silly thing entirely first.  but this fails to provide
16146       ;; such niceties as .newsrc~ creation.
16147       (if gnus-modtime-botch
16148           (delete-file gnus-startup-file)
16149         (clear-visited-file-modtime))
16150       (run-hooks 'gnus-save-standard-newsrc-hook)
16151       (save-buffer)
16152       (kill-buffer (current-buffer)))))
16153
16154 \f
16155 ;;;
16156 ;;; Slave functions.
16157 ;;;
16158
16159 (defun gnus-slave-save-newsrc ()
16160   (save-excursion
16161     (set-buffer gnus-dribble-buffer)
16162     (let ((slave-name
16163            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16164       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16165
16166 (defun gnus-master-read-slave-newsrc ()
16167   (let ((slave-files
16168          (directory-files
16169           (file-name-directory gnus-current-startup-file)
16170           t (concat
16171              "^" (regexp-quote
16172                   (concat
16173                    (file-name-nondirectory gnus-current-startup-file)
16174                    "-slave-")))
16175           t))
16176         file)
16177     (if (not slave-files)
16178         ()                              ; There are no slave files to read.
16179       (gnus-message 7 "Reading slave newsrcs...")
16180       (save-excursion
16181         (set-buffer (get-buffer-create " *gnus slave*"))
16182         (buffer-disable-undo (current-buffer))
16183         (setq slave-files
16184               (sort (mapcar (lambda (file)
16185                               (list (nth 5 (file-attributes file)) file))
16186                             slave-files)
16187                     (lambda (f1 f2)
16188                       (or (< (car (car f1)) (car (car f2)))
16189                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16190         (while slave-files
16191           (erase-buffer)
16192           (setq file (nth 1 (car slave-files)))
16193           (insert-file-contents file)
16194           (if (condition-case ()
16195                   (progn
16196                     (eval-buffer (current-buffer))
16197                     t)
16198                 (error
16199                  (gnus-message 3 "Possible error in %s" file)
16200                  (ding)
16201                  (sit-for 2)
16202                  nil))
16203               (or gnus-slave ; Slaves shouldn't delete these files.
16204                   (condition-case ()
16205                       (delete-file file)
16206                     (error nil))))
16207           (setq slave-files (cdr slave-files))))
16208       (gnus-message 7 "Reading slave newsrcs...done"))))
16209
16210 \f
16211 ;;;
16212 ;;; Group description.
16213 ;;;
16214
16215 (defun gnus-read-all-descriptions-files ()
16216   (let ((methods (cons gnus-select-method 
16217                        (cons "archive"
16218                              gnus-secondary-select-methods))))
16219     (while methods
16220       (gnus-read-descriptions-file (car methods))
16221       (setq methods (cdr methods)))
16222     t))
16223
16224 (defun gnus-read-descriptions-file (&optional method)
16225   (let ((method (or method gnus-select-method)))
16226     (when (stringp method)
16227       (setq method (gnus-server-to-method method)))
16228     ;; We create the hashtable whether we manage to read the desc file
16229     ;; to avoid trying to re-read after a failed read.
16230     (or gnus-description-hashtb
16231         (setq gnus-description-hashtb
16232               (gnus-make-hashtable (length gnus-active-hashtb))))
16233     ;; Mark this method's desc file as read.
16234     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16235                   gnus-description-hashtb)
16236
16237     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16238     (cond
16239      ((not (gnus-check-server method))
16240       (gnus-message 1 "Couldn't open server")
16241       nil)
16242      ((not (gnus-request-list-newsgroups method))
16243       (gnus-message 1 "Couldn't read newsgroups descriptions")
16244       nil)
16245      (t
16246       (let (group)
16247         (save-excursion
16248           (save-restriction
16249             (set-buffer nntp-server-buffer)
16250             (goto-char (point-min))
16251             (if (or (search-forward "\n.\n" nil t)
16252                     (goto-char (point-max)))
16253                 (progn
16254                   (beginning-of-line)
16255                   (narrow-to-region (point-min) (point))))
16256             (goto-char (point-min))
16257             (while (not (eobp))
16258               ;; If we get an error, we set group to 0, which is not a
16259               ;; symbol...
16260               (setq group
16261                     (condition-case ()
16262                         (let ((obarray gnus-description-hashtb))
16263                           ;; Group is set to a symbol interned in this
16264                           ;; hash table.
16265                           (read nntp-server-buffer))
16266                       (error 0)))
16267               (skip-chars-forward " \t")
16268               ;; ...  which leads to this line being effectively ignored.
16269               (and (symbolp group)
16270                    (set group (buffer-substring
16271                                (point) (progn (end-of-line) (point)))))
16272               (forward-line 1))))
16273         (gnus-message 5 "Reading descriptions file...done")
16274         t)))))
16275
16276 (defun gnus-group-get-description (group)
16277   "Get the description of a group by sending XGTITLE to the server."
16278   (when (gnus-request-group-description group)
16279     (save-excursion
16280       (set-buffer nntp-server-buffer)
16281       (goto-char (point-min))
16282       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16283         (match-string 1)))))
16284
16285 \f
16286 ;;;
16287 ;;; Buffering of read articles.
16288 ;;;
16289
16290 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16291 (defvar gnus-backlog-articles nil)
16292 (defvar gnus-backlog-hashtb nil)
16293
16294 (defun gnus-backlog-buffer ()
16295   "Return the backlog buffer."
16296   (or (get-buffer gnus-backlog-buffer)
16297       (save-excursion
16298         (set-buffer (get-buffer-create gnus-backlog-buffer))
16299         (buffer-disable-undo (current-buffer))
16300         (setq buffer-read-only t)
16301         (gnus-add-current-to-buffer-list)
16302         (get-buffer gnus-backlog-buffer))))
16303
16304 (defun gnus-backlog-setup ()
16305   "Initialize backlog variables."
16306   (unless gnus-backlog-hashtb
16307     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16308
16309 (defun gnus-backlog-shutdown ()
16310   "Clear all backlog variables and buffers."
16311   (when (get-buffer gnus-backlog-buffer)
16312     (kill-buffer gnus-backlog-buffer))
16313   (setq gnus-backlog-hashtb nil
16314         gnus-backlog-articles nil))
16315
16316 (defun gnus-backlog-enter-article (group number buffer)
16317   (gnus-backlog-setup)
16318   (let ((ident (intern (concat group ":" (int-to-string number))
16319                        gnus-backlog-hashtb))
16320         b)
16321     (if (memq ident gnus-backlog-articles)
16322         () ; It's already kept.
16323       ;; Remove the oldest article, if necessary.
16324       (and (numberp gnus-keep-backlog)
16325            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16326            (gnus-backlog-remove-oldest-article))
16327       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16328       ;; Insert the new article.
16329       (save-excursion
16330         (set-buffer (gnus-backlog-buffer))
16331         (let (buffer-read-only)
16332           (goto-char (point-max))
16333           (or (bolp) (insert "\n"))
16334           (setq b (point))
16335           (insert-buffer-substring buffer)
16336           ;; Tag the beginning of the article with the ident.
16337           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16338
16339 (defun gnus-backlog-remove-oldest-article ()
16340   (save-excursion
16341     (set-buffer (gnus-backlog-buffer))
16342     (goto-char (point-min))
16343     (if (zerop (buffer-size))
16344         () ; The buffer is empty.
16345       (let ((ident (get-text-property (point) 'gnus-backlog))
16346             buffer-read-only)
16347         ;; Remove the ident from the list of articles.
16348         (when ident
16349           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16350         ;; Delete the article itself.
16351         (delete-region
16352          (point) (next-single-property-change
16353                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16354
16355 (defun gnus-backlog-request-article (group number buffer)
16356   (gnus-backlog-setup)
16357   (let ((ident (intern (concat group ":" (int-to-string number))
16358                        gnus-backlog-hashtb))
16359         beg end)
16360     (when (memq ident gnus-backlog-articles)
16361       ;; It was in the backlog.
16362       (save-excursion
16363         (set-buffer (gnus-backlog-buffer))
16364         (if (not (setq beg (text-property-any
16365                             (point-min) (point-max) 'gnus-backlog
16366                             ident)))
16367             ;; It wasn't in the backlog after all.
16368             (ignore
16369              (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16370           ;; Find the end (i. e., the beginning of the next article).
16371           (setq end
16372                 (next-single-property-change
16373                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16374       (let ((buffer-read-only nil))
16375         (erase-buffer)
16376         (insert-buffer-substring gnus-backlog-buffer beg end)
16377         t))))
16378
16379 ;; Allow redefinition of Gnus functions.
16380
16381 (gnus-ems-redefine)
16382
16383 (provide 'gnus)
16384
16385 ;;; gnus.el ends here