*** 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.41"
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 (defmacro gnus-summary-skip-intangible ()
7053   "If the current article is intangible, then jump to a different article."
7054   '(let ((to (get-text-property (point) 'gnus-intangible)))
7055     (and to (gnus-summary-goto-subject to))))
7056
7057 (defmacro gnus-summary-article-intangible-p ()
7058   "Say whether this article is intangible or not."
7059   '(get-text-property (point) 'gnus-intangible))
7060
7061 ;; Some summary mode macros.
7062
7063 (defmacro gnus-summary-article-number ()
7064   "The article number of the article on the current line.
7065 If there isn's an article number here, then we return the current
7066 article number."
7067   '(progn
7068      (gnus-summary-skip-intangible)
7069      (or (get-text-property (point) 'gnus-number)
7070          (gnus-summary-last-subject))))
7071
7072 (defmacro gnus-summary-article-header (&optional number)
7073   `(gnus-data-header (gnus-data-find
7074                       ,(or number '(gnus-summary-article-number)))))
7075
7076 (defmacro gnus-summary-thread-level (&optional number)
7077   `(if (and (eq gnus-summary-make-false-root 'dummy)
7078             (get-text-property (point) 'gnus-intangible))
7079        0
7080      (gnus-data-level (gnus-data-find
7081                        ,(or number '(gnus-summary-article-number))))))
7082
7083 (defmacro gnus-summary-article-mark (&optional number)
7084   `(gnus-data-mark (gnus-data-find
7085                     ,(or number '(gnus-summary-article-number)))))
7086
7087 (defmacro gnus-summary-article-pos (&optional number)
7088   `(gnus-data-pos (gnus-data-find
7089                    ,(or number '(gnus-summary-article-number)))))
7090
7091 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7092 (defmacro gnus-summary-article-subject (&optional number)
7093   "Return current subject string or nil if nothing."
7094   `(let ((headers
7095           ,(if number
7096                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7097              '(gnus-data-header (assq (gnus-summary-article-number)
7098                                       gnus-newsgroup-data)))))
7099      (and headers
7100           (vectorp headers)
7101           (mail-header-subject headers))))
7102
7103 (defmacro gnus-summary-article-score (&optional number)
7104   "Return current article score."
7105   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7106                   gnus-newsgroup-scored))
7107        gnus-summary-default-score 0))
7108
7109 (defun gnus-summary-article-children (&optional number)
7110   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7111          (level (gnus-data-level (car data)))
7112          l children)
7113     (while (and (setq data (cdr data))
7114                 (> (setq l (gnus-data-level (car data))) level))
7115       (and (= (1+ level) l)
7116            (setq children (cons (gnus-data-number (car data))
7117                                 children))))
7118     (nreverse children)))
7119
7120 (defun gnus-summary-article-parent (&optional number)
7121   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7122                                     (gnus-data-list t)))
7123          (level (gnus-data-level (car data)))
7124          l)
7125     (if (zerop level)
7126         () ; This is a root.
7127       ;; We search until we find an article with a level less than
7128       ;; this one.  That function has to be the parent.
7129       (while (and (setq data (cdr data))
7130                   (not (< (gnus-data-level (car data)) level))))
7131       (and data (gnus-data-number (car data))))))
7132
7133 (defun gnus-unread-mark-p (mark)
7134   "Say whether MARK is the unread mark."
7135   (= mark gnus-unread-mark))
7136
7137 (defun gnus-read-mark-p (mark)
7138   "Say whether MARK is one of the marks that mark as read.
7139 This is all marks except unread, ticked, dormant, and expirable."
7140   (not (or (= mark gnus-unread-mark)
7141            (= mark gnus-ticked-mark)
7142            (= mark gnus-dormant-mark)
7143            (= mark gnus-expirable-mark))))
7144
7145 ;; Various summary mode internalish functions.
7146
7147 (defun gnus-mouse-pick-article (e)
7148   (interactive "e")
7149   (mouse-set-point e)
7150   (gnus-summary-next-page nil t))
7151
7152 (defun gnus-summary-setup-buffer (group)
7153   "Initialize summary buffer."
7154   (let ((buffer (concat "*Summary " group "*")))
7155     (if (get-buffer buffer)
7156         (progn
7157           (set-buffer buffer)
7158           (setq gnus-summary-buffer (current-buffer))
7159           (not gnus-newsgroup-prepared))
7160       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7161       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7162       (gnus-add-current-to-buffer-list)
7163       (gnus-summary-mode group)
7164       (when gnus-carpal
7165         (gnus-carpal-setup-buffer 'summary))
7166       (unless gnus-single-article-buffer
7167         (make-local-variable 'gnus-article-buffer)
7168         (make-local-variable 'gnus-original-article-buffer))
7169       (setq gnus-newsgroup-name group)
7170       t)))
7171
7172 (defun gnus-set-global-variables ()
7173   ;; Set the global equivalents of the summary buffer-local variables
7174   ;; to the latest values they had.  These reflect the summary buffer
7175   ;; that was in action when the last article was fetched.
7176   (when (eq major-mode 'gnus-summary-mode)
7177     (setq gnus-summary-buffer (current-buffer))
7178     (let ((name gnus-newsgroup-name)
7179           (marked gnus-newsgroup-marked)
7180           (unread gnus-newsgroup-unreads)
7181           (headers gnus-current-headers)
7182           (data gnus-newsgroup-data)
7183           (article-buffer gnus-article-buffer)
7184           (original gnus-original-article-buffer)
7185           (score-file gnus-current-score-file))
7186       (save-excursion
7187         (set-buffer gnus-group-buffer)
7188         (setq gnus-newsgroup-name name)
7189         (setq gnus-newsgroup-marked marked)
7190         (setq gnus-newsgroup-unreads unread)
7191         (setq gnus-current-headers headers)
7192         (setq gnus-newsgroup-data data)
7193         (setq gnus-article-buffer article-buffer)
7194         (setq gnus-original-article-buffer original)
7195         (setq gnus-current-score-file score-file)))))
7196
7197 (defun gnus-summary-last-article-p (&optional article)
7198   "Return whether ARTICLE is the last article in the buffer."
7199   (if (not (setq article (or article (gnus-summary-article-number))))
7200       t ; All non-existant numbers are the last article. :-)
7201     (cdr (gnus-data-find-list article))))
7202
7203 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7204   "Insert a dummy root in the summary buffer."
7205   (beginning-of-line)
7206   (add-text-properties
7207    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7208    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7209
7210 (defvar gnus-thread-indent-array nil)
7211 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7212 (defun gnus-make-thread-indent-array ()
7213   (let ((n 200))
7214     (if (and gnus-thread-indent-array
7215              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7216         nil
7217       (setq gnus-thread-indent-array (make-vector 201 "")
7218             gnus-thread-indent-array-level gnus-thread-indent-level)
7219       (while (>= n 0)
7220         (aset gnus-thread-indent-array n
7221               (make-string (* n gnus-thread-indent-level) ? ))
7222         (setq n (1- n))))))
7223
7224 (defun gnus-summary-insert-line
7225   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7226                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7227                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7228   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7229          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7230          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7231          (gnus-tmp-score-char
7232           (if (or (null gnus-summary-default-score)
7233                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7234                       gnus-summary-zcore-fuzz)) ? 
7235             (if (< gnus-tmp-score gnus-summary-default-score)
7236                 gnus-score-below-mark gnus-score-over-mark)))
7237          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7238                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7239                                   gnus-cached-mark)
7240                                  (gnus-tmp-replied gnus-replied-mark)
7241                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7242                                   gnus-saved-mark)
7243                                  (t gnus-unread-mark)))
7244          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7245          (gnus-tmp-name
7246           (cond
7247            ((string-match "(.+)" gnus-tmp-from)
7248             (substring gnus-tmp-from
7249                        (1+ (match-beginning 0)) (1- (match-end 0))))
7250            ((string-match "<[^>]+> *$" gnus-tmp-from)
7251             (let ((beg (match-beginning 0)))
7252               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7253                        (substring gnus-tmp-from (1+ (match-beginning 0))
7254                                   (1- (match-end 0))))
7255                   (substring gnus-tmp-from 0 beg))))
7256            (t gnus-tmp-from)))
7257          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7258          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7259          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7260          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7261          (buffer-read-only nil))
7262     (when (string= gnus-tmp-name "")
7263       (setq gnus-tmp-name gnus-tmp-from))
7264     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7265     (put-text-property
7266      (point)
7267      (progn (eval gnus-summary-line-format-spec) (point))
7268      'gnus-number gnus-tmp-number)
7269     (when (gnus-visual-p 'summary-highlight 'highlight)
7270       (forward-line -1)
7271       (run-hooks 'gnus-summary-update-hook)
7272       (forward-line 1))))
7273
7274 (defun gnus-summary-update-line (&optional dont-update)
7275   ;; Update summary line after change.
7276   (when (and gnus-summary-default-score
7277              (not gnus-summary-inhibit-highlight))
7278     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7279            (article (gnus-summary-article-number))
7280            (score (gnus-summary-article-score article)))
7281       (unless dont-update
7282         (if (and gnus-summary-mark-below
7283                  (< (gnus-summary-article-score)
7284                     gnus-summary-mark-below))
7285             ;; This article has a low score, so we mark it as read.
7286             (when (memq article gnus-newsgroup-unreads)
7287               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7288           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7289             ;; This article was previously marked as read on account
7290             ;; of a low score, but now it has risen, so we mark it as
7291             ;; unread.
7292             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7293         (gnus-summary-update-mark
7294          (if (or (null gnus-summary-default-score)
7295                  (<= (abs (- score gnus-summary-default-score))
7296                      gnus-summary-zcore-fuzz)) ? 
7297            (if (< score gnus-summary-default-score)
7298                gnus-score-below-mark gnus-score-over-mark)) 'score))
7299       ;; Do visual highlighting.
7300       (when (gnus-visual-p 'summary-highlight 'highlight)
7301         (run-hooks 'gnus-summary-update-hook)))))
7302
7303 (defvar gnus-tmp-new-adopts nil)
7304
7305 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7306   ;; Sum up all elements (and sub-elements) in a list.
7307   (let* ((number
7308           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7309           (cond
7310            ((and (consp thread) (cdr thread))
7311             (apply
7312              '+ 1 (mapcar
7313                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7314            ((null thread)
7315             1)
7316            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7317             1)
7318            (t 1))))
7319     (when (and level (zerop level) gnus-tmp-new-adopts)
7320       (incf number
7321             (apply '+ (mapcar
7322                        'gnus-summary-number-of-articles-in-thread
7323                        gnus-tmp-new-adopts))))
7324     (if char
7325         (if (> number 1) gnus-not-empty-thread-mark
7326           gnus-empty-thread-mark)
7327       number)))
7328
7329 (defun gnus-summary-set-local-parameters (group)
7330  "Go through the local params of GROUP and set all variable specs in that list."
7331   (let ((params (gnus-info-params (gnus-get-info group)))
7332         elem)
7333     (while params
7334       (setq elem (car params)
7335             params (cdr params))
7336       (and (consp elem)                 ; Has to be a cons.
7337            (consp (cdr elem))           ; The cdr has to be a list.
7338            (symbolp (car elem))         ; Has to be a symbol in there.
7339            (progn                       ; So we set it.
7340              (make-local-variable (car elem))
7341              (set (car elem) (eval (nth 1 elem))))))))
7342
7343 (defun gnus-summary-read-group
7344   (group &optional show-all no-article kill-buffer no-display)
7345   "Start reading news in newsgroup GROUP.
7346 If SHOW-ALL is non-nil, already read articles are also listed.
7347 If NO-ARTICLE is non-nil, no article is selected initially.
7348 If NO-DISPLAY, don't generate a summary buffer."
7349   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7350   (let* ((new-group (gnus-summary-setup-buffer group))
7351          (quit-config (gnus-group-quit-config group))
7352          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7353     (cond
7354      ;; This summary buffer exists already, so we just select it.
7355      ((not new-group)
7356       (gnus-set-global-variables)
7357       (when kill-buffer
7358         (gnus-kill-or-deaden-summary kill-buffer))
7359       (gnus-configure-windows 'summary 'force)
7360       (gnus-set-mode-line 'summary)
7361       (gnus-summary-position-point)
7362       (message "")
7363       t)
7364      ;; We couldn't select this group.
7365      ((null did-select)
7366       (when (and (eq major-mode 'gnus-summary-mode)
7367                  (not (equal (current-buffer) kill-buffer)))
7368         (kill-buffer (current-buffer))
7369         (if (not quit-config)
7370             (progn
7371               (set-buffer gnus-group-buffer)
7372               (gnus-group-jump-to-group group)
7373               (gnus-group-next-unread-group 1))
7374           (if (not (buffer-name (car quit-config)))
7375               (gnus-configure-windows 'group 'force)
7376             (set-buffer (car quit-config))
7377             (and (eq major-mode 'gnus-summary-mode)
7378                  (gnus-set-global-variables))
7379             (gnus-configure-windows (cdr quit-config)))))
7380       (gnus-message 3 "Can't select group")
7381       nil)
7382      ;; The user did a `C-g' while prompting for number of articles,
7383      ;; so we exit this group.
7384      ((eq did-select 'quit)
7385       (and (eq major-mode 'gnus-summary-mode)
7386            (not (equal (current-buffer) kill-buffer))
7387            (kill-buffer (current-buffer)))
7388       (when kill-buffer
7389         (gnus-kill-or-deaden-summary kill-buffer))
7390       (if (not quit-config)
7391           (progn
7392             (set-buffer gnus-group-buffer)
7393             (gnus-group-jump-to-group group)
7394             (gnus-group-next-unread-group 1)
7395             (gnus-configure-windows 'group 'force))
7396         (if (not (buffer-name (car quit-config)))
7397             (gnus-configure-windows 'group 'force)
7398           (set-buffer (car quit-config))
7399           (and (eq major-mode 'gnus-summary-mode)
7400                (gnus-set-global-variables))
7401           (gnus-configure-windows (cdr quit-config))))
7402       ;; Finally signal the quit.
7403       (signal 'quit nil))
7404      ;; The group was successfully selected.
7405      (t
7406       (gnus-set-global-variables)
7407       ;; Save the active value in effect when the group was entered.
7408       (setq gnus-newsgroup-active
7409             (gnus-copy-sequence
7410              (gnus-active gnus-newsgroup-name)))
7411       ;; You can change the summary buffer in some way with this hook.
7412       (run-hooks 'gnus-select-group-hook)
7413       ;; Set any local variables in the group parameters.
7414       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7415       (gnus-update-format-specifications)
7416       ;; Do score processing.
7417       (when gnus-use-scoring
7418         (gnus-possibly-score-headers))
7419       ;; Check whether to fill in the gaps in the threads.
7420       (when gnus-build-sparse-threads
7421         (gnus-build-sparse-threads))
7422       ;; Find the initial limit.
7423       (if show-all
7424           (let ((gnus-newsgroup-dormant nil))
7425             (gnus-summary-initial-limit show-all))
7426         (gnus-summary-initial-limit show-all))
7427       ;; Generate the summary buffer.
7428       (unless no-display
7429         (gnus-summary-prepare))
7430       (when gnus-use-trees
7431         (gnus-tree-open group)
7432         (setq gnus-summary-highlight-line-function
7433               'gnus-tree-highlight-article))
7434       ;; If the summary buffer is empty, but there are some low-scored
7435       ;; articles or some excluded dormants, we include these in the
7436       ;; buffer.
7437       (when (and (zerop (buffer-size))
7438                  (not no-display))
7439         (cond (gnus-newsgroup-dormant
7440                (gnus-summary-limit-include-dormant))
7441               ((and gnus-newsgroup-scored show-all)
7442                (gnus-summary-limit-include-expunged))))
7443       ;; Function `gnus-apply-kill-file' must be called in this hook.
7444       (run-hooks 'gnus-apply-kill-hook)
7445       (if (and (zerop (buffer-size))
7446                (not no-display))
7447           (progn
7448             ;; This newsgroup is empty.
7449             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7450             (gnus-message 6 "No unread news")
7451             (when kill-buffer
7452               (gnus-kill-or-deaden-summary kill-buffer))
7453             ;; Return nil from this function.
7454             nil)
7455         ;; Hide conversation thread subtrees.  We cannot do this in
7456         ;; gnus-summary-prepare-hook since kill processing may not
7457         ;; work with hidden articles.
7458         (and gnus-show-threads
7459              gnus-thread-hide-subtree
7460              (gnus-summary-hide-all-threads))
7461         ;; Show first unread article if requested.
7462         (if (and (not no-article)
7463                  (not no-display)
7464                  gnus-newsgroup-unreads
7465                  gnus-auto-select-first)
7466             (if (eq gnus-auto-select-first 'best)
7467                 (gnus-summary-best-unread-article)
7468               (gnus-summary-first-unread-article))
7469           ;; Don't select any articles, just move point to the first
7470           ;; article in the group.
7471           (goto-char (point-min))
7472           (gnus-summary-position-point)
7473           (gnus-set-mode-line 'summary)
7474           (gnus-configure-windows 'summary 'force))
7475         ;; If we are in async mode, we send some info to the backend.
7476         (when gnus-newsgroup-async
7477           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7478         (when kill-buffer
7479           (gnus-kill-or-deaden-summary kill-buffer))
7480         (when (get-buffer-window gnus-group-buffer t)
7481           ;; Gotta use windows, because recenter does wierd stuff if
7482           ;; the current buffer ain't the displayed window.
7483           (let ((owin (selected-window)))
7484             (select-window (get-buffer-window gnus-group-buffer t))
7485             (when (gnus-group-goto-group group)
7486               (recenter))
7487             (select-window owin))))
7488       ;; Mark this buffer as "prepared".
7489       (setq gnus-newsgroup-prepared t)
7490       t))))
7491
7492 (defun gnus-summary-prepare ()
7493   "Generate the summary buffer."
7494   (let ((buffer-read-only nil))
7495     (erase-buffer)
7496     (setq gnus-newsgroup-data nil
7497           gnus-newsgroup-data-reverse nil)
7498     (run-hooks 'gnus-summary-generate-hook)
7499     ;; Generate the buffer, either with threads or without.
7500     (when gnus-newsgroup-headers
7501       (gnus-summary-prepare-threads
7502        (if gnus-show-threads
7503            (gnus-sort-gathered-threads
7504             (funcall gnus-summary-thread-gathering-function
7505                      (gnus-sort-threads
7506                       (gnus-cut-threads (gnus-make-threads)))))
7507          ;; Unthreaded display.
7508          (gnus-sort-articles gnus-newsgroup-headers))))
7509     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7510     ;; Call hooks for modifying summary buffer.
7511     (goto-char (point-min))
7512     (run-hooks 'gnus-summary-prepare-hook)))
7513
7514 (defun gnus-gather-threads-by-subject (threads)
7515   "Gather threads by looking at Subject headers."
7516   (if (not gnus-summary-make-false-root)
7517       threads
7518     (let ((hashtb (gnus-make-hashtable 1023))
7519           (prev threads)
7520           (result threads)
7521           subject hthread whole-subject)
7522       (while threads
7523         (setq whole-subject (mail-header-subject (car (car threads))))
7524         (if (and gnus-summary-gather-exclude-subject
7525                  (string-match gnus-summary-gather-exclude-subject
7526                                whole-subject))
7527             () ; We don't want to do anything with this article.
7528           ;; We simplify the subject before looking it up in the
7529           ;; hash table.
7530           (setq subject
7531                 (cond
7532                  ;; Truncate the subject.
7533                  ((numberp gnus-summary-gather-subject-limit)
7534                   (setq subject (gnus-simplify-subject-re whole-subject))
7535                   (if (> (length subject) gnus-summary-gather-subject-limit)
7536                       (substring subject 0 gnus-summary-gather-subject-limit)
7537                     subject))
7538                  ;; Fuzzily simplify it.
7539                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7540                   (gnus-simplify-subject-fuzzy whole-subject))
7541                  ;; Just remove the leading "Re:".
7542                  (t
7543                   (gnus-simplify-subject-re whole-subject))))
7544
7545           (if (setq hthread (gnus-gethash subject hashtb))
7546               (progn
7547                 ;; We enter a dummy root into the thread, if we
7548                 ;; haven't done that already.
7549                 (unless (stringp (car (car hthread)))
7550                   (setcar hthread (list whole-subject (car hthread))))
7551                 ;; We add this new gathered thread to this gathered
7552                 ;; thread.
7553                 (setcdr (car hthread)
7554                         (nconc (cdr (car hthread)) (list (car threads))))
7555                 ;; Remove it from the list of threads.
7556                 (setcdr prev (cdr threads))
7557                 (setq threads prev))
7558             ;; Enter this thread into the hash table.
7559             (gnus-sethash subject threads hashtb)))
7560         (setq prev threads)
7561         (setq threads (cdr threads)))
7562       result)))
7563
7564 (defun gnus-summary-gather-threads-by-references (threads)
7565   "Gather threads by looking at References headers."
7566   (let ((idhashtb (gnus-make-hashtable 1023))
7567         (thhashtb (gnus-make-hashtable 1023))
7568         (prev threads)
7569         (result threads)
7570         ids references id gthread gid entered)
7571     (while threads
7572       (when (setq references (mail-header-references (caar threads)))
7573         (setq id (mail-header-id (caar threads)))
7574         (setq ids (gnus-split-references references))
7575         (setq entered nil)
7576         (while ids
7577           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7578               (progn
7579                 (gnus-sethash (car ids) id idhashtb)
7580                 (gnus-sethash id threads thhashtb))
7581             (setq gthread (gnus-gethash gid thhashtb))
7582             (unless entered
7583               ;; We enter a dummy root into the thread, if we
7584               ;; haven't done that already.
7585               (unless (stringp (caar gthread))
7586                 (setcar gthread (list (mail-header-subject (caar gthread))
7587                                       (car gthread))))
7588               ;; We add this new gathered thread to this gathered
7589               ;; thread.
7590               (setcdr (car gthread)
7591                       (nconc (cdar gthread) (list (car threads)))))
7592             ;; Add it into the thread hash table.
7593             (gnus-sethash id gthread thhashtb)
7594             (setq entered t)
7595             ;; Remove it from the list of threads.
7596             (setcdr prev (cdr threads))
7597             (setq threads prev))
7598           (setq ids (cdr ids))))
7599       (setq prev threads)
7600       (setq threads (cdr threads)))
7601     result))
7602
7603 (defun gnus-sort-gathered-threads (threads)
7604   "Sort subtreads inside each gathered thread by article number."
7605   (let ((result threads))
7606     (while threads
7607       (when (stringp (car (car threads)))
7608         (setcdr (car threads)
7609                 (sort (cdr (car threads)) 'gnus-thread-sort-by-number)))
7610       (setq threads (cdr threads)))
7611     result))
7612
7613 (defun gnus-make-threads ()
7614   "Go through the dependency hashtb and find the roots.  Return all threads."
7615   (let (threads)
7616     (mapatoms
7617      (lambda (refs)
7618        (unless (car (symbol-value refs))
7619          ;; These threads do not refer back to any other articles,
7620          ;; so they're roots.
7621          (setq threads (append (cdr (symbol-value refs)) threads))))
7622      gnus-newsgroup-dependencies)
7623     threads))
7624
7625 (defun gnus-build-sparse-threads ()
7626   (let ((headers gnus-newsgroup-headers)
7627         (deps gnus-newsgroup-dependencies)
7628         header references generation relations 
7629         cthread subject child end pthread relation)
7630     ;; First we create an alist of generations/relations, where 
7631     ;; generations is how much we trust the ralation, and the relation
7632     ;; is parent/child.
7633     (gnus-message 7 "Making sparse threads...")
7634     (save-excursion
7635       (nnheader-set-temp-buffer " *gnus sparse threads*")
7636       (while (setq header (pop headers))
7637         (when (and (setq references (mail-header-references header))
7638                    (not (string= references "")))
7639           (insert references)
7640           (setq child (downcase (mail-header-id header))
7641                 subject (mail-header-subject header))
7642           (setq generation 0)
7643           (while (search-backward ">" nil t)
7644             (setq end (1+ (point)))
7645             (when (search-backward "<" nil t)
7646               (push (list (incf generation) 
7647                           child (setq child (downcase
7648                                              (buffer-substring (point) end)))
7649                           subject)
7650                     relations)))
7651           (push (list (1+ generation) child nil subject) relations)
7652           (erase-buffer)))
7653       (kill-buffer (current-buffer)))
7654     ;; Sort over trustworthiness.
7655     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7656     (while (setq relation (pop relations))
7657       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7658                 (unless (car (symbol-value cthread))
7659                   ;; Make this article the parent of these threads.
7660                   (setcar (symbol-value cthread)
7661                           (vector gnus-reffed-article-number 
7662                                   (cadddr relation) 
7663                                   "" ""
7664                                   (cadr relation) 
7665                                   (or (caddr relation) "") 0 0 "")))
7666               (set cthread (list (vector gnus-reffed-article-number
7667                                          (cadddr relation) 
7668                                          "" "" (cadr relation) 
7669                                          (or (caddr relation) "") 0 0 ""))))
7670         (push gnus-reffed-article-number gnus-newsgroup-limit)
7671         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7672         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7673               gnus-newsgroup-reads)
7674         (decf gnus-reffed-article-number)
7675         ;; Make this new thread the child of its parent.
7676         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7677             (setcdr (symbol-value pthread)
7678                     (nconc (cdr (symbol-value pthread))
7679                            (list (symbol-value cthread))))
7680           (set pthread (list nil (symbol-value cthread))))))
7681     (gnus-message 7 "Making sparse threads...done")))
7682
7683 (defun gnus-build-old-threads ()
7684   ;; Look at all the articles that refer back to old articles, and
7685   ;; fetch the headers for the articles that aren't there.  This will
7686   ;; build complete threads - if the roots haven't been expired by the
7687   ;; server, that is.
7688   (let (id heads)
7689     (mapatoms
7690      (lambda (refs)
7691        (when (not (car (symbol-value refs)))
7692          (setq heads (cdr (symbol-value refs)))
7693          (while heads
7694            (if (memq (mail-header-number (car (car heads)))
7695                      gnus-newsgroup-dormant)
7696                (setq heads (cdr heads))
7697              (setq id (symbol-name refs))
7698              (while (and (setq id (gnus-build-get-header id))
7699                          (not (car (gnus-gethash
7700                                     id gnus-newsgroup-dependencies)))))
7701              (setq heads nil)))))
7702      gnus-newsgroup-dependencies)))
7703
7704 (defun gnus-build-get-header (id)
7705   ;; Look through the buffer of NOV lines and find the header to
7706   ;; ID.  Enter this line into the dependencies hash table, and return
7707   ;; the id of the parent article (if any).
7708   (let ((deps gnus-newsgroup-dependencies)
7709         found header)
7710     (prog1
7711         (save-excursion
7712           (set-buffer nntp-server-buffer)
7713           (goto-char (point-min))
7714           (while (and (not found) (search-forward id nil t))
7715             (beginning-of-line)
7716             (setq found (looking-at
7717                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7718                                  (regexp-quote id))))
7719             (or found (beginning-of-line 2)))
7720           (when found
7721             (let (ref)
7722               (beginning-of-line)
7723               (and
7724                (setq header (gnus-nov-parse-line
7725                              (read (current-buffer)) deps))
7726                (gnus-parent-id (mail-header-references header))))))
7727       (when header
7728         (let ((number (mail-header-number header)))
7729           (push number gnus-newsgroup-limit)
7730           (push header gnus-newsgroup-headers)
7731           (if (memq number gnus-newsgroup-unselected)
7732               (progn
7733                 (push number gnus-newsgroup-unreads)
7734                 (setq gnus-newsgroup-unselected
7735                       (delq number gnus-newsgroup-unselected)))
7736             (push number gnus-newsgroup-ancient)))))))
7737
7738 (defun gnus-summary-update-article (article &optional header)
7739   "Update ARTICLE in the summary buffer."
7740   (let ((id (mail-header-id (gnus-summary-article-header article)))
7741         (data (gnus-data-find article)))
7742     (setcar (gnus-id-to-thread id) nil)
7743     (gnus-summary-insert-subject id)
7744     ;; Set the (possibly) new article number in the data structure.
7745     (gnus-data-set-number data (gnus-id-to-article id))))
7746
7747 (defun gnus-rebuild-thread (id)
7748   "Rebuild the thread containing ID."
7749   (let ((dep gnus-newsgroup-dependencies)
7750         (buffer-read-only nil)
7751         current headers refs thread art data)
7752     (if (not gnus-show-threads)
7753         (setq thread (list (car (gnus-id-to-thread id))))
7754       ;; Get the thread this article is part of.
7755       (setq thread (gnus-remove-thread id)))
7756     (setq current (save-excursion
7757                     (and (zerop (forward-line -1))
7758                          (gnus-summary-article-number))))
7759     ;; If this is a gathered thread, we have to go some re-gathering.
7760     (when (stringp (car thread))
7761       (let ((subject (car thread))
7762             roots thr)
7763         (setq thread (cdr thread))
7764         (while thread
7765           (unless (memq (setq thr (gnus-id-to-thread
7766                                       (gnus-root-id
7767                                        (mail-header-id (car (car thread))))))
7768                         roots)
7769             (push thr roots))
7770           (setq thread (cdr thread)))
7771         ;; We now have all (unique) roots.
7772         (if (= (length roots) 1)
7773             ;; All the loose roots are now one solid root.
7774             (setq thread (car roots))
7775           (setq thread (cons subject (gnus-sort-threads roots))))))
7776     (let ((beg (point))
7777           threads)
7778       ;; We then insert this thread into the summary buffer.
7779       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7780         (gnus-summary-prepare-threads (list thread))
7781         (setq data (nreverse gnus-newsgroup-data))
7782         (setq threads gnus-newsgroup-threads))
7783       ;; We splice the new data into the data structure.
7784       (gnus-data-enter-list current data)
7785       (gnus-data-compute-positions)
7786       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7787
7788 (defun gnus-id-to-thread (id)
7789   "Return the (sub-)thread where ID appears."
7790   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7791
7792 (defun gnus-id-to-article (id)
7793   "Return the article number of ID."
7794   (let ((thread (gnus-id-to-thread id)))
7795     (when thread
7796       (mail-header-number (car thread)))))
7797
7798 (defun gnus-id-to-header (id)
7799   "Return the article headers of ID."
7800   (car (gnus-id-to-thread id)))
7801
7802 (defun gnus-article-displayed-root-p (article)
7803   "Say whether ARTICLE is a root(ish) article."
7804   (let ((level (gnus-summary-thread-level article))
7805         particle)
7806     (cond 
7807      ((null level) nil)
7808      ((zerop level) t)
7809      ((and (= 1 level)
7810            (null (setq particle (gnus-id-to-article
7811                                  (gnus-parent-id 
7812                                   (mail-header-references 
7813                                    (gnus-summary-article-header article))))))
7814            (null (gnus-summary-thread-level particle)))))))
7815
7816 (defun gnus-root-id (id)
7817   "Return the id of the root of the thread where ID appears."
7818   (let (last-id prev)
7819     (while (and id (setq prev (car (gnus-gethash
7820                                     (downcase id)
7821                                     gnus-newsgroup-dependencies))))
7822       (setq last-id id
7823             id (gnus-parent-id (mail-header-references prev))))
7824     last-id))
7825
7826 (defun gnus-remove-thread (id &optional dont-remove)
7827   "Remove the thread that has ID in it."
7828   (let ((dep gnus-newsgroup-dependencies)
7829         headers thread prev last-id)
7830     ;; First go up in this thread until we find the root.
7831     (setq last-id (gnus-root-id id))
7832     (setq headers (list (car (gnus-id-to-thread last-id))
7833                         (car (car (cdr (gnus-id-to-thread last-id))))))
7834     ;; We have now found the real root of this thread.  It might have
7835     ;; been gathered into some loose thread, so we have to search
7836     ;; through the threads to find the thread we wanted.
7837     (let ((threads gnus-newsgroup-threads)
7838           sub)
7839       (while threads
7840         (setq sub (car threads))
7841         (if (stringp (car sub))
7842             ;; This is a gathered threads, so we look at the roots
7843             ;; below it to find whether this article in in this
7844             ;; gathered root.
7845             (progn
7846               (setq sub (cdr sub))
7847               (while sub
7848                 (when (member (car (car sub)) headers)
7849                   (setq thread (car threads)
7850                         threads nil
7851                         sub nil))
7852                 (setq sub (cdr sub))))
7853           ;; It's an ordinary thread, so we check it.
7854           (when (eq (car sub) (car headers))
7855             (setq thread sub
7856                   threads nil)))
7857         (setq threads (cdr threads)))
7858       ;; If this article is in no thread, then it's a root.
7859       (if thread
7860           (unless dont-remove
7861             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7862         (setq thread (gnus-gethash (downcase last-id) dep)))
7863       (when thread
7864         (prog1
7865             thread ; We return this thread.
7866           (unless dont-remove
7867             (if (stringp (car thread))
7868                 (progn
7869                   ;; If we use dummy roots, then we have to remove the
7870                   ;; dummy root as well.
7871                   (when (eq gnus-summary-make-false-root 'dummy)
7872                     ;; Uhm.
7873                     )
7874                   (setq thread (cdr thread))
7875                   (while thread
7876                     (gnus-remove-thread-1 (car thread))
7877                     (setq thread (cdr thread))))
7878               (gnus-remove-thread-1 thread))))))))
7879
7880 (defun gnus-remove-thread-1 (thread)
7881   "Remove the thread THREAD recursively."
7882   (let ((number (mail-header-number (car thread)))
7883         pos)
7884     (when (setq pos (text-property-any
7885                      (point-min) (point-max) 'gnus-number number))
7886       (goto-char pos)
7887       (gnus-delete-line)
7888       (gnus-data-remove number))
7889     (setq thread (cdr thread))
7890     (while thread
7891       (gnus-remove-thread-1 (car thread))
7892       (setq thread (cdr thread)))))
7893
7894 (defun gnus-sort-threads (threads)
7895   "Sort THREADS."
7896   (if (not gnus-thread-sort-functions)
7897       threads
7898     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7899                     (car gnus-thread-sort-functions)
7900                   `(lambda (t1 t2)
7901                      ,(gnus-make-sort-function 
7902                        (reverse gnus-thread-sort-functions))))))
7903       (gnus-message 7 "Sorting threads...")
7904       (prog1
7905           (sort threads func)
7906         (gnus-message 7 "Sorting threads...done")))))
7907
7908 (defun gnus-sort-articles (articles)
7909   "Sort ARTICLES."
7910   (when gnus-article-sort-functions
7911     (let ((func (if (= 1 (length gnus-article-sort-functions))
7912                     (car gnus-article-sort-functions)
7913                   `(lambda (t1 t2)
7914                      ,(gnus-make-sort-function 
7915                        (reverse gnus-article-sort-functions))))))
7916       (gnus-message 7 "Sorting articles...")
7917       (prog1
7918           (setq gnus-newsgroup-headers (sort articles func))
7919         (gnus-message 7 "Sorting articles...done")))))
7920
7921 (defun gnus-make-sort-function (funs)
7922   "Return a composite sort condition based on the functions in FUNC."
7923   (if (cdr funs)
7924       `(or (,(car funs) t1 t2)
7925            (and (not (,(car funs) t2 t1))
7926                 ,(gnus-make-sort-function (cdr funs))))
7927     `(,(car funs) t1 t2)))
7928                  
7929 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7930 (defmacro gnus-thread-header (thread)
7931   ;; Return header of first article in THREAD.
7932   ;; Note that THREAD must never, ever be anything else than a variable -
7933   ;; using some other form will lead to serious barfage.
7934   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7935   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7936   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
7937         (vector thread) 2))
7938
7939 (defsubst gnus-article-sort-by-number (h1 h2)
7940   "Sort articles by article number."
7941   (< (mail-header-number h1)
7942      (mail-header-number h2)))
7943
7944 (defun gnus-thread-sort-by-number (h1 h2)
7945   "Sort threads by root article number."
7946   (gnus-article-sort-by-number
7947    (gnus-thread-header h1) (gnus-thread-header h2)))
7948
7949 (defsubst gnus-article-sort-by-author (h1 h2)
7950   "Sort articles by root author."
7951   (string-lessp
7952    (let ((extract (funcall
7953                    gnus-extract-address-components
7954                    (mail-header-from h1))))
7955      (or (car extract) (cdr extract)))
7956    (let ((extract (funcall
7957                    gnus-extract-address-components
7958                    (mail-header-from h2))))
7959      (or (car extract) (cdr extract)))))
7960
7961 (defun gnus-thread-sort-by-author (h1 h2)
7962   "Sort threads by root author."
7963   (gnus-article-sort-by-author
7964    (gnus-thread-header h1)  (gnus-thread-header h2)))
7965
7966 (defsubst gnus-article-sort-by-subject (h1 h2)
7967   "Sort articles by root subject."
7968   (string-lessp
7969    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
7970    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
7971
7972 (defun gnus-thread-sort-by-subject (h1 h2)
7973   "Sort threads by root subject."
7974   (gnus-article-sort-by-subject
7975    (gnus-thread-header h1) (gnus-thread-header h2)))
7976
7977 (defsubst gnus-article-sort-by-date (h1 h2)
7978   "Sort articles by root article date."
7979   (string-lessp
7980    (gnus-sortable-date (mail-header-date h1))
7981    (gnus-sortable-date (mail-header-date h2))))
7982
7983 (defun gnus-thread-sort-by-date (h1 h2)
7984   "Sort threads by root article date."
7985   (gnus-article-sort-by-date
7986    (gnus-thread-header h1) (gnus-thread-header h2)))
7987
7988 (defsubst gnus-article-sort-by-score (h1 h2)
7989   "Sort articles by root article score.
7990 Unscored articles will be counted as having a score of zero."
7991   (> (or (cdr (assq (mail-header-number h1)
7992                     gnus-newsgroup-scored))
7993          gnus-summary-default-score 0)
7994      (or (cdr (assq (mail-header-number h2)
7995                     gnus-newsgroup-scored))
7996          gnus-summary-default-score 0)))
7997
7998 (defun gnus-thread-sort-by-score (h1 h2)
7999   "Sort threads by root article score."
8000   (gnus-article-sort-by-score
8001    (gnus-thread-header h1) (gnus-thread-header h2)))
8002
8003 (defun gnus-thread-sort-by-total-score (h1 h2)
8004   "Sort threads by the sum of all scores in the thread.
8005 Unscored articles will be counted as having a score of zero."
8006   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8007
8008 (defun gnus-thread-total-score (thread)
8009   ;;  This function find the total score of THREAD.
8010   (if (consp thread)
8011       (if (stringp (car thread))
8012           (apply gnus-thread-score-function 0
8013                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8014         (gnus-thread-total-score-1 thread))
8015     (gnus-thread-total-score-1 (list thread))))
8016
8017 (defun gnus-thread-total-score-1 (root)
8018   ;; This function find the total score of the thread below ROOT.
8019   (setq root (car root))
8020   (apply gnus-thread-score-function
8021          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8022              gnus-summary-default-score 0)
8023          (mapcar 'gnus-thread-total-score
8024                  (cdr (gnus-gethash (downcase (mail-header-id root))
8025                                     gnus-newsgroup-dependencies)))))
8026
8027 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8028 (defvar gnus-tmp-prev-subject nil)
8029 (defvar gnus-tmp-false-parent nil)
8030 (defvar gnus-tmp-root-expunged nil)
8031 (defvar gnus-tmp-dummy-line nil)
8032
8033 (defun gnus-summary-prepare-threads (threads)
8034   "Prepare summary buffer from THREADS and indentation LEVEL.
8035 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8036 or a straight list of headers."
8037   (gnus-message 7 "Generating summary...")
8038
8039   (setq gnus-newsgroup-threads threads)
8040   (beginning-of-line)
8041
8042   (let ((gnus-tmp-level 0)
8043         (default-score (or gnus-summary-default-score 0))
8044         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8045         thread number subject stack state gnus-tmp-gathered beg-match
8046         new-roots gnus-tmp-new-adopts thread-end
8047         gnus-tmp-header gnus-tmp-unread
8048         gnus-tmp-replied gnus-tmp-subject-or-nil
8049         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8050         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8051         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8052
8053     (setq gnus-tmp-prev-subject nil)
8054
8055     (if (vectorp (car threads))
8056         ;; If this is a straight (sic) list of headers, then a
8057         ;; threaded summary display isn't required, so we just create
8058         ;; an unthreaded one.
8059         (gnus-summary-prepare-unthreaded threads)
8060
8061       ;; Do the threaded display.
8062
8063       (while (or threads stack gnus-tmp-new-adopts new-roots)
8064
8065         (if (and (= gnus-tmp-level 0)
8066                  (not (setq gnus-tmp-dummy-line nil))
8067                  (or (not stack)
8068                      (= (car (car stack)) 0))
8069                  (not gnus-tmp-false-parent)
8070                  (or gnus-tmp-new-adopts new-roots))
8071             (if gnus-tmp-new-adopts
8072                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8073                       thread (list (car gnus-tmp-new-adopts))
8074                       gnus-tmp-header (car (car thread))
8075                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8076               (if new-roots
8077                   (setq thread (list (car new-roots))
8078                         gnus-tmp-header (car (car thread))
8079                         new-roots (cdr new-roots))))
8080
8081           (if threads
8082               ;; If there are some threads, we do them before the
8083               ;; threads on the stack.
8084               (setq thread threads
8085                     gnus-tmp-header (car (car thread)))
8086             ;; There were no current threads, so we pop something off
8087             ;; the stack.
8088             (setq state (car stack)
8089                   gnus-tmp-level (car state)
8090                   thread (cdr state)
8091                   stack (cdr stack)
8092                   gnus-tmp-header (car (car thread)))))
8093
8094         (setq gnus-tmp-false-parent nil)
8095         (setq gnus-tmp-root-expunged nil)
8096         (setq thread-end nil)
8097
8098         (if (stringp gnus-tmp-header)
8099             ;; The header is a dummy root.
8100             (cond
8101              ((eq gnus-summary-make-false-root 'adopt)
8102               ;; We let the first article adopt the rest.
8103               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8104                                                (cdr (cdr (car thread)))))
8105               (setq gnus-tmp-gathered
8106                     (nconc (mapcar
8107                             (lambda (h) (mail-header-number (car h)))
8108                             (cdr (cdr (car thread))))
8109                            gnus-tmp-gathered))
8110               (setq thread (cons (list (car (car thread))
8111                                        (car (cdr (car thread))))
8112                                  (cdr thread)))
8113               (setq gnus-tmp-level -1
8114                     gnus-tmp-false-parent t))
8115              ((eq gnus-summary-make-false-root 'empty)
8116               ;; We print adopted articles with empty subject fields.
8117               (setq gnus-tmp-gathered
8118                     (nconc (mapcar
8119                             (lambda (h) (mail-header-number (car h)))
8120                             (cdr (cdr (car thread))))
8121                            gnus-tmp-gathered))
8122               (setq gnus-tmp-level -1))
8123              ((eq gnus-summary-make-false-root 'dummy)
8124               ;; We remember that we probably want to output a dummy
8125               ;; root.
8126               (setq gnus-tmp-dummy-line gnus-tmp-header)
8127               (setq gnus-tmp-prev-subject gnus-tmp-header))
8128              (t
8129               ;; We do not make a root for the gathered
8130               ;; sub-threads at all.
8131               (setq gnus-tmp-level -1)))
8132
8133           (setq number (mail-header-number gnus-tmp-header)
8134                 subject (mail-header-subject gnus-tmp-header))
8135
8136           (cond
8137            ;; If the thread has changed subject, we might want to make
8138            ;; this subthread into a root.
8139            ((and (null gnus-thread-ignore-subject)
8140                  (not (zerop gnus-tmp-level))
8141                  gnus-tmp-prev-subject
8142                  (not (inline
8143                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8144             (setq new-roots (nconc new-roots (list (car thread)))
8145                   thread-end t
8146                   gnus-tmp-header nil))
8147            ;; If the article lies outside the current limit,
8148            ;; then we do not display it.
8149            ((and (not (memq number gnus-newsgroup-limit))
8150                  (not gnus-tmp-dummy-line))
8151             (setq gnus-tmp-gathered
8152                   (nconc (mapcar
8153                           (lambda (h) (mail-header-number (car h)))
8154                           (cdr (car thread)))
8155                          gnus-tmp-gathered))
8156             (setq gnus-tmp-new-adopts (if (cdr (car thread))
8157                                           (append gnus-tmp-new-adopts
8158                                                   (cdr (car thread)))
8159                                         gnus-tmp-new-adopts)
8160                   thread-end t
8161                   gnus-tmp-header nil)
8162             (when (zerop gnus-tmp-level)
8163               (setq gnus-tmp-root-expunged t)))
8164            ;; Perhaps this article is to be marked as read?
8165            ((and gnus-summary-mark-below
8166                  (< (or (cdr (assq number gnus-newsgroup-scored))
8167                         default-score)
8168                     gnus-summary-mark-below))
8169             (setq gnus-newsgroup-unreads
8170                   (delq number gnus-newsgroup-unreads))
8171             (if gnus-newsgroup-auto-expire
8172                 (push number gnus-newsgroup-expirable)
8173               (push (cons number gnus-low-score-mark)
8174                     gnus-newsgroup-reads))))
8175
8176           (when gnus-tmp-header
8177             ;; We may have an old dummy line to output before this
8178             ;; article.
8179             (when gnus-tmp-dummy-line
8180               (gnus-summary-insert-dummy-line
8181                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8182               (setq gnus-tmp-dummy-line nil))
8183
8184             ;; Compute the mark.
8185             (setq
8186              gnus-tmp-unread
8187              (cond
8188               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8189               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8190               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8191               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8192               (t (or (cdr (assq number gnus-newsgroup-reads))
8193                      gnus-ancient-mark))))
8194
8195             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8196                                   gnus-tmp-header gnus-tmp-level)
8197                   gnus-newsgroup-data)
8198
8199             ;; Actually insert the line.
8200             (setq
8201              gnus-tmp-subject-or-nil
8202              (cond
8203               ((and gnus-thread-ignore-subject
8204                     gnus-tmp-prev-subject
8205                     (not (inline (gnus-subject-equal
8206                                   gnus-tmp-prev-subject subject))))
8207                subject)
8208               ((zerop gnus-tmp-level)
8209                (if (and (eq gnus-summary-make-false-root 'empty)
8210                         (memq number gnus-tmp-gathered)
8211                         gnus-tmp-prev-subject
8212                         (inline (gnus-subject-equal
8213                                  gnus-tmp-prev-subject subject)))
8214                    gnus-summary-same-subject
8215                  subject))
8216               (t gnus-summary-same-subject)))
8217             (if (and (eq gnus-summary-make-false-root 'adopt)
8218                      (= gnus-tmp-level 1)
8219                      (memq number gnus-tmp-gathered))
8220                 (setq gnus-tmp-opening-bracket ?\<
8221                       gnus-tmp-closing-bracket ?\>)
8222               (setq gnus-tmp-opening-bracket ?\[
8223                     gnus-tmp-closing-bracket ?\]))
8224             (setq
8225              gnus-tmp-indentation
8226              (aref gnus-thread-indent-array gnus-tmp-level)
8227              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8228              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8229                                 gnus-summary-default-score 0)
8230              gnus-tmp-score-char
8231              (if (or (null gnus-summary-default-score)
8232                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8233                          gnus-summary-zcore-fuzz)) ? 
8234                (if (< gnus-tmp-score gnus-summary-default-score)
8235                    gnus-score-below-mark gnus-score-over-mark))
8236              gnus-tmp-replied
8237              (cond ((memq number gnus-newsgroup-processable)
8238                     gnus-process-mark)
8239                    ((memq number gnus-newsgroup-cached)
8240                     gnus-cached-mark)
8241                    ((memq number gnus-newsgroup-replied)
8242                     gnus-replied-mark)
8243                    (t gnus-unread-mark))
8244              gnus-tmp-from (mail-header-from gnus-tmp-header)
8245              gnus-tmp-name
8246              (cond
8247               ((string-match "(.+)" gnus-tmp-from)
8248                (substring gnus-tmp-from
8249                           (1+ (match-beginning 0)) (1- (match-end 0))))
8250               ((string-match "<[^>]+> *$" gnus-tmp-from)
8251                (setq beg-match (match-beginning 0))
8252                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8253                         (substring gnus-tmp-from (1+ (match-beginning 0))
8254                                    (1- (match-end 0))))
8255                    (substring gnus-tmp-from 0 beg-match)))
8256               (t gnus-tmp-from)))
8257             (when (string= gnus-tmp-name "")
8258               (setq gnus-tmp-name gnus-tmp-from))
8259             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8260             (put-text-property
8261              (point)
8262              (progn (eval gnus-summary-line-format-spec) (point))
8263              'gnus-number number)
8264             (when gnus-visual-p
8265               (forward-line -1)
8266               (run-hooks 'gnus-summary-update-hook)
8267               (forward-line 1))
8268
8269             (setq gnus-tmp-prev-subject subject)))
8270
8271         (when (nth 1 thread)
8272           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8273         (incf gnus-tmp-level)
8274         (setq threads (if thread-end nil (cdr (car thread))))
8275         (unless threads
8276           (setq gnus-tmp-level 0)))))
8277   (gnus-message 7 "Generating summary...done"))
8278
8279 (defun gnus-summary-prepare-unthreaded (headers)
8280   "Generate an unthreaded summary buffer based on HEADERS."
8281   (let (header number mark)
8282
8283     (while headers
8284       (setq header (car headers)
8285             headers (cdr headers)
8286             number (mail-header-number header))
8287
8288       ;; We may have to root out some bad articles...
8289       (when (memq number gnus-newsgroup-limit)
8290         (when (and gnus-summary-mark-below
8291                    (< (or (cdr (assq number gnus-newsgroup-scored))
8292                           gnus-summary-default-score 0)
8293                       gnus-summary-mark-below))
8294           (setq gnus-newsgroup-unreads
8295                 (delq number gnus-newsgroup-unreads))
8296           (if gnus-newsgroup-auto-expire
8297               (push number gnus-newsgroup-expirable)
8298             (push (cons number gnus-low-score-mark)
8299                   gnus-newsgroup-reads)))
8300
8301         (setq mark
8302               (cond
8303                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8304                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8305                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8306                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8307                (t (or (cdr (assq number gnus-newsgroup-reads))
8308                       gnus-ancient-mark))))
8309         (setq gnus-newsgroup-data
8310               (cons (gnus-data-make number mark (1+ (point)) header 0)
8311                     gnus-newsgroup-data))
8312         (gnus-summary-insert-line
8313          header 0 nil mark (memq number gnus-newsgroup-replied)
8314          (memq number gnus-newsgroup-expirable)
8315          (mail-header-subject header) nil
8316          (cdr (assq number gnus-newsgroup-scored))
8317          (memq number gnus-newsgroup-processable))))))
8318
8319 (defun gnus-select-newsgroup (group &optional read-all)
8320   "Select newsgroup GROUP.
8321 If READ-ALL is non-nil, all articles in the group are selected."
8322   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8323          (info (nth 2 entry))
8324          articles fetched-articles cached)
8325
8326     (or (gnus-check-server
8327          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8328         (error "Couldn't open server"))
8329
8330     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8331         (gnus-activate-group group) ; Or we can activate it...
8332         (progn ; Or we bug out.
8333           (kill-buffer (current-buffer))
8334           (error "Couldn't request group %s: %s"
8335                  group (gnus-status-message group))))
8336
8337     (setq gnus-newsgroup-name group)
8338     (setq gnus-newsgroup-unselected nil)
8339     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8340
8341     (and gnus-asynchronous
8342          (gnus-check-backend-function
8343           'request-asynchronous gnus-newsgroup-name)
8344          (setq gnus-newsgroup-async
8345                (gnus-request-asynchronous gnus-newsgroup-name)))
8346
8347     ;; Adjust and set lists of article marks.
8348     (when info
8349       (gnus-adjust-marked-articles info))
8350
8351     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8352     (when (gnus-virtual-group-p group)
8353       (setq cached gnus-newsgroup-cached))
8354
8355     (setq gnus-newsgroup-unreads
8356           (gnus-set-difference
8357            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8358            gnus-newsgroup-dormant))
8359
8360     (setq gnus-newsgroup-processable nil)
8361
8362     (setq articles (gnus-articles-to-read group read-all))
8363
8364     (cond
8365      ((null articles)
8366       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8367       'quit)
8368      ((eq articles 0) nil)
8369      (t
8370       ;; Init the dependencies hash table.
8371       (setq gnus-newsgroup-dependencies
8372             (gnus-make-hashtable (length articles)))
8373       ;; Retrieve the headers and read them in.
8374       (gnus-message 5 "Fetching headers...")
8375       (setq gnus-newsgroup-headers
8376             (if (eq 'nov
8377                     (setq gnus-headers-retrieved-by
8378                           (gnus-retrieve-headers
8379                            articles gnus-newsgroup-name
8380                            ;; We might want to fetch old headers, but
8381                            ;; not if there is only 1 article.
8382                            (and gnus-fetch-old-headers
8383                                 (or (and
8384                                      (not (eq gnus-fetch-old-headers 'some))
8385                                      (not (numberp gnus-fetch-old-headers)))
8386                                     (> (length articles) 1))))))
8387                 (gnus-get-newsgroup-headers-xover articles)
8388               (gnus-get-newsgroup-headers)))
8389       (gnus-message 5 "Fetching headers...done")
8390
8391       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8392       (when cached
8393         (setq gnus-newsgroup-cached cached))
8394
8395       ;; Set the initial limit.
8396       (setq gnus-newsgroup-limit (copy-sequence articles))
8397       ;; Remove canceled articles from the list of unread articles.
8398       (setq gnus-newsgroup-unreads
8399             (gnus-set-sorted-intersection
8400              gnus-newsgroup-unreads
8401              (setq fetched-articles
8402                    (mapcar (lambda (headers) (mail-header-number headers))
8403                            gnus-newsgroup-headers))))
8404       ;; Removed marked articles that do not exist.
8405       (gnus-update-missing-marks
8406        (gnus-sorted-complement fetched-articles articles))
8407       ;; We might want to build some more threads first.
8408       (and gnus-fetch-old-headers
8409            (eq gnus-headers-retrieved-by 'nov)
8410            (gnus-build-old-threads))
8411       ;; Check whether auto-expire is to be done in this group.
8412       (setq gnus-newsgroup-auto-expire
8413             (gnus-group-auto-expirable-p group))
8414       ;; First and last article in this newsgroup.
8415       (and gnus-newsgroup-headers
8416            (setq gnus-newsgroup-begin
8417                  (mail-header-number (car gnus-newsgroup-headers)))
8418            (setq gnus-newsgroup-end
8419                  (mail-header-number
8420                   (gnus-last-element gnus-newsgroup-headers))))
8421       (setq gnus-reffed-article-number -1)
8422       ;; GROUP is successfully selected.
8423       (or gnus-newsgroup-headers t)))))
8424
8425 (defun gnus-articles-to-read (group read-all)
8426   ;; Find out what articles the user wants to read.
8427   (let* ((articles
8428           ;; Select all articles if `read-all' is non-nil, or if there
8429           ;; are no unread articles.
8430           (if (or read-all
8431                   (and (zerop (length gnus-newsgroup-marked))
8432                        (zerop (length gnus-newsgroup-unreads))))
8433               (gnus-uncompress-range (gnus-active group))
8434             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8435                           (copy-sequence gnus-newsgroup-unreads))
8436                   '<)))
8437          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8438          (scored (length scored-list))
8439          (number (length articles))
8440          (marked (+ (length gnus-newsgroup-marked)
8441                     (length gnus-newsgroup-dormant)))
8442          (select
8443           (cond
8444            ((numberp read-all)
8445             read-all)
8446            (t
8447             (condition-case ()
8448                 (cond
8449                  ((and (or (<= scored marked) (= scored number))
8450                        (numberp gnus-large-newsgroup)
8451                        (> number gnus-large-newsgroup))
8452                   (let ((input
8453                          (read-string
8454                           (format
8455                            "How many articles from %s (default %d): "
8456                            gnus-newsgroup-name number))))
8457                     (if (string-match "^[ \t]*$" input) number input)))
8458                  ((and (> scored marked) (< scored number))
8459                   (let ((input
8460                          (read-string
8461                           (format "%s %s (%d scored, %d total): "
8462                                   "How many articles from"
8463                                   group scored number))))
8464                     (if (string-match "^[ \t]*$" input)
8465                         number input)))
8466                  (t number))
8467               (quit nil))))))
8468     (setq select (if (stringp select) (string-to-number select) select))
8469     (if (or (null select) (zerop select))
8470         select
8471       (if (and (not (zerop scored)) (<= (abs select) scored))
8472           (progn
8473             (setq articles (sort scored-list '<))
8474             (setq number (length articles)))
8475         (setq articles (copy-sequence articles)))
8476
8477       (if (< (abs select) number)
8478           (if (< select 0)
8479               ;; Select the N oldest articles.
8480               (setcdr (nthcdr (1- (abs select)) articles) nil)
8481             ;; Select the N most recent articles.
8482             (setq articles (nthcdr (- number select) articles))))
8483       (setq gnus-newsgroup-unselected
8484             (gnus-sorted-intersection
8485              gnus-newsgroup-unreads
8486              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8487       articles)))
8488
8489 (defun gnus-killed-articles (killed articles)
8490   (let (out)
8491     (while articles
8492       (if (inline (gnus-member-of-range (car articles) killed))
8493           (setq out (cons (car articles) out)))
8494       (setq articles (cdr articles)))
8495     out))
8496
8497 (defun gnus-uncompress-marks (marks)
8498   "Uncompress the mark ranges in MARKS."
8499   (let ((uncompressed '(score bookmark))
8500         out)
8501     (while marks
8502       (if (memq (caar marks) uncompressed)
8503           (push (car marks) out)
8504         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8505       (setq marks (cdr marks)))
8506     out))
8507
8508 (defun gnus-adjust-marked-articles (info)
8509   "Set all article lists and remove all marks that are no longer legal."
8510   (let* ((marked-lists (gnus-info-marks info))
8511          (active (gnus-active (gnus-info-group info)))
8512          (min (car active))
8513          (max (cdr active))
8514          (types gnus-article-mark-lists)
8515          (uncompressed '(score bookmark))
8516          marks var articles article mark)
8517
8518     (while marked-lists
8519       (setq marks (pop marked-lists))
8520       (set (setq var (intern (format "gnus-newsgroup-%s"
8521                                      (car (rassq (setq mark (car marks))
8522                                                  types)))))
8523            (if (memq (car marks) uncompressed) (cdr marks)
8524              (gnus-uncompress-range (cdr marks))))
8525
8526       (setq articles (symbol-value var))
8527
8528       ;; All articles have to be subsets of the active articles.
8529       (cond
8530        ;; Adjust "simple" lists.
8531        ((memq mark '(tick dormant expirable reply killed save))
8532         (while articles
8533           (when (or (< (setq article (pop articles)) min) (> article max))
8534             (set var (delq article (symbol-value var))))))
8535        ;; Adjust assocs.
8536        ((memq mark '(score bookmark))
8537         (while articles
8538           (when (or (< (car (setq article (pop articles))) min)
8539                     (> (car article) max))
8540             (set var (delq article (symbol-value var))))))))))
8541
8542 (defun gnus-update-missing-marks (missing)
8543   "Go through the list of MISSING articles and remove them mark lists."
8544   (when missing
8545     (let ((types gnus-article-mark-lists)
8546           var m)
8547       ;; Go through all types.
8548       (while types
8549         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8550         (when (symbol-value var)
8551           ;; This list has articles.  So we delete all missing articles
8552           ;; from it.
8553           (setq m missing)
8554           (while m
8555             (set var (delq (pop m) (symbol-value var)))))))))
8556
8557 (defun gnus-update-marks ()
8558   "Enter the various lists of marked articles into the newsgroup info list."
8559   (let ((types gnus-article-mark-lists)
8560         (info (gnus-get-info gnus-newsgroup-name))
8561         (uncompressed '(score bookmark killed))
8562         var type list newmarked symbol)
8563     (when info
8564       ;; Add all marks lists that are non-nil to the list of marks lists.
8565       (while types
8566         (setq type (pop types))
8567         (when (setq list (symbol-value
8568                           (setq symbol
8569                                 (intern (format "gnus-newsgroup-%s"
8570                                                 (car type))))))
8571           (push (cons (cdr type)
8572                       (if (memq (cdr type) uncompressed) list
8573                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8574                 newmarked)))
8575
8576       ;; Enter these new marks into the info of the group.
8577       (if (nthcdr 3 info)
8578           (setcar (nthcdr 3 info) newmarked)
8579         ;; Add the marks lists to the end of the info.
8580         (when newmarked
8581           (setcdr (nthcdr 2 info) (list newmarked))))
8582
8583       ;; Cut off the end of the info if there's nothing else there.
8584       (let ((i 5))
8585         (while (and (> i 2)
8586                     (not (nth i info)))
8587           (when (nthcdr (decf i) info)
8588             (setcdr (nthcdr i info) nil)))))))
8589
8590 (defun gnus-add-marked-articles (group type articles &optional info force)
8591   ;; Add ARTICLES of TYPE to the info of GROUP.
8592   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8593   ;; add, but replace marked articles of TYPE with ARTICLES.
8594   (let ((info (or info (gnus-get-info group)))
8595         (uncompressed '(score bookmark killed))
8596         marked m)
8597     (or (not info)
8598         (and (not (setq marked (nthcdr 3 info)))
8599              (setcdr (nthcdr 2 info)
8600                      (list (list (cons type (gnus-compress-sequence
8601                                              articles t))))))
8602         (and (not (setq m (assq type (car marked))))
8603              (setcar marked
8604                      (cons (cons type (gnus-compress-sequence articles t) )
8605                            (car marked))))
8606         (if force
8607             (setcdr m (gnus-compress-sequence articles t))
8608           (setcdr m (gnus-compress-sequence
8609                      (sort (nconc (gnus-uncompress-range m)
8610                                   (copy-sequence articles)) '<) t))))))
8611
8612 (defun gnus-set-mode-line (where)
8613   "This function sets the mode line of the article or summary buffers.
8614 If WHERE is `summary', the summary mode line format will be used."
8615   ;; Is this mode line one we keep updated?
8616   (when (memq where gnus-updated-mode-lines)
8617     (let (mode-string)
8618       (save-excursion
8619         ;; We evaluate this in the summary buffer since these
8620         ;; variables are buffer-local to that buffer.
8621         (set-buffer gnus-summary-buffer)
8622         ;; We bind all these variables that are used in the `eval' form
8623         ;; below.
8624         (let* ((mformat (symbol-value
8625                          (intern
8626                           (format "gnus-%s-mode-line-format-spec" where))))
8627                (gnus-tmp-group-name gnus-newsgroup-name)
8628                (gnus-tmp-article-number (or gnus-current-article 0))
8629                (gnus-tmp-unread gnus-newsgroup-unreads)
8630                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8631                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8632                (gnus-tmp-unread-and-unselected
8633                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8634                             (zerop gnus-tmp-unselected)) "")
8635                       ((zerop gnus-tmp-unselected)
8636                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8637                       (t (format "{%d(+%d) more}"
8638                                  gnus-tmp-unread-and-unticked
8639                                  gnus-tmp-unselected))))
8640                (gnus-tmp-subject
8641                 (if (and gnus-current-headers
8642                          (vectorp gnus-current-headers))
8643                     (mail-header-subject gnus-current-headers) ""))
8644                max-len
8645                gnus-tmp-header);; passed as argument to any user-format-funcs
8646           (setq mode-string (eval mformat))
8647           (setq max-len (max 4 (if gnus-mode-non-string-length
8648                                    (- (frame-width)
8649                                       gnus-mode-non-string-length)
8650                                  (length mode-string))))
8651           ;; We might have to chop a bit of the string off...
8652           (when (> (length mode-string) max-len)
8653             (setq mode-string
8654                   (concat (gnus-truncate-string mode-string (- max-len 3))
8655                           "...")))
8656           ;; Pad the mode string a bit.
8657           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8658       ;; Update the mode line.
8659       (setq mode-line-buffer-identification (list mode-string))
8660       (set-buffer-modified-p t))))
8661
8662 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8663   "Go through the HEADERS list and add all Xrefs to a hash table.
8664 The resulting hash table is returned, or nil if no Xrefs were found."
8665   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8666          (virtual (gnus-virtual-group-p from-newsgroup))
8667          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8668          (xref-hashtb (make-vector 63 0))
8669          start group entry number xrefs header)
8670     (while headers
8671       (setq header (pop headers))
8672       (when (and (setq xrefs (mail-header-xref header))
8673                  (not (memq (setq number (mail-header-number header))
8674                             unreads)))
8675         (setq start 0)
8676         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8677           (setq start (match-end 0))
8678           (setq group (concat prefix (substring xrefs (match-beginning 1)
8679                                                 (match-end 1))))
8680           (setq number
8681                 (string-to-int (substring xrefs (match-beginning 2)
8682                                           (match-end 2))))
8683           (if (setq entry (gnus-gethash group xref-hashtb))
8684               (setcdr entry (cons number (cdr entry)))
8685             (gnus-sethash group (cons number nil) xref-hashtb)))))
8686     (and start xref-hashtb)))
8687
8688 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8689   "Look through all the headers and mark the Xrefs as read."
8690   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8691         name entry info xref-hashtb idlist method nth4)
8692     (save-excursion
8693       (set-buffer gnus-group-buffer)
8694       (when (setq xref-hashtb
8695                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8696         (mapatoms
8697          (lambda (group)
8698            (unless (string= from-newsgroup (setq name (symbol-name group)))
8699              (setq idlist (symbol-value group))
8700              ;; Dead groups are not updated.
8701              (and (prog1
8702                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8703                             info (nth 2 entry))
8704                     (if (stringp (setq nth4 (gnus-info-method info)))
8705                         (setq nth4 (gnus-server-to-method nth4))))
8706                   ;; Only do the xrefs if the group has the same
8707                   ;; select method as the group we have just read.
8708                   (or (gnus-methods-equal-p
8709                        nth4 (gnus-find-method-for-group from-newsgroup))
8710                       virtual
8711                       (equal nth4 (setq method (gnus-find-method-for-group
8712                                                 from-newsgroup)))
8713                       (and (equal (car nth4) (car method))
8714                            (equal (nth 1 nth4) (nth 1 method))))
8715                   gnus-use-cross-reference
8716                   (or (not (eq gnus-use-cross-reference t))
8717                       virtual
8718                       ;; Only do cross-references on subscribed
8719                       ;; groups, if that is what is wanted.
8720                       (<= (gnus-info-level info) gnus-level-subscribed))
8721                   (gnus-group-make-articles-read name idlist))))
8722          xref-hashtb)))))
8723
8724 (defun gnus-group-make-articles-read (group articles)
8725   (let* ((num 0)
8726          (entry (gnus-gethash group gnus-newsrc-hashtb))
8727          (info (nth 2 entry))
8728          (active (gnus-active group))
8729          range)
8730     ;; First peel off all illegal article numbers.
8731     (if active
8732         (let ((ids articles)
8733               id first)
8734           (while ids
8735             (setq id (car ids))
8736             (if (and first (> id (cdr active)))
8737                 (progn
8738                   ;; We'll end up in this situation in one particular
8739                   ;; obscure situation.  If you re-scan a group and get
8740                   ;; a new article that is cross-posted to a different
8741                   ;; group that has not been re-scanned, you might get
8742                   ;; crossposted article that has a higher number than
8743                   ;; Gnus believes possible.  So we re-activate this
8744                   ;; group as well.  This might mean doing the
8745                   ;; crossposting thingy will *increase* the number
8746                   ;; of articles in some groups.  Tsk, tsk.
8747                   (setq active (or (gnus-activate-group group) active))))
8748             (if (or (> id (cdr active))
8749                     (< id (car active)))
8750                 (setq articles (delq id articles)))
8751             (setq ids (cdr ids)))))
8752     ;; If the read list is nil, we init it.
8753     (and active
8754          (null (gnus-info-read info))
8755          (> (car active) 1)
8756          (gnus-info-set-read info (cons 1 (1- (car active)))))
8757     ;; Then we add the read articles to the range.
8758     (gnus-info-set-read
8759      info
8760      (setq range
8761            (gnus-add-to-range
8762             (gnus-info-read info) (setq articles (sort articles '<)))))
8763     ;; Then we have to re-compute how many unread
8764     ;; articles there are in this group.
8765     (if active
8766         (progn
8767           (cond
8768            ((not range)
8769             (setq num (- (1+ (cdr active)) (car active))))
8770            ((not (listp (cdr range)))
8771             (setq num (- (cdr active) (- (1+ (cdr range))
8772                                          (car range)))))
8773            (t
8774             (while range
8775               (if (numberp (car range))
8776                   (setq num (1+ num))
8777                 (setq num (+ num (- (1+ (cdr (car range)))
8778                                     (car (car range))))))
8779               (setq range (cdr range)))
8780             (setq num (- (cdr active) num))))
8781           ;; Update the number of unread articles.
8782           (setcar entry num)
8783           ;; Update the group buffer.
8784           (gnus-group-update-group group t)))))
8785
8786 (defun gnus-methods-equal-p (m1 m2)
8787   (let ((m1 (or m1 gnus-select-method))
8788         (m2 (or m2 gnus-select-method)))
8789     (or (equal m1 m2)
8790         (and (eq (car m1) (car m2))
8791              (or (not (memq 'address (assoc (symbol-name (car m1))
8792                                             gnus-valid-select-methods)))
8793                  (equal (nth 1 m1) (nth 1 m2)))))))
8794
8795 (defsubst gnus-header-value ()
8796   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8797
8798 (defvar gnus-newsgroup-none-id 0)
8799
8800 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8801   (let ((cur nntp-server-buffer)
8802         (dependencies
8803          (or dependencies
8804              (save-excursion (set-buffer gnus-summary-buffer)
8805                              gnus-newsgroup-dependencies)))
8806         headers id id-dep ref-dep end ref)
8807     (save-excursion
8808       (set-buffer nntp-server-buffer)
8809       (let ((case-fold-search t)
8810             in-reply-to header number p lines)
8811         (goto-char (point-min))
8812         ;; Search to the beginning of the next header.  Error messages
8813         ;; do not begin with 2 or 3.
8814         (while (re-search-forward "^[23][0-9]+ " nil t)
8815           (setq id nil
8816                 ref nil)
8817           ;; This implementation of this function, with nine
8818           ;; search-forwards instead of the one re-search-forward and
8819           ;; a case (which basically was the old function) is actually
8820           ;; about twice as fast, even though it looks messier.  You
8821           ;; can't have everything, I guess.  Speed and elegance
8822           ;; doesn't always go hand in hand.
8823           (setq
8824            header
8825            (vector
8826             ;; Number.
8827             (prog1
8828                 (read cur)
8829               (end-of-line)
8830               (setq p (point))
8831               (narrow-to-region (point)
8832                                 (or (and (search-forward "\n.\n" nil t)
8833                                          (- (point) 2))
8834                                     (point))))
8835             ;; Subject.
8836             (progn
8837               (goto-char p)
8838               (if (search-forward "\nsubject: " nil t)
8839                   (gnus-header-value) "(none)"))
8840             ;; From.
8841             (progn
8842               (goto-char p)
8843               (if (search-forward "\nfrom: " nil t)
8844                   (gnus-header-value) "(nobody)"))
8845             ;; Date.
8846             (progn
8847               (goto-char p)
8848               (if (search-forward "\ndate: " nil t)
8849                   (gnus-header-value) ""))
8850             ;; Message-ID.
8851             (progn
8852               (goto-char p)
8853               (if (search-forward "\nmessage-id: " nil t)
8854                   (setq id (gnus-header-value))
8855                 ;; If there was no message-id, we just fake one to make
8856                 ;; subsequent routines simpler.
8857                 (setq id (concat "none+"
8858                                  (int-to-string
8859                                   (setq gnus-newsgroup-none-id
8860                                         (1+ gnus-newsgroup-none-id)))))))
8861             ;; References.
8862             (progn
8863               (goto-char p)
8864               (if (search-forward "\nreferences: " nil t)
8865                   (prog1
8866                       (gnus-header-value)
8867                     (setq end (match-end 0))
8868                     (save-excursion
8869                       (setq ref
8870                             (downcase
8871                              (buffer-substring
8872                               (progn
8873                                 (end-of-line)
8874                                 (search-backward ">" end t)
8875                                 (1+ (point)))
8876                               (progn
8877                                 (search-backward "<" end t)
8878                                 (point)))))))
8879                 ;; Get the references from the in-reply-to header if there
8880                 ;; were no references and the in-reply-to header looks
8881                 ;; promising.
8882                 (if (and (search-forward "\nin-reply-to: " nil t)
8883                          (setq in-reply-to (gnus-header-value))
8884                          (string-match "<[^>]+>" in-reply-to))
8885                     (prog1
8886                         (setq ref (substring in-reply-to (match-beginning 0)
8887                                              (match-end 0)))
8888                       (setq ref (downcase ref))))
8889                 (setq ref "")))
8890             ;; Chars.
8891             0
8892             ;; Lines.
8893             (progn
8894               (goto-char p)
8895               (if (search-forward "\nlines: " nil t)
8896                   (if (numberp (setq lines (read cur)))
8897                       lines 0)
8898                 0))
8899             ;; Xref.
8900             (progn
8901               (goto-char p)
8902               (and (search-forward "\nxref: " nil t)
8903                    (gnus-header-value)))))
8904           ;; We do the threading while we read the headers.  The
8905           ;; message-id and the last reference are both entered into
8906           ;; the same hash table.  Some tippy-toeing around has to be
8907           ;; done in case an article has arrived before the article
8908           ;; which it refers to.
8909           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8910               (if (and (car (symbol-value id-dep))
8911                        (not force-new))
8912                   ;; An article with this Message-ID has already
8913                   ;; been seen, so we ignore this one, except we add
8914                   ;; any additional Xrefs (in case the two articles
8915                   ;; came from different servers).
8916                   (progn
8917                     (mail-header-set-xref
8918                      (car (symbol-value id-dep))
8919                      (concat (or (mail-header-xref
8920                                   (car (symbol-value id-dep))) "")
8921                              (or (mail-header-xref header) "")))
8922                     (setq header nil))
8923                 (setcar (symbol-value id-dep) header))
8924             (set id-dep (list header)))
8925           (when header
8926             (if (boundp (setq ref-dep (intern ref dependencies)))
8927                 (setcdr (symbol-value ref-dep)
8928                         (nconc (cdr (symbol-value ref-dep))
8929                                (list (symbol-value id-dep))))
8930               (set ref-dep (list nil (symbol-value id-dep))))
8931             (setq headers (cons header headers)))
8932           (goto-char (point-max))
8933           (widen))
8934         (nreverse headers)))))
8935
8936 ;; The following macros and functions were written by Felix Lee
8937 ;; <flee@cse.psu.edu>.
8938
8939 (defmacro gnus-nov-read-integer ()
8940   '(prog1
8941        (if (= (following-char) ?\t)
8942            0
8943          (let ((num (condition-case nil (read buffer) (error nil))))
8944            (if (numberp num) num 0)))
8945      (or (eobp) (forward-char 1))))
8946
8947 (defmacro gnus-nov-skip-field ()
8948   '(search-forward "\t" eol 'move))
8949
8950 (defmacro gnus-nov-field ()
8951   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
8952
8953 ;; Goes through the xover lines and returns a list of vectors
8954 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
8955   "Parse the news overview data in the server buffer, and return a
8956 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
8957   ;; Get the Xref when the users reads the articles since most/some
8958   ;; NNTP servers do not include Xrefs when using XOVER.
8959   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
8960   (let ((cur nntp-server-buffer)
8961         (dependencies gnus-newsgroup-dependencies)
8962         number headers header)
8963     (save-excursion
8964       (set-buffer nntp-server-buffer)
8965       ;; Allow the user to mangle the headers before parsing them.
8966       (run-hooks 'gnus-parse-headers-hook)
8967       ;; Allow the user to mangle the headers before parsing them.
8968       (run-hooks 'gnus-parse-headers-hook)
8969       (goto-char (point-min))
8970       (while (and sequence (not (eobp)))
8971         (setq number (read cur))
8972         (while (and sequence (< (car sequence) number))
8973           (setq sequence (cdr sequence)))
8974         (and sequence
8975              (eq number (car sequence))
8976              (progn
8977                (setq sequence (cdr sequence))
8978                (if (setq header
8979                          (inline (gnus-nov-parse-line
8980                                   number dependencies force-new)))
8981                    (setq headers (cons header headers)))))
8982         (forward-line 1))
8983       (setq headers (nreverse headers)))
8984     headers))
8985
8986 ;; This function has to be called with point after the article number
8987 ;; on the beginning of the line.
8988 (defun gnus-nov-parse-line (number dependencies &optional force-new)
8989   (let ((none 0)
8990         (eol (gnus-point-at-eol))
8991         (buffer (current-buffer))
8992         header ref id id-dep ref-dep)
8993
8994     ;; overview: [num subject from date id refs chars lines misc]
8995     (narrow-to-region (point) eol)
8996     (or (eobp) (forward-char))
8997
8998     (condition-case nil
8999         (setq header
9000               (vector
9001                number                   ; number
9002                (gnus-nov-field)         ; subject
9003                (gnus-nov-field)         ; from
9004                (gnus-nov-field)         ; date
9005                (setq id (or (gnus-nov-field)
9006                             (concat "none+"
9007                                     (int-to-string
9008                                      (setq none (1+ none)))))) ; id
9009                (progn
9010                  (save-excursion
9011                    (let ((beg (point)))
9012                      (search-forward "\t" eol)
9013                      (if (search-backward ">" beg t)
9014                          (setq ref
9015                                (downcase
9016                                 (buffer-substring
9017                                  (1+ (point))
9018                                  (progn
9019                                    (search-backward "<" beg t)
9020                                    (point)))))
9021                        (setq ref nil))))
9022                  (gnus-nov-field))      ; refs
9023                (gnus-nov-read-integer)  ; chars
9024                (gnus-nov-read-integer)  ; lines
9025                (if (= (following-char) ?\n)
9026                    nil
9027                  (gnus-nov-field))      ; misc
9028                ))
9029       (error (progn
9030                (ding)
9031                (gnus-message 4 "Strange nov line")
9032                (setq header nil)
9033                (goto-char eol))))
9034
9035     (widen)
9036
9037     ;; We build the thread tree.
9038     (when header
9039       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9040           (if (and (car (symbol-value id-dep))
9041                    (not force-new))
9042               ;; An article with this Message-ID has already been seen,
9043               ;; so we ignore this one, except we add any additional
9044               ;; Xrefs (in case the two articles came from different
9045               ;; servers.
9046               (progn
9047                 (mail-header-set-xref
9048                  (car (symbol-value id-dep))
9049                  (concat (or (mail-header-xref
9050                               (car (symbol-value id-dep))) "")
9051                          (or (mail-header-xref header) "")))
9052                 (setq header nil))
9053             (setcar (symbol-value id-dep) header))
9054         (set id-dep (list header))))
9055     (if header
9056         (progn
9057           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9058               (setcdr (symbol-value ref-dep)
9059                       (nconc (cdr (symbol-value ref-dep))
9060                              (list (symbol-value id-dep))))
9061             (set ref-dep (list nil (symbol-value id-dep))))))
9062     header))
9063
9064 (defun gnus-article-get-xrefs ()
9065   "Fill in the Xref value in `gnus-current-headers', if necessary.
9066 This is meant to be called in `gnus-article-internal-prepare-hook'."
9067   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9068                                  gnus-current-headers)))
9069     (or (not gnus-use-cross-reference)
9070         (not headers)
9071         (and (mail-header-xref headers)
9072              (not (string= (mail-header-xref headers) "")))
9073         (let ((case-fold-search t)
9074               xref)
9075           (save-restriction
9076             (nnheader-narrow-to-headers)
9077             (goto-char (point-min))
9078             (if (or (and (eq (downcase (following-char)) ?x)
9079                          (looking-at "Xref:"))
9080                     (search-forward "\nXref:" nil t))
9081                 (progn
9082                   (goto-char (1+ (match-end 0)))
9083                   (setq xref (buffer-substring (point)
9084                                                (progn (end-of-line) (point))))
9085                   (mail-header-set-xref headers xref))))))))
9086
9087 (defun gnus-summary-insert-subject (id)
9088   "Find article ID and insert the summary line for that article."
9089   (let ((header (gnus-read-header id))
9090         (number (and (numberp id) id)))
9091     (when header
9092       ;; Rebuild the thread that this article is part of and go to the
9093       ;; article we have fetched.
9094       (gnus-rebuild-thread (mail-header-id header))
9095       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9096     (when (and (numberp number)
9097                (> number 0))
9098       ;; We have to update the boundaries even if we can't fetch the
9099       ;; article if ID is a number -- so that the next `P' or `N'
9100       ;; command will fetch the previous (or next) article even
9101       ;; if the one we tried to fetch this time has been canceled.
9102       (and (> number gnus-newsgroup-end)
9103            (setq gnus-newsgroup-end number))
9104       (and (< number gnus-newsgroup-begin)
9105            (setq gnus-newsgroup-begin number))
9106       (setq gnus-newsgroup-unselected
9107             (delq number gnus-newsgroup-unselected)))
9108     ;; Report back a success?
9109     (and header number)))
9110
9111 (defun gnus-summary-work-articles (n)
9112   "Return a list of articles to be worked upon.  The prefix argument,
9113 the list of process marked articles, and the current article will be
9114 taken into consideration."
9115   (cond
9116    ((and n (numberp n))
9117     ;; A numerical prefix has been given.
9118     (let ((backward (< n 0))
9119           (n (abs n))
9120           articles article)
9121       (save-excursion
9122         (while
9123             (and (> n 0)
9124                  (push (setq article (gnus-summary-article-number))
9125                        articles)
9126                  (if backward
9127                      (gnus-summary-find-prev nil article)
9128                    (gnus-summary-find-next nil article)))
9129           (decf n)))
9130       (nreverse articles)))
9131    ((and (boundp 'transient-mark-mode)
9132          transient-mark-mode
9133          mark-active)
9134     ;; Work on the region between point and mark.
9135     (let ((max (max (point) (mark)))
9136           articles article)
9137       (save-excursion
9138         (goto-char (min (point) (mark)))
9139         (while
9140             (and
9141              (push (setq article (gnus-summary-article-number)) articles)
9142              (gnus-summary-find-next nil article)
9143              (< (point) max)))
9144         (nreverse articles))))
9145    (gnus-newsgroup-processable
9146     ;; There are process-marked articles present.
9147     (reverse gnus-newsgroup-processable))
9148    (t
9149     ;; Just return the current article.
9150     (list (gnus-summary-article-number)))))
9151
9152 (defun gnus-summary-search-group (&optional backward use-level)
9153   "Search for next unread newsgroup.
9154 If optional argument BACKWARD is non-nil, search backward instead."
9155   (save-excursion
9156     (set-buffer gnus-group-buffer)
9157     (if (gnus-group-search-forward
9158          backward nil (if use-level (gnus-group-group-level) nil))
9159         (gnus-group-group-name))))
9160
9161 (defun gnus-summary-best-group (&optional exclude-group)
9162   "Find the name of the best unread group.
9163 If EXCLUDE-GROUP, do not go to this group."
9164   (save-excursion
9165     (set-buffer gnus-group-buffer)
9166     (save-excursion
9167       (gnus-group-best-unread-group exclude-group))))
9168
9169 (defun gnus-summary-find-next (&optional unread article backward)
9170   (if backward (gnus-summary-find-prev)
9171     (let* ((article (or article (gnus-summary-article-number)))
9172            (arts (gnus-data-find-list article))
9173            result)
9174       (when (or (not gnus-summary-check-current)
9175                 (not unread)
9176                 (not (gnus-data-unread-p (car arts))))
9177         (setq arts (cdr arts)))
9178       (when (setq result
9179                   (if unread
9180                       (progn
9181                         (while arts
9182                           (when (gnus-data-unread-p (car arts))
9183                             (setq result (car arts)
9184                                   arts nil))
9185                           (setq arts (cdr arts)))
9186                         result)
9187                     (car arts)))
9188         (goto-char (gnus-data-pos result))
9189         (gnus-data-number result)))))
9190
9191 (defun gnus-summary-find-prev (&optional unread article)
9192   (let* ((article (or article (gnus-summary-article-number)))
9193          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9194          result)
9195     (when (or (not gnus-summary-check-current)
9196               (not unread)
9197               (not (gnus-data-unread-p (car arts))))
9198       (setq arts (cdr arts)))
9199     (if (setq result
9200               (if unread
9201                   (progn
9202                     (while arts
9203                       (and (gnus-data-unread-p (car arts))
9204                            (setq result (car arts)
9205                                  arts nil))
9206                       (setq arts (cdr arts)))
9207                     result)
9208                 (car arts)))
9209         (progn
9210           (goto-char (gnus-data-pos result))
9211           (gnus-data-number result)))))
9212
9213 (defun gnus-summary-find-subject (subject &optional unread backward article)
9214   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9215          (article (or article (gnus-summary-article-number)))
9216          (articles (gnus-data-list backward))
9217          (arts (gnus-data-find-list article articles))
9218          result)
9219     (when (or (not gnus-summary-check-current)
9220               (not unread)
9221               (not (gnus-data-unread-p (car arts))))
9222       (setq arts (cdr arts)))
9223     (while arts
9224       (and (or (not unread)
9225                (gnus-data-unread-p (car arts)))
9226            (vectorp (gnus-data-header (car arts)))
9227            (gnus-subject-equal
9228             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9229            (setq result (car arts)
9230                  arts nil))
9231       (setq arts (cdr arts)))
9232     (and result
9233          (goto-char (gnus-data-pos result))
9234          (gnus-data-number result))))
9235
9236 (defun gnus-summary-search-forward (&optional unread subject backward)
9237   "Search forward for an article.
9238 If UNREAD, look for unread articles.  If SUBJECT, look for
9239 articles with that subject.  If BACKWARD, search backward instead."
9240   (cond (subject (gnus-summary-find-subject subject unread backward))
9241         (backward (gnus-summary-find-prev unread))
9242         (t (gnus-summary-find-next unread))))
9243
9244 (defun gnus-recenter (&optional n)
9245   "Center point in window and redisplay frame.
9246 Also do horizontal recentering."
9247   (interactive)
9248   (gnus-horizontal-recenter)
9249   (recenter n))
9250
9251 (defun gnus-summary-recenter ()
9252   "Center point in the summary window.
9253 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9254 displayed, no centering will be performed."
9255   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9256   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9257   (let* ((top (cond ((< (window-height) 4) 0)
9258                     ((< (window-height) 7) 1)
9259                     (t 2)))
9260          (height (1- (window-height)))
9261          (bottom (save-excursion (goto-char (point-max))
9262                                  (forward-line (- height))
9263                                  (point)))
9264          (window (get-buffer-window (current-buffer))))
9265     ;; The user has to want it.
9266     (when gnus-auto-center-summary
9267       (when (get-buffer-window gnus-article-buffer)
9268        ;; Only do recentering when the article buffer is displayed,
9269        ;; Set the window start to either `bottom', which is the biggest
9270        ;; possible valid number, or the second line from the top,
9271        ;; whichever is the least.
9272        (set-window-start
9273         window (min bottom (save-excursion 
9274                              (forward-line (- top)) (point)))))
9275       ;; Do horizontal recentering while we're at it.
9276       (when (get-buffer-window (current-buffer) t)
9277         (let ((selected (selected-window)))
9278           (select-window (get-buffer-window (current-buffer) t))
9279           (gnus-summary-position-point)
9280           (gnus-horizontal-recenter)
9281           (select-window selected))))))
9282
9283 (defun gnus-horizontal-recenter ()
9284   "Recenter the current buffer horizontally."
9285   (if (< (current-column) (/ (window-width) 2))
9286       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9287     (let* ((orig (point))
9288            (end (window-end (get-buffer-window (current-buffer) t)))
9289            (max 0))
9290       ;; Find the longest line currently displayed in the window.
9291       (goto-char (window-start))
9292       (while (and (not (eobp)) 
9293                   (< (point) end))
9294         (end-of-line)
9295         (setq max (max max (current-column)))
9296         (forward-line 1))
9297       (goto-char orig)
9298       ;; Scroll horizontally to center (sort of) the point.
9299       (if (> max (window-width))
9300           (set-window-hscroll 
9301            (get-buffer-window (current-buffer) t)
9302            (min (- (current-column) (/ (window-width) 3))
9303                 (+ 2 (- max (window-width)))))
9304         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9305       max)))
9306
9307 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9308 (defun gnus-short-group-name (group &optional levels)
9309   "Collapse GROUP name LEVELS."
9310   (let* ((name "") 
9311          (foreign "")
9312          (depth 0) 
9313          (skip 1)
9314          (levels (or levels
9315                      (progn
9316                        (while (string-match "\\." group skip)
9317                          (setq skip (match-end 0)
9318                                depth (+ depth 1)))
9319                        depth))))
9320     (if (string-match ":" group)
9321         (setq foreign (substring group 0 (match-end 0))
9322               group (substring group (match-end 0))))
9323     (while group
9324       (if (and (string-match "\\." group)
9325                (> levels (- gnus-group-uncollapsed-levels 1)))
9326           (setq name (concat name (substring group 0 1))
9327                 group (substring group (match-end 0))
9328                 levels (- levels 1)
9329                 name (concat name "."))
9330         (setq name (concat foreign name group)
9331               group nil)))
9332     name))
9333
9334 (defun gnus-summary-jump-to-group (newsgroup)
9335   "Move point to NEWSGROUP in group mode buffer."
9336   ;; Keep update point of group mode buffer if visible.
9337   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9338       (save-window-excursion
9339         ;; Take care of tree window mode.
9340         (if (get-buffer-window gnus-group-buffer)
9341             (pop-to-buffer gnus-group-buffer))
9342         (gnus-group-jump-to-group newsgroup))
9343     (save-excursion
9344       ;; Take care of tree window mode.
9345       (if (get-buffer-window gnus-group-buffer)
9346           (pop-to-buffer gnus-group-buffer)
9347         (set-buffer gnus-group-buffer))
9348       (gnus-group-jump-to-group newsgroup))))
9349
9350 ;; This function returns a list of article numbers based on the
9351 ;; difference between the ranges of read articles in this group and
9352 ;; the range of active articles.
9353 (defun gnus-list-of-unread-articles (group)
9354   (let* ((read (gnus-info-read (gnus-get-info group)))
9355          (active (gnus-active group))
9356          (last (cdr active))
9357          first nlast unread)
9358     ;; If none are read, then all are unread.
9359     (if (not read)
9360         (setq first (car active))
9361       ;; If the range of read articles is a single range, then the
9362       ;; first unread article is the article after the last read
9363       ;; article.  Sounds logical, doesn't it?
9364       (if (not (listp (cdr read)))
9365           (setq first (1+ (cdr read)))
9366         ;; `read' is a list of ranges.
9367         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9368                                 (car (car read)))) 1)
9369             (setq first 1))
9370         (while read
9371           (if first
9372               (while (< first nlast)
9373                 (setq unread (cons first unread))
9374                 (setq first (1+ first))))
9375           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9376           (setq nlast (if (atom (car (cdr read)))
9377                           (car (cdr read))
9378                         (car (car (cdr read)))))
9379           (setq read (cdr read)))))
9380     ;; And add the last unread articles.
9381     (while (<= first last)
9382       (setq unread (cons first unread))
9383       (setq first (1+ first)))
9384     ;; Return the list of unread articles.
9385     (nreverse unread)))
9386
9387 (defun gnus-list-of-read-articles (group)
9388   "Return a list of unread, unticked and non-dormant articles."
9389   (let* ((info (gnus-get-info group))
9390          (marked (gnus-info-marks info))
9391          (active (gnus-active group)))
9392     (and info active
9393          (gnus-set-difference
9394           (gnus-sorted-complement
9395            (gnus-uncompress-range active)
9396            (gnus-list-of-unread-articles group))
9397           (append
9398            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9399            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9400
9401 ;; Various summary commands
9402
9403 (defun gnus-summary-universal-argument (arg)
9404   "Perform any operation on all articles that are process/prefixed."
9405   (interactive "P")
9406   (gnus-set-global-variables)
9407   (let ((articles (gnus-summary-work-articles arg))
9408         func article)
9409     (if (eq
9410          (setq
9411           func
9412           (key-binding
9413            (read-key-sequence
9414             (substitute-command-keys
9415              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9416              ))))
9417          'undefined)
9418         (progn
9419           (message "Undefined key")
9420           (ding))
9421       (save-excursion
9422         (while articles
9423           (gnus-summary-goto-subject (setq article (pop articles)))
9424           (command-execute func)
9425           (gnus-summary-remove-process-mark article)))))
9426   (gnus-summary-position-point))
9427
9428 (defun gnus-summary-toggle-truncation (&optional arg)
9429   "Toggle truncation of summary lines.
9430 With arg, turn line truncation on iff arg is positive."
9431   (interactive "P")
9432   (setq truncate-lines
9433         (if (null arg) (not truncate-lines)
9434           (> (prefix-numeric-value arg) 0)))
9435   (redraw-display))
9436
9437 (defun gnus-summary-reselect-current-group (&optional all rescan)
9438   "Exit and then reselect the current newsgroup.
9439 The prefix argument ALL means to select all articles."
9440   (interactive "P")
9441   (gnus-set-global-variables)
9442   (let ((current-subject (gnus-summary-article-number))
9443         (group gnus-newsgroup-name))
9444     (setq gnus-newsgroup-begin nil)
9445     (gnus-summary-exit)
9446     ;; We have to adjust the point of group mode buffer because the
9447     ;; current point was moved to the next unread newsgroup by
9448     ;; exiting.
9449     (gnus-summary-jump-to-group group)
9450     (when rescan
9451       (save-excursion
9452         (gnus-group-get-new-news-this-group 1)))
9453     (gnus-group-read-group all t)
9454     (gnus-summary-goto-subject current-subject)))
9455
9456 (defun gnus-summary-rescan-group (&optional all)
9457   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9458   (interactive "P")
9459   (gnus-summary-reselect-current-group all t))
9460
9461 (defun gnus-summary-update-info ()
9462   (let* ((group gnus-newsgroup-name))
9463     (when gnus-newsgroup-kill-headers
9464       (setq gnus-newsgroup-killed
9465             (gnus-compress-sequence
9466              (nconc
9467               (gnus-set-sorted-intersection
9468                (gnus-uncompress-range gnus-newsgroup-killed)
9469                (setq gnus-newsgroup-unselected
9470                      (sort gnus-newsgroup-unselected '<)))
9471               (setq gnus-newsgroup-unreads
9472                     (sort gnus-newsgroup-unreads '<))) t)))
9473     (unless (listp (cdr gnus-newsgroup-killed))
9474       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9475     (let ((headers gnus-newsgroup-headers))
9476       (gnus-close-group group)
9477       (run-hooks 'gnus-exit-group-hook)
9478       (unless gnus-save-score
9479         (setq gnus-newsgroup-scored nil))
9480       ;; Set the new ranges of read articles.
9481       (gnus-update-read-articles
9482        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9483       ;; Set the current article marks.
9484       (gnus-update-marks)
9485       ;; Do the cross-ref thing.
9486       (when gnus-use-cross-reference
9487         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9488       ;; Do adaptive scoring, and possibly save score files.
9489       (when gnus-newsgroup-adaptive
9490         (gnus-score-adaptive))
9491       (when gnus-use-scoring
9492         (gnus-score-save))
9493       ;; Do not switch windows but change the buffer to work.
9494       (set-buffer gnus-group-buffer)
9495       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9496           (gnus-group-update-group group)))))
9497
9498 (defun gnus-summary-exit (&optional temporary)
9499   "Exit reading current newsgroup, and then return to group selection mode.
9500 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9501   (interactive)
9502   (gnus-set-global-variables)
9503   (gnus-kill-save-kill-buffer)
9504   (let* ((group gnus-newsgroup-name)
9505          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9506          (mode major-mode)
9507          (buf (current-buffer)))
9508     (run-hooks 'gnus-summary-prepare-exit-hook)
9509     ;; If we have several article buffers, we kill them at exit.
9510     (unless gnus-single-article-buffer
9511       (gnus-kill-buffer gnus-article-buffer)
9512       (gnus-kill-buffer gnus-original-article-buffer))
9513     (when gnus-use-cache
9514       (gnus-cache-possibly-remove-articles)
9515       (gnus-cache-save-buffers))
9516     (when gnus-use-trees
9517       (gnus-tree-close group))
9518     ;; Make all changes in this group permanent.
9519     (gnus-summary-update-info)
9520     ;; Make sure where I was, and go to next newsgroup.
9521     (set-buffer gnus-group-buffer)
9522     (unless quit-config
9523       (gnus-group-jump-to-group group)
9524       (gnus-group-next-unread-group 1))
9525     (run-hooks 'gnus-summary-exit-hook)
9526     (if temporary
9527         nil                             ;Nothing to do.
9528       ;; If we have several article buffers, we kill them at exit.
9529       (unless gnus-single-article-buffer
9530         (gnus-kill-buffer gnus-article-buffer)
9531         (gnus-kill-buffer gnus-original-article-buffer))
9532       (set-buffer buf)
9533       (if (not gnus-kill-summary-on-exit)
9534           (gnus-deaden-summary)
9535         ;; We set all buffer-local variables to nil.  It is unclear why
9536         ;; this is needed, but if we don't, buffer-local variables are
9537         ;; not garbage-collected, it seems.  This would the lead to en
9538         ;; ever-growing Emacs.
9539         (gnus-summary-clear-local-variables)
9540         (when (get-buffer gnus-article-buffer)
9541           (bury-buffer gnus-article-buffer))
9542         ;; We clear the global counterparts of the buffer-local
9543         ;; variables as well, just to be on the safe side.
9544         (gnus-configure-windows 'group 'force)
9545         (gnus-summary-clear-local-variables)
9546         ;; Return to group mode buffer.
9547         (if (eq mode 'gnus-summary-mode)
9548             (gnus-kill-buffer buf)))
9549       (setq gnus-current-select-method gnus-select-method)
9550       (pop-to-buffer gnus-group-buffer)
9551       ;; Clear the current group name.
9552       (if (not quit-config)
9553           (progn
9554             (gnus-group-jump-to-group group)
9555             (gnus-group-next-unread-group 1)
9556             (gnus-configure-windows 'group 'force))
9557         (if (not (buffer-name (car quit-config)))
9558             (gnus-configure-windows 'group 'force)
9559           (set-buffer (car quit-config))
9560           (and (eq major-mode 'gnus-summary-mode)
9561                (gnus-set-global-variables))
9562           (gnus-configure-windows (cdr quit-config))))
9563       (unless quit-config
9564         (setq gnus-newsgroup-name nil)))))
9565
9566 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9567 (defun gnus-summary-exit-no-update (&optional no-questions)
9568   "Quit reading current newsgroup without updating read article info."
9569   (interactive)
9570   (gnus-set-global-variables)
9571   (let* ((group gnus-newsgroup-name)
9572          (quit-config (gnus-group-quit-config group)))
9573     (when (or no-questions
9574               gnus-expert-user
9575               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9576       ;; If we have several article buffers, we kill them at exit.
9577       (unless gnus-single-article-buffer
9578         (gnus-kill-buffer gnus-article-buffer)
9579         (gnus-kill-buffer gnus-original-article-buffer))
9580       (if (not gnus-kill-summary-on-exit)
9581           (gnus-deaden-summary)
9582         (gnus-close-group group)
9583         (gnus-summary-clear-local-variables)
9584         (set-buffer gnus-group-buffer)
9585         (gnus-summary-clear-local-variables)
9586         (when (get-buffer gnus-summary-buffer)
9587           (kill-buffer gnus-summary-buffer)))
9588       (when gnus-use-trees
9589         (gnus-tree-close group))
9590       (when (get-buffer gnus-article-buffer)
9591         (bury-buffer gnus-article-buffer))
9592       ;; Return to the group buffer.
9593       (gnus-configure-windows 'group 'force)
9594       ;; Clear the current group name.
9595       (setq gnus-newsgroup-name nil)
9596       (when (equal (gnus-group-group-name) group)
9597         (gnus-group-next-unread-group 1))
9598       (when quit-config
9599         (if (not (buffer-name (car quit-config)))
9600             (gnus-configure-windows 'group 'force)
9601           (set-buffer (car quit-config))
9602           (when (eq major-mode 'gnus-summary-mode)
9603             (gnus-set-global-variables))
9604           (gnus-configure-windows (cdr quit-config)))))))
9605
9606 ;;; Dead summaries.
9607
9608 (defvar gnus-dead-summary-mode-map nil)
9609
9610 (if gnus-dead-summary-mode-map
9611     nil
9612   (setq gnus-dead-summary-mode-map (make-keymap))
9613   (suppress-keymap gnus-dead-summary-mode-map)
9614   (substitute-key-definition
9615    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9616   (let ((keys '("\C-d" "\r" "\177")))
9617     (while keys
9618       (define-key gnus-dead-summary-mode-map
9619         (pop keys) 'gnus-summary-wake-up-the-dead))))
9620
9621 (defvar gnus-dead-summary-mode nil
9622   "Minor mode for Gnus summary buffers.")
9623
9624 (defun gnus-dead-summary-mode (&optional arg)
9625   "Minor mode for Gnus summary buffers."
9626   (interactive "P")
9627   (when (eq major-mode 'gnus-summary-mode)
9628     (make-local-variable 'gnus-dead-summary-mode)
9629     (setq gnus-dead-summary-mode
9630           (if (null arg) (not gnus-dead-summary-mode)
9631             (> (prefix-numeric-value arg) 0)))
9632     (when gnus-dead-summary-mode
9633       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9634         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9635       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9636         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9637               minor-mode-map-alist)))))
9638
9639 (defun gnus-deaden-summary ()
9640   "Make the current summary buffer into a dead summary buffer."
9641   ;; Kill any previous dead summary buffer.
9642   (when (and gnus-dead-summary
9643              (buffer-name gnus-dead-summary))
9644     (save-excursion
9645       (set-buffer gnus-dead-summary)
9646       (when gnus-dead-summary-mode
9647         (kill-buffer (current-buffer)))))
9648   ;; Make this the current dead summary.
9649   (setq gnus-dead-summary (current-buffer))
9650   (gnus-dead-summary-mode 1)
9651   (let ((name (buffer-name)))
9652     (when (string-match "Summary" name)
9653       (rename-buffer
9654        (concat (substring name 0 (match-beginning 0)) "Dead "
9655                (substring name (match-beginning 0))) t))))
9656
9657 (defun gnus-kill-or-deaden-summary (buffer)
9658   "Kill or deaden the summary BUFFER."
9659   (cond (gnus-kill-summary-on-exit
9660          (when (and gnus-use-trees
9661                     (and (get-buffer buffer)
9662                          (buffer-name (get-buffer buffer))))
9663            (save-excursion
9664              (set-buffer (get-buffer buffer))
9665              (gnus-tree-close gnus-newsgroup-name)))
9666          (gnus-kill-buffer buffer))
9667         ((and (get-buffer buffer)
9668               (buffer-name (get-buffer buffer)))
9669          (save-excursion
9670            (set-buffer buffer)
9671            (gnus-deaden-summary)))))
9672
9673 (defun gnus-summary-wake-up-the-dead (&rest args)
9674   "Wake up the dead summary buffer."
9675   (interactive)
9676   (gnus-dead-summary-mode -1)
9677   (let ((name (buffer-name)))
9678     (when (string-match "Dead " name)
9679       (rename-buffer
9680        (concat (substring name 0 (match-beginning 0))
9681                (substring name (match-end 0))) t)))
9682   (gnus-message 3 "This dead summary is now alive again"))
9683
9684 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9685 (defun gnus-summary-fetch-faq (&optional faq-dir)
9686   "Fetch the FAQ for the current group.
9687 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9688 in."
9689   (interactive
9690    (list
9691     (if current-prefix-arg
9692         (completing-read
9693          "Faq dir: " (and (listp gnus-group-faq-directory)
9694                           gnus-group-faq-directory)))))
9695   (let (gnus-faq-buffer)
9696     (and (setq gnus-faq-buffer
9697                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9698          (gnus-configure-windows 'summary-faq))))
9699
9700 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9701 (defun gnus-summary-describe-group (&optional force)
9702   "Describe the current newsgroup."
9703   (interactive "P")
9704   (gnus-group-describe-group force gnus-newsgroup-name))
9705
9706 (defun gnus-summary-describe-briefly ()
9707   "Describe summary mode commands briefly."
9708   (interactive)
9709   (gnus-message 6
9710                 (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")))
9711
9712 ;; Walking around group mode buffer from summary mode.
9713
9714 (defun gnus-summary-next-group (&optional no-article target-group backward)
9715   "Exit current newsgroup and then select next unread newsgroup.
9716 If prefix argument NO-ARTICLE is non-nil, no article is selected
9717 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9718 previous group instead."
9719   (interactive "P")
9720   (gnus-set-global-variables)
9721   (let ((current-group gnus-newsgroup-name)
9722         (current-buffer (current-buffer))
9723         entered)
9724     ;; First we semi-exit this group to update Xrefs and all variables.
9725     ;; We can't do a real exit, because the window conf must remain
9726     ;; the same in case the user is prompted for info, and we don't
9727     ;; want the window conf to change before that...
9728     (gnus-summary-exit t)
9729     (while (not entered)
9730       ;; Then we find what group we are supposed to enter.
9731       (set-buffer gnus-group-buffer)
9732       (gnus-group-jump-to-group current-group)
9733       (setq target-group
9734             (or target-group
9735                 (if (eq gnus-keep-same-level 'best)
9736                     (gnus-summary-best-group gnus-newsgroup-name)
9737                   (gnus-summary-search-group backward gnus-keep-same-level))))
9738       (if (not target-group)
9739           ;; There are no further groups, so we return to the group
9740           ;; buffer.
9741           (progn
9742             (gnus-message 5 "Returning to the group buffer")
9743             (setq entered t)
9744             (set-buffer current-buffer)
9745             (gnus-summary-exit))
9746         ;; We try to enter the target group.
9747         (gnus-group-jump-to-group target-group)
9748         (let ((unreads (gnus-group-group-unread)))
9749           (if (and (or (eq t unreads)
9750                        (and unreads (not (zerop unreads))))
9751                    (gnus-summary-read-group
9752                     target-group nil no-article current-buffer))
9753               (setq entered t)
9754             (setq current-group target-group
9755                   target-group nil)))))))
9756
9757 (defun gnus-summary-prev-group (&optional no-article)
9758   "Exit current newsgroup and then select previous unread newsgroup.
9759 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9760   (interactive "P")
9761   (gnus-summary-next-group no-article nil t))
9762
9763 ;; Walking around summary lines.
9764
9765 (defun gnus-summary-first-subject (&optional unread)
9766   "Go to the first unread subject.
9767 If UNREAD is non-nil, go to the first unread article.
9768 Returns the article selected or nil if there are no unread articles."
9769   (interactive "P")
9770   (prog1
9771       (cond
9772        ;; Empty summary.
9773        ((null gnus-newsgroup-data)
9774         (gnus-message 3 "No articles in the group")
9775         nil)
9776        ;; Pick the first article.
9777        ((not unread)
9778         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9779         (gnus-data-number (car gnus-newsgroup-data)))
9780        ;; No unread articles.
9781        ((null gnus-newsgroup-unreads)
9782         (gnus-message 3 "No more unread articles")
9783         nil)
9784        ;; Find the first unread article.
9785        (t
9786         (let ((data gnus-newsgroup-data))
9787           (while (and data
9788                       (not (gnus-data-unread-p (car data))))
9789             (setq data (cdr data)))
9790           (if data
9791               (progn
9792                 (goto-char (gnus-data-pos (car data)))
9793                 (gnus-data-number (car data)))))))
9794     (gnus-summary-position-point)))
9795
9796 (defun gnus-summary-next-subject (n &optional unread dont-display)
9797   "Go to next N'th summary line.
9798 If N is negative, go to the previous N'th subject line.
9799 If UNREAD is non-nil, only unread articles are selected.
9800 The difference between N and the actual number of steps taken is
9801 returned."
9802   (interactive "p")
9803   (let ((backward (< n 0))
9804         (n (abs n)))
9805     (while (and (> n 0)
9806                 (if backward
9807                     (gnus-summary-find-prev unread)
9808                   (gnus-summary-find-next unread)))
9809       (setq n (1- n)))
9810     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9811                                (if unread " unread" "")))
9812     (or dont-display
9813         (progn
9814           (gnus-summary-recenter)
9815           (gnus-summary-position-point)))
9816     n))
9817
9818 (defun gnus-summary-next-unread-subject (n)
9819   "Go to next N'th unread summary line."
9820   (interactive "p")
9821   (gnus-summary-next-subject n t))
9822
9823 (defun gnus-summary-prev-subject (n &optional unread)
9824   "Go to previous N'th summary line.
9825 If optional argument UNREAD is non-nil, only unread article is selected."
9826   (interactive "p")
9827   (gnus-summary-next-subject (- n) unread))
9828
9829 (defun gnus-summary-prev-unread-subject (n)
9830   "Go to previous N'th unread summary line."
9831   (interactive "p")
9832   (gnus-summary-next-subject (- n) t))
9833
9834 (defun gnus-summary-goto-subject (article &optional force silent)
9835   "Go the subject line of ARTICLE.
9836 If FORCE, also allow jumping to articles not currently shown."
9837   (let ((b (point))
9838         (data (gnus-data-find article)))
9839     ;; We read in the article if we have to.
9840     (and (not data)
9841          force
9842          (gnus-summary-insert-subject article)
9843          (setq data (gnus-data-find article)))
9844     (goto-char b)
9845     (if (not data)
9846         (progn
9847           (unless silent
9848             (gnus-message 3 "Can't find article %d" article))
9849           nil)
9850       (goto-char (gnus-data-pos data))
9851       article)))
9852
9853 ;; Walking around summary lines with displaying articles.
9854
9855 (defun gnus-summary-expand-window (&optional arg)
9856   "Make the summary buffer take up the entire Emacs frame.
9857 Given a prefix, will force an `article' buffer configuration."
9858   (interactive "P")
9859   (gnus-set-global-variables)
9860   (if arg
9861       (gnus-configure-windows 'article 'force)
9862     (gnus-configure-windows 'summary 'force)))
9863
9864 (defun gnus-summary-display-article (article &optional all-header)
9865   "Display ARTICLE in article buffer."
9866   (gnus-set-global-variables)
9867   (if (null article)
9868       nil
9869     (prog1
9870         (if gnus-summary-display-article-function
9871             (funcall gnus-summary-display-article-function article all-header)
9872           (gnus-article-prepare article all-header))
9873       (run-hooks 'gnus-select-article-hook)
9874       (gnus-summary-recenter)
9875       (gnus-summary-goto-subject article)
9876       (when gnus-use-trees
9877         (gnus-possibly-generate-tree article)
9878         (gnus-highlight-selected-tree article))
9879       ;; Successfully display article.
9880       (gnus-article-set-window-start
9881        (cdr (assq article gnus-newsgroup-bookmarks)))
9882       t)))
9883
9884 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9885   "Select the current article.
9886 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9887 non-nil, the article will be re-fetched even if it already present in
9888 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9889 be displayed."
9890   (let ((article (or article (gnus-summary-article-number)))
9891         (all-headers (not (not all-headers))) ;Must be T or NIL.
9892         gnus-summary-display-article-function
9893         did)
9894     (and (not pseudo)
9895          (gnus-summary-article-pseudo-p article)
9896          (error "This is a pseudo-article."))
9897     (prog1
9898         (save-excursion
9899           (set-buffer gnus-summary-buffer)
9900           (if (or (and gnus-single-article-buffer
9901                        (or (null gnus-current-article)
9902                            (null gnus-article-current)
9903                            (null (get-buffer gnus-article-buffer))
9904                            (not (eq article (cdr gnus-article-current)))
9905                            (not (equal (car gnus-article-current)
9906                                        gnus-newsgroup-name))))
9907                   (and (not gnus-single-article-buffer)
9908                        (null gnus-current-article))
9909                   force)
9910               ;; The requested article is different from the current article.
9911               (prog1
9912                   (gnus-summary-display-article article all-headers)
9913                 (setq did article))
9914             (if (or all-headers gnus-show-all-headers)
9915                 (gnus-article-show-all-headers))
9916             'old))
9917       (if did
9918           (gnus-article-set-window-start
9919            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9920
9921 (defun gnus-summary-set-current-mark (&optional current-mark)
9922   "Obsolete function."
9923   nil)
9924
9925 (defun gnus-summary-next-article (&optional unread subject backward push)
9926   "Select the next article.
9927 If UNREAD, only unread articles are selected.
9928 If SUBJECT, only articles with SUBJECT are selected.
9929 If BACKWARD, the previous article is selected instead of the next."
9930   (interactive "P")
9931   (gnus-set-global-variables)
9932   (let (header)
9933     (cond
9934      ;; Is there such an article?
9935      ((and (gnus-summary-search-forward unread subject backward)
9936            (or (gnus-summary-display-article (gnus-summary-article-number))
9937                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9938       (gnus-summary-position-point))
9939      ;; If not, we try the first unread, if that is wanted.
9940      ((and subject
9941            gnus-auto-select-same
9942            (or (gnus-summary-first-unread-article)
9943                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9944       (gnus-summary-position-point)
9945       (gnus-message 6 "Wrapped"))
9946      ;; Try to get next/previous article not displayed in this group.
9947      ((and gnus-auto-extend-newsgroup
9948            (not unread) (not subject))
9949       (gnus-summary-goto-article
9950        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
9951        nil t))
9952      ;; Go to next/previous group.
9953      (t
9954       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9955           (gnus-summary-jump-to-group gnus-newsgroup-name))
9956       (let ((cmd last-command-char)
9957             (group
9958              (if (eq gnus-keep-same-level 'best)
9959                  (gnus-summary-best-group gnus-newsgroup-name)
9960                (gnus-summary-search-group backward gnus-keep-same-level))))
9961         ;; For some reason, the group window gets selected.  We change
9962         ;; it back.
9963         (select-window (get-buffer-window (current-buffer)))
9964         ;; Select next unread newsgroup automagically.
9965         (cond
9966          ((not gnus-auto-select-next)
9967           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
9968          ((or (eq gnus-auto-select-next 'quietly)
9969               (and (eq gnus-auto-select-next 'slightly-quietly)
9970                    push)
9971               (and (eq gnus-auto-select-next 'almost-quietly)
9972                    (gnus-summary-last-article-p)))
9973           ;; Select quietly.
9974           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
9975               (gnus-summary-exit)
9976             (gnus-message 7 "No more%s articles (%s)..."
9977                           (if unread " unread" "")
9978                           (if group (concat "selecting " group)
9979                             "exiting"))
9980             (gnus-summary-next-group nil group backward)))
9981          (t
9982           (gnus-summary-walk-group-buffer
9983            gnus-newsgroup-name cmd unread backward))))))))
9984
9985 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
9986   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
9987                       (?\C-p (gnus-group-prev-unread-group 1))))
9988         keve key group ended)
9989     (save-excursion
9990       (set-buffer gnus-group-buffer)
9991       (gnus-summary-jump-to-group from-group)
9992       (setq group
9993             (if (eq gnus-keep-same-level 'best)
9994                 (gnus-summary-best-group gnus-newsgroup-name)
9995               (gnus-summary-search-group backward gnus-keep-same-level))))
9996     (while (not ended)
9997       (gnus-message
9998        5 "No more%s articles%s" (if unread " unread" "")
9999        (if (and group
10000                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10001            (format " (Type %s for %s [%s])"
10002                    (single-key-description cmd) group
10003                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10004          (format " (Type %s to exit %s)"
10005                  (single-key-description cmd)
10006                  gnus-newsgroup-name)))
10007       ;; Confirm auto selection.
10008       (setq key (car (setq keve (gnus-read-event-char))))
10009       (setq ended t)
10010       (cond
10011        ((assq key keystrokes)
10012         (let ((obuf (current-buffer)))
10013           (switch-to-buffer gnus-group-buffer)
10014           (and group
10015                (gnus-group-jump-to-group group))
10016           (eval (car (cdr (assq key keystrokes))))
10017           (setq group (gnus-group-group-name))
10018           (switch-to-buffer obuf))
10019         (setq ended nil))
10020        ((equal key cmd)
10021         (if (or (not group)
10022                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10023             (gnus-summary-exit)
10024           (gnus-summary-next-group nil group backward)))
10025        (t
10026         (push (cdr keve) unread-command-events))))))
10027
10028 (defun gnus-read-event-char ()
10029   "Get the next event."
10030   (let ((event (read-event)))
10031     (cons (and (numberp event) event) event)))
10032
10033 (defun gnus-summary-next-unread-article ()
10034   "Select unread article after current one."
10035   (interactive)
10036   (gnus-summary-next-article t (and gnus-auto-select-same
10037                                     (gnus-summary-article-subject))))
10038
10039 (defun gnus-summary-prev-article (&optional unread subject)
10040   "Select the article after the current one.
10041 If UNREAD is non-nil, only unread articles are selected."
10042   (interactive "P")
10043   (gnus-summary-next-article unread subject t))
10044
10045 (defun gnus-summary-prev-unread-article ()
10046   "Select unred article before current one."
10047   (interactive)
10048   (gnus-summary-prev-article t (and gnus-auto-select-same
10049                                     (gnus-summary-article-subject))))
10050
10051 (defun gnus-summary-next-page (&optional lines circular)
10052   "Show next page of the selected article.
10053 If at the end of the current article, select the next article.
10054 LINES says how many lines should be scrolled up.
10055
10056 If CIRCULAR is non-nil, go to the start of the article instead of
10057 selecting the next article when reaching the end of the current
10058 article."
10059   (interactive "P")
10060   (setq gnus-summary-buffer (current-buffer))
10061   (gnus-set-global-variables)
10062   (let ((article (gnus-summary-article-number))
10063         (endp nil))
10064     (gnus-configure-windows 'article)
10065     (if (or (null gnus-current-article)
10066             (null gnus-article-current)
10067             (/= article (cdr gnus-article-current))
10068             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10069         ;; Selected subject is different from current article's.
10070         (gnus-summary-display-article article)
10071       (gnus-eval-in-buffer-window
10072        gnus-article-buffer
10073        (setq endp (gnus-article-next-page lines)))
10074       (if endp
10075           (cond (circular
10076                  (gnus-summary-beginning-of-article))
10077                 (lines
10078                  (gnus-message 3 "End of message"))
10079                 ((null lines)
10080                  (if (and (eq gnus-summary-goto-unread 'never)
10081                           (not (eq article gnus-newsgroup-end)))
10082                      (gnus-summary-next-article)
10083                    (gnus-summary-next-unread-article))))))
10084     (gnus-summary-recenter)
10085     (gnus-summary-position-point)))
10086
10087 (defun gnus-summary-prev-page (&optional lines)
10088   "Show previous page of selected article.
10089 Argument LINES specifies lines to be scrolled down."
10090   (interactive "P")
10091   (gnus-set-global-variables)
10092   (let ((article (gnus-summary-article-number)))
10093     (gnus-configure-windows 'article)
10094     (if (or (null gnus-current-article)
10095             (null gnus-article-current)
10096             (/= article (cdr gnus-article-current))
10097             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10098         ;; Selected subject is different from current article's.
10099         (gnus-summary-display-article article)
10100       (gnus-summary-recenter)
10101       (gnus-eval-in-buffer-window gnus-article-buffer
10102                                   (gnus-article-prev-page lines))))
10103   (gnus-summary-position-point))
10104
10105 (defun gnus-summary-scroll-up (lines)
10106   "Scroll up (or down) one line current article.
10107 Argument LINES specifies lines to be scrolled up (or down if negative)."
10108   (interactive "p")
10109   (gnus-set-global-variables)
10110   (gnus-configure-windows 'article)
10111   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10112     (gnus-eval-in-buffer-window
10113      gnus-article-buffer
10114      (cond ((> lines 0)
10115             (if (gnus-article-next-page lines)
10116                 (gnus-message 3 "End of message")))
10117            ((< lines 0)
10118             (gnus-article-prev-page (- lines))))))
10119   (gnus-summary-recenter)
10120   (gnus-summary-position-point))
10121
10122 (defun gnus-summary-next-same-subject ()
10123   "Select next article which has the same subject as current one."
10124   (interactive)
10125   (gnus-set-global-variables)
10126   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10127
10128 (defun gnus-summary-prev-same-subject ()
10129   "Select previous article which has the same subject as current one."
10130   (interactive)
10131   (gnus-set-global-variables)
10132   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10133
10134 (defun gnus-summary-next-unread-same-subject ()
10135   "Select next unread article which has the same subject as current one."
10136   (interactive)
10137   (gnus-set-global-variables)
10138   (gnus-summary-next-article t (gnus-summary-article-subject)))
10139
10140 (defun gnus-summary-prev-unread-same-subject ()
10141   "Select previous unread article which has the same subject as current one."
10142   (interactive)
10143   (gnus-set-global-variables)
10144   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10145
10146 (defun gnus-summary-first-unread-article ()
10147   "Select the first unread article.
10148 Return nil if there are no unread articles."
10149   (interactive)
10150   (gnus-set-global-variables)
10151   (prog1
10152       (if (gnus-summary-first-subject t)
10153           (progn
10154             (gnus-summary-show-thread)
10155             (gnus-summary-first-subject t)
10156             (gnus-summary-display-article (gnus-summary-article-number))))
10157     (gnus-summary-position-point)))
10158
10159 (defun gnus-summary-best-unread-article ()
10160   "Select the unread article with the highest score."
10161   (interactive)
10162   (gnus-set-global-variables)
10163   (let ((best -1000000)
10164         (data gnus-newsgroup-data)
10165         article score)
10166     (while data
10167       (and (gnus-data-unread-p (car data))
10168            (> (setq score
10169                     (gnus-summary-article-score (gnus-data-number (car data))))
10170               best)
10171            (setq best score
10172                  article (gnus-data-number (car data))))
10173       (setq data (cdr data)))
10174     (if article
10175         (gnus-summary-goto-article article)
10176       (error "No unread articles"))
10177     (gnus-summary-position-point)))
10178
10179 (defun gnus-summary-last-subject ()
10180   "Go to the last displayed subject line in the group."
10181   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10182     (when article
10183       (gnus-summary-goto-subject article))))
10184
10185 (defun gnus-summary-goto-article (article &optional all-headers force)
10186   "Fetch ARTICLE and display it if it exists.
10187 If ALL-HEADERS is non-nil, no header lines are hidden."
10188   (interactive
10189    (list
10190     (string-to-int
10191      (completing-read
10192       "Article number: "
10193       (mapcar (lambda (number) (list (int-to-string number)))
10194               gnus-newsgroup-limit)))
10195     current-prefix-arg
10196     t))
10197   (prog1
10198       (if (gnus-summary-goto-subject article force)
10199           (gnus-summary-display-article article all-headers)
10200         (gnus-message 4 "Couldn't go to article %s" article) nil)
10201     (gnus-summary-position-point)))
10202
10203 (defun gnus-summary-goto-last-article ()
10204   "Go to the previously read article."
10205   (interactive)
10206   (prog1
10207       (and gnus-last-article
10208            (gnus-summary-goto-article gnus-last-article))
10209     (gnus-summary-position-point)))
10210
10211 (defun gnus-summary-pop-article (number)
10212   "Pop one article off the history and go to the previous.
10213 NUMBER articles will be popped off."
10214   (interactive "p")
10215   (let (to)
10216     (setq gnus-newsgroup-history
10217           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10218     (if to
10219         (gnus-summary-goto-article (car to))
10220       (error "Article history empty")))
10221   (gnus-summary-position-point))
10222
10223 ;; Summary commands and functions for limiting the summary buffer.
10224
10225 (defun gnus-summary-limit-to-articles (n)
10226   "Limit the summary buffer to the next N articles.
10227 If not given a prefix, use the process marked articles instead."
10228   (interactive "P")
10229   (gnus-set-global-variables)
10230   (prog1
10231       (let ((articles (gnus-summary-work-articles n)))
10232         (setq gnus-newsgroup-processable nil)
10233         (gnus-summary-limit articles))
10234     (gnus-summary-position-point)))
10235
10236 (defun gnus-summary-pop-limit (&optional total)
10237   "Restore the previous limit.
10238 If given a prefix, remove all limits."
10239   (interactive "P")
10240   (gnus-set-global-variables)
10241   (when total 
10242     (setq gnus-newsgroup-limits
10243           (list (mapcar (lambda (h) (mail-header-number h))
10244                         gnus-newsgroup-headers))))
10245   (unless gnus-newsgroup-limits
10246     (error "No limit to pop"))
10247   (prog1
10248       (gnus-summary-limit nil 'pop)
10249     (gnus-summary-position-point)))
10250
10251 (defun gnus-summary-limit-to-subject (subject &optional header)
10252   "Limit the summary buffer to articles that have subjects that match a regexp."
10253   (interactive "sRegexp: ")
10254   (unless header
10255     (setq header "subject"))
10256   (when (not (equal "" subject))
10257     (prog1
10258         (let ((articles (gnus-summary-find-matching
10259                          (or header "subject") subject 'all)))
10260           (or articles (error "Found no matches for \"%s\"" subject))
10261           (gnus-summary-limit articles))
10262       (gnus-summary-position-point))))
10263
10264 (defun gnus-summary-limit-to-author (from)
10265   "Limit the summary buffer to articles that have authors that match a regexp."
10266   (interactive "sRegexp: ")
10267   (gnus-summary-limit-to-subject from "from"))
10268
10269 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10270 (make-obsolete
10271  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10272
10273 (defun gnus-summary-limit-to-unread (&optional all)
10274   "Limit the summary buffer to articles that are not marked as read.
10275 If ALL is non-nil, limit strictly to unread articles."
10276   (interactive "P")
10277   (if all
10278       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10279     (gnus-summary-limit-to-marks
10280      ;; Concat all the marks that say that an article is read and have
10281      ;; those removed.
10282      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10283            gnus-killed-mark gnus-kill-file-mark
10284            gnus-low-score-mark gnus-expirable-mark
10285            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10286      'reverse)))
10287
10288 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10289 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10290
10291 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10292   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10293 If REVERSE, limit the summary buffer to articles that are not marked
10294 with MARKS.  MARKS can either be a string of marks or a list of marks.
10295 Returns how many articles were removed."
10296   (interactive "sMarks: ")
10297   (gnus-set-global-variables)
10298   (prog1
10299       (let ((data gnus-newsgroup-data)
10300             (marks (if (listp marks) marks
10301                      (append marks nil))) ; Transform to list.
10302             articles)
10303         (while data
10304           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10305                  (memq (gnus-data-mark (car data)) marks))
10306                (setq articles (cons (gnus-data-number (car data)) articles)))
10307           (setq data (cdr data)))
10308         (gnus-summary-limit articles))
10309     (gnus-summary-position-point)))
10310
10311 (defun gnus-summary-limit-to-score (&optional score)
10312   "Limit to articles with score at or above SCORE."
10313   (interactive "P")
10314   (gnus-set-global-variables)
10315   (setq score (if score
10316                   (prefix-numeric-value score)
10317                 (or gnus-summary-default-score 0)))
10318   (let ((data gnus-newsgroup-data)
10319         articles)
10320     (while data
10321       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10322                 score)
10323         (push (gnus-data-number (car data)) articles))
10324       (setq data (cdr data)))
10325     (prog1
10326         (gnus-summary-limit articles)
10327       (gnus-summary-position-point))))
10328
10329 (defun gnus-summary-limit-include-dormant ()
10330   "Display all the hidden articles that are marked as dormant."
10331   (interactive)
10332   (gnus-set-global-variables)
10333   (or gnus-newsgroup-dormant
10334       (error "There are no dormant articles in this group"))
10335   (prog1
10336       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10337     (gnus-summary-position-point)))
10338
10339 (defun gnus-summary-limit-exclude-dormant ()
10340   "Hide all dormant articles."
10341   (interactive)
10342   (gnus-set-global-variables)
10343   (prog1
10344       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10345     (gnus-summary-position-point)))
10346
10347 (defun gnus-summary-limit-exclude-childless-dormant ()
10348   "Hide all dormant articles that have no children."
10349   (interactive)
10350   (gnus-set-global-variables)
10351   (let ((data gnus-newsgroup-data)
10352         articles)
10353     ;; Find all articles that are either not dormant or have
10354     ;; children.
10355     (while data
10356       (and (or (not (= (gnus-data-mark (car data)) gnus-dormant-mark))
10357                (gnus-article-parent-p (gnus-data-number (car data))))
10358            (setq articles (cons (gnus-data-number (car data))
10359                                 articles)))
10360       (setq data (cdr data)))
10361     ;; Do the limiting.
10362     (prog1
10363         (gnus-summary-limit articles)
10364       (gnus-summary-position-point))))
10365
10366 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10367   "Mark all unread excluded articles as read.
10368 If ALL, mark even excluded ticked and dormants as read."
10369   (interactive "P")
10370   (let ((articles (gnus-sorted-complement
10371                    (sort
10372                     (mapcar (lambda (h) (mail-header-number h))
10373                             gnus-newsgroup-headers)
10374                     '<)
10375                    (sort gnus-newsgroup-limit '<)))
10376         article)
10377     (setq gnus-newsgroup-unreads nil)
10378     (if all
10379         (setq gnus-newsgroup-dormant nil
10380               gnus-newsgroup-marked nil
10381               gnus-newsgroup-reads
10382               (nconc
10383                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10384                gnus-newsgroup-reads))
10385       (while (setq article (pop articles))
10386         (unless (or (memq article gnus-newsgroup-dormant)
10387                     (memq article gnus-newsgroup-marked))
10388           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10389
10390 (defun gnus-summary-limit (articles &optional pop)
10391   (if pop
10392       ;; We pop the previous limit off the stack and use that.
10393       (setq articles (car gnus-newsgroup-limits)
10394             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10395     ;; We use the new limit, so we push the old limit on the stack.
10396     (setq gnus-newsgroup-limits
10397           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10398   ;; Set the limit.
10399   (setq gnus-newsgroup-limit articles)
10400   (let ((total (length gnus-newsgroup-data))
10401         (data (gnus-data-find-list (gnus-summary-article-number)))
10402         found)
10403     ;; This will do all the work of generating the new summary buffer
10404     ;; according to the new limit.
10405     (gnus-summary-prepare)
10406     ;; Hide any threads, possibly.
10407     (and gnus-show-threads
10408          gnus-thread-hide-subtree
10409          (gnus-summary-hide-all-threads))
10410     ;; Try to return to the article you were at, or one in the
10411     ;; neighborhood.
10412     (if data
10413         ;; We try to find some article after the current one.
10414         (while data
10415           (and (gnus-summary-goto-subject
10416                 (gnus-data-number (car data)) nil t)
10417                (setq data nil
10418                      found t))
10419           (setq data (cdr data))))
10420     (or found
10421         ;; If there is no data, that means that we were after the last
10422         ;; article.  The same goes when we can't find any articles
10423         ;; after the current one.
10424         (progn
10425           (goto-char (point-max))
10426           (gnus-summary-find-prev)))
10427     ;; We return how many articles were removed from the summary
10428     ;; buffer as a result of the new limit.
10429     (- total (length gnus-newsgroup-data))))
10430
10431 (defsubst gnus-cut-thread (thread)
10432   "Go forwards in the thread until we find an article that we want to display."
10433   (if (eq gnus-fetch-old-headers 'some)
10434       (while (and thread
10435                   (memq (mail-header-number (car thread)) 
10436                         gnus-newsgroup-ancient)
10437                   (<= (length (cdr thread)) 1))
10438         (setq thread (cadr thread)))
10439     (while (and thread
10440                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10441                 (= (length (cdr thread)) 1))
10442       (setq thread (cadr thread))))
10443   thread)
10444
10445 (defun gnus-cut-threads (threads)
10446   "Cut off all uninteresting articles from the beginning of threads."
10447   (when (or (eq gnus-fetch-old-headers 'some)
10448             (eq gnus-build-sparse-threads 'some)
10449             (eq gnus-build-sparse-threads 'more))
10450     (let ((th threads))
10451       (while th
10452         (setcar th (gnus-cut-thread (car th)))
10453         (setq th (cdr th)))))
10454   threads)
10455
10456 (defun gnus-summary-initial-limit (&optional show-if-empty)
10457   "Figure out what the initial limit is supposed to be on group entry.
10458 This entails weeding out unwanted dormants, low-scored articles,
10459 fetch-old-headers verbiage, and so on."
10460   ;; Most groups have nothing to remove.
10461   (if (or gnus-inhibit-limiting
10462           (and (null gnus-newsgroup-dormant)
10463                (not (eq gnus-fetch-old-headers 'some))
10464                (null gnus-summary-expunge-below)
10465                (not (eq gnus-build-sparse-threads 'some))
10466                (not (eq gnus-build-sparse-threads 'more))
10467                (null gnus-thread-expunge-below)
10468                (not gnus-use-nocem)))
10469       () ; Do nothing.
10470     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10471     (setq gnus-newsgroup-limit nil)
10472     (mapatoms
10473      (lambda (node)
10474        (unless (car (symbol-value node))
10475          ;; These threads have no parents -- they are roots.
10476          (let ((nodes (cdr (symbol-value node)))
10477                thread)
10478            (while nodes
10479              (if (and gnus-thread-expunge-below
10480                       (< (gnus-thread-total-score (car nodes))
10481                          gnus-thread-expunge-below))
10482                  (gnus-expunge-thread (pop nodes))
10483                (setq thread (pop nodes))
10484                (gnus-summary-limit-children thread))))))
10485      gnus-newsgroup-dependencies)
10486     ;; If this limitation resulted in an empty group, we might
10487     ;; pop the previous limit and use it instead.
10488     (when (and (not gnus-newsgroup-limit)
10489                show-if-empty)
10490       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10491     gnus-newsgroup-limit))
10492
10493 (defun gnus-summary-limit-children (thread)
10494   "Return 1 if this subthread is visible and 0 if it is not."
10495   ;; First we get the number of visible children to this thread.  This
10496   ;; is done by recursing down the thread using this function, so this
10497   ;; will really go down to a leaf article first, before slowly
10498   ;; working its way up towards the root.
10499   (when thread
10500     (let ((children
10501            (if (cdr thread)
10502                (apply '+ (mapcar 'gnus-summary-limit-children
10503                                  (cdr thread)))
10504              0))
10505           (number (mail-header-number (car thread)))
10506           score)
10507       (if (or
10508            ;; If this article is dormant and has absolutely no visible
10509            ;; children, then this article isn't visible.
10510            (and (memq number gnus-newsgroup-dormant)
10511                 (= children 0))
10512            ;; If this is a "fetch-old-headered" and there is only one
10513            ;; visible child (or less), then we don't want this article.
10514            (and (eq gnus-fetch-old-headers 'some)
10515                 (memq number gnus-newsgroup-ancient)
10516                 (zerop children))
10517            ;; If this is a sparsely inserted article with no children,
10518            ;; we don't want it.
10519            (and (eq gnus-build-sparse-threads 'some)
10520                 (memq number gnus-newsgroup-sparse)
10521                 (zerop children))
10522            ;; If we use expunging, and this article is really
10523            ;; low-scored, then we don't want this article.
10524            (when (and gnus-summary-expunge-below
10525                       (< (setq score
10526                                (or (cdr (assq number gnus-newsgroup-scored))
10527                                    gnus-summary-default-score))
10528                          gnus-summary-expunge-below))
10529              ;; We increase the expunge-tally here, but that has
10530              ;; nothing to do with the limits, really.
10531              (incf gnus-newsgroup-expunged-tally)
10532              ;; We also mark as read here, if that's wanted.
10533              (when (and gnus-summary-mark-below
10534                         (< score gnus-summary-mark-below))
10535                (setq gnus-newsgroup-unreads
10536                      (delq number gnus-newsgroup-unreads))
10537                (if gnus-newsgroup-auto-expire
10538                    (push number gnus-newsgroup-expirable)
10539                  (push (cons number gnus-low-score-mark)
10540                        gnus-newsgroup-reads)))
10541              t)
10542            (and gnus-use-nocem
10543                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10544           ;; Nope, invisible article.
10545           0
10546         ;; Ok, this article is to be visible, so we add it to the limit
10547         ;; and return 1.
10548         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10549         1))))
10550
10551 (defun gnus-expunge-thread (thread)
10552   "Mark all articles in THREAD as read."
10553   (let* ((number (mail-header-number (car thread))))
10554     (incf gnus-newsgroup-expunged-tally)
10555     ;; We also mark as read here, if that's wanted.
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   ;; Go recursively through all subthreads.
10563   (mapcar 'gnus-expunge-thread (cdr thread)))
10564
10565 ;; Summary article oriented commands
10566
10567 (defun gnus-summary-refer-parent-article (n)
10568   "Refer parent article N times.
10569 The difference between N and the number of articles fetched is returned."
10570   (interactive "p")
10571   (gnus-set-global-variables)
10572   (while
10573       (and
10574        (> n 0)
10575        (let* ((header (gnus-summary-article-header))
10576               (ref
10577                ;; If we try to find the parent of the currently
10578                ;; displayed article, then we take a look at the actual
10579                ;; References header, since this is slightly more
10580                ;; reliable than the References field we got from the
10581                ;; server.
10582                (if (and (eq (mail-header-number header)
10583                             (cdr gnus-article-current))
10584                         (equal gnus-newsgroup-name
10585                                (car gnus-article-current)))
10586                    (save-excursion
10587                      (set-buffer gnus-original-article-buffer)
10588                      (nnheader-narrow-to-headers)
10589                      (prog1
10590                          (mail-fetch-field "references")
10591                        (widen)))
10592                  ;; It's not the current article, so we take a bet on
10593                  ;; the value we got from the server.
10594                  (mail-header-references header))))
10595          (if (setq ref (or ref (mail-header-references header)))
10596              (or (gnus-summary-refer-article (gnus-parent-id ref))
10597                  (gnus-message 1 "Couldn't find parent"))
10598            (gnus-message 1 "No references in article %d"
10599                          (gnus-summary-article-number))
10600            nil)))
10601     (setq n (1- n)))
10602   (gnus-summary-position-point)
10603   n)
10604
10605 (defun gnus-summary-refer-references ()
10606   "Fetch all articles mentioned in the References header.
10607 Return how many articles were fetched."
10608   (interactive)
10609   (gnus-set-global-variables)
10610   (let ((ref (mail-header-references (gnus-summary-article-header)))
10611         (current (gnus-summary-article-number))
10612         (n 0))
10613     ;; For each Message-ID in the References header...
10614     (while (string-match "<[^>]*>" ref)
10615       (incf n)
10616       ;; ... fetch that article.
10617       (gnus-summary-refer-article
10618        (prog1 (match-string 0 ref)
10619          (setq ref (substring ref (match-end 0))))))
10620     (gnus-summary-goto-subject current)
10621     (gnus-summary-position-point)
10622     n))
10623
10624 (defun gnus-summary-refer-article (message-id)
10625   "Fetch an article specified by MESSAGE-ID."
10626   (interactive "sMessage-ID: ")
10627   (when (and (stringp message-id)
10628              (not (zerop (length message-id))))
10629     ;; Construct the correct Message-ID if necessary.
10630     ;; Suggested by tale@pawl.rpi.edu.
10631     (unless (string-match "^<" message-id)
10632       (setq message-id (concat "<" message-id)))
10633     (unless (string-match ">$" message-id)
10634       (setq message-id (concat message-id ">")))
10635     (let ((header (car (gnus-gethash (downcase message-id)
10636                                      gnus-newsgroup-dependencies))))
10637       (if header
10638           ;; The article is present in the buffer, to we just go to it.
10639           (gnus-summary-goto-article (mail-header-number header) nil t)
10640         ;; We fetch the article
10641         (let ((gnus-override-method gnus-refer-article-method)
10642               number)
10643           ;; Start the special refer-article method, if necessary.
10644           (when gnus-refer-article-method
10645             (gnus-check-server gnus-refer-article-method))
10646           ;; Fetch the header, and display the article.
10647           (if (setq number (gnus-summary-insert-subject message-id))
10648               (gnus-summary-select-article nil nil nil number)
10649             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10650
10651 (defun gnus-summary-enter-digest-group (&optional force)
10652   "Enter a digest group based on the current article."
10653   (interactive "P")
10654   (gnus-set-global-variables)
10655   (gnus-summary-select-article)
10656   (let ((name (format "%s-%d"
10657                       (gnus-group-prefixed-name
10658                        gnus-newsgroup-name (list 'nndoc ""))
10659                       gnus-current-article))
10660         (ogroup gnus-newsgroup-name)
10661         (case-fold-search t)
10662         (buf (current-buffer))
10663         dig)
10664     (save-excursion
10665       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10666       (insert-buffer-substring gnus-original-article-buffer)
10667       (narrow-to-region
10668        (goto-char (point-min))
10669        (or (search-forward "\n\n" nil t) (point)))
10670       (goto-char (point-min))
10671       (delete-matching-lines "^\\(Path\\):\\|^From ")
10672       (widen))
10673     (unwind-protect
10674         (if (gnus-group-read-ephemeral-group
10675              name `(nndoc ,name (nndoc-address
10676                                  ,(get-buffer dig))
10677                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10678             ;; Make all postings to this group go to the parent group.
10679             (setcdr (nthcdr 4 (gnus-get-info name))
10680                     (list (list (cons 'to-group ogroup))))
10681           ;; Couldn't select this doc group.
10682           (switch-to-buffer buf)
10683           (gnus-set-global-variables)
10684           (gnus-configure-windows 'summary)
10685           (gnus-message 3 "Article couldn't be entered?"))
10686       (kill-buffer dig))))
10687
10688 (defun gnus-summary-isearch-article (&optional regexp-p)
10689   "Do incremental search forward on the current article.
10690 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10691   (interactive "P")
10692   (gnus-set-global-variables)
10693   (gnus-summary-select-article)
10694   (gnus-configure-windows 'article)
10695   (gnus-eval-in-buffer-window
10696    gnus-article-buffer
10697    (goto-char (point-min))
10698    (isearch-forward regexp-p)))
10699
10700 (defun gnus-summary-search-article-forward (regexp &optional backward)
10701   "Search for an article containing REGEXP forward.
10702 If BACKWARD, search backward instead."
10703   (interactive
10704    (list (read-string
10705           (format "Search article %s (regexp%s): "
10706                   (if current-prefix-arg "backward" "forward")
10707                   (if gnus-last-search-regexp
10708                       (concat ", default " gnus-last-search-regexp)
10709                     "")))
10710          current-prefix-arg))
10711   (gnus-set-global-variables)
10712   (if (string-equal regexp "")
10713       (setq regexp (or gnus-last-search-regexp ""))
10714     (setq gnus-last-search-regexp regexp))
10715   (if (gnus-summary-search-article regexp backward)
10716       (gnus-article-set-window-start
10717        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10718     (error "Search failed: \"%s\"" regexp)))
10719
10720 (defun gnus-summary-search-article-backward (regexp)
10721   "Search for an article containing REGEXP backward."
10722   (interactive
10723    (list (read-string
10724           (format "Search article backward (regexp%s): "
10725                   (if gnus-last-search-regexp
10726                       (concat ", default " gnus-last-search-regexp)
10727                     "")))))
10728   (gnus-summary-search-article-forward regexp 'backward))
10729
10730 (defun gnus-summary-search-article (regexp &optional backward)
10731   "Search for an article containing REGEXP.
10732 Optional argument BACKWARD means do search for backward.
10733 gnus-select-article-hook is not called during the search."
10734   (let ((gnus-select-article-hook nil)  ;Disable hook.
10735         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10736         (re-search
10737          (if backward
10738              (function re-search-backward) (function re-search-forward)))
10739         (found nil)
10740         (last nil))
10741     ;; Hidden thread subtrees must be searched for ,too.
10742     (gnus-summary-show-all-threads)
10743     ;; First of all, search current article.
10744     ;; We don't want to read article again from NNTP server nor reset
10745     ;; current point.
10746     (gnus-summary-select-article)
10747     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10748     (setq last gnus-current-article)
10749     (gnus-eval-in-buffer-window
10750      gnus-article-buffer
10751      (save-restriction
10752        (widen)
10753        ;; Begin search from current point.
10754        (setq found (funcall re-search regexp nil t))))
10755     ;; Then search next articles.
10756     (while (and (not found)
10757                 (gnus-summary-display-article
10758                  (if backward (gnus-summary-find-prev)
10759                    (gnus-summary-find-next))))
10760       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10761       (gnus-eval-in-buffer-window
10762        gnus-article-buffer
10763        (save-restriction
10764          (widen)
10765          (goto-char (if backward (point-max) (point-min)))
10766          (setq found (funcall re-search regexp nil t)))))
10767     (message "")
10768     ;; Adjust article pointer.
10769     (or (eq last gnus-current-article)
10770         (setq gnus-last-article last))
10771     ;; Return T if found such article.
10772     found))
10773
10774 (defun gnus-summary-find-matching (header regexp &optional backward unread
10775                                           not-case-fold)
10776   "Return a list of all articles that match REGEXP on HEADER.
10777 The search stars on the current article and goes forwards unless
10778 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10779 If UNREAD is non-nil, only unread articles will
10780 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10781 in the comparisons."
10782   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10783                 (gnus-data-find-list
10784                  (gnus-summary-article-number) (gnus-data-list backward))))
10785         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10786         (case-fold-search (not not-case-fold))
10787         articles d)
10788     (or (fboundp (intern (concat "mail-header-" header)))
10789         (error "%s is not a valid header" header))
10790     (while data
10791       (setq d (car data))
10792       (and (or (not unread)             ; We want all articles...
10793                (gnus-data-unread-p d))  ; Or just unreads.
10794            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10795            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10796            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10797       (setq data (cdr data)))
10798     (nreverse articles)))
10799
10800 (defun gnus-summary-execute-command (header regexp command &optional backward)
10801   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10802 If HEADER is an empty string (or nil), the match is done on the entire
10803 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10804   (interactive
10805    (list (let ((completion-ignore-case t))
10806            (completing-read
10807             "Header name: "
10808             (mapcar (lambda (string) (list string))
10809                     '("Number" "Subject" "From" "Lines" "Date"
10810                       "Message-ID" "Xref" "References" "Body"))
10811             nil 'require-match))
10812          (read-string "Regexp: ")
10813          (read-key-sequence "Command: ")
10814          current-prefix-arg))
10815   (when (equal header "Body")
10816     (setq header ""))
10817   (gnus-set-global-variables)
10818   ;; Hidden thread subtrees must be searched as well.
10819   (gnus-summary-show-all-threads)
10820   ;; We don't want to change current point nor window configuration.
10821   (save-excursion
10822     (save-window-excursion
10823       (gnus-message 6 "Executing %s..." (key-description command))
10824       ;; We'd like to execute COMMAND interactively so as to give arguments.
10825       (gnus-execute header regexp
10826                     `(lambda () (call-interactively ',(key-binding command)))
10827                     backward)
10828       (gnus-message 6 "Executing %s...done" (key-description command)))))
10829
10830 (defun gnus-summary-beginning-of-article ()
10831   "Scroll the article back to the beginning."
10832   (interactive)
10833   (gnus-set-global-variables)
10834   (gnus-summary-select-article)
10835   (gnus-configure-windows 'article)
10836   (gnus-eval-in-buffer-window
10837    gnus-article-buffer
10838    (widen)
10839    (goto-char (point-min))
10840    (and gnus-break-pages (gnus-narrow-to-page))))
10841
10842 (defun gnus-summary-end-of-article ()
10843   "Scroll to the end of the article."
10844   (interactive)
10845   (gnus-set-global-variables)
10846   (gnus-summary-select-article)
10847   (gnus-configure-windows 'article)
10848   (gnus-eval-in-buffer-window
10849    gnus-article-buffer
10850    (widen)
10851    (goto-char (point-max))
10852    (recenter -3)
10853    (and gnus-break-pages (gnus-narrow-to-page))))
10854
10855 (defun gnus-summary-show-article (&optional arg)
10856   "Force re-fetching of the current article.
10857 If ARG (the prefix) is non-nil, show the raw article without any
10858 article massaging functions being run."
10859   (interactive "P")
10860   (gnus-set-global-variables)
10861   (if (not arg)
10862       ;; Select the article the normal way.
10863       (gnus-summary-select-article nil 'force)
10864     ;; Bind the article treatment functions to nil.
10865     (let ((gnus-have-all-headers t)
10866           gnus-article-display-hook
10867           gnus-article-prepare-hook
10868           gnus-visual)
10869       (gnus-summary-select-article nil 'force)))
10870 ;  (gnus-configure-windows 'article)
10871   (gnus-summary-position-point))
10872
10873 (defun gnus-summary-verbose-headers (&optional arg)
10874   "Toggle permanent full header display.
10875 If ARG is a positive number, turn header display on.
10876 If ARG is a negative number, turn header display off."
10877   (interactive "P")
10878   (gnus-set-global-variables)
10879   (gnus-summary-toggle-header arg)
10880   (setq gnus-show-all-headers
10881         (cond ((or (not (numberp arg))
10882                    (zerop arg))
10883                (not gnus-show-all-headers))
10884               ((natnump arg)
10885                t))))
10886
10887 (defun gnus-summary-toggle-header (&optional arg)
10888   "Show the headers if they are hidden, or hide them if they are shown.
10889 If ARG is a positive number, show the entire header.
10890 If ARG is a negative number, hide the unwanted header lines."
10891   (interactive "P")
10892   (gnus-set-global-variables)
10893   (save-excursion
10894     (set-buffer gnus-article-buffer)
10895     (let* ((buffer-read-only nil)
10896            (inhibit-point-motion-hooks t)
10897            (hidden (text-property-any
10898                     (goto-char (point-min)) (search-forward "\n\n")
10899                     'invisible t))
10900            e)
10901       (goto-char (point-min))
10902       (when (search-forward "\n\n" nil t)
10903         (delete-region (point-min) (1- (point))))
10904       (goto-char (point-min))
10905       (save-excursion
10906         (set-buffer gnus-original-article-buffer)
10907         (goto-char (point-min))
10908         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10909       (insert-buffer-substring gnus-original-article-buffer 1 e)
10910       (let ((gnus-inhibit-hiding t))
10911         (run-hooks 'gnus-article-display-hook))
10912       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10913           (gnus-article-hide-headers)))))
10914
10915 (defun gnus-summary-show-all-headers ()
10916   "Make all header lines visible."
10917   (interactive)
10918   (gnus-set-global-variables)
10919   (gnus-article-show-all-headers))
10920
10921 (defun gnus-summary-toggle-mime (&optional arg)
10922   "Toggle MIME processing.
10923 If ARG is a positive number, turn MIME processing on."
10924   (interactive "P")
10925   (gnus-set-global-variables)
10926   (setq gnus-show-mime
10927         (if (null arg) (not gnus-show-mime)
10928           (> (prefix-numeric-value arg) 0)))
10929   (gnus-summary-select-article t 'force))
10930
10931 (defun gnus-summary-caesar-message (&optional arg)
10932   "Caesar rotate the current article by 13.
10933 The numerical prefix specifies how manu places to rotate each letter
10934 forward."
10935   (interactive "P")
10936   (gnus-set-global-variables)
10937   (gnus-summary-select-article)
10938   (let ((mail-header-separator ""))
10939     (gnus-eval-in-buffer-window
10940      gnus-article-buffer
10941      (save-restriction
10942        (widen)
10943        (let ((start (window-start)))
10944          (news-caesar-buffer-body arg)
10945          (set-window-start (get-buffer-window (current-buffer)) start))))))
10946
10947 (defun gnus-summary-stop-page-breaking ()
10948   "Stop page breaking in the current article."
10949   (interactive)
10950   (gnus-set-global-variables)
10951   (gnus-summary-select-article)
10952   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
10953
10954 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
10955   "Move the current article to a different newsgroup.
10956 If N is a positive number, move the N next articles.
10957 If N is a negative number, move the N previous articles.
10958 If N is nil and any articles have been marked with the process mark,
10959 move those articles instead.
10960 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
10961 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10962 re-spool using this method.
10963
10964 For this function to work, both the current newsgroup and the
10965 newsgroup that you want to move to have to support the `request-move'
10966 and `request-accept' functions."
10967   (interactive "P")
10968   (unless action (setq action 'move))
10969   (gnus-set-global-variables)
10970   ;; Check whether the source group supports the required functions.
10971   (cond ((and (eq action 'move)
10972               (not (gnus-check-backend-function
10973                     'request-move-article gnus-newsgroup-name)))
10974          (error "The current group does not support article moving"))
10975         ((and (eq action 'crosspost)
10976               (not (gnus-check-backend-function
10977                     'request-replace-article gnus-newsgroup-name)))
10978          (error "The current group does not support article editing")))
10979   (let ((articles (gnus-summary-work-articles n))
10980         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10981         (names '((move "move" "Moving")
10982                  (copy "copy" "Copying")
10983                  (crosspost "crosspost" "Crossposting")))
10984         (copy-buf (save-excursion
10985                     (nnheader-set-temp-buffer " *copy article*")))
10986         art-group to-method new-xref article)
10987     (unless (assq action names)
10988       (error "Unknown action %s" action))
10989     ;; Read the newsgroup name.
10990     (when (and (not to-newsgroup)
10991                (not select-method))
10992       (setq to-newsgroup
10993             (gnus-read-move-group-name
10994              (cadr (assq action names))
10995              gnus-current-move-group articles prefix))
10996       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
10997     (setq to-method (if select-method (list select-method "")
10998                       (gnus-find-method-for-group to-newsgroup)))
10999     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11000     ;;(error "Can't %s to the same group you're already in" action))
11001     ;; Check the method we are to move this article to...
11002     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11003         (error "%s does not support article copying" (car to-method)))
11004     (or (gnus-check-server to-method)
11005         (error "Can't open server %s" (car to-method)))
11006     (gnus-message 6 "%s to %s: %s..."
11007                   (caddr (assq action names))
11008                   (or select-method to-newsgroup) articles)
11009     (while articles
11010       (setq article (pop articles))
11011       (setq
11012        art-group
11013        (cond
11014         ;; Move the article.
11015         ((eq action 'move)
11016          (gnus-request-move-article
11017           article                       ; Article to move
11018           gnus-newsgroup-name           ; From newsgrouo
11019           (nth 1 (gnus-find-method-for-group
11020                   gnus-newsgroup-name)) ; Server
11021           (list 'gnus-request-accept-article
11022                 (if select-method
11023                     (list 'quote select-method)
11024                   to-newsgroup)
11025                 (not articles))         ; Accept form
11026           (not articles)))              ; Only save nov last time
11027         ;; Copy the article.
11028         ((eq action 'copy)
11029          (save-excursion
11030            (set-buffer copy-buf)
11031            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11032            (gnus-request-accept-article
11033             (if select-method select-method to-newsgroup)
11034             (not articles))))
11035         ;; Crosspost the article.
11036         ((eq action 'crosspost)
11037          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11038            (setq new-xref (concat gnus-newsgroup-name ":" article))
11039            (if (and xref (not (string= xref "")))
11040                (progn
11041                  (when (string-match "^Xref: " xref)
11042                    (setq xref (substring xref (match-end 0))))
11043                  (setq new-xref (concat xref " " new-xref)))
11044              (setq new-xref (concat (system-name) " " new-xref)))
11045            (save-excursion
11046              (set-buffer copy-buf)
11047              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11048              (nnheader-replace-header "xref" new-xref)
11049              (gnus-request-accept-article
11050               (if select-method select-method to-newsgroup)
11051               (not articles)))))))
11052       (if (not art-group)
11053           (gnus-message 1 "Couldn't %s article %s"
11054                         (cadr (assq action names)) article)
11055         (let* ((entry
11056                 (or
11057                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11058                  (gnus-gethash
11059                   (gnus-group-prefixed-name
11060                    (car art-group)
11061                    (if select-method (list select-method "")
11062                      (gnus-find-method-for-group to-newsgroup)))
11063                   gnus-newsrc-hashtb)))
11064                (info (nth 2 entry)))
11065           ;; Update the group that has been moved to.
11066           (when (and info
11067                      (memq action '(move copy)))
11068             (unless (memq article gnus-newsgroup-unreads)
11069               (gnus-info-set-read
11070                info (gnus-add-to-range (gnus-info-read info)
11071                                        (list (cdr art-group)))))
11072
11073             ;; Copy any marks over to the new group.
11074             (let ((marks gnus-article-mark-lists)
11075                   (to-article (cdr art-group)))
11076
11077               ;; See whether the article is to be put in the cache.
11078               (when gnus-use-cache
11079                 (gnus-cache-possibly-enter-article
11080                  (gnus-info-group info) to-article
11081                  (let ((header (copy-sequence
11082                                 (gnus-summary-article-header article))))
11083                    (mail-header-set-number header to-article)
11084                    header)
11085                  (memq article gnus-newsgroup-marked)
11086                  (memq article gnus-newsgroup-dormant)
11087                  (memq article gnus-newsgroup-unreads)))
11088
11089               (while marks
11090                 (when (memq article (symbol-value
11091                                      (intern (format "gnus-newsgroup-%s"
11092                                                      (caar marks)))))
11093                   (gnus-add-marked-articles
11094                    (gnus-info-group info) (cdar marks)
11095                    (list to-article) info))
11096                 (setq marks (cdr marks)))))
11097
11098           ;; Update the Xref header in this article to point to
11099           ;; the new crossposted article we have just created.
11100           (when (eq action 'crosspost)
11101             (save-excursion
11102               (set-buffer copy-buf)
11103               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11104               (nnheader-replace-header
11105                "xref" (concat new-xref " " (gnus-group-prefixed-name
11106                                             (car art-group) to-method)
11107                               ":" (cdr art-group)))
11108               (gnus-request-replace-article
11109                article gnus-newsgroup-name (current-buffer)))))
11110
11111         (gnus-summary-goto-subject article)
11112         (when (eq action 'move)
11113           (gnus-summary-mark-article article gnus-canceled-mark)))
11114       (gnus-summary-remove-process-mark article))
11115     (gnus-kill-buffer copy-buf)
11116     (gnus-summary-position-point)
11117     (gnus-set-mode-line 'summary)))
11118
11119 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11120   "Move the current article to a different newsgroup.
11121 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11122 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11123 re-spool using this method."
11124   (interactive "P")
11125   (gnus-summary-move-article n nil nil 'copy))
11126
11127 (defun gnus-summary-crosspost-article (&optional n)
11128   "Crosspost the current article to some other group."
11129   (interactive "P")
11130   (gnus-summary-move-article n nil nil 'crosspost))
11131
11132 (defun gnus-summary-respool-article (&optional n respool-method)
11133   "Respool the current article.
11134 The article will be squeezed through the mail spooling process again,
11135 which means that it will be put in some mail newsgroup or other
11136 depending on `nnmail-split-methods'.
11137 If N is a positive number, respool the N next articles.
11138 If N is a negative number, respool the N previous articles.
11139 If N is nil and any articles have been marked with the process mark,
11140 respool those articles instead.
11141
11142 Respooling can be done both from mail groups and \"real\" newsgroups.
11143 In the former case, the articles in question will be moved from the
11144 current group into whatever groups they are destined to.  In the
11145 latter case, they will be copied into the relevant groups."
11146   (interactive "P")
11147   (gnus-set-global-variables)
11148   (let ((respool-methods (gnus-methods-using 'respool))
11149         (methname
11150          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11151     (or respool-method
11152         (setq respool-method
11153               (completing-read
11154                "What method do you want to use when respooling? "
11155                respool-methods nil t methname)))
11156     (or (string= respool-method "")
11157         (if (assoc (symbol-name
11158                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11159                    respool-methods)
11160             (gnus-summary-move-article n nil (intern respool-method))
11161           (gnus-summary-copy-article n nil (intern respool-method))))))
11162
11163 (defun gnus-summary-import-article (file)
11164   "Import a random file into a mail newsgroup."
11165   (interactive "fImport file: ")
11166   (gnus-set-global-variables)
11167   (let ((group gnus-newsgroup-name)
11168         (now (current-time))
11169         atts lines)
11170     (or (gnus-check-backend-function 'request-accept-article group)
11171         (error "%s does not support article importing" group))
11172     (or (file-readable-p file)
11173         (not (file-regular-p file))
11174         (error "Can't read %s" file))
11175     (save-excursion
11176       (set-buffer (get-buffer-create " *import file*"))
11177       (buffer-disable-undo (current-buffer))
11178       (erase-buffer)
11179       (insert-file-contents file)
11180       (goto-char (point-min))
11181       (unless (nnheader-article-p)
11182         ;; This doesn't look like an article, so we fudge some headers.
11183         (setq atts (file-attributes file)
11184               lines (count-lines (point-min) (point-max)))
11185         (insert "From: " (read-string "From: ") "\n"
11186                 "Subject: " (read-string "Subject: ") "\n"
11187                 "Date: " (timezone-make-date-arpa-standard
11188                           (current-time-string (nth 5 atts))
11189                           (current-time-zone now)
11190                           (current-time-zone now)) "\n"
11191                 "Message-ID: " (gnus-inews-message-id) "\n"
11192                 "Lines: " (int-to-string lines) "\n"
11193                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11194       (gnus-request-accept-article group t)
11195       (kill-buffer (current-buffer)))))
11196
11197 (defun gnus-summary-expire-articles ()
11198   "Expire all articles that are marked as expirable in the current group."
11199   (interactive)
11200   (gnus-set-global-variables)
11201   (when (gnus-check-backend-function
11202          'request-expire-articles gnus-newsgroup-name)
11203     ;; This backend supports expiry.
11204     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11205            (expirable (if total
11206                           (gnus-list-of-read-articles gnus-newsgroup-name)
11207                         (setq gnus-newsgroup-expirable
11208                               (sort gnus-newsgroup-expirable '<))))
11209            (expiry-wait (gnus-group-get-parameter
11210                          gnus-newsgroup-name 'expiry-wait))
11211            es)
11212       (when expirable
11213         ;; There are expirable articles in this group, so we run them
11214         ;; through the expiry process.
11215         (gnus-message 6 "Expiring articles...")
11216         ;; The list of articles that weren't expired is returned.
11217         (if expiry-wait
11218             (let ((nnmail-expiry-wait-function nil)
11219                   (nnmail-expiry-wait expiry-wait))
11220               (setq es (gnus-request-expire-articles
11221                         expirable gnus-newsgroup-name)))
11222           (setq es (gnus-request-expire-articles
11223                     expirable gnus-newsgroup-name)))
11224         (or total (setq gnus-newsgroup-expirable es))
11225         ;; We go through the old list of expirable, and mark all
11226         ;; really expired articles as nonexistent.
11227         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11228           (let ((gnus-use-cache nil))
11229             (while expirable
11230               (unless (memq (car expirable) es)
11231                 (when (gnus-data-find (car expirable))
11232                   (gnus-summary-mark-article
11233                    (car expirable) gnus-canceled-mark)))
11234               (setq expirable (cdr expirable)))))
11235         (gnus-message 6 "Expiring articles...done")))))
11236
11237 (defun gnus-summary-expire-articles-now ()
11238   "Expunge all expirable articles in the current group.
11239 This means that *all* articles that are marked as expirable will be
11240 deleted forever, right now."
11241   (interactive)
11242   (gnus-set-global-variables)
11243   (or gnus-expert-user
11244       (gnus-y-or-n-p
11245        "Are you really, really, really sure you want to expunge? ")
11246       (error "Phew!"))
11247   (let ((nnmail-expiry-wait 'immediate)
11248         (nnmail-expiry-wait-function nil))
11249     (gnus-summary-expire-articles)))
11250
11251 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11252 (defun gnus-summary-delete-article (&optional n)
11253   "Delete the N next (mail) articles.
11254 This command actually deletes articles.  This is not a marking
11255 command.  The article will disappear forever from your life, never to
11256 return.
11257 If N is negative, delete backwards.
11258 If N is nil and articles have been marked with the process mark,
11259 delete these instead."
11260   (interactive "P")
11261   (gnus-set-global-variables)
11262   (or (gnus-check-backend-function 'request-expire-articles
11263                                    gnus-newsgroup-name)
11264       (error "The current newsgroup does not support article deletion."))
11265   ;; Compute the list of articles to delete.
11266   (let ((articles (gnus-summary-work-articles n))
11267         not-deleted)
11268     (if (and gnus-novice-user
11269              (not (gnus-y-or-n-p
11270                    (format "Do you really want to delete %s forever? "
11271                            (if (> (length articles) 1) "these articles"
11272                              "this article")))))
11273         ()
11274       ;; Delete the articles.
11275       (setq not-deleted (gnus-request-expire-articles
11276                          articles gnus-newsgroup-name 'force))
11277       (while articles
11278         (gnus-summary-remove-process-mark (car articles))
11279         ;; The backend might not have been able to delete the article
11280         ;; after all.
11281         (or (memq (car articles) not-deleted)
11282             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11283         (setq articles (cdr articles))))
11284     (gnus-summary-position-point)
11285     (gnus-set-mode-line 'summary)
11286     not-deleted))
11287
11288 (defun gnus-summary-edit-article (&optional force)
11289   "Enter into a buffer and edit the current article.
11290 This will have permanent effect only in mail groups.
11291 If FORCE is non-nil, allow editing of articles even in read-only
11292 groups."
11293   (interactive "P")
11294   (save-excursion
11295     (set-buffer gnus-summary-buffer)
11296     (gnus-set-global-variables)
11297     (when (and (not force)
11298                (gnus-group-read-only-p))
11299       (error "The current newsgroup does not support article editing."))
11300     (gnus-summary-select-article t nil t)
11301     (gnus-configure-windows 'article)
11302     (select-window (get-buffer-window gnus-article-buffer))
11303     (gnus-message 6 "C-c C-c to end edits")
11304     (setq buffer-read-only nil)
11305     (text-mode)
11306     (use-local-map (copy-keymap (current-local-map)))
11307     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11308     (buffer-enable-undo)
11309     (widen)
11310     (goto-char (point-min))
11311     (search-forward "\n\n" nil t)))
11312
11313 (defun gnus-summary-edit-article-done ()
11314   "Make edits to the current article permanent."
11315   (interactive)
11316   (if (gnus-group-read-only-p)
11317       (progn
11318         (gnus-summary-edit-article-postpone)
11319         (gnus-message
11320          1 "The current newsgroup does not support article editing.")
11321         (ding))
11322     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11323       (erase-buffer)
11324       (insert buf)
11325       (if (not (gnus-request-replace-article
11326                 (cdr gnus-article-current) (car gnus-article-current)
11327                 (current-buffer)))
11328           (error "Couldn't replace article.")
11329         (gnus-article-mode)
11330         (use-local-map gnus-article-mode-map)
11331         (setq buffer-read-only t)
11332         (buffer-disable-undo (current-buffer))
11333         (gnus-configure-windows 'summary)
11334         (gnus-summary-update-article (cdr gnus-article-current)))
11335       (run-hooks 'gnus-article-display-hook)
11336       (and (gnus-visual-p 'summary-highlight 'highlight)
11337            (run-hooks 'gnus-visual-mark-article-hook)))))
11338
11339 (defun gnus-summary-edit-article-postpone ()
11340   "Postpone changes to the current article."
11341   (interactive)
11342   (gnus-article-mode)
11343   (use-local-map gnus-article-mode-map)
11344   (setq buffer-read-only t)
11345   (buffer-disable-undo (current-buffer))
11346   (gnus-configure-windows 'summary)
11347   (and (gnus-visual-p 'summary-highlight 'highlight)
11348        (run-hooks 'gnus-visual-mark-article-hook)))
11349
11350 (defun gnus-summary-respool-query ()
11351   "Query where the respool algorithm would put this article."
11352   (interactive)
11353   (gnus-set-global-variables)
11354   (gnus-summary-select-article)
11355   (save-excursion
11356     (set-buffer gnus-article-buffer)
11357     (save-restriction
11358       (goto-char (point-min))
11359       (search-forward "\n\n")
11360       (narrow-to-region (point-min) (point))
11361       (pp-eval-expression
11362        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11363
11364 ;; Summary score commands.
11365
11366 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11367
11368 (defun gnus-summary-raise-score (n)
11369   "Raise the score of the current article by N."
11370   (interactive "p")
11371   (gnus-set-global-variables)
11372   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11373
11374 (defun gnus-summary-set-score (n)
11375   "Set the score of the current article to N."
11376   (interactive "p")
11377   (gnus-set-global-variables)
11378   (save-excursion
11379     (gnus-summary-show-thread)
11380     (let ((buffer-read-only nil))
11381       ;; Set score.
11382       (gnus-summary-update-mark
11383        (if (= n (or gnus-summary-default-score 0)) ? 
11384          (if (< n (or gnus-summary-default-score 0))
11385              gnus-score-below-mark gnus-score-over-mark)) 'score))
11386     (let* ((article (gnus-summary-article-number))
11387            (score (assq article gnus-newsgroup-scored)))
11388       (if score (setcdr score n)
11389         (setq gnus-newsgroup-scored
11390               (cons (cons article n) gnus-newsgroup-scored))))
11391     (gnus-summary-update-line)))
11392
11393 (defun gnus-summary-current-score ()
11394   "Return the score of the current article."
11395   (interactive)
11396   (gnus-set-global-variables)
11397   (message "%s" (gnus-summary-article-score)))
11398
11399 ;; Summary marking commands.
11400
11401 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11402   "Mark articles which has the same subject as read, and then select the next.
11403 If UNMARK is positive, remove any kind of mark.
11404 If UNMARK is negative, tick articles."
11405   (interactive "P")
11406   (gnus-set-global-variables)
11407   (if unmark
11408       (setq unmark (prefix-numeric-value unmark)))
11409   (let ((count
11410          (gnus-summary-mark-same-subject
11411           (gnus-summary-article-subject) unmark)))
11412     ;; Select next unread article.  If auto-select-same mode, should
11413     ;; select the first unread article.
11414     (gnus-summary-next-article t (and gnus-auto-select-same
11415                                       (gnus-summary-article-subject)))
11416     (gnus-message 7 "%d article%s marked as %s"
11417                   count (if (= count 1) " is" "s are")
11418                   (if unmark "unread" "read"))))
11419
11420 (defun gnus-summary-kill-same-subject (&optional unmark)
11421   "Mark articles which has the same subject as read.
11422 If UNMARK is positive, remove any kind of mark.
11423 If UNMARK is negative, tick articles."
11424   (interactive "P")
11425   (gnus-set-global-variables)
11426   (if unmark
11427       (setq unmark (prefix-numeric-value unmark)))
11428   (let ((count
11429          (gnus-summary-mark-same-subject
11430           (gnus-summary-article-subject) unmark)))
11431     ;; If marked as read, go to next unread subject.
11432     (if (null unmark)
11433         ;; Go to next unread subject.
11434         (gnus-summary-next-subject 1 t))
11435     (gnus-message 7 "%d articles are marked as %s"
11436                   count (if unmark "unread" "read"))))
11437
11438 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11439   "Mark articles with same SUBJECT as read, and return marked number.
11440 If optional argument UNMARK is positive, remove any kinds of marks.
11441 If optional argument UNMARK is negative, mark articles as unread instead."
11442   (let ((count 1))
11443     (save-excursion
11444       (cond
11445        ((null unmark)                   ; Mark as read.
11446         (while (and
11447                 (progn
11448                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11449                   (gnus-summary-show-thread) t)
11450                 (gnus-summary-find-subject subject))
11451           (setq count (1+ count))))
11452        ((> unmark 0)                    ; Tick.
11453         (while (and
11454                 (progn
11455                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11456                   (gnus-summary-show-thread) t)
11457                 (gnus-summary-find-subject subject))
11458           (setq count (1+ count))))
11459        (t                               ; Mark as unread.
11460         (while (and
11461                 (progn
11462                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11463                   (gnus-summary-show-thread) t)
11464                 (gnus-summary-find-subject subject))
11465           (setq count (1+ count)))))
11466       (gnus-set-mode-line 'summary)
11467       ;; Return the number of marked articles.
11468       count)))
11469
11470 (defun gnus-summary-mark-as-processable (n &optional unmark)
11471   "Set the process mark on the next N articles.
11472 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11473 the process mark instead.  The difference between N and the actual
11474 number of articles marked is returned."
11475   (interactive "p")
11476   (gnus-set-global-variables)
11477   (let ((backward (< n 0))
11478         (n (abs n)))
11479     (while (and
11480             (> n 0)
11481             (if unmark
11482                 (gnus-summary-remove-process-mark
11483                  (gnus-summary-article-number))
11484               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11485             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11486       (setq n (1- n)))
11487     (if (/= 0 n) (gnus-message 7 "No more articles"))
11488     (gnus-summary-recenter)
11489     (gnus-summary-position-point)
11490     n))
11491
11492 (defun gnus-summary-unmark-as-processable (n)
11493   "Remove the process mark from the next N articles.
11494 If N is negative, mark backward instead.  The difference between N and
11495 the actual number of articles marked is returned."
11496   (interactive "p")
11497   (gnus-set-global-variables)
11498   (gnus-summary-mark-as-processable n t))
11499
11500 (defun gnus-summary-unmark-all-processable ()
11501   "Remove the process mark from all articles."
11502   (interactive)
11503   (gnus-set-global-variables)
11504   (save-excursion
11505     (while gnus-newsgroup-processable
11506       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11507   (gnus-summary-position-point))
11508
11509 (defun gnus-summary-mark-as-expirable (n)
11510   "Mark N articles forward as expirable.
11511 If N is negative, mark backward instead.  The difference between N and
11512 the actual number of articles marked is returned."
11513   (interactive "p")
11514   (gnus-set-global-variables)
11515   (gnus-summary-mark-forward n gnus-expirable-mark))
11516
11517 (defun gnus-summary-mark-article-as-replied (article)
11518   "Mark ARTICLE replied and update the summary line."
11519   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11520   (let ((buffer-read-only nil))
11521     (when (gnus-summary-goto-subject article)
11522       (gnus-summary-update-secondary-mark article))))
11523
11524 (defun gnus-summary-set-bookmark (article)
11525   "Set a bookmark in current article."
11526   (interactive (list (gnus-summary-article-number)))
11527   (gnus-set-global-variables)
11528   (if (or (not (get-buffer gnus-article-buffer))
11529           (not gnus-current-article)
11530           (not gnus-article-current)
11531           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11532       (error "No current article selected"))
11533   ;; Remove old bookmark, if one exists.
11534   (let ((old (assq article gnus-newsgroup-bookmarks)))
11535     (if old (setq gnus-newsgroup-bookmarks
11536                   (delq old gnus-newsgroup-bookmarks))))
11537   ;; Set the new bookmark, which is on the form
11538   ;; (article-number . line-number-in-body).
11539   (setq gnus-newsgroup-bookmarks
11540         (cons
11541          (cons article
11542                (save-excursion
11543                  (set-buffer gnus-article-buffer)
11544                  (count-lines
11545                   (min (point)
11546                        (save-excursion
11547                          (goto-char (point-min))
11548                          (search-forward "\n\n" nil t)
11549                          (point)))
11550                   (point))))
11551          gnus-newsgroup-bookmarks))
11552   (gnus-message 6 "A bookmark has been added to the current article."))
11553
11554 (defun gnus-summary-remove-bookmark (article)
11555   "Remove the bookmark from the current article."
11556   (interactive (list (gnus-summary-article-number)))
11557   (gnus-set-global-variables)
11558   ;; Remove old bookmark, if one exists.
11559   (let ((old (assq article gnus-newsgroup-bookmarks)))
11560     (if old
11561         (progn
11562           (setq gnus-newsgroup-bookmarks
11563                 (delq old gnus-newsgroup-bookmarks))
11564           (gnus-message 6 "Removed bookmark."))
11565       (gnus-message 6 "No bookmark in current article."))))
11566
11567 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11568 (defun gnus-summary-mark-as-dormant (n)
11569   "Mark N articles forward as dormant.
11570 If N is negative, mark backward instead.  The difference between N and
11571 the actual number of articles marked is returned."
11572   (interactive "p")
11573   (gnus-set-global-variables)
11574   (gnus-summary-mark-forward n gnus-dormant-mark))
11575
11576 (defun gnus-summary-set-process-mark (article)
11577   "Set the process mark on ARTICLE and update the summary line."
11578   (setq gnus-newsgroup-processable
11579         (cons article
11580               (delq article gnus-newsgroup-processable)))
11581   (when (gnus-summary-goto-subject article)
11582     (gnus-summary-show-thread)
11583     (gnus-summary-update-secondary-mark article)))
11584
11585 (defun gnus-summary-remove-process-mark (article)
11586   "Remove the process mark from ARTICLE and update the summary line."
11587   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11588   (when (gnus-summary-goto-subject article)
11589     (gnus-summary-show-thread)
11590     (gnus-summary-update-secondary-mark article)))
11591
11592 (defun gnus-summary-set-saved-mark (article)
11593   "Set the process mark on ARTICLE and update the summary line."
11594   (push article gnus-newsgroup-saved)
11595   (when (gnus-summary-goto-subject article)
11596     (gnus-summary-update-secondary-mark article)))
11597
11598 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11599   "Mark N articles as read forwards.
11600 If N is negative, mark backwards instead.
11601 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11602 marked as unread.
11603 The difference between N and the actual number of articles marked is
11604 returned."
11605   (interactive "p")
11606   (gnus-set-global-variables)
11607   (let ((backward (< n 0))
11608         (gnus-summary-goto-unread
11609          (and gnus-summary-goto-unread
11610               (not (eq gnus-summary-goto-unread 'never))
11611               (not (memq mark (list gnus-unread-mark
11612                                     gnus-ticked-mark gnus-dormant-mark)))))
11613         (n (abs n))
11614         (mark (or mark gnus-del-mark)))
11615     (while (and (> n 0)
11616                 (gnus-summary-mark-article nil mark no-expire)
11617                 (zerop (gnus-summary-next-subject
11618                         (if backward -1 1)
11619                         (and gnus-summary-goto-unread
11620                              (not (eq gnus-summary-goto-unread 'never)))
11621                         t)))
11622       (setq n (1- n)))
11623     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11624     (gnus-summary-recenter)
11625     (gnus-summary-position-point)
11626     (gnus-set-mode-line 'summary)
11627     n))
11628
11629 (defun gnus-summary-mark-article-as-read (mark)
11630   "Mark the current article quickly as read with MARK."
11631   (let ((article (gnus-summary-article-number)))
11632     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11633     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11634     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11635     (setq gnus-newsgroup-reads
11636           (cons (cons article mark) gnus-newsgroup-reads))
11637     ;; Possibly remove from cache, if that is used.
11638     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11639     ;; Allow the backend to change the mark.
11640     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11641     ;; Check for auto-expiry.
11642     (when (and gnus-newsgroup-auto-expire
11643                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11644                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11645                    (= mark gnus-ancient-mark)
11646                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11647       (setq mark gnus-expirable-mark)
11648       (push article gnus-newsgroup-expirable))
11649     ;; Set the mark in the buffer.
11650     (gnus-summary-update-mark mark 'unread)
11651     t))
11652
11653 (defun gnus-summary-mark-article-as-unread (mark)
11654   "Mark the current article quickly as unread with MARK."
11655   (let ((article (gnus-summary-article-number)))
11656     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11657     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11658     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11659     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11660     (cond ((= mark gnus-ticked-mark)
11661            (push article gnus-newsgroup-marked))
11662           ((= mark gnus-dormant-mark)
11663            (push article gnus-newsgroup-dormant))
11664           (t
11665            (push article gnus-newsgroup-unreads)))
11666     (setq gnus-newsgroup-reads
11667           (delq (assq article gnus-newsgroup-reads)
11668                 gnus-newsgroup-reads))
11669
11670     ;; See whether the article is to be put in the cache.
11671     (and gnus-use-cache
11672          (vectorp (gnus-summary-article-header article))
11673          (save-excursion
11674            (gnus-cache-possibly-enter-article
11675             gnus-newsgroup-name article
11676             (gnus-summary-article-header article)
11677             (= mark gnus-ticked-mark)
11678             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11679
11680     ;; Fix the mark.
11681     (gnus-summary-update-mark mark 'unread)
11682     t))
11683
11684 (defun gnus-summary-mark-article (&optional article mark no-expire)
11685   "Mark ARTICLE with MARK.  MARK can be any character.
11686 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11687 `??' (dormant) and `?E' (expirable).
11688 If MARK is nil, then the default character `?D' is used.
11689 If ARTICLE is nil, then the article on the current line will be
11690 marked."
11691   ;; The mark might be a string.
11692   (and (stringp mark)
11693        (setq mark (aref mark 0)))
11694   ;; If no mark is given, then we check auto-expiring.
11695   (and (not no-expire)
11696        gnus-newsgroup-auto-expire
11697        (or (not mark)
11698            (and (numberp mark)
11699                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11700                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11701                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11702        (setq mark gnus-expirable-mark))
11703   (let* ((mark (or mark gnus-del-mark))
11704          (article (or article (gnus-summary-article-number))))
11705     (or article (error "No article on current line"))
11706     (if (or (= mark gnus-unread-mark)
11707             (= mark gnus-ticked-mark)
11708             (= mark gnus-dormant-mark))
11709         (gnus-mark-article-as-unread article mark)
11710       (gnus-mark-article-as-read article mark))
11711
11712     ;; See whether the article is to be put in the cache.
11713     (and gnus-use-cache
11714          (not (= mark gnus-canceled-mark))
11715          (vectorp (gnus-summary-article-header article))
11716          (save-excursion
11717            (gnus-cache-possibly-enter-article
11718             gnus-newsgroup-name article
11719             (gnus-summary-article-header article)
11720             (= mark gnus-ticked-mark)
11721             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11722
11723     (if (gnus-summary-goto-subject article nil t)
11724         (let ((buffer-read-only nil))
11725           (gnus-summary-show-thread)
11726           ;; Fix the mark.
11727           (gnus-summary-update-mark mark 'unread)
11728           t))))
11729
11730 (defun gnus-summary-update-secondary-mark (article)
11731   "Update the secondary (read, process, cache) mark."
11732   (gnus-summary-update-mark
11733    (cond ((memq article gnus-newsgroup-processable)
11734           gnus-process-mark)
11735          ((memq article gnus-newsgroup-cached)
11736           gnus-cached-mark)
11737          ((memq article gnus-newsgroup-replied)
11738           gnus-replied-mark)
11739          ((memq article gnus-newsgroup-saved)
11740           gnus-saved-mark)
11741          (t gnus-unread-mark))
11742    'replied)
11743   (when (gnus-visual-p 'summary-highlight 'highlight)
11744     (run-hooks 'gnus-summary-update-hook))
11745   t)
11746
11747 (defun gnus-summary-update-mark (mark type)
11748   (beginning-of-line)
11749   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11750         (buffer-read-only nil))
11751     (when forward
11752       ;; Go to the right position on the line.
11753       (forward-char forward)
11754       ;; Replace the old mark with the new mark.
11755       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11756       ;; Optionally update the marks by some user rule.
11757       (when (eq type 'unread)
11758         (gnus-data-set-mark
11759          (gnus-data-find (gnus-summary-article-number)) mark)
11760         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11761
11762 (defun gnus-mark-article-as-read (article &optional mark)
11763   "Enter ARTICLE in the pertinent lists and remove it from others."
11764   ;; Make the article expirable.
11765   (let ((mark (or mark gnus-del-mark)))
11766     (if (= mark gnus-expirable-mark)
11767         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11768       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11769     ;; Remove from unread and marked lists.
11770     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11771     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11772     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11773     (push (cons article mark) gnus-newsgroup-reads)
11774     ;; Possibly remove from cache, if that is used.
11775     (when gnus-use-cache
11776       (gnus-cache-enter-remove-article article))))
11777
11778 (defun gnus-mark-article-as-unread (article &optional mark)
11779   "Enter ARTICLE in the pertinent lists and remove it from others."
11780   (let ((mark (or mark gnus-ticked-mark)))
11781     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11782     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11783     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11784     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11785     (cond ((= mark gnus-ticked-mark)
11786            (push article gnus-newsgroup-marked))
11787           ((= mark gnus-dormant-mark)
11788            (push article gnus-newsgroup-dormant))
11789           (t
11790            (push article gnus-newsgroup-unreads)))
11791     (setq gnus-newsgroup-reads
11792           (delq (assq article gnus-newsgroup-reads)
11793                 gnus-newsgroup-reads))))
11794
11795 (defalias 'gnus-summary-mark-as-unread-forward
11796   'gnus-summary-tick-article-forward)
11797 (make-obsolete 'gnus-summary-mark-as-unread-forward
11798                'gnus-summary-tick-article-forward)
11799 (defun gnus-summary-tick-article-forward (n)
11800   "Tick N articles forwards.
11801 If N is negative, tick backwards instead.
11802 The difference between N and the number of articles ticked is returned."
11803   (interactive "p")
11804   (gnus-summary-mark-forward n gnus-ticked-mark))
11805
11806 (defalias 'gnus-summary-mark-as-unread-backward
11807   'gnus-summary-tick-article-backward)
11808 (make-obsolete 'gnus-summary-mark-as-unread-backward
11809                'gnus-summary-tick-article-backward)
11810 (defun gnus-summary-tick-article-backward (n)
11811   "Tick N articles backwards.
11812 The difference between N and the number of articles ticked is returned."
11813   (interactive "p")
11814   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11815
11816 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11817 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11818 (defun gnus-summary-tick-article (&optional article clear-mark)
11819   "Mark current article as unread.
11820 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11821 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11822   (interactive)
11823   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11824                                        gnus-ticked-mark)))
11825
11826 (defun gnus-summary-mark-as-read-forward (n)
11827   "Mark N articles as read forwards.
11828 If N is negative, mark backwards instead.
11829 The difference between N and the actual number of articles marked is
11830 returned."
11831   (interactive "p")
11832   (gnus-summary-mark-forward n gnus-del-mark t))
11833
11834 (defun gnus-summary-mark-as-read-backward (n)
11835   "Mark the N articles as read backwards.
11836 The difference between N and the actual number of articles marked is
11837 returned."
11838   (interactive "p")
11839   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11840
11841 (defun gnus-summary-mark-as-read (&optional article mark)
11842   "Mark current article as read.
11843 ARTICLE specifies the article to be marked as read.
11844 MARK specifies a string to be inserted at the beginning of the line."
11845   (gnus-summary-mark-article article mark))
11846
11847 (defun gnus-summary-clear-mark-forward (n)
11848   "Clear marks from N articles forward.
11849 If N is negative, clear backward instead.
11850 The difference between N and the number of marks cleared is returned."
11851   (interactive "p")
11852   (gnus-summary-mark-forward n gnus-unread-mark))
11853
11854 (defun gnus-summary-clear-mark-backward (n)
11855   "Clear marks from N articles backward.
11856 The difference between N and the number of marks cleared is returned."
11857   (interactive "p")
11858   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11859
11860 (defun gnus-summary-mark-unread-as-read ()
11861   "Intended to be used by `gnus-summary-mark-article-hook'."
11862   (when (memq gnus-current-article gnus-newsgroup-unreads)
11863     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11864
11865 (defun gnus-summary-mark-unread-and-read-as-read ()
11866   "Intended to be used by `gnus-summary-mark-article-hook'."
11867   (let ((mark (gnus-summary-article-mark)))
11868     (when (or (gnus-unread-mark-p mark)
11869               (gnus-read-mark-p mark))
11870       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11871
11872 (defun gnus-summary-mark-region-as-read (point mark all)
11873   "Mark all unread articles between point and mark as read.
11874 If given a prefix, mark all articles between point and mark as read,
11875 even ticked and dormant ones."
11876   (interactive "r\nP")
11877   (save-excursion
11878     (let (article)
11879       (goto-char point)
11880       (beginning-of-line)
11881       (while (and
11882               (< (point) mark)
11883               (progn
11884                 (when (or all
11885                           (memq (setq article (gnus-summary-article-number))
11886                                 gnus-newsgroup-unreads))
11887                   (gnus-summary-mark-article article gnus-del-mark))
11888                 t)
11889               (gnus-summary-find-next))))))
11890
11891 (defun gnus-summary-mark-below (score mark)
11892   "Mark articles with score less than SCORE with MARK."
11893   (interactive "P\ncMark: ")
11894   (gnus-set-global-variables)
11895   (setq score (if score
11896                   (prefix-numeric-value score)
11897                 (or gnus-summary-default-score 0)))
11898   (save-excursion
11899     (set-buffer gnus-summary-buffer)
11900     (goto-char (point-min))
11901     (while 
11902         (progn
11903           (and (< (gnus-summary-article-score) score)
11904                (gnus-summary-mark-article nil mark))
11905           (gnus-summary-find-next)))))
11906
11907 (defun gnus-summary-kill-below (&optional score)
11908   "Mark articles with score below SCORE as read."
11909   (interactive "P")
11910   (gnus-set-global-variables)
11911   (gnus-summary-mark-below score gnus-killed-mark))
11912
11913 (defun gnus-summary-clear-above (&optional score)
11914   "Clear all marks from articles with score above SCORE."
11915   (interactive "P")
11916   (gnus-set-global-variables)
11917   (gnus-summary-mark-above score gnus-unread-mark))
11918
11919 (defun gnus-summary-tick-above (&optional score)
11920   "Tick all articles with score above SCORE."
11921   (interactive "P")
11922   (gnus-set-global-variables)
11923   (gnus-summary-mark-above score gnus-ticked-mark))
11924
11925 (defun gnus-summary-mark-above (score mark)
11926   "Mark articles with score over SCORE with MARK."
11927   (interactive "P\ncMark: ")
11928   (gnus-set-global-variables)
11929   (setq score (if score
11930                   (prefix-numeric-value score)
11931                 (or gnus-summary-default-score 0)))
11932   (save-excursion
11933     (set-buffer gnus-summary-buffer)
11934     (goto-char (point-min))
11935     (while (and (progn
11936                   (if (> (gnus-summary-article-score) score)
11937                       (gnus-summary-mark-article nil mark))
11938                   t)
11939                 (gnus-summary-find-next)))))
11940
11941 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11942 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11943 (defun gnus-summary-limit-include-expunged ()
11944   "Display all the hidden articles that were expunged for low scores."
11945   (interactive)
11946   (gnus-set-global-variables)
11947   (let ((buffer-read-only nil))
11948     (let ((scored gnus-newsgroup-scored)
11949           headers h)
11950       (while scored
11951         (or (gnus-summary-goto-subject (car (car scored)))
11952             (and (setq h (gnus-summary-article-header (car (car scored))))
11953                  (< (cdr (car scored)) gnus-summary-expunge-below)
11954                  (setq headers (cons h headers))))
11955         (setq scored (cdr scored)))
11956       (or headers (error "No expunged articles hidden."))
11957       (goto-char (point-min))
11958       (gnus-summary-prepare-unthreaded (nreverse headers)))
11959     (goto-char (point-min))
11960     (gnus-summary-position-point)))
11961
11962 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
11963   "Mark all articles not marked as unread in this newsgroup as read.
11964 If prefix argument ALL is non-nil, all articles are marked as read.
11965 If QUIETLY is non-nil, no questions will be asked.
11966 If TO-HERE is non-nil, it should be a point in the buffer.  All
11967 articles before this point will be marked as read.
11968 The number of articles marked as read is returned."
11969   (interactive "P")
11970   (gnus-set-global-variables)
11971   (prog1
11972       (if (or quietly
11973               (not gnus-interactive-catchup) ;Without confirmation?
11974               gnus-expert-user
11975               (gnus-y-or-n-p
11976                (if all
11977                    "Mark absolutely all articles as read? "
11978                  "Mark all unread articles as read? ")))
11979           (if (and not-mark
11980                    (not gnus-newsgroup-adaptive)
11981                    (not gnus-newsgroup-auto-expire))
11982               (progn
11983                 (when all
11984                   (setq gnus-newsgroup-marked nil
11985                         gnus-newsgroup-dormant nil))
11986                 (setq gnus-newsgroup-unreads nil))
11987             ;; We actually mark all articles as canceled, which we
11988             ;; have to do when using auto-expiry or adaptive scoring.
11989             (gnus-summary-show-all-threads)
11990             (if (gnus-summary-first-subject (not all))
11991                 (while (and
11992                         (if to-here (< (point) to-here) t)
11993                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11994                         (gnus-summary-find-next (not all)))))
11995             (unless to-here
11996               (setq gnus-newsgroup-unreads nil))
11997             (gnus-set-mode-line 'summary)))
11998     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
11999       (if (and (not to-here) (eq 'nnvirtual (car method)))
12000           (nnvirtual-catchup-group
12001            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12002     (gnus-summary-position-point)))
12003
12004 (defun gnus-summary-catchup-to-here (&optional all)
12005   "Mark all unticked articles before the current one as read.
12006 If ALL is non-nil, also mark ticked and dormant articles as read."
12007   (interactive "P")
12008   (gnus-set-global-variables)
12009   (save-excursion
12010     (let ((beg (point)))
12011       ;; We check that there are unread articles.
12012       (when (or all (gnus-summary-find-prev))
12013         (gnus-summary-catchup all t beg))))
12014   (gnus-summary-position-point))
12015
12016 (defun gnus-summary-catchup-all (&optional quietly)
12017   "Mark all articles in this newsgroup as read."
12018   (interactive "P")
12019   (gnus-set-global-variables)
12020   (gnus-summary-catchup t quietly))
12021
12022 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12023   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12024 If prefix argument ALL is non-nil, all articles are marked as read."
12025   (interactive "P")
12026   (gnus-set-global-variables)
12027   (gnus-summary-catchup all quietly nil 'fast)
12028   ;; Select next newsgroup or exit.
12029   (if (eq gnus-auto-select-next 'quietly)
12030       (gnus-summary-next-group nil)
12031     (gnus-summary-exit)))
12032
12033 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12034   "Mark all articles in this newsgroup as read, and then exit."
12035   (interactive "P")
12036   (gnus-set-global-variables)
12037   (gnus-summary-catchup-and-exit t quietly))
12038
12039 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12040 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12041   "Mark all articles in this group as read and select the next group.
12042 If given a prefix, mark all articles, unread as well as ticked, as
12043 read."
12044   (interactive "P")
12045   (gnus-set-global-variables)
12046   (save-excursion
12047     (gnus-summary-catchup all))
12048   (gnus-summary-next-article t nil nil t))
12049
12050 ;; Thread-based commands.
12051
12052 (defun gnus-summary-articles-in-thread (&optional article)
12053   "Return a list of all articles in the current thread.
12054 If ARTICLE is non-nil, return all articles in the thread that starts
12055 with that article."
12056   (let* ((article (or article (gnus-summary-article-number)))
12057          (data (gnus-data-find-list article))
12058          (top-level (gnus-data-level (car data)))
12059          (top-subject
12060           (cond ((null gnus-thread-operation-ignore-subject)
12061                  (gnus-simplify-subject-re
12062                   (mail-header-subject (gnus-data-header (car data)))))
12063                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12064                  (gnus-simplify-subject-fuzzy
12065                   (mail-header-subject (gnus-data-header (car data)))))
12066                 (t nil)))
12067          articles)
12068     (if (not data)
12069         ()                              ; This article doesn't exist.
12070       (while data
12071         (and (or (not top-subject)
12072                  (string= top-subject
12073                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12074                               (gnus-simplify-subject-fuzzy
12075                                (mail-header-subject
12076                                 (gnus-data-header (car data))))
12077                             (gnus-simplify-subject-re
12078                              (mail-header-subject
12079                               (gnus-data-header (car data)))))))
12080              (setq articles (cons (gnus-data-number (car data)) articles)))
12081         (if (and (setq data (cdr data))
12082                  (> (gnus-data-level (car data)) top-level))
12083             ()
12084           (setq data nil)))
12085       ;; Return the list of articles.
12086       (nreverse articles))))
12087
12088 (defun gnus-summary-rethread-current ()
12089   "Rethread the thread the current article is part of."
12090   (interactive)
12091   (gnus-set-global-variables)
12092   (let* ((gnus-show-threads t)
12093          (article (gnus-summary-article-number))
12094          (id (mail-header-id (gnus-summary-article-header)))
12095          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12096     (unless id
12097       (error "No article on the current line"))
12098     (gnus-rebuild-thread id)
12099     (gnus-summary-goto-subject article)))
12100
12101 (defun gnus-summary-reparent-thread ()
12102   "Make current article child of the marked (or previous) article.
12103
12104 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12105 is non-nil or the Subject: of both articles are the same."
12106   (interactive)
12107   (or (not (gnus-group-read-only-p))
12108       (error "The current newsgroup does not support article editing."))
12109   (or (<= (length gnus-newsgroup-processable) 1)
12110       (error "No more than one article may be marked."))
12111   (save-window-excursion
12112     (let ((gnus-article-buffer " *reparent*")
12113           (current-article (gnus-summary-article-number))
12114           ; first grab the marked article, otherwise one line up.
12115           (parent-article (if (not (null gnus-newsgroup-processable))
12116                               (car gnus-newsgroup-processable)
12117                             (save-excursion
12118                               (if (eq (forward-line -1) 0)
12119                                   (gnus-summary-article-number)
12120                                 (error "Beginning of summary buffer."))))))
12121       (or (not (eq current-article parent-article))
12122           (error "An article may not be self-referential."))
12123       (let ((message-id (mail-header-id 
12124                          (gnus-summary-article-header parent-article))))
12125         (or (and message-id (not (equal message-id "")))
12126             (error "No message-id in desired parent."))
12127         (gnus-summary-select-article t t nil current-article)
12128         (set-buffer gnus-article-buffer)
12129         (setq buffer-read-only nil)
12130         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12131           (erase-buffer)
12132           (insert buf))
12133         (goto-char (point-min))
12134         (if (search-forward-regexp "^References: " nil t)
12135             (insert message-id " " )
12136           (insert "References: " message-id "\n"))
12137         (or (gnus-request-replace-article current-article
12138                                           (car gnus-article-current)
12139                                           gnus-article-buffer)
12140             (error "Couldn't replace article."))
12141         (set-buffer gnus-summary-buffer)
12142         (gnus-summary-unmark-all-processable)
12143         (gnus-summary-rethread-current)
12144         (message "Article %d is now the child of article %d."
12145                  current-article parent-article)))))
12146
12147 (defun gnus-summary-toggle-threads (&optional arg)
12148   "Toggle showing conversation threads.
12149 If ARG is positive number, turn showing conversation threads on."
12150   (interactive "P")
12151   (gnus-set-global-variables)
12152   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12153     (setq gnus-show-threads
12154           (if (null arg) (not gnus-show-threads)
12155             (> (prefix-numeric-value arg) 0)))
12156     (gnus-summary-prepare)
12157     (gnus-summary-goto-subject current)
12158     (gnus-summary-position-point)))
12159
12160 (defun gnus-summary-show-all-threads ()
12161   "Show all threads."
12162   (interactive)
12163   (gnus-set-global-variables)
12164   (save-excursion
12165     (let ((buffer-read-only nil))
12166       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12167   (gnus-summary-position-point))
12168
12169 (defun gnus-summary-show-thread ()
12170   "Show thread subtrees.
12171 Returns nil if no thread was there to be shown."
12172   (interactive)
12173   (gnus-set-global-variables)
12174   (let ((buffer-read-only nil)
12175         (orig (point))
12176         ;; first goto end then to beg, to have point at beg after let
12177         (end (progn (end-of-line) (point)))
12178         (beg (progn (beginning-of-line) (point))))
12179     (prog1
12180         ;; Any hidden lines here?
12181         (search-forward "\r" end t)
12182       (subst-char-in-region beg end ?\^M ?\n t)
12183       (goto-char orig)
12184       (gnus-summary-position-point))))
12185
12186 (defun gnus-summary-hide-all-threads ()
12187   "Hide all thread subtrees."
12188   (interactive)
12189   (gnus-set-global-variables)
12190   (save-excursion
12191     (goto-char (point-min))
12192     (gnus-summary-hide-thread)
12193     (while (zerop (gnus-summary-next-thread 1 t))
12194       (gnus-summary-hide-thread)))
12195   (gnus-summary-position-point))
12196
12197 (defun gnus-summary-hide-thread ()
12198   "Hide thread subtrees.
12199 Returns nil if no threads were there to be hidden."
12200   (interactive)
12201   (gnus-set-global-variables)
12202   (let ((buffer-read-only nil)
12203         (start (point))
12204         (article (gnus-summary-article-number))
12205         end)
12206     (goto-char start)
12207     ;; Go forward until either the buffer ends or the subthread
12208     ;; ends.
12209     (when (and (not (eobp))
12210                (or (zerop (gnus-summary-next-thread 1 t))
12211                    (goto-char (point-max))))
12212       (setq end (point))
12213       (prog1
12214           (if (and (> (point) start)
12215                    (search-backward "\n" start t))
12216               (progn
12217                 (subst-char-in-region start (point) ?\n ?\^M)
12218                 (gnus-summary-goto-subject article))
12219             (goto-char start)
12220             nil)
12221         (gnus-summary-position-point)))))
12222
12223 (defun gnus-summary-go-to-next-thread (&optional previous)
12224   "Go to the same level (or less) next thread.
12225 If PREVIOUS is non-nil, go to previous thread instead.
12226 Return the article number moved to, or nil if moving was impossible."
12227   (let* ((level (gnus-summary-thread-level))
12228          (article (gnus-summary-article-number))
12229          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12230          oart)
12231     (while data
12232       (if (<= (gnus-data-level (car data)) level)
12233           (setq oart (gnus-data-number (car data))
12234                 data nil)
12235         (setq data (cdr data))))
12236     (and oart
12237          (gnus-summary-goto-subject oart))))
12238
12239 (defun gnus-summary-next-thread (n &optional silent)
12240   "Go to the same level next N'th thread.
12241 If N is negative, search backward instead.
12242 Returns the difference between N and the number of skips actually
12243 done.
12244
12245 If SILENT, don't output messages."
12246   (interactive "p")
12247   (gnus-set-global-variables)
12248   (let ((backward (< n 0))
12249         (n (abs n))
12250         old dum)
12251     (while (and (> n 0)
12252                 (setq old (save-excursion (forward-line 1) (point)))
12253                 (gnus-summary-go-to-next-thread backward))
12254       (when (and (eq gnus-summary-make-false-root 'dummy)
12255                  (setq dum (text-property-not-all
12256                             old (point) 'gnus-intangible nil)))
12257         (goto-char dum))
12258       (decf n))
12259     (gnus-summary-position-point)
12260     (when (and (not silent) (/= 0 n))
12261       (gnus-message 7 "No more threads"))
12262     n))
12263
12264 (defun gnus-summary-prev-thread (n)
12265   "Go to the same level previous N'th thread.
12266 Returns the difference between N and the number of skips actually
12267 done."
12268   (interactive "p")
12269   (gnus-set-global-variables)
12270   (gnus-summary-next-thread (- n)))
12271
12272 (defun gnus-summary-go-down-thread ()
12273   "Go down one level in the current thread."
12274   (let ((children (gnus-summary-article-children)))
12275     (and children
12276          (gnus-summary-goto-subject (car children)))))
12277
12278 (defun gnus-summary-go-up-thread ()
12279   "Go up one level in the current thread."
12280   (let ((parent (gnus-summary-article-parent)))
12281     (and parent
12282          (gnus-summary-goto-subject parent))))
12283
12284 (defun gnus-summary-down-thread (n)
12285   "Go down thread N steps.
12286 If N is negative, go up instead.
12287 Returns the difference between N and how many steps down that were
12288 taken."
12289   (interactive "p")
12290   (gnus-set-global-variables)
12291   (let ((up (< n 0))
12292         (n (abs n)))
12293     (while (and (> n 0)
12294                 (if up (gnus-summary-go-up-thread)
12295                   (gnus-summary-go-down-thread)))
12296       (setq n (1- n)))
12297     (gnus-summary-position-point)
12298     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12299     n))
12300
12301 (defun gnus-summary-up-thread (n)
12302   "Go up thread N steps.
12303 If N is negative, go up instead.
12304 Returns the difference between N and how many steps down that were
12305 taken."
12306   (interactive "p")
12307   (gnus-set-global-variables)
12308   (gnus-summary-down-thread (- n)))
12309
12310 (defun gnus-summary-top-thread ()
12311   "Go to the top of the thread."
12312   (interactive)
12313   (gnus-set-global-variables)
12314   (while (gnus-summary-go-up-thread))
12315   (gnus-summary-article-number))
12316
12317 (defun gnus-summary-kill-thread (&optional unmark)
12318   "Mark articles under current thread as read.
12319 If the prefix argument is positive, remove any kinds of marks.
12320 If the prefix argument is negative, tick articles instead."
12321   (interactive "P")
12322   (gnus-set-global-variables)
12323   (if unmark
12324       (setq unmark (prefix-numeric-value unmark)))
12325   (let ((articles (gnus-summary-articles-in-thread)))
12326     (save-excursion
12327       ;; Expand the thread.
12328       (gnus-summary-show-thread)
12329       ;; Mark all the articles.
12330       (while articles
12331         (gnus-summary-goto-subject (car articles))
12332         (cond ((null unmark)
12333                (gnus-summary-mark-article-as-read gnus-killed-mark))
12334               ((> unmark 0)
12335                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12336               (t
12337                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12338         (setq articles (cdr articles))))
12339     ;; Hide killed subtrees.
12340     (and (null unmark)
12341          gnus-thread-hide-killed
12342          (gnus-summary-hide-thread))
12343     ;; If marked as read, go to next unread subject.
12344     (if (null unmark)
12345         ;; Go to next unread subject.
12346         (gnus-summary-next-subject 1 t)))
12347   (gnus-set-mode-line 'summary))
12348
12349 ;; Summary sorting commands
12350
12351 (defun gnus-summary-sort-by-number (&optional reverse)
12352   "Sort summary buffer by article number.
12353 Argument REVERSE means reverse order."
12354   (interactive "P")
12355   (gnus-summary-sort 'number reverse))
12356
12357 (defun gnus-summary-sort-by-author (&optional reverse)
12358   "Sort summary buffer by author name alphabetically.
12359 If case-fold-search is non-nil, case of letters is ignored.
12360 Argument REVERSE means reverse order."
12361   (interactive "P")
12362   (gnus-summary-sort 'author reverse))
12363
12364 (defun gnus-summary-sort-by-subject (&optional reverse)
12365   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12366 If case-fold-search is non-nil, case of letters is ignored.
12367 Argument REVERSE means reverse order."
12368   (interactive "P")
12369   (gnus-summary-sort 'subject reverse))
12370
12371 (defun gnus-summary-sort-by-date (&optional reverse)
12372   "Sort summary buffer by date.
12373 Argument REVERSE means reverse order."
12374   (interactive "P")
12375   (gnus-summary-sort 'date reverse))
12376
12377 (defun gnus-summary-sort-by-score (&optional reverse)
12378   "Sort summary buffer by score.
12379 Argument REVERSE means reverse order."
12380   (interactive "P")
12381   (gnus-summary-sort 'score reverse))
12382
12383 (defun gnus-summary-sort (predicate reverse)
12384   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12385   (gnus-set-global-variables)
12386   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12387          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12388          (gnus-thread-sort-functions
12389           (list
12390            (if (not reverse)
12391                thread
12392              `(lambda (t1 t2)
12393                 (,thread t2 t1)))))
12394          (gnus-article-sort-functions
12395           (list
12396            (if (not reverse)
12397                article
12398              `(lambda (t1 t2)
12399                 (,article t2 t1)))))
12400          (buffer-read-only)
12401          (gnus-summary-prepare-hook nil))
12402     ;; We do the sorting by regenerating the threads.
12403     (gnus-summary-prepare)
12404     ;; Hide subthreads if needed.
12405     (when (and gnus-show-threads gnus-thread-hide-subtree)
12406       (gnus-summary-hide-all-threads)))
12407   ;; If in async mode, we send some info to the backend.
12408   (when gnus-newsgroup-async
12409     (gnus-request-asynchronous
12410      gnus-newsgroup-name gnus-newsgroup-data)))
12411
12412 (defun gnus-sortable-date (date)
12413   "Make sortable string by string-lessp from DATE.
12414 Timezone package is used."
12415   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12416          (year (aref date 0))
12417          (month (aref date 1))
12418          (day (aref date 2)))
12419     (timezone-make-sortable-date
12420      year month day
12421      (timezone-make-time-string
12422       (aref date 3) (aref date 4) (aref date 5)))))
12423
12424 ;; Summary saving commands.
12425
12426 (defun gnus-summary-save-article (&optional n not-saved)
12427   "Save the current article using the default saver function.
12428 If N is a positive number, save the N next articles.
12429 If N is a negative number, save the N previous articles.
12430 If N is nil and any articles have been marked with the process mark,
12431 save those articles instead.
12432 The variable `gnus-default-article-saver' specifies the saver function."
12433   (interactive "P")
12434   (gnus-set-global-variables)
12435   (let ((articles (gnus-summary-work-articles n))
12436         file header article)
12437     (while articles
12438       (setq header (gnus-summary-article-header
12439                     (setq article (pop articles))))
12440       (if (not (vectorp header))
12441           ;; This is a pseudo-article.
12442           (if (assq 'name header)
12443               (gnus-copy-file (cdr (assq 'name header)))
12444             (gnus-message 1 "Article %d is unsaveable" article))
12445         ;; This is a real article.
12446         (save-window-excursion
12447           (gnus-summary-select-article t nil nil article))
12448         (unless gnus-save-all-headers
12449           ;; Remove headers accoring to `gnus-saved-headers'.
12450           (let ((gnus-visible-headers
12451                  (or gnus-saved-headers gnus-visible-headers)))
12452             (gnus-article-hide-headers nil t)))
12453         ;; Remove any X-Gnus lines.
12454         (save-excursion
12455           (set-buffer gnus-article-buffer)
12456           (save-restriction
12457             (let ((buffer-read-only nil))
12458               (nnheader-narrow-to-headers)
12459               (while (re-search-forward "^X-Gnus" nil t)
12460                 (gnus-delete-line)))))
12461         (save-window-excursion
12462           (if (not gnus-default-article-saver)
12463               (error "No default saver is defined.")
12464             (setq file (funcall
12465                         gnus-default-article-saver
12466                         (cond
12467                          ((not gnus-prompt-before-saving)
12468                           'default)
12469                          ((eq gnus-prompt-before-saving 'always)
12470                           nil)
12471                          (t file))))))
12472         (gnus-summary-remove-process-mark article)
12473         (unless not-saved
12474           (gnus-summary-set-saved-mark article))))
12475     (gnus-summary-position-point)
12476     n))
12477
12478 (defun gnus-summary-pipe-output (&optional arg)
12479   "Pipe the current article to a subprocess.
12480 If N is a positive number, pipe the N next articles.
12481 If N is a negative number, pipe the N previous articles.
12482 If N is nil and any articles have been marked with the process mark,
12483 pipe those articles instead."
12484   (interactive "P")
12485   (gnus-set-global-variables)
12486   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12487     (gnus-summary-save-article arg t))
12488   (gnus-configure-windows 'pipe))
12489
12490 (defun gnus-summary-save-article-mail (&optional arg)
12491   "Append the current article to an mail file.
12492 If N is a positive number, save the N next articles.
12493 If N is a negative number, save the N previous articles.
12494 If N is nil and any articles have been marked with the process mark,
12495 save those articles instead."
12496   (interactive "P")
12497   (gnus-set-global-variables)
12498   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12499     (gnus-summary-save-article arg)))
12500
12501 (defun gnus-summary-save-article-rmail (&optional arg)
12502   "Append the current article to an rmail file.
12503 If N is a positive number, save the N next articles.
12504 If N is a negative number, save the N previous articles.
12505 If N is nil and any articles have been marked with the process mark,
12506 save those articles instead."
12507   (interactive "P")
12508   (gnus-set-global-variables)
12509   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12510     (gnus-summary-save-article arg)))
12511
12512 (defun gnus-summary-save-article-file (&optional arg)
12513   "Append the current article to a file.
12514 If N is a positive number, save the N next articles.
12515 If N is a negative number, save the N previous articles.
12516 If N is nil and any articles have been marked with the process mark,
12517 save those articles instead."
12518   (interactive "P")
12519   (gnus-set-global-variables)
12520   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12521     (gnus-summary-save-article arg)))
12522
12523 (defun gnus-summary-save-article-body-file (&optional arg)
12524   "Append the current article body to a file.
12525 If N is a positive number, save the N next articles.
12526 If N is a negative number, save the N previous articles.
12527 If N is nil and any articles have been marked with the process mark,
12528 save those articles instead."
12529   (interactive "P")
12530   (gnus-set-global-variables)
12531   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12532     (gnus-summary-save-article arg)))
12533
12534 (defun gnus-get-split-value (methods)
12535   "Return a value based on the split METHODS."
12536   (let (split-name method result match)
12537     (when methods
12538       (save-excursion
12539         (set-buffer gnus-original-article-buffer)
12540         (save-restriction
12541           (nnheader-narrow-to-headers)
12542           (while methods
12543             (goto-char (point-min))
12544             (setq method (pop methods))
12545             (setq match (pop method))
12546             (when (cond
12547                    ((stringp match)
12548                     ;; Regular expression.
12549                     (condition-case ()
12550                         (re-search-forward match nil t)
12551                       (error nil)))
12552                    ((gnus-functionp match)
12553                     ;; Function.
12554                     (save-restriction
12555                       (widen)
12556                       (setq result (funcall match gnus-newsgroup-name))))
12557                    ((consp match)
12558                     ;; Form.
12559                     (save-restriction
12560                       (widen)
12561                       (setq result (eval match)))))
12562               (setq split-name (append (cdr methods) split-name))
12563               (cond ((stringp result)
12564                      (push result split-name))
12565                     ((consp result)
12566                      (setq split-name (append result split-name)))))))))
12567     split-name))
12568
12569 (defun gnus-read-move-group-name (prompt default articles prefix)
12570   "Read a group name."
12571   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12572          (prom
12573           (format "Where do you want to %s %s? "
12574                   prompt
12575                   (if (> (length articles) 1)
12576                       (format "these %d articles" (length articles))
12577                     "this article")))
12578          (to-newsgroup
12579           (cond
12580            ((null split-name)
12581             (completing-read
12582              (concat prom
12583                      (if default
12584                          (format "(default %s) " default)
12585                        ""))
12586              gnus-active-hashtb nil nil prefix))
12587            ((= 1 (length split-name))
12588             (completing-read prom gnus-active-hashtb
12589                              nil nil (cons (car split-name) 0)))
12590            (t
12591             (completing-read
12592              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12593
12594     (when to-newsgroup
12595       (if (or (string= to-newsgroup "")
12596               (string= to-newsgroup prefix))
12597           (setq to-newsgroup (or default "")))
12598       (or (gnus-active to-newsgroup)
12599           (gnus-activate-group to-newsgroup)
12600           (error "No such group: %s" to-newsgroup)))
12601     to-newsgroup))
12602
12603 (defun gnus-read-save-file-name (prompt default-name)
12604   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12605          (file
12606           ;; Let the split methods have their say.
12607           (cond
12608            ;; No split name was found.
12609            ((null split-name)
12610             (read-file-name
12611              (concat prompt " (default "
12612                      (file-name-nondirectory default-name) ") ")
12613              (file-name-directory default-name)
12614              default-name))
12615            ;; A single split name was found
12616            ((= 1 (length split-name))
12617             (read-file-name
12618              (concat prompt " (default " (car split-name) ") ")
12619              gnus-article-save-directory
12620              (concat gnus-article-save-directory (car split-name))))
12621            ;; A list of splits was found.
12622            (t
12623             (setq split-name (mapcar (lambda (el) (list el))
12624                                      (nreverse split-name)))
12625             (let ((result (completing-read
12626                            (concat prompt " ") split-name nil nil)))
12627               (concat gnus-article-save-directory
12628                       (if (string= result "")
12629                           (car (car split-name))
12630                         result)))))))
12631     ;; If we have read a directory, we append the default file name.
12632     (when (file-directory-p file)
12633       (setq file (concat (file-name-as-directory file)
12634                          (file-name-nondirectory default-name))))
12635     ;; Possibly translate some charaters.
12636     (nnheader-translate-file-chars file)))
12637
12638 (defun gnus-article-archive-name (group)
12639   "Return the first instance of an \"Archive-name\" in the current buffer."
12640   (let ((case-fold-search t))
12641     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12642       (match-string 1))))
12643
12644 (defun gnus-summary-save-in-rmail (&optional filename)
12645   "Append this article to Rmail file.
12646 Optional argument FILENAME specifies file name.
12647 Directory to save to is default to `gnus-article-save-directory' which
12648 is initialized from the SAVEDIR environment variable."
12649   (interactive)
12650   (gnus-set-global-variables)
12651   (let ((default-name
12652           (funcall gnus-rmail-save-name gnus-newsgroup-name
12653                    gnus-current-headers gnus-newsgroup-last-rmail)))
12654     (setq filename
12655           (cond ((eq filename 'default)
12656                  default-name)
12657                 (filename filename)
12658                 (t (gnus-read-save-file-name
12659                     "Save in rmail file:" default-name))))
12660     (gnus-make-directory (file-name-directory filename))
12661     (gnus-eval-in-buffer-window
12662      gnus-original-article-buffer
12663      (save-excursion
12664        (save-restriction
12665          (widen)
12666          (gnus-output-to-rmail filename))))
12667     ;; Remember the directory name to save articles
12668     (setq gnus-newsgroup-last-rmail filename)))
12669
12670 (defun gnus-summary-save-in-mail (&optional filename)
12671   "Append this article to Unix mail file.
12672 Optional argument FILENAME specifies file name.
12673 Directory to save to is default to `gnus-article-save-directory' which
12674 is initialized from the SAVEDIR environment variable."
12675   (interactive)
12676   (gnus-set-global-variables)
12677   (let ((default-name
12678           (funcall gnus-mail-save-name gnus-newsgroup-name
12679                    gnus-current-headers gnus-newsgroup-last-mail)))
12680     (setq filename
12681           (cond ((eq filename 'default)
12682                  default-name)
12683                 (filename filename)
12684                 (t (gnus-read-save-file-name
12685                     "Save in Unix mail file:" default-name))))
12686     (setq filename
12687           (expand-file-name filename
12688                             (and default-name
12689                                  (file-name-directory default-name))))
12690     (gnus-make-directory (file-name-directory filename))
12691     (gnus-eval-in-buffer-window
12692      gnus-original-article-buffer
12693      (save-excursion
12694        (save-restriction
12695          (widen)
12696          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12697              (gnus-output-to-rmail filename)
12698            (let ((mail-use-rfc822 t))
12699              (rmail-output filename 1 t t))))))
12700     ;; Remember the directory name to save articles.
12701     (setq gnus-newsgroup-last-mail filename)))
12702
12703 (defun gnus-summary-save-in-file (&optional filename)
12704   "Append this article to file.
12705 Optional argument FILENAME specifies file name.
12706 Directory to save to is default to `gnus-article-save-directory' which
12707 is initialized from the SAVEDIR environment variable."
12708   (interactive)
12709   (gnus-set-global-variables)
12710   (let ((default-name
12711           (funcall gnus-file-save-name gnus-newsgroup-name
12712                    gnus-current-headers gnus-newsgroup-last-file)))
12713     (setq filename
12714           (cond ((eq filename 'default)
12715                  default-name)
12716                 (filename filename)
12717                 (t (gnus-read-save-file-name
12718                     "Save in file:" default-name))))
12719     (gnus-make-directory (file-name-directory filename))
12720     (gnus-eval-in-buffer-window
12721      gnus-original-article-buffer
12722      (save-excursion
12723        (save-restriction
12724          (widen)
12725          (gnus-output-to-file filename))))
12726     ;; Remember the directory name to save articles.
12727     (setq gnus-newsgroup-last-file filename)))
12728
12729 (defun gnus-summary-save-body-in-file (&optional filename)
12730   "Append this article body to a file.
12731 Optional argument FILENAME specifies file name.
12732 The directory to save in defaults to `gnus-article-save-directory' which
12733 is initialized from the SAVEDIR environment variable."
12734   (interactive)
12735   (gnus-set-global-variables)
12736   (let ((default-name
12737           (funcall gnus-file-save-name gnus-newsgroup-name
12738                    gnus-current-headers gnus-newsgroup-last-file)))
12739     (setq filename
12740           (cond ((eq filename 'default)
12741                  default-name)
12742                 (filename filename)
12743                 (t (gnus-read-save-file-name
12744                     "Save body in file:" default-name))))
12745     (gnus-make-directory (file-name-directory filename))
12746     (gnus-eval-in-buffer-window
12747      gnus-article-buffer
12748      (save-excursion
12749        (save-restriction
12750          (widen)
12751          (goto-char (point-min))
12752          (and (search-forward "\n\n" nil t)
12753               (narrow-to-region (point) (point-max)))
12754          (gnus-output-to-file filename))))
12755     ;; Remember the directory name to save articles.
12756     (setq gnus-newsgroup-last-file filename)))
12757
12758 (defun gnus-summary-save-in-pipe (&optional command)
12759   "Pipe this article to subprocess."
12760   (interactive)
12761   (gnus-set-global-variables)
12762   (setq command
12763         (cond ((eq command 'default)
12764                gnus-last-shell-command)
12765               (command command)
12766               (t (read-string "Shell command on article: "
12767                               gnus-last-shell-command))))
12768   (if (string-equal command "")
12769       (setq command gnus-last-shell-command))
12770   (gnus-eval-in-buffer-window
12771    gnus-article-buffer
12772    (save-restriction
12773      (widen)
12774      (shell-command-on-region (point-min) (point-max) command nil)))
12775   (setq gnus-last-shell-command command))
12776
12777 ;; Summary extract commands
12778
12779 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12780   (let ((buffer-read-only nil)
12781         (article (gnus-summary-article-number))
12782         after-article b e)
12783     (or (gnus-summary-goto-subject article)
12784         (error (format "No such article: %d" article)))
12785     (gnus-summary-position-point)
12786     ;; If all commands are to be bunched up on one line, we collect
12787     ;; them here.
12788     (if gnus-view-pseudos-separately
12789         ()
12790       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12791             files action)
12792         (while ps
12793           (setq action (cdr (assq 'action (car ps))))
12794           (setq files (list (cdr (assq 'name (car ps)))))
12795           (while (and ps (cdr ps)
12796                       (string= (or action "1")
12797                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12798             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12799             (setcdr ps (cdr (cdr ps))))
12800           (if (not files)
12801               ()
12802             (if (not (string-match "%s" action))
12803                 (setq files (cons " " files)))
12804             (setq files (cons " " files))
12805             (and (assq 'execute (car ps))
12806                  (setcdr (assq 'execute (car ps))
12807                          (funcall (if (string-match "%s" action)
12808                                       'format 'concat)
12809                                   action
12810                                   (mapconcat (lambda (f) f) files " ")))))
12811           (setq ps (cdr ps)))))
12812     (if (and gnus-view-pseudos (not not-view))
12813         (while pslist
12814           (and (assq 'execute (car pslist))
12815                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12816                                      (eq gnus-view-pseudos 'not-confirm)))
12817           (setq pslist (cdr pslist)))
12818       (save-excursion
12819         (while pslist
12820           (setq after-article (or (cdr (assq 'article (car pslist)))
12821                                   (gnus-summary-article-number)))
12822           (gnus-summary-goto-subject after-article)
12823           (forward-line 1)
12824           (setq b (point))
12825           (insert "          " (file-name-nondirectory
12826                                 (cdr (assq 'name (car pslist))))
12827                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12828           (setq e (point))
12829           (forward-line -1)             ; back to `b'
12830           (add-text-properties
12831            b e (list 'gnus-number gnus-reffed-article-number
12832                      gnus-mouse-face-prop gnus-mouse-face))
12833           (gnus-data-enter
12834            after-article gnus-reffed-article-number
12835            gnus-unread-mark b (car pslist) 0 (- e b))
12836           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12837           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12838           (setq pslist (cdr pslist)))))))
12839
12840 (defun gnus-pseudos< (p1 p2)
12841   (let ((c1 (cdr (assq 'action p1)))
12842         (c2 (cdr (assq 'action p2))))
12843     (and c1 c2 (string< c1 c2))))
12844
12845 (defun gnus-request-pseudo-article (props)
12846   (cond ((assq 'execute props)
12847          (gnus-execute-command (cdr (assq 'execute props)))))
12848   (let ((gnus-current-article (gnus-summary-article-number)))
12849     (run-hooks 'gnus-mark-article-hook)))
12850
12851 (defun gnus-execute-command (command &optional automatic)
12852   (save-excursion
12853     (gnus-article-setup-buffer)
12854     (set-buffer gnus-article-buffer)
12855     (let ((command (if automatic command (read-string "Command: " command)))
12856           (buffer-read-only nil))
12857       (erase-buffer)
12858       (insert "$ " command "\n\n")
12859       (if gnus-view-pseudo-asynchronously
12860           (start-process "gnus-execute" nil "sh" "-c" command)
12861         (call-process "sh" nil t nil "-c" command)))))
12862
12863 (defun gnus-copy-file (file &optional to)
12864   "Copy FILE to TO."
12865   (interactive
12866    (list (read-file-name "Copy file: " default-directory)
12867          (read-file-name "Copy file to: " default-directory)))
12868   (gnus-set-global-variables)
12869   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12870   (and (file-directory-p to)
12871        (setq to (concat (file-name-as-directory to)
12872                         (file-name-nondirectory file))))
12873   (copy-file file to))
12874
12875 ;; Summary kill commands.
12876
12877 (defun gnus-summary-edit-global-kill (article)
12878   "Edit the \"global\" kill file."
12879   (interactive (list (gnus-summary-article-number)))
12880   (gnus-set-global-variables)
12881   (gnus-group-edit-global-kill article))
12882
12883 (defun gnus-summary-edit-local-kill ()
12884   "Edit a local kill file applied to the current newsgroup."
12885   (interactive)
12886   (gnus-set-global-variables)
12887   (setq gnus-current-headers (gnus-summary-article-header))
12888   (gnus-set-global-variables)
12889   (gnus-group-edit-local-kill
12890    (gnus-summary-article-number) gnus-newsgroup-name))
12891
12892 \f
12893 ;;;
12894 ;;; Gnus article mode
12895 ;;;
12896
12897 (put 'gnus-article-mode 'mode-class 'special)
12898
12899 (if gnus-article-mode-map
12900     nil
12901   (setq gnus-article-mode-map (make-keymap))
12902   (suppress-keymap gnus-article-mode-map)
12903
12904   (gnus-define-keys
12905    gnus-article-mode-map
12906    " " gnus-article-goto-next-page
12907    "\177" gnus-article-goto-prev-page
12908    [delete] gnus-article-goto-prev-page
12909    "\C-c^" gnus-article-refer-article
12910    "h" gnus-article-show-summary
12911    "s" gnus-article-show-summary
12912    "\C-c\C-m" gnus-article-mail
12913    "?" gnus-article-describe-briefly
12914    gnus-mouse-2 gnus-article-push-button
12915    "\r" gnus-article-press-button
12916    "\t" gnus-article-next-button
12917    "\M-\t" gnus-article-prev-button
12918    "\C-c\C-b" gnus-bug)
12919
12920   (substitute-key-definition
12921    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
12922
12923 (defun gnus-article-mode ()
12924   "Major mode for displaying an article.
12925
12926 All normal editing commands are switched off.
12927
12928 The following commands are available:
12929
12930 \\<gnus-article-mode-map>
12931 \\[gnus-article-next-page]\t Scroll the article one page forwards
12932 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12933 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12934 \\[gnus-article-show-summary]\t Display the summary buffer
12935 \\[gnus-article-mail]\t Send a reply to the address near point
12936 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12937 \\[gnus-info-find-node]\t Go to the Gnus info node"
12938   (interactive)
12939   (when (and menu-bar-mode
12940              (gnus-visual-p 'article-menu 'menu))
12941     (gnus-article-make-menu-bar))
12942   (kill-all-local-variables)
12943   (gnus-simplify-mode-line)
12944   (setq mode-name "Article")
12945   (setq major-mode 'gnus-article-mode)
12946   (make-local-variable 'minor-mode-alist)
12947   (or (assq 'gnus-show-mime minor-mode-alist)
12948       (setq minor-mode-alist
12949             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
12950   (use-local-map gnus-article-mode-map)
12951   (make-local-variable 'page-delimiter)
12952   (setq page-delimiter gnus-page-delimiter)
12953   (buffer-disable-undo (current-buffer))
12954   (setq buffer-read-only t)             ;Disable modification
12955   (run-hooks 'gnus-article-mode-hook))
12956
12957 (defun gnus-article-setup-buffer ()
12958   "Initialize the article buffer."
12959   (let* ((name (if gnus-single-article-buffer "*Article*"
12960                  (concat "*Article " gnus-newsgroup-name "*")))
12961          (original
12962           (progn (string-match "\\*Article" name)
12963                  (concat " *Original Article"
12964                          (substring name (match-end 0))))))
12965     (setq gnus-article-buffer name)
12966     (setq gnus-original-article-buffer original)
12967     ;; This might be a variable local to the summary buffer.
12968     (unless gnus-single-article-buffer
12969       (save-excursion
12970         (set-buffer gnus-summary-buffer)
12971         (setq gnus-article-buffer name)
12972         (setq gnus-original-article-buffer original)
12973         (gnus-set-global-variables))
12974       (make-local-variable 'gnus-summary-buffer))
12975     (if (get-buffer name)
12976         (save-excursion
12977           (set-buffer name)
12978           (buffer-disable-undo (current-buffer))
12979           (setq buffer-read-only t)
12980           (gnus-add-current-to-buffer-list)
12981           (or (eq major-mode 'gnus-article-mode)
12982               (gnus-article-mode))
12983           (current-buffer))
12984       (save-excursion
12985         (set-buffer (get-buffer-create name))
12986         (gnus-add-current-to-buffer-list)
12987         (gnus-article-mode)
12988         (current-buffer)))))
12989
12990 ;; Set article window start at LINE, where LINE is the number of lines
12991 ;; from the head of the article.
12992 (defun gnus-article-set-window-start (&optional line)
12993   (set-window-start
12994    (get-buffer-window gnus-article-buffer)
12995    (save-excursion
12996      (set-buffer gnus-article-buffer)
12997      (goto-char (point-min))
12998      (if (not line)
12999          (point-min)
13000        (gnus-message 6 "Moved to bookmark")
13001        (search-forward "\n\n" nil t)
13002        (forward-line line)
13003        (point)))))
13004
13005 (defun gnus-kill-all-overlays ()
13006   "Delete all overlays in the current buffer."
13007   (when (fboundp 'overlay-lists)
13008     (let* ((overlayss (overlay-lists))
13009            (buffer-read-only nil)
13010            (overlays (nconc (car overlayss) (cdr overlayss))))
13011       (while overlays
13012         (delete-overlay (pop overlays))))))
13013
13014 (defun gnus-request-article-this-buffer (article group)
13015   "Get an article and insert it into this buffer."
13016   (prog1
13017       (save-excursion
13018         (erase-buffer)
13019         (gnus-kill-all-overlays)
13020         (setq group (or group gnus-newsgroup-name))
13021
13022         ;; Open server if it has closed.
13023         (gnus-check-server (gnus-find-method-for-group group))
13024
13025         ;; Using `gnus-request-article' directly will insert the article into
13026         ;; `nntp-server-buffer' - so we'll save some time by not having to
13027         ;; copy it from the server buffer into the article buffer.
13028
13029         ;; We only request an article by message-id when we do not have the
13030         ;; headers for it, so we'll have to get those.
13031         (when (stringp article)
13032           (let ((gnus-override-method gnus-refer-article-method))
13033             (gnus-read-header article)))
13034
13035         ;; If the article number is negative, that means that this article
13036         ;; doesn't belong in this newsgroup (possibly), so we find its
13037         ;; message-id and request it by id instead of number.
13038         (when (numberp article)
13039           (save-excursion
13040             (set-buffer gnus-summary-buffer)
13041             (let ((header (gnus-summary-article-header article)))
13042               (if (< article 0)
13043                   (cond 
13044                    ((memq article gnus-newsgroup-sparse)
13045                     ;; This is a sparse gap article.
13046                     (setq article (mail-header-id header)))
13047                    ((vectorp header)
13048                     ;; It's a real article.
13049                     (setq article (mail-header-id header)))
13050                    (t
13051                     ;; It is an extracted pseudo-article.
13052                     (setq article 'pseudo)
13053                     (gnus-request-pseudo-article header))))
13054                 
13055               (let ((method (gnus-find-method-for-group 
13056                              gnus-newsgroup-name)))
13057                 (if (not (eq (car method) 'nneething))
13058                     ()
13059                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13060                                      (mail-header-subject header))))
13061                     (if (file-directory-p dir)
13062                         (progn
13063                           (setq article 'nneething)
13064                           (gnus-group-enter-directory dir)))))))))
13065
13066         (cond
13067          ;; We first check `gnus-original-article-buffer'.
13068          ((and (equal (car gnus-original-article) group)
13069                (eq (cdr gnus-original-article) article)
13070                (get-buffer gnus-original-article-buffer))
13071           (insert-buffer-substring gnus-original-article-buffer)
13072           'article)
13073          ;; Check the backlog.
13074          ((and gnus-keep-backlog
13075                (gnus-backlog-request-article group article (current-buffer)))
13076           'article)
13077          ;; Check the cache.
13078          ((and gnus-use-cache
13079                (numberp article)
13080                (gnus-cache-request-article article group))
13081           'article)
13082          ;; Get the article and put into the article buffer.
13083          ((or (stringp article) (numberp article))
13084           (let ((gnus-override-method
13085                  (and (stringp article) gnus-refer-article-method))
13086                 (buffer-read-only nil))
13087             (erase-buffer)
13088             (gnus-kill-all-overlays)
13089             (if (gnus-request-article article group (current-buffer))
13090                 (progn
13091                   (and gnus-keep-backlog
13092                        (gnus-backlog-enter-article
13093                         group article (current-buffer)))
13094                   'article))))
13095          ;; It was a pseudo.
13096          (t article)))
13097
13098     ;; Take the article from the original article buffer
13099     ;; and place it in the buffer it's supposed to be in.
13100     (setq gnus-original-article (cons group article))
13101     (when (and (get-buffer gnus-article-buffer)
13102                (equal (buffer-name (current-buffer))
13103                       (buffer-name (get-buffer gnus-article-buffer))))
13104       (save-excursion
13105         (if (get-buffer gnus-original-article-buffer)
13106             (set-buffer (get-buffer gnus-original-article-buffer))
13107           (set-buffer (get-buffer-create gnus-original-article-buffer))
13108           (buffer-disable-undo (current-buffer))
13109           (setq major-mode 'gnus-original-article-mode)
13110           (setq buffer-read-only t)
13111           (gnus-add-current-to-buffer-list))
13112         (let (buffer-read-only)
13113           (erase-buffer)
13114           (insert-buffer-substring gnus-article-buffer))))
13115     
13116     ;; Update sparse articles.
13117     (when (memq article gnus-newsgroup-sparse)
13118       (gnus-summary-update-article article))))
13119
13120 (defun gnus-read-header (id)
13121   "Read the headers of article ID and enter them into the Gnus system."
13122   (let ((group gnus-newsgroup-name)
13123         (headers gnus-newsgroup-headers)
13124         header where)
13125     ;; First we check to see whether the header in question is already
13126     ;; fetched.
13127     (if (stringp id)
13128         ;; This is a Message-ID.
13129         (setq header (gnus-id-to-header id))
13130       ;; This is an article number.
13131       (setq header (gnus-summary-article-header id)))
13132     (if header
13133         ;; We have found the header.
13134         header
13135       ;; We have to really fetch the header to this article.
13136       (when (setq where
13137                   (if (gnus-check-backend-function 'request-head group)
13138                       (gnus-request-head id group)
13139                     (gnus-request-article id group)))
13140         (save-excursion
13141           (set-buffer nntp-server-buffer)
13142           (and (search-forward "\n\n" nil t)
13143                (delete-region (1- (point)) (point-max)))
13144           (goto-char (point-max))
13145           (insert ".\n")
13146           (goto-char (point-min))
13147           (insert "211 "
13148                   (int-to-string
13149                    (cond
13150                     ((numberp id)
13151                      id)
13152                     ((cdr where)
13153                      (cdr where))
13154                     (t
13155                      gnus-reffed-article-number)))
13156                   " Article retrieved.\n"))
13157         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13158             () ; Malformed head.
13159           (if (and (stringp id)
13160                    (not (string= (gnus-group-real-name group)
13161                                  (car where))))
13162               ;; If we fetched by Message-ID and the article came
13163               ;; from a different group, we fudge some bogus article
13164               ;; numbers for this article.
13165               (mail-header-set-number header gnus-reffed-article-number))
13166           (decf gnus-reffed-article-number)
13167           (push header gnus-newsgroup-headers)
13168           (setq gnus-current-headers header)
13169           (push (mail-header-number header) gnus-newsgroup-limit)
13170           header)))))
13171
13172 (defun gnus-article-prepare (article &optional all-headers header)
13173   "Prepare ARTICLE in article mode buffer.
13174 ARTICLE should either be an article number or a Message-ID.
13175 If ARTICLE is an id, HEADER should be the article headers.
13176 If ALL-HEADERS is non-nil, no headers are hidden."
13177   (save-excursion
13178     ;; Make sure we start in a summary buffer.
13179     (unless (eq major-mode 'gnus-summary-mode)
13180       (set-buffer gnus-summary-buffer))
13181     (setq gnus-summary-buffer (current-buffer))
13182     ;; Make sure the connection to the server is alive.
13183     (unless (gnus-server-opened
13184              (gnus-find-method-for-group gnus-newsgroup-name))
13185       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13186       (gnus-request-group gnus-newsgroup-name t))
13187     (let* ((article (if header (mail-header-number header) article))
13188            (summary-buffer (current-buffer))
13189            (internal-hook gnus-article-internal-prepare-hook)
13190            (group gnus-newsgroup-name)
13191            result)
13192       (save-excursion
13193         (gnus-article-setup-buffer)
13194         (set-buffer gnus-article-buffer)
13195         ;; Deactivate active regions.
13196         (when (and (boundp 'transient-mark-mode)
13197                    transient-mark-mode)
13198           (setq mark-active nil))
13199         (if (not (setq result (let ((buffer-read-only nil))
13200                                 (gnus-request-article-this-buffer
13201                                  article group))))
13202             ;; There is no such article.
13203             (save-excursion
13204               (when (and (numberp article)
13205                          (not (memq article gnus-newsgroup-sparse)))
13206                 (setq gnus-article-current
13207                       (cons gnus-newsgroup-name article))
13208                 (set-buffer gnus-summary-buffer)
13209                 (setq gnus-current-article article)
13210                 (gnus-summary-mark-article article gnus-canceled-mark))
13211               (unless (memq article gnus-newsgroup-sparse)
13212                 (gnus-message
13213                  1 "No such article (may have expired or been canceled)")
13214                 (ding)
13215                 nil))
13216           (if (or (eq result 'pseudo) (eq result 'nneething))
13217               (progn
13218                 (save-excursion
13219                   (set-buffer summary-buffer)
13220                   (setq gnus-last-article gnus-current-article
13221                         gnus-newsgroup-history (cons gnus-current-article
13222                                                      gnus-newsgroup-history)
13223                         gnus-current-article 0
13224                         gnus-current-headers nil
13225                         gnus-article-current nil)
13226                   (if (eq result 'nneething)
13227                       (gnus-configure-windows 'summary)
13228                     (gnus-configure-windows 'article))
13229                   (gnus-set-global-variables))
13230                 (gnus-set-mode-line 'article))
13231             ;; The result from the `request' was an actual article -
13232             ;; or at least some text that is now displayed in the
13233             ;; article buffer.
13234             (if (and (numberp article)
13235                      (not (eq article gnus-current-article)))
13236                 ;; Seems like a new article has been selected.
13237                 ;; `gnus-current-article' must be an article number.
13238                 (save-excursion
13239                   (set-buffer summary-buffer)
13240                   (setq gnus-last-article gnus-current-article
13241                         gnus-newsgroup-history (cons gnus-current-article
13242                                                      gnus-newsgroup-history)
13243                         gnus-current-article article
13244                         gnus-current-headers
13245                         (gnus-summary-article-header gnus-current-article)
13246                         gnus-article-current
13247                         (cons gnus-newsgroup-name gnus-current-article))
13248                   (unless (vectorp gnus-current-headers)
13249                     (setq gnus-current-headers nil))
13250                   (gnus-summary-show-thread)
13251                   (run-hooks 'gnus-mark-article-hook)
13252                   (gnus-set-mode-line 'summary)
13253                   (and (gnus-visual-p 'article-highlight 'highlight)
13254                        (run-hooks 'gnus-visual-mark-article-hook))
13255                   ;; Set the global newsgroup variables here.
13256                   ;; Suggested by Jim Sisolak
13257                   ;; <sisolak@trans4.neep.wisc.edu>.
13258                   (gnus-set-global-variables)
13259                   (setq gnus-have-all-headers
13260                         (or all-headers gnus-show-all-headers))
13261                   (and gnus-use-cache
13262                        (vectorp (gnus-summary-article-header article))
13263                        (gnus-cache-possibly-enter-article
13264                         group article
13265                         (gnus-summary-article-header article)
13266                         (memq article gnus-newsgroup-marked)
13267                         (memq article gnus-newsgroup-dormant)
13268                         (memq article gnus-newsgroup-unreads)))))
13269             ;; Hooks for getting information from the article.
13270             ;; This hook must be called before being narrowed.
13271             (let (buffer-read-only)
13272               (run-hooks 'internal-hook)
13273               (run-hooks 'gnus-article-prepare-hook)
13274               ;; Decode MIME message.
13275               (if gnus-show-mime
13276                   (if (or (not gnus-strict-mime)
13277                           (gnus-fetch-field "Mime-Version"))
13278                       (funcall gnus-show-mime-method)
13279                     (funcall gnus-decode-encoded-word-method)))
13280               ;; Perform the article display hooks.
13281               (run-hooks 'gnus-article-display-hook))
13282             ;; Do page break.
13283             (goto-char (point-min))
13284             (and gnus-break-pages (gnus-narrow-to-page))
13285             (gnus-set-mode-line 'article)
13286             (gnus-configure-windows 'article)
13287             (goto-char (point-min))
13288             t))))))
13289
13290 (defun gnus-article-show-all-headers ()
13291   "Show all article headers in article mode buffer."
13292   (save-excursion
13293     (gnus-article-setup-buffer)
13294     (set-buffer gnus-article-buffer)
13295     (let ((buffer-read-only nil))
13296       (remove-text-properties (point-min) (point-max)
13297                               gnus-hidden-properties))))
13298
13299 (defun gnus-article-hide-headers-if-wanted ()
13300   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13301 Provided for backwards compatibility."
13302   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13303       gnus-inhibit-hiding
13304       (gnus-article-hide-headers)))
13305
13306 (defun gnus-article-hide-headers (&optional arg delete)
13307   "Toggle whether to hide unwanted headers and possibly sort them as well.
13308 If given a negative prefix, always show; if given a positive prefix,
13309 always hide."
13310   (interactive "P")
13311   (unless (gnus-article-check-hidden-text 'headers arg)
13312     ;; This function might be inhibited.
13313     (unless gnus-inhibit-hiding
13314       (save-excursion
13315         (set-buffer gnus-article-buffer)
13316         (save-restriction
13317           (let ((buffer-read-only nil)
13318                 (ignored (when (not (stringp gnus-visible-headers))
13319                            (cond ((stringp gnus-ignored-headers)
13320                                   gnus-ignored-headers)
13321                                  ((listp gnus-ignored-headers)
13322                                   (mapconcat 'identity gnus-ignored-headers
13323                                              "\\|")))))
13324                 (visible
13325                  (cond ((stringp gnus-visible-headers)
13326                         gnus-visible-headers)
13327                        ((and gnus-visible-headers
13328                              (listp gnus-visible-headers))
13329                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13330                 want-list beg want-l)
13331             ;; First we narrow to just the headers.
13332             (widen)
13333             (goto-char (point-min))
13334             ;; Hide any "From " lines at the beginning of (mail) articles.
13335             (while (looking-at "From ")
13336               (forward-line 1))
13337             (unless (bobp)
13338               (add-text-properties
13339                (point-min) (point)
13340                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13341             ;; Then treat the rest of the header lines.
13342             (narrow-to-region
13343              (point)
13344              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13345             ;; Then we use the two regular expressions
13346             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13347             ;; select which header lines is to remain visible in the
13348             ;; article buffer.
13349             (goto-char (point-min))
13350             (while (re-search-forward "^[^ \t]*:" nil t)
13351               (beginning-of-line)
13352               ;; We add the headers we want to keep to a list and delete
13353               ;; them from the buffer.
13354               (if (or (and visible (looking-at visible))
13355                       (and ignored (not (looking-at ignored))))
13356                   (progn
13357                     (push (buffer-substring
13358                            (setq beg (point))
13359                            (progn
13360                              (forward-line 1)
13361                              ;; Be sure to get multi-line headers...
13362                              (re-search-forward "^[^ \t]*:" nil t)
13363                              (beginning-of-line)
13364                              (point)))
13365                           want-list)
13366                     (delete-region beg (point)))
13367                 (forward-line 1)))
13368             ;; Sort the headers that we want to display.
13369             (setq want-list (sort want-list 'gnus-article-header-less))
13370             (goto-char (point-min))
13371             (while want-list
13372               (insert (pop want-list)))
13373             ;; We make the unwanted headers invisible.
13374             (if delete
13375                 (delete-region (point-min) (point-max))
13376               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13377               (add-text-properties
13378                (point) (point-max)
13379                (nconc (list 'gnus-type 'headers)
13380                       gnus-hidden-properties)))))))))
13381
13382 (defsubst gnus-article-header-rank (header)
13383   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13384   (let ((list gnus-sorted-header-list)
13385         (i 0))
13386     (while list
13387       (when (string-match (car list) header)
13388         (setq list nil))
13389       (setq list (cdr list))
13390       (incf i))
13391     i))
13392
13393 (defun gnus-article-header-less (h1 h2)
13394   "Say whether string H1 is \"less\" than string H2."
13395   (< (gnus-article-header-rank h1)
13396      (gnus-article-header-rank h2)))
13397
13398 (defun gnus-article-hide-boring-headers (&optional arg)
13399   "Toggle hiding of headers that aren't very interesting.
13400 If given a negative prefix, always show; if given a positive prefix,
13401 always hide."
13402   (interactive "P")
13403   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13404     (save-excursion
13405       (set-buffer gnus-article-buffer)
13406       (save-restriction
13407         (let ((buffer-read-only nil)
13408               (list gnus-boring-article-headers)
13409               (inhibit-point-motion-hooks t)
13410               elem)
13411           (nnheader-narrow-to-headers)
13412           (while list
13413             (setq elem (pop list))
13414             (goto-char (point-min))
13415             (cond
13416              ;; Hide empty headers.
13417              ((eq elem 'empty)
13418               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13419                 (forward-line -1)
13420                 (add-text-properties
13421                  (progn (beginning-of-line) (point))
13422                  (progn 
13423                    (end-of-line)
13424                    (if (re-search-forward "^[^ \t]" nil t)
13425                        (match-beginning 0)
13426                      (point-max)))
13427                  (nconc (list 'gnus-type 'boring-headers)
13428                         gnus-hidden-properties))))
13429              ;; Hide boring Newsgroups header.
13430              ((eq elem 'newsgroups)
13431               (when (equal (mail-fetch-field "newsgroups")
13432                            (gnus-group-real-name gnus-newsgroup-name))
13433                 (gnus-article-hide-header "newsgroups")))
13434              ((eq elem 'followup-to)
13435               (when (equal (mail-fetch-field "followup-to")
13436                            (mail-fetch-field "newsgroups"))
13437                 (gnus-article-hide-header "followup-to")))
13438              ((eq elem 'reply-to)
13439               (let ((from (mail-fetch-field "from"))
13440                     (reply-to (mail-fetch-field "reply-to")))
13441                 (when (and
13442                        from reply-to
13443                        (equal 
13444                         (nth 1 (mail-extract-address-components from))
13445                         (nth 1 (mail-extract-address-components reply-to))))
13446                   (gnus-article-hide-header "reply-to"))))
13447              ((eq elem 'date)
13448               (let ((date (mail-fetch-field "date")))
13449                 (when (and date
13450                            (< (gnus-days-between date (current-time-string))
13451                               4))
13452                   (gnus-article-hide-header "date")))))))))))
13453
13454 (defun gnus-article-hide-header (header)
13455   (save-excursion
13456     (goto-char (point-min))
13457     (when (re-search-forward (concat "^" header ":") nil t)
13458       (add-text-properties
13459        (progn (beginning-of-line) (point))
13460        (progn 
13461          (end-of-line)
13462          (if (re-search-forward "^[^ \t]" nil t)
13463              (match-beginning 0)
13464            (point-max)))
13465        (nconc (list 'gnus-type 'boring-headers)
13466               gnus-hidden-properties)))))
13467
13468 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13469 (defun gnus-article-treat-overstrike ()
13470   "Translate overstrikes into bold text."
13471   (interactive)
13472   (save-excursion
13473     (set-buffer gnus-article-buffer)
13474     (let ((buffer-read-only nil))
13475       (while (search-forward "\b" nil t)
13476         (let ((next (following-char))
13477               (previous (char-after (- (point) 2))))
13478           (cond ((eq next previous)
13479                  (put-text-property (- (point) 2) (point) 'invisible t)
13480                  (put-text-property (point) (1+ (point)) 'face 'bold))
13481                 ((eq next ?_)
13482                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13483                  (put-text-property
13484                   (- (point) 2) (1- (point)) 'face 'underline))
13485                 ((eq previous ?_)
13486                  (put-text-property (- (point) 2) (point) 'invisible t)
13487                  (put-text-property
13488                   (point) (1+ (point))  'face 'underline))))))))
13489
13490 (defun gnus-article-word-wrap ()
13491   "Format too long lines."
13492   (interactive)
13493   (save-excursion
13494     (set-buffer gnus-article-buffer)
13495     (let ((buffer-read-only nil)
13496           p)
13497       (widen)
13498       (goto-char (point-min))
13499       (search-forward "\n\n" nil t)
13500       (end-of-line 1)
13501       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13502             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13503             (adaptive-fill-mode t))
13504         (while (not (eobp))
13505           (and (>= (current-column) (min fill-column (window-width)))
13506                (/= (preceding-char) ?:)
13507                (fill-paragraph nil))
13508           (end-of-line 2))))))
13509
13510 (defun gnus-article-remove-cr ()
13511   "Remove carriage returns from an article."
13512   (interactive)
13513   (save-excursion
13514     (set-buffer gnus-article-buffer)
13515     (let ((buffer-read-only nil))
13516       (goto-char (point-min))
13517       (while (search-forward "\r" nil t)
13518         (replace-match "" t t)))))
13519
13520 (defun gnus-article-remove-trailing-blank-lines ()
13521   "Remove all trailing blank lines from the article."
13522   (interactive)
13523   (save-excursion
13524     (set-buffer gnus-article-buffer)
13525     (let ((buffer-read-only nil))
13526       (goto-char (point-max))
13527       (delete-region
13528        (point)
13529        (progn
13530          (while (looking-at "^[ \t]*$")
13531            (forward-line -1))
13532          (forward-line 1)
13533          (point))))))
13534
13535 (defun gnus-article-display-x-face (&optional force)
13536   "Look for an X-Face header and display it if present."
13537   (interactive (list 'force))
13538   (save-excursion
13539     (set-buffer gnus-article-buffer)
13540     ;; Delete the old process, if any.
13541     (when (process-status "gnus-x-face")
13542       (delete-process "gnus-x-face"))
13543     (let ((inhibit-point-motion-hooks t)
13544           (case-fold-search nil)
13545           from)
13546       (save-restriction
13547         (nnheader-narrow-to-headers)
13548         (setq from (mail-fetch-field "from"))
13549         (goto-char (point-min))
13550         (when (and gnus-article-x-face-command
13551                    (or force
13552                        ;; Check whether this face is censored.
13553                        (not gnus-article-x-face-too-ugly)
13554                        (and gnus-article-x-face-too-ugly from
13555                             (not (string-match gnus-article-x-face-too-ugly
13556                                                from))))
13557                    ;; Has to be present.
13558                    (re-search-forward "^X-Face: " nil t))
13559           ;; We now have the area of the buffer where the X-Face is stored.
13560           (let ((beg (point))
13561                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13562             ;; We display the face.
13563             (if (symbolp gnus-article-x-face-command)
13564                 ;; The command is a lisp function, so we call it.
13565                 (if (gnus-functionp gnus-article-x-face-command)
13566                     (funcall gnus-article-x-face-command beg end)
13567                   (error "%s is not a function" gnus-article-x-face-command))
13568               ;; The command is a string, so we interpret the command
13569               ;; as a, well, command, and fork it off.
13570               (let ((process-connection-type nil))
13571                 (process-kill-without-query
13572                  (start-process
13573                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13574                 (process-send-region "gnus-x-face" beg end)
13575                 (process-send-eof "gnus-x-face")))))))))
13576
13577 (defun gnus-headers-decode-quoted-printable ()
13578   "Hack to remove QP encoding from headers."
13579   (let ((case-fold-search t)
13580         (inhibit-point-motion-hooks t)
13581         string)
13582     (goto-char (point-min))
13583     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13584       (setq string (match-string 1))
13585       (narrow-to-region (match-beginning 0) (match-end 0))
13586       (delete-region (point-min) (point-max))
13587       (insert string)
13588       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13589       (subst-char-in-region (point-min) (point-max) ?_ ? )
13590       (widen)
13591       (goto-char (point-min)))))
13592
13593 (defun gnus-article-de-quoted-unreadable (&optional force)
13594   "Do a naive translation of a quoted-printable-encoded article.
13595 This is in no way, shape or form meant as a replacement for real MIME
13596 processing, but is simply a stop-gap measure until MIME support is
13597 written.
13598 If FORCE, decode the article whether it is marked as quoted-printable
13599 or not."
13600   (interactive (list 'force))
13601   (save-excursion
13602     (set-buffer gnus-article-buffer)
13603     (let ((case-fold-search t)
13604           (buffer-read-only nil)
13605           (type (gnus-fetch-field "content-transfer-encoding")))
13606       (when (or force
13607                 (and type (string-match "quoted-printable" type)))
13608         (gnus-headers-decode-quoted-printable)
13609         (goto-char (point-min))
13610         (search-forward "\n\n" nil 'move)
13611         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13612
13613 (defun gnus-mime-decode-quoted-printable (from to)
13614   "Decode Quoted-Printable in the region between FROM and TO."
13615   (goto-char from)
13616   (while (search-forward "=" to t)
13617     (cond ((eq (following-char) ?\n)
13618            (delete-char -1)
13619            (delete-char 1))
13620           ((looking-at "[0-9A-F][0-9A-F]")
13621            (delete-char -1)
13622            (insert (hexl-hex-string-to-integer
13623                     (buffer-substring (point) (+ 2 (point)))))
13624            (delete-char 2))
13625           ((looking-at "=")
13626            (delete-char 1))
13627           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13628
13629 (defun gnus-article-hide-pgp (&optional arg)
13630   "Toggle hiding of any PGP headers and signatures in the current article.
13631 If given a negative prefix, always show; if given a positive prefix,
13632 always hide."
13633   (interactive "P")
13634   (unless (gnus-article-check-hidden-text 'pgp arg)
13635     (save-excursion
13636       (set-buffer gnus-article-buffer)
13637       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13638             buffer-read-only beg end)
13639         (widen)
13640         (goto-char (point-min))
13641         ;; Hide the "header".
13642         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13643              (add-text-properties (match-beginning 0) (match-end 0) props))
13644         (setq beg (point))
13645         ;; Hide the actual signature.
13646         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13647              (setq end (match-beginning 0))
13648              (add-text-properties
13649               (match-beginning 0)
13650               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13651                   (match-end 0)
13652                 ;; Perhaps we shouldn't hide to the end of the buffer
13653                 ;; if there is no end to the signature?
13654                 (point-max))
13655               props))
13656         ;; Hide "- " PGP quotation markers.
13657         (when (and beg end)
13658           (narrow-to-region beg end)
13659           (goto-char (point-min))
13660           (while (re-search-forward "^- " nil t)
13661             (add-text-properties (match-beginning 0) (match-end 0) props))
13662           (widen))))))
13663
13664 (defun gnus-article-hide-signature (&optional arg)
13665   "Hide the signature in the current article.
13666 If given a negative prefix, always show; if given a positive prefix,
13667 always hide."
13668   (interactive "P")
13669   (unless (gnus-article-check-hidden-text 'signature arg)
13670     (save-excursion
13671       (set-buffer gnus-article-buffer)
13672       (save-restriction
13673         (let ((buffer-read-only nil))
13674           (when (gnus-narrow-to-signature)
13675             (add-text-properties
13676              (point-min) (point-max)
13677              (nconc (list 'gnus-type 'signature)
13678                     gnus-hidden-properties))))))))
13679
13680 (defvar gnus-signature-limit nil
13681   "Provide a limit to what is considered a signature.
13682 If it is a number, no signature may not be longer (in characters) than
13683 that number.  If it is a function, the function will be called without
13684 any parameters, and if it returns nil, there is no signature in the
13685 buffer.  If it is a string, it will be used as a regexp.  If it
13686 matches, the text in question is not a signature.")
13687
13688 (defun gnus-narrow-to-signature ()
13689   "Narrow to the signature."
13690   (widen)
13691   (goto-char (point-max))
13692   (when (re-search-backward gnus-signature-separator nil t)
13693     (forward-line 1)
13694     (when (or (null gnus-signature-limit)
13695               (and (numberp gnus-signature-limit)
13696                    (< (- (point-max) (point)) gnus-signature-limit))
13697               (and (gnus-functionp gnus-signature-limit)
13698                    (funcall gnus-signature-limit))
13699               (and (stringp gnus-signature-limit)
13700                    (not (re-search-forward gnus-signature-limit nil t))))
13701       (narrow-to-region (point) (point-max))
13702       t)))
13703
13704 (defun gnus-article-check-hidden-text (type arg)
13705   "Return nil if hiding is necessary."
13706   (save-excursion
13707     (set-buffer gnus-article-buffer)
13708     (let ((hide (gnus-article-hidden-text-p type)))
13709       (cond ((or (and (null arg) (eq hide 'hidden))
13710                  (and arg (< 0 (prefix-numeric-value arg))))
13711              (gnus-article-show-hidden-text type))
13712             ((eq hide 'shown)
13713              (gnus-article-show-hidden-text type t))
13714             (t nil)))))
13715
13716 (defun gnus-article-hidden-text-p (type)
13717   "Say whether the current buffer contains hidden text of type TYPE."
13718   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13719         prop)
13720     (when pos
13721       (if (get-text-property pos 'invisible)
13722           'hidden
13723         'shown))))
13724
13725 (defun gnus-article-hide (&optional arg force)
13726   "Hide all the gruft in the current article.
13727 This means that PGP stuff, signatures, cited text and (some)
13728 headers will be hidden.
13729 If given a prefix, show the hidden text instead."
13730   (interactive (list current-prefix-arg 'force))
13731   (gnus-article-hide-headers arg)
13732   (gnus-article-hide-pgp arg)
13733   (gnus-article-hide-citation-maybe arg force)
13734   (gnus-article-hide-signature arg))
13735
13736 (defun gnus-article-show-hidden-text (type &optional hide)
13737   "Show all hidden text of type TYPE.
13738 If HIDE, hide the text instead."
13739   (save-excursion
13740     (set-buffer gnus-article-buffer)
13741     (let ((buffer-read-only nil)
13742           (inhibit-point-motion-hooks t)
13743           (beg (point-min)))
13744       (while (gnus-goto-char (text-property-any
13745                               beg (point-max) 'gnus-type type))
13746         (setq beg (point))
13747         (forward-char)
13748         (if hide
13749             (add-text-properties beg (point) gnus-hidden-properties)
13750           (remove-text-properties beg (point) gnus-hidden-properties))
13751         (setq beg (point)))
13752       t)))
13753
13754 (defvar gnus-article-time-units
13755   `((year . ,(* 365.25 24 60 60))
13756     (week . ,(* 7 24 60 60))
13757     (day . ,(* 24 60 60))
13758     (hour . ,(* 60 60))
13759     (minute . 60)
13760     (second . 1))
13761   "Mapping from time units to seconds.")
13762
13763 (defun gnus-article-date-ut (&optional type highlight)
13764   "Convert DATE date to universal time in the current article.
13765 If TYPE is `local', convert to local time; if it is `lapsed', output
13766 how much time has lapsed since DATE."
13767   (interactive (list 'ut t))
13768   (let* ((header (or gnus-current-headers
13769                      (gnus-summary-article-header) ""))
13770          (date (and (vectorp header) (mail-header-date header)))
13771          (date-regexp "^Date: \\|^X-Sent: ")
13772          (now (current-time))
13773          (inhibit-point-motion-hooks t))
13774     (when (and date (not (string= date "")))
13775       (save-excursion
13776         (set-buffer gnus-article-buffer)
13777         (save-restriction
13778           (nnheader-narrow-to-headers)
13779           (let ((buffer-read-only nil))
13780             ;; Delete any old Date headers.
13781             (if (zerop (nnheader-remove-header date-regexp t))
13782                 (beginning-of-line)
13783               (goto-char (point-max)))
13784             (insert
13785              (cond
13786               ;; Convert to the local timezone.  We have to slap a
13787               ;; `condition-case' round the calls to the timezone
13788               ;; functions since they aren't particularly resistant to
13789               ;; buggy dates.
13790               ((eq type 'local)
13791                (concat "Date: " (condition-case ()
13792                                     (timezone-make-date-arpa-standard date)
13793                                   (error date))
13794                        "\n"))
13795               ;; Convert to Universal Time.
13796               ((eq type 'ut)
13797                (concat "Date: "
13798                        (condition-case ()
13799                            (timezone-make-date-arpa-standard date nil "UT")
13800                          (error date))
13801                        "\n"))
13802               ;; Get the original date from the article.
13803               ((eq type 'original)
13804                (concat "Date: " date "\n"))
13805               ;; Do an X-Sent lapsed format.
13806               ((eq type 'lapsed)
13807                ;; If the date is seriously mangled, the timezone
13808                ;; functions are liable to bug out, so we condition-case
13809                ;; the entire thing.
13810                (let* ((real-time
13811                        (condition-case ()
13812                            (gnus-time-minus
13813                             (gnus-encode-date
13814                              (timezone-make-date-arpa-standard
13815                               (current-time-string now)
13816                               (current-time-zone now) "UT"))
13817                             (gnus-encode-date
13818                              (timezone-make-date-arpa-standard
13819                               date nil "UT")))
13820                          (error '(0 0))))
13821                       (real-sec (+ (* (float (car real-time)) 65536)
13822                                    (cadr real-time)))
13823                       (sec (abs real-sec))
13824                       num prev)
13825                  (if (zerop sec)
13826                      "X-Sent: Now\n"
13827                    (concat
13828                     "X-Sent: "
13829                     ;; This is a bit convoluted, but basically we go
13830                     ;; through the time units for years, weeks, etc,
13831                     ;; and divide things to see whether that results
13832                     ;; in positive answers.
13833                     (mapconcat
13834                      (lambda (unit)
13835                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13836                            ;; The (remaining) seconds are too few to
13837                            ;; be divided into this time unit.
13838                            ""
13839                          ;; It's big enough, so we output it.
13840                          (setq sec (- sec (* num (cdr unit))))
13841                          (prog1
13842                              (concat (if prev ", " "") (int-to-string
13843                                                         (floor num))
13844                                      " " (symbol-name (car unit))
13845                                      (if (> num 1) "s" ""))
13846                            (setq prev t))))
13847                      gnus-article-time-units "")
13848                     ;; If dates are odd, then it might appear like the
13849                     ;; article was sent in the future.
13850                     (if (> real-sec 0)
13851                         " ago\n"
13852                       " in the future\n")))))
13853               (t
13854                (error "Unknown conversion type: %s" type)))))
13855           ;; Do highlighting.
13856           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13857             (gnus-article-highlight-headers)))))))
13858
13859 (defun gnus-article-date-local (&optional highlight)
13860   "Convert the current article date to the local timezone."
13861   (interactive (list t))
13862   (gnus-article-date-ut 'local highlight))
13863
13864 (defun gnus-article-date-original (&optional highlight)
13865   "Convert the current article date to what it was originally.
13866 This is only useful if you have used some other date conversion
13867 function and want to see what the date was before converting."
13868   (interactive (list t))
13869   (gnus-article-date-ut 'original highlight))
13870
13871 (defun gnus-article-date-lapsed (&optional highlight)
13872   "Convert the current article date to time lapsed since it was sent."
13873   (interactive (list t))
13874   (gnus-article-date-ut 'lapsed highlight))
13875
13876 (defun gnus-article-maybe-highlight ()
13877   "Do some article highlighting if `gnus-visual' is non-nil."
13878   (if (gnus-visual-p 'article-highlight 'highlight)
13879       (gnus-article-highlight-some)))
13880
13881 ;; Article savers.
13882
13883 (defun gnus-output-to-rmail (file-name)
13884   "Append the current article to an Rmail file named FILE-NAME."
13885   (require 'rmail)
13886   ;; Most of these codes are borrowed from rmailout.el.
13887   (setq file-name (expand-file-name file-name))
13888   (setq rmail-default-rmail-file file-name)
13889   (let ((artbuf (current-buffer))
13890         (tmpbuf (get-buffer-create " *Gnus-output*")))
13891     (save-excursion
13892       (or (get-file-buffer file-name)
13893           (file-exists-p file-name)
13894           (if (gnus-yes-or-no-p
13895                (concat "\"" file-name "\" does not exist, create it? "))
13896               (let ((file-buffer (create-file-buffer file-name)))
13897                 (save-excursion
13898                   (set-buffer file-buffer)
13899                   (rmail-insert-rmail-file-header)
13900                   (let ((require-final-newline nil))
13901                     (write-region (point-min) (point-max) file-name t 1)))
13902                 (kill-buffer file-buffer))
13903             (error "Output file does not exist")))
13904       (set-buffer tmpbuf)
13905       (buffer-disable-undo (current-buffer))
13906       (erase-buffer)
13907       (insert-buffer-substring artbuf)
13908       (gnus-convert-article-to-rmail)
13909       ;; Decide whether to append to a file or to an Emacs buffer.
13910       (let ((outbuf (get-file-buffer file-name)))
13911         (if (not outbuf)
13912             (append-to-file (point-min) (point-max) file-name)
13913           ;; File has been visited, in buffer OUTBUF.
13914           (set-buffer outbuf)
13915           (let ((buffer-read-only nil)
13916                 (msg (and (boundp 'rmail-current-message)
13917                           (symbol-value 'rmail-current-message))))
13918             ;; If MSG is non-nil, buffer is in RMAIL mode.
13919             (if msg
13920                 (progn (widen)
13921                        (narrow-to-region (point-max) (point-max))))
13922             (insert-buffer-substring tmpbuf)
13923             (if msg
13924                 (progn
13925                   (goto-char (point-min))
13926                   (widen)
13927                   (search-backward "\^_")
13928                   (narrow-to-region (point) (point-max))
13929                   (goto-char (1+ (point-min)))
13930                   (rmail-count-new-messages t)
13931                   (rmail-show-message msg)))))))
13932     (kill-buffer tmpbuf)))
13933
13934 (defun gnus-output-to-file (file-name)
13935   "Append the current article to a file named FILE-NAME."
13936   (setq file-name (expand-file-name file-name))
13937   (let ((artbuf (current-buffer))
13938         (tmpbuf (get-buffer-create " *Gnus-output*")))
13939     (save-excursion
13940       (set-buffer tmpbuf)
13941       (buffer-disable-undo (current-buffer))
13942       (erase-buffer)
13943       (insert-buffer-substring artbuf)
13944       ;; Append newline at end of the buffer as separator, and then
13945       ;; save it to file.
13946       (goto-char (point-max))
13947       (insert "\n")
13948       (append-to-file (point-min) (point-max) file-name))
13949     (kill-buffer tmpbuf)))
13950
13951 (defun gnus-convert-article-to-rmail ()
13952   "Convert article in current buffer to Rmail message format."
13953   (let ((buffer-read-only nil))
13954     ;; Convert article directly into Babyl format.
13955     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
13956     (goto-char (point-min))
13957     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
13958     (while (search-forward "\n\^_" nil t) ;single char
13959       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
13960     (goto-char (point-max))
13961     (insert "\^_")))
13962
13963 (defun gnus-narrow-to-page (&optional arg)
13964   "Narrow the article buffer to a page.
13965 If given a numerical ARG, move forward ARG pages."
13966   (interactive "P")
13967   (setq arg (if arg (prefix-numeric-value arg) 0))
13968   (save-excursion
13969     (set-buffer gnus-article-buffer)
13970     (goto-char (point-min))
13971     (widen)
13972     (when (gnus-visual-p 'page-marker)
13973       (let ((buffer-read-only nil))
13974         (gnus-remove-text-with-property 'gnus-prev)
13975         (gnus-remove-text-with-property 'gnus-next)))
13976     (when
13977         (cond ((< arg 0)
13978                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
13979               ((> arg 0)
13980                (re-search-forward page-delimiter nil 'move arg)))
13981       (goto-char (match-end 0)))
13982     (narrow-to-region
13983      (point)
13984      (if (re-search-forward page-delimiter nil 'move)
13985          (match-beginning 0)
13986        (point)))
13987     (when (and (gnus-visual-p 'page-marker)
13988                (not (= (point-min) 1)))
13989       (save-excursion
13990         (goto-char (point-min))
13991         (gnus-insert-prev-page-button)))
13992     (when (and (gnus-visual-p 'page-marker)
13993                (not (= (1- (point-max)) (buffer-size))))
13994       (save-excursion
13995         (goto-char (point-max))
13996         (gnus-insert-next-page-button)))))
13997
13998 ;; Article mode commands
13999
14000 (defun gnus-article-goto-next-page ()
14001   "Show the next page of the article."
14002   (interactive)
14003   (when (gnus-article-next-page)
14004     (gnus-article-read-summary-keys nil ?n)))
14005
14006 (defun gnus-article-goto-prev-page ()
14007   "Show the next page of the article."
14008   (interactive)
14009   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14010     (gnus-article-prev-page nil)))
14011
14012 (defun gnus-article-next-page (&optional lines)
14013   "Show the next page of the current article.
14014 If end of article, return non-nil.  Otherwise return nil.
14015 Argument LINES specifies lines to be scrolled up."
14016   (interactive "p")
14017   (move-to-window-line -1)
14018   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14019   (if (save-excursion
14020         (end-of-line)
14021         (and (pos-visible-in-window-p)  ;Not continuation line.
14022              (eobp)))
14023       ;; Nothing in this page.
14024       (if (or (not gnus-break-pages)
14025               (save-excursion
14026                 (save-restriction
14027                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14028           t                             ;Nothing more.
14029         (gnus-narrow-to-page 1)         ;Go to next page.
14030         nil)
14031     ;; More in this page.
14032     (condition-case ()
14033         (scroll-up lines)
14034       (end-of-buffer
14035        ;; Long lines may cause an end-of-buffer error.
14036        (goto-char (point-max))))
14037     nil))
14038
14039 (defun gnus-article-prev-page (&optional lines)
14040   "Show previous page of current article.
14041 Argument LINES specifies lines to be scrolled down."
14042   (interactive "p")
14043   (move-to-window-line 0)
14044   (if (and gnus-break-pages
14045            (bobp)
14046            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14047       (progn
14048         (gnus-narrow-to-page -1)        ;Go to previous page.
14049         (goto-char (point-max))
14050         (recenter -1))
14051     (condition-case ()
14052         (scroll-down lines)
14053       (error nil))))
14054
14055 (defun gnus-article-refer-article ()
14056   "Read article specified by message-id around point."
14057   (interactive)
14058   (let ((point (point)))
14059     (search-forward ">" nil t)          ;Move point to end of "<....>".
14060     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14061         (let ((message-id (match-string 1)))
14062           (goto-char point)
14063           (set-buffer gnus-summary-buffer)
14064           (gnus-summary-refer-article message-id))
14065       (goto-char (point))
14066       (error "No references around point"))))
14067
14068 (defun gnus-article-show-summary ()
14069   "Reconfigure windows to show summary buffer."
14070   (interactive)
14071   (gnus-configure-windows 'article)
14072   (gnus-summary-goto-subject gnus-current-article))
14073
14074 (defun gnus-article-describe-briefly ()
14075   "Describe article mode commands briefly."
14076   (interactive)
14077   (gnus-message 6
14078                 (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")))
14079
14080 (defun gnus-article-summary-command ()
14081   "Execute the last keystroke in the summary buffer."
14082   (interactive)
14083   (let ((obuf (current-buffer))
14084         (owin (current-window-configuration))
14085         func)
14086     (switch-to-buffer gnus-summary-buffer 'norecord)
14087     (setq func (lookup-key (current-local-map) (this-command-keys)))
14088     (call-interactively func)
14089     (set-buffer obuf)
14090     (set-window-configuration owin)
14091     (set-window-point (get-buffer-window (current-buffer)) (point))))
14092
14093 (defun gnus-article-summary-command-nosave ()
14094   "Execute the last keystroke in the summary buffer."
14095   (interactive)
14096   (let (func)
14097     (pop-to-buffer gnus-summary-buffer 'norecord)
14098     (setq func (lookup-key (current-local-map) (this-command-keys)))
14099     (call-interactively func)))
14100
14101 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14102   "Read a summary buffer key sequence and execute it from the article buffer."
14103   (interactive "P")
14104   (let ((nosaves
14105          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14106            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14107            "=" "^" "\M-^"))
14108         keys)
14109     (save-excursion
14110       (set-buffer gnus-summary-buffer)
14111       (push (or key last-command-event) unread-command-events)
14112       (setq keys (read-key-sequence nil)))
14113     (message "")
14114
14115     (if (member keys nosaves)
14116         (let (func)
14117           (pop-to-buffer gnus-summary-buffer 'norecord)
14118           (if (setq func (lookup-key (current-local-map) keys))
14119               (call-interactively func)
14120             (ding)))
14121       (let ((obuf (current-buffer))
14122             (owin (current-window-configuration))
14123             (opoint (point))
14124             func in-buffer)
14125         (if not-restore-window
14126             (pop-to-buffer gnus-summary-buffer 'norecord)
14127           (switch-to-buffer gnus-summary-buffer 'norecord))
14128         (setq in-buffer (current-buffer))
14129         (if (setq func (lookup-key (current-local-map) keys))
14130             (call-interactively func)
14131           (ding))
14132         (when (eq in-buffer (current-buffer))
14133           (set-buffer obuf)
14134           (unless not-restore-window
14135             (set-window-configuration owin))
14136           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14137
14138 \f
14139 ;;;
14140 ;;; Kill file handling.
14141 ;;;
14142
14143 ;;;###autoload
14144 (defalias 'gnus-batch-kill 'gnus-batch-score)
14145 ;;;###autoload
14146 (defun gnus-batch-score ()
14147   "Run batched scoring.
14148 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14149 Newsgroups is a list of strings in Bnews format.  If you want to score
14150 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14151 score the alt hierarchy, you'd say \"!alt.all\"."
14152   (interactive)
14153   (let* ((yes-and-no
14154           (gnus-newsrc-parse-options
14155            (apply (function concat)
14156                   (mapcar (lambda (g) (concat g " "))
14157                           command-line-args-left))))
14158          (gnus-expert-user t)
14159          (nnmail-spool-file nil)
14160          (gnus-use-dribble-file nil)
14161          (yes (car yes-and-no))
14162          (no (cdr yes-and-no))
14163          group newsrc entry
14164          ;; Disable verbose message.
14165          gnus-novice-user gnus-large-newsgroup)
14166     ;; Eat all arguments.
14167     (setq command-line-args-left nil)
14168     ;; Start Gnus.
14169     (gnus)
14170     ;; Apply kills to specified newsgroups in command line arguments.
14171     (setq newsrc (cdr gnus-newsrc-alist))
14172     (while newsrc
14173       (setq group (car (car newsrc)))
14174       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14175       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14176                (and (car entry)
14177                     (or (eq (car entry) t)
14178                         (not (zerop (car entry)))))
14179                (if yes (string-match yes group) t)
14180                (or (null no) (not (string-match no group))))
14181           (progn
14182             (gnus-summary-read-group group nil t nil t)
14183             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14184                  (gnus-summary-exit))))
14185       (setq newsrc (cdr newsrc)))
14186     ;; Exit Emacs.
14187     (switch-to-buffer gnus-group-buffer)
14188     (gnus-group-save-newsrc)))
14189
14190 (defun gnus-apply-kill-file ()
14191   "Apply a kill file to the current newsgroup.
14192 Returns the number of articles marked as read."
14193   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14194           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14195       (gnus-apply-kill-file-internal)
14196     0))
14197
14198 (defun gnus-kill-save-kill-buffer ()
14199   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14200     (when (get-file-buffer file)
14201       (save-excursion
14202         (set-buffer (get-file-buffer file))
14203         (and (buffer-modified-p) (save-buffer))
14204         (kill-buffer (current-buffer))))))
14205
14206 (defvar gnus-kill-file-name "KILL"
14207   "Suffix of the kill files.")
14208
14209 (defun gnus-newsgroup-kill-file (newsgroup)
14210   "Return the name of a kill file name for NEWSGROUP.
14211 If NEWSGROUP is nil, return the global kill file name instead."
14212   (cond 
14213    ;; The global KILL file is placed at top of the directory.
14214    ((or (null newsgroup)
14215         (string-equal newsgroup ""))
14216     (expand-file-name gnus-kill-file-name
14217                       (or gnus-kill-files-directory "~/News")))
14218    ;; Append ".KILL" to newsgroup name.
14219    ((gnus-use-long-file-name 'not-kill)
14220     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14221                               "." gnus-kill-file-name)
14222                       (or gnus-kill-files-directory "~/News")))
14223    ;; Place "KILL" under the hierarchical directory.
14224    (t
14225     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14226                               "/" gnus-kill-file-name)
14227                       (or gnus-kill-files-directory "~/News")))))
14228
14229 \f
14230 ;;;
14231 ;;; Dribble file
14232 ;;;
14233
14234 (defvar gnus-dribble-ignore nil)
14235 (defvar gnus-dribble-eval-file nil)
14236
14237 (defun gnus-dribble-file-name ()
14238   "Return the dribble file for the current .newsrc."
14239   (concat
14240    (if gnus-dribble-directory
14241        (concat (file-name-as-directory gnus-dribble-directory)
14242                (file-name-nondirectory gnus-current-startup-file))
14243      gnus-current-startup-file)
14244    "-dribble"))
14245
14246 (defun gnus-dribble-enter (string)
14247   "Enter STRING into the dribble buffer."
14248   (if (and (not gnus-dribble-ignore)
14249            gnus-dribble-buffer
14250            (buffer-name gnus-dribble-buffer))
14251       (let ((obuf (current-buffer)))
14252         (set-buffer gnus-dribble-buffer)
14253         (insert string "\n")
14254         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14255         (set-buffer obuf))))
14256
14257 (defun gnus-dribble-read-file ()
14258   "Read the dribble file from disk."
14259   (let ((dribble-file (gnus-dribble-file-name)))
14260     (save-excursion
14261       (set-buffer (setq gnus-dribble-buffer
14262                         (get-buffer-create
14263                          (file-name-nondirectory dribble-file))))
14264       (gnus-add-current-to-buffer-list)
14265       (erase-buffer)
14266       (setq buffer-file-name dribble-file)
14267       (auto-save-mode t)
14268       (buffer-disable-undo (current-buffer))
14269       (bury-buffer (current-buffer))
14270       (set-buffer-modified-p nil)
14271       (let ((auto (make-auto-save-file-name))
14272             (gnus-dribble-ignore t))
14273         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14274           ;; Load whichever file is newest -- the auto save file
14275           ;; or the "real" file.
14276           (if (file-newer-than-file-p auto dribble-file)
14277               (insert-file-contents auto)
14278             (insert-file-contents dribble-file))
14279           (unless (zerop (buffer-size))
14280             (set-buffer-modified-p t))
14281           ;; Set the file modes to reflect the .newsrc file modes.
14282           (save-buffer)
14283           (when (file-exists-p gnus-current-startup-file)
14284             (set-file-modes dribble-file
14285                             (file-modes gnus-current-startup-file)))
14286           ;; Possibly eval the file later.
14287           (when (gnus-y-or-n-p
14288                  "Auto-save file exists.  Do you want to read it? ")
14289             (setq gnus-dribble-eval-file t)))))))
14290
14291 (defun gnus-dribble-eval-file ()
14292   (if (not gnus-dribble-eval-file)
14293       ()
14294     (setq gnus-dribble-eval-file nil)
14295     (save-excursion
14296       (let ((gnus-dribble-ignore t))
14297         (set-buffer gnus-dribble-buffer)
14298         (eval-buffer (current-buffer))))))
14299
14300 (defun gnus-dribble-delete-file ()
14301   (if (file-exists-p (gnus-dribble-file-name))
14302       (delete-file (gnus-dribble-file-name)))
14303   (if gnus-dribble-buffer
14304       (save-excursion
14305         (set-buffer gnus-dribble-buffer)
14306         (let ((auto (make-auto-save-file-name)))
14307           (if (file-exists-p auto)
14308               (delete-file auto))
14309           (erase-buffer)
14310           (set-buffer-modified-p nil)))))
14311
14312 (defun gnus-dribble-save ()
14313   (if (and gnus-dribble-buffer
14314            (buffer-name gnus-dribble-buffer))
14315       (save-excursion
14316         (set-buffer gnus-dribble-buffer)
14317         (save-buffer))))
14318
14319 (defun gnus-dribble-clear ()
14320   (save-excursion
14321     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14322         (progn
14323           (set-buffer gnus-dribble-buffer)
14324           (erase-buffer)
14325           (set-buffer-modified-p nil)
14326           (setq buffer-saved-size (buffer-size))))))
14327
14328 \f
14329 ;;;
14330 ;;; Server Communication
14331 ;;;
14332
14333 (defun gnus-start-news-server (&optional confirm)
14334   "Open a method for getting news.
14335 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14336   (let (how)
14337     (if gnus-current-select-method
14338         ;; Stream is already opened.
14339         nil
14340       ;; Open NNTP server.
14341       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14342       (if confirm
14343           (progn
14344             ;; Read server name with completion.
14345             (setq gnus-nntp-server
14346                   (completing-read "NNTP server: "
14347                                    (mapcar (lambda (server) (list server))
14348                                            (cons (list gnus-nntp-server)
14349                                                  gnus-secondary-servers))
14350                                    nil nil gnus-nntp-server))))
14351
14352       (if (and gnus-nntp-server
14353                (stringp gnus-nntp-server)
14354                (not (string= gnus-nntp-server "")))
14355           (setq gnus-select-method
14356                 (cond ((or (string= gnus-nntp-server "")
14357                            (string= gnus-nntp-server "::"))
14358                        (list 'nnspool (system-name)))
14359                       ((string-match "^:" gnus-nntp-server)
14360                        (list 'nnmh gnus-nntp-server
14361                              (list 'nnmh-directory
14362                                    (file-name-as-directory
14363                                     (expand-file-name
14364                                      (concat "~/" (substring
14365                                                    gnus-nntp-server 1)))))
14366                              (list 'nnmh-get-new-mail nil)))
14367                       (t
14368                        (list 'nntp gnus-nntp-server)))))
14369
14370       (setq how (car gnus-select-method))
14371       (cond ((eq how 'nnspool)
14372              (require 'nnspool)
14373              (gnus-message 5 "Looking up local news spool..."))
14374             ((eq how 'nnmh)
14375              (require 'nnmh)
14376              (gnus-message 5 "Looking up mh spool..."))
14377             (t
14378              (require 'nntp)))
14379       (setq gnus-current-select-method gnus-select-method)
14380       (run-hooks 'gnus-open-server-hook)
14381       (or
14382        ;; gnus-open-server-hook might have opened it
14383        (gnus-server-opened gnus-select-method)
14384        (gnus-open-server gnus-select-method)
14385        (gnus-y-or-n-p
14386         (format
14387          "%s (%s) open error: '%s'.     Continue? "
14388          (car gnus-select-method) (cadr gnus-select-method)
14389          (gnus-status-message gnus-select-method)))
14390        (progn
14391          (gnus-message 1 "Couldn't open server on %s"
14392                        (nth 1 gnus-select-method))
14393          (ding)
14394          nil)))))
14395
14396 (defun gnus-check-group (group)
14397   "Try to make sure that the server where GROUP exists is alive."
14398   (let ((method (gnus-find-method-for-group group)))
14399     (or (gnus-server-opened method)
14400         (gnus-open-server method))))
14401
14402 (defun gnus-check-server (&optional method)
14403   "Check whether the connection to METHOD is down.
14404 If METHOD is nil, use `gnus-select-method'.
14405 If it is down, start it up (again)."
14406   (let ((method (or method gnus-select-method)))
14407     ;; Transform virtual server names into select methods.
14408     (when (stringp method)
14409       (setq method (gnus-server-to-method method)))
14410     (if (gnus-server-opened method)
14411         ;; The stream is already opened.
14412         t
14413       ;; Open the server.
14414       (gnus-message 5 "Opening %s server%s..." (car method)
14415                     (if (equal (nth 1 method) "") ""
14416                       (format " on %s" (nth 1 method))))
14417       (run-hooks 'gnus-open-server-hook)
14418       (prog1
14419           (gnus-open-server method)
14420         (message "")))))
14421
14422 (defun gnus-get-function (method function)
14423   "Return a function symbol based on METHOD and FUNCTION."
14424   ;; Translate server names into methods.
14425   (unless method
14426     (error "Attempted use of a nil select method"))
14427   (when (stringp method)
14428     (setq method (gnus-server-to-method method)))
14429   (let ((func (intern (format "%s-%s" (car method) function))))
14430     ;; If the functions isn't bound, we require the backend in
14431     ;; question.
14432     (unless (fboundp func)
14433       (require (car method))
14434       (unless (fboundp func)
14435         ;; This backend doesn't implement this function.
14436         (error "No such function: %s" func)))
14437     func))
14438
14439 \f
14440 ;;;
14441 ;;; Interface functions to the backends.
14442 ;;;
14443
14444 (defun gnus-open-server (method)
14445   "Open a connection to METHOD."
14446   (let ((elem (assoc method gnus-opened-servers)))
14447     ;; If this method was previously denied, we just return nil.
14448     (if (eq (nth 1 elem) 'denied)
14449         (progn
14450           (gnus-message 1 "Denied server")
14451           nil)
14452       ;; Open the server.
14453       (let ((result
14454              (funcall (gnus-get-function method 'open-server)
14455                       (nth 1 method) (nthcdr 2 method))))
14456         ;; If this hasn't been opened before, we add it to the list.
14457         (unless elem
14458           (setq elem (list method nil)
14459                 gnus-opened-servers (cons elem gnus-opened-servers)))
14460         ;; Set the status of this server.
14461         (setcar (cdr elem) (if result 'ok 'denied))
14462         ;; Return the result from the "open" call.
14463         result))))
14464
14465 (defun gnus-close-server (method)
14466   "Close the connection to METHOD."
14467   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14468
14469 (defun gnus-request-list (method)
14470   "Request the active file from METHOD."
14471   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14472
14473 (defun gnus-request-list-newsgroups (method)
14474   "Request the newsgroups file from METHOD."
14475   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14476
14477 (defun gnus-request-newgroups (date method)
14478   "Request all new groups since DATE from METHOD."
14479   (funcall (gnus-get-function method 'request-newgroups)
14480            date (nth 1 method)))
14481
14482 (defun gnus-server-opened (method)
14483   "Check whether a connection to METHOD has been opened."
14484   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14485
14486 (defun gnus-status-message (method)
14487   "Return the status message from METHOD.
14488 If METHOD is a string, it is interpreted as a group name.   The method
14489 this group uses will be queried."
14490   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14491                   method)))
14492     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14493
14494 (defun gnus-request-group (group &optional dont-check method)
14495   "Request GROUP.  If DONT-CHECK, no information is required."
14496   (let ((method (or method (gnus-find-method-for-group group))))
14497     (funcall (gnus-get-function method 'request-group)
14498              (gnus-group-real-name group) (nth 1 method) dont-check)))
14499
14500 (defun gnus-request-asynchronous (group &optional articles)
14501   "Request that GROUP behave asynchronously.
14502 ARTICLES is the `data' of the group."
14503   (let ((method (gnus-find-method-for-group group)))
14504     (funcall (gnus-get-function method 'request-asynchronous)
14505              (gnus-group-real-name group) (nth 1 method) articles)))
14506
14507 (defun gnus-list-active-group (group)
14508   "Request active information on GROUP."
14509   (let ((method (gnus-find-method-for-group group))
14510         (func 'list-active-group))
14511     (when (gnus-check-backend-function func group)
14512       (funcall (gnus-get-function method func)
14513                (gnus-group-real-name group) (nth 1 method)))))
14514
14515 (defun gnus-request-group-description (group)
14516   "Request a description of GROUP."
14517   (let ((method (gnus-find-method-for-group group))
14518         (func 'request-group-description))
14519     (when (gnus-check-backend-function func group)
14520       (funcall (gnus-get-function method func)
14521                (gnus-group-real-name group) (nth 1 method)))))
14522
14523 (defun gnus-close-group (group)
14524   "Request the GROUP be closed."
14525   (let ((method (gnus-find-method-for-group group)))
14526     (funcall (gnus-get-function method 'close-group)
14527              (gnus-group-real-name group) (nth 1 method))))
14528
14529 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14530   "Request headers for ARTICLES in GROUP.
14531 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14532   (let ((method (gnus-find-method-for-group group)))
14533     (if (and gnus-use-cache (numberp (car articles)))
14534         (gnus-cache-retrieve-headers articles group fetch-old)
14535       (funcall (gnus-get-function method 'retrieve-headers)
14536                articles (gnus-group-real-name group) (nth 1 method)
14537                fetch-old))))
14538
14539 (defun gnus-retrieve-groups (groups method)
14540   "Request active information on GROUPS from METHOD."
14541   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14542
14543 (defun gnus-request-type (group &optional article)
14544   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14545   (let ((method (gnus-find-method-for-group group)))
14546     (if (not (gnus-check-backend-function 'request-type (car method)))
14547         'unknown
14548       (funcall (gnus-get-function method 'request-type)
14549                (gnus-group-real-name group) article))))
14550
14551 (defun gnus-request-update-mark (group article mark)
14552   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14553   (let ((method (gnus-find-method-for-group group)))
14554     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14555         mark
14556       (funcall (gnus-get-function method 'request-update-mark)
14557                (gnus-group-real-name group) article mark))))
14558
14559 (defun gnus-request-article (article group &optional buffer)
14560   "Request the ARTICLE in GROUP.
14561 ARTICLE can either be an article number or an article Message-ID.
14562 If BUFFER, insert the article in that group."
14563   (let ((method (gnus-find-method-for-group group)))
14564     (funcall (gnus-get-function method 'request-article)
14565              article (gnus-group-real-name group) (nth 1 method) buffer)))
14566
14567 (defun gnus-request-head (article group)
14568   "Request the head of ARTICLE in GROUP."
14569   (let ((method (gnus-find-method-for-group group)))
14570     (funcall (gnus-get-function method 'request-head)
14571              article (gnus-group-real-name group) (nth 1 method))))
14572
14573 (defun gnus-request-body (article group)
14574   "Request the body of ARTICLE in GROUP."
14575   (let ((method (gnus-find-method-for-group group)))
14576     (funcall (gnus-get-function method 'request-body)
14577              article (gnus-group-real-name group) (nth 1 method))))
14578
14579 (defun gnus-request-post (method)
14580   "Post the current buffer using METHOD."
14581   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14582
14583 (defun gnus-request-scan (group method)
14584   "Request a SCAN being performed in GROUP from METHOD.
14585 If GROUP is nil, all groups on METHOD are scanned."
14586   (let ((method (if group (gnus-find-method-for-group group) method)))
14587     (funcall (gnus-get-function method 'request-scan)
14588              (and group (gnus-group-real-name group)) (nth 1 method))))
14589
14590 (defsubst gnus-request-update-info (info method)
14591   "Request that METHOD update INFO."
14592   (when (gnus-check-backend-function 'request-update-info (car method))
14593     (funcall (gnus-get-function method 'request-update-info)
14594              (gnus-group-real-name (gnus-info-group info))
14595              info (nth 1 method))))
14596
14597 (defun gnus-request-expire-articles (articles group &optional force)
14598   (let ((method (gnus-find-method-for-group group)))
14599     (funcall (gnus-get-function method 'request-expire-articles)
14600              articles (gnus-group-real-name group) (nth 1 method)
14601              force)))
14602
14603 (defun gnus-request-move-article
14604   (article group server accept-function &optional last)
14605   (let ((method (gnus-find-method-for-group group)))
14606     (funcall (gnus-get-function method 'request-move-article)
14607              article (gnus-group-real-name group)
14608              (nth 1 method) accept-function last)))
14609
14610 (defun gnus-request-accept-article (group &optional last method)
14611   ;; Make sure there's a newline at the end of the article.
14612   (goto-char (point-max))
14613   (unless (bolp)
14614     (insert "\n"))
14615   (let ((func (if (symbolp group) group
14616                 (car (or method (gnus-find-method-for-group group))))))
14617     (funcall (intern (format "%s-request-accept-article" func))
14618              (if (stringp group) (gnus-group-real-name group) group)
14619              last)))
14620
14621 (defun gnus-request-replace-article (article group buffer)
14622   (let ((func (car (gnus-find-method-for-group group))))
14623     (funcall (intern (format "%s-request-replace-article" func))
14624              article (gnus-group-real-name group) buffer)))
14625
14626 (defun gnus-request-associate-buffer (group)
14627   (let ((method (gnus-find-method-for-group group)))
14628     (funcall (gnus-get-function method 'request-associate-buffer)
14629              (gnus-group-real-name group))))
14630
14631 (defun gnus-request-restore-buffer (article group)
14632   "Request a new buffer restored to the state of ARTICLE."
14633   (let ((method (gnus-find-method-for-group group)))
14634     (funcall (gnus-get-function method 'request-restore-buffer)
14635              article (gnus-group-real-name group) (nth 1 method))))
14636
14637 (defun gnus-request-create-group (group &optional method)
14638   (let ((method (or method (gnus-find-method-for-group group))))
14639     (funcall (gnus-get-function method 'request-create-group)
14640              (gnus-group-real-name group) (nth 1 method))))
14641
14642 (defun gnus-request-delete-group (group &optional force)
14643   (let ((method (gnus-find-method-for-group group)))
14644     (funcall (gnus-get-function method 'request-delete-group)
14645              (gnus-group-real-name group) force (nth 1 method))))
14646
14647 (defun gnus-request-rename-group (group new-name)
14648   (let ((method (gnus-find-method-for-group group)))
14649     (funcall (gnus-get-function method 'request-rename-group)
14650              (gnus-group-real-name group)
14651              (gnus-group-real-name new-name) (nth 1 method))))
14652
14653 (defun gnus-member-of-valid (symbol group)
14654   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14655   (memq symbol (assoc
14656                 (symbol-name (car (gnus-find-method-for-group group)))
14657                 gnus-valid-select-methods)))
14658
14659 (defun gnus-method-option-p (method option)
14660   "Return non-nil if select METHOD has OPTION as a parameter."
14661   (memq option (assoc (format "%s" (car method))
14662                       gnus-valid-select-methods)))
14663
14664 (defun gnus-server-extend-method (group method)
14665   ;; This function "extends" a virtual server.  If the server is
14666   ;; "hello", and the select method is ("hello" (my-var "something"))
14667   ;; in the group "alt.alt", this will result in a new virtual server
14668   ;; called "hello+alt.alt".
14669   (let ((entry
14670          (gnus-copy-sequence
14671           (if (equal (car method) "native") gnus-select-method
14672             (cdr (assoc (car method) gnus-server-alist))))))
14673     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14674     (nconc entry (cdr method))))
14675
14676 (defun gnus-find-method-for-group (group &optional info)
14677   "Find the select method that GROUP uses."
14678   (or gnus-override-method
14679       (and (not group)
14680            gnus-select-method)
14681       (let ((info (or info (gnus-get-info group)))
14682             method)
14683         (if (or (not info)
14684                 (not (setq method (gnus-info-method info))))
14685             (setq method gnus-select-method)
14686           (setq method
14687                 (cond ((stringp method)
14688                        (gnus-server-to-method method))
14689                       ((stringp (car method))
14690                        (gnus-server-extend-method group method))
14691                       (t
14692                        method))))
14693         (gnus-server-add-address method))))
14694
14695 (defun gnus-check-backend-function (func group)
14696   "Check whether GROUP supports function FUNC."
14697   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14698                   group)))
14699     (fboundp (intern (format "%s-%s" method func)))))
14700
14701 (defun gnus-methods-using (feature)
14702   "Find all methods that have FEATURE."
14703   (let ((valids gnus-valid-select-methods)
14704         outs)
14705     (while valids
14706       (if (memq feature (car valids))
14707           (setq outs (cons (car valids) outs)))
14708       (setq valids (cdr valids)))
14709     outs))
14710
14711 \f
14712 ;;;
14713 ;;; Active & Newsrc File Handling
14714 ;;;
14715
14716 (defun gnus-setup-news (&optional rawfile level)
14717   "Setup news information.
14718 If RAWFILE is non-nil, the .newsrc file will also be read.
14719 If LEVEL is non-nil, the news will be set up at level LEVEL."
14720   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14721     ;; Clear some variables to re-initialize news information.
14722     (if init (setq gnus-newsrc-alist nil
14723                    gnus-active-hashtb nil))
14724
14725     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14726     (if init (gnus-read-newsrc-file rawfile))
14727
14728     (unless (assoc "archive" gnus-server-alist)
14729       (push (cons "archive" gnus-message-archive-method)
14730             gnus-server-alist))
14731
14732     ;; If we don't read the complete active file, we fill in the
14733     ;; hashtb here.
14734     (if (or (null gnus-read-active-file)
14735             (eq gnus-read-active-file 'some))
14736         (gnus-update-active-hashtb-from-killed))
14737
14738     ;; Read the active file and create `gnus-active-hashtb'.
14739     ;; If `gnus-read-active-file' is nil, then we just create an empty
14740     ;; hash table.  The partial filling out of the hash table will be
14741     ;; done in `gnus-get-unread-articles'.
14742     (and gnus-read-active-file
14743          (not level)
14744          (gnus-read-active-file))
14745
14746     (or gnus-active-hashtb
14747         (setq gnus-active-hashtb (make-vector 4095 0)))
14748
14749     ;; Initialize the cache.
14750     (when gnus-use-cache
14751       (gnus-cache-open))
14752
14753     ;; Possibly eval the dribble file.
14754     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14755
14756     (gnus-update-format-specifications)
14757
14758     ;; Find new newsgroups and treat them.
14759     (if (and init gnus-check-new-newsgroups (not level)
14760              (gnus-check-server gnus-select-method))
14761         (gnus-find-new-newsgroups))
14762
14763     ;; Find the number of unread articles in each non-dead group.
14764     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14765       (gnus-get-unread-articles level))
14766
14767     (if (and init gnus-check-bogus-newsgroups
14768              gnus-read-active-file (not level)
14769              (gnus-server-opened gnus-select-method))
14770         (gnus-check-bogus-newsgroups))))
14771
14772 (defun gnus-find-new-newsgroups (&optional arg)
14773   "Search for new newsgroups and add them.
14774 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14775 The `-n' option line from .newsrc is respected.
14776 If ARG (the prefix), use the `ask-server' method to query
14777 the server for new groups."
14778   (interactive "P")
14779   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14780                        (null gnus-read-active-file)
14781                        (eq gnus-read-active-file 'some))
14782                    'ask-server gnus-check-new-newsgroups)))
14783     (unless (gnus-check-first-time-used)
14784       (if (or (consp check)
14785               (eq check 'ask-server))
14786           ;; Ask the server for new groups.
14787           (gnus-ask-server-for-new-groups)
14788         ;; Go through the active hashtb and look for new groups.
14789         (let ((groups 0)
14790               group new-newsgroups)
14791           (gnus-message 5 "Looking for new newsgroups...")
14792           (unless gnus-have-read-active-file
14793             (gnus-read-active-file))
14794           (setq gnus-newsrc-last-checked-date (current-time-string))
14795           (unless gnus-killed-hashtb
14796             (gnus-make-hashtable-from-killed))
14797           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14798           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14799           (mapatoms
14800            (lambda (sym)
14801              (if (or (null (setq group (symbol-name sym)))
14802                      (not (boundp sym))
14803                      (null (symbol-value sym))
14804                      (gnus-gethash group gnus-killed-hashtb)
14805                      (gnus-gethash group gnus-newsrc-hashtb))
14806                  ()
14807                (let ((do-sub (gnus-matches-options-n group)))
14808                  (cond
14809                   ((eq do-sub 'subscribe)
14810                    (setq groups (1+ groups))
14811                    (gnus-sethash group group gnus-killed-hashtb)
14812                    (funcall gnus-subscribe-options-newsgroup-method group))
14813                   ((eq do-sub 'ignore)
14814                    nil)
14815                   (t
14816                    (setq groups (1+ groups))
14817                    (gnus-sethash group group gnus-killed-hashtb)
14818                    (if gnus-subscribe-hierarchical-interactive
14819                        (setq new-newsgroups (cons group new-newsgroups))
14820                      (funcall gnus-subscribe-newsgroup-method group)))))))
14821            gnus-active-hashtb)
14822           (when new-newsgroups
14823             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14824           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14825           (if (> groups 0)
14826               (gnus-message 6 "%d new newsgroup%s arrived."
14827                             groups (if (> groups 1) "s have" " has"))
14828             (gnus-message 6 "No new newsgroups.")))))))
14829
14830 (defun gnus-matches-options-n (group)
14831   ;; Returns `subscribe' if the group is to be unconditionally
14832   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14833   ;; no match for the group.
14834
14835   ;; First we check the two user variables.
14836   (cond
14837    ((and gnus-options-subscribe
14838          (string-match gnus-options-subscribe group))
14839     'subscribe)
14840    ((and gnus-auto-subscribed-groups
14841          (string-match gnus-auto-subscribed-groups group))
14842     'subscribe)
14843    ((and gnus-options-not-subscribe
14844          (string-match gnus-options-not-subscribe group))
14845     'ignore)
14846    ;; Then we go through the list that was retrieved from the .newsrc
14847    ;; file.  This list has elements on the form
14848    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14849    ;; is in the reverse order of the options line) is returned.
14850    (t
14851     (let ((regs gnus-newsrc-options-n))
14852       (while (and regs
14853                   (not (string-match (car (car regs)) group)))
14854         (setq regs (cdr regs)))
14855       (and regs (cdr (car regs)))))))
14856
14857 (defun gnus-ask-server-for-new-groups ()
14858   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14859          (methods (cons gnus-select-method
14860                         (cons
14861                          "archive"
14862                          (append
14863                           (and (consp gnus-check-new-newsgroups)
14864                                gnus-check-new-newsgroups)
14865                           gnus-secondary-select-methods))))
14866          (groups 0)
14867          (new-date (current-time-string))
14868          group new-newsgroups got-new method hashtb
14869          gnus-override-subscribe-method)
14870     ;; Go through both primary and secondary select methods and
14871     ;; request new newsgroups.
14872     (while (setq method (gnus-server-get-method nil (pop methods)))
14873       (setq gnus-override-subscribe-method method)
14874       (when (and (gnus-check-server method)
14875                  (gnus-request-newgroups date method))
14876         (save-excursion
14877           (setq got-new t)
14878           (setq hashtb (gnus-make-hashtable 100))
14879           (set-buffer nntp-server-buffer)
14880           ;; Enter all the new groups into a hashtable.
14881           (gnus-active-to-gnus-format method hashtb 'ignore)))
14882       ;; Now all new groups from `method' are in `hashtb'.
14883       (mapatoms
14884        (lambda (group-sym)
14885          (if (or (null (setq group (symbol-name group-sym)))
14886                  (null (symbol-value group-sym))
14887                  (gnus-gethash group gnus-newsrc-hashtb)
14888                  (member group gnus-zombie-list)
14889                  (member group gnus-killed-list))
14890              ;; The group is already known.
14891              ()
14892            ;; Make this group active.
14893            (when (symbol-value group-sym)
14894              (gnus-set-active group (symbol-value group-sym)))
14895            ;; Check whether we want it or not.
14896            (let ((do-sub (gnus-matches-options-n group)))
14897              (cond
14898               ((eq do-sub 'subscribe)
14899                (incf groups)
14900                (gnus-sethash group group gnus-killed-hashtb)
14901                (funcall gnus-subscribe-options-newsgroup-method group))
14902               ((eq do-sub 'ignore)
14903                nil)
14904               (t
14905                (incf groups)
14906                (gnus-sethash group group gnus-killed-hashtb)
14907                (if gnus-subscribe-hierarchical-interactive
14908                    (push group new-newsgroups)
14909                  (funcall gnus-subscribe-newsgroup-method group)))))))
14910        hashtb)
14911       (when new-newsgroups
14912         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
14913     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14914     (when (> groups 0)
14915       (gnus-message 6 "%d new newsgroup%s arrived."
14916                     groups (if (> groups 1) "s have" " has")))
14917     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14918     got-new))
14919
14920 (defun gnus-check-first-time-used ()
14921   (if (or (> (length gnus-newsrc-alist) 1)
14922           (file-exists-p gnus-startup-file)
14923           (file-exists-p (concat gnus-startup-file ".el"))
14924           (file-exists-p (concat gnus-startup-file ".eld")))
14925       nil
14926     (gnus-message 6 "First time user; subscribing you to default groups")
14927     (unless gnus-have-read-active-file
14928       (gnus-read-active-file))
14929     (setq gnus-newsrc-last-checked-date (current-time-string))
14930     (let ((groups gnus-default-subscribed-newsgroups)
14931           group)
14932       (if (eq groups t)
14933           nil
14934         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
14935         (mapatoms
14936          (lambda (sym)
14937            (if (null (setq group (symbol-name sym)))
14938                ()
14939              (let ((do-sub (gnus-matches-options-n group)))
14940                (cond
14941                 ((eq do-sub 'subscribe)
14942                  (gnus-sethash group group gnus-killed-hashtb)
14943                  (funcall gnus-subscribe-options-newsgroup-method group))
14944                 ((eq do-sub 'ignore)
14945                  nil)
14946                 (t
14947                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
14948          gnus-active-hashtb)
14949         (while groups
14950           (if (gnus-active (car groups))
14951               (gnus-group-change-level
14952                (car groups) gnus-level-default-subscribed gnus-level-killed))
14953           (setq groups (cdr groups)))
14954         (gnus-group-make-help-group)
14955         (and gnus-novice-user
14956              (gnus-message 7 "`A k' to list killed groups"))))))
14957
14958 (defun gnus-subscribe-group (group previous &optional method)
14959   (gnus-group-change-level
14960    (if method
14961        (list t group gnus-level-default-subscribed nil nil method)
14962      group)
14963    gnus-level-default-subscribed gnus-level-killed previous t))
14964
14965 ;; `gnus-group-change-level' is the fundamental function for changing
14966 ;; subscription levels of newsgroups.  This might mean just changing
14967 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
14968 ;; again, which subscribes/unsubscribes a group, which is equally
14969 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
14970 ;; from 8-9 to 1-7 means that you remove the group from the list of
14971 ;; killed (or zombie) groups and add them to the (kinda) subscribed
14972 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
14973 ;; which is trivial.
14974 ;; ENTRY can either be a string (newsgroup name) or a list (if
14975 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
14976 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
14977 ;; entries.
14978 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
14979 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
14980 ;; after.
14981 (defun gnus-group-change-level (entry level &optional oldlevel
14982                                       previous fromkilled)
14983   (let (group info active num)
14984     ;; Glean what info we can from the arguments
14985     (if (consp entry)
14986         (if fromkilled (setq group (nth 1 entry))
14987           (setq group (car (nth 2 entry))))
14988       (setq group entry))
14989     (if (and (stringp entry)
14990              oldlevel
14991              (< oldlevel gnus-level-zombie))
14992         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
14993     (if (and (not oldlevel)
14994              (consp entry))
14995         (setq oldlevel (car (cdr (nth 2 entry)))))
14996     (if (stringp previous)
14997         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
14998
14999     (if (and (>= oldlevel gnus-level-zombie)
15000              (gnus-gethash group gnus-newsrc-hashtb))
15001         ;; We are trying to subscribe a group that is already
15002         ;; subscribed.
15003         ()                              ; Do nothing.
15004
15005       (or (gnus-ephemeral-group-p group)
15006           (gnus-dribble-enter
15007            (format "(gnus-group-change-level %S %S %S %S %S)"
15008                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15009
15010       ;; Then we remove the newgroup from any old structures, if needed.
15011       ;; If the group was killed, we remove it from the killed or zombie
15012       ;; list.  If not, and it is in fact going to be killed, we remove
15013       ;; it from the newsrc hash table and assoc.
15014       (cond
15015        ((>= oldlevel gnus-level-zombie)
15016         (if (= oldlevel gnus-level-zombie)
15017             (setq gnus-zombie-list (delete group gnus-zombie-list))
15018           (setq gnus-killed-list (delete group gnus-killed-list))))
15019        (t
15020         (if (and (>= level gnus-level-zombie)
15021                  entry)
15022             (progn
15023               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15024               (if (nth 3 entry)
15025                   (setcdr (gnus-gethash (car (nth 3 entry))
15026                                         gnus-newsrc-hashtb)
15027                           (cdr entry)))
15028               (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
15029
15030       ;; Finally we enter (if needed) the list where it is supposed to
15031       ;; go, and change the subscription level.  If it is to be killed,
15032       ;; we enter it into the killed or zombie list.
15033       (cond 
15034        ((>= level gnus-level-zombie)
15035         ;; Remove from the hash table.
15036         (gnus-sethash group nil gnus-newsrc-hashtb)
15037         ;; We do not enter foreign groups into the list of dead
15038         ;; groups.
15039         (unless (gnus-group-foreign-p group)
15040           (if (= level gnus-level-zombie)
15041               (setq gnus-zombie-list (cons group gnus-zombie-list))
15042             (setq gnus-killed-list (cons group gnus-killed-list)))))
15043        (t
15044         ;; If the list is to be entered into the newsrc assoc, and
15045         ;; it was killed, we have to create an entry in the newsrc
15046         ;; hashtb format and fix the pointers in the newsrc assoc.
15047         (if (< oldlevel gnus-level-zombie)
15048             ;; It was alive, and it is going to stay alive, so we
15049             ;; just change the level and don't change any pointers or
15050             ;; hash table entries.
15051             (setcar (cdr (car (cdr (cdr entry)))) level)
15052           (if (listp entry)
15053               (setq info (cdr entry)
15054                     num (car entry))
15055             (setq active (gnus-active group))
15056             (setq num
15057                   (if active (- (1+ (cdr active)) (car active)) t))
15058             ;; Check whether the group is foreign.  If so, the
15059             ;; foreign select method has to be entered into the
15060             ;; info.
15061             (let ((method (or gnus-override-subscribe-method
15062                               (gnus-group-method group))))
15063               (if (eq method gnus-select-method)
15064                   (setq info (list group level nil))
15065                 (setq info (list group level nil nil method)))))
15066           (unless previous
15067             (setq previous
15068                   (let ((p gnus-newsrc-alist))
15069                     (while (cdr (cdr p))
15070                       (setq p (cdr p)))
15071                     p)))
15072           (setq entry (cons info (cdr (cdr previous))))
15073           (if (cdr previous)
15074               (progn
15075                 (setcdr (cdr previous) entry)
15076                 (gnus-sethash group (cons num (cdr previous))
15077                               gnus-newsrc-hashtb))
15078             (setcdr previous entry)
15079             (gnus-sethash group (cons num previous)
15080                           gnus-newsrc-hashtb))
15081           (when (cdr entry)
15082             (setcdr (gnus-gethash (car (car (cdr entry))) gnus-newsrc-hashtb)
15083                     entry)))))
15084       (when gnus-group-change-level-function
15085         (funcall gnus-group-change-level-function group level oldlevel)))))
15086
15087 (defun gnus-kill-newsgroup (newsgroup)
15088   "Obsolete function.  Kills a newsgroup."
15089   (gnus-group-change-level
15090    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15091
15092 (defun gnus-check-bogus-newsgroups (&optional confirm)
15093   "Remove bogus newsgroups.
15094 If CONFIRM is non-nil, the user has to confirm the deletion of every
15095 newsgroup."
15096   (let ((newsrc (cdr gnus-newsrc-alist))
15097         bogus group entry info)
15098     (gnus-message 5 "Checking bogus newsgroups...")
15099     (unless gnus-have-read-active-file
15100       (gnus-read-active-file))
15101     (when (member gnus-select-method gnus-have-read-active-file)
15102       ;; Find all bogus newsgroup that are subscribed.
15103       (while newsrc
15104         (setq info (pop newsrc)
15105               group (gnus-info-group info))
15106         (unless (or (gnus-active group) ; Active
15107                     (gnus-info-method info) ; Foreign
15108                     (and confirm
15109                          (not (gnus-y-or-n-p
15110                                (format "Remove bogus newsgroup: %s " group)))))
15111           ;; Found a bogus newsgroup.
15112           (push group bogus)))
15113       ;; Remove all bogus subscribed groups by first killing them, and
15114       ;; then removing them from the list of killed groups.
15115       (while bogus
15116         (when (setq entry (gnus-gethash (setq group (pop bogus))
15117                                         gnus-newsrc-hashtb))
15118           (gnus-group-change-level entry gnus-level-killed)
15119           (setq gnus-killed-list (delete group gnus-killed-list))))
15120       ;; Then we remove all bogus groups from the list of killed and
15121       ;; zombie groups.  They are are removed without confirmation.
15122       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15123             killed)
15124         (while dead-lists
15125           (setq killed (symbol-value (car dead-lists)))
15126           (while killed
15127             (unless (gnus-active (setq group (pop killed)))
15128               ;; The group is bogus.
15129               ;; !!!Slow as hell.
15130               (set (car dead-lists)
15131                    (delete group (symbol-value (car dead-lists))))))
15132           (setq dead-lists (cdr dead-lists))))
15133       (gnus-message 5 "Checking bogus newsgroups...done"))))
15134
15135 (defun gnus-check-duplicate-killed-groups ()
15136   "Remove duplicates from the list of killed groups."
15137   (interactive)
15138   (let ((killed gnus-killed-list))
15139     (while killed
15140       (gnus-message 9 "%d" (length killed))
15141       (setcdr killed (delete (car killed) (cdr killed)))
15142       (setq killed (cdr killed)))))
15143
15144 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15145 ;; and compute how many unread articles there are in each group.
15146 (defun gnus-get-unread-articles (&optional level)
15147   (let* ((newsrc (cdr gnus-newsrc-alist))
15148          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15149          (foreign-level
15150           (min
15151            (cond ((and gnus-activate-foreign-newsgroups
15152                        (not (numberp gnus-activate-foreign-newsgroups)))
15153                   (1+ gnus-level-subscribed))
15154                  ((numberp gnus-activate-foreign-newsgroups)
15155                   gnus-activate-foreign-newsgroups)
15156                  (t 0))
15157            level))
15158          info group active virtuals method)
15159     (gnus-message 5 "Checking new news...")
15160
15161     (while newsrc
15162       (setq info (car newsrc)
15163             group (gnus-info-group info)
15164             active (gnus-active group))
15165
15166       ;; Check newsgroups.  If the user doesn't want to check them, or
15167       ;; they can't be checked (for instance, if the news server can't
15168       ;; be reached) we just set the number of unread articles in this
15169       ;; newsgroup to t.  This means that Gnus thinks that there are
15170       ;; unread articles, but it has no idea how many.
15171       (if (and (setq method (gnus-info-method info))
15172                (not (gnus-server-equal
15173                      gnus-select-method
15174                      (gnus-server-get-method nil method)))
15175                (not (gnus-secondary-method-p method)))
15176           ;; These groups are foreign.  Check the level.
15177           (when (<= (gnus-info-level info) foreign-level)
15178             (setq active (gnus-activate-group group 'scan))
15179             (gnus-close-group group))
15180
15181         ;; These groups are native or secondary.
15182         (when (and (<= (gnus-info-level info) level)
15183                    (not gnus-read-active-file))
15184           (setq active (gnus-activate-group group 'scan))
15185           (gnus-close-group group)))
15186
15187       (if active
15188           (gnus-get-unread-articles-in-group info active t)
15189         ;; The group couldn't be reached, so we nix out the number of
15190         ;; unread articles and stuff.
15191         (gnus-set-active group nil)
15192         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
15193
15194       (setq newsrc (cdr newsrc)))
15195
15196     (gnus-message 5 "Checking new news...done")))
15197
15198 ;; Create a hash table out of the newsrc alist.  The `car's of the
15199 ;; alist elements are used as keys.
15200 (defun gnus-make-hashtable-from-newsrc-alist ()
15201   (let ((alist gnus-newsrc-alist)
15202         (ohashtb gnus-newsrc-hashtb)
15203         prev)
15204     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15205     (setq alist
15206           (setq prev (setq gnus-newsrc-alist
15207                            (if (equal (car (car gnus-newsrc-alist))
15208                                       "dummy.group")
15209                                gnus-newsrc-alist
15210                              (cons (list "dummy.group" 0 nil) alist)))))
15211     (while alist
15212       (gnus-sethash
15213        (car (car alist))
15214        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb)))
15215              prev)
15216        gnus-newsrc-hashtb)
15217       (setq prev alist
15218             alist (cdr alist)))))
15219
15220 (defun gnus-make-hashtable-from-killed ()
15221   "Create a hash table from the killed and zombie lists."
15222   (let ((lists '(gnus-killed-list gnus-zombie-list))
15223         list)
15224     (setq gnus-killed-hashtb
15225           (gnus-make-hashtable
15226            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15227     (while lists
15228       (setq list (symbol-value (car lists)))
15229       (setq lists (cdr lists))
15230       (while list
15231         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15232         (setq list (cdr list))))))
15233
15234 (defun gnus-get-unread-articles-in-group (info active &optional update)
15235   (when active
15236     ;; Allow the backend to update the info in the group.
15237     (when update 
15238       (gnus-request-update-info
15239        info (gnus-find-method-for-group (gnus-info-group info))))
15240     (let* ((range (gnus-info-read info))
15241            (num 0)
15242            (marked (gnus-info-marks info)))
15243       ;; If a cache is present, we may have to alter the active info.
15244       (and gnus-use-cache
15245            (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15246       ;; Modify the list of read articles according to what articles
15247       ;; are available; then tally the unread articles and add the
15248       ;; number to the group hash table entry.
15249       (cond
15250        ((zerop (cdr active))
15251         (setq num 0))
15252        ((not range)
15253         (setq num (- (1+ (cdr active)) (car active))))
15254        ((not (listp (cdr range)))
15255         ;; Fix a single (num . num) range according to the
15256         ;; active hash table.
15257         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15258         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15259         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15260         ;; Compute number of unread articles.
15261         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15262        (t
15263         ;; The read list is a list of ranges.  Fix them according to
15264         ;; the active hash table.
15265         ;; First peel off any elements that are below the lower
15266         ;; active limit.
15267         (while (and (cdr range)
15268                     (>= (car active)
15269                         (or (and (atom (car (cdr range))) (car (cdr range)))
15270                             (car (car (cdr range))))))
15271           (if (numberp (car range))
15272               (setcar range
15273                       (cons (car range)
15274                             (or (and (numberp (car (cdr range)))
15275                                      (car (cdr range)))
15276                                 (cdr (car (cdr range))))))
15277             (setcdr (car range)
15278                     (or (and (numberp (nth 1 range)) (nth 1 range))
15279                         (cdr (car (cdr range))))))
15280           (setcdr range (cdr (cdr range))))
15281         ;; Adjust the first element to be the same as the lower limit.
15282         (if (and (not (atom (car range)))
15283                  (< (cdr (car range)) (car active)))
15284             (setcdr (car range) (1- (car active))))
15285         ;; Then we want to peel off any elements that are higher
15286         ;; than the upper active limit.
15287         (let ((srange range))
15288           ;; Go past all legal elements.
15289           (while (and (cdr srange)
15290                       (<= (or (and (atom (car (cdr srange)))
15291                                    (car (cdr srange)))
15292                               (car (car (cdr srange)))) (cdr active)))
15293             (setq srange (cdr srange)))
15294           (if (cdr srange)
15295               ;; Nuke all remaining illegal elements.
15296               (setcdr srange nil))
15297
15298           ;; Adjust the final element.
15299           (if (and (not (atom (car srange)))
15300                    (> (cdr (car srange)) (cdr active)))
15301               (setcdr (car srange) (cdr active))))
15302         ;; Compute the number of unread articles.
15303         (while range
15304           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15305                                       (cdr (car range))))
15306                               (or (and (atom (car range)) (car range))
15307                                   (car (car range))))))
15308           (setq range (cdr range)))
15309         (setq num (max 0 (- (cdr active) num)))))
15310       ;; Set the number of unread articles.
15311       (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
15312       num)))
15313
15314 (defun gnus-activate-group (group &optional scan)
15315   ;; Check whether a group has been activated or not.
15316   ;; If SCAN, request a scan of that group as well.
15317   (let ((method (gnus-find-method-for-group group))
15318         active)
15319     (and (gnus-check-server method)
15320          ;; We escape all bugs and quit here to make it possible to
15321          ;; continue if a group is so out-there that it reports bugs
15322          ;; and stuff.
15323          (progn
15324            (and scan
15325                 (gnus-check-backend-function 'request-scan (car method))
15326                 (gnus-request-scan group method))
15327            t)
15328          (condition-case ()
15329              (gnus-request-group group)
15330         ;   (error nil)
15331            (quit nil))
15332          (save-excursion
15333            (set-buffer nntp-server-buffer)
15334            (goto-char (point-min))
15335            ;; Parse the result we got from `gnus-request-group'.
15336            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15337                 (progn
15338                   (goto-char (match-beginning 1))
15339                   (gnus-set-active
15340                    group (setq active (cons (read (current-buffer))
15341                                             (read (current-buffer)))))
15342                   ;; Return the new active info.
15343                   active))))))
15344
15345 (defun gnus-update-read-articles (group unread)
15346   "Update the list of read and ticked articles in GROUP using the
15347 UNREAD and TICKED lists.
15348 Note: UNSELECTED has to be sorted over `<'.
15349 Returns whether the updating was successful."
15350   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15351          (entry (gnus-gethash group gnus-newsrc-hashtb))
15352          (info (nth 2 entry))
15353          (marked (gnus-info-marks info))
15354          (prev 1)
15355          (unread (sort (copy-sequence unread) '<))
15356          read)
15357     (if (or (not info) (not active))
15358         ;; There is no info on this group if it was, in fact,
15359         ;; killed.  Gnus stores no information on killed groups, so
15360         ;; there's nothing to be done.
15361         ;; One could store the information somewhere temporarily,
15362         ;; perhaps...  Hmmm...
15363         ()
15364       ;; Remove any negative articles numbers.
15365       (while (and unread (< (car unread) 0))
15366         (setq unread (cdr unread)))
15367       ;; Remove any expired article numbers
15368       (while (and unread (< (car unread) (car active)))
15369         (setq unread (cdr unread)))
15370       ;; Compute the ranges of read articles by looking at the list of
15371       ;; unread articles.
15372       (while unread
15373         (if (/= (car unread) prev)
15374             (setq read (cons (if (= prev (1- (car unread))) prev
15375                                (cons prev (1- (car unread)))) read)))
15376         (setq prev (1+ (car unread)))
15377         (setq unread (cdr unread)))
15378       (when (<= prev (cdr active))
15379         (setq read (cons (cons prev (cdr active)) read)))
15380       ;; Enter this list into the group info.
15381       (gnus-info-set-read
15382        info (if (> (length read) 1) (nreverse read) read))
15383       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15384       (gnus-get-unread-articles-in-group info (gnus-active group))
15385       t)))
15386
15387 (defun gnus-make-articles-unread (group articles)
15388   "Mark ARTICLES in GROUP as unread."
15389   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15390                           (gnus-gethash (gnus-group-real-name group)
15391                                         gnus-newsrc-hashtb))))
15392          (ranges (gnus-info-read info))
15393          news article)
15394     (while articles
15395       (when (gnus-member-of-range
15396              (setq article (pop articles)) ranges)
15397         (setq news (cons article news))))
15398     (when news
15399       (gnus-info-set-read
15400        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15401       (gnus-group-update-group group t))))
15402
15403 ;; Enter all dead groups into the hashtb.
15404 (defun gnus-update-active-hashtb-from-killed ()
15405   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15406         (lists (list gnus-killed-list gnus-zombie-list))
15407         killed)
15408     (while lists
15409       (setq killed (car lists))
15410       (while killed
15411         (gnus-sethash (car killed) nil hashtb)
15412         (setq killed (cdr killed)))
15413       (setq lists (cdr lists)))))
15414
15415 ;; Get the active file(s) from the backend(s).
15416 (defun gnus-read-active-file ()
15417   (gnus-group-set-mode-line)
15418   (let ((methods 
15419          (append
15420           (if (gnus-check-server gnus-select-method)
15421               ;; The native server is available.
15422               (cons gnus-select-method gnus-secondary-select-methods)
15423             ;; The native server is down, so we just do the
15424             ;; secondary ones.
15425             gnus-secondary-select-methods)
15426           ;; Also read from the archive server.
15427           (list "archive")))
15428         list-type)
15429     (setq gnus-have-read-active-file nil)
15430     (save-excursion
15431       (set-buffer nntp-server-buffer)
15432       (while methods
15433         (let* ((method (if (stringp (car methods))
15434                            (gnus-server-get-method nil (car methods))
15435                          (car methods)))
15436                (where (nth 1 method))
15437                (mesg (format "Reading active file%s via %s..."
15438                              (if (and where (not (zerop (length where))))
15439                                  (concat " from " where) "")
15440                              (car method))))
15441           (gnus-message 5 mesg)
15442           (if (not (gnus-check-server method))
15443               ()
15444             ;; Request that the backend scan its incoming messages.
15445             (and (gnus-check-backend-function 'request-scan (car method))
15446                  (gnus-request-scan nil method))
15447             (cond
15448              ((and (eq gnus-read-active-file 'some)
15449                    (gnus-check-backend-function 'retrieve-groups (car method)))
15450               (let ((newsrc (cdr gnus-newsrc-alist))
15451                     (gmethod (gnus-server-get-method nil method))
15452                     groups)
15453                 (while newsrc
15454                   (and (gnus-server-equal
15455                         (gnus-find-method-for-group
15456                          (car (car newsrc)) (car newsrc))
15457                         gmethod)
15458                        (setq groups (cons (gnus-group-real-name
15459                                            (car (car newsrc))) groups)))
15460                   (setq newsrc (cdr newsrc)))
15461                 (gnus-check-server method)
15462                 (setq list-type (gnus-retrieve-groups groups method))
15463                 (cond
15464                  ((not list-type)
15465                   (gnus-message
15466                    1 "Cannot read partial active file from %s server."
15467                    (car method))
15468                   (ding)
15469                   (sit-for 2))
15470                  ((eq list-type 'active)
15471                   (gnus-active-to-gnus-format method gnus-active-hashtb))
15472                  (t
15473                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
15474              (t
15475               (if (not (gnus-request-list method))
15476                   (progn
15477                     (unless (equal method gnus-message-archive-method)
15478                       (gnus-message 1 "Cannot read active file from %s server."
15479                                     (car method))
15480                       (ding)))
15481                 (gnus-active-to-gnus-format method)
15482                 ;; We mark this active file as read.
15483                 (push method gnus-have-read-active-file)
15484                 (gnus-message 5 "%sdone" mesg))))))
15485         (setq methods (cdr methods))))))
15486
15487 ;; Read an active file and place the results in `gnus-active-hashtb'.
15488 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15489   (unless method
15490     (setq method gnus-select-method))
15491   (let ((cur (current-buffer))
15492         (hashtb (or hashtb
15493                     (if (and gnus-active-hashtb
15494                              (not (equal method gnus-select-method)))
15495                         gnus-active-hashtb
15496                       (setq gnus-active-hashtb
15497                             (if (equal method gnus-select-method)
15498                                 (gnus-make-hashtable
15499                                  (count-lines (point-min) (point-max)))
15500                               (gnus-make-hashtable 4096))))))
15501         (flag-hashtb (gnus-make-hashtable 60)))
15502     ;; Delete unnecessary lines.
15503     (goto-char (point-min))
15504     (while (search-forward "\nto." nil t)
15505       (delete-region (1+ (match-beginning 0))
15506                      (progn (forward-line 1) (point))))
15507     (or (string= gnus-ignored-newsgroups "")
15508         (progn
15509           (goto-char (point-min))
15510           (delete-matching-lines gnus-ignored-newsgroups)))
15511     ;; Make the group names readable as a lisp expression even if they
15512     ;; contain special characters.
15513     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15514     (goto-char (point-max))
15515     (while (re-search-backward "[][';?()#]" nil t)
15516       (insert ?\\))
15517     ;; If these are groups from a foreign select method, we insert the
15518     ;; group prefix in front of the group names.
15519     (and method (not (gnus-server-equal
15520                       (gnus-server-get-method nil method)
15521                       (gnus-server-get-method nil gnus-select-method)))
15522          (let ((prefix (gnus-group-prefixed-name "" method)))
15523            (goto-char (point-min))
15524            (while (and (not (eobp))
15525                        (progn (insert prefix)
15526                               (zerop (forward-line 1)))))))
15527     ;; Store the active file in a hash table.
15528     (goto-char (point-min))
15529     (if (string-match "%[oO]" gnus-group-line-format)
15530         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15531         ;; If we want information on moderated groups, we use this
15532         ;; loop...
15533         (let* ((mod-hashtb (make-vector 7 0))
15534                (m (intern "m" mod-hashtb))
15535                group max min)
15536           (while (not (eobp))
15537             (condition-case nil
15538                 (progn
15539                   (narrow-to-region (point) (gnus-point-at-eol))
15540                   (setq group (let ((obarray hashtb)) (read cur)))
15541                   (if (and (numberp (setq max (read cur)))
15542                            (numberp (setq min (read cur)))
15543                            (progn
15544                              (skip-chars-forward " \t")
15545                              (not
15546                               (or (= (following-char) ?=)
15547                                   (= (following-char) ?x)
15548                                   (= (following-char) ?j)))))
15549                       (set group (cons min max))
15550                     (set group nil))
15551                   ;; Enter moderated groups into a list.
15552                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15553                       (setq gnus-moderated-list
15554                             (cons (symbol-name group) gnus-moderated-list))))
15555               (error
15556                (and group
15557                     (symbolp group)
15558                     (set group nil))))
15559             (widen)
15560             (forward-line 1)))
15561       ;; And if we do not care about moderation, we use this loop,
15562       ;; which is faster.
15563       (let (group max min)
15564         (while (not (eobp))
15565           (condition-case ()
15566               (progn
15567                 (narrow-to-region (point) (gnus-point-at-eol))
15568                 ;; group gets set to a symbol interned in the hash table
15569                 ;; (what a hack!!) - jwz
15570                 (setq group (let ((obarray hashtb)) (read cur)))
15571                 (if (and (numberp (setq max (read cur)))
15572                          (numberp (setq min (read cur)))
15573                          (progn
15574                            (skip-chars-forward " \t")
15575                            (not
15576                             (or (= (following-char) ?=)
15577                                 (= (following-char) ?x)
15578                                 (= (following-char) ?j)))))
15579                     (set group (cons min max))
15580                   (set group nil)))
15581             (error
15582              (progn
15583                (and group
15584                     (symbolp group)
15585                     (set group nil))
15586                (or ignore-errors
15587                    (gnus-message 3 "Warning - illegal active: %s"
15588                                  (buffer-substring
15589                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15590           (widen)
15591           (forward-line 1))))))
15592
15593 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15594   ;; Parse a "groups" active file.
15595   (let ((cur (current-buffer))
15596         (hashtb (or hashtb
15597                     (if (and method gnus-active-hashtb)
15598                         gnus-active-hashtb
15599                       (setq gnus-active-hashtb
15600                             (gnus-make-hashtable
15601                              (count-lines (point-min) (point-max)))))))
15602         (prefix (and method
15603                      (not (gnus-server-equal
15604                            (gnus-server-get-method nil method)
15605                            (gnus-server-get-method nil gnus-select-method)))
15606                      (gnus-group-prefixed-name "" method))))
15607
15608     (goto-char (point-min))
15609     ;; We split this into to separate loops, one with the prefix
15610     ;; and one without to speed the reading up somewhat.
15611     (if prefix
15612         (let (min max opoint group)
15613           (while (not (eobp))
15614             (condition-case ()
15615                 (progn
15616                   (read cur) (read cur)
15617                   (setq min (read cur)
15618                         max (read cur)
15619                         opoint (point))
15620                   (skip-chars-forward " \t")
15621                   (insert prefix)
15622                   (goto-char opoint)
15623                   (set (let ((obarray hashtb)) (read cur))
15624                        (cons min max)))
15625               (error (and group (symbolp group) (set group nil))))
15626             (forward-line 1)))
15627       (let (min max group)
15628         (while (not (eobp))
15629           (condition-case ()
15630               (if (= (following-char) ?2)
15631                   (progn
15632                     (read cur) (read cur)
15633                     (setq min (read cur)
15634                           max (read cur))
15635                     (set (setq group (let ((obarray hashtb)) (read cur)))
15636                          (cons min max))))
15637             (error (and group (symbolp group) (set group nil))))
15638           (forward-line 1))))))
15639
15640 (defun gnus-read-newsrc-file (&optional force)
15641   "Read startup file.
15642 If FORCE is non-nil, the .newsrc file is read."
15643   ;; Reset variables that might be defined in the .newsrc.eld file.
15644   (let ((variables gnus-variable-list))
15645     (while variables
15646       (set (car variables) nil)
15647       (setq variables (cdr variables))))
15648   (let* ((newsrc-file gnus-current-startup-file)
15649          (quick-file (concat newsrc-file ".el")))
15650     (save-excursion
15651       ;; We always load the .newsrc.eld file.  If always contains
15652       ;; much information that can not be gotten from the .newsrc
15653       ;; file (ticked articles, killed groups, foreign methods, etc.)
15654       (gnus-read-newsrc-el-file quick-file)
15655
15656       (if (or force
15657               (and (file-newer-than-file-p newsrc-file quick-file)
15658                    (file-newer-than-file-p newsrc-file
15659                                            (concat quick-file "d")))
15660               (not gnus-newsrc-alist))
15661           ;; We read the .newsrc file.  Note that if there if a
15662           ;; .newsrc.eld file exists, it has already been read, and
15663           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15664           ;; the .newsrc file, Gnus will only use the information it
15665           ;; can find there for changing the data already read -
15666           ;; ie. reading the .newsrc file will not trash the data
15667           ;; already read (except for read articles).
15668           (save-excursion
15669             (gnus-message 5 "Reading %s..." newsrc-file)
15670             (set-buffer (find-file-noselect newsrc-file))
15671             (buffer-disable-undo (current-buffer))
15672             (gnus-newsrc-to-gnus-format)
15673             (kill-buffer (current-buffer))
15674             (gnus-message 5 "Reading %s...done" newsrc-file)))
15675
15676       ;; Read any slave files.
15677       (or gnus-slave
15678           (gnus-master-read-slave-newsrc)))))
15679
15680 (defun gnus-read-newsrc-el-file (file)
15681   (let ((ding-file (concat file "d")))
15682     ;; We always, always read the .eld file.
15683     (gnus-message 5 "Reading %s..." ding-file)
15684     (let (gnus-newsrc-assoc)
15685       (condition-case nil
15686           (load ding-file t t t)
15687         (error
15688          (gnus-message 1 "Error in %s" ding-file)
15689          (ding)))
15690       (when gnus-newsrc-assoc
15691         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15692     (gnus-make-hashtable-from-newsrc-alist)
15693     (when (file-newer-than-file-p file ding-file)
15694       ;; Old format quick file
15695       (gnus-message 5 "Reading %s..." file)
15696       ;; The .el file is newer than the .eld file, so we read that one
15697       ;; as well.
15698       (gnus-read-old-newsrc-el-file file))))
15699
15700 ;; Parse the old-style quick startup file
15701 (defun gnus-read-old-newsrc-el-file (file)
15702   (let (newsrc killed marked group m)
15703     (prog1
15704         (let ((gnus-killed-assoc nil)
15705               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15706           (prog1
15707               (condition-case nil
15708                   (load file t t t)
15709                 (error nil))
15710             (setq newsrc gnus-newsrc-assoc
15711                   killed gnus-killed-assoc
15712                   marked gnus-marked-assoc)))
15713       (setq gnus-newsrc-alist nil)
15714       (while newsrc
15715         (setq group (car newsrc))
15716         (let ((info (gnus-get-info (car group))))
15717           (if info
15718               (progn
15719                 (gnus-info-set-read info (cdr (cdr group)))
15720                 (gnus-info-set-level
15721                  info (if (nth 1 group) gnus-level-default-subscribed
15722                         gnus-level-default-unsubscribed))
15723                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15724             (setq gnus-newsrc-alist
15725                   (cons
15726                    (setq info
15727                          (list (car group)
15728                                (if (nth 1 group) gnus-level-default-subscribed
15729                                  gnus-level-default-unsubscribed)
15730                                (cdr (cdr group))))
15731                    gnus-newsrc-alist)))
15732           (if (setq m (assoc (car group) marked))
15733               (gnus-info-set-marks
15734                info (cons (list (cons 'tick (gnus-compress-sequence
15735                                              (sort (cdr m) '<) t)))
15736                           nil))))
15737         (setq newsrc (cdr newsrc)))
15738       (setq newsrc killed)
15739       (while newsrc
15740         (setcar newsrc (car (car newsrc)))
15741         (setq newsrc (cdr newsrc)))
15742       (setq gnus-killed-list killed))
15743     ;; The .el file version of this variable does not begin with
15744     ;; "options", while the .eld version does, so we just add it if it
15745     ;; isn't there.
15746     (and
15747      gnus-newsrc-options
15748      (progn
15749        (and (not (string-match "^ *options" gnus-newsrc-options))
15750             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15751        (and (not (string-match "\n$" gnus-newsrc-options))
15752             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15753        ;; Finally, if we read some options lines, we parse them.
15754        (or (string= gnus-newsrc-options "")
15755            (gnus-newsrc-parse-options gnus-newsrc-options))))
15756
15757     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15758     (gnus-make-hashtable-from-newsrc-alist)))
15759
15760 (defun gnus-make-newsrc-file (file)
15761   "Make server dependent file name by catenating FILE and server host name."
15762   (let* ((file (expand-file-name file nil))
15763          (real-file (concat file "-" (nth 1 gnus-select-method))))
15764     (if (or (file-exists-p real-file)
15765             (file-exists-p (concat real-file ".el"))
15766             (file-exists-p (concat real-file ".eld")))
15767         real-file file)))
15768
15769 (defun gnus-newsrc-to-gnus-format ()
15770   (setq gnus-newsrc-options "")
15771   (setq gnus-newsrc-options-n nil)
15772
15773   (or gnus-active-hashtb
15774       (setq gnus-active-hashtb (make-vector 4095 0)))
15775   (let ((buf (current-buffer))
15776         (already-read (> (length gnus-newsrc-alist) 1))
15777         group subscribed options-symbol newsrc Options-symbol
15778         symbol reads num1)
15779     (goto-char (point-min))
15780     ;; We intern the symbol `options' in the active hashtb so that we
15781     ;; can `eq' against it later.
15782     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15783     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15784
15785     (while (not (eobp))
15786       ;; We first read the first word on the line by narrowing and
15787       ;; then reading into `gnus-active-hashtb'.  Most groups will
15788       ;; already exist in that hashtb, so this will save some string
15789       ;; space.
15790       (narrow-to-region
15791        (point)
15792        (progn (skip-chars-forward "^ \t!:\n") (point)))
15793       (goto-char (point-min))
15794       (setq symbol
15795             (and (/= (point-min) (point-max))
15796                  (let ((obarray gnus-active-hashtb)) (read buf))))
15797       (widen)
15798       ;; Now, the symbol we have read is either `options' or a group
15799       ;; name.  If it is an options line, we just add it to a string.
15800       (cond
15801        ((or (eq symbol options-symbol)
15802             (eq symbol Options-symbol))
15803         (setq gnus-newsrc-options
15804               ;; This concating is quite inefficient, but since our
15805               ;; thorough studies show that approx 99.37% of all
15806               ;; .newsrc files only contain a single options line, we
15807               ;; don't give a damn, frankly, my dear.
15808               (concat gnus-newsrc-options
15809                       (buffer-substring
15810                        (gnus-point-at-bol)
15811                        ;; Options may continue on the next line.
15812                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15813                                 (progn (beginning-of-line) (point)))
15814                            (point)))))
15815         (forward-line -1))
15816        (symbol
15817         ;; Group names can be just numbers.  
15818         (when (numberp symbol) 
15819           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15820         (or (boundp symbol) (set symbol nil))
15821         ;; It was a group name.
15822         (setq subscribed (= (following-char) ?:)
15823               group (symbol-name symbol)
15824               reads nil)
15825         (if (eolp)
15826             ;; If the line ends here, this is clearly a buggy line, so
15827             ;; we put point a the beginning of line and let the cond
15828             ;; below do the error handling.
15829             (beginning-of-line)
15830           ;; We skip to the beginning of the ranges.
15831           (skip-chars-forward "!: \t"))
15832         ;; We are now at the beginning of the list of read articles.
15833         ;; We read them range by range.
15834         (while
15835             (cond
15836              ((looking-at "[0-9]+")
15837               ;; We narrow and read a number instead of buffer-substring/
15838               ;; string-to-int because it's faster.  narrow/widen is
15839               ;; faster than save-restriction/narrow, and save-restriction
15840               ;; produces a garbage object.
15841               (setq num1 (progn
15842                            (narrow-to-region (match-beginning 0) (match-end 0))
15843                            (read buf)))
15844               (widen)
15845               ;; If the next character is a dash, then this is a range.
15846               (if (= (following-char) ?-)
15847                   (progn
15848                     ;; We read the upper bound of the range.
15849                     (forward-char 1)
15850                     (if (not (looking-at "[0-9]+"))
15851                         ;; This is a buggy line, by we pretend that
15852                         ;; it's kinda OK.  Perhaps the user should be
15853                         ;; dinged?
15854                         (setq reads (cons num1 reads))
15855                       (setq reads
15856                             (cons
15857                              (cons num1
15858                                    (progn
15859                                      (narrow-to-region (match-beginning 0)
15860                                                        (match-end 0))
15861                                      (read buf)))
15862                              reads))
15863                       (widen)))
15864                 ;; It was just a simple number, so we add it to the
15865                 ;; list of ranges.
15866                 (setq reads (cons num1 reads)))
15867               ;; If the next char in ?\n, then we have reached the end
15868               ;; of the line and return nil.
15869               (/= (following-char) ?\n))
15870              ((= (following-char) ?\n)
15871               ;; End of line, so we end.
15872               nil)
15873              (t
15874               ;; Not numbers and not eol, so this might be a buggy
15875               ;; line...
15876               (or (eobp)
15877                   ;; If it was eob instead of ?\n, we allow it.
15878                   (progn
15879                     ;; The line was buggy.
15880                     (setq group nil)
15881                     (gnus-message 3 "Mangled line: %s"
15882                                   (buffer-substring (gnus-point-at-bol)
15883                                                     (gnus-point-at-eol)))
15884                     (ding)
15885                     (sit-for 1)))
15886               nil))
15887           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15888           ;; we allow them, because it's a common mistake to put a
15889           ;; space after the comma.
15890           (skip-chars-forward ", "))
15891
15892         ;; We have already read .newsrc.eld, so we gently update the
15893         ;; data in the hash table with the information we have just
15894         ;; read.
15895         (when group
15896           (let ((info (gnus-get-info group))
15897                 level)
15898             (if info
15899                 ;; There is an entry for this file in the alist.
15900                 (progn
15901                   (gnus-info-set-read info (nreverse reads))
15902                   ;; We update the level very gently.  In fact, we
15903                   ;; only change it if there's been a status change
15904                   ;; from subscribed to unsubscribed, or vice versa.
15905                   (setq level (gnus-info-level info))
15906                   (cond ((and (<= level gnus-level-subscribed)
15907                               (not subscribed))
15908                          (setq level (if reads
15909                                          gnus-level-default-unsubscribed
15910                                        (1+ gnus-level-default-unsubscribed))))
15911                         ((and (> level gnus-level-subscribed) subscribed)
15912                          (setq level gnus-level-default-subscribed)))
15913                   (gnus-info-set-level info level))
15914               ;; This is a new group.
15915               (setq info (list group
15916                                (if subscribed
15917                                    gnus-level-default-subscribed
15918                                  (if reads
15919                                      (1+ gnus-level-subscribed)
15920                                    gnus-level-default-unsubscribed))
15921                                (nreverse reads))))
15922             (setq newsrc (cons info newsrc))))))
15923       (forward-line 1))
15924
15925     (setq newsrc (nreverse newsrc))
15926
15927     (if (not already-read)
15928         ()
15929       ;; We now have two newsrc lists - `newsrc', which is what we
15930       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15931       ;; what we've read from .newsrc.eld.  We have to merge these
15932       ;; lists.  We do this by "attaching" any (foreign) groups in the
15933       ;; gnus-newsrc-alist to the (native) group that precedes them.
15934       (let ((rc (cdr gnus-newsrc-alist))
15935             (prev gnus-newsrc-alist)
15936             entry mentry)
15937         (while rc
15938           (or (null (nth 4 (car rc)))   ; It's a native group.
15939               (assoc (car (car rc)) newsrc) ; It's already in the alist.
15940               (if (setq entry (assoc (car (car prev)) newsrc))
15941                   (setcdr (setq mentry (memq entry newsrc))
15942                           (cons (car rc) (cdr mentry)))
15943                 (setq newsrc (cons (car rc) newsrc))))
15944           (setq prev rc
15945                 rc (cdr rc)))))
15946
15947     (setq gnus-newsrc-alist newsrc)
15948     ;; We make the newsrc hashtb.
15949     (gnus-make-hashtable-from-newsrc-alist)
15950
15951     ;; Finally, if we read some options lines, we parse them.
15952     (or (string= gnus-newsrc-options "")
15953         (gnus-newsrc-parse-options gnus-newsrc-options))))
15954
15955 ;; Parse options lines to find "options -n !all rec.all" and stuff.
15956 ;; The return value will be a list on the form
15957 ;; ((regexp1 . ignore)
15958 ;;  (regexp2 . subscribe)...)
15959 ;; When handling new newsgroups, groups that match a `ignore' regexp
15960 ;; will be ignored, and groups that match a `subscribe' regexp will be
15961 ;; subscribed.  A line like
15962 ;; options -n !all rec.all
15963 ;; will lead to a list that looks like
15964 ;; (("^rec\\..+" . subscribe)
15965 ;;  ("^.+" . ignore))
15966 ;; So all "rec.*" groups will be subscribed, while all the other
15967 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
15968 ;; different from "options -n rec.all !all".
15969 (defun gnus-newsrc-parse-options (options)
15970   (let (out eol)
15971     (save-excursion
15972       (gnus-set-work-buffer)
15973       (insert (regexp-quote options))
15974       ;; First we treat all continuation lines.
15975       (goto-char (point-min))
15976       (while (re-search-forward "\n[ \t]+" nil t)
15977         (replace-match " " t t))
15978       ;; Then we transform all "all"s into ".+"s.
15979       (goto-char (point-min))
15980       (while (re-search-forward "\\ball\\b" nil t)
15981         (replace-match ".+" t t))
15982       (goto-char (point-min))
15983       ;; We remove all other options than the "-n" ones.
15984       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
15985         (replace-match " ")
15986         (forward-char -1))
15987       (goto-char (point-min))
15988
15989       ;; We are only interested in "options -n" lines - we
15990       ;; ignore the other option lines.
15991       (while (re-search-forward "[ \t]-n" nil t)
15992         (setq eol
15993               (or (save-excursion
15994                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
15995                          (- (point) 2)))
15996                   (gnus-point-at-eol)))
15997         ;; Search for all "words"...
15998         (while (re-search-forward "[^ \t,\n]+" eol t)
15999           (if (= (char-after (match-beginning 0)) ?!)
16000               ;; If the word begins with a bang (!), this is a "not"
16001               ;; spec.  We put this spec (minus the bang) and the
16002               ;; symbol `ignore' into the list.
16003               (setq out (cons (cons (concat
16004                                      "^" (buffer-substring
16005                                           (1+ (match-beginning 0))
16006                                           (match-end 0)))
16007                                     'ignore) out))
16008             ;; There was no bang, so this is a "yes" spec.
16009             (setq out (cons (cons (concat "^" (match-string 0))
16010                                   'subscribe) out)))))
16011
16012       (setq gnus-newsrc-options-n out))))
16013
16014 (defun gnus-save-newsrc-file (&optional force)
16015   "Save .newsrc file."
16016   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16017   ;; from the variable gnus-newsrc-alist.
16018   (when (and (or gnus-newsrc-alist gnus-killed-list)
16019              gnus-current-startup-file)
16020     (save-excursion
16021       (if (and (or gnus-use-dribble-file gnus-slave)
16022                (not force)
16023                (or (not gnus-dribble-buffer)
16024                    (not (buffer-name gnus-dribble-buffer))
16025                    (zerop (save-excursion
16026                             (set-buffer gnus-dribble-buffer)
16027                             (buffer-size)))))
16028           (gnus-message 4 "(No changes need to be saved)")
16029         (run-hooks 'gnus-save-newsrc-hook)
16030         (if gnus-slave
16031             (gnus-slave-save-newsrc)
16032           ;; Save .newsrc.
16033           (when gnus-save-newsrc-file
16034             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16035             (gnus-gnus-to-newsrc-format)
16036             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16037           ;; Save .newsrc.eld.
16038           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16039           (make-local-variable 'version-control)
16040           (setq version-control 'never)
16041           (setq buffer-file-name
16042                 (concat gnus-current-startup-file ".eld"))
16043           (gnus-add-current-to-buffer-list)
16044           (buffer-disable-undo (current-buffer))
16045           (erase-buffer)
16046           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16047           (gnus-gnus-to-quick-newsrc-format)
16048           (run-hooks 'gnus-save-quick-newsrc-hook)
16049           (save-buffer)
16050           (kill-buffer (current-buffer))
16051           (gnus-message
16052            5 "Saving %s.eld...done" gnus-current-startup-file))
16053         (gnus-dribble-delete-file)))))
16054
16055 (defun gnus-gnus-to-quick-newsrc-format ()
16056   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16057   (insert ";; Gnus startup file.\n")
16058   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16059   (insert ";; to read .newsrc.\n")
16060   (insert "(setq gnus-newsrc-file-version "
16061           (prin1-to-string gnus-version) ")\n")
16062   (let ((variables
16063          (if gnus-save-killed-list gnus-variable-list
16064            ;; Remove the `gnus-killed-list' from the list of variables
16065            ;; to be saved, if required.
16066            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16067         ;; Peel off the "dummy" group.
16068         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16069         variable)
16070     ;; Insert the variables into the file.
16071     (while variables
16072       (when (and (boundp (setq variable (pop variables)))
16073                  (symbol-value variable))
16074         (insert "(setq " (symbol-name variable) " '"
16075                 (prin1-to-string (symbol-value variable)) ")\n")))))
16076
16077 (defun gnus-gnus-to-newsrc-format ()
16078   ;; Generate and save the .newsrc file.
16079   (let ((newsrc (cdr gnus-newsrc-alist))
16080         info ranges range)
16081     (save-excursion
16082       (set-buffer (create-file-buffer gnus-current-startup-file))
16083       (setq buffer-file-name gnus-current-startup-file)
16084       (buffer-disable-undo (current-buffer))
16085       (erase-buffer)
16086       ;; Write options.
16087       (if gnus-newsrc-options (insert gnus-newsrc-options))
16088       ;; Write subscribed and unsubscribed.
16089       (while newsrc
16090         (setq info (car newsrc))
16091         (if (not (gnus-info-method info))
16092             ;; Don't write foreign groups to .newsrc.
16093             (progn
16094               (insert (gnus-info-group info)
16095                       (if (> (gnus-info-level info) gnus-level-subscribed)
16096                           "!" ":"))
16097               (if (setq ranges (gnus-info-read info))
16098                   (progn
16099                     (insert " ")
16100                     (if (not (listp (cdr ranges)))
16101                         (if (= (car ranges) (cdr ranges))
16102                             (insert (int-to-string (car ranges)))
16103                           (insert (int-to-string (car ranges)) "-"
16104                                   (int-to-string (cdr ranges))))
16105                       (while ranges
16106                         (setq range (car ranges)
16107                               ranges (cdr ranges))
16108                         (if (or (atom range) (= (car range) (cdr range)))
16109                             (insert (int-to-string
16110                                      (or (and (atom range) range)
16111                                          (car range))))
16112                           (insert (int-to-string (car range)) "-"
16113                                   (int-to-string (cdr range))))
16114                         (if ranges (insert ","))))))
16115               (insert "\n")))
16116         (setq newsrc (cdr newsrc)))
16117       (make-local-variable 'version-control)
16118       (setq version-control 'never)
16119       ;; It has been reported that sometime the modtime on the .newsrc
16120       ;; file seems to be off.  We really do want to overwrite it, so
16121       ;; we clear the modtime here before saving.  It's a bit odd,
16122       ;; though...
16123       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16124       ;; delete the silly thing entirely first.  but this fails to provide
16125       ;; such niceties as .newsrc~ creation.
16126       (if gnus-modtime-botch
16127           (delete-file gnus-startup-file)
16128         (clear-visited-file-modtime))
16129       (run-hooks 'gnus-save-standard-newsrc-hook)
16130       (save-buffer)
16131       (kill-buffer (current-buffer)))))
16132
16133 \f
16134 ;;;
16135 ;;; Slave functions.
16136 ;;;
16137
16138 (defun gnus-slave-save-newsrc ()
16139   (save-excursion
16140     (set-buffer gnus-dribble-buffer)
16141     (let ((slave-name
16142            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16143       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16144
16145 (defun gnus-master-read-slave-newsrc ()
16146   (let ((slave-files
16147          (directory-files
16148           (file-name-directory gnus-current-startup-file)
16149           t (concat
16150              "^" (regexp-quote
16151                   (concat
16152                    (file-name-nondirectory gnus-current-startup-file)
16153                    "-slave-")))
16154           t))
16155         file)
16156     (if (not slave-files)
16157         ()                              ; There are no slave files to read.
16158       (gnus-message 7 "Reading slave newsrcs...")
16159       (save-excursion
16160         (set-buffer (get-buffer-create " *gnus slave*"))
16161         (buffer-disable-undo (current-buffer))
16162         (setq slave-files
16163               (sort (mapcar (lambda (file)
16164                               (list (nth 5 (file-attributes file)) file))
16165                             slave-files)
16166                     (lambda (f1 f2)
16167                       (or (< (car (car f1)) (car (car f2)))
16168                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16169         (while slave-files
16170           (erase-buffer)
16171           (setq file (nth 1 (car slave-files)))
16172           (insert-file-contents file)
16173           (if (condition-case ()
16174                   (progn
16175                     (eval-buffer (current-buffer))
16176                     t)
16177                 (error
16178                  (gnus-message 3 "Possible error in %s" file)
16179                  (ding)
16180                  (sit-for 2)
16181                  nil))
16182               (or gnus-slave ; Slaves shouldn't delete these files.
16183                   (condition-case ()
16184                       (delete-file file)
16185                     (error nil))))
16186           (setq slave-files (cdr slave-files))))
16187       (gnus-message 7 "Reading slave newsrcs...done"))))
16188
16189 \f
16190 ;;;
16191 ;;; Group description.
16192 ;;;
16193
16194 (defun gnus-read-all-descriptions-files ()
16195   (let ((methods (cons gnus-select-method 
16196                        (cons "archive"
16197                              gnus-secondary-select-methods))))
16198     (while methods
16199       (gnus-read-descriptions-file (car methods))
16200       (setq methods (cdr methods)))
16201     t))
16202
16203 (defun gnus-read-descriptions-file (&optional method)
16204   (let ((method (or method gnus-select-method)))
16205     (when (stringp method)
16206       (setq method (gnus-server-to-method method)))
16207     ;; We create the hashtable whether we manage to read the desc file
16208     ;; to avoid trying to re-read after a failed read.
16209     (or gnus-description-hashtb
16210         (setq gnus-description-hashtb
16211               (gnus-make-hashtable (length gnus-active-hashtb))))
16212     ;; Mark this method's desc file as read.
16213     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16214                   gnus-description-hashtb)
16215
16216     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16217     (cond
16218      ((not (gnus-check-server method))
16219       (gnus-message 1 "Couldn't open server")
16220       nil)
16221      ((not (gnus-request-list-newsgroups method))
16222       (gnus-message 1 "Couldn't read newsgroups descriptions")
16223       nil)
16224      (t
16225       (let (group)
16226         (save-excursion
16227           (save-restriction
16228             (set-buffer nntp-server-buffer)
16229             (goto-char (point-min))
16230             (if (or (search-forward "\n.\n" nil t)
16231                     (goto-char (point-max)))
16232                 (progn
16233                   (beginning-of-line)
16234                   (narrow-to-region (point-min) (point))))
16235             (goto-char (point-min))
16236             (while (not (eobp))
16237               ;; If we get an error, we set group to 0, which is not a
16238               ;; symbol...
16239               (setq group
16240                     (condition-case ()
16241                         (let ((obarray gnus-description-hashtb))
16242                           ;; Group is set to a symbol interned in this
16243                           ;; hash table.
16244                           (read nntp-server-buffer))
16245                       (error 0)))
16246               (skip-chars-forward " \t")
16247               ;; ...  which leads to this line being effectively ignored.
16248               (and (symbolp group)
16249                    (set group (buffer-substring
16250                                (point) (progn (end-of-line) (point)))))
16251               (forward-line 1))))
16252         (gnus-message 5 "Reading descriptions file...done")
16253         t)))))
16254
16255 (defun gnus-group-get-description (group)
16256   "Get the description of a group by sending XGTITLE to the server."
16257   (when (gnus-request-group-description group)
16258     (save-excursion
16259       (set-buffer nntp-server-buffer)
16260       (goto-char (point-min))
16261       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16262         (match-string 1)))))
16263
16264 \f
16265 ;;;
16266 ;;; Buffering of read articles.
16267 ;;;
16268
16269 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16270 (defvar gnus-backlog-articles nil)
16271 (defvar gnus-backlog-hashtb nil)
16272
16273 (defun gnus-backlog-buffer ()
16274   "Return the backlog buffer."
16275   (or (get-buffer gnus-backlog-buffer)
16276       (save-excursion
16277         (set-buffer (get-buffer-create gnus-backlog-buffer))
16278         (buffer-disable-undo (current-buffer))
16279         (setq buffer-read-only t)
16280         (gnus-add-current-to-buffer-list)
16281         (get-buffer gnus-backlog-buffer))))
16282
16283 (defun gnus-backlog-setup ()
16284   "Initialize backlog variables."
16285   (unless gnus-backlog-hashtb
16286     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16287
16288 (defun gnus-backlog-shutdown ()
16289   "Clear all backlog variables and buffers."
16290   (when (get-buffer gnus-backlog-buffer)
16291     (kill-buffer gnus-backlog-buffer))
16292   (setq gnus-backlog-hashtb nil
16293         gnus-backlog-articles nil))
16294
16295 (defun gnus-backlog-enter-article (group number buffer)
16296   (gnus-backlog-setup)
16297   (let ((ident (intern (concat group ":" (int-to-string number))
16298                        gnus-backlog-hashtb))
16299         b)
16300     (if (memq ident gnus-backlog-articles)
16301         () ; It's already kept.
16302       ;; Remove the oldest article, if necessary.
16303       (and (numberp gnus-keep-backlog)
16304            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16305            (gnus-backlog-remove-oldest-article))
16306       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16307       ;; Insert the new article.
16308       (save-excursion
16309         (set-buffer (gnus-backlog-buffer))
16310         (let (buffer-read-only)
16311           (goto-char (point-max))
16312           (or (bolp) (insert "\n"))
16313           (setq b (point))
16314           (insert-buffer-substring buffer)
16315           ;; Tag the beginning of the article with the ident.
16316           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16317
16318 (defun gnus-backlog-remove-oldest-article ()
16319   (save-excursion
16320     (set-buffer (gnus-backlog-buffer))
16321     (goto-char (point-min))
16322     (if (zerop (buffer-size))
16323         () ; The buffer is empty.
16324       (let ((ident (get-text-property (point) 'gnus-backlog))
16325             buffer-read-only)
16326         ;; Remove the ident from the list of articles.
16327         (when ident
16328           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16329         ;; Delete the article itself.
16330         (delete-region
16331          (point) (next-single-property-change
16332                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16333
16334 (defun gnus-backlog-request-article (group number buffer)
16335   (gnus-backlog-setup)
16336   (let ((ident (intern (concat group ":" (int-to-string number))
16337                        gnus-backlog-hashtb))
16338         beg end)
16339     (when (memq ident gnus-backlog-articles)
16340       ;; It was in the backlog.
16341       (save-excursion
16342         (set-buffer (gnus-backlog-buffer))
16343         (if (not (setq beg (text-property-any
16344                             (point-min) (point-max) 'gnus-backlog
16345                             ident)))
16346             ;; It wasn't in the backlog after all.
16347             (ignore
16348              (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16349           ;; Find the end (i. e., the beginning of the next article).
16350           (setq end
16351                 (next-single-property-change
16352                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16353       (let ((buffer-read-only nil))
16354         (erase-buffer)
16355         (insert-buffer-substring gnus-backlog-buffer beg end)
16356         t))))
16357
16358 ;; Allow redefinition of Gnus functions.
16359
16360 (gnus-ems-redefine)
16361
16362 (provide 'gnus)
16363
16364 ;;; gnus.el ends here