529450448e0615abf39d7a77e0c36c1cab74986b
[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     ("nnfolder" mail respool address))
1230   "An alist of valid select methods.
1231 The first element of each list lists should be a string with the name
1232 of the select method.  The other elements may be be the category of
1233 this method (ie. `post', `mail', `none' or whatever) or other
1234 properties that this method has (like being respoolable).
1235 If you implement a new select method, all you should have to change is
1236 this variable.  I think.")
1237
1238 (defvar gnus-updated-mode-lines '(group article summary tree)
1239   "*List of buffers that should update their mode lines.
1240 The list may contain the symbols `group', `article' and `summary'.  If
1241 the corresponding symbol is present, Gnus will keep that mode line
1242 updated with information that may be pertinent.
1243 If this variable is nil, screen refresh may be quicker.")
1244
1245 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1246 (defvar gnus-mode-non-string-length nil
1247   "*Max length of mode-line non-string contents.
1248 If this is nil, Gnus will take space as is needed, leaving the rest
1249 of the modeline intact.")
1250
1251 ;see gnus-cus.el
1252 ;(defvar gnus-mouse-face 'highlight
1253 ;  "*Face used for mouse highlighting in Gnus.
1254 ;No mouse highlights will be done if `gnus-visual' is nil.")
1255
1256 (defvar gnus-summary-mark-below nil
1257   "*Mark all articles with a score below this variable as read.
1258 This variable is local to each summary buffer and usually set by the
1259 score file.")
1260
1261 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1262   "*List of functions used for sorting articles in the summary buffer.
1263 This variable is only used when not using a threaded display.")
1264
1265 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1266   "*List of functions used for sorting threads in the summary buffer.
1267 By default, threads are sorted by article number.
1268
1269 Each function takes two threads and return non-nil if the first thread
1270 should be sorted before the other.  If you use more than one function,
1271 the primary sort function should be the last.  You should probably
1272 always include `gnus-thread-sort-by-number' in the list of sorting
1273 functions -- preferably first.
1274
1275 Ready-mady functions include `gnus-thread-sort-by-number',
1276 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1277 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1278 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1279
1280 (defvar gnus-thread-score-function '+
1281   "*Function used for calculating the total score of a thread.
1282
1283 The function is called with the scores of the article and each
1284 subthread and should then return the score of the thread.
1285
1286 Some functions you can use are `+', `max', or `min'.")
1287
1288 (defvar gnus-summary-expunge-below nil
1289   "All articles that have a score less than this variable will be expunged.")
1290
1291 (defvar gnus-thread-expunge-below nil
1292   "All threads that have a total score less than this variable will be expunged.
1293 See `gnus-thread-score-function' for en explanation of what a
1294 \"thread score\" is.")
1295
1296 (defvar gnus-auto-subscribed-groups
1297   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1298   "*All new groups that match this regexp will be subscribed automatically.
1299 Note that this variable only deals with new groups.  It has no effect
1300 whatsoever on old groups.")
1301
1302 (defvar gnus-options-subscribe nil
1303   "*All new groups matching this regexp will be subscribed unconditionally.
1304 Note that this variable deals only with new newsgroups.  This variable
1305 does not affect old newsgroups.")
1306
1307 (defvar gnus-options-not-subscribe nil
1308   "*All new groups matching this regexp will be ignored.
1309 Note that this variable deals only with new newsgroups.  This variable
1310 does not affect old (already subscribed) newsgroups.")
1311
1312 (defvar gnus-auto-expirable-newsgroups nil
1313   "*Groups in which to automatically mark read articles as expirable.
1314 If non-nil, this should be a regexp that should match all groups in
1315 which to perform auto-expiry.  This only makes sense for mail groups.")
1316
1317 (defvar gnus-total-expirable-newsgroups nil
1318   "*Groups in which to perform expiry of all read articles.
1319 Use with extreme caution.  All groups that match this regexp will be
1320 expiring - which means that all read articles will be deleted after
1321 (say) one week.  (This only goes for mail groups and the like, of
1322 course.)")
1323
1324 (defvar gnus-group-uncollapsed-levels 1
1325   "Number of group name elements to leave alone when making a short group name.")
1326
1327 (defvar gnus-hidden-properties '(invisible t intangible t)
1328   "Property list to use for hiding text.")
1329
1330 (defvar gnus-modtime-botch nil
1331   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1332 due to the bogus appearance that .newsrc was modified on disc.")
1333
1334 ;; Hooks.
1335
1336 (defvar gnus-group-mode-hook nil
1337   "*A hook for Gnus group mode.")
1338
1339 (defvar gnus-summary-mode-hook nil
1340   "*A hook for Gnus summary mode.
1341 This hook is run before any variables are set in the summary buffer.")
1342
1343 (defvar gnus-article-mode-hook nil
1344   "*A hook for Gnus article mode.")
1345
1346 (defvar gnus-summary-prepare-exit-hook nil
1347   "*A hook called when preparing to exit from the summary buffer.
1348 It calls `gnus-summary-expire-articles' by default.")
1349 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1350
1351 (defvar gnus-summary-exit-hook nil
1352   "*A hook called on exit from the summary buffer.")
1353
1354 (defvar gnus-group-catchup-group-hook nil
1355   "*A hook run when catching up a group from the group buffer.")
1356
1357 (defvar gnus-open-server-hook nil
1358   "*A hook called just before opening connection to the news server.")
1359
1360 (defvar gnus-load-hook nil
1361   "*A hook run while Gnus is loaded.")
1362
1363 (defvar gnus-startup-hook nil
1364   "*A hook called at startup.
1365 This hook is called after Gnus is connected to the NNTP server.")
1366
1367 (defvar gnus-get-new-news-hook nil
1368   "*A hook run just before Gnus checks for new news.")
1369
1370 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1371   "*A function that is called to generate the group buffer.
1372 The function is called with three arguments: The first is a number;
1373 all group with a level less or equal to that number should be listed,
1374 if the second is non-nil, empty groups should also be displayed.  If
1375 the third is non-nil, it is a number.  No groups with a level lower
1376 than this number should be displayed.
1377
1378 The only current function implemented is `gnus-group-prepare-flat'.")
1379
1380 (defvar gnus-group-prepare-hook nil
1381   "*A hook called after the group buffer has been generated.
1382 If you want to modify the group buffer, you can use this hook.")
1383
1384 (defvar gnus-summary-prepare-hook nil
1385   "*A hook called after the summary buffer has been generated.
1386 If you want to modify the summary buffer, you can use this hook.")
1387
1388 (defvar gnus-summary-generate-hook nil
1389   "*A hook run just before generating the summary buffer.
1390 This hook is commonly used to customize threading variables and the
1391 like.")
1392
1393 (defvar gnus-article-prepare-hook nil
1394   "*A hook called after an article has been prepared in the article buffer.
1395 If you want to run a special decoding program like nkf, use this hook.")
1396
1397 ;(defvar gnus-article-display-hook nil
1398 ;  "*A hook called after the article is displayed in the article buffer.
1399 ;The hook is designed to change the contents of the article
1400 ;buffer.  Typical functions that this hook may contain are
1401 ;`gnus-article-hide-headers' (hide selected headers),
1402 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1403 ;`gnus-article-hide-signature' (hide signature) and
1404 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1405 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1406 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1407 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1408
1409 (defvar gnus-article-x-face-command
1410   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1411   "String or function to be executed to display an X-Face header.
1412 If it is a string, the command will be executed in a sub-shell
1413 asynchronously.  The compressed face will be piped to this command.")
1414
1415 (defvar gnus-article-x-face-too-ugly nil
1416   "Regexp matching posters whose face shouldn't be shown automatically.")
1417
1418 (defvar gnus-select-group-hook nil
1419   "*A hook called when a newsgroup is selected.
1420
1421 If you'd like to simplify subjects like the
1422 `gnus-summary-next-same-subject' command does, you can use the
1423 following hook:
1424
1425  (setq gnus-select-group-hook
1426       (list
1427         (lambda ()
1428           (mapcar (lambda (header)
1429                      (mail-header-set-subject
1430                       header
1431                       (gnus-simplify-subject
1432                        (mail-header-subject header) 're-only)))
1433                   gnus-newsgroup-headers))))")
1434
1435 (defvar gnus-select-article-hook nil
1436   "*A hook called when an article is selected.")
1437
1438 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1439   "*A hook called to apply kill files to a group.
1440 This hook is intended to apply a kill file to the selected newsgroup.
1441 The function `gnus-apply-kill-file' is called by default.
1442
1443 Since a general kill file is too heavy to use only for a few
1444 newsgroups, I recommend you to use a lighter hook function.  For
1445 example, if you'd like to apply a kill file to articles which contains
1446 a string `rmgroup' in subject in newsgroup `control', you can use the
1447 following hook:
1448
1449  (setq gnus-apply-kill-hook
1450       (list
1451         (lambda ()
1452           (cond ((string-match \"control\" gnus-newsgroup-name)
1453                  (gnus-kill \"Subject\" \"rmgroup\")
1454                  (gnus-expunge \"X\"))))))")
1455
1456 (defvar gnus-visual-mark-article-hook
1457   (list 'gnus-highlight-selected-summary)
1458   "*Hook run after selecting an article in the summary buffer.
1459 It is meant to be used for highlighting the article in some way.  It
1460 is not run if `gnus-visual' is nil.")
1461
1462 (defvar gnus-parse-headers-hook nil
1463   "*A hook called before parsing the headers.")
1464
1465 (defvar gnus-exit-group-hook nil
1466   "*A hook called when exiting (not quitting) summary mode.")
1467
1468 (defvar gnus-suspend-gnus-hook nil
1469   "*A hook called when suspending (not exiting) Gnus.")
1470
1471 (defvar gnus-exit-gnus-hook nil
1472   "*A hook called when exiting Gnus.")
1473
1474 (defvar gnus-after-exiting-gnus-hook nil
1475   "*A hook called after exiting Gnus.")
1476
1477 (defvar gnus-save-newsrc-hook nil
1478   "*A hook called before saving any of the newsrc files.")
1479
1480 (defvar gnus-save-quick-newsrc-hook nil
1481   "*A hook called just before saving the quick newsrc file.
1482 Can be used to turn version control on or off.")
1483
1484 (defvar gnus-save-standard-newsrc-hook nil
1485   "*A hook called just before saving the standard newsrc file.
1486 Can be used to turn version control on or off.")
1487
1488 (defvar gnus-summary-update-hook
1489   (list 'gnus-summary-highlight-line)
1490   "*A hook called when a summary line is changed.
1491 The hook will not be called if `gnus-visual' is nil.
1492
1493 The default function `gnus-summary-highlight-line' will
1494 highlight the line according to the `gnus-summary-highlight'
1495 variable.")
1496
1497 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1498   "*A hook called when a group line is changed.
1499 The hook will not be called if `gnus-visual' is nil.
1500
1501 The default function `gnus-group-highlight-line' will
1502 highlight the line according to the `gnus-group-highlight'
1503 variable.")
1504
1505 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1506   "*A hook called when an article is selected for the first time.
1507 The hook is intended to mark an article as read (or unread)
1508 automatically when it is selected.")
1509
1510 (defvar gnus-group-change-level-function nil
1511   "Function run when a group level is changed.
1512 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1513
1514 ;; Remove any hilit infestation.
1515 (add-hook 'gnus-startup-hook
1516           (lambda ()
1517             (remove-hook 'gnus-summary-prepare-hook
1518                          'hilit-rehighlight-buffer-quietly)
1519             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1520             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1521             (remove-hook 'gnus-article-prepare-hook
1522                          'hilit-rehighlight-buffer-quietly)))
1523
1524 \f
1525 ;; Internal variables
1526
1527 (defvar gnus-server-alist nil
1528   "List of available servers.")
1529
1530 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1531
1532 (defvar gnus-goto-missing-group-function nil)
1533
1534 (defvar gnus-override-subscribe-method nil)
1535
1536 (defvar gnus-group-goto-next-group-function nil
1537   "Function to override finding the next group after listing groups.")
1538
1539 (defconst gnus-article-mark-lists
1540   '((marked . tick) (replied . reply)
1541     (expirable . expire) (killed . killed)
1542     (bookmarks . bookmark) (dormant . dormant)
1543     (scored . score) (saved . save)
1544     (cached . cache)
1545     ))
1546
1547 ;; Avoid highlighting in kill files.
1548 (defvar gnus-summary-inhibit-highlight nil)
1549 (defvar gnus-newsgroup-selected-overlay nil)
1550
1551 (defvar gnus-inhibit-hiding nil)
1552 (defvar gnus-group-indentation "")
1553 (defvar gnus-inhibit-limiting nil)
1554
1555 (defvar gnus-article-mode-map nil)
1556 (defvar gnus-dribble-buffer nil)
1557 (defvar gnus-headers-retrieved-by nil)
1558 (defvar gnus-article-reply nil)
1559 (defvar gnus-override-method nil)
1560 (defvar gnus-article-check-size nil)
1561
1562 (defvar gnus-current-score-file nil)
1563 (defvar gnus-newsgroup-adaptive-score-file nil)
1564 (defvar gnus-scores-exclude-files nil)
1565
1566 (defvar gnus-opened-servers nil)
1567
1568 (defvar gnus-current-move-group nil)
1569
1570 (defvar gnus-newsgroup-dependencies nil)
1571 (defvar gnus-newsgroup-async nil)
1572 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1573
1574 (defvar gnus-newsgroup-adaptive nil)
1575
1576 (defvar gnus-summary-display-table nil)
1577 (defvar gnus-summary-display-article-function nil)
1578
1579 (defvar gnus-summary-highlight-line-function nil
1580   "Function called after highlighting a summary line.")
1581
1582 (defvar gnus-group-line-format-alist
1583   `((?M gnus-tmp-marked-mark ?c)
1584     (?S gnus-tmp-subscribed ?c)
1585     (?L gnus-tmp-level ?d)
1586     (?N (cond ((eq number t) "*" )
1587               ((numberp number) 
1588                (int-to-string
1589                 (+ number
1590                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1591                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1592               (t number)) ?s)
1593     (?R gnus-tmp-number-of-read ?s)
1594     (?t gnus-tmp-number-total ?d)
1595     (?y gnus-tmp-number-of-unread ?s)
1596     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1597     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1598     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1599            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1600     (?g gnus-tmp-group ?s)
1601     (?G gnus-tmp-qualified-group ?s)
1602     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1603     (?D gnus-tmp-newsgroup-description ?s)
1604     (?o gnus-tmp-moderated ?c)
1605     (?O gnus-tmp-moderated-string ?s)
1606     (?p gnus-tmp-process-marked ?c)
1607     (?s gnus-tmp-news-server ?s)
1608     (?n gnus-tmp-news-method ?s)
1609     (?P gnus-group-indentation ?s)
1610     (?z gnus-tmp-news-method-string ?s)
1611     (?u gnus-tmp-user-defined ?s)))
1612
1613 (defvar gnus-summary-line-format-alist
1614   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1615     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1616     (?s gnus-tmp-subject-or-nil ?s)
1617     (?n gnus-tmp-name ?s)
1618     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1619         ?s)
1620     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1621             gnus-tmp-from) ?s)
1622     (?F gnus-tmp-from ?s)
1623     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1624     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1625     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1626     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1627     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1628     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1629     (?L gnus-tmp-lines ?d)
1630     (?I gnus-tmp-indentation ?s)
1631     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1632     (?R gnus-tmp-replied ?c)
1633     (?\[ gnus-tmp-opening-bracket ?c)
1634     (?\] gnus-tmp-closing-bracket ?c)
1635     (?\> (make-string gnus-tmp-level ? ) ?s)
1636     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1637     (?i gnus-tmp-score ?d)
1638     (?z gnus-tmp-score-char ?c)
1639     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1640     (?U gnus-tmp-unread ?c)
1641     (?t (gnus-summary-number-of-articles-in-thread
1642          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1643         ?d)
1644     (?e (gnus-summary-number-of-articles-in-thread
1645          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1646         ?c)
1647     (?u gnus-tmp-user-defined ?s))
1648   "An alist of format specifications that can appear in summary lines,
1649 and what variables they correspond with, along with the type of the
1650 variable (string, integer, character, etc).")
1651
1652 (defvar gnus-summary-dummy-line-format-alist
1653   `((?S gnus-tmp-subject ?s)
1654     (?N gnus-tmp-number ?d)
1655     (?u gnus-tmp-user-defined ?s)))
1656
1657 (defvar gnus-summary-mode-line-format-alist
1658   `((?G gnus-tmp-group-name ?s)
1659     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1660     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1661     (?A gnus-tmp-article-number ?d)
1662     (?Z gnus-tmp-unread-and-unselected ?s)
1663     (?V gnus-version ?s)
1664     (?U gnus-tmp-unread ?d)
1665     (?S gnus-tmp-subject ?s)
1666     (?e gnus-tmp-unselected ?d)
1667     (?u gnus-tmp-user-defined ?s)
1668     (?d (length gnus-newsgroup-dormant) ?d)
1669     (?t (length gnus-newsgroup-marked) ?d)
1670     (?r (length gnus-newsgroup-reads) ?d)
1671     (?E gnus-newsgroup-expunged-tally ?d)
1672     (?s (gnus-current-score-file-nondirectory) ?s)))
1673
1674 (defvar gnus-article-mode-line-format-alist
1675   gnus-summary-mode-line-format-alist)
1676
1677 (defvar gnus-group-mode-line-format-alist
1678   `((?S gnus-tmp-news-server ?s)
1679     (?M gnus-tmp-news-method ?s)
1680     (?u gnus-tmp-user-defined ?s)))
1681
1682 (defvar gnus-have-read-active-file nil)
1683
1684 (defconst gnus-maintainer
1685   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1686   "The mail address of the Gnus maintainers.")
1687
1688 (defconst gnus-version "September Gnus v0.40"
1689   "Version number for this version of Gnus.")
1690
1691 (defvar gnus-info-nodes
1692   '((gnus-group-mode            "(gnus)The Group Buffer")
1693     (gnus-summary-mode          "(gnus)The Summary Buffer")
1694     (gnus-article-mode          "(gnus)The Article Buffer"))
1695   "Assoc list of major modes and related Info nodes.")
1696
1697 (defvar gnus-group-buffer "*Group*")
1698 (defvar gnus-summary-buffer "*Summary*")
1699 (defvar gnus-article-buffer "*Article*")
1700 (defvar gnus-server-buffer "*Server*")
1701
1702 (defvar gnus-work-buffer " *gnus work*")
1703
1704 (defvar gnus-original-article-buffer " *Original Article*")
1705 (defvar gnus-original-article nil)
1706
1707 (defvar gnus-buffer-list nil
1708   "Gnus buffers that should be killed on exit.")
1709
1710 (defvar gnus-slave nil
1711   "Whether this Gnus is a slave or not.")
1712
1713 (defvar gnus-variable-list
1714   '(gnus-newsrc-options gnus-newsrc-options-n
1715     gnus-newsrc-last-checked-date
1716     gnus-newsrc-alist gnus-server-alist
1717     gnus-killed-list gnus-zombie-list
1718     gnus-topic-topology gnus-topic-alist
1719     gnus-format-specs)
1720   "Gnus variables saved in the quick startup file.")
1721
1722 (defvar gnus-newsrc-options nil
1723   "Options line in the .newsrc file.")
1724
1725 (defvar gnus-newsrc-options-n nil
1726   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1727
1728 (defvar gnus-newsrc-last-checked-date nil
1729   "Date Gnus last asked server for new newsgroups.")
1730
1731 (defvar gnus-topic-topology nil
1732   "The complete topic hierarchy.")
1733
1734 (defvar gnus-topic-alist nil
1735   "The complete topic-group alist.")
1736
1737 (defvar gnus-newsrc-alist nil
1738   "Assoc list of read articles.
1739 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1740
1741 (defvar gnus-newsrc-hashtb nil
1742   "Hashtable of gnus-newsrc-alist.")
1743
1744 (defvar gnus-killed-list nil
1745   "List of killed newsgroups.")
1746
1747 (defvar gnus-killed-hashtb nil
1748   "Hash table equivalent of gnus-killed-list.")
1749
1750 (defvar gnus-zombie-list nil
1751   "List of almost dead newsgroups.")
1752
1753 (defvar gnus-description-hashtb nil
1754   "Descriptions of newsgroups.")
1755
1756 (defvar gnus-list-of-killed-groups nil
1757   "List of newsgroups that have recently been killed by the user.")
1758
1759 (defvar gnus-active-hashtb nil
1760   "Hashtable of active articles.")
1761
1762 (defvar gnus-moderated-list nil
1763   "List of moderated newsgroups.")
1764
1765 (defvar gnus-group-marked nil)
1766
1767 (defvar gnus-current-startup-file nil
1768   "Startup file for the current host.")
1769
1770 (defvar gnus-last-search-regexp nil
1771   "Default regexp for article search command.")
1772
1773 (defvar gnus-last-shell-command nil
1774   "Default shell command on article.")
1775
1776 (defvar gnus-current-select-method nil
1777   "The current method for selecting a newsgroup.")
1778
1779 (defvar gnus-group-list-mode nil)
1780
1781 (defvar gnus-article-internal-prepare-hook nil)
1782
1783 (defvar gnus-newsgroup-name nil)
1784 (defvar gnus-newsgroup-begin nil)
1785 (defvar gnus-newsgroup-end nil)
1786 (defvar gnus-newsgroup-last-rmail nil)
1787 (defvar gnus-newsgroup-last-mail nil)
1788 (defvar gnus-newsgroup-last-folder nil)
1789 (defvar gnus-newsgroup-last-file nil)
1790 (defvar gnus-newsgroup-auto-expire nil)
1791 (defvar gnus-newsgroup-active nil)
1792
1793 (defvar gnus-newsgroup-data nil)
1794 (defvar gnus-newsgroup-data-reverse nil)
1795 (defvar gnus-newsgroup-limit nil)
1796 (defvar gnus-newsgroup-limits nil)
1797
1798 (defvar gnus-newsgroup-unreads nil
1799   "List of unread articles in the current newsgroup.")
1800
1801 (defvar gnus-newsgroup-unselected nil
1802   "List of unselected unread articles in the current newsgroup.")
1803
1804 (defvar gnus-newsgroup-reads nil
1805   "Alist of read articles and article marks in the current newsgroup.")
1806
1807 (defvar gnus-newsgroup-expunged-tally nil)
1808
1809 (defvar gnus-newsgroup-marked nil
1810   "List of ticked articles in the current newsgroup (a subset of unread art).")
1811
1812 (defvar gnus-newsgroup-killed nil
1813   "List of ranges of articles that have been through the scoring process.")
1814
1815 (defvar gnus-newsgroup-cached nil
1816   "List of articles that come from the article cache.")
1817
1818 (defvar gnus-newsgroup-saved nil
1819   "List of articles that have been saved.")
1820
1821 (defvar gnus-newsgroup-kill-headers nil)
1822
1823 (defvar gnus-newsgroup-replied nil
1824   "List of articles that have been replied to in the current newsgroup.")
1825
1826 (defvar gnus-newsgroup-expirable nil
1827   "List of articles in the current newsgroup that can be expired.")
1828
1829 (defvar gnus-newsgroup-processable nil
1830   "List of articles in the current newsgroup that can be processed.")
1831
1832 (defvar gnus-newsgroup-bookmarks nil
1833   "List of articles in the current newsgroup that have bookmarks.")
1834
1835 (defvar gnus-newsgroup-dormant nil
1836   "List of dormant articles in the current newsgroup.")
1837
1838 (defvar gnus-newsgroup-scored nil
1839   "List of scored articles in the current newsgroup.")
1840
1841 (defvar gnus-newsgroup-headers nil
1842   "List of article headers in the current newsgroup.")
1843
1844 (defvar gnus-newsgroup-threads nil)
1845
1846 (defvar gnus-newsgroup-prepared nil
1847   "Whether the current group has been prepared properly.")
1848
1849 (defvar gnus-newsgroup-ancient nil
1850   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1851
1852 (defvar gnus-newsgroup-sparse nil)
1853
1854 (defvar gnus-current-article nil)
1855 (defvar gnus-article-current nil)
1856 (defvar gnus-current-headers nil)
1857 (defvar gnus-have-all-headers nil)
1858 (defvar gnus-last-article nil)
1859 (defvar gnus-newsgroup-history nil)
1860 (defvar gnus-current-kill-article nil)
1861
1862 ;; Save window configuration.
1863 (defvar gnus-prev-winconf nil)
1864
1865 (defvar gnus-summary-mark-positions nil)
1866 (defvar gnus-group-mark-positions nil)
1867
1868 (defvar gnus-reffed-article-number nil)
1869
1870 ;;; Let the byte-compiler know that we know about this variable.
1871 (defvar rmail-default-rmail-file)
1872
1873 (defvar gnus-cache-removable-articles nil)
1874
1875 (defvar gnus-dead-summary nil)
1876
1877 (defconst gnus-summary-local-variables
1878   '(gnus-newsgroup-name
1879     gnus-newsgroup-begin gnus-newsgroup-end
1880     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1881     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1882     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1883     gnus-newsgroup-unselected gnus-newsgroup-marked
1884     gnus-newsgroup-reads gnus-newsgroup-saved
1885     gnus-newsgroup-replied gnus-newsgroup-expirable
1886     gnus-newsgroup-processable gnus-newsgroup-killed
1887     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1888     gnus-newsgroup-headers gnus-newsgroup-threads
1889     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1890     gnus-current-article gnus-current-headers gnus-have-all-headers
1891     gnus-last-article gnus-article-internal-prepare-hook
1892     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1893     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1894     gnus-newsgroup-async 
1895     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1896     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1897     gnus-newsgroup-history gnus-newsgroup-ancient
1898     gnus-newsgroup-sparse
1899     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1900     gnus-newsgroup-adaptive-score-file
1901     (gnus-newsgroup-expunged-tally . 0)
1902     gnus-cache-removeable-articles gnus-newsgroup-cached
1903     gnus-newsgroup-data gnus-newsgroup-data-reverse
1904     gnus-newsgroup-limit gnus-newsgroup-limits)
1905   "Variables that are buffer-local to the summary buffers.")
1906
1907 (defconst gnus-bug-message
1908   "Sending a bug report to the Gnus Towers.
1909 ========================================
1910
1911 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1912 be sent to the Gnus Bug Exterminators.
1913
1914 At the bottom of the buffer you'll see lots of variable settings.
1915 Please do not delete those.  They will tell the Bug People what your
1916 environment is, so that it will be easier to locate the bugs.
1917
1918 If you have found a bug that makes Emacs go \"beep\", set
1919 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1920 and include the backtrace in your bug report.
1921
1922 Please describe the bug in annoying, painstaking detail.
1923
1924 Thank you for your help in stamping out bugs.
1925 ")
1926
1927 ;;; End of variables.
1928
1929 ;; Define some autoload functions Gnus might use.
1930 (eval-and-compile
1931
1932   ;; This little mapcar goes through the list below and marks the
1933   ;; symbols in question as autoloaded functions.
1934   (mapcar
1935    (lambda (package)
1936      (let ((interactive (nth 1 (memq ':interactive package))))
1937        (mapcar
1938         (lambda (function)
1939           (let (keymap)
1940             (when (consp function)
1941               (setq keymap (car (memq 'keymap function)))
1942               (setq function (car function)))
1943             (autoload function (car package) nil interactive keymap)))
1944         (if (eq (nth 1 package) ':interactive)
1945             (cdddr package)
1946           (cdr package)))))
1947    '(("metamail" metamail-buffer)
1948      ("info" Info-goto-node)
1949      ("hexl" hexl-hex-string-to-integer)
1950      ("pp" pp pp-to-string pp-eval-expression)
1951      ("mail-extr" mail-extract-address-components)
1952      ("nnmail" nnmail-split-fancy nnmail-article-group)
1953      ("nnvirtual" nnvirtual-catchup-group)
1954      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1955       timezone-make-sortable-date timezone-make-time-string)
1956      ("sendmail" mail-position-on-field mail-setup)
1957      ("rmailout" rmail-output)
1958      ("rnewspost" news-mail-other-window news-reply-yank-original
1959       news-caesar-buffer-body)
1960      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1961       rmail-show-message)
1962      ("gnus-soup" :interactive t
1963       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1964       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1965      ("nnsoup" nnsoup-pack-replies)
1966      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1967       gnus-Folder-save-name gnus-folder-save-name)
1968      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1969      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1970       gnus-server-make-menu-bar gnus-article-make-menu-bar
1971       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1972       gnus-summary-highlight-line gnus-carpal-setup-buffer
1973       gnus-group-highlight-line
1974       gnus-article-add-button gnus-insert-next-page-button
1975       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1976      ("gnus-vis" :interactive t
1977       gnus-article-push-button gnus-article-press-button
1978       gnus-article-highlight gnus-article-highlight-some
1979       gnus-article-highlight-headers gnus-article-highlight-signature
1980       gnus-article-add-buttons gnus-article-add-buttons-to-head
1981       gnus-article-next-button gnus-article-prev-button)
1982      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1983       gnus-demon-add-disconnection gnus-demon-add-handler
1984       gnus-demon-remove-handler)
1985      ("gnus-demon" :interactive t
1986       gnus-demon-init gnus-demon-cancel)
1987      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1988       gnus-tree-open gnus-tree-close)
1989      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
1990       gnus-nocem-unwanted-article-p)
1991      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
1992      ("gnus-srvr" gnus-browse-foreign-server)
1993      ("gnus-cite" :interactive t
1994       gnus-article-highlight-citation gnus-article-hide-citation-maybe
1995       gnus-article-hide-citation gnus-article-fill-cited-article)
1996      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
1997       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
1998       gnus-execute gnus-expunge)
1999      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2000       gnus-cache-possibly-remove-articles gnus-cache-request-article
2001       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2002       gnus-cache-enter-remove-article gnus-cached-article-p
2003       gnus-cache-open gnus-cache-close)
2004      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2005       gnus-cache-remove-article)
2006      ("gnus-score" :interactive t
2007       gnus-summary-increase-score gnus-summary-lower-score
2008       gnus-score-flush-cache gnus-score-close
2009       gnus-score-raise-same-subject-and-select
2010       gnus-score-raise-same-subject gnus-score-default
2011       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2012       gnus-score-lower-same-subject gnus-score-lower-thread
2013       gnus-possibly-score-headers)
2014      ("gnus-score"
2015       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2016       gnus-current-score-file-nondirectory gnus-score-adaptive
2017       gnus-score-find-trace gnus-score-file-name)
2018      ("gnus-edit" :interactive t gnus-score-customize)
2019      ("gnus-topic" :interactive t gnus-topic-mode)
2020      ("gnus-topic" gnus-topic-remove-group)
2021      ("gnus-salt" :interactive t gnus-pick-mode)
2022      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2023      ("gnus-uu" :interactive t
2024       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2025       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2026       gnus-uu-mark-by-regexp gnus-uu-mark-all
2027       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2028       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2029       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2030       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2031       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2032       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2033       gnus-uu-decode-binhex-view)
2034      ("gnus-msg" (gnus-summary-send-map keymap)
2035       gnus-mail-yank-original gnus-mail-send-and-exit
2036       gnus-sendmail-setup-mail gnus-article-mail
2037       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2038      ("gnus-msg" :interactive t
2039       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2040       gnus-summary-followup gnus-summary-followup-with-original
2041       gnus-summary-followup-and-reply
2042       gnus-summary-followup-and-reply-with-original
2043       gnus-summary-cancel-article gnus-summary-supersede-article
2044       gnus-post-news gnus-inews-news gnus-cancel-news
2045       gnus-summary-reply gnus-summary-reply-with-original
2046       gnus-summary-mail-forward gnus-summary-mail-other-window
2047       gnus-bug)
2048      ("gnus-picon" :interactive t gnus-article-display-picons
2049       gnus-group-display-picons gnus-picons-article-display-x-face)
2050      ("gnus-vm" gnus-vm-mail-setup)
2051      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2052       gnus-summary-save-article-vm gnus-yank-article))))
2053
2054 \f
2055
2056 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2057 ;; If you want the cursor to go somewhere else, set these two
2058 ;; functions in some startup hook to whatever you want.
2059 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2060 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2061
2062 ;;; Various macros and substs.
2063
2064 (defun gnus-header-from (header)
2065   (mail-header-from header))
2066
2067 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2068   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2069   `(let ((GnusStartBufferWindow (selected-window)))
2070      (unwind-protect
2071          (progn
2072            (pop-to-buffer ,buffer)
2073            ,@forms)
2074        (select-window GnusStartBufferWindow))))
2075
2076 (defmacro gnus-gethash (string hashtable)
2077   "Get hash value of STRING in HASHTABLE."
2078   `(symbol-value (intern-soft ,string ,hashtable)))
2079
2080 (defmacro gnus-sethash (string value hashtable)
2081   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2082   `(set (intern ,string ,hashtable) ,value))
2083
2084 (defmacro gnus-intern-safe (string hashtable)
2085   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2086   `(let ((symbol (intern ,string ,hashtable)))
2087      (or (boundp symbol)
2088          (set symbol nil))
2089      symbol))
2090
2091 (defmacro gnus-group-unread (group)
2092   "Get the currently computed number of unread articles in GROUP."
2093   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2094
2095 (defmacro gnus-group-entry (group)
2096   "Get the newsrc entry for GROUP."
2097   `(gnus-gethash ,group gnus-newsrc-hashtb))
2098
2099 (defmacro gnus-active (group)
2100   "Get active info on GROUP."
2101   `(gnus-gethash ,group gnus-active-hashtb))
2102
2103 (defmacro gnus-set-active (group active)
2104   "Set GROUP's active info."
2105   `(gnus-sethash ,group ,active gnus-active-hashtb))
2106
2107 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2108 ;;   function `substring' might cut on a middle of multi-octet
2109 ;;   character.
2110 (defun gnus-truncate-string (str width)
2111   (substring str 0 width))
2112
2113 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2114 ;; to limit the length of a string.  This function is necessary since
2115 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2116 (defsubst gnus-limit-string (str width)
2117   (if (> (length str) width)
2118       (substring str 0 width)
2119     str))
2120
2121 (defsubst gnus-simplify-subject-re (subject)
2122   "Remove \"Re:\" from subject lines."
2123   (if (string-match "^[Rr][Ee]: *" subject)
2124       (substring subject (match-end 0))
2125     subject))
2126
2127 (defsubst gnus-goto-char (point)
2128   (and point (goto-char point)))
2129
2130 (defmacro gnus-buffer-exists-p (buffer)
2131   `(and ,buffer
2132         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2133                  ,buffer)))
2134
2135 (defmacro gnus-kill-buffer (buffer)
2136   `(let ((buf ,buffer))
2137      (if (gnus-buffer-exists-p buf)
2138          (kill-buffer buf))))
2139
2140 (defsubst gnus-point-at-bol ()
2141   "Return point at the beginning of the line."
2142   (let ((p (point)))
2143     (beginning-of-line)
2144     (prog1
2145         (point)
2146       (goto-char p))))
2147
2148 (defsubst gnus-point-at-eol ()
2149   "Return point at the end of the line."
2150   (let ((p (point)))
2151     (end-of-line)
2152     (prog1
2153         (point)
2154       (goto-char p))))
2155
2156 ;; Delete the current line (and the next N lines.);
2157 (defmacro gnus-delete-line (&optional n)
2158   `(delete-region (progn (beginning-of-line) (point))
2159                   (progn (forward-line ,(or n 1)) (point))))
2160
2161 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2162 (defvar gnus-init-inhibit nil)
2163 (defun gnus-read-init-file (&optional inhibit-next)
2164   (if gnus-init-inhibit
2165       (setq gnus-init-inhibit nil)
2166     (setq gnus-init-inhibit inhibit-next)
2167     (and gnus-init-file
2168          (or (and (file-exists-p gnus-init-file)
2169                   ;; Don't try to load a directory.
2170                   (not (file-directory-p gnus-init-file)))
2171              (file-exists-p (concat gnus-init-file ".el"))
2172              (file-exists-p (concat gnus-init-file ".elc")))
2173          (condition-case var
2174              (load gnus-init-file nil t)
2175            (error
2176             (error "Error in %s: %s" gnus-init-file var))))))
2177
2178 ;; Info access macros.
2179
2180 (defmacro gnus-info-group (info)
2181   `(nth 0 ,info))
2182 (defmacro gnus-info-rank (info)
2183   `(nth 1 ,info))
2184 (defmacro gnus-info-read (info)
2185   `(nth 2 ,info))
2186 (defmacro gnus-info-marks (info)
2187   `(nth 3 ,info))
2188 (defmacro gnus-info-method (info)
2189   `(nth 4 ,info))
2190 (defmacro gnus-info-params (info)
2191   `(nth 5 ,info))
2192
2193 (defmacro gnus-info-level (info)
2194   `(let ((rank (gnus-info-rank ,info)))
2195      (if (consp rank)
2196          (car rank)
2197        rank)))
2198 (defmacro gnus-info-score (info)
2199   `(let ((rank (gnus-info-rank ,info)))
2200      (or (and (consp rank) (cdr rank)) 0)))
2201
2202 (defmacro gnus-info-set-group (info group)
2203   `(setcar ,info ,group))
2204 (defmacro gnus-info-set-rank (info rank)
2205   `(setcar (nthcdr 1 ,info) ,rank))
2206 (defmacro gnus-info-set-read (info read)
2207   `(setcar (nthcdr 2 ,info) ,read))
2208 (defmacro gnus-info-set-marks (info marks)
2209   `(setcar (nthcdr 3 ,info) ,marks))
2210 (defmacro gnus-info-set-method (info method)
2211   `(setcar (nthcdr 4 ,info) ,method))
2212 (defmacro gnus-info-set-params (info params)
2213   `(setcar (nthcdr 5 ,info) ,params))
2214
2215 (defmacro gnus-info-set-level (info level)
2216   `(let ((rank (cdr ,info)))
2217      (if (consp (car rank))
2218          (setcar (car rank) ,level)
2219        (setcar rank ,level))))
2220 (defmacro gnus-info-set-score (info score)
2221   `(let ((rank (cdr ,info)))
2222      (if (consp (car rank))
2223          (setcdr (car rank) ,score)
2224        (setcar rank (cons (car rank) ,score)))))
2225
2226 (defmacro gnus-get-info (group)
2227   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2228
2229 (defun gnus-byte-code (func)
2230   "Return a form that can be `eval'ed based on FUNC."
2231   (let ((fval (symbol-function func)))
2232     (if (byte-code-function-p fval)
2233         (let ((flist (append fval nil)))
2234           (setcar flist 'byte-code)
2235           flist)
2236       (cons 'progn (cddr fval)))))
2237
2238 ;;; Load the compatability functions.
2239
2240 (require 'gnus-cus)
2241 (require 'gnus-ems)
2242
2243 \f
2244
2245 ;; Format specs.  The chunks below are the machine-generated forms
2246 ;; that are to be evaled as the result of the default format strings.
2247 ;; We write them in here to get them byte-compiled.  That way the
2248 ;; default actions will be quite fast, while still retaining the full
2249 ;; flexibility of the user-defined format specs.
2250
2251 ;; First we have lots of dummy defvars to let the compiler know these
2252 ;; are really dynamic variables.
2253
2254 (defvar gnus-tmp-unread)
2255 (defvar gnus-tmp-replied)
2256 (defvar gnus-tmp-score-char)
2257 (defvar gnus-tmp-indentation)
2258 (defvar gnus-tmp-opening-bracket)
2259 (defvar gnus-tmp-lines)
2260 (defvar gnus-tmp-name)
2261 (defvar gnus-tmp-closing-bracket)
2262 (defvar gnus-tmp-subject-or-nil)
2263 (defvar gnus-tmp-subject)
2264 (defvar gnus-tmp-marked)
2265 (defvar gnus-tmp-marked-mark)
2266 (defvar gnus-tmp-subscribed)
2267 (defvar gnus-tmp-process-marked)
2268 (defvar gnus-tmp-number-of-unread)
2269 (defvar gnus-tmp-group-name)
2270 (defvar gnus-tmp-group)
2271 (defvar gnus-tmp-article-number)
2272 (defvar gnus-tmp-unread-and-unselected)
2273 (defvar gnus-tmp-news-method)
2274 (defvar gnus-tmp-news-server)
2275 (defvar gnus-tmp-article-number)
2276 (defvar gnus-mouse-face)
2277 (defvar gnus-mouse-face-prop)
2278
2279 (defun gnus-summary-line-format-spec ()
2280   (insert gnus-tmp-unread gnus-tmp-replied
2281           gnus-tmp-score-char gnus-tmp-indentation)
2282   (put-text-property
2283    (point)
2284    (progn
2285      (insert
2286       gnus-tmp-opening-bracket
2287       (format "%4d: %-20s"
2288               gnus-tmp-lines
2289               (if (> (length gnus-tmp-name) 20)
2290                   (substring gnus-tmp-name 0 20)
2291                 gnus-tmp-name))
2292       gnus-tmp-closing-bracket)
2293      (point))
2294    gnus-mouse-face-prop gnus-mouse-face)
2295   (insert " " gnus-tmp-subject-or-nil "\n"))
2296
2297 (defvar gnus-summary-line-format-spec
2298   (gnus-byte-code 'gnus-summary-line-format-spec))
2299
2300 (defun gnus-summary-dummy-line-format-spec ()
2301   (insert "*  ")
2302   (put-text-property
2303    (point)
2304    (progn
2305      (insert ":                          :")
2306      (point))
2307    gnus-mouse-face-prop gnus-mouse-face)
2308   (insert " " gnus-tmp-subject "\n"))
2309
2310 (defvar gnus-summary-dummy-line-format-spec
2311   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2312
2313 (defun gnus-group-line-format-spec ()
2314   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2315           gnus-tmp-process-marked
2316           gnus-group-indentation
2317           (format "%5s: " gnus-tmp-number-of-unread))
2318   (put-text-property
2319    (point)
2320    (progn
2321      (insert gnus-tmp-group "\n")
2322      (1- (point)))
2323    gnus-mouse-face-prop gnus-mouse-face))
2324 (defvar gnus-group-line-format-spec
2325   (gnus-byte-code 'gnus-group-line-format-spec))
2326
2327 (defvar gnus-format-specs
2328   `((version . ,emacs-version)
2329     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2330     (summary-dummy ,gnus-summary-dummy-line-format
2331                    ,gnus-summary-dummy-line-format-spec)
2332     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2333
2334 (defvar gnus-article-mode-line-format-spec nil)
2335 (defvar gnus-summary-mode-line-format-spec nil)
2336 (defvar gnus-group-mode-line-format-spec nil)
2337
2338 ;;; Phew.  All that gruft is over, fortunately.
2339
2340 \f
2341 ;;;
2342 ;;; Gnus Utility Functions
2343 ;;;
2344
2345 (defun gnus-extract-address-components (from)
2346   (let (name address)
2347     ;; First find the address - the thing with the @ in it.  This may
2348     ;; not be accurate in mail addresses, but does the trick most of
2349     ;; the time in news messages.
2350     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2351         (setq address (substring from (match-beginning 0) (match-end 0))))
2352     ;; Then we check whether the "name <address>" format is used.
2353     (and address
2354          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2355          ;; Linear white space is not required.
2356          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2357          (and (setq name (substring from 0 (match-beginning 0)))
2358               ;; Strip any quotes from the name.
2359               (string-match "\".*\"" name)
2360               (setq name (substring name 1 (1- (match-end 0))))))
2361     ;; If not, then "address (name)" is used.
2362     (or name
2363         (and (string-match "(.+)" from)
2364              (setq name (substring from (1+ (match-beginning 0))
2365                                    (1- (match-end 0)))))
2366         (and (string-match "()" from)
2367              (setq name address))
2368         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2369         ;; XOVER might not support folded From headers.
2370         (and (string-match "(.*" from)
2371              (setq name (substring from (1+ (match-beginning 0))
2372                                    (match-end 0)))))
2373     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2374     (list (or name from) (or address from))))
2375
2376 (defun gnus-fetch-field (field)
2377   "Return the value of the header FIELD of current article."
2378   (save-excursion
2379     (save-restriction
2380       (let ((case-fold-search t))
2381         (nnheader-narrow-to-headers)
2382         (mail-fetch-field field)))))
2383
2384 (defun gnus-goto-colon ()
2385   (beginning-of-line)
2386   (search-forward ":" (gnus-point-at-eol) t))
2387
2388 ;;;###autoload
2389 (defun gnus-update-format (var)
2390   "Update the format specification near point."
2391   (interactive
2392    (list
2393     (save-excursion
2394       (eval-defun nil)
2395       ;; Find the end of the current word.
2396       (re-search-forward "[ \t\n]" nil t)
2397       ;; Search backward.
2398       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2399         (match-string 1)))))
2400   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2401                               (match-string 1 var))))
2402          (entry (assq type gnus-format-specs))
2403          value spec)
2404     (when entry
2405       (setq gnus-format-specs (delq entry gnus-format-specs)))
2406     (set
2407      (intern (format "%s-spec" var))
2408      (gnus-parse-format (setq value (symbol-value (intern var)))
2409                         (symbol-value (intern (format "%s-alist" var)))
2410                         (not (string-match "mode" var))))
2411     (setq spec (symbol-value (intern (format "%s-spec" var))))
2412     (push (list type value spec) gnus-format-specs)
2413
2414     (pop-to-buffer "*Gnus Format*")
2415     (erase-buffer)
2416     (lisp-interaction-mode)
2417     (insert (pp-to-string spec))))
2418
2419 (defun gnus-update-format-specifications (&optional force)
2420   "Update all (necessary) format specifications."
2421   ;; Make the indentation array.
2422   (gnus-make-thread-indent-array)
2423
2424   ;; See whether all the stored info needs to be flushed.
2425   (when (or force
2426             (not (equal emacs-version
2427                         (cdr (assq 'version gnus-format-specs)))))
2428     (setq gnus-format-specs nil))
2429
2430   ;; Go through all the formats and see whether they need updating.
2431   (let ((types '(summary summary-dummy group
2432                          summary-mode group-mode article-mode))
2433         new-format entry type val)
2434     (while (setq type (pop types))
2435       (setq new-format (symbol-value
2436                         (intern (format "gnus-%s-line-format" type))))
2437       (setq entry (cdr (assq type gnus-format-specs)))
2438       (if (and entry
2439                (equal (car entry) new-format))
2440           ;; Use the old format.
2441           (set (intern (format "gnus-%s-line-format-spec" type))
2442                (cadr entry))
2443         ;; This is a new format.
2444         (setq val
2445               (if (not (stringp new-format))
2446                   ;; This is a function call or something.
2447                   new-format
2448                 ;; This is a "real" format.
2449                 (gnus-parse-format
2450                  new-format
2451                  (symbol-value
2452                   (intern (format "gnus-%s-line-format-alist"
2453                                   (if (eq type 'article-mode)
2454                                       'summary-mode type))))
2455                  (not (string-match "mode$" (symbol-name type))))))
2456         ;; Enter the new format spec into the list.
2457         (if entry
2458             (progn
2459               (setcar (cdr entry) val)
2460               (setcar entry new-format))
2461           (push (list type new-format val) gnus-format-specs))
2462         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2463
2464   (gnus-update-group-mark-positions)
2465   (gnus-update-summary-mark-positions)
2466
2467   ;; See whether we need to read the description file.
2468   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2469            (not gnus-description-hashtb)
2470            gnus-read-active-file)
2471       (gnus-read-all-descriptions-files)))
2472
2473 (defun gnus-update-summary-mark-positions ()
2474   "Compute where the summary marks are to go."
2475   (save-excursion
2476     (let ((gnus-replied-mark 129)
2477           (gnus-score-below-mark 130)
2478           (gnus-score-over-mark 130)
2479           (thread nil)
2480           (gnus-visual nil)
2481           pos)
2482       (gnus-set-work-buffer)
2483       (gnus-summary-insert-line
2484        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2485       (goto-char (point-min))
2486       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2487                                          (- (point) 2)))))
2488       (goto-char (point-min))
2489       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2490             pos)
2491       (goto-char (point-min))
2492       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2493             pos)
2494       (setq gnus-summary-mark-positions pos))))
2495
2496 (defun gnus-update-group-mark-positions ()
2497   (save-excursion
2498     (let ((gnus-process-mark 128)
2499           (gnus-group-marked '("dummy.group")))
2500       (gnus-set-active "dummy.group" '(0 . 0))
2501       (gnus-set-work-buffer)
2502       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2503       (goto-char (point-min))
2504       (setq gnus-group-mark-positions
2505             (list (cons 'process (and (search-forward "\200" nil t)
2506                                       (- (point) 2))))))))
2507
2508 (defvar gnus-mouse-face-0 'highlight)
2509 (defvar gnus-mouse-face-1 'highlight)
2510 (defvar gnus-mouse-face-2 'highlight)
2511 (defvar gnus-mouse-face-3 'highlight)
2512 (defvar gnus-mouse-face-4 'highlight)
2513
2514 (defun gnus-mouse-face-function (form type)
2515   `(put-text-property
2516     (point) (progn ,@form (point))
2517     gnus-mouse-face-prop
2518     ,(if (equal type 0)
2519          'gnus-mouse-face
2520        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2521
2522 (defvar gnus-face-0 'bold)
2523 (defvar gnus-face-1 'italic)
2524 (defvar gnus-face-2 'bold-italic)
2525 (defvar gnus-face-3 'bold)
2526 (defvar gnus-face-4 'bold)
2527
2528 (defun gnus-face-face-function (form type)
2529   `(put-text-property
2530     (point) (progn ,@form (point))
2531     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2532
2533 (defun gnus-max-width-function (el max-width)
2534   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2535   (if (symbolp el)
2536       `(if (> (length ,el) ,max-width)
2537            (substring ,el 0 ,max-width)
2538          ,el)
2539     `(let ((val (eval ,el)))
2540        (if (numberp val)
2541            (setq val (int-to-string val)))
2542        (if (> (length val) ,max-width)
2543            (substring val 0 ,max-width)
2544          val))))
2545
2546 (defun gnus-parse-format (format spec-alist &optional insert)
2547   ;; This function parses the FORMAT string with the help of the
2548   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2549   ;; string.  If the FORMAT string contains the specifiers %( and %)
2550   ;; the text between them will have the mouse-face text property.
2551   (if (string-match
2552        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2553        format)
2554       (gnus-parse-complex-format format spec-alist)
2555     ;; This is a simple format.
2556     (gnus-parse-simple-format format spec-alist insert)))
2557
2558 (defun gnus-parse-complex-format (format spec-alist)
2559   (save-excursion
2560     (gnus-set-work-buffer)
2561     (insert format)
2562     (goto-char (point-min))
2563     (while (re-search-forward "\"" nil t)
2564       (replace-match "\\\"" nil t))
2565     (goto-char (point-min))
2566     (insert "(\"")
2567     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2568       (let ((number (if (match-beginning 1)
2569                         (match-string 1) "0"))
2570             (delim (aref (match-string 2) 0)))
2571         (if (or (= delim ?\() (= delim ?\{))
2572             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2573                                    " " number " \""))
2574           (replace-match "\")\""))))
2575     (goto-char (point-max))
2576     (insert "\")")
2577     (goto-char (point-min))
2578     (let ((form (read (current-buffer))))
2579       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2580
2581 (defun gnus-complex-form-to-spec (form spec-alist)
2582   (delq nil
2583         (mapcar
2584          (lambda (sform)
2585            (if (stringp sform)
2586                (gnus-parse-simple-format sform spec-alist t)
2587              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2588                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2589                       (nth 1 sform))))
2590          form)))
2591
2592 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2593   ;; This function parses the FORMAT string with the help of the
2594   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2595   ;; string.
2596   (let ((max-width 0)
2597         spec flist fstring newspec elem beg result dontinsert)
2598     (save-excursion
2599       (gnus-set-work-buffer)
2600       (insert format)
2601       (goto-char (point-min))
2602       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2603                                 nil t)
2604         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2605               (setq newspec "%"
2606                     beg (1+ (match-beginning 0)))
2607           ;; First check if there are any specs that look anything like
2608           ;; "%12,12A", ie. with a "max width specification".  These have
2609           ;; to be treated specially.
2610           (if (setq beg (match-beginning 1))
2611               (setq max-width
2612                     (string-to-int
2613                      (buffer-substring
2614                       (1+ (match-beginning 1)) (match-end 1))))
2615             (setq max-width 0)
2616             (setq beg (match-beginning 2)))
2617           ;; Find the specification from `spec-alist'.
2618           (unless (setq elem (cdr (assq spec spec-alist)))
2619             (setq elem '("*" ?s)))
2620           ;; Treat user defined format specifiers specially.
2621           (when (eq (car elem) 'gnus-tmp-user-defined)
2622             (setq elem
2623                   (list
2624                    (list (intern (concat "gnus-user-format-function-"
2625                                          (match-string 3)))
2626                          'gnus-tmp-header) ?s))
2627             (delete-region (match-beginning 3) (match-end 3)))
2628           (if (not (zerop max-width))
2629               (let ((el (car elem)))
2630                 (cond ((= (cadr elem) ?c)
2631                        (setq el (list 'char-to-string el)))
2632                       ((= (cadr elem) ?d)
2633                        (setq el (list 'int-to-string el))))
2634                 (setq flist (cons (gnus-max-width-function el max-width)
2635                                   flist))
2636                 (setq newspec ?s))
2637             (progn
2638               (setq flist (cons (car elem) flist))
2639               (setq newspec (cadr elem)))))
2640         ;; Remove the old specification (and possibly a ",12" string).
2641         (delete-region beg (match-end 2))
2642         ;; Insert the new specification.
2643         (goto-char beg)
2644         (insert newspec))
2645       (setq fstring (buffer-substring 1 (point-max))))
2646     ;; Do some postprocessing to increase efficiency.
2647     (setq
2648      result
2649      (cond
2650       ;; Emptyness.
2651       ((string= fstring "")
2652        nil)
2653       ;; Not a format string.
2654       ((not (string-match "%" fstring))
2655        (list fstring))
2656       ;; A format string with just a single string spec.
2657       ((string= fstring "%s")
2658        (list (car flist)))
2659       ;; A single character.
2660       ((string= fstring "%c")
2661        (list (car flist)))
2662       ;; A single number.
2663       ((string= fstring "%d")
2664        (setq dontinsert)
2665        (if insert
2666            (list `(princ ,(car flist)))
2667          (list `(int-to-string ,(car flist)))))
2668       ;; Just lots of chars and strings.
2669       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2670        (nreverse flist))
2671       ;; A single string spec at the beginning of the spec.
2672       ((string-match "\\`%[sc][^%]+\\'" fstring)
2673        (list (car flist) (substring fstring 2)))
2674       ;; A single string spec in the middle of the spec.
2675       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2676        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2677       ;; A single string spec in the end of the spec.
2678       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2679        (list (match-string 1 fstring) (car flist)))
2680       ;; A more complex spec.
2681       (t
2682        (list (cons 'format (cons fstring (nreverse flist)))))))
2683
2684     (if insert
2685         (when result
2686           (if dontinsert
2687               result
2688             (cons 'insert result)))
2689       (cond ((stringp result)
2690              result)
2691             ((consp result)
2692              (cons 'concat result))
2693             (t "")))))
2694
2695 (defun gnus-eval-format (format &optional alist props)
2696   "Eval the format variable FORMAT, using ALIST.
2697 If PROPS, insert the result."
2698   (let ((form (gnus-parse-format format alist props)))
2699     (if props
2700         (add-text-properties (point) (progn (eval form) (point)) props)
2701       (eval form))))
2702
2703 (defun gnus-remove-text-with-property (prop)
2704   "Delete all text in the current buffer with text property PROP."
2705   (save-excursion
2706     (goto-char (point-min))
2707     (while (not (eobp))
2708       (while (get-text-property (point) prop)
2709         (delete-char 1))
2710       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2711
2712 (defun gnus-set-work-buffer ()
2713   (if (get-buffer gnus-work-buffer)
2714       (progn
2715         (set-buffer gnus-work-buffer)
2716         (erase-buffer))
2717     (set-buffer (get-buffer-create gnus-work-buffer))
2718     (kill-all-local-variables)
2719     (buffer-disable-undo (current-buffer))
2720     (gnus-add-current-to-buffer-list)))
2721
2722 ;; Article file names when saving.
2723
2724 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2725   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2726 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2727 Otherwise, it is like ~/News/news/group/num."
2728   (let ((default
2729           (expand-file-name
2730            (concat (if (gnus-use-long-file-name 'not-save)
2731                        (gnus-capitalize-newsgroup newsgroup)
2732                      (gnus-newsgroup-directory-form newsgroup))
2733                    "/" (int-to-string (mail-header-number headers)))
2734            (or gnus-article-save-directory "~/News"))))
2735     (if (and last-file
2736              (string-equal (file-name-directory default)
2737                            (file-name-directory last-file))
2738              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2739         default
2740       (or last-file default))))
2741
2742 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2743   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2744 If variable `gnus-use-long-file-name' is non-nil, it is
2745 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2746   (let ((default
2747           (expand-file-name
2748            (concat (if (gnus-use-long-file-name 'not-save)
2749                        newsgroup
2750                      (gnus-newsgroup-directory-form newsgroup))
2751                    "/" (int-to-string (mail-header-number headers)))
2752            (or gnus-article-save-directory "~/News"))))
2753     (if (and last-file
2754              (string-equal (file-name-directory default)
2755                            (file-name-directory last-file))
2756              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2757         default
2758       (or last-file default))))
2759
2760 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2761   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2762 If variable `gnus-use-long-file-name' is non-nil, it is
2763 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2764   (or last-file
2765       (expand-file-name
2766        (if (gnus-use-long-file-name 'not-save)
2767            (gnus-capitalize-newsgroup newsgroup)
2768          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2769        (or gnus-article-save-directory "~/News"))))
2770
2771 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2772   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2773 If variable `gnus-use-long-file-name' is non-nil, it is
2774 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2775   (or last-file
2776       (expand-file-name
2777        (if (gnus-use-long-file-name 'not-save)
2778            newsgroup
2779          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2780        (or gnus-article-save-directory "~/News"))))
2781
2782 ;; For subscribing new newsgroup
2783
2784 (defun gnus-subscribe-hierarchical-interactive (groups)
2785   (let ((groups (sort groups 'string<))
2786         prefixes prefix start ans group starts)
2787     (while groups
2788       (setq prefixes (list "^"))
2789       (while (and groups prefixes)
2790         (while (not (string-match (car prefixes) (car groups)))
2791           (setq prefixes (cdr prefixes)))
2792         (setq prefix (car prefixes))
2793         (setq start (1- (length prefix)))
2794         (if (and (string-match "[^\\.]\\." (car groups) start)
2795                  (cdr groups)
2796                  (setq prefix
2797                        (concat "^" (substring (car groups) 0 (match-end 0))))
2798                  (string-match prefix (cadr groups)))
2799             (progn
2800               (setq prefixes (cons prefix prefixes))
2801               (message "Descend hierarchy %s? ([y]nsq): "
2802                        (substring prefix 1 (1- (length prefix))))
2803               (setq ans (read-char))
2804               (cond ((= ans ?n)
2805                      (while (and groups
2806                                  (string-match prefix
2807                                                (setq group (car groups))))
2808                        (setq gnus-killed-list
2809                              (cons group gnus-killed-list))
2810                        (gnus-sethash group group gnus-killed-hashtb)
2811                        (setq groups (cdr groups)))
2812                      (setq starts (cdr starts)))
2813                     ((= ans ?s)
2814                      (while (and groups
2815                                  (string-match prefix
2816                                                (setq group (car groups))))
2817                        (gnus-sethash group group gnus-killed-hashtb)
2818                        (gnus-subscribe-alphabetically (car groups))
2819                        (setq groups (cdr groups)))
2820                      (setq starts (cdr starts)))
2821                     ((= ans ?q)
2822                      (while groups
2823                        (setq group (car groups))
2824                        (setq gnus-killed-list (cons group gnus-killed-list))
2825                        (gnus-sethash group group gnus-killed-hashtb)
2826                        (setq groups (cdr groups))))
2827                     (t nil)))
2828           (message "Subscribe %s? ([n]yq)" (car groups))
2829           (setq ans (read-char))
2830           (setq group (car groups))
2831           (cond ((= ans ?y)
2832                  (gnus-subscribe-alphabetically (car groups))
2833                  (gnus-sethash group group gnus-killed-hashtb))
2834                 ((= ans ?q)
2835                  (while groups
2836                    (setq group (car groups))
2837                    (setq gnus-killed-list (cons group gnus-killed-list))
2838                    (gnus-sethash group group gnus-killed-hashtb)
2839                    (setq groups (cdr groups))))
2840                 (t
2841                  (setq gnus-killed-list (cons group gnus-killed-list))
2842                  (gnus-sethash group group gnus-killed-hashtb)))
2843           (setq groups (cdr groups)))))))
2844
2845 (defun gnus-subscribe-randomly (newsgroup)
2846   "Subscribe new NEWSGROUP by making it the first newsgroup."
2847   (gnus-subscribe-newsgroup newsgroup))
2848
2849 (defun gnus-subscribe-alphabetically (newgroup)
2850   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2851   (let ((groups (cdr gnus-newsrc-alist))
2852         before)
2853     (while (and (not before) groups)
2854       (if (string< newgroup (caar groups))
2855           (setq before (caar groups))
2856         (setq groups (cdr groups))))
2857     (gnus-subscribe-newsgroup newgroup before)))
2858
2859 (defun gnus-subscribe-hierarchically (newgroup)
2860   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2861   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2862   (save-excursion
2863     (set-buffer (find-file-noselect gnus-current-startup-file))
2864     (let ((groupkey newgroup)
2865           before)
2866       (while (and (not before) groupkey)
2867         (goto-char (point-min))
2868         (let ((groupkey-re
2869                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2870           (while (and (re-search-forward groupkey-re nil t)
2871                       (progn
2872                         (setq before (match-string 1))
2873                         (string< before newgroup)))))
2874         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2875         (setq groupkey
2876               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2877                   (substring groupkey (match-beginning 1) (match-end 1)))))
2878       (gnus-subscribe-newsgroup newgroup before))))
2879
2880 (defun gnus-subscribe-interactively (group)
2881   "Subscribe the new GROUP interactively.
2882 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2883 it is killed."
2884   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2885       (gnus-subscribe-hierarchically group)
2886     (push group gnus-killed-list)))
2887
2888 (defun gnus-subscribe-zombies (group)
2889   "Make the new GROUP into a zombie group."
2890   (push group gnus-zombie-list))
2891
2892 (defun gnus-subscribe-killed (group)
2893   "Make the new GROUP a killed group."
2894   (push group gnus-killed-list))
2895
2896 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2897   "Subscribe new NEWSGROUP.
2898 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2899 the first newsgroup."
2900   ;; We subscribe the group by changing its level to `subscribed'.
2901   (gnus-group-change-level
2902    newsgroup gnus-level-default-subscribed
2903    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2904   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2905
2906 ;; For directories
2907
2908 (defun gnus-newsgroup-directory-form (newsgroup)
2909   "Make hierarchical directory name from NEWSGROUP name."
2910   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2911         (len (length newsgroup))
2912         idx)
2913     ;; If this is a foreign group, we don't want to translate the
2914     ;; entire name.
2915     (if (setq idx (string-match ":" newsgroup))
2916         (aset newsgroup idx ?/)
2917       (setq idx 0))
2918     ;; Replace all occurrences of `.' with `/'.
2919     (while (< idx len)
2920       (if (= (aref newsgroup idx) ?.)
2921           (aset newsgroup idx ?/))
2922       (setq idx (1+ idx)))
2923     newsgroup))
2924
2925 (defun gnus-newsgroup-savable-name (group)
2926   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2927   ;; with dots.
2928   (nnheader-replace-chars-in-string group ?/ ?.))
2929
2930 (defun gnus-make-directory (dir)
2931   "Make DIRECTORY recursively."
2932   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2933   ;; of the many mysteries of the universe.
2934   (let* ((dir (expand-file-name dir default-directory))
2935          dirs err)
2936     (if (string-match "/$" dir)
2937         (setq dir (substring dir 0 (match-beginning 0))))
2938     ;; First go down the path until we find a directory that exists.
2939     (while (not (file-exists-p dir))
2940       (setq dirs (cons dir dirs))
2941       (string-match "/[^/]+$" dir)
2942       (setq dir (substring dir 0 (match-beginning 0))))
2943     ;; Then create all the subdirs.
2944     (while (and dirs (not err))
2945       (condition-case ()
2946           (make-directory (car dirs))
2947         (error (setq err t)))
2948       (setq dirs (cdr dirs)))
2949     ;; We return whether we were successful or not.
2950     (not dirs)))
2951
2952 (defun gnus-capitalize-newsgroup (newsgroup)
2953   "Capitalize NEWSGROUP name."
2954   (and (not (zerop (length newsgroup)))
2955        (concat (char-to-string (upcase (aref newsgroup 0)))
2956                (substring newsgroup 1))))
2957
2958 ;; Various... things.
2959
2960 (defun gnus-simplify-subject (subject &optional re-only)
2961   "Remove `Re:' and words in parentheses.
2962 If RE-ONLY is non-nil, strip leading `Re:'s only."
2963   (let ((case-fold-search t))           ;Ignore case.
2964     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
2965     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
2966       (setq subject (substring subject (match-end 0))))
2967     ;; Remove uninteresting prefixes.
2968     (if (and (not re-only)
2969              gnus-simplify-ignored-prefixes
2970              (string-match gnus-simplify-ignored-prefixes subject))
2971         (setq subject (substring subject (match-end 0))))
2972     ;; Remove words in parentheses from end.
2973     (unless re-only
2974       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2975         (setq subject (substring subject 0 (match-beginning 0)))))
2976     ;; Return subject string.
2977     subject))
2978
2979 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2980 ;; all whitespace.
2981 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2982 (defun gnus-simplify-buffer-fuzzy ()
2983   (goto-char (point-min))
2984   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
2985   (goto-char (match-beginning 0))
2986   (while (or
2987           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
2988           (looking-at "^[[].*:[ \t].*[]]$"))
2989     (goto-char (point-min))
2990     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2991                               nil t)
2992       (replace-match "" t t))
2993     (goto-char (point-min))
2994     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
2995       (goto-char (match-end 0))
2996       (delete-char -1)
2997       (delete-region
2998        (progn (goto-char (match-beginning 0)))
2999        (re-search-forward ":"))))
3000   (goto-char (point-min))
3001   (while (re-search-forward "[ \t\n]*[[{(][^()]*[]})][ \t]*$" nil t)
3002     (replace-match "" t t))
3003   (goto-char (point-min))
3004   (while (re-search-forward "[ \t]+" nil t)
3005     (replace-match " " t t))
3006   (goto-char (point-min))
3007   (while (re-search-forward "[ \t]+$" nil t)
3008     (replace-match "" t t))
3009   (goto-char (point-min))
3010   (while (re-search-forward "^[ \t]+" nil t)
3011     (replace-match "" t t))
3012   (goto-char (point-min))
3013   (if gnus-simplify-subject-fuzzy-regexp
3014       (if (listp gnus-simplify-subject-fuzzy-regexp)
3015           (let ((list gnus-simplify-subject-fuzzy-regexp))
3016             (while list
3017               (goto-char (point-min))
3018               (while (re-search-forward (car list) nil t)
3019                 (replace-match "" t t))
3020               (setq list (cdr list))))
3021         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3022           (replace-match "" t t)))))
3023
3024 (defun gnus-simplify-subject-fuzzy (subject)
3025   "Siplify a subject string fuzzily."
3026   (save-excursion
3027     (gnus-set-work-buffer)
3028     (let ((case-fold-search t))
3029       (insert subject)
3030       (inline (gnus-simplify-buffer-fuzzy))
3031       (buffer-string))))
3032
3033 ;; Add the current buffer to the list of buffers to be killed on exit.
3034 (defun gnus-add-current-to-buffer-list ()
3035   (or (memq (current-buffer) gnus-buffer-list)
3036       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3037
3038 (defun gnus-string> (s1 s2)
3039   (not (or (string< s1 s2)
3040            (string= s1 s2))))
3041
3042 ;;; General various misc type functions.
3043
3044 (defun gnus-clear-system ()
3045   "Clear all variables and buffers."
3046   ;; Clear Gnus variables.
3047   (let ((variables gnus-variable-list))
3048     (while variables
3049       (set (car variables) nil)
3050       (setq variables (cdr variables))))
3051   ;; Clear other internal variables.
3052   (setq gnus-list-of-killed-groups nil
3053         gnus-have-read-active-file nil
3054         gnus-newsrc-alist nil
3055         gnus-newsrc-hashtb nil
3056         gnus-killed-list nil
3057         gnus-zombie-list nil
3058         gnus-killed-hashtb nil
3059         gnus-active-hashtb nil
3060         gnus-moderated-list nil
3061         gnus-description-hashtb nil
3062         gnus-newsgroup-headers nil
3063         gnus-newsgroup-name nil
3064         gnus-server-alist nil
3065         gnus-opened-servers nil
3066         gnus-current-select-method nil)
3067   ;; Reset any score variables.
3068   (and gnus-use-scoring (gnus-score-close))
3069   ;; Kill the startup file.
3070   (and gnus-current-startup-file
3071        (get-file-buffer gnus-current-startup-file)
3072        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3073   ;; Save any cache buffers.
3074   (and gnus-use-cache (gnus-cache-save-buffers))
3075   ;; Clear the dribble buffer.
3076   (gnus-dribble-clear)
3077   ;; Close down NoCeM.
3078   (when gnus-use-nocem 
3079     (gnus-nocem-close))
3080   ;; Shut down the demons.
3081   (when gnus-use-demon
3082     (gnus-demon-cancel))
3083   ;; Kill global KILL file buffer.
3084   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
3085       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3086   (gnus-kill-buffer nntp-server-buffer)
3087   ;; Backlog.
3088   (and gnus-keep-backlog (gnus-backlog-shutdown))
3089   ;; Kill Gnus buffers.
3090   (while gnus-buffer-list
3091     (gnus-kill-buffer (car gnus-buffer-list))
3092     (setq gnus-buffer-list (cdr gnus-buffer-list))))
3093
3094 (defun gnus-windows-old-to-new (setting)
3095   ;; First we take care of the really, really old Gnus 3 actions.
3096   (if (symbolp setting)
3097       (setq setting
3098             (cond ((memq setting '(SelectArticle))
3099                    'article)
3100                   ((memq setting '(SelectSubject ExpandSubject))
3101                    'summary)
3102                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
3103                    'group)
3104                   (t setting))))
3105   (if (or (listp setting)
3106           (not (and gnus-window-configuration
3107                     (memq setting '(group summary article)))))
3108       setting
3109     (let* ((setting (if (eq setting 'group)
3110                         (if (assq 'newsgroup gnus-window-configuration)
3111                             'newsgroup
3112                           'newsgroups) setting))
3113            (elem (cadr (assq setting gnus-window-configuration)))
3114            (total (apply '+ elem))
3115            (types '(group summary article))
3116            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3117            (i 0)
3118            perc
3119            out)
3120       (while (< i 3)
3121         (or (not (numberp (nth i elem)))
3122             (zerop (nth i elem))
3123             (progn
3124               (setq perc  (/ (float (nth 0 elem)) total))
3125               (setq out (cons (if (eq pbuf (nth i types))
3126                                   (vector (nth i types) perc 'point)
3127                                 (vector (nth i types) perc))
3128                               out))))
3129         (setq i (1+ i)))
3130       (list (nreverse out)))))
3131
3132 ;;;###autoload
3133 (defun gnus-add-configuration (conf)
3134   "Add the window configuration CONF to `gnus-buffer-configuration'."
3135   (setq gnus-buffer-configuration
3136         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3137                          gnus-buffer-configuration))))
3138
3139 (defvar gnus-frame-list nil)
3140
3141 (defun gnus-configure-frame (split &optional window)
3142   "Split WINDOW according to SPLIT."
3143   (unless window
3144     (setq window (get-buffer-window (current-buffer))))
3145   (select-window window)
3146   ;; This might be an old-stylee buffer config.
3147   (when (vectorp split)
3148     (setq split (append split nil)))
3149   (when (or (consp (car split))
3150             (vectorp (car split)))
3151     (push 1.0 split)
3152     (push 'vertical split))
3153   ;; The SPLIT might be something that is to be evaled to
3154   ;; return a new SPLIT.
3155   (while (and (not (assq (car split) gnus-window-to-buffer))
3156               (gnus-functionp (car split)))
3157     (setq split (eval split)))
3158   (let* ((type (car split))
3159          (subs (cddr split))
3160          (len (if (eq type 'horizontal) (window-width) (window-height)))
3161          (total 0)
3162          (window-min-width (or gnus-window-min-width window-min-width))
3163          (window-min-height (or gnus-window-min-height window-min-height))
3164          s result new-win rest comp-subs size sub)
3165     (cond
3166      ;; Nothing to do here.
3167      ((null split))
3168      ;; Don't switch buffers.
3169      ((null type)
3170       (and (memq 'point split) window))
3171      ;; This is a buffer to be selected.
3172      ((not (memq type '(frame horizontal vertical)))
3173       (let ((buffer (cond ((stringp type) type)
3174                           (t (cdr (assq type gnus-window-to-buffer)))))
3175             buf)
3176         (unless buffer
3177           (error "Illegal buffer type: %s" type))
3178         (unless (setq buf (get-buffer (if (symbolp buffer)
3179                                           (symbol-value buffer) buffer)))
3180           (setq buf (get-buffer-create (if (symbolp buffer)
3181                                            (symbol-value buffer) buffer))))
3182         (switch-to-buffer buf)
3183         ;; We return the window if it has the `point' spec.
3184         (and (memq 'point split) window)))
3185      ;; This is a frame split.
3186      ((eq type 'frame)
3187       (unless gnus-frame-list
3188         (setq gnus-frame-list (list (window-frame
3189                                      (get-buffer-window (current-buffer))))))
3190       (let ((i 0)
3191             params frame fresult)
3192         (while (< i (length subs))
3193           ;; Frame parameter is gotten from the sub-split.
3194           (setq params (cadr (elt subs i)))
3195           ;; It should be a list.
3196           (unless (listp params)
3197             (setq params nil))
3198           ;; Create a new frame?
3199           (unless (setq frame (elt gnus-frame-list i))
3200             (nconc gnus-frame-list (list (setq frame (make-frame params)))))
3201           ;; Is the old frame still alive?
3202           (unless (frame-live-p frame)
3203             (setcar (nthcdr i gnus-frame-list)
3204                     (setq frame (make-frame params))))
3205           ;; Select the frame in question and do more splits there.
3206           (select-frame frame)
3207           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3208           (incf i))
3209         ;; Select the frame that has the selected buffer.
3210         (when fresult
3211           (select-frame (window-frame fresult)))))
3212      ;; This is a normal split.
3213      (t
3214       (when (> (length subs) 0)
3215         ;; First we have to compute the sizes of all new windows.
3216         (while subs
3217           (setq sub (append (pop subs) nil))
3218           (while (and (not (assq (car sub) gnus-window-to-buffer))
3219                       (gnus-functionp (car sub)))
3220             (setq sub (eval sub)))
3221           (when sub
3222             (push sub comp-subs)
3223             (setq size (cadar comp-subs))
3224             (cond ((equal size 1.0)
3225                    (setq rest (car comp-subs))
3226                    (setq s 0))
3227                   ((floatp size)
3228                    (setq s (floor (* size len))))
3229                   ((integerp size)
3230                    (setq s size))
3231                   (t
3232                    (error "Illegal size: %s" size)))
3233             ;; Try to make sure that we are inside the safe limits.
3234             (cond ((zerop s))
3235                   ((eq type 'horizontal)
3236                    (setq s (max s window-min-width)))
3237                   ((eq type 'vertical)
3238                    (setq s (max s window-min-height))))
3239             (setcar (cdar comp-subs) s)
3240             (incf total s)))
3241         ;; Take care of the "1.0" spec.
3242         (if rest
3243             (setcar (cdr rest) (- len total))
3244           (error "No 1.0 specs in %s" split))
3245         ;; The we do the actual splitting in a nice recursive
3246         ;; fashion.
3247         (setq comp-subs (nreverse comp-subs))
3248         (while comp-subs
3249           (if (null (cdr comp-subs))
3250               (setq new-win window)
3251             (setq new-win
3252                   (split-window window (cadar comp-subs)
3253                                 (eq type 'horizontal))))
3254           (setq result (or (gnus-configure-frame
3255                             (car comp-subs) window) result))
3256           (select-window new-win)
3257           (setq window new-win)
3258           (setq comp-subs (cdr comp-subs))))
3259       ;; Return the proper window, if any.
3260       (when result
3261         (select-window result))))))
3262
3263 (defvar gnus-frame-split-p nil)
3264
3265 (defun gnus-configure-windows (setting &optional force)
3266   (setq setting (gnus-windows-old-to-new setting))
3267   (let ((split (if (symbolp setting)
3268                    (cadr (assq setting gnus-buffer-configuration))
3269                  setting))
3270         (in-buf (current-buffer))
3271         rule val w height hor ohor heights sub jump-buffer
3272         rel total to-buf all-visible)
3273
3274     (setq gnus-frame-split-p nil)
3275
3276     (unless split
3277       (error "No such setting: %s" setting))
3278
3279     (if (and (not force)
3280              (setq all-visible (gnus-all-windows-visible-p split)))
3281         ;; All the windows mentioned are already visible, so we just
3282         ;; put point in the assigned buffer, and do not touch the
3283         ;; winconf.
3284         (select-window all-visible)
3285
3286       ;; Either remove all windows or just remove all Gnus windows.
3287       (let ((frame (selected-frame)))
3288         (unwind-protect
3289             (if gnus-use-full-window
3290                 ;; We want to remove all other windows.
3291                 (if (not gnus-frame-split-p)
3292                     ;; This is not a `frame' split, so we ignore the
3293                     ;; other frames.  
3294                     (delete-other-windows)
3295                   ;; This is a `frame' split, so we delete all windows
3296                   ;; on all frames.
3297                   (mapcar 
3298                    (lambda (frame)
3299                      (unless (eq (cdr (assq 'minibuffer
3300                                             (frame-parameters frame)))
3301                                  'only)
3302                        (select-frame frame)
3303                        (delete-other-windows)))
3304                    (frame-list)))
3305               ;; Just remove some windows.
3306               (gnus-remove-some-windows)
3307               (switch-to-buffer nntp-server-buffer))
3308           (select-frame frame)))
3309
3310       (switch-to-buffer nntp-server-buffer)
3311       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3312
3313 (defun gnus-all-windows-visible-p (split)
3314   (when (vectorp split)
3315     (setq split (append split nil)))
3316   (when (or (consp (car split))
3317             (vectorp (car split)))
3318     (push 1.0 split)
3319     (push 'vertical split))
3320   ;; The SPLIT might be something that is to be evaled to
3321   ;; return a new SPLIT.
3322   (while (and (not (assq (car split) gnus-window-to-buffer))
3323               (gnus-functionp (car split)))
3324     (setq split (eval split)))
3325   (let* ((type (elt split 0)))
3326     (cond
3327      ((null split)
3328       t)
3329      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3330       (let ((buffer (cond ((stringp type) type)
3331                           (t (cdr (assq type gnus-window-to-buffer)))))
3332             win buf)
3333         (unless buffer
3334           (error "Illegal buffer type: %s" type))
3335         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3336                                       buffer)))
3337           (setq win (get-buffer-window buf t)))
3338         (when win
3339           (if (memq 'point split)
3340               win
3341             t))))
3342      (t
3343       (when (eq type 'frame)
3344         (setq gnus-frame-split-p t))
3345       (let ((n (mapcar 'gnus-all-windows-visible-p
3346                        (cddr split)))
3347             (win t))
3348         (while n
3349           (cond ((windowp (car n))
3350                  (setq win (car n)))
3351                 ((null (car n))
3352                  (setq win nil)))
3353           (setq n (cdr n)))
3354         win)))))
3355
3356 (defun gnus-window-top-edge (&optional window)
3357   (nth 1 (window-edges window)))
3358
3359 (defun gnus-remove-some-windows ()
3360   (let ((buffers gnus-window-to-buffer)
3361         buf bufs lowest-buf lowest)
3362     (save-excursion
3363       ;; Remove windows on all known Gnus buffers.
3364       (while buffers
3365         (setq buf (cdar buffers))
3366         (if (symbolp buf)
3367             (setq buf (and (boundp buf) (symbol-value buf))))
3368         (and buf
3369              (get-buffer-window buf)
3370              (progn
3371                (setq bufs (cons buf bufs))
3372                (pop-to-buffer buf)
3373                (if (or (not lowest)
3374                        (< (gnus-window-top-edge) lowest))
3375                    (progn
3376                      (setq lowest (gnus-window-top-edge))
3377                      (setq lowest-buf buf)))))
3378         (setq buffers (cdr buffers)))
3379       ;; Remove windows on *all* summary buffers.
3380       (let (wins)
3381         (walk-windows
3382          (lambda (win)
3383            (let ((buf (window-buffer win)))
3384              (if (string-match  "^\\*Summary" (buffer-name buf))
3385                  (progn
3386                    (setq bufs (cons buf bufs))
3387                    (pop-to-buffer buf)
3388                    (if (or (not lowest)
3389                            (< (gnus-window-top-edge) lowest))
3390                        (progn
3391                          (setq lowest-buf buf)
3392                          (setq lowest (gnus-window-top-edge))))))))))
3393       (and lowest-buf
3394            (progn
3395              (pop-to-buffer lowest-buf)
3396              (switch-to-buffer nntp-server-buffer)))
3397       (while bufs
3398         (and (not (eq (car bufs) lowest-buf))
3399              (delete-windows-on (car bufs)))
3400         (setq bufs (cdr bufs))))))
3401
3402 (defun gnus-version ()
3403   "Version numbers of this version of Gnus."
3404   (interactive)
3405   (let ((methods gnus-valid-select-methods)
3406         (mess gnus-version)
3407         meth)
3408     ;; Go through all the legal select methods and add their version
3409     ;; numbers to the total version string.  Only the backends that are
3410     ;; currently in use will have their message numbers taken into
3411     ;; consideration.
3412     (while methods
3413       (setq meth (intern (concat (caar methods) "-version")))
3414       (and (boundp meth)
3415            (stringp (symbol-value meth))
3416            (setq mess (concat mess "; " (symbol-value meth))))
3417       (setq methods (cdr methods)))
3418     (gnus-message 2 mess)))
3419
3420 (defun gnus-info-find-node ()
3421   "Find Info documentation of Gnus."
3422   (interactive)
3423   ;; Enlarge info window if needed.
3424   (let ((mode major-mode)
3425         gnus-info-buffer)
3426     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3427     (setq gnus-info-buffer (current-buffer))
3428     (gnus-configure-windows 'info)))
3429
3430 (defun gnus-days-between (date1 date2)
3431   ;; Return the number of days between date1 and date2.
3432   (- (gnus-day-number date1) (gnus-day-number date2)))
3433
3434 (defun gnus-day-number (date)
3435   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3436                      (timezone-parse-date date))))
3437     (timezone-absolute-from-gregorian
3438      (nth 1 dat) (nth 2 dat) (car dat))))
3439
3440 (defun gnus-encode-date (date)
3441   "Convert DATE to internal time."
3442   (let* ((parse (timezone-parse-date date))
3443          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3444          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3445     (encode-time (caddr time) (cadr time) (car time)
3446                  (caddr date) (cadr date) (car date) (nth 4 date))))
3447
3448 (defun gnus-time-minus (t1 t2)
3449   "Subtract two internal times."
3450   (let ((borrow (< (cadr t1) (cadr t2))))
3451     (list (- (car t1) (car t2) (if borrow 1 0))
3452           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3453
3454 (defun gnus-file-newer-than (file date)
3455   (let ((fdate (nth 5 (file-attributes file))))
3456     (or (> (car fdate) (car date))
3457         (and (= (car fdate) (car date))
3458              (> (nth 1 fdate) (nth 1 date))))))
3459
3460 (defmacro gnus-define-keys (keymap &rest plist)
3461   "Define all keys in PLIST in KEYMAP."
3462   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3463
3464 (defmacro gnus-define-keymap (keymap &rest plist)
3465   "Define all keys in PLIST in KEYMAP."
3466   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3467
3468 (defun gnus-define-keys-1 (keymap plist)
3469   (when (null keymap)
3470     (error "Can't set keys in a null keymap"))
3471   (cond ((symbolp keymap)
3472          (setq keymap (symbol-value keymap)))
3473         ((keymapp keymap))
3474         ((listp keymap)
3475          (set (car keymap) nil)
3476          (define-prefix-command (car keymap))
3477          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3478          (setq keymap (symbol-value (car keymap)))))
3479   (let (key)
3480     (while plist
3481       (when (symbolp (setq key (pop plist)))
3482         (setq key (symbol-value key)))
3483       (define-key keymap key (pop plist)))))
3484
3485 (defun gnus-group-read-only-p (&optional group)
3486   "Check whether GROUP supports editing or not.
3487 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3488 that that variable is buffer-local to the summary buffers."
3489   (let ((group (or group gnus-newsgroup-name)))
3490     (not (gnus-check-backend-function 'request-replace-article group))))
3491
3492 (defun gnus-group-total-expirable-p (group)
3493   "Check whether GROUP is total-expirable or not."
3494   (let ((params (gnus-info-params (gnus-get-info group))))
3495     (or (memq 'total-expire params)
3496         (cdr (assq 'total-expire params)) ; (total-expire . t)
3497         (and gnus-total-expirable-newsgroups ; Check var.
3498              (string-match gnus-total-expirable-newsgroups group)))))
3499
3500 (defun gnus-group-auto-expirable-p (group)
3501   "Check whether GROUP is total-expirable or not."
3502   (let ((params (gnus-info-params (gnus-get-info group))))
3503     (or (memq 'auto-expire params)
3504         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3505         (and gnus-auto-expirable-newsgroups ; Check var.
3506              (string-match gnus-auto-expirable-newsgroups group)))))
3507
3508 (defun gnus-virtual-group-p (group)
3509   "Say whether GROUP is virtual or not."
3510   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3511                         gnus-valid-select-methods)))
3512
3513 (defsubst gnus-simplify-subject-fully (subject)
3514   "Simplify a subject string according to the user's wishes."
3515   (cond
3516    ((null gnus-summary-gather-subject-limit)
3517     (gnus-simplify-subject-re subject))
3518    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3519     (gnus-simplify-subject-fuzzy subject))
3520    ((numberp gnus-summary-gather-subject-limit)
3521     (gnus-limit-string (gnus-simplify-subject-re subject)
3522                        gnus-summary-gather-subject-limit))
3523    (t
3524     subject)))
3525
3526 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3527   "Check whether two subjects are equal.  If optional argument
3528 simple-first is t, first argument is already simplified."
3529   (cond
3530    ((null simple-first)
3531     (equal (gnus-simplify-subject-fully s1)
3532            (gnus-simplify-subject-fully s2)))
3533    (t
3534     (equal s1
3535            (gnus-simplify-subject-fully s2)))))
3536
3537 ;; Returns a list of writable groups.
3538 (defun gnus-writable-groups ()
3539   (let ((alist gnus-newsrc-alist)
3540         groups group)
3541     (while (setq group (car (pop alist)))
3542       (unless (gnus-group-read-only-p group)
3543         (push group groups)))
3544     (nreverse groups)))
3545
3546 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3547 ;; the echo area.
3548 (defun gnus-y-or-n-p (prompt)
3549   (prog1
3550       (y-or-n-p prompt)
3551     (message "")))
3552
3553 (defun gnus-yes-or-no-p (prompt)
3554   (prog1
3555       (yes-or-no-p prompt)
3556     (message "")))
3557
3558 ;; Check whether to use long file names.
3559 (defun gnus-use-long-file-name (symbol)
3560   ;; The variable has to be set...
3561   (and gnus-use-long-file-name
3562        ;; If it isn't a list, then we return t.
3563        (or (not (listp gnus-use-long-file-name))
3564            ;; If it is a list, and the list contains `symbol', we
3565            ;; return nil.
3566            (not (memq symbol gnus-use-long-file-name)))))
3567
3568 ;; I suspect there's a better way, but I haven't taken the time to do
3569 ;; it yet. -erik selberg@cs.washington.edu
3570 (defun gnus-dd-mmm (messy-date)
3571   "Return a string like DD-MMM from a big messy string"
3572   (let ((datevec (timezone-parse-date messy-date)))
3573     (format "%2s-%s"
3574             (or (aref datevec 2) "??")
3575             (capitalize
3576              (or (car
3577                   (nth (1- (string-to-number (aref datevec 1)))
3578                        timezone-months-assoc))
3579                  "???")))))
3580
3581 ;; Make a hash table (default and minimum size is 255).
3582 ;; Optional argument HASHSIZE specifies the table size.
3583 (defun gnus-make-hashtable (&optional hashsize)
3584   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3585
3586 ;; Make a number that is suitable for hashing; bigger than MIN and one
3587 ;; less than 2^x.
3588 (defun gnus-create-hash-size (min)
3589   (let ((i 1))
3590     (while (< i min)
3591       (setq i (* 2 i)))
3592     (1- i)))
3593
3594 ;; Show message if message has a lower level than `gnus-verbose'.
3595 ;; Guideline for numbers:
3596 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3597 ;; for things that take a long time, 7 - not very important messages
3598 ;; on stuff, 9 - messages inside loops.
3599 (defun gnus-message (level &rest args)
3600   (if (<= level gnus-verbose)
3601       (apply 'message args)
3602     ;; We have to do this format thingy here even if the result isn't
3603     ;; shown - the return value has to be the same as the return value
3604     ;; from `message'.
3605     (apply 'format args)))
3606
3607 (defun gnus-functionp (form)
3608   "Return non-nil if FORM is funcallable."
3609   (or (and (symbolp form) (fboundp form))
3610       (and (listp form) (eq (car form) 'lambda))))
3611
3612 ;; Generate a unique new group name.
3613 (defun gnus-generate-new-group-name (leaf)
3614   (let ((name leaf)
3615         (num 0))
3616     (while (gnus-gethash name gnus-newsrc-hashtb)
3617       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3618     name))
3619
3620 ;; Find out whether the gnus-visual TYPE is wanted.
3621 (defun gnus-visual-p (&optional type class)
3622   (and gnus-visual                      ; Has to be non-nil, at least.
3623        (if (not type)                   ; We don't care about type.
3624            gnus-visual
3625          (if (listp gnus-visual)        ; It's a list, so we check it.
3626              (or (memq type gnus-visual)
3627                  (memq class gnus-visual))
3628            t))))
3629
3630 (defun gnus-parent-id (references)
3631   "Return the last Message-ID in REFERENCES."
3632   (when (and references
3633              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3634     (substring references (match-beginning 1) (match-end 1))))
3635
3636 (defun gnus-split-references (references)
3637   "Return a list of Message-IDs in REFERENCES."
3638   (let ((beg 0)
3639         ids)
3640     (while (string-match "<[^>]+>" references beg)
3641       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3642             ids))
3643     (nreverse ids)))
3644
3645 (defun gnus-ephemeral-group-p (group)
3646   "Say whether GROUP is ephemeral or not."
3647   (gnus-group-get-parameter group 'quit-config))
3648
3649 (defun gnus-group-quit-config (group)
3650   "Return the quit-config of GROUP."
3651   (gnus-group-get-parameter group 'quit-config))
3652
3653 (defun gnus-simplify-mode-line ()
3654   "Make mode lines a bit simpler."
3655   (setq mode-line-modified "-- ")
3656   (when (listp mode-line-format)
3657     (make-local-variable 'mode-line-format)
3658     (setq mode-line-format (copy-sequence mode-line-format))
3659     (when (equal (nth 3 mode-line-format) "   ")
3660       (setcar (nthcdr 3 mode-line-format) " "))))
3661
3662 ;;; List and range functions
3663
3664 (defun gnus-last-element (list)
3665   "Return last element of LIST."
3666   (while (cdr list)
3667     (setq list (cdr list)))
3668   (car list))
3669
3670 (defun gnus-copy-sequence (list)
3671   "Do a complete, total copy of a list."
3672   (if (and (consp list) (not (consp (cdr list))))
3673       (cons (car list) (cdr list))
3674     (mapcar (lambda (elem) (if (consp elem)
3675                                (if (consp (cdr elem))
3676                                    (gnus-copy-sequence elem)
3677                                  (cons (car elem) (cdr elem)))
3678                              elem))
3679             list)))
3680
3681 (defun gnus-set-difference (list1 list2)
3682   "Return a list of elements of LIST1 that do not appear in LIST2."
3683   (let ((list1 (copy-sequence list1)))
3684     (while list2
3685       (setq list1 (delq (car list2) list1))
3686       (setq list2 (cdr list2)))
3687     list1))
3688
3689 (defun gnus-sorted-complement (list1 list2)
3690   "Return a list of elements of LIST1 that do not appear in LIST2.
3691 Both lists have to be sorted over <."
3692   (let (out)
3693     (if (or (null list1) (null list2))
3694         (or list1 list2)
3695       (while (and list1 list2)
3696         (cond ((= (car list1) (car list2))
3697                (setq list1 (cdr list1)
3698                      list2 (cdr list2)))
3699               ((< (car list1) (car list2))
3700                (setq out (cons (car list1) out))
3701                (setq list1 (cdr list1)))
3702               (t
3703                (setq out (cons (car list2) out))
3704                (setq list2 (cdr list2)))))
3705       (nconc (nreverse out) (or list1 list2)))))
3706
3707 (defun gnus-intersection (list1 list2)
3708   (let ((result nil))
3709     (while list2
3710       (if (memq (car list2) list1)
3711           (setq result (cons (car list2) result)))
3712       (setq list2 (cdr list2)))
3713     result))
3714
3715 (defun gnus-sorted-intersection (list1 list2)
3716   ;; LIST1 and LIST2 have to be sorted over <.
3717   (let (out)
3718     (while (and list1 list2)
3719       (cond ((= (car list1) (car list2))
3720              (setq out (cons (car list1) out)
3721                    list1 (cdr list1)
3722                    list2 (cdr list2)))
3723             ((< (car list1) (car list2))
3724              (setq list1 (cdr list1)))
3725             (t
3726              (setq list2 (cdr list2)))))
3727     (nreverse out)))
3728
3729 (defun gnus-set-sorted-intersection (list1 list2)
3730   ;; LIST1 and LIST2 have to be sorted over <.
3731   ;; This function modifies LIST1.
3732   (let* ((top (cons nil list1))
3733          (prev top))
3734     (while (and list1 list2)
3735       (cond ((= (car list1) (car list2))
3736              (setq prev list1
3737                    list1 (cdr list1)
3738                    list2 (cdr list2)))
3739             ((< (car list1) (car list2))
3740              (setcdr prev (cdr list1))
3741              (setq list1 (cdr list1)))
3742             (t
3743              (setq list2 (cdr list2)))))
3744     (setcdr prev nil)
3745     (cdr top)))
3746
3747 (defun gnus-compress-sequence (numbers &optional always-list)
3748   "Convert list of numbers to a list of ranges or a single range.
3749 If ALWAYS-LIST is non-nil, this function will always release a list of
3750 ranges."
3751   (let* ((first (car numbers))
3752          (last (car numbers))
3753          result)
3754     (if (null numbers)
3755         nil
3756       (if (not (listp (cdr numbers)))
3757           numbers
3758         (while numbers
3759           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3760                 ((= (1+ last) (car numbers)) ;Still in sequence
3761                  (setq last (car numbers)))
3762                 (t                      ;End of one sequence
3763                  (setq result
3764                        (cons (if (= first last) first
3765                                (cons first last)) result))
3766                  (setq first (car numbers))
3767                  (setq last  (car numbers))))
3768           (setq numbers (cdr numbers)))
3769         (if (and (not always-list) (null result))
3770             (if (= first last) (list first) (cons first last))
3771           (nreverse (cons (if (= first last) first (cons first last))
3772                           result)))))))
3773
3774 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3775 (defun gnus-uncompress-range (ranges)
3776   "Expand a list of ranges into a list of numbers.
3777 RANGES is either a single range on the form `(num . num)' or a list of
3778 these ranges."
3779   (let (first last result)
3780     (cond
3781      ((null ranges)
3782       nil)
3783      ((not (listp (cdr ranges)))
3784       (setq first (car ranges))
3785       (setq last (cdr ranges))
3786       (while (<= first last)
3787         (setq result (cons first result))
3788         (setq first (1+ first)))
3789       (nreverse result))
3790      (t
3791       (while ranges
3792         (if (atom (car ranges))
3793             (if (numberp (car ranges))
3794                 (setq result (cons (car ranges) result)))
3795           (setq first (caar ranges))
3796           (setq last  (cdar ranges))
3797           (while (<= first last)
3798             (setq result (cons first result))
3799             (setq first (1+ first))))
3800         (setq ranges (cdr ranges)))
3801       (nreverse result)))))
3802
3803 (defun gnus-add-to-range (ranges list)
3804   "Return a list of ranges that has all articles from both RANGES and LIST.
3805 Note: LIST has to be sorted over `<'."
3806   (if (not ranges)
3807       (gnus-compress-sequence list t)
3808     (setq list (copy-sequence list))
3809     (or (listp (cdr ranges))
3810         (setq ranges (list ranges)))
3811     (let ((out ranges)
3812           ilist lowest highest temp)
3813       (while (and ranges list)
3814         (setq ilist list)
3815         (setq lowest (or (and (atom (car ranges)) (car ranges))
3816                          (caar ranges)))
3817         (while (and list (cdr list) (< (cadr list) lowest))
3818           (setq list (cdr list)))
3819         (if (< (car ilist) lowest)
3820             (progn
3821               (setq temp list)
3822               (setq list (cdr list))
3823               (setcdr temp nil)
3824               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3825         (setq highest (or (and (atom (car ranges)) (car ranges))
3826                           (cdar ranges)))
3827         (while (and list (<= (car list) highest))
3828           (setq list (cdr list)))
3829         (setq ranges (cdr ranges)))
3830       (if list
3831           (setq out (nconc (gnus-compress-sequence list t) out)))
3832       (setq out (sort out (lambda (r1 r2)
3833                             (< (or (and (atom r1) r1) (car r1))
3834                                (or (and (atom r2) r2) (car r2))))))
3835       (setq ranges out)
3836       (while ranges
3837         (if (atom (car ranges))
3838             (if (cdr ranges)
3839                 (if (atom (cadr ranges))
3840                     (if (= (1+ (car ranges)) (cadr ranges))
3841                         (progn
3842                           (setcar ranges (cons (car ranges)
3843                                                (cadr ranges)))
3844                           (setcdr ranges (cddr ranges))))
3845                   (if (= (1+ (car ranges)) (caadr ranges))
3846                       (progn
3847                         (setcar (cadr ranges) (car ranges))
3848                         (setcar ranges (cadr ranges))
3849                         (setcdr ranges (cddr ranges))))))
3850           (if (cdr ranges)
3851               (if (atom (cadr ranges))
3852                   (if (= (1+ (cdar ranges)) (cadr ranges))
3853                       (progn
3854                         (setcdr (car ranges) (cadr ranges))
3855                         (setcdr ranges (cddr ranges))))
3856                 (if (= (1+ (cdar ranges)) (caadr ranges))
3857                     (progn
3858                       (setcdr (car ranges) (cdadr ranges))
3859                       (setcdr ranges (cddr ranges)))))))
3860         (setq ranges (cdr ranges)))
3861       out)))
3862
3863 (defun gnus-remove-from-range (ranges list)
3864   "Return a list of ranges that has all articles from LIST removed from RANGES.
3865 Note: LIST has to be sorted over `<'."
3866   ;; !!! This function shouldn't look like this, but I've got a headache.
3867   (gnus-compress-sequence
3868    (gnus-sorted-complement
3869     (gnus-uncompress-range ranges) list)))
3870
3871 (defun gnus-member-of-range (number ranges)
3872   (if (not (listp (cdr ranges)))
3873       (and (>= number (car ranges))
3874            (<= number (cdr ranges)))
3875     (let ((not-stop t))
3876       (while (and ranges
3877                   (if (numberp (car ranges))
3878                       (>= number (car ranges))
3879                     (>= number (caar ranges)))
3880                   not-stop)
3881         (if (if (numberp (car ranges))
3882                 (= number (car ranges))
3883               (and (>= number (caar ranges))
3884                    (<= number (cdar ranges))))
3885             (setq not-stop nil))
3886         (setq ranges (cdr ranges)))
3887       (not not-stop))))
3888
3889 (defun gnus-range-length (range)
3890   "Return the length RANGE would have if uncompressed."
3891   (length (gnus-uncompress-range range)))
3892
3893 (defun gnus-sublist-p (list sublist)
3894   "Test whether all elements in SUBLIST are members of LIST."
3895   (let ((sublistp t))
3896     (while sublist
3897       (unless (memq (pop sublist) list)
3898         (setq sublistp nil
3899               sublist nil)))
3900     sublistp))
3901
3902 \f
3903 ;;;
3904 ;;; Gnus group mode
3905 ;;;
3906
3907 (defvar gnus-group-mode-map nil)
3908 (put 'gnus-group-mode 'mode-class 'special)
3909
3910 (unless gnus-group-mode-map
3911   (setq gnus-group-mode-map (make-keymap))
3912   (suppress-keymap gnus-group-mode-map)
3913
3914   (gnus-define-keys
3915    gnus-group-mode-map
3916    " " gnus-group-read-group
3917    "=" gnus-group-select-group
3918    "\M- " gnus-group-unhidden-select-group
3919    "\r" gnus-group-select-group
3920    "\M-\r" gnus-group-quick-select-group
3921    "j" gnus-group-jump-to-group
3922    "n" gnus-group-next-unread-group
3923    "p" gnus-group-prev-unread-group
3924    "\177" gnus-group-prev-unread-group
3925    [delete] gnus-group-prev-unread-group
3926    "N" gnus-group-next-group
3927    "P" gnus-group-prev-group
3928    "\M-n" gnus-group-next-unread-group-same-level
3929    "\M-p" gnus-group-prev-unread-group-same-level
3930    "," gnus-group-best-unread-group
3931    "." gnus-group-first-unread-group
3932    "u" gnus-group-unsubscribe-current-group
3933    "U" gnus-group-unsubscribe-group
3934    "c" gnus-group-catchup-current
3935    "C" gnus-group-catchup-current-all
3936    "l" gnus-group-list-groups
3937    "L" gnus-group-list-all-groups
3938    "m" gnus-group-mail
3939    "g" gnus-group-get-new-news
3940    "\M-g" gnus-group-get-new-news-this-group
3941    "R" gnus-group-restart
3942    "r" gnus-group-read-init-file
3943    "B" gnus-group-browse-foreign-server
3944    "b" gnus-group-check-bogus-groups
3945    "F" gnus-find-new-newsgroups
3946    "\C-c\C-d" gnus-group-describe-group
3947    "\M-d" gnus-group-describe-all-groups
3948    "\C-c\C-a" gnus-group-apropos
3949    "\C-c\M-\C-a" gnus-group-description-apropos
3950    "a" gnus-group-post-news
3951    "\ek" gnus-group-edit-local-kill
3952    "\eK" gnus-group-edit-global-kill
3953    "\C-k" gnus-group-kill-group
3954    "\C-y" gnus-group-yank-group
3955    "\C-w" gnus-group-kill-region
3956    "\C-x\C-t" gnus-group-transpose-groups
3957    "\C-c\C-l" gnus-group-list-killed
3958    "\C-c\C-x" gnus-group-expire-articles
3959    "\C-c\M-\C-x" gnus-group-expire-all-groups
3960    "V" gnus-version
3961    "s" gnus-group-save-newsrc
3962    "z" gnus-group-suspend
3963    "Z" gnus-group-clear-dribble
3964    "q" gnus-group-exit
3965    "Q" gnus-group-quit
3966    "?" gnus-group-describe-briefly
3967    "\C-c\C-i" gnus-info-find-node
3968    "\M-e" gnus-group-edit-group-method
3969    "^" gnus-group-enter-server-mode
3970    gnus-mouse-2 gnus-mouse-pick-group
3971    "<" beginning-of-buffer
3972    ">" end-of-buffer
3973    "\C-c\C-b" gnus-bug
3974    "\C-c\C-s" gnus-group-sort-groups
3975    "t" gnus-topic-mode
3976    "\C-c\M-g" gnus-activate-all-groups
3977    "\M-&" gnus-group-universal-argument
3978    "#" gnus-group-mark-group
3979    "\M-#" gnus-group-unmark-group)
3980
3981   (gnus-define-keys
3982    (gnus-group-mark-map "M" gnus-group-mode-map)
3983    "m" gnus-group-mark-group
3984    "u" gnus-group-unmark-group
3985    "w" gnus-group-mark-region
3986    "m" gnus-group-mark-buffer
3987    "r" gnus-group-mark-regexp
3988    "U" gnus-group-unmark-all-groups)
3989
3990   (gnus-define-keys
3991    (gnus-group-group-map "G" gnus-group-mode-map)
3992    "d" gnus-group-make-directory-group
3993    "h" gnus-group-make-help-group
3994    "a" gnus-group-make-archive-group
3995    "k" gnus-group-make-kiboze-group
3996    "m" gnus-group-make-group
3997    "E" gnus-group-edit-group
3998    "e" gnus-group-edit-group-method
3999    "p" gnus-group-edit-group-parameters
4000    "v" gnus-group-add-to-virtual
4001    "V" gnus-group-make-empty-virtual
4002    "D" gnus-group-enter-directory
4003    "f" gnus-group-make-doc-group
4004    "r" gnus-group-rename-group
4005    "\177" gnus-group-delete-group
4006    [delete] gnus-group-delete-group)
4007
4008    (gnus-define-keys
4009     (gnus-group-soup-map "s" gnus-group-group-map)
4010     "b" gnus-group-brew-soup
4011     "w" gnus-soup-save-areas
4012     "s" gnus-soup-send-replies
4013     "p" gnus-soup-pack-packet
4014     "r" nnsoup-pack-replies)
4015
4016    (gnus-define-keys
4017     (gnus-group-sort-map "S" gnus-group-group-map)
4018     "s" gnus-group-sort-groups
4019     "a" gnus-group-sort-groups-by-alphabet
4020     "u" gnus-group-sort-groups-by-unread
4021     "l" gnus-group-sort-groups-by-level
4022     "v" gnus-group-sort-groups-by-score
4023     "r" gnus-group-sort-groups-by-rank
4024     "m" gnus-group-sort-groups-by-method)
4025
4026    (gnus-define-keys
4027     (gnus-group-list-map "A" gnus-group-mode-map)
4028     "k" gnus-group-list-killed
4029     "z" gnus-group-list-zombies
4030     "s" gnus-group-list-groups
4031     "u" gnus-group-list-all-groups
4032     "A" gnus-group-list-active
4033     "a" gnus-group-apropos
4034     "d" gnus-group-description-apropos
4035     "m" gnus-group-list-matching
4036     "M" gnus-group-list-all-matching
4037     "l" gnus-group-list-level)
4038
4039    (gnus-define-keys
4040     (gnus-group-score-map "W" gnus-group-mode-map)
4041     "f" gnus-score-flush-cache)
4042
4043    (gnus-define-keys
4044     (gnus-group-help-map "H" gnus-group-mode-map)
4045     "f" gnus-group-fetch-faq)
4046
4047    (gnus-define-keys
4048     (gnus-group-sub-map "S" gnus-group-mode-map)
4049     "l" gnus-group-set-current-level
4050     "t" gnus-group-unsubscribe-current-group
4051     "s" gnus-group-unsubscribe-group
4052     "k" gnus-group-kill-group
4053     "y" gnus-group-yank-group
4054     "w" gnus-group-kill-region
4055     "\C-k" gnus-group-kill-level
4056     "z" gnus-group-kill-all-zombies))
4057
4058 (defun gnus-group-mode ()
4059   "Major mode for reading news.
4060
4061 All normal editing commands are switched off.
4062 \\<gnus-group-mode-map>
4063 The group buffer lists (some of) the groups available.  For instance,
4064 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4065 lists all zombie groups.
4066
4067 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4068 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4069
4070 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4071
4072 The following commands are available:
4073
4074 \\{gnus-group-mode-map}"
4075   (interactive)
4076   (when (and menu-bar-mode
4077              (gnus-visual-p 'group-menu 'menu))
4078     (gnus-group-make-menu-bar))
4079   (kill-all-local-variables)
4080   (gnus-simplify-mode-line)
4081   (setq major-mode 'gnus-group-mode)
4082   (setq mode-name "Group")
4083   (gnus-group-set-mode-line)
4084   (setq mode-line-process nil)
4085   (use-local-map gnus-group-mode-map)
4086   (buffer-disable-undo (current-buffer))
4087   (setq truncate-lines t)
4088   (setq buffer-read-only t)
4089   (run-hooks 'gnus-group-mode-hook))
4090
4091 (defun gnus-mouse-pick-group (e)
4092   "Enter the group under the mouse pointer."
4093   (interactive "e")
4094   (mouse-set-point e)
4095   (gnus-group-read-group nil))
4096
4097 ;; Look at LEVEL and find out what the level is really supposed to be.
4098 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4099 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4100 (defun gnus-group-default-level (&optional level number-or-nil)
4101   (cond
4102    (gnus-group-use-permanent-levels
4103     (setq gnus-group-default-list-level
4104           (or level gnus-group-default-list-level))
4105     (or gnus-group-default-list-level gnus-level-subscribed))
4106    (number-or-nil
4107     level)
4108    (t
4109     (or level gnus-group-default-list-level gnus-level-subscribed))))
4110
4111 ;;;###autoload
4112 (defun gnus-slave-no-server (&optional arg)
4113   "Read network news as a slave, without connecting to local server"
4114   (interactive "P")
4115   (gnus-no-server arg t))
4116
4117 ;;;###autoload
4118 (defun gnus-no-server (&optional arg slave)
4119   "Read network news.
4120 If ARG is a positive number, Gnus will use that as the
4121 startup level.  If ARG is nil, Gnus will be started at level 2.
4122 If ARG is non-nil and not a positive number, Gnus will
4123 prompt the user for the name of an NNTP server to use.
4124 As opposed to `gnus', this command will not connect to the local server."
4125   (interactive "P")
4126   (make-local-variable 'gnus-group-use-permanent-levels)
4127   (setq gnus-group-use-permanent-levels t)
4128   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4129
4130 ;;;###autoload
4131 (defun gnus-slave (&optional arg)
4132   "Read news as a slave."
4133   (interactive "P")
4134   (gnus arg nil 'slave))
4135
4136 ;;;###autoload
4137 (defun gnus-other-frame (&optional arg)
4138   "Pop up a frame to read news."
4139   (interactive "P")
4140   (if (get-buffer gnus-group-buffer)
4141       (let ((pop-up-frames t))
4142         (gnus arg))
4143     (select-frame (make-frame))
4144     (gnus arg)))
4145
4146 ;;;###autoload
4147 (defun gnus (&optional arg dont-connect slave)
4148   "Read network news.
4149 If ARG is non-nil and a positive number, Gnus will use that as the
4150 startup level.  If ARG is non-nil and not a positive number, Gnus will
4151 prompt the user for the name of an NNTP server to use."
4152   (interactive "P")
4153
4154   (if (get-buffer gnus-group-buffer)
4155       (progn
4156         (switch-to-buffer gnus-group-buffer)
4157         (gnus-group-get-new-news))
4158
4159     (gnus-clear-system)
4160     (nnheader-init-server-buffer)
4161     (gnus-read-init-file)
4162     (setq gnus-slave slave)
4163
4164     (gnus-group-setup-buffer)
4165     (let ((buffer-read-only nil))
4166       (erase-buffer)
4167       (if (not gnus-inhibit-startup-message)
4168           (progn
4169             (gnus-group-startup-message)
4170             (sit-for 0))))
4171
4172     (let ((level (and (numberp arg) (> arg 0) arg))
4173           did-connect)
4174       (unwind-protect
4175           (progn
4176             (or dont-connect
4177                 (setq did-connect
4178                       (gnus-start-news-server (and arg (not level))))))
4179         (if (and (not dont-connect)
4180                  (not did-connect))
4181             (gnus-group-quit)
4182           (run-hooks 'gnus-startup-hook)
4183           ;; NNTP server is successfully open.
4184
4185           ;; Find the current startup file name.
4186           (setq gnus-current-startup-file
4187                 (gnus-make-newsrc-file gnus-startup-file))
4188
4189           ;; Read the dribble file.
4190           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4191
4192           (gnus-summary-make-display-table)
4193           ;; Do the actual startup.
4194           (gnus-setup-news nil level)
4195           ;; Generate the group buffer.
4196           (gnus-group-list-groups level)
4197           (gnus-group-first-unread-group)
4198           (gnus-configure-windows 'group)
4199           (gnus-group-set-mode-line))))))
4200
4201 (defun gnus-unload ()
4202   "Unload all Gnus features."
4203   (interactive)
4204   (or (boundp 'load-history)
4205       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4206   (let ((history load-history)
4207         feature)
4208     (while history
4209       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4210            (setq feature (cdr (assq 'provide (car history))))
4211            (unload-feature feature 'force))
4212       (setq history (cdr history)))))
4213
4214 (defun gnus-compile ()
4215   "Byte-compile the user-defined format specs."
4216   (interactive)
4217   (let ((entries gnus-format-specs)
4218         entry gnus-tmp-func)
4219     (save-excursion
4220       (gnus-message 7 "Compiling format specs...")
4221
4222       (while entries
4223         (setq entry (pop entries))
4224         (if (eq (car entry) 'version)
4225             (setq gnus-format-specs (delq entry gnus-format-specs))
4226           (when (and (listp (caddr entry))
4227                      (not (eq 'byte-code (caaddr entry))))
4228             (fset 'gnus-tmp-func
4229                   `(lambda () ,(caddr entry)))
4230             (byte-compile 'gnus-tmp-func)
4231             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4232
4233       (push (cons 'version emacs-version) gnus-format-specs)
4234
4235       (gnus-message 7 "Compiling user specs...done"))))
4236
4237 (defun gnus-indent-rigidly (start end arg)
4238   "Indent rigidly using only spaces and no tabs."
4239   (save-excursion
4240     (save-restriction
4241       (narrow-to-region start end)
4242       (indent-rigidly start end arg)
4243       (goto-char (point-min))
4244       (while (search-forward "\t" nil t)
4245         (replace-match "        " t t)))))
4246
4247 (defun gnus-group-startup-message (&optional x y)
4248   "Insert startup message in current buffer."
4249   ;; Insert the message.
4250   (erase-buffer)
4251   (insert
4252    (format "              %s
4253           _    ___ _             _
4254           _ ___ __ ___  __    _ ___
4255           __   _     ___    __  ___
4256               _           ___     _
4257              _  _ __             _
4258              ___   __            _
4259                    __           _
4260                     _      _   _
4261                    _      _    _
4262                       _  _    _
4263                   __  ___
4264                  _   _ _     _
4265                 _   _
4266               _    _
4267              _    _
4268             _
4269           __
4270
4271 "
4272            ""))
4273   ;; And then hack it.
4274   (gnus-indent-rigidly (point-min) (point-max)
4275                        (/ (max (- (window-width) (or x 46)) 0) 2))
4276   (goto-char (point-min))
4277   (forward-line 1)
4278   (let* ((pheight (count-lines (point-min) (point-max)))
4279          (wheight (window-height))
4280          (rest (- wheight pheight)))
4281     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4282   ;; Fontify some.
4283   (goto-char (point-min))
4284   (and (search-forward "Praxis" nil t)
4285        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4286   (goto-char (point-min))
4287   (let* ((mode-string (gnus-group-set-mode-line)))
4288     (setq mode-line-buffer-identification
4289           (list (concat gnus-version (substring (car mode-string) 4))))
4290     (set-buffer-modified-p t)))
4291
4292 (defun gnus-group-setup-buffer ()
4293   (or (get-buffer gnus-group-buffer)
4294       (progn
4295         (switch-to-buffer gnus-group-buffer)
4296         (gnus-add-current-to-buffer-list)
4297         (gnus-group-mode)
4298         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4299
4300 (defun gnus-group-list-groups (&optional level unread lowest)
4301   "List newsgroups with level LEVEL or lower that have unread articles.
4302 Default is all subscribed groups.
4303 If argument UNREAD is non-nil, groups with no unread articles are also
4304 listed."
4305   (interactive (list (if current-prefix-arg
4306                          (prefix-numeric-value current-prefix-arg)
4307                        (or
4308                         (gnus-group-default-level nil t)
4309                         gnus-group-default-list-level
4310                         gnus-level-subscribed))))
4311   (or level
4312       (setq level (car gnus-group-list-mode)
4313             unread (cdr gnus-group-list-mode)))
4314   (setq level (gnus-group-default-level level))
4315   (gnus-group-setup-buffer)             ;May call from out of group buffer
4316   (gnus-update-format-specifications)
4317   (let ((case-fold-search nil)
4318         (props (text-properties-at (gnus-point-at-bol)))
4319         (group (gnus-group-group-name)))
4320     (funcall gnus-group-prepare-function level unread lowest)
4321     (if (zerop (buffer-size))
4322         (gnus-message 5 gnus-no-groups-message)
4323       (goto-char (point-max))
4324       (when (or (not gnus-group-goto-next-group-function)
4325                 (not (funcall gnus-group-goto-next-group-function 
4326                               group props)))
4327         (if (not group)
4328             ;; Go to the first group with unread articles.
4329             (gnus-group-search-forward t)
4330           ;; Find the right group to put point on.  If the current group
4331           ;; has disappeared in the new listing, try to find the next
4332           ;; one.        If no next one can be found, just leave point at the
4333           ;; first newsgroup in the buffer.
4334           (if (not (gnus-goto-char
4335                     (text-property-any
4336                      (point-min) (point-max)
4337                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4338               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4339                 (while (and newsrc
4340                             (not (gnus-goto-char
4341                                   (text-property-any
4342                                    (point-min) (point-max) 'gnus-group
4343                                    (gnus-intern-safe
4344                                     (caar newsrc) gnus-active-hashtb)))))
4345                   (setq newsrc (cdr newsrc)))
4346                 (or newsrc (progn (goto-char (point-max))
4347                                   (forward-line -1)))))))
4348       ;; Adjust cursor point.
4349       (gnus-group-position-point))))
4350
4351 (defun gnus-group-list-level (level &optional all)
4352   "List groups on LEVEL.
4353 If ALL (the prefix), also list groups that have no unread articles."
4354   (interactive "nList groups on level: \nP")
4355   (gnus-group-list-groups level all level))
4356
4357 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4358   "List all newsgroups with unread articles of level LEVEL or lower.
4359 If ALL is non-nil, list groups that have no unread articles.
4360 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4361 If REGEXP, only list groups matching REGEXP."
4362   (set-buffer gnus-group-buffer)
4363   (let ((buffer-read-only nil)
4364         (newsrc (cdr gnus-newsrc-alist))
4365         (lowest (or lowest 1))
4366         info clevel unread group params)
4367     (erase-buffer)
4368     (if (< lowest gnus-level-zombie)
4369         ;; List living groups.
4370         (while newsrc
4371           (setq info (car newsrc)
4372                 group (gnus-info-group info)
4373                 params (gnus-info-params info)
4374                 newsrc (cdr newsrc)
4375                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4376           (and unread                   ; This group might be bogus
4377                (or (not regexp)
4378                    (string-match regexp group))
4379                (<= (setq clevel (gnus-info-level info)) level)
4380                (>= clevel lowest)
4381                (or all                  ; We list all groups?
4382                    (and gnus-group-list-inactive-groups
4383                         (eq unread t))  ; We list unactivated groups
4384                    (> unread 0)         ; We list groups with unread articles
4385                    (and gnus-list-groups-with-ticked-articles
4386                         (cdr (assq 'tick (gnus-info-marks info))))
4387                                         ; And groups with tickeds
4388                    ;; Check for permanent visibility.
4389                    (and gnus-permanently-visible-groups
4390                         (string-match gnus-permanently-visible-groups
4391                                       group))
4392                    (memq 'visible params)
4393                    (cdr (assq 'visible params)))
4394                (gnus-group-insert-group-line
4395                 group (gnus-info-level info)
4396                 (gnus-info-marks info) unread (gnus-info-method info)))))
4397
4398     ;; List dead groups.
4399     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4400          (gnus-group-prepare-flat-list-dead
4401           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4402           gnus-level-zombie ?Z
4403           regexp))
4404     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4405          (gnus-group-prepare-flat-list-dead
4406           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4407           gnus-level-killed ?K regexp))
4408
4409     (gnus-group-set-mode-line)
4410     (setq gnus-group-list-mode (cons level all))
4411     (run-hooks 'gnus-group-prepare-hook)))
4412
4413 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4414   ;; List zombies and killed lists somewhat faster, which was
4415   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4416   ;; this by ignoring the group format specification altogether.
4417   (let (group beg)
4418     (if regexp
4419         ;; This loop is used when listing groups that match some
4420         ;; regexp.
4421         (while groups
4422           (setq group (pop groups))
4423           (when (string-match regexp group)
4424             (add-text-properties
4425              (point) (prog1 (1+ (point))
4426                        (insert " " mark "     *: " group "\n"))
4427              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4428                    'gnus-unread t
4429                    'gnus-level level))))
4430       ;; This loop is used when listing all groups.
4431       (while groups
4432         (add-text-properties
4433          (point) (prog1 (1+ (point))
4434                    (insert " " mark "     *: "
4435                            (setq group (pop groups)) "\n"))
4436          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4437                'gnus-unread t
4438                'gnus-level level))))))
4439
4440 (defmacro gnus-group-real-name (group)
4441   "Find the real name of a foreign newsgroup."
4442   `(let ((gname ,group))
4443      (if (string-match ":[^:]+$" gname)
4444          (substring gname (1+ (match-beginning 0)))
4445        gname)))
4446
4447 (defsubst gnus-server-add-address (method)
4448   (let ((method-name (symbol-name (car method))))
4449     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4450              (not (assq (intern (concat method-name "-address")) method)))
4451         (append method (list (list (intern (concat method-name "-address"))
4452                                    (nth 1 method))))
4453       method)))
4454
4455 (defsubst gnus-server-get-method (group method)
4456   ;; Input either a server name, and extended server name, or a
4457   ;; select method, and return a select method.
4458   (cond ((stringp method)
4459          (gnus-server-to-method method))
4460         ((and (stringp (car method)) group)
4461          (gnus-server-extend-method group method))
4462         (t
4463          (gnus-server-add-address method))))
4464
4465 (defun gnus-server-to-method (server)
4466   "Map virtual server names to select methods."
4467   (or 
4468    ;; Perhaps this is the native server?
4469    (and (equal server "native") gnus-select-method)
4470    ;; It should be in the server alist.
4471    (cdr (assoc server gnus-server-alist))
4472    ;; If not, we look through all the opened server
4473    ;; to see whether we can find it there.
4474    (let ((opened gnus-opened-servers))
4475      (while (and opened
4476                  (not (equal server (format "%s:%s" (caaar opened)
4477                                             (cadaar opened)))))
4478        (pop opened))
4479      (caar opened))))
4480
4481 (defmacro gnus-server-equal (ss1 ss2)
4482   "Say whether two servers are equal."
4483   `(let ((s1 ,ss1)
4484          (s2 ,ss2))
4485      (or (equal s1 s2)
4486          (and (= (length s1) (length s2))
4487               (progn
4488                 (while (and s1 (member (car s1) s2))
4489                   (setq s1 (cdr s1)))
4490                 (null s1))))))
4491
4492 (defun gnus-group-prefixed-name (group method)
4493   "Return the whole name from GROUP and METHOD."
4494   (and (stringp method) (setq method (gnus-server-to-method method)))
4495   (concat (format "%s" (car method))
4496           (if (and
4497                (or (assoc (format "%s" (car method)) 
4498                           (gnus-methods-using 'address))
4499                    (equal method gnus-message-archive-method))
4500                (not (string= (nth 1 method) "")))
4501               (concat "+" (nth 1 method)))
4502           ":" group))
4503
4504 (defun gnus-group-real-prefix (group)
4505   "Return the prefix of the current group name."
4506   (if (string-match "^[^:]+:" group)
4507       (substring group 0 (match-end 0))
4508     ""))
4509
4510 (defun gnus-group-method (group)
4511   "Return the server or method used for selecting GROUP."
4512   (let ((prefix (gnus-group-real-prefix group)))
4513     (if (equal prefix "")
4514         gnus-select-method
4515       (let ((servers gnus-opened-servers)
4516             (server "")
4517             backend possible found)
4518         (if (string-match "^[^\\+]+\\+" prefix)
4519             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4520                   server (substring prefix (match-end 0) (1- (length prefix))))
4521           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4522         (while servers
4523           (when (eq (caaar servers) backend)
4524             (setq possible (caar servers))
4525             (when (equal (cadaar servers) server)
4526               (setq found (caar servers))))
4527           (pop servers))
4528         (or (car (rassoc found gnus-server-alist))
4529             found
4530             (car (rassoc possible gnus-server-alist))
4531             possible
4532             (list backend server))))))
4533
4534 (defsubst gnus-secondary-method-p (method)
4535   "Return whether METHOD is a secondary select method."
4536   (let ((methods gnus-secondary-select-methods)
4537         (gmethod (gnus-server-get-method nil method)))
4538     (while (and methods
4539                 (not (equal (gnus-server-get-method nil (car methods))
4540                             gmethod)))
4541       (setq methods (cdr methods)))
4542     methods))
4543
4544 (defun gnus-group-foreign-p (group)
4545   "Say whether a group is foreign or not."
4546   (and (not (gnus-group-native-p group))
4547        (not (gnus-group-secondary-p group))))
4548
4549 (defun gnus-group-native-p (group)
4550   "Say whether the group is native or not."
4551   (not (string-match ":" group)))
4552
4553 (defun gnus-group-secondary-p (group)
4554   "Say whether the group is secondary or not."
4555   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4556
4557 (defun gnus-group-get-parameter (group &optional symbol)
4558   "Returns the group parameters for GROUP.
4559 If SYMBOL, return the value of that symbol in the group parameters."
4560   (let ((params (gnus-info-params (gnus-get-info group))))
4561     (if symbol
4562         (gnus-group-parameter-value params symbol)
4563       params)))
4564
4565 (defun gnus-group-parameter-value (params symbol)
4566   "Return the value of SYMBOL in group PARAMS."
4567   (or (car (memq symbol params))        ; It's either a simple symbol
4568       (cdr (assq symbol params))))      ; or a cons.
4569
4570 (defun gnus-group-add-parameter (group param)
4571   "Add parameter PARAM to GROUP."
4572   (let ((info (gnus-get-info group)))
4573     (if (not info)
4574         () ; This is a dead group.  We just ignore it.
4575       ;; Cons the new param to the old one and update.
4576       (gnus-group-set-info (cons param (gnus-info-params info))
4577                            group 'params))))
4578
4579 (defun gnus-group-set-parameter (group name value)
4580   "Set parameter NAME to VALUE in GROUP."
4581   (let ((info (gnus-get-info group)))
4582     (if (not info)
4583         () ; This is a dead group.  We just ignore it.
4584       (let ((old-params (gnus-info-params info))
4585             (new-params (list (cons name value))))
4586         (while old-params
4587           (if (or (not (listp (car old-params)))
4588                   (not (eq (caar old-params) name)))
4589               (setq new-params (append new-params (list (car old-params)))))
4590           (setq old-params (cdr old-params)))
4591         (gnus-group-set-info new-params group 'params)))))
4592
4593 (defun gnus-group-add-score (group &optional score)
4594   "Add SCORE to the GROUP score.
4595 If SCORE is nil, add 1 to the score of GROUP."
4596   (let ((info (gnus-get-info group)))
4597     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4598
4599 (defun gnus-summary-bubble-group ()
4600   "Increase the score of the current group.
4601 This is a handy function to add to `gnus-summary-exit-hook' to
4602 increase the score of each group you read."
4603   (gnus-group-add-score gnus-newsgroup-name))
4604
4605 (defun gnus-group-set-info (info &optional method-only-group part)
4606   (let* ((entry (gnus-gethash
4607                  (or method-only-group (gnus-info-group info))
4608                  gnus-newsrc-hashtb))
4609          (part-info info)
4610          (info (if method-only-group (nth 2 entry) info)))
4611     (when method-only-group
4612       (unless entry
4613         (error "Trying to change non-existent group %s" method-only-group))
4614       ;; We have received parts of the actual group info - either the
4615       ;; select method or the group parameters.  We first check
4616       ;; whether we have to extend the info, and if so, do that.
4617       (let ((len (length info))
4618             (total (if (eq part 'method) 5 6)))
4619         (when (< len total)
4620           (setcdr (nthcdr (1- len) info)
4621                   (make-list (- total len) nil)))
4622         ;; Then we enter the new info.
4623         (setcar (nthcdr (1- total) info) part-info)))
4624     (unless entry
4625       ;; This is a new group, so we just create it.
4626       (save-excursion
4627         (set-buffer gnus-group-buffer)
4628         (if (gnus-info-method info)
4629             ;; It's a foreign group...
4630             (gnus-group-make-group
4631              (gnus-group-real-name (gnus-info-group info))
4632              (prin1-to-string (car (gnus-info-method info)))
4633              (nth 1 (gnus-info-method info)))
4634           ;; It's a native group.
4635           (gnus-group-make-group (gnus-info-group info)))
4636         (gnus-message 6 "Note: New group created")
4637         (setq entry
4638               (gnus-gethash (gnus-group-prefixed-name
4639                              (gnus-group-real-name (gnus-info-group info))
4640                              (or (gnus-info-method info) gnus-select-method))
4641                             gnus-newsrc-hashtb))))
4642     ;; Whether it was a new group or not, we now have the entry, so we
4643     ;; can do the update.
4644     (if entry
4645         (progn
4646           (setcar (nthcdr 2 entry) info)
4647           (when (and (not (eq (car entry) t))
4648                      (gnus-active (gnus-info-group info)))
4649             (let ((marked (gnus-info-marks info)))
4650               (setcar entry (length (gnus-list-of-unread-articles
4651                                      (car info)))))))
4652       (error "No such group: %s" (gnus-info-group info)))))
4653
4654 (defun gnus-group-set-method-info (group select-method)
4655   (gnus-group-set-info select-method group 'method))
4656
4657 (defun gnus-group-set-params-info (group params)
4658   (gnus-group-set-info params group 'params))
4659
4660 (defun gnus-group-update-group-line ()
4661   "Update the current line in the group buffer."
4662   (let* ((buffer-read-only nil)
4663          (group (gnus-group-group-name))
4664          (gnus-group-indentation (gnus-group-group-indentation))
4665          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4666     (and entry
4667          (not (gnus-ephemeral-group-p group))
4668          (gnus-dribble-enter
4669           (concat "(gnus-group-set-info '"
4670                   (prin1-to-string (nth 2 entry)) ")")))
4671     (gnus-delete-line)
4672     (gnus-group-insert-group-line-info group)
4673     (forward-line -1)
4674     (gnus-group-position-point)))
4675
4676 (defun gnus-group-insert-group-line-info (group)
4677   "Insert GROUP on the current line."
4678   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4679         active info)
4680     (if entry
4681         (progn
4682           ;; (Un)subscribed group.
4683           (setq info (nth 2 entry))
4684           (gnus-group-insert-group-line
4685            group (gnus-info-level info) (gnus-info-marks info)
4686            (or (car entry) t) (gnus-info-method info)))
4687       ;; This group is dead.
4688       (gnus-group-insert-group-line
4689        group
4690        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4691        nil
4692        (if (setq active (gnus-active group))
4693            (- (1+ (cdr active)) (car active)) 0)
4694        nil))))
4695
4696 (defun gnus-group-insert-group-line
4697   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4698                   gnus-tmp-method)
4699   "Insert a group line in the group buffer."
4700   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4701          (gnus-tmp-number-total
4702           (if gnus-tmp-active
4703               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4704             0))
4705          (gnus-tmp-number-of-unread
4706           (if (numberp number) (int-to-string (max 0 number))
4707             "*"))
4708          (gnus-tmp-number-of-read
4709           (if (numberp number)
4710               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4711             "*"))
4712          (gnus-tmp-subscribed
4713           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4714                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4715                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4716                 (t ?K)))
4717          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4718          (gnus-tmp-newsgroup-description
4719           (if gnus-description-hashtb
4720               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4721             ""))
4722          (gnus-tmp-moderated
4723           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4724          (gnus-tmp-moderated-string
4725           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4726          (gnus-tmp-method
4727           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4728          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4729          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4730          (gnus-tmp-news-method-string
4731           (if gnus-tmp-method
4732               (format "(%s:%s)" (car gnus-tmp-method)
4733                       (cadr gnus-tmp-method)) ""))
4734          (gnus-tmp-marked-mark
4735           (if (and (numberp number)
4736                    (zerop number)
4737                    (cdr (assq 'tick gnus-tmp-marked)))
4738               ?* ? ))
4739          (gnus-tmp-process-marked
4740           (if (member gnus-tmp-group gnus-group-marked)
4741               gnus-process-mark ? ))
4742          (buffer-read-only nil)
4743          header gnus-tmp-header)                        ; passed as parameter to user-funcs.
4744     (beginning-of-line)
4745     (add-text-properties
4746      (point)
4747      (prog1 (1+ (point))
4748        ;; Insert the text.
4749        (eval gnus-group-line-format-spec))
4750      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4751        gnus-unread ,(if (numberp number)
4752                         (string-to-int gnus-tmp-number-of-unread)
4753                       t)
4754        gnus-marked ,gnus-tmp-marked-mark
4755        gnus-indentation ,gnus-group-indentation
4756        gnus-level ,gnus-tmp-level))
4757     (when (gnus-visual-p 'group-highlight 'highlight)
4758       (forward-line -1)
4759       (run-hooks 'gnus-group-update-hook)
4760       (forward-line))
4761     ;; Allow XEmacs to remove front-sticky text properties.
4762     (gnus-group-remove-excess-properties)))
4763
4764 (defun gnus-group-update-group (group &optional visible-only)
4765   "Update all lines where GROUP appear.
4766 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4767 already."
4768   (save-excursion
4769     (set-buffer gnus-group-buffer)
4770     ;; The buffer may be narrowed.
4771     (save-restriction
4772       (widen)
4773       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4774             (loc (point-min))
4775             found buffer-read-only visible)
4776         ;; Enter the current status into the dribble buffer.
4777         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4778           (if (and entry (not (gnus-ephemeral-group-p group)))
4779               (gnus-dribble-enter
4780                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4781                        ")"))))
4782         ;; Find all group instances.  If topics are in use, each group
4783         ;; may be listed in more than once.
4784         (while (setq loc (text-property-any
4785                           loc (point-max) 'gnus-group ident))
4786           (setq found t)
4787           (goto-char loc)
4788           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4789             (gnus-delete-line)
4790             (gnus-group-insert-group-line-info group))
4791           (setq loc (1+ loc)))
4792         (unless (or found visible-only)
4793           ;; No such line in the buffer, find out where it's supposed to
4794           ;; go, and insert it there (or at the end of the buffer).
4795           (if gnus-goto-missing-group-function
4796               (funcall gnus-goto-missing-group-function group)
4797             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4798               (while (and entry (car entry)
4799                           (not
4800                            (gnus-goto-char
4801                             (text-property-any
4802                              (point-min) (point-max)
4803                              'gnus-group (gnus-intern-safe
4804                                           (caar entry) gnus-active-hashtb)))))
4805                 (setq entry (cdr entry)))
4806               (or entry (goto-char (point-max)))))
4807           ;; Finally insert the line.
4808           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4809             (gnus-group-insert-group-line-info group)))
4810         (gnus-group-set-mode-line)))))
4811
4812 (defun gnus-group-set-mode-line ()
4813   (when (memq 'group gnus-updated-mode-lines)
4814     (let* ((gformat (or gnus-group-mode-line-format-spec
4815                         (setq gnus-group-mode-line-format-spec
4816                               (gnus-parse-format
4817                                gnus-group-mode-line-format
4818                                gnus-group-mode-line-format-alist))))
4819            (gnus-tmp-news-server (car (cdr gnus-select-method)))
4820            (gnus-tmp-news-method (car gnus-select-method))
4821            (max-len 60)
4822            gnus-tmp-header                      ;Dummy binding for user-defined formats
4823            ;; Get the resulting string.
4824            (mode-string (eval gformat)))
4825       ;; If the line is too long, we chop it off.
4826       (when (> (length mode-string) max-len)
4827         (setq mode-string (substring mode-string 0 (- max-len 4))))
4828       (prog1
4829           (setq mode-line-buffer-identification (list mode-string))
4830         (set-buffer-modified-p t)))))
4831
4832 (defun gnus-group-group-name ()
4833   "Get the name of the newsgroup on the current line."
4834   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4835     (and group (symbol-name group))))
4836
4837 (defun gnus-group-group-level ()
4838   "Get the level of the newsgroup on the current line."
4839   (get-text-property (gnus-point-at-bol) 'gnus-level))
4840
4841 (defun gnus-group-group-indentation ()
4842   "Get the indentation of the newsgroup on the current line."
4843   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation) ""))
4844
4845 (defun gnus-group-group-unread ()
4846   "Get the number of unread articles of the newsgroup on the current line."
4847   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4848
4849 (defun gnus-group-search-forward (&optional backward all level first-too)
4850   "Find the next newsgroup with unread articles.
4851 If BACKWARD is non-nil, find the previous newsgroup instead.
4852 If ALL is non-nil, just find any newsgroup.
4853 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4854 group exists.
4855 If FIRST-TOO, the current line is also eligible as a target."
4856   (let ((way (if backward -1 1))
4857         (low gnus-level-killed)
4858         (beg (point))
4859         pos found lev)
4860     (if (and backward (progn (beginning-of-line)) (bobp))
4861         nil
4862       (or first-too (forward-line way))
4863       (while (and
4864               (not (eobp))
4865               (not (setq
4866                     found
4867                     (and (or all
4868                              (and
4869                               (let ((unread
4870                                      (get-text-property (point) 'gnus-unread)))
4871                                 (and (numberp unread) (> unread 0)))
4872                               (setq lev (get-text-property (point)
4873                                                            'gnus-level))
4874                               (<= lev gnus-level-subscribed)))
4875                          (or (not level)
4876                              (and (setq lev (get-text-property (point)
4877                                                                'gnus-level))
4878                                   (or (= lev level)
4879                                       (and (< lev low)
4880                                            (< level lev)
4881                                            (progn
4882                                              (setq low lev)
4883                                              (setq pos (point))
4884                                              nil))))))))
4885               (zerop (forward-line way)))))
4886     (if found
4887         (progn (gnus-group-position-point) t)
4888       (goto-char (or pos beg))
4889       (and pos t))))
4890
4891 ;;; Gnus group mode commands
4892
4893 ;; Group marking.
4894
4895 (defun gnus-group-mark-group (n &optional unmark no-advance)
4896   "Mark the current group."
4897   (interactive "p")
4898   (let ((buffer-read-only nil)
4899         group)
4900     (while
4901         (and (> n 0)
4902              (setq group (gnus-group-group-name))
4903              (progn
4904                (beginning-of-line)
4905                (forward-char
4906                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4907                (delete-char 1)
4908                (if unmark
4909                    (progn
4910                      (insert " ")
4911                      (setq gnus-group-marked (delete group gnus-group-marked)))
4912                  (insert "#")
4913                  (setq gnus-group-marked
4914                        (cons group (delete group gnus-group-marked))))
4915                t)
4916              (or no-advance (zerop (gnus-group-next-group 1))))
4917       (setq n (1- n)))
4918     (gnus-summary-position-point)
4919     n))
4920
4921 (defun gnus-group-unmark-group (n)
4922   "Remove the mark from the current group."
4923   (interactive "p")
4924   (gnus-group-mark-group n 'unmark)
4925   (gnus-group-position-point))
4926
4927 (defun gnus-group-unmark-all-groups ()
4928   "Unmark all groups."
4929   (let ((groups gnus-group-marked))
4930     (save-excursion
4931       (while groups
4932         (gnus-group-remove-mark (pop groups)))))
4933   (gnus-group-position-point))
4934
4935 (defun gnus-group-mark-region (unmark beg end)
4936   "Mark all groups between point and mark.
4937 If UNMARK, remove the mark instead."
4938   (interactive "P\nr")
4939   (let ((num (count-lines beg end)))
4940     (save-excursion
4941       (goto-char beg)
4942       (- num (gnus-group-mark-group num unmark)))))
4943
4944 (defun gnus-group-mark-buffer (&optional unmark)
4945   "Mark all groups in the buffer.
4946 If UNMARK, remove the mark instead."
4947   (interactive "P")
4948   (gnus-group-mark-region unmark (point-min) (point-max)))
4949
4950 (defun gnus-group-mark-regexp (regexp)
4951   "Mark all groups that match some regexp."
4952   (interactive "sMark (regexp): ")
4953   (let ((alist (cdr gnus-newsrc-alist))
4954         group)
4955     (while alist
4956       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
4957         (gnus-group-set-mark group))))
4958   (gnus-group-position-point))
4959
4960 (defun gnus-group-remove-mark (group)
4961   "Remove the process mark from GROUP and move point there.
4962 Return nil if the group isn't displayed."
4963   (if (gnus-group-goto-group group)
4964       (save-excursion
4965         (gnus-group-mark-group 1 'unmark t)
4966         t)
4967     (setq gnus-group-marked
4968           (delete group gnus-group-marked))
4969     nil))
4970
4971 (defun gnus-group-set-mark (group)
4972   "Set the process mark on GROUP."
4973   (if (gnus-group-goto-group group)
4974       (save-excursion
4975         (gnus-group-mark-group 1 nil t))
4976     (setq gnus-group-marked
4977           (cons group (delete group gnus-group-marked)))))
4978
4979 (defun gnus-group-universal-argument (arg &optional groups func)
4980   "Perform any command on all groups accoring to the process/prefix convention."
4981   (interactive "P")
4982   (let ((groups (or groups (gnus-group-process-prefix arg)))
4983         group func)
4984     (if (eq (setq func (or func
4985                            (key-binding
4986                             (read-key-sequence
4987                              (substitute-command-keys
4988                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
4989             'undefined)
4990         (progn
4991           (message "Undefined key")
4992           (ding))
4993       (while groups
4994         (gnus-group-remove-mark (setq group (pop groups)))
4995         (command-execute func))))
4996   (gnus-group-position-point))
4997
4998 (defun gnus-group-process-prefix (n)
4999   "Return a list of groups to work on.
5000 Take into consideration N (the prefix) and the list of marked groups."
5001   (cond
5002    (n
5003     (setq n (prefix-numeric-value n))
5004     ;; There is a prefix, so we return a list of the N next
5005     ;; groups.
5006     (let ((way (if (< n 0) -1 1))
5007           (n (abs n))
5008           group groups)
5009       (save-excursion
5010         (while (and (> n 0)
5011                     (setq group (gnus-group-group-name)))
5012           (setq groups (cons group groups))
5013           (setq n (1- n))
5014           (gnus-group-next-group way)))
5015       (nreverse groups)))
5016    ((and (boundp 'transient-mark-mode)
5017          transient-mark-mode
5018          mark-active)
5019     ;; Work on the region between point and mark.
5020     (let ((max (max (point) (mark)))
5021           groups)
5022       (save-excursion
5023         (goto-char (min (point) (mark)))
5024         (while
5025             (and
5026              (push (gnus-group-group-name) groups)
5027              (zerop (gnus-group-next-group 1))
5028              (< (point) max)))
5029         (nreverse groups))))
5030    (gnus-group-marked
5031     ;; No prefix, but a list of marked articles.
5032     (reverse gnus-group-marked))
5033    (t
5034     ;; Neither marked articles or a prefix, so we return the
5035     ;; current group.
5036     (let ((group (gnus-group-group-name)))
5037       (and group (list group))))))
5038
5039 ;; Selecting groups.
5040
5041 (defun gnus-group-read-group (&optional all no-article group)
5042   "Read news in this newsgroup.
5043 If the prefix argument ALL is non-nil, already read articles become
5044 readable.  IF ALL is a number, fetch this number of articles.  If the
5045 optional argument NO-ARTICLE is non-nil, no article will be
5046 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5047 group."
5048   (interactive "P")
5049   (let ((group (or group (gnus-group-group-name)))
5050         number active marked entry)
5051     (or group (error "No group on current line"))
5052     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5053                                             group gnus-newsrc-hashtb)))))
5054     ;; This group might be a dead group.  In that case we have to get
5055     ;; the number of unread articles from `gnus-active-hashtb'.
5056     (setq number
5057           (cond ((numberp all) all)
5058                 (entry (car entry))
5059                 ((setq active (gnus-active group))
5060                  (- (1+ (cdr active)) (car active)))))
5061     (gnus-summary-read-group
5062      group (or all (and (numberp number)
5063                         (zerop (+ number (length (cdr (assq 'tick marked)))
5064                                   (length (cdr (assq 'dormant marked)))))))
5065      no-article)))
5066
5067 (defun gnus-group-select-group (&optional all)
5068   "Select this newsgroup.
5069 No article is selected automatically.
5070 If ALL is non-nil, already read articles become readable.
5071 If ALL is a number, fetch this number of articles."
5072   (interactive "P")
5073   (gnus-group-read-group all t))
5074
5075 (defun gnus-group-quick-select-group (&optional all)
5076   "Select the current group \"quickly\".
5077 This means that no highlighting or scoring will be performed."
5078   (interactive "P")
5079   (let (gnus-visual
5080         gnus-score-find-score-files-function
5081         gnus-apply-kill-hook
5082         gnus-summary-expunge-below)
5083     (gnus-group-read-group all t)))
5084
5085 (defun gnus-group-visible-select-group (&optional all)
5086   "Select the current group without hiding any articles."
5087   (interactive "P")
5088   (let ((gnus-inhibit-limiting t))
5089     (gnus-group-read-group all t)))
5090
5091 ;;;###autoload
5092 (defun gnus-fetch-group (group)
5093   "Start Gnus if necessary and enter GROUP.
5094 Returns whether the fetching was successful or not."
5095   (interactive "sGroup name: ")
5096   (or (get-buffer gnus-group-buffer)
5097       (gnus))
5098   (gnus-group-select-group))
5099
5100 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5101 ;; if selection was successful.
5102 (defun gnus-group-read-ephemeral-group
5103   (group method &optional activate quit-config)
5104   (let ((group (if (gnus-group-foreign-p group) group
5105                  (gnus-group-prefixed-name group method))))
5106     (gnus-sethash
5107      group
5108      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5109                      ((quit-config . ,(if quit-config quit-config
5110                                         (cons (current-buffer) 'summary))))))
5111      gnus-newsrc-hashtb)
5112     (set-buffer gnus-group-buffer)
5113     (or (gnus-check-server method)
5114         (error "Unable to contact server: %s" (gnus-status-message method)))
5115     (if activate (or (gnus-request-group group)
5116                      (error "Couldn't request group")))
5117     (condition-case ()
5118         (gnus-group-read-group t t group)
5119       (error nil)
5120       (quit nil))))
5121
5122 (defun gnus-group-jump-to-group (group)
5123   "Jump to newsgroup GROUP."
5124   (interactive
5125    (list (completing-read
5126           "Group: " gnus-active-hashtb nil
5127           (memq gnus-select-method gnus-have-read-active-file))))
5128
5129   (if (equal group "")
5130       (error "Empty group name"))
5131
5132   (let ((b (text-property-any
5133             (point-min) (point-max)
5134             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5135     (unless (gnus-ephemeral-group-p group)
5136       (if b
5137           ;; Either go to the line in the group buffer...
5138           (goto-char b)
5139         ;; ... or insert the line.
5140         (or
5141          (gnus-active group)
5142          (gnus-activate-group group)
5143          (error "%s error: %s" group (gnus-status-message group)))
5144
5145         (gnus-group-update-group group)
5146         (goto-char (text-property-any
5147                     (point-min) (point-max)
5148                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5149     ;; Adjust cursor point.
5150     (gnus-group-position-point)))
5151
5152 (defun gnus-group-goto-group (group)
5153   "Goto to newsgroup GROUP."
5154   (when group
5155     (let ((b (text-property-any (point-min) (point-max)
5156                                 'gnus-group (gnus-intern-safe
5157                                              group gnus-active-hashtb))))
5158       (and b (goto-char b)))))
5159
5160 (defun gnus-group-next-group (n)
5161   "Go to next N'th newsgroup.
5162 If N is negative, search backward instead.
5163 Returns the difference between N and the number of skips actually
5164 done."
5165   (interactive "p")
5166   (gnus-group-next-unread-group n t))
5167
5168 (defun gnus-group-next-unread-group (n &optional all level)
5169   "Go to next N'th unread newsgroup.
5170 If N is negative, search backward instead.
5171 If ALL is non-nil, choose any newsgroup, unread or not.
5172 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5173 such group can be found, the next group with a level higher than
5174 LEVEL.
5175 Returns the difference between N and the number of skips actually
5176 made."
5177   (interactive "p")
5178   (let ((backward (< n 0))
5179         (n (abs n)))
5180     (while (and (> n 0)
5181                 (gnus-group-search-forward
5182                  backward (or (not gnus-group-goto-unread) all) level))
5183       (setq n (1- n)))
5184     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5185                                (if level " on this level or higher" "")))
5186     n))
5187
5188 (defun gnus-group-prev-group (n)
5189   "Go to previous N'th newsgroup.
5190 Returns the difference between N and the number of skips actually
5191 done."
5192   (interactive "p")
5193   (gnus-group-next-unread-group (- n) t))
5194
5195 (defun gnus-group-prev-unread-group (n)
5196   "Go to previous N'th unread newsgroup.
5197 Returns the difference between N and the number of skips actually
5198 done."
5199   (interactive "p")
5200   (gnus-group-next-unread-group (- n)))
5201
5202 (defun gnus-group-next-unread-group-same-level (n)
5203   "Go to next N'th unread newsgroup on the same level.
5204 If N is negative, search backward instead.
5205 Returns the difference between N and the number of skips actually
5206 done."
5207   (interactive "p")
5208   (gnus-group-next-unread-group n t (gnus-group-group-level))
5209   (gnus-group-position-point))
5210
5211 (defun gnus-group-prev-unread-group-same-level (n)
5212   "Go to next N'th unread newsgroup on the same level.
5213 Returns the difference between N and the number of skips actually
5214 done."
5215   (interactive "p")
5216   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5217   (gnus-group-position-point))
5218
5219 (defun gnus-group-best-unread-group (&optional exclude-group)
5220   "Go to the group with the highest level.
5221 If EXCLUDE-GROUP, do not go to that group."
5222   (interactive)
5223   (goto-char (point-min))
5224   (let ((best 100000)
5225         unread best-point)
5226     (while (setq unread (get-text-property (point) 'gnus-unread))
5227       (if (and (numberp unread) (> unread 0))
5228           (progn
5229             (if (and (< (get-text-property (point) 'gnus-level) best)
5230                      (or (not exclude-group)
5231                          (not (equal exclude-group (gnus-group-group-name)))))
5232                 (progn
5233                   (setq best (get-text-property (point) 'gnus-level))
5234                   (setq best-point (point))))))
5235       (forward-line 1))
5236     (if best-point (goto-char best-point))
5237     (gnus-summary-position-point)
5238     (and best-point (gnus-group-group-name))))
5239
5240 (defun gnus-group-first-unread-group ()
5241   "Go to the first group with unread articles."
5242   (interactive)
5243   (prog1
5244       (let ((opoint (point))
5245             unread)
5246         (goto-char (point-min))
5247         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5248                 (and (numberp unread)   ; Not a topic.
5249                      (not (zerop unread))) ; Has unread articles.
5250                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5251             (point)                     ; Success.
5252           (goto-char opoint)
5253           nil))                         ; Not success.
5254     (gnus-group-position-point)))
5255
5256 (defun gnus-group-enter-server-mode ()
5257   "Jump to the server buffer."
5258   (interactive)
5259   (gnus-enter-server-buffer))
5260
5261 (defun gnus-group-make-group (name &optional method address)
5262   "Add a new newsgroup.
5263 The user will be prompted for a NAME, for a select METHOD, and an
5264 ADDRESS."
5265   (interactive
5266    (cons
5267     (read-string "Group name: ")
5268     (let ((method
5269            (completing-read
5270             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5271             nil t)))
5272       (if (assoc method gnus-valid-select-methods)
5273           (list method
5274                 (if (memq 'prompt-address
5275                           (assoc method gnus-valid-select-methods))
5276                     (read-string "Address: ")
5277                   ""))
5278         (list method "")))))
5279
5280   (save-excursion
5281     (set-buffer gnus-group-buffer)
5282     (let* ((meth (and method (if address (list (intern method) address)
5283                                method)))
5284            (nname (if method (gnus-group-prefixed-name name meth) name))
5285            info)
5286       (and (gnus-gethash nname gnus-newsrc-hashtb)
5287            (error "Group %s already exists" nname))
5288       (gnus-group-change-level
5289        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5290        gnus-level-default-subscribed gnus-level-killed
5291        (and (gnus-group-group-name)
5292             (gnus-gethash (gnus-group-group-name)
5293                           gnus-newsrc-hashtb))
5294        t)
5295       (gnus-set-active nname (cons 1 0))
5296       (or (gnus-ephemeral-group-p name)
5297           (gnus-dribble-enter
5298            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5299       (gnus-group-insert-group-line-info nname)
5300
5301       (when (assoc (symbol-name (car method)) gnus-valid-select-methods)
5302         (require (car method)))
5303       (gnus-check-server method)
5304       (and (gnus-check-backend-function 'request-create-group nname)
5305            (gnus-request-create-group nname))
5306       t)))
5307
5308 (defun gnus-group-delete-group (group &optional force)
5309   "Delete the current group.
5310 If FORCE (the prefix) is non-nil, all the articles in the group will
5311 be deleted.  This is \"deleted\" as in \"removed forever from the face
5312 of the Earth\".  There is no undo."
5313   (interactive
5314    (list (gnus-group-group-name)
5315          current-prefix-arg))
5316   (or group (error "No group to rename"))
5317   (or (gnus-check-backend-function 'request-delete-group group)
5318       (error "This backend does not support group deletion"))
5319   (prog1
5320       (if (not (gnus-yes-or-no-p
5321                 (format
5322                  "Do you really want to delete %s%s? "
5323                  group (if force " and all its contents" ""))))
5324           () ; Whew!
5325         (gnus-message 6 "Deleting group %s..." group)
5326         (if (not (gnus-request-delete-group group force))
5327             (progn
5328               (gnus-message 3 "Couldn't delete group %s" group)
5329               (ding))
5330           (gnus-message 6 "Deleting group %s...done" group)
5331           (gnus-group-goto-group group)
5332           (gnus-group-kill-group 1 t)
5333           t))
5334     (gnus-group-position-point)))
5335
5336 (defun gnus-group-rename-group (group new-name)
5337   (interactive
5338    (list
5339     (gnus-group-group-name)
5340     (progn
5341       (or (gnus-check-backend-function
5342            'request-rename-group (gnus-group-group-name))
5343           (error "This backend does not support renaming groups"))
5344       (read-string "New group name: "))))
5345
5346   (or (gnus-check-backend-function 'request-rename-group group)
5347       (error "This backend does not support renaming groups"))
5348
5349   (or group (error "No group to rename"))
5350   (and (string-match "^[ \t]*$" new-name)
5351        (error "Not a valid group name"))
5352
5353   ;; We find the proper prefixed name.
5354   (setq new-name
5355         (gnus-group-prefixed-name
5356          (gnus-group-real-name new-name)
5357          (gnus-info-method (gnus-get-info group))))
5358
5359   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5360   (prog1
5361       (if (not (gnus-request-rename-group group new-name))
5362           (progn
5363             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5364             (ding))
5365         ;; We rename the group internally by killing it...
5366         (gnus-group-goto-group group)
5367         (gnus-group-kill-group)
5368         ;; ... changing its name ...
5369         (setcar (cdr (car gnus-list-of-killed-groups))
5370                 new-name)
5371         ;; ... and then yanking it.  Magic!
5372         (gnus-group-yank-group)
5373         (gnus-set-active new-name (gnus-active group))
5374         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5375         new-name)
5376     (gnus-group-position-point)))
5377
5378 (defun gnus-group-edit-group (group &optional part)
5379   "Edit the group on the current line."
5380   (interactive (list (gnus-group-group-name)))
5381   (let ((done-func '(lambda ()
5382                       "Exit editing mode and update the information."
5383                       (interactive)
5384                       (gnus-group-edit-group-done 'part 'group)))
5385         (part (or part 'info))
5386         (winconf (current-window-configuration))
5387         info)
5388     (or group (error "No group on current line"))
5389     (or (setq info (gnus-get-info group))
5390         (error "Killed group; can't be edited"))
5391     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5392     (gnus-configure-windows 'edit-group)
5393     (gnus-add-current-to-buffer-list)
5394     (emacs-lisp-mode)
5395     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5396     (use-local-map (copy-keymap emacs-lisp-mode-map))
5397     (local-set-key "\C-c\C-c" done-func)
5398     (make-local-variable 'gnus-prev-winconf)
5399     (setq gnus-prev-winconf winconf)
5400     ;; We modify the func to let it know what part it is editing.
5401     (setcar (cdr (nth 4 done-func)) (list 'quote part))
5402     (setcar (cdr (cdr (nth 4 done-func))) group)
5403     (erase-buffer)
5404     (insert
5405      (cond
5406       ((eq part 'method)
5407        ";; Type `C-c C-c' after editing the select method.\n\n")
5408       ((eq part 'params)
5409        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5410       ((eq part 'info)
5411        ";; Type `C-c C-c' after editing the group info.\n\n")))
5412     (insert
5413      (pp-to-string
5414       (cond ((eq part 'method)
5415              (or (gnus-info-method info) "native"))
5416             ((eq part 'params)
5417              (gnus-info-params info))
5418             (t info)))
5419      "\n")))
5420
5421 (defun gnus-group-edit-group-method (group)
5422   "Edit the select method of GROUP."
5423   (interactive (list (gnus-group-group-name)))
5424   (gnus-group-edit-group group 'method))
5425
5426 (defun gnus-group-edit-group-parameters (group)
5427   "Edit the group parameters of GROUP."
5428   (interactive (list (gnus-group-group-name)))
5429   (gnus-group-edit-group group 'params))
5430
5431 (defun gnus-group-edit-group-done (part group)
5432   "Get info from buffer, update variables and jump to the group buffer."
5433   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5434   (goto-char (point-min))
5435   (let* ((form (read (current-buffer)))
5436          (winconf gnus-prev-winconf)
5437          (new-group (when (eq part 'info)
5438                       (if (or (not (nth 4 form))
5439                               (gnus-server-equal
5440                                gnus-select-method (nth 4 form)))
5441                           (gnus-group-real-name (car form))
5442                         (gnus-group-prefixed-name
5443                          (gnus-group-real-name (car form)) (nth 4 form))))))
5444     ;; Set the info.
5445     (if (eq part 'info)
5446         (progn
5447           (when new-group (setcar form new-group))
5448           (gnus-group-set-info form))
5449       (gnus-group-set-info form group part))
5450     (kill-buffer (current-buffer))
5451     (and winconf (set-window-configuration winconf))
5452     (set-buffer gnus-group-buffer)
5453     (when (and new-group
5454              (not (equal new-group group)))
5455       (when (gnus-group-goto-group group)
5456         (gnus-group-kill-group 1))
5457       (gnus-activate-group new-group))
5458     (gnus-group-update-group (or new-group group))
5459     (gnus-group-position-point)))
5460
5461 (defun gnus-group-make-help-group ()
5462   "Create the Gnus documentation group."
5463   (interactive)
5464   (let ((path load-path)
5465         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5466         file dir)
5467     (and (gnus-gethash name gnus-newsrc-hashtb)
5468          (error "Documentation group already exists"))
5469     (while path
5470       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5471             file nil)
5472       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5473                 (file-exists-p
5474                  (setq file (concat (file-name-directory
5475                                      (directory-file-name dir))
5476                                     "etc/gnus-tut.txt"))))
5477         (setq path nil)))
5478     (if (not file)
5479         (message "Couldn't find doc group")
5480       (gnus-group-make-group
5481        (gnus-group-real-name name)
5482        (list 'nndoc name
5483              (list 'nndoc-address file)
5484              (list 'nndoc-article-type 'mbox)))))
5485   (gnus-group-position-point))
5486
5487 (defun gnus-group-make-doc-group (file type)
5488   "Create a group that uses a single file as the source."
5489   (interactive
5490    (list (read-file-name "File name: ")
5491          (and current-prefix-arg 'ask)))
5492   (when (eq type 'ask)
5493     (let ((err "")
5494           char found)
5495       (while (not found)
5496         (message
5497          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5498          err)
5499         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5500                           ((= char ?b) 'babyl)
5501                           ((= char ?d) 'digest)
5502                           ((= char ?f) 'forward)
5503                           ((= char ?a) 'mmfd)
5504                           (t (setq err (format "%c unknown. " char))
5505                              nil))))
5506       (setq type found)))
5507   (let* ((file (expand-file-name file))
5508          (name (gnus-generate-new-group-name
5509                 (gnus-group-prefixed-name
5510                  (file-name-nondirectory file) '(nndoc "")))))
5511     (gnus-group-make-group
5512      (gnus-group-real-name name)
5513      (list 'nndoc name
5514            (list 'nndoc-address file)
5515            (list 'nndoc-article-type (or type 'guess))))
5516     (forward-line -1)
5517     (gnus-group-position-point)))
5518
5519 (defun gnus-group-make-archive-group (&optional all)
5520   "Create the (ding) Gnus archive group of the most recent articles.
5521 Given a prefix, create a full group."
5522   (interactive "P")
5523   (let ((group (gnus-group-prefixed-name
5524                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5525     (and (gnus-gethash group gnus-newsrc-hashtb)
5526          (error "Archive group already exists"))
5527     (gnus-group-make-group
5528      (gnus-group-real-name group)
5529      (list 'nndir (if all "hpc" "edu")
5530            (list 'nndir-directory
5531                  (if all gnus-group-archive-directory
5532                    gnus-group-recent-archive-directory)))))
5533   (forward-line -1)
5534   (gnus-group-position-point))
5535
5536 (defun gnus-group-make-directory-group (dir)
5537   "Create an nndir group.
5538 The user will be prompted for a directory.  The contents of this
5539 directory will be used as a newsgroup.  The directory should contain
5540 mail messages or news articles in files that have numeric names."
5541   (interactive
5542    (list (read-file-name "Create group from directory: ")))
5543   (or (file-exists-p dir) (error "No such directory"))
5544   (or (file-directory-p dir) (error "Not a directory"))
5545   (let ((ext "")
5546         (i 0)
5547         group)
5548     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5549       (setq group
5550             (gnus-group-prefixed-name
5551              (concat (file-name-as-directory (directory-file-name dir))
5552                      ext)
5553              '(nndir "")))
5554       (setq ext (format "<%d>" (setq i (1+ i)))))
5555     (gnus-group-make-group
5556      (gnus-group-real-name group)
5557      (list 'nndir group (list 'nndir-directory dir))))
5558   (forward-line -1)
5559   (gnus-group-position-point))
5560
5561 (defun gnus-group-make-kiboze-group (group address scores)
5562   "Create an nnkiboze group.
5563 The user will be prompted for a name, a regexp to match groups, and
5564 score file entries for articles to include in the group."
5565   (interactive
5566    (list
5567     (read-string "nnkiboze group name: ")
5568     (read-string "Source groups (regexp): ")
5569     (let ((headers (mapcar (lambda (group) (list group))
5570                            '("subject" "from" "number" "date" "message-id"
5571                              "references" "chars" "lines" "xref"
5572                              "followup" "all" "body" "head")))
5573           scores header regexp regexps)
5574       (while (not (equal "" (setq header (completing-read
5575                                           "Match on header: " headers nil t))))
5576         (setq regexps nil)
5577         (while (not (equal "" (setq regexp (read-string
5578                                             (format "Match on %s (string): "
5579                                                     header)))))
5580           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5581         (setq scores (cons (cons header regexps) scores)))
5582       scores)))
5583   (gnus-group-make-group group "nnkiboze" address)
5584   (save-excursion
5585     (gnus-set-work-buffer)
5586     (let (emacs-lisp-mode-hook)
5587       (pp scores (current-buffer)))
5588     (write-region (point-min) (point-max)
5589                   (gnus-score-file-name (concat "nnkiboze:" group))))
5590   (forward-line -1)
5591   (gnus-group-position-point))
5592
5593 (defun gnus-group-add-to-virtual (n vgroup)
5594   "Add the current group to a virtual group."
5595   (interactive
5596    (list current-prefix-arg
5597          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5598                           "nnvirtual:")))
5599   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5600       (error "%s is not an nnvirtual group" vgroup))
5601   (let* ((groups (gnus-group-process-prefix n))
5602          (method (gnus-info-method (gnus-get-info vgroup))))
5603     (setcar (cdr method)
5604             (concat
5605              (nth 1 method) "\\|"
5606              (mapconcat
5607               (lambda (s)
5608                 (gnus-group-remove-mark s)
5609                 (concat "\\(^" (regexp-quote s) "$\\)"))
5610               groups "\\|"))))
5611   (gnus-group-position-point))
5612
5613 (defun gnus-group-make-empty-virtual (group)
5614   "Create a new, fresh, empty virtual group."
5615   (interactive "sCreate new, empty virtual group: ")
5616   (let* ((method (list 'nnvirtual "^$"))
5617          (pgroup (gnus-group-prefixed-name group method)))
5618     ;; Check whether it exists already.
5619     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5620          (error "Group %s already exists." pgroup))
5621     ;; Subscribe the new group after the group on the current line.
5622     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5623     (gnus-group-update-group pgroup)
5624     (forward-line -1)
5625     (gnus-group-position-point)))
5626
5627 (defun gnus-group-enter-directory (dir)
5628   "Enter an ephemeral nneething group."
5629   (interactive "DDirectory to read: ")
5630   (let* ((method (list 'nneething dir))
5631          (leaf (gnus-group-prefixed-name
5632                 (file-name-nondirectory (directory-file-name dir))
5633                 method))
5634          (name (gnus-generate-new-group-name leaf)))
5635     (let ((nneething-read-only t))
5636       (or (gnus-group-read-ephemeral-group
5637            name method t
5638            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5639                                       'summary 'group)))
5640           (error "Couldn't enter %s" dir)))))
5641
5642 ;; Group sorting commands
5643 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5644
5645 (defun gnus-group-sort-groups (func &optional reverse)
5646   "Sort the group buffer according to FUNC.
5647 If REVERSE, reverse the sorting order."
5648   (interactive (list gnus-group-sort-function
5649                      current-prefix-arg))
5650   (let ((func (cond 
5651                ((not (listp func)) func)
5652                ((null func) func)
5653                ((= 1 (length func)) (car func))
5654                (t `(lambda (t1 t2)
5655                      ,(gnus-make-sort-function 
5656                        (reverse func)))))))
5657     ;; We peel off the dummy group from the alist.
5658     (when func
5659       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5660         (pop gnus-newsrc-alist))
5661       ;; Do the sorting.
5662       (setq gnus-newsrc-alist
5663             (sort gnus-newsrc-alist func))
5664       (when reverse
5665         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5666       ;; Regenerate the hash table.
5667       (gnus-make-hashtable-from-newsrc-alist)
5668       (gnus-group-list-groups))))
5669
5670 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5671   "Sort the group buffer alphabetically by group name.
5672 If REVERSE, sort in reverse order."
5673   (interactive "P")
5674   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5675
5676 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5677   "Sort the group buffer by number of unread articles.
5678 If REVERSE, sort in reverse order."
5679   (interactive "P")
5680   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5681
5682 (defun gnus-group-sort-groups-by-level (&optional reverse)
5683   "Sort the group buffer by group level.
5684 If REVERSE, sort in reverse order."
5685   (interactive "P")
5686   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5687
5688 (defun gnus-group-sort-groups-by-score (&optional reverse)
5689   "Sort the group buffer by group score.
5690 If REVERSE, sort in reverse order."
5691   (interactive "P")
5692   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5693
5694 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5695   "Sort the group buffer by group rank.
5696 If REVERSE, sort in reverse order."
5697   (interactive "P")
5698   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5699
5700 (defun gnus-group-sort-groups-by-method (&optional reverse)
5701   "Sort the group buffer alphabetically by backend name.
5702 If REVERSE, sort in reverse order."
5703   (interactive "P")
5704   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5705
5706 (defun gnus-group-sort-by-alphabet (info1 info2)
5707   "Sort alphabetically."
5708   (string< (gnus-info-group info1) (gnus-info-group info2)))
5709
5710 (defun gnus-group-sort-by-unread (info1 info2)
5711   "Sort by number of unread articles."
5712   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5713         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5714     (< (or (and (numberp n1) n1) 0)
5715        (or (and (numberp n2) n2) 0))))
5716
5717 (defun gnus-group-sort-by-level (info1 info2)
5718   "Sort by level."
5719   (< (gnus-info-level info1) (gnus-info-level info2)))
5720
5721 (defun gnus-group-sort-by-method (info1 info2)
5722   "Sort alphabetically by backend name."
5723   (string< (symbol-name (car (gnus-find-method-for-group
5724                               (gnus-info-group info1) info1)))
5725            (symbol-name (car (gnus-find-method-for-group
5726                               (gnus-info-group info2) info2)))))
5727
5728 (defun gnus-group-sort-by-score (info1 info2)
5729   "Sort by group score."
5730   (< (gnus-info-score info1) (gnus-info-score info2)))
5731
5732 (defun gnus-group-sort-by-rank (info1 info2)
5733   "Sort by level and score."
5734   (let ((level1 (gnus-info-level info1))
5735         (level2 (gnus-info-level info2)))
5736     (or (< level1 level2)
5737         (and (= level1 level2)
5738              (< (gnus-info-score info1) (gnus-info-score info2))))))
5739
5740 ;; Group catching up.
5741
5742 (defun gnus-group-catchup-current (&optional n all)
5743   "Mark all articles not marked as unread in current newsgroup as read.
5744 If prefix argument N is numeric, the ARG next newsgroups will be
5745 caught up.  If ALL is non-nil, marked articles will also be marked as
5746 read.  Cross references (Xref: header) of articles are ignored.
5747 The difference between N and actual number of newsgroups that were
5748 caught up is returned."
5749   (interactive "P")
5750   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5751                gnus-expert-user
5752                (gnus-y-or-n-p
5753                 (if all
5754                     "Do you really want to mark all articles as read? "
5755                   "Mark all unread articles as read? "))))
5756       n
5757     (let ((groups (gnus-group-process-prefix n))
5758           (ret 0))
5759       (while groups
5760         ;; Virtual groups have to be given special treatment.
5761         (let ((method (gnus-find-method-for-group (car groups))))
5762           (if (eq 'nnvirtual (car method))
5763               (nnvirtual-catchup-group
5764                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5765         (gnus-group-remove-mark (car groups))
5766         (if (prog1
5767                 (gnus-group-goto-group (car groups))
5768               (gnus-group-catchup (car groups) all))
5769             (gnus-group-update-group-line)
5770           (setq ret (1+ ret)))
5771         (setq groups (cdr groups)))
5772       (gnus-group-next-unread-group 1)
5773       ret)))
5774
5775 (defun gnus-group-catchup-current-all (&optional n)
5776   "Mark all articles in current newsgroup as read.
5777 Cross references (Xref: header) of articles are ignored."
5778   (interactive "P")
5779   (gnus-group-catchup-current n 'all))
5780
5781 (defun gnus-group-catchup (group &optional all)
5782   "Mark all articles in GROUP as read.
5783 If ALL is non-nil, all articles are marked as read.
5784 The return value is the number of articles that were marked as read,
5785 or nil if no action could be taken."
5786   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5787          (num (car entry)))
5788     ;; Do the updating only if the newsgroup isn't killed.
5789     (if (not (numberp (car entry)))
5790         (gnus-message 1 "Can't catch up; non-active group")
5791       ;; Do auto-expirable marks if that's required.
5792       (when (gnus-group-auto-expirable-p group)
5793         (gnus-add-marked-articles
5794          group 'expire (gnus-list-of-unread-articles group))
5795         (when all
5796           (let ((marks (nth 3 (nth 2 entry))))
5797             (gnus-add-marked-articles
5798              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5799             (gnus-add-marked-articles
5800              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5801       (when entry
5802         (gnus-update-read-articles group nil)
5803         ;; Also nix out the lists of marks and dormants.
5804         (when all
5805           (gnus-add-marked-articles group 'tick nil nil 'force)
5806           (gnus-add-marked-articles group 'dormant nil nil 'force))
5807         (run-hooks 'gnus-group-catchup-group-hook)
5808         num))))
5809
5810 (defun gnus-group-expire-articles (&optional n)
5811   "Expire all expirable articles in the current newsgroup."
5812   (interactive "P")
5813   (let ((groups (gnus-group-process-prefix n))
5814         group)
5815     (unless groups
5816       (error "No groups to expire"))
5817     (while (setq group (pop groups))
5818       (gnus-group-remove-mark group)
5819       (when (gnus-check-backend-function 'request-expire-articles group)
5820         (gnus-message 6 "Expiring articles in %s..." group)
5821         (let* ((info (gnus-get-info group))
5822                (expirable (if (gnus-group-total-expirable-p group)
5823                               (cons nil (gnus-list-of-read-articles group))
5824                             (assq 'expire (gnus-info-marks info))))
5825                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5826           (when expirable
5827             (setcdr
5828              expirable
5829              (gnus-compress-sequence
5830               (if expiry-wait
5831                   ;; We set the expiry variables to the groupp
5832                   ;; parameter. 
5833                   (let ((nnmail-expiry-wait-function nil)
5834                         (nnmail-expiry-wait expiry-wait))
5835                     (gnus-request-expire-articles
5836                      (gnus-uncompress-sequence (cdr expirable)) group))
5837                 ;; Just expire using the normal expiry values.
5838                 (gnus-request-expire-articles
5839                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5840           (gnus-message 6 "Expiring articles in %s...done" group)))
5841       (gnus-group-position-point))))
5842
5843 (defun gnus-group-expire-all-groups ()
5844   "Expire all expirable articles in all newsgroups."
5845   (interactive)
5846   (save-excursion
5847     (gnus-message 5 "Expiring...")
5848     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5849                                      (cdr gnus-newsrc-alist))))
5850       (gnus-group-expire-articles nil)))
5851   (gnus-group-position-point)
5852   (gnus-message 5 "Expiring...done"))
5853
5854 (defun gnus-group-set-current-level (n level)
5855   "Set the level of the next N groups to LEVEL."
5856   (interactive
5857    (list
5858     current-prefix-arg
5859     (if (not (gnus-group-group-name))
5860         (error "No group on the current line")
5861       (string-to-int
5862        (let ((s (read-string
5863                  (format "Level (default %s): " (gnus-group-group-level)))))
5864          (if (string-match "^\\s-*$" s)
5865              (int-to-string (gnus-group-group-level))
5866            s))))))
5867   (or (and (>= level 1) (<= level gnus-level-killed))
5868       (error "Illegal level: %d" level))
5869   (let ((groups (gnus-group-process-prefix n))
5870         group)
5871     (while (setq group (pop groups))
5872       (gnus-group-remove-mark group)
5873       (gnus-message 6 "Changed level of %s from %d to %d"
5874                     group (or (gnus-group-group-level) gnus-level-killed)
5875                     level)
5876       (gnus-group-change-level
5877        group level (or (gnus-group-group-level) gnus-level-killed))
5878       (gnus-group-update-group-line)))
5879   (gnus-group-position-point))
5880
5881 (defun gnus-group-unsubscribe-current-group (&optional n)
5882   "Toggle subscription of the current group.
5883 If given numerical prefix, toggle the N next groups."
5884   (interactive "P")
5885   (let ((groups (gnus-group-process-prefix n))
5886         group)
5887     (while groups
5888       (setq group (car groups)
5889             groups (cdr groups))
5890       (gnus-group-remove-mark group)
5891       (gnus-group-unsubscribe-group
5892        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5893                  gnus-level-default-unsubscribed
5894                gnus-level-default-subscribed) t)
5895       (gnus-group-update-group-line))
5896     (gnus-group-next-group 1)))
5897
5898 (defun gnus-group-unsubscribe-group (group &optional level silent)
5899   "Toggle subscription to GROUP.
5900 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5901 group line."
5902   (interactive
5903    (list (completing-read
5904           "Group: " gnus-active-hashtb nil
5905           (memq gnus-select-method gnus-have-read-active-file))))
5906   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5907     (cond
5908      ((string-match "^[ \t]$" group)
5909       (error "Empty group name"))
5910      (newsrc
5911       ;; Toggle subscription flag.
5912       (gnus-group-change-level
5913        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
5914                                       gnus-level-subscribed)
5915                                   (1+ gnus-level-subscribed)
5916                                 gnus-level-default-subscribed)))
5917       (unless silent
5918         (gnus-group-update-group group)))
5919      ((and (stringp group)
5920            (or (not (memq gnus-select-method gnus-have-read-active-file))
5921                (gnus-active group)))
5922       ;; Add new newsgroup.
5923       (gnus-group-change-level
5924        group
5925        (if level level gnus-level-default-subscribed)
5926        (or (and (member group gnus-zombie-list)
5927                 gnus-level-zombie)
5928            gnus-level-killed)
5929        (and (gnus-group-group-name)
5930             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5931       (unless silent
5932         (gnus-group-update-group group)))
5933      (t (error "No such newsgroup: %s" group)))
5934     (gnus-group-position-point)))
5935
5936 (defun gnus-group-transpose-groups (n)
5937   "Move the current newsgroup up N places.
5938 If given a negative prefix, move down instead.  The difference between
5939 N and the number of steps taken is returned."
5940   (interactive "p")
5941   (or (gnus-group-group-name)
5942       (error "No group on current line"))
5943   (gnus-group-kill-group 1)
5944   (prog1
5945       (forward-line (- n))
5946     (gnus-group-yank-group)
5947     (gnus-group-position-point)))
5948
5949 (defun gnus-group-kill-all-zombies ()
5950   "Kill all zombie newsgroups."
5951   (interactive)
5952   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
5953   (setq gnus-zombie-list nil)
5954   (gnus-group-list-groups))
5955
5956 (defun gnus-group-kill-region (begin end)
5957   "Kill newsgroups in current region (excluding current point).
5958 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
5959   (interactive "r")
5960   (let ((lines
5961          ;; Count lines.
5962          (save-excursion
5963            (count-lines
5964             (progn
5965               (goto-char begin)
5966               (beginning-of-line)
5967               (point))
5968             (progn
5969               (goto-char end)
5970               (beginning-of-line)
5971               (point))))))
5972     (goto-char begin)
5973     (beginning-of-line)                 ;Important when LINES < 1
5974     (gnus-group-kill-group lines)))
5975
5976 (defun gnus-group-kill-group (&optional n discard)
5977   "Kill the next N groups.
5978 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
5979 However, only groups that were alive can be yanked; already killed
5980 groups or zombie groups can't be yanked.
5981 The return value is the name of the group that was killed, or a list
5982 of groups killed."
5983   (interactive "P")
5984   (let ((buffer-read-only nil)
5985         (groups (gnus-group-process-prefix n))
5986         group entry level out)
5987     (if (< (length groups) 10)
5988         ;; This is faster when there are few groups.
5989         (while groups
5990           (push (setq group (pop groups)) out)
5991           (gnus-group-remove-mark group)
5992           (setq level (gnus-group-group-level))
5993           (gnus-delete-line)
5994           (if (and (not discard)
5995                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
5996               (setq gnus-list-of-killed-groups
5997                     (cons (cons (car entry) (nth 2 entry))
5998                           gnus-list-of-killed-groups)))
5999           (gnus-group-change-level
6000            (if entry entry group) gnus-level-killed (if entry nil level)))
6001       ;; If there are lots and lots of groups to be killed, we use
6002       ;; this thing instead.
6003       (let (entry)
6004         (setq groups (nreverse groups))
6005         (while groups
6006           (gnus-group-remove-mark (setq group (pop groups)))
6007           (gnus-delete-line)
6008           (cond
6009            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6010             (push (cons (car entry) (nth 2 entry))
6011                   gnus-list-of-killed-groups)
6012             (setcdr (cdr entry) (cdr (cdr (cdr entry)))))
6013            ((member group gnus-zombie-list)
6014             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6015         (gnus-make-hashtable-from-newsrc-alist)))
6016
6017     (gnus-group-position-point)
6018     (if (< (length out) 2) (car out) (nreverse out))))
6019
6020 (defun gnus-group-yank-group (&optional arg)
6021   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6022 inserting it before the current newsgroup.  The numeric ARG specifies
6023 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6024 is returned, or (if several groups are yanked) a list of yanked groups
6025 is returned."
6026   (interactive "p")
6027   (setq arg (or arg 1))
6028   (let (info group prev out)
6029     (while (>= (decf arg) 0)
6030       (if (not (setq info (pop gnus-list-of-killed-groups)))
6031           (error "No more newsgroups to yank"))
6032       (push (setq group (nth 1 info)) out)
6033       ;; Find which newsgroup to insert this one before - search
6034       ;; backward until something suitable is found.  If there are no
6035       ;; other newsgroups in this buffer, just make this newsgroup the
6036       ;; first newsgroup.
6037       (setq prev (gnus-group-group-name))
6038       (gnus-group-change-level
6039        info (nth 2 info) gnus-level-killed
6040        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6041        t)
6042       (gnus-group-insert-group-line-info group))
6043     (forward-line -1)
6044     (gnus-group-position-point)
6045     (if (< (length out) 2) (car out) (nreverse out))))
6046
6047 (defun gnus-group-kill-level (level)
6048   "Kill all groups that is on a certain LEVEL."
6049   (interactive "nKill all groups on level: ")
6050   (cond
6051    ((= level gnus-level-zombie)
6052     (setq gnus-killed-list
6053           (nconc gnus-zombie-list gnus-killed-list))
6054     (setq gnus-zombie-list nil))
6055    ((and (< level gnus-level-zombie)
6056          (> level 0)
6057          (or gnus-expert-user
6058              (gnus-yes-or-no-p
6059               (format
6060                "Do you really want to kill all groups on level %d? "
6061                level))))
6062     (let* ((prev gnus-newsrc-alist)
6063            (alist (cdr prev)))
6064       (while alist
6065         (if (= (gnus-info-level level) level)
6066             (setcdr prev (cdr alist))
6067           (setq prev alist))
6068         (setq alist (cdr alist)))
6069       (gnus-make-hashtable-from-newsrc-alist)
6070       (gnus-group-list-groups)))
6071    (t
6072     (error "Can't kill; illegal level: %d" level))))
6073
6074 (defun gnus-group-list-all-groups (&optional arg)
6075   "List all newsgroups with level ARG or lower.
6076 Default is gnus-level-unsubscribed, which lists all subscribed and most
6077 unsubscribed groups."
6078   (interactive "P")
6079   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6080
6081 ;; Redefine this to list ALL killed groups if prefix arg used.
6082 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6083 (defun gnus-group-list-killed (&optional arg)
6084   "List all killed newsgroups in the group buffer.
6085 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6086 entail asking the server for the groups."
6087   (interactive "P")
6088   ;; Find all possible killed newsgroups if arg.
6089   (when arg
6090     ;; First make sure active file has been read.
6091     (unless gnus-have-read-active-file
6092       (let ((gnus-read-active-file t))
6093         (gnus-read-active-file)))
6094     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6095     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6096     (mapatoms
6097      (lambda (sym)
6098        (let ((groups 0)
6099              (group (symbol-name sym)))
6100          (if (or (null group)
6101                  (gnus-gethash group gnus-killed-hashtb)
6102                  (gnus-gethash group gnus-newsrc-hashtb))
6103              ()
6104            (let ((do-sub (gnus-matches-options-n group)))
6105              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6106                  ()
6107                (setq groups (1+ groups))
6108                (setq gnus-killed-list
6109                      (cons group gnus-killed-list))
6110                (gnus-sethash group group gnus-killed-hashtb))))))
6111      gnus-active-hashtb))
6112   (if (not gnus-killed-list)
6113       (gnus-message 6 "No killed groups")
6114     (let (gnus-group-list-mode)
6115       (funcall gnus-group-prepare-function
6116                gnus-level-killed t gnus-level-killed))
6117     (goto-char (point-min)))
6118   (gnus-group-position-point))
6119
6120 (defun gnus-group-list-zombies ()
6121   "List all zombie newsgroups in the group buffer."
6122   (interactive)
6123   (if (not gnus-zombie-list)
6124       (gnus-message 6 "No zombie groups")
6125     (let (gnus-group-list-mode)
6126       (funcall gnus-group-prepare-function
6127                gnus-level-zombie t gnus-level-zombie))
6128     (goto-char (point-min)))
6129   (gnus-group-position-point))
6130
6131 (defun gnus-group-list-active ()
6132   "List all groups that are available from the server(s)."
6133   (interactive)
6134   ;; First we make sure that we have really read the active file.
6135   (unless gnus-have-read-active-file
6136     (let ((gnus-read-active-file t))
6137       (gnus-read-active-file)))
6138   ;; Find all groups and sort them.
6139   (let ((groups
6140          (sort
6141           (let (list)
6142             (mapatoms
6143              (lambda (sym)
6144                (and (symbol-value sym)
6145                     (setq list (cons (symbol-name sym) list))))
6146              gnus-active-hashtb)
6147             list)
6148           'string<))
6149         (buffer-read-only nil))
6150     (erase-buffer)
6151     (while groups
6152       (gnus-group-insert-group-line-info (car groups))
6153       (setq groups (cdr groups)))
6154     (goto-char (point-min))))
6155
6156 (defun gnus-activate-all-groups (level)
6157   "Activate absolutely all groups."
6158   (interactive (list 7))
6159   (let ((gnus-activate-level level)
6160         (gnus-activate-foreign-newsgroups level))
6161     (gnus-group-get-new-news)))
6162
6163 (defun gnus-group-get-new-news (&optional arg)
6164   "Get newly arrived articles.
6165 If ARG is a number, it specifies which levels you are interested in
6166 re-scanning.  If ARG is non-nil and not a number, this will force
6167 \"hard\" re-reading of the active files from all servers."
6168   (interactive "P")
6169   (run-hooks 'gnus-get-new-news-hook)
6170   ;; We might read in new NoCeM messages here.
6171   (when gnus-use-nocem 
6172     (gnus-nocem-scan-groups))
6173   ;; If ARG is not a number, then we read the active file.
6174   (when (and arg (not (numberp arg)))
6175     (let ((gnus-read-active-file t))
6176       (gnus-read-active-file))
6177     (setq arg nil))
6178
6179   (setq arg (gnus-group-default-level arg t))
6180   (if (and gnus-read-active-file (not arg))
6181       (progn
6182         (gnus-read-active-file)
6183         (gnus-get-unread-articles arg))
6184     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6185       (gnus-get-unread-articles arg)))
6186   (gnus-group-list-groups))
6187
6188 (defun gnus-group-get-new-news-this-group (&optional n)
6189   "Check for newly arrived news in the current group (and the N-1 next groups).
6190 The difference between N and the number of newsgroup checked is returned.
6191 If N is negative, this group and the N-1 previous groups will be checked."
6192   (interactive "P")
6193   (let* ((groups (gnus-group-process-prefix n))
6194          (ret (if (numberp n) (- n (length groups)) 0))
6195          group)
6196     (while groups
6197       (setq group (car groups)
6198             groups (cdr groups))
6199       (gnus-group-remove-mark group)
6200       (unless (gnus-get-new-news-in-group group)
6201         (ding)
6202         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6203     (when gnus-goto-next-group-when-activating
6204       (gnus-group-next-unread-group 1 t))
6205     (gnus-summary-position-point)
6206     ret))
6207
6208 (defun gnus-get-new-news-in-group (group)
6209   (when (and group (gnus-activate-group group 'scan))
6210     (gnus-get-unread-articles-in-group
6211      (gnus-get-info group) (gnus-active group) t)
6212     (when (gnus-group-goto-group group)
6213       (gnus-group-update-group-line))
6214     t))
6215
6216 (defun gnus-group-fetch-faq (group &optional faq-dir)
6217   "Fetch the FAQ for the current group."
6218   (interactive
6219    (list
6220     (gnus-group-real-name (gnus-group-group-name))
6221     (cond (current-prefix-arg
6222            (completing-read
6223             "Faq dir: " (and (listp gnus-group-faq-directory)
6224                              gnus-group-faq-directory))))))
6225   (or faq-dir
6226       (setq faq-dir (if (listp gnus-group-faq-directory)
6227                         (car gnus-group-faq-directory)
6228                       gnus-group-faq-directory)))
6229   (or group (error "No group name given"))
6230   (let ((file (concat (file-name-as-directory faq-dir)
6231                       (gnus-group-real-name group))))
6232     (if (not (file-exists-p file))
6233         (error "No such file: %s" file)
6234       (find-file file))))
6235
6236 (defun gnus-group-describe-group (force &optional group)
6237   "Display a description of the current newsgroup."
6238   (interactive (list current-prefix-arg (gnus-group-group-name)))
6239   (and force (setq gnus-description-hashtb nil))
6240   (let ((method (gnus-find-method-for-group group))
6241         desc)
6242     (or group (error "No group name given"))
6243     (and (or (and gnus-description-hashtb
6244                   ;; We check whether this group's method has been
6245                   ;; queried for a description file.
6246                   (gnus-gethash
6247                    (gnus-group-prefixed-name "" method)
6248                    gnus-description-hashtb))
6249              (setq desc (gnus-group-get-description group))
6250              (gnus-read-descriptions-file method))
6251          (message
6252           (or desc (gnus-gethash group gnus-description-hashtb)
6253               "No description available")))))
6254
6255 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6256 (defun gnus-group-describe-all-groups (&optional force)
6257   "Pop up a buffer with descriptions of all newsgroups."
6258   (interactive "P")
6259   (and force (setq gnus-description-hashtb nil))
6260   (if (not (or gnus-description-hashtb
6261                (gnus-read-all-descriptions-files)))
6262       (error "Couldn't request descriptions file"))
6263   (let ((buffer-read-only nil)
6264         b)
6265     (erase-buffer)
6266     (mapatoms
6267      (lambda (group)
6268        (setq b (point))
6269        (insert (format "      *: %-20s %s\n" (symbol-name group)
6270                        (symbol-value group)))
6271        (add-text-properties
6272         b (1+ b) (list 'gnus-group group
6273                        'gnus-unread t 'gnus-marked nil
6274                        'gnus-level (1+ gnus-level-subscribed))))
6275      gnus-description-hashtb)
6276     (goto-char (point-min))
6277     (gnus-group-position-point)))
6278
6279 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6280 (defun gnus-group-apropos (regexp &optional search-description)
6281   "List all newsgroups that have names that match a regexp."
6282   (interactive "sGnus apropos (regexp): ")
6283   (let ((prev "")
6284         (obuf (current-buffer))
6285         groups des)
6286     ;; Go through all newsgroups that are known to Gnus.
6287     (mapatoms
6288      (lambda (group)
6289        (and (symbol-name group)
6290             (string-match regexp (symbol-name group))
6291             (setq groups (cons (symbol-name group) groups))))
6292      gnus-active-hashtb)
6293     ;; Go through all descriptions that are known to Gnus.
6294     (if search-description
6295         (mapatoms
6296          (lambda (group)
6297            (and (string-match regexp (symbol-value group))
6298                 (gnus-active (symbol-name group))
6299                 (setq groups (cons (symbol-name group) groups))))
6300          gnus-description-hashtb))
6301     (if (not groups)
6302         (gnus-message 3 "No groups matched \"%s\"." regexp)
6303       ;; Print out all the groups.
6304       (save-excursion
6305         (pop-to-buffer "*Gnus Help*")
6306         (buffer-disable-undo (current-buffer))
6307         (erase-buffer)
6308         (setq groups (sort groups 'string<))
6309         (while groups
6310           ;; Groups may be entered twice into the list of groups.
6311           (if (not (string= (car groups) prev))
6312               (progn
6313                 (insert (setq prev (car groups)) "\n")
6314                 (if (and gnus-description-hashtb
6315                          (setq des (gnus-gethash (car groups)
6316                                                  gnus-description-hashtb)))
6317                     (insert "  " des "\n"))))
6318           (setq groups (cdr groups)))
6319         (goto-char (point-min))))
6320     (pop-to-buffer obuf)))
6321
6322 (defun gnus-group-description-apropos (regexp)
6323   "List all newsgroups that have names or descriptions that match a regexp."
6324   (interactive "sGnus description apropos (regexp): ")
6325   (if (not (or gnus-description-hashtb
6326                (gnus-read-all-descriptions-files)))
6327       (error "Couldn't request descriptions file"))
6328   (gnus-group-apropos regexp t))
6329
6330 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6331 (defun gnus-group-list-matching (level regexp &optional all lowest)
6332   "List all groups with unread articles that match REGEXP.
6333 If the prefix LEVEL is non-nil, it should be a number that says which
6334 level to cut off listing groups.
6335 If ALL, also list groups with no unread articles.
6336 If LOWEST, don't list groups with level lower than LOWEST."
6337   (interactive "P\nsList newsgroups matching: ")
6338   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6339                            all (or lowest 1) regexp)
6340   (goto-char (point-min))
6341   (gnus-group-position-point))
6342
6343 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6344   "List all groups that match REGEXP.
6345 If the prefix LEVEL is non-nil, it should be a number that says which
6346 level to cut off listing groups.
6347 If LOWEST, don't list groups with level lower than LOWEST."
6348   (interactive "P\nsList newsgroups matching: ")
6349   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6350
6351 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6352 (defun gnus-group-save-newsrc (&optional force)
6353   "Save the Gnus startup files.
6354 If FORCE, force saving whether it is necessary or not."
6355   (interactive "P")
6356   (gnus-save-newsrc-file force))
6357
6358 (defun gnus-group-restart (&optional arg)
6359   "Force Gnus to read the .newsrc file."
6360   (interactive "P")
6361   (gnus-save-newsrc-file)
6362   (gnus-setup-news 'force)
6363   (gnus-group-list-groups arg))
6364
6365 (defun gnus-group-read-init-file ()
6366   "Read the Gnus elisp init file."
6367   (interactive)
6368   (gnus-read-init-file))
6369
6370 (defun gnus-group-check-bogus-groups (&optional silent)
6371   "Check bogus newsgroups.
6372 If given a prefix, don't ask for confirmation before removing a bogus
6373 group."
6374   (interactive "P")
6375   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6376   (gnus-group-list-groups))
6377
6378 (defun gnus-group-edit-global-kill (&optional article group)
6379   "Edit the global kill file.
6380 If GROUP, edit that local kill file instead."
6381   (interactive "P")
6382   (setq gnus-current-kill-article article)
6383   (gnus-kill-file-edit-file group)
6384   (gnus-message
6385    6
6386    (substitute-command-keys
6387     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6388             (if group "local" "global")))))
6389
6390 (defun gnus-group-edit-local-kill (article group)
6391   "Edit a local kill file."
6392   (interactive (list nil (gnus-group-group-name)))
6393   (gnus-group-edit-global-kill article group))
6394
6395 (defun gnus-group-force-update ()
6396   "Update `.newsrc' file."
6397   (interactive)
6398   (gnus-save-newsrc-file))
6399
6400 (defun gnus-group-suspend ()
6401   "Suspend the current Gnus session.
6402 In fact, cleanup buffers except for group mode buffer.
6403 The hook gnus-suspend-gnus-hook is called before actually suspending."
6404   (interactive)
6405   (run-hooks 'gnus-suspend-gnus-hook)
6406   ;; Kill Gnus buffers except for group mode buffer.
6407   (let ((group-buf (get-buffer gnus-group-buffer)))
6408     ;; Do this on a separate list in case the user does a ^G before we finish
6409     (let ((gnus-buffer-list
6410            (delq group-buf (delq gnus-dribble-buffer
6411                                  (append gnus-buffer-list nil)))))
6412       (while gnus-buffer-list
6413         (gnus-kill-buffer (car gnus-buffer-list))
6414         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6415     (if group-buf
6416         (progn
6417           (setq gnus-buffer-list (list group-buf))
6418           (bury-buffer group-buf)
6419           (delete-windows-on group-buf t)))))
6420
6421 (defun gnus-group-clear-dribble ()
6422   "Clear all information from the dribble buffer."
6423   (interactive)
6424   (gnus-dribble-clear)
6425   (gnus-message 7 "Cleared dribble buffer"))
6426
6427 (defun gnus-group-exit ()
6428   "Quit reading news after updating .newsrc.eld and .newsrc.
6429 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6430   (interactive)
6431   (when 
6432       (or noninteractive                ;For gnus-batch-kill
6433           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6434           (not gnus-interactive-exit)   ;Without confirmation
6435           gnus-expert-user
6436           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6437     (run-hooks 'gnus-exit-gnus-hook)
6438     ;; Offer to save data from non-quitted summary buffers.
6439     (gnus-offer-save-summaries)
6440     ;; Save the newsrc file(s).
6441     (gnus-save-newsrc-file)
6442     ;; Kill-em-all.
6443     (gnus-close-backends)
6444     ;; Shut down the cache.
6445     (when gnus-use-cache
6446       (gnus-cache-close))
6447     ;; Reset everything.
6448     (gnus-clear-system)
6449     ;; Allow the user to do things after cleaning up.
6450     (run-hooks 'gnus-after-exiting-gnus-hook)))
6451
6452 (defun gnus-close-backends ()
6453   ;; Send a close request to all backends that support such a request.
6454   (let ((methods gnus-valid-select-methods)
6455         func)
6456     (while methods
6457       (if (fboundp (setq func (intern (concat (car (car methods))
6458                                               "-request-close"))))
6459           (funcall func))
6460       (setq methods (cdr methods)))))
6461
6462 (defun gnus-group-quit ()
6463   "Quit reading news without updating .newsrc.eld or .newsrc.
6464 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6465   (interactive)
6466   (when (or noninteractive              ;For gnus-batch-kill
6467             (zerop (buffer-size))
6468             (not (gnus-server-opened gnus-select-method))
6469             gnus-expert-user
6470             (not gnus-current-startup-file)
6471             (gnus-yes-or-no-p
6472              (format "Quit reading news without saving %s? "
6473                      (file-name-nondirectory gnus-current-startup-file))))
6474     (run-hooks 'gnus-exit-gnus-hook)
6475     (if gnus-use-full-window
6476         (delete-other-windows)
6477       (gnus-remove-some-windows))
6478     (gnus-dribble-save)
6479     (gnus-close-backends)
6480     ;; Shut down the cache.
6481     (when gnus-use-cache
6482       (gnus-cache-close))
6483     (gnus-clear-system)
6484     ;; Allow the user to do things after cleaning up.
6485     (run-hooks 'gnus-after-exiting-gnus-hook)))
6486
6487 (defun gnus-offer-save-summaries ()
6488   "Offer to save all active summary buffers."
6489   (save-excursion
6490     (let ((buflist (buffer-list))
6491           buffers bufname)
6492       ;; Go through all buffers and find all summaries.
6493       (while buflist
6494         (and (setq bufname (buffer-name (car buflist)))
6495              (string-match "Summary" bufname)
6496              (save-excursion
6497                (set-buffer bufname)
6498                ;; We check that this is, indeed, a summary buffer.
6499                (and (eq major-mode 'gnus-summary-mode)
6500                     ;; Also make sure this isn't bogus.
6501                     gnus-newsgroup-prepared))
6502              (push bufname buffers))
6503         (setq buflist (cdr buflist)))
6504       ;; Go through all these summary buffers and offer to save them.
6505       (when buffers
6506         (map-y-or-n-p
6507          "Update summary buffer %s? "
6508          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6509          buffers)))))
6510
6511 (defun gnus-group-describe-briefly ()
6512   "Give a one line description of the group mode commands."
6513   (interactive)
6514   (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")))
6515
6516 (defun gnus-group-browse-foreign-server (method)
6517   "Browse a foreign news server.
6518 If called interactively, this function will ask for a select method
6519  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6520 If not, METHOD should be a list where the first element is the method
6521 and the second element is the address."
6522   (interactive
6523    (list (let ((how (completing-read
6524                      "Which backend: "
6525                      (append gnus-valid-select-methods gnus-server-alist)
6526                      nil t (cons "nntp" 0))))
6527            ;; We either got a backend name or a virtual server name.
6528            ;; If the first, we also need an address.
6529            (if (assoc how gnus-valid-select-methods)
6530                (list (intern how)
6531                      ;; Suggested by mapjph@bath.ac.uk.
6532                      (completing-read
6533                       "Address: "
6534                       (mapcar (lambda (server) (list server))
6535                               gnus-secondary-servers)))
6536              ;; We got a server name, so we find the method.
6537              (gnus-server-to-method how)))))
6538   (gnus-browse-foreign-server method))
6539
6540 \f
6541 ;;;
6542 ;;; Gnus summary mode
6543 ;;;
6544
6545 (defvar gnus-summary-mode-map nil)
6546
6547 (put 'gnus-summary-mode 'mode-class 'special)
6548
6549 (unless gnus-summary-mode-map
6550   (setq gnus-summary-mode-map (make-keymap))
6551   (suppress-keymap gnus-summary-mode-map)
6552
6553   ;; Non-orthogonal keys
6554
6555   (gnus-define-keys
6556    gnus-summary-mode-map
6557    " " gnus-summary-next-page
6558    "\177" gnus-summary-prev-page
6559    [delete] gnus-summary-prev-page
6560    "\r" gnus-summary-scroll-up
6561    "n" gnus-summary-next-unread-article
6562    "p" gnus-summary-prev-unread-article
6563    "N" gnus-summary-next-article
6564    "P" gnus-summary-prev-article
6565    "\M-\C-n" gnus-summary-next-same-subject
6566    "\M-\C-p" gnus-summary-prev-same-subject
6567    "\M-n" gnus-summary-next-unread-subject
6568    "\M-p" gnus-summary-prev-unread-subject
6569    "." gnus-summary-first-unread-article
6570    "," gnus-summary-best-unread-article
6571    "\M-s" gnus-summary-search-article-forward
6572    "\M-r" gnus-summary-search-article-backward
6573    "<" gnus-summary-beginning-of-article
6574    ">" gnus-summary-end-of-article
6575    "j" gnus-summary-goto-article
6576    "^" gnus-summary-refer-parent-article
6577    "\M-^" gnus-summary-refer-article
6578    "u" gnus-summary-tick-article-forward
6579    "!" gnus-summary-tick-article-forward
6580    "U" gnus-summary-tick-article-backward
6581    "d" gnus-summary-mark-as-read-forward
6582    "D" gnus-summary-mark-as-read-backward
6583    "E" gnus-summary-mark-as-expirable
6584    "\M-u" gnus-summary-clear-mark-forward
6585    "\M-U" gnus-summary-clear-mark-backward
6586    "k" gnus-summary-kill-same-subject-and-select
6587    "\C-k" gnus-summary-kill-same-subject
6588    "\M-\C-k" gnus-summary-kill-thread
6589    "\M-\C-l" gnus-summary-lower-thread
6590    "e" gnus-summary-edit-article
6591    "#" gnus-summary-mark-as-processable
6592    "\M-#" gnus-summary-unmark-as-processable
6593    "\M-\C-t" gnus-summary-toggle-threads
6594    "\M-\C-s" gnus-summary-show-thread
6595    "\M-\C-h" gnus-summary-hide-thread
6596    "\M-\C-f" gnus-summary-next-thread
6597    "\M-\C-b" gnus-summary-prev-thread
6598    "\M-\C-u" gnus-summary-up-thread
6599    "\M-\C-d" gnus-summary-down-thread
6600    "&" gnus-summary-execute-command
6601    "c" gnus-summary-catchup-and-exit
6602    "\C-w" gnus-summary-mark-region-as-read
6603    "\C-t" gnus-summary-toggle-truncation
6604    "?" gnus-summary-mark-as-dormant
6605    "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6606    "\C-c\C-s\C-n" gnus-summary-sort-by-number
6607    "\C-c\C-s\C-a" gnus-summary-sort-by-author
6608    "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6609    "\C-c\C-s\C-d" gnus-summary-sort-by-date
6610    "\C-c\C-s\C-i" gnus-summary-sort-by-score
6611    "=" gnus-summary-expand-window
6612    "\C-x\C-s" gnus-summary-reselect-current-group
6613    "\M-g" gnus-summary-rescan-group
6614    "w" gnus-summary-stop-page-breaking
6615    "\C-c\C-r" gnus-summary-caesar-message
6616    "\M-t" gnus-summary-toggle-mime
6617    "f" gnus-summary-followup
6618    "F" gnus-summary-followup-with-original
6619    "C" gnus-summary-cancel-article
6620    "r" gnus-summary-reply
6621    "R" gnus-summary-reply-with-original
6622    "\C-c\C-f" gnus-summary-mail-forward
6623    "o" gnus-summary-save-article
6624    "\C-o" gnus-summary-save-article-mail
6625    "|" gnus-summary-pipe-output
6626    "\M-k" gnus-summary-edit-local-kill
6627    "\M-K" gnus-summary-edit-global-kill
6628    "V" gnus-version
6629    "\C-c\C-d" gnus-summary-describe-group
6630    "q" gnus-summary-exit
6631    "Q" gnus-summary-exit-no-update
6632    "\C-c\C-i" gnus-info-find-node
6633    gnus-mouse-2 gnus-mouse-pick-article
6634    "m" gnus-summary-mail-other-window
6635    "a" gnus-summary-post-news
6636    "x" gnus-summary-limit-to-unread
6637    "s" gnus-summary-isearch-article
6638    "t" gnus-article-hide-headers
6639    "g" gnus-summary-show-article
6640    "l" gnus-summary-goto-last-article
6641    "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6642    "\C-d" gnus-summary-enter-digest-group
6643    "\C-c\C-b" gnus-bug
6644    "*" gnus-cache-enter-article
6645    "\M-*" gnus-cache-remove-article
6646    "\M-&" gnus-summary-universal-argument
6647    "\C-l" gnus-recenter
6648    "I" gnus-summary-increase-score
6649    "L" gnus-summary-lower-score
6650
6651    "V" gnus-summary-score-map
6652    "X" gnus-uu-extract-map
6653    "S" gnus-summary-send-map)
6654
6655   ;; Sort of orthogonal keymap
6656   (gnus-define-keys
6657    (gnus-summary-mark-map "M" gnus-summary-mode-map)
6658    "t" gnus-summary-tick-article-forward
6659    "!" gnus-summary-tick-article-forward
6660    "d" gnus-summary-mark-as-read-forward
6661    "r" gnus-summary-mark-as-read-forward
6662    "c" gnus-summary-clear-mark-forward
6663    " " gnus-summary-clear-mark-forward
6664    "e" gnus-summary-mark-as-expirable
6665    "x" gnus-summary-mark-as-expirable
6666    "?" gnus-summary-mark-as-dormant
6667    "b" gnus-summary-set-bookmark
6668    "B" gnus-summary-remove-bookmark
6669    "#" gnus-summary-mark-as-processable
6670    "\M-#" gnus-summary-unmark-as-processable
6671    "S" gnus-summary-limit-include-expunged
6672    "C" gnus-summary-catchup
6673    "H" gnus-summary-catchup-to-here
6674    "\C-c" gnus-summary-catchup-all
6675    "k" gnus-summary-kill-same-subject-and-select
6676    "K" gnus-summary-kill-same-subject
6677    "P" gnus-uu-mark-map)
6678
6679   (gnus-define-keys
6680    (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6681    "c" gnus-summary-clear-above
6682    "u" gnus-summary-tick-above
6683    "m" gnus-summary-mark-above
6684    "k" gnus-summary-kill-below)
6685
6686   (gnus-define-keys
6687    (gnus-summary-limit-map "/" gnus-summary-mode-map)
6688    "/" gnus-summary-limit-to-subject
6689    "n" gnus-summary-limit-to-articles
6690    "w" gnus-summary-pop-limit
6691    "s" gnus-summary-limit-to-subject
6692    "a" gnus-summary-limit-to-author
6693    "u" gnus-summary-limit-to-unread
6694    "m" gnus-summary-limit-to-marks
6695    "v" gnus-summary-limit-to-score
6696    "D" gnus-summary-limit-include-dormant
6697    "d" gnus-summary-limit-exclude-dormant
6698 ;;  "t" gnus-summary-limit-exclude-thread
6699    "E" gnus-summary-limit-include-expunged
6700    "c" gnus-summary-limit-exclude-childless-dormant
6701    "C" gnus-summary-limit-mark-excluded-as-read)
6702
6703   (gnus-define-keys
6704    (gnus-summary-goto-map "G" gnus-summary-mode-map)
6705    "n" gnus-summary-next-unread-article
6706    "p" gnus-summary-prev-unread-article
6707    "N" gnus-summary-next-article
6708    "P" gnus-summary-prev-article
6709    "\C-n" gnus-summary-next-same-subject
6710    "\C-p" gnus-summary-prev-same-subject
6711    "\M-n" gnus-summary-next-unread-subject
6712    "\M-p" gnus-summary-prev-unread-subject
6713    "f" gnus-summary-first-unread-article
6714    "b" gnus-summary-best-unread-article
6715    "g" gnus-summary-goto-subject
6716    "l" gnus-summary-goto-last-article
6717    "p" gnus-summary-pop-article)
6718
6719   (gnus-define-keys
6720    (gnus-summary-thread-map "T" gnus-summary-mode-map)
6721    "k" gnus-summary-kill-thread
6722    "l" gnus-summary-lower-thread
6723    "i" gnus-summary-raise-thread
6724    "T" gnus-summary-toggle-threads
6725    "t" gnus-summary-rethread-current
6726    "^" gnus-summary-reparent-thread
6727    "s" gnus-summary-show-thread
6728    "S" gnus-summary-show-all-threads
6729    "h" gnus-summary-hide-thread
6730    "H" gnus-summary-hide-all-threads
6731    "n" gnus-summary-next-thread
6732    "p" gnus-summary-prev-thread
6733    "u" gnus-summary-up-thread
6734    "o" gnus-summary-top-thread
6735    "d" gnus-summary-down-thread
6736    "#" gnus-uu-mark-thread
6737    "\M-#" gnus-uu-unmark-thread)
6738
6739   (gnus-define-keys
6740    (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6741    "c" gnus-summary-catchup-and-exit
6742    "C" gnus-summary-catchup-all-and-exit
6743    "E" gnus-summary-exit-no-update
6744    "Q" gnus-summary-exit
6745    "Z" gnus-summary-exit
6746    "n" gnus-summary-catchup-and-goto-next-group
6747    "R" gnus-summary-reselect-current-group
6748    "G" gnus-summary-rescan-group
6749    "N" gnus-summary-next-group
6750    "P" gnus-summary-prev-group)
6751
6752   (gnus-define-keys
6753    (gnus-summary-article-map "A" gnus-summary-mode-map)
6754    " " gnus-summary-next-page
6755    "n" gnus-summary-next-page
6756    "\177" gnus-summary-prev-page
6757    [delete] gnus-summary-prev-page
6758    "p" gnus-summary-prev-page
6759    "\r" gnus-summary-scroll-up
6760    "<" gnus-summary-beginning-of-article
6761    ">" gnus-summary-end-of-article
6762    "b" gnus-summary-beginning-of-article
6763    "e" gnus-summary-end-of-article
6764    "^" gnus-summary-refer-parent-article
6765    "r" gnus-summary-refer-parent-article
6766    "R" gnus-summary-refer-references
6767    "g" gnus-summary-show-article
6768    "s" gnus-summary-isearch-article)
6769
6770   (gnus-define-keys
6771    (gnus-summary-wash-map "W" gnus-summary-mode-map)
6772    "b" gnus-article-add-buttons
6773    "B" gnus-article-add-buttons-to-head
6774    "o" gnus-article-treat-overstrike
6775 ;;  "w" gnus-article-word-wrap
6776    "w" gnus-article-fill-cited-article
6777    "c" gnus-article-remove-cr
6778    "L" gnus-article-remove-trailing-blank-lines
6779    "q" gnus-article-de-quoted-unreadable
6780    "f" gnus-article-display-x-face
6781    "l" gnus-summary-stop-page-breaking
6782    "r" gnus-summary-caesar-message
6783    "t" gnus-article-hide-headers
6784    "v" gnus-summary-verbose-headers
6785    "m" gnus-summary-toggle-mime)
6786
6787   (gnus-define-keys
6788    (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6789    "a" gnus-article-hide
6790    "h" gnus-article-hide-headers
6791    "b" gnus-article-hide-boring-headers
6792    "s" gnus-article-hide-signature
6793    "c" gnus-article-hide-citation
6794    "p" gnus-article-hide-pgp
6795    "\C-c" gnus-article-hide-citation-maybe)
6796
6797   (gnus-define-keys
6798    (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6799    "a" gnus-article-highlight
6800    "h" gnus-article-highlight-headers
6801    "c" gnus-article-highlight-citation
6802    "s" gnus-article-highlight-signature)
6803
6804   (gnus-define-keys
6805    (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6806    "z" gnus-article-date-ut
6807    "u" gnus-article-date-ut
6808    "l" gnus-article-date-local
6809    "e" gnus-article-date-lapsed
6810    "o" gnus-article-date-original)
6811
6812   (gnus-define-keys
6813    (gnus-summary-help-map "H" gnus-summary-mode-map)
6814    "v" gnus-version
6815    "f" gnus-summary-fetch-faq
6816    "d" gnus-summary-describe-group
6817    "h" gnus-summary-describe-briefly
6818    "i" gnus-info-find-node)
6819
6820   (gnus-define-keys
6821    (gnus-summary-backend-map "B" gnus-summary-mode-map)
6822    "e" gnus-summary-expire-articles
6823    "\M-\C-e" gnus-summary-expire-articles-now
6824    "\177" gnus-summary-delete-article
6825    [delete] gnus-summary-delete-article
6826    "m" gnus-summary-move-article
6827    "r" gnus-summary-respool-article
6828    "w" gnus-summary-edit-article
6829    "c" gnus-summary-copy-article
6830    "B" gnus-summary-crosspost-article
6831    "q" gnus-summary-respool-query
6832    "i" gnus-summary-import-article)
6833
6834   (gnus-define-keys
6835    (gnus-summary-save-map "O" gnus-summary-mode-map)
6836    "o" gnus-summary-save-article
6837    "m" gnus-summary-save-article-mail
6838    "r" gnus-summary-save-article-rmail
6839    "f" gnus-summary-save-article-file
6840    "b" gnus-summary-save-article-body-file
6841    "h" gnus-summary-save-article-folder
6842    "v" gnus-summary-save-article-vm
6843    "p" gnus-summary-pipe-output
6844    "s" gnus-soup-add-article)
6845   )
6846
6847 \f
6848
6849 (defun gnus-summary-mode (&optional group)
6850   "Major mode for reading articles.
6851
6852 All normal editing commands are switched off.
6853 \\<gnus-summary-mode-map>
6854 Each line in this buffer represents one article.  To read an
6855 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6856 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6857 respectively.
6858
6859 You can also post articles and send mail from this buffer.  To
6860 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6861 of an article, type `\\[gnus-summary-reply]'.
6862
6863 There are approx. one gazillion commands you can execute in this
6864 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6865
6866 The following commands are available:
6867
6868 \\{gnus-summary-mode-map}"
6869   (interactive)
6870   (when (and menu-bar-mode
6871              (gnus-visual-p 'summary-menu 'menu))
6872     (gnus-summary-make-menu-bar))
6873   (kill-all-local-variables)
6874   (let ((locals gnus-summary-local-variables))
6875     (while locals
6876       (if (consp (car locals))
6877           (progn
6878             (make-local-variable (car (car locals)))
6879             (set (car (car locals)) (eval (cdr (car locals)))))
6880         (make-local-variable (car locals))
6881         (set (car locals) nil))
6882       (setq locals (cdr locals))))
6883   (gnus-make-thread-indent-array)
6884   (gnus-simplify-mode-line)
6885   (setq major-mode 'gnus-summary-mode)
6886   (setq mode-name "Summary")
6887   (make-local-variable 'minor-mode-alist)
6888   (use-local-map gnus-summary-mode-map)
6889   (buffer-disable-undo (current-buffer))
6890   (setq buffer-read-only t)             ;Disable modification
6891   (setq truncate-lines t)
6892   (setq selective-display t)
6893   (setq selective-display-ellipses t)   ;Display `...'
6894   (setq buffer-display-table gnus-summary-display-table)
6895   (setq gnus-newsgroup-name group)
6896   (run-hooks 'gnus-summary-mode-hook))
6897
6898 (defun gnus-summary-make-display-table ()
6899   ;; Change the display table.  Odd characters have a tendency to mess
6900   ;; up nicely formatted displays - we make all possible glyphs
6901   ;; display only a single character.
6902
6903   ;; We start from the standard display table, if any.
6904   (setq gnus-summary-display-table
6905         (or (copy-sequence standard-display-table)
6906             (make-display-table)))
6907   ;; Nix out all the control chars...
6908   (let ((i 32))
6909     (while (>= (setq i (1- i)) 0)
6910       (aset gnus-summary-display-table i [??])))
6911   ;; ... but not newline and cr, of course. (cr is necessary for the
6912   ;; selective display).
6913   (aset gnus-summary-display-table ?\n nil)
6914   (aset gnus-summary-display-table ?\r nil)
6915   ;; We nix out any glyphs over 126 that are not set already.
6916   (let ((i 256))
6917     (while (>= (setq i (1- i)) 127)
6918       ;; Only modify if the entry is nil.
6919       (or (aref gnus-summary-display-table i)
6920           (aset gnus-summary-display-table i [??])))))
6921
6922 (defun gnus-summary-clear-local-variables ()
6923   (let ((locals gnus-summary-local-variables))
6924     (while locals
6925       (if (consp (car locals))
6926           (and (vectorp (car (car locals)))
6927                (set (car (car locals)) nil))
6928         (and (vectorp (car locals))
6929              (set (car locals) nil)))
6930       (setq locals (cdr locals)))))
6931
6932 ;; Summary data functions.
6933
6934 (defmacro gnus-data-number (data)
6935   `(car ,data))
6936
6937 (defmacro gnus-data-set-number (data number)
6938   `(setcar ,data ,number))
6939
6940 (defmacro gnus-data-mark (data)
6941   `(nth 1 ,data))
6942
6943 (defmacro gnus-data-set-mark (data mark)
6944   `(setcar (nthcdr 1 ,data) ,mark))
6945
6946 (defmacro gnus-data-pos (data)
6947   `(nth 2 ,data))
6948
6949 (defmacro gnus-data-set-pos (data pos)
6950   `(setcar (nthcdr 2 ,data) ,pos))
6951
6952 (defmacro gnus-data-header (data)
6953   `(nth 3 ,data))
6954
6955 (defmacro gnus-data-level (data)
6956   `(nth 4 ,data))
6957
6958 (defmacro gnus-data-unread-p (data)
6959   `(= (nth 1 ,data) gnus-unread-mark))
6960
6961 (defmacro gnus-data-pseudo-p (data)
6962   `(consp (nth 3 ,data)))
6963
6964 (defmacro gnus-data-find (number)
6965   `(assq ,number gnus-newsgroup-data))
6966
6967 (defmacro gnus-data-find-list (number &optional data)
6968   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
6969      (memq (assq ,number bdata)
6970            bdata)))
6971
6972 (defmacro gnus-data-make (number mark pos header level)
6973   `(list ,number ,mark ,pos ,header ,level))
6974
6975 (defun gnus-data-enter (after-article number mark pos header level offset)
6976   (let ((data (gnus-data-find-list after-article)))
6977     (or data (error "No such article: %d" after-article))
6978     (setcdr data (cons (gnus-data-make number mark pos header level)
6979                        (cdr data)))
6980     (setq gnus-newsgroup-data-reverse nil)
6981     (gnus-data-update-list (cdr (cdr data)) offset)))
6982
6983 (defun gnus-data-enter-list (after-article list &optional offset)
6984   (when list
6985     (let ((data (and after-article (gnus-data-find-list after-article)))
6986           (ilist list))
6987       (or data (not after-article) (error "No such article: %d" after-article))
6988       ;; Find the last element in the list to be spliced into the main
6989       ;; list.
6990       (while (cdr list)
6991         (setq list (cdr list)))
6992       (if (not data)
6993           (progn
6994             (setcdr list gnus-newsgroup-data)
6995             (setq gnus-newsgroup-data ilist)
6996             (and offset (gnus-data-update-list (cdr list) offset)))
6997         (setcdr list (cdr data))
6998         (setcdr data ilist)
6999         (and offset (gnus-data-update-list (cdr data) offset)))
7000       (setq gnus-newsgroup-data-reverse nil))))
7001
7002 (defun gnus-data-remove (article &optional offset)
7003   (let ((data gnus-newsgroup-data))
7004     (if (= (gnus-data-number (car data)) article)
7005         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7006               gnus-newsgroup-data-reverse nil)
7007       (while (cdr data)
7008         (and (= (gnus-data-number (car (cdr data))) article)
7009              (progn
7010                (setcdr data (cdr (cdr data)))
7011                (and offset (gnus-data-update-list (cdr data) offset))
7012                (setq data nil
7013                      gnus-newsgroup-data-reverse nil)))
7014         (setq data (cdr data))))))
7015
7016 (defmacro gnus-data-list (backward)
7017   `(if ,backward
7018        (or gnus-newsgroup-data-reverse
7019            (setq gnus-newsgroup-data-reverse
7020                  (reverse gnus-newsgroup-data)))
7021      gnus-newsgroup-data))
7022
7023 (defun gnus-data-update-list (data offset)
7024   "Add OFFSET to the POS of all data entries in DATA."
7025   (while data
7026     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7027     (setq data (cdr data))))
7028
7029 (defun gnus-data-compute-positions ()
7030   "Compute the positions of all articles."
7031   (let ((data gnus-newsgroup-data)
7032         pos)
7033     (while data
7034       (when (setq pos (text-property-any
7035                        (point-min) (point-max)
7036                        'gnus-number (gnus-data-number (car data))))
7037         (gnus-data-set-pos (car data) (+ pos 3)))
7038       (setq data (cdr data)))))
7039
7040 (defun gnus-summary-article-pseudo-p (article)
7041   "Say whether this article is a pseudo article or not."
7042   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7043
7044 (defun gnus-article-parent-p (number)
7045   "Say whether this article is a parent or not."
7046   (let* ((data (gnus-data-find-list number)))
7047     (and (cdr data)                     ; There has to be an article after...
7048          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7049             (gnus-data-level (nth 1 data))))))
7050
7051 (defmacro gnus-summary-skip-intangible ()
7052   "If the current article is intangible, then jump to a different article."
7053   '(let ((to (get-text-property (point) 'gnus-intangible)))
7054     (and to (gnus-summary-goto-subject to))))
7055
7056 (defmacro gnus-summary-article-intangible-p ()
7057   "Say whether this article is intangible or not."
7058   '(get-text-property (point) 'gnus-intangible))
7059
7060 ;; Some summary mode macros.
7061
7062 (defmacro gnus-summary-article-number ()
7063   "The article number of the article on the current line.
7064 If there isn's an article number here, then we return the current
7065 article number."
7066   '(progn
7067      (gnus-summary-skip-intangible)
7068      (or (get-text-property (point) 'gnus-number)
7069          (gnus-summary-last-subject))))
7070
7071 (defmacro gnus-summary-article-header (&optional number)
7072   `(gnus-data-header (gnus-data-find
7073                       ,(or number '(gnus-summary-article-number)))))
7074
7075 (defmacro gnus-summary-thread-level (&optional number)
7076   `(if (and (eq gnus-summary-make-false-root 'dummy)
7077             (get-text-property (point) 'gnus-intangible))
7078        0
7079      (gnus-data-level (gnus-data-find
7080                        ,(or number '(gnus-summary-article-number))))))
7081
7082 (defmacro gnus-summary-article-mark (&optional number)
7083   `(gnus-data-mark (gnus-data-find
7084                     ,(or number '(gnus-summary-article-number)))))
7085
7086 (defmacro gnus-summary-article-pos (&optional number)
7087   `(gnus-data-pos (gnus-data-find
7088                    ,(or number '(gnus-summary-article-number)))))
7089
7090 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7091 (defmacro gnus-summary-article-subject (&optional number)
7092   "Return current subject string or nil if nothing."
7093   `(let ((headers
7094           ,(if number
7095                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7096              '(gnus-data-header (assq (gnus-summary-article-number)
7097                                       gnus-newsgroup-data)))))
7098      (and headers
7099           (vectorp headers)
7100           (mail-header-subject headers))))
7101
7102 (defmacro gnus-summary-article-score (&optional number)
7103   "Return current article score."
7104   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7105                   gnus-newsgroup-scored))
7106        gnus-summary-default-score 0))
7107
7108 (defun gnus-summary-article-children (&optional number)
7109   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7110          (level (gnus-data-level (car data)))
7111          l children)
7112     (while (and (setq data (cdr data))
7113                 (> (setq l (gnus-data-level (car data))) level))
7114       (and (= (1+ level) l)
7115            (setq children (cons (gnus-data-number (car data))
7116                                 children))))
7117     (nreverse children)))
7118
7119 (defun gnus-summary-article-parent (&optional number)
7120   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7121                                     (gnus-data-list t)))
7122          (level (gnus-data-level (car data)))
7123          l)
7124     (if (zerop level)
7125         () ; This is a root.
7126       ;; We search until we find an article with a level less than
7127       ;; this one.  That function has to be the parent.
7128       (while (and (setq data (cdr data))
7129                   (not (< (gnus-data-level (car data)) level))))
7130       (and data (gnus-data-number (car data))))))
7131
7132 ;; Various summary mode internalish functions.
7133
7134 (defun gnus-mouse-pick-article (e)
7135   (interactive "e")
7136   (mouse-set-point e)
7137   (gnus-summary-next-page nil t))
7138
7139 (defun gnus-summary-setup-buffer (group)
7140   "Initialize summary buffer."
7141   (let ((buffer (concat "*Summary " group "*")))
7142     (if (get-buffer buffer)
7143         (progn
7144           (set-buffer buffer)
7145           (setq gnus-summary-buffer (current-buffer))
7146           (not gnus-newsgroup-prepared))
7147       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7148       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7149       (gnus-add-current-to-buffer-list)
7150       (gnus-summary-mode group)
7151       (when gnus-carpal
7152         (gnus-carpal-setup-buffer 'summary))
7153       (unless gnus-single-article-buffer
7154         (make-local-variable 'gnus-article-buffer)
7155         (make-local-variable 'gnus-original-article-buffer))
7156       (setq gnus-newsgroup-name group)
7157       t)))
7158
7159 (defun gnus-set-global-variables ()
7160   ;; Set the global equivalents of the summary buffer-local variables
7161   ;; to the latest values they had.  These reflect the summary buffer
7162   ;; that was in action when the last article was fetched.
7163   (when (eq major-mode 'gnus-summary-mode)
7164     (setq gnus-summary-buffer (current-buffer))
7165     (let ((name gnus-newsgroup-name)
7166           (marked gnus-newsgroup-marked)
7167           (unread gnus-newsgroup-unreads)
7168           (headers gnus-current-headers)
7169           (data gnus-newsgroup-data)
7170           (article-buffer gnus-article-buffer)
7171           (original gnus-original-article-buffer)
7172           (score-file gnus-current-score-file))
7173       (save-excursion
7174         (set-buffer gnus-group-buffer)
7175         (setq gnus-newsgroup-name name)
7176         (setq gnus-newsgroup-marked marked)
7177         (setq gnus-newsgroup-unreads unread)
7178         (setq gnus-current-headers headers)
7179         (setq gnus-newsgroup-data data)
7180         (setq gnus-article-buffer article-buffer)
7181         (setq gnus-original-article-buffer original)
7182         (setq gnus-current-score-file score-file)))))
7183
7184 (defun gnus-summary-last-article-p (&optional article)
7185   "Return whether ARTICLE is the last article in the buffer."
7186   (if (not (setq article (or article (gnus-summary-article-number))))
7187       t ; All non-existant numbers are the last article. :-)
7188     (cdr (gnus-data-find-list article))))
7189
7190 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7191   "Insert a dummy root in the summary buffer."
7192   (beginning-of-line)
7193   (add-text-properties
7194    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7195    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7196
7197 (defvar gnus-thread-indent-array nil)
7198 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7199 (defun gnus-make-thread-indent-array ()
7200   (let ((n 200))
7201     (if (and gnus-thread-indent-array
7202              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7203         nil
7204       (setq gnus-thread-indent-array (make-vector 201 "")
7205             gnus-thread-indent-array-level gnus-thread-indent-level)
7206       (while (>= n 0)
7207         (aset gnus-thread-indent-array n
7208               (make-string (* n gnus-thread-indent-level) ? ))
7209         (setq n (1- n))))))
7210
7211 (defun gnus-summary-insert-line
7212   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7213                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7214                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7215   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7216          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7217          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7218          (gnus-tmp-score-char
7219           (if (or (null gnus-summary-default-score)
7220                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7221                       gnus-summary-zcore-fuzz)) ? 
7222             (if (< gnus-tmp-score gnus-summary-default-score)
7223                 gnus-score-below-mark gnus-score-over-mark)))
7224          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7225                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7226                                   gnus-cached-mark)
7227                                  (gnus-tmp-replied gnus-replied-mark)
7228                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7229                                   gnus-saved-mark)
7230                                  (t gnus-unread-mark)))
7231          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7232          (gnus-tmp-name
7233           (cond
7234            ((string-match "(.+)" gnus-tmp-from)
7235             (substring gnus-tmp-from
7236                        (1+ (match-beginning 0)) (1- (match-end 0))))
7237            ((string-match "<[^>]+> *$" gnus-tmp-from)
7238             (let ((beg (match-beginning 0)))
7239               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7240                        (substring gnus-tmp-from (1+ (match-beginning 0))
7241                                   (1- (match-end 0))))
7242                   (substring gnus-tmp-from 0 beg))))
7243            (t gnus-tmp-from)))
7244          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7245          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7246          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7247          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7248          (buffer-read-only nil))
7249     (when (string= gnus-tmp-name "")
7250       (setq gnus-tmp-name gnus-tmp-from))
7251     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7252     (put-text-property
7253      (point)
7254      (progn (eval gnus-summary-line-format-spec) (point))
7255      'gnus-number gnus-tmp-number)
7256     (when (gnus-visual-p 'summary-highlight 'highlight)
7257       (forward-line -1)
7258       (run-hooks 'gnus-summary-update-hook)
7259       (forward-line 1))))
7260
7261 (defun gnus-summary-update-line (&optional dont-update)
7262   ;; Update summary line after change.
7263   (when (and gnus-summary-default-score
7264              (not gnus-summary-inhibit-highlight))
7265     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7266            (article (gnus-summary-article-number))
7267            (score (gnus-summary-article-score article)))
7268       (unless dont-update
7269         (if (and gnus-summary-mark-below
7270                  (< (gnus-summary-article-score)
7271                     gnus-summary-mark-below))
7272             ;; This article has a low score, so we mark it as read.
7273             (when (memq article gnus-newsgroup-unreads)
7274               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7275           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7276             ;; This article was previously marked as read on account
7277             ;; of a low score, but now it has risen, so we mark it as
7278             ;; unread.
7279             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7280         (gnus-summary-update-mark
7281          (if (or (null gnus-summary-default-score)
7282                  (<= (abs (- score gnus-summary-default-score))
7283                      gnus-summary-zcore-fuzz)) ? 
7284            (if (< score gnus-summary-default-score)
7285                gnus-score-below-mark gnus-score-over-mark)) 'score))
7286       ;; Do visual highlighting.
7287       (when (gnus-visual-p 'summary-highlight 'highlight)
7288         (run-hooks 'gnus-summary-update-hook)))))
7289
7290 (defvar gnus-tmp-new-adopts nil)
7291
7292 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7293   ;; Sum up all elements (and sub-elements) in a list.
7294   (let* ((number
7295           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7296           (cond
7297            ((and (consp thread) (cdr thread))
7298             (apply
7299              '+ 1 (mapcar
7300                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7301            ((null thread)
7302             1)
7303            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7304             1)
7305            (t 1))))
7306     (when (and level (zerop level) gnus-tmp-new-adopts)
7307       (incf number
7308             (apply '+ (mapcar
7309                        'gnus-summary-number-of-articles-in-thread
7310                        gnus-tmp-new-adopts))))
7311     (if char
7312         (if (> number 1) gnus-not-empty-thread-mark
7313           gnus-empty-thread-mark)
7314       number)))
7315
7316 (defun gnus-summary-set-local-parameters (group)
7317  "Go through the local params of GROUP and set all variable specs in that list."
7318   (let ((params (gnus-info-params (gnus-get-info group)))
7319         elem)
7320     (while params
7321       (setq elem (car params)
7322             params (cdr params))
7323       (and (consp elem)                 ; Has to be a cons.
7324            (consp (cdr elem))           ; The cdr has to be a list.
7325            (symbolp (car elem))         ; Has to be a symbol in there.
7326            (progn                       ; So we set it.
7327              (make-local-variable (car elem))
7328              (set (car elem) (eval (nth 1 elem))))))))
7329
7330 (defun gnus-summary-read-group
7331   (group &optional show-all no-article kill-buffer no-display)
7332   "Start reading news in newsgroup GROUP.
7333 If SHOW-ALL is non-nil, already read articles are also listed.
7334 If NO-ARTICLE is non-nil, no article is selected initially.
7335 If NO-DISPLAY, don't generate a summary buffer."
7336   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7337   (let* ((new-group (gnus-summary-setup-buffer group))
7338          (quit-config (gnus-group-quit-config group))
7339          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7340     (cond
7341      ;; This summary buffer exists already, so we just select it.
7342      ((not new-group)
7343       (gnus-set-global-variables)
7344       (when kill-buffer
7345         (gnus-kill-or-deaden-summary kill-buffer))
7346       (gnus-configure-windows 'summary 'force)
7347       (gnus-set-mode-line 'summary)
7348       (gnus-summary-position-point)
7349       (message "")
7350       t)
7351      ;; We couldn't select this group.
7352      ((null did-select)
7353       (when (and (eq major-mode 'gnus-summary-mode)
7354                  (not (equal (current-buffer) kill-buffer)))
7355         (kill-buffer (current-buffer))
7356         (if (not quit-config)
7357             (progn
7358               (set-buffer gnus-group-buffer)
7359               (gnus-group-jump-to-group group)
7360               (gnus-group-next-unread-group 1))
7361           (if (not (buffer-name (car quit-config)))
7362               (gnus-configure-windows 'group 'force)
7363             (set-buffer (car quit-config))
7364             (and (eq major-mode 'gnus-summary-mode)
7365                  (gnus-set-global-variables))
7366             (gnus-configure-windows (cdr quit-config)))))
7367       (gnus-message 3 "Can't select group")
7368       nil)
7369      ;; The user did a `C-g' while prompting for number of articles,
7370      ;; so we exit this group.
7371      ((eq did-select 'quit)
7372       (and (eq major-mode 'gnus-summary-mode)
7373            (not (equal (current-buffer) kill-buffer))
7374            (kill-buffer (current-buffer)))
7375       (when kill-buffer
7376         (gnus-kill-or-deaden-summary kill-buffer))
7377       (if (not quit-config)
7378           (progn
7379             (set-buffer gnus-group-buffer)
7380             (gnus-group-jump-to-group group)
7381             (gnus-group-next-unread-group 1)
7382             (gnus-configure-windows 'group 'force))
7383         (if (not (buffer-name (car quit-config)))
7384             (gnus-configure-windows 'group 'force)
7385           (set-buffer (car quit-config))
7386           (and (eq major-mode 'gnus-summary-mode)
7387                (gnus-set-global-variables))
7388           (gnus-configure-windows (cdr quit-config))))
7389       ;; Finally signal the quit.
7390       (signal 'quit nil))
7391      ;; The group was successfully selected.
7392      (t
7393       (gnus-set-global-variables)
7394       ;; Save the active value in effect when the group was entered.
7395       (setq gnus-newsgroup-active
7396             (gnus-copy-sequence
7397              (gnus-active gnus-newsgroup-name)))
7398       ;; You can change the summary buffer in some way with this hook.
7399       (run-hooks 'gnus-select-group-hook)
7400       ;; Set any local variables in the group parameters.
7401       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7402       (gnus-update-format-specifications)
7403       ;; Do score processing.
7404       (when gnus-use-scoring
7405         (gnus-possibly-score-headers))
7406       ;; Check whether to fill in the gaps in the threads.
7407       (when gnus-build-sparse-threads
7408         (gnus-build-sparse-threads))
7409       ;; Find the initial limit.
7410       (if show-all
7411           (let ((gnus-newsgroup-dormant nil))
7412             (gnus-summary-initial-limit show-all))
7413         (gnus-summary-initial-limit show-all))
7414       ;; Generate the summary buffer.
7415       (unless no-display
7416         (gnus-summary-prepare))
7417       (when gnus-use-trees
7418         (gnus-tree-open group)
7419         (setq gnus-summary-highlight-line-function
7420               'gnus-tree-highlight-article))
7421       ;; If the summary buffer is empty, but there are some low-scored
7422       ;; articles or some excluded dormants, we include these in the
7423       ;; buffer.
7424       (when (and (zerop (buffer-size))
7425                  (not no-display))
7426         (cond (gnus-newsgroup-dormant
7427                (gnus-summary-limit-include-dormant))
7428               ((and gnus-newsgroup-scored show-all)
7429                (gnus-summary-limit-include-expunged))))
7430       ;; Function `gnus-apply-kill-file' must be called in this hook.
7431       (run-hooks 'gnus-apply-kill-hook)
7432       (if (and (zerop (buffer-size))
7433                (not no-display))
7434           (progn
7435             ;; This newsgroup is empty.
7436             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7437             (gnus-message 6 "No unread news")
7438             (when kill-buffer
7439               (gnus-kill-or-deaden-summary kill-buffer))
7440             ;; Return nil from this function.
7441             nil)
7442         ;; Hide conversation thread subtrees.  We cannot do this in
7443         ;; gnus-summary-prepare-hook since kill processing may not
7444         ;; work with hidden articles.
7445         (and gnus-show-threads
7446              gnus-thread-hide-subtree
7447              (gnus-summary-hide-all-threads))
7448         ;; Show first unread article if requested.
7449         (if (and (not no-article)
7450                  (not no-display)
7451                  gnus-newsgroup-unreads
7452                  gnus-auto-select-first)
7453             (if (eq gnus-auto-select-first 'best)
7454                 (gnus-summary-best-unread-article)
7455               (gnus-summary-first-unread-article))
7456           ;; Don't select any articles, just move point to the first
7457           ;; article in the group.
7458           (goto-char (point-min))
7459           (gnus-summary-position-point)
7460           (gnus-set-mode-line 'summary)
7461           (gnus-configure-windows 'summary 'force))
7462         ;; If we are in async mode, we send some info to the backend.
7463         (when gnus-newsgroup-async
7464           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7465         (when kill-buffer
7466           (gnus-kill-or-deaden-summary kill-buffer))
7467         (when (get-buffer-window gnus-group-buffer t)
7468           ;; Gotta use windows, because recenter does wierd stuff if
7469           ;; the current buffer ain't the displayed window.
7470           (let ((owin (selected-window)))
7471             (select-window (get-buffer-window gnus-group-buffer t))
7472             (when (gnus-group-goto-group group)
7473               (recenter))
7474             (select-window owin))))
7475       ;; Mark this buffer as "prepared".
7476       (setq gnus-newsgroup-prepared t)
7477       t))))
7478
7479 (defun gnus-summary-prepare ()
7480   "Generate the summary buffer."
7481   (let ((buffer-read-only nil))
7482     (erase-buffer)
7483     (setq gnus-newsgroup-data nil
7484           gnus-newsgroup-data-reverse nil)
7485     (run-hooks 'gnus-summary-generate-hook)
7486     ;; Generate the buffer, either with threads or without.
7487     (when gnus-newsgroup-headers
7488       (gnus-summary-prepare-threads
7489        (if gnus-show-threads
7490            (gnus-sort-gathered-threads
7491             (funcall gnus-summary-thread-gathering-function
7492                      (gnus-sort-threads
7493                       (gnus-cut-threads (gnus-make-threads)))))
7494          ;; Unthreaded display.
7495          (gnus-sort-articles gnus-newsgroup-headers))))
7496     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7497     ;; Call hooks for modifying summary buffer.
7498     (goto-char (point-min))
7499     (run-hooks 'gnus-summary-prepare-hook)))
7500
7501 (defun gnus-gather-threads-by-subject (threads)
7502   "Gather threads by looking at Subject headers."
7503   (if (not gnus-summary-make-false-root)
7504       threads
7505     (let ((hashtb (gnus-make-hashtable 1023))
7506           (prev threads)
7507           (result threads)
7508           subject hthread whole-subject)
7509       (while threads
7510         (setq whole-subject (mail-header-subject (car (car threads))))
7511         (if (and gnus-summary-gather-exclude-subject
7512                  (string-match gnus-summary-gather-exclude-subject
7513                                whole-subject))
7514             () ; We don't want to do anything with this article.
7515           ;; We simplify the subject before looking it up in the
7516           ;; hash table.
7517           (setq subject
7518                 (cond
7519                  ;; Truncate the subject.
7520                  ((numberp gnus-summary-gather-subject-limit)
7521                   (setq subject (gnus-simplify-subject-re whole-subject))
7522                   (if (> (length subject) gnus-summary-gather-subject-limit)
7523                       (substring subject 0 gnus-summary-gather-subject-limit)
7524                     subject))
7525                  ;; Fuzzily simplify it.
7526                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7527                   (gnus-simplify-subject-fuzzy whole-subject))
7528                  ;; Just remove the leading "Re:".
7529                  (t
7530                   (gnus-simplify-subject-re whole-subject))))
7531
7532           (if (setq hthread (gnus-gethash subject hashtb))
7533               (progn
7534                 ;; We enter a dummy root into the thread, if we
7535                 ;; haven't done that already.
7536                 (unless (stringp (car (car hthread)))
7537                   (setcar hthread (list whole-subject (car hthread))))
7538                 ;; We add this new gathered thread to this gathered
7539                 ;; thread.
7540                 (setcdr (car hthread)
7541                         (nconc (cdr (car hthread)) (list (car threads))))
7542                 ;; Remove it from the list of threads.
7543                 (setcdr prev (cdr threads))
7544                 (setq threads prev))
7545             ;; Enter this thread into the hash table.
7546             (gnus-sethash subject threads hashtb)))
7547         (setq prev threads)
7548         (setq threads (cdr threads)))
7549       result)))
7550
7551 (defun gnus-summary-gather-threads-by-references (threads)
7552   "Gather threads by looking at References headers."
7553   (let ((idhashtb (gnus-make-hashtable 1023))
7554         (thhashtb (gnus-make-hashtable 1023))
7555         (prev threads)
7556         (result threads)
7557         ids references id gthread gid entered)
7558     (while threads
7559       (when (setq references (mail-header-references (caar threads)))
7560         (setq id (mail-header-id (caar threads)))
7561         (setq ids (gnus-split-references references))
7562         (setq entered nil)
7563         (while ids
7564           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7565               (progn
7566                 (gnus-sethash (car ids) id idhashtb)
7567                 (gnus-sethash id threads thhashtb))
7568             (setq gthread (gnus-gethash gid thhashtb))
7569             (unless entered
7570               ;; We enter a dummy root into the thread, if we
7571               ;; haven't done that already.
7572               (unless (stringp (caar gthread))
7573                 (setcar gthread (list (mail-header-subject (caar gthread))
7574                                       (car gthread))))
7575               ;; We add this new gathered thread to this gathered
7576               ;; thread.
7577               (setcdr (car gthread)
7578                       (nconc (cdar gthread) (list (car threads)))))
7579             ;; Add it into the thread hash table.
7580             (gnus-sethash id gthread thhashtb)
7581             (setq entered t)
7582             ;; Remove it from the list of threads.
7583             (setcdr prev (cdr threads))
7584             (setq threads prev))
7585           (setq ids (cdr ids))))
7586       (setq prev threads)
7587       (setq threads (cdr threads)))
7588     result))
7589
7590 (defun gnus-sort-gathered-threads (threads)
7591   "Sort subtreads inside each gathered thread by article number."
7592   (let ((result threads))
7593     (while threads
7594       (when (stringp (car (car threads)))
7595         (setcdr (car threads)
7596                 (sort (cdr (car threads)) 'gnus-thread-sort-by-number)))
7597       (setq threads (cdr threads)))
7598     result))
7599
7600 (defun gnus-make-threads ()
7601   "Go through the dependency hashtb and find the roots.  Return all threads."
7602   (let (threads)
7603     (mapatoms
7604      (lambda (refs)
7605        (unless (car (symbol-value refs))
7606          ;; These threads do not refer back to any other articles,
7607          ;; so they're roots.
7608          (setq threads (append (cdr (symbol-value refs)) threads))))
7609      gnus-newsgroup-dependencies)
7610     threads))
7611
7612 (defun gnus-build-sparse-threads ()
7613   (let ((headers gnus-newsgroup-headers)
7614         (deps gnus-newsgroup-dependencies)
7615         header references generation relations 
7616         cthread subject child end pthread relation)
7617     ;; First we create an alist of generations/relations, where 
7618     ;; generations is how much we trust the ralation, and the relation
7619     ;; is parent/child.
7620     (gnus-message 7 "Making sparse threads...")
7621     (save-excursion
7622       (nnheader-set-temp-buffer " *gnus sparse threads*")
7623       (while (setq header (pop headers))
7624         (when (and (setq references (mail-header-references header))
7625                    (not (string= references "")))
7626           (insert references)
7627           (setq child (downcase (mail-header-id header))
7628                 subject (mail-header-subject header))
7629           (setq generation 0)
7630           (while (search-backward ">" nil t)
7631             (setq end (1+ (point)))
7632             (when (search-backward "<" nil t)
7633               (push (list (incf generation) 
7634                           child (setq child (downcase
7635                                              (buffer-substring (point) end)))
7636                           subject)
7637                     relations)))
7638           (push (list (1+ generation) child nil subject) relations)
7639           (erase-buffer)))
7640       (kill-buffer (current-buffer)))
7641     ;; Sort over trustworthiness.
7642     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7643     (while (setq relation (pop relations))
7644       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7645                 (unless (car (symbol-value cthread))
7646                   ;; Make this article the parent of these threads.
7647                   (setcar (symbol-value cthread)
7648                           (vector gnus-reffed-article-number 
7649                                   (cadddr relation) 
7650                                   "" ""
7651                                   (cadr relation) 
7652                                   (or (caddr relation) "") 0 0 "")))
7653               (set cthread (list (vector gnus-reffed-article-number
7654                                          (cadddr relation) 
7655                                          "" "" (cadr relation) 
7656                                          (or (caddr relation) "") 0 0 ""))))
7657         (push gnus-reffed-article-number gnus-newsgroup-limit)
7658         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7659         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7660               gnus-newsgroup-reads)
7661         (decf gnus-reffed-article-number)
7662         ;; Make this new thread the child of its parent.
7663         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7664             (setcdr (symbol-value pthread)
7665                     (nconc (cdr (symbol-value pthread))
7666                            (list (symbol-value cthread))))
7667           (set pthread (list nil (symbol-value cthread))))))
7668     (gnus-message 7 "Making sparse threads...done")))
7669
7670 (defun gnus-build-old-threads ()
7671   ;; Look at all the articles that refer back to old articles, and
7672   ;; fetch the headers for the articles that aren't there.  This will
7673   ;; build complete threads - if the roots haven't been expired by the
7674   ;; server, that is.
7675   (let (id heads)
7676     (mapatoms
7677      (lambda (refs)
7678        (when (not (car (symbol-value refs)))
7679          (setq heads (cdr (symbol-value refs)))
7680          (while heads
7681            (if (memq (mail-header-number (car (car heads)))
7682                      gnus-newsgroup-dormant)
7683                (setq heads (cdr heads))
7684              (setq id (symbol-name refs))
7685              (while (and (setq id (gnus-build-get-header id))
7686                          (not (car (gnus-gethash
7687                                     id gnus-newsgroup-dependencies)))))
7688              (setq heads nil)))))
7689      gnus-newsgroup-dependencies)))
7690
7691 (defun gnus-build-get-header (id)
7692   ;; Look through the buffer of NOV lines and find the header to
7693   ;; ID.  Enter this line into the dependencies hash table, and return
7694   ;; the id of the parent article (if any).
7695   (let ((deps gnus-newsgroup-dependencies)
7696         found header)
7697     (prog1
7698         (save-excursion
7699           (set-buffer nntp-server-buffer)
7700           (goto-char (point-min))
7701           (while (and (not found) (search-forward id nil t))
7702             (beginning-of-line)
7703             (setq found (looking-at
7704                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7705                                  (regexp-quote id))))
7706             (or found (beginning-of-line 2)))
7707           (when found
7708             (let (ref)
7709               (beginning-of-line)
7710               (and
7711                (setq header (gnus-nov-parse-line
7712                              (read (current-buffer)) deps))
7713                (gnus-parent-id (mail-header-references header))))))
7714       (when header
7715         (let ((number (mail-header-number header)))
7716           (push number gnus-newsgroup-limit)
7717           (push header gnus-newsgroup-headers)
7718           (if (memq number gnus-newsgroup-unselected)
7719               (progn
7720                 (push number gnus-newsgroup-unreads)
7721                 (setq gnus-newsgroup-unselected
7722                       (delq number gnus-newsgroup-unselected)))
7723             (push number gnus-newsgroup-ancient)))))))
7724
7725 (defun gnus-summary-update-article (article &optional header)
7726   "Update ARTICLE in the summary buffer."
7727   (let ((id (mail-header-id (gnus-summary-article-header article)))
7728         (data (gnus-data-find article)))
7729     (setcar (gnus-id-to-thread id) nil)
7730     (gnus-summary-insert-subject id)
7731     ;; Set the (possibly) new article number in the data structure.
7732     (gnus-data-set-number data (gnus-id-to-article id))))
7733
7734 (defun gnus-rebuild-thread (id)
7735   "Rebuild the thread containing ID."
7736   (let ((dep gnus-newsgroup-dependencies)
7737         (buffer-read-only nil)
7738         current headers refs thread art data)
7739     (if (not gnus-show-threads)
7740         (setq thread (list (car (gnus-id-to-thread id))))
7741       ;; Get the thread this article is part of.
7742       (setq thread (gnus-remove-thread id)))
7743     (setq current (save-excursion
7744                     (and (zerop (forward-line -1))
7745                          (gnus-summary-article-number))))
7746     ;; If this is a gathered thread, we have to go some re-gathering.
7747     (when (stringp (car thread))
7748       (let ((subject (car thread))
7749             roots thr)
7750         (setq thread (cdr thread))
7751         (while thread
7752           (unless (memq (setq thr (gnus-id-to-thread
7753                                       (gnus-root-id
7754                                        (mail-header-id (car (car thread))))))
7755                         roots)
7756             (push thr roots))
7757           (setq thread (cdr thread)))
7758         ;; We now have all (unique) roots.
7759         (if (= (length roots) 1)
7760             ;; All the loose roots are now one solid root.
7761             (setq thread (car roots))
7762           (setq thread (cons subject (gnus-sort-threads roots))))))
7763     (let ((beg (point))
7764           threads)
7765       ;; We then insert this thread into the summary buffer.
7766       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7767         (gnus-summary-prepare-threads (list thread))
7768         (setq data (nreverse gnus-newsgroup-data))
7769         (setq threads gnus-newsgroup-threads))
7770       ;; We splice the new data into the data structure.
7771       (gnus-data-enter-list current data)
7772       (gnus-data-compute-positions)
7773       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7774
7775 (defun gnus-id-to-thread (id)
7776   "Return the (sub-)thread where ID appears."
7777   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7778
7779 (defun gnus-id-to-article (id)
7780   "Return the article number of ID."
7781   (let ((thread (gnus-id-to-thread id)))
7782     (when thread
7783       (mail-header-number (car thread)))))
7784
7785 (defun gnus-id-to-header (id)
7786   "Return the article headers of ID."
7787   (car (gnus-id-to-thread id)))
7788
7789 (defun gnus-article-displayed-root-p (article)
7790   "Say whether ARTICLE is a root(ish) article."
7791   (let ((level (gnus-summary-thread-level article))
7792         particle)
7793     (cond 
7794      ((null level) nil)
7795      ((zerop level) t)
7796      ((and (= 1 level)
7797            (null (setq particle (gnus-id-to-article
7798                                  (gnus-parent-id 
7799                                   (mail-header-references 
7800                                    (gnus-summary-article-header article))))))
7801            (null (gnus-summary-thread-level particle)))))))
7802
7803 (defun gnus-root-id (id)
7804   "Return the id of the root of the thread where ID appears."
7805   (let (last-id prev)
7806     (while (and id (setq prev (car (gnus-gethash
7807                                     (downcase id)
7808                                     gnus-newsgroup-dependencies))))
7809       (setq last-id id
7810             id (gnus-parent-id (mail-header-references prev))))
7811     last-id))
7812
7813 (defun gnus-remove-thread (id &optional dont-remove)
7814   "Remove the thread that has ID in it."
7815   (let ((dep gnus-newsgroup-dependencies)
7816         headers thread prev last-id)
7817     ;; First go up in this thread until we find the root.
7818     (setq last-id (gnus-root-id id))
7819     (setq headers (list (car (gnus-id-to-thread last-id))
7820                         (car (car (cdr (gnus-id-to-thread last-id))))))
7821     ;; We have now found the real root of this thread.  It might have
7822     ;; been gathered into some loose thread, so we have to search
7823     ;; through the threads to find the thread we wanted.
7824     (let ((threads gnus-newsgroup-threads)
7825           sub)
7826       (while threads
7827         (setq sub (car threads))
7828         (if (stringp (car sub))
7829             ;; This is a gathered threads, so we look at the roots
7830             ;; below it to find whether this article in in this
7831             ;; gathered root.
7832             (progn
7833               (setq sub (cdr sub))
7834               (while sub
7835                 (when (member (car (car sub)) headers)
7836                   (setq thread (car threads)
7837                         threads nil
7838                         sub nil))
7839                 (setq sub (cdr sub))))
7840           ;; It's an ordinary thread, so we check it.
7841           (when (eq (car sub) (car headers))
7842             (setq thread sub
7843                   threads nil)))
7844         (setq threads (cdr threads)))
7845       ;; If this article is in no thread, then it's a root.
7846       (if thread
7847           (unless dont-remove
7848             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7849         (setq thread (gnus-gethash (downcase last-id) dep)))
7850       (when thread
7851         (prog1
7852             thread ; We return this thread.
7853           (unless dont-remove
7854             (if (stringp (car thread))
7855                 (progn
7856                   ;; If we use dummy roots, then we have to remove the
7857                   ;; dummy root as well.
7858                   (when (eq gnus-summary-make-false-root 'dummy)
7859                     ;; Uhm.
7860                     )
7861                   (setq thread (cdr thread))
7862                   (while thread
7863                     (gnus-remove-thread-1 (car thread))
7864                     (setq thread (cdr thread))))
7865               (gnus-remove-thread-1 thread))))))))
7866
7867 (defun gnus-remove-thread-1 (thread)
7868   "Remove the thread THREAD recursively."
7869   (let ((number (mail-header-number (car thread)))
7870         pos)
7871     (when (setq pos (text-property-any
7872                      (point-min) (point-max) 'gnus-number number))
7873       (goto-char pos)
7874       (gnus-delete-line)
7875       (gnus-data-remove number))
7876     (setq thread (cdr thread))
7877     (while thread
7878       (gnus-remove-thread-1 (car thread))
7879       (setq thread (cdr thread)))))
7880
7881 (defun gnus-sort-threads (threads)
7882   "Sort THREADS."
7883   (if (not gnus-thread-sort-functions)
7884       threads
7885     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7886                     (car gnus-thread-sort-functions)
7887                   `(lambda (t1 t2)
7888                      ,(gnus-make-sort-function 
7889                        (reverse gnus-thread-sort-functions))))))
7890       (gnus-message 7 "Sorting threads...")
7891       (prog1
7892           (sort threads func)
7893         (gnus-message 7 "Sorting threads...done")))))
7894
7895 (defun gnus-sort-articles (articles)
7896   "Sort ARTICLES."
7897   (when gnus-article-sort-functions
7898     (let ((func (if (= 1 (length gnus-article-sort-functions))
7899                     (car gnus-article-sort-functions)
7900                   `(lambda (t1 t2)
7901                      ,(gnus-make-sort-function 
7902                        (reverse gnus-article-sort-functions))))))
7903       (gnus-message 7 "Sorting articles...")
7904       (prog1
7905           (setq gnus-newsgroup-headers (sort articles func))
7906         (gnus-message 7 "Sorting articles...done")))))
7907
7908 (defun gnus-make-sort-function (funs)
7909   "Return a composite sort condition based on the functions in FUNC."
7910   (if (cdr funs)
7911       `(or (,(car funs) t1 t2)
7912            (and (not (,(car funs) t2 t1))
7913                 ,(gnus-make-sort-function (cdr funs))))
7914     `(,(car funs) t1 t2)))
7915                  
7916 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7917 (defmacro gnus-thread-header (thread)
7918   ;; Return header of first article in THREAD.
7919   ;; Note that THREAD must never, ever be anything else than a variable -
7920   ;; using some other form will lead to serious barfage.
7921   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7922   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7923   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
7924         (vector thread) 2))
7925
7926 (defsubst gnus-article-sort-by-number (h1 h2)
7927   "Sort articles by article number."
7928   (< (mail-header-number h1)
7929      (mail-header-number h2)))
7930
7931 (defun gnus-thread-sort-by-number (h1 h2)
7932   "Sort threads by root article number."
7933   (gnus-article-sort-by-number
7934    (gnus-thread-header h1) (gnus-thread-header h2)))
7935
7936 (defsubst gnus-article-sort-by-author (h1 h2)
7937   "Sort articles by root author."
7938   (string-lessp
7939    (let ((extract (funcall
7940                    gnus-extract-address-components
7941                    (mail-header-from h1))))
7942      (or (car extract) (cdr extract)))
7943    (let ((extract (funcall
7944                    gnus-extract-address-components
7945                    (mail-header-from h2))))
7946      (or (car extract) (cdr extract)))))
7947
7948 (defun gnus-thread-sort-by-author (h1 h2)
7949   "Sort threads by root author."
7950   (gnus-article-sort-by-author
7951    (gnus-thread-header h1)  (gnus-thread-header h2)))
7952
7953 (defsubst gnus-article-sort-by-subject (h1 h2)
7954   "Sort articles by root subject."
7955   (string-lessp
7956    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
7957    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
7958
7959 (defun gnus-thread-sort-by-subject (h1 h2)
7960   "Sort threads by root subject."
7961   (gnus-article-sort-by-subject
7962    (gnus-thread-header h1) (gnus-thread-header h2)))
7963
7964 (defsubst gnus-article-sort-by-date (h1 h2)
7965   "Sort articles by root article date."
7966   (string-lessp
7967    (gnus-sortable-date (mail-header-date h1))
7968    (gnus-sortable-date (mail-header-date h2))))
7969
7970 (defun gnus-thread-sort-by-date (h1 h2)
7971   "Sort threads by root article date."
7972   (gnus-article-sort-by-date
7973    (gnus-thread-header h1) (gnus-thread-header h2)))
7974
7975 (defsubst gnus-article-sort-by-score (h1 h2)
7976   "Sort articles by root article score.
7977 Unscored articles will be counted as having a score of zero."
7978   (> (or (cdr (assq (mail-header-number h1)
7979                     gnus-newsgroup-scored))
7980          gnus-summary-default-score 0)
7981      (or (cdr (assq (mail-header-number h2)
7982                     gnus-newsgroup-scored))
7983          gnus-summary-default-score 0)))
7984
7985 (defun gnus-thread-sort-by-score (h1 h2)
7986   "Sort threads by root article score."
7987   (gnus-article-sort-by-score
7988    (gnus-thread-header h1) (gnus-thread-header h2)))
7989
7990 (defun gnus-thread-sort-by-total-score (h1 h2)
7991   "Sort threads by the sum of all scores in the thread.
7992 Unscored articles will be counted as having a score of zero."
7993   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
7994
7995 (defun gnus-thread-total-score (thread)
7996   ;;  This function find the total score of THREAD.
7997   (if (consp thread)
7998       (if (stringp (car thread))
7999           (apply gnus-thread-score-function 0
8000                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8001         (gnus-thread-total-score-1 thread))
8002     (gnus-thread-total-score-1 (list thread))))
8003
8004 (defun gnus-thread-total-score-1 (root)
8005   ;; This function find the total score of the thread below ROOT.
8006   (setq root (car root))
8007   (apply gnus-thread-score-function
8008          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8009              gnus-summary-default-score 0)
8010          (mapcar 'gnus-thread-total-score
8011                  (cdr (gnus-gethash (downcase (mail-header-id root))
8012                                     gnus-newsgroup-dependencies)))))
8013
8014 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8015 (defvar gnus-tmp-prev-subject nil)
8016 (defvar gnus-tmp-false-parent nil)
8017 (defvar gnus-tmp-root-expunged nil)
8018 (defvar gnus-tmp-dummy-line nil)
8019
8020 (defun gnus-summary-prepare-threads (threads)
8021   "Prepare summary buffer from THREADS and indentation LEVEL.
8022 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8023 or a straight list of headers."
8024   (gnus-message 7 "Generating summary...")
8025
8026   (setq gnus-newsgroup-threads threads)
8027   (beginning-of-line)
8028
8029   (let ((gnus-tmp-level 0)
8030         (default-score (or gnus-summary-default-score 0))
8031         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8032         thread number subject stack state gnus-tmp-gathered beg-match
8033         new-roots gnus-tmp-new-adopts thread-end
8034         gnus-tmp-header gnus-tmp-unread
8035         gnus-tmp-replied gnus-tmp-subject-or-nil
8036         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8037         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8038         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8039
8040     (setq gnus-tmp-prev-subject nil)
8041
8042     (if (vectorp (car threads))
8043         ;; If this is a straight (sic) list of headers, then a
8044         ;; threaded summary display isn't required, so we just create
8045         ;; an unthreaded one.
8046         (gnus-summary-prepare-unthreaded threads)
8047
8048       ;; Do the threaded display.
8049
8050       (while (or threads stack gnus-tmp-new-adopts new-roots)
8051
8052         (if (and (= gnus-tmp-level 0)
8053                  (not (setq gnus-tmp-dummy-line nil))
8054                  (or (not stack)
8055                      (= (car (car stack)) 0))
8056                  (not gnus-tmp-false-parent)
8057                  (or gnus-tmp-new-adopts new-roots))
8058             (if gnus-tmp-new-adopts
8059                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8060                       thread (list (car gnus-tmp-new-adopts))
8061                       gnus-tmp-header (car (car thread))
8062                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8063               (if new-roots
8064                   (setq thread (list (car new-roots))
8065                         gnus-tmp-header (car (car thread))
8066                         new-roots (cdr new-roots))))
8067
8068           (if threads
8069               ;; If there are some threads, we do them before the
8070               ;; threads on the stack.
8071               (setq thread threads
8072                     gnus-tmp-header (car (car thread)))
8073             ;; There were no current threads, so we pop something off
8074             ;; the stack.
8075             (setq state (car stack)
8076                   gnus-tmp-level (car state)
8077                   thread (cdr state)
8078                   stack (cdr stack)
8079                   gnus-tmp-header (car (car thread)))))
8080
8081         (setq gnus-tmp-false-parent nil)
8082         (setq gnus-tmp-root-expunged nil)
8083         (setq thread-end nil)
8084
8085         (if (stringp gnus-tmp-header)
8086             ;; The header is a dummy root.
8087             (cond
8088              ((eq gnus-summary-make-false-root 'adopt)
8089               ;; We let the first article adopt the rest.
8090               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8091                                                (cdr (cdr (car thread)))))
8092               (setq gnus-tmp-gathered
8093                     (nconc (mapcar
8094                             (lambda (h) (mail-header-number (car h)))
8095                             (cdr (cdr (car thread))))
8096                            gnus-tmp-gathered))
8097               (setq thread (cons (list (car (car thread))
8098                                        (car (cdr (car thread))))
8099                                  (cdr thread)))
8100               (setq gnus-tmp-level -1
8101                     gnus-tmp-false-parent t))
8102              ((eq gnus-summary-make-false-root 'empty)
8103               ;; We print adopted articles with empty subject fields.
8104               (setq gnus-tmp-gathered
8105                     (nconc (mapcar
8106                             (lambda (h) (mail-header-number (car h)))
8107                             (cdr (cdr (car thread))))
8108                            gnus-tmp-gathered))
8109               (setq gnus-tmp-level -1))
8110              ((eq gnus-summary-make-false-root 'dummy)
8111               ;; We remember that we probably want to output a dummy
8112               ;; root.
8113               (setq gnus-tmp-dummy-line gnus-tmp-header)
8114               (setq gnus-tmp-prev-subject gnus-tmp-header))
8115              (t
8116               ;; We do not make a root for the gathered
8117               ;; sub-threads at all.
8118               (setq gnus-tmp-level -1)))
8119
8120           (setq number (mail-header-number gnus-tmp-header)
8121                 subject (mail-header-subject gnus-tmp-header))
8122
8123           (cond
8124            ;; If the thread has changed subject, we might want to make
8125            ;; this subthread into a root.
8126            ((and (null gnus-thread-ignore-subject)
8127                  (not (zerop gnus-tmp-level))
8128                  gnus-tmp-prev-subject
8129                  (not (inline
8130                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8131             (setq new-roots (nconc new-roots (list (car thread)))
8132                   thread-end t
8133                   gnus-tmp-header nil))
8134            ;; If the article lies outside the current limit,
8135            ;; then we do not display it.
8136            ((and (not (memq number gnus-newsgroup-limit))
8137                  (not gnus-tmp-dummy-line))
8138             (setq gnus-tmp-gathered
8139                   (nconc (mapcar
8140                           (lambda (h) (mail-header-number (car h)))
8141                           (cdr (car thread)))
8142                          gnus-tmp-gathered))
8143             (setq gnus-tmp-new-adopts (if (cdr (car thread))
8144                                           (append gnus-tmp-new-adopts
8145                                                   (cdr (car thread)))
8146                                         gnus-tmp-new-adopts)
8147                   thread-end t
8148                   gnus-tmp-header nil)
8149             (when (zerop gnus-tmp-level)
8150               (setq gnus-tmp-root-expunged t)))
8151            ;; Perhaps this article is to be marked as read?
8152            ((and gnus-summary-mark-below
8153                  (< (or (cdr (assq number gnus-newsgroup-scored))
8154                         default-score)
8155                     gnus-summary-mark-below))
8156             (setq gnus-newsgroup-unreads
8157                   (delq number gnus-newsgroup-unreads))
8158             (if gnus-newsgroup-auto-expire
8159                 (push number gnus-newsgroup-expirable)
8160               (push (cons number gnus-low-score-mark)
8161                     gnus-newsgroup-reads))))
8162
8163           (when gnus-tmp-header
8164             ;; We may have an old dummy line to output before this
8165             ;; article.
8166             (when gnus-tmp-dummy-line
8167               (gnus-summary-insert-dummy-line
8168                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8169               (setq gnus-tmp-dummy-line nil))
8170
8171             ;; Compute the mark.
8172             (setq
8173              gnus-tmp-unread
8174              (cond
8175               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8176               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8177               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8178               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8179               (t (or (cdr (assq number gnus-newsgroup-reads))
8180                      gnus-ancient-mark))))
8181
8182             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8183                                   gnus-tmp-header gnus-tmp-level)
8184                   gnus-newsgroup-data)
8185
8186             ;; Actually insert the line.
8187             (setq
8188              gnus-tmp-subject-or-nil
8189              (cond
8190               ((and gnus-thread-ignore-subject
8191                     gnus-tmp-prev-subject
8192                     (not (inline (gnus-subject-equal
8193                                   gnus-tmp-prev-subject subject))))
8194                subject)
8195               ((zerop gnus-tmp-level)
8196                (if (and (eq gnus-summary-make-false-root 'empty)
8197                         (memq number gnus-tmp-gathered)
8198                         gnus-tmp-prev-subject
8199                         (inline (gnus-subject-equal
8200                                  gnus-tmp-prev-subject subject)))
8201                    gnus-summary-same-subject
8202                  subject))
8203               (t gnus-summary-same-subject)))
8204             (if (and (eq gnus-summary-make-false-root 'adopt)
8205                      (= gnus-tmp-level 1)
8206                      (memq number gnus-tmp-gathered))
8207                 (setq gnus-tmp-opening-bracket ?\<
8208                       gnus-tmp-closing-bracket ?\>)
8209               (setq gnus-tmp-opening-bracket ?\[
8210                     gnus-tmp-closing-bracket ?\]))
8211             (setq
8212              gnus-tmp-indentation
8213              (aref gnus-thread-indent-array gnus-tmp-level)
8214              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8215              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8216                                 gnus-summary-default-score 0)
8217              gnus-tmp-score-char
8218              (if (or (null gnus-summary-default-score)
8219                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8220                          gnus-summary-zcore-fuzz)) ? 
8221                (if (< gnus-tmp-score gnus-summary-default-score)
8222                    gnus-score-below-mark gnus-score-over-mark))
8223              gnus-tmp-replied
8224              (cond ((memq number gnus-newsgroup-processable)
8225                     gnus-process-mark)
8226                    ((memq number gnus-newsgroup-cached)
8227                     gnus-cached-mark)
8228                    ((memq number gnus-newsgroup-replied)
8229                     gnus-replied-mark)
8230                    (t gnus-unread-mark))
8231              gnus-tmp-from (mail-header-from gnus-tmp-header)
8232              gnus-tmp-name
8233              (cond
8234               ((string-match "(.+)" gnus-tmp-from)
8235                (substring gnus-tmp-from
8236                           (1+ (match-beginning 0)) (1- (match-end 0))))
8237               ((string-match "<[^>]+> *$" gnus-tmp-from)
8238                (setq beg-match (match-beginning 0))
8239                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8240                         (substring gnus-tmp-from (1+ (match-beginning 0))
8241                                    (1- (match-end 0))))
8242                    (substring gnus-tmp-from 0 beg-match)))
8243               (t gnus-tmp-from)))
8244             (when (string= gnus-tmp-name "")
8245               (setq gnus-tmp-name gnus-tmp-from))
8246             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8247             (put-text-property
8248              (point)
8249              (progn (eval gnus-summary-line-format-spec) (point))
8250              'gnus-number number)
8251             (when gnus-visual-p
8252               (forward-line -1)
8253               (run-hooks 'gnus-summary-update-hook)
8254               (forward-line 1))
8255
8256             (setq gnus-tmp-prev-subject subject)))
8257
8258         (when (nth 1 thread)
8259           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8260         (incf gnus-tmp-level)
8261         (setq threads (if thread-end nil (cdr (car thread))))
8262         (unless threads
8263           (setq gnus-tmp-level 0)))))
8264   (gnus-message 7 "Generating summary...done"))
8265
8266 (defun gnus-summary-prepare-unthreaded (headers)
8267   "Generate an unthreaded summary buffer based on HEADERS."
8268   (let (header number mark)
8269
8270     (while headers
8271       (setq header (car headers)
8272             headers (cdr headers)
8273             number (mail-header-number header))
8274
8275       ;; We may have to root out some bad articles...
8276       (when (memq number gnus-newsgroup-limit)
8277         (when (and gnus-summary-mark-below
8278                    (< (or (cdr (assq number gnus-newsgroup-scored))
8279                           gnus-summary-default-score 0)
8280                       gnus-summary-mark-below))
8281           (setq gnus-newsgroup-unreads
8282                 (delq number gnus-newsgroup-unreads))
8283           (if gnus-newsgroup-auto-expire
8284               (push number gnus-newsgroup-expirable)
8285             (push (cons number gnus-low-score-mark)
8286                   gnus-newsgroup-reads)))
8287
8288         (setq mark
8289               (cond
8290                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8291                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8292                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8293                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8294                (t (or (cdr (assq number gnus-newsgroup-reads))
8295                       gnus-ancient-mark))))
8296         (setq gnus-newsgroup-data
8297               (cons (gnus-data-make number mark (1+ (point)) header 0)
8298                     gnus-newsgroup-data))
8299         (gnus-summary-insert-line
8300          header 0 nil mark (memq number gnus-newsgroup-replied)
8301          (memq number gnus-newsgroup-expirable)
8302          (mail-header-subject header) nil
8303          (cdr (assq number gnus-newsgroup-scored))
8304          (memq number gnus-newsgroup-processable))))))
8305
8306 (defun gnus-select-newsgroup (group &optional read-all)
8307   "Select newsgroup GROUP.
8308 If READ-ALL is non-nil, all articles in the group are selected."
8309   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8310          (info (nth 2 entry))
8311          articles fetched-articles cached)
8312
8313     (or (gnus-check-server
8314          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8315         (error "Couldn't open server"))
8316
8317     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8318         (gnus-activate-group group) ; Or we can activate it...
8319         (progn ; Or we bug out.
8320           (kill-buffer (current-buffer))
8321           (error "Couldn't request group %s: %s"
8322                  group (gnus-status-message group))))
8323
8324     (setq gnus-newsgroup-name group)
8325     (setq gnus-newsgroup-unselected nil)
8326     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8327
8328     (and gnus-asynchronous
8329          (gnus-check-backend-function
8330           'request-asynchronous gnus-newsgroup-name)
8331          (setq gnus-newsgroup-async
8332                (gnus-request-asynchronous gnus-newsgroup-name)))
8333
8334     ;; Adjust and set lists of article marks.
8335     (when info
8336       (gnus-adjust-marked-articles info))
8337
8338     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8339     (when (gnus-virtual-group-p group)
8340       (setq cached gnus-newsgroup-cached))
8341
8342     (setq gnus-newsgroup-unreads
8343           (gnus-set-difference
8344            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8345            gnus-newsgroup-dormant))
8346
8347     (setq gnus-newsgroup-processable nil)
8348
8349     (setq articles (gnus-articles-to-read group read-all))
8350
8351     (cond
8352      ((null articles)
8353       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8354       'quit)
8355      ((eq articles 0) nil)
8356      (t
8357       ;; Init the dependencies hash table.
8358       (setq gnus-newsgroup-dependencies
8359             (gnus-make-hashtable (length articles)))
8360       ;; Retrieve the headers and read them in.
8361       (gnus-message 5 "Fetching headers...")
8362       (setq gnus-newsgroup-headers
8363             (if (eq 'nov
8364                     (setq gnus-headers-retrieved-by
8365                           (gnus-retrieve-headers
8366                            articles gnus-newsgroup-name
8367                            ;; We might want to fetch old headers, but
8368                            ;; not if there is only 1 article.
8369                            (and gnus-fetch-old-headers
8370                                 (or (and
8371                                      (not (eq gnus-fetch-old-headers 'some))
8372                                      (not (numberp gnus-fetch-old-headers)))
8373                                     (> (length articles) 1))))))
8374                 (gnus-get-newsgroup-headers-xover articles)
8375               (gnus-get-newsgroup-headers)))
8376       (gnus-message 5 "Fetching headers...done")
8377
8378       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8379       (when cached
8380         (setq gnus-newsgroup-cached cached))
8381
8382       ;; Set the initial limit.
8383       (setq gnus-newsgroup-limit (copy-sequence articles))
8384       ;; Remove canceled articles from the list of unread articles.
8385       (setq gnus-newsgroup-unreads
8386             (gnus-set-sorted-intersection
8387              gnus-newsgroup-unreads
8388              (setq fetched-articles
8389                    (mapcar (lambda (headers) (mail-header-number headers))
8390                            gnus-newsgroup-headers))))
8391       ;; Removed marked articles that do not exist.
8392       (gnus-update-missing-marks
8393        (gnus-sorted-complement fetched-articles articles))
8394       ;; We might want to build some more threads first.
8395       (and gnus-fetch-old-headers
8396            (eq gnus-headers-retrieved-by 'nov)
8397            (gnus-build-old-threads))
8398       ;; Check whether auto-expire is to be done in this group.
8399       (setq gnus-newsgroup-auto-expire
8400             (gnus-group-auto-expirable-p group))
8401       ;; First and last article in this newsgroup.
8402       (and gnus-newsgroup-headers
8403            (setq gnus-newsgroup-begin
8404                  (mail-header-number (car gnus-newsgroup-headers)))
8405            (setq gnus-newsgroup-end
8406                  (mail-header-number
8407                   (gnus-last-element gnus-newsgroup-headers))))
8408       (setq gnus-reffed-article-number -1)
8409       ;; GROUP is successfully selected.
8410       (or gnus-newsgroup-headers t)))))
8411
8412 (defun gnus-articles-to-read (group read-all)
8413   ;; Find out what articles the user wants to read.
8414   (let* ((articles
8415           ;; Select all articles if `read-all' is non-nil, or if there
8416           ;; are no unread articles.
8417           (if (or read-all
8418                   (and (zerop (length gnus-newsgroup-marked))
8419                        (zerop (length gnus-newsgroup-unreads))))
8420               (gnus-uncompress-range (gnus-active group))
8421             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8422                           (copy-sequence gnus-newsgroup-unreads))
8423                   '<)))
8424          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8425          (scored (length scored-list))
8426          (number (length articles))
8427          (marked (+ (length gnus-newsgroup-marked)
8428                     (length gnus-newsgroup-dormant)))
8429          (select
8430           (cond
8431            ((numberp read-all)
8432             read-all)
8433            (t
8434             (condition-case ()
8435                 (cond
8436                  ((and (or (<= scored marked) (= scored number))
8437                        (numberp gnus-large-newsgroup)
8438                        (> number gnus-large-newsgroup))
8439                   (let ((input
8440                          (read-string
8441                           (format
8442                            "How many articles from %s (default %d): "
8443                            gnus-newsgroup-name number))))
8444                     (if (string-match "^[ \t]*$" input) number input)))
8445                  ((and (> scored marked) (< scored number))
8446                   (let ((input
8447                          (read-string
8448                           (format "%s %s (%d scored, %d total): "
8449                                   "How many articles from"
8450                                   group scored number))))
8451                     (if (string-match "^[ \t]*$" input)
8452                         number input)))
8453                  (t number))
8454               (quit nil))))))
8455     (setq select (if (stringp select) (string-to-number select) select))
8456     (if (or (null select) (zerop select))
8457         select
8458       (if (and (not (zerop scored)) (<= (abs select) scored))
8459           (progn
8460             (setq articles (sort scored-list '<))
8461             (setq number (length articles)))
8462         (setq articles (copy-sequence articles)))
8463
8464       (if (< (abs select) number)
8465           (if (< select 0)
8466               ;; Select the N oldest articles.
8467               (setcdr (nthcdr (1- (abs select)) articles) nil)
8468             ;; Select the N most recent articles.
8469             (setq articles (nthcdr (- number select) articles))))
8470       (setq gnus-newsgroup-unselected
8471             (gnus-sorted-intersection
8472              gnus-newsgroup-unreads
8473              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8474       articles)))
8475
8476 (defun gnus-killed-articles (killed articles)
8477   (let (out)
8478     (while articles
8479       (if (inline (gnus-member-of-range (car articles) killed))
8480           (setq out (cons (car articles) out)))
8481       (setq articles (cdr articles)))
8482     out))
8483
8484 (defun gnus-uncompress-marks (marks)
8485   "Uncompress the mark ranges in MARKS."
8486   (let ((uncompressed '(score bookmark))
8487         out)
8488     (while marks
8489       (if (memq (caar marks) uncompressed)
8490           (push (car marks) out)
8491         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8492       (setq marks (cdr marks)))
8493     out))
8494
8495 (defun gnus-adjust-marked-articles (info)
8496   "Set all article lists and remove all marks that are no longer legal."
8497   (let* ((marked-lists (gnus-info-marks info))
8498          (active (gnus-active (gnus-info-group info)))
8499          (min (car active))
8500          (max (cdr active))
8501          (types gnus-article-mark-lists)
8502          (uncompressed '(score bookmark))
8503          marks var articles article mark)
8504
8505     (while marked-lists
8506       (setq marks (pop marked-lists))
8507       (set (setq var (intern (format "gnus-newsgroup-%s"
8508                                      (car (rassq (setq mark (car marks))
8509                                                  types)))))
8510            (if (memq (car marks) uncompressed) (cdr marks)
8511              (gnus-uncompress-range (cdr marks))))
8512
8513       (setq articles (symbol-value var))
8514
8515       ;; All articles have to be subsets of the active articles.
8516       (cond
8517        ;; Adjust "simple" lists.
8518        ((memq mark '(tick dormant expirable reply killed save))
8519         (while articles
8520           (when (or (< (setq article (pop articles)) min) (> article max))
8521             (set var (delq article (symbol-value var))))))
8522        ;; Adjust assocs.
8523        ((memq mark '(score bookmark))
8524         (while articles
8525           (when (or (< (car (setq article (pop articles))) min)
8526                     (> (car article) max))
8527             (set var (delq article (symbol-value var))))))))))
8528
8529 (defun gnus-update-missing-marks (missing)
8530   "Go through the list of MISSING articles and remove them mark lists."
8531   (when missing
8532     (let ((types gnus-article-mark-lists)
8533           var m)
8534       ;; Go through all types.
8535       (while types
8536         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8537         (when (symbol-value var)
8538           ;; This list has articles.  So we delete all missing articles
8539           ;; from it.
8540           (setq m missing)
8541           (while m
8542             (set var (delq (pop m) (symbol-value var)))))))))
8543
8544 (defun gnus-update-marks ()
8545   "Enter the various lists of marked articles into the newsgroup info list."
8546   (let ((types gnus-article-mark-lists)
8547         (info (gnus-get-info gnus-newsgroup-name))
8548         (uncompressed '(score bookmark killed))
8549         var type list newmarked symbol)
8550     (when info
8551       ;; Add all marks lists that are non-nil to the list of marks lists.
8552       (while types
8553         (setq type (pop types))
8554         (when (setq list (symbol-value
8555                           (setq symbol
8556                                 (intern (format "gnus-newsgroup-%s"
8557                                                 (car type))))))
8558           (push (cons (cdr type)
8559                       (if (memq (cdr type) uncompressed) list
8560                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8561                 newmarked)))
8562
8563       ;; Enter these new marks into the info of the group.
8564       (if (nthcdr 3 info)
8565           (setcar (nthcdr 3 info) newmarked)
8566         ;; Add the marks lists to the end of the info.
8567         (when newmarked
8568           (setcdr (nthcdr 2 info) (list newmarked))))
8569
8570       ;; Cut off the end of the info if there's nothing else there.
8571       (let ((i 5))
8572         (while (and (> i 2)
8573                     (not (nth i info)))
8574           (when (nthcdr (decf i) info)
8575             (setcdr (nthcdr i info) nil)))))))
8576
8577 (defun gnus-add-marked-articles (group type articles &optional info force)
8578   ;; Add ARTICLES of TYPE to the info of GROUP.
8579   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8580   ;; add, but replace marked articles of TYPE with ARTICLES.
8581   (let ((info (or info (gnus-get-info group)))
8582         (uncompressed '(score bookmark killed))
8583         marked m)
8584     (or (not info)
8585         (and (not (setq marked (nthcdr 3 info)))
8586              (setcdr (nthcdr 2 info)
8587                      (list (list (cons type (gnus-compress-sequence
8588                                              articles t))))))
8589         (and (not (setq m (assq type (car marked))))
8590              (setcar marked
8591                      (cons (cons type (gnus-compress-sequence articles t) )
8592                            (car marked))))
8593         (if force
8594             (setcdr m (gnus-compress-sequence articles t))
8595           (setcdr m (gnus-compress-sequence
8596                      (sort (nconc (gnus-uncompress-range m)
8597                                   (copy-sequence articles)) '<) t))))))
8598
8599 (defun gnus-set-mode-line (where)
8600   "This function sets the mode line of the article or summary buffers.
8601 If WHERE is `summary', the summary mode line format will be used."
8602   ;; Is this mode line one we keep updated?
8603   (when (memq where gnus-updated-mode-lines)
8604     (let (mode-string)
8605       (save-excursion
8606         ;; We evaluate this in the summary buffer since these
8607         ;; variables are buffer-local to that buffer.
8608         (set-buffer gnus-summary-buffer)
8609         ;; We bind all these variables that are used in the `eval' form
8610         ;; below.
8611         (let* ((mformat (symbol-value
8612                          (intern
8613                           (format "gnus-%s-mode-line-format-spec" where))))
8614                (gnus-tmp-group-name gnus-newsgroup-name)
8615                (gnus-tmp-article-number (or gnus-current-article 0))
8616                (gnus-tmp-unread gnus-newsgroup-unreads)
8617                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8618                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8619                (gnus-tmp-unread-and-unselected
8620                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8621                             (zerop gnus-tmp-unselected)) "")
8622                       ((zerop gnus-tmp-unselected)
8623                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8624                       (t (format "{%d(+%d) more}"
8625                                  gnus-tmp-unread-and-unticked
8626                                  gnus-tmp-unselected))))
8627                (gnus-tmp-subject
8628                 (if (and gnus-current-headers
8629                          (vectorp gnus-current-headers))
8630                     (mail-header-subject gnus-current-headers) ""))
8631                max-len
8632                gnus-tmp-header);; passed as argument to any user-format-funcs
8633           (setq mode-string (eval mformat))
8634           (setq max-len (max 4 (if gnus-mode-non-string-length
8635                                    (- (frame-width)
8636                                       gnus-mode-non-string-length)
8637                                  (length mode-string))))
8638           ;; We might have to chop a bit of the string off...
8639           (when (> (length mode-string) max-len)
8640             (setq mode-string
8641                   (concat (gnus-truncate-string mode-string (- max-len 3))
8642                           "...")))
8643           ;; Pad the mode string a bit.
8644           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8645       ;; Update the mode line.
8646       (setq mode-line-buffer-identification (list mode-string))
8647       (set-buffer-modified-p t))))
8648
8649 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8650   "Go through the HEADERS list and add all Xrefs to a hash table.
8651 The resulting hash table is returned, or nil if no Xrefs were found."
8652   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8653          (virtual (gnus-virtual-group-p from-newsgroup))
8654          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8655          (xref-hashtb (make-vector 63 0))
8656          start group entry number xrefs header)
8657     (while headers
8658       (setq header (pop headers))
8659       (when (and (setq xrefs (mail-header-xref header))
8660                  (not (memq (setq number (mail-header-number header))
8661                             unreads)))
8662         (setq start 0)
8663         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8664           (setq start (match-end 0))
8665           (setq group (concat prefix (substring xrefs (match-beginning 1)
8666                                                 (match-end 1))))
8667           (setq number
8668                 (string-to-int (substring xrefs (match-beginning 2)
8669                                           (match-end 2))))
8670           (if (setq entry (gnus-gethash group xref-hashtb))
8671               (setcdr entry (cons number (cdr entry)))
8672             (gnus-sethash group (cons number nil) xref-hashtb)))))
8673     (and start xref-hashtb)))
8674
8675 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8676   "Look through all the headers and mark the Xrefs as read."
8677   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8678         name entry info xref-hashtb idlist method nth4)
8679     (save-excursion
8680       (set-buffer gnus-group-buffer)
8681       (when (setq xref-hashtb
8682                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8683         (mapatoms
8684          (lambda (group)
8685            (unless (string= from-newsgroup (setq name (symbol-name group)))
8686              (setq idlist (symbol-value group))
8687              ;; Dead groups are not updated.
8688              (and (prog1
8689                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8690                             info (nth 2 entry))
8691                     (if (stringp (setq nth4 (gnus-info-method info)))
8692                         (setq nth4 (gnus-server-to-method nth4))))
8693                   ;; Only do the xrefs if the group has the same
8694                   ;; select method as the group we have just read.
8695                   (or (gnus-methods-equal-p
8696                        nth4 (gnus-find-method-for-group from-newsgroup))
8697                       virtual
8698                       (equal nth4 (setq method (gnus-find-method-for-group
8699                                                 from-newsgroup)))
8700                       (and (equal (car nth4) (car method))
8701                            (equal (nth 1 nth4) (nth 1 method))))
8702                   gnus-use-cross-reference
8703                   (or (not (eq gnus-use-cross-reference t))
8704                       virtual
8705                       ;; Only do cross-references on subscribed
8706                       ;; groups, if that is what is wanted.
8707                       (<= (gnus-info-level info) gnus-level-subscribed))
8708                   (gnus-group-make-articles-read name idlist))))
8709          xref-hashtb)))))
8710
8711 (defun gnus-group-make-articles-read (group articles)
8712   (let* ((num 0)
8713          (entry (gnus-gethash group gnus-newsrc-hashtb))
8714          (info (nth 2 entry))
8715          (active (gnus-active group))
8716          range)
8717     ;; First peel off all illegal article numbers.
8718     (if active
8719         (let ((ids articles)
8720               id first)
8721           (while ids
8722             (setq id (car ids))
8723             (if (and first (> id (cdr active)))
8724                 (progn
8725                   ;; We'll end up in this situation in one particular
8726                   ;; obscure situation.  If you re-scan a group and get
8727                   ;; a new article that is cross-posted to a different
8728                   ;; group that has not been re-scanned, you might get
8729                   ;; crossposted article that has a higher number than
8730                   ;; Gnus believes possible.  So we re-activate this
8731                   ;; group as well.  This might mean doing the
8732                   ;; crossposting thingy will *increase* the number
8733                   ;; of articles in some groups.  Tsk, tsk.
8734                   (setq active (or (gnus-activate-group group) active))))
8735             (if (or (> id (cdr active))
8736                     (< id (car active)))
8737                 (setq articles (delq id articles)))
8738             (setq ids (cdr ids)))))
8739     ;; If the read list is nil, we init it.
8740     (and active
8741          (null (gnus-info-read info))
8742          (> (car active) 1)
8743          (gnus-info-set-read info (cons 1 (1- (car active)))))
8744     ;; Then we add the read articles to the range.
8745     (gnus-info-set-read
8746      info
8747      (setq range
8748            (gnus-add-to-range
8749             (gnus-info-read info) (setq articles (sort articles '<)))))
8750     ;; Then we have to re-compute how many unread
8751     ;; articles there are in this group.
8752     (if active
8753         (progn
8754           (cond
8755            ((not range)
8756             (setq num (- (1+ (cdr active)) (car active))))
8757            ((not (listp (cdr range)))
8758             (setq num (- (cdr active) (- (1+ (cdr range))
8759                                          (car range)))))
8760            (t
8761             (while range
8762               (if (numberp (car range))
8763                   (setq num (1+ num))
8764                 (setq num (+ num (- (1+ (cdr (car range)))
8765                                     (car (car range))))))
8766               (setq range (cdr range)))
8767             (setq num (- (cdr active) num))))
8768           ;; Update the number of unread articles.
8769           (setcar entry num)
8770           ;; Update the group buffer.
8771           (gnus-group-update-group group t)))))
8772
8773 (defun gnus-methods-equal-p (m1 m2)
8774   (let ((m1 (or m1 gnus-select-method))
8775         (m2 (or m2 gnus-select-method)))
8776     (or (equal m1 m2)
8777         (and (eq (car m1) (car m2))
8778              (or (not (memq 'address (assoc (symbol-name (car m1))
8779                                             gnus-valid-select-methods)))
8780                  (equal (nth 1 m1) (nth 1 m2)))))))
8781
8782 (defsubst gnus-header-value ()
8783   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8784
8785 (defvar gnus-newsgroup-none-id 0)
8786
8787 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8788   (let ((cur nntp-server-buffer)
8789         (dependencies
8790          (or dependencies
8791              (save-excursion (set-buffer gnus-summary-buffer)
8792                              gnus-newsgroup-dependencies)))
8793         headers id id-dep ref-dep end ref)
8794     (save-excursion
8795       (set-buffer nntp-server-buffer)
8796       (let ((case-fold-search t)
8797             in-reply-to header number p lines)
8798         (goto-char (point-min))
8799         ;; Search to the beginning of the next header.  Error messages
8800         ;; do not begin with 2 or 3.
8801         (while (re-search-forward "^[23][0-9]+ " nil t)
8802           (setq id nil
8803                 ref nil)
8804           ;; This implementation of this function, with nine
8805           ;; search-forwards instead of the one re-search-forward and
8806           ;; a case (which basically was the old function) is actually
8807           ;; about twice as fast, even though it looks messier.  You
8808           ;; can't have everything, I guess.  Speed and elegance
8809           ;; doesn't always go hand in hand.
8810           (setq
8811            header
8812            (vector
8813             ;; Number.
8814             (prog1
8815                 (read cur)
8816               (end-of-line)
8817               (setq p (point))
8818               (narrow-to-region (point)
8819                                 (or (and (search-forward "\n.\n" nil t)
8820                                          (- (point) 2))
8821                                     (point))))
8822             ;; Subject.
8823             (progn
8824               (goto-char p)
8825               (if (search-forward "\nsubject: " nil t)
8826                   (gnus-header-value) "(none)"))
8827             ;; From.
8828             (progn
8829               (goto-char p)
8830               (if (search-forward "\nfrom: " nil t)
8831                   (gnus-header-value) "(nobody)"))
8832             ;; Date.
8833             (progn
8834               (goto-char p)
8835               (if (search-forward "\ndate: " nil t)
8836                   (gnus-header-value) ""))
8837             ;; Message-ID.
8838             (progn
8839               (goto-char p)
8840               (if (search-forward "\nmessage-id: " nil t)
8841                   (setq id (gnus-header-value))
8842                 ;; If there was no message-id, we just fake one to make
8843                 ;; subsequent routines simpler.
8844                 (setq id (concat "none+"
8845                                  (int-to-string
8846                                   (setq gnus-newsgroup-none-id
8847                                         (1+ gnus-newsgroup-none-id)))))))
8848             ;; References.
8849             (progn
8850               (goto-char p)
8851               (if (search-forward "\nreferences: " nil t)
8852                   (prog1
8853                       (gnus-header-value)
8854                     (setq end (match-end 0))
8855                     (save-excursion
8856                       (setq ref
8857                             (downcase
8858                              (buffer-substring
8859                               (progn
8860                                 (end-of-line)
8861                                 (search-backward ">" end t)
8862                                 (1+ (point)))
8863                               (progn
8864                                 (search-backward "<" end t)
8865                                 (point)))))))
8866                 ;; Get the references from the in-reply-to header if there
8867                 ;; were no references and the in-reply-to header looks
8868                 ;; promising.
8869                 (if (and (search-forward "\nin-reply-to: " nil t)
8870                          (setq in-reply-to (gnus-header-value))
8871                          (string-match "<[^>]+>" in-reply-to))
8872                     (prog1
8873                         (setq ref (substring in-reply-to (match-beginning 0)
8874                                              (match-end 0)))
8875                       (setq ref (downcase ref))))
8876                 (setq ref "")))
8877             ;; Chars.
8878             0
8879             ;; Lines.
8880             (progn
8881               (goto-char p)
8882               (if (search-forward "\nlines: " nil t)
8883                   (if (numberp (setq lines (read cur)))
8884                       lines 0)
8885                 0))
8886             ;; Xref.
8887             (progn
8888               (goto-char p)
8889               (and (search-forward "\nxref: " nil t)
8890                    (gnus-header-value)))))
8891           ;; We do the threading while we read the headers.  The
8892           ;; message-id and the last reference are both entered into
8893           ;; the same hash table.  Some tippy-toeing around has to be
8894           ;; done in case an article has arrived before the article
8895           ;; which it refers to.
8896           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8897               (if (and (car (symbol-value id-dep))
8898                        (not force-new))
8899                   ;; An article with this Message-ID has already
8900                   ;; been seen, so we ignore this one, except we add
8901                   ;; any additional Xrefs (in case the two articles
8902                   ;; came from different servers).
8903                   (progn
8904                     (mail-header-set-xref
8905                      (car (symbol-value id-dep))
8906                      (concat (or (mail-header-xref
8907                                   (car (symbol-value id-dep))) "")
8908                              (or (mail-header-xref header) "")))
8909                     (setq header nil))
8910                 (setcar (symbol-value id-dep) header))
8911             (set id-dep (list header)))
8912           (when header
8913             (if (boundp (setq ref-dep (intern ref dependencies)))
8914                 (setcdr (symbol-value ref-dep)
8915                         (nconc (cdr (symbol-value ref-dep))
8916                                (list (symbol-value id-dep))))
8917               (set ref-dep (list nil (symbol-value id-dep))))
8918             (setq headers (cons header headers)))
8919           (goto-char (point-max))
8920           (widen))
8921         (nreverse headers)))))
8922
8923 ;; The following macros and functions were written by Felix Lee
8924 ;; <flee@cse.psu.edu>.
8925
8926 (defmacro gnus-nov-read-integer ()
8927   '(prog1
8928        (if (= (following-char) ?\t)
8929            0
8930          (let ((num (condition-case nil (read buffer) (error nil))))
8931            (if (numberp num) num 0)))
8932      (or (eobp) (forward-char 1))))
8933
8934 (defmacro gnus-nov-skip-field ()
8935   '(search-forward "\t" eol 'move))
8936
8937 (defmacro gnus-nov-field ()
8938   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
8939
8940 ;; Goes through the xover lines and returns a list of vectors
8941 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
8942   "Parse the news overview data in the server buffer, and return a
8943 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
8944   ;; Get the Xref when the users reads the articles since most/some
8945   ;; NNTP servers do not include Xrefs when using XOVER.
8946   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
8947   (let ((cur nntp-server-buffer)
8948         (dependencies gnus-newsgroup-dependencies)
8949         number headers header)
8950     (save-excursion
8951       (set-buffer nntp-server-buffer)
8952       ;; Allow the user to mangle the headers before parsing them.
8953       (run-hooks 'gnus-parse-headers-hook)
8954       ;; Allow the user to mangle the headers before parsing them.
8955       (run-hooks 'gnus-parse-headers-hook)
8956       (goto-char (point-min))
8957       (while (and sequence (not (eobp)))
8958         (setq number (read cur))
8959         (while (and sequence (< (car sequence) number))
8960           (setq sequence (cdr sequence)))
8961         (and sequence
8962              (eq number (car sequence))
8963              (progn
8964                (setq sequence (cdr sequence))
8965                (if (setq header
8966                          (inline (gnus-nov-parse-line
8967                                   number dependencies force-new)))
8968                    (setq headers (cons header headers)))))
8969         (forward-line 1))
8970       (setq headers (nreverse headers)))
8971     headers))
8972
8973 ;; This function has to be called with point after the article number
8974 ;; on the beginning of the line.
8975 (defun gnus-nov-parse-line (number dependencies &optional force-new)
8976   (let ((none 0)
8977         (eol (gnus-point-at-eol))
8978         (buffer (current-buffer))
8979         header ref id id-dep ref-dep)
8980
8981     ;; overview: [num subject from date id refs chars lines misc]
8982     (narrow-to-region (point) eol)
8983     (or (eobp) (forward-char))
8984
8985     (condition-case nil
8986         (setq header
8987               (vector
8988                number                   ; number
8989                (gnus-nov-field)         ; subject
8990                (gnus-nov-field)         ; from
8991                (gnus-nov-field)         ; date
8992                (setq id (or (gnus-nov-field)
8993                             (concat "none+"
8994                                     (int-to-string
8995                                      (setq none (1+ none)))))) ; id
8996                (progn
8997                  (save-excursion
8998                    (let ((beg (point)))
8999                      (search-forward "\t" eol)
9000                      (if (search-backward ">" beg t)
9001                          (setq ref
9002                                (downcase
9003                                 (buffer-substring
9004                                  (1+ (point))
9005                                  (progn
9006                                    (search-backward "<" beg t)
9007                                    (point)))))
9008                        (setq ref nil))))
9009                  (gnus-nov-field))      ; refs
9010                (gnus-nov-read-integer)  ; chars
9011                (gnus-nov-read-integer)  ; lines
9012                (if (= (following-char) ?\n)
9013                    nil
9014                  (gnus-nov-field))      ; misc
9015                ))
9016       (error (progn
9017                (ding)
9018                (gnus-message 4 "Strange nov line")
9019                (setq header nil)
9020                (goto-char eol))))
9021
9022     (widen)
9023
9024     ;; We build the thread tree.
9025     (when header
9026       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9027           (if (and (car (symbol-value id-dep))
9028                    (not force-new))
9029               ;; An article with this Message-ID has already been seen,
9030               ;; so we ignore this one, except we add any additional
9031               ;; Xrefs (in case the two articles came from different
9032               ;; servers.
9033               (progn
9034                 (mail-header-set-xref
9035                  (car (symbol-value id-dep))
9036                  (concat (or (mail-header-xref
9037                               (car (symbol-value id-dep))) "")
9038                          (or (mail-header-xref header) "")))
9039                 (setq header nil))
9040             (setcar (symbol-value id-dep) header))
9041         (set id-dep (list header))))
9042     (if header
9043         (progn
9044           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9045               (setcdr (symbol-value ref-dep)
9046                       (nconc (cdr (symbol-value ref-dep))
9047                              (list (symbol-value id-dep))))
9048             (set ref-dep (list nil (symbol-value id-dep))))))
9049     header))
9050
9051 (defun gnus-article-get-xrefs ()
9052   "Fill in the Xref value in `gnus-current-headers', if necessary.
9053 This is meant to be called in `gnus-article-internal-prepare-hook'."
9054   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9055                                  gnus-current-headers)))
9056     (or (not gnus-use-cross-reference)
9057         (not headers)
9058         (and (mail-header-xref headers)
9059              (not (string= (mail-header-xref headers) "")))
9060         (let ((case-fold-search t)
9061               xref)
9062           (save-restriction
9063             (nnheader-narrow-to-headers)
9064             (goto-char (point-min))
9065             (if (or (and (eq (downcase (following-char)) ?x)
9066                          (looking-at "Xref:"))
9067                     (search-forward "\nXref:" nil t))
9068                 (progn
9069                   (goto-char (1+ (match-end 0)))
9070                   (setq xref (buffer-substring (point)
9071                                                (progn (end-of-line) (point))))
9072                   (mail-header-set-xref headers xref))))))))
9073
9074 (defun gnus-summary-insert-subject (id)
9075   "Find article ID and insert the summary line for that article."
9076   (let ((header (gnus-read-header id))
9077         (number (and (numberp id) id)))
9078     (when header
9079       ;; Rebuild the thread that this article is part of and go to the
9080       ;; article we have fetched.
9081       (gnus-rebuild-thread (mail-header-id header))
9082       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9083     (when (and (numberp number)
9084                (> number 0))
9085       ;; We have to update the boundaries even if we can't fetch the
9086       ;; article if ID is a number -- so that the next `P' or `N'
9087       ;; command will fetch the previous (or next) article even
9088       ;; if the one we tried to fetch this time has been canceled.
9089       (and (> number gnus-newsgroup-end)
9090            (setq gnus-newsgroup-end number))
9091       (and (< number gnus-newsgroup-begin)
9092            (setq gnus-newsgroup-begin number))
9093       (setq gnus-newsgroup-unselected
9094             (delq number gnus-newsgroup-unselected)))
9095     ;; Report back a success?
9096     (and header number)))
9097
9098 (defun gnus-summary-work-articles (n)
9099   "Return a list of articles to be worked upon.  The prefix argument,
9100 the list of process marked articles, and the current article will be
9101 taken into consideration."
9102   (cond
9103    ((and n (numberp n))
9104     ;; A numerical prefix has been given.
9105     (let ((backward (< n 0))
9106           (n (abs n))
9107           articles article)
9108       (save-excursion
9109         (while
9110             (and (> n 0)
9111                  (push (setq article (gnus-summary-article-number))
9112                        articles)
9113                  (if backward
9114                      (gnus-summary-find-prev nil article)
9115                    (gnus-summary-find-next nil article)))
9116           (decf n)))
9117       (nreverse articles)))
9118    ((and (boundp 'transient-mark-mode)
9119          transient-mark-mode
9120          mark-active)
9121     ;; Work on the region between point and mark.
9122     (let ((max (max (point) (mark)))
9123           articles article)
9124       (save-excursion
9125         (goto-char (min (point) (mark)))
9126         (while
9127             (and
9128              (push (setq article (gnus-summary-article-number)) articles)
9129              (gnus-summary-find-next nil article)
9130              (< (point) max)))
9131         (nreverse articles))))
9132    (gnus-newsgroup-processable
9133     ;; There are process-marked articles present.
9134     (reverse gnus-newsgroup-processable))
9135    (t
9136     ;; Just return the current article.
9137     (list (gnus-summary-article-number)))))
9138
9139 (defun gnus-summary-search-group (&optional backward use-level)
9140   "Search for next unread newsgroup.
9141 If optional argument BACKWARD is non-nil, search backward instead."
9142   (save-excursion
9143     (set-buffer gnus-group-buffer)
9144     (if (gnus-group-search-forward
9145          backward nil (if use-level (gnus-group-group-level) nil))
9146         (gnus-group-group-name))))
9147
9148 (defun gnus-summary-best-group (&optional exclude-group)
9149   "Find the name of the best unread group.
9150 If EXCLUDE-GROUP, do not go to this group."
9151   (save-excursion
9152     (set-buffer gnus-group-buffer)
9153     (save-excursion
9154       (gnus-group-best-unread-group exclude-group))))
9155
9156 (defun gnus-summary-find-next (&optional unread article backward)
9157   (if backward (gnus-summary-find-prev)
9158     (let* ((article (or article (gnus-summary-article-number)))
9159            (arts (gnus-data-find-list article))
9160            result)
9161       (when (or (not gnus-summary-check-current)
9162                 (not unread)
9163                 (not (gnus-data-unread-p (car arts))))
9164         (setq arts (cdr arts)))
9165       (when (setq result
9166                   (if unread
9167                       (progn
9168                         (while arts
9169                           (when (gnus-data-unread-p (car arts))
9170                             (setq result (car arts)
9171                                   arts nil))
9172                           (setq arts (cdr arts)))
9173                         result)
9174                     (car arts)))
9175         (goto-char (gnus-data-pos result))
9176         (gnus-data-number result)))))
9177
9178 (defun gnus-summary-find-prev (&optional unread article)
9179   (let* ((article (or article (gnus-summary-article-number)))
9180          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9181          result)
9182     (when (or (not gnus-summary-check-current)
9183               (not unread)
9184               (not (gnus-data-unread-p (car arts))))
9185       (setq arts (cdr arts)))
9186     (if (setq result
9187               (if unread
9188                   (progn
9189                     (while arts
9190                       (and (gnus-data-unread-p (car arts))
9191                            (setq result (car arts)
9192                                  arts nil))
9193                       (setq arts (cdr arts)))
9194                     result)
9195                 (car arts)))
9196         (progn
9197           (goto-char (gnus-data-pos result))
9198           (gnus-data-number result)))))
9199
9200 (defun gnus-summary-find-subject (subject &optional unread backward article)
9201   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9202          (article (or article (gnus-summary-article-number)))
9203          (articles (gnus-data-list backward))
9204          (arts (gnus-data-find-list article articles))
9205          result)
9206     (when (or (not gnus-summary-check-current)
9207               (not unread)
9208               (not (gnus-data-unread-p (car arts))))
9209       (setq arts (cdr arts)))
9210     (while arts
9211       (and (or (not unread)
9212                (gnus-data-unread-p (car arts)))
9213            (vectorp (gnus-data-header (car arts)))
9214            (gnus-subject-equal
9215             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9216            (setq result (car arts)
9217                  arts nil))
9218       (setq arts (cdr arts)))
9219     (and result
9220          (goto-char (gnus-data-pos result))
9221          (gnus-data-number result))))
9222
9223 (defun gnus-summary-search-forward (&optional unread subject backward)
9224   "Search forward for an article.
9225 If UNREAD, look for unread articles.  If SUBJECT, look for
9226 articles with that subject.  If BACKWARD, search backward instead."
9227   (cond (subject (gnus-summary-find-subject subject unread backward))
9228         (backward (gnus-summary-find-prev unread))
9229         (t (gnus-summary-find-next unread))))
9230
9231 (defun gnus-recenter (&optional n)
9232   "Center point in window and redisplay frame.
9233 Also do horizontal recentering."
9234   (interactive)
9235   (gnus-horizontal-recenter)
9236   (recenter n))
9237
9238 (defun gnus-summary-recenter ()
9239   "Center point in the summary window.
9240 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9241 displayed, no centering will be performed."
9242   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9243   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9244   (let* ((top (cond ((< (window-height) 4) 0)
9245                     ((< (window-height) 7) 1)
9246                     (t 2)))
9247          (height (1- (window-height)))
9248          (bottom (save-excursion (goto-char (point-max))
9249                                  (forward-line (- height))
9250                                  (point)))
9251          (window (get-buffer-window (current-buffer))))
9252     ;; The user has to want it.
9253     (when gnus-auto-center-summary
9254       (when (get-buffer-window gnus-article-buffer)
9255        ;; Only do recentering when the article buffer is displayed,
9256        ;; Set the window start to either `bottom', which is the biggest
9257        ;; possible valid number, or the second line from the top,
9258        ;; whichever is the least.
9259        (set-window-start
9260         window (min bottom (save-excursion 
9261                              (forward-line (- top)) (point)))))
9262       ;; Do horizontal recentering while we're at it.
9263       (when (get-buffer-window (current-buffer) t)
9264         (let ((selected (selected-window)))
9265           (select-window (get-buffer-window (current-buffer) t))
9266           (gnus-summary-position-point)
9267           (gnus-horizontal-recenter)
9268           (select-window selected))))))
9269
9270 (defun gnus-horizontal-recenter ()
9271   "Recenter the current buffer horizontally."
9272   (if (< (current-column) (/ (window-width) 2))
9273       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9274     (let* ((orig (point))
9275            (end (window-end (get-buffer-window (current-buffer) t)))
9276            (max 0))
9277       ;; Find the longest line currently displayed in the window.
9278       (goto-char (window-start))
9279       (while (and (not (eobp)) 
9280                   (< (point) end))
9281         (end-of-line)
9282         (setq max (max max (current-column)))
9283         (forward-line 1))
9284       (goto-char orig)
9285       ;; Scroll horizontally to center (sort of) the point.
9286       (if (> max (window-width))
9287           (set-window-hscroll 
9288            (get-buffer-window (current-buffer) t)
9289            (min (- (current-column) (/ (window-width) 3))
9290                 (+ 2 (- max (window-width)))))
9291         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9292       max)))
9293
9294 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9295 (defun gnus-short-group-name (group &optional levels)
9296   "Collapse GROUP name LEVELS."
9297   (let* ((name "") 
9298          (foreign "")
9299          (depth 0) 
9300          (skip 1)
9301          (levels (or levels
9302                      (progn
9303                        (while (string-match "\\." group skip)
9304                          (setq skip (match-end 0)
9305                                depth (+ depth 1)))
9306                        depth))))
9307     (if (string-match ":" group)
9308         (setq foreign (substring group 0 (match-end 0))
9309               group (substring group (match-end 0))))
9310     (while group
9311       (if (and (string-match "\\." group)
9312                (> levels (- gnus-group-uncollapsed-levels 1)))
9313           (setq name (concat name (substring group 0 1))
9314                 group (substring group (match-end 0))
9315                 levels (- levels 1)
9316                 name (concat name "."))
9317         (setq name (concat foreign name group)
9318               group nil)))
9319     name))
9320
9321 (defun gnus-summary-jump-to-group (newsgroup)
9322   "Move point to NEWSGROUP in group mode buffer."
9323   ;; Keep update point of group mode buffer if visible.
9324   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9325       (save-window-excursion
9326         ;; Take care of tree window mode.
9327         (if (get-buffer-window gnus-group-buffer)
9328             (pop-to-buffer gnus-group-buffer))
9329         (gnus-group-jump-to-group newsgroup))
9330     (save-excursion
9331       ;; Take care of tree window mode.
9332       (if (get-buffer-window gnus-group-buffer)
9333           (pop-to-buffer gnus-group-buffer)
9334         (set-buffer gnus-group-buffer))
9335       (gnus-group-jump-to-group newsgroup))))
9336
9337 ;; This function returns a list of article numbers based on the
9338 ;; difference between the ranges of read articles in this group and
9339 ;; the range of active articles.
9340 (defun gnus-list-of-unread-articles (group)
9341   (let* ((read (gnus-info-read (gnus-get-info group)))
9342          (active (gnus-active group))
9343          (last (cdr active))
9344          first nlast unread)
9345     ;; If none are read, then all are unread.
9346     (if (not read)
9347         (setq first (car active))
9348       ;; If the range of read articles is a single range, then the
9349       ;; first unread article is the article after the last read
9350       ;; article.  Sounds logical, doesn't it?
9351       (if (not (listp (cdr read)))
9352           (setq first (1+ (cdr read)))
9353         ;; `read' is a list of ranges.
9354         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9355                                 (car (car read)))) 1)
9356             (setq first 1))
9357         (while read
9358           (if first
9359               (while (< first nlast)
9360                 (setq unread (cons first unread))
9361                 (setq first (1+ first))))
9362           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9363           (setq nlast (if (atom (car (cdr read)))
9364                           (car (cdr read))
9365                         (car (car (cdr read)))))
9366           (setq read (cdr read)))))
9367     ;; And add the last unread articles.
9368     (while (<= first last)
9369       (setq unread (cons first unread))
9370       (setq first (1+ first)))
9371     ;; Return the list of unread articles.
9372     (nreverse unread)))
9373
9374 (defun gnus-list-of-read-articles (group)
9375   "Return a list of unread, unticked and non-dormant articles."
9376   (let* ((info (gnus-get-info group))
9377          (marked (gnus-info-marks info))
9378          (active (gnus-active group)))
9379     (and info active
9380          (gnus-set-difference
9381           (gnus-sorted-complement
9382            (gnus-uncompress-range active)
9383            (gnus-list-of-unread-articles group))
9384           (append
9385            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9386            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9387
9388 ;; Various summary commands
9389
9390 (defun gnus-summary-universal-argument (arg)
9391   "Perform any operation on all articles that are process/prefixed."
9392   (interactive "P")
9393   (gnus-set-global-variables)
9394   (let ((articles (gnus-summary-work-articles arg))
9395         func article)
9396     (if (eq
9397          (setq
9398           func
9399           (key-binding
9400            (read-key-sequence
9401             (substitute-command-keys
9402              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9403              ))))
9404          'undefined)
9405         (progn
9406           (message "Undefined key")
9407           (ding))
9408       (save-excursion
9409         (while articles
9410           (gnus-summary-goto-subject (setq article (pop articles)))
9411           (command-execute func)
9412           (gnus-summary-remove-process-mark article)))))
9413   (gnus-summary-position-point))
9414
9415 (defun gnus-summary-toggle-truncation (&optional arg)
9416   "Toggle truncation of summary lines.
9417 With arg, turn line truncation on iff arg is positive."
9418   (interactive "P")
9419   (setq truncate-lines
9420         (if (null arg) (not truncate-lines)
9421           (> (prefix-numeric-value arg) 0)))
9422   (redraw-display))
9423
9424 (defun gnus-summary-reselect-current-group (&optional all rescan)
9425   "Exit and then reselect the current newsgroup.
9426 The prefix argument ALL means to select all articles."
9427   (interactive "P")
9428   (gnus-set-global-variables)
9429   (let ((current-subject (gnus-summary-article-number))
9430         (group gnus-newsgroup-name))
9431     (setq gnus-newsgroup-begin nil)
9432     (gnus-summary-exit)
9433     ;; We have to adjust the point of group mode buffer because the
9434     ;; current point was moved to the next unread newsgroup by
9435     ;; exiting.
9436     (gnus-summary-jump-to-group group)
9437     (when rescan
9438       (save-excursion
9439         (gnus-group-get-new-news-this-group 1)))
9440     (gnus-group-read-group all t)
9441     (gnus-summary-goto-subject current-subject)))
9442
9443 (defun gnus-summary-rescan-group (&optional all)
9444   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9445   (interactive "P")
9446   (gnus-summary-reselect-current-group all t))
9447
9448 (defun gnus-summary-update-info ()
9449   (let* ((group gnus-newsgroup-name))
9450     (when gnus-newsgroup-kill-headers
9451       (setq gnus-newsgroup-killed
9452             (gnus-compress-sequence
9453              (nconc
9454               (gnus-set-sorted-intersection
9455                (gnus-uncompress-range gnus-newsgroup-killed)
9456                (setq gnus-newsgroup-unselected
9457                      (sort gnus-newsgroup-unselected '<)))
9458               (setq gnus-newsgroup-unreads
9459                     (sort gnus-newsgroup-unreads '<))) t)))
9460     (unless (listp (cdr gnus-newsgroup-killed))
9461       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9462     (let ((headers gnus-newsgroup-headers))
9463       (gnus-close-group group)
9464       (run-hooks 'gnus-exit-group-hook)
9465       (unless gnus-save-score
9466         (setq gnus-newsgroup-scored nil))
9467       ;; Set the new ranges of read articles.
9468       (gnus-update-read-articles
9469        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9470       ;; Set the current article marks.
9471       (gnus-update-marks)
9472       ;; Do the cross-ref thing.
9473       (when gnus-use-cross-reference
9474         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9475       ;; Do adaptive scoring, and possibly save score files.
9476       (when gnus-newsgroup-adaptive
9477         (gnus-score-adaptive))
9478       (when gnus-use-scoring
9479         (gnus-score-save))
9480       ;; Do not switch windows but change the buffer to work.
9481       (set-buffer gnus-group-buffer)
9482       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9483           (gnus-group-update-group group)))))
9484
9485 (defun gnus-summary-exit (&optional temporary)
9486   "Exit reading current newsgroup, and then return to group selection mode.
9487 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9488   (interactive)
9489   (gnus-set-global-variables)
9490   (gnus-kill-save-kill-buffer)
9491   (let* ((group gnus-newsgroup-name)
9492          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9493          (mode major-mode)
9494          (buf (current-buffer)))
9495     (run-hooks 'gnus-summary-prepare-exit-hook)
9496     ;; If we have several article buffers, we kill them at exit.
9497     (unless gnus-single-article-buffer
9498       (gnus-kill-buffer gnus-article-buffer)
9499       (gnus-kill-buffer gnus-original-article-buffer))
9500     (when gnus-use-cache
9501       (gnus-cache-possibly-remove-articles)
9502       (gnus-cache-save-buffers))
9503     (when gnus-use-trees
9504       (gnus-tree-close group))
9505     ;; Make all changes in this group permanent.
9506     (gnus-summary-update-info)
9507     ;; Make sure where I was, and go to next newsgroup.
9508     (set-buffer gnus-group-buffer)
9509     (unless quit-config
9510       (gnus-group-jump-to-group group)
9511       (gnus-group-next-unread-group 1))
9512     (run-hooks 'gnus-summary-exit-hook)
9513     (if temporary
9514         nil                             ;Nothing to do.
9515       ;; If we have several article buffers, we kill them at exit.
9516       (unless gnus-single-article-buffer
9517         (gnus-kill-buffer gnus-article-buffer)
9518         (gnus-kill-buffer gnus-original-article-buffer))
9519       (set-buffer buf)
9520       (if (not gnus-kill-summary-on-exit)
9521           (gnus-deaden-summary)
9522         ;; We set all buffer-local variables to nil.  It is unclear why
9523         ;; this is needed, but if we don't, buffer-local variables are
9524         ;; not garbage-collected, it seems.  This would the lead to en
9525         ;; ever-growing Emacs.
9526         (gnus-summary-clear-local-variables)
9527         (when (get-buffer gnus-article-buffer)
9528           (bury-buffer gnus-article-buffer))
9529         ;; We clear the global counterparts of the buffer-local
9530         ;; variables as well, just to be on the safe side.
9531         (gnus-configure-windows 'group 'force)
9532         (gnus-summary-clear-local-variables)
9533         ;; Return to group mode buffer.
9534         (if (eq mode 'gnus-summary-mode)
9535             (gnus-kill-buffer buf)))
9536       (setq gnus-current-select-method gnus-select-method)
9537       (pop-to-buffer gnus-group-buffer)
9538       ;; Clear the current group name.
9539       (if (not quit-config)
9540           (progn
9541             (gnus-group-jump-to-group group)
9542             (gnus-group-next-unread-group 1)
9543             (gnus-configure-windows 'group 'force))
9544         (if (not (buffer-name (car quit-config)))
9545             (gnus-configure-windows 'group 'force)
9546           (set-buffer (car quit-config))
9547           (and (eq major-mode 'gnus-summary-mode)
9548                (gnus-set-global-variables))
9549           (gnus-configure-windows (cdr quit-config))))
9550       (unless quit-config
9551         (setq gnus-newsgroup-name nil)))))
9552
9553 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9554 (defun gnus-summary-exit-no-update (&optional no-questions)
9555   "Quit reading current newsgroup without updating read article info."
9556   (interactive)
9557   (gnus-set-global-variables)
9558   (let* ((group gnus-newsgroup-name)
9559          (quit-config (gnus-group-quit-config group)))
9560     (when (or no-questions
9561               gnus-expert-user
9562               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9563       ;; If we have several article buffers, we kill them at exit.
9564       (unless gnus-single-article-buffer
9565         (gnus-kill-buffer gnus-article-buffer)
9566         (gnus-kill-buffer gnus-original-article-buffer))
9567       (if (not gnus-kill-summary-on-exit)
9568           (gnus-deaden-summary)
9569         (gnus-close-group group)
9570         (gnus-summary-clear-local-variables)
9571         (set-buffer gnus-group-buffer)
9572         (gnus-summary-clear-local-variables)
9573         (when (get-buffer gnus-summary-buffer)
9574           (kill-buffer gnus-summary-buffer)))
9575       (when gnus-use-trees
9576         (gnus-tree-close group))
9577       (when (get-buffer gnus-article-buffer)
9578         (bury-buffer gnus-article-buffer))
9579       ;; Return to the group buffer.
9580       (gnus-configure-windows 'group 'force)
9581       ;; Clear the current group name.
9582       (setq gnus-newsgroup-name nil)
9583       (when (equal (gnus-group-group-name) group)
9584         (gnus-group-next-unread-group 1))
9585       (when quit-config
9586         (if (not (buffer-name (car quit-config)))
9587             (gnus-configure-windows 'group 'force)
9588           (set-buffer (car quit-config))
9589           (when (eq major-mode 'gnus-summary-mode)
9590             (gnus-set-global-variables))
9591           (gnus-configure-windows (cdr quit-config)))))))
9592
9593 ;;; Dead summaries.
9594
9595 (defvar gnus-dead-summary-mode-map nil)
9596
9597 (if gnus-dead-summary-mode-map
9598     nil
9599   (setq gnus-dead-summary-mode-map (make-keymap))
9600   (suppress-keymap gnus-dead-summary-mode-map)
9601   (substitute-key-definition
9602    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9603   (let ((keys '("\C-d" "\r" "\177")))
9604     (while keys
9605       (define-key gnus-dead-summary-mode-map
9606         (pop keys) 'gnus-summary-wake-up-the-dead))))
9607
9608 (defvar gnus-dead-summary-mode nil
9609   "Minor mode for Gnus summary buffers.")
9610
9611 (defun gnus-dead-summary-mode (&optional arg)
9612   "Minor mode for Gnus summary buffers."
9613   (interactive "P")
9614   (when (eq major-mode 'gnus-summary-mode)
9615     (make-local-variable 'gnus-dead-summary-mode)
9616     (setq gnus-dead-summary-mode
9617           (if (null arg) (not gnus-dead-summary-mode)
9618             (> (prefix-numeric-value arg) 0)))
9619     (when gnus-dead-summary-mode
9620       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9621         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9622       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9623         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9624               minor-mode-map-alist)))))
9625
9626 (defun gnus-deaden-summary ()
9627   "Make the current summary buffer into a dead summary buffer."
9628   ;; Kill any previous dead summary buffer.
9629   (when (and gnus-dead-summary
9630              (buffer-name gnus-dead-summary))
9631     (save-excursion
9632       (set-buffer gnus-dead-summary)
9633       (when gnus-dead-summary-mode
9634         (kill-buffer (current-buffer)))))
9635   ;; Make this the current dead summary.
9636   (setq gnus-dead-summary (current-buffer))
9637   (gnus-dead-summary-mode 1)
9638   (let ((name (buffer-name)))
9639     (when (string-match "Summary" name)
9640       (rename-buffer
9641        (concat (substring name 0 (match-beginning 0)) "Dead "
9642                (substring name (match-beginning 0))) t))))
9643
9644 (defun gnus-kill-or-deaden-summary (buffer)
9645   "Kill or deaden the summary BUFFER."
9646   (cond (gnus-kill-summary-on-exit
9647          (when (and gnus-use-trees
9648                     (and (get-buffer buffer)
9649                          (buffer-name (get-buffer buffer))))
9650            (save-excursion
9651              (set-buffer (get-buffer buffer))
9652              (gnus-tree-close gnus-newsgroup-name)))
9653          (gnus-kill-buffer buffer))
9654         ((and (get-buffer buffer)
9655               (buffer-name (get-buffer buffer)))
9656          (save-excursion
9657            (set-buffer buffer)
9658            (gnus-deaden-summary)))))
9659
9660 (defun gnus-summary-wake-up-the-dead (&rest args)
9661   "Wake up the dead summary buffer."
9662   (interactive)
9663   (gnus-dead-summary-mode -1)
9664   (let ((name (buffer-name)))
9665     (when (string-match "Dead " name)
9666       (rename-buffer
9667        (concat (substring name 0 (match-beginning 0))
9668                (substring name (match-end 0))) t)))
9669   (gnus-message 3 "This dead summary is now alive again"))
9670
9671 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9672 (defun gnus-summary-fetch-faq (&optional faq-dir)
9673   "Fetch the FAQ for the current group.
9674 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9675 in."
9676   (interactive
9677    (list
9678     (if current-prefix-arg
9679         (completing-read
9680          "Faq dir: " (and (listp gnus-group-faq-directory)
9681                           gnus-group-faq-directory)))))
9682   (let (gnus-faq-buffer)
9683     (and (setq gnus-faq-buffer
9684                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9685          (gnus-configure-windows 'summary-faq))))
9686
9687 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9688 (defun gnus-summary-describe-group (&optional force)
9689   "Describe the current newsgroup."
9690   (interactive "P")
9691   (gnus-group-describe-group force gnus-newsgroup-name))
9692
9693 (defun gnus-summary-describe-briefly ()
9694   "Describe summary mode commands briefly."
9695   (interactive)
9696   (gnus-message 6
9697                 (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")))
9698
9699 ;; Walking around group mode buffer from summary mode.
9700
9701 (defun gnus-summary-next-group (&optional no-article target-group backward)
9702   "Exit current newsgroup and then select next unread newsgroup.
9703 If prefix argument NO-ARTICLE is non-nil, no article is selected
9704 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9705 previous group instead."
9706   (interactive "P")
9707   (gnus-set-global-variables)
9708   (let ((current-group gnus-newsgroup-name)
9709         (current-buffer (current-buffer))
9710         entered)
9711     ;; First we semi-exit this group to update Xrefs and all variables.
9712     ;; We can't do a real exit, because the window conf must remain
9713     ;; the same in case the user is prompted for info, and we don't
9714     ;; want the window conf to change before that...
9715     (gnus-summary-exit t)
9716     (while (not entered)
9717       ;; Then we find what group we are supposed to enter.
9718       (set-buffer gnus-group-buffer)
9719       (gnus-group-jump-to-group current-group)
9720       (setq target-group
9721             (or target-group
9722                 (if (eq gnus-keep-same-level 'best)
9723                     (gnus-summary-best-group gnus-newsgroup-name)
9724                   (gnus-summary-search-group backward gnus-keep-same-level))))
9725       (if (not target-group)
9726           ;; There are no further groups, so we return to the group
9727           ;; buffer.
9728           (progn
9729             (gnus-message 5 "Returning to the group buffer")
9730             (setq entered t)
9731             (set-buffer current-buffer)
9732             (gnus-summary-exit))
9733         ;; We try to enter the target group.
9734         (gnus-group-jump-to-group target-group)
9735         (let ((unreads (gnus-group-group-unread)))
9736           (if (and (or (eq t unreads)
9737                        (and unreads (not (zerop unreads))))
9738                    (gnus-summary-read-group
9739                     target-group nil no-article current-buffer))
9740               (setq entered t)
9741             (setq current-group target-group
9742                   target-group nil)))))))
9743
9744 (defun gnus-summary-prev-group (&optional no-article)
9745   "Exit current newsgroup and then select previous unread newsgroup.
9746 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9747   (interactive "P")
9748   (gnus-summary-next-group no-article nil t))
9749
9750 ;; Walking around summary lines.
9751
9752 (defun gnus-summary-first-subject (&optional unread)
9753   "Go to the first unread subject.
9754 If UNREAD is non-nil, go to the first unread article.
9755 Returns the article selected or nil if there are no unread articles."
9756   (interactive "P")
9757   (prog1
9758       (cond
9759        ;; Empty summary.
9760        ((null gnus-newsgroup-data)
9761         (gnus-message 3 "No articles in the group")
9762         nil)
9763        ;; Pick the first article.
9764        ((not unread)
9765         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9766         (gnus-data-number (car gnus-newsgroup-data)))
9767        ;; No unread articles.
9768        ((null gnus-newsgroup-unreads)
9769         (gnus-message 3 "No more unread articles")
9770         nil)
9771        ;; Find the first unread article.
9772        (t
9773         (let ((data gnus-newsgroup-data))
9774           (while (and data
9775                       (not (gnus-data-unread-p (car data))))
9776             (setq data (cdr data)))
9777           (if data
9778               (progn
9779                 (goto-char (gnus-data-pos (car data)))
9780                 (gnus-data-number (car data)))))))
9781     (gnus-summary-position-point)))
9782
9783 (defun gnus-summary-next-subject (n &optional unread dont-display)
9784   "Go to next N'th summary line.
9785 If N is negative, go to the previous N'th subject line.
9786 If UNREAD is non-nil, only unread articles are selected.
9787 The difference between N and the actual number of steps taken is
9788 returned."
9789   (interactive "p")
9790   (let ((backward (< n 0))
9791         (n (abs n)))
9792     (while (and (> n 0)
9793                 (if backward
9794                     (gnus-summary-find-prev unread)
9795                   (gnus-summary-find-next unread)))
9796       (setq n (1- n)))
9797     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9798                                (if unread " unread" "")))
9799     (or dont-display
9800         (progn
9801           (gnus-summary-recenter)
9802           (gnus-summary-position-point)))
9803     n))
9804
9805 (defun gnus-summary-next-unread-subject (n)
9806   "Go to next N'th unread summary line."
9807   (interactive "p")
9808   (gnus-summary-next-subject n t))
9809
9810 (defun gnus-summary-prev-subject (n &optional unread)
9811   "Go to previous N'th summary line.
9812 If optional argument UNREAD is non-nil, only unread article is selected."
9813   (interactive "p")
9814   (gnus-summary-next-subject (- n) unread))
9815
9816 (defun gnus-summary-prev-unread-subject (n)
9817   "Go to previous N'th unread summary line."
9818   (interactive "p")
9819   (gnus-summary-next-subject (- n) t))
9820
9821 (defun gnus-summary-goto-subject (article &optional force silent)
9822   "Go the subject line of ARTICLE.
9823 If FORCE, also allow jumping to articles not currently shown."
9824   (let ((b (point))
9825         (data (gnus-data-find article)))
9826     ;; We read in the article if we have to.
9827     (and (not data)
9828          force
9829          (gnus-summary-insert-subject article)
9830          (setq data (gnus-data-find article)))
9831     (goto-char b)
9832     (if (not data)
9833         (progn
9834           (unless silent
9835             (gnus-message 3 "Can't find article %d" article))
9836           nil)
9837       (goto-char (gnus-data-pos data))
9838       article)))
9839
9840 ;; Walking around summary lines with displaying articles.
9841
9842 (defun gnus-summary-expand-window (&optional arg)
9843   "Make the summary buffer take up the entire Emacs frame.
9844 Given a prefix, will force an `article' buffer configuration."
9845   (interactive "P")
9846   (gnus-set-global-variables)
9847   (if arg
9848       (gnus-configure-windows 'article 'force)
9849     (gnus-configure-windows 'summary 'force)))
9850
9851 (defun gnus-summary-display-article (article &optional all-header)
9852   "Display ARTICLE in article buffer."
9853   (gnus-set-global-variables)
9854   (if (null article)
9855       nil
9856     (prog1
9857         (if gnus-summary-display-article-function
9858             (funcall gnus-summary-display-article-function article all-header)
9859           (gnus-article-prepare article all-header))
9860       (run-hooks 'gnus-select-article-hook)
9861       (gnus-summary-recenter)
9862       (gnus-summary-goto-subject article)
9863       (when gnus-use-trees
9864         (gnus-possibly-generate-tree article)
9865         (gnus-highlight-selected-tree article))
9866       ;; Successfully display article.
9867       (gnus-article-set-window-start
9868        (cdr (assq article gnus-newsgroup-bookmarks)))
9869       t)))
9870
9871 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9872   "Select the current article.
9873 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9874 non-nil, the article will be re-fetched even if it already present in
9875 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9876 be displayed."
9877   (let ((article (or article (gnus-summary-article-number)))
9878         (all-headers (not (not all-headers))) ;Must be T or NIL.
9879         gnus-summary-display-article-function
9880         did)
9881     (and (not pseudo)
9882          (gnus-summary-article-pseudo-p article)
9883          (error "This is a pseudo-article."))
9884     (prog1
9885         (save-excursion
9886           (set-buffer gnus-summary-buffer)
9887           (if (or (and gnus-single-article-buffer
9888                        (or (null gnus-current-article)
9889                            (null gnus-article-current)
9890                            (null (get-buffer gnus-article-buffer))
9891                            (not (eq article (cdr gnus-article-current)))
9892                            (not (equal (car gnus-article-current)
9893                                        gnus-newsgroup-name))))
9894                   (and (not gnus-single-article-buffer)
9895                        (null gnus-current-article))
9896                   force)
9897               ;; The requested article is different from the current article.
9898               (prog1
9899                   (gnus-summary-display-article article all-headers)
9900                 (setq did article))
9901             (if (or all-headers gnus-show-all-headers)
9902                 (gnus-article-show-all-headers))
9903             'old))
9904       (if did
9905           (gnus-article-set-window-start
9906            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9907
9908 (defun gnus-summary-set-current-mark (&optional current-mark)
9909   "Obsolete function."
9910   nil)
9911
9912 (defun gnus-summary-next-article (&optional unread subject backward push)
9913   "Select the next article.
9914 If UNREAD, only unread articles are selected.
9915 If SUBJECT, only articles with SUBJECT are selected.
9916 If BACKWARD, the previous article is selected instead of the next."
9917   (interactive "P")
9918   (gnus-set-global-variables)
9919   (let (header)
9920     (cond
9921      ;; Is there such an article?
9922      ((and (gnus-summary-search-forward unread subject backward)
9923            (or (gnus-summary-display-article (gnus-summary-article-number))
9924                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9925       (gnus-summary-position-point))
9926      ;; If not, we try the first unread, if that is wanted.
9927      ((and subject
9928            gnus-auto-select-same
9929            (or (gnus-summary-first-unread-article)
9930                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9931       (gnus-summary-position-point)
9932       (gnus-message 6 "Wrapped"))
9933      ;; Try to get next/previous article not displayed in this group.
9934      ((and gnus-auto-extend-newsgroup
9935            (not unread) (not subject))
9936       (gnus-summary-goto-article
9937        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
9938        nil t))
9939      ;; Go to next/previous group.
9940      (t
9941       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9942           (gnus-summary-jump-to-group gnus-newsgroup-name))
9943       (let ((cmd last-command-char)
9944             (group
9945              (if (eq gnus-keep-same-level 'best)
9946                  (gnus-summary-best-group gnus-newsgroup-name)
9947                (gnus-summary-search-group backward gnus-keep-same-level))))
9948         ;; For some reason, the group window gets selected.  We change
9949         ;; it back.
9950         (select-window (get-buffer-window (current-buffer)))
9951         ;; Select next unread newsgroup automagically.
9952         (cond
9953          ((not gnus-auto-select-next)
9954           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
9955          ((or (eq gnus-auto-select-next 'quietly)
9956               (and (eq gnus-auto-select-next 'slightly-quietly)
9957                    push)
9958               (and (eq gnus-auto-select-next 'almost-quietly)
9959                    (gnus-summary-last-article-p)))
9960           ;; Select quietly.
9961           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
9962               (gnus-summary-exit)
9963             (gnus-message 7 "No more%s articles (%s)..."
9964                           (if unread " unread" "")
9965                           (if group (concat "selecting " group)
9966                             "exiting"))
9967             (gnus-summary-next-group nil group backward)))
9968          (t
9969           (gnus-summary-walk-group-buffer
9970            gnus-newsgroup-name cmd unread backward))))))))
9971
9972 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
9973   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
9974                       (?\C-p (gnus-group-prev-unread-group 1))))
9975         keve key group ended)
9976     (save-excursion
9977       (set-buffer gnus-group-buffer)
9978       (gnus-summary-jump-to-group from-group)
9979       (setq group
9980             (if (eq gnus-keep-same-level 'best)
9981                 (gnus-summary-best-group gnus-newsgroup-name)
9982               (gnus-summary-search-group backward gnus-keep-same-level))))
9983     (while (not ended)
9984       (gnus-message
9985        5 "No more%s articles%s" (if unread " unread" "")
9986        (if (and group
9987                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
9988            (format " (Type %s for %s [%s])"
9989                    (single-key-description cmd) group
9990                    (car (gnus-gethash group gnus-newsrc-hashtb)))
9991          (format " (Type %s to exit %s)"
9992                  (single-key-description cmd)
9993                  gnus-newsgroup-name)))
9994       ;; Confirm auto selection.
9995       (setq key (car (setq keve (gnus-read-event-char))))
9996       (setq ended t)
9997       (cond
9998        ((assq key keystrokes)
9999         (let ((obuf (current-buffer)))
10000           (switch-to-buffer gnus-group-buffer)
10001           (and group
10002                (gnus-group-jump-to-group group))
10003           (eval (car (cdr (assq key keystrokes))))
10004           (setq group (gnus-group-group-name))
10005           (switch-to-buffer obuf))
10006         (setq ended nil))
10007        ((equal key cmd)
10008         (if (or (not group)
10009                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10010             (gnus-summary-exit)
10011           (gnus-summary-next-group nil group backward)))
10012        (t
10013         (push (cdr keve) unread-command-events))))))
10014
10015 (defun gnus-read-event-char ()
10016   "Get the next event."
10017   (let ((event (read-event)))
10018     (cons (and (numberp event) event) event)))
10019
10020 (defun gnus-summary-next-unread-article ()
10021   "Select unread article after current one."
10022   (interactive)
10023   (gnus-summary-next-article t (and gnus-auto-select-same
10024                                     (gnus-summary-article-subject))))
10025
10026 (defun gnus-summary-prev-article (&optional unread subject)
10027   "Select the article after the current one.
10028 If UNREAD is non-nil, only unread articles are selected."
10029   (interactive "P")
10030   (gnus-summary-next-article unread subject t))
10031
10032 (defun gnus-summary-prev-unread-article ()
10033   "Select unred article before current one."
10034   (interactive)
10035   (gnus-summary-prev-article t (and gnus-auto-select-same
10036                                     (gnus-summary-article-subject))))
10037
10038 (defun gnus-summary-next-page (&optional lines circular)
10039   "Show next page of the selected article.
10040 If at the end of the current article, select the next article.
10041 LINES says how many lines should be scrolled up.
10042
10043 If CIRCULAR is non-nil, go to the start of the article instead of
10044 selecting the next article when reaching the end of the current
10045 article."
10046   (interactive "P")
10047   (setq gnus-summary-buffer (current-buffer))
10048   (gnus-set-global-variables)
10049   (let ((article (gnus-summary-article-number))
10050         (endp nil))
10051     (gnus-configure-windows 'article)
10052     (if (or (null gnus-current-article)
10053             (null gnus-article-current)
10054             (/= article (cdr gnus-article-current))
10055             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10056         ;; Selected subject is different from current article's.
10057         (gnus-summary-display-article article)
10058       (gnus-eval-in-buffer-window
10059        gnus-article-buffer
10060        (setq endp (gnus-article-next-page lines)))
10061       (if endp
10062           (cond (circular
10063                  (gnus-summary-beginning-of-article))
10064                 (lines
10065                  (gnus-message 3 "End of message"))
10066                 ((null lines)
10067                  (if (and (eq gnus-summary-goto-unread 'never)
10068                           (not (eq article gnus-newsgroup-end)))
10069                      (gnus-summary-next-article)
10070                    (gnus-summary-next-unread-article))))))
10071     (gnus-summary-recenter)
10072     (gnus-summary-position-point)))
10073
10074 (defun gnus-summary-prev-page (&optional lines)
10075   "Show previous page of selected article.
10076 Argument LINES specifies lines to be scrolled down."
10077   (interactive "P")
10078   (gnus-set-global-variables)
10079   (let ((article (gnus-summary-article-number)))
10080     (gnus-configure-windows 'article)
10081     (if (or (null gnus-current-article)
10082             (null gnus-article-current)
10083             (/= article (cdr gnus-article-current))
10084             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10085         ;; Selected subject is different from current article's.
10086         (gnus-summary-display-article article)
10087       (gnus-summary-recenter)
10088       (gnus-eval-in-buffer-window gnus-article-buffer
10089                                   (gnus-article-prev-page lines))))
10090   (gnus-summary-position-point))
10091
10092 (defun gnus-summary-scroll-up (lines)
10093   "Scroll up (or down) one line current article.
10094 Argument LINES specifies lines to be scrolled up (or down if negative)."
10095   (interactive "p")
10096   (gnus-set-global-variables)
10097   (gnus-configure-windows 'article)
10098   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10099     (gnus-eval-in-buffer-window
10100      gnus-article-buffer
10101      (cond ((> lines 0)
10102             (if (gnus-article-next-page lines)
10103                 (gnus-message 3 "End of message")))
10104            ((< lines 0)
10105             (gnus-article-prev-page (- lines))))))
10106   (gnus-summary-recenter)
10107   (gnus-summary-position-point))
10108
10109 (defun gnus-summary-next-same-subject ()
10110   "Select next article which has the same subject as current one."
10111   (interactive)
10112   (gnus-set-global-variables)
10113   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10114
10115 (defun gnus-summary-prev-same-subject ()
10116   "Select previous article which has the same subject as current one."
10117   (interactive)
10118   (gnus-set-global-variables)
10119   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10120
10121 (defun gnus-summary-next-unread-same-subject ()
10122   "Select next unread article which has the same subject as current one."
10123   (interactive)
10124   (gnus-set-global-variables)
10125   (gnus-summary-next-article t (gnus-summary-article-subject)))
10126
10127 (defun gnus-summary-prev-unread-same-subject ()
10128   "Select previous unread article which has the same subject as current one."
10129   (interactive)
10130   (gnus-set-global-variables)
10131   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10132
10133 (defun gnus-summary-first-unread-article ()
10134   "Select the first unread article.
10135 Return nil if there are no unread articles."
10136   (interactive)
10137   (gnus-set-global-variables)
10138   (prog1
10139       (if (gnus-summary-first-subject t)
10140           (progn
10141             (gnus-summary-show-thread)
10142             (gnus-summary-first-subject t)
10143             (gnus-summary-display-article (gnus-summary-article-number))))
10144     (gnus-summary-position-point)))
10145
10146 (defun gnus-summary-best-unread-article ()
10147   "Select the unread article with the highest score."
10148   (interactive)
10149   (gnus-set-global-variables)
10150   (let ((best -1000000)
10151         (data gnus-newsgroup-data)
10152         article score)
10153     (while data
10154       (and (gnus-data-unread-p (car data))
10155            (> (setq score
10156                     (gnus-summary-article-score (gnus-data-number (car data))))
10157               best)
10158            (setq best score
10159                  article (gnus-data-number (car data))))
10160       (setq data (cdr data)))
10161     (if article
10162         (gnus-summary-goto-article article)
10163       (error "No unread articles"))
10164     (gnus-summary-position-point)))
10165
10166 (defun gnus-summary-last-subject ()
10167   "Go to the last displayed subject line in the group."
10168   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10169     (when article
10170       (gnus-summary-goto-subject article))))
10171
10172 (defun gnus-summary-goto-article (article &optional all-headers force)
10173   "Fetch ARTICLE and display it if it exists.
10174 If ALL-HEADERS is non-nil, no header lines are hidden."
10175   (interactive
10176    (list
10177     (string-to-int
10178      (completing-read
10179       "Article number: "
10180       (mapcar (lambda (number) (list (int-to-string number)))
10181               gnus-newsgroup-limit)))
10182     current-prefix-arg
10183     t))
10184   (prog1
10185       (if (gnus-summary-goto-subject article force)
10186           (gnus-summary-display-article article all-headers)
10187         (gnus-message 4 "Couldn't go to article %s" article) nil)
10188     (gnus-summary-position-point)))
10189
10190 (defun gnus-summary-goto-last-article ()
10191   "Go to the previously read article."
10192   (interactive)
10193   (prog1
10194       (and gnus-last-article
10195            (gnus-summary-goto-article gnus-last-article))
10196     (gnus-summary-position-point)))
10197
10198 (defun gnus-summary-pop-article (number)
10199   "Pop one article off the history and go to the previous.
10200 NUMBER articles will be popped off."
10201   (interactive "p")
10202   (let (to)
10203     (setq gnus-newsgroup-history
10204           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10205     (if to
10206         (gnus-summary-goto-article (car to))
10207       (error "Article history empty")))
10208   (gnus-summary-position-point))
10209
10210 ;; Summary commands and functions for limiting the summary buffer.
10211
10212 (defun gnus-summary-limit-to-articles (n)
10213   "Limit the summary buffer to the next N articles.
10214 If not given a prefix, use the process marked articles instead."
10215   (interactive "P")
10216   (gnus-set-global-variables)
10217   (prog1
10218       (let ((articles (gnus-summary-work-articles n)))
10219         (setq gnus-newsgroup-processable nil)
10220         (gnus-summary-limit articles))
10221     (gnus-summary-position-point)))
10222
10223 (defun gnus-summary-pop-limit (&optional total)
10224   "Restore the previous limit.
10225 If given a prefix, remove all limits."
10226   (interactive "P")
10227   (gnus-set-global-variables)
10228   (when total 
10229     (setq gnus-newsgroup-limits
10230           (list (mapcar (lambda (h) (mail-header-number h))
10231                         gnus-newsgroup-headers))))
10232   (unless gnus-newsgroup-limits
10233     (error "No limit to pop"))
10234   (prog1
10235       (gnus-summary-limit nil 'pop)
10236     (gnus-summary-position-point)))
10237
10238 (defun gnus-summary-limit-to-subject (subject &optional header)
10239   "Limit the summary buffer to articles that have subjects that match a regexp."
10240   (interactive "sRegexp: ")
10241   (unless header
10242     (setq header "subject"))
10243   (when (not (equal "" subject))
10244     (prog1
10245         (let ((articles (gnus-summary-find-matching
10246                          (or header "subject") subject 'all)))
10247           (or articles (error "Found no matches for \"%s\"" subject))
10248           (gnus-summary-limit articles))
10249       (gnus-summary-position-point))))
10250
10251 (defun gnus-summary-limit-to-author (from)
10252   "Limit the summary buffer to articles that have authors that match a regexp."
10253   (interactive "sRegexp: ")
10254   (gnus-summary-limit-to-subject from "from"))
10255
10256 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10257 (make-obsolete
10258  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10259
10260 (defun gnus-summary-limit-to-unread (&optional all)
10261   "Limit the summary buffer to articles that are not marked as read.
10262 If ALL is non-nil, limit strictly to unread articles."
10263   (interactive "P")
10264   (if all
10265       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10266     (gnus-summary-limit-to-marks
10267      ;; Concat all the marks that say that an article is read and have
10268      ;; those removed.
10269      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10270            gnus-killed-mark gnus-kill-file-mark
10271            gnus-low-score-mark gnus-expirable-mark
10272            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10273      'reverse)))
10274
10275 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10276 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10277
10278 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10279   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10280 If REVERSE, limit the summary buffer to articles that are not marked
10281 with MARKS.  MARKS can either be a string of marks or a list of marks.
10282 Returns how many articles were removed."
10283   (interactive "sMarks: ")
10284   (gnus-set-global-variables)
10285   (prog1
10286       (let ((data gnus-newsgroup-data)
10287             (marks (if (listp marks) marks
10288                      (append marks nil))) ; Transform to list.
10289             articles)
10290         (while data
10291           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10292                  (memq (gnus-data-mark (car data)) marks))
10293                (setq articles (cons (gnus-data-number (car data)) articles)))
10294           (setq data (cdr data)))
10295         (gnus-summary-limit articles))
10296     (gnus-summary-position-point)))
10297
10298 (defun gnus-summary-limit-to-score (&optional score)
10299   "Limit to articles with score at or above SCORE."
10300   (interactive "P")
10301   (gnus-set-global-variables)
10302   (setq score (if score
10303                   (prefix-numeric-value score)
10304                 (or gnus-summary-default-score 0)))
10305   (let ((data gnus-newsgroup-data)
10306         articles)
10307     (while data
10308       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10309                 score)
10310         (push (gnus-data-number (car data)) articles))
10311       (setq data (cdr data)))
10312     (prog1
10313         (gnus-summary-limit articles)
10314       (gnus-summary-position-point))))
10315
10316 (defun gnus-summary-limit-include-dormant ()
10317   "Display all the hidden articles that are marked as dormant."
10318   (interactive)
10319   (gnus-set-global-variables)
10320   (or gnus-newsgroup-dormant
10321       (error "There are no dormant articles in this group"))
10322   (prog1
10323       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-limit-exclude-dormant ()
10327   "Hide all dormant articles."
10328   (interactive)
10329   (gnus-set-global-variables)
10330   (prog1
10331       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10332     (gnus-summary-position-point)))
10333
10334 (defun gnus-summary-limit-exclude-childless-dormant ()
10335   "Hide all dormant articles that have no children."
10336   (interactive)
10337   (gnus-set-global-variables)
10338   (let ((data gnus-newsgroup-data)
10339         articles)
10340     ;; Find all articles that are either not dormant or have
10341     ;; children.
10342     (while data
10343       (and (or (not (= (gnus-data-mark (car data)) gnus-dormant-mark))
10344                (gnus-article-parent-p (gnus-data-number (car data))))
10345            (setq articles (cons (gnus-data-number (car data))
10346                                 articles)))
10347       (setq data (cdr data)))
10348     ;; Do the limiting.
10349     (prog1
10350         (gnus-summary-limit articles)
10351       (gnus-summary-position-point))))
10352
10353 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10354   "Mark all unread excluded articles as read.
10355 If ALL, mark even excluded ticked and dormants as read."
10356   (interactive "P")
10357   (let ((articles (gnus-sorted-complement
10358                    (sort
10359                     (mapcar (lambda (h) (mail-header-number h))
10360                             gnus-newsgroup-headers)
10361                     '<)
10362                    (sort gnus-newsgroup-limit '<)))
10363         article)
10364     (setq gnus-newsgroup-unreads nil)
10365     (if all
10366         (setq gnus-newsgroup-dormant nil
10367               gnus-newsgroup-marked nil
10368               gnus-newsgroup-reads
10369               (nconc
10370                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10371                gnus-newsgroup-reads))
10372       (while (setq article (pop articles))
10373         (unless (or (memq article gnus-newsgroup-dormant)
10374                     (memq article gnus-newsgroup-marked))
10375           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10376
10377 (defun gnus-summary-limit (articles &optional pop)
10378   (if pop
10379       ;; We pop the previous limit off the stack and use that.
10380       (setq articles (car gnus-newsgroup-limits)
10381             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10382     ;; We use the new limit, so we push the old limit on the stack.
10383     (setq gnus-newsgroup-limits
10384           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10385   ;; Set the limit.
10386   (setq gnus-newsgroup-limit articles)
10387   (let ((total (length gnus-newsgroup-data))
10388         (data (gnus-data-find-list (gnus-summary-article-number)))
10389         found)
10390     ;; This will do all the work of generating the new summary buffer
10391     ;; according to the new limit.
10392     (gnus-summary-prepare)
10393     ;; Hide any threads, possibly.
10394     (and gnus-show-threads
10395          gnus-thread-hide-subtree
10396          (gnus-summary-hide-all-threads))
10397     ;; Try to return to the article you were at, or one in the
10398     ;; neighborhood.
10399     (if data
10400         ;; We try to find some article after the current one.
10401         (while data
10402           (and (gnus-summary-goto-subject
10403                 (gnus-data-number (car data)) nil t)
10404                (setq data nil
10405                      found t))
10406           (setq data (cdr data))))
10407     (or found
10408         ;; If there is no data, that means that we were after the last
10409         ;; article.  The same goes when we can't find any articles
10410         ;; after the current one.
10411         (progn
10412           (goto-char (point-max))
10413           (gnus-summary-find-prev)))
10414     ;; We return how many articles were removed from the summary
10415     ;; buffer as a result of the new limit.
10416     (- total (length gnus-newsgroup-data))))
10417
10418 (defsubst gnus-cut-thread (thread)
10419   "Go forwards in the thread until we find an article that we want to display."
10420   (if (eq gnus-fetch-old-headers 'some)
10421       (while (and thread
10422                   (memq (mail-header-number (car thread)) 
10423                         gnus-newsgroup-ancient)
10424                   (<= (length (cdr thread)) 1))
10425         (setq thread (cadr thread)))
10426     (while (and thread
10427                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10428                 (= (length (cdr thread)) 1))
10429       (setq thread (cadr thread))))
10430   thread)
10431
10432 (defun gnus-cut-threads (threads)
10433   "Cut off all uninteresting articles from the beginning of threads."
10434   (when (or (eq gnus-fetch-old-headers 'some)
10435             (eq gnus-build-sparse-threads 'some)
10436             (eq gnus-build-sparse-threads 'more))
10437     (let ((th threads))
10438       (while th
10439         (setcar th (gnus-cut-thread (car th)))
10440         (setq th (cdr th)))))
10441   threads)
10442
10443 (defun gnus-summary-initial-limit (&optional show-if-empty)
10444   "Figure out what the initial limit is supposed to be on group entry.
10445 This entails weeding out unwanted dormants, low-scored articles,
10446 fetch-old-headers verbiage, and so on."
10447   ;; Most groups have nothing to remove.
10448   (if (or gnus-inhibit-limiting
10449           (and (null gnus-newsgroup-dormant)
10450                (not (eq gnus-fetch-old-headers 'some))
10451                (null gnus-summary-expunge-below)
10452                (not (eq gnus-build-sparse-threads 'some))
10453                (not (eq gnus-build-sparse-threads 'more))
10454                (null gnus-thread-expunge-below)
10455                (not gnus-use-nocem)))
10456       () ; Do nothing.
10457     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10458     (setq gnus-newsgroup-limit nil)
10459     (mapatoms
10460      (lambda (node)
10461        (unless (car (symbol-value node))
10462          ;; These threads have no parents -- they are roots.
10463          (let ((nodes (cdr (symbol-value node)))
10464                thread)
10465            (while nodes
10466              (if (and gnus-thread-expunge-below
10467                       (< (gnus-thread-total-score (car nodes))
10468                          gnus-thread-expunge-below))
10469                  (gnus-expunge-thread (pop nodes))
10470                (setq thread (pop nodes))
10471                (gnus-summary-limit-children thread))))))
10472      gnus-newsgroup-dependencies)
10473     ;; If this limitation resulted in an empty group, we might
10474     ;; pop the previous limit and use it instead.
10475     (when (and (not gnus-newsgroup-limit)
10476                show-if-empty)
10477       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10478     gnus-newsgroup-limit))
10479
10480 (defun gnus-summary-limit-children (thread)
10481   "Return 1 if this subthread is visible and 0 if it is not."
10482   ;; First we get the number of visible children to this thread.  This
10483   ;; is done by recursing down the thread using this function, so this
10484   ;; will really go down to a leaf article first, before slowly
10485   ;; working its way up towards the root.
10486   (when thread
10487     (let ((children
10488            (if (cdr thread)
10489                (apply '+ (mapcar 'gnus-summary-limit-children
10490                                  (cdr thread)))
10491              0))
10492           (number (mail-header-number (car thread)))
10493           score)
10494       (if (or
10495            ;; If this article is dormant and has absolutely no visible
10496            ;; children, then this article isn't visible.
10497            (and (memq number gnus-newsgroup-dormant)
10498                 (= children 0))
10499            ;; If this is a "fetch-old-headered" and there is only one
10500            ;; visible child (or less), then we don't want this article.
10501            (and (eq gnus-fetch-old-headers 'some)
10502                 (memq number gnus-newsgroup-ancient)
10503                 (zerop children))
10504            ;; If this is a sparsely inserted article with no children,
10505            ;; we don't want it.
10506            (and (eq gnus-build-sparse-threads 'some)
10507                 (memq number gnus-newsgroup-sparse)
10508                 (zerop children))
10509            ;; If we use expunging, and this article is really
10510            ;; low-scored, then we don't want this article.
10511            (when (and gnus-summary-expunge-below
10512                       (< (setq score
10513                                (or (cdr (assq number gnus-newsgroup-scored))
10514                                    gnus-summary-default-score))
10515                          gnus-summary-expunge-below))
10516              ;; We increase the expunge-tally here, but that has
10517              ;; nothing to do with the limits, really.
10518              (incf gnus-newsgroup-expunged-tally)
10519              ;; We also mark as read here, if that's wanted.
10520              (when (and gnus-summary-mark-below
10521                         (< score gnus-summary-mark-below))
10522                (setq gnus-newsgroup-unreads
10523                      (delq number gnus-newsgroup-unreads))
10524                (if gnus-newsgroup-auto-expire
10525                    (push number gnus-newsgroup-expirable)
10526                  (push (cons number gnus-low-score-mark)
10527                        gnus-newsgroup-reads)))
10528              t)
10529            (and gnus-use-nocem
10530                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10531           ;; Nope, invisible article.
10532           0
10533         ;; Ok, this article is to be visible, so we add it to the limit
10534         ;; and return 1.
10535         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10536         1))))
10537
10538 (defun gnus-expunge-thread (thread)
10539   "Mark all articles in THREAD as read."
10540   (let* ((number (mail-header-number (car thread))))
10541     (incf gnus-newsgroup-expunged-tally)
10542     ;; We also mark as read here, if that's wanted.
10543     (setq gnus-newsgroup-unreads
10544           (delq number gnus-newsgroup-unreads))
10545     (if gnus-newsgroup-auto-expire
10546         (push number gnus-newsgroup-expirable)
10547       (push (cons number gnus-low-score-mark)
10548             gnus-newsgroup-reads)))
10549   ;; Go recursively through all subthreads.
10550   (mapcar 'gnus-expunge-thread (cdr thread)))
10551
10552 ;; Summary article oriented commands
10553
10554 (defun gnus-summary-refer-parent-article (n)
10555   "Refer parent article N times.
10556 The difference between N and the number of articles fetched is returned."
10557   (interactive "p")
10558   (gnus-set-global-variables)
10559   (while
10560       (and
10561        (> n 0)
10562        (let* ((header (gnus-summary-article-header))
10563               (ref
10564                ;; If we try to find the parent of the currently
10565                ;; displayed article, then we take a look at the actual
10566                ;; References header, since this is slightly more
10567                ;; reliable than the References field we got from the
10568                ;; server.
10569                (if (and (eq (mail-header-number header)
10570                             (cdr gnus-article-current))
10571                         (equal gnus-newsgroup-name
10572                                (car gnus-article-current)))
10573                    (save-excursion
10574                      (set-buffer gnus-original-article-buffer)
10575                      (nnheader-narrow-to-headers)
10576                      (prog1
10577                          (mail-fetch-field "references")
10578                        (widen)))
10579                  ;; It's not the current article, so we take a bet on
10580                  ;; the value we got from the server.
10581                  (mail-header-references header))))
10582          (if (setq ref (or ref (mail-header-references header)))
10583              (or (gnus-summary-refer-article (gnus-parent-id ref))
10584                  (gnus-message 1 "Couldn't find parent"))
10585            (gnus-message 1 "No references in article %d"
10586                          (gnus-summary-article-number))
10587            nil)))
10588     (setq n (1- n)))
10589   (gnus-summary-position-point)
10590   n)
10591
10592 (defun gnus-summary-refer-references ()
10593   "Fetch all articles mentioned in the References header.
10594 Return how many articles were fetched."
10595   (interactive)
10596   (gnus-set-global-variables)
10597   (let ((ref (mail-header-references (gnus-summary-article-header)))
10598         (current (gnus-summary-article-number))
10599         (n 0))
10600     ;; For each Message-ID in the References header...
10601     (while (string-match "<[^>]*>" ref)
10602       (incf n)
10603       ;; ... fetch that article.
10604       (gnus-summary-refer-article
10605        (prog1 (match-string 0 ref)
10606          (setq ref (substring ref (match-end 0))))))
10607     (gnus-summary-goto-subject current)
10608     (gnus-summary-position-point)
10609     n))
10610
10611 (defun gnus-summary-refer-article (message-id)
10612   "Fetch an article specified by MESSAGE-ID."
10613   (interactive "sMessage-ID: ")
10614   (when (and (stringp message-id)
10615              (not (zerop (length message-id))))
10616     ;; Construct the correct Message-ID if necessary.
10617     ;; Suggested by tale@pawl.rpi.edu.
10618     (unless (string-match "^<" message-id)
10619       (setq message-id (concat "<" message-id)))
10620     (unless (string-match ">$" message-id)
10621       (setq message-id (concat message-id ">")))
10622     (let ((header (car (gnus-gethash (downcase message-id)
10623                                      gnus-newsgroup-dependencies))))
10624       (if header
10625           ;; The article is present in the buffer, to we just go to it.
10626           (gnus-summary-goto-article (mail-header-number header) nil t)
10627         ;; We fetch the article
10628         (let ((gnus-override-method gnus-refer-article-method)
10629               number)
10630           ;; Start the special refer-article method, if necessary.
10631           (when gnus-refer-article-method
10632             (gnus-check-server gnus-refer-article-method))
10633           ;; Fetch the header, and display the article.
10634           (if (setq number (gnus-summary-insert-subject message-id))
10635               (gnus-summary-select-article nil nil nil number)
10636             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10637
10638 (defun gnus-summary-enter-digest-group (&optional force)
10639   "Enter a digest group based on the current article."
10640   (interactive "P")
10641   (gnus-set-global-variables)
10642   (gnus-summary-select-article)
10643   (let ((name (format "%s-%d"
10644                       (gnus-group-prefixed-name
10645                        gnus-newsgroup-name (list 'nndoc ""))
10646                       gnus-current-article))
10647         (ogroup gnus-newsgroup-name)
10648         (case-fold-search t)
10649         (buf (current-buffer))
10650         dig)
10651     (save-excursion
10652       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10653       (insert-buffer-substring gnus-original-article-buffer)
10654       (narrow-to-region
10655        (goto-char (point-min))
10656        (or (search-forward "\n\n" nil t) (point)))
10657       (goto-char (point-min))
10658       (delete-matching-lines "^\\(Path\\):\\|^From ")
10659       (widen))
10660     (unwind-protect
10661         (if (gnus-group-read-ephemeral-group
10662              name `(nndoc ,name (nndoc-address
10663                                  ,(get-buffer dig))
10664                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10665             ;; Make all postings to this group go to the parent group.
10666             (setcdr (nthcdr 4 (gnus-get-info name))
10667                     (list (list (cons 'to-group ogroup))))
10668           ;; Couldn't select this doc group.
10669           (switch-to-buffer buf)
10670           (gnus-set-global-variables)
10671           (gnus-configure-windows 'summary)
10672           (gnus-message 3 "Article couldn't be entered?"))
10673       (kill-buffer dig))))
10674
10675 (defun gnus-summary-isearch-article (&optional regexp-p)
10676   "Do incremental search forward on the current article.
10677 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10678   (interactive "P")
10679   (gnus-set-global-variables)
10680   (gnus-summary-select-article)
10681   (gnus-configure-windows 'article)
10682   (gnus-eval-in-buffer-window
10683    gnus-article-buffer
10684    (goto-char (point-min))
10685    (isearch-forward regexp-p)))
10686
10687 (defun gnus-summary-search-article-forward (regexp &optional backward)
10688   "Search for an article containing REGEXP forward.
10689 If BACKWARD, search backward instead."
10690   (interactive
10691    (list (read-string
10692           (format "Search article %s (regexp%s): "
10693                   (if current-prefix-arg "backward" "forward")
10694                   (if gnus-last-search-regexp
10695                       (concat ", default " gnus-last-search-regexp)
10696                     "")))
10697          current-prefix-arg))
10698   (gnus-set-global-variables)
10699   (if (string-equal regexp "")
10700       (setq regexp (or gnus-last-search-regexp ""))
10701     (setq gnus-last-search-regexp regexp))
10702   (if (gnus-summary-search-article regexp backward)
10703       (gnus-article-set-window-start
10704        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10705     (error "Search failed: \"%s\"" regexp)))
10706
10707 (defun gnus-summary-search-article-backward (regexp)
10708   "Search for an article containing REGEXP backward."
10709   (interactive
10710    (list (read-string
10711           (format "Search article backward (regexp%s): "
10712                   (if gnus-last-search-regexp
10713                       (concat ", default " gnus-last-search-regexp)
10714                     "")))))
10715   (gnus-summary-search-article-forward regexp 'backward))
10716
10717 (defun gnus-summary-search-article (regexp &optional backward)
10718   "Search for an article containing REGEXP.
10719 Optional argument BACKWARD means do search for backward.
10720 gnus-select-article-hook is not called during the search."
10721   (let ((gnus-select-article-hook nil)  ;Disable hook.
10722         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10723         (re-search
10724          (if backward
10725              (function re-search-backward) (function re-search-forward)))
10726         (found nil)
10727         (last nil))
10728     ;; Hidden thread subtrees must be searched for ,too.
10729     (gnus-summary-show-all-threads)
10730     ;; First of all, search current article.
10731     ;; We don't want to read article again from NNTP server nor reset
10732     ;; current point.
10733     (gnus-summary-select-article)
10734     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10735     (setq last gnus-current-article)
10736     (gnus-eval-in-buffer-window
10737      gnus-article-buffer
10738      (save-restriction
10739        (widen)
10740        ;; Begin search from current point.
10741        (setq found (funcall re-search regexp nil t))))
10742     ;; Then search next articles.
10743     (while (and (not found)
10744                 (gnus-summary-display-article
10745                  (if backward (gnus-summary-find-prev)
10746                    (gnus-summary-find-next))))
10747       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10748       (gnus-eval-in-buffer-window
10749        gnus-article-buffer
10750        (save-restriction
10751          (widen)
10752          (goto-char (if backward (point-max) (point-min)))
10753          (setq found (funcall re-search regexp nil t)))))
10754     (message "")
10755     ;; Adjust article pointer.
10756     (or (eq last gnus-current-article)
10757         (setq gnus-last-article last))
10758     ;; Return T if found such article.
10759     found))
10760
10761 (defun gnus-summary-find-matching (header regexp &optional backward unread
10762                                           not-case-fold)
10763   "Return a list of all articles that match REGEXP on HEADER.
10764 The search stars on the current article and goes forwards unless
10765 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10766 If UNREAD is non-nil, only unread articles will
10767 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10768 in the comparisons."
10769   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10770                 (gnus-data-find-list
10771                  (gnus-summary-article-number) (gnus-data-list backward))))
10772         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10773         (case-fold-search (not not-case-fold))
10774         articles d)
10775     (or (fboundp (intern (concat "mail-header-" header)))
10776         (error "%s is not a valid header" header))
10777     (while data
10778       (setq d (car data))
10779       (and (or (not unread)             ; We want all articles...
10780                (gnus-data-unread-p d))  ; Or just unreads.
10781            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10782            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10783            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10784       (setq data (cdr data)))
10785     (nreverse articles)))
10786
10787 (defun gnus-summary-execute-command (header regexp command &optional backward)
10788   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10789 If HEADER is an empty string (or nil), the match is done on the entire
10790 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10791   (interactive
10792    (list (let ((completion-ignore-case t))
10793            (completing-read
10794             "Header name: "
10795             (mapcar (lambda (string) (list string))
10796                     '("Number" "Subject" "From" "Lines" "Date"
10797                       "Message-ID" "Xref" "References" "Body"))
10798             nil 'require-match))
10799          (read-string "Regexp: ")
10800          (read-key-sequence "Command: ")
10801          current-prefix-arg))
10802   (when (equal header "Body")
10803     (setq header ""))
10804   (gnus-set-global-variables)
10805   ;; Hidden thread subtrees must be searched as well.
10806   (gnus-summary-show-all-threads)
10807   ;; We don't want to change current point nor window configuration.
10808   (save-excursion
10809     (save-window-excursion
10810       (gnus-message 6 "Executing %s..." (key-description command))
10811       ;; We'd like to execute COMMAND interactively so as to give arguments.
10812       (gnus-execute header regexp
10813                     `(lambda () (call-interactively ',(key-binding command)))
10814                     backward)
10815       (gnus-message 6 "Executing %s...done" (key-description command)))))
10816
10817 (defun gnus-summary-beginning-of-article ()
10818   "Scroll the article back to the beginning."
10819   (interactive)
10820   (gnus-set-global-variables)
10821   (gnus-summary-select-article)
10822   (gnus-configure-windows 'article)
10823   (gnus-eval-in-buffer-window
10824    gnus-article-buffer
10825    (widen)
10826    (goto-char (point-min))
10827    (and gnus-break-pages (gnus-narrow-to-page))))
10828
10829 (defun gnus-summary-end-of-article ()
10830   "Scroll to the end of the article."
10831   (interactive)
10832   (gnus-set-global-variables)
10833   (gnus-summary-select-article)
10834   (gnus-configure-windows 'article)
10835   (gnus-eval-in-buffer-window
10836    gnus-article-buffer
10837    (widen)
10838    (goto-char (point-max))
10839    (recenter -3)
10840    (and gnus-break-pages (gnus-narrow-to-page))))
10841
10842 (defun gnus-summary-show-article (&optional arg)
10843   "Force re-fetching of the current article.
10844 If ARG (the prefix) is non-nil, show the raw article without any
10845 article massaging functions being run."
10846   (interactive "P")
10847   (gnus-set-global-variables)
10848   (if (not arg)
10849       ;; Select the article the normal way.
10850       (gnus-summary-select-article nil 'force)
10851     ;; Bind the article treatment functions to nil.
10852     (let ((gnus-have-all-headers t)
10853           gnus-article-display-hook
10854           gnus-article-prepare-hook
10855           gnus-visual)
10856       (gnus-summary-select-article nil 'force)))
10857 ;  (gnus-configure-windows 'article)
10858   (gnus-summary-position-point))
10859
10860 (defun gnus-summary-verbose-headers (&optional arg)
10861   "Toggle permanent full header display.
10862 If ARG is a positive number, turn header display on.
10863 If ARG is a negative number, turn header display off."
10864   (interactive "P")
10865   (gnus-set-global-variables)
10866   (gnus-summary-toggle-header arg)
10867   (setq gnus-show-all-headers
10868         (cond ((or (not (numberp arg))
10869                    (zerop arg))
10870                (not gnus-show-all-headers))
10871               ((natnump arg)
10872                t))))
10873
10874 (defun gnus-summary-toggle-header (&optional arg)
10875   "Show the headers if they are hidden, or hide them if they are shown.
10876 If ARG is a positive number, show the entire header.
10877 If ARG is a negative number, hide the unwanted header lines."
10878   (interactive "P")
10879   (gnus-set-global-variables)
10880   (save-excursion
10881     (set-buffer gnus-article-buffer)
10882     (let* ((buffer-read-only nil)
10883            (inhibit-point-motion-hooks t)
10884            (hidden (text-property-any
10885                     (goto-char (point-min)) (search-forward "\n\n")
10886                     'invisible t))
10887            e)
10888       (goto-char (point-min))
10889       (when (search-forward "\n\n" nil t)
10890         (delete-region (point-min) (1- (point))))
10891       (goto-char (point-min))
10892       (save-excursion
10893         (set-buffer gnus-original-article-buffer)
10894         (goto-char (point-min))
10895         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10896       (insert-buffer-substring gnus-original-article-buffer 1 e)
10897       (let ((gnus-inhibit-hiding t))
10898         (run-hooks 'gnus-article-display-hook))
10899       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10900           (gnus-article-hide-headers)))))
10901
10902 (defun gnus-summary-show-all-headers ()
10903   "Make all header lines visible."
10904   (interactive)
10905   (gnus-set-global-variables)
10906   (gnus-article-show-all-headers))
10907
10908 (defun gnus-summary-toggle-mime (&optional arg)
10909   "Toggle MIME processing.
10910 If ARG is a positive number, turn MIME processing on."
10911   (interactive "P")
10912   (gnus-set-global-variables)
10913   (setq gnus-show-mime
10914         (if (null arg) (not gnus-show-mime)
10915           (> (prefix-numeric-value arg) 0)))
10916   (gnus-summary-select-article t 'force))
10917
10918 (defun gnus-summary-caesar-message (&optional arg)
10919   "Caesar rotate the current article by 13.
10920 The numerical prefix specifies how manu places to rotate each letter
10921 forward."
10922   (interactive "P")
10923   (gnus-set-global-variables)
10924   (gnus-summary-select-article)
10925   (let ((mail-header-separator ""))
10926     (gnus-eval-in-buffer-window
10927      gnus-article-buffer
10928      (save-restriction
10929        (widen)
10930        (let ((start (window-start)))
10931          (news-caesar-buffer-body arg)
10932          (set-window-start (get-buffer-window (current-buffer)) start))))))
10933
10934 (defun gnus-summary-stop-page-breaking ()
10935   "Stop page breaking in the current article."
10936   (interactive)
10937   (gnus-set-global-variables)
10938   (gnus-summary-select-article)
10939   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
10940
10941 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
10942   "Move the current article to a different newsgroup.
10943 If N is a positive number, move the N next articles.
10944 If N is a negative number, move the N previous articles.
10945 If N is nil and any articles have been marked with the process mark,
10946 move those articles instead.
10947 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
10948 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10949 re-spool using this method.
10950
10951 For this function to work, both the current newsgroup and the
10952 newsgroup that you want to move to have to support the `request-move'
10953 and `request-accept' functions."
10954   (interactive "P")
10955   (unless action (setq action 'move))
10956   (gnus-set-global-variables)
10957   ;; Check whether the source group supports the required functions.
10958   (cond ((and (eq action 'move)
10959               (not (gnus-check-backend-function
10960                     'request-move-article gnus-newsgroup-name)))
10961          (error "The current group does not support article moving"))
10962         ((and (eq action 'crosspost)
10963               (not (gnus-check-backend-function
10964                     'request-replace-article gnus-newsgroup-name)))
10965          (error "The current group does not support article editing")))
10966   (let ((articles (gnus-summary-work-articles n))
10967         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10968         (names '((move "move" "Moving")
10969                  (copy "copy" "Copying")
10970                  (crosspost "crosspost" "Crossposting")))
10971         (copy-buf (save-excursion
10972                     (nnheader-set-temp-buffer " *copy article*")))
10973         art-group to-method new-xref article)
10974     (unless (assq action names)
10975       (error "Unknown action %s" action))
10976     ;; Read the newsgroup name.
10977     (when (and (not to-newsgroup)
10978                (not select-method))
10979       (setq to-newsgroup
10980             (gnus-read-move-group-name
10981              (cadr (assq action names))
10982              gnus-current-move-group articles prefix))
10983       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
10984     (setq to-method (if select-method (list select-method "")
10985                       (gnus-find-method-for-group to-newsgroup)))
10986     ;;(when (equal to-newsgroup gnus-newsgroup-name)
10987     ;;(error "Can't %s to the same group you're already in" action))
10988     ;; Check the method we are to move this article to...
10989     (or (gnus-check-backend-function 'request-accept-article (car to-method))
10990         (error "%s does not support article copying" (car to-method)))
10991     (or (gnus-check-server to-method)
10992         (error "Can't open server %s" (car to-method)))
10993     (gnus-message 6 "%s to %s: %s..."
10994                   (caddr (assq action names))
10995                   (or select-method to-newsgroup) articles)
10996     (while articles
10997       (setq article (pop articles))
10998       (setq
10999        art-group
11000        (cond
11001         ;; Move the article.
11002         ((eq action 'move)
11003          (gnus-request-move-article
11004           article                       ; Article to move
11005           gnus-newsgroup-name           ; From newsgrouo
11006           (nth 1 (gnus-find-method-for-group
11007                   gnus-newsgroup-name)) ; Server
11008           (list 'gnus-request-accept-article
11009                 (if select-method
11010                     (list 'quote select-method)
11011                   to-newsgroup)
11012                 (not articles))         ; Accept form
11013           (not articles)))              ; Only save nov last time
11014         ;; Copy the article.
11015         ((eq action 'copy)
11016          (save-excursion
11017            (set-buffer copy-buf)
11018            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11019            (gnus-request-accept-article
11020             (if select-method select-method to-newsgroup)
11021             (not articles))))
11022         ;; Crosspost the article.
11023         ((eq action 'crosspost)
11024          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11025            (setq new-xref (concat gnus-newsgroup-name ":" article))
11026            (if (and xref (not (string= xref "")))
11027                (progn
11028                  (when (string-match "^Xref: " xref)
11029                    (setq xref (substring xref (match-end 0))))
11030                  (setq new-xref (concat xref " " new-xref)))
11031              (setq new-xref (concat (system-name) " " new-xref)))
11032            (save-excursion
11033              (set-buffer copy-buf)
11034              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11035              (nnheader-replace-header "xref" new-xref)
11036              (gnus-request-accept-article
11037               (if select-method select-method to-newsgroup)
11038               (not articles)))))))
11039       (if (not art-group)
11040           (gnus-message 1 "Couldn't %s article %s"
11041                         (cadr (assq action names)) article)
11042         (let* ((entry
11043                 (or
11044                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11045                  (gnus-gethash
11046                   (gnus-group-prefixed-name
11047                    (car art-group)
11048                    (if select-method (list select-method "")
11049                      (gnus-find-method-for-group to-newsgroup)))
11050                   gnus-newsrc-hashtb)))
11051                (info (nth 2 entry)))
11052           ;; Update the group that has been moved to.
11053           (when (and info
11054                      (memq action '(move copy)))
11055             (unless (memq article gnus-newsgroup-unreads)
11056               (gnus-info-set-read
11057                info (gnus-add-to-range (gnus-info-read info)
11058                                        (list (cdr art-group)))))
11059
11060             ;; Copy any marks over to the new group.
11061             (let ((marks gnus-article-mark-lists)
11062                   (to-article (cdr art-group)))
11063
11064               ;; See whether the article is to be put in the cache.
11065               (when gnus-use-cache
11066                 (gnus-cache-possibly-enter-article
11067                  (gnus-info-group info) to-article
11068                  (let ((header (copy-sequence
11069                                 (gnus-summary-article-header article))))
11070                    (mail-header-set-number header to-article)
11071                    header)
11072                  (memq article gnus-newsgroup-marked)
11073                  (memq article gnus-newsgroup-dormant)
11074                  (memq article gnus-newsgroup-unreads)))
11075
11076               (while marks
11077                 (when (memq article (symbol-value
11078                                      (intern (format "gnus-newsgroup-%s"
11079                                                      (caar marks)))))
11080                   (gnus-add-marked-articles
11081                    (gnus-info-group info) (cdar marks)
11082                    (list to-article) info))
11083                 (setq marks (cdr marks)))))
11084
11085           ;; Update the Xref header in this article to point to
11086           ;; the new crossposted article we have just created.
11087           (when (eq action 'crosspost)
11088             (save-excursion
11089               (set-buffer copy-buf)
11090               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11091               (nnheader-replace-header
11092                "xref" (concat new-xref " " (gnus-group-prefixed-name
11093                                             (car art-group) to-method)
11094                               ":" (cdr art-group)))
11095               (gnus-request-replace-article
11096                article gnus-newsgroup-name (current-buffer)))))
11097
11098         (gnus-summary-goto-subject article)
11099         (when (eq action 'move)
11100           (gnus-summary-mark-article article gnus-canceled-mark)))
11101       (gnus-summary-remove-process-mark article))
11102     (gnus-kill-buffer copy-buf)
11103     (gnus-summary-position-point)
11104     (gnus-set-mode-line 'summary)))
11105
11106 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11107   "Move the current article to a different newsgroup.
11108 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11109 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11110 re-spool using this method."
11111   (interactive "P")
11112   (gnus-summary-move-article n nil nil 'copy))
11113
11114 (defun gnus-summary-crosspost-article (&optional n)
11115   "Crosspost the current article to some other group."
11116   (interactive "P")
11117   (gnus-summary-move-article n nil nil 'crosspost))
11118
11119 (defun gnus-summary-respool-article (&optional n respool-method)
11120   "Respool the current article.
11121 The article will be squeezed through the mail spooling process again,
11122 which means that it will be put in some mail newsgroup or other
11123 depending on `nnmail-split-methods'.
11124 If N is a positive number, respool the N next articles.
11125 If N is a negative number, respool the N previous articles.
11126 If N is nil and any articles have been marked with the process mark,
11127 respool those articles instead.
11128
11129 Respooling can be done both from mail groups and \"real\" newsgroups.
11130 In the former case, the articles in question will be moved from the
11131 current group into whatever groups they are destined to.  In the
11132 latter case, they will be copied into the relevant groups."
11133   (interactive "P")
11134   (gnus-set-global-variables)
11135   (let ((respool-methods (gnus-methods-using 'respool))
11136         (methname
11137          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11138     (or respool-method
11139         (setq respool-method
11140               (completing-read
11141                "What method do you want to use when respooling? "
11142                respool-methods nil t methname)))
11143     (or (string= respool-method "")
11144         (if (assoc (symbol-name
11145                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11146                    respool-methods)
11147             (gnus-summary-move-article n nil (intern respool-method))
11148           (gnus-summary-copy-article n nil (intern respool-method))))))
11149
11150 (defun gnus-summary-import-article (file)
11151   "Import a random file into a mail newsgroup."
11152   (interactive "fImport file: ")
11153   (gnus-set-global-variables)
11154   (let ((group gnus-newsgroup-name)
11155         (now (current-time))
11156         atts lines)
11157     (or (gnus-check-backend-function 'request-accept-article group)
11158         (error "%s does not support article importing" group))
11159     (or (file-readable-p file)
11160         (not (file-regular-p file))
11161         (error "Can't read %s" file))
11162     (save-excursion
11163       (set-buffer (get-buffer-create " *import file*"))
11164       (buffer-disable-undo (current-buffer))
11165       (erase-buffer)
11166       (insert-file-contents file)
11167       (goto-char (point-min))
11168       (unless (nnheader-article-p)
11169         ;; This doesn't look like an article, so we fudge some headers.
11170         (setq atts (file-attributes file)
11171               lines (count-lines (point-min) (point-max)))
11172         (insert "From: " (read-string "From: ") "\n"
11173                 "Subject: " (read-string "Subject: ") "\n"
11174                 "Date: " (timezone-make-date-arpa-standard
11175                           (current-time-string (nth 5 atts))
11176                           (current-time-zone now)
11177                           (current-time-zone now)) "\n"
11178                 "Message-ID: " (gnus-inews-message-id) "\n"
11179                 "Lines: " (int-to-string lines) "\n"
11180                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11181       (gnus-request-accept-article group t)
11182       (kill-buffer (current-buffer)))))
11183
11184 (defun gnus-summary-expire-articles ()
11185   "Expire all articles that are marked as expirable in the current group."
11186   (interactive)
11187   (gnus-set-global-variables)
11188   (when (gnus-check-backend-function
11189          'request-expire-articles gnus-newsgroup-name)
11190     ;; This backend supports expiry.
11191     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11192            (expirable (if total
11193                           (gnus-list-of-read-articles gnus-newsgroup-name)
11194                         (setq gnus-newsgroup-expirable
11195                               (sort gnus-newsgroup-expirable '<))))
11196            (expiry-wait (gnus-group-get-parameter
11197                          gnus-newsgroup-name 'expiry-wait))
11198            es)
11199       (when expirable
11200         ;; There are expirable articles in this group, so we run them
11201         ;; through the expiry process.
11202         (gnus-message 6 "Expiring articles...")
11203         ;; The list of articles that weren't expired is returned.
11204         (if expiry-wait
11205             (let ((nnmail-expiry-wait-function nil)
11206                   (nnmail-expiry-wait expiry-wait))
11207               (setq es (gnus-request-expire-articles
11208                         expirable gnus-newsgroup-name)))
11209           (setq es (gnus-request-expire-articles
11210                     expirable gnus-newsgroup-name)))
11211         (or total (setq gnus-newsgroup-expirable es))
11212         ;; We go through the old list of expirable, and mark all
11213         ;; really expired articles as nonexistent.
11214         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11215           (let ((gnus-use-cache nil))
11216             (while expirable
11217               (unless (memq (car expirable) es)
11218                 (when (gnus-data-find (car expirable))
11219                   (gnus-summary-mark-article
11220                    (car expirable) gnus-canceled-mark)))
11221               (setq expirable (cdr expirable)))))
11222         (gnus-message 6 "Expiring articles...done")))))
11223
11224 (defun gnus-summary-expire-articles-now ()
11225   "Expunge all expirable articles in the current group.
11226 This means that *all* articles that are marked as expirable will be
11227 deleted forever, right now."
11228   (interactive)
11229   (gnus-set-global-variables)
11230   (or gnus-expert-user
11231       (gnus-y-or-n-p
11232        "Are you really, really, really sure you want to expunge? ")
11233       (error "Phew!"))
11234   (let ((nnmail-expiry-wait 'immediate)
11235         (nnmail-expiry-wait-function nil))
11236     (gnus-summary-expire-articles)))
11237
11238 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11239 (defun gnus-summary-delete-article (&optional n)
11240   "Delete the N next (mail) articles.
11241 This command actually deletes articles.  This is not a marking
11242 command.  The article will disappear forever from your life, never to
11243 return.
11244 If N is negative, delete backwards.
11245 If N is nil and articles have been marked with the process mark,
11246 delete these instead."
11247   (interactive "P")
11248   (gnus-set-global-variables)
11249   (or (gnus-check-backend-function 'request-expire-articles
11250                                    gnus-newsgroup-name)
11251       (error "The current newsgroup does not support article deletion."))
11252   ;; Compute the list of articles to delete.
11253   (let ((articles (gnus-summary-work-articles n))
11254         not-deleted)
11255     (if (and gnus-novice-user
11256              (not (gnus-y-or-n-p
11257                    (format "Do you really want to delete %s forever? "
11258                            (if (> (length articles) 1) "these articles"
11259                              "this article")))))
11260         ()
11261       ;; Delete the articles.
11262       (setq not-deleted (gnus-request-expire-articles
11263                          articles gnus-newsgroup-name 'force))
11264       (while articles
11265         (gnus-summary-remove-process-mark (car articles))
11266         ;; The backend might not have been able to delete the article
11267         ;; after all.
11268         (or (memq (car articles) not-deleted)
11269             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11270         (setq articles (cdr articles))))
11271     (gnus-summary-position-point)
11272     (gnus-set-mode-line 'summary)
11273     not-deleted))
11274
11275 (defun gnus-summary-edit-article (&optional force)
11276   "Enter into a buffer and edit the current article.
11277 This will have permanent effect only in mail groups.
11278 If FORCE is non-nil, allow editing of articles even in read-only
11279 groups."
11280   (interactive "P")
11281   (save-excursion
11282     (set-buffer gnus-summary-buffer)
11283     (gnus-set-global-variables)
11284     (when (and (not force)
11285                (gnus-group-read-only-p))
11286       (error "The current newsgroup does not support article editing."))
11287     (gnus-summary-select-article t nil t)
11288     (gnus-configure-windows 'article)
11289     (select-window (get-buffer-window gnus-article-buffer))
11290     (gnus-message 6 "C-c C-c to end edits")
11291     (setq buffer-read-only nil)
11292     (text-mode)
11293     (use-local-map (copy-keymap (current-local-map)))
11294     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11295     (buffer-enable-undo)
11296     (widen)
11297     (goto-char (point-min))
11298     (search-forward "\n\n" nil t)))
11299
11300 (defun gnus-summary-edit-article-done ()
11301   "Make edits to the current article permanent."
11302   (interactive)
11303   (if (gnus-group-read-only-p)
11304       (progn
11305         (gnus-summary-edit-article-postpone)
11306         (gnus-message
11307          1 "The current newsgroup does not support article editing.")
11308         (ding))
11309     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11310       (erase-buffer)
11311       (insert buf)
11312       (if (not (gnus-request-replace-article
11313                 (cdr gnus-article-current) (car gnus-article-current)
11314                 (current-buffer)))
11315           (error "Couldn't replace article.")
11316         (gnus-article-mode)
11317         (use-local-map gnus-article-mode-map)
11318         (setq buffer-read-only t)
11319         (buffer-disable-undo (current-buffer))
11320         (gnus-configure-windows 'summary)
11321         (gnus-summary-update-article (cdr gnus-article-current)))
11322       (run-hooks 'gnus-article-display-hook)
11323       (and (gnus-visual-p 'summary-highlight 'highlight)
11324            (run-hooks 'gnus-visual-mark-article-hook)))))
11325
11326 (defun gnus-summary-edit-article-postpone ()
11327   "Postpone changes to the current article."
11328   (interactive)
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   (and (gnus-visual-p 'summary-highlight 'highlight)
11335        (run-hooks 'gnus-visual-mark-article-hook)))
11336
11337 (defun gnus-summary-respool-query ()
11338   "Query where the respool algorithm would put this article."
11339   (interactive)
11340   (gnus-set-global-variables)
11341   (gnus-summary-select-article)
11342   (save-excursion
11343     (set-buffer gnus-article-buffer)
11344     (save-restriction
11345       (goto-char (point-min))
11346       (search-forward "\n\n")
11347       (narrow-to-region (point-min) (point))
11348       (pp-eval-expression
11349        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11350
11351 ;; Summary score commands.
11352
11353 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11354
11355 (defun gnus-summary-raise-score (n)
11356   "Raise the score of the current article by N."
11357   (interactive "p")
11358   (gnus-set-global-variables)
11359   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11360
11361 (defun gnus-summary-set-score (n)
11362   "Set the score of the current article to N."
11363   (interactive "p")
11364   (gnus-set-global-variables)
11365   (save-excursion
11366     (gnus-summary-show-thread)
11367     (let ((buffer-read-only nil))
11368       ;; Set score.
11369       (gnus-summary-update-mark
11370        (if (= n (or gnus-summary-default-score 0)) ? 
11371          (if (< n (or gnus-summary-default-score 0))
11372              gnus-score-below-mark gnus-score-over-mark)) 'score))
11373     (let* ((article (gnus-summary-article-number))
11374            (score (assq article gnus-newsgroup-scored)))
11375       (if score (setcdr score n)
11376         (setq gnus-newsgroup-scored
11377               (cons (cons article n) gnus-newsgroup-scored))))
11378     (gnus-summary-update-line)))
11379
11380 (defun gnus-summary-current-score ()
11381   "Return the score of the current article."
11382   (interactive)
11383   (gnus-set-global-variables)
11384   (message "%s" (gnus-summary-article-score)))
11385
11386 ;; Summary marking commands.
11387
11388 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11389   "Mark articles which has the same subject as read, and then select the next.
11390 If UNMARK is positive, remove any kind of mark.
11391 If UNMARK is negative, tick articles."
11392   (interactive "P")
11393   (gnus-set-global-variables)
11394   (if unmark
11395       (setq unmark (prefix-numeric-value unmark)))
11396   (let ((count
11397          (gnus-summary-mark-same-subject
11398           (gnus-summary-article-subject) unmark)))
11399     ;; Select next unread article.  If auto-select-same mode, should
11400     ;; select the first unread article.
11401     (gnus-summary-next-article t (and gnus-auto-select-same
11402                                       (gnus-summary-article-subject)))
11403     (gnus-message 7 "%d article%s marked as %s"
11404                   count (if (= count 1) " is" "s are")
11405                   (if unmark "unread" "read"))))
11406
11407 (defun gnus-summary-kill-same-subject (&optional unmark)
11408   "Mark articles which has the same subject as read.
11409 If UNMARK is positive, remove any kind of mark.
11410 If UNMARK is negative, tick articles."
11411   (interactive "P")
11412   (gnus-set-global-variables)
11413   (if unmark
11414       (setq unmark (prefix-numeric-value unmark)))
11415   (let ((count
11416          (gnus-summary-mark-same-subject
11417           (gnus-summary-article-subject) unmark)))
11418     ;; If marked as read, go to next unread subject.
11419     (if (null unmark)
11420         ;; Go to next unread subject.
11421         (gnus-summary-next-subject 1 t))
11422     (gnus-message 7 "%d articles are marked as %s"
11423                   count (if unmark "unread" "read"))))
11424
11425 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11426   "Mark articles with same SUBJECT as read, and return marked number.
11427 If optional argument UNMARK is positive, remove any kinds of marks.
11428 If optional argument UNMARK is negative, mark articles as unread instead."
11429   (let ((count 1))
11430     (save-excursion
11431       (cond
11432        ((null unmark)                   ; Mark as read.
11433         (while (and
11434                 (progn
11435                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11436                   (gnus-summary-show-thread) t)
11437                 (gnus-summary-find-subject subject))
11438           (setq count (1+ count))))
11439        ((> unmark 0)                    ; Tick.
11440         (while (and
11441                 (progn
11442                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11443                   (gnus-summary-show-thread) t)
11444                 (gnus-summary-find-subject subject))
11445           (setq count (1+ count))))
11446        (t                               ; Mark as unread.
11447         (while (and
11448                 (progn
11449                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11450                   (gnus-summary-show-thread) t)
11451                 (gnus-summary-find-subject subject))
11452           (setq count (1+ count)))))
11453       (gnus-set-mode-line 'summary)
11454       ;; Return the number of marked articles.
11455       count)))
11456
11457 (defun gnus-summary-mark-as-processable (n &optional unmark)
11458   "Set the process mark on the next N articles.
11459 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11460 the process mark instead.  The difference between N and the actual
11461 number of articles marked is returned."
11462   (interactive "p")
11463   (gnus-set-global-variables)
11464   (let ((backward (< n 0))
11465         (n (abs n)))
11466     (while (and
11467             (> n 0)
11468             (if unmark
11469                 (gnus-summary-remove-process-mark
11470                  (gnus-summary-article-number))
11471               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11472             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11473       (setq n (1- n)))
11474     (if (/= 0 n) (gnus-message 7 "No more articles"))
11475     (gnus-summary-recenter)
11476     (gnus-summary-position-point)
11477     n))
11478
11479 (defun gnus-summary-unmark-as-processable (n)
11480   "Remove the process mark from the next N articles.
11481 If N is negative, mark backward instead.  The difference between N and
11482 the actual number of articles marked is returned."
11483   (interactive "p")
11484   (gnus-set-global-variables)
11485   (gnus-summary-mark-as-processable n t))
11486
11487 (defun gnus-summary-unmark-all-processable ()
11488   "Remove the process mark from all articles."
11489   (interactive)
11490   (gnus-set-global-variables)
11491   (save-excursion
11492     (while gnus-newsgroup-processable
11493       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11494   (gnus-summary-position-point))
11495
11496 (defun gnus-summary-mark-as-expirable (n)
11497   "Mark N articles forward as expirable.
11498 If N is negative, mark backward instead.  The difference between N and
11499 the actual number of articles marked is returned."
11500   (interactive "p")
11501   (gnus-set-global-variables)
11502   (gnus-summary-mark-forward n gnus-expirable-mark))
11503
11504 (defun gnus-summary-mark-article-as-replied (article)
11505   "Mark ARTICLE replied and update the summary line."
11506   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11507   (let ((buffer-read-only nil))
11508     (when (gnus-summary-goto-subject article)
11509       (gnus-summary-update-secondary-mark article))))
11510
11511 (defun gnus-summary-set-bookmark (article)
11512   "Set a bookmark in current article."
11513   (interactive (list (gnus-summary-article-number)))
11514   (gnus-set-global-variables)
11515   (if (or (not (get-buffer gnus-article-buffer))
11516           (not gnus-current-article)
11517           (not gnus-article-current)
11518           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11519       (error "No current article selected"))
11520   ;; Remove old bookmark, if one exists.
11521   (let ((old (assq article gnus-newsgroup-bookmarks)))
11522     (if old (setq gnus-newsgroup-bookmarks
11523                   (delq old gnus-newsgroup-bookmarks))))
11524   ;; Set the new bookmark, which is on the form
11525   ;; (article-number . line-number-in-body).
11526   (setq gnus-newsgroup-bookmarks
11527         (cons
11528          (cons article
11529                (save-excursion
11530                  (set-buffer gnus-article-buffer)
11531                  (count-lines
11532                   (min (point)
11533                        (save-excursion
11534                          (goto-char (point-min))
11535                          (search-forward "\n\n" nil t)
11536                          (point)))
11537                   (point))))
11538          gnus-newsgroup-bookmarks))
11539   (gnus-message 6 "A bookmark has been added to the current article."))
11540
11541 (defun gnus-summary-remove-bookmark (article)
11542   "Remove the bookmark from the current article."
11543   (interactive (list (gnus-summary-article-number)))
11544   (gnus-set-global-variables)
11545   ;; Remove old bookmark, if one exists.
11546   (let ((old (assq article gnus-newsgroup-bookmarks)))
11547     (if old
11548         (progn
11549           (setq gnus-newsgroup-bookmarks
11550                 (delq old gnus-newsgroup-bookmarks))
11551           (gnus-message 6 "Removed bookmark."))
11552       (gnus-message 6 "No bookmark in current article."))))
11553
11554 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11555 (defun gnus-summary-mark-as-dormant (n)
11556   "Mark N articles forward as dormant.
11557 If N is negative, mark backward instead.  The difference between N and
11558 the actual number of articles marked is returned."
11559   (interactive "p")
11560   (gnus-set-global-variables)
11561   (gnus-summary-mark-forward n gnus-dormant-mark))
11562
11563 (defun gnus-summary-set-process-mark (article)
11564   "Set the process mark on ARTICLE and update the summary line."
11565   (setq gnus-newsgroup-processable
11566         (cons article
11567               (delq article gnus-newsgroup-processable)))
11568   (when (gnus-summary-goto-subject article)
11569     (gnus-summary-show-thread)
11570     (gnus-summary-update-secondary-mark article)))
11571
11572 (defun gnus-summary-remove-process-mark (article)
11573   "Remove the process mark from ARTICLE and update the summary line."
11574   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11575   (when (gnus-summary-goto-subject article)
11576     (gnus-summary-show-thread)
11577     (gnus-summary-update-secondary-mark article)))
11578
11579 (defun gnus-summary-set-saved-mark (article)
11580   "Set the process mark on ARTICLE and update the summary line."
11581   (push article gnus-newsgroup-saved)
11582   (when (gnus-summary-goto-subject article)
11583     (gnus-summary-update-secondary-mark article)))
11584
11585 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11586   "Mark N articles as read forwards.
11587 If N is negative, mark backwards instead.
11588 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11589 marked as unread.
11590 The difference between N and the actual number of articles marked is
11591 returned."
11592   (interactive "p")
11593   (gnus-set-global-variables)
11594   (let ((backward (< n 0))
11595         (gnus-summary-goto-unread
11596          (and gnus-summary-goto-unread
11597               (not (eq gnus-summary-goto-unread 'never))
11598               (not (memq mark (list gnus-unread-mark
11599                                     gnus-ticked-mark gnus-dormant-mark)))))
11600         (n (abs n))
11601         (mark (or mark gnus-del-mark)))
11602     (while (and (> n 0)
11603                 (gnus-summary-mark-article nil mark no-expire)
11604                 (zerop (gnus-summary-next-subject
11605                         (if backward -1 1)
11606                         (and gnus-summary-goto-unread
11607                              (not (eq gnus-summary-goto-unread 'never)))
11608                         t)))
11609       (setq n (1- n)))
11610     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11611     (gnus-summary-recenter)
11612     (gnus-summary-position-point)
11613     (gnus-set-mode-line 'summary)
11614     n))
11615
11616 (defun gnus-summary-mark-article-as-read (mark)
11617   "Mark the current article quickly as read with MARK."
11618   (let ((article (gnus-summary-article-number)))
11619     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11620     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11621     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11622     (setq gnus-newsgroup-reads
11623           (cons (cons article mark) gnus-newsgroup-reads))
11624     ;; Possibly remove from cache, if that is used.
11625     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11626     ;; Allow the backend to change the mark.
11627     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11628     ;; Check for auto-expiry.
11629     (when (and gnus-newsgroup-auto-expire
11630                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11631                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11632                    (= mark gnus-ancient-mark)
11633                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11634       (setq mark gnus-expirable-mark)
11635       (push article gnus-newsgroup-expirable))
11636     ;; Set the mark in the buffer.
11637     (gnus-summary-update-mark mark 'unread)
11638     t))
11639
11640 (defun gnus-summary-mark-article-as-unread (mark)
11641   "Mark the current article quickly as unread with MARK."
11642   (let ((article (gnus-summary-article-number)))
11643     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11644     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11645     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11646     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11647     (cond ((= mark gnus-ticked-mark)
11648            (push article gnus-newsgroup-marked))
11649           ((= mark gnus-dormant-mark)
11650            (push article gnus-newsgroup-dormant))
11651           (t
11652            (push article gnus-newsgroup-unreads)))
11653     (setq gnus-newsgroup-reads
11654           (delq (assq article gnus-newsgroup-reads)
11655                 gnus-newsgroup-reads))
11656
11657     ;; See whether the article is to be put in the cache.
11658     (and gnus-use-cache
11659          (vectorp (gnus-summary-article-header article))
11660          (save-excursion
11661            (gnus-cache-possibly-enter-article
11662             gnus-newsgroup-name article
11663             (gnus-summary-article-header article)
11664             (= mark gnus-ticked-mark)
11665             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11666
11667     ;; Fix the mark.
11668     (gnus-summary-update-mark mark 'unread)
11669     t))
11670
11671 (defun gnus-summary-mark-article (&optional article mark no-expire)
11672   "Mark ARTICLE with MARK.  MARK can be any character.
11673 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11674 `??' (dormant) and `?E' (expirable).
11675 If MARK is nil, then the default character `?D' is used.
11676 If ARTICLE is nil, then the article on the current line will be
11677 marked."
11678   ;; The mark might be a string.
11679   (and (stringp mark)
11680        (setq mark (aref mark 0)))
11681   ;; If no mark is given, then we check auto-expiring.
11682   (and (not no-expire)
11683        gnus-newsgroup-auto-expire
11684        (or (not mark)
11685            (and (numberp mark)
11686                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11687                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11688                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11689        (setq mark gnus-expirable-mark))
11690   (let* ((mark (or mark gnus-del-mark))
11691          (article (or article (gnus-summary-article-number))))
11692     (or article (error "No article on current line"))
11693     (if (or (= mark gnus-unread-mark)
11694             (= mark gnus-ticked-mark)
11695             (= mark gnus-dormant-mark))
11696         (gnus-mark-article-as-unread article mark)
11697       (gnus-mark-article-as-read article mark))
11698
11699     ;; See whether the article is to be put in the cache.
11700     (and gnus-use-cache
11701          (not (= mark gnus-canceled-mark))
11702          (vectorp (gnus-summary-article-header article))
11703          (save-excursion
11704            (gnus-cache-possibly-enter-article
11705             gnus-newsgroup-name article
11706             (gnus-summary-article-header article)
11707             (= mark gnus-ticked-mark)
11708             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11709
11710     (if (gnus-summary-goto-subject article nil t)
11711         (let ((buffer-read-only nil))
11712           (gnus-summary-show-thread)
11713           ;; Fix the mark.
11714           (gnus-summary-update-mark mark 'unread)
11715           t))))
11716
11717 (defun gnus-summary-update-secondary-mark (article)
11718   "Update the secondary (read, process, cache) mark."
11719   (gnus-summary-update-mark
11720    (cond ((memq article gnus-newsgroup-processable)
11721           gnus-process-mark)
11722          ((memq article gnus-newsgroup-cached)
11723           gnus-cached-mark)
11724          ((memq article gnus-newsgroup-replied)
11725           gnus-replied-mark)
11726          ((memq article gnus-newsgroup-saved)
11727           gnus-saved-mark)
11728          (t gnus-unread-mark))
11729    'replied)
11730   (when (gnus-visual-p 'summary-highlight 'highlight)
11731     (run-hooks 'gnus-summary-update-hook))
11732   t)
11733
11734 (defun gnus-summary-update-mark (mark type)
11735   (beginning-of-line)
11736   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11737         (buffer-read-only nil))
11738     (when forward
11739       ;; Go to the right position on the line.
11740       (forward-char forward)
11741       ;; Replace the old mark with the new mark.
11742       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11743       ;; Optionally update the marks by some user rule.
11744       (when (eq type 'unread)
11745         (gnus-data-set-mark
11746          (gnus-data-find (gnus-summary-article-number)) mark)
11747         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11748
11749 (defun gnus-mark-article-as-read (article &optional mark)
11750   "Enter ARTICLE in the pertinent lists and remove it from others."
11751   ;; Make the article expirable.
11752   (let ((mark (or mark gnus-del-mark)))
11753     (if (= mark gnus-expirable-mark)
11754         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11755       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11756     ;; Remove from unread and marked lists.
11757     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11758     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11759     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11760     (push (cons article mark) gnus-newsgroup-reads)
11761     ;; Possibly remove from cache, if that is used.
11762     (when gnus-use-cache
11763       (gnus-cache-enter-remove-article article))))
11764
11765 (defun gnus-mark-article-as-unread (article &optional mark)
11766   "Enter ARTICLE in the pertinent lists and remove it from others."
11767   (let ((mark (or mark gnus-ticked-mark)))
11768     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11769     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11770     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11771     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11772     (cond ((= mark gnus-ticked-mark)
11773            (push article gnus-newsgroup-marked))
11774           ((= mark gnus-dormant-mark)
11775            (push article gnus-newsgroup-dormant))
11776           (t
11777            (push article gnus-newsgroup-unreads)))
11778     (setq gnus-newsgroup-reads
11779           (delq (assq article gnus-newsgroup-reads)
11780                 gnus-newsgroup-reads))))
11781
11782 (defalias 'gnus-summary-mark-as-unread-forward
11783   'gnus-summary-tick-article-forward)
11784 (make-obsolete 'gnus-summary-mark-as-unread-forward
11785                'gnus-summary-tick-article-forward)
11786 (defun gnus-summary-tick-article-forward (n)
11787   "Tick N articles forwards.
11788 If N is negative, tick backwards instead.
11789 The difference between N and the number of articles ticked is returned."
11790   (interactive "p")
11791   (gnus-summary-mark-forward n gnus-ticked-mark))
11792
11793 (defalias 'gnus-summary-mark-as-unread-backward
11794   'gnus-summary-tick-article-backward)
11795 (make-obsolete 'gnus-summary-mark-as-unread-backward
11796                'gnus-summary-tick-article-backward)
11797 (defun gnus-summary-tick-article-backward (n)
11798   "Tick N articles backwards.
11799 The difference between N and the number of articles ticked is returned."
11800   (interactive "p")
11801   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11802
11803 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11804 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11805 (defun gnus-summary-tick-article (&optional article clear-mark)
11806   "Mark current article as unread.
11807 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11808 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11809   (interactive)
11810   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11811                                        gnus-ticked-mark)))
11812
11813 (defun gnus-summary-mark-as-read-forward (n)
11814   "Mark N articles as read forwards.
11815 If N is negative, mark backwards instead.
11816 The difference between N and the actual number of articles marked is
11817 returned."
11818   (interactive "p")
11819   (gnus-summary-mark-forward n gnus-del-mark t))
11820
11821 (defun gnus-summary-mark-as-read-backward (n)
11822   "Mark the N articles as read backwards.
11823 The difference between N and the actual number of articles marked is
11824 returned."
11825   (interactive "p")
11826   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11827
11828 (defun gnus-summary-mark-as-read (&optional article mark)
11829   "Mark current article as read.
11830 ARTICLE specifies the article to be marked as read.
11831 MARK specifies a string to be inserted at the beginning of the line."
11832   (gnus-summary-mark-article article mark))
11833
11834 (defun gnus-summary-clear-mark-forward (n)
11835   "Clear marks from N articles forward.
11836 If N is negative, clear backward instead.
11837 The difference between N and the number of marks cleared is returned."
11838   (interactive "p")
11839   (gnus-summary-mark-forward n gnus-unread-mark))
11840
11841 (defun gnus-summary-clear-mark-backward (n)
11842   "Clear marks from N articles backward.
11843 The difference between N and the number of marks cleared is returned."
11844   (interactive "p")
11845   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11846
11847 (defun gnus-summary-mark-unread-as-read ()
11848   "Intended to be used by `gnus-summary-mark-article-hook'."
11849   (when (memq gnus-current-article gnus-newsgroup-unreads)
11850     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11851
11852 (defun gnus-summary-mark-region-as-read (point mark all)
11853   "Mark all unread articles between point and mark as read.
11854 If given a prefix, mark all articles between point and mark as read,
11855 even ticked and dormant ones."
11856   (interactive "r\nP")
11857   (save-excursion
11858     (let (article)
11859       (goto-char point)
11860       (beginning-of-line)
11861       (while (and
11862               (< (point) mark)
11863               (progn
11864                 (when (or all
11865                           (memq (setq article (gnus-summary-article-number))
11866                                 gnus-newsgroup-unreads))
11867                   (gnus-summary-mark-article article gnus-del-mark))
11868                 t)
11869               (gnus-summary-find-next))))))
11870
11871 (defun gnus-summary-mark-below (score mark)
11872   "Mark articles with score less than SCORE with MARK."
11873   (interactive "P\ncMark: ")
11874   (gnus-set-global-variables)
11875   (setq score (if score
11876                   (prefix-numeric-value score)
11877                 (or gnus-summary-default-score 0)))
11878   (save-excursion
11879     (set-buffer gnus-summary-buffer)
11880     (goto-char (point-min))
11881     (while 
11882         (progn
11883           (and (< (gnus-summary-article-score) score)
11884                (gnus-summary-mark-article nil mark))
11885           (gnus-summary-find-next)))))
11886
11887 (defun gnus-summary-kill-below (&optional score)
11888   "Mark articles with score below SCORE as read."
11889   (interactive "P")
11890   (gnus-set-global-variables)
11891   (gnus-summary-mark-below score gnus-killed-mark))
11892
11893 (defun gnus-summary-clear-above (&optional score)
11894   "Clear all marks from articles with score above SCORE."
11895   (interactive "P")
11896   (gnus-set-global-variables)
11897   (gnus-summary-mark-above score gnus-unread-mark))
11898
11899 (defun gnus-summary-tick-above (&optional score)
11900   "Tick all articles with score above SCORE."
11901   (interactive "P")
11902   (gnus-set-global-variables)
11903   (gnus-summary-mark-above score gnus-ticked-mark))
11904
11905 (defun gnus-summary-mark-above (score mark)
11906   "Mark articles with score over SCORE with MARK."
11907   (interactive "P\ncMark: ")
11908   (gnus-set-global-variables)
11909   (setq score (if score
11910                   (prefix-numeric-value score)
11911                 (or gnus-summary-default-score 0)))
11912   (save-excursion
11913     (set-buffer gnus-summary-buffer)
11914     (goto-char (point-min))
11915     (while (and (progn
11916                   (if (> (gnus-summary-article-score) score)
11917                       (gnus-summary-mark-article nil mark))
11918                   t)
11919                 (gnus-summary-find-next)))))
11920
11921 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11922 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11923 (defun gnus-summary-limit-include-expunged ()
11924   "Display all the hidden articles that were expunged for low scores."
11925   (interactive)
11926   (gnus-set-global-variables)
11927   (let ((buffer-read-only nil))
11928     (let ((scored gnus-newsgroup-scored)
11929           headers h)
11930       (while scored
11931         (or (gnus-summary-goto-subject (car (car scored)))
11932             (and (setq h (gnus-summary-article-header (car (car scored))))
11933                  (< (cdr (car scored)) gnus-summary-expunge-below)
11934                  (setq headers (cons h headers))))
11935         (setq scored (cdr scored)))
11936       (or headers (error "No expunged articles hidden."))
11937       (goto-char (point-min))
11938       (gnus-summary-prepare-unthreaded (nreverse headers)))
11939     (goto-char (point-min))
11940     (gnus-summary-position-point)))
11941
11942 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
11943   "Mark all articles not marked as unread in this newsgroup as read.
11944 If prefix argument ALL is non-nil, all articles are marked as read.
11945 If QUIETLY is non-nil, no questions will be asked.
11946 If TO-HERE is non-nil, it should be a point in the buffer.  All
11947 articles before this point will be marked as read.
11948 The number of articles marked as read is returned."
11949   (interactive "P")
11950   (gnus-set-global-variables)
11951   (prog1
11952       (if (or quietly
11953               (not gnus-interactive-catchup) ;Without confirmation?
11954               gnus-expert-user
11955               (gnus-y-or-n-p
11956                (if all
11957                    "Mark absolutely all articles as read? "
11958                  "Mark all unread articles as read? ")))
11959           (if (and not-mark
11960                    (not gnus-newsgroup-adaptive)
11961                    (not gnus-newsgroup-auto-expire))
11962               (progn
11963                 (when all
11964                   (setq gnus-newsgroup-marked nil
11965                         gnus-newsgroup-dormant nil))
11966                 (setq gnus-newsgroup-unreads nil))
11967             ;; We actually mark all articles as canceled, which we
11968             ;; have to do when using auto-expiry or adaptive scoring.
11969             (gnus-summary-show-all-threads)
11970             (if (gnus-summary-first-subject (not all))
11971                 (while (and
11972                         (if to-here (< (point) to-here) t)
11973                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11974                         (gnus-summary-find-next (not all)))))
11975             (unless to-here
11976               (setq gnus-newsgroup-unreads nil))
11977             (gnus-set-mode-line 'summary)))
11978     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
11979       (if (and (not to-here) (eq 'nnvirtual (car method)))
11980           (nnvirtual-catchup-group
11981            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
11982     (gnus-summary-position-point)))
11983
11984 (defun gnus-summary-catchup-to-here (&optional all)
11985   "Mark all unticked articles before the current one as read.
11986 If ALL is non-nil, also mark ticked and dormant articles as read."
11987   (interactive "P")
11988   (gnus-set-global-variables)
11989   (save-excursion
11990     (let ((beg (point)))
11991       ;; We check that there are unread articles.
11992       (when (or all (gnus-summary-find-prev))
11993         (gnus-summary-catchup all t beg))))
11994   (gnus-summary-position-point))
11995
11996 (defun gnus-summary-catchup-all (&optional quietly)
11997   "Mark all articles in this newsgroup as read."
11998   (interactive "P")
11999   (gnus-set-global-variables)
12000   (gnus-summary-catchup t quietly))
12001
12002 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12003   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12004 If prefix argument ALL is non-nil, all articles are marked as read."
12005   (interactive "P")
12006   (gnus-set-global-variables)
12007   (gnus-summary-catchup all quietly nil 'fast)
12008   ;; Select next newsgroup or exit.
12009   (if (eq gnus-auto-select-next 'quietly)
12010       (gnus-summary-next-group nil)
12011     (gnus-summary-exit)))
12012
12013 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12014   "Mark all articles in this newsgroup as read, and then exit."
12015   (interactive "P")
12016   (gnus-set-global-variables)
12017   (gnus-summary-catchup-and-exit t quietly))
12018
12019 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12020 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12021   "Mark all articles in this group as read and select the next group.
12022 If given a prefix, mark all articles, unread as well as ticked, as
12023 read."
12024   (interactive "P")
12025   (gnus-set-global-variables)
12026   (save-excursion
12027     (gnus-summary-catchup all))
12028   (gnus-summary-next-article t nil nil t))
12029
12030 ;; Thread-based commands.
12031
12032 (defun gnus-summary-articles-in-thread (&optional article)
12033   "Return a list of all articles in the current thread.
12034 If ARTICLE is non-nil, return all articles in the thread that starts
12035 with that article."
12036   (let* ((article (or article (gnus-summary-article-number)))
12037          (data (gnus-data-find-list article))
12038          (top-level (gnus-data-level (car data)))
12039          (top-subject
12040           (cond ((null gnus-thread-operation-ignore-subject)
12041                  (gnus-simplify-subject-re
12042                   (mail-header-subject (gnus-data-header (car data)))))
12043                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12044                  (gnus-simplify-subject-fuzzy
12045                   (mail-header-subject (gnus-data-header (car data)))))
12046                 (t nil)))
12047          articles)
12048     (if (not data)
12049         ()                              ; This article doesn't exist.
12050       (while data
12051         (and (or (not top-subject)
12052                  (string= top-subject
12053                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12054                               (gnus-simplify-subject-fuzzy
12055                                (mail-header-subject
12056                                 (gnus-data-header (car data))))
12057                             (gnus-simplify-subject-re
12058                              (mail-header-subject
12059                               (gnus-data-header (car data)))))))
12060              (setq articles (cons (gnus-data-number (car data)) articles)))
12061         (if (and (setq data (cdr data))
12062                  (> (gnus-data-level (car data)) top-level))
12063             ()
12064           (setq data nil)))
12065       ;; Return the list of articles.
12066       (nreverse articles))))
12067
12068 (defun gnus-summary-rethread-current ()
12069   "Rethread the thread the current article is part of."
12070   (interactive)
12071   (gnus-set-global-variables)
12072   (let* ((gnus-show-threads t)
12073          (article (gnus-summary-article-number))
12074          (id (mail-header-id (gnus-summary-article-header)))
12075          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12076     (unless id
12077       (error "No article on the current line"))
12078     (gnus-rebuild-thread id)
12079     (gnus-summary-goto-subject article)))
12080
12081 (defun gnus-summary-reparent-thread ()
12082   "Make current article child of the marked (or previous) article.
12083
12084 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12085 is non-nil or the Subject: of both articles are the same."
12086   (interactive)
12087   (or (not (gnus-group-read-only-p))
12088       (error "The current newsgroup does not support article editing."))
12089   (or (<= (length gnus-newsgroup-processable) 1)
12090       (error "No more than one article may be marked."))
12091   (save-window-excursion
12092     (let ((gnus-article-buffer " *reparent*")
12093           (current-article (gnus-summary-article-number))
12094           ; first grab the marked article, otherwise one line up.
12095           (parent-article (if (not (null gnus-newsgroup-processable))
12096                               (car gnus-newsgroup-processable)
12097                             (save-excursion
12098                               (if (eq (forward-line -1) 0)
12099                                   (gnus-summary-article-number)
12100                                 (error "Beginning of summary buffer."))))))
12101       (or (not (eq current-article parent-article))
12102           (error "An article may not be self-referential."))
12103       (let ((message-id (mail-header-id 
12104                          (gnus-summary-article-header parent-article))))
12105         (or (and message-id (not (equal message-id "")))
12106             (error "No message-id in desired parent."))
12107         (gnus-summary-select-article t t nil current-article)
12108         (set-buffer gnus-article-buffer)
12109         (setq buffer-read-only nil)
12110         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12111           (erase-buffer)
12112           (insert buf))
12113         (goto-char (point-min))
12114         (if (search-forward-regexp "^References: " nil t)
12115             (insert message-id " " )
12116           (insert "References: " message-id "\n"))
12117         (or (gnus-request-replace-article current-article
12118                                           (car gnus-article-current)
12119                                           gnus-article-buffer)
12120             (error "Couldn't replace article."))
12121         (set-buffer gnus-summary-buffer)
12122         (gnus-summary-unmark-all-processable)
12123         (gnus-summary-rethread-current)
12124         (message "Article %d is now the child of article %d."
12125                  current-article parent-article)))))
12126
12127 (defun gnus-summary-toggle-threads (&optional arg)
12128   "Toggle showing conversation threads.
12129 If ARG is positive number, turn showing conversation threads on."
12130   (interactive "P")
12131   (gnus-set-global-variables)
12132   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12133     (setq gnus-show-threads
12134           (if (null arg) (not gnus-show-threads)
12135             (> (prefix-numeric-value arg) 0)))
12136     (gnus-summary-prepare)
12137     (gnus-summary-goto-subject current)
12138     (gnus-summary-position-point)))
12139
12140 (defun gnus-summary-show-all-threads ()
12141   "Show all threads."
12142   (interactive)
12143   (gnus-set-global-variables)
12144   (save-excursion
12145     (let ((buffer-read-only nil))
12146       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12147   (gnus-summary-position-point))
12148
12149 (defun gnus-summary-show-thread ()
12150   "Show thread subtrees.
12151 Returns nil if no thread was there to be shown."
12152   (interactive)
12153   (gnus-set-global-variables)
12154   (let ((buffer-read-only nil)
12155         (orig (point))
12156         ;; first goto end then to beg, to have point at beg after let
12157         (end (progn (end-of-line) (point)))
12158         (beg (progn (beginning-of-line) (point))))
12159     (prog1
12160         ;; Any hidden lines here?
12161         (search-forward "\r" end t)
12162       (subst-char-in-region beg end ?\^M ?\n t)
12163       (goto-char orig)
12164       (gnus-summary-position-point))))
12165
12166 (defun gnus-summary-hide-all-threads ()
12167   "Hide all thread subtrees."
12168   (interactive)
12169   (gnus-set-global-variables)
12170   (save-excursion
12171     (goto-char (point-min))
12172     (gnus-summary-hide-thread)
12173     (while (zerop (gnus-summary-next-thread 1 t))
12174       (gnus-summary-hide-thread)))
12175   (gnus-summary-position-point))
12176
12177 (defun gnus-summary-hide-thread ()
12178   "Hide thread subtrees.
12179 Returns nil if no threads were there to be hidden."
12180   (interactive)
12181   (gnus-set-global-variables)
12182   (let ((buffer-read-only nil)
12183         (start (point))
12184         (article (gnus-summary-article-number))
12185         end)
12186     (goto-char start)
12187     ;; Go forward until either the buffer ends or the subthread
12188     ;; ends.
12189     (when (and (not (eobp))
12190                (or (zerop (gnus-summary-next-thread 1 t))
12191                    (goto-char (point-max))))
12192       (setq end (point))
12193       (prog1
12194           (if (and (> (point) start)
12195                    (search-backward "\n" start t))
12196               (progn
12197                 (subst-char-in-region start (point) ?\n ?\^M)
12198                 (gnus-summary-goto-subject article))
12199             (goto-char start)
12200             nil)
12201         (gnus-summary-position-point)))))
12202
12203 (defun gnus-summary-go-to-next-thread (&optional previous)
12204   "Go to the same level (or less) next thread.
12205 If PREVIOUS is non-nil, go to previous thread instead.
12206 Return the article number moved to, or nil if moving was impossible."
12207   (let* ((level (gnus-summary-thread-level))
12208          (article (gnus-summary-article-number))
12209          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12210          oart)
12211     (while data
12212       (if (<= (gnus-data-level (car data)) level)
12213           (setq oart (gnus-data-number (car data))
12214                 data nil)
12215         (setq data (cdr data))))
12216     (and oart
12217          (gnus-summary-goto-subject oart))))
12218
12219 (defun gnus-summary-next-thread (n &optional silent)
12220   "Go to the same level next N'th thread.
12221 If N is negative, search backward instead.
12222 Returns the difference between N and the number of skips actually
12223 done.
12224
12225 If SILENT, don't output messages."
12226   (interactive "p")
12227   (gnus-set-global-variables)
12228   (let ((backward (< n 0))
12229         (n (abs n))
12230         old dum)
12231     (while (and (> n 0)
12232                 (setq old (save-excursion (forward-line 1) (point)))
12233                 (gnus-summary-go-to-next-thread backward))
12234       (when (and (eq gnus-summary-make-false-root 'dummy)
12235                  (setq dum (text-property-not-all
12236                             old (point) 'gnus-intangible nil)))
12237         (goto-char dum))
12238       (decf n))
12239     (gnus-summary-position-point)
12240     (when (and (not silent) (/= 0 n))
12241       (gnus-message 7 "No more threads"))
12242     n))
12243
12244 (defun gnus-summary-prev-thread (n)
12245   "Go to the same level previous N'th thread.
12246 Returns the difference between N and the number of skips actually
12247 done."
12248   (interactive "p")
12249   (gnus-set-global-variables)
12250   (gnus-summary-next-thread (- n)))
12251
12252 (defun gnus-summary-go-down-thread ()
12253   "Go down one level in the current thread."
12254   (let ((children (gnus-summary-article-children)))
12255     (and children
12256          (gnus-summary-goto-subject (car children)))))
12257
12258 (defun gnus-summary-go-up-thread ()
12259   "Go up one level in the current thread."
12260   (let ((parent (gnus-summary-article-parent)))
12261     (and parent
12262          (gnus-summary-goto-subject parent))))
12263
12264 (defun gnus-summary-down-thread (n)
12265   "Go down thread N steps.
12266 If N is negative, go up instead.
12267 Returns the difference between N and how many steps down that were
12268 taken."
12269   (interactive "p")
12270   (gnus-set-global-variables)
12271   (let ((up (< n 0))
12272         (n (abs n)))
12273     (while (and (> n 0)
12274                 (if up (gnus-summary-go-up-thread)
12275                   (gnus-summary-go-down-thread)))
12276       (setq n (1- n)))
12277     (gnus-summary-position-point)
12278     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12279     n))
12280
12281 (defun gnus-summary-up-thread (n)
12282   "Go up thread N steps.
12283 If N is negative, go up instead.
12284 Returns the difference between N and how many steps down that were
12285 taken."
12286   (interactive "p")
12287   (gnus-set-global-variables)
12288   (gnus-summary-down-thread (- n)))
12289
12290 (defun gnus-summary-top-thread ()
12291   "Go to the top of the thread."
12292   (interactive)
12293   (gnus-set-global-variables)
12294   (while (gnus-summary-go-up-thread))
12295   (gnus-summary-article-number))
12296
12297 (defun gnus-summary-kill-thread (&optional unmark)
12298   "Mark articles under current thread as read.
12299 If the prefix argument is positive, remove any kinds of marks.
12300 If the prefix argument is negative, tick articles instead."
12301   (interactive "P")
12302   (gnus-set-global-variables)
12303   (if unmark
12304       (setq unmark (prefix-numeric-value unmark)))
12305   (let ((articles (gnus-summary-articles-in-thread)))
12306     (save-excursion
12307       ;; Expand the thread.
12308       (gnus-summary-show-thread)
12309       ;; Mark all the articles.
12310       (while articles
12311         (gnus-summary-goto-subject (car articles))
12312         (cond ((null unmark)
12313                (gnus-summary-mark-article-as-read gnus-killed-mark))
12314               ((> unmark 0)
12315                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12316               (t
12317                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12318         (setq articles (cdr articles))))
12319     ;; Hide killed subtrees.
12320     (and (null unmark)
12321          gnus-thread-hide-killed
12322          (gnus-summary-hide-thread))
12323     ;; If marked as read, go to next unread subject.
12324     (if (null unmark)
12325         ;; Go to next unread subject.
12326         (gnus-summary-next-subject 1 t)))
12327   (gnus-set-mode-line 'summary))
12328
12329 ;; Summary sorting commands
12330
12331 (defun gnus-summary-sort-by-number (&optional reverse)
12332   "Sort summary buffer by article number.
12333 Argument REVERSE means reverse order."
12334   (interactive "P")
12335   (gnus-summary-sort 'number reverse))
12336
12337 (defun gnus-summary-sort-by-author (&optional reverse)
12338   "Sort summary buffer by author name alphabetically.
12339 If case-fold-search is non-nil, case of letters is ignored.
12340 Argument REVERSE means reverse order."
12341   (interactive "P")
12342   (gnus-summary-sort 'author reverse))
12343
12344 (defun gnus-summary-sort-by-subject (&optional reverse)
12345   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12346 If case-fold-search is non-nil, case of letters is ignored.
12347 Argument REVERSE means reverse order."
12348   (interactive "P")
12349   (gnus-summary-sort 'subject reverse))
12350
12351 (defun gnus-summary-sort-by-date (&optional reverse)
12352   "Sort summary buffer by date.
12353 Argument REVERSE means reverse order."
12354   (interactive "P")
12355   (gnus-summary-sort 'date reverse))
12356
12357 (defun gnus-summary-sort-by-score (&optional reverse)
12358   "Sort summary buffer by score.
12359 Argument REVERSE means reverse order."
12360   (interactive "P")
12361   (gnus-summary-sort 'score reverse))
12362
12363 (defun gnus-summary-sort (predicate reverse)
12364   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12365   (gnus-set-global-variables)
12366   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12367          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12368          (gnus-thread-sort-functions
12369           (list
12370            (if (not reverse)
12371                thread
12372              `(lambda (t1 t2)
12373                 (,thread t2 t1)))))
12374          (gnus-article-sort-functions
12375           (list
12376            (if (not reverse)
12377                article
12378              `(lambda (t1 t2)
12379                 (,article t2 t1)))))
12380          (buffer-read-only)
12381          (gnus-summary-prepare-hook nil))
12382     ;; We do the sorting by regenerating the threads.
12383     (gnus-summary-prepare)
12384     ;; Hide subthreads if needed.
12385     (when (and gnus-show-threads gnus-thread-hide-subtree)
12386       (gnus-summary-hide-all-threads)))
12387   ;; If in async mode, we send some info to the backend.
12388   (when gnus-newsgroup-async
12389     (gnus-request-asynchronous
12390      gnus-newsgroup-name gnus-newsgroup-data)))
12391
12392 (defun gnus-sortable-date (date)
12393   "Make sortable string by string-lessp from DATE.
12394 Timezone package is used."
12395   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12396          (year (aref date 0))
12397          (month (aref date 1))
12398          (day (aref date 2)))
12399     (timezone-make-sortable-date
12400      year month day
12401      (timezone-make-time-string
12402       (aref date 3) (aref date 4) (aref date 5)))))
12403
12404 ;; Summary saving commands.
12405
12406 (defun gnus-summary-save-article (&optional n not-saved)
12407   "Save the current article using the default saver function.
12408 If N is a positive number, save the N next articles.
12409 If N is a negative number, save the N previous articles.
12410 If N is nil and any articles have been marked with the process mark,
12411 save those articles instead.
12412 The variable `gnus-default-article-saver' specifies the saver function."
12413   (interactive "P")
12414   (gnus-set-global-variables)
12415   (let ((articles (gnus-summary-work-articles n))
12416         file header article)
12417     (while articles
12418       (setq header (gnus-summary-article-header
12419                     (setq article (pop articles))))
12420       (if (not (vectorp header))
12421           ;; This is a pseudo-article.
12422           (if (assq 'name header)
12423               (gnus-copy-file (cdr (assq 'name header)))
12424             (gnus-message 1 "Article %d is unsaveable" article))
12425         ;; This is a real article.
12426         (save-window-excursion
12427           (gnus-summary-select-article t nil nil article))
12428         (unless gnus-save-all-headers
12429           ;; Remove headers accoring to `gnus-saved-headers'.
12430           (let ((gnus-visible-headers
12431                  (or gnus-saved-headers gnus-visible-headers)))
12432             (gnus-article-hide-headers nil t)))
12433         ;; Remove any X-Gnus lines.
12434         (save-excursion
12435           (set-buffer gnus-article-buffer)
12436           (save-restriction
12437             (let ((buffer-read-only nil))
12438               (nnheader-narrow-to-headers)
12439               (while (re-search-forward "^X-Gnus" nil t)
12440                 (gnus-delete-line)))))
12441         (save-window-excursion
12442           (if (not gnus-default-article-saver)
12443               (error "No default saver is defined.")
12444             (setq file (funcall
12445                         gnus-default-article-saver
12446                         (cond
12447                          ((not gnus-prompt-before-saving)
12448                           'default)
12449                          ((eq gnus-prompt-before-saving 'always)
12450                           nil)
12451                          (t file))))))
12452         (gnus-summary-remove-process-mark article)
12453         (unless not-saved
12454           (gnus-summary-set-saved-mark article))))
12455     (gnus-summary-position-point)
12456     n))
12457
12458 (defun gnus-summary-pipe-output (&optional arg)
12459   "Pipe the current article to a subprocess.
12460 If N is a positive number, pipe the N next articles.
12461 If N is a negative number, pipe the N previous articles.
12462 If N is nil and any articles have been marked with the process mark,
12463 pipe those articles instead."
12464   (interactive "P")
12465   (gnus-set-global-variables)
12466   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12467     (gnus-summary-save-article arg t))
12468   (gnus-configure-windows 'pipe))
12469
12470 (defun gnus-summary-save-article-mail (&optional arg)
12471   "Append the current article to an mail file.
12472 If N is a positive number, save the N next articles.
12473 If N is a negative number, save the N previous articles.
12474 If N is nil and any articles have been marked with the process mark,
12475 save those articles instead."
12476   (interactive "P")
12477   (gnus-set-global-variables)
12478   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12479     (gnus-summary-save-article arg)))
12480
12481 (defun gnus-summary-save-article-rmail (&optional arg)
12482   "Append the current article to an rmail file.
12483 If N is a positive number, save the N next articles.
12484 If N is a negative number, save the N previous articles.
12485 If N is nil and any articles have been marked with the process mark,
12486 save those articles instead."
12487   (interactive "P")
12488   (gnus-set-global-variables)
12489   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12490     (gnus-summary-save-article arg)))
12491
12492 (defun gnus-summary-save-article-file (&optional arg)
12493   "Append the current article to a file.
12494 If N is a positive number, save the N next articles.
12495 If N is a negative number, save the N previous articles.
12496 If N is nil and any articles have been marked with the process mark,
12497 save those articles instead."
12498   (interactive "P")
12499   (gnus-set-global-variables)
12500   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12501     (gnus-summary-save-article arg)))
12502
12503 (defun gnus-summary-save-article-body-file (&optional arg)
12504   "Append the current article body to a file.
12505 If N is a positive number, save the N next articles.
12506 If N is a negative number, save the N previous articles.
12507 If N is nil and any articles have been marked with the process mark,
12508 save those articles instead."
12509   (interactive "P")
12510   (gnus-set-global-variables)
12511   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12512     (gnus-summary-save-article arg)))
12513
12514 (defun gnus-get-split-value (methods)
12515   "Return a value based on the split METHODS."
12516   (let (split-name method result match)
12517     (when methods
12518       (save-excursion
12519         (set-buffer gnus-original-article-buffer)
12520         (save-restriction
12521           (nnheader-narrow-to-headers)
12522           (while methods
12523             (goto-char (point-min))
12524             (setq method (pop methods))
12525             (setq match (pop method))
12526             (when (cond
12527                    ((stringp match)
12528                     ;; Regular expression.
12529                     (condition-case ()
12530                         (re-search-forward match nil t)
12531                       (error nil)))
12532                    ((gnus-functionp match)
12533                     ;; Function.
12534                     (save-restriction
12535                       (widen)
12536                       (setq result (funcall match gnus-newsgroup-name))))
12537                    ((consp match)
12538                     ;; Form.
12539                     (save-restriction
12540                       (widen)
12541                       (setq result (eval match)))))
12542               (setq split-name (append (cdr methods) split-name))
12543               (cond ((stringp result)
12544                      (push result split-name))
12545                     ((consp result)
12546                      (setq split-name (append result split-name)))))))))
12547     split-name))
12548
12549 (defun gnus-read-move-group-name (prompt default articles prefix)
12550   "Read a group name."
12551   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12552          (prom
12553           (format "Where do you want to %s %s? "
12554                   prompt
12555                   (if (> (length articles) 1)
12556                       (format "these %d articles" (length articles))
12557                     "this article")))
12558          (to-newsgroup
12559           (cond
12560            ((null split-name)
12561             (completing-read
12562              (concat prom
12563                      (if default
12564                          (format "(default %s) " default)
12565                        ""))
12566              gnus-active-hashtb nil nil prefix))
12567            ((= 1 (length split-name))
12568             (completing-read prom gnus-active-hashtb
12569                              nil nil (cons (car split-name) 0)))
12570            (t
12571             (completing-read
12572              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12573
12574     (when to-newsgroup
12575       (if (or (string= to-newsgroup "")
12576               (string= to-newsgroup prefix))
12577           (setq to-newsgroup (or default "")))
12578       (or (gnus-active to-newsgroup)
12579           (gnus-activate-group to-newsgroup)
12580           (error "No such group: %s" to-newsgroup)))
12581     to-newsgroup))
12582
12583 (defun gnus-read-save-file-name (prompt default-name)
12584   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12585          (file
12586           ;; Let the split methods have their say.
12587           (cond
12588            ;; No split name was found.
12589            ((null split-name)
12590             (read-file-name
12591              (concat prompt " (default "
12592                      (file-name-nondirectory default-name) ") ")
12593              (file-name-directory default-name)
12594              default-name))
12595            ;; A single split name was found
12596            ((= 1 (length split-name))
12597             (read-file-name
12598              (concat prompt " (default " (car split-name) ") ")
12599              gnus-article-save-directory
12600              (concat gnus-article-save-directory (car split-name))))
12601            ;; A list of splits was found.
12602            (t
12603             (setq split-name (mapcar (lambda (el) (list el))
12604                                      (nreverse split-name)))
12605             (let ((result (completing-read
12606                            (concat prompt " ") split-name nil nil)))
12607               (concat gnus-article-save-directory
12608                       (if (string= result "")
12609                           (car (car split-name))
12610                         result)))))))
12611     ;; If we have read a directory, we append the default file name.
12612     (when (file-directory-p file)
12613       (setq file (concat (file-name-as-directory file)
12614                          (file-name-nondirectory default-name))))
12615     ;; Possibly translate some charaters.
12616     (nnheader-translate-file-chars file)))
12617
12618 (defun gnus-article-archive-name (group)
12619   "Return the first instance of an \"Archive-name\" in the current buffer."
12620   (let ((case-fold-search t))
12621     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12622       (match-string 1))))
12623
12624 (defun gnus-summary-save-in-rmail (&optional filename)
12625   "Append this article to Rmail file.
12626 Optional argument FILENAME specifies file name.
12627 Directory to save to is default to `gnus-article-save-directory' which
12628 is initialized from the SAVEDIR environment variable."
12629   (interactive)
12630   (gnus-set-global-variables)
12631   (let ((default-name
12632           (funcall gnus-rmail-save-name gnus-newsgroup-name
12633                    gnus-current-headers gnus-newsgroup-last-rmail)))
12634     (setq filename
12635           (cond ((eq filename 'default)
12636                  default-name)
12637                 (filename filename)
12638                 (t (gnus-read-save-file-name
12639                     "Save in rmail file:" default-name))))
12640     (gnus-make-directory (file-name-directory filename))
12641     (gnus-eval-in-buffer-window
12642      gnus-original-article-buffer
12643      (save-excursion
12644        (save-restriction
12645          (widen)
12646          (gnus-output-to-rmail filename))))
12647     ;; Remember the directory name to save articles
12648     (setq gnus-newsgroup-last-rmail filename)))
12649
12650 (defun gnus-summary-save-in-mail (&optional filename)
12651   "Append this article to Unix mail file.
12652 Optional argument FILENAME specifies file name.
12653 Directory to save to is default to `gnus-article-save-directory' which
12654 is initialized from the SAVEDIR environment variable."
12655   (interactive)
12656   (gnus-set-global-variables)
12657   (let ((default-name
12658           (funcall gnus-mail-save-name gnus-newsgroup-name
12659                    gnus-current-headers gnus-newsgroup-last-mail)))
12660     (setq filename
12661           (cond ((eq filename 'default)
12662                  default-name)
12663                 (filename filename)
12664                 (t (gnus-read-save-file-name
12665                     "Save in Unix mail file:" default-name))))
12666     (setq filename
12667           (expand-file-name filename
12668                             (and default-name
12669                                  (file-name-directory default-name))))
12670     (gnus-make-directory (file-name-directory filename))
12671     (gnus-eval-in-buffer-window
12672      gnus-original-article-buffer
12673      (save-excursion
12674        (save-restriction
12675          (widen)
12676          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12677              (gnus-output-to-rmail filename)
12678            (let ((mail-use-rfc822 t))
12679              (rmail-output filename 1 t t))))))
12680     ;; Remember the directory name to save articles.
12681     (setq gnus-newsgroup-last-mail filename)))
12682
12683 (defun gnus-summary-save-in-file (&optional filename)
12684   "Append this article to file.
12685 Optional argument FILENAME specifies file name.
12686 Directory to save to is default to `gnus-article-save-directory' which
12687 is initialized from the SAVEDIR environment variable."
12688   (interactive)
12689   (gnus-set-global-variables)
12690   (let ((default-name
12691           (funcall gnus-file-save-name gnus-newsgroup-name
12692                    gnus-current-headers gnus-newsgroup-last-file)))
12693     (setq filename
12694           (cond ((eq filename 'default)
12695                  default-name)
12696                 (filename filename)
12697                 (t (gnus-read-save-file-name
12698                     "Save in file:" default-name))))
12699     (gnus-make-directory (file-name-directory filename))
12700     (gnus-eval-in-buffer-window
12701      gnus-original-article-buffer
12702      (save-excursion
12703        (save-restriction
12704          (widen)
12705          (gnus-output-to-file filename))))
12706     ;; Remember the directory name to save articles.
12707     (setq gnus-newsgroup-last-file filename)))
12708
12709 (defun gnus-summary-save-body-in-file (&optional filename)
12710   "Append this article body to a file.
12711 Optional argument FILENAME specifies file name.
12712 The directory to save in defaults to `gnus-article-save-directory' which
12713 is initialized from the SAVEDIR environment variable."
12714   (interactive)
12715   (gnus-set-global-variables)
12716   (let ((default-name
12717           (funcall gnus-file-save-name gnus-newsgroup-name
12718                    gnus-current-headers gnus-newsgroup-last-file)))
12719     (setq filename
12720           (cond ((eq filename 'default)
12721                  default-name)
12722                 (filename filename)
12723                 (t (gnus-read-save-file-name
12724                     "Save body in file:" default-name))))
12725     (gnus-make-directory (file-name-directory filename))
12726     (gnus-eval-in-buffer-window
12727      gnus-article-buffer
12728      (save-excursion
12729        (save-restriction
12730          (widen)
12731          (goto-char (point-min))
12732          (and (search-forward "\n\n" nil t)
12733               (narrow-to-region (point) (point-max)))
12734          (gnus-output-to-file filename))))
12735     ;; Remember the directory name to save articles.
12736     (setq gnus-newsgroup-last-file filename)))
12737
12738 (defun gnus-summary-save-in-pipe (&optional command)
12739   "Pipe this article to subprocess."
12740   (interactive)
12741   (gnus-set-global-variables)
12742   (setq command
12743         (cond ((eq command 'default)
12744                gnus-last-shell-command)
12745               (command command)
12746               (t (read-string "Shell command on article: "
12747                               gnus-last-shell-command))))
12748   (if (string-equal command "")
12749       (setq command gnus-last-shell-command))
12750   (gnus-eval-in-buffer-window
12751    gnus-article-buffer
12752    (save-restriction
12753      (widen)
12754      (shell-command-on-region (point-min) (point-max) command nil)))
12755   (setq gnus-last-shell-command command))
12756
12757 ;; Summary extract commands
12758
12759 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12760   (let ((buffer-read-only nil)
12761         (article (gnus-summary-article-number))
12762         after-article b e)
12763     (or (gnus-summary-goto-subject article)
12764         (error (format "No such article: %d" article)))
12765     (gnus-summary-position-point)
12766     ;; If all commands are to be bunched up on one line, we collect
12767     ;; them here.
12768     (if gnus-view-pseudos-separately
12769         ()
12770       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12771             files action)
12772         (while ps
12773           (setq action (cdr (assq 'action (car ps))))
12774           (setq files (list (cdr (assq 'name (car ps)))))
12775           (while (and ps (cdr ps)
12776                       (string= (or action "1")
12777                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12778             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12779             (setcdr ps (cdr (cdr ps))))
12780           (if (not files)
12781               ()
12782             (if (not (string-match "%s" action))
12783                 (setq files (cons " " files)))
12784             (setq files (cons " " files))
12785             (and (assq 'execute (car ps))
12786                  (setcdr (assq 'execute (car ps))
12787                          (funcall (if (string-match "%s" action)
12788                                       'format 'concat)
12789                                   action
12790                                   (mapconcat (lambda (f) f) files " ")))))
12791           (setq ps (cdr ps)))))
12792     (if (and gnus-view-pseudos (not not-view))
12793         (while pslist
12794           (and (assq 'execute (car pslist))
12795                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12796                                      (eq gnus-view-pseudos 'not-confirm)))
12797           (setq pslist (cdr pslist)))
12798       (save-excursion
12799         (while pslist
12800           (setq after-article (or (cdr (assq 'article (car pslist)))
12801                                   (gnus-summary-article-number)))
12802           (gnus-summary-goto-subject after-article)
12803           (forward-line 1)
12804           (setq b (point))
12805           (insert "          " (file-name-nondirectory
12806                                 (cdr (assq 'name (car pslist))))
12807                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12808           (setq e (point))
12809           (forward-line -1)             ; back to `b'
12810           (add-text-properties
12811            b e (list 'gnus-number gnus-reffed-article-number
12812                      gnus-mouse-face-prop gnus-mouse-face))
12813           (gnus-data-enter
12814            after-article gnus-reffed-article-number
12815            gnus-unread-mark b (car pslist) 0 (- e b))
12816           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12817           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12818           (setq pslist (cdr pslist)))))))
12819
12820 (defun gnus-pseudos< (p1 p2)
12821   (let ((c1 (cdr (assq 'action p1)))
12822         (c2 (cdr (assq 'action p2))))
12823     (and c1 c2 (string< c1 c2))))
12824
12825 (defun gnus-request-pseudo-article (props)
12826   (cond ((assq 'execute props)
12827          (gnus-execute-command (cdr (assq 'execute props)))))
12828   (let ((gnus-current-article (gnus-summary-article-number)))
12829     (run-hooks 'gnus-mark-article-hook)))
12830
12831 (defun gnus-execute-command (command &optional automatic)
12832   (save-excursion
12833     (gnus-article-setup-buffer)
12834     (set-buffer gnus-article-buffer)
12835     (let ((command (if automatic command (read-string "Command: " command)))
12836           (buffer-read-only nil))
12837       (erase-buffer)
12838       (insert "$ " command "\n\n")
12839       (if gnus-view-pseudo-asynchronously
12840           (start-process "gnus-execute" nil "sh" "-c" command)
12841         (call-process "sh" nil t nil "-c" command)))))
12842
12843 (defun gnus-copy-file (file &optional to)
12844   "Copy FILE to TO."
12845   (interactive
12846    (list (read-file-name "Copy file: " default-directory)
12847          (read-file-name "Copy file to: " default-directory)))
12848   (gnus-set-global-variables)
12849   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12850   (and (file-directory-p to)
12851        (setq to (concat (file-name-as-directory to)
12852                         (file-name-nondirectory file))))
12853   (copy-file file to))
12854
12855 ;; Summary kill commands.
12856
12857 (defun gnus-summary-edit-global-kill (article)
12858   "Edit the \"global\" kill file."
12859   (interactive (list (gnus-summary-article-number)))
12860   (gnus-set-global-variables)
12861   (gnus-group-edit-global-kill article))
12862
12863 (defun gnus-summary-edit-local-kill ()
12864   "Edit a local kill file applied to the current newsgroup."
12865   (interactive)
12866   (gnus-set-global-variables)
12867   (setq gnus-current-headers (gnus-summary-article-header))
12868   (gnus-set-global-variables)
12869   (gnus-group-edit-local-kill
12870    (gnus-summary-article-number) gnus-newsgroup-name))
12871
12872 \f
12873 ;;;
12874 ;;; Gnus article mode
12875 ;;;
12876
12877 (put 'gnus-article-mode 'mode-class 'special)
12878
12879 (if gnus-article-mode-map
12880     nil
12881   (setq gnus-article-mode-map (make-keymap))
12882   (suppress-keymap gnus-article-mode-map)
12883
12884   (gnus-define-keys
12885    gnus-article-mode-map
12886    " " gnus-article-goto-next-page
12887    "\177" gnus-article-goto-prev-page
12888    [delete] gnus-article-goto-prev-page
12889    "\C-c^" gnus-article-refer-article
12890    "h" gnus-article-show-summary
12891    "s" gnus-article-show-summary
12892    "\C-c\C-m" gnus-article-mail
12893    "?" gnus-article-describe-briefly
12894    gnus-mouse-2 gnus-article-push-button
12895    "\r" gnus-article-press-button
12896    "\t" gnus-article-next-button
12897    "\M-\t" gnus-article-prev-button
12898    "\C-c\C-b" gnus-bug)
12899
12900   (substitute-key-definition
12901    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
12902
12903 (defun gnus-article-mode ()
12904   "Major mode for displaying an article.
12905
12906 All normal editing commands are switched off.
12907
12908 The following commands are available:
12909
12910 \\<gnus-article-mode-map>
12911 \\[gnus-article-next-page]\t Scroll the article one page forwards
12912 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12913 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12914 \\[gnus-article-show-summary]\t Display the summary buffer
12915 \\[gnus-article-mail]\t Send a reply to the address near point
12916 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12917 \\[gnus-info-find-node]\t Go to the Gnus info node"
12918   (interactive)
12919   (when (and menu-bar-mode
12920              (gnus-visual-p 'article-menu 'menu))
12921     (gnus-article-make-menu-bar))
12922   (kill-all-local-variables)
12923   (gnus-simplify-mode-line)
12924   (setq mode-name "Article")
12925   (setq major-mode 'gnus-article-mode)
12926   (make-local-variable 'minor-mode-alist)
12927   (or (assq 'gnus-show-mime minor-mode-alist)
12928       (setq minor-mode-alist
12929             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
12930   (use-local-map gnus-article-mode-map)
12931   (make-local-variable 'page-delimiter)
12932   (setq page-delimiter gnus-page-delimiter)
12933   (buffer-disable-undo (current-buffer))
12934   (setq buffer-read-only t)             ;Disable modification
12935   (run-hooks 'gnus-article-mode-hook))
12936
12937 (defun gnus-article-setup-buffer ()
12938   "Initialize the article buffer."
12939   (let* ((name (if gnus-single-article-buffer "*Article*"
12940                  (concat "*Article " gnus-newsgroup-name "*")))
12941          (original
12942           (progn (string-match "\\*Article" name)
12943                  (concat " *Original Article"
12944                          (substring name (match-end 0))))))
12945     (setq gnus-article-buffer name)
12946     (setq gnus-original-article-buffer original)
12947     ;; This might be a variable local to the summary buffer.
12948     (unless gnus-single-article-buffer
12949       (save-excursion
12950         (set-buffer gnus-summary-buffer)
12951         (setq gnus-article-buffer name)
12952         (setq gnus-original-article-buffer original)
12953         (gnus-set-global-variables))
12954       (make-local-variable 'gnus-summary-buffer))
12955     (if (get-buffer name)
12956         (save-excursion
12957           (set-buffer name)
12958           (buffer-disable-undo (current-buffer))
12959           (setq buffer-read-only t)
12960           (gnus-add-current-to-buffer-list)
12961           (or (eq major-mode 'gnus-article-mode)
12962               (gnus-article-mode))
12963           (current-buffer))
12964       (save-excursion
12965         (set-buffer (get-buffer-create name))
12966         (gnus-add-current-to-buffer-list)
12967         (gnus-article-mode)
12968         (current-buffer)))))
12969
12970 ;; Set article window start at LINE, where LINE is the number of lines
12971 ;; from the head of the article.
12972 (defun gnus-article-set-window-start (&optional line)
12973   (set-window-start
12974    (get-buffer-window gnus-article-buffer)
12975    (save-excursion
12976      (set-buffer gnus-article-buffer)
12977      (goto-char (point-min))
12978      (if (not line)
12979          (point-min)
12980        (gnus-message 6 "Moved to bookmark")
12981        (search-forward "\n\n" nil t)
12982        (forward-line line)
12983        (point)))))
12984
12985 (defun gnus-kill-all-overlays ()
12986   "Delete all overlays in the current buffer."
12987   (when (fboundp 'overlay-lists)
12988     (let* ((overlayss (overlay-lists))
12989            (buffer-read-only nil)
12990            (overlays (nconc (car overlayss) (cdr overlayss))))
12991       (while overlays
12992         (delete-overlay (pop overlays))))))
12993
12994 (defun gnus-request-article-this-buffer (article group)
12995   "Get an article and insert it into this buffer."
12996   (prog1
12997       (save-excursion
12998         (erase-buffer)
12999         (gnus-kill-all-overlays)
13000         (setq group (or group gnus-newsgroup-name))
13001
13002         ;; Open server if it has closed.
13003         (gnus-check-server (gnus-find-method-for-group group))
13004
13005         ;; Using `gnus-request-article' directly will insert the article into
13006         ;; `nntp-server-buffer' - so we'll save some time by not having to
13007         ;; copy it from the server buffer into the article buffer.
13008
13009         ;; We only request an article by message-id when we do not have the
13010         ;; headers for it, so we'll have to get those.
13011         (when (stringp article)
13012           (let ((gnus-override-method gnus-refer-article-method))
13013             (gnus-read-header article)))
13014
13015         ;; If the article number is negative, that means that this article
13016         ;; doesn't belong in this newsgroup (possibly), so we find its
13017         ;; message-id and request it by id instead of number.
13018         (when (numberp article)
13019           (save-excursion
13020             (set-buffer gnus-summary-buffer)
13021             (let ((header (gnus-summary-article-header article)))
13022               (if (< article 0)
13023                   (cond 
13024                    ((memq article gnus-newsgroup-sparse)
13025                     ;; This is a sparse gap article.
13026                     (setq article (mail-header-id header)))
13027                    ((vectorp header)
13028                     ;; It's a real article.
13029                     (setq article (mail-header-id header)))
13030                    (t
13031                     ;; It is an extracted pseudo-article.
13032                     (setq article 'pseudo)
13033                     (gnus-request-pseudo-article header))))
13034                 
13035               (let ((method (gnus-find-method-for-group 
13036                              gnus-newsgroup-name)))
13037                 (if (not (eq (car method) 'nneething))
13038                     ()
13039                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13040                                      (mail-header-subject header))))
13041                     (if (file-directory-p dir)
13042                         (progn
13043                           (setq article 'nneething)
13044                           (gnus-group-enter-directory dir)))))))))
13045
13046         (cond
13047          ;; We first check `gnus-original-article-buffer'.
13048          ((and (equal (car gnus-original-article) group)
13049                (eq (cdr gnus-original-article) article)
13050                (get-buffer gnus-original-article-buffer))
13051           (insert-buffer-substring gnus-original-article-buffer)
13052           'article)
13053          ;; Check the backlog.
13054          ((and gnus-keep-backlog
13055                (gnus-backlog-request-article group article (current-buffer)))
13056           'article)
13057          ;; Check the cache.
13058          ((and gnus-use-cache
13059                (numberp article)
13060                (gnus-cache-request-article article group))
13061           'article)
13062          ;; Get the article and put into the article buffer.
13063          ((or (stringp article) (numberp article))
13064           (let ((gnus-override-method
13065                  (and (stringp article) gnus-refer-article-method))
13066                 (buffer-read-only nil))
13067             (erase-buffer)
13068             (gnus-kill-all-overlays)
13069             (if (gnus-request-article article group (current-buffer))
13070                 (progn
13071                   (and gnus-keep-backlog
13072                        (gnus-backlog-enter-article
13073                         group article (current-buffer)))
13074                   'article))))
13075          ;; It was a pseudo.
13076          (t article)))
13077
13078     ;; Take the article from the original article buffer
13079     ;; and place it in the buffer it's supposed to be in.
13080     (setq gnus-original-article (cons group article))
13081     (when (and (get-buffer gnus-article-buffer)
13082                (equal (buffer-name (current-buffer))
13083                       (buffer-name (get-buffer gnus-article-buffer))))
13084       (save-excursion
13085         (if (get-buffer gnus-original-article-buffer)
13086             (set-buffer (get-buffer gnus-original-article-buffer))
13087           (set-buffer (get-buffer-create gnus-original-article-buffer))
13088           (buffer-disable-undo (current-buffer))
13089           (setq major-mode 'gnus-original-article-mode)
13090           (setq buffer-read-only t)
13091           (gnus-add-current-to-buffer-list))
13092         (let (buffer-read-only)
13093           (erase-buffer)
13094           (insert-buffer-substring gnus-article-buffer))))
13095     
13096     ;; Update sparse articles.
13097     (when (memq article gnus-newsgroup-sparse)
13098       (gnus-summary-update-article article))))
13099
13100 (defun gnus-read-header (id)
13101   "Read the headers of article ID and enter them into the Gnus system."
13102   (let ((group gnus-newsgroup-name)
13103         (headers gnus-newsgroup-headers)
13104         header where)
13105     ;; First we check to see whether the header in question is already
13106     ;; fetched.
13107     (if (stringp id)
13108         ;; This is a Message-ID.
13109         (setq header (gnus-id-to-header id))
13110       ;; This is an article number.
13111       (setq header (gnus-summary-article-header id)))
13112     (if header
13113         ;; We have found the header.
13114         header
13115       ;; We have to really fetch the header to this article.
13116       (when (setq where
13117                   (if (gnus-check-backend-function 'request-head group)
13118                       (gnus-request-head id group)
13119                     (gnus-request-article id group)))
13120         (save-excursion
13121           (set-buffer nntp-server-buffer)
13122           (and (search-forward "\n\n" nil t)
13123                (delete-region (1- (point)) (point-max)))
13124           (goto-char (point-max))
13125           (insert ".\n")
13126           (goto-char (point-min))
13127           (insert "211 "
13128                   (int-to-string
13129                    (cond
13130                     ((numberp id)
13131                      id)
13132                     ((cdr where)
13133                      (cdr where))
13134                     (t
13135                      gnus-reffed-article-number)))
13136                   " Article retrieved.\n"))
13137         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13138             () ; Malformed head.
13139           (if (and (stringp id)
13140                    (not (string= (gnus-group-real-name group)
13141                                  (car where))))
13142               ;; If we fetched by Message-ID and the article came
13143               ;; from a different group, we fudge some bogus article
13144               ;; numbers for this article.
13145               (mail-header-set-number header gnus-reffed-article-number))
13146           (decf gnus-reffed-article-number)
13147           (push header gnus-newsgroup-headers)
13148           (setq gnus-current-headers header)
13149           (push (mail-header-number header) gnus-newsgroup-limit)
13150           header)))))
13151
13152 (defun gnus-article-prepare (article &optional all-headers header)
13153   "Prepare ARTICLE in article mode buffer.
13154 ARTICLE should either be an article number or a Message-ID.
13155 If ARTICLE is an id, HEADER should be the article headers.
13156 If ALL-HEADERS is non-nil, no headers are hidden."
13157   (save-excursion
13158     ;; Make sure we start in a summary buffer.
13159     (unless (eq major-mode 'gnus-summary-mode)
13160       (set-buffer gnus-summary-buffer))
13161     (setq gnus-summary-buffer (current-buffer))
13162     ;; Make sure the connection to the server is alive.
13163     (unless (gnus-server-opened
13164              (gnus-find-method-for-group gnus-newsgroup-name))
13165       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13166       (gnus-request-group gnus-newsgroup-name t))
13167     (let* ((article (if header (mail-header-number header) article))
13168            (summary-buffer (current-buffer))
13169            (internal-hook gnus-article-internal-prepare-hook)
13170            (group gnus-newsgroup-name)
13171            result)
13172       (save-excursion
13173         (gnus-article-setup-buffer)
13174         (set-buffer gnus-article-buffer)
13175         ;; Deactivate active regions.
13176         (when (and (boundp 'transient-mark-mode)
13177                    transient-mark-mode)
13178           (setq mark-active nil))
13179         (if (not (setq result (let ((buffer-read-only nil))
13180                                 (gnus-request-article-this-buffer
13181                                  article group))))
13182             ;; There is no such article.
13183             (save-excursion
13184               (when (and (numberp article)
13185                          (not (memq article gnus-newsgroup-sparse)))
13186                 (setq gnus-article-current
13187                       (cons gnus-newsgroup-name article))
13188                 (set-buffer gnus-summary-buffer)
13189                 (setq gnus-current-article article)
13190                 (gnus-summary-mark-article article gnus-canceled-mark))
13191               (unless (memq article gnus-newsgroup-sparse)
13192                 (gnus-message
13193                  1 "No such article (may have expired or been canceled)")
13194                 (ding)
13195                 nil))
13196           (if (or (eq result 'pseudo) (eq result 'nneething))
13197               (progn
13198                 (save-excursion
13199                   (set-buffer summary-buffer)
13200                   (setq gnus-last-article gnus-current-article
13201                         gnus-newsgroup-history (cons gnus-current-article
13202                                                      gnus-newsgroup-history)
13203                         gnus-current-article 0
13204                         gnus-current-headers nil
13205                         gnus-article-current nil)
13206                   (if (eq result 'nneething)
13207                       (gnus-configure-windows 'summary)
13208                     (gnus-configure-windows 'article))
13209                   (gnus-set-global-variables))
13210                 (gnus-set-mode-line 'article))
13211             ;; The result from the `request' was an actual article -
13212             ;; or at least some text that is now displayed in the
13213             ;; article buffer.
13214             (if (and (numberp article)
13215                      (not (eq article gnus-current-article)))
13216                 ;; Seems like a new article has been selected.
13217                 ;; `gnus-current-article' must be an article number.
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 article
13224                         gnus-current-headers
13225                         (gnus-summary-article-header gnus-current-article)
13226                         gnus-article-current
13227                         (cons gnus-newsgroup-name gnus-current-article))
13228                   (unless (vectorp gnus-current-headers)
13229                     (setq gnus-current-headers nil))
13230                   (gnus-summary-show-thread)
13231                   (run-hooks 'gnus-mark-article-hook)
13232                   (gnus-set-mode-line 'summary)
13233                   (and (gnus-visual-p 'article-highlight 'highlight)
13234                        (run-hooks 'gnus-visual-mark-article-hook))
13235                   ;; Set the global newsgroup variables here.
13236                   ;; Suggested by Jim Sisolak
13237                   ;; <sisolak@trans4.neep.wisc.edu>.
13238                   (gnus-set-global-variables)
13239                   (setq gnus-have-all-headers
13240                         (or all-headers gnus-show-all-headers))
13241                   (and gnus-use-cache
13242                        (vectorp (gnus-summary-article-header article))
13243                        (gnus-cache-possibly-enter-article
13244                         group article
13245                         (gnus-summary-article-header article)
13246                         (memq article gnus-newsgroup-marked)
13247                         (memq article gnus-newsgroup-dormant)
13248                         (memq article gnus-newsgroup-unreads)))))
13249             ;; Hooks for getting information from the article.
13250             ;; This hook must be called before being narrowed.
13251             (let (buffer-read-only)
13252               (run-hooks 'internal-hook)
13253               (run-hooks 'gnus-article-prepare-hook)
13254               ;; Decode MIME message.
13255               (if gnus-show-mime
13256                   (if (or (not gnus-strict-mime)
13257                           (gnus-fetch-field "Mime-Version"))
13258                       (funcall gnus-show-mime-method)
13259                     (funcall gnus-decode-encoded-word-method)))
13260               ;; Perform the article display hooks.
13261               (run-hooks 'gnus-article-display-hook))
13262             ;; Do page break.
13263             (goto-char (point-min))
13264             (and gnus-break-pages (gnus-narrow-to-page))
13265             (gnus-set-mode-line 'article)
13266             (gnus-configure-windows 'article)
13267             (goto-char (point-min))
13268             t))))))
13269
13270 (defun gnus-article-show-all-headers ()
13271   "Show all article headers in article mode buffer."
13272   (save-excursion
13273     (gnus-article-setup-buffer)
13274     (set-buffer gnus-article-buffer)
13275     (let ((buffer-read-only nil))
13276       (remove-text-properties (point-min) (point-max)
13277                               gnus-hidden-properties))))
13278
13279 (defun gnus-article-hide-headers-if-wanted ()
13280   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13281 Provided for backwards compatibility."
13282   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13283       gnus-inhibit-hiding
13284       (gnus-article-hide-headers)))
13285
13286 (defun gnus-article-hide-headers (&optional arg delete)
13287   "Toggle whether to hide unwanted headers and possibly sort them as well.
13288 If given a negative prefix, always show; if given a positive prefix,
13289 always hide."
13290   (interactive "P")
13291   (unless (gnus-article-check-hidden-text 'headers arg)
13292     ;; This function might be inhibited.
13293     (unless gnus-inhibit-hiding
13294       (save-excursion
13295         (set-buffer gnus-article-buffer)
13296         (save-restriction
13297           (let ((buffer-read-only nil)
13298                 (ignored (when (not (stringp gnus-visible-headers))
13299                            (cond ((stringp gnus-ignored-headers)
13300                                   gnus-ignored-headers)
13301                                  ((listp gnus-ignored-headers)
13302                                   (mapconcat 'identity gnus-ignored-headers
13303                                              "\\|")))))
13304                 (visible
13305                  (cond ((stringp gnus-visible-headers)
13306                         gnus-visible-headers)
13307                        ((and gnus-visible-headers
13308                              (listp gnus-visible-headers))
13309                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13310                 want-list beg want-l)
13311             ;; First we narrow to just the headers.
13312             (widen)
13313             (goto-char (point-min))
13314             ;; Hide any "From " lines at the beginning of (mail) articles.
13315             (while (looking-at "From ")
13316               (forward-line 1))
13317             (unless (bobp)
13318               (add-text-properties
13319                (point-min) (point)
13320                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13321             ;; Then treat the rest of the header lines.
13322             (narrow-to-region
13323              (point)
13324              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13325             ;; Then we use the two regular expressions
13326             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13327             ;; select which header lines is to remain visible in the
13328             ;; article buffer.
13329             (goto-char (point-min))
13330             (while (re-search-forward "^[^ \t]*:" nil t)
13331               (beginning-of-line)
13332               ;; We add the headers we want to keep to a list and delete
13333               ;; them from the buffer.
13334               (if (or (and visible (looking-at visible))
13335                       (and ignored (not (looking-at ignored))))
13336                   (progn
13337                     (push (buffer-substring
13338                            (setq beg (point))
13339                            (progn
13340                              (forward-line 1)
13341                              ;; Be sure to get multi-line headers...
13342                              (re-search-forward "^[^ \t]*:" nil t)
13343                              (beginning-of-line)
13344                              (point)))
13345                           want-list)
13346                     (delete-region beg (point)))
13347                 (forward-line 1)))
13348             ;; Sort the headers that we want to display.
13349             (setq want-list (sort want-list 'gnus-article-header-less))
13350             (goto-char (point-min))
13351             (while want-list
13352               (insert (pop want-list)))
13353             ;; We make the unwanted headers invisible.
13354             (if delete
13355                 (delete-region (point-min) (point-max))
13356               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13357               (add-text-properties
13358                (point) (point-max)
13359                (nconc (list 'gnus-type 'headers)
13360                       gnus-hidden-properties)))))))))
13361
13362 (defsubst gnus-article-header-rank (header)
13363   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13364   (let ((list gnus-sorted-header-list)
13365         (i 0))
13366     (while list
13367       (when (string-match (car list) header)
13368         (setq list nil))
13369       (setq list (cdr list))
13370       (incf i))
13371     i))
13372
13373 (defun gnus-article-header-less (h1 h2)
13374   "Say whether string H1 is \"less\" than string H2."
13375   (< (gnus-article-header-rank h1)
13376      (gnus-article-header-rank h2)))
13377
13378 (defun gnus-article-hide-boring-headers (&optional arg)
13379   "Toggle hiding of headers that aren't very interesting.
13380 If given a negative prefix, always show; if given a positive prefix,
13381 always hide."
13382   (interactive "P")
13383   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13384     (save-excursion
13385       (set-buffer gnus-article-buffer)
13386       (save-restriction
13387         (let ((buffer-read-only nil)
13388               (list gnus-boring-article-headers)
13389               (inhibit-point-motion-hooks t)
13390               elem)
13391           (nnheader-narrow-to-headers)
13392           (while list
13393             (setq elem (pop list))
13394             (goto-char (point-min))
13395             (cond
13396              ;; Hide empty headers.
13397              ((eq elem 'empty)
13398               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13399                 (forward-line -1)
13400                 (add-text-properties
13401                  (progn (beginning-of-line) (point))
13402                  (progn 
13403                    (end-of-line)
13404                    (if (re-search-forward "^[^ \t]" nil t)
13405                        (match-beginning 0)
13406                      (point-max)))
13407                  (nconc (list 'gnus-type 'boring-headers)
13408                         gnus-hidden-properties))))
13409              ;; Hide boring Newsgroups header.
13410              ((eq elem 'newsgroups)
13411               (when (equal (mail-fetch-field "newsgroups")
13412                            (gnus-group-real-name gnus-newsgroup-name))
13413                 (gnus-article-hide-header "newsgroups")))
13414              ((eq elem 'followup-to)
13415               (when (equal (mail-fetch-field "followup-to")
13416                            (mail-fetch-field "newsgroups"))
13417                 (gnus-article-hide-header "followup-to")))
13418              ((eq elem 'reply-to)
13419               (let ((from (mail-fetch-field "from"))
13420                     (reply-to (mail-fetch-field "reply-to")))
13421                 (when (and
13422                        from reply-to
13423                        (equal 
13424                         (nth 1 (mail-extract-address-components from))
13425                         (nth 1 (mail-extract-address-components reply-to))))
13426                   (gnus-article-hide-header "reply-to"))))
13427              ((eq elem 'date)
13428               (let ((date (mail-fetch-field "date")))
13429                 (when (and date
13430                            (< (gnus-days-between date (current-time-string))
13431                               4))
13432                   (gnus-article-hide-header "date")))))))))))
13433
13434 (defun gnus-article-hide-header (header)
13435   (save-excursion
13436     (goto-char (point-min))
13437     (when (re-search-forward (concat "^" header ":") nil t)
13438       (add-text-properties
13439        (progn (beginning-of-line) (point))
13440        (progn 
13441          (end-of-line)
13442          (if (re-search-forward "^[^ \t]" nil t)
13443              (match-beginning 0)
13444            (point-max)))
13445        (nconc (list 'gnus-type 'boring-headers)
13446               gnus-hidden-properties)))))
13447
13448 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13449 (defun gnus-article-treat-overstrike ()
13450   "Translate overstrikes into bold text."
13451   (interactive)
13452   (save-excursion
13453     (set-buffer gnus-article-buffer)
13454     (let ((buffer-read-only nil))
13455       (while (search-forward "\b" nil t)
13456         (let ((next (following-char))
13457               (previous (char-after (- (point) 2))))
13458           (cond ((eq next previous)
13459                  (put-text-property (- (point) 2) (point) 'invisible t)
13460                  (put-text-property (point) (1+ (point)) 'face 'bold))
13461                 ((eq next ?_)
13462                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13463                  (put-text-property
13464                   (- (point) 2) (1- (point)) 'face 'underline))
13465                 ((eq previous ?_)
13466                  (put-text-property (- (point) 2) (point) 'invisible t)
13467                  (put-text-property
13468                   (point) (1+ (point))  'face 'underline))))))))
13469
13470 (defun gnus-article-word-wrap ()
13471   "Format too long lines."
13472   (interactive)
13473   (save-excursion
13474     (set-buffer gnus-article-buffer)
13475     (let ((buffer-read-only nil)
13476           p)
13477       (widen)
13478       (goto-char (point-min))
13479       (search-forward "\n\n" nil t)
13480       (end-of-line 1)
13481       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13482             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13483             (adaptive-fill-mode t))
13484         (while (not (eobp))
13485           (and (>= (current-column) (min fill-column (window-width)))
13486                (/= (preceding-char) ?:)
13487                (fill-paragraph nil))
13488           (end-of-line 2))))))
13489
13490 (defun gnus-article-remove-cr ()
13491   "Remove carriage returns from an article."
13492   (interactive)
13493   (save-excursion
13494     (set-buffer gnus-article-buffer)
13495     (let ((buffer-read-only nil))
13496       (goto-char (point-min))
13497       (while (search-forward "\r" nil t)
13498         (replace-match "" t t)))))
13499
13500 (defun gnus-article-remove-trailing-blank-lines ()
13501   "Remove all trailing blank lines from the article."
13502   (interactive)
13503   (save-excursion
13504     (set-buffer gnus-article-buffer)
13505     (let ((buffer-read-only nil))
13506       (goto-char (point-max))
13507       (delete-region
13508        (point)
13509        (progn
13510          (while (looking-at "^[ \t]*$")
13511            (forward-line -1))
13512          (forward-line 1)
13513          (point))))))
13514
13515 (defun gnus-article-display-x-face (&optional force)
13516   "Look for an X-Face header and display it if present."
13517   (interactive (list 'force))
13518   (save-excursion
13519     (set-buffer gnus-article-buffer)
13520     ;; Delete the old process, if any.
13521     (when (process-status "gnus-x-face")
13522       (delete-process "gnus-x-face"))
13523     (let ((inhibit-point-motion-hooks t)
13524           (case-fold-search nil)
13525           from)
13526       (save-restriction
13527         (nnheader-narrow-to-headers)
13528         (setq from (mail-fetch-field "from"))
13529         (goto-char (point-min))
13530         (when (and gnus-article-x-face-command
13531                    (or force
13532                        ;; Check whether this face is censored.
13533                        (not gnus-article-x-face-too-ugly)
13534                        (and gnus-article-x-face-too-ugly from
13535                             (not (string-match gnus-article-x-face-too-ugly
13536                                                from))))
13537                    ;; Has to be present.
13538                    (re-search-forward "^X-Face: " nil t))
13539           ;; We now have the area of the buffer where the X-Face is stored.
13540           (let ((beg (point))
13541                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13542             ;; We display the face.
13543             (if (symbolp gnus-article-x-face-command)
13544                 ;; The command is a lisp function, so we call it.
13545                 (if (gnus-functionp gnus-article-x-face-command)
13546                     (funcall gnus-article-x-face-command beg end)
13547                   (error "%s is not a function" gnus-article-x-face-command))
13548               ;; The command is a string, so we interpret the command
13549               ;; as a, well, command, and fork it off.
13550               (let ((process-connection-type nil))
13551                 (process-kill-without-query
13552                  (start-process
13553                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13554                 (process-send-region "gnus-x-face" beg end)
13555                 (process-send-eof "gnus-x-face")))))))))
13556
13557 (defun gnus-headers-decode-quoted-printable ()
13558   "Hack to remove QP encoding from headers."
13559   (let ((case-fold-search t)
13560         (inhibit-point-motion-hooks t)
13561         string)
13562     (goto-char (point-min))
13563     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13564       (setq string (match-string 1))
13565       (narrow-to-region (match-beginning 0) (match-end 0))
13566       (delete-region (point-min) (point-max))
13567       (insert string)
13568       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13569       (subst-char-in-region (point-min) (point-max) ?_ ? )
13570       (widen)
13571       (goto-char (point-min)))))
13572
13573 (defun gnus-article-de-quoted-unreadable (&optional force)
13574   "Do a naive translation of a quoted-printable-encoded article.
13575 This is in no way, shape or form meant as a replacement for real MIME
13576 processing, but is simply a stop-gap measure until MIME support is
13577 written.
13578 If FORCE, decode the article whether it is marked as quoted-printable
13579 or not."
13580   (interactive (list 'force))
13581   (save-excursion
13582     (set-buffer gnus-article-buffer)
13583     (let ((case-fold-search t)
13584           (buffer-read-only nil)
13585           (type (gnus-fetch-field "content-transfer-encoding")))
13586       (when (or force
13587                 (and type (string-match "quoted-printable" type)))
13588         (gnus-headers-decode-quoted-printable)
13589         (goto-char (point-min))
13590         (search-forward "\n\n" nil 'move)
13591         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13592
13593 (defun gnus-mime-decode-quoted-printable (from to)
13594   "Decode Quoted-Printable in the region between FROM and TO."
13595   (goto-char from)
13596   (while (search-forward "=" to t)
13597     (cond ((eq (following-char) ?\n)
13598            (delete-char -1)
13599            (delete-char 1))
13600           ((looking-at "[0-9A-F][0-9A-F]")
13601            (delete-char -1)
13602            (insert (hexl-hex-string-to-integer
13603                     (buffer-substring (point) (+ 2 (point)))))
13604            (delete-char 2))
13605           ((looking-at "=")
13606            (delete-char 1))
13607           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13608
13609 (defun gnus-article-hide-pgp (&optional arg)
13610   "Toggle hiding of any PGP headers and signatures in the current article.
13611 If given a negative prefix, always show; if given a positive prefix,
13612 always hide."
13613   (interactive "P")
13614   (unless (gnus-article-check-hidden-text 'pgp arg)
13615     (save-excursion
13616       (set-buffer gnus-article-buffer)
13617       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13618             buffer-read-only beg end)
13619         (widen)
13620         (goto-char (point-min))
13621         ;; Hide the "header".
13622         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13623              (add-text-properties (match-beginning 0) (match-end 0) props))
13624         (setq beg (point))
13625         ;; Hide the actual signature.
13626         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13627              (setq end (match-beginning 0))
13628              (add-text-properties
13629               (match-beginning 0)
13630               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13631                   (match-end 0)
13632                 ;; Perhaps we shouldn't hide to the end of the buffer
13633                 ;; if there is no end to the signature?
13634                 (point-max))
13635               props))
13636         ;; Hide "- " PGP quotation markers.
13637         (when (and beg end)
13638           (narrow-to-region beg end)
13639           (goto-char (point-min))
13640           (while (re-search-forward "^- " nil t)
13641             (add-text-properties (match-beginning 0) (match-end 0) props))
13642           (widen))))))
13643
13644 (defun gnus-article-hide-signature (&optional arg)
13645   "Hide the signature in the current article.
13646 If given a negative prefix, always show; if given a positive prefix,
13647 always hide."
13648   (interactive "P")
13649   (unless (gnus-article-check-hidden-text 'signature arg)
13650     (save-excursion
13651       (set-buffer gnus-article-buffer)
13652       (save-restriction
13653         (let ((buffer-read-only nil))
13654           (when (gnus-narrow-to-signature)
13655             (add-text-properties
13656              (point-min) (point-max)
13657              (nconc (list 'gnus-type 'signature)
13658                     gnus-hidden-properties))))))))
13659
13660 (defvar gnus-signature-limit nil
13661   "Provide a limit to what is considered a signature.
13662 If it is a number, no signature may not be longer (in characters) than
13663 that number.  If it is a function, the function will be called without
13664 any parameters, and if it returns nil, there is no signature in the
13665 buffer.  If it is a string, it will be used as a regexp.  If it
13666 matches, the text in question is not a signature.")
13667
13668 (defun gnus-narrow-to-signature ()
13669   "Narrow to the signature."
13670   (widen)
13671   (goto-char (point-max))
13672   (when (re-search-backward gnus-signature-separator nil t)
13673     (forward-line 1)
13674     (when (or (null gnus-signature-limit)
13675               (and (numberp gnus-signature-limit)
13676                    (< (- (point-max) (point)) gnus-signature-limit))
13677               (and (gnus-functionp gnus-signature-limit)
13678                    (funcall gnus-signature-limit))
13679               (and (stringp gnus-signature-limit)
13680                    (not (re-search-forward gnus-signature-limit nil t))))
13681       (narrow-to-region (point) (point-max))
13682       t)))
13683
13684 (defun gnus-article-check-hidden-text (type arg)
13685   "Return nil if hiding is necessary."
13686   (save-excursion
13687     (set-buffer gnus-article-buffer)
13688     (let ((hide (gnus-article-hidden-text-p type)))
13689       (cond ((or (and (null arg) (eq hide 'hidden))
13690                  (and arg (< 0 (prefix-numeric-value arg))))
13691              (gnus-article-show-hidden-text type))
13692             ((eq hide 'shown)
13693              (gnus-article-show-hidden-text type t))
13694             (t nil)))))
13695
13696 (defun gnus-article-hidden-text-p (type)
13697   "Say whether the current buffer contains hidden text of type TYPE."
13698   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13699         prop)
13700     (when pos
13701       (if (get-text-property pos 'invisible)
13702           'hidden
13703         'shown))))
13704
13705 (defun gnus-article-hide (&optional arg force)
13706   "Hide all the gruft in the current article.
13707 This means that PGP stuff, signatures, cited text and (some)
13708 headers will be hidden.
13709 If given a prefix, show the hidden text instead."
13710   (interactive (list current-prefix-arg 'force))
13711   (gnus-article-hide-headers arg)
13712   (gnus-article-hide-pgp arg)
13713   (gnus-article-hide-citation-maybe arg force)
13714   (gnus-article-hide-signature arg))
13715
13716 (defun gnus-article-show-hidden-text (type &optional hide)
13717   "Show all hidden text of type TYPE.
13718 If HIDE, hide the text instead."
13719   (save-excursion
13720     (set-buffer gnus-article-buffer)
13721     (let ((buffer-read-only nil)
13722           (inhibit-point-motion-hooks t)
13723           (beg (point-min)))
13724       (while (gnus-goto-char (text-property-any
13725                               beg (point-max) 'gnus-type type))
13726         (setq beg (point))
13727         (forward-char)
13728         (if hide
13729             (add-text-properties beg (point) gnus-hidden-properties)
13730           (remove-text-properties beg (point) gnus-hidden-properties))
13731         (setq beg (point)))
13732       t)))
13733
13734 (defvar gnus-article-time-units
13735   `((year . ,(* 365.25 24 60 60))
13736     (week . ,(* 7 24 60 60))
13737     (day . ,(* 24 60 60))
13738     (hour . ,(* 60 60))
13739     (minute . 60)
13740     (second . 1))
13741   "Mapping from time units to seconds.")
13742
13743 (defun gnus-article-date-ut (&optional type highlight)
13744   "Convert DATE date to universal time in the current article.
13745 If TYPE is `local', convert to local time; if it is `lapsed', output
13746 how much time has lapsed since DATE."
13747   (interactive (list 'ut t))
13748   (let* ((header (or gnus-current-headers
13749                      (gnus-summary-article-header) ""))
13750          (date (and (vectorp header) (mail-header-date header)))
13751          (date-regexp "^Date: \\|^X-Sent: ")
13752          (now (current-time))
13753          (inhibit-point-motion-hooks t))
13754     (when (and date (not (string= date "")))
13755       (save-excursion
13756         (set-buffer gnus-article-buffer)
13757         (save-restriction
13758           (nnheader-narrow-to-headers)
13759           (let ((buffer-read-only nil))
13760             ;; Delete any old Date headers.
13761             (if (zerop (nnheader-remove-header date-regexp t))
13762                 (beginning-of-line)
13763               (goto-char (point-max)))
13764             (insert
13765              (cond
13766               ;; Convert to the local timezone.  We have to slap a
13767               ;; `condition-case' round the calls to the timezone
13768               ;; functions since they aren't particularly resistant to
13769               ;; buggy dates.
13770               ((eq type 'local)
13771                (concat "Date: " (condition-case ()
13772                                     (timezone-make-date-arpa-standard date)
13773                                   (error date))
13774                        "\n"))
13775               ;; Convert to Universal Time.
13776               ((eq type 'ut)
13777                (concat "Date: "
13778                        (condition-case ()
13779                            (timezone-make-date-arpa-standard date nil "UT")
13780                          (error date))
13781                        "\n"))
13782               ;; Get the original date from the article.
13783               ((eq type 'original)
13784                (concat "Date: " date "\n"))
13785               ;; Do an X-Sent lapsed format.
13786               ((eq type 'lapsed)
13787                ;; If the date is seriously mangled, the timezone
13788                ;; functions are liable to bug out, so we condition-case
13789                ;; the entire thing.
13790                (let* ((real-time
13791                        (condition-case ()
13792                            (gnus-time-minus
13793                             (gnus-encode-date
13794                              (timezone-make-date-arpa-standard
13795                               (current-time-string now)
13796                               (current-time-zone now) "UT"))
13797                             (gnus-encode-date
13798                              (timezone-make-date-arpa-standard
13799                               date nil "UT")))
13800                          (error '(0 0))))
13801                       (real-sec (+ (* (float (car real-time)) 65536)
13802                                    (cadr real-time)))
13803                       (sec (abs real-sec))
13804                       num prev)
13805                  (if (zerop sec)
13806                      "X-Sent: Now\n"
13807                    (concat
13808                     "X-Sent: "
13809                     ;; This is a bit convoluted, but basically we go
13810                     ;; through the time units for years, weeks, etc,
13811                     ;; and divide things to see whether that results
13812                     ;; in positive answers.
13813                     (mapconcat
13814                      (lambda (unit)
13815                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13816                            ;; The (remaining) seconds are too few to
13817                            ;; be divided into this time unit.
13818                            ""
13819                          ;; It's big enough, so we output it.
13820                          (setq sec (- sec (* num (cdr unit))))
13821                          (prog1
13822                              (concat (if prev ", " "") (int-to-string
13823                                                         (floor num))
13824                                      " " (symbol-name (car unit))
13825                                      (if (> num 1) "s" ""))
13826                            (setq prev t))))
13827                      gnus-article-time-units "")
13828                     ;; If dates are odd, then it might appear like the
13829                     ;; article was sent in the future.
13830                     (if (> real-sec 0)
13831                         " ago\n"
13832                       " in the future\n")))))
13833               (t
13834                (error "Unknown conversion type: %s" type)))))
13835           ;; Do highlighting.
13836           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13837             (gnus-article-highlight-headers)))))))
13838
13839 (defun gnus-article-date-local (&optional highlight)
13840   "Convert the current article date to the local timezone."
13841   (interactive (list t))
13842   (gnus-article-date-ut 'local highlight))
13843
13844 (defun gnus-article-date-original (&optional highlight)
13845   "Convert the current article date to what it was originally.
13846 This is only useful if you have used some other date conversion
13847 function and want to see what the date was before converting."
13848   (interactive (list t))
13849   (gnus-article-date-ut 'original highlight))
13850
13851 (defun gnus-article-date-lapsed (&optional highlight)
13852   "Convert the current article date to time lapsed since it was sent."
13853   (interactive (list t))
13854   (gnus-article-date-ut 'lapsed highlight))
13855
13856 (defun gnus-article-maybe-highlight ()
13857   "Do some article highlighting if `gnus-visual' is non-nil."
13858   (if (gnus-visual-p 'article-highlight 'highlight)
13859       (gnus-article-highlight-some)))
13860
13861 ;; Article savers.
13862
13863 (defun gnus-output-to-rmail (file-name)
13864   "Append the current article to an Rmail file named FILE-NAME."
13865   (require 'rmail)
13866   ;; Most of these codes are borrowed from rmailout.el.
13867   (setq file-name (expand-file-name file-name))
13868   (setq rmail-default-rmail-file file-name)
13869   (let ((artbuf (current-buffer))
13870         (tmpbuf (get-buffer-create " *Gnus-output*")))
13871     (save-excursion
13872       (or (get-file-buffer file-name)
13873           (file-exists-p file-name)
13874           (if (gnus-yes-or-no-p
13875                (concat "\"" file-name "\" does not exist, create it? "))
13876               (let ((file-buffer (create-file-buffer file-name)))
13877                 (save-excursion
13878                   (set-buffer file-buffer)
13879                   (rmail-insert-rmail-file-header)
13880                   (let ((require-final-newline nil))
13881                     (write-region (point-min) (point-max) file-name t 1)))
13882                 (kill-buffer file-buffer))
13883             (error "Output file does not exist")))
13884       (set-buffer tmpbuf)
13885       (buffer-disable-undo (current-buffer))
13886       (erase-buffer)
13887       (insert-buffer-substring artbuf)
13888       (gnus-convert-article-to-rmail)
13889       ;; Decide whether to append to a file or to an Emacs buffer.
13890       (let ((outbuf (get-file-buffer file-name)))
13891         (if (not outbuf)
13892             (append-to-file (point-min) (point-max) file-name)
13893           ;; File has been visited, in buffer OUTBUF.
13894           (set-buffer outbuf)
13895           (let ((buffer-read-only nil)
13896                 (msg (and (boundp 'rmail-current-message)
13897                           (symbol-value 'rmail-current-message))))
13898             ;; If MSG is non-nil, buffer is in RMAIL mode.
13899             (if msg
13900                 (progn (widen)
13901                        (narrow-to-region (point-max) (point-max))))
13902             (insert-buffer-substring tmpbuf)
13903             (if msg
13904                 (progn
13905                   (goto-char (point-min))
13906                   (widen)
13907                   (search-backward "\^_")
13908                   (narrow-to-region (point) (point-max))
13909                   (goto-char (1+ (point-min)))
13910                   (rmail-count-new-messages t)
13911                   (rmail-show-message msg)))))))
13912     (kill-buffer tmpbuf)))
13913
13914 (defun gnus-output-to-file (file-name)
13915   "Append the current article to a file named FILE-NAME."
13916   (setq file-name (expand-file-name file-name))
13917   (let ((artbuf (current-buffer))
13918         (tmpbuf (get-buffer-create " *Gnus-output*")))
13919     (save-excursion
13920       (set-buffer tmpbuf)
13921       (buffer-disable-undo (current-buffer))
13922       (erase-buffer)
13923       (insert-buffer-substring artbuf)
13924       ;; Append newline at end of the buffer as separator, and then
13925       ;; save it to file.
13926       (goto-char (point-max))
13927       (insert "\n")
13928       (append-to-file (point-min) (point-max) file-name))
13929     (kill-buffer tmpbuf)))
13930
13931 (defun gnus-convert-article-to-rmail ()
13932   "Convert article in current buffer to Rmail message format."
13933   (let ((buffer-read-only nil))
13934     ;; Convert article directly into Babyl format.
13935     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
13936     (goto-char (point-min))
13937     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
13938     (while (search-forward "\n\^_" nil t) ;single char
13939       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
13940     (goto-char (point-max))
13941     (insert "\^_")))
13942
13943 (defun gnus-narrow-to-page (&optional arg)
13944   "Narrow the article buffer to a page.
13945 If given a numerical ARG, move forward ARG pages."
13946   (interactive "P")
13947   (setq arg (if arg (prefix-numeric-value arg) 0))
13948   (save-excursion
13949     (set-buffer gnus-article-buffer)
13950     (goto-char (point-min))
13951     (widen)
13952     (when (gnus-visual-p 'page-marker)
13953       (let ((buffer-read-only nil))
13954         (gnus-remove-text-with-property 'gnus-prev)
13955         (gnus-remove-text-with-property 'gnus-next)))
13956     (when
13957         (cond ((< arg 0)
13958                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
13959               ((> arg 0)
13960                (re-search-forward page-delimiter nil 'move arg)))
13961       (goto-char (match-end 0)))
13962     (narrow-to-region
13963      (point)
13964      (if (re-search-forward page-delimiter nil 'move)
13965          (match-beginning 0)
13966        (point)))
13967     (when (and (gnus-visual-p 'page-marker)
13968                (not (= (point-min) 1)))
13969       (save-excursion
13970         (goto-char (point-min))
13971         (gnus-insert-prev-page-button)))
13972     (when (and (gnus-visual-p 'page-marker)
13973                (not (= (1- (point-max)) (buffer-size))))
13974       (save-excursion
13975         (goto-char (point-max))
13976         (gnus-insert-next-page-button)))))
13977
13978 ;; Article mode commands
13979
13980 (defun gnus-article-goto-next-page ()
13981   "Show the next page of the article."
13982   (interactive)
13983   (when (gnus-article-next-page)
13984     (gnus-article-read-summary-keys nil ?n)))
13985
13986 (defun gnus-article-goto-prev-page ()
13987   "Show the next page of the article."
13988   (interactive)
13989   (if (bobp) (gnus-article-read-summary-keys nil ?n)
13990     (gnus-article-prev-page nil)))
13991
13992 (defun gnus-article-next-page (&optional lines)
13993   "Show the next page of the current article.
13994 If end of article, return non-nil.  Otherwise return nil.
13995 Argument LINES specifies lines to be scrolled up."
13996   (interactive "p")
13997   (move-to-window-line -1)
13998   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
13999   (if (save-excursion
14000         (end-of-line)
14001         (and (pos-visible-in-window-p)  ;Not continuation line.
14002              (eobp)))
14003       ;; Nothing in this page.
14004       (if (or (not gnus-break-pages)
14005               (save-excursion
14006                 (save-restriction
14007                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14008           t                             ;Nothing more.
14009         (gnus-narrow-to-page 1)         ;Go to next page.
14010         nil)
14011     ;; More in this page.
14012     (condition-case ()
14013         (scroll-up lines)
14014       (end-of-buffer
14015        ;; Long lines may cause an end-of-buffer error.
14016        (goto-char (point-max))))
14017     nil))
14018
14019 (defun gnus-article-prev-page (&optional lines)
14020   "Show previous page of current article.
14021 Argument LINES specifies lines to be scrolled down."
14022   (interactive "p")
14023   (move-to-window-line 0)
14024   (if (and gnus-break-pages
14025            (bobp)
14026            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14027       (progn
14028         (gnus-narrow-to-page -1)        ;Go to previous page.
14029         (goto-char (point-max))
14030         (recenter -1))
14031     (condition-case ()
14032         (scroll-down lines)
14033       (error nil))))
14034
14035 (defun gnus-article-refer-article ()
14036   "Read article specified by message-id around point."
14037   (interactive)
14038   (let ((point (point)))
14039     (search-forward ">" nil t)          ;Move point to end of "<....>".
14040     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14041         (let ((message-id (match-string 1)))
14042           (goto-char point)
14043           (set-buffer gnus-summary-buffer)
14044           (gnus-summary-refer-article message-id))
14045       (goto-char (point))
14046       (error "No references around point"))))
14047
14048 (defun gnus-article-show-summary ()
14049   "Reconfigure windows to show summary buffer."
14050   (interactive)
14051   (gnus-configure-windows 'article)
14052   (gnus-summary-goto-subject gnus-current-article))
14053
14054 (defun gnus-article-describe-briefly ()
14055   "Describe article mode commands briefly."
14056   (interactive)
14057   (gnus-message 6
14058                 (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")))
14059
14060 (defun gnus-article-summary-command ()
14061   "Execute the last keystroke in the summary buffer."
14062   (interactive)
14063   (let ((obuf (current-buffer))
14064         (owin (current-window-configuration))
14065         func)
14066     (switch-to-buffer gnus-summary-buffer 'norecord)
14067     (setq func (lookup-key (current-local-map) (this-command-keys)))
14068     (call-interactively func)
14069     (set-buffer obuf)
14070     (set-window-configuration owin)
14071     (set-window-point (get-buffer-window (current-buffer)) (point))))
14072
14073 (defun gnus-article-summary-command-nosave ()
14074   "Execute the last keystroke in the summary buffer."
14075   (interactive)
14076   (let (func)
14077     (pop-to-buffer gnus-summary-buffer 'norecord)
14078     (setq func (lookup-key (current-local-map) (this-command-keys)))
14079     (call-interactively func)))
14080
14081 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14082   "Read a summary buffer key sequence and execute it from the article buffer."
14083   (interactive "P")
14084   (let ((nosaves
14085          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14086            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14087            "=" "^" "\M-^"))
14088         keys)
14089     (save-excursion
14090       (set-buffer gnus-summary-buffer)
14091       (push (or key last-command-event) unread-command-events)
14092       (setq keys (read-key-sequence nil)))
14093     (message "")
14094
14095     (if (member keys nosaves)
14096         (let (func)
14097           (pop-to-buffer gnus-summary-buffer 'norecord)
14098           (if (setq func (lookup-key (current-local-map) keys))
14099               (call-interactively func)
14100             (ding)))
14101       (let ((obuf (current-buffer))
14102             (owin (current-window-configuration))
14103             (opoint (point))
14104             func in-buffer)
14105         (if not-restore-window
14106             (pop-to-buffer gnus-summary-buffer 'norecord)
14107           (switch-to-buffer gnus-summary-buffer 'norecord))
14108         (setq in-buffer (current-buffer))
14109         (if (setq func (lookup-key (current-local-map) keys))
14110             (call-interactively func)
14111           (ding))
14112         (when (eq in-buffer (current-buffer))
14113           (set-buffer obuf)
14114           (unless not-restore-window
14115             (set-window-configuration owin))
14116           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14117
14118 \f
14119 ;;;
14120 ;;; Kill file handling.
14121 ;;;
14122
14123 ;;;###autoload
14124 (defalias 'gnus-batch-kill 'gnus-batch-score)
14125 ;;;###autoload
14126 (defun gnus-batch-score ()
14127   "Run batched scoring.
14128 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14129 Newsgroups is a list of strings in Bnews format.  If you want to score
14130 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14131 score the alt hierarchy, you'd say \"!alt.all\"."
14132   (interactive)
14133   (let* ((yes-and-no
14134           (gnus-newsrc-parse-options
14135            (apply (function concat)
14136                   (mapcar (lambda (g) (concat g " "))
14137                           command-line-args-left))))
14138          (gnus-expert-user t)
14139          (nnmail-spool-file nil)
14140          (gnus-use-dribble-file nil)
14141          (yes (car yes-and-no))
14142          (no (cdr yes-and-no))
14143          group newsrc entry
14144          ;; Disable verbose message.
14145          gnus-novice-user gnus-large-newsgroup)
14146     ;; Eat all arguments.
14147     (setq command-line-args-left nil)
14148     ;; Start Gnus.
14149     (gnus)
14150     ;; Apply kills to specified newsgroups in command line arguments.
14151     (setq newsrc (cdr gnus-newsrc-alist))
14152     (while newsrc
14153       (setq group (car (car newsrc)))
14154       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14155       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14156                (and (car entry)
14157                     (or (eq (car entry) t)
14158                         (not (zerop (car entry)))))
14159                (if yes (string-match yes group) t)
14160                (or (null no) (not (string-match no group))))
14161           (progn
14162             (gnus-summary-read-group group nil t nil t)
14163             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14164                  (gnus-summary-exit))))
14165       (setq newsrc (cdr newsrc)))
14166     ;; Exit Emacs.
14167     (switch-to-buffer gnus-group-buffer)
14168     (gnus-group-save-newsrc)))
14169
14170 (defun gnus-apply-kill-file ()
14171   "Apply a kill file to the current newsgroup.
14172 Returns the number of articles marked as read."
14173   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14174           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14175       (gnus-apply-kill-file-internal)
14176     0))
14177
14178 (defun gnus-kill-save-kill-buffer ()
14179   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14180     (when (get-file-buffer file)
14181       (save-excursion
14182         (set-buffer (get-file-buffer file))
14183         (and (buffer-modified-p) (save-buffer))
14184         (kill-buffer (current-buffer))))))
14185
14186 (defvar gnus-kill-file-name "KILL"
14187   "Suffix of the kill files.")
14188
14189 (defun gnus-newsgroup-kill-file (newsgroup)
14190   "Return the name of a kill file name for NEWSGROUP.
14191 If NEWSGROUP is nil, return the global kill file name instead."
14192   (cond 
14193    ;; The global KILL file is placed at top of the directory.
14194    ((or (null newsgroup)
14195         (string-equal newsgroup ""))
14196     (expand-file-name gnus-kill-file-name
14197                       (or gnus-kill-files-directory "~/News")))
14198    ;; Append ".KILL" to newsgroup name.
14199    ((gnus-use-long-file-name 'not-kill)
14200     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14201                               "." gnus-kill-file-name)
14202                       (or gnus-kill-files-directory "~/News")))
14203    ;; Place "KILL" under the hierarchical directory.
14204    (t
14205     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14206                               "/" gnus-kill-file-name)
14207                       (or gnus-kill-files-directory "~/News")))))
14208
14209 \f
14210 ;;;
14211 ;;; Dribble file
14212 ;;;
14213
14214 (defvar gnus-dribble-ignore nil)
14215 (defvar gnus-dribble-eval-file nil)
14216
14217 (defun gnus-dribble-file-name ()
14218   "Return the dribble file for the current .newsrc."
14219   (concat
14220    (if gnus-dribble-directory
14221        (concat (file-name-as-directory gnus-dribble-directory)
14222                (file-name-nondirectory gnus-current-startup-file))
14223      gnus-current-startup-file)
14224    "-dribble"))
14225
14226 (defun gnus-dribble-enter (string)
14227   "Enter STRING into the dribble buffer."
14228   (if (and (not gnus-dribble-ignore)
14229            gnus-dribble-buffer
14230            (buffer-name gnus-dribble-buffer))
14231       (let ((obuf (current-buffer)))
14232         (set-buffer gnus-dribble-buffer)
14233         (insert string "\n")
14234         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14235         (set-buffer obuf))))
14236
14237 (defun gnus-dribble-read-file ()
14238   "Read the dribble file from disk."
14239   (let ((dribble-file (gnus-dribble-file-name)))
14240     (save-excursion
14241       (set-buffer (setq gnus-dribble-buffer
14242                         (get-buffer-create
14243                          (file-name-nondirectory dribble-file))))
14244       (gnus-add-current-to-buffer-list)
14245       (erase-buffer)
14246       (setq buffer-file-name dribble-file)
14247       (auto-save-mode t)
14248       (buffer-disable-undo (current-buffer))
14249       (bury-buffer (current-buffer))
14250       (set-buffer-modified-p nil)
14251       (let ((auto (make-auto-save-file-name))
14252             (gnus-dribble-ignore t))
14253         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14254           ;; Load whichever file is newest -- the auto save file
14255           ;; or the "real" file.
14256           (if (file-newer-than-file-p auto dribble-file)
14257               (insert-file-contents auto)
14258             (insert-file-contents dribble-file))
14259           (unless (zerop (buffer-size))
14260             (set-buffer-modified-p t))
14261           ;; Set the file modes to reflect the .newsrc file modes.
14262           (save-buffer)
14263           (when (file-exists-p gnus-current-startup-file)
14264             (set-file-modes dribble-file
14265                             (file-modes gnus-current-startup-file)))
14266           ;; Possibly eval the file later.
14267           (when (gnus-y-or-n-p
14268                  "Auto-save file exists.  Do you want to read it? ")
14269             (setq gnus-dribble-eval-file t)))))))
14270
14271 (defun gnus-dribble-eval-file ()
14272   (if (not gnus-dribble-eval-file)
14273       ()
14274     (setq gnus-dribble-eval-file nil)
14275     (save-excursion
14276       (let ((gnus-dribble-ignore t))
14277         (set-buffer gnus-dribble-buffer)
14278         (eval-buffer (current-buffer))))))
14279
14280 (defun gnus-dribble-delete-file ()
14281   (if (file-exists-p (gnus-dribble-file-name))
14282       (delete-file (gnus-dribble-file-name)))
14283   (if gnus-dribble-buffer
14284       (save-excursion
14285         (set-buffer gnus-dribble-buffer)
14286         (let ((auto (make-auto-save-file-name)))
14287           (if (file-exists-p auto)
14288               (delete-file auto))
14289           (erase-buffer)
14290           (set-buffer-modified-p nil)))))
14291
14292 (defun gnus-dribble-save ()
14293   (if (and gnus-dribble-buffer
14294            (buffer-name gnus-dribble-buffer))
14295       (save-excursion
14296         (set-buffer gnus-dribble-buffer)
14297         (save-buffer))))
14298
14299 (defun gnus-dribble-clear ()
14300   (save-excursion
14301     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14302         (progn
14303           (set-buffer gnus-dribble-buffer)
14304           (erase-buffer)
14305           (set-buffer-modified-p nil)
14306           (setq buffer-saved-size (buffer-size))))))
14307
14308 \f
14309 ;;;
14310 ;;; Server Communication
14311 ;;;
14312
14313 (defun gnus-start-news-server (&optional confirm)
14314   "Open a method for getting news.
14315 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14316   (let (how)
14317     (if gnus-current-select-method
14318         ;; Stream is already opened.
14319         nil
14320       ;; Open NNTP server.
14321       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14322       (if confirm
14323           (progn
14324             ;; Read server name with completion.
14325             (setq gnus-nntp-server
14326                   (completing-read "NNTP server: "
14327                                    (mapcar (lambda (server) (list server))
14328                                            (cons (list gnus-nntp-server)
14329                                                  gnus-secondary-servers))
14330                                    nil nil gnus-nntp-server))))
14331
14332       (if (and gnus-nntp-server
14333                (stringp gnus-nntp-server)
14334                (not (string= gnus-nntp-server "")))
14335           (setq gnus-select-method
14336                 (cond ((or (string= gnus-nntp-server "")
14337                            (string= gnus-nntp-server "::"))
14338                        (list 'nnspool (system-name)))
14339                       ((string-match "^:" gnus-nntp-server)
14340                        (list 'nnmh gnus-nntp-server
14341                              (list 'nnmh-directory
14342                                    (file-name-as-directory
14343                                     (expand-file-name
14344                                      (concat "~/" (substring
14345                                                    gnus-nntp-server 1)))))
14346                              (list 'nnmh-get-new-mail nil)))
14347                       (t
14348                        (list 'nntp gnus-nntp-server)))))
14349
14350       (setq how (car gnus-select-method))
14351       (cond ((eq how 'nnspool)
14352              (require 'nnspool)
14353              (gnus-message 5 "Looking up local news spool..."))
14354             ((eq how 'nnmh)
14355              (require 'nnmh)
14356              (gnus-message 5 "Looking up mh spool..."))
14357             (t
14358              (require 'nntp)))
14359       (setq gnus-current-select-method gnus-select-method)
14360       (run-hooks 'gnus-open-server-hook)
14361       (or
14362        ;; gnus-open-server-hook might have opened it
14363        (gnus-server-opened gnus-select-method)
14364        (gnus-open-server gnus-select-method)
14365        (gnus-y-or-n-p
14366         (format
14367          "%s (%s) open error: '%s'.     Continue? "
14368          (car gnus-select-method) (cadr gnus-select-method)
14369          (gnus-status-message gnus-select-method)))
14370        (progn
14371          (gnus-message 1 "Couldn't open server on %s"
14372                        (nth 1 gnus-select-method))
14373          (ding)
14374          nil)))))
14375
14376 (defun gnus-check-group (group)
14377   "Try to make sure that the server where GROUP exists is alive."
14378   (let ((method (gnus-find-method-for-group group)))
14379     (or (gnus-server-opened method)
14380         (gnus-open-server method))))
14381
14382 (defun gnus-check-server (&optional method)
14383   "Check whether the connection to METHOD is down.
14384 If METHOD is nil, use `gnus-select-method'.
14385 If it is down, start it up (again)."
14386   (let ((method (or method gnus-select-method)))
14387     ;; Transform virtual server names into select methods.
14388     (when (stringp method)
14389       (setq method (gnus-server-to-method method)))
14390     (if (gnus-server-opened method)
14391         ;; The stream is already opened.
14392         t
14393       ;; Open the server.
14394       (gnus-message 5 "Opening %s server%s..." (car method)
14395                     (if (equal (nth 1 method) "") ""
14396                       (format " on %s" (nth 1 method))))
14397       (run-hooks 'gnus-open-server-hook)
14398       (prog1
14399           (gnus-open-server method)
14400         (message "")))))
14401
14402 (defun gnus-get-function (method function)
14403   "Return a function symbol based on METHOD and FUNCTION."
14404   ;; Translate server names into methods.
14405   (unless method
14406     (error "Attempted use of a nil select method"))
14407   (when (stringp method)
14408     (setq method (gnus-server-to-method method)))
14409   (let ((func (intern (format "%s-%s" (car method) function))))
14410     ;; If the functions isn't bound, we require the backend in
14411     ;; question.
14412     (unless (fboundp func)
14413       (require (car method))
14414       (unless (fboundp func)
14415         ;; This backend doesn't implement this function.
14416         (error "No such function: %s" func)))
14417     func))
14418
14419 \f
14420 ;;;
14421 ;;; Interface functions to the backends.
14422 ;;;
14423
14424 (defun gnus-open-server (method)
14425   "Open a connection to METHOD."
14426   (let ((elem (assoc method gnus-opened-servers)))
14427     ;; If this method was previously denied, we just return nil.
14428     (if (eq (nth 1 elem) 'denied)
14429         (progn
14430           (gnus-message 1 "Denied server")
14431           nil)
14432       ;; Open the server.
14433       (let ((result
14434              (funcall (gnus-get-function method 'open-server)
14435                       (nth 1 method) (nthcdr 2 method))))
14436         ;; If this hasn't been opened before, we add it to the list.
14437         (unless elem
14438           (setq elem (list method nil)
14439                 gnus-opened-servers (cons elem gnus-opened-servers)))
14440         ;; Set the status of this server.
14441         (setcar (cdr elem) (if result 'ok 'denied))
14442         ;; Return the result from the "open" call.
14443         result))))
14444
14445 (defun gnus-close-server (method)
14446   "Close the connection to METHOD."
14447   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14448
14449 (defun gnus-request-list (method)
14450   "Request the active file from METHOD."
14451   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14452
14453 (defun gnus-request-list-newsgroups (method)
14454   "Request the newsgroups file from METHOD."
14455   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14456
14457 (defun gnus-request-newgroups (date method)
14458   "Request all new groups since DATE from METHOD."
14459   (funcall (gnus-get-function method 'request-newgroups)
14460            date (nth 1 method)))
14461
14462 (defun gnus-server-opened (method)
14463   "Check whether a connection to METHOD has been opened."
14464   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14465
14466 (defun gnus-status-message (method)
14467   "Return the status message from METHOD.
14468 If METHOD is a string, it is interpreted as a group name.   The method
14469 this group uses will be queried."
14470   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14471                   method)))
14472     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14473
14474 (defun gnus-request-group (group &optional dont-check method)
14475   "Request GROUP.  If DONT-CHECK, no information is required."
14476   (let ((method (or method (gnus-find-method-for-group group))))
14477     (funcall (gnus-get-function method 'request-group)
14478              (gnus-group-real-name group) (nth 1 method) dont-check)))
14479
14480 (defun gnus-request-asynchronous (group &optional articles)
14481   "Request that GROUP behave asynchronously.
14482 ARTICLES is the `data' of the group."
14483   (let ((method (gnus-find-method-for-group group)))
14484     (funcall (gnus-get-function method 'request-asynchronous)
14485              (gnus-group-real-name group) (nth 1 method) articles)))
14486
14487 (defun gnus-list-active-group (group)
14488   "Request active information on GROUP."
14489   (let ((method (gnus-find-method-for-group group))
14490         (func 'list-active-group))
14491     (when (gnus-check-backend-function func group)
14492       (funcall (gnus-get-function method func)
14493                (gnus-group-real-name group) (nth 1 method)))))
14494
14495 (defun gnus-request-group-description (group)
14496   "Request a description of GROUP."
14497   (let ((method (gnus-find-method-for-group group))
14498         (func 'request-group-description))
14499     (when (gnus-check-backend-function func group)
14500       (funcall (gnus-get-function method func)
14501                (gnus-group-real-name group) (nth 1 method)))))
14502
14503 (defun gnus-close-group (group)
14504   "Request the GROUP be closed."
14505   (let ((method (gnus-find-method-for-group group)))
14506     (funcall (gnus-get-function method 'close-group)
14507              (gnus-group-real-name group) (nth 1 method))))
14508
14509 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14510   "Request headers for ARTICLES in GROUP.
14511 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14512   (let ((method (gnus-find-method-for-group group)))
14513     (if (and gnus-use-cache (numberp (car articles)))
14514         (gnus-cache-retrieve-headers articles group fetch-old)
14515       (funcall (gnus-get-function method 'retrieve-headers)
14516                articles (gnus-group-real-name group) (nth 1 method)
14517                fetch-old))))
14518
14519 (defun gnus-retrieve-groups (groups method)
14520   "Request active information on GROUPS from METHOD."
14521   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14522
14523 (defun gnus-request-type (group &optional article)
14524   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14525   (let ((method (gnus-find-method-for-group group)))
14526     (if (not (gnus-check-backend-function 'request-type (car method)))
14527         'unknown
14528       (funcall (gnus-get-function method 'request-type)
14529                (gnus-group-real-name group) article))))
14530
14531 (defun gnus-request-update-mark (group article mark)
14532   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14533   (let ((method (gnus-find-method-for-group group)))
14534     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14535         mark
14536       (funcall (gnus-get-function method 'request-update-mark)
14537                (gnus-group-real-name group) article mark))))
14538
14539 (defun gnus-request-article (article group &optional buffer)
14540   "Request the ARTICLE in GROUP.
14541 ARTICLE can either be an article number or an article Message-ID.
14542 If BUFFER, insert the article in that group."
14543   (let ((method (gnus-find-method-for-group group)))
14544     (funcall (gnus-get-function method 'request-article)
14545              article (gnus-group-real-name group) (nth 1 method) buffer)))
14546
14547 (defun gnus-request-head (article group)
14548   "Request the head of ARTICLE in GROUP."
14549   (let ((method (gnus-find-method-for-group group)))
14550     (funcall (gnus-get-function method 'request-head)
14551              article (gnus-group-real-name group) (nth 1 method))))
14552
14553 (defun gnus-request-body (article group)
14554   "Request the body of ARTICLE in GROUP."
14555   (let ((method (gnus-find-method-for-group group)))
14556     (funcall (gnus-get-function method 'request-body)
14557              article (gnus-group-real-name group) (nth 1 method))))
14558
14559 (defun gnus-request-post (method)
14560   "Post the current buffer using METHOD."
14561   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14562
14563 (defun gnus-request-scan (group method)
14564   "Request a SCAN being performed in GROUP from METHOD.
14565 If GROUP is nil, all groups on METHOD are scanned."
14566   (let ((method (if group (gnus-find-method-for-group group) method)))
14567     (funcall (gnus-get-function method 'request-scan)
14568              (and group (gnus-group-real-name group)) (nth 1 method))))
14569
14570 (defsubst gnus-request-update-info (info method)
14571   "Request that METHOD update INFO."
14572   (when (gnus-check-backend-function 'request-update-info (car method))
14573     (funcall (gnus-get-function method 'request-update-info)
14574              (gnus-group-real-name (gnus-info-group info))
14575              info (nth 1 method))))
14576
14577 (defun gnus-request-expire-articles (articles group &optional force)
14578   (let ((method (gnus-find-method-for-group group)))
14579     (funcall (gnus-get-function method 'request-expire-articles)
14580              articles (gnus-group-real-name group) (nth 1 method)
14581              force)))
14582
14583 (defun gnus-request-move-article
14584   (article group server accept-function &optional last)
14585   (let ((method (gnus-find-method-for-group group)))
14586     (funcall (gnus-get-function method 'request-move-article)
14587              article (gnus-group-real-name group)
14588              (nth 1 method) accept-function last)))
14589
14590 (defun gnus-request-accept-article (group &optional last method)
14591   ;; Make sure there's a newline at the end of the article.
14592   (goto-char (point-max))
14593   (unless (bolp)
14594     (insert "\n"))
14595   (let ((func (if (symbolp group) group
14596                 (car (or method (gnus-find-method-for-group group))))))
14597     (funcall (intern (format "%s-request-accept-article" func))
14598              (if (stringp group) (gnus-group-real-name group) group)
14599              last)))
14600
14601 (defun gnus-request-replace-article (article group buffer)
14602   (let ((func (car (gnus-find-method-for-group group))))
14603     (funcall (intern (format "%s-request-replace-article" func))
14604              article (gnus-group-real-name group) buffer)))
14605
14606 (defun gnus-request-associate-buffer (group)
14607   (let ((method (gnus-find-method-for-group group)))
14608     (funcall (gnus-get-function method 'request-associate-buffer)
14609              (gnus-group-real-name group))))
14610
14611 (defun gnus-request-restore-buffer (article group)
14612   "Request a new buffer restored to the state of ARTICLE."
14613   (let ((method (gnus-find-method-for-group group)))
14614     (funcall (gnus-get-function method 'request-restore-buffer)
14615              article (gnus-group-real-name group) (nth 1 method))))
14616
14617 (defun gnus-request-create-group (group &optional method)
14618   (let ((method (or method (gnus-find-method-for-group group))))
14619     (funcall (gnus-get-function method 'request-create-group)
14620              (gnus-group-real-name group) (nth 1 method))))
14621
14622 (defun gnus-request-delete-group (group &optional force)
14623   (let ((method (gnus-find-method-for-group group)))
14624     (funcall (gnus-get-function method 'request-delete-group)
14625              (gnus-group-real-name group) force (nth 1 method))))
14626
14627 (defun gnus-request-rename-group (group new-name)
14628   (let ((method (gnus-find-method-for-group group)))
14629     (funcall (gnus-get-function method 'request-rename-group)
14630              (gnus-group-real-name group)
14631              (gnus-group-real-name new-name) (nth 1 method))))
14632
14633 (defun gnus-member-of-valid (symbol group)
14634   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14635   (memq symbol (assoc
14636                 (symbol-name (car (gnus-find-method-for-group group)))
14637                 gnus-valid-select-methods)))
14638
14639 (defun gnus-method-option-p (method option)
14640   "Return non-nil if select METHOD has OPTION as a parameter."
14641   (memq option (assoc (format "%s" (car method))
14642                       gnus-valid-select-methods)))
14643
14644 (defun gnus-server-extend-method (group method)
14645   ;; This function "extends" a virtual server.  If the server is
14646   ;; "hello", and the select method is ("hello" (my-var "something"))
14647   ;; in the group "alt.alt", this will result in a new virtual server
14648   ;; called "hello+alt.alt".
14649   (let ((entry
14650          (gnus-copy-sequence
14651           (if (equal (car method) "native") gnus-select-method
14652             (cdr (assoc (car method) gnus-server-alist))))))
14653     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14654     (nconc entry (cdr method))))
14655
14656 (defun gnus-find-method-for-group (group &optional info)
14657   "Find the select method that GROUP uses."
14658   (or gnus-override-method
14659       (and (not group)
14660            gnus-select-method)
14661       (let ((info (or info (gnus-get-info group)))
14662             method)
14663         (if (or (not info)
14664                 (not (setq method (gnus-info-method info))))
14665             (setq method gnus-select-method)
14666           (setq method
14667                 (cond ((stringp method)
14668                        (gnus-server-to-method method))
14669                       ((stringp (car method))
14670                        (gnus-server-extend-method group method))
14671                       (t
14672                        method))))
14673         (gnus-server-add-address method))))
14674
14675 (defun gnus-check-backend-function (func group)
14676   "Check whether GROUP supports function FUNC."
14677   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14678                   group)))
14679     (fboundp (intern (format "%s-%s" method func)))))
14680
14681 (defun gnus-methods-using (feature)
14682   "Find all methods that have FEATURE."
14683   (let ((valids gnus-valid-select-methods)
14684         outs)
14685     (while valids
14686       (if (memq feature (car valids))
14687           (setq outs (cons (car valids) outs)))
14688       (setq valids (cdr valids)))
14689     outs))
14690
14691 \f
14692 ;;;
14693 ;;; Active & Newsrc File Handling
14694 ;;;
14695
14696 (defun gnus-setup-news (&optional rawfile level)
14697   "Setup news information.
14698 If RAWFILE is non-nil, the .newsrc file will also be read.
14699 If LEVEL is non-nil, the news will be set up at level LEVEL."
14700   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14701     ;; Clear some variables to re-initialize news information.
14702     (if init (setq gnus-newsrc-alist nil
14703                    gnus-active-hashtb nil))
14704
14705     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14706     (if init (gnus-read-newsrc-file rawfile))
14707
14708     (unless (assoc "archive" gnus-server-alist)
14709       (push (cons "archive" gnus-message-archive-method)
14710             gnus-server-alist))
14711
14712     ;; If we don't read the complete active file, we fill in the
14713     ;; hashtb here.
14714     (if (or (null gnus-read-active-file)
14715             (eq gnus-read-active-file 'some))
14716         (gnus-update-active-hashtb-from-killed))
14717
14718     ;; Read the active file and create `gnus-active-hashtb'.
14719     ;; If `gnus-read-active-file' is nil, then we just create an empty
14720     ;; hash table.  The partial filling out of the hash table will be
14721     ;; done in `gnus-get-unread-articles'.
14722     (and gnus-read-active-file
14723          (not level)
14724          (gnus-read-active-file))
14725
14726     (or gnus-active-hashtb
14727         (setq gnus-active-hashtb (make-vector 4095 0)))
14728
14729     ;; Initialize the cache.
14730     (when gnus-use-cache
14731       (gnus-cache-open))
14732
14733     ;; Possibly eval the dribble file.
14734     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14735
14736     (gnus-update-format-specifications)
14737
14738     ;; Find new newsgroups and treat them.
14739     (if (and init gnus-check-new-newsgroups (not level)
14740              (gnus-check-server gnus-select-method))
14741         (gnus-find-new-newsgroups))
14742
14743     ;; Find the number of unread articles in each non-dead group.
14744     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14745       (gnus-get-unread-articles level))
14746
14747     (if (and init gnus-check-bogus-newsgroups
14748              gnus-read-active-file (not level)
14749              (gnus-server-opened gnus-select-method))
14750         (gnus-check-bogus-newsgroups))))
14751
14752 (defun gnus-find-new-newsgroups (&optional arg)
14753   "Search for new newsgroups and add them.
14754 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14755 The `-n' option line from .newsrc is respected.
14756 If ARG (the prefix), use the `ask-server' method to query
14757 the server for new groups."
14758   (interactive "P")
14759   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14760                        (null gnus-read-active-file)
14761                        (eq gnus-read-active-file 'some))
14762                    'ask-server gnus-check-new-newsgroups)))
14763     (unless (gnus-check-first-time-used)
14764       (if (or (consp check)
14765               (eq check 'ask-server))
14766           ;; Ask the server for new groups.
14767           (gnus-ask-server-for-new-groups)
14768         ;; Go through the active hashtb and look for new groups.
14769         (let ((groups 0)
14770               group new-newsgroups)
14771           (gnus-message 5 "Looking for new newsgroups...")
14772           (unless gnus-have-read-active-file
14773             (gnus-read-active-file))
14774           (setq gnus-newsrc-last-checked-date (current-time-string))
14775           (unless gnus-killed-hashtb
14776             (gnus-make-hashtable-from-killed))
14777           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14778           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14779           (mapatoms
14780            (lambda (sym)
14781              (if (or (null (setq group (symbol-name sym)))
14782                      (not (boundp sym))
14783                      (null (symbol-value sym))
14784                      (gnus-gethash group gnus-killed-hashtb)
14785                      (gnus-gethash group gnus-newsrc-hashtb))
14786                  ()
14787                (let ((do-sub (gnus-matches-options-n group)))
14788                  (cond
14789                   ((eq do-sub 'subscribe)
14790                    (setq groups (1+ groups))
14791                    (gnus-sethash group group gnus-killed-hashtb)
14792                    (funcall gnus-subscribe-options-newsgroup-method group))
14793                   ((eq do-sub 'ignore)
14794                    nil)
14795                   (t
14796                    (setq groups (1+ groups))
14797                    (gnus-sethash group group gnus-killed-hashtb)
14798                    (if gnus-subscribe-hierarchical-interactive
14799                        (setq new-newsgroups (cons group new-newsgroups))
14800                      (funcall gnus-subscribe-newsgroup-method group)))))))
14801            gnus-active-hashtb)
14802           (when new-newsgroups
14803             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14804           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14805           (if (> groups 0)
14806               (gnus-message 6 "%d new newsgroup%s arrived."
14807                             groups (if (> groups 1) "s have" " has"))
14808             (gnus-message 6 "No new newsgroups.")))))))
14809
14810 (defun gnus-matches-options-n (group)
14811   ;; Returns `subscribe' if the group is to be unconditionally
14812   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14813   ;; no match for the group.
14814
14815   ;; First we check the two user variables.
14816   (cond
14817    ((and gnus-options-subscribe
14818          (string-match gnus-options-subscribe group))
14819     'subscribe)
14820    ((and gnus-auto-subscribed-groups
14821          (string-match gnus-auto-subscribed-groups group))
14822     'subscribe)
14823    ((and gnus-options-not-subscribe
14824          (string-match gnus-options-not-subscribe group))
14825     'ignore)
14826    ;; Then we go through the list that was retrieved from the .newsrc
14827    ;; file.  This list has elements on the form
14828    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14829    ;; is in the reverse order of the options line) is returned.
14830    (t
14831     (let ((regs gnus-newsrc-options-n))
14832       (while (and regs
14833                   (not (string-match (car (car regs)) group)))
14834         (setq regs (cdr regs)))
14835       (and regs (cdr (car regs)))))))
14836
14837 (defun gnus-ask-server-for-new-groups ()
14838   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14839          (methods (cons gnus-select-method
14840                         (cons
14841                          "archive"
14842                          (append
14843                           (and (consp gnus-check-new-newsgroups)
14844                                gnus-check-new-newsgroups)
14845                           gnus-secondary-select-methods))))
14846          (groups 0)
14847          (new-date (current-time-string))
14848          group new-newsgroups got-new method hashtb
14849          gnus-override-subscribe-method)
14850     ;; Go through both primary and secondary select methods and
14851     ;; request new newsgroups.
14852     (while (setq method (gnus-server-get-method nil (pop methods)))
14853       (setq gnus-override-subscribe-method method)
14854       (when (and (gnus-check-server method)
14855                  (gnus-request-newgroups date method))
14856         (save-excursion
14857           (setq got-new t)
14858           (setq hashtb (gnus-make-hashtable 100))
14859           (set-buffer nntp-server-buffer)
14860           ;; Enter all the new groups into a hashtable.
14861           (gnus-active-to-gnus-format method hashtb 'ignore)))
14862       ;; Now all new groups from `method' are in `hashtb'.
14863       (mapatoms
14864        (lambda (group-sym)
14865          (if (or (null (setq group (symbol-name group-sym)))
14866                  (null (symbol-value group-sym))
14867                  (gnus-gethash group gnus-newsrc-hashtb)
14868                  (member group gnus-zombie-list)
14869                  (member group gnus-killed-list))
14870              ;; The group is already known.
14871              ()
14872            ;; Make this group active.
14873            (when (symbol-value group-sym)
14874              (gnus-set-active group (symbol-value group-sym)))
14875            ;; Check whether we want it or not.
14876            (let ((do-sub (gnus-matches-options-n group)))
14877              (cond
14878               ((eq do-sub 'subscribe)
14879                (incf groups)
14880                (gnus-sethash group group gnus-killed-hashtb)
14881                (funcall gnus-subscribe-options-newsgroup-method group))
14882               ((eq do-sub 'ignore)
14883                nil)
14884               (t
14885                (incf groups)
14886                (gnus-sethash group group gnus-killed-hashtb)
14887                (if gnus-subscribe-hierarchical-interactive
14888                    (push group new-newsgroups)
14889                  (funcall gnus-subscribe-newsgroup-method group)))))))
14890        hashtb)
14891       (when new-newsgroups
14892         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
14893     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14894     (when (> groups 0)
14895       (gnus-message 6 "%d new newsgroup%s arrived."
14896                     groups (if (> groups 1) "s have" " has")))
14897     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14898     got-new))
14899
14900 (defun gnus-check-first-time-used ()
14901   (if (or (> (length gnus-newsrc-alist) 1)
14902           (file-exists-p gnus-startup-file)
14903           (file-exists-p (concat gnus-startup-file ".el"))
14904           (file-exists-p (concat gnus-startup-file ".eld")))
14905       nil
14906     (gnus-message 6 "First time user; subscribing you to default groups")
14907     (unless gnus-have-read-active-file
14908       (gnus-read-active-file))
14909     (setq gnus-newsrc-last-checked-date (current-time-string))
14910     (let ((groups gnus-default-subscribed-newsgroups)
14911           group)
14912       (if (eq groups t)
14913           nil
14914         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
14915         (mapatoms
14916          (lambda (sym)
14917            (if (null (setq group (symbol-name sym)))
14918                ()
14919              (let ((do-sub (gnus-matches-options-n group)))
14920                (cond
14921                 ((eq do-sub 'subscribe)
14922                  (gnus-sethash group group gnus-killed-hashtb)
14923                  (funcall gnus-subscribe-options-newsgroup-method group))
14924                 ((eq do-sub 'ignore)
14925                  nil)
14926                 (t
14927                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
14928          gnus-active-hashtb)
14929         (while groups
14930           (if (gnus-active (car groups))
14931               (gnus-group-change-level
14932                (car groups) gnus-level-default-subscribed gnus-level-killed))
14933           (setq groups (cdr groups)))
14934         (gnus-group-make-help-group)
14935         (and gnus-novice-user
14936              (gnus-message 7 "`A k' to list killed groups"))))))
14937
14938 (defun gnus-subscribe-group (group previous &optional method)
14939   (gnus-group-change-level
14940    (if method
14941        (list t group gnus-level-default-subscribed nil nil method)
14942      group)
14943    gnus-level-default-subscribed gnus-level-killed previous t))
14944
14945 ;; `gnus-group-change-level' is the fundamental function for changing
14946 ;; subscription levels of newsgroups.  This might mean just changing
14947 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
14948 ;; again, which subscribes/unsubscribes a group, which is equally
14949 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
14950 ;; from 8-9 to 1-7 means that you remove the group from the list of
14951 ;; killed (or zombie) groups and add them to the (kinda) subscribed
14952 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
14953 ;; which is trivial.
14954 ;; ENTRY can either be a string (newsgroup name) or a list (if
14955 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
14956 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
14957 ;; entries.
14958 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
14959 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
14960 ;; after.
14961 (defun gnus-group-change-level (entry level &optional oldlevel
14962                                       previous fromkilled)
14963   (let (group info active num)
14964     ;; Glean what info we can from the arguments
14965     (if (consp entry)
14966         (if fromkilled (setq group (nth 1 entry))
14967           (setq group (car (nth 2 entry))))
14968       (setq group entry))
14969     (if (and (stringp entry)
14970              oldlevel
14971              (< oldlevel gnus-level-zombie))
14972         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
14973     (if (and (not oldlevel)
14974              (consp entry))
14975         (setq oldlevel (car (cdr (nth 2 entry)))))
14976     (if (stringp previous)
14977         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
14978
14979     (if (and (>= oldlevel gnus-level-zombie)
14980              (gnus-gethash group gnus-newsrc-hashtb))
14981         ;; We are trying to subscribe a group that is already
14982         ;; subscribed.
14983         ()                              ; Do nothing.
14984
14985       (or (gnus-ephemeral-group-p group)
14986           (gnus-dribble-enter
14987            (format "(gnus-group-change-level %S %S %S %S %S)"
14988                    group level oldlevel (car (nth 2 previous)) fromkilled)))
14989
14990       ;; Then we remove the newgroup from any old structures, if needed.
14991       ;; If the group was killed, we remove it from the killed or zombie
14992       ;; list.  If not, and it is in fact going to be killed, we remove
14993       ;; it from the newsrc hash table and assoc.
14994       (cond
14995        ((>= oldlevel gnus-level-zombie)
14996         (if (= oldlevel gnus-level-zombie)
14997             (setq gnus-zombie-list (delete group gnus-zombie-list))
14998           (setq gnus-killed-list (delete group gnus-killed-list))))
14999        (t
15000         (if (and (>= level gnus-level-zombie)
15001                  entry)
15002             (progn
15003               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15004               (if (nth 3 entry)
15005                   (setcdr (gnus-gethash (car (nth 3 entry))
15006                                         gnus-newsrc-hashtb)
15007                           (cdr entry)))
15008               (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
15009
15010       ;; Finally we enter (if needed) the list where it is supposed to
15011       ;; go, and change the subscription level.  If it is to be killed,
15012       ;; we enter it into the killed or zombie list.
15013       (cond 
15014        ((>= level gnus-level-zombie)
15015         ;; Remove from the hash table.
15016         (gnus-sethash group nil gnus-newsrc-hashtb)
15017         ;; We do not enter foreign groups into the list of dead
15018         ;; groups.
15019         (unless (gnus-group-foreign-p group)
15020           (if (= level gnus-level-zombie)
15021               (setq gnus-zombie-list (cons group gnus-zombie-list))
15022             (setq gnus-killed-list (cons group gnus-killed-list)))))
15023        (t
15024         ;; If the list is to be entered into the newsrc assoc, and
15025         ;; it was killed, we have to create an entry in the newsrc
15026         ;; hashtb format and fix the pointers in the newsrc assoc.
15027         (if (< oldlevel gnus-level-zombie)
15028             ;; It was alive, and it is going to stay alive, so we
15029             ;; just change the level and don't change any pointers or
15030             ;; hash table entries.
15031             (setcar (cdr (car (cdr (cdr entry)))) level)
15032           (if (listp entry)
15033               (setq info (cdr entry)
15034                     num (car entry))
15035             (setq active (gnus-active group))
15036             (setq num
15037                   (if active (- (1+ (cdr active)) (car active)) t))
15038             ;; Check whether the group is foreign.  If so, the
15039             ;; foreign select method has to be entered into the
15040             ;; info.
15041             (let ((method (or gnus-override-subscribe-method
15042                               (gnus-group-method group))))
15043               (if (eq method gnus-select-method)
15044                   (setq info (list group level nil))
15045                 (setq info (list group level nil nil method)))))
15046           (unless previous
15047             (setq previous
15048                   (let ((p gnus-newsrc-alist))
15049                     (while (cdr (cdr p))
15050                       (setq p (cdr p)))
15051                     p)))
15052           (setq entry (cons info (cdr (cdr previous))))
15053           (if (cdr previous)
15054               (progn
15055                 (setcdr (cdr previous) entry)
15056                 (gnus-sethash group (cons num (cdr previous))
15057                               gnus-newsrc-hashtb))
15058             (setcdr previous entry)
15059             (gnus-sethash group (cons num previous)
15060                           gnus-newsrc-hashtb))
15061           (when (cdr entry)
15062             (setcdr (gnus-gethash (car (car (cdr entry))) gnus-newsrc-hashtb)
15063                     entry)))))
15064       (when gnus-group-change-level-function
15065         (funcall gnus-group-change-level-function group level oldlevel)))))
15066
15067 (defun gnus-kill-newsgroup (newsgroup)
15068   "Obsolete function.  Kills a newsgroup."
15069   (gnus-group-change-level
15070    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15071
15072 (defun gnus-check-bogus-newsgroups (&optional confirm)
15073   "Remove bogus newsgroups.
15074 If CONFIRM is non-nil, the user has to confirm the deletion of every
15075 newsgroup."
15076   (let ((newsrc (cdr gnus-newsrc-alist))
15077         bogus group entry info)
15078     (gnus-message 5 "Checking bogus newsgroups...")
15079     (unless gnus-have-read-active-file
15080       (gnus-read-active-file))
15081     (when (member gnus-select-method gnus-have-read-active-file)
15082       ;; Find all bogus newsgroup that are subscribed.
15083       (while newsrc
15084         (setq info (pop newsrc)
15085               group (gnus-info-group info))
15086         (unless (or (gnus-active group) ; Active
15087                     (gnus-info-method info) ; Foreign
15088                     (and confirm
15089                          (not (gnus-y-or-n-p
15090                                (format "Remove bogus newsgroup: %s " group)))))
15091           ;; Found a bogus newsgroup.
15092           (push group bogus)))
15093       ;; Remove all bogus subscribed groups by first killing them, and
15094       ;; then removing them from the list of killed groups.
15095       (while bogus
15096         (when (setq entry (gnus-gethash (setq group (pop bogus))
15097                                         gnus-newsrc-hashtb))
15098           (gnus-group-change-level entry gnus-level-killed)
15099           (setq gnus-killed-list (delete group gnus-killed-list))))
15100       ;; Then we remove all bogus groups from the list of killed and
15101       ;; zombie groups.  They are are removed without confirmation.
15102       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15103             killed)
15104         (while dead-lists
15105           (setq killed (symbol-value (car dead-lists)))
15106           (while killed
15107             (unless (gnus-active (setq group (pop killed)))
15108               ;; The group is bogus.
15109               ;; !!!Slow as hell.
15110               (set (car dead-lists)
15111                    (delete group (symbol-value (car dead-lists))))))
15112           (setq dead-lists (cdr dead-lists))))
15113       (gnus-message 5 "Checking bogus newsgroups...done"))))
15114
15115 (defun gnus-check-duplicate-killed-groups ()
15116   "Remove duplicates from the list of killed groups."
15117   (interactive)
15118   (let ((killed gnus-killed-list))
15119     (while killed
15120       (gnus-message 9 "%d" (length killed))
15121       (setcdr killed (delete (car killed) (cdr killed)))
15122       (setq killed (cdr killed)))))
15123
15124 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15125 ;; and compute how many unread articles there are in each group.
15126 (defun gnus-get-unread-articles (&optional level)
15127   (let* ((newsrc (cdr gnus-newsrc-alist))
15128          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15129          (foreign-level
15130           (min
15131            (cond ((and gnus-activate-foreign-newsgroups
15132                        (not (numberp gnus-activate-foreign-newsgroups)))
15133                   (1+ gnus-level-subscribed))
15134                  ((numberp gnus-activate-foreign-newsgroups)
15135                   gnus-activate-foreign-newsgroups)
15136                  (t 0))
15137            level))
15138          info group active virtuals method)
15139     (gnus-message 5 "Checking new news...")
15140
15141     (while newsrc
15142       (setq info (car newsrc)
15143             group (gnus-info-group info)
15144             active (gnus-active group))
15145
15146       ;; Check newsgroups.  If the user doesn't want to check them, or
15147       ;; they can't be checked (for instance, if the news server can't
15148       ;; be reached) we just set the number of unread articles in this
15149       ;; newsgroup to t.  This means that Gnus thinks that there are
15150       ;; unread articles, but it has no idea how many.
15151       (if (and (setq method (gnus-info-method info))
15152                (not (gnus-server-equal
15153                      gnus-select-method
15154                      (gnus-server-get-method nil method)))
15155                (not (gnus-secondary-method-p method)))
15156           ;; These groups are foreign.  Check the level.
15157           (when (<= (gnus-info-level info) foreign-level)
15158             (setq active (gnus-activate-group group 'scan))
15159             (gnus-close-group group))
15160
15161         ;; These groups are native or secondary.
15162         (when (and (<= (gnus-info-level info) level)
15163                    (not gnus-read-active-file))
15164           (setq active (gnus-activate-group group 'scan))
15165           (gnus-close-group group)))
15166
15167       (if active
15168           (gnus-get-unread-articles-in-group info active t)
15169         ;; The group couldn't be reached, so we nix out the number of
15170         ;; unread articles and stuff.
15171         (gnus-set-active group nil)
15172         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
15173
15174       (setq newsrc (cdr newsrc)))
15175
15176     (gnus-message 5 "Checking new news...done")))
15177
15178 ;; Create a hash table out of the newsrc alist.  The `car's of the
15179 ;; alist elements are used as keys.
15180 (defun gnus-make-hashtable-from-newsrc-alist ()
15181   (let ((alist gnus-newsrc-alist)
15182         (ohashtb gnus-newsrc-hashtb)
15183         prev)
15184     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15185     (setq alist
15186           (setq prev (setq gnus-newsrc-alist
15187                            (if (equal (car (car gnus-newsrc-alist))
15188                                       "dummy.group")
15189                                gnus-newsrc-alist
15190                              (cons (list "dummy.group" 0 nil) alist)))))
15191     (while alist
15192       (gnus-sethash
15193        (car (car alist))
15194        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb)))
15195              prev)
15196        gnus-newsrc-hashtb)
15197       (setq prev alist
15198             alist (cdr alist)))))
15199
15200 (defun gnus-make-hashtable-from-killed ()
15201   "Create a hash table from the killed and zombie lists."
15202   (let ((lists '(gnus-killed-list gnus-zombie-list))
15203         list)
15204     (setq gnus-killed-hashtb
15205           (gnus-make-hashtable
15206            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15207     (while lists
15208       (setq list (symbol-value (car lists)))
15209       (setq lists (cdr lists))
15210       (while list
15211         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15212         (setq list (cdr list))))))
15213
15214 (defun gnus-get-unread-articles-in-group (info active &optional update)
15215   (when active
15216     ;; Allow the backend to update the info in the group.
15217     (when update 
15218       (gnus-request-update-info
15219        info (gnus-find-method-for-group (gnus-info-group info))))
15220     (let* ((range (gnus-info-read info))
15221            (num 0)
15222            (marked (gnus-info-marks info)))
15223       ;; If a cache is present, we may have to alter the active info.
15224       (and gnus-use-cache
15225            (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15226       ;; Modify the list of read articles according to what articles
15227       ;; are available; then tally the unread articles and add the
15228       ;; number to the group hash table entry.
15229       (cond
15230        ((zerop (cdr active))
15231         (setq num 0))
15232        ((not range)
15233         (setq num (- (1+ (cdr active)) (car active))))
15234        ((not (listp (cdr range)))
15235         ;; Fix a single (num . num) range according to the
15236         ;; active hash table.
15237         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15238         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15239         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15240         ;; Compute number of unread articles.
15241         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15242        (t
15243         ;; The read list is a list of ranges.  Fix them according to
15244         ;; the active hash table.
15245         ;; First peel off any elements that are below the lower
15246         ;; active limit.
15247         (while (and (cdr range)
15248                     (>= (car active)
15249                         (or (and (atom (car (cdr range))) (car (cdr range)))
15250                             (car (car (cdr range))))))
15251           (if (numberp (car range))
15252               (setcar range
15253                       (cons (car range)
15254                             (or (and (numberp (car (cdr range)))
15255                                      (car (cdr range)))
15256                                 (cdr (car (cdr range))))))
15257             (setcdr (car range)
15258                     (or (and (numberp (nth 1 range)) (nth 1 range))
15259                         (cdr (car (cdr range))))))
15260           (setcdr range (cdr (cdr range))))
15261         ;; Adjust the first element to be the same as the lower limit.
15262         (if (and (not (atom (car range)))
15263                  (< (cdr (car range)) (car active)))
15264             (setcdr (car range) (1- (car active))))
15265         ;; Then we want to peel off any elements that are higher
15266         ;; than the upper active limit.
15267         (let ((srange range))
15268           ;; Go past all legal elements.
15269           (while (and (cdr srange)
15270                       (<= (or (and (atom (car (cdr srange)))
15271                                    (car (cdr srange)))
15272                               (car (car (cdr srange)))) (cdr active)))
15273             (setq srange (cdr srange)))
15274           (if (cdr srange)
15275               ;; Nuke all remaining illegal elements.
15276               (setcdr srange nil))
15277
15278           ;; Adjust the final element.
15279           (if (and (not (atom (car srange)))
15280                    (> (cdr (car srange)) (cdr active)))
15281               (setcdr (car srange) (cdr active))))
15282         ;; Compute the number of unread articles.
15283         (while range
15284           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15285                                       (cdr (car range))))
15286                               (or (and (atom (car range)) (car range))
15287                                   (car (car range))))))
15288           (setq range (cdr range)))
15289         (setq num (max 0 (- (cdr active) num)))))
15290       ;; Set the number of unread articles.
15291       (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
15292       num)))
15293
15294 (defun gnus-activate-group (group &optional scan)
15295   ;; Check whether a group has been activated or not.
15296   ;; If SCAN, request a scan of that group as well.
15297   (let ((method (gnus-find-method-for-group group))
15298         active)
15299     (and (gnus-check-server method)
15300          ;; We escape all bugs and quit here to make it possible to
15301          ;; continue if a group is so out-there that it reports bugs
15302          ;; and stuff.
15303          (progn
15304            (and scan
15305                 (gnus-check-backend-function 'request-scan (car method))
15306                 (gnus-request-scan group method))
15307            t)
15308          (condition-case ()
15309              (gnus-request-group group)
15310         ;   (error nil)
15311            (quit nil))
15312          (save-excursion
15313            (set-buffer nntp-server-buffer)
15314            (goto-char (point-min))
15315            ;; Parse the result we got from `gnus-request-group'.
15316            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15317                 (progn
15318                   (goto-char (match-beginning 1))
15319                   (gnus-set-active
15320                    group (setq active (cons (read (current-buffer))
15321                                             (read (current-buffer)))))
15322                   ;; Return the new active info.
15323                   active))))))
15324
15325 (defun gnus-update-read-articles (group unread)
15326   "Update the list of read and ticked articles in GROUP using the
15327 UNREAD and TICKED lists.
15328 Note: UNSELECTED has to be sorted over `<'.
15329 Returns whether the updating was successful."
15330   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15331          (entry (gnus-gethash group gnus-newsrc-hashtb))
15332          (info (nth 2 entry))
15333          (marked (gnus-info-marks info))
15334          (prev 1)
15335          (unread (sort (copy-sequence unread) '<))
15336          read)
15337     (if (or (not info) (not active))
15338         ;; There is no info on this group if it was, in fact,
15339         ;; killed.  Gnus stores no information on killed groups, so
15340         ;; there's nothing to be done.
15341         ;; One could store the information somewhere temporarily,
15342         ;; perhaps...  Hmmm...
15343         ()
15344       ;; Remove any negative articles numbers.
15345       (while (and unread (< (car unread) 0))
15346         (setq unread (cdr unread)))
15347       ;; Remove any expired article numbers
15348       (while (and unread (< (car unread) (car active)))
15349         (setq unread (cdr unread)))
15350       ;; Compute the ranges of read articles by looking at the list of
15351       ;; unread articles.
15352       (while unread
15353         (if (/= (car unread) prev)
15354             (setq read (cons (if (= prev (1- (car unread))) prev
15355                                (cons prev (1- (car unread)))) read)))
15356         (setq prev (1+ (car unread)))
15357         (setq unread (cdr unread)))
15358       (when (<= prev (cdr active))
15359         (setq read (cons (cons prev (cdr active)) read)))
15360       ;; Enter this list into the group info.
15361       (gnus-info-set-read
15362        info (if (> (length read) 1) (nreverse read) read))
15363       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15364       (gnus-get-unread-articles-in-group info (gnus-active group))
15365       t)))
15366
15367 (defun gnus-make-articles-unread (group articles)
15368   "Mark ARTICLES in GROUP as unread."
15369   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15370                           (gnus-gethash (gnus-group-real-name group)
15371                                         gnus-newsrc-hashtb))))
15372          (ranges (gnus-info-read info))
15373          news article)
15374     (while articles
15375       (when (gnus-member-of-range
15376              (setq article (pop articles)) ranges)
15377         (setq news (cons article news))))
15378     (when news
15379       (gnus-info-set-read
15380        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15381       (gnus-group-update-group group t))))
15382
15383 ;; Enter all dead groups into the hashtb.
15384 (defun gnus-update-active-hashtb-from-killed ()
15385   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15386         (lists (list gnus-killed-list gnus-zombie-list))
15387         killed)
15388     (while lists
15389       (setq killed (car lists))
15390       (while killed
15391         (gnus-sethash (car killed) nil hashtb)
15392         (setq killed (cdr killed)))
15393       (setq lists (cdr lists)))))
15394
15395 ;; Get the active file(s) from the backend(s).
15396 (defun gnus-read-active-file ()
15397   (gnus-group-set-mode-line)
15398   (let ((methods 
15399          (append
15400           (if (gnus-check-server gnus-select-method)
15401               ;; The native server is available.
15402               (cons gnus-select-method gnus-secondary-select-methods)
15403             ;; The native server is down, so we just do the
15404             ;; secondary ones.
15405             gnus-secondary-select-methods)
15406           ;; Also read from the archive server.
15407           (list "archive")))
15408         list-type)
15409     (setq gnus-have-read-active-file nil)
15410     (save-excursion
15411       (set-buffer nntp-server-buffer)
15412       (while methods
15413         (let* ((method (gnus-server-get-method nil (car methods)))
15414                (where (nth 1 method))
15415                (mesg (format "Reading active file%s via %s..."
15416                              (if (and where (not (zerop (length where))))
15417                                  (concat " from " where) "")
15418                              (car method))))
15419           (gnus-message 5 mesg)
15420           (if (not (gnus-check-server method))
15421               ()
15422             ;; Request that the backend scan its incoming messages.
15423             (and (gnus-check-backend-function 'request-scan (car method))
15424                  (gnus-request-scan nil method))
15425             (cond
15426              ((and (eq gnus-read-active-file 'some)
15427                    (gnus-check-backend-function 'retrieve-groups (car method)))
15428               (let ((newsrc (cdr gnus-newsrc-alist))
15429                     (gmethod (gnus-server-get-method nil method))
15430                     groups)
15431                 (while newsrc
15432                   (and (gnus-server-equal
15433                         (gnus-find-method-for-group
15434                          (car (car newsrc)) (car newsrc))
15435                         gmethod)
15436                        (setq groups (cons (gnus-group-real-name
15437                                            (car (car newsrc))) groups)))
15438                   (setq newsrc (cdr newsrc)))
15439                 (gnus-check-server method)
15440                 (setq list-type (gnus-retrieve-groups groups method))
15441                 (cond
15442                  ((not list-type)
15443                   (gnus-message
15444                    1 "Cannot read partial active file from %s server."
15445                    (car method))
15446                   (ding)
15447                   (sit-for 2))
15448                  ((eq list-type 'active)
15449                   (gnus-active-to-gnus-format method gnus-active-hashtb))
15450                  (t
15451                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
15452              (t
15453               (if (not (gnus-request-list method))
15454                   (progn
15455                     (unless (equal method gnus-message-archive-method)
15456                       (gnus-message 1 "Cannot read active file from %s server."
15457                                     (car method))
15458                       (ding)))
15459                 (gnus-active-to-gnus-format method)
15460                 ;; We mark this active file as read.
15461                 (push method gnus-have-read-active-file)
15462                 (gnus-message 5 "%sdone" mesg))))))
15463         (setq methods (cdr methods))))))
15464
15465 ;; Read an active file and place the results in `gnus-active-hashtb'.
15466 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15467   (unless method
15468     (setq method gnus-select-method))
15469   (let ((cur (current-buffer))
15470         (hashtb (or hashtb
15471                     (if (and gnus-active-hashtb
15472                              (not (equal method gnus-select-method)))
15473                         gnus-active-hashtb
15474                       (setq gnus-active-hashtb
15475                             (if (equal method gnus-select-method)
15476                                 (gnus-make-hashtable
15477                                  (count-lines (point-min) (point-max)))
15478                               (gnus-make-hashtable 4096))))))
15479         (flag-hashtb (gnus-make-hashtable 60)))
15480     ;; Delete unnecessary lines.
15481     (goto-char (point-min))
15482     (while (search-forward "\nto." nil t)
15483       (delete-region (1+ (match-beginning 0))
15484                      (progn (forward-line 1) (point))))
15485     (or (string= gnus-ignored-newsgroups "")
15486         (progn
15487           (goto-char (point-min))
15488           (delete-matching-lines gnus-ignored-newsgroups)))
15489     ;; Make the group names readable as a lisp expression even if they
15490     ;; contain special characters.
15491     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15492     (goto-char (point-max))
15493     (while (re-search-backward "[][';?()#]" nil t)
15494       (insert ?\\))
15495     ;; If these are groups from a foreign select method, we insert the
15496     ;; group prefix in front of the group names.
15497     (and method (not (gnus-server-equal
15498                       (gnus-server-get-method nil method)
15499                       (gnus-server-get-method nil gnus-select-method)))
15500          (let ((prefix (gnus-group-prefixed-name "" method)))
15501            (goto-char (point-min))
15502            (while (and (not (eobp))
15503                        (progn (insert prefix)
15504                               (zerop (forward-line 1)))))))
15505     ;; Store the active file in a hash table.
15506     (goto-char (point-min))
15507     (if (string-match "%[oO]" gnus-group-line-format)
15508         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15509         ;; If we want information on moderated groups, we use this
15510         ;; loop...
15511         (let* ((mod-hashtb (make-vector 7 0))
15512                (m (intern "m" mod-hashtb))
15513                group max min)
15514           (while (not (eobp))
15515             (condition-case nil
15516                 (progn
15517                   (narrow-to-region (point) (gnus-point-at-eol))
15518                   (setq group (let ((obarray hashtb)) (read cur)))
15519                   (if (and (numberp (setq max (read cur)))
15520                            (numberp (setq min (read cur)))
15521                            (progn
15522                              (skip-chars-forward " \t")
15523                              (not
15524                               (or (= (following-char) ?=)
15525                                   (= (following-char) ?x)
15526                                   (= (following-char) ?j)))))
15527                       (set group (cons min max))
15528                     (set group nil))
15529                   ;; Enter moderated groups into a list.
15530                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15531                       (setq gnus-moderated-list
15532                             (cons (symbol-name group) gnus-moderated-list))))
15533               (error
15534                (and group
15535                     (symbolp group)
15536                     (set group nil))))
15537             (widen)
15538             (forward-line 1)))
15539       ;; And if we do not care about moderation, we use this loop,
15540       ;; which is faster.
15541       (let (group max min)
15542         (while (not (eobp))
15543           (condition-case ()
15544               (progn
15545                 (narrow-to-region (point) (gnus-point-at-eol))
15546                 ;; group gets set to a symbol interned in the hash table
15547                 ;; (what a hack!!) - jwz
15548                 (setq group (let ((obarray hashtb)) (read cur)))
15549                 (if (and (numberp (setq max (read cur)))
15550                          (numberp (setq min (read cur)))
15551                          (progn
15552                            (skip-chars-forward " \t")
15553                            (not
15554                             (or (= (following-char) ?=)
15555                                 (= (following-char) ?x)
15556                                 (= (following-char) ?j)))))
15557                     (set group (cons min max))
15558                   (set group nil)))
15559             (error
15560              (progn
15561                (and group
15562                     (symbolp group)
15563                     (set group nil))
15564                (or ignore-errors
15565                    (gnus-message 3 "Warning - illegal active: %s"
15566                                  (buffer-substring
15567                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15568           (widen)
15569           (forward-line 1))))))
15570
15571 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15572   ;; Parse a "groups" active file.
15573   (let ((cur (current-buffer))
15574         (hashtb (or hashtb
15575                     (if (and method gnus-active-hashtb)
15576                         gnus-active-hashtb
15577                       (setq gnus-active-hashtb
15578                             (gnus-make-hashtable
15579                              (count-lines (point-min) (point-max)))))))
15580         (prefix (and method
15581                      (not (gnus-server-equal
15582                            (gnus-server-get-method nil method)
15583                            (gnus-server-get-method nil gnus-select-method)))
15584                      (gnus-group-prefixed-name "" method))))
15585
15586     (goto-char (point-min))
15587     ;; We split this into to separate loops, one with the prefix
15588     ;; and one without to speed the reading up somewhat.
15589     (if prefix
15590         (let (min max opoint group)
15591           (while (not (eobp))
15592             (condition-case ()
15593                 (progn
15594                   (read cur) (read cur)
15595                   (setq min (read cur)
15596                         max (read cur)
15597                         opoint (point))
15598                   (skip-chars-forward " \t")
15599                   (insert prefix)
15600                   (goto-char opoint)
15601                   (set (let ((obarray hashtb)) (read cur))
15602                        (cons min max)))
15603               (error (and group (symbolp group) (set group nil))))
15604             (forward-line 1)))
15605       (let (min max group)
15606         (while (not (eobp))
15607           (condition-case ()
15608               (if (= (following-char) ?2)
15609                   (progn
15610                     (read cur) (read cur)
15611                     (setq min (read cur)
15612                           max (read cur))
15613                     (set (setq group (let ((obarray hashtb)) (read cur)))
15614                          (cons min max))))
15615             (error (and group (symbolp group) (set group nil))))
15616           (forward-line 1))))))
15617
15618 (defun gnus-read-newsrc-file (&optional force)
15619   "Read startup file.
15620 If FORCE is non-nil, the .newsrc file is read."
15621   ;; Reset variables that might be defined in the .newsrc.eld file.
15622   (let ((variables gnus-variable-list))
15623     (while variables
15624       (set (car variables) nil)
15625       (setq variables (cdr variables))))
15626   (let* ((newsrc-file gnus-current-startup-file)
15627          (quick-file (concat newsrc-file ".el")))
15628     (save-excursion
15629       ;; We always load the .newsrc.eld file.  If always contains
15630       ;; much information that can not be gotten from the .newsrc
15631       ;; file (ticked articles, killed groups, foreign methods, etc.)
15632       (gnus-read-newsrc-el-file quick-file)
15633
15634       (if (or force
15635               (and (file-newer-than-file-p newsrc-file quick-file)
15636                    (file-newer-than-file-p newsrc-file
15637                                            (concat quick-file "d")))
15638               (not gnus-newsrc-alist))
15639           ;; We read the .newsrc file.  Note that if there if a
15640           ;; .newsrc.eld file exists, it has already been read, and
15641           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15642           ;; the .newsrc file, Gnus will only use the information it
15643           ;; can find there for changing the data already read -
15644           ;; ie. reading the .newsrc file will not trash the data
15645           ;; already read (except for read articles).
15646           (save-excursion
15647             (gnus-message 5 "Reading %s..." newsrc-file)
15648             (set-buffer (find-file-noselect newsrc-file))
15649             (buffer-disable-undo (current-buffer))
15650             (gnus-newsrc-to-gnus-format)
15651             (kill-buffer (current-buffer))
15652             (gnus-message 5 "Reading %s...done" newsrc-file)))
15653
15654       ;; Read any slave files.
15655       (or gnus-slave
15656           (gnus-master-read-slave-newsrc)))))
15657
15658 (defun gnus-read-newsrc-el-file (file)
15659   (let ((ding-file (concat file "d")))
15660     ;; We always, always read the .eld file.
15661     (gnus-message 5 "Reading %s..." ding-file)
15662     (let (gnus-newsrc-assoc)
15663       (condition-case nil
15664           (load ding-file t t t)
15665         (error
15666          (gnus-message 1 "Error in %s" ding-file)
15667          (ding)))
15668       (when gnus-newsrc-assoc
15669         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15670     (gnus-make-hashtable-from-newsrc-alist)
15671     (when (file-newer-than-file-p file ding-file)
15672       ;; Old format quick file
15673       (gnus-message 5 "Reading %s..." file)
15674       ;; The .el file is newer than the .eld file, so we read that one
15675       ;; as well.
15676       (gnus-read-old-newsrc-el-file file))))
15677
15678 ;; Parse the old-style quick startup file
15679 (defun gnus-read-old-newsrc-el-file (file)
15680   (let (newsrc killed marked group m)
15681     (prog1
15682         (let ((gnus-killed-assoc nil)
15683               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15684           (prog1
15685               (condition-case nil
15686                   (load file t t t)
15687                 (error nil))
15688             (setq newsrc gnus-newsrc-assoc
15689                   killed gnus-killed-assoc
15690                   marked gnus-marked-assoc)))
15691       (setq gnus-newsrc-alist nil)
15692       (while newsrc
15693         (setq group (car newsrc))
15694         (let ((info (gnus-get-info (car group))))
15695           (if info
15696               (progn
15697                 (gnus-info-set-read info (cdr (cdr group)))
15698                 (gnus-info-set-level
15699                  info (if (nth 1 group) gnus-level-default-subscribed
15700                         gnus-level-default-unsubscribed))
15701                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15702             (setq gnus-newsrc-alist
15703                   (cons
15704                    (setq info
15705                          (list (car group)
15706                                (if (nth 1 group) gnus-level-default-subscribed
15707                                  gnus-level-default-unsubscribed)
15708                                (cdr (cdr group))))
15709                    gnus-newsrc-alist)))
15710           (if (setq m (assoc (car group) marked))
15711               (gnus-info-set-marks
15712                info (cons (list (cons 'tick (gnus-compress-sequence
15713                                              (sort (cdr m) '<) t)))
15714                           nil))))
15715         (setq newsrc (cdr newsrc)))
15716       (setq newsrc killed)
15717       (while newsrc
15718         (setcar newsrc (car (car newsrc)))
15719         (setq newsrc (cdr newsrc)))
15720       (setq gnus-killed-list killed))
15721     ;; The .el file version of this variable does not begin with
15722     ;; "options", while the .eld version does, so we just add it if it
15723     ;; isn't there.
15724     (and
15725      gnus-newsrc-options
15726      (progn
15727        (and (not (string-match "^ *options" gnus-newsrc-options))
15728             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15729        (and (not (string-match "\n$" gnus-newsrc-options))
15730             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15731        ;; Finally, if we read some options lines, we parse them.
15732        (or (string= gnus-newsrc-options "")
15733            (gnus-newsrc-parse-options gnus-newsrc-options))))
15734
15735     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15736     (gnus-make-hashtable-from-newsrc-alist)))
15737
15738 (defun gnus-make-newsrc-file (file)
15739   "Make server dependent file name by catenating FILE and server host name."
15740   (let* ((file (expand-file-name file nil))
15741          (real-file (concat file "-" (nth 1 gnus-select-method))))
15742     (if (or (file-exists-p real-file)
15743             (file-exists-p (concat real-file ".el"))
15744             (file-exists-p (concat real-file ".eld")))
15745         real-file file)))
15746
15747 (defun gnus-newsrc-to-gnus-format ()
15748   (setq gnus-newsrc-options "")
15749   (setq gnus-newsrc-options-n nil)
15750
15751   (or gnus-active-hashtb
15752       (setq gnus-active-hashtb (make-vector 4095 0)))
15753   (let ((buf (current-buffer))
15754         (already-read (> (length gnus-newsrc-alist) 1))
15755         group subscribed options-symbol newsrc Options-symbol
15756         symbol reads num1)
15757     (goto-char (point-min))
15758     ;; We intern the symbol `options' in the active hashtb so that we
15759     ;; can `eq' against it later.
15760     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15761     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15762
15763     (while (not (eobp))
15764       ;; We first read the first word on the line by narrowing and
15765       ;; then reading into `gnus-active-hashtb'.  Most groups will
15766       ;; already exist in that hashtb, so this will save some string
15767       ;; space.
15768       (narrow-to-region
15769        (point)
15770        (progn (skip-chars-forward "^ \t!:\n") (point)))
15771       (goto-char (point-min))
15772       (setq symbol
15773             (and (/= (point-min) (point-max))
15774                  (let ((obarray gnus-active-hashtb)) (read buf))))
15775       (widen)
15776       ;; Now, the symbol we have read is either `options' or a group
15777       ;; name.  If it is an options line, we just add it to a string.
15778       (cond
15779        ((or (eq symbol options-symbol)
15780             (eq symbol Options-symbol))
15781         (setq gnus-newsrc-options
15782               ;; This concating is quite inefficient, but since our
15783               ;; thorough studies show that approx 99.37% of all
15784               ;; .newsrc files only contain a single options line, we
15785               ;; don't give a damn, frankly, my dear.
15786               (concat gnus-newsrc-options
15787                       (buffer-substring
15788                        (gnus-point-at-bol)
15789                        ;; Options may continue on the next line.
15790                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15791                                 (progn (beginning-of-line) (point)))
15792                            (point)))))
15793         (forward-line -1))
15794        (symbol
15795         ;; Group names can be just numbers.  
15796         (when (numberp symbol) 
15797           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15798         (or (boundp symbol) (set symbol nil))
15799         ;; It was a group name.
15800         (setq subscribed (= (following-char) ?:)
15801               group (symbol-name symbol)
15802               reads nil)
15803         (if (eolp)
15804             ;; If the line ends here, this is clearly a buggy line, so
15805             ;; we put point a the beginning of line and let the cond
15806             ;; below do the error handling.
15807             (beginning-of-line)
15808           ;; We skip to the beginning of the ranges.
15809           (skip-chars-forward "!: \t"))
15810         ;; We are now at the beginning of the list of read articles.
15811         ;; We read them range by range.
15812         (while
15813             (cond
15814              ((looking-at "[0-9]+")
15815               ;; We narrow and read a number instead of buffer-substring/
15816               ;; string-to-int because it's faster.  narrow/widen is
15817               ;; faster than save-restriction/narrow, and save-restriction
15818               ;; produces a garbage object.
15819               (setq num1 (progn
15820                            (narrow-to-region (match-beginning 0) (match-end 0))
15821                            (read buf)))
15822               (widen)
15823               ;; If the next character is a dash, then this is a range.
15824               (if (= (following-char) ?-)
15825                   (progn
15826                     ;; We read the upper bound of the range.
15827                     (forward-char 1)
15828                     (if (not (looking-at "[0-9]+"))
15829                         ;; This is a buggy line, by we pretend that
15830                         ;; it's kinda OK.  Perhaps the user should be
15831                         ;; dinged?
15832                         (setq reads (cons num1 reads))
15833                       (setq reads
15834                             (cons
15835                              (cons num1
15836                                    (progn
15837                                      (narrow-to-region (match-beginning 0)
15838                                                        (match-end 0))
15839                                      (read buf)))
15840                              reads))
15841                       (widen)))
15842                 ;; It was just a simple number, so we add it to the
15843                 ;; list of ranges.
15844                 (setq reads (cons num1 reads)))
15845               ;; If the next char in ?\n, then we have reached the end
15846               ;; of the line and return nil.
15847               (/= (following-char) ?\n))
15848              ((= (following-char) ?\n)
15849               ;; End of line, so we end.
15850               nil)
15851              (t
15852               ;; Not numbers and not eol, so this might be a buggy
15853               ;; line...
15854               (or (eobp)
15855                   ;; If it was eob instead of ?\n, we allow it.
15856                   (progn
15857                     ;; The line was buggy.
15858                     (setq group nil)
15859                     (gnus-message 3 "Mangled line: %s"
15860                                   (buffer-substring (gnus-point-at-bol)
15861                                                     (gnus-point-at-eol)))
15862                     (ding)
15863                     (sit-for 1)))
15864               nil))
15865           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15866           ;; we allow them, because it's a common mistake to put a
15867           ;; space after the comma.
15868           (skip-chars-forward ", "))
15869
15870         ;; We have already read .newsrc.eld, so we gently update the
15871         ;; data in the hash table with the information we have just
15872         ;; read.
15873         (when group
15874           (let ((info (gnus-get-info group))
15875                 level)
15876             (if info
15877                 ;; There is an entry for this file in the alist.
15878                 (progn
15879                   (gnus-info-set-read info (nreverse reads))
15880                   ;; We update the level very gently.  In fact, we
15881                   ;; only change it if there's been a status change
15882                   ;; from subscribed to unsubscribed, or vice versa.
15883                   (setq level (gnus-info-level info))
15884                   (cond ((and (<= level gnus-level-subscribed)
15885                               (not subscribed))
15886                          (setq level (if reads
15887                                          gnus-level-default-unsubscribed
15888                                        (1+ gnus-level-default-unsubscribed))))
15889                         ((and (> level gnus-level-subscribed) subscribed)
15890                          (setq level gnus-level-default-subscribed)))
15891                   (gnus-info-set-level info level))
15892               ;; This is a new group.
15893               (setq info (list group
15894                                (if subscribed
15895                                    gnus-level-default-subscribed
15896                                  (if reads
15897                                      (1+ gnus-level-subscribed)
15898                                    gnus-level-default-unsubscribed))
15899                                (nreverse reads))))
15900             (setq newsrc (cons info newsrc))))))
15901       (forward-line 1))
15902
15903     (setq newsrc (nreverse newsrc))
15904
15905     (if (not already-read)
15906         ()
15907       ;; We now have two newsrc lists - `newsrc', which is what we
15908       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15909       ;; what we've read from .newsrc.eld.  We have to merge these
15910       ;; lists.  We do this by "attaching" any (foreign) groups in the
15911       ;; gnus-newsrc-alist to the (native) group that precedes them.
15912       (let ((rc (cdr gnus-newsrc-alist))
15913             (prev gnus-newsrc-alist)
15914             entry mentry)
15915         (while rc
15916           (or (null (nth 4 (car rc)))   ; It's a native group.
15917               (assoc (car (car rc)) newsrc) ; It's already in the alist.
15918               (if (setq entry (assoc (car (car prev)) newsrc))
15919                   (setcdr (setq mentry (memq entry newsrc))
15920                           (cons (car rc) (cdr mentry)))
15921                 (setq newsrc (cons (car rc) newsrc))))
15922           (setq prev rc
15923                 rc (cdr rc)))))
15924
15925     (setq gnus-newsrc-alist newsrc)
15926     ;; We make the newsrc hashtb.
15927     (gnus-make-hashtable-from-newsrc-alist)
15928
15929     ;; Finally, if we read some options lines, we parse them.
15930     (or (string= gnus-newsrc-options "")
15931         (gnus-newsrc-parse-options gnus-newsrc-options))))
15932
15933 ;; Parse options lines to find "options -n !all rec.all" and stuff.
15934 ;; The return value will be a list on the form
15935 ;; ((regexp1 . ignore)
15936 ;;  (regexp2 . subscribe)...)
15937 ;; When handling new newsgroups, groups that match a `ignore' regexp
15938 ;; will be ignored, and groups that match a `subscribe' regexp will be
15939 ;; subscribed.  A line like
15940 ;; options -n !all rec.all
15941 ;; will lead to a list that looks like
15942 ;; (("^rec\\..+" . subscribe)
15943 ;;  ("^.+" . ignore))
15944 ;; So all "rec.*" groups will be subscribed, while all the other
15945 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
15946 ;; different from "options -n rec.all !all".
15947 (defun gnus-newsrc-parse-options (options)
15948   (let (out eol)
15949     (save-excursion
15950       (gnus-set-work-buffer)
15951       (insert (regexp-quote options))
15952       ;; First we treat all continuation lines.
15953       (goto-char (point-min))
15954       (while (re-search-forward "\n[ \t]+" nil t)
15955         (replace-match " " t t))
15956       ;; Then we transform all "all"s into ".+"s.
15957       (goto-char (point-min))
15958       (while (re-search-forward "\\ball\\b" nil t)
15959         (replace-match ".+" t t))
15960       (goto-char (point-min))
15961       ;; We remove all other options than the "-n" ones.
15962       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
15963         (replace-match " ")
15964         (forward-char -1))
15965       (goto-char (point-min))
15966
15967       ;; We are only interested in "options -n" lines - we
15968       ;; ignore the other option lines.
15969       (while (re-search-forward "[ \t]-n" nil t)
15970         (setq eol
15971               (or (save-excursion
15972                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
15973                          (- (point) 2)))
15974                   (gnus-point-at-eol)))
15975         ;; Search for all "words"...
15976         (while (re-search-forward "[^ \t,\n]+" eol t)
15977           (if (= (char-after (match-beginning 0)) ?!)
15978               ;; If the word begins with a bang (!), this is a "not"
15979               ;; spec.  We put this spec (minus the bang) and the
15980               ;; symbol `ignore' into the list.
15981               (setq out (cons (cons (concat
15982                                      "^" (buffer-substring
15983                                           (1+ (match-beginning 0))
15984                                           (match-end 0)))
15985                                     'ignore) out))
15986             ;; There was no bang, so this is a "yes" spec.
15987             (setq out (cons (cons (concat "^" (match-string 0))
15988                                   'subscribe) out)))))
15989
15990       (setq gnus-newsrc-options-n out))))
15991
15992 (defun gnus-save-newsrc-file (&optional force)
15993   "Save .newsrc file."
15994   ;; Note: We cannot save .newsrc file if all newsgroups are removed
15995   ;; from the variable gnus-newsrc-alist.
15996   (when (and (or gnus-newsrc-alist gnus-killed-list)
15997              gnus-current-startup-file)
15998     (save-excursion
15999       (if (and (or gnus-use-dribble-file gnus-slave)
16000                (not force)
16001                (or (not gnus-dribble-buffer)
16002                    (not (buffer-name gnus-dribble-buffer))
16003                    (zerop (save-excursion
16004                             (set-buffer gnus-dribble-buffer)
16005                             (buffer-size)))))
16006           (gnus-message 4 "(No changes need to be saved)")
16007         (run-hooks 'gnus-save-newsrc-hook)
16008         (if gnus-slave
16009             (gnus-slave-save-newsrc)
16010           ;; Save .newsrc.
16011           (when gnus-save-newsrc-file
16012             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16013             (gnus-gnus-to-newsrc-format)
16014             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16015           ;; Save .newsrc.eld.
16016           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16017           (make-local-variable 'version-control)
16018           (setq version-control 'never)
16019           (setq buffer-file-name
16020                 (concat gnus-current-startup-file ".eld"))
16021           (gnus-add-current-to-buffer-list)
16022           (buffer-disable-undo (current-buffer))
16023           (erase-buffer)
16024           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16025           (gnus-gnus-to-quick-newsrc-format)
16026           (run-hooks 'gnus-save-quick-newsrc-hook)
16027           (save-buffer)
16028           (kill-buffer (current-buffer))
16029           (gnus-message
16030            5 "Saving %s.eld...done" gnus-current-startup-file))
16031         (gnus-dribble-delete-file)))))
16032
16033 (defun gnus-gnus-to-quick-newsrc-format ()
16034   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16035   (insert ";; Gnus startup file.\n")
16036   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16037   (insert ";; to read .newsrc.\n")
16038   (insert "(setq gnus-newsrc-file-version "
16039           (prin1-to-string gnus-version) ")\n")
16040   (let ((variables
16041          (if gnus-save-killed-list gnus-variable-list
16042            ;; Remove the `gnus-killed-list' from the list of variables
16043            ;; to be saved, if required.
16044            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16045         ;; Peel off the "dummy" group.
16046         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16047         variable)
16048     ;; Insert the variables into the file.
16049     (while variables
16050       (when (and (boundp (setq variable (pop variables)))
16051                  (symbol-value variable))
16052         (insert "(setq " (symbol-name variable) " '"
16053                 (prin1-to-string (symbol-value variable)) ")\n")))))
16054
16055 (defun gnus-gnus-to-newsrc-format ()
16056   ;; Generate and save the .newsrc file.
16057   (let ((newsrc (cdr gnus-newsrc-alist))
16058         info ranges range)
16059     (save-excursion
16060       (set-buffer (create-file-buffer gnus-current-startup-file))
16061       (setq buffer-file-name gnus-current-startup-file)
16062       (buffer-disable-undo (current-buffer))
16063       (erase-buffer)
16064       ;; Write options.
16065       (if gnus-newsrc-options (insert gnus-newsrc-options))
16066       ;; Write subscribed and unsubscribed.
16067       (while newsrc
16068         (setq info (car newsrc))
16069         (if (not (gnus-info-method info))
16070             ;; Don't write foreign groups to .newsrc.
16071             (progn
16072               (insert (gnus-info-group info)
16073                       (if (> (gnus-info-level info) gnus-level-subscribed)
16074                           "!" ":"))
16075               (if (setq ranges (gnus-info-read info))
16076                   (progn
16077                     (insert " ")
16078                     (if (not (listp (cdr ranges)))
16079                         (if (= (car ranges) (cdr ranges))
16080                             (insert (int-to-string (car ranges)))
16081                           (insert (int-to-string (car ranges)) "-"
16082                                   (int-to-string (cdr ranges))))
16083                       (while ranges
16084                         (setq range (car ranges)
16085                               ranges (cdr ranges))
16086                         (if (or (atom range) (= (car range) (cdr range)))
16087                             (insert (int-to-string
16088                                      (or (and (atom range) range)
16089                                          (car range))))
16090                           (insert (int-to-string (car range)) "-"
16091                                   (int-to-string (cdr range))))
16092                         (if ranges (insert ","))))))
16093               (insert "\n")))
16094         (setq newsrc (cdr newsrc)))
16095       (make-local-variable 'version-control)
16096       (setq version-control 'never)
16097       ;; It has been reported that sometime the modtime on the .newsrc
16098       ;; file seems to be off.  We really do want to overwrite it, so
16099       ;; we clear the modtime here before saving.  It's a bit odd,
16100       ;; though...
16101       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16102       ;; delete the silly thing entirely first.  but this fails to provide
16103       ;; such niceties as .newsrc~ creation.
16104       (if gnus-modtime-botch
16105           (delete-file gnus-startup-file)
16106         (clear-visited-file-modtime))
16107       (run-hooks 'gnus-save-standard-newsrc-hook)
16108       (save-buffer)
16109       (kill-buffer (current-buffer)))))
16110
16111 \f
16112 ;;;
16113 ;;; Slave functions.
16114 ;;;
16115
16116 (defun gnus-slave-save-newsrc ()
16117   (save-excursion
16118     (set-buffer gnus-dribble-buffer)
16119     (let ((slave-name
16120            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16121       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16122
16123 (defun gnus-master-read-slave-newsrc ()
16124   (let ((slave-files
16125          (directory-files
16126           (file-name-directory gnus-current-startup-file)
16127           t (concat
16128              "^" (regexp-quote
16129                   (concat
16130                    (file-name-nondirectory gnus-current-startup-file)
16131                    "-slave-")))
16132           t))
16133         file)
16134     (if (not slave-files)
16135         ()                              ; There are no slave files to read.
16136       (gnus-message 7 "Reading slave newsrcs...")
16137       (save-excursion
16138         (set-buffer (get-buffer-create " *gnus slave*"))
16139         (buffer-disable-undo (current-buffer))
16140         (setq slave-files
16141               (sort (mapcar (lambda (file)
16142                               (list (nth 5 (file-attributes file)) file))
16143                             slave-files)
16144                     (lambda (f1 f2)
16145                       (or (< (car (car f1)) (car (car f2)))
16146                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16147         (while slave-files
16148           (erase-buffer)
16149           (setq file (nth 1 (car slave-files)))
16150           (insert-file-contents file)
16151           (if (condition-case ()
16152                   (progn
16153                     (eval-buffer (current-buffer))
16154                     t)
16155                 (error
16156                  (gnus-message 3 "Possible error in %s" file)
16157                  (ding)
16158                  (sit-for 2)
16159                  nil))
16160               (or gnus-slave ; Slaves shouldn't delete these files.
16161                   (condition-case ()
16162                       (delete-file file)
16163                     (error nil))))
16164           (setq slave-files (cdr slave-files))))
16165       (gnus-message 7 "Reading slave newsrcs...done"))))
16166
16167 \f
16168 ;;;
16169 ;;; Group description.
16170 ;;;
16171
16172 (defun gnus-read-all-descriptions-files ()
16173   (let ((methods (cons gnus-select-method 
16174                        (cons "archive"
16175                              gnus-secondary-select-methods))))
16176     (while methods
16177       (gnus-read-descriptions-file (car methods))
16178       (setq methods (cdr methods)))
16179     t))
16180
16181 (defun gnus-read-descriptions-file (&optional method)
16182   (let ((method (or method gnus-select-method)))
16183     (when (stringp method)
16184       (setq method (gnus-server-to-method method)))
16185     ;; We create the hashtable whether we manage to read the desc file
16186     ;; to avoid trying to re-read after a failed read.
16187     (or gnus-description-hashtb
16188         (setq gnus-description-hashtb
16189               (gnus-make-hashtable (length gnus-active-hashtb))))
16190     ;; Mark this method's desc file as read.
16191     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16192                   gnus-description-hashtb)
16193
16194     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16195     (cond
16196      ((not (gnus-check-server method))
16197       (gnus-message 1 "Couldn't open server")
16198       nil)
16199      ((not (gnus-request-list-newsgroups method))
16200       (gnus-message 1 "Couldn't read newsgroups descriptions")
16201       nil)
16202      (t
16203       (let (group)
16204         (save-excursion
16205           (save-restriction
16206             (set-buffer nntp-server-buffer)
16207             (goto-char (point-min))
16208             (if (or (search-forward "\n.\n" nil t)
16209                     (goto-char (point-max)))
16210                 (progn
16211                   (beginning-of-line)
16212                   (narrow-to-region (point-min) (point))))
16213             (goto-char (point-min))
16214             (while (not (eobp))
16215               ;; If we get an error, we set group to 0, which is not a
16216               ;; symbol...
16217               (setq group
16218                     (condition-case ()
16219                         (let ((obarray gnus-description-hashtb))
16220                           ;; Group is set to a symbol interned in this
16221                           ;; hash table.
16222                           (read nntp-server-buffer))
16223                       (error 0)))
16224               (skip-chars-forward " \t")
16225               ;; ...  which leads to this line being effectively ignored.
16226               (and (symbolp group)
16227                    (set group (buffer-substring
16228                                (point) (progn (end-of-line) (point)))))
16229               (forward-line 1))))
16230         (gnus-message 5 "Reading descriptions file...done")
16231         t)))))
16232
16233 (defun gnus-group-get-description (group)
16234   "Get the description of a group by sending XGTITLE to the server."
16235   (when (gnus-request-group-description group)
16236     (save-excursion
16237       (set-buffer nntp-server-buffer)
16238       (goto-char (point-min))
16239       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16240         (match-string 1)))))
16241
16242 \f
16243 ;;;
16244 ;;; Buffering of read articles.
16245 ;;;
16246
16247 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16248 (defvar gnus-backlog-articles nil)
16249 (defvar gnus-backlog-hashtb nil)
16250
16251 (defun gnus-backlog-buffer ()
16252   "Return the backlog buffer."
16253   (or (get-buffer gnus-backlog-buffer)
16254       (save-excursion
16255         (set-buffer (get-buffer-create gnus-backlog-buffer))
16256         (buffer-disable-undo (current-buffer))
16257         (setq buffer-read-only t)
16258         (gnus-add-current-to-buffer-list)
16259         (get-buffer gnus-backlog-buffer))))
16260
16261 (defun gnus-backlog-setup ()
16262   "Initialize backlog variables."
16263   (unless gnus-backlog-hashtb
16264     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16265
16266 (defun gnus-backlog-shutdown ()
16267   "Clear all backlog variables and buffers."
16268   (when (get-buffer gnus-backlog-buffer)
16269     (kill-buffer gnus-backlog-buffer))
16270   (setq gnus-backlog-hashtb nil
16271         gnus-backlog-articles nil))
16272
16273 (defun gnus-backlog-enter-article (group number buffer)
16274   (gnus-backlog-setup)
16275   (let ((ident (intern (concat group ":" (int-to-string number))
16276                        gnus-backlog-hashtb))
16277         b)
16278     (if (memq ident gnus-backlog-articles)
16279         () ; It's already kept.
16280       ;; Remove the oldest article, if necessary.
16281       (and (numberp gnus-keep-backlog)
16282            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16283            (gnus-backlog-remove-oldest-article))
16284       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16285       ;; Insert the new article.
16286       (save-excursion
16287         (set-buffer (gnus-backlog-buffer))
16288         (let (buffer-read-only)
16289           (goto-char (point-max))
16290           (or (bolp) (insert "\n"))
16291           (setq b (point))
16292           (insert-buffer-substring buffer)
16293           ;; Tag the beginning of the article with the ident.
16294           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16295
16296 (defun gnus-backlog-remove-oldest-article ()
16297   (save-excursion
16298     (set-buffer (gnus-backlog-buffer))
16299     (goto-char (point-min))
16300     (if (zerop (buffer-size))
16301         () ; The buffer is empty.
16302       (let ((ident (get-text-property (point) 'gnus-backlog))
16303             buffer-read-only)
16304         ;; Remove the ident from the list of articles.
16305         (when ident
16306           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16307         ;; Delete the article itself.
16308         (delete-region
16309          (point) (next-single-property-change
16310                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16311
16312 (defun gnus-backlog-request-article (group number buffer)
16313   (gnus-backlog-setup)
16314   (let ((ident (intern (concat group ":" (int-to-string number))
16315                        gnus-backlog-hashtb))
16316         beg end)
16317     (when (memq ident gnus-backlog-articles)
16318       ;; It was in the backlog.
16319       (save-excursion
16320         (set-buffer (gnus-backlog-buffer))
16321         (if (not (setq beg (text-property-any
16322                             (point-min) (point-max) 'gnus-backlog
16323                             ident)))
16324             ;; It wasn't in the backlog after all.
16325             (ignore
16326              (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16327           ;; Find the end (i. e., the beginning of the next article).
16328           (setq end
16329                 (next-single-property-change
16330                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16331       (let ((buffer-read-only nil))
16332         (erase-buffer)
16333         (insert-buffer-substring gnus-backlog-buffer beg end)
16334         t))))
16335
16336 ;; Allow redefinition of Gnus functions.
16337
16338 (gnus-ems-redefine)
16339
16340 (provide 'gnus)
16341
16342 ;;; gnus.el ends here